musb_uboot.c 13 KB

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