Merge branch 'for-linus' from kernel.org:/.../shaggy/jfs-2.6 manually
[pandora-kernel.git] / drivers / pci / pcie / portdrv_pci.c
1 /*
2  * File:        portdrv_pci.c
3  * Purpose:     PCI Express Port Bus Driver
4  *
5  * Copyright (C) 2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/pm.h>
14 #include <linux/init.h>
15 #include <linux/pcieport_if.h>
16
17 #include "portdrv.h"
18
19 /*
20  * Version Information
21  */
22 #define DRIVER_VERSION "v1.0"
23 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
24 #define DRIVER_DESC "PCIE Port Bus Driver"
25 MODULE_AUTHOR(DRIVER_AUTHOR);
26 MODULE_DESCRIPTION(DRIVER_DESC);
27 MODULE_LICENSE("GPL");
28
29 /* global data */
30 static const char device_name[] = "pcieport-driver";
31
32 static void pci_save_msi_state(struct pci_dev *dev)
33 {
34         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
35         int i = 0, pos;
36         u16 control;
37
38         if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) <= 0)
39                 return;
40
41         pci_read_config_dword(dev, pos, &p_ext->saved_msi_config_space[i++]);
42         control = p_ext->saved_msi_config_space[0] >> 16;
43         pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
44                 &p_ext->saved_msi_config_space[i++]);
45         if (control & PCI_MSI_FLAGS_64BIT) {
46                 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
47                         &p_ext->saved_msi_config_space[i++]);
48                 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64,
49                         &p_ext->saved_msi_config_space[i++]);
50         } else
51                 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32,
52                         &p_ext->saved_msi_config_space[i++]);
53         if (control & PCI_MSI_FLAGS_MASKBIT)
54                 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT,
55                         &p_ext->saved_msi_config_space[i++]);
56 }
57
58 static void pci_restore_msi_state(struct pci_dev *dev)
59 {
60         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
61         int i = 0, pos;
62         u16 control;
63
64         if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) <= 0)
65                 return;
66
67         control = p_ext->saved_msi_config_space[i++] >> 16;
68         pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
69         pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
70                 p_ext->saved_msi_config_space[i++]);
71         if (control & PCI_MSI_FLAGS_64BIT) {
72                 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
73                         p_ext->saved_msi_config_space[i++]);
74                 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64,
75                         p_ext->saved_msi_config_space[i++]);
76         } else
77                 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32,
78                         p_ext->saved_msi_config_space[i++]);
79         if (control & PCI_MSI_FLAGS_MASKBIT)
80                 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT,
81                         p_ext->saved_msi_config_space[i++]);
82 }
83
84 static void pcie_portdrv_save_config(struct pci_dev *dev)
85 {
86         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
87
88         pci_save_state(dev);
89         if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
90                 pci_save_msi_state(dev);
91 }
92
93 static int pcie_portdrv_restore_config(struct pci_dev *dev)
94 {
95         struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
96         int retval;
97
98         pci_restore_state(dev);
99         if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
100                 pci_restore_msi_state(dev);
101         retval = pci_enable_device(dev);
102         if (retval)
103                 return retval;
104         pci_set_master(dev);
105         return 0;
106 }
107
108 /*
109  * pcie_portdrv_probe - Probe PCI-Express port devices
110  * @dev: PCI-Express port device being probed
111  *
112  * If detected invokes the pcie_port_device_register() method for 
113  * this port device.
114  *
115  */
116 static int __devinit pcie_portdrv_probe (struct pci_dev *dev, 
117                                 const struct pci_device_id *id )
118 {
119         int                     status;
120
121         status = pcie_port_device_probe(dev);
122         if (status)
123                 return status;
124
125         if (pci_enable_device(dev) < 0) 
126                 return -ENODEV;
127         
128         pci_set_master(dev);
129         if (!dev->irq) {
130                 printk(KERN_WARNING 
131                 "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n", 
132                 __FUNCTION__, dev->device, dev->vendor);
133         }
134         if (pcie_port_device_register(dev)) 
135                 return -ENOMEM;
136
137         return 0;
138 }
139
140 static void pcie_portdrv_remove (struct pci_dev *dev)
141 {
142         pcie_port_device_remove(dev);
143         kfree(pci_get_drvdata(dev));
144 }
145
146 #ifdef CONFIG_PM
147 static int pcie_portdrv_suspend (struct pci_dev *dev, pm_message_t state)
148 {
149         int ret = pcie_port_device_suspend(dev, state);
150
151         pcie_portdrv_save_config(dev);
152         return ret;
153 }
154
155 static int pcie_portdrv_resume (struct pci_dev *dev)
156 {
157         pcie_portdrv_restore_config(dev);
158         return pcie_port_device_resume(dev);
159 }
160 #endif
161
162 /*
163  * LINUX Device Driver Model
164  */
165 static const struct pci_device_id port_pci_ids[] = { {
166         /* handle any PCI-Express port */
167         PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
168         }, { /* end: all zeroes */ }
169 };
170 MODULE_DEVICE_TABLE(pci, port_pci_ids);
171
172 static struct pci_driver pcie_portdrv = {
173         .name           = (char *)device_name,
174         .id_table       = &port_pci_ids[0],
175
176         .probe          = pcie_portdrv_probe,
177         .remove         = pcie_portdrv_remove,
178
179 #ifdef  CONFIG_PM
180         .suspend        = pcie_portdrv_suspend,
181         .resume         = pcie_portdrv_resume,
182 #endif  /* PM */
183 };
184
185 static int __init pcie_portdrv_init(void)
186 {
187         int retval = 0;
188
189         pcie_port_bus_register();
190         retval = pci_register_driver(&pcie_portdrv);
191         if (retval)
192                 pcie_port_bus_unregister();
193         return retval;
194 }
195
196 static void __exit pcie_portdrv_exit(void) 
197 {
198         pci_unregister_driver(&pcie_portdrv);
199         pcie_port_bus_unregister();
200 }
201
202 module_init(pcie_portdrv_init);
203 module_exit(pcie_portdrv_exit);