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