]> git.sur5r.net Git - u-boot/blob - drivers/usb/musb-new/sunxi.c
musb: sunxi: Use simple way to fill musb_hdrc pdata
[u-boot] / drivers / usb / musb-new / sunxi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
17  */
18 #include <common.h>
19 #include <dm.h>
20 #include <asm/arch/cpu.h>
21 #include <asm/arch/clock.h>
22 #include <asm/arch/gpio.h>
23 #include <asm/arch/usb_phy.h>
24 #include <asm-generic/gpio.h>
25 #include <dm/lists.h>
26 #include <dm/root.h>
27 #include <linux/usb/musb.h>
28 #include "linux-compat.h"
29 #include "musb_core.h"
30 #include "musb_uboot.h"
31
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
78 struct 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
85 static 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
96 static 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
106 static 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
116 static 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
127 static 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
138 static 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
149 static 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
160 static 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 /******************************************************************************
172  * Needed for the DFU polling magic
173  ******************************************************************************/
174
175 static u8 last_int_usb;
176
177 bool dfu_usb_get_reset(void)
178 {
179         return !!(last_int_usb & MUSB_INTR_RESET);
180 }
181
182 /******************************************************************************
183  * MUSB Glue code
184  ******************************************************************************/
185
186 static 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);
193         last_int_usb = musb->int_usb;
194         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
209 /* musb_core does not call enable / disable in a balanced manner <sigh> */
210 static bool enabled = false;
211
212 static int sunxi_musb_enable(struct musb *musb)
213 {
214         int ret;
215
216         pr_debug("%s():\n", __func__);
217
218         musb_ep_select(musb->mregs, 0);
219         musb_writeb(musb->mregs, MUSB_FADDR, 0);
220
221         if (enabled)
222                 return 0;
223
224         /* select PIO mode */
225         musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
226
227         if (is_host_enabled(musb)) {
228                 ret = sunxi_usb_phy_vbus_detect(0);
229                 if (ret == 1) {
230                         printf("A charger is plugged into the OTG: ");
231                         return -ENODEV;
232                 }
233                 ret = sunxi_usb_phy_id_detect(0);
234                 if (ret == 1) {
235                         printf("No host cable detected: ");
236                         return -ENODEV;
237                 }
238                 sunxi_usb_phy_power_on(0); /* port power on */
239         }
240
241         USBC_ForceVbusValidToHigh(musb->mregs);
242
243         enabled = true;
244         return 0;
245 }
246
247 static void sunxi_musb_disable(struct musb *musb)
248 {
249         pr_debug("%s():\n", __func__);
250
251         if (!enabled)
252                 return;
253
254         if (is_host_enabled(musb))
255                 sunxi_usb_phy_power_off(0); /* port power off */
256
257         USBC_ForceVbusValidToLow(musb->mregs);
258         mdelay(200); /* Wait for the current session to timeout */
259
260         enabled = false;
261 }
262
263 static int sunxi_musb_init(struct musb *musb)
264 {
265         struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
266
267         pr_debug("%s():\n", __func__);
268
269         musb->isr = sunxi_musb_interrupt;
270
271         setbits_le32(&glue->ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
272 #ifdef CONFIG_SUNXI_GEN_SUN6I
273         setbits_le32(&glue->ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
274 #endif
275         sunxi_usb_phy_init(0);
276
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);
284         } else {
285                 /* Peripheral mode */
286                 USBC_ForceIdToHigh(musb->mregs);
287         }
288         USBC_ForceVbusValidToHigh(musb->mregs);
289
290         return 0;
291 }
292
293 static const struct musb_platform_ops sunxi_musb_ops = {
294         .init           = sunxi_musb_init,
295         .enable         = sunxi_musb_enable,
296         .disable        = sunxi_musb_disable,
297 };
298
299 /* Allwinner OTG supports up to 5 endpoints */
300 #define SUNXI_MUSB_MAX_EP_NUM           6
301 #define SUNXI_MUSB_RAM_BITS             11
302
303 static struct musb_hdrc_config musb_config = {
304         .multipoint     = true,
305         .dyn_fifo       = true,
306         .num_eps        = SUNXI_MUSB_MAX_EP_NUM,
307         .ram_bits       = SUNXI_MUSB_RAM_BITS,
308 };
309
310 static int musb_usb_probe(struct udevice *dev)
311 {
312         struct sunxi_glue *glue = dev_get_priv(dev);
313         struct musb_host_data *host = &glue->mdata;
314         struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
315         struct musb_hdrc_platform_data pdata;
316         void *base = dev_read_addr_ptr(dev);
317         int ret;
318
319         if (!base)
320                 return -EINVAL;
321
322         glue->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
323         if (IS_ERR(glue->ccm))
324                 return PTR_ERR(glue->ccm);
325
326         priv->desc_before_addr = true;
327
328         memset(&pdata, 0, sizeof(pdata));
329         pdata.power = 250;
330         pdata.platform_ops = &sunxi_musb_ops;
331         pdata.config = &musb_config;
332
333 #ifdef CONFIG_USB_MUSB_HOST
334         pdata.mode = MUSB_HOST;
335         host->host = musb_init_controller(&pdata, &glue->dev, base);
336         if (!host->host)
337                 return -EIO;
338
339         ret = musb_lowlevel_init(host);
340         if (!ret)
341                 printf("Allwinner mUSB OTG (Host)\n");
342 #else
343         pdata.mode = MUSB_PERIPHERAL;
344         ret = musb_register(&pdata, &glue->dev, base);
345         if (!ret)
346                 printf("Allwinner mUSB OTG (Peripheral)\n");
347 #endif
348
349         return ret;
350 }
351
352 static int musb_usb_remove(struct udevice *dev)
353 {
354         struct sunxi_glue *glue = dev_get_priv(dev);
355         struct musb_host_data *host = &glue->mdata;
356
357         musb_stop(host->host);
358
359         sunxi_usb_phy_exit(0);
360 #ifdef CONFIG_SUNXI_GEN_SUN6I
361         clrbits_le32(&glue->ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
362 #endif
363         clrbits_le32(&glue->ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
364
365         free(host->host);
366         host->host = NULL;
367
368         return 0;
369 }
370
371 static const struct udevice_id sunxi_musb_ids[] = {
372         { .compatible = "allwinner,sun4i-a10-musb" },
373         { .compatible = "allwinner,sun6i-a31-musb" },
374         { .compatible = "allwinner,sun8i-a33-musb" },
375         { .compatible = "allwinner,sun8i-h3-musb" },
376         { }
377 };
378
379 U_BOOT_DRIVER(usb_musb) = {
380         .name           = "sunxi-musb",
381 #ifdef CONFIG_USB_MUSB_HOST
382         .id             = UCLASS_USB,
383 #else
384         .id             = UCLASS_USB_DEV_GENERIC,
385 #endif
386         .of_match       = sunxi_musb_ids,
387         .probe          = musb_usb_probe,
388         .remove         = musb_usb_remove,
389 #ifdef CONFIG_USB_MUSB_HOST
390         .ops            = &musb_usb_ops,
391 #endif
392         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
393         .priv_auto_alloc_size = sizeof(struct sunxi_glue),
394 };