blob: 6df8e2ed40fd9966229bf6487d34b2eb331b8d28 [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
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +020059static struct clk *usb_host_clk;
Vitaly Wool60bbfc82006-06-29 18:28:18 +040060
Roland Stigge53dc25a2012-03-12 22:54:51 +010061static void isp1301_configure_lpc32xx(void)
62{
63 /* LPC32XX only supports DAT_SE0 USB mode */
64 /* This sequence is important */
65
66 /* Disable transparent UART mode first */
67 i2c_smbus_write_byte_data(isp1301_i2c_client,
68 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
69 MC1_UART_EN);
70 i2c_smbus_write_byte_data(isp1301_i2c_client,
71 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
72 ~MC1_SPEED_REG);
73 i2c_smbus_write_byte_data(isp1301_i2c_client,
74 ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
75 i2c_smbus_write_byte_data(isp1301_i2c_client,
76 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
77 ~0);
78 i2c_smbus_write_byte_data(isp1301_i2c_client,
79 ISP1301_I2C_MODE_CONTROL_2,
80 (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
81 i2c_smbus_write_byte_data(isp1301_i2c_client,
82 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
83 i2c_smbus_write_byte_data(isp1301_i2c_client,
84 ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
85 i2c_smbus_write_byte_data(isp1301_i2c_client,
86 ISP1301_I2C_OTG_CONTROL_1,
87 (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
88 i2c_smbus_write_byte_data(isp1301_i2c_client,
89 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
90 (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
91 i2c_smbus_write_byte_data(isp1301_i2c_client,
92 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
93 i2c_smbus_write_byte_data(isp1301_i2c_client,
94 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
95 ~0);
96 i2c_smbus_write_byte_data(isp1301_i2c_client,
97 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
98
Roland Stigge53dc25a2012-03-12 22:54:51 +010099 printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
100 i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
101 printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
102 i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
103 printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
104 i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
105}
106
107static void isp1301_configure(void)
108{
Roland Stigged684f052012-08-26 16:30:37 +0200109 isp1301_configure_lpc32xx();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400110}
111
112static inline void isp1301_vbus_on(void)
113{
Roland Stiggea6a36002012-03-12 22:54:52 +0100114 i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
115 OTG1_VBUS_DRV);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400116}
117
118static inline void isp1301_vbus_off(void)
119{
Roland Stiggea6a36002012-03-12 22:54:52 +0100120 i2c_smbus_write_byte_data(isp1301_i2c_client,
121 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
122 OTG1_VBUS_DRV);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400123}
124
Manjunath Goudar30330b82013-09-21 16:38:44 +0530125static void ohci_nxp_start_hc(void)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400126{
127 unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
Manjunath Goudar5c5a94a2016-12-07 23:54:11 +0000128
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400129 __raw_writel(tmp, USB_OTG_STAT_CONTROL);
130 isp1301_vbus_on();
131}
132
Manjunath Goudar30330b82013-09-21 16:38:44 +0530133static void ohci_nxp_stop_hc(void)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400134{
135 unsigned long tmp;
Manjunath Goudar5c5a94a2016-12-07 23:54:11 +0000136
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400137 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);
Manjunath Goudar5c5a94a2016-12-07 23:54:11 +0000158 if (!isp1301_i2c_client)
Libo Chenb3517d52013-05-10 14:22:42 +0800159 return -EPROBE_DEFER;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400160
Russell Kinge1fd7342013-06-27 12:36:37 +0100161 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100162 if (ret)
163 goto fail_disable;
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200164
Roland Stigge28643102012-03-12 22:54:50 +0100165 dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400166 if (usb_disabled()) {
Greg Kroah-Hartmanb6c227e2012-04-27 11:24:41 -0700167 dev_err(&pdev->dev, "USB is disabled\n");
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400168 ret = -ENODEV;
Libo Chenb3517d52013-05-10 14:22:42 +0800169 goto fail_disable;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400170 }
171
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200172 /* Enable USB host clock */
173 usb_host_clk = devm_clk_get(&pdev->dev, NULL);
174 if (IS_ERR(usb_host_clk)) {
175 dev_err(&pdev->dev, "failed to acquire USB OHCI clock\n");
176 ret = PTR_ERR(usb_host_clk);
Jingoo Hanac8d81f2013-12-11 16:30:35 +0900177 goto fail_disable;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400178 }
179
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200180 ret = clk_prepare_enable(usb_host_clk);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400181 if (ret < 0) {
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200182 dev_err(&pdev->dev, "failed to start USB OHCI clock\n");
Jingoo Hanac8d81f2013-12-11 16:30:35 +0900183 goto fail_disable;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400184 }
185
Alexandre Pereira da Silva78091dc2012-06-20 09:02:23 -0300186 isp1301_configure();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400187
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200188 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400189 if (!hcd) {
Greg Kroah-Hartmanb6c227e2012-04-27 11:24:41 -0700190 dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400191 ret = -ENOMEM;
Libo Chenb3517d52013-05-10 14:22:42 +0800192 goto fail_hcd;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400193 }
194
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200195 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100196 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
197 if (IS_ERR(hcd->regs)) {
198 ret = PTR_ERR(hcd->regs);
Libo Chenb3517d52013-05-10 14:22:42 +0800199 goto fail_resource;
Roland Stiggea6a99cf2012-04-29 16:47:02 +0200200 }
201 hcd->rsrc_start = res->start;
202 hcd->rsrc_len = resource_size(res);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400203
204 irq = platform_get_irq(pdev, 0);
205 if (irq < 0) {
206 ret = -ENXIO;
Libo Chenb3517d52013-05-10 14:22:42 +0800207 goto fail_resource;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400208 }
209
Manjunath Goudar30330b82013-09-21 16:38:44 +0530210 ohci_nxp_start_hc();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400211 platform_set_drvdata(pdev, hcd);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400212
213 dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800214 ret = usb_add_hcd(hcd, irq, 0);
Peter Chen3c9740a2013-11-05 10:46:02 +0800215 if (ret == 0) {
216 device_wakeup_enable(hcd->self.controller);
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400217 return ret;
Peter Chen3c9740a2013-11-05 10:46:02 +0800218 }
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400219
Manjunath Goudar30330b82013-09-21 16:38:44 +0530220 ohci_nxp_stop_hc();
Libo Chenb3517d52013-05-10 14:22:42 +0800221fail_resource:
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400222 usb_put_hcd(hcd);
Libo Chenb3517d52013-05-10 14:22:42 +0800223fail_hcd:
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200224 clk_disable_unprepare(usb_host_clk);
Libo Chenb3517d52013-05-10 14:22:42 +0800225fail_disable:
Jean Delvare3a407e72008-11-13 18:57:53 +0100226 isp1301_i2c_client = NULL;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400227 return ret;
228}
229
Manjunath Goudar30330b82013-09-21 16:38:44 +0530230static int ohci_hcd_nxp_remove(struct platform_device *pdev)
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400231{
232 struct usb_hcd *hcd = platform_get_drvdata(pdev);
233
234 usb_remove_hcd(hcd);
Manjunath Goudar30330b82013-09-21 16:38:44 +0530235 ohci_nxp_stop_hc();
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400236 usb_put_hcd(hcd);
Vladimir Zapolskiy83253b32016-03-03 06:19:36 +0200237 clk_disable_unprepare(usb_host_clk);
Jean Delvare3a407e72008-11-13 18:57:53 +0100238 isp1301_i2c_client = NULL;
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400239
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400240 return 0;
241}
242
Kay Sieversf4fce612008-04-10 21:29:22 -0700243/* work with hotplug and coldplug */
244MODULE_ALIAS("platform:usb-ohci");
245
Roland Stigge2265efe2012-04-29 16:47:03 +0200246#ifdef CONFIG_OF
Manjunath Goudar30330b82013-09-21 16:38:44 +0530247static const struct of_device_id ohci_hcd_nxp_match[] = {
Roland Stigge2265efe2012-04-29 16:47:03 +0200248 { .compatible = "nxp,ohci-nxp" },
249 {},
250};
Manjunath Goudar30330b82013-09-21 16:38:44 +0530251MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
Roland Stigge2265efe2012-04-29 16:47:03 +0200252#endif
253
Manjunath Goudar30330b82013-09-21 16:38:44 +0530254static struct platform_driver ohci_hcd_nxp_driver = {
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400255 .driver = {
256 .name = "usb-ohci",
Manjunath Goudar30330b82013-09-21 16:38:44 +0530257 .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400258 },
Manjunath Goudar30330b82013-09-21 16:38:44 +0530259 .probe = ohci_hcd_nxp_probe,
260 .remove = ohci_hcd_nxp_remove,
Vitaly Wool60bbfc82006-06-29 18:28:18 +0400261};
262
Manjunath Goudar30330b82013-09-21 16:38:44 +0530263static int __init ohci_nxp_init(void)
264{
265 if (usb_disabled())
266 return -ENODEV;
267
268 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
269
270 ohci_init_driver(&ohci_nxp_hc_driver, NULL);
271 return platform_driver_register(&ohci_hcd_nxp_driver);
272}
273module_init(ohci_nxp_init);
274
275static void __exit ohci_nxp_cleanup(void)
276{
277 platform_driver_unregister(&ohci_hcd_nxp_driver);
278}
279module_exit(ohci_nxp_cleanup);
280
281MODULE_DESCRIPTION(DRIVER_DESC);
282MODULE_LICENSE("GPL v2");