Home | History | Annotate | Line # | Download | only in oea
      1  1.3       rin /*	$NetBSD: cpu_speedctl.c,v 1.3 2020/07/06 10:58:06 rin Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2006 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.2       rin 
     29  1.1  macallan #include <sys/cdefs.h>
     30  1.3       rin __KERNEL_RCSID(0, "$NetBSD: cpu_speedctl.c,v 1.3 2020/07/06 10:58:06 rin Exp $");
     31  1.1  macallan 
     32  1.1  macallan #include <sys/param.h>
     33  1.1  macallan #include <sys/systm.h>
     34  1.1  macallan #include <sys/device.h>
     35  1.1  macallan #include <sys/types.h>
     36  1.1  macallan 
     37  1.1  macallan #include <powerpc/psl.h>
     38  1.1  macallan #include <powerpc/spr.h>
     39  1.1  macallan #include <powerpc/oea/cpufeat.h>
     40  1.1  macallan #include <powerpc/oea/spr.h>
     41  1.1  macallan 
     42  1.1  macallan #include <sys/sysctl.h>
     43  1.1  macallan #include <dev/ofw/openfirm.h>
     44  1.1  macallan 
     45  1.1  macallan void init_scom_speedctl(void);
     46  1.1  macallan static int scom_speedctl = 0;
     47  1.1  macallan static uint32_t scom_speeds[2];
     48  1.1  macallan static uint32_t scom_reg[2];
     49  1.1  macallan static int  sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
     50  1.1  macallan static int  sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
     51  1.1  macallan static int  sysctl_cpuspeed_available(SYSCTLFN_ARGS);
     52  1.1  macallan 
     53  1.1  macallan void
     54  1.1  macallan init_scom_speedctl(void)
     55  1.1  macallan {
     56  1.1  macallan 	int node;
     57  1.1  macallan 	const struct sysctlnode *sysctl_node, *me, *freq;
     58  1.1  macallan 
     59  1.1  macallan 	/* do this only once */
     60  1.1  macallan 	if (scom_speedctl != 0) return;
     61  1.1  macallan 	scom_speedctl = 1;
     62  1.1  macallan 
     63  1.1  macallan 	node = OF_finddevice("/cpus/@0");
     64  1.1  macallan 	if (OF_getprop(node, "power-mode-data", scom_reg, 8) != 8)
     65  1.1  macallan 		return;
     66  1.1  macallan 	if (OF_getprop(node, "clock-frequency", scom_speeds, 4) != 4)
     67  1.1  macallan 		return;
     68  1.1  macallan 	scom_speeds[0] = scom_speeds[0] / 1000000; /* Hz -> MHz */
     69  1.1  macallan 	scom_speeds[1] = scom_speeds[0] / 2;
     70  1.1  macallan 
     71  1.1  macallan 	sysctl_node = NULL;
     72  1.1  macallan 
     73  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     74  1.1  macallan 	    &me,
     75  1.1  macallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
     76  1.1  macallan 	    0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
     77  1.1  macallan 		printf("couldn't create 'cpu' node\n");
     78  1.1  macallan 
     79  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     80  1.1  macallan 	    &freq,
     81  1.1  macallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
     82  1.1  macallan 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
     83  1.1  macallan 		printf("couldn't create 'frequency' node\n");
     84  1.1  macallan 
     85  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     86  1.1  macallan 	    &sysctl_node,
     87  1.1  macallan 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
     88  1.1  macallan 	    CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
     89  1.1  macallan 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
     90  1.1  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
     91  1.1  macallan 	} else
     92  1.1  macallan 		printf("couldn't create 'target' node\n");
     93  1.1  macallan 
     94  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     95  1.1  macallan 	    &sysctl_node,
     96  1.1  macallan 	    CTLFLAG_READWRITE,
     97  1.1  macallan 	    CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
     98  1.1  macallan 	    1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
     99  1.1  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    100  1.1  macallan 	} else
    101  1.1  macallan 		printf("couldn't create 'current' node\n");
    102  1.1  macallan 
    103  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
    104  1.1  macallan 	    &sysctl_node,
    105  1.1  macallan 	    CTLFLAG_READWRITE,
    106  1.1  macallan 	    CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
    107  1.1  macallan 	    2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    108  1.1  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    109  1.1  macallan 	} else
    110  1.1  macallan 		printf("couldn't create 'available' node\n");
    111  1.1  macallan }
    112  1.1  macallan 
    113  1.1  macallan static int
    114  1.1  macallan sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
    115  1.1  macallan {
    116  1.1  macallan 	struct sysctlnode node = *rnode;
    117  1.1  macallan 	int speed, nspeed = -1, mhz;
    118  1.1  macallan 
    119  1.1  macallan 	speed = (scom_read(SCOM_PSR) >> 56) & 3;
    120  1.1  macallan 	if (speed > 1) speed = 1;
    121  1.1  macallan 	mhz = scom_speeds[speed];
    122  1.1  macallan 	node.sysctl_data = &mhz;
    123  1.1  macallan 
    124  1.1  macallan 	if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    125  1.1  macallan 		int new_reg;
    126  1.1  macallan 
    127  1.1  macallan 		new_reg = *(int *)node.sysctl_data;
    128  1.1  macallan 		if (new_reg == scom_speeds[0]) {
    129  1.1  macallan 			nspeed = 0;
    130  1.1  macallan 		} else if (new_reg == scom_speeds[1]) {
    131  1.1  macallan 			nspeed = 1;
    132  1.1  macallan 		} else {
    133  1.1  macallan 			printf("%s: new_reg %d\n", __func__, new_reg);
    134  1.1  macallan 			return EINVAL;
    135  1.1  macallan 		}
    136  1.1  macallan 		if (nspeed != speed) {
    137  1.1  macallan 			volatile uint64_t junk;
    138  1.1  macallan 			scom_write(SCOM_PCR, 0x80000000);
    139  1.1  macallan 			scom_write(SCOM_PCR, scom_reg[nspeed]);
    140  1.1  macallan 			junk = scom_read(SCOM_PSR);
    141  1.1  macallan 			__USE(junk);
    142  1.1  macallan 		}
    143  1.1  macallan 		return 0;
    144  1.1  macallan 	}
    145  1.1  macallan 	return EINVAL;
    146  1.1  macallan }
    147  1.1  macallan 
    148  1.1  macallan static int
    149  1.1  macallan sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
    150  1.1  macallan {
    151  1.1  macallan 	struct sysctlnode node = *rnode;
    152  1.1  macallan 	uint64_t speed;
    153  1.1  macallan 	int mhz;
    154  1.1  macallan 
    155  1.1  macallan 	speed = (scom_read(SCOM_PSR) >> 56) & 3;
    156  1.1  macallan 	if (speed > 1) speed = 1;
    157  1.1  macallan 
    158  1.1  macallan 	mhz = scom_speeds[speed];
    159  1.1  macallan 	node.sysctl_data = &mhz;
    160  1.1  macallan 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    161  1.1  macallan }
    162  1.1  macallan 
    163  1.1  macallan static int
    164  1.1  macallan sysctl_cpuspeed_available(SYSCTLFN_ARGS)
    165  1.1  macallan {
    166  1.1  macallan 	struct sysctlnode node = *rnode;
    167  1.1  macallan 	char buf[128];
    168  1.1  macallan 
    169  1.1  macallan 	snprintf(buf, 128, "%d %d", scom_speeds[0], scom_speeds[1]);
    170  1.1  macallan 	node.sysctl_data = buf;
    171  1.1  macallan 	return(sysctl_lookup(SYSCTLFN_CALL(&node)));
    172  1.1  macallan }
    173  1.1  macallan 
    174  1.1  macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
    175  1.1  macallan {
    176  1.1  macallan 
    177  1.1  macallan 	sysctl_createv(NULL, 0, NULL, NULL,
    178  1.1  macallan 		       CTLFLAG_PERMANENT,
    179  1.1  macallan 		       CTLTYPE_NODE, "machdep", NULL,
    180  1.1  macallan 		       NULL, 0, NULL, 0,
    181  1.1  macallan 		       CTL_MACHDEP, CTL_EOL);
    182  1.1  macallan }
    183