ehci-hcd.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*-
  2. * Copyright (c) 2007-2008, Juniper Networks, Inc.
  3. * Copyright (c) 2008, Excito Elektronik i Skåne AB
  4. * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2 of
  11. * the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <errno.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <usb.h>
  28. #include <asm/io.h>
  29. #include <malloc.h>
  30. #include <watchdog.h>
  31. #include <linux/compiler.h>
  32. #include "ehci.h"
  33. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  34. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  35. #endif
  36. /*
  37. * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
  38. * Let's time out after 8 to have a little safety margin on top of that.
  39. */
  40. #define HCHALT_TIMEOUT (8 * 1000)
  41. static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  42. #define ALIGN_END_ADDR(type, ptr, size) \
  43. ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
  44. static struct descriptor {
  45. struct usb_hub_descriptor hub;
  46. struct usb_device_descriptor device;
  47. struct usb_linux_config_descriptor config;
  48. struct usb_linux_interface_descriptor interface;
  49. struct usb_endpoint_descriptor endpoint;
  50. } __attribute__ ((packed)) descriptor = {
  51. {
  52. 0x8, /* bDescLength */
  53. 0x29, /* bDescriptorType: hub descriptor */
  54. 2, /* bNrPorts -- runtime modified */
  55. 0, /* wHubCharacteristics */
  56. 10, /* bPwrOn2PwrGood */
  57. 0, /* bHubCntrCurrent */
  58. {}, /* Device removable */
  59. {} /* at most 7 ports! XXX */
  60. },
  61. {
  62. 0x12, /* bLength */
  63. 1, /* bDescriptorType: UDESC_DEVICE */
  64. cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
  65. 9, /* bDeviceClass: UDCLASS_HUB */
  66. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  67. 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
  68. 64, /* bMaxPacketSize: 64 bytes */
  69. 0x0000, /* idVendor */
  70. 0x0000, /* idProduct */
  71. cpu_to_le16(0x0100), /* bcdDevice */
  72. 1, /* iManufacturer */
  73. 2, /* iProduct */
  74. 0, /* iSerialNumber */
  75. 1 /* bNumConfigurations: 1 */
  76. },
  77. {
  78. 0x9,
  79. 2, /* bDescriptorType: UDESC_CONFIG */
  80. cpu_to_le16(0x19),
  81. 1, /* bNumInterface */
  82. 1, /* bConfigurationValue */
  83. 0, /* iConfiguration */
  84. 0x40, /* bmAttributes: UC_SELF_POWER */
  85. 0 /* bMaxPower */
  86. },
  87. {
  88. 0x9, /* bLength */
  89. 4, /* bDescriptorType: UDESC_INTERFACE */
  90. 0, /* bInterfaceNumber */
  91. 0, /* bAlternateSetting */
  92. 1, /* bNumEndpoints */
  93. 9, /* bInterfaceClass: UICLASS_HUB */
  94. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  95. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  96. 0 /* iInterface */
  97. },
  98. {
  99. 0x7, /* bLength */
  100. 5, /* bDescriptorType: UDESC_ENDPOINT */
  101. 0x81, /* bEndpointAddress:
  102. * UE_DIR_IN | EHCI_INTR_ENDPT
  103. */
  104. 3, /* bmAttributes: UE_INTERRUPT */
  105. 8, /* wMaxPacketSize */
  106. 255 /* bInterval */
  107. },
  108. };
  109. #if defined(CONFIG_EHCI_IS_TDI)
  110. #define ehci_is_TDI() (1)
  111. #else
  112. #define ehci_is_TDI() (0)
  113. #endif
  114. __weak int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
  115. {
  116. return PORTSC_PSPD(reg);
  117. }
  118. __weak void ehci_set_usbmode(struct ehci_ctrl *ctrl)
  119. {
  120. uint32_t tmp;
  121. uint32_t *reg_ptr;
  122. reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
  123. tmp = ehci_readl(reg_ptr);
  124. tmp |= USBMODE_CM_HC;
  125. #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
  126. tmp |= USBMODE_BE;
  127. #endif
  128. ehci_writel(reg_ptr, tmp);
  129. }
  130. __weak void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
  131. uint32_t *reg)
  132. {
  133. mdelay(50);
  134. }
  135. __weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
  136. {
  137. if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
  138. /* Printing the message would cause a scan failure! */
  139. debug("The request port(%u) is not configured\n", port);
  140. return NULL;
  141. }
  142. return (uint32_t *)&hcor->or_portsc[port];
  143. }
  144. static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
  145. {
  146. uint32_t result;
  147. do {
  148. result = ehci_readl(ptr);
  149. udelay(5);
  150. if (result == ~(uint32_t)0)
  151. return -1;
  152. result &= mask;
  153. if (result == done)
  154. return 0;
  155. usec--;
  156. } while (usec > 0);
  157. return -1;
  158. }
  159. static int ehci_reset(int index)
  160. {
  161. uint32_t cmd;
  162. int ret = 0;
  163. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  164. cmd = (cmd & ~CMD_RUN) | CMD_RESET;
  165. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  166. ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
  167. CMD_RESET, 0, 250 * 1000);
  168. if (ret < 0) {
  169. printf("EHCI fail to reset\n");
  170. goto out;
  171. }
  172. if (ehci_is_TDI())
  173. ehci_set_usbmode(&ehcic[index]);
  174. #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
  175. cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
  176. cmd &= ~TXFIFO_THRESH_MASK;
  177. cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
  178. ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
  179. #endif
  180. out:
  181. return ret;
  182. }
  183. static int ehci_shutdown(struct ehci_ctrl *ctrl)
  184. {
  185. int i, ret = 0;
  186. uint32_t cmd, reg;
  187. if (!ctrl || !ctrl->hcor)
  188. return -EINVAL;
  189. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  190. cmd &= ~(CMD_PSE | CMD_ASE);
  191. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  192. ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
  193. 100 * 1000);
  194. if (!ret) {
  195. for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
  196. reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
  197. reg |= EHCI_PS_SUSP;
  198. ehci_writel(&ctrl->hcor->or_portsc[i], reg);
  199. }
  200. cmd &= ~CMD_RUN;
  201. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  202. ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
  203. HCHALT_TIMEOUT);
  204. }
  205. if (ret)
  206. puts("EHCI failed to shut down host controller.\n");
  207. return ret;
  208. }
  209. static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
  210. {
  211. uint32_t delta, next;
  212. uint32_t addr = (unsigned long)buf;
  213. int idx;
  214. if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
  215. debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
  216. flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
  217. idx = 0;
  218. while (idx < QT_BUFFER_CNT) {
  219. td->qt_buffer[idx] = cpu_to_hc32(addr);
  220. td->qt_buffer_hi[idx] = 0;
  221. next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
  222. delta = next - addr;
  223. if (delta >= sz)
  224. break;
  225. sz -= delta;
  226. addr = next;
  227. idx++;
  228. }
  229. if (idx == QT_BUFFER_CNT) {
  230. printf("out of buffer pointers (%zu bytes left)\n", sz);
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. static inline u8 ehci_encode_speed(enum usb_device_speed speed)
  236. {
  237. #define QH_HIGH_SPEED 2
  238. #define QH_FULL_SPEED 0
  239. #define QH_LOW_SPEED 1
  240. if (speed == USB_SPEED_HIGH)
  241. return QH_HIGH_SPEED;
  242. if (speed == USB_SPEED_LOW)
  243. return QH_LOW_SPEED;
  244. return QH_FULL_SPEED;
  245. }
  246. static void ehci_update_endpt2_dev_n_port(struct usb_device *dev,
  247. struct QH *qh)
  248. {
  249. struct usb_device *ttdev;
  250. if (dev->speed != USB_SPEED_LOW && dev->speed != USB_SPEED_FULL)
  251. return;
  252. /*
  253. * For full / low speed devices we need to get the devnum and portnr of
  254. * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
  255. * in the tree before that one!
  256. */
  257. ttdev = dev;
  258. while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
  259. ttdev = ttdev->parent;
  260. if (!ttdev->parent)
  261. return;
  262. qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
  263. QH_ENDPT2_HUBADDR(ttdev->parent->devnum));
  264. }
  265. static int
  266. ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
  267. int length, struct devrequest *req)
  268. {
  269. ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
  270. struct qTD *qtd;
  271. int qtd_count = 0;
  272. int qtd_counter = 0;
  273. volatile struct qTD *vtd;
  274. unsigned long ts;
  275. uint32_t *tdp;
  276. uint32_t endpt, maxpacket, token, usbsts;
  277. uint32_t c, toggle;
  278. uint32_t cmd;
  279. int timeout;
  280. int ret = 0;
  281. struct ehci_ctrl *ctrl = dev->controller;
  282. debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
  283. buffer, length, req);
  284. if (req != NULL)
  285. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  286. req->request, req->request,
  287. req->requesttype, req->requesttype,
  288. le16_to_cpu(req->value), le16_to_cpu(req->value),
  289. le16_to_cpu(req->index));
  290. #define PKT_ALIGN 512
  291. /*
  292. * The USB transfer is split into qTD transfers. Eeach qTD transfer is
  293. * described by a transfer descriptor (the qTD). The qTDs form a linked
  294. * list with a queue head (QH).
  295. *
  296. * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
  297. * have its beginning in a qTD transfer and its end in the following
  298. * one, so the qTD transfer lengths have to be chosen accordingly.
  299. *
  300. * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
  301. * single pages. The first data buffer can start at any offset within a
  302. * page (not considering the cache-line alignment issues), while the
  303. * following buffers must be page-aligned. There is no alignment
  304. * constraint on the size of a qTD transfer.
  305. */
  306. if (req != NULL)
  307. /* 1 qTD will be needed for SETUP, and 1 for ACK. */
  308. qtd_count += 1 + 1;
  309. if (length > 0 || req == NULL) {
  310. /*
  311. * Determine the qTD transfer size that will be used for the
  312. * data payload (not considering the first qTD transfer, which
  313. * may be longer or shorter, and the final one, which may be
  314. * shorter).
  315. *
  316. * In order to keep each packet within a qTD transfer, the qTD
  317. * transfer size is aligned to PKT_ALIGN, which is a multiple of
  318. * wMaxPacketSize (except in some cases for interrupt transfers,
  319. * see comment in submit_int_msg()).
  320. *
  321. * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
  322. * QT_BUFFER_CNT full pages will be used.
  323. */
  324. int xfr_sz = QT_BUFFER_CNT;
  325. /*
  326. * However, if the input buffer is not aligned to PKT_ALIGN, the
  327. * qTD transfer size will be one page shorter, and the first qTD
  328. * data buffer of each transfer will be page-unaligned.
  329. */
  330. if ((unsigned long)buffer & (PKT_ALIGN - 1))
  331. xfr_sz--;
  332. /* Convert the qTD transfer size to bytes. */
  333. xfr_sz *= EHCI_PAGE_SIZE;
  334. /*
  335. * Approximate by excess the number of qTDs that will be
  336. * required for the data payload. The exact formula is way more
  337. * complicated and saves at most 2 qTDs, i.e. a total of 128
  338. * bytes.
  339. */
  340. qtd_count += 2 + length / xfr_sz;
  341. }
  342. /*
  343. * Threshold value based on the worst-case total size of the allocated qTDs for
  344. * a mass-storage transfer of 65535 blocks of 512 bytes.
  345. */
  346. #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
  347. #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
  348. #endif
  349. qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
  350. if (qtd == NULL) {
  351. printf("unable to allocate TDs\n");
  352. return -1;
  353. }
  354. memset(qh, 0, sizeof(struct QH));
  355. memset(qtd, 0, qtd_count * sizeof(*qtd));
  356. toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  357. /*
  358. * Setup QH (3.6 in ehci-r10.pdf)
  359. *
  360. * qh_link ................. 03-00 H
  361. * qh_endpt1 ............... 07-04 H
  362. * qh_endpt2 ............... 0B-08 H
  363. * - qh_curtd
  364. * qh_overlay.qt_next ...... 13-10 H
  365. * - qh_overlay.qt_altnext
  366. */
  367. qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
  368. c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
  369. maxpacket = usb_maxpacket(dev, pipe);
  370. endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
  371. QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
  372. QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
  373. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  374. QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
  375. QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
  376. qh->qh_endpt1 = cpu_to_hc32(endpt);
  377. endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
  378. qh->qh_endpt2 = cpu_to_hc32(endpt);
  379. ehci_update_endpt2_dev_n_port(dev, qh);
  380. qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  381. qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  382. tdp = &qh->qh_overlay.qt_next;
  383. if (req != NULL) {
  384. /*
  385. * Setup request qTD (3.5 in ehci-r10.pdf)
  386. *
  387. * qt_next ................ 03-00 H
  388. * qt_altnext ............. 07-04 H
  389. * qt_token ............... 0B-08 H
  390. *
  391. * [ buffer, buffer_hi ] loaded with "req".
  392. */
  393. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  394. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  395. token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
  396. QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  397. QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
  398. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  399. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  400. if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
  401. printf("unable to construct SETUP TD\n");
  402. goto fail;
  403. }
  404. /* Update previous qTD! */
  405. *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
  406. tdp = &qtd[qtd_counter++].qt_next;
  407. toggle = 1;
  408. }
  409. if (length > 0 || req == NULL) {
  410. uint8_t *buf_ptr = buffer;
  411. int left_length = length;
  412. do {
  413. /*
  414. * Determine the size of this qTD transfer. By default,
  415. * QT_BUFFER_CNT full pages can be used.
  416. */
  417. int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
  418. /*
  419. * However, if the input buffer is not page-aligned, the
  420. * portion of the first page before the buffer start
  421. * offset within that page is unusable.
  422. */
  423. xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
  424. /*
  425. * In order to keep each packet within a qTD transfer,
  426. * align the qTD transfer size to PKT_ALIGN.
  427. */
  428. xfr_bytes &= ~(PKT_ALIGN - 1);
  429. /*
  430. * This transfer may be shorter than the available qTD
  431. * transfer size that has just been computed.
  432. */
  433. xfr_bytes = min(xfr_bytes, left_length);
  434. /*
  435. * Setup request qTD (3.5 in ehci-r10.pdf)
  436. *
  437. * qt_next ................ 03-00 H
  438. * qt_altnext ............. 07-04 H
  439. * qt_token ............... 0B-08 H
  440. *
  441. * [ buffer, buffer_hi ] loaded with "buffer".
  442. */
  443. qtd[qtd_counter].qt_next =
  444. cpu_to_hc32(QT_NEXT_TERMINATE);
  445. qtd[qtd_counter].qt_altnext =
  446. cpu_to_hc32(QT_NEXT_TERMINATE);
  447. token = QT_TOKEN_DT(toggle) |
  448. QT_TOKEN_TOTALBYTES(xfr_bytes) |
  449. QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
  450. QT_TOKEN_CERR(3) |
  451. QT_TOKEN_PID(usb_pipein(pipe) ?
  452. QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
  453. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  454. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  455. if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
  456. xfr_bytes)) {
  457. printf("unable to construct DATA TD\n");
  458. goto fail;
  459. }
  460. /* Update previous qTD! */
  461. *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
  462. tdp = &qtd[qtd_counter++].qt_next;
  463. /*
  464. * Data toggle has to be adjusted since the qTD transfer
  465. * size is not always an even multiple of
  466. * wMaxPacketSize.
  467. */
  468. if ((xfr_bytes / maxpacket) & 1)
  469. toggle ^= 1;
  470. buf_ptr += xfr_bytes;
  471. left_length -= xfr_bytes;
  472. } while (left_length > 0);
  473. }
  474. if (req != NULL) {
  475. /*
  476. * Setup request qTD (3.5 in ehci-r10.pdf)
  477. *
  478. * qt_next ................ 03-00 H
  479. * qt_altnext ............. 07-04 H
  480. * qt_token ............... 0B-08 H
  481. */
  482. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  483. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  484. token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
  485. QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  486. QT_TOKEN_PID(usb_pipein(pipe) ?
  487. QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
  488. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  489. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  490. /* Update previous qTD! */
  491. *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
  492. tdp = &qtd[qtd_counter++].qt_next;
  493. }
  494. ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
  495. /* Flush dcache */
  496. flush_dcache_range((unsigned long)&ctrl->qh_list,
  497. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  498. flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
  499. flush_dcache_range((unsigned long)qtd,
  500. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  501. /* Set async. queue head pointer. */
  502. ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
  503. usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
  504. ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
  505. /* Enable async. schedule. */
  506. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  507. cmd |= CMD_ASE;
  508. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  509. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
  510. 100 * 1000);
  511. if (ret < 0) {
  512. printf("EHCI fail timeout STS_ASS set\n");
  513. goto fail;
  514. }
  515. /* Wait for TDs to be processed. */
  516. ts = get_timer(0);
  517. vtd = &qtd[qtd_counter - 1];
  518. timeout = USB_TIMEOUT_MS(pipe);
  519. do {
  520. /* Invalidate dcache */
  521. invalidate_dcache_range((unsigned long)&ctrl->qh_list,
  522. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  523. invalidate_dcache_range((unsigned long)qh,
  524. ALIGN_END_ADDR(struct QH, qh, 1));
  525. invalidate_dcache_range((unsigned long)qtd,
  526. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  527. token = hc32_to_cpu(vtd->qt_token);
  528. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
  529. break;
  530. WATCHDOG_RESET();
  531. } while (get_timer(ts) < timeout);
  532. /*
  533. * Invalidate the memory area occupied by buffer
  534. * Don't try to fix the buffer alignment, if it isn't properly
  535. * aligned it's upper layer's fault so let invalidate_dcache_range()
  536. * vow about it. But we have to fix the length as it's actual
  537. * transfer length and can be unaligned. This is potentially
  538. * dangerous operation, it's responsibility of the calling
  539. * code to make sure enough space is reserved.
  540. */
  541. invalidate_dcache_range((unsigned long)buffer,
  542. ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
  543. /* Check that the TD processing happened */
  544. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
  545. printf("EHCI timed out on TD - token=%#x\n", token);
  546. /* Disable async schedule. */
  547. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  548. cmd &= ~CMD_ASE;
  549. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  550. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
  551. 100 * 1000);
  552. if (ret < 0) {
  553. printf("EHCI fail timeout STS_ASS reset\n");
  554. goto fail;
  555. }
  556. token = hc32_to_cpu(qh->qh_overlay.qt_token);
  557. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
  558. debug("TOKEN=%#x\n", token);
  559. switch (QT_TOKEN_GET_STATUS(token) &
  560. ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
  561. case 0:
  562. toggle = QT_TOKEN_GET_DT(token);
  563. usb_settoggle(dev, usb_pipeendpoint(pipe),
  564. usb_pipeout(pipe), toggle);
  565. dev->status = 0;
  566. break;
  567. case QT_TOKEN_STATUS_HALTED:
  568. dev->status = USB_ST_STALLED;
  569. break;
  570. case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
  571. case QT_TOKEN_STATUS_DATBUFERR:
  572. dev->status = USB_ST_BUF_ERR;
  573. break;
  574. case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
  575. case QT_TOKEN_STATUS_BABBLEDET:
  576. dev->status = USB_ST_BABBLE_DET;
  577. break;
  578. default:
  579. dev->status = USB_ST_CRC_ERR;
  580. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
  581. dev->status |= USB_ST_STALLED;
  582. break;
  583. }
  584. dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
  585. } else {
  586. dev->act_len = 0;
  587. #ifndef CONFIG_USB_EHCI_FARADAY
  588. debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
  589. dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
  590. ehci_readl(&ctrl->hcor->or_portsc[0]),
  591. ehci_readl(&ctrl->hcor->or_portsc[1]));
  592. #endif
  593. }
  594. free(qtd);
  595. return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
  596. fail:
  597. free(qtd);
  598. return -1;
  599. }
  600. int
  601. ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
  602. int length, struct devrequest *req)
  603. {
  604. uint8_t tmpbuf[4];
  605. u16 typeReq;
  606. void *srcptr = NULL;
  607. int len, srclen;
  608. uint32_t reg;
  609. uint32_t *status_reg;
  610. int port = le16_to_cpu(req->index) & 0xff;
  611. struct ehci_ctrl *ctrl = dev->controller;
  612. srclen = 0;
  613. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  614. req->request, req->request,
  615. req->requesttype, req->requesttype,
  616. le16_to_cpu(req->value), le16_to_cpu(req->index));
  617. typeReq = req->request | req->requesttype << 8;
  618. switch (typeReq) {
  619. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  620. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  621. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  622. status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
  623. if (!status_reg)
  624. return -1;
  625. break;
  626. default:
  627. status_reg = NULL;
  628. break;
  629. }
  630. switch (typeReq) {
  631. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  632. switch (le16_to_cpu(req->value) >> 8) {
  633. case USB_DT_DEVICE:
  634. debug("USB_DT_DEVICE request\n");
  635. srcptr = &descriptor.device;
  636. srclen = descriptor.device.bLength;
  637. break;
  638. case USB_DT_CONFIG:
  639. debug("USB_DT_CONFIG config\n");
  640. srcptr = &descriptor.config;
  641. srclen = descriptor.config.bLength +
  642. descriptor.interface.bLength +
  643. descriptor.endpoint.bLength;
  644. break;
  645. case USB_DT_STRING:
  646. debug("USB_DT_STRING config\n");
  647. switch (le16_to_cpu(req->value) & 0xff) {
  648. case 0: /* Language */
  649. srcptr = "\4\3\1\0";
  650. srclen = 4;
  651. break;
  652. case 1: /* Vendor */
  653. srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
  654. srclen = 14;
  655. break;
  656. case 2: /* Product */
  657. srcptr = "\52\3E\0H\0C\0I\0 "
  658. "\0H\0o\0s\0t\0 "
  659. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  660. srclen = 42;
  661. break;
  662. default:
  663. debug("unknown value DT_STRING %x\n",
  664. le16_to_cpu(req->value));
  665. goto unknown;
  666. }
  667. break;
  668. default:
  669. debug("unknown value %x\n", le16_to_cpu(req->value));
  670. goto unknown;
  671. }
  672. break;
  673. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  674. switch (le16_to_cpu(req->value) >> 8) {
  675. case USB_DT_HUB:
  676. debug("USB_DT_HUB config\n");
  677. srcptr = &descriptor.hub;
  678. srclen = descriptor.hub.bLength;
  679. break;
  680. default:
  681. debug("unknown value %x\n", le16_to_cpu(req->value));
  682. goto unknown;
  683. }
  684. break;
  685. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  686. debug("USB_REQ_SET_ADDRESS\n");
  687. ctrl->rootdev = le16_to_cpu(req->value);
  688. break;
  689. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  690. debug("USB_REQ_SET_CONFIGURATION\n");
  691. /* Nothing to do */
  692. break;
  693. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  694. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  695. tmpbuf[1] = 0;
  696. srcptr = tmpbuf;
  697. srclen = 2;
  698. break;
  699. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  700. memset(tmpbuf, 0, 4);
  701. reg = ehci_readl(status_reg);
  702. if (reg & EHCI_PS_CS)
  703. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  704. if (reg & EHCI_PS_PE)
  705. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  706. if (reg & EHCI_PS_SUSP)
  707. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  708. if (reg & EHCI_PS_OCA)
  709. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  710. if (reg & EHCI_PS_PR)
  711. tmpbuf[0] |= USB_PORT_STAT_RESET;
  712. if (reg & EHCI_PS_PP)
  713. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  714. if (ehci_is_TDI()) {
  715. switch (ehci_get_port_speed(ctrl, reg)) {
  716. case PORTSC_PSPD_FS:
  717. break;
  718. case PORTSC_PSPD_LS:
  719. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  720. break;
  721. case PORTSC_PSPD_HS:
  722. default:
  723. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  724. break;
  725. }
  726. } else {
  727. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  728. }
  729. if (reg & EHCI_PS_CSC)
  730. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  731. if (reg & EHCI_PS_PEC)
  732. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  733. if (reg & EHCI_PS_OCC)
  734. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  735. if (ctrl->portreset & (1 << port))
  736. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  737. srcptr = tmpbuf;
  738. srclen = 4;
  739. break;
  740. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  741. reg = ehci_readl(status_reg);
  742. reg &= ~EHCI_PS_CLEAR;
  743. switch (le16_to_cpu(req->value)) {
  744. case USB_PORT_FEAT_ENABLE:
  745. reg |= EHCI_PS_PE;
  746. ehci_writel(status_reg, reg);
  747. break;
  748. case USB_PORT_FEAT_POWER:
  749. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
  750. reg |= EHCI_PS_PP;
  751. ehci_writel(status_reg, reg);
  752. }
  753. break;
  754. case USB_PORT_FEAT_RESET:
  755. if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
  756. !ehci_is_TDI() &&
  757. EHCI_PS_IS_LOWSPEED(reg)) {
  758. /* Low speed device, give up ownership. */
  759. debug("port %d low speed --> companion\n",
  760. port - 1);
  761. reg |= EHCI_PS_PO;
  762. ehci_writel(status_reg, reg);
  763. break;
  764. } else {
  765. int ret;
  766. reg |= EHCI_PS_PR;
  767. reg &= ~EHCI_PS_PE;
  768. ehci_writel(status_reg, reg);
  769. /*
  770. * caller must wait, then call GetPortStatus
  771. * usb 2.0 specification say 50 ms resets on
  772. * root
  773. */
  774. ehci_powerup_fixup(ctrl, status_reg, &reg);
  775. ehci_writel(status_reg, reg & ~EHCI_PS_PR);
  776. /*
  777. * A host controller must terminate the reset
  778. * and stabilize the state of the port within
  779. * 2 milliseconds
  780. */
  781. ret = handshake(status_reg, EHCI_PS_PR, 0,
  782. 2 * 1000);
  783. if (!ret)
  784. ctrl->portreset |= 1 << port;
  785. else
  786. printf("port(%d) reset error\n",
  787. port - 1);
  788. }
  789. break;
  790. case USB_PORT_FEAT_TEST:
  791. ehci_shutdown(ctrl);
  792. reg &= ~(0xf << 16);
  793. reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
  794. ehci_writel(status_reg, reg);
  795. break;
  796. default:
  797. debug("unknown feature %x\n", le16_to_cpu(req->value));
  798. goto unknown;
  799. }
  800. /* unblock posted writes */
  801. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  802. break;
  803. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  804. reg = ehci_readl(status_reg);
  805. reg &= ~EHCI_PS_CLEAR;
  806. switch (le16_to_cpu(req->value)) {
  807. case USB_PORT_FEAT_ENABLE:
  808. reg &= ~EHCI_PS_PE;
  809. break;
  810. case USB_PORT_FEAT_C_ENABLE:
  811. reg |= EHCI_PS_PE;
  812. break;
  813. case USB_PORT_FEAT_POWER:
  814. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
  815. reg &= ~EHCI_PS_PP;
  816. break;
  817. case USB_PORT_FEAT_C_CONNECTION:
  818. reg |= EHCI_PS_CSC;
  819. break;
  820. case USB_PORT_FEAT_OVER_CURRENT:
  821. reg |= EHCI_PS_OCC;
  822. break;
  823. case USB_PORT_FEAT_C_RESET:
  824. ctrl->portreset &= ~(1 << port);
  825. break;
  826. default:
  827. debug("unknown feature %x\n", le16_to_cpu(req->value));
  828. goto unknown;
  829. }
  830. ehci_writel(status_reg, reg);
  831. /* unblock posted write */
  832. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  833. break;
  834. default:
  835. debug("Unknown request\n");
  836. goto unknown;
  837. }
  838. mdelay(1);
  839. len = min3(srclen, (int)le16_to_cpu(req->length), length);
  840. if (srcptr != NULL && len > 0)
  841. memcpy(buffer, srcptr, len);
  842. else
  843. debug("Len is 0\n");
  844. dev->act_len = len;
  845. dev->status = 0;
  846. return 0;
  847. unknown:
  848. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  849. req->requesttype, req->request, le16_to_cpu(req->value),
  850. le16_to_cpu(req->index), le16_to_cpu(req->length));
  851. dev->act_len = 0;
  852. dev->status = USB_ST_STALLED;
  853. return -1;
  854. }
  855. void ehci_set_controller_priv(int index, void *priv)
  856. {
  857. ehcic[index].priv = priv;
  858. }
  859. void *ehci_get_controller_priv(int index)
  860. {
  861. return ehcic[index].priv;
  862. }
  863. int usb_lowlevel_stop(int index)
  864. {
  865. ehci_shutdown(&ehcic[index]);
  866. return ehci_hcd_stop(index);
  867. }
  868. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  869. {
  870. uint32_t reg;
  871. uint32_t cmd;
  872. struct QH *qh_list;
  873. struct QH *periodic;
  874. int i;
  875. int rc;
  876. rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
  877. if (rc)
  878. return rc;
  879. if (init == USB_INIT_DEVICE)
  880. goto done;
  881. /* EHCI spec section 4.1 */
  882. if (ehci_reset(index))
  883. return -1;
  884. #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
  885. rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
  886. if (rc)
  887. return rc;
  888. #endif
  889. /* Set the high address word (aka segment) for 64-bit controller */
  890. if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
  891. ehci_writel(&ehcic[index].hcor->or_ctrldssegment, 0);
  892. qh_list = &ehcic[index].qh_list;
  893. /* Set head of reclaim list */
  894. memset(qh_list, 0, sizeof(*qh_list));
  895. qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
  896. qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
  897. QH_ENDPT1_EPS(USB_SPEED_HIGH));
  898. qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  899. qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  900. qh_list->qh_overlay.qt_token =
  901. cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
  902. flush_dcache_range((unsigned long)qh_list,
  903. ALIGN_END_ADDR(struct QH, qh_list, 1));
  904. /* Set async. queue head pointer. */
  905. ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (unsigned long)qh_list);
  906. /*
  907. * Set up periodic list
  908. * Step 1: Parent QH for all periodic transfers.
  909. */
  910. ehcic[index].periodic_schedules = 0;
  911. periodic = &ehcic[index].periodic_queue;
  912. memset(periodic, 0, sizeof(*periodic));
  913. periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  914. periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  915. periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  916. flush_dcache_range((unsigned long)periodic,
  917. ALIGN_END_ADDR(struct QH, periodic, 1));
  918. /*
  919. * Step 2: Setup frame-list: Every microframe, USB tries the same list.
  920. * In particular, device specifications on polling frequency
  921. * are disregarded. Keyboards seem to send NAK/NYet reliably
  922. * when polled with an empty buffer.
  923. *
  924. * Split Transactions will be spread across microframes using
  925. * S-mask and C-mask.
  926. */
  927. if (ehcic[index].periodic_list == NULL)
  928. ehcic[index].periodic_list = memalign(4096, 1024 * 4);
  929. if (!ehcic[index].periodic_list)
  930. return -ENOMEM;
  931. for (i = 0; i < 1024; i++) {
  932. ehcic[index].periodic_list[i] = cpu_to_hc32((unsigned long)periodic
  933. | QH_LINK_TYPE_QH);
  934. }
  935. flush_dcache_range((unsigned long)ehcic[index].periodic_list,
  936. ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
  937. 1024));
  938. /* Set periodic list base address */
  939. ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
  940. (unsigned long)ehcic[index].periodic_list);
  941. reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
  942. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  943. debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  944. /* Port Indicators */
  945. if (HCS_INDICATOR(reg))
  946. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  947. | 0x80, &descriptor.hub.wHubCharacteristics);
  948. /* Port Power Control */
  949. if (HCS_PPC(reg))
  950. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  951. | 0x01, &descriptor.hub.wHubCharacteristics);
  952. /* Start the host controller. */
  953. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  954. /*
  955. * Philips, Intel, and maybe others need CMD_RUN before the
  956. * root hub will detect new devices (why?); NEC doesn't
  957. */
  958. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  959. cmd |= CMD_RUN;
  960. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  961. #ifndef CONFIG_USB_EHCI_FARADAY
  962. /* take control over the ports */
  963. cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
  964. cmd |= FLAG_CF;
  965. ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
  966. #endif
  967. /* unblock posted write */
  968. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  969. mdelay(5);
  970. reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
  971. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  972. ehcic[index].rootdev = 0;
  973. done:
  974. *controller = &ehcic[index];
  975. return 0;
  976. }
  977. int
  978. submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  979. int length)
  980. {
  981. if (usb_pipetype(pipe) != PIPE_BULK) {
  982. debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  983. return -1;
  984. }
  985. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  986. }
  987. int
  988. submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  989. int length, struct devrequest *setup)
  990. {
  991. struct ehci_ctrl *ctrl = dev->controller;
  992. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  993. debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
  994. return -1;
  995. }
  996. if (usb_pipedevice(pipe) == ctrl->rootdev) {
  997. if (!ctrl->rootdev)
  998. dev->speed = USB_SPEED_HIGH;
  999. return ehci_submit_root(dev, pipe, buffer, length, setup);
  1000. }
  1001. return ehci_submit_async(dev, pipe, buffer, length, setup);
  1002. }
  1003. struct int_queue {
  1004. int elementsize;
  1005. struct QH *first;
  1006. struct QH *current;
  1007. struct QH *last;
  1008. struct qTD *tds;
  1009. };
  1010. #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
  1011. static int
  1012. enable_periodic(struct ehci_ctrl *ctrl)
  1013. {
  1014. uint32_t cmd;
  1015. struct ehci_hcor *hcor = ctrl->hcor;
  1016. int ret;
  1017. cmd = ehci_readl(&hcor->or_usbcmd);
  1018. cmd |= CMD_PSE;
  1019. ehci_writel(&hcor->or_usbcmd, cmd);
  1020. ret = handshake((uint32_t *)&hcor->or_usbsts,
  1021. STS_PSS, STS_PSS, 100 * 1000);
  1022. if (ret < 0) {
  1023. printf("EHCI failed: timeout when enabling periodic list\n");
  1024. return -ETIMEDOUT;
  1025. }
  1026. udelay(1000);
  1027. return 0;
  1028. }
  1029. static int
  1030. disable_periodic(struct ehci_ctrl *ctrl)
  1031. {
  1032. uint32_t cmd;
  1033. struct ehci_hcor *hcor = ctrl->hcor;
  1034. int ret;
  1035. cmd = ehci_readl(&hcor->or_usbcmd);
  1036. cmd &= ~CMD_PSE;
  1037. ehci_writel(&hcor->or_usbcmd, cmd);
  1038. ret = handshake((uint32_t *)&hcor->or_usbsts,
  1039. STS_PSS, 0, 100 * 1000);
  1040. if (ret < 0) {
  1041. printf("EHCI failed: timeout when disabling periodic list\n");
  1042. return -ETIMEDOUT;
  1043. }
  1044. return 0;
  1045. }
  1046. struct int_queue *
  1047. create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
  1048. int elementsize, void *buffer, int interval)
  1049. {
  1050. struct ehci_ctrl *ctrl = dev->controller;
  1051. struct int_queue *result = NULL;
  1052. int i;
  1053. /*
  1054. * Interrupt transfers requiring several transactions are not supported
  1055. * because bInterval is ignored.
  1056. *
  1057. * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
  1058. * <= PKT_ALIGN if several qTDs are required, while the USB
  1059. * specification does not constrain this for interrupt transfers. That
  1060. * means that ehci_submit_async() would support interrupt transfers
  1061. * requiring several transactions only as long as the transfer size does
  1062. * not require more than a single qTD.
  1063. */
  1064. if (elementsize > usb_maxpacket(dev, pipe)) {
  1065. printf("%s: xfers requiring several transactions are not supported.\n",
  1066. __func__);
  1067. return NULL;
  1068. }
  1069. debug("Enter create_int_queue\n");
  1070. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  1071. debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  1072. return NULL;
  1073. }
  1074. /* limit to 4 full pages worth of data -
  1075. * we can safely fit them in a single TD,
  1076. * no matter the alignment
  1077. */
  1078. if (elementsize >= 16384) {
  1079. debug("too large elements for interrupt transfers\n");
  1080. return NULL;
  1081. }
  1082. result = malloc(sizeof(*result));
  1083. if (!result) {
  1084. debug("ehci intr queue: out of memory\n");
  1085. goto fail1;
  1086. }
  1087. result->elementsize = elementsize;
  1088. result->first = memalign(USB_DMA_MINALIGN,
  1089. sizeof(struct QH) * queuesize);
  1090. if (!result->first) {
  1091. debug("ehci intr queue: out of memory\n");
  1092. goto fail2;
  1093. }
  1094. result->current = result->first;
  1095. result->last = result->first + queuesize - 1;
  1096. result->tds = memalign(USB_DMA_MINALIGN,
  1097. sizeof(struct qTD) * queuesize);
  1098. if (!result->tds) {
  1099. debug("ehci intr queue: out of memory\n");
  1100. goto fail3;
  1101. }
  1102. memset(result->first, 0, sizeof(struct QH) * queuesize);
  1103. memset(result->tds, 0, sizeof(struct qTD) * queuesize);
  1104. for (i = 0; i < queuesize; i++) {
  1105. struct QH *qh = result->first + i;
  1106. struct qTD *td = result->tds + i;
  1107. void **buf = &qh->buffer;
  1108. qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
  1109. if (i == queuesize - 1)
  1110. qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  1111. qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
  1112. qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  1113. qh->qh_endpt1 =
  1114. cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
  1115. (usb_maxpacket(dev, pipe) << 16) | /* MPS */
  1116. (1 << 14) |
  1117. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  1118. (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
  1119. (usb_pipedevice(pipe) << 0));
  1120. qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
  1121. (1 << 0)); /* S-mask: microframe 0 */
  1122. if (dev->speed == USB_SPEED_LOW ||
  1123. dev->speed == USB_SPEED_FULL) {
  1124. /* C-mask: microframes 2-4 */
  1125. qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
  1126. }
  1127. ehci_update_endpt2_dev_n_port(dev, qh);
  1128. td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  1129. td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  1130. debug("communication direction is '%s'\n",
  1131. usb_pipein(pipe) ? "in" : "out");
  1132. td->qt_token = cpu_to_hc32((elementsize << 16) |
  1133. ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
  1134. 0x80); /* active */
  1135. td->qt_buffer[0] =
  1136. cpu_to_hc32((unsigned long)buffer + i * elementsize);
  1137. td->qt_buffer[1] =
  1138. cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
  1139. td->qt_buffer[2] =
  1140. cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
  1141. td->qt_buffer[3] =
  1142. cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
  1143. td->qt_buffer[4] =
  1144. cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
  1145. *buf = buffer + i * elementsize;
  1146. }
  1147. flush_dcache_range((unsigned long)buffer,
  1148. ALIGN_END_ADDR(char, buffer,
  1149. queuesize * elementsize));
  1150. flush_dcache_range((unsigned long)result->first,
  1151. ALIGN_END_ADDR(struct QH, result->first,
  1152. queuesize));
  1153. flush_dcache_range((unsigned long)result->tds,
  1154. ALIGN_END_ADDR(struct qTD, result->tds,
  1155. queuesize));
  1156. if (ctrl->periodic_schedules > 0) {
  1157. if (disable_periodic(ctrl) < 0) {
  1158. debug("FATAL: periodic should never fail, but did");
  1159. goto fail3;
  1160. }
  1161. }
  1162. /* hook up to periodic list */
  1163. struct QH *list = &ctrl->periodic_queue;
  1164. result->last->qh_link = list->qh_link;
  1165. list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
  1166. flush_dcache_range((unsigned long)result->last,
  1167. ALIGN_END_ADDR(struct QH, result->last, 1));
  1168. flush_dcache_range((unsigned long)list,
  1169. ALIGN_END_ADDR(struct QH, list, 1));
  1170. if (enable_periodic(ctrl) < 0) {
  1171. debug("FATAL: periodic should never fail, but did");
  1172. goto fail3;
  1173. }
  1174. ctrl->periodic_schedules++;
  1175. debug("Exit create_int_queue\n");
  1176. return result;
  1177. fail3:
  1178. if (result->tds)
  1179. free(result->tds);
  1180. fail2:
  1181. if (result->first)
  1182. free(result->first);
  1183. if (result)
  1184. free(result);
  1185. fail1:
  1186. return NULL;
  1187. }
  1188. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
  1189. {
  1190. struct QH *cur = queue->current;
  1191. struct qTD *cur_td;
  1192. /* depleted queue */
  1193. if (cur == NULL) {
  1194. debug("Exit poll_int_queue with completed queue\n");
  1195. return NULL;
  1196. }
  1197. /* still active */
  1198. cur_td = &queue->tds[queue->current - queue->first];
  1199. invalidate_dcache_range((unsigned long)cur_td,
  1200. ALIGN_END_ADDR(struct qTD, cur_td, 1));
  1201. if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
  1202. QT_TOKEN_STATUS_ACTIVE) {
  1203. debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
  1204. hc32_to_cpu(cur_td->qt_token));
  1205. return NULL;
  1206. }
  1207. if (!(cur->qh_link & QH_LINK_TERMINATE))
  1208. queue->current++;
  1209. else
  1210. queue->current = NULL;
  1211. invalidate_dcache_range((unsigned long)cur->buffer,
  1212. ALIGN_END_ADDR(char, cur->buffer,
  1213. queue->elementsize));
  1214. debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
  1215. hc32_to_cpu(cur_td->qt_token), cur, queue->first);
  1216. return cur->buffer;
  1217. }
  1218. /* Do not free buffers associated with QHs, they're owned by someone else */
  1219. int
  1220. destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
  1221. {
  1222. struct ehci_ctrl *ctrl = dev->controller;
  1223. int result = -1;
  1224. unsigned long timeout;
  1225. if (disable_periodic(ctrl) < 0) {
  1226. debug("FATAL: periodic should never fail, but did");
  1227. goto out;
  1228. }
  1229. ctrl->periodic_schedules--;
  1230. struct QH *cur = &ctrl->periodic_queue;
  1231. timeout = get_timer(0) + 500; /* abort after 500ms */
  1232. while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
  1233. debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
  1234. if (NEXT_QH(cur) == queue->first) {
  1235. debug("found candidate. removing from chain\n");
  1236. cur->qh_link = queue->last->qh_link;
  1237. flush_dcache_range((unsigned long)cur,
  1238. ALIGN_END_ADDR(struct QH, cur, 1));
  1239. result = 0;
  1240. break;
  1241. }
  1242. cur = NEXT_QH(cur);
  1243. if (get_timer(0) > timeout) {
  1244. printf("Timeout destroying interrupt endpoint queue\n");
  1245. result = -1;
  1246. goto out;
  1247. }
  1248. }
  1249. if (ctrl->periodic_schedules > 0) {
  1250. result = enable_periodic(ctrl);
  1251. if (result < 0)
  1252. debug("FATAL: periodic should never fail, but did");
  1253. }
  1254. out:
  1255. free(queue->tds);
  1256. free(queue->first);
  1257. free(queue);
  1258. return result;
  1259. }
  1260. int
  1261. submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1262. int length, int interval)
  1263. {
  1264. void *backbuffer;
  1265. struct int_queue *queue;
  1266. unsigned long timeout;
  1267. int result = 0, ret;
  1268. debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
  1269. dev, pipe, buffer, length, interval);
  1270. queue = create_int_queue(dev, pipe, 1, length, buffer, interval);
  1271. if (!queue)
  1272. return -1;
  1273. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  1274. while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
  1275. if (get_timer(0) > timeout) {
  1276. printf("Timeout poll on interrupt endpoint\n");
  1277. result = -ETIMEDOUT;
  1278. break;
  1279. }
  1280. if (backbuffer != buffer) {
  1281. debug("got wrong buffer back (%p instead of %p)\n",
  1282. backbuffer, buffer);
  1283. return -EINVAL;
  1284. }
  1285. ret = destroy_int_queue(dev, queue);
  1286. if (ret < 0)
  1287. return ret;
  1288. /* everything worked out fine */
  1289. return result;
  1290. }