mad16.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Copyright (C) by Hannu Savolainen 1993-1997
  3. *
  4. * mad16.c
  5. *
  6. * Initialization code for OPTi MAD16 compatible audio chips. Including
  7. *
  8. * OPTi 82C928 MAD16 (replaced by C929)
  9. * OAK OTI-601D Mozart
  10. * OAK OTI-605 Mozart (later version with MPU401 Midi)
  11. * OPTi 82C929 MAD16 Pro
  12. * OPTi 82C930
  13. * OPTi 82C924
  14. *
  15. * These audio interface chips don't produce sound themselves. They just
  16. * connect some other components (OPL-[234] and a WSS compatible codec)
  17. * to the PC bus and perform I/O, DMA and IRQ address decoding. There is
  18. * also a UART for the MPU-401 mode (not 82C928/Mozart).
  19. * The Mozart chip appears to be compatible with the 82C928, although later
  20. * issues of the card, using the OTI-605 chip, have an MPU-401 compatible Midi
  21. * port. This port is configured differently to that of the OPTi audio chips.
  22. *
  23. * Changes
  24. *
  25. * Alan Cox Clean up, added module selections.
  26. *
  27. * A. Wik Added support for Opti924 PnP.
  28. * Improved debugging support. 16-May-1998
  29. * Fixed bug. 16-Jun-1998
  30. *
  31. * Torsten Duwe Made Opti924 PnP support non-destructive
  32. * 23-Dec-1998
  33. *
  34. * Paul Grayson Added support for Midi on later Mozart cards.
  35. * 25-Nov-1999
  36. * Christoph Hellwig Adapted to module_init/module_exit.
  37. * Arnaldo C. de Melo got rid of attach_uart401 21-Sep-2000
  38. *
  39. * Pavel Rabel Clean up Nov-2000
  40. */
  41. #include <linux/config.h>
  42. #include <linux/init.h>
  43. #include <linux/module.h>
  44. #include <linux/gameport.h>
  45. #include <linux/spinlock.h>
  46. #include "sound_config.h"
  47. #include "ad1848.h"
  48. #include "sb.h"
  49. #include "mpu401.h"
  50. static int mad16_conf;
  51. static int mad16_cdsel;
  52. static struct gameport *gameport;
  53. static DEFINE_SPINLOCK(lock);
  54. #define C928 1
  55. #define MOZART 2
  56. #define C929 3
  57. #define C930 4
  58. #define C924 5
  59. /*
  60. * Registers
  61. *
  62. * The MAD16 occupies I/O ports 0xf8d to 0xf93 (fixed locations).
  63. * All ports are inactive by default. They can be activated by
  64. * writing 0xE2 or 0xE3 to the password register. The password is valid
  65. * only until the next I/O read or write.
  66. *
  67. * 82C930 uses 0xE4 as the password and indirect addressing to access
  68. * the config registers.
  69. */
  70. #define MC0_PORT 0xf8c /* Dummy port */
  71. #define MC1_PORT 0xf8d /* SB address, CD-ROM interface type, joystick */
  72. #define MC2_PORT 0xf8e /* CD-ROM address, IRQ, DMA, plus OPL4 bit */
  73. #define MC3_PORT 0xf8f
  74. #define PASSWD_REG 0xf8f
  75. #define MC4_PORT 0xf90
  76. #define MC5_PORT 0xf91
  77. #define MC6_PORT 0xf92
  78. #define MC7_PORT 0xf93
  79. #define MC8_PORT 0xf94
  80. #define MC9_PORT 0xf95
  81. #define MC10_PORT 0xf96
  82. #define MC11_PORT 0xf97
  83. #define MC12_PORT 0xf98
  84. static int board_type = C928;
  85. static int *mad16_osp;
  86. static int c931_detected; /* minor differences from C930 */
  87. static char c924pnp; /* " " " C924 */
  88. static int debug; /* debugging output */
  89. #ifdef DDB
  90. #undef DDB
  91. #endif
  92. #define DDB(x) do {if (debug) x;} while (0)
  93. static unsigned char mad_read(int port)
  94. {
  95. unsigned long flags;
  96. unsigned char tmp;
  97. spin_lock_irqsave(&lock,flags);
  98. switch (board_type) /* Output password */
  99. {
  100. case C928:
  101. case MOZART:
  102. outb((0xE2), PASSWD_REG);
  103. break;
  104. case C929:
  105. outb((0xE3), PASSWD_REG);
  106. break;
  107. case C930:
  108. /* outb(( 0xE4), PASSWD_REG); */
  109. break;
  110. case C924:
  111. /* the c924 has its ports relocated by -128 if
  112. PnP is enabled -aw */
  113. if (!c924pnp)
  114. outb((0xE5), PASSWD_REG); else
  115. outb((0xE5), PASSWD_REG - 0x80);
  116. break;
  117. }
  118. if (board_type == C930)
  119. {
  120. outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
  121. tmp = inb(0xe0f); /* Read from data reg */
  122. }
  123. else
  124. if (!c924pnp)
  125. tmp = inb(port); else
  126. tmp = inb(port-0x80);
  127. spin_unlock_irqrestore(&lock,flags);
  128. return tmp;
  129. }
  130. static void mad_write(int port, int value)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&lock,flags);
  134. switch (board_type) /* Output password */
  135. {
  136. case C928:
  137. case MOZART:
  138. outb((0xE2), PASSWD_REG);
  139. break;
  140. case C929:
  141. outb((0xE3), PASSWD_REG);
  142. break;
  143. case C930:
  144. /* outb(( 0xE4), PASSWD_REG); */
  145. break;
  146. case C924:
  147. if (!c924pnp)
  148. outb((0xE5), PASSWD_REG); else
  149. outb((0xE5), PASSWD_REG - 0x80);
  150. break;
  151. }
  152. if (board_type == C930)
  153. {
  154. outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
  155. outb(((unsigned char) (value & 0xff)), 0xe0f);
  156. }
  157. else
  158. if (!c924pnp)
  159. outb(((unsigned char) (value & 0xff)), port); else
  160. outb(((unsigned char) (value & 0xff)), port-0x80);
  161. spin_unlock_irqrestore(&lock,flags);
  162. }
  163. static int __init detect_c930(void)
  164. {
  165. unsigned char tmp = mad_read(MC1_PORT);
  166. if ((tmp & 0x06) != 0x06)
  167. {
  168. DDB(printk("Wrong C930 signature (%x)\n", tmp));
  169. /* return 0; */
  170. }
  171. mad_write(MC1_PORT, 0);
  172. if (mad_read(MC1_PORT) != 0x06)
  173. {
  174. DDB(printk("Wrong C930 signature2 (%x)\n", tmp));
  175. /* return 0; */
  176. }
  177. mad_write(MC1_PORT, tmp); /* Restore bits */
  178. mad_write(MC7_PORT, 0);
  179. if ((tmp = mad_read(MC7_PORT)) != 0)
  180. {
  181. DDB(printk("MC7 not writable (%x)\n", tmp));
  182. return 0;
  183. }
  184. mad_write(MC7_PORT, 0xcb);
  185. if ((tmp = mad_read(MC7_PORT)) != 0xcb)
  186. {
  187. DDB(printk("MC7 not writable2 (%x)\n", tmp));
  188. return 0;
  189. }
  190. tmp = mad_read(MC0_PORT+18);
  191. if (tmp == 0xff || tmp == 0x00)
  192. return 1;
  193. /* We probably have a C931 */
  194. DDB(printk("Detected C931 config=0x%02x\n", tmp));
  195. c931_detected = 1;
  196. /*
  197. * We cannot configure the chip if it is in PnP mode.
  198. * If we have a CSN assigned (bit 8 in MC13) we first try
  199. * a software reset, then a software power off, finally
  200. * Clearing PnP mode. The last option is not
  201. * Bit 8 in MC13
  202. */
  203. if ((mad_read(MC0_PORT+13) & 0x80) == 0)
  204. return 1;
  205. /* Software reset */
  206. mad_write(MC9_PORT, 0x02);
  207. mad_write(MC9_PORT, 0x00);
  208. if ((mad_read(MC0_PORT+13) & 0x80) == 0)
  209. return 1;
  210. /* Power off, and on again */
  211. mad_write(MC9_PORT, 0xc2);
  212. mad_write(MC9_PORT, 0xc0);
  213. if ((mad_read(MC0_PORT+13) & 0x80) == 0)
  214. return 1;
  215. #if 0
  216. /* Force off PnP mode. This is not recommended because
  217. * the PnP bios will not recognize the chip on the next
  218. * warm boot and may assignd different resources to other
  219. * PnP/PCI cards.
  220. */
  221. mad_write(MC0_PORT+17, 0x04);
  222. #endif
  223. return 1;
  224. }
  225. static int __init detect_mad16(void)
  226. {
  227. unsigned char tmp, tmp2, bit;
  228. int i, port;
  229. /*
  230. * Check that reading a register doesn't return bus float (0xff)
  231. * when the card is accessed using password. This may fail in case
  232. * the card is in low power mode. Normally at least the power saving
  233. * mode bit should be 0.
  234. */
  235. if ((tmp = mad_read(MC1_PORT)) == 0xff)
  236. {
  237. DDB(printk("MC1_PORT returned 0xff\n"));
  238. return 0;
  239. }
  240. for (i = 0xf8d; i <= 0xf98; i++)
  241. if (!c924pnp)
  242. DDB(printk("Port %0x (init value) = %0x\n", i, mad_read(i)));
  243. else
  244. DDB(printk("Port %0x (init value) = %0x\n", i-0x80, mad_read(i)));
  245. if (board_type == C930)
  246. return detect_c930();
  247. /*
  248. * Now check that the gate is closed on first I/O after writing
  249. * the password. (This is how a MAD16 compatible card works).
  250. */
  251. if ((tmp2 = inb(MC1_PORT)) == tmp) /* It didn't close */
  252. {
  253. DDB(printk("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
  254. return 0;
  255. }
  256. bit = (c924pnp) ? 0x20 : 0x80;
  257. port = (c924pnp) ? MC2_PORT : MC1_PORT;
  258. tmp = mad_read(port);
  259. mad_write(port, tmp ^ bit); /* Toggle a bit */
  260. if ((tmp2 = mad_read(port)) != (tmp ^ bit)) /* Compare the bit */
  261. {
  262. mad_write(port, tmp); /* Restore */
  263. DDB(printk("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
  264. return 0;
  265. }
  266. mad_write(port, tmp); /* Restore */
  267. return 1; /* Bingo */
  268. }
  269. static int __init wss_init(struct address_info *hw_config)
  270. {
  271. /*
  272. * Check if the IO port returns valid signature. The original MS Sound
  273. * system returns 0x04 while some cards (AudioTrix Pro for example)
  274. * return 0x00.
  275. */
  276. if ((inb(hw_config->io_base + 3) & 0x3f) != 0x04 &&
  277. (inb(hw_config->io_base + 3) & 0x3f) != 0x00)
  278. {
  279. DDB(printk("No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, inb(hw_config->io_base + 3)));
  280. return 0;
  281. }
  282. /*
  283. * Check that DMA0 is not in use with a 8 bit board.
  284. */
  285. if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
  286. {
  287. printk("MSS: Can't use DMA0 with a 8 bit card/slot\n");
  288. return 0;
  289. }
  290. if (hw_config->irq > 9 && inb(hw_config->io_base + 3) & 0x80)
  291. printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
  292. return 1;
  293. }
  294. static void __init init_c930(struct address_info *hw_config, int base)
  295. {
  296. unsigned char cfg = 0;
  297. cfg |= (0x0f & mad16_conf);
  298. if(c931_detected)
  299. {
  300. /* Bit 0 has reversd meaning. Bits 1 and 2 sese
  301. reversed on write.
  302. Support only IDE cdrom. IDE port programmed
  303. somewhere else. */
  304. cfg = (cfg & 0x09) ^ 0x07;
  305. }
  306. cfg |= base << 4;
  307. mad_write(MC1_PORT, cfg);
  308. /* MC2 is CD configuration. Don't touch it. */
  309. mad_write(MC3_PORT, 0); /* Disable SB mode IRQ and DMA */
  310. /* bit 2 of MC4 reverses it's meaning between the C930
  311. and the C931. */
  312. cfg = c931_detected ? 0x04 : 0x00;
  313. if(mad16_cdsel & 0x20)
  314. mad_write(MC4_PORT, 0x62|cfg); /* opl4 */
  315. else
  316. mad_write(MC4_PORT, 0x52|cfg); /* opl3 */
  317. mad_write(MC5_PORT, 0x3C); /* Init it into mode2 */
  318. mad_write(MC6_PORT, 0x02); /* Enable WSS, Disable MPU and SB */
  319. mad_write(MC7_PORT, 0xCB);
  320. mad_write(MC10_PORT, 0x11);
  321. }
  322. static int __init chip_detect(void)
  323. {
  324. int i;
  325. /*
  326. * Then try to detect with the old password
  327. */
  328. board_type = C924;
  329. DDB(printk("Detect using password = 0xE5\n"));
  330. if (detect_mad16()) {
  331. return 1;
  332. }
  333. board_type = C928;
  334. DDB(printk("Detect using password = 0xE2\n"));
  335. if (detect_mad16())
  336. {
  337. unsigned char model;
  338. if (((model = mad_read(MC3_PORT)) & 0x03) == 0x03) {
  339. DDB(printk("mad16.c: Mozart detected\n"));
  340. board_type = MOZART;
  341. } else {
  342. DDB(printk("mad16.c: 82C928 detected???\n"));
  343. board_type = C928;
  344. }
  345. return 1;
  346. }
  347. board_type = C929;
  348. DDB(printk("Detect using password = 0xE3\n"));
  349. if (detect_mad16())
  350. {
  351. DDB(printk("mad16.c: 82C929 detected\n"));
  352. return 1;
  353. }
  354. if (inb(PASSWD_REG) != 0xff)
  355. return 0;
  356. /*
  357. * First relocate MC# registers to 0xe0e/0xe0f, disable password
  358. */
  359. outb((0xE4), PASSWD_REG);
  360. outb((0x80), PASSWD_REG);
  361. board_type = C930;
  362. DDB(printk("Detect using password = 0xE4\n"));
  363. for (i = 0xf8d; i <= 0xf93; i++)
  364. DDB(printk("port %03x = %02x\n", i, mad_read(i)));
  365. if(detect_mad16()) {
  366. DDB(printk("mad16.c: 82C930 detected\n"));
  367. return 1;
  368. }
  369. /* The C931 has the password reg at F8D */
  370. outb((0xE4), 0xF8D);
  371. outb((0x80), 0xF8D);
  372. DDB(printk("Detect using password = 0xE4 for C931\n"));
  373. if (detect_mad16()) {
  374. return 1;
  375. }
  376. board_type = C924;
  377. c924pnp++;
  378. DDB(printk("Detect using password = 0xE5 (again), port offset -0x80\n"));
  379. if (detect_mad16()) {
  380. DDB(printk("mad16.c: 82C924 PnP detected\n"));
  381. return 1;
  382. }
  383. c924pnp=0;
  384. return 0;
  385. }
  386. static int __init probe_mad16(struct address_info *hw_config)
  387. {
  388. int i;
  389. unsigned char tmp;
  390. unsigned char cs4231_mode = 0;
  391. int ad_flags = 0;
  392. signed char bits;
  393. static char dma_bits[4] = {
  394. 1, 2, 0, 3
  395. };
  396. int config_port = hw_config->io_base + 0, version_port = hw_config->io_base + 3;
  397. int dma = hw_config->dma, dma2 = hw_config->dma2;
  398. unsigned char dma2_bit = 0;
  399. int base;
  400. struct resource *ports;
  401. mad16_osp = hw_config->osp;
  402. switch (hw_config->io_base) {
  403. case 0x530:
  404. base = 0;
  405. break;
  406. case 0xe80:
  407. base = 1;
  408. break;
  409. case 0xf40:
  410. base = 2;
  411. break;
  412. case 0x604:
  413. base = 3;
  414. break;
  415. default:
  416. printk(KERN_ERR "MAD16/Mozart: Bad WSS base address 0x%x\n", hw_config->io_base);
  417. return 0;
  418. }
  419. if (dma != 0 && dma != 1 && dma != 3) {
  420. printk(KERN_ERR "MSS: Bad DMA %d\n", dma);
  421. return 0;
  422. }
  423. /*
  424. * Check that all ports return 0xff (bus float) when no password
  425. * is written to the password register.
  426. */
  427. DDB(printk("--- Detecting MAD16 / Mozart ---\n"));
  428. if (!chip_detect())
  429. return 0;
  430. switch (hw_config->irq) {
  431. case 7:
  432. bits = 8;
  433. break;
  434. case 9:
  435. bits = 0x10;
  436. break;
  437. case 10:
  438. bits = 0x18;
  439. break;
  440. case 12:
  441. bits = 0x20;
  442. break;
  443. case 5: /* Also IRQ5 is possible on C930 */
  444. if (board_type == C930 || c924pnp) {
  445. bits = 0x28;
  446. break;
  447. }
  448. default:
  449. printk(KERN_ERR "MAD16/Mozart: Bad IRQ %d\n", hw_config->irq);
  450. return 0;
  451. }
  452. ports = request_region(hw_config->io_base + 4, 4, "ad1848");
  453. if (!ports) {
  454. printk(KERN_ERR "MSS: I/O port conflict\n");
  455. return 0;
  456. }
  457. if (!request_region(hw_config->io_base, 4, "mad16 WSS config")) {
  458. release_region(hw_config->io_base + 4, 4);
  459. printk(KERN_ERR "MSS: I/O port conflict\n");
  460. return 0;
  461. }
  462. if (board_type == C930) {
  463. init_c930(hw_config, base);
  464. goto got_it;
  465. }
  466. for (i = 0xf8d; i <= 0xf93; i++) {
  467. if (!c924pnp)
  468. DDB(printk("port %03x = %02x\n", i, mad_read(i)));
  469. else
  470. DDB(printk("port %03x = %02x\n", i-0x80, mad_read(i)));
  471. }
  472. /*
  473. * Set the WSS address
  474. */
  475. tmp = (mad_read(MC1_PORT) & 0x0f) | 0x80; /* Enable WSS, Disable SB */
  476. tmp |= base << 4; /* WSS port select bits */
  477. /*
  478. * Set optional CD-ROM and joystick settings.
  479. */
  480. tmp &= ~0x0f;
  481. tmp |= (mad16_conf & 0x0f); /* CD-ROM and joystick bits */
  482. mad_write(MC1_PORT, tmp);
  483. tmp = mad16_cdsel;
  484. mad_write(MC2_PORT, tmp);
  485. mad_write(MC3_PORT, 0xf0); /* Disable SB */
  486. if (board_type == C924) /* Specific C924 init values */
  487. {
  488. mad_write(MC4_PORT, 0xA0);
  489. mad_write(MC5_PORT, 0x05);
  490. mad_write(MC6_PORT, 0x03);
  491. }
  492. if (!ad1848_detect(ports, &ad_flags, mad16_osp))
  493. goto fail;
  494. if (ad_flags & (AD_F_CS4231 | AD_F_CS4248))
  495. cs4231_mode = 0x02; /* CS4248/CS4231 sync delay switch */
  496. if (board_type == C929)
  497. {
  498. mad_write(MC4_PORT, 0xa2);
  499. mad_write(MC5_PORT, 0xA5 | cs4231_mode);
  500. mad_write(MC6_PORT, 0x03); /* Disable MPU401 */
  501. }
  502. else
  503. {
  504. mad_write(MC4_PORT, 0x02);
  505. mad_write(MC5_PORT, 0x30 | cs4231_mode);
  506. }
  507. for (i = 0xf8d; i <= 0xf93; i++) {
  508. if (!c924pnp)
  509. DDB(printk("port %03x after init = %02x\n", i, mad_read(i)));
  510. else
  511. DDB(printk("port %03x after init = %02x\n", i-0x80, mad_read(i)));
  512. }
  513. got_it:
  514. ad_flags = 0;
  515. if (!ad1848_detect(ports, &ad_flags, mad16_osp))
  516. goto fail;
  517. if (!wss_init(hw_config))
  518. goto fail;
  519. /*
  520. * Set the IRQ and DMA addresses.
  521. */
  522. outb((bits | 0x40), config_port);
  523. if ((inb(version_port) & 0x40) == 0)
  524. printk(KERN_ERR "[IRQ Conflict?]\n");
  525. /*
  526. * Handle the capture DMA channel
  527. */
  528. if (ad_flags & AD_F_CS4231 && dma2 != -1 && dma2 != dma)
  529. {
  530. if (!((dma == 0 && dma2 == 1) ||
  531. (dma == 1 && dma2 == 0) ||
  532. (dma == 3 && dma2 == 0)))
  533. { /* Unsupported combination. Try to swap channels */
  534. int tmp = dma;
  535. dma = dma2;
  536. dma2 = tmp;
  537. }
  538. if ((dma == 0 && dma2 == 1) || (dma == 1 && dma2 == 0) ||
  539. (dma == 3 && dma2 == 0))
  540. {
  541. dma2_bit = 0x04; /* Enable capture DMA */
  542. }
  543. else
  544. {
  545. printk("MAD16: Invalid capture DMA\n");
  546. dma2 = dma;
  547. }
  548. }
  549. else dma2 = dma;
  550. outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
  551. hw_config->slots[0] = ad1848_init("mad16 WSS", ports,
  552. hw_config->irq,
  553. dma,
  554. dma2, 0,
  555. hw_config->osp,
  556. THIS_MODULE);
  557. return 1;
  558. fail:
  559. release_region(hw_config->io_base + 4, 4);
  560. release_region(hw_config->io_base, 4);
  561. return 0;
  562. }
  563. static int __init probe_mad16_mpu(struct address_info *hw_config)
  564. {
  565. unsigned char tmp;
  566. if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
  567. {
  568. #ifdef CONFIG_MAD16_OLDCARD
  569. tmp = mad_read(MC3_PORT);
  570. /*
  571. * MAD16 SB base is defined by the WSS base. It cannot be changed
  572. * alone.
  573. * Ignore configured I/O base. Use the active setting.
  574. */
  575. if (mad_read(MC1_PORT) & 0x20)
  576. hw_config->io_base = 0x240;
  577. else
  578. hw_config->io_base = 0x220;
  579. switch (hw_config->irq)
  580. {
  581. case 5:
  582. tmp = (tmp & 0x3f) | 0x80;
  583. break;
  584. case 7:
  585. tmp = (tmp & 0x3f);
  586. break;
  587. case 11:
  588. tmp = (tmp & 0x3f) | 0x40;
  589. break;
  590. default:
  591. printk(KERN_ERR "mad16/Mozart: Invalid MIDI IRQ\n");
  592. return 0;
  593. }
  594. mad_write(MC3_PORT, tmp | 0x04);
  595. hw_config->driver_use_1 = SB_MIDI_ONLY;
  596. if (!request_region(hw_config->io_base, 16, "soundblaster"))
  597. return 0;
  598. if (!sb_dsp_detect(hw_config, 0, 0, NULL)) {
  599. release_region(hw_config->io_base, 16);
  600. return 0;
  601. }
  602. if (mad_read(MC1_PORT) & 0x20)
  603. hw_config->io_base = 0x240;
  604. else
  605. hw_config->io_base = 0x220;
  606. hw_config->name = "Mad16/Mozart";
  607. sb_dsp_init(hw_config, THIS_MODULE);
  608. return 1;
  609. #else
  610. /* assuming all later Mozart cards are identified as
  611. * either 82C928 or Mozart. If so, following code attempts
  612. * to set MPU register. TODO - add probing
  613. */
  614. tmp = mad_read(MC8_PORT);
  615. switch (hw_config->irq)
  616. {
  617. case 5:
  618. tmp |= 0x08;
  619. break;
  620. case 7:
  621. tmp |= 0x10;
  622. break;
  623. case 9:
  624. tmp |= 0x18;
  625. break;
  626. case 10:
  627. tmp |= 0x20;
  628. break;
  629. case 11:
  630. tmp |= 0x28;
  631. break;
  632. default:
  633. printk(KERN_ERR "mad16/MOZART: invalid mpu_irq\n");
  634. return 0;
  635. }
  636. switch (hw_config->io_base)
  637. {
  638. case 0x300:
  639. tmp |= 0x01;
  640. break;
  641. case 0x310:
  642. tmp |= 0x03;
  643. break;
  644. case 0x320:
  645. tmp |= 0x05;
  646. break;
  647. case 0x330:
  648. tmp |= 0x07;
  649. break;
  650. default:
  651. printk(KERN_ERR "mad16/MOZART: invalid mpu_io\n");
  652. return 0;
  653. }
  654. mad_write(MC8_PORT, tmp); /* write MPU port parameters */
  655. goto probe_401;
  656. #endif
  657. }
  658. tmp = mad_read(MC6_PORT) & 0x83;
  659. tmp |= 0x80; /* MPU-401 enable */
  660. /* Set the MPU base bits */
  661. switch (hw_config->io_base)
  662. {
  663. case 0x300:
  664. tmp |= 0x60;
  665. break;
  666. case 0x310:
  667. tmp |= 0x40;
  668. break;
  669. case 0x320:
  670. tmp |= 0x20;
  671. break;
  672. case 0x330:
  673. tmp |= 0x00;
  674. break;
  675. default:
  676. printk(KERN_ERR "MAD16: Invalid MIDI port 0x%x\n", hw_config->io_base);
  677. return 0;
  678. }
  679. /* Set the MPU IRQ bits */
  680. switch (hw_config->irq)
  681. {
  682. case 5:
  683. tmp |= 0x10;
  684. break;
  685. case 7:
  686. tmp |= 0x18;
  687. break;
  688. case 9:
  689. tmp |= 0x00;
  690. break;
  691. case 10:
  692. tmp |= 0x08;
  693. break;
  694. default:
  695. printk(KERN_ERR "MAD16: Invalid MIDI IRQ %d\n", hw_config->irq);
  696. break;
  697. }
  698. mad_write(MC6_PORT, tmp); /* Write MPU401 config */
  699. #ifndef CONFIG_MAD16_OLDCARD
  700. probe_401:
  701. #endif
  702. hw_config->driver_use_1 = SB_MIDI_ONLY;
  703. hw_config->name = "Mad16/Mozart";
  704. return probe_uart401(hw_config, THIS_MODULE);
  705. }
  706. static void __exit unload_mad16(struct address_info *hw_config)
  707. {
  708. ad1848_unload(hw_config->io_base + 4,
  709. hw_config->irq,
  710. hw_config->dma,
  711. hw_config->dma2, 0);
  712. release_region(hw_config->io_base, 4);
  713. sound_unload_audiodev(hw_config->slots[0]);
  714. }
  715. static void __exit unload_mad16_mpu(struct address_info *hw_config)
  716. {
  717. #ifdef CONFIG_MAD16_OLDCARD
  718. if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
  719. {
  720. sb_dsp_unload(hw_config, 0);
  721. return;
  722. }
  723. #endif
  724. unload_uart401(hw_config);
  725. }
  726. static struct address_info cfg;
  727. static struct address_info cfg_mpu;
  728. static int found_mpu;
  729. static int __initdata mpu_io = 0;
  730. static int __initdata mpu_irq = 0;
  731. static int __initdata io = -1;
  732. static int __initdata dma = -1;
  733. static int __initdata dma16 = -1; /* Set this for modules that need it */
  734. static int __initdata irq = -1;
  735. static int __initdata cdtype = 0;
  736. static int __initdata cdirq = 0;
  737. static int __initdata cdport = 0x340;
  738. static int __initdata cddma = -1;
  739. static int __initdata opl4 = 0;
  740. static int __initdata joystick = 0;
  741. module_param(mpu_io, int, 0);
  742. module_param(mpu_irq, int, 0);
  743. module_param(io, int, 0);
  744. module_param(dma, int, 0);
  745. module_param(dma16, int, 0);
  746. module_param(irq, int, 0);
  747. module_param(cdtype, int, 0);
  748. module_param(cdirq, int, 0);
  749. module_param(cdport, int, 0);
  750. module_param(cddma, int, 0);
  751. module_param(opl4, int, 0);
  752. module_param(joystick, bool, 0);
  753. module_param(debug, bool, 0644);
  754. static int __initdata dma_map[2][8] =
  755. {
  756. {0x03, -1, -1, -1, -1, 0x00, 0x01, 0x02},
  757. {0x03, -1, 0x01, 0x00, -1, -1, -1, -1}
  758. };
  759. static int __initdata irq_map[16] =
  760. {
  761. 0x00, -1, -1, 0x0A,
  762. -1, 0x04, -1, 0x08,
  763. -1, 0x10, 0x14, 0x18,
  764. -1, -1, -1, -1
  765. };
  766. static int __devinit mad16_register_gameport(int io_port)
  767. {
  768. if (!request_region(io_port, 1, "mad16 gameport")) {
  769. printk(KERN_ERR "mad16: gameport address 0x%#x already in use\n", io_port);
  770. return -EBUSY;
  771. }
  772. gameport = gameport_allocate_port();
  773. if (!gameport) {
  774. printk(KERN_ERR "mad16: can not allocate memory for gameport\n");
  775. release_region(io_port, 1);
  776. return -ENOMEM;
  777. }
  778. gameport_set_name(gameport, "MAD16 Gameport");
  779. gameport_set_phys(gameport, "isa%04x/gameport0", io_port);
  780. gameport->io = io_port;
  781. gameport_register_port(gameport);
  782. return 0;
  783. }
  784. static int __devinit init_mad16(void)
  785. {
  786. int dmatype = 0;
  787. printk(KERN_INFO "MAD16 audio driver Copyright (C) by Hannu Savolainen 1993-1996\n");
  788. printk(KERN_INFO "CDROM ");
  789. switch (cdtype)
  790. {
  791. case 0x00:
  792. printk("Disabled");
  793. cdirq = 0;
  794. break;
  795. case 0x02:
  796. printk("Sony CDU31A");
  797. dmatype = 1;
  798. if(cddma == -1) cddma = 3;
  799. break;
  800. case 0x04:
  801. printk("Mitsumi");
  802. dmatype = 0;
  803. if(cddma == -1) cddma = 5;
  804. break;
  805. case 0x06:
  806. printk("Panasonic Lasermate");
  807. dmatype = 1;
  808. if(cddma == -1) cddma = 3;
  809. break;
  810. case 0x08:
  811. printk("Secondary IDE");
  812. dmatype = 0;
  813. if(cddma == -1) cddma = 5;
  814. break;
  815. case 0x0A:
  816. printk("Primary IDE");
  817. dmatype = 0;
  818. if(cddma == -1) cddma = 5;
  819. break;
  820. default:
  821. printk("\n");
  822. printk(KERN_ERR "Invalid CDROM type\n");
  823. return -EINVAL;
  824. }
  825. /*
  826. * Build the config words
  827. */
  828. mad16_conf = (joystick ^ 1) | cdtype;
  829. mad16_cdsel = 0;
  830. if (opl4)
  831. mad16_cdsel |= 0x20;
  832. if(cdtype){
  833. if (cddma > 7 || cddma < 0 || dma_map[dmatype][cddma] == -1)
  834. {
  835. printk("\n");
  836. printk(KERN_ERR "Invalid CDROM DMA\n");
  837. return -EINVAL;
  838. }
  839. if (cddma)
  840. printk(", DMA %d", cddma);
  841. else
  842. printk(", no DMA");
  843. if (!cdirq)
  844. printk(", no IRQ");
  845. else if (cdirq < 0 || cdirq > 15 || irq_map[cdirq] == -1)
  846. {
  847. printk(", invalid IRQ (disabling)");
  848. cdirq = 0;
  849. }
  850. else printk(", IRQ %d", cdirq);
  851. mad16_cdsel |= dma_map[dmatype][cddma];
  852. if (cdtype < 0x08)
  853. {
  854. switch (cdport)
  855. {
  856. case 0x340:
  857. mad16_cdsel |= 0x00;
  858. break;
  859. case 0x330:
  860. mad16_cdsel |= 0x40;
  861. break;
  862. case 0x360:
  863. mad16_cdsel |= 0x80;
  864. break;
  865. case 0x320:
  866. mad16_cdsel |= 0xC0;
  867. break;
  868. default:
  869. printk(KERN_ERR "Unknown CDROM I/O base %d\n", cdport);
  870. return -EINVAL;
  871. }
  872. }
  873. mad16_cdsel |= irq_map[cdirq];
  874. }
  875. printk(".\n");
  876. cfg.io_base = io;
  877. cfg.irq = irq;
  878. cfg.dma = dma;
  879. cfg.dma2 = dma16;
  880. if (cfg.io_base == -1 || cfg.dma == -1 || cfg.irq == -1) {
  881. printk(KERN_ERR "I/O, DMA and irq are mandatory\n");
  882. return -EINVAL;
  883. }
  884. if (!request_region(MC0_PORT, 12, "mad16"))
  885. return -EBUSY;
  886. if (!probe_mad16(&cfg)) {
  887. release_region(MC0_PORT, 12);
  888. return -ENODEV;
  889. }
  890. cfg_mpu.io_base = mpu_io;
  891. cfg_mpu.irq = mpu_irq;
  892. found_mpu = probe_mad16_mpu(&cfg_mpu);
  893. if (joystick)
  894. mad16_register_gameport(0x201);
  895. return 0;
  896. }
  897. static void __exit cleanup_mad16(void)
  898. {
  899. if (found_mpu)
  900. unload_mad16_mpu(&cfg_mpu);
  901. if (gameport) {
  902. /* the gameport was initialized so we must free it up */
  903. gameport_unregister_port(gameport);
  904. gameport = NULL;
  905. release_region(0x201, 1);
  906. }
  907. unload_mad16(&cfg);
  908. release_region(MC0_PORT, 12);
  909. }
  910. module_init(init_mad16);
  911. module_exit(cleanup_mad16);
  912. #ifndef MODULE
  913. static int __init setup_mad16(char *str)
  914. {
  915. /* io, irq */
  916. int ints[8];
  917. str = get_options(str, ARRAY_SIZE(ints), ints);
  918. io = ints[1];
  919. irq = ints[2];
  920. dma = ints[3];
  921. dma16 = ints[4];
  922. mpu_io = ints[5];
  923. mpu_irq = ints[6];
  924. joystick = ints[7];
  925. return 1;
  926. }
  927. __setup("mad16=", setup_mad16);
  928. #endif
  929. MODULE_LICENSE("GPL");