fifo.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 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. 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. 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. 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. 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. 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. 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. 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. return info->dma_map_ctrl(pkt, map);
  665. }
  666. static void usbhsf_dma_complete(void *arg);
  667. static void xfer_work(struct work_struct *work)
  668. {
  669. struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
  670. struct usbhs_pipe *pipe = pkt->pipe;
  671. struct usbhs_fifo *fifo;
  672. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  673. struct dma_async_tx_descriptor *desc;
  674. struct dma_chan *chan;
  675. struct device *dev = usbhs_priv_to_dev(priv);
  676. enum dma_transfer_direction dir;
  677. unsigned long flags;
  678. usbhs_lock(priv, flags);
  679. fifo = usbhs_pipe_to_fifo(pipe);
  680. if (!fifo)
  681. goto xfer_work_end;
  682. chan = usbhsf_dma_chan_get(fifo, pkt);
  683. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  684. desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
  685. pkt->trans, dir,
  686. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  687. if (!desc)
  688. goto xfer_work_end;
  689. desc->callback = usbhsf_dma_complete;
  690. desc->callback_param = pipe;
  691. pkt->cookie = dmaengine_submit(desc);
  692. if (pkt->cookie < 0) {
  693. dev_err(dev, "Failed to submit dma descriptor\n");
  694. goto xfer_work_end;
  695. }
  696. dev_dbg(dev, " %s %d (%d/ %d)\n",
  697. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  698. usbhs_pipe_running(pipe, 1);
  699. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
  700. dma_async_issue_pending(chan);
  701. usbhsf_dma_start(pipe, fifo);
  702. usbhs_pipe_enable(pipe);
  703. xfer_work_end:
  704. usbhs_unlock(priv, flags);
  705. }
  706. /*
  707. * DMA push handler
  708. */
  709. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  710. {
  711. struct usbhs_pipe *pipe = pkt->pipe;
  712. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  713. struct usbhs_fifo *fifo;
  714. int len = pkt->length - pkt->actual;
  715. int ret;
  716. uintptr_t align_mask;
  717. if (usbhs_pipe_is_busy(pipe))
  718. return 0;
  719. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  720. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  721. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  722. goto usbhsf_pio_prepare_push;
  723. /* check data length if this driver don't use USB-DMAC */
  724. if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
  725. goto usbhsf_pio_prepare_push;
  726. /* check buffer alignment */
  727. align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
  728. USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
  729. if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
  730. goto usbhsf_pio_prepare_push;
  731. /* return at this time if the pipe is running */
  732. if (usbhs_pipe_is_running(pipe))
  733. return 0;
  734. /* get enable DMA fifo */
  735. fifo = usbhsf_get_dma_fifo(priv, pkt);
  736. if (!fifo)
  737. goto usbhsf_pio_prepare_push;
  738. if (usbhsf_dma_map(pkt) < 0)
  739. goto usbhsf_pio_prepare_push;
  740. ret = usbhsf_fifo_select(pipe, fifo, 0);
  741. if (ret < 0)
  742. goto usbhsf_pio_prepare_push_unmap;
  743. pkt->trans = len;
  744. usbhsf_tx_irq_ctrl(pipe, 0);
  745. INIT_WORK(&pkt->work, xfer_work);
  746. schedule_work(&pkt->work);
  747. return 0;
  748. usbhsf_pio_prepare_push_unmap:
  749. usbhsf_dma_unmap(pkt);
  750. usbhsf_pio_prepare_push:
  751. /*
  752. * change handler to PIO
  753. */
  754. pkt->handler = &usbhs_fifo_pio_push_handler;
  755. return pkt->handler->prepare(pkt, is_done);
  756. }
  757. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  758. {
  759. struct usbhs_pipe *pipe = pkt->pipe;
  760. int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
  761. pkt->actual += pkt->trans;
  762. if (pkt->actual < pkt->length)
  763. *is_done = 0; /* there are remainder data */
  764. else if (is_short)
  765. *is_done = 1; /* short packet */
  766. else
  767. *is_done = !pkt->zero; /* send zero packet? */
  768. usbhs_pipe_running(pipe, !*is_done);
  769. usbhsf_dma_stop(pipe, pipe->fifo);
  770. usbhsf_dma_unmap(pkt);
  771. usbhsf_fifo_unselect(pipe, pipe->fifo);
  772. if (!*is_done) {
  773. /* change handler to PIO */
  774. pkt->handler = &usbhs_fifo_pio_push_handler;
  775. return pkt->handler->try_run(pkt, is_done);
  776. }
  777. return 0;
  778. }
  779. struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  780. .prepare = usbhsf_dma_prepare_push,
  781. .dma_done = usbhsf_dma_push_done,
  782. };
  783. /*
  784. * DMA pop handler
  785. */
  786. static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
  787. int *is_done)
  788. {
  789. return usbhsf_prepare_pop(pkt, is_done);
  790. }
  791. static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
  792. int *is_done)
  793. {
  794. struct usbhs_pipe *pipe = pkt->pipe;
  795. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  796. struct usbhs_fifo *fifo;
  797. int ret;
  798. if (usbhs_pipe_is_busy(pipe))
  799. return 0;
  800. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  801. if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
  802. usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
  803. goto usbhsf_pio_prepare_pop;
  804. fifo = usbhsf_get_dma_fifo(priv, pkt);
  805. if (!fifo)
  806. goto usbhsf_pio_prepare_pop;
  807. if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
  808. goto usbhsf_pio_prepare_pop;
  809. /* return at this time if the pipe is running */
  810. if (usbhs_pipe_is_running(pipe))
  811. return 0;
  812. usbhs_pipe_config_change_bfre(pipe, 1);
  813. ret = usbhsf_fifo_select(pipe, fifo, 0);
  814. if (ret < 0)
  815. goto usbhsf_pio_prepare_pop;
  816. if (usbhsf_dma_map(pkt) < 0)
  817. goto usbhsf_pio_prepare_pop_unselect;
  818. /* DMA */
  819. /*
  820. * usbhs_fifo_dma_pop_handler :: prepare
  821. * enabled irq to come here.
  822. * but it is no longer needed for DMA. disable it.
  823. */
  824. usbhsf_rx_irq_ctrl(pipe, 0);
  825. pkt->trans = pkt->length;
  826. INIT_WORK(&pkt->work, xfer_work);
  827. schedule_work(&pkt->work);
  828. return 0;
  829. usbhsf_pio_prepare_pop_unselect:
  830. usbhsf_fifo_unselect(pipe, fifo);
  831. usbhsf_pio_prepare_pop:
  832. /*
  833. * change handler to PIO
  834. */
  835. pkt->handler = &usbhs_fifo_pio_pop_handler;
  836. usbhs_pipe_config_change_bfre(pipe, 0);
  837. return pkt->handler->prepare(pkt, is_done);
  838. }
  839. static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  840. {
  841. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  842. if (usbhs_get_dparam(priv, has_usb_dmac))
  843. return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
  844. else
  845. return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
  846. }
  847. static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
  848. {
  849. struct usbhs_pipe *pipe = pkt->pipe;
  850. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  851. struct usbhs_fifo *fifo;
  852. int len, ret;
  853. if (usbhs_pipe_is_busy(pipe))
  854. return 0;
  855. if (usbhs_pipe_is_dcp(pipe))
  856. goto usbhsf_pio_prepare_pop;
  857. /* get enable DMA fifo */
  858. fifo = usbhsf_get_dma_fifo(priv, pkt);
  859. if (!fifo)
  860. goto usbhsf_pio_prepare_pop;
  861. if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  862. goto usbhsf_pio_prepare_pop;
  863. ret = usbhsf_fifo_select(pipe, fifo, 0);
  864. if (ret < 0)
  865. goto usbhsf_pio_prepare_pop;
  866. /* use PIO if packet is less than pio_dma_border */
  867. len = usbhsf_fifo_rcv_len(priv, fifo);
  868. len = min(pkt->length - pkt->actual, len);
  869. if (len & 0x7) /* 8byte alignment */
  870. goto usbhsf_pio_prepare_pop_unselect;
  871. if (len < usbhs_get_dparam(priv, pio_dma_border))
  872. goto usbhsf_pio_prepare_pop_unselect;
  873. ret = usbhsf_fifo_barrier(priv, fifo);
  874. if (ret < 0)
  875. goto usbhsf_pio_prepare_pop_unselect;
  876. if (usbhsf_dma_map(pkt) < 0)
  877. goto usbhsf_pio_prepare_pop_unselect;
  878. /* DMA */
  879. /*
  880. * usbhs_fifo_dma_pop_handler :: prepare
  881. * enabled irq to come here.
  882. * but it is no longer needed for DMA. disable it.
  883. */
  884. usbhsf_rx_irq_ctrl(pipe, 0);
  885. pkt->trans = len;
  886. INIT_WORK(&pkt->work, xfer_work);
  887. schedule_work(&pkt->work);
  888. return 0;
  889. usbhsf_pio_prepare_pop_unselect:
  890. usbhsf_fifo_unselect(pipe, fifo);
  891. usbhsf_pio_prepare_pop:
  892. /*
  893. * change handler to PIO
  894. */
  895. pkt->handler = &usbhs_fifo_pio_pop_handler;
  896. return pkt->handler->try_run(pkt, is_done);
  897. }
  898. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  899. {
  900. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  901. BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
  902. return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
  903. }
  904. static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
  905. {
  906. struct usbhs_pipe *pipe = pkt->pipe;
  907. int maxp = usbhs_pipe_get_maxpacket(pipe);
  908. usbhsf_dma_stop(pipe, pipe->fifo);
  909. usbhsf_dma_unmap(pkt);
  910. usbhsf_fifo_unselect(pipe, pipe->fifo);
  911. pkt->actual += pkt->trans;
  912. if ((pkt->actual == pkt->length) || /* receive all data */
  913. (pkt->trans < maxp)) { /* short packet */
  914. *is_done = 1;
  915. usbhs_pipe_running(pipe, 0);
  916. } else {
  917. /* re-enable */
  918. usbhs_pipe_running(pipe, 0);
  919. usbhsf_prepare_pop(pkt, is_done);
  920. }
  921. return 0;
  922. }
  923. static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
  924. struct dma_chan *chan, int dtln)
  925. {
  926. struct usbhs_pipe *pipe = pkt->pipe;
  927. struct dma_tx_state state;
  928. size_t received_size;
  929. int maxp = usbhs_pipe_get_maxpacket(pipe);
  930. dmaengine_tx_status(chan, pkt->cookie, &state);
  931. received_size = pkt->length - state.residue;
  932. if (dtln) {
  933. received_size -= USBHS_USB_DMAC_XFER_SIZE;
  934. received_size &= ~(maxp - 1);
  935. received_size += dtln;
  936. }
  937. return received_size;
  938. }
  939. static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
  940. int *is_done)
  941. {
  942. struct usbhs_pipe *pipe = pkt->pipe;
  943. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  944. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  945. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  946. int rcv_len;
  947. /*
  948. * Since the driver disables rx_irq in DMA mode, the interrupt handler
  949. * cannot the BRDYSTS. So, the function clears it here because the
  950. * driver may use PIO mode next time.
  951. */
  952. usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
  953. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  954. usbhsf_fifo_clear(pipe, fifo);
  955. pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
  956. usbhs_pipe_running(pipe, 0);
  957. usbhsf_dma_stop(pipe, fifo);
  958. usbhsf_dma_unmap(pkt);
  959. usbhsf_fifo_unselect(pipe, pipe->fifo);
  960. /* The driver can assume the rx transaction is always "done" */
  961. *is_done = 1;
  962. return 0;
  963. }
  964. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  965. {
  966. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  967. if (usbhs_get_dparam(priv, has_usb_dmac))
  968. return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
  969. else
  970. return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
  971. }
  972. struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  973. .prepare = usbhsf_dma_prepare_pop,
  974. .try_run = usbhsf_dma_try_pop,
  975. .dma_done = usbhsf_dma_pop_done
  976. };
  977. /*
  978. * DMA setting
  979. */
  980. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  981. {
  982. struct sh_dmae_slave *slave = param;
  983. /*
  984. * FIXME
  985. *
  986. * usbhs doesn't recognize id = 0 as valid DMA
  987. */
  988. if (0 == slave->shdma_slave.slave_id)
  989. return false;
  990. chan->private = slave;
  991. return true;
  992. }
  993. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  994. {
  995. if (fifo->tx_chan)
  996. dma_release_channel(fifo->tx_chan);
  997. if (fifo->rx_chan)
  998. dma_release_channel(fifo->rx_chan);
  999. fifo->tx_chan = NULL;
  1000. fifo->rx_chan = NULL;
  1001. }
  1002. static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
  1003. {
  1004. dma_cap_mask_t mask;
  1005. dma_cap_zero(mask);
  1006. dma_cap_set(DMA_SLAVE, mask);
  1007. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  1008. &fifo->tx_slave);
  1009. dma_cap_zero(mask);
  1010. dma_cap_set(DMA_SLAVE, mask);
  1011. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  1012. &fifo->rx_slave);
  1013. }
  1014. static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
  1015. int channel)
  1016. {
  1017. char name[16];
  1018. /*
  1019. * To avoid complex handing for DnFIFOs, the driver uses each
  1020. * DnFIFO as TX or RX direction (not bi-direction).
  1021. * So, the driver uses odd channels for TX, even channels for RX.
  1022. */
  1023. snprintf(name, sizeof(name), "ch%d", channel);
  1024. if (channel & 1) {
  1025. fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
  1026. if (IS_ERR(fifo->tx_chan))
  1027. fifo->tx_chan = NULL;
  1028. } else {
  1029. fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
  1030. if (IS_ERR(fifo->rx_chan))
  1031. fifo->rx_chan = NULL;
  1032. }
  1033. }
  1034. static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
  1035. int channel)
  1036. {
  1037. struct device *dev = usbhs_priv_to_dev(priv);
  1038. if (dev->of_node)
  1039. usbhsf_dma_init_dt(dev, fifo, channel);
  1040. else
  1041. usbhsf_dma_init_pdev(fifo);
  1042. if (fifo->tx_chan || fifo->rx_chan)
  1043. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  1044. fifo->name,
  1045. fifo->tx_chan ? "[TX]" : " ",
  1046. fifo->rx_chan ? "[RX]" : " ");
  1047. }
  1048. /*
  1049. * irq functions
  1050. */
  1051. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  1052. struct usbhs_irq_state *irq_state)
  1053. {
  1054. struct usbhs_pipe *pipe;
  1055. struct device *dev = usbhs_priv_to_dev(priv);
  1056. int i, ret;
  1057. if (!irq_state->bempsts) {
  1058. dev_err(dev, "debug %s !!\n", __func__);
  1059. return -EIO;
  1060. }
  1061. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  1062. /*
  1063. * search interrupted "pipe"
  1064. * not "uep".
  1065. */
  1066. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  1067. if (!(irq_state->bempsts & (1 << i)))
  1068. continue;
  1069. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  1070. if (ret < 0)
  1071. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  1072. }
  1073. return 0;
  1074. }
  1075. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  1076. struct usbhs_irq_state *irq_state)
  1077. {
  1078. struct usbhs_pipe *pipe;
  1079. struct device *dev = usbhs_priv_to_dev(priv);
  1080. int i, ret;
  1081. if (!irq_state->brdysts) {
  1082. dev_err(dev, "debug %s !!\n", __func__);
  1083. return -EIO;
  1084. }
  1085. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  1086. /*
  1087. * search interrupted "pipe"
  1088. * not "uep".
  1089. */
  1090. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  1091. if (!(irq_state->brdysts & (1 << i)))
  1092. continue;
  1093. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  1094. if (ret < 0)
  1095. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  1096. }
  1097. return 0;
  1098. }
  1099. static void usbhsf_dma_complete(void *arg)
  1100. {
  1101. struct usbhs_pipe *pipe = arg;
  1102. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  1103. struct device *dev = usbhs_priv_to_dev(priv);
  1104. int ret;
  1105. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
  1106. if (ret < 0)
  1107. dev_err(dev, "dma_complete run_error %d : %d\n",
  1108. usbhs_pipe_number(pipe), ret);
  1109. }
  1110. void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
  1111. {
  1112. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  1113. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  1114. /* clear DCP FIFO of transmission */
  1115. if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
  1116. return;
  1117. usbhsf_fifo_clear(pipe, fifo);
  1118. usbhsf_fifo_unselect(pipe, fifo);
  1119. /* clear DCP FIFO of reception */
  1120. if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
  1121. return;
  1122. usbhsf_fifo_clear(pipe, fifo);
  1123. usbhsf_fifo_unselect(pipe, fifo);
  1124. }
  1125. /*
  1126. * fifo init
  1127. */
  1128. void usbhs_fifo_init(struct usbhs_priv *priv)
  1129. {
  1130. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1131. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  1132. struct usbhs_fifo *dfifo;
  1133. int i;
  1134. mod->irq_empty = usbhsf_irq_empty;
  1135. mod->irq_ready = usbhsf_irq_ready;
  1136. mod->irq_bempsts = 0;
  1137. mod->irq_brdysts = 0;
  1138. cfifo->pipe = NULL;
  1139. usbhs_for_each_dfifo(priv, dfifo, i)
  1140. dfifo->pipe = NULL;
  1141. }
  1142. void usbhs_fifo_quit(struct usbhs_priv *priv)
  1143. {
  1144. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1145. mod->irq_empty = NULL;
  1146. mod->irq_ready = NULL;
  1147. mod->irq_bempsts = 0;
  1148. mod->irq_brdysts = 0;
  1149. }
  1150. #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
  1151. do { \
  1152. fifo = usbhsf_get_dnfifo(priv, channel); \
  1153. fifo->name = "D"#channel"FIFO"; \
  1154. fifo->port = fifo_port; \
  1155. fifo->sel = D##channel##FIFOSEL; \
  1156. fifo->ctr = D##channel##FIFOCTR; \
  1157. fifo->tx_slave.shdma_slave.slave_id = \
  1158. usbhs_get_dparam(priv, d##channel##_tx_id); \
  1159. fifo->rx_slave.shdma_slave.slave_id = \
  1160. usbhs_get_dparam(priv, d##channel##_rx_id); \
  1161. usbhsf_dma_init(priv, fifo, channel); \
  1162. } while (0)
  1163. #define USBHS_DFIFO_INIT(priv, fifo, channel) \
  1164. __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
  1165. #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
  1166. __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
  1167. int usbhs_fifo_probe(struct usbhs_priv *priv)
  1168. {
  1169. struct usbhs_fifo *fifo;
  1170. /* CFIFO */
  1171. fifo = usbhsf_get_cfifo(priv);
  1172. fifo->name = "CFIFO";
  1173. fifo->port = CFIFO;
  1174. fifo->sel = CFIFOSEL;
  1175. fifo->ctr = CFIFOCTR;
  1176. /* DFIFO */
  1177. USBHS_DFIFO_INIT(priv, fifo, 0);
  1178. USBHS_DFIFO_INIT(priv, fifo, 1);
  1179. USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
  1180. USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
  1181. return 0;
  1182. }
  1183. void usbhs_fifo_remove(struct usbhs_priv *priv)
  1184. {
  1185. struct usbhs_fifo *fifo;
  1186. int i;
  1187. usbhs_for_each_dfifo(priv, fifo, i)
  1188. usbhsf_dma_quit(priv, fifo);
  1189. }