fifo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 "./common.h"
  20. #include "./pipe.h"
  21. #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
  22. #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
  23. /*
  24. * packet info 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 struct usbhs_pkt_handle usbhsf_null_handler = {
  34. .prepare = usbhsf_null_handle,
  35. .try_run = usbhsf_null_handle,
  36. };
  37. void usbhs_pkt_init(struct usbhs_pkt *pkt)
  38. {
  39. INIT_LIST_HEAD(&pkt->node);
  40. }
  41. void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
  42. struct usbhs_pkt_handle *handler,
  43. void *buf, int len, int zero)
  44. {
  45. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  46. struct device *dev = usbhs_priv_to_dev(priv);
  47. unsigned long flags;
  48. /******************** spin lock ********************/
  49. usbhs_lock(priv, flags);
  50. if (!handler) {
  51. dev_err(dev, "no handler function\n");
  52. handler = &usbhsf_null_handler;
  53. }
  54. list_del_init(&pkt->node);
  55. list_add_tail(&pkt->node, &pipe->list);
  56. pkt->pipe = pipe;
  57. pkt->buf = buf;
  58. pkt->handler = handler;
  59. pkt->length = len;
  60. pkt->zero = zero;
  61. pkt->actual = 0;
  62. usbhs_unlock(priv, flags);
  63. /******************** spin unlock ******************/
  64. usbhs_pkt_start(pipe);
  65. }
  66. static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
  67. {
  68. list_del_init(&pkt->node);
  69. }
  70. static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
  71. {
  72. if (list_empty(&pipe->list))
  73. return NULL;
  74. return list_entry(pipe->list.next, struct usbhs_pkt, node);
  75. }
  76. struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
  77. {
  78. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  79. unsigned long flags;
  80. /******************** spin lock ********************/
  81. usbhs_lock(priv, flags);
  82. if (!pkt)
  83. pkt = __usbhsf_pkt_get(pipe);
  84. if (pkt)
  85. __usbhsf_pkt_del(pkt);
  86. usbhs_unlock(priv, flags);
  87. /******************** spin unlock ******************/
  88. return pkt;
  89. }
  90. int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
  91. {
  92. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  93. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  94. struct usbhs_pkt *pkt;
  95. struct device *dev = usbhs_priv_to_dev(priv);
  96. int (*func)(struct usbhs_pkt *pkt, int *is_done);
  97. unsigned long flags;
  98. int ret = 0;
  99. int is_done = 0;
  100. /******************** spin lock ********************/
  101. usbhs_lock(priv, flags);
  102. pkt = __usbhsf_pkt_get(pipe);
  103. if (!pkt)
  104. goto __usbhs_pkt_handler_end;
  105. switch (type) {
  106. case USBHSF_PKT_PREPARE:
  107. func = pkt->handler->prepare;
  108. break;
  109. case USBHSF_PKT_TRY_RUN:
  110. func = pkt->handler->try_run;
  111. break;
  112. default:
  113. dev_err(dev, "unknown pkt hander\n");
  114. goto __usbhs_pkt_handler_end;
  115. }
  116. ret = func(pkt, &is_done);
  117. if (is_done)
  118. __usbhsf_pkt_del(pkt);
  119. __usbhs_pkt_handler_end:
  120. usbhs_unlock(priv, flags);
  121. /******************** spin unlock ******************/
  122. if (is_done) {
  123. info->done(pkt);
  124. usbhs_pkt_start(pipe);
  125. }
  126. return ret;
  127. }
  128. /*
  129. * irq enable/disable function
  130. */
  131. #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
  132. #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
  133. #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
  134. ({ \
  135. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
  136. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  137. u16 status = (1 << usbhs_pipe_number(pipe)); \
  138. if (!mod) \
  139. return; \
  140. if (enable) \
  141. mod->irq_##status |= status; \
  142. else \
  143. mod->irq_##status &= ~status; \
  144. usbhs_irq_callback_update(priv, mod); \
  145. })
  146. static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  147. {
  148. /*
  149. * And DCP pipe can NOT use "ready interrupt" for "send"
  150. * it should use "empty" interrupt.
  151. * see
  152. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  153. *
  154. * on the other hand, normal pipe can use "ready interrupt" for "send"
  155. * even though it is single/double buffer
  156. */
  157. if (usbhs_pipe_is_dcp(pipe))
  158. usbhsf_irq_empty_ctrl(pipe, enable);
  159. else
  160. usbhsf_irq_ready_ctrl(pipe, enable);
  161. }
  162. static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  163. {
  164. usbhsf_irq_ready_ctrl(pipe, enable);
  165. }
  166. /*
  167. * FIFO ctrl
  168. */
  169. static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
  170. struct usbhs_fifo *fifo)
  171. {
  172. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  173. usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
  174. }
  175. static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
  176. struct usbhs_fifo *fifo)
  177. {
  178. int timeout = 1024;
  179. do {
  180. /* The FIFO port is accessible */
  181. if (usbhs_read(priv, fifo->ctr) & FRDY)
  182. return 0;
  183. udelay(10);
  184. } while (timeout--);
  185. return -EBUSY;
  186. }
  187. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
  188. struct usbhs_fifo *fifo)
  189. {
  190. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  191. if (!usbhs_pipe_is_dcp(pipe))
  192. usbhsf_fifo_barrier(priv, fifo);
  193. usbhs_write(priv, fifo->ctr, BCLR);
  194. }
  195. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
  196. struct usbhs_fifo *fifo)
  197. {
  198. return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
  199. }
  200. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  201. struct usbhs_fifo *fifo)
  202. {
  203. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  204. usbhs_pipe_select_fifo(pipe, NULL);
  205. usbhs_write(priv, fifo->sel, 0);
  206. }
  207. static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
  208. struct usbhs_fifo *fifo,
  209. int write)
  210. {
  211. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  212. struct device *dev = usbhs_priv_to_dev(priv);
  213. int timeout = 1024;
  214. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  215. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  216. if (usbhs_pipe_is_busy(pipe) ||
  217. usbhsf_fifo_is_busy(fifo))
  218. return -EBUSY;
  219. if (usbhs_pipe_is_dcp(pipe))
  220. base |= (1 == write) << 5; /* ISEL */
  221. /* "base" will be used below */
  222. usbhs_write(priv, fifo->sel, base | MBW_32);
  223. /* check ISEL and CURPIPE value */
  224. while (timeout--) {
  225. if (base == (mask & usbhs_read(priv, fifo->sel))) {
  226. usbhs_pipe_select_fifo(pipe, fifo);
  227. return 0;
  228. }
  229. udelay(10);
  230. }
  231. dev_err(dev, "fifo select error\n");
  232. return -EIO;
  233. }
  234. /*
  235. * PIO fifo functions
  236. */
  237. static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
  238. {
  239. struct usbhs_pipe *pipe = pkt->pipe;
  240. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  241. struct device *dev = usbhs_priv_to_dev(priv);
  242. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  243. void __iomem *addr = priv->base + fifo->port;
  244. u8 *buf;
  245. int maxp = usbhs_pipe_get_maxpacket(pipe);
  246. int total_len;
  247. int i, ret, len;
  248. int is_short;
  249. ret = usbhsf_fifo_select(pipe, fifo, 1);
  250. if (ret < 0)
  251. return 0;
  252. ret = usbhs_pipe_is_accessible(pipe);
  253. if (ret < 0)
  254. goto usbhs_fifo_write_busy;
  255. ret = usbhsf_fifo_barrier(priv, fifo);
  256. if (ret < 0)
  257. goto usbhs_fifo_write_busy;
  258. buf = pkt->buf + pkt->actual;
  259. len = pkt->length - pkt->actual;
  260. len = min(len, maxp);
  261. total_len = len;
  262. is_short = total_len < maxp;
  263. /*
  264. * FIXME
  265. *
  266. * 32-bit access only
  267. */
  268. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  269. iowrite32_rep(addr, buf, len / 4);
  270. len %= 4;
  271. buf += total_len - len;
  272. }
  273. /* the rest operation */
  274. for (i = 0; i < len; i++)
  275. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  276. /*
  277. * variable update
  278. */
  279. pkt->actual += total_len;
  280. if (pkt->actual < pkt->length)
  281. *is_done = 0; /* there are remainder data */
  282. else if (is_short)
  283. *is_done = 1; /* short packet */
  284. else
  285. *is_done = !pkt->zero; /* send zero packet ? */
  286. /*
  287. * pipe/irq handling
  288. */
  289. if (is_short)
  290. usbhsf_send_terminator(pipe, fifo);
  291. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  292. usbhs_pipe_enable(pipe);
  293. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  294. usbhs_pipe_number(pipe),
  295. pkt->length, pkt->actual, *is_done, pkt->zero);
  296. /*
  297. * Transmission end
  298. */
  299. if (*is_done) {
  300. if (usbhs_pipe_is_dcp(pipe))
  301. usbhs_dcp_control_transfer_done(pipe);
  302. }
  303. usbhsf_fifo_unselect(pipe, fifo);
  304. return 0;
  305. usbhs_fifo_write_busy:
  306. usbhsf_fifo_unselect(pipe, fifo);
  307. /*
  308. * pipe is busy.
  309. * retry in interrupt
  310. */
  311. usbhsf_tx_irq_ctrl(pipe, 1);
  312. return ret;
  313. }
  314. struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  315. .prepare = usbhsf_pio_try_push,
  316. .try_run = usbhsf_pio_try_push,
  317. };
  318. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  319. {
  320. struct usbhs_pipe *pipe = pkt->pipe;
  321. if (usbhs_pipe_is_busy(pipe))
  322. return 0;
  323. /*
  324. * pipe enable to prepare packet receive
  325. */
  326. usbhs_pipe_enable(pipe);
  327. usbhsf_rx_irq_ctrl(pipe, 1);
  328. return 0;
  329. }
  330. static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
  331. {
  332. struct usbhs_pipe *pipe = pkt->pipe;
  333. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  334. struct device *dev = usbhs_priv_to_dev(priv);
  335. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  336. void __iomem *addr = priv->base + fifo->port;
  337. u8 *buf;
  338. u32 data = 0;
  339. int maxp = usbhs_pipe_get_maxpacket(pipe);
  340. int rcv_len, len;
  341. int i, ret;
  342. int total_len = 0;
  343. ret = usbhsf_fifo_select(pipe, fifo, 0);
  344. if (ret < 0)
  345. return 0;
  346. ret = usbhsf_fifo_barrier(priv, fifo);
  347. if (ret < 0)
  348. goto usbhs_fifo_read_busy;
  349. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  350. buf = pkt->buf + pkt->actual;
  351. len = pkt->length - pkt->actual;
  352. len = min(len, rcv_len);
  353. total_len = len;
  354. /*
  355. * Buffer clear if Zero-Length packet
  356. *
  357. * see
  358. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  359. */
  360. if (0 == rcv_len) {
  361. usbhsf_fifo_clear(pipe, fifo);
  362. goto usbhs_fifo_read_end;
  363. }
  364. /*
  365. * FIXME
  366. *
  367. * 32-bit access only
  368. */
  369. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  370. ioread32_rep(addr, buf, len / 4);
  371. len %= 4;
  372. buf += total_len - len;
  373. }
  374. /* the rest operation */
  375. for (i = 0; i < len; i++) {
  376. if (!(i & 0x03))
  377. data = ioread32(addr);
  378. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  379. }
  380. pkt->actual += total_len;
  381. usbhs_fifo_read_end:
  382. if ((pkt->actual == pkt->length) || /* receive all data */
  383. (total_len < maxp)) { /* short packet */
  384. *is_done = 1;
  385. usbhsf_rx_irq_ctrl(pipe, 0);
  386. usbhs_pipe_disable(pipe);
  387. }
  388. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  389. usbhs_pipe_number(pipe),
  390. pkt->length, pkt->actual, *is_done, pkt->zero);
  391. usbhs_fifo_read_busy:
  392. usbhsf_fifo_unselect(pipe, fifo);
  393. return ret;
  394. }
  395. struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  396. .prepare = usbhsf_prepare_pop,
  397. .try_run = usbhsf_pio_try_pop,
  398. };
  399. /*
  400. * handler function
  401. */
  402. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  403. {
  404. usbhs_dcp_control_transfer_done(pkt->pipe);
  405. *is_done = 1;
  406. return 0;
  407. }
  408. struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  409. .prepare = usbhsf_ctrl_stage_end,
  410. .try_run = usbhsf_ctrl_stage_end,
  411. };
  412. /*
  413. * irq functions
  414. */
  415. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  416. struct usbhs_irq_state *irq_state)
  417. {
  418. struct usbhs_pipe *pipe;
  419. struct device *dev = usbhs_priv_to_dev(priv);
  420. int i, ret;
  421. if (!irq_state->bempsts) {
  422. dev_err(dev, "debug %s !!\n", __func__);
  423. return -EIO;
  424. }
  425. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  426. /*
  427. * search interrupted "pipe"
  428. * not "uep".
  429. */
  430. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  431. if (!(irq_state->bempsts & (1 << i)))
  432. continue;
  433. ret = usbhs_pkt_run(pipe);
  434. if (ret < 0)
  435. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  436. }
  437. return 0;
  438. }
  439. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  440. struct usbhs_irq_state *irq_state)
  441. {
  442. struct usbhs_pipe *pipe;
  443. struct device *dev = usbhs_priv_to_dev(priv);
  444. int i, ret;
  445. if (!irq_state->brdysts) {
  446. dev_err(dev, "debug %s !!\n", __func__);
  447. return -EIO;
  448. }
  449. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  450. /*
  451. * search interrupted "pipe"
  452. * not "uep".
  453. */
  454. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  455. if (!(irq_state->brdysts & (1 << i)))
  456. continue;
  457. ret = usbhs_pkt_run(pipe);
  458. if (ret < 0)
  459. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  460. }
  461. return 0;
  462. }
  463. /*
  464. * fifo init
  465. */
  466. void usbhs_fifo_init(struct usbhs_priv *priv)
  467. {
  468. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  469. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  470. mod->irq_empty = usbhsf_irq_empty;
  471. mod->irq_ready = usbhsf_irq_ready;
  472. mod->irq_bempsts = 0;
  473. mod->irq_brdysts = 0;
  474. cfifo->pipe = NULL;
  475. }
  476. void usbhs_fifo_quit(struct usbhs_priv *priv)
  477. {
  478. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  479. mod->irq_empty = NULL;
  480. mod->irq_ready = NULL;
  481. mod->irq_bempsts = 0;
  482. mod->irq_brdysts = 0;
  483. }
  484. int usbhs_fifo_probe(struct usbhs_priv *priv)
  485. {
  486. struct usbhs_fifo *fifo;
  487. /* CFIFO */
  488. fifo = usbhsf_get_cfifo(priv);
  489. fifo->port = CFIFO;
  490. fifo->sel = CFIFOSEL;
  491. fifo->ctr = CFIFOCTR;
  492. return 0;
  493. }
  494. void usbhs_fifo_remove(struct usbhs_priv *priv)
  495. {
  496. }