summaryrefslogtreecommitdiff
path: root/sys/dev/eisa/cac_eisa.c
blob: 6d77c21db4149cd49ca478bd5c5661dd69470322 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*	$NetBSD: cac_eisa.c,v 1.26 2021/01/27 04:35:15 thorpej Exp $	*/

/*-
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

/*
 * Copyright (c) 2000 Jonathan Lemon
 * Copyright (c) 1999 by Matthew N. Dodd <winter@jurai.net>
 * 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,
 *    without modification, immediately at the beginning of the file.
 * 2. 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 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.
 */

/*
 * EISA front-end for cac(4) driver.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cac_eisa.c,v 1.26 2021/01/27 04:35:15 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/intr.h>

#include <dev/eisa/eisavar.h>
#include <dev/eisa/eisadevs.h>

#include <dev/ic/cacreg.h>
#include <dev/ic/cacvar.h>

#include "ioconf.h"

#define CAC_EISA_SLOT_OFFSET		0x0c88
#define CAC_EISA_IOSIZE			0x0017
#define CAC_EISA_IOCONF			0x38

static void	cac_eisa_attach(device_t, device_t, void *);
static int	cac_eisa_match(device_t, cfdata_t, void *);

static struct	cac_ccb *cac_eisa_l0_completed(struct cac_softc *);
static int	cac_eisa_l0_fifo_full(struct cac_softc *);
static void	cac_eisa_l0_intr_enable(struct cac_softc *, int);
static int	cac_eisa_l0_intr_pending(struct cac_softc *);
static void	cac_eisa_l0_submit(struct cac_softc *, struct cac_ccb *);

CFATTACH_DECL3_NEW(cac_eisa, sizeof(struct cac_softc),
    cac_eisa_match, cac_eisa_attach, NULL, NULL, cac_rescan, NULL, 0);

static const struct cac_linkage cac_eisa_l0 = {
	cac_eisa_l0_completed,
	cac_eisa_l0_fifo_full,
	cac_eisa_l0_intr_enable,
	cac_eisa_l0_intr_pending,
	cac_eisa_l0_submit
};

struct cac_eisa_type {
	const char	*ct_typestr;
	const struct	cac_linkage *ct_linkage;
};

static const struct cac_eisa_type cpq4001 = {
	.ct_typestr = "IDA",
	.ct_linkage = &cac_eisa_l0
};

static const struct cac_eisa_type cpq4002 = {
	.ct_typestr = "IDA-2",
	.ct_linkage = &cac_eisa_l0
};

static const struct cac_eisa_type cpq4010 = {
	.ct_typestr = "IEAS",
	.ct_linkage = &cac_eisa_l0
};

static const struct cac_eisa_type cpq4020 = {
	.ct_typestr = "SMART",
	.ct_linkage = &cac_eisa_l0
};

static const struct cac_eisa_type cpq4030 = {
	.ct_typestr = "SMART-2/E",
	.ct_linkage = &cac_l0
};

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "CPQ4001",	.data = &cpq4001 },
	{ .compat = "CPQ4002",	.data = &cpq4002 },
	{ .compat = "CPQ4010",	.data = &cpq4010 },
	{ .compat = "CPQ4020",	.data = &cpq4020 },
	{ .compat = "CPQ4030",	.data = &cpq4030 },
	DEVICE_COMPAT_EOL
};

static int
cac_eisa_match(device_t parent, cfdata_t match, void *aux)
{
	struct eisa_attach_args *ea = aux;

	return (eisa_compatible_match(ea, compat_data));
}

static void
cac_eisa_attach(device_t parent, device_t self, void *aux)
{
	struct eisa_attach_args *ea = aux;
	const struct device_compatible_entry *dce;
	const struct cac_eisa_type *ct;
	bus_space_handle_t ioh;
	eisa_chipset_tag_t ec;
	eisa_intr_handle_t ih;
	struct cac_softc *sc;
	bus_space_tag_t iot;
	const char *intrstr;
	int irq;
	char intrbuf[EISA_INTRSTR_LEN];

	sc = device_private(self);
	iot = ea->ea_iot;
	ec = ea->ea_ec;

	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
	    CAC_EISA_SLOT_OFFSET, CAC_EISA_IOSIZE, 0, &ioh)) {
		aprint_error(": can't map i/o space\n");
		return;
	}

	dce = eisa_compatible_lookup(ea, compat_data);
	KASSERT(dce != NULL);
	ct = dce->data;

	sc->sc_dev = self;
	sc->sc_iot = iot;
	sc->sc_ioh = ioh;
	sc->sc_dmat = ea->ea_dmat;

	/*
	 * Map and establish the interrupt.
	 */
	switch (bus_space_read_1(iot, ioh, CAC_EISA_IOCONF) & 0xf0) {
	case 0x20:
		irq = 10;
		break;
	case 0x10:
		irq = 11;
		break;
	case 0x40:
		irq = 14;
		break;
	case 0x80:
		irq = 15;
		break;
	default:
		aprint_error(": controller on invalid IRQ\n");
		return;
	}

	if (eisa_intr_map(ec, irq, &ih)) {
		aprint_error(": can't map interrupt (%d)\n", irq);
		return;
	}

	intrstr = eisa_intr_string(ec, ih, intrbuf, sizeof(intrbuf));
	if ((sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
	    cac_intr, sc)) == NULL) {
		aprint_error(": can't establish interrupt");
		if (intrstr != NULL)
			aprint_normal(" at %s", intrstr);
		aprint_normal("\n");
		return;
	}

	/*
	 * Print board type and attach to the bus-independent code.
	 */
	aprint_normal(": Compaq %s\n", ct->ct_typestr);
	memcpy(&sc->sc_cl, ct->ct_linkage, sizeof(sc->sc_cl));
	cac_init(sc, intrstr, 0);
}

/*
 * Linkage specific to EISA boards.
 */

static int
cac_eisa_l0_fifo_full(struct cac_softc *sc)
{

	return ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
	    CAC_EISA_CHANNEL_CLEAR) == 0);
}

static void
cac_eisa_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
{
	u_int16_t size;

	/*
	 * On these boards, `ccb_hdr.size' is actually for control flags.
	 * Set it to zero and pass the value by means of an I/O port.
	 */
	size = le16toh(ccb->ccb_hdr.size) << 2;
	ccb->ccb_hdr.size = 0;

	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
	    (char *)ccb - (char *)sc->sc_ccbs,
	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
	cac_outl(sc, CAC_EISAREG_LIST_ADDR, ccb->ccb_paddr);
	cac_outw(sc, CAC_EISAREG_LIST_LEN, size);
	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_BUSY);
}

static struct cac_ccb *
cac_eisa_l0_completed(struct cac_softc *sc)
{
	struct cac_ccb *ccb;
	u_int32_t off;
	u_int8_t status;

	if ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
	    CAC_EISA_CHANNEL_BUSY) == 0)
		return (NULL);

	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_BUSY);
	off = cac_inl(sc, CAC_EISAREG_COMPLETE_ADDR);
	status = cac_inb(sc, CAC_EISAREG_LIST_STATUS);
	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_CLEAR);

	if (off == 0)
		return (NULL);

	off = (off & ~3) - sc->sc_ccbs_paddr;
	ccb = (struct cac_ccb *)((char *)sc->sc_ccbs + off);

	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);

	ccb->ccb_req.error = status;

	if ((off & 3) != 0 && ccb->ccb_req.error == 0)
		ccb->ccb_req.error = CAC_RET_CMD_REJECTED;

	return (ccb);
}

static int
cac_eisa_l0_intr_pending(struct cac_softc *sc)
{

	return (cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
	    CAC_EISA_CHANNEL_BUSY);
}

static void
cac_eisa_l0_intr_enable(struct cac_softc *sc, int state)
{

	if (state) {
		cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL,
		    ~CAC_EISA_CHANNEL_CLEAR);
		cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL,
		    CAC_EISA_CHANNEL_BUSY);
		cac_outb(sc, CAC_EISAREG_INTR_MASK, CAC_INTR_ENABLE);
		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_ENABLE);
	} else
		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_DISABLE);
}

MODULE(MODULE_CLASS_DRIVER, cac_eisa, "cac");	/* No eisa module yet! */

#ifdef _MODULE
/*
 * XXX Don't allow ioconf.c to redefine the "struct cfdriver cac_cd"
 * XXX it will be defined in the common-code module
 */
#undef  CFDRIVER_DECL
#define CFDRIVER_DECL(name, class, attr)
#include "ioconf.c"
#endif
 
static int
cac_eisa_modcmd(modcmd_t cmd, void *opaque)
{
	int error = 0;
 
#ifdef _MODULE
	switch (cmd) {
	case MODULE_CMD_INIT:
		/* 
		 * We skip over the first entry in cfdriver[] array
		 * since the cfdriver is attached by the common
		 * (non-attachment-specific) code.
		 */
		error = config_init_component(&cfdriver_ioconf_cac_eisa[1],
		    cfattach_ioconf_cac_eisa, cfdata_ioconf_cac_eisa);
		break;
	case MODULE_CMD_FINI:
		error = config_fini_component(&cfdriver_ioconf_cac_eisa[1],  
		    cfattach_ioconf_cac_eisa, cfdata_ioconf_cac_eisa);
		break;
	default:
		error = ENOTTY;
		break;
	}
#endif

	return error;
}