blob: 3a9b4f6d29cbd5ead39e0edb74c5ecca251c3b62 [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
Sebastian Siewiordb11e472008-04-24 00:37:04 +020021#include "isp1760-hcd.h"
22
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010023#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +020024#include <linux/pci.h>
25#endif
26
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010027#ifdef CONFIG_PCI
Bill Pemberton41ac7b32012-11-19 13:21:48 -050028static int isp1761_pci_probe(struct pci_dev *dev,
Sebastian Siewiordb11e472008-04-24 00:37:04 +020029 const struct pci_device_id *id)
30{
31 u8 latency, limit;
32 __u32 reg_data;
33 int retry_count;
Nate Case3faefc82008-06-17 11:11:38 -050034 unsigned int devflags = 0;
Karl Bongers6013bbb2008-12-01 11:47:40 +010035 int ret_status = 0;
36
37 resource_size_t pci_mem_phy0;
38 resource_size_t memlength;
39
40 u8 __iomem *chip_addr;
41 u8 __iomem *iobase;
42 resource_size_t nxp_pci_io_base;
43 resource_size_t iolength;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020044
45 if (usb_disabled())
46 return -ENODEV;
47
48 if (pci_enable_device(dev) < 0)
49 return -ENODEV;
50
51 if (!dev->irq)
52 return -ENODEV;
53
54 /* Grab the PLX PCI mem maped port start address we need */
55 nxp_pci_io_base = pci_resource_start(dev, 0);
56 iolength = pci_resource_len(dev, 0);
57
58 if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
59 printk(KERN_ERR "request region #1\n");
60 return -EBUSY;
61 }
62
63 iobase = ioremap_nocache(nxp_pci_io_base, iolength);
64 if (!iobase) {
65 printk(KERN_ERR "ioremap #1\n");
Karl Bongers6013bbb2008-12-01 11:47:40 +010066 ret_status = -ENOMEM;
67 goto cleanup1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020068 }
69 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
70 pci_mem_phy0 = pci_resource_start(dev, 3);
Karl Bongers6013bbb2008-12-01 11:47:40 +010071 memlength = pci_resource_len(dev, 3);
72 if (memlength < 0xffff) {
73 printk(KERN_ERR "memory length for this resource is wrong\n");
74 ret_status = -ENOMEM;
75 goto cleanup2;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020076 }
77
Karl Bongers6013bbb2008-12-01 11:47:40 +010078 if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +020079 printk(KERN_ERR "host controller already in use\n");
Karl Bongers6013bbb2008-12-01 11:47:40 +010080 ret_status = -EBUSY;
81 goto cleanup2;
82 }
83
84 /* map available memory */
85 chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
86 if (!chip_addr) {
87 printk(KERN_ERR "Error ioremap failed\n");
88 ret_status = -ENOMEM;
89 goto cleanup3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020090 }
91
92 /* bad pci latencies can contribute to overruns */
93 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
94 if (latency) {
95 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
96 if (limit && limit < latency)
97 pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
98 }
99
100 /* Try to check whether we can access Scratch Register of
101 * Host Controller or not. The initial PCI access is retried until
102 * local init for the PCI bridge is completed
103 */
104 retry_count = 20;
105 reg_data = 0;
106 while ((reg_data != 0xFACE) && retry_count) {
107 /*by default host is in 16bit mode, so
108 * io operations at this stage must be 16 bit
109 * */
110 writel(0xface, chip_addr + HC_SCRATCH_REG);
111 udelay(100);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100112 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200113 retry_count--;
114 }
115
Karl Bongers6013bbb2008-12-01 11:47:40 +0100116 iounmap(chip_addr);
117
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200118 /* Host Controller presence is detected by writing to scratch register
119 * and reading back and checking the contents are same or not
120 */
121 if (reg_data != 0xFACE) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700122 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100123 ret_status = -ENOMEM;
124 goto cleanup3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200125 }
126
127 pci_set_master(dev);
128
Karl Bongers6013bbb2008-12-01 11:47:40 +0100129 /* configure PLX PCI chip to pass interrupts */
130#define PLX_INT_CSR_REG 0x68
131 reg_data = readl(iobase + PLX_INT_CSR_REG);
132 reg_data |= 0x900;
133 writel(reg_data, iobase + PLX_INT_CSR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200134
135 dev->dev.dma_mask = NULL;
Laurent Pinchart4942e002015-01-21 00:55:51 +0200136 ret_status = isp1760_register(&dev->resource[3], dev->irq, IRQF_SHARED,
137 &dev->dev, devflags);
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +0200138 if (ret_status < 0)
Karl Bongers6013bbb2008-12-01 11:47:40 +0100139 goto cleanup3;
Karl Bongers6013bbb2008-12-01 11:47:40 +0100140
141 /* done with PLX IO access */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200142 iounmap(iobase);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200143 release_mem_region(nxp_pci_io_base, iolength);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100144
Karl Bongers6013bbb2008-12-01 11:47:40 +0100145 return 0;
146
147cleanup3:
148 release_mem_region(pci_mem_phy0, memlength);
149cleanup2:
150 iounmap(iobase);
151cleanup1:
152 release_mem_region(nxp_pci_io_base, iolength);
153 return ret_status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200154}
Karl Bongers6013bbb2008-12-01 11:47:40 +0100155
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200156static void isp1761_pci_remove(struct pci_dev *dev)
157{
Laurent Pinchart10c73f02015-01-21 00:55:45 +0200158 isp1760_unregister(&dev->dev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200159
160 pci_disable_device(dev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200161}
162
163static void isp1761_pci_shutdown(struct pci_dev *dev)
164{
165 printk(KERN_ERR "ips1761_pci_shutdown\n");
166}
167
Sebastian Andrzej Siewior6c073562008-11-30 16:50:04 +0100168static const struct pci_device_id isp1760_plx [] = {
169 {
170 .class = PCI_CLASS_BRIDGE_OTHER << 8,
171 .class_mask = ~0,
172 .vendor = PCI_VENDOR_ID_PLX,
173 .device = 0x5406,
174 .subvendor = PCI_VENDOR_ID_PLX,
175 .subdevice = 0x9054,
176 },
177 { }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200178};
179MODULE_DEVICE_TABLE(pci, isp1760_plx);
180
181static struct pci_driver isp1761_pci_driver = {
182 .name = "isp1760",
183 .id_table = isp1760_plx,
184 .probe = isp1761_pci_probe,
185 .remove = isp1761_pci_remove,
186 .shutdown = isp1761_pci_shutdown,
187};
188#endif
189
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500190static int isp1760_plat_probe(struct platform_device *pdev)
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000191{
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200192 unsigned long irqflags = IRQF_SHARED;
193 unsigned int devflags = 0;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000194 struct resource *mem_res;
195 struct resource *irq_res;
196 resource_size_t mem_size;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200197 int ret;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000198
199 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 if (!mem_res) {
201 pr_warning("isp1760: Memory resource not available\n");
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200202 return -ENODEV;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000203 }
204 mem_size = resource_size(mem_res);
205 if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
206 pr_warning("isp1760: Cannot reserve the memory resource\n");
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200207 return -EBUSY;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000208 }
209
210 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
211 if (!irq_res) {
212 pr_warning("isp1760: IRQ resource not available\n");
Libo Chen72d9c8b2013-05-09 12:58:09 +0800213 ret = -ENODEV;
214 goto cleanup;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000215 }
Libo Chen72d9c8b2013-05-09 12:58:09 +0800216
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000217 irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
218
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200219 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
220 struct device_node *dp = pdev->dev.of_node;
221 u32 bus_width = 0;
222
223 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400224 devflags |= ISP1760_FLAG_ISP1761;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200225
226 /* Some systems wire up only 16 of the 32 data lines */
227 of_property_read_u32(dp, "bus-width", &bus_width);
228 if (bus_width == 16)
Michael Hennerich9da69c62009-07-15 23:22:54 -0400229 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200230
231 if (of_property_read_bool(dp, "port1-otg"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400232 devflags |= ISP1760_FLAG_OTG_EN;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200233
234 if (of_property_read_bool(dp, "analog-oc"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400235 devflags |= ISP1760_FLAG_ANALOG_OC;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200236
237 if (of_property_read_bool(dp, "dack-polarity"))
Michael Hennerich9da69c62009-07-15 23:22:54 -0400238 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200239
240 if (of_property_read_bool(dp, "dreq-polarity"))
241 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
242 } else if (dev_get_platdata(&pdev->dev)) {
243 struct isp1760_platform_data *pdata =
244 dev_get_platdata(&pdev->dev);
245
246 if (pdata->is_isp1761)
247 devflags |= ISP1760_FLAG_ISP1761;
248 if (pdata->bus_width_16)
249 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
250 if (pdata->port1_otg)
251 devflags |= ISP1760_FLAG_OTG_EN;
252 if (pdata->analog_oc)
253 devflags |= ISP1760_FLAG_ANALOG_OC;
254 if (pdata->dack_polarity_high)
255 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
256 if (pdata->dreq_polarity_high)
Michael Hennerich9da69c62009-07-15 23:22:54 -0400257 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
258 }
259
Laurent Pinchart4942e002015-01-21 00:55:51 +0200260 ret = isp1760_register(mem_res, irq_res->start, irqflags, &pdev->dev,
261 devflags);
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +0200262 if (ret < 0)
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000263 goto cleanup;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000264
265 pr_info("ISP1760 USB device initialised\n");
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200266 return 0;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000267
268cleanup:
269 release_mem_region(mem_res->start, mem_size);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000270 return ret;
271}
272
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500273static int isp1760_plat_remove(struct platform_device *pdev)
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000274{
Laurent Pinchart10c73f02015-01-21 00:55:45 +0200275 isp1760_unregister(&pdev->dev);
Michael Grzeschik310e9e32012-04-05 14:56:07 +0200276
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000277 return 0;
278}
279
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200280#ifdef CONFIG_OF
281static const struct of_device_id isp1760_of_match[] = {
282 { .compatible = "nxp,usb-isp1760", },
283 { .compatible = "nxp,usb-isp1761", },
284 { },
285};
286MODULE_DEVICE_TABLE(of, isp1760_of_match);
287#endif
288
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000289static struct platform_driver isp1760_plat_driver = {
290 .probe = isp1760_plat_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500291 .remove = isp1760_plat_remove,
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000292 .driver = {
293 .name = "isp1760",
Laurent Pinchartc3cc40c2015-01-21 00:55:44 +0200294 .of_match_table = of_match_ptr(isp1760_of_match),
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000295 },
296};
297
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200298static int __init isp1760_init(void)
299{
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000300 int ret, any_ret = -ENODEV;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200301
Laurent Pinchart5a6356a2015-01-21 00:55:49 +0200302 isp1760_init_kmem_once();
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200303
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000304 ret = platform_driver_register(&isp1760_plat_driver);
305 if (!ret)
306 any_ret = 0;
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100307#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200308 ret = pci_register_driver(&isp1761_pci_driver);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000309 if (!ret)
310 any_ret = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200311#endif
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200312
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000313 if (any_ret)
Laurent Pinchart5a6356a2015-01-21 00:55:49 +0200314 isp1760_deinit_kmem_cache();
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000315 return any_ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200316}
317module_init(isp1760_init);
318
319static void __exit isp1760_exit(void)
320{
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000321 platform_driver_unregister(&isp1760_plat_driver);
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100322#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200323 pci_unregister_driver(&isp1761_pci_driver);
324#endif
Laurent Pinchart5a6356a2015-01-21 00:55:49 +0200325 isp1760_deinit_kmem_cache();
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200326}
327module_exit(isp1760_exit);