blob: a373c4e64b44eb3303eeca01d765757e07fcafd5 [file] [log] [blame]
Yoshihiro Shimodac4144242009-08-19 04:59:39 +00001/*
2 * R8A66597 UDC
3 *
4 * Copyright (C) 2007-2009 Renesas Solutions Corp.
5 *
Yoshihiro Shimoda5db05c02011-07-07 09:59:07 +09006 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
Yoshihiro Shimodac4144242009-08-19 04:59:39 +00007 *
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.
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000011 */
12
13#ifndef __R8A66597_H__
14#define __R8A66597_H__
15
Magnus Dammd2e27bd2009-08-19 09:50:49 +000016#ifdef CONFIG_HAVE_CLK
17#include <linux/clk.h>
18#endif
19
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000020#include <linux/usb/r8a66597.h>
21
22#define R8A66597_MAX_SAMPLING 10
23
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
28
29#define R8A66597_BASE_PIPENUM_BULK 3
30#define R8A66597_BASE_PIPENUM_ISOC 1
31#define R8A66597_BASE_PIPENUM_INT 6
32
33#define R8A66597_BASE_BUFNUM 6
34#define R8A66597_MAX_BUFNUM 0x4F
35
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)))
45
46struct r8a66597_pipe_info {
47 u16 pipe;
48 u16 epnum;
49 u16 maxpacket;
50 u16 type;
51 u16 interval;
52 u16 dir_in;
53};
54
55struct r8a66597_request {
56 struct usb_request req;
57 struct list_head queue;
58};
59
60struct r8a66597_ep {
61 struct usb_ep ep;
62 struct r8a66597 *r8a66597;
63
64 struct list_head queue;
65 unsigned busy:1;
Yoshihiro Shimoda9e7291c2009-08-20 07:01:06 +000066 unsigned wedge:1;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000067 unsigned internal_ccpl:1; /* use only control */
68
69 /* this member can able to after r8a66597_enable */
70 unsigned use_dma:1;
71 u16 pipenum;
72 u16 type;
73 const struct usb_endpoint_descriptor *desc;
74 /* register address */
75 unsigned char fifoaddr;
76 unsigned char fifosel;
77 unsigned char fifoctr;
78 unsigned char fifotrn;
79 unsigned char pipectr;
80};
81
82struct r8a66597 {
83 spinlock_t lock;
Paul Mundte8b48662010-06-02 16:27:12 +090084 void __iomem *reg;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000085
Magnus Dammd2e27bd2009-08-19 09:50:49 +000086#ifdef CONFIG_HAVE_CLK
87 struct clk *clk;
88#endif
Yoshihiro Shimodac4144242009-08-19 04:59:39 +000089 struct r8a66597_platdata *pdata;
90
91 struct usb_gadget gadget;
92 struct usb_gadget_driver *driver;
93
94 struct r8a66597_ep ep[R8A66597_MAX_NUM_PIPE];
95 struct r8a66597_ep *pipenum2ep[R8A66597_MAX_NUM_PIPE];
96 struct r8a66597_ep *epaddr2ep[16];
97
98 struct timer_list timer;
99 struct usb_request *ep0_req; /* for internal request */
100 u16 ep0_data; /* for internal request */
101 u16 old_vbus;
102 u16 scount;
103 u16 old_dvsq;
104
105 /* pipe config */
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000106 unsigned char bulk;
107 unsigned char interrupt;
108 unsigned char isochronous;
109 unsigned char num_dma;
110
111 unsigned irq_sense_low:1;
112};
113
114#define gadget_to_r8a66597(_gadget) \
115 container_of(_gadget, struct r8a66597, gadget)
116#define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget)
117
118static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset)
119{
Paul Mundte8b48662010-06-02 16:27:12 +0900120 return ioread16(r8a66597->reg + offset);
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000121}
122
123static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597,
Magnus Damm59c82d12009-09-30 11:57:24 +0000124 unsigned long offset,
125 unsigned char *buf,
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000126 int len)
127{
Paul Mundte8b48662010-06-02 16:27:12 +0900128 void __iomem *fifoaddr = r8a66597->reg + offset;
Javier Martinez Canillas92d34892010-09-10 08:42:08 -0400129 unsigned int data = 0;
Magnus Damm59c82d12009-09-30 11:57:24 +0000130 int i;
131
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000132 if (r8a66597->pdata->on_chip) {
Magnus Damm59c82d12009-09-30 11:57:24 +0000133 /* 32-bit accesses for on_chip controllers */
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000134
Magnus Damm59c82d12009-09-30 11:57:24 +0000135 /* aligned buf case */
136 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900137 ioread32_rep(fifoaddr, buf, len / 4);
Magnus Damm59c82d12009-09-30 11:57:24 +0000138 buf += len & ~0x03;
139 len &= 0x03;
140 }
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000141
Magnus Damm59c82d12009-09-30 11:57:24 +0000142 /* unaligned buf case */
143 for (i = 0; i < len; i++) {
144 if (!(i & 0x03))
Paul Mundte8b48662010-06-02 16:27:12 +0900145 data = ioread32(fifoaddr);
Magnus Damm59c82d12009-09-30 11:57:24 +0000146
147 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000148 }
149 } else {
Magnus Damm59c82d12009-09-30 11:57:24 +0000150 /* 16-bit accesses for external controllers */
151
152 /* aligned buf case */
153 if (len >= 2 && !((unsigned long)buf & 0x01)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900154 ioread16_rep(fifoaddr, buf, len / 2);
Magnus Damm59c82d12009-09-30 11:57:24 +0000155 buf += len & ~0x01;
156 len &= 0x01;
157 }
158
159 /* unaligned buf case */
160 for (i = 0; i < len; i++) {
161 if (!(i & 0x01))
Paul Mundte8b48662010-06-02 16:27:12 +0900162 data = ioread16(fifoaddr);
Magnus Damm59c82d12009-09-30 11:57:24 +0000163
164 buf[i] = (data >> ((i & 0x01) * 8)) & 0xff;
165 }
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000166 }
167}
168
169static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val,
170 unsigned long offset)
171{
Paul Mundte8b48662010-06-02 16:27:12 +0900172 iowrite16(val, r8a66597->reg + offset);
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000173}
174
175static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
Magnus Damm59c82d12009-09-30 11:57:24 +0000176 unsigned long offset,
177 unsigned char *buf,
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000178 int len)
179{
Paul Mundte8b48662010-06-02 16:27:12 +0900180 void __iomem *fifoaddr = r8a66597->reg + offset;
Magnus Damm59c82d12009-09-30 11:57:24 +0000181 int adj = 0;
182 int i;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000183
184 if (r8a66597->pdata->on_chip) {
Magnus Damm59c82d12009-09-30 11:57:24 +0000185 /* 32-bit access only if buf is 32-bit aligned */
186 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900187 iowrite32_rep(fifoaddr, buf, len / 4);
Magnus Damm59c82d12009-09-30 11:57:24 +0000188 buf += len & ~0x03;
189 len &= 0x03;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000190 }
191 } else {
Magnus Damm59c82d12009-09-30 11:57:24 +0000192 /* 16-bit access only if buf is 16-bit aligned */
193 if (len >= 2 && !((unsigned long)buf & 0x01)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900194 iowrite16_rep(fifoaddr, buf, len / 2);
Magnus Damm59c82d12009-09-30 11:57:24 +0000195 buf += len & ~0x01;
196 len &= 0x01;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000197 }
198 }
Magnus Damm59c82d12009-09-30 11:57:24 +0000199
200 /* adjust fifo address in the little endian case */
201 if (!(r8a66597_read(r8a66597, CFIFOSEL) & BIGEND)) {
202 if (r8a66597->pdata->on_chip)
203 adj = 0x03; /* 32-bit wide */
204 else
205 adj = 0x01; /* 16-bit wide */
206 }
207
208 for (i = 0; i < len; i++)
Paul Mundte8b48662010-06-02 16:27:12 +0900209 iowrite8(buf[i], fifoaddr + adj - (i & adj));
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000210}
211
212static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
213 u16 val, u16 pat, unsigned long offset)
214{
215 u16 tmp;
216 tmp = r8a66597_read(r8a66597, offset);
217 tmp = tmp & (~pat);
218 tmp = tmp | val;
219 r8a66597_write(r8a66597, tmp, offset);
220}
221
222static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
223{
224 u16 clock = 0;
225
226 switch (pdata->xtal) {
227 case R8A66597_PLATDATA_XTAL_12MHZ:
228 clock = XTAL12;
229 break;
230 case R8A66597_PLATDATA_XTAL_24MHZ:
231 clock = XTAL24;
232 break;
233 case R8A66597_PLATDATA_XTAL_48MHZ:
234 clock = XTAL48;
235 break;
236 default:
237 printk(KERN_ERR "r8a66597: platdata clock is wrong.\n");
238 break;
239 }
240
241 return clock;
242}
243
244#define r8a66597_bclr(r8a66597, val, offset) \
245 r8a66597_mdfy(r8a66597, 0, val, offset)
246#define r8a66597_bset(r8a66597, val, offset) \
247 r8a66597_mdfy(r8a66597, val, 0, offset)
248
249#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
250
251#define enable_irq_ready(r8a66597, pipenum) \
252 enable_pipe_irq(r8a66597, pipenum, BRDYENB)
253#define disable_irq_ready(r8a66597, pipenum) \
254 disable_pipe_irq(r8a66597, pipenum, BRDYENB)
255#define enable_irq_empty(r8a66597, pipenum) \
256 enable_pipe_irq(r8a66597, pipenum, BEMPENB)
257#define disable_irq_empty(r8a66597, pipenum) \
258 disable_pipe_irq(r8a66597, pipenum, BEMPENB)
259#define enable_irq_nrdy(r8a66597, pipenum) \
260 enable_pipe_irq(r8a66597, pipenum, NRDYENB)
261#define disable_irq_nrdy(r8a66597, pipenum) \
262 disable_pipe_irq(r8a66597, pipenum, NRDYENB)
263
264#endif /* __R8A66597_H__ */
265