ehci-hcd.c 22 KB

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