fifo.c 27 KB

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