xref: /haiku/src/add-ons/kernel/drivers/audio/ac97/auvia/io.c (revision 5076aaf050c0d14aa59d2e32952207124da35f2a)
1 /*
2  * Auvia BeOS Driver for Via VT82xx Southbridge audio
3  *
4  * Copyright (c) 2003, Jerome Duval (jerome.duval@free.fr)
5  *
6  * Original code : BeOS Driver for Intel ICH AC'97 Link interface
7  * Copyright (c) 2002, Marcus Overhagen <marcus@overhagen.de>
8  *
9  * All rights reserved.
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * - Redistributions of source code must retain the above copyright notice,
14  *   this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright notice,
16  *   this list of conditions and the following disclaimer in the documentation
17  *   and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 #include <KernelExport.h>
32 #include <OS.h>
33 #include "io.h"
34 #include "auviareg.h"
35 #include "debug.h"
36 #include <PCI.h>
37 
38 extern pci_module_info  *pci;
39 
40 uint8
auvia_reg_read_8(device_config * config,int regno)41 auvia_reg_read_8(device_config *config, int regno)
42 {
43 	return pci->read_io_8(config->nabmbar + regno);
44 }
45 
46 uint16
auvia_reg_read_16(device_config * config,int regno)47 auvia_reg_read_16(device_config *config, int regno)
48 {
49 	return pci->read_io_16(config->nabmbar + regno);
50 }
51 
52 uint32
auvia_reg_read_32(device_config * config,int regno)53 auvia_reg_read_32(device_config *config, int regno)
54 {
55 	return pci->read_io_32(config->nabmbar + regno);
56 }
57 
58 void
auvia_reg_write_8(device_config * config,int regno,uint8 value)59 auvia_reg_write_8(device_config *config, int regno, uint8 value)
60 {
61 	pci->write_io_8(config->nabmbar + regno, value);
62 }
63 
64 void
auvia_reg_write_16(device_config * config,int regno,uint16 value)65 auvia_reg_write_16(device_config *config, int regno, uint16 value)
66 {
67 	pci->write_io_16(config->nabmbar + regno, value);
68 }
69 
70 void
auvia_reg_write_32(device_config * config,int regno,uint32 value)71 auvia_reg_write_32(device_config *config, int regno, uint32 value)
72 {
73 	pci->write_io_32(config->nabmbar + regno, value);
74 }
75 
76 /* codec */
77 
78 #define AUVIA_TIMEOUT 	200
79 
80 static int
auvia_codec_waitready(device_config * config)81 auvia_codec_waitready(device_config *config)
82 {
83 	int i;
84 
85 	/* poll until codec not busy */
86 	for(i = 0; (i < AUVIA_TIMEOUT) && (pci->read_io_32(config->nabmbar
87 		+ AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY) ; i++)
88 		spin(1);
89 	if(i>=AUVIA_TIMEOUT) {
90 		//PRINT(("codec busy\n"));
91 		return B_ERROR;
92 	}
93 	return B_OK;
94 }
95 
96 static int
auvia_codec_waitvalid(device_config * config)97 auvia_codec_waitvalid(device_config *config)
98 {
99 	int i;
100 
101 	/* poll until codec valid */
102 	for(i = 0; (i < AUVIA_TIMEOUT) && !(pci->read_io_32(config->nabmbar
103 		+ AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID) ; i++)
104 		spin(1);
105 	if(i>=AUVIA_TIMEOUT) {
106 		//PRINT(("codec invalid\n"));
107 		return B_ERROR;
108 	}
109 	return B_OK;
110 }
111 
112 uint16
auvia_codec_read(device_config * config,int regno)113 auvia_codec_read(device_config *config, int regno)
114 {
115 	if(auvia_codec_waitready(config)!=B_OK) {
116 		PRINT(("codec busy (1)\n"));
117 		return -1;
118 	}
119 	pci->write_io_32(config->nabmbar + AUVIA_CODEC_CTL,
120 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(regno));
121 
122 	if(auvia_codec_waitready(config)!=B_OK) {
123 		PRINT(("codec busy (2)\n"));
124 		return -1;
125 	}
126 	if(auvia_codec_waitvalid(config)!=B_OK) {
127 		PRINT(("codec invalid (3)\n"));
128 		return -1;
129 	}
130 
131 	return pci->read_io_16(config->nabmbar + AUVIA_CODEC_CTL);
132 }
133 
134 void
auvia_codec_write(device_config * config,int regno,uint16 value)135 auvia_codec_write(device_config *config, int regno, uint16 value)
136 {
137 	if(auvia_codec_waitready(config)!=B_OK) {
138 		PRINT(("codec busy (4)\n"));
139 		return;
140 	}
141 	pci->write_io_32(config->nabmbar + AUVIA_CODEC_CTL,
142 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(regno) | value);
143 }
144