2 * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 * from: OpenBSD: clkbrd.c,v 1.5 2004/10/01 18:18:49 jason Exp
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/resource.h>
40 #include <dev/led/led.h>
41 #include <dev/ofw/ofw_bus.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <sparc64/fhc/clkbrdreg.h>
52 #define CLKBRD_CLKVER 2
55 struct device *sc_dev;
56 struct resource *sc_res[CLKBRD_NREG];
57 int sc_rid[CLKBRD_NREG];
58 bus_space_tag_t sc_bt[CLKBRD_NREG];
59 bus_space_handle_t sc_bh[CLKBRD_NREG];
61 struct cdev *sc_led_dev;
63 #define CLKBRD_HAS_CLKVER (1 << 0)
66 static devclass_t clkbrd_devclass;
68 static device_probe_t clkbrd_probe;
69 static device_attach_t clkbrd_attach;
70 static device_detach_t clkbrd_detach;
72 static void clkbrd_free_resources(struct clkbrd_softc *);
73 static void clkbrd_led_func(void *, int);
75 static device_method_t clkbrd_methods[] = {
76 /* Device interface */
77 DEVMETHOD(device_probe, clkbrd_probe),
78 DEVMETHOD(device_attach, clkbrd_attach),
79 DEVMETHOD(device_detach, clkbrd_detach),
84 static driver_t clkbrd_driver = {
87 sizeof(struct clkbrd_softc),
90 DRIVER_MODULE(clkbrd, fhc, clkbrd_driver, clkbrd_devclass, 0, 0);
93 clkbrd_probe(device_t dev)
96 if (strcmp(ofw_bus_get_name(dev), "clock-board") == 0) {
97 device_set_desc(dev, "Clock Board");
104 clkbrd_attach(device_t dev)
106 struct clkbrd_softc *sc;
110 sc = device_get_softc(dev);
113 for (i = CLKBRD_CF; i <= CLKBRD_CLKVER; i++) {
115 sc->sc_res[i] = bus_alloc_resource_any(sc->sc_dev,
116 SYS_RES_MEMORY, &sc->sc_rid[i], RF_ACTIVE);
117 if (sc->sc_res[i] == NULL) {
118 if (i != CLKBRD_CLKVER) {
119 device_printf(sc->sc_dev,
120 "could not allocate resource %d\n", i);
125 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
126 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
127 if (i == CLKBRD_CLKVER)
128 sc->sc_flags |= CLKBRD_HAS_CLKVER;
132 r = bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
134 switch (r & CLK_STS1_SLOTS_MASK) {
135 case CLK_STS1_SLOTS_16:
138 case CLK_STS1_SLOTS_8:
141 case CLK_STS1_SLOTS_4:
142 if (sc->sc_flags & CLKBRD_HAS_CLKVER) {
143 r = bus_space_read_1(sc->sc_bt[CLKBRD_CLKVER],
144 sc->sc_bh[CLKBRD_CLKVER], CLKVER_SLOTS);
146 (r & CLKVER_SLOTS_MASK) == CLKVER_SLOTS_PLUS)
151 device_printf(sc->sc_dev, "Sun Enterprise Exx00 machine: %d slots\n",
154 sc->sc_clk_ctrl = bus_space_read_1(sc->sc_bt[CLKBRD_CLK],
155 sc->sc_bh[CLKBRD_CLK], CLK_CTRL);
156 sc->sc_led_dev = led_create(clkbrd_led_func, sc, "clockboard");
161 clkbrd_free_resources(sc);
167 clkbrd_detach(device_t dev)
169 struct clkbrd_softc *sc;
171 sc = device_get_softc(dev);
173 led_destroy(sc->sc_led_dev);
174 bus_space_write_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
175 CLK_CTRL, sc->sc_clk_ctrl);
176 bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
178 clkbrd_free_resources(sc);
184 clkbrd_free_resources(struct clkbrd_softc *sc)
188 for (i = CLKBRD_CF; i <= CLKBRD_CLKVER; i++)
189 if (sc->sc_res[i] != NULL)
190 bus_release_resource(sc->sc_dev, SYS_RES_MEMORY,
191 sc->sc_rid[i], sc->sc_res[i]);
195 clkbrd_led_func(void *arg, int onoff)
197 struct clkbrd_softc *sc;
200 sc = (struct clkbrd_softc *)arg;
202 r = bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
208 bus_space_write_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
210 bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],