gusc.c revision 62947
1/*-
2 * Copyright (c) 1999 Seigo Tanimura
3 * Copyright (c) 1999 Ville-Pertti Keinonen
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/sound/isa/gusc.c 62947 2000-07-11 11:49:33Z tanimura $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/bus.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36#include <machine/resource.h>
37#include <machine/bus.h>
38#include <machine/clock.h>
39#include <sys/rman.h>
40#include <sys/soundcard.h>
41#include <dev/sound/pcm/sound.h>
42#include <dev/sound/chip.h>
43#include "bus_if.h"
44
45#include <isa/isavar.h>
46#include <isa/isa_common.h>
47#ifdef __alpha__		/* XXX workaround a stupid warning */
48#include <alpha/isa/isavar.h>
49#endif
50
51#define LOGICALID_NOPNP 0
52#define LOGICALID_PCM   0x0000561e
53#define LOGICALID_OPL   0x0300561e
54#define LOGICALID_MIDI  0x0400561e
55
56/* PnP IDs */
57static struct isa_pnp_id gusc_ids[] = {
58	{LOGICALID_PCM,  "GRV0000 Gravis UltraSound PnP PCM"},	/* GRV0000 */
59	{LOGICALID_OPL,  "GRV0003 Gravis UltraSound PnP OPL"},	/* GRV0003 */
60	{LOGICALID_MIDI, "GRV0004 Gravis UltraSound PnP MIDI"},	/* GRV0004 */
61};
62
63/* Interrupt handler.  */
64struct gusc_ihandler {
65	void (*intr)(void *);
66	void *arg;
67};
68
69/* Here is the parameter structure per a device. */
70struct gusc_softc {
71	device_t dev; /* device */
72	int io_rid[3]; /* io port rids */
73	struct resource *io[3]; /* io port resources */
74	int io_alloced[3]; /* io port alloc flag */
75	int irq_rid; /* irq rids */
76	struct resource *irq; /* irq resources */
77	int irq_alloced; /* irq alloc flag */
78	int drq_rid[2]; /* drq rids */
79	struct resource *drq[2]; /* drq resources */
80	int drq_alloced[2]; /* drq alloc flag */
81
82	/* Interrupts are shared (XXX non-PnP only?) */
83	struct gusc_ihandler midi_intr;
84	struct gusc_ihandler pcm_intr;
85};
86
87typedef struct gusc_softc *sc_p;
88
89static int gusc_probe(device_t dev);
90static int gusc_attach(device_t dev);
91static int gusisa_probe(device_t dev);
92static void gusc_intr(void *);
93static struct resource *gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
94					      u_long start, u_long end, u_long count, u_int flags);
95static int gusc_release_resource(device_t bus, device_t child, int type, int rid,
96				   struct resource *r);
97
98static device_t find_masterdev(sc_p scp);
99static int alloc_resource(sc_p scp);
100static int release_resource(sc_p scp);
101
102static devclass_t gusc_devclass;
103
104static int
105gusc_probe(device_t dev)
106{
107	device_t child;
108	u_int32_t logical_id;
109	char *s;
110	struct sndcard_func *func;
111	int ret;
112
113	logical_id = isa_get_logicalid(dev);
114	s = NULL;
115
116	/* Check isapnp ids */
117	if (logical_id != 0 && (ret = ISA_PNP_PROBE(device_get_parent(dev), dev, gusc_ids)) != 0)
118		return (ret);
119	else {
120		if (logical_id == 0)
121			return gusisa_probe(dev);
122	}
123
124	switch (logical_id) {
125	case LOGICALID_PCM:
126		s = "Gravis UltraSound Plug & Play PCM";
127		func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
128		if (func == NULL)
129			return (ENOMEM);
130		bzero(func, sizeof(*func));
131		func->func = SCF_PCM;
132		child = device_add_child(dev, "pcm", -1);
133		device_set_ivars(child, func);
134		break;
135	case LOGICALID_OPL:
136		s = "Gravis UltraSound Plug & Play OPL";
137		func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
138		if (func == NULL)
139			return (ENOMEM);
140		bzero(func, sizeof(*func));
141		func->func = SCF_SYNTH;
142		child = device_add_child(dev, "midi", -1);
143		device_set_ivars(child, func);
144		break;
145	case LOGICALID_MIDI:
146		s = "Gravis UltraSound Plug & Play MIDI";
147		func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
148		if (func == NULL)
149			return (ENOMEM);
150		bzero(func, sizeof(*func));
151		func->func = SCF_MIDI;
152		child = device_add_child(dev, "midi", -1);
153		device_set_ivars(child, func);
154		break;
155	}
156
157	if (s != NULL) {
158		device_set_desc(dev, s);
159		return (0);
160	}
161
162	return (ENXIO);
163}
164
165static void
166port_wr(struct resource *r, int i, unsigned char v)
167{
168	bus_space_write_1(rman_get_bustag(r), rman_get_bushandle(r), i, v);
169}
170
171static int
172port_rd(struct resource *r, int i)
173{
174	return bus_space_read_1(rman_get_bustag(r), rman_get_bushandle(r), i);
175}
176
177/*
178 * Probe for an old (non-PnP) GUS card on the ISA bus.
179 */
180
181static int
182gusisa_probe(device_t dev)
183{
184	device_t child;
185	struct resource *res, *res2;
186	int base, rid, rid2, s, flags;
187	unsigned char val;
188
189	base = isa_get_port(dev);
190	flags = device_get_flags(dev);
191	rid = 1;
192	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x100,
193				 base + 0x107, 8, RF_ACTIVE);
194
195	if (res == NULL)
196		return ENXIO;
197
198	res2 = NULL;
199
200	/*
201	 * Check for the presence of some GUS card.  Reset the card,
202	 * then see if we can access the memory on it.
203	 */
204
205	port_wr(res, 3, 0x4c);
206	port_wr(res, 5, 0);
207	DELAY(30 * 1000);
208
209	port_wr(res, 3, 0x4c);
210	port_wr(res, 5, 1);
211	DELAY(30 * 1000);
212
213	s = splhigh();
214
215	/* Write to DRAM.  */
216
217	port_wr(res, 3, 0x43);		/* Register select */
218	port_wr(res, 4, 0);		/* Low addr */
219	port_wr(res, 5, 0);		/* Med addr */
220
221	port_wr(res, 3, 0x44);		/* Register select */
222	port_wr(res, 4, 0);		/* High addr */
223	port_wr(res, 7, 0x55);		/* DRAM */
224
225	/* Read from DRAM.  */
226
227	port_wr(res, 3, 0x43);		/* Register select */
228	port_wr(res, 4, 0);		/* Low addr */
229	port_wr(res, 5, 0);		/* Med addr */
230
231	port_wr(res, 3, 0x44);		/* Register select */
232	port_wr(res, 4, 0);		/* High addr */
233	val = port_rd(res, 7);		/* DRAM */
234
235	splx(s);
236
237	if (val != 0x55)
238		goto fail;
239
240	rid2 = 0;
241	res2 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid2, base, base, 1,
242				  RF_ACTIVE);
243
244	if (res2 == NULL)
245		goto fail;
246
247	s = splhigh();
248	port_wr(res2, 0x0f, 0x20);
249	val = port_rd(res2, 0x0f);
250	splx(s);
251
252	if (val == 0xff || (val & 0x06) == 0)
253		val = 0;
254	else {
255		val = port_rd(res2, 0x506);	/* XXX Out of range.  */
256		if (val == 0xff)
257			val = 0;
258	}
259
260	bus_release_resource(dev, SYS_RES_IOPORT, rid2, res2);
261	bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
262
263	if (val >= 10) {
264		struct sndcard_func *func;
265
266		/* Looks like a GUS MAX.  Set the rest of the resources.  */
267
268		bus_set_resource(dev, SYS_RES_IOPORT, 2, base + 0x10c, 8);
269
270		if (flags & DV_F_DUAL_DMA)
271			bus_set_resource(dev, SYS_RES_DRQ, 1,
272					 flags & DV_F_DRQ_MASK, 1);
273
274		/* We can support the CS4231 and MIDI devices.  */
275
276		func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
277		if (func == NULL)
278			return ENOMEM;
279		bzero(func, sizeof *func);
280		func->func = SCF_MIDI;
281		child = device_add_child(dev, "midi", -1);
282		device_set_ivars(child, func);
283
284		func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
285		if (func == NULL)
286			printf("xxx: gus pcm not attached, out of memory\n");
287		else {
288			bzero(func, sizeof *func);
289			func->func = SCF_PCM;
290			child = device_add_child(dev, "pcm", -1);
291			device_set_ivars(child, func);
292		}
293		device_set_desc(dev, "Gravis UltraSound MAX");
294		return 0;
295	} else {
296
297		/*
298		 * TODO: Support even older GUS cards.  MIDI should work on
299		 * all models.
300		 */
301		return ENXIO;
302	}
303
304fail:
305	bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
306	return ENXIO;
307}
308
309static int
310gusc_attach(device_t dev)
311{
312	sc_p scp;
313	int unit;
314	void *ih;
315
316	scp = device_get_softc(dev);
317	unit = device_get_unit(dev);
318
319	bzero(scp, sizeof(*scp));
320
321	scp->dev = dev;
322	if (alloc_resource(scp)) {
323		release_resource(scp);
324		return (ENXIO);
325	}
326
327	bus_setup_intr(dev, scp->irq, INTR_TYPE_TTY, gusc_intr, scp, &ih);
328	bus_generic_attach(dev);
329
330	return (0);
331}
332
333/*
334 * Handle interrupts on GUS devices until there aren't any left.
335 */
336static void
337gusc_intr(void *arg)
338{
339	sc_p scp = (sc_p)arg;
340	int did_something;
341
342	do {
343		did_something = 0;
344		if (scp->pcm_intr.intr != NULL &&
345		    (port_rd(scp->io[2], 2) & 1)) {
346			(*scp->pcm_intr.intr)(scp->pcm_intr.arg);
347			did_something = 1;
348		}
349		if (scp->midi_intr.intr != NULL &&
350		    (port_rd(scp->io[1], 0) & 0x80)) {
351			(*scp->midi_intr.intr)(scp->midi_intr.arg);
352			did_something = 1;
353		}
354	} while (did_something != 0);
355}
356
357static struct resource *
358gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
359		      u_long start, u_long end, u_long count, u_int flags)
360{
361	sc_p scp;
362	int *alloced, rid_max, alloced_max;
363	struct resource **res;
364
365	scp = device_get_softc(bus);
366	switch (type) {
367	case SYS_RES_IOPORT:
368		alloced = scp->io_alloced;
369		res = scp->io;
370		rid_max = 2;
371		alloced_max = 2; /* pcm + midi (more to include synth) */
372		break;
373	case SYS_RES_IRQ:
374		alloced = &scp->irq_alloced;
375		res = &scp->irq;
376		rid_max = 0;
377		alloced_max = 2; /* pcm and midi share the single irq. */
378		break;
379	case SYS_RES_DRQ:
380		alloced = scp->drq_alloced;
381		res = scp->drq;
382		rid_max = 1;
383		alloced_max = 1;
384		break;
385	default:
386		return (NULL);
387	}
388
389	if (*rid > rid_max || alloced[*rid] == alloced_max)
390		return (NULL);
391
392	alloced[*rid]++;
393	return (res[*rid]);
394}
395
396static int
397gusc_release_resource(device_t bus, device_t child, int type, int rid,
398			struct resource *r)
399{
400	sc_p scp;
401	int *alloced, rid_max;
402
403	scp = device_get_softc(bus);
404	switch (type) {
405	case SYS_RES_IOPORT:
406		alloced = scp->io_alloced;
407		rid_max = 2;
408		break;
409	case SYS_RES_IRQ:
410		alloced = &scp->irq_alloced;
411		rid_max = 0;
412		break;
413	case SYS_RES_DRQ:
414		alloced = scp->drq_alloced;
415		rid_max = 1;
416		break;
417	default:
418		return (1);
419	}
420
421	if (rid > rid_max || alloced[rid] == 0)
422		return (1);
423
424	alloced[rid]--;
425	return (0);
426}
427
428static int
429gusc_setup_intr(device_t dev, device_t child, struct resource *irq,
430		int flags, driver_intr_t *intr, void *arg, void **cookiep)
431{
432	sc_p scp = (sc_p)device_get_softc(dev);
433	devclass_t devclass;
434
435	devclass = device_get_devclass(child);
436	if (strcmp(devclass_get_name(devclass), "midi") == 0) {
437		scp->midi_intr.intr = intr;
438		scp->midi_intr.arg = arg;
439		return 0;
440	} else if (strcmp(devclass_get_name(devclass), "pcm") == 0) {
441		scp->pcm_intr.intr = intr;
442		scp->pcm_intr.arg = arg;
443		return 0;
444	}
445	return bus_generic_setup_intr(dev, child, irq, flags, intr,
446				      arg, cookiep);
447}
448
449static device_t
450find_masterdev(sc_p scp)
451{
452	int i, units;
453	devclass_t devclass;
454	device_t dev;
455
456	devclass = device_get_devclass(scp->dev);
457	units = devclass_get_maxunit(devclass);
458	dev = NULL;
459	for (i = 0 ; i < units ; i++) {
460		dev = devclass_get_device(devclass, i);
461		if (isa_get_vendorid(dev) == isa_get_vendorid(scp->dev)
462		    && isa_get_logicalid(dev) == LOGICALID_PCM
463		    && isa_get_serial(dev) == isa_get_serial(scp->dev))
464			break;
465	}
466	if (i == units)
467		return (NULL);
468
469	return (dev);
470}
471
472static int io_range[3]  = {0x10, 0x8  , 0x4  };
473static int io_offset[3] = {0x0 , 0x100, 0x10c};
474static int
475alloc_resource(sc_p scp)
476{
477	int i, base, lid, flags;
478	device_t dev;
479
480	flags = 0;
481	if (isa_get_vendorid(scp->dev))
482		lid = isa_get_logicalid(scp->dev);
483	else {
484		lid = LOGICALID_NOPNP;
485		flags = device_get_flags(scp->dev);
486	}
487	switch(lid) {
488	case LOGICALID_PCM:
489	case LOGICALID_NOPNP:		/* XXX Non-PnP */
490		if (lid == LOGICALID_NOPNP)
491			base = isa_get_port(scp->dev);
492		else
493			base = 0;
494		for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) {
495			if (scp->io[i] == NULL) {
496				scp->io_rid[i] = i;
497				if (base == 0)
498					scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
499									0, ~0, io_range[i], RF_ACTIVE);
500				else
501					scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
502									base + io_offset[i],
503									base + io_offset[i] + io_range[i] - 1
504									, io_range[i], RF_ACTIVE);
505				if (scp->io[i] == NULL)
506					return (1);
507				scp->io_alloced[i] = 0;
508			}
509		}
510		if (scp->irq == NULL) {
511			scp->irq_rid = 0;
512			scp->irq = bus_alloc_resource(scp->dev, SYS_RES_IRQ, &scp->irq_rid,
513						      0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
514			if (scp->irq == NULL)
515				return (1);
516			scp->irq_alloced = 0;
517		}
518		for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) {
519			if (scp->drq[i] == NULL) {
520				scp->drq_rid[i] = i;
521				if (base == 0 || i == 0)
522					scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
523									 0, ~0, 1, RF_ACTIVE);
524				else if ((flags & DV_F_DUAL_DMA) != 0)
525					/* XXX The secondary drq is specified in the flag. */
526					scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
527									 flags & DV_F_DRQ_MASK,
528									 flags & DV_F_DRQ_MASK, 1, RF_ACTIVE);
529				if (scp->drq[i] == NULL)
530					return (1);
531				scp->drq_alloced[i] = 0;
532			}
533		}
534		break;
535	case LOGICALID_OPL:
536		if (scp->io[0] == NULL) {
537			scp->io_rid[0] = 0;
538			scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0],
539							0, ~0, io_range[0], RF_ACTIVE);
540			if (scp->io[0] == NULL)
541				return (1);
542			scp->io_alloced[0] = 0;
543		}
544		break;
545	case LOGICALID_MIDI:
546		if (scp->io[0] == NULL) {
547			scp->io_rid[0] = 0;
548			scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0],
549							0, ~0, io_range[0], RF_ACTIVE);
550			if (scp->io[0] == NULL)
551				return (1);
552			scp->io_alloced[0] = 0;
553		}
554		if (scp->irq == NULL) {
555			/* The irq is shared with pcm audio. */
556			dev = find_masterdev(scp);
557			if (dev == NULL)
558				return (1);
559			scp->irq_rid = 0;
560			scp->irq = BUS_ALLOC_RESOURCE(dev, NULL, SYS_RES_IRQ, &scp->irq_rid,
561						      0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
562			if (scp->irq == NULL)
563				return (1);
564			scp->irq_alloced = 0;
565		}
566		break;
567	}
568	return (0);
569}
570
571static int
572release_resource(sc_p scp)
573{
574	int i, lid, flags;
575	device_t dev;
576
577	flags = 0;
578	if (isa_get_vendorid(scp->dev))
579		lid = isa_get_logicalid(scp->dev);
580	else {
581		lid = LOGICALID_NOPNP;
582		flags = device_get_flags(scp->dev);
583	}
584	switch(lid) {
585	case LOGICALID_PCM:
586	case LOGICALID_NOPNP:		/* XXX Non-PnP */
587		for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) {
588			if (scp->io[i] != NULL) {
589				bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
590				scp->io[i] = NULL;
591			}
592		}
593		if (scp->irq != NULL) {
594			bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid, scp->irq);
595			scp->irq = NULL;
596		}
597		for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) {
598			if (scp->drq[i] != NULL) {
599				bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
600				scp->drq[i] = NULL;
601			}
602		}
603		break;
604	case LOGICALID_OPL:
605		if (scp->io[0] != NULL) {
606			bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
607			scp->io[0] = NULL;
608		}
609		break;
610	case LOGICALID_MIDI:
611		if (scp->io[0] != NULL) {
612			bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
613			scp->io[0] = NULL;
614		}
615		if (scp->irq != NULL) {
616			/* The irq is shared with pcm audio. */
617			dev = find_masterdev(scp);
618			if (dev == NULL)
619				return (1);
620			BUS_RELEASE_RESOURCE(dev, NULL, SYS_RES_IOPORT, scp->irq_rid, scp->irq);
621			scp->irq = NULL;
622		}
623		break;
624	}
625	return (0);
626}
627
628static device_method_t gusc_methods[] = {
629	/* Device interface */
630	DEVMETHOD(device_probe,		gusc_probe),
631	DEVMETHOD(device_attach,	gusc_attach),
632	DEVMETHOD(device_detach,	bus_generic_detach),
633	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
634	DEVMETHOD(device_suspend,	bus_generic_suspend),
635	DEVMETHOD(device_resume,	bus_generic_resume),
636
637	/* Bus interface */
638	DEVMETHOD(bus_print_child,	bus_generic_print_child),
639	DEVMETHOD(bus_alloc_resource,	gusc_alloc_resource),
640	DEVMETHOD(bus_release_resource,	gusc_release_resource),
641	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
642	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
643	DEVMETHOD(bus_setup_intr,	gusc_setup_intr),
644	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
645
646	{ 0, 0 }
647};
648
649static driver_t gusc_driver = {
650	"gusc",
651	gusc_methods,
652	sizeof(struct gusc_softc),
653};
654
655/*
656 * gusc can be attached to an isa bus.
657 */
658DRIVER_MODULE(snd_gusc, isa, gusc_driver, gusc_devclass, 0, 0);
659MODULE_DEPEND(snd_gusc, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
660MODULE_VERSION(snd_gusc, 1);
661
662
663