fifo.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. /* get enable DMA fifo */
  525. fifo = usbhsf_get_dma_fifo(priv, pkt);
  526. if (!fifo)
  527. goto usbhsf_pio_prepare_push;
  528. if (usbhsf_dma_map(pkt) < 0)
  529. goto usbhsf_pio_prepare_push;
  530. ret = usbhsf_fifo_select(pipe, fifo, 0);
  531. if (ret < 0)
  532. goto usbhsf_pio_prepare_push_unmap;
  533. pkt->trans = len;
  534. tasklet_init(&fifo->tasklet,
  535. usbhsf_dma_prepare_tasklet,
  536. (unsigned long)pkt);
  537. tasklet_schedule(&fifo->tasklet);
  538. return 0;
  539. usbhsf_pio_prepare_push_unmap:
  540. usbhsf_dma_unmap(pkt);
  541. usbhsf_pio_prepare_push:
  542. /*
  543. * change handler to PIO
  544. */
  545. pkt->handler = &usbhs_fifo_pio_push_handler;
  546. return pkt->handler->prepare(pkt, is_done);
  547. }
  548. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  549. {
  550. struct usbhs_pipe *pipe = pkt->pipe;
  551. pkt->actual = pkt->trans;
  552. *is_done = !pkt->zero; /* send zero packet ? */
  553. usbhsf_dma_stop(pipe, pipe->fifo);
  554. usbhsf_dma_unmap(pkt);
  555. usbhsf_fifo_unselect(pipe, pipe->fifo);
  556. return 0;
  557. }
  558. struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  559. .prepare = usbhsf_dma_prepare_push,
  560. .dma_done = usbhsf_dma_push_done,
  561. };
  562. /*
  563. * DMA pop handler
  564. */
  565. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  566. {
  567. struct usbhs_pipe *pipe = pkt->pipe;
  568. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  569. struct usbhs_fifo *fifo;
  570. int len, ret;
  571. if (usbhs_pipe_is_busy(pipe))
  572. return 0;
  573. if (usbhs_pipe_is_dcp(pipe))
  574. goto usbhsf_pio_prepare_pop;
  575. /* get enable DMA fifo */
  576. fifo = usbhsf_get_dma_fifo(priv, pkt);
  577. if (!fifo)
  578. goto usbhsf_pio_prepare_pop;
  579. ret = usbhsf_fifo_select(pipe, fifo, 0);
  580. if (ret < 0)
  581. goto usbhsf_pio_prepare_pop;
  582. /* use PIO if packet is less than pio_dma_border */
  583. len = usbhsf_fifo_rcv_len(priv, fifo);
  584. len = min(pkt->length - pkt->actual, len);
  585. if (len % 4) /* 32bit alignment */
  586. goto usbhsf_pio_prepare_pop_unselect;
  587. if (len < usbhs_get_dparam(priv, pio_dma_border))
  588. goto usbhsf_pio_prepare_pop_unselect;
  589. ret = usbhsf_fifo_barrier(priv, fifo);
  590. if (ret < 0)
  591. goto usbhsf_pio_prepare_pop_unselect;
  592. if (usbhsf_dma_map(pkt) < 0)
  593. goto usbhsf_pio_prepare_pop_unselect;
  594. /* DMA */
  595. /*
  596. * usbhs_fifo_dma_pop_handler :: prepare
  597. * enabled irq to come here.
  598. * but it is no longer needed for DMA. disable it.
  599. */
  600. usbhsf_rx_irq_ctrl(pipe, 0);
  601. pkt->trans = len;
  602. tasklet_init(&fifo->tasklet,
  603. usbhsf_dma_prepare_tasklet,
  604. (unsigned long)pkt);
  605. tasklet_schedule(&fifo->tasklet);
  606. return 0;
  607. usbhsf_pio_prepare_pop_unselect:
  608. usbhsf_fifo_unselect(pipe, fifo);
  609. usbhsf_pio_prepare_pop:
  610. /*
  611. * change handler to PIO
  612. */
  613. pkt->handler = &usbhs_fifo_pio_pop_handler;
  614. return pkt->handler->try_run(pkt, is_done);
  615. }
  616. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  617. {
  618. struct usbhs_pipe *pipe = pkt->pipe;
  619. int maxp = usbhs_pipe_get_maxpacket(pipe);
  620. usbhsf_dma_stop(pipe, pipe->fifo);
  621. usbhsf_dma_unmap(pkt);
  622. usbhsf_fifo_unselect(pipe, pipe->fifo);
  623. pkt->actual += pkt->trans;
  624. if ((pkt->actual == pkt->length) || /* receive all data */
  625. (pkt->trans < maxp)) { /* short packet */
  626. *is_done = 1;
  627. } else {
  628. /* re-enable */
  629. usbhsf_prepare_pop(pkt, is_done);
  630. }
  631. return 0;
  632. }
  633. struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  634. .prepare = usbhsf_prepare_pop,
  635. .try_run = usbhsf_dma_try_pop,
  636. .dma_done = usbhsf_dma_pop_done
  637. };
  638. /*
  639. * DMA setting
  640. */
  641. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  642. {
  643. struct sh_dmae_slave *slave = param;
  644. /*
  645. * FIXME
  646. *
  647. * usbhs doesn't recognize id = 0 as valid DMA
  648. */
  649. if (0 == slave->slave_id)
  650. return false;
  651. chan->private = slave;
  652. return true;
  653. }
  654. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  655. {
  656. if (fifo->tx_chan)
  657. dma_release_channel(fifo->tx_chan);
  658. if (fifo->rx_chan)
  659. dma_release_channel(fifo->rx_chan);
  660. fifo->tx_chan = NULL;
  661. fifo->rx_chan = NULL;
  662. }
  663. static void usbhsf_dma_init(struct usbhs_priv *priv,
  664. struct usbhs_fifo *fifo)
  665. {
  666. struct device *dev = usbhs_priv_to_dev(priv);
  667. dma_cap_mask_t mask;
  668. dma_cap_zero(mask);
  669. dma_cap_set(DMA_SLAVE, mask);
  670. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  671. &fifo->tx_slave);
  672. dma_cap_zero(mask);
  673. dma_cap_set(DMA_SLAVE, mask);
  674. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  675. &fifo->rx_slave);
  676. if (fifo->tx_chan || fifo->rx_chan)
  677. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  678. fifo->name,
  679. fifo->tx_chan ? "[TX]" : " ",
  680. fifo->rx_chan ? "[RX]" : " ");
  681. }
  682. /*
  683. * irq functions
  684. */
  685. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  686. struct usbhs_irq_state *irq_state)
  687. {
  688. struct usbhs_pipe *pipe;
  689. struct device *dev = usbhs_priv_to_dev(priv);
  690. int i, ret;
  691. if (!irq_state->bempsts) {
  692. dev_err(dev, "debug %s !!\n", __func__);
  693. return -EIO;
  694. }
  695. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  696. /*
  697. * search interrupted "pipe"
  698. * not "uep".
  699. */
  700. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  701. if (!(irq_state->bempsts & (1 << i)))
  702. continue;
  703. ret = usbhs_pkt_run(pipe);
  704. if (ret < 0)
  705. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  706. }
  707. return 0;
  708. }
  709. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  710. struct usbhs_irq_state *irq_state)
  711. {
  712. struct usbhs_pipe *pipe;
  713. struct device *dev = usbhs_priv_to_dev(priv);
  714. int i, ret;
  715. if (!irq_state->brdysts) {
  716. dev_err(dev, "debug %s !!\n", __func__);
  717. return -EIO;
  718. }
  719. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  720. /*
  721. * search interrupted "pipe"
  722. * not "uep".
  723. */
  724. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  725. if (!(irq_state->brdysts & (1 << i)))
  726. continue;
  727. ret = usbhs_pkt_run(pipe);
  728. if (ret < 0)
  729. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  730. }
  731. return 0;
  732. }
  733. static void usbhsf_dma_complete(void *arg)
  734. {
  735. struct usbhs_pipe *pipe = arg;
  736. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  737. struct device *dev = usbhs_priv_to_dev(priv);
  738. int ret;
  739. ret = usbhs_pkt_dmadone(pipe);
  740. if (ret < 0)
  741. dev_err(dev, "dma_complete run_error %d : %d\n",
  742. usbhs_pipe_number(pipe), ret);
  743. }
  744. /*
  745. * fifo init
  746. */
  747. void usbhs_fifo_init(struct usbhs_priv *priv)
  748. {
  749. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  750. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  751. struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
  752. struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
  753. mod->irq_empty = usbhsf_irq_empty;
  754. mod->irq_ready = usbhsf_irq_ready;
  755. mod->irq_bempsts = 0;
  756. mod->irq_brdysts = 0;
  757. cfifo->pipe = NULL;
  758. cfifo->tx_chan = NULL;
  759. cfifo->rx_chan = NULL;
  760. d0fifo->pipe = NULL;
  761. d0fifo->tx_chan = NULL;
  762. d0fifo->rx_chan = NULL;
  763. d1fifo->pipe = NULL;
  764. d1fifo->tx_chan = NULL;
  765. d1fifo->rx_chan = NULL;
  766. usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
  767. usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
  768. }
  769. void usbhs_fifo_quit(struct usbhs_priv *priv)
  770. {
  771. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  772. mod->irq_empty = NULL;
  773. mod->irq_ready = NULL;
  774. mod->irq_bempsts = 0;
  775. mod->irq_brdysts = 0;
  776. usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
  777. usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
  778. }
  779. int usbhs_fifo_probe(struct usbhs_priv *priv)
  780. {
  781. struct usbhs_fifo *fifo;
  782. /* CFIFO */
  783. fifo = usbhsf_get_cfifo(priv);
  784. fifo->name = "CFIFO";
  785. fifo->port = CFIFO;
  786. fifo->sel = CFIFOSEL;
  787. fifo->ctr = CFIFOCTR;
  788. /* D0FIFO */
  789. fifo = usbhsf_get_d0fifo(priv);
  790. fifo->name = "D0FIFO";
  791. fifo->port = D0FIFO;
  792. fifo->sel = D0FIFOSEL;
  793. fifo->ctr = D0FIFOCTR;
  794. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
  795. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
  796. /* D1FIFO */
  797. fifo = usbhsf_get_d1fifo(priv);
  798. fifo->name = "D1FIFO";
  799. fifo->port = D1FIFO;
  800. fifo->sel = D1FIFOSEL;
  801. fifo->ctr = D1FIFOCTR;
  802. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
  803. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
  804. return 0;
  805. }
  806. void usbhs_fifo_remove(struct usbhs_priv *priv)
  807. {
  808. }