obio.c revision 1.45 1 1.45 macallan /* $NetBSD: obio.c,v 1.45 2018/05/04 17:17:48 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.45 macallan __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.45 2018/05/04 17:17:48 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.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.1 tsubai config_found(self, &ca, obio_print);
275 1.1 tsubai }
276 1.1 tsubai }
277 1.1 tsubai
278 1.27 garbled static const char * const skiplist[] = {
279 1.8 tsubai "interrupt-controller",
280 1.8 tsubai "gpio",
281 1.8 tsubai "escc-legacy",
282 1.8 tsubai "timer",
283 1.9 tsubai "i2c",
284 1.24 sanjayl "power-mgt",
285 1.27 garbled "escc",
286 1.27 garbled "battery",
287 1.27 garbled "backlight"
288 1.24 sanjayl
289 1.8 tsubai };
290 1.8 tsubai
291 1.8 tsubai #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
292 1.8 tsubai
293 1.1 tsubai int
294 1.29 dsl obio_print(void *aux, const char *obio)
295 1.1 tsubai {
296 1.1 tsubai struct confargs *ca = aux;
297 1.8 tsubai int i;
298 1.8 tsubai
299 1.8 tsubai for (i = 0; i < N_LIST; i++)
300 1.8 tsubai if (strcmp(ca->ca_name, skiplist[i]) == 0)
301 1.8 tsubai return QUIET;
302 1.1 tsubai
303 1.1 tsubai if (obio)
304 1.17 thorpej aprint_normal("%s at %s", ca->ca_name, obio);
305 1.1 tsubai
306 1.1 tsubai if (ca->ca_nreg > 0)
307 1.17 thorpej aprint_normal(" offset 0x%x", ca->ca_reg[0]);
308 1.1 tsubai
309 1.1 tsubai return UNCONF;
310 1.1 tsubai }
311 1.27 garbled
312 1.28 macallan void obio_write_4(int offset, uint32_t value)
313 1.28 macallan {
314 1.28 macallan if (obio0 == NULL)
315 1.28 macallan return;
316 1.28 macallan bus_space_write_4(obio0->sc_tag, obio0->sc_bh, offset, value);
317 1.28 macallan }
318 1.28 macallan
319 1.28 macallan void obio_write_1(int offset, uint8_t value)
320 1.28 macallan {
321 1.28 macallan if (obio0 == NULL)
322 1.28 macallan return;
323 1.28 macallan bus_space_write_1(obio0->sc_tag, obio0->sc_bh, offset, value);
324 1.28 macallan }
325 1.28 macallan
326 1.28 macallan uint32_t obio_read_4(int offset)
327 1.28 macallan {
328 1.28 macallan if (obio0 == NULL)
329 1.28 macallan return 0xffffffff;
330 1.28 macallan return bus_space_read_4(obio0->sc_tag, obio0->sc_bh, offset);
331 1.28 macallan }
332 1.28 macallan
333 1.28 macallan uint8_t obio_read_1(int offset)
334 1.28 macallan {
335 1.28 macallan if (obio0 == NULL)
336 1.28 macallan return 0xff;
337 1.28 macallan return bus_space_read_1(obio0->sc_tag, obio0->sc_bh, offset);
338 1.28 macallan }
339 1.28 macallan
340 1.45 macallan int
341 1.45 macallan obio_space_map(bus_addr_t addr, bus_size_t size, bus_space_handle_t *bh)
342 1.45 macallan {
343 1.45 macallan if (obio0 == NULL)
344 1.45 macallan return 0xff;
345 1.45 macallan return bus_space_subregion(obio0->sc_tag, obio0->sc_bh,
346 1.45 macallan addr & 0xfffff, size, bh);
347 1.45 macallan }
348 1.45 macallan
349 1.27 garbled #ifdef OBIO_SPEED_CONTROL
350 1.27 garbled
351 1.27 garbled static void
352 1.36 macallan obio_setup_cpufreq(device_t dev)
353 1.36 macallan {
354 1.36 macallan struct obio_softc *sc = device_private(dev);
355 1.36 macallan int ret;
356 1.36 macallan
357 1.36 macallan ret = cpufreq_register(&sc->sc_cf);
358 1.36 macallan if (ret != 0)
359 1.36 macallan aprint_error_dev(sc->sc_dev, "cpufreq_register() failed, error %d\n", ret);
360 1.36 macallan }
361 1.36 macallan
362 1.36 macallan static void
363 1.27 garbled obio_setup_gpios(struct obio_softc *sc, int node)
364 1.27 garbled {
365 1.31 phx uint32_t gpio_base, reg[6];
366 1.35 macallan const struct sysctlnode *sysctl_node, *me, *freq;
367 1.36 macallan struct cpufreq *cf = &sc->sc_cf;
368 1.27 garbled char name[32];
369 1.32 macallan int child, use_dfs, cpunode, hiclock;
370 1.27 garbled
371 1.27 garbled if (of_compatible(sc->sc_node, keylargo) == -1)
372 1.27 garbled return;
373 1.27 garbled
374 1.27 garbled if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
375 1.27 garbled return;
376 1.27 garbled
377 1.27 garbled gpio_base = reg[0];
378 1.27 garbled DPRINTF("gpio_base: %02x\n", gpio_base);
379 1.27 garbled
380 1.27 garbled /* now look for voltage and bus speed gpios */
381 1.30 phx use_dfs = 0;
382 1.27 garbled for (child = OF_child(node); child; child = OF_peer(child)) {
383 1.27 garbled
384 1.27 garbled if (OF_getprop(child, "name", name, sizeof(name)) < 1)
385 1.27 garbled continue;
386 1.27 garbled
387 1.27 garbled if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
388 1.27 garbled continue;
389 1.27 garbled
390 1.31 phx /*
391 1.31 phx * These register offsets either have to be added to the obio
392 1.31 phx * base address or to the gpio base address. This differs
393 1.31 phx * even in the same OF-tree! So we guess the offset is
394 1.31 phx * based on obio when it is larger than the gpio_base.
395 1.31 phx */
396 1.31 phx if (reg[0] >= gpio_base)
397 1.31 phx reg[0] -= gpio_base;
398 1.31 phx
399 1.27 garbled if (strcmp(name, "frequency-gpio") == 0) {
400 1.27 garbled DPRINTF("found frequency_gpio at %02x\n", reg[0]);
401 1.27 garbled sc->sc_busspeed = gpio_base + reg[0];
402 1.27 garbled }
403 1.27 garbled if (strcmp(name, "voltage-gpio") == 0) {
404 1.27 garbled DPRINTF("found voltage_gpio at %02x\n", reg[0]);
405 1.27 garbled sc->sc_voltage = gpio_base + reg[0];
406 1.27 garbled }
407 1.30 phx if (strcmp(name, "cpu-vcore-select") == 0) {
408 1.30 phx DPRINTF("found cpu-vcore-select at %02x\n", reg[0]);
409 1.30 phx sc->sc_voltage = gpio_base + reg[0];
410 1.30 phx /* frequency gpio is not needed, we use cpu's DFS */
411 1.30 phx use_dfs = 1;
412 1.30 phx }
413 1.27 garbled }
414 1.27 garbled
415 1.30 phx if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0 && !use_dfs))
416 1.27 garbled return;
417 1.27 garbled
418 1.27 garbled printf("%s: enabling Intrepid CPU speed control\n",
419 1.34 macallan device_xname(sc->sc_dev));
420 1.27 garbled
421 1.32 macallan sc->sc_spd_lo = curcpu()->ci_khz / 1000;
422 1.32 macallan hiclock = 0;
423 1.32 macallan cpunode = OF_finddevice("/cpus/@0");
424 1.32 macallan OF_getprop(cpunode, "clock-frequency", &hiclock, 4);
425 1.36 macallan if (hiclock != 0)
426 1.36 macallan sc->sc_spd_hi = (hiclock + 500000) / 1000000;
427 1.36 macallan printf("hiclock: %d\n", sc->sc_spd_hi);
428 1.36 macallan
429 1.30 phx sysctl_node = NULL;
430 1.32 macallan
431 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
432 1.35 macallan &me,
433 1.32 macallan CTLFLAG_READWRITE, CTLTYPE_NODE, "intrepid", NULL, NULL,
434 1.32 macallan 0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
435 1.35 macallan printf("couldn't create 'intrepid' node\n");
436 1.32 macallan
437 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
438 1.35 macallan &freq,
439 1.32 macallan CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
440 1.32 macallan 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
441 1.32 macallan printf("couldn't create 'frequency' node\n");
442 1.32 macallan
443 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
444 1.35 macallan &sysctl_node,
445 1.35 macallan CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
446 1.32 macallan CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
447 1.37 dsl 0, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
448 1.32 macallan CTL_CREATE, CTL_EOL) == 0) {
449 1.32 macallan } else
450 1.32 macallan printf("couldn't create 'target' node\n");
451 1.32 macallan
452 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
453 1.35 macallan &sysctl_node,
454 1.35 macallan CTLFLAG_READWRITE,
455 1.32 macallan CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
456 1.37 dsl 1, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
457 1.32 macallan CTL_CREATE, CTL_EOL) == 0) {
458 1.32 macallan } else
459 1.32 macallan printf("couldn't create 'current' node\n");
460 1.32 macallan
461 1.32 macallan if (sysctl_createv(NULL, 0, NULL,
462 1.35 macallan &sysctl_node,
463 1.32 macallan CTLFLAG_READWRITE,
464 1.32 macallan CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
465 1.37 dsl 2, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
466 1.32 macallan CTL_CREATE, CTL_EOL) == 0) {
467 1.32 macallan } else
468 1.32 macallan printf("couldn't create 'available' node\n");
469 1.32 macallan printf("speed: %d\n", curcpu()->ci_khz);
470 1.36 macallan
471 1.36 macallan /* support cpufreq */
472 1.36 macallan snprintf(cf->cf_name, CPUFREQ_NAME_MAX, "Intrepid");
473 1.36 macallan cf->cf_state[0].cfs_freq = sc->sc_spd_hi;
474 1.36 macallan cf->cf_state[1].cfs_freq = sc->sc_spd_lo;
475 1.36 macallan cf->cf_state_count = 2;
476 1.36 macallan cf->cf_mp = FALSE;
477 1.36 macallan cf->cf_cookie = sc;
478 1.36 macallan cf->cf_get_freq = obio_get_freq;
479 1.36 macallan cf->cf_set_freq = obio_set_freq;
480 1.36 macallan /*
481 1.36 macallan * XXX
482 1.36 macallan * cpufreq_register() calls xc_broadcast() which relies on kthreads
483 1.36 macallan * running so we need to postpone it
484 1.36 macallan */
485 1.36 macallan config_interrupts(sc->sc_dev, obio_setup_cpufreq);
486 1.27 garbled }
487 1.27 garbled
488 1.27 garbled static void
489 1.27 garbled obio_set_cpu_speed(struct obio_softc *sc, int fast)
490 1.27 garbled {
491 1.27 garbled
492 1.30 phx if (sc->sc_voltage < 0)
493 1.27 garbled return;
494 1.27 garbled
495 1.30 phx if (sc->sc_busspeed >= 0) {
496 1.30 phx /* set voltage and speed via gpio */
497 1.30 phx if (fast) {
498 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
499 1.30 phx sc->sc_voltage, 5);
500 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
501 1.30 phx sc->sc_busspeed, 5);
502 1.30 phx } else {
503 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
504 1.30 phx sc->sc_busspeed, 4);
505 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
506 1.30 phx sc->sc_voltage, 4);
507 1.30 phx }
508 1.30 phx }
509 1.30 phx else {
510 1.30 phx /* set voltage via gpio and speed via the 7447A's DFS bit */
511 1.30 phx if (fast) {
512 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
513 1.30 phx sc->sc_voltage, 5);
514 1.30 phx DELAY(1000);
515 1.30 phx }
516 1.30 phx
517 1.30 phx /* set DFS for all cpus */
518 1.30 phx cpu_set_dfs(fast ? 1 : 2);
519 1.30 phx DELAY(100);
520 1.30 phx
521 1.30 phx if (!fast) {
522 1.30 phx bus_space_write_1(sc->sc_tag, sc->sc_bh,
523 1.30 phx sc->sc_voltage, 4);
524 1.30 phx DELAY(1000);
525 1.30 phx }
526 1.27 garbled }
527 1.27 garbled }
528 1.27 garbled
529 1.27 garbled static int
530 1.27 garbled obio_get_cpu_speed(struct obio_softc *sc)
531 1.27 garbled {
532 1.27 garbled
533 1.30 phx if (sc->sc_voltage < 0)
534 1.27 garbled return 0;
535 1.27 garbled
536 1.30 phx if (sc->sc_busspeed >= 0) {
537 1.30 phx if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed)
538 1.30 phx & 1)
539 1.31 phx return 1;
540 1.30 phx }
541 1.30 phx else
542 1.30 phx return cpu_get_dfs() == 1;
543 1.27 garbled
544 1.27 garbled return 0;
545 1.27 garbled }
546 1.27 garbled
547 1.36 macallan static void
548 1.36 macallan obio_get_freq(void *cookie, void *spd)
549 1.36 macallan {
550 1.36 macallan struct obio_softc *sc = cookie;
551 1.36 macallan uint32_t *freq;
552 1.36 macallan
553 1.36 macallan freq = spd;
554 1.36 macallan if (obio_get_cpu_speed(sc) == 0) {
555 1.36 macallan *freq = sc->sc_spd_lo;
556 1.36 macallan } else
557 1.36 macallan *freq = sc->sc_spd_hi;
558 1.36 macallan }
559 1.36 macallan
560 1.36 macallan static void
561 1.36 macallan obio_set_freq(void *cookie, void *spd)
562 1.36 macallan {
563 1.36 macallan struct obio_softc *sc = cookie;
564 1.36 macallan uint32_t *freq;
565 1.36 macallan
566 1.36 macallan freq = spd;
567 1.36 macallan if (*freq == sc->sc_spd_lo) {
568 1.36 macallan obio_set_cpu_speed(sc, 0);
569 1.36 macallan } else if (*freq == sc->sc_spd_hi) {
570 1.36 macallan obio_set_cpu_speed(sc, 1);
571 1.36 macallan } else
572 1.36 macallan aprint_error_dev(sc->sc_dev, "%s(%d) bogus CPU speed\n", __func__, *freq);
573 1.36 macallan }
574 1.36 macallan
575 1.27 garbled static int
576 1.27 garbled sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
577 1.27 garbled {
578 1.27 garbled struct sysctlnode node = *rnode;
579 1.27 garbled struct obio_softc *sc = node.sysctl_data;
580 1.32 macallan int speed, mhz;
581 1.27 garbled
582 1.27 garbled speed = obio_get_cpu_speed(sc);
583 1.32 macallan switch (speed) {
584 1.32 macallan case 0:
585 1.32 macallan mhz = sc->sc_spd_lo;
586 1.32 macallan break;
587 1.32 macallan case 1:
588 1.32 macallan mhz = sc->sc_spd_hi;
589 1.32 macallan break;
590 1.32 macallan default:
591 1.32 macallan speed = -1;
592 1.32 macallan }
593 1.32 macallan node.sysctl_data = &mhz;
594 1.32 macallan if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
595 1.32 macallan int new_reg;
596 1.32 macallan
597 1.35 macallan new_reg = *(int *)node.sysctl_data;
598 1.32 macallan if (new_reg == sc->sc_spd_lo) {
599 1.32 macallan obio_set_cpu_speed(sc, 0);
600 1.32 macallan } else if (new_reg == sc->sc_spd_hi) {
601 1.32 macallan obio_set_cpu_speed(sc, 1);
602 1.32 macallan } else {
603 1.32 macallan printf("%s: new_reg %d\n", __func__, new_reg);
604 1.32 macallan return EINVAL;
605 1.27 garbled }
606 1.32 macallan return 0;
607 1.27 garbled }
608 1.32 macallan return EINVAL;
609 1.32 macallan }
610 1.32 macallan
611 1.32 macallan static int
612 1.32 macallan sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
613 1.32 macallan {
614 1.32 macallan struct sysctlnode node = *rnode;
615 1.32 macallan struct obio_softc *sc = node.sysctl_data;
616 1.32 macallan int speed, mhz;
617 1.32 macallan
618 1.32 macallan speed = obio_get_cpu_speed(sc);
619 1.32 macallan switch (speed) {
620 1.32 macallan case 0:
621 1.32 macallan mhz = sc->sc_spd_lo;
622 1.32 macallan break;
623 1.32 macallan case 1:
624 1.32 macallan mhz = sc->sc_spd_hi;
625 1.32 macallan break;
626 1.32 macallan default:
627 1.32 macallan speed = -1;
628 1.32 macallan }
629 1.32 macallan node.sysctl_data = &mhz;
630 1.32 macallan return sysctl_lookup(SYSCTLFN_CALL(&node));
631 1.32 macallan }
632 1.32 macallan
633 1.32 macallan static int
634 1.32 macallan sysctl_cpuspeed_available(SYSCTLFN_ARGS)
635 1.32 macallan {
636 1.32 macallan struct sysctlnode node = *rnode;
637 1.32 macallan struct obio_softc *sc = node.sysctl_data;
638 1.32 macallan char buf[128];
639 1.32 macallan
640 1.32 macallan snprintf(buf, 128, "%d %d", sc->sc_spd_lo, sc->sc_spd_hi);
641 1.32 macallan node.sysctl_data = buf;
642 1.32 macallan return(sysctl_lookup(SYSCTLFN_CALL(&node)));
643 1.32 macallan }
644 1.32 macallan
645 1.32 macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
646 1.32 macallan {
647 1.32 macallan
648 1.32 macallan sysctl_createv(NULL, 0, NULL, NULL,
649 1.32 macallan CTLFLAG_PERMANENT,
650 1.32 macallan CTLTYPE_NODE, "machdep", NULL,
651 1.32 macallan NULL, 0, NULL, 0,
652 1.32 macallan CTL_MACHDEP, CTL_EOL);
653 1.27 garbled }
654 1.27 garbled
655 1.27 garbled #endif /* OBIO_SPEEDCONTROL */
656