| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* driver for NXP USB Host devices |
| 4 |
* |
| 5 |
* Currently supported OHCI host devices: |
| 6 |
* - NXP LPC32xx |
| 7 |
* |
| 8 |
* Authors: Dmitry Chigirev <[email protected]> |
| 9 |
* Vitaly Wool <[email protected]> |
| 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 |
* |
| 17 |
* 2005-2006 (c) MontaVista Software, Inc. |
| 18 |
*/ |
| 19 |
#include <linux/clk.h> |
| 20 |
#include <linux/dma-mapping.h> |
| 21 |
#include <linux/io.h> |
| 22 |
#include <linux/i2c.h> |
| 23 |
#include <linux/module.h> |
| 24 |
#include <linux/of.h> |
| 25 |
#include <linux/platform_device.h> |
| 26 |
#include <linux/usb/isp1301.h> |
| 27 |
#include <linux/usb.h> |
| 28 |
#include <linux/usb/hcd.h> |
| 29 |
|
| 30 |
#include "ohci.h" |
| 31 |
|
| 32 |
#define USB_CONFIG_BASE 0x31020000 |
| 33 |
|
| 34 |
/* USB_OTG_STAT_CONTROL bit defines */ |
| 35 |
#define TRANSPARENT_I2C_EN (1 << 7) |
| 36 |
#define HOST_EN (1 << 0) |
| 37 |
|
| 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 |
|
| 47 |
#define DRIVER_DESC "OHCI NXP driver" |
| 48 |
|
| 49 |
static const char hcd_name[] = "ohci-nxp"; |
| 50 |
static struct hc_driver __read_mostly ohci_nxp_hc_driver; |
| 51 |
|
| 52 |
static struct i2c_client *isp1301_i2c_client; |
| 53 |
|
| 54 |
static void isp1301_configure_lpc32xx(void) |
| 55 |
{ |
| 56 |
/* LPC32XX only supports DAT_SE0 USB mode */ |
| 57 |
/* This sequence is important */ |
| 58 |
|
| 59 |
/* Disable transparent UART mode first */ |
| 60 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 61 |
(ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), |
| 62 |
MC1_UART_EN); |
| 63 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 64 |
(ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), |
| 65 |
~MC1_SPEED_REG); |
| 66 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 67 |
ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG); |
| 68 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 69 |
(ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR), |
| 70 |
~0); |
| 71 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 72 |
ISP1301_I2C_MODE_CONTROL_2, |
| 73 |
(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL)); |
| 74 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 75 |
(ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0); |
| 76 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 77 |
ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0); |
| 78 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 79 |
ISP1301_I2C_OTG_CONTROL_1, |
| 80 |
(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN)); |
| 81 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 82 |
(ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), |
| 83 |
(OTG1_DM_PULLUP | OTG1_DP_PULLUP)); |
| 84 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 85 |
ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0); |
| 86 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 87 |
ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR, |
| 88 |
~0); |
| 89 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 90 |
ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0); |
| 91 |
|
| 92 |
printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n", |
| 93 |
i2c_smbus_read_word_data(isp1301_i2c_client, 0x00)); |
| 94 |
printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n", |
| 95 |
i2c_smbus_read_word_data(isp1301_i2c_client, 0x02)); |
| 96 |
printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n", |
| 97 |
i2c_smbus_read_word_data(isp1301_i2c_client, 0x14)); |
| 98 |
} |
| 99 |
|
| 100 |
static void isp1301_configure(void) |
| 101 |
{ |
| 102 |
isp1301_configure_lpc32xx(); |
| 103 |
} |
| 104 |
|
| 105 |
static inline void isp1301_vbus_on(void) |
| 106 |
{ |
| 107 |
i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1, |
| 108 |
OTG1_VBUS_DRV); |
| 109 |
} |
| 110 |
|
| 111 |
static inline void isp1301_vbus_off(void) |
| 112 |
{ |
| 113 |
i2c_smbus_write_byte_data(isp1301_i2c_client, |
| 114 |
ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR, |
| 115 |
OTG1_VBUS_DRV); |
| 116 |
} |
| 117 |
|
| 118 |
static void ohci_nxp_start_hc(void) |
| 119 |
{ |
| 120 |
void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4); |
| 121 |
unsigned long tmp; |
| 122 |
|
| 123 |
if (WARN_ON(!usb_otg_stat_control)) |
| 124 |
return; |
| 125 |
|
| 126 |
tmp = __raw_readl(usb_otg_stat_control) | HOST_EN; |
| 127 |
|
| 128 |
__raw_writel(tmp, usb_otg_stat_control); |
| 129 |
isp1301_vbus_on(); |
| 130 |
|
| 131 |
iounmap(usb_otg_stat_control); |
| 132 |
} |
| 133 |
|
| 134 |
static void ohci_nxp_stop_hc(void) |
| 135 |
{ |
| 136 |
void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4); |
| 137 |
unsigned long tmp; |
| 138 |
|
| 139 |
if (WARN_ON(!usb_otg_stat_control)) |
| 140 |
return; |
| 141 |
|
| 142 |
isp1301_vbus_off(); |
| 143 |
tmp = __raw_readl(usb_otg_stat_control) & ~HOST_EN; |
| 144 |
__raw_writel(tmp, usb_otg_stat_control); |
| 145 |
|
| 146 |
iounmap(usb_otg_stat_control); |
| 147 |
} |
| 148 |
|
| 149 |
static int ohci_hcd_nxp_probe(struct platform_device *pdev) |
| 150 |
{ |
| 151 |
struct usb_hcd *hcd = NULL; |
| 152 |
const struct hc_driver *driver = &ohci_nxp_hc_driver; |
| 153 |
struct resource *res; |
| 154 |
int ret = 0, irq; |
| 155 |
struct device_node *isp1301_node; |
| 156 |
struct clk *usb_host_clk; |
| 157 |
|
| 158 |
if (pdev->dev.of_node) { |
| 159 |
isp1301_node = of_parse_phandle(pdev->dev.of_node, |
| 160 |
"transceiver", 0); |
| 161 |
} else { |
| 162 |
isp1301_node = NULL; |
| 163 |
} |
| 164 |
|
| 165 |
isp1301_i2c_client = isp1301_get_client(isp1301_node); |
| 166 |
of_node_put(isp1301_node); |
| 167 |
if (!isp1301_i2c_client) |
| 168 |
return -EPROBE_DEFER; |
| 169 |
|
| 170 |
ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 171 |
if (ret) |
| 172 |
goto err_put_client; |
| 173 |
|
| 174 |
dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name); |
| 175 |
if (usb_disabled()) { |
| 176 |
dev_err(&pdev->dev, "USB is disabled\n"); |
| 177 |
ret = -ENODEV; |
| 178 |
goto err_put_client; |
| 179 |
} |
| 180 |
|
| 181 |
/* Enable USB host clock */ |
| 182 |
usb_host_clk = devm_clk_get_enabled(&pdev->dev, NULL); |
| 183 |
if (IS_ERR(usb_host_clk)) { |
| 184 |
dev_err(&pdev->dev, "failed to acquire and start USB OHCI clock\n"); |
| 185 |
ret = PTR_ERR(usb_host_clk); |
| 186 |
goto err_put_client; |
| 187 |
} |
| 188 |
|
| 189 |
isp1301_configure(); |
| 190 |
|
| 191 |
hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 192 |
if (!hcd) { |
| 193 |
dev_err(&pdev->dev, "Failed to allocate HC buffer\n"); |
| 194 |
ret = -ENOMEM; |
| 195 |
goto err_put_client; |
| 196 |
} |
| 197 |
|
| 198 |
hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); |
| 199 |
if (IS_ERR(hcd->regs)) { |
| 200 |
ret = PTR_ERR(hcd->regs); |
| 201 |
goto err_put_hcd; |
| 202 |
} |
| 203 |
hcd->rsrc_start = res->start; |
| 204 |
hcd->rsrc_len = resource_size(res); |
| 205 |
|
| 206 |
irq = platform_get_irq(pdev, 0); |
| 207 |
if (irq < 0) { |
| 208 |
ret = -ENXIO; |
| 209 |
goto err_put_hcd; |
| 210 |
} |
| 211 |
|
| 212 |
ohci_nxp_start_hc(); |
| 213 |
platform_set_drvdata(pdev, hcd); |
| 214 |
|
| 215 |
dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq); |
| 216 |
ret = usb_add_hcd(hcd, irq, 0); |
| 217 |
if (ret == 0) { |
| 218 |
device_wakeup_enable(hcd->self.controller); |
| 219 |
return ret; |
| 220 |
} |
| 221 |
|
| 222 |
ohci_nxp_stop_hc(); |
| 223 |
err_put_hcd: |
| 224 |
usb_put_hcd(hcd); |
| 225 |
err_put_client: |
| 226 |
put_device(&isp1301_i2c_client->dev); |
| 227 |
isp1301_i2c_client = NULL; |
| 228 |
return ret; |
| 229 |
} |
| 230 |
|
| 231 |
static void ohci_hcd_nxp_remove(struct platform_device *pdev) |
| 232 |
{ |
| 233 |
struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 234 |
|
| 235 |
usb_remove_hcd(hcd); |
| 236 |
ohci_nxp_stop_hc(); |
| 237 |
usb_put_hcd(hcd); |
| 238 |
put_device(&isp1301_i2c_client->dev); |
| 239 |
isp1301_i2c_client = NULL; |
| 240 |
} |
| 241 |
|
| 242 |
/* work with hotplug and coldplug */ |
| 243 |
MODULE_ALIAS("platform:usb-ohci"); |
| 244 |
|
| 245 |
#ifdef CONFIG_OF |
| 246 |
static const struct of_device_id ohci_hcd_nxp_match[] = { |
| 247 |
{ .compatible = "nxp,ohci-nxp" }, |
| 248 |
{}, |
| 249 |
}; |
| 250 |
MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match); |
| 251 |
#endif |
| 252 |
|
| 253 |
static struct platform_driver ohci_hcd_nxp_driver = { |
| 254 |
.driver = { |
| 255 |
.name = "usb-ohci", |
| 256 |
.of_match_table = of_match_ptr(ohci_hcd_nxp_match), |
| 257 |
}, |
| 258 |
.probe = ohci_hcd_nxp_probe, |
| 259 |
.remove = ohci_hcd_nxp_remove, |
| 260 |
}; |
| 261 |
|
| 262 |
static int __init ohci_nxp_init(void) |
| 263 |
{ |
| 264 |
if (usb_disabled()) |
| 265 |
return -ENODEV; |
| 266 |
|
| 267 |
ohci_init_driver(&ohci_nxp_hc_driver, NULL); |
| 268 |
return platform_driver_register(&ohci_hcd_nxp_driver); |
| 269 |
} |
| 270 |
module_init(ohci_nxp_init); |
| 271 |
|
| 272 |
static void __exit ohci_nxp_cleanup(void) |
| 273 |
{ |
| 274 |
platform_driver_unregister(&ohci_hcd_nxp_driver); |
| 275 |
} |
| 276 |
module_exit(ohci_nxp_cleanup); |
| 277 |
|
| 278 |
MODULE_DESCRIPTION(DRIVER_DESC); |
| 279 |
MODULE_LICENSE("GPL v2"); |