linux/drivers/usb/host/ohci-tmio.c
<<
>>
Prefs
   1/*
   2 * OHCI HCD(Host Controller Driver) for USB.
   3 *
   4 *(C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
   5 *(C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
   6 *(C) Copyright 2002 Hewlett-Packard Company
   7 *
   8 * Bus glue for Toshiba Mobile IO(TMIO) Controller's OHCI core
   9 * (C) Copyright 2005 Chris Humbert <mahadri-usb@drigon.com>
  10 * (C) Copyright 2007, 2008 Dmitry Baryshkov <dbaryshkov@gmail.com>
  11 *
  12 * This is known to work with the following variants:
  13 *      TC6393XB revision 3     (32kB SRAM)
  14 *
  15 * The TMIO's OHCI core DMAs through a small internal buffer that
  16 * is directly addressable by the CPU.
  17 *
  18 * Written from sparse documentation from Toshiba and Sharp's driver
  19 * for the 2.4 kernel,
  20 *      usb-ohci-tc6393.c(C) Copyright 2004 Lineo Solutions, Inc.
  21 *
  22 * This program is free software; you can redistribute it and/or modify
  23 * it under the terms of the GNU General Public License version 2 as
  24 * published by the Free Software Foundation.
  25 */
  26
  27/*#include <linux/fs.h>
  28#include <linux/mount.h>
  29#include <linux/pagemap.h>
  30#include <linux/init.h>
  31#include <linux/namei.h>
  32#include <linux/sched.h>*/
  33#include <linux/platform_device.h>
  34#include <linux/mfd/core.h>
  35#include <linux/mfd/tmio.h>
  36#include <linux/dma-mapping.h>
  37
  38/*-------------------------------------------------------------------------*/
  39
  40/*
  41 * USB Host Controller Configuration Register
  42 */
  43#define CCR_REVID       0x08    /* b Revision ID                                */
  44#define CCR_BASE        0x10    /* l USB Control Register Base Address Low      */
  45#define CCR_ILME        0x40    /* b Internal Local Memory Enable               */
  46#define CCR_PM          0x4c    /* w Power Management                   */
  47#define CCR_INTC        0x50    /* b INT Control                                */
  48#define CCR_LMW1L       0x54    /* w Local Memory Window 1 LMADRS Low   */
  49#define CCR_LMW1H       0x56    /* w Local Memory Window 1 LMADRS High  */
  50#define CCR_LMW1BL      0x58    /* w Local Memory Window 1 Base Address Low     */
  51#define CCR_LMW1BH      0x5A    /* w Local Memory Window 1 Base Address High    */
  52#define CCR_LMW2L       0x5C    /* w Local Memory Window 2 LMADRS Low   */
  53#define CCR_LMW2H       0x5E    /* w Local Memory Window 2 LMADRS High  */
  54#define CCR_LMW2BL      0x60    /* w Local Memory Window 2 Base Address Low     */
  55#define CCR_LMW2BH      0x62    /* w Local Memory Window 2 Base Address High    */
  56#define CCR_MISC        0xFC    /* b MISC                                       */
  57
  58#define CCR_PM_GKEN      0x0001
  59#define CCR_PM_CKRNEN    0x0002
  60#define CCR_PM_USBPW1    0x0004
  61#define CCR_PM_USBPW2    0x0008
  62#define CCR_PM_USBPW3    0x0008
  63#define CCR_PM_PMEE      0x0100
  64#define CCR_PM_PMES      0x8000
  65
  66/*-------------------------------------------------------------------------*/
  67
  68struct tmio_hcd {
  69        void __iomem            *ccr;
  70        spinlock_t              lock; /* protects RMW cycles */
  71};
  72
  73#define hcd_to_tmio(hcd)        ((struct tmio_hcd *)(hcd_to_ohci(hcd) + 1))
  74
  75/*-------------------------------------------------------------------------*/
  76
  77static void tmio_write_pm(struct platform_device *dev)
  78{
  79        struct usb_hcd *hcd = platform_get_drvdata(dev);
  80        struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  81        u16 pm;
  82        unsigned long flags;
  83
  84        spin_lock_irqsave(&tmio->lock, flags);
  85
  86        pm = CCR_PM_GKEN | CCR_PM_CKRNEN |
  87             CCR_PM_PMEE | CCR_PM_PMES;
  88
  89        tmio_iowrite16(pm, tmio->ccr + CCR_PM);
  90        spin_unlock_irqrestore(&tmio->lock, flags);
  91}
  92
  93static void tmio_stop_hc(struct platform_device *dev)
  94{
  95        struct usb_hcd *hcd = platform_get_drvdata(dev);
  96        struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  97        struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  98        u16 pm;
  99
 100        pm = CCR_PM_GKEN | CCR_PM_CKRNEN;
 101        switch (ohci->num_ports) {
 102                default:
 103                        dev_err(&dev->dev, "Unsupported amount of ports: %d\n", ohci->num_ports);
 104                case 3:
 105                        pm |= CCR_PM_USBPW3;
 106                case 2:
 107                        pm |= CCR_PM_USBPW2;
 108                case 1:
 109                        pm |= CCR_PM_USBPW1;
 110        }
 111        tmio_iowrite8(0, tmio->ccr + CCR_INTC);
 112        tmio_iowrite8(0, tmio->ccr + CCR_ILME);
 113        tmio_iowrite16(0, tmio->ccr + CCR_BASE);
 114        tmio_iowrite16(0, tmio->ccr + CCR_BASE + 2);
 115        tmio_iowrite16(pm, tmio->ccr + CCR_PM);
 116}
 117
 118static void tmio_start_hc(struct platform_device *dev)
 119{
 120        struct usb_hcd *hcd = platform_get_drvdata(dev);
 121        struct tmio_hcd *tmio = hcd_to_tmio(hcd);
 122        unsigned long base = hcd->rsrc_start;
 123
 124        tmio_write_pm(dev);
 125        tmio_iowrite16(base, tmio->ccr + CCR_BASE);
 126        tmio_iowrite16(base >> 16, tmio->ccr + CCR_BASE + 2);
 127        tmio_iowrite8(1, tmio->ccr + CCR_ILME);
 128        tmio_iowrite8(2, tmio->ccr + CCR_INTC);
 129
 130        dev_info(&dev->dev, "revision %d @ 0x%08llx, irq %d\n",
 131                        tmio_ioread8(tmio->ccr + CCR_REVID),
 132                        (u64) hcd->rsrc_start, hcd->irq);
 133}
 134
 135static int ohci_tmio_start(struct usb_hcd *hcd)
 136{
 137        struct ohci_hcd *ohci = hcd_to_ohci(hcd);
 138        int ret;
 139
 140        if ((ret = ohci_init(ohci)) < 0)
 141                return ret;
 142
 143        if ((ret = ohci_run(ohci)) < 0) {
 144                dev_err(hcd->self.controller, "can't start %s\n",
 145                        hcd->self.bus_name);
 146                ohci_stop(hcd);
 147                return ret;
 148        }
 149
 150        return 0;
 151}
 152
 153static const struct hc_driver ohci_tmio_hc_driver = {
 154        .description =          hcd_name,
 155        .product_desc =         "TMIO OHCI USB Host Controller",
 156        .hcd_priv_size =        sizeof(struct ohci_hcd) + sizeof (struct tmio_hcd),
 157
 158        /* generic hardware linkage */
 159        .irq =                  ohci_irq,
 160        .flags =                HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM,
 161
 162        /* basic lifecycle operations */
 163        .start =                ohci_tmio_start,
 164        .stop =                 ohci_stop,
 165        .shutdown =             ohci_shutdown,
 166
 167        /* managing i/o requests and associated device resources */
 168        .urb_enqueue =          ohci_urb_enqueue,
 169        .urb_dequeue =          ohci_urb_dequeue,
 170        .endpoint_disable =     ohci_endpoint_disable,
 171
 172        /* scheduling support */
 173        .get_frame_number =     ohci_get_frame,
 174
 175        /* root hub support */
 176        .hub_status_data =      ohci_hub_status_data,
 177        .hub_control =          ohci_hub_control,
 178#ifdef  CONFIG_PM
 179        .bus_suspend =          ohci_bus_suspend,
 180        .bus_resume =           ohci_bus_resume,
 181#endif
 182        .start_port_reset =     ohci_start_port_reset,
 183};
 184
 185/*-------------------------------------------------------------------------*/
 186static struct platform_driver ohci_hcd_tmio_driver;
 187
 188static int ohci_hcd_tmio_drv_probe(struct platform_device *dev)
 189{
 190        const struct mfd_cell *cell = mfd_get_cell(dev);
 191        struct resource *regs = platform_get_resource(dev, IORESOURCE_MEM, 0);
 192        struct resource *config = platform_get_resource(dev, IORESOURCE_MEM, 1);
 193        struct resource *sram = platform_get_resource(dev, IORESOURCE_MEM, 2);
 194        int irq = platform_get_irq(dev, 0);
 195        struct tmio_hcd *tmio;
 196        struct ohci_hcd *ohci;
 197        struct usb_hcd *hcd;
 198        int ret;
 199
 200        if (usb_disabled())
 201                return -ENODEV;
 202
 203        if (!cell)
 204                return -EINVAL;
 205
 206        hcd = usb_create_hcd(&ohci_tmio_hc_driver, &dev->dev, dev_name(&dev->dev));
 207        if (!hcd) {
 208                ret = -ENOMEM;
 209                goto err_usb_create_hcd;
 210        }
 211
 212        hcd->rsrc_start = regs->start;
 213        hcd->rsrc_len = resource_size(regs);
 214
 215        tmio = hcd_to_tmio(hcd);
 216
 217        spin_lock_init(&tmio->lock);
 218
 219        tmio->ccr = ioremap(config->start, resource_size(config));
 220        if (!tmio->ccr) {
 221                ret = -ENOMEM;
 222                goto err_ioremap_ccr;
 223        }
 224
 225        hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
 226        if (!hcd->regs) {
 227                ret = -ENOMEM;
 228                goto err_ioremap_regs;
 229        }
 230
 231        if (!dma_declare_coherent_memory(&dev->dev, sram->start,
 232                                sram->start,
 233                                resource_size(sram),
 234                                DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE)) {
 235                ret = -EBUSY;
 236                goto err_dma_declare;
 237        }
 238
 239        if (cell->enable) {
 240                ret = cell->enable(dev);
 241                if (ret)
 242                        goto err_enable;
 243        }
 244
 245        tmio_start_hc(dev);
 246        ohci = hcd_to_ohci(hcd);
 247        ohci_hcd_init(ohci);
 248
 249        ret = usb_add_hcd(hcd, irq, 0);
 250        if (ret)
 251                goto err_add_hcd;
 252
 253        if (ret == 0)
 254                return ret;
 255
 256        usb_remove_hcd(hcd);
 257
 258err_add_hcd:
 259        tmio_stop_hc(dev);
 260        if (cell->disable)
 261                cell->disable(dev);
 262err_enable:
 263        dma_release_declared_memory(&dev->dev);
 264err_dma_declare:
 265        iounmap(hcd->regs);
 266err_ioremap_regs:
 267        iounmap(tmio->ccr);
 268err_ioremap_ccr:
 269        usb_put_hcd(hcd);
 270err_usb_create_hcd:
 271
 272        return ret;
 273}
 274
 275static int ohci_hcd_tmio_drv_remove(struct platform_device *dev)
 276{
 277        struct usb_hcd *hcd = platform_get_drvdata(dev);
 278        struct tmio_hcd *tmio = hcd_to_tmio(hcd);
 279        const struct mfd_cell *cell = mfd_get_cell(dev);
 280
 281        usb_remove_hcd(hcd);
 282        tmio_stop_hc(dev);
 283        if (cell->disable)
 284                cell->disable(dev);
 285        dma_release_declared_memory(&dev->dev);
 286        iounmap(hcd->regs);
 287        iounmap(tmio->ccr);
 288        usb_put_hcd(hcd);
 289
 290        platform_set_drvdata(dev, NULL);
 291
 292        return 0;
 293}
 294
 295#ifdef CONFIG_PM
 296static int ohci_hcd_tmio_drv_suspend(struct platform_device *dev, pm_message_t state)
 297{
 298        const struct mfd_cell *cell = mfd_get_cell(dev);
 299        struct usb_hcd *hcd = platform_get_drvdata(dev);
 300        struct ohci_hcd *ohci = hcd_to_ohci(hcd);
 301        struct tmio_hcd *tmio = hcd_to_tmio(hcd);
 302        unsigned long flags;
 303        u8 misc;
 304        int ret;
 305
 306        if (time_before(jiffies, ohci->next_statechange))
 307                msleep(5);
 308        ohci->next_statechange = jiffies;
 309
 310        spin_lock_irqsave(&tmio->lock, flags);
 311
 312        misc = tmio_ioread8(tmio->ccr + CCR_MISC);
 313        misc |= 1 << 3; /* USSUSP */
 314        tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
 315
 316        spin_unlock_irqrestore(&tmio->lock, flags);
 317
 318        if (cell->suspend) {
 319                ret = cell->suspend(dev);
 320                if (ret)
 321                        return ret;
 322        }
 323        return 0;
 324}
 325
 326static int ohci_hcd_tmio_drv_resume(struct platform_device *dev)
 327{
 328        const struct mfd_cell *cell = mfd_get_cell(dev);
 329        struct usb_hcd *hcd = platform_get_drvdata(dev);
 330        struct ohci_hcd *ohci = hcd_to_ohci(hcd);
 331        struct tmio_hcd *tmio = hcd_to_tmio(hcd);
 332        unsigned long flags;
 333        u8 misc;
 334        int ret;
 335
 336        if (time_before(jiffies, ohci->next_statechange))
 337                msleep(5);
 338        ohci->next_statechange = jiffies;
 339
 340        if (cell->resume) {
 341                ret = cell->resume(dev);
 342                if (ret)
 343                        return ret;
 344        }
 345
 346        tmio_start_hc(dev);
 347
 348        spin_lock_irqsave(&tmio->lock, flags);
 349
 350        misc = tmio_ioread8(tmio->ccr + CCR_MISC);
 351        misc &= ~(1 << 3); /* USSUSP */
 352        tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
 353
 354        spin_unlock_irqrestore(&tmio->lock, flags);
 355
 356        ohci_resume(hcd, false);
 357
 358        return 0;
 359}
 360#else
 361#define ohci_hcd_tmio_drv_suspend NULL
 362#define ohci_hcd_tmio_drv_resume NULL
 363#endif
 364
 365static struct platform_driver ohci_hcd_tmio_driver = {
 366        .probe          = ohci_hcd_tmio_drv_probe,
 367        .remove         = ohci_hcd_tmio_drv_remove,
 368        .shutdown       = usb_hcd_platform_shutdown,
 369        .suspend        = ohci_hcd_tmio_drv_suspend,
 370        .resume         = ohci_hcd_tmio_drv_resume,
 371        .driver         = {
 372                .name   = "tmio-ohci",
 373                .owner  = THIS_MODULE,
 374        },
 375};
 376