2 * linux/arch/arm/mach-pxa/pxa3xx-ulpi.c
4 * code specific to pxa3xx aka Monahans
6 * Copyright (C) 2010 CompuLab Ltd.
8 * 2010-13-07: Igor Grinberg <grinberg@compulab.co.il>
9 * initial version: pxa310 USB Host mode support
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/err.h>
23 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <linux/usb.h>
26 #include <linux/usb/otg.h>
28 #include <mach/hardware.h>
29 #include <mach/regs-u2d.h>
30 #include <mach/pxa3xx-u2d.h>
32 struct pxa3xx_u2d_ulpi
{
34 void __iomem
*mmio_base
;
36 struct otg_transceiver
*otg
;
37 unsigned int ulpi_mode
;
40 static struct pxa3xx_u2d_ulpi
*u2d
;
42 static inline u32
u2d_readl(u32 reg
)
44 return __raw_readl(u2d
->mmio_base
+ reg
);
47 static inline void u2d_writel(u32 reg
, u32 val
)
49 __raw_writel(val
, u2d
->mmio_base
+ reg
);
52 #if defined(CONFIG_PXA310_ULPI)
53 enum u2d_ulpi_phy_mode
{
61 static inline enum u2d_ulpi_phy_mode
pxa310_ulpi_get_phymode(void)
63 return (u2d_readl(U2DOTGUSR
) >> 28) & 0xF;
66 static int pxa310_ulpi_poll(void)
71 if (!(u2d_readl(U2DOTGUCR
) & U2DOTGUCR_RUN
))
77 pr_warning("%s: ULPI access timed out!\n", __func__
);
82 static int pxa310_ulpi_read(struct otg_transceiver
*otg
, u32 reg
)
86 if (pxa310_ulpi_get_phymode() != SYNCH
) {
87 pr_warning("%s: PHY is not in SYNCH mode!\n", __func__
);
91 u2d_writel(U2DOTGUCR
, U2DOTGUCR_RUN
| U2DOTGUCR_RNW
| (reg
<< 16));
94 err
= pxa310_ulpi_poll();
98 return u2d_readl(U2DOTGUCR
) & U2DOTGUCR_RDATA
;
101 static int pxa310_ulpi_write(struct otg_transceiver
*otg
, u32 val
, u32 reg
)
103 if (pxa310_ulpi_get_phymode() != SYNCH
) {
104 pr_warning("%s: PHY is not in SYNCH mode!\n", __func__
);
108 u2d_writel(U2DOTGUCR
, U2DOTGUCR_RUN
| (reg
<< 16) | (val
<< 8));
111 return pxa310_ulpi_poll();
114 struct otg_io_access_ops pxa310_ulpi_access_ops
= {
115 .read
= pxa310_ulpi_read
,
116 .write
= pxa310_ulpi_write
,
119 static void pxa310_otg_transceiver_rtsm(void)
123 /* put PHY to sync mode */
124 u2dotgcr
= u2d_readl(U2DOTGCR
);
125 u2dotgcr
|= U2DOTGCR_RTSM
| U2DOTGCR_UTMID
;
126 u2d_writel(U2DOTGCR
, u2dotgcr
);
129 /* setup OTG sync mode */
130 u2dotgcr
= u2d_readl(U2DOTGCR
);
131 u2dotgcr
|= U2DOTGCR_ULAF
;
132 u2dotgcr
&= ~(U2DOTGCR_SMAF
| U2DOTGCR_CKAF
);
133 u2d_writel(U2DOTGCR
, u2dotgcr
);
136 static int pxa310_start_otg_host_transcvr(struct usb_bus
*host
)
140 pxa310_otg_transceiver_rtsm();
142 err
= otg_init(u2d
->otg
);
144 pr_err("OTG transceiver init failed");
148 err
= otg_set_vbus(u2d
->otg
, 1);
150 pr_err("OTG transceiver VBUS set failed");
154 err
= otg_set_host(u2d
->otg
, host
);
156 pr_err("OTG transceiver Host mode set failed");
161 static int pxa310_start_otg_hc(struct usb_bus
*host
)
166 /* disable USB device controller */
167 u2d_writel(U2DCR
, u2d_readl(U2DCR
) & ~U2DCR_UDE
);
168 u2d_writel(U2DOTGCR
, u2d_readl(U2DOTGCR
) | U2DOTGCR_UTMID
);
169 u2d_writel(U2DOTGICR
, u2d_readl(U2DOTGICR
) & ~0x37F7F);
171 err
= pxa310_start_otg_host_transcvr(host
);
175 /* set xceiver mode */
176 if (u2d
->ulpi_mode
& ULPI_IC_6PIN_SERIAL
)
177 u2d_writel(U2DP3CR
, u2d_readl(U2DP3CR
) & ~U2DP3CR_P2SS
);
178 else if (u2d
->ulpi_mode
& ULPI_IC_3PIN_SERIAL
)
179 u2d_writel(U2DP3CR
, u2d_readl(U2DP3CR
) | U2DP3CR_P2SS
);
181 /* start OTG host controller */
182 u2dotgcr
= u2d_readl(U2DOTGCR
) | U2DOTGCR_SMAF
;
183 u2d_writel(U2DOTGCR
, u2dotgcr
& ~(U2DOTGCR_ULAF
| U2DOTGCR_CKAF
));
188 static void pxa310_stop_otg_hc(void)
190 pxa310_otg_transceiver_rtsm();
192 otg_set_host(u2d
->otg
, NULL
);
193 otg_set_vbus(u2d
->otg
, 0);
194 otg_shutdown(u2d
->otg
);
197 static void pxa310_u2d_setup_otg_hc(void)
201 u2dotgcr
= u2d_readl(U2DOTGCR
);
202 u2dotgcr
|= U2DOTGCR_ULAF
| U2DOTGCR_UTMID
;
203 u2dotgcr
&= ~(U2DOTGCR_SMAF
| U2DOTGCR_CKAF
);
204 u2d_writel(U2DOTGCR
, u2dotgcr
);
206 u2d_writel(U2DOTGCR
, u2dotgcr
| U2DOTGCR_ULE
);
208 u2d_writel(U2DOTGICR
, u2d_readl(U2DOTGICR
) & ~0x37F7F);
211 static int pxa310_otg_init(struct pxa3xx_u2d_platform_data
*pdata
)
213 unsigned int ulpi_mode
= ULPI_OTG_DRVVBUS
;
216 if (pdata
->ulpi_mode
& ULPI_SER_6PIN
)
217 ulpi_mode
|= ULPI_IC_6PIN_SERIAL
;
218 else if (pdata
->ulpi_mode
& ULPI_SER_3PIN
)
219 ulpi_mode
|= ULPI_IC_3PIN_SERIAL
;
222 u2d
->ulpi_mode
= ulpi_mode
;
224 u2d
->otg
= otg_ulpi_create(&pxa310_ulpi_access_ops
, ulpi_mode
);
228 u2d
->otg
->io_priv
= u2d
->mmio_base
;
233 static void pxa310_otg_exit(void)
238 static inline void pxa310_u2d_setup_otg_hc(void) {}
239 static inline int pxa310_start_otg_hc(struct usb_bus
*host
)
243 static inline void pxa310_stop_otg_hc(void) {}
244 static inline int pxa310_otg_init(struct pxa3xx_u2d_platform_data
*pdata
)
248 static inline void pxa310_otg_exit(void) {}
249 #endif /* CONFIG_PXA310_ULPI */
251 int pxa3xx_u2d_start_hc(struct usb_bus
*host
)
255 /* In case the PXA3xx ULPI isn't used, do nothing. */
259 clk_enable(u2d
->clk
);
261 if (cpu_is_pxa310()) {
262 pxa310_u2d_setup_otg_hc();
263 err
= pxa310_start_otg_hc(host
);
268 EXPORT_SYMBOL_GPL(pxa3xx_u2d_start_hc
);
270 void pxa3xx_u2d_stop_hc(struct usb_bus
*host
)
272 /* In case the PXA3xx ULPI isn't used, do nothing. */
277 pxa310_stop_otg_hc();
279 clk_disable(u2d
->clk
);
281 EXPORT_SYMBOL_GPL(pxa3xx_u2d_stop_hc
);
283 static int pxa3xx_u2d_probe(struct platform_device
*pdev
)
285 struct pxa3xx_u2d_platform_data
*pdata
= pdev
->dev
.platform_data
;
289 u2d
= kzalloc(sizeof(struct pxa3xx_u2d_ulpi
), GFP_KERNEL
);
291 dev_err(&pdev
->dev
, "failed to allocate memory\n");
295 u2d
->clk
= clk_get(&pdev
->dev
, NULL
);
296 if (IS_ERR(u2d
->clk
)) {
297 dev_err(&pdev
->dev
, "failed to get u2d clock\n");
298 err
= PTR_ERR(u2d
->clk
);
302 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
304 dev_err(&pdev
->dev
, "no IO memory resource defined\n");
309 r
= request_mem_region(r
->start
, resource_size(r
), pdev
->name
);
311 dev_err(&pdev
->dev
, "failed to request memory resource\n");
316 u2d
->mmio_base
= ioremap(r
->start
, resource_size(r
));
317 if (!u2d
->mmio_base
) {
318 dev_err(&pdev
->dev
, "ioremap() failed\n");
324 err
= pdata
->init(&pdev
->dev
);
329 /* Only PXA310 U2D has OTG functionality */
330 if (cpu_is_pxa310()) {
331 err
= pxa310_otg_init(pdata
);
336 platform_set_drvdata(pdev
, &u2d
);
342 pdata
->exit(&pdev
->dev
);
344 iounmap(u2d
->mmio_base
);
346 release_mem_region(r
->start
, resource_size(r
));
354 static int pxa3xx_u2d_remove(struct platform_device
*pdev
)
356 struct pxa3xx_u2d_platform_data
*pdata
= pdev
->dev
.platform_data
;
359 if (cpu_is_pxa310()) {
360 pxa310_stop_otg_hc();
365 pdata
->exit(&pdev
->dev
);
367 platform_set_drvdata(pdev
, NULL
);
368 iounmap(u2d
->mmio_base
);
369 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
370 release_mem_region(r
->start
, resource_size(r
));
379 static struct platform_driver pxa3xx_u2d_ulpi_driver
= {
381 .name
= "pxa3xx-u2d",
382 .owner
= THIS_MODULE
,
384 .probe
= pxa3xx_u2d_probe
,
385 .remove
= pxa3xx_u2d_remove
,
388 static int pxa3xx_u2d_ulpi_init(void)
390 return platform_driver_register(&pxa3xx_u2d_ulpi_driver
);
392 module_init(pxa3xx_u2d_ulpi_init
);
394 static void __exit
pxa3xx_u2d_ulpi_exit(void)
396 platform_driver_unregister(&pxa3xx_u2d_ulpi_driver
);
398 module_exit(pxa3xx_u2d_ulpi_exit
);
400 MODULE_DESCRIPTION("PXA3xx U2D ULPI driver");
401 MODULE_AUTHOR("Igor Grinberg");
402 MODULE_LICENSE("GPL v2");