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