Debian Codegraph @

drivers/phy/renesas/phy-rcar-gen3-usb2.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas R-Car Gen3 for USB2.0 PHY driver
4 *
5 * Copyright (C) 2015-2017 Renesas Electronics Corporation
6 *
7 * This is based on the phy-rcar-gen2 driver:
8 * Copyright (C) 2014 Renesas Solutions Corp.
9 * Copyright (C) 2014 Cogent Embedded, Inc.
10 */
11
12 #include <linux/bitfield.h>
13 #include <linux/bits.h>
14 #include <linux/cleanup.h>
15 #include <linux/extcon-provider.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/mux/consumer.h>
21 #include <linux/of.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/reset.h>
28 #include <linux/string.h>
29 #include <linux/usb/of.h>
30 #include <linux/workqueue.h>
31
32 /******* USB2.0 Host registers (original offset is +0x200) *******/
33 #define USB2_INT_ENABLE 0x000
34 #define USB2_AHB_BUS_CTR 0x008
35 #define USB2_USBCTR 0x00c
36 #define USB2_REGEN_CG_CTRL 0x104 /* RZ/V2H(P) only */
37 #define USB2_SPD_RSM_TIMSET 0x10c
38 #define USB2_OC_TIMSET 0x110
39 #define USB2_UTMI_CTRL 0x118 /* RZ/V2H(P) only */
40 #define USB2_COMMCTRL 0x600
41 #define USB2_OBINTSTA 0x604
42 #define USB2_OBINTEN 0x608
43 #define USB2_VBCTRL 0x60c
44 #define USB2_LINECTRL1 0x610
45 #define USB2_ADPCTRL 0x630
46
47 /* INT_ENABLE */
48 #define USB2_INT_ENABLE_UCOM_INTEN BIT(3)
49 #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2) /* For EHCI */
50 #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1) /* For OHCI */
51
52 /* AHB_BUS_CTR */
53 #define USB2_AHB_BUS_CTR_MBL_MASK GENMASK(1, 0)
54 #define USB2_AHB_BUS_CTR_MBL_INCR4 2
55
56 /* USBCTR */
57 #define USB2_USBCTR_DIRPD BIT(2)
58 #define USB2_USBCTR_PLL_RST BIT(1)
59
60 /* REGEN_CG_CTRL*/
61 #define USB2_REGEN_CG_CTRL_UPHY_WEN BIT(0)
62
63 /* SPD_RSM_TIMSET */
64 #define USB2_SPD_RSM_TIMSET_INIT 0x014e029b
65
66 /* OC_TIMSET */
67 #define USB2_OC_TIMSET_INIT 0x000209ab
68
69 /* UTMI_CTRL */
70 #define USB2_UTMI_CTRL_INIT 0x8000018f
71
72 /* COMMCTRL */
73 #define USB2_COMMCTRL_OTG_PERI BIT(31) /* 1 = Peripheral mode */
74
75 /* OBINTSTA and OBINTEN */
76 #define USB2_OBINTSTA_CLEAR GENMASK(31, 0)
77 #define USB2_OBINT_SESSVLDCHG BIT(12)
78 #define USB2_OBINT_IDDIGCHG BIT(11)
79 #define USB2_OBINT_VBSTAINT BIT(3)
80 #define USB2_OBINT_IDCHG_EN BIT(0) /* RZ/G2L specific */
81
82 /* VBCTRL */
83 #define USB2_VBCTRL_VBSTA_MASK GENMASK(31, 28)
84 #define USB2_VBCTRL_VBSTA_DEFAULT 2
85 #define USB2_VBCTRL_VBLVL_MASK GENMASK(23, 20)
86 #define USB2_VBCTRL_VBLVL(m) FIELD_PREP_CONST(USB2_VBCTRL_VBLVL_MASK, (m))
87 #define USB2_VBCTRL_OCCLREN BIT(16)
88 #define USB2_VBCTRL_DRVVBUSSEL BIT(8)
89 #define USB2_VBCTRL_SIDDQREL BIT(2)
90 #define USB2_VBCTRL_VBOUT BIT(0)
91
92 /* LINECTRL1 */
93 #define USB2_LINECTRL1_DPRPD_EN BIT(19)
94 #define USB2_LINECTRL1_DP_RPD BIT(18)
95 #define USB2_LINECTRL1_DMRPD_EN BIT(17)
96 #define USB2_LINECTRL1_DM_RPD BIT(16)
97 #define USB2_LINECTRL1_OPMODE_NODRV BIT(6)
98
99 /* ADPCTRL */
100 #define USB2_ADPCTRL_OTGSESSVLD BIT(20)
101 #define USB2_ADPCTRL_IDDIG BIT(19)
102 #define USB2_ADPCTRL_VBUSVALID BIT(18)
103 #define USB2_ADPCTRL_IDPULLUP BIT(5) /* 1 = ID sampling is enabled */
104 #define USB2_ADPCTRL_DRVVBUS BIT(4)
105
106 /* RZ/G2L specific */
107 #define USB2_LINECTRL1_USB2_IDMON BIT(0)
108
109 #define NUM_OF_PHYS 4
110 enum rcar_gen3_phy_index {
111 PHY_INDEX_BOTH_HC,
112 PHY_INDEX_OHCI,
113 PHY_INDEX_EHCI,
114 PHY_INDEX_HSUSB
115 };
116
117 static const u32 rcar_gen3_int_enable[NUM_OF_PHYS] = {
118 USB2_INT_ENABLE_USBH_INTB_EN | USB2_INT_ENABLE_USBH_INTA_EN,
119 USB2_INT_ENABLE_USBH_INTA_EN,
120 USB2_INT_ENABLE_USBH_INTB_EN,
121 0
122 };
123
124 struct rcar_gen3_phy {
125 struct phy *phy;
126 struct rcar_gen3_chan *ch;
127 u32 int_enable_bits;
128 bool initialized;
129 bool powered;
130 };
131
132 struct rcar_gen3_chan {
133 void __iomem *base;
134 struct device *dev; /* platform_device's device */
135 const struct rcar_gen3_phy_drv_data *phy_data;
136 struct extcon_dev *extcon;
137 struct reset_control *rstc;
138 struct rcar_gen3_phy rphys[NUM_OF_PHYS];
139 struct regulator *vbus;
140 struct work_struct work;
141 spinlock_t lock; /* protects access to hardware and driver data structure. */
142 enum usb_dr_mode dr_mode;
143 bool extcon_host;
144 bool is_otg_channel;
145 bool uses_otg_pins;
146 bool otg_internal_reg;
147 };
148
149 struct rcar_gen3_phy_drv_data {
150 const struct phy_ops *phy_usb2_ops;
151 bool no_adp_ctrl;
152 bool init_bus;
153 bool utmi_ctrl;
154 bool vblvl_ctrl;
155 u32 obint_enable_bits;
156 };
157
158 /*
159 * Combination about is_otg_channel and uses_otg_pins:
160 *
161 * Parameters || Behaviors
162 * is_otg_channel | uses_otg_pins || irqs | role sysfs
163 * ---------------------+---------------++--------------+------------
164 * true | true || enabled | enabled
165 * true | false || disabled | enabled
166 * false | any || disabled | disabled
167 */
168
169 static void rcar_gen3_phy_usb2_work(struct work_struct *work)
170 {
171 struct rcar_gen3_chan *ch = container_of(work, struct rcar_gen3_chan,
172 work);
173
174 if (ch->extcon_host) {
175 extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, true);
176 extcon_set_state_sync(ch->extcon, EXTCON_USB, false);
177 } else {
178 extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, false);
179 extcon_set_state_sync(ch->extcon, EXTCON_USB, true);
180 }
181 }
182
183 static void rcar_gen3_set_host_mode(struct rcar_gen3_chan *ch, int host)
184 {
185 void __iomem *usb2_base = ch->base;
186 u32 val = readl(usb2_base + USB2_COMMCTRL);
187
188 dev_vdbg(ch->dev, "%s: %08x, %d\n", __func__, val, host);
189 if (host)
190 val &= ~USB2_COMMCTRL_OTG_PERI;
191 else
192 val |= USB2_COMMCTRL_OTG_PERI;
193 writel(val, usb2_base + USB2_COMMCTRL);
194 }
195
196 static void rcar_gen3_set_linectrl(struct rcar_gen3_chan *ch, int dp, int dm)
197 {
198 void __iomem *usb2_base = ch->base;
199 u32 val = readl(usb2_base + USB2_LINECTRL1);
200
201 dev_vdbg(ch->dev, "%s: %08x, %d, %d\n", __func__, val, dp, dm);
202 val &= ~(USB2_LINECTRL1_DP_RPD | USB2_LINECTRL1_DM_RPD);
203 if (dp)
204 val |= USB2_LINECTRL1_DP_RPD;
205 if (dm)
206 val |= USB2_LINECTRL1_DM_RPD;
207 writel(val, usb2_base + USB2_LINECTRL1);
208 }
209
210 static void rcar_gen3_phy_usb2_set_vbus(struct rcar_gen3_chan *ch,
211 u32 vbus_ctrl_reg,
212 u32 vbus_ctrl_val,
213 bool enable)
214 {
215 void __iomem *usb2_base = ch->base;
216 u32 val;
217
218 val = readl(usb2_base + vbus_ctrl_reg);
219 if (enable)
220 val |= vbus_ctrl_val;
221 else
222 val &= ~vbus_ctrl_val;
223 writel(val, usb2_base + vbus_ctrl_reg);
224
225 dev_vdbg(ch->dev, "%s: reg=0x%08x, val=%08x, enable=%d\n",
226 __func__, vbus_ctrl_reg, val, enable);
227 }
228
229 static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan *ch, int vbus)
230 {
231 if (ch->otg_internal_reg) {
232 regulator_hardware_enable(ch->vbus, vbus);
233 return;
234 }
235
236 if (ch->phy_data->no_adp_ctrl || ch->phy_data->vblvl_ctrl) {
237 if (ch->vbus)
238 regulator_hardware_enable(ch->vbus, vbus);
239
240 rcar_gen3_phy_usb2_set_vbus(ch, USB2_VBCTRL,
241 USB2_VBCTRL_VBOUT, vbus);
242 return;
243 }
244
245 rcar_gen3_phy_usb2_set_vbus(ch, USB2_ADPCTRL,
246 USB2_ADPCTRL_DRVVBUS, vbus);
247 }
248
249 static void rcar_gen3_control_otg_irq(struct rcar_gen3_chan *ch, int enable)
250 {
251 void __iomem *usb2_base = ch->base;
252 u32 val = readl(usb2_base + USB2_OBINTEN);
253
254 if (ch->uses_otg_pins && enable)
255 val |= ch->phy_data->obint_enable_bits;
256 else
257 val &= ~ch->phy_data->obint_enable_bits;
258 writel(val, usb2_base + USB2_OBINTEN);
259 }
260
261 static void rcar_gen3_init_for_host(struct rcar_gen3_chan *ch)
262 {
263 rcar_gen3_set_linectrl(ch, 1, 1);
264 rcar_gen3_set_host_mode(ch, 1);
265 rcar_gen3_enable_vbus_ctrl(ch, 1);
266
267 ch->extcon_host = true;
268 schedule_work(&ch->work);
269 }
270
271 static void rcar_gen3_init_for_peri(struct rcar_gen3_chan *ch)
272 {
273 rcar_gen3_set_linectrl(ch, 0, 1);
274 rcar_gen3_set_host_mode(ch, 0);
275 rcar_gen3_enable_vbus_ctrl(ch, 0);
276
277 ch->extcon_host = false;
278 schedule_work(&ch->work);
279 }
280
281 static void rcar_gen3_init_for_b_host(struct rcar_gen3_chan *ch)
282 {
283 void __iomem *usb2_base = ch->base;
284 u32 val;
285
286 val = readl(usb2_base + USB2_LINECTRL1);
287 writel(val | USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1);
288
289 rcar_gen3_set_linectrl(ch, 1, 1);
290 rcar_gen3_set_host_mode(ch, 1);
291 rcar_gen3_enable_vbus_ctrl(ch, 0);
292
293 val = readl(usb2_base + USB2_LINECTRL1);
294 writel(val & ~USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1);
295 }
296
297 static void rcar_gen3_init_for_a_peri(struct rcar_gen3_chan *ch)
298 {
299 rcar_gen3_set_linectrl(ch, 0, 1);
300 rcar_gen3_set_host_mode(ch, 0);
301 rcar_gen3_enable_vbus_ctrl(ch, 1);
302 }
303
304 static void rcar_gen3_init_from_a_peri_to_a_host(struct rcar_gen3_chan *ch)
305 {
306 rcar_gen3_control_otg_irq(ch, 0);
307
308 rcar_gen3_enable_vbus_ctrl(ch, 1);
309 rcar_gen3_init_for_host(ch);
310
311 rcar_gen3_control_otg_irq(ch, 1);
312 }
313
314 static bool rcar_gen3_check_id(struct rcar_gen3_chan *ch)
315 {
316 if (ch->phy_data->vblvl_ctrl) {
317 bool vbus_valid;
318 bool device;
319
320 device = !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG);
321 vbus_valid = !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_VBUSVALID);
322
323 return vbus_valid ? device : !device;
324 }
325
326 if (!ch->uses_otg_pins)
327 return ch->dr_mode != USB_DR_MODE_HOST;
328
329 if (ch->phy_data->no_adp_ctrl)
330 return !!(readl(ch->base + USB2_LINECTRL1) & USB2_LINECTRL1_USB2_IDMON);
331
332 return !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG);
333 }
334
335 static void rcar_gen3_device_recognition(struct rcar_gen3_chan *ch)
336 {
337 if (!rcar_gen3_check_id(ch))
338 rcar_gen3_init_for_host(ch);
339 else
340 rcar_gen3_init_for_peri(ch);
341 }
342
343 static bool rcar_gen3_is_host(struct rcar_gen3_chan *ch)
344 {
345 return !(readl(ch->base + USB2_COMMCTRL) & USB2_COMMCTRL_OTG_PERI);
346 }
347
348 static enum phy_mode rcar_gen3_get_phy_mode(struct rcar_gen3_chan *ch)
349 {
350 if (rcar_gen3_is_host(ch))
351 return PHY_MODE_USB_HOST;
352
353 return PHY_MODE_USB_DEVICE;
354 }
355
356 static bool rcar_gen3_is_any_rphy_initialized(struct rcar_gen3_chan *ch)
357 {
358 int i;
359
360 for (i = 0; i < NUM_OF_PHYS; i++) {
361 if (ch->rphys[i].initialized)
362 return true;
363 }
364
365 return false;
366 }
367
368 static bool rcar_gen3_is_any_otg_rphy_initialized(struct rcar_gen3_chan *ch)
369 {
370 for (enum rcar_gen3_phy_index i = PHY_INDEX_BOTH_HC; i <= PHY_INDEX_EHCI;
371 i++) {
372 if (ch->rphys[i].initialized)
373 return true;
374 }
375
376 return false;
377 }
378
379 static bool rcar_gen3_are_all_rphys_power_off(struct rcar_gen3_chan *ch)
380 {
381 int i;
382
383 for (i = 0; i < NUM_OF_PHYS; i++) {
384 if (ch->rphys[i].powered)
385 return false;
386 }
387
388 return true;
389 }
390
391 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t count)
393 {
394 struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
395 bool is_b_device;
396 enum phy_mode cur_mode, new_mode;
397
398 guard(spinlock_irqsave)(&ch->lock);
399
400 if (!ch->is_otg_channel || !rcar_gen3_is_any_otg_rphy_initialized(ch))
401 return -EIO;
402
403 if (sysfs_streq(buf, "host"))
404 new_mode = PHY_MODE_USB_HOST;
405 else if (sysfs_streq(buf, "peripheral"))
406 new_mode = PHY_MODE_USB_DEVICE;
407 else
408 return -EINVAL;
409
410 /* is_b_device: true is B-Device. false is A-Device. */
411 is_b_device = rcar_gen3_check_id(ch);
412 cur_mode = rcar_gen3_get_phy_mode(ch);
413
414 /* If current and new mode is the same, this returns the error */
415 if (cur_mode == new_mode)
416 return -EINVAL;
417
418 if (new_mode == PHY_MODE_USB_HOST) { /* And is_host must be false */
419 if (!is_b_device) /* A-Peripheral */
420 rcar_gen3_init_from_a_peri_to_a_host(ch);
421 else /* B-Peripheral */
422 rcar_gen3_init_for_b_host(ch);
423 } else { /* And is_host must be true */
424 if (!is_b_device) /* A-Host */
425 rcar_gen3_init_for_a_peri(ch);
426 else /* B-Host */
427 rcar_gen3_init_for_peri(ch);
428 }
429
430 return count;
431 }
432
433 static ssize_t role_show(struct device *dev, struct device_attribute *attr,
434 char *buf)
435 {
436 struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
437
438 if (!ch->is_otg_channel || !rcar_gen3_is_any_otg_rphy_initialized(ch))
439 return -EIO;
440
441 return sprintf(buf, "%s\n", rcar_gen3_is_host(ch) ? "host" :
442 "peripheral");
443 }
444 static DEVICE_ATTR_RW(role);
445
446 static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch)
447 {
448 void __iomem *usb2_base = ch->base;
449 u32 val;
450
451 if (!ch->is_otg_channel || rcar_gen3_is_any_otg_rphy_initialized(ch))
452 return;
453
454 /* Should not use functions of read-modify-write a register */
455 val = readl(usb2_base + USB2_LINECTRL1);
456 val = (val & ~USB2_LINECTRL1_DP_RPD) | USB2_LINECTRL1_DPRPD_EN |
457 USB2_LINECTRL1_DMRPD_EN | USB2_LINECTRL1_DM_RPD;
458 writel(val, usb2_base + USB2_LINECTRL1);
459
460 if (!ch->phy_data->no_adp_ctrl) {
461 if (ch->phy_data->vblvl_ctrl) {
462 val = readl(usb2_base + USB2_VBCTRL);
463 val = (val & ~USB2_VBCTRL_VBLVL_MASK) | USB2_VBCTRL_VBLVL(2);
464 writel(val, usb2_base + USB2_VBCTRL);
465 val = readl(usb2_base + USB2_ADPCTRL);
466 writel(val | USB2_ADPCTRL_IDPULLUP | USB2_ADPCTRL_DRVVBUS,
467 usb2_base + USB2_ADPCTRL);
468 } else {
469 val = readl(usb2_base + USB2_VBCTRL);
470 val &= ~USB2_VBCTRL_OCCLREN;
471 writel(val | USB2_VBCTRL_DRVVBUSSEL, usb2_base + USB2_VBCTRL);
472 val = readl(usb2_base + USB2_ADPCTRL);
473 writel(val | USB2_ADPCTRL_IDPULLUP, usb2_base + USB2_ADPCTRL);
474 }
475 }
476 mdelay(20);
477
478 writel(0xffffffff, usb2_base + USB2_OBINTSTA);
479 writel(ch->phy_data->obint_enable_bits, usb2_base + USB2_OBINTEN);
480
481 rcar_gen3_device_recognition(ch);
482 }
483
484 static void rcar_gen3_configure_vblvl_ctrl(struct rcar_gen3_chan *ch)
485 {
486 void __iomem *usb2_base = ch->base;
487 u32 val;
488
489 if (!ch->phy_data->vblvl_ctrl)
490 return;
491
492 val = readl(usb2_base + USB2_VBCTRL);
493 if ((val & USB2_VBCTRL_VBSTA_MASK) ==
494 FIELD_PREP_CONST(USB2_VBCTRL_VBSTA_MASK, USB2_VBCTRL_VBSTA_DEFAULT))
495 val &= ~USB2_VBCTRL_VBLVL_MASK;
496 else
497 val |= USB2_VBCTRL_VBLVL(USB2_VBCTRL_VBSTA_DEFAULT);
498 writel(val, usb2_base + USB2_VBCTRL);
499 }
500
501 static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, void *_ch)
502 {
503 struct rcar_gen3_chan *ch = _ch;
504 void __iomem *usb2_base = ch->base;
505 struct device *dev = ch->dev;
506 irqreturn_t ret = IRQ_NONE;
507 u32 status;
508
509 pm_runtime_get_noresume(dev);
510
511 if (pm_runtime_suspended(dev))
512 goto rpm_put;
513
514 scoped_guard(spinlock, &ch->lock) {
515 status = readl(usb2_base + USB2_OBINTSTA);
516 if (status & ch->phy_data->obint_enable_bits) {
517 dev_vdbg(dev, "%s: %08x\n", __func__, status);
518 if (ch->phy_data->vblvl_ctrl)
519 writel(USB2_OBINTSTA_CLEAR, usb2_base + USB2_OBINTSTA);
520 else
521 writel(ch->phy_data->obint_enable_bits, usb2_base + USB2_OBINTSTA);
522 rcar_gen3_device_recognition(ch);
523 rcar_gen3_configure_vblvl_ctrl(ch);
524 ret = IRQ_HANDLED;
525 }
526 }
527
528 rpm_put:
529 pm_runtime_put_noidle(dev);
530 return ret;
531 }
532
533 static int rcar_gen3_phy_usb2_init(struct phy *p)
534 {
535 struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
536 struct rcar_gen3_chan *channel = rphy->ch;
537 void __iomem *usb2_base = channel->base;
538 u32 val;
539
540 guard(spinlock_irqsave)(&channel->lock);
541
542 /* Initialize USB2 part */
543 val = readl(usb2_base + USB2_INT_ENABLE);
544 val |= USB2_INT_ENABLE_UCOM_INTEN | rphy->int_enable_bits;
545 writel(val, usb2_base + USB2_INT_ENABLE);
546
547 if (!rcar_gen3_is_any_rphy_initialized(channel)) {
548 writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET);
549 writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET);
550 }
551
552 /* Initialize otg part (only if we initialize a PHY with IRQs). */
553 if (rphy->int_enable_bits)
554 rcar_gen3_init_otg(channel);
555
556 if (channel->phy_data->vblvl_ctrl) {
557 /* SIDDQ mode release */
558 writel(readl(usb2_base + USB2_VBCTRL) | USB2_VBCTRL_SIDDQREL,
559 usb2_base + USB2_VBCTRL);
560 udelay(250);
561 }
562
563 if (channel->phy_data->utmi_ctrl) {
564 val = readl(usb2_base + USB2_REGEN_CG_CTRL) | USB2_REGEN_CG_CTRL_UPHY_WEN;
565 writel(val, usb2_base + USB2_REGEN_CG_CTRL);
566
567 writel(USB2_UTMI_CTRL_INIT, usb2_base + USB2_UTMI_CTRL);
568 writel(val & ~USB2_REGEN_CG_CTRL_UPHY_WEN, usb2_base + USB2_REGEN_CG_CTRL);
569 }
570
571 rphy->initialized = true;
572
573 return 0;
574 }
575
576 static int rcar_gen3_phy_usb2_exit(struct phy *p)
577 {
578 struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
579 struct rcar_gen3_chan *channel = rphy->ch;
580 void __iomem *usb2_base = channel->base;
581 u32 val;
582
583 guard(spinlock_irqsave)(&channel->lock);
584
585 rphy->initialized = false;
586
587 val = readl(usb2_base + USB2_INT_ENABLE);
588 val &= ~rphy->int_enable_bits;
589 if (!rcar_gen3_is_any_rphy_initialized(channel))
590 val &= ~USB2_INT_ENABLE_UCOM_INTEN;
591 writel(val, usb2_base + USB2_INT_ENABLE);
592
593 return 0;
594 }
595
596 static int rcar_gen3_phy_usb2_power_on(struct phy *p)
597 {
598 struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
599 struct rcar_gen3_chan *channel = rphy->ch;
600 void __iomem *usb2_base = channel->base;
601 u32 val;
602 int ret = 0;
603
604 if (channel->vbus && !channel->otg_internal_reg) {
605 ret = regulator_enable(channel->vbus);
606 if (ret)
607 return ret;
608 }
609
610 guard(spinlock_irqsave)(&channel->lock);
611
612 if (!rcar_gen3_are_all_rphys_power_off(channel))
613 goto out;
614
615 val = readl(usb2_base + USB2_USBCTR);
616 val |= USB2_USBCTR_PLL_RST;
617 writel(val, usb2_base + USB2_USBCTR);
618 val &= ~USB2_USBCTR_PLL_RST;
619 writel(val, usb2_base + USB2_USBCTR);
620
621 out:
622 /* The powered flag should be set for any other phys anyway */
623 rphy->powered = true;
624
625 return 0;
626 }
627
628 static int rcar_gen3_phy_usb2_power_off(struct phy *p)
629 {
630 struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
631 struct rcar_gen3_chan *channel = rphy->ch;
632 int ret = 0;
633
634 scoped_guard(spinlock_irqsave, &channel->lock) {
635 rphy->powered = false;
636
637 if (rcar_gen3_are_all_rphys_power_off(channel)) {
638 u32 val = readl(channel->base + USB2_USBCTR);
639
640 val |= USB2_USBCTR_PLL_RST;
641 writel(val, channel->base + USB2_USBCTR);
642 }
643 }
644
645 if (channel->vbus && !channel->otg_internal_reg)
646 ret = regulator_disable(channel->vbus);
647
648 return ret;
649 }
650
651 static const struct phy_ops rcar_gen3_phy_usb2_ops = {
652 .init = rcar_gen3_phy_usb2_init,
653 .exit = rcar_gen3_phy_usb2_exit,
654 .power_on = rcar_gen3_phy_usb2_power_on,
655 .power_off = rcar_gen3_phy_usb2_power_off,
656 .owner = THIS_MODULE,
657 };
658
659 static const struct phy_ops rz_g1c_phy_usb2_ops = {
660 .init = rcar_gen3_phy_usb2_init,
661 .exit = rcar_gen3_phy_usb2_exit,
662 .owner = THIS_MODULE,
663 };
664
665 static const struct rcar_gen3_phy_drv_data rcar_gen3_phy_usb2_data = {
666 .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
667 .no_adp_ctrl = false,
668 .obint_enable_bits = USB2_OBINT_SESSVLDCHG |
669 USB2_OBINT_IDDIGCHG,
670 };
671
672 static const struct rcar_gen3_phy_drv_data rz_g1c_phy_usb2_data = {
673 .phy_usb2_ops = &rz_g1c_phy_usb2_ops,
674 .no_adp_ctrl = false,
675 .obint_enable_bits = USB2_OBINT_SESSVLDCHG |
676 USB2_OBINT_IDDIGCHG,
677 };
678
679 static const struct rcar_gen3_phy_drv_data rz_g2l_phy_usb2_data = {
680 .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
681 .no_adp_ctrl = true,
682 .obint_enable_bits = USB2_OBINT_IDCHG_EN,
683 };
684
685 static const struct rcar_gen3_phy_drv_data rz_g3s_phy_usb2_data = {
686 .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
687 .no_adp_ctrl = true,
688 .init_bus = true,
689 .obint_enable_bits = USB2_OBINT_IDCHG_EN,
690 };
691
692 static const struct rcar_gen3_phy_drv_data rz_t2h_phy_usb2_data = {
693 .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
694 .vblvl_ctrl = true,
695 .obint_enable_bits = USB2_OBINT_IDCHG_EN | USB2_OBINT_VBSTAINT,
696 };
697
698 static const struct rcar_gen3_phy_drv_data rz_v2h_phy_usb2_data = {
699 .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
700 .no_adp_ctrl = true,
701 .utmi_ctrl = true,
702 .obint_enable_bits = USB2_OBINT_IDCHG_EN,
703 };
704
705 static const struct of_device_id rcar_gen3_phy_usb2_match_table[] = {
706 {
707 .compatible = "renesas,usb2-phy-r8a77470",
708 .data = &rz_g1c_phy_usb2_data,
709 },
710 {
711 .compatible = "renesas,usb2-phy-r8a7795",
712 .data = &rcar_gen3_phy_usb2_data,
713 },
714 {
715 .compatible = "renesas,usb2-phy-r8a7796",
716 .data = &rcar_gen3_phy_usb2_data,
717 },
718 {
719 .compatible = "renesas,usb2-phy-r8a77965",
720 .data = &rcar_gen3_phy_usb2_data,
721 },
722 {
723 .compatible = "renesas,usb2-phy-r9a08g045",
724 .data = &rz_g3s_phy_usb2_data,
725 },
726 {
727 .compatible = "renesas,usb2-phy-r9a09g057",
728 .data = &rz_v2h_phy_usb2_data,
729 },
730 {
731 .compatible = "renesas,usb2-phy-r9a09g077",
732 .data = &rz_t2h_phy_usb2_data,
733 },
734 {
735 .compatible = "renesas,rzg2l-usb2-phy",
736 .data = &rz_g2l_phy_usb2_data,
737 },
738 {
739 .compatible = "renesas,rcar-gen3-usb2-phy",
740 .data = &rcar_gen3_phy_usb2_data,
741 },
742 { /* sentinel */ },
743 };
744 MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb2_match_table);
745
746 static const unsigned int rcar_gen3_phy_cable[] = {
747 EXTCON_USB,
748 EXTCON_USB_HOST,
749 EXTCON_NONE,
750 };
751
752 static struct phy *rcar_gen3_phy_usb2_xlate(struct device *dev,
753 const struct of_phandle_args *args)
754 {
755 struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
756
757 if (args->args_count == 0) /* For old version dts */
758 return ch->rphys[PHY_INDEX_BOTH_HC].phy;
759 else if (args->args_count > 1) /* Prevent invalid args count */
760 return ERR_PTR(-ENODEV);
761
762 if (args->args[0] >= NUM_OF_PHYS)
763 return ERR_PTR(-ENODEV);
764
765 return ch->rphys[args->args[0]].phy;
766 }
767
768 static enum usb_dr_mode rcar_gen3_get_dr_mode(struct device_node *np)
769 {
770 enum usb_dr_mode candidate = USB_DR_MODE_UNKNOWN;
771 int i;
772
773 /*
774 * If one of device nodes has other dr_mode except UNKNOWN,
775 * this function returns UNKNOWN. To achieve backward compatibility,
776 * this loop starts the index as 0.
777 */
778 for (i = 0; i < NUM_OF_PHYS; i++) {
779 enum usb_dr_mode mode = of_usb_get_dr_mode_by_phy(np, i);
780
781 if (mode != USB_DR_MODE_UNKNOWN) {
782 if (candidate == USB_DR_MODE_UNKNOWN)
783 candidate = mode;
784 else if (candidate != mode)
785 return USB_DR_MODE_UNKNOWN;
786 }
787 }
788
789 return candidate;
790 }
791
792 static void rcar_gen3_reset_assert(void *data)
793 {
794 reset_control_assert(data);
795 }
796
797 static int rcar_gen3_phy_usb2_init_bus(struct rcar_gen3_chan *channel)
798 {
799 struct device *dev = channel->dev;
800 int ret;
801 u32 val;
802
803 if (!channel->phy_data->init_bus)
804 return 0;
805
806 ret = pm_runtime_resume_and_get(dev);
807 if (ret)
808 return ret;
809
810 val = readl(channel->base + USB2_AHB_BUS_CTR);
811 val &= ~USB2_AHB_BUS_CTR_MBL_MASK;
812 val |= USB2_AHB_BUS_CTR_MBL_INCR4;
813 writel(val, channel->base + USB2_AHB_BUS_CTR);
814
815 pm_runtime_put(dev);
816
817 return 0;
818 }
819
820 static int rcar_gen3_phy_usb2_regulator_endisable(struct regulator_dev *rdev,
821 bool enable)
822 {
823 struct rcar_gen3_chan *channel = rdev_get_drvdata(rdev);
824 struct device *dev = channel->dev;
825 int ret;
826
827 ret = pm_runtime_resume_and_get(dev);
828 if (ret < 0) {
829 dev_warn(dev, "pm_runtime_get failed: %i\n", ret);
830 return ret;
831 }
832
833 rcar_gen3_phy_usb2_set_vbus(channel, USB2_VBCTRL,
834 USB2_VBCTRL_VBOUT, enable);
835 pm_runtime_put_noidle(dev);
836
837 return ret;
838 }
839
840 static int rcar_gen3_phy_usb2_regulator_enable(struct regulator_dev *rdev)
841 {
842 return rcar_gen3_phy_usb2_regulator_endisable(rdev, true);
843 }
844
845 static int rcar_gen3_phy_usb2_regulator_disable(struct regulator_dev *rdev)
846 {
847 return rcar_gen3_phy_usb2_regulator_endisable(rdev, false);
848 }
849
850 static int rcar_gen3_phy_usb2_regulator_is_enabled(struct regulator_dev *rdev)
851 {
852 struct rcar_gen3_chan *channel = rdev_get_drvdata(rdev);
853 void __iomem *usb2_base = channel->base;
854 struct device *dev = channel->dev;
855 u32 vbus_ctrl_reg = USB2_VBCTRL;
856 u32 val;
857 int ret;
858
859 ret = pm_runtime_resume_and_get(dev);
860 if (ret < 0) {
861 dev_warn(dev, "pm_runtime_get failed: %i\n", ret);
862 return ret;
863 }
864
865 val = readl(usb2_base + vbus_ctrl_reg);
866
867 pm_runtime_put_noidle(dev);
868 dev_dbg(channel->dev, "%s: %08x\n", __func__, val);
869
870 return (val & USB2_VBCTRL_VBOUT) ? 1 : 0;
871 }
872
873 static const struct regulator_ops rcar_gen3_phy_usb2_regulator_ops = {
874 .enable = rcar_gen3_phy_usb2_regulator_enable,
875 .disable = rcar_gen3_phy_usb2_regulator_disable,
876 .is_enabled = rcar_gen3_phy_usb2_regulator_is_enabled,
877 };
878
879 static const struct regulator_desc rcar_gen3_phy_usb2_regulator = {
880 .name = "otg-vbus-regulator",
881 .of_match = of_match_ptr("vbus-regulator"),
882 .ops = &rcar_gen3_phy_usb2_regulator_ops,
883 .type = REGULATOR_VOLTAGE,
884 .owner = THIS_MODULE,
885 .fixed_uV = 5000000,
886 .n_voltages = 1,
887 };
888
889 static void rcar_gen3_phy_usb2_vbus_disable_action(void *data)
890 {
891 struct regulator *vbus = data;
892
893 regulator_disable(vbus);
894 }
895
896 static int rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(struct rcar_gen3_chan *channel,
897 bool enable)
898 {
899 struct device *dev = channel->dev;
900 int ret;
901
902 channel->vbus = devm_regulator_get_exclusive(dev, "vbus");
903 if (IS_ERR(channel->vbus))
904 return PTR_ERR(channel->vbus);
905
906 if (!enable)
907 return 0;
908
909 ret = regulator_enable(channel->vbus);
910 if (ret)
911 return ret;
912
913 return devm_add_action_or_reset(dev, rcar_gen3_phy_usb2_vbus_disable_action,
914 channel->vbus);
915 }
916
917 static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *channel)
918 {
919 struct device *dev = channel->dev;
920 struct regulator_config rcfg = { .dev = dev, };
921 struct regulator_dev *rdev;
922 bool enable = false;
923
924 rcfg.of_node = of_get_available_child_by_name(dev->of_node,
925 "vbus-regulator");
926 if (rcfg.of_node) {
927 rcfg.driver_data = channel;
928 rdev = devm_regulator_register(dev, &rcar_gen3_phy_usb2_regulator,
929 &rcfg);
930 of_node_put(rcfg.of_node);
931 if (IS_ERR(rdev))
932 return dev_err_probe(dev, PTR_ERR(rdev),
933 "Failed to create vbus-regulator\n");
934
935 channel->otg_internal_reg = true;
936 enable = true;
937 }
938
939 return rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(channel, enable);
940 }
941
942 /* Temporary wrapper until the multiplexer subsystem supports optional muxes */
943 static inline struct mux_state *
944 devm_mux_state_get_optional(struct device *dev, const char *mux_name)
945 {
946 if (!of_property_present(dev->of_node, "mux-states"))
947 return NULL;
948
949 return devm_mux_state_get(dev, mux_name);
950 }
951
952 static void rcar_gen3_phy_mux_state_deselect(void *data)
953 {
954 mux_state_deselect(data);
955 }
956
957 static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
958 {
959 struct device *dev = &pdev->dev;
960 struct rcar_gen3_chan *channel;
961 struct phy_provider *provider;
962 struct mux_state *mux_state;
963 int ret = 0, i, irq;
964
965 if (!dev->of_node) {
966 dev_err(dev, "This driver needs device tree\n");
967 return -EINVAL;
968 }
969
970 channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
971 if (!channel)
972 return -ENOMEM;
973
974 channel->base = devm_platform_ioremap_resource(pdev, 0);
975 if (IS_ERR(channel->base))
976 return PTR_ERR(channel->base);
977
978 channel->dr_mode = rcar_gen3_get_dr_mode(dev->of_node);
979 if (channel->dr_mode != USB_DR_MODE_UNKNOWN) {
980 channel->is_otg_channel = true;
981 channel->uses_otg_pins = !of_property_read_bool(dev->of_node,
982 "renesas,no-otg-pins");
983 channel->extcon = devm_extcon_dev_allocate(dev,
984 rcar_gen3_phy_cable);
985 if (IS_ERR(channel->extcon))
986 return PTR_ERR(channel->extcon);
987
988 ret = devm_extcon_dev_register(dev, channel->extcon);
989 if (ret < 0) {
990 dev_err(dev, "Failed to register extcon\n");
991 return ret;
992 }
993 }
994
995 channel->rstc = devm_reset_control_array_get_optional_shared(dev);
996 if (IS_ERR(channel->rstc))
997 return PTR_ERR(channel->rstc);
998
999 ret = reset_control_deassert(channel->rstc);
1000 if (ret)
1001 return ret;
1002
1003 ret = devm_add_action_or_reset(dev, rcar_gen3_reset_assert, channel->rstc);
1004 if (ret)
1005 return ret;
1006
1007 /*
1008 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
1009 * And then, phy-core will manage runtime pm for this device.
1010 */
1011 ret = devm_pm_runtime_enable(dev);
1012 if (ret)
1013 return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
1014
1015 channel->phy_data = of_device_get_match_data(dev);
1016 if (!channel->phy_data)
1017 return -EINVAL;
1018
1019 platform_set_drvdata(pdev, channel);
1020 channel->dev = dev;
1021
1022 ret = rcar_gen3_phy_usb2_init_bus(channel);
1023 if (ret)
1024 return ret;
1025
1026 spin_lock_init(&channel->lock);
1027 for (i = 0; i < NUM_OF_PHYS; i++) {
1028 channel->rphys[i].phy = devm_phy_create(dev, NULL,
1029 channel->phy_data->phy_usb2_ops);
1030 if (IS_ERR(channel->rphys[i].phy))
1031 return dev_err_probe(dev, PTR_ERR(channel->rphys[i].phy),
1032 "Failed to create USB2 PHY\n");
1033
1034 channel->rphys[i].ch = channel;
1035 channel->rphys[i].int_enable_bits = rcar_gen3_int_enable[i];
1036 phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
1037 }
1038
1039 mux_state = devm_mux_state_get_optional(dev, NULL);
1040 if (IS_ERR(mux_state))
1041 return PTR_ERR(mux_state);
1042 if (mux_state) {
1043 ret = mux_state_select(mux_state);
1044 if (ret)
1045 return dev_err_probe(dev, ret, "Failed to select USB mux\n");
1046
1047 ret = devm_add_action_or_reset(dev, rcar_gen3_phy_mux_state_deselect,
1048 mux_state);
1049 if (ret)
1050 return dev_err_probe(dev, ret,
1051 "Failed to register USB mux state deselect\n");
1052 }
1053
1054 if (channel->phy_data->no_adp_ctrl && channel->is_otg_channel) {
1055 ret = rcar_gen3_phy_usb2_vbus_regulator_register(channel);
1056 if (ret)
1057 return ret;
1058 } else {
1059 channel->vbus = devm_regulator_get_optional(dev, "vbus");
1060 }
1061 if (IS_ERR(channel->vbus)) {
1062 if (PTR_ERR(channel->vbus) == -EPROBE_DEFER)
1063 return PTR_ERR(channel->vbus);
1064
1065 channel->vbus = NULL;
1066 }
1067
1068 irq = platform_get_irq_optional(pdev, 0);
1069 if (irq < 0 && irq != -ENXIO) {
1070 return irq;
1071 } else if (irq > 0) {
1072 INIT_WORK(&channel->work, rcar_gen3_phy_usb2_work);
1073 ret = devm_request_irq(dev, irq, rcar_gen3_phy_usb2_irq,
1074 IRQF_SHARED, dev_name(dev), channel);
1075 if (ret < 0)
1076 return dev_err_probe(dev, ret,
1077 "Failed to request irq (%d)\n",
1078 irq);
1079 }
1080
1081 provider = devm_of_phy_provider_register(dev, rcar_gen3_phy_usb2_xlate);
1082 if (IS_ERR(provider)) {
1083 return dev_err_probe(dev, PTR_ERR(provider),
1084 "Failed to register PHY provider\n");
1085 } else if (channel->is_otg_channel) {
1086 ret = device_create_file(dev, &dev_attr_role);
1087 if (ret < 0)
1088 return ret;
1089 }
1090
1091 return 0;
1092 }
1093
1094 static void rcar_gen3_phy_usb2_remove(struct platform_device *pdev)
1095 {
1096 struct rcar_gen3_chan *channel = platform_get_drvdata(pdev);
1097
1098 if (channel->is_otg_channel)
1099 device_remove_file(&pdev->dev, &dev_attr_role);
1100 }
1101
1102 static int rcar_gen3_phy_usb2_suspend(struct device *dev)
1103 {
1104 struct rcar_gen3_chan *channel = dev_get_drvdata(dev);
1105
1106 return reset_control_assert(channel->rstc);
1107 }
1108
1109 static int rcar_gen3_phy_usb2_resume(struct device *dev)
1110 {
1111 struct rcar_gen3_chan *channel = dev_get_drvdata(dev);
1112 int ret;
1113
1114 ret = reset_control_deassert(channel->rstc);
1115 if (ret)
1116 return ret;
1117
1118 ret = rcar_gen3_phy_usb2_init_bus(channel);
1119 if (ret)
1120 reset_control_assert(channel->rstc);
1121
1122 return ret;
1123 }
1124
1125 static DEFINE_SIMPLE_DEV_PM_OPS(rcar_gen3_phy_usb2_pm_ops,
1126 rcar_gen3_phy_usb2_suspend,
1127 rcar_gen3_phy_usb2_resume);
1128
1129 static struct platform_driver rcar_gen3_phy_usb2_driver = {
1130 .driver = {
1131 .name = "phy_rcar_gen3_usb2",
1132 .of_match_table = rcar_gen3_phy_usb2_match_table,
1133 .pm = pm_ptr(&rcar_gen3_phy_usb2_pm_ops),
1134 },
1135 .probe = rcar_gen3_phy_usb2_probe,
1136 .remove = rcar_gen3_phy_usb2_remove,
1137 };
1138 module_platform_driver(rcar_gen3_phy_usb2_driver);
1139
1140 MODULE_LICENSE("GPL v2");
1141 MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY");
1142 MODULE_AUTHOR("Yoshihiro Shimoda <[email protected]>");