hbm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/usb/hbm.h>
  15. #include <mach/usb_bam.h>
  16. /**
  17. * USB HBM Hardware registers.
  18. *
  19. */
  20. #define USB_OTG_HS_HBM_CFG (0x00000290)
  21. #define USB_OTG_HS_HBM_QH_MAP_PIPE(n) (0x00000294 + 4 * (n))
  22. #define USB_OTG_HS_HBM_PIPE_PRODUCER (0x00000314)
  23. #define USB_OTG_HS_HBM_PARK_MODE_DISABLE (0x00000318)
  24. #define USB_OTG_HS_HBM_PIPE_ZLT_DISABLE (0x0000031C)
  25. #define USB_OTG_HS_HBM_PIPE_EN (0x00000310)
  26. #define USB_OTG_HS_HBM_SW_RST (0x00000324)
  27. #define USB_OTG_HS_HBM_SB_SW_RST (0x00000320)
  28. #define USB_OTG_HS_USBCMD (0x00000140)
  29. #define USB_OTG_HS_USBSTS (0x00000144)
  30. /**
  31. * USB HBM Hardware registers bitmask.
  32. */
  33. #define HBM_EN 0x00000001
  34. #define ASE 0x20
  35. #define AS 0x8000
  36. #define PIPE_PRODUCER 1
  37. #define MAX_PIPE_NUM 16
  38. #define HBM_QH_MAP_PIPE 0xffffffc0
  39. #define QTD_CERR_MASK 0xfffff3ff
  40. struct hbm_msm {
  41. u32 *base;
  42. struct usb_hcd *hcd;
  43. bool disable_park_mode;
  44. };
  45. static struct hbm_msm *hbm_ctx;
  46. /**
  47. * Read register masked field.
  48. *
  49. * @base - hbm base virtual address.
  50. * @offset - register offset.
  51. * @mask - register bitmask.
  52. *
  53. * @return u32
  54. */
  55. static inline u32 hbm_msm_read_reg_field(void *base,
  56. u32 offset, const u32 mask)
  57. {
  58. u32 shift = find_first_bit((void *)&mask, 32);
  59. u32 val = ioread32(base + offset);
  60. val &= mask; /* clear other bits */
  61. val >>= shift;
  62. return val;
  63. }
  64. /**
  65. * Write register field.
  66. *
  67. * @base - hbm base virtual address.
  68. * @offset - register offset.
  69. * @val - value to be written.
  70. *
  71. */
  72. static inline void hbm_msm_write_reg(void *base, u32 offset, u32 val)
  73. {
  74. iowrite32(val, base + offset);
  75. }
  76. /**
  77. * Write register masked field.
  78. *
  79. * @base - hbm base virtual address.
  80. * @offset - register offset.
  81. * @mask - register bitmask.
  82. * @val - value to write.
  83. *
  84. */
  85. static inline void hbm_msm_write_reg_field(void *base, u32 offset,
  86. const u32 mask, u32 val)
  87. {
  88. u32 shift = find_first_bit((void *)&mask, 32);
  89. u32 tmp = ioread32(base + offset);
  90. tmp &= ~mask;
  91. val = tmp | (val << shift);
  92. iowrite32(val, base + offset);
  93. }
  94. /**
  95. * Enable/disable park mode. Park mode enables executing up to 3 usb packets
  96. * from each QH.
  97. *
  98. * @pipe_num - Connection index.
  99. *
  100. * @disable_park_mode - Enable/disable park mode.
  101. *
  102. */
  103. int set_disable_park_mode(u8 pipe_num, bool disable_park_mode)
  104. {
  105. if (pipe_num >= MAX_PIPE_NUM) {
  106. pr_err("%s: illegal pipe num %d", __func__, pipe_num);
  107. return -EINVAL;
  108. }
  109. /* enable/disable park mode */
  110. hbm_msm_write_reg_field(hbm_ctx->base,
  111. USB_OTG_HS_HBM_PARK_MODE_DISABLE, 1 << pipe_num,
  112. (disable_park_mode ? 1 : 0));
  113. return 0;
  114. }
  115. /**
  116. * Enable/disable zero length transfer.
  117. *
  118. * @pipe_num - Connection index.
  119. *
  120. * @disable_zlt - Enable/disable zlt.
  121. *
  122. */
  123. int set_disable_zlt(u8 pipe_num, bool disable_zlt)
  124. {
  125. if (pipe_num >= MAX_PIPE_NUM) {
  126. pr_err("%s: illegal pipe num %d", __func__, pipe_num);
  127. return -EINVAL;
  128. }
  129. /* enable/disable zlt */
  130. hbm_msm_write_reg_field(hbm_ctx->base,
  131. USB_OTG_HS_HBM_PIPE_ZLT_DISABLE, 1 << pipe_num,
  132. (disable_zlt ? 1 : 0));
  133. return 0;
  134. }
  135. static void hbm_reset(bool reset)
  136. {
  137. hbm_msm_write_reg_field(hbm_ctx->base, USB_OTG_HS_HBM_SW_RST, 1 << 0,
  138. reset ? 1 : 0);
  139. }
  140. static void hbm_config(bool enable)
  141. {
  142. hbm_msm_write_reg_field(hbm_ctx->base, USB_OTG_HS_HBM_CFG, HBM_EN,
  143. enable ? 1 : 0);
  144. }
  145. int hbm_pipe_init(u32 QH_addr, u32 pipe_num, bool is_consumer)
  146. {
  147. if (pipe_num >= MAX_PIPE_NUM) {
  148. pr_err("%s: illegal pipe num %d", __func__, pipe_num);
  149. return -EINVAL;
  150. }
  151. /* map QH(ep) <> pipe */
  152. hbm_msm_write_reg(hbm_ctx->base,
  153. USB_OTG_HS_HBM_QH_MAP_PIPE(pipe_num), QH_addr);
  154. /* set pipe producer/consumer mode - (IN EP is producer) */
  155. hbm_msm_write_reg_field(hbm_ctx->base,
  156. USB_OTG_HS_HBM_PIPE_PRODUCER, 1 << pipe_num,
  157. (is_consumer ? 0 : 1));
  158. /* set park mode */
  159. set_disable_park_mode(pipe_num, hbm_ctx->disable_park_mode);
  160. /* enable zlt as default*/
  161. set_disable_zlt(pipe_num, false);
  162. /* activate pipe */
  163. hbm_msm_write_reg_field(hbm_ctx->base, USB_OTG_HS_HBM_PIPE_EN,
  164. 1 << pipe_num, 1);
  165. return 0;
  166. }
  167. void hbm_init(struct usb_hcd *hcd, bool disable_park_mode)
  168. {
  169. pr_info("%s\n", __func__);
  170. hbm_ctx = kzalloc(sizeof(*hbm_ctx), GFP_KERNEL);
  171. if (!hbm_ctx) {
  172. pr_err("%s: hbm_ctx alloc failed\n", __func__);
  173. return;
  174. }
  175. hbm_ctx->base = hcd->regs;
  176. hbm_ctx->hcd = hcd;
  177. hbm_ctx->disable_park_mode = disable_park_mode;
  178. /* reset hbm */
  179. hbm_reset(true);
  180. /* delay was added to allow the reset process the end */
  181. udelay(1000);
  182. hbm_reset(false);
  183. hbm_config(true);
  184. }
  185. void hbm_uninit(void)
  186. {
  187. hbm_config(false);
  188. kfree(hbm_ctx);
  189. }
  190. static int hbm_submit_async(struct ehci_hcd *ehci, struct urb *urb,
  191. struct list_head *qtd_list, gfp_t mem_flags)
  192. {
  193. int epnum;
  194. unsigned long flags;
  195. struct ehci_qh *qh = NULL;
  196. int rc;
  197. struct usb_host_bam_type *bam =
  198. (struct usb_host_bam_type *)urb->priv_data;
  199. epnum = urb->ep->desc.bEndpointAddress;
  200. spin_lock_irqsave(&ehci->lock, flags);
  201. if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
  202. rc = -ESHUTDOWN;
  203. goto done;
  204. }
  205. rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  206. if (unlikely(rc))
  207. goto done;
  208. qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
  209. if (unlikely(qh == NULL)) {
  210. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  211. rc = -ENOMEM;
  212. goto done;
  213. }
  214. hbm_pipe_init(qh->qh_dma, bam->pipe_num, bam->dir);
  215. if (likely(qh->qh_state == QH_STATE_IDLE))
  216. qh_link_async(ehci, qh);
  217. done:
  218. spin_unlock_irqrestore(&ehci->lock, flags);
  219. if (unlikely(qh == NULL))
  220. qtd_list_free(ehci, urb, qtd_list);
  221. return rc;
  222. }
  223. int hbm_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  224. gfp_t mem_flags)
  225. {
  226. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  227. struct list_head qtd_list;
  228. struct ehci_qtd *qtd;
  229. INIT_LIST_HEAD(&qtd_list);
  230. if (usb_pipetype(urb->pipe) != PIPE_BULK) {
  231. pr_err("%s pipe type is not BULK\n", __func__);
  232. return -EINVAL;
  233. }
  234. /*no sg support*/
  235. urb->transfer_buffer_length = 0;
  236. urb->transfer_dma = 0;
  237. urb->transfer_flags |= URB_NO_INTERRUPT;
  238. if (!qh_urb_transaction(ehci, urb, &qtd_list, mem_flags))
  239. return -ENOMEM;
  240. /* set err counter in qTD token to zero */
  241. qtd = list_entry(qtd_list.next, struct ehci_qtd, qtd_list);
  242. if (qtd != NULL)
  243. qtd->hw_token &= QTD_CERR_MASK;
  244. return hbm_submit_async(ehci, urb, &qtd_list, mem_flags);
  245. }