blob: 9535b2872183468dcd4b063c3dec930e92a5551f [file] [log] [blame]
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001/*
2 * Glue code for the ISP1760 driver and bus
3 * Currently there is support for
4 * - OpenFirmware
5 * - PCI
Michael Hennerich9da69c62009-07-15 23:22:54 -04006 * - PDEV (generic platform device centralized driver model)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02007 *
8 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
9 *
10 */
11
12#include <linux/usb.h>
13#include <linux/io.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040014#include <linux/module.h>
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +020015#include <linux/of.h>
Catalin Marinasf7e7aa52009-02-10 16:55:51 +000016#include <linux/platform_device.h>
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +020017#include <linux/slab.h>
Michael Hennerich9da69c62009-07-15 23:22:54 -040018#include <linux/usb/isp1760.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020019#include <linux/usb/hcd.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020020
Laurent Pinchart4b1a5772015-01-21 00:55:57 +020021#include "isp1760-core.h"
Laurent Pincharte19c99e2015-01-21 00:55:56 +020022#include "isp1760-regs.h"
Sebastian Siewiordb11e472008-04-24 00:37:04 +020023
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010024#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +020025#include <linux/pci.h>
26#endif
27
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010028#ifdef CONFIG_PCI
Laurent Pinchartc770e002015-01-21 00:55:53 +020029static int isp1761_pci_init(struct pci_dev *dev)
Sebastian Siewiordb11e472008-04-24 00:37:04 +020030{
Laurent Pinchartc770e002015-01-21 00:55:53 +020031 resource_size_t mem_start;
32 resource_size_t mem_length;
Karl Bongers6013bbb2008-12-01 11:47:40 +010033 u8 __iomem *iobase;
Laurent Pinchartc770e002015-01-21 00:55:53 +020034 u8 latency, limit;
35 int retry_count;
36 u32 reg_data;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020037
Laurent Pinchartc770e002015-01-21 00:55:53 +020038 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
39 mem_start = pci_resource_start(dev, 3);
40 mem_length = pci_resource_len(dev, 3);
41 if (mem_length < 0xffff) {
42 printk(KERN_ERR "memory length for this resource is wrong\n");
43 return -ENOMEM;
44 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +020045
Laurent Pinchartc770e002015-01-21 00:55:53 +020046 if (!request_mem_region(mem_start, mem_length, "ISP-PCI")) {
47 printk(KERN_ERR "host controller already in use\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +020048 return -EBUSY;
49 }
50
Karl Bongers6013bbb2008-12-01 11:47:40 +010051 /* map available memory */
Laurent Pinchartc770e002015-01-21 00:55:53 +020052 iobase = ioremap_nocache(mem_start, mem_length);
53 if (!iobase) {
Karl Bongers6013bbb2008-12-01 11:47:40 +010054 printk(KERN_ERR "Error ioremap failed\n");
Laurent Pinchartc770e002015-01-21 00:55:53 +020055 release_mem_region(mem_start, mem_length);
56 return -ENOMEM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020057 }
58
59 /* bad pci latencies can contribute to overruns */
60 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
61 if (latency) {
62 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
63 if (limit && limit < latency)
64 pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
65 }
66
67 /* Try to check whether we can access Scratch Register of
68 * Host Controller or not. The initial PCI access is retried until
69 * local init for the PCI bridge is completed
70 */
71 retry_count = 20;
72 reg_data = 0;
73 while ((reg_data != 0xFACE) && retry_count) {
74 /*by default host is in 16bit mode, so
75 * io operations at this stage must be 16 bit
76 * */
Laurent Pinchartc770e002015-01-21 00:55:53 +020077 writel(0xface, iobase + HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +020078 udelay(100);
Laurent Pinchartc770e002015-01-21 00:55:53 +020079 reg_data = readl(iobase + HC_SCRATCH_REG) & 0x0000ffff;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020080 retry_count--;
81 }
82
Laurent Pinchartc770e002015-01-21 00:55:53 +020083 iounmap(iobase);
84 release_mem_region(mem_start, mem_length);
Karl Bongers6013bbb2008-12-01 11:47:40 +010085
Sebastian Siewiordb11e472008-04-24 00:37:04 +020086 /* Host Controller presence is detected by writing to scratch register
87 * and reading back and checking the contents are same or not
88 */
89 if (reg_data != 0xFACE) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -070090 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
Laurent Pinchartc770e002015-01-21 00:55:53 +020091 return -ENOMEM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020092 }
93
Laurent Pinchartc770e002015-01-21 00:55:53 +020094 /* Grab the PLX PCI mem maped port start address we need */
95 mem_start = pci_resource_start(dev, 0);
96 mem_length = pci_resource_len(dev, 0);
97
98 if (!request_mem_region(mem_start, mem_length, "ISP1761 IO MEM")) {
99 printk(KERN_ERR "request region #1\n");
100 return -EBUSY;
101 }
102
103 iobase = ioremap_nocache(mem_start, mem_length);
104 if (!iobase) {
105 printk(KERN_ERR "ioremap #1\n");
106 release_mem_region(mem_start, mem_length);
107 return -ENOMEM;
108 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200109
Karl Bongers6013bbb2008-12-01 11:47:40 +0100110 /* configure PLX PCI chip to pass interrupts */
111#define PLX_INT_CSR_REG 0x68
112 reg_data = readl(iobase + PLX_INT_CSR_REG);
113 reg_data |= 0x900;
114 writel(reg_data, iobase + PLX_INT_CSR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200115
Karl Bongers6013bbb2008-12-01 11:47:40 +0100116 /* done with PLX IO access */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200117 iounmap(iobase);
Laurent Pinchartc770e002015-01-21 00:55:53 +0200118 release_mem_region(mem_start, mem_length);
119
120 return 0;
121}
122
123static int isp1761_pci_probe(struct pci_dev *dev,
124 const struct pci_device_id *id)
125{
126 unsigned int devflags = 0;
127 int ret;
128
Laurent Pinchartc770e002015-01-21 00:55:53 +0200129 if (!dev->irq)
130 return -ENODEV;
131
132 if (pci_enable_device(dev) < 0)
133 return -ENODEV;
134
135 ret = isp1761_pci_init(dev);
136 if (ret < 0)
137 goto error;
138
139 pci_set_master(dev);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100140
Laurent Pinchart10feee12015-01-21 00:55:52 +0200141 dev->dev.dma_mask = NULL;
Laurent Pinchart667c45c2015-01-21 00:55:58 +0200142 ret = isp1760_register(&dev->resource[3], dev->irq, 0, &dev->dev,
143 devflags);
Laurent Pinchartc770e002015-01-21 00:55:53 +0200144 if (ret < 0)
145 goto error;
Karl Bongers6013bbb2008-12-01 11:47:40 +0100146
Laurent Pinchartc770e002015-01-21 00:55:53 +0200147 return 0;
148
149error:
150 pci_disable_device(dev);
151 return ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200152}
Karl Bongers6013bbb2008-12-01 11:47:40 +0100153
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200154static void isp1761_pci_remove(struct pci_dev *dev)
155{
Laurent Pinchart10c73f02015-01-21 00:55:45 +0200156 isp1760_unregister(&dev->dev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200157
158 pci_disable_device(dev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200159}
160
161static void isp1761_pci_shutdown(struct pci_dev *dev)
162{
163 printk(KERN_ERR "ips1761_pci_shutdown\n");
164}
165
Sandhya Bankar2b320d82016-05-04 07:45:37 +0530166static const struct pci_device_id isp1760_plx[] = {
Sebastian Andrzej Siewior6c073562008-11-30 16:50:04 +0100167 {
168 .class = PCI_CLASS_BRIDGE_OTHER << 8,
169 .class_mask = ~0,
170 .vendor = PCI_VENDOR_ID_PLX,
171 .device = 0x5406,
172 .subvendor = PCI_VENDOR_ID_PLX,
173 .subdevice = 0x9054,
174 },
175 { }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200176};
177MODULE_DEVICE_TABLE(pci, isp1760_plx);
178
179static struct pci_driver isp1761_pci_driver = {
180 .name = "isp1760",
181 .id_table = isp1760_plx,
182 .probe = isp1761_pci_probe,
183 .remove = isp1761_pci_remove,
184 .shutdown = isp1761_pci_shutdown,
185};
186#endif
187
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500188static int isp1760_plat_probe(struct platform_device *pdev)
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000189{
Laurent Pinchart667c45c2015-01-21 00:55:58 +0200190 unsigned long irqflags;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200191 unsigned int devflags = 0;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000192 struct resource *mem_res;
193 struct resource *irq_res;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200194 int ret;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000195
196 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000197
198 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
199 if (!irq_res) {
200 pr_warning("isp1760: IRQ resource not available\n");
Laurent Pinchart10feee12015-01-21 00:55:52 +0200201 return -ENODEV;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000202 }
Laurent Pinchart667c45c2015-01-21 00:55:58 +0200203 irqflags = irq_res->flags & IRQF_TRIGGER_MASK;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000204
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200205 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
206 struct device_node *dp = pdev->dev.of_node;
207 u32 bus_width = 0;
208
209 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400210 devflags |= ISP1760_FLAG_ISP1761;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200211
212 /* Some systems wire up only 16 of the 32 data lines */
213 of_property_read_u32(dp, "bus-width", &bus_width);
214 if (bus_width == 16)
Michael Hennerich9da69c62009-07-15 23:22:54 -0400215 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200216
217 if (of_property_read_bool(dp, "port1-otg"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400218 devflags |= ISP1760_FLAG_OTG_EN;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200219
220 if (of_property_read_bool(dp, "analog-oc"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400221 devflags |= ISP1760_FLAG_ANALOG_OC;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200222
223 if (of_property_read_bool(dp, "dack-polarity"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400224 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200225
226 if (of_property_read_bool(dp, "dreq-polarity"))
227 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
228 } else if (dev_get_platdata(&pdev->dev)) {
229 struct isp1760_platform_data *pdata =
230 dev_get_platdata(&pdev->dev);
231
232 if (pdata->is_isp1761)
233 devflags |= ISP1760_FLAG_ISP1761;
234 if (pdata->bus_width_16)
235 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
236 if (pdata->port1_otg)
237 devflags |= ISP1760_FLAG_OTG_EN;
238 if (pdata->analog_oc)
239 devflags |= ISP1760_FLAG_ANALOG_OC;
240 if (pdata->dack_polarity_high)
241 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
242 if (pdata->dreq_polarity_high)
Michael Hennerich9da69c62009-07-15 23:22:54 -0400243 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
244 }
245
Laurent Pinchart4942e002015-01-21 00:55:51 +0200246 ret = isp1760_register(mem_res, irq_res->start, irqflags, &pdev->dev,
247 devflags);
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +0200248 if (ret < 0)
Laurent Pinchart10feee12015-01-21 00:55:52 +0200249 return ret;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000250
251 pr_info("ISP1760 USB device initialised\n");
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200252 return 0;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000253}
254
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500255static int isp1760_plat_remove(struct platform_device *pdev)
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000256{
Laurent Pinchart10c73f02015-01-21 00:55:45 +0200257 isp1760_unregister(&pdev->dev);
Michael Grzeschik310e9e32012-04-05 14:56:07 +0200258
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000259 return 0;
260}
261
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200262#ifdef CONFIG_OF
263static const struct of_device_id isp1760_of_match[] = {
264 { .compatible = "nxp,usb-isp1760", },
265 { .compatible = "nxp,usb-isp1761", },
266 { },
267};
268MODULE_DEVICE_TABLE(of, isp1760_of_match);
269#endif
270
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000271static struct platform_driver isp1760_plat_driver = {
272 .probe = isp1760_plat_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500273 .remove = isp1760_plat_remove,
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000274 .driver = {
275 .name = "isp1760",
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200276 .of_match_table = of_match_ptr(isp1760_of_match),
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000277 },
278};
279
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200280static int __init isp1760_init(void)
281{
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000282 int ret, any_ret = -ENODEV;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200283
Laurent Pinchart5a6356a2015-01-21 00:55:49 +0200284 isp1760_init_kmem_once();
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200285
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000286 ret = platform_driver_register(&isp1760_plat_driver);
287 if (!ret)
288 any_ret = 0;
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100289#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200290 ret = pci_register_driver(&isp1761_pci_driver);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000291 if (!ret)
292 any_ret = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200293#endif
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200294
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000295 if (any_ret)
Laurent Pinchart5a6356a2015-01-21 00:55:49 +0200296 isp1760_deinit_kmem_cache();
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000297 return any_ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200298}
299module_init(isp1760_init);
300
301static void __exit isp1760_exit(void)
302{
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000303 platform_driver_unregister(&isp1760_plat_driver);
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100304#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200305 pci_unregister_driver(&isp1761_pci_driver);
306#endif
Laurent Pinchart5a6356a2015-01-21 00:55:49 +0200307 isp1760_deinit_kmem_cache();
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200308}
309module_exit(isp1760_exit);