]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/fhc/clkbrd.c
MFV r339640,339641,339644:
[FreeBSD/FreeBSD.git] / sys / sparc64 / fhc / clkbrd.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
5  * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  *      from: OpenBSD: clkbrd.c,v 1.5 2004/10/01 18:18:49 jason Exp
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/resource.h>
40 #include <sys/rman.h>
41
42 #include <dev/led/led.h>
43 #include <dev/ofw/ofw_bus.h>
44
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47
48 #include <sparc64/fhc/clkbrdreg.h>
49
50 #define CLKBRD_NREG     3
51
52 #define CLKBRD_CF       0
53 #define CLKBRD_CLK      1
54 #define CLKBRD_CLKVER   2
55
56 struct clkbrd_softc {
57         device_t                sc_dev;
58         struct resource         *sc_res[CLKBRD_NREG];
59         int                     sc_rid[CLKBRD_NREG];
60         bus_space_tag_t         sc_bt[CLKBRD_NREG];
61         bus_space_handle_t      sc_bh[CLKBRD_NREG];
62         uint8_t                 sc_clk_ctrl;
63         struct cdev             *sc_led_dev;
64         int                     sc_flags;
65 #define CLKBRD_HAS_CLKVER       (1 << 0)
66 };
67
68 static devclass_t clkbrd_devclass;
69
70 static device_probe_t clkbrd_probe;
71 static device_attach_t clkbrd_attach;
72 static device_detach_t clkbrd_detach;
73
74 static void clkbrd_free_resources(struct clkbrd_softc *);
75 static void clkbrd_led_func(void *, int);
76
77 static device_method_t clkbrd_methods[] = {
78         /* Device interface */
79         DEVMETHOD(device_probe,         clkbrd_probe),
80         DEVMETHOD(device_attach,        clkbrd_attach),
81         DEVMETHOD(device_detach,        clkbrd_detach),
82
83         { 0, 0 }
84 };
85
86 static driver_t clkbrd_driver = {
87         "clkbrd",
88         clkbrd_methods,
89         sizeof(struct clkbrd_softc),
90 };
91
92 DRIVER_MODULE(clkbrd, fhc, clkbrd_driver, clkbrd_devclass, 0, 0);
93
94 static int
95 clkbrd_probe(device_t dev)
96 {
97
98         if (strcmp(ofw_bus_get_name(dev), "clock-board") == 0) {
99                 device_set_desc(dev, "Clock Board");
100                 return (0);
101         }
102         return (ENXIO);
103 }
104
105 static int
106 clkbrd_attach(device_t dev)
107 {
108         struct clkbrd_softc *sc;
109         int i, slots;
110         uint8_t r;
111
112         sc = device_get_softc(dev);
113         sc->sc_dev = dev;
114
115         for (i = CLKBRD_CF; i <= CLKBRD_CLKVER; i++) {
116                 sc->sc_rid[i] = i;
117                 sc->sc_res[i] = bus_alloc_resource_any(sc->sc_dev,
118                     SYS_RES_MEMORY, &sc->sc_rid[i], RF_ACTIVE);
119                 if (sc->sc_res[i] == NULL) {
120                         if (i != CLKBRD_CLKVER) {
121                                 device_printf(sc->sc_dev,
122                                     "could not allocate resource %d\n", i);
123                                 goto fail;
124                         }
125                         continue;
126                 }
127                 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
128                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
129                 if (i == CLKBRD_CLKVER)
130                         sc->sc_flags |= CLKBRD_HAS_CLKVER;
131         }
132
133         slots = 4;
134         r = bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
135             CLK_STS1);
136         switch (r & CLK_STS1_SLOTS_MASK) {
137         case CLK_STS1_SLOTS_16:
138                 slots = 16;
139                 break;
140         case CLK_STS1_SLOTS_8:
141                 slots = 8;
142                 break;
143         case CLK_STS1_SLOTS_4:
144                 if (sc->sc_flags & CLKBRD_HAS_CLKVER) {
145                         r = bus_space_read_1(sc->sc_bt[CLKBRD_CLKVER],
146                             sc->sc_bh[CLKBRD_CLKVER], CLKVER_SLOTS);
147                         if (r != 0 &&
148                             (r & CLKVER_SLOTS_MASK) == CLKVER_SLOTS_PLUS)
149                                 slots = 5;
150                 }
151         }
152
153         device_printf(sc->sc_dev, "Sun Enterprise Exx00 machine: %d slots\n",
154             slots);
155
156         sc->sc_clk_ctrl = bus_space_read_1(sc->sc_bt[CLKBRD_CLK],
157             sc->sc_bh[CLKBRD_CLK], CLK_CTRL);
158         sc->sc_led_dev = led_create(clkbrd_led_func, sc, "clockboard");
159
160         return (0);
161
162  fail:
163         clkbrd_free_resources(sc);
164
165         return (ENXIO);
166 }
167
168 static int
169 clkbrd_detach(device_t dev)
170 {
171         struct clkbrd_softc *sc;
172
173         sc = device_get_softc(dev);
174
175         led_destroy(sc->sc_led_dev);
176         bus_space_write_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
177             CLK_CTRL, sc->sc_clk_ctrl);
178         bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
179             CLK_CTRL);
180         clkbrd_free_resources(sc);
181
182         return (0);
183 }
184
185 static void
186 clkbrd_free_resources(struct clkbrd_softc *sc)
187 {
188         int i;
189
190         for (i = CLKBRD_CF; i <= CLKBRD_CLKVER; i++)
191                 if (sc->sc_res[i] != NULL)
192                         bus_release_resource(sc->sc_dev, SYS_RES_MEMORY,
193                             sc->sc_rid[i], sc->sc_res[i]);
194 }
195
196 static void
197 clkbrd_led_func(void *arg, int onoff)
198 {
199         struct clkbrd_softc *sc;
200         uint8_t r;
201
202         sc = (struct clkbrd_softc *)arg;
203
204         r = bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
205             CLK_CTRL);
206         if (onoff)
207                 r |= CLK_CTRL_RLED;
208         else
209                 r &= ~CLK_CTRL_RLED;
210         bus_space_write_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
211             CLK_CTRL, r);
212         bus_space_read_1(sc->sc_bt[CLKBRD_CLK], sc->sc_bh[CLKBRD_CLK],
213             CLK_CTRL);
214 }