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

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

sb16: DMA buffer uses separate memory area.

  • Property mode set to 100644
File size: 9.0 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 <libarch/barrier.h>
37#include <str_error.h>
38
39#include "dma.h"
40#include "dma_controller.h"
41#include "ddf_log.h"
42#include "dsp_commands.h"
43#include "dsp.h"
44
45#define BUFFER_SIZE (PAGE_SIZE)
46#define PLAY_BLOCK_SIZE (BUFFER_SIZE / 2)
47
48#ifndef DSP_RETRY_COUNT
49#define DSP_RETRY_COUNT 100
50#endif
51
52#define DSP_RESET_RESPONSE 0xaa
53#define SB_DMA_CHAN_16 5
54#define SB_DMA_CHAN_8 1
55
56#define AUTO_DMA_MODE
57
58#ifdef AUTO_DMA_MODE
59#undef AUTO_DMA_MODE
60#define AUTO_DMA_MODE true
61#else
62#define AUTO_DMA_MODE false
63#endif
64
65static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
66{
67 assert(data);
68 assert(dsp);
69 uint8_t status;
70 size_t attempts = DSP_RETRY_COUNT;
71 do {
72 status = pio_read_8(&dsp->regs->dsp_read_status);
73 } while (--attempts && ((status & DSP_READ_READY) == 0));
74
75 if ((status & DSP_READ_READY) == 0)
76 return EIO;
77
78 *data = pio_read_8(&dsp->regs->dsp_data_read);
79 return EOK;
80}
81/*----------------------------------------------------------------------------*/
82static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
83{
84 assert(dsp);
85 uint8_t status;
86 size_t attempts = DSP_RETRY_COUNT;
87 do {
88 status = pio_read_8(&dsp->regs->dsp_write);
89 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
90
91 if ((status & DSP_WRITE_BUSY))
92 return EIO;
93
94 pio_write_8(&dsp->regs->dsp_write, data);
95 return EOK;
96}
97/*----------------------------------------------------------------------------*/
98static inline void sb_dsp_reset(sb_dsp_t *dsp)
99{
100 assert(dsp);
101 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
102 pio_write_8(&dsp->regs->dsp_reset, 1);
103 udelay(3); /* Keep reset for 3 us */
104 pio_write_8(&dsp->regs->dsp_reset, 0);
105}
106/*----------------------------------------------------------------------------*/
107static inline int sb_setup_buffer(sb_dsp_t *dsp)
108{
109 assert(dsp);
110 uint8_t *buffer = dma_create_buffer24(BUFFER_SIZE);
111 if (buffer == NULL) {
112 ddf_log_error("Failed to allocate buffer.\n");
113 return ENOMEM;
114 }
115
116 const uintptr_t pa = addr_to_phys(buffer);
117 assert(pa < (1 << 25));
118 /* Set 16 bit channel */
119 const int ret = dma_setup_channel(SB_DMA_CHAN_16, pa, BUFFER_SIZE,
120 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
121 if (ret == EOK) {
122 dsp->buffer.data = buffer;
123 dsp->buffer.position = buffer;
124 dsp->buffer.size = BUFFER_SIZE;
125 bzero(buffer, BUFFER_SIZE);
126 } else {
127 ddf_log_error("Failed to setup DMA16 channel %s.\n",
128 str_error(ret));
129 dma_destroy_buffer(buffer);
130 }
131 return ret;
132}
133/*----------------------------------------------------------------------------*/
134static inline void sb_clear_buffer(sb_dsp_t *dsp)
135{
136 dma_destroy_buffer(dsp->buffer.data);
137 dsp->buffer.data = NULL;
138 dsp->buffer.position = NULL;
139 dsp->buffer.size = 0;
140}
141/*----------------------------------------------------------------------------*/
142static inline size_t sample_count(uint8_t mode, size_t byte_count)
143{
144 if (mode & DSP_MODE_16BIT) {
145 return byte_count / 2;
146 }
147 return byte_count;
148}
149/*----------------------------------------------------------------------------*/
150int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs)
151{
152 assert(dsp);
153 dsp->regs = regs;
154 sb_dsp_reset(dsp);
155 /* "DSP takes about 100 microseconds to initialize itself" */
156 udelay(100);
157 uint8_t response;
158 const int ret = sb_dsp_read(dsp, &response);
159 if (ret != EOK) {
160 ddf_log_error("Failed to read DSP reset response value.\n");
161 return ret;
162 }
163 if (response != DSP_RESET_RESPONSE) {
164 ddf_log_error("Invalid DSP reset response: %x.\n", response);
165 return EIO;
166 }
167
168 /* Get DSP version number */
169 sb_dsp_write(dsp, DSP_VERSION);
170 sb_dsp_read(dsp, &dsp->version.major);
171 sb_dsp_read(dsp, &dsp->version.minor);
172
173 return ret;
174}
175/*----------------------------------------------------------------------------*/
176void sb_dsp_interrupt(sb_dsp_t *dsp)
177{
178 assert(dsp);
179 /* We don't really care about the mode of transport, so ack both */
180 if (dsp->version.major >= 4) {
181 /* ACK dma16 transfer interrupt */
182 pio_read_8(&dsp->regs->dma16_ack);
183 }
184 /* ACK dma8 transfer interrupt */
185 pio_read_8(&dsp->regs->dsp_read_status);
186
187 const size_t remain_size = dsp->playing.size -
188 (dsp->playing.position - dsp->playing.data);
189
190 if (remain_size == 0) {
191 ddf_log_note("Nothing more to play");
192 if (AUTO_DMA_MODE) {
193 sb_dsp_write(dsp, DMA_16B_EXIT);
194 }
195 sb_clear_buffer(dsp);
196 return;
197 }
198 if (remain_size <= PLAY_BLOCK_SIZE) {
199 ddf_log_note("Last %zu bytes to play.\n", remain_size);
200 /* This is the last block */
201 memcpy(dsp->buffer.position, dsp->playing.position, remain_size);
202 write_barrier();
203 dsp->playing.position += remain_size;
204 dsp->buffer.position += remain_size;
205 const size_t samples =
206 sample_count(dsp->playing.mode, remain_size);
207 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
208 sb_dsp_write(dsp, dsp->playing.mode);
209 sb_dsp_write(dsp, (samples - 1) & 0xff);
210 sb_dsp_write(dsp, (samples - 1) >> 8);
211 return;
212 }
213 memcpy(dsp->buffer.position, dsp->playing.position, PLAY_BLOCK_SIZE);
214 write_barrier();
215 dsp->playing.position += PLAY_BLOCK_SIZE;
216 dsp->buffer.position += PLAY_BLOCK_SIZE;
217 /* Wrap around */
218 if (dsp->buffer.position == (dsp->buffer.data + dsp->buffer.size))
219 dsp->buffer.position = dsp->buffer.data;
220 if (!AUTO_DMA_MODE) {
221 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
222 sb_dsp_write(dsp, dsp->playing.mode);
223 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) & 0xff);
224 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) >> 8);
225 }
226
227}
228/*----------------------------------------------------------------------------*/
229int sb_dsp_play_direct(sb_dsp_t *dsp, const uint8_t *data, size_t size,
230 unsigned sampling_rate, unsigned channels, unsigned bit_depth)
231{
232 assert(dsp);
233 if (channels != 1 || bit_depth != 8)
234 return EIO;
235 /* In microseconds */
236 const unsigned wait_period = 1000000 / sampling_rate;
237 while (size--) {
238 pio_write_8(&dsp->regs->dsp_write, DIRECT_8B_OUTPUT);
239 pio_write_8(&dsp->regs->dsp_write, *data++);
240 udelay(wait_period);
241 }
242 return EOK;
243}
244/*----------------------------------------------------------------------------*/
245int sb_dsp_play(sb_dsp_t *dsp, const uint8_t *data, size_t size,
246 uint16_t sampling_rate, unsigned channels, unsigned sample_size)
247{
248 assert(dsp);
249 if (!data)
250 return EOK;
251
252 /* Check supported parameters */
253 if (sample_size != 8 && sample_size != 16)
254 return ENOTSUP;
255 if (channels != 1 && channels != 2)
256 return ENOTSUP;
257
258 ddf_log_debug("Buffer prepare.\n");
259 const int ret = sb_setup_buffer(dsp);
260 if (ret != EOK)
261 return ret;
262
263 const size_t play_size =
264 size < PLAY_BLOCK_SIZE ? size : PLAY_BLOCK_SIZE;
265 const size_t copy_size =
266 size < BUFFER_SIZE ? size : BUFFER_SIZE;
267
268 dsp->playing.data = data;
269 dsp->playing.position = data + copy_size;
270 dsp->playing.size = size;
271 dsp->playing.mode = 0;
272 if (sample_size == 16)
273 dsp->playing.mode |= DSP_MODE_16BIT;
274 if (channels == 2)
275 dsp->playing.mode |= DSP_MODE_STEREO;
276
277 const size_t samples = sample_count(dsp->playing.mode, play_size);
278
279 ddf_log_debug("Playing %s sound: %zu(%zu) bytes => %zu samples.\n",
280 mode_to_str(dsp->playing.mode), play_size, size, samples);
281
282 memcpy(dsp->buffer.data, dsp->playing.data, copy_size);
283 write_barrier();
284
285 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
286 sb_dsp_write(dsp, sampling_rate >> 8);
287 sb_dsp_write(dsp, sampling_rate & 0xff);
288
289 ddf_log_debug("Sampling rate: %hhx:%hhx.\n",
290 sampling_rate >> 8, sampling_rate & 0xff);
291
292 if (!AUTO_DMA_MODE || play_size == size) {
293 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
294 } else {
295 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
296 }
297 sb_dsp_write(dsp, dsp->playing.mode);
298 sb_dsp_write(dsp, (samples - 1) & 0xff);
299 sb_dsp_write(dsp, (samples - 1) >> 8);
300
301 return EOK;
302}
303/**
304 * @}
305 */
Note: See TracBrowser for help on using the repository browser.