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
6 Signed-off-by: John Crispin <blogic@openwrt.org>
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
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).
21 + bool "Ralink USB PHY controller Driver"
22 + depends on MIPS && RALINK
23 + select USB_OTG_UTILS
25 + Enable this to support ralink USB phy controller for ralink
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
37 +++ b/drivers/usb/phy/ralink-phy.c
40 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
42 + * based on: Renesas R-Car USB phy driver
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.
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>
58 +#include <asm/mach-ralink/ralink_regs.h>
60 +#define RT_SYSC_REG_SYSCFG1 0x014
61 +#define RT_SYSC_REG_CLKCFG1 0x030
62 +#define RT_SYSC_REG_USB_PHY_CFG 0x05c
64 +#define RT_RSTCTRL_UDEV BIT(25)
65 +#define RT_RSTCTRL_UHST BIT(22)
66 +#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
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)
72 +#define USB_PHY_UTMI_8B60M BIT(1)
73 +#define UDEV_WAKEUP BIT(0)
75 +static atomic_t usb_pwr_ref = ATOMIC_INIT(0);
76 +static struct reset_control *rstdev;
77 +static struct reset_control *rsthost;
80 +static void usb_phy_enable(int state)
83 + rt_sysc_m32(0, phy_clk, RT_SYSC_REG_CLKCFG1);
85 + rt_sysc_m32(phy_clk, 0, RT_SYSC_REG_CLKCFG1);
89 +static int usb_power_on(struct usb_phy *phy)
91 + if (atomic_inc_return(&usb_pwr_ref) == 1) {
96 +// reset_control_assert(rstdev);
97 +// reset_control_assert(rsthost);
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);
103 + rt_sysc_m32(RT_SYSCFG1_USB0_HOST_MODE, 0, RT_SYSC_REG_SYSCFG1);
104 + reset_control_deassert(rstdev);
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");
114 + dev_info(phy->dev, "UTMI 16bit 30MHz\n");
120 +static void usb_power_off(struct usb_phy *phy)
122 + if (atomic_dec_return(&usb_pwr_ref) == 0) {
124 + reset_control_assert(rstdev);
125 + reset_control_assert(rsthost);
129 +static int usb_set_host(struct usb_otg *otg, struct usb_bus *host)
131 + otg->gadget = NULL;
137 +static int usb_set_peripheral(struct usb_otg *otg,
138 + struct usb_gadget *gadget)
141 + otg->gadget = gadget;
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 },
151 +MODULE_DEVICE_TABLE(of, ralink_usbphy_dt_match);
153 +static int usb_phy_probe(struct platform_device *pdev)
155 + const struct of_device_id *match;
156 + struct device *dev = &pdev->dev;
157 + struct usb_otg *otg;
158 + struct usb_phy *phy;
161 + match = of_match_device(ralink_usbphy_dt_match, &pdev->dev);
162 + phy_clk = (int) match->data;
164 + rsthost = devm_reset_control_get(&pdev->dev, "host");
165 + if (IS_ERR(rsthost))
166 + return PTR_ERR(rsthost);
168 + rstdev = devm_reset_control_get(&pdev->dev, "device");
169 + if (IS_ERR(rstdev))
170 + return PTR_ERR(rstdev);
172 + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
174 + dev_err(&pdev->dev, "unable to allocate memory for USB PHY\n");
178 + otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
180 + dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
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;
192 + ret = usb_add_phy(phy, USB_PHY_TYPE_USB2);
195 + dev_err(dev, "usb phy addition error\n");
199 + platform_set_drvdata(pdev, phy);
201 + dev_info(&pdev->dev, "loaded\n");
206 +static int usb_phy_remove(struct platform_device *pdev)
208 + struct usb_phy *phy = platform_get_drvdata(pdev);
210 + usb_remove_phy(phy);
215 +static struct platform_driver usb_phy_driver = {
217 + .owner = THIS_MODULE,
218 + .name = "rt3xxx-usbphy",
219 + .of_match_table = of_match_ptr(ralink_usbphy_dt_match),
221 + .probe = usb_phy_probe,
222 + .remove = usb_phy_remove,
225 +module_platform_driver(usb_phy_driver);
227 +MODULE_LICENSE("GPL v2");
228 +MODULE_DESCRIPTION("Ralink USB phy");
229 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");