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