mv_udc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright 2011, Marvell Semiconductor Inc.
  3. * Lei Wen <leiwen@marvell.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  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; either version 2 of
  11. * the License, or (at your option) any later version.
  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. * Back ported to the 8xx platform (from the 8260 platform) by
  24. * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <config.h>
  29. #include <net.h>
  30. #include <malloc.h>
  31. #include <asm/io.h>
  32. #include <linux/types.h>
  33. #include <usb/mv_udc.h>
  34. #ifndef DEBUG
  35. #define DBG(x...) do {} while (0)
  36. #else
  37. #define DBG(x...) printf(x)
  38. static const char *reqname(unsigned r)
  39. {
  40. switch (r) {
  41. case USB_REQ_GET_STATUS: return "GET_STATUS";
  42. case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
  43. case USB_REQ_SET_FEATURE: return "SET_FEATURE";
  44. case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
  45. case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
  46. case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
  47. case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
  48. case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
  49. case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
  50. case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
  51. default: return "*UNKNOWN*";
  52. }
  53. }
  54. #endif
  55. #define PAGE_SIZE 4096
  56. #define QH_MAXNUM 32
  57. static struct usb_endpoint_descriptor ep0_out_desc = {
  58. .bLength = sizeof(struct usb_endpoint_descriptor),
  59. .bDescriptorType = USB_DT_ENDPOINT,
  60. .bEndpointAddress = 0,
  61. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  62. };
  63. static struct usb_endpoint_descriptor ep0_in_desc = {
  64. .bLength = sizeof(struct usb_endpoint_descriptor),
  65. .bDescriptorType = USB_DT_ENDPOINT,
  66. .bEndpointAddress = USB_DIR_IN,
  67. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  68. };
  69. struct ept_queue_head *epts;
  70. struct ept_queue_item *items[2 * NUM_ENDPOINTS];
  71. static int mv_pullup(struct usb_gadget *gadget, int is_on);
  72. static int mv_ep_enable(struct usb_ep *ep,
  73. const struct usb_endpoint_descriptor *desc);
  74. static int mv_ep_disable(struct usb_ep *ep);
  75. static int mv_ep_queue(struct usb_ep *ep,
  76. struct usb_request *req, gfp_t gfp_flags);
  77. static struct usb_request *
  78. mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
  79. static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
  80. static struct usb_gadget_ops mv_udc_ops = {
  81. .pullup = mv_pullup,
  82. };
  83. static struct usb_ep_ops mv_ep_ops = {
  84. .enable = mv_ep_enable,
  85. .disable = mv_ep_disable,
  86. .queue = mv_ep_queue,
  87. .alloc_request = mv_ep_alloc_request,
  88. .free_request = mv_ep_free_request,
  89. };
  90. static struct mv_ep ep[2 * NUM_ENDPOINTS];
  91. static struct mv_drv controller = {
  92. .gadget = {
  93. .ep0 = &ep[0].ep,
  94. .name = "mv_udc",
  95. },
  96. };
  97. static struct usb_request *
  98. mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
  99. {
  100. struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
  101. return &mv_ep->req;
  102. }
  103. static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
  104. {
  105. return;
  106. }
  107. static void ep_enable(int num, int in)
  108. {
  109. struct ept_queue_head *head;
  110. struct mv_udc *udc = controller.udc;
  111. unsigned n;
  112. head = epts + 2*num + in;
  113. n = readl(&udc->epctrl[num]);
  114. if (in)
  115. n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
  116. else
  117. n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
  118. if (num != 0)
  119. head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
  120. writel(n, &udc->epctrl[num]);
  121. }
  122. static int mv_ep_enable(struct usb_ep *ep,
  123. const struct usb_endpoint_descriptor *desc)
  124. {
  125. struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
  126. int num, in;
  127. num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  128. in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  129. ep_enable(num, in);
  130. mv_ep->desc = desc;
  131. return 0;
  132. }
  133. static int mv_ep_disable(struct usb_ep *ep)
  134. {
  135. return 0;
  136. }
  137. static int mv_ep_queue(struct usb_ep *ep,
  138. struct usb_request *req, gfp_t gfp_flags)
  139. {
  140. struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
  141. struct mv_udc *udc = controller.udc;
  142. struct ept_queue_item *item;
  143. struct ept_queue_head *head;
  144. unsigned phys;
  145. int bit, num, len, in;
  146. num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  147. in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
  148. item = items[2 * num + in];
  149. head = epts + 2 * num + in;
  150. phys = (unsigned)req->buf;
  151. len = req->length;
  152. item->next = TERMINATE;
  153. item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
  154. item->page0 = phys;
  155. item->page1 = (phys & 0xfffff000) + 0x1000;
  156. head->next = (unsigned) item;
  157. head->info = 0;
  158. DBG("ept%d %s queue len %x, buffer %x\n",
  159. num, in ? "in" : "out", len, phys);
  160. if (in)
  161. bit = EPT_TX(num);
  162. else
  163. bit = EPT_RX(num);
  164. flush_cache(phys, len);
  165. flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
  166. writel(bit, &udc->epprime);
  167. return 0;
  168. }
  169. static void handle_ep_complete(struct mv_ep *ep)
  170. {
  171. struct ept_queue_item *item;
  172. int num, in, len;
  173. num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  174. in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
  175. if (num == 0)
  176. ep->desc = &ep0_out_desc;
  177. item = items[2 * num + in];
  178. if (item->info & 0xff)
  179. printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
  180. num, in ? "in" : "out", item->info, item->page0);
  181. len = (item->info >> 16) & 0x7fff;
  182. ep->req.length -= len;
  183. DBG("ept%d %s complete %x\n",
  184. num, in ? "in" : "out", len);
  185. ep->req.complete(&ep->ep, &ep->req);
  186. if (num == 0) {
  187. ep->req.length = 0;
  188. usb_ep_queue(&ep->ep, &ep->req, 0);
  189. ep->desc = &ep0_in_desc;
  190. }
  191. }
  192. #define SETUP(type, request) (((type) << 8) | (request))
  193. static void handle_setup(void)
  194. {
  195. struct usb_request *req = &ep[0].req;
  196. struct mv_udc *udc = controller.udc;
  197. struct ept_queue_head *head;
  198. struct usb_ctrlrequest r;
  199. int status = 0;
  200. int num, in, _num, _in, i;
  201. char *buf;
  202. head = epts;
  203. flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
  204. memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
  205. writel(EPT_RX(0), &udc->epstat);
  206. DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
  207. r.bRequestType, r.bRequest, r.wIndex, r.wValue);
  208. switch (SETUP(r.bRequestType, r.bRequest)) {
  209. case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
  210. _num = r.wIndex & 15;
  211. _in = !!(r.wIndex & 0x80);
  212. if ((r.wValue == 0) && (r.wLength == 0)) {
  213. req->length = 0;
  214. for (i = 0; i < NUM_ENDPOINTS; i++) {
  215. if (!ep[i].desc)
  216. continue;
  217. num = ep[i].desc->bEndpointAddress
  218. & USB_ENDPOINT_NUMBER_MASK;
  219. in = (ep[i].desc->bEndpointAddress
  220. & USB_DIR_IN) != 0;
  221. if ((num == _num) && (in == _in)) {
  222. ep_enable(num, in);
  223. usb_ep_queue(controller.gadget.ep0,
  224. req, 0);
  225. break;
  226. }
  227. }
  228. }
  229. return;
  230. case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
  231. /*
  232. * write address delayed (will take effect
  233. * after the next IN txn)
  234. */
  235. writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
  236. req->length = 0;
  237. usb_ep_queue(controller.gadget.ep0, req, 0);
  238. return;
  239. case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
  240. req->length = 2;
  241. buf = (char *)req->buf;
  242. buf[0] = 1 << USB_DEVICE_SELF_POWERED;
  243. buf[1] = 0;
  244. usb_ep_queue(controller.gadget.ep0, req, 0);
  245. return;
  246. }
  247. /* pass request up to the gadget driver */
  248. if (controller.driver)
  249. status = controller.driver->setup(&controller.gadget, &r);
  250. else
  251. status = -ENODEV;
  252. if (!status)
  253. return;
  254. DBG("STALL reqname %s type %x value %x, index %x\n",
  255. reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
  256. writel((1<<16) | (1 << 0), &udc->epctrl[0]);
  257. }
  258. static void stop_activity(void)
  259. {
  260. int i, num, in;
  261. struct ept_queue_head *head;
  262. struct mv_udc *udc = controller.udc;
  263. writel(readl(&udc->epcomp), &udc->epcomp);
  264. writel(readl(&udc->epstat), &udc->epstat);
  265. writel(0xffffffff, &udc->epflush);
  266. /* error out any pending reqs */
  267. for (i = 0; i < NUM_ENDPOINTS; i++) {
  268. if (i != 0)
  269. writel(0, &udc->epctrl[i]);
  270. if (ep[i].desc) {
  271. num = ep[i].desc->bEndpointAddress
  272. & USB_ENDPOINT_NUMBER_MASK;
  273. in = (ep[i].desc->bEndpointAddress & USB_DIR_IN) != 0;
  274. head = epts + (num * 2) + (in);
  275. head->info = INFO_ACTIVE;
  276. }
  277. }
  278. }
  279. void udc_irq(void)
  280. {
  281. struct mv_udc *udc = controller.udc;
  282. unsigned n = readl(&udc->usbsts);
  283. writel(n, &udc->usbsts);
  284. int bit, i, num, in;
  285. n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
  286. if (n == 0)
  287. return;
  288. if (n & STS_URI) {
  289. DBG("-- reset --\n");
  290. stop_activity();
  291. }
  292. if (n & STS_SLI)
  293. DBG("-- suspend --\n");
  294. if (n & STS_PCI) {
  295. DBG("-- portchange --\n");
  296. bit = (readl(&udc->portsc) >> 26) & 3;
  297. if (bit == 2) {
  298. controller.gadget.speed = USB_SPEED_HIGH;
  299. for (i = 1; i < NUM_ENDPOINTS && n; i++)
  300. if (ep[i].desc)
  301. ep[i].ep.maxpacket = 512;
  302. } else {
  303. controller.gadget.speed = USB_SPEED_FULL;
  304. }
  305. }
  306. if (n & STS_UEI)
  307. printf("<UEI %x>\n", readl(&udc->epcomp));
  308. if ((n & STS_UI) || (n & STS_UEI)) {
  309. n = readl(&udc->epstat);
  310. if (n & EPT_RX(0))
  311. handle_setup();
  312. n = readl(&udc->epcomp);
  313. if (n != 0)
  314. writel(n, &udc->epcomp);
  315. for (i = 0; i < NUM_ENDPOINTS && n; i++) {
  316. if (ep[i].desc) {
  317. num = ep[i].desc->bEndpointAddress
  318. & USB_ENDPOINT_NUMBER_MASK;
  319. in = (ep[i].desc->bEndpointAddress
  320. & USB_DIR_IN) != 0;
  321. bit = (in) ? EPT_TX(num) : EPT_RX(num);
  322. if (n & bit)
  323. handle_ep_complete(&ep[i]);
  324. }
  325. }
  326. }
  327. }
  328. int usb_gadget_handle_interrupts(void)
  329. {
  330. u32 value;
  331. struct mv_udc *udc = controller.udc;
  332. value = readl(&udc->usbsts);
  333. if (value)
  334. udc_irq();
  335. return value;
  336. }
  337. static int mv_pullup(struct usb_gadget *gadget, int is_on)
  338. {
  339. struct mv_udc *udc = controller.udc;
  340. if (is_on) {
  341. /* RESET */
  342. writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
  343. udelay(200);
  344. writel((unsigned) epts, &udc->epinitaddr);
  345. /* select DEVICE mode */
  346. writel(USBMODE_DEVICE, &udc->usbmode);
  347. writel(0xffffffff, &udc->epflush);
  348. /* Turn on the USB connection by enabling the pullup resistor */
  349. writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
  350. } else {
  351. stop_activity();
  352. writel(USBCMD_FS2, &udc->usbcmd);
  353. udelay(800);
  354. if (controller.driver)
  355. controller.driver->disconnect(gadget);
  356. }
  357. return 0;
  358. }
  359. void udc_disconnect(void)
  360. {
  361. struct mv_udc *udc = controller.udc;
  362. /* disable pullup */
  363. stop_activity();
  364. writel(USBCMD_FS2, &udc->usbcmd);
  365. udelay(800);
  366. if (controller.driver)
  367. controller.driver->disconnect(&controller.gadget);
  368. }
  369. static int mvudc_probe(void)
  370. {
  371. struct ept_queue_head *head;
  372. int i;
  373. controller.gadget.ops = &mv_udc_ops;
  374. controller.udc = (struct mv_udc *)CONFIG_USB_REG_BASE;
  375. epts = memalign(PAGE_SIZE, QH_MAXNUM * sizeof(struct ept_queue_head));
  376. memset(epts, 0, QH_MAXNUM * sizeof(struct ept_queue_head));
  377. for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
  378. /*
  379. * For item0 and item1, they are served as ep0
  380. * out&in seperately
  381. */
  382. head = epts + i;
  383. if (i < 2)
  384. head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
  385. | CONFIG_ZLT | CONFIG_IOS;
  386. else
  387. head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
  388. | CONFIG_ZLT;
  389. head->next = TERMINATE;
  390. head->info = 0;
  391. items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item));
  392. }
  393. INIT_LIST_HEAD(&controller.gadget.ep_list);
  394. ep[0].ep.maxpacket = 64;
  395. ep[0].ep.name = "ep0";
  396. ep[0].desc = &ep0_in_desc;
  397. INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
  398. for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
  399. if (i != 0) {
  400. ep[i].ep.maxpacket = 512;
  401. ep[i].ep.name = "ep-";
  402. list_add_tail(&ep[i].ep.ep_list,
  403. &controller.gadget.ep_list);
  404. ep[i].desc = NULL;
  405. }
  406. ep[i].ep.ops = &mv_ep_ops;
  407. }
  408. return 0;
  409. }
  410. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  411. {
  412. struct mv_udc *udc = controller.udc;
  413. int retval;
  414. if (!driver
  415. || driver->speed < USB_SPEED_FULL
  416. || !driver->bind
  417. || !driver->setup) {
  418. DBG("bad parameter.\n");
  419. return -EINVAL;
  420. }
  421. if (!mvudc_probe()) {
  422. usb_lowlevel_init();
  423. /* select ULPI phy */
  424. writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
  425. }
  426. retval = driver->bind(&controller.gadget);
  427. if (retval) {
  428. DBG("driver->bind() returned %d\n", retval);
  429. return retval;
  430. }
  431. controller.driver = driver;
  432. return 0;
  433. }
  434. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  435. {
  436. return 0;
  437. }