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 BUFFER_SIZE (PAGE_SIZE)
|
---|
49 | #define PLAY_BLOCK_SIZE (BUFFER_SIZE / 2)
|
---|
50 |
|
---|
51 | #ifndef DSP_RETRY_COUNT
|
---|
52 | #define DSP_RETRY_COUNT 100
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #define DSP_RESET_RESPONSE 0xaa
|
---|
56 |
|
---|
57 | #define AUTO_DMA_MODE
|
---|
58 |
|
---|
59 | static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
|
---|
60 | {
|
---|
61 | assert(data);
|
---|
62 | assert(dsp);
|
---|
63 | uint8_t status;
|
---|
64 | size_t attempts = DSP_RETRY_COUNT;
|
---|
65 | do {
|
---|
66 | status = pio_read_8(&dsp->regs->dsp_read_status);
|
---|
67 | } while (--attempts && ((status & DSP_READ_READY) == 0));
|
---|
68 |
|
---|
69 | if ((status & DSP_READ_READY) == 0)
|
---|
70 | return EIO;
|
---|
71 |
|
---|
72 | *data = pio_read_8(&dsp->regs->dsp_data_read);
|
---|
73 | return EOK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
|
---|
77 | {
|
---|
78 | assert(dsp);
|
---|
79 | uint8_t status;
|
---|
80 | size_t attempts = DSP_RETRY_COUNT;
|
---|
81 | do {
|
---|
82 | status = pio_read_8(&dsp->regs->dsp_write);
|
---|
83 | } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
|
---|
84 |
|
---|
85 | if ((status & DSP_WRITE_BUSY))
|
---|
86 | return EIO;
|
---|
87 |
|
---|
88 | pio_write_8(&dsp->regs->dsp_write, data);
|
---|
89 | return EOK;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static inline void sb_dsp_reset(sb_dsp_t *dsp)
|
---|
93 | {
|
---|
94 | assert(dsp);
|
---|
95 | /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
|
---|
96 | pio_write_8(&dsp->regs->dsp_reset, 1);
|
---|
97 | udelay(3); /* Keep reset for 3 us */
|
---|
98 | pio_write_8(&dsp->regs->dsp_reset, 0);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static inline int sb_setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
|
---|
102 | {
|
---|
103 | async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
|
---|
104 | dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
|
---|
105 | if (!sess)
|
---|
106 | return ENOMEM;
|
---|
107 |
|
---|
108 | const int ret = hw_res_dma_channel_setup(sess,
|
---|
109 | dsp->dma16_channel, pa, size,
|
---|
110 | DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
|
---|
111 | async_hangup(sess);
|
---|
112 | return ret;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static inline int sb_setup_buffer(sb_dsp_t *dsp, size_t size)
|
---|
116 | {
|
---|
117 | assert(dsp);
|
---|
118 | if (size > BUFFER_SIZE || size == 0 || (size % 2) == 1)
|
---|
119 | size = BUFFER_SIZE;
|
---|
120 | uint8_t *buffer = dma_create_buffer24(size);
|
---|
121 | if (buffer == NULL) {
|
---|
122 | ddf_log_error("Failed to allocate DMA buffer.");
|
---|
123 | return ENOMEM;
|
---|
124 | }
|
---|
125 |
|
---|
126 | const uintptr_t pa = addr_to_phys(buffer);
|
---|
127 | assert(pa < (1 << 25));
|
---|
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 |
|
---|
142 | static 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 |
|
---|
149 | static 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 |
|
---|
157 | int 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 |
|
---|
189 | void sb_dsp_interrupt(sb_dsp_t *dsp)
|
---|
190 | {
|
---|
191 | assert(dsp);
|
---|
192 | if (dsp->event_exchange) {
|
---|
193 | ddf_log_verbose("Sending interrupt event.");
|
---|
194 | async_msg_0(dsp->event_exchange, IPC_FIRST_USER_METHOD);
|
---|
195 | } else {
|
---|
196 | ddf_log_warning("Interrupt with no event consumer.");
|
---|
197 | }
|
---|
198 | #ifndef AUTO_DMA_MODE
|
---|
199 | sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
|
---|
200 | sb_dsp_write(dsp, dsp->playing.mode);
|
---|
201 | sb_dsp_write(dsp, (dsp->playing.samples - 1) & 0xff);
|
---|
202 | sb_dsp_write(dsp, (dsp->playing.samples - 1) >> 8);
|
---|
203 | #endif
|
---|
204 | }
|
---|
205 |
|
---|
206 | int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size, unsigned *id)
|
---|
207 | {
|
---|
208 | assert(dsp);
|
---|
209 | assert(size);
|
---|
210 |
|
---|
211 | /* buffer is already setup by for someone, refuse to work until
|
---|
212 | * it's released */
|
---|
213 | if (dsp->buffer.data)
|
---|
214 | return EBUSY;
|
---|
215 |
|
---|
216 | const int ret = sb_setup_buffer(dsp, *size);
|
---|
217 | if (ret == EOK) {
|
---|
218 | ddf_log_debug("Providing buffer(%u): %p, %zu B.",
|
---|
219 | BUFFER_ID, dsp->buffer.data, dsp->buffer.size);
|
---|
220 |
|
---|
221 | if (buffer)
|
---|
222 | *buffer = dsp->buffer.data;
|
---|
223 | if (size)
|
---|
224 | *size = dsp->buffer.size;
|
---|
225 | if (id)
|
---|
226 | *id = BUFFER_ID;
|
---|
227 | }
|
---|
228 | return ret;
|
---|
229 | }
|
---|
230 |
|
---|
231 | int sb_dsp_set_event_session(sb_dsp_t *dsp, unsigned id, async_sess_t *session)
|
---|
232 | {
|
---|
233 | assert(dsp);
|
---|
234 | assert(session);
|
---|
235 | if (id != BUFFER_ID)
|
---|
236 | return ENOENT;
|
---|
237 | if (dsp->event_session)
|
---|
238 | return EBUSY;
|
---|
239 | dsp->event_session = session;
|
---|
240 | ddf_log_debug("Set event session.");
|
---|
241 | return EOK;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int sb_dsp_release_buffer(sb_dsp_t *dsp, unsigned id)
|
---|
245 | {
|
---|
246 | assert(dsp);
|
---|
247 | if (id != BUFFER_ID)
|
---|
248 | return ENOENT;
|
---|
249 | sb_clear_buffer(dsp);
|
---|
250 | if (dsp->event_exchange)
|
---|
251 | async_exchange_end(dsp->event_exchange);
|
---|
252 | dsp->event_exchange = NULL;
|
---|
253 | if (dsp->event_session)
|
---|
254 | async_hangup(dsp->event_session);
|
---|
255 | dsp->event_session = NULL;
|
---|
256 | ddf_log_debug("DSP buffer released.");
|
---|
257 | return EOK;
|
---|
258 | }
|
---|
259 |
|
---|
260 | int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned id, unsigned parts,
|
---|
261 | unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
|
---|
262 | {
|
---|
263 | assert(dsp);
|
---|
264 |
|
---|
265 | if (!dsp->event_session)
|
---|
266 | return EINVAL;
|
---|
267 |
|
---|
268 | /* Play block size must be even number (we use DMA 16)*/
|
---|
269 | if (dsp->buffer.size % (parts * 2))
|
---|
270 | return EINVAL;
|
---|
271 |
|
---|
272 | const unsigned play_block_size = dsp->buffer.size / parts;
|
---|
273 |
|
---|
274 | /* Check supported parameters */
|
---|
275 | ddf_log_debug("Starting playback on buffer(%u): rate: %u, size: %u, "
|
---|
276 | " channels: %u, signed: %s.", id, sampling_rate, sample_size,
|
---|
277 | channels, sign ? "YES" : "NO" );
|
---|
278 | if (id != BUFFER_ID)
|
---|
279 | return ENOENT;
|
---|
280 | if (sample_size != 16) // FIXME We only support 16 bit playback
|
---|
281 | return ENOTSUP;
|
---|
282 | if (channels != 1 && channels != 2)
|
---|
283 | return ENOTSUP;
|
---|
284 | if (sampling_rate > 44100)
|
---|
285 | return ENOTSUP;
|
---|
286 |
|
---|
287 | dsp->event_exchange = async_exchange_begin(dsp->event_session);
|
---|
288 | if (!dsp->event_exchange)
|
---|
289 | return ENOMEM;
|
---|
290 |
|
---|
291 | sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
|
---|
292 | sb_dsp_write(dsp, sampling_rate >> 8);
|
---|
293 | sb_dsp_write(dsp, sampling_rate & 0xff);
|
---|
294 |
|
---|
295 | ddf_log_debug("Sampling rate: %hhx:%hhx.",
|
---|
296 | sampling_rate >> 8, sampling_rate & 0xff);
|
---|
297 |
|
---|
298 | #ifdef AUTO_DMA_MODE
|
---|
299 | sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
|
---|
300 | #else
|
---|
301 | sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | dsp->playing.mode = 0 |
|
---|
305 | (sign ? DSP_MODE_SIGNED : 0) | (channels == 2 ? DSP_MODE_STEREO : 0);
|
---|
306 | sb_dsp_write(dsp, dsp->playing.mode);
|
---|
307 |
|
---|
308 | dsp->playing.samples = sample_count(sample_size, play_block_size);
|
---|
309 | sb_dsp_write(dsp, (dsp->playing.samples - 1) & 0xff);
|
---|
310 | sb_dsp_write(dsp, (dsp->playing.samples - 1) >> 8);
|
---|
311 |
|
---|
312 | return EOK;
|
---|
313 | }
|
---|
314 |
|
---|
315 | int sb_dsp_stop_playback(sb_dsp_t *dsp, unsigned id)
|
---|
316 | {
|
---|
317 | assert(dsp);
|
---|
318 | if (id != BUFFER_ID)
|
---|
319 | return ENOENT;
|
---|
320 | async_exchange_end(dsp->event_exchange);
|
---|
321 | sb_dsp_write(dsp, DMA_16B_EXIT);
|
---|
322 | return EOK;
|
---|
323 | }
|
---|
324 |
|
---|
325 | int sb_dsp_start_record(sb_dsp_t *dsp, unsigned id, unsigned sample_rate,
|
---|
326 | unsigned sample_size, unsigned channels, bool sign)
|
---|
327 | {
|
---|
328 | return ENOTSUP;
|
---|
329 | }
|
---|
330 |
|
---|
331 | int sb_dsp_stop_record(sb_dsp_t *dsp, unsigned id)
|
---|
332 | {
|
---|
333 | return ENOTSUP;
|
---|
334 | }
|
---|
335 | /**
|
---|
336 | * @}
|
---|
337 | */
|
---|