183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
228a15ef7SHans de Goede /*
328a15ef7SHans de Goede * Allwinner SUNXI "glue layer"
428a15ef7SHans de Goede *
528a15ef7SHans de Goede * Copyright © 2015 Hans de Goede <hdegoede@redhat.com>
628a15ef7SHans de Goede * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
728a15ef7SHans de Goede *
828a15ef7SHans de Goede * Based on the sw_usb "Allwinner OTG Dual Role Controller" code.
928a15ef7SHans de Goede * Copyright 2007-2012 (C) Allwinner Technology Co., Ltd.
1028a15ef7SHans de Goede * javen <javen@allwinnertech.com>
1128a15ef7SHans de Goede *
1228a15ef7SHans de Goede * Based on the DA8xx "glue layer" code.
1328a15ef7SHans de Goede * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
1428a15ef7SHans de Goede * Copyright (C) 2005-2006 by Texas Instruments
1528a15ef7SHans de Goede *
1628a15ef7SHans de Goede * This file is part of the Inventra Controller Driver for Linux.
1728a15ef7SHans de Goede */
1828a15ef7SHans de Goede #include <common.h>
19*b9aa0a93SJagan Teki #include <clk.h>
209d922450SSimon Glass #include <dm.h>
21dd322817SJagan Teki #include <generic-phy.h>
22dd322817SJagan Teki #include <phy-sun4i-usb.h>
23*b9aa0a93SJagan Teki #include <reset.h>
2428a15ef7SHans de Goede #include <asm/arch/cpu.h>
25375de017SHans de Goede #include <asm/arch/clock.h>
2652defe8fSHans de Goede #include <asm/arch/gpio.h>
2752defe8fSHans de Goede #include <asm-generic/gpio.h>
2891183babSHans de Goede #include <dm/lists.h>
2991183babSHans de Goede #include <dm/root.h>
30d42faf31SHans de Goede #include <linux/usb/musb.h>
3128a15ef7SHans de Goede #include "linux-compat.h"
3228a15ef7SHans de Goede #include "musb_core.h"
3391183babSHans de Goede #include "musb_uboot.h"
3428a15ef7SHans de Goede
3528a15ef7SHans de Goede /******************************************************************************
3628a15ef7SHans de Goede ******************************************************************************
3728a15ef7SHans de Goede * From the Allwinner driver
3828a15ef7SHans de Goede ******************************************************************************
3928a15ef7SHans de Goede ******************************************************************************/
4028a15ef7SHans de Goede
4128a15ef7SHans de Goede /******************************************************************************
4228a15ef7SHans de Goede * From include/sunxi_usb_bsp.h
4328a15ef7SHans de Goede ******************************************************************************/
4428a15ef7SHans de Goede
4528a15ef7SHans de Goede /* reg offsets */
4628a15ef7SHans de Goede #define USBC_REG_o_ISCR 0x0400
4728a15ef7SHans de Goede #define USBC_REG_o_PHYCTL 0x0404
4828a15ef7SHans de Goede #define USBC_REG_o_PHYBIST 0x0408
4928a15ef7SHans de Goede #define USBC_REG_o_PHYTUNE 0x040c
5028a15ef7SHans de Goede
5128a15ef7SHans de Goede #define USBC_REG_o_VEND0 0x0043
5228a15ef7SHans de Goede
5328a15ef7SHans de Goede /* Interface Status and Control */
5428a15ef7SHans de Goede #define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
5528a15ef7SHans de Goede #define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
5628a15ef7SHans de Goede #define USBC_BP_ISCR_EXT_ID_STATUS 28
5728a15ef7SHans de Goede #define USBC_BP_ISCR_EXT_DM_STATUS 27
5828a15ef7SHans de Goede #define USBC_BP_ISCR_EXT_DP_STATUS 26
5928a15ef7SHans de Goede #define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
6028a15ef7SHans de Goede #define USBC_BP_ISCR_MERGED_ID_STATUS 24
6128a15ef7SHans de Goede
6228a15ef7SHans de Goede #define USBC_BP_ISCR_ID_PULLUP_EN 17
6328a15ef7SHans de Goede #define USBC_BP_ISCR_DPDM_PULLUP_EN 16
6428a15ef7SHans de Goede #define USBC_BP_ISCR_FORCE_ID 14
6528a15ef7SHans de Goede #define USBC_BP_ISCR_FORCE_VBUS_VALID 12
6628a15ef7SHans de Goede #define USBC_BP_ISCR_VBUS_VALID_SRC 10
6728a15ef7SHans de Goede
6828a15ef7SHans de Goede #define USBC_BP_ISCR_HOSC_EN 7
6928a15ef7SHans de Goede #define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
7028a15ef7SHans de Goede #define USBC_BP_ISCR_ID_CHANGE_DETECT 5
7128a15ef7SHans de Goede #define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
7228a15ef7SHans de Goede #define USBC_BP_ISCR_IRQ_ENABLE 3
7328a15ef7SHans de Goede #define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
7428a15ef7SHans de Goede #define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
7528a15ef7SHans de Goede #define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
7628a15ef7SHans de Goede
7728a15ef7SHans de Goede /******************************************************************************
7828a15ef7SHans de Goede * From usbc/usbc.c
7928a15ef7SHans de Goede ******************************************************************************/
8028a15ef7SHans de Goede
811034bcc2SJagan Teki #define OFF_SUN6I_AHB_RESET0 0x2c0
821034bcc2SJagan Teki
8397202dd6SJagan Teki struct sunxi_musb_config {
8497202dd6SJagan Teki struct musb_hdrc_config *config;
8597202dd6SJagan Teki };
8697202dd6SJagan Teki
87831cc98bSJagan Teki struct sunxi_glue {
88831cc98bSJagan Teki struct musb_host_data mdata;
89*b9aa0a93SJagan Teki struct clk clk;
90*b9aa0a93SJagan Teki struct reset_ctl rst;
9197202dd6SJagan Teki struct sunxi_musb_config *cfg;
92831cc98bSJagan Teki struct device dev;
93622fd2b9SJagan Teki struct phy phy;
94831cc98bSJagan Teki };
95831cc98bSJagan Teki #define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
96831cc98bSJagan Teki
USBC_WakeUp_ClearChangeDetect(u32 reg_val)9728a15ef7SHans de Goede static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
9828a15ef7SHans de Goede {
9928a15ef7SHans de Goede u32 temp = reg_val;
10028a15ef7SHans de Goede
1015c5fe883SJagan Teki temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
1025c5fe883SJagan Teki temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
1035c5fe883SJagan Teki temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
10428a15ef7SHans de Goede
10528a15ef7SHans de Goede return temp;
10628a15ef7SHans de Goede }
10728a15ef7SHans de Goede
USBC_EnableIdPullUp(__iomem void * base)10828a15ef7SHans de Goede static void USBC_EnableIdPullUp(__iomem void *base)
10928a15ef7SHans de Goede {
11028a15ef7SHans de Goede u32 reg_val;
11128a15ef7SHans de Goede
11228a15ef7SHans de Goede reg_val = musb_readl(base, USBC_REG_o_ISCR);
1135c5fe883SJagan Teki reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
11428a15ef7SHans de Goede reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
11528a15ef7SHans de Goede musb_writel(base, USBC_REG_o_ISCR, reg_val);
11628a15ef7SHans de Goede }
11728a15ef7SHans de Goede
USBC_EnableDpDmPullUp(__iomem void * base)11828a15ef7SHans de Goede static void USBC_EnableDpDmPullUp(__iomem void *base)
11928a15ef7SHans de Goede {
12028a15ef7SHans de Goede u32 reg_val;
12128a15ef7SHans de Goede
12228a15ef7SHans de Goede reg_val = musb_readl(base, USBC_REG_o_ISCR);
1235c5fe883SJagan Teki reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
12428a15ef7SHans de Goede reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
12528a15ef7SHans de Goede musb_writel(base, USBC_REG_o_ISCR, reg_val);
12628a15ef7SHans de Goede }
12728a15ef7SHans de Goede
USBC_ForceIdToLow(__iomem void * base)12828a15ef7SHans de Goede static void USBC_ForceIdToLow(__iomem void *base)
12928a15ef7SHans de Goede {
13028a15ef7SHans de Goede u32 reg_val;
13128a15ef7SHans de Goede
13228a15ef7SHans de Goede reg_val = musb_readl(base, USBC_REG_o_ISCR);
13328a15ef7SHans de Goede reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
13428a15ef7SHans de Goede reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
13528a15ef7SHans de Goede reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
13628a15ef7SHans de Goede musb_writel(base, USBC_REG_o_ISCR, reg_val);
13728a15ef7SHans de Goede }
13828a15ef7SHans de Goede
USBC_ForceIdToHigh(__iomem void * base)13928a15ef7SHans de Goede static void USBC_ForceIdToHigh(__iomem void *base)
14028a15ef7SHans de Goede {
14128a15ef7SHans de Goede u32 reg_val;
14228a15ef7SHans de Goede
14328a15ef7SHans de Goede reg_val = musb_readl(base, USBC_REG_o_ISCR);
14428a15ef7SHans de Goede reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
14528a15ef7SHans de Goede reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
14628a15ef7SHans de Goede reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
14728a15ef7SHans de Goede musb_writel(base, USBC_REG_o_ISCR, reg_val);
14828a15ef7SHans de Goede }
14928a15ef7SHans de Goede
USBC_ForceVbusValidToLow(__iomem void * base)150e1abfa43SHans de Goede static void USBC_ForceVbusValidToLow(__iomem void *base)
151e1abfa43SHans de Goede {
152e1abfa43SHans de Goede u32 reg_val;
153e1abfa43SHans de Goede
154e1abfa43SHans de Goede reg_val = musb_readl(base, USBC_REG_o_ISCR);
155e1abfa43SHans de Goede reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
156e1abfa43SHans de Goede reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
157e1abfa43SHans de Goede reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
158e1abfa43SHans de Goede musb_writel(base, USBC_REG_o_ISCR, reg_val);
159e1abfa43SHans de Goede }
160e1abfa43SHans de Goede
USBC_ForceVbusValidToHigh(__iomem void * base)16128a15ef7SHans de Goede static void USBC_ForceVbusValidToHigh(__iomem void *base)
16228a15ef7SHans de Goede {
16328a15ef7SHans de Goede u32 reg_val;
16428a15ef7SHans de Goede
16528a15ef7SHans de Goede reg_val = musb_readl(base, USBC_REG_o_ISCR);
16628a15ef7SHans de Goede reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
16728a15ef7SHans de Goede reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
16828a15ef7SHans de Goede reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
16928a15ef7SHans de Goede musb_writel(base, USBC_REG_o_ISCR, reg_val);
17028a15ef7SHans de Goede }
17128a15ef7SHans de Goede
USBC_ConfigFIFO_Base(void)17228a15ef7SHans de Goede static void USBC_ConfigFIFO_Base(void)
17328a15ef7SHans de Goede {
17428a15ef7SHans de Goede u32 reg_value;
17528a15ef7SHans de Goede
17628a15ef7SHans de Goede /* config usb fifo, 8kb mode */
17728a15ef7SHans de Goede reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
17828a15ef7SHans de Goede reg_value &= ~(0x03 << 0);
1795c5fe883SJagan Teki reg_value |= BIT(0);
18028a15ef7SHans de Goede writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
18128a15ef7SHans de Goede }
18228a15ef7SHans de Goede
18328a15ef7SHans de Goede /******************************************************************************
1846047a3a9SSiarhei Siamashka * Needed for the DFU polling magic
1856047a3a9SSiarhei Siamashka ******************************************************************************/
1866047a3a9SSiarhei Siamashka
1876047a3a9SSiarhei Siamashka static u8 last_int_usb;
1886047a3a9SSiarhei Siamashka
dfu_usb_get_reset(void)1896047a3a9SSiarhei Siamashka bool dfu_usb_get_reset(void)
1906047a3a9SSiarhei Siamashka {
1916047a3a9SSiarhei Siamashka return !!(last_int_usb & MUSB_INTR_RESET);
1926047a3a9SSiarhei Siamashka }
1936047a3a9SSiarhei Siamashka
1946047a3a9SSiarhei Siamashka /******************************************************************************
19528a15ef7SHans de Goede * MUSB Glue code
19628a15ef7SHans de Goede ******************************************************************************/
19728a15ef7SHans de Goede
sunxi_musb_interrupt(int irq,void * __hci)19828a15ef7SHans de Goede static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
19928a15ef7SHans de Goede {
20028a15ef7SHans de Goede struct musb *musb = __hci;
20128a15ef7SHans de Goede irqreturn_t retval = IRQ_NONE;
20228a15ef7SHans de Goede
20328a15ef7SHans de Goede /* read and flush interrupts */
20428a15ef7SHans de Goede musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
2056047a3a9SSiarhei Siamashka last_int_usb = musb->int_usb;
20628a15ef7SHans de Goede if (musb->int_usb)
20728a15ef7SHans de Goede musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
20828a15ef7SHans de Goede musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
20928a15ef7SHans de Goede if (musb->int_tx)
21028a15ef7SHans de Goede musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
21128a15ef7SHans de Goede musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
21228a15ef7SHans de Goede if (musb->int_rx)
21328a15ef7SHans de Goede musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
21428a15ef7SHans de Goede
21528a15ef7SHans de Goede if (musb->int_usb || musb->int_tx || musb->int_rx)
21628a15ef7SHans de Goede retval |= musb_interrupt(musb);
21728a15ef7SHans de Goede
21828a15ef7SHans de Goede return retval;
21928a15ef7SHans de Goede }
22028a15ef7SHans de Goede
221e1abfa43SHans de Goede /* musb_core does not call enable / disable in a balanced manner <sigh> */
222e1abfa43SHans de Goede static bool enabled = false;
223e1abfa43SHans de Goede
sunxi_musb_enable(struct musb * musb)22415837236SHans de Goede static int sunxi_musb_enable(struct musb *musb)
22528a15ef7SHans de Goede {
226dd322817SJagan Teki struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
22757075a47SChen-Yu Tsai int ret;
22857075a47SChen-Yu Tsai
22928a15ef7SHans de Goede pr_debug("%s():\n", __func__);
23028a15ef7SHans de Goede
2311feda63eSMaxime Ripard musb_ep_select(musb->mregs, 0);
2321feda63eSMaxime Ripard musb_writeb(musb->mregs, MUSB_FADDR, 0);
2331feda63eSMaxime Ripard
234e1abfa43SHans de Goede if (enabled)
23515837236SHans de Goede return 0;
236e1abfa43SHans de Goede
23728a15ef7SHans de Goede /* select PIO mode */
23828a15ef7SHans de Goede musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
23928a15ef7SHans de Goede
240b41972e7SHans de Goede if (is_host_enabled(musb)) {
241622fd2b9SJagan Teki ret = sun4i_usb_phy_vbus_detect(&glue->phy);
24257075a47SChen-Yu Tsai if (ret == 1) {
243b41972e7SHans de Goede printf("A charger is plugged into the OTG: ");
244b41972e7SHans de Goede return -ENODEV;
245b41972e7SHans de Goede }
246dd322817SJagan Teki
247622fd2b9SJagan Teki ret = sun4i_usb_phy_id_detect(&glue->phy);
24857075a47SChen-Yu Tsai if (ret == 1) {
24971cbe0d6SHans de Goede printf("No host cable detected: ");
25071cbe0d6SHans de Goede return -ENODEV;
25171cbe0d6SHans de Goede }
252dd322817SJagan Teki
253622fd2b9SJagan Teki ret = generic_phy_power_on(&glue->phy);
254dd322817SJagan Teki if (ret) {
255dd322817SJagan Teki pr_err("failed to power on USB PHY\n");
256dd322817SJagan Teki return ret;
257dd322817SJagan Teki }
258b41972e7SHans de Goede }
259e1abfa43SHans de Goede
260e1abfa43SHans de Goede USBC_ForceVbusValidToHigh(musb->mregs);
261e1abfa43SHans de Goede
262e1abfa43SHans de Goede enabled = true;
26315837236SHans de Goede return 0;
26428a15ef7SHans de Goede }
26528a15ef7SHans de Goede
sunxi_musb_disable(struct musb * musb)26628a15ef7SHans de Goede static void sunxi_musb_disable(struct musb *musb)
26728a15ef7SHans de Goede {
268dd322817SJagan Teki struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
269dd322817SJagan Teki int ret;
270dd322817SJagan Teki
27128a15ef7SHans de Goede pr_debug("%s():\n", __func__);
27228a15ef7SHans de Goede
273e1abfa43SHans de Goede if (!enabled)
274e1abfa43SHans de Goede return;
275375de017SHans de Goede
276dd322817SJagan Teki if (is_host_enabled(musb)) {
277622fd2b9SJagan Teki ret = generic_phy_power_off(&glue->phy);
278dd322817SJagan Teki if (ret) {
279dd322817SJagan Teki pr_err("failed to power off USB PHY\n");
280dd322817SJagan Teki return;
281dd322817SJagan Teki }
282dd322817SJagan Teki }
28357075a47SChen-Yu Tsai
284e1abfa43SHans de Goede USBC_ForceVbusValidToLow(musb->mregs);
285e1abfa43SHans de Goede mdelay(200); /* Wait for the current session to timeout */
286e1abfa43SHans de Goede
287e1abfa43SHans de Goede enabled = false;
28828a15ef7SHans de Goede }
28928a15ef7SHans de Goede
sunxi_musb_init(struct musb * musb)29028a15ef7SHans de Goede static int sunxi_musb_init(struct musb *musb)
29128a15ef7SHans de Goede {
292831cc98bSJagan Teki struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
293dd322817SJagan Teki int ret;
29428a15ef7SHans de Goede
29528a15ef7SHans de Goede pr_debug("%s():\n", __func__);
29628a15ef7SHans de Goede
297*b9aa0a93SJagan Teki ret = clk_enable(&glue->clk);
298dd322817SJagan Teki if (ret) {
299*b9aa0a93SJagan Teki dev_err(dev, "failed to enable clock\n");
300dd322817SJagan Teki return ret;
301dd322817SJagan Teki }
302dd322817SJagan Teki
303*b9aa0a93SJagan Teki if (reset_valid(&glue->rst)) {
304*b9aa0a93SJagan Teki ret = reset_deassert(&glue->rst);
305*b9aa0a93SJagan Teki if (ret) {
306*b9aa0a93SJagan Teki dev_err(dev, "failed to deassert reset\n");
307*b9aa0a93SJagan Teki goto err_clk;
308*b9aa0a93SJagan Teki }
309*b9aa0a93SJagan Teki }
310*b9aa0a93SJagan Teki
311*b9aa0a93SJagan Teki ret = generic_phy_init(&glue->phy);
312*b9aa0a93SJagan Teki if (ret) {
313*b9aa0a93SJagan Teki dev_err(dev, "failed to init USB PHY\n");
314*b9aa0a93SJagan Teki goto err_rst;
315*b9aa0a93SJagan Teki }
316*b9aa0a93SJagan Teki
31728a15ef7SHans de Goede musb->isr = sunxi_musb_interrupt;
318375de017SHans de Goede
31928a15ef7SHans de Goede USBC_ConfigFIFO_Base();
32028a15ef7SHans de Goede USBC_EnableDpDmPullUp(musb->mregs);
32128a15ef7SHans de Goede USBC_EnableIdPullUp(musb->mregs);
32228a15ef7SHans de Goede
32328a15ef7SHans de Goede if (is_host_enabled(musb)) {
32428a15ef7SHans de Goede /* Host mode */
32528a15ef7SHans de Goede USBC_ForceIdToLow(musb->mregs);
32628a15ef7SHans de Goede } else {
32728a15ef7SHans de Goede /* Peripheral mode */
32828a15ef7SHans de Goede USBC_ForceIdToHigh(musb->mregs);
32928a15ef7SHans de Goede }
330b1b912ddSHans de Goede USBC_ForceVbusValidToHigh(musb->mregs);
33128a15ef7SHans de Goede
33228a15ef7SHans de Goede return 0;
333*b9aa0a93SJagan Teki
334*b9aa0a93SJagan Teki err_rst:
335*b9aa0a93SJagan Teki if (reset_valid(&glue->rst))
336*b9aa0a93SJagan Teki reset_assert(&glue->rst);
337*b9aa0a93SJagan Teki err_clk:
338*b9aa0a93SJagan Teki clk_disable(&glue->clk);
339*b9aa0a93SJagan Teki return ret;
34028a15ef7SHans de Goede }
34128a15ef7SHans de Goede
sunxi_musb_exit(struct musb * musb)34214b6a07cSJagan Teki static int sunxi_musb_exit(struct musb *musb)
34314b6a07cSJagan Teki {
34414b6a07cSJagan Teki struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
34514b6a07cSJagan Teki int ret = 0;
34614b6a07cSJagan Teki
34714b6a07cSJagan Teki if (generic_phy_valid(&glue->phy)) {
34814b6a07cSJagan Teki ret = generic_phy_exit(&glue->phy);
34914b6a07cSJagan Teki if (ret) {
35014b6a07cSJagan Teki dev_err(dev, "failed to power off usb phy\n");
35114b6a07cSJagan Teki return ret;
35214b6a07cSJagan Teki }
35314b6a07cSJagan Teki }
35414b6a07cSJagan Teki
355*b9aa0a93SJagan Teki if (reset_valid(&glue->rst))
356*b9aa0a93SJagan Teki reset_assert(&glue->rst);
357*b9aa0a93SJagan Teki clk_disable(&glue->clk);
35814b6a07cSJagan Teki
35914b6a07cSJagan Teki return 0;
36014b6a07cSJagan Teki }
36114b6a07cSJagan Teki
sunxi_musb_pre_root_reset_end(struct musb * musb)362aa29b11bSJagan Teki static void sunxi_musb_pre_root_reset_end(struct musb *musb)
363aa29b11bSJagan Teki {
364aa29b11bSJagan Teki struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
365aa29b11bSJagan Teki
366622fd2b9SJagan Teki sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
367aa29b11bSJagan Teki }
368aa29b11bSJagan Teki
sunxi_musb_post_root_reset_end(struct musb * musb)369aa29b11bSJagan Teki static void sunxi_musb_post_root_reset_end(struct musb *musb)
370aa29b11bSJagan Teki {
371aa29b11bSJagan Teki struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
372aa29b11bSJagan Teki
373622fd2b9SJagan Teki sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
374aa29b11bSJagan Teki }
375aa29b11bSJagan Teki
376d42faf31SHans de Goede static const struct musb_platform_ops sunxi_musb_ops = {
37728a15ef7SHans de Goede .init = sunxi_musb_init,
37814b6a07cSJagan Teki .exit = sunxi_musb_exit,
37928a15ef7SHans de Goede .enable = sunxi_musb_enable,
38028a15ef7SHans de Goede .disable = sunxi_musb_disable,
381aa29b11bSJagan Teki .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
382aa29b11bSJagan Teki .post_root_reset_end = sunxi_musb_post_root_reset_end,
38328a15ef7SHans de Goede };
384d42faf31SHans de Goede
385ae8b78deSJagan Teki /* Allwinner OTG supports up to 5 endpoints */
386ae8b78deSJagan Teki #define SUNXI_MUSB_MAX_EP_NUM 6
387ae8b78deSJagan Teki #define SUNXI_MUSB_RAM_BITS 11
388ae8b78deSJagan Teki
38997202dd6SJagan Teki static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
39097202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
39197202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
39297202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
39397202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
39497202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
39597202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
39697202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
39797202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
39897202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
39997202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
40097202dd6SJagan Teki };
40197202dd6SJagan Teki
40297202dd6SJagan Teki /* H3/V3s OTG supports only 4 endpoints */
40397202dd6SJagan Teki #define SUNXI_MUSB_MAX_EP_NUM_H3 5
40497202dd6SJagan Teki
40597202dd6SJagan Teki static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
40697202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
40797202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
40897202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
40997202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
41097202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
41197202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
41297202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
41397202dd6SJagan Teki MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
41497202dd6SJagan Teki };
41597202dd6SJagan Teki
416d42faf31SHans de Goede static struct musb_hdrc_config musb_config = {
41797202dd6SJagan Teki .fifo_cfg = sunxi_musb_mode_cfg,
41897202dd6SJagan Teki .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
419ae8b78deSJagan Teki .multipoint = true,
420ae8b78deSJagan Teki .dyn_fifo = true,
421ae8b78deSJagan Teki .num_eps = SUNXI_MUSB_MAX_EP_NUM,
422ae8b78deSJagan Teki .ram_bits = SUNXI_MUSB_RAM_BITS,
423d42faf31SHans de Goede };
424d42faf31SHans de Goede
42597202dd6SJagan Teki static struct musb_hdrc_config musb_config_h3 = {
42697202dd6SJagan Teki .fifo_cfg = sunxi_musb_mode_cfg_h3,
42797202dd6SJagan Teki .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
42897202dd6SJagan Teki .multipoint = true,
42997202dd6SJagan Teki .dyn_fifo = true,
43097202dd6SJagan Teki .soft_con = true,
43197202dd6SJagan Teki .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
43297202dd6SJagan Teki .ram_bits = SUNXI_MUSB_RAM_BITS,
43397202dd6SJagan Teki };
43497202dd6SJagan Teki
musb_usb_probe(struct udevice * dev)4357c22e26eSHans de Goede static int musb_usb_probe(struct udevice *dev)
43691183babSHans de Goede {
437831cc98bSJagan Teki struct sunxi_glue *glue = dev_get_priv(dev);
438831cc98bSJagan Teki struct musb_host_data *host = &glue->mdata;
43998424b70SJagan Teki struct musb_hdrc_platform_data pdata;
440f4f9896aSChen-Yu Tsai void *base = dev_read_addr_ptr(dev);
44156a20854SHans de Goede int ret;
44291183babSHans de Goede
44346a3f276SStefan Mavrodiev #ifdef CONFIG_USB_MUSB_HOST
44446a3f276SStefan Mavrodiev struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
44546a3f276SStefan Mavrodiev #endif
44646a3f276SStefan Mavrodiev
447f4f9896aSChen-Yu Tsai if (!base)
448f4f9896aSChen-Yu Tsai return -EINVAL;
449f4f9896aSChen-Yu Tsai
45097202dd6SJagan Teki glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
45197202dd6SJagan Teki if (!glue->cfg)
45297202dd6SJagan Teki return -EINVAL;
45397202dd6SJagan Teki
454*b9aa0a93SJagan Teki ret = clk_get_by_index(dev, 0, &glue->clk);
455*b9aa0a93SJagan Teki if (ret) {
456*b9aa0a93SJagan Teki dev_err(dev, "failed to get clock\n");
457*b9aa0a93SJagan Teki return ret;
458*b9aa0a93SJagan Teki }
459831cc98bSJagan Teki
460*b9aa0a93SJagan Teki ret = reset_get_by_index(dev, 0, &glue->rst);
461*b9aa0a93SJagan Teki if (ret && ret != -ENOENT) {
462*b9aa0a93SJagan Teki dev_err(dev, "failed to get reset\n");
463*b9aa0a93SJagan Teki return ret;
464*b9aa0a93SJagan Teki }
4651034bcc2SJagan Teki
466622fd2b9SJagan Teki ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
467dd322817SJagan Teki if (ret) {
468dd322817SJagan Teki pr_err("failed to get usb PHY\n");
469dd322817SJagan Teki return ret;
470dd322817SJagan Teki }
471dd322817SJagan Teki
47298424b70SJagan Teki memset(&pdata, 0, sizeof(pdata));
47398424b70SJagan Teki pdata.power = 250;
47498424b70SJagan Teki pdata.platform_ops = &sunxi_musb_ops;
47597202dd6SJagan Teki pdata.config = glue->cfg->config;
47698424b70SJagan Teki
4773a61b080SMaxime Ripard #ifdef CONFIG_USB_MUSB_HOST
47846a3f276SStefan Mavrodiev priv->desc_before_addr = true;
47946a3f276SStefan Mavrodiev
48098424b70SJagan Teki pdata.mode = MUSB_HOST;
48198424b70SJagan Teki host->host = musb_init_controller(&pdata, &glue->dev, base);
48256a20854SHans de Goede if (!host->host)
48391183babSHans de Goede return -EIO;
48491183babSHans de Goede
48556a20854SHans de Goede ret = musb_lowlevel_init(host);
4863a61b080SMaxime Ripard if (!ret)
4873a61b080SMaxime Ripard printf("Allwinner mUSB OTG (Host)\n");
4883a61b080SMaxime Ripard #else
48998424b70SJagan Teki pdata.mode = MUSB_PERIPHERAL;
4908b8d59f3SJagan Teki host->host = musb_register(&pdata, &glue->dev, base);
4918b8d59f3SJagan Teki if (!host->host)
4928b8d59f3SJagan Teki return -EIO;
4938b8d59f3SJagan Teki
4943a61b080SMaxime Ripard printf("Allwinner mUSB OTG (Peripheral)\n");
4953a61b080SMaxime Ripard #endif
49691183babSHans de Goede
49756a20854SHans de Goede return ret;
49891183babSHans de Goede }
49991183babSHans de Goede
musb_usb_remove(struct udevice * dev)5007c22e26eSHans de Goede static int musb_usb_remove(struct udevice *dev)
50191183babSHans de Goede {
502831cc98bSJagan Teki struct sunxi_glue *glue = dev_get_priv(dev);
503831cc98bSJagan Teki struct musb_host_data *host = &glue->mdata;
50491183babSHans de Goede
50591183babSHans de Goede musb_stop(host->host);
5067c22e26eSHans de Goede free(host->host);
5077c22e26eSHans de Goede host->host = NULL;
5087c22e26eSHans de Goede
50991183babSHans de Goede return 0;
51091183babSHans de Goede }
51191183babSHans de Goede
51297202dd6SJagan Teki static const struct sunxi_musb_config sun4i_a10_cfg = {
51397202dd6SJagan Teki .config = &musb_config,
5141034bcc2SJagan Teki };
5151034bcc2SJagan Teki
5161034bcc2SJagan Teki static const struct sunxi_musb_config sun6i_a31_cfg = {
5171034bcc2SJagan Teki .config = &musb_config,
51897202dd6SJagan Teki };
51997202dd6SJagan Teki
52097202dd6SJagan Teki static const struct sunxi_musb_config sun8i_h3_cfg = {
52197202dd6SJagan Teki .config = &musb_config_h3,
52297202dd6SJagan Teki };
52397202dd6SJagan Teki
5243a61b080SMaxime Ripard static const struct udevice_id sunxi_musb_ids[] = {
52597202dd6SJagan Teki { .compatible = "allwinner,sun4i-a10-musb",
52697202dd6SJagan Teki .data = (ulong)&sun4i_a10_cfg },
52797202dd6SJagan Teki { .compatible = "allwinner,sun6i-a31-musb",
5281034bcc2SJagan Teki .data = (ulong)&sun6i_a31_cfg },
52997202dd6SJagan Teki { .compatible = "allwinner,sun8i-a33-musb",
5301034bcc2SJagan Teki .data = (ulong)&sun6i_a31_cfg },
53197202dd6SJagan Teki { .compatible = "allwinner,sun8i-h3-musb",
53297202dd6SJagan Teki .data = (ulong)&sun8i_h3_cfg },
5333a61b080SMaxime Ripard { }
5343a61b080SMaxime Ripard };
5353a61b080SMaxime Ripard
53691183babSHans de Goede U_BOOT_DRIVER(usb_musb) = {
53791183babSHans de Goede .name = "sunxi-musb",
5383a61b080SMaxime Ripard #ifdef CONFIG_USB_MUSB_HOST
53991183babSHans de Goede .id = UCLASS_USB,
5403a61b080SMaxime Ripard #else
54101311624SJean-Jacques Hiblot .id = UCLASS_USB_GADGET_GENERIC,
5423a61b080SMaxime Ripard #endif
5433a61b080SMaxime Ripard .of_match = sunxi_musb_ids,
54491183babSHans de Goede .probe = musb_usb_probe,
54591183babSHans de Goede .remove = musb_usb_remove,
5463a61b080SMaxime Ripard #ifdef CONFIG_USB_MUSB_HOST
54791183babSHans de Goede .ops = &musb_usb_ops,
5483a61b080SMaxime Ripard #endif
54991183babSHans de Goede .platdata_auto_alloc_size = sizeof(struct usb_platdata),
550831cc98bSJagan Teki .priv_auto_alloc_size = sizeof(struct sunxi_glue),
55191183babSHans de Goede };
552