ich.c revision 109182
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
35SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/ich.c 109182 2003-01-13 17:42:13Z orion $");
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 */
48struct ich_desc {
49	volatile u_int32_t buffer;
50	volatile u_int32_t length;
51};
52
53struct sc_info;
54
55/* channel registers */
56struct 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 */
71struct 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
95static u_int32_t ich_fmt[] = {
96	AFMT_STEREO | AFMT_S16_LE,
97	0
98};
99static struct pcmchan_caps ich_vrcaps = {8000, 48000, ich_fmt, 0};
100static struct pcmchan_caps ich_caps = {48000, 48000, ich_fmt, 0};
101
102/* -------------------------------------------------------------------- */
103/* Hardware */
104static u_int32_t
105ich_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
119static void
120ich_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 */
136static int
137ich_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
152static int
153ich_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
163static int
164ich_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
175static kobj_method_t ich_ac97_methods[] = {
176	KOBJMETHOD(ac97_read,		ich_rdcd),
177	KOBJMETHOD(ac97_write,		ich_wrcd),
178	{ 0, 0 }
179};
180AC97_DECLARE(ich_ac97);
181
182/* -------------------------------------------------------------------- */
183/* common routines */
184
185static void
186ich_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
205static int
206ich_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
235static void *
236ichchan_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
287static int
288ichchan_setformat(kobj_t obj, void *data, u_int32_t format)
289{
290	return 0;
291}
292
293static int
294ichchan_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
316static int
317ichchan_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
329static int
330ichchan_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
350static int
351ichchan_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
364static struct pcmchan_caps *
365ichchan_getcaps(kobj_t obj, void *data)
366{
367	struct sc_chinfo *ch = data;
368
369	return ch->spdreg? &ich_vrcaps : &ich_caps;
370}
371
372static 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};
382CHANNEL_DECLARE(ichchan);
383
384/* -------------------------------------------------------------------- */
385/* The interrupt handler */
386
387static void
388ich_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
444static int
445ich_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
460static
461void 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
553static void
554ich_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
555{
556	return;
557}
558
559static int
560ich_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
595static int
596ich_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	case 0x006a10de:
632		device_set_desc(dev, "Nvidia nForce2 AC97 controller");
633		return 0;
634
635	default:
636		return ENXIO;
637	}
638}
639
640static int
641ich_pci_attach(device_t dev)
642{
643	u_int16_t		extcaps;
644	struct sc_info 		*sc;
645	char 			status[SND_STATUSLEN];
646
647	if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT)) == NULL) {
648		device_printf(dev, "cannot allocate softc\n");
649		return ENXIO;
650	}
651
652	bzero(sc, sizeof(*sc));
653	sc->dev = dev;
654
655	/*
656	 * The SiS 7012 register set isn't quite like the standard ich.
657	 * There really should be a general "quirks" mechanism.
658	 */
659	if (pci_get_devid(dev) == SIS7012ID) {
660		sc->swap_reg = 1;
661		sc->sample_size = 1;
662	} else {
663		sc->swap_reg = 0;
664		sc->sample_size = 2;
665	}
666
667	/*
668	 * By default, ich4 has NAMBAR and NABMBAR i/o spaces as
669	 * read-only.  Need to enable "legacy support", by poking into
670	 * pci config space.  The driver should use MMBAR and MBBAR,
671	 * but doing so will mess things up here.  ich4 has enough new
672	 * features it warrants it's own driver.
673	 */
674	if (pci_get_devid(dev) == ICH4ID) {
675		pci_write_config(dev, PCIR_ICH_LEGACY, ICH_LEGACY_ENABLE, 1);
676	}
677
678	pci_enable_io(dev, SYS_RES_IOPORT);
679	/*
680	 * Enable bus master. On ich4 this may prevent the detection of
681	 * the primary codec becoming ready in ich_init().
682	 */
683	pci_enable_busmaster(dev);
684
685	sc->nambarid = PCIR_NAMBAR;
686	sc->nabmbarid = PCIR_NABMBAR;
687	sc->nambar = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->nambarid, 0, ~0, 1, RF_ACTIVE);
688	sc->nabmbar = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->nabmbarid, 0, ~0, 1, RF_ACTIVE);
689
690	if (!sc->nambar || !sc->nabmbar) {
691		device_printf(dev, "unable to map IO port space\n");
692		goto bad;
693	}
694
695	sc->nambart = rman_get_bustag(sc->nambar);
696	sc->nambarh = rman_get_bushandle(sc->nambar);
697	sc->nabmbart = rman_get_bustag(sc->nabmbar);
698	sc->nabmbarh = rman_get_bushandle(sc->nabmbar);
699
700	sc->bufsz = pcm_getbuffersize(dev, 4096, ICH_DEFAULT_BUFSZ, ICH_MAX_BUFSZ);
701	if (bus_dma_tag_create(NULL, 8, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
702			       NULL, NULL, sc->bufsz, 1, 0x3ffff, 0, &sc->dmat) != 0) {
703		device_printf(dev, "unable to create dma tag\n");
704		goto bad;
705	}
706
707	sc->irqid = 0;
708	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqid, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
709	if (!sc->irq || snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ich_intr, sc, &sc->ih)) {
710		device_printf(dev, "unable to map interrupt\n");
711		goto bad;
712	}
713
714	if (ich_init(sc)) {
715		device_printf(dev, "unable to initialize the card\n");
716		goto bad;
717	}
718
719	sc->codec = AC97_CREATE(dev, sc, ich_ac97);
720	if (sc->codec == NULL)
721		goto bad;
722	mixer_init(dev, ac97_getmixerclass(), sc->codec);
723
724	/* check and set VRA function */
725	extcaps = ac97_getextcaps(sc->codec);
726	sc->hasvra = extcaps & AC97_EXTCAP_VRA;
727	sc->hasvrm = extcaps & AC97_EXTCAP_VRM;
728	sc->hasmic = ac97_getcaps(sc->codec) & AC97_CAP_MICCHANNEL;
729	ac97_setextmode(sc->codec, sc->hasvra | sc->hasvrm);
730
731	if (pcm_register(dev, sc, 1, sc->hasmic? 2 : 1))
732		goto bad;
733
734	pcm_addchan(dev, PCMDIR_PLAY, &ichchan_class, sc);		/* play */
735	pcm_addchan(dev, PCMDIR_REC, &ichchan_class, sc);		/* record */
736	if (sc->hasmic)
737		pcm_addchan(dev, PCMDIR_REC, &ichchan_class, sc);	/* record mic */
738
739	snprintf(status, SND_STATUSLEN, "at io 0x%lx, 0x%lx irq %ld bufsz %u",
740		 rman_get_start(sc->nambar), rman_get_start(sc->nabmbar), rman_get_start(sc->irq), sc->bufsz);
741
742	pcm_setstatus(dev, status);
743
744	ich_initsys(sc);
745
746	sc->intrhook.ich_func = ich_calibrate;
747	sc->intrhook.ich_arg = sc;
748	sc->use_intrhook = 1;
749	if (config_intrhook_establish(&sc->intrhook) != 0) {
750		device_printf(dev, "Cannot establish calibration hook, will calibrate now\n");
751		sc->use_intrhook = 0;
752		ich_calibrate(sc);
753	}
754
755	return 0;
756
757bad:
758	if (sc->codec)
759		ac97_destroy(sc->codec);
760	if (sc->ih)
761		bus_teardown_intr(dev, sc->irq, sc->ih);
762	if (sc->irq)
763		bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
764	if (sc->nambar)
765		bus_release_resource(dev, SYS_RES_IOPORT,
766		    sc->nambarid, sc->nambar);
767	if (sc->nabmbar)
768		bus_release_resource(dev, SYS_RES_IOPORT,
769		    sc->nabmbarid, sc->nabmbar);
770	free(sc, M_DEVBUF);
771	return ENXIO;
772}
773
774static int
775ich_pci_detach(device_t dev)
776{
777	struct sc_info *sc;
778	int r;
779
780	r = pcm_unregister(dev);
781	if (r)
782		return r;
783	sc = pcm_getdevinfo(dev);
784
785	bus_teardown_intr(dev, sc->irq, sc->ih);
786	bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
787	bus_release_resource(dev, SYS_RES_IOPORT, sc->nambarid, sc->nambar);
788	bus_release_resource(dev, SYS_RES_IOPORT, sc->nabmbarid, sc->nabmbar);
789	bus_dma_tag_destroy(sc->dmat);
790	free(sc, M_DEVBUF);
791	return 0;
792}
793
794static int
795ich_pci_suspend(device_t dev)
796{
797	struct sc_info *sc;
798	int i;
799
800	sc = pcm_getdevinfo(dev);
801	for (i = 0 ; i < 3; i++) {
802		sc->ch[i].run_save = sc->ch[i].run;
803		if (sc->ch[i].run) {
804			ichchan_trigger(0, &sc->ch[i], PCMTRIG_ABORT);
805		}
806	}
807	return 0;
808}
809
810static int
811ich_pci_resume(device_t dev)
812{
813	struct sc_info *sc;
814	int i;
815
816	sc = pcm_getdevinfo(dev);
817
818	/* Reinit audio device */
819    	if (ich_init(sc) == -1) {
820		device_printf(dev, "unable to reinitialize the card\n");
821		return ENXIO;
822	}
823	/* Reinit mixer */
824    	if (mixer_reinit(dev) == -1) {
825		device_printf(dev, "unable to reinitialize the mixer\n");
826		return ENXIO;
827	}
828	/* Re-start DMA engines */
829	for (i = 0 ; i < 3; i++) {
830		struct sc_chinfo *ch = &sc->ch[i];
831		if (sc->ch[i].run_save) {
832			ichchan_setblocksize(0, ch, ch->blksz);
833			ichchan_setspeed(0, ch, ch->spd);
834			ichchan_trigger(0, ch, PCMTRIG_START);
835		}
836	}
837	return 0;
838}
839
840static device_method_t ich_methods[] = {
841	/* Device interface */
842	DEVMETHOD(device_probe,		ich_pci_probe),
843	DEVMETHOD(device_attach,	ich_pci_attach),
844	DEVMETHOD(device_detach,	ich_pci_detach),
845	DEVMETHOD(device_suspend, 	ich_pci_suspend),
846	DEVMETHOD(device_resume,	ich_pci_resume),
847	{ 0, 0 }
848};
849
850static driver_t ich_driver = {
851	"pcm",
852	ich_methods,
853	PCM_SOFTC_SIZE,
854};
855
856DRIVER_MODULE(snd_ich, pci, ich_driver, pcm_devclass, 0, 0);
857MODULE_DEPEND(snd_ich, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
858MODULE_VERSION(snd_ich, 1);
859