fifo.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. /* inaccessible pipe is not an error */
  264. ret = 0;
  265. goto usbhs_fifo_write_busy;
  266. }
  267. ret = usbhsf_fifo_barrier(priv, fifo);
  268. if (ret < 0)
  269. goto usbhs_fifo_write_busy;
  270. buf = pkt->buf + pkt->actual;
  271. len = pkt->length - pkt->actual;
  272. len = min(len, maxp);
  273. total_len = len;
  274. is_short = total_len < maxp;
  275. /*
  276. * FIXME
  277. *
  278. * 32-bit access only
  279. */
  280. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  281. iowrite32_rep(addr, buf, len / 4);
  282. len %= 4;
  283. buf += total_len - len;
  284. }
  285. /* the rest operation */
  286. for (i = 0; i < len; i++)
  287. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  288. /*
  289. * variable update
  290. */
  291. pkt->actual += total_len;
  292. if (pkt->actual < pkt->length)
  293. *is_done = 0; /* there are remainder data */
  294. else if (is_short)
  295. *is_done = 1; /* short packet */
  296. else
  297. *is_done = !pkt->zero; /* send zero packet ? */
  298. /*
  299. * pipe/irq handling
  300. */
  301. if (is_short)
  302. usbhsf_send_terminator(pipe, fifo);
  303. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  304. usbhs_pipe_enable(pipe);
  305. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  306. usbhs_pipe_number(pipe),
  307. pkt->length, pkt->actual, *is_done, pkt->zero);
  308. /*
  309. * Transmission end
  310. */
  311. if (*is_done) {
  312. if (usbhs_pipe_is_dcp(pipe))
  313. usbhs_dcp_control_transfer_done(pipe);
  314. }
  315. usbhsf_fifo_unselect(pipe, fifo);
  316. return 0;
  317. usbhs_fifo_write_busy:
  318. usbhsf_fifo_unselect(pipe, fifo);
  319. /*
  320. * pipe is busy.
  321. * retry in interrupt
  322. */
  323. usbhsf_tx_irq_ctrl(pipe, 1);
  324. return ret;
  325. }
  326. struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  327. .prepare = usbhsf_pio_try_push,
  328. .try_run = usbhsf_pio_try_push,
  329. };
  330. /*
  331. * PIO pop handler
  332. */
  333. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  334. {
  335. struct usbhs_pipe *pipe = pkt->pipe;
  336. if (usbhs_pipe_is_busy(pipe))
  337. return 0;
  338. /*
  339. * pipe enable to prepare packet receive
  340. */
  341. usbhs_pipe_enable(pipe);
  342. usbhsf_rx_irq_ctrl(pipe, 1);
  343. return 0;
  344. }
  345. static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
  346. {
  347. struct usbhs_pipe *pipe = pkt->pipe;
  348. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  349. struct device *dev = usbhs_priv_to_dev(priv);
  350. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  351. void __iomem *addr = priv->base + fifo->port;
  352. u8 *buf;
  353. u32 data = 0;
  354. int maxp = usbhs_pipe_get_maxpacket(pipe);
  355. int rcv_len, len;
  356. int i, ret;
  357. int total_len = 0;
  358. ret = usbhsf_fifo_select(pipe, fifo, 0);
  359. if (ret < 0)
  360. return 0;
  361. ret = usbhsf_fifo_barrier(priv, fifo);
  362. if (ret < 0)
  363. goto usbhs_fifo_read_busy;
  364. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  365. buf = pkt->buf + pkt->actual;
  366. len = pkt->length - pkt->actual;
  367. len = min(len, rcv_len);
  368. total_len = len;
  369. /*
  370. * Buffer clear if Zero-Length packet
  371. *
  372. * see
  373. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  374. */
  375. if (0 == rcv_len) {
  376. usbhsf_fifo_clear(pipe, fifo);
  377. goto usbhs_fifo_read_end;
  378. }
  379. /*
  380. * FIXME
  381. *
  382. * 32-bit access only
  383. */
  384. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  385. ioread32_rep(addr, buf, len / 4);
  386. len %= 4;
  387. buf += total_len - len;
  388. }
  389. /* the rest operation */
  390. for (i = 0; i < len; i++) {
  391. if (!(i & 0x03))
  392. data = ioread32(addr);
  393. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  394. }
  395. pkt->actual += total_len;
  396. usbhs_fifo_read_end:
  397. if ((pkt->actual == pkt->length) || /* receive all data */
  398. (total_len < maxp)) { /* short packet */
  399. *is_done = 1;
  400. usbhsf_rx_irq_ctrl(pipe, 0);
  401. usbhs_pipe_disable(pipe);
  402. }
  403. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  404. usbhs_pipe_number(pipe),
  405. pkt->length, pkt->actual, *is_done, pkt->zero);
  406. usbhs_fifo_read_busy:
  407. usbhsf_fifo_unselect(pipe, fifo);
  408. return ret;
  409. }
  410. struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  411. .prepare = usbhsf_prepare_pop,
  412. .try_run = usbhsf_pio_try_pop,
  413. };
  414. /*
  415. * DCP ctrol statge handler
  416. */
  417. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  418. {
  419. usbhs_dcp_control_transfer_done(pkt->pipe);
  420. *is_done = 1;
  421. return 0;
  422. }
  423. struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  424. .prepare = usbhsf_ctrl_stage_end,
  425. .try_run = usbhsf_ctrl_stage_end,
  426. };
  427. /*
  428. * DMA fifo functions
  429. */
  430. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  431. struct usbhs_pkt *pkt)
  432. {
  433. if (&usbhs_fifo_dma_push_handler == pkt->handler)
  434. return fifo->tx_chan;
  435. if (&usbhs_fifo_dma_pop_handler == pkt->handler)
  436. return fifo->rx_chan;
  437. return NULL;
  438. }
  439. static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
  440. struct usbhs_pkt *pkt)
  441. {
  442. struct usbhs_fifo *fifo;
  443. /* DMA :: D0FIFO */
  444. fifo = usbhsf_get_d0fifo(priv);
  445. if (usbhsf_dma_chan_get(fifo, pkt) &&
  446. !usbhsf_fifo_is_busy(fifo))
  447. return fifo;
  448. /* DMA :: D1FIFO */
  449. fifo = usbhsf_get_d1fifo(priv);
  450. if (usbhsf_dma_chan_get(fifo, pkt) &&
  451. !usbhsf_fifo_is_busy(fifo))
  452. return fifo;
  453. return NULL;
  454. }
  455. #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
  456. #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
  457. static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
  458. struct usbhs_fifo *fifo,
  459. u16 dreqe)
  460. {
  461. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  462. usbhs_bset(priv, fifo->sel, DREQE, dreqe);
  463. }
  464. #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
  465. #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
  466. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  467. {
  468. struct usbhs_pipe *pipe = pkt->pipe;
  469. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  470. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  471. return info->dma_map_ctrl(pkt, map);
  472. }
  473. static void usbhsf_dma_complete(void *arg);
  474. static void usbhsf_dma_prepare_tasklet(unsigned long data)
  475. {
  476. struct usbhs_pkt *pkt = (struct usbhs_pkt *)data;
  477. struct usbhs_pipe *pipe = pkt->pipe;
  478. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  479. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  480. struct scatterlist sg;
  481. struct dma_async_tx_descriptor *desc;
  482. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  483. struct device *dev = usbhs_priv_to_dev(priv);
  484. enum dma_data_direction dir;
  485. dma_cookie_t cookie;
  486. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  487. sg_init_table(&sg, 1);
  488. sg_set_page(&sg, virt_to_page(pkt->dma),
  489. pkt->length, offset_in_page(pkt->dma));
  490. sg_dma_address(&sg) = pkt->dma + pkt->actual;
  491. sg_dma_len(&sg) = pkt->trans;
  492. desc = chan->device->device_prep_slave_sg(chan, &sg, 1, dir,
  493. DMA_PREP_INTERRUPT |
  494. DMA_CTRL_ACK);
  495. if (!desc)
  496. return;
  497. desc->callback = usbhsf_dma_complete;
  498. desc->callback_param = pipe;
  499. cookie = desc->tx_submit(desc);
  500. if (cookie < 0) {
  501. dev_err(dev, "Failed to submit dma descriptor\n");
  502. return;
  503. }
  504. dev_dbg(dev, " %s %d (%d/ %d)\n",
  505. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  506. usbhsf_dma_start(pipe, fifo);
  507. dma_async_issue_pending(chan);
  508. }
  509. /*
  510. * DMA push handler
  511. */
  512. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  513. {
  514. struct usbhs_pipe *pipe = pkt->pipe;
  515. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  516. struct usbhs_fifo *fifo;
  517. int len = pkt->length - pkt->actual;
  518. int ret;
  519. if (usbhs_pipe_is_busy(pipe))
  520. return 0;
  521. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  522. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  523. usbhs_pipe_is_dcp(pipe))
  524. goto usbhsf_pio_prepare_push;
  525. if (len % 4) /* 32bit alignment */
  526. goto usbhsf_pio_prepare_push;
  527. if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  528. goto usbhsf_pio_prepare_push;
  529. /* get enable DMA fifo */
  530. fifo = usbhsf_get_dma_fifo(priv, pkt);
  531. if (!fifo)
  532. goto usbhsf_pio_prepare_push;
  533. if (usbhsf_dma_map(pkt) < 0)
  534. goto usbhsf_pio_prepare_push;
  535. ret = usbhsf_fifo_select(pipe, fifo, 0);
  536. if (ret < 0)
  537. goto usbhsf_pio_prepare_push_unmap;
  538. pkt->trans = len;
  539. tasklet_init(&fifo->tasklet,
  540. usbhsf_dma_prepare_tasklet,
  541. (unsigned long)pkt);
  542. tasklet_schedule(&fifo->tasklet);
  543. return 0;
  544. usbhsf_pio_prepare_push_unmap:
  545. usbhsf_dma_unmap(pkt);
  546. usbhsf_pio_prepare_push:
  547. /*
  548. * change handler to PIO
  549. */
  550. pkt->handler = &usbhs_fifo_pio_push_handler;
  551. return pkt->handler->prepare(pkt, is_done);
  552. }
  553. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  554. {
  555. struct usbhs_pipe *pipe = pkt->pipe;
  556. pkt->actual = pkt->trans;
  557. *is_done = !pkt->zero; /* send zero packet ? */
  558. usbhsf_dma_stop(pipe, pipe->fifo);
  559. usbhsf_dma_unmap(pkt);
  560. usbhsf_fifo_unselect(pipe, pipe->fifo);
  561. return 0;
  562. }
  563. struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  564. .prepare = usbhsf_dma_prepare_push,
  565. .dma_done = usbhsf_dma_push_done,
  566. };
  567. /*
  568. * DMA pop handler
  569. */
  570. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  571. {
  572. struct usbhs_pipe *pipe = pkt->pipe;
  573. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  574. struct usbhs_fifo *fifo;
  575. int len, ret;
  576. if (usbhs_pipe_is_busy(pipe))
  577. return 0;
  578. if (usbhs_pipe_is_dcp(pipe))
  579. goto usbhsf_pio_prepare_pop;
  580. /* get enable DMA fifo */
  581. fifo = usbhsf_get_dma_fifo(priv, pkt);
  582. if (!fifo)
  583. goto usbhsf_pio_prepare_pop;
  584. if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  585. goto usbhsf_pio_prepare_pop;
  586. ret = usbhsf_fifo_select(pipe, fifo, 0);
  587. if (ret < 0)
  588. goto usbhsf_pio_prepare_pop;
  589. /* use PIO if packet is less than pio_dma_border */
  590. len = usbhsf_fifo_rcv_len(priv, fifo);
  591. len = min(pkt->length - pkt->actual, len);
  592. if (len % 4) /* 32bit alignment */
  593. goto usbhsf_pio_prepare_pop_unselect;
  594. if (len < usbhs_get_dparam(priv, pio_dma_border))
  595. goto usbhsf_pio_prepare_pop_unselect;
  596. ret = usbhsf_fifo_barrier(priv, fifo);
  597. if (ret < 0)
  598. goto usbhsf_pio_prepare_pop_unselect;
  599. if (usbhsf_dma_map(pkt) < 0)
  600. goto usbhsf_pio_prepare_pop_unselect;
  601. /* DMA */
  602. /*
  603. * usbhs_fifo_dma_pop_handler :: prepare
  604. * enabled irq to come here.
  605. * but it is no longer needed for DMA. disable it.
  606. */
  607. usbhsf_rx_irq_ctrl(pipe, 0);
  608. pkt->trans = len;
  609. tasklet_init(&fifo->tasklet,
  610. usbhsf_dma_prepare_tasklet,
  611. (unsigned long)pkt);
  612. tasklet_schedule(&fifo->tasklet);
  613. return 0;
  614. usbhsf_pio_prepare_pop_unselect:
  615. usbhsf_fifo_unselect(pipe, fifo);
  616. usbhsf_pio_prepare_pop:
  617. /*
  618. * change handler to PIO
  619. */
  620. pkt->handler = &usbhs_fifo_pio_pop_handler;
  621. return pkt->handler->try_run(pkt, is_done);
  622. }
  623. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  624. {
  625. struct usbhs_pipe *pipe = pkt->pipe;
  626. int maxp = usbhs_pipe_get_maxpacket(pipe);
  627. usbhsf_dma_stop(pipe, pipe->fifo);
  628. usbhsf_dma_unmap(pkt);
  629. usbhsf_fifo_unselect(pipe, pipe->fifo);
  630. pkt->actual += pkt->trans;
  631. if ((pkt->actual == pkt->length) || /* receive all data */
  632. (pkt->trans < maxp)) { /* short packet */
  633. *is_done = 1;
  634. } else {
  635. /* re-enable */
  636. usbhsf_prepare_pop(pkt, is_done);
  637. }
  638. return 0;
  639. }
  640. struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  641. .prepare = usbhsf_prepare_pop,
  642. .try_run = usbhsf_dma_try_pop,
  643. .dma_done = usbhsf_dma_pop_done
  644. };
  645. /*
  646. * DMA setting
  647. */
  648. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  649. {
  650. struct sh_dmae_slave *slave = param;
  651. /*
  652. * FIXME
  653. *
  654. * usbhs doesn't recognize id = 0 as valid DMA
  655. */
  656. if (0 == slave->slave_id)
  657. return false;
  658. chan->private = slave;
  659. return true;
  660. }
  661. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  662. {
  663. if (fifo->tx_chan)
  664. dma_release_channel(fifo->tx_chan);
  665. if (fifo->rx_chan)
  666. dma_release_channel(fifo->rx_chan);
  667. fifo->tx_chan = NULL;
  668. fifo->rx_chan = NULL;
  669. }
  670. static void usbhsf_dma_init(struct usbhs_priv *priv,
  671. struct usbhs_fifo *fifo)
  672. {
  673. struct device *dev = usbhs_priv_to_dev(priv);
  674. dma_cap_mask_t mask;
  675. dma_cap_zero(mask);
  676. dma_cap_set(DMA_SLAVE, mask);
  677. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  678. &fifo->tx_slave);
  679. dma_cap_zero(mask);
  680. dma_cap_set(DMA_SLAVE, mask);
  681. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  682. &fifo->rx_slave);
  683. if (fifo->tx_chan || fifo->rx_chan)
  684. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  685. fifo->name,
  686. fifo->tx_chan ? "[TX]" : " ",
  687. fifo->rx_chan ? "[RX]" : " ");
  688. }
  689. /*
  690. * irq functions
  691. */
  692. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  693. struct usbhs_irq_state *irq_state)
  694. {
  695. struct usbhs_pipe *pipe;
  696. struct device *dev = usbhs_priv_to_dev(priv);
  697. int i, ret;
  698. if (!irq_state->bempsts) {
  699. dev_err(dev, "debug %s !!\n", __func__);
  700. return -EIO;
  701. }
  702. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  703. /*
  704. * search interrupted "pipe"
  705. * not "uep".
  706. */
  707. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  708. if (!(irq_state->bempsts & (1 << i)))
  709. continue;
  710. ret = usbhs_pkt_run(pipe);
  711. if (ret < 0)
  712. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  713. }
  714. return 0;
  715. }
  716. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  717. struct usbhs_irq_state *irq_state)
  718. {
  719. struct usbhs_pipe *pipe;
  720. struct device *dev = usbhs_priv_to_dev(priv);
  721. int i, ret;
  722. if (!irq_state->brdysts) {
  723. dev_err(dev, "debug %s !!\n", __func__);
  724. return -EIO;
  725. }
  726. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  727. /*
  728. * search interrupted "pipe"
  729. * not "uep".
  730. */
  731. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  732. if (!(irq_state->brdysts & (1 << i)))
  733. continue;
  734. ret = usbhs_pkt_run(pipe);
  735. if (ret < 0)
  736. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  737. }
  738. return 0;
  739. }
  740. static void usbhsf_dma_complete(void *arg)
  741. {
  742. struct usbhs_pipe *pipe = arg;
  743. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  744. struct device *dev = usbhs_priv_to_dev(priv);
  745. int ret;
  746. ret = usbhs_pkt_dmadone(pipe);
  747. if (ret < 0)
  748. dev_err(dev, "dma_complete run_error %d : %d\n",
  749. usbhs_pipe_number(pipe), ret);
  750. }
  751. /*
  752. * fifo init
  753. */
  754. void usbhs_fifo_init(struct usbhs_priv *priv)
  755. {
  756. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  757. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  758. struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
  759. struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
  760. mod->irq_empty = usbhsf_irq_empty;
  761. mod->irq_ready = usbhsf_irq_ready;
  762. mod->irq_bempsts = 0;
  763. mod->irq_brdysts = 0;
  764. cfifo->pipe = NULL;
  765. cfifo->tx_chan = NULL;
  766. cfifo->rx_chan = NULL;
  767. d0fifo->pipe = NULL;
  768. d0fifo->tx_chan = NULL;
  769. d0fifo->rx_chan = NULL;
  770. d1fifo->pipe = NULL;
  771. d1fifo->tx_chan = NULL;
  772. d1fifo->rx_chan = NULL;
  773. usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
  774. usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
  775. }
  776. void usbhs_fifo_quit(struct usbhs_priv *priv)
  777. {
  778. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  779. mod->irq_empty = NULL;
  780. mod->irq_ready = NULL;
  781. mod->irq_bempsts = 0;
  782. mod->irq_brdysts = 0;
  783. usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
  784. usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
  785. }
  786. int usbhs_fifo_probe(struct usbhs_priv *priv)
  787. {
  788. struct usbhs_fifo *fifo;
  789. /* CFIFO */
  790. fifo = usbhsf_get_cfifo(priv);
  791. fifo->name = "CFIFO";
  792. fifo->port = CFIFO;
  793. fifo->sel = CFIFOSEL;
  794. fifo->ctr = CFIFOCTR;
  795. /* D0FIFO */
  796. fifo = usbhsf_get_d0fifo(priv);
  797. fifo->name = "D0FIFO";
  798. fifo->port = D0FIFO;
  799. fifo->sel = D0FIFOSEL;
  800. fifo->ctr = D0FIFOCTR;
  801. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
  802. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
  803. /* D1FIFO */
  804. fifo = usbhsf_get_d1fifo(priv);
  805. fifo->name = "D1FIFO";
  806. fifo->port = D1FIFO;
  807. fifo->sel = D1FIFOSEL;
  808. fifo->ctr = D1FIFOCTR;
  809. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
  810. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
  811. return 0;
  812. }
  813. void usbhs_fifo_remove(struct usbhs_priv *priv)
  814. {
  815. }