wss_isa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* $OpenBSD: wss_isa.c,v 1.6 2014/09/14 14:17:25 jsg Exp $ */
  2. /* $NetBSD: wss_isa.c,v 1.1 1998/01/19 22:18:24 augustss Exp $ */
  3. /*
  4. * Copyright (c) 1994 John Brezak
  5. * Copyright (c) 1991-1993 Regents of the University of California.
  6. * All rights reserved.
  7. *
  8. * MAD support:
  9. * Copyright (c) 1996 Lennart Augustsson
  10. * Based on code which is
  11. * Copyright (c) 1994 Hannu Savolainen
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. All advertising materials mentioning features or use of this software
  22. * must display the following acknowledgement:
  23. * This product includes software developed by the Computer Systems
  24. * Engineering Group at Lawrence Berkeley Laboratory.
  25. * 4. Neither the name of the University nor of the Laboratory may be used
  26. * to endorse or promote products derived from this software without
  27. * specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  30. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  38. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  39. * SUCH DAMAGE.
  40. *
  41. */
  42. #include <sys/param.h>
  43. #include <sys/systm.h>
  44. #include <sys/errno.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/syslog.h>
  47. #include <sys/device.h>
  48. #include <sys/buf.h>
  49. #include <machine/cpu.h>
  50. #include <machine/intr.h>
  51. #include <machine/bus.h>
  52. #include <sys/audioio.h>
  53. #include <dev/audio_if.h>
  54. #include <dev/isa/isavar.h>
  55. #include <dev/isa/isadmavar.h>
  56. #include <dev/ic/ad1848reg.h>
  57. #include <dev/isa/ad1848var.h>
  58. #include <dev/isa/wssreg.h>
  59. #include <dev/isa/wssvar.h>
  60. #include <dev/isa/madreg.h>
  61. #ifdef AUDIO_DEBUG
  62. #define DPRINTF(x) if (wssdebug) printf x
  63. extern int wssdebug;
  64. #else
  65. #define DPRINTF(x)
  66. #endif
  67. static int wssfind(struct device *, struct wss_softc *, struct isa_attach_args *);
  68. static void madprobe(struct wss_softc *, int);
  69. static void madunmap(struct wss_softc *);
  70. static int detect_mad16(struct wss_softc *, int);
  71. int wss_isa_probe(struct device *, void *, void *);
  72. void wss_isa_attach(struct device *, struct device *, void *);
  73. struct cfattach wss_isa_ca = {
  74. sizeof(struct wss_softc), wss_isa_probe, wss_isa_attach
  75. };
  76. struct cfdriver wss_cd = {
  77. NULL, "wss", DV_DULL
  78. };
  79. /*
  80. * Probe for the Microsoft Sound System hardware.
  81. */
  82. int
  83. wss_isa_probe(parent, match, aux)
  84. struct device *parent;
  85. void *match;
  86. void *aux;
  87. {
  88. struct wss_softc probesc, *sc = &probesc;
  89. bzero(sc, sizeof *sc);
  90. sc->sc_dev.dv_cfdata = ((struct device *)match)->dv_cfdata;
  91. if (wssfind(parent, sc, aux)) {
  92. bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
  93. ad1848_unmap(&sc->sc_ad1848);
  94. madunmap(sc);
  95. return 1;
  96. } else
  97. /* Everything is already unmapped */
  98. return 0;
  99. }
  100. static int
  101. wssfind(parent, sc, ia)
  102. struct device *parent;
  103. struct wss_softc *sc;
  104. struct isa_attach_args *ia;
  105. {
  106. static u_char interrupt_bits[12] = {
  107. -1, -1, -1, -1, -1, 0x0, -1, 0x08, -1, 0x10, 0x18, 0x20
  108. };
  109. static u_char dma_bits[4] = {1, 2, 0, 3};
  110. sc->sc_iot = ia->ia_iot;
  111. if (sc->sc_dev.dv_cfdata->cf_flags & 1)
  112. madprobe(sc, ia->ia_iobase);
  113. else
  114. sc->mad_chip_type = MAD_NONE;
  115. if (!WSS_BASE_VALID(ia->ia_iobase)) {
  116. DPRINTF(("wss: configured iobase %x invalid\n", ia->ia_iobase));
  117. goto bad1;
  118. }
  119. /* Map the ports upto the AD1848 port */
  120. if (bus_space_map(sc->sc_iot, ia->ia_iobase, WSS_CODEC, 0, &sc->sc_ioh))
  121. goto bad1;
  122. sc->sc_ad1848.sc_iot = sc->sc_iot;
  123. /* Is there an ad1848 chip at (WSS iobase + WSS_CODEC)? */
  124. if (ad1848_mapprobe(&sc->sc_ad1848, ia->ia_iobase + WSS_CODEC) == 0)
  125. goto bad;
  126. ia->ia_iosize = WSS_NPORT;
  127. /* Setup WSS interrupt and DMA */
  128. if (!WSS_DRQ_VALID(ia->ia_drq)) {
  129. DPRINTF(("wss: configured dma chan %d invalid\n", ia->ia_drq));
  130. goto bad;
  131. }
  132. sc->wss_drq = ia->ia_drq;
  133. if (sc->wss_drq != DRQUNK && !isa_drq_isfree(parent, sc->wss_drq))
  134. goto bad;
  135. if (!WSS_IRQ_VALID(ia->ia_irq)) {
  136. DPRINTF(("wss: configured interrupt %d invalid\n", ia->ia_irq));
  137. goto bad;
  138. }
  139. sc->wss_irq = ia->ia_irq;
  140. if (sc->sc_ad1848.mode <= 1)
  141. ia->ia_drq2 = DRQUNK;
  142. sc->wss_recdrq =
  143. sc->sc_ad1848.mode > 1 && ia->ia_drq2 != DRQUNK ?
  144. ia->ia_drq2 : ia->ia_drq;
  145. if (sc->wss_recdrq != sc->wss_drq && !isa_drq_isfree(parent, sc->wss_recdrq))
  146. goto bad;
  147. /* XXX recdrq */
  148. bus_space_write_1(sc->sc_iot, sc->sc_ioh, WSS_CONFIG,
  149. (interrupt_bits[ia->ia_irq] | dma_bits[ia->ia_drq]));
  150. return 1;
  151. bad:
  152. bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
  153. bad1:
  154. madunmap(sc);
  155. return 0;
  156. }
  157. /*
  158. * Attach hardware to driver, attach hardware driver to audio
  159. * pseudo-device driver .
  160. */
  161. void
  162. wss_isa_attach(parent, self, aux)
  163. struct device *parent, *self;
  164. void *aux;
  165. {
  166. struct wss_softc *sc = (struct wss_softc *)self;
  167. struct isa_attach_args *ia = (struct isa_attach_args *)aux;
  168. if (!wssfind(parent, sc, ia)) {
  169. printf("%s: wssfind failed\n", sc->sc_dev.dv_xname);
  170. return;
  171. }
  172. sc->sc_ic = ia->ia_ic;
  173. sc->sc_ad1848.sc_isa = parent;
  174. wssattach(sc);
  175. }
  176. /*
  177. * Copyright by Hannu Savolainen 1994
  178. *
  179. * Redistribution and use in source and binary forms, with or without
  180. * modification, are permitted provided that the following conditions are
  181. * met: 1. Redistributions of source code must retain the above copyright
  182. * notice, this list of conditions and the following disclaimer. 2.
  183. * Redistributions in binary form must reproduce the above copyright notice,
  184. * this list of conditions and the following disclaimer in the documentation
  185. * and/or other materials provided with the distribution.
  186. *
  187. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  188. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  189. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  190. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  191. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  192. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  193. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  194. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  195. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  196. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  197. * SUCH DAMAGE.
  198. *
  199. */
  200. /*
  201. * Initialization code for OPTi MAD16 compatible audio chips. Including
  202. *
  203. * OPTi 82C928 MAD16 (replaced by C929)
  204. * OAK OTI-601D Mozart
  205. * OPTi 82C929 MAD16 Pro
  206. * OPTi 82C931
  207. */
  208. static int
  209. detect_mad16(sc, chip_type)
  210. struct wss_softc *sc;
  211. int chip_type;
  212. {
  213. unsigned char tmp, tmp2;
  214. sc->mad_chip_type = chip_type;
  215. /*
  216. * Check that reading a register doesn't return bus float (0xff)
  217. * when the card is accessed using password. This may fail in case
  218. * the card is in low power mode. Normally at least the power saving mode
  219. * bit should be 0.
  220. */
  221. if ((tmp = mad_read(sc, MC1_PORT)) == 0xff) {
  222. DPRINTF(("MC1_PORT returned 0xff\n"));
  223. return 0;
  224. }
  225. /*
  226. * Now check that the gate is closed on first I/O after writing
  227. * the password. (This is how a MAD16 compatible card works).
  228. */
  229. if ((tmp2 = bus_space_read_1(sc->sc_iot, sc->mad_ioh, MC1_PORT)) == tmp) {
  230. DPRINTF(("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
  231. return 0;
  232. }
  233. mad_write(sc, MC1_PORT, tmp ^ 0x80); /* Toggle a bit */
  234. /* Compare the bit */
  235. if ((tmp2 = mad_read(sc, MC1_PORT)) != (tmp ^ 0x80)) {
  236. mad_write(sc, MC1_PORT, tmp); /* Restore */
  237. DPRINTF(("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
  238. return 0;
  239. }
  240. mad_write(sc, MC1_PORT, tmp); /* Restore */
  241. return 1;
  242. }
  243. static void
  244. madprobe(sc, iobase)
  245. struct wss_softc *sc;
  246. int iobase;
  247. {
  248. static int valid_ports[M_WSS_NPORTS] =
  249. { M_WSS_PORT0, M_WSS_PORT1, M_WSS_PORT2, M_WSS_PORT3 };
  250. int i;
  251. /* Allocate bus space that the MAD chip wants */
  252. if (bus_space_map(sc->sc_iot, MAD_BASE, MAD_NPORT, 0, &sc->mad_ioh))
  253. goto bad0;
  254. if (bus_space_map(sc->sc_iot, MAD_REG1, MAD_LEN1, 0, &sc->mad_ioh1))
  255. goto bad1;
  256. if (bus_space_map(sc->sc_iot, MAD_REG2, MAD_LEN2, 0, &sc->mad_ioh2))
  257. goto bad2;
  258. if (bus_space_map(sc->sc_iot, MAD_REG3, MAD_LEN3, 0, &sc->mad_ioh3))
  259. goto bad3;
  260. DPRINTF(("mad: Detect using password = 0xE2\n"));
  261. if (!detect_mad16(sc, MAD_82C928)) {
  262. /* No luck. Try different model */
  263. DPRINTF(("mad: Detect using password = 0xE3\n"));
  264. if (!detect_mad16(sc, MAD_82C929))
  265. goto bad;
  266. sc->mad_chip_type = MAD_82C929;
  267. DPRINTF(("mad: 82C929 detected\n"));
  268. } else {
  269. sc->mad_chip_type = MAD_82C928;
  270. if ((mad_read(sc, MC3_PORT) & 0x03) == 0x03) {
  271. DPRINTF(("mad: Mozart detected\n"));
  272. sc->mad_chip_type = MAD_OTI601D;
  273. } else {
  274. DPRINTF(("mad: 82C928 detected?\n"));
  275. sc->mad_chip_type = MAD_82C928;
  276. }
  277. }
  278. #ifdef AUDIO_DEBUG
  279. if (wssdebug)
  280. for (i = MC1_PORT; i <= MC7_PORT; i++)
  281. printf("mad: port %03x = %02x\n", i, mad_read(sc, i));
  282. #endif
  283. /* Set the WSS address. */
  284. for (i = 0; i < M_WSS_NPORTS; i++)
  285. if (valid_ports[i] == iobase)
  286. break;
  287. if (i >= M_WSS_NPORTS) { /* Not a valid port */
  288. printf("mad: Bad WSS base address 0x%x\n", iobase);
  289. goto bad;
  290. }
  291. sc->mad_ioindex = i;
  292. /* enable WSS emulation at the I/O port, no joystick */
  293. mad_write(sc, MC1_PORT, M_WSS_PORT_SELECT(i) | MC1_JOYDISABLE);
  294. mad_write(sc, MC2_PORT, 0x03); /* ? */
  295. mad_write(sc, MC3_PORT, 0xf0); /* Disable SB */
  296. return;
  297. bad:
  298. bus_space_unmap(sc->sc_iot, sc->mad_ioh3, MAD_LEN3);
  299. bad3:
  300. bus_space_unmap(sc->sc_iot, sc->mad_ioh2, MAD_LEN2);
  301. bad2:
  302. bus_space_unmap(sc->sc_iot, sc->mad_ioh1, MAD_LEN1);
  303. bad1:
  304. bus_space_unmap(sc->sc_iot, sc->mad_ioh, MAD_NPORT);
  305. bad0:
  306. sc->mad_chip_type = MAD_NONE;
  307. }
  308. static void
  309. madunmap(sc)
  310. struct wss_softc *sc;
  311. {
  312. if (sc->mad_chip_type == MAD_NONE)
  313. return;
  314. bus_space_unmap(sc->sc_iot, sc->mad_ioh, MAD_NPORT);
  315. bus_space_unmap(sc->sc_iot, sc->mad_ioh1, MAD_LEN1);
  316. bus_space_unmap(sc->sc_iot, sc->mad_ioh2, MAD_LEN2);
  317. bus_space_unmap(sc->sc_iot, sc->mad_ioh3, MAD_LEN3);
  318. }