Merge from vendor branch NCURSES:
[dragonfly.git] / sys / dev / sound / isa / es1888.c
1 /*
2  * Copyright (c) 1999 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/sound/isa/es1888.c,v 1.5.2.5 2002/04/22 15:49:30 cg Exp $
27  * $DragonFly: src/sys/dev/sound/isa/Attic/es1888.c,v 1.2 2003/06/17 04:28:30 dillon Exp $
28  */
29
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/isa/sb.h>
32
33 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/Attic/es1888.c,v 1.2 2003/06/17 04:28:30 dillon Exp $");
34
35 #ifdef __alpha__
36 static int
37 es1888_dspready(u_int32_t port)
38 {
39         return ((inb(port + SBDSP_STATUS) & 0x80) == 0);
40 }
41
42 static int
43 es1888_dspwr(u_int32_t port, u_char val)
44 {
45         int  i;
46
47         for (i = 0; i < 1000; i++) {
48                 if (es1888_dspready(port)) {
49                         outb(port + SBDSP_CMD, val);
50                         return 0;
51                 }
52                 if (i > 10) DELAY((i > 100)? 1000 : 10);
53         }
54         return ENXIO;
55 }
56
57 static u_int
58 es1888_get_byte(u_int32_t port)
59 {
60         int i;
61
62         for (i = 1000; i > 0; i--) {
63                 if (inb(port + DSP_DATA_AVAIL) & 0x80)
64                         return inb(port + DSP_READ);
65                 else
66                         DELAY(20);
67         }
68         return 0xffff;
69 }
70
71 static int
72 es1888_reset(u_int32_t port)
73 {
74         outb(port + SBDSP_RST, 3);
75         DELAY(100);
76         outb(port + SBDSP_RST, 0);
77         if (es1888_get_byte(port) != 0xAA) {
78                 return ENXIO;   /* Sorry */
79         }
80         return 0;
81 }
82
83 static void
84 es1888_configuration_mode(void)
85 {
86         /*
87          * Emit the Read-Sequence-Key to enter configuration
88          * mode. Note this only works after a reset (or after bit 2 of
89          * mixer register 0x40 is set).
90          *
91          * 3 reads from 0x229 in a row guarantees reset of key
92          * sequence to beginning.
93          */
94         inb(0x229);
95         inb(0x229);
96         inb(0x229);
97
98         inb(0x22b);             /* state 1 */
99         inb(0x229);             /* state 2 */
100         inb(0x22b);             /* state 3 */
101         inb(0x229);             /* state 4 */
102         inb(0x229);             /* state 5 */
103         inb(0x22b);             /* state 6 */
104         inb(0x229);             /* state 7 */
105 }
106
107 static void
108 es1888_set_port(u_int32_t port)
109 {
110         es1888_configuration_mode();
111         inb(port);
112 }
113 #endif
114
115 static void
116 es1888_identify(driver_t *driver, device_t parent)
117 {
118 /*
119  * Only use this on alpha since PNPBIOS is a better solution on x86.
120  */
121 #ifdef __alpha__
122         u_int32_t lo, hi;
123         device_t dev;
124
125         es1888_set_port(0x220);
126         if (es1888_reset(0x220))
127                 return;
128
129         /*
130          * Check identification bytes for es1888.
131          */
132         if (es1888_dspwr(0x220, 0xe7))
133                 return;
134         hi = es1888_get_byte(0x220);
135         lo = es1888_get_byte(0x220);
136         if (hi != 0x68 || (lo & 0xf0) != 0x80)
137                 return;
138
139         /*
140          * Program irq and drq.
141          */
142         if (es1888_dspwr(0x220, 0xc6) /* enter extended mode */
143             || es1888_dspwr(0x220, 0xb1) /* write register b1 */
144             || es1888_dspwr(0x220, 0x14) /* enable irq 5 */
145             || es1888_dspwr(0x220, 0xb2) /* write register b1 */
146             || es1888_dspwr(0x220, 0x18)) /* enable drq 1 */
147                 return;
148
149         /*
150          * Create the device and program its resources.
151          */
152         dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
153         bus_set_resource(dev, SYS_RES_IOPORT, 0, 0x220, 0x10);
154         bus_set_resource(dev, SYS_RES_IRQ, 0, 5, 1);
155         bus_set_resource(dev, SYS_RES_DRQ, 0, 1, 1);
156         isa_set_vendorid(dev, PNP_EISAID("ESS1888"));
157         isa_set_logicalid(dev, PNP_EISAID("ESS1888"));
158 #endif
159 }
160
161 static device_method_t es1888_methods[] = {
162         /* Device interface */
163         DEVMETHOD(device_identify,      es1888_identify),
164
165         { 0, 0 }
166 };
167
168 static driver_t es1888_driver = {
169         "pcm",
170         es1888_methods,
171         1,                      /* no softc */
172 };
173
174 DRIVER_MODULE(snd_es1888, isa, es1888_driver, pcm_devclass, 0, 0);
175 MODULE_DEPEND(snd_es1888, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
176 MODULE_VERSION(snd_es1888, 1);
177
178