summaryrefslogtreecommitdiffstats
path: root/sys/dev/auxio/auxio.c
blob: b104d5af5ee9833880beb3bbfc7b6a0279019a51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*-
 * Copyright (c) 2004 Pyun YongHyeon
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/*	$NetBSD: auxio.c,v 1.11 2003/07/15 03:36:04 lukem Exp $	*/

/*-
 * Copyright (c) 2000, 2001 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * AUXIO registers support on the SBus & EBus2, used for the floppy driver
 * and to control the system LED, for the BLINK option.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/resource.h>
#include <sys/rman.h>

#include <dev/led/led.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/openfirm.h>

#include <machine/bus.h>
#include <machine/ofw_machdep.h>
#include <machine/resource.h>

#include <sparc64/sbus/sbusvar.h>
#include <dev/auxio/auxioreg.h>

/*
 * On sun4u, auxio exists with one register (LED) on the SBus, and 5
 * registers on the EBus2 (pci) (LED, PCIMODE, FREQUENCY, SCSI
 * OSCILLATOR, and TEMP SENSE.
 */

#define AUXIO_PCIO_LED	0
#define AUXIO_PCIO_PCI	1
#define AUXIO_PCIO_FREQ	2
#define AUXIO_PCIO_OSC	3
#define AUXIO_PCIO_TEMP	4
#define AUXIO_PCIO_NREG	5

struct auxio_softc {
	struct device		*sc_dev;

	int			sc_nauxio;
	struct resource		*sc_res[AUXIO_PCIO_NREG];
	int			sc_rid[AUXIO_PCIO_NREG];
	bus_space_tag_t		sc_regt[AUXIO_PCIO_NREG];
	bus_space_handle_t	sc_regh[AUXIO_PCIO_NREG];
	struct cdev		*sc_led_dev;
	u_int32_t		sc_led_stat;

	int			sc_flags;
#define	AUXIO_LEDONLY		0x1
#define	AUXIO_EBUS		0x2
#define	AUXIO_SBUS		0x4

	struct mtx		sc_lock;
};

static void	auxio_led_func(void *arg, int onoff);
static int	auxio_attach_common(struct auxio_softc *);
static int	auxio_bus_probe(device_t);
static int	auxio_sbus_attach(device_t);
static int	auxio_ebus_attach(device_t);
static int	auxio_bus_detach(device_t);
static void	auxio_free_resource(struct auxio_softc *);
static __inline u_int32_t auxio_led_read(struct auxio_softc *);
static __inline void auxio_led_write(struct auxio_softc *, u_int32_t);

/* SBus */
static device_method_t auxio_sbus_methods[] = {
	DEVMETHOD(device_probe,		auxio_bus_probe),
	DEVMETHOD(device_attach,	auxio_sbus_attach),
	DEVMETHOD(device_detach,	auxio_bus_detach),

	DEVMETHOD_END
};

static driver_t auxio_sbus_driver = {
	"auxio",
	auxio_sbus_methods,
	sizeof(struct auxio_softc)
};

static devclass_t	auxio_devclass;
/* The probe order is handled by sbus(4). */
EARLY_DRIVER_MODULE(auxio, sbus, auxio_sbus_driver, auxio_devclass, 0, 0,
    BUS_PASS_DEFAULT);
MODULE_DEPEND(auxio, sbus, 1, 1, 1);

/* EBus */
static device_method_t auxio_ebus_methods[] = {
	DEVMETHOD(device_probe,		auxio_bus_probe),
	DEVMETHOD(device_attach,	auxio_ebus_attach),
	DEVMETHOD(device_detach,	auxio_bus_detach),

	DEVMETHOD_END
};

static driver_t auxio_ebus_driver = {
	"auxio",
	auxio_ebus_methods,
	sizeof(struct auxio_softc)
};

EARLY_DRIVER_MODULE(auxio, ebus, auxio_ebus_driver, auxio_devclass, 0, 0,
    BUS_PASS_DEFAULT);
MODULE_DEPEND(auxio, ebus, 1, 1, 1);
MODULE_VERSION(auxio, 1);

#define AUXIO_LOCK_INIT(sc)	\
	mtx_init(&sc->sc_lock, "auxio mtx", NULL, MTX_DEF)
#define AUXIO_LOCK(sc)		mtx_lock(&sc->sc_lock)
#define AUXIO_UNLOCK(sc)	mtx_unlock(&sc->sc_lock)
#define AUXIO_LOCK_DESTROY(sc)	mtx_destroy(&sc->sc_lock)

static __inline void
auxio_led_write(struct auxio_softc *sc, u_int32_t v)
{
	if (sc->sc_flags & AUXIO_EBUS)
		bus_space_write_4(sc->sc_regt[AUXIO_PCIO_LED],
		    sc->sc_regh[AUXIO_PCIO_LED], 0, v);
	else
		bus_space_write_1(sc->sc_regt[AUXIO_PCIO_LED],
		    sc->sc_regh[AUXIO_PCIO_LED], 0, v);
}

static __inline u_int32_t
auxio_led_read(struct auxio_softc *sc)
{
	u_int32_t led;

	if (sc->sc_flags & AUXIO_EBUS)
		led = bus_space_read_4(sc->sc_regt[AUXIO_PCIO_LED],
		    sc->sc_regh[AUXIO_PCIO_LED], 0);
	else
		led = bus_space_read_1(sc->sc_regt[AUXIO_PCIO_LED],
		    sc->sc_regh[AUXIO_PCIO_LED], 0);

	return (led);
}

static void
auxio_led_func(void *arg, int onoff)
{
	struct auxio_softc *sc;
	u_int32_t led;

	sc = (struct auxio_softc *)arg;

	AUXIO_LOCK(sc);
	/*
	 * NB: We must not touch the other bits of the SBus AUXIO reg.
	 */
	led = auxio_led_read(sc);
	if (onoff)
		led |= AUXIO_LED_LED;
	else
		led &= ~AUXIO_LED_LED;
	auxio_led_write(sc, led);
	AUXIO_UNLOCK(sc);
}

static int
auxio_bus_probe(device_t dev)
{
	const char *name;

	name = ofw_bus_get_name(dev);
	if (strcmp("auxio", name) == 0) {
		device_set_desc(dev, "Sun Auxiliary I/O");
		return (0);
	}

	return (ENXIO);
}

static int
auxio_ebus_attach(device_t dev)
{
	struct auxio_softc *sc;

	sc = device_get_softc(dev);
	sc->sc_dev = dev;

	AUXIO_LOCK_INIT(sc);
	sc->sc_nauxio = AUXIO_PCIO_NREG;
	sc->sc_flags = AUXIO_LEDONLY | AUXIO_EBUS;

	return(auxio_attach_common(sc));
}

static int
auxio_attach_common(struct auxio_softc *sc)
{
	struct resource *res;
	int i;

	for (i = 0; i < sc->sc_nauxio; i++) {
		sc->sc_rid[i] = i;
		res = bus_alloc_resource_any(sc->sc_dev, SYS_RES_MEMORY,
		    &sc->sc_rid[i], RF_ACTIVE);
		if (res == NULL) {
			device_printf(sc->sc_dev,
			    "could not allocate resources\n");
			goto attach_fail;
		}
		sc->sc_res[i] = res;
		sc->sc_regt[i] = rman_get_bustag(res);
		sc->sc_regh[i] = rman_get_bushandle(res);
	}

	sc->sc_led_stat = auxio_led_read(sc) & AUXIO_LED_LED;
	sc->sc_led_dev = led_create(auxio_led_func, sc, "auxioled");
	/* turn on the LED */
	auxio_led_func(sc, 1);

	return (0);

attach_fail:
	auxio_free_resource(sc);

	return (ENXIO);
}

static int
auxio_bus_detach(device_t dev)
{
	struct auxio_softc *sc;

	sc = device_get_softc(dev);
	led_destroy(sc->sc_led_dev);
	auxio_led_func(sc, sc->sc_led_stat);
	auxio_free_resource(sc);

	return (0);
}

static void
auxio_free_resource(struct auxio_softc *sc)
{
	int i;

	for (i = 0; i < sc->sc_nauxio; i++)
		if (sc->sc_res[i])
			bus_release_resource(sc->sc_dev, SYS_RES_MEMORY,
			    sc->sc_rid[i], sc->sc_res[i]);
	AUXIO_LOCK_DESTROY(sc);
}

static int
auxio_sbus_attach(device_t dev)
{
	struct auxio_softc *sc;

	sc = device_get_softc(dev);
	sc->sc_dev = dev;

	AUXIO_LOCK_INIT(sc);
	sc->sc_nauxio = 1;
	sc->sc_flags = AUXIO_LEDONLY | AUXIO_SBUS;

	return (auxio_attach_common(sc));
}
OpenPOWER on IntegriCloud