fifo.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. struct usbhs_pkt_handle *handler,
  50. void *buf, int len, int zero)
  51. {
  52. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  53. struct device *dev = usbhs_priv_to_dev(priv);
  54. unsigned long flags;
  55. /******************** spin lock ********************/
  56. usbhs_lock(priv, flags);
  57. if (!handler) {
  58. dev_err(dev, "no handler function\n");
  59. handler = &usbhsf_null_handler;
  60. }
  61. list_del_init(&pkt->node);
  62. list_add_tail(&pkt->node, &pipe->list);
  63. pkt->pipe = pipe;
  64. pkt->buf = buf;
  65. pkt->handler = handler;
  66. pkt->length = len;
  67. pkt->zero = zero;
  68. pkt->actual = 0;
  69. usbhs_unlock(priv, flags);
  70. /******************** spin unlock ******************/
  71. usbhs_pkt_start(pipe);
  72. }
  73. static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
  74. {
  75. list_del_init(&pkt->node);
  76. }
  77. static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
  78. {
  79. if (list_empty(&pipe->list))
  80. return NULL;
  81. return list_entry(pipe->list.next, struct usbhs_pkt, node);
  82. }
  83. struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
  84. {
  85. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  86. unsigned long flags;
  87. /******************** spin lock ********************/
  88. usbhs_lock(priv, flags);
  89. if (!pkt)
  90. pkt = __usbhsf_pkt_get(pipe);
  91. if (pkt)
  92. __usbhsf_pkt_del(pkt);
  93. usbhs_unlock(priv, flags);
  94. /******************** spin unlock ******************/
  95. return pkt;
  96. }
  97. int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
  98. {
  99. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  100. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  101. struct usbhs_pkt *pkt;
  102. struct device *dev = usbhs_priv_to_dev(priv);
  103. int (*func)(struct usbhs_pkt *pkt, int *is_done);
  104. unsigned long flags;
  105. int ret = 0;
  106. int is_done = 0;
  107. /******************** spin lock ********************/
  108. usbhs_lock(priv, flags);
  109. pkt = __usbhsf_pkt_get(pipe);
  110. if (!pkt)
  111. goto __usbhs_pkt_handler_end;
  112. switch (type) {
  113. case USBHSF_PKT_PREPARE:
  114. func = pkt->handler->prepare;
  115. break;
  116. case USBHSF_PKT_TRY_RUN:
  117. func = pkt->handler->try_run;
  118. break;
  119. case USBHSF_PKT_DMA_DONE:
  120. func = pkt->handler->dma_done;
  121. break;
  122. default:
  123. dev_err(dev, "unknown pkt hander\n");
  124. goto __usbhs_pkt_handler_end;
  125. }
  126. ret = func(pkt, &is_done);
  127. if (is_done)
  128. __usbhsf_pkt_del(pkt);
  129. __usbhs_pkt_handler_end:
  130. usbhs_unlock(priv, flags);
  131. /******************** spin unlock ******************/
  132. if (is_done) {
  133. info->done(pkt);
  134. usbhs_pkt_start(pipe);
  135. }
  136. return ret;
  137. }
  138. /*
  139. * irq enable/disable function
  140. */
  141. #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
  142. #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
  143. #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
  144. ({ \
  145. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
  146. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  147. u16 status = (1 << usbhs_pipe_number(pipe)); \
  148. if (!mod) \
  149. return; \
  150. if (enable) \
  151. mod->irq_##status |= status; \
  152. else \
  153. mod->irq_##status &= ~status; \
  154. usbhs_irq_callback_update(priv, mod); \
  155. })
  156. static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  157. {
  158. /*
  159. * And DCP pipe can NOT use "ready interrupt" for "send"
  160. * it should use "empty" interrupt.
  161. * see
  162. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  163. *
  164. * on the other hand, normal pipe can use "ready interrupt" for "send"
  165. * even though it is single/double buffer
  166. */
  167. if (usbhs_pipe_is_dcp(pipe))
  168. usbhsf_irq_empty_ctrl(pipe, enable);
  169. else
  170. usbhsf_irq_ready_ctrl(pipe, enable);
  171. }
  172. static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  173. {
  174. usbhsf_irq_ready_ctrl(pipe, enable);
  175. }
  176. /*
  177. * FIFO ctrl
  178. */
  179. static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
  180. struct usbhs_fifo *fifo)
  181. {
  182. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  183. usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
  184. }
  185. static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
  186. struct usbhs_fifo *fifo)
  187. {
  188. int timeout = 1024;
  189. do {
  190. /* The FIFO port is accessible */
  191. if (usbhs_read(priv, fifo->ctr) & FRDY)
  192. return 0;
  193. udelay(10);
  194. } while (timeout--);
  195. return -EBUSY;
  196. }
  197. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
  198. struct usbhs_fifo *fifo)
  199. {
  200. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  201. if (!usbhs_pipe_is_dcp(pipe))
  202. usbhsf_fifo_barrier(priv, fifo);
  203. usbhs_write(priv, fifo->ctr, BCLR);
  204. }
  205. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
  206. struct usbhs_fifo *fifo)
  207. {
  208. return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
  209. }
  210. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  211. struct usbhs_fifo *fifo)
  212. {
  213. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  214. usbhs_pipe_select_fifo(pipe, NULL);
  215. usbhs_write(priv, fifo->sel, 0);
  216. }
  217. static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
  218. struct usbhs_fifo *fifo,
  219. int write)
  220. {
  221. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  222. struct device *dev = usbhs_priv_to_dev(priv);
  223. int timeout = 1024;
  224. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  225. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  226. if (usbhs_pipe_is_busy(pipe) ||
  227. usbhsf_fifo_is_busy(fifo))
  228. return -EBUSY;
  229. if (usbhs_pipe_is_dcp(pipe))
  230. base |= (1 == write) << 5; /* ISEL */
  231. /* "base" will be used below */
  232. usbhs_write(priv, fifo->sel, base | MBW_32);
  233. /* check ISEL and CURPIPE value */
  234. while (timeout--) {
  235. if (base == (mask & usbhs_read(priv, fifo->sel))) {
  236. usbhs_pipe_select_fifo(pipe, fifo);
  237. return 0;
  238. }
  239. udelay(10);
  240. }
  241. dev_err(dev, "fifo select error\n");
  242. return -EIO;
  243. }
  244. /*
  245. * PIO push handler
  246. */
  247. static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
  248. {
  249. struct usbhs_pipe *pipe = pkt->pipe;
  250. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  251. struct device *dev = usbhs_priv_to_dev(priv);
  252. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  253. void __iomem *addr = priv->base + fifo->port;
  254. u8 *buf;
  255. int maxp = usbhs_pipe_get_maxpacket(pipe);
  256. int total_len;
  257. int i, ret, len;
  258. int is_short;
  259. ret = usbhsf_fifo_select(pipe, fifo, 1);
  260. if (ret < 0)
  261. return 0;
  262. ret = usbhs_pipe_is_accessible(pipe);
  263. if (ret < 0) {
  264. /* inaccessible pipe is not an error */
  265. ret = 0;
  266. goto usbhs_fifo_write_busy;
  267. }
  268. ret = usbhsf_fifo_barrier(priv, fifo);
  269. if (ret < 0)
  270. goto usbhs_fifo_write_busy;
  271. buf = pkt->buf + pkt->actual;
  272. len = pkt->length - pkt->actual;
  273. len = min(len, maxp);
  274. total_len = len;
  275. is_short = total_len < maxp;
  276. /*
  277. * FIXME
  278. *
  279. * 32-bit access only
  280. */
  281. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  282. iowrite32_rep(addr, buf, len / 4);
  283. len %= 4;
  284. buf += total_len - len;
  285. }
  286. /* the rest operation */
  287. for (i = 0; i < len; i++)
  288. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  289. /*
  290. * variable update
  291. */
  292. pkt->actual += total_len;
  293. if (pkt->actual < pkt->length)
  294. *is_done = 0; /* there are remainder data */
  295. else if (is_short)
  296. *is_done = 1; /* short packet */
  297. else
  298. *is_done = !pkt->zero; /* send zero packet ? */
  299. /*
  300. * pipe/irq handling
  301. */
  302. if (is_short)
  303. usbhsf_send_terminator(pipe, fifo);
  304. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  305. usbhs_pipe_enable(pipe);
  306. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  307. usbhs_pipe_number(pipe),
  308. pkt->length, pkt->actual, *is_done, pkt->zero);
  309. /*
  310. * Transmission end
  311. */
  312. if (*is_done) {
  313. if (usbhs_pipe_is_dcp(pipe))
  314. usbhs_dcp_control_transfer_done(pipe);
  315. }
  316. usbhsf_fifo_unselect(pipe, fifo);
  317. return 0;
  318. usbhs_fifo_write_busy:
  319. usbhsf_fifo_unselect(pipe, fifo);
  320. /*
  321. * pipe is busy.
  322. * retry in interrupt
  323. */
  324. usbhsf_tx_irq_ctrl(pipe, 1);
  325. return ret;
  326. }
  327. struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  328. .prepare = usbhsf_pio_try_push,
  329. .try_run = usbhsf_pio_try_push,
  330. };
  331. /*
  332. * PIO pop handler
  333. */
  334. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  335. {
  336. struct usbhs_pipe *pipe = pkt->pipe;
  337. if (usbhs_pipe_is_busy(pipe))
  338. return 0;
  339. /*
  340. * pipe enable to prepare packet receive
  341. */
  342. usbhs_pipe_enable(pipe);
  343. usbhsf_rx_irq_ctrl(pipe, 1);
  344. return 0;
  345. }
  346. static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
  347. {
  348. struct usbhs_pipe *pipe = pkt->pipe;
  349. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  350. struct device *dev = usbhs_priv_to_dev(priv);
  351. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  352. void __iomem *addr = priv->base + fifo->port;
  353. u8 *buf;
  354. u32 data = 0;
  355. int maxp = usbhs_pipe_get_maxpacket(pipe);
  356. int rcv_len, len;
  357. int i, ret;
  358. int total_len = 0;
  359. ret = usbhsf_fifo_select(pipe, fifo, 0);
  360. if (ret < 0)
  361. return 0;
  362. ret = usbhsf_fifo_barrier(priv, fifo);
  363. if (ret < 0)
  364. goto usbhs_fifo_read_busy;
  365. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  366. buf = pkt->buf + pkt->actual;
  367. len = pkt->length - pkt->actual;
  368. len = min(len, rcv_len);
  369. total_len = len;
  370. /*
  371. * Buffer clear if Zero-Length packet
  372. *
  373. * see
  374. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  375. */
  376. if (0 == rcv_len) {
  377. usbhsf_fifo_clear(pipe, fifo);
  378. goto usbhs_fifo_read_end;
  379. }
  380. /*
  381. * FIXME
  382. *
  383. * 32-bit access only
  384. */
  385. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  386. ioread32_rep(addr, buf, len / 4);
  387. len %= 4;
  388. buf += total_len - len;
  389. }
  390. /* the rest operation */
  391. for (i = 0; i < len; i++) {
  392. if (!(i & 0x03))
  393. data = ioread32(addr);
  394. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  395. }
  396. pkt->actual += total_len;
  397. usbhs_fifo_read_end:
  398. if ((pkt->actual == pkt->length) || /* receive all data */
  399. (total_len < maxp)) { /* short packet */
  400. *is_done = 1;
  401. usbhsf_rx_irq_ctrl(pipe, 0);
  402. usbhs_pipe_disable(pipe);
  403. }
  404. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  405. usbhs_pipe_number(pipe),
  406. pkt->length, pkt->actual, *is_done, pkt->zero);
  407. usbhs_fifo_read_busy:
  408. usbhsf_fifo_unselect(pipe, fifo);
  409. return ret;
  410. }
  411. struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  412. .prepare = usbhsf_prepare_pop,
  413. .try_run = usbhsf_pio_try_pop,
  414. };
  415. /*
  416. * DCP ctrol statge handler
  417. */
  418. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  419. {
  420. usbhs_dcp_control_transfer_done(pkt->pipe);
  421. *is_done = 1;
  422. return 0;
  423. }
  424. struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  425. .prepare = usbhsf_ctrl_stage_end,
  426. .try_run = usbhsf_ctrl_stage_end,
  427. };
  428. /*
  429. * DMA fifo functions
  430. */
  431. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  432. struct usbhs_pkt *pkt)
  433. {
  434. if (&usbhs_fifo_dma_push_handler == pkt->handler)
  435. return fifo->tx_chan;
  436. if (&usbhs_fifo_dma_pop_handler == pkt->handler)
  437. return fifo->rx_chan;
  438. return NULL;
  439. }
  440. static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
  441. struct usbhs_pkt *pkt)
  442. {
  443. struct usbhs_fifo *fifo;
  444. /* DMA :: D0FIFO */
  445. fifo = usbhsf_get_d0fifo(priv);
  446. if (usbhsf_dma_chan_get(fifo, pkt) &&
  447. !usbhsf_fifo_is_busy(fifo))
  448. return fifo;
  449. /* DMA :: D1FIFO */
  450. fifo = usbhsf_get_d1fifo(priv);
  451. if (usbhsf_dma_chan_get(fifo, pkt) &&
  452. !usbhsf_fifo_is_busy(fifo))
  453. return fifo;
  454. return NULL;
  455. }
  456. #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
  457. #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
  458. static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
  459. struct usbhs_fifo *fifo,
  460. u16 dreqe)
  461. {
  462. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  463. usbhs_bset(priv, fifo->sel, DREQE, dreqe);
  464. }
  465. #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
  466. #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
  467. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  468. {
  469. struct usbhs_pipe *pipe = pkt->pipe;
  470. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  471. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  472. return info->dma_map_ctrl(pkt, map);
  473. }
  474. static void usbhsf_dma_complete(void *arg);
  475. static void usbhsf_dma_prepare_tasklet(unsigned long data)
  476. {
  477. struct usbhs_pkt *pkt = (struct usbhs_pkt *)data;
  478. struct usbhs_pipe *pipe = pkt->pipe;
  479. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  480. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  481. struct scatterlist sg;
  482. struct dma_async_tx_descriptor *desc;
  483. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  484. struct device *dev = usbhs_priv_to_dev(priv);
  485. enum dma_data_direction dir;
  486. dma_cookie_t cookie;
  487. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  488. sg_init_table(&sg, 1);
  489. sg_set_page(&sg, virt_to_page(pkt->dma),
  490. pkt->length, offset_in_page(pkt->dma));
  491. sg_dma_address(&sg) = pkt->dma + pkt->actual;
  492. sg_dma_len(&sg) = pkt->trans;
  493. desc = chan->device->device_prep_slave_sg(chan, &sg, 1, dir,
  494. DMA_PREP_INTERRUPT |
  495. DMA_CTRL_ACK);
  496. if (!desc)
  497. return;
  498. desc->callback = usbhsf_dma_complete;
  499. desc->callback_param = pipe;
  500. cookie = desc->tx_submit(desc);
  501. if (cookie < 0) {
  502. dev_err(dev, "Failed to submit dma descriptor\n");
  503. return;
  504. }
  505. dev_dbg(dev, " %s %d (%d/ %d)\n",
  506. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  507. usbhsf_dma_start(pipe, fifo);
  508. dma_async_issue_pending(chan);
  509. }
  510. /*
  511. * DMA push handler
  512. */
  513. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  514. {
  515. struct usbhs_pipe *pipe = pkt->pipe;
  516. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  517. struct usbhs_fifo *fifo;
  518. int len = pkt->length - pkt->actual;
  519. int ret;
  520. if (usbhs_pipe_is_busy(pipe))
  521. return 0;
  522. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  523. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  524. usbhs_pipe_is_dcp(pipe))
  525. goto usbhsf_pio_prepare_push;
  526. if (len % 4) /* 32bit alignment */
  527. goto usbhsf_pio_prepare_push;
  528. if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  529. goto usbhsf_pio_prepare_push;
  530. /* get enable DMA fifo */
  531. fifo = usbhsf_get_dma_fifo(priv, pkt);
  532. if (!fifo)
  533. goto usbhsf_pio_prepare_push;
  534. if (usbhsf_dma_map(pkt) < 0)
  535. goto usbhsf_pio_prepare_push;
  536. ret = usbhsf_fifo_select(pipe, fifo, 0);
  537. if (ret < 0)
  538. goto usbhsf_pio_prepare_push_unmap;
  539. pkt->trans = len;
  540. tasklet_init(&fifo->tasklet,
  541. usbhsf_dma_prepare_tasklet,
  542. (unsigned long)pkt);
  543. tasklet_schedule(&fifo->tasklet);
  544. return 0;
  545. usbhsf_pio_prepare_push_unmap:
  546. usbhsf_dma_unmap(pkt);
  547. usbhsf_pio_prepare_push:
  548. /*
  549. * change handler to PIO
  550. */
  551. pkt->handler = &usbhs_fifo_pio_push_handler;
  552. return pkt->handler->prepare(pkt, is_done);
  553. }
  554. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  555. {
  556. struct usbhs_pipe *pipe = pkt->pipe;
  557. pkt->actual = pkt->trans;
  558. *is_done = !pkt->zero; /* send zero packet ? */
  559. usbhsf_dma_stop(pipe, pipe->fifo);
  560. usbhsf_dma_unmap(pkt);
  561. usbhsf_fifo_unselect(pipe, pipe->fifo);
  562. return 0;
  563. }
  564. struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  565. .prepare = usbhsf_dma_prepare_push,
  566. .dma_done = usbhsf_dma_push_done,
  567. };
  568. /*
  569. * DMA pop handler
  570. */
  571. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  572. {
  573. struct usbhs_pipe *pipe = pkt->pipe;
  574. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  575. struct usbhs_fifo *fifo;
  576. int len, ret;
  577. if (usbhs_pipe_is_busy(pipe))
  578. return 0;
  579. if (usbhs_pipe_is_dcp(pipe))
  580. goto usbhsf_pio_prepare_pop;
  581. /* get enable DMA fifo */
  582. fifo = usbhsf_get_dma_fifo(priv, pkt);
  583. if (!fifo)
  584. goto usbhsf_pio_prepare_pop;
  585. if (((u32)pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  586. goto usbhsf_pio_prepare_pop;
  587. ret = usbhsf_fifo_select(pipe, fifo, 0);
  588. if (ret < 0)
  589. goto usbhsf_pio_prepare_pop;
  590. /* use PIO if packet is less than pio_dma_border */
  591. len = usbhsf_fifo_rcv_len(priv, fifo);
  592. len = min(pkt->length - pkt->actual, len);
  593. if (len % 4) /* 32bit alignment */
  594. goto usbhsf_pio_prepare_pop_unselect;
  595. if (len < usbhs_get_dparam(priv, pio_dma_border))
  596. goto usbhsf_pio_prepare_pop_unselect;
  597. ret = usbhsf_fifo_barrier(priv, fifo);
  598. if (ret < 0)
  599. goto usbhsf_pio_prepare_pop_unselect;
  600. if (usbhsf_dma_map(pkt) < 0)
  601. goto usbhsf_pio_prepare_pop_unselect;
  602. /* DMA */
  603. /*
  604. * usbhs_fifo_dma_pop_handler :: prepare
  605. * enabled irq to come here.
  606. * but it is no longer needed for DMA. disable it.
  607. */
  608. usbhsf_rx_irq_ctrl(pipe, 0);
  609. pkt->trans = len;
  610. tasklet_init(&fifo->tasklet,
  611. usbhsf_dma_prepare_tasklet,
  612. (unsigned long)pkt);
  613. tasklet_schedule(&fifo->tasklet);
  614. return 0;
  615. usbhsf_pio_prepare_pop_unselect:
  616. usbhsf_fifo_unselect(pipe, fifo);
  617. usbhsf_pio_prepare_pop:
  618. /*
  619. * change handler to PIO
  620. */
  621. pkt->handler = &usbhs_fifo_pio_pop_handler;
  622. return pkt->handler->try_run(pkt, is_done);
  623. }
  624. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  625. {
  626. struct usbhs_pipe *pipe = pkt->pipe;
  627. int maxp = usbhs_pipe_get_maxpacket(pipe);
  628. usbhsf_dma_stop(pipe, pipe->fifo);
  629. usbhsf_dma_unmap(pkt);
  630. usbhsf_fifo_unselect(pipe, pipe->fifo);
  631. pkt->actual += pkt->trans;
  632. if ((pkt->actual == pkt->length) || /* receive all data */
  633. (pkt->trans < maxp)) { /* short packet */
  634. *is_done = 1;
  635. } else {
  636. /* re-enable */
  637. usbhsf_prepare_pop(pkt, is_done);
  638. }
  639. return 0;
  640. }
  641. struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  642. .prepare = usbhsf_prepare_pop,
  643. .try_run = usbhsf_dma_try_pop,
  644. .dma_done = usbhsf_dma_pop_done
  645. };
  646. /*
  647. * DMA setting
  648. */
  649. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  650. {
  651. struct sh_dmae_slave *slave = param;
  652. /*
  653. * FIXME
  654. *
  655. * usbhs doesn't recognize id = 0 as valid DMA
  656. */
  657. if (0 == slave->slave_id)
  658. return false;
  659. chan->private = slave;
  660. return true;
  661. }
  662. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  663. {
  664. if (fifo->tx_chan)
  665. dma_release_channel(fifo->tx_chan);
  666. if (fifo->rx_chan)
  667. dma_release_channel(fifo->rx_chan);
  668. fifo->tx_chan = NULL;
  669. fifo->rx_chan = NULL;
  670. }
  671. static void usbhsf_dma_init(struct usbhs_priv *priv,
  672. struct usbhs_fifo *fifo)
  673. {
  674. struct device *dev = usbhs_priv_to_dev(priv);
  675. dma_cap_mask_t mask;
  676. dma_cap_zero(mask);
  677. dma_cap_set(DMA_SLAVE, mask);
  678. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  679. &fifo->tx_slave);
  680. dma_cap_zero(mask);
  681. dma_cap_set(DMA_SLAVE, mask);
  682. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  683. &fifo->rx_slave);
  684. if (fifo->tx_chan || fifo->rx_chan)
  685. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  686. fifo->name,
  687. fifo->tx_chan ? "[TX]" : " ",
  688. fifo->rx_chan ? "[RX]" : " ");
  689. }
  690. /*
  691. * irq functions
  692. */
  693. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  694. struct usbhs_irq_state *irq_state)
  695. {
  696. struct usbhs_pipe *pipe;
  697. struct device *dev = usbhs_priv_to_dev(priv);
  698. int i, ret;
  699. if (!irq_state->bempsts) {
  700. dev_err(dev, "debug %s !!\n", __func__);
  701. return -EIO;
  702. }
  703. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  704. /*
  705. * search interrupted "pipe"
  706. * not "uep".
  707. */
  708. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  709. if (!(irq_state->bempsts & (1 << i)))
  710. continue;
  711. ret = usbhs_pkt_run(pipe);
  712. if (ret < 0)
  713. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  714. }
  715. return 0;
  716. }
  717. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  718. struct usbhs_irq_state *irq_state)
  719. {
  720. struct usbhs_pipe *pipe;
  721. struct device *dev = usbhs_priv_to_dev(priv);
  722. int i, ret;
  723. if (!irq_state->brdysts) {
  724. dev_err(dev, "debug %s !!\n", __func__);
  725. return -EIO;
  726. }
  727. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  728. /*
  729. * search interrupted "pipe"
  730. * not "uep".
  731. */
  732. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  733. if (!(irq_state->brdysts & (1 << i)))
  734. continue;
  735. ret = usbhs_pkt_run(pipe);
  736. if (ret < 0)
  737. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  738. }
  739. return 0;
  740. }
  741. static void usbhsf_dma_complete(void *arg)
  742. {
  743. struct usbhs_pipe *pipe = arg;
  744. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  745. struct device *dev = usbhs_priv_to_dev(priv);
  746. int ret;
  747. ret = usbhs_pkt_dmadone(pipe);
  748. if (ret < 0)
  749. dev_err(dev, "dma_complete run_error %d : %d\n",
  750. usbhs_pipe_number(pipe), ret);
  751. }
  752. /*
  753. * fifo init
  754. */
  755. void usbhs_fifo_init(struct usbhs_priv *priv)
  756. {
  757. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  758. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  759. struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
  760. struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
  761. mod->irq_empty = usbhsf_irq_empty;
  762. mod->irq_ready = usbhsf_irq_ready;
  763. mod->irq_bempsts = 0;
  764. mod->irq_brdysts = 0;
  765. cfifo->pipe = NULL;
  766. cfifo->tx_chan = NULL;
  767. cfifo->rx_chan = NULL;
  768. d0fifo->pipe = NULL;
  769. d0fifo->tx_chan = NULL;
  770. d0fifo->rx_chan = NULL;
  771. d1fifo->pipe = NULL;
  772. d1fifo->tx_chan = NULL;
  773. d1fifo->rx_chan = NULL;
  774. usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
  775. usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
  776. }
  777. void usbhs_fifo_quit(struct usbhs_priv *priv)
  778. {
  779. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  780. mod->irq_empty = NULL;
  781. mod->irq_ready = NULL;
  782. mod->irq_bempsts = 0;
  783. mod->irq_brdysts = 0;
  784. usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
  785. usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
  786. }
  787. int usbhs_fifo_probe(struct usbhs_priv *priv)
  788. {
  789. struct usbhs_fifo *fifo;
  790. /* CFIFO */
  791. fifo = usbhsf_get_cfifo(priv);
  792. fifo->name = "CFIFO";
  793. fifo->port = CFIFO;
  794. fifo->sel = CFIFOSEL;
  795. fifo->ctr = CFIFOCTR;
  796. /* D0FIFO */
  797. fifo = usbhsf_get_d0fifo(priv);
  798. fifo->name = "D0FIFO";
  799. fifo->port = D0FIFO;
  800. fifo->sel = D0FIFOSEL;
  801. fifo->ctr = D0FIFOCTR;
  802. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
  803. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
  804. /* D1FIFO */
  805. fifo = usbhsf_get_d1fifo(priv);
  806. fifo->name = "D1FIFO";
  807. fifo->port = D1FIFO;
  808. fifo->sel = D1FIFOSEL;
  809. fifo->ctr = D1FIFOCTR;
  810. fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
  811. fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
  812. return 0;
  813. }
  814. void usbhs_fifo_remove(struct usbhs_priv *priv)
  815. {
  816. }