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