blob: dc4cfc2194b4920a5b99cfbb55d19324a5839de0 [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>
Simon Glass336d4612020-02-03 07:36:16 -070028#include <dm/device_compat.h>
Hans de Goede91183ba2015-06-17 17:44:58 +020029#include <dm/lists.h>
30#include <dm/root.h>
Simon Glasscd93d622020-05-10 11:40:13 -060031#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060032#include <linux/delay.h>
Hans de Goeded42faf32015-06-17 15:49:26 +020033#include <linux/usb/musb.h>
Hans de Goede28a15ef2015-01-11 20:34:48 +010034#include "linux-compat.h"
35#include "musb_core.h"
Hans de Goede91183ba2015-06-17 17:44:58 +020036#include "musb_uboot.h"
Hans de Goede28a15ef2015-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 Teki1034bcc2018-07-20 12:43:59 +053084#define OFF_SUN6I_AHB_RESET0 0x2c0
85
Jagan Teki97202dd2018-05-07 13:03:20 +053086struct sunxi_musb_config {
87 struct musb_hdrc_config *config;
88};
89
Jagan Teki831cc982018-05-07 13:03:17 +053090struct sunxi_glue {
91 struct musb_host_data mdata;
Jagan Tekib9aa0a92018-12-31 17:05:40 +053092 struct clk clk;
93 struct reset_ctl rst;
Jagan Teki97202dd2018-05-07 13:03:20 +053094 struct sunxi_musb_config *cfg;
Jagan Teki831cc982018-05-07 13:03:17 +053095 struct device dev;
Jagan Teki622fd2b2018-07-20 12:43:57 +053096 struct phy phy;
Jagan Teki831cc982018-05-07 13:03:17 +053097};
98#define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
99
Hans de Goede28a15ef2015-01-11 20:34:48 +0100100static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
101{
102 u32 temp = reg_val;
103
Jagan Teki5c5fe882018-05-07 13:03:23 +0530104 temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
105 temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
106 temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100107
108 return temp;
109}
110
111static void USBC_EnableIdPullUp(__iomem void *base)
112{
113 u32 reg_val;
114
115 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Teki5c5fe882018-05-07 13:03:23 +0530116 reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100117 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
118 musb_writel(base, USBC_REG_o_ISCR, reg_val);
119}
120
Hans de Goede28a15ef2015-01-11 20:34:48 +0100121static void USBC_EnableDpDmPullUp(__iomem void *base)
122{
123 u32 reg_val;
124
125 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Teki5c5fe882018-05-07 13:03:23 +0530126 reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100127 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
128 musb_writel(base, USBC_REG_o_ISCR, reg_val);
129}
130
Hans de Goede28a15ef2015-01-11 20:34:48 +0100131static void USBC_ForceIdToLow(__iomem void *base)
132{
133 u32 reg_val;
134
135 reg_val = musb_readl(base, USBC_REG_o_ISCR);
136 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
137 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
138 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
139 musb_writel(base, USBC_REG_o_ISCR, reg_val);
140}
141
142static void USBC_ForceIdToHigh(__iomem void *base)
143{
144 u32 reg_val;
145
146 reg_val = musb_readl(base, USBC_REG_o_ISCR);
147 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
148 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
149 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
150 musb_writel(base, USBC_REG_o_ISCR, reg_val);
151}
152
Hans de Goedee1abfa42015-06-14 11:55:28 +0200153static void USBC_ForceVbusValidToLow(__iomem void *base)
154{
155 u32 reg_val;
156
157 reg_val = musb_readl(base, USBC_REG_o_ISCR);
158 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
159 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
160 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
161 musb_writel(base, USBC_REG_o_ISCR, reg_val);
162}
163
Hans de Goede28a15ef2015-01-11 20:34:48 +0100164static void USBC_ForceVbusValidToHigh(__iomem void *base)
165{
166 u32 reg_val;
167
168 reg_val = musb_readl(base, USBC_REG_o_ISCR);
169 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
170 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
171 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
172 musb_writel(base, USBC_REG_o_ISCR, reg_val);
173}
174
175static void USBC_ConfigFIFO_Base(void)
176{
177 u32 reg_value;
178
179 /* config usb fifo, 8kb mode */
180 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
181 reg_value &= ~(0x03 << 0);
Jagan Teki5c5fe882018-05-07 13:03:23 +0530182 reg_value |= BIT(0);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100183 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
184}
185
186/******************************************************************************
Siarhei Siamashka6047a3a2015-10-25 06:44:47 +0200187 * Needed for the DFU polling magic
188 ******************************************************************************/
189
190static u8 last_int_usb;
191
192bool dfu_usb_get_reset(void)
193{
194 return !!(last_int_usb & MUSB_INTR_RESET);
195}
196
197/******************************************************************************
Hans de Goede28a15ef2015-01-11 20:34:48 +0100198 * MUSB Glue code
199 ******************************************************************************/
200
201static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
202{
203 struct musb *musb = __hci;
204 irqreturn_t retval = IRQ_NONE;
205
206 /* read and flush interrupts */
207 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
Siarhei Siamashka6047a3a2015-10-25 06:44:47 +0200208 last_int_usb = musb->int_usb;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100209 if (musb->int_usb)
210 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
211 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
212 if (musb->int_tx)
213 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
214 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
215 if (musb->int_rx)
216 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
217
218 if (musb->int_usb || musb->int_tx || musb->int_rx)
219 retval |= musb_interrupt(musb);
220
221 return retval;
222}
223
Hans de Goedee1abfa42015-06-14 11:55:28 +0200224/* musb_core does not call enable / disable in a balanced manner <sigh> */
225static bool enabled = false;
226
Hans de Goede15837232015-06-17 21:33:54 +0200227static int sunxi_musb_enable(struct musb *musb)
Hans de Goede28a15ef2015-01-11 20:34:48 +0100228{
Jagan Tekidd322812018-05-07 13:03:38 +0530229 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800230 int ret;
231
Hans de Goede28a15ef2015-01-11 20:34:48 +0100232 pr_debug("%s():\n", __func__);
233
Maxime Ripard1feda632015-08-04 17:04:10 +0200234 musb_ep_select(musb->mregs, 0);
235 musb_writeb(musb->mregs, MUSB_FADDR, 0);
236
Hans de Goedee1abfa42015-06-14 11:55:28 +0200237 if (enabled)
Hans de Goede15837232015-06-17 21:33:54 +0200238 return 0;
Hans de Goedee1abfa42015-06-14 11:55:28 +0200239
Hans de Goede28a15ef2015-01-11 20:34:48 +0100240 /* select PIO mode */
241 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
242
Hans de Goedeb41972e2015-06-14 16:48:56 +0200243 if (is_host_enabled(musb)) {
Jagan Teki622fd2b2018-07-20 12:43:57 +0530244 ret = sun4i_usb_phy_id_detect(&glue->phy);
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800245 if (ret == 1) {
Hans de Goede71cbe0d2015-06-14 17:40:37 +0200246 printf("No host cable detected: ");
247 return -ENODEV;
248 }
Jagan Tekidd322812018-05-07 13:03:38 +0530249
Jagan Teki622fd2b2018-07-20 12:43:57 +0530250 ret = generic_phy_power_on(&glue->phy);
Jagan Tekidd322812018-05-07 13:03:38 +0530251 if (ret) {
Patrick Delaunayc1e1dbb2020-07-03 17:36:45 +0200252 pr_debug("failed to power on USB PHY\n");
Jagan Tekidd322812018-05-07 13:03:38 +0530253 return ret;
254 }
Hans de Goedeb41972e2015-06-14 16:48:56 +0200255 }
Hans de Goedee1abfa42015-06-14 11:55:28 +0200256
257 USBC_ForceVbusValidToHigh(musb->mregs);
258
259 enabled = true;
Hans de Goede15837232015-06-17 21:33:54 +0200260 return 0;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100261}
262
263static void sunxi_musb_disable(struct musb *musb)
264{
Jagan Tekidd322812018-05-07 13:03:38 +0530265 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
266 int ret;
267
Hans de Goede28a15ef2015-01-11 20:34:48 +0100268 pr_debug("%s():\n", __func__);
269
Hans de Goedee1abfa42015-06-14 11:55:28 +0200270 if (!enabled)
271 return;
Hans de Goede375de012015-04-27 11:44:22 +0200272
Jagan Tekidd322812018-05-07 13:03:38 +0530273 if (is_host_enabled(musb)) {
Jagan Teki622fd2b2018-07-20 12:43:57 +0530274 ret = generic_phy_power_off(&glue->phy);
Jagan Tekidd322812018-05-07 13:03:38 +0530275 if (ret) {
Patrick Delaunayc1e1dbb2020-07-03 17:36:45 +0200276 pr_debug("failed to power off USB PHY\n");
Jagan Tekidd322812018-05-07 13:03:38 +0530277 return;
278 }
279 }
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800280
Hans de Goedee1abfa42015-06-14 11:55:28 +0200281 USBC_ForceVbusValidToLow(musb->mregs);
282 mdelay(200); /* Wait for the current session to timeout */
283
284 enabled = false;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100285}
286
287static int sunxi_musb_init(struct musb *musb)
288{
Jagan Teki831cc982018-05-07 13:03:17 +0530289 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Jagan Tekidd322812018-05-07 13:03:38 +0530290 int ret;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100291
292 pr_debug("%s():\n", __func__);
293
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530294 ret = clk_enable(&glue->clk);
Jagan Tekidd322812018-05-07 13:03:38 +0530295 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400296 dev_err(musb->controller, "failed to enable clock\n");
Jagan Tekidd322812018-05-07 13:03:38 +0530297 return ret;
298 }
299
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530300 if (reset_valid(&glue->rst)) {
301 ret = reset_deassert(&glue->rst);
302 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400303 dev_err(musb->controller, "failed to deassert reset\n");
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530304 goto err_clk;
305 }
306 }
307
308 ret = generic_phy_init(&glue->phy);
309 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400310 dev_dbg(musb->controller, "failed to init USB PHY\n");
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530311 goto err_rst;
312 }
313
Hans de Goede28a15ef2015-01-11 20:34:48 +0100314 musb->isr = sunxi_musb_interrupt;
Hans de Goede375de012015-04-27 11:44:22 +0200315
Hans de Goede28a15ef2015-01-11 20:34:48 +0100316 USBC_ConfigFIFO_Base();
317 USBC_EnableDpDmPullUp(musb->mregs);
318 USBC_EnableIdPullUp(musb->mregs);
319
320 if (is_host_enabled(musb)) {
321 /* Host mode */
322 USBC_ForceIdToLow(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100323 } else {
324 /* Peripheral mode */
325 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100326 }
Hans de Goedeb1b912d2015-02-11 09:05:18 +0100327 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100328
329 return 0;
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530330
331err_rst:
332 if (reset_valid(&glue->rst))
333 reset_assert(&glue->rst);
334err_clk:
335 clk_disable(&glue->clk);
336 return ret;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100337}
338
Jagan Teki14b6a072018-07-20 12:44:00 +0530339static int sunxi_musb_exit(struct musb *musb)
340{
341 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
342 int ret = 0;
343
344 if (generic_phy_valid(&glue->phy)) {
345 ret = generic_phy_exit(&glue->phy);
346 if (ret) {
Sean Anderson7fe8cfd2020-09-15 10:45:19 -0400347 dev_dbg(musb->controller,
348 "failed to power off usb phy\n");
Jagan Teki14b6a072018-07-20 12:44:00 +0530349 return ret;
350 }
351 }
352
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530353 if (reset_valid(&glue->rst))
354 reset_assert(&glue->rst);
355 clk_disable(&glue->clk);
Jagan Teki14b6a072018-07-20 12:44:00 +0530356
357 return 0;
358}
359
Jagan Tekiaa29b112018-05-07 13:03:37 +0530360static void sunxi_musb_pre_root_reset_end(struct musb *musb)
361{
362 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
363
Jagan Teki622fd2b2018-07-20 12:43:57 +0530364 sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
Jagan Tekiaa29b112018-05-07 13:03:37 +0530365}
366
367static void sunxi_musb_post_root_reset_end(struct musb *musb)
368{
369 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
370
Jagan Teki622fd2b2018-07-20 12:43:57 +0530371 sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
Jagan Tekiaa29b112018-05-07 13:03:37 +0530372}
373
Hans de Goeded42faf32015-06-17 15:49:26 +0200374static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede28a15ef2015-01-11 20:34:48 +0100375 .init = sunxi_musb_init,
Jagan Teki14b6a072018-07-20 12:44:00 +0530376 .exit = sunxi_musb_exit,
Hans de Goede28a15ef2015-01-11 20:34:48 +0100377 .enable = sunxi_musb_enable,
378 .disable = sunxi_musb_disable,
Jagan Tekiaa29b112018-05-07 13:03:37 +0530379 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
380 .post_root_reset_end = sunxi_musb_post_root_reset_end,
Hans de Goede28a15ef2015-01-11 20:34:48 +0100381};
Hans de Goeded42faf32015-06-17 15:49:26 +0200382
Jagan Tekiae8b78d2018-05-07 13:03:18 +0530383/* Allwinner OTG supports up to 5 endpoints */
384#define SUNXI_MUSB_MAX_EP_NUM 6
385#define SUNXI_MUSB_RAM_BITS 11
386
Jagan Teki97202dd2018-05-07 13:03:20 +0530387static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
388 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
389 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
390 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
391 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
392 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
393 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
394 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
395 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
396 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
397 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
398};
399
400/* H3/V3s OTG supports only 4 endpoints */
401#define SUNXI_MUSB_MAX_EP_NUM_H3 5
402
403static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
404 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
405 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
406 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
407 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
408 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
409 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
410 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
411 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
412};
413
Hans de Goeded42faf32015-06-17 15:49:26 +0200414static struct musb_hdrc_config musb_config = {
Jagan Teki97202dd2018-05-07 13:03:20 +0530415 .fifo_cfg = sunxi_musb_mode_cfg,
416 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
Jagan Tekiae8b78d2018-05-07 13:03:18 +0530417 .multipoint = true,
418 .dyn_fifo = true,
419 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
420 .ram_bits = SUNXI_MUSB_RAM_BITS,
Hans de Goeded42faf32015-06-17 15:49:26 +0200421};
422
Jagan Teki97202dd2018-05-07 13:03:20 +0530423static struct musb_hdrc_config musb_config_h3 = {
424 .fifo_cfg = sunxi_musb_mode_cfg_h3,
425 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
426 .multipoint = true,
427 .dyn_fifo = true,
428 .soft_con = true,
429 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
430 .ram_bits = SUNXI_MUSB_RAM_BITS,
431};
432
Hans de Goede7c22e262016-09-17 16:02:38 +0200433static int musb_usb_probe(struct udevice *dev)
Hans de Goede91183ba2015-06-17 17:44:58 +0200434{
Jagan Teki831cc982018-05-07 13:03:17 +0530435 struct sunxi_glue *glue = dev_get_priv(dev);
436 struct musb_host_data *host = &glue->mdata;
Jagan Teki98424b72018-05-07 13:03:19 +0530437 struct musb_hdrc_platform_data pdata;
Chen-Yu Tsaif4f98962017-12-30 20:44:07 +0800438 void *base = dev_read_addr_ptr(dev);
Hans de Goede56a20852015-06-18 22:45:34 +0200439 int ret;
Hans de Goede91183ba2015-06-17 17:44:58 +0200440
Stefan Mavrodiev46a3f272018-12-05 14:49:44 +0200441#ifdef CONFIG_USB_MUSB_HOST
442 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
443#endif
444
Chen-Yu Tsaif4f98962017-12-30 20:44:07 +0800445 if (!base)
446 return -EINVAL;
447
Jagan Teki97202dd2018-05-07 13:03:20 +0530448 glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
449 if (!glue->cfg)
450 return -EINVAL;
451
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530452 ret = clk_get_by_index(dev, 0, &glue->clk);
453 if (ret) {
454 dev_err(dev, "failed to get clock\n");
455 return ret;
456 }
Jagan Teki831cc982018-05-07 13:03:17 +0530457
Jagan Tekib9aa0a92018-12-31 17:05:40 +0530458 ret = reset_get_by_index(dev, 0, &glue->rst);
459 if (ret && ret != -ENOENT) {
460 dev_err(dev, "failed to get reset\n");
461 return ret;
462 }
Jagan Teki1034bcc2018-07-20 12:43:59 +0530463
Jagan Teki622fd2b2018-07-20 12:43:57 +0530464 ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
Jagan Tekidd322812018-05-07 13:03:38 +0530465 if (ret) {
466 pr_err("failed to get usb PHY\n");
467 return ret;
468 }
469
Jagan Teki98424b72018-05-07 13:03:19 +0530470 memset(&pdata, 0, sizeof(pdata));
471 pdata.power = 250;
472 pdata.platform_ops = &sunxi_musb_ops;
Jagan Teki97202dd2018-05-07 13:03:20 +0530473 pdata.config = glue->cfg->config;
Jagan Teki98424b72018-05-07 13:03:19 +0530474
Maxime Ripard3a61b082017-09-05 22:10:35 +0200475#ifdef CONFIG_USB_MUSB_HOST
Stefan Mavrodiev46a3f272018-12-05 14:49:44 +0200476 priv->desc_before_addr = true;
477
Jagan Teki98424b72018-05-07 13:03:19 +0530478 pdata.mode = MUSB_HOST;
479 host->host = musb_init_controller(&pdata, &glue->dev, base);
Hans de Goede38b4a3e2016-04-02 20:46:09 +0200480 if (!host->host)
481 return -EIO;
482
Hans de Goede56a20852015-06-18 22:45:34 +0200483 ret = musb_lowlevel_init(host);
Maxime Ripard3a61b082017-09-05 22:10:35 +0200484 if (!ret)
485 printf("Allwinner mUSB OTG (Host)\n");
486#else
Jagan Teki98424b72018-05-07 13:03:19 +0530487 pdata.mode = MUSB_PERIPHERAL;
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530488 host->host = musb_register(&pdata, &glue->dev, base);
489 if (!host->host)
490 return -EIO;
491
492 printf("Allwinner mUSB OTG (Peripheral)\n");
Maxime Ripard3a61b082017-09-05 22:10:35 +0200493#endif
Hans de Goede91183ba2015-06-17 17:44:58 +0200494
Hans de Goede56a20852015-06-18 22:45:34 +0200495 return ret;
Hans de Goede91183ba2015-06-17 17:44:58 +0200496}
497
Hans de Goede7c22e262016-09-17 16:02:38 +0200498static int musb_usb_remove(struct udevice *dev)
Hans de Goede91183ba2015-06-17 17:44:58 +0200499{
Jagan Teki831cc982018-05-07 13:03:17 +0530500 struct sunxi_glue *glue = dev_get_priv(dev);
501 struct musb_host_data *host = &glue->mdata;
Hans de Goede91183ba2015-06-17 17:44:58 +0200502
503 musb_stop(host->host);
Hans de Goede7c22e262016-09-17 16:02:38 +0200504 free(host->host);
505 host->host = NULL;
506
Hans de Goede91183ba2015-06-17 17:44:58 +0200507 return 0;
508}
509
Jagan Teki97202dd2018-05-07 13:03:20 +0530510static const struct sunxi_musb_config sun4i_a10_cfg = {
511 .config = &musb_config,
Jagan Teki1034bcc2018-07-20 12:43:59 +0530512};
513
514static const struct sunxi_musb_config sun6i_a31_cfg = {
515 .config = &musb_config,
Jagan Teki97202dd2018-05-07 13:03:20 +0530516};
517
518static const struct sunxi_musb_config sun8i_h3_cfg = {
519 .config = &musb_config_h3,
520};
521
Maxime Ripard3a61b082017-09-05 22:10:35 +0200522static const struct udevice_id sunxi_musb_ids[] = {
Jagan Teki97202dd2018-05-07 13:03:20 +0530523 { .compatible = "allwinner,sun4i-a10-musb",
524 .data = (ulong)&sun4i_a10_cfg },
525 { .compatible = "allwinner,sun6i-a31-musb",
Jagan Teki1034bcc2018-07-20 12:43:59 +0530526 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki97202dd2018-05-07 13:03:20 +0530527 { .compatible = "allwinner,sun8i-a33-musb",
Jagan Teki1034bcc2018-07-20 12:43:59 +0530528 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki97202dd2018-05-07 13:03:20 +0530529 { .compatible = "allwinner,sun8i-h3-musb",
530 .data = (ulong)&sun8i_h3_cfg },
Maxime Ripard3a61b082017-09-05 22:10:35 +0200531 { }
532};
533
Hans de Goede91183ba2015-06-17 17:44:58 +0200534U_BOOT_DRIVER(usb_musb) = {
Maxime Ripard3a61b082017-09-05 22:10:35 +0200535 .name = "sunxi-musb",
536#ifdef CONFIG_USB_MUSB_HOST
537 .id = UCLASS_USB,
538#else
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100539 .id = UCLASS_USB_GADGET_GENERIC,
Maxime Ripard3a61b082017-09-05 22:10:35 +0200540#endif
541 .of_match = sunxi_musb_ids,
542 .probe = musb_usb_probe,
543 .remove = musb_usb_remove,
544#ifdef CONFIG_USB_MUSB_HOST
545 .ops = &musb_usb_ops,
546#endif
Simon Glass8a8d24b2020-12-03 16:55:23 -0700547 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700548 .priv_auto = sizeof(struct sunxi_glue),
Hans de Goede91183ba2015-06-17 17:44:58 +0200549};