blob: 269615228b1b06caac8cded10ea351055418e4d8 [file] [log] [blame]
Yoshihiro Shimodaf3b5a8d2015-11-30 10:44:30 +09001/*
2 * Renesas R-Car Gen3 for USB2.0 PHY driver
3 *
4 * Copyright (C) 2015 Renesas Electronics Corporation
5 *
6 * This is based on the phy-rcar-gen2 driver:
7 * Copyright (C) 2014 Renesas Solutions Corp.
8 * Copyright (C) 2014 Cogent Embedded, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/of_address.h>
19#include <linux/phy/phy.h>
20#include <linux/platform_device.h>
21
22/******* USB2.0 Host registers (original offset is +0x200) *******/
23#define USB2_INT_ENABLE 0x000
24#define USB2_USBCTR 0x00c
25#define USB2_SPD_RSM_TIMSET 0x10c
26#define USB2_OC_TIMSET 0x110
27
28/* INT_ENABLE */
29#define USB2_INT_ENABLE_USBH_INTB_EN BIT(2)
30#define USB2_INT_ENABLE_USBH_INTA_EN BIT(1)
31#define USB2_INT_ENABLE_INIT (USB2_INT_ENABLE_USBH_INTB_EN | \
32 USB2_INT_ENABLE_USBH_INTA_EN)
33
34/* USBCTR */
35#define USB2_USBCTR_DIRPD BIT(2)
36#define USB2_USBCTR_PLL_RST BIT(1)
37
38/* SPD_RSM_TIMSET */
39#define USB2_SPD_RSM_TIMSET_INIT 0x014e029b
40
41/* OC_TIMSET */
42#define USB2_OC_TIMSET_INIT 0x000209ab
43
44/******* HSUSB registers (original offset is +0x100) *******/
45#define HSUSB_LPSTS 0x02
46#define HSUSB_UGCTRL2 0x84
47
48/* Low Power Status register (LPSTS) */
49#define HSUSB_LPSTS_SUSPM 0x4000
50
51/* USB General control register 2 (UGCTRL2) */
52#define HSUSB_UGCTRL2_MASK 0x00000031 /* bit[31:6] should be 0 */
53#define HSUSB_UGCTRL2_USB0SEL 0x00000030
54#define HSUSB_UGCTRL2_USB0SEL_HOST 0x00000010
55#define HSUSB_UGCTRL2_USB0SEL_HS_USB 0x00000020
56#define HSUSB_UGCTRL2_USB0SEL_OTG 0x00000030
57
58struct rcar_gen3_data {
59 void __iomem *base;
60 struct clk *clk;
61};
62
63struct rcar_gen3_chan {
64 struct rcar_gen3_data usb2;
65 struct rcar_gen3_data hsusb;
66 struct phy *phy;
67};
68
69static int rcar_gen3_phy_usb2_init(struct phy *p)
70{
71 struct rcar_gen3_chan *channel = phy_get_drvdata(p);
72 void __iomem *usb2_base = channel->usb2.base;
73 void __iomem *hsusb_base = channel->hsusb.base;
74 u32 val;
75
76 /* Initialize USB2 part */
77 writel(USB2_INT_ENABLE_INIT, usb2_base + USB2_INT_ENABLE);
78 writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET);
79 writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET);
80
81 /* Initialize HSUSB part */
82 if (hsusb_base) {
83 /* TODO: support "OTG" mode */
84 val = readl(hsusb_base + HSUSB_UGCTRL2);
85 val = (val & ~HSUSB_UGCTRL2_USB0SEL) |
86 HSUSB_UGCTRL2_USB0SEL_HOST;
87 writel(val & HSUSB_UGCTRL2_MASK, hsusb_base + HSUSB_UGCTRL2);
88 }
89
90 return 0;
91}
92
93static int rcar_gen3_phy_usb2_exit(struct phy *p)
94{
95 struct rcar_gen3_chan *channel = phy_get_drvdata(p);
96
97 writel(0, channel->usb2.base + USB2_INT_ENABLE);
98
99 return 0;
100}
101
102static int rcar_gen3_phy_usb2_power_on(struct phy *p)
103{
104 struct rcar_gen3_chan *channel = phy_get_drvdata(p);
105 void __iomem *usb2_base = channel->usb2.base;
106 void __iomem *hsusb_base = channel->hsusb.base;
107 u32 val;
108
109 val = readl(usb2_base + USB2_USBCTR);
110 val |= USB2_USBCTR_PLL_RST;
111 writel(val, usb2_base + USB2_USBCTR);
112 val &= ~USB2_USBCTR_PLL_RST;
113 writel(val, usb2_base + USB2_USBCTR);
114
115 /*
116 * TODO: To reduce power consuming, this driver should set the SUSPM
117 * after the PHY detects ID pin as peripheral.
118 */
119 if (hsusb_base) {
120 /* Power on HSUSB PHY */
121 val = readw(hsusb_base + HSUSB_LPSTS);
122 val |= HSUSB_LPSTS_SUSPM;
123 writew(val, hsusb_base + HSUSB_LPSTS);
124 }
125
126 return 0;
127}
128
129static int rcar_gen3_phy_usb2_power_off(struct phy *p)
130{
131 struct rcar_gen3_chan *channel = phy_get_drvdata(p);
132 void __iomem *hsusb_base = channel->hsusb.base;
133 u32 val;
134
135 if (hsusb_base) {
136 /* Power off HSUSB PHY */
137 val = readw(hsusb_base + HSUSB_LPSTS);
138 val &= ~HSUSB_LPSTS_SUSPM;
139 writew(val, hsusb_base + HSUSB_LPSTS);
140 }
141
142 return 0;
143}
144
145static struct phy_ops rcar_gen3_phy_usb2_ops = {
146 .init = rcar_gen3_phy_usb2_init,
147 .exit = rcar_gen3_phy_usb2_exit,
148 .power_on = rcar_gen3_phy_usb2_power_on,
149 .power_off = rcar_gen3_phy_usb2_power_off,
150 .owner = THIS_MODULE,
151};
152
153static const struct of_device_id rcar_gen3_phy_usb2_match_table[] = {
154 { .compatible = "renesas,usb2-phy-r8a7795" },
155 { }
156};
157MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb2_match_table);
158
159static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
160{
161 struct device *dev = &pdev->dev;
162 struct rcar_gen3_chan *channel;
163 struct phy_provider *provider;
164 struct resource *res;
165
166 if (!dev->of_node) {
167 dev_err(dev, "This driver needs device tree\n");
168 return -EINVAL;
169 }
170
171 channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
172 if (!channel)
173 return -ENOMEM;
174
175 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "usb2_host");
176 channel->usb2.base = devm_ioremap_resource(dev, res);
177 if (IS_ERR(channel->usb2.base))
178 return PTR_ERR(channel->usb2.base);
179
180 /* "hsusb" memory resource is optional */
181 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hsusb");
182
183 /* To avoid error message by devm_ioremap_resource() */
184 if (res) {
185 channel->hsusb.base = devm_ioremap_resource(dev, res);
186 if (IS_ERR(channel->hsusb.base))
187 channel->hsusb.base = NULL;
188 }
189
190 /* devm_phy_create() will call pm_runtime_enable(dev); */
191 channel->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb2_ops);
192 if (IS_ERR(channel->phy)) {
193 dev_err(dev, "Failed to create USB2 PHY\n");
194 return PTR_ERR(channel->phy);
195 }
196
197 phy_set_drvdata(channel->phy, channel);
198
199 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
200 if (IS_ERR(provider))
201 dev_err(dev, "Failed to register PHY provider\n");
202
203 return PTR_ERR_OR_ZERO(provider);
204}
205
206static struct platform_driver rcar_gen3_phy_usb2_driver = {
207 .driver = {
208 .name = "phy_rcar_gen3_usb2",
209 .of_match_table = rcar_gen3_phy_usb2_match_table,
210 },
211 .probe = rcar_gen3_phy_usb2_probe,
212};
213module_platform_driver(rcar_gen3_phy_usb2_driver);
214
215MODULE_LICENSE("GPL v2");
216MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY");
217MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");