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