fifo.c 23 KB

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