blob: 832ee59c8e457cc09fa367a02580951761d60694 [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)
Yoshihiro Shimoda12158f42011-09-30 20:07:30 +0900117#define r8a66597_to_dev(r8a66597) (r8a66597->gadget.dev.parent)
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000118
119static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset)
120{
Paul Mundte8b48662010-06-02 16:27:12 +0900121 return ioread16(r8a66597->reg + offset);
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000122}
123
124static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597,
Magnus Damm59c82d12009-09-30 11:57:24 +0000125 unsigned long offset,
126 unsigned char *buf,
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000127 int len)
128{
Paul Mundte8b48662010-06-02 16:27:12 +0900129 void __iomem *fifoaddr = r8a66597->reg + offset;
Javier Martinez Canillas92d34892010-09-10 08:42:08 -0400130 unsigned int data = 0;
Magnus Damm59c82d12009-09-30 11:57:24 +0000131 int i;
132
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000133 if (r8a66597->pdata->on_chip) {
Magnus Damm59c82d12009-09-30 11:57:24 +0000134 /* 32-bit accesses for on_chip controllers */
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000135
Magnus Damm59c82d12009-09-30 11:57:24 +0000136 /* aligned buf case */
137 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900138 ioread32_rep(fifoaddr, buf, len / 4);
Magnus Damm59c82d12009-09-30 11:57:24 +0000139 buf += len & ~0x03;
140 len &= 0x03;
141 }
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000142
Magnus Damm59c82d12009-09-30 11:57:24 +0000143 /* unaligned buf case */
144 for (i = 0; i < len; i++) {
145 if (!(i & 0x03))
Paul Mundte8b48662010-06-02 16:27:12 +0900146 data = ioread32(fifoaddr);
Magnus Damm59c82d12009-09-30 11:57:24 +0000147
148 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000149 }
150 } else {
Magnus Damm59c82d12009-09-30 11:57:24 +0000151 /* 16-bit accesses for external controllers */
152
153 /* aligned buf case */
154 if (len >= 2 && !((unsigned long)buf & 0x01)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900155 ioread16_rep(fifoaddr, buf, len / 2);
Magnus Damm59c82d12009-09-30 11:57:24 +0000156 buf += len & ~0x01;
157 len &= 0x01;
158 }
159
160 /* unaligned buf case */
161 for (i = 0; i < len; i++) {
162 if (!(i & 0x01))
Paul Mundte8b48662010-06-02 16:27:12 +0900163 data = ioread16(fifoaddr);
Magnus Damm59c82d12009-09-30 11:57:24 +0000164
165 buf[i] = (data >> ((i & 0x01) * 8)) & 0xff;
166 }
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000167 }
168}
169
170static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val,
171 unsigned long offset)
172{
Paul Mundte8b48662010-06-02 16:27:12 +0900173 iowrite16(val, r8a66597->reg + offset);
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000174}
175
Yoshihiro Shimoda5c481a62011-09-30 20:07:21 +0900176static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
177 u16 val, u16 pat, unsigned long offset)
178{
179 u16 tmp;
180 tmp = r8a66597_read(r8a66597, offset);
181 tmp = tmp & (~pat);
182 tmp = tmp | val;
183 r8a66597_write(r8a66597, tmp, offset);
184}
185
186#define r8a66597_bclr(r8a66597, val, offset) \
187 r8a66597_mdfy(r8a66597, 0, val, offset)
188#define r8a66597_bset(r8a66597, val, offset) \
189 r8a66597_mdfy(r8a66597, val, 0, offset)
190
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000191static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
Yoshihiro Shimoda0a855772011-09-30 20:07:08 +0900192 struct r8a66597_ep *ep,
Magnus Damm59c82d12009-09-30 11:57:24 +0000193 unsigned char *buf,
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000194 int len)
195{
Yoshihiro Shimoda0a855772011-09-30 20:07:08 +0900196 void __iomem *fifoaddr = r8a66597->reg + ep->fifoaddr;
Magnus Damm59c82d12009-09-30 11:57:24 +0000197 int adj = 0;
198 int i;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000199
200 if (r8a66597->pdata->on_chip) {
Magnus Damm59c82d12009-09-30 11:57:24 +0000201 /* 32-bit access only if buf is 32-bit aligned */
202 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900203 iowrite32_rep(fifoaddr, buf, len / 4);
Magnus Damm59c82d12009-09-30 11:57:24 +0000204 buf += len & ~0x03;
205 len &= 0x03;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000206 }
207 } else {
Magnus Damm59c82d12009-09-30 11:57:24 +0000208 /* 16-bit access only if buf is 16-bit aligned */
209 if (len >= 2 && !((unsigned long)buf & 0x01)) {
Paul Mundte8b48662010-06-02 16:27:12 +0900210 iowrite16_rep(fifoaddr, buf, len / 2);
Magnus Damm59c82d12009-09-30 11:57:24 +0000211 buf += len & ~0x01;
212 len &= 0x01;
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000213 }
214 }
Magnus Damm59c82d12009-09-30 11:57:24 +0000215
216 /* adjust fifo address in the little endian case */
217 if (!(r8a66597_read(r8a66597, CFIFOSEL) & BIGEND)) {
218 if (r8a66597->pdata->on_chip)
219 adj = 0x03; /* 32-bit wide */
220 else
221 adj = 0x01; /* 16-bit wide */
222 }
223
Yoshihiro Shimoda5c481a62011-09-30 20:07:21 +0900224 if (r8a66597->pdata->wr0_shorted_to_wr1)
225 r8a66597_bclr(r8a66597, MBW_16, ep->fifosel);
Magnus Damm59c82d12009-09-30 11:57:24 +0000226 for (i = 0; i < len; i++)
Paul Mundte8b48662010-06-02 16:27:12 +0900227 iowrite8(buf[i], fifoaddr + adj - (i & adj));
Yoshihiro Shimoda5c481a62011-09-30 20:07:21 +0900228 if (r8a66597->pdata->wr0_shorted_to_wr1)
229 r8a66597_bclr(r8a66597, MBW_16, ep->fifosel);
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000230}
231
232static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
233{
234 u16 clock = 0;
235
236 switch (pdata->xtal) {
237 case R8A66597_PLATDATA_XTAL_12MHZ:
238 clock = XTAL12;
239 break;
240 case R8A66597_PLATDATA_XTAL_24MHZ:
241 clock = XTAL24;
242 break;
243 case R8A66597_PLATDATA_XTAL_48MHZ:
244 clock = XTAL48;
245 break;
246 default:
247 printk(KERN_ERR "r8a66597: platdata clock is wrong.\n");
248 break;
249 }
250
251 return clock;
252}
253
Yoshihiro Shimodac4144242009-08-19 04:59:39 +0000254#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
255
256#define enable_irq_ready(r8a66597, pipenum) \
257 enable_pipe_irq(r8a66597, pipenum, BRDYENB)
258#define disable_irq_ready(r8a66597, pipenum) \
259 disable_pipe_irq(r8a66597, pipenum, BRDYENB)
260#define enable_irq_empty(r8a66597, pipenum) \
261 enable_pipe_irq(r8a66597, pipenum, BEMPENB)
262#define disable_irq_empty(r8a66597, pipenum) \
263 disable_pipe_irq(r8a66597, pipenum, BEMPENB)
264#define enable_irq_nrdy(r8a66597, pipenum) \
265 enable_pipe_irq(r8a66597, pipenum, NRDYENB)
266#define disable_irq_nrdy(r8a66597, pipenum) \
267 disable_pipe_irq(r8a66597, pipenum, NRDYENB)
268
269#endif /* __R8A66597_H__ */
270