obio.c revision 1.27 1 1.27 garbled /* $NetBSD: obio.c,v 1.27 2007/10/17 19:55:19 garbled 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.27 garbled __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.27 2007/10/17 19:55:19 garbled 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.27 garbled #include "opt_obio.h"
51 1.27 garbled
52 1.27 garbled #ifdef OBIO_DEBUG
53 1.27 garbled # define DPRINTF printf
54 1.27 garbled #else
55 1.27 garbled # define DPRINTF while (0) printf
56 1.27 garbled #endif
57 1.27 garbled
58 1.27 garbled static void obio_attach(struct device *, struct device *, void *);
59 1.27 garbled static int obio_match(struct device *, struct cfdata *, void *);
60 1.27 garbled static int obio_print(void *, const char *);
61 1.1 tsubai
62 1.1 tsubai struct obio_softc {
63 1.1 tsubai struct device sc_dev;
64 1.27 garbled bus_space_tag_t sc_tag;
65 1.27 garbled bus_space_handle_t sc_bh;
66 1.1 tsubai int sc_node;
67 1.27 garbled #ifdef OBIO_SPEED_CONTROL
68 1.27 garbled int sc_voltage;
69 1.27 garbled int sc_busspeed;
70 1.27 garbled #endif
71 1.1 tsubai };
72 1.1 tsubai
73 1.27 garbled #ifdef OBIO_SPEED_CONTROL
74 1.27 garbled static void obio_setup_gpios(struct obio_softc *, int);
75 1.27 garbled static void obio_set_cpu_speed(struct obio_softc *, int);
76 1.27 garbled static int obio_get_cpu_speed(struct obio_softc *);
77 1.27 garbled static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
78 1.27 garbled
79 1.27 garbled static const char *keylargo[] = {"Keylargo",
80 1.27 garbled "AAPL,Keylargo",
81 1.27 garbled NULL};
82 1.27 garbled
83 1.27 garbled #endif
84 1.1 tsubai
85 1.16 thorpej CFATTACH_DECL(obio, sizeof(struct obio_softc),
86 1.16 thorpej obio_match, obio_attach, NULL, NULL);
87 1.1 tsubai
88 1.1 tsubai int
89 1.27 garbled obio_match(struct device *parent, struct cfdata *cf, void *aux)
90 1.1 tsubai {
91 1.1 tsubai struct pci_attach_args *pa = aux;
92 1.1 tsubai
93 1.2 tsubai if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
94 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
95 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
96 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
97 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
98 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
99 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
100 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
101 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
102 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
103 1.2 tsubai return 1;
104 1.2 tsubai }
105 1.1 tsubai
106 1.1 tsubai return 0;
107 1.1 tsubai }
108 1.1 tsubai
109 1.1 tsubai /*
110 1.1 tsubai * Attach all the sub-devices we can find
111 1.1 tsubai */
112 1.1 tsubai void
113 1.27 garbled obio_attach(struct device *parent, struct device *self, void *aux)
114 1.1 tsubai {
115 1.1 tsubai struct obio_softc *sc = (struct obio_softc *)self;
116 1.2 tsubai struct pci_attach_args *pa = aux;
117 1.1 tsubai struct confargs ca;
118 1.27 garbled bus_space_handle_t bsh;
119 1.27 garbled int node, child, namelen, error;
120 1.1 tsubai u_int reg[20];
121 1.21 briggs int intr[6], parent_intr = 0, parent_nintr = 0;
122 1.1 tsubai char name[32];
123 1.21 briggs char compat[32];
124 1.1 tsubai
125 1.27 garbled #ifdef OBIO_SPEED_CONTROL
126 1.27 garbled sc->sc_voltage = -1;
127 1.27 garbled sc->sc_busspeed = -1;
128 1.27 garbled #endif
129 1.27 garbled
130 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
131 1.2 tsubai
132 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
133 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
134 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
135 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
136 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
137 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
138 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
139 1.21 briggs node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
140 1.6 tsubai if (node == -1)
141 1.21 briggs node = OF_finddevice("mac-io");
142 1.21 briggs if (node == -1)
143 1.21 briggs node = OF_finddevice("/pci/mac-io");
144 1.2 tsubai break;
145 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
146 1.24 sanjayl node = OF_finddevice("mac-io");
147 1.24 sanjayl break;
148 1.2 tsubai
149 1.2 tsubai default:
150 1.27 garbled node = -1;
151 1.27 garbled break;
152 1.2 tsubai }
153 1.27 garbled if (node == -1)
154 1.27 garbled panic("macio not found or unknown");
155 1.2 tsubai
156 1.1 tsubai sc->sc_node = node;
157 1.1 tsubai
158 1.24 sanjayl #if defined (PMAC_G5)
159 1.24 sanjayl if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
160 1.24 sanjayl {
161 1.24 sanjayl return;
162 1.24 sanjayl }
163 1.24 sanjayl #else
164 1.1 tsubai if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
165 1.1 tsubai return;
166 1.24 sanjayl #endif /* PMAC_G5 */
167 1.24 sanjayl
168 1.1 tsubai ca.ca_baseaddr = reg[2];
169 1.27 garbled ca.ca_tag = pa->pa_memt;
170 1.27 garbled sc->sc_tag = pa->pa_memt;
171 1.27 garbled error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, 0x80, 0, &bsh);
172 1.27 garbled if (error)
173 1.27 garbled panic(": failed to map mac-io %#x", ca.ca_baseaddr);
174 1.27 garbled sc->sc_bh = bsh;
175 1.1 tsubai
176 1.1 tsubai printf(": addr 0x%x\n", ca.ca_baseaddr);
177 1.13 tsubai
178 1.20 briggs /* Enable internal modem (KeyLargo) */
179 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
180 1.27 garbled aprint_normal("%s: enabling KeyLargo internal modem\n",
181 1.27 garbled self->dv_xname);
182 1.27 garbled bus_space_write_4(ca.ca_tag, bsh, 0x40,
183 1.27 garbled bus_space_read_4(ca.ca_tag, bsh, 0x40) & ~(1<<25));
184 1.20 briggs }
185 1.26 macallan
186 1.20 briggs /* Enable internal modem (Pangea) */
187 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
188 1.27 garbled /* set reset */
189 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04);
190 1.27 garbled /* power modem on */
191 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04);
192 1.27 garbled /* unset reset */
193 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x05);
194 1.20 briggs }
195 1.20 briggs
196 1.21 briggs /* Gatwick and Paddington use same product ID */
197 1.21 briggs namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
198 1.1 tsubai
199 1.21 briggs if (strcmp(compat, "gatwick") == 0) {
200 1.21 briggs parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
201 1.21 briggs sizeof(intr));
202 1.21 briggs parent_intr = intr[0];
203 1.21 briggs } else {
204 1.21 briggs /* Enable CD and microphone sound input. */
205 1.21 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
206 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
207 1.21 briggs }
208 1.27 garbled
209 1.1 tsubai for (child = OF_child(node); child; child = OF_peer(child)) {
210 1.1 tsubai namelen = OF_getprop(child, "name", name, sizeof(name));
211 1.1 tsubai if (namelen < 0)
212 1.1 tsubai continue;
213 1.1 tsubai if (namelen >= sizeof(name))
214 1.1 tsubai continue;
215 1.1 tsubai
216 1.27 garbled #ifdef OBIO_SPEED_CONTROL
217 1.27 garbled if (strcmp(name, "gpio") == 0) {
218 1.27 garbled
219 1.27 garbled obio_setup_gpios(sc, child);
220 1.27 garbled continue;
221 1.27 garbled }
222 1.27 garbled #endif
223 1.27 garbled
224 1.1 tsubai name[namelen] = 0;
225 1.1 tsubai ca.ca_name = name;
226 1.1 tsubai ca.ca_node = child;
227 1.25 macallan ca.ca_tag = pa->pa_memt;
228 1.26 macallan
229 1.21 briggs ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
230 1.26 macallan
231 1.21 briggs if (strcmp(compat, "gatwick") != 0) {
232 1.21 briggs ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
233 1.5 tsubai sizeof(intr));
234 1.21 briggs if (ca.ca_nintr == -1)
235 1.21 briggs ca.ca_nintr = OF_getprop(child, "interrupts", intr,
236 1.21 briggs sizeof(intr));
237 1.21 briggs } else {
238 1.21 briggs intr[0] = parent_intr;
239 1.21 briggs ca.ca_nintr = parent_nintr;
240 1.21 briggs }
241 1.1 tsubai ca.ca_reg = reg;
242 1.1 tsubai ca.ca_intr = intr;
243 1.1 tsubai
244 1.1 tsubai config_found(self, &ca, obio_print);
245 1.1 tsubai }
246 1.1 tsubai }
247 1.1 tsubai
248 1.27 garbled static const char * const skiplist[] = {
249 1.8 tsubai "interrupt-controller",
250 1.8 tsubai "gpio",
251 1.8 tsubai "escc-legacy",
252 1.8 tsubai "timer",
253 1.9 tsubai "i2c",
254 1.24 sanjayl "power-mgt",
255 1.27 garbled "escc",
256 1.27 garbled "battery",
257 1.27 garbled "backlight"
258 1.24 sanjayl
259 1.8 tsubai };
260 1.8 tsubai
261 1.8 tsubai #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
262 1.8 tsubai
263 1.1 tsubai int
264 1.1 tsubai obio_print(aux, obio)
265 1.1 tsubai void *aux;
266 1.1 tsubai const char *obio;
267 1.1 tsubai {
268 1.1 tsubai struct confargs *ca = aux;
269 1.8 tsubai int i;
270 1.8 tsubai
271 1.8 tsubai for (i = 0; i < N_LIST; i++)
272 1.8 tsubai if (strcmp(ca->ca_name, skiplist[i]) == 0)
273 1.8 tsubai return QUIET;
274 1.1 tsubai
275 1.1 tsubai if (obio)
276 1.17 thorpej aprint_normal("%s at %s", ca->ca_name, obio);
277 1.1 tsubai
278 1.1 tsubai if (ca->ca_nreg > 0)
279 1.17 thorpej aprint_normal(" offset 0x%x", ca->ca_reg[0]);
280 1.1 tsubai
281 1.1 tsubai return UNCONF;
282 1.1 tsubai }
283 1.27 garbled
284 1.27 garbled #ifdef OBIO_SPEED_CONTROL
285 1.27 garbled
286 1.27 garbled static void
287 1.27 garbled obio_setup_gpios(struct obio_softc *sc, int node)
288 1.27 garbled {
289 1.27 garbled uint32_t reg[6];
290 1.27 garbled struct sysctlnode *sysctl_node = NULL;
291 1.27 garbled char name[32];
292 1.27 garbled int gpio_base, child;
293 1.27 garbled
294 1.27 garbled if (of_compatible(sc->sc_node, keylargo) == -1)
295 1.27 garbled return;
296 1.27 garbled
297 1.27 garbled if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
298 1.27 garbled return;
299 1.27 garbled
300 1.27 garbled gpio_base = reg[0];
301 1.27 garbled DPRINTF("gpio_base: %02x\n", gpio_base);
302 1.27 garbled
303 1.27 garbled /* now look for voltage and bus speed gpios */
304 1.27 garbled for (child = OF_child(node); child; child = OF_peer(child)) {
305 1.27 garbled
306 1.27 garbled if (OF_getprop(child, "name", name, sizeof(name)) < 1)
307 1.27 garbled continue;
308 1.27 garbled
309 1.27 garbled if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
310 1.27 garbled continue;
311 1.27 garbled
312 1.27 garbled if (strcmp(name, "frequency-gpio") == 0) {
313 1.27 garbled DPRINTF("found frequency_gpio at %02x\n", reg[0]);
314 1.27 garbled sc->sc_busspeed = gpio_base + reg[0];
315 1.27 garbled }
316 1.27 garbled if (strcmp(name, "voltage-gpio") == 0) {
317 1.27 garbled DPRINTF("found voltage_gpio at %02x\n", reg[0]);
318 1.27 garbled sc->sc_voltage = gpio_base + reg[0];
319 1.27 garbled }
320 1.27 garbled }
321 1.27 garbled
322 1.27 garbled if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
323 1.27 garbled return;
324 1.27 garbled
325 1.27 garbled printf("%s: enabling Intrepid CPU speed control\n",
326 1.27 garbled sc->sc_dev.dv_xname);
327 1.27 garbled
328 1.27 garbled sysctl_createv(NULL, 0, NULL,
329 1.27 garbled (const struct sysctlnode **)&sysctl_node,
330 1.27 garbled CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
331 1.27 garbled CTLTYPE_INT, "cpu_speed", "CPU speed", sysctl_cpuspeed_temp,
332 1.27 garbled (unsigned long)sc, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
333 1.27 garbled if (sysctl_node != NULL) {
334 1.27 garbled sysctl_node->sysctl_data = (void *)sc;
335 1.27 garbled }
336 1.27 garbled }
337 1.27 garbled
338 1.27 garbled static void
339 1.27 garbled obio_set_cpu_speed(struct obio_softc *sc, int fast)
340 1.27 garbled {
341 1.27 garbled
342 1.27 garbled if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
343 1.27 garbled return;
344 1.27 garbled
345 1.27 garbled if (fast) {
346 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_voltage, 5);
347 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed, 5);
348 1.27 garbled } else {
349 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed, 4);
350 1.27 garbled bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_voltage, 4);
351 1.27 garbled }
352 1.27 garbled }
353 1.27 garbled
354 1.27 garbled static int
355 1.27 garbled obio_get_cpu_speed(struct obio_softc *sc)
356 1.27 garbled {
357 1.27 garbled
358 1.27 garbled if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
359 1.27 garbled return 0;
360 1.27 garbled
361 1.27 garbled if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed) & 1)
362 1.27 garbled return 1;
363 1.27 garbled
364 1.27 garbled return 0;
365 1.27 garbled }
366 1.27 garbled
367 1.27 garbled static int
368 1.27 garbled sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
369 1.27 garbled {
370 1.27 garbled struct sysctlnode node = *rnode;
371 1.27 garbled struct obio_softc *sc = node.sysctl_data;
372 1.27 garbled const int *np = newp;
373 1.27 garbled int speed, nd = 0;
374 1.27 garbled
375 1.27 garbled speed = obio_get_cpu_speed(sc);
376 1.27 garbled node.sysctl_idata = speed;
377 1.27 garbled if (np) {
378 1.27 garbled /* we're asked to write */
379 1.27 garbled nd = *np;
380 1.27 garbled node.sysctl_data = &speed;
381 1.27 garbled if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
382 1.27 garbled int new_reg;
383 1.27 garbled
384 1.27 garbled new_reg = (max(0, min(1, node.sysctl_idata)));
385 1.27 garbled obio_set_cpu_speed(sc, new_reg);
386 1.27 garbled return 0;
387 1.27 garbled }
388 1.27 garbled return EINVAL;
389 1.27 garbled } else {
390 1.27 garbled node.sysctl_size = 4;
391 1.27 garbled return(sysctl_lookup(SYSCTLFN_CALL(&node)));
392 1.27 garbled }
393 1.27 garbled }
394 1.27 garbled
395 1.27 garbled #endif /* OBIO_SPEEDCONTROL */
396 1.27 garbled
397