1 /* $NetBSD: obio.c,v 1.28 2008/09/26 03:19:05 macallan Exp $ */
4 * Copyright (C) 1998 Internet Research Institute, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.28 2008/09/26 03:19:05 macallan Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/sysctl.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
46 #include <dev/ofw/openfirm.h>
48 #include <machine/autoconf.h>
50 #include <macppc/dev/obiovar.h>
55 # define DPRINTF printf
57 # define DPRINTF while (0) printf
60 static void obio_attach(struct device
*, struct device
*, void *);
61 static int obio_match(struct device
*, struct cfdata
*, void *);
62 static int obio_print(void *, const char *);
66 bus_space_tag_t sc_tag
;
67 bus_space_handle_t sc_bh
;
69 #ifdef OBIO_SPEED_CONTROL
75 static struct obio_softc
*obio0
= NULL
;
77 #ifdef OBIO_SPEED_CONTROL
78 static void obio_setup_gpios(struct obio_softc
*, int);
79 static void obio_set_cpu_speed(struct obio_softc
*, int);
80 static int obio_get_cpu_speed(struct obio_softc
*);
81 static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS
);
83 static const char *keylargo
[] = {"Keylargo",
89 CFATTACH_DECL(obio
, sizeof(struct obio_softc
),
90 obio_match
, obio_attach
, NULL
, NULL
);
93 obio_match(struct device
*parent
, struct cfdata
*cf
, void *aux
)
95 struct pci_attach_args
*pa
= aux
;
97 if (PCI_VENDOR(pa
->pa_id
) == PCI_VENDOR_APPLE
)
98 switch (PCI_PRODUCT(pa
->pa_id
)) {
99 case PCI_PRODUCT_APPLE_GC
:
100 case PCI_PRODUCT_APPLE_OHARE
:
101 case PCI_PRODUCT_APPLE_HEATHROW
:
102 case PCI_PRODUCT_APPLE_PADDINGTON
:
103 case PCI_PRODUCT_APPLE_KEYLARGO
:
104 case PCI_PRODUCT_APPLE_PANGEA_MACIO
:
105 case PCI_PRODUCT_APPLE_INTREPID
:
106 case PCI_PRODUCT_APPLE_K2
:
114 * Attach all the sub-devices we can find
117 obio_attach(struct device
*parent
, struct device
*self
, void *aux
)
119 struct obio_softc
*sc
= (struct obio_softc
*)self
;
120 struct pci_attach_args
*pa
= aux
;
122 bus_space_handle_t bsh
;
123 int node
, child
, namelen
, error
;
125 int intr
[6], parent_intr
= 0, parent_nintr
= 0;
129 #ifdef OBIO_SPEED_CONTROL
131 sc
->sc_busspeed
= -1;
134 switch (PCI_PRODUCT(pa
->pa_id
)) {
136 case PCI_PRODUCT_APPLE_GC
:
137 case PCI_PRODUCT_APPLE_OHARE
:
138 case PCI_PRODUCT_APPLE_HEATHROW
:
139 case PCI_PRODUCT_APPLE_PADDINGTON
:
140 case PCI_PRODUCT_APPLE_KEYLARGO
:
141 case PCI_PRODUCT_APPLE_PANGEA_MACIO
:
142 case PCI_PRODUCT_APPLE_INTREPID
:
143 node
= pcidev_to_ofdev(pa
->pa_pc
, pa
->pa_tag
);
145 node
= OF_finddevice("mac-io");
147 node
= OF_finddevice("/pci/mac-io");
149 case PCI_PRODUCT_APPLE_K2
:
150 node
= OF_finddevice("mac-io");
158 panic("macio not found or unknown");
162 #if defined (PMAC_G5)
163 if (OF_getprop(node
, "assigned-addresses", reg
, sizeof(reg
)) < 20)
168 if (OF_getprop(node
, "assigned-addresses", reg
, sizeof(reg
)) < 12)
174 * This relies on the primary obio always attaching first which is
175 * true on the PowerBook 3400c and similar machines but may or may
176 * not work on others. We can't rely on the node name since Apple
177 * didn't follow anything remotely resembling a consistent naming
183 ca
.ca_baseaddr
= reg
[2];
184 ca
.ca_tag
= pa
->pa_memt
;
185 sc
->sc_tag
= pa
->pa_memt
;
186 error
= bus_space_map (pa
->pa_memt
, ca
.ca_baseaddr
, 0x80, 0, &bsh
);
188 panic(": failed to map mac-io %#x", ca
.ca_baseaddr
);
191 printf(": addr 0x%x\n", ca
.ca_baseaddr
);
193 /* Enable internal modem (KeyLargo) */
194 if (PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_APPLE_KEYLARGO
) {
195 aprint_normal("%s: enabling KeyLargo internal modem\n",
197 bus_space_write_4(ca
.ca_tag
, bsh
, 0x40,
198 bus_space_read_4(ca
.ca_tag
, bsh
, 0x40) & ~(1<<25));
201 /* Enable internal modem (Pangea) */
202 if (PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_APPLE_PANGEA_MACIO
) {
204 bus_space_write_1(ca
.ca_tag
, bsh
, 0x006a + 0x03, 0x04);
206 bus_space_write_1(ca
.ca_tag
, bsh
, 0x006a + 0x02, 0x04);
208 bus_space_write_1(ca
.ca_tag
, bsh
, 0x006a + 0x03, 0x05);
211 /* Gatwick and Paddington use same product ID */
212 namelen
= OF_getprop(node
, "compatible", compat
, sizeof(compat
));
214 if (strcmp(compat
, "gatwick") == 0) {
215 parent_nintr
= OF_getprop(node
, "AAPL,interrupts", intr
,
217 parent_intr
= intr
[0];
219 /* Enable CD and microphone sound input. */
220 if (PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_APPLE_PADDINGTON
)
221 bus_space_write_1(ca
.ca_tag
, bsh
, 0x37, 0x03);
224 for (child
= OF_child(node
); child
; child
= OF_peer(child
)) {
225 namelen
= OF_getprop(child
, "name", name
, sizeof(name
));
228 if (namelen
>= sizeof(name
))
231 #ifdef OBIO_SPEED_CONTROL
232 if (strcmp(name
, "gpio") == 0) {
234 obio_setup_gpios(sc
, child
);
242 ca
.ca_tag
= pa
->pa_memt
;
244 ca
.ca_nreg
= OF_getprop(child
, "reg", reg
, sizeof(reg
));
246 if (strcmp(compat
, "gatwick") != 0) {
247 ca
.ca_nintr
= OF_getprop(child
, "AAPL,interrupts", intr
,
249 if (ca
.ca_nintr
== -1)
250 ca
.ca_nintr
= OF_getprop(child
, "interrupts", intr
,
253 intr
[0] = parent_intr
;
254 ca
.ca_nintr
= parent_nintr
;
259 config_found(self
, &ca
, obio_print
);
263 static const char * const skiplist
[] = {
264 "interrupt-controller",
276 #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
279 obio_print(void *aux
, const char *obio
)
281 struct confargs
*ca
= aux
;
284 for (i
= 0; i
< N_LIST
; i
++)
285 if (strcmp(ca
->ca_name
, skiplist
[i
]) == 0)
289 aprint_normal("%s at %s", ca
->ca_name
, obio
);
292 aprint_normal(" offset 0x%x", ca
->ca_reg
[0]);
297 void obio_write_4(int offset
, uint32_t value
)
301 bus_space_write_4(obio0
->sc_tag
, obio0
->sc_bh
, offset
, value
);
304 void obio_write_1(int offset
, uint8_t value
)
308 bus_space_write_1(obio0
->sc_tag
, obio0
->sc_bh
, offset
, value
);
311 uint32_t obio_read_4(int offset
)
315 return bus_space_read_4(obio0
->sc_tag
, obio0
->sc_bh
, offset
);
318 uint8_t obio_read_1(int offset
)
322 return bus_space_read_1(obio0
->sc_tag
, obio0
->sc_bh
, offset
);
325 #ifdef OBIO_SPEED_CONTROL
328 obio_setup_gpios(struct obio_softc
*sc
, int node
)
331 struct sysctlnode
*sysctl_node
= NULL
;
333 int gpio_base
, child
;
335 if (of_compatible(sc
->sc_node
, keylargo
) == -1)
338 if (OF_getprop(node
, "reg", reg
, sizeof(reg
)) < 4)
342 DPRINTF("gpio_base: %02x\n", gpio_base
);
344 /* now look for voltage and bus speed gpios */
345 for (child
= OF_child(node
); child
; child
= OF_peer(child
)) {
347 if (OF_getprop(child
, "name", name
, sizeof(name
)) < 1)
350 if (OF_getprop(child
, "reg", reg
, sizeof(reg
)) < 4)
353 if (strcmp(name
, "frequency-gpio") == 0) {
354 DPRINTF("found frequency_gpio at %02x\n", reg
[0]);
355 sc
->sc_busspeed
= gpio_base
+ reg
[0];
357 if (strcmp(name
, "voltage-gpio") == 0) {
358 DPRINTF("found voltage_gpio at %02x\n", reg
[0]);
359 sc
->sc_voltage
= gpio_base
+ reg
[0];
363 if ((sc
->sc_voltage
< 0) || (sc
->sc_busspeed
< 0))
366 printf("%s: enabling Intrepid CPU speed control\n",
367 sc
->sc_dev
.dv_xname
);
369 sysctl_createv(NULL
, 0, NULL
,
370 (const struct sysctlnode
**)&sysctl_node
,
371 CTLFLAG_READWRITE
| CTLFLAG_OWNDESC
| CTLFLAG_IMMEDIATE
,
372 CTLTYPE_INT
, "cpu_speed", "CPU speed", sysctl_cpuspeed_temp
,
373 (unsigned long)sc
, NULL
, 0, CTL_MACHDEP
, CTL_CREATE
, CTL_EOL
);
374 if (sysctl_node
!= NULL
) {
375 sysctl_node
->sysctl_data
= (void *)sc
;
380 obio_set_cpu_speed(struct obio_softc
*sc
, int fast
)
383 if ((sc
->sc_voltage
< 0) || (sc
->sc_busspeed
< 0))
387 bus_space_write_1(sc
->sc_tag
, sc
->sc_bh
, sc
->sc_voltage
, 5);
388 bus_space_write_1(sc
->sc_tag
, sc
->sc_bh
, sc
->sc_busspeed
, 5);
390 bus_space_write_1(sc
->sc_tag
, sc
->sc_bh
, sc
->sc_busspeed
, 4);
391 bus_space_write_1(sc
->sc_tag
, sc
->sc_bh
, sc
->sc_voltage
, 4);
396 obio_get_cpu_speed(struct obio_softc
*sc
)
399 if ((sc
->sc_voltage
< 0) || (sc
->sc_busspeed
< 0))
402 if (bus_space_read_1(sc
->sc_tag
, sc
->sc_bh
, sc
->sc_busspeed
) & 1)
409 sysctl_cpuspeed_temp(SYSCTLFN_ARGS
)
411 struct sysctlnode node
= *rnode
;
412 struct obio_softc
*sc
= node
.sysctl_data
;
413 const int *np
= newp
;
416 speed
= obio_get_cpu_speed(sc
);
417 node
.sysctl_idata
= speed
;
419 /* we're asked to write */
421 node
.sysctl_data
= &speed
;
422 if (sysctl_lookup(SYSCTLFN_CALL(&node
)) == 0) {
425 new_reg
= (max(0, min(1, node
.sysctl_idata
)));
426 obio_set_cpu_speed(sc
, new_reg
);
431 node
.sysctl_size
= 4;
432 return(sysctl_lookup(SYSCTLFN_CALL(&node
)));
436 #endif /* OBIO_SPEEDCONTROL */