blob: 2ddcf33b5f022ae665cc553643410f6a0efd0b6c [file] [log] [blame]
Adam Ford56083312019-07-10 13:59:10 -05001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Texas Instruments da8xx "glue layer"
4 *
5 * Copyright (c) 2019, by Texas Instruments
6 *
7 * Based on the DA8xx "glue layer" code.
8 * Copyright (c) 2008-2019, MontaVista Software, Inc. <source@mvista.com>
9 *
10 * DT support
11 * Copyright (c) 2016 Petr Kulhavy <petr@barix.com>
12 * This file is part of the Inventra Controller Driver for Linux.
13 *
14 */
15
16#include <common.h>
17#include <dm.h>
18#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Adam Ford56083312019-07-10 13:59:10 -050020#include <dm/lists.h>
21#include <asm/arch/hardware.h>
22#include <asm/arch/da8xx-usb.h>
23#include <linux/usb/otg.h>
24#include <asm/omap_musb.h>
25#include <generic-phy.h>
26#include "linux-compat.h"
27#include "musb_core.h"
28#include "musb_uboot.h"
29
30/* USB 2.0 OTG module registers */
31#define DA8XX_USB_REVISION_REG 0x00
32#define DA8XX_USB_CTRL_REG 0x04
33#define DA8XX_USB_STAT_REG 0x08
34#define DA8XX_USB_EMULATION_REG 0x0c
35#define DA8XX_USB_SRP_FIX_TIME_REG 0x18
36#define DA8XX_USB_INTR_SRC_REG 0x20
37#define DA8XX_USB_INTR_SRC_SET_REG 0x24
38#define DA8XX_USB_INTR_SRC_CLEAR_REG 0x28
39#define DA8XX_USB_INTR_MASK_REG 0x2c
40#define DA8XX_USB_INTR_MASK_SET_REG 0x30
41#define DA8XX_USB_INTR_MASK_CLEAR_REG 0x34
42#define DA8XX_USB_INTR_SRC_MASKED_REG 0x38
43#define DA8XX_USB_END_OF_INTR_REG 0x3c
44#define DA8XX_USB_GENERIC_RNDIS_EP_SIZE_REG(n) (0x50 + (((n) - 1) << 2))
45
46/* Control register bits */
47#define DA8XX_SOFT_RESET_MASK 1
48
49#define DA8XX_USB_TX_EP_MASK 0x1f /* EP0 + 4 Tx EPs */
50#define DA8XX_USB_RX_EP_MASK 0x1e /* 4 Rx EPs */
51
52/* USB interrupt register bits */
53#define DA8XX_INTR_USB_SHIFT 16
54#define DA8XX_INTR_USB_MASK (0x1ff << DA8XX_INTR_USB_SHIFT) /* 8 Mentor */
55 /* interrupts and DRVVBUS interrupt */
56#define DA8XX_INTR_DRVVBUS 0x100
57#define DA8XX_INTR_RX_SHIFT 8
58#define DA8XX_INTR_RX_MASK (DA8XX_USB_RX_EP_MASK << DA8XX_INTR_RX_SHIFT)
59#define DA8XX_INTR_TX_SHIFT 0
60#define DA8XX_INTR_TX_MASK (DA8XX_USB_TX_EP_MASK << DA8XX_INTR_TX_SHIFT)
61
62#define DA8XX_MENTOR_CORE_OFFSET 0x400
63
64static irqreturn_t da8xx_musb_interrupt(int irq, void *hci)
65{
66 struct musb *musb = hci;
67 void __iomem *reg_base = musb->ctrl_base;
68 unsigned long flags;
69 irqreturn_t ret = IRQ_NONE;
70 u32 status;
71
72 spin_lock_irqsave(&musb->lock, flags);
73
74 /*
75 * NOTE: DA8XX shadows the Mentor IRQs. Don't manage them through
76 * the Mentor registers (except for setup), use the TI ones and EOI.
77 */
78
79 /* Acknowledge and handle non-CPPI interrupts */
80 status = musb_readl(reg_base, DA8XX_USB_INTR_SRC_MASKED_REG);
81 if (!status)
82 goto eoi;
83
84 musb_writel(reg_base, DA8XX_USB_INTR_SRC_CLEAR_REG, status);
85 dev_dbg(musb->controller, "USB IRQ %08x\n", status);
86
87 musb->int_rx = (status & DA8XX_INTR_RX_MASK) >> DA8XX_INTR_RX_SHIFT;
88 musb->int_tx = (status & DA8XX_INTR_TX_MASK) >> DA8XX_INTR_TX_SHIFT;
89 musb->int_usb = (status & DA8XX_INTR_USB_MASK) >> DA8XX_INTR_USB_SHIFT;
90
91 /*
92 * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
93 * DA8xx's missing ID change IRQ. We need an ID change IRQ to
94 * switch appropriately between halves of the OTG state machine.
95 * Managing DEVCTL.Session per Mentor docs requires that we know its
96 * value but DEVCTL.BDevice is invalid without DEVCTL.Session set.
97 * Also, DRVVBUS pulses for SRP (but not at 5 V)...
98 */
99 if (status & (DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT)) {
100 int drvvbus = musb_readl(reg_base, DA8XX_USB_STAT_REG);
101 void __iomem *mregs = musb->mregs;
102 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
103 int err;
104
105 err = musb->int_usb & MUSB_INTR_VBUSERROR;
106 if (err) {
107 /*
108 * The Mentor core doesn't debounce VBUS as needed
109 * to cope with device connect current spikes. This
110 * means it's not uncommon for bus-powered devices
111 * to get VBUS errors during enumeration.
112 *
113 * This is a workaround, but newer RTL from Mentor
114 * seems to allow a better one: "re"-starting sessions
115 * without waiting for VBUS to stop registering in
116 * devctl.
117 */
118 musb->int_usb &= ~MUSB_INTR_VBUSERROR;
119 WARNING("VBUS error workaround (delay coming)\n");
120 } else if (drvvbus) {
121 MUSB_HST_MODE(musb);
122 musb->port1_status |= USB_PORT_STAT_POWER;
123 } else if (!(musb->int_usb & MUSB_INTR_BABBLE)) {
124 /*
125 * When babble condition happens, drvvbus interrupt
126 * is also generated. Ignore this drvvbus interrupt
127 * and let babble interrupt handler recovers the
128 * controller; otherwise, the host-mode flag is lost
129 * due to the MUSB_DEV_MODE() call below and babble
130 * recovery logic will not be called.
131 */
132 musb->is_active = 0;
133 MUSB_DEV_MODE(musb);
134 musb->port1_status &= ~USB_PORT_STAT_POWER;
135 }
136 ret = IRQ_HANDLED;
137 }
138
139 if (musb->int_tx || musb->int_rx || musb->int_usb)
140 ret |= musb_interrupt(musb);
141eoi:
142 /* EOI needs to be written for the IRQ to be re-asserted. */
143 if (ret == IRQ_HANDLED || status)
144 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
145
146 spin_unlock_irqrestore(&musb->lock, flags);
147
148 return ret;
149}
150
151static int da8xx_musb_init(struct musb *musb)
152{
153 u32 revision;
154 void __iomem *reg_base = musb->ctrl_base;
155
156 int ret;
157
158 /* reset the controller */
159 writel(0x1, &da8xx_usb_regs->control);
160 udelay(50);
161
162 /* Returns zero if e.g. not clocked */
163 revision = readl(&da8xx_usb_regs->revision);
164 if (revision == 0)
165 return -ENODEV;
166
167 /* Disable all interrupts */
168 writel((DA8XX_USB_USBINT_MASK | DA8XX_USB_TXINT_MASK |
169 DA8XX_USB_RXINT_MASK), &da8xx_usb_regs->intmsk_set);
170
171 musb->mregs += DA8XX_MENTOR_CORE_OFFSET;
172
173 /* NOTE: IRQs are in mixed mode, not bypass to pure MUSB */
174 debug("DA8xx OTG revision %08x, control %02x\n", revision,
175 musb_readb(reg_base, DA8XX_USB_CTRL_REG));
176
177 musb->isr = da8xx_musb_interrupt;
178 return 0;
179}
180
181static int da8xx_musb_exit(struct musb *musb)
182{
183 /* flush any interrupts */
184 writel((DA8XX_USB_USBINT_MASK | DA8XX_USB_TXINT_MASK |
185 DA8XX_USB_RXINT_MASK), &da8xx_usb_regs->intmsk_clr);
186 writel(0, &da8xx_usb_regs->eoi);
187
188 return 0;
189}
190
191/**
192 * da8xx_musb_enable - enable interrupts
193 */
194static int da8xx_musb_enable(struct musb *musb)
195{
196 void __iomem *reg_base = musb->ctrl_base;
197 u32 mask;
198
199 /* Workaround: setup IRQs through both register sets. */
200 mask = ((musb->epmask & DA8XX_USB_TX_EP_MASK) << DA8XX_INTR_TX_SHIFT) |
201 ((musb->epmask & DA8XX_USB_RX_EP_MASK) << DA8XX_INTR_RX_SHIFT) |
202 DA8XX_INTR_USB_MASK;
203 musb_writel(reg_base, DA8XX_USB_INTR_MASK_SET_REG, mask);
204
205 /* Force the DRVVBUS IRQ so we can start polling for ID change. */
206 musb_writel(reg_base, DA8XX_USB_INTR_SRC_SET_REG,
207 DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT);
208
209 return 0;
210}
211
212/**
213 * da8xx_musb_disable - disable HDRC and flush interrupts
214 */
215static void da8xx_musb_disable(struct musb *musb)
216{
217 void __iomem *reg_base = musb->ctrl_base;
218
219 musb_writel(reg_base, DA8XX_USB_INTR_MASK_CLEAR_REG,
220 DA8XX_INTR_USB_MASK |
221 DA8XX_INTR_TX_MASK | DA8XX_INTR_RX_MASK);
222 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
223}
224
225void da8xx_musb_reset(struct udevice *dev)
226{
227 void *reg_base = dev_read_addr_ptr(dev);
228
229 /* Reset the controller */
230 musb_writel(reg_base, DA8XX_USB_CTRL_REG, DA8XX_SOFT_RESET_MASK);
231}
232
233void da8xx_musb_clear_irq(struct udevice *dev)
234{
235 /* flush any interrupts */
236 writel((DA8XX_USB_USBINT_MASK | DA8XX_USB_TXINT_MASK |
237 DA8XX_USB_RXINT_MASK), &da8xx_usb_regs->intmsk_clr);
238 writel(0, &da8xx_usb_regs->eoi);
239}
240
241const struct musb_platform_ops da8xx_ops = {
242 .init = da8xx_musb_init,
243 .exit = da8xx_musb_exit,
244 .enable = da8xx_musb_enable,
245 .disable = da8xx_musb_disable,
246};
247
248struct da8xx_musb_platdata {
249 void *base;
250 void *ctrl_mod_base;
251 struct musb_hdrc_platform_data plat;
252 struct musb_hdrc_config musb_config;
253 struct omap_musb_board_data otg_board_data;
254 struct phy phy;
255};
256
257static int da8xx_musb_ofdata_to_platdata(struct udevice *dev)
258{
259 struct da8xx_musb_platdata *platdata = dev_get_platdata(dev);
260 const void *fdt = gd->fdt_blob;
261 int node = dev_of_offset(dev);
262
263 platdata->base = (void *)dev_read_addr_ptr(dev);
264 platdata->musb_config.multipoint = 1;
265 platdata->musb_config.dyn_fifo = 1;
266 platdata->musb_config.num_eps = 5;
267 platdata->musb_config.ram_bits = 10;
268 platdata->plat.power = fdtdec_get_int(fdt, node, "power", 50);
269 platdata->otg_board_data.interface_type = MUSB_INTERFACE_UTMI;
270 platdata->plat.mode = MUSB_HOST;
271 platdata->otg_board_data.dev = dev;
272 platdata->plat.config = &platdata->musb_config;
273 platdata->plat.platform_ops = &da8xx_ops;
274 platdata->plat.board_data = &platdata->otg_board_data;
275 platdata->otg_board_data.clear_irq = da8xx_musb_clear_irq;
276 platdata->otg_board_data.reset = da8xx_musb_reset;
277 return 0;
278}
279
280static int da8xx_musb_probe(struct udevice *dev)
281{
282 struct musb_host_data *host = dev_get_priv(dev);
283 struct da8xx_musb_platdata *platdata = dev_get_platdata(dev);
284 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
285 struct omap_musb_board_data *otg_board_data;
286 int ret;
287 void *base = dev_read_addr_ptr(dev);
288
289 /* Get the phy info from the device tree */
290 ret = generic_phy_get_by_name(dev, "usb-phy", &platdata->phy);
291 if (ret)
292 return ret;
293
294 /* Initialize the phy */
295 ret = generic_phy_init(&platdata->phy);
296 if (ret)
297 return ret;
298
299 /* enable psc for usb2.0 */
300 lpsc_on(33);
301
302 /* Enable phy */
303 generic_phy_power_on(&platdata->phy);
304
305 priv->desc_before_addr = true;
306 otg_board_data = &platdata->otg_board_data;
307
308 host->host = musb_init_controller(&platdata->plat,
309 (struct device *)otg_board_data,
310 platdata->base);
311 if (!host->host) {
312 ret = -ENODEV;
313 goto shutdown; /* Shutdown what we started */
314 }
315
316 ret = musb_lowlevel_init(host);
317
318 if (ret == 0)
319 return 0;
320shutdown:
321 /* Turn off the phy if we fail */
322 generic_phy_power_off(&platdata->phy);
323 lpsc_disable(33);
324 return ret;
325}
326
327static int da8xx_musb_remove(struct udevice *dev)
328{
329 struct musb_host_data *host = dev_get_priv(dev);
330
331 musb_stop(host->host);
332
333 return 0;
334}
335
336static const struct udevice_id da8xx_musb_ids[] = {
337 { .compatible = "ti,da830-musb" },
338 { }
339};
340
341U_BOOT_DRIVER(da8xx_musb) = {
342 .name = "da8xx-musb",
343 .id = UCLASS_USB,
344 .of_match = da8xx_musb_ids,
345 .ofdata_to_platdata = da8xx_musb_ofdata_to_platdata,
346 .probe = da8xx_musb_probe,
347 .remove = da8xx_musb_remove,
348 .ops = &musb_usb_ops,
349 .platdata_auto_alloc_size = sizeof(struct da8xx_musb_platdata),
350 .priv_auto_alloc_size = sizeof(struct musb_host_data),
351};