audio810.c revision 9484:fbd5ddc28e96
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26
27/*
28 * audio810 Audio Driver
29 *
30 * The driver is primarily targeted at providing audio support for the
31 * W1100z and W2100z systems, which use the AMD 8111 audio core and
32 * the Realtek ALC 655 codec. The ALC 655 chip supports only fixed 48k
33 * sample rate. However, the audio core of AMD 8111 is completely
34 * compatible to the Intel ICHx chips (Intel 8x0 chipsets), so the
35 * driver can work for the ICHx.  We only support the 48k maximum
36 * rate, since we only have a single PCM out channel.
37 *
38 * The AMD 8111 audio core, as an AC'97 controller, has independent
39 * channels for PCM in, PCM out, mic in, modem in, and modem out.
40 * The AC'97 controller is a PCI bus master with scatter/gather
41 * support. Each channel has a DMA engine. Currently, we use only
42 * the PCM in and PCM out channels. Each DMA engine uses one buffer
43 * descriptor list. And the buffer descriptor list is an array of up
44 * to 32 entries, each of which describes a data buffer. Each entry
45 * contains a pointer to a data buffer, control bits, and the length
46 * of the buffer being pointed to, where the length is expressed as
47 * the number of samples. This, combined with the 16-bit sample size,
48 * gives the actual physical length of the buffer.
49 *
50 * A workaround for the AD1980 and AD1985 codec:
51 *	Most vendors connect the surr-out of the codecs to the line-out jack.
52 *	So far we haven't found which vendors don't do that. So we assume that
53 *	all vendors swap the surr-out and the line-out outputs. So we need swap
54 *	the two outputs. But we still internally process the
55 *	"ad198x-swap-output" property. If someday some vendors do not swap the
56 *	outputs, we would set "ad198x-swap-output = 0" in the
57 *	/kernel/drv/audio810.conf file, and unload and reload the audio810
58 *	driver (or reboot).
59 *
60 * 	NOTE:
61 * 	This driver depends on the drv/audio and misc/ac97
62 * 	modules being loaded first.
63 */
64#include <sys/types.h>
65#include <sys/modctl.h>
66#include <sys/kmem.h>
67#include <sys/conf.h>
68#include <sys/ddi.h>
69#include <sys/sunddi.h>
70#include <sys/pci.h>
71#include <sys/note.h>
72#include <sys/audio/audio_driver.h>
73#include <sys/audio/ac97.h>
74#include "audio810.h"
75
76/*
77 * Module linkage routines for the kernel
78 */
79static int audio810_ddi_attach(dev_info_t *, ddi_attach_cmd_t);
80static int audio810_ddi_detach(dev_info_t *, ddi_detach_cmd_t);
81static int audio810_ddi_quiesce(dev_info_t *);
82
83/*
84 * Entry point routine prototypes
85 */
86static int audio810_open(void *, int, unsigned *, unsigned *, caddr_t *);
87static void audio810_close(void *);
88static int audio810_start(void *);
89static void audio810_stop(void *);
90static int audio810_format(void *);
91static int audio810_channels(void *);
92static int audio810_rate(void *);
93static uint64_t audio810_count(void *);
94static void audio810_sync(void *, unsigned);
95static size_t audio810_qlen(void *);
96
97static audio_engine_ops_t audio810_engine_ops = {
98	AUDIO_ENGINE_VERSION,
99	audio810_open,
100	audio810_close,
101	audio810_start,
102	audio810_stop,
103	audio810_count,
104	audio810_format,
105	audio810_channels,
106	audio810_rate,
107	audio810_sync,
108	audio810_qlen
109};
110
111/*
112 * interrupt handler
113 */
114static uint_t	audio810_intr(caddr_t);
115
116/*
117 * Local Routine Prototypes
118 */
119static int audio810_attach(dev_info_t *);
120static int audio810_resume(dev_info_t *);
121static int audio810_detach(dev_info_t *);
122static int audio810_suspend(dev_info_t *);
123
124static int audio810_alloc_port(audio810_state_t *, int, uint8_t);
125static void audio810_start_port(audio810_port_t *);
126static void audio810_stop_port(audio810_port_t *);
127static void audio810_reset_port(audio810_port_t *);
128static void audio810_update_port(audio810_port_t *);
129static int audio810_codec_sync(audio810_state_t *);
130static void audio810_write_ac97(void *, uint8_t, uint16_t);
131static uint16_t audio810_read_ac97(void *, uint8_t);
132static int audio810_map_regs(dev_info_t *, audio810_state_t *);
133static void audio810_unmap_regs(audio810_state_t *);
134static void audio810_stop_dma(audio810_state_t *);
135static int audio810_chip_init(audio810_state_t *);
136static void audio810_destroy(audio810_state_t *);
137
138/*
139 * Global variables, but used only by this file.
140 */
141
142/* driver name, so we don't have to call ddi_driver_name() or hard code strs */
143static char	*audio810_name = I810_NAME;
144
145
146/*
147 * DDI Structures
148 */
149
150/* Device operations structure */
151static struct dev_ops audio810_dev_ops = {
152	DEVO_REV,		/* devo_rev */
153	0,			/* devo_refcnt */
154	NULL,			/* devo_getinfo */
155	nulldev,		/* devo_identify - obsolete */
156	nulldev,		/* devo_probe */
157	audio810_ddi_attach,	/* devo_attach */
158	audio810_ddi_detach,	/* devo_detach */
159	nodev,			/* devo_reset */
160	NULL,			/* devi_cb_ops */
161	NULL,			/* devo_bus_ops */
162	NULL,			/* devo_power */
163	audio810_ddi_quiesce,	/* devo_quiesce */
164};
165
166/* Linkage structure for loadable drivers */
167static struct modldrv audio810_modldrv = {
168	&mod_driverops,		/* drv_modops */
169	I810_MOD_NAME,		/* drv_linkinfo */
170	&audio810_dev_ops,	/* drv_dev_ops */
171};
172
173/* Module linkage structure */
174static struct modlinkage audio810_modlinkage = {
175	MODREV_1,			/* ml_rev */
176	(void *)&audio810_modldrv,	/* ml_linkage */
177	NULL				/* NULL terminates the list */
178};
179
180/*
181 * device access attributes for register mapping
182 */
183static struct ddi_device_acc_attr dev_attr = {
184	DDI_DEVICE_ATTR_V0,
185	DDI_STRUCTURE_LE_ACC,
186	DDI_STRICTORDER_ACC
187};
188
189static struct ddi_device_acc_attr buf_attr = {
190	DDI_DEVICE_ATTR_V0,
191	DDI_STRUCTURE_LE_ACC,
192	DDI_STRICTORDER_ACC
193};
194
195/*
196 * DMA attributes of buffer descriptor list
197 */
198static ddi_dma_attr_t	bdlist_dma_attr = {
199	DMA_ATTR_V0,	/* version */
200	0,		/* addr_lo */
201	0xffffffff,	/* addr_hi */
202	0x0000ffff,	/* count_max */
203	8,		/* align, BDL must be aligned on a 8-byte boundary */
204	0x3c,		/* burstsize */
205	8,		/* minxfer, set to the size of a BDlist entry */
206	0x0000ffff,	/* maxxfer */
207	0x00000fff,	/* seg, set to the RAM pagesize of intel platform */
208	1,		/* sgllen, there's no scatter-gather list */
209	8,		/* granular, set to the value of minxfer */
210	0		/* flags, use virtual address */
211};
212
213/*
214 * DMA attributes of buffers to be used to receive/send audio data
215 */
216static ddi_dma_attr_t	sample_buf_dma_attr = {
217	DMA_ATTR_V0,
218	0,		/* addr_lo */
219	0xffffffff,	/* addr_hi */
220	0x0001ffff,	/* count_max */
221	4,		/* align, data buffer is aligned on a 4-byte boundary */
222	0x3c,		/* burstsize */
223	4,		/* minxfer, set to the size of a sample data */
224	0x0001ffff,	/* maxxfer */
225	0x0001ffff,	/* seg */
226	1,		/* sgllen, no scatter-gather */
227	4,		/* granular, set to the value of minxfer */
228	0,		/* flags, use virtual address */
229};
230
231/*
232 * _init()
233 *
234 * Description:
235 *	Driver initialization, called when driver is first loaded.
236 *	This is how access is initially given to all the static structures.
237 *
238 * Arguments:
239 *	None
240 *
241 * Returns:
242 *	mod_install() status, see mod_install(9f)
243 */
244int
245_init(void)
246{
247	int	error;
248
249	audio_init_ops(&audio810_dev_ops, I810_NAME);
250
251	if ((error = mod_install(&audio810_modlinkage)) != 0) {
252		audio_fini_ops(&audio810_dev_ops);
253	}
254
255	return (error);
256}
257
258/*
259 * _fini()
260 *
261 * Description:
262 *	Module de-initialization, called when the driver is to be unloaded.
263 *
264 * Arguments:
265 *	None
266 *
267 * Returns:
268 *	mod_remove() status, see mod_remove(9f)
269 */
270int
271_fini(void)
272{
273	int		error;
274
275	if ((error = mod_remove(&audio810_modlinkage)) != 0) {
276		return (error);
277	}
278
279	/* clean up ops */
280	audio_fini_ops(&audio810_dev_ops);
281
282	return (0);
283}
284
285/*
286 * _info()
287 *
288 * Description:
289 *	Module information, returns information about the driver.
290 *
291 * Arguments:
292 *	modinfo		*modinfop	Pointer to the opaque modinfo structure
293 *
294 * Returns:
295 *	mod_info() status, see mod_info(9f)
296 */
297int
298_info(struct modinfo *modinfop)
299{
300	return (mod_info(&audio810_modlinkage, modinfop));
301}
302
303
304/* ******************* Driver Entry Points ********************************* */
305
306/*
307 * audio810_ddi_attach()
308 *
309 * Description:
310 *	Implements the DDI attach(9e) entry point.
311 *
312 * Arguments:
313 *	dev_info_t	*dip	Pointer to the device's dev_info struct
314 *	ddi_attach_cmd_t cmd	Attach command
315 *
316 * Returns:
317 *	DDI_SUCCESS		The driver was initialized properly
318 *	DDI_FAILURE		The driver couldn't be initialized properly
319 */
320static int
321audio810_ddi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
322{
323	switch (cmd) {
324	case DDI_ATTACH:
325		return (audio810_attach(dip));
326
327	case DDI_RESUME:
328		return (audio810_resume(dip));
329	}
330	return (DDI_FAILURE);
331}
332
333/*
334 * audio810_ddi_detach()
335 *
336 * Description:
337 *	Implements the detach(9e) entry point.
338 *
339 * Arguments:
340 *	dev_info_t		*dip	Pointer to the device's dev_info struct
341 *	ddi_detach_cmd_t	cmd	Detach command
342 *
343 * Returns:
344 *	DDI_SUCCESS	The driver was detached
345 *	DDI_FAILURE	The driver couldn't be detached
346 */
347static int
348audio810_ddi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
349{
350	switch (cmd) {
351	case DDI_DETACH:
352		return (audio810_detach(dip));
353
354	case DDI_SUSPEND:
355		return (audio810_suspend(dip));
356	}
357	return (DDI_FAILURE);
358}
359
360/*
361 * audio810_ddi_quiesce()
362 *
363 * Description:
364 *	Implements the quiesce(9e) entry point.
365 *
366 * Arguments:
367 *	dev_info_t		*dip	Pointer to the device's dev_info struct
368 *
369 * Returns:
370 *	DDI_SUCCESS	The driver was quiesced
371 *	DDI_FAILURE	The driver couldn't be quiesced
372 */
373static int
374audio810_ddi_quiesce(dev_info_t *dip)
375{
376	audio810_state_t	*statep;
377
378	if ((statep = ddi_get_driver_private(dip)) == NULL)
379		return (DDI_FAILURE);
380
381	audio810_stop_dma(statep);
382	return (DDI_SUCCESS);
383}
384
385/*
386 * audio810_intr()
387 *
388 * Description:
389 *	Interrupt service routine for both play and record. For play we
390 *	get the next buffers worth of audio. For record we send it on to
391 *	the mixer.
392 *
393 *	Each of buffer descriptor has a field IOC(interrupt on completion)
394 *	When both this and the IOC bit of correspondent dma control register
395 *	is set, it means that the controller should issue an interrupt upon
396 *	completion of this buffer.
397 *	(AMD 8111 hypertransport I/O hub data sheet. 3.8.3 page 71)
398 *
399 * Arguments:
400 *	caddr_t		arg	Pointer to the interrupting device's state
401 *				structure
402 *
403 * Returns:
404 *	DDI_INTR_CLAIMED	Interrupt claimed and processed
405 *	DDI_INTR_UNCLAIMED	Interrupt not claimed, and thus ignored
406 */
407static uint_t
408audio810_intr(caddr_t arg)
409{
410	audio810_state_t	*statep;
411	uint16_t		gsr;
412
413	statep = (void *)arg;
414	mutex_enter(&statep->inst_lock);
415
416	if (statep->suspended) {
417		mutex_exit(&statep->inst_lock);
418		return (DDI_INTR_UNCLAIMED);
419	}
420
421	gsr = I810_BM_GET32(I810_REG_GSR);
422
423	/* check if device is interrupting */
424	if ((gsr & I810_GSR_USE_INTR) == 0) {
425		mutex_exit(&statep->inst_lock);
426		return (DDI_INTR_UNCLAIMED);
427	}
428
429	for (int pnum = 0; pnum < I810_NUM_PORTS; pnum++) {
430		audio810_port_t	*port;
431		uint8_t		regoff, index;
432
433		port = statep->ports[pnum];
434		if (port == NULL) {
435			continue;
436		}
437		regoff = port->regoff;
438
439		if (!(I810_BM_GET8(port->stsoff) & I810_BM_SR_BCIS))
440			continue;
441
442		/* update the LVI -- we just set it to the current value - 1 */
443		index = I810_BM_GET8(regoff + I810_OFFSET_CIV);
444		index = (index - 1) % I810_BD_NUMS;
445
446		I810_BM_PUT8(regoff + I810_OFFSET_LVI, index);
447
448		/* clear any interrupt */
449		I810_BM_PUT8(port->stsoff,
450		    I810_BM_SR_LVBCI | I810_BM_SR_BCIS | I810_BM_SR_FIFOE);
451	}
452
453	/* update the kernel interrupt statistics */
454	if (statep->ksp) {
455		I810_KIOP(statep)->intrs[KSTAT_INTR_HARD]++;
456	}
457
458	mutex_exit(&statep->inst_lock);
459
460	/* notify the framework */
461	if (gsr & I810_GSR_INTR_PIN) {
462		audio_engine_produce(statep->ports[I810_PCM_IN]->engine);
463	}
464	if (gsr & I810_GSR_INTR_POUT) {
465		audio_engine_consume(statep->ports[I810_PCM_OUT]->engine);
466	}
467
468	return (DDI_INTR_CLAIMED);
469}
470
471/*
472 * audio810_open()
473 *
474 * Description:
475 *	Opens a DMA engine for use.
476 *
477 * Arguments:
478 *	void		*arg		The DMA engine to set up
479 *	int		flag		Open flags
480 *	unsigned	*fragfrp	Receives number of frames per fragment
481 *	unsigned	*nfragsp	Receives number of fragments
482 *	caddr_t		*bufp		Receives kernel data buffer
483 *
484 * Returns:
485 *	0	on success
486 *	errno	on failure
487 */
488static int
489audio810_open(void *arg, int flag, unsigned *fragfrp, unsigned *nfragsp,
490    caddr_t *bufp)
491{
492	audio810_port_t	*port = arg;
493
494	_NOTE(ARGUNUSED(flag));
495
496	port->started = B_FALSE;
497	port->count = 0;
498	*fragfrp = port->fragfr;
499	*nfragsp = port->nfrag;
500	*bufp = port->samp_kaddr;
501
502	mutex_enter(&port->statep->inst_lock);
503	audio810_reset_port(port);
504	mutex_exit(&port->statep->inst_lock);
505
506	return (0);
507}
508
509/*
510 * audio810_close()
511 *
512 * Description:
513 *	Closes an audio DMA engine that was previously opened.  Since
514 *	nobody is using it, we take this opportunity to possibly power
515 *	down the entire device.
516 *
517 * Arguments:
518 *	void	*arg		The DMA engine to shut down
519 */
520static void
521audio810_close(void *arg)
522{
523	audio810_port_t		*port = arg;
524	audio810_state_t	*statep = port->statep;
525
526	mutex_enter(&statep->inst_lock);
527	audio810_stop_port(port);
528	port->started = B_FALSE;
529	mutex_exit(&statep->inst_lock);
530}
531
532/*
533 * audio810_stop()
534 *
535 * Description:
536 *	This is called by the framework to stop a port that is
537 *	transferring data.
538 *
539 * Arguments:
540 *	void	*arg		The DMA engine to stop
541 */
542static void
543audio810_stop(void *arg)
544{
545	audio810_port_t		*port = arg;
546	audio810_state_t	*statep = port->statep;
547
548	mutex_enter(&statep->inst_lock);
549	if (port->started) {
550		audio810_stop_port(port);
551	}
552	port->started = B_FALSE;
553	mutex_exit(&statep->inst_lock);
554}
555
556/*
557 * audio810_start()
558 *
559 * Description:
560 *	This is called by the framework to start a port transferring data.
561 *
562 * Arguments:
563 *	void	*arg		The DMA engine to start
564 *
565 * Returns:
566 *	0 	on success (never fails, errno if it did)
567 */
568static int
569audio810_start(void *arg)
570{
571	audio810_port_t		*port = arg;
572	audio810_state_t	*statep = port->statep;
573
574	mutex_enter(&statep->inst_lock);
575	if (!port->started) {
576		audio810_start_port(port);
577		port->started = B_TRUE;
578	}
579	mutex_exit(&statep->inst_lock);
580	return (0);
581}
582
583/*
584 * audio810_format()
585 *
586 * Description:
587 *	This is called by the framework to query the format of the device.
588 *
589 * Arguments:
590 *	void	*arg		The DMA engine to query
591 *
592 * Returns:
593 *	Format of the device (fixed at AUDIO_FORMAT_S16_LE)
594 */
595static int
596audio810_format(void *arg)
597{
598	_NOTE(ARGUNUSED(arg));
599
600	return (AUDIO_FORMAT_S16_LE);
601}
602
603/*
604 * audio810_channels()
605 *
606 * Description:
607 *	This is called by the framework to query the num channels of
608 *	the device.
609 *
610 * Arguments:
611 *	void	*arg		The DMA engine to query
612 *
613 * Returns:
614 *	0 number of channels for device
615 */
616static int
617audio810_channels(void *arg)
618{
619	audio810_port_t	*port = arg;
620
621	return (port->nchan);
622}
623
624/*
625 * audio810_rate()
626 *
627 * Description:
628 *	This is called by the framework to query the rate of the device.
629 *
630 * Arguments:
631 *	void	*arg		The DMA engine to query
632 *
633 * Returns:
634 *	Rate of device (fixed at 48000 Hz)
635 */
636static int
637audio810_rate(void *arg)
638{
639	_NOTE(ARGUNUSED(arg));
640
641	return (48000);
642}
643
644/*
645 * audio810_count()
646 *
647 * Description:
648 *	This is called by the framework to get the engine's frame counter
649 *
650 * Arguments:
651 *	void	*arg		The DMA engine to query
652 *
653 * Returns:
654 *	frame count for current engine
655 */
656static uint64_t
657audio810_count(void *arg)
658{
659	audio810_port_t		*port = arg;
660	audio810_state_t	*statep = port->statep;
661	uint64_t		val;
662	uint16_t		picb;
663	uint64_t		count;
664	uint8_t			nchan;
665
666	mutex_enter(&statep->inst_lock);
667	audio810_update_port(port);
668	count = port->count;
669	picb = port->picb;
670	nchan = port->nchan;
671	mutex_exit(&statep->inst_lock);
672
673	if (statep->quirk == QUIRK_SIS7012) {
674		val = count + picb / (2 * nchan);
675	} else {
676		val = count + (picb / nchan);
677	}
678
679	return (val);
680}
681
682/*
683 * audio810_sync()
684 *
685 * Description:
686 *	This is called by the framework to synchronize DMA caches.
687 *
688 * Arguments:
689 *	void	*arg		The DMA engine to sync
690 */
691static void
692audio810_sync(void *arg, unsigned nframes)
693{
694	audio810_port_t *port = arg;
695	_NOTE(ARGUNUSED(nframes));
696
697	(void) ddi_dma_sync(port->samp_dmah, 0, 0, port->sync_dir);
698}
699
700/*
701 * audio810_qlen()
702 *
703 * Description:
704 *	This is called by the framework to determine on-device queue length.
705 *
706 * Arguments:
707 *	void	*arg		The DMA engine to query
708 *
709 * Returns:
710 *	hardware queue length not reported by count (0 for this device)
711 */
712static size_t
713audio810_qlen(void *arg)
714{
715	_NOTE(ARGUNUSED(arg));
716	return (0);
717}
718
719/* *********************** Local Routines *************************** */
720
721/*
722 * audio810_start_port()
723 *
724 * Description:
725 *	This routine starts the DMA engine.
726 *
727 * Arguments:
728 *	audio810_port_t	*port		Port of DMA engine to start.
729 */
730static void
731audio810_start_port(audio810_port_t *port)
732{
733	audio810_state_t	*statep = port->statep;
734	uint8_t			cr;
735
736	ASSERT(mutex_owned(&statep->inst_lock));
737
738	/* if suspended, then do nothing else */
739	if (statep->suspended) {
740		return;
741	}
742
743	cr = I810_BM_GET8(port->regoff + I810_OFFSET_CR);
744	cr |= I810_BM_CR_IOCE;
745	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr);
746	cr |= I810_BM_CR_RUN;
747	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr);
748}
749
750/*
751 * audio810_stop_port()
752 *
753 * Description:
754 *	This routine stops the DMA engine.
755 *
756 * Arguments:
757 *	audio810_port_t	*port		Port of DMA engine to stop.
758 */
759static void
760audio810_stop_port(audio810_port_t *port)
761{
762	audio810_state_t	*statep = port->statep;
763	uint8_t			cr;
764
765	ASSERT(mutex_owned(&statep->inst_lock));
766
767	/* if suspended, then do nothing else */
768	if (statep->suspended) {
769		return;
770	}
771
772	cr = I810_BM_GET8(port->regoff + I810_OFFSET_CR);
773	cr &= ~I810_BM_CR_RUN;
774	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr);
775}
776
777/*
778 * audio810_reset_port()
779 *
780 * Description:
781 *	This routine resets the DMA engine pareparing it for work.
782 *
783 * Arguments:
784 *	audio810_port_t	*port		Port of DMA engine to reset.
785 */
786static void
787audio810_reset_port(audio810_port_t *port)
788{
789	audio810_state_t	*statep = port->statep;
790	uint32_t		gcr;
791
792	ASSERT(mutex_owned(&statep->inst_lock));
793
794	port->civ = 0;
795	port->picb = 0;
796
797	if (statep->suspended)
798		return;
799
800	/*
801	 * Make sure we put once in stereo, to ensure we always start from
802	 * front left.
803	 */
804	if (port->num == I810_PCM_OUT) {
805
806		if (statep->quirk == QUIRK_SIS7012) {
807			/*
808			 * SiS 7012 needs its own special multichannel config.
809			 */
810			gcr = I810_BM_GET32(I810_REG_GCR);
811			gcr &= ~I810_GCR_SIS_CHANNELS_MASK;
812			I810_BM_PUT32(I810_REG_GCR, gcr);
813			delay(drv_usectohz(50000));	/* 50 msec */
814
815			switch (statep->maxch) {
816			case 2:
817				gcr |= I810_GCR_SIS_2_CHANNELS;
818				break;
819			case 4:
820				gcr |= I810_GCR_SIS_4_CHANNELS;
821				break;
822			case 6:
823				gcr |= I810_GCR_SIS_6_CHANNELS;
824				break;
825			}
826			I810_BM_PUT32(I810_REG_GCR, gcr);
827			delay(drv_usectohz(50000));	/* 50 msec */
828
829			/*
830			 * SiS 7012 has special unmute bit.
831			 */
832			I810_BM_PUT8(I810_REG_SISCTL, I810_SISCTL_UNMUTE);
833
834		} else {
835
836			/*
837			 * All other devices work the same.
838			 */
839			gcr = I810_BM_GET32(I810_REG_GCR);
840			gcr &= ~I810_GCR_CHANNELS_MASK;
841
842			I810_BM_PUT32(I810_REG_GCR, gcr);
843			delay(drv_usectohz(50000));	/* 50 msec */
844
845			switch (statep->maxch) {
846			case 2:
847				gcr |= I810_GCR_2_CHANNELS;
848				break;
849			case 4:
850				gcr |= I810_GCR_4_CHANNELS;
851				break;
852			case 6:
853				gcr |= I810_GCR_6_CHANNELS;
854				break;
855			}
856			I810_BM_PUT32(I810_REG_GCR, gcr);
857			delay(drv_usectohz(50000));	/* 50 msec */
858		}
859	}
860
861	/*
862	 * Perform full reset of the engine, but leave it turned off.
863	 */
864	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, 0);
865	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, I810_BM_CR_RST);
866
867	/* program the offset of the BD list */
868	I810_BM_PUT32(port->regoff + I810_OFFSET_BD_BASE, port->bdl_paddr);
869
870	/* we set the last index to the full count -- all buffers are valid */
871	I810_BM_PUT8(port->regoff + I810_OFFSET_LVI, I810_BD_NUMS - 1);
872}
873
874/*
875 * audio810_update_port()
876 *
877 * Description:
878 *	This routine updates the ports frame counter from hardware, and
879 *	gracefully handles wraps.
880 *
881 * Arguments:
882 *	audio810_port_t	*port		The port to update.
883 */
884static void
885audio810_update_port(audio810_port_t *port)
886{
887	audio810_state_t	*statep = port->statep;
888	uint8_t			regoff = port->regoff;
889	uint8_t			civ;
890	uint16_t		picb;
891	unsigned		n;
892
893	if (statep->suspended) {
894		civ = 0;
895		picb = 0;
896	} else {
897		/*
898		 * We read the position counters, but we're careful to avoid
899		 * the situation where the position counter resets at the end
900		 * of a buffer.
901		 */
902		for (int i = 0; i < 2; i++) {
903			civ = I810_BM_GET8(regoff + I810_OFFSET_CIV);
904			picb = I810_BM_GET16(port->picboff);
905			if (I810_BM_GET8(regoff + I810_OFFSET_CIV) == civ) {
906				/*
907				 * Chip did not start a new index,
908				 * so the picb is valid.
909				 */
910				break;
911			}
912		}
913
914		if (civ >= port->civ) {
915			n = civ - port->civ;
916		} else {
917			n = civ + (I810_BD_NUMS - port->civ);
918		}
919		port->count += (n * port->fragfr);
920	}
921	port->civ = civ;
922	port->picb = picb;
923}
924
925/*
926 * audio810_attach()
927 *
928 * Description:
929 *	Attach an instance of the audio810 driver. This routine does the
930 * 	device dependent attach tasks, and registers with the audio framework.
931 *
932 * Arguments:
933 *	dev_info_t	*dip	Pointer to the device's dev_info struct
934 *	ddi_attach_cmd_t cmd	Attach command
935 *
936 * Returns:
937 *	DDI_SUCCESS		The driver was initialized properly
938 *	DDI_FAILURE		The driver couldn't be initialized properly
939 */
940static int
941audio810_attach(dev_info_t *dip)
942{
943	uint16_t		cmdreg;
944	audio810_state_t	*statep;
945	audio_dev_t		*adev;
946	ddi_acc_handle_t	pcih;
947	uint32_t		devid;
948	uint32_t		gsr;
949	const char		*name;
950	const char		*vers;
951	uint8_t			nch;
952	int			maxch;
953
954	/* we don't support high level interrupts in the driver */
955	if (ddi_intr_hilevel(dip, 0) != 0) {
956		cmn_err(CE_WARN, "!%s: unsupported high level interrupt",
957		    audio810_name);
958		return (DDI_FAILURE);
959	}
960
961	/* allocate the soft state structure */
962	statep = kmem_zalloc(sizeof (*statep), KM_SLEEP);
963	ddi_set_driver_private(dip, statep);
964
965	/* get iblock cookie information */
966	if (ddi_get_iblock_cookie(dip, 0, &statep->iblock) != DDI_SUCCESS) {
967		cmn_err(CE_WARN, "!%s: cannot get iblock cookie",
968		    audio810_name);
969		kmem_free(statep, sizeof (*statep));
970		return (DDI_FAILURE);
971	}
972	mutex_init(&statep->inst_lock, NULL, MUTEX_DRIVER, statep->iblock);
973	mutex_init(&statep->ac_lock, NULL, MUTEX_DRIVER, statep->iblock);
974
975	if ((adev = audio_dev_alloc(dip, 0)) == NULL) {
976		cmn_err(CE_WARN, "!%s: unable to allocate audio dev",
977		    audio810_name);
978		goto error;
979	}
980	statep->adev = adev;
981	statep->dip = dip;
982
983	/* map in the registers, allocate DMA buffers, etc. */
984	if (audio810_map_regs(dip, statep) != DDI_SUCCESS) {
985		audio_dev_warn(adev, "couldn't map registers");
986		goto error;
987	}
988
989	/* set PCI command register */
990	if (pci_config_setup(dip, &pcih) != DDI_SUCCESS) {
991		audio_dev_warn(adev, "pci conf mapping failed");
992		goto error;
993	}
994	cmdreg = pci_config_get16(pcih, PCI_CONF_COMM);
995	pci_config_put16(pcih, PCI_CONF_COMM,
996	    cmdreg | PCI_COMM_IO | PCI_COMM_MAE | PCI_COMM_ME);
997	devid = pci_config_get16(pcih, PCI_CONF_VENID);
998	devid <<= 16;
999	devid |= pci_config_get16(pcih, PCI_CONF_DEVID);
1000	pci_config_teardown(&pcih);
1001
1002	/*
1003	 * Override "max-channels" property to prevent configuration
1004	 * of 4 or 6 (or possibly even 8!) channel audio.  The default
1005	 * is to support as many channels as the hardware can do.
1006	 *
1007	 * (Hmmm... perhaps this should be driven in the common
1008	 * framework.  The framework could even offer simplistic upmix
1009	 * and downmix for various standard configs.)
1010	 */
1011	maxch = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1012	    DDI_PROP_DONTPASS, "max-channels", 8);
1013	if (maxch < 2) {
1014		maxch = 2;
1015	}
1016
1017	gsr = I810_BM_GET32(I810_REG_GSR);
1018	if (gsr & I810_GSR_CAP6CH) {
1019		nch = 6;
1020	} else if (gsr & I810_GSR_CAP4CH) {
1021		nch = 4;
1022	} else {
1023		nch = 2;
1024	}
1025
1026	statep->maxch = (uint8_t)min(nch, maxch);
1027	statep->maxch &= ~1;
1028	name = "Unknown AC'97";
1029	vers = "";
1030
1031	statep->quirk = QUIRK_NONE;
1032	switch (devid) {
1033	case 0x80862415:
1034		name = "Intel AC'97";
1035		vers = "ICH";
1036		break;
1037	case 0x80862425:
1038		name = "Intel AC'97";
1039		vers = "ICH0";
1040		break;
1041	case 0x80867195:
1042		name = "Intel AC'97";
1043		vers = "440MX";
1044		break;
1045	case 0x80862445:
1046		name = "Intel AC'97";
1047		vers = "ICH2";
1048		break;
1049	case 0x80862485:
1050		name = "Intel AC'97";
1051		vers = "ICH3";
1052		break;
1053	case 0x808624C5:
1054		name = "Intel AC'97";
1055		vers = "ICH4";
1056		break;
1057	case 0x808624D5:
1058		name = "Intel AC'97";
1059		vers = "ICH5";
1060		break;
1061	case 0x8086266E:
1062		name = "Intel AC'97";
1063		vers = "ICH6";
1064		break;
1065	case 0x808627DE:
1066		name = "Intel AC'97";
1067		vers = "ICH7";
1068		break;
1069	case 0x808625A6:
1070		name = "Intel AC'97";
1071		vers = "6300ESB";
1072		break;
1073	case 0x80862698:
1074		name = "Intel AC'97";
1075		vers = "ESB2";
1076		break;
1077	case 0x10397012:
1078		name = "SiS AC'97";
1079		vers = "7012";
1080		statep->quirk = QUIRK_SIS7012;
1081		break;
1082	case 0x10de01b1:	/* nForce */
1083		name = "NVIDIA AC'97";
1084		vers = "MCP1";
1085		break;
1086	case 0x10de006a:	/* nForce 2 */
1087		name = "NVIDIA AC'97";
1088		vers = "MCP2";
1089		break;
1090	case 0x10de00da:	/* nForce 3 */
1091		name = "NVIDIA AC'97";
1092		vers = "MCP3";
1093		break;
1094	case 0x10de00ea:
1095		name = "NVIDIA AC'97";
1096		vers = "CK8S";
1097		break;
1098	case 0x10de0059:
1099		name = "NVIDIA AC'97";
1100		vers = "CK804";
1101		break;
1102	case 0x10de008a:
1103		name = "NVIDIA AC'97";
1104		vers = "CK8";
1105		break;
1106	case 0x10de003a:	/* nForce 4 */
1107		name = "NVIDIA AC'97";
1108		vers = "MCP4";
1109		break;
1110	case 0x10de026b:
1111		name = "NVIDIA AC'97";
1112		vers = "MCP51";
1113		break;
1114	case 0x1022746d:
1115		name = "AMD AC'97";
1116		vers = "8111";
1117		break;
1118	case 0x10227445:
1119		name = "AMD AC'97";
1120		vers = "AMD768";
1121		break;
1122	}
1123	/* set device information */
1124	audio_dev_set_description(adev, name);
1125	audio_dev_set_version(adev, vers);
1126
1127	/* allocate port structures */
1128	if ((audio810_alloc_port(statep, I810_PCM_OUT, statep->maxch) !=
1129	    DDI_SUCCESS) ||
1130	    (audio810_alloc_port(statep, I810_PCM_IN, 2) != DDI_SUCCESS)) {
1131		goto error;
1132	}
1133
1134	/* allocate ac97 handle */
1135	statep->ac97 = ac97_alloc(dip, audio810_read_ac97, audio810_write_ac97,
1136	    statep);
1137	if (statep->ac97 == NULL) {
1138		audio_dev_warn(adev, "failed to allocate ac97 handle");
1139		goto error;
1140	}
1141
1142	/* initialize audio controller and AC97 codec */
1143	if (audio810_chip_init(statep) != DDI_SUCCESS) {
1144		audio_dev_warn(adev, "failed to init chip");
1145		goto error;
1146	}
1147
1148	/* initialize the AC'97 part */
1149	if (ac97_init(statep->ac97, adev) != DDI_SUCCESS) {
1150		audio_dev_warn(adev, "ac'97 initialization failed");
1151		goto error;
1152	}
1153
1154	/* set up kernel statistics */
1155	if ((statep->ksp = kstat_create(I810_NAME, ddi_get_instance(dip),
1156	    I810_NAME, "controller", KSTAT_TYPE_INTR, 1,
1157	    KSTAT_FLAG_PERSISTENT)) != NULL) {
1158		kstat_install(statep->ksp);
1159	}
1160
1161	/* set up the interrupt handler */
1162	if (ddi_add_intr(dip, 0, &statep->iblock,
1163	    NULL, audio810_intr, (caddr_t)statep) != DDI_SUCCESS) {
1164		audio_dev_warn(adev, "bad interrupt specification");
1165		goto error;
1166	}
1167	statep->intr_added = B_TRUE;
1168
1169	if (audio_dev_register(adev) != DDI_SUCCESS) {
1170		audio_dev_warn(adev, "unable to register with framework");
1171		goto error;
1172	}
1173
1174	ddi_report_dev(dip);
1175
1176	return (DDI_SUCCESS);
1177
1178error:
1179	audio810_destroy(statep);
1180
1181	return (DDI_FAILURE);
1182}
1183
1184
1185/*
1186 * audio810_resume()
1187 *
1188 * Description:
1189 *	Resume operation of the device after sleeping or hibernating.
1190 *	Note that this should never fail, even if hardware goes wonky,
1191 *	because the current PM framework will panic if it does.
1192 *
1193 * Arguments:
1194 *	dev_info_t	*dip	Pointer to the device's dev_info struct
1195 *
1196 * Returns:
1197 *	DDI_SUCCESS		The driver was resumed.
1198 */
1199static int
1200audio810_resume(dev_info_t *dip)
1201{
1202	audio810_state_t	*statep;
1203	audio_dev_t		*adev;
1204
1205	/* this should always be valid */
1206	statep = ddi_get_driver_private(dip);
1207	adev = statep->adev;
1208
1209	ASSERT(statep != NULL);
1210	ASSERT(dip == statep->dip);
1211
1212	/* Restore the audio810 chip's state */
1213	if (audio810_chip_init(statep) != DDI_SUCCESS) {
1214		/*
1215		 * Note that PM gurus say we should return
1216		 * success here.  Failure of audio shouldn't
1217		 * be considered FATAL to the system.  The
1218		 * upshot is that audio will not progress.
1219		 */
1220		audio_dev_warn(adev, "DDI_RESUME failed to init chip");
1221		return (DDI_SUCCESS);
1222	}
1223
1224	/* allow ac97 operations again */
1225	ac97_resume(statep->ac97);
1226
1227	mutex_enter(&statep->inst_lock);
1228
1229	ASSERT(statep->suspended);
1230	statep->suspended = B_FALSE;
1231
1232	for (int i = 0; i < I810_NUM_PORTS; i++) {
1233
1234		audio810_port_t *port = statep->ports[i];
1235
1236		if (port != NULL) {
1237			/* reset framework DMA engine buffer */
1238			if (port->engine != NULL) {
1239				audio_engine_reset(port->engine);
1240			}
1241
1242			/* reset and initialize hardware ports */
1243			audio810_reset_port(port);
1244			if (port->started) {
1245				audio810_start_port(port);
1246			} else {
1247				audio810_stop_port(port);
1248			}
1249		}
1250	}
1251	mutex_exit(&statep->inst_lock);
1252
1253	return (DDI_SUCCESS);
1254}
1255
1256/*
1257 * audio810_detach()
1258 *
1259 * Description:
1260 *	Detach an instance of the audio810 driver.
1261 *
1262 * Arguments:
1263 *	dev_info_t		*dip	Pointer to the device's dev_info struct
1264 *
1265 * Returns:
1266 *	DDI_SUCCESS	The driver was detached
1267 *	DDI_FAILURE	The driver couldn't be detached
1268 */
1269static int
1270audio810_detach(dev_info_t *dip)
1271{
1272	audio810_state_t	*statep;
1273
1274	statep = ddi_get_driver_private(dip);
1275	ASSERT(statep != NULL);
1276
1277	/* don't detach us if we are still in use */
1278	if (audio_dev_unregister(statep->adev) != DDI_SUCCESS) {
1279		return (DDI_FAILURE);
1280	}
1281
1282	audio810_destroy(statep);
1283	return (DDI_SUCCESS);
1284}
1285
1286/*
1287 * audio810_suspend()
1288 *
1289 * Description:
1290 *	Suspend an instance of the audio810 driver, in preparation for
1291 *	sleep or hibernation.
1292 *
1293 * Arguments:
1294 *	dev_info_t		*dip	Pointer to the device's dev_info struct
1295 *
1296 * Returns:
1297 *	DDI_SUCCESS	The driver was suspended
1298 */
1299static int
1300audio810_suspend(dev_info_t *dip)
1301{
1302	audio810_state_t	*statep;
1303
1304	statep = ddi_get_driver_private(dip);
1305	ASSERT(statep != NULL);
1306
1307	ac97_suspend(statep->ac97);
1308
1309	mutex_enter(&statep->inst_lock);
1310
1311	ASSERT(statep->suspended == B_FALSE);
1312
1313	statep->suspended = B_TRUE; /* stop new ops */
1314
1315	/* stop DMA engines */
1316	audio810_stop_dma(statep);
1317
1318	mutex_exit(&statep->inst_lock);
1319
1320	return (DDI_SUCCESS);
1321}
1322
1323/*
1324 * audio810_alloc_port()
1325 *
1326 * Description:
1327 *	This routine allocates the DMA handles and the memory for the
1328 *	DMA engines to use.  It also configures the BDL lists properly
1329 *	for use.
1330 *
1331 * Arguments:
1332 *	dev_info_t	*dip	Pointer to the device's devinfo
1333 *
1334 * Returns:
1335 *	DDI_SUCCESS		Registers successfully mapped
1336 *	DDI_FAILURE		Registers not successfully mapped
1337 */
1338static int
1339audio810_alloc_port(audio810_state_t *statep, int num, uint8_t nchan)
1340{
1341	ddi_dma_cookie_t	cookie;
1342	uint_t			count;
1343	int			dir;
1344	unsigned		caps;
1345	char			*prop;
1346	char			*nfprop;
1347	audio_dev_t		*adev;
1348	audio810_port_t		*port;
1349	uint32_t		paddr;
1350	int			rc;
1351	dev_info_t		*dip;
1352	i810_bd_entry_t		*bdentry;
1353
1354	adev = statep->adev;
1355	dip = statep->dip;
1356
1357	port = kmem_zalloc(sizeof (*port), KM_SLEEP);
1358	statep->ports[num] = port;
1359	port->statep = statep;
1360	port->started = B_FALSE;
1361	port->nchan = nchan;
1362	port->num = num;
1363
1364	switch (num) {
1365	case I810_PCM_IN:
1366		prop = "record-interrupts";
1367		nfprop = "record-fragments";
1368		dir = DDI_DMA_READ;
1369		caps = ENGINE_INPUT_CAP;
1370		port->sync_dir = DDI_DMA_SYNC_FORKERNEL;
1371		port->regoff = I810_BASE_PCM_IN;
1372		break;
1373	case I810_PCM_OUT:
1374		prop = "play-interrupts";
1375		nfprop = "play-fragments";
1376		dir = DDI_DMA_WRITE;
1377		caps = ENGINE_OUTPUT_CAP;
1378		port->sync_dir = DDI_DMA_SYNC_FORDEV;
1379		port->regoff = I810_BASE_PCM_OUT;
1380		break;
1381	default:
1382		audio_dev_warn(adev, "bad port number (%d)!", num);
1383		return (DDI_FAILURE);
1384	}
1385
1386	/*
1387	 * SiS 7012 swaps status and picb registers.
1388	 */
1389	if (statep->quirk == QUIRK_SIS7012) {
1390		port->stsoff = port->regoff + I810_OFFSET_PICB;
1391		port->picboff = port->regoff + I810_OFFSET_SR;
1392	} else {
1393		port->stsoff = port->regoff + I810_OFFSET_SR;
1394		port->picboff = port->regoff + I810_OFFSET_PICB;
1395	}
1396
1397	port->intrs = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1398	    DDI_PROP_DONTPASS, prop, I810_INTS);
1399
1400	/* make sure the values are good */
1401	if (port->intrs < I810_MIN_INTS) {
1402		audio_dev_warn(adev, "%s too low, %d, resetting to %d",
1403		    prop, port->intrs, I810_INTS);
1404		port->intrs = I810_INTS;
1405	} else if (port->intrs > I810_MAX_INTS) {
1406		audio_dev_warn(adev, "%s too high, %d, resetting to %d",
1407		    prop, port->intrs, I810_INTS);
1408		port->intrs = I810_INTS;
1409	}
1410
1411	port->nfrag = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1412	    DDI_PROP_DONTPASS, nfprop, I810_NFRAGS);
1413
1414	/*
1415	 * Note that fragments must divide evenly into I810_BD_NUMS (32).
1416	 */
1417	if (port->nfrag <= 4) {
1418		port->nfrag = 4;
1419	} else if (port->nfrag <= 8) {
1420		port->nfrag = 8;
1421	} else if (port->nfrag <= 16) {
1422		port->nfrag = 16;
1423	} else {
1424		port->nfrag = I810_BD_NUMS;
1425	}
1426
1427	/*
1428	 * Figure out how much space we need.  Sample rate is 48kHz, and
1429	 * we need to store 32 chunks.  (Note that this means that low
1430	 * interrupt frequencies will require more RAM.  We could probably
1431	 * do some cleverness to use a shorter BD list.)
1432	 */
1433	port->fragfr = 48000 / port->intrs;
1434	port->fragfr = I810_ROUNDUP(port->fragfr, I810_MOD_SIZE);
1435	port->fragsz = port->fragfr * port->nchan * 2;
1436	port->samp_size = port->fragsz * port->nfrag;
1437
1438	/* allocate dma handle */
1439	rc = ddi_dma_alloc_handle(dip, &sample_buf_dma_attr, DDI_DMA_SLEEP,
1440	    NULL, &port->samp_dmah);
1441	if (rc != DDI_SUCCESS) {
1442		audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc);
1443		return (DDI_FAILURE);
1444	}
1445	/* allocate DMA buffer */
1446	rc = ddi_dma_mem_alloc(port->samp_dmah, port->samp_size, &buf_attr,
1447	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->samp_kaddr,
1448	    &port->samp_size, &port->samp_acch);
1449	if (rc == DDI_FAILURE) {
1450		audio_dev_warn(adev, "dma_mem_alloc (%d) failed: %d",
1451		    port->samp_size, rc);
1452		return (DDI_FAILURE);
1453	}
1454
1455	/* bind DMA buffer */
1456	rc = ddi_dma_addr_bind_handle(port->samp_dmah, NULL,
1457	    port->samp_kaddr, port->samp_size, dir|DDI_DMA_CONSISTENT,
1458	    DDI_DMA_SLEEP, NULL, &cookie, &count);
1459	if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
1460		audio_dev_warn(adev,
1461		    "ddi_dma_addr_bind_handle failed: %d", rc);
1462		return (DDI_FAILURE);
1463	}
1464	port->samp_paddr = cookie.dmac_address;
1465
1466	/*
1467	 * now, from here we allocate DMA memory for buffer descriptor list.
1468	 * we allocate adjacent DMA memory for all DMA engines.
1469	 */
1470	rc = ddi_dma_alloc_handle(dip, &bdlist_dma_attr, DDI_DMA_SLEEP,
1471	    NULL, &port->bdl_dmah);
1472	if (rc != DDI_SUCCESS) {
1473		audio_dev_warn(adev, "ddi_dma_alloc_handle(bdlist) failed");
1474		return (DDI_FAILURE);
1475	}
1476
1477	/*
1478	 * we allocate all buffer descriptors lists in continuous dma memory.
1479	 */
1480	port->bdl_size = sizeof (i810_bd_entry_t) * I810_BD_NUMS;
1481	rc = ddi_dma_mem_alloc(port->bdl_dmah, port->bdl_size,
1482	    &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1483	    &port->bdl_kaddr, &port->bdl_size, &port->bdl_acch);
1484	if (rc != DDI_SUCCESS) {
1485		audio_dev_warn(adev, "ddi_dma_mem_alloc(bdlist) failed");
1486		return (DDI_FAILURE);
1487	}
1488
1489	rc = ddi_dma_addr_bind_handle(port->bdl_dmah, NULL, port->bdl_kaddr,
1490	    port->bdl_size, DDI_DMA_WRITE|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1491	    NULL, &cookie, &count);
1492	if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
1493		audio_dev_warn(adev, "addr_bind_handle failed");
1494		return (DDI_FAILURE);
1495	}
1496	port->bdl_paddr = cookie.dmac_address;
1497
1498	/*
1499	 * Wire up the BD list.
1500	 */
1501	paddr = port->samp_paddr;
1502	bdentry = (void *)port->bdl_kaddr;
1503	for (int i = 0; i < I810_BD_NUMS; i++) {
1504
1505		/* set base address of buffer */
1506		ddi_put32(port->bdl_acch, &bdentry->buf_base, paddr);
1507		/*
1508		 * SiS 7012 counts samples in bytes, all other count
1509		 * in words.
1510		 */
1511		ddi_put16(port->bdl_acch, &bdentry->buf_len,
1512		    statep->quirk == QUIRK_SIS7012 ? port->fragsz :
1513		    port->fragsz / 2);
1514		ddi_put16(port->bdl_acch, &bdentry->buf_cmd,
1515		    BUF_CMD_IOC | BUF_CMD_BUP);
1516		paddr += port->fragsz;
1517		if ((i % port->nfrag) == (port->nfrag - 1)) {
1518			/* handle wrap */
1519			paddr = port->samp_paddr;
1520		}
1521		bdentry++;
1522	}
1523	(void) ddi_dma_sync(port->bdl_dmah, 0, 0, DDI_DMA_SYNC_FORDEV);
1524
1525	port->engine = audio_engine_alloc(&audio810_engine_ops, caps);
1526	if (port->engine == NULL) {
1527		audio_dev_warn(adev, "audio_engine_alloc failed");
1528		return (DDI_FAILURE);
1529	}
1530
1531	audio_engine_set_private(port->engine, port);
1532	audio_dev_add_engine(adev, port->engine);
1533
1534	return (DDI_SUCCESS);
1535}
1536
1537/*
1538 * audio810_free_port()
1539 *
1540 * Description:
1541 *	This routine unbinds the DMA cookies, frees the DMA buffers,
1542 *	deallocates the DMA handles.
1543 *
1544 * Arguments:
1545 *	audio810_port_t	*port	The port structure for a DMA engine.
1546 */
1547static void
1548audio810_free_port(audio810_port_t *port)
1549{
1550	if (port == NULL)
1551		return;
1552
1553	if (port->engine) {
1554		audio_dev_remove_engine(port->statep->adev, port->engine);
1555		audio_engine_free(port->engine);
1556	}
1557	if (port->bdl_paddr) {
1558		(void) ddi_dma_unbind_handle(port->bdl_dmah);
1559	}
1560	if (port->bdl_acch) {
1561		ddi_dma_mem_free(&port->bdl_acch);
1562	}
1563	if (port->bdl_dmah) {
1564		ddi_dma_free_handle(&port->bdl_dmah);
1565	}
1566	if (port->samp_paddr) {
1567		(void) ddi_dma_unbind_handle(port->samp_dmah);
1568	}
1569	if (port->samp_acch) {
1570		ddi_dma_mem_free(&port->samp_acch);
1571	}
1572	if (port->samp_dmah) {
1573		ddi_dma_free_handle(&port->samp_dmah);
1574	}
1575	kmem_free(port, sizeof (*port));
1576}
1577
1578/*
1579 * audio810_map_regs()
1580 *
1581 * Description:
1582 *	The registers are mapped in.
1583 *
1584 * Arguments:
1585 *	dev_info_t	*dip	Pointer to the device's devinfo
1586 *
1587 * Returns:
1588 *	DDI_SUCCESS		Registers successfully mapped
1589 *	DDI_FAILURE		Registers not successfully mapped
1590 */
1591static int
1592audio810_map_regs(dev_info_t *dip, audio810_state_t *statep)
1593{
1594	uint_t			nregs = 0;
1595	int			*regs_list;
1596	int			i;
1597	int			pciBar1 = 0;
1598	int			pciBar2 = 0;
1599	int			pciBar3 = 0;
1600	int			pciBar4 = 0;
1601
1602	/* check the "reg" property to get the length of memory-mapped I/O */
1603	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1604	    "reg", (int **)&regs_list, &nregs) != DDI_PROP_SUCCESS) {
1605		audio_dev_warn(statep->adev, "inquire regs property failed");
1606		goto error;
1607	}
1608	/*
1609	 * Some hardwares, such as Intel ICH0/ICH and AMD 8111, use PCI 0x10
1610	 * and 0x14 BAR separately for native audio mixer BAR and native bus
1611	 * mastering BAR. More advanced hardwares, such as Intel ICH4 and ICH5,
1612	 * support PCI memory BAR, via PCI 0x18 and 0x1C BAR, that allows for
1613	 * higher performance access to the controller register. All features
1614	 * can be accessed via this BAR making the I/O BAR (PCI 0x10 and 0x14
1615	 * BAR) capabilities obsolete. However, these controller maintain the
1616	 * I/O BAR capability to allow for the reuse of legacy code maintaining
1617	 * backward compatibility. The I/O BAR is disabled unless system BIOS
1618	 * enables the simultaneous backward compatible capability on the 0x41
1619	 * register.
1620	 *
1621	 * When I/O BAR is enabled, the value of "reg" property should be like
1622	 * this,
1623	 *	phys_hi   phys_mid  phys_lo   size_hi   size_lo
1624	 * --------------------------------------------------------
1625	 *	0000fd00  00000000  00000000  00000000  00000000
1626	 *	0100fd10  00000000  00000000  00000000  00000100
1627	 *	0100fd14  00000000  00000000  00000000  00000040
1628	 *	0200fd18  00000000  00000000  00000000  00000200
1629	 *	0200fd1c  00000000  00000000  00000000  00000100
1630	 *
1631	 * When I/O BAR is disabled, the "reg" property of the device node does
1632	 * not consist of the description for the I/O BAR. The following example
1633	 * illustrates the vaule of "reg" property,
1634	 *
1635	 *	phys_hi   phys_mid  phys_lo   size_hi   size_lo
1636	 * --------------------------------------------------------
1637	 *	0000fd00  00000000  00000000  00000000  00000000
1638	 *	0200fd18  00000000  00000000  00000000  00000200
1639	 *	0200fd1c  00000000  00000000  00000000  00000100
1640	 *
1641	 * If the hardware has memory-mapped I/O access, first try to use
1642	 * this facility, otherwise we will try I/O access.
1643	 */
1644	for (i = 1; i < nregs/I810_INTS_PER_REG_PROP; i++) {
1645		switch (regs_list[I810_INTS_PER_REG_PROP * i] & 0x000000ff) {
1646			case 0x10:
1647				pciBar1 = i;
1648				break;
1649			case 0x14:
1650				pciBar2 = i;
1651				break;
1652			case 0x18:
1653				pciBar3 = i;
1654				break;
1655			case 0x1c:
1656				pciBar4 = i;
1657				break;
1658			default:	/* we don't care others */
1659				break;
1660		}
1661	}
1662
1663	if ((pciBar3 != 0) && (pciBar4 != 0)) {
1664		/* map audio mixer registers */
1665		if ((ddi_regs_map_setup(dip, pciBar3, &statep->am_regs_base, 0,
1666		    0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) {
1667			audio_dev_warn(statep->adev,
1668			    "memory am mapping failed");
1669			goto error;
1670		}
1671
1672		/* map bus master register */
1673		if ((ddi_regs_map_setup(dip, pciBar4, &statep->bm_regs_base, 0,
1674		    0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) {
1675			audio_dev_warn(statep->adev,
1676			    "memory bm mapping failed");
1677			goto error;
1678		}
1679
1680	} else if ((pciBar1 != 0) && (pciBar2 != 0)) {
1681		/* map audio mixer registers */
1682		if ((ddi_regs_map_setup(dip, pciBar1, &statep->am_regs_base, 0,
1683		    0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) {
1684			audio_dev_warn(statep->adev, "I/O am mapping failed");
1685			goto error;
1686		}
1687
1688		/* map bus master register */
1689		if ((ddi_regs_map_setup(dip, pciBar2, &statep->bm_regs_base, 0,
1690		    0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) {
1691			audio_dev_warn(statep->adev, "I/O bm mapping failed");
1692			goto error;
1693		}
1694	} else {
1695		audio_dev_warn(statep->adev, "map_regs() pci BAR error");
1696		goto error;
1697	}
1698
1699	ddi_prop_free(regs_list);
1700
1701	return (DDI_SUCCESS);
1702
1703error:
1704	if (nregs > 0) {
1705		ddi_prop_free(regs_list);
1706	}
1707	audio810_unmap_regs(statep);
1708
1709	return (DDI_FAILURE);
1710}
1711
1712/*
1713 * audio810_unmap_regs()
1714 *
1715 * Description:
1716 *	This routine unmaps control registers.
1717 *
1718 * Arguments:
1719 *	audio810_state_t	*state	The device's state structure
1720 */
1721static void
1722audio810_unmap_regs(audio810_state_t *statep)
1723{
1724	if (statep->bm_regs_handle) {
1725		ddi_regs_map_free(&statep->bm_regs_handle);
1726	}
1727
1728	if (statep->am_regs_handle) {
1729		ddi_regs_map_free(&statep->am_regs_handle);
1730	}
1731}
1732
1733/*
1734 * audio810_chip_init()
1735 *
1736 * Description:
1737 *	This routine initializes the AMD 8111 audio controller.
1738 *	codec.
1739 *
1740 * Arguments:
1741 *	audio810_state_t	*state		The device's state structure
1742 *
1743 * Returns:
1744 *	DDI_SUCCESS	The hardware was initialized properly
1745 *	DDI_FAILURE	The hardware couldn't be initialized properly
1746 */
1747static int
1748audio810_chip_init(audio810_state_t *statep)
1749{
1750	uint32_t	gcr;
1751	uint32_t	gsr;
1752	uint32_t	codec_ready;
1753	int 		loop;
1754	clock_t		ticks;
1755
1756	gcr = I810_BM_GET32(I810_REG_GCR);
1757	ticks = drv_usectohz(100);
1758
1759	/*
1760	 * Clear the channels bits for now.  We'll set them later in
1761	 * reset port.
1762	 */
1763	if (statep->quirk == QUIRK_SIS7012) {
1764		gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_SIS_CHANNELS_MASK);
1765	} else {
1766		gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_CHANNELS_MASK);
1767	}
1768
1769	/*
1770	 * Datasheet(ICH5, document number of Intel: 252751-001):
1771	 * 3.6.5.5(page 37)
1772	 * 	if reset bit(bit1) is "0", driver must set it
1773	 * 	to "1" to de-assert the AC_RESET# signal in AC
1774	 * 	link, thus completing a cold reset. But if the
1775	 * 	bit is "1", then a warm reset is required.
1776	 */
1777	gcr |= (gcr & I810_GCR_COLD_RST) == 0 ?
1778	    I810_GCR_COLD_RST:I810_GCR_WARM_RST;
1779	I810_BM_PUT32(I810_REG_GCR, gcr);
1780
1781	/* according AC'97 spec, wait for codec reset */
1782	for (loop = 6000; --loop >= 0; ) {
1783		delay(ticks);
1784		gcr = I810_BM_GET32(I810_REG_GCR);
1785		if ((gcr & I810_GCR_WARM_RST) == 0) {
1786			break;
1787		}
1788	}
1789
1790	/* codec reset failed */
1791	if (loop < 0) {
1792		audio_dev_warn(statep->adev, "Failed to reset codec");
1793		return (DDI_FAILURE);
1794	}
1795
1796	/*
1797	 * Wait for codec ready. The hardware can provide the state of
1798	 * codec ready bit on SDATA_IN[0], SDATA_IN[1] or SDATA_IN[2]
1799	 */
1800	codec_ready =
1801	    I810_GSR_PRI_READY | I810_GSR_SEC_READY | I810_GSR_TRI_READY;
1802	for (loop = 7000; --loop >= 0; ) {
1803		delay(ticks);
1804		gsr = I810_BM_GET32(I810_REG_GSR);
1805		if ((gsr & codec_ready) != 0) {
1806			break;
1807		}
1808	}
1809	if (loop < 0) {
1810		audio_dev_warn(statep->adev, "No codec ready signal received");
1811		return (DDI_FAILURE);
1812	}
1813
1814	/*
1815	 * put the audio controller into quiet state, everything off
1816	 */
1817	audio810_stop_dma(statep);
1818
1819	return (DDI_SUCCESS);
1820}
1821
1822/*
1823 * audio810_stop_dma()
1824 *
1825 * Description:
1826 *	This routine is used to put each DMA engine into the quiet state.
1827 *
1828 * Arguments:
1829 *	audio810_state_t	*state		The device's state structure
1830 */
1831static void
1832audio810_stop_dma(audio810_state_t *statep)
1833{
1834	if (statep->bm_regs_handle == NULL) {
1835		return;
1836	}
1837	/* pause bus master (needed for the following reset register) */
1838	I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, 0x0);
1839	I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, 0x0);
1840	I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, 0x0);
1841
1842	/* and then reset the bus master registers for a three DMA engines */
1843	I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, I810_BM_CR_RST);
1844	I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, I810_BM_CR_RST);
1845	I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, I810_BM_CR_RST);
1846}
1847
1848
1849/*
1850 * audio810_codec_sync()
1851 *
1852 * Description:
1853 *	Serialize access to the AC97 audio mixer registers.
1854 *
1855 * Arguments:
1856 *	audio810_state_t	*state		The device's state structure
1857 *
1858 * Returns:
1859 *	DDI_SUCCESS		Ready for an I/O access to the codec
1860 *	DDI_FAILURE		An I/O access is currently in progress, can't
1861 *				perform another I/O access.
1862 */
1863static int
1864audio810_codec_sync(audio810_state_t *statep)
1865{
1866	int 		i;
1867	uint16_t	casr;
1868
1869	for (i = 0; i < 300; i++) {
1870		casr = I810_BM_GET8(I810_REG_CASR);
1871		if ((casr & 1) == 0) {
1872			return (DDI_SUCCESS);
1873		}
1874		drv_usecwait(10);
1875	}
1876
1877	return (DDI_FAILURE);
1878}
1879
1880/*
1881 * audio810_write_ac97()
1882 *
1883 * Description:
1884 *	Set the specific AC97 Codec register.
1885 *
1886 * Arguments:
1887 *	void		*arg		The device's state structure
1888 *	uint8_t		reg		AC97 register number
1889 *	uint16_t	data		The data want to be set
1890 */
1891static void
1892audio810_write_ac97(void *arg, uint8_t reg, uint16_t data)
1893{
1894	audio810_state_t	*statep = arg;
1895
1896	mutex_enter(&statep->ac_lock);
1897	if (audio810_codec_sync(statep) == DDI_SUCCESS) {
1898		I810_AM_PUT16(reg, data);
1899	}
1900	mutex_exit(&statep->ac_lock);
1901
1902	(void) audio810_read_ac97(statep, reg);
1903}
1904
1905/*
1906 * audio810_read_ac97()
1907 *
1908 * Description:
1909 *	Get the specific AC97 Codec register.
1910 *
1911 * Arguments:
1912 *	void		*arg		The device's state structure
1913 *	uint8_t		reg		AC97 register number
1914 *
1915 * Returns:
1916 *	The register value.
1917 */
1918static uint16_t
1919audio810_read_ac97(void *arg, uint8_t reg)
1920{
1921	audio810_state_t	*statep = arg;
1922	uint16_t		val = 0xffff;
1923
1924	mutex_enter(&statep->ac_lock);
1925	if (audio810_codec_sync(statep) == DDI_SUCCESS) {
1926		val = I810_AM_GET16(reg);
1927	}
1928	mutex_exit(&statep->ac_lock);
1929	return (val);
1930}
1931
1932/*
1933 * audio810_destroy()
1934 *
1935 * Description:
1936 *	This routine releases all resources held by the device instance,
1937 *	as part of either detach or a failure in attach.
1938 *
1939 * Arguments:
1940 *	audio810_state_t	*state	The device soft state.
1941 */
1942void
1943audio810_destroy(audio810_state_t *statep)
1944{
1945	/* stop DMA engines */
1946	audio810_stop_dma(statep);
1947
1948	if (statep->intr_added) {
1949		ddi_remove_intr(statep->dip, 0, NULL);
1950	}
1951
1952	if (statep->ksp) {
1953		kstat_delete(statep->ksp);
1954	}
1955
1956	for (int i = 0; i < I810_NUM_PORTS; i++) {
1957		audio810_free_port(statep->ports[i]);
1958	}
1959
1960	audio810_unmap_regs(statep);
1961
1962	if (statep->ac97)
1963		ac97_free(statep->ac97);
1964
1965	if (statep->adev)
1966		audio_dev_free(statep->adev);
1967
1968	mutex_destroy(&statep->inst_lock);
1969	mutex_destroy(&statep->ac_lock);
1970	kmem_free(statep, sizeof (*statep));
1971}
1972