e840a1d3676b1696bea4c71269c3da4c91c3b869
[firefly-linux-kernel-4.4.55.git] / drivers / usb / host / isp1760-core.c
1 /*
2  * Driver for the NXP ISP1760 chip
3  *
4  * Copyright 2014 Laurent Pinchart
5  * Copyright 2007 Sebastian Siewior
6  *
7  * Contacts:
8  *      Sebastian Siewior <bigeasy@linutronix.de>
9  *      Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  */
15
16 #include <linux/delay.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/usb.h>
23
24 #include "isp1760-core.h"
25 #include "isp1760-hcd.h"
26 #include "isp1760-regs.h"
27
28 static void isp1760_init_core(struct isp1760_device *isp)
29 {
30         u32 hwmode;
31
32         /* Low-level chip reset */
33         if (isp->rst_gpio) {
34                 gpiod_set_value_cansleep(isp->rst_gpio, 1);
35                 mdelay(50);
36                 gpiod_set_value_cansleep(isp->rst_gpio, 0);
37         }
38
39         /*
40          * Reset the host controller, including the CPU interface
41          * configuration.
42          */
43         isp1760_write32(isp->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
44         msleep(100);
45
46         /* Setup HW Mode Control: This assumes a level active-low interrupt */
47         hwmode = HW_DATA_BUS_32BIT;
48
49         if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_16)
50                 hwmode &= ~HW_DATA_BUS_32BIT;
51         if (isp->devflags & ISP1760_FLAG_ANALOG_OC)
52                 hwmode |= HW_ANA_DIGI_OC;
53         if (isp->devflags & ISP1760_FLAG_DACK_POL_HIGH)
54                 hwmode |= HW_DACK_POL_HIGH;
55         if (isp->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
56                 hwmode |= HW_DREQ_POL_HIGH;
57         if (isp->devflags & ISP1760_FLAG_INTR_POL_HIGH)
58                 hwmode |= HW_INTR_HIGH_ACT;
59         if (isp->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
60                 hwmode |= HW_INTR_EDGE_TRIG;
61
62         /*
63          * We have to set this first in case we're in 16-bit mode.
64          * Write it twice to ensure correct upper bits if switching
65          * to 16-bit mode.
66          */
67         isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
68         isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
69
70         dev_info(isp->dev, "bus width: %u, oc: %s\n",
71                  isp->devflags & ISP1760_FLAG_BUS_WIDTH_16 ? 16 : 32,
72                  isp->devflags & ISP1760_FLAG_ANALOG_OC ? "analog" : "digital");
73 }
74
75 int isp1760_register(struct resource *mem, int irq, unsigned long irqflags,
76                      struct device *dev, unsigned int devflags)
77 {
78         struct isp1760_device *isp;
79         int ret;
80
81         if (usb_disabled())
82                 return -ENODEV;
83
84         /* prevent usb-core allocating DMA pages */
85         dev->dma_mask = NULL;
86
87         isp = devm_kzalloc(dev, sizeof(*isp), GFP_KERNEL);
88         if (!isp)
89                 return -ENOMEM;
90
91         isp->dev = dev;
92         isp->devflags = devflags;
93
94         isp->rst_gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
95         if (IS_ERR(isp->rst_gpio))
96                 return PTR_ERR(isp->rst_gpio);
97
98         isp->regs = devm_ioremap_resource(dev, mem);
99         if (IS_ERR(isp->regs))
100                 return PTR_ERR(isp->regs);
101
102         isp1760_init_core(isp);
103
104         ret = isp1760_hcd_register(&isp->hcd, isp->regs, mem, irq,
105                                    irqflags | IRQF_SHARED, dev);
106         if (ret < 0)
107                 return ret;
108
109         dev_set_drvdata(dev, isp);
110
111         return 0;
112 }
113
114 void isp1760_unregister(struct device *dev)
115 {
116         struct isp1760_device *isp = dev_get_drvdata(dev);
117
118         isp1760_hcd_unregister(&isp->hcd);
119 }
120
121 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
122 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
123 MODULE_LICENSE("GPL v2");