2 * MUSB OTG driver defines
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
8 * SPDX-License-Identifier: GPL-2.0
11 #ifndef __MUSB_CORE_H__
12 #define __MUSB_CORE_H__
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/interrupt.h>
18 #include <linux/errno.h>
19 #include <linux/timer.h>
20 #include <linux/device.h>
21 #include <linux/usb.h>
22 #include <linux/usb/otg.h>
24 #include <asm/errno.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
28 #include <linux/usb/musb.h>
34 /* Helper defines for struct musb->hwvers */
35 #define MUSB_HWVERS_MAJOR(x) ((x >> 10) & 0x1f)
36 #define MUSB_HWVERS_MINOR(x) (x & 0x3ff)
37 #define MUSB_HWVERS_RC 0x8000
38 #define MUSB_HWVERS_1300 0x52C
39 #define MUSB_HWVERS_1400 0x590
40 #define MUSB_HWVERS_1800 0x720
41 #define MUSB_HWVERS_1900 0x784
42 #define MUSB_HWVERS_2000 0x800
44 #include "musb_debug.h"
48 #include "musb_regs.h"
50 #include "musb_gadget.h"
52 #include <linux/usb/hcd.h>
54 #include "musb_host.h"
56 #define is_peripheral_enabled(musb) ((musb)->board_mode != MUSB_HOST)
57 #define is_host_enabled(musb) ((musb)->board_mode != MUSB_PERIPHERAL)
58 #define is_otg_enabled(musb) ((musb)->board_mode == MUSB_OTG)
60 /* NOTE: otg and peripheral-only state machines start at B_IDLE.
61 * OTG or host-only go to A_IDLE when ID is sensed.
63 #define is_peripheral_active(m) (!(m)->is_host)
64 #define is_host_active(m) ((m)->is_host)
68 #define MUSB_CONFIG_PROC_FS
71 /****************************** PERIPHERAL ROLE *****************************/
74 #define is_peripheral_capable() (1)
76 #ifdef CONFIG_USB_MUSB_GADGET
77 #define is_peripheral_capable() (1)
79 #define is_peripheral_capable() (0)
83 extern irqreturn_t
musb_g_ep0_irq(struct musb
*);
84 extern void musb_g_tx(struct musb
*, u8
);
85 extern void musb_g_rx(struct musb
*, u8
);
86 extern void musb_g_reset(struct musb
*);
87 extern void musb_g_suspend(struct musb
*);
88 extern void musb_g_resume(struct musb
*);
89 extern void musb_g_wakeup(struct musb
*);
90 extern void musb_g_disconnect(struct musb
*);
92 /****************************** HOST ROLE ***********************************/
95 #define is_host_capable() (1)
97 #ifdef CONFIG_USB_MUSB_HOST
98 #define is_host_capable() (1)
100 #define is_host_capable() (0)
104 extern irqreturn_t
musb_h_ep0_irq(struct musb
*);
105 extern void musb_host_tx(struct musb
*, u8
);
106 extern void musb_host_rx(struct musb
*, u8
);
108 /****************************** CONSTANTS ********************************/
110 #ifndef MUSB_C_NUM_EPS
111 #define MUSB_C_NUM_EPS ((u8)16)
114 #ifndef MUSB_MAX_END0_PACKET
115 #define MUSB_MAX_END0_PACKET ((u16)MUSB_EP0_FIFOSIZE)
118 /* host side ep0 states */
119 enum musb_h_ep0_state
{
121 MUSB_EP0_START
, /* expect ack of setup */
122 MUSB_EP0_IN
, /* expect IN DATA */
123 MUSB_EP0_OUT
, /* expect ack of OUT DATA */
124 MUSB_EP0_STATUS
, /* expect ack of STATUS */
125 } __attribute__ ((packed
));
127 /* peripheral side ep0 states */
128 enum musb_g_ep0_state
{
129 MUSB_EP0_STAGE_IDLE
, /* idle, waiting for SETUP */
130 MUSB_EP0_STAGE_SETUP
, /* received SETUP */
131 MUSB_EP0_STAGE_TX
, /* IN data */
132 MUSB_EP0_STAGE_RX
, /* OUT data */
133 MUSB_EP0_STAGE_STATUSIN
, /* (after OUT data) */
134 MUSB_EP0_STAGE_STATUSOUT
, /* (after IN data) */
135 MUSB_EP0_STAGE_ACKWAIT
, /* after zlp, before statusin */
136 } __attribute__ ((packed
));
139 * OTG protocol constants. See USB OTG 1.3 spec,
140 * sections 5.5 "Device Timings" and 6.6.5 "Timers".
142 #define OTG_TIME_A_WAIT_VRISE 100 /* msec (max) */
143 #define OTG_TIME_A_WAIT_BCON 1100 /* min 1 second */
144 #define OTG_TIME_A_AIDL_BDIS 200 /* min 200 msec */
145 #define OTG_TIME_B_ASE0_BRST 100 /* min 3.125 ms */
148 /*************************** REGISTER ACCESS ********************************/
150 /* Endpoint registers (other than dynfifo setup) can be accessed either
151 * directly with the "flat" model, or after setting up an index register.
154 #if defined(CONFIG_ARCH_DAVINCI) || defined(CONFIG_SOC_OMAP2430) \
155 || defined(CONFIG_SOC_OMAP3430) || defined(CONFIG_BLACKFIN) \
156 || defined(CONFIG_ARCH_OMAP4)
157 /* REVISIT indexed access seemed to
158 * misbehave (on DaVinci) for at least peripheral IN ...
160 #define MUSB_FLAT_REG
163 /* TUSB mapping: "flat" plus ep0 special cases */
164 #if defined(CONFIG_USB_MUSB_TUSB6010) || \
165 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
166 #define musb_ep_select(_mbase, _epnum) \
167 musb_writeb((_mbase), MUSB_INDEX, (_epnum))
168 #define MUSB_EP_OFFSET MUSB_TUSB_OFFSET
170 /* "flat" mapping: each endpoint has its own i/o address */
171 #elif defined(MUSB_FLAT_REG)
172 #define musb_ep_select(_mbase, _epnum) (((void)(_mbase)), ((void)(_epnum)))
173 #define MUSB_EP_OFFSET MUSB_FLAT_OFFSET
175 /* "indexed" mapping: INDEX register controls register bank select */
177 #define musb_ep_select(_mbase, _epnum) \
178 musb_writeb((_mbase), MUSB_INDEX, (_epnum))
179 #define MUSB_EP_OFFSET MUSB_INDEXED_OFFSET
182 /****************************** FUNCTIONS ********************************/
184 #define MUSB_HST_MODE(_musb)\
185 { (_musb)->is_host = true; }
186 #define MUSB_DEV_MODE(_musb) \
187 { (_musb)->is_host = false; }
189 #define test_devctl_hst_mode(_x) \
190 (musb_readb((_x)->mregs, MUSB_DEVCTL)&MUSB_DEVCTL_HM)
192 #define MUSB_MODE(musb) ((musb)->is_host ? "Host" : "Peripheral")
194 /******************************** TYPES *************************************/
197 * struct musb_platform_ops - Operations passed to musb_core by HW glue layer
198 * @init: turns on clocks, sets up platform-specific registers, etc
199 * @exit: undoes @init
200 * @set_mode: forcefully changes operating mode
201 * @try_ilde: tries to idle the IP
202 * @vbus_status: returns vbus status if possible
203 * @set_vbus: forces vbus status
204 * @adjust_channel_params: pre check for standard dma channel_program func
206 struct musb_platform_ops
{
207 int (*init
)(struct musb
*musb
);
208 int (*exit
)(struct musb
*musb
);
211 void (*enable
)(struct musb
*musb
);
213 int (*enable
)(struct musb
*musb
);
215 void (*disable
)(struct musb
*musb
);
217 int (*set_mode
)(struct musb
*musb
, u8 mode
);
218 void (*try_idle
)(struct musb
*musb
, unsigned long timeout
);
220 int (*vbus_status
)(struct musb
*musb
);
221 void (*set_vbus
)(struct musb
*musb
, int on
);
223 int (*adjust_channel_params
)(struct dma_channel
*channel
,
224 u16 packet_sz
, u8
*mode
,
225 dma_addr_t
*dma_addr
, u32
*len
);
229 * struct musb_hw_ep - endpoint hardware (bidirectional)
231 * Ordered slightly for better cacheline locality.
238 #if defined(CONFIG_USB_MUSB_TUSB6010) || \
239 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
243 /* index in musb->endpoints[] */
246 /* hardware configuration, possibly dynamic */
248 bool tx_double_buffered
;
249 bool rx_double_buffered
;
250 u16 max_packet_sz_tx
;
251 u16 max_packet_sz_rx
;
253 struct dma_channel
*tx_channel
;
254 struct dma_channel
*rx_channel
;
256 #if defined(CONFIG_USB_MUSB_TUSB6010) || \
257 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
258 /* TUSB has "asynchronous" and "synchronous" dma modes */
259 dma_addr_t fifo_async
;
260 dma_addr_t fifo_sync
;
261 void __iomem
*fifo_sync_va
;
264 void __iomem
*target_regs
;
266 /* currently scheduled peripheral endpoint */
267 struct musb_qh
*in_qh
;
268 struct musb_qh
*out_qh
;
273 /* peripheral side */
274 struct musb_ep ep_in
; /* TX */
275 struct musb_ep ep_out
; /* RX */
278 static inline struct musb_request
*next_in_request(struct musb_hw_ep
*hw_ep
)
280 return next_request(&hw_ep
->ep_in
);
283 static inline struct musb_request
*next_out_request(struct musb_hw_ep
*hw_ep
)
285 return next_request(&hw_ep
->ep_out
);
288 struct musb_csr_regs
{
290 u16 txmaxp
, txcsr
, rxmaxp
, rxcsr
;
291 u16 rxfifoadd
, txfifoadd
;
292 u8 txtype
, txinterval
, rxtype
, rxinterval
;
293 u8 rxfifosz
, txfifosz
;
294 u8 txfunaddr
, txhubaddr
, txhubport
;
295 u8 rxfunaddr
, rxhubaddr
, rxhubport
;
298 struct musb_context_registers
{
301 u16 intrtxe
, intrrxe
;
306 u8 devctl
, busctl
, misc
;
309 struct musb_csr_regs index_regs
[MUSB_C_NUM_EPS
];
313 * struct musb - Driver instance data.
319 const struct musb_platform_ops
*ops
;
320 struct musb_context_registers context
;
322 irqreturn_t (*isr
)(int, void *);
323 struct work_struct irq_work
;
326 /* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
327 #define MUSB_PORT_STAT_RESUME (1 << 31)
331 unsigned long rh_timer
;
333 enum musb_h_ep0_state ep0_stage
;
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
341 struct musb_hw_ep
*bulk_ep
;
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 */
347 struct timer_list otg_timer
;
348 struct notifier_block nb
;
350 struct dma_controller
*dma_controller
;
352 struct device
*controller
;
353 void __iomem
*ctrl_base
;
356 #if defined(CONFIG_USB_MUSB_TUSB6010) || \
357 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
360 void __iomem
*sync_va
;
363 /* passed down from chip/board specific irq handlers */
368 struct usb_phy
*xceiv
;
373 struct musb_hw_ep endpoints
[MUSB_C_NUM_EPS
];
374 #define control_ep endpoints
376 #define VBUSERR_RETRY_COUNT 3
381 u8 board_mode
; /* enum musb_mode */
382 int (*board_set_power
)(int state
);
384 u8 min_power
; /* vbus for periph, in mA/2 */
388 int a_wait_bcon
; /* VBUS timeout in msecs */
389 unsigned long idle_timeout
; /* Next timeout in jiffies */
391 /* active means connected and not suspended */
392 unsigned is_active
:1;
394 unsigned is_multipoint
:1;
395 unsigned ignore_disconnect
:1; /* during bus resets */
397 unsigned hb_iso_rx
:1; /* high bandwidth iso rx? */
398 unsigned hb_iso_tx
:1; /* high bandwidth iso tx? */
399 unsigned dyn_fifo
:1; /* dynamic FIFO supported? */
401 unsigned bulk_split
:1;
402 #define can_bulk_split(musb,type) \
403 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_split)
405 unsigned bulk_combine
:1;
406 #define can_bulk_combine(musb,type) \
407 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_combine)
409 /* is_suspended means USB B_PERIPHERAL suspend */
410 unsigned is_suspended
:1;
412 /* may_wakeup means remote wakeup is enabled */
413 unsigned may_wakeup
:1;
415 /* is_self_powered is reported in device status and the
416 * config descriptor. is_bus_powered means B_PERIPHERAL
417 * draws some VBUS current; both can be true.
419 unsigned is_self_powered
:1;
420 unsigned is_bus_powered
:1;
422 unsigned set_address
:1;
423 unsigned test_mode
:1;
424 unsigned softconnect
:1;
428 u16 ackpend
; /* ep0 */
429 enum musb_g_ep0_state ep0_state
;
430 struct usb_gadget g
; /* the gadget */
431 struct usb_gadget_driver
*gadget_driver
; /* its driver */
434 * FIXME: Remove this flag.
436 * This is only added to allow Blackfin to work
437 * with current driver. For some unknown reason
438 * Blackfin doesn't work with double buffering
439 * and that's enabled by default.
441 * We added this flag to forcefully disable double
442 * buffering until we get it working.
444 unsigned double_buffer_not_ok
:1;
446 struct musb_hdrc_config
*config
;
448 #ifdef MUSB_CONFIG_PROC_FS
449 struct proc_dir_entry
*proc_entry
;
453 static inline struct musb
*gadget_to_musb(struct usb_gadget
*g
)
455 return container_of(g
, struct musb
, g
);
458 #ifdef CONFIG_BLACKFIN
459 static inline int musb_read_fifosize(struct musb
*musb
,
460 struct musb_hw_ep
*hw_ep
, u8 epnum
)
462 musb
->nr_endpoints
++;
463 musb
->epmask
|= (1 << epnum
);
466 hw_ep
->max_packet_sz_tx
= 128;
467 hw_ep
->max_packet_sz_rx
= 128;
469 hw_ep
->max_packet_sz_tx
= 1024;
470 hw_ep
->max_packet_sz_rx
= 1024;
472 hw_ep
->is_shared_fifo
= false;
477 static inline void musb_configure_ep0(struct musb
*musb
)
479 musb
->endpoints
[0].max_packet_sz_tx
= MUSB_EP0_FIFOSIZE
;
480 musb
->endpoints
[0].max_packet_sz_rx
= MUSB_EP0_FIFOSIZE
;
481 musb
->endpoints
[0].is_shared_fifo
= true;
486 static inline int musb_read_fifosize(struct musb
*musb
,
487 struct musb_hw_ep
*hw_ep
, u8 epnum
)
489 void *mbase
= musb
->mregs
;
492 /* read from core using indexed model */
493 reg
= musb_readb(mbase
, MUSB_EP_OFFSET(epnum
, MUSB_FIFOSIZE
));
494 /* 0's returned when no more endpoints */
498 musb
->nr_endpoints
++;
499 musb
->epmask
|= (1 << epnum
);
501 hw_ep
->max_packet_sz_tx
= 1 << (reg
& 0x0f);
503 /* shared TX/RX FIFO? */
504 if ((reg
& 0xf0) == 0xf0) {
505 hw_ep
->max_packet_sz_rx
= hw_ep
->max_packet_sz_tx
;
506 hw_ep
->is_shared_fifo
= true;
509 hw_ep
->max_packet_sz_rx
= 1 << ((reg
& 0xf0) >> 4);
510 hw_ep
->is_shared_fifo
= false;
516 static inline void musb_configure_ep0(struct musb
*musb
)
518 musb
->endpoints
[0].max_packet_sz_tx
= MUSB_EP0_FIFOSIZE
;
519 musb
->endpoints
[0].max_packet_sz_rx
= MUSB_EP0_FIFOSIZE
;
520 musb
->endpoints
[0].is_shared_fifo
= true;
522 #endif /* CONFIG_BLACKFIN */
525 /***************************** Glue it together *****************************/
527 extern const char musb_driver_name
[];
530 extern void musb_start(struct musb
*musb
);
532 extern int musb_start(struct musb
*musb
);
534 extern void musb_stop(struct musb
*musb
);
536 extern void musb_write_fifo(struct musb_hw_ep
*ep
, u16 len
, const u8
*src
);
537 extern void musb_read_fifo(struct musb_hw_ep
*ep
, u16 len
, u8
*dst
);
539 extern void musb_load_testpacket(struct musb
*);
541 extern irqreturn_t
musb_interrupt(struct musb
*);
543 extern void musb_hnp_stop(struct musb
*musb
);
545 static inline void musb_platform_set_vbus(struct musb
*musb
, int is_on
)
547 if (musb
->ops
->set_vbus
)
548 musb
->ops
->set_vbus(musb
, is_on
);
552 static inline void musb_platform_enable(struct musb
*musb
)
554 if (musb
->ops
->enable
)
555 musb
->ops
->enable(musb
);
558 static inline int musb_platform_enable(struct musb
*musb
)
560 if (!musb
->ops
->enable
)
563 return musb
->ops
->enable(musb
);
567 static inline void musb_platform_disable(struct musb
*musb
)
569 if (musb
->ops
->disable
)
570 musb
->ops
->disable(musb
);
573 static inline int musb_platform_set_mode(struct musb
*musb
, u8 mode
)
575 if (!musb
->ops
->set_mode
)
578 return musb
->ops
->set_mode(musb
, mode
);
581 static inline void musb_platform_try_idle(struct musb
*musb
,
582 unsigned long timeout
)
584 if (musb
->ops
->try_idle
)
585 musb
->ops
->try_idle(musb
, timeout
);
588 static inline int musb_platform_get_vbus_status(struct musb
*musb
)
590 if (!musb
->ops
->vbus_status
)
593 return musb
->ops
->vbus_status(musb
);
596 static inline int musb_platform_init(struct musb
*musb
)
598 if (!musb
->ops
->init
)
601 return musb
->ops
->init(musb
);
604 static inline int musb_platform_exit(struct musb
*musb
)
606 if (!musb
->ops
->exit
)
609 return musb
->ops
->exit(musb
);
614 musb_init_controller(struct musb_hdrc_platform_data
*plat
, struct device
*dev
,
617 #endif /* __MUSB_CORE_H__ */