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

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

sb16: Add skeleton driver for 8237 DMA controller.

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