19ecb8875SAjay Kumar Gupta /* 29ecb8875SAjay Kumar Gupta * Texas Instruments DSPS platforms "glue layer" 39ecb8875SAjay Kumar Gupta * 49ecb8875SAjay Kumar Gupta * Copyright (C) 2012, by Texas Instruments 59ecb8875SAjay Kumar Gupta * 69ecb8875SAjay Kumar Gupta * Based on the am35x "glue layer" code. 79ecb8875SAjay Kumar Gupta * 89ecb8875SAjay Kumar Gupta * This file is part of the Inventra Controller Driver for Linux. 99ecb8875SAjay Kumar Gupta * 109ecb8875SAjay Kumar Gupta * The Inventra Controller Driver for Linux is free software; you 119ecb8875SAjay Kumar Gupta * can redistribute it and/or modify it under the terms of the GNU 129ecb8875SAjay Kumar Gupta * General Public License version 2 as published by the Free Software 139ecb8875SAjay Kumar Gupta * Foundation. 149ecb8875SAjay Kumar Gupta * 159ecb8875SAjay Kumar Gupta * The Inventra Controller Driver for Linux is distributed in 169ecb8875SAjay Kumar Gupta * the hope that it will be useful, but WITHOUT ANY WARRANTY; 179ecb8875SAjay Kumar Gupta * without even the implied warranty of MERCHANTABILITY or 189ecb8875SAjay Kumar Gupta * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 199ecb8875SAjay Kumar Gupta * License for more details. 209ecb8875SAjay Kumar Gupta * 219ecb8875SAjay Kumar Gupta * You should have received a copy of the GNU General Public License 229ecb8875SAjay Kumar Gupta * along with The Inventra Controller Driver for Linux ; if not, 239ecb8875SAjay Kumar Gupta * write to the Free Software Foundation, Inc., 59 Temple Place, 249ecb8875SAjay Kumar Gupta * Suite 330, Boston, MA 02111-1307 USA 259ecb8875SAjay Kumar Gupta * 269ecb8875SAjay Kumar Gupta * musb_dsps.c will be a common file for all the TI DSPS platforms 279ecb8875SAjay Kumar Gupta * such as dm64x, dm36x, dm35x, da8x, am35x and ti81x. 289ecb8875SAjay Kumar Gupta * For now only ti81x is using this and in future davinci.c, am35x.c 299ecb8875SAjay Kumar Gupta * da8xx.c would be merged to this file after testing. 309ecb8875SAjay Kumar Gupta */ 319ecb8875SAjay Kumar Gupta 329ecb8875SAjay Kumar Gupta #include <linux/io.h> 33ded017eeSKishon Vijay Abraham I #include <linux/err.h> 349ecb8875SAjay Kumar Gupta #include <linux/platform_device.h> 359ecb8875SAjay Kumar Gupta #include <linux/dma-mapping.h> 369ecb8875SAjay Kumar Gupta #include <linux/pm_runtime.h> 379ecb8875SAjay Kumar Gupta #include <linux/module.h> 38d7078df6SFelipe Balbi #include <linux/usb/usb_phy_generic.h> 39e8c4a7acSFelipe Balbi #include <linux/platform_data/usb-omap.h> 400f53e481SFelipe Balbi #include <linux/sizes.h> 419ecb8875SAjay Kumar Gupta 429ecb8875SAjay Kumar Gupta #include <linux/of.h> 439ecb8875SAjay Kumar Gupta #include <linux/of_device.h> 449ecb8875SAjay Kumar Gupta #include <linux/of_address.h> 4597238b35SSebastian Andrzej Siewior #include <linux/of_irq.h> 46c031a7d4SSebastian Andrzej Siewior #include <linux/usb/of.h> 479ecb8875SAjay Kumar Gupta 4840f099e3SMarkus Pargmann #include <linux/debugfs.h> 4940f099e3SMarkus Pargmann 509ecb8875SAjay Kumar Gupta #include "musb_core.h" 519ecb8875SAjay Kumar Gupta 5265145677SAjay Kumar Gupta static const struct of_device_id musb_dsps_of_match[]; 5365145677SAjay Kumar Gupta 549ecb8875SAjay Kumar Gupta /** 559ecb8875SAjay Kumar Gupta * avoid using musb_readx()/musb_writex() as glue layer should not be 569ecb8875SAjay Kumar Gupta * dependent on musb core layer symbols. 579ecb8875SAjay Kumar Gupta */ 589ecb8875SAjay Kumar Gupta static inline u8 dsps_readb(const void __iomem *addr, unsigned offset) 59fc7af215SLothar Waßmann { 60fc7af215SLothar Waßmann return __raw_readb(addr + offset); 61fc7af215SLothar Waßmann } 629ecb8875SAjay Kumar Gupta 639ecb8875SAjay Kumar Gupta static inline u32 dsps_readl(const void __iomem *addr, unsigned offset) 64fc7af215SLothar Waßmann { 65fc7af215SLothar Waßmann return __raw_readl(addr + offset); 66fc7af215SLothar Waßmann } 679ecb8875SAjay Kumar Gupta 689ecb8875SAjay Kumar Gupta static inline void dsps_writeb(void __iomem *addr, unsigned offset, u8 data) 69fc7af215SLothar Waßmann { 70fc7af215SLothar Waßmann __raw_writeb(data, addr + offset); 71fc7af215SLothar Waßmann } 729ecb8875SAjay Kumar Gupta 739ecb8875SAjay Kumar Gupta static inline void dsps_writel(void __iomem *addr, unsigned offset, u32 data) 74fc7af215SLothar Waßmann { 75fc7af215SLothar Waßmann __raw_writel(data, addr + offset); 76fc7af215SLothar Waßmann } 779ecb8875SAjay Kumar Gupta 789ecb8875SAjay Kumar Gupta /** 799ecb8875SAjay Kumar Gupta * DSPS musb wrapper register offset. 809ecb8875SAjay Kumar Gupta * FIXME: This should be expanded to have all the wrapper registers from TI DSPS 819ecb8875SAjay Kumar Gupta * musb ips. 829ecb8875SAjay Kumar Gupta */ 839ecb8875SAjay Kumar Gupta struct dsps_musb_wrapper { 849ecb8875SAjay Kumar Gupta u16 revision; 859ecb8875SAjay Kumar Gupta u16 control; 869ecb8875SAjay Kumar Gupta u16 status; 879ecb8875SAjay Kumar Gupta u16 epintr_set; 889ecb8875SAjay Kumar Gupta u16 epintr_clear; 899ecb8875SAjay Kumar Gupta u16 epintr_status; 909ecb8875SAjay Kumar Gupta u16 coreintr_set; 919ecb8875SAjay Kumar Gupta u16 coreintr_clear; 929ecb8875SAjay Kumar Gupta u16 coreintr_status; 939ecb8875SAjay Kumar Gupta u16 phy_utmi; 949ecb8875SAjay Kumar Gupta u16 mode; 95b991f9b7SDaniel Mack u16 tx_mode; 96b991f9b7SDaniel Mack u16 rx_mode; 979ecb8875SAjay Kumar Gupta 989ecb8875SAjay Kumar Gupta /* bit positions for control */ 999ecb8875SAjay Kumar Gupta unsigned reset:5; 1009ecb8875SAjay Kumar Gupta 1019ecb8875SAjay Kumar Gupta /* bit positions for interrupt */ 1029ecb8875SAjay Kumar Gupta unsigned usb_shift:5; 1039ecb8875SAjay Kumar Gupta u32 usb_mask; 1049ecb8875SAjay Kumar Gupta u32 usb_bitmap; 1059ecb8875SAjay Kumar Gupta unsigned drvvbus:5; 1069ecb8875SAjay Kumar Gupta 1079ecb8875SAjay Kumar Gupta unsigned txep_shift:5; 1089ecb8875SAjay Kumar Gupta u32 txep_mask; 1099ecb8875SAjay Kumar Gupta u32 txep_bitmap; 1109ecb8875SAjay Kumar Gupta 1119ecb8875SAjay Kumar Gupta unsigned rxep_shift:5; 1129ecb8875SAjay Kumar Gupta u32 rxep_mask; 1139ecb8875SAjay Kumar Gupta u32 rxep_bitmap; 1149ecb8875SAjay Kumar Gupta 1159ecb8875SAjay Kumar Gupta /* bit positions for phy_utmi */ 1169ecb8875SAjay Kumar Gupta unsigned otg_disable:5; 1179ecb8875SAjay Kumar Gupta 1189ecb8875SAjay Kumar Gupta /* bit positions for mode */ 1199ecb8875SAjay Kumar Gupta unsigned iddig:5; 120943c1397SFelipe Balbi unsigned iddig_mux:5; 1219ecb8875SAjay Kumar Gupta /* miscellaneous stuff */ 1229ecb8875SAjay Kumar Gupta u8 poll_seconds; 1239ecb8875SAjay Kumar Gupta }; 1249ecb8875SAjay Kumar Gupta 125869c5978SDaniel Mack /* 126869c5978SDaniel Mack * register shadow for suspend 127869c5978SDaniel Mack */ 128869c5978SDaniel Mack struct dsps_context { 129869c5978SDaniel Mack u32 control; 130869c5978SDaniel Mack u32 epintr; 131869c5978SDaniel Mack u32 coreintr; 132869c5978SDaniel Mack u32 phy_utmi; 133869c5978SDaniel Mack u32 mode; 134869c5978SDaniel Mack u32 tx_mode; 135869c5978SDaniel Mack u32 rx_mode; 136869c5978SDaniel Mack }; 137869c5978SDaniel Mack 1389ecb8875SAjay Kumar Gupta /** 1399ecb8875SAjay Kumar Gupta * DSPS glue structure. 1409ecb8875SAjay Kumar Gupta */ 1419ecb8875SAjay Kumar Gupta struct dsps_glue { 1429ecb8875SAjay Kumar Gupta struct device *dev; 14397238b35SSebastian Andrzej Siewior struct platform_device *musb; /* child musb pdev */ 1449ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp; /* wrapper register offsets */ 14597238b35SSebastian Andrzej Siewior struct timer_list timer; /* otg_workaround timer */ 14697238b35SSebastian Andrzej Siewior unsigned long last_timer; /* last timer data for each instance */ 147371254ceSGeorge Cherian bool sw_babble_enabled; 148869c5978SDaniel Mack 149869c5978SDaniel Mack struct dsps_context context; 15040f099e3SMarkus Pargmann struct debugfs_regset32 regset; 15140f099e3SMarkus Pargmann struct dentry *dbgfs_root; 15240f099e3SMarkus Pargmann }; 15340f099e3SMarkus Pargmann 15440f099e3SMarkus Pargmann static const struct debugfs_reg32 dsps_musb_regs[] = { 15540f099e3SMarkus Pargmann { "revision", 0x00 }, 15640f099e3SMarkus Pargmann { "control", 0x14 }, 15740f099e3SMarkus Pargmann { "status", 0x18 }, 15840f099e3SMarkus Pargmann { "eoi", 0x24 }, 15940f099e3SMarkus Pargmann { "intr0_stat", 0x30 }, 16040f099e3SMarkus Pargmann { "intr1_stat", 0x34 }, 16140f099e3SMarkus Pargmann { "intr0_set", 0x38 }, 16240f099e3SMarkus Pargmann { "intr1_set", 0x3c }, 16340f099e3SMarkus Pargmann { "txmode", 0x70 }, 16440f099e3SMarkus Pargmann { "rxmode", 0x74 }, 16540f099e3SMarkus Pargmann { "autoreq", 0xd0 }, 16640f099e3SMarkus Pargmann { "srpfixtime", 0xd4 }, 16740f099e3SMarkus Pargmann { "tdown", 0xd8 }, 16840f099e3SMarkus Pargmann { "phy_utmi", 0xe0 }, 16940f099e3SMarkus Pargmann { "mode", 0xe8 }, 1709ecb8875SAjay Kumar Gupta }; 1719ecb8875SAjay Kumar Gupta 172807d0d2bSSebastian Andrzej Siewior static void dsps_musb_try_idle(struct musb *musb, unsigned long timeout) 173807d0d2bSSebastian Andrzej Siewior { 174807d0d2bSSebastian Andrzej Siewior struct device *dev = musb->controller; 175807d0d2bSSebastian Andrzej Siewior struct dsps_glue *glue = dev_get_drvdata(dev->parent); 176807d0d2bSSebastian Andrzej Siewior 177807d0d2bSSebastian Andrzej Siewior if (timeout == 0) 178807d0d2bSSebastian Andrzej Siewior timeout = jiffies + msecs_to_jiffies(3); 179807d0d2bSSebastian Andrzej Siewior 180807d0d2bSSebastian Andrzej Siewior /* Never idle if active, or when VBUS timeout is not set as host */ 181807d0d2bSSebastian Andrzej Siewior if (musb->is_active || (musb->a_wait_bcon == 0 && 182e47d9254SAntoine Tenart musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)) { 183807d0d2bSSebastian Andrzej Siewior dev_dbg(musb->controller, "%s active, deleting timer\n", 184e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 185807d0d2bSSebastian Andrzej Siewior del_timer(&glue->timer); 186807d0d2bSSebastian Andrzej Siewior glue->last_timer = jiffies; 187807d0d2bSSebastian Andrzej Siewior return; 188807d0d2bSSebastian Andrzej Siewior } 18924616eb6SSebastian Andrzej Siewior if (musb->port_mode != MUSB_PORT_MODE_DUAL_ROLE) 190807d0d2bSSebastian Andrzej Siewior return; 191807d0d2bSSebastian Andrzej Siewior 192807d0d2bSSebastian Andrzej Siewior if (!musb->g.dev.driver) 193807d0d2bSSebastian Andrzej Siewior return; 194807d0d2bSSebastian Andrzej Siewior 195807d0d2bSSebastian Andrzej Siewior if (time_after(glue->last_timer, timeout) && 196807d0d2bSSebastian Andrzej Siewior timer_pending(&glue->timer)) { 197807d0d2bSSebastian Andrzej Siewior dev_dbg(musb->controller, 198807d0d2bSSebastian Andrzej Siewior "Longer idle timer already pending, ignoring...\n"); 199807d0d2bSSebastian Andrzej Siewior return; 200807d0d2bSSebastian Andrzej Siewior } 201807d0d2bSSebastian Andrzej Siewior glue->last_timer = timeout; 202807d0d2bSSebastian Andrzej Siewior 203807d0d2bSSebastian Andrzej Siewior dev_dbg(musb->controller, "%s inactive, starting idle timer for %u ms\n", 204e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state), 205807d0d2bSSebastian Andrzej Siewior jiffies_to_msecs(timeout - jiffies)); 206807d0d2bSSebastian Andrzej Siewior mod_timer(&glue->timer, timeout); 207807d0d2bSSebastian Andrzej Siewior } 208807d0d2bSSebastian Andrzej Siewior 2099ecb8875SAjay Kumar Gupta /** 2109ecb8875SAjay Kumar Gupta * dsps_musb_enable - enable interrupts 2119ecb8875SAjay Kumar Gupta */ 2129ecb8875SAjay Kumar Gupta static void dsps_musb_enable(struct musb *musb) 2139ecb8875SAjay Kumar Gupta { 2149ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 2159ecb8875SAjay Kumar Gupta struct platform_device *pdev = to_platform_device(dev->parent); 2169ecb8875SAjay Kumar Gupta struct dsps_glue *glue = platform_get_drvdata(pdev); 2179ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 2189ecb8875SAjay Kumar Gupta void __iomem *reg_base = musb->ctrl_base; 2199ecb8875SAjay Kumar Gupta u32 epmask, coremask; 2209ecb8875SAjay Kumar Gupta 2219ecb8875SAjay Kumar Gupta /* Workaround: setup IRQs through both register sets. */ 2229ecb8875SAjay Kumar Gupta epmask = ((musb->epmask & wrp->txep_mask) << wrp->txep_shift) | 2239ecb8875SAjay Kumar Gupta ((musb->epmask & wrp->rxep_mask) << wrp->rxep_shift); 2249ecb8875SAjay Kumar Gupta coremask = (wrp->usb_bitmap & ~MUSB_INTR_SOF); 2259ecb8875SAjay Kumar Gupta 2269ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->epintr_set, epmask); 2279ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->coreintr_set, coremask); 2289ecb8875SAjay Kumar Gupta /* Force the DRVVBUS IRQ so we can start polling for ID change. */ 2299ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->coreintr_set, 2309ecb8875SAjay Kumar Gupta (1 << wrp->drvvbus) << wrp->usb_shift); 2318b9fcce2SSebastian Andrzej Siewior dsps_musb_try_idle(musb, 0); 2329ecb8875SAjay Kumar Gupta } 2339ecb8875SAjay Kumar Gupta 2349ecb8875SAjay Kumar Gupta /** 2359ecb8875SAjay Kumar Gupta * dsps_musb_disable - disable HDRC and flush interrupts 2369ecb8875SAjay Kumar Gupta */ 2379ecb8875SAjay Kumar Gupta static void dsps_musb_disable(struct musb *musb) 2389ecb8875SAjay Kumar Gupta { 2399ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 2409ecb8875SAjay Kumar Gupta struct platform_device *pdev = to_platform_device(dev->parent); 2419ecb8875SAjay Kumar Gupta struct dsps_glue *glue = platform_get_drvdata(pdev); 2429ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 2439ecb8875SAjay Kumar Gupta void __iomem *reg_base = musb->ctrl_base; 2449ecb8875SAjay Kumar Gupta 2459ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap); 2469ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->epintr_clear, 2479ecb8875SAjay Kumar Gupta wrp->txep_bitmap | wrp->rxep_bitmap); 2489ecb8875SAjay Kumar Gupta dsps_writeb(musb->mregs, MUSB_DEVCTL, 0); 2499ecb8875SAjay Kumar Gupta } 2509ecb8875SAjay Kumar Gupta 2519ecb8875SAjay Kumar Gupta static void otg_timer(unsigned long _musb) 2529ecb8875SAjay Kumar Gupta { 2539ecb8875SAjay Kumar Gupta struct musb *musb = (void *)_musb; 2549ecb8875SAjay Kumar Gupta void __iomem *mregs = musb->mregs; 2559ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 256db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 2579ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 2589ecb8875SAjay Kumar Gupta u8 devctl; 2599ecb8875SAjay Kumar Gupta unsigned long flags; 2600f901c98SSebastian Andrzej Siewior int skip_session = 0; 2619ecb8875SAjay Kumar Gupta 2629ecb8875SAjay Kumar Gupta /* 2639ecb8875SAjay Kumar Gupta * We poll because DSPS IP's won't expose several OTG-critical 2649ecb8875SAjay Kumar Gupta * status change events (from the transceiver) otherwise. 2659ecb8875SAjay Kumar Gupta */ 2669ecb8875SAjay Kumar Gupta devctl = dsps_readb(mregs, MUSB_DEVCTL); 2679ecb8875SAjay Kumar Gupta dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl, 268e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 2699ecb8875SAjay Kumar Gupta 2709ecb8875SAjay Kumar Gupta spin_lock_irqsave(&musb->lock, flags); 271e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 2729ecb8875SAjay Kumar Gupta case OTG_STATE_A_WAIT_BCON: 2730f901c98SSebastian Andrzej Siewior dsps_writeb(musb->mregs, MUSB_DEVCTL, 0); 2740f901c98SSebastian Andrzej Siewior skip_session = 1; 2750f901c98SSebastian Andrzej Siewior /* fall */ 2769ecb8875SAjay Kumar Gupta 2770f901c98SSebastian Andrzej Siewior case OTG_STATE_A_IDLE: 2780f901c98SSebastian Andrzej Siewior case OTG_STATE_B_IDLE: 2799ecb8875SAjay Kumar Gupta if (devctl & MUSB_DEVCTL_BDEVICE) { 280e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 2819ecb8875SAjay Kumar Gupta MUSB_DEV_MODE(musb); 2829ecb8875SAjay Kumar Gupta } else { 283e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_IDLE; 2849ecb8875SAjay Kumar Gupta MUSB_HST_MODE(musb); 2859ecb8875SAjay Kumar Gupta } 2860f901c98SSebastian Andrzej Siewior if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session) 2870f901c98SSebastian Andrzej Siewior dsps_writeb(mregs, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 2880f901c98SSebastian Andrzej Siewior mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ); 2899ecb8875SAjay Kumar Gupta break; 2909ecb8875SAjay Kumar Gupta case OTG_STATE_A_WAIT_VFALL: 291e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE; 2929ecb8875SAjay Kumar Gupta dsps_writel(musb->ctrl_base, wrp->coreintr_set, 2939ecb8875SAjay Kumar Gupta MUSB_INTR_VBUSERROR << wrp->usb_shift); 2949ecb8875SAjay Kumar Gupta break; 2959ecb8875SAjay Kumar Gupta default: 2969ecb8875SAjay Kumar Gupta break; 2979ecb8875SAjay Kumar Gupta } 2989ecb8875SAjay Kumar Gupta spin_unlock_irqrestore(&musb->lock, flags); 2999ecb8875SAjay Kumar Gupta } 3009ecb8875SAjay Kumar Gupta 3019ecb8875SAjay Kumar Gupta static irqreturn_t dsps_interrupt(int irq, void *hci) 3029ecb8875SAjay Kumar Gupta { 3039ecb8875SAjay Kumar Gupta struct musb *musb = hci; 3049ecb8875SAjay Kumar Gupta void __iomem *reg_base = musb->ctrl_base; 3059ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 306db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 3079ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 3089ecb8875SAjay Kumar Gupta unsigned long flags; 3099ecb8875SAjay Kumar Gupta irqreturn_t ret = IRQ_NONE; 3109ecb8875SAjay Kumar Gupta u32 epintr, usbintr; 3119ecb8875SAjay Kumar Gupta 3129ecb8875SAjay Kumar Gupta spin_lock_irqsave(&musb->lock, flags); 3139ecb8875SAjay Kumar Gupta 3149ecb8875SAjay Kumar Gupta /* Get endpoint interrupts */ 3159ecb8875SAjay Kumar Gupta epintr = dsps_readl(reg_base, wrp->epintr_status); 3169ecb8875SAjay Kumar Gupta musb->int_rx = (epintr & wrp->rxep_bitmap) >> wrp->rxep_shift; 3179ecb8875SAjay Kumar Gupta musb->int_tx = (epintr & wrp->txep_bitmap) >> wrp->txep_shift; 3189ecb8875SAjay Kumar Gupta 3199ecb8875SAjay Kumar Gupta if (epintr) 3209ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->epintr_status, epintr); 3219ecb8875SAjay Kumar Gupta 3229ecb8875SAjay Kumar Gupta /* Get usb core interrupts */ 3239ecb8875SAjay Kumar Gupta usbintr = dsps_readl(reg_base, wrp->coreintr_status); 3249ecb8875SAjay Kumar Gupta if (!usbintr && !epintr) 3259be73baeSSebastian Andrzej Siewior goto out; 3269ecb8875SAjay Kumar Gupta 3279ecb8875SAjay Kumar Gupta musb->int_usb = (usbintr & wrp->usb_bitmap) >> wrp->usb_shift; 3289ecb8875SAjay Kumar Gupta if (usbintr) 3299ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->coreintr_status, usbintr); 3309ecb8875SAjay Kumar Gupta 3319ecb8875SAjay Kumar Gupta dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n", 3329ecb8875SAjay Kumar Gupta usbintr, epintr); 3331d57de30SDaniel Mack 3349ecb8875SAjay Kumar Gupta if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) { 3359ecb8875SAjay Kumar Gupta int drvvbus = dsps_readl(reg_base, wrp->status); 3369ecb8875SAjay Kumar Gupta void __iomem *mregs = musb->mregs; 3379ecb8875SAjay Kumar Gupta u8 devctl = dsps_readb(mregs, MUSB_DEVCTL); 3389ecb8875SAjay Kumar Gupta int err; 3399ecb8875SAjay Kumar Gupta 340032ec49fSFelipe Balbi err = musb->int_usb & MUSB_INTR_VBUSERROR; 3419ecb8875SAjay Kumar Gupta if (err) { 3429ecb8875SAjay Kumar Gupta /* 3439ecb8875SAjay Kumar Gupta * The Mentor core doesn't debounce VBUS as needed 3449ecb8875SAjay Kumar Gupta * to cope with device connect current spikes. This 3459ecb8875SAjay Kumar Gupta * means it's not uncommon for bus-powered devices 3469ecb8875SAjay Kumar Gupta * to get VBUS errors during enumeration. 3479ecb8875SAjay Kumar Gupta * 3489ecb8875SAjay Kumar Gupta * This is a workaround, but newer RTL from Mentor 3499ecb8875SAjay Kumar Gupta * seems to allow a better one: "re"-starting sessions 3509ecb8875SAjay Kumar Gupta * without waiting for VBUS to stop registering in 3519ecb8875SAjay Kumar Gupta * devctl. 3529ecb8875SAjay Kumar Gupta */ 3539ecb8875SAjay Kumar Gupta musb->int_usb &= ~MUSB_INTR_VBUSERROR; 354e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL; 35597238b35SSebastian Andrzej Siewior mod_timer(&glue->timer, 3569ecb8875SAjay Kumar Gupta jiffies + wrp->poll_seconds * HZ); 3579ecb8875SAjay Kumar Gupta WARNING("VBUS error workaround (delay coming)\n"); 358032ec49fSFelipe Balbi } else if (drvvbus) { 3599ecb8875SAjay Kumar Gupta MUSB_HST_MODE(musb); 3609ecb8875SAjay Kumar Gupta musb->xceiv->otg->default_a = 1; 361e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE; 36297238b35SSebastian Andrzej Siewior del_timer(&glue->timer); 3639ecb8875SAjay Kumar Gupta } else { 3649ecb8875SAjay Kumar Gupta musb->is_active = 0; 3659ecb8875SAjay Kumar Gupta MUSB_DEV_MODE(musb); 3669ecb8875SAjay Kumar Gupta musb->xceiv->otg->default_a = 0; 367e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 3689ecb8875SAjay Kumar Gupta } 3699ecb8875SAjay Kumar Gupta 3709ecb8875SAjay Kumar Gupta /* NOTE: this must complete power-on within 100 ms. */ 3719ecb8875SAjay Kumar Gupta dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n", 3729ecb8875SAjay Kumar Gupta drvvbus ? "on" : "off", 373e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state), 3749ecb8875SAjay Kumar Gupta err ? " ERROR" : "", 3759ecb8875SAjay Kumar Gupta devctl); 3769ecb8875SAjay Kumar Gupta ret = IRQ_HANDLED; 3779ecb8875SAjay Kumar Gupta } 3789ecb8875SAjay Kumar Gupta 3799ecb8875SAjay Kumar Gupta if (musb->int_tx || musb->int_rx || musb->int_usb) 3809ecb8875SAjay Kumar Gupta ret |= musb_interrupt(musb); 3819ecb8875SAjay Kumar Gupta 3825ace3d00SBin Liu /* Poll for ID change in OTG port mode */ 383e47d9254SAntoine Tenart if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 3845ace3d00SBin Liu musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 38597238b35SSebastian Andrzej Siewior mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ); 3869be73baeSSebastian Andrzej Siewior out: 3879ecb8875SAjay Kumar Gupta spin_unlock_irqrestore(&musb->lock, flags); 3889ecb8875SAjay Kumar Gupta 3899ecb8875SAjay Kumar Gupta return ret; 3909ecb8875SAjay Kumar Gupta } 3919ecb8875SAjay Kumar Gupta 39240f099e3SMarkus Pargmann static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue) 39340f099e3SMarkus Pargmann { 39440f099e3SMarkus Pargmann struct dentry *root; 39540f099e3SMarkus Pargmann struct dentry *file; 39640f099e3SMarkus Pargmann char buf[128]; 39740f099e3SMarkus Pargmann 39840f099e3SMarkus Pargmann sprintf(buf, "%s.dsps", dev_name(musb->controller)); 39940f099e3SMarkus Pargmann root = debugfs_create_dir(buf, NULL); 40040f099e3SMarkus Pargmann if (!root) 40140f099e3SMarkus Pargmann return -ENOMEM; 40240f099e3SMarkus Pargmann glue->dbgfs_root = root; 40340f099e3SMarkus Pargmann 40440f099e3SMarkus Pargmann glue->regset.regs = dsps_musb_regs; 40540f099e3SMarkus Pargmann glue->regset.nregs = ARRAY_SIZE(dsps_musb_regs); 40640f099e3SMarkus Pargmann glue->regset.base = musb->ctrl_base; 40740f099e3SMarkus Pargmann 40840f099e3SMarkus Pargmann file = debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset); 40940f099e3SMarkus Pargmann if (!file) { 41040f099e3SMarkus Pargmann debugfs_remove_recursive(root); 41140f099e3SMarkus Pargmann return -ENOMEM; 41240f099e3SMarkus Pargmann } 41340f099e3SMarkus Pargmann return 0; 41440f099e3SMarkus Pargmann } 41540f099e3SMarkus Pargmann 4169ecb8875SAjay Kumar Gupta static int dsps_musb_init(struct musb *musb) 4179ecb8875SAjay Kumar Gupta { 4189ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 419db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 42097238b35SSebastian Andrzej Siewior struct platform_device *parent = to_platform_device(dev->parent); 4219ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 42297238b35SSebastian Andrzej Siewior void __iomem *reg_base; 42397238b35SSebastian Andrzej Siewior struct resource *r; 4249ecb8875SAjay Kumar Gupta u32 rev, val; 42540f099e3SMarkus Pargmann int ret; 4269ecb8875SAjay Kumar Gupta 42797238b35SSebastian Andrzej Siewior r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control"); 42897238b35SSebastian Andrzej Siewior reg_base = devm_ioremap_resource(dev, r); 42951ef74f6SJulia Lawall if (IS_ERR(reg_base)) 43051ef74f6SJulia Lawall return PTR_ERR(reg_base); 43197238b35SSebastian Andrzej Siewior musb->ctrl_base = reg_base; 4329ecb8875SAjay Kumar Gupta 433d7554226SAfzal Mohammed /* NOP driver needs change if supporting dual instance */ 43497238b35SSebastian Andrzej Siewior musb->xceiv = devm_usb_get_phy_by_phandle(dev, "phys", 0); 43597238b35SSebastian Andrzej Siewior if (IS_ERR(musb->xceiv)) 43697238b35SSebastian Andrzej Siewior return PTR_ERR(musb->xceiv); 4379ecb8875SAjay Kumar Gupta 438bb90600dSTony Lindgren musb->phy = devm_phy_get(dev->parent, "usb2-phy"); 439bb90600dSTony Lindgren 4409ecb8875SAjay Kumar Gupta /* Returns zero if e.g. not clocked */ 4419ecb8875SAjay Kumar Gupta rev = dsps_readl(reg_base, wrp->revision); 44297238b35SSebastian Andrzej Siewior if (!rev) 44397238b35SSebastian Andrzej Siewior return -ENODEV; 4449ecb8875SAjay Kumar Gupta 4457557a57fSSebastian Andrzej Siewior usb_phy_init(musb->xceiv); 446bb90600dSTony Lindgren if (IS_ERR(musb->phy)) { 447bb90600dSTony Lindgren musb->phy = NULL; 448bb90600dSTony Lindgren } else { 449bb90600dSTony Lindgren ret = phy_init(musb->phy); 450bb90600dSTony Lindgren if (ret < 0) 451bb90600dSTony Lindgren return ret; 452bb90600dSTony Lindgren ret = phy_power_on(musb->phy); 453bb90600dSTony Lindgren if (ret) { 454bb90600dSTony Lindgren phy_exit(musb->phy); 455bb90600dSTony Lindgren return ret; 456bb90600dSTony Lindgren } 457bb90600dSTony Lindgren } 458bb90600dSTony Lindgren 45997238b35SSebastian Andrzej Siewior setup_timer(&glue->timer, otg_timer, (unsigned long) musb); 4609ecb8875SAjay Kumar Gupta 4619ecb8875SAjay Kumar Gupta /* Reset the musb */ 4629ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->control, (1 << wrp->reset)); 4639ecb8875SAjay Kumar Gupta 4649ecb8875SAjay Kumar Gupta musb->isr = dsps_interrupt; 4659ecb8875SAjay Kumar Gupta 4669ecb8875SAjay Kumar Gupta /* reset the otgdisable bit, needed for host mode to work */ 4679ecb8875SAjay Kumar Gupta val = dsps_readl(reg_base, wrp->phy_utmi); 4689ecb8875SAjay Kumar Gupta val &= ~(1 << wrp->otg_disable); 4699ecb8875SAjay Kumar Gupta dsps_writel(musb->ctrl_base, wrp->phy_utmi, val); 4709ecb8875SAjay Kumar Gupta 471371254ceSGeorge Cherian /* 472371254ceSGeorge Cherian * Check whether the dsps version has babble control enabled. 473371254ceSGeorge Cherian * In latest silicon revision the babble control logic is enabled. 474371254ceSGeorge Cherian * If MUSB_BABBLE_CTL returns 0x4 then we have the babble control 475371254ceSGeorge Cherian * logic enabled. 476371254ceSGeorge Cherian */ 477371254ceSGeorge Cherian val = dsps_readb(musb->mregs, MUSB_BABBLE_CTL); 478f860f0b1SFelipe Balbi if (val & MUSB_BABBLE_RCV_DISABLE) { 479371254ceSGeorge Cherian glue->sw_babble_enabled = true; 480371254ceSGeorge Cherian val |= MUSB_BABBLE_SW_SESSION_CTRL; 481371254ceSGeorge Cherian dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, val); 482371254ceSGeorge Cherian } 483371254ceSGeorge Cherian 48440f099e3SMarkus Pargmann ret = dsps_musb_dbg_init(musb, glue); 48540f099e3SMarkus Pargmann if (ret) 48640f099e3SMarkus Pargmann return ret; 48740f099e3SMarkus Pargmann 4889ecb8875SAjay Kumar Gupta return 0; 4899ecb8875SAjay Kumar Gupta } 4909ecb8875SAjay Kumar Gupta 4919ecb8875SAjay Kumar Gupta static int dsps_musb_exit(struct musb *musb) 4929ecb8875SAjay Kumar Gupta { 4939ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 494db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 4959ecb8875SAjay Kumar Gupta 49697238b35SSebastian Andrzej Siewior del_timer_sync(&glue->timer); 4977557a57fSSebastian Andrzej Siewior usb_phy_shutdown(musb->xceiv); 498bb90600dSTony Lindgren phy_power_off(musb->phy); 499bb90600dSTony Lindgren phy_exit(musb->phy); 5000fca91b8SDaniel Mack debugfs_remove_recursive(glue->dbgfs_root); 5010fca91b8SDaniel Mack 5029ecb8875SAjay Kumar Gupta return 0; 5039ecb8875SAjay Kumar Gupta } 5049ecb8875SAjay Kumar Gupta 505943c1397SFelipe Balbi static int dsps_musb_set_mode(struct musb *musb, u8 mode) 506943c1397SFelipe Balbi { 507943c1397SFelipe Balbi struct device *dev = musb->controller; 508943c1397SFelipe Balbi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 509943c1397SFelipe Balbi const struct dsps_musb_wrapper *wrp = glue->wrp; 510943c1397SFelipe Balbi void __iomem *ctrl_base = musb->ctrl_base; 511943c1397SFelipe Balbi u32 reg; 512943c1397SFelipe Balbi 51380356913SLothar Waßmann reg = dsps_readl(ctrl_base, wrp->mode); 514943c1397SFelipe Balbi 515943c1397SFelipe Balbi switch (mode) { 516943c1397SFelipe Balbi case MUSB_HOST: 517943c1397SFelipe Balbi reg &= ~(1 << wrp->iddig); 518943c1397SFelipe Balbi 519943c1397SFelipe Balbi /* 520943c1397SFelipe Balbi * if we're setting mode to host-only or device-only, we're 521943c1397SFelipe Balbi * going to ignore whatever the PHY sends us and just force 522943c1397SFelipe Balbi * ID pin status by SW 523943c1397SFelipe Balbi */ 524943c1397SFelipe Balbi reg |= (1 << wrp->iddig_mux); 525943c1397SFelipe Balbi 52680356913SLothar Waßmann dsps_writel(ctrl_base, wrp->mode, reg); 527943c1397SFelipe Balbi dsps_writel(ctrl_base, wrp->phy_utmi, 0x02); 528943c1397SFelipe Balbi break; 529943c1397SFelipe Balbi case MUSB_PERIPHERAL: 530943c1397SFelipe Balbi reg |= (1 << wrp->iddig); 531943c1397SFelipe Balbi 532943c1397SFelipe Balbi /* 533943c1397SFelipe Balbi * if we're setting mode to host-only or device-only, we're 534943c1397SFelipe Balbi * going to ignore whatever the PHY sends us and just force 535943c1397SFelipe Balbi * ID pin status by SW 536943c1397SFelipe Balbi */ 537943c1397SFelipe Balbi reg |= (1 << wrp->iddig_mux); 538943c1397SFelipe Balbi 53980356913SLothar Waßmann dsps_writel(ctrl_base, wrp->mode, reg); 540943c1397SFelipe Balbi break; 541943c1397SFelipe Balbi case MUSB_OTG: 54280356913SLothar Waßmann dsps_writel(ctrl_base, wrp->phy_utmi, 0x02); 543943c1397SFelipe Balbi break; 544943c1397SFelipe Balbi default: 545943c1397SFelipe Balbi dev_err(glue->dev, "unsupported mode %d\n", mode); 546943c1397SFelipe Balbi return -EINVAL; 547943c1397SFelipe Balbi } 548943c1397SFelipe Balbi 549943c1397SFelipe Balbi return 0; 550943c1397SFelipe Balbi } 551943c1397SFelipe Balbi 552*3709ffcaSFelipe Balbi static bool dsps_sw_babble_control(struct musb *musb) 553371254ceSGeorge Cherian { 554371254ceSGeorge Cherian u8 babble_ctl; 555371254ceSGeorge Cherian bool session_restart = false; 556371254ceSGeorge Cherian 557371254ceSGeorge Cherian babble_ctl = dsps_readb(musb->mregs, MUSB_BABBLE_CTL); 558371254ceSGeorge Cherian dev_dbg(musb->controller, "babble: MUSB_BABBLE_CTL value %x\n", 559371254ceSGeorge Cherian babble_ctl); 560371254ceSGeorge Cherian /* 561371254ceSGeorge Cherian * check line monitor flag to check whether babble is 562371254ceSGeorge Cherian * due to noise 563371254ceSGeorge Cherian */ 564371254ceSGeorge Cherian dev_dbg(musb->controller, "STUCK_J is %s\n", 565371254ceSGeorge Cherian babble_ctl & MUSB_BABBLE_STUCK_J ? "set" : "reset"); 566371254ceSGeorge Cherian 567371254ceSGeorge Cherian if (babble_ctl & MUSB_BABBLE_STUCK_J) { 568371254ceSGeorge Cherian int timeout = 10; 569371254ceSGeorge Cherian 570371254ceSGeorge Cherian /* 571371254ceSGeorge Cherian * babble is due to noise, then set transmit idle (d7 bit) 572371254ceSGeorge Cherian * to resume normal operation 573371254ceSGeorge Cherian */ 574371254ceSGeorge Cherian babble_ctl = dsps_readb(musb->mregs, MUSB_BABBLE_CTL); 575371254ceSGeorge Cherian babble_ctl |= MUSB_BABBLE_FORCE_TXIDLE; 576371254ceSGeorge Cherian dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, babble_ctl); 577371254ceSGeorge Cherian 578371254ceSGeorge Cherian /* wait till line monitor flag cleared */ 579371254ceSGeorge Cherian dev_dbg(musb->controller, "Set TXIDLE, wait J to clear\n"); 580371254ceSGeorge Cherian do { 581371254ceSGeorge Cherian babble_ctl = dsps_readb(musb->mregs, MUSB_BABBLE_CTL); 582371254ceSGeorge Cherian udelay(1); 583371254ceSGeorge Cherian } while ((babble_ctl & MUSB_BABBLE_STUCK_J) && timeout--); 584371254ceSGeorge Cherian 585371254ceSGeorge Cherian /* check whether stuck_at_j bit cleared */ 586371254ceSGeorge Cherian if (babble_ctl & MUSB_BABBLE_STUCK_J) { 587371254ceSGeorge Cherian /* 588371254ceSGeorge Cherian * real babble condition has occurred 589371254ceSGeorge Cherian * restart the controller to start the 590371254ceSGeorge Cherian * session again 591371254ceSGeorge Cherian */ 592371254ceSGeorge Cherian dev_dbg(musb->controller, "J not cleared, misc (%x)\n", 593371254ceSGeorge Cherian babble_ctl); 594371254ceSGeorge Cherian session_restart = true; 595371254ceSGeorge Cherian } 596371254ceSGeorge Cherian } else { 597371254ceSGeorge Cherian session_restart = true; 598371254ceSGeorge Cherian } 599371254ceSGeorge Cherian 600371254ceSGeorge Cherian return session_restart; 601371254ceSGeorge Cherian } 602371254ceSGeorge Cherian 603d871c622SGeorge Cherian static int dsps_musb_reset(struct musb *musb) 6041d57de30SDaniel Mack { 6051d57de30SDaniel Mack struct device *dev = musb->controller; 6061d57de30SDaniel Mack struct dsps_glue *glue = dev_get_drvdata(dev->parent); 6071d57de30SDaniel Mack const struct dsps_musb_wrapper *wrp = glue->wrp; 608bb90600dSTony Lindgren int session_restart = 0, error; 6091d57de30SDaniel Mack 610371254ceSGeorge Cherian if (glue->sw_babble_enabled) 611*3709ffcaSFelipe Balbi session_restart = dsps_sw_babble_control(musb); 612371254ceSGeorge Cherian /* 613371254ceSGeorge Cherian * In case of new silicon version babble condition can be recovered 614371254ceSGeorge Cherian * without resetting the MUSB. But for older silicon versions, MUSB 615371254ceSGeorge Cherian * reset is needed 616371254ceSGeorge Cherian */ 617371254ceSGeorge Cherian if (session_restart || !glue->sw_babble_enabled) { 618371254ceSGeorge Cherian dev_info(musb->controller, "Restarting MUSB to recover from Babble\n"); 6191d57de30SDaniel Mack dsps_writel(musb->ctrl_base, wrp->control, (1 << wrp->reset)); 62056700178SGeorge Cherian usleep_range(100, 200); 62156700178SGeorge Cherian usb_phy_shutdown(musb->xceiv); 622bb90600dSTony Lindgren error = phy_power_off(musb->phy); 623bb90600dSTony Lindgren if (error) 624bb90600dSTony Lindgren dev_err(dev, "phy shutdown failed: %i\n", error); 62556700178SGeorge Cherian usleep_range(100, 200); 62656700178SGeorge Cherian usb_phy_init(musb->xceiv); 627bb90600dSTony Lindgren error = phy_power_on(musb->phy); 628bb90600dSTony Lindgren if (error) 629bb90600dSTony Lindgren dev_err(dev, "phy powerup failed: %i\n", error); 630371254ceSGeorge Cherian session_restart = 1; 631371254ceSGeorge Cherian } 63256700178SGeorge Cherian 633d0cddae7SFelipe Balbi return session_restart ? 0 : -EPIPE; 6341d57de30SDaniel Mack } 6351d57de30SDaniel Mack 6369ecb8875SAjay Kumar Gupta static struct musb_platform_ops dsps_ops = { 637d026e9c7STony Lindgren .quirks = MUSB_INDEXED_EP, 6389ecb8875SAjay Kumar Gupta .init = dsps_musb_init, 6399ecb8875SAjay Kumar Gupta .exit = dsps_musb_exit, 6409ecb8875SAjay Kumar Gupta 6419ecb8875SAjay Kumar Gupta .enable = dsps_musb_enable, 6429ecb8875SAjay Kumar Gupta .disable = dsps_musb_disable, 6439ecb8875SAjay Kumar Gupta 6449ecb8875SAjay Kumar Gupta .try_idle = dsps_musb_try_idle, 645943c1397SFelipe Balbi .set_mode = dsps_musb_set_mode, 6461d57de30SDaniel Mack .reset = dsps_musb_reset, 6479ecb8875SAjay Kumar Gupta }; 6489ecb8875SAjay Kumar Gupta 6499ecb8875SAjay Kumar Gupta static u64 musb_dmamask = DMA_BIT_MASK(32); 6509ecb8875SAjay Kumar Gupta 65197238b35SSebastian Andrzej Siewior static int get_int_prop(struct device_node *dn, const char *s) 6529ecb8875SAjay Kumar Gupta { 65397238b35SSebastian Andrzej Siewior int ret; 65497238b35SSebastian Andrzej Siewior u32 val; 65597238b35SSebastian Andrzej Siewior 65697238b35SSebastian Andrzej Siewior ret = of_property_read_u32(dn, s, &val); 65797238b35SSebastian Andrzej Siewior if (ret) 65897238b35SSebastian Andrzej Siewior return 0; 65997238b35SSebastian Andrzej Siewior return val; 66097238b35SSebastian Andrzej Siewior } 66197238b35SSebastian Andrzej Siewior 662c031a7d4SSebastian Andrzej Siewior static int get_musb_port_mode(struct device *dev) 663c031a7d4SSebastian Andrzej Siewior { 664c031a7d4SSebastian Andrzej Siewior enum usb_dr_mode mode; 665c031a7d4SSebastian Andrzej Siewior 666c031a7d4SSebastian Andrzej Siewior mode = of_usb_get_dr_mode(dev->of_node); 667c031a7d4SSebastian Andrzej Siewior switch (mode) { 668c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_HOST: 669c031a7d4SSebastian Andrzej Siewior return MUSB_PORT_MODE_HOST; 670c031a7d4SSebastian Andrzej Siewior 671c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_PERIPHERAL: 672c031a7d4SSebastian Andrzej Siewior return MUSB_PORT_MODE_GADGET; 673c031a7d4SSebastian Andrzej Siewior 674c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_UNKNOWN: 675c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_OTG: 676c031a7d4SSebastian Andrzej Siewior default: 677c031a7d4SSebastian Andrzej Siewior return MUSB_PORT_MODE_DUAL_ROLE; 6782b84f92bSJoe Perches } 679c031a7d4SSebastian Andrzej Siewior } 680c031a7d4SSebastian Andrzej Siewior 68197238b35SSebastian Andrzej Siewior static int dsps_create_musb_pdev(struct dsps_glue *glue, 68297238b35SSebastian Andrzej Siewior struct platform_device *parent) 68397238b35SSebastian Andrzej Siewior { 68497238b35SSebastian Andrzej Siewior struct musb_hdrc_platform_data pdata; 68597238b35SSebastian Andrzej Siewior struct resource resources[2]; 686c031a7d4SSebastian Andrzej Siewior struct resource *res; 68797238b35SSebastian Andrzej Siewior struct device *dev = &parent->dev; 68865145677SAjay Kumar Gupta struct musb_hdrc_config *config; 6899ecb8875SAjay Kumar Gupta struct platform_device *musb; 69097238b35SSebastian Andrzej Siewior struct device_node *dn = parent->dev.of_node; 691606bf4d5STony Lindgren int ret, val; 6929ecb8875SAjay Kumar Gupta 69397238b35SSebastian Andrzej Siewior memset(resources, 0, sizeof(resources)); 694c031a7d4SSebastian Andrzej Siewior res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc"); 695c031a7d4SSebastian Andrzej Siewior if (!res) { 69697238b35SSebastian Andrzej Siewior dev_err(dev, "failed to get memory.\n"); 697c031a7d4SSebastian Andrzej Siewior return -EINVAL; 6989ecb8875SAjay Kumar Gupta } 699c031a7d4SSebastian Andrzej Siewior resources[0] = *res; 70097238b35SSebastian Andrzej Siewior 701c031a7d4SSebastian Andrzej Siewior res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc"); 702c031a7d4SSebastian Andrzej Siewior if (!res) { 70397238b35SSebastian Andrzej Siewior dev_err(dev, "failed to get irq.\n"); 704c031a7d4SSebastian Andrzej Siewior return -EINVAL; 70597238b35SSebastian Andrzej Siewior } 706c031a7d4SSebastian Andrzej Siewior resources[1] = *res; 7079ecb8875SAjay Kumar Gupta 70865b3d52dSB, Ravi /* allocate the child platform device */ 7092f771164SSebastian Andrzej Siewior musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO); 71065b3d52dSB, Ravi if (!musb) { 71165b3d52dSB, Ravi dev_err(dev, "failed to allocate musb device\n"); 71297238b35SSebastian Andrzej Siewior return -ENOMEM; 71365b3d52dSB, Ravi } 7149ecb8875SAjay Kumar Gupta 7159ecb8875SAjay Kumar Gupta musb->dev.parent = dev; 7169ecb8875SAjay Kumar Gupta musb->dev.dma_mask = &musb_dmamask; 7179ecb8875SAjay Kumar Gupta musb->dev.coherent_dma_mask = musb_dmamask; 718c031a7d4SSebastian Andrzej Siewior musb->dev.of_node = of_node_get(dn); 7199ecb8875SAjay Kumar Gupta 72097238b35SSebastian Andrzej Siewior glue->musb = musb; 7219ecb8875SAjay Kumar Gupta 72297238b35SSebastian Andrzej Siewior ret = platform_device_add_resources(musb, resources, 72397238b35SSebastian Andrzej Siewior ARRAY_SIZE(resources)); 7249ecb8875SAjay Kumar Gupta if (ret) { 7259ecb8875SAjay Kumar Gupta dev_err(dev, "failed to add resources\n"); 72697238b35SSebastian Andrzej Siewior goto err; 7279ecb8875SAjay Kumar Gupta } 7289ecb8875SAjay Kumar Gupta 72997238b35SSebastian Andrzej Siewior config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL); 73065145677SAjay Kumar Gupta if (!config) { 731b25e5f1cSWei Yongjun ret = -ENOMEM; 73297238b35SSebastian Andrzej Siewior goto err; 73365145677SAjay Kumar Gupta } 73497238b35SSebastian Andrzej Siewior pdata.config = config; 73597238b35SSebastian Andrzej Siewior pdata.platform_ops = &dsps_ops; 73665145677SAjay Kumar Gupta 737c031a7d4SSebastian Andrzej Siewior config->num_eps = get_int_prop(dn, "mentor,num-eps"); 738c031a7d4SSebastian Andrzej Siewior config->ram_bits = get_int_prop(dn, "mentor,ram-bits"); 739869c5978SDaniel Mack config->host_port_deassert_reset_at_resume = 1; 740c031a7d4SSebastian Andrzej Siewior pdata.mode = get_musb_port_mode(dev); 741c031a7d4SSebastian Andrzej Siewior /* DT keeps this entry in mA, musb expects it as per USB spec */ 742c031a7d4SSebastian Andrzej Siewior pdata.power = get_int_prop(dn, "mentor,power") / 2; 743606bf4d5STony Lindgren 744606bf4d5STony Lindgren ret = of_property_read_u32(dn, "mentor,multipoint", &val); 745606bf4d5STony Lindgren if (!ret && val) 746606bf4d5STony Lindgren config->multipoint = true; 74765145677SAjay Kumar Gupta 74897238b35SSebastian Andrzej Siewior ret = platform_device_add_data(musb, &pdata, sizeof(pdata)); 7499ecb8875SAjay Kumar Gupta if (ret) { 7509ecb8875SAjay Kumar Gupta dev_err(dev, "failed to add platform_data\n"); 75197238b35SSebastian Andrzej Siewior goto err; 7529ecb8875SAjay Kumar Gupta } 7539ecb8875SAjay Kumar Gupta 7549ecb8875SAjay Kumar Gupta ret = platform_device_add(musb); 7559ecb8875SAjay Kumar Gupta if (ret) { 7569ecb8875SAjay Kumar Gupta dev_err(dev, "failed to register musb device\n"); 75797238b35SSebastian Andrzej Siewior goto err; 7589ecb8875SAjay Kumar Gupta } 7599ecb8875SAjay Kumar Gupta return 0; 7609ecb8875SAjay Kumar Gupta 76197238b35SSebastian Andrzej Siewior err: 7629ecb8875SAjay Kumar Gupta platform_device_put(musb); 7639ecb8875SAjay Kumar Gupta return ret; 7649ecb8875SAjay Kumar Gupta } 7659ecb8875SAjay Kumar Gupta 76641ac7b3aSBill Pemberton static int dsps_probe(struct platform_device *pdev) 7679ecb8875SAjay Kumar Gupta { 76865145677SAjay Kumar Gupta const struct of_device_id *match; 76965145677SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp; 7709ecb8875SAjay Kumar Gupta struct dsps_glue *glue; 77197238b35SSebastian Andrzej Siewior int ret; 7729ecb8875SAjay Kumar Gupta 7734fc4b274SSebastian Andrzej Siewior if (!strcmp(pdev->name, "musb-hdrc")) 7744fc4b274SSebastian Andrzej Siewior return -ENODEV; 7754fc4b274SSebastian Andrzej Siewior 776cc506036SFelipe Balbi match = of_match_node(musb_dsps_of_match, pdev->dev.of_node); 77765145677SAjay Kumar Gupta if (!match) { 77865145677SAjay Kumar Gupta dev_err(&pdev->dev, "fail to get matching of_match struct\n"); 77997238b35SSebastian Andrzej Siewior return -EINVAL; 78065145677SAjay Kumar Gupta } 78165145677SAjay Kumar Gupta wrp = match->data; 7829ecb8875SAjay Kumar Gupta 7839ecb8875SAjay Kumar Gupta /* allocate glue */ 784de9db572SMarkus Pargmann glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL); 7850816ea2fSPeter Chen if (!glue) 78697238b35SSebastian Andrzej Siewior return -ENOMEM; 7879ecb8875SAjay Kumar Gupta 7889ecb8875SAjay Kumar Gupta glue->dev = &pdev->dev; 78997238b35SSebastian Andrzej Siewior glue->wrp = wrp; 7909ecb8875SAjay Kumar Gupta 7919ecb8875SAjay Kumar Gupta platform_set_drvdata(pdev, glue); 7929ecb8875SAjay Kumar Gupta pm_runtime_enable(&pdev->dev); 7939ecb8875SAjay Kumar Gupta 7949ecb8875SAjay Kumar Gupta ret = pm_runtime_get_sync(&pdev->dev); 7959ecb8875SAjay Kumar Gupta if (ret < 0) { 7969ecb8875SAjay Kumar Gupta dev_err(&pdev->dev, "pm_runtime_get_sync FAILED"); 7970e38c4edSAjay Kumar Gupta goto err2; 7980e38c4edSAjay Kumar Gupta } 7990e38c4edSAjay Kumar Gupta 80097238b35SSebastian Andrzej Siewior ret = dsps_create_musb_pdev(glue, pdev); 80197238b35SSebastian Andrzej Siewior if (ret) 8029ecb8875SAjay Kumar Gupta goto err3; 8039ecb8875SAjay Kumar Gupta 8049ecb8875SAjay Kumar Gupta return 0; 8059ecb8875SAjay Kumar Gupta 8069ecb8875SAjay Kumar Gupta err3: 8070e38c4edSAjay Kumar Gupta pm_runtime_put(&pdev->dev); 8089ecb8875SAjay Kumar Gupta err2: 8090e38c4edSAjay Kumar Gupta pm_runtime_disable(&pdev->dev); 8109ecb8875SAjay Kumar Gupta return ret; 8119ecb8875SAjay Kumar Gupta } 81297238b35SSebastian Andrzej Siewior 813fb4e98abSBill Pemberton static int dsps_remove(struct platform_device *pdev) 8149ecb8875SAjay Kumar Gupta { 8159ecb8875SAjay Kumar Gupta struct dsps_glue *glue = platform_get_drvdata(pdev); 8169ecb8875SAjay Kumar Gupta 81797238b35SSebastian Andrzej Siewior platform_device_unregister(glue->musb); 8189ecb8875SAjay Kumar Gupta 8199ecb8875SAjay Kumar Gupta /* disable usbss clocks */ 8209ecb8875SAjay Kumar Gupta pm_runtime_put(&pdev->dev); 8219ecb8875SAjay Kumar Gupta pm_runtime_disable(&pdev->dev); 82240f099e3SMarkus Pargmann 8239ecb8875SAjay Kumar Gupta return 0; 8249ecb8875SAjay Kumar Gupta } 8259ecb8875SAjay Kumar Gupta 826fa7b4ca5SSebastian Andrzej Siewior static const struct dsps_musb_wrapper am33xx_driver_data = { 8279ecb8875SAjay Kumar Gupta .revision = 0x00, 8289ecb8875SAjay Kumar Gupta .control = 0x14, 8299ecb8875SAjay Kumar Gupta .status = 0x18, 8309ecb8875SAjay Kumar Gupta .epintr_set = 0x38, 8319ecb8875SAjay Kumar Gupta .epintr_clear = 0x40, 8329ecb8875SAjay Kumar Gupta .epintr_status = 0x30, 8339ecb8875SAjay Kumar Gupta .coreintr_set = 0x3c, 8349ecb8875SAjay Kumar Gupta .coreintr_clear = 0x44, 8359ecb8875SAjay Kumar Gupta .coreintr_status = 0x34, 8369ecb8875SAjay Kumar Gupta .phy_utmi = 0xe0, 8379ecb8875SAjay Kumar Gupta .mode = 0xe8, 838b991f9b7SDaniel Mack .tx_mode = 0x70, 839b991f9b7SDaniel Mack .rx_mode = 0x74, 8409ecb8875SAjay Kumar Gupta .reset = 0, 8419ecb8875SAjay Kumar Gupta .otg_disable = 21, 8429ecb8875SAjay Kumar Gupta .iddig = 8, 843943c1397SFelipe Balbi .iddig_mux = 7, 8449ecb8875SAjay Kumar Gupta .usb_shift = 0, 8459ecb8875SAjay Kumar Gupta .usb_mask = 0x1ff, 8469ecb8875SAjay Kumar Gupta .usb_bitmap = (0x1ff << 0), 8479ecb8875SAjay Kumar Gupta .drvvbus = 8, 8489ecb8875SAjay Kumar Gupta .txep_shift = 0, 8499ecb8875SAjay Kumar Gupta .txep_mask = 0xffff, 8509ecb8875SAjay Kumar Gupta .txep_bitmap = (0xffff << 0), 8519ecb8875SAjay Kumar Gupta .rxep_shift = 16, 8529ecb8875SAjay Kumar Gupta .rxep_mask = 0xfffe, 8539ecb8875SAjay Kumar Gupta .rxep_bitmap = (0xfffe << 16), 8549ecb8875SAjay Kumar Gupta .poll_seconds = 2, 8559ecb8875SAjay Kumar Gupta }; 8569ecb8875SAjay Kumar Gupta 8572f82686eSBill Pemberton static const struct of_device_id musb_dsps_of_match[] = { 85865145677SAjay Kumar Gupta { .compatible = "ti,musb-am33xx", 859fa7b4ca5SSebastian Andrzej Siewior .data = (void *) &am33xx_driver_data, }, 8609ecb8875SAjay Kumar Gupta { }, 8619ecb8875SAjay Kumar Gupta }; 8629ecb8875SAjay Kumar Gupta MODULE_DEVICE_TABLE(of, musb_dsps_of_match); 8639ecb8875SAjay Kumar Gupta 8645b783983SWolfram Sang #ifdef CONFIG_PM_SLEEP 865869c5978SDaniel Mack static int dsps_suspend(struct device *dev) 866869c5978SDaniel Mack { 867869c5978SDaniel Mack struct dsps_glue *glue = dev_get_drvdata(dev); 868869c5978SDaniel Mack const struct dsps_musb_wrapper *wrp = glue->wrp; 869869c5978SDaniel Mack struct musb *musb = platform_get_drvdata(glue->musb); 870f042e9cbSSebastian Andrzej Siewior void __iomem *mbase; 871869c5978SDaniel Mack 872468bcc2aSFelipe Balbi del_timer_sync(&glue->timer); 873f042e9cbSSebastian Andrzej Siewior 874f042e9cbSSebastian Andrzej Siewior if (!musb) 875f042e9cbSSebastian Andrzej Siewior /* This can happen if the musb device is in -EPROBE_DEFER */ 876f042e9cbSSebastian Andrzej Siewior return 0; 877f042e9cbSSebastian Andrzej Siewior 878f042e9cbSSebastian Andrzej Siewior mbase = musb->ctrl_base; 879869c5978SDaniel Mack glue->context.control = dsps_readl(mbase, wrp->control); 880869c5978SDaniel Mack glue->context.epintr = dsps_readl(mbase, wrp->epintr_set); 881869c5978SDaniel Mack glue->context.coreintr = dsps_readl(mbase, wrp->coreintr_set); 882869c5978SDaniel Mack glue->context.phy_utmi = dsps_readl(mbase, wrp->phy_utmi); 883869c5978SDaniel Mack glue->context.mode = dsps_readl(mbase, wrp->mode); 884869c5978SDaniel Mack glue->context.tx_mode = dsps_readl(mbase, wrp->tx_mode); 885869c5978SDaniel Mack glue->context.rx_mode = dsps_readl(mbase, wrp->rx_mode); 886869c5978SDaniel Mack 887869c5978SDaniel Mack return 0; 888869c5978SDaniel Mack } 889869c5978SDaniel Mack 890869c5978SDaniel Mack static int dsps_resume(struct device *dev) 891869c5978SDaniel Mack { 892869c5978SDaniel Mack struct dsps_glue *glue = dev_get_drvdata(dev); 893869c5978SDaniel Mack const struct dsps_musb_wrapper *wrp = glue->wrp; 894869c5978SDaniel Mack struct musb *musb = platform_get_drvdata(glue->musb); 895f042e9cbSSebastian Andrzej Siewior void __iomem *mbase; 896869c5978SDaniel Mack 897f042e9cbSSebastian Andrzej Siewior if (!musb) 898f042e9cbSSebastian Andrzej Siewior return 0; 899f042e9cbSSebastian Andrzej Siewior 900f042e9cbSSebastian Andrzej Siewior mbase = musb->ctrl_base; 901869c5978SDaniel Mack dsps_writel(mbase, wrp->control, glue->context.control); 902869c5978SDaniel Mack dsps_writel(mbase, wrp->epintr_set, glue->context.epintr); 903869c5978SDaniel Mack dsps_writel(mbase, wrp->coreintr_set, glue->context.coreintr); 904869c5978SDaniel Mack dsps_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi); 905869c5978SDaniel Mack dsps_writel(mbase, wrp->mode, glue->context.mode); 906869c5978SDaniel Mack dsps_writel(mbase, wrp->tx_mode, glue->context.tx_mode); 907869c5978SDaniel Mack dsps_writel(mbase, wrp->rx_mode, glue->context.rx_mode); 908e47d9254SAntoine Tenart if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 90953185b3aSSebastian Andrzej Siewior musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 91053185b3aSSebastian Andrzej Siewior mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ); 911869c5978SDaniel Mack 912869c5978SDaniel Mack return 0; 913869c5978SDaniel Mack } 914869c5978SDaniel Mack #endif 915869c5978SDaniel Mack 916869c5978SDaniel Mack static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume); 917869c5978SDaniel Mack 9189ecb8875SAjay Kumar Gupta static struct platform_driver dsps_usbss_driver = { 9199ecb8875SAjay Kumar Gupta .probe = dsps_probe, 9207690417dSBill Pemberton .remove = dsps_remove, 9219ecb8875SAjay Kumar Gupta .driver = { 9229ecb8875SAjay Kumar Gupta .name = "musb-dsps", 923869c5978SDaniel Mack .pm = &dsps_pm_ops, 924b432cb83SSachin Kamat .of_match_table = musb_dsps_of_match, 9259ecb8875SAjay Kumar Gupta }, 9269ecb8875SAjay Kumar Gupta }; 9279ecb8875SAjay Kumar Gupta 9289ecb8875SAjay Kumar Gupta MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer"); 9299ecb8875SAjay Kumar Gupta MODULE_AUTHOR("Ravi B <ravibabu@ti.com>"); 9309ecb8875SAjay Kumar Gupta MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>"); 9319ecb8875SAjay Kumar Gupta MODULE_LICENSE("GPL v2"); 9329ecb8875SAjay Kumar Gupta 93397238b35SSebastian Andrzej Siewior module_platform_driver(dsps_usbss_driver); 934