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

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

sb16: Add pseudo DMA malloc.

  • Property mode set to 100644
File size: 4.1 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 <libarch/ddi.h>
36
37#include "dma.h"
38#include "ddf_log.h"
39#include "dsp_commands.h"
40#include "dsp.h"
41
42
43#ifndef DSP_RETRY_COUNT
44#define DSP_RETRY_COUNT 100
45#endif
46
47#define DSP_RESET_RESPONSE 0xaa
48
49static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
50{
51 assert(data);
52 assert(dsp);
53 uint8_t status;
54 size_t attempts = DSP_RETRY_COUNT;
55 do {
56 status = pio_read_8(&dsp->regs->dsp_read_status);
57 } while (--attempts && ((status & DSP_READ_READY) == 0));
58
59 if ((status & DSP_READ_READY) == 0)
60 return EIO;
61
62 *data = pio_read_8(&dsp->regs->dsp_data_read);
63 return EOK;
64}
65/*----------------------------------------------------------------------------*/
66static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
67{
68 assert(dsp);
69 uint8_t status;
70 size_t attempts = DSP_RETRY_COUNT;
71 do {
72 status = pio_read_8(&dsp->regs->dsp_write);
73 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
74
75 if ((status & DSP_WRITE_BUSY))
76 return EIO;
77
78 pio_write_8(&dsp->regs->dsp_write, data);
79 return EOK;
80}
81/*----------------------------------------------------------------------------*/
82static inline void sb_dsp_reset(sb_dsp_t *dsp)
83{
84 assert(dsp);
85 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
86 pio_write_8(&dsp->regs->dsp_reset, 1);
87 udelay(3); /* Keep reset for 3 us */
88 pio_write_8(&dsp->regs->dsp_reset, 0);
89}
90/*----------------------------------------------------------------------------*/
91int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs)
92{
93 assert(dsp);
94 dsp->regs = regs;
95 sb_dsp_reset(dsp);
96 /* "DSP takes about 100 microseconds to initialize itself" */
97 udelay(100);
98 uint8_t response;
99 const int ret = sb_dsp_read(dsp, &response);
100 if (ret != EOK) {
101 ddf_log_error("Failed to read DSP reset response value.\n");
102 return ret;
103 }
104 if (response != DSP_RESET_RESPONSE) {
105 ddf_log_error("Invalid DSP reset response: %x.\n", response);
106 return EIO;
107 }
108
109 /* Get DSP version number */
110 sb_dsp_write(dsp, DSP_VERSION);
111 sb_dsp_read(dsp, &dsp->version.major);
112 sb_dsp_read(dsp, &dsp->version.minor);
113 return EOK;
114}
115/*----------------------------------------------------------------------------*/
116int sb_dsp_play_direct(sb_dsp_t *dsp, const uint8_t *data, size_t size,
117 unsigned sampling_rate, unsigned channels, unsigned bit_depth)
118{
119 assert(dsp);
120 if (channels != 1 || bit_depth != 8)
121 return EIO;
122 /* In microseconds */
123 const unsigned wait_period = 1000000 / sampling_rate;
124 while (size--) {
125 pio_write_8(&dsp->regs->dsp_write, DIRECT_8B_OUTPUT);
126 pio_write_8(&dsp->regs->dsp_write, *data++);
127 udelay(wait_period);
128 }
129 return EOK;
130}
131/**
132 * @}
133 */
Note: See TracBrowser for help on using the repository browser.