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

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

sb16: Add TODO

  • 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 // TODO: Move irq ACK to irq_code
198 if (dsp->version.major >= 4) {
199 /* ACK dma16 transfer interrupt */
200 pio_read_8(&dsp->regs->dma16_ack);
201 }
202 /* ACK dma8 transfer interrupt */
203 pio_read_8(&dsp->regs->dsp_read_status);
204
205 const size_t remain_size = dsp->playing.size -
206 (dsp->playing.position - dsp->playing.data);
207
208 if (remain_size == 0) {
209 ddf_log_note("Nothing more to play");
210 if (AUTO_DMA_MODE) {
211 sb_dsp_write(dsp, DMA_16B_EXIT);
212 }
213 sb_clear_buffer(dsp);
214 return;
215 }
216 if (remain_size <= PLAY_BLOCK_SIZE) {
217 ddf_log_note("Last %zu bytes to play.\n", remain_size);
218 /* This is the last block */
219 memcpy(dsp->buffer.position, dsp->playing.position, remain_size);
220 write_barrier();
221 dsp->playing.position += remain_size;
222 dsp->buffer.position += remain_size;
223 const size_t samples =
224 sample_count(dsp->playing.mode, remain_size);
225 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
226 sb_dsp_write(dsp, dsp->playing.mode);
227 sb_dsp_write(dsp, (samples - 1) & 0xff);
228 sb_dsp_write(dsp, (samples - 1) >> 8);
229 return;
230 }
231 memcpy(dsp->buffer.position, dsp->playing.position, PLAY_BLOCK_SIZE);
232 write_barrier();
233 dsp->playing.position += PLAY_BLOCK_SIZE;
234 dsp->buffer.position += PLAY_BLOCK_SIZE;
235 /* Wrap around */
236 if (dsp->buffer.position == (dsp->buffer.data + dsp->buffer.size))
237 dsp->buffer.position = dsp->buffer.data;
238 if (!AUTO_DMA_MODE) {
239 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
240 sb_dsp_write(dsp, dsp->playing.mode);
241 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) & 0xff);
242 sb_dsp_write(dsp, (PLAY_BLOCK_SIZE - 1) >> 8);
243 }
244
245}
246/*----------------------------------------------------------------------------*/
247int sb_dsp_play_direct(sb_dsp_t *dsp, const uint8_t *data, size_t size,
248 unsigned sampling_rate, unsigned channels, unsigned bit_depth)
249{
250 assert(dsp);
251 if (channels != 1 || bit_depth != 8)
252 return EIO;
253 /* In microseconds */
254 const unsigned wait_period = 1000000 / sampling_rate;
255 while (size--) {
256 pio_write_8(&dsp->regs->dsp_write, DIRECT_8B_OUTPUT);
257 pio_write_8(&dsp->regs->dsp_write, *data++);
258 udelay(wait_period);
259 }
260 return EOK;
261}
262/*----------------------------------------------------------------------------*/
263int sb_dsp_play(sb_dsp_t *dsp, const uint8_t *data, size_t size,
264 uint16_t sampling_rate, unsigned channels, unsigned sample_size)
265{
266 assert(dsp);
267 if (!data)
268 return EOK;
269
270 /* Check supported parameters */
271 if (sample_size != 8 && sample_size != 16)
272 return ENOTSUP;
273 if (channels != 1 && channels != 2)
274 return ENOTSUP;
275
276 ddf_log_debug("Buffer prepare.\n");
277 const int ret = sb_setup_buffer(dsp);
278 if (ret != EOK)
279 return ret;
280
281 const size_t play_size =
282 size < PLAY_BLOCK_SIZE ? size : PLAY_BLOCK_SIZE;
283 const size_t copy_size =
284 size < BUFFER_SIZE ? size : BUFFER_SIZE;
285
286 dsp->playing.data = data;
287 dsp->playing.position = data + copy_size;
288 dsp->playing.size = size;
289 dsp->playing.mode = 0;
290 if (sample_size == 16)
291 dsp->playing.mode |= DSP_MODE_16BIT;
292 if (channels == 2)
293 dsp->playing.mode |= DSP_MODE_STEREO;
294
295 const size_t samples = sample_count(dsp->playing.mode, play_size);
296
297 ddf_log_debug("Playing %s sound: %zu(%zu) bytes => %zu samples.\n",
298 mode_to_str(dsp->playing.mode), play_size, size, samples);
299
300 memcpy(dsp->buffer.data, dsp->playing.data, copy_size);
301 write_barrier();
302
303 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
304 sb_dsp_write(dsp, sampling_rate >> 8);
305 sb_dsp_write(dsp, sampling_rate & 0xff);
306
307 ddf_log_debug("Sampling rate: %hhx:%hhx.\n",
308 sampling_rate >> 8, sampling_rate & 0xff);
309
310 if (!AUTO_DMA_MODE || play_size == size) {
311 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
312 } else {
313 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
314 }
315 sb_dsp_write(dsp, dsp->playing.mode);
316 sb_dsp_write(dsp, (samples - 1) & 0xff);
317 sb_dsp_write(dsp, (samples - 1) >> 8);
318
319 return EOK;
320}
321/**
322 * @}
323 */
Note: See TracBrowser for help on using the repository browser.