1/*	$NetBSD: clkctrl.c,v 1.4 2008/05/08 02:16:27 macallan Exp $	*/
2
3/*
4 * Copyright (c) 2005 Michael Lorenz
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: clkctrl.c,v 1.4 2008/05/08 02:16:27 macallan Exp $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/device.h>
34#include <sys/kernel.h>
35
36#include <machine/autoconf.h>
37#include <machine/cpu.h>
38
39#include <sparc/sparc/vaddrs.h>
40
41static int clkctrl_match(device_t, cfdata_t, void *);
42static void clkctrl_attach(device_t, device_t, void *);
43
44CFATTACH_DECL_NEW(clkctrl, 0,
45     clkctrl_match, clkctrl_attach, NULL, NULL);
46
47static void tadpole_cpu_sleep(struct cpu_info *);
48volatile uint8_t *clkctrl_reg = NULL;
49
50static int
51clkctrl_match(device_t parent, cfdata_t cf, void *aux)
52{
53	union obio_attach_args *uoba = aux;
54
55	if ((uoba->uoba_isobio4 != 0) || (clkctrl_reg != NULL))
56		return (0);
57
58	return (strcmp("clk-ctrl", uoba->uoba_sbus.sa_name) == 0);
59}
60
61static void
62clkctrl_attach(device_t parent, device_t self, void *aux)
63{
64	union obio_attach_args *uoba = aux;
65	struct sbus_attach_args *sa = &uoba->uoba_sbus;
66	bus_space_handle_t bh;
67	struct cpu_info *cur;
68
69	if (clkctrl_reg != NULL) {
70		aprint_error("unable to attach more than once\n");
71		return;
72	}
73	if (sbus_bus_map(sa->sa_bustag,
74			 sa->sa_slot, sa->sa_offset,
75			 sizeof(long),
76			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
77		aprint_error("unable to map register\n");
78		return;
79	}
80
81	clkctrl_reg = (volatile uint8_t *)bus_space_vaddr(sa->sa_bustag, bh);
82
83#ifdef DEBUG
84	printf(" reg: %x", (uint32_t)clkctrl_reg);
85#endif
86	cur = curcpu();
87	cur->idlespin = tadpole_cpu_sleep;
88
89	printf("\n");
90}
91
92/* ARGSUSED */
93static void
94tadpole_cpu_sleep(struct cpu_info *ci)
95{
96	if (clkctrl_reg == 0)
97		return;
98	*clkctrl_reg = 0;
99}
100