aboutsummaryrefslogtreecommitdiff
path: root/sys/i386/isa/sound/trix.c
blob: 6e3db0fa7c9019a6d407d368f86cb21f2b3ce7d8 (plain) (blame)
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
/*
 * sound/trix.c
 *
 * Low level driver for the MediaTriX AudioTriX Pro
 * (MT-0002-PC Control Chip)
 *
 * Copyright by Hannu Savolainen 1995
 *
 * 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.
 *
 */

#include "sound_config.h"

#if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_TRIX)

#ifdef INCLUDE_TRIX_BOOT
#include "trix_boot.h"
#endif

static int      kilroy_was_here = 0;	/* Don't detect twice */
static int      sb_initialized = 0;
static int      mpu_initialized = 0;

static unsigned char
trix_read (int addr)
{
  OUTB ((unsigned char) addr, 0x390);	/* MT-0002-PC ASIC address */
  return INB (0x391);		/* MT-0002-PC ASIC data */
}

static void
trix_write (int addr, int data)
{
  OUTB ((unsigned char) addr, 0x390);	/* MT-0002-PC ASIC address */
  OUTB ((unsigned char) data, 0x391);	/* MT-0002-PC ASIC data */
}

static void
download_boot (int base)
{
  int             i = 0, n = sizeof (trix_boot);

  trix_write (0xf8, 0x00);	/* ??????? */
  OUTB (0x01, base + 6);	/* Clear the internal data pointer */
  OUTB (0x00, base + 6);	/* Restart */

  /*
     *  Write the boot code to the RAM upload/download register.
     *  Each write increments the internal data pointer.
   */
  OUTB (0x01, base + 6);	/* Clear the internal data pointer */
  OUTB (0x1A, 0x390);		/* Select RAM download/upload port */

  for (i = 0; i < n; i++)
    OUTB (trix_boot[i], 0x391);
  for (i = n; i < 10016; i++)	/* Clear up to first 16 bytes of data RAM */
    OUTB (0x00, 0x391);
  OUTB (0x00, base + 6);	/* Reset */
  OUTB (0x50, 0x390);		/* ?????? */
}

static int
trix_set_wss_port (struct address_info *hw_config)
{
  unsigned char   addr_bits;

  if (kilroy_was_here)		/* Already initialized */
    return 0;

  kilroy_was_here = 1;

  if (trix_read (0x15) != 0x71)	/* No asic signature */
    return 0;

  /*
     * Disable separate wave playback and recording DMA channels since
     * the driver doesn't support duplex mode yet.
   */

  trix_write (0x13, trix_read (0x13) & ~0x80);
  trix_write (0x14, trix_read (0x14) & ~0x80);

  /*
     * Configure the ASIC to place the codec to the proper I/O location
   */

  switch (hw_config->io_base)
    {
    case 0x530:
      addr_bits = 0;
      break;
    case 0x604:
      addr_bits = 1;
      break;
    case 0xE80:
      addr_bits = 2;
      break;
    case 0xF40:
      addr_bits = 3;
      break;
    default:
      return 0;
    }

  trix_write (0x19, (trix_read (0x19) & 0x03) | addr_bits);
  return 1;
}

/*
 *    Probe and attach routines for the Windows Sound System mode of
 *      AudioTriX Pro
 */

int
probe_trix_wss (struct address_info *hw_config)
{
  /*
     * Check if the IO port returns valid signature. The original MS Sound
     * system returns 0x04 while some cards (AudioTriX Pro for example)
     * return 0x00.
   */
  if (!trix_set_wss_port (hw_config))
    return 0;

  if ((INB (hw_config->io_base + 3) & 0x3f) != 0x00)
    {
      DDB (printk ("No MSS signature detected on port 0x%x\n", hw_config->io_base));
      return 0;
    }

  if (hw_config->irq > 11)
    {
      printk ("AudioTriX: Bad WSS IRQ %d\n", hw_config->irq);
      return 0;
    }

  if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
    {
      printk ("AudioTriX: Bad WSS DMA %d\n", hw_config->dma);
      return 0;
    }

  /*
     * Check that DMA0 is not in use with a 8 bit board.
   */

  if (hw_config->dma == 0 && INB (hw_config->io_base + 3) & 0x80)
    {
      printk ("AudioTriX: Can't use DMA0 with a 8 bit card\n");
      return 0;
    }

  if (hw_config->irq > 7 && hw_config->irq != 9 && INB (hw_config->io_base + 3) & 0x80)
    {
      printk ("AudioTriX: Can't use IRQ%d with a 8 bit card\n", hw_config->irq);
      return 0;
    }

  return ad1848_detect (hw_config->io_base + 4);
}

long
attach_trix_wss (long mem_start, struct address_info *hw_config)
{
  static unsigned char interrupt_bits[12] =
  {-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20};
  char            bits;

  static unsigned char dma_bits[4] =
  {1, 2, 0, 3};

  int             config_port = hw_config->io_base + 0, version_port = hw_config->io_base + 3;

  if (!kilroy_was_here)
    return mem_start;

  /*
     * Set the IRQ and DMA addresses.
   */

  bits = interrupt_bits[hw_config->irq];
  if (bits == -1)
    return mem_start;

  OUTB (bits | 0x40, config_port);
  if ((INB (version_port) & 0x40) == 0)
    printk ("[IRQ Conflict?]");

  OUTB (bits | dma_bits[hw_config->dma], config_port);	/* Write IRQ+DMA setup */

  ad1848_init ("AudioTriX Pro", hw_config->io_base + 4,
	       hw_config->irq,
	       hw_config->dma,
	       hw_config->dma);
  return mem_start;
}

int
probe_trix_sb (struct address_info *hw_config)
{

  int             tmp;
  unsigned char   conf;
  static char     irq_translate[] =
  {-1, -1, -1, 0, 1, 2, -1, 3};

#ifndef INCLUDE_TRIX_BOOT
  return 0;			/* No boot code -> no fun */
#endif
  if (!kilroy_was_here)
    return 0;			/* AudioTriX Pro has not been detected earlier */

  if (sb_initialized)
    return 0;

  if (hw_config->io_base & 0xffffff8f != 0x200)
    return 0;

  tmp = hw_config->irq;
  if (tmp > 7)
    return 0;
  if (irq_translate[tmp] == -1)
    return 0;

  tmp = hw_config->dma;
  if (tmp != 1 && tmp != 3)
    return 0;

  conf = 0x84;			/* DMA and IRQ enable */
  conf |= hw_config->io_base & 0x70;	/* I/O address bits */
  conf |= irq_translate[hw_config->irq];
  if (hw_config->dma == 3)
    conf |= 0x08;
  trix_write (0x1b, conf);

  download_boot (hw_config->io_base);
  sb_initialized = 1;

  return 1;
}

long
attach_trix_sb (long mem_start, struct address_info *hw_config)
{
  printk (" <AudioTriX>");
  return mem_start;
}

long
attach_trix_mpu (long mem_start, struct address_info *hw_config)
{
  return attach_mpu401 (mem_start, hw_config);
}

int
probe_trix_mpu (struct address_info *hw_config)
{
  unsigned char   conf;
  static char     irq_bits[] =
  {-1, -1, -1, 1, 2, 3, -1, 4, -1, 5};

  if (!kilroy_was_here)
    return 0;			/* AudioTriX Pro has not been detected earlier */

  if (!sb_initialized)
    return 0;

  if (mpu_initialized)
    return 0;

  if (hw_config->irq > 9)
    return 0;

  if (irq_bits[hw_config->irq] == -1)
    return 0;

  switch (hw_config->io_base)
    {
    case 0x330:
      conf = 0x00;
      break;
    case 0x370:
      conf = 0x04;
      break;
    case 0x3b0:
      conf = 0x08;
      break;
    case 0x3f0:
      conf = 0x0c;
      break;
    default:
      return 0;			/* Invalid port */
    }

  conf |= irq_bits[hw_config->irq] << 4;

  trix_write (0x19, (trix_read (0x19) & 0x83) | conf);

  mpu_initialized = 1;

  return probe_mpu401 (hw_config);
}

#endif