obio.c revision 1.49 1 1.49 thorpej /* $NetBSD: obio.c,v 1.49 2021/04/24 23:36:41 thorpej 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.49 thorpej __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.49 2021/04/24 23:36:41 thorpej 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.30 phx #include <powerpc/cpu.h>
53 1.36 macallan #include <sys/cpufreq.h>
54 1.30 phx
55 1.27 garbled #include "opt_obio.h"
56 1.27 garbled
57 1.27 garbled #ifdef OBIO_DEBUG
58 1.27 garbled # define DPRINTF printf
59 1.27 garbled #else
60 1.27 garbled # define DPRINTF while (0) printf
61 1.27 garbled #endif
62 1.27 garbled
63 1.33 matt static void obio_attach(device_t, device_t, void *);
64 1.33 matt static int obio_match(device_t, cfdata_t, void *);
65 1.27 garbled static int obio_print(void *, const char *);
66 1.1 tsubai
67 1.1 tsubai struct obio_softc {
68 1.34 macallan device_t sc_dev;
69 1.27 garbled bus_space_tag_t sc_tag;
70 1.27 garbled bus_space_handle_t sc_bh;
71 1.1 tsubai int sc_node;
72 1.27 garbled #ifdef OBIO_SPEED_CONTROL
73 1.27 garbled int sc_voltage;
74 1.27 garbled int sc_busspeed;
75 1.32 macallan int sc_spd_hi, sc_spd_lo;
76 1.36 macallan struct cpufreq sc_cf;
77 1.27 garbled #endif
78 1.1 tsubai };
79 1.1 tsubai
80 1.28 macallan static struct obio_softc *obio0 = NULL;
81 1.28 macallan
82 1.27 garbled #ifdef OBIO_SPEED_CONTROL
83 1.27 garbled static void obio_setup_gpios(struct obio_softc *, int);
84 1.27 garbled static void obio_set_cpu_speed(struct obio_softc *, int);
85 1.27 garbled static int obio_get_cpu_speed(struct obio_softc *);
86 1.27 garbled static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
87 1.32 macallan static int sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
88 1.32 macallan static int sysctl_cpuspeed_available(SYSCTLFN_ARGS);
89 1.36 macallan static void obio_get_freq(void *, void *);
90 1.36 macallan static void obio_set_freq(void *, void *);
91 1.27 garbled static const char *keylargo[] = {"Keylargo",
92 1.27 garbled "AAPL,Keylargo",
93 1.27 garbled NULL};
94 1.27 garbled
95 1.27 garbled #endif
96 1.1 tsubai
97 1.34 macallan CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
98 1.16 thorpej obio_match, obio_attach, NULL, NULL);
99 1.1 tsubai
100 1.1 tsubai int
101 1.33 matt obio_match(device_t parent, cfdata_t cf, void *aux)
102 1.1 tsubai {
103 1.1 tsubai struct pci_attach_args *pa = aux;
104 1.1 tsubai
105 1.2 tsubai if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
106 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
107 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
108 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
109 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
110 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
111 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
112 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
113 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
114 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
115 1.39 macallan case PCI_PRODUCT_APPLE_SHASTA:
116 1.2 tsubai return 1;
117 1.2 tsubai }
118 1.1 tsubai
119 1.1 tsubai return 0;
120 1.1 tsubai }
121 1.1 tsubai
122 1.1 tsubai /*
123 1.1 tsubai * Attach all the sub-devices we can find
124 1.1 tsubai */
125 1.1 tsubai void
126 1.33 matt obio_attach(device_t parent, device_t self, void *aux)
127 1.1 tsubai {
128 1.33 matt struct obio_softc *sc = device_private(self);
129 1.2 tsubai struct pci_attach_args *pa = aux;
130 1.1 tsubai struct confargs ca;
131 1.27 garbled bus_space_handle_t bsh;
132 1.27 garbled int node, child, namelen, error;
133 1.1 tsubai u_int reg[20];
134 1.21 briggs int intr[6], parent_intr = 0, parent_nintr = 0;
135 1.40 macallan int map_size = 0x1000;
136 1.1 tsubai char name[32];
137 1.21 briggs char compat[32];
138 1.1 tsubai
139 1.34 macallan sc->sc_dev = self;
140 1.27 garbled #ifdef OBIO_SPEED_CONTROL
141 1.27 garbled sc->sc_voltage = -1;
142 1.27 garbled sc->sc_busspeed = -1;
143 1.32 macallan sc->sc_spd_lo = 600;
144 1.32 macallan sc->sc_spd_hi = 800;
145 1.27 garbled #endif
146 1.27 garbled
147 1.2 tsubai switch (PCI_PRODUCT(pa->pa_id)) {
148 1.2 tsubai
149 1.7 tsubai case PCI_PRODUCT_APPLE_GC:
150 1.7 tsubai case PCI_PRODUCT_APPLE_OHARE:
151 1.7 tsubai case PCI_PRODUCT_APPLE_HEATHROW:
152 1.7 tsubai case PCI_PRODUCT_APPLE_PADDINGTON:
153 1.7 tsubai case PCI_PRODUCT_APPLE_KEYLARGO:
154 1.14 tsubai case PCI_PRODUCT_APPLE_PANGEA_MACIO:
155 1.18 hamajima case PCI_PRODUCT_APPLE_INTREPID:
156 1.21 briggs node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
157 1.43 mrg if (node == -1)
158 1.21 briggs node = OF_finddevice("mac-io");
159 1.43 mrg if (node == -1)
160 1.43 mrg node = OF_finddevice("/pci/mac-io");
161 1.2 tsubai break;
162 1.24 sanjayl case PCI_PRODUCT_APPLE_K2:
163 1.39 macallan case PCI_PRODUCT_APPLE_SHASTA:
164 1.24 sanjayl node = OF_finddevice("mac-io");
165 1.45 macallan map_size = 0x10000;
166 1.24 sanjayl break;
167 1.2 tsubai
168 1.2 tsubai default:
169 1.27 garbled node = -1;
170 1.27 garbled break;
171 1.2 tsubai }
172 1.27 garbled if (node == -1)
173 1.27 garbled panic("macio not found or unknown");
174 1.2 tsubai
175 1.1 tsubai sc->sc_node = node;
176 1.1 tsubai
177 1.24 sanjayl #if defined (PMAC_G5)
178 1.24 sanjayl if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
179 1.24 sanjayl {
180 1.24 sanjayl return;
181 1.24 sanjayl }
182 1.24 sanjayl #else
183 1.1 tsubai if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
184 1.1 tsubai return;
185 1.24 sanjayl #endif /* PMAC_G5 */
186 1.24 sanjayl
187 1.28 macallan /*
188 1.28 macallan * XXX
189 1.28 macallan * This relies on the primary obio always attaching first which is
190 1.28 macallan * true on the PowerBook 3400c and similar machines but may or may
191 1.28 macallan * not work on others. We can't rely on the node name since Apple
192 1.28 macallan * didn't follow anything remotely resembling a consistent naming
193 1.28 macallan * scheme.
194 1.28 macallan */
195 1.28 macallan if (obio0 == NULL)
196 1.28 macallan obio0 = sc;
197 1.28 macallan
198 1.1 tsubai ca.ca_baseaddr = reg[2];
199 1.27 garbled ca.ca_tag = pa->pa_memt;
200 1.27 garbled sc->sc_tag = pa->pa_memt;
201 1.40 macallan error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, map_size, 0, &bsh);
202 1.27 garbled if (error)
203 1.27 garbled panic(": failed to map mac-io %#x", ca.ca_baseaddr);
204 1.27 garbled sc->sc_bh = bsh;
205 1.1 tsubai
206 1.1 tsubai printf(": addr 0x%x\n", ca.ca_baseaddr);
207 1.13 tsubai
208 1.20 briggs /* Enable internal modem (KeyLargo) */
209 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
210 1.27 garbled aprint_normal("%s: enabling KeyLargo internal modem\n",
211 1.38 chs device_xname(self));
212 1.27 garbled bus_space_write_4(ca.ca_tag, bsh, 0x40,
213 1.27 garbled bus_space_read_4(ca.ca_tag, bsh, 0x40) & ~(1<<25));
214 1.20 briggs }
215 1.26 macallan
216 1.20 briggs /* Enable internal modem (Pangea) */
217 1.20 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
218 1.27 garbled /* set reset */
219 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04);
220 1.27 garbled /* power modem on */
221 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04);
222 1.27 garbled /* unset reset */
223 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x05);
224 1.20 briggs }
225 1.20 briggs
226 1.21 briggs /* Gatwick and Paddington use same product ID */
227 1.21 briggs namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
228 1.1 tsubai
229 1.21 briggs if (strcmp(compat, "gatwick") == 0) {
230 1.21 briggs parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
231 1.21 briggs sizeof(intr));
232 1.21 briggs parent_intr = intr[0];
233 1.21 briggs } else {
234 1.21 briggs /* Enable CD and microphone sound input. */
235 1.21 briggs if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
236 1.27 garbled bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
237 1.21 briggs }
238 1.27 garbled
239 1.1 tsubai for (child = OF_child(node); child; child = OF_peer(child)) {
240 1.1 tsubai namelen = OF_getprop(child, "name", name, sizeof(name));
241 1.1 tsubai if (namelen < 0)
242 1.1 tsubai continue;
243 1.1 tsubai if (namelen >= sizeof(name))
244 1.1 tsubai continue;
245 1.1 tsubai
246 1.27 garbled #ifdef OBIO_SPEED_CONTROL
247 1.27 garbled if (strcmp(name, "gpio") == 0) {
248 1.27 garbled
249 1.27 garbled obio_setup_gpios(sc, child);
250 1.27 garbled continue;
251 1.27 garbled }
252 1.27 garbled #endif
253 1.27 garbled
254 1.1 tsubai name[namelen] = 0;
255 1.1 tsubai ca.ca_name = name;
256 1.1 tsubai ca.ca_node = child;
257 1.25 macallan ca.ca_tag = pa->pa_memt;
258 1.26 macallan
259 1.21 briggs ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
260 1.26 macallan
261 1.21 briggs if (strcmp(compat, "gatwick") != 0) {
262 1.21 briggs ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
263 1.5 tsubai sizeof(intr));
264 1.21 briggs if (ca.ca_nintr == -1)
265 1.21 briggs ca.ca_nintr = OF_getprop(child, "interrupts", intr,
266 1.21 briggs sizeof(intr));
267 1.21 briggs } else {
268 1.21 briggs intr[0] = parent_intr;
269 1.21 briggs ca.ca_nintr = parent_nintr;
270 1.21 briggs }
271 1.1 tsubai ca.ca_reg = reg;
272 1.1 tsubai ca.ca_intr = intr;
273 1.1 tsubai
274 1.49 thorpej config_found(self, &ca, obio_print,
275 1.49 thorpej CFARG_DEVHANDLE, devhandle_from_of(child),
276 1.49 thorpej CFARG_EOL);
277 1.1 tsubai }
278 1.1 tsubai }
279 1.1 tsubai
280 1.27 garbled static const char * const skiplist[] = {
281 1.8 tsubai "interrupt-controller",
282 1.8 tsubai "gpio",
283 1.8 tsubai "escc-legacy",
284 1.8 tsubai "timer",
285 1.9 tsubai "i2c",
286 1.24 sanjayl "power-mgt",
287 1.27 garbled "escc",
288 1.27 garbled "battery",
289 1.27 garbled "backlight"
290 1.24 sanjayl
291 1.8 tsubai };
292 1.8 tsubai
293 1.8 tsubai #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
294 1.8 tsubai
295 1.1 tsubai int
296 1.29 dsl obio_print(void *aux, const char *obio)
297 1.1 tsubai {
298 1.1 tsubai struct confargs *ca = aux;
299 1.8 tsubai int i;
300 1.8 tsubai
301 1.8 tsubai for (i = 0; i < N_LIST; i++)
302 1.8 tsubai if (strcmp(ca->ca_name, skiplist[i]) == 0)
303 1.8 tsubai return QUIET;
304 1.1 tsubai
305 1.1 tsubai if (obio)
306 1.17 thorpej aprint_normal("%s at %s", ca->ca_name, obio);
307 1.1 tsubai
308 1.1 tsubai if (ca->ca_nreg > 0)
309 1.17 thorpej aprint_normal(" offset 0x%x", ca->ca_reg[0]);
310 1.1 tsubai
311 1.1 tsubai return UNCONF;
312 1.1 tsubai }
313 1.27 garbled
314 1.28 macallan void obio_write_4(int offset, uint32_t value)
315 1.28 macallan {
316 1.28 macallan if (obio0 == NULL)
317 1.28 macallan return;
318 1.28 macallan bus_space_write_4(obio0->sc_tag, obio0->sc_bh, offset, value);
319 1.28 macallan }
320 1.28 macallan
321 1.28 macallan void obio_write_1(int offset, uint8_t value)
322 1.28 macallan {
323 1.28 macallan if (obio0 == NULL)
324 1.28 macallan return;
325 1.28 macallan bus_space_write_1(obio0->sc_tag, obio0->sc_bh, offset, value);
326 1.28 macallan }
327 1.28 macallan
328 1.28 macallan uint32_t obio_read_4(int offset)
329 1.28 macallan {
330 1.28 macallan if (obio0 == NULL)
331 1.28 macallan return 0xffffffff;
332 1.28 macallan return bus_space_read_4(obio0->sc_tag, obio0->sc_bh, offset);
333 1.28 macallan }
334 1.28 macallan
335 1.28 macallan uint8_t obio_read_1(int offset)
336 1.28 macallan {
337 1.28 macallan if (obio0 == NULL)
338 1.28 macallan return 0xff;
339 1.28 macallan return bus_space_read_1(obio0->sc_tag, obio0->sc_bh, offset);
340 1.28 macallan }
341 1.28 macallan
342 1.45 macallan int
343 1.45 macallan obio_space_map(bus_addr_t addr, bus_size_t size, bus_space_handle_t *bh)
344 1.45 macallan {
345 1.45 macallan if (obio0 == NULL)
346 1.45 macallan return 0xff;
347 1.45 macallan return bus_space_subregion(obio0->sc_tag, obio0->sc_bh,
348 1.45 macallan addr & 0xfffff, size, bh);
349 1.45 macallan }
350 1.45 macallan
351 1.27 garbled #ifdef OBIO_SPEED_CONTROL
352 1.27 garbled
353 1.27 garbled static void
354 1.36 macallan obio_setup_cpufreq(device_t dev)
355 1.36 macallan {
356 1.36 macallan struct obio_softc *sc = device_private(dev);
357 1.36 macallan int ret;
358 1.36 macallan
359 1.36 macallan ret = cpufreq_register(&sc->sc_cf);
360 1.36 macallan if (ret != 0)
361 1.36 macallan aprint_error_dev(sc->sc_dev, "cpufreq_register() failed, error %d\n", ret);
362 1.36 macallan }
363 1.36 macallan
364 1.36 macallan static void
365 1.27 garbled obio_setup_gpios(struct obio_softc *sc, int node)
366 1.27 garbled {
367 1.31 phx uint32_t gpio_base, reg[6];
368 1.35 macallan const struct sysctlnode *sysctl_node, *me, *freq;
369 1.36 macallan struct cpufreq *cf = &sc->sc_cf;
370 1.27 garbled char name[32];
371 1.32 macallan int child, use_dfs, cpunode, hiclock;
372 1.27 garbled
373 1.48 thorpej if (! of_compatible(sc->sc_node, keylargo))
374 1.27 garbled return;
375 1.27 garbled
376 1.27 garbled if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
377 1.27 garbled return;
378 1.27 garbled
379 1.27 garbled gpio_base = reg[0];
380 1.27 garbled DPRINTF("gpio_base: %02x\n", gpio_base);
381 1.27 garbled
382 1.27 garbled /* now look for voltage and bus speed gpios */
383 1.30 phx use_dfs = 0;
384 1.27 garbled for (child = OF_child(node); child; child = OF_peer(child)) {
385 1.27 garbled
386 1.27 garbled if (OF_getprop(child, "name", name, sizeof(name)) < 1)
387 1.27 garbled continue;
388 1.27 garbled
389 1.27 garbled if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
390 1.27 garbled continue;
391 1.27 garbled
392 1.31 phx /*
393 1.31 phx * These register offsets either have to be added to the obio
394 1.31 phx * base address or to the gpio base address. This differs
395 1.31 phx * even in the same OF-tree! So we guess the offset is
396 1.31 phx * based on obio when it is larger than the gpio_base.
397 1.31 phx */
398 1.31 phx if (reg[0] >= gpio_base)
399 1.31 phx reg[0] -= gpio_base;
400 1.31 phx
401 1.27 garbled if (strcmp(name, "frequency-gpio") == 0) {
402 1.27 garbled DPRINTF("found frequency_gpio at %02x\n", reg[0]);
403 1.27 garbled sc->sc_busspeed = gpio_base + reg[0];
404 1.27 garbled }
405 1.27 garbled if (strcmp(name, "voltage-gpio") == 0) {
406 1.27 garbled DPRINTF("found voltage_gpio at %02x\n", reg[0]);
407 1.27 garbled sc->sc_voltage = gpio_base + reg[0];
408 1.27 garbled }
409 1.30 phx if (strcmp(name, "cpu-vcore-select") == 0) {
410 1.30 phx DPRINTF("found cpu-vcore-select at %02x\n", reg[0]);
411 1.30 phx sc->sc_voltage = gpio_base + reg[0];
412 1.30 phx /* frequency gpio is not needed, we use cpu's DFS */
413 1.30 phx use_dfs = 1;
414 1.30 phx }
415 1.27 garbled }
416 1.27 garbled
417 1.30 phx if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0 && !use_dfs))
418 1.27 garbled return;
419 1.27 garbled
420 1.27 garbled printf("%s: enabling Intrepid CPU speed control\n",
421 1.34 macallan device_xname(sc->sc_dev));
422 1.27 garbled
423 1.32 macallan sc->sc_spd_lo = curcpu()->ci_khz / 1000;
424 1.32 macallan hiclock = 0;
425 1.32 macallan cpunode = OF_finddevice("/cpus/@0");
426 1.32 macallan OF_getprop(cpunode, "clock-frequency", &hiclock, 4);
427 1.36 macallan if (hiclock != 0)
428 1.36 macallan sc->sc_spd_hi = (hiclock + 500000) / 1000000;
429 1.36 macallan printf("hiclock: %d\n", sc->sc_spd_hi);
430 1.46 macallan if (use_dfs) sc->sc_spd_lo = sc->sc_spd_hi / 2;
431 1.46 macallan
432 1.30 phx sysctl_node = NULL;
433 1.32 macallan
434 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
435 1.35 macallan &me,
436 1.47 nia CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
437 1.32 macallan 0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
438 1.47 nia printf("couldn't create 'cpu' node\n");
439 1.32 macallan
440 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
441 1.35 macallan &freq,
442 1.32 macallan CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
443 1.32 macallan 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
444 1.32 macallan printf("couldn't create 'frequency' node\n");
445 1.32 macallan
446 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
447 1.35 macallan &sysctl_node,
448 1.35 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
449 1.32 macallan CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
450 1.37 dsl 0, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
451 1.32 macallan CTL_CREATE, CTL_EOL) == 0) {
452 1.32 macallan } else
453 1.32 macallan printf("couldn't create 'target' node\n");
454 1.32 macallan
455 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
456 1.35 macallan &sysctl_node,
457 1.35 macallan CTLFLAG_READWRITE,
458 1.32 macallan CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
459 1.37 dsl 1, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
460 1.32 macallan CTL_CREATE, CTL_EOL) == 0) {
461 1.32 macallan } else
462 1.32 macallan printf("couldn't create 'current' node\n");
463 1.32 macallan
464 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
465 1.35 macallan &sysctl_node,
466 1.32 macallan CTLFLAG_READWRITE,
467 1.32 macallan CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
468 1.37 dsl 2, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
469 1.32 macallan CTL_CREATE, CTL_EOL) == 0) {
470 1.32 macallan } else
471 1.32 macallan printf("couldn't create 'available' node\n");
472 1.32 macallan printf("speed: %d\n", curcpu()->ci_khz);
473 1.36 macallan
474 1.36 macallan /* support cpufreq */
475 1.36 macallan snprintf(cf->cf_name, CPUFREQ_NAME_MAX, "Intrepid");
476 1.36 macallan cf->cf_state[0].cfs_freq = sc->sc_spd_hi;
477 1.36 macallan cf->cf_state[1].cfs_freq = sc->sc_spd_lo;
478 1.36 macallan cf->cf_state_count = 2;
479 1.36 macallan cf->cf_mp = FALSE;
480 1.36 macallan cf->cf_cookie = sc;
481 1.36 macallan cf->cf_get_freq = obio_get_freq;
482 1.36 macallan cf->cf_set_freq = obio_set_freq;
483 1.36 macallan /*
484 1.36 macallan * XXX
485 1.36 macallan * cpufreq_register() calls xc_broadcast() which relies on kthreads
486 1.36 macallan * running so we need to postpone it
487 1.36 macallan */
488 1.36 macallan config_interrupts(sc->sc_dev, obio_setup_cpufreq);
489 1.27 garbled }
490 1.27 garbled
491 1.27 garbled static void
492 1.27 garbled obio_set_cpu_speed(struct obio_softc *sc, int fast)
493 1.27 garbled {
494 1.27 garbled
495 1.30 phx if (sc->sc_voltage < 0)
496 1.27 garbled return;
497 1.27 garbled
498 1.30 phx if (sc->sc_busspeed >= 0) {
499 1.30 phx /* set voltage and speed via gpio */
500 1.30 phx if (fast) {
501 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
502 1.30 phx sc->sc_voltage, 5);
503 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
504 1.30 phx sc->sc_busspeed, 5);
505 1.30 phx } else {
506 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
507 1.30 phx sc->sc_busspeed, 4);
508 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
509 1.30 phx sc->sc_voltage, 4);
510 1.30 phx }
511 1.30 phx }
512 1.30 phx else {
513 1.30 phx /* set voltage via gpio and speed via the 7447A's DFS bit */
514 1.30 phx if (fast) {
515 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
516 1.30 phx sc->sc_voltage, 5);
517 1.30 phx DELAY(1000);
518 1.30 phx }
519 1.30 phx
520 1.30 phx /* set DFS for all cpus */
521 1.30 phx cpu_set_dfs(fast ? 1 : 2);
522 1.30 phx DELAY(100);
523 1.30 phx
524 1.30 phx if (!fast) {
525 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
526 1.30 phx sc->sc_voltage, 4);
527 1.30 phx DELAY(1000);
528 1.30 phx }
529 1.27 garbled }
530 1.27 garbled }
531 1.27 garbled
532 1.27 garbled static int
533 1.27 garbled obio_get_cpu_speed(struct obio_softc *sc)
534 1.27 garbled {
535 1.27 garbled
536 1.30 phx if (sc->sc_voltage < 0)
537 1.27 garbled return 0;
538 1.27 garbled
539 1.30 phx if (sc->sc_busspeed >= 0) {
540 1.30 phx if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed)
541 1.30 phx & 1)
542 1.31 phx return 1;
543 1.30 phx }
544 1.30 phx else
545 1.30 phx return cpu_get_dfs() == 1;
546 1.27 garbled
547 1.27 garbled return 0;
548 1.27 garbled }
549 1.27 garbled
550 1.36 macallan static void
551 1.36 macallan obio_get_freq(void *cookie, void *spd)
552 1.36 macallan {
553 1.36 macallan struct obio_softc *sc = cookie;
554 1.36 macallan uint32_t *freq;
555 1.36 macallan
556 1.36 macallan freq = spd;
557 1.36 macallan if (obio_get_cpu_speed(sc) == 0) {
558 1.36 macallan *freq = sc->sc_spd_lo;
559 1.36 macallan } else
560 1.36 macallan *freq = sc->sc_spd_hi;
561 1.36 macallan }
562 1.36 macallan
563 1.36 macallan static void
564 1.36 macallan obio_set_freq(void *cookie, void *spd)
565 1.36 macallan {
566 1.36 macallan struct obio_softc *sc = cookie;
567 1.36 macallan uint32_t *freq;
568 1.36 macallan
569 1.36 macallan freq = spd;
570 1.36 macallan if (*freq == sc->sc_spd_lo) {
571 1.36 macallan obio_set_cpu_speed(sc, 0);
572 1.36 macallan } else if (*freq == sc->sc_spd_hi) {
573 1.36 macallan obio_set_cpu_speed(sc, 1);
574 1.36 macallan } else
575 1.36 macallan aprint_error_dev(sc->sc_dev, "%s(%d) bogus CPU speed\n", __func__, *freq);
576 1.36 macallan }
577 1.36 macallan
578 1.27 garbled static int
579 1.27 garbled sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
580 1.27 garbled {
581 1.27 garbled struct sysctlnode node = *rnode;
582 1.27 garbled struct obio_softc *sc = node.sysctl_data;
583 1.32 macallan int speed, mhz;
584 1.27 garbled
585 1.27 garbled speed = obio_get_cpu_speed(sc);
586 1.32 macallan switch (speed) {
587 1.32 macallan case 0:
588 1.32 macallan mhz = sc->sc_spd_lo;
589 1.32 macallan break;
590 1.32 macallan case 1:
591 1.32 macallan mhz = sc->sc_spd_hi;
592 1.32 macallan break;
593 1.32 macallan default:
594 1.32 macallan speed = -1;
595 1.32 macallan }
596 1.32 macallan node.sysctl_data = &mhz;
597 1.32 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
598 1.32 macallan int new_reg;
599 1.32 macallan
600 1.35 macallan new_reg = *(int *)node.sysctl_data;
601 1.32 macallan if (new_reg == sc->sc_spd_lo) {
602 1.32 macallan obio_set_cpu_speed(sc, 0);
603 1.32 macallan } else if (new_reg == sc->sc_spd_hi) {
604 1.32 macallan obio_set_cpu_speed(sc, 1);
605 1.32 macallan } else {
606 1.32 macallan printf("%s: new_reg %d\n", __func__, new_reg);
607 1.32 macallan return EINVAL;
608 1.27 garbled }
609 1.32 macallan return 0;
610 1.27 garbled }
611 1.32 macallan return EINVAL;
612 1.32 macallan }
613 1.32 macallan
614 1.32 macallan static int
615 1.32 macallan sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
616 1.32 macallan {
617 1.32 macallan struct sysctlnode node = *rnode;
618 1.32 macallan struct obio_softc *sc = node.sysctl_data;
619 1.32 macallan int speed, mhz;
620 1.32 macallan
621 1.32 macallan speed = obio_get_cpu_speed(sc);
622 1.32 macallan switch (speed) {
623 1.32 macallan case 0:
624 1.32 macallan mhz = sc->sc_spd_lo;
625 1.32 macallan break;
626 1.32 macallan case 1:
627 1.32 macallan mhz = sc->sc_spd_hi;
628 1.32 macallan break;
629 1.32 macallan default:
630 1.32 macallan speed = -1;
631 1.32 macallan }
632 1.32 macallan node.sysctl_data = &mhz;
633 1.32 macallan return sysctl_lookup(SYSCTLFN_CALL(&node));
634 1.32 macallan }
635 1.32 macallan
636 1.32 macallan static int
637 1.32 macallan sysctl_cpuspeed_available(SYSCTLFN_ARGS)
638 1.32 macallan {
639 1.32 macallan struct sysctlnode node = *rnode;
640 1.32 macallan struct obio_softc *sc = node.sysctl_data;
641 1.32 macallan char buf[128];
642 1.32 macallan
643 1.32 macallan snprintf(buf, 128, "%d %d", sc->sc_spd_lo, sc->sc_spd_hi);
644 1.32 macallan node.sysctl_data = buf;
645 1.32 macallan return(sysctl_lookup(SYSCTLFN_CALL(&node)));
646 1.32 macallan }
647 1.32 macallan
648 1.32 macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
649 1.32 macallan {
650 1.32 macallan
651 1.32 macallan sysctl_createv(NULL, 0, NULL, NULL,
652 1.32 macallan CTLFLAG_PERMANENT,
653 1.32 macallan CTLTYPE_NODE, "machdep", NULL,
654 1.32 macallan NULL, 0, NULL, 0,
655 1.32 macallan CTL_MACHDEP, CTL_EOL);
656 1.27 garbled }
657 1.27 garbled
658 1.27 garbled #endif /* OBIO_SPEEDCONTROL */
659