ohci-nxp.c 9.1 KB
Newer Older
1
/*
2
 * driver for NXP USB Host devices
3
 *
4
 * Currently supported OHCI host devices:
5
 * - NXP LPC32xx
6 7
 *
 * Authors: Dmitry Chigirev <source@mvista.com>
8
 *	    Vitaly Wool <vitalywool@gmail.com>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * register initialization is based on code examples provided by Philips
 * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
 *
 * NOTE: This driver does not have suspend/resume functionality
 * This driver is intended for engineering development purposes only
 *
 * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
 * the terms of the GNU General Public License version 2. This program
 * is licensed "as is" without any warranty of any kind, whether express
 * or implied.
 */
#include <linux/clk.h>
22 23
#include <linux/dma-mapping.h>
#include <linux/io.h>
24
#include <linux/i2c.h>
25 26
#include <linux/kernel.h>
#include <linux/module.h>
R
Roland Stigge 已提交
27
#include <linux/of.h>
28
#include <linux/platform_device.h>
29
#include <linux/usb/isp1301.h>
30 31 32 33 34
#include <linux/usb.h>
#include <linux/usb/hcd.h>

#include "ohci.h"

35

36
#include <mach/hardware.h>
37
#include <asm/mach-types.h>
38 39
#include <asm/io.h>

40 41
#include <mach/platform.h>
#include <mach/irqs.h>
42

43 44 45 46
#define USB_CONFIG_BASE		0x31020000
#define PWRMAN_BASE		0x40004000

#define USB_CTRL		IO_ADDRESS(PWRMAN_BASE + 0x64)
47 48 49

/* USB_CTRL bit defines */
#define USB_SLAVE_HCLK_EN	(1 << 24)
R
Roland Stigge 已提交
50
#define USB_DEV_NEED_CLK_EN	(1 << 22)
51
#define USB_HOST_NEED_CLK_EN	(1 << 21)
R
Roland Stigge 已提交
52
#define PAD_CONTROL_LAST_DRIVEN	(1 << 19)
53

54
#define USB_OTG_STAT_CONTROL	IO_ADDRESS(USB_CONFIG_BASE + 0x110)
55 56 57 58 59

/* USB_OTG_STAT_CONTROL bit defines */
#define TRANSPARENT_I2C_EN	(1 << 7)
#define HOST_EN			(1 << 0)

60 61 62 63 64 65 66 67 68
/* On LPC32xx, those are undefined */
#ifndef start_int_set_falling_edge
#define start_int_set_falling_edge(irq)
#define start_int_set_rising_edge(irq)
#define start_int_ack(irq)
#define start_int_mask(irq)
#define start_int_umask(irq)
#endif

69 70 71 72 73
#define DRIVER_DESC "OHCI NXP driver"

static const char hcd_name[] = "ohci-nxp";
static struct hc_driver __read_mostly ohci_nxp_hc_driver;

74
static struct i2c_client *isp1301_i2c_client;
75 76 77

extern int usb_disabled(void);

78 79 80
static struct clk *usb_pll_clk;
static struct clk *usb_dev_clk;
static struct clk *usb_otg_clk;
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static void isp1301_configure_lpc32xx(void)
{
	/* LPC32XX only supports DAT_SE0 USB mode */
	/* This sequence is important */

	/* Disable transparent UART mode first */
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		(ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
		MC1_UART_EN);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		(ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
		~MC1_SPEED_REG);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
				  ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		  (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
		  ~0);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_MODE_CONTROL_2,
		(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		(ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_OTG_CONTROL_1,
		(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		(ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
		(OTG1_DM_PULLUP | OTG1_DP_PULLUP));
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
		~0);
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);

	/* Enable usb_need_clk clock after transceiver is initialized */
R
Roland Stigge 已提交
121
	__raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
122 123 124 125 126 127 128 129 130 131 132

	printk(KERN_INFO "ISP1301 Vendor ID  : 0x%04x\n",
	      i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
	printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
	      i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
	printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
	      i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
}

static void isp1301_configure(void)
{
133
	isp1301_configure_lpc32xx();
134 135 136 137
}

static inline void isp1301_vbus_on(void)
{
138 139
	i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
				  OTG1_VBUS_DRV);
140 141 142 143
}

static inline void isp1301_vbus_off(void)
{
144 145 146
	i2c_smbus_write_byte_data(isp1301_i2c_client,
		ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
		OTG1_VBUS_DRV);
147 148
}

149
static void ohci_nxp_start_hc(void)
150 151 152 153 154 155
{
	unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
	__raw_writel(tmp, USB_OTG_STAT_CONTROL);
	isp1301_vbus_on();
}

156
static void ohci_nxp_stop_hc(void)
157 158 159 160 161 162 163
{
	unsigned long tmp;
	isp1301_vbus_off();
	tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
	__raw_writel(tmp, USB_OTG_STAT_CONTROL);
}

164
static int ohci_hcd_nxp_probe(struct platform_device *pdev)
165 166
{
	struct usb_hcd *hcd = 0;
167
	const struct hc_driver *driver = &ohci_nxp_hc_driver;
R
Roland Stigge 已提交
168
	struct resource *res;
169
	int ret = 0, irq;
170 171 172 173 174 175 176 177 178 179 180
	struct device_node *isp1301_node;

	if (pdev->dev.of_node) {
		isp1301_node = of_parse_phandle(pdev->dev.of_node,
						"transceiver", 0);
	} else {
		isp1301_node = NULL;
	}

	isp1301_i2c_client = isp1301_get_client(isp1301_node);
	if (!isp1301_i2c_client) {
181
		return -EPROBE_DEFER;
182
	}
183

184
	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
185 186
	if (ret)
		goto fail_disable;
R
Roland Stigge 已提交
187

188
	dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
189
	if (usb_disabled()) {
190
		dev_err(&pdev->dev, "USB is disabled\n");
191
		ret = -ENODEV;
192
		goto fail_disable;
193 194 195
	}

	/* Enable AHB slave USB clock, needed for further USB clock control */
R
Roland Stigge 已提交
196
	__raw_writel(USB_SLAVE_HCLK_EN | PAD_CONTROL_LAST_DRIVEN, USB_CTRL);
197 198

	/* Enable USB PLL */
J
Jingoo Han 已提交
199
	usb_pll_clk = devm_clk_get(&pdev->dev, "ck_pll5");
200
	if (IS_ERR(usb_pll_clk)) {
201
		dev_err(&pdev->dev, "failed to acquire USB PLL\n");
202
		ret = PTR_ERR(usb_pll_clk);
J
Jingoo Han 已提交
203
		goto fail_disable;
204 205
	}

206
	ret = clk_enable(usb_pll_clk);
207
	if (ret < 0) {
208
		dev_err(&pdev->dev, "failed to start USB PLL\n");
J
Jingoo Han 已提交
209
		goto fail_disable;
210 211
	}

212
	ret = clk_set_rate(usb_pll_clk, 48000);
213
	if (ret < 0) {
214
		dev_err(&pdev->dev, "failed to set USB clock rate\n");
215
		goto fail_rate;
216 217
	}

218
	/* Enable USB device clock */
J
Jingoo Han 已提交
219
	usb_dev_clk = devm_clk_get(&pdev->dev, "ck_usbd");
220 221 222
	if (IS_ERR(usb_dev_clk)) {
		dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
		ret = PTR_ERR(usb_dev_clk);
J
Jingoo Han 已提交
223
		goto fail_rate;
224 225 226 227 228
	}

	ret = clk_enable(usb_dev_clk);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
J
Jingoo Han 已提交
229
		goto fail_rate;
230 231 232
	}

	/* Enable USB otg clocks */
J
Jingoo Han 已提交
233
	usb_otg_clk = devm_clk_get(&pdev->dev, "ck_usb_otg");
234 235
	if (IS_ERR(usb_otg_clk)) {
		dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
236
		ret = PTR_ERR(usb_otg_clk);
237
		goto fail_otg;
238 239
	}

240 241
	__raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);

242 243 244
	ret = clk_enable(usb_otg_clk);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
J
Jingoo Han 已提交
245
		goto fail_otg;
246
	}
247

248
	isp1301_configure();
249

R
Roland Stigge 已提交
250
	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
251
	if (!hcd) {
252
		dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
253
		ret = -ENOMEM;
254
		goto fail_hcd;
255 256
	}

R
Roland Stigge 已提交
257
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258 259 260
	hcd->regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(hcd->regs)) {
		ret = PTR_ERR(hcd->regs);
261
		goto fail_resource;
262
	}
R
Roland Stigge 已提交
263 264
	hcd->rsrc_start = res->start;
	hcd->rsrc_len = resource_size(res);
265 266 267 268

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		ret = -ENXIO;
269
		goto fail_resource;
270 271
	}

272
	ohci_nxp_start_hc();
273 274 275
	platform_set_drvdata(pdev, hcd);

	dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
Y
Yong Zhang 已提交
276
	ret = usb_add_hcd(hcd, irq, 0);
277 278
	if (ret == 0) {
		device_wakeup_enable(hcd->self.controller);
279
		return ret;
280
	}
281

282
	ohci_nxp_stop_hc();
283
fail_resource:
284
	usb_put_hcd(hcd);
285
fail_hcd:
286
	clk_disable(usb_otg_clk);
287
fail_otg:
288
	clk_disable(usb_dev_clk);
289
fail_rate:
290
	clk_disable(usb_pll_clk);
291
fail_disable:
292
	isp1301_i2c_client = NULL;
293 294 295
	return ret;
}

296
static int ohci_hcd_nxp_remove(struct platform_device *pdev)
297 298 299 300
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);

	usb_remove_hcd(hcd);
301
	ohci_nxp_stop_hc();
302
	usb_put_hcd(hcd);
303 304
	clk_disable(usb_pll_clk);
	clk_disable(usb_dev_clk);
305
	i2c_unregister_device(isp1301_i2c_client);
306
	isp1301_i2c_client = NULL;
307 308 309 310

	return 0;
}

311 312 313
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:usb-ohci");

R
Roland Stigge 已提交
314
#ifdef CONFIG_OF
315
static const struct of_device_id ohci_hcd_nxp_match[] = {
R
Roland Stigge 已提交
316 317 318
	{ .compatible = "nxp,ohci-nxp" },
	{},
};
319
MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
R
Roland Stigge 已提交
320 321
#endif

322
static struct platform_driver ohci_hcd_nxp_driver = {
323 324
	.driver = {
		.name = "usb-ohci",
325
		.owner	= THIS_MODULE,
326
		.of_match_table = of_match_ptr(ohci_hcd_nxp_match),
327
	},
328 329
	.probe = ohci_hcd_nxp_probe,
	.remove = ohci_hcd_nxp_remove,
330 331
};

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static int __init ohci_nxp_init(void)
{
	if (usb_disabled())
		return -ENODEV;

	pr_info("%s: " DRIVER_DESC "\n", hcd_name);

	ohci_init_driver(&ohci_nxp_hc_driver, NULL);
	return platform_driver_register(&ohci_hcd_nxp_driver);
}
module_init(ohci_nxp_init);

static void __exit ohci_nxp_cleanup(void)
{
	platform_driver_unregister(&ohci_hcd_nxp_driver);
}
module_exit(ohci_nxp_cleanup);

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL v2");