source: mainline/uspace/drv/audio/sb16/dsp.c@ 5984107

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5984107 was 5984107, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

sb16: Add non-automode alternative.

  • Property mode set to 100644
File size: 8.8 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup drvaudiosb16
29 * @{
30 */
31/** @file
32 * @brief DSP helper functions implementation
33 */
34
35#include <libarch/ddi.h>
36#include <str_error.h>
37
38#include "dma.h"
39#include "dma_controller.h"
40#include "ddf_log.h"
41#include "dsp_commands.h"
42#include "dsp.h"
43
44#define BUFFER_SIZE (PAGE_SIZE / 4)
45#define PLAY_BLOCK_SIZE (BUFFER_SIZE / 2)
46
47#ifndef DSP_RETRY_COUNT
48#define DSP_RETRY_COUNT 100
49#endif
50
51#define DSP_RESET_RESPONSE 0xaa
52#define SB_DMA_CHAN_16 5
53#define SB_DMA_CHAN_8 1
54
55#define AUTO_DMA_MODE
56
57#ifdef AUTO_DMA_MODE
58#undef AUTO_DMA_MODE
59#define AUTO_DMA_MODE true
60#else
61#define AUTO_DMA_MODE false
62#endif
63
64static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
65{
66 assert(data);
67 assert(dsp);
68 uint8_t status;
69 size_t attempts = DSP_RETRY_COUNT;
70 do {
71 status = pio_read_8(&dsp->regs->dsp_read_status);
72 } while (--attempts && ((status & DSP_READ_READY) == 0));
73
74 if ((status & DSP_READ_READY) == 0)
75 return EIO;
76
77 *data = pio_read_8(&dsp->regs->dsp_data_read);
78 return EOK;
79}
80/*----------------------------------------------------------------------------*/
81static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
82{
83 assert(dsp);
84 uint8_t status;
85 size_t attempts = DSP_RETRY_COUNT;
86 do {
87 status = pio_read_8(&dsp->regs->dsp_write);
88 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
89
90 if ((status & DSP_WRITE_BUSY))
91 return EIO;
92
93 pio_write_8(&dsp->regs->dsp_write, data);
94 return EOK;
95}
96/*----------------------------------------------------------------------------*/
97static inline void sb_dsp_reset(sb_dsp_t *dsp)
98{
99 assert(dsp);
100 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
101 pio_write_8(&dsp->regs->dsp_reset, 1);
102 udelay(3); /* Keep reset for 3 us */
103 pio_write_8(&dsp->regs->dsp_reset, 0);
104}
105/*----------------------------------------------------------------------------*/
106static inline int sb_setup_buffer(sb_dsp_t *dsp)
107{
108 assert(dsp);
109 uint8_t *buffer = malloc24(BUFFER_SIZE);
110 if (buffer == NULL) {
111 ddf_log_error("Failed to allocate buffer.\n");
112 return ENOMEM;
113 }
114
115 const uintptr_t pa = addr_to_phys(buffer);
116 assert(pa < (1 << 25));
117// assert((pa & 0xffff) == 0);
118 /* Set 16 bit channel */
119 const int ret = dma_setup_channel(SB_DMA_CHAN_16, pa, BUFFER_SIZE);
120 if (ret == EOK) {
121 dsp->buffer.data = buffer;
122 dsp->buffer.position = buffer;
123 dsp->buffer.size = BUFFER_SIZE;
124 bzero(buffer, BUFFER_SIZE);
125 dma_prepare_channel(
126 SB_DMA_CHAN_16, false, AUTO_DMA_MODE, BLOCK_DMA);
127 /* Set 8bit channel */
128 const int ret = dma_setup_channel(SB_DMA_CHAN_8, pa, BUFFER_SIZE);
129 if (ret == EOK) {
130 dma_prepare_channel(
131 SB_DMA_CHAN_8, false, AUTO_DMA_MODE, BLOCK_DMA);
132 } else {
133 ddf_log_warning("Failed to setup DMA8 channel: %s.\n",
134 str_error(ret));
135 }
136 } else {
137 ddf_log_error("Failed to setup DMA16 channel %s.\n",
138 str_error(ret));
139 free24(buffer);
140 }
141 return ret;
142}
143/*----------------------------------------------------------------------------*/
144static inline void sb_clear_buffer(sb_dsp_t *dsp)
145{
146 free24(dsp->buffer.data);
147 dsp->buffer.data = NULL;
148 dsp->buffer.position = NULL;
149 dsp->buffer.size = 0;
150}
151/*----------------------------------------------------------------------------*/
152int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs)
153{
154 assert(dsp);
155 dsp->regs = regs;
156 sb_dsp_reset(dsp);
157 /* "DSP takes about 100 microseconds to initialize itself" */
158 udelay(100);
159 uint8_t response;
160 const int ret = sb_dsp_read(dsp, &response);
161 if (ret != EOK) {
162 ddf_log_error("Failed to read DSP reset response value.\n");
163 return ret;
164 }
165 if (response != DSP_RESET_RESPONSE) {
166 ddf_log_error("Invalid DSP reset response: %x.\n", response);
167 return EIO;
168 }
169
170 /* Get DSP version number */
171 sb_dsp_write(dsp, DSP_VERSION);
172 sb_dsp_read(dsp, &dsp->version.major);
173 sb_dsp_read(dsp, &dsp->version.minor);
174
175 return ret;
176}
177/*----------------------------------------------------------------------------*/
178void sb_dsp_interrupt(sb_dsp_t *dsp)
179{
180 assert(dsp);
181 /* We don't really care about the mode of transport, so ack both */
182 if (dsp->version.major >= 4) {
183 /* ACK dma16 transfer interrupt */
184 pio_read_8(&dsp->regs->dma16_ack);
185 }
186 /* ACK dma8 transfer interrupt */
187 pio_read_8(&dsp->regs->dsp_read_status);
188
189 static size_t interrupt_count = 0;
190
191 const size_t remain_size = dsp->playing.size -
192 (dsp->playing.position - dsp->playing.data);
193
194 ddf_log_note("Interrupt count %zu, remaining: %zu.\n",
195 ++interrupt_count, remain_size);
196 if (remain_size == 0) {
197 ddf_log_note("Nothing more to play");
198 sb_dsp_write(dsp, DMA_16B_EXIT);
199 sb_clear_buffer(dsp);
200 return;
201 }
202 if (remain_size < PLAY_BLOCK_SIZE) {
203 ddf_log_note("Last %zu bytes to play.\n", remain_size);
204 /* This is the last block */
205 memcpy(dsp->buffer.position, dsp->playing.position, remain_size);
206 dsp->playing.position += remain_size;
207 dsp->buffer.position += remain_size;
208 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
209 sb_dsp_write(dsp, dsp->playing.mode);
210 sb_dsp_write(dsp, (remain_size - 1) & 0xff);
211 sb_dsp_write(dsp, (remain_size - 1) >> 8);
212 return;
213 }
214 ddf_log_note("Playing full block.\n");
215 memcpy(dsp->buffer.position, dsp->playing.position, PLAY_BLOCK_SIZE);
216 dsp->playing.position += PLAY_BLOCK_SIZE;
217 dsp->buffer.position += PLAY_BLOCK_SIZE;
218 /* Wrap around */
219 if (dsp->buffer.position == (dsp->buffer.data + dsp->buffer.size))
220 dsp->buffer.position = dsp->buffer.data;
221 if (!AUTO_DMA_MODE) {
222 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
223 sb_dsp_write(dsp, dsp->playing.mode);
224 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) & 0xff);
225 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) >> 8);
226 }
227
228}
229/*----------------------------------------------------------------------------*/
230int sb_dsp_play_direct(sb_dsp_t *dsp, const uint8_t *data, size_t size,
231 unsigned sampling_rate, unsigned channels, unsigned bit_depth)
232{
233 assert(dsp);
234 if (channels != 1 || bit_depth != 8)
235 return EIO;
236 /* In microseconds */
237 const unsigned wait_period = 1000000 / sampling_rate;
238 while (size--) {
239 pio_write_8(&dsp->regs->dsp_write, DIRECT_8B_OUTPUT);
240 pio_write_8(&dsp->regs->dsp_write, *data++);
241 udelay(wait_period);
242 }
243 return EOK;
244}
245/*----------------------------------------------------------------------------*/
246int sb_dsp_play(sb_dsp_t *dsp, const uint8_t *data, size_t size,
247 uint16_t sampling_rate, unsigned channels, unsigned bit_depth)
248{
249 assert(dsp);
250 if (!data)
251 return EOK;
252
253 /* Check supported parameters */
254 if (bit_depth != 8 && bit_depth != 16)
255 return ENOTSUP;
256 if (channels != 1 && channels != 2)
257 return ENOTSUP;
258
259 ddf_log_debug("Buffer prepare.\n");
260 const int ret = sb_setup_buffer(dsp);
261 if (ret != EOK)
262 return ret;
263
264 const size_t play_size =
265 size < PLAY_BLOCK_SIZE ? size : PLAY_BLOCK_SIZE;
266 memcpy(dsp->buffer.data, dsp->playing.data, play_size);
267
268 ddf_log_debug("Playing sound: %zu(%zu) bytes.\n", play_size, size);
269
270 dsp->playing.data = data;
271 dsp->playing.position = data + play_size;
272 dsp->playing.size = size;
273 dsp->playing.mode =
274 (bit_depth == 16 ? 0x10 : 0) | (channels == 2 ? 0x20 : 0);
275
276 ddf_log_debug("Setting mode %hhx.\n", dsp->playing.mode);
277 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
278 sb_dsp_write(dsp, sampling_rate >> 8);
279 sb_dsp_write(dsp, sampling_rate & 0xff);
280
281 ddf_log_debug("Set sampling rate %hhx:%hhx.\n",
282 sampling_rate >> 8, sampling_rate & 0xff);
283
284 if (AUTO_DMA_MODE) {
285 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
286 } else {
287 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
288 }
289 sb_dsp_write(dsp, dsp->playing.mode);
290 sb_dsp_write(dsp, (play_size - 1) & 0xff);
291 sb_dsp_write(dsp, (play_size - 1) >> 8);
292
293 return EOK;
294}
295/**
296 * @}
297 */
Note: See TracBrowser for help on using the repository browser.