blob: fea4105f3d10fa44af830e1750393646723864d1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Hans de Goede28a15ef2015-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 Goede28a15ef2015-01-11 20:34:48 +010017 */
18#include <common.h>
Jagan Tekib9aa0a92018-12-31 17:05:40 +053019#include <clk.h>
Simon Glass9d922452017-05-17 17:18:03 -060020#include <dm.h>
Jagan Tekidd322812018-05-07 13:03:38 +053021#include <generic-phy.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060022#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <malloc.h>
Jagan Tekidd322812018-05-07 13:03:38 +053024#include <phy-sun4i-usb.h>
Jagan Tekib9aa0a92018-12-31 17:05:40 +053025#include <reset.h>
Hans de Goede28a15ef2015-01-11 20:34:48 +010026#include <asm/arch/cpu.h>
Hans de Goede375de012015-04-27 11:44:22 +020027#include <asm/arch/clock.h>
Hans de Goede52defe82015-02-16 22:13:43 +010028#include <asm/arch/gpio.h>
Hans de Goede52defe82015-02-16 22:13:43 +010029#include <asm-generic/gpio.h>
Simon Glass336d4612020-02-03 07:36:16 -070030#include <dm/device_compat.h>
Hans de Goede91183ba2015-06-17 17:44:58 +020031#include <dm/lists.h>
32#include <dm/root.h>
Simon Glasscd93d622020-05-10 11:40:13 -060033#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060034#include <linux/delay.h>
Hans de Goeded42faf32015-06-17 15:49:26 +020035#include <linux/usb/musb.h>
Hans de Goede28a15ef2015-01-11 20:34:48 +010036#include "linux-compat.h"
37#include "musb_core.h"
Hans de Goede91183ba2015-06-17 17:44:58 +020038#include "musb_uboot.h"
Hans de Goede28a15ef2015-01-11 20:34:48 +010039
40/******************************************************************************
41 ******************************************************************************
42 * From the Allwinner driver
43 ******************************************************************************
44 ******************************************************************************/
45
46/******************************************************************************
47 * From include/sunxi_usb_bsp.h
48 ******************************************************************************/
49
50/* reg offsets */
51#define USBC_REG_o_ISCR 0x0400
52#define USBC_REG_o_PHYCTL 0x0404
53#define USBC_REG_o_PHYBIST 0x0408
54#define USBC_REG_o_PHYTUNE 0x040c
55
56#define USBC_REG_o_VEND0 0x0043
57
58/* Interface Status and Control */
59#define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
60#define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
61#define USBC_BP_ISCR_EXT_ID_STATUS 28
62#define USBC_BP_ISCR_EXT_DM_STATUS 27
63#define USBC_BP_ISCR_EXT_DP_STATUS 26
64#define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
65#define USBC_BP_ISCR_MERGED_ID_STATUS 24
66
67#define USBC_BP_ISCR_ID_PULLUP_EN 17
68#define USBC_BP_ISCR_DPDM_PULLUP_EN 16
69#define USBC_BP_ISCR_FORCE_ID 14
70#define USBC_BP_ISCR_FORCE_VBUS_VALID 12
71#define USBC_BP_ISCR_VBUS_VALID_SRC 10
72
73#define USBC_BP_ISCR_HOSC_EN 7
74#define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
75#define USBC_BP_ISCR_ID_CHANGE_DETECT 5
76#define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
77#define USBC_BP_ISCR_IRQ_ENABLE 3
78#define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
79#define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
80#define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
81
82/******************************************************************************
83 * From usbc/usbc.c
84 ******************************************************************************/
85
Jagan Teki1034bcc2018-07-20 12:43:59 +053086#define OFF_SUN6I_AHB_RESET0 0x2c0
87
Jagan Teki97202dd2018-05-07 13:03:20 +053088struct sunxi_musb_config {
89 struct musb_hdrc_config *config;
90};
91
Jagan Teki831cc982018-05-07 13:03:17 +053092struct sunxi_glue {
93 struct musb_host_data mdata;
Jagan Tekib9aa0a92018-12-31 17:05:40 +053094 struct clk clk;
95 struct reset_ctl rst;
Jagan Teki97202dd2018-05-07 13:03:20 +053096 struct sunxi_musb_config *cfg;
Jagan Teki831cc982018-05-07 13:03:17 +053097 struct device dev;
Jagan Teki622fd2b2018-07-20 12:43:57 +053098 struct phy phy;
Jagan Teki831cc982018-05-07 13:03:17 +053099};
100#define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
101
Hans de Goede28a15ef2015-01-11 20:34:48 +0100102static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
103{
104 u32 temp = reg_val;
105
Jagan Teki5c5fe882018-05-07 13:03:23 +0530106 temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
107 temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
108 temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100109
110 return temp;
111}
112
113static void USBC_EnableIdPullUp(__iomem void *base)
114{
115 u32 reg_val;
116
117 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Teki5c5fe882018-05-07 13:03:23 +0530118 reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100119 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
120 musb_writel(base, USBC_REG_o_ISCR, reg_val);
121}
122
Hans de Goede28a15ef2015-01-11 20:34:48 +0100123static void USBC_EnableDpDmPullUp(__iomem void *base)
124{
125 u32 reg_val;
126
127 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Teki5c5fe882018-05-07 13:03:23 +0530128 reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100129 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
130 musb_writel(base, USBC_REG_o_ISCR, reg_val);
131}
132
Hans de Goede28a15ef2015-01-11 20:34:48 +0100133static void USBC_ForceIdToLow(__iomem void *base)
134{
135 u32 reg_val;
136
137 reg_val = musb_readl(base, USBC_REG_o_ISCR);
138 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
139 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
140 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
141 musb_writel(base, USBC_REG_o_ISCR, reg_val);
142}
143
144static void USBC_ForceIdToHigh(__iomem void *base)
145{
146 u32 reg_val;
147
148 reg_val = musb_readl(base, USBC_REG_o_ISCR);
149 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
150 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
151 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
152 musb_writel(base, USBC_REG_o_ISCR, reg_val);
153}
154
Hans de Goedee1abfa42015-06-14 11:55:28 +0200155static void USBC_ForceVbusValidToLow(__iomem void *base)
156{
157 u32 reg_val;
158
159 reg_val = musb_readl(base, USBC_REG_o_ISCR);
160 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
161 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
162 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
163 musb_writel(base, USBC_REG_o_ISCR, reg_val);
164}
165
Hans de Goede28a15ef2015-01-11 20:34:48 +0100166static void USBC_ForceVbusValidToHigh(__iomem void *base)
167{
168 u32 reg_val;
169
170 reg_val = musb_readl(base, USBC_REG_o_ISCR);
171 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
172 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
173 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
174 musb_writel(base, USBC_REG_o_ISCR, reg_val);
175}
176
177static void USBC_ConfigFIFO_Base(void)
178{
179 u32 reg_value;
180
181 /* config usb fifo, 8kb mode */
182 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
183 reg_value &= ~(0x03 << 0);
Jagan Teki5c5fe882018-05-07 13:03:23 +0530184 reg_value |= BIT(0);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100185 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
186}
187
188/******************************************************************************
Siarhei Siamashka6047a3a2015-10-25 06:44:47 +0200189 * Needed for the DFU polling magic
190 ******************************************************************************/
191
192static u8 last_int_usb;
193
194bool dfu_usb_get_reset(void)
195{
196 return !!(last_int_usb & MUSB_INTR_RESET);
197}
198
199/******************************************************************************
Hans de Goede28a15ef2015-01-11 20:34:48 +0100200 * MUSB Glue code
201 ******************************************************************************/
202
203static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
204{
205 struct musb *musb = __hci;
206 irqreturn_t retval = IRQ_NONE;
207
208 /* read and flush interrupts */
209 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
Siarhei Siamashka6047a3a2015-10-25 06:44:47 +0200210 last_int_usb = musb->int_usb;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100211 if (musb->int_usb)
212 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
213 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
214 if (musb->int_tx)
215 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
216 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
217 if (musb->int_rx)
218 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
219
220 if (musb->int_usb || musb->int_tx || musb->int_rx)
221 retval |= musb_interrupt(musb);
222
223 return retval;
224}
225
Hans de Goedee1abfa42015-06-14 11:55:28 +0200226/* musb_core does not call enable / disable in a balanced manner <sigh> */
227static bool enabled = false;
228
Hans de Goede15837232015-06-17 21:33:54 +0200229static int sunxi_musb_enable(struct musb *musb)
Hans de Goede28a15ef2015-01-11 20:34:48 +0100230{
Jagan Tekidd322812018-05-07 13:03:38 +0530231 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800232 int ret;
233
Hans de Goede28a15ef2015-01-11 20:34:48 +0100234 pr_debug("%s():\n", __func__);
235
Maxime Ripard1feda632015-08-04 17:04:10 +0200236 musb_ep_select(musb->mregs, 0);
237 musb_writeb(musb->mregs, MUSB_FADDR, 0);
238
Hans de Goedee1abfa42015-06-14 11:55:28 +0200239 if (enabled)
Hans de Goede15837232015-06-17 21:33:54 +0200240 return 0;
Hans de Goedee1abfa42015-06-14 11:55:28 +0200241
Hans de Goede28a15ef2015-01-11 20:34:48 +0100242 /* select PIO mode */
243 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
244
Hans de Goedeb41972e2015-06-14 16:48:56 +0200245 if (is_host_enabled(musb)) {
Jagan Teki622fd2b2018-07-20 12:43:57 +0530246 ret = sun4i_usb_phy_vbus_detect(&glue->phy);
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800247 if (ret == 1) {
248 printf("A charger is plugged into the OTG: ");
249 return -ENODEV;
Hans de Goedeb41972e2015-06-14 16:48:56 +0200250 }
Jagan Tekidd322812018-05-07 13:03:38 +0530251
Jagan Teki622fd2b2018-07-20 12:43:57 +0530252 ret = sun4i_usb_phy_id_detect(&glue->phy);
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800253 if (ret == 1) {
Hans de Goede71cbe0d2015-06-14 17:40:37 +0200254 printf("No host cable detected: ");
255 return -ENODEV;
256 }
Jagan Tekidd322812018-05-07 13:03:38 +0530257
Jagan Teki622fd2b2018-07-20 12:43:57 +0530258 ret = generic_phy_power_on(&glue->phy);
Jagan Tekidd322812018-05-07 13:03:38 +0530259 if (ret) {
Patrick Delaunayc1e1dbb2020-07-03 17:36:45 +0200260 pr_debug("failed to power on USB PHY\n");
Jagan Tekidd322812018-05-07 13:03:38 +0530261 return ret;
262 }
Hans de Goedeb41972e2015-06-14 16:48:56 +0200263 }
Hans de Goedee1abfa42015-06-14 11:55:28 +0200264
265 USBC_ForceVbusValidToHigh(musb->mregs);
266
267 enabled = true;
Hans de Goede15837232015-06-17 21:33:54 +0200268 return 0;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100269}
270
271static void sunxi_musb_disable(struct musb *musb)
272{
Jagan Tekidd322812018-05-07 13:03:38 +0530273 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
274 int ret;
275
Hans de Goede28a15ef2015-01-11 20:34:48 +0100276 pr_debug("%s():\n", __func__);
277
Hans de Goedee1abfa42015-06-14 11:55:28 +0200278 if (!enabled)
279 return;
Hans de Goede375de012015-04-27 11:44:22 +0200280
Jagan Tekidd322812018-05-07 13:03:38 +0530281 if (is_host_enabled(musb)) {
Jagan Teki622fd2b2018-07-20 12:43:57 +0530282 ret = generic_phy_power_off(&glue->phy);
Jagan Tekidd322812018-05-07 13:03:38 +0530283 if (ret) {
Patrick Delaunayc1e1dbb2020-07-03 17:36:45 +0200284 pr_debug("failed to power off USB PHY\n");
Jagan Tekidd322812018-05-07 13:03:38 +0530285 return;
286 }
287 }
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800288
Hans de Goedee1abfa42015-06-14 11:55:28 +0200289 USBC_ForceVbusValidToLow(musb->mregs);
290 mdelay(200); /* Wait for the current session to timeout */
291
292 enabled = false;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100293}
294
295static int sunxi_musb_init(struct musb *musb)
296{
Jagan Teki831cc982018-05-07 13:03:17 +0530297 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Jagan Tekidd322812018-05-07 13:03:38 +0530298 int ret;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100299
300 pr_debug("%s():\n", __func__);
301
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530302 ret = clk_enable(&glue->clk);
Jagan Tekidd322812018-05-07 13:03:38 +0530303 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400304 dev_err(musb->controller, "failed to enable clock\n");
Jagan Tekidd322812018-05-07 13:03:38 +0530305 return ret;
306 }
307
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530308 if (reset_valid(&glue->rst)) {
309 ret = reset_deassert(&glue->rst);
310 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400311 dev_err(musb->controller, "failed to deassert reset\n");
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530312 goto err_clk;
313 }
314 }
315
316 ret = generic_phy_init(&glue->phy);
317 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400318 dev_dbg(musb->controller, "failed to init USB PHY\n");
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530319 goto err_rst;
320 }
321
Hans de Goede28a15ef2015-01-11 20:34:48 +0100322 musb->isr = sunxi_musb_interrupt;
Hans de Goede375de012015-04-27 11:44:22 +0200323
Hans de Goede28a15ef2015-01-11 20:34:48 +0100324 USBC_ConfigFIFO_Base();
325 USBC_EnableDpDmPullUp(musb->mregs);
326 USBC_EnableIdPullUp(musb->mregs);
327
328 if (is_host_enabled(musb)) {
329 /* Host mode */
330 USBC_ForceIdToLow(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100331 } else {
332 /* Peripheral mode */
333 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100334 }
Hans de Goedeb1b912d2015-02-11 09:05:18 +0100335 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100336
337 return 0;
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530338
339err_rst:
340 if (reset_valid(&glue->rst))
341 reset_assert(&glue->rst);
342err_clk:
343 clk_disable(&glue->clk);
344 return ret;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100345}
346
Jagan Teki14b6a072018-07-20 12:44:00 +0530347static int sunxi_musb_exit(struct musb *musb)
348{
349 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
350 int ret = 0;
351
352 if (generic_phy_valid(&glue->phy)) {
353 ret = generic_phy_exit(&glue->phy);
354 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400355 dev_dbg(musb->controller,
356 "failed to power off usb phy\n");
Jagan Teki14b6a072018-07-20 12:44:00 +0530357 return ret;
358 }
359 }
360
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530361 if (reset_valid(&glue->rst))
362 reset_assert(&glue->rst);
363 clk_disable(&glue->clk);
Jagan Teki14b6a072018-07-20 12:44:00 +0530364
365 return 0;
366}
367
Jagan Tekiaa29b112018-05-07 13:03:37 +0530368static void sunxi_musb_pre_root_reset_end(struct musb *musb)
369{
370 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
371
Jagan Teki622fd2b2018-07-20 12:43:57 +0530372 sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
Jagan Tekiaa29b112018-05-07 13:03:37 +0530373}
374
375static void sunxi_musb_post_root_reset_end(struct musb *musb)
376{
377 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
378
Jagan Teki622fd2b2018-07-20 12:43:57 +0530379 sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
Jagan Tekiaa29b112018-05-07 13:03:37 +0530380}
381
Hans de Goeded42faf32015-06-17 15:49:26 +0200382static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede28a15ef2015-01-11 20:34:48 +0100383 .init = sunxi_musb_init,
Jagan Teki14b6a072018-07-20 12:44:00 +0530384 .exit = sunxi_musb_exit,
Hans de Goede28a15ef2015-01-11 20:34:48 +0100385 .enable = sunxi_musb_enable,
386 .disable = sunxi_musb_disable,
Jagan Tekiaa29b112018-05-07 13:03:37 +0530387 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
388 .post_root_reset_end = sunxi_musb_post_root_reset_end,
Hans de Goede28a15ef2015-01-11 20:34:48 +0100389};
Hans de Goeded42faf32015-06-17 15:49:26 +0200390
Jagan Tekiae8b78d2018-05-07 13:03:18 +0530391/* Allwinner OTG supports up to 5 endpoints */
392#define SUNXI_MUSB_MAX_EP_NUM 6
393#define SUNXI_MUSB_RAM_BITS 11
394
Jagan Teki97202dd2018-05-07 13:03:20 +0530395static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
396 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
397 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
398 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
399 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
400 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
401 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
402 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
403 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
404 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
405 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
406};
407
408/* H3/V3s OTG supports only 4 endpoints */
409#define SUNXI_MUSB_MAX_EP_NUM_H3 5
410
411static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
412 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
413 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
414 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
415 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
416 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
417 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
418 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
419 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
420};
421
Hans de Goeded42faf32015-06-17 15:49:26 +0200422static struct musb_hdrc_config musb_config = {
Jagan Teki97202dd2018-05-07 13:03:20 +0530423 .fifo_cfg = sunxi_musb_mode_cfg,
424 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
Jagan Tekiae8b78d2018-05-07 13:03:18 +0530425 .multipoint = true,
426 .dyn_fifo = true,
427 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
428 .ram_bits = SUNXI_MUSB_RAM_BITS,
Hans de Goeded42faf32015-06-17 15:49:26 +0200429};
430
Jagan Teki97202dd2018-05-07 13:03:20 +0530431static struct musb_hdrc_config musb_config_h3 = {
432 .fifo_cfg = sunxi_musb_mode_cfg_h3,
433 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
434 .multipoint = true,
435 .dyn_fifo = true,
436 .soft_con = true,
437 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
438 .ram_bits = SUNXI_MUSB_RAM_BITS,
439};
440
Hans de Goede7c22e262016-09-17 16:02:38 +0200441static int musb_usb_probe(struct udevice *dev)
Hans de Goede91183ba2015-06-17 17:44:58 +0200442{
Jagan Teki831cc982018-05-07 13:03:17 +0530443 struct sunxi_glue *glue = dev_get_priv(dev);
444 struct musb_host_data *host = &glue->mdata;
Jagan Teki98424b72018-05-07 13:03:19 +0530445 struct musb_hdrc_platform_data pdata;
Chen-Yu Tsaif4f98962017-12-30 20:44:07 +0800446 void *base = dev_read_addr_ptr(dev);
Hans de Goede56a20852015-06-18 22:45:34 +0200447 int ret;
Hans de Goede91183ba2015-06-17 17:44:58 +0200448
Stefan Mavrodiev46a3f272018-12-05 14:49:44 +0200449#ifdef CONFIG_USB_MUSB_HOST
450 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
451#endif
452
Chen-Yu Tsaif4f98962017-12-30 20:44:07 +0800453 if (!base)
454 return -EINVAL;
455
Jagan Teki97202dd2018-05-07 13:03:20 +0530456 glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
457 if (!glue->cfg)
458 return -EINVAL;
459
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530460 ret = clk_get_by_index(dev, 0, &glue->clk);
461 if (ret) {
462 dev_err(dev, "failed to get clock\n");
463 return ret;
464 }
Jagan Teki831cc982018-05-07 13:03:17 +0530465
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530466 ret = reset_get_by_index(dev, 0, &glue->rst);
467 if (ret && ret != -ENOENT) {
468 dev_err(dev, "failed to get reset\n");
469 return ret;
470 }
Jagan Teki1034bcc2018-07-20 12:43:59 +0530471
Jagan Teki622fd2b2018-07-20 12:43:57 +0530472 ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
Jagan Tekidd322812018-05-07 13:03:38 +0530473 if (ret) {
474 pr_err("failed to get usb PHY\n");
475 return ret;
476 }
477
Jagan Teki98424b72018-05-07 13:03:19 +0530478 memset(&pdata, 0, sizeof(pdata));
479 pdata.power = 250;
480 pdata.platform_ops = &sunxi_musb_ops;
Jagan Teki97202dd2018-05-07 13:03:20 +0530481 pdata.config = glue->cfg->config;
Jagan Teki98424b72018-05-07 13:03:19 +0530482
Maxime Ripard3a61b082017-09-05 22:10:35 +0200483#ifdef CONFIG_USB_MUSB_HOST
Stefan Mavrodiev46a3f272018-12-05 14:49:44 +0200484 priv->desc_before_addr = true;
485
Jagan Teki98424b72018-05-07 13:03:19 +0530486 pdata.mode = MUSB_HOST;
487 host->host = musb_init_controller(&pdata, &glue->dev, base);
Hans de Goede38b4a3e2016-04-02 20:46:09 +0200488 if (!host->host)
489 return -EIO;
490
Hans de Goede56a20852015-06-18 22:45:34 +0200491 ret = musb_lowlevel_init(host);
Maxime Ripard3a61b082017-09-05 22:10:35 +0200492 if (!ret)
493 printf("Allwinner mUSB OTG (Host)\n");
494#else
Jagan Teki98424b72018-05-07 13:03:19 +0530495 pdata.mode = MUSB_PERIPHERAL;
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530496 host->host = musb_register(&pdata, &glue->dev, base);
497 if (!host->host)
498 return -EIO;
499
500 printf("Allwinner mUSB OTG (Peripheral)\n");
Maxime Ripard3a61b082017-09-05 22:10:35 +0200501#endif
Hans de Goede91183ba2015-06-17 17:44:58 +0200502
Hans de Goede56a20852015-06-18 22:45:34 +0200503 return ret;
Hans de Goede91183ba2015-06-17 17:44:58 +0200504}
505
Hans de Goede7c22e262016-09-17 16:02:38 +0200506static int musb_usb_remove(struct udevice *dev)
Hans de Goede91183ba2015-06-17 17:44:58 +0200507{
Jagan Teki831cc982018-05-07 13:03:17 +0530508 struct sunxi_glue *glue = dev_get_priv(dev);
509 struct musb_host_data *host = &glue->mdata;
Hans de Goede91183ba2015-06-17 17:44:58 +0200510
511 musb_stop(host->host);
Hans de Goede7c22e262016-09-17 16:02:38 +0200512 free(host->host);
513 host->host = NULL;
514
Hans de Goede91183ba2015-06-17 17:44:58 +0200515 return 0;
516}
517
Jagan Teki97202dd2018-05-07 13:03:20 +0530518static const struct sunxi_musb_config sun4i_a10_cfg = {
519 .config = &musb_config,
Jagan Teki1034bcc2018-07-20 12:43:59 +0530520};
521
522static const struct sunxi_musb_config sun6i_a31_cfg = {
523 .config = &musb_config,
Jagan Teki97202dd2018-05-07 13:03:20 +0530524};
525
526static const struct sunxi_musb_config sun8i_h3_cfg = {
527 .config = &musb_config_h3,
528};
529
Maxime Ripard3a61b082017-09-05 22:10:35 +0200530static const struct udevice_id sunxi_musb_ids[] = {
Jagan Teki97202dd2018-05-07 13:03:20 +0530531 { .compatible = "allwinner,sun4i-a10-musb",
532 .data = (ulong)&sun4i_a10_cfg },
533 { .compatible = "allwinner,sun6i-a31-musb",
Jagan Teki1034bcc2018-07-20 12:43:59 +0530534 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki97202dd2018-05-07 13:03:20 +0530535 { .compatible = "allwinner,sun8i-a33-musb",
Jagan Teki1034bcc2018-07-20 12:43:59 +0530536 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki97202dd2018-05-07 13:03:20 +0530537 { .compatible = "allwinner,sun8i-h3-musb",
538 .data = (ulong)&sun8i_h3_cfg },
Maxime Ripard3a61b082017-09-05 22:10:35 +0200539 { }
540};
541
Hans de Goede91183ba2015-06-17 17:44:58 +0200542U_BOOT_DRIVER(usb_musb) = {
Maxime Ripard3a61b082017-09-05 22:10:35 +0200543 .name = "sunxi-musb",
544#ifdef CONFIG_USB_MUSB_HOST
545 .id = UCLASS_USB,
546#else
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100547 .id = UCLASS_USB_GADGET_GENERIC,
Maxime Ripard3a61b082017-09-05 22:10:35 +0200548#endif
549 .of_match = sunxi_musb_ids,
550 .probe = musb_usb_probe,
551 .remove = musb_usb_remove,
552#ifdef CONFIG_USB_MUSB_HOST
553 .ops = &musb_usb_ops,
554#endif
Simon Glass8a8d24b2020-12-03 16:55:23 -0700555 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700556 .priv_auto = sizeof(struct sunxi_glue),
Hans de Goede91183ba2015-06-17 17:44:58 +0200557};