4 * Copyright (C) 2007-2009 Renesas Solutions Corp.
6 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #ifndef __R8A66597_H__
14 #define __R8A66597_H__
16 #ifdef CONFIG_HAVE_CLK
17 #include <linux/clk.h>
20 #include <linux/usb/r8a66597.h>
22 #define R8A66597_MAX_SAMPLING 10
24 #define R8A66597_MAX_NUM_PIPE 8
25 #define R8A66597_MAX_NUM_BULK 3
26 #define R8A66597_MAX_NUM_ISOC 2
27 #define R8A66597_MAX_NUM_INT 2
29 #define R8A66597_BASE_PIPENUM_BULK 3
30 #define R8A66597_BASE_PIPENUM_ISOC 1
31 #define R8A66597_BASE_PIPENUM_INT 6
33 #define R8A66597_BASE_BUFNUM 6
34 #define R8A66597_MAX_BUFNUM 0x4F
36 #define is_bulk_pipe(pipenum) \
37 ((pipenum >= R8A66597_BASE_PIPENUM_BULK) && \
38 (pipenum < (R8A66597_BASE_PIPENUM_BULK + R8A66597_MAX_NUM_BULK)))
39 #define is_interrupt_pipe(pipenum) \
40 ((pipenum >= R8A66597_BASE_PIPENUM_INT) && \
41 (pipenum < (R8A66597_BASE_PIPENUM_INT + R8A66597_MAX_NUM_INT)))
42 #define is_isoc_pipe(pipenum) \
43 ((pipenum >= R8A66597_BASE_PIPENUM_ISOC) && \
44 (pipenum < (R8A66597_BASE_PIPENUM_ISOC + R8A66597_MAX_NUM_ISOC)))
46 #define r8a66597_is_sudmac(r8a66597) (r8a66597->pdata->sudmac)
47 struct r8a66597_pipe_info
{
56 struct r8a66597_request
{
57 struct usb_request req
;
58 struct list_head queue
;
63 struct r8a66597
*r8a66597
;
64 struct r8a66597_dma
*dma
;
66 struct list_head queue
;
69 unsigned internal_ccpl
:1; /* use only control */
71 /* this member can able to after r8a66597_enable */
76 /* register address */
77 unsigned char fifoaddr
;
78 unsigned char fifosel
;
79 unsigned char fifoctr
;
80 unsigned char pipectr
;
81 unsigned char pipetre
;
82 unsigned char pipetrn
;
87 unsigned dir
:1; /* 1 = IN(write), 0 = OUT(read) */
93 void __iomem
*sudmac_reg
;
95 #ifdef CONFIG_HAVE_CLK
98 struct r8a66597_platdata
*pdata
;
100 struct usb_gadget gadget
;
101 struct usb_gadget_driver
*driver
;
103 struct r8a66597_ep ep
[R8A66597_MAX_NUM_PIPE
];
104 struct r8a66597_ep
*pipenum2ep
[R8A66597_MAX_NUM_PIPE
];
105 struct r8a66597_ep
*epaddr2ep
[16];
106 struct r8a66597_dma dma
;
108 struct timer_list timer
;
109 struct usb_request
*ep0_req
; /* for internal request */
110 u16 ep0_data
; /* for internal request */
114 u16 device_status
; /* for GET_STATUS */
118 unsigned char interrupt
;
119 unsigned char isochronous
;
120 unsigned char num_dma
;
122 unsigned irq_sense_low
:1;
125 #define gadget_to_r8a66597(_gadget) \
126 container_of(_gadget, struct r8a66597, gadget)
127 #define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget)
128 #define r8a66597_to_dev(r8a66597) (r8a66597->gadget.dev.parent)
130 static inline u16
r8a66597_read(struct r8a66597
*r8a66597
, unsigned long offset
)
132 return ioread16(r8a66597
->reg
+ offset
);
135 static inline void r8a66597_read_fifo(struct r8a66597
*r8a66597
,
136 unsigned long offset
,
140 void __iomem
*fifoaddr
= r8a66597
->reg
+ offset
;
141 unsigned int data
= 0;
144 if (r8a66597
->pdata
->on_chip
) {
145 /* 32-bit accesses for on_chip controllers */
147 /* aligned buf case */
148 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
149 ioread32_rep(fifoaddr
, buf
, len
/ 4);
154 /* unaligned buf case */
155 for (i
= 0; i
< len
; i
++) {
157 data
= ioread32(fifoaddr
);
159 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
162 /* 16-bit accesses for external controllers */
164 /* aligned buf case */
165 if (len
>= 2 && !((unsigned long)buf
& 0x01)) {
166 ioread16_rep(fifoaddr
, buf
, len
/ 2);
171 /* unaligned buf case */
172 for (i
= 0; i
< len
; i
++) {
174 data
= ioread16(fifoaddr
);
176 buf
[i
] = (data
>> ((i
& 0x01) * 8)) & 0xff;
181 static inline void r8a66597_write(struct r8a66597
*r8a66597
, u16 val
,
182 unsigned long offset
)
184 iowrite16(val
, r8a66597
->reg
+ offset
);
187 static inline void r8a66597_mdfy(struct r8a66597
*r8a66597
,
188 u16 val
, u16 pat
, unsigned long offset
)
191 tmp
= r8a66597_read(r8a66597
, offset
);
194 r8a66597_write(r8a66597
, tmp
, offset
);
197 #define r8a66597_bclr(r8a66597, val, offset) \
198 r8a66597_mdfy(r8a66597, 0, val, offset)
199 #define r8a66597_bset(r8a66597, val, offset) \
200 r8a66597_mdfy(r8a66597, val, 0, offset)
202 static inline void r8a66597_write_fifo(struct r8a66597
*r8a66597
,
203 struct r8a66597_ep
*ep
,
207 void __iomem
*fifoaddr
= r8a66597
->reg
+ ep
->fifoaddr
;
211 if (r8a66597
->pdata
->on_chip
) {
212 /* 32-bit access only if buf is 32-bit aligned */
213 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
214 iowrite32_rep(fifoaddr
, buf
, len
/ 4);
219 /* 16-bit access only if buf is 16-bit aligned */
220 if (len
>= 2 && !((unsigned long)buf
& 0x01)) {
221 iowrite16_rep(fifoaddr
, buf
, len
/ 2);
227 /* adjust fifo address in the little endian case */
228 if (!(r8a66597_read(r8a66597
, CFIFOSEL
) & BIGEND
)) {
229 if (r8a66597
->pdata
->on_chip
)
230 adj
= 0x03; /* 32-bit wide */
232 adj
= 0x01; /* 16-bit wide */
235 if (r8a66597
->pdata
->wr0_shorted_to_wr1
)
236 r8a66597_bclr(r8a66597
, MBW_16
, ep
->fifosel
);
237 for (i
= 0; i
< len
; i
++)
238 iowrite8(buf
[i
], fifoaddr
+ adj
- (i
& adj
));
239 if (r8a66597
->pdata
->wr0_shorted_to_wr1
)
240 r8a66597_bclr(r8a66597
, MBW_16
, ep
->fifosel
);
243 static inline u16
get_xtal_from_pdata(struct r8a66597_platdata
*pdata
)
247 switch (pdata
->xtal
) {
248 case R8A66597_PLATDATA_XTAL_12MHZ
:
251 case R8A66597_PLATDATA_XTAL_24MHZ
:
254 case R8A66597_PLATDATA_XTAL_48MHZ
:
258 printk(KERN_ERR
"r8a66597: platdata clock is wrong.\n");
265 static inline u32
r8a66597_sudmac_read(struct r8a66597
*r8a66597
,
266 unsigned long offset
)
268 return ioread32(r8a66597
->sudmac_reg
+ offset
);
271 static inline void r8a66597_sudmac_write(struct r8a66597
*r8a66597
, u32 val
,
272 unsigned long offset
)
274 iowrite32(val
, r8a66597
->sudmac_reg
+ offset
);
277 #define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
278 #define get_pipetre_addr(pipenum) (PIPE1TRE + (pipenum - 1) * 4)
279 #define get_pipetrn_addr(pipenum) (PIPE1TRN + (pipenum - 1) * 4)
281 #define enable_irq_ready(r8a66597, pipenum) \
282 enable_pipe_irq(r8a66597, pipenum, BRDYENB)
283 #define disable_irq_ready(r8a66597, pipenum) \
284 disable_pipe_irq(r8a66597, pipenum, BRDYENB)
285 #define enable_irq_empty(r8a66597, pipenum) \
286 enable_pipe_irq(r8a66597, pipenum, BEMPENB)
287 #define disable_irq_empty(r8a66597, pipenum) \
288 disable_pipe_irq(r8a66597, pipenum, BEMPENB)
289 #define enable_irq_nrdy(r8a66597, pipenum) \
290 enable_pipe_irq(r8a66597, pipenum, NRDYENB)
291 #define disable_irq_nrdy(r8a66597, pipenum) \
292 disable_pipe_irq(r8a66597, pipenum, NRDYENB)
294 #endif /* __R8A66597_H__ */