mt_ssusb_qmu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. if (ept == NULL) {
  151. DBG_I("%s: ept is NULL.\n", __func__);
  152. return;
  153. }
  154. /* do not use "urb->ept->tx_pktsz ??*/
  155. //request = &req->request;
  156. // Note: request -> current_urb;
  157. current_urb = ept->tx_urb;
  158. //TODO: fix
  159. //txcsr0 = readl(musb->endpoints[ep_num].addr_txcsr0);
  160. //txcsr0 = readl(musb->p_ep_array[ep_num].addr_txcsr0);
  161. txcsr0 = readl(U3D_TX1CSR0);
  162. if (txcsr0 & TX_SENDSTALL) {
  163. DBG_I("%s: ep %d stalling, txcsr %03x\n",
  164. __func__, ept->num, txcsr0);
  165. return;
  166. }
  167. // request->actual = request->length;
  168. // 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);
  169. 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);
  170. mu3d_hal_resume_qmu(ep_num, USB_DIR_IN);
  171. DBG_I("tx start...length : %d\n", current_urb->actual_length);
  172. }
  173. /* the caller ensures that req is not NULL. */
  174. void rxstate_qmu(struct urb *req)
  175. {
  176. u8 ep_num;
  177. struct udc_endpoint *ept;
  178. //struct usb_request *request;
  179. struct urb *current_urb;
  180. u32 rxcsr0 = 0;
  181. //ep_num = GET_EP_NUM(req->p_ep->endpoint_address);
  182. ep_num = req->ept->num;
  183. //TODO: fix
  184. //struct udc_endpoint *ept = &musb->endpoints[epnum].ep_out;
  185. //ept = &musb->p_ep_array[ep_num];
  186. ept = mt_find_ep(ep_num, USB_DIR_OUT);
  187. //TODO: fix
  188. //rxcsr0 = readl(musb->endpoints[ep_num].addr_rxcsr0);
  189. //rxcsr0 = readl(musb->p_ep_array[ep_num]);
  190. rxcsr0 = readl(U3D_RX1CSR0);
  191. //request = &req->request;
  192. current_urb = ept->rcv_urb;
  193. if (rxcsr0 & RX_SENDSTALL) {
  194. DBG_I("%s: ep %d stalling, RXCSR %04x\n",
  195. __func__, ept->num, rxcsr0);
  196. return;
  197. }
  198. DBG_I("rxstate_qmu 0\n");
  199. //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);
  200. 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);
  201. mu3d_hal_resume_qmu(ep_num, USB_DIR_OUT);
  202. DBG_I("rx start\n");
  203. }
  204. /*
  205. * 1. Find the last gpd HW has executed and update tx_gpd_last[]
  206. * 2. Set the flag for txstate to know that TX has been completed
  207. * ported from proc_qmu_tx() from test driver.
  208. * caller:qmu_interrupt after getting QMU done interrupt and TX is raised
  209. */
  210. void qmu_tx_interrupt(u8 ep_num)
  211. {
  212. struct tgpd *gpd = tx_gpd_last[ep_num];
  213. struct tgpd *gpd_current = (struct tgpd*)(readl(U3D_TXQCPR(ep_num)));
  214. struct udc_endpoint *ept;
  215. ept = mt_find_ep(ep_num, USB_DIR_IN);
  216. if (ept == NULL) {
  217. DBG_I("ept is NULL!\n");
  218. return;
  219. }
  220. #if defined(SUPPORT_VA)
  221. gpd_current = gpd_phys_to_virt(gpd_current, USB_DIR_IN, ep_num);
  222. #endif
  223. 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]);
  224. if (gpd == gpd_current) {//gpd_current should at least point to the next GPD to the previous last one.
  225. DBG_I("should not happen: %s %d\n", __func__, __LINE__);
  226. return;
  227. }
  228. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  229. while (gpd != gpd_current && !TGPD_IS_FLAGS_HWO(gpd)) {
  230. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  231. DBG_I("Tx gpd %x info { HWO %d, BPD %d, Next_GPD %x , DataBuffer %x, BufferLen %d, Endpoint %d}\n",
  232. (u32)gpd, (u32)TGPD_GET_FLAG(gpd), (u32)TGPD_GET_FORMAT(gpd), (u32)TGPD_GET_NEXT(gpd),
  233. (u32)TGPD_GET_DATA(gpd), (u32)TGPD_GET_BUF_LEN(gpd), (u32)TGPD_GET_EPaddr(gpd));
  234. /* required for mt_udc_epx_handler */
  235. ept->sent = (u32)TGPD_GET_BUF_LEN(gpd);
  236. gpd = TGPD_GET_NEXT(gpd);
  237. #if defined(SUPPORT_VA)
  238. gpd = gpd_phys_to_virt(gpd, USB_DIR_IN, ep_num);
  239. #endif
  240. tx_gpd_last[ep_num] = gpd;
  241. }
  242. DBG_I("tx_gpd_last[%d]: 0x%x\n", ep_num, (u32)tx_gpd_last[ep_num]);
  243. DBG_I("tx_gpd_end[%d]: 0x%x\n", ep_num, (u32)tx_gpd_end[ep_num]);
  244. DBG_I("Tx %d complete\n", ep_num);
  245. return;
  246. }
  247. /*
  248. * When receiving RXQ done interrupt, qmu_interrupt calls this function.
  249. *
  250. * 1. Traverse GPD/BD data structures to count actual transferred length.
  251. * 2. Set the done flag to notify rxstate_qmu() to report status to upper gadget driver.
  252. *
  253. * ported from proc_qmu_rx() from test driver.
  254. * caller:qmu_interrupt after getting QMU done interrupt and TX is raised
  255. */
  256. void qmu_rx_interrupt(u8 ep_num)
  257. {
  258. #ifdef DBG_USB_QMU
  259. u32 bufferlength;
  260. #endif
  261. u32 recivedlength = 0;
  262. struct tgpd *gpd = (struct tgpd*) rx_gpd_last[ep_num];
  263. struct tgpd *gpd_current = (struct tgpd*)(readl(U3D_RXQCPR(ep_num)));
  264. struct tbd *bd;
  265. struct udc_endpoint *ept;
  266. struct urb *current_urb;
  267. ept = mt_find_ep(ep_num, USB_DIR_OUT);
  268. current_urb = ept->rcv_urb;
  269. #if defined(SUPPORT_VA)
  270. gpd_current = gpd_phys_to_virt(gpd_current, USB_DIR_OUT, ep_num);
  271. #endif
  272. 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]);
  273. if (gpd == gpd_current) {
  274. DBG_I("should not happen: %s %d\n", __func__, __LINE__);
  275. return;
  276. }
  277. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  278. while (gpd != gpd_current && !TGPD_IS_FLAGS_HWO(gpd)) {
  279. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  280. if (TGPD_IS_FORMAT_BDP(gpd)) {
  281. bd = (struct tbd *)TGPD_GET_DATA(gpd);
  282. #if defined(SUPPORT_VA)
  283. bd = (struct tbd *)bd_phys_to_virt(bd,USB_DIR_OUT, ep_num);
  284. #endif
  285. while (1) {
  286. DBG_I("BD: 0x%x\n", (u32)bd);
  287. DBG_I("Buf Len: 0x%x\n", (u32)TBD_GET_BUF_LEN(bd));
  288. //req->transferCount += TBD_GET_BUF_LEN(bd);
  289. //ept->qmu_done_length += TBD_GET_BUF_LEN(bd);
  290. current_urb->actual_length += TBD_GET_BUF_LEN(bd);
  291. //DBG_I("Total Len : 0x%x\n",ept->qmu_done_length);
  292. if (TBD_IS_FLAGS_EOL(bd)) {
  293. break;
  294. }
  295. bd = TBD_GET_NEXT(bd);
  296. #if defined(SUPPORT_VA)
  297. bd = bd_phys_to_virt(bd, USB_DIR_OUT, ep_num);
  298. #endif
  299. }
  300. } else {
  301. recivedlength = (u32)TGPD_GET_BUF_LEN(gpd);
  302. #ifdef DBG_USB_QMU
  303. bufferlength = (u32)TGPD_GET_DATABUF_LEN(gpd);
  304. #endif
  305. /* required for mt_udc_epx_handler */
  306. current_urb->actual_length += recivedlength;
  307. #ifdef DBG_USB_QMU
  308. DBG_I("%s: recivedlength: %d, bufferlength: %d, current_urb->actual_length: %d\n", __func__, recivedlength, bufferlength, current_urb->actual_length);
  309. #endif
  310. }
  311. DBG_I("Rx gpd info { HWO %d, Next_GPD %x ,DataBufferLength %d, DataBuffer %x, Recived Len %d, Endpoint %d, TGL %d, ZLP %d}\n",
  312. (u32)TGPD_GET_FLAG(gpd), (u32)TGPD_GET_NEXT(gpd),
  313. (u32)TGPD_GET_DATABUF_LEN(gpd), (u32)TGPD_GET_DATA(gpd),
  314. (u32)TGPD_GET_BUF_LEN(gpd), (u32)TGPD_GET_EPaddr(gpd),
  315. (u32)TGPD_GET_TGL(gpd), (u32)TGPD_GET_ZLP(gpd));
  316. gpd = TGPD_GET_NEXT(gpd);
  317. #if defined(SUPPORT_VA)
  318. gpd = gpd_phys_to_virt(gpd,USB_DIR_OUT, ep_num);
  319. #endif
  320. rx_gpd_last[ep_num] = gpd;
  321. }
  322. DBG_I("rx_gpd_last[%d]: 0x%x\n", ep_num, (u32)rx_gpd_last[ep_num]);
  323. DBG_I("rx_gpd_end[%d]: 0x%x\n", ep_num, (u32)rx_gpd_end[ep_num]);
  324. }
  325. void qmu_done_interrupt(u32 qmu_val)
  326. {
  327. int i;
  328. DBG_I("[USB][QMU] qmu_interrupt\n");
  329. for (i = 1; i <= MAX_QMU_EP; i++) {
  330. if (qmu_val & QMU_RX_DONE(i)) {
  331. qmu_rx_interrupt(i);
  332. }
  333. if (qmu_val & QMU_TX_DONE(i)) {
  334. qmu_tx_interrupt(i);
  335. }
  336. }
  337. }
  338. void qmu_exception_interrupt(void *base, u32 qmu_val)
  339. {
  340. u32 err;
  341. int i = (int) qmu_val;
  342. if (qmu_val & RXQ_CSERR_INT) {
  343. DBG_I("Rx %d checksum error!\n", i);
  344. }
  345. if (qmu_val & RXQ_LENERR_INT) {
  346. DBG_I("Rx %d length error!\n", i);
  347. }
  348. if (qmu_val & TXQ_CSERR_INT) {
  349. DBG_I("Tx %d checksum error!\n", i);
  350. }
  351. if (qmu_val & TXQ_LENERR_INT) {
  352. DBG_I("Tx %d length error!\n", i);
  353. }
  354. if ((qmu_val & RXQ_CSERR_INT) || (qmu_val & RXQ_LENERR_INT)) {
  355. err = readl(U3D_RQERRIR0);
  356. DBG_I("Rx Queue error in QMU mode![0x%x]\n", (unsigned int)err);
  357. for (i = 1; i <= MAX_QMU_EP; i++) {
  358. if (err & QMU_RX_CS_ERR(i)) {
  359. DBG_I("Rx %d length error!\n", i);
  360. }
  361. if (err & QMU_RX_LEN_ERR(i)) {
  362. DBG_I("Rx %d recieve length error!\n", i);
  363. }
  364. }
  365. writel(err, U3D_RQERRIR0);
  366. }
  367. if (qmu_val & RXQ_ZLPERR_INT) {
  368. err = readl(U3D_RQERRIR1);
  369. DBG_I("Rx Queue error in QMU mode![0x%x]\n", (unsigned int)err);
  370. for (i = 1; i <= MAX_QMU_EP; i++) {
  371. if (err &QMU_RX_ZLP_ERR(i)) {
  372. DBG_I("Rx %d recieve an zlp packet!\n", i);
  373. }
  374. }
  375. writel(err, U3D_RQERRIR1);
  376. }
  377. if ((qmu_val & TXQ_CSERR_INT) || (qmu_val & TXQ_LENERR_INT)) {
  378. err = readl(U3D_TQERRIR0);
  379. DBG_I("Tx Queue error in QMU mode![0x%x]\n", (unsigned int)err);
  380. for (i = 1; i <= MAX_QMU_EP; i++) {
  381. if (err & QMU_TX_CS_ERR(i)) {
  382. DBG_I("Tx %d checksum error!\n", i);
  383. }
  384. if (err & QMU_TX_LEN_ERR(i)) {
  385. DBG_I("Tx %d buffer length error!\n", i);
  386. }
  387. }
  388. writel(err, U3D_TQERRIR0);
  389. }
  390. if ((qmu_val & RXQ_EMPTY_INT) || (qmu_val & TXQ_EMPTY_INT)) {
  391. u32 empty = readl(U3D_QEMIR);
  392. DBG_I("Rx Empty in QMU mode![0x%x]\n", empty);
  393. writel(empty, U3D_QEMIR);
  394. mu3d_hal_flush_qmu(1, USB_DIR_OUT);
  395. mu3d_hal_restart_qmu(1, USB_DIR_OUT);
  396. }
  397. }
  398. #endif /* ifdef SUPPORT_QMU */