blob: 5509b50bc417c320573c7afcf4b7092dc6f302ed [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Vitaly Wool60bbfc82006-06-29 18:28:18 +04002/*
Roland Stigge28643102012-03-12 22:54:50 +01003 * driver for NXP USB Host devices
Vitaly Wool60bbfc82006-06-29 18:28:18 +04004 *
Roland Stigge28643102012-03-12 22:54:50 +01005 * Currently supported OHCI host devices:
Roland Stigge53dc25a2012-03-12 22:54:51 +01006 * - NXP LPC32xx
Vitaly Wool60bbfc82006-06-29 18:28:18 +04007 *
8 * Authors: Dmitry Chigirev <source@mvista.com>
David Brownelldd9048a2006-12-05 03:18:31 -08009 * Vitaly Wool <vitalywool@gmail.com>
Vitaly Wool60bbfc82006-06-29 18:28:18 +040010 *
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. This file is licensed under
18 * the terms of the GNU General Public License version 2. This program
19 * is licensed "as is" without any warranty of any kind, whether express
20 * or implied.
21 */
22#include <linux/clk.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053023#include <linux/dma-mapping.h>
24#include <linux/io.h>
Vitaly Wool60bbfc82006-06-29 18:28:18 +040025#include <linux/i2c.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053026#include <linux/module.h>
Roland Stigge2265efe2012-04-29 16:47:03 +020027#include <linux/of.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053028#include <linux/platform_device.h>
Roland Stigge73108aa2012-04-29 16:47:06 +020029#include <linux/usb/isp1301.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053030#include <linux/usb.h>
31#include <linux/usb/hcd.h>
32
33#include "ohci.h"
34
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/hardware.h>
Vitaly Wool60bbfc82006-06-29 18:28:18 +040036
Roland Stigge53dc25a2012-03-12 22:54:51 +010037#define USB_CONFIG_BASE 0x31020000
Roland Stigge53dc25a2012-03-12 22:54:51 +010038#define USB_OTG_STAT_CONTROL IO_ADDRESS(USB_CONFIG_BASE + 0x110)
Vitaly Wool60bbfc82006-06-29 18:28:18 +040039
40/* USB_OTG_STAT_CONTROL bit defines */
41#define TRANSPARENT_I2C_EN (1 << 7)
42#define HOST_EN (1 << 0)
43
Roland Stigge53dc25a2012-03-12 22:54:51 +010044/* On LPC32xx, those are undefined */
45#ifndef start_int_set_falling_edge
46#define start_int_set_falling_edge(irq)
47#define start_int_set_rising_edge(irq)
48#define start_int_ack(irq)
49#define start_int_mask(irq)
50#define start_int_umask(irq)
51#endif
52
Manjunath Goudar30330b82013-09-21 16:38:44 +053053#define DRIVER_DESC "OHCI NXP driver"
54
55static const char hcd_name[] = "ohci-nxp";
56static struct hc_driver __read_mostly ohci_nxp_hc_driver;
57
Jean Delvare09ce4972009-10-01 19:03:13 +020058static struct i2c_client *isp1301_i2c_client;
Vitaly Wool60bbfc82006-06-29 18:28:18 +040059
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +020060static struct clk *usb_host_clk;
Vitaly Wool60bbfc82006-06-29 18:28:18 +040061
Roland Stigge53dc25a2012-03-12 22:54:51 +010062static void isp1301_configure_lpc32xx(void)
63{
64 /* LPC32XX only supports DAT_SE0 USB mode */
65 /* This sequence is important */
66
67 /* Disable transparent UART mode first */
68 i2c_smbus_write_byte_data(isp1301_i2c_client,
69 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
70 MC1_UART_EN);
71 i2c_smbus_write_byte_data(isp1301_i2c_client,
72 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
73 ~MC1_SPEED_REG);
74 i2c_smbus_write_byte_data(isp1301_i2c_client,
75 ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
76 i2c_smbus_write_byte_data(isp1301_i2c_client,
77 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
78 ~0);
79 i2c_smbus_write_byte_data(isp1301_i2c_client,
80 ISP1301_I2C_MODE_CONTROL_2,
81 (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
82 i2c_smbus_write_byte_data(isp1301_i2c_client,
83 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
84 i2c_smbus_write_byte_data(isp1301_i2c_client,
85 ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
86 i2c_smbus_write_byte_data(isp1301_i2c_client,
87 ISP1301_I2C_OTG_CONTROL_1,
88 (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
89 i2c_smbus_write_byte_data(isp1301_i2c_client,
90 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
91 (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
92 i2c_smbus_write_byte_data(isp1301_i2c_client,
93 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
94 i2c_smbus_write_byte_data(isp1301_i2c_client,
95 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
96 ~0);
97 i2c_smbus_write_byte_data(isp1301_i2c_client,
98 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
99
Roland Stigge53dc25a2012-03-12 22:54:51 +0100100 printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
101 i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
102 printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
103 i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
104 printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
105 i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
106}
107
108static void isp1301_configure(void)
109{
Roland Stigged684f052012-08-26 16:30:37 +0200110 isp1301_configure_lpc32xx();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400111}
112
113static inline void isp1301_vbus_on(void)
114{
Roland Stiggea6a36002012-03-12 22:54:52 +0100115 i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
116 OTG1_VBUS_DRV);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400117}
118
119static inline void isp1301_vbus_off(void)
120{
Roland Stiggea6a36002012-03-12 22:54:52 +0100121 i2c_smbus_write_byte_data(isp1301_i2c_client,
122 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
123 OTG1_VBUS_DRV);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400124}
125
Manjunath Goudar30330b82013-09-21 16:38:44 +0530126static void ohci_nxp_start_hc(void)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400127{
128 unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
Manjunath Goudar5c5a94a2016-12-07 23:54:11 +0000129
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400130 __raw_writel(tmp, USB_OTG_STAT_CONTROL);
131 isp1301_vbus_on();
132}
133
Manjunath Goudar30330b82013-09-21 16:38:44 +0530134static void ohci_nxp_stop_hc(void)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400135{
136 unsigned long tmp;
Manjunath Goudar5c5a94a2016-12-07 23:54:11 +0000137
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400138 isp1301_vbus_off();
139 tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
140 __raw_writel(tmp, USB_OTG_STAT_CONTROL);
141}
142
Manjunath Goudar30330b82013-09-21 16:38:44 +0530143static int ohci_hcd_nxp_probe(struct platform_device *pdev)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400144{
145 struct usb_hcd *hcd = 0;
Roland Stigge28643102012-03-12 22:54:50 +0100146 const struct hc_driver *driver = &ohci_nxp_hc_driver;
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200147 struct resource *res;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400148 int ret = 0, irq;
Roland Stigge73108aa2012-04-29 16:47:06 +0200149 struct device_node *isp1301_node;
150
151 if (pdev->dev.of_node) {
152 isp1301_node = of_parse_phandle(pdev->dev.of_node,
153 "transceiver", 0);
154 } else {
155 isp1301_node = NULL;
156 }
157
158 isp1301_i2c_client = isp1301_get_client(isp1301_node);
Manjunath Goudar5c5a94a2016-12-07 23:54:11 +0000159 if (!isp1301_i2c_client)
Libo Chenb3517d52013-05-10 14:22:42 +0800160 return -EPROBE_DEFER;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400161
Russell Kinge1fd7342013-06-27 12:36:37 +0100162 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100163 if (ret)
164 goto fail_disable;
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200165
Roland Stigge28643102012-03-12 22:54:50 +0100166 dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400167 if (usb_disabled()) {
Greg Kroah-Hartmanb6c227e2012-04-27 11:24:41 -0700168 dev_err(&pdev->dev, "USB is disabled\n");
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400169 ret = -ENODEV;
Libo Chenb3517d52013-05-10 14:22:42 +0800170 goto fail_disable;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400171 }
172
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200173 /* Enable USB host clock */
174 usb_host_clk = devm_clk_get(&pdev->dev, NULL);
175 if (IS_ERR(usb_host_clk)) {
176 dev_err(&pdev->dev, "failed to acquire USB OHCI clock\n");
177 ret = PTR_ERR(usb_host_clk);
Jingoo Hanac8d81f2013-12-11 16:30:35 +0900178 goto fail_disable;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400179 }
180
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200181 ret = clk_prepare_enable(usb_host_clk);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400182 if (ret < 0) {
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200183 dev_err(&pdev->dev, "failed to start USB OHCI clock\n");
Jingoo Hanac8d81f2013-12-11 16:30:35 +0900184 goto fail_disable;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400185 }
186
Alexandre Pereira da Silva78091dc2012-06-20 09:02:23 -0300187 isp1301_configure();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400188
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200189 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400190 if (!hcd) {
Greg Kroah-Hartmanb6c227e2012-04-27 11:24:41 -0700191 dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400192 ret = -ENOMEM;
Libo Chenb3517d52013-05-10 14:22:42 +0800193 goto fail_hcd;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400194 }
195
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100197 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
198 if (IS_ERR(hcd->regs)) {
199 ret = PTR_ERR(hcd->regs);
Libo Chenb3517d52013-05-10 14:22:42 +0800200 goto fail_resource;
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200201 }
202 hcd->rsrc_start = res->start;
203 hcd->rsrc_len = resource_size(res);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400204
205 irq = platform_get_irq(pdev, 0);
206 if (irq < 0) {
207 ret = -ENXIO;
Libo Chenb3517d52013-05-10 14:22:42 +0800208 goto fail_resource;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400209 }
210
Manjunath Goudar30330b82013-09-21 16:38:44 +0530211 ohci_nxp_start_hc();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400212 platform_set_drvdata(pdev, hcd);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400213
214 dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800215 ret = usb_add_hcd(hcd, irq, 0);
Peter Chen3c9740a2013-11-05 10:46:02 +0800216 if (ret == 0) {
217 device_wakeup_enable(hcd->self.controller);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400218 return ret;
Peter Chen3c9740a2013-11-05 10:46:02 +0800219 }
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400220
Manjunath Goudar30330b82013-09-21 16:38:44 +0530221 ohci_nxp_stop_hc();
Libo Chenb3517d52013-05-10 14:22:42 +0800222fail_resource:
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400223 usb_put_hcd(hcd);
Libo Chenb3517d52013-05-10 14:22:42 +0800224fail_hcd:
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200225 clk_disable_unprepare(usb_host_clk);
Libo Chenb3517d52013-05-10 14:22:42 +0800226fail_disable:
Jean Delvare3a407e72008-11-13 18:57:53 +0100227 isp1301_i2c_client = NULL;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400228 return ret;
229}
230
Manjunath Goudar30330b82013-09-21 16:38:44 +0530231static int ohci_hcd_nxp_remove(struct platform_device *pdev)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400232{
233 struct usb_hcd *hcd = platform_get_drvdata(pdev);
234
235 usb_remove_hcd(hcd);
Manjunath Goudar30330b82013-09-21 16:38:44 +0530236 ohci_nxp_stop_hc();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400237 usb_put_hcd(hcd);
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200238 clk_disable_unprepare(usb_host_clk);
Jean Delvare3a407e72008-11-13 18:57:53 +0100239 isp1301_i2c_client = NULL;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400240
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400241 return 0;
242}
243
Kay Sieversf4fce612008-04-10 21:29:22 -0700244/* work with hotplug and coldplug */
245MODULE_ALIAS("platform:usb-ohci");
246
Roland Stigge2265efe2012-04-29 16:47:03 +0200247#ifdef CONFIG_OF
Manjunath Goudar30330b82013-09-21 16:38:44 +0530248static const struct of_device_id ohci_hcd_nxp_match[] = {
Roland Stigge2265efe2012-04-29 16:47:03 +0200249 { .compatible = "nxp,ohci-nxp" },
250 {},
251};
Manjunath Goudar30330b82013-09-21 16:38:44 +0530252MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
Roland Stigge2265efe2012-04-29 16:47:03 +0200253#endif
254
Manjunath Goudar30330b82013-09-21 16:38:44 +0530255static struct platform_driver ohci_hcd_nxp_driver = {
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400256 .driver = {
257 .name = "usb-ohci",
Manjunath Goudar30330b82013-09-21 16:38:44 +0530258 .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400259 },
Manjunath Goudar30330b82013-09-21 16:38:44 +0530260 .probe = ohci_hcd_nxp_probe,
261 .remove = ohci_hcd_nxp_remove,
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400262};
263
Manjunath Goudar30330b82013-09-21 16:38:44 +0530264static int __init ohci_nxp_init(void)
265{
266 if (usb_disabled())
267 return -ENODEV;
268
269 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
270
271 ohci_init_driver(&ohci_nxp_hc_driver, NULL);
272 return platform_driver_register(&ohci_hcd_nxp_driver);
273}
274module_init(ohci_nxp_init);
275
276static void __exit ohci_nxp_cleanup(void)
277{
278 platform_driver_unregister(&ohci_hcd_nxp_driver);
279}
280module_exit(ohci_nxp_cleanup);
281
282MODULE_DESCRIPTION(DRIVER_DESC);
283MODULE_LICENSE("GPL v2");