blob: 98bf73697872e7129a5be9d50fd36a1bfc8441d9 [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 */
18#include <common.h>
Jagan Tekice8a6972018-12-31 17:05:40 +053019#include <clk.h>
Simon Glass11c89f32017-05-17 17:18:03 -060020#include <dm.h>
Jagan Teki137fc752018-05-07 13:03:38 +053021#include <generic-phy.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>
Hans de Goedeeaa0d702015-02-16 22:13:43 +010027#include <asm/arch/gpio.h>
Hans de Goedeeaa0d702015-02-16 22:13:43 +010028#include <asm-generic/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070029#include <dm/device_compat.h>
Hans de Goede0b3845a2015-06-17 17:44:58 +020030#include <dm/lists.h>
31#include <dm/root.h>
Hans de Goedeea059bf2015-06-17 15:49:26 +020032#include <linux/usb/musb.h>
Hans de Goede9c4f11d2015-01-11 20:34:48 +010033#include "linux-compat.h"
34#include "musb_core.h"
Hans de Goede0b3845a2015-06-17 17:44:58 +020035#include "musb_uboot.h"
Hans de Goede9c4f11d2015-01-11 20:34:48 +010036
37/******************************************************************************
38 ******************************************************************************
39 * From the Allwinner driver
40 ******************************************************************************
41 ******************************************************************************/
42
43/******************************************************************************
44 * From include/sunxi_usb_bsp.h
45 ******************************************************************************/
46
47/* reg offsets */
48#define USBC_REG_o_ISCR 0x0400
49#define USBC_REG_o_PHYCTL 0x0404
50#define USBC_REG_o_PHYBIST 0x0408
51#define USBC_REG_o_PHYTUNE 0x040c
52
53#define USBC_REG_o_VEND0 0x0043
54
55/* Interface Status and Control */
56#define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
57#define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
58#define USBC_BP_ISCR_EXT_ID_STATUS 28
59#define USBC_BP_ISCR_EXT_DM_STATUS 27
60#define USBC_BP_ISCR_EXT_DP_STATUS 26
61#define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
62#define USBC_BP_ISCR_MERGED_ID_STATUS 24
63
64#define USBC_BP_ISCR_ID_PULLUP_EN 17
65#define USBC_BP_ISCR_DPDM_PULLUP_EN 16
66#define USBC_BP_ISCR_FORCE_ID 14
67#define USBC_BP_ISCR_FORCE_VBUS_VALID 12
68#define USBC_BP_ISCR_VBUS_VALID_SRC 10
69
70#define USBC_BP_ISCR_HOSC_EN 7
71#define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
72#define USBC_BP_ISCR_ID_CHANGE_DETECT 5
73#define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
74#define USBC_BP_ISCR_IRQ_ENABLE 3
75#define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
76#define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
77#define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
78
79/******************************************************************************
80 * From usbc/usbc.c
81 ******************************************************************************/
82
Jagan Teki2e8e67c2018-07-20 12:43:59 +053083#define OFF_SUN6I_AHB_RESET0 0x2c0
84
Jagan Teki65ce4962018-05-07 13:03:20 +053085struct sunxi_musb_config {
86 struct musb_hdrc_config *config;
87};
88
Jagan Tekifda15692018-05-07 13:03:17 +053089struct sunxi_glue {
90 struct musb_host_data mdata;
Jagan Tekice8a6972018-12-31 17:05:40 +053091 struct clk clk;
92 struct reset_ctl rst;
Jagan Teki65ce4962018-05-07 13:03:20 +053093 struct sunxi_musb_config *cfg;
Jagan Tekifda15692018-05-07 13:03:17 +053094 struct device dev;
Jagan Tekid0edfee2018-07-20 12:43:57 +053095 struct phy phy;
Jagan Tekifda15692018-05-07 13:03:17 +053096};
97#define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
98
Hans de Goede9c4f11d2015-01-11 20:34:48 +010099static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
100{
101 u32 temp = reg_val;
102
Jagan Tekicecffaa2018-05-07 13:03:23 +0530103 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 Goede9c4f11d2015-01-11 20:34:48 +0100106
107 return temp;
108}
109
110static void USBC_EnableIdPullUp(__iomem void *base)
111{
112 u32 reg_val;
113
114 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530115 reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100116 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
117 musb_writel(base, USBC_REG_o_ISCR, reg_val);
118}
119
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100120static void USBC_EnableDpDmPullUp(__iomem void *base)
121{
122 u32 reg_val;
123
124 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530125 reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100126 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
127 musb_writel(base, USBC_REG_o_ISCR, reg_val);
128}
129
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100130static 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
141static 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 Goede836e3342015-06-14 11:55:28 +0200152static 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 Goede9c4f11d2015-01-11 20:34:48 +0100163static 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
174static 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 Tekicecffaa2018-05-07 13:03:23 +0530181 reg_value |= BIT(0);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100182 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
183}
184
185/******************************************************************************
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200186 * Needed for the DFU polling magic
187 ******************************************************************************/
188
189static u8 last_int_usb;
190
191bool dfu_usb_get_reset(void)
192{
193 return !!(last_int_usb & MUSB_INTR_RESET);
194}
195
196/******************************************************************************
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100197 * MUSB Glue code
198 ******************************************************************************/
199
200static 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 Siamashkabce07f62015-10-25 06:44:47 +0200207 last_int_usb = musb->int_usb;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100208 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 Goede836e3342015-06-14 11:55:28 +0200223/* musb_core does not call enable / disable in a balanced manner <sigh> */
224static bool enabled = false;
225
Hans de Goede81c49982015-06-17 21:33:54 +0200226static int sunxi_musb_enable(struct musb *musb)
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100227{
Jagan Teki137fc752018-05-07 13:03:38 +0530228 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800229 int ret;
230
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100231 pr_debug("%s():\n", __func__);
232
Maxime Ripard9634fa52015-08-04 17:04:10 +0200233 musb_ep_select(musb->mregs, 0);
234 musb_writeb(musb->mregs, MUSB_FADDR, 0);
235
Hans de Goede836e3342015-06-14 11:55:28 +0200236 if (enabled)
Hans de Goede81c49982015-06-17 21:33:54 +0200237 return 0;
Hans de Goede836e3342015-06-14 11:55:28 +0200238
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100239 /* select PIO mode */
240 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
241
Hans de Goedec93ecd02015-06-14 16:48:56 +0200242 if (is_host_enabled(musb)) {
Jagan Tekid0edfee2018-07-20 12:43:57 +0530243 ret = sun4i_usb_phy_vbus_detect(&glue->phy);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800244 if (ret == 1) {
245 printf("A charger is plugged into the OTG: ");
246 return -ENODEV;
Hans de Goedec93ecd02015-06-14 16:48:56 +0200247 }
Jagan Teki137fc752018-05-07 13:03:38 +0530248
Jagan Tekid0edfee2018-07-20 12:43:57 +0530249 ret = sun4i_usb_phy_id_detect(&glue->phy);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800250 if (ret == 1) {
Hans de Goedec77fbfb2015-06-14 17:40:37 +0200251 printf("No host cable detected: ");
252 return -ENODEV;
253 }
Jagan Teki137fc752018-05-07 13:03:38 +0530254
Jagan Tekid0edfee2018-07-20 12:43:57 +0530255 ret = generic_phy_power_on(&glue->phy);
Jagan Teki137fc752018-05-07 13:03:38 +0530256 if (ret) {
257 pr_err("failed to power on USB PHY\n");
258 return ret;
259 }
Hans de Goedec93ecd02015-06-14 16:48:56 +0200260 }
Hans de Goede836e3342015-06-14 11:55:28 +0200261
262 USBC_ForceVbusValidToHigh(musb->mregs);
263
264 enabled = true;
Hans de Goede81c49982015-06-17 21:33:54 +0200265 return 0;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100266}
267
268static void sunxi_musb_disable(struct musb *musb)
269{
Jagan Teki137fc752018-05-07 13:03:38 +0530270 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
271 int ret;
272
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100273 pr_debug("%s():\n", __func__);
274
Hans de Goede836e3342015-06-14 11:55:28 +0200275 if (!enabled)
276 return;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200277
Jagan Teki137fc752018-05-07 13:03:38 +0530278 if (is_host_enabled(musb)) {
Jagan Tekid0edfee2018-07-20 12:43:57 +0530279 ret = generic_phy_power_off(&glue->phy);
Jagan Teki137fc752018-05-07 13:03:38 +0530280 if (ret) {
281 pr_err("failed to power off USB PHY\n");
282 return;
283 }
284 }
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800285
Hans de Goede836e3342015-06-14 11:55:28 +0200286 USBC_ForceVbusValidToLow(musb->mregs);
287 mdelay(200); /* Wait for the current session to timeout */
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200288
Hans de Goede836e3342015-06-14 11:55:28 +0200289 enabled = false;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100290}
291
292static int sunxi_musb_init(struct musb *musb)
293{
Jagan Tekifda15692018-05-07 13:03:17 +0530294 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Jagan Teki137fc752018-05-07 13:03:38 +0530295 int ret;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100296
297 pr_debug("%s():\n", __func__);
298
Jagan Tekice8a6972018-12-31 17:05:40 +0530299 ret = clk_enable(&glue->clk);
Jagan Teki137fc752018-05-07 13:03:38 +0530300 if (ret) {
Jagan Tekice8a6972018-12-31 17:05:40 +0530301 dev_err(dev, "failed to enable clock\n");
Jagan Teki137fc752018-05-07 13:03:38 +0530302 return ret;
303 }
304
Jagan Tekice8a6972018-12-31 17:05:40 +0530305 if (reset_valid(&glue->rst)) {
306 ret = reset_deassert(&glue->rst);
307 if (ret) {
308 dev_err(dev, "failed to deassert reset\n");
309 goto err_clk;
310 }
311 }
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530312
Jagan Tekice8a6972018-12-31 17:05:40 +0530313 ret = generic_phy_init(&glue->phy);
314 if (ret) {
315 dev_err(dev, "failed to init USB PHY\n");
316 goto err_rst;
317 }
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530318
Jagan Tekice8a6972018-12-31 17:05:40 +0530319 musb->isr = sunxi_musb_interrupt;
Jagan Teki767a58e2018-05-07 13:03:22 +0530320
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100321 USBC_ConfigFIFO_Base();
322 USBC_EnableDpDmPullUp(musb->mregs);
323 USBC_EnableIdPullUp(musb->mregs);
324
325 if (is_host_enabled(musb)) {
326 /* Host mode */
327 USBC_ForceIdToLow(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100328 } else {
329 /* Peripheral mode */
330 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100331 }
Hans de Goedef2b45c82015-02-11 09:05:18 +0100332 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100333
334 return 0;
Jagan Tekice8a6972018-12-31 17:05:40 +0530335
336err_rst:
337 if (reset_valid(&glue->rst))
338 reset_assert(&glue->rst);
339err_clk:
340 clk_disable(&glue->clk);
341 return ret;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100342}
343
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530344static int sunxi_musb_exit(struct musb *musb)
345{
346 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
347 int ret = 0;
348
349 if (generic_phy_valid(&glue->phy)) {
350 ret = generic_phy_exit(&glue->phy);
351 if (ret) {
352 dev_err(dev, "failed to power off usb phy\n");
353 return ret;
354 }
355 }
356
Jagan Tekice8a6972018-12-31 17:05:40 +0530357 if (reset_valid(&glue->rst))
358 reset_assert(&glue->rst);
359 clk_disable(&glue->clk);
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530360
361 return 0;
362}
363
Jagan Teki37671e12018-05-07 13:03:37 +0530364static void sunxi_musb_pre_root_reset_end(struct musb *musb)
365{
366 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
367
Jagan Tekid0edfee2018-07-20 12:43:57 +0530368 sun4i_usb_phy_set_squelch_detect(&glue->phy, false);
Jagan Teki37671e12018-05-07 13:03:37 +0530369}
370
371static void sunxi_musb_post_root_reset_end(struct musb *musb)
372{
373 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
374
Jagan Tekid0edfee2018-07-20 12:43:57 +0530375 sun4i_usb_phy_set_squelch_detect(&glue->phy, true);
Jagan Teki37671e12018-05-07 13:03:37 +0530376}
377
Hans de Goedeea059bf2015-06-17 15:49:26 +0200378static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100379 .init = sunxi_musb_init,
Jagan Teki1d0d63e2018-07-20 12:44:00 +0530380 .exit = sunxi_musb_exit,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100381 .enable = sunxi_musb_enable,
382 .disable = sunxi_musb_disable,
Jagan Teki37671e12018-05-07 13:03:37 +0530383 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
384 .post_root_reset_end = sunxi_musb_post_root_reset_end,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100385};
Hans de Goedeea059bf2015-06-17 15:49:26 +0200386
Jagan Teki4e5af302018-05-07 13:03:18 +0530387/* Allwinner OTG supports up to 5 endpoints */
388#define SUNXI_MUSB_MAX_EP_NUM 6
389#define SUNXI_MUSB_RAM_BITS 11
390
Jagan Teki65ce4962018-05-07 13:03:20 +0530391static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
392 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
393 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
394 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
395 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
396 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
397 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
398 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
399 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
400 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
401 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
402};
403
404/* H3/V3s OTG supports only 4 endpoints */
405#define SUNXI_MUSB_MAX_EP_NUM_H3 5
406
407static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
408 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
409 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
410 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
411 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
412 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
413 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
414 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
415 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
416};
417
Hans de Goedeea059bf2015-06-17 15:49:26 +0200418static struct musb_hdrc_config musb_config = {
Jagan Teki65ce4962018-05-07 13:03:20 +0530419 .fifo_cfg = sunxi_musb_mode_cfg,
420 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
Jagan Teki4e5af302018-05-07 13:03:18 +0530421 .multipoint = true,
422 .dyn_fifo = true,
423 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
424 .ram_bits = SUNXI_MUSB_RAM_BITS,
Hans de Goedeea059bf2015-06-17 15:49:26 +0200425};
426
Jagan Teki65ce4962018-05-07 13:03:20 +0530427static struct musb_hdrc_config musb_config_h3 = {
428 .fifo_cfg = sunxi_musb_mode_cfg_h3,
429 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
430 .multipoint = true,
431 .dyn_fifo = true,
432 .soft_con = true,
433 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
434 .ram_bits = SUNXI_MUSB_RAM_BITS,
435};
436
Hans de Goede4a67d732016-09-17 16:02:38 +0200437static int musb_usb_probe(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200438{
Jagan Tekifda15692018-05-07 13:03:17 +0530439 struct sunxi_glue *glue = dev_get_priv(dev);
440 struct musb_host_data *host = &glue->mdata;
Jagan Teki69bb76e2018-05-07 13:03:19 +0530441 struct musb_hdrc_platform_data pdata;
Chen-Yu Tsai63107552017-12-30 20:44:07 +0800442 void *base = dev_read_addr_ptr(dev);
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200443 int ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200444
Stefan Mavrodiev4beaec12018-12-05 14:49:44 +0200445#ifdef CONFIG_USB_MUSB_HOST
446 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
447#endif
448
Chen-Yu Tsai63107552017-12-30 20:44:07 +0800449 if (!base)
450 return -EINVAL;
451
Jagan Teki65ce4962018-05-07 13:03:20 +0530452 glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
453 if (!glue->cfg)
454 return -EINVAL;
455
Jagan Tekice8a6972018-12-31 17:05:40 +0530456 ret = clk_get_by_index(dev, 0, &glue->clk);
457 if (ret) {
458 dev_err(dev, "failed to get clock\n");
459 return ret;
460 }
Jagan Tekifda15692018-05-07 13:03:17 +0530461
Jagan Tekice8a6972018-12-31 17:05:40 +0530462 ret = reset_get_by_index(dev, 0, &glue->rst);
463 if (ret && ret != -ENOENT) {
464 dev_err(dev, "failed to get reset\n");
465 return ret;
466 }
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530467
Jagan Tekid0edfee2018-07-20 12:43:57 +0530468 ret = generic_phy_get_by_name(dev, "usb", &glue->phy);
Jagan Teki137fc752018-05-07 13:03:38 +0530469 if (ret) {
470 pr_err("failed to get usb PHY\n");
471 return ret;
472 }
473
Jagan Teki69bb76e2018-05-07 13:03:19 +0530474 memset(&pdata, 0, sizeof(pdata));
475 pdata.power = 250;
476 pdata.platform_ops = &sunxi_musb_ops;
Jagan Teki65ce4962018-05-07 13:03:20 +0530477 pdata.config = glue->cfg->config;
Jagan Teki69bb76e2018-05-07 13:03:19 +0530478
Maxime Ripard847bcf02017-09-05 22:10:35 +0200479#ifdef CONFIG_USB_MUSB_HOST
Stefan Mavrodiev4beaec12018-12-05 14:49:44 +0200480 priv->desc_before_addr = true;
481
Jagan Teki69bb76e2018-05-07 13:03:19 +0530482 pdata.mode = MUSB_HOST;
483 host->host = musb_init_controller(&pdata, &glue->dev, base);
Hans de Goedeec89e162016-04-02 20:46:09 +0200484 if (!host->host)
485 return -EIO;
486
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200487 ret = musb_lowlevel_init(host);
Maxime Ripard847bcf02017-09-05 22:10:35 +0200488 if (!ret)
489 printf("Allwinner mUSB OTG (Host)\n");
490#else
Jagan Teki69bb76e2018-05-07 13:03:19 +0530491 pdata.mode = MUSB_PERIPHERAL;
Jagan Tekif0a6d792018-07-20 12:43:56 +0530492 host->host = musb_register(&pdata, &glue->dev, base);
493 if (!host->host)
494 return -EIO;
495
496 printf("Allwinner mUSB OTG (Peripheral)\n");
Maxime Ripard847bcf02017-09-05 22:10:35 +0200497#endif
Hans de Goede0b3845a2015-06-17 17:44:58 +0200498
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200499 return ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200500}
501
Hans de Goede4a67d732016-09-17 16:02:38 +0200502static int musb_usb_remove(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200503{
Jagan Tekifda15692018-05-07 13:03:17 +0530504 struct sunxi_glue *glue = dev_get_priv(dev);
505 struct musb_host_data *host = &glue->mdata;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200506
507 musb_stop(host->host);
Hans de Goede4a67d732016-09-17 16:02:38 +0200508 free(host->host);
509 host->host = NULL;
510
Hans de Goede0b3845a2015-06-17 17:44:58 +0200511 return 0;
512}
513
Jagan Teki65ce4962018-05-07 13:03:20 +0530514static const struct sunxi_musb_config sun4i_a10_cfg = {
515 .config = &musb_config,
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530516};
517
518static const struct sunxi_musb_config sun6i_a31_cfg = {
519 .config = &musb_config,
Jagan Teki65ce4962018-05-07 13:03:20 +0530520};
521
522static const struct sunxi_musb_config sun8i_h3_cfg = {
523 .config = &musb_config_h3,
524};
525
Maxime Ripard847bcf02017-09-05 22:10:35 +0200526static const struct udevice_id sunxi_musb_ids[] = {
Jagan Teki65ce4962018-05-07 13:03:20 +0530527 { .compatible = "allwinner,sun4i-a10-musb",
528 .data = (ulong)&sun4i_a10_cfg },
529 { .compatible = "allwinner,sun6i-a31-musb",
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530530 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki65ce4962018-05-07 13:03:20 +0530531 { .compatible = "allwinner,sun8i-a33-musb",
Jagan Teki2e8e67c2018-07-20 12:43:59 +0530532 .data = (ulong)&sun6i_a31_cfg },
Jagan Teki65ce4962018-05-07 13:03:20 +0530533 { .compatible = "allwinner,sun8i-h3-musb",
534 .data = (ulong)&sun8i_h3_cfg },
Maxime Ripard847bcf02017-09-05 22:10:35 +0200535 { }
Hans de Goede0b3845a2015-06-17 17:44:58 +0200536};
Hans de Goede0b3845a2015-06-17 17:44:58 +0200537
Maxime Ripard847bcf02017-09-05 22:10:35 +0200538U_BOOT_DRIVER(usb_musb) = {
539 .name = "sunxi-musb",
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200540#ifdef CONFIG_USB_MUSB_HOST
Maxime Ripard847bcf02017-09-05 22:10:35 +0200541 .id = UCLASS_USB,
Hans de Goede0b3845a2015-06-17 17:44:58 +0200542#else
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100543 .id = UCLASS_USB_GADGET_GENERIC,
Hans de Goede0b3845a2015-06-17 17:44:58 +0200544#endif
Maxime Ripard847bcf02017-09-05 22:10:35 +0200545 .of_match = sunxi_musb_ids,
546 .probe = musb_usb_probe,
547 .remove = musb_usb_remove,
548#ifdef CONFIG_USB_MUSB_HOST
549 .ops = &musb_usb_ops,
550#endif
551 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
Jagan Tekifda15692018-05-07 13:03:17 +0530552 .priv_auto_alloc_size = sizeof(struct sunxi_glue),
Maxime Ripard847bcf02017-09-05 22:10:35 +0200553};