]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/musbdefs.h
a64e2edcc45fa20f24e9cdef0b1384dfd484c548
[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_disconnect(struct musb *);
128
129 #else
130
131 #define is_peripheral_capable() (0)
132
133 static inline irqreturn_t musb_g_ep0_irq(struct musb *m) { return IRQ_NONE; }
134 static inline void musb_g_reset(struct musb *m) {}
135 static inline void musb_g_suspend(struct musb *m) {}
136 static inline void musb_g_resume(struct musb *m) {}
137 static inline void musb_g_disconnect(struct musb *m) {}
138
139 #endif
140
141 /****************************** HOST ROLE ***********************************/
142
143 #ifdef CONFIG_USB_MUSB_HDRC_HCD
144
145 #define is_host_capable()       (1)
146
147 extern irqreturn_t musb_h_ep0_irq(struct musb *);
148 extern void musb_host_tx(struct musb *, u8);
149 extern void musb_host_rx(struct musb *, u8);
150
151 #else
152
153 #define is_host_capable()       (0)
154
155 static inline irqreturn_t musb_h_ep0_irq(struct musb *m) { return IRQ_NONE; }
156 static inline void musb_host_tx(struct musb *m, u8 e) {}
157 static inline void musb_host_rx(struct musb *m, u8 e) {}
158
159 #endif
160
161
162 /****************************** CONSTANTS ********************************/
163
164 #ifndef TRUE
165 #define TRUE 1
166 #endif
167 #ifndef FALSE
168 #define FALSE 0
169 #endif
170
171 #ifndef MUSB_C_NUM_EPS
172 #define MUSB_C_NUM_EPS ((u8)16)
173 #endif
174
175 #ifndef MUSB_MAX_END0_PACKET
176 #define MUSB_MAX_END0_PACKET ((u16)MGC_END0_FIFOSIZE)
177 #endif
178
179 /* host side ep0 states */
180 enum musb_h_ep0_state {
181         MGC_END0_IDLE,
182         MGC_END0_START,                 /* expect ack of setup */
183         MGC_END0_IN,                    /* expect IN DATA */
184         MGC_END0_OUT,                   /* expect ack of OUT DATA */
185         MGC_END0_STATUS,                /* expect ack of STATUS */
186 } __attribute__ ((packed));
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_OMAP2430)
210 /* REVISIT indexed access seemed to
211  * misbehave (on DaVinci) for at least peripheral IN ...
212  */
213 #define MUSB_FLAT_REG
214 #endif
215
216 /* TUSB mapping: "flat" plus ep0 special cases */
217 #if     defined(CONFIG_USB_TUSB6010)
218 #define MGC_SelectEnd(_pBase, _bEnd) \
219         musb_writeb((_pBase), MGC_O_HDRC_INDEX, (_bEnd))
220 #define MGC_END_OFFSET                  MGC_TUSB_OFFSET
221
222 /* "flat" mapping: each endpoint has its own i/o address */
223 #elif   defined(MUSB_FLAT_REG)
224 #define MGC_SelectEnd(_pBase, _bEnd)    (((void)(_pBase)),((void)(_bEnd)))
225 #define MGC_END_OFFSET                  MGC_FLAT_OFFSET
226
227 /* "indexed" mapping: INDEX register controls register bank select */
228 #else
229 #define MGC_SelectEnd(_pBase, _bEnd) \
230         musb_writeb((_pBase), MGC_O_HDRC_INDEX, (_bEnd))
231 #define MGC_END_OFFSET                  MGC_INDEXED_OFFSET
232 #endif
233
234 /****************************** FUNCTIONS ********************************/
235
236 #define MUSB_HST_MODE(_pthis)\
237         { (_pthis)->bIsHost=TRUE; }
238 #define MUSB_DEV_MODE(_pthis) \
239         { (_pthis)->bIsHost=FALSE; }
240
241 #define test_devctl_hst_mode(_x) \
242         (musb_readb((_x)->pRegs, MGC_O_HDRC_DEVCTL)&MGC_M_DEVCTL_HM)
243
244 #define MUSB_MODE(musb) ((musb)->bIsHost ? "Host" : "Peripheral")
245
246 /************************** Ep Configuration ********************************/
247
248 /** The End point descriptor */
249 struct MUSB_EpFifoDescriptor {
250         u8 bType;               /* 0 for autoconfig, CNTR, ISOC, BULK, INTR */
251         u8 bDir;                /* 0 for autoconfig, INOUT, IN, OUT */
252         int wSize;              /* 0 for autoconfig, or the size */
253 };
254
255 #define MUSB_EPD_AUTOCONFIG     0
256
257 #define MUSB_EPD_T_CNTRL        1
258 #define MUSB_EPD_T_ISOC         2
259 #define MUSB_EPD_T_BULK         3
260 #define MUSB_EPD_T_INTR         4
261
262 #define MUSB_EPD_D_INOUT        0
263 #define MUSB_EPD_D_TX           1
264 #define MUSB_EPD_D_RX           2
265
266 /******************************** TYPES *************************************/
267
268 /*
269  * struct musb_hw_ep - endpoint hardware (bidirectional)
270  *
271  * Ordered slightly for better cacheline locality.
272  */
273 struct musb_hw_ep {
274         struct musb             *musb;
275         void __iomem            *fifo;
276         void __iomem            *regs;
277
278 #ifdef CONFIG_USB_TUSB6010
279         void __iomem            *conf;
280 #endif
281
282         /* index in musb->aLocalEnd[]  */
283         u8                      bLocalEnd;
284
285         /* hardware configuration, possibly dynamic */
286         u8                      bIsSharedFifo;
287         u8                      tx_double_buffered;
288         u8                      rx_double_buffered;
289         u16                     wMaxPacketSizeTx;
290         u16                     wMaxPacketSizeRx;
291
292         struct dma_channel      *tx_channel;
293         struct dma_channel      *rx_channel;
294
295 #ifdef CONFIG_USB_TUSB6010
296         /* TUSB has "asynchronous" and "synchronous" dma modes */
297         dma_addr_t              fifo_async;
298         dma_addr_t              fifo_sync;
299         void __iomem            *fifo_sync_va;
300 #endif
301
302 #ifdef CONFIG_USB_MUSB_HDRC_HCD
303         void __iomem            *target_regs;
304
305         /* currently scheduled peripheral endpoint */
306         struct musb_qh          *in_qh;
307         struct musb_qh          *out_qh;
308
309         u8                      rx_reinit;
310         u8                      tx_reinit;
311 #endif
312
313 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
314         /* peripheral side */
315         struct musb_ep          ep_in;                  /* TX */
316         struct musb_ep          ep_out;                 /* RX */
317 #endif
318 };
319
320 static inline struct usb_request *next_in_request(struct musb_hw_ep *hw_ep)
321 {
322 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
323         return next_request(&hw_ep->ep_in);
324 #else
325         return NULL;
326 #endif
327 }
328
329 static inline struct usb_request *next_out_request(struct musb_hw_ep *hw_ep)
330 {
331 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
332         return next_request(&hw_ep->ep_out);
333 #else
334         return NULL;
335 #endif
336 }
337
338 /*
339  * struct musb - Driver instance data.
340  */
341 struct musb {
342         spinlock_t              Lock;
343         struct clk              *clock;
344         irqreturn_t             (*isr)(int, void *);
345         struct work_struct      irq_work;
346
347 #ifdef CONFIG_USB_MUSB_HDRC_HCD
348
349 /* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
350 #define MUSB_PORT_STAT_RESUME   (1 << 31)
351
352         u32                     port1_status;
353         unsigned long           rh_timer;
354
355         enum musb_h_ep0_state   bEnd0Stage;
356
357         /* bulk traffic normally dedicates endpoint hardware, and each
358          * direction has its own ring of host side endpoints.
359          * we try to progress the transfer at the head of each endpoint's
360          * queue until it completes or NAKs too much; then we try the next
361          * endpoint.
362          */
363         struct musb_hw_ep       *bulk_ep;
364
365         struct list_head        control;        /* of musb_qh */
366         struct list_head        in_bulk;        /* of musb_qh */
367         struct list_head        out_bulk;       /* of musb_qh */
368         struct musb_qh          *periodic[32];  /* tree of interrupt+iso */
369 #endif
370
371         /* called with IRQs blocked; ON/nonzero implies starting a session,
372          * and waiting at least a_wait_vrise_tmout.
373          */
374         void                    (*board_set_vbus)(struct musb *, int is_on);
375
376         struct dma_controller   *pDmaController;
377
378         struct device           *controller;
379         void __iomem            *ctrl_base;
380         void __iomem            *pRegs;
381
382 #ifdef CONFIG_USB_TUSB6010
383         dma_addr_t              async;
384         dma_addr_t              sync;
385         void __iomem            *sync_va;
386 #endif
387
388         /* passed down from chip/board specific irq handlers */
389         u8                      int_usb;
390         u16                     int_rx;
391         u16                     int_tx;
392
393         struct otg_transceiver  xceiv;
394
395         int nIrq;
396
397         struct musb_hw_ep        aLocalEnd[MUSB_C_NUM_EPS];
398 #define control_ep              aLocalEnd
399
400 #define VBUSERR_RETRY_COUNT     3
401         u16                     vbuserr_retry;
402         u16 wEndMask;
403         u8 bEndCount;
404
405         u8 board_mode;          /* enum musb_mode */
406         int                     (*board_set_power)(int state);
407
408         int                     (*set_clock)(struct clk *clk, int is_active);
409
410         u8                      min_power;      /* vbus for periph, in mA/2 */
411
412         /* active means connected and not suspended */
413         unsigned                is_active:1;
414
415         unsigned bIsMultipoint:1;
416         unsigned bIsHost:1;
417         unsigned bIgnoreDisconnect:1;   /* during bus resets */
418
419 #ifdef C_MP_TX
420         unsigned bBulkSplit:1;
421 #define can_bulk_split(musb,type) \
422                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bBulkSplit)
423 #else
424 #define can_bulk_split(musb,type)       0
425 #endif
426
427 #ifdef C_MP_RX
428         unsigned bBulkCombine:1;
429         /* REVISIT allegedly doesn't work reliably */
430 #if 0
431 #define can_bulk_combine(musb,type) \
432                 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bBulkCombine)
433 #else
434 #define can_bulk_combine(musb,type)     0
435 #endif
436 #else
437 #define can_bulk_combine(musb,type)     0
438 #endif
439
440 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
441         /* is_suspended means USB B_PERIPHERAL suspend */
442         unsigned                is_suspended:1;
443
444         /* may_wakeup means remote wakeup is enabled */
445         unsigned                may_wakeup:1;
446
447         /* is_self_powered is reported in device status and the
448          * config descriptor.  is_bus_powered means B_PERIPHERAL
449          * draws some VBUS current; both can be true.
450          */
451         unsigned                is_self_powered:1;
452         unsigned                is_bus_powered:1;
453
454         unsigned bSetAddress:1;
455         unsigned bTestMode:1;
456         unsigned softconnect:1;
457
458         enum musb_g_ep0_state   ep0_state;
459         u8                      bAddress;
460         u8                      bTestModeValue;
461         u16                     ackpend;                /* ep0 */
462         struct usb_gadget       g;                      /* the gadget */
463         struct usb_gadget_driver *pGadgetDriver;        /* its driver */
464 #endif
465
466 #ifdef CONFIG_USB_MUSB_OTG
467         /* FIXME this can't be OTG-specific ... ? */
468         u8 bDelayPortPowerOff;
469 #endif
470
471 #ifdef MUSB_CONFIG_PROC_FS
472         struct proc_dir_entry *pProcEntry;
473 #endif
474 };
475
476 static inline void musb_set_vbus(struct musb *musb, int is_on)
477 {
478         musb->board_set_vbus(musb, is_on);
479 }
480
481 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
482 static inline struct musb *gadget_to_musb(struct usb_gadget *g)
483 {
484         return container_of(g, struct musb, g);
485 }
486 #endif
487
488
489 /***************************** Glue it together *****************************/
490
491 extern const char musb_driver_name[];
492
493 extern void musb_start(struct musb *pThis);
494 extern void musb_stop(struct musb *pThis);
495
496 extern void musb_write_fifo(struct musb_hw_ep *ep,
497                              u16 wCount, const u8 * pSource);
498 extern void musb_read_fifo(struct musb_hw_ep *ep,
499                                u16 wCount, u8 * pDest);
500
501 extern void musb_load_testpacket(struct musb *);
502
503 extern irqreturn_t musb_interrupt(struct musb *);
504
505 extern void musb_platform_enable(struct musb *musb);
506 extern void musb_platform_disable(struct musb *musb);
507
508 #ifdef CONFIG_USB_TUSB6010
509 extern void musb_platform_try_idle(struct musb *musb);
510 extern int musb_platform_get_vbus_status(struct musb *musb);
511 extern void musb_platform_set_mode(struct musb *musb, u8 musb_mode);
512 #else
513 #define musb_platform_try_idle(x)               do {} while (0)
514 #define musb_platform_get_vbus_status(x)        0
515 #define musb_platform_set_mode(x, y)            do {} while (0)
516 #endif
517
518 extern int __init musb_platform_init(struct musb *musb);
519 extern int musb_platform_exit(struct musb *musb);
520
521 /*-------------------------- ProcFS definitions ---------------------*/
522
523 struct proc_dir_entry;
524
525 #if (MUSB_DEBUG > 0) && defined(MUSB_CONFIG_PROC_FS)
526 extern struct proc_dir_entry *musb_debug_create(char *name,
527                                                     struct musb *data);
528 extern void musb_debug_delete(char *name, struct musb *data);
529
530 #else
531 static inline struct proc_dir_entry *musb_debug_create(char *name,
532                                                            struct musb *data)
533 {
534         return NULL;
535 }
536 static inline void musb_debug_delete(char *name, struct musb *data)
537 {
538 }
539 #endif
540
541 #endif  /* __MUSB_MUSBDEFS_H__ */