1/*	$NetBSD: auxreg.c,v 1.38 2007/10/17 19:57:14 garbled Exp $ */
2
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 *	This product includes software developed by the University of
14 *	California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 *    may be used to endorse or promote products derived from this software
26 *    without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 *	@(#)auxreg.c	8.1 (Berkeley) 6/11/93
41 */
42
43#include <sys/cdefs.h>
44__KERNEL_RCSID(0, "$NetBSD: auxreg.c,v 1.38 2007/10/17 19:57:14 garbled Exp $");
45
46#include "opt_blink.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/callout.h>
51#include <sys/device.h>
52#include <sys/kernel.h>
53
54#include <machine/autoconf.h>
55
56#include <sparc/sparc/vaddrs.h>
57#include <sparc/sparc/auxreg.h>
58
59static int auxregmatch_mainbus(device_t, cfdata_t, void *);
60static int auxregmatch_obio(device_t, cfdata_t, void *);
61static void auxregattach_mainbus(device_t, device_t, void *);
62static void auxregattach_obio(device_t, device_t, void *);
63
64static void auxregattach(void);
65
66CFATTACH_DECL_NEW(auxreg_mainbus, 0,
67    auxregmatch_mainbus, auxregattach_mainbus, NULL, NULL);
68
69CFATTACH_DECL_NEW(auxreg_obio, 0,
70    auxregmatch_obio, auxregattach_obio, NULL, NULL);
71
72#ifdef BLINK
73static callout_t blink_ch;
74
75static void blink(void *);
76
77static void
78blink(void *zero)
79{
80	register int s;
81
82	s = splhigh();
83	LED_FLIP;
84	splx(s);
85	/*
86	 * Blink rate is:
87	 *	full cycle every second if completely idle (loadav = 0)
88	 *	full cycle every 2 seconds if loadav = 1
89	 *	full cycle every 3 seconds if loadav = 2
90	 * etc.
91	 */
92	s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
93	callout_reset(&blink_ch, s, blink, NULL);
94}
95#endif
96
97/*
98 * The OPENPROM calls this "auxiliary-io" (sun4c) or "auxio" (sun4m).
99 */
100static int
101auxregmatch_mainbus(device_t parent, cfdata_t cf, void *aux)
102{
103	struct mainbus_attach_args *ma = aux;
104
105	return (strcmp("auxiliary-io", ma->ma_name) == 0);
106}
107
108static int
109auxregmatch_obio(device_t parent, cfdata_t cf, void *aux)
110{
111	union obio_attach_args *uoba = aux;
112
113	if (uoba->uoba_isobio4 != 0)
114		return (0);
115
116	return (strcmp("auxio", uoba->uoba_sbus.sa_name) == 0);
117}
118
119/* ARGSUSED */
120static void
121auxregattach_mainbus(device_t parent, device_t self, void *aux)
122{
123	struct mainbus_attach_args *ma = aux;
124	bus_space_handle_t bh;
125
126	if (bus_space_map2(ma->ma_bustag,
127			  (bus_addr_t)ma->ma_paddr,
128			  sizeof(long),
129			  BUS_SPACE_MAP_LINEAR,
130			  AUXREG_VA,
131			  &bh) != 0) {
132		printf("auxregattach_mainbus: can't map register\n");
133		return;
134	}
135
136	auxio_reg = AUXIO4C_REG;
137	auxio_regval = *AUXIO4C_REG | AUXIO4C_FEJ | AUXIO4C_MB1;
138	auxregattach();
139}
140
141static void
142auxregattach_obio(device_t parent, device_t self, void *aux)
143{
144	union obio_attach_args *uoba = aux;
145	struct sbus_attach_args *sa = &uoba->uoba_sbus;
146	bus_space_handle_t bh;
147
148	if (bus_space_map2(sa->sa_bustag,
149			  BUS_ADDR(sa->sa_slot, sa->sa_offset),
150			  sizeof(long),
151			  BUS_SPACE_MAP_LINEAR,
152			  AUXREG_VA, &bh) != 0) {
153		printf("auxregattach_obio: can't map register\n");
154		return;
155	}
156
157	auxio_reg = AUXIO4M_REG;
158	auxio_regval = *AUXIO4M_REG | AUXIO4M_MB1;
159	auxregattach();
160}
161
162static void
163auxregattach(void)
164{
165
166	printf("\n");
167#ifdef BLINK
168	callout_init(&blink_ch, 0);
169	blink((void *)0);
170#else
171	LED_ON;
172#endif
173}
174
175unsigned int
176auxregbisc(int bis, int bic)
177{
178	register int s;
179
180	if (auxio_reg == 0)
181		/*
182		 * Not all machines have an `aux' register; devices that
183		 * depend on it should not get configured if it's absent.
184		 */
185		panic("no aux register");
186
187	s = splhigh();
188	auxio_regval = (auxio_regval | bis) & ~bic;
189	*auxio_reg = auxio_regval;
190	splx(s);
191	return (auxio_regval);
192}
193