obio.c revision 1.26.26.1 1 1.26.26.1 yamt /* $NetBSD: obio.c,v 1.26.26.1 2007/10/18 08:32:09 yamt 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.26.26.1 yamt __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.26.26.1 2007/10/18 08:32:09 yamt 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.26.26.1 yamt #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.26.26.1 yamt #include "opt_obio.h"
51 1.26.26.1 yamt
52 1.26.26.1 yamt #ifdef OBIO_DEBUG
53 1.26.26.1 yamt # define DPRINTF printf
54 1.26.26.1 yamt #else
55 1.26.26.1 yamt # define DPRINTF while (0) printf
56 1.26.26.1 yamt #endif
57 1.26.26.1 yamt
58 1.26.26.1 yamt static void obio_attach(struct device *, struct device *, void *);
59 1.26.26.1 yamt static int obio_match(struct device *, struct cfdata *, void *);
60 1.26.26.1 yamt 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.26.26.1 yamt bus_space_tag_t sc_tag;
65 1.26.26.1 yamt bus_space_handle_t sc_bh;
66 1.1 tsubai int sc_node;
67 1.26.26.1 yamt #ifdef OBIO_SPEED_CONTROL
68 1.26.26.1 yamt int sc_voltage;
69 1.26.26.1 yamt int sc_busspeed;
70 1.26.26.1 yamt #endif
71 1.1 tsubai };
72 1.1 tsubai
73 1.26.26.1 yamt #ifdef OBIO_SPEED_CONTROL
74 1.26.26.1 yamt static void obio_setup_gpios(struct obio_softc *, int);
75 1.26.26.1 yamt static void obio_set_cpu_speed(struct obio_softc *, int);
76 1.26.26.1 yamt static int obio_get_cpu_speed(struct obio_softc *);
77 1.26.26.1 yamt static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
78 1.26.26.1 yamt
79 1.26.26.1 yamt static const char *keylargo[] = {"Keylargo",
80 1.26.26.1 yamt "AAPL,Keylargo",
81 1.26.26.1 yamt NULL};
82 1.26.26.1 yamt
83 1.26.26.1 yamt #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.26.26.1 yamt 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.26.26.1 yamt 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.26.26.1 yamt bus_space_handle_t bsh;
119 1.26.26.1 yamt 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.26.26.1 yamt #ifdef OBIO_SPEED_CONTROL
126 1.26.26.1 yamt sc->sc_voltage = -1;
127 1.26.26.1 yamt sc->sc_busspeed = -1;
128 1.26.26.1 yamt #endif
129 1.26.26.1 yamt
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.26.26.1 yamt node = -1;
151 1.26.26.1 yamt break;
152 1.2 tsubai }
153 1.26.26.1 yamt if (node == -1)
154 1.26.26.1 yamt 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.26.26.1 yamt ca.ca_tag = pa->pa_memt;
170 1.26.26.1 yamt sc->sc_tag = pa->pa_memt;
171 1.26.26.1 yamt error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, 0x80, 0, &bsh);
172 1.26.26.1 yamt if (error)
173 1.26.26.1 yamt panic(": failed to map mac-io %#x", ca.ca_baseaddr);
174 1.26.26.1 yamt 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.26.26.1 yamt aprint_normal("%s: enabling KeyLargo internal modem\n",
181 1.26.26.1 yamt self->dv_xname);
182 1.26.26.1 yamt bus_space_write_4(ca.ca_tag, bsh, 0x40,
183 1.26.26.1 yamt 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.26.26.1 yamt /* set reset */
189 1.26.26.1 yamt bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04);
190 1.26.26.1 yamt /* power modem on */
191 1.26.26.1 yamt bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04);
192 1.26.26.1 yamt /* unset reset */
193 1.26.26.1 yamt 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.26.26.1 yamt bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
207 1.21 briggs }
208 1.26.26.1 yamt
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.26.26.1 yamt #ifdef OBIO_SPEED_CONTROL
217 1.26.26.1 yamt if (strcmp(name, "gpio") == 0) {
218 1.26.26.1 yamt
219 1.26.26.1 yamt obio_setup_gpios(sc, child);
220 1.26.26.1 yamt continue;
221 1.26.26.1 yamt }
222 1.26.26.1 yamt #endif
223 1.26.26.1 yamt
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.26.26.1 yamt 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.26.26.1 yamt "escc",
256 1.26.26.1 yamt "battery",
257 1.26.26.1 yamt "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.26.26.1 yamt
284 1.26.26.1 yamt #ifdef OBIO_SPEED_CONTROL
285 1.26.26.1 yamt
286 1.26.26.1 yamt static void
287 1.26.26.1 yamt obio_setup_gpios(struct obio_softc *sc, int node)
288 1.26.26.1 yamt {
289 1.26.26.1 yamt uint32_t reg[6];
290 1.26.26.1 yamt struct sysctlnode *sysctl_node = NULL;
291 1.26.26.1 yamt char name[32];
292 1.26.26.1 yamt int gpio_base, child;
293 1.26.26.1 yamt
294 1.26.26.1 yamt if (of_compatible(sc->sc_node, keylargo) == -1)
295 1.26.26.1 yamt return;
296 1.26.26.1 yamt
297 1.26.26.1 yamt if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
298 1.26.26.1 yamt return;
299 1.26.26.1 yamt
300 1.26.26.1 yamt gpio_base = reg[0];
301 1.26.26.1 yamt DPRINTF("gpio_base: %02x\n", gpio_base);
302 1.26.26.1 yamt
303 1.26.26.1 yamt /* now look for voltage and bus speed gpios */
304 1.26.26.1 yamt for (child = OF_child(node); child; child = OF_peer(child)) {
305 1.26.26.1 yamt
306 1.26.26.1 yamt if (OF_getprop(child, "name", name, sizeof(name)) < 1)
307 1.26.26.1 yamt continue;
308 1.26.26.1 yamt
309 1.26.26.1 yamt if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
310 1.26.26.1 yamt continue;
311 1.26.26.1 yamt
312 1.26.26.1 yamt if (strcmp(name, "frequency-gpio") == 0) {
313 1.26.26.1 yamt DPRINTF("found frequency_gpio at %02x\n", reg[0]);
314 1.26.26.1 yamt sc->sc_busspeed = gpio_base + reg[0];
315 1.26.26.1 yamt }
316 1.26.26.1 yamt if (strcmp(name, "voltage-gpio") == 0) {
317 1.26.26.1 yamt DPRINTF("found voltage_gpio at %02x\n", reg[0]);
318 1.26.26.1 yamt sc->sc_voltage = gpio_base + reg[0];
319 1.26.26.1 yamt }
320 1.26.26.1 yamt }
321 1.26.26.1 yamt
322 1.26.26.1 yamt if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
323 1.26.26.1 yamt return;
324 1.26.26.1 yamt
325 1.26.26.1 yamt printf("%s: enabling Intrepid CPU speed control\n",
326 1.26.26.1 yamt sc->sc_dev.dv_xname);
327 1.26.26.1 yamt
328 1.26.26.1 yamt sysctl_createv(NULL, 0, NULL,
329 1.26.26.1 yamt (const struct sysctlnode **)&sysctl_node,
330 1.26.26.1 yamt CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
331 1.26.26.1 yamt CTLTYPE_INT, "cpu_speed", "CPU speed", sysctl_cpuspeed_temp,
332 1.26.26.1 yamt (unsigned long)sc, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
333 1.26.26.1 yamt if (sysctl_node != NULL) {
334 1.26.26.1 yamt sysctl_node->sysctl_data = (void *)sc;
335 1.26.26.1 yamt }
336 1.26.26.1 yamt }
337 1.26.26.1 yamt
338 1.26.26.1 yamt static void
339 1.26.26.1 yamt obio_set_cpu_speed(struct obio_softc *sc, int fast)
340 1.26.26.1 yamt {
341 1.26.26.1 yamt
342 1.26.26.1 yamt if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
343 1.26.26.1 yamt return;
344 1.26.26.1 yamt
345 1.26.26.1 yamt if (fast) {
346 1.26.26.1 yamt bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_voltage, 5);
347 1.26.26.1 yamt bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed, 5);
348 1.26.26.1 yamt } else {
349 1.26.26.1 yamt bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed, 4);
350 1.26.26.1 yamt bus_space_write_1(sc->sc_tag, sc->sc_bh, sc->sc_voltage, 4);
351 1.26.26.1 yamt }
352 1.26.26.1 yamt }
353 1.26.26.1 yamt
354 1.26.26.1 yamt static int
355 1.26.26.1 yamt obio_get_cpu_speed(struct obio_softc *sc)
356 1.26.26.1 yamt {
357 1.26.26.1 yamt
358 1.26.26.1 yamt if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0))
359 1.26.26.1 yamt return 0;
360 1.26.26.1 yamt
361 1.26.26.1 yamt if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed) & 1)
362 1.26.26.1 yamt return 1;
363 1.26.26.1 yamt
364 1.26.26.1 yamt return 0;
365 1.26.26.1 yamt }
366 1.26.26.1 yamt
367 1.26.26.1 yamt static int
368 1.26.26.1 yamt sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
369 1.26.26.1 yamt {
370 1.26.26.1 yamt struct sysctlnode node = *rnode;
371 1.26.26.1 yamt struct obio_softc *sc = node.sysctl_data;
372 1.26.26.1 yamt const int *np = newp;
373 1.26.26.1 yamt int speed, nd = 0;
374 1.26.26.1 yamt
375 1.26.26.1 yamt speed = obio_get_cpu_speed(sc);
376 1.26.26.1 yamt node.sysctl_idata = speed;
377 1.26.26.1 yamt if (np) {
378 1.26.26.1 yamt /* we're asked to write */
379 1.26.26.1 yamt nd = *np;
380 1.26.26.1 yamt node.sysctl_data = &speed;
381 1.26.26.1 yamt if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
382 1.26.26.1 yamt int new_reg;
383 1.26.26.1 yamt
384 1.26.26.1 yamt new_reg = (max(0, min(1, node.sysctl_idata)));
385 1.26.26.1 yamt obio_set_cpu_speed(sc, new_reg);
386 1.26.26.1 yamt return 0;
387 1.26.26.1 yamt }
388 1.26.26.1 yamt return EINVAL;
389 1.26.26.1 yamt } else {
390 1.26.26.1 yamt node.sysctl_size = 4;
391 1.26.26.1 yamt return(sysctl_lookup(SYSCTLFN_CALL(&node)));
392 1.26.26.1 yamt }
393 1.26.26.1 yamt }
394 1.26.26.1 yamt
395 1.26.26.1 yamt #endif /* OBIO_SPEEDCONTROL */
396 1.26.26.1 yamt
397