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 */ 1229e204d88SFelipe Balbi unsigned poll_timeout; 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); 228b8239dccSBin Liu /* start polling for ID change in dual-role idle mode */ 229b8239dccSBin Liu if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 230b8239dccSBin Liu musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 231b8239dccSBin Liu mod_timer(&glue->timer, jiffies + 232b8239dccSBin Liu msecs_to_jiffies(wrp->poll_timeout)); 2338b9fcce2SSebastian Andrzej Siewior dsps_musb_try_idle(musb, 0); 2349ecb8875SAjay Kumar Gupta } 2359ecb8875SAjay Kumar Gupta 2369ecb8875SAjay Kumar Gupta /** 2379ecb8875SAjay Kumar Gupta * dsps_musb_disable - disable HDRC and flush interrupts 2389ecb8875SAjay Kumar Gupta */ 2399ecb8875SAjay Kumar Gupta static void dsps_musb_disable(struct musb *musb) 2409ecb8875SAjay Kumar Gupta { 2419ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 2429ecb8875SAjay Kumar Gupta struct platform_device *pdev = to_platform_device(dev->parent); 2439ecb8875SAjay Kumar Gupta struct dsps_glue *glue = platform_get_drvdata(pdev); 2449ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 2459ecb8875SAjay Kumar Gupta void __iomem *reg_base = musb->ctrl_base; 2469ecb8875SAjay Kumar Gupta 2479ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap); 2489ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->epintr_clear, 2499ecb8875SAjay Kumar Gupta wrp->txep_bitmap | wrp->rxep_bitmap); 2509ecb8875SAjay Kumar Gupta dsps_writeb(musb->mregs, MUSB_DEVCTL, 0); 2519ecb8875SAjay Kumar Gupta } 2529ecb8875SAjay Kumar Gupta 2539ecb8875SAjay Kumar Gupta static void otg_timer(unsigned long _musb) 2549ecb8875SAjay Kumar Gupta { 2559ecb8875SAjay Kumar Gupta struct musb *musb = (void *)_musb; 2569ecb8875SAjay Kumar Gupta void __iomem *mregs = musb->mregs; 2579ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 258db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 2599ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 2609ecb8875SAjay Kumar Gupta u8 devctl; 2619ecb8875SAjay Kumar Gupta unsigned long flags; 2620f901c98SSebastian Andrzej Siewior int skip_session = 0; 2639ecb8875SAjay Kumar Gupta 2649ecb8875SAjay Kumar Gupta /* 2659ecb8875SAjay Kumar Gupta * We poll because DSPS IP's won't expose several OTG-critical 2669ecb8875SAjay Kumar Gupta * status change events (from the transceiver) otherwise. 2679ecb8875SAjay Kumar Gupta */ 2689ecb8875SAjay Kumar Gupta devctl = dsps_readb(mregs, MUSB_DEVCTL); 2699ecb8875SAjay Kumar Gupta dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl, 270e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 2719ecb8875SAjay Kumar Gupta 2729ecb8875SAjay Kumar Gupta spin_lock_irqsave(&musb->lock, flags); 273e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 2749ecb8875SAjay Kumar Gupta case OTG_STATE_A_WAIT_BCON: 2750f901c98SSebastian Andrzej Siewior dsps_writeb(musb->mregs, MUSB_DEVCTL, 0); 2760f901c98SSebastian Andrzej Siewior skip_session = 1; 2770f901c98SSebastian Andrzej Siewior /* fall */ 2789ecb8875SAjay Kumar Gupta 2790f901c98SSebastian Andrzej Siewior case OTG_STATE_A_IDLE: 2800f901c98SSebastian Andrzej Siewior case OTG_STATE_B_IDLE: 2819ecb8875SAjay Kumar Gupta if (devctl & MUSB_DEVCTL_BDEVICE) { 282e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 2839ecb8875SAjay Kumar Gupta MUSB_DEV_MODE(musb); 2849ecb8875SAjay Kumar Gupta } else { 285e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_IDLE; 2869ecb8875SAjay Kumar Gupta MUSB_HST_MODE(musb); 2879ecb8875SAjay Kumar Gupta } 2880f901c98SSebastian Andrzej Siewior if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session) 2890f901c98SSebastian Andrzej Siewior dsps_writeb(mregs, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 2909e204d88SFelipe Balbi mod_timer(&glue->timer, jiffies + 2919e204d88SFelipe Balbi msecs_to_jiffies(wrp->poll_timeout)); 2929ecb8875SAjay Kumar Gupta break; 2939ecb8875SAjay Kumar Gupta case OTG_STATE_A_WAIT_VFALL: 294e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE; 2959ecb8875SAjay Kumar Gupta dsps_writel(musb->ctrl_base, wrp->coreintr_set, 2969ecb8875SAjay Kumar Gupta MUSB_INTR_VBUSERROR << wrp->usb_shift); 2979ecb8875SAjay Kumar Gupta break; 2989ecb8875SAjay Kumar Gupta default: 2999ecb8875SAjay Kumar Gupta break; 3009ecb8875SAjay Kumar Gupta } 3019ecb8875SAjay Kumar Gupta spin_unlock_irqrestore(&musb->lock, flags); 3029ecb8875SAjay Kumar Gupta } 3039ecb8875SAjay Kumar Gupta 3049ecb8875SAjay Kumar Gupta static irqreturn_t dsps_interrupt(int irq, void *hci) 3059ecb8875SAjay Kumar Gupta { 3069ecb8875SAjay Kumar Gupta struct musb *musb = hci; 3079ecb8875SAjay Kumar Gupta void __iomem *reg_base = musb->ctrl_base; 3089ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 309db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 3109ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 3119ecb8875SAjay Kumar Gupta unsigned long flags; 3129ecb8875SAjay Kumar Gupta irqreturn_t ret = IRQ_NONE; 3139ecb8875SAjay Kumar Gupta u32 epintr, usbintr; 3149ecb8875SAjay Kumar Gupta 3159ecb8875SAjay Kumar Gupta spin_lock_irqsave(&musb->lock, flags); 3169ecb8875SAjay Kumar Gupta 3179ecb8875SAjay Kumar Gupta /* Get endpoint interrupts */ 3189ecb8875SAjay Kumar Gupta epintr = dsps_readl(reg_base, wrp->epintr_status); 3199ecb8875SAjay Kumar Gupta musb->int_rx = (epintr & wrp->rxep_bitmap) >> wrp->rxep_shift; 3209ecb8875SAjay Kumar Gupta musb->int_tx = (epintr & wrp->txep_bitmap) >> wrp->txep_shift; 3219ecb8875SAjay Kumar Gupta 3229ecb8875SAjay Kumar Gupta if (epintr) 3239ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->epintr_status, epintr); 3249ecb8875SAjay Kumar Gupta 3259ecb8875SAjay Kumar Gupta /* Get usb core interrupts */ 3269ecb8875SAjay Kumar Gupta usbintr = dsps_readl(reg_base, wrp->coreintr_status); 3279ecb8875SAjay Kumar Gupta if (!usbintr && !epintr) 3289be73baeSSebastian Andrzej Siewior goto out; 3299ecb8875SAjay Kumar Gupta 3309ecb8875SAjay Kumar Gupta musb->int_usb = (usbintr & wrp->usb_bitmap) >> wrp->usb_shift; 3319ecb8875SAjay Kumar Gupta if (usbintr) 3329ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->coreintr_status, usbintr); 3339ecb8875SAjay Kumar Gupta 3349ecb8875SAjay Kumar Gupta dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n", 3359ecb8875SAjay Kumar Gupta usbintr, epintr); 3361d57de30SDaniel Mack 3379ecb8875SAjay Kumar Gupta if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) { 3389ecb8875SAjay Kumar Gupta int drvvbus = dsps_readl(reg_base, wrp->status); 3399ecb8875SAjay Kumar Gupta void __iomem *mregs = musb->mregs; 3409ecb8875SAjay Kumar Gupta u8 devctl = dsps_readb(mregs, MUSB_DEVCTL); 3419ecb8875SAjay Kumar Gupta int err; 3429ecb8875SAjay Kumar Gupta 343032ec49fSFelipe Balbi err = musb->int_usb & MUSB_INTR_VBUSERROR; 3449ecb8875SAjay Kumar Gupta if (err) { 3459ecb8875SAjay Kumar Gupta /* 3469ecb8875SAjay Kumar Gupta * The Mentor core doesn't debounce VBUS as needed 3479ecb8875SAjay Kumar Gupta * to cope with device connect current spikes. This 3489ecb8875SAjay Kumar Gupta * means it's not uncommon for bus-powered devices 3499ecb8875SAjay Kumar Gupta * to get VBUS errors during enumeration. 3509ecb8875SAjay Kumar Gupta * 3519ecb8875SAjay Kumar Gupta * This is a workaround, but newer RTL from Mentor 3529ecb8875SAjay Kumar Gupta * seems to allow a better one: "re"-starting sessions 3539ecb8875SAjay Kumar Gupta * without waiting for VBUS to stop registering in 3549ecb8875SAjay Kumar Gupta * devctl. 3559ecb8875SAjay Kumar Gupta */ 3569ecb8875SAjay Kumar Gupta musb->int_usb &= ~MUSB_INTR_VBUSERROR; 357e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL; 3589e204d88SFelipe Balbi mod_timer(&glue->timer, jiffies + 3599e204d88SFelipe Balbi msecs_to_jiffies(wrp->poll_timeout)); 3609ecb8875SAjay Kumar Gupta WARNING("VBUS error workaround (delay coming)\n"); 361032ec49fSFelipe Balbi } else if (drvvbus) { 3629ecb8875SAjay Kumar Gupta MUSB_HST_MODE(musb); 3639ecb8875SAjay Kumar Gupta musb->xceiv->otg->default_a = 1; 364e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE; 36597238b35SSebastian Andrzej Siewior del_timer(&glue->timer); 3669ecb8875SAjay Kumar Gupta } else { 3679ecb8875SAjay Kumar Gupta musb->is_active = 0; 3689ecb8875SAjay Kumar Gupta MUSB_DEV_MODE(musb); 3699ecb8875SAjay Kumar Gupta musb->xceiv->otg->default_a = 0; 370e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 3719ecb8875SAjay Kumar Gupta } 3729ecb8875SAjay Kumar Gupta 3739ecb8875SAjay Kumar Gupta /* NOTE: this must complete power-on within 100 ms. */ 3749ecb8875SAjay Kumar Gupta dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n", 3759ecb8875SAjay Kumar Gupta drvvbus ? "on" : "off", 376e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state), 3779ecb8875SAjay Kumar Gupta err ? " ERROR" : "", 3789ecb8875SAjay Kumar Gupta devctl); 3799ecb8875SAjay Kumar Gupta ret = IRQ_HANDLED; 3809ecb8875SAjay Kumar Gupta } 3819ecb8875SAjay Kumar Gupta 3829ecb8875SAjay Kumar Gupta if (musb->int_tx || musb->int_rx || musb->int_usb) 3839ecb8875SAjay Kumar Gupta ret |= musb_interrupt(musb); 3849ecb8875SAjay Kumar Gupta 3855ace3d00SBin Liu /* Poll for ID change in OTG port mode */ 386e47d9254SAntoine Tenart if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 3875ace3d00SBin Liu musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 3889e204d88SFelipe Balbi mod_timer(&glue->timer, jiffies + 3899e204d88SFelipe Balbi msecs_to_jiffies(wrp->poll_timeout)); 3909be73baeSSebastian Andrzej Siewior out: 3919ecb8875SAjay Kumar Gupta spin_unlock_irqrestore(&musb->lock, flags); 3929ecb8875SAjay Kumar Gupta 3939ecb8875SAjay Kumar Gupta return ret; 3949ecb8875SAjay Kumar Gupta } 3959ecb8875SAjay Kumar Gupta 39640f099e3SMarkus Pargmann static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue) 39740f099e3SMarkus Pargmann { 39840f099e3SMarkus Pargmann struct dentry *root; 39940f099e3SMarkus Pargmann struct dentry *file; 40040f099e3SMarkus Pargmann char buf[128]; 40140f099e3SMarkus Pargmann 40240f099e3SMarkus Pargmann sprintf(buf, "%s.dsps", dev_name(musb->controller)); 40340f099e3SMarkus Pargmann root = debugfs_create_dir(buf, NULL); 40440f099e3SMarkus Pargmann if (!root) 40540f099e3SMarkus Pargmann return -ENOMEM; 40640f099e3SMarkus Pargmann glue->dbgfs_root = root; 40740f099e3SMarkus Pargmann 40840f099e3SMarkus Pargmann glue->regset.regs = dsps_musb_regs; 40940f099e3SMarkus Pargmann glue->regset.nregs = ARRAY_SIZE(dsps_musb_regs); 41040f099e3SMarkus Pargmann glue->regset.base = musb->ctrl_base; 41140f099e3SMarkus Pargmann 41240f099e3SMarkus Pargmann file = debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset); 41340f099e3SMarkus Pargmann if (!file) { 41440f099e3SMarkus Pargmann debugfs_remove_recursive(root); 41540f099e3SMarkus Pargmann return -ENOMEM; 41640f099e3SMarkus Pargmann } 41740f099e3SMarkus Pargmann return 0; 41840f099e3SMarkus Pargmann } 41940f099e3SMarkus Pargmann 4209ecb8875SAjay Kumar Gupta static int dsps_musb_init(struct musb *musb) 4219ecb8875SAjay Kumar Gupta { 4229ecb8875SAjay Kumar Gupta struct device *dev = musb->controller; 423db4a9320SB, Ravi struct dsps_glue *glue = dev_get_drvdata(dev->parent); 42497238b35SSebastian Andrzej Siewior struct platform_device *parent = to_platform_device(dev->parent); 4259ecb8875SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp = glue->wrp; 42697238b35SSebastian Andrzej Siewior void __iomem *reg_base; 42797238b35SSebastian Andrzej Siewior struct resource *r; 4289ecb8875SAjay Kumar Gupta u32 rev, val; 42940f099e3SMarkus Pargmann int ret; 4309ecb8875SAjay Kumar Gupta 43197238b35SSebastian Andrzej Siewior r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control"); 43297238b35SSebastian Andrzej Siewior reg_base = devm_ioremap_resource(dev, r); 43351ef74f6SJulia Lawall if (IS_ERR(reg_base)) 43451ef74f6SJulia Lawall return PTR_ERR(reg_base); 43597238b35SSebastian Andrzej Siewior musb->ctrl_base = reg_base; 4369ecb8875SAjay Kumar Gupta 437d7554226SAfzal Mohammed /* NOP driver needs change if supporting dual instance */ 438983f3cabSFelipe Balbi musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, "phys", 0); 43997238b35SSebastian Andrzej Siewior if (IS_ERR(musb->xceiv)) 44097238b35SSebastian Andrzej Siewior return PTR_ERR(musb->xceiv); 4419ecb8875SAjay Kumar Gupta 442bb90600dSTony Lindgren musb->phy = devm_phy_get(dev->parent, "usb2-phy"); 443bb90600dSTony Lindgren 4449ecb8875SAjay Kumar Gupta /* Returns zero if e.g. not clocked */ 4459ecb8875SAjay Kumar Gupta rev = dsps_readl(reg_base, wrp->revision); 44697238b35SSebastian Andrzej Siewior if (!rev) 44797238b35SSebastian Andrzej Siewior return -ENODEV; 4489ecb8875SAjay Kumar Gupta 4497557a57fSSebastian Andrzej Siewior usb_phy_init(musb->xceiv); 450bb90600dSTony Lindgren if (IS_ERR(musb->phy)) { 451bb90600dSTony Lindgren musb->phy = NULL; 452bb90600dSTony Lindgren } else { 453bb90600dSTony Lindgren ret = phy_init(musb->phy); 454bb90600dSTony Lindgren if (ret < 0) 455bb90600dSTony Lindgren return ret; 456bb90600dSTony Lindgren ret = phy_power_on(musb->phy); 457bb90600dSTony Lindgren if (ret) { 458bb90600dSTony Lindgren phy_exit(musb->phy); 459bb90600dSTony Lindgren return ret; 460bb90600dSTony Lindgren } 461bb90600dSTony Lindgren } 462bb90600dSTony Lindgren 46397238b35SSebastian Andrzej Siewior setup_timer(&glue->timer, otg_timer, (unsigned long) musb); 4649ecb8875SAjay Kumar Gupta 4659ecb8875SAjay Kumar Gupta /* Reset the musb */ 4669ecb8875SAjay Kumar Gupta dsps_writel(reg_base, wrp->control, (1 << wrp->reset)); 4679ecb8875SAjay Kumar Gupta 4689ecb8875SAjay Kumar Gupta musb->isr = dsps_interrupt; 4699ecb8875SAjay Kumar Gupta 4709ecb8875SAjay Kumar Gupta /* reset the otgdisable bit, needed for host mode to work */ 4719ecb8875SAjay Kumar Gupta val = dsps_readl(reg_base, wrp->phy_utmi); 4729ecb8875SAjay Kumar Gupta val &= ~(1 << wrp->otg_disable); 4739ecb8875SAjay Kumar Gupta dsps_writel(musb->ctrl_base, wrp->phy_utmi, val); 4749ecb8875SAjay Kumar Gupta 475371254ceSGeorge Cherian /* 476371254ceSGeorge Cherian * Check whether the dsps version has babble control enabled. 477371254ceSGeorge Cherian * In latest silicon revision the babble control logic is enabled. 478371254ceSGeorge Cherian * If MUSB_BABBLE_CTL returns 0x4 then we have the babble control 479371254ceSGeorge Cherian * logic enabled. 480371254ceSGeorge Cherian */ 481371254ceSGeorge Cherian val = dsps_readb(musb->mregs, MUSB_BABBLE_CTL); 482f860f0b1SFelipe Balbi if (val & MUSB_BABBLE_RCV_DISABLE) { 483371254ceSGeorge Cherian glue->sw_babble_enabled = true; 484371254ceSGeorge Cherian val |= MUSB_BABBLE_SW_SESSION_CTRL; 485371254ceSGeorge Cherian dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, val); 486371254ceSGeorge Cherian } 487371254ceSGeorge Cherian 488e94a7369SSaurabh Karajgaonkar return dsps_musb_dbg_init(musb, glue); 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 5523709ffcaSFelipe 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 603b28a6432SFelipe Balbi static int dsps_musb_recover(struct musb *musb) 6041d57de30SDaniel Mack { 6051d57de30SDaniel Mack struct device *dev = musb->controller; 6061d57de30SDaniel Mack struct dsps_glue *glue = dev_get_drvdata(dev->parent); 607011d0dd5SFelipe Balbi int session_restart = 0; 6081d57de30SDaniel Mack 609371254ceSGeorge Cherian if (glue->sw_babble_enabled) 6103709ffcaSFelipe Balbi session_restart = dsps_sw_babble_control(musb); 611011d0dd5SFelipe Balbi else 612371254ceSGeorge Cherian session_restart = 1; 61356700178SGeorge Cherian 614d0cddae7SFelipe Balbi return session_restart ? 0 : -EPIPE; 6151d57de30SDaniel Mack } 6161d57de30SDaniel Mack 6173e457371STony Lindgren /* Similar to am35x, dm81xx support only 32-bit read operation */ 6183e457371STony Lindgren static void dsps_read_fifo32(struct musb_hw_ep *hw_ep, u16 len, u8 *dst) 6193e457371STony Lindgren { 6203e457371STony Lindgren void __iomem *fifo = hw_ep->fifo; 6213e457371STony Lindgren 6223e457371STony Lindgren if (len >= 4) { 623d30323f8STony Lindgren ioread32_rep(fifo, dst, len >> 2); 6243e457371STony Lindgren dst += len & ~0x03; 6253e457371STony Lindgren len &= 0x03; 6263e457371STony Lindgren } 6273e457371STony Lindgren 6283e457371STony Lindgren /* Read any remaining 1 to 3 bytes */ 6293e457371STony Lindgren if (len > 0) { 6303e457371STony Lindgren u32 val = musb_readl(fifo, 0); 6313e457371STony Lindgren memcpy(dst, &val, len); 6323e457371STony Lindgren } 6333e457371STony Lindgren } 6343e457371STony Lindgren 6359ecb8875SAjay Kumar Gupta static struct musb_platform_ops dsps_ops = { 636f8e9f34fSTony Lindgren .quirks = MUSB_DMA_CPPI41 | MUSB_INDEXED_EP, 6379ecb8875SAjay Kumar Gupta .init = dsps_musb_init, 6389ecb8875SAjay Kumar Gupta .exit = dsps_musb_exit, 6399ecb8875SAjay Kumar Gupta 6407f6283edSTony Lindgren #ifdef CONFIG_USB_TI_CPPI41_DMA 6417f6283edSTony Lindgren .dma_init = cppi41_dma_controller_create, 6427f6283edSTony Lindgren .dma_exit = cppi41_dma_controller_destroy, 6437f6283edSTony Lindgren #endif 6449ecb8875SAjay Kumar Gupta .enable = dsps_musb_enable, 6459ecb8875SAjay Kumar Gupta .disable = dsps_musb_disable, 6469ecb8875SAjay Kumar Gupta 6479ecb8875SAjay Kumar Gupta .try_idle = dsps_musb_try_idle, 648943c1397SFelipe Balbi .set_mode = dsps_musb_set_mode, 649b28a6432SFelipe Balbi .recover = dsps_musb_recover, 6509ecb8875SAjay Kumar Gupta }; 6519ecb8875SAjay Kumar Gupta 6529ecb8875SAjay Kumar Gupta static u64 musb_dmamask = DMA_BIT_MASK(32); 6539ecb8875SAjay Kumar Gupta 65497238b35SSebastian Andrzej Siewior static int get_int_prop(struct device_node *dn, const char *s) 6559ecb8875SAjay Kumar Gupta { 65697238b35SSebastian Andrzej Siewior int ret; 65797238b35SSebastian Andrzej Siewior u32 val; 65897238b35SSebastian Andrzej Siewior 65997238b35SSebastian Andrzej Siewior ret = of_property_read_u32(dn, s, &val); 66097238b35SSebastian Andrzej Siewior if (ret) 66197238b35SSebastian Andrzej Siewior return 0; 66297238b35SSebastian Andrzej Siewior return val; 66397238b35SSebastian Andrzej Siewior } 66497238b35SSebastian Andrzej Siewior 665c031a7d4SSebastian Andrzej Siewior static int get_musb_port_mode(struct device *dev) 666c031a7d4SSebastian Andrzej Siewior { 667c031a7d4SSebastian Andrzej Siewior enum usb_dr_mode mode; 668c031a7d4SSebastian Andrzej Siewior 669*06e7114fSHeikki Krogerus mode = usb_get_dr_mode(dev); 670c031a7d4SSebastian Andrzej Siewior switch (mode) { 671c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_HOST: 672c031a7d4SSebastian Andrzej Siewior return MUSB_PORT_MODE_HOST; 673c031a7d4SSebastian Andrzej Siewior 674c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_PERIPHERAL: 675c031a7d4SSebastian Andrzej Siewior return MUSB_PORT_MODE_GADGET; 676c031a7d4SSebastian Andrzej Siewior 677c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_UNKNOWN: 678c031a7d4SSebastian Andrzej Siewior case USB_DR_MODE_OTG: 679c031a7d4SSebastian Andrzej Siewior default: 680c031a7d4SSebastian Andrzej Siewior return MUSB_PORT_MODE_DUAL_ROLE; 6812b84f92bSJoe Perches } 682c031a7d4SSebastian Andrzej Siewior } 683c031a7d4SSebastian Andrzej Siewior 68497238b35SSebastian Andrzej Siewior static int dsps_create_musb_pdev(struct dsps_glue *glue, 68597238b35SSebastian Andrzej Siewior struct platform_device *parent) 68697238b35SSebastian Andrzej Siewior { 68797238b35SSebastian Andrzej Siewior struct musb_hdrc_platform_data pdata; 68897238b35SSebastian Andrzej Siewior struct resource resources[2]; 689c031a7d4SSebastian Andrzej Siewior struct resource *res; 69097238b35SSebastian Andrzej Siewior struct device *dev = &parent->dev; 69165145677SAjay Kumar Gupta struct musb_hdrc_config *config; 6929ecb8875SAjay Kumar Gupta struct platform_device *musb; 69397238b35SSebastian Andrzej Siewior struct device_node *dn = parent->dev.of_node; 694606bf4d5STony Lindgren int ret, val; 6959ecb8875SAjay Kumar Gupta 69697238b35SSebastian Andrzej Siewior memset(resources, 0, sizeof(resources)); 697c031a7d4SSebastian Andrzej Siewior res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc"); 698c031a7d4SSebastian Andrzej Siewior if (!res) { 69997238b35SSebastian Andrzej Siewior dev_err(dev, "failed to get memory.\n"); 700c031a7d4SSebastian Andrzej Siewior return -EINVAL; 7019ecb8875SAjay Kumar Gupta } 702c031a7d4SSebastian Andrzej Siewior resources[0] = *res; 70397238b35SSebastian Andrzej Siewior 704c031a7d4SSebastian Andrzej Siewior res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc"); 705c031a7d4SSebastian Andrzej Siewior if (!res) { 70697238b35SSebastian Andrzej Siewior dev_err(dev, "failed to get irq.\n"); 707c031a7d4SSebastian Andrzej Siewior return -EINVAL; 70897238b35SSebastian Andrzej Siewior } 709c031a7d4SSebastian Andrzej Siewior resources[1] = *res; 7109ecb8875SAjay Kumar Gupta 71165b3d52dSB, Ravi /* allocate the child platform device */ 7122f771164SSebastian Andrzej Siewior musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO); 71365b3d52dSB, Ravi if (!musb) { 71465b3d52dSB, Ravi dev_err(dev, "failed to allocate musb device\n"); 71597238b35SSebastian Andrzej Siewior return -ENOMEM; 71665b3d52dSB, Ravi } 7179ecb8875SAjay Kumar Gupta 7189ecb8875SAjay Kumar Gupta musb->dev.parent = dev; 7199ecb8875SAjay Kumar Gupta musb->dev.dma_mask = &musb_dmamask; 7209ecb8875SAjay Kumar Gupta musb->dev.coherent_dma_mask = musb_dmamask; 7219ecb8875SAjay Kumar Gupta 72297238b35SSebastian Andrzej Siewior glue->musb = musb; 7239ecb8875SAjay Kumar Gupta 72497238b35SSebastian Andrzej Siewior ret = platform_device_add_resources(musb, resources, 72597238b35SSebastian Andrzej Siewior ARRAY_SIZE(resources)); 7269ecb8875SAjay Kumar Gupta if (ret) { 7279ecb8875SAjay Kumar Gupta dev_err(dev, "failed to add resources\n"); 72897238b35SSebastian Andrzej Siewior goto err; 7299ecb8875SAjay Kumar Gupta } 7309ecb8875SAjay Kumar Gupta 73197238b35SSebastian Andrzej Siewior config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL); 73265145677SAjay Kumar Gupta if (!config) { 733b25e5f1cSWei Yongjun ret = -ENOMEM; 73497238b35SSebastian Andrzej Siewior goto err; 73565145677SAjay Kumar Gupta } 73697238b35SSebastian Andrzej Siewior pdata.config = config; 73797238b35SSebastian Andrzej Siewior pdata.platform_ops = &dsps_ops; 73865145677SAjay Kumar Gupta 739c031a7d4SSebastian Andrzej Siewior config->num_eps = get_int_prop(dn, "mentor,num-eps"); 740c031a7d4SSebastian Andrzej Siewior config->ram_bits = get_int_prop(dn, "mentor,ram-bits"); 741869c5978SDaniel Mack config->host_port_deassert_reset_at_resume = 1; 742c031a7d4SSebastian Andrzej Siewior pdata.mode = get_musb_port_mode(dev); 743c031a7d4SSebastian Andrzej Siewior /* DT keeps this entry in mA, musb expects it as per USB spec */ 744c031a7d4SSebastian Andrzej Siewior pdata.power = get_int_prop(dn, "mentor,power") / 2; 745606bf4d5STony Lindgren 746606bf4d5STony Lindgren ret = of_property_read_u32(dn, "mentor,multipoint", &val); 747606bf4d5STony Lindgren if (!ret && val) 748606bf4d5STony Lindgren config->multipoint = true; 74965145677SAjay Kumar Gupta 75063863b98SHeikki Krogerus config->maximum_speed = usb_get_maximum_speed(&parent->dev); 75141932b9bSBin Liu switch (config->maximum_speed) { 75241932b9bSBin Liu case USB_SPEED_LOW: 75341932b9bSBin Liu case USB_SPEED_FULL: 75441932b9bSBin Liu break; 75541932b9bSBin Liu case USB_SPEED_SUPER: 75641932b9bSBin Liu dev_warn(dev, "ignore incorrect maximum_speed " 75741932b9bSBin Liu "(super-speed) setting in dts"); 75841932b9bSBin Liu /* fall through */ 75941932b9bSBin Liu default: 76041932b9bSBin Liu config->maximum_speed = USB_SPEED_HIGH; 76141932b9bSBin Liu } 76241932b9bSBin Liu 76397238b35SSebastian Andrzej Siewior ret = platform_device_add_data(musb, &pdata, sizeof(pdata)); 7649ecb8875SAjay Kumar Gupta if (ret) { 7659ecb8875SAjay Kumar Gupta dev_err(dev, "failed to add platform_data\n"); 76697238b35SSebastian Andrzej Siewior goto err; 7679ecb8875SAjay Kumar Gupta } 7689ecb8875SAjay Kumar Gupta 7699ecb8875SAjay Kumar Gupta ret = platform_device_add(musb); 7709ecb8875SAjay Kumar Gupta if (ret) { 7719ecb8875SAjay Kumar Gupta dev_err(dev, "failed to register musb device\n"); 77297238b35SSebastian Andrzej Siewior goto err; 7739ecb8875SAjay Kumar Gupta } 7749ecb8875SAjay Kumar Gupta return 0; 7759ecb8875SAjay Kumar Gupta 77697238b35SSebastian Andrzej Siewior err: 7779ecb8875SAjay Kumar Gupta platform_device_put(musb); 7789ecb8875SAjay Kumar Gupta return ret; 7799ecb8875SAjay Kumar Gupta } 7809ecb8875SAjay Kumar Gupta 78141ac7b3aSBill Pemberton static int dsps_probe(struct platform_device *pdev) 7829ecb8875SAjay Kumar Gupta { 78365145677SAjay Kumar Gupta const struct of_device_id *match; 78465145677SAjay Kumar Gupta const struct dsps_musb_wrapper *wrp; 7859ecb8875SAjay Kumar Gupta struct dsps_glue *glue; 78697238b35SSebastian Andrzej Siewior int ret; 7879ecb8875SAjay Kumar Gupta 7884fc4b274SSebastian Andrzej Siewior if (!strcmp(pdev->name, "musb-hdrc")) 7894fc4b274SSebastian Andrzej Siewior return -ENODEV; 7904fc4b274SSebastian Andrzej Siewior 791cc506036SFelipe Balbi match = of_match_node(musb_dsps_of_match, pdev->dev.of_node); 79265145677SAjay Kumar Gupta if (!match) { 79365145677SAjay Kumar Gupta dev_err(&pdev->dev, "fail to get matching of_match struct\n"); 79497238b35SSebastian Andrzej Siewior return -EINVAL; 79565145677SAjay Kumar Gupta } 79665145677SAjay Kumar Gupta wrp = match->data; 7979ecb8875SAjay Kumar Gupta 7983e457371STony Lindgren if (of_device_is_compatible(pdev->dev.of_node, "ti,musb-dm816")) 7993e457371STony Lindgren dsps_ops.read_fifo = dsps_read_fifo32; 8003e457371STony Lindgren 8019ecb8875SAjay Kumar Gupta /* allocate glue */ 802de9db572SMarkus Pargmann glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL); 8030816ea2fSPeter Chen if (!glue) 80497238b35SSebastian Andrzej Siewior return -ENOMEM; 8059ecb8875SAjay Kumar Gupta 8069ecb8875SAjay Kumar Gupta glue->dev = &pdev->dev; 80797238b35SSebastian Andrzej Siewior glue->wrp = wrp; 8089ecb8875SAjay Kumar Gupta 8099ecb8875SAjay Kumar Gupta platform_set_drvdata(pdev, glue); 8109ecb8875SAjay Kumar Gupta pm_runtime_enable(&pdev->dev); 8119ecb8875SAjay Kumar Gupta 8129ecb8875SAjay Kumar Gupta ret = pm_runtime_get_sync(&pdev->dev); 8139ecb8875SAjay Kumar Gupta if (ret < 0) { 8149ecb8875SAjay Kumar Gupta dev_err(&pdev->dev, "pm_runtime_get_sync FAILED"); 8150e38c4edSAjay Kumar Gupta goto err2; 8160e38c4edSAjay Kumar Gupta } 8170e38c4edSAjay Kumar Gupta 81897238b35SSebastian Andrzej Siewior ret = dsps_create_musb_pdev(glue, pdev); 81997238b35SSebastian Andrzej Siewior if (ret) 8209ecb8875SAjay Kumar Gupta goto err3; 8219ecb8875SAjay Kumar Gupta 8229ecb8875SAjay Kumar Gupta return 0; 8239ecb8875SAjay Kumar Gupta 8249ecb8875SAjay Kumar Gupta err3: 8250e38c4edSAjay Kumar Gupta pm_runtime_put(&pdev->dev); 8269ecb8875SAjay Kumar Gupta err2: 8270e38c4edSAjay Kumar Gupta pm_runtime_disable(&pdev->dev); 8289ecb8875SAjay Kumar Gupta return ret; 8299ecb8875SAjay Kumar Gupta } 83097238b35SSebastian Andrzej Siewior 831fb4e98abSBill Pemberton static int dsps_remove(struct platform_device *pdev) 8329ecb8875SAjay Kumar Gupta { 8339ecb8875SAjay Kumar Gupta struct dsps_glue *glue = platform_get_drvdata(pdev); 8349ecb8875SAjay Kumar Gupta 83597238b35SSebastian Andrzej Siewior platform_device_unregister(glue->musb); 8369ecb8875SAjay Kumar Gupta 8379ecb8875SAjay Kumar Gupta /* disable usbss clocks */ 8389ecb8875SAjay Kumar Gupta pm_runtime_put(&pdev->dev); 8399ecb8875SAjay Kumar Gupta pm_runtime_disable(&pdev->dev); 84040f099e3SMarkus Pargmann 8419ecb8875SAjay Kumar Gupta return 0; 8429ecb8875SAjay Kumar Gupta } 8439ecb8875SAjay Kumar Gupta 844fa7b4ca5SSebastian Andrzej Siewior static const struct dsps_musb_wrapper am33xx_driver_data = { 8459ecb8875SAjay Kumar Gupta .revision = 0x00, 8469ecb8875SAjay Kumar Gupta .control = 0x14, 8479ecb8875SAjay Kumar Gupta .status = 0x18, 8489ecb8875SAjay Kumar Gupta .epintr_set = 0x38, 8499ecb8875SAjay Kumar Gupta .epintr_clear = 0x40, 8509ecb8875SAjay Kumar Gupta .epintr_status = 0x30, 8519ecb8875SAjay Kumar Gupta .coreintr_set = 0x3c, 8529ecb8875SAjay Kumar Gupta .coreintr_clear = 0x44, 8539ecb8875SAjay Kumar Gupta .coreintr_status = 0x34, 8549ecb8875SAjay Kumar Gupta .phy_utmi = 0xe0, 8559ecb8875SAjay Kumar Gupta .mode = 0xe8, 856b991f9b7SDaniel Mack .tx_mode = 0x70, 857b991f9b7SDaniel Mack .rx_mode = 0x74, 8589ecb8875SAjay Kumar Gupta .reset = 0, 8599ecb8875SAjay Kumar Gupta .otg_disable = 21, 8609ecb8875SAjay Kumar Gupta .iddig = 8, 861943c1397SFelipe Balbi .iddig_mux = 7, 8629ecb8875SAjay Kumar Gupta .usb_shift = 0, 8639ecb8875SAjay Kumar Gupta .usb_mask = 0x1ff, 8649ecb8875SAjay Kumar Gupta .usb_bitmap = (0x1ff << 0), 8659ecb8875SAjay Kumar Gupta .drvvbus = 8, 8669ecb8875SAjay Kumar Gupta .txep_shift = 0, 8679ecb8875SAjay Kumar Gupta .txep_mask = 0xffff, 8689ecb8875SAjay Kumar Gupta .txep_bitmap = (0xffff << 0), 8699ecb8875SAjay Kumar Gupta .rxep_shift = 16, 8709ecb8875SAjay Kumar Gupta .rxep_mask = 0xfffe, 8719ecb8875SAjay Kumar Gupta .rxep_bitmap = (0xfffe << 16), 8729e204d88SFelipe Balbi .poll_timeout = 2000, /* ms */ 8739ecb8875SAjay Kumar Gupta }; 8749ecb8875SAjay Kumar Gupta 8752f82686eSBill Pemberton static const struct of_device_id musb_dsps_of_match[] = { 87665145677SAjay Kumar Gupta { .compatible = "ti,musb-am33xx", 8773e457371STony Lindgren .data = &am33xx_driver_data, }, 8783e457371STony Lindgren { .compatible = "ti,musb-dm816", 8793e457371STony Lindgren .data = &am33xx_driver_data, }, 8809ecb8875SAjay Kumar Gupta { }, 8819ecb8875SAjay Kumar Gupta }; 8829ecb8875SAjay Kumar Gupta MODULE_DEVICE_TABLE(of, musb_dsps_of_match); 8839ecb8875SAjay Kumar Gupta 8845b783983SWolfram Sang #ifdef CONFIG_PM_SLEEP 885869c5978SDaniel Mack static int dsps_suspend(struct device *dev) 886869c5978SDaniel Mack { 887869c5978SDaniel Mack struct dsps_glue *glue = dev_get_drvdata(dev); 888869c5978SDaniel Mack const struct dsps_musb_wrapper *wrp = glue->wrp; 889869c5978SDaniel Mack struct musb *musb = platform_get_drvdata(glue->musb); 890f042e9cbSSebastian Andrzej Siewior void __iomem *mbase; 891869c5978SDaniel Mack 892468bcc2aSFelipe Balbi del_timer_sync(&glue->timer); 893f042e9cbSSebastian Andrzej Siewior 894f042e9cbSSebastian Andrzej Siewior if (!musb) 895f042e9cbSSebastian Andrzej Siewior /* This can happen if the musb device is in -EPROBE_DEFER */ 896f042e9cbSSebastian Andrzej Siewior return 0; 897f042e9cbSSebastian Andrzej Siewior 898f042e9cbSSebastian Andrzej Siewior mbase = musb->ctrl_base; 899869c5978SDaniel Mack glue->context.control = dsps_readl(mbase, wrp->control); 900869c5978SDaniel Mack glue->context.epintr = dsps_readl(mbase, wrp->epintr_set); 901869c5978SDaniel Mack glue->context.coreintr = dsps_readl(mbase, wrp->coreintr_set); 902869c5978SDaniel Mack glue->context.phy_utmi = dsps_readl(mbase, wrp->phy_utmi); 903869c5978SDaniel Mack glue->context.mode = dsps_readl(mbase, wrp->mode); 904869c5978SDaniel Mack glue->context.tx_mode = dsps_readl(mbase, wrp->tx_mode); 905869c5978SDaniel Mack glue->context.rx_mode = dsps_readl(mbase, wrp->rx_mode); 906869c5978SDaniel Mack 907869c5978SDaniel Mack return 0; 908869c5978SDaniel Mack } 909869c5978SDaniel Mack 910869c5978SDaniel Mack static int dsps_resume(struct device *dev) 911869c5978SDaniel Mack { 912869c5978SDaniel Mack struct dsps_glue *glue = dev_get_drvdata(dev); 913869c5978SDaniel Mack const struct dsps_musb_wrapper *wrp = glue->wrp; 914869c5978SDaniel Mack struct musb *musb = platform_get_drvdata(glue->musb); 915f042e9cbSSebastian Andrzej Siewior void __iomem *mbase; 916869c5978SDaniel Mack 917f042e9cbSSebastian Andrzej Siewior if (!musb) 918f042e9cbSSebastian Andrzej Siewior return 0; 919f042e9cbSSebastian Andrzej Siewior 920f042e9cbSSebastian Andrzej Siewior mbase = musb->ctrl_base; 921869c5978SDaniel Mack dsps_writel(mbase, wrp->control, glue->context.control); 922869c5978SDaniel Mack dsps_writel(mbase, wrp->epintr_set, glue->context.epintr); 923869c5978SDaniel Mack dsps_writel(mbase, wrp->coreintr_set, glue->context.coreintr); 924869c5978SDaniel Mack dsps_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi); 925869c5978SDaniel Mack dsps_writel(mbase, wrp->mode, glue->context.mode); 926869c5978SDaniel Mack dsps_writel(mbase, wrp->tx_mode, glue->context.tx_mode); 927869c5978SDaniel Mack dsps_writel(mbase, wrp->rx_mode, glue->context.rx_mode); 928e47d9254SAntoine Tenart if (musb->xceiv->otg->state == OTG_STATE_B_IDLE && 92953185b3aSSebastian Andrzej Siewior musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) 9309e204d88SFelipe Balbi mod_timer(&glue->timer, jiffies + 9319e204d88SFelipe Balbi msecs_to_jiffies(wrp->poll_timeout)); 932869c5978SDaniel Mack 933869c5978SDaniel Mack return 0; 934869c5978SDaniel Mack } 935869c5978SDaniel Mack #endif 936869c5978SDaniel Mack 937869c5978SDaniel Mack static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume); 938869c5978SDaniel Mack 9399ecb8875SAjay Kumar Gupta static struct platform_driver dsps_usbss_driver = { 9409ecb8875SAjay Kumar Gupta .probe = dsps_probe, 9417690417dSBill Pemberton .remove = dsps_remove, 9429ecb8875SAjay Kumar Gupta .driver = { 9439ecb8875SAjay Kumar Gupta .name = "musb-dsps", 944869c5978SDaniel Mack .pm = &dsps_pm_ops, 945b432cb83SSachin Kamat .of_match_table = musb_dsps_of_match, 9469ecb8875SAjay Kumar Gupta }, 9479ecb8875SAjay Kumar Gupta }; 9489ecb8875SAjay Kumar Gupta 9499ecb8875SAjay Kumar Gupta MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer"); 9509ecb8875SAjay Kumar Gupta MODULE_AUTHOR("Ravi B <ravibabu@ti.com>"); 9519ecb8875SAjay Kumar Gupta MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>"); 9529ecb8875SAjay Kumar Gupta MODULE_LICENSE("GPL v2"); 9539ecb8875SAjay Kumar Gupta 95497238b35SSebastian Andrzej Siewior module_platform_driver(dsps_usbss_driver); 955