USB: Add MUSB and TUSB support
[sfrench/cifs-2.6.git] / drivers / usb / musb / musb_core.h
1 /*
2  * MUSB OTG driver defines
3  *
4  * Copyright 2005 Mentor Graphics Corporation
5  * Copyright (C) 2005-2006 by Texas Instruments
6  * Copyright (C) 2006-2007 Nokia Corporation
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * 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
20  * 02110-1301 USA
21  *
22  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
25  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #ifndef __MUSB_CORE_H__
36 #define __MUSB_CORE_H__
37
38 #include <linux/slab.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/smp_lock.h>
42 #include <linux/errno.h>
43 #include <linux/clk.h>
44 #include <linux/device.h>
45 #include <linux/usb/ch9.h>
46 #include <linux/usb/gadget.h>
47 #include <linux/usb.h>
48 #include <linux/usb/otg.h>
49 #include <linux/usb/musb.h>
50
51 struct musb;
52 struct musb_hw_ep;
53 struct musb_ep;
54
55
56 #include "musb_debug.h"
57 #include "musb_dma.h"
58
59 #ifdef CONFIG_USB_MUSB_SOC
60 /*
61  * Get core configuration from a header converted (by cfg_conv)
62  * from the Verilog config file generated by the core config utility
63  *
64  * For now we assume that header is provided along with other
65  * arch-specific files.  Discrete chips will need a build tweak.
66  * So will using AHB IDs from silicon that provides them.
67  */
68 #include <asm/arch/hdrc_cnf.h>
69 #endif
70
71 #include "musb_io.h"
72 #include "musb_regs.h"
73
74 #include "musb_gadget.h"
75 #include "../core/hcd.h"
76 #include "musb_host.h"
77
78
79
80 #ifdef CONFIG_USB_MUSB_OTG
81
82 #define is_peripheral_enabled(musb)     ((musb)->board_mode != MUSB_HOST)
83 #define is_host_enabled(musb)           ((musb)->board_mode != MUSB_PERIPHERAL)
84 #define is_otg_enabled(musb)            ((musb)->board_mode == MUSB_OTG)
85
86 /* NOTE:  otg and peripheral-only state machines start at B_IDLE.
87  * OTG or host-only go to A_IDLE when ID is sensed.
88  */
89 #define is_peripheral_active(m)         (!(m)->is_host)
90 #define is_host_active(m)               ((m)->is_host)
91
92 #else
93 #define is_peripheral_enabled(musb)     is_peripheral_capable()
94 #define is_host_enabled(musb)           is_host_capable()
95 #define is_otg_enabled(musb)            0
96
97 #define is_peripheral_active(musb)      is_peripheral_capable()
98 #define is_host_active(musb)            is_host_capable()
99 #endif
100
101 #if defined(CONFIG_USB_MUSB_OTG) || defined(CONFIG_USB_MUSB_PERIPHERAL)
102 /* for some reason, the "select USB_GADGET_MUSB_HDRC" doesn't always
103  * override that choice selection (often USB_GADGET_DUMMY_HCD).
104  */
105 #ifndef CONFIG_USB_GADGET_MUSB_HDRC
106 #error bogus Kconfig output ... select CONFIG_USB_GADGET_MUSB_HDRC
107 #endif
108 #endif  /* need MUSB gadget selection */
109
110
111 #ifdef CONFIG_PROC_FS
112 #include <linux/fs.h>
113 #define MUSB_CONFIG_PROC_FS
114 #endif
115
116 /****************************** PERIPHERAL ROLE *****************************/
117
118 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
119
120 #define is_peripheral_capable() (1)
121
122 extern irqreturn_t musb_g_ep0_irq(struct musb *);
123 extern void musb_g_tx(struct musb *, u8);
124 extern void musb_g_rx(struct musb *, u8);
125 extern void musb_g_reset(struct musb *);
126 extern void musb_g_suspend(struct musb *);
127 extern void musb_g_resume(struct musb *);
128 extern void musb_g_wakeup(struct musb *);
129 extern void musb_g_disconnect(struct musb *);
130
131 #else
132
133 #define is_peripheral_capable() (0)
134
135 static inline irqreturn_t musb_g_ep0_irq(struct musb *m) { return IRQ_NONE; }
136 static inline void musb_g_reset(struct musb *m) {}
137 static inline void musb_g_suspend(struct musb *m) {}
138 static inline void musb_g_resume(struct musb *m) {}
139 static inline void musb_g_wakeup(struct musb *m) {}
140 static inline void musb_g_disconnect(struct musb *m) {}
141
142 #endif
143
144 /****************************** HOST ROLE ***********************************/
145
146 #ifdef CONFIG_USB_MUSB_HDRC_HCD
147
148 #define is_host_capable()       (1)
149
150 extern irqreturn_t musb_h_ep0_irq(struct musb *);
151 extern void musb_host_tx(struct musb *, u8);
152 extern void musb_host_rx(struct musb *, u8);
153
154 #else
155
156 #define is_host_capable()       (0)
157
158 static inline irqreturn_t musb_h_ep0_irq(struct musb *m) { return IRQ_NONE; }
159 static inline void musb_host_tx(struct musb *m, u8 e) {}
160 static inline void musb_host_rx(struct musb *m, u8 e) {}
161
162 #endif
163
164
165 /****************************** CONSTANTS ********************************/
166
167 #ifndef MUSB_C_NUM_EPS
168 #define MUSB_C_NUM_EPS ((u8)16)
169 #endif
170
171 #ifndef MUSB_MAX_END0_PACKET
172 #define MUSB_MAX_END0_PACKET ((u16)MUSB_EP0_FIFOSIZE)
173 #endif
174
175 /* host side ep0 states */
176 enum musb_h_ep0_state {
177         MUSB_EP0_IDLE,
178         MUSB_EP0_START,                 /* expect ack of setup */
179         MUSB_EP0_IN,                    /* expect IN DATA */
180         MUSB_EP0_OUT,                   /* expect ack of OUT DATA */
181         MUSB_EP0_STATUS,                /* expect ack of STATUS */
182 } __attribute__ ((packed));
183
184 /* peripheral side ep0 states */
185 enum musb_g_ep0_state {
186         MUSB_EP0_STAGE_SETUP,           /* idle, waiting for setup */
187         MUSB_EP0_STAGE_TX,              /* IN data */
188         MUSB_EP0_STAGE_RX,              /* OUT data */
189         MUSB_EP0_STAGE_STATUSIN,        /* (after OUT data) */
190         MUSB_EP0_STAGE_STATUSOUT,       /* (after IN data) */
191         MUSB_EP0_STAGE_ACKWAIT,         /* after zlp, before statusin */
192 } __attribute__ ((packed));
193
194 /* OTG protocol constants */
195 #define OTG_TIME_A_WAIT_VRISE   100             /* msec (max) */
196 #define OTG_TIME_A_WAIT_BCON    0               /* 0=infinite; min 1000 msec */
197 #define OTG_TIME_A_IDLE_BDIS    200             /* msec (min) */
198
199 /*************************** REGISTER ACCESS ********************************/
200
201 /* Endpoint registers (other than dynfifo setup) can be accessed either
202  * directly with the "flat" model, or after setting up an index register.
203  */
204
205 #if defined(CONFIG_ARCH_DAVINCI) || defined(CONFIG_ARCH_OMAP2430) \
206                 || defined(CONFIG_ARCH_OMAP3430)
207 /* REVISIT indexed access seemed to
208  * misbehave (on DaVinci) for at least peripheral IN ...
209  */
210 #define MUSB_FLAT_REG
211 #endif
212
213 /* TUSB mapping: "flat" plus ep0 special cases */
214 #if     defined(CONFIG_USB_TUSB6010)
215 #define musb_ep_select(_mbase, _epnum) \
216         musb_writeb((_mbase), MUSB_INDEX, (_epnum))
217 #define MUSB_EP_OFFSET                  MUSB_TUSB_OFFSET
218
219 /* "flat" mapping: each endpoint has its own i/o address */
220 #elif   defined(MUSB_FLAT_REG)
221 #define musb_ep_select(_mbase, _epnum)  (((void)(_mbase)), ((void)(_epnum)))
222 #define MUSB_EP_OFFSET                  MUSB_FLAT_OFFSET
223
224 /* "indexed" mapping: INDEX register controls register bank select */
225 #else
226 #define musb_ep_select(_mbase, _epnum) \
227         musb_writeb((_mbase), MUSB_INDEX, (_epnum))
228 #define MUSB_EP_OFFSET                  MUSB_INDEXED_OFFSET
229 #endif
230
231 /****************************** FUNCTIONS ********************************/
232
233 #define MUSB_HST_MODE(_musb)\
234         { (_musb)->is_host = true; }
235 #define MUSB_DEV_MODE(_musb) \
236         { (_musb)->is_host = false; }
237
238 #define test_devctl_hst_mode(_x) \
239         (musb_readb((_x)->mregs, MUSB_DEVCTL)&MUSB_DEVCTL_HM)
240
241 #define MUSB_MODE(musb) ((musb)->is_host ? "Host" : "Peripheral")
242
243 /******************************** TYPES *************************************/
244
245 /*
246  * struct musb_hw_ep - endpoint hardware (bidirectional)
247  *
248  * Ordered slightly for better cacheline locality.
249  */
250 struct musb_hw_ep {
251         struct musb             *musb;
252         void __iomem            *fifo;
253         void __iomem            *regs;
254
255 #ifdef CONFIG_USB_TUSB6010
256         void __iomem            *conf;
257 #endif
258
259         /* index in musb->endpoints[]  */
260         u8                      epnum;
261
262         /* hardware configuration, possibly dynamic */
263         bool                    is_shared_fifo;
264         bool                    tx_double_buffered;
265         bool                    rx_double_buffered;
266         u16                     max_packet_sz_tx;
267         u16                     max_packet_sz_rx;
268
269         struct dma_channel      *tx_channel;
270         struct dma_channel      *rx_channel;
271
272 #ifdef CONFIG_USB_TUSB6010
273         /* TUSB has "asynchronous" and "synchronous" dma modes */
274         dma_addr_t              fifo_async;
275         dma_addr_t              fifo_sync;
276         void __iomem            *fifo_sync_va;
277 #endif
278
279 #ifdef CONFIG_USB_MUSB_HDRC_HCD
280         void __iomem            *target_regs;
281
282         /* currently scheduled peripheral endpoint */
283         struct musb_qh          *in_qh;
284         struct musb_qh          *out_qh;
285
286         u8                      rx_reinit;
287         u8                      tx_reinit;
288 #endif
289
290 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
291         /* peripheral side */
292         struct musb_ep          ep_in;                  /* TX */
293         struct musb_ep          ep_out;                 /* RX */
294 #endif
295 };
296
297 static inline struct usb_request *next_in_request(struct musb_hw_ep *hw_ep)
298 {
299 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
300         return next_request(&hw_ep->ep_in);
301 #else
302         return NULL;
303 #endif
304 }
305
306 static inline struct usb_request *next_out_request(struct musb_hw_ep *hw_ep)
307 {
308 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
309         return next_request(&hw_ep->ep_out);
310 #else
311         return NULL;
312 #endif
313 }
314
315 /*
316  * struct musb - Driver instance data.
317  */
318 struct musb {
319         /* device lock */
320         spinlock_t              lock;
321         struct clk              *clock;
322         irqreturn_t             (*isr)(int, void *);
323         struct work_struct      irq_work;
324
325 /* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
326 #define MUSB_PORT_STAT_RESUME   (1 << 31)
327
328         u32                     port1_status;
329
330 #ifdef CONFIG_USB_MUSB_HDRC_HCD
331         unsigned long           rh_timer;
332
333         enum musb_h_ep0_state   ep0_stage;
334
335         /* bulk traffic normally dedicates endpoint hardware, and each
336          * direction has its own ring of host side endpoints.
337          * we try to progress the transfer at the head of each endpoint's
338          * queue until it completes or NAKs too much; then we try the next
339          * endpoint.
340          */
341         struct musb_hw_ep       *bulk_ep;
342
343         struct list_head        control;        /* of musb_qh */
344         struct list_head        in_bulk;        /* of musb_qh */
345         struct list_head        out_bulk;       /* of musb_qh */
346         struct musb_qh          *periodic[32];  /* tree of interrupt+iso */
347 #endif
348
349         /* called with IRQs blocked; ON/nonzero implies starting a session,
350          * and waiting at least a_wait_vrise_tmout.
351          */
352         void                    (*board_set_vbus)(struct musb *, int is_on);
353
354         struct dma_controller   *dma_controller;
355
356         struct device           *controller;
357         void __iomem            *ctrl_base;
358         void __iomem            *mregs;
359
360 #ifdef CONFIG_USB_TUSB6010
361         dma_addr_t              async;
362         dma_addr_t              sync;
363         void __iomem            *sync_va;
364 #endif
365
366         /* passed down from chip/board specific irq handlers */
367         u8                      int_usb;
368         u16                     int_rx;
369         u16                     int_tx;
370
371         struct otg_transceiver  xceiv;
372
373         int nIrq;
374
375         struct musb_hw_ep        endpoints[MUSB_C_NUM_EPS];
376 #define control_ep              endpoints
377
378 #define VBUSERR_RETRY_COUNT     3
379         u16                     vbuserr_retry;
380         u16 epmask;
381         u8 nr_endpoints;
382
383         u8 board_mode;          /* enum musb_mode */
384         int                     (*board_set_power)(int state);
385
386         int                     (*set_clock)(struct clk *clk, int is_active);
387
388         u8                      min_power;      /* vbus for periph, in mA/2 */
389
390         bool                    is_host;
391
392         int                     a_wait_bcon;    /* VBUS timeout in msecs */
393         unsigned long           idle_timeout;   /* Next timeout in jiffies */
394
395         /* active means connected and not suspended */
396         unsigned                is_active:1;
397
398         unsigned is_multipoint:1;
399         unsigned ignore_disconnect:1;   /* during bus resets */
400
401 #ifdef C_MP_TX
402         unsigned bulk_split:1;
403 #define can_bulk_split(musb,type) \
404                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_split)
405 #else
406 #define can_bulk_split(musb, type)      0
407 #endif
408
409 #ifdef C_MP_RX
410         unsigned bulk_combine:1;
411 #define can_bulk_combine(musb,type) \
412                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_combine)
413 #else
414 #define can_bulk_combine(musb, type)    0
415 #endif
416
417 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
418         /* is_suspended means USB B_PERIPHERAL suspend */
419         unsigned                is_suspended:1;
420
421         /* may_wakeup means remote wakeup is enabled */
422         unsigned                may_wakeup:1;
423
424         /* is_self_powered is reported in device status and the
425          * config descriptor.  is_bus_powered means B_PERIPHERAL
426          * draws some VBUS current; both can be true.
427          */
428         unsigned                is_self_powered:1;
429         unsigned                is_bus_powered:1;
430
431         unsigned                set_address:1;
432         unsigned                test_mode:1;
433         unsigned                softconnect:1;
434
435         u8                      address;
436         u8                      test_mode_nr;
437         u16                     ackpend;                /* ep0 */
438         enum musb_g_ep0_state   ep0_state;
439         struct usb_gadget       g;                      /* the gadget */
440         struct usb_gadget_driver *gadget_driver;        /* its driver */
441 #endif
442
443 #ifdef MUSB_CONFIG_PROC_FS
444         struct proc_dir_entry *proc_entry;
445 #endif
446 };
447
448 static inline void musb_set_vbus(struct musb *musb, int is_on)
449 {
450         musb->board_set_vbus(musb, is_on);
451 }
452
453 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
454 static inline struct musb *gadget_to_musb(struct usb_gadget *g)
455 {
456         return container_of(g, struct musb, g);
457 }
458 #endif
459
460
461 /***************************** Glue it together *****************************/
462
463 extern const char musb_driver_name[];
464
465 extern void musb_start(struct musb *musb);
466 extern void musb_stop(struct musb *musb);
467
468 extern void musb_write_fifo(struct musb_hw_ep *ep, u16 len, const u8 *src);
469 extern void musb_read_fifo(struct musb_hw_ep *ep, u16 len, u8 *dst);
470
471 extern void musb_load_testpacket(struct musb *);
472
473 extern irqreturn_t musb_interrupt(struct musb *);
474
475 extern void musb_platform_enable(struct musb *musb);
476 extern void musb_platform_disable(struct musb *musb);
477
478 extern void musb_hnp_stop(struct musb *musb);
479
480 extern void musb_platform_set_mode(struct musb *musb, u8 musb_mode);
481
482 #if defined(CONFIG_USB_TUSB6010) || \
483         defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
484 extern void musb_platform_try_idle(struct musb *musb, unsigned long timeout);
485 #else
486 #define musb_platform_try_idle(x, y)            do {} while (0)
487 #endif
488
489 #ifdef CONFIG_USB_TUSB6010
490 extern int musb_platform_get_vbus_status(struct musb *musb);
491 #else
492 #define musb_platform_get_vbus_status(x)        0
493 #endif
494
495 extern int __init musb_platform_init(struct musb *musb);
496 extern int musb_platform_exit(struct musb *musb);
497
498 /*-------------------------- ProcFS definitions ---------------------*/
499
500 struct proc_dir_entry;
501
502 #if (MUSB_DEBUG > 0) && defined(MUSB_CONFIG_PROC_FS)
503 extern struct proc_dir_entry *musb_debug_create(char *name, struct musb *data);
504 extern void musb_debug_delete(char *name, struct musb *data);
505
506 #else
507 static inline struct proc_dir_entry *
508 musb_debug_create(char *name, struct musb *data)
509 {
510         return NULL;
511 }
512 static inline void musb_debug_delete(char *name, struct musb *data)
513 {
514 }
515 #endif
516
517 #endif  /* __MUSB_CORE_H__ */