ralink: fix mt7620 ohci 3.14
[openwrt/svn-archive/archive.git] / target / linux / ramips / patches-3.14 / 0037-USB-phy-add-ralink-SoC-driver.patch
1 From 900fa0abfea0cb7562c523769981dadc25f1f8cd Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 27 Jul 2014 09:43:42 +0100
4 Subject: [PATCH 37/57] USB: phy: add ralink SoC driver
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 drivers/usb/phy/Kconfig | 8 ++
9 drivers/usb/phy/Makefile | 1 +
10 drivers/usb/phy/ralink-phy.c | 190 ++++++++++++++++++++++++++++++++++++++++++
11 3 files changed, 199 insertions(+)
12 create mode 100644 drivers/usb/phy/ralink-phy.c
13
14 Index: linux-3.14.16/drivers/usb/phy/Kconfig
15 ===================================================================
16 --- linux-3.14.16.orig/drivers/usb/phy/Kconfig 2014-08-08 01:50:59.000000000 +0200
17 +++ linux-3.14.16/drivers/usb/phy/Kconfig 2014-08-24 12:39:21.094371467 +0200
18 @@ -251,6 +251,14 @@
19 To compile this driver as a module, choose M here: the
20 module will be called phy-rcar-gen2-usb.
21
22 +config RALINK_USBPHY
23 + bool "Ralink USB PHY controller Driver"
24 + depends on MIPS && RALINK
25 + select USB_PHY
26 + help
27 + Enable this to support ralink USB phy controller for ralink
28 + SoCs.
29 +
30 config USB_ULPI
31 bool "Generic ULPI Transceiver Driver"
32 depends on ARM
33 Index: linux-3.14.16/drivers/usb/phy/Makefile
34 ===================================================================
35 --- linux-3.14.16.orig/drivers/usb/phy/Makefile 2014-08-08 01:50:59.000000000 +0200
36 +++ linux-3.14.16/drivers/usb/phy/Makefile 2014-08-24 12:39:21.094371467 +0200
37 @@ -33,3 +33,4 @@
38 obj-$(CONFIG_USB_ULPI) += phy-ulpi.o
39 obj-$(CONFIG_USB_ULPI_VIEWPORT) += phy-ulpi-viewport.o
40 obj-$(CONFIG_KEYSTONE_USB_PHY) += phy-keystone.o
41 +obj-$(CONFIG_RALINK_USBPHY) += ralink-phy.o
42 Index: linux-3.14.16/drivers/usb/phy/ralink-phy.c
43 ===================================================================
44 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
45 +++ linux-3.14.16/drivers/usb/phy/ralink-phy.c 2014-08-24 14:28:35.886531881 +0200
46 @@ -0,0 +1,191 @@
47 +/*
48 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
49 + *
50 + * based on: Renesas R-Car USB phy driver
51 + *
52 + * This program is free software; you can redistribute it and/or modify
53 + * it under the terms of the GNU General Public License version 2 as
54 + * published by the Free Software Foundation.
55 + */
56 +
57 +#include <linux/delay.h>
58 +#include <linux/io.h>
59 +#include <linux/usb/otg.h>
60 +#include <linux/of_platform.h>
61 +#include <linux/platform_device.h>
62 +#include <linux/spinlock.h>
63 +#include <linux/module.h>
64 +#include <linux/reset.h>
65 +
66 +#include <asm/mach-ralink/ralink_regs.h>
67 +
68 +#define RT_SYSC_REG_SYSCFG1 0x014
69 +#define RT_SYSC_REG_CLKCFG1 0x030
70 +#define RT_SYSC_REG_USB_PHY_CFG 0x05c
71 +
72 +#define RT_RSTCTRL_UDEV BIT(25)
73 +#define RT_RSTCTRL_UHST BIT(22)
74 +#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
75 +
76 +#define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
77 +#define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
78 +#define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
79 +#define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
80 +
81 +#define USB_PHY_UTMI_8B60M BIT(1)
82 +#define UDEV_WAKEUP BIT(0)
83 +
84 +static atomic_t usb_pwr_ref = ATOMIC_INIT(0);
85 +static struct reset_control *rstdev;
86 +static struct reset_control *rsthost;
87 +static u32 phy_clk;
88 +
89 +static void usb_phy_enable(int state)
90 +{
91 + if (state)
92 + rt_sysc_m32(0, phy_clk, RT_SYSC_REG_CLKCFG1);
93 + else
94 + rt_sysc_m32(phy_clk, 0, RT_SYSC_REG_CLKCFG1);
95 + mdelay(100);
96 +}
97 +
98 +static int usb_power_on(struct usb_phy *phy)
99 +{
100 + if (atomic_inc_return(&usb_pwr_ref) == 1) {
101 + u32 t;
102 +
103 + usb_phy_enable(1);
104 +
105 +// reset_control_assert(rstdev);
106 +// reset_control_assert(rsthost);
107 +
108 + if (OTG_STATE_B_HOST) {
109 + rt_sysc_m32(0, RT_SYSCFG1_USB0_HOST_MODE, RT_SYSC_REG_SYSCFG1);
110 + if (!IS_ERR(rsthost))
111 + reset_control_deassert(rsthost);
112 + } else {
113 + rt_sysc_m32(RT_SYSCFG1_USB0_HOST_MODE, 0, RT_SYSC_REG_SYSCFG1);
114 + if (!IS_ERR(rstdev))
115 + reset_control_deassert(rstdev);
116 + }
117 + mdelay(100);
118 +
119 + t = rt_sysc_r32(RT_SYSC_REG_USB_PHY_CFG);
120 + dev_info(phy->dev, "remote usb device wakeup %s\n",
121 + (t & UDEV_WAKEUP) ? ("enabbled") : ("disabled"));
122 + if (t & USB_PHY_UTMI_8B60M)
123 + dev_info(phy->dev, "UTMI 8bit 60MHz\n");
124 + else
125 + dev_info(phy->dev, "UTMI 16bit 30MHz\n");
126 + }
127 +
128 + return 0;
129 +}
130 +
131 +static void usb_power_off(struct usb_phy *phy)
132 +{
133 + if (atomic_dec_return(&usb_pwr_ref) == 0) {
134 + usb_phy_enable(0);
135 + if (!IS_ERR(rstdev))
136 + reset_control_assert(rstdev);
137 + if (!IS_ERR(rsthost))
138 + reset_control_assert(rsthost);
139 + }
140 +}
141 +
142 +static int usb_set_host(struct usb_otg *otg, struct usb_bus *host)
143 +{
144 + otg->gadget = NULL;
145 + otg->host = host;
146 +
147 + return 0;
148 +}
149 +
150 +static int usb_set_peripheral(struct usb_otg *otg,
151 + struct usb_gadget *gadget)
152 +{
153 + otg->host = NULL;
154 + otg->gadget = gadget;
155 +
156 + return 0;
157 +}
158 +
159 +static const struct of_device_id ralink_usbphy_dt_match[] = {
160 + { .compatible = "ralink,rt3xxx-usbphy", .data = (void *) (RT_CLKCFG1_UPHY1_CLK_EN | RT_CLKCFG1_UPHY0_CLK_EN) },
161 + { .compatible = "ralink,mt7620a-usbphy", .data = (void *) (MT7620_CLKCFG1_UPHY1_CLK_EN | MT7620_CLKCFG1_UPHY0_CLK_EN) },
162 + {},
163 +};
164 +MODULE_DEVICE_TABLE(of, ralink_usbphy_dt_match);
165 +
166 +static int usb_phy_probe(struct platform_device *pdev)
167 +{
168 + const struct of_device_id *match;
169 + struct device *dev = &pdev->dev;
170 + struct usb_otg *otg;
171 + struct usb_phy *phy;
172 + int ret;
173 +
174 + match = of_match_device(ralink_usbphy_dt_match, &pdev->dev);
175 + phy_clk = (int) match->data;
176 +
177 + rsthost = devm_reset_control_get(&pdev->dev, "host");
178 + rstdev = devm_reset_control_get(&pdev->dev, "device");
179 +
180 + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
181 + if (!phy) {
182 + dev_err(&pdev->dev, "unable to allocate memory for USB PHY\n");
183 + return -ENOMEM;
184 + }
185 +
186 + otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
187 + if (!otg) {
188 + dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
189 + return -ENOMEM;
190 + }
191 +
192 + phy->dev = dev;
193 + phy->label = dev_name(dev);
194 + phy->init = usb_power_on;
195 + phy->shutdown = usb_power_off;
196 + otg->set_host = usb_set_host;
197 + otg->set_peripheral = usb_set_peripheral;
198 + otg->phy = phy;
199 + phy->otg = otg;
200 + ret = usb_add_phy(phy, USB_PHY_TYPE_USB2);
201 +
202 + if (ret < 0) {
203 + dev_err(dev, "usb phy addition error\n");
204 + return ret;
205 + }
206 +
207 + platform_set_drvdata(pdev, phy);
208 +
209 + dev_info(&pdev->dev, "loaded\n");
210 +
211 + return ret;
212 +}
213 +
214 +static int usb_phy_remove(struct platform_device *pdev)
215 +{
216 + struct usb_phy *phy = platform_get_drvdata(pdev);
217 +
218 + usb_remove_phy(phy);
219 +
220 + return 0;
221 +}
222 +
223 +static struct platform_driver usb_phy_driver = {
224 + .driver = {
225 + .owner = THIS_MODULE,
226 + .name = "rt3xxx-usbphy",
227 + .of_match_table = of_match_ptr(ralink_usbphy_dt_match),
228 + },
229 + .probe = usb_phy_probe,
230 + .remove = usb_phy_remove,
231 +};
232 +
233 +module_platform_driver(usb_phy_driver);
234 +
235 +MODULE_LICENSE("GPL v2");
236 +MODULE_DESCRIPTION("Ralink USB phy");
237 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");