blob: 469377fe4e6ddcdc98a277b2c6a81378732569c1 [file] [log] [blame]
Hans de Goede9c4f11d2015-01-11 20:34:48 +01001/*
2 * Allwinner SUNXI "glue layer"
3 *
4 * Copyright © 2015 Hans de Goede <hdegoede@redhat.com>
5 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
6 *
7 * Based on the sw_usb "Allwinner OTG Dual Role Controller" code.
8 * Copyright 2007-2012 (C) Allwinner Technology Co., Ltd.
9 * javen <javen@allwinnertech.com>
10 *
11 * Based on the DA8xx "glue layer" code.
12 * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
13 * Copyright (C) 2005-2006 by Texas Instruments
14 *
15 * This file is part of the Inventra Controller Driver for Linux.
16 *
Tom Rinie2378802016-01-14 22:05:13 -050017 * SPDX-License-Identifier: GPL-2.0
Hans de Goede9c4f11d2015-01-11 20:34:48 +010018 */
19#include <common.h>
20#include <asm/arch/cpu.h>
Hans de Goede7e5aabd2015-04-27 11:44:22 +020021#include <asm/arch/clock.h>
Hans de Goedeeaa0d702015-02-16 22:13:43 +010022#include <asm/arch/gpio.h>
Hans de Goede26a90052015-04-27 15:05:10 +020023#include <asm/arch/usb_phy.h>
Hans de Goedeeaa0d702015-02-16 22:13:43 +010024#include <asm-generic/gpio.h>
Hans de Goede0b3845a2015-06-17 17:44:58 +020025#include <dm/lists.h>
26#include <dm/root.h>
Hans de Goedeea059bf2015-06-17 15:49:26 +020027#include <linux/usb/musb.h>
Hans de Goede9c4f11d2015-01-11 20:34:48 +010028#include "linux-compat.h"
29#include "musb_core.h"
Hans de Goede0b3845a2015-06-17 17:44:58 +020030#include "musb_uboot.h"
Hans de Goede9c4f11d2015-01-11 20:34:48 +010031
32/******************************************************************************
33 ******************************************************************************
34 * From the Allwinner driver
35 ******************************************************************************
36 ******************************************************************************/
37
38/******************************************************************************
39 * From include/sunxi_usb_bsp.h
40 ******************************************************************************/
41
42/* reg offsets */
43#define USBC_REG_o_ISCR 0x0400
44#define USBC_REG_o_PHYCTL 0x0404
45#define USBC_REG_o_PHYBIST 0x0408
46#define USBC_REG_o_PHYTUNE 0x040c
47
48#define USBC_REG_o_VEND0 0x0043
49
50/* Interface Status and Control */
51#define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
52#define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
53#define USBC_BP_ISCR_EXT_ID_STATUS 28
54#define USBC_BP_ISCR_EXT_DM_STATUS 27
55#define USBC_BP_ISCR_EXT_DP_STATUS 26
56#define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
57#define USBC_BP_ISCR_MERGED_ID_STATUS 24
58
59#define USBC_BP_ISCR_ID_PULLUP_EN 17
60#define USBC_BP_ISCR_DPDM_PULLUP_EN 16
61#define USBC_BP_ISCR_FORCE_ID 14
62#define USBC_BP_ISCR_FORCE_VBUS_VALID 12
63#define USBC_BP_ISCR_VBUS_VALID_SRC 10
64
65#define USBC_BP_ISCR_HOSC_EN 7
66#define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
67#define USBC_BP_ISCR_ID_CHANGE_DETECT 5
68#define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
69#define USBC_BP_ISCR_IRQ_ENABLE 3
70#define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
71#define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
72#define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
73
74/******************************************************************************
75 * From usbc/usbc.c
76 ******************************************************************************/
77
78static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
79{
80 u32 temp = reg_val;
81
82 temp &= ~(1 << USBC_BP_ISCR_VBUS_CHANGE_DETECT);
83 temp &= ~(1 << USBC_BP_ISCR_ID_CHANGE_DETECT);
84 temp &= ~(1 << USBC_BP_ISCR_DPDM_CHANGE_DETECT);
85
86 return temp;
87}
88
89static void USBC_EnableIdPullUp(__iomem void *base)
90{
91 u32 reg_val;
92
93 reg_val = musb_readl(base, USBC_REG_o_ISCR);
94 reg_val |= (1 << USBC_BP_ISCR_ID_PULLUP_EN);
95 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
96 musb_writel(base, USBC_REG_o_ISCR, reg_val);
97}
98
Hans de Goede9c4f11d2015-01-11 20:34:48 +010099static void USBC_EnableDpDmPullUp(__iomem void *base)
100{
101 u32 reg_val;
102
103 reg_val = musb_readl(base, USBC_REG_o_ISCR);
104 reg_val |= (1 << USBC_BP_ISCR_DPDM_PULLUP_EN);
105 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
106 musb_writel(base, USBC_REG_o_ISCR, reg_val);
107}
108
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100109static void USBC_ForceIdToLow(__iomem void *base)
110{
111 u32 reg_val;
112
113 reg_val = musb_readl(base, USBC_REG_o_ISCR);
114 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
115 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
116 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
117 musb_writel(base, USBC_REG_o_ISCR, reg_val);
118}
119
120static void USBC_ForceIdToHigh(__iomem void *base)
121{
122 u32 reg_val;
123
124 reg_val = musb_readl(base, USBC_REG_o_ISCR);
125 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
126 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
127 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
128 musb_writel(base, USBC_REG_o_ISCR, reg_val);
129}
130
Hans de Goede836e3342015-06-14 11:55:28 +0200131static void USBC_ForceVbusValidToLow(__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_VBUS_VALID);
137 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
138 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
139 musb_writel(base, USBC_REG_o_ISCR, reg_val);
140}
141
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100142static void USBC_ForceVbusValidToHigh(__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_VBUS_VALID);
148 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
149 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
150 musb_writel(base, USBC_REG_o_ISCR, reg_val);
151}
152
153static void USBC_ConfigFIFO_Base(void)
154{
155 u32 reg_value;
156
157 /* config usb fifo, 8kb mode */
158 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
159 reg_value &= ~(0x03 << 0);
160 reg_value |= (1 << 0);
161 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
162}
163
164/******************************************************************************
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200165 * Needed for the DFU polling magic
166 ******************************************************************************/
167
168static u8 last_int_usb;
169
170bool dfu_usb_get_reset(void)
171{
172 return !!(last_int_usb & MUSB_INTR_RESET);
173}
174
175/******************************************************************************
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100176 * MUSB Glue code
177 ******************************************************************************/
178
179static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
180{
181 struct musb *musb = __hci;
182 irqreturn_t retval = IRQ_NONE;
183
184 /* read and flush interrupts */
185 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
Siarhei Siamashkabce07f62015-10-25 06:44:47 +0200186 last_int_usb = musb->int_usb;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100187 if (musb->int_usb)
188 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
189 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
190 if (musb->int_tx)
191 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
192 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
193 if (musb->int_rx)
194 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
195
196 if (musb->int_usb || musb->int_tx || musb->int_rx)
197 retval |= musb_interrupt(musb);
198
199 return retval;
200}
201
Hans de Goede836e3342015-06-14 11:55:28 +0200202/* musb_core does not call enable / disable in a balanced manner <sigh> */
203static bool enabled = false;
204
Hans de Goede81c49982015-06-17 21:33:54 +0200205static int sunxi_musb_enable(struct musb *musb)
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100206{
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800207 int ret;
208
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100209 pr_debug("%s():\n", __func__);
210
Maxime Ripard9634fa52015-08-04 17:04:10 +0200211 musb_ep_select(musb->mregs, 0);
212 musb_writeb(musb->mregs, MUSB_FADDR, 0);
213
Hans de Goede836e3342015-06-14 11:55:28 +0200214 if (enabled)
Hans de Goede81c49982015-06-17 21:33:54 +0200215 return 0;
Hans de Goede836e3342015-06-14 11:55:28 +0200216
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100217 /* select PIO mode */
218 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
219
Hans de Goedec93ecd02015-06-14 16:48:56 +0200220 if (is_host_enabled(musb)) {
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800221 ret = sunxi_usb_phy_vbus_detect(0);
222 if (ret == 1) {
223 printf("A charger is plugged into the OTG: ");
224 return -ENODEV;
Hans de Goedec93ecd02015-06-14 16:48:56 +0200225 }
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800226 ret = sunxi_usb_phy_id_detect(0);
227 if (ret == 1) {
Hans de Goedec77fbfb2015-06-14 17:40:37 +0200228 printf("No host cable detected: ");
229 return -ENODEV;
230 }
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800231 sunxi_usb_phy_power_on(0); /* port power on */
Hans de Goedec93ecd02015-06-14 16:48:56 +0200232 }
Hans de Goede836e3342015-06-14 11:55:28 +0200233
234 USBC_ForceVbusValidToHigh(musb->mregs);
235
236 enabled = true;
Hans de Goede81c49982015-06-17 21:33:54 +0200237 return 0;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100238}
239
240static void sunxi_musb_disable(struct musb *musb)
241{
242 pr_debug("%s():\n", __func__);
243
Hans de Goede836e3342015-06-14 11:55:28 +0200244 if (!enabled)
245 return;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200246
Chen-Yu Tsaid64afdc2016-09-07 14:25:21 +0800247 if (is_host_enabled(musb))
248 sunxi_usb_phy_power_off(0); /* port power off */
249
Hans de Goede836e3342015-06-14 11:55:28 +0200250 USBC_ForceVbusValidToLow(musb->mregs);
251 mdelay(200); /* Wait for the current session to timeout */
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200252
Hans de Goede836e3342015-06-14 11:55:28 +0200253 enabled = false;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100254}
255
256static int sunxi_musb_init(struct musb *musb)
257{
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200258 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100259
260 pr_debug("%s():\n", __func__);
261
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100262 musb->isr = sunxi_musb_interrupt;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200263
264 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
265#ifdef CONFIG_SUNXI_GEN_SUN6I
266 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
267#endif
Hans de Goede86979092015-04-27 14:54:47 +0200268 sunxi_usb_phy_init(0);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100269
270 USBC_ConfigFIFO_Base();
271 USBC_EnableDpDmPullUp(musb->mregs);
272 USBC_EnableIdPullUp(musb->mregs);
273
274 if (is_host_enabled(musb)) {
275 /* Host mode */
276 USBC_ForceIdToLow(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100277 } else {
278 /* Peripheral mode */
279 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100280 }
Hans de Goedef2b45c82015-02-11 09:05:18 +0100281 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100282
283 return 0;
284}
285
Hans de Goedeea059bf2015-06-17 15:49:26 +0200286static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100287 .init = sunxi_musb_init,
Hans de Goede9c4f11d2015-01-11 20:34:48 +0100288 .enable = sunxi_musb_enable,
289 .disable = sunxi_musb_disable,
290};
Hans de Goedeea059bf2015-06-17 15:49:26 +0200291
292static struct musb_hdrc_config musb_config = {
293 .multipoint = 1,
294 .dyn_fifo = 1,
295 .num_eps = 6,
296 .ram_bits = 11,
297};
298
299static struct musb_hdrc_platform_data musb_plat = {
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200300#if defined(CONFIG_USB_MUSB_HOST)
Hans de Goedeea059bf2015-06-17 15:49:26 +0200301 .mode = MUSB_HOST,
302#else
303 .mode = MUSB_PERIPHERAL,
304#endif
305 .config = &musb_config,
306 .power = 250,
307 .platform_ops = &sunxi_musb_ops,
308};
309
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200310#ifdef CONFIG_USB_MUSB_HOST
Hans de Goede4a67d732016-09-17 16:02:38 +0200311static int musb_usb_remove(struct udevice *dev);
312
313static int musb_usb_probe(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200314{
315 struct musb_host_data *host = dev_get_priv(dev);
316 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200317 int ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200318
319 priv->desc_before_addr = true;
320
Hans de Goede4a67d732016-09-17 16:02:38 +0200321 host->host = musb_init_controller(&musb_plat, NULL,
322 (void *)SUNXI_USB0_BASE);
Hans de Goedeec89e162016-04-02 20:46:09 +0200323 if (!host->host)
324 return -EIO;
325
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200326 ret = musb_lowlevel_init(host);
327 if (ret == 0)
328 printf("MUSB OTG\n");
Hans de Goede4a67d732016-09-17 16:02:38 +0200329 else
330 musb_usb_remove(dev);
Hans de Goede0b3845a2015-06-17 17:44:58 +0200331
Hans de Goede31e4f6f2015-06-18 22:45:34 +0200332 return ret;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200333}
334
Hans de Goede4a67d732016-09-17 16:02:38 +0200335static int musb_usb_remove(struct udevice *dev)
Hans de Goede0b3845a2015-06-17 17:44:58 +0200336{
337 struct musb_host_data *host = dev_get_priv(dev);
Hans de Goede779afb62016-06-05 16:53:04 +0200338 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede0b3845a2015-06-17 17:44:58 +0200339
340 musb_stop(host->host);
341
Hans de Goede779afb62016-06-05 16:53:04 +0200342 sunxi_usb_phy_exit(0);
343#ifdef CONFIG_SUNXI_GEN_SUN6I
344 clrbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
345#endif
346 clrbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
347
Hans de Goede4a67d732016-09-17 16:02:38 +0200348 free(host->host);
349 host->host = NULL;
350
Hans de Goede0b3845a2015-06-17 17:44:58 +0200351 return 0;
352}
353
354U_BOOT_DRIVER(usb_musb) = {
355 .name = "sunxi-musb",
356 .id = UCLASS_USB,
357 .probe = musb_usb_probe,
358 .remove = musb_usb_remove,
359 .ops = &musb_usb_ops,
360 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
361 .priv_auto_alloc_size = sizeof(struct musb_host_data),
362};
363#endif
364
Hans de Goedeea059bf2015-06-17 15:49:26 +0200365void sunxi_musb_board_init(void)
366{
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200367#ifdef CONFIG_USB_MUSB_HOST
Hans de Goede0b3845a2015-06-17 17:44:58 +0200368 struct udevice *dev;
369
370 /*
371 * Bind the driver directly for now as musb linux kernel support is
372 * still pending upstream so our dts files do not have the necessary
373 * nodes yet. TODO: Remove this as soon as the dts nodes are in place
374 * and bind by compatible instead.
375 */
376 device_bind_driver(dm_root(), "sunxi-musb", "sunxi-musb", &dev);
377#else
Hans de Goedeea059bf2015-06-17 15:49:26 +0200378 musb_register(&musb_plat, NULL, (void *)SUNXI_USB0_BASE);
Hans de Goede0b3845a2015-06-17 17:44:58 +0200379#endif
Hans de Goedeea059bf2015-06-17 15:49:26 +0200380}