ehci-mxc.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

20 21 22
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/io.h>
23 24 25 26
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/usb/otg.h>
27
#include <linux/usb/ulpi.h>
28
#include <linux/slab.h>
29 30
#include <linux/usb.h>
#include <linux/usb/hcd.h>
31

32
#include <linux/platform_data/usb-ehci-mxc.h>
33

34 35
#include <asm/mach-types.h>

36 37 38 39 40 41
#include "ehci.h"

#define DRIVER_DESC "Freescale On-Chip EHCI Host driver"

static const char hcd_name[] = "ehci-mxc";

42 43 44
#define ULPI_VIEWPORT_OFFSET	0x170

struct ehci_mxc_priv {
45
	struct clk *usbclk, *ahbclk, *phyclk;
46 47
};

48
static struct hc_driver __read_mostly ehci_mxc_hc_driver;
49

50 51
static const struct ehci_driver_overrides ehci_mxc_overrides __initdata = {
	.extra_priv_size =	sizeof(struct ehci_mxc_priv),
52 53 54 55 56 57 58
};

static int ehci_mxc_drv_probe(struct platform_device *pdev)
{
	struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
	struct usb_hcd *hcd;
	struct resource *res;
U
Uwe Kleine-König 已提交
59
	int irq, ret;
60
	unsigned int flags;
61 62
	struct ehci_mxc_priv *priv;
	struct device *dev = &pdev->dev;
63
	struct ehci_hcd *ehci;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	dev_info(&pdev->dev, "initializing i.MX USB Controller\n");

	if (!pdata) {
		dev_err(dev, "No platform data given, bailing out.\n");
		return -EINVAL;
	}

	irq = platform_get_irq(pdev, 0);

	hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
	if (!hcd)
		return -ENOMEM;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(dev, "Found HC with no register addr. Check setup!\n");
		ret = -ENODEV;
82
		goto err_alloc;
83 84 85 86 87
	}

	hcd->rsrc_start = res->start;
	hcd->rsrc_len = resource_size(res);

88
	hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
89 90 91
	if (!hcd->regs) {
		dev_err(dev, "error mapping memory\n");
		ret = -EFAULT;
92
		goto err_alloc;
93 94
	}

95 96 97 98
	hcd->has_tt = 1;
	ehci = hcd_to_ehci(hcd);
	priv = (struct ehci_mxc_priv *) ehci->priv;

99
	/* enable clocks */
100
	priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
101 102
	if (IS_ERR(priv->usbclk)) {
		ret = PTR_ERR(priv->usbclk);
103
		goto err_alloc;
104
	}
105
	clk_prepare_enable(priv->usbclk);
106

107
	priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
108 109 110
	if (IS_ERR(priv->ahbclk)) {
		ret = PTR_ERR(priv->ahbclk);
		goto err_clk_ahb;
111
	}
112
	clk_prepare_enable(priv->ahbclk);
113

114
	/* "dr" device has its own clock on i.MX51 */
115
	priv->phyclk = devm_clk_get(&pdev->dev, "phy");
116 117 118 119
	if (IS_ERR(priv->phyclk))
		priv->phyclk = NULL;
	if (priv->phyclk)
		clk_prepare_enable(priv->phyclk);
120 121 122 123 124 125 126 127 128 129 130 131 132


	/* call platform specific init function */
	if (pdata->init) {
		ret = pdata->init(pdev);
		if (ret) {
			dev_err(dev, "platform init failed\n");
			goto err_init;
		}
		/* platforms need some time to settle changed IO settings */
		mdelay(10);
	}

133 134 135
	/* EHCI registers start at offset 0x100 */
	ehci->caps = hcd->regs + 0x100;
	ehci->regs = hcd->regs + 0x100 +
136
		HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
137 138 139 140 141 142 143

	/* set up the PORTSCx register */
	ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);

	/* is this really needed? */
	msleep(10);

144 145 146
	/* Initialize the transceiver */
	if (pdata->otg) {
		pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
147
		ret = usb_phy_init(pdata->otg);
148 149 150 151 152
		if (ret) {
			dev_err(dev, "unable to init transceiver, probably missing\n");
			ret = -ENODEV;
			goto err_add;
		}
153
		ret = otg_set_vbus(pdata->otg->otg, 1);
154
		if (ret) {
155
			dev_err(dev, "unable to enable vbus on transceiver\n");
156 157
			goto err_add;
		}
158 159
	}

160
	platform_set_drvdata(pdev, hcd);
161

Y
Yong Zhang 已提交
162
	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
163 164 165
	if (ret)
		goto err_add;

166 167 168 169 170 171 172
	if (pdata->otg) {
		/*
		 * efikamx and efikasb have some hardware bug which is
		 * preventing usb to work unless CHRGVBUS is set.
		 * It's in violation of USB specs
		 */
		if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
173 174
			flags = usb_phy_io_read(pdata->otg,
							ULPI_OTG_CTRL);
175
			flags |= ULPI_OTG_CTRL_CHRGVBUS;
176 177
			ret = usb_phy_io_write(pdata->otg, flags,
							ULPI_OTG_CTRL);
178 179 180 181 182 183 184
			if (ret) {
				dev_err(dev, "unable to set CHRVBUS\n");
				goto err_add;
			}
		}
	}

185 186 187 188 189 190
	return 0;

err_add:
	if (pdata && pdata->exit)
		pdata->exit(pdev);
err_init:
191
	if (priv->phyclk)
192 193 194
		clk_disable_unprepare(priv->phyclk);

	clk_disable_unprepare(priv->ahbclk);
195
err_clk_ahb:
196
	clk_disable_unprepare(priv->usbclk);
197 198 199 200 201 202 203 204
err_alloc:
	usb_put_hcd(hcd);
	return ret;
}

static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
{
	struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
205 206 207 208 209
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;

	usb_remove_hcd(hcd);
210 211 212 213 214

	if (pdata && pdata->exit)
		pdata->exit(pdev);

	if (pdata->otg)
215
		usb_phy_shutdown(pdata->otg);
216

217
	clk_disable_unprepare(priv->usbclk);
218 219
	clk_disable_unprepare(priv->ahbclk);

220
	if (priv->phyclk)
221
		clk_disable_unprepare(priv->phyclk);
222

223 224
	usb_put_hcd(hcd);
	platform_set_drvdata(pdev, NULL);
225 226 227 228 229
	return 0;
}

static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
{
230
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
231 232 233 234 235 236 237 238 239

	if (hcd->driver->shutdown)
		hcd->driver->shutdown(hcd);
}

MODULE_ALIAS("platform:mxc-ehci");

static struct platform_driver ehci_mxc_driver = {
	.probe = ehci_mxc_drv_probe,
240
	.remove = ehci_mxc_drv_remove,
241 242 243 244 245
	.shutdown = ehci_mxc_drv_shutdown,
	.driver = {
		   .name = "mxc-ehci",
	},
};
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

static int __init ehci_mxc_init(void)
{
	if (usb_disabled())
		return -ENODEV;

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

	ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
	return platform_driver_register(&ehci_mxc_driver);
}
module_init(ehci_mxc_init);

static void __exit ehci_mxc_cleanup(void)
{
	platform_driver_unregister(&ehci_mxc_driver);
}
module_exit(ehci_mxc_cleanup);

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR("Sascha Hauer");
MODULE_LICENSE("GPL");