obio.c revision 1.29 1 1.29 dsl /* $NetBSD: obio.c,v 1.29 2009/03/14 15:36:09 dsl Exp $ */
2 1.2 tsubai
3 1.2 tsubai /*-
4 1.2 tsubai * Copyright (C) 1998 Internet Research Institute, Inc.
5 1.2 tsubai * All rights reserved.
6 1.2 tsubai *
7 1.2 tsubai * Redistribution and use in source and binary forms, with or without
8 1.2 tsubai * modification, are permitted provided that the following conditions
9 1.2 tsubai * are met:
10 1.2 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.2 tsubai * notice, this list of conditions and the following disclaimer.
12 1.2 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.2 tsubai * documentation and/or other materials provided with the distribution.
15 1.2 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.2 tsubai * must display the following acknowledgement:
17 1.2 tsubai * This product includes software developed by
18 1.2 tsubai * Internet Research Institute, Inc.
19 1.2 tsubai * 4. The name of the author may not be used to endorse or promote products
20 1.2 tsubai * derived from this software without specific prior written permission.
21 1.2 tsubai *
22 1.2 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.2 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.2 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.2 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.2 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.2 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.2 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.2 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.2 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.2 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.2 tsubai */
33 1.19 lukem
34 1.19 lukem #include <sys/cdefs.h>
35 1.29 dsl __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.29 2009/03/14 15:36:09 dsl Exp $");
36 1.2 tsubai
37 1.1 tsubai #include <sys/param.h>
38 1.1 tsubai #include <sys/systm.h>
39 1.1 tsubai #include <sys/kernel.h>
40 1.1 tsubai #include <sys/device.h>
41 1.27 garbled #include <sys/sysctl.h>
42 1.1 tsubai
43 1.1 tsubai #include <dev/pci/pcivar.h>
44 1.1 tsubai #include <dev/pci/pcidevs.h>
45 1.1 tsubai
46 1.1 tsubai #include <dev/ofw/openfirm.h>
47 1.1 tsubai
48 1.1 tsubai #include <machine/autoconf.h>
49 1.1 tsubai
50 1.28 macallan #include <macppc/dev/obiovar.h>
51 1.28 macallan
52 1.27 garbled #include "opt_obio.h"
53 1.27 garbled
54 1.27 garbled #ifdef OBIO_DEBUG
55 1.27 garbled # define DPRINTF printf
56 1.27 garbled #else
57 1.27 garbled # define DPRINTF while (0) printf
58 1.27 garbled #endif
59 1.27 garbled
60 1.27 garbled static void obio_attach(struct device *, struct device *, void *);
61 1.27 garbled static int obio_match(struct device *, struct cfdata *, void *);
62 1.27 garbled static int obio_print(void *, const char *);
63 1.1 tsubai
64 1.1 tsubai struct obio_softc {
65 1.1 tsubai struct device sc_dev;
66 1.27 garbled bus_space_tag_t sc_tag;
67 1.27 garbled bus_space_handle_t sc_bh;
68 1.1 tsubai int sc_node;
69 1.27 garbled #ifdef OBIO_SPEED_CONTROL
70 1.27 garbled int sc_voltage;
71 1.27 garbled int sc_busspeed;
72 1.27 garbled #endif
73 1.1 tsubai };
74 1.1 tsubai
75 1.28 macallan static struct obio_softc *obio0 = NULL;
76 1.28 macallan
77 1.27 garbled #ifdef OBIO_SPEED_CONTROL
78 1.27 garbled static void obio_setup_gpios(struct obio_softc *, int);
79 1.27 garbled static void obio_set_cpu_speed(struct obio_softc *, int);
80 1.27 garbled static int obio_get_cpu_speed(struct obio_softc *);
81 1.27 garbled static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
82 1.27 garbled
83 1.27 garbled static const char *keylargo[] = {"Keylargo",
84 1.27 garbled "AAPL,Keylargo",
85 1.27 garbled NULL};
86 1.27 garbled
87 1.27 garbled #endif
88 1.1 tsubai
89 1.16 thorpej CFATTACH_DECL(obio, sizeof(struct obio_softc),
90 1.16 thorpej obio_match, obio_attach, NULL, NULL);
91 1.1 tsubai
92 1.1 tsubai int
93 1.27 garbled obio_match(struct device *parent, struct cfdata *cf, void *aux)
94 1.1 tsubai {
95 1.1 tsubai struct pci_attach_args *pa = aux;
96 1.1 tsubai
97 1.2 tsubai if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
98 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
99 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
100 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
101 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
102 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
103 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
104 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
105 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
106 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
107 1.2 tsubai return 1;
108 1.2 tsubai }
109 1.1 tsubai
110 1.1 tsubai return 0;
111 1.1 tsubai }
112 1.1 tsubai
113 1.1 tsubai /*
114 1.1 tsubai * Attach all the sub-devices we can find
115 1.1 tsubai */
116 1.1 tsubai void
117 1.27 garbled obio_attach(struct device *parent, struct device *self, void *aux)
118 1.1 tsubai {
119 1.1 tsubai struct obio_softc *sc = (struct obio_softc *)self;
120 1.2 tsubai struct pci_attach_args *pa = aux;
121 1.1 tsubai struct confargs ca;
122 1.27 garbled bus_space_handle_t bsh;
123 1.27 garbled int node, child, namelen, error;
124 1.1 tsubai u_int reg[20];
125 1.21 briggs int intr[6], parent_intr = 0, parent_nintr = 0;
126 1.1 tsubai char name[32];
127 1.21 briggs char compat[32];
128 1.1 tsubai
129 1.27 garbled #ifdef OBIO_SPEED_CONTROL
130 1.27 garbled sc->sc_voltage = -1;
131 1.27 garbled sc->sc_busspeed = -1;
132 1.27 garbled #endif
133 1.27 garbled
134 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
135 1.2 tsubai
136 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
137 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
138 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
139 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
140 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
141 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
142 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
143 1.21 briggs node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
144 1.6 tsubai if (node == -1)
145 1.21 briggs node = OF_finddevice("mac-io");
146 1.21 briggs if (node == -1)
147 1.21 briggs node = OF_finddevice("/pci/mac-io");
148 1.2 tsubai break;
149 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
150 1.24 sanjayl node = OF_finddevice("mac-io");
151 1.24 sanjayl break;
152 1.2 tsubai
153 1.2 tsubai default:
154 1.27 garbled node = -1;
155 1.27 garbled break;
156 1.2 tsubai }
157 1.27 garbled if (node == -1)
158 1.27 garbled panic("macio not found or unknown");
159 1.2 tsubai
160 1.1 tsubai sc->sc_node = node;
161 1.1 tsubai
162 1.24 sanjayl #if defined (PMAC_G5)
163 1.24 sanjayl if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
164 1.24 sanjayl {
165 1.24 sanjayl return;
166 1.24 sanjayl }
167 1.24 sanjayl #else
168 1.1 tsubai if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
169 1.1 tsubai return;
170 1.24 sanjayl #endif /* PMAC_G5 */
171 1.24 sanjayl
172 1.28 macallan /*
173 1.28 macallan * XXX
174 1.28 macallan * This relies on the primary obio always attaching first which is
175 1.28 macallan * true on the PowerBook 3400c and similar machines but may or may
176 1.28 macallan * not work on others. We can't rely on the node name since Apple
177 1.28 macallan * didn't follow anything remotely resembling a consistent naming
178 1.28 macallan * scheme.
179 1.28 macallan */
180 1.28 macallan if (obio0 == NULL)
181 1.28 macallan obio0 = sc;
182 1.28 macallan
183 1.1 tsubai ca.ca_baseaddr = reg[2];
184 1.27 garbled ca.ca_tag = pa->pa_memt;
185 1.27 garbled sc->sc_tag = pa->pa_memt;
186 1.27 garbled error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, 0x80, 0, &bsh);
187 1.27 garbled if (error)
188 1.27 garbled panic(": failed to map mac-io %#x", ca.ca_baseaddr);
189 1.27 garbled sc->sc_bh = bsh;
190 1.1 tsubai
191 1.1 tsubai printf(": addr 0x%x\n", ca.ca_baseaddr);
192 1.13 tsubai
193 1.20 briggs /* Enable internal modem (KeyLargo) */
194 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
195 1.27 garbled aprint_normal("%s: enabling KeyLargo internal modem\n",
196 1.27 garbled self->dv_xname);
197 1.27 garbled bus_space_write_4(ca.ca_tag, bsh, 0x40,
198 1.27 garbled bus_space_read_4(ca.ca_tag, bsh, 0x40) & ~(1<<25));
199 1.20 briggs }
200 1.26 macallan
201 1.20 briggs /* Enable internal modem (Pangea) */
202 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
203 1.27 garbled /* set reset */
204 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04);
205 1.27 garbled /* power modem on */
206 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04);
207 1.27 garbled /* unset reset */
208 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x05);
209 1.20 briggs }
210 1.20 briggs
211 1.21 briggs /* Gatwick and Paddington use same product ID */
212 1.21 briggs namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
213 1.1 tsubai
214 1.21 briggs if (strcmp(compat, "gatwick") == 0) {
215 1.21 briggs parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
216 1.21 briggs sizeof(intr));
217 1.21 briggs parent_intr = intr[0];
218 1.21 briggs } else {
219 1.21 briggs /* Enable CD and microphone sound input. */
220 1.21 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
221 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
222 1.21 briggs }
223 1.27 garbled
224 1.1 tsubai for (child = OF_child(node); child; child = OF_peer(child)) {
225 1.1 tsubai namelen = OF_getprop(child, "name", name, sizeof(name));
226 1.1 tsubai if (namelen < 0)
227 1.1 tsubai continue;
228 1.1 tsubai if (namelen >= sizeof(name))
229 1.1 tsubai continue;
230 1.1 tsubai
231 1.27 garbled #ifdef OBIO_SPEED_CONTROL
232 1.27 garbled if (strcmp(name, "gpio") == 0) {
233 1.27 garbled
234 1.27 garbled obio_setup_gpios(sc, child);
235 1.27 garbled continue;
236 1.27 garbled }
237 1.27 garbled #endif
238 1.27 garbled
239 1.1 tsubai name[namelen] = 0;
240 1.1 tsubai ca.ca_name = name;
241 1.1 tsubai ca.ca_node = child;
242 1.25 macallan ca.ca_tag = pa->pa_memt;
243 1.26 macallan
244 1.21 briggs ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
245 1.26 macallan
246 1.21 briggs if (strcmp(compat, "gatwick") != 0) {
247 1.21 briggs ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
248 1.5 tsubai sizeof(intr));
249 1.21 briggs if (ca.ca_nintr == -1)
250 1.21 briggs ca.ca_nintr = OF_getprop(child, "interrupts", intr,
251 1.21 briggs sizeof(intr));
252 1.21 briggs } else {
253 1.21 briggs intr[0] = parent_intr;
254 1.21 briggs ca.ca_nintr = parent_nintr;
255 1.21 briggs }
256 1.1 tsubai ca.ca_reg = reg;
257 1.1 tsubai ca.ca_intr = intr;
258 1.1 tsubai
259 1.1 tsubai config_found(self, &ca, obio_print);
260 1.1 tsubai }
261 1.1 tsubai }
262 1.1 tsubai
263 1.27 garbled static const char * const skiplist[] = {
264 1.8 tsubai "interrupt-controller",
265 1.8 tsubai "gpio",
266 1.8 tsubai "escc-legacy",
267 1.8 tsubai "timer",
268 1.9 tsubai "i2c",
269 1.24 sanjayl "power-mgt",
270 1.27 garbled "escc",
271 1.27 garbled "battery",
272 1.27 garbled "backlight"
273 1.24 sanjayl
274 1.8 tsubai };
275 1.8 tsubai
276 1.8 tsubai #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
277 1.8 tsubai
278 1.1 tsubai int
279 1.29 dsl obio_print(void *aux, const char *obio)
280 1.1 tsubai {
281 1.1 tsubai struct confargs *ca = aux;
282 1.8 tsubai int i;
283 1.8 tsubai
284 1.8 tsubai for (i = 0; i < N_LIST; i++)
285 1.8 tsubai if (strcmp(ca->ca_name, skiplist[i]) == 0)
286 1.8 tsubai return QUIET;
287 1.1 tsubai
288 1.1 tsubai if (obio)
289 1.17 thorpej aprint_normal("%s at %s", ca->ca_name, obio);
290 1.1 tsubai
291 1.1 tsubai if (ca->ca_nreg > 0)
292 1.17 thorpej aprint_normal(" offset 0x%x", ca->ca_reg[0]);
293 1.1 tsubai
294 1.1 tsubai return UNCONF;
295 1.1 tsubai }
296 1.27 garbled
297 1.28 macallan void obio_write_4(int offset, uint32_t value)
298 1.28 macallan {
299 1.28 macallan if (obio0 == NULL)
300 1.28 macallan return;
301 1.28 macallan bus_space_write_4(obio0->sc_tag, obio0->sc_bh, offset, value);
302 1.28 macallan }
303 1.28 macallan
304 1.28 macallan void obio_write_1(int offset, uint8_t value)
305 1.28 macallan {
306 1.28 macallan if (obio0 == NULL)
307 1.28 macallan return;
308 1.28 macallan bus_space_write_1(obio0->sc_tag, obio0->sc_bh, offset, value);
309 1.28 macallan }
310 1.28 macallan
311 1.28 macallan uint32_t obio_read_4(int offset)
312 1.28 macallan {
313 1.28 macallan if (obio0 == NULL)
314 1.28 macallan return 0xffffffff;
315 1.28 macallan return bus_space_read_4(obio0->sc_tag, obio0->sc_bh, offset);
316 1.28 macallan }
317 1.28 macallan
318 1.28 macallan uint8_t obio_read_1(int offset)
319 1.28 macallan {
320 1.28 macallan if (obio0 == NULL)
321 1.28 macallan return 0xff;
322 1.28 macallan return bus_space_read_1(obio0->sc_tag, obio0->sc_bh, offset);
323 1.28 macallan }
324 1.28 macallan
325 1.27 garbled #ifdef OBIO_SPEED_CONTROL
326 1.27 garbled
327 1.27 garbled static void
328 1.27 garbled obio_setup_gpios(struct obio_softc *sc, int node)
329 1.27 garbled {
330 1.27 garbled uint32_t reg[6];
331 1.27 garbled struct sysctlnode *sysctl_node = NULL;
332 1.27 garbled char name[32];
333 1.27 garbled int gpio_base, child;
334 1.27 garbled
335 1.27 garbled if (of_compatible(sc->sc_node, keylargo) == -1)
336 1.27 garbled return;
337 1.27 garbled
338 1.27 garbled if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
339 1.27 garbled return;
340 1.27 garbled
341 1.27 garbled gpio_base = reg[0];
342 1.27 garbled DPRINTF("gpio_base: %02x\n", gpio_base);
343 1.27 garbled
344 1.27 garbled /* now look for voltage and bus speed gpios */
345 1.27 garbled for (child = OF_child(node); child; child = OF_peer(child)) {
346 1.27 garbled
347 1.27 garbled if (OF_getprop(child, "name", name, sizeof(name)) < 1)
348 1.27 garbled continue;
349 1.27 garbled
350 1.27 garbled if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
351 1.27 garbled continue;
352 1.27 garbled
353 1.27 garbled if (strcmp(name, "frequency-gpio") == 0) {
354 1.27 garbled DPRINTF("found frequency_gpio at %02x\n", reg[0]);
355 1.27 garbled sc->sc_busspeed = gpio_base + reg[0];
356 1.27 garbled }
357 1.27 garbled if (strcmp(name, "voltage-gpio") == 0) {
358 1.27 garbled DPRINTF("found voltage_gpio at %02x\n", reg[0]);
359 1.27 garbled sc->sc_voltage = gpio_base + reg[0];
360 1.27 garbled }
361 1.27 garbled }
362 1.27 garbled
363 1.27 garbled if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
364 1.27 garbled return;
365 1.27 garbled
366 1.27 garbled printf("%s: enabling Intrepid CPU speed control\n",
367 1.27 garbled sc->sc_dev.dv_xname);
368 1.27 garbled
369 1.27 garbled sysctl_createv(NULL, 0, NULL,
370 1.27 garbled (const struct sysctlnode **)&sysctl_node,
371 1.27 garbled CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
372 1.27 garbled CTLTYPE_INT, "cpu_speed", "CPU speed", sysctl_cpuspeed_temp,
373 1.27 garbled (unsigned long)sc, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
374 1.27 garbled if (sysctl_node != NULL) {
375 1.27 garbled sysctl_node->sysctl_data = (void *)sc;
376 1.27 garbled }
377 1.27 garbled }
378 1.27 garbled
379 1.27 garbled static void
380 1.27 garbled obio_set_cpu_speed(struct obio_softc *sc, int fast)
381 1.27 garbled {
382 1.27 garbled
383 1.27 garbled if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
384 1.27 garbled return;
385 1.27 garbled
386 1.27 garbled if (fast) {
387 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_voltage, 5);
388 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed, 5);
389 1.27 garbled } else {
390 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed, 4);
391 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_voltage, 4);
392 1.27 garbled }
393 1.27 garbled }
394 1.27 garbled
395 1.27 garbled static int
396 1.27 garbled obio_get_cpu_speed(struct obio_softc *sc)
397 1.27 garbled {
398 1.27 garbled
399 1.27 garbled if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
400 1.27 garbled return 0;
401 1.27 garbled
402 1.27 garbled if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed) & 1)
403 1.27 garbled return 1;
404 1.27 garbled
405 1.27 garbled return 0;
406 1.27 garbled }
407 1.27 garbled
408 1.27 garbled static int
409 1.27 garbled sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
410 1.27 garbled {
411 1.27 garbled struct sysctlnode node = *rnode;
412 1.27 garbled struct obio_softc *sc = node.sysctl_data;
413 1.27 garbled const int *np = newp;
414 1.27 garbled int speed, nd = 0;
415 1.27 garbled
416 1.27 garbled speed = obio_get_cpu_speed(sc);
417 1.27 garbled node.sysctl_idata = speed;
418 1.27 garbled if (np) {
419 1.27 garbled /* we're asked to write */
420 1.27 garbled nd = *np;
421 1.27 garbled node.sysctl_data = &speed;
422 1.27 garbled if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
423 1.27 garbled int new_reg;
424 1.27 garbled
425 1.27 garbled new_reg = (max(0, min(1, node.sysctl_idata)));
426 1.27 garbled obio_set_cpu_speed(sc, new_reg);
427 1.27 garbled return 0;
428 1.27 garbled }
429 1.27 garbled return EINVAL;
430 1.27 garbled } else {
431 1.27 garbled node.sysctl_size = 4;
432 1.27 garbled return(sysctl_lookup(SYSCTLFN_CALL(&node)));
433 1.27 garbled }
434 1.27 garbled }
435 1.27 garbled
436 1.27 garbled #endif /* OBIO_SPEEDCONTROL */
437 1.27 garbled
438