musb_uboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #include <common.h>
  2. #include <console.h>
  3. #include <watchdog.h>
  4. #ifdef CONFIG_ARCH_SUNXI
  5. #include <asm/arch/usb_phy.h>
  6. #endif
  7. #include <linux/errno.h>
  8. #include <linux/usb/ch9.h>
  9. #include <linux/usb/gadget.h>
  10. #include <usb.h>
  11. #include "linux-compat.h"
  12. #include "usb-compat.h"
  13. #include "musb_core.h"
  14. #include "musb_host.h"
  15. #include "musb_gadget.h"
  16. #include "musb_uboot.h"
  17. #ifdef CONFIG_USB_MUSB_HOST
  18. struct int_queue {
  19. struct usb_host_endpoint hep;
  20. struct urb urb;
  21. };
  22. #ifndef CONFIG_DM_USB
  23. struct musb_host_data musb_host;
  24. #endif
  25. static void musb_host_complete_urb(struct urb *urb)
  26. {
  27. urb->dev->status &= ~USB_ST_NOT_PROC;
  28. urb->dev->act_len = urb->actual_length;
  29. }
  30. static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
  31. struct usb_device *dev, int endpoint_type,
  32. unsigned long pipe, void *buffer, int len,
  33. struct devrequest *setup, int interval)
  34. {
  35. int epnum = usb_pipeendpoint(pipe);
  36. int is_in = usb_pipein(pipe);
  37. memset(urb, 0, sizeof(struct urb));
  38. memset(hep, 0, sizeof(struct usb_host_endpoint));
  39. INIT_LIST_HEAD(&hep->urb_list);
  40. INIT_LIST_HEAD(&urb->urb_list);
  41. urb->ep = hep;
  42. urb->complete = musb_host_complete_urb;
  43. urb->status = -EINPROGRESS;
  44. urb->dev = dev;
  45. urb->pipe = pipe;
  46. urb->transfer_buffer = buffer;
  47. urb->transfer_dma = (unsigned long)buffer;
  48. urb->transfer_buffer_length = len;
  49. urb->setup_packet = (unsigned char *)setup;
  50. urb->ep->desc.wMaxPacketSize =
  51. __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
  52. dev->epmaxpacketout[epnum]);
  53. urb->ep->desc.bmAttributes = endpoint_type;
  54. urb->ep->desc.bEndpointAddress =
  55. (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
  56. urb->ep->desc.bInterval = interval;
  57. }
  58. static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
  59. {
  60. struct musb *host = hcd->hcd_priv;
  61. int ret;
  62. unsigned long timeout;
  63. ret = musb_urb_enqueue(hcd, urb, 0);
  64. if (ret < 0) {
  65. printf("Failed to enqueue URB to controller\n");
  66. return ret;
  67. }
  68. timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
  69. do {
  70. if (ctrlc())
  71. return -EIO;
  72. host->isr(0, host);
  73. } while (urb->status == -EINPROGRESS &&
  74. get_timer(0) < timeout);
  75. if (urb->status == -EINPROGRESS)
  76. musb_urb_dequeue(hcd, urb, -ETIME);
  77. return urb->status;
  78. }
  79. static int _musb_submit_control_msg(struct musb_host_data *host,
  80. struct usb_device *dev, unsigned long pipe,
  81. void *buffer, int len, struct devrequest *setup)
  82. {
  83. construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
  84. pipe, buffer, len, setup, 0);
  85. /* Fix speed for non hub-attached devices */
  86. if (!usb_dev_get_parent(dev))
  87. dev->speed = host->host_speed;
  88. return submit_urb(&host->hcd, &host->urb);
  89. }
  90. static int _musb_submit_bulk_msg(struct musb_host_data *host,
  91. struct usb_device *dev, unsigned long pipe, void *buffer, int len)
  92. {
  93. construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
  94. pipe, buffer, len, NULL, 0);
  95. return submit_urb(&host->hcd, &host->urb);
  96. }
  97. static int _musb_submit_int_msg(struct musb_host_data *host,
  98. struct usb_device *dev, unsigned long pipe,
  99. void *buffer, int len, int interval)
  100. {
  101. construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
  102. buffer, len, NULL, interval);
  103. return submit_urb(&host->hcd, &host->urb);
  104. }
  105. static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
  106. struct usb_device *dev, unsigned long pipe, int queuesize,
  107. int elementsize, void *buffer, int interval)
  108. {
  109. struct int_queue *queue;
  110. int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
  111. if (queuesize != 1) {
  112. printf("ERROR musb int-queues only support queuesize 1\n");
  113. return NULL;
  114. }
  115. if (dev->int_pending & (1 << index)) {
  116. printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
  117. return NULL;
  118. }
  119. queue = malloc(sizeof(*queue));
  120. if (!queue)
  121. return NULL;
  122. construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
  123. pipe, buffer, elementsize, NULL, interval);
  124. ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
  125. if (ret < 0) {
  126. printf("Failed to enqueue URB to controller\n");
  127. free(queue);
  128. return NULL;
  129. }
  130. dev->int_pending |= 1 << index;
  131. return queue;
  132. }
  133. static int _musb_destroy_int_queue(struct musb_host_data *host,
  134. struct usb_device *dev, struct int_queue *queue)
  135. {
  136. int index = usb_pipein(queue->urb.pipe) * 16 +
  137. usb_pipeendpoint(queue->urb.pipe);
  138. if (queue->urb.status == -EINPROGRESS)
  139. musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
  140. dev->int_pending &= ~(1 << index);
  141. free(queue);
  142. return 0;
  143. }
  144. static void *_musb_poll_int_queue(struct musb_host_data *host,
  145. struct usb_device *dev, struct int_queue *queue)
  146. {
  147. if (queue->urb.status != -EINPROGRESS)
  148. return NULL; /* URB has already completed in a prev. poll */
  149. host->host->isr(0, host->host);
  150. if (queue->urb.status != -EINPROGRESS)
  151. return queue->urb.transfer_buffer; /* Done */
  152. return NULL; /* URB still pending */
  153. }
  154. static int _musb_reset_root_port(struct musb_host_data *host,
  155. struct usb_device *dev)
  156. {
  157. void *mbase = host->host->mregs;
  158. u8 power;
  159. power = musb_readb(mbase, MUSB_POWER);
  160. power &= 0xf0;
  161. musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
  162. mdelay(50);
  163. if (host->host->ops->pre_root_reset_end)
  164. host->host->ops->pre_root_reset_end(host->host);
  165. power = musb_readb(mbase, MUSB_POWER);
  166. musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
  167. if (host->host->ops->post_root_reset_end)
  168. host->host->ops->post_root_reset_end(host->host);
  169. host->host->isr(0, host->host);
  170. host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
  171. USB_SPEED_HIGH :
  172. (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
  173. USB_SPEED_FULL : USB_SPEED_LOW;
  174. mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
  175. return 0;
  176. }
  177. int musb_lowlevel_init(struct musb_host_data *host)
  178. {
  179. void *mbase;
  180. /* USB spec says it may take up to 1 second for a device to connect */
  181. unsigned long timeout = get_timer(0) + 1000;
  182. int ret;
  183. if (!host->host) {
  184. printf("MUSB host is not registered\n");
  185. return -ENODEV;
  186. }
  187. ret = musb_start(host->host);
  188. if (ret)
  189. return ret;
  190. mbase = host->host->mregs;
  191. do {
  192. if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
  193. break;
  194. } while (get_timer(0) < timeout);
  195. if (get_timer(0) >= timeout) {
  196. musb_stop(host->host);
  197. return -ENODEV;
  198. }
  199. _musb_reset_root_port(host, NULL);
  200. host->host->is_active = 1;
  201. host->hcd.hcd_priv = host->host;
  202. return 0;
  203. }
  204. #ifndef CONFIG_DM_USB
  205. int usb_lowlevel_stop(int index)
  206. {
  207. if (!musb_host.host) {
  208. printf("MUSB host is not registered\n");
  209. return -ENODEV;
  210. }
  211. musb_stop(musb_host.host);
  212. return 0;
  213. }
  214. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  215. void *buffer, int length)
  216. {
  217. return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
  218. }
  219. int submit_control_msg(struct usb_device *dev, unsigned long pipe,
  220. void *buffer, int length, struct devrequest *setup)
  221. {
  222. return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
  223. }
  224. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  225. void *buffer, int length, int interval)
  226. {
  227. return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, interval);
  228. }
  229. struct int_queue *create_int_queue(struct usb_device *dev,
  230. unsigned long pipe, int queuesize, int elementsize,
  231. void *buffer, int interval)
  232. {
  233. return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
  234. buffer, interval);
  235. }
  236. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
  237. {
  238. return _musb_poll_int_queue(&musb_host, dev, queue);
  239. }
  240. int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
  241. {
  242. return _musb_destroy_int_queue(&musb_host, dev, queue);
  243. }
  244. int usb_reset_root_port(struct usb_device *dev)
  245. {
  246. return _musb_reset_root_port(&musb_host, dev);
  247. }
  248. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  249. {
  250. return musb_lowlevel_init(&musb_host);
  251. }
  252. #endif /* !CONFIG_DM_USB */
  253. #ifdef CONFIG_DM_USB
  254. static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev,
  255. unsigned long pipe, void *buffer, int length,
  256. struct devrequest *setup)
  257. {
  258. struct musb_host_data *host = dev_get_priv(dev);
  259. return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
  260. }
  261. static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
  262. unsigned long pipe, void *buffer, int length)
  263. {
  264. struct musb_host_data *host = dev_get_priv(dev);
  265. return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
  266. }
  267. static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
  268. unsigned long pipe, void *buffer, int length,
  269. int interval)
  270. {
  271. struct musb_host_data *host = dev_get_priv(dev);
  272. return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval);
  273. }
  274. static struct int_queue *musb_create_int_queue(struct udevice *dev,
  275. struct usb_device *udev, unsigned long pipe, int queuesize,
  276. int elementsize, void *buffer, int interval)
  277. {
  278. struct musb_host_data *host = dev_get_priv(dev);
  279. return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
  280. buffer, interval);
  281. }
  282. static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
  283. struct int_queue *queue)
  284. {
  285. struct musb_host_data *host = dev_get_priv(dev);
  286. return _musb_poll_int_queue(host, udev, queue);
  287. }
  288. static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
  289. struct int_queue *queue)
  290. {
  291. struct musb_host_data *host = dev_get_priv(dev);
  292. return _musb_destroy_int_queue(host, udev, queue);
  293. }
  294. static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
  295. {
  296. struct musb_host_data *host = dev_get_priv(dev);
  297. return _musb_reset_root_port(host, udev);
  298. }
  299. struct dm_usb_ops musb_usb_ops = {
  300. .control = musb_submit_control_msg,
  301. .bulk = musb_submit_bulk_msg,
  302. .interrupt = musb_submit_int_msg,
  303. .create_int_queue = musb_create_int_queue,
  304. .poll_int_queue = musb_poll_int_queue,
  305. .destroy_int_queue = musb_destroy_int_queue,
  306. .reset_root_port = musb_reset_root_port,
  307. };
  308. #endif /* CONFIG_DM_USB */
  309. #endif /* CONFIG_USB_MUSB_HOST */
  310. #ifdef CONFIG_USB_MUSB_GADGET
  311. static struct musb *gadget;
  312. int usb_gadget_handle_interrupts(int index)
  313. {
  314. WATCHDOG_RESET();
  315. if (!gadget || !gadget->isr)
  316. return -EINVAL;
  317. return gadget->isr(0, gadget);
  318. }
  319. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  320. {
  321. int ret;
  322. if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
  323. !driver->setup) {
  324. printf("bad parameter.\n");
  325. return -EINVAL;
  326. }
  327. if (!gadget) {
  328. printf("Controller uninitialized\n");
  329. return -ENXIO;
  330. }
  331. ret = musb_gadget_start(&gadget->g, driver);
  332. if (ret < 0) {
  333. printf("gadget_start failed with %d\n", ret);
  334. return ret;
  335. }
  336. ret = driver->bind(&gadget->g);
  337. if (ret < 0) {
  338. printf("bind failed with %d\n", ret);
  339. return ret;
  340. }
  341. return 0;
  342. }
  343. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  344. {
  345. if (driver->disconnect)
  346. driver->disconnect(&gadget->g);
  347. if (driver->unbind)
  348. driver->unbind(&gadget->g);
  349. return 0;
  350. }
  351. #endif /* CONFIG_USB_MUSB_GADGET */
  352. int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
  353. void *ctl_regs)
  354. {
  355. struct musb **musbp;
  356. switch (plat->mode) {
  357. #if defined(CONFIG_USB_MUSB_HOST) && !defined(CONFIG_DM_USB)
  358. case MUSB_HOST:
  359. musbp = &musb_host.host;
  360. break;
  361. #endif
  362. #ifdef CONFIG_USB_MUSB_GADGET
  363. case MUSB_PERIPHERAL:
  364. musbp = &gadget;
  365. break;
  366. #endif
  367. default:
  368. return -EINVAL;
  369. }
  370. *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
  371. if (!*musbp) {
  372. printf("Failed to init the controller\n");
  373. return -EIO;
  374. }
  375. return 0;
  376. }