usb_ehci_core.c 19 KB

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