1c32f3fc06893991e13d8baf6371bf3c659f9b3f
[oweals/u-boot.git] / drivers / usb / musb-new / sunxi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Allwinner SUNXI "glue layer"
4  *
5  * Copyright © 2015 Hans de Goede <hdegoede@redhat.com>
6  * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
7  *
8  * Based on the sw_usb "Allwinner OTG Dual Role Controller" code.
9  *  Copyright 2007-2012 (C) Allwinner Technology Co., Ltd.
10  *  javen <javen@allwinnertech.com>
11  *
12  * Based on the DA8xx "glue layer" code.
13  *  Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
14  *  Copyright (C) 2005-2006 by Texas Instruments
15  *
16  * This file is part of the Inventra Controller Driver for Linux.
17  */
18 #include <common.h>
19 #include <clk.h>
20 #include <dm.h>
21 #include <generic-phy.h>
22 #include <log.h>
23 #include <malloc.h>
24 #include <phy-sun4i-usb.h>
25 #include <reset.h>
26 #include <asm/arch/cpu.h>
27 #include <asm/arch/clock.h>
28 #include <asm/arch/gpio.h>
29 #include <asm-generic/gpio.h>
30 #include <dm/device_compat.h>
31 #include <dm/lists.h>
32 #include <dm/root.h>
33 #include <linux/usb/musb.h>
34 #include "linux-compat.h"
35 #include "musb_core.h"
36 #include "musb_uboot.h"
37
38 /******************************************************************************
39  ******************************************************************************
40  * From the Allwinner driver
41  ******************************************************************************
42  ******************************************************************************/
43
44 /******************************************************************************
45  * From include/sunxi_usb_bsp.h
46  ******************************************************************************/
47
48 /* reg offsets */
49 #define  USBC_REG_o_ISCR        0x0400
50 #define  USBC_REG_o_PHYCTL      0x0404
51 #define  USBC_REG_o_PHYBIST     0x0408
52 #define  USBC_REG_o_PHYTUNE     0x040c
53
54 #define  USBC_REG_o_VEND0       0x0043
55
56 /* Interface Status and Control */
57 #define  USBC_BP_ISCR_VBUS_VALID_FROM_DATA      30
58 #define  USBC_BP_ISCR_VBUS_VALID_FROM_VBUS      29
59 #define  USBC_BP_ISCR_EXT_ID_STATUS             28
60 #define  USBC_BP_ISCR_EXT_DM_STATUS             27
61 #define  USBC_BP_ISCR_EXT_DP_STATUS             26
62 #define  USBC_BP_ISCR_MERGED_VBUS_STATUS        25
63 #define  USBC_BP_ISCR_MERGED_ID_STATUS          24
64
65 #define  USBC_BP_ISCR_ID_PULLUP_EN              17
66 #define  USBC_BP_ISCR_DPDM_PULLUP_EN            16
67 #define  USBC_BP_ISCR_FORCE_ID                  14
68 #define  USBC_BP_ISCR_FORCE_VBUS_VALID          12
69 #define  USBC_BP_ISCR_VBUS_VALID_SRC            10
70
71 #define  USBC_BP_ISCR_HOSC_EN                   7
72 #define  USBC_BP_ISCR_VBUS_CHANGE_DETECT        6
73 #define  USBC_BP_ISCR_ID_CHANGE_DETECT          5
74 #define  USBC_BP_ISCR_DPDM_CHANGE_DETECT        4
75 #define  USBC_BP_ISCR_IRQ_ENABLE                3
76 #define  USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN     2
77 #define  USBC_BP_ISCR_ID_CHANGE_DETECT_EN       1
78 #define  USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN     0
79
80 /******************************************************************************
81  * From usbc/usbc.c
82  ******************************************************************************/
83
84 #define OFF_SUN6I_AHB_RESET0    0x2c0
85
86 struct sunxi_musb_config {
87         struct musb_hdrc_config *config;
88 };
89
90 struct sunxi_glue {
91         struct musb_host_data mdata;
92         struct clk clk;
93         struct reset_ctl rst;
94         struct sunxi_musb_config *cfg;
95         struct device dev;
96         struct phy phy;
97 };
98 #define to_sunxi_glue(d)        container_of(d, struct sunxi_glue, dev)
99
100 static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
101 {
102         u32 temp = reg_val;
103
104         temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
105         temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
106         temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
107
108         return temp;
109 }
110
111 static void USBC_EnableIdPullUp(__iomem void *base)
112 {
113         u32 reg_val;
114
115         reg_val = musb_readl(base, USBC_REG_o_ISCR);
116         reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
117         reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
118         musb_writel(base, USBC_REG_o_ISCR, reg_val);
119 }
120
121 static void USBC_EnableDpDmPullUp(__iomem void *base)
122 {
123         u32 reg_val;
124
125         reg_val = musb_readl(base, USBC_REG_o_ISCR);
126         reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
127         reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
128         musb_writel(base, USBC_REG_o_ISCR, reg_val);
129 }
130
131 static void USBC_ForceIdToLow(__iomem void *base)
132 {
133         u32 reg_val;
134
135         reg_val = musb_readl(base, USBC_REG_o_ISCR);
136         reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
137         reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
138         reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
139         musb_writel(base, USBC_REG_o_ISCR, reg_val);
140 }
141
142 static void USBC_ForceIdToHigh(__iomem void *base)
143 {
144         u32 reg_val;
145
146         reg_val = musb_readl(base, USBC_REG_o_ISCR);
147         reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
148         reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
149         reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
150         musb_writel(base, USBC_REG_o_ISCR, reg_val);
151 }
152
153 static void USBC_ForceVbusValidToLow(__iomem void *base)
154 {
155         u32 reg_val;
156
157         reg_val = musb_readl(base, USBC_REG_o_ISCR);
158         reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
159         reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
160         reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
161         musb_writel(base, USBC_REG_o_ISCR, reg_val);
162 }
163
164 static void USBC_ForceVbusValidToHigh(__iomem void *base)
165 {
166         u32 reg_val;
167
168         reg_val = musb_readl(base, USBC_REG_o_ISCR);
169         reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
170         reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
171         reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
172         musb_writel(base, USBC_REG_o_ISCR, reg_val);
173 }
174
175 static void USBC_ConfigFIFO_Base(void)
176 {
177         u32 reg_value;
178
179         /* config usb fifo, 8kb mode */
180         reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
181         reg_value &= ~(0x03 << 0);
182         reg_value |= BIT(0);
183         writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
184 }
185
186 /******************************************************************************
187  * Needed for the DFU polling magic
188  ******************************************************************************/
189
190 static u8 last_int_usb;
191
192 bool dfu_usb_get_reset(void)
193 {
194         return !!(last_int_usb & MUSB_INTR_RESET);
195 }
196
197 /******************************************************************************
198  * MUSB Glue code
199  ******************************************************************************/
200
201 static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
202 {
203         struct musb             *musb = __hci;
204         irqreturn_t             retval = IRQ_NONE;
205
206         /* read and flush interrupts */
207         musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
208         last_int_usb = musb->int_usb;
209         if (musb->int_usb)
210                 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
211         musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
212         if (musb->int_tx)
213                 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
214         musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
215         if (musb->int_rx)
216                 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
217
218         if (musb->int_usb || musb->int_tx || musb->int_rx)
219                 retval |= musb_interrupt(musb);
220
221         return retval;
222 }
223
224 /* musb_core does not call enable / disable in a balanced manner <sigh> */
225 static bool enabled = false;
226
227 static int sunxi_musb_enable(struct musb *musb)
228 {
229         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
230         int ret;
231
232         pr_debug("%s():\n", __func__);
233
234         musb_ep_select(musb->mregs, 0);
235         musb_writeb(musb->mregs, MUSB_FADDR, 0);
236
237         if (enabled)
238                 return 0;
239
240         /* select PIO mode */
241         musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
242
243         if (is_host_enabled(musb)) {
244                 ret = sun4i_usb_phy_vbus_detect(&glue->phy);
245                 if (ret == 1) {
246                         printf("A charger is plugged into the OTG: ");
247                         return -ENODEV;
248                 }
249
250                 ret = sun4i_usb_phy_id_detect(&glue->phy);
251                 if (ret == 1) {
252                         printf("No host cable detected: ");
253                         return -ENODEV;
254                 }
255
256                 ret = generic_phy_power_on(&glue->phy);
257                 if (ret) {
258                         pr_err("failed to power on USB PHY\n");
259                         return ret;
260                 }
261         }
262
263         USBC_ForceVbusValidToHigh(musb->mregs);
264
265         enabled = true;
266         return 0;
267 }
268
269 static void sunxi_musb_disable(struct musb *musb)
270 {
271         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
272         int ret;
273
274         pr_debug("%s():\n", __func__);
275
276         if (!enabled)
277                 return;
278
279         if (is_host_enabled(musb)) {
280                 ret = generic_phy_power_off(&glue->phy);
281                 if (ret) {
282                         pr_err("failed to power off USB PHY\n");
283                         return;
284                 }
285         }
286
287         USBC_ForceVbusValidToLow(musb->mregs);
288         mdelay(200); /* Wait for the current session to timeout */
289
290         enabled = false;
291 }
292
293 static int sunxi_musb_init(struct musb *musb)
294 {
295         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
296         int ret;
297
298         pr_debug("%s():\n", __func__);
299
300         ret = clk_enable(&glue->clk);
301         if (ret) {
302                 dev_err(dev, "failed to enable clock\n");
303                 return ret;
304         }
305
306         if (reset_valid(&glue->rst)) {
307                 ret = reset_deassert(&glue->rst);
308                 if (ret) {
309                         dev_err(dev, "failed to deassert reset\n");
310                         goto err_clk;
311                 }
312         }
313
314         ret = generic_phy_init(&glue->phy);
315         if (ret) {
316                 dev_err(dev, "failed to init USB PHY\n");
317                 goto err_rst;
318         }
319
320         musb->isr = sunxi_musb_interrupt;
321
322         USBC_ConfigFIFO_Base();
323         USBC_EnableDpDmPullUp(musb->mregs);
324         USBC_EnableIdPullUp(musb->mregs);
325
326         if (is_host_enabled(musb)) {
327                 /* Host mode */
328                 USBC_ForceIdToLow(musb->mregs);
329         } else {
330                 /* Peripheral mode */
331                 USBC_ForceIdToHigh(musb->mregs);
332         }
333         USBC_ForceVbusValidToHigh(musb->mregs);
334
335         return 0;
336
337 err_rst:
338         if (reset_valid(&glue->rst))
339                 reset_assert(&glue->rst);
340 err_clk:
341         clk_disable(&glue->clk);
342         return ret;
343 }
344
345 static int sunxi_musb_exit(struct musb *musb)
346 {
347         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
348         int ret = 0;
349
350         if (generic_phy_valid(&glue->phy)) {
351                 ret = generic_phy_exit(&glue->phy);
352                 if (ret) {
353                         dev_err(dev, "failed to power off usb phy\n");
354                         return ret;
355                 }
356         }
357
358         if (reset_valid(&glue->rst))
359                 reset_assert(&glue->rst);
360         clk_disable(&glue->clk);
361
362         return 0;
363 }
364
365 static void sunxi_musb_pre_root_reset_end(struct musb *musb)
366 {
367         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
368
369         sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
370 }
371
372 static void sunxi_musb_post_root_reset_end(struct musb *musb)
373 {
374         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
375
376         sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
377 }
378
379 static const struct musb_platform_ops sunxi_musb_ops = {
380         .init           = sunxi_musb_init,
381         .exit           = sunxi_musb_exit,
382         .enable         = sunxi_musb_enable,
383         .disable        = sunxi_musb_disable,
384         .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
385         .post_root_reset_end = sunxi_musb_post_root_reset_end,
386 };
387
388 /* Allwinner OTG supports up to 5 endpoints */
389 #define SUNXI_MUSB_MAX_EP_NUM           6
390 #define SUNXI_MUSB_RAM_BITS             11
391
392 static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
393         MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
394         MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
395         MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
396         MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
397         MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
398         MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
399         MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
400         MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
401         MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
402         MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
403 };
404
405 /* H3/V3s OTG supports only 4 endpoints */
406 #define SUNXI_MUSB_MAX_EP_NUM_H3        5
407
408 static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
409         MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
410         MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
411         MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
412         MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
413         MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
414         MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
415         MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
416         MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
417 };
418
419 static struct musb_hdrc_config musb_config = {
420         .fifo_cfg       = sunxi_musb_mode_cfg,
421         .fifo_cfg_size  = ARRAY_SIZE(sunxi_musb_mode_cfg),
422         .multipoint     = true,
423         .dyn_fifo       = true,
424         .num_eps        = SUNXI_MUSB_MAX_EP_NUM,
425         .ram_bits       = SUNXI_MUSB_RAM_BITS,
426 };
427
428 static struct musb_hdrc_config musb_config_h3 = {
429         .fifo_cfg       = sunxi_musb_mode_cfg_h3,
430         .fifo_cfg_size  = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
431         .multipoint     = true,
432         .dyn_fifo       = true,
433         .soft_con       = true,
434         .num_eps        = SUNXI_MUSB_MAX_EP_NUM_H3,
435         .ram_bits       = SUNXI_MUSB_RAM_BITS,
436 };
437
438 static int musb_usb_probe(struct udevice *dev)
439 {
440         struct sunxi_glue *glue = dev_get_priv(dev);
441         struct musb_host_data *host = &glue->mdata;
442         struct musb_hdrc_platform_data pdata;
443         void *base = dev_read_addr_ptr(dev);
444         int ret;
445
446 #ifdef CONFIG_USB_MUSB_HOST
447         struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
448 #endif
449
450         if (!base)
451                 return -EINVAL;
452
453         glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
454         if (!glue->cfg)
455                 return -EINVAL;
456
457         ret = clk_get_by_index(dev, 0, &glue->clk);
458         if (ret) {
459                 dev_err(dev, "failed to get clock\n");
460                 return ret;
461         }
462
463         ret = reset_get_by_index(dev, 0, &glue->rst);
464         if (ret && ret != -ENOENT) {
465                 dev_err(dev, "failed to get reset\n");
466                 return ret;
467         }
468
469         ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
470         if (ret) {
471                 pr_err("failed to get usb PHY\n");
472                 return ret;
473         }
474
475         memset(&pdata, 0, sizeof(pdata));
476         pdata.power = 250;
477         pdata.platform_ops = &sunxi_musb_ops;
478         pdata.config = glue->cfg->config;
479
480 #ifdef CONFIG_USB_MUSB_HOST
481         priv->desc_before_addr = true;
482
483         pdata.mode = MUSB_HOST;
484         host->host = musb_init_controller(&pdata, &glue->dev, base);
485         if (!host->host)
486                 return -EIO;
487
488         ret = musb_lowlevel_init(host);
489         if (!ret)
490                 printf("Allwinner mUSB OTG (Host)\n");
491 #else
492         pdata.mode = MUSB_PERIPHERAL;
493         host->host = musb_register(&pdata, &glue->dev, base);
494         if (!host->host)
495                 return -EIO;
496
497         printf("Allwinner mUSB OTG (Peripheral)\n");
498 #endif
499
500         return ret;
501 }
502
503 static int musb_usb_remove(struct udevice *dev)
504 {
505         struct sunxi_glue *glue = dev_get_priv(dev);
506         struct musb_host_data *host = &glue->mdata;
507
508         musb_stop(host->host);
509         free(host->host);
510         host->host = NULL;
511
512         return 0;
513 }
514
515 static const struct sunxi_musb_config sun4i_a10_cfg = {
516         .config = &musb_config,
517 };
518
519 static const struct sunxi_musb_config sun6i_a31_cfg = {
520         .config = &musb_config,
521 };
522
523 static const struct sunxi_musb_config sun8i_h3_cfg = {
524         .config = &musb_config_h3,
525 };
526
527 static const struct udevice_id sunxi_musb_ids[] = {
528         { .compatible = "allwinner,sun4i-a10-musb",
529                         .data = (ulong)&sun4i_a10_cfg },
530         { .compatible = "allwinner,sun6i-a31-musb",
531                         .data = (ulong)&sun6i_a31_cfg },
532         { .compatible = "allwinner,sun8i-a33-musb",
533                         .data = (ulong)&sun6i_a31_cfg },
534         { .compatible = "allwinner,sun8i-h3-musb",
535                         .data = (ulong)&sun8i_h3_cfg },
536         { }
537 };
538
539 U_BOOT_DRIVER(usb_musb) = {
540         .name           = "sunxi-musb",
541 #ifdef CONFIG_USB_MUSB_HOST
542         .id             = UCLASS_USB,
543 #else
544         .id             = UCLASS_USB_GADGET_GENERIC,
545 #endif
546         .of_match       = sunxi_musb_ids,
547         .probe          = musb_usb_probe,
548         .remove         = musb_usb_remove,
549 #ifdef CONFIG_USB_MUSB_HOST
550         .ops            = &musb_usb_ops,
551 #endif
552         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
553         .priv_auto_alloc_size = sizeof(struct sunxi_glue),
554 };