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
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.
9 Signed-off-by: John Crispin <blogic@openwrt.org>
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
20 +++ b/Documentation/devicetree/bindings/phy/ralink-usb-phy.txt
22 +Mediatek/Ralink USB PHY
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
33 + compatible = "mediatek,mt7620-usbphy";
36 + resets = <&rstctrl 22 &rstctrl 25>;
37 + reset-names = "host", "device";
39 --- a/drivers/phy/Kconfig
40 +++ b/drivers/phy/Kconfig
43 This option enables support for APM X-Gene SoC multi-purpose PHY.
45 +config PHY_RALINK_USB
46 + tristate "Ralink USB PHY driver"
50 + This option enables support for the Ralink USB PHY found inside
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
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
64 +++ b/drivers/phy/phy-ralink-usb.c
67 + * Allwinner ralink USB phy driver
69 + * Copyright (C) 2016 John Crispin <blogic@openwrt.org>
71 + * Based on code from
72 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
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.
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.
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>
96 +#include <asm/mach-ralink/ralink_regs.h>
98 +#define RT_SYSC_REG_SYSCFG1 0x014
99 +#define RT_SYSC_REG_CLKCFG1 0x030
100 +#define RT_SYSC_REG_USB_PHY_CFG 0x05c
102 +#define RT_RSTCTRL_UDEV BIT(25)
103 +#define RT_RSTCTRL_UHST BIT(22)
104 +#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
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)
111 +#define USB_PHY_UTMI_8B60M BIT(1)
112 +#define UDEV_WAKEUP BIT(0)
114 +struct ralink_usb_phy {
115 + struct reset_control *rstdev;
116 + struct reset_control *rsthost;
121 +static int ralink_usb_phy_power_on(struct phy *_phy)
123 + struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
126 + /* enable the phy */
127 + rt_sysc_m32(0, phy->clk, RT_SYSC_REG_CLKCFG1);
129 + /* setup host mode */
130 + rt_sysc_m32(0, RT_SYSCFG1_USB0_HOST_MODE, RT_SYSC_REG_SYSCFG1);
132 + /* deassert the reset lines */
133 + reset_control_deassert(phy->rsthost);
134 + reset_control_deassert(phy->rstdev);
137 + * The SDK kernel had a delay of 100ms. however on device
138 + * testing showed that 10ms is enough
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");
149 + dev_info(&phy->phy->dev, "UTMI 16bit 30MHz\n");
154 +static int ralink_usb_phy_power_off(struct phy *_phy)
156 + struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
158 + /* disable the phy */
159 + rt_sysc_m32(phy->clk, 0, RT_SYSC_REG_CLKCFG1);
161 + /* assert the reset lines */
162 + reset_control_assert(phy->rstdev);
163 + reset_control_assert(phy->rsthost);
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,
174 +static const struct of_device_id ralink_usb_phy_of_match[] = {
176 + .compatible = "ralink,rt3352-usbphy",
177 + .data = (void *) (RT_CLKCFG1_UPHY1_CLK_EN |
178 + RT_CLKCFG1_UPHY0_CLK_EN)
181 + .compatible = "mediatek,mt7620-usbphy",
182 + .data = (void *) (MT7620_CLKCFG1_UPHY1_CLK_EN |
183 + MT7620_CLKCFG1_UPHY0_CLK_EN) },
186 +MODULE_DEVICE_TABLE(of, ralink_usb_phy_of_match);
188 +static int ralink_usb_phy_probe(struct platform_device *pdev)
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;
195 + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
199 + match = of_match_device(ralink_usb_phy_of_match, &pdev->dev);
203 + phy->clk = (int) match->data;
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);
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);
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);
222 + phy_set_drvdata(phy->phy, phy);
224 + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
226 + return PTR_ERR_OR_ZERO(phy_provider);
229 +static struct platform_driver ralink_usb_phy_driver = {
230 + .probe = ralink_usb_phy_probe,
232 + .of_match_table = ralink_usb_phy_of_match,
233 + .name = "ralink-usb-phy",
236 +module_platform_driver(ralink_usb_phy_driver);
238 +MODULE_DESCRIPTION("Ralink USB phy driver");
239 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
240 +MODULE_LICENSE("GPL v2");