| 1 | /*
|
|---|
| 2 | * Copyright (c) 2014 Jiri Svoboda
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup hdaudio
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file High Definition Audio stream
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <as.h>
|
|---|
| 36 | #include <bitops.h>
|
|---|
| 37 | #include <byteorder.h>
|
|---|
| 38 | #include <ddf/log.h>
|
|---|
| 39 | #include <ddi.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include <macros.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "hdactl.h"
|
|---|
| 46 | #include "hdaudio.h"
|
|---|
| 47 | #include "regif.h"
|
|---|
| 48 | #include "spec/bdl.h"
|
|---|
| 49 | #include "stream.h"
|
|---|
| 50 |
|
|---|
| 51 | errno_t hda_stream_buffers_alloc(hda_t *hda, hda_stream_buffers_t **rbufs)
|
|---|
| 52 | {
|
|---|
| 53 | void *bdl;
|
|---|
| 54 | void *buffer;
|
|---|
| 55 | uintptr_t buffer_phys;
|
|---|
| 56 | hda_stream_buffers_t *bufs = NULL;
|
|---|
| 57 | size_t i;
|
|---|
| 58 | //size_t j, k;
|
|---|
| 59 | errno_t rc;
|
|---|
| 60 |
|
|---|
| 61 | bufs = calloc(1, sizeof(hda_stream_buffers_t));
|
|---|
| 62 | if (bufs == NULL) {
|
|---|
| 63 | rc = ENOMEM;
|
|---|
| 64 | goto error;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | bufs->nbuffers = 4;
|
|---|
| 68 | bufs->bufsize = 16384;
|
|---|
| 69 |
|
|---|
| 70 | /*
|
|---|
| 71 | * BDL must be aligned to 128 bytes. If 64OK is not set,
|
|---|
| 72 | * it must be within the 32-bit address space.
|
|---|
| 73 | */
|
|---|
| 74 | bdl = AS_AREA_ANY;
|
|---|
| 75 | rc = dmamem_map_anonymous(bufs->nbuffers * sizeof(hda_buffer_desc_t),
|
|---|
| 76 | hda->ctl->ok64bit ? 0 : DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 77 | 0, &bufs->bdl_phys, &bdl);
|
|---|
| 78 | if (rc != EOK)
|
|---|
| 79 | goto error;
|
|---|
| 80 |
|
|---|
| 81 | bufs->bdl = bdl;
|
|---|
| 82 |
|
|---|
| 83 | /* Allocate arrays of buffer pointers */
|
|---|
| 84 |
|
|---|
| 85 | bufs->buf = calloc(bufs->nbuffers, sizeof(void *));
|
|---|
| 86 | if (bufs->buf == NULL)
|
|---|
| 87 | goto error;
|
|---|
| 88 |
|
|---|
| 89 | bufs->buf_phys = calloc(bufs->nbuffers, sizeof(uintptr_t));
|
|---|
| 90 | if (bufs->buf_phys == NULL)
|
|---|
| 91 | goto error;
|
|---|
| 92 |
|
|---|
| 93 | /* Allocate buffers */
|
|---|
| 94 | /*
|
|---|
| 95 | for (i = 0; i < bufs->nbuffers; i++) {
|
|---|
| 96 | buffer = AS_AREA_ANY;
|
|---|
| 97 | rc = dmamem_map_anonymous(bufs->bufsize,
|
|---|
| 98 | bufs->hda->ctl->ok64bit ? 0 : DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 99 | 0, &buffer_phys, &buffer);
|
|---|
| 100 | if (rc != EOK)
|
|---|
| 101 | goto error;
|
|---|
| 102 |
|
|---|
| 103 | ddf_msg(LVL_NOTE, "Stream buf phys=0x%llx virt=%p",
|
|---|
| 104 | (unsigned long long)buffer_phys, buffer);
|
|---|
| 105 |
|
|---|
| 106 | bufs->buf[i] = buffer;
|
|---|
| 107 | bufs->buf_phys[i] = buffer_phys;
|
|---|
| 108 |
|
|---|
| 109 | k = 0;
|
|---|
| 110 | for (j = 0; j < bufs->bufsize / 2; j++) {
|
|---|
| 111 | int16_t *bp = bufs->buf[i];
|
|---|
| 112 | bp[j] = (k > 128) ? -100 : 100;
|
|---|
| 113 | ++k;
|
|---|
| 114 | if (k >= 256)
|
|---|
| 115 | k = 0;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | */
|
|---|
| 119 | /* audio_pcm_iface requires a single contiguous buffer */
|
|---|
| 120 | buffer = AS_AREA_ANY;
|
|---|
| 121 | rc = dmamem_map_anonymous(bufs->bufsize * bufs->nbuffers,
|
|---|
| 122 | hda->ctl->ok64bit ? 0 : DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 123 | 0, &buffer_phys, &buffer);
|
|---|
| 124 | if (rc != EOK) {
|
|---|
| 125 | ddf_msg(LVL_NOTE, "dmamem_map_anon -> %s", str_error_name(rc));
|
|---|
| 126 | goto error;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | for (i = 0; i < bufs->nbuffers; i++) {
|
|---|
| 130 | bufs->buf[i] = buffer + i * bufs->bufsize;
|
|---|
| 131 | bufs->buf_phys[i] = buffer_phys + i * bufs->bufsize;
|
|---|
| 132 |
|
|---|
| 133 | ddf_msg(LVL_NOTE, "Stream buf phys=0x%llx virt=%p",
|
|---|
| 134 | (long long unsigned)(uintptr_t)bufs->buf[i],
|
|---|
| 135 | (void *)bufs->buf_phys[i]);
|
|---|
| 136 | /* k = 0;
|
|---|
| 137 | for (j = 0; j < bufs->bufsize / 2; j++) {
|
|---|
| 138 | int16_t *bp = bufs->buf[i];
|
|---|
| 139 | bp[j] = (k > 128) ? -10000 : 10000;
|
|---|
| 140 | ++k;
|
|---|
| 141 | if (k >= 256)
|
|---|
| 142 | k = 0;
|
|---|
| 143 | }
|
|---|
| 144 | */
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /* Fill in BDL */
|
|---|
| 148 | for (i = 0; i < bufs->nbuffers; i++) {
|
|---|
| 149 | bufs->bdl[i].address = host2uint64_t_le(bufs->buf_phys[i]);
|
|---|
| 150 | bufs->bdl[i].length = host2uint32_t_le(bufs->bufsize);
|
|---|
| 151 | bufs->bdl[i].flags = BIT_V(uint32_t, bdf_ioc);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | *rbufs = bufs;
|
|---|
| 155 | return EOK;
|
|---|
| 156 | error:
|
|---|
| 157 | hda_stream_buffers_free(bufs);
|
|---|
| 158 | return ENOMEM;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void hda_stream_buffers_free(hda_stream_buffers_t *bufs)
|
|---|
| 162 | {
|
|---|
| 163 | if (bufs == NULL)
|
|---|
| 164 | return;
|
|---|
| 165 |
|
|---|
| 166 | /* XXX */
|
|---|
| 167 | free(bufs);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | static void hda_stream_desc_configure(hda_stream_t *stream)
|
|---|
| 171 | {
|
|---|
| 172 | hda_sdesc_regs_t *sdregs;
|
|---|
| 173 | hda_stream_buffers_t *bufs = stream->buffers;
|
|---|
| 174 | uint8_t ctl1;
|
|---|
| 175 | uint8_t ctl3;
|
|---|
| 176 |
|
|---|
| 177 | ctl3 = (stream->sid << 4);
|
|---|
| 178 | ctl1 = 0x4;
|
|---|
| 179 |
|
|---|
| 180 | sdregs = &stream->hda->regs->sdesc[stream->sdid];
|
|---|
| 181 | hda_reg8_write(&sdregs->ctl3, ctl3);
|
|---|
| 182 | hda_reg8_write(&sdregs->ctl1, ctl1);
|
|---|
| 183 | hda_reg32_write(&sdregs->cbl, bufs->nbuffers * bufs->bufsize);
|
|---|
| 184 | hda_reg16_write(&sdregs->lvi, bufs->nbuffers - 1);
|
|---|
| 185 | hda_reg16_write(&sdregs->fmt, stream->fmt);
|
|---|
| 186 | hda_reg32_write(&sdregs->bdpl, LOWER32(bufs->bdl_phys));
|
|---|
| 187 | hda_reg32_write(&sdregs->bdpu, UPPER32(bufs->bdl_phys));
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static void hda_stream_set_run(hda_stream_t *stream, bool run)
|
|---|
| 191 | {
|
|---|
| 192 | uint32_t ctl;
|
|---|
| 193 | hda_sdesc_regs_t *sdregs;
|
|---|
| 194 |
|
|---|
| 195 | sdregs = &stream->hda->regs->sdesc[stream->sdid];
|
|---|
| 196 |
|
|---|
| 197 | ctl = hda_reg8_read(&sdregs->ctl1);
|
|---|
| 198 | if (run)
|
|---|
| 199 | ctl = ctl | BIT_V(uint8_t, sdctl1_run);
|
|---|
| 200 | else
|
|---|
| 201 | ctl = ctl & ~BIT_V(uint8_t, sdctl1_run);
|
|---|
| 202 |
|
|---|
| 203 | hda_reg8_write(&sdregs->ctl1, ctl);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | static void hda_stream_reset_noinit(hda_stream_t *stream)
|
|---|
| 207 | {
|
|---|
| 208 | uint32_t ctl;
|
|---|
| 209 | hda_sdesc_regs_t *sdregs;
|
|---|
| 210 |
|
|---|
| 211 | sdregs = &stream->hda->regs->sdesc[stream->sdid];
|
|---|
| 212 |
|
|---|
| 213 | ctl = hda_reg8_read(&sdregs->ctl1);
|
|---|
| 214 | ctl = ctl | BIT_V(uint8_t, sdctl1_srst);
|
|---|
| 215 | hda_reg8_write(&sdregs->ctl1, ctl);
|
|---|
| 216 |
|
|---|
| 217 | async_usleep(100 * 1000);
|
|---|
| 218 |
|
|---|
| 219 | ctl = hda_reg8_read(&sdregs->ctl1);
|
|---|
| 220 | ctl = ctl & ~BIT_V(uint8_t, sdctl1_srst);
|
|---|
| 221 | hda_reg8_write(&sdregs->ctl1, ctl);
|
|---|
| 222 |
|
|---|
| 223 | async_usleep(100 * 1000);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | hda_stream_t *hda_stream_create(hda_t *hda, hda_stream_dir_t dir,
|
|---|
| 227 | hda_stream_buffers_t *bufs, uint32_t fmt)
|
|---|
| 228 | {
|
|---|
| 229 | hda_stream_t *stream;
|
|---|
| 230 | uint8_t sdid;
|
|---|
| 231 |
|
|---|
| 232 | stream = calloc(1, sizeof(hda_stream_t));
|
|---|
| 233 | if (stream == NULL)
|
|---|
| 234 | return NULL;
|
|---|
| 235 |
|
|---|
| 236 | sdid = 0;
|
|---|
| 237 |
|
|---|
| 238 | switch (dir) {
|
|---|
| 239 | case sdir_input:
|
|---|
| 240 | sdid = 0; /* XXX Allocate - first input SDESC */
|
|---|
| 241 | break;
|
|---|
| 242 | case sdir_output:
|
|---|
| 243 | sdid = hda->ctl->iss; /* XXX Allocate - First output SDESC */
|
|---|
| 244 | break;
|
|---|
| 245 | case sdir_bidi:
|
|---|
| 246 | sdid = hda->ctl->iss + hda->ctl->oss; /* XXX Allocate - First bidi SDESC */
|
|---|
| 247 | break;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | stream->hda = hda;
|
|---|
| 251 | stream->dir = dir;
|
|---|
| 252 | stream->sid = 1; /* XXX Allocate this */
|
|---|
| 253 | stream->sdid = sdid;
|
|---|
| 254 | stream->fmt = fmt;
|
|---|
| 255 | stream->buffers = bufs;
|
|---|
| 256 |
|
|---|
| 257 | ddf_msg(LVL_NOTE, "snum=%d sdidx=%d", stream->sid, stream->sdid);
|
|---|
| 258 |
|
|---|
| 259 | ddf_msg(LVL_NOTE, "Configure stream descriptor");
|
|---|
| 260 | hda_stream_desc_configure(stream);
|
|---|
| 261 | return stream;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | void hda_stream_destroy(hda_stream_t *stream)
|
|---|
| 265 | {
|
|---|
| 266 | ddf_msg(LVL_NOTE, "hda_stream_destroy()");
|
|---|
| 267 | hda_stream_reset_noinit(stream);
|
|---|
| 268 | free(stream);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | void hda_stream_start(hda_stream_t *stream)
|
|---|
| 272 | {
|
|---|
| 273 | ddf_msg(LVL_NOTE, "hda_stream_start()");
|
|---|
| 274 | hda_stream_set_run(stream, true);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | void hda_stream_stop(hda_stream_t *stream)
|
|---|
| 278 | {
|
|---|
| 279 | ddf_msg(LVL_NOTE, "hda_stream_stop()");
|
|---|
| 280 | hda_stream_set_run(stream, false);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | void hda_stream_reset(hda_stream_t *stream)
|
|---|
| 284 | {
|
|---|
| 285 | ddf_msg(LVL_NOTE, "hda_stream_reset()");
|
|---|
| 286 | hda_stream_reset_noinit(stream);
|
|---|
| 287 | hda_stream_desc_configure(stream);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /** @}
|
|---|
| 291 | */
|
|---|