fifo.c 27 KB

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