mt_ssusb_qmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2013 MediaTek Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <sys/types.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <debug.h>
  27. #include <reg.h>
  28. #include <platform/bitops.h>
  29. #include <platform/mt_irq.h>
  30. #include <platform/mt_reg_base.h>
  31. #include <platform/mt_typedefs.h>
  32. #include <kernel/thread.h>
  33. #include <dev/udc.h>
  34. #include <mt_ssusb.h>
  35. #include <mt_ssusb_qmu.h>
  36. #include <mt_mu3d_hal_qmu_drv.h>
  37. //#define MTK_QMU_DRV_EXT /* only need to define this when QMU vars are extern */
  38. /* USB DEBUG */
  39. #ifdef DBG_USB_QMU
  40. #define DBG_C(x...) dprintf(CRITICAL, "[USB][QMU] " x)
  41. #define DBG_I(x...) dprintf(INFO, "[USB][QMU] " x)
  42. #define DBG_S(x...) dprintf(SPEW, "[USB][QMU] " x)
  43. #else
  44. #define DBG_C(x...) do {} while (0)
  45. #define DBG_I(x...) do {} while (0)
  46. #define DBG_S(x...) do {} while (0)
  47. #endif
  48. /* TODO: replace these 2 static to extern */
  49. extern struct udc_device *the_device;
  50. extern struct udc_gadget *the_gadget;
  51. u8 is_bdp;
  52. #ifdef SUPPORT_QMU
  53. /*
  54. * 1. Configure the HW register (starting address) with gpd heads (starting address) for each Queue
  55. * 2. Enable Queue with each EP and interrupts
  56. */
  57. void usb_initialize_qmu()
  58. {
  59. mu3d_hal_init_qmu();
  60. is_bdp = 0; /* We need to use BD because for mass storage, the largest data > GPD limitation */
  61. //gpd_buf_size = GPD_BUF_SIZE; //max allowable data buffer length. Don't care when linking with BD.
  62. gpd_buf_size = GPD_BUF_SIZE_ALIGN; //max allowable data buffer length. Don't care when linking with BD.
  63. bd_buf_size = BD_BUF_SIZE;
  64. bd_extension = 0;
  65. gpd_extension = 0;
  66. g_dma_buffer_size = STRESS_DATA_LENGTH * MAX_GPD_NUM;
  67. }
  68. /*
  69. * qmu_handler - handle qmu error events
  70. * @args - arg1: ep number
  71. */
  72. void qmu_handler(u32 qmu_val)
  73. {
  74. int i, err;
  75. i = (int)qmu_val;
  76. DBG_I("%s: qmu_val: %x\n", __func__, qmu_val);
  77. if (qmu_val & RXQ_CSERR_INT) {
  78. DBG_I("Rx %d checksum error!\n", i);
  79. }
  80. if (qmu_val & RXQ_LENERR_INT) {
  81. DBG_I("Rx %d length error!\n", i);
  82. //g_rx_len_err_cnt++;
  83. }
  84. if (qmu_val & TXQ_CSERR_INT) {
  85. DBG_I("Tx %d checksum error!\n", i);
  86. }
  87. if (qmu_val & TXQ_LENERR_INT) {
  88. DBG_I("Tx %d length error!\n", i);
  89. }
  90. if ((qmu_val & RXQ_CSERR_INT) || (qmu_val & RXQ_LENERR_INT)) {
  91. err = readl(U3D_RQERRIR0);
  92. DBG_I("U3D_RQERRIR0 : [0x%x]\n", err);
  93. for (i = 1; i <= MAX_QMU_EP; i++) {
  94. if (err & QMU_RX_CS_ERR(i)) {
  95. DBG_I("Rx %d length error!\n", i);
  96. }
  97. if (err &QMU_RX_LEN_ERR(i)) {
  98. DBG_I("Rx %d recieve length error!\n", i);
  99. }
  100. }
  101. writel(err, U3D_RQERRIR0);
  102. }
  103. if (qmu_val & RXQ_ZLPERR_INT) {
  104. err = readl(U3D_RQERRIR1);
  105. DBG_I("U3D_RQERRIR1: [0x%x]\n", err);
  106. for (i = 1; i <= MAX_QMU_EP; i++) {
  107. if (err & QMU_RX_ZLP_ERR(i)) {
  108. DBG_I("Rx %d recieve an zlp packet!\n", i);
  109. }
  110. }
  111. writel(err, U3D_RQERRIR1);
  112. }
  113. if ((qmu_val & TXQ_CSERR_INT) || (qmu_val & TXQ_LENERR_INT)) {
  114. err = readl(U3D_TQERRIR0);
  115. DBG_I("Tx Queue error in QMU mode![0x%x]\n", err);
  116. for (i = 1; i <= MAX_QMU_EP; i++) {
  117. if (err & QMU_TX_CS_ERR(i)) {
  118. DBG_I("Tx %d checksum error!\n", i);
  119. }
  120. if (err & QMU_TX_LEN_ERR(i)) {
  121. DBG_I("Tx %d buffer length error!\n", i);
  122. }
  123. }
  124. writel(err, U3D_TQERRIR0);
  125. }
  126. if ((qmu_val & RXQ_EMPTY_INT) || (qmu_val & TXQ_EMPTY_INT)) {
  127. u32 empty = readl(U3D_QEMIR);
  128. DBG_I("Rx/Tx Empty in QMU mode![0x%x]\n", empty);
  129. writel(empty, U3D_QEMIR);
  130. }
  131. }
  132. /*
  133. * put usb_request buffer into GPD/BD data structures, and ask QMU to start TX.
  134. *
  135. * caller: musb_g_tx
  136. */
  137. void txstate_qmu(struct urb *req)
  138. {
  139. u8 ep_num;
  140. struct udc_endpoint *ept;
  141. //struct usb_request *request;
  142. struct urb *current_urb;
  143. u32 txcsr0 = 0;
  144. ep_num = req->ept->num;
  145. //ept = &musb->endpoints[ep_num].ep_in;
  146. //struct udc_endpoint *endpoint = &mt_usb_ep[TX_ENDPOINT];
  147. //TODO: fix
  148. //ept = &musb->p_ep_array[ep_num];
  149. ept = mt_find_ep(ep_num, USB_DIR_IN);
  150. /* do not use "urb->ept->tx_pktsz ??*/
  151. //request = &req->request;
  152. // Note: request -> current_urb;
  153. current_urb = ept->tx_urb;
  154. //TODO: fix
  155. //txcsr0 = readl(musb->endpoints[ep_num].addr_txcsr0);
  156. //txcsr0 = readl(musb->p_ep_array[ep_num].addr_txcsr0);
  157. txcsr0 = readl(U3D_TX1CSR0);
  158. if (txcsr0 & TX_SENDSTALL) {
  159. DBG_I("%s: ep %d stalling, txcsr %03x\n",
  160. __func__, ept->num, txcsr0);
  161. return;
  162. }
  163. // request->actual = request->length;
  164. // mu3d_hal_insert_transfer_gpd(ep_num, USB_DIR_IN, (u8 *)request->dma, request->length, true,true,false,(ept->type==USB_ENDPOINT_XFER_ISOC?0:1), ept->packet_sz);
  165. mu3d_hal_insert_transfer_gpd(ep_num, USB_DIR_IN, (u8 *)current_urb->buf, current_urb->actual_length, true, true, false, (ept->type == USB_EP_XFER_ISO ? 0 : 1), ept->maxpkt);
  166. mu3d_hal_resume_qmu(ep_num, USB_DIR_IN);
  167. DBG_I("tx start...length : %d\n", current_urb->actual_length);
  168. }
  169. /* the caller ensures that req is not NULL. */
  170. void rxstate_qmu(struct urb *req)
  171. {
  172. u8 ep_num;
  173. struct udc_endpoint *ept;
  174. //struct usb_request *request;
  175. struct urb *current_urb;
  176. u32 rxcsr0 = 0;
  177. //ep_num = GET_EP_NUM(req->p_ep->endpoint_address);
  178. ep_num = req->ept->num;
  179. //TODO: fix
  180. //struct udc_endpoint *ept = &musb->endpoints[epnum].ep_out;
  181. //ept = &musb->p_ep_array[ep_num];
  182. ept = mt_find_ep(ep_num, USB_DIR_OUT);
  183. //TODO: fix
  184. //rxcsr0 = readl(musb->endpoints[ep_num].addr_rxcsr0);
  185. //rxcsr0 = readl(musb->p_ep_array[ep_num]);
  186. rxcsr0 = readl(U3D_RX1CSR0);
  187. //request = &req->request;
  188. current_urb = ept->rcv_urb;
  189. if (rxcsr0 & RX_SENDSTALL) {
  190. DBG_I("%s: ep %d stalling, RXCSR %04x\n",
  191. __func__, ept->num, rxcsr0);
  192. return;
  193. }
  194. DBG_I("rxstate_qmu 0\n");
  195. //mu3d_hal_insert_transfer_gpd(ep_num, USB_DIR_OUT, (u8*)request->dma, request->length, true, true, false, (ept->type==USB_ENDPOINT_XFER_ISOC?0:1), ept->maxpkt);
  196. mu3d_hal_insert_transfer_gpd(ep_num, USB_DIR_OUT, (u8 *)current_urb->buf, current_urb->actual_length, true, true, false, (ept->type == USB_EP_XFER_ISO ? 0 : 1), ept->maxpkt);
  197. mu3d_hal_resume_qmu(ep_num, USB_DIR_OUT);
  198. DBG_I("rx start\n");
  199. }
  200. /*
  201. * 1. Find the last gpd HW has executed and update tx_gpd_last[]
  202. * 2. Set the flag for txstate to know that TX has been completed
  203. * ported from proc_qmu_tx() from test driver.
  204. * caller:qmu_interrupt after getting QMU done interrupt and TX is raised
  205. */
  206. void qmu_tx_interrupt(u8 ep_num)
  207. {
  208. struct tgpd *gpd = tx_gpd_last[ep_num];
  209. struct tgpd *gpd_current = (struct tgpd*)(readl(U3D_TXQCPR(ep_num)));
  210. struct udc_endpoint *ept;
  211. ept = mt_find_ep(ep_num, USB_DIR_IN);
  212. #if defined(SUPPORT_VA)
  213. gpd_current = gpd_phys_to_virt(gpd_current, USB_DIR_IN, ep_num);
  214. #endif
  215. DBG_I("tx_gpd_last 0x%x, gpd_current 0x%x, gpd_end 0x%x, \n", (u32)gpd, (u32)gpd_current, (u32)tx_gpd_end[ep_num]);
  216. if (gpd == gpd_current) {//gpd_current should at least point to the next GPD to the previous last one.
  217. DBG_I("should not happen: %s %d\n", __func__, __LINE__);
  218. return;
  219. }
  220. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  221. while (gpd != gpd_current && !TGPD_IS_FLAGS_HWO(gpd)) {
  222. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  223. DBG_I("Tx gpd %x info { HWO %d, BPD %d, Next_GPD %x , DataBuffer %x, BufferLen %d, Endpoint %d}\n",
  224. (u32)gpd, (u32)TGPD_GET_FLAG(gpd), (u32)TGPD_GET_FORMAT(gpd), (u32)TGPD_GET_NEXT(gpd),
  225. (u32)TGPD_GET_DATA(gpd), (u32)TGPD_GET_BUF_LEN(gpd), (u32)TGPD_GET_EPaddr(gpd));
  226. /* required for mt_udc_epx_handler */
  227. ept->sent = (u32)TGPD_GET_BUF_LEN(gpd);
  228. gpd = TGPD_GET_NEXT(gpd);
  229. #if defined(SUPPORT_VA)
  230. gpd = gpd_phys_to_virt(gpd, USB_DIR_IN, ep_num);
  231. #endif
  232. tx_gpd_last[ep_num] = gpd;
  233. }
  234. DBG_I("tx_gpd_last[%d]: 0x%x\n", ep_num, (u32)tx_gpd_last[ep_num]);
  235. DBG_I("tx_gpd_end[%d]: 0x%x\n", ep_num, (u32)tx_gpd_end[ep_num]);
  236. DBG_I("Tx %d complete\n", ep_num);
  237. return;
  238. }
  239. /*
  240. * When receiving RXQ done interrupt, qmu_interrupt calls this function.
  241. *
  242. * 1. Traverse GPD/BD data structures to count actual transferred length.
  243. * 2. Set the done flag to notify rxstate_qmu() to report status to upper gadget driver.
  244. *
  245. * ported from proc_qmu_rx() from test driver.
  246. * caller:qmu_interrupt after getting QMU done interrupt and TX is raised
  247. */
  248. void qmu_rx_interrupt(u8 ep_num)
  249. {
  250. #ifdef DBG_USB_QMU
  251. u32 bufferlength;
  252. #endif
  253. u32 recivedlength = 0;
  254. struct tgpd *gpd = (struct tgpd*) rx_gpd_last[ep_num];
  255. struct tgpd *gpd_current = (struct tgpd*)(readl(U3D_RXQCPR(ep_num)));
  256. struct tbd *bd;
  257. struct udc_endpoint *ept;
  258. struct urb *current_urb;
  259. ept = mt_find_ep(ep_num, USB_DIR_OUT);
  260. current_urb = ept->rcv_urb;
  261. #if defined(SUPPORT_VA)
  262. gpd_current = gpd_phys_to_virt(gpd_current, USB_DIR_OUT, ep_num);
  263. #endif
  264. DBG_I("ep_num: %d ,Rx_gpd_last: 0x%x, gpd_current: 0x%x, gpd_end: 0x%x \n",ep_num,(u32)gpd, (u32)gpd_current, (u32)rx_gpd_end[ep_num]);
  265. if (gpd == gpd_current) {
  266. DBG_I("should not happen: %s %d\n", __func__, __LINE__);
  267. return;
  268. }
  269. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  270. while (gpd != gpd_current && !TGPD_IS_FLAGS_HWO(gpd)) {
  271. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  272. if (TGPD_IS_FORMAT_BDP(gpd)) {
  273. bd = (struct tbd *)TGPD_GET_DATA(gpd);
  274. #if defined(SUPPORT_VA)
  275. bd = (struct tbd *)bd_phys_to_virt(bd,USB_DIR_OUT, ep_num);
  276. #endif
  277. while (1) {
  278. DBG_I("BD: 0x%x\n", (u32)bd);
  279. DBG_I("Buf Len: 0x%x\n", (u32)TBD_GET_BUF_LEN(bd));
  280. //req->transferCount += TBD_GET_BUF_LEN(bd);
  281. //ept->qmu_done_length += TBD_GET_BUF_LEN(bd);
  282. current_urb->actual_length += TBD_GET_BUF_LEN(bd);
  283. //DBG_I("Total Len : 0x%x\n",ept->qmu_done_length);
  284. if (TBD_IS_FLAGS_EOL(bd)) {
  285. break;
  286. }
  287. bd = TBD_GET_NEXT(bd);
  288. #if defined(SUPPORT_VA)
  289. bd = bd_phys_to_virt(bd, USB_DIR_OUT, ep_num);
  290. #endif
  291. }
  292. } else {
  293. recivedlength = (u32)TGPD_GET_BUF_LEN(gpd);
  294. #ifdef DBG_USB_QMU
  295. bufferlength = (u32)TGPD_GET_DATABUF_LEN(gpd);
  296. #endif
  297. /* required for mt_udc_epx_handler */
  298. current_urb->actual_length += recivedlength;
  299. #ifdef DBG_USB_QMU
  300. DBG_I("%s: recivedlength: %d, bufferlength: %d, current_urb->actual_length: %d\n", __func__, recivedlength, bufferlength, current_urb->actual_length);
  301. #endif
  302. }
  303. DBG_I("Rx gpd info { HWO %d, Next_GPD %x ,DataBufferLength %d, DataBuffer %x, Recived Len %d, Endpoint %d, TGL %d, ZLP %d}\n",
  304. (u32)TGPD_GET_FLAG(gpd), (u32)TGPD_GET_NEXT(gpd),
  305. (u32)TGPD_GET_DATABUF_LEN(gpd), (u32)TGPD_GET_DATA(gpd),
  306. (u32)TGPD_GET_BUF_LEN(gpd), (u32)TGPD_GET_EPaddr(gpd),
  307. (u32)TGPD_GET_TGL(gpd), (u32)TGPD_GET_ZLP(gpd));
  308. gpd = TGPD_GET_NEXT(gpd);
  309. #if defined(SUPPORT_VA)
  310. gpd = gpd_phys_to_virt(gpd,USB_DIR_OUT, ep_num);
  311. #endif
  312. rx_gpd_last[ep_num] = gpd;
  313. }
  314. DBG_I("rx_gpd_last[%d]: 0x%x\n", ep_num, (u32)rx_gpd_last[ep_num]);
  315. DBG_I("rx_gpd_end[%d]: 0x%x\n", ep_num, (u32)rx_gpd_end[ep_num]);
  316. }
  317. void qmu_done_interrupt(u32 qmu_val)
  318. {
  319. int i;
  320. DBG_I("[USB][QMU] qmu_interrupt\n");
  321. for (i = 1; i <= MAX_QMU_EP; i++) {
  322. if (qmu_val & QMU_RX_DONE(i)) {
  323. qmu_rx_interrupt(i);
  324. }
  325. if (qmu_val & QMU_TX_DONE(i)) {
  326. qmu_tx_interrupt(i);
  327. }
  328. }
  329. }
  330. void qmu_exception_interrupt(void *base, u32 qmu_val)
  331. {
  332. u32 err;
  333. int i = (int) qmu_val;
  334. if (qmu_val & RXQ_CSERR_INT) {
  335. DBG_I("Rx %d checksum error!\n", i);
  336. }
  337. if (qmu_val & RXQ_LENERR_INT) {
  338. DBG_I("Rx %d length error!\n", i);
  339. }
  340. if (qmu_val & TXQ_CSERR_INT) {
  341. DBG_I("Tx %d checksum error!\n", i);
  342. }
  343. if (qmu_val & TXQ_LENERR_INT) {
  344. DBG_I("Tx %d length error!\n", i);
  345. }
  346. if ((qmu_val & RXQ_CSERR_INT) || (qmu_val & RXQ_LENERR_INT)) {
  347. err = readl(U3D_RQERRIR0);
  348. DBG_I("Rx Queue error in QMU mode![0x%x]\n", (unsigned int)err);
  349. for (i = 1; i <= MAX_QMU_EP; i++) {
  350. if (err & QMU_RX_CS_ERR(i)) {
  351. DBG_I("Rx %d length error!\n", i);
  352. }
  353. if (err & QMU_RX_LEN_ERR(i)) {
  354. DBG_I("Rx %d recieve length error!\n", i);
  355. }
  356. }
  357. writel(err, U3D_RQERRIR0);
  358. }
  359. if (qmu_val & RXQ_ZLPERR_INT) {
  360. err = readl(U3D_RQERRIR1);
  361. DBG_I("Rx Queue error in QMU mode![0x%x]\n", (unsigned int)err);
  362. for (i = 1; i <= MAX_QMU_EP; i++) {
  363. if (err &QMU_RX_ZLP_ERR(i)) {
  364. DBG_I("Rx %d recieve an zlp packet!\n", i);
  365. }
  366. }
  367. writel(err, U3D_RQERRIR1);
  368. }
  369. if ((qmu_val & TXQ_CSERR_INT) || (qmu_val & TXQ_LENERR_INT)) {
  370. err = readl(U3D_TQERRIR0);
  371. DBG_I("Tx Queue error in QMU mode![0x%x]\n", (unsigned int)err);
  372. for (i = 1; i <= MAX_QMU_EP; i++) {
  373. if (err & QMU_TX_CS_ERR(i)) {
  374. DBG_I("Tx %d checksum error!\n", i);
  375. }
  376. if (err & QMU_TX_LEN_ERR(i)) {
  377. DBG_I("Tx %d buffer length error!\n", i);
  378. }
  379. }
  380. writel(err, U3D_TQERRIR0);
  381. }
  382. if ((qmu_val & RXQ_EMPTY_INT) || (qmu_val & TXQ_EMPTY_INT)) {
  383. u32 empty = readl(U3D_QEMIR);
  384. DBG_I("Rx Empty in QMU mode![0x%x]\n", empty);
  385. writel(empty, U3D_QEMIR);
  386. mu3d_hal_flush_qmu(1, USB_DIR_OUT);
  387. mu3d_hal_restart_qmu(1, USB_DIR_OUT);
  388. }
  389. }
  390. #endif /* ifdef SUPPORT_QMU */