]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/musbdefs.h
MUSB: More TUSB OTG support
[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/device.h>
43 #include <linux/usb_ch9.h>
44 #include <linux/usb_gadget.h>
45 #include <linux/usb.h>
46 #include <linux/usb_otg.h>
47 #include <linux/usb/musb.h>
48
49 struct musb;
50 struct musb_hw_ep;
51 struct musb_ep;
52
53
54 #include "debug.h"
55 #include "dma.h"
56
57 #ifdef CONFIG_USB_MUSB_SOC
58 /*
59  * Get core configuration from a header converted (by cfg_conv)
60  * from the Verilog config file generated by the core config utility
61  *
62  * For now we assume that header is provided along with other
63  * arch-specific files.  Discrete chips will need a build tweak.
64  * So will using AHB IDs from silicon that provides them.
65  */
66 #include <asm/arch/hdrc_cnf.h>
67 #endif
68
69 #include "plat_arc.h"
70 #include "musbhdrc.h"
71
72 #include "musb_gadget.h"
73 #include "../core/hcd.h"
74 #include "musb_host.h"
75 #include "otg.h"
76
77
78 /* REVISIT tune this */
79 #define MIN_DMA_REQUEST         1       /* use PIO below this xfer size */
80
81
82 #ifdef CONFIG_USB_MUSB_OTG
83
84 #define is_peripheral_enabled(musb)     ((musb)->board_mode != MUSB_HOST)
85 #define is_host_enabled(musb)           ((musb)->board_mode != MUSB_PERIPHERAL)
86 #define is_otg_enabled(musb)            ((musb)->board_mode == MUSB_OTG)
87
88 /* NOTE:  otg and peripheral-only state machines start at B_IDLE.
89  * OTG or host-only go to A_IDLE when ID is sensed.
90  */
91 #define is_peripheral_active(m)         (!(m)->bIsHost)
92 #define is_host_active(m)               ((m)->bIsHost)
93
94 #else
95 #define is_peripheral_enabled(musb)     is_peripheral_capable()
96 #define is_host_enabled(musb)           is_host_capable()
97 #define is_otg_enabled(musb)            0
98
99 #define is_peripheral_active(musb)      is_peripheral_capable()
100 #define is_host_active(musb)            is_host_capable()
101 #endif
102
103 #if defined(CONFIG_USB_MUSB_OTG) || defined(CONFIG_USB_MUSB_PERIPHERAL)
104 /* for some reason, the "select USB_GADGET_MUSB_HDRC" doesn't always
105  * override that choice selection (often USB_GADGET_DUMMY_HCD).
106  */
107 #ifndef CONFIG_USB_GADGET_MUSB_HDRC
108 #error bogus Kconfig output ... select CONFIG_USB_GADGET_MUSB_HDRC
109 #endif
110 #endif  /* need MUSB gadget selection */
111
112
113 #ifdef CONFIG_PROC_FS
114 #include <linux/fs.h>
115 #define MUSB_CONFIG_PROC_FS
116 #endif
117
118 /****************************** PERIPHERAL ROLE *****************************/
119
120 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
121
122 #define is_peripheral_capable() (1)
123
124 extern irqreturn_t musb_g_ep0_irq(struct musb *);
125 extern void musb_g_tx(struct musb *, u8);
126 extern void musb_g_rx(struct musb *, u8);
127 extern void musb_g_reset(struct musb *);
128 extern void musb_g_suspend(struct musb *);
129 extern void musb_g_resume(struct musb *);
130 extern void musb_g_disconnect(struct musb *);
131
132 #else
133
134 #define is_peripheral_capable() (0)
135
136 static inline irqreturn_t musb_g_ep0_irq(struct musb *m) { return IRQ_NONE; }
137 static inline void musb_g_reset(struct musb *m) {}
138 static inline void musb_g_suspend(struct musb *m) {}
139 static inline void musb_g_resume(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 TRUE
168 #define TRUE 1
169 #endif
170 #ifndef FALSE
171 #define FALSE 0
172 #endif
173
174 #ifndef MUSB_C_NUM_EPS
175 #define MUSB_C_NUM_EPS ((u8)16)
176 #endif
177
178 #ifndef MUSB_MAX_END0_PACKET
179 #define MUSB_MAX_END0_PACKET ((u16)MGC_END0_FIFOSIZE)
180 #endif
181
182 /* host side ep0 states */
183 #define MGC_END0_START  0x0
184 #define MGC_END0_OUT    0x2
185 #define MGC_END0_IN     0x4
186 #define MGC_END0_STATUS 0x8
187
188 /* peripheral side ep0 states */
189 enum musb_g_ep0_state {
190         MGC_END0_STAGE_SETUP,           /* idle, waiting for setup */
191         MGC_END0_STAGE_TX,              /* IN data */
192         MGC_END0_STAGE_RX,              /* OUT data */
193         MGC_END0_STAGE_STATUSIN,        /* (after OUT data) */
194         MGC_END0_STAGE_STATUSOUT,       /* (after IN data) */
195         MGC_END0_STAGE_ACKWAIT,         /* after zlp, before statusin */
196 } __attribute__ ((packed));
197
198 /* OTG protocol constants */
199 #define OTG_TIME_A_WAIT_VRISE   100             /* msec (max) */
200 #define OTG_TIME_A_WAIT_BCON    0               /* 0=infinite; min 1000 msec */
201 #define OTG_TIME_A_IDLE_BDIS    200             /* msec (min) */
202
203 /*************************** REGISTER ACCESS ********************************/
204
205 /* Endpoint registers (other than dynfifo setup) can be accessed either
206  * directly with the "flat" model, or after setting up an index register.
207  */
208
209 #if defined(CONFIG_ARCH_DAVINCI) || defined(CONFIG_ARCH_OMAP243X)
210 /* REVISIT "flat" takes about 1% more object code space and can't be very
211  * noticeable for speed differences.  But for now indexed access seems to
212  * misbehave (on DaVinci) for at least peripheral IN ...
213  */
214 #define MUSB_FLAT_REG
215 #endif
216
217 /* TUSB mapping: "flat" plus ep0 special cases */
218 #if     defined(CONFIG_USB_TUSB6010)
219 #define MGC_SelectEnd(_pBase, _bEnd) \
220         musb_writeb((_pBase), MGC_O_HDRC_INDEX, (_bEnd))
221 #define MGC_END_OFFSET                  MGC_TUSB_OFFSET
222
223 /* "flat" mapping: each endpoint has its own i/o address */
224 #elif   defined(MUSB_FLAT_REG)
225 #define MGC_SelectEnd(_pBase, _bEnd)    (((void)(_pBase)),((void)(_bEnd)))
226 #define MGC_END_OFFSET                  MGC_FLAT_OFFSET
227
228 /* "indexed" mapping: INDEX register controls register bank select */
229 #else
230 #define MGC_SelectEnd(_pBase, _bEnd) \
231         musb_writeb((_pBase), MGC_O_HDRC_INDEX, (_bEnd))
232 #define MGC_END_OFFSET                  MGC_INDEXED_OFFSET
233 #endif
234
235 /* FIXME: replace with musb_readcsr(hw_ep *, REGNAME), etc
236  * using hw_ep->regs, for all access except writing INDEX
237  */
238 #ifdef  MUSB_FLAT_REG
239 #define MGC_ReadCsr8(_pBase, _bOffset, _bEnd) \
240         musb_readb((_pBase), MGC_END_OFFSET((_bEnd), (_bOffset)))
241 #define MGC_ReadCsr16(_pBase, _bOffset, _bEnd) \
242         musb_readw((_pBase), MGC_END_OFFSET((_bEnd), (_bOffset)))
243 #define MGC_WriteCsr8(_pBase, _bOffset, _bEnd, _bData) \
244         musb_writeb((_pBase), MGC_END_OFFSET((_bEnd), (_bOffset)), (_bData))
245 #define MGC_WriteCsr16(_pBase, _bOffset, _bEnd, _bData) \
246         musb_writew((_pBase), MGC_END_OFFSET((_bEnd), (_bOffset)), (_bData))
247 #else
248 #define MGC_ReadCsr8(_pBase, _bOffset, _bEnd) \
249             musb_readb(_pBase, (_bOffset + 0x10))
250 #define MGC_ReadCsr16(_pBase, _bOffset, _bEnd) \
251         musb_readw(_pBase, (_bOffset + 0x10))
252 #define MGC_WriteCsr8(_pBase, _bOffset, _bEnd, _bData) \
253         musb_writeb(_pBase, (_bOffset + 0x10), _bData)
254 #define MGC_WriteCsr16(_pBase, _bOffset, _bEnd, _bData) \
255         musb_writew(_pBase, (_bOffset + 0x10), _bData)
256 #endif
257
258 /****************************** FUNCTIONS ********************************/
259
260 #define MUSB_HST_MODE(_pthis)\
261         { (_pthis)->bIsHost=TRUE; (_pthis)->bIsDevice=FALSE; }
262 #define MUSB_DEV_MODE(_pthis) \
263         { (_pthis)->bIsHost=FALSE; (_pthis)->bIsDevice=TRUE; }
264 #define MUSB_OTG_MODE(_pthis) \
265         { (_pthis)->bIsHost=FALSE; (_pthis)->bIsDevice=FALSE; }
266
267 #define MUSB_IS_HST(_x) ((_x)->bIsHost && !(_x)->bIsDevice)
268 #define MUSB_IS_DEV(_x) (!(_x)->bIsHost && (_x)->bIsDevice)
269 #define MUSB_IS_OTG(_x) (!(_x)->bIsHost && !(_x)->bIsDevice)
270
271 #define test_devctl_hst_mode(_x) \
272         (musb_readb((_x)->pRegs, MGC_O_HDRC_DEVCTL)&MGC_M_DEVCTL_HM)
273
274 /* REVISIT OTG isn't a third non-error mode... */
275 #define MUSB_MODE(_x) ( MUSB_IS_HST(_x)?"HOST" \
276                 :(MUSB_IS_DEV(_x)?"PERIPHERAL" \
277                 :(MUSB_IS_OTG(_x)?"UNCONNECTED" \
278                 :"ERROR")) )
279
280 /************************** Ep Configuration ********************************/
281
282 /** The End point descriptor */
283 struct MUSB_EpFifoDescriptor {
284         u8 bType;               /* 0 for autoconfig, CNTR, ISOC, BULK, INTR */
285         u8 bDir;                /* 0 for autoconfig, INOUT, IN, OUT */
286         int wSize;              /* 0 for autoconfig, or the size */
287 };
288
289 #define MUSB_EPD_AUTOCONFIG     0
290
291 #define MUSB_EPD_T_CNTRL        1
292 #define MUSB_EPD_T_ISOC         2
293 #define MUSB_EPD_T_BULK         3
294 #define MUSB_EPD_T_INTR         4
295
296 #define MUSB_EPD_D_INOUT        0
297 #define MUSB_EPD_D_TX           1
298 #define MUSB_EPD_D_RX           2
299
300 /******************************** TYPES *************************************/
301
302 /*
303  * struct musb_hw_ep - endpoint hardware (bidirectional)
304  *
305  * Ordered slightly for better cacheline locality.
306  */
307 struct musb_hw_ep {
308         struct musb             *musb;
309         void __iomem            *fifo;
310         void __iomem            *regs;
311
312 #ifdef CONFIG_USB_TUSB6010
313         void __iomem            *conf;
314 #endif
315
316         /* index in musb->aLocalEnd[]  */
317         u8                      bLocalEnd;
318
319         /* hardware configuration, possibly dynamic */
320         u8                      bIsSharedFifo;
321         u8                      tx_double_buffered;
322         u8                      rx_double_buffered;
323         u16                     wMaxPacketSizeTx;
324         u16                     wMaxPacketSizeRx;
325
326         struct dma_channel      *tx_channel;
327         struct dma_channel      *rx_channel;
328
329 #ifdef CONFIG_USB_TUSB6010
330         /* TUSB has "asynchronous" and "synchronous" dma modes */
331         dma_addr_t              fifo_async;
332         dma_addr_t              fifo_sync;
333 #endif
334
335 #ifdef CONFIG_USB_MUSB_HDRC_HCD
336         void __iomem            *target_regs;
337
338         /* currently scheduled peripheral endpoint */
339         struct musb_qh          *in_qh;
340         struct musb_qh          *out_qh;
341
342         u8                      rx_reinit;
343         u8                      tx_reinit;
344 #endif
345
346 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
347         /* peripheral side */
348         struct musb_ep          ep_in;                  /* TX */
349         struct musb_ep          ep_out;                 /* RX */
350 #endif
351 };
352
353 static inline struct usb_request *next_in_request(struct musb_hw_ep *hw_ep)
354 {
355 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
356         return next_request(&hw_ep->ep_in);
357 #else
358         return NULL;
359 #endif
360 }
361
362 static inline struct usb_request *next_out_request(struct musb_hw_ep *hw_ep)
363 {
364 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
365         return next_request(&hw_ep->ep_out);
366 #else
367         return NULL;
368 #endif
369 }
370
371 /*
372  * struct musb - Driver instance data.
373  */
374 struct musb {
375         spinlock_t              Lock;
376         struct clk              *clock;
377         irqreturn_t             (*isr)(int, void *, struct pt_regs *);
378         struct work_struct      irq_work;
379
380 #ifdef CONFIG_USB_MUSB_HDRC_HCD
381
382         u32                     port1_status;
383         unsigned long           rh_timer;
384
385         u8 bEnd0Stage;          /* end0 stage while in host */
386
387         /* bulk traffic normally dedicates endpoint hardware, and each
388          * direction has its own ring of host side endpoints.
389          * we try to progress the transfer at the head of each endpoint's
390          * queue until it completes or NAKs too much; then we try the next
391          * endpoint.
392          */
393         struct musb_hw_ep       *bulk_ep;
394
395         struct list_head        control;        /* of musb_qh */
396         struct list_head        in_bulk;        /* of musb_qh */
397         struct list_head        out_bulk;       /* of musb_qh */
398         struct musb_qh          *periodic[32];  /* tree of interrupt+iso */
399 #endif
400
401         /* called with IRQs blocked; ON/nonzero implies starting a session,
402          * and waiting at least a_wait_vrise_tmout.
403          */
404         void                    (*board_set_vbus)(struct musb *, int is_on);
405
406         struct dma_controller   *pDmaController;
407
408         struct device           *controller;
409         void __iomem            *ctrl_base;
410         void __iomem            *pRegs;
411
412 #ifdef CONFIG_USB_TUSB6010
413         dma_addr_t              async;
414         dma_addr_t              sync;
415 #endif
416
417         /* passed down from chip/board specific irq handlers */
418         u8                      int_usb;
419         u16                     int_rx;
420         u16                     int_tx;
421         struct pt_regs          *int_regs;
422
423         struct otg_transceiver  xceiv;
424
425         int nIrq;
426
427         struct musb_hw_ep        aLocalEnd[MUSB_C_NUM_EPS];
428 #define control_ep              aLocalEnd
429
430         u16                     vbuserr_retry;
431         u16 wEndMask;
432         u8 bEndCount;
433
434         u8 board_mode;          /* enum musb_mode */
435         int                     (*board_set_power)(int state);
436
437         u8                      min_power;      /* vbus for periph, in mA/2 */
438
439         /* active means connected and not suspended */
440         unsigned is_active:1;
441
442         unsigned bIsMultipoint:1;
443         unsigned bIsDevice:1;
444         unsigned bIsHost:1;
445         unsigned bIgnoreDisconnect:1;   /* during bus resets */
446
447 #ifdef C_MP_TX
448         unsigned bBulkSplit:1;
449 #define can_bulk_split(musb,type) \
450                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bBulkSplit)
451 #else
452 #define can_bulk_split(musb,type)       0
453 #endif
454
455 #ifdef C_MP_RX
456         unsigned bBulkCombine:1;
457         /* REVISIT allegedly doesn't work reliably */
458 #if 0
459 #define can_bulk_combine(musb,type) \
460                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bBulkCombine)
461 #else
462 #define can_bulk_combine(musb,type)     0
463 #endif
464 #else
465 #define can_bulk_combine(musb,type)     0
466 #endif
467
468 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
469         unsigned bIsSelfPowered:1;
470         unsigned bMayWakeup:1;
471         unsigned bSetAddress:1;
472         unsigned bTestMode:1;
473         unsigned softconnect:1;
474
475         enum musb_g_ep0_state   ep0_state;
476         u8                      bAddress;
477         u8                      bTestModeValue;
478         u16                     ackpend;                /* ep0 */
479         struct usb_gadget       g;                      /* the gadget */
480         struct usb_gadget_driver *pGadgetDriver;        /* its driver */
481 #endif
482
483 #ifdef CONFIG_USB_MUSB_OTG
484         struct otg_machine      OtgMachine;
485         u8 bDelayPortPowerOff;
486 #endif
487
488 #ifdef MUSB_CONFIG_PROC_FS
489         struct proc_dir_entry *pProcEntry;
490 #endif
491 };
492
493 static inline void musb_set_vbus(struct musb *musb, int is_on)
494 {
495         musb->board_set_vbus(musb, is_on);
496 }
497
498 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
499 static inline struct musb *gadget_to_musb(struct usb_gadget *g)
500 {
501         return container_of(g, struct musb, g);
502 }
503 #endif
504
505
506 /***************************** Glue it together *****************************/
507
508 extern const char musb_driver_name[];
509
510 extern void musb_start(struct musb *pThis);
511 extern void musb_stop(struct musb *pThis);
512
513 extern void musb_write_fifo(struct musb_hw_ep *ep,
514                              u16 wCount, const u8 * pSource);
515 extern void musb_read_fifo(struct musb_hw_ep *ep,
516                                u16 wCount, u8 * pDest);
517
518 extern void musb_load_testpacket(struct musb *);
519
520 extern irqreturn_t musb_interrupt(struct musb *);
521
522 extern void musb_platform_enable(struct musb *musb);
523 extern void musb_platform_disable(struct musb *musb);
524
525 #ifdef CONFIG_USB_TUSB6010
526 extern void musb_platform_try_idle(struct musb *musb);
527 extern int musb_platform_get_vbus_status(struct musb *musb);
528 #else
529 #define musb_platform_try_idle(x)               do {} while (0)
530 #define musb_platform_get_vbus_status(x)        0
531 #endif
532
533 extern int __devinit musb_platform_init(struct musb *musb);
534 extern int musb_platform_exit(struct musb *musb);
535
536 /*-------------------------- ProcFS definitions ---------------------*/
537
538 struct proc_dir_entry;
539
540 #if (MUSB_DEBUG > 0) && defined(MUSB_CONFIG_PROC_FS)
541 extern struct proc_dir_entry *musb_debug_create(char *name,
542                                                     struct musb *data);
543 extern void musb_debug_delete(char *name, struct musb *data);
544
545 #else
546 static inline struct proc_dir_entry *musb_debug_create(char *name,
547                                                            struct musb *data)
548 {
549         return NULL;
550 }
551 static inline void musb_debug_delete(char *name, struct musb *data)
552 {
553 }
554 #endif
555
556 #endif  /* __MUSB_MUSBDEFS_H__ */