fifo.c 13 KB

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