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