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

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

sb16, dsp: Remove internal playback functions.

External playback works reasonably well.

  • Property mode set to 100644
File size: 8.4 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 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
59static 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/*----------------------------------------------------------------------------*/
76static 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/*----------------------------------------------------------------------------*/
92static 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/*----------------------------------------------------------------------------*/
101static 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/*----------------------------------------------------------------------------*/
115static inline int sb_setup_buffer(sb_dsp_t *dsp)
116{
117 assert(dsp);
118 uint8_t *buffer = dma_create_buffer24(BUFFER_SIZE);
119 if (buffer == NULL) {
120 ddf_log_error("Failed to allocate buffer.\n");
121 return ENOMEM;
122 }
123
124 const uintptr_t pa = addr_to_phys(buffer);
125 assert(pa < (1 << 25));
126 /* Set 16 bit channel */
127 const int ret = sb_setup_dma(dsp, pa, BUFFER_SIZE);
128 if (ret == EOK) {
129 dsp->buffer.data = buffer;
130 dsp->buffer.size = BUFFER_SIZE;
131 bzero(buffer, BUFFER_SIZE);
132 } else {
133 ddf_log_error("Failed to setup DMA16 channel %s.\n",
134 str_error(ret));
135 dma_destroy_buffer(buffer);
136 }
137 return ret;
138}
139/*----------------------------------------------------------------------------*/
140static inline void sb_clear_buffer(sb_dsp_t *dsp)
141{
142 dma_destroy_buffer(dsp->buffer.data);
143 dsp->buffer.data = NULL;
144 dsp->buffer.size = 0;
145}
146/*----------------------------------------------------------------------------*/
147static inline size_t sample_count(unsigned sample_size, size_t byte_count)
148{
149 if (sample_size == 16) {
150 return byte_count / 2;
151 }
152 return byte_count;
153}
154/*----------------------------------------------------------------------------*/
155int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
156 int dma8, int dma16)
157{
158 assert(dsp);
159 dsp->regs = regs;
160 dsp->dma8_channel = dma8;
161 dsp->dma16_channel = dma16;
162 dsp->sb_dev = dev;
163 sb_dsp_reset(dsp);
164 /* "DSP takes about 100 microseconds to initialize itself" */
165 udelay(100);
166 uint8_t response;
167 const int ret = sb_dsp_read(dsp, &response);
168 if (ret != EOK) {
169 ddf_log_error("Failed to read DSP reset response value.\n");
170 return ret;
171 }
172 if (response != DSP_RESET_RESPONSE) {
173 ddf_log_error("Invalid DSP reset response: %x.\n", response);
174 return EIO;
175 }
176
177 /* Get DSP version number */
178 sb_dsp_write(dsp, DSP_VERSION);
179 sb_dsp_read(dsp, &dsp->version.major);
180 sb_dsp_read(dsp, &dsp->version.minor);
181
182 return ret;
183}
184/*----------------------------------------------------------------------------*/
185void sb_dsp_interrupt(sb_dsp_t *dsp)
186{
187#ifndef AUTO_DMA_MODE
188 assert(dsp);
189 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
190 sb_dsp_write(dsp, dsp->playing.mode);
191 sb_dsp_write(dsp, (dsp->playing.samples - 1) & 0xff);
192 sb_dsp_write(dsp, (dsp->playing.samples - 1) >> 8);
193#endif
194}
195/*----------------------------------------------------------------------------*/
196int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size, unsigned *id)
197{
198 assert(dsp);
199 const int ret = sb_setup_buffer(dsp);
200 ddf_log_debug("Providing buffer(%u): %p, %zu.\n",
201 BUFFER_ID, dsp->buffer.data, dsp->buffer.size);
202 if (ret == EOK && buffer)
203 *buffer = dsp->buffer.data;
204 if (ret == EOK && size)
205 *size = dsp->buffer.size;
206 if (ret == EOK && id)
207 *id = BUFFER_ID;
208 return ret;
209}
210/*----------------------------------------------------------------------------*/
211int sb_dsp_release_buffer(sb_dsp_t *dsp, unsigned id)
212{
213 assert(dsp);
214 if (id != BUFFER_ID)
215 return ENOENT;
216 sb_clear_buffer(dsp);
217 return EOK;
218}
219/*----------------------------------------------------------------------------*/
220int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned id, unsigned sampling_rate,
221 unsigned sample_size, unsigned channels, bool sign)
222{
223 assert(dsp);
224
225 /* Check supported parameters */
226 ddf_log_debug("Starting playback on buffer(%u): rate: %u, size: %u, "
227 " channels: %u, signed: %s.\n", id, sampling_rate, sample_size,
228 channels, sign ? "YES" : "NO" );
229 if (id != BUFFER_ID)
230 return ENOENT;
231 if (sample_size != 16) // FIXME We only support 16 bit playback
232 return ENOTSUP;
233 if (channels != 1 && channels != 2)
234 return ENOTSUP;
235 if (sampling_rate > 44100)
236 return ENOTSUP;
237
238
239 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
240 sb_dsp_write(dsp, sampling_rate >> 8);
241 sb_dsp_write(dsp, sampling_rate & 0xff);
242
243 ddf_log_debug("Sampling rate: %hhx:%hhx.\n",
244 sampling_rate >> 8, sampling_rate & 0xff);
245
246#ifdef AUTO_DMA_MODE
247 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
248#else
249 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
250#endif
251
252 dsp->playing.mode = 0 |
253 (sign ? DSP_MODE_SIGNED : 0) | (channels == 2 ? DSP_MODE_STEREO : 0);
254 sb_dsp_write(dsp, dsp->playing.mode);
255
256 dsp->playing.samples = sample_count(sample_size, PLAY_BLOCK_SIZE);
257 sb_dsp_write(dsp, (dsp->playing.samples - 1) & 0xff);
258 sb_dsp_write(dsp, (dsp->playing.samples - 1) >> 8);
259
260 return EOK;
261 return ENOTSUP;
262}
263/*----------------------------------------------------------------------------*/
264int sb_dsp_stop_playback(sb_dsp_t *dsp, unsigned id)
265{
266 assert(dsp);
267 if (id != BUFFER_ID)
268 return ENOENT;
269 sb_dsp_write(dsp, DMA_16B_EXIT);
270 return EOK;
271}
272/*----------------------------------------------------------------------------*/
273int sb_dsp_start_record(sb_dsp_t *dsp, unsigned id, unsigned sample_rate,
274 unsigned sample_size, unsigned channels, bool sign)
275{
276 return ENOTSUP;
277}
278/*----------------------------------------------------------------------------*/
279int sb_dsp_stop_record(sb_dsp_t *dsp, unsigned id)
280{
281 return ENOTSUP;
282}
283/**
284 * @}
285 */
Note: See TracBrowser for help on using the repository browser.