fifo.c 23 KB

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