1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2007-2009 Renesas Solutions Corp.
7 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
10 #ifndef __R8A66597_H__
11 #define __R8A66597_H__
13 #include <linux/clk.h>
14 #include <linux/usb/r8a66597.h>
16 #define R8A66597_MAX_SAMPLING 10
18 #define R8A66597_MAX_NUM_PIPE 8
19 #define R8A66597_MAX_NUM_BULK 3
20 #define R8A66597_MAX_NUM_ISOC 2
21 #define R8A66597_MAX_NUM_INT 2
23 #define R8A66597_BASE_PIPENUM_BULK 3
24 #define R8A66597_BASE_PIPENUM_ISOC 1
25 #define R8A66597_BASE_PIPENUM_INT 6
27 #define R8A66597_BASE_BUFNUM 6
28 #define R8A66597_MAX_BUFNUM 0x4F
30 #define is_bulk_pipe(pipenum) \
31 ((pipenum >= R8A66597_BASE_PIPENUM_BULK) && \
32 (pipenum < (R8A66597_BASE_PIPENUM_BULK + R8A66597_MAX_NUM_BULK)))
33 #define is_interrupt_pipe(pipenum) \
34 ((pipenum >= R8A66597_BASE_PIPENUM_INT) && \
35 (pipenum < (R8A66597_BASE_PIPENUM_INT + R8A66597_MAX_NUM_INT)))
36 #define is_isoc_pipe(pipenum) \
37 ((pipenum >= R8A66597_BASE_PIPENUM_ISOC) && \
38 (pipenum < (R8A66597_BASE_PIPENUM_ISOC + R8A66597_MAX_NUM_ISOC)))
40 #define r8a66597_is_sudmac(r8a66597) (r8a66597->pdata->sudmac)
41 struct r8a66597_pipe_info
{
50 struct r8a66597_request
{
51 struct usb_request req
;
52 struct list_head queue
;
57 struct r8a66597
*r8a66597
;
58 struct r8a66597_dma
*dma
;
60 struct list_head queue
;
63 unsigned internal_ccpl
:1; /* use only control */
65 /* this member can able to after r8a66597_enable */
70 /* register address */
71 unsigned char fifoaddr
;
72 unsigned char fifosel
;
73 unsigned char fifoctr
;
74 unsigned char pipectr
;
75 unsigned char pipetre
;
76 unsigned char pipetrn
;
81 unsigned dir
:1; /* 1 = IN(write), 0 = OUT(read) */
87 void __iomem
*sudmac_reg
;
90 struct r8a66597_platdata
*pdata
;
92 struct usb_gadget gadget
;
93 struct usb_gadget_driver
*driver
;
95 struct r8a66597_ep ep
[R8A66597_MAX_NUM_PIPE
];
96 struct r8a66597_ep
*pipenum2ep
[R8A66597_MAX_NUM_PIPE
];
97 struct r8a66597_ep
*epaddr2ep
[16];
98 struct r8a66597_dma dma
;
100 struct timer_list timer
;
101 struct usb_request
*ep0_req
; /* for internal request */
102 u16 ep0_data
; /* for internal request */
106 u16 device_status
; /* for GET_STATUS */
110 unsigned char interrupt
;
111 unsigned char isochronous
;
112 unsigned char num_dma
;
114 unsigned irq_sense_low
:1;
117 #define gadget_to_r8a66597(_gadget) \
118 container_of(_gadget, struct r8a66597, gadget)
119 #define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget)
120 #define r8a66597_to_dev(r8a66597) (r8a66597->gadget.dev.parent)
122 static inline u16
r8a66597_read(struct r8a66597
*r8a66597
, unsigned long offset
)
124 return ioread16(r8a66597
->reg
+ offset
);
127 static inline void r8a66597_read_fifo(struct r8a66597
*r8a66597
,
128 unsigned long offset
,
132 void __iomem
*fifoaddr
= r8a66597
->reg
+ offset
;
133 unsigned int data
= 0;
136 if (r8a66597
->pdata
->on_chip
) {
137 /* 32-bit accesses for on_chip controllers */
139 /* aligned buf case */
140 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
141 ioread32_rep(fifoaddr
, buf
, len
/ 4);
146 /* unaligned buf case */
147 for (i
= 0; i
< len
; i
++) {
149 data
= ioread32(fifoaddr
);
151 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
154 /* 16-bit accesses for external controllers */
156 /* aligned buf case */
157 if (len
>= 2 && !((unsigned long)buf
& 0x01)) {
158 ioread16_rep(fifoaddr
, buf
, len
/ 2);
163 /* unaligned buf case */
164 for (i
= 0; i
< len
; i
++) {
166 data
= ioread16(fifoaddr
);
168 buf
[i
] = (data
>> ((i
& 0x01) * 8)) & 0xff;
173 static inline void r8a66597_write(struct r8a66597
*r8a66597
, u16 val
,
174 unsigned long offset
)
176 iowrite16(val
, r8a66597
->reg
+ offset
);
179 static inline void r8a66597_mdfy(struct r8a66597
*r8a66597
,
180 u16 val
, u16 pat
, unsigned long offset
)
183 tmp
= r8a66597_read(r8a66597
, offset
);
186 r8a66597_write(r8a66597
, tmp
, offset
);
189 #define r8a66597_bclr(r8a66597, val, offset) \
190 r8a66597_mdfy(r8a66597, 0, val, offset)
191 #define r8a66597_bset(r8a66597, val, offset) \
192 r8a66597_mdfy(r8a66597, val, 0, offset)
194 static inline void r8a66597_write_fifo(struct r8a66597
*r8a66597
,
195 struct r8a66597_ep
*ep
,
199 void __iomem
*fifoaddr
= r8a66597
->reg
+ ep
->fifoaddr
;
203 if (r8a66597
->pdata
->on_chip
) {
204 /* 32-bit access only if buf is 32-bit aligned */
205 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
206 iowrite32_rep(fifoaddr
, buf
, len
/ 4);
211 /* 16-bit access only if buf is 16-bit aligned */
212 if (len
>= 2 && !((unsigned long)buf
& 0x01)) {
213 iowrite16_rep(fifoaddr
, buf
, len
/ 2);
219 /* adjust fifo address in the little endian case */
220 if (!(r8a66597_read(r8a66597
, CFIFOSEL
) & BIGEND
)) {
221 if (r8a66597
->pdata
->on_chip
)
222 adj
= 0x03; /* 32-bit wide */
224 adj
= 0x01; /* 16-bit wide */
227 if (r8a66597
->pdata
->wr0_shorted_to_wr1
)
228 r8a66597_bclr(r8a66597
, MBW_16
, ep
->fifosel
);
229 for (i
= 0; i
< len
; i
++)
230 iowrite8(buf
[i
], fifoaddr
+ adj
- (i
& adj
));
231 if (r8a66597
->pdata
->wr0_shorted_to_wr1
)
232 r8a66597_bclr(r8a66597
, MBW_16
, ep
->fifosel
);
235 static inline u16
get_xtal_from_pdata(struct r8a66597_platdata
*pdata
)
239 switch (pdata
->xtal
) {
240 case R8A66597_PLATDATA_XTAL_12MHZ
:
243 case R8A66597_PLATDATA_XTAL_24MHZ
:
246 case R8A66597_PLATDATA_XTAL_48MHZ
:
250 printk(KERN_ERR
"r8a66597: platdata clock is wrong.\n");
257 static inline u32
r8a66597_sudmac_read(struct r8a66597
*r8a66597
,
258 unsigned long offset
)
260 return ioread32(r8a66597
->sudmac_reg
+ offset
);
263 static inline void r8a66597_sudmac_write(struct r8a66597
*r8a66597
, u32 val
,
264 unsigned long offset
)
266 iowrite32(val
, r8a66597
->sudmac_reg
+ offset
);
269 #define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
270 #define get_pipetre_addr(pipenum) (PIPE1TRE + (pipenum - 1) * 4)
271 #define get_pipetrn_addr(pipenum) (PIPE1TRN + (pipenum - 1) * 4)
273 #define enable_irq_ready(r8a66597, pipenum) \
274 enable_pipe_irq(r8a66597, pipenum, BRDYENB)
275 #define disable_irq_ready(r8a66597, pipenum) \
276 disable_pipe_irq(r8a66597, pipenum, BRDYENB)
277 #define enable_irq_empty(r8a66597, pipenum) \
278 enable_pipe_irq(r8a66597, pipenum, BEMPENB)
279 #define disable_irq_empty(r8a66597, pipenum) \
280 disable_pipe_irq(r8a66597, pipenum, BEMPENB)
281 #define enable_irq_nrdy(r8a66597, pipenum) \
282 enable_pipe_irq(r8a66597, pipenum, NRDYENB)
283 #define disable_irq_nrdy(r8a66597, pipenum) \
284 disable_pipe_irq(r8a66597, pipenum, NRDYENB)
286 #endif /* __R8A66597_H__ */