fifo.c 27 KB

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