Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * R8A66597 UDC |
| 3 | * |
| 4 | * Copyright (C) 2007-2009 Renesas Solutions Corp. |
| 5 | * |
Yoshihiro Shimoda | 5db05c0 | 2011-07-07 09:59:07 +0900 | [diff] [blame] | 6 | * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 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. |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef __R8A66597_H__ |
| 14 | #define __R8A66597_H__ |
| 15 | |
Magnus Damm | d2e27bd | 2009-08-19 09:50:49 +0000 | [diff] [blame] | 16 | #ifdef CONFIG_HAVE_CLK |
| 17 | #include <linux/clk.h> |
| 18 | #endif |
| 19 | |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 20 | #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 | |
| 46 | struct r8a66597_pipe_info { |
| 47 | u16 pipe; |
| 48 | u16 epnum; |
| 49 | u16 maxpacket; |
| 50 | u16 type; |
| 51 | u16 interval; |
| 52 | u16 dir_in; |
| 53 | }; |
| 54 | |
| 55 | struct r8a66597_request { |
| 56 | struct usb_request req; |
| 57 | struct list_head queue; |
| 58 | }; |
| 59 | |
| 60 | struct r8a66597_ep { |
| 61 | struct usb_ep ep; |
| 62 | struct r8a66597 *r8a66597; |
| 63 | |
| 64 | struct list_head queue; |
| 65 | unsigned busy:1; |
Yoshihiro Shimoda | 9e7291c | 2009-08-20 07:01:06 +0000 | [diff] [blame] | 66 | unsigned wedge:1; |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 67 | 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 | |
| 82 | struct r8a66597 { |
| 83 | spinlock_t lock; |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 84 | void __iomem *reg; |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 85 | |
Magnus Damm | d2e27bd | 2009-08-19 09:50:49 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_HAVE_CLK |
| 87 | struct clk *clk; |
| 88 | #endif |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 89 | 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 Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 106 | 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 | |
| 118 | static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset) |
| 119 | { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 120 | return ioread16(r8a66597->reg + offset); |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597, |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 124 | unsigned long offset, |
| 125 | unsigned char *buf, |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 126 | int len) |
| 127 | { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 128 | void __iomem *fifoaddr = r8a66597->reg + offset; |
Javier Martinez Canillas | 92d3489 | 2010-09-10 08:42:08 -0400 | [diff] [blame] | 129 | unsigned int data = 0; |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 130 | int i; |
| 131 | |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 132 | if (r8a66597->pdata->on_chip) { |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 133 | /* 32-bit accesses for on_chip controllers */ |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 134 | |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 135 | /* aligned buf case */ |
| 136 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 137 | ioread32_rep(fifoaddr, buf, len / 4); |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 138 | buf += len & ~0x03; |
| 139 | len &= 0x03; |
| 140 | } |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 141 | |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 142 | /* unaligned buf case */ |
| 143 | for (i = 0; i < len; i++) { |
| 144 | if (!(i & 0x03)) |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 145 | data = ioread32(fifoaddr); |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 146 | |
| 147 | buf[i] = (data >> ((i & 0x03) * 8)) & 0xff; |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 148 | } |
| 149 | } else { |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 150 | /* 16-bit accesses for external controllers */ |
| 151 | |
| 152 | /* aligned buf case */ |
| 153 | if (len >= 2 && !((unsigned long)buf & 0x01)) { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 154 | ioread16_rep(fifoaddr, buf, len / 2); |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 155 | buf += len & ~0x01; |
| 156 | len &= 0x01; |
| 157 | } |
| 158 | |
| 159 | /* unaligned buf case */ |
| 160 | for (i = 0; i < len; i++) { |
| 161 | if (!(i & 0x01)) |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 162 | data = ioread16(fifoaddr); |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 163 | |
| 164 | buf[i] = (data >> ((i & 0x01) * 8)) & 0xff; |
| 165 | } |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val, |
| 170 | unsigned long offset) |
| 171 | { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 172 | iowrite16(val, r8a66597->reg + offset); |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597, |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 176 | unsigned long offset, |
| 177 | unsigned char *buf, |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 178 | int len) |
| 179 | { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 180 | void __iomem *fifoaddr = r8a66597->reg + offset; |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 181 | int adj = 0; |
| 182 | int i; |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 183 | |
| 184 | if (r8a66597->pdata->on_chip) { |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 185 | /* 32-bit access only if buf is 32-bit aligned */ |
| 186 | if (len >= 4 && !((unsigned long)buf & 0x03)) { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 187 | iowrite32_rep(fifoaddr, buf, len / 4); |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 188 | buf += len & ~0x03; |
| 189 | len &= 0x03; |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 190 | } |
| 191 | } else { |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 192 | /* 16-bit access only if buf is 16-bit aligned */ |
| 193 | if (len >= 2 && !((unsigned long)buf & 0x01)) { |
Paul Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 194 | iowrite16_rep(fifoaddr, buf, len / 2); |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 195 | buf += len & ~0x01; |
| 196 | len &= 0x01; |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
Magnus Damm | 59c82d1 | 2009-09-30 11:57:24 +0000 | [diff] [blame] | 199 | |
| 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 Mundt | e8b4866 | 2010-06-02 16:27:12 +0900 | [diff] [blame] | 209 | iowrite8(buf[i], fifoaddr + adj - (i & adj)); |
Yoshihiro Shimoda | c414424 | 2009-08-19 04:59:39 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static 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 | |
| 222 | static 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 | |