Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2017 John Crispin <john@phrozen.org>
4 *
5 * Based on code from
6 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
7 */
8
9#include <linux/delay.h>
10#include <linux/err.h>
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <linux/mfd/syscon.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/of_platform.h>
17#include <linux/phy/phy.h>
18#include <linux/platform_device.h>
19#include <linux/regmap.h>
20#include <linux/reset.h>
21
22#define RT_SYSC_REG_SYSCFG1 0x014
23#define RT_SYSC_REG_CLKCFG1 0x030
24#define RT_SYSC_REG_USB_PHY_CFG 0x05c
25
26#define OFS_U2_PHY_AC0 0x800
27#define OFS_U2_PHY_AC1 0x804
28#define OFS_U2_PHY_AC2 0x808
29#define OFS_U2_PHY_ACR0 0x810
30#define OFS_U2_PHY_ACR1 0x814
31#define OFS_U2_PHY_ACR2 0x818
32#define OFS_U2_PHY_ACR3 0x81C
33#define OFS_U2_PHY_ACR4 0x820
34#define OFS_U2_PHY_AMON0 0x824
35#define OFS_U2_PHY_DCR0 0x860
36#define OFS_U2_PHY_DCR1 0x864
37#define OFS_U2_PHY_DTM0 0x868
38#define OFS_U2_PHY_DTM1 0x86C
39
40#define RT_RSTCTRL_UDEV BIT(25)
41#define RT_RSTCTRL_UHST BIT(22)
42#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
43
44#define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
45#define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
46#define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
47#define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
48
49#define USB_PHY_UTMI_8B60M BIT(1)
50#define UDEV_WAKEUP BIT(0)
51
52struct ralink_usb_phy {
53 struct reset_control *rstdev;
54 struct reset_control *rsthost;
55 u32 clk;
56 struct phy *phy;
57 void __iomem *base;
58 struct regmap *sysctl;
59};
60
61static void u2_phy_w32(struct ralink_usb_phy *phy, u32 val, u32 reg)
62{
63 writel(val, phy->base + reg);
64}
65
66static u32 u2_phy_r32(struct ralink_usb_phy *phy, u32 reg)
67{
68 return readl(phy->base + reg);
69}
70
71static void ralink_usb_phy_init(struct ralink_usb_phy *phy)
72{
73 u2_phy_r32(phy, OFS_U2_PHY_AC2);
74 u2_phy_r32(phy, OFS_U2_PHY_ACR0);
75 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
76
77 u2_phy_w32(phy, 0x00ffff02, OFS_U2_PHY_DCR0);
78 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
79 u2_phy_w32(phy, 0x00555502, OFS_U2_PHY_DCR0);
80 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
81 u2_phy_w32(phy, 0x00aaaa02, OFS_U2_PHY_DCR0);
82 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
83 u2_phy_w32(phy, 0x00000402, OFS_U2_PHY_DCR0);
84 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
85 u2_phy_w32(phy, 0x0048086a, OFS_U2_PHY_AC0);
86 u2_phy_w32(phy, 0x4400001c, OFS_U2_PHY_AC1);
87 u2_phy_w32(phy, 0xc0200000, OFS_U2_PHY_ACR3);
88 u2_phy_w32(phy, 0x02000000, OFS_U2_PHY_DTM0);
89}
90
91static int ralink_usb_phy_power_on(struct phy *_phy)
92{
93 struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
94 u32 t;
95
96 /* enable the phy */
97 regmap_update_bits(phy->sysctl, RT_SYSC_REG_CLKCFG1,
98 phy->clk, phy->clk);
99
100 /* setup host mode */
101 regmap_update_bits(phy->sysctl, RT_SYSC_REG_SYSCFG1,
102 RT_SYSCFG1_USB0_HOST_MODE,
103 RT_SYSCFG1_USB0_HOST_MODE);
104
105 /* deassert the reset lines */
106 reset_control_deassert(phy->rsthost);
107 reset_control_deassert(phy->rstdev);
108
109 /*
110 * The SDK kernel had a delay of 100ms. however on device
111 * testing showed that 10ms is enough
112 */
113 mdelay(10);
114
115 if (phy->base)
116 ralink_usb_phy_init(phy);
117
118 /* print some status info */
119 regmap_read(phy->sysctl, RT_SYSC_REG_USB_PHY_CFG, &t);
120 dev_info(&phy->phy->dev, "remote usb device wakeup %s\n",
121 (t & UDEV_WAKEUP) ? ("enabled") : ("disabled"));
122 if (t & USB_PHY_UTMI_8B60M)
123 dev_info(&phy->phy->dev, "UTMI 8bit 60MHz\n");
124 else
125 dev_info(&phy->phy->dev, "UTMI 16bit 30MHz\n");
126
127 return 0;
128}
129
130static int ralink_usb_phy_power_off(struct phy *_phy)
131{
132 struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
133
134 /* disable the phy */
135 regmap_update_bits(phy->sysctl, RT_SYSC_REG_CLKCFG1,
136 phy->clk, 0);
137
138 /* assert the reset lines */
139 reset_control_assert(phy->rstdev);
140 reset_control_assert(phy->rsthost);
141
142 return 0;
143}
144
145static struct phy_ops ralink_usb_phy_ops = {
146 .power_on = ralink_usb_phy_power_on,
147 .power_off = ralink_usb_phy_power_off,
148 .owner = THIS_MODULE,
149};
150
151static const struct of_device_id ralink_usb_phy_of_match[] = {
152 {
153 .compatible = "ralink,rt3352-usbphy",
154 .data = (void *)(uintptr_t)(RT_CLKCFG1_UPHY1_CLK_EN |
155 RT_CLKCFG1_UPHY0_CLK_EN)
156 },
157 {
158 .compatible = "mediatek,mt7620-usbphy",
159 .data = (void *)(uintptr_t)(MT7620_CLKCFG1_UPHY1_CLK_EN |
160 MT7620_CLKCFG1_UPHY0_CLK_EN)
161 },
162 {
163 .compatible = "mediatek,mt7628-usbphy",
164 .data = (void *)(uintptr_t)(MT7620_CLKCFG1_UPHY1_CLK_EN |
165 MT7620_CLKCFG1_UPHY0_CLK_EN) },
166 { },
167};
168MODULE_DEVICE_TABLE(of, ralink_usb_phy_of_match);
169
170static int ralink_usb_phy_probe(struct platform_device *pdev)
171{
172 struct device *dev = &pdev->dev;
173 struct resource *res;
174 struct phy_provider *phy_provider;
175 const struct of_device_id *match;
176 struct ralink_usb_phy *phy;
177
178 match = of_match_device(ralink_usb_phy_of_match, &pdev->dev);
179 if (!match)
180 return -ENODEV;
181
182 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
183 if (!phy)
184 return -ENOMEM;
185
186 phy->clk = (uintptr_t)match->data;
187 phy->base = NULL;
188
189 phy->sysctl = syscon_regmap_lookup_by_phandle(dev->of_node, "ralink,sysctl");
190 if (IS_ERR(phy->sysctl)) {
191 dev_err(dev, "failed to get sysctl registers\n");
192 return PTR_ERR(phy->sysctl);
193 }
194
195 /* The MT7628 and MT7688 require extra setup of PHY registers. */
196 if (of_device_is_compatible(dev->of_node, "mediatek,mt7628-usbphy")) {
197 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
198 phy->base = devm_ioremap_resource(&pdev->dev, res);
199 if (IS_ERR(phy->base)) {
200 dev_err(dev, "failed to remap register memory\n");
201 return PTR_ERR(phy->base);
202 }
203 }
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
229static 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};
236module_platform_driver(ralink_usb_phy_driver);
237
238MODULE_DESCRIPTION("Ralink USB phy driver");
239MODULE_AUTHOR("John Crispin <john@phrozen.org>");
240MODULE_LICENSE("GPL v2");
1/*
2 * Copyright (C) 2017 John Crispin <john@phrozen.org>
3 *
4 * Based on code from
5 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/mfd/syscon.h>
23#include <linux/module.h>
24#include <linux/mutex.h>
25#include <linux/of_platform.h>
26#include <linux/phy/phy.h>
27#include <linux/platform_device.h>
28#include <linux/regmap.h>
29#include <linux/reset.h>
30
31#define RT_SYSC_REG_SYSCFG1 0x014
32#define RT_SYSC_REG_CLKCFG1 0x030
33#define RT_SYSC_REG_USB_PHY_CFG 0x05c
34
35#define OFS_U2_PHY_AC0 0x800
36#define OFS_U2_PHY_AC1 0x804
37#define OFS_U2_PHY_AC2 0x808
38#define OFS_U2_PHY_ACR0 0x810
39#define OFS_U2_PHY_ACR1 0x814
40#define OFS_U2_PHY_ACR2 0x818
41#define OFS_U2_PHY_ACR3 0x81C
42#define OFS_U2_PHY_ACR4 0x820
43#define OFS_U2_PHY_AMON0 0x824
44#define OFS_U2_PHY_DCR0 0x860
45#define OFS_U2_PHY_DCR1 0x864
46#define OFS_U2_PHY_DTM0 0x868
47#define OFS_U2_PHY_DTM1 0x86C
48
49#define RT_RSTCTRL_UDEV BIT(25)
50#define RT_RSTCTRL_UHST BIT(22)
51#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
52
53#define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
54#define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
55#define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
56#define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
57
58#define USB_PHY_UTMI_8B60M BIT(1)
59#define UDEV_WAKEUP BIT(0)
60
61struct ralink_usb_phy {
62 struct reset_control *rstdev;
63 struct reset_control *rsthost;
64 u32 clk;
65 struct phy *phy;
66 void __iomem *base;
67 struct regmap *sysctl;
68};
69
70static void u2_phy_w32(struct ralink_usb_phy *phy, u32 val, u32 reg)
71{
72 writel(val, phy->base + reg);
73}
74
75static u32 u2_phy_r32(struct ralink_usb_phy *phy, u32 reg)
76{
77 return readl(phy->base + reg);
78}
79
80static void ralink_usb_phy_init(struct ralink_usb_phy *phy)
81{
82 u2_phy_r32(phy, OFS_U2_PHY_AC2);
83 u2_phy_r32(phy, OFS_U2_PHY_ACR0);
84 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
85
86 u2_phy_w32(phy, 0x00ffff02, OFS_U2_PHY_DCR0);
87 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
88 u2_phy_w32(phy, 0x00555502, OFS_U2_PHY_DCR0);
89 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
90 u2_phy_w32(phy, 0x00aaaa02, OFS_U2_PHY_DCR0);
91 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
92 u2_phy_w32(phy, 0x00000402, OFS_U2_PHY_DCR0);
93 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
94 u2_phy_w32(phy, 0x0048086a, OFS_U2_PHY_AC0);
95 u2_phy_w32(phy, 0x4400001c, OFS_U2_PHY_AC1);
96 u2_phy_w32(phy, 0xc0200000, OFS_U2_PHY_ACR3);
97 u2_phy_w32(phy, 0x02000000, OFS_U2_PHY_DTM0);
98}
99
100static int ralink_usb_phy_power_on(struct phy *_phy)
101{
102 struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
103 u32 t;
104
105 /* enable the phy */
106 regmap_update_bits(phy->sysctl, RT_SYSC_REG_CLKCFG1,
107 phy->clk, phy->clk);
108
109 /* setup host mode */
110 regmap_update_bits(phy->sysctl, RT_SYSC_REG_SYSCFG1,
111 RT_SYSCFG1_USB0_HOST_MODE,
112 RT_SYSCFG1_USB0_HOST_MODE);
113
114 /* deassert the reset lines */
115 reset_control_deassert(phy->rsthost);
116 reset_control_deassert(phy->rstdev);
117
118 /*
119 * The SDK kernel had a delay of 100ms. however on device
120 * testing showed that 10ms is enough
121 */
122 mdelay(10);
123
124 if (phy->base)
125 ralink_usb_phy_init(phy);
126
127 /* print some status info */
128 regmap_read(phy->sysctl, RT_SYSC_REG_USB_PHY_CFG, &t);
129 dev_info(&phy->phy->dev, "remote usb device wakeup %s\n",
130 (t & UDEV_WAKEUP) ? ("enabled") : ("disabled"));
131 if (t & USB_PHY_UTMI_8B60M)
132 dev_info(&phy->phy->dev, "UTMI 8bit 60MHz\n");
133 else
134 dev_info(&phy->phy->dev, "UTMI 16bit 30MHz\n");
135
136 return 0;
137}
138
139static int ralink_usb_phy_power_off(struct phy *_phy)
140{
141 struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
142
143 /* disable the phy */
144 regmap_update_bits(phy->sysctl, RT_SYSC_REG_CLKCFG1,
145 phy->clk, 0);
146
147 /* assert the reset lines */
148 reset_control_assert(phy->rstdev);
149 reset_control_assert(phy->rsthost);
150
151 return 0;
152}
153
154static struct phy_ops ralink_usb_phy_ops = {
155 .power_on = ralink_usb_phy_power_on,
156 .power_off = ralink_usb_phy_power_off,
157 .owner = THIS_MODULE,
158};
159
160static const struct of_device_id ralink_usb_phy_of_match[] = {
161 {
162 .compatible = "ralink,rt3352-usbphy",
163 .data = (void *)(uintptr_t)(RT_CLKCFG1_UPHY1_CLK_EN |
164 RT_CLKCFG1_UPHY0_CLK_EN)
165 },
166 {
167 .compatible = "mediatek,mt7620-usbphy",
168 .data = (void *)(uintptr_t)(MT7620_CLKCFG1_UPHY1_CLK_EN |
169 MT7620_CLKCFG1_UPHY0_CLK_EN)
170 },
171 {
172 .compatible = "mediatek,mt7628-usbphy",
173 .data = (void *)(uintptr_t)(MT7620_CLKCFG1_UPHY1_CLK_EN |
174 MT7620_CLKCFG1_UPHY0_CLK_EN) },
175 { },
176};
177MODULE_DEVICE_TABLE(of, ralink_usb_phy_of_match);
178
179static int ralink_usb_phy_probe(struct platform_device *pdev)
180{
181 struct device *dev = &pdev->dev;
182 struct resource *res;
183 struct phy_provider *phy_provider;
184 const struct of_device_id *match;
185 struct ralink_usb_phy *phy;
186
187 match = of_match_device(ralink_usb_phy_of_match, &pdev->dev);
188 if (!match)
189 return -ENODEV;
190
191 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
192 if (!phy)
193 return -ENOMEM;
194
195 phy->clk = (uintptr_t)match->data;
196 phy->base = NULL;
197
198 phy->sysctl = syscon_regmap_lookup_by_phandle(dev->of_node, "ralink,sysctl");
199 if (IS_ERR(phy->sysctl)) {
200 dev_err(dev, "failed to get sysctl registers\n");
201 return PTR_ERR(phy->sysctl);
202 }
203
204 /* The MT7628 and MT7688 require extra setup of PHY registers. */
205 if (of_device_is_compatible(dev->of_node, "mediatek,mt7628-usbphy")) {
206 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
207 phy->base = devm_ioremap_resource(&pdev->dev, res);
208 if (IS_ERR(phy->base)) {
209 dev_err(dev, "failed to remap register memory\n");
210 return PTR_ERR(phy->base);
211 }
212 }
213
214 phy->rsthost = devm_reset_control_get(&pdev->dev, "host");
215 if (IS_ERR(phy->rsthost)) {
216 dev_err(dev, "host reset is missing\n");
217 return PTR_ERR(phy->rsthost);
218 }
219
220 phy->rstdev = devm_reset_control_get(&pdev->dev, "device");
221 if (IS_ERR(phy->rstdev)) {
222 dev_err(dev, "device reset is missing\n");
223 return PTR_ERR(phy->rstdev);
224 }
225
226 phy->phy = devm_phy_create(dev, NULL, &ralink_usb_phy_ops);
227 if (IS_ERR(phy->phy)) {
228 dev_err(dev, "failed to create PHY\n");
229 return PTR_ERR(phy->phy);
230 }
231 phy_set_drvdata(phy->phy, phy);
232
233 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
234
235 return PTR_ERR_OR_ZERO(phy_provider);
236}
237
238static struct platform_driver ralink_usb_phy_driver = {
239 .probe = ralink_usb_phy_probe,
240 .driver = {
241 .of_match_table = ralink_usb_phy_of_match,
242 .name = "ralink-usb-phy",
243 }
244};
245module_platform_driver(ralink_usb_phy_driver);
246
247MODULE_DESCRIPTION("Ralink USB phy driver");
248MODULE_AUTHOR("John Crispin <john@phrozen.org>");
249MODULE_LICENSE("GPL v2");