fifo.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Renesas USB driver
  4. *
  5. * Copyright (C) 2011 Renesas Solutions Corp.
  6. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/scatterlist.h>
  11. #include "common.h"
  12. #include "pipe.h"
  13. #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
  14. #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
  15. #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
  16. /*
  17. * packet initialize
  18. */
  19. void usbhs_pkt_init(struct usbhs_pkt *pkt)
  20. {
  21. INIT_LIST_HEAD(&pkt->node);
  22. }
  23. /*
  24. * packet control 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 const struct usbhs_pkt_handle usbhsf_null_handler = {
  34. .prepare = usbhsf_null_handle,
  35. .try_run = usbhsf_null_handle,
  36. };
  37. void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
  38. void (*done)(struct usbhs_priv *priv,
  39. struct usbhs_pkt *pkt),
  40. void *buf, int len, int zero, int sequence)
  41. {
  42. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  43. struct device *dev = usbhs_priv_to_dev(priv);
  44. unsigned long flags;
  45. if (!done) {
  46. dev_err(dev, "no done function\n");
  47. return;
  48. }
  49. /******************** spin lock ********************/
  50. usbhs_lock(priv, flags);
  51. if (!pipe->handler) {
  52. dev_err(dev, "no handler function\n");
  53. pipe->handler = &usbhsf_null_handler;
  54. }
  55. list_move_tail(&pkt->node, &pipe->list);
  56. /*
  57. * each pkt must hold own handler.
  58. * because handler might be changed by its situation.
  59. * dma handler -> pio handler.
  60. */
  61. pkt->pipe = pipe;
  62. pkt->buf = buf;
  63. pkt->handler = pipe->handler;
  64. pkt->length = len;
  65. pkt->zero = zero;
  66. pkt->actual = 0;
  67. pkt->done = done;
  68. pkt->sequence = sequence;
  69. usbhs_unlock(priv, flags);
  70. /******************** spin unlock ******************/
  71. }
  72. static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
  73. {
  74. list_del_init(&pkt->node);
  75. }
  76. struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
  77. {
  78. return list_first_entry_or_null(&pipe->list, struct usbhs_pkt, node);
  79. }
  80. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  81. struct usbhs_fifo *fifo);
  82. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  83. struct usbhs_pkt *pkt);
  84. #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
  85. #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
  86. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
  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. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  91. unsigned long flags;
  92. /******************** spin lock ********************/
  93. usbhs_lock(priv, flags);
  94. usbhs_pipe_disable(pipe);
  95. if (!pkt)
  96. pkt = __usbhsf_pkt_get(pipe);
  97. if (pkt) {
  98. struct dma_chan *chan = NULL;
  99. if (fifo)
  100. chan = usbhsf_dma_chan_get(fifo, pkt);
  101. if (chan) {
  102. dmaengine_terminate_all(chan);
  103. usbhsf_dma_unmap(pkt);
  104. }
  105. usbhs_pipe_clear_without_sequence(pipe, 0, 0);
  106. __usbhsf_pkt_del(pkt);
  107. }
  108. if (fifo)
  109. usbhsf_fifo_unselect(pipe, fifo);
  110. usbhs_unlock(priv, flags);
  111. /******************** spin unlock ******************/
  112. return pkt;
  113. }
  114. enum {
  115. USBHSF_PKT_PREPARE,
  116. USBHSF_PKT_TRY_RUN,
  117. USBHSF_PKT_DMA_DONE,
  118. };
  119. static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
  120. {
  121. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  122. struct usbhs_pkt *pkt;
  123. struct device *dev = usbhs_priv_to_dev(priv);
  124. int (*func)(struct usbhs_pkt *pkt, int *is_done);
  125. unsigned long flags;
  126. int ret = 0;
  127. int is_done = 0;
  128. /******************** spin lock ********************/
  129. usbhs_lock(priv, flags);
  130. pkt = __usbhsf_pkt_get(pipe);
  131. if (!pkt)
  132. goto __usbhs_pkt_handler_end;
  133. switch (type) {
  134. case USBHSF_PKT_PREPARE:
  135. func = pkt->handler->prepare;
  136. break;
  137. case USBHSF_PKT_TRY_RUN:
  138. func = pkt->handler->try_run;
  139. break;
  140. case USBHSF_PKT_DMA_DONE:
  141. func = pkt->handler->dma_done;
  142. break;
  143. default:
  144. dev_err(dev, "unknown pkt handler\n");
  145. goto __usbhs_pkt_handler_end;
  146. }
  147. if (likely(func))
  148. ret = func(pkt, &is_done);
  149. if (is_done)
  150. __usbhsf_pkt_del(pkt);
  151. __usbhs_pkt_handler_end:
  152. usbhs_unlock(priv, flags);
  153. /******************** spin unlock ******************/
  154. if (is_done) {
  155. pkt->done(priv, pkt);
  156. usbhs_pkt_start(pipe);
  157. }
  158. return ret;
  159. }
  160. void usbhs_pkt_start(struct usbhs_pipe *pipe)
  161. {
  162. usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
  163. }
  164. /*
  165. * irq enable/disable function
  166. */
  167. #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
  168. #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
  169. #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
  170. ({ \
  171. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
  172. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  173. u16 status = (1 << usbhs_pipe_number(pipe)); \
  174. if (!mod) \
  175. return; \
  176. if (enable) \
  177. mod->status |= status; \
  178. else \
  179. mod->status &= ~status; \
  180. usbhs_irq_callback_update(priv, mod); \
  181. })
  182. static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  183. {
  184. /*
  185. * And DCP pipe can NOT use "ready interrupt" for "send"
  186. * it should use "empty" interrupt.
  187. * see
  188. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  189. *
  190. * on the other hand, normal pipe can use "ready interrupt" for "send"
  191. * even though it is single/double buffer
  192. */
  193. if (usbhs_pipe_is_dcp(pipe))
  194. usbhsf_irq_empty_ctrl(pipe, enable);
  195. else
  196. usbhsf_irq_ready_ctrl(pipe, enable);
  197. }
  198. static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  199. {
  200. usbhsf_irq_ready_ctrl(pipe, enable);
  201. }
  202. /*
  203. * FIFO ctrl
  204. */
  205. static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
  206. struct usbhs_fifo *fifo)
  207. {
  208. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  209. usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
  210. }
  211. static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
  212. struct usbhs_fifo *fifo)
  213. {
  214. /* The FIFO port is accessible */
  215. if (usbhs_read(priv, fifo->ctr) & FRDY)
  216. return 0;
  217. return -EBUSY;
  218. }
  219. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
  220. struct usbhs_fifo *fifo)
  221. {
  222. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  223. int ret = 0;
  224. if (!usbhs_pipe_is_dcp(pipe)) {
  225. /*
  226. * This driver checks the pipe condition first to avoid -EBUSY
  227. * from usbhsf_fifo_barrier() if the pipe is RX direction and
  228. * empty.
  229. */
  230. if (usbhs_pipe_is_dir_in(pipe))
  231. ret = usbhs_pipe_is_accessible(pipe);
  232. if (!ret)
  233. ret = usbhsf_fifo_barrier(priv, fifo);
  234. }
  235. /*
  236. * if non-DCP pipe, this driver should set BCLR when
  237. * usbhsf_fifo_barrier() returns 0.
  238. */
  239. if (!ret)
  240. usbhs_write(priv, fifo->ctr, BCLR);
  241. }
  242. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
  243. struct usbhs_fifo *fifo)
  244. {
  245. return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
  246. }
  247. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  248. struct usbhs_fifo *fifo)
  249. {
  250. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  251. usbhs_pipe_select_fifo(pipe, NULL);
  252. usbhs_write(priv, fifo->sel, 0);
  253. }
  254. static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
  255. struct usbhs_fifo *fifo,
  256. int write)
  257. {
  258. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  259. struct device *dev = usbhs_priv_to_dev(priv);
  260. int timeout = 1024;
  261. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  262. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  263. if (usbhs_pipe_is_busy(pipe) ||
  264. usbhsf_fifo_is_busy(fifo))
  265. return -EBUSY;
  266. if (usbhs_pipe_is_dcp(pipe)) {
  267. base |= (1 == write) << 5; /* ISEL */
  268. if (usbhs_mod_is_host(priv))
  269. usbhs_dcp_dir_for_host(pipe, write);
  270. }
  271. /* "base" will be used below */
  272. if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
  273. usbhs_write(priv, fifo->sel, base);
  274. else
  275. usbhs_write(priv, fifo->sel, base | MBW_32);
  276. /* check ISEL and CURPIPE value */
  277. while (timeout--) {
  278. if (base == (mask & usbhs_read(priv, fifo->sel))) {
  279. usbhs_pipe_select_fifo(pipe, fifo);
  280. return 0;
  281. }
  282. udelay(10);
  283. }
  284. dev_err(dev, "fifo select error\n");
  285. return -EIO;
  286. }
  287. /*
  288. * DCP status stage
  289. */
  290. static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
  291. {
  292. struct usbhs_pipe *pipe = pkt->pipe;
  293. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  294. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  295. struct device *dev = usbhs_priv_to_dev(priv);
  296. int ret;
  297. usbhs_pipe_disable(pipe);
  298. ret = usbhsf_fifo_select(pipe, fifo, 1);
  299. if (ret < 0) {
  300. dev_err(dev, "%s() faile\n", __func__);
  301. return ret;
  302. }
  303. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  304. usbhsf_fifo_clear(pipe, fifo);
  305. usbhsf_send_terminator(pipe, fifo);
  306. usbhsf_fifo_unselect(pipe, fifo);
  307. usbhsf_tx_irq_ctrl(pipe, 1);
  308. usbhs_pipe_enable(pipe);
  309. return ret;
  310. }
  311. static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
  312. {
  313. struct usbhs_pipe *pipe = pkt->pipe;
  314. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  315. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  316. struct device *dev = usbhs_priv_to_dev(priv);
  317. int ret;
  318. usbhs_pipe_disable(pipe);
  319. ret = usbhsf_fifo_select(pipe, fifo, 0);
  320. if (ret < 0) {
  321. dev_err(dev, "%s() fail\n", __func__);
  322. return ret;
  323. }
  324. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  325. usbhsf_fifo_clear(pipe, fifo);
  326. usbhsf_fifo_unselect(pipe, fifo);
  327. usbhsf_rx_irq_ctrl(pipe, 1);
  328. usbhs_pipe_enable(pipe);
  329. return ret;
  330. }
  331. static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
  332. {
  333. struct usbhs_pipe *pipe = pkt->pipe;
  334. if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
  335. usbhsf_tx_irq_ctrl(pipe, 0);
  336. else
  337. usbhsf_rx_irq_ctrl(pipe, 0);
  338. pkt->actual = pkt->length;
  339. *is_done = 1;
  340. return 0;
  341. }
  342. const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
  343. .prepare = usbhs_dcp_dir_switch_to_write,
  344. .try_run = usbhs_dcp_dir_switch_done,
  345. };
  346. const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
  347. .prepare = usbhs_dcp_dir_switch_to_read,
  348. .try_run = usbhs_dcp_dir_switch_done,
  349. };
  350. /*
  351. * DCP data stage (push)
  352. */
  353. static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
  354. {
  355. struct usbhs_pipe *pipe = pkt->pipe;
  356. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  357. /*
  358. * change handler to PIO push
  359. */
  360. pkt->handler = &usbhs_fifo_pio_push_handler;
  361. return pkt->handler->prepare(pkt, is_done);
  362. }
  363. const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
  364. .prepare = usbhsf_dcp_data_stage_try_push,
  365. };
  366. /*
  367. * DCP data stage (pop)
  368. */
  369. static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
  370. int *is_done)
  371. {
  372. struct usbhs_pipe *pipe = pkt->pipe;
  373. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  374. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
  375. if (usbhs_pipe_is_busy(pipe))
  376. return 0;
  377. /*
  378. * prepare pop for DCP should
  379. * - change DCP direction,
  380. * - clear fifo
  381. * - DATA1
  382. */
  383. usbhs_pipe_disable(pipe);
  384. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  385. usbhsf_fifo_select(pipe, fifo, 0);
  386. usbhsf_fifo_clear(pipe, fifo);
  387. usbhsf_fifo_unselect(pipe, fifo);
  388. /*
  389. * change handler to PIO pop
  390. */
  391. pkt->handler = &usbhs_fifo_pio_pop_handler;
  392. return pkt->handler->prepare(pkt, is_done);
  393. }
  394. const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
  395. .prepare = usbhsf_dcp_data_stage_prepare_pop,
  396. };
  397. /*
  398. * PIO push handler
  399. */
  400. static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
  401. {
  402. struct usbhs_pipe *pipe = pkt->pipe;
  403. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  404. struct device *dev = usbhs_priv_to_dev(priv);
  405. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  406. void __iomem *addr = priv->base + fifo->port;
  407. u8 *buf;
  408. int maxp = usbhs_pipe_get_maxpacket(pipe);
  409. int total_len;
  410. int i, ret, len;
  411. int is_short;
  412. usbhs_pipe_data_sequence(pipe, pkt->sequence);
  413. pkt->sequence = -1; /* -1 sequence will be ignored */
  414. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
  415. ret = usbhsf_fifo_select(pipe, fifo, 1);
  416. if (ret < 0)
  417. return 0;
  418. ret = usbhs_pipe_is_accessible(pipe);
  419. if (ret < 0) {
  420. /* inaccessible pipe is not an error */
  421. ret = 0;
  422. goto usbhs_fifo_write_busy;
  423. }
  424. ret = usbhsf_fifo_barrier(priv, fifo);
  425. if (ret < 0)
  426. goto usbhs_fifo_write_busy;
  427. buf = pkt->buf + pkt->actual;
  428. len = pkt->length - pkt->actual;
  429. len = min(len, maxp);
  430. total_len = len;
  431. is_short = total_len < maxp;
  432. /*
  433. * FIXME
  434. *
  435. * 32-bit access only
  436. */
  437. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  438. iowrite32_rep(addr, buf, len / 4);
  439. len %= 4;
  440. buf += total_len - len;
  441. }
  442. /* the rest operation */
  443. for (i = 0; i < len; i++)
  444. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  445. /*
  446. * variable update
  447. */
  448. pkt->actual += total_len;
  449. if (pkt->actual < pkt->length)
  450. *is_done = 0; /* there are remainder data */
  451. else if (is_short)
  452. *is_done = 1; /* short packet */
  453. else
  454. *is_done = !pkt->zero; /* send zero packet ? */
  455. /*
  456. * pipe/irq handling
  457. */
  458. if (is_short)
  459. usbhsf_send_terminator(pipe, fifo);
  460. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  461. usbhs_pipe_running(pipe, !*is_done);
  462. usbhs_pipe_enable(pipe);
  463. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  464. usbhs_pipe_number(pipe),
  465. pkt->length, pkt->actual, *is_done, pkt->zero);
  466. usbhsf_fifo_unselect(pipe, fifo);
  467. return 0;
  468. usbhs_fifo_write_busy:
  469. usbhsf_fifo_unselect(pipe, fifo);
  470. /*
  471. * pipe is busy.
  472. * retry in interrupt
  473. */
  474. usbhsf_tx_irq_ctrl(pipe, 1);
  475. usbhs_pipe_running(pipe, 1);
  476. return ret;
  477. }
  478. static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  479. {
  480. if (usbhs_pipe_is_running(pkt->pipe))
  481. return 0;
  482. return usbhsf_pio_try_push(pkt, is_done);
  483. }
  484. const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  485. .prepare = usbhsf_pio_prepare_push,
  486. .try_run = usbhsf_pio_try_push,
  487. };
  488. /*
  489. * PIO pop handler
  490. */
  491. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  492. {
  493. struct usbhs_pipe *pipe = pkt->pipe;
  494. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  495. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
  496. if (usbhs_pipe_is_busy(pipe))
  497. return 0;
  498. if (usbhs_pipe_is_running(pipe))
  499. return 0;
  500. /*
  501. * pipe enable to prepare packet receive
  502. */
  503. usbhs_pipe_data_sequence(pipe, pkt->sequence);
  504. pkt->sequence = -1; /* -1 sequence will be ignored */
  505. if (usbhs_pipe_is_dcp(pipe))
  506. usbhsf_fifo_clear(pipe, fifo);
  507. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
  508. usbhs_pipe_enable(pipe);
  509. usbhs_pipe_running(pipe, 1);
  510. usbhsf_rx_irq_ctrl(pipe, 1);
  511. return 0;
  512. }
  513. static int usbhsf_pio_try_pop(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 device *dev = usbhs_priv_to_dev(priv);
  518. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  519. void __iomem *addr = priv->base + fifo->port;
  520. u8 *buf;
  521. u32 data = 0;
  522. int maxp = usbhs_pipe_get_maxpacket(pipe);
  523. int rcv_len, len;
  524. int i, ret;
  525. int total_len = 0;
  526. ret = usbhsf_fifo_select(pipe, fifo, 0);
  527. if (ret < 0)
  528. return 0;
  529. ret = usbhsf_fifo_barrier(priv, fifo);
  530. if (ret < 0)
  531. goto usbhs_fifo_read_busy;
  532. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  533. buf = pkt->buf + pkt->actual;
  534. len = pkt->length - pkt->actual;
  535. len = min(len, rcv_len);
  536. total_len = len;
  537. /*
  538. * update actual length first here to decide disable pipe.
  539. * if this pipe keeps BUF status and all data were popped,
  540. * then, next interrupt/token will be issued again
  541. */
  542. pkt->actual += total_len;
  543. if ((pkt->actual == pkt->length) || /* receive all data */
  544. (total_len < maxp)) { /* short packet */
  545. *is_done = 1;
  546. usbhsf_rx_irq_ctrl(pipe, 0);
  547. usbhs_pipe_running(pipe, 0);
  548. /*
  549. * If function mode, since this controller is possible to enter
  550. * Control Write status stage at this timing, this driver
  551. * should not disable the pipe. If such a case happens, this
  552. * controller is not able to complete the status stage.
  553. */
  554. if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
  555. usbhs_pipe_disable(pipe); /* disable pipe first */
  556. }
  557. /*
  558. * Buffer clear if Zero-Length packet
  559. *
  560. * see
  561. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  562. */
  563. if (0 == rcv_len) {
  564. pkt->zero = 1;
  565. usbhsf_fifo_clear(pipe, fifo);
  566. goto usbhs_fifo_read_end;
  567. }
  568. /*
  569. * FIXME
  570. *
  571. * 32-bit access only
  572. */
  573. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  574. ioread32_rep(addr, buf, len / 4);
  575. len %= 4;
  576. buf += total_len - len;
  577. }
  578. /* the rest operation */
  579. for (i = 0; i < len; i++) {
  580. if (!(i & 0x03))
  581. data = ioread32(addr);
  582. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  583. }
  584. usbhs_fifo_read_end:
  585. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  586. usbhs_pipe_number(pipe),
  587. pkt->length, pkt->actual, *is_done, pkt->zero);
  588. usbhs_fifo_read_busy:
  589. usbhsf_fifo_unselect(pipe, fifo);
  590. return ret;
  591. }
  592. const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  593. .prepare = usbhsf_prepare_pop,
  594. .try_run = usbhsf_pio_try_pop,
  595. };
  596. /*
  597. * DCP ctrol statge handler
  598. */
  599. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  600. {
  601. usbhs_dcp_control_transfer_done(pkt->pipe);
  602. *is_done = 1;
  603. return 0;
  604. }
  605. const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  606. .prepare = usbhsf_ctrl_stage_end,
  607. .try_run = usbhsf_ctrl_stage_end,
  608. };
  609. /*
  610. * DMA fifo functions
  611. */
  612. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  613. struct usbhs_pkt *pkt)
  614. {
  615. if (&usbhs_fifo_dma_push_handler == pkt->handler)
  616. return fifo->tx_chan;
  617. if (&usbhs_fifo_dma_pop_handler == pkt->handler)
  618. return fifo->rx_chan;
  619. return NULL;
  620. }
  621. static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
  622. struct usbhs_pkt *pkt)
  623. {
  624. struct usbhs_fifo *fifo;
  625. int i;
  626. usbhs_for_each_dfifo(priv, fifo, i) {
  627. if (usbhsf_dma_chan_get(fifo, pkt) &&
  628. !usbhsf_fifo_is_busy(fifo))
  629. return fifo;
  630. }
  631. return NULL;
  632. }
  633. #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
  634. #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
  635. static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
  636. struct usbhs_fifo *fifo,
  637. u16 dreqe)
  638. {
  639. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  640. usbhs_bset(priv, fifo->sel, DREQE, dreqe);
  641. }
  642. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  643. {
  644. struct usbhs_pipe *pipe = pkt->pipe;
  645. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  646. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  647. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  648. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  649. return info->dma_map_ctrl(chan->device->dev, pkt, map);
  650. }
  651. static void usbhsf_dma_complete(void *arg);
  652. static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
  653. {
  654. struct usbhs_pipe *pipe = pkt->pipe;
  655. struct usbhs_fifo *fifo;
  656. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  657. struct dma_async_tx_descriptor *desc;
  658. struct dma_chan *chan;
  659. struct device *dev = usbhs_priv_to_dev(priv);
  660. enum dma_transfer_direction dir;
  661. fifo = usbhs_pipe_to_fifo(pipe);
  662. if (!fifo)
  663. return;
  664. chan = usbhsf_dma_chan_get(fifo, pkt);
  665. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  666. desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
  667. pkt->trans, dir,
  668. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  669. if (!desc)
  670. return;
  671. desc->callback = usbhsf_dma_complete;
  672. desc->callback_param = pipe;
  673. pkt->cookie = dmaengine_submit(desc);
  674. if (pkt->cookie < 0) {
  675. dev_err(dev, "Failed to submit dma descriptor\n");
  676. return;
  677. }
  678. dev_dbg(dev, " %s %d (%d/ %d)\n",
  679. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  680. usbhs_pipe_running(pipe, 1);
  681. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
  682. dma_async_issue_pending(chan);
  683. usbhsf_dma_start(pipe, fifo);
  684. usbhs_pipe_enable(pipe);
  685. }
  686. static void xfer_work(struct work_struct *work)
  687. {
  688. struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
  689. struct usbhs_pipe *pipe = pkt->pipe;
  690. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  691. unsigned long flags;
  692. usbhs_lock(priv, flags);
  693. usbhsf_dma_xfer_preparing(pkt);
  694. usbhs_unlock(priv, flags);
  695. }
  696. /*
  697. * DMA push handler
  698. */
  699. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  700. {
  701. struct usbhs_pipe *pipe = pkt->pipe;
  702. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  703. struct usbhs_fifo *fifo;
  704. int len = pkt->length - pkt->actual;
  705. int ret;
  706. uintptr_t align_mask;
  707. if (usbhs_pipe_is_busy(pipe))
  708. return 0;
  709. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  710. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  711. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  712. goto usbhsf_pio_prepare_push;
  713. /* check data length if this driver don't use USB-DMAC */
  714. if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
  715. goto usbhsf_pio_prepare_push;
  716. /* check buffer alignment */
  717. align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
  718. USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
  719. if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
  720. goto usbhsf_pio_prepare_push;
  721. /* return at this time if the pipe is running */
  722. if (usbhs_pipe_is_running(pipe))
  723. return 0;
  724. /* get enable DMA fifo */
  725. fifo = usbhsf_get_dma_fifo(priv, pkt);
  726. if (!fifo)
  727. goto usbhsf_pio_prepare_push;
  728. ret = usbhsf_fifo_select(pipe, fifo, 0);
  729. if (ret < 0)
  730. goto usbhsf_pio_prepare_push;
  731. if (usbhsf_dma_map(pkt) < 0)
  732. goto usbhsf_pio_prepare_push_unselect;
  733. pkt->trans = len;
  734. usbhsf_tx_irq_ctrl(pipe, 0);
  735. /* FIXME: Workaound for usb dmac that driver can be used in atomic */
  736. if (usbhs_get_dparam(priv, has_usb_dmac)) {
  737. usbhsf_dma_xfer_preparing(pkt);
  738. } else {
  739. INIT_WORK(&pkt->work, xfer_work);
  740. schedule_work(&pkt->work);
  741. }
  742. return 0;
  743. usbhsf_pio_prepare_push_unselect:
  744. usbhsf_fifo_unselect(pipe, fifo);
  745. usbhsf_pio_prepare_push:
  746. /*
  747. * change handler to PIO
  748. */
  749. pkt->handler = &usbhs_fifo_pio_push_handler;
  750. return pkt->handler->prepare(pkt, is_done);
  751. }
  752. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  753. {
  754. struct usbhs_pipe *pipe = pkt->pipe;
  755. int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
  756. pkt->actual += pkt->trans;
  757. if (pkt->actual < pkt->length)
  758. *is_done = 0; /* there are remainder data */
  759. else if (is_short)
  760. *is_done = 1; /* short packet */
  761. else
  762. *is_done = !pkt->zero; /* send zero packet? */
  763. usbhs_pipe_running(pipe, !*is_done);
  764. usbhsf_dma_stop(pipe, pipe->fifo);
  765. usbhsf_dma_unmap(pkt);
  766. usbhsf_fifo_unselect(pipe, pipe->fifo);
  767. if (!*is_done) {
  768. /* change handler to PIO */
  769. pkt->handler = &usbhs_fifo_pio_push_handler;
  770. return pkt->handler->try_run(pkt, is_done);
  771. }
  772. return 0;
  773. }
  774. const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  775. .prepare = usbhsf_dma_prepare_push,
  776. .dma_done = usbhsf_dma_push_done,
  777. };
  778. /*
  779. * DMA pop handler
  780. */
  781. static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
  782. int *is_done)
  783. {
  784. return usbhsf_prepare_pop(pkt, is_done);
  785. }
  786. static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
  787. int *is_done)
  788. {
  789. struct usbhs_pipe *pipe = pkt->pipe;
  790. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  791. struct usbhs_fifo *fifo;
  792. int ret;
  793. if (usbhs_pipe_is_busy(pipe))
  794. return 0;
  795. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  796. if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
  797. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  798. goto usbhsf_pio_prepare_pop;
  799. fifo = usbhsf_get_dma_fifo(priv, pkt);
  800. if (!fifo)
  801. goto usbhsf_pio_prepare_pop;
  802. if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
  803. goto usbhsf_pio_prepare_pop;
  804. /* return at this time if the pipe is running */
  805. if (usbhs_pipe_is_running(pipe))
  806. return 0;
  807. usbhs_pipe_config_change_bfre(pipe, 1);
  808. ret = usbhsf_fifo_select(pipe, fifo, 0);
  809. if (ret < 0)
  810. goto usbhsf_pio_prepare_pop;
  811. if (usbhsf_dma_map(pkt) < 0)
  812. goto usbhsf_pio_prepare_pop_unselect;
  813. /* DMA */
  814. /*
  815. * usbhs_fifo_dma_pop_handler :: prepare
  816. * enabled irq to come here.
  817. * but it is no longer needed for DMA. disable it.
  818. */
  819. usbhsf_rx_irq_ctrl(pipe, 0);
  820. pkt->trans = pkt->length;
  821. usbhsf_dma_xfer_preparing(pkt);
  822. return 0;
  823. usbhsf_pio_prepare_pop_unselect:
  824. usbhsf_fifo_unselect(pipe, fifo);
  825. usbhsf_pio_prepare_pop:
  826. /*
  827. * change handler to PIO
  828. */
  829. pkt->handler = &usbhs_fifo_pio_pop_handler;
  830. usbhs_pipe_config_change_bfre(pipe, 0);
  831. return pkt->handler->prepare(pkt, is_done);
  832. }
  833. static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  834. {
  835. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  836. if (usbhs_get_dparam(priv, has_usb_dmac))
  837. return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
  838. else
  839. return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
  840. }
  841. static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
  842. {
  843. struct usbhs_pipe *pipe = pkt->pipe;
  844. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  845. struct usbhs_fifo *fifo;
  846. int len, ret;
  847. if (usbhs_pipe_is_busy(pipe))
  848. return 0;
  849. if (usbhs_pipe_is_dcp(pipe))
  850. goto usbhsf_pio_prepare_pop;
  851. /* get enable DMA fifo */
  852. fifo = usbhsf_get_dma_fifo(priv, pkt);
  853. if (!fifo)
  854. goto usbhsf_pio_prepare_pop;
  855. if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  856. goto usbhsf_pio_prepare_pop;
  857. ret = usbhsf_fifo_select(pipe, fifo, 0);
  858. if (ret < 0)
  859. goto usbhsf_pio_prepare_pop;
  860. /* use PIO if packet is less than pio_dma_border */
  861. len = usbhsf_fifo_rcv_len(priv, fifo);
  862. len = min(pkt->length - pkt->actual, len);
  863. if (len & 0x7) /* 8byte alignment */
  864. goto usbhsf_pio_prepare_pop_unselect;
  865. if (len < usbhs_get_dparam(priv, pio_dma_border))
  866. goto usbhsf_pio_prepare_pop_unselect;
  867. ret = usbhsf_fifo_barrier(priv, fifo);
  868. if (ret < 0)
  869. goto usbhsf_pio_prepare_pop_unselect;
  870. if (usbhsf_dma_map(pkt) < 0)
  871. goto usbhsf_pio_prepare_pop_unselect;
  872. /* DMA */
  873. /*
  874. * usbhs_fifo_dma_pop_handler :: prepare
  875. * enabled irq to come here.
  876. * but it is no longer needed for DMA. disable it.
  877. */
  878. usbhsf_rx_irq_ctrl(pipe, 0);
  879. pkt->trans = len;
  880. INIT_WORK(&pkt->work, xfer_work);
  881. schedule_work(&pkt->work);
  882. return 0;
  883. usbhsf_pio_prepare_pop_unselect:
  884. usbhsf_fifo_unselect(pipe, fifo);
  885. usbhsf_pio_prepare_pop:
  886. /*
  887. * change handler to PIO
  888. */
  889. pkt->handler = &usbhs_fifo_pio_pop_handler;
  890. return pkt->handler->try_run(pkt, is_done);
  891. }
  892. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  893. {
  894. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  895. BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
  896. return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
  897. }
  898. static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
  899. {
  900. struct usbhs_pipe *pipe = pkt->pipe;
  901. int maxp = usbhs_pipe_get_maxpacket(pipe);
  902. usbhsf_dma_stop(pipe, pipe->fifo);
  903. usbhsf_dma_unmap(pkt);
  904. usbhsf_fifo_unselect(pipe, pipe->fifo);
  905. pkt->actual += pkt->trans;
  906. if ((pkt->actual == pkt->length) || /* receive all data */
  907. (pkt->trans < maxp)) { /* short packet */
  908. *is_done = 1;
  909. usbhs_pipe_running(pipe, 0);
  910. } else {
  911. /* re-enable */
  912. usbhs_pipe_running(pipe, 0);
  913. usbhsf_prepare_pop(pkt, is_done);
  914. }
  915. return 0;
  916. }
  917. static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
  918. struct dma_chan *chan, int dtln)
  919. {
  920. struct usbhs_pipe *pipe = pkt->pipe;
  921. struct dma_tx_state state;
  922. size_t received_size;
  923. int maxp = usbhs_pipe_get_maxpacket(pipe);
  924. dmaengine_tx_status(chan, pkt->cookie, &state);
  925. received_size = pkt->length - state.residue;
  926. if (dtln) {
  927. received_size -= USBHS_USB_DMAC_XFER_SIZE;
  928. received_size &= ~(maxp - 1);
  929. received_size += dtln;
  930. }
  931. return received_size;
  932. }
  933. static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
  934. int *is_done)
  935. {
  936. struct usbhs_pipe *pipe = pkt->pipe;
  937. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  938. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  939. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  940. int rcv_len;
  941. /*
  942. * Since the driver disables rx_irq in DMA mode, the interrupt handler
  943. * cannot the BRDYSTS. So, the function clears it here because the
  944. * driver may use PIO mode next time.
  945. */
  946. usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
  947. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  948. usbhsf_fifo_clear(pipe, fifo);
  949. pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
  950. usbhs_pipe_running(pipe, 0);
  951. usbhsf_dma_stop(pipe, fifo);
  952. usbhsf_dma_unmap(pkt);
  953. usbhsf_fifo_unselect(pipe, pipe->fifo);
  954. /* The driver can assume the rx transaction is always "done" */
  955. *is_done = 1;
  956. return 0;
  957. }
  958. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  959. {
  960. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  961. if (usbhs_get_dparam(priv, has_usb_dmac))
  962. return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
  963. else
  964. return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
  965. }
  966. const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  967. .prepare = usbhsf_dma_prepare_pop,
  968. .try_run = usbhsf_dma_try_pop,
  969. .dma_done = usbhsf_dma_pop_done
  970. };
  971. /*
  972. * DMA setting
  973. */
  974. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  975. {
  976. struct sh_dmae_slave *slave = param;
  977. /*
  978. * FIXME
  979. *
  980. * usbhs doesn't recognize id = 0 as valid DMA
  981. */
  982. if (0 == slave->shdma_slave.slave_id)
  983. return false;
  984. chan->private = slave;
  985. return true;
  986. }
  987. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  988. {
  989. if (fifo->tx_chan)
  990. dma_release_channel(fifo->tx_chan);
  991. if (fifo->rx_chan)
  992. dma_release_channel(fifo->rx_chan);
  993. fifo->tx_chan = NULL;
  994. fifo->rx_chan = NULL;
  995. }
  996. static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
  997. {
  998. dma_cap_mask_t mask;
  999. dma_cap_zero(mask);
  1000. dma_cap_set(DMA_SLAVE, mask);
  1001. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  1002. &fifo->tx_slave);
  1003. dma_cap_zero(mask);
  1004. dma_cap_set(DMA_SLAVE, mask);
  1005. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  1006. &fifo->rx_slave);
  1007. }
  1008. static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
  1009. int channel)
  1010. {
  1011. char name[16];
  1012. /*
  1013. * To avoid complex handing for DnFIFOs, the driver uses each
  1014. * DnFIFO as TX or RX direction (not bi-direction).
  1015. * So, the driver uses odd channels for TX, even channels for RX.
  1016. */
  1017. snprintf(name, sizeof(name), "ch%d", channel);
  1018. if (channel & 1) {
  1019. fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
  1020. if (IS_ERR(fifo->tx_chan))
  1021. fifo->tx_chan = NULL;
  1022. } else {
  1023. fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
  1024. if (IS_ERR(fifo->rx_chan))
  1025. fifo->rx_chan = NULL;
  1026. }
  1027. }
  1028. static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
  1029. int channel)
  1030. {
  1031. struct device *dev = usbhs_priv_to_dev(priv);
  1032. if (dev->of_node)
  1033. usbhsf_dma_init_dt(dev, fifo, channel);
  1034. else
  1035. usbhsf_dma_init_pdev(fifo);
  1036. if (fifo->tx_chan || fifo->rx_chan)
  1037. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  1038. fifo->name,
  1039. fifo->tx_chan ? "[TX]" : " ",
  1040. fifo->rx_chan ? "[RX]" : " ");
  1041. }
  1042. /*
  1043. * irq functions
  1044. */
  1045. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  1046. struct usbhs_irq_state *irq_state)
  1047. {
  1048. struct usbhs_pipe *pipe;
  1049. struct device *dev = usbhs_priv_to_dev(priv);
  1050. int i, ret;
  1051. if (!irq_state->bempsts) {
  1052. dev_err(dev, "debug %s !!\n", __func__);
  1053. return -EIO;
  1054. }
  1055. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  1056. /*
  1057. * search interrupted "pipe"
  1058. * not "uep".
  1059. */
  1060. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  1061. if (!(irq_state->bempsts & (1 << i)))
  1062. continue;
  1063. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  1064. if (ret < 0)
  1065. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  1066. }
  1067. return 0;
  1068. }
  1069. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  1070. struct usbhs_irq_state *irq_state)
  1071. {
  1072. struct usbhs_pipe *pipe;
  1073. struct device *dev = usbhs_priv_to_dev(priv);
  1074. int i, ret;
  1075. if (!irq_state->brdysts) {
  1076. dev_err(dev, "debug %s !!\n", __func__);
  1077. return -EIO;
  1078. }
  1079. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  1080. /*
  1081. * search interrupted "pipe"
  1082. * not "uep".
  1083. */
  1084. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  1085. if (!(irq_state->brdysts & (1 << i)))
  1086. continue;
  1087. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  1088. if (ret < 0)
  1089. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  1090. }
  1091. return 0;
  1092. }
  1093. static void usbhsf_dma_complete(void *arg)
  1094. {
  1095. struct usbhs_pipe *pipe = arg;
  1096. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  1097. struct device *dev = usbhs_priv_to_dev(priv);
  1098. int ret;
  1099. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
  1100. if (ret < 0)
  1101. dev_err(dev, "dma_complete run_error %d : %d\n",
  1102. usbhs_pipe_number(pipe), ret);
  1103. }
  1104. void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
  1105. {
  1106. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  1107. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  1108. /* clear DCP FIFO of transmission */
  1109. if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
  1110. return;
  1111. usbhsf_fifo_clear(pipe, fifo);
  1112. usbhsf_fifo_unselect(pipe, fifo);
  1113. /* clear DCP FIFO of reception */
  1114. if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
  1115. return;
  1116. usbhsf_fifo_clear(pipe, fifo);
  1117. usbhsf_fifo_unselect(pipe, fifo);
  1118. }
  1119. /*
  1120. * fifo init
  1121. */
  1122. void usbhs_fifo_init(struct usbhs_priv *priv)
  1123. {
  1124. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1125. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  1126. struct usbhs_fifo *dfifo;
  1127. int i;
  1128. mod->irq_empty = usbhsf_irq_empty;
  1129. mod->irq_ready = usbhsf_irq_ready;
  1130. mod->irq_bempsts = 0;
  1131. mod->irq_brdysts = 0;
  1132. cfifo->pipe = NULL;
  1133. usbhs_for_each_dfifo(priv, dfifo, i)
  1134. dfifo->pipe = NULL;
  1135. }
  1136. void usbhs_fifo_quit(struct usbhs_priv *priv)
  1137. {
  1138. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1139. mod->irq_empty = NULL;
  1140. mod->irq_ready = NULL;
  1141. mod->irq_bempsts = 0;
  1142. mod->irq_brdysts = 0;
  1143. }
  1144. #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
  1145. do { \
  1146. fifo = usbhsf_get_dnfifo(priv, channel); \
  1147. fifo->name = "D"#channel"FIFO"; \
  1148. fifo->port = fifo_port; \
  1149. fifo->sel = D##channel##FIFOSEL; \
  1150. fifo->ctr = D##channel##FIFOCTR; \
  1151. fifo->tx_slave.shdma_slave.slave_id = \
  1152. usbhs_get_dparam(priv, d##channel##_tx_id); \
  1153. fifo->rx_slave.shdma_slave.slave_id = \
  1154. usbhs_get_dparam(priv, d##channel##_rx_id); \
  1155. usbhsf_dma_init(priv, fifo, channel); \
  1156. } while (0)
  1157. #define USBHS_DFIFO_INIT(priv, fifo, channel) \
  1158. __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
  1159. #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
  1160. __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
  1161. int usbhs_fifo_probe(struct usbhs_priv *priv)
  1162. {
  1163. struct usbhs_fifo *fifo;
  1164. /* CFIFO */
  1165. fifo = usbhsf_get_cfifo(priv);
  1166. fifo->name = "CFIFO";
  1167. fifo->port = CFIFO;
  1168. fifo->sel = CFIFOSEL;
  1169. fifo->ctr = CFIFOCTR;
  1170. /* DFIFO */
  1171. USBHS_DFIFO_INIT(priv, fifo, 0);
  1172. USBHS_DFIFO_INIT(priv, fifo, 1);
  1173. USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
  1174. USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
  1175. return 0;
  1176. }
  1177. void usbhs_fifo_remove(struct usbhs_priv *priv)
  1178. {
  1179. struct usbhs_fifo *fifo;
  1180. int i;
  1181. usbhs_for_each_dfifo(priv, fifo, i)
  1182. usbhsf_dma_quit(priv, fifo);
  1183. }