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