blob: b7d4756232ae305ed2502fb531cab50fa51addc5 [file] [log] [blame]
Vitaly Wool60bbfc82006-06-29 18:28:18 +04001/*
Roland Stigge28643102012-03-12 22:54:50 +01002 * driver for NXP USB Host devices
Vitaly Wool60bbfc82006-06-29 18:28:18 +04003 *
Roland Stigge28643102012-03-12 22:54:50 +01004 * Currently supported OHCI host devices:
Roland Stigge53dc25a2012-03-12 22:54:51 +01005 * - NXP LPC32xx
Vitaly Wool60bbfc82006-06-29 18:28:18 +04006 *
7 * Authors: Dmitry Chigirev <source@mvista.com>
David Brownelldd9048a2006-12-05 03:18:31 -08008 * Vitaly Wool <vitalywool@gmail.com>
Vitaly Wool60bbfc82006-06-29 18:28:18 +04009 *
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>
Manjunath Goudar30330b82013-09-21 16:38:44 +053022#include <linux/dma-mapping.h>
23#include <linux/io.h>
Vitaly Wool60bbfc82006-06-29 18:28:18 +040024#include <linux/i2c.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053025#include <linux/module.h>
Roland Stigge2265efe2012-04-29 16:47:03 +020026#include <linux/of.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053027#include <linux/platform_device.h>
Roland Stigge73108aa2012-04-29 16:47:06 +020028#include <linux/usb/isp1301.h>
Manjunath Goudar30330b82013-09-21 16:38:44 +053029#include <linux/usb.h>
30#include <linux/usb/hcd.h>
31
32#include "ohci.h"
33
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Vitaly Wool60bbfc82006-06-29 18:28:18 +040035
Roland Stigge53dc25a2012-03-12 22:54:51 +010036#define USB_CONFIG_BASE 0x31020000
Roland Stigge53dc25a2012-03-12 22:54:51 +010037#define USB_OTG_STAT_CONTROL IO_ADDRESS(USB_CONFIG_BASE + 0x110)
Vitaly Wool60bbfc82006-06-29 18:28:18 +040038
39/* USB_OTG_STAT_CONTROL bit defines */
40#define TRANSPARENT_I2C_EN (1 << 7)
41#define HOST_EN (1 << 0)
42
Roland Stigge53dc25a2012-03-12 22:54:51 +010043/* 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
Manjunath Goudar30330b82013-09-21 16:38:44 +053052#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
Jean Delvare09ce4972009-10-01 19:03:13 +020057static struct i2c_client *isp1301_i2c_client;
Vitaly Wool60bbfc82006-06-29 18:28:18 +040058
59extern int usb_disabled(void);
Vitaly Wool60bbfc82006-06-29 18:28:18 +040060
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +020061static struct clk *usb_host_clk;
Vitaly Wool60bbfc82006-06-29 18:28:18 +040062
Roland Stigge53dc25a2012-03-12 22:54:51 +010063static void isp1301_configure_lpc32xx(void)
64{
65 /* LPC32XX only supports DAT_SE0 USB mode */
66 /* This sequence is important */
67
68 /* Disable transparent UART mode first */
69 i2c_smbus_write_byte_data(isp1301_i2c_client,
70 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
71 MC1_UART_EN);
72 i2c_smbus_write_byte_data(isp1301_i2c_client,
73 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
74 ~MC1_SPEED_REG);
75 i2c_smbus_write_byte_data(isp1301_i2c_client,
76 ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
77 i2c_smbus_write_byte_data(isp1301_i2c_client,
78 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
79 ~0);
80 i2c_smbus_write_byte_data(isp1301_i2c_client,
81 ISP1301_I2C_MODE_CONTROL_2,
82 (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
83 i2c_smbus_write_byte_data(isp1301_i2c_client,
84 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
85 i2c_smbus_write_byte_data(isp1301_i2c_client,
86 ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
87 i2c_smbus_write_byte_data(isp1301_i2c_client,
88 ISP1301_I2C_OTG_CONTROL_1,
89 (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
90 i2c_smbus_write_byte_data(isp1301_i2c_client,
91 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
92 (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
93 i2c_smbus_write_byte_data(isp1301_i2c_client,
94 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
95 i2c_smbus_write_byte_data(isp1301_i2c_client,
96 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
97 ~0);
98 i2c_smbus_write_byte_data(isp1301_i2c_client,
99 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
100
Roland Stigge53dc25a2012-03-12 22:54:51 +0100101 printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
102 i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
103 printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
104 i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
105 printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
106 i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
107}
108
109static void isp1301_configure(void)
110{
Roland Stigged684f052012-08-26 16:30:37 +0200111 isp1301_configure_lpc32xx();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400112}
113
114static inline void isp1301_vbus_on(void)
115{
Roland Stiggea6a36002012-03-12 22:54:52 +0100116 i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
117 OTG1_VBUS_DRV);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400118}
119
120static inline void isp1301_vbus_off(void)
121{
Roland Stiggea6a36002012-03-12 22:54:52 +0100122 i2c_smbus_write_byte_data(isp1301_i2c_client,
123 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
124 OTG1_VBUS_DRV);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400125}
126
Manjunath Goudar30330b82013-09-21 16:38:44 +0530127static void ohci_nxp_start_hc(void)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400128{
129 unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
130 __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;
137 isp1301_vbus_off();
138 tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
139 __raw_writel(tmp, USB_OTG_STAT_CONTROL);
140}
141
Manjunath Goudar30330b82013-09-21 16:38:44 +0530142static int ohci_hcd_nxp_probe(struct platform_device *pdev)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400143{
144 struct usb_hcd *hcd = 0;
Roland Stigge28643102012-03-12 22:54:50 +0100145 const struct hc_driver *driver = &ohci_nxp_hc_driver;
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200146 struct resource *res;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400147 int ret = 0, irq;
Roland Stigge73108aa2012-04-29 16:47:06 +0200148 struct device_node *isp1301_node;
149
150 if (pdev->dev.of_node) {
151 isp1301_node = of_parse_phandle(pdev->dev.of_node,
152 "transceiver", 0);
153 } else {
154 isp1301_node = NULL;
155 }
156
157 isp1301_i2c_client = isp1301_get_client(isp1301_node);
158 if (!isp1301_i2c_client) {
Libo Chenb3517d52013-05-10 14:22:42 +0800159 return -EPROBE_DEFER;
Roland Stigge73108aa2012-04-29 16:47:06 +0200160 }
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");