1 // SPDX-License-Identifier: GPL-2.0
3 * Allwinner SUNXI "glue layer"
5 * Copyright © 2015 Hans de Goede <hdegoede@redhat.com>
6 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
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>
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
16 * This file is part of the Inventra Controller Driver for Linux.
21 #include <generic-phy.h>
22 #include <phy-sun4i-usb.h>
24 #include <asm/arch/cpu.h>
25 #include <asm/arch/clock.h>
26 #include <asm/arch/gpio.h>
27 #include <asm-generic/gpio.h>
30 #include <linux/usb/musb.h>
31 #include "linux-compat.h"
32 #include "musb_core.h"
33 #include "musb_uboot.h"
35 /******************************************************************************
36 ******************************************************************************
37 * From the Allwinner driver
38 ******************************************************************************
39 ******************************************************************************/
41 /******************************************************************************
42 * From include/sunxi_usb_bsp.h
43 ******************************************************************************/
46 #define USBC_REG_o_ISCR 0x0400
47 #define USBC_REG_o_PHYCTL 0x0404
48 #define USBC_REG_o_PHYBIST 0x0408
49 #define USBC_REG_o_PHYTUNE 0x040c
51 #define USBC_REG_o_VEND0 0x0043
53 /* Interface Status and Control */
54 #define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
55 #define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
56 #define USBC_BP_ISCR_EXT_ID_STATUS 28
57 #define USBC_BP_ISCR_EXT_DM_STATUS 27
58 #define USBC_BP_ISCR_EXT_DP_STATUS 26
59 #define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
60 #define USBC_BP_ISCR_MERGED_ID_STATUS 24
62 #define USBC_BP_ISCR_ID_PULLUP_EN 17
63 #define USBC_BP_ISCR_DPDM_PULLUP_EN 16
64 #define USBC_BP_ISCR_FORCE_ID 14
65 #define USBC_BP_ISCR_FORCE_VBUS_VALID 12
66 #define USBC_BP_ISCR_VBUS_VALID_SRC 10
68 #define USBC_BP_ISCR_HOSC_EN 7
69 #define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
70 #define USBC_BP_ISCR_ID_CHANGE_DETECT 5
71 #define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
72 #define USBC_BP_ISCR_IRQ_ENABLE 3
73 #define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
74 #define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
75 #define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
77 /******************************************************************************
79 ******************************************************************************/
81 #define OFF_SUN6I_AHB_RESET0 0x2c0
83 struct sunxi_musb_config {
84 struct musb_hdrc_config *config;
88 struct musb_host_data mdata;
91 struct sunxi_musb_config *cfg;
95 #define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
97 static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
101 temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
102 temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
103 temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
108 static void USBC_EnableIdPullUp(__iomem void *base)
112 reg_val = musb_readl(base, USBC_REG_o_ISCR);
113 reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
114 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
115 musb_writel(base, USBC_REG_o_ISCR, reg_val);
118 static void USBC_EnableDpDmPullUp(__iomem void *base)
122 reg_val = musb_readl(base, USBC_REG_o_ISCR);
123 reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
124 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
125 musb_writel(base, USBC_REG_o_ISCR, reg_val);
128 static void USBC_ForceIdToLow(__iomem void *base)
132 reg_val = musb_readl(base, USBC_REG_o_ISCR);
133 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
134 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
135 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
136 musb_writel(base, USBC_REG_o_ISCR, reg_val);
139 static void USBC_ForceIdToHigh(__iomem void *base)
143 reg_val = musb_readl(base, USBC_REG_o_ISCR);
144 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
145 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
146 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
147 musb_writel(base, USBC_REG_o_ISCR, reg_val);
150 static void USBC_ForceVbusValidToLow(__iomem void *base)
154 reg_val = musb_readl(base, USBC_REG_o_ISCR);
155 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
156 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
157 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
158 musb_writel(base, USBC_REG_o_ISCR, reg_val);
161 static void USBC_ForceVbusValidToHigh(__iomem void *base)
165 reg_val = musb_readl(base, USBC_REG_o_ISCR);
166 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
167 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
168 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
169 musb_writel(base, USBC_REG_o_ISCR, reg_val);
172 static void USBC_ConfigFIFO_Base(void)
176 /* config usb fifo, 8kb mode */
177 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
178 reg_value &= ~(0x03 << 0);
180 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
183 /******************************************************************************
184 * Needed for the DFU polling magic
185 ******************************************************************************/
187 static u8 last_int_usb;
189 bool dfu_usb_get_reset(void)
191 return !!(last_int_usb & MUSB_INTR_RESET);
194 /******************************************************************************
196 ******************************************************************************/
198 static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
200 struct musb *musb = __hci;
201 irqreturn_t retval = IRQ_NONE;
203 /* read and flush interrupts */
204 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
205 last_int_usb = musb->int_usb;
207 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
208 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
210 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
211 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
213 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
215 if (musb->int_usb || musb->int_tx || musb->int_rx)
216 retval |= musb_interrupt(musb);
221 /* musb_core does not call enable / disable in a balanced manner <sigh> */
222 static bool enabled = false;
224 static int sunxi_musb_enable(struct musb *musb)
226 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
229 pr_debug("%s():\n", __func__);
231 musb_ep_select(musb->mregs, 0);
232 musb_writeb(musb->mregs, MUSB_FADDR, 0);
237 /* select PIO mode */
238 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
240 if (is_host_enabled(musb)) {
241 ret = sun4i_usb_phy_vbus_detect(&glue->phy);
243 printf("A charger is plugged into the OTG: ");
247 ret = sun4i_usb_phy_id_detect(&glue->phy);
249 printf("No host cable detected: ");
253 ret = generic_phy_power_on(&glue->phy);
255 pr_err("failed to power on USB PHY\n");
260 USBC_ForceVbusValidToHigh(musb->mregs);
266 static void sunxi_musb_disable(struct musb *musb)
268 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
271 pr_debug("%s():\n", __func__);
276 if (is_host_enabled(musb)) {
277 ret = generic_phy_power_off(&glue->phy);
279 pr_err("failed to power off USB PHY\n");
284 USBC_ForceVbusValidToLow(musb->mregs);
285 mdelay(200); /* Wait for the current session to timeout */
290 static int sunxi_musb_init(struct musb *musb)
292 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
295 pr_debug("%s():\n", __func__);
297 ret = clk_enable(&glue->clk);
299 dev_err(dev, "failed to enable clock\n");
303 if (reset_valid(&glue->rst)) {
304 ret = reset_deassert(&glue->rst);
306 dev_err(dev, "failed to deassert reset\n");
311 ret = generic_phy_init(&glue->phy);
313 dev_err(dev, "failed to init USB PHY\n");
317 musb->isr = sunxi_musb_interrupt;
319 USBC_ConfigFIFO_Base();
320 USBC_EnableDpDmPullUp(musb->mregs);
321 USBC_EnableIdPullUp(musb->mregs);
323 if (is_host_enabled(musb)) {
325 USBC_ForceIdToLow(musb->mregs);
327 /* Peripheral mode */
328 USBC_ForceIdToHigh(musb->mregs);
330 USBC_ForceVbusValidToHigh(musb->mregs);
335 if (reset_valid(&glue->rst))
336 reset_assert(&glue->rst);
338 clk_disable(&glue->clk);
342 static int sunxi_musb_exit(struct musb *musb)
344 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
347 if (generic_phy_valid(&glue->phy)) {
348 ret = generic_phy_exit(&glue->phy);
350 dev_err(dev, "failed to power off usb phy\n");
355 if (reset_valid(&glue->rst))
356 reset_assert(&glue->rst);
357 clk_disable(&glue->clk);
362 static void sunxi_musb_pre_root_reset_end(struct musb *musb)
364 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
366 sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
369 static void sunxi_musb_post_root_reset_end(struct musb *musb)
371 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
373 sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
376 static const struct musb_platform_ops sunxi_musb_ops = {
377 .init = sunxi_musb_init,
378 .exit = sunxi_musb_exit,
379 .enable = sunxi_musb_enable,
380 .disable = sunxi_musb_disable,
381 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
382 .post_root_reset_end = sunxi_musb_post_root_reset_end,
385 /* Allwinner OTG supports up to 5 endpoints */
386 #define SUNXI_MUSB_MAX_EP_NUM 6
387 #define SUNXI_MUSB_RAM_BITS 11
389 static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
390 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
391 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
392 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
393 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
394 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
395 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
396 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
397 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
398 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
399 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
402 /* H3/V3s OTG supports only 4 endpoints */
403 #define SUNXI_MUSB_MAX_EP_NUM_H3 5
405 static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
406 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
407 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
408 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
409 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
410 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
411 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
412 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
413 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
416 static struct musb_hdrc_config musb_config = {
417 .fifo_cfg = sunxi_musb_mode_cfg,
418 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
421 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
422 .ram_bits = SUNXI_MUSB_RAM_BITS,
425 static struct musb_hdrc_config musb_config_h3 = {
426 .fifo_cfg = sunxi_musb_mode_cfg_h3,
427 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
431 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
432 .ram_bits = SUNXI_MUSB_RAM_BITS,
435 static int musb_usb_probe(struct udevice *dev)
437 struct sunxi_glue *glue = dev_get_priv(dev);
438 struct musb_host_data *host = &glue->mdata;
439 struct musb_hdrc_platform_data pdata;
440 void *base = dev_read_addr_ptr(dev);
443 #ifdef CONFIG_USB_MUSB_HOST
444 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
450 glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
454 ret = clk_get_by_index(dev, 0, &glue->clk);
456 dev_err(dev, "failed to get clock\n");
460 ret = reset_get_by_index(dev, 0, &glue->rst);
461 if (ret && ret != -ENOENT) {
462 dev_err(dev, "failed to get reset\n");
466 ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
468 pr_err("failed to get usb PHY\n");
472 memset(&pdata, 0, sizeof(pdata));
474 pdata.platform_ops = &sunxi_musb_ops;
475 pdata.config = glue->cfg->config;
477 #ifdef CONFIG_USB_MUSB_HOST
478 priv->desc_before_addr = true;
480 pdata.mode = MUSB_HOST;
481 host->host = musb_init_controller(&pdata, &glue->dev, base);
485 ret = musb_lowlevel_init(host);
487 printf("Allwinner mUSB OTG (Host)\n");
489 pdata.mode = MUSB_PERIPHERAL;
490 host->host = musb_register(&pdata, &glue->dev, base);
494 printf("Allwinner mUSB OTG (Peripheral)\n");
500 static int musb_usb_remove(struct udevice *dev)
502 struct sunxi_glue *glue = dev_get_priv(dev);
503 struct musb_host_data *host = &glue->mdata;
505 musb_stop(host->host);
512 static const struct sunxi_musb_config sun4i_a10_cfg = {
513 .config = &musb_config,
516 static const struct sunxi_musb_config sun6i_a31_cfg = {
517 .config = &musb_config,
520 static const struct sunxi_musb_config sun8i_h3_cfg = {
521 .config = &musb_config_h3,
524 static const struct udevice_id sunxi_musb_ids[] = {
525 { .compatible = "allwinner,sun4i-a10-musb",
526 .data = (ulong)&sun4i_a10_cfg },
527 { .compatible = "allwinner,sun6i-a31-musb",
528 .data = (ulong)&sun6i_a31_cfg },
529 { .compatible = "allwinner,sun8i-a33-musb",
530 .data = (ulong)&sun6i_a31_cfg },
531 { .compatible = "allwinner,sun8i-h3-musb",
532 .data = (ulong)&sun8i_h3_cfg },
536 U_BOOT_DRIVER(usb_musb) = {
537 .name = "sunxi-musb",
538 #ifdef CONFIG_USB_MUSB_HOST
541 .id = UCLASS_USB_GADGET_GENERIC,
543 .of_match = sunxi_musb_ids,
544 .probe = musb_usb_probe,
545 .remove = musb_usb_remove,
546 #ifdef CONFIG_USB_MUSB_HOST
547 .ops = &musb_usb_ops,
549 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
550 .priv_auto_alloc_size = sizeof(struct sunxi_glue),