obio.c revision 1.25 1 1.25 macallan /* $NetBSD: obio.c,v 1.25 2007/01/18 00:17:22 macallan 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.25 macallan __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.25 2007/01/18 00:17:22 macallan 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.1 tsubai
42 1.1 tsubai #include <dev/pci/pcivar.h>
43 1.1 tsubai #include <dev/pci/pcidevs.h>
44 1.1 tsubai
45 1.1 tsubai #include <dev/ofw/openfirm.h>
46 1.1 tsubai
47 1.1 tsubai #include <machine/autoconf.h>
48 1.1 tsubai
49 1.25 macallan #include <sys/sysctl.h>
50 1.25 macallan
51 1.1 tsubai static void obio_attach __P((struct device *, struct device *, void *));
52 1.1 tsubai static int obio_match __P((struct device *, struct cfdata *, void *));
53 1.1 tsubai static int obio_print __P((void *, const char *));
54 1.1 tsubai
55 1.1 tsubai struct obio_softc {
56 1.1 tsubai struct device sc_dev;
57 1.1 tsubai int sc_node;
58 1.1 tsubai };
59 1.1 tsubai
60 1.25 macallan static void obio_setup_ohare2(struct obio_softc *, struct confargs *);
61 1.25 macallan static int obio_get_cpu_speed(paddr_t);
62 1.25 macallan static void obio_set_cpu_speed(paddr_t, int);
63 1.25 macallan static void setup_sysctl(paddr_t);
64 1.1 tsubai
65 1.16 thorpej CFATTACH_DECL(obio, sizeof(struct obio_softc),
66 1.16 thorpej obio_match, obio_attach, NULL, NULL);
67 1.1 tsubai
68 1.1 tsubai int
69 1.1 tsubai obio_match(parent, cf, aux)
70 1.1 tsubai struct device *parent;
71 1.1 tsubai struct cfdata *cf;
72 1.1 tsubai void *aux;
73 1.1 tsubai {
74 1.1 tsubai struct pci_attach_args *pa = aux;
75 1.1 tsubai
76 1.2 tsubai if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
77 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
78 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
79 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
80 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
81 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
82 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
83 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
84 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
85 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
86 1.2 tsubai return 1;
87 1.2 tsubai }
88 1.1 tsubai
89 1.1 tsubai return 0;
90 1.1 tsubai }
91 1.1 tsubai
92 1.1 tsubai /*
93 1.1 tsubai * Attach all the sub-devices we can find
94 1.1 tsubai */
95 1.1 tsubai void
96 1.1 tsubai obio_attach(parent, self, aux)
97 1.1 tsubai struct device *parent, *self;
98 1.1 tsubai void *aux;
99 1.1 tsubai {
100 1.1 tsubai struct obio_softc *sc = (struct obio_softc *)self;
101 1.2 tsubai struct pci_attach_args *pa = aux;
102 1.1 tsubai struct confargs ca;
103 1.1 tsubai int node, child, namelen;
104 1.1 tsubai u_int reg[20];
105 1.21 briggs int intr[6], parent_intr = 0, parent_nintr = 0;
106 1.1 tsubai char name[32];
107 1.21 briggs char compat[32];
108 1.1 tsubai
109 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
110 1.2 tsubai
111 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
112 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
113 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
114 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
115 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
116 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
117 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
118 1.21 briggs node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
119 1.6 tsubai if (node == -1)
120 1.21 briggs node = OF_finddevice("mac-io");
121 1.21 briggs if (node == -1)
122 1.21 briggs node = OF_finddevice("/pci/mac-io");
123 1.2 tsubai break;
124 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
125 1.24 sanjayl node = OF_finddevice("mac-io");
126 1.24 sanjayl if (node == -1)
127 1.24 sanjayl panic("macio not found");
128 1.24 sanjayl break;
129 1.2 tsubai
130 1.2 tsubai default:
131 1.2 tsubai printf("obio_attach: unknown obio controller\n");
132 1.2 tsubai return;
133 1.2 tsubai }
134 1.2 tsubai
135 1.1 tsubai sc->sc_node = node;
136 1.1 tsubai
137 1.24 sanjayl #if defined (PMAC_G5)
138 1.24 sanjayl if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
139 1.24 sanjayl {
140 1.24 sanjayl return;
141 1.24 sanjayl }
142 1.24 sanjayl #else
143 1.1 tsubai if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
144 1.1 tsubai return;
145 1.24 sanjayl #endif /* PMAC_G5 */
146 1.24 sanjayl
147 1.1 tsubai ca.ca_baseaddr = reg[2];
148 1.1 tsubai
149 1.1 tsubai printf(": addr 0x%x\n", ca.ca_baseaddr);
150 1.13 tsubai
151 1.20 briggs /* Enable internal modem (KeyLargo) */
152 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
153 1.20 briggs printf("enabling KeyLargo internal modem\n");
154 1.20 briggs out32rb(ca.ca_baseaddr + 0x40,
155 1.20 briggs in32rb(ca.ca_baseaddr + 0x40) & ~((u_int32_t)1<<25));
156 1.20 briggs }
157 1.25 macallan
158 1.25 macallan if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID) {
159 1.25 macallan paddr_t addr;
160 1.25 macallan
161 1.25 macallan printf("enabling Intrepid CPU speed control\nCPU speed is ");
162 1.25 macallan addr = ca.ca_baseaddr + 0x6a;
163 1.25 macallan if (obio_get_cpu_speed(addr)) {
164 1.25 macallan printf("high\n");
165 1.25 macallan } else {
166 1.25 macallan printf("low\n");
167 1.25 macallan }
168 1.25 macallan setup_sysctl(addr);
169 1.25 macallan }
170 1.25 macallan
171 1.20 briggs /* Enable internal modem (Pangea) */
172 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
173 1.20 briggs out8(ca.ca_baseaddr + 0x006a + 0x03, 0x04); /* set reset */
174 1.20 briggs out8(ca.ca_baseaddr + 0x006a + 0x02, 0x04); /* power modem on */
175 1.20 briggs out8(ca.ca_baseaddr + 0x006a + 0x03, 0x05); /* unset reset */
176 1.20 briggs }
177 1.20 briggs
178 1.21 briggs /* Gatwick and Paddington use same product ID */
179 1.21 briggs namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
180 1.1 tsubai
181 1.21 briggs if (strcmp(compat, "gatwick") == 0) {
182 1.21 briggs parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
183 1.21 briggs sizeof(intr));
184 1.21 briggs parent_intr = intr[0];
185 1.21 briggs } else {
186 1.21 briggs /* Enable CD and microphone sound input. */
187 1.21 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
188 1.21 briggs out8(ca.ca_baseaddr + 0x37, 0x03);
189 1.21 briggs }
190 1.25 macallan
191 1.25 macallan if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_OHARE) {
192 1.25 macallan uint32_t freg;
193 1.25 macallan
194 1.25 macallan freg = in32rb(ca.ca_baseaddr + 0x38);
195 1.25 macallan printf("%s: FCR %08x\n", sc->sc_dev.dv_xname, freg);
196 1.25 macallan
197 1.25 macallan if (ca.ca_baseaddr != 0xf3000000) {
198 1.25 macallan obio_setup_ohare2(sc, &ca);
199 1.25 macallan return;
200 1.25 macallan }
201 1.25 macallan printf("enabling 2nd IDE channel on ohare\n");
202 1.25 macallan freg |= 8;
203 1.25 macallan out32rb(ca.ca_baseaddr + 0x38, freg);
204 1.25 macallan }
205 1.25 macallan
206 1.1 tsubai for (child = OF_child(node); child; child = OF_peer(child)) {
207 1.1 tsubai namelen = OF_getprop(child, "name", name, sizeof(name));
208 1.1 tsubai if (namelen < 0)
209 1.1 tsubai continue;
210 1.1 tsubai if (namelen >= sizeof(name))
211 1.1 tsubai continue;
212 1.1 tsubai
213 1.1 tsubai name[namelen] = 0;
214 1.1 tsubai ca.ca_name = name;
215 1.1 tsubai ca.ca_node = child;
216 1.25 macallan ca.ca_tag = pa->pa_memt;
217 1.25 macallan
218 1.21 briggs ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
219 1.25 macallan if (strcmp(name, "backlight") == 0) {
220 1.25 macallan paddr_t addr = (ca.ca_baseaddr + reg[0]);
221 1.25 macallan printf("backlight: %08x %08x\n",(uint32_t)addr, in32rb(addr));
222 1.25 macallan }
223 1.21 briggs if (strcmp(compat, "gatwick") != 0) {
224 1.21 briggs ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
225 1.5 tsubai sizeof(intr));
226 1.21 briggs if (ca.ca_nintr == -1)
227 1.21 briggs ca.ca_nintr = OF_getprop(child, "interrupts", intr,
228 1.21 briggs sizeof(intr));
229 1.21 briggs } else {
230 1.21 briggs intr[0] = parent_intr;
231 1.21 briggs ca.ca_nintr = parent_nintr;
232 1.21 briggs }
233 1.1 tsubai ca.ca_reg = reg;
234 1.1 tsubai ca.ca_intr = intr;
235 1.1 tsubai
236 1.1 tsubai config_found(self, &ca, obio_print);
237 1.1 tsubai }
238 1.1 tsubai }
239 1.1 tsubai
240 1.22 nathanw static const char *skiplist[] = {
241 1.8 tsubai "interrupt-controller",
242 1.8 tsubai "gpio",
243 1.8 tsubai "escc-legacy",
244 1.8 tsubai "timer",
245 1.9 tsubai "i2c",
246 1.24 sanjayl "power-mgt",
247 1.24 sanjayl "escc"
248 1.24 sanjayl
249 1.8 tsubai };
250 1.8 tsubai
251 1.8 tsubai #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
252 1.8 tsubai
253 1.1 tsubai int
254 1.1 tsubai obio_print(aux, obio)
255 1.1 tsubai void *aux;
256 1.1 tsubai const char *obio;
257 1.1 tsubai {
258 1.1 tsubai struct confargs *ca = aux;
259 1.8 tsubai int i;
260 1.8 tsubai
261 1.8 tsubai for (i = 0; i < N_LIST; i++)
262 1.8 tsubai if (strcmp(ca->ca_name, skiplist[i]) == 0)
263 1.8 tsubai return QUIET;
264 1.1 tsubai
265 1.1 tsubai if (obio)
266 1.17 thorpej aprint_normal("%s at %s", ca->ca_name, obio);
267 1.1 tsubai
268 1.1 tsubai if (ca->ca_nreg > 0)
269 1.17 thorpej aprint_normal(" offset 0x%x", ca->ca_reg[0]);
270 1.1 tsubai
271 1.1 tsubai return UNCONF;
272 1.1 tsubai }
273 1.25 macallan
274 1.25 macallan static void
275 1.25 macallan obio_setup_ohare2(struct obio_softc *sc, struct confargs *ca)
276 1.25 macallan {
277 1.25 macallan printf("ohare2: %08x\n", ca->ca_baseaddr);
278 1.25 macallan }
279 1.25 macallan
280 1.25 macallan static void
281 1.25 macallan obio_set_cpu_speed(paddr_t addr, int fast)
282 1.25 macallan {
283 1.25 macallan
284 1.25 macallan if(addr != 0) {
285 1.25 macallan if (fast) {
286 1.25 macallan out8rb(addr + 1, 5); /* bump Vcore */
287 1.25 macallan out8rb(addr, 5); /* bump CPU speed */
288 1.25 macallan } else {
289 1.25 macallan out8rb(addr, 4); /* lower CPU speed */
290 1.25 macallan out8rb(addr + 1, 4); /* lower Vcore */
291 1.25 macallan }
292 1.25 macallan }
293 1.25 macallan }
294 1.25 macallan
295 1.25 macallan static int
296 1.25 macallan obio_get_cpu_speed(paddr_t addr)
297 1.25 macallan {
298 1.25 macallan
299 1.25 macallan if(addr != 0) {
300 1.25 macallan if(in8rb(addr) & 1)
301 1.25 macallan return 1;
302 1.25 macallan }
303 1.25 macallan return 0;
304 1.25 macallan }
305 1.25 macallan
306 1.25 macallan SYSCTL_SETUP(sysctl_cpuspeed_setup, "sysctl cpu speed setup")
307 1.25 macallan {
308 1.25 macallan
309 1.25 macallan sysctl_createv(NULL, 0, NULL, NULL,
310 1.25 macallan CTLFLAG_PERMANENT,
311 1.25 macallan CTLTYPE_NODE, "machdep", NULL,
312 1.25 macallan NULL, 0, NULL, 0,
313 1.25 macallan CTL_MACHDEP, CTL_EOL);
314 1.25 macallan }
315 1.25 macallan
316 1.25 macallan static int
317 1.25 macallan sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
318 1.25 macallan {
319 1.25 macallan struct sysctlnode node = *rnode;
320 1.25 macallan paddr_t addr = (paddr_t)node.sysctl_data;
321 1.25 macallan const int *np = newp;
322 1.25 macallan int speed, nd = 0;
323 1.25 macallan
324 1.25 macallan speed = obio_get_cpu_speed(addr);
325 1.25 macallan node.sysctl_idata = speed;
326 1.25 macallan if (np) {
327 1.25 macallan /* we're asked to write */
328 1.25 macallan nd = *np;
329 1.25 macallan node.sysctl_data = &speed;
330 1.25 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
331 1.25 macallan int new_reg;
332 1.25 macallan
333 1.25 macallan new_reg = (max(0, min(1, node.sysctl_idata)));
334 1.25 macallan obio_set_cpu_speed(addr, new_reg);
335 1.25 macallan return 0;
336 1.25 macallan }
337 1.25 macallan return EINVAL;
338 1.25 macallan } else {
339 1.25 macallan node.sysctl_size = 4;
340 1.25 macallan return(sysctl_lookup(SYSCTLFN_CALL(&node)));
341 1.25 macallan }
342 1.25 macallan }
343 1.25 macallan
344 1.25 macallan static void
345 1.25 macallan setup_sysctl(paddr_t addr)
346 1.25 macallan {
347 1.25 macallan struct sysctlnode *node=NULL;
348 1.25 macallan int ret;
349 1.25 macallan
350 1.25 macallan ret = sysctl_createv(NULL, 0, NULL,
351 1.25 macallan (const struct sysctlnode **)&node,
352 1.25 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
353 1.25 macallan CTLTYPE_INT, "cpu_speed", "CPU speed", sysctl_cpuspeed_temp,
354 1.25 macallan addr , NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
355 1.25 macallan if (node != NULL) {
356 1.25 macallan node->sysctl_data = (void *)addr;
357 1.25 macallan }
358 1.25 macallan }
359