musb_uboot.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include <common.h>
  2. #include <watchdog.h>
  3. #ifdef CONFIG_ARCH_SUNXI
  4. #include <asm/arch/usb_phy.h>
  5. #endif
  6. #include <asm/errno.h>
  7. #include <linux/usb/ch9.h>
  8. #include <linux/usb/gadget.h>
  9. #include <usb.h>
  10. #include "linux-compat.h"
  11. #include "usb-compat.h"
  12. #include "musb_core.h"
  13. #include "musb_host.h"
  14. #include "musb_gadget.h"
  15. #ifdef CONFIG_MUSB_HOST
  16. struct int_queue {
  17. struct usb_host_endpoint hep;
  18. struct urb urb;
  19. };
  20. static struct musb *host;
  21. static struct usb_hcd hcd;
  22. static enum usb_device_speed host_speed;
  23. static void musb_host_complete_urb(struct urb *urb)
  24. {
  25. urb->dev->status &= ~USB_ST_NOT_PROC;
  26. urb->dev->act_len = urb->actual_length;
  27. }
  28. static struct usb_host_endpoint hep;
  29. static struct urb urb;
  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. int submit_control_msg(struct usb_device *dev, unsigned long pipe,
  80. void *buffer, int len, struct devrequest *setup)
  81. {
  82. construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_CONTROL, pipe,
  83. buffer, len, setup, 0);
  84. /* Fix speed for non hub-attached devices */
  85. if (!dev->parent)
  86. dev->speed = host_speed;
  87. return submit_urb(&hcd, &urb);
  88. }
  89. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  90. void *buffer, int len)
  91. {
  92. construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_BULK, pipe,
  93. buffer, len, NULL, 0);
  94. return submit_urb(&hcd, &urb);
  95. }
  96. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  97. void *buffer, int len, int interval)
  98. {
  99. construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_INT, pipe,
  100. buffer, len, NULL, interval);
  101. return submit_urb(&hcd, &urb);
  102. }
  103. struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
  104. int queuesize, int elementsize, void *buffer, int interval)
  105. {
  106. struct int_queue *queue;
  107. int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
  108. if (queuesize != 1) {
  109. printf("ERROR musb int-queues only support queuesize 1\n");
  110. return NULL;
  111. }
  112. if (dev->int_pending & (1 << index)) {
  113. printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
  114. return NULL;
  115. }
  116. queue = malloc(sizeof(*queue));
  117. if (!queue)
  118. return NULL;
  119. construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
  120. pipe, buffer, elementsize, NULL, interval);
  121. ret = musb_urb_enqueue(&hcd, &queue->urb, 0);
  122. if (ret < 0) {
  123. printf("Failed to enqueue URB to controller\n");
  124. free(queue);
  125. return NULL;
  126. }
  127. dev->int_pending |= 1 << index;
  128. return queue;
  129. }
  130. int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
  131. {
  132. int index = usb_pipein(queue->urb.pipe) * 16 +
  133. usb_pipeendpoint(queue->urb.pipe);
  134. if (queue->urb.status == -EINPROGRESS)
  135. musb_urb_dequeue(&hcd, &queue->urb, -ETIME);
  136. dev->int_pending &= ~(1 << index);
  137. free(queue);
  138. return 0;
  139. }
  140. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
  141. {
  142. if (queue->urb.status != -EINPROGRESS)
  143. return NULL; /* URB has already completed in a prev. poll */
  144. host->isr(0, host);
  145. if (queue->urb.status != -EINPROGRESS)
  146. return queue->urb.transfer_buffer; /* Done */
  147. return NULL; /* URB still pending */
  148. }
  149. int usb_reset_root_port(struct usb_device *dev)
  150. {
  151. void *mbase = host->mregs;
  152. u8 power;
  153. power = musb_readb(mbase, MUSB_POWER);
  154. power &= 0xf0;
  155. musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
  156. mdelay(50);
  157. #ifdef CONFIG_ARCH_SUNXI
  158. /*
  159. * sunxi phy has a bug and it will wrongly detect high speed squelch
  160. * when clearing reset on low-speed devices, temporary disable
  161. * squelch detection to work around this.
  162. */
  163. sunxi_usb_phy_enable_squelch_detect(0, 0);
  164. #endif
  165. power = musb_readb(mbase, MUSB_POWER);
  166. musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
  167. #ifdef CONFIG_ARCH_SUNXI
  168. sunxi_usb_phy_enable_squelch_detect(0, 1);
  169. #endif
  170. host->isr(0, host);
  171. 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_speed == USB_SPEED_LOW) ? 200 : 50);
  176. return 0;
  177. }
  178. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  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) {
  185. printf("MUSB host is not registered\n");
  186. return -ENODEV;
  187. }
  188. ret = musb_start(host);
  189. if (ret)
  190. return ret;
  191. mbase = 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. return -ENODEV;
  198. usb_reset_root_port(NULL);
  199. host->is_active = 1;
  200. hcd.hcd_priv = host;
  201. return 0;
  202. }
  203. int usb_lowlevel_stop(int index)
  204. {
  205. if (!host) {
  206. printf("MUSB host is not registered\n");
  207. return -ENODEV;
  208. }
  209. musb_stop(host);
  210. return 0;
  211. }
  212. #endif /* CONFIG_MUSB_HOST */
  213. #ifdef CONFIG_MUSB_GADGET
  214. static struct musb *gadget;
  215. int usb_gadget_handle_interrupts(int index)
  216. {
  217. WATCHDOG_RESET();
  218. if (!gadget || !gadget->isr)
  219. return -EINVAL;
  220. return gadget->isr(0, gadget);
  221. }
  222. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  223. {
  224. int ret;
  225. if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
  226. !driver->setup) {
  227. printf("bad parameter.\n");
  228. return -EINVAL;
  229. }
  230. if (!gadget) {
  231. printf("Controller uninitialized\n");
  232. return -ENXIO;
  233. }
  234. ret = musb_gadget_start(&gadget->g, driver);
  235. if (ret < 0) {
  236. printf("gadget_start failed with %d\n", ret);
  237. return ret;
  238. }
  239. ret = driver->bind(&gadget->g);
  240. if (ret < 0) {
  241. printf("bind failed with %d\n", ret);
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  247. {
  248. if (driver->disconnect)
  249. driver->disconnect(&gadget->g);
  250. if (driver->unbind)
  251. driver->unbind(&gadget->g);
  252. return 0;
  253. }
  254. #endif /* CONFIG_MUSB_GADGET */
  255. int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
  256. void *ctl_regs)
  257. {
  258. struct musb **musbp;
  259. switch (plat->mode) {
  260. #ifdef CONFIG_MUSB_HOST
  261. case MUSB_HOST:
  262. musbp = &host;
  263. break;
  264. #endif
  265. #ifdef CONFIG_MUSB_GADGET
  266. case MUSB_PERIPHERAL:
  267. musbp = &gadget;
  268. break;
  269. #endif
  270. default:
  271. return -EINVAL;
  272. }
  273. *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
  274. if (!musbp) {
  275. printf("Failed to init the controller\n");
  276. return -EIO;
  277. }
  278. return 0;
  279. }