blob: b577ba4187807f6cd350ec5859fb347030c58f6c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Hans de Goede9c4f11d2015-01-11 20:34:48 +01002/*
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 Goede9c4f11d2015-01-11 20:34:48 +010017 */
Jagan Tekice8a6972018-12-31 17:05:40 +053018#include <clk.h>
Simon Glass11c89f32017-05-17 17:18:03 -060019#include <dm.h>
Jagan Teki137fc752018-05-07 13:03:38 +053020#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <malloc.h>
Jagan Teki137fc752018-05-07 13:03:38 +053023#include <phy-sun4i-usb.h>
Jagan Tekice8a6972018-12-31 17:05:40 +053024#include <reset.h>
Hans de Goede9c4f11d2015-01-11 20:34:48 +010025#include <asm/arch/cpu.h>
Hans de Goede7e5aabd2015-04-27 11:44:22 +020026#include <asm/arch/clock.h>
Simon Glass9bc15642020-02-03 07:36:16 -070027#include <dm/device_compat.h>
Hans de Goede0b3845a2015-06-17 17:44:58 +020028#include <dm/lists.h>
29#include <dm/root.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060030#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060031#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060032#include <linux/printk.h>
Hans de Goedeea059bf2015-06-17 15:49:26 +020033#include <linux/usb/musb.h>
Hans de Goede9c4f11d2015-01-11 20:34:48 +010034#include "linux-compat.h"
35#include "musb_core.h"
Hans de Goede0b3845a2015-06-17 17:44:58 +020036#include "musb_uboot.h"
Hans de Goede9c4f11d2015-01-11 20:34:48 +010037
38/******************************************************************************
39 ******************************************************************************
40 * From the Allwinner driver
41 ******************************************************************************
42 ******************************************************************************/
43
44/******************************************************************************
45 * From include/sunxi_usb_bsp.h
46 ******************************************************************************/
47
48/* reg offsets */
49#define USBC_REG_o_ISCR 0x0400
50#define USBC_REG_o_PHYCTL 0x0404
51#define USBC_REG_o_PHYBIST 0x0408
52#define USBC_REG_o_PHYTUNE 0x040c
53
54#define USBC_REG_o_VEND0 0x0043
55
56/* Interface Status and Control */
57#define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
58#define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
59#define USBC_BP_ISCR_EXT_ID_STATUS 28
60#define USBC_BP_ISCR_EXT_DM_STATUS 27
61#define USBC_BP_ISCR_EXT_DP_STATUS 26
62#define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
63#define USBC_BP_ISCR_MERGED_ID_STATUS 24
64
65#define USBC_BP_ISCR_ID_PULLUP_EN 17
66#define USBC_BP_ISCR_DPDM_PULLUP_EN 16
67#define USBC_BP_ISCR_FORCE_ID 14
68#define USBC_BP_ISCR_FORCE_VBUS_VALID 12
69#define USBC_BP_ISCR_VBUS_VALID_SRC 10
70
71#define USBC_BP_ISCR_HOSC_EN 7
72#define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
73#define USBC_BP_ISCR_ID_CHANGE_DETECT 5
74#define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
75#define USBC_BP_ISCR_IRQ_ENABLE 3
76#define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
77#define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
78#define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
79
80/******************************************************************************
81 * From usbc/usbc.c
82 ******************************************************************************/
83
Jagan Teki65ce4962018-05-07 13:03:20 +053084struct sunxi_musb_config {
85 struct musb_hdrc_config *config;
86};
87
Jagan Tekifda15692018-05-07 13:03:17 +053088struct sunxi_glue {
89 struct musb_host_data mdata;
Jagan Tekice8a6972018-12-31 17:05:40 +053090 struct clk clk;
91 struct reset_ctl rst;
Jagan Teki65ce4962018-05-07 13:03:20 +053092 struct sunxi_musb_config *cfg;
Jagan Tekifda15692018-05-07 13:03:17 +053093 struct device dev;
Jagan Tekid0edfee2018-07-20 12:43:57 +053094 struct phy phy;
Jagan Tekifda15692018-05-07 13:03:17 +053095};
96#define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
97
Hans de Goede9c4f11d2015-01-11 20:34:48 +010098static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
99{
100 u32 temp = reg_val;
101
Jagan Tekicecffaa2018-05-07 13:03:23 +0530102 temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
103 temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
104 temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100105
106 return temp;
107}
108
109static void USBC_EnableIdPullUp(__iomem void *base)
110{
111 u32 reg_val;
112
113 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530114 reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100115 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
116 musb_writel(base, USBC_REG_o_ISCR, reg_val);
117}
118
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100119static void USBC_EnableDpDmPullUp(__iomem void *base)
120{
121 u32 reg_val;
122
123 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530124 reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100125 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
126 musb_writel(base, USBC_REG_o_ISCR, reg_val);
127}
128
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100129static void USBC_ForceIdToLow(__iomem void *base)
130{
131 u32 reg_val;
132
133 reg_val = musb_readl(base, USBC_REG_o_ISCR);
134 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
135 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
136 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
137 musb_writel(base, USBC_REG_o_ISCR, reg_val);
138}
139
140static void USBC_ForceIdToHigh(__iomem void *base)
141{
142 u32 reg_val;
143
144 reg_val = musb_readl(base, USBC_REG_o_ISCR);
145 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
146 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
147 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
148 musb_writel(base, USBC_REG_o_ISCR, reg_val);
149}
150
Hans de Goede836e3342015-06-14 11:55:28 +0200151static void USBC_ForceVbusValidToLow(__iomem void *base)
152{
153 u32 reg_val;
154
155 reg_val = musb_readl(base, USBC_REG_o_ISCR);
156 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
157 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
158 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
159 musb_writel(base, USBC_REG_o_ISCR, reg_val);
160}
161
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100162static void USBC_ForceVbusValidToHigh(__iomem void *base)
163{
164 u32 reg_val;
165
166 reg_val = musb_readl(base, USBC_REG_o_ISCR);
167 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
168 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
169 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
170 musb_writel(base, USBC_REG_o_ISCR, reg_val);
171}
172
173static void USBC_ConfigFIFO_Base(void)
174{
175 u32 reg_value;
176
177 /* config usb fifo, 8kb mode */
178 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
179 reg_value &= ~(0x03 << 0);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530180 reg_value |= BIT(0);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100181 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
182}
183
184/******************************************************************************
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200185 * Needed for the DFU polling magic
186 ******************************************************************************/
187
188static u8 last_int_usb;
189
190bool dfu_usb_get_reset(void)
191{
192 return !!(last_int_usb & MUSB_INTR_RESET);
193}
194
195/******************************************************************************
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100196 * MUSB Glue code
197 ******************************************************************************/
198
199static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
200{
201 struct musb *musb = __hci;
202 irqreturn_t retval = IRQ_NONE;
203
204 /* read and flush interrupts */
205 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200206 last_int_usb = musb->int_usb;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100207 if (musb->int_usb)
208 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
209 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
210 if (musb->int_tx)
211 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
212 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
213 if (musb->int_rx)
214 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
215
216 if (musb->int_usb || musb->int_tx || musb->int_rx)
217 retval |= musb_interrupt(musb);
218
219 return retval;
220}
221
Hans de Goede836e3342015-06-14 11:55:28 +0200222/* musb_core does not call enable / disable in a balanced manner <sigh> */
223static bool enabled = false;
224
Hans de Goede81c49982015-06-17 21:33:54 +0200225static int sunxi_musb_enable(struct musb *musb)
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100226{
Jagan Teki137fc752018-05-07 13:03:38 +0530227 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800228 int ret;
229
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100230 pr_debug("%s():\n", __func__);
231
Maxime Ripard9634fa52015-08-04 17:04:10 +0200232 musb_ep_select(musb->mregs, 0);
233 musb_writeb(musb->mregs, MUSB_FADDR, 0);
234
Hans de Goede836e3342015-06-14 11:55:28 +0200235 if (enabled)
Hans de Goede81c49982015-06-17 21:33:54 +0200236 return 0;
Hans de Goede836e3342015-06-14 11:55:28 +0200237
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100238 /* select PIO mode */
239 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
240
Hans de Goedec93ecd02015-06-14 16:48:56 +0200241 if (is_host_enabled(musb)) {
Jagan Tekid0edfee2018-07-20 12:43:57 +0530242 ret = sun4i_usb_phy_id_detect(&glue->phy);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800243 if (ret == 1) {
Hans de Goedec77fbfb2015-06-14 17:40:37 +0200244 printf("No host cable detected: ");
245 return -ENODEV;
246 }
Jagan Teki137fc752018-05-07 13:03:38 +0530247
Jagan Tekid0edfee2018-07-20 12:43:57 +0530248 ret = generic_phy_power_on(&glue->phy);
Jagan Teki137fc752018-05-07 13:03:38 +0530249 if (ret) {
Patrick Delaunay1b9711b2020-07-03 17:36:45 +0200250 pr_debug("failed to power on USB PHY\n");
Jagan Teki137fc752018-05-07 13:03:38 +0530251 return ret;
252 }
Hans de Goedec93ecd02015-06-14 16:48:56 +0200253 }
Hans de Goede836e3342015-06-14 11:55:28 +0200254
255 USBC_ForceVbusValidToHigh(musb->mregs);
256
257 enabled = true;
Hans de Goede81c49982015-06-17 21:33:54 +0200258 return 0;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100259}
260
261static void sunxi_musb_disable(struct musb *musb)
262{
Jagan Teki137fc752018-05-07 13:03:38 +0530263 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
264 int ret;
265
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100266 pr_debug("%s():\n", __func__);
267
Hans de Goede836e3342015-06-14 11:55:28 +0200268 if (!enabled)
269 return;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200270
Jagan Teki137fc752018-05-07 13:03:38 +0530271 if (is_host_enabled(musb)) {
Jagan Tekid0edfee2018-07-20 12:43:57 +0530272 ret = generic_phy_power_off(&glue->phy);
Jagan Teki137fc752018-05-07 13:03:38 +0530273 if (ret) {
Patrick Delaunay1b9711b2020-07-03 17:36:45 +0200274 pr_debug("failed to power off USB PHY\n");
Jagan Teki137fc752018-05-07 13:03:38 +0530275 return;
276 }
277 }
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800278
Hans de Goede836e3342015-06-14 11:55:28 +0200279 USBC_ForceVbusValidToLow(musb->mregs);
280 mdelay(200); /* Wait for the current session to timeout */
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200281
Hans de Goede836e3342015-06-14 11:55:28 +0200282 enabled = false;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100283}
284
285static int sunxi_musb_init(struct musb *musb)
286{
Jagan Tekifda15692018-05-07 13:03:17 +0530287 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Jagan Teki137fc752018-05-07 13:03:38 +0530288 int ret;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100289
290 pr_debug("%s():\n", __func__);
291
Jagan Tekice8a6972018-12-31 17:05:40 +0530292 ret = clk_enable(&glue->clk);
Jagan Teki137fc752018-05-07 13:03:38 +0530293 if (ret) {
Sean Anderson46362c72020-09-15 10:45:19 -0400294 dev_err(musb->controller, "failed to enable clock\n");
Jagan Teki137fc752018-05-07 13:03:38 +0530295 return ret;
296 }
297
Jagan Tekice8a6972018-12-31 17:05:40 +0530298 if (reset_valid(&glue->rst)) {
299 ret = reset_deassert(&glue->rst);
300 if (ret) {
Sean Anderson46362c72020-09-15 10:45:19 -0400301 dev_err(musb->controller, "failed to deassert reset\n");
Jagan Tekice8a6972018-12-31 17:05:40 +0530302 goto err_clk;
303 }
304 }
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530305
Jagan Tekice8a6972018-12-31 17:05:40 +0530306 ret = generic_phy_init(&glue->phy);
307 if (ret) {
Sean Anderson46362c72020-09-15 10:45:19 -0400308 dev_dbg(musb->controller, "failed to init USB PHY\n");
Jagan Tekice8a6972018-12-31 17:05:40 +0530309 goto err_rst;
310 }
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530311
Jagan Tekice8a6972018-12-31 17:05:40 +0530312 musb->isr = sunxi_musb_interrupt;
Jagan Teki767a58e2018-05-07 13:03:22 +0530313
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100314 USBC_ConfigFIFO_Base();
315 USBC_EnableDpDmPullUp(musb->mregs);
316 USBC_EnableIdPullUp(musb->mregs);
317
318 if (is_host_enabled(musb)) {
319 /* Host mode */
320 USBC_ForceIdToLow(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100321 } else {
322 /* Peripheral mode */
323 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100324 }
Hans de Goedef2b45c82015-02-11 09:05:18 +0100325 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100326
327 return 0;
Jagan Tekice8a6972018-12-31 17:05:40 +0530328
329err_rst:
330 if (reset_valid(&glue->rst))
331 reset_assert(&glue->rst);
332err_clk:
333 clk_disable(&glue->clk);
334 return ret;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100335}
336
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530337static int sunxi_musb_exit(struct musb *musb)
338{
339 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
340 int ret = 0;
341
342 if (generic_phy_valid(&glue->phy)) {
343 ret = generic_phy_exit(&glue->phy);
344 if (ret) {
Sean Anderson46362c72020-09-15 10:45:19 -0400345 dev_dbg(musb->controller,
346 "failed to power off usb phy\n");
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530347 return ret;
348 }
349 }
350
Jagan Tekice8a6972018-12-31 17:05:40 +0530351 if (reset_valid(&glue->rst))
352 reset_assert(&glue->rst);
353 clk_disable(&glue->clk);
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530354
355 return 0;
356}
357
Jagan Teki37671e12018-05-07 13:03:37 +0530358static void sunxi_musb_pre_root_reset_end(struct musb *musb)
359{
360 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
361
Jagan Tekid0edfee2018-07-20 12:43:57 +0530362 sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
Jagan Teki37671e12018-05-07 13:03:37 +0530363}
364
365static void sunxi_musb_post_root_reset_end(struct musb *musb)
366{
367 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
368
Jagan Tekid0edfee2018-07-20 12:43:57 +0530369 sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
Jagan Teki37671e12018-05-07 13:03:37 +0530370}
371
Hans de Goedeea059bf2015-06-17 15:49:26 +0200372static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100373 .init = sunxi_musb_init,
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530374 .exit = sunxi_musb_exit,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100375 .enable = sunxi_musb_enable,
376 .disable = sunxi_musb_disable,
Jagan Teki37671e12018-05-07 13:03:37 +0530377 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
378 .post_root_reset_end = sunxi_musb_post_root_reset_end,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100379};
Hans de Goedeea059bf2015-06-17 15:49:26 +0200380
Jagan Teki4e5af302018-05-07 13:03:18 +0530381/* Allwinner OTG supports up to 5 endpoints */
382#define SUNXI_MUSB_MAX_EP_NUM 6
383#define SUNXI_MUSB_RAM_BITS 11
384
Jagan Teki65ce4962018-05-07 13:03:20 +0530385static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
386 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
387 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
388 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
389 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
390 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
391 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
392 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
393 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
394 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
395 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
396};
397
398/* H3/V3s OTG supports only 4 endpoints */
399#define SUNXI_MUSB_MAX_EP_NUM_H3 5
400
401static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
402 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
403 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
404 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
405 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
406 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
407 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
408 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
409 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
410};
411
Hans de Goedeea059bf2015-06-17 15:49:26 +0200412static struct musb_hdrc_config musb_config = {
Jagan Teki65ce4962018-05-07 13:03:20 +0530413 .fifo_cfg = sunxi_musb_mode_cfg,
414 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
Jagan Teki4e5af302018-05-07 13:03:18 +0530415 .multipoint = true,
416 .dyn_fifo = true,
417 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
418 .ram_bits = SUNXI_MUSB_RAM_BITS,
Hans de Goedeea059bf2015-06-17 15:49:26 +0200419};
420
Jagan Teki65ce4962018-05-07 13:03:20 +0530421static struct musb_hdrc_config musb_config_h3 = {
422 .fifo_cfg = sunxi_musb_mode_cfg_h3,
423 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
424 .multipoint = true,
425 .dyn_fifo = true,
426 .soft_con = true,
427 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
428 .ram_bits = SUNXI_MUSB_RAM_BITS,
429};
430
Hans de Goede4a67d732016-09-17 16:02:38 +0200431static int musb_usb_probe(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200432{
Jagan Tekifda15692018-05-07 13:03:17 +0530433 struct sunxi_glue *glue = dev_get_priv(dev);
434 struct musb_host_data *host = &glue->mdata;
Jagan Teki69bb76e2018-05-07 13:03:19 +0530435 struct musb_hdrc_platform_data pdata;
Chen-Yu Tsai63107552017-12-30 20:44:07 +0800436 void *base = dev_read_addr_ptr(dev);
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200437 int ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200438
Stefan Mavrodiev4beaec12018-12-05 14:49:44 +0200439#ifdef CONFIG_USB_MUSB_HOST
440 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
441#endif
442
Chen-Yu Tsai63107552017-12-30 20:44:07 +0800443 if (!base)
444 return -EINVAL;
445
Jagan Teki65ce4962018-05-07 13:03:20 +0530446 glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
447 if (!glue->cfg)
448 return -EINVAL;
449
Jagan Tekice8a6972018-12-31 17:05:40 +0530450 ret = clk_get_by_index(dev, 0, &glue->clk);
451 if (ret) {
452 dev_err(dev, "failed to get clock\n");
453 return ret;
454 }
Jagan Tekifda15692018-05-07 13:03:17 +0530455
Jagan Tekice8a6972018-12-31 17:05:40 +0530456 ret = reset_get_by_index(dev, 0, &glue->rst);
457 if (ret && ret != -ENOENT) {
458 dev_err(dev, "failed to get reset\n");
459 return ret;
460 }
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530461
Jagan Tekid0edfee2018-07-20 12:43:57 +0530462 ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
Jagan Teki137fc752018-05-07 13:03:38 +0530463 if (ret) {
464 pr_err("failed to get usb PHY\n");
465 return ret;
466 }
467
Jagan Teki69bb76e2018-05-07 13:03:19 +0530468 memset(&pdata, 0, sizeof(pdata));
469 pdata.power = 250;
470 pdata.platform_ops = &sunxi_musb_ops;
Jagan Teki65ce4962018-05-07 13:03:20 +0530471 pdata.config = glue->cfg->config;
Jagan Teki69bb76e2018-05-07 13:03:19 +0530472
Maxime Ripard847bcf02017-09-05 22:10:35 +0200473#ifdef CONFIG_USB_MUSB_HOST
Stefan Mavrodiev4beaec12018-12-05 14:49:44 +0200474 priv->desc_before_addr = true;
475
Jagan Teki69bb76e2018-05-07 13:03:19 +0530476 pdata.mode = MUSB_HOST;
477 host->host = musb_init_controller(&pdata, &glue->dev, base);
Hans de Goedeec89e162016-04-02 20:46:09 +0200478 if (!host->host)
479 return -EIO;
480
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200481 ret = musb_lowlevel_init(host);
Maxime Ripard847bcf02017-09-05 22:10:35 +0200482 if (!ret)
483 printf("Allwinner mUSB OTG (Host)\n");
484#else
Jagan Teki69bb76e2018-05-07 13:03:19 +0530485 pdata.mode = MUSB_PERIPHERAL;
Jagan Tekif0a6d792018-07-20 12:43:56 +0530486 host->host = musb_register(&pdata, &glue->dev, base);
Sam Edwards45105062023-06-05 11:19:37 -0600487 if (IS_ERR_OR_NULL(host->host))
Jagan Tekif0a6d792018-07-20 12:43:56 +0530488 return -EIO;
489
490 printf("Allwinner mUSB OTG (Peripheral)\n");
Maxime Ripard847bcf02017-09-05 22:10:35 +0200491#endif
Hans de Goede0b3845a2015-06-17 17:44:58 +0200492
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200493 return ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200494}
495
Hans de Goede4a67d732016-09-17 16:02:38 +0200496static int musb_usb_remove(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200497{
Jagan Tekifda15692018-05-07 13:03:17 +0530498 struct sunxi_glue *glue = dev_get_priv(dev);
499 struct musb_host_data *host = &glue->mdata;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200500
501 musb_stop(host->host);
Hans de Goede4a67d732016-09-17 16:02:38 +0200502 free(host->host);
503 host->host = NULL;
504
Hans de Goede0b3845a2015-06-17 17:44:58 +0200505 return 0;
506}
507
Andre Przywara0bde97a2023-10-13 23:12:14 +0100508/*
509 * The Linux driver has a config struct, its fields mapping to this driver
510 * like this:
511 * .hdrc_config:
512 * sunxi_musb_hdrc_config_5eps => musb_config
513 * sunxi_musb_hdrc_config_4eps => musb_config_h3
514 * .has_sram: always enabled, ideally no-op on SoCs not using it
515 * .has_reset: automatically detected from DT
516 * .no_configdata: handled via Kconfig's CONFIG_USB_MUSB_FIXED_CONFIGDATA
517 */
Jagan Teki65ce4962018-05-07 13:03:20 +0530518static const struct sunxi_musb_config sun4i_a10_cfg = {
519 .config = &musb_config,
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530520};
521
522static const struct sunxi_musb_config sun6i_a31_cfg = {
523 .config = &musb_config,
Jagan Teki65ce4962018-05-07 13:03:20 +0530524};
525
526static const struct sunxi_musb_config sun8i_h3_cfg = {
527 .config = &musb_config_h3,
528};
529
Andre Przywara0bde97a2023-10-13 23:12:14 +0100530static const struct sunxi_musb_config suniv_f1c100s_cfg = {
531 .config = &musb_config,
532};
533
Maxime Ripard847bcf02017-09-05 22:10:35 +0200534static const struct udevice_id sunxi_musb_ids[] = {
Jagan Teki65ce4962018-05-07 13:03:20 +0530535 { .compatible = "allwinner,sun4i-a10-musb",
536 .data = (ulong)&sun4i_a10_cfg },
537 { .compatible = "allwinner,sun6i-a31-musb",
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530538 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki65ce4962018-05-07 13:03:20 +0530539 { .compatible = "allwinner,sun8i-a33-musb",
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530540 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki65ce4962018-05-07 13:03:20 +0530541 { .compatible = "allwinner,sun8i-h3-musb",
542 .data = (ulong)&sun8i_h3_cfg },
Andre Przywara0bde97a2023-10-13 23:12:14 +0100543 { .compatible = "allwinner,suniv-f1c100s-musb",
544 .data = (ulong)&suniv_f1c100s_cfg },
Maxime Ripard847bcf02017-09-05 22:10:35 +0200545 { }
Hans de Goede0b3845a2015-06-17 17:44:58 +0200546};
Hans de Goede0b3845a2015-06-17 17:44:58 +0200547
Maxime Ripard847bcf02017-09-05 22:10:35 +0200548U_BOOT_DRIVER(usb_musb) = {
549 .name = "sunxi-musb",
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200550#ifdef CONFIG_USB_MUSB_HOST
Maxime Ripard847bcf02017-09-05 22:10:35 +0200551 .id = UCLASS_USB,
Hans de Goede0b3845a2015-06-17 17:44:58 +0200552#else
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100553 .id = UCLASS_USB_GADGET_GENERIC,
Hans de Goede0b3845a2015-06-17 17:44:58 +0200554#endif
Maxime Ripard847bcf02017-09-05 22:10:35 +0200555 .of_match = sunxi_musb_ids,
556 .probe = musb_usb_probe,
557 .remove = musb_usb_remove,
558#ifdef CONFIG_USB_MUSB_HOST
559 .ops = &musb_usb_ops,
560#endif
Simon Glassb75b15b2020-12-03 16:55:23 -0700561 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700562 .priv_auto = sizeof(struct sunxi_glue),
Maxime Ripard847bcf02017-09-05 22:10:35 +0200563};