fifo.c 33 KB

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