kernel/4.3: update to version 4.3.3
[openwrt/staging/stintel.git] / target / linux / ramips / patches-4.3 / 0029-phy-usb-add-ralink-phy.patch
1 From b00b5eafa7e8d059bd0ce844e66f648916953270 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 3 Jan 2016 19:11:22 +0100
4 Subject: [PATCH 2/3] phy: ralink-usb: add driver for Mediatek/Ralink
5
6 Add a driver to setup the USB phy on Mediatek/Ralink SoCs.
7 The driver is trivial and only sets up power and host mode.
8
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 ---
11 .../devicetree/bindings/phy/ralink-usb-phy.txt | 17 ++
12 drivers/phy/Kconfig | 8 +
13 drivers/phy/Makefile | 1 +
14 drivers/phy/phy-ralink-usb.c | 171 ++++++++++++++++++++
15 4 files changed, 197 insertions(+)
16 create mode 100644 Documentation/devicetree/bindings/phy/ralink-usb-phy.txt
17 create mode 100644 drivers/phy/phy-ralink-usb.c
18
19 --- /dev/null
20 +++ b/Documentation/devicetree/bindings/phy/ralink-usb-phy.txt
21 @@ -0,0 +1,17 @@
22 +Mediatek/Ralink USB PHY
23 +
24 +Required properties:
25 + - compatible: ralink,rt3352-usbphy or mediatek,mt7620-usbphy
26 + - #phy-cells: should be 0
27 + - resets: the two reset controllers for host and device
28 + - reset-names: the names of the 2 reset controllers
29 +
30 +Example:
31 +
32 +usbphy: phy {
33 + compatible = "mediatek,mt7620-usbphy";
34 + #phy-cells = <0>;
35 +
36 + resets = <&rstctrl 22 &rstctrl 25>;
37 + reset-names = "host", "device";
38 +};
39 --- a/drivers/phy/Kconfig
40 +++ b/drivers/phy/Kconfig
41 @@ -331,6 +331,14 @@ config PHY_XGENE
42 help
43 This option enables support for APM X-Gene SoC multi-purpose PHY.
44
45 +config PHY_RALINK_USB
46 + tristate "Ralink USB PHY driver"
47 + select GENERIC_PHY
48 + depends on RALINK
49 + help
50 + This option enables support for the Ralink USB PHY found inside
51 + RT3352 and MT7620.
52 +
53 config PHY_STIH407_USB
54 tristate "STMicroelectronics USB2 picoPHY driver for STiH407 family"
55 depends on RESET_CONTROLLER
56 --- a/drivers/phy/Makefile
57 +++ b/drivers/phy/Makefile
58 @@ -46,3 +46,4 @@ obj-$(CONFIG_PHY_QCOM_UFS) += phy-qcom-
59 obj-$(CONFIG_PHY_TUSB1210) += phy-tusb1210.o
60 obj-$(CONFIG_PHY_BRCMSTB_SATA) += phy-brcmstb-sata.o
61 obj-$(CONFIG_PHY_PISTACHIO_USB) += phy-pistachio-usb.o
62 +obj-$(CONFIG_PHY_RALINK_USB) += phy-ralink-usb.o
63 --- /dev/null
64 +++ b/drivers/phy/phy-ralink-usb.c
65 @@ -0,0 +1,175 @@
66 +/*
67 + * Allwinner ralink USB phy driver
68 + *
69 + * Copyright (C) 2016 John Crispin <blogic@openwrt.org>
70 + *
71 + * Based on code from
72 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
73 + *
74 + * This program is free software; you can redistribute it and/or modify
75 + * it under the terms of the GNU General Public License as published by
76 + * the Free Software Foundation; either version 2 of the License, or
77 + * (at your option) any later version.
78 + *
79 + * This program is distributed in the hope that it will be useful,
80 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
81 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82 + * GNU General Public License for more details.
83 + */
84 +
85 +#include <linux/delay.h>
86 +#include <linux/err.h>
87 +#include <linux/io.h>
88 +#include <linux/kernel.h>
89 +#include <linux/module.h>
90 +#include <linux/mutex.h>
91 +#include <linux/phy/phy.h>
92 +#include <linux/platform_device.h>
93 +#include <linux/reset.h>
94 +#include <linux/of_platform.h>
95 +
96 +#include <asm/mach-ralink/ralink_regs.h>
97 +
98 +#define RT_SYSC_REG_SYSCFG1 0x014
99 +#define RT_SYSC_REG_CLKCFG1 0x030
100 +#define RT_SYSC_REG_USB_PHY_CFG 0x05c
101 +
102 +#define RT_RSTCTRL_UDEV BIT(25)
103 +#define RT_RSTCTRL_UHST BIT(22)
104 +#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
105 +
106 +#define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
107 +#define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
108 +#define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
109 +#define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
110 +
111 +#define USB_PHY_UTMI_8B60M BIT(1)
112 +#define UDEV_WAKEUP BIT(0)
113 +
114 +struct ralink_usb_phy {
115 + struct reset_control *rstdev;
116 + struct reset_control *rsthost;
117 + u32 clk;
118 + struct phy *phy;
119 +};
120 +
121 +static int ralink_usb_phy_power_on(struct phy *_phy)
122 +{
123 + struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
124 + u32 t;
125 +
126 + /* enable the phy */
127 + rt_sysc_m32(0, phy->clk, RT_SYSC_REG_CLKCFG1);
128 +
129 + /* setup host mode */
130 + rt_sysc_m32(0, RT_SYSCFG1_USB0_HOST_MODE, RT_SYSC_REG_SYSCFG1);
131 +
132 + /* deassert the reset lines */
133 + reset_control_deassert(phy->rsthost);
134 + reset_control_deassert(phy->rstdev);
135 +
136 + /*
137 + * The SDK kernel had a delay of 100ms. however on device
138 + * testing showed that 10ms is enough
139 + */
140 + mdelay(10);
141 +
142 + /* print some status info */
143 + t = rt_sysc_r32(RT_SYSC_REG_USB_PHY_CFG);
144 + dev_info(&phy->phy->dev, "remote usb device wakeup %s\n",
145 + (t & UDEV_WAKEUP) ? ("enabled") : ("disabled"));
146 + if (t & USB_PHY_UTMI_8B60M)
147 + dev_info(&phy->phy->dev, "UTMI 8bit 60MHz\n");
148 + else
149 + dev_info(&phy->phy->dev, "UTMI 16bit 30MHz\n");
150 +
151 + return 0;
152 +}
153 +
154 +static int ralink_usb_phy_power_off(struct phy *_phy)
155 +{
156 + struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
157 +
158 + /* disable the phy */
159 + rt_sysc_m32(phy->clk, 0, RT_SYSC_REG_CLKCFG1);
160 +
161 + /* assert the reset lines */
162 + reset_control_assert(phy->rstdev);
163 + reset_control_assert(phy->rsthost);
164 +
165 + return 0;
166 +}
167 +
168 +static struct phy_ops ralink_usb_phy_ops = {
169 + .power_on = ralink_usb_phy_power_on,
170 + .power_off = ralink_usb_phy_power_off,
171 + .owner = THIS_MODULE,
172 +};
173 +
174 +static const struct of_device_id ralink_usb_phy_of_match[] = {
175 + {
176 + .compatible = "ralink,rt3352-usbphy",
177 + .data = (void *) (RT_CLKCFG1_UPHY1_CLK_EN |
178 + RT_CLKCFG1_UPHY0_CLK_EN)
179 + },
180 + {
181 + .compatible = "mediatek,mt7620-usbphy",
182 + .data = (void *) (MT7620_CLKCFG1_UPHY1_CLK_EN |
183 + MT7620_CLKCFG1_UPHY0_CLK_EN) },
184 + { },
185 +};
186 +MODULE_DEVICE_TABLE(of, ralink_usb_phy_of_match);
187 +
188 +static int ralink_usb_phy_probe(struct platform_device *pdev)
189 +{
190 + struct device *dev = &pdev->dev;
191 + struct phy_provider *phy_provider;
192 + const struct of_device_id *match;
193 + struct ralink_usb_phy *phy;
194 +
195 + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
196 + if (!phy)
197 + return -ENOMEM;
198 +
199 + match = of_match_device(ralink_usb_phy_of_match, &pdev->dev);
200 + if (!match)
201 + return -ENODEV;
202 +
203 + phy->clk = (int) match->data;
204 +
205 + phy->rsthost = devm_reset_control_get(&pdev->dev, "host");
206 + if (IS_ERR(phy->rsthost)) {
207 + dev_err(dev, "host reset is missing\n");
208 + return PTR_ERR(phy->rsthost);
209 + }
210 +
211 + phy->rstdev = devm_reset_control_get(&pdev->dev, "device");
212 + if (IS_ERR(phy->rstdev)) {
213 + dev_err(dev, "device reset is missing\n");
214 + return PTR_ERR(phy->rstdev);
215 + }
216 +
217 + phy->phy = devm_phy_create(dev, NULL, &ralink_usb_phy_ops);
218 + if (IS_ERR(phy->phy)) {
219 + dev_err(dev, "failed to create PHY\n");
220 + return PTR_ERR(phy->phy);
221 + }
222 + phy_set_drvdata(phy->phy, phy);
223 +
224 + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
225 +
226 + return PTR_ERR_OR_ZERO(phy_provider);
227 +}
228 +
229 +static struct platform_driver ralink_usb_phy_driver = {
230 + .probe = ralink_usb_phy_probe,
231 + .driver = {
232 + .of_match_table = ralink_usb_phy_of_match,
233 + .name = "ralink-usb-phy",
234 + }
235 +};
236 +module_platform_driver(ralink_usb_phy_driver);
237 +
238 +MODULE_DESCRIPTION("Ralink USB phy driver");
239 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
240 +MODULE_LICENSE("GPL v2");