added mtd driver
[linux-2.4.git] / drivers / acpi / pci_root.c
1 /*
2  *  pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 41 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38
39 #define _COMPONENT              ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME                ("pci_root")
41
42 extern struct pci_ops *pci_root_ops;
43
44 #define PREFIX                  "ACPI: "
45
46 static int acpi_pci_root_add (struct acpi_device *device);
47 static int acpi_pci_root_remove (struct acpi_device *device, int type);
48
49 static struct acpi_driver acpi_pci_root_driver = {
50         .name =         ACPI_PCI_ROOT_DRIVER_NAME,
51         .class =        ACPI_PCI_ROOT_CLASS,
52         .ids =          ACPI_PCI_ROOT_HID,
53         .ops =          {
54                                 .add =    acpi_pci_root_add,
55                                 .remove = acpi_pci_root_remove,
56                         },
57 };
58
59 struct acpi_pci_root {
60         struct list_head        node;
61         acpi_handle             handle;
62         struct acpi_pci_id      id;
63         struct pci_bus          *bus;
64 };
65
66 static LIST_HEAD(acpi_pci_roots);
67
68 static struct acpi_pci_driver *sub_driver;
69
70 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
71 {
72         int n = 0;
73         struct list_head *entry;
74
75         struct acpi_pci_driver **pptr = &sub_driver;
76         while (*pptr)
77                 pptr = &(*pptr)->next;
78         *pptr = driver;
79
80         if (!driver->add)
81                 return 0;
82
83         list_for_each(entry, &acpi_pci_roots) {
84                 struct acpi_pci_root *root;
85                 root = list_entry(entry, struct acpi_pci_root, node);
86                 driver->add(root->handle);
87                 n++;
88         }
89
90         return n;
91 }
92
93 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
94 {
95         struct list_head *entry;
96
97         struct acpi_pci_driver **pptr = &sub_driver;
98         while (*pptr) {
99                 if (*pptr != driver)
100                         continue;
101                 *pptr = (*pptr)->next;
102                 break;
103         }
104
105         if (!driver->remove)
106                 return;
107
108         list_for_each(entry, &acpi_pci_roots) {
109                 struct acpi_pci_root *root;
110                 root = list_entry(entry, struct acpi_pci_root, node);
111                 driver->remove(root->handle);
112         }
113 }
114
115 static acpi_status
116 get_root_bridge_busnr_callback (struct acpi_resource *resource, void *data)
117 {
118         int *busnr = (int *)data;
119         struct acpi_resource_address64 address;
120
121         if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
122             resource->id != ACPI_RSTYPE_ADDRESS32 &&
123             resource->id != ACPI_RSTYPE_ADDRESS64)
124                 return AE_OK;
125
126         acpi_resource_to_address64(resource, &address);
127         if ((address.address_length > 0) && 
128            (address.resource_type == ACPI_BUS_NUMBER_RANGE))
129                 *busnr = address.min_address_range;
130
131         return AE_OK;
132 }
133
134 static acpi_status 
135 try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
136 {
137         acpi_status status;
138
139         *busnum = -1;
140         status = acpi_walk_resources(handle, METHOD_NAME__CRS, get_root_bridge_busnr_callback, busnum);
141         if (ACPI_FAILURE(status))
142                 return status;
143         /* Check if we really get a bus number from _CRS */
144         if (*busnum == -1)
145                 return AE_ERROR;
146         return AE_OK;
147 }
148
149 static int
150 acpi_pci_root_add (
151         struct acpi_device      *device)
152 {
153         int                     result = 0;
154         struct acpi_pci_root    *root = NULL;
155         struct acpi_pci_root    *tmp;
156         acpi_status             status = AE_OK;
157         unsigned long           value = 0;
158         acpi_handle             handle = NULL;
159
160         ACPI_FUNCTION_TRACE("acpi_pci_root_add");
161
162         if (!device)
163                 return_VALUE(-EINVAL);
164
165         root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
166         if (!root)
167                 return_VALUE(-ENOMEM);
168         memset(root, 0, sizeof(struct acpi_pci_root));
169
170         root->handle = device->handle;
171         sprintf(acpi_device_name(device), "%s", ACPI_PCI_ROOT_DEVICE_NAME);
172         sprintf(acpi_device_class(device), "%s", ACPI_PCI_ROOT_CLASS);
173         acpi_driver_data(device) = root;
174
175         /*
176          * TBD: Doesn't the bus driver automatically set this?
177          */
178         device->ops.bind = acpi_pci_bind;
179
180         /* 
181          * Segment
182          * -------
183          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
184          */
185         status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL, 
186                 &value);
187         switch (status) {
188         case AE_OK:
189                 root->id.segment = (u16) value;
190                 break;
191         case AE_NOT_FOUND:
192                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
193                         "Assuming segment 0 (no _SEG)\n"));
194                 root->id.segment = 0;
195                 break;
196         default:
197                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SEG\n"));
198                 result = -ENODEV;
199                 goto end;
200         }
201
202         /* 
203          * Bus
204          * ---
205          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
206          */
207         status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL, 
208                 &value);
209         switch (status) {
210         case AE_OK:
211                 root->id.bus = (u16) value;
212                 break;
213         case AE_NOT_FOUND:
214                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
215                 root->id.bus = 0;
216                 break;
217         default:
218                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BBN\n"));
219                 result = -ENODEV;
220                 goto end;
221         }
222
223         /* Some systems have wrong _BBN */
224         list_for_each_entry(tmp, &acpi_pci_roots, node) {
225                 if ((tmp->id.segment == root->id.segment)
226                                 && (tmp->id.bus == root->id.bus)) {
227                         int bus = 0;
228                         acpi_status status;
229
230                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
231                                 "Wrong _BBN value, please reboot and using option 'pci=noacpi'\n"));
232
233                         status = try_get_root_bridge_busnr(root->handle, &bus);
234                         if (ACPI_FAILURE(status))
235                                 break;
236                         if (bus != root->id.bus) {
237                                 printk(KERN_INFO PREFIX "PCI _CRS %d overrides _BBN 0\n", bus);
238                                 root->id.bus = bus;
239                         }
240                         break;
241                 }
242         }
243
244         /*
245          * Device & Function
246          * -----------------
247          * Obtained from _ADR (which has already been evaluated for us).
248          */
249         root->id.device = device->pnp.bus_address >> 16;
250         root->id.function = device->pnp.bus_address & 0xFFFF;
251
252         /*
253          * TBD: Need PCI interface for enumeration/configuration of roots.
254          */
255
256         /* TBD: Locking */
257         list_add_tail(&root->node, &acpi_pci_roots);
258
259         printk(KERN_INFO PREFIX "%s [%s] (%02x:%02x)\n", 
260                 acpi_device_name(device), acpi_device_bid(device),
261                 root->id.segment, root->id.bus);
262
263         /*
264          * Scan the Root Bridge
265          * --------------------
266          * Must do this prior to any attempt to bind the root device, as the
267          * PCI namespace does not get created until this call is made (and 
268          * thus the root bridge's pci_dev does not exist).
269          */
270 #ifdef CONFIG_X86
271         root->bus = pcibios_scan_root(root->id.bus);
272 #else
273         root->bus = pcibios_scan_root(root->handle,
274                                       root->id.segment, root->id.bus);
275 #endif
276         if (!root->bus) {
277                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
278                         "Bus %02x:%02x not present in PCI namespace\n", 
279                         root->id.segment, root->id.bus));
280                 result = -ENODEV;
281                 goto end;
282         }
283
284         /*
285          * Attach ACPI-PCI Context
286          * -----------------------
287          * Thus binding the ACPI and PCI devices.
288          */
289         result = acpi_pci_bind_root(device, &root->id, root->bus);
290         if (result)
291                 goto end;
292
293         /*
294          * PCI Routing Table
295          * -----------------
296          * Evaluate and parse _PRT, if exists.
297          */
298         status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
299         if (ACPI_SUCCESS(status))
300                 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
301                         root->id.bus);
302
303 end:
304         if (result)
305                 kfree(root);
306
307         return_VALUE(result);
308 }
309
310
311 static int
312 acpi_pci_root_remove (
313         struct acpi_device      *device,
314         int                     type)
315 {
316         struct acpi_pci_root    *root = NULL;
317
318         ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
319
320         if (!device || !acpi_driver_data(device))
321                 return_VALUE(-EINVAL);
322
323         root = (struct acpi_pci_root *) acpi_driver_data(device);
324
325         kfree(root);
326
327         return_VALUE(0);
328 }
329
330
331 int __init
332 acpi_pci_root_init (void)
333 {
334         ACPI_FUNCTION_TRACE("acpi_pci_root_init");
335
336         /* DEBUG:
337         acpi_dbg_layer = ACPI_PCI_COMPONENT;
338         acpi_dbg_level = 0xFFFFFFFF;
339          */
340
341         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
342                 return_VALUE(-ENODEV);
343
344         return_VALUE(0);
345 }
346
347
348 void __exit
349 acpi_pci_root_exit (void)
350 {
351         ACPI_FUNCTION_TRACE("acpi_pci_root_exit");
352
353         acpi_bus_unregister_driver(&acpi_pci_root_driver);
354
355         return_VOID;
356 }