2 * USB block power/access management abstraction.
4 * Au1000+: The OHCI block control register is at the far end of the OHCI memory
5 * area. Au1550 has OHCI on different base address. No need to handle
7 * Au1200: one register to control access and clocks to O/EHCI, UDC and OTG
8 * as well as the PHY for EHCI and UDC.
12 #include <linux/clk.h>
13 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/syscore_ops.h>
19 #include <asm/mach-au1x00/au1000.h>
21 /* control register offsets */
22 #define AU1000_OHCICFG 0x7fffc
23 #define AU1550_OHCICFG 0x07ffc
24 #define AU1200_USBCFG 0x04
26 /* Au1000 USB block config bits */
27 #define USBHEN_RD (1 << 4) /* OHCI reset-done indicator */
28 #define USBHEN_CE (1 << 3) /* OHCI block clock enable */
29 #define USBHEN_E (1 << 2) /* OHCI block enable */
30 #define USBHEN_C (1 << 1) /* OHCI block coherency bit */
31 #define USBHEN_BE (1 << 0) /* OHCI Big-Endian */
33 /* Au1200 USB config bits */
34 #define USBCFG_PFEN (1 << 31) /* prefetch enable (undoc) */
35 #define USBCFG_RDCOMB (1 << 30) /* read combining (undoc) */
36 #define USBCFG_UNKNOWN (5 << 20) /* unknown, leave this way */
37 #define USBCFG_SSD (1 << 23) /* serial short detect en */
38 #define USBCFG_PPE (1 << 19) /* HS PHY PLL */
39 #define USBCFG_UCE (1 << 18) /* UDC clock enable */
40 #define USBCFG_ECE (1 << 17) /* EHCI clock enable */
41 #define USBCFG_OCE (1 << 16) /* OHCI clock enable */
42 #define USBCFG_FLA(x) (((x) & 0x3f) << 8)
43 #define USBCFG_UCAM (1 << 7) /* coherent access (undoc) */
44 #define USBCFG_GME (1 << 6) /* OTG mem access */
45 #define USBCFG_DBE (1 << 5) /* UDC busmaster enable */
46 #define USBCFG_DME (1 << 4) /* UDC mem enable */
47 #define USBCFG_EBE (1 << 3) /* EHCI busmaster enable */
48 #define USBCFG_EME (1 << 2) /* EHCI mem enable */
49 #define USBCFG_OBE (1 << 1) /* OHCI busmaster enable */
50 #define USBCFG_OME (1 << 0) /* OHCI mem enable */
51 #define USBCFG_INIT_AU1200 (USBCFG_PFEN | USBCFG_RDCOMB | USBCFG_UNKNOWN |\
52 USBCFG_SSD | USBCFG_FLA(0x20) | USBCFG_UCAM | \
53 USBCFG_GME | USBCFG_DBE | USBCFG_DME | \
54 USBCFG_EBE | USBCFG_EME | USBCFG_OBE | \
57 /* Au1300 USB config registers */
58 #define USB_DWC_CTRL1 0x00
59 #define USB_DWC_CTRL2 0x04
60 #define USB_VBUS_TIMER 0x10
61 #define USB_SBUS_CTRL 0x14
62 #define USB_MSR_ERR 0x18
63 #define USB_DWC_CTRL3 0x1C
64 #define USB_DWC_CTRL4 0x20
65 #define USB_OTG_STATUS 0x28
66 #define USB_DWC_CTRL5 0x2C
67 #define USB_DWC_CTRL6 0x30
68 #define USB_DWC_CTRL7 0x34
69 #define USB_PHY_STATUS 0xC0
70 #define USB_INT_STATUS 0xC4
71 #define USB_INT_ENABLE 0xC8
73 #define USB_DWC_CTRL1_OTGD 0x04 /* set to DISable OTG */
74 #define USB_DWC_CTRL1_HSTRS 0x02 /* set to ENable EHCI */
75 #define USB_DWC_CTRL1_DCRS 0x01 /* set to ENable UDC */
77 #define USB_DWC_CTRL2_PHY1RS 0x04 /* set to enable PHY1 */
78 #define USB_DWC_CTRL2_PHY0RS 0x02 /* set to enable PHY0 */
79 #define USB_DWC_CTRL2_PHYRS 0x01 /* set to enable PHY */
81 #define USB_DWC_CTRL3_OHCI1_CKEN (1 << 19)
82 #define USB_DWC_CTRL3_OHCI0_CKEN (1 << 18)
83 #define USB_DWC_CTRL3_EHCI0_CKEN (1 << 17)
84 #define USB_DWC_CTRL3_OTG0_CKEN (1 << 16)
86 #define USB_SBUS_CTRL_SBCA 0x04 /* coherent access */
88 #define USB_INTEN_FORCE 0x20
89 #define USB_INTEN_PHY 0x10
90 #define USB_INTEN_UDC 0x08
91 #define USB_INTEN_EHCI 0x04
92 #define USB_INTEN_OHCI1 0x02
93 #define USB_INTEN_OHCI0 0x01
95 static DEFINE_SPINLOCK(alchemy_usb_lock
);
97 static inline void __au1300_usb_phyctl(void __iomem
*base
, int enable
)
101 r
= __raw_readl(base
+ USB_DWC_CTRL2
);
102 s
= __raw_readl(base
+ USB_DWC_CTRL3
);
104 s
&= USB_DWC_CTRL3_OHCI1_CKEN
| USB_DWC_CTRL3_OHCI0_CKEN
|
105 USB_DWC_CTRL3_EHCI0_CKEN
| USB_DWC_CTRL3_OTG0_CKEN
;
108 /* simply enable all PHYs */
109 r
|= USB_DWC_CTRL2_PHY1RS
| USB_DWC_CTRL2_PHY0RS
|
111 __raw_writel(r
, base
+ USB_DWC_CTRL2
);
114 /* no USB block active, do disable all PHYs */
115 r
&= ~(USB_DWC_CTRL2_PHY1RS
| USB_DWC_CTRL2_PHY0RS
|
116 USB_DWC_CTRL2_PHYRS
);
117 __raw_writel(r
, base
+ USB_DWC_CTRL2
);
122 static inline void __au1300_ohci_control(void __iomem
*base
, int enable
, int id
)
127 __raw_writel(1, base
+ USB_DWC_CTRL7
); /* start OHCI clock */
130 r
= __raw_readl(base
+ USB_DWC_CTRL3
); /* enable OHCI block */
131 r
|= (id
== 0) ? USB_DWC_CTRL3_OHCI0_CKEN
132 : USB_DWC_CTRL3_OHCI1_CKEN
;
133 __raw_writel(r
, base
+ USB_DWC_CTRL3
);
136 __au1300_usb_phyctl(base
, enable
); /* power up the PHYs */
138 r
= __raw_readl(base
+ USB_INT_ENABLE
);
139 r
|= (id
== 0) ? USB_INTEN_OHCI0
: USB_INTEN_OHCI1
;
140 __raw_writel(r
, base
+ USB_INT_ENABLE
);
143 /* reset the OHCI start clock bit */
144 __raw_writel(0, base
+ USB_DWC_CTRL7
);
147 r
= __raw_readl(base
+ USB_INT_ENABLE
);
148 r
&= ~((id
== 0) ? USB_INTEN_OHCI0
: USB_INTEN_OHCI1
);
149 __raw_writel(r
, base
+ USB_INT_ENABLE
);
152 r
= __raw_readl(base
+ USB_DWC_CTRL3
);
153 r
&= ~((id
== 0) ? USB_DWC_CTRL3_OHCI0_CKEN
154 : USB_DWC_CTRL3_OHCI1_CKEN
);
155 __raw_writel(r
, base
+ USB_DWC_CTRL3
);
158 __au1300_usb_phyctl(base
, enable
);
162 static inline void __au1300_ehci_control(void __iomem
*base
, int enable
)
167 r
= __raw_readl(base
+ USB_DWC_CTRL3
);
168 r
|= USB_DWC_CTRL3_EHCI0_CKEN
;
169 __raw_writel(r
, base
+ USB_DWC_CTRL3
);
172 r
= __raw_readl(base
+ USB_DWC_CTRL1
);
173 r
|= USB_DWC_CTRL1_HSTRS
;
174 __raw_writel(r
, base
+ USB_DWC_CTRL1
);
177 __au1300_usb_phyctl(base
, enable
);
179 r
= __raw_readl(base
+ USB_INT_ENABLE
);
181 __raw_writel(r
, base
+ USB_INT_ENABLE
);
184 r
= __raw_readl(base
+ USB_INT_ENABLE
);
185 r
&= ~USB_INTEN_EHCI
;
186 __raw_writel(r
, base
+ USB_INT_ENABLE
);
189 r
= __raw_readl(base
+ USB_DWC_CTRL1
);
190 r
&= ~USB_DWC_CTRL1_HSTRS
;
191 __raw_writel(r
, base
+ USB_DWC_CTRL1
);
194 r
= __raw_readl(base
+ USB_DWC_CTRL3
);
195 r
&= ~USB_DWC_CTRL3_EHCI0_CKEN
;
196 __raw_writel(r
, base
+ USB_DWC_CTRL3
);
199 __au1300_usb_phyctl(base
, enable
);
203 static inline void __au1300_udc_control(void __iomem
*base
, int enable
)
208 r
= __raw_readl(base
+ USB_DWC_CTRL1
);
209 r
|= USB_DWC_CTRL1_DCRS
;
210 __raw_writel(r
, base
+ USB_DWC_CTRL1
);
213 __au1300_usb_phyctl(base
, enable
);
215 r
= __raw_readl(base
+ USB_INT_ENABLE
);
217 __raw_writel(r
, base
+ USB_INT_ENABLE
);
220 r
= __raw_readl(base
+ USB_INT_ENABLE
);
222 __raw_writel(r
, base
+ USB_INT_ENABLE
);
225 r
= __raw_readl(base
+ USB_DWC_CTRL1
);
226 r
&= ~USB_DWC_CTRL1_DCRS
;
227 __raw_writel(r
, base
+ USB_DWC_CTRL1
);
230 __au1300_usb_phyctl(base
, enable
);
234 static inline void __au1300_otg_control(void __iomem
*base
, int enable
)
238 r
= __raw_readl(base
+ USB_DWC_CTRL3
);
239 r
|= USB_DWC_CTRL3_OTG0_CKEN
;
240 __raw_writel(r
, base
+ USB_DWC_CTRL3
);
243 r
= __raw_readl(base
+ USB_DWC_CTRL1
);
244 r
&= ~USB_DWC_CTRL1_OTGD
;
245 __raw_writel(r
, base
+ USB_DWC_CTRL1
);
248 __au1300_usb_phyctl(base
, enable
);
250 r
= __raw_readl(base
+ USB_DWC_CTRL1
);
251 r
|= USB_DWC_CTRL1_OTGD
;
252 __raw_writel(r
, base
+ USB_DWC_CTRL1
);
255 r
= __raw_readl(base
+ USB_DWC_CTRL3
);
256 r
&= ~USB_DWC_CTRL3_OTG0_CKEN
;
257 __raw_writel(r
, base
+ USB_DWC_CTRL3
);
260 __au1300_usb_phyctl(base
, enable
);
264 static inline int au1300_usb_control(int block
, int enable
)
267 (void __iomem
*)KSEG1ADDR(AU1300_USB_CTL_PHYS_ADDR
);
271 case ALCHEMY_USB_OHCI0
:
272 __au1300_ohci_control(base
, enable
, 0);
274 case ALCHEMY_USB_OHCI1
:
275 __au1300_ohci_control(base
, enable
, 1);
277 case ALCHEMY_USB_EHCI0
:
278 __au1300_ehci_control(base
, enable
);
280 case ALCHEMY_USB_UDC0
:
281 __au1300_udc_control(base
, enable
);
283 case ALCHEMY_USB_OTG0
:
284 __au1300_otg_control(base
, enable
);
292 static inline void au1300_usb_init(void)
295 (void __iomem
*)KSEG1ADDR(AU1300_USB_CTL_PHYS_ADDR
);
297 /* set some sane defaults. Note: we don't fiddle with DWC_CTRL4
298 * here at all: Port 2 routing (EHCI or UDC) must be set either
299 * by boot firmware or platform init code; I can't autodetect
302 __raw_writel(0, base
+ USB_INT_ENABLE
); /* disable all USB irqs */
304 __raw_writel(0, base
+ USB_DWC_CTRL3
); /* disable all clocks */
306 __raw_writel(~0, base
+ USB_MSR_ERR
); /* clear all errors */
308 __raw_writel(~0, base
+ USB_INT_STATUS
); /* clear int status */
310 /* set coherent access bit */
311 __raw_writel(USB_SBUS_CTRL_SBCA
, base
+ USB_SBUS_CTRL
);
315 static inline void __au1200_ohci_control(void __iomem
*base
, int enable
)
317 unsigned long r
= __raw_readl(base
+ AU1200_USBCFG
);
319 __raw_writel(r
| USBCFG_OCE
, base
+ AU1200_USBCFG
);
323 __raw_writel(r
& ~USBCFG_OCE
, base
+ AU1200_USBCFG
);
329 static inline void __au1200_ehci_control(void __iomem
*base
, int enable
)
331 unsigned long r
= __raw_readl(base
+ AU1200_USBCFG
);
333 __raw_writel(r
| USBCFG_ECE
| USBCFG_PPE
, base
+ AU1200_USBCFG
);
337 if (!(r
& USBCFG_UCE
)) /* UDC also off? */
338 r
&= ~USBCFG_PPE
; /* yes: disable HS PHY PLL */
339 __raw_writel(r
& ~USBCFG_ECE
, base
+ AU1200_USBCFG
);
345 static inline void __au1200_udc_control(void __iomem
*base
, int enable
)
347 unsigned long r
= __raw_readl(base
+ AU1200_USBCFG
);
349 __raw_writel(r
| USBCFG_UCE
| USBCFG_PPE
, base
+ AU1200_USBCFG
);
352 if (!(r
& USBCFG_ECE
)) /* EHCI also off? */
353 r
&= ~USBCFG_PPE
; /* yes: disable HS PHY PLL */
354 __raw_writel(r
& ~USBCFG_UCE
, base
+ AU1200_USBCFG
);
359 static inline int au1200_usb_control(int block
, int enable
)
362 (void __iomem
*)KSEG1ADDR(AU1200_USB_CTL_PHYS_ADDR
);
365 case ALCHEMY_USB_OHCI0
:
366 __au1200_ohci_control(base
, enable
);
368 case ALCHEMY_USB_UDC0
:
369 __au1200_udc_control(base
, enable
);
371 case ALCHEMY_USB_EHCI0
:
372 __au1200_ehci_control(base
, enable
);
381 /* initialize USB block(s) to a known working state */
382 static inline void au1200_usb_init(void)
385 (void __iomem
*)KSEG1ADDR(AU1200_USB_CTL_PHYS_ADDR
);
386 __raw_writel(USBCFG_INIT_AU1200
, base
+ AU1200_USBCFG
);
391 static inline int au1000_usb_init(unsigned long rb
, int reg
)
393 void __iomem
*base
= (void __iomem
*)KSEG1ADDR(rb
+ reg
);
394 unsigned long r
= __raw_readl(base
);
397 /* 48MHz check. Don't init if no one can provide it */
398 c
= clk_get(NULL
, "usbh_clk");
401 if (clk_round_rate(c
, 48000000) != 48000000) {
405 if (clk_set_rate(c
, 48000000)) {
411 #if defined(__BIG_ENDIAN)
416 __raw_writel(r
, base
);
424 static inline void __au1xx0_ohci_control(int enable
, unsigned long rb
, int creg
)
426 void __iomem
*base
= (void __iomem
*)KSEG1ADDR(rb
);
427 unsigned long r
= __raw_readl(base
+ creg
);
428 struct clk
*c
= clk_get(NULL
, "usbh_clk");
434 if (clk_prepare_enable(c
))
437 __raw_writel(r
| USBHEN_CE
, base
+ creg
);
440 __raw_writel(r
| USBHEN_CE
| USBHEN_E
, base
+ creg
);
444 /* wait for reset complete (read reg twice: au1500 erratum) */
445 while (__raw_readl(base
+ creg
),
446 !(__raw_readl(base
+ creg
) & USBHEN_RD
))
449 __raw_writel(r
& ~(USBHEN_CE
| USBHEN_E
), base
+ creg
);
451 clk_disable_unprepare(c
);
457 static inline int au1000_usb_control(int block
, int enable
, unsigned long rb
,
463 case ALCHEMY_USB_OHCI0
:
464 __au1xx0_ohci_control(enable
, rb
, creg
);
473 * alchemy_usb_control - control Alchemy on-chip USB blocks
474 * @block: USB block to target
475 * @enable: set 1 to enable a block, 0 to disable
477 int alchemy_usb_control(int block
, int enable
)
482 spin_lock_irqsave(&alchemy_usb_lock
, flags
);
483 switch (alchemy_get_cputype()) {
484 case ALCHEMY_CPU_AU1000
:
485 case ALCHEMY_CPU_AU1500
:
486 case ALCHEMY_CPU_AU1100
:
487 ret
= au1000_usb_control(block
, enable
,
488 AU1000_USB_OHCI_PHYS_ADDR
, AU1000_OHCICFG
);
490 case ALCHEMY_CPU_AU1550
:
491 ret
= au1000_usb_control(block
, enable
,
492 AU1550_USB_OHCI_PHYS_ADDR
, AU1550_OHCICFG
);
494 case ALCHEMY_CPU_AU1200
:
495 ret
= au1200_usb_control(block
, enable
);
497 case ALCHEMY_CPU_AU1300
:
498 ret
= au1300_usb_control(block
, enable
);
503 spin_unlock_irqrestore(&alchemy_usb_lock
, flags
);
506 EXPORT_SYMBOL_GPL(alchemy_usb_control
);
509 static unsigned long alchemy_usb_pmdata
[2];
511 static void au1000_usb_pm(unsigned long br
, int creg
, int susp
)
513 void __iomem
*base
= (void __iomem
*)KSEG1ADDR(br
);
516 alchemy_usb_pmdata
[0] = __raw_readl(base
+ creg
);
517 /* There appears to be some undocumented reset register.... */
518 __raw_writel(0, base
+ 0x04);
520 __raw_writel(0, base
+ creg
);
523 __raw_writel(alchemy_usb_pmdata
[0], base
+ creg
);
528 static void au1200_usb_pm(int susp
)
531 (void __iomem
*)KSEG1ADDR(AU1200_USB_OTG_PHYS_ADDR
);
533 /* save OTG_CAP/MUX registers which indicate port routing */
534 /* FIXME: write an OTG driver to do that */
535 alchemy_usb_pmdata
[0] = __raw_readl(base
+ 0x00);
536 alchemy_usb_pmdata
[1] = __raw_readl(base
+ 0x04);
538 /* restore access to all MMIO areas */
541 /* restore OTG_CAP/MUX registers */
542 __raw_writel(alchemy_usb_pmdata
[0], base
+ 0x00);
543 __raw_writel(alchemy_usb_pmdata
[1], base
+ 0x04);
548 static void au1300_usb_pm(int susp
)
551 (void __iomem
*)KSEG1ADDR(AU1300_USB_CTL_PHYS_ADDR
);
552 /* remember Port2 routing */
554 alchemy_usb_pmdata
[0] = __raw_readl(base
+ USB_DWC_CTRL4
);
557 __raw_writel(alchemy_usb_pmdata
[0], base
+ USB_DWC_CTRL4
);
562 static void alchemy_usb_pm(int susp
)
564 switch (alchemy_get_cputype()) {
565 case ALCHEMY_CPU_AU1000
:
566 case ALCHEMY_CPU_AU1500
:
567 case ALCHEMY_CPU_AU1100
:
568 au1000_usb_pm(AU1000_USB_OHCI_PHYS_ADDR
, AU1000_OHCICFG
, susp
);
570 case ALCHEMY_CPU_AU1550
:
571 au1000_usb_pm(AU1550_USB_OHCI_PHYS_ADDR
, AU1550_OHCICFG
, susp
);
573 case ALCHEMY_CPU_AU1200
:
576 case ALCHEMY_CPU_AU1300
:
582 static int alchemy_usb_suspend(void)
588 static void alchemy_usb_resume(void)
593 static struct syscore_ops alchemy_usb_pm_ops
= {
594 .suspend
= alchemy_usb_suspend
,
595 .resume
= alchemy_usb_resume
,
598 static int __init
alchemy_usb_init(void)
602 switch (alchemy_get_cputype()) {
603 case ALCHEMY_CPU_AU1000
:
604 case ALCHEMY_CPU_AU1500
:
605 case ALCHEMY_CPU_AU1100
:
606 ret
= au1000_usb_init(AU1000_USB_OHCI_PHYS_ADDR
,
609 case ALCHEMY_CPU_AU1550
:
610 ret
= au1000_usb_init(AU1550_USB_OHCI_PHYS_ADDR
,
613 case ALCHEMY_CPU_AU1200
:
616 case ALCHEMY_CPU_AU1300
:
622 register_syscore_ops(&alchemy_usb_pm_ops
);
626 arch_initcall(alchemy_usb_init
);