xref: /freebsd/sys/powerpc/powermac/vcoregpio.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/cpu.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/openfirm.h>
43 
44 #include <powerpc/powermac/macgpiovar.h>
45 
46 static int	vcoregpio_probe(device_t);
47 static int	vcoregpio_attach(device_t);
48 static void	vcoregpio_pre_change(device_t, const struct cf_level *level);
49 static void	vcoregpio_post_change(device_t, const struct cf_level *level);
50 
51 static device_method_t  vcoregpio_methods[] = {
52 	/* Device interface */
53 	DEVMETHOD(device_probe,		vcoregpio_probe),
54 	DEVMETHOD(device_attach,	vcoregpio_attach),
55 	{ 0, 0 },
56 };
57 
58 static driver_t vcoregpio_driver = {
59 	"vcoregpio",
60 	vcoregpio_methods,
61 	0
62 };
63 
64 DRIVER_MODULE(vcoregpio, macgpio, vcoregpio_driver, 0, 0);
65 
66 static int
67 vcoregpio_probe(device_t dev)
68 {
69 	const char *name = ofw_bus_get_name(dev);
70 
71 	if (strcmp(name, "cpu-vcore-select") != 0)
72 		return (ENXIO);
73 
74 	device_set_desc(dev, "CPU Core Voltage Control");
75 	return (0);
76 }
77 
78 static int
79 vcoregpio_attach(device_t dev)
80 {
81 	EVENTHANDLER_REGISTER(cpufreq_pre_change, vcoregpio_pre_change, dev,
82 	    EVENTHANDLER_PRI_ANY);
83 	EVENTHANDLER_REGISTER(cpufreq_post_change, vcoregpio_post_change, dev,
84 	    EVENTHANDLER_PRI_ANY);
85 
86 	return (0);
87 }
88 
89 static void
90 vcoregpio_pre_change(device_t dev, const struct cf_level *level)
91 {
92 	if (level->rel_set[0].freq == 10000 /* max */) {
93 		/*
94 		 * Make sure the CPU voltage is raised before we raise
95 		 * the clock.
96 		 */
97 		macgpio_write(dev, GPIO_DDR_OUTPUT | 1);
98 		DELAY(1000);
99 	}
100 }
101 
102 static void
103 vcoregpio_post_change(device_t dev, const struct cf_level *level)
104 {
105 	if (level->rel_set[0].freq < 10000 /* max */) {
106 		DELAY(1000);
107 		/* We are safe to reduce CPU voltage now */
108 		macgpio_write(dev, GPIO_DDR_OUTPUT | 0);
109 	}
110 }
111