xref: /illumos-gate/usr/src/uts/common/io/audio/drv/audio810/audio810.c (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
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  */
79 static int audio810_ddi_attach(dev_info_t *, ddi_attach_cmd_t);
80 static int audio810_ddi_detach(dev_info_t *, ddi_detach_cmd_t);
81 static int audio810_ddi_quiesce(dev_info_t *);
82 
83 /*
84  * Entry point routine prototypes
85  */
86 static int audio810_open(void *, int, unsigned *, unsigned *, caddr_t *);
87 static void audio810_close(void *);
88 static int audio810_start(void *);
89 static void audio810_stop(void *);
90 static int audio810_format(void *);
91 static int audio810_channels(void *);
92 static int audio810_rate(void *);
93 static uint64_t audio810_count(void *);
94 static void audio810_sync(void *, unsigned);
95 static size_t audio810_qlen(void *);
96 
97 static 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  */
114 static uint_t	audio810_intr(caddr_t);
115 
116 /*
117  * Local Routine Prototypes
118  */
119 static int audio810_attach(dev_info_t *);
120 static int audio810_resume(dev_info_t *);
121 static int audio810_detach(dev_info_t *);
122 static int audio810_suspend(dev_info_t *);
123 
124 static int audio810_alloc_port(audio810_state_t *, int, uint8_t);
125 static void audio810_start_port(audio810_port_t *);
126 static void audio810_stop_port(audio810_port_t *);
127 static void audio810_reset_port(audio810_port_t *);
128 static void audio810_update_port(audio810_port_t *);
129 static int audio810_codec_sync(audio810_state_t *);
130 static void audio810_write_ac97(void *, uint8_t, uint16_t);
131 static uint16_t audio810_read_ac97(void *, uint8_t);
132 static int audio810_map_regs(dev_info_t *, audio810_state_t *);
133 static void audio810_unmap_regs(audio810_state_t *);
134 static void audio810_stop_dma(audio810_state_t *);
135 static int audio810_chip_init(audio810_state_t *);
136 static 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 */
143 static char	*audio810_name = I810_NAME;
144 
145 
146 /*
147  * DDI Structures
148  */
149 
150 /* Device operations structure */
151 static 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 */
167 static 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 */
174 static 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  */
183 static struct ddi_device_acc_attr dev_attr = {
184 	DDI_DEVICE_ATTR_V0,
185 	DDI_STRUCTURE_LE_ACC,
186 	DDI_STRICTORDER_ACC
187 };
188 
189 static 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  */
198 static 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  */
216 static 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  */
244 int
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  */
270 int
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  */
297 int
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  */
320 static int
321 audio810_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  */
347 static int
348 audio810_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  */
373 static int
374 audio810_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  */
407 static uint_t
408 audio810_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  */
488 static int
489 audio810_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  */
520 static void
521 audio810_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  */
542 static void
543 audio810_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  */
568 static int
569 audio810_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  */
595 static int
596 audio810_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  */
616 static int
617 audio810_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  */
636 static int
637 audio810_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  */
656 static uint64_t
657 audio810_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  */
691 static void
692 audio810_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  */
712 static size_t
713 audio810_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  */
730 static void
731 audio810_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  */
759 static void
760 audio810_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  */
786 static void
787 audio810_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  */
884 static void
885 audio810_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  */
940 static int
941 audio810_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 	name = "Unknown AC'97";
1003 	vers = "";
1004 
1005 	statep->quirk = QUIRK_NONE;
1006 	switch (devid) {
1007 	case 0x80862415:
1008 		name = "Intel AC'97";
1009 		vers = "ICH";
1010 		break;
1011 	case 0x80862425:
1012 		name = "Intel AC'97";
1013 		vers = "ICH0";
1014 		break;
1015 	case 0x80867195:
1016 		name = "Intel AC'97";
1017 		vers = "440MX";
1018 		break;
1019 	case 0x80862445:
1020 		name = "Intel AC'97";
1021 		vers = "ICH2";
1022 		break;
1023 	case 0x80862485:
1024 		name = "Intel AC'97";
1025 		vers = "ICH3";
1026 		break;
1027 	case 0x808624C5:
1028 		name = "Intel AC'97";
1029 		vers = "ICH4";
1030 		break;
1031 	case 0x808624D5:
1032 		name = "Intel AC'97";
1033 		vers = "ICH5";
1034 		break;
1035 	case 0x8086266E:
1036 		name = "Intel AC'97";
1037 		vers = "ICH6";
1038 		break;
1039 	case 0x808627DE:
1040 		name = "Intel AC'97";
1041 		vers = "ICH7";
1042 		break;
1043 	case 0x808625A6:
1044 		name = "Intel AC'97";
1045 		vers = "6300ESB";
1046 		break;
1047 	case 0x80862698:
1048 		name = "Intel AC'97";
1049 		vers = "ESB2";
1050 		break;
1051 	case 0x10397012:
1052 		name = "SiS AC'97";
1053 		vers = "7012";
1054 		statep->quirk = QUIRK_SIS7012;
1055 		break;
1056 	case 0x10de01b1:	/* nForce */
1057 		name = "NVIDIA AC'97";
1058 		vers = "MCP1";
1059 		break;
1060 	case 0x10de006a:	/* nForce 2 */
1061 		name = "NVIDIA AC'97";
1062 		vers = "MCP2";
1063 		break;
1064 	case 0x10de00da:	/* nForce 3 */
1065 		name = "NVIDIA AC'97";
1066 		vers = "MCP3";
1067 		break;
1068 	case 0x10de00ea:
1069 		name = "NVIDIA AC'97";
1070 		vers = "CK8S";
1071 		break;
1072 	case 0x10de0059:
1073 		name = "NVIDIA AC'97";
1074 		vers = "CK804";
1075 		break;
1076 	case 0x10de008a:
1077 		name = "NVIDIA AC'97";
1078 		vers = "CK8";
1079 		break;
1080 	case 0x10de003a:	/* nForce 4 */
1081 		name = "NVIDIA AC'97";
1082 		vers = "MCP4";
1083 		break;
1084 	case 0x10de026b:
1085 		name = "NVIDIA AC'97";
1086 		vers = "MCP51";
1087 		break;
1088 	case 0x1022746d:
1089 		name = "AMD AC'97";
1090 		vers = "8111";
1091 		break;
1092 	case 0x10227445:
1093 		name = "AMD AC'97";
1094 		vers = "AMD768";
1095 		break;
1096 	}
1097 	/* set device information */
1098 	audio_dev_set_description(adev, name);
1099 	audio_dev_set_version(adev, vers);
1100 
1101 	/* initialize audio controller and AC97 codec */
1102 	if (audio810_chip_init(statep) != DDI_SUCCESS) {
1103 		audio_dev_warn(adev, "failed to init chip");
1104 		goto error;
1105 	}
1106 
1107 	/* allocate ac97 handle */
1108 	statep->ac97 = ac97_alloc(dip, audio810_read_ac97, audio810_write_ac97,
1109 	    statep);
1110 	if (statep->ac97 == NULL) {
1111 		audio_dev_warn(adev, "failed to allocate ac97 handle");
1112 		goto error;
1113 	}
1114 
1115 	/* initialize the AC'97 part */
1116 	if (ac97_init(statep->ac97, adev) != DDI_SUCCESS) {
1117 		audio_dev_warn(adev, "ac'97 initialization failed");
1118 		goto error;
1119 	}
1120 
1121 	/*
1122 	 * Override "max-channels" property to prevent configuration
1123 	 * of 4 or 6 (or possibly even 8!) channel audio.  The default
1124 	 * is to support as many channels as the hardware can do.
1125 	 *
1126 	 * (Hmmm... perhaps this should be driven in the common
1127 	 * framework.  The framework could even offer simplistic upmix
1128 	 * and downmix for various standard configs.)
1129 	 */
1130 	maxch = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1131 	    "max-channels", ac97_num_channels(statep->ac97));
1132 	if (maxch < 2) {
1133 		maxch = 2;
1134 	}
1135 
1136 	gsr = I810_BM_GET32(I810_REG_GSR);
1137 	if (gsr & I810_GSR_CAP6CH) {
1138 		nch = 6;
1139 	} else if (gsr & I810_GSR_CAP4CH) {
1140 		nch = 4;
1141 	} else {
1142 		nch = 2;
1143 	}
1144 
1145 	statep->maxch = (uint8_t)min(nch, maxch);
1146 	statep->maxch &= ~1;
1147 
1148 	/* allocate port structures */
1149 	if ((audio810_alloc_port(statep, I810_PCM_OUT, statep->maxch) !=
1150 	    DDI_SUCCESS) ||
1151 	    (audio810_alloc_port(statep, I810_PCM_IN, 2) != DDI_SUCCESS)) {
1152 		goto error;
1153 	}
1154 
1155 	/* set up kernel statistics */
1156 	if ((statep->ksp = kstat_create(I810_NAME, ddi_get_instance(dip),
1157 	    I810_NAME, "controller", KSTAT_TYPE_INTR, 1,
1158 	    KSTAT_FLAG_PERSISTENT)) != NULL) {
1159 		kstat_install(statep->ksp);
1160 	}
1161 
1162 	/* set up the interrupt handler */
1163 	if (ddi_add_intr(dip, 0, &statep->iblock,
1164 	    NULL, audio810_intr, (caddr_t)statep) != DDI_SUCCESS) {
1165 		audio_dev_warn(adev, "bad interrupt specification");
1166 		goto error;
1167 	}
1168 	statep->intr_added = B_TRUE;
1169 
1170 	if (audio_dev_register(adev) != DDI_SUCCESS) {
1171 		audio_dev_warn(adev, "unable to register with framework");
1172 		goto error;
1173 	}
1174 
1175 	ddi_report_dev(dip);
1176 
1177 	return (DDI_SUCCESS);
1178 
1179 error:
1180 	audio810_destroy(statep);
1181 
1182 	return (DDI_FAILURE);
1183 }
1184 
1185 
1186 /*
1187  * audio810_resume()
1188  *
1189  * Description:
1190  *	Resume operation of the device after sleeping or hibernating.
1191  *	Note that this should never fail, even if hardware goes wonky,
1192  *	because the current PM framework will panic if it does.
1193  *
1194  * Arguments:
1195  *	dev_info_t	*dip	Pointer to the device's dev_info struct
1196  *
1197  * Returns:
1198  *	DDI_SUCCESS		The driver was resumed.
1199  */
1200 static int
1201 audio810_resume(dev_info_t *dip)
1202 {
1203 	audio810_state_t	*statep;
1204 	audio_dev_t		*adev;
1205 
1206 	/* this should always be valid */
1207 	statep = ddi_get_driver_private(dip);
1208 	adev = statep->adev;
1209 
1210 	ASSERT(statep != NULL);
1211 	ASSERT(dip == statep->dip);
1212 
1213 	/* Restore the audio810 chip's state */
1214 	if (audio810_chip_init(statep) != DDI_SUCCESS) {
1215 		/*
1216 		 * Note that PM gurus say we should return
1217 		 * success here.  Failure of audio shouldn't
1218 		 * be considered FATAL to the system.  The
1219 		 * upshot is that audio will not progress.
1220 		 */
1221 		audio_dev_warn(adev, "DDI_RESUME failed to init chip");
1222 		return (DDI_SUCCESS);
1223 	}
1224 
1225 	/* allow ac97 operations again */
1226 	ac97_resume(statep->ac97);
1227 
1228 	mutex_enter(&statep->inst_lock);
1229 
1230 	ASSERT(statep->suspended);
1231 	statep->suspended = B_FALSE;
1232 
1233 	for (int i = 0; i < I810_NUM_PORTS; i++) {
1234 
1235 		audio810_port_t *port = statep->ports[i];
1236 
1237 		if (port != NULL) {
1238 			/* reset framework DMA engine buffer */
1239 			if (port->engine != NULL) {
1240 				audio_engine_reset(port->engine);
1241 			}
1242 
1243 			/* reset and initialize hardware ports */
1244 			audio810_reset_port(port);
1245 			if (port->started) {
1246 				audio810_start_port(port);
1247 			} else {
1248 				audio810_stop_port(port);
1249 			}
1250 		}
1251 	}
1252 	mutex_exit(&statep->inst_lock);
1253 
1254 	return (DDI_SUCCESS);
1255 }
1256 
1257 /*
1258  * audio810_detach()
1259  *
1260  * Description:
1261  *	Detach an instance of the audio810 driver.
1262  *
1263  * Arguments:
1264  *	dev_info_t		*dip	Pointer to the device's dev_info struct
1265  *
1266  * Returns:
1267  *	DDI_SUCCESS	The driver was detached
1268  *	DDI_FAILURE	The driver couldn't be detached
1269  */
1270 static int
1271 audio810_detach(dev_info_t *dip)
1272 {
1273 	audio810_state_t	*statep;
1274 
1275 	statep = ddi_get_driver_private(dip);
1276 	ASSERT(statep != NULL);
1277 
1278 	/* don't detach us if we are still in use */
1279 	if (audio_dev_unregister(statep->adev) != DDI_SUCCESS) {
1280 		return (DDI_FAILURE);
1281 	}
1282 
1283 	audio810_destroy(statep);
1284 	return (DDI_SUCCESS);
1285 }
1286 
1287 /*
1288  * audio810_suspend()
1289  *
1290  * Description:
1291  *	Suspend an instance of the audio810 driver, in preparation for
1292  *	sleep or hibernation.
1293  *
1294  * Arguments:
1295  *	dev_info_t		*dip	Pointer to the device's dev_info struct
1296  *
1297  * Returns:
1298  *	DDI_SUCCESS	The driver was suspended
1299  */
1300 static int
1301 audio810_suspend(dev_info_t *dip)
1302 {
1303 	audio810_state_t	*statep;
1304 
1305 	statep = ddi_get_driver_private(dip);
1306 	ASSERT(statep != NULL);
1307 
1308 	ac97_suspend(statep->ac97);
1309 
1310 	mutex_enter(&statep->inst_lock);
1311 
1312 	ASSERT(statep->suspended == B_FALSE);
1313 
1314 	statep->suspended = B_TRUE; /* stop new ops */
1315 
1316 	/* stop DMA engines */
1317 	audio810_stop_dma(statep);
1318 
1319 	mutex_exit(&statep->inst_lock);
1320 
1321 	return (DDI_SUCCESS);
1322 }
1323 
1324 /*
1325  * audio810_alloc_port()
1326  *
1327  * Description:
1328  *	This routine allocates the DMA handles and the memory for the
1329  *	DMA engines to use.  It also configures the BDL lists properly
1330  *	for use.
1331  *
1332  * Arguments:
1333  *	dev_info_t	*dip	Pointer to the device's devinfo
1334  *
1335  * Returns:
1336  *	DDI_SUCCESS		Registers successfully mapped
1337  *	DDI_FAILURE		Registers not successfully mapped
1338  */
1339 static int
1340 audio810_alloc_port(audio810_state_t *statep, int num, uint8_t nchan)
1341 {
1342 	ddi_dma_cookie_t	cookie;
1343 	uint_t			count;
1344 	int			dir;
1345 	unsigned		caps;
1346 	char			*prop;
1347 	char			*nfprop;
1348 	audio_dev_t		*adev;
1349 	audio810_port_t		*port;
1350 	uint32_t		paddr;
1351 	int			rc;
1352 	dev_info_t		*dip;
1353 	i810_bd_entry_t		*bdentry;
1354 
1355 	adev = statep->adev;
1356 	dip = statep->dip;
1357 
1358 	port = kmem_zalloc(sizeof (*port), KM_SLEEP);
1359 	statep->ports[num] = port;
1360 	port->statep = statep;
1361 	port->started = B_FALSE;
1362 	port->nchan = nchan;
1363 	port->num = num;
1364 
1365 	switch (num) {
1366 	case I810_PCM_IN:
1367 		prop = "record-interrupts";
1368 		nfprop = "record-fragments";
1369 		dir = DDI_DMA_READ;
1370 		caps = ENGINE_INPUT_CAP;
1371 		port->sync_dir = DDI_DMA_SYNC_FORKERNEL;
1372 		port->regoff = I810_BASE_PCM_IN;
1373 		break;
1374 	case I810_PCM_OUT:
1375 		prop = "play-interrupts";
1376 		nfprop = "play-fragments";
1377 		dir = DDI_DMA_WRITE;
1378 		caps = ENGINE_OUTPUT_CAP;
1379 		port->sync_dir = DDI_DMA_SYNC_FORDEV;
1380 		port->regoff = I810_BASE_PCM_OUT;
1381 		break;
1382 	default:
1383 		audio_dev_warn(adev, "bad port number (%d)!", num);
1384 		return (DDI_FAILURE);
1385 	}
1386 
1387 	/*
1388 	 * SiS 7012 swaps status and picb registers.
1389 	 */
1390 	if (statep->quirk == QUIRK_SIS7012) {
1391 		port->stsoff = port->regoff + I810_OFFSET_PICB;
1392 		port->picboff = port->regoff + I810_OFFSET_SR;
1393 	} else {
1394 		port->stsoff = port->regoff + I810_OFFSET_SR;
1395 		port->picboff = port->regoff + I810_OFFSET_PICB;
1396 	}
1397 
1398 	port->intrs = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1399 	    DDI_PROP_DONTPASS, prop, I810_INTS);
1400 
1401 	/* make sure the values are good */
1402 	if (port->intrs < I810_MIN_INTS) {
1403 		audio_dev_warn(adev, "%s too low, %d, resetting to %d",
1404 		    prop, port->intrs, I810_INTS);
1405 		port->intrs = I810_INTS;
1406 	} else if (port->intrs > I810_MAX_INTS) {
1407 		audio_dev_warn(adev, "%s too high, %d, resetting to %d",
1408 		    prop, port->intrs, I810_INTS);
1409 		port->intrs = I810_INTS;
1410 	}
1411 
1412 	port->nfrag = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1413 	    DDI_PROP_DONTPASS, nfprop, I810_NFRAGS);
1414 
1415 	/*
1416 	 * Note that fragments must divide evenly into I810_BD_NUMS (32).
1417 	 */
1418 	if (port->nfrag <= 4) {
1419 		port->nfrag = 4;
1420 	} else if (port->nfrag <= 8) {
1421 		port->nfrag = 8;
1422 	} else if (port->nfrag <= 16) {
1423 		port->nfrag = 16;
1424 	} else {
1425 		port->nfrag = I810_BD_NUMS;
1426 	}
1427 
1428 	/*
1429 	 * Figure out how much space we need.  Sample rate is 48kHz, and
1430 	 * we need to store 32 chunks.  (Note that this means that low
1431 	 * interrupt frequencies will require more RAM.  We could probably
1432 	 * do some cleverness to use a shorter BD list.)
1433 	 */
1434 	port->fragfr = 48000 / port->intrs;
1435 	port->fragfr = I810_ROUNDUP(port->fragfr, I810_MOD_SIZE);
1436 	port->fragsz = port->fragfr * port->nchan * 2;
1437 	port->samp_size = port->fragsz * port->nfrag;
1438 
1439 	/* allocate dma handle */
1440 	rc = ddi_dma_alloc_handle(dip, &sample_buf_dma_attr, DDI_DMA_SLEEP,
1441 	    NULL, &port->samp_dmah);
1442 	if (rc != DDI_SUCCESS) {
1443 		audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc);
1444 		return (DDI_FAILURE);
1445 	}
1446 	/* allocate DMA buffer */
1447 	rc = ddi_dma_mem_alloc(port->samp_dmah, port->samp_size, &buf_attr,
1448 	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->samp_kaddr,
1449 	    &port->samp_size, &port->samp_acch);
1450 	if (rc == DDI_FAILURE) {
1451 		audio_dev_warn(adev, "dma_mem_alloc (%d) failed: %d",
1452 		    port->samp_size, rc);
1453 		return (DDI_FAILURE);
1454 	}
1455 
1456 	/* bind DMA buffer */
1457 	rc = ddi_dma_addr_bind_handle(port->samp_dmah, NULL,
1458 	    port->samp_kaddr, port->samp_size, dir|DDI_DMA_CONSISTENT,
1459 	    DDI_DMA_SLEEP, NULL, &cookie, &count);
1460 	if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
1461 		audio_dev_warn(adev,
1462 		    "ddi_dma_addr_bind_handle failed: %d", rc);
1463 		return (DDI_FAILURE);
1464 	}
1465 	port->samp_paddr = cookie.dmac_address;
1466 
1467 	/*
1468 	 * now, from here we allocate DMA memory for buffer descriptor list.
1469 	 * we allocate adjacent DMA memory for all DMA engines.
1470 	 */
1471 	rc = ddi_dma_alloc_handle(dip, &bdlist_dma_attr, DDI_DMA_SLEEP,
1472 	    NULL, &port->bdl_dmah);
1473 	if (rc != DDI_SUCCESS) {
1474 		audio_dev_warn(adev, "ddi_dma_alloc_handle(bdlist) failed");
1475 		return (DDI_FAILURE);
1476 	}
1477 
1478 	/*
1479 	 * we allocate all buffer descriptors lists in continuous dma memory.
1480 	 */
1481 	port->bdl_size = sizeof (i810_bd_entry_t) * I810_BD_NUMS;
1482 	rc = ddi_dma_mem_alloc(port->bdl_dmah, port->bdl_size,
1483 	    &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1484 	    &port->bdl_kaddr, &port->bdl_size, &port->bdl_acch);
1485 	if (rc != DDI_SUCCESS) {
1486 		audio_dev_warn(adev, "ddi_dma_mem_alloc(bdlist) failed");
1487 		return (DDI_FAILURE);
1488 	}
1489 
1490 	rc = ddi_dma_addr_bind_handle(port->bdl_dmah, NULL, port->bdl_kaddr,
1491 	    port->bdl_size, DDI_DMA_WRITE|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1492 	    NULL, &cookie, &count);
1493 	if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
1494 		audio_dev_warn(adev, "addr_bind_handle failed");
1495 		return (DDI_FAILURE);
1496 	}
1497 	port->bdl_paddr = cookie.dmac_address;
1498 
1499 	/*
1500 	 * Wire up the BD list.
1501 	 */
1502 	paddr = port->samp_paddr;
1503 	bdentry = (void *)port->bdl_kaddr;
1504 	for (int i = 0; i < I810_BD_NUMS; i++) {
1505 
1506 		/* set base address of buffer */
1507 		ddi_put32(port->bdl_acch, &bdentry->buf_base, paddr);
1508 		/*
1509 		 * SiS 7012 counts samples in bytes, all other count
1510 		 * in words.
1511 		 */
1512 		ddi_put16(port->bdl_acch, &bdentry->buf_len,
1513 		    statep->quirk == QUIRK_SIS7012 ? port->fragsz :
1514 		    port->fragsz / 2);
1515 		ddi_put16(port->bdl_acch, &bdentry->buf_cmd,
1516 		    BUF_CMD_IOC | BUF_CMD_BUP);
1517 		paddr += port->fragsz;
1518 		if ((i % port->nfrag) == (port->nfrag - 1)) {
1519 			/* handle wrap */
1520 			paddr = port->samp_paddr;
1521 		}
1522 		bdentry++;
1523 	}
1524 	(void) ddi_dma_sync(port->bdl_dmah, 0, 0, DDI_DMA_SYNC_FORDEV);
1525 
1526 	port->engine = audio_engine_alloc(&audio810_engine_ops, caps);
1527 	if (port->engine == NULL) {
1528 		audio_dev_warn(adev, "audio_engine_alloc failed");
1529 		return (DDI_FAILURE);
1530 	}
1531 
1532 	audio_engine_set_private(port->engine, port);
1533 	audio_dev_add_engine(adev, port->engine);
1534 
1535 	return (DDI_SUCCESS);
1536 }
1537 
1538 /*
1539  * audio810_free_port()
1540  *
1541  * Description:
1542  *	This routine unbinds the DMA cookies, frees the DMA buffers,
1543  *	deallocates the DMA handles.
1544  *
1545  * Arguments:
1546  *	audio810_port_t	*port	The port structure for a DMA engine.
1547  */
1548 static void
1549 audio810_free_port(audio810_port_t *port)
1550 {
1551 	if (port == NULL)
1552 		return;
1553 
1554 	if (port->engine) {
1555 		audio_dev_remove_engine(port->statep->adev, port->engine);
1556 		audio_engine_free(port->engine);
1557 	}
1558 	if (port->bdl_paddr) {
1559 		(void) ddi_dma_unbind_handle(port->bdl_dmah);
1560 	}
1561 	if (port->bdl_acch) {
1562 		ddi_dma_mem_free(&port->bdl_acch);
1563 	}
1564 	if (port->bdl_dmah) {
1565 		ddi_dma_free_handle(&port->bdl_dmah);
1566 	}
1567 	if (port->samp_paddr) {
1568 		(void) ddi_dma_unbind_handle(port->samp_dmah);
1569 	}
1570 	if (port->samp_acch) {
1571 		ddi_dma_mem_free(&port->samp_acch);
1572 	}
1573 	if (port->samp_dmah) {
1574 		ddi_dma_free_handle(&port->samp_dmah);
1575 	}
1576 	kmem_free(port, sizeof (*port));
1577 }
1578 
1579 /*
1580  * audio810_map_regs()
1581  *
1582  * Description:
1583  *	The registers are mapped in.
1584  *
1585  * Arguments:
1586  *	dev_info_t	*dip	Pointer to the device's devinfo
1587  *
1588  * Returns:
1589  *	DDI_SUCCESS		Registers successfully mapped
1590  *	DDI_FAILURE		Registers not successfully mapped
1591  */
1592 static int
1593 audio810_map_regs(dev_info_t *dip, audio810_state_t *statep)
1594 {
1595 	uint_t			nregs = 0;
1596 	int			*regs_list;
1597 	int			i;
1598 	int			pciBar1 = 0;
1599 	int			pciBar2 = 0;
1600 	int			pciBar3 = 0;
1601 	int			pciBar4 = 0;
1602 
1603 	/* check the "reg" property to get the length of memory-mapped I/O */
1604 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1605 	    "reg", (int **)&regs_list, &nregs) != DDI_PROP_SUCCESS) {
1606 		audio_dev_warn(statep->adev, "inquire regs property failed");
1607 		goto error;
1608 	}
1609 	/*
1610 	 * Some hardwares, such as Intel ICH0/ICH and AMD 8111, use PCI 0x10
1611 	 * and 0x14 BAR separately for native audio mixer BAR and native bus
1612 	 * mastering BAR. More advanced hardwares, such as Intel ICH4 and ICH5,
1613 	 * support PCI memory BAR, via PCI 0x18 and 0x1C BAR, that allows for
1614 	 * higher performance access to the controller register. All features
1615 	 * can be accessed via this BAR making the I/O BAR (PCI 0x10 and 0x14
1616 	 * BAR) capabilities obsolete. However, these controller maintain the
1617 	 * I/O BAR capability to allow for the reuse of legacy code maintaining
1618 	 * backward compatibility. The I/O BAR is disabled unless system BIOS
1619 	 * enables the simultaneous backward compatible capability on the 0x41
1620 	 * register.
1621 	 *
1622 	 * When I/O BAR is enabled, the value of "reg" property should be like
1623 	 * this,
1624 	 *	phys_hi   phys_mid  phys_lo   size_hi   size_lo
1625 	 * --------------------------------------------------------
1626 	 *	0000fd00  00000000  00000000  00000000  00000000
1627 	 *	0100fd10  00000000  00000000  00000000  00000100
1628 	 *	0100fd14  00000000  00000000  00000000  00000040
1629 	 *	0200fd18  00000000  00000000  00000000  00000200
1630 	 *	0200fd1c  00000000  00000000  00000000  00000100
1631 	 *
1632 	 * When I/O BAR is disabled, the "reg" property of the device node does
1633 	 * not consist of the description for the I/O BAR. The following example
1634 	 * illustrates the vaule of "reg" property,
1635 	 *
1636 	 *	phys_hi   phys_mid  phys_lo   size_hi   size_lo
1637 	 * --------------------------------------------------------
1638 	 *	0000fd00  00000000  00000000  00000000  00000000
1639 	 *	0200fd18  00000000  00000000  00000000  00000200
1640 	 *	0200fd1c  00000000  00000000  00000000  00000100
1641 	 *
1642 	 * If the hardware has memory-mapped I/O access, first try to use
1643 	 * this facility, otherwise we will try I/O access.
1644 	 */
1645 	for (i = 1; i < nregs/I810_INTS_PER_REG_PROP; i++) {
1646 		switch (regs_list[I810_INTS_PER_REG_PROP * i] & 0x000000ff) {
1647 			case 0x10:
1648 				pciBar1 = i;
1649 				break;
1650 			case 0x14:
1651 				pciBar2 = i;
1652 				break;
1653 			case 0x18:
1654 				pciBar3 = i;
1655 				break;
1656 			case 0x1c:
1657 				pciBar4 = i;
1658 				break;
1659 			default:	/* we don't care others */
1660 				break;
1661 		}
1662 	}
1663 
1664 	if ((pciBar3 != 0) && (pciBar4 != 0)) {
1665 		/* map audio mixer registers */
1666 		if ((ddi_regs_map_setup(dip, pciBar3, &statep->am_regs_base, 0,
1667 		    0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) {
1668 			audio_dev_warn(statep->adev,
1669 			    "memory am mapping failed");
1670 			goto error;
1671 		}
1672 
1673 		/* map bus master register */
1674 		if ((ddi_regs_map_setup(dip, pciBar4, &statep->bm_regs_base, 0,
1675 		    0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) {
1676 			audio_dev_warn(statep->adev,
1677 			    "memory bm mapping failed");
1678 			goto error;
1679 		}
1680 
1681 	} else if ((pciBar1 != 0) && (pciBar2 != 0)) {
1682 		/* map audio mixer registers */
1683 		if ((ddi_regs_map_setup(dip, pciBar1, &statep->am_regs_base, 0,
1684 		    0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) {
1685 			audio_dev_warn(statep->adev, "I/O am mapping failed");
1686 			goto error;
1687 		}
1688 
1689 		/* map bus master register */
1690 		if ((ddi_regs_map_setup(dip, pciBar2, &statep->bm_regs_base, 0,
1691 		    0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) {
1692 			audio_dev_warn(statep->adev, "I/O bm mapping failed");
1693 			goto error;
1694 		}
1695 	} else {
1696 		audio_dev_warn(statep->adev, "map_regs() pci BAR error");
1697 		goto error;
1698 	}
1699 
1700 	ddi_prop_free(regs_list);
1701 
1702 	return (DDI_SUCCESS);
1703 
1704 error:
1705 	if (nregs > 0) {
1706 		ddi_prop_free(regs_list);
1707 	}
1708 	audio810_unmap_regs(statep);
1709 
1710 	return (DDI_FAILURE);
1711 }
1712 
1713 /*
1714  * audio810_unmap_regs()
1715  *
1716  * Description:
1717  *	This routine unmaps control registers.
1718  *
1719  * Arguments:
1720  *	audio810_state_t	*state	The device's state structure
1721  */
1722 static void
1723 audio810_unmap_regs(audio810_state_t *statep)
1724 {
1725 	if (statep->bm_regs_handle) {
1726 		ddi_regs_map_free(&statep->bm_regs_handle);
1727 	}
1728 
1729 	if (statep->am_regs_handle) {
1730 		ddi_regs_map_free(&statep->am_regs_handle);
1731 	}
1732 }
1733 
1734 /*
1735  * audio810_chip_init()
1736  *
1737  * Description:
1738  *	This routine initializes the AMD 8111 audio controller.
1739  *	codec.
1740  *
1741  * Arguments:
1742  *	audio810_state_t	*state		The device's state structure
1743  *
1744  * Returns:
1745  *	DDI_SUCCESS	The hardware was initialized properly
1746  *	DDI_FAILURE	The hardware couldn't be initialized properly
1747  */
1748 static int
1749 audio810_chip_init(audio810_state_t *statep)
1750 {
1751 	uint32_t	gcr;
1752 	uint32_t	gsr;
1753 	uint32_t	codec_ready;
1754 	int 		loop;
1755 	clock_t		ticks;
1756 
1757 	gcr = I810_BM_GET32(I810_REG_GCR);
1758 	ticks = drv_usectohz(100);
1759 
1760 	/*
1761 	 * Clear the channels bits for now.  We'll set them later in
1762 	 * reset port.
1763 	 */
1764 	if (statep->quirk == QUIRK_SIS7012) {
1765 		gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_SIS_CHANNELS_MASK);
1766 	} else {
1767 		gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_CHANNELS_MASK);
1768 	}
1769 
1770 	/*
1771 	 * Datasheet(ICH5, document number of Intel: 252751-001):
1772 	 * 3.6.5.5(page 37)
1773 	 * 	if reset bit(bit1) is "0", driver must set it
1774 	 * 	to "1" to de-assert the AC_RESET# signal in AC
1775 	 * 	link, thus completing a cold reset. But if the
1776 	 * 	bit is "1", then a warm reset is required.
1777 	 */
1778 	gcr |= (gcr & I810_GCR_COLD_RST) == 0 ?
1779 	    I810_GCR_COLD_RST:I810_GCR_WARM_RST;
1780 	I810_BM_PUT32(I810_REG_GCR, gcr);
1781 
1782 	/* according AC'97 spec, wait for codec reset */
1783 	for (loop = 6000; --loop >= 0; ) {
1784 		delay(ticks);
1785 		gcr = I810_BM_GET32(I810_REG_GCR);
1786 		if ((gcr & I810_GCR_WARM_RST) == 0) {
1787 			break;
1788 		}
1789 	}
1790 
1791 	/* codec reset failed */
1792 	if (loop < 0) {
1793 		audio_dev_warn(statep->adev, "Failed to reset codec");
1794 		return (DDI_FAILURE);
1795 	}
1796 
1797 	/*
1798 	 * Wait for codec ready. The hardware can provide the state of
1799 	 * codec ready bit on SDATA_IN[0], SDATA_IN[1] or SDATA_IN[2]
1800 	 */
1801 	codec_ready =
1802 	    I810_GSR_PRI_READY | I810_GSR_SEC_READY | I810_GSR_TRI_READY;
1803 	for (loop = 7000; --loop >= 0; ) {
1804 		delay(ticks);
1805 		gsr = I810_BM_GET32(I810_REG_GSR);
1806 		if ((gsr & codec_ready) != 0) {
1807 			break;
1808 		}
1809 	}
1810 	if (loop < 0) {
1811 		audio_dev_warn(statep->adev, "No codec ready signal received");
1812 		return (DDI_FAILURE);
1813 	}
1814 
1815 	/*
1816 	 * put the audio controller into quiet state, everything off
1817 	 */
1818 	audio810_stop_dma(statep);
1819 
1820 	return (DDI_SUCCESS);
1821 }
1822 
1823 /*
1824  * audio810_stop_dma()
1825  *
1826  * Description:
1827  *	This routine is used to put each DMA engine into the quiet state.
1828  *
1829  * Arguments:
1830  *	audio810_state_t	*state		The device's state structure
1831  */
1832 static void
1833 audio810_stop_dma(audio810_state_t *statep)
1834 {
1835 	if (statep->bm_regs_handle == NULL) {
1836 		return;
1837 	}
1838 	/* pause bus master (needed for the following reset register) */
1839 	I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, 0x0);
1840 	I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, 0x0);
1841 	I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, 0x0);
1842 
1843 	/* and then reset the bus master registers for a three DMA engines */
1844 	I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, I810_BM_CR_RST);
1845 	I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, I810_BM_CR_RST);
1846 	I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, I810_BM_CR_RST);
1847 }
1848 
1849 
1850 /*
1851  * audio810_codec_sync()
1852  *
1853  * Description:
1854  *	Serialize access to the AC97 audio mixer registers.
1855  *
1856  * Arguments:
1857  *	audio810_state_t	*state		The device's state structure
1858  *
1859  * Returns:
1860  *	DDI_SUCCESS		Ready for an I/O access to the codec
1861  *	DDI_FAILURE		An I/O access is currently in progress, can't
1862  *				perform another I/O access.
1863  */
1864 static int
1865 audio810_codec_sync(audio810_state_t *statep)
1866 {
1867 	int 		i;
1868 	uint16_t	casr;
1869 
1870 	for (i = 0; i < 300; i++) {
1871 		casr = I810_BM_GET8(I810_REG_CASR);
1872 		if ((casr & 1) == 0) {
1873 			return (DDI_SUCCESS);
1874 		}
1875 		drv_usecwait(10);
1876 	}
1877 
1878 	return (DDI_FAILURE);
1879 }
1880 
1881 /*
1882  * audio810_write_ac97()
1883  *
1884  * Description:
1885  *	Set the specific AC97 Codec register.
1886  *
1887  * Arguments:
1888  *	void		*arg		The device's state structure
1889  *	uint8_t		reg		AC97 register number
1890  *	uint16_t	data		The data want to be set
1891  */
1892 static void
1893 audio810_write_ac97(void *arg, uint8_t reg, uint16_t data)
1894 {
1895 	audio810_state_t	*statep = arg;
1896 
1897 	mutex_enter(&statep->ac_lock);
1898 	if (audio810_codec_sync(statep) == DDI_SUCCESS) {
1899 		I810_AM_PUT16(reg, data);
1900 	}
1901 	mutex_exit(&statep->ac_lock);
1902 
1903 	(void) audio810_read_ac97(statep, reg);
1904 }
1905 
1906 /*
1907  * audio810_read_ac97()
1908  *
1909  * Description:
1910  *	Get the specific AC97 Codec register.
1911  *
1912  * Arguments:
1913  *	void		*arg		The device's state structure
1914  *	uint8_t		reg		AC97 register number
1915  *
1916  * Returns:
1917  *	The register value.
1918  */
1919 static uint16_t
1920 audio810_read_ac97(void *arg, uint8_t reg)
1921 {
1922 	audio810_state_t	*statep = arg;
1923 	uint16_t		val = 0xffff;
1924 
1925 	mutex_enter(&statep->ac_lock);
1926 	if (audio810_codec_sync(statep) == DDI_SUCCESS) {
1927 		val = I810_AM_GET16(reg);
1928 	}
1929 	mutex_exit(&statep->ac_lock);
1930 	return (val);
1931 }
1932 
1933 /*
1934  * audio810_destroy()
1935  *
1936  * Description:
1937  *	This routine releases all resources held by the device instance,
1938  *	as part of either detach or a failure in attach.
1939  *
1940  * Arguments:
1941  *	audio810_state_t	*state	The device soft state.
1942  */
1943 void
1944 audio810_destroy(audio810_state_t *statep)
1945 {
1946 	/* stop DMA engines */
1947 	audio810_stop_dma(statep);
1948 
1949 	if (statep->intr_added) {
1950 		ddi_remove_intr(statep->dip, 0, NULL);
1951 	}
1952 
1953 	if (statep->ksp) {
1954 		kstat_delete(statep->ksp);
1955 	}
1956 
1957 	for (int i = 0; i < I810_NUM_PORTS; i++) {
1958 		audio810_free_port(statep->ports[i]);
1959 	}
1960 
1961 	audio810_unmap_regs(statep);
1962 
1963 	if (statep->ac97)
1964 		ac97_free(statep->ac97);
1965 
1966 	if (statep->adev)
1967 		audio_dev_free(statep->adev);
1968 
1969 	mutex_destroy(&statep->inst_lock);
1970 	mutex_destroy(&statep->ac_lock);
1971 	kmem_free(statep, sizeof (*statep));
1972 }
1973