Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 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. |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 17 | */ |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 18 | #include <common.h> |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 19 | #include <clk.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 20 | #include <dm.h> |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 21 | #include <generic-phy.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 22 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 23 | #include <malloc.h> |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 24 | #include <phy-sun4i-usb.h> |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 25 | #include <reset.h> |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 26 | #include <asm/arch/cpu.h> |
Hans de Goede | 7e5aabd | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 27 | #include <asm/arch/clock.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 28 | #include <dm/device_compat.h> |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 29 | #include <dm/lists.h> |
| 30 | #include <dm/root.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 31 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 32 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 33 | #include <linux/printk.h> |
Hans de Goede | ea059bf | 2015-06-17 15:49:26 +0200 | [diff] [blame] | 34 | #include <linux/usb/musb.h> |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 35 | #include "linux-compat.h" |
| 36 | #include "musb_core.h" |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 37 | #include "musb_uboot.h" |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 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 | |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 85 | struct sunxi_musb_config { |
| 86 | struct musb_hdrc_config *config; |
| 87 | }; |
| 88 | |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 89 | struct sunxi_glue { |
| 90 | struct musb_host_data mdata; |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 91 | struct clk clk; |
| 92 | struct reset_ctl rst; |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 93 | struct sunxi_musb_config *cfg; |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 94 | struct device dev; |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 95 | struct phy phy; |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 96 | }; |
| 97 | #define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev) |
| 98 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 99 | static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val) |
| 100 | { |
| 101 | u32 temp = reg_val; |
| 102 | |
Jagan Teki | cecffaa | 2018-05-07 13:03:23 +0530 | [diff] [blame] | 103 | temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT); |
| 104 | temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT); |
| 105 | temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 106 | |
| 107 | return temp; |
| 108 | } |
| 109 | |
| 110 | static void USBC_EnableIdPullUp(__iomem void *base) |
| 111 | { |
| 112 | u32 reg_val; |
| 113 | |
| 114 | reg_val = musb_readl(base, USBC_REG_o_ISCR); |
Jagan Teki | cecffaa | 2018-05-07 13:03:23 +0530 | [diff] [blame] | 115 | reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 116 | reg_val = USBC_WakeUp_ClearChangeDetect(reg_val); |
| 117 | musb_writel(base, USBC_REG_o_ISCR, reg_val); |
| 118 | } |
| 119 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 120 | static void USBC_EnableDpDmPullUp(__iomem void *base) |
| 121 | { |
| 122 | u32 reg_val; |
| 123 | |
| 124 | reg_val = musb_readl(base, USBC_REG_o_ISCR); |
Jagan Teki | cecffaa | 2018-05-07 13:03:23 +0530 | [diff] [blame] | 125 | reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 126 | reg_val = USBC_WakeUp_ClearChangeDetect(reg_val); |
| 127 | musb_writel(base, USBC_REG_o_ISCR, reg_val); |
| 128 | } |
| 129 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 130 | static void USBC_ForceIdToLow(__iomem void *base) |
| 131 | { |
| 132 | u32 reg_val; |
| 133 | |
| 134 | reg_val = musb_readl(base, USBC_REG_o_ISCR); |
| 135 | reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID); |
| 136 | reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID); |
| 137 | reg_val = USBC_WakeUp_ClearChangeDetect(reg_val); |
| 138 | musb_writel(base, USBC_REG_o_ISCR, reg_val); |
| 139 | } |
| 140 | |
| 141 | static void USBC_ForceIdToHigh(__iomem void *base) |
| 142 | { |
| 143 | u32 reg_val; |
| 144 | |
| 145 | reg_val = musb_readl(base, USBC_REG_o_ISCR); |
| 146 | reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID); |
| 147 | reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID); |
| 148 | reg_val = USBC_WakeUp_ClearChangeDetect(reg_val); |
| 149 | musb_writel(base, USBC_REG_o_ISCR, reg_val); |
| 150 | } |
| 151 | |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 152 | static void USBC_ForceVbusValidToLow(__iomem void *base) |
| 153 | { |
| 154 | u32 reg_val; |
| 155 | |
| 156 | reg_val = musb_readl(base, USBC_REG_o_ISCR); |
| 157 | reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID); |
| 158 | reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID); |
| 159 | reg_val = USBC_WakeUp_ClearChangeDetect(reg_val); |
| 160 | musb_writel(base, USBC_REG_o_ISCR, reg_val); |
| 161 | } |
| 162 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 163 | static void USBC_ForceVbusValidToHigh(__iomem void *base) |
| 164 | { |
| 165 | u32 reg_val; |
| 166 | |
| 167 | reg_val = musb_readl(base, USBC_REG_o_ISCR); |
| 168 | reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID); |
| 169 | reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID); |
| 170 | reg_val = USBC_WakeUp_ClearChangeDetect(reg_val); |
| 171 | musb_writel(base, USBC_REG_o_ISCR, reg_val); |
| 172 | } |
| 173 | |
| 174 | static void USBC_ConfigFIFO_Base(void) |
| 175 | { |
| 176 | u32 reg_value; |
| 177 | |
| 178 | /* config usb fifo, 8kb mode */ |
| 179 | reg_value = readl(SUNXI_SRAMC_BASE + 0x04); |
| 180 | reg_value &= ~(0x03 << 0); |
Jagan Teki | cecffaa | 2018-05-07 13:03:23 +0530 | [diff] [blame] | 181 | reg_value |= BIT(0); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 182 | writel(reg_value, SUNXI_SRAMC_BASE + 0x04); |
| 183 | } |
| 184 | |
| 185 | /****************************************************************************** |
Siarhei Siamashka | bce07f6 | 2015-10-25 06:44:47 +0200 | [diff] [blame] | 186 | * Needed for the DFU polling magic |
| 187 | ******************************************************************************/ |
| 188 | |
| 189 | static u8 last_int_usb; |
| 190 | |
| 191 | bool dfu_usb_get_reset(void) |
| 192 | { |
| 193 | return !!(last_int_usb & MUSB_INTR_RESET); |
| 194 | } |
| 195 | |
| 196 | /****************************************************************************** |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 197 | * MUSB Glue code |
| 198 | ******************************************************************************/ |
| 199 | |
| 200 | static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci) |
| 201 | { |
| 202 | struct musb *musb = __hci; |
| 203 | irqreturn_t retval = IRQ_NONE; |
| 204 | |
| 205 | /* read and flush interrupts */ |
| 206 | musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); |
Siarhei Siamashka | bce07f6 | 2015-10-25 06:44:47 +0200 | [diff] [blame] | 207 | last_int_usb = musb->int_usb; |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 208 | if (musb->int_usb) |
| 209 | musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb); |
| 210 | musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); |
| 211 | if (musb->int_tx) |
| 212 | musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx); |
| 213 | musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); |
| 214 | if (musb->int_rx) |
| 215 | musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx); |
| 216 | |
| 217 | if (musb->int_usb || musb->int_tx || musb->int_rx) |
| 218 | retval |= musb_interrupt(musb); |
| 219 | |
| 220 | return retval; |
| 221 | } |
| 222 | |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 223 | /* musb_core does not call enable / disable in a balanced manner <sigh> */ |
| 224 | static bool enabled = false; |
| 225 | |
Hans de Goede | 81c4998 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 226 | static int sunxi_musb_enable(struct musb *musb) |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 227 | { |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 228 | struct sunxi_glue *glue = to_sunxi_glue(musb->controller); |
Chen-Yu Tsai | d64afdc | 2016-09-07 14:25:21 +0800 | [diff] [blame] | 229 | int ret; |
| 230 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 231 | pr_debug("%s():\n", __func__); |
| 232 | |
Maxime Ripard | 9634fa5 | 2015-08-04 17:04:10 +0200 | [diff] [blame] | 233 | musb_ep_select(musb->mregs, 0); |
| 234 | musb_writeb(musb->mregs, MUSB_FADDR, 0); |
| 235 | |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 236 | if (enabled) |
Hans de Goede | 81c4998 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 237 | return 0; |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 238 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 239 | /* select PIO mode */ |
| 240 | musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0); |
| 241 | |
Hans de Goede | c93ecd0 | 2015-06-14 16:48:56 +0200 | [diff] [blame] | 242 | if (is_host_enabled(musb)) { |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 243 | ret = sun4i_usb_phy_id_detect(&glue->phy); |
Chen-Yu Tsai | d64afdc | 2016-09-07 14:25:21 +0800 | [diff] [blame] | 244 | if (ret == 1) { |
Hans de Goede | c77fbfb | 2015-06-14 17:40:37 +0200 | [diff] [blame] | 245 | printf("No host cable detected: "); |
| 246 | return -ENODEV; |
| 247 | } |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 248 | |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 249 | ret = generic_phy_power_on(&glue->phy); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 250 | if (ret) { |
Patrick Delaunay | 1b9711b | 2020-07-03 17:36:45 +0200 | [diff] [blame] | 251 | pr_debug("failed to power on USB PHY\n"); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 252 | return ret; |
| 253 | } |
Hans de Goede | c93ecd0 | 2015-06-14 16:48:56 +0200 | [diff] [blame] | 254 | } |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 255 | |
| 256 | USBC_ForceVbusValidToHigh(musb->mregs); |
| 257 | |
| 258 | enabled = true; |
Hans de Goede | 81c4998 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 259 | return 0; |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static void sunxi_musb_disable(struct musb *musb) |
| 263 | { |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 264 | struct sunxi_glue *glue = to_sunxi_glue(musb->controller); |
| 265 | int ret; |
| 266 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 267 | pr_debug("%s():\n", __func__); |
| 268 | |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 269 | if (!enabled) |
| 270 | return; |
Hans de Goede | 7e5aabd | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 271 | |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 272 | if (is_host_enabled(musb)) { |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 273 | ret = generic_phy_power_off(&glue->phy); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 274 | if (ret) { |
Patrick Delaunay | 1b9711b | 2020-07-03 17:36:45 +0200 | [diff] [blame] | 275 | pr_debug("failed to power off USB PHY\n"); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | } |
Chen-Yu Tsai | d64afdc | 2016-09-07 14:25:21 +0800 | [diff] [blame] | 279 | |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 280 | USBC_ForceVbusValidToLow(musb->mregs); |
| 281 | mdelay(200); /* Wait for the current session to timeout */ |
Hans de Goede | 7e5aabd | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 282 | |
Hans de Goede | 836e334 | 2015-06-14 11:55:28 +0200 | [diff] [blame] | 283 | enabled = false; |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static int sunxi_musb_init(struct musb *musb) |
| 287 | { |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 288 | struct sunxi_glue *glue = to_sunxi_glue(musb->controller); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 289 | int ret; |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 290 | |
| 291 | pr_debug("%s():\n", __func__); |
| 292 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 293 | ret = clk_enable(&glue->clk); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 294 | if (ret) { |
Sean Anderson | 46362c7 | 2020-09-15 10:45:19 -0400 | [diff] [blame] | 295 | dev_err(musb->controller, "failed to enable clock\n"); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 296 | return ret; |
| 297 | } |
| 298 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 299 | if (reset_valid(&glue->rst)) { |
| 300 | ret = reset_deassert(&glue->rst); |
| 301 | if (ret) { |
Sean Anderson | 46362c7 | 2020-09-15 10:45:19 -0400 | [diff] [blame] | 302 | dev_err(musb->controller, "failed to deassert reset\n"); |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 303 | goto err_clk; |
| 304 | } |
| 305 | } |
Jagan Teki | 2e8e67c | 2018-07-20 12:43:59 +0530 | [diff] [blame] | 306 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 307 | ret = generic_phy_init(&glue->phy); |
| 308 | if (ret) { |
Sean Anderson | 46362c7 | 2020-09-15 10:45:19 -0400 | [diff] [blame] | 309 | dev_dbg(musb->controller, "failed to init USB PHY\n"); |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 310 | goto err_rst; |
| 311 | } |
Jagan Teki | 2e8e67c | 2018-07-20 12:43:59 +0530 | [diff] [blame] | 312 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 313 | musb->isr = sunxi_musb_interrupt; |
Jagan Teki | 767a58e | 2018-05-07 13:03:22 +0530 | [diff] [blame] | 314 | |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 315 | USBC_ConfigFIFO_Base(); |
| 316 | USBC_EnableDpDmPullUp(musb->mregs); |
| 317 | USBC_EnableIdPullUp(musb->mregs); |
| 318 | |
| 319 | if (is_host_enabled(musb)) { |
| 320 | /* Host mode */ |
| 321 | USBC_ForceIdToLow(musb->mregs); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 322 | } else { |
| 323 | /* Peripheral mode */ |
| 324 | USBC_ForceIdToHigh(musb->mregs); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 325 | } |
Hans de Goede | f2b45c8 | 2015-02-11 09:05:18 +0100 | [diff] [blame] | 326 | USBC_ForceVbusValidToHigh(musb->mregs); |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 327 | |
| 328 | return 0; |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 329 | |
| 330 | err_rst: |
| 331 | if (reset_valid(&glue->rst)) |
| 332 | reset_assert(&glue->rst); |
| 333 | err_clk: |
| 334 | clk_disable(&glue->clk); |
| 335 | return ret; |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Jagan Teki | 1d0d63e | 2018-07-20 12:44:00 +0530 | [diff] [blame] | 338 | static int sunxi_musb_exit(struct musb *musb) |
| 339 | { |
| 340 | struct sunxi_glue *glue = to_sunxi_glue(musb->controller); |
| 341 | int ret = 0; |
| 342 | |
| 343 | if (generic_phy_valid(&glue->phy)) { |
| 344 | ret = generic_phy_exit(&glue->phy); |
| 345 | if (ret) { |
Sean Anderson | 46362c7 | 2020-09-15 10:45:19 -0400 | [diff] [blame] | 346 | dev_dbg(musb->controller, |
| 347 | "failed to power off usb phy\n"); |
Jagan Teki | 1d0d63e | 2018-07-20 12:44:00 +0530 | [diff] [blame] | 348 | return ret; |
| 349 | } |
| 350 | } |
| 351 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 352 | if (reset_valid(&glue->rst)) |
| 353 | reset_assert(&glue->rst); |
| 354 | clk_disable(&glue->clk); |
Jagan Teki | 1d0d63e | 2018-07-20 12:44:00 +0530 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Jagan Teki | 37671e1 | 2018-05-07 13:03:37 +0530 | [diff] [blame] | 359 | static void sunxi_musb_pre_root_reset_end(struct musb *musb) |
| 360 | { |
| 361 | struct sunxi_glue *glue = to_sunxi_glue(musb->controller); |
| 362 | |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 363 | sun4i_usb_phy_set_squelch_detect(&glue->phy, false); |
Jagan Teki | 37671e1 | 2018-05-07 13:03:37 +0530 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void sunxi_musb_post_root_reset_end(struct musb *musb) |
| 367 | { |
| 368 | struct sunxi_glue *glue = to_sunxi_glue(musb->controller); |
| 369 | |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 370 | sun4i_usb_phy_set_squelch_detect(&glue->phy, true); |
Jagan Teki | 37671e1 | 2018-05-07 13:03:37 +0530 | [diff] [blame] | 371 | } |
| 372 | |
Hans de Goede | ea059bf | 2015-06-17 15:49:26 +0200 | [diff] [blame] | 373 | static const struct musb_platform_ops sunxi_musb_ops = { |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 374 | .init = sunxi_musb_init, |
Jagan Teki | 1d0d63e | 2018-07-20 12:44:00 +0530 | [diff] [blame] | 375 | .exit = sunxi_musb_exit, |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 376 | .enable = sunxi_musb_enable, |
| 377 | .disable = sunxi_musb_disable, |
Jagan Teki | 37671e1 | 2018-05-07 13:03:37 +0530 | [diff] [blame] | 378 | .pre_root_reset_end = sunxi_musb_pre_root_reset_end, |
| 379 | .post_root_reset_end = sunxi_musb_post_root_reset_end, |
Hans de Goede | 9c4f11d | 2015-01-11 20:34:48 +0100 | [diff] [blame] | 380 | }; |
Hans de Goede | ea059bf | 2015-06-17 15:49:26 +0200 | [diff] [blame] | 381 | |
Jagan Teki | 4e5af30 | 2018-05-07 13:03:18 +0530 | [diff] [blame] | 382 | /* Allwinner OTG supports up to 5 endpoints */ |
| 383 | #define SUNXI_MUSB_MAX_EP_NUM 6 |
| 384 | #define SUNXI_MUSB_RAM_BITS 11 |
| 385 | |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 386 | static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = { |
| 387 | MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512), |
| 388 | MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512), |
| 389 | MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512), |
| 390 | MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512), |
| 391 | MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512), |
| 392 | MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512), |
| 393 | MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512), |
| 394 | MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512), |
| 395 | MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512), |
| 396 | MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512), |
| 397 | }; |
| 398 | |
| 399 | /* H3/V3s OTG supports only 4 endpoints */ |
| 400 | #define SUNXI_MUSB_MAX_EP_NUM_H3 5 |
| 401 | |
| 402 | static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = { |
| 403 | MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512), |
| 404 | MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512), |
| 405 | MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512), |
| 406 | MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512), |
| 407 | MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512), |
| 408 | MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512), |
| 409 | MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512), |
| 410 | MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512), |
| 411 | }; |
| 412 | |
Hans de Goede | ea059bf | 2015-06-17 15:49:26 +0200 | [diff] [blame] | 413 | static struct musb_hdrc_config musb_config = { |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 414 | .fifo_cfg = sunxi_musb_mode_cfg, |
| 415 | .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg), |
Jagan Teki | 4e5af30 | 2018-05-07 13:03:18 +0530 | [diff] [blame] | 416 | .multipoint = true, |
| 417 | .dyn_fifo = true, |
| 418 | .num_eps = SUNXI_MUSB_MAX_EP_NUM, |
| 419 | .ram_bits = SUNXI_MUSB_RAM_BITS, |
Hans de Goede | ea059bf | 2015-06-17 15:49:26 +0200 | [diff] [blame] | 420 | }; |
| 421 | |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 422 | static struct musb_hdrc_config musb_config_h3 = { |
| 423 | .fifo_cfg = sunxi_musb_mode_cfg_h3, |
| 424 | .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3), |
| 425 | .multipoint = true, |
| 426 | .dyn_fifo = true, |
| 427 | .soft_con = true, |
| 428 | .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3, |
| 429 | .ram_bits = SUNXI_MUSB_RAM_BITS, |
| 430 | }; |
| 431 | |
Hans de Goede | 4a67d73 | 2016-09-17 16:02:38 +0200 | [diff] [blame] | 432 | static int musb_usb_probe(struct udevice *dev) |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 433 | { |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 434 | struct sunxi_glue *glue = dev_get_priv(dev); |
| 435 | struct musb_host_data *host = &glue->mdata; |
Jagan Teki | 69bb76e | 2018-05-07 13:03:19 +0530 | [diff] [blame] | 436 | struct musb_hdrc_platform_data pdata; |
Chen-Yu Tsai | 6310755 | 2017-12-30 20:44:07 +0800 | [diff] [blame] | 437 | void *base = dev_read_addr_ptr(dev); |
Hans de Goede | 31e4f6f | 2015-06-18 22:45:34 +0200 | [diff] [blame] | 438 | int ret; |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 439 | |
Stefan Mavrodiev | 4beaec1 | 2018-12-05 14:49:44 +0200 | [diff] [blame] | 440 | #ifdef CONFIG_USB_MUSB_HOST |
| 441 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
| 442 | #endif |
| 443 | |
Chen-Yu Tsai | 6310755 | 2017-12-30 20:44:07 +0800 | [diff] [blame] | 444 | if (!base) |
| 445 | return -EINVAL; |
| 446 | |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 447 | glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev); |
| 448 | if (!glue->cfg) |
| 449 | return -EINVAL; |
| 450 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 451 | ret = clk_get_by_index(dev, 0, &glue->clk); |
| 452 | if (ret) { |
| 453 | dev_err(dev, "failed to get clock\n"); |
| 454 | return ret; |
| 455 | } |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 456 | |
Jagan Teki | ce8a697 | 2018-12-31 17:05:40 +0530 | [diff] [blame] | 457 | ret = reset_get_by_index(dev, 0, &glue->rst); |
| 458 | if (ret && ret != -ENOENT) { |
| 459 | dev_err(dev, "failed to get reset\n"); |
| 460 | return ret; |
| 461 | } |
Jagan Teki | 2e8e67c | 2018-07-20 12:43:59 +0530 | [diff] [blame] | 462 | |
Jagan Teki | d0edfee | 2018-07-20 12:43:57 +0530 | [diff] [blame] | 463 | ret = generic_phy_get_by_name(dev, "usb", &glue->phy); |
Jagan Teki | 137fc75 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 464 | if (ret) { |
| 465 | pr_err("failed to get usb PHY\n"); |
| 466 | return ret; |
| 467 | } |
| 468 | |
Jagan Teki | 69bb76e | 2018-05-07 13:03:19 +0530 | [diff] [blame] | 469 | memset(&pdata, 0, sizeof(pdata)); |
| 470 | pdata.power = 250; |
| 471 | pdata.platform_ops = &sunxi_musb_ops; |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 472 | pdata.config = glue->cfg->config; |
Jagan Teki | 69bb76e | 2018-05-07 13:03:19 +0530 | [diff] [blame] | 473 | |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 474 | #ifdef CONFIG_USB_MUSB_HOST |
Stefan Mavrodiev | 4beaec1 | 2018-12-05 14:49:44 +0200 | [diff] [blame] | 475 | priv->desc_before_addr = true; |
| 476 | |
Jagan Teki | 69bb76e | 2018-05-07 13:03:19 +0530 | [diff] [blame] | 477 | pdata.mode = MUSB_HOST; |
| 478 | host->host = musb_init_controller(&pdata, &glue->dev, base); |
Hans de Goede | ec89e16 | 2016-04-02 20:46:09 +0200 | [diff] [blame] | 479 | if (!host->host) |
| 480 | return -EIO; |
| 481 | |
Hans de Goede | 31e4f6f | 2015-06-18 22:45:34 +0200 | [diff] [blame] | 482 | ret = musb_lowlevel_init(host); |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 483 | if (!ret) |
| 484 | printf("Allwinner mUSB OTG (Host)\n"); |
| 485 | #else |
Jagan Teki | 69bb76e | 2018-05-07 13:03:19 +0530 | [diff] [blame] | 486 | pdata.mode = MUSB_PERIPHERAL; |
Jagan Teki | f0a6d79 | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 487 | host->host = musb_register(&pdata, &glue->dev, base); |
Sam Edwards | 4510506 | 2023-06-05 11:19:37 -0600 | [diff] [blame] | 488 | if (IS_ERR_OR_NULL(host->host)) |
Jagan Teki | f0a6d79 | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 489 | return -EIO; |
| 490 | |
| 491 | printf("Allwinner mUSB OTG (Peripheral)\n"); |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 492 | #endif |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 493 | |
Hans de Goede | 31e4f6f | 2015-06-18 22:45:34 +0200 | [diff] [blame] | 494 | return ret; |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Hans de Goede | 4a67d73 | 2016-09-17 16:02:38 +0200 | [diff] [blame] | 497 | static int musb_usb_remove(struct udevice *dev) |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 498 | { |
Jagan Teki | fda1569 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 499 | struct sunxi_glue *glue = dev_get_priv(dev); |
| 500 | struct musb_host_data *host = &glue->mdata; |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 501 | |
| 502 | musb_stop(host->host); |
Hans de Goede | 4a67d73 | 2016-09-17 16:02:38 +0200 | [diff] [blame] | 503 | free(host->host); |
| 504 | host->host = NULL; |
| 505 | |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Andre Przywara | 0bde97a | 2023-10-13 23:12:14 +0100 | [diff] [blame] | 509 | /* |
| 510 | * The Linux driver has a config struct, its fields mapping to this driver |
| 511 | * like this: |
| 512 | * .hdrc_config: |
| 513 | * sunxi_musb_hdrc_config_5eps => musb_config |
| 514 | * sunxi_musb_hdrc_config_4eps => musb_config_h3 |
| 515 | * .has_sram: always enabled, ideally no-op on SoCs not using it |
| 516 | * .has_reset: automatically detected from DT |
| 517 | * .no_configdata: handled via Kconfig's CONFIG_USB_MUSB_FIXED_CONFIGDATA |
| 518 | */ |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 519 | static const struct sunxi_musb_config sun4i_a10_cfg = { |
| 520 | .config = &musb_config, |
Jagan Teki | 2e8e67c | 2018-07-20 12:43:59 +0530 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | static const struct sunxi_musb_config sun6i_a31_cfg = { |
| 524 | .config = &musb_config, |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | static const struct sunxi_musb_config sun8i_h3_cfg = { |
| 528 | .config = &musb_config_h3, |
| 529 | }; |
| 530 | |
Andre Przywara | 0bde97a | 2023-10-13 23:12:14 +0100 | [diff] [blame] | 531 | static const struct sunxi_musb_config suniv_f1c100s_cfg = { |
| 532 | .config = &musb_config, |
| 533 | }; |
| 534 | |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 535 | static const struct udevice_id sunxi_musb_ids[] = { |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 536 | { .compatible = "allwinner,sun4i-a10-musb", |
| 537 | .data = (ulong)&sun4i_a10_cfg }, |
| 538 | { .compatible = "allwinner,sun6i-a31-musb", |
Jagan Teki | 2e8e67c | 2018-07-20 12:43:59 +0530 | [diff] [blame] | 539 | .data = (ulong)&sun6i_a31_cfg }, |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 540 | { .compatible = "allwinner,sun8i-a33-musb", |
Jagan Teki | 2e8e67c | 2018-07-20 12:43:59 +0530 | [diff] [blame] | 541 | .data = (ulong)&sun6i_a31_cfg }, |
Jagan Teki | 65ce496 | 2018-05-07 13:03:20 +0530 | [diff] [blame] | 542 | { .compatible = "allwinner,sun8i-h3-musb", |
| 543 | .data = (ulong)&sun8i_h3_cfg }, |
Andre Przywara | 0bde97a | 2023-10-13 23:12:14 +0100 | [diff] [blame] | 544 | { .compatible = "allwinner,suniv-f1c100s-musb", |
| 545 | .data = (ulong)&suniv_f1c100s_cfg }, |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 546 | { } |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 547 | }; |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 548 | |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 549 | U_BOOT_DRIVER(usb_musb) = { |
| 550 | .name = "sunxi-musb", |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 551 | #ifdef CONFIG_USB_MUSB_HOST |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 552 | .id = UCLASS_USB, |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 553 | #else |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 554 | .id = UCLASS_USB_GADGET_GENERIC, |
Hans de Goede | 0b3845a | 2015-06-17 17:44:58 +0200 | [diff] [blame] | 555 | #endif |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 556 | .of_match = sunxi_musb_ids, |
| 557 | .probe = musb_usb_probe, |
| 558 | .remove = musb_usb_remove, |
| 559 | #ifdef CONFIG_USB_MUSB_HOST |
| 560 | .ops = &musb_usb_ops, |
| 561 | #endif |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 562 | .plat_auto = sizeof(struct usb_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 563 | .priv_auto = sizeof(struct sunxi_glue), |
Maxime Ripard | 847bcf0 | 2017-09-05 22:10:35 +0200 | [diff] [blame] | 564 | }; |