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