blob: 4cf7ca428b335ce049e86e27fcaa824ea65a607b [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
6 *
7 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
8 *
9 */
10
11#include <linux/usb.h>
12#include <linux/io.h>
13
14#include "../core/hcd.h"
15#include "isp1760-hcd.h"
16
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010017#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +020018#include <linux/of.h>
19#include <linux/of_platform.h>
20#endif
21
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010022#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +020023#include <linux/pci.h>
24#endif
25
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010026#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +020027static int of_isp1760_probe(struct of_device *dev,
28 const struct of_device_id *match)
29{
30 struct usb_hcd *hcd;
31 struct device_node *dp = dev->node;
32 struct resource *res;
33 struct resource memory;
34 struct of_irq oirq;
35 int virq;
36 u64 res_len;
37 int ret;
Nate Case3faefc82008-06-17 11:11:38 -050038 const unsigned int *prop;
39 unsigned int devflags = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020040
41 ret = of_address_to_resource(dp, 0, &memory);
42 if (ret)
43 return -ENXIO;
44
45 res = request_mem_region(memory.start, memory.end - memory.start + 1,
Kay Sievers7071a3c2008-05-02 06:02:41 +020046 dev_name(&dev->dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +020047 if (!res)
48 return -EBUSY;
49
50 res_len = memory.end - memory.start + 1;
51
52 if (of_irq_map_one(dp, 0, &oirq)) {
53 ret = -ENODEV;
54 goto release_reg;
55 }
56
57 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
58 oirq.size);
59
Nate Case3faefc82008-06-17 11:11:38 -050060 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
61 devflags |= ISP1760_FLAG_ISP1761;
62
Nate Case3faefc82008-06-17 11:11:38 -050063 /* Some systems wire up only 16 of the 32 data lines */
64 prop = of_get_property(dp, "bus-width", NULL);
65 if (prop && *prop == 16)
66 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
67
68 if (of_get_property(dp, "port1-otg", NULL) != NULL)
69 devflags |= ISP1760_FLAG_OTG_EN;
70
71 if (of_get_property(dp, "analog-oc", NULL) != NULL)
72 devflags |= ISP1760_FLAG_ANALOG_OC;
73
74 if (of_get_property(dp, "dack-polarity", NULL) != NULL)
75 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
76
77 if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
78 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
79
Sebastian Siewiordb11e472008-04-24 00:37:04 +020080 hcd = isp1760_register(memory.start, res_len, virq,
Nate Case3faefc82008-06-17 11:11:38 -050081 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
82 devflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +020083 if (IS_ERR(hcd)) {
84 ret = PTR_ERR(hcd);
85 goto release_reg;
86 }
87
88 dev_set_drvdata(&dev->dev, hcd);
89 return ret;
90
91release_reg:
92 release_mem_region(memory.start, memory.end - memory.start + 1);
93 return ret;
94}
95
96static int of_isp1760_remove(struct of_device *dev)
97{
98 struct usb_hcd *hcd = dev_get_drvdata(&dev->dev);
99
100 dev_set_drvdata(&dev->dev, NULL);
101
102 usb_remove_hcd(hcd);
103 iounmap(hcd->regs);
104 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
105 usb_put_hcd(hcd);
106 return 0;
107}
108
109static struct of_device_id of_isp1760_match[] = {
110 {
111 .compatible = "nxp,usb-isp1760",
112 },
Nate Case3faefc82008-06-17 11:11:38 -0500113 {
114 .compatible = "nxp,usb-isp1761",
115 },
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200116 { },
117};
118MODULE_DEVICE_TABLE(of, of_isp1760_match);
119
120static struct of_platform_driver isp1760_of_driver = {
121 .name = "nxp-isp1760",
122 .match_table = of_isp1760_match,
123 .probe = of_isp1760_probe,
124 .remove = of_isp1760_remove,
125};
126#endif
127
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100128#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200129static int __devinit isp1761_pci_probe(struct pci_dev *dev,
130 const struct pci_device_id *id)
131{
132 u8 latency, limit;
133 __u32 reg_data;
134 int retry_count;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200135 struct usb_hcd *hcd;
Nate Case3faefc82008-06-17 11:11:38 -0500136 unsigned int devflags = 0;
Karl Bongers6013bbb2008-12-01 11:47:40 +0100137 int ret_status = 0;
138
139 resource_size_t pci_mem_phy0;
140 resource_size_t memlength;
141
142 u8 __iomem *chip_addr;
143 u8 __iomem *iobase;
144 resource_size_t nxp_pci_io_base;
145 resource_size_t iolength;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200146
147 if (usb_disabled())
148 return -ENODEV;
149
150 if (pci_enable_device(dev) < 0)
151 return -ENODEV;
152
153 if (!dev->irq)
154 return -ENODEV;
155
156 /* Grab the PLX PCI mem maped port start address we need */
157 nxp_pci_io_base = pci_resource_start(dev, 0);
158 iolength = pci_resource_len(dev, 0);
159
160 if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
161 printk(KERN_ERR "request region #1\n");
162 return -EBUSY;
163 }
164
165 iobase = ioremap_nocache(nxp_pci_io_base, iolength);
166 if (!iobase) {
167 printk(KERN_ERR "ioremap #1\n");
Karl Bongers6013bbb2008-12-01 11:47:40 +0100168 ret_status = -ENOMEM;
169 goto cleanup1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200170 }
171 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
172 pci_mem_phy0 = pci_resource_start(dev, 3);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100173 memlength = pci_resource_len(dev, 3);
174 if (memlength < 0xffff) {
175 printk(KERN_ERR "memory length for this resource is wrong\n");
176 ret_status = -ENOMEM;
177 goto cleanup2;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200178 }
179
Karl Bongers6013bbb2008-12-01 11:47:40 +0100180 if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200181 printk(KERN_ERR "host controller already in use\n");
Karl Bongers6013bbb2008-12-01 11:47:40 +0100182 ret_status = -EBUSY;
183 goto cleanup2;
184 }
185
186 /* map available memory */
187 chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
188 if (!chip_addr) {
189 printk(KERN_ERR "Error ioremap failed\n");
190 ret_status = -ENOMEM;
191 goto cleanup3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200192 }
193
194 /* bad pci latencies can contribute to overruns */
195 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
196 if (latency) {
197 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
198 if (limit && limit < latency)
199 pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
200 }
201
202 /* Try to check whether we can access Scratch Register of
203 * Host Controller or not. The initial PCI access is retried until
204 * local init for the PCI bridge is completed
205 */
206 retry_count = 20;
207 reg_data = 0;
208 while ((reg_data != 0xFACE) && retry_count) {
209 /*by default host is in 16bit mode, so
210 * io operations at this stage must be 16 bit
211 * */
212 writel(0xface, chip_addr + HC_SCRATCH_REG);
213 udelay(100);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100214 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200215 retry_count--;
216 }
217
Karl Bongers6013bbb2008-12-01 11:47:40 +0100218 iounmap(chip_addr);
219
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200220 /* Host Controller presence is detected by writing to scratch register
221 * and reading back and checking the contents are same or not
222 */
223 if (reg_data != 0xFACE) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700224 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100225 ret_status = -ENOMEM;
226 goto cleanup3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200227 }
228
229 pci_set_master(dev);
230
Karl Bongers6013bbb2008-12-01 11:47:40 +0100231 /* configure PLX PCI chip to pass interrupts */
232#define PLX_INT_CSR_REG 0x68
233 reg_data = readl(iobase + PLX_INT_CSR_REG);
234 reg_data |= 0x900;
235 writel(reg_data, iobase + PLX_INT_CSR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200236
237 dev->dev.dma_mask = NULL;
Karl Bongers6013bbb2008-12-01 11:47:40 +0100238 hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
Nate Case3faefc82008-06-17 11:11:38 -0500239 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
240 devflags);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100241 if (IS_ERR(hcd)) {
242 ret_status = -ENODEV;
243 goto cleanup3;
Julien Brunelce5dee52008-09-24 18:00:36 +0200244 }
Karl Bongers6013bbb2008-12-01 11:47:40 +0100245
246 /* done with PLX IO access */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200247 iounmap(iobase);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200248 release_mem_region(nxp_pci_io_base, iolength);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100249
250 pci_set_drvdata(dev, hcd);
251 return 0;
252
253cleanup3:
254 release_mem_region(pci_mem_phy0, memlength);
255cleanup2:
256 iounmap(iobase);
257cleanup1:
258 release_mem_region(nxp_pci_io_base, iolength);
259 return ret_status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200260}
Karl Bongers6013bbb2008-12-01 11:47:40 +0100261
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200262static void isp1761_pci_remove(struct pci_dev *dev)
263{
264 struct usb_hcd *hcd;
265
266 hcd = pci_get_drvdata(dev);
267
268 usb_remove_hcd(hcd);
269 iounmap(hcd->regs);
270 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
271 usb_put_hcd(hcd);
272
273 pci_disable_device(dev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200274}
275
276static void isp1761_pci_shutdown(struct pci_dev *dev)
277{
278 printk(KERN_ERR "ips1761_pci_shutdown\n");
279}
280
Sebastian Andrzej Siewior6c073562008-11-30 16:50:04 +0100281static const struct pci_device_id isp1760_plx [] = {
282 {
283 .class = PCI_CLASS_BRIDGE_OTHER << 8,
284 .class_mask = ~0,
285 .vendor = PCI_VENDOR_ID_PLX,
286 .device = 0x5406,
287 .subvendor = PCI_VENDOR_ID_PLX,
288 .subdevice = 0x9054,
289 },
290 { }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200291};
292MODULE_DEVICE_TABLE(pci, isp1760_plx);
293
294static struct pci_driver isp1761_pci_driver = {
295 .name = "isp1760",
296 .id_table = isp1760_plx,
297 .probe = isp1761_pci_probe,
298 .remove = isp1761_pci_remove,
299 .shutdown = isp1761_pci_shutdown,
300};
301#endif
302
303static int __init isp1760_init(void)
304{
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100305 int ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200306
307 init_kmem_once();
308
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100309#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200310 ret = of_register_platform_driver(&isp1760_of_driver);
311 if (ret) {
312 deinit_kmem_cache();
313 return ret;
314 }
315#endif
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100316#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200317 ret = pci_register_driver(&isp1761_pci_driver);
318 if (ret)
319 goto unreg_of;
320#endif
321 return ret;
322
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100323#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200324unreg_of:
325#endif
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100326#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200327 of_unregister_platform_driver(&isp1760_of_driver);
328#endif
329 deinit_kmem_cache();
330 return ret;
331}
332module_init(isp1760_init);
333
334static void __exit isp1760_exit(void)
335{
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100336#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200337 of_unregister_platform_driver(&isp1760_of_driver);
338#endif
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100339#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200340 pci_unregister_driver(&isp1761_pci_driver);
341#endif
342 deinit_kmem_cache();
343}
344module_exit(isp1760_exit);