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