fifo.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * Renesas USB driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  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. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. *
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include "./common.h"
  20. #include "./pipe.h"
  21. #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
  22. #define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
  23. #define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
  24. #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
  25. /*
  26. * packet initialize
  27. */
  28. void usbhs_pkt_init(struct usbhs_pkt *pkt)
  29. {
  30. pkt->dma = DMA_ADDR_INVALID;
  31. INIT_LIST_HEAD(&pkt->node);
  32. }
  33. /*
  34. * packet control function
  35. */
  36. static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
  37. {
  38. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  39. struct device *dev = usbhs_priv_to_dev(priv);
  40. dev_err(dev, "null handler\n");
  41. return -EINVAL;
  42. }
  43. static struct usbhs_pkt_handle usbhsf_null_handler = {
  44. .prepare = usbhsf_null_handle,
  45. .try_run = usbhsf_null_handle,
  46. };
  47. void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
  48. struct usbhs_pkt_handle *handler,
  49. void *buf, int len, int zero)
  50. {
  51. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  52. struct device *dev = usbhs_priv_to_dev(priv);
  53. unsigned long flags;
  54. /******************** spin lock ********************/
  55. usbhs_lock(priv, flags);
  56. if (!handler) {
  57. dev_err(dev, "no handler function\n");
  58. handler = &usbhsf_null_handler;
  59. }
  60. list_del_init(&pkt->node);
  61. list_add_tail(&pkt->node, &pipe->list);
  62. pkt->pipe = pipe;
  63. pkt->buf = buf;
  64. pkt->handler = handler;
  65. pkt->length = len;
  66. pkt->zero = zero;
  67. pkt->actual = 0;
  68. usbhs_unlock(priv, flags);
  69. /******************** spin unlock ******************/
  70. usbhs_pkt_start(pipe);
  71. }
  72. static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
  73. {
  74. list_del_init(&pkt->node);
  75. }
  76. static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
  77. {
  78. if (list_empty(&pipe->list))
  79. return NULL;
  80. return list_entry(pipe->list.next, struct usbhs_pkt, node);
  81. }
  82. struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
  83. {
  84. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  85. unsigned long flags;
  86. /******************** spin lock ********************/
  87. usbhs_lock(priv, flags);
  88. if (!pkt)
  89. pkt = __usbhsf_pkt_get(pipe);
  90. if (pkt)
  91. __usbhsf_pkt_del(pkt);
  92. usbhs_unlock(priv, flags);
  93. /******************** spin unlock ******************/
  94. return pkt;
  95. }
  96. int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
  97. {
  98. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  99. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  100. struct usbhs_pkt *pkt;
  101. struct device *dev = usbhs_priv_to_dev(priv);
  102. int (*func)(struct usbhs_pkt *pkt, int *is_done);
  103. unsigned long flags;
  104. int ret = 0;
  105. int is_done = 0;
  106. /******************** spin lock ********************/
  107. usbhs_lock(priv, flags);
  108. pkt = __usbhsf_pkt_get(pipe);
  109. if (!pkt)
  110. goto __usbhs_pkt_handler_end;
  111. switch (type) {
  112. case USBHSF_PKT_PREPARE:
  113. func = pkt->handler->prepare;
  114. break;
  115. case USBHSF_PKT_TRY_RUN:
  116. func = pkt->handler->try_run;
  117. break;
  118. case USBHSF_PKT_DMA_DONE:
  119. func = pkt->handler->dma_done;
  120. break;
  121. default:
  122. dev_err(dev, "unknown pkt hander\n");
  123. goto __usbhs_pkt_handler_end;
  124. }
  125. ret = func(pkt, &is_done);
  126. if (is_done)
  127. __usbhsf_pkt_del(pkt);
  128. __usbhs_pkt_handler_end:
  129. usbhs_unlock(priv, flags);
  130. /******************** spin unlock ******************/
  131. if (is_done) {
  132. info->done(pkt);
  133. usbhs_pkt_start(pipe);
  134. }
  135. return ret;
  136. }
  137. /*
  138. * irq enable/disable function
  139. */
  140. #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
  141. #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
  142. #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
  143. ({ \
  144. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
  145. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  146. u16 status = (1 << usbhs_pipe_number(pipe)); \
  147. if (!mod) \
  148. return; \
  149. if (enable) \
  150. mod->irq_##status |= status; \
  151. else \
  152. mod->irq_##status &= ~status; \
  153. usbhs_irq_callback_update(priv, mod); \
  154. })
  155. static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  156. {
  157. /*
  158. * And DCP pipe can NOT use "ready interrupt" for "send"
  159. * it should use "empty" interrupt.
  160. * see
  161. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  162. *
  163. * on the other hand, normal pipe can use "ready interrupt" for "send"
  164. * even though it is single/double buffer
  165. */
  166. if (usbhs_pipe_is_dcp(pipe))
  167. usbhsf_irq_empty_ctrl(pipe, enable);
  168. else
  169. usbhsf_irq_ready_ctrl(pipe, enable);
  170. }
  171. static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  172. {
  173. usbhsf_irq_ready_ctrl(pipe, enable);
  174. }
  175. /*
  176. * FIFO ctrl
  177. */
  178. static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
  179. struct usbhs_fifo *fifo)
  180. {
  181. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  182. usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
  183. }
  184. static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
  185. struct usbhs_fifo *fifo)
  186. {
  187. int timeout = 1024;
  188. do {
  189. /* The FIFO port is accessible */
  190. if (usbhs_read(priv, fifo->ctr) & FRDY)
  191. return 0;
  192. udelay(10);
  193. } while (timeout--);
  194. return -EBUSY;
  195. }
  196. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
  197. struct usbhs_fifo *fifo)
  198. {
  199. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  200. if (!usbhs_pipe_is_dcp(pipe))
  201. usbhsf_fifo_barrier(priv, fifo);
  202. usbhs_write(priv, fifo->ctr, BCLR);
  203. }
  204. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
  205. struct usbhs_fifo *fifo)
  206. {
  207. return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
  208. }
  209. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  210. struct usbhs_fifo *fifo)
  211. {
  212. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  213. usbhs_pipe_select_fifo(pipe, NULL);
  214. usbhs_write(priv, fifo->sel, 0);
  215. }
  216. static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
  217. struct usbhs_fifo *fifo,
  218. int write)
  219. {
  220. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  221. struct device *dev = usbhs_priv_to_dev(priv);
  222. int timeout = 1024;
  223. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  224. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  225. if (usbhs_pipe_is_busy(pipe) ||
  226. usbhsf_fifo_is_busy(fifo))
  227. return -EBUSY;
  228. if (usbhs_pipe_is_dcp(pipe))
  229. base |= (1 == write) << 5; /* ISEL */
  230. /* "base" will be used below */
  231. usbhs_write(priv, fifo->sel, base | MBW_32);
  232. /* check ISEL and CURPIPE value */
  233. while (timeout--) {
  234. if (base == (mask & usbhs_read(priv, fifo->sel))) {
  235. usbhs_pipe_select_fifo(pipe, fifo);
  236. return 0;
  237. }
  238. udelay(10);
  239. }
  240. dev_err(dev, "fifo select error\n");
  241. return -EIO;
  242. }
  243. /*
  244. * PIO push handler
  245. */
  246. static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
  247. {
  248. struct usbhs_pipe *pipe = pkt->pipe;
  249. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  250. struct device *dev = usbhs_priv_to_dev(priv);
  251. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  252. void __iomem *addr = priv->base + fifo->port;
  253. u8 *buf;
  254. int maxp = usbhs_pipe_get_maxpacket(pipe);
  255. int total_len;
  256. int i, ret, len;
  257. int is_short;
  258. ret = usbhsf_fifo_select(pipe, fifo, 1);
  259. if (ret < 0)
  260. return 0;
  261. ret = usbhs_pipe_is_accessible(pipe);
  262. if (ret < 0)
  263. goto usbhs_fifo_write_busy;
  264. ret = usbhsf_fifo_barrier(priv, fifo);
  265. if (ret < 0)
  266. goto usbhs_fifo_write_busy;
  267. buf = pkt->buf + pkt->actual;
  268. len = pkt->length - pkt->actual;
  269. len = min(len, maxp);
  270. total_len = len;
  271. is_short = total_len < maxp;
  272. /*
  273. * FIXME
  274. *
  275. * 32-bit access only
  276. */
  277. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  278. iowrite32_rep(addr, buf, len / 4);
  279. len %= 4;
  280. buf += total_len - len;
  281. }
  282. /* the rest operation */
  283. for (i = 0; i < len; i++)
  284. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  285. /*
  286. * variable update
  287. */
  288. pkt->actual += total_len;
  289. if (pkt->actual < pkt->length)
  290. *is_done = 0; /* there are remainder data */
  291. else if (is_short)
  292. *is_done = 1; /* short packet */
  293. else
  294. *is_done = !pkt->zero; /* send zero packet ? */
  295. /*
  296. * pipe/irq handling
  297. */
  298. if (is_short)
  299. usbhsf_send_terminator(pipe, fifo);
  300. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  301. usbhs_pipe_enable(pipe);
  302. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  303. usbhs_pipe_number(pipe),
  304. pkt->length, pkt->actual, *is_done, pkt->zero);
  305. /*
  306. * Transmission end
  307. */
  308. if (*is_done) {
  309. if (usbhs_pipe_is_dcp(pipe))
  310. usbhs_dcp_control_transfer_done(pipe);
  311. }
  312. usbhsf_fifo_unselect(pipe, fifo);
  313. return 0;
  314. usbhs_fifo_write_busy:
  315. usbhsf_fifo_unselect(pipe, fifo);
  316. /*
  317. * pipe is busy.
  318. * retry in interrupt
  319. */
  320. usbhsf_tx_irq_ctrl(pipe, 1);
  321. return ret;
  322. }
  323. struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  324. .prepare = usbhsf_pio_try_push,
  325. .try_run = usbhsf_pio_try_push,
  326. };
  327. /*
  328. * PIO pop handler
  329. */
  330. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  331. {
  332. struct usbhs_pipe *pipe = pkt->pipe;
  333. if (usbhs_pipe_is_busy(pipe))
  334. return 0;
  335. /*
  336. * pipe enable to prepare packet receive
  337. */
  338. usbhs_pipe_enable(pipe);
  339. usbhsf_rx_irq_ctrl(pipe, 1);
  340. return 0;
  341. }
  342. static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
  343. {
  344. struct usbhs_pipe *pipe = pkt->pipe;
  345. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  346. struct device *dev = usbhs_priv_to_dev(priv);
  347. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  348. void __iomem *addr = priv->base + fifo->port;
  349. u8 *buf;
  350. u32 data = 0;
  351. int maxp = usbhs_pipe_get_maxpacket(pipe);
  352. int rcv_len, len;
  353. int i, ret;
  354. int total_len = 0;
  355. ret = usbhsf_fifo_select(pipe, fifo, 0);
  356. if (ret < 0)
  357. return 0;
  358. ret = usbhsf_fifo_barrier(priv, fifo);
  359. if (ret < 0)
  360. goto usbhs_fifo_read_busy;
  361. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  362. buf = pkt->buf + pkt->actual;
  363. len = pkt->length - pkt->actual;
  364. len = min(len, rcv_len);
  365. total_len = len;
  366. /*
  367. * Buffer clear if Zero-Length packet
  368. *
  369. * see
  370. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  371. */
  372. if (0 == rcv_len) {
  373. usbhsf_fifo_clear(pipe, fifo);
  374. goto usbhs_fifo_read_end;
  375. }
  376. /*
  377. * FIXME
  378. *
  379. * 32-bit access only
  380. */
  381. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  382. ioread32_rep(addr, buf, len / 4);
  383. len %= 4;
  384. buf += total_len - len;
  385. }
  386. /* the rest operation */
  387. for (i = 0; i < len; i++) {
  388. if (!(i & 0x03))
  389. data = ioread32(addr);
  390. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  391. }
  392. pkt->actual += total_len;
  393. usbhs_fifo_read_end:
  394. if ((pkt->actual == pkt->length) || /* receive all data */
  395. (total_len < maxp)) { /* short packet */
  396. *is_done = 1;
  397. usbhsf_rx_irq_ctrl(pipe, 0);
  398. usbhs_pipe_disable(pipe);
  399. }
  400. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  401. usbhs_pipe_number(pipe),
  402. pkt->length, pkt->actual, *is_done, pkt->zero);
  403. usbhs_fifo_read_busy:
  404. usbhsf_fifo_unselect(pipe, fifo);
  405. return ret;
  406. }
  407. struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  408. .prepare = usbhsf_prepare_pop,
  409. .try_run = usbhsf_pio_try_pop,
  410. };
  411. /*
  412. * DCP ctrol statge handler
  413. */
  414. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  415. {
  416. usbhs_dcp_control_transfer_done(pkt->pipe);
  417. *is_done = 1;
  418. return 0;
  419. }
  420. struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  421. .prepare = usbhsf_ctrl_stage_end,
  422. .try_run = usbhsf_ctrl_stage_end,
  423. };
  424. /*
  425. * DMA fifo functions
  426. */
  427. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  428. struct usbhs_pkt *pkt)
  429. {
  430. if (&usbhs_fifo_dma_push_handler == pkt->handler)
  431. return fifo->tx_chan;
  432. if (&usbhs_fifo_dma_pop_handler == pkt->handler)
  433. return fifo->rx_chan;
  434. return NULL;
  435. }
  436. static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
  437. struct usbhs_pkt *pkt)
  438. {
  439. struct usbhs_fifo *fifo;
  440. /* DMA :: D0FIFO */
  441. fifo = usbhsf_get_d0fifo(priv);
  442. if (usbhsf_dma_chan_get(fifo, pkt) &&
  443. !usbhsf_fifo_is_busy(fifo))
  444. return fifo;
  445. /* DMA :: D1FIFO */
  446. fifo = usbhsf_get_d1fifo(priv);
  447. if (usbhsf_dma_chan_get(fifo, pkt) &&
  448. !usbhsf_fifo_is_busy(fifo))
  449. return fifo;
  450. return NULL;
  451. }
  452. #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
  453. #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
  454. static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
  455. struct usbhs_fifo *fifo,
  456. u16 dreqe)
  457. {
  458. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  459. usbhs_bset(priv, fifo->sel, DREQE, dreqe);
  460. }
  461. #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
  462. #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
  463. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  464. {
  465. struct usbhs_pipe *pipe = pkt->pipe;
  466. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  467. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  468. return info->dma_map_ctrl(pkt, map);
  469. }
  470. static void usbhsf_dma_complete(void *arg);
  471. static void usbhsf_dma_prepare_tasklet(unsigned long data)
  472. {
  473. struct usbhs_pkt *pkt = (struct usbhs_pkt *)data;
  474. struct usbhs_pipe *pipe = pkt->pipe;
  475. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  476. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  477. struct scatterlist sg;
  478. struct dma_async_tx_descriptor *desc;
  479. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  480. struct device *dev = usbhs_priv_to_dev(priv);
  481. enum dma_data_direction dir;
  482. dma_cookie_t cookie;
  483. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  484. sg_init_table(&sg, 1);
  485. sg_set_page(&sg, virt_to_page(pkt->dma),
  486. pkt->length, offset_in_page(pkt->dma));
  487. sg_dma_address(&sg) = pkt->dma + pkt->actual;
  488. sg_dma_len(&sg) = pkt->trans;
  489. desc = chan->device->device_prep_slave_sg(chan, &sg, 1, dir,
  490. DMA_PREP_INTERRUPT |
  491. DMA_CTRL_ACK);
  492. if (!desc)
  493. return;
  494. desc->callback = usbhsf_dma_complete;
  495. desc->callback_param = pipe;
  496. cookie = desc->tx_submit(desc);
  497. if (cookie < 0) {
  498. dev_err(dev, "Failed to submit dma descriptor\n");
  499. return;
  500. }
  501. dev_dbg(dev, " %s %d (%d/ %d)\n",
  502. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  503. usbhsf_dma_start(pipe, fifo);
  504. dma_async_issue_pending(chan);
  505. }
  506. /*
  507. * DMA push handler
  508. */
  509. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  510. {
  511. struct usbhs_pipe *pipe = pkt->pipe;
  512. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  513. struct usbhs_fifo *fifo;
  514. int len = pkt->length - pkt->actual;
  515. int ret;
  516. if (usbhs_pipe_is_busy(pipe))
  517. return 0;
  518. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  519. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  520. usbhs_pipe_is_dcp(pipe))
  521. goto usbhsf_pio_prepare_push;
  522. if (len % 4) /* 32bit alignment */
  523. goto usbhsf_pio_prepare_push;
  524. if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  525. goto usbhsf_pio_prepare_push;
  526. /* get enable DMA fifo */
  527. fifo = usbhsf_get_dma_fifo(priv, pkt);
  528. if (!fifo)
  529. goto usbhsf_pio_prepare_push;
  530. if (usbhsf_dma_map(pkt) < 0)
  531. goto usbhsf_pio_prepare_push;
  532. ret = usbhsf_fifo_select(pipe, fifo, 0);
  533. if (ret < 0)
  534. goto usbhsf_pio_prepare_push_unmap;
  535. pkt->trans = len;
  536. tasklet_init(&fifo->tasklet,
  537. usbhsf_dma_prepare_tasklet,
  538. (unsigned long)pkt);
  539. tasklet_schedule(&fifo->tasklet);
  540. return 0;
  541. usbhsf_pio_prepare_push_unmap:
  542. usbhsf_dma_unmap(pkt);
  543. usbhsf_pio_prepare_push:
  544. /*
  545. * change handler to PIO
  546. */
  547. pkt->handler = &usbhs_fifo_pio_push_handler;
  548. return pkt->handler->prepare(pkt, is_done);
  549. }
  550. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  551. {
  552. struct usbhs_pipe *pipe = pkt->pipe;
  553. pkt->actual = pkt->trans;
  554. *is_done = !pkt->zero; /* send zero packet ? */
  555. usbhsf_dma_stop(pipe, pipe->fifo);
  556. usbhsf_dma_unmap(pkt);
  557. usbhsf_fifo_unselect(pipe, pipe->fifo);
  558. return 0;
  559. }
  560. struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  561. .prepare = usbhsf_dma_prepare_push,
  562. .dma_done = usbhsf_dma_push_done,
  563. };
  564. /*
  565. * DMA pop handler
  566. */
  567. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  568. {
  569. struct usbhs_pipe *pipe = pkt->pipe;
  570. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  571. struct usbhs_fifo *fifo;
  572. int len, ret;
  573. if (usbhs_pipe_is_busy(pipe))
  574. return 0;
  575. if (usbhs_pipe_is_dcp(pipe))
  576. goto usbhsf_pio_prepare_pop;
  577. /* get enable DMA fifo */
  578. fifo = usbhsf_get_dma_fifo(priv, pkt);
  579. if (!fifo)
  580. goto usbhsf_pio_prepare_pop;
  581. if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  582. goto usbhsf_pio_prepare_pop;
  583. ret = usbhsf_fifo_select(pipe, fifo, 0);
  584. if (ret < 0)
  585. goto usbhsf_pio_prepare_pop;
  586. /* use PIO if packet is less than pio_dma_border */
  587. len = usbhsf_fifo_rcv_len(priv, fifo);
  588. len = min(pkt->length - pkt->actual, len);
  589. if (len % 4) /* 32bit alignment */
  590. goto usbhsf_pio_prepare_pop_unselect;
  591. if (len < usbhs_get_dparam(priv, pio_dma_border))
  592. goto usbhsf_pio_prepare_pop_unselect;
  593. ret = usbhsf_fifo_barrier(priv, fifo);
  594. if (ret < 0)
  595. goto usbhsf_pio_prepare_pop_unselect;
  596. if (usbhsf_dma_map(pkt) < 0)
  597. goto usbhsf_pio_prepare_pop_unselect;
  598. /* DMA */
  599. /*
  600. * usbhs_fifo_dma_pop_handler :: prepare
  601. * enabled irq to come here.
  602. * but it is no longer needed for DMA. disable it.
  603. */
  604. usbhsf_rx_irq_ctrl(pipe, 0);
  605. pkt->trans = len;
  606. tasklet_init(&fifo->tasklet,
  607. usbhsf_dma_prepare_tasklet,
  608. (unsigned long)pkt);
  609. tasklet_schedule(&fifo->tasklet);
  610. return 0;
  611. usbhsf_pio_prepare_pop_unselect:
  612. usbhsf_fifo_unselect(pipe, fifo);
  613. usbhsf_pio_prepare_pop:
  614. /*
  615. * change handler to PIO
  616. */
  617. pkt->handler = &usbhs_fifo_pio_pop_handler;
  618. return pkt->handler->try_run(pkt, is_done);
  619. }
  620. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  621. {
  622. struct usbhs_pipe *pipe = pkt->pipe;
  623. int maxp = usbhs_pipe_get_maxpacket(pipe);
  624. usbhsf_dma_stop(pipe, pipe->fifo);
  625. usbhsf_dma_unmap(pkt);
  626. usbhsf_fifo_unselect(pipe, pipe->fifo);
  627. pkt->actual += pkt->trans;
  628. if ((pkt->actual == pkt->length) || /* receive all data */
  629. (pkt->trans < maxp)) { /* short packet */
  630. *is_done = 1;
  631. } else {
  632. /* re-enable */
  633. usbhsf_prepare_pop(pkt, is_done);
  634. }
  635. return 0;
  636. }
  637. struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  638. .prepare = usbhsf_prepare_pop,
  639. .try_run = usbhsf_dma_try_pop,
  640. .dma_done = usbhsf_dma_pop_done
  641. };
  642. /*
  643. * DMA setting
  644. */
  645. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  646. {
  647. struct sh_dmae_slave *slave = param;
  648. /*
  649. * FIXME
  650. *
  651. * usbhs doesn't recognize id = 0 as valid DMA
  652. */
  653. if (0 == slave->slave_id)
  654. return false;
  655. chan->private = slave;
  656. return true;
  657. }
  658. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  659. {
  660. if (fifo->tx_chan)
  661. dma_release_channel(fifo->tx_chan);
  662. if (fifo->rx_chan)
  663. dma_release_channel(fifo->rx_chan);
  664. fifo->tx_chan = NULL;
  665. fifo->rx_chan = NULL;
  666. }
  667. static void usbhsf_dma_init(struct usbhs_priv *priv,
  668. struct usbhs_fifo *fifo)
  669. {
  670. struct device *dev = usbhs_priv_to_dev(priv);
  671. dma_cap_mask_t mask;
  672. dma_cap_zero(mask);
  673. dma_cap_set(DMA_SLAVE, mask);
  674. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  675. &fifo->tx_slave);
  676. dma_cap_zero(mask);
  677. dma_cap_set(DMA_SLAVE, mask);
  678. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  679. &fifo->rx_slave);
  680. if (fifo->tx_chan || fifo->rx_chan)
  681. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  682. fifo->name,
  683. fifo->tx_chan ? "[TX]" : " ",
  684. fifo->rx_chan ? "[RX]" : " ");
  685. }
  686. /*
  687. * irq functions
  688. */
  689. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  690. struct usbhs_irq_state *irq_state)
  691. {
  692. struct usbhs_pipe *pipe;
  693. struct device *dev = usbhs_priv_to_dev(priv);
  694. int i, ret;
  695. if (!irq_state->bempsts) {
  696. dev_err(dev, "debug %s !!\n", __func__);
  697. return -EIO;
  698. }
  699. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  700. /*
  701. * search interrupted "pipe"
  702. * not "uep".
  703. */
  704. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  705. if (!(irq_state->bempsts & (1 << i)))
  706. continue;
  707. ret = usbhs_pkt_run(pipe);
  708. if (ret < 0)
  709. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  710. }
  711. return 0;
  712. }
  713. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  714. struct usbhs_irq_state *irq_state)
  715. {
  716. struct usbhs_pipe *pipe;
  717. struct device *dev = usbhs_priv_to_dev(priv);
  718. int i, ret;
  719. if (!irq_state->brdysts) {
  720. dev_err(dev, "debug %s !!\n", __func__);
  721. return -EIO;
  722. }
  723. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  724. /*
  725. * search interrupted "pipe"
  726. * not "uep".
  727. */
  728. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  729. if (!(irq_state->brdysts & (1 << i)))
  730. continue;
  731. ret = usbhs_pkt_run(pipe);
  732. if (ret < 0)
  733. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  734. }
  735. return 0;
  736. }
  737. static void usbhsf_dma_complete(void *arg)
  738. {
  739. struct usbhs_pipe *pipe = arg;
  740. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  741. struct device *dev = usbhs_priv_to_dev(priv);
  742. int ret;
  743. ret = usbhs_pkt_dmadone(pipe);
  744. if (ret < 0)
  745. dev_err(dev, "dma_complete run_error %d : %d\n",
  746. usbhs_pipe_number(pipe), ret);
  747. }
  748. /*
  749. * fifo init
  750. */
  751. void usbhs_fifo_init(struct usbhs_priv *priv)
  752. {
  753. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  754. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  755. struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
  756. struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
  757. mod->irq_empty = usbhsf_irq_empty;
  758. mod->irq_ready = usbhsf_irq_ready;
  759. mod->irq_bempsts = 0;
  760. mod->irq_brdysts = 0;
  761. cfifo->pipe = NULL;
  762. cfifo->tx_chan = NULL;
  763. cfifo->rx_chan = NULL;
  764. d0fifo->pipe = NULL;
  765. d0fifo->tx_chan = NULL;
  766. d0fifo->rx_chan = NULL;
  767. d1fifo->pipe = NULL;
  768. d1fifo->tx_chan = NULL;
  769. d1fifo->rx_chan = NULL;
  770. usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
  771. usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
  772. }
  773. void usbhs_fifo_quit(struct usbhs_priv *priv)
  774. {
  775. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  776. mod->irq_empty = NULL;
  777. mod->irq_ready = NULL;
  778. mod->irq_bempsts = 0;
  779. mod->irq_brdysts = 0;
  780. usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
  781. usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
  782. }
  783. int usbhs_fifo_probe(struct usbhs_priv *priv)
  784. {
  785. struct usbhs_fifo *fifo;
  786. /* CFIFO */
  787. fifo = usbhsf_get_cfifo(priv);
  788. fifo->name = "CFIFO";
  789. fifo->port = CFIFO;
  790. fifo->sel = CFIFOSEL;
  791. fifo->ctr = CFIFOCTR;
  792. /* D0FIFO */
  793. fifo = usbhsf_get_d0fifo(priv);
  794. fifo->name = "D0FIFO";
  795. fifo->port = D0FIFO;
  796. fifo->sel = D0FIFOSEL;
  797. fifo->ctr = D0FIFOCTR;
  798. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
  799. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
  800. /* D1FIFO */
  801. fifo = usbhsf_get_d1fifo(priv);
  802. fifo->name = "D1FIFO";
  803. fifo->port = D1FIFO;
  804. fifo->sel = D1FIFOSEL;
  805. fifo->ctr = D1FIFOCTR;
  806. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
  807. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
  808. return 0;
  809. }
  810. void usbhs_fifo_remove(struct usbhs_priv *priv)
  811. {
  812. }