File: /usr/src/linux/drivers/i2o/i2o_pci.c
1 /*
2 * Find I2O capable controllers on the PCI bus, and register/install
3 * them with the I2O layer
4 *
5 * (C) Copyright 1999 Red Hat Software
6 *
7 * Written by Alan Cox, Building Number Three Ltd
8 * Modified by Deepak Saxena <deepak@plexity.net>
9 * Modified by Boji T Kannanthanam <boji.t.kannanthanam@intel.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 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * TODO:
17 * Support polled I2O PCI controllers.
18 */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/pci.h>
24 #include <linux/i2o.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <asm/io.h>
29
30 #ifdef CONFIG_MTRR
31 #include <asm/mtrr.h>
32 #endif // CONFIG_MTRR
33
34 #ifdef MODULE
35 /*
36 * Core function table
37 * See <include/linux/i2o.h> for an explanation
38 */
39 static struct i2o_core_func_table *core;
40
41 /* Core attach function */
42 extern int i2o_pci_core_attach(struct i2o_core_func_table *);
43 extern void i2o_pci_core_detach(void);
44 #endif /* MODULE */
45
46 /*
47 * Free bus specific resources
48 */
49 static void i2o_pci_dispose(struct i2o_controller *c)
50 {
51 I2O_IRQ_WRITE32(c,0xFFFFFFFF);
52 if(c->bus.pci.irq > 0)
53 free_irq(c->bus.pci.irq, c);
54 iounmap(((u8 *)c->post_port)-0x40);
55
56 #ifdef CONFIG_MTRR
57 if(c->bus.pci.mtrr_reg0 > 0)
58 mtrr_del(c->bus.pci.mtrr_reg0, 0, 0);
59 if(c->bus.pci.mtrr_reg1 > 0)
60 mtrr_del(c->bus.pci.mtrr_reg1, 0, 0);
61 #endif
62 }
63
64 /*
65 * No real bus specific handling yet (note that later we will
66 * need to 'steal' PCI devices on i960 mainboards)
67 */
68
69 static int i2o_pci_bind(struct i2o_controller *c, struct i2o_device *dev)
70 {
71 MOD_INC_USE_COUNT;
72 return 0;
73 }
74
75 static int i2o_pci_unbind(struct i2o_controller *c, struct i2o_device *dev)
76 {
77 MOD_DEC_USE_COUNT;
78 return 0;
79 }
80
81 /*
82 * Bus specific enable/disable functions
83 */
84 static void i2o_pci_enable(struct i2o_controller *c)
85 {
86 I2O_IRQ_WRITE32(c, 0);
87 c->enabled = 1;
88 }
89
90 static void i2o_pci_disable(struct i2o_controller *c)
91 {
92 I2O_IRQ_WRITE32(c, 0xFFFFFFFF);
93 c->enabled = 0;
94 }
95
96 /*
97 * Bus specific interrupt handler
98 */
99
100 static void i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
101 {
102 struct i2o_controller *c = dev_id;
103 #ifdef MODULE
104 core->run_queue(c);
105 #else
106 i2o_run_queue(c);
107 #endif /* MODULE */
108 }
109
110 /*
111 * Install a PCI (or in theory AGP) i2o controller
112 *
113 * TODO: Add support for polled controllers
114 */
115 int __init i2o_pci_install(struct pci_dev *dev)
116 {
117 struct i2o_controller *c=kmalloc(sizeof(struct i2o_controller),
118 GFP_KERNEL);
119 u8 *mem;
120 u32 memptr = 0;
121 u32 size;
122
123 int i;
124
125 if(c==NULL)
126 {
127 printk(KERN_ERR "i2o: Insufficient memory to add controller.\n");
128 return -ENOMEM;
129 }
130 memset(c, 0, sizeof(*c));
131
132 for(i=0; i<6; i++)
133 {
134 /* Skip I/O spaces */
135 if(!(pci_resource_flags(dev, i) & IORESOURCE_IO))
136 {
137 memptr = pci_resource_start(dev, i);
138 break;
139 }
140 }
141
142 if(i==6)
143 {
144 printk(KERN_ERR "i2o: I2O controller has no memory regions defined.\n");
145 kfree(c);
146 return -EINVAL;
147 }
148
149 size = dev->resource[i].end-dev->resource[i].start+1;
150 /* Map the I2O controller */
151
152 printk(KERN_INFO "i2o: PCI I2O controller at 0x%08X size=%d\n", memptr, size);
153 mem = ioremap(memptr, size);
154 if(mem==NULL)
155 {
156 printk(KERN_ERR "i2o: Unable to map controller.\n");
157 kfree(c);
158 return -EINVAL;
159 }
160
161 c->bus.pci.irq = -1;
162 c->bus.pci.queue_buggy = 0;
163 c->bus.pci.dpt = 0;
164 c->bus.pci.short_req = 0;
165 c->bus.pci.pdev = dev;
166
167 c->irq_mask = (volatile u32 *)(mem+0x34);
168 c->post_port = (volatile u32 *)(mem+0x40);
169 c->reply_port = (volatile u32 *)(mem+0x44);
170
171 c->mem_phys = memptr;
172 c->mem_offset = (u32)mem;
173 c->destructor = i2o_pci_dispose;
174
175 c->bind = i2o_pci_bind;
176 c->unbind = i2o_pci_unbind;
177 c->bus_enable = i2o_pci_enable;
178 c->bus_disable = i2o_pci_disable;
179
180 c->type = I2O_TYPE_PCI;
181
182 /*
183 * Cards that fall apart if you hit them with large I/O
184 * loads...
185 */
186
187 if(dev->vendor == PCI_VENDOR_ID_NCR && dev->device == 0x0630)
188 {
189 c->bus.pci.short_req=1;
190 printk(KERN_INFO "I2O: Symbios FC920 workarounds activated.\n");
191 }
192 if(dev->subsystem_vendor == PCI_VENDOR_ID_PROMISE)
193 {
194 c->bus.pci.queue_buggy=1;
195 printk(KERN_INFO "I2O: Promise workarounds activated.\n");
196 }
197
198 /*
199 * Cards that go bananas if you quiesce them before you reset
200 * them
201 */
202
203 if(dev->vendor == PCI_VENDOR_ID_DPT)
204 c->bus.pci.dpt=1;
205
206 /*
207 * Enable Write Combining MTRR for IOP's memory region
208 */
209 #ifdef CONFIG_MTRR
210 c->bus.pci.mtrr_reg0 =
211 mtrr_add(c->mem_phys, size, MTRR_TYPE_WRCOMB, 1);
212 /*
213 * If it is an INTEL i960 I/O processor then set the first 64K to Uncacheable
214 * since the region contains the Messaging unit which shouldn't be cached.
215 */
216 c->bus.pci.mtrr_reg1 = -1;
217 if(dev->vendor == PCI_VENDOR_ID_INTEL || dev->vendor == PCI_VENDOR_ID_DPT)
218 {
219 printk(KERN_INFO "I2O: MTRR workaround for Intel i960 processor\n");
220 c->bus.pci.mtrr_reg1 = mtrr_add(c->mem_phys, 65536, MTRR_TYPE_UNCACHABLE, 1);
221 if(c->bus.pci.mtrr_reg1< 0)
222 printk(KERN_INFO "i2o_pci: Error in setting MTRR_TYPE_UNCACHABLE\n");
223 }
224
225 #endif
226
227 I2O_IRQ_WRITE32(c,0xFFFFFFFF);
228
229 #ifdef MODULE
230 i = core->install(c);
231 #else
232 i = i2o_install_controller(c);
233 #endif /* MODULE */
234
235 if(i<0)
236 {
237 printk(KERN_ERR "i2o: Unable to install controller.\n");
238 kfree(c);
239 iounmap(mem);
240 return i;
241 }
242
243 c->bus.pci.irq = dev->irq;
244 if(c->bus.pci.irq)
245 {
246 i=request_irq(dev->irq, i2o_pci_interrupt, SA_SHIRQ,
247 c->name, c);
248 if(i<0)
249 {
250 printk(KERN_ERR "%s: unable to allocate interrupt %d.\n",
251 c->name, dev->irq);
252 c->bus.pci.irq = -1;
253 #ifdef MODULE
254 core->delete(c);
255 #else
256 i2o_delete_controller(c);
257 #endif /* MODULE */
258 iounmap(mem);
259 return -EBUSY;
260 }
261 }
262
263 printk(KERN_INFO "%s: Installed at IRQ%d\n", c->name, dev->irq);
264 I2O_IRQ_WRITE32(c,0x0);
265 c->enabled = 1;
266 return 0;
267 }
268
269 int __init i2o_pci_scan(void)
270 {
271 struct pci_dev *dev;
272 int count=0;
273
274 printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
275
276 pci_for_each_dev(dev)
277 {
278 if((dev->class>>8)!=PCI_CLASS_INTELLIGENT_I2O)
279 continue;
280 if((dev->class&0xFF)>1)
281 {
282 printk(KERN_INFO "i2o: I2O Controller found but does not support I2O 1.5 (skipping).\n");
283 continue;
284 }
285 if (pci_enable_device(dev))
286 continue;
287 printk(KERN_INFO "i2o: I2O controller on bus %d at %d.\n",
288 dev->bus->number, dev->devfn);
289 pci_set_master(dev);
290 if(i2o_pci_install(dev)==0)
291 count++;
292 }
293 if(count)
294 printk(KERN_INFO "i2o: %d I2O controller%s found and installed.\n", count,
295 count==1?"":"s");
296 return count?count:-ENODEV;
297 }
298
299 #ifdef I2O_HOTPLUG_SUPPORT
300 /*
301 * Activate a newly found PCI I2O controller
302 * Not used now, but will be needed in future for
303 * hot plug PCI support
304 */
305 static void i2o_pci_activate(i2o_controller * c)
306 {
307 int i=0;
308 struct i2o_controller *c;
309
310 if(c->type == I2O_TYPE_PCI)
311 {
312 I2O_IRQ_WRITE32(c,0);
313 #ifdef MODULE
314 if(core->activate(c))
315 #else
316 if(i2o_activate_controller(c))
317 #endif /* MODULE */
318 {
319 printk("%s: Failed to initialize.\n", c->name);
320 #ifdef MODULE
321 core->unlock(c);
322 core->delete(c);
323 #else
324 i2o_unlock_controller(c);
325 i2o_delete_controller(c);
326 #endif
327 continue;
328 }
329 }
330 }
331 #endif // I2O_HOTPLUG_SUPPORT
332
333 #ifdef MODULE
334
335 int i2o_pci_core_attach(struct i2o_core_func_table *table)
336 {
337 MOD_INC_USE_COUNT;
338
339 core = table;
340
341 return i2o_pci_scan();
342 }
343
344 void i2o_pci_core_detach(void)
345 {
346 core = NULL;
347
348 MOD_DEC_USE_COUNT;
349 }
350
351 int init_module(void)
352 {
353 printk(KERN_INFO "Linux I2O PCI support (c) 1999 Red Hat Software.\n");
354
355 core = NULL;
356
357 return 0;
358
359 }
360
361 void cleanup_module(void)
362 {
363 }
364
365 EXPORT_SYMBOL(i2o_pci_core_attach);
366 EXPORT_SYMBOL(i2o_pci_core_detach);
367
368 MODULE_AUTHOR("Red Hat Software");
369 MODULE_DESCRIPTION("I2O PCI Interface");
370
371 #else
372 void __init i2o_pci_init(void)
373 {
374 printk(KERN_INFO "Linux I2O PCI support (c) 1999 Red Hat Software.\n");
375 i2o_pci_scan();
376 }
377 #endif
378