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

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

sb16: Switch to new ISA bus provided DMA controller access.

  • Property mode set to 100644
File size: 9.5 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 <devman.h>
36#include <device/hw_res.h>
37#include <libarch/ddi.h>
38#include <libarch/barrier.h>
39#include <str_error.h>
40#include <bool.h>
41
42#include "dma.h"
43#include "ddf_log.h"
44#include "dsp_commands.h"
45#include "dsp.h"
46
47#define BUFFER_SIZE (PAGE_SIZE)
48#define PLAY_BLOCK_SIZE (BUFFER_SIZE / 2)
49
50#ifndef DSP_RETRY_COUNT
51#define DSP_RETRY_COUNT 100
52#endif
53
54#define DSP_RESET_RESPONSE 0xaa
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_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
108{
109 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
110 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
111 if (!sess)
112 return ENOMEM;
113
114 const int ret = hw_res_dma_channel_setup(sess,
115 dsp->dma16_channel, pa, size,
116 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
117 async_hangup(sess);
118 return ret;
119}
120/*----------------------------------------------------------------------------*/
121static inline int sb_setup_buffer(sb_dsp_t *dsp)
122{
123 assert(dsp);
124 uint8_t *buffer = dma_create_buffer24(BUFFER_SIZE);
125 if (buffer == NULL) {
126 ddf_log_error("Failed to allocate buffer.\n");
127 return ENOMEM;
128 }
129
130 const uintptr_t pa = addr_to_phys(buffer);
131 assert(pa < (1 << 25));
132 /* Set 16 bit channel */
133 const int ret = sb_setup_dma(dsp, pa, BUFFER_SIZE);
134 if (ret == EOK) {
135 dsp->buffer.data = buffer;
136 dsp->buffer.position = buffer;
137 dsp->buffer.size = BUFFER_SIZE;
138 bzero(buffer, BUFFER_SIZE);
139 } else {
140 ddf_log_error("Failed to setup DMA16 channel %s.\n",
141 str_error(ret));
142 dma_destroy_buffer(buffer);
143 }
144 return ret;
145}
146/*----------------------------------------------------------------------------*/
147static inline void sb_clear_buffer(sb_dsp_t *dsp)
148{
149 dma_destroy_buffer(dsp->buffer.data);
150 dsp->buffer.data = NULL;
151 dsp->buffer.position = NULL;
152 dsp->buffer.size = 0;
153}
154/*----------------------------------------------------------------------------*/
155static inline size_t sample_count(uint8_t mode, size_t byte_count)
156{
157 if (mode & DSP_MODE_16BIT) {
158 return byte_count / 2;
159 }
160 return byte_count;
161}
162/*----------------------------------------------------------------------------*/
163int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
164 int dma8, int dma16)
165{
166 assert(dsp);
167 dsp->regs = regs;
168 dsp->dma8_channel = dma8;
169 dsp->dma16_channel = dma16;
170 dsp->sb_dev = dev;
171 sb_dsp_reset(dsp);
172 /* "DSP takes about 100 microseconds to initialize itself" */
173 udelay(100);
174 uint8_t response;
175 const int ret = sb_dsp_read(dsp, &response);
176 if (ret != EOK) {
177 ddf_log_error("Failed to read DSP reset response value.\n");
178 return ret;
179 }
180 if (response != DSP_RESET_RESPONSE) {
181 ddf_log_error("Invalid DSP reset response: %x.\n", response);
182 return EIO;
183 }
184
185 /* Get DSP version number */
186 sb_dsp_write(dsp, DSP_VERSION);
187 sb_dsp_read(dsp, &dsp->version.major);
188 sb_dsp_read(dsp, &dsp->version.minor);
189
190 return ret;
191}
192/*----------------------------------------------------------------------------*/
193void sb_dsp_interrupt(sb_dsp_t *dsp)
194{
195 assert(dsp);
196 /* We don't really care about the mode of transport, so ack both */
197 if (dsp->version.major >= 4) {
198 /* ACK dma16 transfer interrupt */
199 pio_read_8(&dsp->regs->dma16_ack);
200 }
201 /* ACK dma8 transfer interrupt */
202 pio_read_8(&dsp->regs->dsp_read_status);
203
204 const size_t remain_size = dsp->playing.size -
205 (dsp->playing.position - dsp->playing.data);
206
207 if (remain_size == 0) {
208 ddf_log_note("Nothing more to play");
209 if (AUTO_DMA_MODE) {
210 sb_dsp_write(dsp, DMA_16B_EXIT);
211 }
212 sb_clear_buffer(dsp);
213 return;
214 }
215 if (remain_size <= PLAY_BLOCK_SIZE) {
216 ddf_log_note("Last %zu bytes to play.\n", remain_size);
217 /* This is the last block */
218 memcpy(dsp->buffer.position, dsp->playing.position, remain_size);
219 write_barrier();
220 dsp->playing.position += remain_size;
221 dsp->buffer.position += remain_size;
222 const size_t samples =
223 sample_count(dsp->playing.mode, remain_size);
224 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
225 sb_dsp_write(dsp, dsp->playing.mode);
226 sb_dsp_write(dsp, (samples - 1) & 0xff);
227 sb_dsp_write(dsp, (samples - 1) >> 8);
228 return;
229 }
230 memcpy(dsp->buffer.position, dsp->playing.position, PLAY_BLOCK_SIZE);
231 write_barrier();
232 dsp->playing.position += PLAY_BLOCK_SIZE;
233 dsp->buffer.position += PLAY_BLOCK_SIZE;
234 /* Wrap around */
235 if (dsp->buffer.position == (dsp->buffer.data + dsp->buffer.size))
236 dsp->buffer.position = dsp->buffer.data;
237 if (!AUTO_DMA_MODE) {
238 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
239 sb_dsp_write(dsp, dsp->playing.mode);
240 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) & 0xff);
241 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) >> 8);
242 }
243
244}
245/*----------------------------------------------------------------------------*/
246int sb_dsp_play_direct(sb_dsp_t *dsp, const uint8_t *data, size_t size,
247 unsigned sampling_rate, unsigned channels, unsigned bit_depth)
248{
249 assert(dsp);
250 if (channels != 1 || bit_depth != 8)
251 return EIO;
252 /* In microseconds */
253 const unsigned wait_period = 1000000 / sampling_rate;
254 while (size--) {
255 pio_write_8(&dsp->regs->dsp_write, DIRECT_8B_OUTPUT);
256 pio_write_8(&dsp->regs->dsp_write, *data++);
257 udelay(wait_period);
258 }
259 return EOK;
260}
261/*----------------------------------------------------------------------------*/
262int sb_dsp_play(sb_dsp_t *dsp, const uint8_t *data, size_t size,
263 uint16_t sampling_rate, unsigned channels, unsigned sample_size)
264{
265 assert(dsp);
266 if (!data)
267 return EOK;
268
269 /* Check supported parameters */
270 if (sample_size != 8 && sample_size != 16)
271 return ENOTSUP;
272 if (channels != 1 && channels != 2)
273 return ENOTSUP;
274
275 ddf_log_debug("Buffer prepare.\n");
276 const int ret = sb_setup_buffer(dsp);
277 if (ret != EOK)
278 return ret;
279
280 const size_t play_size =
281 size < PLAY_BLOCK_SIZE ? size : PLAY_BLOCK_SIZE;
282 const size_t copy_size =
283 size < BUFFER_SIZE ? size : BUFFER_SIZE;
284
285 dsp->playing.data = data;
286 dsp->playing.position = data + copy_size;
287 dsp->playing.size = size;
288 dsp->playing.mode = 0;
289 if (sample_size == 16)
290 dsp->playing.mode |= DSP_MODE_16BIT;
291 if (channels == 2)
292 dsp->playing.mode |= DSP_MODE_STEREO;
293
294 const size_t samples = sample_count(dsp->playing.mode, play_size);
295
296 ddf_log_debug("Playing %s sound: %zu(%zu) bytes => %zu samples.\n",
297 mode_to_str(dsp->playing.mode), play_size, size, samples);
298
299 memcpy(dsp->buffer.data, dsp->playing.data, copy_size);
300 write_barrier();
301
302 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
303 sb_dsp_write(dsp, sampling_rate >> 8);
304 sb_dsp_write(dsp, sampling_rate & 0xff);
305
306 ddf_log_debug("Sampling rate: %hhx:%hhx.\n",
307 sampling_rate >> 8, sampling_rate & 0xff);
308
309 if (!AUTO_DMA_MODE || play_size == size) {
310 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
311 } else {
312 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
313 }
314 sb_dsp_write(dsp, dsp->playing.mode);
315 sb_dsp_write(dsp, (samples - 1) & 0xff);
316 sb_dsp_write(dsp, (samples - 1) >> 8);
317
318 return EOK;
319}
320/**
321 * @}
322 */
Note: See TracBrowser for help on using the repository browser.