fifo.c 26 KB

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