blob: ab902c65748a61c93d790b157a6e1434084753a0 [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>
Simon Glass9d922452017-05-17 17:18:03 -060019#include <dm.h>
Hans de Goede28a15ef2015-01-11 20:34:48 +010020#include <asm/arch/cpu.h>
Hans de Goede375de012015-04-27 11:44:22 +020021#include <asm/arch/clock.h>
Hans de Goede52defe82015-02-16 22:13:43 +010022#include <asm/arch/gpio.h>
Hans de Goede2aacc422015-04-27 15:05:10 +020023#include <asm/arch/usb_phy.h>
Hans de Goede52defe82015-02-16 22:13:43 +010024#include <asm-generic/gpio.h>
Hans de Goede91183ba2015-06-17 17:44:58 +020025#include <dm/lists.h>
26#include <dm/root.h>
Hans de Goeded42faf32015-06-17 15:49:26 +020027#include <linux/usb/musb.h>
Hans de Goede28a15ef2015-01-11 20:34:48 +010028#include "linux-compat.h"
29#include "musb_core.h"
Hans de Goede91183ba2015-06-17 17:44:58 +020030#include "musb_uboot.h"
Hans de Goede28a15ef2015-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
Jagan Teki831cc982018-05-07 13:03:17 +053078struct sunxi_glue {
79 struct musb_host_data mdata;
80 struct sunxi_ccm_reg *ccm;
81 struct device dev;
82};
83#define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
84
Hans de Goede28a15ef2015-01-11 20:34:48 +010085static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
86{
87 u32 temp = reg_val;
88
89 temp &= ~(1 << USBC_BP_ISCR_VBUS_CHANGE_DETECT);
90 temp &= ~(1 << USBC_BP_ISCR_ID_CHANGE_DETECT);
91 temp &= ~(1 << USBC_BP_ISCR_DPDM_CHANGE_DETECT);
92
93 return temp;
94}
95
96static void USBC_EnableIdPullUp(__iomem void *base)
97{
98 u32 reg_val;
99
100 reg_val = musb_readl(base, USBC_REG_o_ISCR);
101 reg_val |= (1 << USBC_BP_ISCR_ID_PULLUP_EN);
102 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
103 musb_writel(base, USBC_REG_o_ISCR, reg_val);
104}
105
Hans de Goede28a15ef2015-01-11 20:34:48 +0100106static void USBC_EnableDpDmPullUp(__iomem void *base)
107{
108 u32 reg_val;
109
110 reg_val = musb_readl(base, USBC_REG_o_ISCR);
111 reg_val |= (1 << USBC_BP_ISCR_DPDM_PULLUP_EN);
112 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
113 musb_writel(base, USBC_REG_o_ISCR, reg_val);
114}
115
Hans de Goede28a15ef2015-01-11 20:34:48 +0100116static void USBC_ForceIdToLow(__iomem void *base)
117{
118 u32 reg_val;
119
120 reg_val = musb_readl(base, USBC_REG_o_ISCR);
121 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
122 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
123 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
124 musb_writel(base, USBC_REG_o_ISCR, reg_val);
125}
126
127static void USBC_ForceIdToHigh(__iomem void *base)
128{
129 u32 reg_val;
130
131 reg_val = musb_readl(base, USBC_REG_o_ISCR);
132 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
133 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
134 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
135 musb_writel(base, USBC_REG_o_ISCR, reg_val);
136}
137
Hans de Goedee1abfa42015-06-14 11:55:28 +0200138static void USBC_ForceVbusValidToLow(__iomem void *base)
139{
140 u32 reg_val;
141
142 reg_val = musb_readl(base, USBC_REG_o_ISCR);
143 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
144 reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
145 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
146 musb_writel(base, USBC_REG_o_ISCR, reg_val);
147}
148
Hans de Goede28a15ef2015-01-11 20:34:48 +0100149static void USBC_ForceVbusValidToHigh(__iomem void *base)
150{
151 u32 reg_val;
152
153 reg_val = musb_readl(base, USBC_REG_o_ISCR);
154 reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
155 reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
156 reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
157 musb_writel(base, USBC_REG_o_ISCR, reg_val);
158}
159
160static void USBC_ConfigFIFO_Base(void)
161{
162 u32 reg_value;
163
164 /* config usb fifo, 8kb mode */
165 reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
166 reg_value &= ~(0x03 << 0);
167 reg_value |= (1 << 0);
168 writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
169}
170
171/******************************************************************************
Siarhei Siamashka6047a3a2015-10-25 06:44:47 +0200172 * Needed for the DFU polling magic
173 ******************************************************************************/
174
175static u8 last_int_usb;
176
177bool dfu_usb_get_reset(void)
178{
179 return !!(last_int_usb & MUSB_INTR_RESET);
180}
181
182/******************************************************************************
Hans de Goede28a15ef2015-01-11 20:34:48 +0100183 * MUSB Glue code
184 ******************************************************************************/
185
186static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
187{
188 struct musb *musb = __hci;
189 irqreturn_t retval = IRQ_NONE;
190
191 /* read and flush interrupts */
192 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
Siarhei Siamashka6047a3a2015-10-25 06:44:47 +0200193 last_int_usb = musb->int_usb;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100194 if (musb->int_usb)
195 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
196 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
197 if (musb->int_tx)
198 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
199 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
200 if (musb->int_rx)
201 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
202
203 if (musb->int_usb || musb->int_tx || musb->int_rx)
204 retval |= musb_interrupt(musb);
205
206 return retval;
207}
208
Hans de Goedee1abfa42015-06-14 11:55:28 +0200209/* musb_core does not call enable / disable in a balanced manner <sigh> */
210static bool enabled = false;
211
Hans de Goede15837232015-06-17 21:33:54 +0200212static int sunxi_musb_enable(struct musb *musb)
Hans de Goede28a15ef2015-01-11 20:34:48 +0100213{
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800214 int ret;
215
Hans de Goede28a15ef2015-01-11 20:34:48 +0100216 pr_debug("%s():\n", __func__);
217
Maxime Ripard1feda632015-08-04 17:04:10 +0200218 musb_ep_select(musb->mregs, 0);
219 musb_writeb(musb->mregs, MUSB_FADDR, 0);
220
Hans de Goedee1abfa42015-06-14 11:55:28 +0200221 if (enabled)
Hans de Goede15837232015-06-17 21:33:54 +0200222 return 0;
Hans de Goedee1abfa42015-06-14 11:55:28 +0200223
Hans de Goede28a15ef2015-01-11 20:34:48 +0100224 /* select PIO mode */
225 musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
226
Hans de Goedeb41972e2015-06-14 16:48:56 +0200227 if (is_host_enabled(musb)) {
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800228 ret = sunxi_usb_phy_vbus_detect(0);
229 if (ret == 1) {
230 printf("A charger is plugged into the OTG: ");
231 return -ENODEV;
Hans de Goedeb41972e2015-06-14 16:48:56 +0200232 }
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800233 ret = sunxi_usb_phy_id_detect(0);
234 if (ret == 1) {
Hans de Goede71cbe0d2015-06-14 17:40:37 +0200235 printf("No host cable detected: ");
236 return -ENODEV;
237 }
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800238 sunxi_usb_phy_power_on(0); /* port power on */
Hans de Goedeb41972e2015-06-14 16:48:56 +0200239 }
Hans de Goedee1abfa42015-06-14 11:55:28 +0200240
241 USBC_ForceVbusValidToHigh(musb->mregs);
242
243 enabled = true;
Hans de Goede15837232015-06-17 21:33:54 +0200244 return 0;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100245}
246
247static void sunxi_musb_disable(struct musb *musb)
248{
249 pr_debug("%s():\n", __func__);
250
Hans de Goedee1abfa42015-06-14 11:55:28 +0200251 if (!enabled)
252 return;
Hans de Goede375de012015-04-27 11:44:22 +0200253
Chen-Yu Tsai57075a42016-09-07 14:25:21 +0800254 if (is_host_enabled(musb))
255 sunxi_usb_phy_power_off(0); /* port power off */
256
Hans de Goedee1abfa42015-06-14 11:55:28 +0200257 USBC_ForceVbusValidToLow(musb->mregs);
258 mdelay(200); /* Wait for the current session to timeout */
259
260 enabled = false;
Hans de Goede28a15ef2015-01-11 20:34:48 +0100261}
262
263static int sunxi_musb_init(struct musb *musb)
264{
Jagan Teki831cc982018-05-07 13:03:17 +0530265 struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100266
267 pr_debug("%s():\n", __func__);
268
Hans de Goede28a15ef2015-01-11 20:34:48 +0100269 musb->isr = sunxi_musb_interrupt;
Hans de Goede375de012015-04-27 11:44:22 +0200270
Jagan Teki831cc982018-05-07 13:03:17 +0530271 setbits_le32(&glue->ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
Hans de Goede375de012015-04-27 11:44:22 +0200272#ifdef CONFIG_SUNXI_GEN_SUN6I
Jagan Teki831cc982018-05-07 13:03:17 +0530273 setbits_le32(&glue->ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
Hans de Goede375de012015-04-27 11:44:22 +0200274#endif
Hans de Goede7b798652015-04-27 14:54:47 +0200275 sunxi_usb_phy_init(0);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100276
277 USBC_ConfigFIFO_Base();
278 USBC_EnableDpDmPullUp(musb->mregs);
279 USBC_EnableIdPullUp(musb->mregs);
280
281 if (is_host_enabled(musb)) {
282 /* Host mode */
283 USBC_ForceIdToLow(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100284 } else {
285 /* Peripheral mode */
286 USBC_ForceIdToHigh(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100287 }
Hans de Goedeb1b912d2015-02-11 09:05:18 +0100288 USBC_ForceVbusValidToHigh(musb->mregs);
Hans de Goede28a15ef2015-01-11 20:34:48 +0100289
290 return 0;
291}
292
Hans de Goeded42faf32015-06-17 15:49:26 +0200293static const struct musb_platform_ops sunxi_musb_ops = {
Hans de Goede28a15ef2015-01-11 20:34:48 +0100294 .init = sunxi_musb_init,
Hans de Goede28a15ef2015-01-11 20:34:48 +0100295 .enable = sunxi_musb_enable,
296 .disable = sunxi_musb_disable,
297};
Hans de Goeded42faf32015-06-17 15:49:26 +0200298
299static struct musb_hdrc_config musb_config = {
300 .multipoint = 1,
301 .dyn_fifo = 1,
302 .num_eps = 6,
303 .ram_bits = 11,
304};
305
306static struct musb_hdrc_platform_data musb_plat = {
Paul Kocialkowski95de1e22015-08-04 17:04:06 +0200307#if defined(CONFIG_USB_MUSB_HOST)
Hans de Goeded42faf32015-06-17 15:49:26 +0200308 .mode = MUSB_HOST,
309#else
310 .mode = MUSB_PERIPHERAL,
311#endif
312 .config = &musb_config,
313 .power = 250,
314 .platform_ops = &sunxi_musb_ops,
315};
316
Hans de Goede7c22e262016-09-17 16:02:38 +0200317static int musb_usb_probe(struct udevice *dev)
Hans de Goede91183ba2015-06-17 17:44:58 +0200318{
Jagan Teki831cc982018-05-07 13:03:17 +0530319 struct sunxi_glue *glue = dev_get_priv(dev);
320 struct musb_host_data *host = &glue->mdata;
Hans de Goede91183ba2015-06-17 17:44:58 +0200321 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
Chen-Yu Tsaif4f98962017-12-30 20:44:07 +0800322 void *base = dev_read_addr_ptr(dev);
Hans de Goede56a20852015-06-18 22:45:34 +0200323 int ret;
Hans de Goede91183ba2015-06-17 17:44:58 +0200324
Chen-Yu Tsaif4f98962017-12-30 20:44:07 +0800325 if (!base)
326 return -EINVAL;
327
Jagan Teki831cc982018-05-07 13:03:17 +0530328 glue->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
329 if (IS_ERR(glue->ccm))
330 return PTR_ERR(glue->ccm);
331
Hans de Goede91183ba2015-06-17 17:44:58 +0200332 priv->desc_before_addr = true;
333
Maxime Ripard3a61b082017-09-05 22:10:35 +0200334#ifdef CONFIG_USB_MUSB_HOST
Jagan Teki831cc982018-05-07 13:03:17 +0530335 host->host = musb_init_controller(&musb_plat, &glue->dev, base);
Hans de Goede38b4a3e2016-04-02 20:46:09 +0200336 if (!host->host)
337 return -EIO;
338
Hans de Goede56a20852015-06-18 22:45:34 +0200339 ret = musb_lowlevel_init(host);
Maxime Ripard3a61b082017-09-05 22:10:35 +0200340 if (!ret)
341 printf("Allwinner mUSB OTG (Host)\n");
342#else
Jagan Teki831cc982018-05-07 13:03:17 +0530343 ret = musb_register(&musb_plat, &glue->dev, base);
Maxime Ripard3a61b082017-09-05 22:10:35 +0200344 if (!ret)
345 printf("Allwinner mUSB OTG (Peripheral)\n");
346#endif
Hans de Goede91183ba2015-06-17 17:44:58 +0200347
Hans de Goede56a20852015-06-18 22:45:34 +0200348 return ret;
Hans de Goede91183ba2015-06-17 17:44:58 +0200349}
350
Hans de Goede7c22e262016-09-17 16:02:38 +0200351static int musb_usb_remove(struct udevice *dev)
Hans de Goede91183ba2015-06-17 17:44:58 +0200352{
Jagan Teki831cc982018-05-07 13:03:17 +0530353 struct sunxi_glue *glue = dev_get_priv(dev);
354 struct musb_host_data *host = &glue->mdata;
Hans de Goede91183ba2015-06-17 17:44:58 +0200355
356 musb_stop(host->host);
357
Hans de Goedebca4c3c2016-06-05 16:53:04 +0200358 sunxi_usb_phy_exit(0);
359#ifdef CONFIG_SUNXI_GEN_SUN6I
Jagan Teki831cc982018-05-07 13:03:17 +0530360 clrbits_le32(&glue->ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
Hans de Goedebca4c3c2016-06-05 16:53:04 +0200361#endif
Jagan Teki831cc982018-05-07 13:03:17 +0530362 clrbits_le32(&glue->ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
Hans de Goedebca4c3c2016-06-05 16:53:04 +0200363
Hans de Goede7c22e262016-09-17 16:02:38 +0200364 free(host->host);
365 host->host = NULL;
366
Hans de Goede91183ba2015-06-17 17:44:58 +0200367 return 0;
368}
369
Maxime Ripard3a61b082017-09-05 22:10:35 +0200370static const struct udevice_id sunxi_musb_ids[] = {
371 { .compatible = "allwinner,sun4i-a10-musb" },
372 { .compatible = "allwinner,sun6i-a31-musb" },
373 { .compatible = "allwinner,sun8i-a33-musb" },
374 { .compatible = "allwinner,sun8i-h3-musb" },
375 { }
376};
377
Hans de Goede91183ba2015-06-17 17:44:58 +0200378U_BOOT_DRIVER(usb_musb) = {
Maxime Ripard3a61b082017-09-05 22:10:35 +0200379 .name = "sunxi-musb",
380#ifdef CONFIG_USB_MUSB_HOST
381 .id = UCLASS_USB,
382#else
383 .id = UCLASS_USB_DEV_GENERIC,
384#endif
385 .of_match = sunxi_musb_ids,
386 .probe = musb_usb_probe,
387 .remove = musb_usb_remove,
388#ifdef CONFIG_USB_MUSB_HOST
389 .ops = &musb_usb_ops,
390#endif
Hans de Goede91183ba2015-06-17 17:44:58 +0200391 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
Jagan Teki831cc982018-05-07 13:03:17 +0530392 .priv_auto_alloc_size = sizeof(struct sunxi_glue),
Hans de Goede91183ba2015-06-17 17:44:58 +0200393};