usb_ehci_core.c 19 KB

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