1
2
3
4
5
6
7
8
9
10#ifndef __MUSB_CORE_H__
11#define __MUSB_CORE_H__
12
13#ifndef __UBOOT__
14#include <linux/slab.h>
15#include <linux/list.h>
16#include <linux/interrupt.h>
17#include <linux/errno.h>
18#include <linux/timer.h>
19#include <linux/device.h>
20#include <linux/usb.h>
21#include <linux/usb/otg.h>
22#else
23#include <linux/errno.h>
24#endif
25#include <linux/usb/ch9.h>
26#include <linux/usb/gadget.h>
27#include <linux/usb/musb.h>
28
29struct musb;
30struct musb_hw_ep;
31struct musb_ep;
32
33
34#define MUSB_HWVERS_MAJOR(x) ((x >> 10) & 0x1f)
35#define MUSB_HWVERS_MINOR(x) (x & 0x3ff)
36#define MUSB_HWVERS_RC 0x8000
37#define MUSB_HWVERS_1300 0x52C
38#define MUSB_HWVERS_1400 0x590
39#define MUSB_HWVERS_1800 0x720
40#define MUSB_HWVERS_1900 0x784
41#define MUSB_HWVERS_2000 0x800
42
43#include "musb_debug.h"
44#include "musb_dma.h"
45
46#include "musb_io.h"
47#include "musb_regs.h"
48
49#include "musb_gadget.h"
50#ifndef __UBOOT__
51#include <linux/usb/hcd.h>
52#endif
53#include "musb_host.h"
54
55#define is_peripheral_enabled(musb) ((musb)->board_mode != MUSB_HOST)
56#define is_host_enabled(musb) ((musb)->board_mode != MUSB_PERIPHERAL)
57#define is_otg_enabled(musb) ((musb)->board_mode == MUSB_OTG)
58
59
60
61
62#define is_peripheral_active(m) (!(m)->is_host)
63#define is_host_active(m) ((m)->is_host)
64
65#ifdef CONFIG_PROC_FS
66#include <linux/fs.h>
67#define MUSB_CONFIG_PROC_FS
68#endif
69
70
71
72#ifndef __UBOOT__
73#define is_peripheral_capable() (1)
74#else
75#ifdef CONFIG_USB_MUSB_GADGET
76#define is_peripheral_capable() (1)
77#else
78#define is_peripheral_capable() (0)
79#endif
80#endif
81
82extern irqreturn_t musb_g_ep0_irq(struct musb *);
83extern void musb_g_tx(struct musb *, u8);
84extern void musb_g_rx(struct musb *, u8);
85extern void musb_g_reset(struct musb *);
86extern void musb_g_suspend(struct musb *);
87extern void musb_g_resume(struct musb *);
88extern void musb_g_wakeup(struct musb *);
89extern void musb_g_disconnect(struct musb *);
90
91
92
93#ifndef __UBOOT__
94#define is_host_capable() (1)
95#else
96#ifdef CONFIG_USB_MUSB_HOST
97#define is_host_capable() (1)
98#else
99#define is_host_capable() (0)
100#endif
101#endif
102
103extern irqreturn_t musb_h_ep0_irq(struct musb *);
104extern void musb_host_tx(struct musb *, u8);
105extern void musb_host_rx(struct musb *, u8);
106
107
108
109#ifndef MUSB_C_NUM_EPS
110#define MUSB_C_NUM_EPS ((u8)16)
111#endif
112
113#ifndef MUSB_MAX_END0_PACKET
114#define MUSB_MAX_END0_PACKET ((u16)MUSB_EP0_FIFOSIZE)
115#endif
116
117
118enum musb_h_ep0_state {
119 MUSB_EP0_IDLE,
120 MUSB_EP0_START,
121 MUSB_EP0_IN,
122 MUSB_EP0_OUT,
123 MUSB_EP0_STATUS,
124} __attribute__ ((packed));
125
126
127enum musb_g_ep0_state {
128 MUSB_EP0_STAGE_IDLE,
129 MUSB_EP0_STAGE_SETUP,
130 MUSB_EP0_STAGE_TX,
131 MUSB_EP0_STAGE_RX,
132 MUSB_EP0_STAGE_STATUSIN,
133 MUSB_EP0_STAGE_STATUSOUT,
134 MUSB_EP0_STAGE_ACKWAIT,
135} __attribute__ ((packed));
136
137
138
139
140
141#define OTG_TIME_A_WAIT_VRISE 100
142#define OTG_TIME_A_WAIT_BCON 1100
143#define OTG_TIME_A_AIDL_BDIS 200
144#define OTG_TIME_B_ASE0_BRST 100
145
146
147
148
149
150
151
152
153#if defined(CONFIG_ARCH_DAVINCI) || defined(CONFIG_SOC_OMAP2430) \
154 || defined(CFG_SOC_OMAP3430) || defined(CFG_ARCH_OMAP4)
155
156
157
158#define MUSB_FLAT_REG
159#endif
160
161
162#if defined(CONFIG_USB_MUSB_TUSB6010) || \
163 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
164#define musb_ep_select(_mbase, _epnum) \
165 musb_writeb((_mbase), MUSB_INDEX, (_epnum))
166#define MUSB_EP_OFFSET MUSB_TUSB_OFFSET
167
168
169#elif defined(MUSB_FLAT_REG)
170#define musb_ep_select(_mbase, _epnum) (((void)(_mbase)), ((void)(_epnum)))
171#define MUSB_EP_OFFSET MUSB_FLAT_OFFSET
172
173
174#else
175#define musb_ep_select(_mbase, _epnum) \
176 musb_writeb((_mbase), MUSB_INDEX, (_epnum))
177#define MUSB_EP_OFFSET MUSB_INDEXED_OFFSET
178#endif
179
180
181
182#define MUSB_HST_MODE(_musb)\
183 { (_musb)->is_host = true; }
184#define MUSB_DEV_MODE(_musb) \
185 { (_musb)->is_host = false; }
186
187#define test_devctl_hst_mode(_x) \
188 (musb_readb((_x)->mregs, MUSB_DEVCTL)&MUSB_DEVCTL_HM)
189
190#define MUSB_MODE(musb) ((musb)->is_host ? "Host" : "Peripheral")
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206struct musb_platform_ops {
207 int (*init)(struct musb *musb);
208 int (*exit)(struct musb *musb);
209
210#ifndef __UBOOT__
211 void (*enable)(struct musb *musb);
212#else
213 int (*enable)(struct musb *musb);
214#endif
215 void (*disable)(struct musb *musb);
216
217 int (*set_mode)(struct musb *musb, u8 mode);
218 void (*try_idle)(struct musb *musb, unsigned long timeout);
219
220 int (*vbus_status)(struct musb *musb);
221 void (*set_vbus)(struct musb *musb, int on);
222
223 int (*adjust_channel_params)(struct dma_channel *channel,
224 u16 packet_sz, u8 *mode,
225 dma_addr_t *dma_addr, u32 *len);
226 void (*pre_root_reset_end)(struct musb *musb);
227 void (*post_root_reset_end)(struct musb *musb);
228};
229
230
231
232
233
234
235struct musb_hw_ep {
236 struct musb *musb;
237 void __iomem *fifo;
238 void __iomem *regs;
239
240#if defined(CONFIG_USB_MUSB_TUSB6010) || \
241 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
242 void __iomem *conf;
243#endif
244
245
246 u8 epnum;
247
248
249 bool is_shared_fifo;
250 bool tx_double_buffered;
251 bool rx_double_buffered;
252 u16 max_packet_sz_tx;
253 u16 max_packet_sz_rx;
254
255 struct dma_channel *tx_channel;
256 struct dma_channel *rx_channel;
257
258#if defined(CONFIG_USB_MUSB_TUSB6010) || \
259 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
260
261 dma_addr_t fifo_async;
262 dma_addr_t fifo_sync;
263 void __iomem *fifo_sync_va;
264#endif
265
266 void __iomem *target_regs;
267
268
269 struct musb_qh *in_qh;
270 struct musb_qh *out_qh;
271
272 u8 rx_reinit;
273 u8 tx_reinit;
274
275
276 struct musb_ep ep_in;
277 struct musb_ep ep_out;
278};
279
280static inline struct musb_request *next_in_request(struct musb_hw_ep *hw_ep)
281{
282 return next_request(&hw_ep->ep_in);
283}
284
285static inline struct musb_request *next_out_request(struct musb_hw_ep *hw_ep)
286{
287 return next_request(&hw_ep->ep_out);
288}
289
290struct musb_csr_regs {
291
292 u16 txmaxp, txcsr, rxmaxp, rxcsr;
293 u16 rxfifoadd, txfifoadd;
294 u8 txtype, txinterval, rxtype, rxinterval;
295 u8 rxfifosz, txfifosz;
296 u8 txfunaddr, txhubaddr, txhubport;
297 u8 rxfunaddr, rxhubaddr, rxhubport;
298};
299
300struct musb_context_registers {
301
302 u8 power;
303 u16 intrtxe, intrrxe;
304 u8 intrusbe;
305 u16 frame;
306 u8 index, testmode;
307
308 u8 devctl, busctl, misc;
309 u32 otg_interfsel;
310
311 struct musb_csr_regs index_regs[MUSB_C_NUM_EPS];
312};
313
314
315
316
317struct musb {
318
319 spinlock_t lock;
320
321 const struct musb_platform_ops *ops;
322 struct musb_context_registers context;
323
324 irqreturn_t (*isr)(int, void *);
325 struct work_struct irq_work;
326 u16 hwvers;
327
328
329#define MUSB_PORT_STAT_RESUME (1 << 31)
330
331 u32 port1_status;
332
333 unsigned long rh_timer;
334
335 enum musb_h_ep0_state ep0_stage;
336
337
338
339
340
341
342
343 struct musb_hw_ep *bulk_ep;
344
345 struct list_head control;
346 struct list_head in_bulk;
347 struct list_head out_bulk;
348
349 struct timer_list otg_timer;
350 struct notifier_block nb;
351
352 struct dma_controller *dma_controller;
353
354 struct device *controller;
355 void __iomem *ctrl_base;
356 void __iomem *mregs;
357
358#if defined(CONFIG_USB_MUSB_TUSB6010) || \
359 defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
360 dma_addr_t async;
361 dma_addr_t sync;
362 void __iomem *sync_va;
363#endif
364
365
366 u8 int_usb;
367 u16 int_rx;
368 u16 int_tx;
369
370 struct usb_phy *xceiv;
371
372 int nIrq;
373 unsigned irq_wake:1;
374
375 struct musb_hw_ep endpoints[MUSB_C_NUM_EPS];
376#define control_ep endpoints
377
378#define VBUSERR_RETRY_COUNT 3
379 u16 vbuserr_retry;
380 u16 epmask;
381 u8 nr_endpoints;
382
383 u8 board_mode;
384 int (*board_set_power)(int state);
385
386 u8 min_power;
387
388 bool is_host;
389
390 int a_wait_bcon;
391 unsigned long idle_timeout;
392
393
394 unsigned is_active:1;
395
396 unsigned is_multipoint:1;
397 unsigned ignore_disconnect:1;
398
399 unsigned hb_iso_rx:1;
400 unsigned hb_iso_tx:1;
401 unsigned dyn_fifo:1;
402
403 unsigned bulk_split:1;
404#define can_bulk_split(musb,type) \
405 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_split)
406
407 unsigned bulk_combine:1;
408#define can_bulk_combine(musb,type) \
409 (((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_combine)
410
411
412 unsigned is_suspended:1;
413
414
415 unsigned may_wakeup:1;
416
417
418
419
420
421 unsigned is_self_powered:1;
422 unsigned is_bus_powered:1;
423
424 unsigned set_address:1;
425 unsigned test_mode:1;
426 unsigned softconnect:1;
427
428 u8 address;
429 u8 test_mode_nr;
430 u16 ackpend;
431 enum musb_g_ep0_state ep0_state;
432 struct usb_gadget g;
433 struct usb_gadget_driver *gadget_driver;
434
435
436
437
438
439
440
441
442
443
444
445
446 unsigned double_buffer_not_ok:1;
447
448 struct musb_hdrc_config *config;
449
450#ifdef MUSB_CONFIG_PROC_FS
451 struct proc_dir_entry *proc_entry;
452#endif
453};
454
455static inline struct musb *gadget_to_musb(struct usb_gadget *g)
456{
457 return container_of(g, struct musb, g);
458}
459
460static inline int musb_read_fifosize(struct musb *musb,
461 struct musb_hw_ep *hw_ep, u8 epnum)
462{
463 void *mbase = musb->mregs;
464 u8 reg = 0;
465
466
467 reg = musb_readb(mbase, MUSB_EP_OFFSET(epnum, MUSB_FIFOSIZE));
468
469 if (!reg)
470 return -ENODEV;
471
472 musb->nr_endpoints++;
473 musb->epmask |= (1 << epnum);
474
475 hw_ep->max_packet_sz_tx = 1 << (reg & 0x0f);
476
477
478 if ((reg & 0xf0) == 0xf0) {
479 hw_ep->max_packet_sz_rx = hw_ep->max_packet_sz_tx;
480 hw_ep->is_shared_fifo = true;
481 return 0;
482 } else {
483 hw_ep->max_packet_sz_rx = 1 << ((reg & 0xf0) >> 4);
484 hw_ep->is_shared_fifo = false;
485 }
486
487 return 0;
488}
489
490static inline void musb_configure_ep0(struct musb *musb)
491{
492 musb->endpoints[0].max_packet_sz_tx = MUSB_EP0_FIFOSIZE;
493 musb->endpoints[0].max_packet_sz_rx = MUSB_EP0_FIFOSIZE;
494 musb->endpoints[0].is_shared_fifo = true;
495}
496
497
498
499extern const char musb_driver_name[];
500
501#ifndef __UBOOT__
502extern void musb_start(struct musb *musb);
503#else
504extern int musb_start(struct musb *musb);
505#endif
506extern void musb_stop(struct musb *musb);
507
508extern void musb_write_fifo(struct musb_hw_ep *ep, u16 len, const u8 *src);
509extern void musb_read_fifo(struct musb_hw_ep *ep, u16 len, u8 *dst);
510
511extern void musb_load_testpacket(struct musb *);
512
513extern irqreturn_t musb_interrupt(struct musb *);
514
515extern void musb_hnp_stop(struct musb *musb);
516
517static inline void musb_platform_set_vbus(struct musb *musb, int is_on)
518{
519 if (musb->ops->set_vbus)
520 musb->ops->set_vbus(musb, is_on);
521}
522
523#ifndef __UBOOT__
524static inline void musb_platform_enable(struct musb *musb)
525{
526 if (musb->ops->enable)
527 musb->ops->enable(musb);
528}
529#else
530static inline int musb_platform_enable(struct musb *musb)
531{
532 if (!musb->ops->enable)
533 return 0;
534
535 return musb->ops->enable(musb);
536}
537#endif
538
539static inline void musb_platform_disable(struct musb *musb)
540{
541 if (musb->ops->disable)
542 musb->ops->disable(musb);
543}
544
545static inline int musb_platform_set_mode(struct musb *musb, u8 mode)
546{
547 if (!musb->ops->set_mode)
548 return 0;
549
550 return musb->ops->set_mode(musb, mode);
551}
552
553static inline void musb_platform_try_idle(struct musb *musb,
554 unsigned long timeout)
555{
556 if (musb->ops->try_idle)
557 musb->ops->try_idle(musb, timeout);
558}
559
560static inline int musb_platform_get_vbus_status(struct musb *musb)
561{
562 if (!musb->ops->vbus_status)
563 return 0;
564
565 return musb->ops->vbus_status(musb);
566}
567
568static inline int musb_platform_init(struct musb *musb)
569{
570 if (!musb->ops->init)
571 return -EINVAL;
572
573 return musb->ops->init(musb);
574}
575
576static inline int musb_platform_exit(struct musb *musb)
577{
578 if (!musb->ops->exit)
579 return -EINVAL;
580
581 return musb->ops->exit(musb);
582}
583
584#ifdef __UBOOT__
585struct musb *
586musb_init_controller(struct musb_hdrc_platform_data *plat, struct device *dev,
587 void *ctrl);
588#endif
589#endif
590