xref: /linux/arch/sparc/kernel/devices.c (revision 14b42963f64b98ab61fa9723c03d71aa5ef4f862)
1 /* devices.c: Initial scan of the prom device tree for important
2  *	      Sparc device nodes which we need to find.
3  *
4  * This is based on the sparc64 version, but sun4m doesn't always use
5  * the hardware MIDs, so be careful.
6  *
7  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/threads.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 
16 #include <asm/page.h>
17 #include <asm/oplib.h>
18 #include <asm/smp.h>
19 #include <asm/system.h>
20 #include <asm/cpudata.h>
21 
22 extern void cpu_probe(void);
23 extern void clock_stop_probe(void); /* tadpole.c */
24 extern void sun4c_probe_memerr_reg(void);
25 
26 static char *cpu_mid_prop(void)
27 {
28 	if (sparc_cpu_model == sun4d)
29 		return "cpu-id";
30 	return "mid";
31 }
32 
33 static int check_cpu_node(int nd, int *cur_inst,
34 			  int (*compare)(int, int, void *), void *compare_arg,
35 			  int *prom_node, int *mid)
36 {
37 	char node_str[128];
38 
39 	prom_getstring(nd, "device_type", node_str, sizeof(node_str));
40 	if (strcmp(node_str, "cpu"))
41 		return -ENODEV;
42 
43 	if (!compare(nd, *cur_inst, compare_arg)) {
44 		if (prom_node)
45 			*prom_node = nd;
46 		if (mid) {
47 			*mid = prom_getintdefault(nd, cpu_mid_prop(), 0);
48 			if (sparc_cpu_model == sun4m)
49 				*mid &= 3;
50 		}
51 		return 0;
52 	}
53 
54 	(*cur_inst)++;
55 
56 	return -ENODEV;
57 }
58 
59 static int __cpu_find_by(int (*compare)(int, int, void *), void *compare_arg,
60 			 int *prom_node, int *mid)
61 {
62 	int nd, cur_inst, err;
63 
64 	nd = prom_root_node;
65 	cur_inst = 0;
66 
67 	err = check_cpu_node(nd, &cur_inst, compare, compare_arg,
68 			     prom_node, mid);
69 	if (!err)
70 		return 0;
71 
72 	nd = prom_getchild(nd);
73 	while ((nd = prom_getsibling(nd)) != 0) {
74 		err = check_cpu_node(nd, &cur_inst, compare, compare_arg,
75 				     prom_node, mid);
76 		if (!err)
77 			return 0;
78 	}
79 
80 	return -ENODEV;
81 }
82 
83 static int cpu_instance_compare(int nd, int instance, void *_arg)
84 {
85 	int desired_instance = (int) _arg;
86 
87 	if (instance == desired_instance)
88 		return 0;
89 	return -ENODEV;
90 }
91 
92 int cpu_find_by_instance(int instance, int *prom_node, int *mid)
93 {
94 	return __cpu_find_by(cpu_instance_compare, (void *)instance,
95 			     prom_node, mid);
96 }
97 
98 static int cpu_mid_compare(int nd, int instance, void *_arg)
99 {
100 	int desired_mid = (int) _arg;
101 	int this_mid;
102 
103 	this_mid = prom_getintdefault(nd, cpu_mid_prop(), 0);
104 	if (this_mid == desired_mid
105 	    || (sparc_cpu_model == sun4m && (this_mid & 3) == desired_mid))
106 		return 0;
107 	return -ENODEV;
108 }
109 
110 int cpu_find_by_mid(int mid, int *prom_node)
111 {
112 	return __cpu_find_by(cpu_mid_compare, (void *)mid,
113 			     prom_node, NULL);
114 }
115 
116 /* sun4m uses truncated mids since we base the cpuid on the ttable/irqset
117  * address (0-3).  This gives us the true hardware mid, which might have
118  * some other bits set.  On 4d hardware and software mids are the same.
119  */
120 int cpu_get_hwmid(int prom_node)
121 {
122 	return prom_getintdefault(prom_node, cpu_mid_prop(), -ENODEV);
123 }
124 
125 void __init device_scan(void)
126 {
127 	prom_printf("Booting Linux...\n");
128 
129 #ifndef CONFIG_SMP
130 	{
131 		int err, cpu_node;
132 		err = cpu_find_by_instance(0, &cpu_node, NULL);
133 		if (err) {
134 			/* Probably a sun4e, Sun is trying to trick us ;-) */
135 			prom_printf("No cpu nodes, cannot continue\n");
136 			prom_halt();
137 		}
138 		cpu_data(0).clock_tick = prom_getintdefault(cpu_node,
139 							    "clock-frequency",
140 							    0);
141 	}
142 #endif /* !CONFIG_SMP */
143 
144 	cpu_probe();
145 #ifdef CONFIG_SUN_AUXIO
146 	{
147 		extern void auxio_probe(void);
148 		extern void auxio_power_probe(void);
149 		auxio_probe();
150 		auxio_power_probe();
151 	}
152 #endif
153 	clock_stop_probe();
154 
155 	if (ARCH_SUN4C_SUN4)
156 		sun4c_probe_memerr_reg();
157 
158 	return;
159 }
160