fifo.c 23 KB

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