]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/host/ohci-nxp.c
Merge tag 'pm-5.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / host / ohci-nxp.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
60bbfc84 2/*
28643104 3 * driver for NXP USB Host devices
60bbfc84 4 *
28643104 5 * Currently supported OHCI host devices:
53dc25ad 6 * - NXP LPC32xx
60bbfc84
VW
7 *
8 * Authors: Dmitry Chigirev <source@mvista.com>
dd9048af 9 * Vitaly Wool <vitalywool@gmail.com>
60bbfc84
VW
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 *
ef1362d9 17 * 2005-2006 (c) MontaVista Software, Inc.
60bbfc84
VW
18 */
19#include <linux/clk.h>
30330b8f
MG
20#include <linux/dma-mapping.h>
21#include <linux/io.h>
60bbfc84 22#include <linux/i2c.h>
30330b8f 23#include <linux/module.h>
2265efea 24#include <linux/of.h>
30330b8f 25#include <linux/platform_device.h>
73108aa9 26#include <linux/usb/isp1301.h>
30330b8f
MG
27#include <linux/usb.h>
28#include <linux/usb/hcd.h>
29
30#include "ohci.h"
31
53dc25ad 32#define USB_CONFIG_BASE 0x31020000
60bbfc84
VW
33
34/* USB_OTG_STAT_CONTROL bit defines */
35#define TRANSPARENT_I2C_EN (1 << 7)
36#define HOST_EN (1 << 0)
37
53dc25ad
RS
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
30330b8f
MG
47#define DRIVER_DESC "OHCI NXP driver"
48
49static const char hcd_name[] = "ohci-nxp";
50static struct hc_driver __read_mostly ohci_nxp_hc_driver;
51
09ce497e 52static struct i2c_client *isp1301_i2c_client;
60bbfc84 53
83253b3b 54static struct clk *usb_host_clk;
60bbfc84 55
53dc25ad
RS
56static 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
53dc25ad
RS
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
102static void isp1301_configure(void)
103{
d684f05f 104 isp1301_configure_lpc32xx();
60bbfc84
VW
105}
106
107static inline void isp1301_vbus_on(void)
108{
a6a36008
RS
109 i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
110 OTG1_VBUS_DRV);
60bbfc84
VW
111}
112
113static inline void isp1301_vbus_off(void)
114{
a6a36008
RS
115 i2c_smbus_write_byte_data(isp1301_i2c_client,
116 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
117 OTG1_VBUS_DRV);
60bbfc84
VW
118}
119
30330b8f 120static void ohci_nxp_start_hc(void)
60bbfc84 121{
23565bae
AB
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;
5c5a94a5 129
23565bae 130 __raw_writel(tmp, usb_otg_stat_control);
60bbfc84 131 isp1301_vbus_on();
23565bae
AB
132
133 iounmap(usb_otg_stat_control);
60bbfc84
VW
134}
135
30330b8f 136static void ohci_nxp_stop_hc(void)
60bbfc84 137{
23565bae 138 void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
60bbfc84 139 unsigned long tmp;
5c5a94a5 140
23565bae
AB
141 if (WARN_ON(!usb_otg_stat_control))
142 return;
143
60bbfc84 144 isp1301_vbus_off();
23565bae
AB
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);
60bbfc84
VW
149}
150
30330b8f 151static int ohci_hcd_nxp_probe(struct platform_device *pdev)
60bbfc84 152{
09e2a8b7 153 struct usb_hcd *hcd = NULL;
28643104 154 const struct hc_driver *driver = &ohci_nxp_hc_driver;
a6a99cf0 155 struct resource *res;
60bbfc84 156 int ret = 0, irq;
73108aa9
RS
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);
5c5a94a5 167 if (!isp1301_i2c_client)
b3517d5d 168 return -EPROBE_DEFER;
60bbfc84 169
e1fd7341 170 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
22d9d8e8
RK
171 if (ret)
172 goto fail_disable;
a6a99cf0 173
28643104 174 dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
60bbfc84 175 if (usb_disabled()) {
b6c227ed 176 dev_err(&pdev->dev, "USB is disabled\n");
60bbfc84 177 ret = -ENODEV;
b3517d5d 178 goto fail_disable;
60bbfc84
VW
179 }
180
83253b3b
VZ
181 /* Enable USB host clock */
182 usb_host_clk = devm_clk_get(&pdev->dev, NULL);
183 if (IS_ERR(usb_host_clk)) {
184 dev_err(&pdev->dev, "failed to acquire USB OHCI clock\n");
185 ret = PTR_ERR(usb_host_clk);
ac8d81f3 186 goto fail_disable;
60bbfc84
VW
187 }
188
83253b3b 189 ret = clk_prepare_enable(usb_host_clk);
60bbfc84 190 if (ret < 0) {
83253b3b 191 dev_err(&pdev->dev, "failed to start USB OHCI clock\n");
ac8d81f3 192 goto fail_disable;
60bbfc84
VW
193 }
194
78091dc2 195 isp1301_configure();
60bbfc84 196
a6a99cf0 197 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
60bbfc84 198 if (!hcd) {
b6c227ed 199 dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
60bbfc84 200 ret = -ENOMEM;
b3517d5d 201 goto fail_hcd;
60bbfc84
VW
202 }
203
a6a99cf0 204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
148e1134
TR
205 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
206 if (IS_ERR(hcd->regs)) {
207 ret = PTR_ERR(hcd->regs);
b3517d5d 208 goto fail_resource;
60bbfc84 209 }
a6a99cf0
RS
210 hcd->rsrc_start = res->start;
211 hcd->rsrc_len = resource_size(res);
60bbfc84
VW
212
213 irq = platform_get_irq(pdev, 0);
214 if (irq < 0) {
215 ret = -ENXIO;
b3517d5d 216 goto fail_resource;
60bbfc84
VW
217 }
218
30330b8f 219 ohci_nxp_start_hc();
60bbfc84 220 platform_set_drvdata(pdev, hcd);
60bbfc84
VW
221
222 dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
b5dd18d8 223 ret = usb_add_hcd(hcd, irq, 0);
3c9740a1
PC
224 if (ret == 0) {
225 device_wakeup_enable(hcd->self.controller);
60bbfc84 226 return ret;
3c9740a1 227 }
60bbfc84 228
30330b8f 229 ohci_nxp_stop_hc();
b3517d5d 230fail_resource:
60bbfc84 231 usb_put_hcd(hcd);
b3517d5d 232fail_hcd:
83253b3b 233 clk_disable_unprepare(usb_host_clk);
b3517d5d 234fail_disable:
3a407e73 235 isp1301_i2c_client = NULL;
60bbfc84
VW
236 return ret;
237}
238
30330b8f 239static int ohci_hcd_nxp_remove(struct platform_device *pdev)
60bbfc84
VW
240{
241 struct usb_hcd *hcd = platform_get_drvdata(pdev);
242
243 usb_remove_hcd(hcd);
30330b8f 244 ohci_nxp_stop_hc();
60bbfc84 245 usb_put_hcd(hcd);
83253b3b 246 clk_disable_unprepare(usb_host_clk);
3a407e73 247 isp1301_i2c_client = NULL;
60bbfc84 248
60bbfc84
VW
249 return 0;
250}
251
f4fce61d
KS
252/* work with hotplug and coldplug */
253MODULE_ALIAS("platform:usb-ohci");
254
2265efea 255#ifdef CONFIG_OF
30330b8f 256static const struct of_device_id ohci_hcd_nxp_match[] = {
2265efea
RS
257 { .compatible = "nxp,ohci-nxp" },
258 {},
259};
30330b8f 260MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
2265efea
RS
261#endif
262
30330b8f 263static struct platform_driver ohci_hcd_nxp_driver = {
60bbfc84
VW
264 .driver = {
265 .name = "usb-ohci",
30330b8f 266 .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
60bbfc84 267 },
30330b8f
MG
268 .probe = ohci_hcd_nxp_probe,
269 .remove = ohci_hcd_nxp_remove,
60bbfc84
VW
270};
271
30330b8f
MG
272static int __init ohci_nxp_init(void)
273{
274 if (usb_disabled())
275 return -ENODEV;
276
277 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
278
279 ohci_init_driver(&ohci_nxp_hc_driver, NULL);
280 return platform_driver_register(&ohci_hcd_nxp_driver);
281}
282module_init(ohci_nxp_init);
283
284static void __exit ohci_nxp_cleanup(void)
285{
286 platform_driver_unregister(&ohci_hcd_nxp_driver);
287}
288module_exit(ohci_nxp_cleanup);
289
290MODULE_DESCRIPTION(DRIVER_DESC);
291MODULE_LICENSE("GPL v2");