fifo.c 28 KB

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