ehci-hcd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 <asm/byteorder.h>
  25. #include <usb.h>
  26. #include <asm/io.h>
  27. #include <malloc.h>
  28. #include <watchdog.h>
  29. #ifdef CONFIG_USB_KEYBOARD
  30. #include <stdio_dev.h>
  31. extern unsigned char new[];
  32. #endif
  33. #include "ehci.h"
  34. int rootdev;
  35. struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
  36. volatile struct ehci_hcor *hcor;
  37. static uint16_t portreset;
  38. static struct QH qh_list __attribute__((aligned(32)));
  39. static struct descriptor {
  40. struct usb_hub_descriptor hub;
  41. struct usb_device_descriptor device;
  42. struct usb_linux_config_descriptor config;
  43. struct usb_linux_interface_descriptor interface;
  44. struct usb_endpoint_descriptor endpoint;
  45. } __attribute__ ((packed)) descriptor = {
  46. {
  47. 0x8, /* bDescLength */
  48. 0x29, /* bDescriptorType: hub descriptor */
  49. 2, /* bNrPorts -- runtime modified */
  50. 0, /* wHubCharacteristics */
  51. 10, /* bPwrOn2PwrGood */
  52. 0, /* bHubCntrCurrent */
  53. {}, /* Device removable */
  54. {} /* at most 7 ports! XXX */
  55. },
  56. {
  57. 0x12, /* bLength */
  58. 1, /* bDescriptorType: UDESC_DEVICE */
  59. cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
  60. 9, /* bDeviceClass: UDCLASS_HUB */
  61. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  62. 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
  63. 64, /* bMaxPacketSize: 64 bytes */
  64. 0x0000, /* idVendor */
  65. 0x0000, /* idProduct */
  66. cpu_to_le16(0x0100), /* bcdDevice */
  67. 1, /* iManufacturer */
  68. 2, /* iProduct */
  69. 0, /* iSerialNumber */
  70. 1 /* bNumConfigurations: 1 */
  71. },
  72. {
  73. 0x9,
  74. 2, /* bDescriptorType: UDESC_CONFIG */
  75. cpu_to_le16(0x19),
  76. 1, /* bNumInterface */
  77. 1, /* bConfigurationValue */
  78. 0, /* iConfiguration */
  79. 0x40, /* bmAttributes: UC_SELF_POWER */
  80. 0 /* bMaxPower */
  81. },
  82. {
  83. 0x9, /* bLength */
  84. 4, /* bDescriptorType: UDESC_INTERFACE */
  85. 0, /* bInterfaceNumber */
  86. 0, /* bAlternateSetting */
  87. 1, /* bNumEndpoints */
  88. 9, /* bInterfaceClass: UICLASS_HUB */
  89. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  90. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  91. 0 /* iInterface */
  92. },
  93. {
  94. 0x7, /* bLength */
  95. 5, /* bDescriptorType: UDESC_ENDPOINT */
  96. 0x81, /* bEndpointAddress:
  97. * UE_DIR_IN | EHCI_INTR_ENDPT
  98. */
  99. 3, /* bmAttributes: UE_INTERRUPT */
  100. 8, /* wMaxPacketSize */
  101. 255 /* bInterval */
  102. },
  103. };
  104. #if defined(CONFIG_EHCI_IS_TDI)
  105. #define ehci_is_TDI() (1)
  106. #else
  107. #define ehci_is_TDI() (0)
  108. #endif
  109. #if defined(CONFIG_EHCI_DCACHE)
  110. /*
  111. * Routines to handle (flush/invalidate) the dcache for the QH and qTD
  112. * structures and data buffers. This is needed on platforms using this
  113. * EHCI support with dcache enabled.
  114. */
  115. static void flush_invalidate(u32 addr, int size, int flush)
  116. {
  117. if (flush)
  118. flush_dcache_range(addr, addr + size);
  119. else
  120. invalidate_dcache_range(addr, addr + size);
  121. }
  122. static void cache_qtd(struct qTD *qtd, int flush)
  123. {
  124. u32 *ptr = (u32 *)qtd->qt_buffer[0];
  125. int len = (qtd->qt_token & 0x7fff0000) >> 16;
  126. flush_invalidate((u32)qtd, sizeof(struct qTD), flush);
  127. if (ptr && len)
  128. flush_invalidate((u32)ptr, len, flush);
  129. }
  130. static inline struct QH *qh_addr(struct QH *qh)
  131. {
  132. return (struct QH *)((u32)qh & 0xffffffe0);
  133. }
  134. static void cache_qh(struct QH *qh, int flush)
  135. {
  136. struct qTD *qtd;
  137. struct qTD *next;
  138. static struct qTD *first_qtd;
  139. /*
  140. * Walk the QH list and flush/invalidate all entries
  141. */
  142. while (1) {
  143. flush_invalidate((u32)qh_addr(qh), sizeof(struct QH), flush);
  144. if ((u32)qh & QH_LINK_TYPE_QH)
  145. break;
  146. qh = qh_addr(qh);
  147. qh = (struct QH *)qh->qh_link;
  148. }
  149. qh = qh_addr(qh);
  150. /*
  151. * Save first qTD pointer, needed for invalidating pass on this QH
  152. */
  153. if (flush)
  154. first_qtd = qtd = (struct qTD *)(*(u32 *)&qh->qh_overlay &
  155. 0xffffffe0);
  156. else
  157. qtd = first_qtd;
  158. /*
  159. * Walk the qTD list and flush/invalidate all entries
  160. */
  161. while (1) {
  162. if (qtd == NULL)
  163. break;
  164. cache_qtd(qtd, flush);
  165. next = (struct qTD *)((u32)qtd->qt_next & 0xffffffe0);
  166. if (next == qtd)
  167. break;
  168. qtd = next;
  169. }
  170. }
  171. static inline void ehci_flush_dcache(struct QH *qh)
  172. {
  173. cache_qh(qh, 1);
  174. }
  175. static inline void ehci_invalidate_dcache(struct QH *qh)
  176. {
  177. cache_qh(qh, 0);
  178. }
  179. #else /* CONFIG_EHCI_DCACHE */
  180. /*
  181. *
  182. */
  183. static inline void ehci_flush_dcache(struct QH *qh)
  184. {
  185. }
  186. static inline void ehci_invalidate_dcache(struct QH *qh)
  187. {
  188. }
  189. #endif /* CONFIG_EHCI_DCACHE */
  190. void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  191. {
  192. mdelay(50);
  193. }
  194. void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  195. __attribute__((weak, alias("__ehci_powerup_fixup")));
  196. static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
  197. {
  198. uint32_t result;
  199. do {
  200. result = ehci_readl(ptr);
  201. udelay(5);
  202. if (result == ~(uint32_t)0)
  203. return -1;
  204. result &= mask;
  205. if (result == done)
  206. return 0;
  207. usec--;
  208. } while (usec > 0);
  209. return -1;
  210. }
  211. static void ehci_free(void *p, size_t sz)
  212. {
  213. }
  214. static int ehci_reset(void)
  215. {
  216. uint32_t cmd;
  217. uint32_t tmp;
  218. uint32_t *reg_ptr;
  219. int ret = 0;
  220. cmd = ehci_readl(&hcor->or_usbcmd);
  221. cmd = (cmd & ~CMD_RUN) | CMD_RESET;
  222. ehci_writel(&hcor->or_usbcmd, cmd);
  223. ret = handshake((uint32_t *)&hcor->or_usbcmd, CMD_RESET, 0, 250 * 1000);
  224. if (ret < 0) {
  225. printf("EHCI fail to reset\n");
  226. goto out;
  227. }
  228. if (ehci_is_TDI()) {
  229. reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE);
  230. tmp = ehci_readl(reg_ptr);
  231. tmp |= USBMODE_CM_HC;
  232. #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
  233. tmp |= USBMODE_BE;
  234. #endif
  235. ehci_writel(reg_ptr, tmp);
  236. }
  237. out:
  238. return ret;
  239. }
  240. static void *ehci_alloc(size_t sz, size_t align)
  241. {
  242. static struct QH qh __attribute__((aligned(32)));
  243. static struct qTD td[3] __attribute__((aligned (32)));
  244. static int ntds;
  245. void *p;
  246. switch (sz) {
  247. case sizeof(struct QH):
  248. p = &qh;
  249. ntds = 0;
  250. break;
  251. case sizeof(struct qTD):
  252. if (ntds == 3) {
  253. debug("out of TDs\n");
  254. return NULL;
  255. }
  256. p = &td[ntds];
  257. ntds++;
  258. break;
  259. default:
  260. debug("unknown allocation size\n");
  261. return NULL;
  262. }
  263. memset(p, 0, sz);
  264. return p;
  265. }
  266. static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
  267. {
  268. uint32_t addr, delta, next;
  269. int idx;
  270. addr = (uint32_t) buf;
  271. idx = 0;
  272. while (idx < 5) {
  273. td->qt_buffer[idx] = cpu_to_hc32(addr);
  274. td->qt_buffer_hi[idx] = 0;
  275. next = (addr + 4096) & ~4095;
  276. delta = next - addr;
  277. if (delta >= sz)
  278. break;
  279. sz -= delta;
  280. addr = next;
  281. idx++;
  282. }
  283. if (idx == 5) {
  284. debug("out of buffer pointers (%u bytes left)\n", sz);
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. static int
  290. ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
  291. int length, struct devrequest *req)
  292. {
  293. struct QH *qh;
  294. struct qTD *td;
  295. volatile struct qTD *vtd;
  296. unsigned long ts;
  297. uint32_t *tdp;
  298. uint32_t endpt, token, usbsts;
  299. uint32_t c, toggle;
  300. uint32_t cmd;
  301. int timeout;
  302. int ret = 0;
  303. debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
  304. buffer, length, req);
  305. if (req != NULL)
  306. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  307. req->request, req->request,
  308. req->requesttype, req->requesttype,
  309. le16_to_cpu(req->value), le16_to_cpu(req->value),
  310. le16_to_cpu(req->index));
  311. qh = ehci_alloc(sizeof(struct QH), 32);
  312. if (qh == NULL) {
  313. debug("unable to allocate QH\n");
  314. return -1;
  315. }
  316. qh->qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
  317. c = (usb_pipespeed(pipe) != USB_SPEED_HIGH &&
  318. usb_pipeendpoint(pipe) == 0) ? 1 : 0;
  319. endpt = (8 << 28) |
  320. (c << 27) |
  321. (usb_maxpacket(dev, pipe) << 16) |
  322. (0 << 15) |
  323. (1 << 14) |
  324. (usb_pipespeed(pipe) << 12) |
  325. (usb_pipeendpoint(pipe) << 8) |
  326. (0 << 7) | (usb_pipedevice(pipe) << 0);
  327. qh->qh_endpt1 = cpu_to_hc32(endpt);
  328. endpt = (1 << 30) |
  329. (dev->portnr << 23) |
  330. (dev->parent->devnum << 16) | (0 << 8) | (0 << 0);
  331. qh->qh_endpt2 = cpu_to_hc32(endpt);
  332. qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  333. td = NULL;
  334. tdp = &qh->qh_overlay.qt_next;
  335. toggle =
  336. usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  337. if (req != NULL) {
  338. td = ehci_alloc(sizeof(struct qTD), 32);
  339. if (td == NULL) {
  340. debug("unable to allocate SETUP td\n");
  341. goto fail;
  342. }
  343. td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  344. td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  345. token = (0 << 31) |
  346. (sizeof(*req) << 16) |
  347. (0 << 15) | (0 << 12) | (3 << 10) | (2 << 8) | (0x80 << 0);
  348. td->qt_token = cpu_to_hc32(token);
  349. if (ehci_td_buffer(td, req, sizeof(*req)) != 0) {
  350. debug("unable construct SETUP td\n");
  351. ehci_free(td, sizeof(*td));
  352. goto fail;
  353. }
  354. *tdp = cpu_to_hc32((uint32_t) td);
  355. tdp = &td->qt_next;
  356. toggle = 1;
  357. }
  358. if (length > 0 || req == NULL) {
  359. td = ehci_alloc(sizeof(struct qTD), 32);
  360. if (td == NULL) {
  361. debug("unable to allocate DATA td\n");
  362. goto fail;
  363. }
  364. td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  365. td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  366. token = (toggle << 31) |
  367. (length << 16) |
  368. ((req == NULL ? 1 : 0) << 15) |
  369. (0 << 12) |
  370. (3 << 10) |
  371. ((usb_pipein(pipe) ? 1 : 0) << 8) | (0x80 << 0);
  372. td->qt_token = cpu_to_hc32(token);
  373. if (ehci_td_buffer(td, buffer, length) != 0) {
  374. debug("unable construct DATA td\n");
  375. ehci_free(td, sizeof(*td));
  376. goto fail;
  377. }
  378. *tdp = cpu_to_hc32((uint32_t) td);
  379. tdp = &td->qt_next;
  380. }
  381. if (req != NULL) {
  382. td = ehci_alloc(sizeof(struct qTD), 32);
  383. if (td == NULL) {
  384. debug("unable to allocate ACK td\n");
  385. goto fail;
  386. }
  387. td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  388. td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  389. token = (toggle << 31) |
  390. (0 << 16) |
  391. (1 << 15) |
  392. (0 << 12) |
  393. (3 << 10) |
  394. ((usb_pipein(pipe) ? 0 : 1) << 8) | (0x80 << 0);
  395. td->qt_token = cpu_to_hc32(token);
  396. *tdp = cpu_to_hc32((uint32_t) td);
  397. tdp = &td->qt_next;
  398. }
  399. qh_list.qh_link = cpu_to_hc32((uint32_t) qh | QH_LINK_TYPE_QH);
  400. /* Flush dcache */
  401. ehci_flush_dcache(&qh_list);
  402. usbsts = ehci_readl(&hcor->or_usbsts);
  403. ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f));
  404. /* Enable async. schedule. */
  405. cmd = ehci_readl(&hcor->or_usbcmd);
  406. cmd |= CMD_ASE;
  407. ehci_writel(&hcor->or_usbcmd, cmd);
  408. ret = handshake((uint32_t *)&hcor->or_usbsts, STD_ASS, STD_ASS,
  409. 100 * 1000);
  410. if (ret < 0) {
  411. printf("EHCI fail timeout STD_ASS set\n");
  412. goto fail;
  413. }
  414. /* Wait for TDs to be processed. */
  415. ts = get_timer(0);
  416. vtd = td;
  417. timeout = USB_TIMEOUT_MS(pipe);
  418. do {
  419. /* Invalidate dcache */
  420. ehci_invalidate_dcache(&qh_list);
  421. token = hc32_to_cpu(vtd->qt_token);
  422. if (!(token & 0x80))
  423. break;
  424. WATCHDOG_RESET();
  425. } while (get_timer(ts) < timeout);
  426. /* Check that the TD processing happened */
  427. if (token & 0x80) {
  428. printf("EHCI timed out on TD - token=%#x\n", token);
  429. }
  430. /* Disable async schedule. */
  431. cmd = ehci_readl(&hcor->or_usbcmd);
  432. cmd &= ~CMD_ASE;
  433. ehci_writel(&hcor->or_usbcmd, cmd);
  434. ret = handshake((uint32_t *)&hcor->or_usbsts, STD_ASS, 0,
  435. 100 * 1000);
  436. if (ret < 0) {
  437. printf("EHCI fail timeout STD_ASS reset\n");
  438. goto fail;
  439. }
  440. qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
  441. token = hc32_to_cpu(qh->qh_overlay.qt_token);
  442. if (!(token & 0x80)) {
  443. debug("TOKEN=%#x\n", token);
  444. switch (token & 0xfc) {
  445. case 0:
  446. toggle = token >> 31;
  447. usb_settoggle(dev, usb_pipeendpoint(pipe),
  448. usb_pipeout(pipe), toggle);
  449. dev->status = 0;
  450. break;
  451. case 0x40:
  452. dev->status = USB_ST_STALLED;
  453. break;
  454. case 0xa0:
  455. case 0x20:
  456. dev->status = USB_ST_BUF_ERR;
  457. break;
  458. case 0x50:
  459. case 0x10:
  460. dev->status = USB_ST_BABBLE_DET;
  461. break;
  462. default:
  463. dev->status = USB_ST_CRC_ERR;
  464. if ((token & 0x40) == 0x40)
  465. dev->status |= USB_ST_STALLED;
  466. break;
  467. }
  468. dev->act_len = length - ((token >> 16) & 0x7fff);
  469. } else {
  470. dev->act_len = 0;
  471. debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
  472. dev->devnum, ehci_readl(&hcor->or_usbsts),
  473. ehci_readl(&hcor->or_portsc[0]),
  474. ehci_readl(&hcor->or_portsc[1]));
  475. }
  476. return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
  477. fail:
  478. td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next);
  479. while (td != (void *)QT_NEXT_TERMINATE) {
  480. qh->qh_overlay.qt_next = td->qt_next;
  481. ehci_free(td, sizeof(*td));
  482. td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next);
  483. }
  484. ehci_free(qh, sizeof(*qh));
  485. return -1;
  486. }
  487. static inline int min3(int a, int b, int c)
  488. {
  489. if (b < a)
  490. a = b;
  491. if (c < a)
  492. a = c;
  493. return a;
  494. }
  495. int
  496. ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
  497. int length, struct devrequest *req)
  498. {
  499. uint8_t tmpbuf[4];
  500. u16 typeReq;
  501. void *srcptr = NULL;
  502. int len, srclen;
  503. uint32_t reg;
  504. uint32_t *status_reg;
  505. if (le16_to_cpu(req->index) > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
  506. printf("The request port(%d) is not configured\n",
  507. le16_to_cpu(req->index) - 1);
  508. return -1;
  509. }
  510. status_reg = (uint32_t *)&hcor->or_portsc[
  511. le16_to_cpu(req->index) - 1];
  512. srclen = 0;
  513. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  514. req->request, req->request,
  515. req->requesttype, req->requesttype,
  516. le16_to_cpu(req->value), le16_to_cpu(req->index));
  517. typeReq = req->request | req->requesttype << 8;
  518. switch (typeReq) {
  519. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  520. switch (le16_to_cpu(req->value) >> 8) {
  521. case USB_DT_DEVICE:
  522. debug("USB_DT_DEVICE request\n");
  523. srcptr = &descriptor.device;
  524. srclen = 0x12;
  525. break;
  526. case USB_DT_CONFIG:
  527. debug("USB_DT_CONFIG config\n");
  528. srcptr = &descriptor.config;
  529. srclen = 0x19;
  530. break;
  531. case USB_DT_STRING:
  532. debug("USB_DT_STRING config\n");
  533. switch (le16_to_cpu(req->value) & 0xff) {
  534. case 0: /* Language */
  535. srcptr = "\4\3\1\0";
  536. srclen = 4;
  537. break;
  538. case 1: /* Vendor */
  539. srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
  540. srclen = 14;
  541. break;
  542. case 2: /* Product */
  543. srcptr = "\52\3E\0H\0C\0I\0 "
  544. "\0H\0o\0s\0t\0 "
  545. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  546. srclen = 42;
  547. break;
  548. default:
  549. debug("unknown value DT_STRING %x\n",
  550. le16_to_cpu(req->value));
  551. goto unknown;
  552. }
  553. break;
  554. default:
  555. debug("unknown value %x\n", le16_to_cpu(req->value));
  556. goto unknown;
  557. }
  558. break;
  559. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  560. switch (le16_to_cpu(req->value) >> 8) {
  561. case USB_DT_HUB:
  562. debug("USB_DT_HUB config\n");
  563. srcptr = &descriptor.hub;
  564. srclen = 0x8;
  565. break;
  566. default:
  567. debug("unknown value %x\n", le16_to_cpu(req->value));
  568. goto unknown;
  569. }
  570. break;
  571. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  572. debug("USB_REQ_SET_ADDRESS\n");
  573. rootdev = le16_to_cpu(req->value);
  574. break;
  575. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  576. debug("USB_REQ_SET_CONFIGURATION\n");
  577. /* Nothing to do */
  578. break;
  579. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  580. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  581. tmpbuf[1] = 0;
  582. srcptr = tmpbuf;
  583. srclen = 2;
  584. break;
  585. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  586. memset(tmpbuf, 0, 4);
  587. reg = ehci_readl(status_reg);
  588. if (reg & EHCI_PS_CS)
  589. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  590. if (reg & EHCI_PS_PE)
  591. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  592. if (reg & EHCI_PS_SUSP)
  593. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  594. if (reg & EHCI_PS_OCA)
  595. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  596. if (reg & EHCI_PS_PR)
  597. tmpbuf[0] |= USB_PORT_STAT_RESET;
  598. if (reg & EHCI_PS_PP)
  599. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  600. if (ehci_is_TDI()) {
  601. switch ((reg >> 26) & 3) {
  602. case 0:
  603. break;
  604. case 1:
  605. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  606. break;
  607. case 2:
  608. default:
  609. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  610. break;
  611. }
  612. } else {
  613. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  614. }
  615. if (reg & EHCI_PS_CSC)
  616. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  617. if (reg & EHCI_PS_PEC)
  618. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  619. if (reg & EHCI_PS_OCC)
  620. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  621. if (portreset & (1 << le16_to_cpu(req->index)))
  622. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  623. srcptr = tmpbuf;
  624. srclen = 4;
  625. break;
  626. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  627. reg = ehci_readl(status_reg);
  628. reg &= ~EHCI_PS_CLEAR;
  629. switch (le16_to_cpu(req->value)) {
  630. case USB_PORT_FEAT_ENABLE:
  631. reg |= EHCI_PS_PE;
  632. ehci_writel(status_reg, reg);
  633. break;
  634. case USB_PORT_FEAT_POWER:
  635. if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) {
  636. reg |= EHCI_PS_PP;
  637. ehci_writel(status_reg, reg);
  638. }
  639. break;
  640. case USB_PORT_FEAT_RESET:
  641. if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
  642. !ehci_is_TDI() &&
  643. EHCI_PS_IS_LOWSPEED(reg)) {
  644. /* Low speed device, give up ownership. */
  645. debug("port %d low speed --> companion\n",
  646. req->index - 1);
  647. reg |= EHCI_PS_PO;
  648. ehci_writel(status_reg, reg);
  649. break;
  650. } else {
  651. int ret;
  652. reg |= EHCI_PS_PR;
  653. reg &= ~EHCI_PS_PE;
  654. ehci_writel(status_reg, reg);
  655. /*
  656. * caller must wait, then call GetPortStatus
  657. * usb 2.0 specification say 50 ms resets on
  658. * root
  659. */
  660. ehci_powerup_fixup(status_reg, &reg);
  661. ehci_writel(status_reg, reg & ~EHCI_PS_PR);
  662. /*
  663. * A host controller must terminate the reset
  664. * and stabilize the state of the port within
  665. * 2 milliseconds
  666. */
  667. ret = handshake(status_reg, EHCI_PS_PR, 0,
  668. 2 * 1000);
  669. if (!ret)
  670. portreset |=
  671. 1 << le16_to_cpu(req->index);
  672. else
  673. printf("port(%d) reset error\n",
  674. le16_to_cpu(req->index) - 1);
  675. }
  676. break;
  677. default:
  678. debug("unknown feature %x\n", le16_to_cpu(req->value));
  679. goto unknown;
  680. }
  681. /* unblock posted writes */
  682. (void) ehci_readl(&hcor->or_usbcmd);
  683. break;
  684. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  685. reg = ehci_readl(status_reg);
  686. switch (le16_to_cpu(req->value)) {
  687. case USB_PORT_FEAT_ENABLE:
  688. reg &= ~EHCI_PS_PE;
  689. break;
  690. case USB_PORT_FEAT_C_ENABLE:
  691. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE;
  692. break;
  693. case USB_PORT_FEAT_POWER:
  694. if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams)))
  695. reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP);
  696. case USB_PORT_FEAT_C_CONNECTION:
  697. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC;
  698. break;
  699. case USB_PORT_FEAT_OVER_CURRENT:
  700. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC;
  701. break;
  702. case USB_PORT_FEAT_C_RESET:
  703. portreset &= ~(1 << le16_to_cpu(req->index));
  704. break;
  705. default:
  706. debug("unknown feature %x\n", le16_to_cpu(req->value));
  707. goto unknown;
  708. }
  709. ehci_writel(status_reg, reg);
  710. /* unblock posted write */
  711. (void) ehci_readl(&hcor->or_usbcmd);
  712. break;
  713. default:
  714. debug("Unknown request\n");
  715. goto unknown;
  716. }
  717. wait_ms(1);
  718. len = min3(srclen, le16_to_cpu(req->length), length);
  719. if (srcptr != NULL && len > 0)
  720. memcpy(buffer, srcptr, len);
  721. else
  722. debug("Len is 0\n");
  723. dev->act_len = len;
  724. dev->status = 0;
  725. return 0;
  726. unknown:
  727. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  728. req->requesttype, req->request, le16_to_cpu(req->value),
  729. le16_to_cpu(req->index), le16_to_cpu(req->length));
  730. dev->act_len = 0;
  731. dev->status = USB_ST_STALLED;
  732. return -1;
  733. }
  734. int usb_lowlevel_stop(void)
  735. {
  736. return ehci_hcd_stop();
  737. }
  738. int usb_lowlevel_init(void)
  739. {
  740. uint32_t reg;
  741. uint32_t cmd;
  742. if (ehci_hcd_init() != 0)
  743. return -1;
  744. /* EHCI spec section 4.1 */
  745. if (ehci_reset() != 0)
  746. return -1;
  747. #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
  748. if (ehci_hcd_init() != 0)
  749. return -1;
  750. #endif
  751. /* Set head of reclaim list */
  752. memset(&qh_list, 0, sizeof(qh_list));
  753. qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
  754. qh_list.qh_endpt1 = cpu_to_hc32((1 << 15) | (USB_SPEED_HIGH << 12));
  755. qh_list.qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
  756. qh_list.qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  757. qh_list.qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  758. qh_list.qh_overlay.qt_token = cpu_to_hc32(0x40);
  759. /* Set async. queue head pointer. */
  760. ehci_writel(&hcor->or_asynclistaddr, (uint32_t)&qh_list);
  761. reg = ehci_readl(&hccr->cr_hcsparams);
  762. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  763. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  764. /* Port Indicators */
  765. if (HCS_INDICATOR(reg))
  766. descriptor.hub.wHubCharacteristics |= 0x80;
  767. /* Port Power Control */
  768. if (HCS_PPC(reg))
  769. descriptor.hub.wHubCharacteristics |= 0x01;
  770. /* Start the host controller. */
  771. cmd = ehci_readl(&hcor->or_usbcmd);
  772. /*
  773. * Philips, Intel, and maybe others need CMD_RUN before the
  774. * root hub will detect new devices (why?); NEC doesn't
  775. */
  776. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  777. cmd |= CMD_RUN;
  778. ehci_writel(&hcor->or_usbcmd, cmd);
  779. /* take control over the ports */
  780. cmd = ehci_readl(&hcor->or_configflag);
  781. cmd |= FLAG_CF;
  782. ehci_writel(&hcor->or_configflag, cmd);
  783. /* unblock posted write */
  784. cmd = ehci_readl(&hcor->or_usbcmd);
  785. wait_ms(5);
  786. reg = HC_VERSION(ehci_readl(&hccr->cr_capbase));
  787. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  788. rootdev = 0;
  789. return 0;
  790. }
  791. int
  792. submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  793. int length)
  794. {
  795. if (usb_pipetype(pipe) != PIPE_BULK) {
  796. debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  797. return -1;
  798. }
  799. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  800. }
  801. int
  802. submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  803. int length, struct devrequest *setup)
  804. {
  805. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  806. debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
  807. return -1;
  808. }
  809. if (usb_pipedevice(pipe) == rootdev) {
  810. if (rootdev == 0)
  811. dev->speed = USB_SPEED_HIGH;
  812. return ehci_submit_root(dev, pipe, buffer, length, setup);
  813. }
  814. return ehci_submit_async(dev, pipe, buffer, length, setup);
  815. }
  816. int
  817. submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  818. int length, int interval)
  819. {
  820. debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
  821. dev, pipe, buffer, length, interval);
  822. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  823. }
  824. #ifdef CONFIG_SYS_USB_EVENT_POLL
  825. /*
  826. * This function polls for USB keyboard data.
  827. */
  828. void usb_event_poll()
  829. {
  830. struct stdio_dev *dev;
  831. struct usb_device *usb_kbd_dev;
  832. struct usb_interface *iface;
  833. struct usb_endpoint_descriptor *ep;
  834. int pipe;
  835. int maxp;
  836. /* Get the pointer to USB Keyboard device pointer */
  837. dev = stdio_get_by_name("usbkbd");
  838. usb_kbd_dev = (struct usb_device *)dev->priv;
  839. iface = &usb_kbd_dev->config.if_desc[0];
  840. ep = &iface->ep_desc[0];
  841. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  842. /* Submit a interrupt transfer request */
  843. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  844. usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
  845. maxp > 8 ? 8 : maxp, ep->bInterval);
  846. }
  847. #endif /* CONFIG_SYS_USB_EVENT_POLL */