1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * driver for NXP USB Host devices
4 *
5 * Currently supported OHCI host devices:
6 * - NXP LPC32xx
7 *
8 * Authors: Dmitry Chigirev <source@mvista.com>
9 * Vitaly Wool <vitalywool@gmail.com>
10 *
11 * register initialization is based on code examples provided by Philips
12 * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
13 *
14 * NOTE: This driver does not have suspend/resume functionality
15 * This driver is intended for engineering development purposes only
16 *
17 * 2005-2006 (c) MontaVista Software, Inc.
18 */
19 #include <linux/clk.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/io.h>
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/usb/isp1301.h>
27 #include <linux/usb.h>
28 #include <linux/usb/hcd.h>
29
30 #include "ohci.h"
31
32 #define USB_CONFIG_BASE 0x31020000
33
34 /* USB_OTG_STAT_CONTROL bit defines */
35 #define TRANSPARENT_I2C_EN (1 << 7)
36 #define HOST_EN (1 << 0)
37
38 /* On LPC32xx, those are undefined */
39 #ifndef start_int_set_falling_edge
40 #define start_int_set_falling_edge(irq)
41 #define start_int_set_rising_edge(irq)
42 #define start_int_ack(irq)
43 #define start_int_mask(irq)
44 #define start_int_umask(irq)
45 #endif
46
47 #define DRIVER_DESC "OHCI NXP driver"
48
49 static const char hcd_name[] = "ohci-nxp";
50 static struct hc_driver __read_mostly ohci_nxp_hc_driver;
51
52 static struct i2c_client *isp1301_i2c_client;
53
54 static struct clk *usb_host_clk;
55
isp1301_configure_lpc32xx(void)56 static void isp1301_configure_lpc32xx(void)
57 {
58 /* LPC32XX only supports DAT_SE0 USB mode */
59 /* This sequence is important */
60
61 /* Disable transparent UART mode first */
62 i2c_smbus_write_byte_data(isp1301_i2c_client,
63 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
64 MC1_UART_EN);
65 i2c_smbus_write_byte_data(isp1301_i2c_client,
66 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
67 ~MC1_SPEED_REG);
68 i2c_smbus_write_byte_data(isp1301_i2c_client,
69 ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
70 i2c_smbus_write_byte_data(isp1301_i2c_client,
71 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
72 ~0);
73 i2c_smbus_write_byte_data(isp1301_i2c_client,
74 ISP1301_I2C_MODE_CONTROL_2,
75 (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
76 i2c_smbus_write_byte_data(isp1301_i2c_client,
77 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
78 i2c_smbus_write_byte_data(isp1301_i2c_client,
79 ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
80 i2c_smbus_write_byte_data(isp1301_i2c_client,
81 ISP1301_I2C_OTG_CONTROL_1,
82 (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
83 i2c_smbus_write_byte_data(isp1301_i2c_client,
84 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
85 (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
86 i2c_smbus_write_byte_data(isp1301_i2c_client,
87 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
88 i2c_smbus_write_byte_data(isp1301_i2c_client,
89 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
90 ~0);
91 i2c_smbus_write_byte_data(isp1301_i2c_client,
92 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
93
94 printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
95 i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
96 printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
97 i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
98 printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
99 i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
100 }
101
isp1301_configure(void)102 static void isp1301_configure(void)
103 {
104 isp1301_configure_lpc32xx();
105 }
106
isp1301_vbus_on(void)107 static inline void isp1301_vbus_on(void)
108 {
109 i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
110 OTG1_VBUS_DRV);
111 }
112
isp1301_vbus_off(void)113 static inline void isp1301_vbus_off(void)
114 {
115 i2c_smbus_write_byte_data(isp1301_i2c_client,
116 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
117 OTG1_VBUS_DRV);
118 }
119
ohci_nxp_start_hc(void)120 static void ohci_nxp_start_hc(void)
121 {
122 void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
123 unsigned long tmp;
124
125 if (WARN_ON(!usb_otg_stat_control))
126 return;
127
128 tmp = __raw_readl(usb_otg_stat_control) | HOST_EN;
129
130 __raw_writel(tmp, usb_otg_stat_control);
131 isp1301_vbus_on();
132
133 iounmap(usb_otg_stat_control);
134 }
135
ohci_nxp_stop_hc(void)136 static void ohci_nxp_stop_hc(void)
137 {
138 void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
139 unsigned long tmp;
140
141 if (WARN_ON(!usb_otg_stat_control))
142 return;
143
144 isp1301_vbus_off();
145 tmp = __raw_readl(usb_otg_stat_control) & ~HOST_EN;
146 __raw_writel(tmp, usb_otg_stat_control);
147
148 iounmap(usb_otg_stat_control);
149 }
150
ohci_hcd_nxp_probe(struct platform_device * pdev)151 static int ohci_hcd_nxp_probe(struct platform_device *pdev)
152 {
153 struct usb_hcd *hcd = NULL;
154 const struct hc_driver *driver = &ohci_nxp_hc_driver;
155 struct resource *res;
156 int ret = 0, irq;
157 struct device_node *isp1301_node;
158
159 if (pdev->dev.of_node) {
160 isp1301_node = of_parse_phandle(pdev->dev.of_node,
161 "transceiver", 0);
162 } else {
163 isp1301_node = NULL;
164 }
165
166 isp1301_i2c_client = isp1301_get_client(isp1301_node);
167 of_node_put(isp1301_node);
168 if (!isp1301_i2c_client)
169 return -EPROBE_DEFER;
170
171 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
172 if (ret)
173 goto fail_disable;
174
175 dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
176 if (usb_disabled()) {
177 dev_err(&pdev->dev, "USB is disabled\n");
178 ret = -ENODEV;
179 goto fail_disable;
180 }
181
182 /* Enable USB host clock */
183 usb_host_clk = devm_clk_get(&pdev->dev, NULL);
184 if (IS_ERR(usb_host_clk)) {
185 dev_err(&pdev->dev, "failed to acquire USB OHCI clock\n");
186 ret = PTR_ERR(usb_host_clk);
187 goto fail_disable;
188 }
189
190 ret = clk_prepare_enable(usb_host_clk);
191 if (ret < 0) {
192 dev_err(&pdev->dev, "failed to start USB OHCI clock\n");
193 goto fail_disable;
194 }
195
196 isp1301_configure();
197
198 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
199 if (!hcd) {
200 dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
201 ret = -ENOMEM;
202 goto fail_hcd;
203 }
204
205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
206 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
207 if (IS_ERR(hcd->regs)) {
208 ret = PTR_ERR(hcd->regs);
209 goto fail_resource;
210 }
211 hcd->rsrc_start = res->start;
212 hcd->rsrc_len = resource_size(res);
213
214 irq = platform_get_irq(pdev, 0);
215 if (irq < 0) {
216 ret = -ENXIO;
217 goto fail_resource;
218 }
219
220 ohci_nxp_start_hc();
221 platform_set_drvdata(pdev, hcd);
222
223 dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
224 ret = usb_add_hcd(hcd, irq, 0);
225 if (ret == 0) {
226 device_wakeup_enable(hcd->self.controller);
227 return ret;
228 }
229
230 ohci_nxp_stop_hc();
231 fail_resource:
232 usb_put_hcd(hcd);
233 fail_hcd:
234 clk_disable_unprepare(usb_host_clk);
235 fail_disable:
236 isp1301_i2c_client = NULL;
237 return ret;
238 }
239
ohci_hcd_nxp_remove(struct platform_device * pdev)240 static int ohci_hcd_nxp_remove(struct platform_device *pdev)
241 {
242 struct usb_hcd *hcd = platform_get_drvdata(pdev);
243
244 usb_remove_hcd(hcd);
245 ohci_nxp_stop_hc();
246 usb_put_hcd(hcd);
247 clk_disable_unprepare(usb_host_clk);
248 isp1301_i2c_client = NULL;
249
250 return 0;
251 }
252
253 /* work with hotplug and coldplug */
254 MODULE_ALIAS("platform:usb-ohci");
255
256 #ifdef CONFIG_OF
257 static const struct of_device_id ohci_hcd_nxp_match[] = {
258 { .compatible = "nxp,ohci-nxp" },
259 {},
260 };
261 MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
262 #endif
263
264 static struct platform_driver ohci_hcd_nxp_driver = {
265 .driver = {
266 .name = "usb-ohci",
267 .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
268 },
269 .probe = ohci_hcd_nxp_probe,
270 .remove = ohci_hcd_nxp_remove,
271 };
272
ohci_nxp_init(void)273 static int __init ohci_nxp_init(void)
274 {
275 if (usb_disabled())
276 return -ENODEV;
277
278 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
279
280 ohci_init_driver(&ohci_nxp_hc_driver, NULL);
281 return platform_driver_register(&ohci_hcd_nxp_driver);
282 }
283 module_init(ohci_nxp_init);
284
ohci_nxp_cleanup(void)285 static void __exit ohci_nxp_cleanup(void)
286 {
287 platform_driver_unregister(&ohci_hcd_nxp_driver);
288 }
289 module_exit(ohci_nxp_cleanup);
290
291 MODULE_DESCRIPTION(DRIVER_DESC);
292 MODULE_LICENSE("GPL v2");
293