xref: /freebsd/sys/dev/sound/pci/ich.c (revision 51a9219f5780e61e1437d25220bf8750d9df7f8b)
1 /*
2  * Copyright (c) 2000 Katsurajima Naoto <raven@katsurajima.seya.yokohama.jp>
3  * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
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, WHETHERIN 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 THEPOSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <dev/sound/pcm/sound.h>
29 #include <dev/sound/pcm/ac97.h>
30 #include <dev/sound/pci/ich.h>
31 
32 #include <pci/pcireg.h>
33 #include <pci/pcivar.h>
34 
35 SND_DECLARE_FILE("$FreeBSD$");
36 
37 /* -------------------------------------------------------------------- */
38 
39 #define ICH_TIMEOUT 1000 /* semaphore timeout polling count */
40 #define ICH_DTBL_LENGTH 32
41 #define ICH_DEFAULT_BUFSZ 16384
42 #define ICH_MAX_BUFSZ 65536
43 
44 #define SIS7012ID       0x70121039      /* SiS 7012 needs special handling */
45 #define ICH4ID		0x24c58086	/* ICH4 needs special handling too */
46 
47 /* buffer descriptor */
48 struct ich_desc {
49 	volatile u_int32_t buffer;
50 	volatile u_int32_t length;
51 };
52 
53 struct sc_info;
54 
55 /* channel registers */
56 struct sc_chinfo {
57 	u_int32_t num:8, run:1, run_save:1;
58 	u_int32_t blksz, blkcnt, spd;
59 	u_int32_t regbase, spdreg;
60 	u_int32_t imask;
61 	u_int32_t civ;
62 
63 	struct snd_dbuf *buffer;
64 	struct pcm_channel *channel;
65 	struct sc_info *parent;
66 
67 	struct ich_desc *dtbl;
68 };
69 
70 /* device private data */
71 struct sc_info {
72 	device_t dev;
73 	int hasvra, hasvrm, hasmic;
74 	unsigned int chnum, bufsz;
75 	int sample_size, swap_reg;
76 
77 	struct resource *nambar, *nabmbar, *irq;
78 	int nambarid, nabmbarid, irqid;
79 	bus_space_tag_t nambart, nabmbart;
80 	bus_space_handle_t nambarh, nabmbarh;
81 	bus_dma_tag_t dmat;
82 	bus_dmamap_t dtmap;
83 	void *ih;
84 
85 	struct ac97_info *codec;
86 	struct sc_chinfo ch[3];
87 	int ac97rate;
88 	struct ich_desc *dtbl;
89 	struct intr_config_hook	intrhook;
90 	int use_intrhook;
91 };
92 
93 /* -------------------------------------------------------------------- */
94 
95 static u_int32_t ich_fmt[] = {
96 	AFMT_STEREO | AFMT_S16_LE,
97 	0
98 };
99 static struct pcmchan_caps ich_vrcaps = {8000, 48000, ich_fmt, 0};
100 static struct pcmchan_caps ich_caps = {48000, 48000, ich_fmt, 0};
101 
102 /* -------------------------------------------------------------------- */
103 /* Hardware */
104 static u_int32_t
105 ich_rd(struct sc_info *sc, int regno, int size)
106 {
107 	switch (size) {
108 	case 1:
109 		return bus_space_read_1(sc->nabmbart, sc->nabmbarh, regno);
110 	case 2:
111 		return bus_space_read_2(sc->nabmbart, sc->nabmbarh, regno);
112 	case 4:
113 		return bus_space_read_4(sc->nabmbart, sc->nabmbarh, regno);
114 	default:
115 		return 0xffffffff;
116 	}
117 }
118 
119 static void
120 ich_wr(struct sc_info *sc, int regno, u_int32_t data, int size)
121 {
122 	switch (size) {
123 	case 1:
124 		bus_space_write_1(sc->nabmbart, sc->nabmbarh, regno, data);
125 		break;
126 	case 2:
127 		bus_space_write_2(sc->nabmbart, sc->nabmbarh, regno, data);
128 		break;
129 	case 4:
130 		bus_space_write_4(sc->nabmbart, sc->nabmbarh, regno, data);
131 		break;
132 	}
133 }
134 
135 /* ac97 codec */
136 static int
137 ich_waitcd(void *devinfo)
138 {
139 	int i;
140 	u_int32_t data;
141 	struct sc_info *sc = (struct sc_info *)devinfo;
142 
143 	for (i = 0; i < ICH_TIMEOUT; i++) {
144 		data = ich_rd(sc, ICH_REG_ACC_SEMA, 1);
145 		if ((data & 0x01) == 0)
146 			return 0;
147 	}
148 	device_printf(sc->dev, "CODEC semaphore timeout\n");
149 	return ETIMEDOUT;
150 }
151 
152 static int
153 ich_rdcd(kobj_t obj, void *devinfo, int regno)
154 {
155 	struct sc_info *sc = (struct sc_info *)devinfo;
156 
157 	regno &= 0xff;
158 	ich_waitcd(sc);
159 
160 	return bus_space_read_2(sc->nambart, sc->nambarh, regno);
161 }
162 
163 static int
164 ich_wrcd(kobj_t obj, void *devinfo, int regno, u_int16_t data)
165 {
166 	struct sc_info *sc = (struct sc_info *)devinfo;
167 
168 	regno &= 0xff;
169 	ich_waitcd(sc);
170 	bus_space_write_2(sc->nambart, sc->nambarh, regno, data);
171 
172 	return 0;
173 }
174 
175 static kobj_method_t ich_ac97_methods[] = {
176 	KOBJMETHOD(ac97_read,		ich_rdcd),
177 	KOBJMETHOD(ac97_write,		ich_wrcd),
178 	{ 0, 0 }
179 };
180 AC97_DECLARE(ich_ac97);
181 
182 /* -------------------------------------------------------------------- */
183 /* common routines */
184 
185 static void
186 ich_filldtbl(struct sc_chinfo *ch)
187 {
188 	u_int32_t base;
189 	int i;
190 
191 	base = vtophys(sndbuf_getbuf(ch->buffer));
192 	ch->blkcnt = sndbuf_getsize(ch->buffer) / ch->blksz;
193 	if (ch->blkcnt != 2 && ch->blkcnt != 4 && ch->blkcnt != 8 && ch->blkcnt != 16 && ch->blkcnt != 32) {
194 		ch->blkcnt = 2;
195 		ch->blksz = sndbuf_getsize(ch->buffer) / ch->blkcnt;
196 	}
197 
198 	for (i = 0; i < ICH_DTBL_LENGTH; i++) {
199 		ch->dtbl[i].buffer = base + (ch->blksz * (i % ch->blkcnt));
200 		ch->dtbl[i].length = ICH_BDC_IOC
201 				   | (ch->blksz / ch->parent->sample_size);
202 	}
203 }
204 
205 static int
206 ich_resetchan(struct sc_info *sc, int num)
207 {
208 	int i, cr, regbase;
209 
210 	if (num == 0)
211 		regbase = ICH_REG_PO_BASE;
212 	else if (num == 1)
213 		regbase = ICH_REG_PI_BASE;
214 	else if (num == 2)
215 		regbase = ICH_REG_MC_BASE;
216 	else
217 		return ENXIO;
218 
219 	ich_wr(sc, regbase + ICH_REG_X_CR, 0, 1);
220 	DELAY(100);
221 	ich_wr(sc, regbase + ICH_REG_X_CR, ICH_X_CR_RR, 1);
222 	for (i = 0; i < ICH_TIMEOUT; i++) {
223 		cr = ich_rd(sc, regbase + ICH_REG_X_CR, 1);
224 		if (cr == 0)
225 			return 0;
226 	}
227 
228 	device_printf(sc->dev, "cannot reset channel %d\n", num);
229 	return ENXIO;
230 }
231 
232 /* -------------------------------------------------------------------- */
233 /* channel interface */
234 
235 static void *
236 ichchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
237 {
238 	struct sc_info *sc = devinfo;
239 	struct sc_chinfo *ch;
240 	unsigned int num;
241 
242 	num = sc->chnum++;
243 	ch = &sc->ch[num];
244 	ch->num = num;
245 	ch->buffer = b;
246 	ch->channel = c;
247 	ch->parent = sc;
248 	ch->run = 0;
249 	ch->dtbl = sc->dtbl + (ch->num * ICH_DTBL_LENGTH);
250 	ch->blkcnt = 2;
251 	ch->blksz = sc->bufsz / ch->blkcnt;
252 
253 	switch(ch->num) {
254 	case 0: /* play */
255 		KASSERT(dir == PCMDIR_PLAY, ("wrong direction"));
256 		ch->regbase = ICH_REG_PO_BASE;
257 		ch->spdreg = sc->hasvra? AC97_REGEXT_FDACRATE : 0;
258 		ch->imask = ICH_GLOB_STA_POINT;
259 		break;
260 
261 	case 1: /* record */
262 		KASSERT(dir == PCMDIR_REC, ("wrong direction"));
263 		ch->regbase = ICH_REG_PI_BASE;
264 		ch->spdreg = sc->hasvra? AC97_REGEXT_LADCRATE : 0;
265 		ch->imask = ICH_GLOB_STA_PIINT;
266 		break;
267 
268 	case 2: /* mic */
269 		KASSERT(dir == PCMDIR_REC, ("wrong direction"));
270 		ch->regbase = ICH_REG_MC_BASE;
271 		ch->spdreg = sc->hasvrm? AC97_REGEXT_MADCRATE : 0;
272 		ch->imask = ICH_GLOB_STA_MINT;
273 		break;
274 
275 	default:
276 		return NULL;
277 	}
278 
279 	if (sndbuf_alloc(ch->buffer, sc->dmat, sc->bufsz))
280 		return NULL;
281 
282 	ich_wr(sc, ch->regbase + ICH_REG_X_BDBAR, (u_int32_t)vtophys(ch->dtbl), 4);
283 
284 	return ch;
285 }
286 
287 static int
288 ichchan_setformat(kobj_t obj, void *data, u_int32_t format)
289 {
290 	return 0;
291 }
292 
293 static int
294 ichchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
295 {
296 	struct sc_chinfo *ch = data;
297 	struct sc_info *sc = ch->parent;
298 
299 	if (ch->spdreg) {
300 		int r;
301 		if (sc->ac97rate <= 32000 || sc->ac97rate >= 64000)
302 			sc->ac97rate = 48000;
303 		r = (speed * 48000) / sc->ac97rate;
304 		/*
305 		 * Cast the return value of ac97_setrate() to u_int so that
306 		 * the math don't overflow into the negative range.
307 		 */
308 		ch->spd = ((u_int)ac97_setrate(sc->codec, ch->spdreg, r) *
309 		    sc->ac97rate) / 48000;
310 	} else {
311 		ch->spd = 48000;
312 	}
313 	return ch->spd;
314 }
315 
316 static int
317 ichchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
318 {
319 	struct sc_chinfo *ch = data;
320 	struct sc_info *sc = ch->parent;
321 
322 	ch->blksz = blocksize;
323 	ich_filldtbl(ch);
324 	ich_wr(sc, ch->regbase + ICH_REG_X_LVI, ch->blkcnt - 1, 1);
325 
326 	return ch->blksz;
327 }
328 
329 static int
330 ichchan_trigger(kobj_t obj, void *data, int go)
331 {
332 	struct sc_chinfo *ch = data;
333 	struct sc_info *sc = ch->parent;
334 
335 	switch (go) {
336 	case PCMTRIG_START:
337 		ch->run = 1;
338 		ich_wr(sc, ch->regbase + ICH_REG_X_BDBAR, (u_int32_t)vtophys(ch->dtbl), 4);
339 		ich_wr(sc, ch->regbase + ICH_REG_X_CR, ICH_X_CR_RPBM | ICH_X_CR_LVBIE | ICH_X_CR_IOCE, 1);
340 		break;
341 
342 	case PCMTRIG_ABORT:
343 		ich_resetchan(sc, ch->num);
344 		ch->run = 0;
345 		break;
346 	}
347 	return 0;
348 }
349 
350 static int
351 ichchan_getptr(kobj_t obj, void *data)
352 {
353 	struct sc_chinfo *ch = data;
354 	struct sc_info *sc = ch->parent;
355       	u_int32_t pos;
356 
357 	ch->civ = ich_rd(sc, ch->regbase + ICH_REG_X_CIV, 1) % ch->blkcnt;
358 
359 	pos = ch->civ * ch->blksz;
360 
361 	return pos;
362 }
363 
364 static struct pcmchan_caps *
365 ichchan_getcaps(kobj_t obj, void *data)
366 {
367 	struct sc_chinfo *ch = data;
368 
369 	return ch->spdreg? &ich_vrcaps : &ich_caps;
370 }
371 
372 static kobj_method_t ichchan_methods[] = {
373 	KOBJMETHOD(channel_init,		ichchan_init),
374 	KOBJMETHOD(channel_setformat,		ichchan_setformat),
375 	KOBJMETHOD(channel_setspeed,		ichchan_setspeed),
376 	KOBJMETHOD(channel_setblocksize,	ichchan_setblocksize),
377 	KOBJMETHOD(channel_trigger,		ichchan_trigger),
378 	KOBJMETHOD(channel_getptr,		ichchan_getptr),
379 	KOBJMETHOD(channel_getcaps,		ichchan_getcaps),
380 	{ 0, 0 }
381 };
382 CHANNEL_DECLARE(ichchan);
383 
384 /* -------------------------------------------------------------------- */
385 /* The interrupt handler */
386 
387 static void
388 ich_intr(void *p)
389 {
390 	struct sc_info *sc = (struct sc_info *)p;
391 	struct sc_chinfo *ch;
392 	u_int32_t cbi, lbi, lvi, st, gs;
393 	int i;
394 
395 	gs = ich_rd(sc, ICH_REG_GLOB_STA, 4) & ICH_GLOB_STA_IMASK;
396 	if (gs & (ICH_GLOB_STA_PRES | ICH_GLOB_STA_SRES)) {
397 		/* Clear resume interrupt(s) - nothing doing with them */
398 		ich_wr(sc, ICH_REG_GLOB_STA, gs, 4);
399 	}
400 	gs &= ~(ICH_GLOB_STA_PRES | ICH_GLOB_STA_SRES);
401 
402 	for (i = 0; i < 3; i++) {
403 		ch = &sc->ch[i];
404 		if ((ch->imask & gs) == 0)
405 			continue;
406 		gs &= ~ch->imask;
407 		st = ich_rd(sc, ch->regbase +
408 				(sc->swap_reg ? ICH_REG_X_PICB : ICH_REG_X_SR),
409 			    2);
410 		st &= ICH_X_SR_FIFOE | ICH_X_SR_BCIS | ICH_X_SR_LVBCI;
411 		if (st & (ICH_X_SR_BCIS | ICH_X_SR_LVBCI)) {
412 				/* block complete - update buffer */
413 			if (ch->run)
414 				chn_intr(ch->channel);
415 			lvi = ich_rd(sc, ch->regbase + ICH_REG_X_LVI, 1);
416 			cbi = ch->civ % ch->blkcnt;
417 			if (cbi == 0)
418 				cbi = ch->blkcnt - 1;
419 			else
420 				cbi--;
421 			lbi = lvi % ch->blkcnt;
422 			if (cbi >= lbi)
423 				lvi += cbi - lbi;
424 			else
425 				lvi += cbi + ch->blkcnt - lbi;
426 			lvi %= ICH_DTBL_LENGTH;
427 			ich_wr(sc, ch->regbase + ICH_REG_X_LVI, lvi, 1);
428 
429 		}
430 		/* clear status bit */
431 		ich_wr(sc, ch->regbase +
432 			   (sc->swap_reg ? ICH_REG_X_PICB : ICH_REG_X_SR),
433 		       st, 2);
434 	}
435 	if (gs != 0) {
436 		device_printf(sc->dev,
437 			      "Unhandled interrupt, gs_intr = %x\n", gs);
438 	}
439 }
440 
441 /* ------------------------------------------------------------------------- */
442 /* Sysctl to control ac97 speed (some boards overclocked ac97). */
443 
444 static int
445 ich_initsys(struct sc_info* sc)
446 {
447 #ifdef SND_DYNSYSCTL
448 	SYSCTL_ADD_INT(snd_sysctl_tree(sc->dev),
449 		       SYSCTL_CHILDREN(snd_sysctl_tree_top(sc->dev)),
450 		       OID_AUTO, "ac97rate", CTLFLAG_RW,
451 		       &sc->ac97rate, 48000,
452 		       "AC97 link rate (default = 48000)");
453 #endif /* SND_DYNSYSCTL */
454 	return 0;
455 }
456 
457 /* -------------------------------------------------------------------- */
458 /* Calibrate card (some boards are overclocked and need scaling) */
459 
460 static
461 void ich_calibrate(void *arg)
462 {
463 	struct sc_info *sc;
464 	struct sc_chinfo *ch;
465 	struct timeval t1, t2;
466 	u_int8_t ociv, nciv;
467 	u_int32_t wait_us, actual_48k_rate, bytes;
468 
469 	sc = (struct sc_info *)arg;
470 	ch = &sc->ch[1];
471 
472 	if (sc->use_intrhook)
473 		config_intrhook_disestablish(&sc->intrhook);
474 
475 	/*
476 	 * Grab audio from input for fixed interval and compare how
477 	 * much we actually get with what we expect.  Interval needs
478 	 * to be sufficiently short that no interrupts are
479 	 * generated.
480 	 */
481 
482 	KASSERT(ch->regbase == ICH_REG_PI_BASE, ("wrong direction"));
483 
484 	bytes = sndbuf_getsize(ch->buffer) / 2;
485 	ichchan_setblocksize(0, ch, bytes);
486 
487 	/*
488 	 * our data format is stereo, 16 bit so each sample is 4 bytes.
489 	 * assuming we get 48000 samples per second, we get 192000 bytes/sec.
490 	 * we're going to start recording with interrupts disabled and measure
491 	 * the time taken for one block to complete.  we know the block size,
492 	 * we know the time in microseconds, we calculate the sample rate:
493 	 *
494 	 * actual_rate [bps] = bytes / (time [s] * 4)
495 	 * actual_rate [bps] = (bytes * 1000000) / (time [us] * 4)
496 	 * actual_rate [Hz] = (bytes * 250000) / time [us]
497 	 */
498 
499 	/* prepare */
500 	ociv = ich_rd(sc, ch->regbase + ICH_REG_X_CIV, 1);
501 	nciv = ociv;
502 	ich_wr(sc, ch->regbase + ICH_REG_X_BDBAR, (u_int32_t)vtophys(ch->dtbl), 4);
503 
504 	/* start */
505 	microtime(&t1);
506 	ich_wr(sc, ch->regbase + ICH_REG_X_CR, ICH_X_CR_RPBM, 1);
507 
508 	/* wait */
509 	while (nciv == ociv) {
510 		microtime(&t2);
511 		if (t2.tv_sec - t1.tv_sec > 1)
512 			break;
513 		nciv = ich_rd(sc, ch->regbase + ICH_REG_X_CIV, 1);
514 	}
515 	microtime(&t2);
516 
517 	/* stop */
518 	ich_wr(sc, ch->regbase + ICH_REG_X_CR, 0, 1);
519 
520 	/* reset */
521 	DELAY(100);
522 	ich_wr(sc, ch->regbase + ICH_REG_X_CR, ICH_X_CR_RR, 1);
523 
524 	/* turn time delta into us */
525 	wait_us = ((t2.tv_sec - t1.tv_sec) * 1000000) + t2.tv_usec - t1.tv_usec;
526 
527 	if (nciv == ociv) {
528 		device_printf(sc->dev, "ac97 link rate calibration timed out after %d us\n", wait_us);
529 		return;
530 	}
531 
532 	actual_48k_rate = (bytes * 250000) / wait_us;
533 
534 	if (actual_48k_rate < 47500 || actual_48k_rate > 48500) {
535 		sc->ac97rate = actual_48k_rate;
536 	} else {
537 		sc->ac97rate = 48000;
538 	}
539 
540 	if (bootverbose || sc->ac97rate != 48000) {
541 		device_printf(sc->dev, "measured ac97 link rate at %d Hz", actual_48k_rate);
542 		if (sc->ac97rate != actual_48k_rate)
543 			printf(", will use %d Hz", sc->ac97rate);
544 	 	printf("\n");
545 	}
546 
547 	return;
548 }
549 
550 /* -------------------------------------------------------------------- */
551 /* Probe and attach the card */
552 
553 static void
554 ich_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
555 {
556 	return;
557 }
558 
559 static int
560 ich_init(struct sc_info *sc)
561 {
562 	u_int32_t stat;
563 	int sz;
564 
565 	ich_wr(sc, ICH_REG_GLOB_CNT, ICH_GLOB_CTL_COLD, 4);
566 	DELAY(600000);
567 	stat = ich_rd(sc, ICH_REG_GLOB_STA, 4);
568 
569 	if ((stat & ICH_GLOB_STA_PCR) == 0) {
570 		/* ICH4 may fail when busmastering is enabled. Continue */
571 		if (pci_get_devid(sc->dev) != ICH4ID) {
572 			return ENXIO;
573 		}
574 	}
575 
576 	ich_wr(sc, ICH_REG_GLOB_CNT, ICH_GLOB_CTL_COLD | ICH_GLOB_CTL_PRES, 4);
577 
578 	if (ich_resetchan(sc, 0) || ich_resetchan(sc, 1))
579 		return ENXIO;
580 	if (sc->hasmic && ich_resetchan(sc, 2))
581 		return ENXIO;
582 
583 	if (bus_dmamem_alloc(sc->dmat, (void **)&sc->dtbl, BUS_DMA_NOWAIT, &sc->dtmap))
584 		return ENOSPC;
585 
586 	sz = sizeof(struct ich_desc) * ICH_DTBL_LENGTH * 3;
587 	if (bus_dmamap_load(sc->dmat, sc->dtmap, sc->dtbl, sz, ich_setmap, NULL, 0)) {
588 		bus_dmamem_free(sc->dmat, (void **)&sc->dtbl, sc->dtmap);
589 		return ENOSPC;
590 	}
591 
592 	return 0;
593 }
594 
595 static int
596 ich_pci_probe(device_t dev)
597 {
598 	switch(pci_get_devid(dev)) {
599 	case 0x71958086:
600 		device_set_desc(dev, "Intel 443MX");
601 		return 0;
602 
603 	case 0x24158086:
604 		device_set_desc(dev, "Intel 82801AA (ICH)");
605 		return 0;
606 
607 	case 0x24258086:
608 		device_set_desc(dev, "Intel 82801AB (ICH)");
609 		return 0;
610 
611 	case 0x24458086:
612 		device_set_desc(dev, "Intel 82801BA (ICH2)");
613 		return 0;
614 
615 	case 0x24858086:
616 		device_set_desc(dev, "Intel 82801CA (ICH3)");
617 		return 0;
618 
619 	case ICH4ID:
620 		device_set_desc(dev, "Intel 82801DB (ICH4)");
621 		return 0;
622 
623 	case SIS7012ID:
624 		device_set_desc(dev, "SiS 7012");
625 		return 0;
626 
627 	case 0x01b110de:
628 		device_set_desc(dev, "Nvidia nForce AC97 controller");
629 		return 0;
630 
631 	default:
632 		return ENXIO;
633 	}
634 }
635 
636 static int
637 ich_pci_attach(device_t dev)
638 {
639 	u_int16_t		extcaps;
640 	struct sc_info 		*sc;
641 	char 			status[SND_STATUSLEN];
642 
643 	if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT)) == NULL) {
644 		device_printf(dev, "cannot allocate softc\n");
645 		return ENXIO;
646 	}
647 
648 	bzero(sc, sizeof(*sc));
649 	sc->dev = dev;
650 
651 	/*
652 	 * The SiS 7012 register set isn't quite like the standard ich.
653 	 * There really should be a general "quirks" mechanism.
654 	 */
655 	if (pci_get_devid(dev) == SIS7012ID) {
656 		sc->swap_reg = 1;
657 		sc->sample_size = 1;
658 	} else {
659 		sc->swap_reg = 0;
660 		sc->sample_size = 2;
661 	}
662 
663 	/*
664 	 * By default, ich4 has NAMBAR and NABMBAR i/o spaces as
665 	 * read-only.  Need to enable "legacy support", by poking into
666 	 * pci config space.  The driver should use MMBAR and MBBAR,
667 	 * but doing so will mess things up here.  ich4 has enough new
668 	 * features it warrants it's own driver.
669 	 */
670 	if (pci_get_devid(dev) == ICH4ID) {
671 		pci_write_config(dev, PCIR_ICH_LEGACY, ICH_LEGACY_ENABLE, 1);
672 	}
673 
674 	pci_enable_io(dev, SYS_RES_IOPORT);
675 	/*
676 	 * Enable bus master. On ich4 this may prevent the detection of
677 	 * the primary codec becoming ready in ich_init().
678 	 */
679 	pci_enable_busmaster(dev);
680 
681 	sc->nambarid = PCIR_NAMBAR;
682 	sc->nabmbarid = PCIR_NABMBAR;
683 	sc->nambar = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->nambarid, 0, ~0, 1, RF_ACTIVE);
684 	sc->nabmbar = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->nabmbarid, 0, ~0, 1, RF_ACTIVE);
685 
686 	if (!sc->nambar || !sc->nabmbar) {
687 		device_printf(dev, "unable to map IO port space\n");
688 		goto bad;
689 	}
690 
691 	sc->nambart = rman_get_bustag(sc->nambar);
692 	sc->nambarh = rman_get_bushandle(sc->nambar);
693 	sc->nabmbart = rman_get_bustag(sc->nabmbar);
694 	sc->nabmbarh = rman_get_bushandle(sc->nabmbar);
695 
696 	sc->bufsz = pcm_getbuffersize(dev, 4096, ICH_DEFAULT_BUFSZ, ICH_MAX_BUFSZ);
697 	if (bus_dma_tag_create(NULL, 8, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
698 			       NULL, NULL, sc->bufsz, 1, 0x3ffff, 0, &sc->dmat) != 0) {
699 		device_printf(dev, "unable to create dma tag\n");
700 		goto bad;
701 	}
702 
703 	sc->irqid = 0;
704 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqid, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
705 	if (!sc->irq || snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ich_intr, sc, &sc->ih)) {
706 		device_printf(dev, "unable to map interrupt\n");
707 		goto bad;
708 	}
709 
710 	if (ich_init(sc)) {
711 		device_printf(dev, "unable to initialize the card\n");
712 		goto bad;
713 	}
714 
715 	sc->codec = AC97_CREATE(dev, sc, ich_ac97);
716 	if (sc->codec == NULL)
717 		goto bad;
718 	mixer_init(dev, ac97_getmixerclass(), sc->codec);
719 
720 	/* check and set VRA function */
721 	extcaps = ac97_getextcaps(sc->codec);
722 	sc->hasvra = extcaps & AC97_EXTCAP_VRA;
723 	sc->hasvrm = extcaps & AC97_EXTCAP_VRM;
724 	sc->hasmic = ac97_getcaps(sc->codec) & AC97_CAP_MICCHANNEL;
725 	ac97_setextmode(sc->codec, sc->hasvra | sc->hasvrm);
726 
727 	if (pcm_register(dev, sc, 1, sc->hasmic? 2 : 1))
728 		goto bad;
729 
730 	pcm_addchan(dev, PCMDIR_PLAY, &ichchan_class, sc);		/* play */
731 	pcm_addchan(dev, PCMDIR_REC, &ichchan_class, sc);		/* record */
732 	if (sc->hasmic)
733 		pcm_addchan(dev, PCMDIR_REC, &ichchan_class, sc);	/* record mic */
734 
735 	snprintf(status, SND_STATUSLEN, "at io 0x%lx, 0x%lx irq %ld bufsz %u",
736 		 rman_get_start(sc->nambar), rman_get_start(sc->nabmbar), rman_get_start(sc->irq), sc->bufsz);
737 
738 	pcm_setstatus(dev, status);
739 
740 	ich_initsys(sc);
741 
742 	sc->intrhook.ich_func = ich_calibrate;
743 	sc->intrhook.ich_arg = sc;
744 	sc->use_intrhook = 1;
745 	if (config_intrhook_establish(&sc->intrhook) != 0) {
746 		device_printf(dev, "Cannot establish calibration hook, will calibrate now\n");
747 		sc->use_intrhook = 0;
748 		ich_calibrate(sc);
749 	}
750 
751 	return 0;
752 
753 bad:
754 	if (sc->codec)
755 		ac97_destroy(sc->codec);
756 	if (sc->ih)
757 		bus_teardown_intr(dev, sc->irq, sc->ih);
758 	if (sc->irq)
759 		bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
760 	if (sc->nambar)
761 		bus_release_resource(dev, SYS_RES_IOPORT,
762 		    sc->nambarid, sc->nambar);
763 	if (sc->nabmbar)
764 		bus_release_resource(dev, SYS_RES_IOPORT,
765 		    sc->nabmbarid, sc->nabmbar);
766 	free(sc, M_DEVBUF);
767 	return ENXIO;
768 }
769 
770 static int
771 ich_pci_detach(device_t dev)
772 {
773 	struct sc_info *sc;
774 	int r;
775 
776 	r = pcm_unregister(dev);
777 	if (r)
778 		return r;
779 	sc = pcm_getdevinfo(dev);
780 
781 	bus_teardown_intr(dev, sc->irq, sc->ih);
782 	bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
783 	bus_release_resource(dev, SYS_RES_IOPORT, sc->nambarid, sc->nambar);
784 	bus_release_resource(dev, SYS_RES_IOPORT, sc->nabmbarid, sc->nabmbar);
785 	bus_dma_tag_destroy(sc->dmat);
786 	free(sc, M_DEVBUF);
787 	return 0;
788 }
789 
790 static int
791 ich_pci_suspend(device_t dev)
792 {
793 	struct sc_info *sc;
794 	int i;
795 
796 	sc = pcm_getdevinfo(dev);
797 	for (i = 0 ; i < 3; i++) {
798 		sc->ch[i].run_save = sc->ch[i].run;
799 		if (sc->ch[i].run) {
800 			ichchan_trigger(0, &sc->ch[i], PCMTRIG_ABORT);
801 		}
802 	}
803 	return 0;
804 }
805 
806 static int
807 ich_pci_resume(device_t dev)
808 {
809 	struct sc_info *sc;
810 	int i;
811 
812 	sc = pcm_getdevinfo(dev);
813 
814 	/* Reinit audio device */
815     	if (ich_init(sc) == -1) {
816 		device_printf(dev, "unable to reinitialize the card\n");
817 		return ENXIO;
818 	}
819 	/* Reinit mixer */
820     	if (mixer_reinit(dev) == -1) {
821 		device_printf(dev, "unable to reinitialize the mixer\n");
822 		return ENXIO;
823 	}
824 	/* Re-start DMA engines */
825 	for (i = 0 ; i < 3; i++) {
826 		struct sc_chinfo *ch = &sc->ch[i];
827 		if (sc->ch[i].run_save) {
828 			ichchan_setblocksize(0, ch, ch->blksz);
829 			ichchan_setspeed(0, ch, ch->spd);
830 			ichchan_trigger(0, ch, PCMTRIG_START);
831 		}
832 	}
833 	return 0;
834 }
835 
836 static device_method_t ich_methods[] = {
837 	/* Device interface */
838 	DEVMETHOD(device_probe,		ich_pci_probe),
839 	DEVMETHOD(device_attach,	ich_pci_attach),
840 	DEVMETHOD(device_detach,	ich_pci_detach),
841 	DEVMETHOD(device_suspend, 	ich_pci_suspend),
842 	DEVMETHOD(device_resume,	ich_pci_resume),
843 	{ 0, 0 }
844 };
845 
846 static driver_t ich_driver = {
847 	"pcm",
848 	ich_methods,
849 	PCM_SOFTC_SIZE,
850 };
851 
852 DRIVER_MODULE(snd_ich, pci, ich_driver, pcm_devclass, 0, 0);
853 MODULE_DEPEND(snd_ich, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
854 MODULE_VERSION(snd_ich, 1);
855