2 * Renesas R-Car Gen3 for USB2.0 PHY driver
4 * Copyright (C) 2015 Renesas Electronics Corporation
6 * This is based on the phy-rcar-gen2 driver:
7 * Copyright (C) 2014 Renesas Solutions Corp.
8 * Copyright (C) 2014 Cogent Embedded, Inc.
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.
15 #include <linux/interrupt.h>
17 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_device.h>
23 /******* USB2.0 Host registers (original offset is +0x200) *******/
24 #define USB2_INT_ENABLE 0x000
25 #define USB2_USBCTR 0x00c
26 #define USB2_SPD_RSM_TIMSET 0x10c
27 #define USB2_OC_TIMSET 0x110
28 #define USB2_COMMCTRL 0x600
29 #define USB2_OBINTSTA 0x604
30 #define USB2_OBINTEN 0x608
31 #define USB2_VBCTRL 0x60c
32 #define USB2_LINECTRL1 0x610
33 #define USB2_ADPCTRL 0x630
36 #define USB2_INT_ENABLE_UCOM_INTEN BIT(3)
37 #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2)
38 #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1)
39 #define USB2_INT_ENABLE_INIT (USB2_INT_ENABLE_UCOM_INTEN | \
40 USB2_INT_ENABLE_USBH_INTB_EN | \
41 USB2_INT_ENABLE_USBH_INTA_EN)
44 #define USB2_USBCTR_DIRPD BIT(2)
45 #define USB2_USBCTR_PLL_RST BIT(1)
48 #define USB2_SPD_RSM_TIMSET_INIT 0x014e029b
51 #define USB2_OC_TIMSET_INIT 0x000209ab
54 #define USB2_COMMCTRL_OTG_PERI BIT(31) /* 1 = Peripheral mode */
56 /* OBINTSTA and OBINTEN */
57 #define USB2_OBINT_SESSVLDCHG BIT(12)
58 #define USB2_OBINT_IDDIGCHG BIT(11)
59 #define USB2_OBINT_BITS (USB2_OBINT_SESSVLDCHG | \
63 #define USB2_VBCTRL_DRVVBUSSEL BIT(8)
66 #define USB2_LINECTRL1_DPRPD_EN BIT(19)
67 #define USB2_LINECTRL1_DP_RPD BIT(18)
68 #define USB2_LINECTRL1_DMRPD_EN BIT(17)
69 #define USB2_LINECTRL1_DM_RPD BIT(16)
72 #define USB2_ADPCTRL_OTGSESSVLD BIT(20)
73 #define USB2_ADPCTRL_IDDIG BIT(19)
74 #define USB2_ADPCTRL_IDPULLUP BIT(5) /* 1 = ID sampling is enabled */
75 #define USB2_ADPCTRL_DRVVBUS BIT(4)
77 /******* HSUSB registers (original offset is +0x100) *******/
78 #define HSUSB_LPSTS 0x02
79 #define HSUSB_UGCTRL2 0x84
81 /* Low Power Status register (LPSTS) */
82 #define HSUSB_LPSTS_SUSPM 0x4000
84 /* USB General control register 2 (UGCTRL2) */
85 #define HSUSB_UGCTRL2_MASK 0x00000031 /* bit[31:6] should be 0 */
86 #define HSUSB_UGCTRL2_USB0SEL 0x00000030
87 #define HSUSB_UGCTRL2_USB0SEL_HOST 0x00000010
88 #define HSUSB_UGCTRL2_USB0SEL_HS_USB 0x00000020
89 #define HSUSB_UGCTRL2_USB0SEL_OTG 0x00000030
91 struct rcar_gen3_data
{
96 struct rcar_gen3_chan
{
97 struct rcar_gen3_data usb2
;
98 struct rcar_gen3_data hsusb
;
102 static void rcar_gen3_set_host_mode(struct rcar_gen3_chan
*ch
, int host
)
104 void __iomem
*usb2_base
= ch
->usb2
.base
;
105 u32 val
= readl(usb2_base
+ USB2_COMMCTRL
);
107 dev_vdbg(&ch
->phy
->dev
, "%s: %08x, %d\n", __func__
, val
, host
);
109 val
&= ~USB2_COMMCTRL_OTG_PERI
;
111 val
|= USB2_COMMCTRL_OTG_PERI
;
112 writel(val
, usb2_base
+ USB2_COMMCTRL
);
115 static void rcar_gen3_set_linectrl(struct rcar_gen3_chan
*ch
, int dp
, int dm
)
117 void __iomem
*usb2_base
= ch
->usb2
.base
;
118 u32 val
= readl(usb2_base
+ USB2_LINECTRL1
);
120 dev_vdbg(&ch
->phy
->dev
, "%s: %08x, %d, %d\n", __func__
, val
, dp
, dm
);
121 val
&= ~(USB2_LINECTRL1_DP_RPD
| USB2_LINECTRL1_DM_RPD
);
123 val
|= USB2_LINECTRL1_DP_RPD
;
125 val
|= USB2_LINECTRL1_DM_RPD
;
126 writel(val
, usb2_base
+ USB2_LINECTRL1
);
129 static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan
*ch
, int vbus
)
131 void __iomem
*usb2_base
= ch
->usb2
.base
;
132 u32 val
= readl(usb2_base
+ USB2_ADPCTRL
);
134 dev_vdbg(&ch
->phy
->dev
, "%s: %08x, %d\n", __func__
, val
, vbus
);
136 val
|= USB2_ADPCTRL_DRVVBUS
;
138 val
&= ~USB2_ADPCTRL_DRVVBUS
;
139 writel(val
, usb2_base
+ USB2_ADPCTRL
);
142 static void rcar_gen3_init_for_host(struct rcar_gen3_chan
*ch
)
144 rcar_gen3_set_linectrl(ch
, 1, 1);
145 rcar_gen3_set_host_mode(ch
, 1);
146 rcar_gen3_enable_vbus_ctrl(ch
, 1);
149 static void rcar_gen3_init_for_peri(struct rcar_gen3_chan
*ch
)
151 rcar_gen3_set_linectrl(ch
, 0, 1);
152 rcar_gen3_set_host_mode(ch
, 0);
153 rcar_gen3_enable_vbus_ctrl(ch
, 0);
156 static bool rcar_gen3_check_vbus(struct rcar_gen3_chan
*ch
)
158 return !!(readl(ch
->usb2
.base
+ USB2_ADPCTRL
) &
159 USB2_ADPCTRL_OTGSESSVLD
);
162 static bool rcar_gen3_check_id(struct rcar_gen3_chan
*ch
)
164 return !!(readl(ch
->usb2
.base
+ USB2_ADPCTRL
) & USB2_ADPCTRL_IDDIG
);
167 static void rcar_gen3_device_recognition(struct rcar_gen3_chan
*ch
)
172 if (rcar_gen3_check_id(ch
) && rcar_gen3_check_vbus(ch
))
176 rcar_gen3_init_for_host(ch
);
178 rcar_gen3_init_for_peri(ch
);
181 static void rcar_gen3_init_otg(struct rcar_gen3_chan
*ch
)
183 void __iomem
*usb2_base
= ch
->usb2
.base
;
186 val
= readl(usb2_base
+ USB2_VBCTRL
);
187 writel(val
| USB2_VBCTRL_DRVVBUSSEL
, usb2_base
+ USB2_VBCTRL
);
188 writel(USB2_OBINT_BITS
, usb2_base
+ USB2_OBINTSTA
);
189 val
= readl(usb2_base
+ USB2_OBINTEN
);
190 writel(val
| USB2_OBINT_BITS
, usb2_base
+ USB2_OBINTEN
);
191 val
= readl(usb2_base
+ USB2_ADPCTRL
);
192 writel(val
| USB2_ADPCTRL_IDPULLUP
, usb2_base
+ USB2_ADPCTRL
);
193 val
= readl(usb2_base
+ USB2_LINECTRL1
);
194 rcar_gen3_set_linectrl(ch
, 0, 0);
195 writel(val
| USB2_LINECTRL1_DPRPD_EN
| USB2_LINECTRL1_DMRPD_EN
,
196 usb2_base
+ USB2_LINECTRL1
);
198 rcar_gen3_device_recognition(ch
);
201 static int rcar_gen3_phy_usb2_init(struct phy
*p
)
203 struct rcar_gen3_chan
*channel
= phy_get_drvdata(p
);
204 void __iomem
*usb2_base
= channel
->usb2
.base
;
205 void __iomem
*hsusb_base
= channel
->hsusb
.base
;
208 /* Initialize USB2 part */
209 writel(USB2_INT_ENABLE_INIT
, usb2_base
+ USB2_INT_ENABLE
);
210 writel(USB2_SPD_RSM_TIMSET_INIT
, usb2_base
+ USB2_SPD_RSM_TIMSET
);
211 writel(USB2_OC_TIMSET_INIT
, usb2_base
+ USB2_OC_TIMSET
);
213 /* Initialize HSUSB part */
215 val
= readl(hsusb_base
+ HSUSB_UGCTRL2
);
216 val
= (val
& ~HSUSB_UGCTRL2_USB0SEL
) |
217 HSUSB_UGCTRL2_USB0SEL_OTG
;
218 writel(val
& HSUSB_UGCTRL2_MASK
, hsusb_base
+ HSUSB_UGCTRL2
);
220 /* Initialize otg part */
221 rcar_gen3_init_otg(channel
);
227 static int rcar_gen3_phy_usb2_exit(struct phy
*p
)
229 struct rcar_gen3_chan
*channel
= phy_get_drvdata(p
);
231 writel(0, channel
->usb2
.base
+ USB2_INT_ENABLE
);
236 static int rcar_gen3_phy_usb2_power_on(struct phy
*p
)
238 struct rcar_gen3_chan
*channel
= phy_get_drvdata(p
);
239 void __iomem
*usb2_base
= channel
->usb2
.base
;
240 void __iomem
*hsusb_base
= channel
->hsusb
.base
;
243 val
= readl(usb2_base
+ USB2_USBCTR
);
244 val
|= USB2_USBCTR_PLL_RST
;
245 writel(val
, usb2_base
+ USB2_USBCTR
);
246 val
&= ~USB2_USBCTR_PLL_RST
;
247 writel(val
, usb2_base
+ USB2_USBCTR
);
250 * TODO: To reduce power consuming, this driver should set the SUSPM
251 * after the PHY detects ID pin as peripheral.
254 /* Power on HSUSB PHY */
255 val
= readw(hsusb_base
+ HSUSB_LPSTS
);
256 val
|= HSUSB_LPSTS_SUSPM
;
257 writew(val
, hsusb_base
+ HSUSB_LPSTS
);
263 static int rcar_gen3_phy_usb2_power_off(struct phy
*p
)
265 struct rcar_gen3_chan
*channel
= phy_get_drvdata(p
);
266 void __iomem
*hsusb_base
= channel
->hsusb
.base
;
270 /* Power off HSUSB PHY */
271 val
= readw(hsusb_base
+ HSUSB_LPSTS
);
272 val
&= ~HSUSB_LPSTS_SUSPM
;
273 writew(val
, hsusb_base
+ HSUSB_LPSTS
);
279 static struct phy_ops rcar_gen3_phy_usb2_ops
= {
280 .init
= rcar_gen3_phy_usb2_init
,
281 .exit
= rcar_gen3_phy_usb2_exit
,
282 .power_on
= rcar_gen3_phy_usb2_power_on
,
283 .power_off
= rcar_gen3_phy_usb2_power_off
,
284 .owner
= THIS_MODULE
,
287 static irqreturn_t
rcar_gen3_phy_usb2_irq(int irq
, void *_ch
)
289 struct rcar_gen3_chan
*ch
= _ch
;
290 void __iomem
*usb2_base
= ch
->usb2
.base
;
291 u32 status
= readl(usb2_base
+ USB2_OBINTSTA
);
292 irqreturn_t ret
= IRQ_NONE
;
294 if (status
& USB2_OBINT_BITS
) {
295 dev_vdbg(&ch
->phy
->dev
, "%s: %08x\n", __func__
, status
);
296 writel(USB2_OBINT_BITS
, usb2_base
+ USB2_OBINTSTA
);
297 rcar_gen3_device_recognition(ch
);
304 static const struct of_device_id rcar_gen3_phy_usb2_match_table
[] = {
305 { .compatible
= "renesas,usb2-phy-r8a7795" },
308 MODULE_DEVICE_TABLE(of
, rcar_gen3_phy_usb2_match_table
);
310 static int rcar_gen3_phy_usb2_probe(struct platform_device
*pdev
)
312 struct device
*dev
= &pdev
->dev
;
313 struct rcar_gen3_chan
*channel
;
314 struct phy_provider
*provider
;
315 struct resource
*res
;
318 dev_err(dev
, "This driver needs device tree\n");
322 channel
= devm_kzalloc(dev
, sizeof(*channel
), GFP_KERNEL
);
326 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "usb2_host");
327 channel
->usb2
.base
= devm_ioremap_resource(dev
, res
);
328 if (IS_ERR(channel
->usb2
.base
))
329 return PTR_ERR(channel
->usb2
.base
);
331 /* "hsusb" memory resource is optional */
332 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "hsusb");
334 /* To avoid error message by devm_ioremap_resource() */
338 channel
->hsusb
.base
= devm_ioremap_resource(dev
, res
);
339 if (IS_ERR(channel
->hsusb
.base
))
340 channel
->hsusb
.base
= NULL
;
341 /* call request_irq for OTG */
342 irq
= platform_get_irq(pdev
, 0);
344 irq
= devm_request_irq(dev
, irq
, rcar_gen3_phy_usb2_irq
,
345 IRQF_SHARED
, dev_name(dev
),
348 dev_err(dev
, "No irq handler (%d)\n", irq
);
351 /* devm_phy_create() will call pm_runtime_enable(dev); */
352 channel
->phy
= devm_phy_create(dev
, NULL
, &rcar_gen3_phy_usb2_ops
);
353 if (IS_ERR(channel
->phy
)) {
354 dev_err(dev
, "Failed to create USB2 PHY\n");
355 return PTR_ERR(channel
->phy
);
358 phy_set_drvdata(channel
->phy
, channel
);
360 provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
361 if (IS_ERR(provider
))
362 dev_err(dev
, "Failed to register PHY provider\n");
364 return PTR_ERR_OR_ZERO(provider
);
367 static struct platform_driver rcar_gen3_phy_usb2_driver
= {
369 .name
= "phy_rcar_gen3_usb2",
370 .of_match_table
= rcar_gen3_phy_usb2_match_table
,
372 .probe
= rcar_gen3_phy_usb2_probe
,
374 module_platform_driver(rcar_gen3_phy_usb2_driver
);
376 MODULE_LICENSE("GPL v2");
377 MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY");
378 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");