fifo.c 13 KB

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