Commit | Line | Data |
---|---|---|
60bbfc84 | 1 | /* |
28643104 | 2 | * driver for NXP USB Host devices |
60bbfc84 | 3 | * |
28643104 | 4 | * Currently supported OHCI host devices: |
53dc25ad | 5 | * - NXP LPC32xx |
60bbfc84 VW |
6 | * |
7 | * Authors: Dmitry Chigirev <source@mvista.com> | |
dd9048af | 8 | * Vitaly Wool <vitalywool@gmail.com> |
60bbfc84 VW |
9 | * |
10 | * register initialization is based on code examples provided by Philips | |
11 | * Copyright (c) 2005 Koninklijke Philips Electronics N.V. | |
12 | * | |
13 | * NOTE: This driver does not have suspend/resume functionality | |
14 | * This driver is intended for engineering development purposes only | |
15 | * | |
16 | * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under | |
17 | * the terms of the GNU General Public License version 2. This program | |
18 | * is licensed "as is" without any warranty of any kind, whether express | |
19 | * or implied. | |
20 | */ | |
21 | #include <linux/clk.h> | |
30330b8f MG |
22 | #include <linux/dma-mapping.h> |
23 | #include <linux/io.h> | |
60bbfc84 | 24 | #include <linux/i2c.h> |
30330b8f MG |
25 | #include <linux/kernel.h> |
26 | #include <linux/module.h> | |
2265efea | 27 | #include <linux/of.h> |
30330b8f | 28 | #include <linux/platform_device.h> |
73108aa9 | 29 | #include <linux/usb/isp1301.h> |
30330b8f MG |
30 | #include <linux/usb.h> |
31 | #include <linux/usb/hcd.h> | |
32 | ||
33 | #include "ohci.h" | |
34 | ||
60bbfc84 | 35 | |
a09e64fb | 36 | #include <mach/hardware.h> |
53dc25ad | 37 | #include <asm/mach-types.h> |
60bbfc84 | 38 | #include <asm/io.h> |
60bbfc84 | 39 | |
a09e64fb RK |
40 | #include <mach/platform.h> |
41 | #include <mach/irqs.h> | |
60bbfc84 | 42 | |
53dc25ad RS |
43 | #define USB_CONFIG_BASE 0x31020000 |
44 | #define PWRMAN_BASE 0x40004000 | |
45 | ||
46 | #define USB_CTRL IO_ADDRESS(PWRMAN_BASE + 0x64) | |
60bbfc84 VW |
47 | |
48 | /* USB_CTRL bit defines */ | |
49 | #define USB_SLAVE_HCLK_EN (1 << 24) | |
a6a99cf0 | 50 | #define USB_DEV_NEED_CLK_EN (1 << 22) |
60bbfc84 | 51 | #define USB_HOST_NEED_CLK_EN (1 << 21) |
a6a99cf0 | 52 | #define PAD_CONTROL_LAST_DRIVEN (1 << 19) |
60bbfc84 | 53 | |
53dc25ad | 54 | #define USB_OTG_STAT_CONTROL IO_ADDRESS(USB_CONFIG_BASE + 0x110) |
60bbfc84 VW |
55 | |
56 | /* USB_OTG_STAT_CONTROL bit defines */ | |
57 | #define TRANSPARENT_I2C_EN (1 << 7) | |
58 | #define HOST_EN (1 << 0) | |
59 | ||
53dc25ad RS |
60 | /* On LPC32xx, those are undefined */ |
61 | #ifndef start_int_set_falling_edge | |
62 | #define start_int_set_falling_edge(irq) | |
63 | #define start_int_set_rising_edge(irq) | |
64 | #define start_int_ack(irq) | |
65 | #define start_int_mask(irq) | |
66 | #define start_int_umask(irq) | |
67 | #endif | |
68 | ||
30330b8f MG |
69 | #define DRIVER_DESC "OHCI NXP driver" |
70 | ||
71 | static const char hcd_name[] = "ohci-nxp"; | |
72 | static struct hc_driver __read_mostly ohci_nxp_hc_driver; | |
73 | ||
09ce497e | 74 | static struct i2c_client *isp1301_i2c_client; |
60bbfc84 VW |
75 | |
76 | extern int usb_disabled(void); | |
60bbfc84 | 77 | |
78091dc2 APS |
78 | static struct clk *usb_pll_clk; |
79 | static struct clk *usb_dev_clk; | |
80 | static struct clk *usb_otg_clk; | |
60bbfc84 | 81 | |
53dc25ad RS |
82 | static void isp1301_configure_lpc32xx(void) |
83 | { | |
84 | /* LPC32XX only supports DAT_SE0 USB mode */ | |
85 | /* This sequence is important */ | |
86 | ||
87 | /* Disable transparent UART mode first */ | |
88 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
89 | (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), | |
90 | MC1_UART_EN); | |
91 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
92 | (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), | |
93 | ~MC1_SPEED_REG); | |
94 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
95 | ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG); | |
96 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
97 | (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR), | |
98 | ~0); | |
99 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
100 | ISP1301_I2C_MODE_CONTROL_2, | |
101 | (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL)); | |
102 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
103 | (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0); | |
104 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
105 | ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0); | |
106 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
107 | ISP1301_I2C_OTG_CONTROL_1, | |
108 | (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN)); | |
109 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
110 | (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), | |
111 | (OTG1_DM_PULLUP | OTG1_DP_PULLUP)); | |
112 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
113 | ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0); | |
114 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
115 | ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR, | |
116 | ~0); | |
117 | i2c_smbus_write_byte_data(isp1301_i2c_client, | |
118 | ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0); | |
119 | ||
120 | /* Enable usb_need_clk clock after transceiver is initialized */ | |
a6a99cf0 | 121 | __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL); |
53dc25ad RS |
122 | |
123 | printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n", | |
124 | i2c_smbus_read_word_data(isp1301_i2c_client, 0x00)); | |
125 | printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n", | |
126 | i2c_smbus_read_word_data(isp1301_i2c_client, 0x02)); | |
127 | printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n", | |
128 | i2c_smbus_read_word_data(isp1301_i2c_client, 0x14)); | |
129 | } | |
130 | ||
131 | static void isp1301_configure(void) | |
132 | { | |
d684f05f | 133 | isp1301_configure_lpc32xx(); |
60bbfc84 VW |
134 | } |
135 | ||
136 | static inline void isp1301_vbus_on(void) | |
137 | { | |
a6a36008 RS |
138 | i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1, |
139 | OTG1_VBUS_DRV); | |
60bbfc84 VW |
140 | } |
141 | ||
142 | static inline void isp1301_vbus_off(void) | |
143 | { | |
a6a36008 RS |
144 | i2c_smbus_write_byte_data(isp1301_i2c_client, |
145 | ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR, | |
146 | OTG1_VBUS_DRV); | |
60bbfc84 VW |
147 | } |
148 | ||
30330b8f | 149 | static void ohci_nxp_start_hc(void) |
60bbfc84 VW |
150 | { |
151 | unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN; | |
152 | __raw_writel(tmp, USB_OTG_STAT_CONTROL); | |
153 | isp1301_vbus_on(); | |
154 | } | |
155 | ||
30330b8f | 156 | static void ohci_nxp_stop_hc(void) |
60bbfc84 VW |
157 | { |
158 | unsigned long tmp; | |
159 | isp1301_vbus_off(); | |
160 | tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN; | |
161 | __raw_writel(tmp, USB_OTG_STAT_CONTROL); | |
162 | } | |
163 | ||
30330b8f | 164 | static int ohci_hcd_nxp_probe(struct platform_device *pdev) |
60bbfc84 VW |
165 | { |
166 | struct usb_hcd *hcd = 0; | |
28643104 | 167 | const struct hc_driver *driver = &ohci_nxp_hc_driver; |
a6a99cf0 | 168 | struct resource *res; |
60bbfc84 | 169 | int ret = 0, irq; |
73108aa9 RS |
170 | struct device_node *isp1301_node; |
171 | ||
172 | if (pdev->dev.of_node) { | |
173 | isp1301_node = of_parse_phandle(pdev->dev.of_node, | |
174 | "transceiver", 0); | |
175 | } else { | |
176 | isp1301_node = NULL; | |
177 | } | |
178 | ||
179 | isp1301_i2c_client = isp1301_get_client(isp1301_node); | |
180 | if (!isp1301_i2c_client) { | |
b3517d5d | 181 | return -EPROBE_DEFER; |
73108aa9 | 182 | } |
60bbfc84 | 183 | |
a6a99cf0 RS |
184 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
185 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; | |
186 | ||
28643104 | 187 | dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name); |
60bbfc84 | 188 | if (usb_disabled()) { |
b6c227ed | 189 | dev_err(&pdev->dev, "USB is disabled\n"); |
60bbfc84 | 190 | ret = -ENODEV; |
b3517d5d | 191 | goto fail_disable; |
60bbfc84 VW |
192 | } |
193 | ||
60bbfc84 | 194 | /* Enable AHB slave USB clock, needed for further USB clock control */ |
a6a99cf0 | 195 | __raw_writel(USB_SLAVE_HCLK_EN | PAD_CONTROL_LAST_DRIVEN, USB_CTRL); |
60bbfc84 | 196 | |
60bbfc84 | 197 | /* Enable USB PLL */ |
78091dc2 APS |
198 | usb_pll_clk = clk_get(&pdev->dev, "ck_pll5"); |
199 | if (IS_ERR(usb_pll_clk)) { | |
b6c227ed | 200 | dev_err(&pdev->dev, "failed to acquire USB PLL\n"); |
78091dc2 | 201 | ret = PTR_ERR(usb_pll_clk); |
b3517d5d | 202 | goto fail_pll; |
60bbfc84 VW |
203 | } |
204 | ||
78091dc2 | 205 | ret = clk_enable(usb_pll_clk); |
60bbfc84 | 206 | if (ret < 0) { |
b6c227ed | 207 | dev_err(&pdev->dev, "failed to start USB PLL\n"); |
b3517d5d | 208 | goto fail_pllen; |
60bbfc84 VW |
209 | } |
210 | ||
78091dc2 | 211 | ret = clk_set_rate(usb_pll_clk, 48000); |
60bbfc84 | 212 | if (ret < 0) { |
b6c227ed | 213 | dev_err(&pdev->dev, "failed to set USB clock rate\n"); |
b3517d5d | 214 | goto fail_rate; |
60bbfc84 VW |
215 | } |
216 | ||
78091dc2 APS |
217 | /* Enable USB device clock */ |
218 | usb_dev_clk = clk_get(&pdev->dev, "ck_usbd"); | |
219 | if (IS_ERR(usb_dev_clk)) { | |
220 | dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n"); | |
221 | ret = PTR_ERR(usb_dev_clk); | |
b3517d5d | 222 | goto fail_dev; |
78091dc2 APS |
223 | } |
224 | ||
225 | ret = clk_enable(usb_dev_clk); | |
226 | if (ret < 0) { | |
227 | dev_err(&pdev->dev, "failed to start USB DEV Clock\n"); | |
b3517d5d | 228 | goto fail_deven; |
78091dc2 APS |
229 | } |
230 | ||
231 | /* Enable USB otg clocks */ | |
232 | usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg"); | |
233 | if (IS_ERR(usb_otg_clk)) { | |
234 | dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n"); | |
2ec15a6f | 235 | ret = PTR_ERR(usb_otg_clk); |
b3517d5d | 236 | goto fail_otg; |
78091dc2 APS |
237 | } |
238 | ||
60bbfc84 VW |
239 | __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL); |
240 | ||
78091dc2 APS |
241 | ret = clk_enable(usb_otg_clk); |
242 | if (ret < 0) { | |
243 | dev_err(&pdev->dev, "failed to start USB DEV Clock\n"); | |
b3517d5d | 244 | goto fail_otgen; |
78091dc2 | 245 | } |
60bbfc84 | 246 | |
78091dc2 | 247 | isp1301_configure(); |
60bbfc84 | 248 | |
a6a99cf0 | 249 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
60bbfc84 | 250 | if (!hcd) { |
b6c227ed | 251 | dev_err(&pdev->dev, "Failed to allocate HC buffer\n"); |
60bbfc84 | 252 | ret = -ENOMEM; |
b3517d5d | 253 | goto fail_hcd; |
60bbfc84 VW |
254 | } |
255 | ||
a6a99cf0 | 256 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
148e1134 TR |
257 | hcd->regs = devm_ioremap_resource(&pdev->dev, res); |
258 | if (IS_ERR(hcd->regs)) { | |
259 | ret = PTR_ERR(hcd->regs); | |
b3517d5d | 260 | goto fail_resource; |
60bbfc84 | 261 | } |
a6a99cf0 RS |
262 | hcd->rsrc_start = res->start; |
263 | hcd->rsrc_len = resource_size(res); | |
60bbfc84 VW |
264 | |
265 | irq = platform_get_irq(pdev, 0); | |
266 | if (irq < 0) { | |
267 | ret = -ENXIO; | |
b3517d5d | 268 | goto fail_resource; |
60bbfc84 VW |
269 | } |
270 | ||
30330b8f | 271 | ohci_nxp_start_hc(); |
60bbfc84 | 272 | platform_set_drvdata(pdev, hcd); |
60bbfc84 VW |
273 | |
274 | dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq); | |
b5dd18d8 | 275 | ret = usb_add_hcd(hcd, irq, 0); |
60bbfc84 VW |
276 | if (ret == 0) |
277 | return ret; | |
278 | ||
30330b8f | 279 | ohci_nxp_stop_hc(); |
b3517d5d | 280 | fail_resource: |
60bbfc84 | 281 | usb_put_hcd(hcd); |
b3517d5d | 282 | fail_hcd: |
78091dc2 | 283 | clk_disable(usb_otg_clk); |
b3517d5d | 284 | fail_otgen: |
78091dc2 | 285 | clk_put(usb_otg_clk); |
b3517d5d | 286 | fail_otg: |
78091dc2 | 287 | clk_disable(usb_dev_clk); |
b3517d5d | 288 | fail_deven: |
78091dc2 | 289 | clk_put(usb_dev_clk); |
b3517d5d LC |
290 | fail_dev: |
291 | fail_rate: | |
78091dc2 | 292 | clk_disable(usb_pll_clk); |
b3517d5d | 293 | fail_pllen: |
78091dc2 | 294 | clk_put(usb_pll_clk); |
b3517d5d LC |
295 | fail_pll: |
296 | fail_disable: | |
3a407e73 | 297 | isp1301_i2c_client = NULL; |
60bbfc84 VW |
298 | return ret; |
299 | } | |
300 | ||
30330b8f | 301 | static int ohci_hcd_nxp_remove(struct platform_device *pdev) |
60bbfc84 VW |
302 | { |
303 | struct usb_hcd *hcd = platform_get_drvdata(pdev); | |
304 | ||
305 | usb_remove_hcd(hcd); | |
30330b8f | 306 | ohci_nxp_stop_hc(); |
60bbfc84 | 307 | usb_put_hcd(hcd); |
78091dc2 APS |
308 | clk_disable(usb_pll_clk); |
309 | clk_put(usb_pll_clk); | |
310 | clk_disable(usb_dev_clk); | |
311 | clk_put(usb_dev_clk); | |
8740cc7d | 312 | i2c_unregister_device(isp1301_i2c_client); |
3a407e73 | 313 | isp1301_i2c_client = NULL; |
60bbfc84 | 314 | |
60bbfc84 VW |
315 | return 0; |
316 | } | |
317 | ||
f4fce61d KS |
318 | /* work with hotplug and coldplug */ |
319 | MODULE_ALIAS("platform:usb-ohci"); | |
320 | ||
2265efea | 321 | #ifdef CONFIG_OF |
30330b8f | 322 | static const struct of_device_id ohci_hcd_nxp_match[] = { |
2265efea RS |
323 | { .compatible = "nxp,ohci-nxp" }, |
324 | {}, | |
325 | }; | |
30330b8f | 326 | MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match); |
2265efea RS |
327 | #endif |
328 | ||
30330b8f | 329 | static struct platform_driver ohci_hcd_nxp_driver = { |
60bbfc84 VW |
330 | .driver = { |
331 | .name = "usb-ohci", | |
f4fce61d | 332 | .owner = THIS_MODULE, |
30330b8f | 333 | .of_match_table = of_match_ptr(ohci_hcd_nxp_match), |
60bbfc84 | 334 | }, |
30330b8f MG |
335 | .probe = ohci_hcd_nxp_probe, |
336 | .remove = ohci_hcd_nxp_remove, | |
60bbfc84 VW |
337 | }; |
338 | ||
30330b8f MG |
339 | static int __init ohci_nxp_init(void) |
340 | { | |
341 | if (usb_disabled()) | |
342 | return -ENODEV; | |
343 | ||
344 | pr_info("%s: " DRIVER_DESC "\n", hcd_name); | |
345 | ||
346 | ohci_init_driver(&ohci_nxp_hc_driver, NULL); | |
347 | return platform_driver_register(&ohci_hcd_nxp_driver); | |
348 | } | |
349 | module_init(ohci_nxp_init); | |
350 | ||
351 | static void __exit ohci_nxp_cleanup(void) | |
352 | { | |
353 | platform_driver_unregister(&ohci_hcd_nxp_driver); | |
354 | } | |
355 | module_exit(ohci_nxp_cleanup); | |
356 | ||
357 | MODULE_DESCRIPTION(DRIVER_DESC); | |
358 | MODULE_LICENSE("GPL v2"); |