2 * CompactPCI Hot Plug Driver PCI functions
4 * Copyright (C) 2002 by SOMA Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Send feedback to <scottm@somanetworks.com>
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/pci.h>
30 #include <linux/proc_fs.h>
32 #include "pci_hotplug.h"
33 #include "cpci_hotplug.h"
35 #if !defined(CONFIG_HOTPLUG_CPCI_MODULE)
36 #define MY_NAME "cpci_hotplug"
38 #define MY_NAME THIS_MODULE->name
41 extern int cpci_debug;
43 #define dbg(format, arg...) \
46 printk (KERN_DEBUG "%s: " format "\n", \
49 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
50 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
51 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
53 #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
56 u8 cpci_get_attention_status(struct slot* slot)
61 hs_cap = pci_bus_find_capability(slot->bus,
68 if(pci_bus_read_config_word(slot->bus,
74 return hs_csr & 0x0008 ? 1 : 0;
77 int cpci_set_attention_status(struct slot* slot, int status)
82 hs_cap = pci_bus_find_capability(slot->bus,
89 if(pci_bus_read_config_word(slot->bus,
98 hs_csr &= ~HS_CSR_LOO;
100 if(pci_bus_write_config_word(slot->bus,
109 u16 cpci_get_hs_csr(struct slot* slot)
114 hs_cap = pci_bus_find_capability(slot->bus,
121 if(pci_bus_read_config_word(slot->bus,
130 u16 cpci_set_hs_csr(struct slot* slot, u16 hs_csr)
135 hs_cap = pci_bus_find_capability(slot->bus,
142 /* Write out the new value */
143 if(pci_bus_write_config_word(slot->bus,
150 /* Read back what we just wrote out */
151 if(pci_bus_read_config_word(slot->bus,
160 int cpci_check_and_clear_ins(struct slot* slot)
166 hs_cap = pci_bus_find_capability(slot->bus,
172 if(pci_bus_read_config_word(slot->bus,
178 if(hs_csr & HS_CSR_INS) {
179 /* Clear INS (by setting it) */
180 if(pci_bus_write_config_word(slot->bus,
191 int cpci_check_ext(struct slot* slot)
197 hs_cap = pci_bus_find_capability(slot->bus,
203 if(pci_bus_read_config_word(slot->bus,
209 if(hs_csr & HS_CSR_EXT) {
215 int cpci_clear_ext(struct slot* slot)
220 hs_cap = pci_bus_find_capability(slot->bus,
226 if(pci_bus_read_config_word(slot->bus,
232 if(hs_csr & HS_CSR_EXT) {
233 /* Clear EXT (by setting it) */
234 if(pci_bus_write_config_word(slot->bus,
244 int cpci_led_on(struct slot* slot)
249 hs_cap = pci_bus_find_capability(slot->bus,
255 if(pci_bus_read_config_word(slot->bus,
261 if((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
263 hs_csr |= HS_CSR_LOO;
264 if(pci_bus_write_config_word(slot->bus,
268 err("Could not set LOO for slot %s",
269 slot->hotplug_slot->name);
276 int cpci_led_off(struct slot* slot)
281 hs_cap = pci_bus_find_capability(slot->bus,
287 if(pci_bus_read_config_word(slot->bus,
293 if(hs_csr & HS_CSR_LOO) {
295 hs_csr &= ~HS_CSR_LOO;
296 if(pci_bus_write_config_word(slot->bus,
300 err("Could not clear LOO for slot %s",
301 slot->hotplug_slot->name);
310 * Device configuration functions
313 static int cpci_configure_dev(struct pci_bus *bus, struct pci_dev *dev)
318 dbg("%s - enter", __FUNCTION__);
320 /* NOTE: device already setup from prior scan */
322 /* FIXME: How would we know if we need to enable the expansion ROM? */
323 pci_write_config_word(dev, PCI_ROM_ADDRESS, 0x00L);
325 /* Assign resources */
326 dbg("assigning resources for %02x:%02x.%x",
327 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
328 for (r = 0; r < 6; r++) {
329 struct resource *res = dev->resource + r;
331 pci_assign_resource(dev, r);
333 dbg("finished assigning resources for %02x:%02x.%x",
334 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
336 /* Does this function have an interrupt at all? */
337 dbg("checking for function interrupt");
338 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
340 dbg("function uses interrupt pin %d", irq_pin);
344 * Need to explicitly set irq field to 0 so that it'll get assigned
345 * by the pcibios platform dependent code called by pci_enable_device.
349 dbg("enabling device");
350 pci_enable_device(dev); /* XXX check return */
351 dbg("now dev->irq = %d", dev->irq);
352 if(irq_pin && dev->irq) {
353 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
356 /* Can't use pci_insert_device at the moment, do it manually for now */
357 pci_proc_attach_device(dev);
358 dbg("notifying drivers");
359 //pci_announce_device_to_drivers(dev);
360 dbg("%s - exit", __FUNCTION__);
364 static int cpci_configure_bridge(struct pci_bus* bus, struct pci_dev* dev)
367 struct pci_bus* child;
372 dbg("%s - enter", __FUNCTION__);
374 /* Do basic bridge initialization */
375 rc = pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x40);
377 printk(KERN_ERR "%s - write of PCI_LATENCY_TIMER failed\n", __FUNCTION__);
379 rc = pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 0x40);
381 printk(KERN_ERR "%s - write of PCI_SEC_LATENCY_TIMER failed\n", __FUNCTION__);
383 rc = pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, L1_CACHE_BYTES / 4);
385 printk(KERN_ERR "%s - write of PCI_CACHE_LINE_SIZE failed\n", __FUNCTION__);
389 * Set parent bridge's subordinate field so that configuration space
390 * access will work in pci_scan_bridge and friends.
392 max = pci_max_busnr();
393 bus->subordinate = max + 1;
394 pci_write_config_byte(bus->self, PCI_SUBORDINATE_BUS, max + 1);
396 /* Scan behind bridge */
397 n = pci_scan_bridge(bus, dev, max, 2);
398 child = pci_find_bus(0, max + 1);
401 pci_proc_attach_bus(child);
404 * Update parent bridge's subordinate field if there were more bridges
405 * behind the bridge that was scanned.
408 bus->subordinate = n;
409 pci_write_config_byte(bus->self, PCI_SUBORDINATE_BUS, n);
413 * Update the bridge resources of the bridge to accommodate devices
416 pci_bus_size_bridges(child);
417 pci_bus_assign_resources(child);
419 /* Enable resource mapping via command register */
420 command = PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
421 r = child->resource[0];
423 command |= PCI_COMMAND_IO;
425 r = child->resource[1];
427 command |= PCI_COMMAND_MEMORY;
429 r = child->resource[2];
431 command |= PCI_COMMAND_MEMORY;
433 rc = pci_write_config_word(dev, PCI_COMMAND, command);
435 err("Error setting command register");
439 /* Set bridge control register */
440 command = PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR | PCI_BRIDGE_CTL_NO_ISA;
441 rc = pci_write_config_word(dev, PCI_BRIDGE_CONTROL, command);
443 err("Error setting bridge control register");
446 dbg("%s - exit", __FUNCTION__);
450 static int configure_visit_pci_dev(struct pci_dev_wrapped *wrapped_dev,
451 struct pci_bus_wrapped *wrapped_bus)
454 struct pci_dev *dev = wrapped_dev->dev;
455 struct pci_bus *bus = wrapped_bus->bus;
458 dbg("%s - enter", __FUNCTION__);
461 * We need to fix up the hotplug representation with the Linux
464 if(wrapped_dev->data) {
465 slot = (struct slot*) wrapped_dev->data;
469 /* If it's a bridge, scan behind it for devices */
470 if(dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
471 rc = cpci_configure_bridge(bus, dev);
476 /* Actually configure device */
478 rc = cpci_configure_dev(bus, dev);
482 dbg("%s - exit", __FUNCTION__);
486 static int unconfigure_visit_pci_dev_phase2(struct pci_dev_wrapped *wrapped_dev,
487 struct pci_bus_wrapped *wrapped_bus)
489 struct pci_dev *dev = wrapped_dev->dev;
492 dbg("%s - enter", __FUNCTION__);
496 /* Remove the Linux representation */
497 if(pci_remove_device_safe(dev)) {
498 err("Could not remove device\n");
503 * Now remove the hotplug representation.
505 if(wrapped_dev->data) {
506 slot = (struct slot*) wrapped_dev->data;
509 dbg("No hotplug representation for %02x:%02x.%x",
510 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
512 dbg("%s - exit", __FUNCTION__);
516 static int unconfigure_visit_pci_bus_phase2(struct pci_bus_wrapped *wrapped_bus,
517 struct pci_dev_wrapped *wrapped_dev)
519 struct pci_bus *bus = wrapped_bus->bus;
520 struct pci_bus *parent = bus->self->bus;
522 dbg("%s - enter", __FUNCTION__);
524 /* The cleanup code for proc entries regarding buses should be in the kernel... */
526 dbg("detach_pci_bus %s", bus->procdir->name);
527 pci_proc_detach_bus(bus);
529 /* The cleanup code should live in the kernel... */
530 bus->self->subordinate = NULL;
532 /* unlink from parent bus */
533 list_del(&bus->node);
539 /* Update parent's subordinate field */
541 u8 n = pci_bus_max_busnr(parent);
542 if(n < parent->subordinate) {
543 parent->subordinate = n;
544 pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, n);
547 dbg("%s - exit", __FUNCTION__);
551 static struct pci_visit configure_functions = {
552 .visit_pci_dev = configure_visit_pci_dev,
555 static struct pci_visit unconfigure_functions_phase2 = {
556 .post_visit_pci_bus = unconfigure_visit_pci_bus_phase2,
557 .post_visit_pci_dev = unconfigure_visit_pci_dev_phase2
561 int cpci_configure_slot(struct slot* slot)
565 dbg("%s - enter", __FUNCTION__);
567 if(slot->dev == NULL) {
568 dbg("pci_dev null, finding %02x:%02x:%x",
569 slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
570 slot->dev = pci_find_slot(slot->bus->number, slot->devfn);
573 /* Still NULL? Well then scan for it! */
574 if(slot->dev == NULL) {
576 dbg("pci_dev still null");
579 * This will generate pci_dev structures for all functions, but
580 * we will only call this case when lookup fails.
582 n = pci_scan_slot(slot->bus, slot->devfn);
583 dbg("%s: pci_scan_slot returned %d", __FUNCTION__, n);
585 pci_bus_add_devices(slot->bus);
586 slot->dev = pci_find_slot(slot->bus->number, slot->devfn);
587 if(slot->dev == NULL) {
588 err("Could not find PCI device for slot %02x", slot->number);
592 dbg("slot->dev = %p", slot->dev);
595 struct pci_dev_wrapped wrapped_dev;
596 struct pci_bus_wrapped wrapped_bus;
599 memset(&wrapped_dev, 0, sizeof (struct pci_dev_wrapped));
600 memset(&wrapped_bus, 0, sizeof (struct pci_bus_wrapped));
602 for (i = 0; i < 8; i++) {
603 dev = pci_find_slot(slot->bus->number,
604 PCI_DEVFN(PCI_SLOT(slot->dev->devfn), i));
607 wrapped_dev.dev = dev;
608 wrapped_bus.bus = slot->dev->bus;
610 wrapped_dev.data = NULL;
612 wrapped_dev.data = (void*) slot;
613 rc = pci_visit_dev(&configure_functions, &wrapped_dev, &wrapped_bus);
617 dbg("%s - exit, rc = %d", __FUNCTION__, rc);
621 int cpci_unconfigure_slot(struct slot* slot)
625 struct pci_dev_wrapped wrapped_dev;
626 struct pci_bus_wrapped wrapped_bus;
629 dbg("%s - enter", __FUNCTION__);
632 err("No device for slot %02x\n", slot->number);
636 memset(&wrapped_dev, 0, sizeof (struct pci_dev_wrapped));
637 memset(&wrapped_bus, 0, sizeof (struct pci_bus_wrapped));
639 for (i = 0; i < 8; i++) {
640 dev = pci_find_slot(slot->bus->number,
641 PCI_DEVFN(PCI_SLOT(slot->devfn), i));
643 wrapped_dev.dev = dev;
644 wrapped_bus.bus = dev->bus;
646 wrapped_dev.data = NULL;
648 wrapped_dev.data = (void*) slot;
649 dbg("%s - unconfigure phase 2", __FUNCTION__);
650 rc = pci_visit_dev(&unconfigure_functions_phase2,
657 dbg("%s - exit, rc = %d", __FUNCTION__, rc);