sbus.c 7.33 KB
Newer Older
1
/* sbus.c: SBus support routines.
Linus Torvalds's avatar
Linus Torvalds committed
2
 *
3
 * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
Linus Torvalds's avatar
Linus Torvalds committed
4 5 6 7 8
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
9
#include <linux/device.h>
10
#include <linux/of_device.h>
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15

#include <asm/system.h>
#include <asm/sbus.h>
#include <asm/dma.h>
#include <asm/oplib.h>
16
#include <asm/prom.h>
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19
#include <asm/bpp.h>
#include <asm/irq.h>

20 21 22 23 24 25 26 27 28 29 30 31
static ssize_t
show_sbusobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
{
	struct sbus_dev *sbus;

	sbus = to_sbus_device(dev);

	return snprintf (buf, PAGE_SIZE, "%s\n", sbus->ofdev.node->full_name);
}

static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_sbusobppath_attr, NULL);

32
struct sbus_bus *sbus_root;
Linus Torvalds's avatar
Linus Torvalds committed
33

34
static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
Linus Torvalds's avatar
Linus Torvalds committed
35
{
36
	struct dev_archdata *sd;
37
	unsigned long base;
38
	const void *pval;
39
	int len, err;
Linus Torvalds's avatar
Linus Torvalds committed
40

41 42 43 44
	sdev->prom_node = dp->node;
	strcpy(sdev->prom_name, dp->name);

	pval = of_get_property(dp, "reg", &len);
45
	sdev->num_registers = 0;
46 47 48
	if (pval) {
		memcpy(sdev->reg_addrs, pval, len);

49 50
		sdev->num_registers =
			len / sizeof(struct linux_prom_registers);
Linus Torvalds's avatar
Linus Torvalds committed
51

52
		base = (unsigned long) sdev->reg_addrs[0].phys_addr;
Linus Torvalds's avatar
Linus Torvalds committed
53

54 55 56 57 58
		/* Compute the slot number. */
		if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
			sdev->slot = sbus_dev_slot(base);
		else
			sdev->slot = sdev->reg_addrs[0].which_io;
Linus Torvalds's avatar
Linus Torvalds committed
59 60
	}

61
	pval = of_get_property(dp, "ranges", &len);
62
	sdev->num_device_ranges = 0;
63 64
	if (pval) {
		memcpy(sdev->device_ranges, pval, len);
65 66
		sdev->num_device_ranges =
			len / sizeof(struct linux_prom_ranges);
67
	}
Linus Torvalds's avatar
Linus Torvalds committed
68

69
	sbus_fill_device_irq(sdev);
Linus Torvalds's avatar
Linus Torvalds committed
70

71 72 73 74
	sd = &sdev->ofdev.dev.archdata;
	sd->prom_node = dp;
	sd->op = &sdev->ofdev;

75 76 77 78 79 80
	sdev->ofdev.node = dp;
	if (sdev->parent)
		sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
	else
		sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
	sdev->ofdev.dev.bus = &sbus_bus_type;
81
	dev_set_name(&sdev->ofdev.dev, "sbus[%08x]", dp->node);
82 83 84

	if (of_device_register(&sdev->ofdev) != 0)
		printk(KERN_DEBUG "sbus: device registration error for %s!\n",
85
		       dp->path_component_name);
86 87 88

	/* WE HAVE BEEN INVADED BY ALIENS! */
	err = sysfs_create_file(&sdev->ofdev.dev.kobj, &dev_attr_obppath.attr);
Linus Torvalds's avatar
Linus Torvalds committed
89 90
}

91
static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
Linus Torvalds's avatar
Linus Torvalds committed
92
{
93
	const void *pval;
Linus Torvalds's avatar
Linus Torvalds committed
94 95
	int len;

96 97 98 99 100 101 102 103
	pval = of_get_property(dp, "ranges", &len);
	sbus->num_sbus_ranges = 0;
	if (pval) {
		memcpy(sbus->sbus_ranges, pval, len);
		sbus->num_sbus_ranges =
			len / sizeof(struct linux_prom_ranges);

		sbus_arch_bus_ranges_init(dp->parent, sbus);
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	}
}

static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
					  int num_ranges,
					  struct linux_prom_registers *regs,
					  int num_regs)
{
	if (num_ranges) {
		int regnum;

		for (regnum = 0; regnum < num_regs; regnum++) {
			int rngnum;

			for (rngnum = 0; rngnum < num_ranges; rngnum++) {
				if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
					break;
			}
			if (rngnum == num_ranges) {
				/* We used to flag this as an error.  Actually
				 * some devices do not report the regs as we expect.
				 * For example, see SUNW,pln device.  In that case
				 * the reg property is in a format internal to that
				 * node, ie. it is not in the SBUS register space
				 * per se. -DaveM
				 */
				return;
			}
			regs[regnum].which_io = ranges[rngnum].ot_parent_space;
			regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
			regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
		}
	}
}

static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
{
	if (sdev->num_registers != 0) {
		struct sbus_dev *parent = sdev->parent;
		int i;

		while (parent != NULL) {
			__apply_ranges_to_regs(parent->device_ranges,
					       parent->num_device_ranges,
					       sdev->reg_addrs,
					       sdev->num_registers);

			parent = parent->parent;
		}

		__apply_ranges_to_regs(sdev->bus->sbus_ranges,
				       sdev->bus->num_sbus_ranges,
				       sdev->reg_addrs,
				       sdev->num_registers);

		for (i = 0; i < sdev->num_registers; i++) {
			struct resource *res = &sdev->resource[i];

			res->start = sdev->reg_addrs[i].phys_addr;
			res->end = (res->start +
				    (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
			res->flags = IORESOURCE_IO |
				(sdev->reg_addrs[i].which_io & 0xff);
		}
	}
}

static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
{
	struct sbus_dev *sdev;

	for (sdev = first_sdev; sdev; sdev = sdev->next) {
		if (sdev->child)
			sbus_fixup_all_regs(sdev->child);
		__fixup_regs_sdev(sdev);
	}
}

182 183 184 185 186 187 188 189 190 191
/* We preserve the "probe order" of these bus and device lists to give
 * the same ordering as the old code.
 */
static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
{
	while (*root)
		root = &(*root)->next;
	*root = sbus;
	sbus->next = NULL;
}
Linus Torvalds's avatar
Linus Torvalds committed
192

193 194 195 196 197 198 199
static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
{
	while (*root)
		root = &(*root)->next;
	*root = sdev;
	sdev->next = NULL;
}
Linus Torvalds's avatar
Linus Torvalds committed
200

201
static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
Linus Torvalds's avatar
Linus Torvalds committed
202
{
203 204 205
	dp = dp->child;
	while (dp) {
		struct sbus_dev *sdev;
Linus Torvalds's avatar
Linus Torvalds committed
206

207 208 209
		sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
		if (sdev) {
			sdev_insert(sdev, &parent->child);
Linus Torvalds's avatar
Linus Torvalds committed
210

211 212
			sdev->bus = sbus;
			sdev->parent = parent;
213 214 215 216
			sdev->ofdev.dev.archdata.iommu =
				sbus->ofdev.dev.archdata.iommu;
			sdev->ofdev.dev.archdata.stc =
				sbus->ofdev.dev.archdata.stc;
217 218 219 220

			fill_sbus_device(dp, sdev);

			walk_children(dp, sdev, sbus);
Linus Torvalds's avatar
Linus Torvalds committed
221
		}
222
		dp = dp->sibling;
Linus Torvalds's avatar
Linus Torvalds committed
223
	}
224
}
Linus Torvalds's avatar
Linus Torvalds committed
225

226 227 228 229 230
static void __init build_one_sbus(struct device_node *dp, int num_sbus)
{
	struct sbus_bus *sbus;
	unsigned int sbus_clock;
	struct device_node *dev_dp;
Linus Torvalds's avatar
Linus Torvalds committed
231

232 233 234
	sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
	if (!sbus)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
235

236 237
	sbus_insert(sbus, &sbus_root);
	sbus->prom_node = dp->node;
Linus Torvalds's avatar
Linus Torvalds committed
238

239
	sbus_setup_iommu(sbus, dp);
Linus Torvalds's avatar
Linus Torvalds committed
240

241
	printk("sbus%d: ", num_sbus);
Linus Torvalds's avatar
Linus Torvalds committed
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	sbus_clock = of_getintprop_default(dp, "clock-frequency",
					   (25*1000*1000));
	sbus->clock_freq = sbus_clock;

	printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
	       (int) (((sbus_clock/1000)%1000 != 0) ? 
		      (((sbus_clock/1000)%1000) + 1000) : 0));

	strcpy(sbus->prom_name, dp->name);

	sbus_setup_arch_props(sbus, dp);

	sbus_bus_ranges_init(dp, sbus);

	sbus->ofdev.node = dp;
	sbus->ofdev.dev.parent = NULL;
	sbus->ofdev.dev.bus = &sbus_bus_type;
260
	dev_set_name(&sbus->ofdev.dev, "sbus%d", num_sbus);
261 262 263

	if (of_device_register(&sbus->ofdev) != 0)
		printk(KERN_DEBUG "sbus: device registration error for %s!\n",
264
		       dev_name(&sbus->ofdev.dev));
265 266 267 268 269 270 271 272 273 274 275

	dev_dp = dp->child;
	while (dev_dp) {
		struct sbus_dev *sdev;

		sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
		if (sdev) {
			sdev_insert(sdev, &sbus->devices);

			sdev->bus = sbus;
			sdev->parent = NULL;
276 277 278 279 280
			sdev->ofdev.dev.archdata.iommu =
				sbus->ofdev.dev.archdata.iommu;
			sdev->ofdev.dev.archdata.stc =
				sbus->ofdev.dev.archdata.stc;

281 282 283
			fill_sbus_device(dev_dp, sdev);

			walk_children(dev_dp, sdev, sbus);
Linus Torvalds's avatar
Linus Torvalds committed
284
		}
285 286 287 288 289 290 291 292 293 294 295 296 297
		dev_dp = dev_dp->sibling;
	}

	sbus_fixup_all_regs(sbus->devices);

	dvma_init(sbus);
}

static int __init sbus_init(void)
{
	struct device_node *dp;
	const char *sbus_name = "sbus";
	int num_sbus = 0;
Linus Torvalds's avatar
Linus Torvalds committed
298

299 300
	if (sbus_arch_preinit())
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
301

302 303 304 305 306
	if (sparc_cpu_model == sun4d)
		sbus_name = "sbi";

	for_each_node_by_name(dp, sbus_name) {
		build_one_sbus(dp, num_sbus);
Linus Torvalds's avatar
Linus Torvalds committed
307 308 309
		num_sbus++;

	}
310 311

	sbus_arch_postinit();
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316

	return 0;
}

subsys_initcall(sbus_init);