4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
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
17 #include <linux/delay.h>
19 #include <linux/slab.h>
26 #define usbhsp_priv_to_pipeinfo(pr) (&(pr)->pipe_info)
27 #define usbhsp_pipe_to_priv(p) ((p)->priv)
29 #define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2)
31 #define usbhsp_is_dcp(p) ((p)->priv->pipe_info.pipe == (p))
33 #define usbhsp_flags_set(p, f) ((p)->flags |= USBHS_PIPE_FLAGS_##f)
34 #define usbhsp_flags_clr(p, f) ((p)->flags &= ~USBHS_PIPE_FLAGS_##f)
35 #define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f)
36 #define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0)
38 #define usbhsp_type(p) ((p)->pipe_type)
39 #define usbhsp_type_is(p, t) ((p)->pipe_type == t)
44 static char *usbhsp_pipe_name
[] = {
45 [USB_ENDPOINT_XFER_CONTROL
] = "DCP",
46 [USB_ENDPOINT_XFER_BULK
] = "BULK",
47 [USB_ENDPOINT_XFER_INT
] = "INT",
48 [USB_ENDPOINT_XFER_ISOC
] = "ISO",
52 * usb request functions
54 void usbhs_usbreq_get_val(struct usbhs_priv
*priv
, struct usb_ctrlrequest
*req
)
58 val
= usbhs_read(priv
, USBREQ
);
59 req
->bRequest
= (val
>> 8) & 0xFF;
60 req
->bRequestType
= (val
>> 0) & 0xFF;
62 req
->wValue
= usbhs_read(priv
, USBVAL
);
63 req
->wIndex
= usbhs_read(priv
, USBINDX
);
64 req
->wLength
= usbhs_read(priv
, USBLENG
);
67 void usbhs_usbreq_set_val(struct usbhs_priv
*priv
, struct usb_ctrlrequest
*req
)
69 usbhs_write(priv
, USBREQ
, (req
->bRequest
<< 8) | req
->bRequestType
);
70 usbhs_write(priv
, USBVAL
, req
->wValue
);
71 usbhs_write(priv
, USBINDX
, req
->wIndex
);
72 usbhs_write(priv
, USBLENG
, req
->wLength
);
76 * DCPCTR/PIPEnCTR functions
78 static void usbhsp_pipectrl_set(struct usbhs_pipe
*pipe
, u16 mask
, u16 val
)
80 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
81 int offset
= usbhsp_addr_offset(pipe
);
83 if (usbhsp_is_dcp(pipe
))
84 usbhs_bset(priv
, DCPCTR
, mask
, val
);
86 usbhs_bset(priv
, PIPEnCTR
+ offset
, mask
, val
);
89 static u16
usbhsp_pipectrl_get(struct usbhs_pipe
*pipe
)
91 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
92 int offset
= usbhsp_addr_offset(pipe
);
94 if (usbhsp_is_dcp(pipe
))
95 return usbhs_read(priv
, DCPCTR
);
97 return usbhs_read(priv
, PIPEnCTR
+ offset
);
103 static void __usbhsp_pipe_xxx_set(struct usbhs_pipe
*pipe
,
104 u16 dcp_reg
, u16 pipe_reg
,
107 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
109 if (usbhsp_is_dcp(pipe
))
110 usbhs_bset(priv
, dcp_reg
, mask
, val
);
112 usbhs_bset(priv
, pipe_reg
, mask
, val
);
115 static u16
__usbhsp_pipe_xxx_get(struct usbhs_pipe
*pipe
,
116 u16 dcp_reg
, u16 pipe_reg
)
118 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
120 if (usbhsp_is_dcp(pipe
))
121 return usbhs_read(priv
, dcp_reg
);
123 return usbhs_read(priv
, pipe_reg
);
127 * DCPCFG/PIPECFG functions
129 static void usbhsp_pipe_cfg_set(struct usbhs_pipe
*pipe
, u16 mask
, u16 val
)
131 __usbhsp_pipe_xxx_set(pipe
, DCPCFG
, PIPECFG
, mask
, val
);
137 static void usbhsp_pipe_buf_set(struct usbhs_pipe
*pipe
, u16 mask
, u16 val
)
139 if (usbhsp_is_dcp(pipe
))
142 __usbhsp_pipe_xxx_set(pipe
, 0, PIPEBUF
, mask
, val
);
148 static void usbhsp_pipe_maxp_set(struct usbhs_pipe
*pipe
, u16 mask
, u16 val
)
150 __usbhsp_pipe_xxx_set(pipe
, DCPMAXP
, PIPEMAXP
, mask
, val
);
153 static u16
usbhsp_pipe_maxp_get(struct usbhs_pipe
*pipe
)
155 return __usbhsp_pipe_xxx_get(pipe
, DCPMAXP
, PIPEMAXP
);
159 * pipe control functions
161 static void usbhsp_pipe_select(struct usbhs_pipe
*pipe
)
163 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
166 * On pipe, this is necessary before
167 * accesses to below registers.
169 * PIPESEL : usbhsp_pipe_select
170 * PIPECFG : usbhsp_pipe_cfg_xxx
171 * PIPEBUF : usbhsp_pipe_buf_xxx
172 * PIPEMAXP : usbhsp_pipe_maxp_xxx
177 * if pipe is dcp, no pipe is selected.
178 * it is no problem, because dcp have its register
180 usbhs_write(priv
, PIPESEL
, 0xF & usbhs_pipe_number(pipe
));
183 static int usbhsp_pipe_barrier(struct usbhs_pipe
*pipe
)
185 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
192 * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
193 * specified by the CURPIPE bits.
194 * When changing the setting of this bit after changing
195 * the PID bits for the selected pipe from BUF to NAK,
196 * check that CSSTS = 0 and PBUSY = 0.
205 * - "Pipe Control Registers Switching Procedure"
207 usbhs_write(priv
, CFIFOSEL
, 0);
208 usbhs_fifo_disable(pipe
);
211 val
= usbhsp_pipectrl_get(pipe
);
212 val
&= CSSTS
| PID_MASK
;
223 static int usbhsp_pipe_is_accessible(struct usbhs_pipe
*pipe
)
227 val
= usbhsp_pipectrl_get(pipe
);
237 static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe
*pipe
)
239 u16 pid
= usbhsp_pipectrl_get(pipe
);
245 * "Pipe n Control Register" - "PID"
249 usbhsp_pipectrl_set(pipe
, PID_MASK
, PID_STALL10
);
252 usbhsp_pipectrl_set(pipe
, PID_MASK
, PID_NAK
);
256 void usbhs_fifo_disable(struct usbhs_pipe
*pipe
)
261 /* see "Pipe n Control Register" - "PID" */
262 __usbhsp_pid_try_nak_if_stall(pipe
);
264 usbhsp_pipectrl_set(pipe
, PID_MASK
, PID_NAK
);
267 val
= usbhsp_pipectrl_get(pipe
);
276 void usbhs_fifo_enable(struct usbhs_pipe
*pipe
)
278 /* see "Pipe n Control Register" - "PID" */
279 __usbhsp_pid_try_nak_if_stall(pipe
);
281 usbhsp_pipectrl_set(pipe
, PID_MASK
, PID_BUF
);
284 void usbhs_fifo_stall(struct usbhs_pipe
*pipe
)
286 u16 pid
= usbhsp_pipectrl_get(pipe
);
292 * "Pipe n Control Register" - "PID"
296 usbhsp_pipectrl_set(pipe
, PID_MASK
, PID_STALL10
);
299 usbhsp_pipectrl_set(pipe
, PID_MASK
, PID_STALL11
);
307 void usbhs_fifo_send_terminator(struct usbhs_pipe
*pipe
)
309 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
311 usbhs_bset(priv
, CFIFOCTR
, BVAL
, BVAL
);
314 static void usbhsp_fifo_clear(struct usbhs_pipe
*pipe
)
316 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
318 usbhs_write(priv
, CFIFOCTR
, BCLR
);
321 static int usbhsp_fifo_barrier(struct usbhs_priv
*priv
)
326 /* The FIFO port is accessible */
327 if (usbhs_read(priv
, CFIFOCTR
) & FRDY
)
336 static int usbhsp_fifo_rcv_len(struct usbhs_priv
*priv
)
338 return usbhs_read(priv
, CFIFOCTR
) & DTLN_MASK
;
341 static int usbhsp_fifo_select(struct usbhs_pipe
*pipe
, int write
)
343 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
344 struct device
*dev
= usbhs_priv_to_dev(priv
);
346 u16 mask
= ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
347 u16 base
= usbhs_pipe_number(pipe
); /* CURPIPE */
349 if (usbhsp_is_dcp(pipe
))
350 base
|= (1 == write
) << 5; /* ISEL */
352 /* "base" will be used below */
353 usbhs_write(priv
, CFIFOSEL
, base
| MBW_32
);
355 /* check ISEL and CURPIPE value */
357 if (base
== (mask
& usbhs_read(priv
, CFIFOSEL
)))
362 dev_err(dev
, "fifo select error\n");
367 int usbhs_fifo_prepare_write(struct usbhs_pipe
*pipe
)
369 return usbhsp_fifo_select(pipe
, 1);
372 int usbhs_fifo_write(struct usbhs_pipe
*pipe
, u8
*buf
, int len
)
374 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
375 void __iomem
*addr
= priv
->base
+ CFIFO
;
376 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
380 ret
= usbhsp_pipe_is_accessible(pipe
);
384 ret
= usbhsp_fifo_select(pipe
, 1);
388 ret
= usbhsp_fifo_barrier(priv
);
392 len
= min(len
, maxp
);
401 !((unsigned long)buf
& 0x03)) {
402 iowrite32_rep(addr
, buf
, len
/ 4);
404 buf
+= total_len
- len
;
407 /* the rest operation */
408 for (i
= 0; i
< len
; i
++)
409 iowrite8(buf
[i
], addr
+ (0x03 - (i
& 0x03)));
411 if (total_len
< maxp
)
412 usbhs_fifo_send_terminator(pipe
);
417 int usbhs_fifo_prepare_read(struct usbhs_pipe
*pipe
)
422 * select pipe and enable it to prepare packet receive
424 ret
= usbhsp_fifo_select(pipe
, 0);
428 usbhs_fifo_enable(pipe
);
433 int usbhs_fifo_read(struct usbhs_pipe
*pipe
, u8
*buf
, int len
)
435 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
436 void __iomem
*addr
= priv
->base
+ CFIFO
;
442 ret
= usbhsp_fifo_select(pipe
, 0);
446 ret
= usbhsp_fifo_barrier(priv
);
450 rcv_len
= usbhsp_fifo_rcv_len(priv
);
453 * Buffer clear if Zero-Length packet
456 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
459 usbhsp_fifo_clear(pipe
);
463 len
= min(rcv_len
, len
);
472 !((unsigned long)buf
& 0x03)) {
473 ioread32_rep(addr
, buf
, len
/ 4);
475 buf
+= rcv_len
- len
;
478 /* the rest operation */
479 for (i
= 0; i
< len
; i
++) {
481 data
= ioread32(addr
);
483 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
492 static int usbhsp_possible_double_buffer(struct usbhs_pipe
*pipe
)
495 * only ISO / BULK pipe can use double buffer
497 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_BULK
) ||
498 usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_ISOC
))
504 static u16
usbhsp_setup_pipecfg(struct usbhs_pipe
*pipe
,
505 const struct usb_endpoint_descriptor
*desc
,
516 [USB_ENDPOINT_XFER_BULK
] = TYPE_BULK
,
517 [USB_ENDPOINT_XFER_INT
] = TYPE_INT
,
518 [USB_ENDPOINT_XFER_ISOC
] = TYPE_ISO
,
520 int is_double
= usbhsp_possible_double_buffer(pipe
);
522 if (usbhsp_is_dcp(pipe
))
529 * - "Register Descriptions" - "PIPECFG" register
530 * - "Features" - "Pipe configuration"
531 * - "Operation" - "Pipe Control"
535 type
= type_array
[usbhsp_type(pipe
)];
538 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_ISOC
) ||
539 usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_BULK
))
540 bfre
= 0; /* FIXME */
543 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_ISOC
) ||
544 usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_BULK
))
545 dblb
= (is_double
) ? DBLB
: 0;
548 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_BULK
))
549 cntmd
= 0; /* FIXME */
552 if (usb_endpoint_dir_in(desc
))
553 usbhsp_flags_set(pipe
, IS_DIR_IN
);
555 if ((is_host
&& usb_endpoint_dir_out(desc
)) ||
556 (!is_host
&& usb_endpoint_dir_in(desc
)))
560 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_BULK
) &&
565 epnum
= 0xF & usb_endpoint_num(desc
);
576 static u16
usbhsp_setup_pipemaxp(struct usbhs_pipe
*pipe
,
577 const struct usb_endpoint_descriptor
*desc
,
580 /* host should set DEVSEL */
583 return PIPE_MAXP_MASK
& le16_to_cpu(desc
->wMaxPacketSize
);
586 static u16
usbhsp_setup_pipebuff(struct usbhs_pipe
*pipe
,
587 const struct usb_endpoint_descriptor
*desc
,
590 struct usbhs_priv
*priv
= usbhsp_pipe_to_priv(pipe
);
591 struct usbhs_pipe_info
*info
= usbhsp_priv_to_pipeinfo(priv
);
592 struct device
*dev
= usbhs_priv_to_dev(priv
);
593 int pipe_num
= usbhs_pipe_number(pipe
);
594 int is_double
= usbhsp_possible_double_buffer(pipe
);
603 * - "Register Descriptions" - "PIPEBUF" register
604 * - "Features" - "Pipe configuration"
605 * - "Operation" - "FIFO Buffer Memory"
606 * - "Operation" - "Pipe Control"
608 * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
611 * 0: pipe0 (DCP 256byte)
615 * 4: pipe6 (INT 64byte)
616 * 5: pipe7 (INT 64byte)
617 * 6: pipe8 (INT 64byte)
618 * 7: pipe9 (INT 64byte)
619 * 8 - xx: free (for BULK, ISOC)
625 * it doesn't have good buffer allocator
632 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_CONTROL
))
634 else if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_INT
))
639 /* change buff_size to register value */
640 bufnmb_cnt
= (buff_size
/ 64) - 1;
642 /* BUFNMB has been reserved for INT pipe
644 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_INT
)) {
645 bufnmb
= pipe_num
- 2;
647 bufnmb
= info
->bufnmb_last
;
648 info
->bufnmb_last
+= bufnmb_cnt
+ 1;
654 info
->bufnmb_last
+= bufnmb_cnt
+ 1;
657 dev_dbg(dev
, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
658 pipe_num
, buff_size
, bufnmb
);
660 return (0x1f & bufnmb_cnt
) << 10 |
661 (0xff & bufnmb
) << 0;
667 int usbhs_pipe_get_maxpacket(struct usbhs_pipe
*pipe
)
669 u16 mask
= usbhsp_is_dcp(pipe
) ? DCP_MAXP_MASK
: PIPE_MAXP_MASK
;
671 usbhsp_pipe_select(pipe
);
673 return (int)(usbhsp_pipe_maxp_get(pipe
) & mask
);
676 int usbhs_pipe_is_dir_in(struct usbhs_pipe
*pipe
)
678 return usbhsp_flags_has(pipe
, IS_DIR_IN
);
681 void usbhs_pipe_clear_sequence(struct usbhs_pipe
*pipe
)
683 usbhsp_pipectrl_set(pipe
, SQCLR
, SQCLR
);
686 static struct usbhs_pipe
*usbhsp_get_pipe(struct usbhs_priv
*priv
, u32 type
)
688 struct usbhs_pipe
*pos
, *pipe
;
695 usbhs_for_each_pipe_with_dcp(pos
, priv
, i
) {
696 if (!usbhsp_type_is(pos
, type
))
698 if (usbhsp_flags_has(pos
, IS_USED
))
709 * initialize pipe flags
711 usbhsp_flags_init(pipe
);
712 usbhsp_flags_set(pipe
, IS_USED
);
717 void usbhs_pipe_init(struct usbhs_priv
*priv
)
719 struct usbhs_pipe_info
*info
= usbhsp_priv_to_pipeinfo(priv
);
720 struct usbhs_pipe
*pipe
;
726 * driver needs good allocator.
728 * find first free buffer area (BULK, ISOC)
729 * (DCP, INT area is fixed)
731 * buffer number 0 - 3 have been reserved for DCP
735 info
->bufnmb_last
= 4;
736 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
737 if (usbhsp_type_is(pipe
, USB_ENDPOINT_XFER_INT
))
740 usbhsp_flags_init(pipe
);
741 pipe
->mod_private
= NULL
;
743 usbhsp_fifo_clear(pipe
);
747 struct usbhs_pipe
*usbhs_pipe_malloc(struct usbhs_priv
*priv
,
748 const struct usb_endpoint_descriptor
*desc
)
750 struct device
*dev
= usbhs_priv_to_dev(priv
);
751 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
752 struct usbhs_pipe
*pipe
;
753 int is_host
= usbhs_mod_is_host(priv
, mod
);
755 u16 pipecfg
, pipebuf
, pipemaxp
;
757 pipe
= usbhsp_get_pipe(priv
, usb_endpoint_type(desc
));
759 dev_err(dev
, "can't get pipe (%s)\n",
760 usbhsp_pipe_name
[usb_endpoint_type(desc
)]);
764 usbhs_fifo_disable(pipe
);
766 /* make sure pipe is not busy */
767 ret
= usbhsp_pipe_barrier(pipe
);
769 dev_err(dev
, "pipe setup failed %d\n", usbhs_pipe_number(pipe
));
773 pipecfg
= usbhsp_setup_pipecfg(pipe
, desc
, is_host
);
774 pipebuf
= usbhsp_setup_pipebuff(pipe
, desc
, is_host
);
775 pipemaxp
= usbhsp_setup_pipemaxp(pipe
, desc
, is_host
);
778 * see PIPECFG :: BFRE */
779 usbhsp_pipectrl_set(pipe
, ACLRM
, ACLRM
);
780 usbhsp_pipectrl_set(pipe
, ACLRM
, 0);
782 usbhsp_pipe_select(pipe
);
783 usbhsp_pipe_cfg_set(pipe
, 0xFFFF, pipecfg
);
784 usbhsp_pipe_buf_set(pipe
, 0xFFFF, pipebuf
);
785 usbhsp_pipe_maxp_set(pipe
, 0xFFFF, pipemaxp
);
787 usbhs_pipe_clear_sequence(pipe
);
789 dev_dbg(dev
, "enable pipe %d : %s (%s)\n",
790 usbhs_pipe_number(pipe
),
791 usbhsp_pipe_name
[usb_endpoint_type(desc
)],
792 usbhs_pipe_is_dir_in(pipe
) ? "in" : "out");
800 struct usbhs_pipe
*usbhs_dcp_malloc(struct usbhs_priv
*priv
)
802 struct usbhs_pipe
*pipe
;
804 pipe
= usbhsp_get_pipe(priv
, USB_ENDPOINT_XFER_CONTROL
);
811 * pipebuf : nothing to do
814 usbhsp_pipe_select(pipe
);
815 usbhs_pipe_clear_sequence(pipe
);
820 void usbhs_dcp_control_transfer_done(struct usbhs_pipe
*pipe
)
822 WARN_ON(!usbhsp_is_dcp(pipe
));
824 usbhs_fifo_enable(pipe
);
825 usbhsp_pipectrl_set(pipe
, CCPL
, CCPL
);
830 * pipe module function
832 int usbhs_pipe_probe(struct usbhs_priv
*priv
)
834 struct usbhs_pipe_info
*info
= usbhsp_priv_to_pipeinfo(priv
);
835 struct usbhs_pipe
*pipe
;
836 struct device
*dev
= usbhs_priv_to_dev(priv
);
837 u32
*pipe_type
= usbhs_get_dparam(priv
, pipe_type
);
838 int pipe_size
= usbhs_get_dparam(priv
, pipe_size
);
841 /* This driver expects 1st pipe is DCP */
842 if (pipe_type
[0] != USB_ENDPOINT_XFER_CONTROL
) {
843 dev_err(dev
, "1st PIPE is not DCP\n");
847 info
->pipe
= kzalloc(sizeof(struct usbhs_pipe
) * pipe_size
, GFP_KERNEL
);
849 dev_err(dev
, "Could not allocate pipe\n");
853 info
->size
= pipe_size
;
858 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
860 usbhsp_type(pipe
) = pipe_type
[i
] & USB_ENDPOINT_XFERTYPE_MASK
;
862 dev_dbg(dev
, "pipe %x\t: %s\n",
863 i
, usbhsp_pipe_name
[pipe_type
[i
]]);
869 void usbhs_pipe_remove(struct usbhs_priv
*priv
)
871 struct usbhs_pipe_info
*info
= usbhsp_priv_to_pipeinfo(priv
);