Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 3 | * Copyright (C) 2012 Marek Vasut <marex@denx.de> |
| 4 | * on behalf of DENX Software Engineering GmbH |
| 5 | * |
| 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: |
| 9 | * |
| 10 | * http://www.opensource.org/licenses/gpl-license.html |
| 11 | * http://www.gnu.org/copyleft/gpl.html |
| 12 | */ |
| 13 | |
| 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> |
| 22 | #include <linux/io.h> |
| 23 | |
| 24 | #define DRIVER_NAME "mxs_phy" |
| 25 | |
| 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 |
| 30 | |
| 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) |
| 36 | |
| 37 | struct mxs_phy { |
| 38 | struct usb_phy phy; |
| 39 | struct clk *clk; |
| 40 | }; |
| 41 | |
| 42 | #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy) |
| 43 | |
Fabio Estevam | 51e563e | 2013-07-03 16:34:13 -0300 | [diff] [blame] | 44 | static int mxs_phy_hw_init(struct mxs_phy *mxs_phy) |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 45 | { |
Fabio Estevam | 51e563e | 2013-07-03 16:34:13 -0300 | [diff] [blame] | 46 | int ret; |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 47 | void __iomem *base = mxs_phy->phy.io_priv; |
| 48 | |
Fabio Estevam | 51e563e | 2013-07-03 16:34:13 -0300 | [diff] [blame] | 49 | ret = stmp_reset_block(base + HW_USBPHY_CTRL); |
| 50 | if (ret) |
| 51 | return ret; |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 52 | |
| 53 | /* Power up the PHY */ |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 54 | writel(0, base + HW_USBPHY_PWD); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 55 | |
| 56 | /* enable FS/LS device */ |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 57 | writel(BM_USBPHY_CTRL_ENUTMILEVEL2 | |
| 58 | BM_USBPHY_CTRL_ENUTMILEVEL3, |
| 59 | base + HW_USBPHY_CTRL_SET); |
Fabio Estevam | 51e563e | 2013-07-03 16:34:13 -0300 | [diff] [blame] | 60 | |
| 61 | return 0; |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int mxs_phy_init(struct usb_phy *phy) |
| 65 | { |
| 66 | struct mxs_phy *mxs_phy = to_mxs_phy(phy); |
| 67 | |
| 68 | clk_prepare_enable(mxs_phy->clk); |
Fabio Estevam | 51e563e | 2013-07-03 16:34:13 -0300 | [diff] [blame] | 69 | return mxs_phy_hw_init(mxs_phy); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void mxs_phy_shutdown(struct usb_phy *phy) |
| 73 | { |
| 74 | struct mxs_phy *mxs_phy = to_mxs_phy(phy); |
| 75 | |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 76 | writel(BM_USBPHY_CTRL_CLKGATE, |
| 77 | phy->io_priv + HW_USBPHY_CTRL_SET); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 78 | |
| 79 | clk_disable_unprepare(mxs_phy->clk); |
| 80 | } |
| 81 | |
Peter Chen | 04a6221 | 2013-01-10 16:35:53 +0800 | [diff] [blame] | 82 | static int mxs_phy_suspend(struct usb_phy *x, int suspend) |
| 83 | { |
| 84 | struct mxs_phy *mxs_phy = to_mxs_phy(x); |
| 85 | |
| 86 | if (suspend) { |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 87 | writel(0xffffffff, x->io_priv + HW_USBPHY_PWD); |
| 88 | writel(BM_USBPHY_CTRL_CLKGATE, |
| 89 | x->io_priv + HW_USBPHY_CTRL_SET); |
Peter Chen | 04a6221 | 2013-01-10 16:35:53 +0800 | [diff] [blame] | 90 | clk_disable_unprepare(mxs_phy->clk); |
| 91 | } else { |
| 92 | clk_prepare_enable(mxs_phy->clk); |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 93 | writel(BM_USBPHY_CTRL_CLKGATE, |
| 94 | x->io_priv + HW_USBPHY_CTRL_CLR); |
| 95 | writel(0, x->io_priv + HW_USBPHY_PWD); |
Peter Chen | 04a6221 | 2013-01-10 16:35:53 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 101 | static int mxs_phy_on_connect(struct usb_phy *phy, |
| 102 | enum usb_device_speed speed) |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 103 | { |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 104 | dev_dbg(phy->dev, "%s speed device has connected\n", |
| 105 | (speed == USB_SPEED_HIGH) ? "high" : "non-high"); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 106 | |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 107 | if (speed == USB_SPEED_HIGH) |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 108 | writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT, |
| 109 | phy->io_priv + HW_USBPHY_CTRL_SET); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 114 | static int mxs_phy_on_disconnect(struct usb_phy *phy, |
| 115 | enum usb_device_speed speed) |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 116 | { |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 117 | dev_dbg(phy->dev, "%s speed device has disconnected\n", |
| 118 | (speed == USB_SPEED_HIGH) ? "high" : "non-high"); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 119 | |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 120 | if (speed == USB_SPEED_HIGH) |
Marc Kleine-Budde | b5a726b | 2013-02-28 11:52:30 +0100 | [diff] [blame] | 121 | writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT, |
| 122 | phy->io_priv + HW_USBPHY_CTRL_CLR); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int mxs_phy_probe(struct platform_device *pdev) |
| 128 | { |
| 129 | struct resource *res; |
| 130 | void __iomem *base; |
| 131 | struct clk *clk; |
| 132 | struct mxs_phy *mxs_phy; |
Sascha Hauer | 25df639 | 2013-02-27 15:16:30 +0100 | [diff] [blame] | 133 | int ret; |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 134 | |
| 135 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 148e113 | 2013-01-21 11:09:22 +0100 | [diff] [blame] | 136 | base = devm_ioremap_resource(&pdev->dev, res); |
| 137 | if (IS_ERR(base)) |
| 138 | return PTR_ERR(base); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 139 | |
| 140 | clk = devm_clk_get(&pdev->dev, NULL); |
| 141 | if (IS_ERR(clk)) { |
| 142 | dev_err(&pdev->dev, |
| 143 | "can't get the clock, err=%ld", PTR_ERR(clk)); |
| 144 | return PTR_ERR(clk); |
| 145 | } |
| 146 | |
| 147 | mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL); |
| 148 | if (!mxs_phy) { |
| 149 | dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n"); |
| 150 | return -ENOMEM; |
| 151 | } |
| 152 | |
| 153 | mxs_phy->phy.io_priv = base; |
| 154 | mxs_phy->phy.dev = &pdev->dev; |
| 155 | mxs_phy->phy.label = DRIVER_NAME; |
| 156 | mxs_phy->phy.init = mxs_phy_init; |
| 157 | mxs_phy->phy.shutdown = mxs_phy_shutdown; |
Peter Chen | 04a6221 | 2013-01-10 16:35:53 +0800 | [diff] [blame] | 158 | mxs_phy->phy.set_suspend = mxs_phy_suspend; |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 159 | mxs_phy->phy.notify_connect = mxs_phy_on_connect; |
| 160 | mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect; |
Michael Grzeschik | 4e0aa63 | 2013-05-15 15:03:14 +0200 | [diff] [blame] | 161 | mxs_phy->phy.type = USB_PHY_TYPE_USB2; |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 162 | |
| 163 | ATOMIC_INIT_NOTIFIER_HEAD(&mxs_phy->phy.notifier); |
| 164 | |
| 165 | mxs_phy->clk = clk; |
| 166 | |
Jisheng Zhang | 97a27f7 | 2013-11-07 10:55:49 +0800 | [diff] [blame] | 167 | platform_set_drvdata(pdev, mxs_phy); |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 168 | |
Sascha Hauer | 25df639 | 2013-02-27 15:16:30 +0100 | [diff] [blame] | 169 | ret = usb_add_phy_dev(&mxs_phy->phy); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 176 | static int mxs_phy_remove(struct platform_device *pdev) |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 177 | { |
Sascha Hauer | 25df639 | 2013-02-27 15:16:30 +0100 | [diff] [blame] | 178 | struct mxs_phy *mxs_phy = platform_get_drvdata(pdev); |
| 179 | |
| 180 | usb_remove_phy(&mxs_phy->phy); |
| 181 | |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static const struct of_device_id mxs_phy_dt_ids[] = { |
| 186 | { .compatible = "fsl,imx23-usbphy", }, |
| 187 | { /* sentinel */ } |
| 188 | }; |
| 189 | MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids); |
| 190 | |
| 191 | static struct platform_driver mxs_phy_driver = { |
| 192 | .probe = mxs_phy_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 193 | .remove = mxs_phy_remove, |
Richard Zhao | b3d9968 | 2012-07-07 22:56:47 +0800 | [diff] [blame] | 194 | .driver = { |
| 195 | .name = DRIVER_NAME, |
| 196 | .owner = THIS_MODULE, |
| 197 | .of_match_table = mxs_phy_dt_ids, |
| 198 | }, |
| 199 | }; |
| 200 | |
| 201 | static int __init mxs_phy_module_init(void) |
| 202 | { |
| 203 | return platform_driver_register(&mxs_phy_driver); |
| 204 | } |
| 205 | postcore_initcall(mxs_phy_module_init); |
| 206 | |
| 207 | static void __exit mxs_phy_module_exit(void) |
| 208 | { |
| 209 | platform_driver_unregister(&mxs_phy_driver); |
| 210 | } |
| 211 | module_exit(mxs_phy_module_exit); |
| 212 | |
| 213 | MODULE_ALIAS("platform:mxs-usb-phy"); |
| 214 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
| 215 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); |
| 216 | MODULE_DESCRIPTION("Freescale MXS USB PHY driver"); |
| 217 | MODULE_LICENSE("GPL"); |