ehci-hcd.c 38 KB

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