xref: /freebsd/sys/dev/sound/pci/ich.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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 <dev/pci/pcireg.h>
33 #include <dev/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 INTEL_VENDORID	0x8086
45 #define SIS_VENDORID	0x1039
46 #define NVIDIA_VENDORID	0x10de
47 #define AMD_VENDORID	0x1022
48 
49 #define INTEL_82440MX	0x7195
50 #define INTEL_82801AA	0x2415
51 #define INTEL_82801AB	0x2425
52 #define INTEL_82801BA	0x2445
53 #define INTEL_82801CA	0x2485
54 #define INTEL_82801DB	0x24c5	/* ICH4 needs special handling */
55 #define INTEL_82801EB	0x24d5	/* ICH5 needs to be treated as ICH4 */
56 #define INTEL_6300ESB	0x25a6	/* 6300ESB needs to be treated as ICH4 */
57 #define INTEL_82801FB	0x266e	/* ICH6 needs to be treated as ICH4 */
58 #define INTEL_82801GB	0x27de	/* ICH7 needs to be treated as ICH4 */
59 #define SIS_7012	0x7012	/* SiS 7012 needs special handling */
60 #define NVIDIA_NFORCE	0x01b1
61 #define NVIDIA_NFORCE2	0x006a
62 #define NVIDIA_NFORCE2_400	0x008a
63 #define NVIDIA_NFORCE3	0x00da
64 #define NVIDIA_NFORCE3_250	0x00ea
65 #define NVIDIA_NFORCE4	0x0059
66 #define AMD_768		0x7445
67 #define AMD_8111	0x746d
68 
69 #define ICH_LOCK(sc)		snd_mtxlock((sc)->ich_lock)
70 #define ICH_UNLOCK(sc)		snd_mtxunlock((sc)->ich_lock)
71 #define ICH_LOCK_ASSERT(sc)	snd_mtxassert((sc)->ich_lock)
72 
73 static const struct ich_type {
74         uint16_t	vendor;
75         uint16_t	devid;
76 	uint32_t	options;
77 #define PROBE_LOW	0x01
78         char		*name;
79 } ich_devs[] = {
80 	{ INTEL_VENDORID,	INTEL_82440MX,	0,
81 		"Intel 440MX" },
82 	{ INTEL_VENDORID,	INTEL_82801AA,	0,
83 		"Intel ICH (82801AA)" },
84 	{ INTEL_VENDORID,	INTEL_82801AB,	0,
85 		"Intel ICH (82801AB)" },
86 	{ INTEL_VENDORID,	INTEL_82801BA,	0,
87 		"Intel ICH2 (82801BA)" },
88 	{ INTEL_VENDORID,	INTEL_82801CA,	0,
89 		"Intel ICH3 (82801CA)" },
90 	{ INTEL_VENDORID,	INTEL_82801DB,	PROBE_LOW,
91 		"Intel ICH4 (82801DB)" },
92 	{ INTEL_VENDORID,	INTEL_82801EB,	PROBE_LOW,
93 		"Intel ICH5 (82801EB)" },
94 	{ INTEL_VENDORID,	INTEL_6300ESB,	PROBE_LOW,
95 		"Intel 6300ESB" },
96 	{ INTEL_VENDORID,	INTEL_82801FB,	PROBE_LOW,
97 		"Intel ICH6 (82801FB)" },
98 	{ INTEL_VENDORID,	INTEL_82801GB,	PROBE_LOW,
99 		"Intel ICH7 (82801GB)" },
100 	{ SIS_VENDORID,		SIS_7012,	0,
101 		"SiS 7012" },
102 	{ NVIDIA_VENDORID,	NVIDIA_NFORCE,	0,
103 		"nVidia nForce" },
104 	{ NVIDIA_VENDORID,	NVIDIA_NFORCE2,	0,
105 		"nVidia nForce2" },
106 	{ NVIDIA_VENDORID,	NVIDIA_NFORCE2_400,	0,
107 		"nVidia nForce2 400" },
108 	{ NVIDIA_VENDORID,	NVIDIA_NFORCE3,	0,
109 		"nVidia nForce3" },
110 	{ NVIDIA_VENDORID,	NVIDIA_NFORCE3_250,	0,
111 		"nVidia nForce3 250" },
112 	{ NVIDIA_VENDORID,	NVIDIA_NFORCE4,	0,
113 		"nVidia nForce4" },
114 	{ AMD_VENDORID,		AMD_768,	0,
115 		"AMD-768" },
116 	{ AMD_VENDORID,		AMD_8111,	0,
117 		"AMD-8111" }
118 };
119 
120 /* buffer descriptor */
121 struct ich_desc {
122 	volatile u_int32_t buffer;
123 	volatile u_int32_t length;
124 };
125 
126 struct sc_info;
127 
128 /* channel registers */
129 struct sc_chinfo {
130 	u_int32_t num:8, run:1, run_save:1;
131 	u_int32_t blksz, blkcnt, spd;
132 	u_int32_t regbase, spdreg;
133 	u_int32_t imask;
134 	u_int32_t civ;
135 
136 	struct snd_dbuf *buffer;
137 	struct pcm_channel *channel;
138 	struct sc_info *parent;
139 
140 	struct ich_desc *dtbl;
141 	bus_addr_t desc_addr;
142 };
143 
144 /* device private data */
145 struct sc_info {
146 	device_t dev;
147 	int hasvra, hasvrm, hasmic;
148 	unsigned int chnum, bufsz;
149 	int sample_size, swap_reg;
150 
151 	struct resource *nambar, *nabmbar, *irq;
152 	int regtype, nambarid, nabmbarid, irqid;
153 	bus_space_tag_t nambart, nabmbart;
154 	bus_space_handle_t nambarh, nabmbarh;
155 	bus_dma_tag_t dmat;
156 	bus_dmamap_t dtmap;
157 	void *ih;
158 
159 	struct ac97_info *codec;
160 	struct sc_chinfo ch[3];
161 	int ac97rate;
162 	struct ich_desc *dtbl;
163 	bus_addr_t desc_addr;
164 	struct intr_config_hook	intrhook;
165 	int use_intrhook;
166 	uint16_t vendor;
167 	uint16_t devid;
168 	uint32_t flags;
169 #define IGNORE_PCR	0x01
170 	struct mtx *ich_lock;
171 };
172 
173 /* -------------------------------------------------------------------- */
174 
175 static u_int32_t ich_fmt[] = {
176 	AFMT_STEREO | AFMT_S16_LE,
177 	0
178 };
179 static struct pcmchan_caps ich_vrcaps = {8000, 48000, ich_fmt, 0};
180 static struct pcmchan_caps ich_caps = {48000, 48000, ich_fmt, 0};
181 
182 /* -------------------------------------------------------------------- */
183 /* Hardware */
184 static __inline u_int32_t
185 ich_rd(struct sc_info *sc, int regno, int size)
186 {
187 	switch (size) {
188 	case 1:
189 		return bus_space_read_1(sc->nabmbart, sc->nabmbarh, regno);
190 	case 2:
191 		return bus_space_read_2(sc->nabmbart, sc->nabmbarh, regno);
192 	case 4:
193 		return bus_space_read_4(sc->nabmbart, sc->nabmbarh, regno);
194 	default:
195 		return 0xffffffff;
196 	}
197 }
198 
199 static __inline void
200 ich_wr(struct sc_info *sc, int regno, u_int32_t data, int size)
201 {
202 	switch (size) {
203 	case 1:
204 		bus_space_write_1(sc->nabmbart, sc->nabmbarh, regno, data);
205 		break;
206 	case 2:
207 		bus_space_write_2(sc->nabmbart, sc->nabmbarh, regno, data);
208 		break;
209 	case 4:
210 		bus_space_write_4(sc->nabmbart, sc->nabmbarh, regno, data);
211 		break;
212 	}
213 }
214 
215 /* ac97 codec */
216 static int
217 ich_waitcd(void *devinfo)
218 {
219 	int i;
220 	u_int32_t data;
221 	struct sc_info *sc = (struct sc_info *)devinfo;
222 
223 	for (i = 0; i < ICH_TIMEOUT; i++) {
224 		data = ich_rd(sc, ICH_REG_ACC_SEMA, 1);
225 		if ((data & 0x01) == 0)
226 			return 0;
227 		DELAY(1);
228 	}
229 	if ((sc->flags & IGNORE_PCR) != 0)
230 		return (0);
231 	device_printf(sc->dev, "CODEC semaphore timeout\n");
232 	return ETIMEDOUT;
233 }
234 
235 static int
236 ich_rdcd(kobj_t obj, void *devinfo, int regno)
237 {
238 	struct sc_info *sc = (struct sc_info *)devinfo;
239 
240 	regno &= 0xff;
241 	ich_waitcd(sc);
242 
243 	return bus_space_read_2(sc->nambart, sc->nambarh, regno);
244 }
245 
246 static int
247 ich_wrcd(kobj_t obj, void *devinfo, int regno, u_int16_t data)
248 {
249 	struct sc_info *sc = (struct sc_info *)devinfo;
250 
251 	regno &= 0xff;
252 	ich_waitcd(sc);
253 	bus_space_write_2(sc->nambart, sc->nambarh, regno, data);
254 
255 	return 0;
256 }
257 
258 static kobj_method_t ich_ac97_methods[] = {
259 	KOBJMETHOD(ac97_read,		ich_rdcd),
260 	KOBJMETHOD(ac97_write,		ich_wrcd),
261 	{ 0, 0 }
262 };
263 AC97_DECLARE(ich_ac97);
264 
265 /* -------------------------------------------------------------------- */
266 /* common routines */
267 
268 static void
269 ich_filldtbl(struct sc_chinfo *ch)
270 {
271 	struct sc_info *sc = ch->parent;
272 	u_int32_t base;
273 	int i;
274 
275 	base = sndbuf_getbufaddr(ch->buffer);
276 	if (ch->blksz > sc->bufsz / ch->blkcnt)
277 		ch->blksz = sc->bufsz / ch->blkcnt;
278 	sndbuf_resize(ch->buffer, ch->blkcnt, ch->blksz);
279 	ch->blksz = sndbuf_getblksz(ch->buffer);
280 
281 	for (i = 0; i < ICH_DTBL_LENGTH; i++) {
282 		ch->dtbl[i].buffer = base + (ch->blksz * (i % ch->blkcnt));
283 		ch->dtbl[i].length = ICH_BDC_IOC
284 				   | (ch->blksz / ch->parent->sample_size);
285 	}
286 }
287 
288 static int
289 ich_resetchan(struct sc_info *sc, int num)
290 {
291 	int i, cr, regbase;
292 
293 	if (num == 0)
294 		regbase = ICH_REG_PO_BASE;
295 	else if (num == 1)
296 		regbase = ICH_REG_PI_BASE;
297 	else if (num == 2)
298 		regbase = ICH_REG_MC_BASE;
299 	else
300 		return ENXIO;
301 
302 	ich_wr(sc, regbase + ICH_REG_X_CR, 0, 1);
303 #if 1
304 	/* This may result in no sound output on NForce 2 MBs, see PR 73987 */
305 	DELAY(100);
306 #else
307 	(void)ich_rd(sc, regbase + ICH_REG_X_CR, 1);
308 #endif
309 	ich_wr(sc, regbase + ICH_REG_X_CR, ICH_X_CR_RR, 1);
310 	for (i = 0; i < ICH_TIMEOUT; i++) {
311 		cr = ich_rd(sc, regbase + ICH_REG_X_CR, 1);
312 		if (cr == 0)
313 			return 0;
314 	}
315 
316 	device_printf(sc->dev, "cannot reset channel %d\n", num);
317 	return ENXIO;
318 }
319 
320 /* -------------------------------------------------------------------- */
321 /* channel interface */
322 
323 static void *
324 ichchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
325 {
326 	struct sc_info *sc = devinfo;
327 	struct sc_chinfo *ch;
328 	unsigned int num;
329 
330 	ICH_LOCK(sc);
331 	num = sc->chnum++;
332 	ch = &sc->ch[num];
333 	ch->num = num;
334 	ch->buffer = b;
335 	ch->channel = c;
336 	ch->parent = sc;
337 	ch->run = 0;
338 	ch->dtbl = sc->dtbl + (ch->num * ICH_DTBL_LENGTH);
339 	ch->desc_addr = sc->desc_addr + (ch->num * ICH_DTBL_LENGTH) *
340 		sizeof(struct ich_desc);
341 	ch->blkcnt = 2;
342 	ch->blksz = sc->bufsz / ch->blkcnt;
343 
344 	switch(ch->num) {
345 	case 0: /* play */
346 		KASSERT(dir == PCMDIR_PLAY, ("wrong direction"));
347 		ch->regbase = ICH_REG_PO_BASE;
348 		ch->spdreg = sc->hasvra? AC97_REGEXT_FDACRATE : 0;
349 		ch->imask = ICH_GLOB_STA_POINT;
350 		break;
351 
352 	case 1: /* record */
353 		KASSERT(dir == PCMDIR_REC, ("wrong direction"));
354 		ch->regbase = ICH_REG_PI_BASE;
355 		ch->spdreg = sc->hasvra? AC97_REGEXT_LADCRATE : 0;
356 		ch->imask = ICH_GLOB_STA_PIINT;
357 		break;
358 
359 	case 2: /* mic */
360 		KASSERT(dir == PCMDIR_REC, ("wrong direction"));
361 		ch->regbase = ICH_REG_MC_BASE;
362 		ch->spdreg = sc->hasvrm? AC97_REGEXT_MADCRATE : 0;
363 		ch->imask = ICH_GLOB_STA_MINT;
364 		break;
365 
366 	default:
367 		return NULL;
368 	}
369 
370 	ICH_UNLOCK(sc);
371 	if (sndbuf_alloc(ch->buffer, sc->dmat, sc->bufsz) != 0)
372 		return NULL;
373 
374 	ICH_LOCK(sc);
375 	ich_wr(sc, ch->regbase + ICH_REG_X_BDBAR, (u_int32_t)(ch->desc_addr), 4);
376 	ICH_UNLOCK(sc);
377 
378 	return ch;
379 }
380 
381 static int
382 ichchan_setformat(kobj_t obj, void *data, u_int32_t format)
383 {
384 	return 0;
385 }
386 
387 static int
388 ichchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
389 {
390 	struct sc_chinfo *ch = data;
391 	struct sc_info *sc = ch->parent;
392 
393 	if (ch->spdreg) {
394 		int r, ac97rate;
395 
396 		ICH_LOCK(sc);
397 		if (sc->ac97rate <= 32000 || sc->ac97rate >= 64000)
398 			sc->ac97rate = 48000;
399 		ac97rate = sc->ac97rate;
400 		ICH_UNLOCK(sc);
401 		r = (speed * 48000) / ac97rate;
402 		/*
403 		 * Cast the return value of ac97_setrate() to u_int so that
404 		 * the math don't overflow into the negative range.
405 		 */
406 		ch->spd = ((u_int)ac97_setrate(sc->codec, ch->spdreg, r) *
407 				ac97rate) / 48000;
408 	} else {
409 		ch->spd = 48000;
410 	}
411 	return ch->spd;
412 }
413 
414 static int
415 ichchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
416 {
417 	struct sc_chinfo *ch = data;
418 	struct sc_info *sc = ch->parent;
419 
420 	ch->blksz = blocksize;
421 	ich_filldtbl(ch);
422 	ICH_LOCK(sc);
423 	ich_wr(sc, ch->regbase + ICH_REG_X_LVI, ch->blkcnt - 1, 1);
424 	ICH_UNLOCK(sc);
425 
426 	return ch->blksz;
427 }
428 
429 static int
430 ichchan_trigger(kobj_t obj, void *data, int go)
431 {
432 	struct sc_chinfo *ch = data;
433 	struct sc_info *sc = ch->parent;
434 
435 	switch (go) {
436 	case PCMTRIG_START:
437 		ch->run = 1;
438 		ICH_LOCK(sc);
439 		ich_wr(sc, ch->regbase + ICH_REG_X_BDBAR, (u_int32_t)(ch->desc_addr), 4);
440 		ich_wr(sc, ch->regbase + ICH_REG_X_CR, ICH_X_CR_RPBM | ICH_X_CR_LVBIE | ICH_X_CR_IOCE, 1);
441 		ICH_UNLOCK(sc);
442 		break;
443 
444 	case PCMTRIG_ABORT:
445 		ICH_LOCK(sc);
446 		ich_resetchan(sc, ch->num);
447 		ICH_UNLOCK(sc);
448 		ch->run = 0;
449 		break;
450 	}
451 	return 0;
452 }
453 
454 static int
455 ichchan_getptr(kobj_t obj, void *data)
456 {
457 	struct sc_chinfo *ch = data;
458 	struct sc_info *sc = ch->parent;
459       	u_int32_t pos;
460 
461 	ICH_LOCK(sc);
462 	ch->civ = ich_rd(sc, ch->regbase + ICH_REG_X_CIV, 1) % ch->blkcnt;
463 	ICH_UNLOCK(sc);
464 
465 	pos = ch->civ * ch->blksz;
466 
467 	return pos;
468 }
469 
470 static struct pcmchan_caps *
471 ichchan_getcaps(kobj_t obj, void *data)
472 {
473 	struct sc_chinfo *ch = data;
474 
475 	return ch->spdreg? &ich_vrcaps : &ich_caps;
476 }
477 
478 static kobj_method_t ichchan_methods[] = {
479 	KOBJMETHOD(channel_init,		ichchan_init),
480 	KOBJMETHOD(channel_setformat,		ichchan_setformat),
481 	KOBJMETHOD(channel_setspeed,		ichchan_setspeed),
482 	KOBJMETHOD(channel_setblocksize,	ichchan_setblocksize),
483 	KOBJMETHOD(channel_trigger,		ichchan_trigger),
484 	KOBJMETHOD(channel_getptr,		ichchan_getptr),
485 	KOBJMETHOD(channel_getcaps,		ichchan_getcaps),
486 	{ 0, 0 }
487 };
488 CHANNEL_DECLARE(ichchan);
489 
490 /* -------------------------------------------------------------------- */
491 /* The interrupt handler */
492 
493 static void
494 ich_intr(void *p)
495 {
496 	struct sc_info *sc = (struct sc_info *)p;
497 	struct sc_chinfo *ch;
498 	u_int32_t cbi, lbi, lvi, st, gs;
499 	int i;
500 
501 	ICH_LOCK(sc);
502 	gs = ich_rd(sc, ICH_REG_GLOB_STA, 4) & ICH_GLOB_STA_IMASK;
503 	if (gs & (ICH_GLOB_STA_PRES | ICH_GLOB_STA_SRES)) {
504 		/* Clear resume interrupt(s) - nothing doing with them */
505 		ich_wr(sc, ICH_REG_GLOB_STA, gs, 4);
506 	}
507 	gs &= ~(ICH_GLOB_STA_PRES | ICH_GLOB_STA_SRES);
508 
509 	for (i = 0; i < 3; i++) {
510 		ch = &sc->ch[i];
511 		if ((ch->imask & gs) == 0)
512 			continue;
513 		gs &= ~ch->imask;
514 		st = ich_rd(sc, ch->regbase +
515 				(sc->swap_reg ? ICH_REG_X_PICB : ICH_REG_X_SR),
516 			    2);
517 		st &= ICH_X_SR_FIFOE | ICH_X_SR_BCIS | ICH_X_SR_LVBCI;
518 		if (st & (ICH_X_SR_BCIS | ICH_X_SR_LVBCI)) {
519 				/* block complete - update buffer */
520 			if (ch->run) {
521 				ICH_UNLOCK(sc);
522 				chn_intr(ch->channel);
523 				ICH_LOCK(sc);
524 			}
525 			lvi = ich_rd(sc, ch->regbase + ICH_REG_X_LVI, 1);
526 			cbi = ch->civ % ch->blkcnt;
527 			if (cbi == 0)
528 				cbi = ch->blkcnt - 1;
529 			else
530 				cbi--;
531 			lbi = lvi % ch->blkcnt;
532 			if (cbi >= lbi)
533 				lvi += cbi - lbi;
534 			else
535 				lvi += cbi + ch->blkcnt - lbi;
536 			lvi %= ICH_DTBL_LENGTH;
537 			ich_wr(sc, ch->regbase + ICH_REG_X_LVI, lvi, 1);
538 
539 		}
540 		/* clear status bit */
541 		ich_wr(sc, ch->regbase +
542 			   (sc->swap_reg ? ICH_REG_X_PICB : ICH_REG_X_SR),
543 		       st, 2);
544 	}
545 	ICH_UNLOCK(sc);
546 	if (gs != 0) {
547 		device_printf(sc->dev,
548 			      "Unhandled interrupt, gs_intr = %x\n", gs);
549 	}
550 }
551 
552 /* ------------------------------------------------------------------------- */
553 /* Sysctl to control ac97 speed (some boards appear to end up using
554  * XTAL_IN rather than BIT_CLK for link timing).
555  */
556 
557 static int
558 ich_initsys(struct sc_info* sc)
559 {
560 #ifdef SND_DYNSYSCTL
561 	SYSCTL_ADD_INT(snd_sysctl_tree(sc->dev),
562 		       SYSCTL_CHILDREN(snd_sysctl_tree_top(sc->dev)),
563 		       OID_AUTO, "ac97rate", CTLFLAG_RW,
564 		       &sc->ac97rate, 48000,
565 		       "AC97 link rate (default = 48000)");
566 #endif /* SND_DYNSYSCTL */
567 	return 0;
568 }
569 
570 /* -------------------------------------------------------------------- */
571 /* Calibrate card to determine the clock source.  The source maybe a
572  * function of the ac97 codec initialization code (to be investigated).
573  */
574 
575 static
576 void ich_calibrate(void *arg)
577 {
578 	struct sc_info *sc;
579 	struct sc_chinfo *ch;
580 	struct timeval t1, t2;
581 	u_int8_t ociv, nciv;
582 	u_int32_t wait_us, actual_48k_rate, bytes;
583 
584 	sc = (struct sc_info *)arg;
585 	ch = &sc->ch[1];
586 
587 	if (sc->use_intrhook)
588 		config_intrhook_disestablish(&sc->intrhook);
589 
590 	/*
591 	 * Grab audio from input for fixed interval and compare how
592 	 * much we actually get with what we expect.  Interval needs
593 	 * to be sufficiently short that no interrupts are
594 	 * generated.
595 	 */
596 
597 	KASSERT(ch->regbase == ICH_REG_PI_BASE, ("wrong direction"));
598 
599 	bytes = sndbuf_getsize(ch->buffer) / 2;
600 	ichchan_setblocksize(0, ch, bytes);
601 
602 	/*
603 	 * our data format is stereo, 16 bit so each sample is 4 bytes.
604 	 * assuming we get 48000 samples per second, we get 192000 bytes/sec.
605 	 * we're going to start recording with interrupts disabled and measure
606 	 * the time taken for one block to complete.  we know the block size,
607 	 * we know the time in microseconds, we calculate the sample rate:
608 	 *
609 	 * actual_rate [bps] = bytes / (time [s] * 4)
610 	 * actual_rate [bps] = (bytes * 1000000) / (time [us] * 4)
611 	 * actual_rate [Hz] = (bytes * 250000) / time [us]
612 	 */
613 
614 	/* prepare */
615 	ociv = ich_rd(sc, ch->regbase + ICH_REG_X_CIV, 1);
616 	nciv = ociv;
617 	ich_wr(sc, ch->regbase + ICH_REG_X_BDBAR, (u_int32_t)(ch->desc_addr), 4);
618 
619 	/* start */
620 	microtime(&t1);
621 	ich_wr(sc, ch->regbase + ICH_REG_X_CR, ICH_X_CR_RPBM, 1);
622 
623 	/* wait */
624 	while (nciv == ociv) {
625 		microtime(&t2);
626 		if (t2.tv_sec - t1.tv_sec > 1)
627 			break;
628 		nciv = ich_rd(sc, ch->regbase + ICH_REG_X_CIV, 1);
629 	}
630 	microtime(&t2);
631 
632 	/* stop */
633 	ich_wr(sc, ch->regbase + ICH_REG_X_CR, 0, 1);
634 
635 	/* reset */
636 	DELAY(100);
637 	ich_wr(sc, ch->regbase + ICH_REG_X_CR, ICH_X_CR_RR, 1);
638 
639 	/* turn time delta into us */
640 	wait_us = ((t2.tv_sec - t1.tv_sec) * 1000000) + t2.tv_usec - t1.tv_usec;
641 
642 	if (nciv == ociv) {
643 		device_printf(sc->dev, "ac97 link rate calibration timed out after %d us\n", wait_us);
644 		return;
645 	}
646 
647 	actual_48k_rate = (bytes * 250000) / wait_us;
648 
649 	if (actual_48k_rate < 47500 || actual_48k_rate > 48500) {
650 		sc->ac97rate = actual_48k_rate;
651 	} else {
652 		sc->ac97rate = 48000;
653 	}
654 
655 	if (bootverbose || sc->ac97rate != 48000) {
656 		device_printf(sc->dev, "measured ac97 link rate at %d Hz", actual_48k_rate);
657 		if (sc->ac97rate != actual_48k_rate)
658 			printf(", will use %d Hz", sc->ac97rate);
659 	 	printf("\n");
660 	}
661 
662 	return;
663 }
664 
665 /* -------------------------------------------------------------------- */
666 /* Probe and attach the card */
667 
668 static void
669 ich_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
670 {
671 	struct sc_info *sc = (struct sc_info *)arg;
672 	sc->desc_addr = segs->ds_addr;
673 	return;
674 }
675 
676 static int
677 ich_init(struct sc_info *sc)
678 {
679 	u_int32_t stat;
680 
681 	ich_wr(sc, ICH_REG_GLOB_CNT, ICH_GLOB_CTL_COLD, 4);
682 	DELAY(600000);
683 	stat = ich_rd(sc, ICH_REG_GLOB_STA, 4);
684 
685 	if ((stat & ICH_GLOB_STA_PCR) == 0) {
686 		/* ICH4/ICH5 may fail when busmastering is enabled. Continue */
687 		if (sc->vendor == INTEL_VENDORID && (
688 		    sc->devid == INTEL_82801DB || sc->devid == INTEL_82801EB ||
689 		    sc->devid == INTEL_6300ESB || sc->devid == INTEL_82801FB ||
690 		    sc->devid == INTEL_82801GB)) {
691 			sc->flags |= IGNORE_PCR;
692 			device_printf(sc->dev, "primary codec not ready!\n");
693 		}
694 	}
695 
696 	ich_wr(sc, ICH_REG_GLOB_CNT, ICH_GLOB_CTL_COLD | ICH_GLOB_CTL_PRES, 4);
697 
698 	if (ich_resetchan(sc, 0) || ich_resetchan(sc, 1))
699 		return ENXIO;
700 	if (sc->hasmic && ich_resetchan(sc, 2))
701 		return ENXIO;
702 
703 	return 0;
704 }
705 
706 static int
707 ich_pci_probe(device_t dev)
708 {
709 	int i;
710 	uint16_t devid, vendor;
711 
712 	vendor = pci_get_vendor(dev);
713 	devid = pci_get_device(dev);
714 	for (i = 0; i < sizeof(ich_devs)/sizeof(ich_devs[0]); i++) {
715 		if (vendor == ich_devs[i].vendor &&
716 				devid == ich_devs[i].devid) {
717 			device_set_desc(dev, ich_devs[i].name);
718 			/* allow a better driver to override us */
719 			if ((ich_devs[i].options & PROBE_LOW) != 0)
720 				return (BUS_PROBE_LOW_PRIORITY);
721 			return (BUS_PROBE_DEFAULT);
722 		}
723 	}
724 	return (ENXIO);
725 }
726 
727 static int
728 ich_pci_attach(device_t dev)
729 {
730 	uint32_t		subdev;
731 	u_int16_t		extcaps;
732 	uint16_t		devid, vendor;
733 	struct sc_info 		*sc;
734 	char 			status[SND_STATUSLEN];
735 
736 	if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
737 		device_printf(dev, "cannot allocate softc\n");
738 		return ENXIO;
739 	}
740 
741 	sc->ich_lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
742 	sc->dev = dev;
743 
744 	vendor = sc->vendor = pci_get_vendor(dev);
745 	devid = sc->devid = pci_get_device(dev);
746 	subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
747 	/*
748 	 * The SiS 7012 register set isn't quite like the standard ich.
749 	 * There really should be a general "quirks" mechanism.
750 	 */
751 	if (vendor == SIS_VENDORID && devid == SIS_7012) {
752 		sc->swap_reg = 1;
753 		sc->sample_size = 1;
754 	} else {
755 		sc->swap_reg = 0;
756 		sc->sample_size = 2;
757 	}
758 
759 	/*
760 	 * Enable bus master. On ich4/5 this may prevent the detection of
761 	 * the primary codec becoming ready in ich_init().
762 	 */
763 	pci_enable_busmaster(dev);
764 
765 	/*
766 	 * By default, ich4 has NAMBAR and NABMBAR i/o spaces as
767 	 * read-only.  Need to enable "legacy support", by poking into
768 	 * pci config space.  The driver should use MMBAR and MBBAR,
769 	 * but doing so will mess things up here.  ich4 has enough new
770 	 * features it warrants it's own driver.
771 	 */
772 	if (vendor == INTEL_VENDORID && (devid == INTEL_82801DB ||
773 	    devid == INTEL_82801EB || devid == INTEL_6300ESB ||
774 	    devid == INTEL_82801FB || devid == INTEL_82801GB)) {
775 		sc->nambarid = PCIR_MMBAR;
776 		sc->nabmbarid = PCIR_MBBAR;
777 		sc->regtype = SYS_RES_MEMORY;
778 		pci_write_config(dev, PCIR_ICH_LEGACY, ICH_LEGACY_ENABLE, 1);
779 	} else {
780 		sc->nambarid = PCIR_NAMBAR;
781 		sc->nabmbarid = PCIR_NABMBAR;
782 		sc->regtype = SYS_RES_IOPORT;
783 	}
784 
785 	sc->nambar = bus_alloc_resource_any(dev, sc->regtype,
786 		&sc->nambarid, RF_ACTIVE);
787 	sc->nabmbar = bus_alloc_resource_any(dev, sc->regtype,
788 		&sc->nabmbarid, RF_ACTIVE);
789 
790 	if (!sc->nambar || !sc->nabmbar) {
791 		device_printf(dev, "unable to map IO port space\n");
792 		goto bad;
793 	}
794 
795 	sc->nambart = rman_get_bustag(sc->nambar);
796 	sc->nambarh = rman_get_bushandle(sc->nambar);
797 	sc->nabmbart = rman_get_bustag(sc->nabmbar);
798 	sc->nabmbarh = rman_get_bushandle(sc->nabmbar);
799 
800 	sc->bufsz = pcm_getbuffersize(dev, 4096, ICH_DEFAULT_BUFSZ, ICH_MAX_BUFSZ);
801 	if (bus_dma_tag_create(NULL, 8, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
802 			       NULL, NULL, sc->bufsz, 1, 0x3ffff, 0,
803 			       NULL, NULL, &sc->dmat) != 0) {
804 		device_printf(dev, "unable to create dma tag\n");
805 		goto bad;
806 	}
807 
808 	sc->irqid = 0;
809 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
810 		RF_ACTIVE | RF_SHAREABLE);
811 	if (!sc->irq || snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ich_intr, sc, &sc->ih)) {
812 		device_printf(dev, "unable to map interrupt\n");
813 		goto bad;
814 	}
815 
816 	if (ich_init(sc)) {
817 		device_printf(dev, "unable to initialize the card\n");
818 		goto bad;
819 	}
820 
821 	if (bus_dmamem_alloc(sc->dmat, (void **)&sc->dtbl,
822 		    BUS_DMA_NOWAIT, &sc->dtmap))
823 		goto bad;
824 
825 	if (bus_dmamap_load(sc->dmat, sc->dtmap, sc->dtbl,
826 		    sizeof(struct ich_desc) * ICH_DTBL_LENGTH * 3,
827 		    ich_setmap, sc, 0))
828 		goto bad;
829 
830 	sc->codec = AC97_CREATE(dev, sc, ich_ac97);
831 	if (sc->codec == NULL)
832 		goto bad;
833 
834 	/*
835 	 * Turn on inverted external amplifier sense flags for few
836 	 * 'special' boards.
837 	 */
838 	switch (subdev) {
839 	case 0x202f161f:	/* Gateway 7326GZ */
840 	case 0x203a161f:	/* Gateway 4028GZ */
841 	case 0x204c161f:	/* Kvazar-Micro Senator 3592XT */
842 	case 0x8144104d:	/* Sony VAIO PCG-TR* */
843 	case 0x81c0104d:	/* Sony VAIO type T */
844 	case 0x8197104d:	/* Sony S1XP */
845 		ac97_setflags(sc->codec, ac97_getflags(sc->codec) | AC97_F_EAPD_INV);
846 		break;
847 	default:
848 		break;
849 	}
850 
851 	mixer_init(dev, ac97_getmixerclass(), sc->codec);
852 
853 	/* check and set VRA function */
854 	extcaps = ac97_getextcaps(sc->codec);
855 	sc->hasvra = extcaps & AC97_EXTCAP_VRA;
856 	sc->hasvrm = extcaps & AC97_EXTCAP_VRM;
857 	sc->hasmic = ac97_getcaps(sc->codec) & AC97_CAP_MICCHANNEL;
858 	ac97_setextmode(sc->codec, sc->hasvra | sc->hasvrm);
859 
860 	if (pcm_register(dev, sc, 1, sc->hasmic? 2 : 1))
861 		goto bad;
862 
863 	pcm_addchan(dev, PCMDIR_PLAY, &ichchan_class, sc);		/* play */
864 	pcm_addchan(dev, PCMDIR_REC, &ichchan_class, sc);		/* record */
865 	if (sc->hasmic)
866 		pcm_addchan(dev, PCMDIR_REC, &ichchan_class, sc);	/* record mic */
867 
868 	snprintf(status, SND_STATUSLEN, "at io 0x%lx, 0x%lx irq %ld bufsz %u %s",
869 		 rman_get_start(sc->nambar), rman_get_start(sc->nabmbar), rman_get_start(sc->irq), sc->bufsz,PCM_KLDSTRING(snd_ich));
870 
871 	pcm_setstatus(dev, status);
872 
873 	ich_initsys(sc);
874 
875 	sc->intrhook.ich_func = ich_calibrate;
876 	sc->intrhook.ich_arg = sc;
877 	sc->use_intrhook = 1;
878 	if (config_intrhook_establish(&sc->intrhook) != 0) {
879 		device_printf(dev, "Cannot establish calibration hook, will calibrate now\n");
880 		sc->use_intrhook = 0;
881 		ich_calibrate(sc);
882 	}
883 
884 	return 0;
885 
886 bad:
887 	if (sc->codec)
888 		ac97_destroy(sc->codec);
889 	if (sc->ih)
890 		bus_teardown_intr(dev, sc->irq, sc->ih);
891 	if (sc->irq)
892 		bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
893 	if (sc->nambar)
894 		bus_release_resource(dev, sc->regtype,
895 		    sc->nambarid, sc->nambar);
896 	if (sc->nabmbar)
897 		bus_release_resource(dev, sc->regtype,
898 		    sc->nabmbarid, sc->nabmbar);
899 	if (sc->dtmap)
900 		bus_dmamap_unload(sc->dmat, sc->dtmap);
901 	if (sc->dmat)
902 		bus_dma_tag_destroy(sc->dmat);
903 	if (sc->ich_lock)
904 		snd_mtxfree(sc->ich_lock);
905 	free(sc, M_DEVBUF);
906 	return ENXIO;
907 }
908 
909 static int
910 ich_pci_detach(device_t dev)
911 {
912 	struct sc_info *sc;
913 	int r;
914 
915 	r = pcm_unregister(dev);
916 	if (r)
917 		return r;
918 	sc = pcm_getdevinfo(dev);
919 
920 	bus_teardown_intr(dev, sc->irq, sc->ih);
921 	bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
922 	bus_release_resource(dev, sc->regtype, sc->nambarid, sc->nambar);
923 	bus_release_resource(dev, sc->regtype, sc->nabmbarid, sc->nabmbar);
924 	bus_dmamap_unload(sc->dmat, sc->dtmap);
925 	bus_dma_tag_destroy(sc->dmat);
926 	snd_mtxfree(sc->ich_lock);
927 	free(sc, M_DEVBUF);
928 	return 0;
929 }
930 
931 static void
932 ich_pci_codec_reset(struct sc_info *sc)
933 {
934 	int i;
935 	uint32_t control;
936 
937 	control = ich_rd(sc, ICH_REG_GLOB_CNT, 4);
938 	control &= ~(ICH_GLOB_CTL_SHUT);
939 	control |= (control & ICH_GLOB_CTL_COLD) ?
940 		    ICH_GLOB_CTL_WARM : ICH_GLOB_CTL_COLD;
941 	ich_wr(sc, ICH_REG_GLOB_CNT, control, 4);
942 
943 	for (i = 500000; i; i--) {
944 	     	if (ich_rd(sc, ICH_REG_GLOB_STA, 4) & ICH_GLOB_STA_PCR)
945 			break;		/*		or ICH_SCR? */
946 		DELAY(1);
947 	}
948 
949 	if (i <= 0)
950 		printf("%s: time out\n", __func__);
951 }
952 
953 static int
954 ich_pci_suspend(device_t dev)
955 {
956 	struct sc_info *sc;
957 	int i;
958 
959 	sc = pcm_getdevinfo(dev);
960 	ICH_LOCK(sc);
961 	for (i = 0 ; i < 3; i++) {
962 		sc->ch[i].run_save = sc->ch[i].run;
963 		if (sc->ch[i].run) {
964 			ICH_UNLOCK(sc);
965 			ichchan_trigger(0, &sc->ch[i], PCMTRIG_ABORT);
966 			ICH_LOCK(sc);
967 		}
968 	}
969 	ICH_UNLOCK(sc);
970 	return 0;
971 }
972 
973 static int
974 ich_pci_resume(device_t dev)
975 {
976 	struct sc_info *sc;
977 	int i;
978 
979 	sc = pcm_getdevinfo(dev);
980 
981 	if (sc->regtype == SYS_RES_IOPORT)
982 		pci_enable_io(dev, SYS_RES_IOPORT);
983 	else
984 		pci_enable_io(dev, SYS_RES_MEMORY);
985 	pci_enable_busmaster(dev);
986 
987 	ICH_LOCK(sc);
988 	/* Reinit audio device */
989     	if (ich_init(sc) == -1) {
990 		device_printf(dev, "unable to reinitialize the card\n");
991 		ICH_UNLOCK(sc);
992 		return ENXIO;
993 	}
994 	/* Reinit mixer */
995 	ich_pci_codec_reset(sc);
996 	ICH_UNLOCK(sc);
997 	ac97_setextmode(sc->codec, sc->hasvra | sc->hasvrm);
998     	if (mixer_reinit(dev) == -1) {
999 		device_printf(dev, "unable to reinitialize the mixer\n");
1000 		return ENXIO;
1001 	}
1002 	/* Re-start DMA engines */
1003 	for (i = 0 ; i < 3; i++) {
1004 		struct sc_chinfo *ch = &sc->ch[i];
1005 		if (sc->ch[i].run_save) {
1006 			ichchan_setblocksize(0, ch, ch->blksz);
1007 			ichchan_setspeed(0, ch, ch->spd);
1008 			ichchan_trigger(0, ch, PCMTRIG_START);
1009 		}
1010 	}
1011 	return 0;
1012 }
1013 
1014 static device_method_t ich_methods[] = {
1015 	/* Device interface */
1016 	DEVMETHOD(device_probe,		ich_pci_probe),
1017 	DEVMETHOD(device_attach,	ich_pci_attach),
1018 	DEVMETHOD(device_detach,	ich_pci_detach),
1019 	DEVMETHOD(device_suspend, 	ich_pci_suspend),
1020 	DEVMETHOD(device_resume,	ich_pci_resume),
1021 	{ 0, 0 }
1022 };
1023 
1024 static driver_t ich_driver = {
1025 	"pcm",
1026 	ich_methods,
1027 	PCM_SOFTC_SIZE,
1028 };
1029 
1030 DRIVER_MODULE(snd_ich, pci, ich_driver, pcm_devclass, 0, 0);
1031 MODULE_DEPEND(snd_ich, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1032 MODULE_VERSION(snd_ich, 1);
1033