]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/musbdefs.h
Merge omap-upstream
[linux-2.6-omap-h63xx.git] / drivers / usb / musb / musbdefs.h
1 /******************************************************************
2  * Copyright 2005 Mentor Graphics Corporation
3  * Copyright (C) 2005-2006 by Texas Instruments
4  *
5  * This file is part of the Inventra Controller Driver for Linux.
6  *
7  * The Inventra Controller Driver for Linux is free software; you
8  * can redistribute it and/or modify it under the terms of the GNU
9  * General Public License version 2 as published by the Free Software
10  * Foundation.
11  *
12  * The Inventra Controller Driver for Linux is distributed in
13  * the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with The Inventra Controller Driver for Linux ; if not,
20  * write to the Free Software Foundation, Inc., 59 Temple Place,
21  * Suite 330, Boston, MA  02111-1307  USA
22  *
23  * ANY DOWNLOAD, USE, REPRODUCTION, MODIFICATION OR DISTRIBUTION
24  * OF THIS DRIVER INDICATES YOUR COMPLETE AND UNCONDITIONAL ACCEPTANCE
25  * OF THOSE TERMS.THIS DRIVER IS PROVIDED "AS IS" AND MENTOR GRAPHICS
26  * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, RELATED TO THIS DRIVER.
27  * MENTOR GRAPHICS SPECIFICALLY DISCLAIMS ALL IMPLIED WARRANTIES
28  * OF MERCHANTABILITY; FITNESS FOR A PARTICULAR PURPOSE AND
29  * NON-INFRINGEMENT.  MENTOR GRAPHICS DOES NOT PROVIDE SUPPORT
30  * SERVICES OR UPDATES FOR THIS DRIVER, EVEN IF YOU ARE A MENTOR
31  * GRAPHICS SUPPORT CUSTOMER.
32  ******************************************************************/
33
34 #ifndef __MUSB_MUSBDEFS_H__
35 #define __MUSB_MUSBDEFS_H__
36
37 #include <linux/slab.h>
38 #include <linux/list.h>
39 #include <linux/interrupt.h>
40 #include <linux/smp_lock.h>
41 #include <linux/errno.h>
42 #include <linux/clk.h>
43 #include <linux/device.h>
44 #include <linux/usb/ch9.h>
45 #include <linux/usb_gadget.h>
46 #include <linux/usb.h>
47 #include <linux/usb/otg.h>
48 #include <linux/usb/musb.h>
49
50 struct musb;
51 struct musb_hw_ep;
52 struct musb_ep;
53
54
55 #include "debug.h"
56 #include "dma.h"
57
58 #ifdef CONFIG_USB_MUSB_SOC
59 /*
60  * Get core configuration from a header converted (by cfg_conv)
61  * from the Verilog config file generated by the core config utility
62  *
63  * For now we assume that header is provided along with other
64  * arch-specific files.  Discrete chips will need a build tweak.
65  * So will using AHB IDs from silicon that provides them.
66  */
67 #include <asm/arch/hdrc_cnf.h>
68 #endif
69
70 #include "plat_arc.h"
71 #include "musbhdrc.h"
72
73 #include "musb_gadget.h"
74 #include "../core/hcd.h"
75 #include "musb_host.h"
76
77
78
79 #ifdef CONFIG_USB_MUSB_OTG
80
81 #define is_peripheral_enabled(musb)     ((musb)->board_mode != MUSB_HOST)
82 #define is_host_enabled(musb)           ((musb)->board_mode != MUSB_PERIPHERAL)
83 #define is_otg_enabled(musb)            ((musb)->board_mode == MUSB_OTG)
84
85 /* NOTE:  otg and peripheral-only state machines start at B_IDLE.
86  * OTG or host-only go to A_IDLE when ID is sensed.
87  */
88 #define is_peripheral_active(m)         (!(m)->bIsHost)
89 #define is_host_active(m)               ((m)->bIsHost)
90
91 #else
92 #define is_peripheral_enabled(musb)     is_peripheral_capable()
93 #define is_host_enabled(musb)           is_host_capable()
94 #define is_otg_enabled(musb)            0
95
96 #define is_peripheral_active(musb)      is_peripheral_capable()
97 #define is_host_active(musb)            is_host_capable()
98 #endif
99
100 #if defined(CONFIG_USB_MUSB_OTG) || defined(CONFIG_USB_MUSB_PERIPHERAL)
101 /* for some reason, the "select USB_GADGET_MUSB_HDRC" doesn't always
102  * override that choice selection (often USB_GADGET_DUMMY_HCD).
103  */
104 #ifndef CONFIG_USB_GADGET_MUSB_HDRC
105 #error bogus Kconfig output ... select CONFIG_USB_GADGET_MUSB_HDRC
106 #endif
107 #endif  /* need MUSB gadget selection */
108
109
110 #ifdef CONFIG_PROC_FS
111 #include <linux/fs.h>
112 #define MUSB_CONFIG_PROC_FS
113 #endif
114
115 /****************************** PERIPHERAL ROLE *****************************/
116
117 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
118
119 #define is_peripheral_capable() (1)
120
121 extern irqreturn_t musb_g_ep0_irq(struct musb *);
122 extern void musb_g_tx(struct musb *, u8);
123 extern void musb_g_rx(struct musb *, u8);
124 extern void musb_g_reset(struct musb *);
125 extern void musb_g_suspend(struct musb *);
126 extern void musb_g_resume(struct musb *);
127 extern void musb_g_wakeup(struct musb *);
128 extern void musb_g_disconnect(struct musb *);
129
130 #else
131
132 #define is_peripheral_capable() (0)
133
134 static inline irqreturn_t musb_g_ep0_irq(struct musb *m) { return IRQ_NONE; }
135 static inline void musb_g_reset(struct musb *m) {}
136 static inline void musb_g_suspend(struct musb *m) {}
137 static inline void musb_g_resume(struct musb *m) {}
138 static inline void musb_g_wakeup(struct musb *m) {}
139 static inline void musb_g_disconnect(struct musb *m) {}
140
141 #endif
142
143 /****************************** HOST ROLE ***********************************/
144
145 #ifdef CONFIG_USB_MUSB_HDRC_HCD
146
147 #define is_host_capable()       (1)
148
149 extern irqreturn_t musb_h_ep0_irq(struct musb *);
150 extern void musb_host_tx(struct musb *, u8);
151 extern void musb_host_rx(struct musb *, u8);
152
153 #else
154
155 #define is_host_capable()       (0)
156
157 static inline irqreturn_t musb_h_ep0_irq(struct musb *m) { return IRQ_NONE; }
158 static inline void musb_host_tx(struct musb *m, u8 e) {}
159 static inline void musb_host_rx(struct musb *m, u8 e) {}
160
161 #endif
162
163
164 /****************************** CONSTANTS ********************************/
165
166 #ifndef TRUE
167 #define TRUE 1
168 #endif
169 #ifndef FALSE
170 #define FALSE 0
171 #endif
172
173 #ifndef MUSB_C_NUM_EPS
174 #define MUSB_C_NUM_EPS ((u8)16)
175 #endif
176
177 #ifndef MUSB_MAX_END0_PACKET
178 #define MUSB_MAX_END0_PACKET ((u16)MGC_END0_FIFOSIZE)
179 #endif
180
181 /* host side ep0 states */
182 enum musb_h_ep0_state {
183         MGC_END0_IDLE,
184         MGC_END0_START,                 /* expect ack of setup */
185         MGC_END0_IN,                    /* expect IN DATA */
186         MGC_END0_OUT,                   /* expect ack of OUT DATA */
187         MGC_END0_STATUS,                /* expect ack of STATUS */
188 } __attribute__ ((packed));
189
190 /* peripheral side ep0 states */
191 enum musb_g_ep0_state {
192         MGC_END0_STAGE_SETUP,           /* idle, waiting for setup */
193         MGC_END0_STAGE_TX,              /* IN data */
194         MGC_END0_STAGE_RX,              /* OUT data */
195         MGC_END0_STAGE_STATUSIN,        /* (after OUT data) */
196         MGC_END0_STAGE_STATUSOUT,       /* (after IN data) */
197         MGC_END0_STAGE_ACKWAIT,         /* after zlp, before statusin */
198 } __attribute__ ((packed));
199
200 /* OTG protocol constants */
201 #define OTG_TIME_A_WAIT_VRISE   100             /* msec (max) */
202 #define OTG_TIME_A_WAIT_BCON    0               /* 0=infinite; min 1000 msec */
203 #define OTG_TIME_A_IDLE_BDIS    200             /* msec (min) */
204
205 /*************************** REGISTER ACCESS ********************************/
206
207 /* Endpoint registers (other than dynfifo setup) can be accessed either
208  * directly with the "flat" model, or after setting up an index register.
209  */
210
211 #if defined(CONFIG_ARCH_DAVINCI) || defined(CONFIG_ARCH_OMAP2430) || \
212                                      defined(CONFIG_ARCH_OMAP3430)
213 /* REVISIT indexed access seemed to
214  * misbehave (on DaVinci) for at least peripheral IN ...
215  */
216 #define MUSB_FLAT_REG
217 #endif
218
219 /* TUSB mapping: "flat" plus ep0 special cases */
220 #if     defined(CONFIG_USB_TUSB6010)
221 #define MGC_SelectEnd(_pBase, _bEnd) \
222         musb_writeb((_pBase), MGC_O_HDRC_INDEX, (_bEnd))
223 #define MGC_END_OFFSET                  MGC_TUSB_OFFSET
224
225 /* "flat" mapping: each endpoint has its own i/o address */
226 #elif   defined(MUSB_FLAT_REG)
227 #define MGC_SelectEnd(_pBase, _bEnd)    (((void)(_pBase)),((void)(_bEnd)))
228 #define MGC_END_OFFSET                  MGC_FLAT_OFFSET
229
230 /* "indexed" mapping: INDEX register controls register bank select */
231 #else
232 #define MGC_SelectEnd(_pBase, _bEnd) \
233         musb_writeb((_pBase), MGC_O_HDRC_INDEX, (_bEnd))
234 #define MGC_END_OFFSET                  MGC_INDEXED_OFFSET
235 #endif
236
237 /****************************** FUNCTIONS ********************************/
238
239 #define MUSB_HST_MODE(_pthis)\
240         { (_pthis)->bIsHost=TRUE; }
241 #define MUSB_DEV_MODE(_pthis) \
242         { (_pthis)->bIsHost=FALSE; }
243
244 #define test_devctl_hst_mode(_x) \
245         (musb_readb((_x)->pRegs, MGC_O_HDRC_DEVCTL)&MGC_M_DEVCTL_HM)
246
247 #define MUSB_MODE(musb) ((musb)->bIsHost ? "Host" : "Peripheral")
248
249 /************************** Ep Configuration ********************************/
250
251 /** The End point descriptor */
252 struct MUSB_EpFifoDescriptor {
253         u8 bType;               /* 0 for autoconfig, CNTR, ISOC, BULK, INTR */
254         u8 bDir;                /* 0 for autoconfig, INOUT, IN, OUT */
255         int wSize;              /* 0 for autoconfig, or the size */
256 };
257
258 #define MUSB_EPD_AUTOCONFIG     0
259
260 #define MUSB_EPD_T_CNTRL        1
261 #define MUSB_EPD_T_ISOC         2
262 #define MUSB_EPD_T_BULK         3
263 #define MUSB_EPD_T_INTR         4
264
265 #define MUSB_EPD_D_INOUT        0
266 #define MUSB_EPD_D_TX           1
267 #define MUSB_EPD_D_RX           2
268
269 /******************************** TYPES *************************************/
270
271 /*
272  * struct musb_hw_ep - endpoint hardware (bidirectional)
273  *
274  * Ordered slightly for better cacheline locality.
275  */
276 struct musb_hw_ep {
277         struct musb             *musb;
278         void __iomem            *fifo;
279         void __iomem            *regs;
280
281 #ifdef CONFIG_USB_TUSB6010
282         void __iomem            *conf;
283 #endif
284
285         /* index in musb->aLocalEnd[]  */
286         u8                      bLocalEnd;
287
288         /* hardware configuration, possibly dynamic */
289         u8                      bIsSharedFifo;
290         u8                      tx_double_buffered;
291         u8                      rx_double_buffered;
292         u16                     wMaxPacketSizeTx;
293         u16                     wMaxPacketSizeRx;
294
295         struct dma_channel      *tx_channel;
296         struct dma_channel      *rx_channel;
297
298 #ifdef CONFIG_USB_TUSB6010
299         /* TUSB has "asynchronous" and "synchronous" dma modes */
300         dma_addr_t              fifo_async;
301         dma_addr_t              fifo_sync;
302         void __iomem            *fifo_sync_va;
303 #endif
304
305 #ifdef CONFIG_USB_MUSB_HDRC_HCD
306         void __iomem            *target_regs;
307
308         /* currently scheduled peripheral endpoint */
309         struct musb_qh          *in_qh;
310         struct musb_qh          *out_qh;
311
312         u8                      rx_reinit;
313         u8                      tx_reinit;
314 #endif
315
316 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
317         /* peripheral side */
318         struct musb_ep          ep_in;                  /* TX */
319         struct musb_ep          ep_out;                 /* RX */
320 #endif
321 };
322
323 static inline struct usb_request *next_in_request(struct musb_hw_ep *hw_ep)
324 {
325 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
326         return next_request(&hw_ep->ep_in);
327 #else
328         return NULL;
329 #endif
330 }
331
332 static inline struct usb_request *next_out_request(struct musb_hw_ep *hw_ep)
333 {
334 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
335         return next_request(&hw_ep->ep_out);
336 #else
337         return NULL;
338 #endif
339 }
340
341 /*
342  * struct musb - Driver instance data.
343  */
344 struct musb {
345         spinlock_t              Lock;
346         struct clk              *clock;
347         irqreturn_t             (*isr)(int, void *);
348         struct work_struct      irq_work;
349
350 #ifdef CONFIG_USB_MUSB_HDRC_HCD
351
352 /* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
353 #define MUSB_PORT_STAT_RESUME   (1 << 31)
354
355         u32                     port1_status;
356         unsigned long           rh_timer;
357
358         enum musb_h_ep0_state   bEnd0Stage;
359
360         /* bulk traffic normally dedicates endpoint hardware, and each
361          * direction has its own ring of host side endpoints.
362          * we try to progress the transfer at the head of each endpoint's
363          * queue until it completes or NAKs too much; then we try the next
364          * endpoint.
365          */
366         struct musb_hw_ep       *bulk_ep;
367
368         struct list_head        control;        /* of musb_qh */
369         struct list_head        in_bulk;        /* of musb_qh */
370         struct list_head        out_bulk;       /* of musb_qh */
371         struct musb_qh          *periodic[32];  /* tree of interrupt+iso */
372 #endif
373
374         /* called with IRQs blocked; ON/nonzero implies starting a session,
375          * and waiting at least a_wait_vrise_tmout.
376          */
377         void                    (*board_set_vbus)(struct musb *, int is_on);
378
379         struct dma_controller   *pDmaController;
380
381         struct device           *controller;
382         void __iomem            *ctrl_base;
383         void __iomem            *pRegs;
384
385 #ifdef CONFIG_USB_TUSB6010
386         dma_addr_t              async;
387         dma_addr_t              sync;
388         void __iomem            *sync_va;
389 #endif
390
391         /* passed down from chip/board specific irq handlers */
392         u8                      int_usb;
393         u16                     int_rx;
394         u16                     int_tx;
395
396         struct otg_transceiver  xceiv;
397
398         int nIrq;
399
400         struct musb_hw_ep        aLocalEnd[MUSB_C_NUM_EPS];
401 #define control_ep              aLocalEnd
402
403 #define VBUSERR_RETRY_COUNT     3
404         u16                     vbuserr_retry;
405         u16 wEndMask;
406         u8 bEndCount;
407
408         u8 board_mode;          /* enum musb_mode */
409         int                     (*board_set_power)(int state);
410
411         int                     (*set_clock)(struct clk *clk, int is_active);
412
413         u8                      min_power;      /* vbus for periph, in mA/2 */
414
415         /* active means connected and not suspended */
416         unsigned                is_active:1;
417
418         unsigned bIsMultipoint:1;
419         unsigned bIsHost:1;
420         unsigned bIgnoreDisconnect:1;   /* during bus resets */
421
422         int                     a_wait_bcon;    /* VBUS timeout in msecs */
423         unsigned long           idle_timeout;   /* Next timeout in jiffies */
424
425 #ifdef C_MP_TX
426         unsigned bBulkSplit:1;
427 #define can_bulk_split(musb,type) \
428                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bBulkSplit)
429 #else
430 #define can_bulk_split(musb,type)       0
431 #endif
432
433 #ifdef C_MP_RX
434         unsigned bBulkCombine:1;
435         /* REVISIT allegedly doesn't work reliably */
436 #if 0
437 #define can_bulk_combine(musb,type) \
438                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bBulkCombine)
439 #else
440 #define can_bulk_combine(musb,type)     0
441 #endif
442 #else
443 #define can_bulk_combine(musb,type)     0
444 #endif
445
446 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
447         /* is_suspended means USB B_PERIPHERAL suspend */
448         unsigned                is_suspended:1;
449
450         /* may_wakeup means remote wakeup is enabled */
451         unsigned                may_wakeup:1;
452
453         /* is_self_powered is reported in device status and the
454          * config descriptor.  is_bus_powered means B_PERIPHERAL
455          * draws some VBUS current; both can be true.
456          */
457         unsigned                is_self_powered:1;
458         unsigned                is_bus_powered:1;
459
460         unsigned bSetAddress:1;
461         unsigned bTestMode:1;
462         unsigned softconnect:1;
463
464         enum musb_g_ep0_state   ep0_state;
465         u8                      bAddress;
466         u8                      bTestModeValue;
467         u16                     ackpend;                /* ep0 */
468         struct usb_gadget       g;                      /* the gadget */
469         struct usb_gadget_driver *pGadgetDriver;        /* its driver */
470 #endif
471
472 #ifdef CONFIG_USB_MUSB_OTG
473         /* FIXME this can't be OTG-specific ... ? */
474         u8 bDelayPortPowerOff;
475 #endif
476
477 #ifdef MUSB_CONFIG_PROC_FS
478         struct proc_dir_entry *pProcEntry;
479 #endif
480 };
481
482 static inline void musb_set_vbus(struct musb *musb, int is_on)
483 {
484         musb->board_set_vbus(musb, is_on);
485 }
486
487 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
488 static inline struct musb *gadget_to_musb(struct usb_gadget *g)
489 {
490         return container_of(g, struct musb, g);
491 }
492 #endif
493
494
495 /***************************** Glue it together *****************************/
496
497 extern const char musb_driver_name[];
498
499 extern void musb_start(struct musb *pThis);
500 extern void musb_stop(struct musb *pThis);
501
502 extern void musb_write_fifo(struct musb_hw_ep *ep,
503                              u16 wCount, const u8 * pSource);
504 extern void musb_read_fifo(struct musb_hw_ep *ep,
505                                u16 wCount, u8 * pDest);
506
507 extern void musb_load_testpacket(struct musb *);
508
509 extern irqreturn_t musb_interrupt(struct musb *);
510
511 extern void musb_platform_enable(struct musb *musb);
512 extern void musb_platform_disable(struct musb *musb);
513
514 extern void musb_hnp_stop(struct musb *musb);
515
516 #ifdef CONFIG_USB_TUSB6010
517 extern void musb_platform_try_idle(struct musb *musb, unsigned long timeout);
518 extern int musb_platform_get_vbus_status(struct musb *musb);
519 extern void musb_platform_set_mode(struct musb *musb, u8 musb_mode);
520 #else
521 #define musb_platform_try_idle(x, y)            do {} while (0)
522 #define musb_platform_get_vbus_status(x)        0
523 #define musb_platform_set_mode(x, y)            do {} while (0)
524 #endif
525
526 extern int __init musb_platform_init(struct musb *musb);
527 extern int musb_platform_exit(struct musb *musb);
528
529 /*-------------------------- ProcFS definitions ---------------------*/
530
531 struct proc_dir_entry;
532
533 #if (MUSB_DEBUG > 0) && defined(MUSB_CONFIG_PROC_FS)
534 extern struct proc_dir_entry *musb_debug_create(char *name,
535                                                     struct musb *data);
536 extern void musb_debug_delete(char *name, struct musb *data);
537
538 #else
539 static inline struct proc_dir_entry *musb_debug_create(char *name,
540                                                            struct musb *data)
541 {
542         return NULL;
543 }
544 static inline void musb_debug_delete(char *name, struct musb *data)
545 {
546 }
547 #endif
548
549 #endif  /* __MUSB_MUSBDEFS_H__ */