blob: 08de9c69c71c2462cb6b815d891e51c06395d18d [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>
Simon Glass11c89f32017-05-17 17:18:03 -060019#include <dm.h>
Jagan Teki137fc752018-05-07 13:03:38 +053020#include <generic-phy.h>
21#include <phy-sun4i-usb.h>
Hans de Goede9c4f11d2015-01-11 20:34:48 +010022#include <asm/arch/cpu.h>
Hans de Goede7e5aabd2015-04-27 11:44:22 +020023#include <asm/arch/clock.h>
Hans de Goedeeaa0d702015-02-16 22:13:43 +010024#include <asm/arch/gpio.h>
Hans de Goedeeaa0d702015-02-16 22:13:43 +010025#include <asm-generic/gpio.h>
Hans de Goede0b3845a2015-06-17 17:44:58 +020026#include <dm/lists.h>
27#include <dm/root.h>
Hans de Goedeea059bf2015-06-17 15:49:26 +020028#include <linux/usb/musb.h>
Hans de Goede9c4f11d2015-01-11 20:34:48 +010029#include "linux-compat.h"
30#include "musb_core.h"
Hans de Goede0b3845a2015-06-17 17:44:58 +020031#include "musb_uboot.h"
Hans de Goede9c4f11d2015-01-11 20:34:48 +010032
33/******************************************************************************
34 ******************************************************************************
35 * From the Allwinner driver
36 ******************************************************************************
37 ******************************************************************************/
38
39/******************************************************************************
40 * From include/sunxi_usb_bsp.h
41 ******************************************************************************/
42
43/* reg offsets */
44#define USBC_REG_o_ISCR 0x0400
45#define USBC_REG_o_PHYCTL 0x0404
46#define USBC_REG_o_PHYBIST 0x0408
47#define USBC_REG_o_PHYTUNE 0x040c
48
49#define USBC_REG_o_VEND0 0x0043
50
51/* Interface Status and Control */
52#define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
53#define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
54#define USBC_BP_ISCR_EXT_ID_STATUS 28
55#define USBC_BP_ISCR_EXT_DM_STATUS 27
56#define USBC_BP_ISCR_EXT_DP_STATUS 26
57#define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
58#define USBC_BP_ISCR_MERGED_ID_STATUS 24
59
60#define USBC_BP_ISCR_ID_PULLUP_EN 17
61#define USBC_BP_ISCR_DPDM_PULLUP_EN 16
62#define USBC_BP_ISCR_FORCE_ID 14
63#define USBC_BP_ISCR_FORCE_VBUS_VALID 12
64#define USBC_BP_ISCR_VBUS_VALID_SRC 10
65
66#define USBC_BP_ISCR_HOSC_EN 7
67#define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
68#define USBC_BP_ISCR_ID_CHANGE_DETECT 5
69#define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
70#define USBC_BP_ISCR_IRQ_ENABLE 3
71#define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
72#define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
73#define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
74
75/******************************************************************************
76 * From usbc/usbc.c
77 ******************************************************************************/
78
Jagan Teki65ce4962018-05-07 13:03:20 +053079struct sunxi_musb_config {
80 struct musb_hdrc_config *config;
Jagan Teki767a58e2018-05-07 13:03:22 +053081 u8 rst_bit;
82 u8 clkgate_bit;
Jagan Teki65ce4962018-05-07 13:03:20 +053083};
84
Jagan Tekifda15692018-05-07 13:03:17 +053085struct sunxi_glue {
86 struct musb_host_data mdata;
87 struct sunxi_ccm_reg *ccm;
Jagan Teki65ce4962018-05-07 13:03:20 +053088 struct sunxi_musb_config *cfg;
Jagan Tekifda15692018-05-07 13:03:17 +053089 struct device dev;
Jagan Teki137fc752018-05-07 13:03:38 +053090 struct phy *phy;
Jagan Tekifda15692018-05-07 13:03:17 +053091};
92#define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
93
Hans de Goede9c4f11d2015-01-11 20:34:48 +010094static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
95{
96 u32 temp = reg_val;
97
Jagan Tekicecffaa2018-05-07 13:03:23 +053098 temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
99 temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
100 temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100101
102 return temp;
103}
104
105static void USBC_EnableIdPullUp(__iomem void *base)
106{
107 u32 reg_val;
108
109 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530110 reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100111 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
112 musb_writel(base, USBC_REG_o_ISCR, reg_val);
113}
114
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100115static void USBC_EnableDpDmPullUp(__iomem void *base)
116{
117 u32 reg_val;
118
119 reg_val = musb_readl(base, USBC_REG_o_ISCR);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530120 reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100121 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
122 musb_writel(base, USBC_REG_o_ISCR, reg_val);
123}
124
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100125static void USBC_ForceIdToLow(__iomem void *base)
126{
127 u32 reg_val;
128
129 reg_val = musb_readl(base, USBC_REG_o_ISCR);
130 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
131 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
132 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
133 musb_writel(base, USBC_REG_o_ISCR, reg_val);
134}
135
136static void USBC_ForceIdToHigh(__iomem void *base)
137{
138 u32 reg_val;
139
140 reg_val = musb_readl(base, USBC_REG_o_ISCR);
141 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
142 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
143 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
144 musb_writel(base, USBC_REG_o_ISCR, reg_val);
145}
146
Hans de Goede836e3342015-06-14 11:55:28 +0200147static void USBC_ForceVbusValidToLow(__iomem void *base)
148{
149 u32 reg_val;
150
151 reg_val = musb_readl(base, USBC_REG_o_ISCR);
152 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
153 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
154 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
155 musb_writel(base, USBC_REG_o_ISCR, reg_val);
156}
157
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100158static void USBC_ForceVbusValidToHigh(__iomem void *base)
159{
160 u32 reg_val;
161
162 reg_val = musb_readl(base, USBC_REG_o_ISCR);
163 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
164 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
165 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
166 musb_writel(base, USBC_REG_o_ISCR, reg_val);
167}
168
169static void USBC_ConfigFIFO_Base(void)
170{
171 u32 reg_value;
172
173 /* config usb fifo, 8kb mode */
174 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
175 reg_value &= ~(0x03 << 0);
Jagan Tekicecffaa2018-05-07 13:03:23 +0530176 reg_value |= BIT(0);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100177 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
178}
179
180/******************************************************************************
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200181 * Needed for the DFU polling magic
182 ******************************************************************************/
183
184static u8 last_int_usb;
185
186bool dfu_usb_get_reset(void)
187{
188 return !!(last_int_usb & MUSB_INTR_RESET);
189}
190
191/******************************************************************************
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100192 * MUSB Glue code
193 ******************************************************************************/
194
195static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
196{
197 struct musb *musb = __hci;
198 irqreturn_t retval = IRQ_NONE;
199
200 /* read and flush interrupts */
201 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200202 last_int_usb = musb->int_usb;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100203 if (musb->int_usb)
204 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
205 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
206 if (musb->int_tx)
207 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
208 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
209 if (musb->int_rx)
210 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
211
212 if (musb->int_usb || musb->int_tx || musb->int_rx)
213 retval |= musb_interrupt(musb);
214
215 return retval;
216}
217
Hans de Goede836e3342015-06-14 11:55:28 +0200218/* musb_core does not call enable / disable in a balanced manner <sigh> */
219static bool enabled = false;
220
Hans de Goede81c49982015-06-17 21:33:54 +0200221static int sunxi_musb_enable(struct musb *musb)
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100222{
Jagan Teki137fc752018-05-07 13:03:38 +0530223 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800224 int ret;
225
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100226 pr_debug("%s():\n", __func__);
227
Maxime Ripard9634fa52015-08-04 17:04:10 +0200228 musb_ep_select(musb->mregs, 0);
229 musb_writeb(musb->mregs, MUSB_FADDR, 0);
230
Hans de Goede836e3342015-06-14 11:55:28 +0200231 if (enabled)
Hans de Goede81c49982015-06-17 21:33:54 +0200232 return 0;
Hans de Goede836e3342015-06-14 11:55:28 +0200233
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100234 /* select PIO mode */
235 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
236
Hans de Goedec93ecd02015-06-14 16:48:56 +0200237 if (is_host_enabled(musb)) {
Jagan Teki137fc752018-05-07 13:03:38 +0530238 ret = sun4i_usb_phy_vbus_detect(glue->phy);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800239 if (ret == 1) {
240 printf("A charger is plugged into the OTG: ");
241 return -ENODEV;
Hans de Goedec93ecd02015-06-14 16:48:56 +0200242 }
Jagan Teki137fc752018-05-07 13:03:38 +0530243
244 ret = sun4i_usb_phy_id_detect(glue->phy);
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800245 if (ret == 1) {
Hans de Goedec77fbfb2015-06-14 17:40:37 +0200246 printf("No host cable detected: ");
247 return -ENODEV;
248 }
Jagan Teki137fc752018-05-07 13:03:38 +0530249
250 ret = generic_phy_power_on(glue->phy);
251 if (ret) {
252 pr_err("failed to power on USB PHY\n");
253 return ret;
254 }
Hans de Goedec93ecd02015-06-14 16:48:56 +0200255 }
Hans de Goede836e3342015-06-14 11:55:28 +0200256
257 USBC_ForceVbusValidToHigh(musb->mregs);
258
259 enabled = true;
Hans de Goede81c49982015-06-17 21:33:54 +0200260 return 0;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100261}
262
263static void sunxi_musb_disable(struct musb *musb)
264{
Jagan Teki137fc752018-05-07 13:03:38 +0530265 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
266 int ret;
267
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100268 pr_debug("%s():\n", __func__);
269
Hans de Goede836e3342015-06-14 11:55:28 +0200270 if (!enabled)
271 return;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200272
Jagan Teki137fc752018-05-07 13:03:38 +0530273 if (is_host_enabled(musb)) {
274 ret = generic_phy_power_off(glue->phy);
275 if (ret) {
276 pr_err("failed to power off USB PHY\n");
277 return;
278 }
279 }
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800280
Hans de Goede836e3342015-06-14 11:55:28 +0200281 USBC_ForceVbusValidToLow(musb->mregs);
282 mdelay(200); /* Wait for the current session to timeout */
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200283
Hans de Goede836e3342015-06-14 11:55:28 +0200284 enabled = false;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100285}
286
287static int sunxi_musb_init(struct musb *musb)
288{
Jagan Tekifda15692018-05-07 13:03:17 +0530289 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Jagan Teki137fc752018-05-07 13:03:38 +0530290 int ret;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100291
292 pr_debug("%s():\n", __func__);
293
Jagan Teki137fc752018-05-07 13:03:38 +0530294 ret = generic_phy_init(glue->phy);
295 if (ret) {
296 pr_err("failed to init USB PHY\n");
297 return ret;
298 }
299
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100300 musb->isr = sunxi_musb_interrupt;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200301
Jagan Tekicecffaa2018-05-07 13:03:23 +0530302 setbits_le32(&glue->ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_USB0));
Jagan Teki767a58e2018-05-07 13:03:22 +0530303 if (glue->cfg->clkgate_bit)
304 setbits_le32(&glue->ccm->ahb_gate0,
Jagan Tekicecffaa2018-05-07 13:03:23 +0530305 BIT(glue->cfg->clkgate_bit));
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200306#ifdef CONFIG_SUNXI_GEN_SUN6I
Jagan Tekicecffaa2018-05-07 13:03:23 +0530307 setbits_le32(&glue->ccm->ahb_reset0_cfg, BIT(AHB_GATE_OFFSET_USB0));
Jagan Teki767a58e2018-05-07 13:03:22 +0530308 if (glue->cfg->rst_bit)
309 setbits_le32(&glue->ccm->ahb_reset0_cfg,
Jagan Tekicecffaa2018-05-07 13:03:23 +0530310 BIT(glue->cfg->rst_bit));
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200311#endif
Jagan Teki767a58e2018-05-07 13:03:22 +0530312
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100313 USBC_ConfigFIFO_Base();
314 USBC_EnableDpDmPullUp(musb->mregs);
315 USBC_EnableIdPullUp(musb->mregs);
316
317 if (is_host_enabled(musb)) {
318 /* Host mode */
319 USBC_ForceIdToLow(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100320 } else {
321 /* Peripheral mode */
322 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100323 }
Hans de Goedef2b45c82015-02-11 09:05:18 +0100324 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100325
326 return 0;
327}
328
Jagan Teki37671e12018-05-07 13:03:37 +0530329static void sunxi_musb_pre_root_reset_end(struct musb *musb)
330{
331 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
332
333 sun4i_usb_phy_set_squelch_detect(glue->phy, false);
334}
335
336static void sunxi_musb_post_root_reset_end(struct musb *musb)
337{
338 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
339
340 sun4i_usb_phy_set_squelch_detect(glue->phy, true);
341}
342
Hans de Goedeea059bf2015-06-17 15:49:26 +0200343static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100344 .init = sunxi_musb_init,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100345 .enable = sunxi_musb_enable,
346 .disable = sunxi_musb_disable,
Jagan Teki37671e12018-05-07 13:03:37 +0530347 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
348 .post_root_reset_end = sunxi_musb_post_root_reset_end,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100349};
Hans de Goedeea059bf2015-06-17 15:49:26 +0200350
Jagan Teki4e5af302018-05-07 13:03:18 +0530351/* Allwinner OTG supports up to 5 endpoints */
352#define SUNXI_MUSB_MAX_EP_NUM 6
353#define SUNXI_MUSB_RAM_BITS 11
354
Jagan Teki65ce4962018-05-07 13:03:20 +0530355static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
356 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
357 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
358 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
359 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
360 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
361 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
362 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
363 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
364 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
365 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
366};
367
368/* H3/V3s OTG supports only 4 endpoints */
369#define SUNXI_MUSB_MAX_EP_NUM_H3 5
370
371static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
372 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
373 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
374 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
375 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
376 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
377 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
378 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
379 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
380};
381
Hans de Goedeea059bf2015-06-17 15:49:26 +0200382static struct musb_hdrc_config musb_config = {
Jagan Teki65ce4962018-05-07 13:03:20 +0530383 .fifo_cfg = sunxi_musb_mode_cfg,
384 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
Jagan Teki4e5af302018-05-07 13:03:18 +0530385 .multipoint = true,
386 .dyn_fifo = true,
387 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
388 .ram_bits = SUNXI_MUSB_RAM_BITS,
Hans de Goedeea059bf2015-06-17 15:49:26 +0200389};
390
Jagan Teki65ce4962018-05-07 13:03:20 +0530391static struct musb_hdrc_config musb_config_h3 = {
392 .fifo_cfg = sunxi_musb_mode_cfg_h3,
393 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
394 .multipoint = true,
395 .dyn_fifo = true,
396 .soft_con = true,
397 .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
398 .ram_bits = SUNXI_MUSB_RAM_BITS,
399};
400
Hans de Goede4a67d732016-09-17 16:02:38 +0200401static int musb_usb_probe(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200402{
Jagan Tekifda15692018-05-07 13:03:17 +0530403 struct sunxi_glue *glue = dev_get_priv(dev);
404 struct musb_host_data *host = &glue->mdata;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200405 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
Jagan Teki69bb76e2018-05-07 13:03:19 +0530406 struct musb_hdrc_platform_data pdata;
Chen-Yu Tsai63107552017-12-30 20:44:07 +0800407 void *base = dev_read_addr_ptr(dev);
Jagan Teki137fc752018-05-07 13:03:38 +0530408 struct phy phy;
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200409 int ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200410
Chen-Yu Tsai63107552017-12-30 20:44:07 +0800411 if (!base)
412 return -EINVAL;
413
Jagan Teki65ce4962018-05-07 13:03:20 +0530414 glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
415 if (!glue->cfg)
416 return -EINVAL;
417
Jagan Tekifda15692018-05-07 13:03:17 +0530418 glue->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
419 if (IS_ERR(glue->ccm))
420 return PTR_ERR(glue->ccm);
421
Jagan Teki137fc752018-05-07 13:03:38 +0530422 ret = generic_phy_get_by_name(dev, "usb", &phy);
423 if (ret) {
424 pr_err("failed to get usb PHY\n");
425 return ret;
426 }
427
428 glue->phy = &phy;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200429 priv->desc_before_addr = true;
430
Jagan Teki69bb76e2018-05-07 13:03:19 +0530431 memset(&pdata, 0, sizeof(pdata));
432 pdata.power = 250;
433 pdata.platform_ops = &sunxi_musb_ops;
Jagan Teki65ce4962018-05-07 13:03:20 +0530434 pdata.config = glue->cfg->config;
Jagan Teki69bb76e2018-05-07 13:03:19 +0530435
Maxime Ripard847bcf02017-09-05 22:10:35 +0200436#ifdef CONFIG_USB_MUSB_HOST
Jagan Teki69bb76e2018-05-07 13:03:19 +0530437 pdata.mode = MUSB_HOST;
438 host->host = musb_init_controller(&pdata, &glue->dev, base);
Hans de Goedeec89e162016-04-02 20:46:09 +0200439 if (!host->host)
440 return -EIO;
441
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200442 ret = musb_lowlevel_init(host);
Maxime Ripard847bcf02017-09-05 22:10:35 +0200443 if (!ret)
444 printf("Allwinner mUSB OTG (Host)\n");
445#else
Jagan Teki69bb76e2018-05-07 13:03:19 +0530446 pdata.mode = MUSB_PERIPHERAL;
447 ret = musb_register(&pdata, &glue->dev, base);
Maxime Ripard847bcf02017-09-05 22:10:35 +0200448 if (!ret)
449 printf("Allwinner mUSB OTG (Peripheral)\n");
450#endif
Hans de Goede0b3845a2015-06-17 17:44:58 +0200451
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200452 return ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200453}
454
Hans de Goede4a67d732016-09-17 16:02:38 +0200455static int musb_usb_remove(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200456{
Jagan Tekifda15692018-05-07 13:03:17 +0530457 struct sunxi_glue *glue = dev_get_priv(dev);
458 struct musb_host_data *host = &glue->mdata;
Jagan Teki137fc752018-05-07 13:03:38 +0530459 int ret;
460
461 if (generic_phy_valid(glue->phy)) {
462 ret = generic_phy_exit(glue->phy);
463 if (ret) {
464 pr_err("failed to exit %s USB PHY\n", dev->name);
465 return ret;
466 }
467 }
Hans de Goede0b3845a2015-06-17 17:44:58 +0200468
469 musb_stop(host->host);
470
Hans de Goede779afb62016-06-05 16:53:04 +0200471#ifdef CONFIG_SUNXI_GEN_SUN6I
Jagan Tekicecffaa2018-05-07 13:03:23 +0530472 clrbits_le32(&glue->ccm->ahb_reset0_cfg, BIT(AHB_GATE_OFFSET_USB0));
Jagan Teki767a58e2018-05-07 13:03:22 +0530473 if (glue->cfg->rst_bit)
474 clrbits_le32(&glue->ccm->ahb_reset0_cfg,
Jagan Tekicecffaa2018-05-07 13:03:23 +0530475 BIT(glue->cfg->rst_bit));
Hans de Goede779afb62016-06-05 16:53:04 +0200476#endif
Jagan Tekicecffaa2018-05-07 13:03:23 +0530477 clrbits_le32(&glue->ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_USB0));
Jagan Teki767a58e2018-05-07 13:03:22 +0530478 if (glue->cfg->clkgate_bit)
479 clrbits_le32(&glue->ccm->ahb_gate0,
Jagan Tekicecffaa2018-05-07 13:03:23 +0530480 BIT(glue->cfg->clkgate_bit));
Hans de Goede779afb62016-06-05 16:53:04 +0200481
Hans de Goede4a67d732016-09-17 16:02:38 +0200482 free(host->host);
483 host->host = NULL;
484
Hans de Goede0b3845a2015-06-17 17:44:58 +0200485 return 0;
486}
487
Jagan Teki65ce4962018-05-07 13:03:20 +0530488static const struct sunxi_musb_config sun4i_a10_cfg = {
489 .config = &musb_config,
490};
491
492static const struct sunxi_musb_config sun8i_h3_cfg = {
493 .config = &musb_config_h3,
Jagan Teki767a58e2018-05-07 13:03:22 +0530494 .rst_bit = 23,
495 .clkgate_bit = 23,
Jagan Teki65ce4962018-05-07 13:03:20 +0530496};
497
Maxime Ripard847bcf02017-09-05 22:10:35 +0200498static const struct udevice_id sunxi_musb_ids[] = {
Jagan Teki65ce4962018-05-07 13:03:20 +0530499 { .compatible = "allwinner,sun4i-a10-musb",
500 .data = (ulong)&sun4i_a10_cfg },
501 { .compatible = "allwinner,sun6i-a31-musb",
502 .data = (ulong)&sun4i_a10_cfg },
503 { .compatible = "allwinner,sun8i-a33-musb",
504 .data = (ulong)&sun4i_a10_cfg },
505 { .compatible = "allwinner,sun8i-h3-musb",
506 .data = (ulong)&sun8i_h3_cfg },
Maxime Ripard847bcf02017-09-05 22:10:35 +0200507 { }
Hans de Goede0b3845a2015-06-17 17:44:58 +0200508};
Hans de Goede0b3845a2015-06-17 17:44:58 +0200509
Maxime Ripard847bcf02017-09-05 22:10:35 +0200510U_BOOT_DRIVER(usb_musb) = {
511 .name = "sunxi-musb",
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200512#ifdef CONFIG_USB_MUSB_HOST
Maxime Ripard847bcf02017-09-05 22:10:35 +0200513 .id = UCLASS_USB,
Hans de Goede0b3845a2015-06-17 17:44:58 +0200514#else
Maxime Ripard847bcf02017-09-05 22:10:35 +0200515 .id = UCLASS_USB_DEV_GENERIC,
Hans de Goede0b3845a2015-06-17 17:44:58 +0200516#endif
Maxime Ripard847bcf02017-09-05 22:10:35 +0200517 .of_match = sunxi_musb_ids,
518 .probe = musb_usb_probe,
519 .remove = musb_usb_remove,
520#ifdef CONFIG_USB_MUSB_HOST
521 .ops = &musb_usb_ops,
522#endif
523 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
Jagan Tekifda15692018-05-07 13:03:17 +0530524 .priv_auto_alloc_size = sizeof(struct sunxi_glue),
Maxime Ripard847bcf02017-09-05 22:10:35 +0200525};