11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * cpcihp_generic.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Generic port I/O CompactPCI driver 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 2002 SOMA Networks, Inc. 71da177e4SLinus Torvalds * Copyright 2001 Intel San Luis Obispo 81da177e4SLinus Torvalds * Copyright 2000,2001 MontaVista Software Inc. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 111da177e4SLinus Torvalds * under the terms of the GNU General Public License as published by the 121da177e4SLinus Torvalds * Free Software Foundation; either version 2 of the License, or (at your 131da177e4SLinus Torvalds * option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 161da177e4SLinus Torvalds * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 171da177e4SLinus Torvalds * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 181da177e4SLinus Torvalds * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 191da177e4SLinus Torvalds * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 201da177e4SLinus Torvalds * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 211da177e4SLinus Torvalds * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 221da177e4SLinus Torvalds * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 231da177e4SLinus Torvalds * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 241da177e4SLinus Torvalds * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License along 271da177e4SLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc., 281da177e4SLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * This generic CompactPCI hotplug driver should allow using the PCI hotplug 311da177e4SLinus Torvalds * mechanism on any CompactPCI board that exposes the #ENUM signal as a bit 321da177e4SLinus Torvalds * in a system register that can be read through standard port I/O. 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Send feedback to <scottm@somanetworks.com> 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #include <linux/config.h> 381da177e4SLinus Torvalds #include <linux/module.h> 391da177e4SLinus Torvalds #include <linux/init.h> 401da177e4SLinus Torvalds #include <linux/errno.h> 411da177e4SLinus Torvalds #include <linux/pci.h> 42*4e57b681STim Schmielau #include <linux/string.h> 431da177e4SLinus Torvalds #include "cpci_hotplug.h" 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #define DRIVER_VERSION "0.1" 461da177e4SLinus Torvalds #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>" 471da177e4SLinus Torvalds #define DRIVER_DESC "Generic port I/O CompactPCI Hot Plug Driver" 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #if !defined(MODULE) 501da177e4SLinus Torvalds #define MY_NAME "cpcihp_generic" 511da177e4SLinus Torvalds #else 521da177e4SLinus Torvalds #define MY_NAME THIS_MODULE->name 531da177e4SLinus Torvalds #endif 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #define dbg(format, arg...) \ 561da177e4SLinus Torvalds do { \ 571da177e4SLinus Torvalds if(debug) \ 581da177e4SLinus Torvalds printk (KERN_DEBUG "%s: " format "\n", \ 591da177e4SLinus Torvalds MY_NAME , ## arg); \ 601da177e4SLinus Torvalds } while(0) 611da177e4SLinus Torvalds #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg) 621da177e4SLinus Torvalds #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg) 631da177e4SLinus Torvalds #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg) 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* local variables */ 661da177e4SLinus Torvalds static int debug; 671da177e4SLinus Torvalds static char *bridge; 681da177e4SLinus Torvalds static u8 bridge_busnr; 691da177e4SLinus Torvalds static u8 bridge_slot; 701da177e4SLinus Torvalds static struct pci_bus *bus; 711da177e4SLinus Torvalds static u8 first_slot; 721da177e4SLinus Torvalds static u8 last_slot; 731da177e4SLinus Torvalds static u16 port; 741da177e4SLinus Torvalds static unsigned int enum_bit; 751da177e4SLinus Torvalds static u8 enum_mask; 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static struct cpci_hp_controller_ops generic_hpc_ops; 781da177e4SLinus Torvalds static struct cpci_hp_controller generic_hpc; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static int __init validate_parameters(void) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds char* str; 831da177e4SLinus Torvalds char* p; 841da177e4SLinus Torvalds unsigned long tmp; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds if(!bridge) { 871da177e4SLinus Torvalds info("not configured, disabling."); 881da177e4SLinus Torvalds return 1; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds str = bridge; 911da177e4SLinus Torvalds if(!*str) 921da177e4SLinus Torvalds return -EINVAL; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds tmp = simple_strtoul(str, &p, 16); 951da177e4SLinus Torvalds if(p == str || tmp > 0xff) { 961da177e4SLinus Torvalds err("Invalid hotplug bus bridge device bus number"); 971da177e4SLinus Torvalds return -EINVAL; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds bridge_busnr = (u8) tmp; 1001da177e4SLinus Torvalds dbg("bridge_busnr = 0x%02x", bridge_busnr); 1011da177e4SLinus Torvalds if(*p != ':') { 1021da177e4SLinus Torvalds err("Invalid hotplug bus bridge device"); 1031da177e4SLinus Torvalds return -EINVAL; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds str = p + 1; 1061da177e4SLinus Torvalds tmp = simple_strtoul(str, &p, 16); 1071da177e4SLinus Torvalds if(p == str || tmp > 0x1f) { 1081da177e4SLinus Torvalds err("Invalid hotplug bus bridge device slot number"); 1091da177e4SLinus Torvalds return -EINVAL; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds bridge_slot = (u8) tmp; 1121da177e4SLinus Torvalds dbg("bridge_slot = 0x%02x", bridge_slot); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds dbg("first_slot = 0x%02x", first_slot); 1151da177e4SLinus Torvalds dbg("last_slot = 0x%02x", last_slot); 1161da177e4SLinus Torvalds if(!(first_slot && last_slot)) { 1171da177e4SLinus Torvalds err("Need to specify first_slot and last_slot"); 1181da177e4SLinus Torvalds return -EINVAL; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds if(last_slot < first_slot) { 1211da177e4SLinus Torvalds err("first_slot must be less than last_slot"); 1221da177e4SLinus Torvalds return -EINVAL; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds dbg("port = 0x%04x", port); 1261da177e4SLinus Torvalds dbg("enum_bit = 0x%02x", enum_bit); 1271da177e4SLinus Torvalds if(enum_bit > 7) { 1281da177e4SLinus Torvalds err("Invalid #ENUM bit"); 1291da177e4SLinus Torvalds return -EINVAL; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds enum_mask = 1 << enum_bit; 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static int query_enum(void) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds u8 value; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds value = inb_p(port); 1401da177e4SLinus Torvalds return ((value & enum_mask) == enum_mask); 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static int __init cpcihp_generic_init(void) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds int status; 1461da177e4SLinus Torvalds struct resource* r; 1471da177e4SLinus Torvalds struct pci_dev* dev; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds info(DRIVER_DESC " version: " DRIVER_VERSION); 1501da177e4SLinus Torvalds status = validate_parameters(); 1511da177e4SLinus Torvalds if(status != 0) 1521da177e4SLinus Torvalds return status; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds r = request_region(port, 1, "#ENUM hotswap signal register"); 1551da177e4SLinus Torvalds if(!r) 1561da177e4SLinus Torvalds return -EBUSY; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds dev = pci_find_slot(bridge_busnr, PCI_DEVFN(bridge_slot, 0)); 1591da177e4SLinus Torvalds if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { 1601da177e4SLinus Torvalds err("Invalid bridge device %s", bridge); 1611da177e4SLinus Torvalds return -EINVAL; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds bus = dev->subordinate; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller)); 1661da177e4SLinus Torvalds generic_hpc_ops.query_enum = query_enum; 1671da177e4SLinus Torvalds generic_hpc.ops = &generic_hpc_ops; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds status = cpci_hp_register_controller(&generic_hpc); 1701da177e4SLinus Torvalds if(status != 0) { 1711da177e4SLinus Torvalds err("Could not register cPCI hotplug controller"); 1721da177e4SLinus Torvalds return -ENODEV; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds dbg("registered controller"); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds status = cpci_hp_register_bus(bus, first_slot, last_slot); 1771da177e4SLinus Torvalds if(status != 0) { 1781da177e4SLinus Torvalds err("Could not register cPCI hotplug bus"); 1791da177e4SLinus Torvalds goto init_bus_register_error; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds dbg("registered bus"); 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds status = cpci_hp_start(); 1841da177e4SLinus Torvalds if(status != 0) { 1851da177e4SLinus Torvalds err("Could not started cPCI hotplug system"); 1861da177e4SLinus Torvalds goto init_start_error; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds dbg("started cpci hp system"); 1891da177e4SLinus Torvalds return 0; 1901da177e4SLinus Torvalds init_start_error: 1911da177e4SLinus Torvalds cpci_hp_unregister_bus(bus); 1921da177e4SLinus Torvalds init_bus_register_error: 1931da177e4SLinus Torvalds cpci_hp_unregister_controller(&generic_hpc); 1941da177e4SLinus Torvalds err("status = %d", status); 1951da177e4SLinus Torvalds return status; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static void __exit cpcihp_generic_exit(void) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds cpci_hp_stop(); 2021da177e4SLinus Torvalds cpci_hp_unregister_bus(bus); 2031da177e4SLinus Torvalds cpci_hp_unregister_controller(&generic_hpc); 2041da177e4SLinus Torvalds release_region(port, 1); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds module_init(cpcihp_generic_init); 2081da177e4SLinus Torvalds module_exit(cpcihp_generic_exit); 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR); 2111da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 2121da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2131da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR); 2141da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); 2151da177e4SLinus Torvalds module_param(bridge, charp, 0); 2161da177e4SLinus Torvalds MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)"); 2171da177e4SLinus Torvalds module_param(first_slot, byte, 0); 2181da177e4SLinus Torvalds MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number"); 2191da177e4SLinus Torvalds module_param(last_slot, byte, 0); 2201da177e4SLinus Torvalds MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number"); 2211da177e4SLinus Torvalds module_param(port, ushort, 0); 2221da177e4SLinus Torvalds MODULE_PARM_DESC(port, "#ENUM signal I/O port"); 2231da177e4SLinus Torvalds module_param(enum_bit, uint, 0); 2241da177e4SLinus Torvalds MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)"); 225