Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / arch / sparc64 / kernel / isa.c
1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/pci.h>
4 #include <linux/slab.h>
5 #include <asm/oplib.h>
6 #include <asm/prom.h>
7 #include <asm/of_device.h>
8 #include <asm/isa.h>
9
10 struct sparc_isa_bridge *isa_chain;
11
12 static void __init fatal_err(const char *reason)
13 {
14         prom_printf("ISA: fatal error, %s.\n", reason);
15 }
16
17 static void __init report_dev(struct sparc_isa_device *isa_dev, int child)
18 {
19         if (child)
20                 printk(" (%s)", isa_dev->prom_node->name);
21         else
22                 printk(" [%s", isa_dev->prom_node->name);
23 }
24
25 static void __init isa_dev_get_resource(struct sparc_isa_device *isa_dev)
26 {
27         struct linux_prom_registers *pregs;
28         unsigned long base, len;
29         int prop_len;
30
31         pregs = of_get_property(isa_dev->prom_node, "reg", &prop_len);
32         if (!pregs)
33                 return;
34
35         /* Only the first one is interesting. */
36         len = pregs[0].reg_size;
37         base = (((unsigned long)pregs[0].which_io << 32) |
38                 (unsigned long)pregs[0].phys_addr);
39         base += isa_dev->bus->parent->io_space.start;
40
41         isa_dev->resource.start = base;
42         isa_dev->resource.end   = (base + len - 1UL);
43         isa_dev->resource.flags = IORESOURCE_IO;
44         isa_dev->resource.name  = isa_dev->prom_node->name;
45
46         request_resource(&isa_dev->bus->parent->io_space,
47                          &isa_dev->resource);
48 }
49
50 static void __init isa_dev_get_irq(struct sparc_isa_device *isa_dev)
51 {
52         struct of_device *op = of_find_device_by_node(isa_dev->prom_node);
53
54         if (!op || !op->num_irqs) {
55                 isa_dev->irq = PCI_IRQ_NONE;
56         } else {
57                 isa_dev->irq = op->irqs[0];
58         }
59 }
60
61 static void __init isa_fill_children(struct sparc_isa_device *parent_isa_dev)
62 {
63         struct device_node *dp = parent_isa_dev->prom_node->child;
64
65         if (!dp)
66                 return;
67
68         printk(" ->");
69         while (dp) {
70                 struct sparc_isa_device *isa_dev;
71
72                 isa_dev = kzalloc(sizeof(*isa_dev), GFP_KERNEL);
73                 if (!isa_dev) {
74                         fatal_err("cannot allocate child isa_dev");
75                         prom_halt();
76                 }
77
78                 /* Link it in to parent. */
79                 isa_dev->next = parent_isa_dev->child;
80                 parent_isa_dev->child = isa_dev;
81
82                 isa_dev->bus = parent_isa_dev->bus;
83                 isa_dev->prom_node = dp;
84
85                 isa_dev_get_resource(isa_dev);
86                 isa_dev_get_irq(isa_dev);
87
88                 report_dev(isa_dev, 1);
89
90                 dp = dp->sibling;
91         }
92 }
93
94 static void __init isa_fill_devices(struct sparc_isa_bridge *isa_br)
95 {
96         struct device_node *dp = isa_br->prom_node->child;
97
98         while (dp) {
99                 struct sparc_isa_device *isa_dev;
100
101                 isa_dev = kzalloc(sizeof(*isa_dev), GFP_KERNEL);
102                 if (!isa_dev) {
103                         printk(KERN_DEBUG "ISA: cannot allocate isa_dev");
104                         return;
105                 }
106
107                 isa_dev->ofdev.node = dp;
108                 isa_dev->ofdev.dev.parent = &isa_br->ofdev.dev;
109                 isa_dev->ofdev.dev.bus = &isa_bus_type;
110                 sprintf(isa_dev->ofdev.dev.bus_id, "isa[%08x]", dp->node);
111
112                 /* Register with core */
113                 if (of_device_register(&isa_dev->ofdev) != 0) {
114                         printk(KERN_DEBUG "isa: device registration error for %s!\n",
115                                dp->path_component_name);
116                         kfree(isa_dev);
117                         goto next_sibling;
118                 }
119
120                 /* Link it in. */
121                 isa_dev->next = NULL;
122                 if (isa_br->devices == NULL) {
123                         isa_br->devices = isa_dev;
124                 } else {
125                         struct sparc_isa_device *tmp = isa_br->devices;
126
127                         while (tmp->next)
128                                 tmp = tmp->next;
129
130                         tmp->next = isa_dev;
131                 }
132
133                 isa_dev->bus = isa_br;
134                 isa_dev->prom_node = dp;
135
136                 isa_dev_get_resource(isa_dev);
137                 isa_dev_get_irq(isa_dev);
138
139                 report_dev(isa_dev, 0);
140
141                 isa_fill_children(isa_dev);
142
143                 printk("]");
144
145         next_sibling:
146                 dp = dp->sibling;
147         }
148 }
149
150 void __init isa_init(void)
151 {
152         struct pci_dev *pdev;
153         unsigned short vendor, device;
154         int index = 0;
155
156         vendor = PCI_VENDOR_ID_AL;
157         device = PCI_DEVICE_ID_AL_M1533;
158
159         pdev = NULL;
160         while ((pdev = pci_get_device(vendor, device, pdev)) != NULL) {
161                 struct pcidev_cookie *pdev_cookie;
162                 struct pci_pbm_info *pbm;
163                 struct sparc_isa_bridge *isa_br;
164                 struct device_node *dp;
165
166                 pdev_cookie = pdev->sysdata;
167                 if (!pdev_cookie) {
168                         printk("ISA: Warning, ISA bridge ignored due to "
169                                "lack of OBP data.\n");
170                         continue;
171                 }
172                 pbm = pdev_cookie->pbm;
173                 dp = pdev_cookie->prom_node;
174
175                 isa_br = kzalloc(sizeof(*isa_br), GFP_KERNEL);
176                 if (!isa_br) {
177                         printk(KERN_DEBUG "isa: cannot allocate sparc_isa_bridge");
178                         return;
179                 }
180
181                 isa_br->ofdev.node = dp;
182                 isa_br->ofdev.dev.parent = &pdev->dev;
183                 isa_br->ofdev.dev.bus = &isa_bus_type;
184                 sprintf(isa_br->ofdev.dev.bus_id, "isa%d", index);
185
186                 /* Register with core */
187                 if (of_device_register(&isa_br->ofdev) != 0) {
188                         printk(KERN_DEBUG "isa: device registration error for %s!\n",
189                                dp->path_component_name);
190                         kfree(isa_br);
191                         return;
192                 }
193
194                 /* Link it in. */
195                 isa_br->next = isa_chain;
196                 isa_chain = isa_br;
197
198                 isa_br->parent = pbm;
199                 isa_br->self = pdev;
200                 isa_br->index = index++;
201                 isa_br->prom_node = pdev_cookie->prom_node;
202
203                 printk("isa%d:", isa_br->index);
204
205                 isa_fill_devices(isa_br);
206
207                 printk("\n");
208         }
209 }