2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4 * on behalf of DENX Software Engineering GmbH
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/clk.h>
18 #include <linux/usb/otg.h>
19 #include <linux/stmp_device.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
24 #define DRIVER_NAME "mxs_phy"
26 #define HW_USBPHY_PWD 0x00
27 #define HW_USBPHY_CTRL 0x30
28 #define HW_USBPHY_CTRL_SET 0x34
29 #define HW_USBPHY_CTRL_CLR 0x38
31 #define BM_USBPHY_CTRL_SFTRST BIT(31)
32 #define BM_USBPHY_CTRL_CLKGATE BIT(30)
33 #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
34 #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
35 #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
42 #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
44 static void mxs_phy_hw_init(struct mxs_phy *mxs_phy)
46 void __iomem *base = mxs_phy->phy.io_priv;
48 stmp_reset_block(base + HW_USBPHY_CTRL);
50 /* Power up the PHY */
51 writel_relaxed(0, base + HW_USBPHY_PWD);
53 /* enable FS/LS device */
54 writel_relaxed(BM_USBPHY_CTRL_ENUTMILEVEL2 |
55 BM_USBPHY_CTRL_ENUTMILEVEL3,
56 base + HW_USBPHY_CTRL_SET);
59 static int mxs_phy_init(struct usb_phy *phy)
61 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
63 clk_prepare_enable(mxs_phy->clk);
64 mxs_phy_hw_init(mxs_phy);
69 static void mxs_phy_shutdown(struct usb_phy *phy)
71 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
73 writel_relaxed(BM_USBPHY_CTRL_CLKGATE,
74 phy->io_priv + HW_USBPHY_CTRL_SET);
76 clk_disable_unprepare(mxs_phy->clk);
79 static int mxs_phy_on_connect(struct usb_phy *phy, int port)
81 dev_dbg(phy->dev, "Connect on port %d\n", port);
83 mxs_phy_hw_init(to_mxs_phy(phy));
84 writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
85 phy->io_priv + HW_USBPHY_CTRL_SET);
90 static int mxs_phy_on_disconnect(struct usb_phy *phy, int port)
92 dev_dbg(phy->dev, "Disconnect on port %d\n", port);
94 writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
95 phy->io_priv + HW_USBPHY_CTRL_CLR);
100 static int mxs_phy_probe(struct platform_device *pdev)
102 struct resource *res;
105 struct mxs_phy *mxs_phy;
107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
109 dev_err(&pdev->dev, "can't get device resources\n");
113 base = devm_request_and_ioremap(&pdev->dev, res);
117 clk = devm_clk_get(&pdev->dev, NULL);
120 "can't get the clock, err=%ld", PTR_ERR(clk));
124 mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
126 dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
130 mxs_phy->phy.io_priv = base;
131 mxs_phy->phy.dev = &pdev->dev;
132 mxs_phy->phy.label = DRIVER_NAME;
133 mxs_phy->phy.init = mxs_phy_init;
134 mxs_phy->phy.shutdown = mxs_phy_shutdown;
135 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
136 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
138 ATOMIC_INIT_NOTIFIER_HEAD(&mxs_phy->phy.notifier);
142 platform_set_drvdata(pdev, &mxs_phy->phy);
147 static int __devexit mxs_phy_remove(struct platform_device *pdev)
149 platform_set_drvdata(pdev, NULL);
154 static const struct of_device_id mxs_phy_dt_ids[] = {
155 { .compatible = "fsl,imx23-usbphy", },
158 MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
160 static struct platform_driver mxs_phy_driver = {
161 .probe = mxs_phy_probe,
162 .remove = __devexit_p(mxs_phy_remove),
165 .owner = THIS_MODULE,
166 .of_match_table = mxs_phy_dt_ids,
170 static int __init mxs_phy_module_init(void)
172 return platform_driver_register(&mxs_phy_driver);
174 postcore_initcall(mxs_phy_module_init);
176 static void __exit mxs_phy_module_exit(void)
178 platform_driver_unregister(&mxs_phy_driver);
180 module_exit(mxs_phy_module_exit);
182 MODULE_ALIAS("platform:mxs-usb-phy");
183 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
184 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
185 MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
186 MODULE_LICENSE("GPL");