Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2010 ST-Ericsson AB
0004  * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
0005  *
0006  * Based on omap2430.c
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/kernel.h>
0011 #include <linux/clk.h>
0012 #include <linux/err.h>
0013 #include <linux/io.h>
0014 #include <linux/of.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/usb/musb-ux500.h>
0017 
0018 #include "musb_core.h"
0019 
0020 static const struct musb_hdrc_config ux500_musb_hdrc_config = {
0021     .multipoint = true,
0022     .dyn_fifo   = true,
0023     .num_eps    = 16,
0024     .ram_bits   = 16,
0025 };
0026 
0027 struct ux500_glue {
0028     struct device       *dev;
0029     struct platform_device  *musb;
0030     struct clk      *clk;
0031 };
0032 #define glue_to_musb(g) platform_get_drvdata(g->musb)
0033 
0034 static void ux500_musb_set_vbus(struct musb *musb, int is_on)
0035 {
0036     u8            devctl;
0037     unsigned long timeout = jiffies + msecs_to_jiffies(1000);
0038     /* HDRC controls CPEN, but beware current surges during device
0039      * connect.  They can trigger transient overcurrent conditions
0040      * that must be ignored.
0041      */
0042 
0043     devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
0044 
0045     if (is_on) {
0046         if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
0047             /* start the session */
0048             devctl |= MUSB_DEVCTL_SESSION;
0049             musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
0050             /*
0051              * Wait for the musb to set as A device to enable the
0052              * VBUS
0053              */
0054             while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
0055 
0056                 if (time_after(jiffies, timeout)) {
0057                     dev_err(musb->controller,
0058                     "configured as A device timeout");
0059                     break;
0060                 }
0061             }
0062 
0063         } else {
0064             musb->is_active = 1;
0065             musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
0066             devctl |= MUSB_DEVCTL_SESSION;
0067             MUSB_HST_MODE(musb);
0068         }
0069     } else {
0070         musb->is_active = 0;
0071 
0072         /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
0073          * right to B_IDLE...
0074          */
0075         devctl &= ~MUSB_DEVCTL_SESSION;
0076         MUSB_DEV_MODE(musb);
0077     }
0078     musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
0079 
0080     /*
0081      * Devctl values will be updated after vbus goes below
0082      * session_valid. The time taken depends on the capacitance
0083      * on VBUS line. The max discharge time can be upto 1 sec
0084      * as per the spec. Typically on our platform, it is 200ms
0085      */
0086     if (!is_on)
0087         mdelay(200);
0088 
0089     dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
0090         usb_otg_state_string(musb->xceiv->otg->state),
0091         musb_readb(musb->mregs, MUSB_DEVCTL));
0092 }
0093 
0094 static int musb_otg_notifications(struct notifier_block *nb,
0095         unsigned long event, void *unused)
0096 {
0097     struct musb *musb = container_of(nb, struct musb, nb);
0098 
0099     dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
0100             event, usb_otg_state_string(musb->xceiv->otg->state));
0101 
0102     switch (event) {
0103     case UX500_MUSB_ID:
0104         dev_dbg(musb->controller, "ID GND\n");
0105         ux500_musb_set_vbus(musb, 1);
0106         break;
0107     case UX500_MUSB_VBUS:
0108         dev_dbg(musb->controller, "VBUS Connect\n");
0109         break;
0110     case UX500_MUSB_NONE:
0111         dev_dbg(musb->controller, "VBUS Disconnect\n");
0112         if (is_host_active(musb))
0113             ux500_musb_set_vbus(musb, 0);
0114         else
0115             musb->xceiv->otg->state = OTG_STATE_B_IDLE;
0116         break;
0117     default:
0118         dev_dbg(musb->controller, "ID float\n");
0119         return NOTIFY_DONE;
0120     }
0121     return NOTIFY_OK;
0122 }
0123 
0124 static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
0125 {
0126     unsigned long   flags;
0127     irqreturn_t     retval = IRQ_NONE;
0128     struct musb     *musb = __hci;
0129 
0130     spin_lock_irqsave(&musb->lock, flags);
0131 
0132     musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
0133     musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
0134     musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
0135 
0136     if (musb->int_usb || musb->int_tx || musb->int_rx)
0137         retval = musb_interrupt(musb);
0138 
0139     spin_unlock_irqrestore(&musb->lock, flags);
0140 
0141     return retval;
0142 }
0143 
0144 static int ux500_musb_init(struct musb *musb)
0145 {
0146     int status;
0147 
0148     musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
0149     if (IS_ERR_OR_NULL(musb->xceiv)) {
0150         pr_err("HS USB OTG: no transceiver configured\n");
0151         return -EPROBE_DEFER;
0152     }
0153 
0154     musb->nb.notifier_call = musb_otg_notifications;
0155     status = usb_register_notifier(musb->xceiv, &musb->nb);
0156     if (status < 0) {
0157         dev_dbg(musb->controller, "notification register failed\n");
0158         return status;
0159     }
0160 
0161     musb->isr = ux500_musb_interrupt;
0162 
0163     return 0;
0164 }
0165 
0166 static int ux500_musb_exit(struct musb *musb)
0167 {
0168     usb_unregister_notifier(musb->xceiv, &musb->nb);
0169 
0170     usb_put_phy(musb->xceiv);
0171 
0172     return 0;
0173 }
0174 
0175 static const struct musb_platform_ops ux500_ops = {
0176     .quirks     = MUSB_DMA_UX500 | MUSB_INDEXED_EP,
0177 #ifdef CONFIG_USB_UX500_DMA
0178     .dma_init   = ux500_dma_controller_create,
0179     .dma_exit   = ux500_dma_controller_destroy,
0180 #endif
0181     .init       = ux500_musb_init,
0182     .exit       = ux500_musb_exit,
0183     .fifo_mode  = 5,
0184 
0185     .set_vbus   = ux500_musb_set_vbus,
0186 };
0187 
0188 static struct musb_hdrc_platform_data *
0189 ux500_of_probe(struct platform_device *pdev, struct device_node *np)
0190 {
0191     struct musb_hdrc_platform_data *pdata;
0192     const char *mode;
0193     int strlen;
0194 
0195     pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0196     if (!pdata)
0197         return NULL;
0198 
0199     mode = of_get_property(np, "dr_mode", &strlen);
0200     if (!mode) {
0201         dev_err(&pdev->dev, "No 'dr_mode' property found\n");
0202         return NULL;
0203     }
0204 
0205     if (strlen > 0) {
0206         if (!strcmp(mode, "host"))
0207             pdata->mode = MUSB_HOST;
0208         if (!strcmp(mode, "otg"))
0209             pdata->mode = MUSB_OTG;
0210         if (!strcmp(mode, "peripheral"))
0211             pdata->mode = MUSB_PERIPHERAL;
0212     }
0213 
0214     return pdata;
0215 }
0216 
0217 static int ux500_probe(struct platform_device *pdev)
0218 {
0219     struct musb_hdrc_platform_data  *pdata = dev_get_platdata(&pdev->dev);
0220     struct device_node      *np = pdev->dev.of_node;
0221     struct platform_device      *musb;
0222     struct ux500_glue       *glue;
0223     struct clk          *clk;
0224     int             ret = -ENOMEM;
0225 
0226     if (!pdata) {
0227         if (np) {
0228             pdata = ux500_of_probe(pdev, np);
0229             if (!pdata)
0230                 goto err0;
0231 
0232             pdev->dev.platform_data = pdata;
0233         } else {
0234             dev_err(&pdev->dev, "no pdata or device tree found\n");
0235             goto err0;
0236         }
0237     }
0238 
0239     glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
0240     if (!glue)
0241         goto err0;
0242 
0243     musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
0244     if (!musb) {
0245         dev_err(&pdev->dev, "failed to allocate musb device\n");
0246         goto err0;
0247     }
0248 
0249     clk = devm_clk_get(&pdev->dev, NULL);
0250     if (IS_ERR(clk)) {
0251         dev_err(&pdev->dev, "failed to get clock\n");
0252         ret = PTR_ERR(clk);
0253         goto err1;
0254     }
0255 
0256     ret = clk_prepare_enable(clk);
0257     if (ret) {
0258         dev_err(&pdev->dev, "failed to enable clock\n");
0259         goto err1;
0260     }
0261 
0262     musb->dev.parent        = &pdev->dev;
0263     musb->dev.dma_mask      = &pdev->dev.coherent_dma_mask;
0264     musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
0265     device_set_of_node_from_dev(&musb->dev, &pdev->dev);
0266 
0267     glue->dev           = &pdev->dev;
0268     glue->musb          = musb;
0269     glue->clk           = clk;
0270 
0271     pdata->platform_ops     = &ux500_ops;
0272     pdata->config           = &ux500_musb_hdrc_config;
0273 
0274     platform_set_drvdata(pdev, glue);
0275 
0276     ret = platform_device_add_resources(musb, pdev->resource, pdev->num_resources);
0277     if (ret) {
0278         dev_err(&pdev->dev, "failed to add resources\n");
0279         goto err2;
0280     }
0281 
0282     ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
0283     if (ret) {
0284         dev_err(&pdev->dev, "failed to add platform_data\n");
0285         goto err2;
0286     }
0287 
0288     ret = platform_device_add(musb);
0289     if (ret) {
0290         dev_err(&pdev->dev, "failed to register musb device\n");
0291         goto err2;
0292     }
0293 
0294     return 0;
0295 
0296 err2:
0297     clk_disable_unprepare(clk);
0298 
0299 err1:
0300     platform_device_put(musb);
0301 
0302 err0:
0303     return ret;
0304 }
0305 
0306 static int ux500_remove(struct platform_device *pdev)
0307 {
0308     struct ux500_glue   *glue = platform_get_drvdata(pdev);
0309 
0310     platform_device_unregister(glue->musb);
0311     clk_disable_unprepare(glue->clk);
0312 
0313     return 0;
0314 }
0315 
0316 #ifdef CONFIG_PM_SLEEP
0317 static int ux500_suspend(struct device *dev)
0318 {
0319     struct ux500_glue   *glue = dev_get_drvdata(dev);
0320     struct musb     *musb = glue_to_musb(glue);
0321 
0322     if (musb)
0323         usb_phy_set_suspend(musb->xceiv, 1);
0324 
0325     clk_disable_unprepare(glue->clk);
0326 
0327     return 0;
0328 }
0329 
0330 static int ux500_resume(struct device *dev)
0331 {
0332     struct ux500_glue   *glue = dev_get_drvdata(dev);
0333     struct musb     *musb = glue_to_musb(glue);
0334     int         ret;
0335 
0336     ret = clk_prepare_enable(glue->clk);
0337     if (ret) {
0338         dev_err(dev, "failed to enable clock\n");
0339         return ret;
0340     }
0341 
0342     if (musb)
0343         usb_phy_set_suspend(musb->xceiv, 0);
0344 
0345     return 0;
0346 }
0347 #endif
0348 
0349 static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
0350 
0351 static const struct of_device_id ux500_match[] = {
0352         { .compatible = "stericsson,db8500-musb", },
0353         {}
0354 };
0355 
0356 MODULE_DEVICE_TABLE(of, ux500_match);
0357 
0358 static struct platform_driver ux500_driver = {
0359     .probe      = ux500_probe,
0360     .remove     = ux500_remove,
0361     .driver     = {
0362         .name   = "musb-ux500",
0363         .pm = &ux500_pm_ops,
0364         .of_match_table = ux500_match,
0365     },
0366 };
0367 
0368 MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
0369 MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
0370 MODULE_LICENSE("GPL v2");
0371 module_platform_driver(ux500_driver);