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