fifo.c 34 KB

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