fifo.c 22 KB

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