blob: 387a6aa8c020cf8746c4f8a8807d803f3499930a [file] [log] [blame]
David S. Miller8fae0972006-06-20 15:23:28 -07001/* sbus.c: SBus support routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller8fae0972006-06-20 15:23:28 -07003 * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#include <linux/config.h>
9#include <linux/init.h>
10#include <linux/pci.h>
11
12#include <asm/system.h>
13#include <asm/sbus.h>
14#include <asm/dma.h>
15#include <asm/oplib.h>
David S. Miller576c3522006-06-23 15:55:45 -070016#include <asm/prom.h>
17#include <asm/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/bpp.h>
19#include <asm/irq.h>
20
David S. Miller8fae0972006-06-20 15:23:28 -070021struct sbus_bus *sbus_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
David S. Miller576c3522006-06-23 15:55:45 -070023static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
David S. Miller576c3522006-06-23 15:55:45 -070025 unsigned long base;
26 void *pval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 int len;
28
David S. Miller576c3522006-06-23 15:55:45 -070029 sdev->prom_node = dp->node;
30 strcpy(sdev->prom_name, dp->name);
31
32 pval = of_get_property(dp, "reg", &len);
David S. Miller8fae0972006-06-20 15:23:28 -070033 sdev->num_registers = 0;
David S. Miller576c3522006-06-23 15:55:45 -070034 if (pval) {
35 memcpy(sdev->reg_addrs, pval, len);
36
David S. Miller8fae0972006-06-20 15:23:28 -070037 sdev->num_registers =
38 len / sizeof(struct linux_prom_registers);
David S. Miller8fae0972006-06-20 15:23:28 -070039
40 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
41
42 /* Compute the slot number. */
43 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
44 sdev->slot = sbus_dev_slot(base);
45 else
46 sdev->slot = sdev->reg_addrs[0].which_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48
David S. Miller576c3522006-06-23 15:55:45 -070049 pval = of_get_property(dp, "ranges", &len);
David S. Miller8fae0972006-06-20 15:23:28 -070050 sdev->num_device_ranges = 0;
David S. Miller576c3522006-06-23 15:55:45 -070051 if (pval) {
52 memcpy(sdev->device_ranges, pval, len);
David S. Miller8fae0972006-06-20 15:23:28 -070053 sdev->num_device_ranges =
54 len / sizeof(struct linux_prom_ranges);
David S. Miller576c3522006-06-23 15:55:45 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David S. Miller8fae0972006-06-20 15:23:28 -070057 sbus_fill_device_irq(sdev);
David S. Miller576c3522006-06-23 15:55:45 -070058
59 sdev->ofdev.node = dp;
60 if (sdev->parent)
61 sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
62 else
63 sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
64 sdev->ofdev.dev.bus = &sbus_bus_type;
65 strcpy(sdev->ofdev.dev.bus_id, dp->path_component_name);
66
67 if (of_device_register(&sdev->ofdev) != 0)
68 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
69 sdev->ofdev.dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
David S. Miller576c3522006-06-23 15:55:45 -070072static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
David S. Miller576c3522006-06-23 15:55:45 -070074 void *pval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int len;
76
David S. Miller576c3522006-06-23 15:55:45 -070077 pval = of_get_property(dp, "ranges", &len);
78 sbus->num_sbus_ranges = 0;
79 if (pval) {
80 memcpy(sbus->sbus_ranges, pval, len);
81 sbus->num_sbus_ranges =
82 len / sizeof(struct linux_prom_ranges);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
David S. Miller576c3522006-06-23 15:55:45 -070084 sbus_arch_bus_ranges_init(dp->parent, sbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
89 int num_ranges,
90 struct linux_prom_registers *regs,
91 int num_regs)
92{
93 if (num_ranges) {
94 int regnum;
95
96 for (regnum = 0; regnum < num_regs; regnum++) {
97 int rngnum;
98
99 for (rngnum = 0; rngnum < num_ranges; rngnum++) {
100 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
101 break;
102 }
103 if (rngnum == num_ranges) {
104 /* We used to flag this as an error. Actually
105 * some devices do not report the regs as we expect.
106 * For example, see SUNW,pln device. In that case
107 * the reg property is in a format internal to that
108 * node, ie. it is not in the SBUS register space
109 * per se. -DaveM
110 */
111 return;
112 }
113 regs[regnum].which_io = ranges[rngnum].ot_parent_space;
114 regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
115 regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
116 }
117 }
118}
119
120static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
121{
122 if (sdev->num_registers != 0) {
123 struct sbus_dev *parent = sdev->parent;
124 int i;
125
126 while (parent != NULL) {
127 __apply_ranges_to_regs(parent->device_ranges,
128 parent->num_device_ranges,
129 sdev->reg_addrs,
130 sdev->num_registers);
131
132 parent = parent->parent;
133 }
134
135 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
136 sdev->bus->num_sbus_ranges,
137 sdev->reg_addrs,
138 sdev->num_registers);
139
140 for (i = 0; i < sdev->num_registers; i++) {
141 struct resource *res = &sdev->resource[i];
142
143 res->start = sdev->reg_addrs[i].phys_addr;
144 res->end = (res->start +
145 (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
146 res->flags = IORESOURCE_IO |
147 (sdev->reg_addrs[i].which_io & 0xff);
148 }
149 }
150}
151
152static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
153{
154 struct sbus_dev *sdev;
155
156 for (sdev = first_sdev; sdev; sdev = sdev->next) {
157 if (sdev->child)
158 sbus_fixup_all_regs(sdev->child);
159 __fixup_regs_sdev(sdev);
160 }
161}
162
David S. Miller576c3522006-06-23 15:55:45 -0700163/* We preserve the "probe order" of these bus and device lists to give
164 * the same ordering as the old code.
165 */
166static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
167{
168 while (*root)
169 root = &(*root)->next;
170 *root = sbus;
171 sbus->next = NULL;
172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David S. Miller576c3522006-06-23 15:55:45 -0700174static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
175{
176 while (*root)
177 root = &(*root)->next;
178 *root = sdev;
179 sdev->next = NULL;
180}
181
182static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
183{
184 dp = dp->child;
185 while (dp) {
186 struct sbus_dev *sdev;
187
188 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
189 if (sdev) {
190 sdev_insert(sdev, &parent->child);
191
192 sdev->bus = sbus;
193 sdev->parent = parent;
194
195 fill_sbus_device(dp, sdev);
196
197 walk_children(dp, sdev, sbus);
198 }
199 dp = dp->sibling;
200 }
201}
202
203static void __init build_one_sbus(struct device_node *dp, int num_sbus)
204{
205 struct sbus_bus *sbus;
206 unsigned int sbus_clock;
207 struct device_node *dev_dp;
208
209 sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
210 if (!sbus)
211 return;
212
213 sbus_insert(sbus, &sbus_root);
214 sbus->prom_node = dp->node;
215
216 sbus_setup_iommu(sbus, dp);
217
218 printk("sbus%d: ", num_sbus);
219
220 sbus_clock = of_getintprop_default(dp, "clock-frequency",
221 (25*1000*1000));
222 sbus->clock_freq = sbus_clock;
223
224 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
225 (int) (((sbus_clock/1000)%1000 != 0) ?
226 (((sbus_clock/1000)%1000) + 1000) : 0));
227
228 strcpy(sbus->prom_name, dp->name);
229
230 sbus_setup_arch_props(sbus, dp);
231
232 sbus_bus_ranges_init(dp, sbus);
233
234 sbus->ofdev.node = dp;
235 sbus->ofdev.dev.parent = NULL;
236 sbus->ofdev.dev.bus = &sbus_bus_type;
237 strcpy(sbus->ofdev.dev.bus_id, dp->path_component_name);
238
239 if (of_device_register(&sbus->ofdev) != 0)
240 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
241 sbus->ofdev.dev.bus_id);
242
243 dev_dp = dp->child;
244 while (dev_dp) {
245 struct sbus_dev *sdev;
246
247 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
248 if (sdev) {
249 sdev_insert(sdev, &sbus->devices);
250
251 sdev->bus = sbus;
252 sdev->parent = NULL;
253 fill_sbus_device(dev_dp, sdev);
254
255 walk_children(dev_dp, sdev, sbus);
256 }
257 dev_dp = dev_dp->sibling;
258 }
259
260 sbus_fixup_all_regs(sbus->devices);
261
262 dvma_init(sbus);
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265static int __init sbus_init(void)
266{
David S. Miller576c3522006-06-23 15:55:45 -0700267 struct device_node *dp;
268 const char *sbus_name = "sbus";
269 int num_sbus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David S. Miller576c3522006-06-23 15:55:45 -0700271 if (sbus_arch_preinit())
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David S. Miller576c3522006-06-23 15:55:45 -0700274 if (sparc_cpu_model == sun4d)
275 sbus_name = "sbi";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
David S. Miller576c3522006-06-23 15:55:45 -0700277 for_each_node_by_name(dp, sbus_name) {
278 build_one_sbus(dp, num_sbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 num_sbus++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David S. Miller576c3522006-06-23 15:55:45 -0700283 sbus_arch_postinit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return 0;
286}
287
288subsys_initcall(sbus_init);