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

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

drv/auid/sb16: Refactor debug output.

  • Property mode set to 100644
File size: 8.7 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_ID 1
48#define MAX_BUFFER_SIZE (PAGE_SIZE)
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
58static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
59{
60 assert(data);
61 assert(dsp);
62 uint8_t status;
63 size_t attempts = DSP_RETRY_COUNT;
64 do {
65 status = pio_read_8(&dsp->regs->dsp_read_status);
66 } while (--attempts && ((status & DSP_READ_READY) == 0));
67
68 if ((status & DSP_READ_READY) == 0)
69 return EIO;
70
71 *data = pio_read_8(&dsp->regs->dsp_data_read);
72 return EOK;
73}
74
75static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
76{
77 assert(dsp);
78 uint8_t status;
79 size_t attempts = DSP_RETRY_COUNT;
80 do {
81 status = pio_read_8(&dsp->regs->dsp_write);
82 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
83
84 if ((status & DSP_WRITE_BUSY))
85 return EIO;
86
87 pio_write_8(&dsp->regs->dsp_write, data);
88 return EOK;
89}
90
91static inline void sb_dsp_reset(sb_dsp_t *dsp)
92{
93 assert(dsp);
94 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
95 pio_write_8(&dsp->regs->dsp_reset, 1);
96 udelay(3); /* Keep reset for 3 us */
97 pio_write_8(&dsp->regs->dsp_reset, 0);
98}
99
100static inline int sb_setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
101{
102 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
103 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
104 if (!sess)
105 return ENOMEM;
106
107 const int ret = hw_res_dma_channel_setup(sess,
108 dsp->dma16_channel, pa, size,
109 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
110 async_hangup(sess);
111 return ret;
112}
113
114static inline int sb_setup_buffer(sb_dsp_t *dsp, size_t size)
115{
116 assert(dsp);
117 if (size > MAX_BUFFER_SIZE || size == 0 || (size % 2) == 1)
118 size = MAX_BUFFER_SIZE;
119 uint8_t *buffer = dma_create_buffer24(size);
120 if (buffer == NULL) {
121 ddf_log_error("Failed to allocate DMA buffer.");
122 return ENOMEM;
123 }
124
125 const uintptr_t pa = addr_to_phys(buffer);
126 assert(pa < (1 << 25));
127
128 /* Set 16 bit channel */
129 const int ret = sb_setup_dma(dsp, pa, size);
130 if (ret == EOK) {
131 dsp->buffer.data = buffer;
132 dsp->buffer.size = size;
133 bzero(dsp->buffer.data, dsp->buffer.size);
134 } else {
135 ddf_log_error("Failed to setup DMA16 channel: %s.",
136 str_error(ret));
137 dma_destroy_buffer(buffer);
138 }
139 return ret;
140}
141
142static inline void sb_clear_buffer(sb_dsp_t *dsp)
143{
144 dma_destroy_buffer(dsp->buffer.data);
145 dsp->buffer.data = NULL;
146 dsp->buffer.size = 0;
147}
148
149static inline size_t sample_count(unsigned sample_size, size_t byte_count)
150{
151 if (sample_size == 16) {
152 return byte_count / 2;
153 }
154 return byte_count;
155}
156
157int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
158 int dma8, int dma16)
159{
160 assert(dsp);
161 dsp->regs = regs;
162 dsp->dma8_channel = dma8;
163 dsp->dma16_channel = dma16;
164 dsp->event_session = NULL;
165 dsp->event_exchange = NULL;
166 dsp->sb_dev = dev;
167 sb_dsp_reset(dsp);
168 /* "DSP takes about 100 microseconds to initialize itself" */
169 udelay(100);
170 uint8_t response;
171 const int ret = sb_dsp_read(dsp, &response);
172 if (ret != EOK) {
173 ddf_log_error("Failed to read DSP reset response value.");
174 return ret;
175 }
176 if (response != DSP_RESET_RESPONSE) {
177 ddf_log_error("Invalid DSP reset response: %x.", response);
178 return EIO;
179 }
180
181 /* Get DSP version number */
182 sb_dsp_write(dsp, DSP_VERSION);
183 sb_dsp_read(dsp, &dsp->version.major);
184 sb_dsp_read(dsp, &dsp->version.minor);
185
186 return ret;
187}
188
189void sb_dsp_interrupt(sb_dsp_t *dsp)
190{
191 assert(dsp);
192 if (dsp->event_exchange) {
193 async_msg_0(dsp->event_exchange, IPC_FIRST_USER_METHOD);
194 } else {
195 ddf_log_warning("Interrupt with no event consumer.");
196 }
197#ifndef AUTO_DMA_MODE
198 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
199 sb_dsp_write(dsp, dsp->playing.mode);
200 sb_dsp_write(dsp, (dsp->playing.samples - 1) & 0xff);
201 sb_dsp_write(dsp, (dsp->playing.samples - 1) >> 8);
202#endif
203}
204
205int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size, unsigned *id)
206{
207 assert(dsp);
208 assert(size);
209
210 /* buffer is already setup by for someone, refuse to work until
211 * it's released */
212 if (dsp->buffer.data)
213 return EBUSY;
214
215 const int ret = sb_setup_buffer(dsp, *size);
216 if (ret == EOK) {
217 ddf_log_debug("Providing buffer(%u): %p, %zu B.",
218 BUFFER_ID, dsp->buffer.data, dsp->buffer.size);
219
220 if (buffer)
221 *buffer = dsp->buffer.data;
222 if (size)
223 *size = dsp->buffer.size;
224 if (id)
225 *id = BUFFER_ID;
226 }
227 return ret;
228}
229
230int sb_dsp_set_event_session(sb_dsp_t *dsp, unsigned id, async_sess_t *session)
231{
232 assert(dsp);
233 assert(session);
234 if (id != BUFFER_ID)
235 return ENOENT;
236 if (dsp->event_session)
237 return EBUSY;
238 dsp->event_session = session;
239 ddf_log_debug("Set event session.");
240 return EOK;
241}
242
243int sb_dsp_release_buffer(sb_dsp_t *dsp, unsigned id)
244{
245 assert(dsp);
246 if (id != BUFFER_ID)
247 return ENOENT;
248 sb_clear_buffer(dsp);
249 async_exchange_end(dsp->event_exchange);
250 dsp->event_exchange = NULL;
251 if (dsp->event_session)
252 async_hangup(dsp->event_session);
253 dsp->event_session = NULL;
254 ddf_log_debug("DSP buffer released.");
255 return EOK;
256}
257
258int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned id, unsigned parts,
259 unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
260{
261 assert(dsp);
262
263 if (!dsp->event_session)
264 return EINVAL;
265
266 /* Play block size must be even number (we use DMA 16)*/
267 if (dsp->buffer.size % (parts * 2))
268 return EINVAL;
269
270 const unsigned play_block_size = dsp->buffer.size / parts;
271
272 /* Check supported parameters */
273 ddf_log_debug("Requested playback on buffer \"%u\" (%u parts): %uHz, "
274 "%ssinged %u bit, %u channel(s).", id, parts, sampling_rate,
275 sign ? "" : "un", sample_size, channels);
276 if (id != BUFFER_ID)
277 return ENOENT;
278 if (sample_size != 16) // FIXME We only support 16 bit playback
279 return ENOTSUP;
280 if (channels != 1 && channels != 2)
281 return ENOTSUP;
282 if (sampling_rate > 44100)
283 return ENOTSUP;
284
285 dsp->event_exchange = async_exchange_begin(dsp->event_session);
286 if (!dsp->event_exchange)
287 return ENOMEM;
288
289 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
290 sb_dsp_write(dsp, sampling_rate >> 8);
291 sb_dsp_write(dsp, sampling_rate & 0xff);
292
293 ddf_log_verbose("Sampling rate: %hhx:%hhx.",
294 sampling_rate >> 8, sampling_rate & 0xff);
295
296#ifdef AUTO_DMA_MODE
297 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
298#else
299 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
300#endif
301
302 dsp->playing.mode = 0 |
303 (sign ? DSP_MODE_SIGNED : 0) | (channels == 2 ? DSP_MODE_STEREO : 0);
304 sb_dsp_write(dsp, dsp->playing.mode);
305
306 dsp->playing.samples = sample_count(sample_size, play_block_size);
307 sb_dsp_write(dsp, (dsp->playing.samples - 1) & 0xff);
308 sb_dsp_write(dsp, (dsp->playing.samples - 1) >> 8);
309
310 ddf_log_verbose("Playback started, interrupt every %u samples "
311 "(~1/%u sec)", dsp->playing.samples,
312 sampling_rate / dsp->playing.samples);
313
314 return EOK;
315}
316
317int sb_dsp_stop_playback(sb_dsp_t *dsp, unsigned id)
318{
319 assert(dsp);
320 if (id != BUFFER_ID)
321 return ENOENT;
322 async_exchange_end(dsp->event_exchange);
323 sb_dsp_write(dsp, DMA_16B_EXIT);
324 return EOK;
325}
326
327int sb_dsp_start_record(sb_dsp_t *dsp, unsigned id, unsigned sample_rate,
328 unsigned sample_size, unsigned channels, bool sign)
329{
330 return ENOTSUP;
331}
332
333int sb_dsp_stop_record(sb_dsp_t *dsp, unsigned id)
334{
335 return ENOTSUP;
336}
337/**
338 * @}
339 */
Note: See TracBrowser for help on using the repository browser.