fifo.c 34 KB

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