fifo.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. /*
  2. * Renesas USB driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. *
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/scatterlist.h>
  20. #include "common.h"
  21. #include "pipe.h"
  22. #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
  23. #define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
  24. #define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
  25. #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
  26. #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
  27. /*
  28. * packet initialize
  29. */
  30. void usbhs_pkt_init(struct usbhs_pkt *pkt)
  31. {
  32. pkt->dma = DMA_ADDR_INVALID;
  33. INIT_LIST_HEAD(&pkt->node);
  34. }
  35. /*
  36. * packet control function
  37. */
  38. static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
  39. {
  40. struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
  41. struct device *dev = usbhs_priv_to_dev(priv);
  42. dev_err(dev, "null handler\n");
  43. return -EINVAL;
  44. }
  45. static struct usbhs_pkt_handle usbhsf_null_handler = {
  46. .prepare = usbhsf_null_handle,
  47. .try_run = usbhsf_null_handle,
  48. };
  49. void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
  50. void (*done)(struct usbhs_priv *priv,
  51. struct usbhs_pkt *pkt),
  52. void *buf, int len, int zero, int sequence)
  53. {
  54. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  55. struct device *dev = usbhs_priv_to_dev(priv);
  56. unsigned long flags;
  57. if (!done) {
  58. dev_err(dev, "no done function\n");
  59. return;
  60. }
  61. /******************** spin lock ********************/
  62. usbhs_lock(priv, flags);
  63. if (!pipe->handler) {
  64. dev_err(dev, "no handler function\n");
  65. pipe->handler = &usbhsf_null_handler;
  66. }
  67. list_move_tail(&pkt->node, &pipe->list);
  68. /*
  69. * each pkt must hold own handler.
  70. * because handler might be changed by its situation.
  71. * dma handler -> pio handler.
  72. */
  73. pkt->pipe = pipe;
  74. pkt->buf = buf;
  75. pkt->handler = pipe->handler;
  76. pkt->length = len;
  77. pkt->zero = zero;
  78. pkt->actual = 0;
  79. pkt->done = done;
  80. pkt->sequence = sequence;
  81. usbhs_unlock(priv, flags);
  82. /******************** spin unlock ******************/
  83. }
  84. static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
  85. {
  86. list_del_init(&pkt->node);
  87. }
  88. static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
  89. {
  90. if (list_empty(&pipe->list))
  91. return NULL;
  92. return list_first_entry(&pipe->list, struct usbhs_pkt, node);
  93. }
  94. struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
  95. {
  96. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  97. unsigned long flags;
  98. /******************** spin lock ********************/
  99. usbhs_lock(priv, flags);
  100. if (!pkt)
  101. pkt = __usbhsf_pkt_get(pipe);
  102. if (pkt)
  103. __usbhsf_pkt_del(pkt);
  104. usbhs_unlock(priv, flags);
  105. /******************** spin unlock ******************/
  106. return pkt;
  107. }
  108. enum {
  109. USBHSF_PKT_PREPARE,
  110. USBHSF_PKT_TRY_RUN,
  111. USBHSF_PKT_DMA_DONE,
  112. };
  113. static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
  114. {
  115. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  116. struct usbhs_pkt *pkt;
  117. struct device *dev = usbhs_priv_to_dev(priv);
  118. int (*func)(struct usbhs_pkt *pkt, int *is_done);
  119. unsigned long flags;
  120. int ret = 0;
  121. int is_done = 0;
  122. /******************** spin lock ********************/
  123. usbhs_lock(priv, flags);
  124. pkt = __usbhsf_pkt_get(pipe);
  125. if (!pkt)
  126. goto __usbhs_pkt_handler_end;
  127. switch (type) {
  128. case USBHSF_PKT_PREPARE:
  129. func = pkt->handler->prepare;
  130. break;
  131. case USBHSF_PKT_TRY_RUN:
  132. func = pkt->handler->try_run;
  133. break;
  134. case USBHSF_PKT_DMA_DONE:
  135. func = pkt->handler->dma_done;
  136. break;
  137. default:
  138. dev_err(dev, "unknown pkt handler\n");
  139. goto __usbhs_pkt_handler_end;
  140. }
  141. ret = func(pkt, &is_done);
  142. if (is_done)
  143. __usbhsf_pkt_del(pkt);
  144. __usbhs_pkt_handler_end:
  145. usbhs_unlock(priv, flags);
  146. /******************** spin unlock ******************/
  147. if (is_done) {
  148. pkt->done(priv, pkt);
  149. usbhs_pkt_start(pipe);
  150. }
  151. return ret;
  152. }
  153. void usbhs_pkt_start(struct usbhs_pipe *pipe)
  154. {
  155. usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
  156. }
  157. /*
  158. * irq enable/disable function
  159. */
  160. #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
  161. #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
  162. #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
  163. ({ \
  164. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
  165. struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
  166. u16 status = (1 << usbhs_pipe_number(pipe)); \
  167. if (!mod) \
  168. return; \
  169. if (enable) \
  170. mod->status |= status; \
  171. else \
  172. mod->status &= ~status; \
  173. usbhs_irq_callback_update(priv, mod); \
  174. })
  175. static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  176. {
  177. /*
  178. * And DCP pipe can NOT use "ready interrupt" for "send"
  179. * it should use "empty" interrupt.
  180. * see
  181. * "Operation" - "Interrupt Function" - "BRDY Interrupt"
  182. *
  183. * on the other hand, normal pipe can use "ready interrupt" for "send"
  184. * even though it is single/double buffer
  185. */
  186. if (usbhs_pipe_is_dcp(pipe))
  187. usbhsf_irq_empty_ctrl(pipe, enable);
  188. else
  189. usbhsf_irq_ready_ctrl(pipe, enable);
  190. }
  191. static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
  192. {
  193. usbhsf_irq_ready_ctrl(pipe, enable);
  194. }
  195. /*
  196. * FIFO ctrl
  197. */
  198. static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
  199. struct usbhs_fifo *fifo)
  200. {
  201. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  202. usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
  203. }
  204. static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
  205. struct usbhs_fifo *fifo)
  206. {
  207. int timeout = 1024;
  208. do {
  209. /* The FIFO port is accessible */
  210. if (usbhs_read(priv, fifo->ctr) & FRDY)
  211. return 0;
  212. udelay(10);
  213. } while (timeout--);
  214. return -EBUSY;
  215. }
  216. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
  217. struct usbhs_fifo *fifo)
  218. {
  219. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  220. if (!usbhs_pipe_is_dcp(pipe))
  221. usbhsf_fifo_barrier(priv, fifo);
  222. usbhs_write(priv, fifo->ctr, BCLR);
  223. }
  224. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
  225. struct usbhs_fifo *fifo)
  226. {
  227. return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
  228. }
  229. static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
  230. struct usbhs_fifo *fifo)
  231. {
  232. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  233. usbhs_pipe_select_fifo(pipe, NULL);
  234. usbhs_write(priv, fifo->sel, 0);
  235. }
  236. static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
  237. struct usbhs_fifo *fifo,
  238. int write)
  239. {
  240. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  241. struct device *dev = usbhs_priv_to_dev(priv);
  242. int timeout = 1024;
  243. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  244. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  245. if (usbhs_pipe_is_busy(pipe) ||
  246. usbhsf_fifo_is_busy(fifo))
  247. return -EBUSY;
  248. if (usbhs_pipe_is_dcp(pipe)) {
  249. base |= (1 == write) << 5; /* ISEL */
  250. if (usbhs_mod_is_host(priv))
  251. usbhs_dcp_dir_for_host(pipe, write);
  252. }
  253. /* "base" will be used below */
  254. if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
  255. usbhs_write(priv, fifo->sel, base);
  256. else
  257. usbhs_write(priv, fifo->sel, base | MBW_32);
  258. /* check ISEL and CURPIPE value */
  259. while (timeout--) {
  260. if (base == (mask & usbhs_read(priv, fifo->sel))) {
  261. usbhs_pipe_select_fifo(pipe, fifo);
  262. return 0;
  263. }
  264. udelay(10);
  265. }
  266. dev_err(dev, "fifo select error\n");
  267. return -EIO;
  268. }
  269. /*
  270. * DCP status stage
  271. */
  272. static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
  273. {
  274. struct usbhs_pipe *pipe = pkt->pipe;
  275. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  276. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  277. struct device *dev = usbhs_priv_to_dev(priv);
  278. int ret;
  279. usbhs_pipe_disable(pipe);
  280. ret = usbhsf_fifo_select(pipe, fifo, 1);
  281. if (ret < 0) {
  282. dev_err(dev, "%s() faile\n", __func__);
  283. return ret;
  284. }
  285. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  286. usbhsf_fifo_clear(pipe, fifo);
  287. usbhsf_send_terminator(pipe, fifo);
  288. usbhsf_fifo_unselect(pipe, fifo);
  289. usbhsf_tx_irq_ctrl(pipe, 1);
  290. usbhs_pipe_enable(pipe);
  291. return ret;
  292. }
  293. static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
  294. {
  295. struct usbhs_pipe *pipe = pkt->pipe;
  296. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  297. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  298. struct device *dev = usbhs_priv_to_dev(priv);
  299. int ret;
  300. usbhs_pipe_disable(pipe);
  301. ret = usbhsf_fifo_select(pipe, fifo, 0);
  302. if (ret < 0) {
  303. dev_err(dev, "%s() fail\n", __func__);
  304. return ret;
  305. }
  306. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  307. usbhsf_fifo_clear(pipe, fifo);
  308. usbhsf_fifo_unselect(pipe, fifo);
  309. usbhsf_rx_irq_ctrl(pipe, 1);
  310. usbhs_pipe_enable(pipe);
  311. return ret;
  312. }
  313. static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
  314. {
  315. struct usbhs_pipe *pipe = pkt->pipe;
  316. if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
  317. usbhsf_tx_irq_ctrl(pipe, 0);
  318. else
  319. usbhsf_rx_irq_ctrl(pipe, 0);
  320. pkt->actual = pkt->length;
  321. *is_done = 1;
  322. return 0;
  323. }
  324. struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
  325. .prepare = usbhs_dcp_dir_switch_to_write,
  326. .try_run = usbhs_dcp_dir_switch_done,
  327. };
  328. struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
  329. .prepare = usbhs_dcp_dir_switch_to_read,
  330. .try_run = usbhs_dcp_dir_switch_done,
  331. };
  332. /*
  333. * DCP data stage (push)
  334. */
  335. static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
  336. {
  337. struct usbhs_pipe *pipe = pkt->pipe;
  338. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  339. /*
  340. * change handler to PIO push
  341. */
  342. pkt->handler = &usbhs_fifo_pio_push_handler;
  343. return pkt->handler->prepare(pkt, is_done);
  344. }
  345. struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
  346. .prepare = usbhsf_dcp_data_stage_try_push,
  347. };
  348. /*
  349. * DCP data stage (pop)
  350. */
  351. static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
  352. int *is_done)
  353. {
  354. struct usbhs_pipe *pipe = pkt->pipe;
  355. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  356. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
  357. if (usbhs_pipe_is_busy(pipe))
  358. return 0;
  359. /*
  360. * prepare pop for DCP should
  361. * - change DCP direction,
  362. * - clear fifo
  363. * - DATA1
  364. */
  365. usbhs_pipe_disable(pipe);
  366. usbhs_pipe_sequence_data1(pipe); /* DATA1 */
  367. usbhsf_fifo_select(pipe, fifo, 0);
  368. usbhsf_fifo_clear(pipe, fifo);
  369. usbhsf_fifo_unselect(pipe, fifo);
  370. /*
  371. * change handler to PIO pop
  372. */
  373. pkt->handler = &usbhs_fifo_pio_pop_handler;
  374. return pkt->handler->prepare(pkt, is_done);
  375. }
  376. struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
  377. .prepare = usbhsf_dcp_data_stage_prepare_pop,
  378. };
  379. /*
  380. * PIO push handler
  381. */
  382. static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
  383. {
  384. struct usbhs_pipe *pipe = pkt->pipe;
  385. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  386. struct device *dev = usbhs_priv_to_dev(priv);
  387. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  388. void __iomem *addr = priv->base + fifo->port;
  389. u8 *buf;
  390. int maxp = usbhs_pipe_get_maxpacket(pipe);
  391. int total_len;
  392. int i, ret, len;
  393. int is_short;
  394. usbhs_pipe_data_sequence(pipe, pkt->sequence);
  395. pkt->sequence = -1; /* -1 sequence will be ignored */
  396. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
  397. ret = usbhsf_fifo_select(pipe, fifo, 1);
  398. if (ret < 0)
  399. return 0;
  400. ret = usbhs_pipe_is_accessible(pipe);
  401. if (ret < 0) {
  402. /* inaccessible pipe is not an error */
  403. ret = 0;
  404. goto usbhs_fifo_write_busy;
  405. }
  406. ret = usbhsf_fifo_barrier(priv, fifo);
  407. if (ret < 0)
  408. goto usbhs_fifo_write_busy;
  409. buf = pkt->buf + pkt->actual;
  410. len = pkt->length - pkt->actual;
  411. len = min(len, maxp);
  412. total_len = len;
  413. is_short = total_len < maxp;
  414. /*
  415. * FIXME
  416. *
  417. * 32-bit access only
  418. */
  419. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  420. iowrite32_rep(addr, buf, len / 4);
  421. len %= 4;
  422. buf += total_len - len;
  423. }
  424. /* the rest operation */
  425. for (i = 0; i < len; i++)
  426. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  427. /*
  428. * variable update
  429. */
  430. pkt->actual += total_len;
  431. if (pkt->actual < pkt->length)
  432. *is_done = 0; /* there are remainder data */
  433. else if (is_short)
  434. *is_done = 1; /* short packet */
  435. else
  436. *is_done = !pkt->zero; /* send zero packet ? */
  437. /*
  438. * pipe/irq handling
  439. */
  440. if (is_short)
  441. usbhsf_send_terminator(pipe, fifo);
  442. usbhsf_tx_irq_ctrl(pipe, !*is_done);
  443. usbhs_pipe_enable(pipe);
  444. dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
  445. usbhs_pipe_number(pipe),
  446. pkt->length, pkt->actual, *is_done, pkt->zero);
  447. /*
  448. * Transmission end
  449. */
  450. if (*is_done) {
  451. if (usbhs_pipe_is_dcp(pipe))
  452. usbhs_dcp_control_transfer_done(pipe);
  453. }
  454. usbhsf_fifo_unselect(pipe, fifo);
  455. return 0;
  456. usbhs_fifo_write_busy:
  457. usbhsf_fifo_unselect(pipe, fifo);
  458. /*
  459. * pipe is busy.
  460. * retry in interrupt
  461. */
  462. usbhsf_tx_irq_ctrl(pipe, 1);
  463. return ret;
  464. }
  465. struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
  466. .prepare = usbhsf_pio_try_push,
  467. .try_run = usbhsf_pio_try_push,
  468. };
  469. /*
  470. * PIO pop handler
  471. */
  472. static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
  473. {
  474. struct usbhs_pipe *pipe = pkt->pipe;
  475. if (usbhs_pipe_is_busy(pipe))
  476. return 0;
  477. /*
  478. * pipe enable to prepare packet receive
  479. */
  480. usbhs_pipe_data_sequence(pipe, pkt->sequence);
  481. pkt->sequence = -1; /* -1 sequence will be ignored */
  482. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
  483. usbhs_pipe_enable(pipe);
  484. usbhsf_rx_irq_ctrl(pipe, 1);
  485. return 0;
  486. }
  487. static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
  488. {
  489. struct usbhs_pipe *pipe = pkt->pipe;
  490. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  491. struct device *dev = usbhs_priv_to_dev(priv);
  492. struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
  493. void __iomem *addr = priv->base + fifo->port;
  494. u8 *buf;
  495. u32 data = 0;
  496. int maxp = usbhs_pipe_get_maxpacket(pipe);
  497. int rcv_len, len;
  498. int i, ret;
  499. int total_len = 0;
  500. ret = usbhsf_fifo_select(pipe, fifo, 0);
  501. if (ret < 0)
  502. return 0;
  503. ret = usbhsf_fifo_barrier(priv, fifo);
  504. if (ret < 0)
  505. goto usbhs_fifo_read_busy;
  506. rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
  507. buf = pkt->buf + pkt->actual;
  508. len = pkt->length - pkt->actual;
  509. len = min(len, rcv_len);
  510. total_len = len;
  511. /*
  512. * update actual length first here to decide disable pipe.
  513. * if this pipe keeps BUF status and all data were popped,
  514. * then, next interrupt/token will be issued again
  515. */
  516. pkt->actual += total_len;
  517. if ((pkt->actual == pkt->length) || /* receive all data */
  518. (total_len < maxp)) { /* short packet */
  519. *is_done = 1;
  520. usbhsf_rx_irq_ctrl(pipe, 0);
  521. usbhs_pipe_disable(pipe); /* disable pipe first */
  522. }
  523. /*
  524. * Buffer clear if Zero-Length packet
  525. *
  526. * see
  527. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  528. */
  529. if (0 == rcv_len) {
  530. pkt->zero = 1;
  531. usbhsf_fifo_clear(pipe, fifo);
  532. goto usbhs_fifo_read_end;
  533. }
  534. /*
  535. * FIXME
  536. *
  537. * 32-bit access only
  538. */
  539. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  540. ioread32_rep(addr, buf, len / 4);
  541. len %= 4;
  542. buf += total_len - len;
  543. }
  544. /* the rest operation */
  545. for (i = 0; i < len; i++) {
  546. if (!(i & 0x03))
  547. data = ioread32(addr);
  548. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  549. }
  550. usbhs_fifo_read_end:
  551. dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
  552. usbhs_pipe_number(pipe),
  553. pkt->length, pkt->actual, *is_done, pkt->zero);
  554. usbhs_fifo_read_busy:
  555. usbhsf_fifo_unselect(pipe, fifo);
  556. return ret;
  557. }
  558. struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
  559. .prepare = usbhsf_prepare_pop,
  560. .try_run = usbhsf_pio_try_pop,
  561. };
  562. /*
  563. * DCP ctrol statge handler
  564. */
  565. static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
  566. {
  567. usbhs_dcp_control_transfer_done(pkt->pipe);
  568. *is_done = 1;
  569. return 0;
  570. }
  571. struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
  572. .prepare = usbhsf_ctrl_stage_end,
  573. .try_run = usbhsf_ctrl_stage_end,
  574. };
  575. /*
  576. * DMA fifo functions
  577. */
  578. static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
  579. struct usbhs_pkt *pkt)
  580. {
  581. if (&usbhs_fifo_dma_push_handler == pkt->handler)
  582. return fifo->tx_chan;
  583. if (&usbhs_fifo_dma_pop_handler == pkt->handler)
  584. return fifo->rx_chan;
  585. return NULL;
  586. }
  587. static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
  588. struct usbhs_pkt *pkt)
  589. {
  590. struct usbhs_fifo *fifo;
  591. /* DMA :: D0FIFO */
  592. fifo = usbhsf_get_d0fifo(priv);
  593. if (usbhsf_dma_chan_get(fifo, pkt) &&
  594. !usbhsf_fifo_is_busy(fifo))
  595. return fifo;
  596. /* DMA :: D1FIFO */
  597. fifo = usbhsf_get_d1fifo(priv);
  598. if (usbhsf_dma_chan_get(fifo, pkt) &&
  599. !usbhsf_fifo_is_busy(fifo))
  600. return fifo;
  601. return NULL;
  602. }
  603. #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
  604. #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
  605. static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
  606. struct usbhs_fifo *fifo,
  607. u16 dreqe)
  608. {
  609. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  610. usbhs_bset(priv, fifo->sel, DREQE, dreqe);
  611. }
  612. #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
  613. #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
  614. static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  615. {
  616. struct usbhs_pipe *pipe = pkt->pipe;
  617. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  618. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  619. return info->dma_map_ctrl(pkt, map);
  620. }
  621. static void usbhsf_dma_complete(void *arg);
  622. static void xfer_work(struct work_struct *work)
  623. {
  624. struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
  625. struct usbhs_pipe *pipe = pkt->pipe;
  626. struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
  627. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  628. struct dma_async_tx_descriptor *desc;
  629. struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
  630. struct device *dev = usbhs_priv_to_dev(priv);
  631. enum dma_transfer_direction dir;
  632. dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  633. desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
  634. pkt->trans, dir,
  635. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  636. if (!desc)
  637. return;
  638. desc->callback = usbhsf_dma_complete;
  639. desc->callback_param = pipe;
  640. if (dmaengine_submit(desc) < 0) {
  641. dev_err(dev, "Failed to submit dma descriptor\n");
  642. return;
  643. }
  644. dev_dbg(dev, " %s %d (%d/ %d)\n",
  645. fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
  646. usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
  647. usbhs_pipe_enable(pipe);
  648. usbhsf_dma_start(pipe, fifo);
  649. dma_async_issue_pending(chan);
  650. }
  651. /*
  652. * DMA push handler
  653. */
  654. static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
  655. {
  656. struct usbhs_pipe *pipe = pkt->pipe;
  657. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  658. struct usbhs_fifo *fifo;
  659. int len = pkt->length - pkt->actual;
  660. int ret;
  661. if (usbhs_pipe_is_busy(pipe))
  662. return 0;
  663. /* use PIO if packet is less than pio_dma_border or pipe is DCP */
  664. if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
  665. usbhs_pipe_is_dcp(pipe))
  666. goto usbhsf_pio_prepare_push;
  667. if (len & 0x7) /* 8byte alignment */
  668. goto usbhsf_pio_prepare_push;
  669. if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  670. goto usbhsf_pio_prepare_push;
  671. /* get enable DMA fifo */
  672. fifo = usbhsf_get_dma_fifo(priv, pkt);
  673. if (!fifo)
  674. goto usbhsf_pio_prepare_push;
  675. if (usbhsf_dma_map(pkt) < 0)
  676. goto usbhsf_pio_prepare_push;
  677. ret = usbhsf_fifo_select(pipe, fifo, 0);
  678. if (ret < 0)
  679. goto usbhsf_pio_prepare_push_unmap;
  680. pkt->trans = len;
  681. INIT_WORK(&pkt->work, xfer_work);
  682. schedule_work(&pkt->work);
  683. return 0;
  684. usbhsf_pio_prepare_push_unmap:
  685. usbhsf_dma_unmap(pkt);
  686. usbhsf_pio_prepare_push:
  687. /*
  688. * change handler to PIO
  689. */
  690. pkt->handler = &usbhs_fifo_pio_push_handler;
  691. return pkt->handler->prepare(pkt, is_done);
  692. }
  693. static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
  694. {
  695. struct usbhs_pipe *pipe = pkt->pipe;
  696. pkt->actual = pkt->trans;
  697. *is_done = !pkt->zero; /* send zero packet ? */
  698. usbhsf_dma_stop(pipe, pipe->fifo);
  699. usbhsf_dma_unmap(pkt);
  700. usbhsf_fifo_unselect(pipe, pipe->fifo);
  701. return 0;
  702. }
  703. struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
  704. .prepare = usbhsf_dma_prepare_push,
  705. .dma_done = usbhsf_dma_push_done,
  706. };
  707. /*
  708. * DMA pop handler
  709. */
  710. static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
  711. {
  712. struct usbhs_pipe *pipe = pkt->pipe;
  713. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  714. struct usbhs_fifo *fifo;
  715. int len, ret;
  716. if (usbhs_pipe_is_busy(pipe))
  717. return 0;
  718. if (usbhs_pipe_is_dcp(pipe))
  719. goto usbhsf_pio_prepare_pop;
  720. /* get enable DMA fifo */
  721. fifo = usbhsf_get_dma_fifo(priv, pkt);
  722. if (!fifo)
  723. goto usbhsf_pio_prepare_pop;
  724. if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
  725. goto usbhsf_pio_prepare_pop;
  726. ret = usbhsf_fifo_select(pipe, fifo, 0);
  727. if (ret < 0)
  728. goto usbhsf_pio_prepare_pop;
  729. /* use PIO if packet is less than pio_dma_border */
  730. len = usbhsf_fifo_rcv_len(priv, fifo);
  731. len = min(pkt->length - pkt->actual, len);
  732. if (len & 0x7) /* 8byte alignment */
  733. goto usbhsf_pio_prepare_pop_unselect;
  734. if (len < usbhs_get_dparam(priv, pio_dma_border))
  735. goto usbhsf_pio_prepare_pop_unselect;
  736. ret = usbhsf_fifo_barrier(priv, fifo);
  737. if (ret < 0)
  738. goto usbhsf_pio_prepare_pop_unselect;
  739. if (usbhsf_dma_map(pkt) < 0)
  740. goto usbhsf_pio_prepare_pop_unselect;
  741. /* DMA */
  742. /*
  743. * usbhs_fifo_dma_pop_handler :: prepare
  744. * enabled irq to come here.
  745. * but it is no longer needed for DMA. disable it.
  746. */
  747. usbhsf_rx_irq_ctrl(pipe, 0);
  748. pkt->trans = len;
  749. INIT_WORK(&pkt->work, xfer_work);
  750. schedule_work(&pkt->work);
  751. return 0;
  752. usbhsf_pio_prepare_pop_unselect:
  753. usbhsf_fifo_unselect(pipe, fifo);
  754. usbhsf_pio_prepare_pop:
  755. /*
  756. * change handler to PIO
  757. */
  758. pkt->handler = &usbhs_fifo_pio_pop_handler;
  759. return pkt->handler->try_run(pkt, is_done);
  760. }
  761. static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
  762. {
  763. struct usbhs_pipe *pipe = pkt->pipe;
  764. int maxp = usbhs_pipe_get_maxpacket(pipe);
  765. usbhsf_dma_stop(pipe, pipe->fifo);
  766. usbhsf_dma_unmap(pkt);
  767. usbhsf_fifo_unselect(pipe, pipe->fifo);
  768. pkt->actual += pkt->trans;
  769. if ((pkt->actual == pkt->length) || /* receive all data */
  770. (pkt->trans < maxp)) { /* short packet */
  771. *is_done = 1;
  772. } else {
  773. /* re-enable */
  774. usbhsf_prepare_pop(pkt, is_done);
  775. }
  776. return 0;
  777. }
  778. struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
  779. .prepare = usbhsf_prepare_pop,
  780. .try_run = usbhsf_dma_try_pop,
  781. .dma_done = usbhsf_dma_pop_done
  782. };
  783. /*
  784. * DMA setting
  785. */
  786. static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
  787. {
  788. struct sh_dmae_slave *slave = param;
  789. /*
  790. * FIXME
  791. *
  792. * usbhs doesn't recognize id = 0 as valid DMA
  793. */
  794. if (0 == slave->shdma_slave.slave_id)
  795. return false;
  796. chan->private = slave;
  797. return true;
  798. }
  799. static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
  800. {
  801. if (fifo->tx_chan)
  802. dma_release_channel(fifo->tx_chan);
  803. if (fifo->rx_chan)
  804. dma_release_channel(fifo->rx_chan);
  805. fifo->tx_chan = NULL;
  806. fifo->rx_chan = NULL;
  807. }
  808. static void usbhsf_dma_init(struct usbhs_priv *priv,
  809. struct usbhs_fifo *fifo)
  810. {
  811. struct device *dev = usbhs_priv_to_dev(priv);
  812. dma_cap_mask_t mask;
  813. dma_cap_zero(mask);
  814. dma_cap_set(DMA_SLAVE, mask);
  815. fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  816. &fifo->tx_slave);
  817. dma_cap_zero(mask);
  818. dma_cap_set(DMA_SLAVE, mask);
  819. fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
  820. &fifo->rx_slave);
  821. if (fifo->tx_chan || fifo->rx_chan)
  822. dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
  823. fifo->name,
  824. fifo->tx_chan ? "[TX]" : " ",
  825. fifo->rx_chan ? "[RX]" : " ");
  826. }
  827. /*
  828. * irq functions
  829. */
  830. static int usbhsf_irq_empty(struct usbhs_priv *priv,
  831. struct usbhs_irq_state *irq_state)
  832. {
  833. struct usbhs_pipe *pipe;
  834. struct device *dev = usbhs_priv_to_dev(priv);
  835. int i, ret;
  836. if (!irq_state->bempsts) {
  837. dev_err(dev, "debug %s !!\n", __func__);
  838. return -EIO;
  839. }
  840. dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
  841. /*
  842. * search interrupted "pipe"
  843. * not "uep".
  844. */
  845. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  846. if (!(irq_state->bempsts & (1 << i)))
  847. continue;
  848. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  849. if (ret < 0)
  850. dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
  851. }
  852. return 0;
  853. }
  854. static int usbhsf_irq_ready(struct usbhs_priv *priv,
  855. struct usbhs_irq_state *irq_state)
  856. {
  857. struct usbhs_pipe *pipe;
  858. struct device *dev = usbhs_priv_to_dev(priv);
  859. int i, ret;
  860. if (!irq_state->brdysts) {
  861. dev_err(dev, "debug %s !!\n", __func__);
  862. return -EIO;
  863. }
  864. dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
  865. /*
  866. * search interrupted "pipe"
  867. * not "uep".
  868. */
  869. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  870. if (!(irq_state->brdysts & (1 << i)))
  871. continue;
  872. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
  873. if (ret < 0)
  874. dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
  875. }
  876. return 0;
  877. }
  878. static void usbhsf_dma_complete(void *arg)
  879. {
  880. struct usbhs_pipe *pipe = arg;
  881. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  882. struct device *dev = usbhs_priv_to_dev(priv);
  883. int ret;
  884. ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
  885. if (ret < 0)
  886. dev_err(dev, "dma_complete run_error %d : %d\n",
  887. usbhs_pipe_number(pipe), ret);
  888. }
  889. /*
  890. * fifo init
  891. */
  892. void usbhs_fifo_init(struct usbhs_priv *priv)
  893. {
  894. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  895. struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
  896. struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
  897. struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
  898. mod->irq_empty = usbhsf_irq_empty;
  899. mod->irq_ready = usbhsf_irq_ready;
  900. mod->irq_bempsts = 0;
  901. mod->irq_brdysts = 0;
  902. cfifo->pipe = NULL;
  903. cfifo->tx_chan = NULL;
  904. cfifo->rx_chan = NULL;
  905. d0fifo->pipe = NULL;
  906. d0fifo->tx_chan = NULL;
  907. d0fifo->rx_chan = NULL;
  908. d1fifo->pipe = NULL;
  909. d1fifo->tx_chan = NULL;
  910. d1fifo->rx_chan = NULL;
  911. usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
  912. usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
  913. }
  914. void usbhs_fifo_quit(struct usbhs_priv *priv)
  915. {
  916. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  917. mod->irq_empty = NULL;
  918. mod->irq_ready = NULL;
  919. mod->irq_bempsts = 0;
  920. mod->irq_brdysts = 0;
  921. usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
  922. usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
  923. }
  924. int usbhs_fifo_probe(struct usbhs_priv *priv)
  925. {
  926. struct usbhs_fifo *fifo;
  927. /* CFIFO */
  928. fifo = usbhsf_get_cfifo(priv);
  929. fifo->name = "CFIFO";
  930. fifo->port = CFIFO;
  931. fifo->sel = CFIFOSEL;
  932. fifo->ctr = CFIFOCTR;
  933. /* D0FIFO */
  934. fifo = usbhsf_get_d0fifo(priv);
  935. fifo->name = "D0FIFO";
  936. fifo->port = D0FIFO;
  937. fifo->sel = D0FIFOSEL;
  938. fifo->ctr = D0FIFOCTR;
  939. fifo->tx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
  940. fifo->rx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
  941. /* D1FIFO */
  942. fifo = usbhsf_get_d1fifo(priv);
  943. fifo->name = "D1FIFO";
  944. fifo->port = D1FIFO;
  945. fifo->sel = D1FIFOSEL;
  946. fifo->ctr = D1FIFOCTR;
  947. fifo->tx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
  948. fifo->rx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
  949. return 0;
  950. }
  951. void usbhs_fifo_remove(struct usbhs_priv *priv)
  952. {
  953. }