obio.c revision 1.25 1 /* $NetBSD: obio.c,v 1.25 2007/01/18 00:17:22 macallan Exp $ */
2
3 /*-
4 * Copyright (C) 1998 Internet Research Institute, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by
18 * Internet Research Institute, Inc.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.25 2007/01/18 00:17:22 macallan Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcidevs.h>
44
45 #include <dev/ofw/openfirm.h>
46
47 #include <machine/autoconf.h>
48
49 #include <sys/sysctl.h>
50
51 static void obio_attach __P((struct device *, struct device *, void *));
52 static int obio_match __P((struct device *, struct cfdata *, void *));
53 static int obio_print __P((void *, const char *));
54
55 struct obio_softc {
56 struct device sc_dev;
57 int sc_node;
58 };
59
60 static void obio_setup_ohare2(struct obio_softc *, struct confargs *);
61 static int obio_get_cpu_speed(paddr_t);
62 static void obio_set_cpu_speed(paddr_t, int);
63 static void setup_sysctl(paddr_t);
64
65 CFATTACH_DECL(obio, sizeof(struct obio_softc),
66 obio_match, obio_attach, NULL, NULL);
67
68 int
69 obio_match(parent, cf, aux)
70 struct device *parent;
71 struct cfdata *cf;
72 void *aux;
73 {
74 struct pci_attach_args *pa = aux;
75
76 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
77 switch (PCI_PRODUCT(pa->pa_id)) {
78 case PCI_PRODUCT_APPLE_GC:
79 case PCI_PRODUCT_APPLE_OHARE:
80 case PCI_PRODUCT_APPLE_HEATHROW:
81 case PCI_PRODUCT_APPLE_PADDINGTON:
82 case PCI_PRODUCT_APPLE_KEYLARGO:
83 case PCI_PRODUCT_APPLE_PANGEA_MACIO:
84 case PCI_PRODUCT_APPLE_INTREPID:
85 case PCI_PRODUCT_APPLE_K2:
86 return 1;
87 }
88
89 return 0;
90 }
91
92 /*
93 * Attach all the sub-devices we can find
94 */
95 void
96 obio_attach(parent, self, aux)
97 struct device *parent, *self;
98 void *aux;
99 {
100 struct obio_softc *sc = (struct obio_softc *)self;
101 struct pci_attach_args *pa = aux;
102 struct confargs ca;
103 int node, child, namelen;
104 u_int reg[20];
105 int intr[6], parent_intr = 0, parent_nintr = 0;
106 char name[32];
107 char compat[32];
108
109 switch (PCI_PRODUCT(pa->pa_id)) {
110
111 case PCI_PRODUCT_APPLE_GC:
112 case PCI_PRODUCT_APPLE_OHARE:
113 case PCI_PRODUCT_APPLE_HEATHROW:
114 case PCI_PRODUCT_APPLE_PADDINGTON:
115 case PCI_PRODUCT_APPLE_KEYLARGO:
116 case PCI_PRODUCT_APPLE_PANGEA_MACIO:
117 case PCI_PRODUCT_APPLE_INTREPID:
118 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
119 if (node == -1)
120 node = OF_finddevice("mac-io");
121 if (node == -1)
122 node = OF_finddevice("/pci/mac-io");
123 break;
124 case PCI_PRODUCT_APPLE_K2:
125 node = OF_finddevice("mac-io");
126 if (node == -1)
127 panic("macio not found");
128 break;
129
130 default:
131 printf("obio_attach: unknown obio controller\n");
132 return;
133 }
134
135 sc->sc_node = node;
136
137 #if defined (PMAC_G5)
138 if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
139 {
140 return;
141 }
142 #else
143 if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
144 return;
145 #endif /* PMAC_G5 */
146
147 ca.ca_baseaddr = reg[2];
148
149 printf(": addr 0x%x\n", ca.ca_baseaddr);
150
151 /* Enable internal modem (KeyLargo) */
152 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
153 printf("enabling KeyLargo internal modem\n");
154 out32rb(ca.ca_baseaddr + 0x40,
155 in32rb(ca.ca_baseaddr + 0x40) & ~((u_int32_t)1<<25));
156 }
157
158 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID) {
159 paddr_t addr;
160
161 printf("enabling Intrepid CPU speed control\nCPU speed is ");
162 addr = ca.ca_baseaddr + 0x6a;
163 if (obio_get_cpu_speed(addr)) {
164 printf("high\n");
165 } else {
166 printf("low\n");
167 }
168 setup_sysctl(addr);
169 }
170
171 /* Enable internal modem (Pangea) */
172 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
173 out8(ca.ca_baseaddr + 0x006a + 0x03, 0x04); /* set reset */
174 out8(ca.ca_baseaddr + 0x006a + 0x02, 0x04); /* power modem on */
175 out8(ca.ca_baseaddr + 0x006a + 0x03, 0x05); /* unset reset */
176 }
177
178 /* Gatwick and Paddington use same product ID */
179 namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
180
181 if (strcmp(compat, "gatwick") == 0) {
182 parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
183 sizeof(intr));
184 parent_intr = intr[0];
185 } else {
186 /* Enable CD and microphone sound input. */
187 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
188 out8(ca.ca_baseaddr + 0x37, 0x03);
189 }
190
191 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_OHARE) {
192 uint32_t freg;
193
194 freg = in32rb(ca.ca_baseaddr + 0x38);
195 printf("%s: FCR %08x\n", sc->sc_dev.dv_xname, freg);
196
197 if (ca.ca_baseaddr != 0xf3000000) {
198 obio_setup_ohare2(sc, &ca);
199 return;
200 }
201 printf("enabling 2nd IDE channel on ohare\n");
202 freg |= 8;
203 out32rb(ca.ca_baseaddr + 0x38, freg);
204 }
205
206 for (child = OF_child(node); child; child = OF_peer(child)) {
207 namelen = OF_getprop(child, "name", name, sizeof(name));
208 if (namelen < 0)
209 continue;
210 if (namelen >= sizeof(name))
211 continue;
212
213 name[namelen] = 0;
214 ca.ca_name = name;
215 ca.ca_node = child;
216 ca.ca_tag = pa->pa_memt;
217
218 ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
219 if (strcmp(name, "backlight") == 0) {
220 paddr_t addr = (ca.ca_baseaddr + reg[0]);
221 printf("backlight: %08x %08x\n",(uint32_t)addr, in32rb(addr));
222 }
223 if (strcmp(compat, "gatwick") != 0) {
224 ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
225 sizeof(intr));
226 if (ca.ca_nintr == -1)
227 ca.ca_nintr = OF_getprop(child, "interrupts", intr,
228 sizeof(intr));
229 } else {
230 intr[0] = parent_intr;
231 ca.ca_nintr = parent_nintr;
232 }
233 ca.ca_reg = reg;
234 ca.ca_intr = intr;
235
236 config_found(self, &ca, obio_print);
237 }
238 }
239
240 static const char *skiplist[] = {
241 "interrupt-controller",
242 "gpio",
243 "escc-legacy",
244 "timer",
245 "i2c",
246 "power-mgt",
247 "escc"
248
249 };
250
251 #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
252
253 int
254 obio_print(aux, obio)
255 void *aux;
256 const char *obio;
257 {
258 struct confargs *ca = aux;
259 int i;
260
261 for (i = 0; i < N_LIST; i++)
262 if (strcmp(ca->ca_name, skiplist[i]) == 0)
263 return QUIET;
264
265 if (obio)
266 aprint_normal("%s at %s", ca->ca_name, obio);
267
268 if (ca->ca_nreg > 0)
269 aprint_normal(" offset 0x%x", ca->ca_reg[0]);
270
271 return UNCONF;
272 }
273
274 static void
275 obio_setup_ohare2(struct obio_softc *sc, struct confargs *ca)
276 {
277 printf("ohare2: %08x\n", ca->ca_baseaddr);
278 }
279
280 static void
281 obio_set_cpu_speed(paddr_t addr, int fast)
282 {
283
284 if(addr != 0) {
285 if (fast) {
286 out8rb(addr + 1, 5); /* bump Vcore */
287 out8rb(addr, 5); /* bump CPU speed */
288 } else {
289 out8rb(addr, 4); /* lower CPU speed */
290 out8rb(addr + 1, 4); /* lower Vcore */
291 }
292 }
293 }
294
295 static int
296 obio_get_cpu_speed(paddr_t addr)
297 {
298
299 if(addr != 0) {
300 if(in8rb(addr) & 1)
301 return 1;
302 }
303 return 0;
304 }
305
306 SYSCTL_SETUP(sysctl_cpuspeed_setup, "sysctl cpu speed setup")
307 {
308
309 sysctl_createv(NULL, 0, NULL, NULL,
310 CTLFLAG_PERMANENT,
311 CTLTYPE_NODE, "machdep", NULL,
312 NULL, 0, NULL, 0,
313 CTL_MACHDEP, CTL_EOL);
314 }
315
316 static int
317 sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
318 {
319 struct sysctlnode node = *rnode;
320 paddr_t addr = (paddr_t)node.sysctl_data;
321 const int *np = newp;
322 int speed, nd = 0;
323
324 speed = obio_get_cpu_speed(addr);
325 node.sysctl_idata = speed;
326 if (np) {
327 /* we're asked to write */
328 nd = *np;
329 node.sysctl_data = &speed;
330 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
331 int new_reg;
332
333 new_reg = (max(0, min(1, node.sysctl_idata)));
334 obio_set_cpu_speed(addr, new_reg);
335 return 0;
336 }
337 return EINVAL;
338 } else {
339 node.sysctl_size = 4;
340 return(sysctl_lookup(SYSCTLFN_CALL(&node)));
341 }
342 }
343
344 static void
345 setup_sysctl(paddr_t addr)
346 {
347 struct sysctlnode *node=NULL;
348 int ret;
349
350 ret = sysctl_createv(NULL, 0, NULL,
351 (const struct sysctlnode **)&node,
352 CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
353 CTLTYPE_INT, "cpu_speed", "CPU speed", sysctl_cpuspeed_temp,
354 addr , NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
355 if (node != NULL) {
356 node->sysctl_data = (void *)addr;
357 }
358 }
359