2 * PCI handling of I2O controller
4 * Copyright (C) 1999-2002 Red Hat Software
6 * Written by Alan Cox, Building Number Three Ltd
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * A lot of the I2O message side code from this is taken from the Red
14 * Creek RCPCI45 adapter driver by Red Creek Communications
18 * Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
19 * Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
20 * Deepak Saxena <deepak@plexity.net>
21 * Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
22 * Alan Cox <alan@lxorguk.ukuu.org.uk>:
23 * Ported to Linux 2.5.
24 * Markus Lidel <Markus.Lidel@shadowconnect.com>:
25 * Minor fixes for 2.6.
26 * Markus Lidel <Markus.Lidel@shadowconnect.com>:
27 * Support for sysfs included.
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/slab.h>
33 #include <linux/i2o.h>
34 #include <linux/module.h>
37 #define OSM_DESCRIPTION "I2O-subsystem"
39 /* PCI device id table for all I2O controllers */
40 static struct pci_device_id i2o_pci_ids
[] = {
41 {PCI_DEVICE_CLASS(PCI_CLASS_INTELLIGENT_I2O
<< 8, 0xffff00)},
42 {PCI_DEVICE(PCI_VENDOR_ID_DPT
, 0xa511)},
43 {.vendor
= PCI_VENDOR_ID_INTEL
,.device
= 0x1962,
44 .subvendor
= PCI_VENDOR_ID_PROMISE
,.subdevice
= PCI_ANY_ID
},
49 * i2o_pci_free - Frees the DMA memory for the I2O controller
50 * @c: I2O controller to free
52 * Remove all allocated DMA memory and unmap memory IO regions. If MTRR
53 * is enabled, also remove it again.
55 static void i2o_pci_free(struct i2o_controller
*c
)
61 i2o_dma_free(dev
, &c
->out_queue
);
62 i2o_dma_free(dev
, &c
->status_block
);
64 i2o_dma_free(dev
, &c
->dlct
);
65 i2o_dma_free(dev
, &c
->hrt
);
66 i2o_dma_free(dev
, &c
->status
);
68 if (c
->raptor
&& c
->in_queue
.virt
)
69 iounmap(c
->in_queue
.virt
);
72 iounmap(c
->base
.virt
);
74 pci_release_regions(c
->pdev
);
78 * i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
81 * Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
82 * IO mappings are also done here. If MTRR is enabled, also do add memory
85 * Returns 0 on success or negative error code on failure.
87 static int i2o_pci_alloc(struct i2o_controller
*c
)
89 struct pci_dev
*pdev
= c
->pdev
;
90 struct device
*dev
= &pdev
->dev
;
93 if (pci_request_regions(pdev
, OSM_DESCRIPTION
)) {
94 printk(KERN_ERR
"%s: device already claimed\n", c
->name
);
98 for (i
= 0; i
< 6; i
++) {
100 if (!(pci_resource_flags(pdev
, i
) & IORESOURCE_IO
)) {
102 c
->base
.phys
= pci_resource_start(pdev
, i
);
103 c
->base
.len
= pci_resource_len(pdev
, i
);
106 * If we know what card it is, set the size
107 * correctly. Code is taken from dpt_i2o.c
109 if (pdev
->device
== 0xa501) {
110 if (pdev
->subsystem_device
>= 0xc032 &&
111 pdev
->subsystem_device
<= 0xc03b) {
112 if (c
->base
.len
> 0x400000)
113 c
->base
.len
= 0x400000;
115 if (c
->base
.len
> 0x100000)
116 c
->base
.len
= 0x100000;
122 c
->in_queue
.phys
= pci_resource_start(pdev
, i
);
123 c
->in_queue
.len
= pci_resource_len(pdev
, i
);
130 printk(KERN_ERR
"%s: I2O controller has no memory regions"
131 " defined.\n", c
->name
);
136 /* Map the I2O controller */
138 printk(KERN_INFO
"%s: PCI I2O controller\n", c
->name
);
139 printk(KERN_INFO
" BAR0 at 0x%08lX size=%ld\n",
140 (unsigned long)c
->base
.phys
, (unsigned long)c
->base
.len
);
141 printk(KERN_INFO
" BAR1 at 0x%08lX size=%ld\n",
142 (unsigned long)c
->in_queue
.phys
,
143 (unsigned long)c
->in_queue
.len
);
145 printk(KERN_INFO
"%s: PCI I2O controller at %08lX size=%ld\n",
146 c
->name
, (unsigned long)c
->base
.phys
,
147 (unsigned long)c
->base
.len
);
149 c
->base
.virt
= ioremap_nocache(c
->base
.phys
, c
->base
.len
);
151 printk(KERN_ERR
"%s: Unable to map controller.\n", c
->name
);
158 ioremap_nocache(c
->in_queue
.phys
, c
->in_queue
.len
);
159 if (!c
->in_queue
.virt
) {
160 printk(KERN_ERR
"%s: Unable to map controller.\n",
166 c
->in_queue
= c
->base
;
168 c
->irq_status
= c
->base
.virt
+ I2O_IRQ_STATUS
;
169 c
->irq_mask
= c
->base
.virt
+ I2O_IRQ_MASK
;
170 c
->in_port
= c
->base
.virt
+ I2O_IN_PORT
;
171 c
->out_port
= c
->base
.virt
+ I2O_OUT_PORT
;
173 /* Motorola/Freescale chip does not follow spec */
174 if (pdev
->vendor
== PCI_VENDOR_ID_MOTOROLA
&& pdev
->device
== 0x18c0) {
175 /* Check if CPU is enabled */
176 if (be32_to_cpu(readl(c
->base
.virt
+ 0x10000)) & 0x10000000) {
177 printk(KERN_INFO
"%s: MPC82XX needs CPU running to "
178 "service I2O.\n", c
->name
);
182 c
->irq_status
+= I2O_MOTOROLA_PORT_OFFSET
;
183 c
->irq_mask
+= I2O_MOTOROLA_PORT_OFFSET
;
184 c
->in_port
+= I2O_MOTOROLA_PORT_OFFSET
;
185 c
->out_port
+= I2O_MOTOROLA_PORT_OFFSET
;
186 printk(KERN_INFO
"%s: MPC82XX workarounds activated.\n",
191 if (i2o_dma_alloc(dev
, &c
->status
, 8)) {
196 if (i2o_dma_alloc(dev
, &c
->hrt
, sizeof(i2o_hrt
))) {
201 if (i2o_dma_alloc(dev
, &c
->dlct
, 8192)) {
206 if (i2o_dma_alloc(dev
, &c
->status_block
, sizeof(i2o_status_block
))) {
211 if (i2o_dma_alloc(dev
, &c
->out_queue
,
212 I2O_MAX_OUTBOUND_MSG_FRAMES
* I2O_OUTBOUND_MSG_FRAME_SIZE
*
218 pci_set_drvdata(pdev
, c
);
224 * i2o_pci_interrupt - Interrupt handler for I2O controller
225 * @irq: interrupt line
226 * @dev_id: pointer to the I2O controller
228 * Handle an interrupt from a PCI based I2O controller. This turns out
229 * to be rather simple. We keep the controller pointer in the cookie.
231 static irqreturn_t
i2o_pci_interrupt(int irq
, void *dev_id
)
233 struct i2o_controller
*c
= dev_id
;
235 irqreturn_t rc
= IRQ_NONE
;
237 while (readl(c
->irq_status
) & I2O_IRQ_OUTBOUND_POST
) {
238 m
= readl(c
->out_port
);
239 if (m
== I2O_QUEUE_EMPTY
) {
241 * Old 960 steppings had a bug in the I2O unit that
242 * caused the queue to appear empty when it wasn't.
244 m
= readl(c
->out_port
);
245 if (unlikely(m
== I2O_QUEUE_EMPTY
))
250 if (i2o_driver_dispatch(c
, m
))
251 /* flush it if result != 0 */
252 i2o_flush_reply(c
, m
);
261 * i2o_pci_irq_enable - Allocate interrupt for I2O controller
262 * @c: i2o_controller that the request is for
264 * Allocate an interrupt for the I2O controller, and activate interrupts
265 * on the I2O controller.
267 * Returns 0 on success or negative error code on failure.
269 static int i2o_pci_irq_enable(struct i2o_controller
*c
)
271 struct pci_dev
*pdev
= c
->pdev
;
274 writel(0xffffffff, c
->irq_mask
);
277 rc
= request_irq(pdev
->irq
, i2o_pci_interrupt
, IRQF_SHARED
,
280 printk(KERN_ERR
"%s: unable to allocate interrupt %d."
281 "\n", c
->name
, pdev
->irq
);
286 writel(0x00000000, c
->irq_mask
);
288 printk(KERN_INFO
"%s: Installed at IRQ %d\n", c
->name
, pdev
->irq
);
294 * i2o_pci_irq_disable - Free interrupt for I2O controller
297 * Disable interrupts in I2O controller and then free interrupt.
299 static void i2o_pci_irq_disable(struct i2o_controller
*c
)
301 writel(0xffffffff, c
->irq_mask
);
303 if (c
->pdev
->irq
> 0)
304 free_irq(c
->pdev
->irq
, c
);
308 * i2o_pci_probe - Probe the PCI device for an I2O controller
309 * @pdev: PCI device to test
310 * @id: id which matched with the PCI device id table
312 * Probe the PCI device for any device which is a memory of the
313 * Intelligent, I2O class or an Adaptec Zero Channel Controller. We
314 * attempt to set up each such device and register it with the core.
316 * Returns 0 on success or negative error code on failure.
318 static int i2o_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
320 struct i2o_controller
*c
;
322 struct pci_dev
*i960
= NULL
;
324 printk(KERN_INFO
"i2o: Checking for PCI I2O controllers...\n");
326 if ((pdev
->class & 0xff) > 1) {
327 printk(KERN_WARNING
"i2o: %s does not support I2O 1.5 "
328 "(skipping).\n", pci_name(pdev
));
332 if ((rc
= pci_enable_device(pdev
))) {
333 printk(KERN_WARNING
"i2o: couldn't enable device %s\n",
338 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
339 printk(KERN_WARNING
"i2o: no suitable DMA found for %s\n",
345 pci_set_master(pdev
);
349 printk(KERN_ERR
"i2o: couldn't allocate memory for %s\n",
354 printk(KERN_INFO
"%s: controller found (%s)\n", c
->name
,
358 c
->device
.parent
= &pdev
->dev
;
360 /* Cards that fall apart if you hit them with large I/O loads... */
361 if (pdev
->vendor
== PCI_VENDOR_ID_NCR
&& pdev
->device
== 0x0630) {
363 printk(KERN_INFO
"%s: Symbios FC920 workarounds activated.\n",
367 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_PROMISE
) {
369 * Expose the ship behind i960 for initialization, or it will
372 i960
= pci_get_slot(c
->pdev
->bus
,
373 PCI_DEVFN(PCI_SLOT(c
->pdev
->devfn
), 0));
376 pci_write_config_word(i960
, 0x42, 0);
381 c
->limit_sectors
= 1;
384 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_DPT
)
387 /* Cards that go bananas if you quiesce them before you reset them. */
388 if (pdev
->vendor
== PCI_VENDOR_ID_DPT
) {
390 if (pdev
->device
== 0xa511)
393 if (pdev
->subsystem_device
== 0xc05a) {
394 c
->limit_sectors
= 1;
396 "%s: limit sectors per request to %d\n", c
->name
,
397 I2O_MAX_SECTORS_LIMITED
);
399 #ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64
400 if (sizeof(dma_addr_t
) > 4) {
401 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)))
402 printk(KERN_INFO
"%s: 64-bit DMA unavailable\n",
406 printk(KERN_INFO
"%s: using 64-bit DMA\n",
413 if ((rc
= i2o_pci_alloc(c
))) {
414 printk(KERN_ERR
"%s: DMA / IO allocation for I2O controller "
415 "failed\n", c
->name
);
416 goto free_controller
;
419 if (i2o_pci_irq_enable(c
)) {
420 printk(KERN_ERR
"%s: unable to enable interrupts for I2O "
421 "controller\n", c
->name
);
425 if ((rc
= i2o_iop_add(c
)))
429 pci_write_config_word(i960
, 0x42, 0x03ff);
434 i2o_pci_irq_disable(c
);
443 pci_disable_device(pdev
);
449 * i2o_pci_remove - Removes a I2O controller from the system
450 * @pdev: I2O controller which should be removed
452 * Reset the I2O controller, disable interrupts and remove all allocated
455 static void i2o_pci_remove(struct pci_dev
*pdev
)
457 struct i2o_controller
*c
;
458 c
= pci_get_drvdata(pdev
);
461 i2o_pci_irq_disable(c
);
464 pci_disable_device(pdev
);
466 printk(KERN_INFO
"%s: Controller removed.\n", c
->name
);
468 put_device(&c
->device
);
471 /* PCI driver for I2O controller */
472 static struct pci_driver i2o_pci_driver
= {
474 .id_table
= i2o_pci_ids
,
475 .probe
= i2o_pci_probe
,
476 .remove
= i2o_pci_remove
,
480 * i2o_pci_init - registers I2O PCI driver in PCI subsystem
482 * Returns > 0 on success or negative error code on failure.
484 int __init
i2o_pci_init(void)
486 return pci_register_driver(&i2o_pci_driver
);
490 * i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
492 void __exit
i2o_pci_exit(void)
494 pci_unregister_driver(&i2o_pci_driver
);
497 MODULE_DEVICE_TABLE(pci
, i2o_pci_ids
);