fifo.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Renesas USB driver
  4. *
  5. * Copyright (C) 2011 Renesas Solutions Corp.
  6. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/scatterlist.h>
  11. #include "common.h"
  12. #include "pipe.h"
  13. #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
  14. #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
  15. #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
  16. /*
  17. * packet initialize
  18. */
  19. void usbhs_pkt_init(struct usbhs_pkt *pkt)
  20. {
  21. INIT_LIST_HEAD(&pkt->node);
  22. }
  23. /*
  24. * packet control function
  25. */
  26. static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
  27. {
  28. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  29. struct device *dev = usbhs_priv_to_dev(priv);
  30. dev_err(dev, "null handler\n");
  31. return -EINVAL;
  32. }
  33. static const struct usbhs_pkt_handle usbhsf_null_handler = {
  34. .prepare = usbhsf_null_handle,
  35. .try_run = usbhsf_null_handle,
  36. };
  37. void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
  38. void (*done)(struct usbhs_priv *priv,
  39. struct usbhs_pkt *pkt),
  40. void *buf, int len, int zero, int sequence)
  41. {
  42. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  43. struct device *dev = usbhs_priv_to_dev(priv);
  44. unsigned long flags;
  45. if (!done) {
  46. dev_err(dev, "no done function\n");
  47. return;
  48. }
  49. /******************** spin lock ********************/
  50. usbhs_lock(priv, flags);
  51. if (!pipe->handler) {
  52. dev_err(dev, "no handler function\n");
  53. pipe->handler = &usbhsf_null_handler;
  54. }
  55. list_move_tail(&pkt->node, &pipe->list);
  56. /*
  57. * each pkt must hold own handler.
  58. * because handler might be changed by its situation.
  59. * dma handler -> pio handler.
  60. */
  61. pkt->pipe = pipe;
  62. pkt->buf = buf;
  63. pkt->handler = pipe->handler;
  64. pkt->length = len;
  65. pkt->zero = zero;
  66. pkt->actual = 0;
  67. pkt->done = done;
  68. pkt->sequence = sequence;
  69. usbhs_unlock(priv, flags);
  70. /******************** spin unlock ******************/
  71. }
  72. static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
  73. {
  74. list_del_init(&pkt->node);
  75. }
  76. static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
  77. {
  78. return list_first_entry_or_null(&pipe->list, struct usbhs_pkt, node);
  79. }
  80. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  81. struct usbhs_fifo *fifo);
  82. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  83. struct usbhs_pkt *pkt);
  84. #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
  85. #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
  86. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
  87. struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
  88. {
  89. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  90. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  91. unsigned long flags;
  92. /******************** spin lock ********************/
  93. usbhs_lock(priv, flags);
  94. usbhs_pipe_disable(pipe);
  95. if (!pkt)
  96. pkt = __usbhsf_pkt_get(pipe);
  97. if (pkt) {
  98. struct dma_chan *chan = NULL;
  99. if (fifo)
  100. chan = usbhsf_dma_chan_get(fifo, pkt);
  101. if (chan) {
  102. dmaengine_terminate_all(chan);
  103. usbhsf_dma_unmap(pkt);
  104. }
  105. usbhs_pipe_clear_without_sequence(pipe, 0, 0);
  106. __usbhsf_pkt_del(pkt);
  107. }
  108. if (fifo)
  109. usbhsf_fifo_unselect(pipe, fifo);
  110. usbhs_unlock(priv, flags);
  111. /******************** spin unlock ******************/
  112. return pkt;
  113. }
  114. enum {
  115. USBHSF_PKT_PREPARE,
  116. USBHSF_PKT_TRY_RUN,
  117. USBHSF_PKT_DMA_DONE,
  118. };
  119. static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
  120. {
  121. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  122. struct usbhs_pkt *pkt;
  123. struct device *dev = usbhs_priv_to_dev(priv);
  124. int (*func)(struct usbhs_pkt *pkt, int *is_done);
  125. unsigned long flags;
  126. int ret = 0;
  127. int is_done = 0;
  128. /******************** spin lock ********************/
  129. usbhs_lock(priv, flags);
  130. pkt = __usbhsf_pkt_get(pipe);
  131. if (!pkt)
  132. goto __usbhs_pkt_handler_end;
  133. switch (type) {
  134. case USBHSF_PKT_PREPARE:
  135. func = pkt->handler->prepare;
  136. break;
  137. case USBHSF_PKT_TRY_RUN:
  138. func = pkt->handler->try_run;
  139. break;
  140. case USBHSF_PKT_DMA_DONE:
  141. func = pkt->handler->dma_done;
  142. break;
  143. default:
  144. dev_err(dev, "unknown pkt handler\n");
  145. goto __usbhs_pkt_handler_end;
  146. }
  147. if (likely(func))
  148. ret = func(pkt, &is_done);
  149. if (is_done)
  150. __usbhsf_pkt_del(pkt);
  151. __usbhs_pkt_handler_end:
  152. usbhs_unlock(priv, flags);
  153. /******************** spin unlock ******************/
  154. if (is_done) {
  155. pkt->done(priv, pkt);
  156. usbhs_pkt_start(pipe);
  157. }
  158. return ret;
  159. }
  160. void usbhs_pkt_start(struct usbhs_pipe *pipe)
  161. {
  162. usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
  163. }
  164. /*
  165. * irq enable/disable function
  166. */
  167. #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
  168. #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
  169. #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
  170. ({ \
  171. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
  172. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  173. u16 status = (1 << usbhs_pipe_number(pipe)); \
  174. if (!mod) \
  175. return; \
  176. if (enable) \
  177. mod->status |= status; \
  178. else \
  179. mod->status &= ~status; \
  180. usbhs_irq_callback_update(priv, mod); \
  181. })
  182. static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  183. {
  184. /*
  185. * And DCP pipe can NOT use "ready interrupt" for "send"
  186. * it should use "empty" interrupt.
  187. * see
  188. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  189. *
  190. * on the other hand, normal pipe can use "ready interrupt" for "send"
  191. * even though it is single/double buffer
  192. */
  193. if (usbhs_pipe_is_dcp(pipe))
  194. usbhsf_irq_empty_ctrl(pipe, enable);
  195. else
  196. usbhsf_irq_ready_ctrl(pipe, enable);
  197. }
  198. static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  199. {
  200. usbhsf_irq_ready_ctrl(pipe, enable);
  201. }
  202. /*
  203. * FIFO ctrl
  204. */
  205. static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
  206. struct usbhs_fifo *fifo)
  207. {
  208. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  209. usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
  210. }
  211. static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
  212. struct usbhs_fifo *fifo)
  213. {
  214. /* The FIFO port is accessible */
  215. if (usbhs_read(priv, fifo->ctr) & FRDY)
  216. return 0;
  217. return -EBUSY;
  218. }
  219. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
  220. struct usbhs_fifo *fifo)
  221. {
  222. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  223. int ret = 0;
  224. if (!usbhs_pipe_is_dcp(pipe)) {
  225. /*
  226. * This driver checks the pipe condition first to avoid -EBUSY
  227. * from usbhsf_fifo_barrier() if the pipe is RX direction and
  228. * empty.
  229. */
  230. if (usbhs_pipe_is_dir_in(pipe))
  231. ret = usbhs_pipe_is_accessible(pipe);
  232. if (!ret)
  233. ret = usbhsf_fifo_barrier(priv, fifo);
  234. }
  235. /*
  236. * if non-DCP pipe, this driver should set BCLR when
  237. * usbhsf_fifo_barrier() returns 0.
  238. */
  239. if (!ret)
  240. usbhs_write(priv, fifo->ctr, BCLR);
  241. }
  242. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
  243. struct usbhs_fifo *fifo)
  244. {
  245. return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
  246. }
  247. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  248. struct usbhs_fifo *fifo)
  249. {
  250. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  251. usbhs_pipe_select_fifo(pipe, NULL);
  252. usbhs_write(priv, fifo->sel, 0);
  253. }
  254. static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
  255. struct usbhs_fifo *fifo,
  256. int write)
  257. {
  258. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  259. struct device *dev = usbhs_priv_to_dev(priv);
  260. int timeout = 1024;
  261. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  262. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  263. if (usbhs_pipe_is_busy(pipe) ||
  264. usbhsf_fifo_is_busy(fifo))
  265. return -EBUSY;
  266. if (usbhs_pipe_is_dcp(pipe)) {
  267. base |= (1 == write) << 5; /* ISEL */
  268. if (usbhs_mod_is_host(priv))
  269. usbhs_dcp_dir_for_host(pipe, write);
  270. }
  271. /* "base" will be used below */
  272. if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
  273. usbhs_write(priv, fifo->sel, base);
  274. else
  275. usbhs_write(priv, fifo->sel, base | MBW_32);
  276. /* check ISEL and CURPIPE value */
  277. while (timeout--) {
  278. if (base == (mask & usbhs_read(priv, fifo->sel))) {
  279. usbhs_pipe_select_fifo(pipe, fifo);
  280. return 0;
  281. }
  282. udelay(10);
  283. }
  284. dev_err(dev, "fifo select error\n");
  285. return -EIO;
  286. }
  287. /*
  288. * DCP status stage
  289. */
  290. static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
  291. {
  292. struct usbhs_pipe *pipe = pkt->pipe;
  293. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  294. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  295. struct device *dev = usbhs_priv_to_dev(priv);
  296. int ret;
  297. usbhs_pipe_disable(pipe);
  298. ret = usbhsf_fifo_select(pipe, fifo, 1);
  299. if (ret < 0) {
  300. dev_err(dev, "%s() faile\n", __func__);
  301. return ret;
  302. }
  303. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  304. usbhsf_fifo_clear(pipe, fifo);
  305. usbhsf_send_terminator(pipe, fifo);
  306. usbhsf_fifo_unselect(pipe, fifo);
  307. usbhsf_tx_irq_ctrl(pipe, 1);
  308. usbhs_pipe_enable(pipe);
  309. return ret;
  310. }
  311. static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
  312. {
  313. struct usbhs_pipe *pipe = pkt->pipe;
  314. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  315. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  316. struct device *dev = usbhs_priv_to_dev(priv);
  317. int ret;
  318. usbhs_pipe_disable(pipe);
  319. ret = usbhsf_fifo_select(pipe, fifo, 0);
  320. if (ret < 0) {
  321. dev_err(dev, "%s() fail\n", __func__);
  322. return ret;
  323. }
  324. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  325. usbhsf_fifo_clear(pipe, fifo);
  326. usbhsf_fifo_unselect(pipe, fifo);
  327. usbhsf_rx_irq_ctrl(pipe, 1);
  328. usbhs_pipe_enable(pipe);
  329. return ret;
  330. }
  331. static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
  332. {
  333. struct usbhs_pipe *pipe = pkt->pipe;
  334. if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
  335. usbhsf_tx_irq_ctrl(pipe, 0);
  336. else
  337. usbhsf_rx_irq_ctrl(pipe, 0);
  338. pkt->actual = pkt->length;
  339. *is_done = 1;
  340. return 0;
  341. }
  342. const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
  343. .prepare = usbhs_dcp_dir_switch_to_write,
  344. .try_run = usbhs_dcp_dir_switch_done,
  345. };
  346. const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
  347. .prepare = usbhs_dcp_dir_switch_to_read,
  348. .try_run = usbhs_dcp_dir_switch_done,
  349. };
  350. /*
  351. * DCP data stage (push)
  352. */
  353. static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
  354. {
  355. struct usbhs_pipe *pipe = pkt->pipe;
  356. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  357. /*
  358. * change handler to PIO push
  359. */
  360. pkt->handler = &usbhs_fifo_pio_push_handler;
  361. return pkt->handler->prepare(pkt, is_done);
  362. }
  363. const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
  364. .prepare = usbhsf_dcp_data_stage_try_push,
  365. };
  366. /*
  367. * DCP data stage (pop)
  368. */
  369. static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
  370. int *is_done)
  371. {
  372. struct usbhs_pipe *pipe = pkt->pipe;
  373. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  374. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
  375. if (usbhs_pipe_is_busy(pipe))
  376. return 0;
  377. /*
  378. * prepare pop for DCP should
  379. * - change DCP direction,
  380. * - clear fifo
  381. * - DATA1
  382. */
  383. usbhs_pipe_disable(pipe);
  384. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  385. usbhsf_fifo_select(pipe, fifo, 0);
  386. usbhsf_fifo_clear(pipe, fifo);
  387. usbhsf_fifo_unselect(pipe, fifo);
  388. /*
  389. * change handler to PIO pop
  390. */
  391. pkt->handler = &usbhs_fifo_pio_pop_handler;
  392. return pkt->handler->prepare(pkt, is_done);
  393. }
  394. const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
  395. .prepare = usbhsf_dcp_data_stage_prepare_pop,
  396. };
  397. /*
  398. * PIO push handler
  399. */
  400. static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
  401. {
  402. struct usbhs_pipe *pipe = pkt->pipe;
  403. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  404. struct device *dev = usbhs_priv_to_dev(priv);
  405. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  406. void __iomem *addr = priv->base + fifo->port;
  407. u8 *buf;
  408. int maxp = usbhs_pipe_get_maxpacket(pipe);
  409. int total_len;
  410. int i, ret, len;
  411. int is_short;
  412. usbhs_pipe_data_sequence(pipe, pkt->sequence);
  413. pkt->sequence = -1; /* -1 sequence will be ignored */
  414. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
  415. ret = usbhsf_fifo_select(pipe, fifo, 1);
  416. if (ret < 0)
  417. return 0;
  418. ret = usbhs_pipe_is_accessible(pipe);
  419. if (ret < 0) {
  420. /* inaccessible pipe is not an error */
  421. ret = 0;
  422. goto usbhs_fifo_write_busy;
  423. }
  424. ret = usbhsf_fifo_barrier(priv, fifo);
  425. if (ret < 0)
  426. goto usbhs_fifo_write_busy;
  427. buf = pkt->buf + pkt->actual;
  428. len = pkt->length - pkt->actual;
  429. len = min(len, maxp);
  430. total_len = len;
  431. is_short = total_len < maxp;
  432. /*
  433. * FIXME
  434. *
  435. * 32-bit access only
  436. */
  437. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  438. iowrite32_rep(addr, buf, len / 4);
  439. len %= 4;
  440. buf += total_len - len;
  441. }
  442. /* the rest operation */
  443. for (i = 0; i < len; i++)
  444. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  445. /*
  446. * variable update
  447. */
  448. pkt->actual += total_len;
  449. if (pkt->actual < pkt->length)
  450. *is_done = 0; /* there are remainder data */
  451. else if (is_short)
  452. *is_done = 1; /* short packet */
  453. else
  454. *is_done = !pkt->zero; /* send zero packet ? */
  455. /*
  456. * pipe/irq handling
  457. */
  458. if (is_short)
  459. usbhsf_send_terminator(pipe, fifo);
  460. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  461. usbhs_pipe_running(pipe, !*is_done);
  462. usbhs_pipe_enable(pipe);
  463. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  464. usbhs_pipe_number(pipe),
  465. pkt->length, pkt->actual, *is_done, pkt->zero);
  466. usbhsf_fifo_unselect(pipe, fifo);
  467. return 0;
  468. usbhs_fifo_write_busy:
  469. usbhsf_fifo_unselect(pipe, fifo);
  470. /*
  471. * pipe is busy.
  472. * retry in interrupt
  473. */
  474. usbhsf_tx_irq_ctrl(pipe, 1);
  475. usbhs_pipe_running(pipe, 1);
  476. return ret;
  477. }
  478. static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  479. {
  480. if (usbhs_pipe_is_running(pkt->pipe))
  481. return 0;
  482. return usbhsf_pio_try_push(pkt, is_done);
  483. }
  484. const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  485. .prepare = usbhsf_pio_prepare_push,
  486. .try_run = usbhsf_pio_try_push,
  487. };
  488. /*
  489. * PIO pop handler
  490. */
  491. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  492. {
  493. struct usbhs_pipe *pipe = pkt->pipe;
  494. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  495. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
  496. if (usbhs_pipe_is_busy(pipe))
  497. return 0;
  498. if (usbhs_pipe_is_running(pipe))
  499. return 0;
  500. /*
  501. * pipe enable to prepare packet receive
  502. */
  503. usbhs_pipe_data_sequence(pipe, pkt->sequence);
  504. pkt->sequence = -1; /* -1 sequence will be ignored */
  505. if (usbhs_pipe_is_dcp(pipe))
  506. usbhsf_fifo_clear(pipe, fifo);
  507. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
  508. usbhs_pipe_enable(pipe);
  509. usbhs_pipe_running(pipe, 1);
  510. usbhsf_rx_irq_ctrl(pipe, 1);
  511. return 0;
  512. }
  513. static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
  514. {
  515. struct usbhs_pipe *pipe = pkt->pipe;
  516. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  517. struct device *dev = usbhs_priv_to_dev(priv);
  518. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  519. void __iomem *addr = priv->base + fifo->port;
  520. u8 *buf;
  521. u32 data = 0;
  522. int maxp = usbhs_pipe_get_maxpacket(pipe);
  523. int rcv_len, len;
  524. int i, ret;
  525. int total_len = 0;
  526. ret = usbhsf_fifo_select(pipe, fifo, 0);
  527. if (ret < 0)
  528. return 0;
  529. ret = usbhsf_fifo_barrier(priv, fifo);
  530. if (ret < 0)
  531. goto usbhs_fifo_read_busy;
  532. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  533. buf = pkt->buf + pkt->actual;
  534. len = pkt->length - pkt->actual;
  535. len = min(len, rcv_len);
  536. total_len = len;
  537. /*
  538. * update actual length first here to decide disable pipe.
  539. * if this pipe keeps BUF status and all data were popped,
  540. * then, next interrupt/token will be issued again
  541. */
  542. pkt->actual += total_len;
  543. if ((pkt->actual == pkt->length) || /* receive all data */
  544. (total_len < maxp)) { /* short packet */
  545. *is_done = 1;
  546. usbhsf_rx_irq_ctrl(pipe, 0);
  547. usbhs_pipe_running(pipe, 0);
  548. /*
  549. * If function mode, since this controller is possible to enter
  550. * Control Write status stage at this timing, this driver
  551. * should not disable the pipe. If such a case happens, this
  552. * controller is not able to complete the status stage.
  553. */
  554. if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
  555. usbhs_pipe_disable(pipe); /* disable pipe first */
  556. }
  557. /*
  558. * Buffer clear if Zero-Length packet
  559. *
  560. * see
  561. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  562. */
  563. if (0 == rcv_len) {
  564. pkt->zero = 1;
  565. usbhsf_fifo_clear(pipe, fifo);
  566. goto usbhs_fifo_read_end;
  567. }
  568. /*
  569. * FIXME
  570. *
  571. * 32-bit access only
  572. */
  573. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  574. ioread32_rep(addr, buf, len / 4);
  575. len %= 4;
  576. buf += total_len - len;
  577. }
  578. /* the rest operation */
  579. for (i = 0; i < len; i++) {
  580. if (!(i & 0x03))
  581. data = ioread32(addr);
  582. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  583. }
  584. usbhs_fifo_read_end:
  585. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  586. usbhs_pipe_number(pipe),
  587. pkt->length, pkt->actual, *is_done, pkt->zero);
  588. usbhs_fifo_read_busy:
  589. usbhsf_fifo_unselect(pipe, fifo);
  590. return ret;
  591. }
  592. const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  593. .prepare = usbhsf_prepare_pop,
  594. .try_run = usbhsf_pio_try_pop,
  595. };
  596. /*
  597. * DCP ctrol statge handler
  598. */
  599. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  600. {
  601. usbhs_dcp_control_transfer_done(pkt->pipe);
  602. *is_done = 1;
  603. return 0;
  604. }
  605. const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  606. .prepare = usbhsf_ctrl_stage_end,
  607. .try_run = usbhsf_ctrl_stage_end,
  608. };
  609. /*
  610. * DMA fifo functions
  611. */
  612. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  613. struct usbhs_pkt *pkt)
  614. {
  615. if (&usbhs_fifo_dma_push_handler == pkt->handler)
  616. return fifo->tx_chan;
  617. if (&usbhs_fifo_dma_pop_handler == pkt->handler)
  618. return fifo->rx_chan;
  619. return NULL;
  620. }
  621. static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
  622. struct usbhs_pkt *pkt)
  623. {
  624. struct usbhs_fifo *fifo;
  625. int i;
  626. usbhs_for_each_dfifo(priv, fifo, i) {
  627. if (usbhsf_dma_chan_get(fifo, pkt) &&
  628. !usbhsf_fifo_is_busy(fifo))
  629. return fifo;
  630. }
  631. return NULL;
  632. }
  633. #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
  634. #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
  635. static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
  636. struct usbhs_fifo *fifo,
  637. u16 dreqe)
  638. {
  639. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  640. usbhs_bset(priv, fifo->sel, DREQE, dreqe);
  641. }
  642. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  643. {
  644. struct usbhs_pipe *pipe = pkt->pipe;
  645. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  646. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  647. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  648. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  649. return info->dma_map_ctrl(chan->device->dev, pkt, map);
  650. }
  651. static void usbhsf_dma_complete(void *arg);
  652. static void xfer_work(struct work_struct *work)
  653. {
  654. struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
  655. struct usbhs_pipe *pipe = pkt->pipe;
  656. struct usbhs_fifo *fifo;
  657. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  658. struct dma_async_tx_descriptor *desc;
  659. struct dma_chan *chan;
  660. struct device *dev = usbhs_priv_to_dev(priv);
  661. enum dma_transfer_direction dir;
  662. unsigned long flags;
  663. usbhs_lock(priv, flags);
  664. fifo = usbhs_pipe_to_fifo(pipe);
  665. if (!fifo)
  666. goto xfer_work_end;
  667. chan = usbhsf_dma_chan_get(fifo, pkt);
  668. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  669. desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
  670. pkt->trans, dir,
  671. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  672. if (!desc)
  673. goto xfer_work_end;
  674. desc->callback = usbhsf_dma_complete;
  675. desc->callback_param = pipe;
  676. pkt->cookie = dmaengine_submit(desc);
  677. if (pkt->cookie < 0) {
  678. dev_err(dev, "Failed to submit dma descriptor\n");
  679. goto xfer_work_end;
  680. }
  681. dev_dbg(dev, " %s %d (%d/ %d)\n",
  682. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  683. usbhs_pipe_running(pipe, 1);
  684. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
  685. dma_async_issue_pending(chan);
  686. usbhsf_dma_start(pipe, fifo);
  687. usbhs_pipe_enable(pipe);
  688. xfer_work_end:
  689. usbhs_unlock(priv, flags);
  690. }
  691. /*
  692. * DMA push handler
  693. */
  694. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  695. {
  696. struct usbhs_pipe *pipe = pkt->pipe;
  697. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  698. struct usbhs_fifo *fifo;
  699. int len = pkt->length - pkt->actual;
  700. int ret;
  701. uintptr_t align_mask;
  702. if (usbhs_pipe_is_busy(pipe))
  703. return 0;
  704. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  705. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  706. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  707. goto usbhsf_pio_prepare_push;
  708. /* check data length if this driver don't use USB-DMAC */
  709. if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
  710. goto usbhsf_pio_prepare_push;
  711. /* check buffer alignment */
  712. align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
  713. USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
  714. if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
  715. goto usbhsf_pio_prepare_push;
  716. /* return at this time if the pipe is running */
  717. if (usbhs_pipe_is_running(pipe))
  718. return 0;
  719. /* get enable DMA fifo */
  720. fifo = usbhsf_get_dma_fifo(priv, pkt);
  721. if (!fifo)
  722. goto usbhsf_pio_prepare_push;
  723. ret = usbhsf_fifo_select(pipe, fifo, 0);
  724. if (ret < 0)
  725. goto usbhsf_pio_prepare_push;
  726. if (usbhsf_dma_map(pkt) < 0)
  727. goto usbhsf_pio_prepare_push_unselect;
  728. pkt->trans = len;
  729. usbhsf_tx_irq_ctrl(pipe, 0);
  730. INIT_WORK(&pkt->work, xfer_work);
  731. schedule_work(&pkt->work);
  732. return 0;
  733. usbhsf_pio_prepare_push_unselect:
  734. usbhsf_fifo_unselect(pipe, fifo);
  735. usbhsf_pio_prepare_push:
  736. /*
  737. * change handler to PIO
  738. */
  739. pkt->handler = &usbhs_fifo_pio_push_handler;
  740. return pkt->handler->prepare(pkt, is_done);
  741. }
  742. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  743. {
  744. struct usbhs_pipe *pipe = pkt->pipe;
  745. int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
  746. pkt->actual += pkt->trans;
  747. if (pkt->actual < pkt->length)
  748. *is_done = 0; /* there are remainder data */
  749. else if (is_short)
  750. *is_done = 1; /* short packet */
  751. else
  752. *is_done = !pkt->zero; /* send zero packet? */
  753. usbhs_pipe_running(pipe, !*is_done);
  754. usbhsf_dma_stop(pipe, pipe->fifo);
  755. usbhsf_dma_unmap(pkt);
  756. usbhsf_fifo_unselect(pipe, pipe->fifo);
  757. if (!*is_done) {
  758. /* change handler to PIO */
  759. pkt->handler = &usbhs_fifo_pio_push_handler;
  760. return pkt->handler->try_run(pkt, is_done);
  761. }
  762. return 0;
  763. }
  764. const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  765. .prepare = usbhsf_dma_prepare_push,
  766. .dma_done = usbhsf_dma_push_done,
  767. };
  768. /*
  769. * DMA pop handler
  770. */
  771. static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
  772. int *is_done)
  773. {
  774. return usbhsf_prepare_pop(pkt, is_done);
  775. }
  776. static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
  777. int *is_done)
  778. {
  779. struct usbhs_pipe *pipe = pkt->pipe;
  780. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  781. struct usbhs_fifo *fifo;
  782. int ret;
  783. if (usbhs_pipe_is_busy(pipe))
  784. return 0;
  785. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  786. if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
  787. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  788. goto usbhsf_pio_prepare_pop;
  789. fifo = usbhsf_get_dma_fifo(priv, pkt);
  790. if (!fifo)
  791. goto usbhsf_pio_prepare_pop;
  792. if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
  793. goto usbhsf_pio_prepare_pop;
  794. /* return at this time if the pipe is running */
  795. if (usbhs_pipe_is_running(pipe))
  796. return 0;
  797. usbhs_pipe_config_change_bfre(pipe, 1);
  798. ret = usbhsf_fifo_select(pipe, fifo, 0);
  799. if (ret < 0)
  800. goto usbhsf_pio_prepare_pop;
  801. if (usbhsf_dma_map(pkt) < 0)
  802. goto usbhsf_pio_prepare_pop_unselect;
  803. /* DMA */
  804. /*
  805. * usbhs_fifo_dma_pop_handler :: prepare
  806. * enabled irq to come here.
  807. * but it is no longer needed for DMA. disable it.
  808. */
  809. usbhsf_rx_irq_ctrl(pipe, 0);
  810. pkt->trans = pkt->length;
  811. INIT_WORK(&pkt->work, xfer_work);
  812. schedule_work(&pkt->work);
  813. return 0;
  814. usbhsf_pio_prepare_pop_unselect:
  815. usbhsf_fifo_unselect(pipe, fifo);
  816. usbhsf_pio_prepare_pop:
  817. /*
  818. * change handler to PIO
  819. */
  820. pkt->handler = &usbhs_fifo_pio_pop_handler;
  821. usbhs_pipe_config_change_bfre(pipe, 0);
  822. return pkt->handler->prepare(pkt, is_done);
  823. }
  824. static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  825. {
  826. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  827. if (usbhs_get_dparam(priv, has_usb_dmac))
  828. return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
  829. else
  830. return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
  831. }
  832. static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
  833. {
  834. struct usbhs_pipe *pipe = pkt->pipe;
  835. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  836. struct usbhs_fifo *fifo;
  837. int len, ret;
  838. if (usbhs_pipe_is_busy(pipe))
  839. return 0;
  840. if (usbhs_pipe_is_dcp(pipe))
  841. goto usbhsf_pio_prepare_pop;
  842. /* get enable DMA fifo */
  843. fifo = usbhsf_get_dma_fifo(priv, pkt);
  844. if (!fifo)
  845. goto usbhsf_pio_prepare_pop;
  846. if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  847. goto usbhsf_pio_prepare_pop;
  848. ret = usbhsf_fifo_select(pipe, fifo, 0);
  849. if (ret < 0)
  850. goto usbhsf_pio_prepare_pop;
  851. /* use PIO if packet is less than pio_dma_border */
  852. len = usbhsf_fifo_rcv_len(priv, fifo);
  853. len = min(pkt->length - pkt->actual, len);
  854. if (len & 0x7) /* 8byte alignment */
  855. goto usbhsf_pio_prepare_pop_unselect;
  856. if (len < usbhs_get_dparam(priv, pio_dma_border))
  857. goto usbhsf_pio_prepare_pop_unselect;
  858. ret = usbhsf_fifo_barrier(priv, fifo);
  859. if (ret < 0)
  860. goto usbhsf_pio_prepare_pop_unselect;
  861. if (usbhsf_dma_map(pkt) < 0)
  862. goto usbhsf_pio_prepare_pop_unselect;
  863. /* DMA */
  864. /*
  865. * usbhs_fifo_dma_pop_handler :: prepare
  866. * enabled irq to come here.
  867. * but it is no longer needed for DMA. disable it.
  868. */
  869. usbhsf_rx_irq_ctrl(pipe, 0);
  870. pkt->trans = len;
  871. INIT_WORK(&pkt->work, xfer_work);
  872. schedule_work(&pkt->work);
  873. return 0;
  874. usbhsf_pio_prepare_pop_unselect:
  875. usbhsf_fifo_unselect(pipe, fifo);
  876. usbhsf_pio_prepare_pop:
  877. /*
  878. * change handler to PIO
  879. */
  880. pkt->handler = &usbhs_fifo_pio_pop_handler;
  881. return pkt->handler->try_run(pkt, is_done);
  882. }
  883. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  884. {
  885. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  886. BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
  887. return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
  888. }
  889. static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
  890. {
  891. struct usbhs_pipe *pipe = pkt->pipe;
  892. int maxp = usbhs_pipe_get_maxpacket(pipe);
  893. usbhsf_dma_stop(pipe, pipe->fifo);
  894. usbhsf_dma_unmap(pkt);
  895. usbhsf_fifo_unselect(pipe, pipe->fifo);
  896. pkt->actual += pkt->trans;
  897. if ((pkt->actual == pkt->length) || /* receive all data */
  898. (pkt->trans < maxp)) { /* short packet */
  899. *is_done = 1;
  900. usbhs_pipe_running(pipe, 0);
  901. } else {
  902. /* re-enable */
  903. usbhs_pipe_running(pipe, 0);
  904. usbhsf_prepare_pop(pkt, is_done);
  905. }
  906. return 0;
  907. }
  908. static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
  909. struct dma_chan *chan, int dtln)
  910. {
  911. struct usbhs_pipe *pipe = pkt->pipe;
  912. struct dma_tx_state state;
  913. size_t received_size;
  914. int maxp = usbhs_pipe_get_maxpacket(pipe);
  915. dmaengine_tx_status(chan, pkt->cookie, &state);
  916. received_size = pkt->length - state.residue;
  917. if (dtln) {
  918. received_size -= USBHS_USB_DMAC_XFER_SIZE;
  919. received_size &= ~(maxp - 1);
  920. received_size += dtln;
  921. }
  922. return received_size;
  923. }
  924. static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
  925. int *is_done)
  926. {
  927. struct usbhs_pipe *pipe = pkt->pipe;
  928. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  929. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  930. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  931. int rcv_len;
  932. /*
  933. * Since the driver disables rx_irq in DMA mode, the interrupt handler
  934. * cannot the BRDYSTS. So, the function clears it here because the
  935. * driver may use PIO mode next time.
  936. */
  937. usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
  938. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  939. usbhsf_fifo_clear(pipe, fifo);
  940. pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
  941. usbhs_pipe_running(pipe, 0);
  942. usbhsf_dma_stop(pipe, fifo);
  943. usbhsf_dma_unmap(pkt);
  944. usbhsf_fifo_unselect(pipe, pipe->fifo);
  945. /* The driver can assume the rx transaction is always "done" */
  946. *is_done = 1;
  947. return 0;
  948. }
  949. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  950. {
  951. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  952. if (usbhs_get_dparam(priv, has_usb_dmac))
  953. return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
  954. else
  955. return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
  956. }
  957. const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  958. .prepare = usbhsf_dma_prepare_pop,
  959. .try_run = usbhsf_dma_try_pop,
  960. .dma_done = usbhsf_dma_pop_done
  961. };
  962. /*
  963. * DMA setting
  964. */
  965. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  966. {
  967. struct sh_dmae_slave *slave = param;
  968. /*
  969. * FIXME
  970. *
  971. * usbhs doesn't recognize id = 0 as valid DMA
  972. */
  973. if (0 == slave->shdma_slave.slave_id)
  974. return false;
  975. chan->private = slave;
  976. return true;
  977. }
  978. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  979. {
  980. if (fifo->tx_chan)
  981. dma_release_channel(fifo->tx_chan);
  982. if (fifo->rx_chan)
  983. dma_release_channel(fifo->rx_chan);
  984. fifo->tx_chan = NULL;
  985. fifo->rx_chan = NULL;
  986. }
  987. static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
  988. {
  989. dma_cap_mask_t mask;
  990. dma_cap_zero(mask);
  991. dma_cap_set(DMA_SLAVE, mask);
  992. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  993. &fifo->tx_slave);
  994. dma_cap_zero(mask);
  995. dma_cap_set(DMA_SLAVE, mask);
  996. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  997. &fifo->rx_slave);
  998. }
  999. static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
  1000. int channel)
  1001. {
  1002. char name[16];
  1003. /*
  1004. * To avoid complex handing for DnFIFOs, the driver uses each
  1005. * DnFIFO as TX or RX direction (not bi-direction).
  1006. * So, the driver uses odd channels for TX, even channels for RX.
  1007. */
  1008. snprintf(name, sizeof(name), "ch%d", channel);
  1009. if (channel & 1) {
  1010. fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
  1011. if (IS_ERR(fifo->tx_chan))
  1012. fifo->tx_chan = NULL;
  1013. } else {
  1014. fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
  1015. if (IS_ERR(fifo->rx_chan))
  1016. fifo->rx_chan = NULL;
  1017. }
  1018. }
  1019. static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
  1020. int channel)
  1021. {
  1022. struct device *dev = usbhs_priv_to_dev(priv);
  1023. if (dev->of_node)
  1024. usbhsf_dma_init_dt(dev, fifo, channel);
  1025. else
  1026. usbhsf_dma_init_pdev(fifo);
  1027. if (fifo->tx_chan || fifo->rx_chan)
  1028. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  1029. fifo->name,
  1030. fifo->tx_chan ? "[TX]" : " ",
  1031. fifo->rx_chan ? "[RX]" : " ");
  1032. }
  1033. /*
  1034. * irq functions
  1035. */
  1036. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  1037. struct usbhs_irq_state *irq_state)
  1038. {
  1039. struct usbhs_pipe *pipe;
  1040. struct device *dev = usbhs_priv_to_dev(priv);
  1041. int i, ret;
  1042. if (!irq_state->bempsts) {
  1043. dev_err(dev, "debug %s !!\n", __func__);
  1044. return -EIO;
  1045. }
  1046. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  1047. /*
  1048. * search interrupted "pipe"
  1049. * not "uep".
  1050. */
  1051. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  1052. if (!(irq_state->bempsts & (1 << i)))
  1053. continue;
  1054. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  1055. if (ret < 0)
  1056. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  1057. }
  1058. return 0;
  1059. }
  1060. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  1061. struct usbhs_irq_state *irq_state)
  1062. {
  1063. struct usbhs_pipe *pipe;
  1064. struct device *dev = usbhs_priv_to_dev(priv);
  1065. int i, ret;
  1066. if (!irq_state->brdysts) {
  1067. dev_err(dev, "debug %s !!\n", __func__);
  1068. return -EIO;
  1069. }
  1070. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  1071. /*
  1072. * search interrupted "pipe"
  1073. * not "uep".
  1074. */
  1075. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  1076. if (!(irq_state->brdysts & (1 << i)))
  1077. continue;
  1078. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  1079. if (ret < 0)
  1080. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  1081. }
  1082. return 0;
  1083. }
  1084. static void usbhsf_dma_complete(void *arg)
  1085. {
  1086. struct usbhs_pipe *pipe = arg;
  1087. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  1088. struct device *dev = usbhs_priv_to_dev(priv);
  1089. int ret;
  1090. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
  1091. if (ret < 0)
  1092. dev_err(dev, "dma_complete run_error %d : %d\n",
  1093. usbhs_pipe_number(pipe), ret);
  1094. }
  1095. void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
  1096. {
  1097. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  1098. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  1099. /* clear DCP FIFO of transmission */
  1100. if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
  1101. return;
  1102. usbhsf_fifo_clear(pipe, fifo);
  1103. usbhsf_fifo_unselect(pipe, fifo);
  1104. /* clear DCP FIFO of reception */
  1105. if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
  1106. return;
  1107. usbhsf_fifo_clear(pipe, fifo);
  1108. usbhsf_fifo_unselect(pipe, fifo);
  1109. }
  1110. /*
  1111. * fifo init
  1112. */
  1113. void usbhs_fifo_init(struct usbhs_priv *priv)
  1114. {
  1115. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1116. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  1117. struct usbhs_fifo *dfifo;
  1118. int i;
  1119. mod->irq_empty = usbhsf_irq_empty;
  1120. mod->irq_ready = usbhsf_irq_ready;
  1121. mod->irq_bempsts = 0;
  1122. mod->irq_brdysts = 0;
  1123. cfifo->pipe = NULL;
  1124. usbhs_for_each_dfifo(priv, dfifo, i)
  1125. dfifo->pipe = NULL;
  1126. }
  1127. void usbhs_fifo_quit(struct usbhs_priv *priv)
  1128. {
  1129. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1130. mod->irq_empty = NULL;
  1131. mod->irq_ready = NULL;
  1132. mod->irq_bempsts = 0;
  1133. mod->irq_brdysts = 0;
  1134. }
  1135. #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
  1136. do { \
  1137. fifo = usbhsf_get_dnfifo(priv, channel); \
  1138. fifo->name = "D"#channel"FIFO"; \
  1139. fifo->port = fifo_port; \
  1140. fifo->sel = D##channel##FIFOSEL; \
  1141. fifo->ctr = D##channel##FIFOCTR; \
  1142. fifo->tx_slave.shdma_slave.slave_id = \
  1143. usbhs_get_dparam(priv, d##channel##_tx_id); \
  1144. fifo->rx_slave.shdma_slave.slave_id = \
  1145. usbhs_get_dparam(priv, d##channel##_rx_id); \
  1146. usbhsf_dma_init(priv, fifo, channel); \
  1147. } while (0)
  1148. #define USBHS_DFIFO_INIT(priv, fifo, channel) \
  1149. __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
  1150. #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
  1151. __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
  1152. int usbhs_fifo_probe(struct usbhs_priv *priv)
  1153. {
  1154. struct usbhs_fifo *fifo;
  1155. /* CFIFO */
  1156. fifo = usbhsf_get_cfifo(priv);
  1157. fifo->name = "CFIFO";
  1158. fifo->port = CFIFO;
  1159. fifo->sel = CFIFOSEL;
  1160. fifo->ctr = CFIFOCTR;
  1161. /* DFIFO */
  1162. USBHS_DFIFO_INIT(priv, fifo, 0);
  1163. USBHS_DFIFO_INIT(priv, fifo, 1);
  1164. USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
  1165. USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
  1166. return 0;
  1167. }
  1168. void usbhs_fifo_remove(struct usbhs_priv *priv)
  1169. {
  1170. struct usbhs_fifo *fifo;
  1171. int i;
  1172. usbhs_for_each_dfifo(priv, fifo, i)
  1173. usbhsf_dma_quit(priv, fifo);
  1174. }