1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Texas Instruments DSPS platforms "glue layer"
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012, by Texas Instruments
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Based on the am35x "glue layer" code.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This file is part of the Inventra Controller Driver for Linux.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * musb_dsps.c will be a common file for all the TI DSPS platforms
12*4882a593Smuzhiyun * such as dm64x, dm36x, dm35x, da8x, am35x and ti81x.
13*4882a593Smuzhiyun * For now only ti81x is using this and in future davinci.c, am35x.c
14*4882a593Smuzhiyun * da8xx.c would be merged to this file after testing.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/io.h>
18*4882a593Smuzhiyun #include <linux/err.h>
19*4882a593Smuzhiyun #include <linux/platform_device.h>
20*4882a593Smuzhiyun #include <linux/dma-mapping.h>
21*4882a593Smuzhiyun #include <linux/pm_runtime.h>
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/usb/usb_phy_generic.h>
24*4882a593Smuzhiyun #include <linux/platform_data/usb-omap.h>
25*4882a593Smuzhiyun #include <linux/sizes.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/of.h>
28*4882a593Smuzhiyun #include <linux/of_device.h>
29*4882a593Smuzhiyun #include <linux/of_address.h>
30*4882a593Smuzhiyun #include <linux/of_irq.h>
31*4882a593Smuzhiyun #include <linux/usb/of.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include <linux/debugfs.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include "musb_core.h"
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun static const struct of_device_id musb_dsps_of_match[];
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * DSPS musb wrapper register offset.
41*4882a593Smuzhiyun * FIXME: This should be expanded to have all the wrapper registers from TI DSPS
42*4882a593Smuzhiyun * musb ips.
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun struct dsps_musb_wrapper {
45*4882a593Smuzhiyun u16 revision;
46*4882a593Smuzhiyun u16 control;
47*4882a593Smuzhiyun u16 status;
48*4882a593Smuzhiyun u16 epintr_set;
49*4882a593Smuzhiyun u16 epintr_clear;
50*4882a593Smuzhiyun u16 epintr_status;
51*4882a593Smuzhiyun u16 coreintr_set;
52*4882a593Smuzhiyun u16 coreintr_clear;
53*4882a593Smuzhiyun u16 coreintr_status;
54*4882a593Smuzhiyun u16 phy_utmi;
55*4882a593Smuzhiyun u16 mode;
56*4882a593Smuzhiyun u16 tx_mode;
57*4882a593Smuzhiyun u16 rx_mode;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* bit positions for control */
60*4882a593Smuzhiyun unsigned reset:5;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* bit positions for interrupt */
63*4882a593Smuzhiyun unsigned usb_shift:5;
64*4882a593Smuzhiyun u32 usb_mask;
65*4882a593Smuzhiyun u32 usb_bitmap;
66*4882a593Smuzhiyun unsigned drvvbus:5;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun unsigned txep_shift:5;
69*4882a593Smuzhiyun u32 txep_mask;
70*4882a593Smuzhiyun u32 txep_bitmap;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun unsigned rxep_shift:5;
73*4882a593Smuzhiyun u32 rxep_mask;
74*4882a593Smuzhiyun u32 rxep_bitmap;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* bit positions for phy_utmi */
77*4882a593Smuzhiyun unsigned otg_disable:5;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* bit positions for mode */
80*4882a593Smuzhiyun unsigned iddig:5;
81*4882a593Smuzhiyun unsigned iddig_mux:5;
82*4882a593Smuzhiyun /* miscellaneous stuff */
83*4882a593Smuzhiyun unsigned poll_timeout;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * register shadow for suspend
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun struct dsps_context {
90*4882a593Smuzhiyun u32 control;
91*4882a593Smuzhiyun u32 epintr;
92*4882a593Smuzhiyun u32 coreintr;
93*4882a593Smuzhiyun u32 phy_utmi;
94*4882a593Smuzhiyun u32 mode;
95*4882a593Smuzhiyun u32 tx_mode;
96*4882a593Smuzhiyun u32 rx_mode;
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * DSPS glue structure.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun struct dsps_glue {
103*4882a593Smuzhiyun struct device *dev;
104*4882a593Smuzhiyun struct platform_device *musb; /* child musb pdev */
105*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp; /* wrapper register offsets */
106*4882a593Smuzhiyun int vbus_irq; /* optional vbus irq */
107*4882a593Smuzhiyun unsigned long last_timer; /* last timer data for each instance */
108*4882a593Smuzhiyun bool sw_babble_enabled;
109*4882a593Smuzhiyun void __iomem *usbss_base;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun struct dsps_context context;
112*4882a593Smuzhiyun struct debugfs_regset32 regset;
113*4882a593Smuzhiyun struct dentry *dbgfs_root;
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun static const struct debugfs_reg32 dsps_musb_regs[] = {
117*4882a593Smuzhiyun { "revision", 0x00 },
118*4882a593Smuzhiyun { "control", 0x14 },
119*4882a593Smuzhiyun { "status", 0x18 },
120*4882a593Smuzhiyun { "eoi", 0x24 },
121*4882a593Smuzhiyun { "intr0_stat", 0x30 },
122*4882a593Smuzhiyun { "intr1_stat", 0x34 },
123*4882a593Smuzhiyun { "intr0_set", 0x38 },
124*4882a593Smuzhiyun { "intr1_set", 0x3c },
125*4882a593Smuzhiyun { "txmode", 0x70 },
126*4882a593Smuzhiyun { "rxmode", 0x74 },
127*4882a593Smuzhiyun { "autoreq", 0xd0 },
128*4882a593Smuzhiyun { "srpfixtime", 0xd4 },
129*4882a593Smuzhiyun { "tdown", 0xd8 },
130*4882a593Smuzhiyun { "phy_utmi", 0xe0 },
131*4882a593Smuzhiyun { "mode", 0xe8 },
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
dsps_mod_timer(struct dsps_glue * glue,int wait_ms)134*4882a593Smuzhiyun static void dsps_mod_timer(struct dsps_glue *glue, int wait_ms)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun struct musb *musb = platform_get_drvdata(glue->musb);
137*4882a593Smuzhiyun int wait;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (wait_ms < 0)
140*4882a593Smuzhiyun wait = msecs_to_jiffies(glue->wrp->poll_timeout);
141*4882a593Smuzhiyun else
142*4882a593Smuzhiyun wait = msecs_to_jiffies(wait_ms);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun mod_timer(&musb->dev_timer, jiffies + wait);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun * If no vbus irq from the PMIC is configured, we need to poll VBUS status.
149*4882a593Smuzhiyun */
dsps_mod_timer_optional(struct dsps_glue * glue)150*4882a593Smuzhiyun static void dsps_mod_timer_optional(struct dsps_glue *glue)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun if (glue->vbus_irq)
153*4882a593Smuzhiyun return;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun dsps_mod_timer(glue, -1);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* USBSS / USB AM335x */
159*4882a593Smuzhiyun #define USBSS_IRQ_STATUS 0x28
160*4882a593Smuzhiyun #define USBSS_IRQ_ENABLER 0x2c
161*4882a593Smuzhiyun #define USBSS_IRQ_CLEARR 0x30
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun #define USBSS_IRQ_PD_COMP (1 << 2)
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * dsps_musb_enable - enable interrupts
167*4882a593Smuzhiyun */
dsps_musb_enable(struct musb * musb)168*4882a593Smuzhiyun static void dsps_musb_enable(struct musb *musb)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun struct device *dev = musb->controller;
171*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
172*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
173*4882a593Smuzhiyun void __iomem *reg_base = musb->ctrl_base;
174*4882a593Smuzhiyun u32 epmask, coremask;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /* Workaround: setup IRQs through both register sets. */
177*4882a593Smuzhiyun epmask = ((musb->epmask & wrp->txep_mask) << wrp->txep_shift) |
178*4882a593Smuzhiyun ((musb->epmask & wrp->rxep_mask) << wrp->rxep_shift);
179*4882a593Smuzhiyun coremask = (wrp->usb_bitmap & ~MUSB_INTR_SOF);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun musb_writel(reg_base, wrp->epintr_set, epmask);
182*4882a593Smuzhiyun musb_writel(reg_base, wrp->coreintr_set, coremask);
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * start polling for runtime PM active and idle,
185*4882a593Smuzhiyun * and for ID change in dual-role idle mode.
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun if (musb->xceiv->otg->state == OTG_STATE_B_IDLE)
188*4882a593Smuzhiyun dsps_mod_timer(glue, -1);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * dsps_musb_disable - disable HDRC and flush interrupts
193*4882a593Smuzhiyun */
dsps_musb_disable(struct musb * musb)194*4882a593Smuzhiyun static void dsps_musb_disable(struct musb *musb)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun struct device *dev = musb->controller;
197*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
198*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
199*4882a593Smuzhiyun void __iomem *reg_base = musb->ctrl_base;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun musb_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap);
202*4882a593Smuzhiyun musb_writel(reg_base, wrp->epintr_clear,
203*4882a593Smuzhiyun wrp->txep_bitmap | wrp->rxep_bitmap);
204*4882a593Smuzhiyun del_timer_sync(&musb->dev_timer);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* Caller must take musb->lock */
dsps_check_status(struct musb * musb,void * unused)208*4882a593Smuzhiyun static int dsps_check_status(struct musb *musb, void *unused)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun void __iomem *mregs = musb->mregs;
211*4882a593Smuzhiyun struct device *dev = musb->controller;
212*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
213*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
214*4882a593Smuzhiyun u8 devctl;
215*4882a593Smuzhiyun int skip_session = 0;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (glue->vbus_irq)
218*4882a593Smuzhiyun del_timer(&musb->dev_timer);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun * We poll because DSPS IP's won't expose several OTG-critical
222*4882a593Smuzhiyun * status change events (from the transceiver) otherwise.
223*4882a593Smuzhiyun */
224*4882a593Smuzhiyun devctl = musb_readb(mregs, MUSB_DEVCTL);
225*4882a593Smuzhiyun dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
226*4882a593Smuzhiyun usb_otg_state_string(musb->xceiv->otg->state));
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun switch (musb->xceiv->otg->state) {
229*4882a593Smuzhiyun case OTG_STATE_A_WAIT_VRISE:
230*4882a593Smuzhiyun if (musb->port_mode == MUSB_HOST) {
231*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
232*4882a593Smuzhiyun dsps_mod_timer_optional(glue);
233*4882a593Smuzhiyun break;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun fallthrough;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun case OTG_STATE_A_WAIT_BCON:
238*4882a593Smuzhiyun /* keep VBUS on for host-only mode */
239*4882a593Smuzhiyun if (musb->port_mode == MUSB_HOST) {
240*4882a593Smuzhiyun dsps_mod_timer_optional(glue);
241*4882a593Smuzhiyun break;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
244*4882a593Smuzhiyun skip_session = 1;
245*4882a593Smuzhiyun fallthrough;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun case OTG_STATE_A_IDLE:
248*4882a593Smuzhiyun case OTG_STATE_B_IDLE:
249*4882a593Smuzhiyun if (!glue->vbus_irq) {
250*4882a593Smuzhiyun if (devctl & MUSB_DEVCTL_BDEVICE) {
251*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_B_IDLE;
252*4882a593Smuzhiyun MUSB_DEV_MODE(musb);
253*4882a593Smuzhiyun } else {
254*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_A_IDLE;
255*4882a593Smuzhiyun MUSB_HST_MODE(musb);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun if (musb->port_mode == MUSB_PERIPHERAL)
259*4882a593Smuzhiyun skip_session = 1;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session)
262*4882a593Smuzhiyun musb_writeb(mregs, MUSB_DEVCTL,
263*4882a593Smuzhiyun MUSB_DEVCTL_SESSION);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun dsps_mod_timer_optional(glue);
266*4882a593Smuzhiyun break;
267*4882a593Smuzhiyun case OTG_STATE_A_WAIT_VFALL:
268*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
269*4882a593Smuzhiyun musb_writel(musb->ctrl_base, wrp->coreintr_set,
270*4882a593Smuzhiyun MUSB_INTR_VBUSERROR << wrp->usb_shift);
271*4882a593Smuzhiyun break;
272*4882a593Smuzhiyun default:
273*4882a593Smuzhiyun break;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun return 0;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
otg_timer(struct timer_list * t)279*4882a593Smuzhiyun static void otg_timer(struct timer_list *t)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct musb *musb = from_timer(musb, t, dev_timer);
282*4882a593Smuzhiyun struct device *dev = musb->controller;
283*4882a593Smuzhiyun unsigned long flags;
284*4882a593Smuzhiyun int err;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun err = pm_runtime_get(dev);
287*4882a593Smuzhiyun if ((err != -EINPROGRESS) && err < 0) {
288*4882a593Smuzhiyun dev_err(dev, "Poll could not pm_runtime_get: %i\n", err);
289*4882a593Smuzhiyun pm_runtime_put_noidle(dev);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun return;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun spin_lock_irqsave(&musb->lock, flags);
295*4882a593Smuzhiyun err = musb_queue_resume_work(musb, dsps_check_status, NULL);
296*4882a593Smuzhiyun if (err < 0)
297*4882a593Smuzhiyun dev_err(dev, "%s resume work: %i\n", __func__, err);
298*4882a593Smuzhiyun spin_unlock_irqrestore(&musb->lock, flags);
299*4882a593Smuzhiyun pm_runtime_mark_last_busy(dev);
300*4882a593Smuzhiyun pm_runtime_put_autosuspend(dev);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
dsps_musb_clear_ep_rxintr(struct musb * musb,int epnum)303*4882a593Smuzhiyun static void dsps_musb_clear_ep_rxintr(struct musb *musb, int epnum)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun u32 epintr;
306*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(musb->controller->parent);
307*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* musb->lock might already been held */
310*4882a593Smuzhiyun epintr = (1 << epnum) << wrp->rxep_shift;
311*4882a593Smuzhiyun musb_writel(musb->ctrl_base, wrp->epintr_status, epintr);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
dsps_interrupt(int irq,void * hci)314*4882a593Smuzhiyun static irqreturn_t dsps_interrupt(int irq, void *hci)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun struct musb *musb = hci;
317*4882a593Smuzhiyun void __iomem *reg_base = musb->ctrl_base;
318*4882a593Smuzhiyun struct device *dev = musb->controller;
319*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
320*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
321*4882a593Smuzhiyun unsigned long flags;
322*4882a593Smuzhiyun irqreturn_t ret = IRQ_NONE;
323*4882a593Smuzhiyun u32 epintr, usbintr;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun spin_lock_irqsave(&musb->lock, flags);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun /* Get endpoint interrupts */
328*4882a593Smuzhiyun epintr = musb_readl(reg_base, wrp->epintr_status);
329*4882a593Smuzhiyun musb->int_rx = (epintr & wrp->rxep_bitmap) >> wrp->rxep_shift;
330*4882a593Smuzhiyun musb->int_tx = (epintr & wrp->txep_bitmap) >> wrp->txep_shift;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (epintr)
333*4882a593Smuzhiyun musb_writel(reg_base, wrp->epintr_status, epintr);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* Get usb core interrupts */
336*4882a593Smuzhiyun usbintr = musb_readl(reg_base, wrp->coreintr_status);
337*4882a593Smuzhiyun if (!usbintr && !epintr)
338*4882a593Smuzhiyun goto out;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun musb->int_usb = (usbintr & wrp->usb_bitmap) >> wrp->usb_shift;
341*4882a593Smuzhiyun if (usbintr)
342*4882a593Smuzhiyun musb_writel(reg_base, wrp->coreintr_status, usbintr);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n",
345*4882a593Smuzhiyun usbintr, epintr);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) {
348*4882a593Smuzhiyun int drvvbus = musb_readl(reg_base, wrp->status);
349*4882a593Smuzhiyun void __iomem *mregs = musb->mregs;
350*4882a593Smuzhiyun u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
351*4882a593Smuzhiyun int err;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun err = musb->int_usb & MUSB_INTR_VBUSERROR;
354*4882a593Smuzhiyun if (err) {
355*4882a593Smuzhiyun /*
356*4882a593Smuzhiyun * The Mentor core doesn't debounce VBUS as needed
357*4882a593Smuzhiyun * to cope with device connect current spikes. This
358*4882a593Smuzhiyun * means it's not uncommon for bus-powered devices
359*4882a593Smuzhiyun * to get VBUS errors during enumeration.
360*4882a593Smuzhiyun *
361*4882a593Smuzhiyun * This is a workaround, but newer RTL from Mentor
362*4882a593Smuzhiyun * seems to allow a better one: "re"-starting sessions
363*4882a593Smuzhiyun * without waiting for VBUS to stop registering in
364*4882a593Smuzhiyun * devctl.
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun musb->int_usb &= ~MUSB_INTR_VBUSERROR;
367*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
368*4882a593Smuzhiyun dsps_mod_timer_optional(glue);
369*4882a593Smuzhiyun WARNING("VBUS error workaround (delay coming)\n");
370*4882a593Smuzhiyun } else if (drvvbus) {
371*4882a593Smuzhiyun MUSB_HST_MODE(musb);
372*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
373*4882a593Smuzhiyun dsps_mod_timer_optional(glue);
374*4882a593Smuzhiyun } else {
375*4882a593Smuzhiyun musb->is_active = 0;
376*4882a593Smuzhiyun MUSB_DEV_MODE(musb);
377*4882a593Smuzhiyun musb->xceiv->otg->state = OTG_STATE_B_IDLE;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /* NOTE: this must complete power-on within 100 ms. */
381*4882a593Smuzhiyun dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n",
382*4882a593Smuzhiyun drvvbus ? "on" : "off",
383*4882a593Smuzhiyun usb_otg_state_string(musb->xceiv->otg->state),
384*4882a593Smuzhiyun err ? " ERROR" : "",
385*4882a593Smuzhiyun devctl);
386*4882a593Smuzhiyun ret = IRQ_HANDLED;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun if (musb->int_tx || musb->int_rx || musb->int_usb)
390*4882a593Smuzhiyun ret |= musb_interrupt(musb);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /* Poll for ID change and connect */
393*4882a593Smuzhiyun switch (musb->xceiv->otg->state) {
394*4882a593Smuzhiyun case OTG_STATE_B_IDLE:
395*4882a593Smuzhiyun case OTG_STATE_A_WAIT_BCON:
396*4882a593Smuzhiyun dsps_mod_timer_optional(glue);
397*4882a593Smuzhiyun break;
398*4882a593Smuzhiyun default:
399*4882a593Smuzhiyun break;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun out:
403*4882a593Smuzhiyun spin_unlock_irqrestore(&musb->lock, flags);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun return ret;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
dsps_musb_dbg_init(struct musb * musb,struct dsps_glue * glue)408*4882a593Smuzhiyun static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun struct dentry *root;
411*4882a593Smuzhiyun char buf[128];
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun sprintf(buf, "%s.dsps", dev_name(musb->controller));
414*4882a593Smuzhiyun root = debugfs_create_dir(buf, usb_debug_root);
415*4882a593Smuzhiyun glue->dbgfs_root = root;
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun glue->regset.regs = dsps_musb_regs;
418*4882a593Smuzhiyun glue->regset.nregs = ARRAY_SIZE(dsps_musb_regs);
419*4882a593Smuzhiyun glue->regset.base = musb->ctrl_base;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset);
422*4882a593Smuzhiyun return 0;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun
dsps_musb_init(struct musb * musb)425*4882a593Smuzhiyun static int dsps_musb_init(struct musb *musb)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun struct device *dev = musb->controller;
428*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
429*4882a593Smuzhiyun struct platform_device *parent = to_platform_device(dev->parent);
430*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
431*4882a593Smuzhiyun void __iomem *reg_base;
432*4882a593Smuzhiyun struct resource *r;
433*4882a593Smuzhiyun u32 rev, val;
434*4882a593Smuzhiyun int ret;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control");
437*4882a593Smuzhiyun reg_base = devm_ioremap_resource(dev, r);
438*4882a593Smuzhiyun if (IS_ERR(reg_base))
439*4882a593Smuzhiyun return PTR_ERR(reg_base);
440*4882a593Smuzhiyun musb->ctrl_base = reg_base;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /* NOP driver needs change if supporting dual instance */
443*4882a593Smuzhiyun musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, "phys", 0);
444*4882a593Smuzhiyun if (IS_ERR(musb->xceiv))
445*4882a593Smuzhiyun return PTR_ERR(musb->xceiv);
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun musb->phy = devm_phy_get(dev->parent, "usb2-phy");
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* Returns zero if e.g. not clocked */
450*4882a593Smuzhiyun rev = musb_readl(reg_base, wrp->revision);
451*4882a593Smuzhiyun if (!rev)
452*4882a593Smuzhiyun return -ENODEV;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun if (IS_ERR(musb->phy)) {
455*4882a593Smuzhiyun musb->phy = NULL;
456*4882a593Smuzhiyun } else {
457*4882a593Smuzhiyun ret = phy_init(musb->phy);
458*4882a593Smuzhiyun if (ret < 0)
459*4882a593Smuzhiyun return ret;
460*4882a593Smuzhiyun ret = phy_power_on(musb->phy);
461*4882a593Smuzhiyun if (ret) {
462*4882a593Smuzhiyun phy_exit(musb->phy);
463*4882a593Smuzhiyun return ret;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun timer_setup(&musb->dev_timer, otg_timer, 0);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /* Reset the musb */
470*4882a593Smuzhiyun musb_writel(reg_base, wrp->control, (1 << wrp->reset));
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun musb->isr = dsps_interrupt;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* reset the otgdisable bit, needed for host mode to work */
475*4882a593Smuzhiyun val = musb_readl(reg_base, wrp->phy_utmi);
476*4882a593Smuzhiyun val &= ~(1 << wrp->otg_disable);
477*4882a593Smuzhiyun musb_writel(musb->ctrl_base, wrp->phy_utmi, val);
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun * Check whether the dsps version has babble control enabled.
481*4882a593Smuzhiyun * In latest silicon revision the babble control logic is enabled.
482*4882a593Smuzhiyun * If MUSB_BABBLE_CTL returns 0x4 then we have the babble control
483*4882a593Smuzhiyun * logic enabled.
484*4882a593Smuzhiyun */
485*4882a593Smuzhiyun val = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
486*4882a593Smuzhiyun if (val & MUSB_BABBLE_RCV_DISABLE) {
487*4882a593Smuzhiyun glue->sw_babble_enabled = true;
488*4882a593Smuzhiyun val |= MUSB_BABBLE_SW_SESSION_CTRL;
489*4882a593Smuzhiyun musb_writeb(musb->mregs, MUSB_BABBLE_CTL, val);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun dsps_mod_timer(glue, -1);
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun return dsps_musb_dbg_init(musb, glue);
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
dsps_musb_exit(struct musb * musb)497*4882a593Smuzhiyun static int dsps_musb_exit(struct musb *musb)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun struct device *dev = musb->controller;
500*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun del_timer_sync(&musb->dev_timer);
503*4882a593Smuzhiyun phy_power_off(musb->phy);
504*4882a593Smuzhiyun phy_exit(musb->phy);
505*4882a593Smuzhiyun debugfs_remove_recursive(glue->dbgfs_root);
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun return 0;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
dsps_musb_set_mode(struct musb * musb,u8 mode)510*4882a593Smuzhiyun static int dsps_musb_set_mode(struct musb *musb, u8 mode)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun struct device *dev = musb->controller;
513*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
514*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
515*4882a593Smuzhiyun void __iomem *ctrl_base = musb->ctrl_base;
516*4882a593Smuzhiyun u32 reg;
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun reg = musb_readl(ctrl_base, wrp->mode);
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun switch (mode) {
521*4882a593Smuzhiyun case MUSB_HOST:
522*4882a593Smuzhiyun reg &= ~(1 << wrp->iddig);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /*
525*4882a593Smuzhiyun * if we're setting mode to host-only or device-only, we're
526*4882a593Smuzhiyun * going to ignore whatever the PHY sends us and just force
527*4882a593Smuzhiyun * ID pin status by SW
528*4882a593Smuzhiyun */
529*4882a593Smuzhiyun reg |= (1 << wrp->iddig_mux);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun musb_writel(ctrl_base, wrp->mode, reg);
532*4882a593Smuzhiyun musb_writel(ctrl_base, wrp->phy_utmi, 0x02);
533*4882a593Smuzhiyun break;
534*4882a593Smuzhiyun case MUSB_PERIPHERAL:
535*4882a593Smuzhiyun reg |= (1 << wrp->iddig);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /*
538*4882a593Smuzhiyun * if we're setting mode to host-only or device-only, we're
539*4882a593Smuzhiyun * going to ignore whatever the PHY sends us and just force
540*4882a593Smuzhiyun * ID pin status by SW
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun reg |= (1 << wrp->iddig_mux);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun musb_writel(ctrl_base, wrp->mode, reg);
545*4882a593Smuzhiyun break;
546*4882a593Smuzhiyun case MUSB_OTG:
547*4882a593Smuzhiyun musb_writel(ctrl_base, wrp->phy_utmi, 0x02);
548*4882a593Smuzhiyun break;
549*4882a593Smuzhiyun default:
550*4882a593Smuzhiyun dev_err(glue->dev, "unsupported mode %d\n", mode);
551*4882a593Smuzhiyun return -EINVAL;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun return 0;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
dsps_sw_babble_control(struct musb * musb)557*4882a593Smuzhiyun static bool dsps_sw_babble_control(struct musb *musb)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun u8 babble_ctl;
560*4882a593Smuzhiyun bool session_restart = false;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
563*4882a593Smuzhiyun dev_dbg(musb->controller, "babble: MUSB_BABBLE_CTL value %x\n",
564*4882a593Smuzhiyun babble_ctl);
565*4882a593Smuzhiyun /*
566*4882a593Smuzhiyun * check line monitor flag to check whether babble is
567*4882a593Smuzhiyun * due to noise
568*4882a593Smuzhiyun */
569*4882a593Smuzhiyun dev_dbg(musb->controller, "STUCK_J is %s\n",
570*4882a593Smuzhiyun babble_ctl & MUSB_BABBLE_STUCK_J ? "set" : "reset");
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun if (babble_ctl & MUSB_BABBLE_STUCK_J) {
573*4882a593Smuzhiyun int timeout = 10;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /*
576*4882a593Smuzhiyun * babble is due to noise, then set transmit idle (d7 bit)
577*4882a593Smuzhiyun * to resume normal operation
578*4882a593Smuzhiyun */
579*4882a593Smuzhiyun babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
580*4882a593Smuzhiyun babble_ctl |= MUSB_BABBLE_FORCE_TXIDLE;
581*4882a593Smuzhiyun musb_writeb(musb->mregs, MUSB_BABBLE_CTL, babble_ctl);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /* wait till line monitor flag cleared */
584*4882a593Smuzhiyun dev_dbg(musb->controller, "Set TXIDLE, wait J to clear\n");
585*4882a593Smuzhiyun do {
586*4882a593Smuzhiyun babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
587*4882a593Smuzhiyun udelay(1);
588*4882a593Smuzhiyun } while ((babble_ctl & MUSB_BABBLE_STUCK_J) && timeout--);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /* check whether stuck_at_j bit cleared */
591*4882a593Smuzhiyun if (babble_ctl & MUSB_BABBLE_STUCK_J) {
592*4882a593Smuzhiyun /*
593*4882a593Smuzhiyun * real babble condition has occurred
594*4882a593Smuzhiyun * restart the controller to start the
595*4882a593Smuzhiyun * session again
596*4882a593Smuzhiyun */
597*4882a593Smuzhiyun dev_dbg(musb->controller, "J not cleared, misc (%x)\n",
598*4882a593Smuzhiyun babble_ctl);
599*4882a593Smuzhiyun session_restart = true;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun } else {
602*4882a593Smuzhiyun session_restart = true;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun return session_restart;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
dsps_musb_recover(struct musb * musb)608*4882a593Smuzhiyun static int dsps_musb_recover(struct musb *musb)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun struct device *dev = musb->controller;
611*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev->parent);
612*4882a593Smuzhiyun int session_restart = 0;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun if (glue->sw_babble_enabled)
615*4882a593Smuzhiyun session_restart = dsps_sw_babble_control(musb);
616*4882a593Smuzhiyun else
617*4882a593Smuzhiyun session_restart = 1;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun return session_restart ? 0 : -EPIPE;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun /* Similar to am35x, dm81xx support only 32-bit read operation */
dsps_read_fifo32(struct musb_hw_ep * hw_ep,u16 len,u8 * dst)623*4882a593Smuzhiyun static void dsps_read_fifo32(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun void __iomem *fifo = hw_ep->fifo;
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun if (len >= 4) {
628*4882a593Smuzhiyun ioread32_rep(fifo, dst, len >> 2);
629*4882a593Smuzhiyun dst += len & ~0x03;
630*4882a593Smuzhiyun len &= 0x03;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /* Read any remaining 1 to 3 bytes */
634*4882a593Smuzhiyun if (len > 0) {
635*4882a593Smuzhiyun u32 val = musb_readl(fifo, 0);
636*4882a593Smuzhiyun memcpy(dst, &val, len);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun #ifdef CONFIG_USB_TI_CPPI41_DMA
dsps_dma_controller_callback(struct dma_controller * c)641*4882a593Smuzhiyun static void dsps_dma_controller_callback(struct dma_controller *c)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun struct musb *musb = c->musb;
644*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(musb->controller->parent);
645*4882a593Smuzhiyun void __iomem *usbss_base = glue->usbss_base;
646*4882a593Smuzhiyun u32 status;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun status = musb_readl(usbss_base, USBSS_IRQ_STATUS);
649*4882a593Smuzhiyun if (status & USBSS_IRQ_PD_COMP)
650*4882a593Smuzhiyun musb_writel(usbss_base, USBSS_IRQ_STATUS, USBSS_IRQ_PD_COMP);
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun static struct dma_controller *
dsps_dma_controller_create(struct musb * musb,void __iomem * base)654*4882a593Smuzhiyun dsps_dma_controller_create(struct musb *musb, void __iomem *base)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun struct dma_controller *controller;
657*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(musb->controller->parent);
658*4882a593Smuzhiyun void __iomem *usbss_base = glue->usbss_base;
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun controller = cppi41_dma_controller_create(musb, base);
661*4882a593Smuzhiyun if (IS_ERR_OR_NULL(controller))
662*4882a593Smuzhiyun return controller;
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun musb_writel(usbss_base, USBSS_IRQ_ENABLER, USBSS_IRQ_PD_COMP);
665*4882a593Smuzhiyun controller->dma_callback = dsps_dma_controller_callback;
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun return controller;
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
dsps_dma_controller_suspend(struct dsps_glue * glue)671*4882a593Smuzhiyun static void dsps_dma_controller_suspend(struct dsps_glue *glue)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun void __iomem *usbss_base = glue->usbss_base;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun musb_writel(usbss_base, USBSS_IRQ_CLEARR, USBSS_IRQ_PD_COMP);
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun
dsps_dma_controller_resume(struct dsps_glue * glue)678*4882a593Smuzhiyun static void dsps_dma_controller_resume(struct dsps_glue *glue)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun void __iomem *usbss_base = glue->usbss_base;
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun musb_writel(usbss_base, USBSS_IRQ_ENABLER, USBSS_IRQ_PD_COMP);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun #endif
685*4882a593Smuzhiyun #else /* CONFIG_USB_TI_CPPI41_DMA */
686*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
dsps_dma_controller_suspend(struct dsps_glue * glue)687*4882a593Smuzhiyun static void dsps_dma_controller_suspend(struct dsps_glue *glue) {}
dsps_dma_controller_resume(struct dsps_glue * glue)688*4882a593Smuzhiyun static void dsps_dma_controller_resume(struct dsps_glue *glue) {}
689*4882a593Smuzhiyun #endif
690*4882a593Smuzhiyun #endif /* CONFIG_USB_TI_CPPI41_DMA */
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun static struct musb_platform_ops dsps_ops = {
693*4882a593Smuzhiyun .quirks = MUSB_DMA_CPPI41 | MUSB_INDEXED_EP,
694*4882a593Smuzhiyun .init = dsps_musb_init,
695*4882a593Smuzhiyun .exit = dsps_musb_exit,
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun #ifdef CONFIG_USB_TI_CPPI41_DMA
698*4882a593Smuzhiyun .dma_init = dsps_dma_controller_create,
699*4882a593Smuzhiyun .dma_exit = cppi41_dma_controller_destroy,
700*4882a593Smuzhiyun #endif
701*4882a593Smuzhiyun .enable = dsps_musb_enable,
702*4882a593Smuzhiyun .disable = dsps_musb_disable,
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun .set_mode = dsps_musb_set_mode,
705*4882a593Smuzhiyun .recover = dsps_musb_recover,
706*4882a593Smuzhiyun .clear_ep_rxintr = dsps_musb_clear_ep_rxintr,
707*4882a593Smuzhiyun };
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun static u64 musb_dmamask = DMA_BIT_MASK(32);
710*4882a593Smuzhiyun
get_int_prop(struct device_node * dn,const char * s)711*4882a593Smuzhiyun static int get_int_prop(struct device_node *dn, const char *s)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun int ret;
714*4882a593Smuzhiyun u32 val;
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun ret = of_property_read_u32(dn, s, &val);
717*4882a593Smuzhiyun if (ret)
718*4882a593Smuzhiyun return 0;
719*4882a593Smuzhiyun return val;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
dsps_create_musb_pdev(struct dsps_glue * glue,struct platform_device * parent)722*4882a593Smuzhiyun static int dsps_create_musb_pdev(struct dsps_glue *glue,
723*4882a593Smuzhiyun struct platform_device *parent)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun struct musb_hdrc_platform_data pdata;
726*4882a593Smuzhiyun struct resource resources[2];
727*4882a593Smuzhiyun struct resource *res;
728*4882a593Smuzhiyun struct device *dev = &parent->dev;
729*4882a593Smuzhiyun struct musb_hdrc_config *config;
730*4882a593Smuzhiyun struct platform_device *musb;
731*4882a593Smuzhiyun struct device_node *dn = parent->dev.of_node;
732*4882a593Smuzhiyun int ret, val;
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun memset(resources, 0, sizeof(resources));
735*4882a593Smuzhiyun res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc");
736*4882a593Smuzhiyun if (!res) {
737*4882a593Smuzhiyun dev_err(dev, "failed to get memory.\n");
738*4882a593Smuzhiyun return -EINVAL;
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun resources[0] = *res;
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc");
743*4882a593Smuzhiyun if (!res) {
744*4882a593Smuzhiyun dev_err(dev, "failed to get irq.\n");
745*4882a593Smuzhiyun return -EINVAL;
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun resources[1] = *res;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /* allocate the child platform device */
750*4882a593Smuzhiyun musb = platform_device_alloc("musb-hdrc",
751*4882a593Smuzhiyun (resources[0].start & 0xFFF) == 0x400 ? 0 : 1);
752*4882a593Smuzhiyun if (!musb) {
753*4882a593Smuzhiyun dev_err(dev, "failed to allocate musb device\n");
754*4882a593Smuzhiyun return -ENOMEM;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun musb->dev.parent = dev;
758*4882a593Smuzhiyun musb->dev.dma_mask = &musb_dmamask;
759*4882a593Smuzhiyun musb->dev.coherent_dma_mask = musb_dmamask;
760*4882a593Smuzhiyun device_set_of_node_from_dev(&musb->dev, &parent->dev);
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun glue->musb = musb;
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun ret = platform_device_add_resources(musb, resources,
765*4882a593Smuzhiyun ARRAY_SIZE(resources));
766*4882a593Smuzhiyun if (ret) {
767*4882a593Smuzhiyun dev_err(dev, "failed to add resources\n");
768*4882a593Smuzhiyun goto err;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL);
772*4882a593Smuzhiyun if (!config) {
773*4882a593Smuzhiyun ret = -ENOMEM;
774*4882a593Smuzhiyun goto err;
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun pdata.config = config;
777*4882a593Smuzhiyun pdata.platform_ops = &dsps_ops;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun config->num_eps = get_int_prop(dn, "mentor,num-eps");
780*4882a593Smuzhiyun config->ram_bits = get_int_prop(dn, "mentor,ram-bits");
781*4882a593Smuzhiyun config->host_port_deassert_reset_at_resume = 1;
782*4882a593Smuzhiyun pdata.mode = musb_get_mode(dev);
783*4882a593Smuzhiyun /* DT keeps this entry in mA, musb expects it as per USB spec */
784*4882a593Smuzhiyun pdata.power = get_int_prop(dn, "mentor,power") / 2;
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun ret = of_property_read_u32(dn, "mentor,multipoint", &val);
787*4882a593Smuzhiyun if (!ret && val)
788*4882a593Smuzhiyun config->multipoint = true;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun config->maximum_speed = usb_get_maximum_speed(&parent->dev);
791*4882a593Smuzhiyun switch (config->maximum_speed) {
792*4882a593Smuzhiyun case USB_SPEED_LOW:
793*4882a593Smuzhiyun case USB_SPEED_FULL:
794*4882a593Smuzhiyun break;
795*4882a593Smuzhiyun case USB_SPEED_SUPER:
796*4882a593Smuzhiyun dev_warn(dev, "ignore incorrect maximum_speed "
797*4882a593Smuzhiyun "(super-speed) setting in dts");
798*4882a593Smuzhiyun fallthrough;
799*4882a593Smuzhiyun default:
800*4882a593Smuzhiyun config->maximum_speed = USB_SPEED_HIGH;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun ret = platform_device_add_data(musb, &pdata, sizeof(pdata));
804*4882a593Smuzhiyun if (ret) {
805*4882a593Smuzhiyun dev_err(dev, "failed to add platform_data\n");
806*4882a593Smuzhiyun goto err;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun ret = platform_device_add(musb);
810*4882a593Smuzhiyun if (ret) {
811*4882a593Smuzhiyun dev_err(dev, "failed to register musb device\n");
812*4882a593Smuzhiyun goto err;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun return 0;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun err:
817*4882a593Smuzhiyun platform_device_put(musb);
818*4882a593Smuzhiyun return ret;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun
dsps_vbus_threaded_irq(int irq,void * priv)821*4882a593Smuzhiyun static irqreturn_t dsps_vbus_threaded_irq(int irq, void *priv)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun struct dsps_glue *glue = priv;
824*4882a593Smuzhiyun struct musb *musb = platform_get_drvdata(glue->musb);
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun if (!musb)
827*4882a593Smuzhiyun return IRQ_NONE;
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun dev_dbg(glue->dev, "VBUS interrupt\n");
830*4882a593Smuzhiyun dsps_mod_timer(glue, 0);
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun return IRQ_HANDLED;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun
dsps_setup_optional_vbus_irq(struct platform_device * pdev,struct dsps_glue * glue)835*4882a593Smuzhiyun static int dsps_setup_optional_vbus_irq(struct platform_device *pdev,
836*4882a593Smuzhiyun struct dsps_glue *glue)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun int error;
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun glue->vbus_irq = platform_get_irq_byname(pdev, "vbus");
841*4882a593Smuzhiyun if (glue->vbus_irq == -EPROBE_DEFER)
842*4882a593Smuzhiyun return -EPROBE_DEFER;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun if (glue->vbus_irq <= 0) {
845*4882a593Smuzhiyun glue->vbus_irq = 0;
846*4882a593Smuzhiyun return 0;
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun error = devm_request_threaded_irq(glue->dev, glue->vbus_irq,
850*4882a593Smuzhiyun NULL, dsps_vbus_threaded_irq,
851*4882a593Smuzhiyun IRQF_ONESHOT,
852*4882a593Smuzhiyun "vbus", glue);
853*4882a593Smuzhiyun if (error) {
854*4882a593Smuzhiyun glue->vbus_irq = 0;
855*4882a593Smuzhiyun return error;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun dev_dbg(glue->dev, "VBUS irq %i configured\n", glue->vbus_irq);
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun return 0;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun
dsps_probe(struct platform_device * pdev)862*4882a593Smuzhiyun static int dsps_probe(struct platform_device *pdev)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun const struct of_device_id *match;
865*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp;
866*4882a593Smuzhiyun struct dsps_glue *glue;
867*4882a593Smuzhiyun int ret;
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun if (!strcmp(pdev->name, "musb-hdrc"))
870*4882a593Smuzhiyun return -ENODEV;
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun match = of_match_node(musb_dsps_of_match, pdev->dev.of_node);
873*4882a593Smuzhiyun if (!match) {
874*4882a593Smuzhiyun dev_err(&pdev->dev, "fail to get matching of_match struct\n");
875*4882a593Smuzhiyun return -EINVAL;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun wrp = match->data;
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun if (of_device_is_compatible(pdev->dev.of_node, "ti,musb-dm816"))
880*4882a593Smuzhiyun dsps_ops.read_fifo = dsps_read_fifo32;
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun /* allocate glue */
883*4882a593Smuzhiyun glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
884*4882a593Smuzhiyun if (!glue)
885*4882a593Smuzhiyun return -ENOMEM;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun glue->dev = &pdev->dev;
888*4882a593Smuzhiyun glue->wrp = wrp;
889*4882a593Smuzhiyun glue->usbss_base = of_iomap(pdev->dev.parent->of_node, 0);
890*4882a593Smuzhiyun if (!glue->usbss_base)
891*4882a593Smuzhiyun return -ENXIO;
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun platform_set_drvdata(pdev, glue);
894*4882a593Smuzhiyun pm_runtime_enable(&pdev->dev);
895*4882a593Smuzhiyun ret = dsps_create_musb_pdev(glue, pdev);
896*4882a593Smuzhiyun if (ret)
897*4882a593Smuzhiyun goto err;
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun if (usb_get_dr_mode(&pdev->dev) == USB_DR_MODE_PERIPHERAL) {
900*4882a593Smuzhiyun ret = dsps_setup_optional_vbus_irq(pdev, glue);
901*4882a593Smuzhiyun if (ret)
902*4882a593Smuzhiyun goto unregister_pdev;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun return 0;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun unregister_pdev:
908*4882a593Smuzhiyun platform_device_unregister(glue->musb);
909*4882a593Smuzhiyun err:
910*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
911*4882a593Smuzhiyun iounmap(glue->usbss_base);
912*4882a593Smuzhiyun return ret;
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
dsps_remove(struct platform_device * pdev)915*4882a593Smuzhiyun static int dsps_remove(struct platform_device *pdev)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun struct dsps_glue *glue = platform_get_drvdata(pdev);
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun platform_device_unregister(glue->musb);
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
922*4882a593Smuzhiyun iounmap(glue->usbss_base);
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun return 0;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun static const struct dsps_musb_wrapper am33xx_driver_data = {
928*4882a593Smuzhiyun .revision = 0x00,
929*4882a593Smuzhiyun .control = 0x14,
930*4882a593Smuzhiyun .status = 0x18,
931*4882a593Smuzhiyun .epintr_set = 0x38,
932*4882a593Smuzhiyun .epintr_clear = 0x40,
933*4882a593Smuzhiyun .epintr_status = 0x30,
934*4882a593Smuzhiyun .coreintr_set = 0x3c,
935*4882a593Smuzhiyun .coreintr_clear = 0x44,
936*4882a593Smuzhiyun .coreintr_status = 0x34,
937*4882a593Smuzhiyun .phy_utmi = 0xe0,
938*4882a593Smuzhiyun .mode = 0xe8,
939*4882a593Smuzhiyun .tx_mode = 0x70,
940*4882a593Smuzhiyun .rx_mode = 0x74,
941*4882a593Smuzhiyun .reset = 0,
942*4882a593Smuzhiyun .otg_disable = 21,
943*4882a593Smuzhiyun .iddig = 8,
944*4882a593Smuzhiyun .iddig_mux = 7,
945*4882a593Smuzhiyun .usb_shift = 0,
946*4882a593Smuzhiyun .usb_mask = 0x1ff,
947*4882a593Smuzhiyun .usb_bitmap = (0x1ff << 0),
948*4882a593Smuzhiyun .drvvbus = 8,
949*4882a593Smuzhiyun .txep_shift = 0,
950*4882a593Smuzhiyun .txep_mask = 0xffff,
951*4882a593Smuzhiyun .txep_bitmap = (0xffff << 0),
952*4882a593Smuzhiyun .rxep_shift = 16,
953*4882a593Smuzhiyun .rxep_mask = 0xfffe,
954*4882a593Smuzhiyun .rxep_bitmap = (0xfffe << 16),
955*4882a593Smuzhiyun .poll_timeout = 2000, /* ms */
956*4882a593Smuzhiyun };
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun static const struct of_device_id musb_dsps_of_match[] = {
959*4882a593Smuzhiyun { .compatible = "ti,musb-am33xx",
960*4882a593Smuzhiyun .data = &am33xx_driver_data, },
961*4882a593Smuzhiyun { .compatible = "ti,musb-dm816",
962*4882a593Smuzhiyun .data = &am33xx_driver_data, },
963*4882a593Smuzhiyun { },
964*4882a593Smuzhiyun };
965*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
966*4882a593Smuzhiyun
967*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
dsps_suspend(struct device * dev)968*4882a593Smuzhiyun static int dsps_suspend(struct device *dev)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev);
971*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
972*4882a593Smuzhiyun struct musb *musb = platform_get_drvdata(glue->musb);
973*4882a593Smuzhiyun void __iomem *mbase;
974*4882a593Smuzhiyun int ret;
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun if (!musb)
977*4882a593Smuzhiyun /* This can happen if the musb device is in -EPROBE_DEFER */
978*4882a593Smuzhiyun return 0;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun ret = pm_runtime_get_sync(dev);
981*4882a593Smuzhiyun if (ret < 0) {
982*4882a593Smuzhiyun pm_runtime_put_noidle(dev);
983*4882a593Smuzhiyun return ret;
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun del_timer_sync(&musb->dev_timer);
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun mbase = musb->ctrl_base;
989*4882a593Smuzhiyun glue->context.control = musb_readl(mbase, wrp->control);
990*4882a593Smuzhiyun glue->context.epintr = musb_readl(mbase, wrp->epintr_set);
991*4882a593Smuzhiyun glue->context.coreintr = musb_readl(mbase, wrp->coreintr_set);
992*4882a593Smuzhiyun glue->context.phy_utmi = musb_readl(mbase, wrp->phy_utmi);
993*4882a593Smuzhiyun glue->context.mode = musb_readl(mbase, wrp->mode);
994*4882a593Smuzhiyun glue->context.tx_mode = musb_readl(mbase, wrp->tx_mode);
995*4882a593Smuzhiyun glue->context.rx_mode = musb_readl(mbase, wrp->rx_mode);
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun dsps_dma_controller_suspend(glue);
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun return 0;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun
dsps_resume(struct device * dev)1002*4882a593Smuzhiyun static int dsps_resume(struct device *dev)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun struct dsps_glue *glue = dev_get_drvdata(dev);
1005*4882a593Smuzhiyun const struct dsps_musb_wrapper *wrp = glue->wrp;
1006*4882a593Smuzhiyun struct musb *musb = platform_get_drvdata(glue->musb);
1007*4882a593Smuzhiyun void __iomem *mbase;
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun if (!musb)
1010*4882a593Smuzhiyun return 0;
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun dsps_dma_controller_resume(glue);
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun mbase = musb->ctrl_base;
1015*4882a593Smuzhiyun musb_writel(mbase, wrp->control, glue->context.control);
1016*4882a593Smuzhiyun musb_writel(mbase, wrp->epintr_set, glue->context.epintr);
1017*4882a593Smuzhiyun musb_writel(mbase, wrp->coreintr_set, glue->context.coreintr);
1018*4882a593Smuzhiyun musb_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi);
1019*4882a593Smuzhiyun musb_writel(mbase, wrp->mode, glue->context.mode);
1020*4882a593Smuzhiyun musb_writel(mbase, wrp->tx_mode, glue->context.tx_mode);
1021*4882a593Smuzhiyun musb_writel(mbase, wrp->rx_mode, glue->context.rx_mode);
1022*4882a593Smuzhiyun if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
1023*4882a593Smuzhiyun musb->port_mode == MUSB_OTG)
1024*4882a593Smuzhiyun dsps_mod_timer(glue, -1);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun pm_runtime_put(dev);
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun return 0;
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun #endif
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume);
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun static struct platform_driver dsps_usbss_driver = {
1035*4882a593Smuzhiyun .probe = dsps_probe,
1036*4882a593Smuzhiyun .remove = dsps_remove,
1037*4882a593Smuzhiyun .driver = {
1038*4882a593Smuzhiyun .name = "musb-dsps",
1039*4882a593Smuzhiyun .pm = &dsps_pm_ops,
1040*4882a593Smuzhiyun .of_match_table = musb_dsps_of_match,
1041*4882a593Smuzhiyun },
1042*4882a593Smuzhiyun };
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer");
1045*4882a593Smuzhiyun MODULE_AUTHOR("Ravi B <ravibabu@ti.com>");
1046*4882a593Smuzhiyun MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>");
1047*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun module_platform_driver(dsps_usbss_driver);
1050