blob: e653575d4ceb8f1399c12a5f5b133ee59838712b [file] [log] [blame]
Yoshihiro Shimodac4144242009-08-19 04:59:39 +00001/*
2 * R8A66597 UDC
3 *
4 * Copyright (C) 2007-2009 Renesas Solutions Corp.
5 *
6 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
7 *
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.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22
23#ifndef __R8A66597_H__
24#define __R8A66597_H__
25
Magnus Dammd2e27bd2009-08-19 09:50:49 +000026#ifdef CONFIG_HAVE_CLK
27#include <linux/clk.h>
28#endif
29
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000030#include <linux/usb/r8a66597.h>
31
32#define R8A66597_MAX_SAMPLING 10
33
34#define R8A66597_MAX_NUM_PIPE 8
35#define R8A66597_MAX_NUM_BULK 3
36#define R8A66597_MAX_NUM_ISOC 2
37#define R8A66597_MAX_NUM_INT 2
38
39#define R8A66597_BASE_PIPENUM_BULK 3
40#define R8A66597_BASE_PIPENUM_ISOC 1
41#define R8A66597_BASE_PIPENUM_INT 6
42
43#define R8A66597_BASE_BUFNUM 6
44#define R8A66597_MAX_BUFNUM 0x4F
45
46#define is_bulk_pipe(pipenum) \
47 ((pipenum >= R8A66597_BASE_PIPENUM_BULK) && \
48 (pipenum < (R8A66597_BASE_PIPENUM_BULK + R8A66597_MAX_NUM_BULK)))
49#define is_interrupt_pipe(pipenum) \
50 ((pipenum >= R8A66597_BASE_PIPENUM_INT) && \
51 (pipenum < (R8A66597_BASE_PIPENUM_INT + R8A66597_MAX_NUM_INT)))
52#define is_isoc_pipe(pipenum) \
53 ((pipenum >= R8A66597_BASE_PIPENUM_ISOC) && \
54 (pipenum < (R8A66597_BASE_PIPENUM_ISOC + R8A66597_MAX_NUM_ISOC)))
55
56struct r8a66597_pipe_info {
57 u16 pipe;
58 u16 epnum;
59 u16 maxpacket;
60 u16 type;
61 u16 interval;
62 u16 dir_in;
63};
64
65struct r8a66597_request {
66 struct usb_request req;
67 struct list_head queue;
68};
69
70struct r8a66597_ep {
71 struct usb_ep ep;
72 struct r8a66597 *r8a66597;
73
74 struct list_head queue;
75 unsigned busy:1;
76 unsigned internal_ccpl:1; /* use only control */
77
78 /* this member can able to after r8a66597_enable */
79 unsigned use_dma:1;
80 u16 pipenum;
81 u16 type;
82 const struct usb_endpoint_descriptor *desc;
83 /* register address */
84 unsigned char fifoaddr;
85 unsigned char fifosel;
86 unsigned char fifoctr;
87 unsigned char fifotrn;
88 unsigned char pipectr;
89};
90
91struct r8a66597 {
92 spinlock_t lock;
93 unsigned long reg;
94
Magnus Dammd2e27bd2009-08-19 09:50:49 +000095#ifdef CONFIG_HAVE_CLK
96 struct clk *clk;
97#endif
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000098 struct r8a66597_platdata *pdata;
99
100 struct usb_gadget gadget;
101 struct usb_gadget_driver *driver;
102
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
107 struct timer_list timer;
108 struct usb_request *ep0_req; /* for internal request */
109 u16 ep0_data; /* for internal request */
110 u16 old_vbus;
111 u16 scount;
112 u16 old_dvsq;
113
114 /* pipe config */
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000115 unsigned char bulk;
116 unsigned char interrupt;
117 unsigned char isochronous;
118 unsigned char num_dma;
119
120 unsigned irq_sense_low:1;
121};
122
123#define gadget_to_r8a66597(_gadget) \
124 container_of(_gadget, struct r8a66597, gadget)
125#define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget)
126
127static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset)
128{
129 return inw(r8a66597->reg + offset);
130}
131
132static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597,
133 unsigned long offset, u16 *buf,
134 int len)
135{
136 if (r8a66597->pdata->on_chip) {
137 unsigned long fifoaddr = r8a66597->reg + offset;
138 unsigned long count;
139 union {
140 unsigned long dword;
141 unsigned char byte[4];
142 } data;
143 unsigned char *pb;
144 int i;
145
146 count = len / 4;
147 insl(fifoaddr, buf, count);
148
149 if (len & 0x00000003) {
150 data.dword = inl(fifoaddr);
151 pb = (unsigned char *)buf + count * 4;
152 for (i = 0; i < (len & 0x00000003); i++)
153 pb[i] = data.byte[i];
154 }
155 } else {
156 len = (len + 1) / 2;
157 insw(r8a66597->reg + offset, buf, len);
158 }
159}
160
161static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val,
162 unsigned long offset)
163{
164 outw(val, r8a66597->reg + offset);
165}
166
167static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
168 unsigned long offset, u16 *buf,
169 int len)
170{
171 unsigned long fifoaddr = r8a66597->reg + offset;
172
173 if (r8a66597->pdata->on_chip) {
174 unsigned long count;
175 unsigned char *pb;
176 int i;
177
178 count = len / 4;
179 outsl(fifoaddr, buf, count);
180
181 if (len & 0x00000003) {
182 pb = (unsigned char *)buf + count * 4;
183 for (i = 0; i < (len & 0x00000003); i++) {
184 if (r8a66597_read(r8a66597, CFIFOSEL) & BIGEND)
185 outb(pb[i], fifoaddr + i);
186 else
187 outb(pb[i], fifoaddr + 3 - i);
188 }
189 }
190 } else {
191 int odd = len & 0x0001;
192
193 len = len / 2;
194 outsw(fifoaddr, buf, len);
195 if (unlikely(odd)) {
196 buf = &buf[len];
197 outb((unsigned char)*buf, fifoaddr);
198 }
199 }
200}
201
202static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
203 u16 val, u16 pat, unsigned long offset)
204{
205 u16 tmp;
206 tmp = r8a66597_read(r8a66597, offset);
207 tmp = tmp & (~pat);
208 tmp = tmp | val;
209 r8a66597_write(r8a66597, tmp, offset);
210}
211
212static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
213{
214 u16 clock = 0;
215
216 switch (pdata->xtal) {
217 case R8A66597_PLATDATA_XTAL_12MHZ:
218 clock = XTAL12;
219 break;
220 case R8A66597_PLATDATA_XTAL_24MHZ:
221 clock = XTAL24;
222 break;
223 case R8A66597_PLATDATA_XTAL_48MHZ:
224 clock = XTAL48;
225 break;
226 default:
227 printk(KERN_ERR "r8a66597: platdata clock is wrong.\n");
228 break;
229 }
230
231 return clock;
232}
233
234#define r8a66597_bclr(r8a66597, val, offset) \
235 r8a66597_mdfy(r8a66597, 0, val, offset)
236#define r8a66597_bset(r8a66597, val, offset) \
237 r8a66597_mdfy(r8a66597, val, 0, offset)
238
239#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
240
241#define enable_irq_ready(r8a66597, pipenum) \
242 enable_pipe_irq(r8a66597, pipenum, BRDYENB)
243#define disable_irq_ready(r8a66597, pipenum) \
244 disable_pipe_irq(r8a66597, pipenum, BRDYENB)
245#define enable_irq_empty(r8a66597, pipenum) \
246 enable_pipe_irq(r8a66597, pipenum, BEMPENB)
247#define disable_irq_empty(r8a66597, pipenum) \
248 disable_pipe_irq(r8a66597, pipenum, BEMPENB)
249#define enable_irq_nrdy(r8a66597, pipenum) \
250 enable_pipe_irq(r8a66597, pipenum, NRDYENB)
251#define disable_irq_nrdy(r8a66597, pipenum) \
252 disable_pipe_irq(r8a66597, pipenum, NRDYENB)
253
254#endif /* __R8A66597_H__ */
255