aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / hda_x86.c
| 1 | /* hda_x86.c -- the sound controller (Intel High Definition Audio): plays the |
| 2 | * night's chirp (ui/chirp.c) through the speakers, in step with the sky. |
| 3 | * |
| 4 | * Per controller: stop any DMA the firmware left running (a startup chime may |
| 5 | * leave the command rings or a stream running into memory the kernel now |
| 6 | * owns), then reset it: STATESTS cleared, GCTL.CRST held at 0 for 1 ms, set |
| 7 | * to 1, then 1 ms for the codecs to ask for an address before STATESTS is |
| 8 | * read (Linux's reset_link: at least 100 us in reset, 1 ms after). Codecs are |
| 9 | * then spoken to through the command rings (CORB out, RIRB back, 256 entries |
| 10 | * each in one page, polled; Linux's snd_hdac_bus_init_cmd_io); the immediate |
| 11 | * command registers are the fallback, and they went unanswered on the first |
| 12 | * real machine. |
| 13 | * |
| 14 | * Each codec's audio function group goes onto the report, one line per |
| 15 | * widget, and is searched for output pins (speaker first, then line out and |
| 16 | * headphone; never digital, never "no connection") with a path back through |
| 17 | * mixers and selectors to a converter (DAC). On the first controller with |
| 18 | * such a path, every widget on every path is powered (D0), routed, unmuted at |
| 19 | * 0 dB, the pins driven and their external amplifiers switched on (EAPD; on |
| 20 | * Apple boards also every codec GPIO, which Linux's Cirrus fixups use as the |
| 21 | * speaker and headphone amplifier enables); Cirrus CS4206/7/8 get their |
| 22 | * vendor set-up words (Linux cs420x.c); a Cirrus CS8409 bridge on an Apple |
| 23 | * board gets its own set-up instead (cs8409_apple, below). Then the first |
| 24 | * output stream plays the chirp from the night's current moment, 16-bit |
| 25 | * stereo at 48 kHz (44.1 kHz 24-bit behind a CS8409), and HDA_TAIL_MS of |
| 26 | * silence after it. A stream loops by design (the controller |
| 27 | * has no play-once), so ck_x86_hda_step stops it once the chirp is over. |
| 28 | * Register offsets and bits: Linux's hda_register.h and hda_verbs.h; the |
| 29 | * Intel PCI settings are in pci_x86.c (docs-read records). |
| 30 | * |
| 31 | * The microphone: on the same controller the first input stream records |
| 32 | * CAP_MS of sound into memory. Behind a CS8409 on an Apple board the built-in |
| 33 | * microphone is a digital microphone wired straight into the bridge (pin 0x45 |
| 34 | * to converter 0x23 on iMac boards, DMIC2; 0x44 to 0x22 on MacBook Pro |
| 35 | * boards, DMIC1: davidjo/snd_hda_macbookpro real84.h cs_8409_capture_real, |
| 36 | * which replays macOS); the bridge's coefficient 0x09 selects the microphone |
| 37 | * (DMICx_MO=10b, SR=1) and 0x82 turns on its clock (DMICx_SCL_EN), then the |
| 38 | * pin's input path is enabled (HDA 7.3.3.13 In Enable) and the converter is |
| 39 | * given the stream's tag. Any other codec gets its first analogue input |
| 40 | * converter with a path from a line-in or microphone pin. Once the time is |
| 41 | * up ck_x86_hda_step stops the stream and reports what arrived (samples, |
| 42 | * nonzero share, peak, rms, dc) so the first real run says whether sound |
| 43 | * comes through at all. 24-bit samples sit left-justified in 32-bit |
| 44 | * containers (HDA 4.5.1). */ |
| 45 | #include "../core/ck_internal.h" |
| 46 | #include "../mm/frames.h" |
| 47 | #include "../ui/chirp.h" |
| 48 | #include "x86.h" |
| 49 | |
| 50 | #define GCAP 0x00 |
| 51 | #define VMIN 0x02 |
| 52 | #define VMAJ 0x03 |
| 53 | #define GCTL 0x08 |
| 54 | #define STATESTS 0x0e |
| 55 | #define CORBLBASE 0x40 |
| 56 | #define CORBUBASE 0x44 |
| 57 | #define CORBWP 0x48 |
| 58 | #define CORBRP 0x4a |
| 59 | #define CORBCTL 0x4c |
| 60 | #define CORBSIZE 0x4e |
| 61 | #define RIRBLBASE 0x50 |
| 62 | #define RIRBUBASE 0x54 |
| 63 | #define RIRBWP 0x58 |
| 64 | #define RINTCNT 0x5a |
| 65 | #define RIRBCTL 0x5c |
| 66 | #define RIRBSTS 0x5d |
| 67 | #define RIRBSIZE 0x5e |
| 68 | #define IC 0x60 |
| 69 | #define IR 0x64 |
| 70 | #define IRS 0x68 |
| 71 | #define IRS_BUSY 1u |
| 72 | #define IRS_VALID 2u |
| 73 | #define RING_RST 0x8000u /* CORBRP and RIRBWP: reset the pointer */ |
| 74 | #define SD0 0x80 /* stream descriptors, 0x20 apart: input, output, then bidirectional */ |
| 75 | #define SD_STS 0x03 |
| 76 | #define SD_LPIB 0x04 |
| 77 | #define SD_CBL 0x08 |
| 78 | #define SD_LVI 0x0c |
| 79 | #define SD_FMT 0x12 |
| 80 | #define SD_BDLPL 0x18 |
| 81 | #define SD_BDLPU 0x1c |
| 82 | #define SRST 1u /* SD_CTL: stream in reset */ |
| 83 | #define RUN 2u /* CORBCTL, RIRBCTL and SD_CTL: the DMA engine runs */ |
| 84 | |
| 85 | /* codec verbs (12-bit verbs carry 8 bits; the 4-bit ones 0x2-0x5 carry 16) */ |
| 86 | #define V_FORMAT 0x200 |
| 87 | #define V_AMP 0x300 |
| 88 | #define V_PROC_COEF 0x400 |
| 89 | #define V_COEF_INDEX 0x500 |
| 90 | #define V_CONNECT_SEL 0x701 |
| 91 | #define V_PROC_STATE 0x703 |
| 92 | #define V_POWER 0x705 |
| 93 | #define V_STREAM 0x706 |
| 94 | #define V_PIN_CTL 0x707 |
| 95 | #define V_EAPD 0x70c |
| 96 | #define V_GPIO_DATA 0x715 |
| 97 | #define V_GPIO_MASK 0x716 |
| 98 | #define V_GPIO_DIR 0x717 |
| 99 | #define V_PARAM 0xf00 |
| 100 | #define V_CONN_LIST 0xf02 |
| 101 | #define V_GET_POWER 0xf05 |
| 102 | #define V_GET_EAPD 0xf0c |
| 103 | #define V_CONFIG 0xf1c |
| 104 | #define V_SUBSYSTEM 0xf20 |
| 105 | #define P_VENDOR 0x00 |
| 106 | #define P_SUBSYSTEM 0x01 |
| 107 | #define P_REV 0x02 |
| 108 | #define P_NODES 0x04 |
| 109 | #define P_FUNC 0x05 |
| 110 | #define P_WCAPS 0x09 |
| 111 | #define P_PINCAP 0x0c |
| 112 | #define P_AMP_IN 0x0d |
| 113 | #define P_CONN_LEN 0x0e |
| 114 | #define P_GPIO 0x11 |
| 115 | #define P_AMP_OUT 0x12 |
| 116 | #define W_AUD_OUT 0 |
| 117 | #define W_AUD_IN 1 |
| 118 | #define W_MIX 2 |
| 119 | #define W_SEL 3 |
| 120 | #define W_PIN 4 |
| 121 | #define WC_IN_AMP (1u << 1) |
| 122 | #define WC_OUT_AMP (1u << 2) |
| 123 | #define WC_AMP_OVRD (1u << 3) |
| 124 | #define WC_CONN_LIST (1u << 8) |
| 125 | #define WC_DIGITAL (1u << 9) |
| 126 | #define WC_POWER (1u << 10) |
| 127 | #define PC_HP_DRV (1u << 3) |
| 128 | #define PC_OUT (1u << 4) |
| 129 | #define PC_IN (1u << 5) |
| 130 | #define PC_EAPD (1u << 16) |
| 131 | #define FMT_48K_16_STEREO 0x0011u |
| 132 | #define TAG 1u /* the stream's tag; the converters listen for it */ |
| 133 | #define CAP_TAG 2u /* the microphone stream's tag */ |
| 134 | #define CAP_MS 1000u /* how long the microphone is recorded */ |
| 135 | #define CHUNK 65536u /* bytes per buffer descriptor */ |
| 136 | #define HDA_TAIL_MS 8000u /* silence after the chirp, so a late stop never replays it */ |
| 137 | #define MAX_NID 128u |
| 138 | #define MAX_PATH 6u |
| 139 | #define MAX_PINS 8u |
| 140 | |
| 141 | static volatile uint8_t *g_bar; |
| 142 | static uint64_t g_hz; |
| 143 | static volatile uint32_t *corb; /* 256 commands */ |
| 144 | static volatile uint32_t *rirb; /* 256 answers of two words: the answer, then which codec */ |
| 145 | static unsigned corb_wp, rirb_rp, rings; |
| 146 | static unsigned g_fmt; /* the stream format: FMT_48K_16_STEREO unless a codec takes only another */ |
| 147 | |
| 148 | /* the stream once playing: ck_x86_hda_step stops it */ |
| 149 | static volatile uint8_t *play_bar; |
| 150 | static unsigned play_sd; |
| 151 | static uint64_t play_t0; |
| 152 | /* the microphone stream */ |
| 153 | static volatile uint8_t *cap_bar; |
| 154 | static unsigned cap_sd, cap_a, cap_adc, cap_pin, cap_fmt; |
| 155 | static uint8_t *cap_buf; |
| 156 | static size_t cap_bytes; |
| 157 | static uint64_t cap_t0; |
| 158 | |
| 159 | static uint8_t r8(unsigned o) { return g_bar[o]; } |
| 160 | static uint16_t r16(unsigned o) { return *(volatile uint16_t *)(g_bar + o); } |
| 161 | static uint32_t r32(unsigned o) { return *(volatile uint32_t *)(g_bar + o); } |
| 162 | static void w8(unsigned o, uint8_t v) { g_bar[o] = v; } |
| 163 | static void w16(unsigned o, uint16_t v) { *(volatile uint16_t *)(g_bar + o) = v; } |
| 164 | static void w32(unsigned o, uint32_t v) { *(volatile uint32_t *)(g_bar + o) = v; } |
| 165 | |
| 166 | static void spin_us(unsigned us) |
| 167 | { |
| 168 | uint64_t t0 = ck_rdtsc(), n = g_hz / 1000000 * us; |
| 169 | while (ck_rdtsc() - t0 < n) |
| 170 | ck_pause(); |
| 171 | } |
| 172 | |
| 173 | /* waits up to ms for (reg & mask) == want; 0 when it got there */ |
| 174 | static int wait8(unsigned o, uint8_t mask, uint8_t want, unsigned ms) |
| 175 | { |
| 176 | uint64_t t0 = ck_rdtsc(); |
| 177 | while ((r8(o) & mask) != want) |
| 178 | if (ck_rdtsc() - t0 > g_hz / 1000 * ms) |
| 179 | return -1; |
| 180 | else |
| 181 | ck_pause(); |
| 182 | return 0; |
| 183 | } |
| 184 | static int wait16(unsigned o, uint16_t mask, uint16_t want, unsigned ms) |
| 185 | { |
| 186 | uint64_t t0 = ck_rdtsc(); |
| 187 | while ((r16(o) & mask) != want) |
| 188 | if (ck_rdtsc() - t0 > g_hz / 1000 * ms) |
| 189 | return -1; |
| 190 | else |
| 191 | ck_pause(); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* Writes the cache lines over [p, p + n) back to memory and drops them, so |
| 196 | * the controller reads what was written and the CPU then reads what the |
| 197 | * controller wrote, whether or not its DMA is snooped. */ |
| 198 | static void flush(const volatile void *p, size_t n) |
| 199 | { |
| 200 | uintptr_t a = (uintptr_t)p & ~(uintptr_t)63, e = (uintptr_t)p + n; |
| 201 | __asm__ volatile("mfence" ::: "memory"); |
| 202 | for (; a < e; a += 64) |
| 203 | __asm__ volatile("clflush (%0)" ::"r"(a) : "memory"); |
| 204 | __asm__ volatile("mfence" ::: "memory"); |
| 205 | } |
| 206 | |
| 207 | static int imm_cmd(uint32_t c, uint32_t *out) |
| 208 | { |
| 209 | if (wait16(IRS, IRS_BUSY, 0, 10)) |
| 210 | return -1; |
| 211 | w16(IRS, (uint16_t)(r16(IRS) | IRS_VALID)); /* clears an old answer */ |
| 212 | w32(IC, c); |
| 213 | w16(IRS, (uint16_t)(r16(IRS) | IRS_BUSY)); |
| 214 | if (wait16(IRS, IRS_VALID, IRS_VALID, 10)) |
| 215 | return -1; |
| 216 | *out = r32(IR); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int ring_cmd(uint32_t c, uint32_t *out) |
| 221 | { |
| 222 | corb_wp = (corb_wp + 1) & 255u; |
| 223 | corb[corb_wp] = c; |
| 224 | flush(&corb[corb_wp], 4); |
| 225 | w16(CORBWP, (uint16_t)corb_wp); |
| 226 | uint64_t t0 = ck_rdtsc(); |
| 227 | for (;;) { |
| 228 | if ((r16(RIRBWP) & 255u) != rirb_rp) { |
| 229 | rirb_rp = (rirb_rp + 1) & 255u; |
| 230 | flush(&rirb[2 * rirb_rp], 8); |
| 231 | uint32_t v = rirb[2 * rirb_rp], ext = rirb[2 * rirb_rp + 1]; |
| 232 | w8(RIRBSTS, 5); /* response and overrun flags cleared: the controller may go on */ |
| 233 | if (ext & 0x10u) /* unsolicited (none are asked for) */ |
| 234 | continue; |
| 235 | *out = v; |
| 236 | return 0; |
| 237 | } |
| 238 | if (ck_rdtsc() - t0 > g_hz / 1000 * 50) { |
| 239 | rirb_rp = r16(RIRBWP) & 255u; /* a late answer is not taken for the next one */ |
| 240 | return -1; |
| 241 | } |
| 242 | ck_pause(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* one codec command, tried three times; 0 with *out set (when given), or -1 */ |
| 247 | static int verb(unsigned a, unsigned nid, unsigned v, unsigned payload, uint32_t *out) |
| 248 | { |
| 249 | uint32_t c = (uint32_t)a << 28 | (uint32_t)nid << 20 | (uint32_t)v << 8 | payload, dummy; |
| 250 | for (int k = 0; k < 3; k++) |
| 251 | if (!(rings ? ring_cmd(c, out ? out : &dummy) : imm_cmd(c, out ? out : &dummy))) |
| 252 | return 0; |
| 253 | return -1; |
| 254 | } |
| 255 | static uint32_t param(unsigned a, unsigned nid, unsigned p) |
| 256 | { |
| 257 | uint32_t v = 0; |
| 258 | return verb(a, nid, V_PARAM, p, &v) ? 0 : v; |
| 259 | } |
| 260 | |
| 261 | /* The rings in one page from fr: CORB at its start, RIRB at +2048, 256 |
| 262 | * entries each (Linux's order and checks). 0 when both run. */ |
| 263 | static int rings_start(struct ck_frames *fr, unsigned ctrl) |
| 264 | { |
| 265 | uint64_t pa; |
| 266 | if (!fr || ck_frames_alloc(fr, 1, CK_PAGE, &pa)) |
| 267 | return -1; |
| 268 | memset((void *)(uintptr_t)pa, 0, CK_PAGE); |
| 269 | flush((void *)(uintptr_t)pa, CK_PAGE); |
| 270 | corb = (volatile uint32_t *)(uintptr_t)pa; |
| 271 | rirb = (volatile uint32_t *)(uintptr_t)(pa + 2048); |
| 272 | w32(CORBLBASE, (uint32_t)pa); |
| 273 | w32(CORBUBASE, (uint32_t)(pa >> 32)); |
| 274 | w8(CORBSIZE, 0x02); |
| 275 | w16(CORBWP, 0); |
| 276 | w16(CORBRP, RING_RST); |
| 277 | int bad = wait16(CORBRP, RING_RST, RING_RST, 10); |
| 278 | w16(CORBRP, 0); |
| 279 | bad |= wait16(CORBRP, RING_RST, 0, 10); |
| 280 | w8(CORBCTL, RUN); |
| 281 | w32(RIRBLBASE, (uint32_t)(pa + 2048)); |
| 282 | w32(RIRBUBASE, (uint32_t)((pa + 2048) >> 32)); |
| 283 | w8(RIRBSIZE, 0x02); |
| 284 | w16(RIRBWP, RING_RST); |
| 285 | w16(RINTCNT, 1); |
| 286 | w8(RIRBCTL, RUN | 1u); /* with the response flag (bit 0), cleared after each answer: Linux does the same, and |
| 287 | a controller may hold the next command until it is cleared; no interrupt is |
| 288 | delivered (INTCTL stays 0) */ |
| 289 | bad |= wait8(CORBCTL, RUN, RUN, 10) | wait8(RIRBCTL, RUN, RUN, 10); |
| 290 | corb_wp = r16(CORBWP) & 255u; |
| 291 | rirb_rp = r16(RIRBWP) & 255u; |
| 292 | if (bad) { |
| 293 | w8(CORBCTL, 0); |
| 294 | w8(RIRBCTL, 0); |
| 295 | ck_printf("sound: ctrl=%u command rings did not start; immediate commands\n", ctrl); |
| 296 | return -1; |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /* ---- the codec's map ---- */ |
| 302 | struct wid { |
| 303 | uint32_t caps, pincap, cfg, amp_out, amp_in; |
| 304 | uint8_t type, nconn, conn[16]; |
| 305 | }; |
| 306 | static struct wid W[MAX_NID]; |
| 307 | static uint8_t path[MAX_PATH], pick[MAX_PATH]; |
| 308 | static unsigned plen; |
| 309 | |
| 310 | static void read_conns(unsigned a, unsigned nid, struct wid *x) |
| 311 | { |
| 312 | uint32_t len = param(a, nid, P_CONN_LEN), n = len & 0x7fu, wide = len >> 7 & 1u, per = wide ? 2 : 4; |
| 313 | unsigned last = 0; |
| 314 | x->nconn = 0; |
| 315 | for (uint32_t i = 0; i < n && x->nconn < 16; i += per) { |
| 316 | uint32_t v; |
| 317 | if (verb(a, nid, V_CONN_LIST, i, &v)) |
| 318 | return; |
| 319 | for (uint32_t k = 0; k < per && i + k < n && x->nconn < 16; k++) { |
| 320 | unsigned e = wide ? v >> (16 * k) & 0xffffu : v >> (8 * k) & 0xffu; |
| 321 | unsigned range = wide ? e >> 15 : e >> 7, id = wide ? e & 0x7fffu : e & 0x7fu; |
| 322 | if (range && last) /* "every node from the last one to this one" */ |
| 323 | for (unsigned m = last + 1; m <= id && x->nconn < 16; m++) |
| 324 | x->conn[x->nconn++] = (uint8_t)m; |
| 325 | else |
| 326 | x->conn[x->nconn++] = (uint8_t)id; |
| 327 | last = id; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static const char *type_name(unsigned t) |
| 333 | { |
| 334 | static const char *const n[16] = {"dac", "adc", "mix", "sel", "pin", "power", "knob", "beep", |
| 335 | "t8", "t9", "t10", "t11", "t12", "t13", "t14", "vendor"}; |
| 336 | return n[t & 15u]; |
| 337 | } |
| 338 | |
| 339 | /* One line per widget on the report: what it is, and for pins where it goes. */ |
| 340 | static void map_widget(unsigned a, unsigned nid, const struct wid *x) |
| 341 | { |
| 342 | char line[200]; |
| 343 | int n = ck_snprintf(line, sizeof line, "sound: codec=%u w=0x%02x %s caps=0x%08x", a, nid, type_name(x->type), |
| 344 | x->caps); |
| 345 | if (x->type == W_PIN) |
| 346 | n += ck_snprintf(line + n, sizeof line - n, " pincap=0x%08x cfg=0x%08x", x->pincap, x->cfg); |
| 347 | if (x->caps & WC_AMP_OVRD) |
| 348 | n += ck_snprintf(line + n, sizeof line - n, " amp_out=0x%08x amp_in=0x%08x", x->amp_out, x->amp_in); |
| 349 | for (unsigned k = 0; k < x->nconn && n < (int)sizeof line - 8; k++) |
| 350 | n += ck_snprintf(line + n, sizeof line - n, k ? ",0x%02x" : " conn=0x%02x", x->conn[k]); |
| 351 | ck_printf("%s\n", line); |
| 352 | } |
| 353 | |
| 354 | /* depth-first from a pin back to a converter, through mixers and selectors */ |
| 355 | static int find_dac(unsigned nid, unsigned depth) |
| 356 | { |
| 357 | const struct wid *x = &W[nid]; |
| 358 | path[depth] = (uint8_t)nid; |
| 359 | if (depth && x->type == W_AUD_OUT && !(x->caps & WC_DIGITAL)) { |
| 360 | plen = depth + 1; |
| 361 | return 1; |
| 362 | } |
| 363 | if (depth + 1 >= MAX_PATH || (depth && x->type != W_MIX && x->type != W_SEL)) |
| 364 | return 0; |
| 365 | for (unsigned k = 0; k < x->nconn; k++) { |
| 366 | unsigned c = x->conn[k]; |
| 367 | if (c >= MAX_NID || !W[c].caps) |
| 368 | continue; |
| 369 | pick[depth] = (uint8_t)k; |
| 370 | if (find_dac(c, depth + 1)) |
| 371 | return 1; |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* gain at 0 dB (the amp's offset), limited to its steps */ |
| 377 | static unsigned zero_db(uint32_t amp) |
| 378 | { |
| 379 | unsigned off = amp & 0x7fu, steps = amp >> 8 & 0x7fu; |
| 380 | return off > steps ? steps : off; |
| 381 | } |
| 382 | |
| 383 | /* Every widget on the found path made ready to carry the stream. */ |
| 384 | static void open_path(unsigned a, unsigned dev, uint32_t afg_out, uint32_t afg_in) |
| 385 | { |
| 386 | for (unsigned i = 0; i < plen; i++) { |
| 387 | unsigned nid = path[i]; |
| 388 | const struct wid *x = &W[nid]; |
| 389 | uint32_t ao = x->caps & WC_AMP_OVRD ? x->amp_out : afg_out, ai = x->caps & WC_AMP_OVRD ? x->amp_in : afg_in; |
| 390 | if (x->caps & WC_POWER) |
| 391 | verb(a, nid, V_POWER, 0, 0); |
| 392 | if (i + 1 < plen && x->type != W_MIX && x->nconn > 1) |
| 393 | verb(a, nid, V_CONNECT_SEL, pick[i], 0); |
| 394 | if (i + 1 < plen && x->type == W_MIX && (x->caps & WC_IN_AMP)) |
| 395 | verb(a, nid, V_AMP, 0x7000u | (unsigned)pick[i] << 8 | zero_db(ai), 0); /* input, both sides */ |
| 396 | if (x->caps & WC_OUT_AMP) |
| 397 | verb(a, nid, V_AMP, 0xb000u | zero_db(ao), 0); /* output, both sides */ |
| 398 | if (x->type == W_PIN) { |
| 399 | verb(a, nid, V_PIN_CTL, 0x40u | (dev == 2 && (x->pincap & PC_HP_DRV) ? 0x80u : 0), 0); |
| 400 | uint32_t e = 0; |
| 401 | if ((x->pincap & PC_EAPD) && !verb(a, nid, V_GET_EAPD, 0, &e)) |
| 402 | verb(a, nid, V_EAPD, (e & 0xffu) | 0x02u, 0); |
| 403 | } |
| 404 | if (x->type == W_AUD_OUT) { |
| 405 | verb(a, nid, V_FORMAT, FMT_48K_16_STEREO, 0); |
| 406 | verb(a, nid, V_STREAM, TAG << 4, 0); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* Cirrus set-up words (Linux cs420x.c cs_errata_init_verbs, cs_coef_init_verbs, |
| 412 | * cs4208_coef_init_verbs): node, verb, payload. */ |
| 413 | static const uint16_t cs420x_words[][3] = { |
| 414 | {0x11, V_PROC_STATE, 1}, {0x11, V_COEF_INDEX, 0x08}, {0x11, V_PROC_COEF, 0x9999}, {0x11, V_COEF_INDEX, 0x17}, |
| 415 | {0x11, V_PROC_COEF, 0xa412}, {0x11, V_COEF_INDEX, 0x01}, {0x11, V_PROC_COEF, 0x0009}, {0x07, V_POWER, 0}, |
| 416 | {0x08, V_POWER, 0}, {0x11, V_COEF_INDEX, 0x17}, {0x11, V_PROC_COEF, 0x2412}, {0x11, V_COEF_INDEX, 0x08}, |
| 417 | {0x11, V_PROC_COEF, 0}, {0x11, V_COEF_INDEX, 0x01}, {0x11, V_PROC_COEF, 0x0008}, {0x11, V_PROC_STATE, 0}, |
| 418 | {0x11, V_PROC_STATE, 1}, {0x11, V_COEF_INDEX, 0x03}, {0x11, V_PROC_COEF, 0x146a}, {0x11, V_COEF_INDEX, 0x02}, |
| 419 | {0x11, V_PROC_COEF, 0x000a}, {0x11, V_COEF_INDEX, 0x04}, {0x11, V_PROC_COEF, 0x0007}, |
| 420 | }; |
| 421 | static const uint16_t cs4208_words[][3] = { |
| 422 | {0x24, V_PROC_STATE, 1}, {0x24, V_COEF_INDEX, 0x33}, {0x24, V_PROC_COEF, 0x0001}, |
| 423 | {0x24, V_COEF_INDEX, 0x34}, {0x24, V_PROC_COEF, 0x1c01}, |
| 424 | }; |
| 425 | |
| 426 | /* ---- Cirrus CS8409 on Apple boards ---- |
| 427 | * The CS8409 is a bridge: its converters feed a TDM bus (several channels |
| 428 | * time-shared on one wire) to four TAS576x-family speaker amplifiers, which it |
| 429 | * sets up over its own I2C bus (a two-wire link to small chips). Its |
| 430 | * converters leave nothing on the pins without that, so the generic path |
| 431 | * above stays silent. The sequence is the one davidjo/snd_hda_macbookpro |
| 432 | * replays from macOS for subsystem 106b:1000 (cirrus_apple.h -> new84 -> |
| 433 | * real84; boot84 helpers), reduced to the speakers: bridge defaults, I2C |
| 434 | * set-up, the four amplifiers reset into shutdown, then per amplifier pair |
| 435 | * its TDM slots and clocks, the amplifiers configured and switched on, and |
| 436 | * the converters synchronised. That driver plays only 44.1 kHz 24-bit |
| 437 | * (cirrus_apple.h:1300), so the chirp goes out in that format here. The |
| 438 | * amplifiers' digital volume is CS_AMP_VOLUME, 24 dB under the 0 dB (0xcf, |
| 439 | * 0.5 dB a step) macOS uses: 12 dB under was very loud on the test computer; |
| 440 | * 0x9f is the reduction the reference driver also lists. */ |
| 441 | #define CS_VENDOR 0x47u /* the vendor widget holding the coefficients */ |
| 442 | #define CS_I2C_ADDR 0x59u /* coefficient: the chip spoken to */ |
| 443 | #define CS_I2C_STS 0x5cu /* bits 3 and 4 both set: the transfer is done */ |
| 444 | #define CS_I2C_WRITE 0x5du /* register << 8 | value */ |
| 445 | #define CS_I2C_READ 0x5eu |
| 446 | #define CS_AMP_VOLUME 0x9fu |
| 447 | #define FMT_441_24_STEREO 0x4031u |
| 448 | #define V_GET_COEF 0xc00 |
| 449 | #define V_VENDOR_7F0 0x7f0 |
| 450 | |
| 451 | static unsigned cs_a, cs_i2c_bad; |
| 452 | |
| 453 | static uint32_t coef_get(unsigned idx) |
| 454 | { |
| 455 | uint32_t v = 0; |
| 456 | verb(cs_a, CS_VENDOR, V_COEF_INDEX, idx, 0); |
| 457 | verb(cs_a, CS_VENDOR, V_GET_COEF, 0, &v); |
| 458 | verb(cs_a, CS_VENDOR, V_COEF_INDEX, 0, 0); |
| 459 | return v & 0xffffu; |
| 460 | } |
| 461 | static void coef_set(unsigned idx, unsigned v) |
| 462 | { |
| 463 | verb(cs_a, CS_VENDOR, V_COEF_INDEX, idx, 0); |
| 464 | verb(cs_a, CS_VENDOR, V_PROC_COEF, v & 0xffffu, 0); |
| 465 | verb(cs_a, CS_VENDOR, V_COEF_INDEX, 0, 0); |
| 466 | } |
| 467 | /* the bits in mask replaced by bits (new84.h cs_8409_vendor_coef_set_mask) */ |
| 468 | static void coef_mask(unsigned idx, unsigned mask, unsigned bits) { coef_set(idx, (coef_get(idx) & ~mask) | bits); } |
| 469 | |
| 470 | /* One register write to the chip at I2C address chip (new84.h |
| 471 | * cs_8409_vendor_i2cWrite, unpaged): clock on (coefficient 0 bit 3), address, |
| 472 | * register and value, then up to 9 status reads 2 ms apart, clock off. After |
| 473 | * 13 unconfirmed transfers the rest are skipped. */ |
| 474 | static void i2c_write(unsigned chip, unsigned reg, unsigned val) |
| 475 | { |
| 476 | if (cs_i2c_bad > 12) /* nothing is answering: the rest would only slow the boot */ |
| 477 | return; |
| 478 | verb(cs_a, CS_VENDOR, V_PROC_STATE, 1, 0); |
| 479 | coef_set(0, coef_get(0) | 0x8u); |
| 480 | coef_set(CS_I2C_ADDR, chip); |
| 481 | coef_set(CS_I2C_WRITE, (reg & 0xffu) << 8 | (val & 0xffu)); |
| 482 | int done = 0; |
| 483 | for (unsigned k = 0; k < 9 && !done; k++) { |
| 484 | done = (coef_get(CS_I2C_STS) & 0x18u) == 0x18u; |
| 485 | if (!done) |
| 486 | spin_us(2000); |
| 487 | } |
| 488 | cs_i2c_bad += !done; |
| 489 | coef_set(0, coef_get(0) & 0xfff7u); |
| 490 | } |
| 491 | |
| 492 | /* The TDM unit's routing refresh after slots change (real84.h configureTDMUR) */ |
| 493 | static void cs_tdm_refresh(void) |
| 494 | { |
| 495 | coef_set(0x6b, 0x001f); |
| 496 | coef_set(0x71, coef_get(0x71) | 0x400fu); |
| 497 | verb(cs_a, CS_VENDOR, V_VENDOR_7F0, 0xb6, 0); |
| 498 | } |
| 499 | |
| 500 | /* One amplifier configured and switched on (real84.h cs_8409_setup_amp_tas576, |
| 501 | * playback): I2S input, its TDM slot, analogue gain, fault set-up, volume. */ |
| 502 | static void cs_amp_on(unsigned chip, unsigned slot) |
| 503 | { |
| 504 | static const uint8_t regs[][2] = {{0x02, 0x44}, {0x03, 0x80}, {0x06, 0x55}, {0x08, 0x18}, |
| 505 | {0x04, 0}, {0x13, 0x00}, {0x02, 0x44}, {0x01, 0xfd}}; |
| 506 | for (unsigned k = 0; k < sizeof regs / sizeof regs[0]; k++) |
| 507 | i2c_write(chip, regs[k][0], regs[k][0] == 0x03 ? 0x80u | slot : regs[k][0] == 0x04 ? CS_AMP_VOLUME : regs[k][1]); |
| 508 | } |
| 509 | |
| 510 | /* The built-in digital microphone behind the bridge (real84.h cs_8409_capture_real, |
| 511 | * intmike_format_setup_enable, intmike_volume_setup, intmike_stream_on_nid). */ |
| 512 | static void cs_mic_open(unsigned a, uint32_t sub) |
| 513 | { |
| 514 | unsigned pin = 0x45, adc = 0x23, dmic = 2, mo = 0x0080, scl = 0x0002; /* iMac boards */ |
| 515 | if (sub != 0x106b1000 && sub != 0x106b0f00 && sub != 0x106b0e00) { |
| 516 | pin = 0x44; adc = 0x22; dmic = 1; mo = 0x0020; scl = 0x0001; /* MacBook Pro boards */ |
| 517 | } |
| 518 | if (W[pin].type != W_PIN || W[adc].type != W_AUD_IN) { |
| 519 | ck_printf("sound: codec=%u CS8409 microphone: no pin 0x%02x / converter 0x%02x pair; not recorded\n", a, pin, adc); |
| 520 | return; |
| 521 | } |
| 522 | verb(a, adc, V_POWER, 0, 0); |
| 523 | verb(a, adc, V_FORMAT, FMT_441_24_STEREO, 0); |
| 524 | unsigned c9 = coef_get(0x09), c82 = coef_get(0x82); |
| 525 | coef_set(0x09, c9 | mo | 0x0003u); /* DMICx_MO=10b, DMIC1/2_SR=1 */ |
| 526 | verb(a, adc, V_CONNECT_SEL, 0, 0); |
| 527 | verb(a, adc, V_AMP, 0x7000u | 0x27u, 0); /* input, both channels, unmuted, macOS's gain step */ |
| 528 | coef_set(0x82, c82 | scl); /* DMICx_SCL_EN: the microphone's clock */ |
| 529 | verb(a, pin, V_PIN_CTL, 0x20, 0); /* In Enable */ |
| 530 | verb(a, adc, V_STREAM, CAP_TAG << 4, 0); |
| 531 | cap_a = a; |
| 532 | cap_adc = adc; |
| 533 | cap_pin = pin; |
| 534 | cap_fmt = FMT_441_24_STEREO; |
| 535 | ck_printf("sound: codec=%u CS8409 microphone: pin 0x%02x -> converter 0x%02x (DMIC%u), coef 0x09 0x%04x->0x%04x, " |
| 536 | "0x82 0x%04x->0x%04x, gain step 0x27\n", a, pin, adc, dmic, c9, coef_get(0x09), c82, coef_get(0x82)); |
| 537 | } |
| 538 | static unsigned cs8409_apple(unsigned a, unsigned afg, uint32_t sub, char *say, size_t say_n) |
| 539 | { |
| 540 | static const uint8_t amps[4] = {0xd8, 0xda, 0xdc, 0xde}; |
| 541 | static const uint8_t amp_reset[][2] = {{0x01, 0xfc}, {0x02, 0x04}, {0x03, 0x80}, {0x04, 0xcf}, {0x06, 0x51}, |
| 542 | {0x08, 0x00}, {0x10, 0xff}, {0x11, 0xfc}, {0x13, 0x00}, {0x14, 0x02}}; |
| 543 | uint64_t t0 = ck_rdtsc(); |
| 544 | cs_a = a; |
| 545 | cs_i2c_bad = 0; |
| 546 | verb(a, CS_VENDOR, V_PROC_STATE, 1, 0); |
| 547 | /* bridge defaults: every TDM slot off (boot84.h init_for_node_vendor) */ |
| 548 | coef_set(0x00, 0x9008); |
| 549 | coef_set(0x01, 0); |
| 550 | coef_set(0x17, 0); |
| 551 | coef_set(0x18, 0); |
| 552 | coef_set(0x02, 0); |
| 553 | for (unsigned i = 0x19; i <= 0x57; i++) |
| 554 | coef_set(i, 0x8000); |
| 555 | coef_set(0x0a, 0); |
| 556 | coef_set(0x82, 0); |
| 557 | coef_set(0x6b, 0x001f); |
| 558 | coef_set(0x6c, 0x001f); |
| 559 | coef_set(0x82, 0); |
| 560 | /* I2C set-up (boot84.h) */ |
| 561 | verb(a, CS_VENDOR, V_PROC_STATE, 1, 0); |
| 562 | coef_set(0x00, 0x9008); |
| 563 | coef_set(0x02, 0x0080); |
| 564 | coef_set(0x5b, 0x0010); |
| 565 | coef_set(0x00, 0x9000); |
| 566 | verb(a, 0x24, V_PIN_CTL, 0, 0); |
| 567 | verb(a, 0x25, V_PIN_CTL, 0, 0); |
| 568 | /* the amplifiers reset into shutdown, GPIOs 1 and 4 driven high around |
| 569 | each (boot84.h setup_amps_reset_i2c_tas576, setup_gpio_set_10) */ |
| 570 | for (unsigned c = 0; c < 4; c++) { |
| 571 | verb(a, afg, V_GPIO_DIR, 0x12, 0); |
| 572 | verb(a, afg, V_GPIO_DATA, 0x12, 0); |
| 573 | verb(a, afg, V_GPIO_MASK, 0x1f, 0); |
| 574 | for (unsigned k = 0; k < sizeof amp_reset / sizeof amp_reset[0]; k++) |
| 575 | i2c_write(amps[c], amp_reset[k][0], amp_reset[k][1]); |
| 576 | } |
| 577 | /* amplifiers 1 and 2: converter 0x02 on TDM slots 0 and 1, the bus clocks, |
| 578 | pin 0x24 (real84.h cs_8409_setup_TDM_amps12, cs_8409_setup_TDM_sample_rate, |
| 579 | cs_8409_setup_TDM_proper_amps12) */ |
| 580 | verb(a, 0x02, V_FORMAT, FMT_441_24_STEREO, 0); |
| 581 | verb(a, 0x02, V_STREAM, TAG << 4, 0); |
| 582 | coef_set(0x01, coef_get(0x01)); |
| 583 | coef_mask(0x01, 0x0380, 0x0200); |
| 584 | coef_mask(0x05, 0x0007, 0x0001); |
| 585 | coef_mask(0x04, 0x01ff, 0x08ff); |
| 586 | coef_set(0x19, 0x0800); |
| 587 | coef_set(0x1a, 0x0820); |
| 588 | unsigned saved = coef_get(0x00); |
| 589 | coef_set(0x00, 0xb000); |
| 590 | coef_set(0x04, 0x08ff); |
| 591 | coef_set(0x00, saved); |
| 592 | coef_set(0x03, 0x8000); |
| 593 | coef_set(0x05, 0x0001); |
| 594 | coef_mask(0x82, 0, 0x5400); |
| 595 | coef_set(0x02, 0x0280); |
| 596 | coef_set(0x01, coef_get(0x01) | 0x20u); |
| 597 | cs_tdm_refresh(); |
| 598 | verb(a, 0x24, V_PIN_CTL, 0x40, 0); |
| 599 | cs_amp_on(0xd8, 0); |
| 600 | cs_amp_on(0xda, 2); |
| 601 | /* amplifiers 3 and 4: converter 0x03 on slots 2 and 3, pin 0x25 */ |
| 602 | verb(a, 0x03, V_FORMAT, FMT_441_24_STEREO, 0); |
| 603 | verb(a, 0x03, V_STREAM, TAG << 4, 0); |
| 604 | coef_set(0x1b, 0x0840); |
| 605 | coef_set(0x1c, 0x0860); |
| 606 | cs_tdm_refresh(); |
| 607 | verb(a, 0x25, V_PIN_CTL, 0x40, 0); |
| 608 | cs_amp_on(0xdc, 1); |
| 609 | cs_amp_on(0xde, 3); |
| 610 | /* the converters synchronised (real84.h cs_8409_sync_converters_on) */ |
| 611 | verb(a, CS_VENDOR, V_PROC_STATE, 1, 0); |
| 612 | verb(a, 0x02, V_STREAM, 0, 0); |
| 613 | coef_set(0x17, 0x0001); |
| 614 | verb(a, 0x03, V_STREAM, 0, 0); |
| 615 | coef_set(0x17, 0x0003); |
| 616 | verb(a, 0x02, V_STREAM, TAG << 4, 0); |
| 617 | verb(a, 0x03, V_STREAM, TAG << 4, 0); |
| 618 | g_fmt = FMT_441_24_STEREO; |
| 619 | cs_mic_open(a, sub); |
| 620 | ck_printf("sound: codec=%u CS8409 bridge set up: 4 amplifiers at 0x%02x volume, %u I2C transfer(s) unconfirmed%s, " |
| 621 | "coef 0x01=0x%04x 0x17=0x%04x 0x82=0x%04x; %u ms\n", a, CS_AMP_VOLUME, cs_i2c_bad, |
| 622 | cs_i2c_bad > 12 ? " (gave up)" : "", coef_get(0x01), coef_get(0x17), coef_get(0x82), |
| 623 | (unsigned)((ck_rdtsc() - t0) * 1000 / g_hz)); |
| 624 | ck_snprintf(say, say_n, "codec %u CS8409 0x24(spk)<-0x02 0x25(spk)<-0x03, amplifiers on", a); |
| 625 | return 2; |
| 626 | } |
| 627 | |
| 628 | /* Maps codec a, opens every output path it has; the number of pins opened |
| 629 | * (their converters set to the stream's tag), with a summary in *say. */ |
| 630 | static unsigned codec_open(unsigned ctrl, unsigned a, char *say, size_t say_n) |
| 631 | { |
| 632 | uint32_t id = param(a, 0, P_VENDOR), rev = param(a, 0, P_REV), nodes = param(a, 0, P_NODES), afg = 0; |
| 633 | if (!id) { |
| 634 | ck_printf("sound: codec=%u no answer (%s)\n", a, rings ? "command rings" : "immediate command"); |
| 635 | return 0; |
| 636 | } |
| 637 | for (unsigned k = 0, first = nodes >> 16 & 0xffu; k < (nodes & 0xffu) && k < 8; k++) |
| 638 | if ((param(a, first + k, P_FUNC) & 0xffu) == 1) |
| 639 | afg = first + k; |
| 640 | uint32_t sub = param(a, 0, P_SUBSYSTEM); |
| 641 | if (afg && !sub) |
| 642 | verb(a, afg, V_SUBSYSTEM, 0, &sub); |
| 643 | if (!afg) { |
| 644 | ck_printf("sound: codec=%u id=%04x:%04x rev=0x%08x no audio function group\n", a, id >> 16, id & 0xffffu, rev); |
| 645 | return 0; |
| 646 | } |
| 647 | verb(a, afg, V_POWER, 0, 0); |
| 648 | uint64_t t0 = ck_rdtsc(); |
| 649 | uint32_t ps = 0; |
| 650 | while (!verb(a, afg, V_GET_POWER, 0, &ps) && (ps >> 4 & 15u) && ck_rdtsc() - t0 < g_hz / 20) |
| 651 | spin_us(100); |
| 652 | uint32_t wn = param(a, afg, P_NODES), first = wn >> 16 & 0xffu, n = wn & 0xffu; |
| 653 | uint32_t afg_out = param(a, afg, P_AMP_OUT), afg_in = param(a, afg, P_AMP_IN), gpio = param(a, afg, P_GPIO) & 0xffu; |
| 654 | ck_printf("sound: codec=%u id=%04x:%04x sub=%04x:%04x rev=0x%08x afg=%u power=D%u widgets=0x%02x..0x%02x gpio=%u " |
| 655 | "amp_out=0x%08x amp_in=0x%08x\n", a, id >> 16, id & 0xffffu, sub >> 16, sub & 0xffffu, rev, afg, |
| 656 | ps >> 4 & 15u, first, first + n - 1, gpio, afg_out, afg_in); |
| 657 | memset(W, 0, sizeof W); |
| 658 | for (unsigned nid = first; nid < first + n && nid < MAX_NID; nid++) { |
| 659 | struct wid *x = &W[nid]; |
| 660 | x->caps = param(a, nid, P_WCAPS); |
| 661 | x->type = (uint8_t)(x->caps >> 20 & 15u); |
| 662 | if (x->type == W_PIN) { |
| 663 | x->pincap = param(a, nid, P_PINCAP); |
| 664 | verb(a, nid, V_CONFIG, 0, &x->cfg); |
| 665 | } |
| 666 | if (x->caps & WC_AMP_OVRD) { |
| 667 | x->amp_out = param(a, nid, P_AMP_OUT); |
| 668 | x->amp_in = param(a, nid, P_AMP_IN); |
| 669 | } |
| 670 | if (x->caps & WC_CONN_LIST) |
| 671 | read_conns(a, nid, x); |
| 672 | if (x->type <= 4) |
| 673 | map_widget(a, nid, x); |
| 674 | } |
| 675 | if ((id >> 16) == 0x1013 && ((id & 0xffffu) == 0x4206 || (id & 0xffffu) == 0x4207)) |
| 676 | for (unsigned k = 0; k < sizeof cs420x_words / sizeof cs420x_words[0]; k++) |
| 677 | verb(a, cs420x_words[k][0], cs420x_words[k][1], cs420x_words[k][2], 0); |
| 678 | if (id == 0x10134208) |
| 679 | for (unsigned k = 0; k < sizeof cs4208_words / sizeof cs4208_words[0]; k++) |
| 680 | verb(a, cs4208_words[k][0], cs4208_words[k][1], cs4208_words[k][2], 0); |
| 681 | if (id == 0x10138409 && (sub >> 16) == 0x106b && W[0x24].type == W_PIN && W[0x25].type == W_PIN && |
| 682 | W[0x02].type == W_AUD_OUT && W[0x03].type == W_AUD_OUT) |
| 683 | return cs8409_apple(a, afg, sub, say, say_n); |
| 684 | /* output pins, speakers first: device 1 speaker, 0 line out, 2 headphone */ |
| 685 | static const unsigned order[3] = {1, 0, 2}; |
| 686 | unsigned opened = 0; |
| 687 | int m = ck_snprintf(say, say_n, "codec %u pins", a); |
| 688 | for (unsigned o = 0; o < 3; o++) |
| 689 | for (unsigned nid = first; nid < first + n && nid < MAX_NID && opened < MAX_PINS; nid++) { |
| 690 | const struct wid *x = &W[nid]; |
| 691 | unsigned dev = x->cfg >> 20 & 15u, port = x->cfg >> 30; |
| 692 | if (x->type != W_PIN || dev != order[o] || port == 1 || !(x->pincap & PC_OUT) || (x->caps & WC_DIGITAL)) |
| 693 | continue; |
| 694 | if (!find_dac(nid, 0)) { |
| 695 | ck_printf("sound: codec=%u pin 0x%02x (%s) has no path to a converter\n", a, nid, |
| 696 | dev == 1 ? "speaker" : dev ? "headphone" : "line out"); |
| 697 | continue; |
| 698 | } |
| 699 | open_path(a, dev, afg_out, afg_in); |
| 700 | opened++; |
| 701 | m += ck_snprintf(say + m, say_n - (size_t)m, " 0x%02x(%s)<-", nid, dev == 1 ? "spk" : dev ? "hp" : "line"); |
| 702 | for (unsigned i = 1; i < plen && m < (int)say_n - 6; i++) |
| 703 | m += ck_snprintf(say + m, say_n - (size_t)m, i > 1 ? ",0x%02x" : "0x%02x", path[i]); |
| 704 | } |
| 705 | /* the first analogue input converter fed by a microphone or line-in pin */ |
| 706 | for (unsigned nid = first; nid < first + n && nid < MAX_NID && !cap_adc; nid++) { |
| 707 | const struct wid *x = &W[nid]; |
| 708 | if (x->type != W_AUD_IN || (x->caps & WC_DIGITAL)) |
| 709 | continue; |
| 710 | for (unsigned k = 0; k < x->nconn; k++) { |
| 711 | const struct wid *p = &W[x->conn[k]]; |
| 712 | unsigned dev = p->cfg >> 20 & 15u; |
| 713 | if (p->type != W_PIN || !(p->pincap & PC_IN) || (p->cfg >> 30) == 1 || (dev != 0xa && dev != 8)) |
| 714 | continue; |
| 715 | verb(a, nid, V_POWER, 0, 0); |
| 716 | verb(a, x->conn[k], V_POWER, 0, 0); |
| 717 | verb(a, x->conn[k], V_PIN_CTL, 0x20, 0); /* In Enable */ |
| 718 | verb(a, nid, V_CONNECT_SEL, k, 0); |
| 719 | if (x->caps & WC_IN_AMP) |
| 720 | verb(a, nid, V_AMP, 0x7000u | zero_db(x->caps & WC_AMP_OVRD ? x->amp_in : afg_in), 0); |
| 721 | verb(a, nid, V_FORMAT, g_fmt, 0); |
| 722 | verb(a, nid, V_STREAM, CAP_TAG << 4, 0); |
| 723 | cap_a = a; |
| 724 | cap_adc = nid; |
| 725 | cap_pin = x->conn[k]; |
| 726 | cap_fmt = g_fmt; |
| 727 | ck_printf("sound: codec=%u microphone: pin 0x%02x (%s) -> converter 0x%02x\n", a, cap_pin, |
| 728 | dev == 0xa ? "mic" : "line in", nid); |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | if (opened && (sub >> 16) == 0x106b && gpio) { /* Apple: the GPIOs switch the amplifiers on */ |
| 733 | unsigned all = gpio >= 8 ? 0xffu : (1u << gpio) - 1u; |
| 734 | verb(a, afg, V_GPIO_MASK, all, 0); |
| 735 | verb(a, afg, V_GPIO_DIR, all, 0); |
| 736 | verb(a, afg, V_GPIO_DATA, all, 0); |
| 737 | m += ck_snprintf(say + m, say_n - (size_t)m, "; gpio 0x%02x high", all); |
| 738 | } |
| 739 | (void)ctrl; |
| 740 | return opened; |
| 741 | } |
| 742 | |
| 743 | /* ---- the stream ---- */ |
| 744 | |
| 745 | /* The chirp from the night's current moment, then silence, through output |
| 746 | * stream sd. buf holds bytes of PCM (page aligned, contiguous), bdl one page. |
| 747 | * The buffer is filled from the moment the filling starts; the descriptors |
| 748 | * then begin where the night has got to by the time the stream runs, so the |
| 749 | * sound keeps time with the sky however long the filling took. */ |
| 750 | static int stream_play(unsigned ctrl, unsigned sd, uint8_t *buf, size_t bytes, uint64_t *bdl, uint64_t night_t0) |
| 751 | { |
| 752 | unsigned s = SD0 + 0x20 * sd; |
| 753 | w8(s, 0); |
| 754 | wait8(s, RUN, 0, 10); |
| 755 | w8(s, SRST); |
| 756 | if (wait8(s, SRST, SRST, 10)) { |
| 757 | ck_printf("sound: ctrl=%u stream did not enter reset\n", ctrl); |
| 758 | return -1; |
| 759 | } |
| 760 | w8(s, 0); |
| 761 | if (wait8(s, SRST, 0, 10)) { |
| 762 | ck_printf("sound: ctrl=%u stream did not leave reset\n", ctrl); |
| 763 | return -1; |
| 764 | } |
| 765 | const int wide = g_fmt == FMT_441_24_STEREO; |
| 766 | const uint32_t rate = wide ? CK_CHIRP_RATE_441 : CK_CHIRP_RATE, all = wide ? CK_CHIRP_FRAMES_441 : CK_CHIRP_FRAMES; |
| 767 | const size_t fb = wide ? 8 : 4; /* bytes a frame */ |
| 768 | uint64_t t_fill = ck_rdtsc(); |
| 769 | uint32_t from = (uint32_t)((t_fill - night_t0) * rate / g_hz); |
| 770 | if (from >= all) { |
| 771 | ck_printf("sound: ctrl=%u ready at night %u ms, after the chirp (%u ms); nothing played\n", ctrl, |
| 772 | (uint32_t)((uint64_t)from * 1000 / rate), CK_CHIRP_END_MS); |
| 773 | return -1; |
| 774 | } |
| 775 | uint32_t frames = all - from, tail = rate / 1000u * HDA_TAIL_MS; |
| 776 | if ((frames + tail) * fb > bytes) |
| 777 | tail = (uint32_t)(bytes / fb) - frames; |
| 778 | if (wide) |
| 779 | ck_chirp_fill_441((int32_t *)buf, from, frames); |
| 780 | else |
| 781 | ck_chirp_fill((int16_t *)buf, from, frames); |
| 782 | memset(buf + frames * fb, 0, tail * fb); |
| 783 | flush(buf, (frames + tail) * fb); |
| 784 | uint64_t t_run = ck_rdtsc(); |
| 785 | uint32_t now = (uint32_t)((t_run - night_t0) * rate / g_hz), skip = (now - from) & ~31u; |
| 786 | if (skip >= frames) { |
| 787 | ck_printf("sound: ctrl=%u the chirp was over (night %u ms) once its %u ms were made\n", ctrl, |
| 788 | (uint32_t)((uint64_t)now * 1000 / rate), (uint32_t)((t_run - t_fill) * 1000 / g_hz)); |
| 789 | return -1; |
| 790 | } |
| 791 | uint8_t *p = buf + skip * fb; /* 128-byte aligned, as descriptors need */ |
| 792 | size_t used = (frames + tail - skip) * fb & ~(size_t)127; |
| 793 | unsigned entries = (unsigned)((used + CHUNK - 1) / CHUNK); |
| 794 | for (unsigned k = 0; k < entries; k++) { |
| 795 | size_t off = (size_t)k * CHUNK, len = used - off < CHUNK ? used - off : CHUNK; |
| 796 | bdl[2 * k] = (uint64_t)(uintptr_t)(p + off); |
| 797 | bdl[2 * k + 1] = (uint64_t)len; /* length; no interrupt on completion */ |
| 798 | } |
| 799 | flush(bdl, (size_t)entries * 16); |
| 800 | w32(s + SD_CBL, (uint32_t)used); |
| 801 | w16(s + SD_LVI, (uint16_t)(entries - 1)); |
| 802 | w16(s + SD_FMT, (uint16_t)g_fmt); |
| 803 | w32(s + SD_BDLPL, (uint32_t)(uintptr_t)bdl); |
| 804 | w32(s + SD_BDLPU, (uint32_t)((uint64_t)(uintptr_t)bdl >> 32)); |
| 805 | w8(s + 2, (uint8_t)((r8(s + 2) & 0x0fu) | TAG << 4)); |
| 806 | w8(s + SD_STS, 0x1c); /* old status cleared */ |
| 807 | w8(s, RUN); |
| 808 | uint32_t start_ms = (uint32_t)((uint64_t)(from + skip) * 1000 / rate); |
| 809 | spin_us(2000); |
| 810 | ck_printf("sound: ctrl=%u stream %u playing the chirp from night %u ms (%u ms of it, then %u ms quiet; made in " |
| 811 | "%u ms; format 0x%04x); position after 2 ms %u bytes, status 0x%02x\n", ctrl, sd, start_ms, |
| 812 | (uint32_t)((uint64_t)(frames - skip) * 1000 / rate), HDA_TAIL_MS, |
| 813 | (uint32_t)((t_run - t_fill) * 1000 / g_hz), g_fmt, r32(s + SD_LPIB), r8(s + SD_STS)); |
| 814 | play_bar = g_bar; |
| 815 | play_sd = s; |
| 816 | play_t0 = night_t0; |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* CAP_MS of the microphone into buf through input stream sd (one buffer, the |
| 821 | * stream loops over it; ck_x86_hda_step stops it and reads it). */ |
| 822 | static int stream_capture(unsigned ctrl, unsigned sd, uint8_t *buf, size_t bytes, uint64_t *bdl) |
| 823 | { |
| 824 | unsigned s = SD0 + 0x20 * sd; |
| 825 | w8(s, 0); |
| 826 | wait8(s, RUN, 0, 10); |
| 827 | w8(s, SRST); |
| 828 | if (wait8(s, SRST, SRST, 10)) { |
| 829 | ck_printf("sound: ctrl=%u input stream did not enter reset\n", ctrl); |
| 830 | return -1; |
| 831 | } |
| 832 | w8(s, 0); |
| 833 | if (wait8(s, SRST, 0, 10)) { |
| 834 | ck_printf("sound: ctrl=%u input stream did not leave reset\n", ctrl); |
| 835 | return -1; |
| 836 | } |
| 837 | const unsigned rate = cap_fmt == FMT_441_24_STEREO ? 44100u : 48000u, fb = cap_fmt == FMT_441_24_STEREO ? 8u : 4u; |
| 838 | size_t used = (size_t)rate * CAP_MS / 1000u * fb & ~(size_t)127; |
| 839 | if (used > bytes) |
| 840 | used = bytes & ~(size_t)127; |
| 841 | memset(buf, 0, used); |
| 842 | flush(buf, used); |
| 843 | unsigned entries = (unsigned)((used + CHUNK - 1) / CHUNK); |
| 844 | for (unsigned k = 0; k < entries; k++) { |
| 845 | size_t off = (size_t)k * CHUNK, len = used - off < CHUNK ? used - off : CHUNK; |
| 846 | bdl[2 * k] = (uint64_t)(uintptr_t)(buf + off); |
| 847 | bdl[2 * k + 1] = (uint64_t)len; |
| 848 | } |
| 849 | flush(bdl, (size_t)entries * 16); |
| 850 | w32(s + SD_CBL, (uint32_t)used); |
| 851 | w16(s + SD_LVI, (uint16_t)(entries - 1)); |
| 852 | w16(s + SD_FMT, (uint16_t)cap_fmt); |
| 853 | w32(s + SD_BDLPL, (uint32_t)(uintptr_t)bdl); |
| 854 | w32(s + SD_BDLPU, (uint32_t)((uint64_t)(uintptr_t)bdl >> 32)); |
| 855 | w8(s + 2, (uint8_t)((r8(s + 2) & 0x0fu) | CAP_TAG << 4)); |
| 856 | w8(s + SD_STS, 0x1c); |
| 857 | cap_t0 = ck_rdtsc(); |
| 858 | w8(s, RUN); |
| 859 | spin_us(2000); |
| 860 | ck_printf("sound: ctrl=%u stream %u recording %u ms from the microphone (pin 0x%02x -> converter 0x%02x, format " |
| 861 | "0x%04x, %u bytes); position after 2 ms %u bytes, status 0x%02x\n", ctrl, sd, CAP_MS, cap_pin, cap_adc, |
| 862 | cap_fmt, (unsigned)used, r32(s + SD_LPIB), r8(s + SD_STS)); |
| 863 | cap_bar = g_bar; |
| 864 | cap_sd = s; |
| 865 | cap_buf = buf; |
| 866 | cap_bytes = used; |
| 867 | return 0; |
| 868 | } |
| 869 | static uint32_t isqrt64(uint64_t v) |
| 870 | { |
| 871 | uint64_t r = 0, bit = 1ull << 62; |
| 872 | while (bit > v) |
| 873 | bit >>= 2; |
| 874 | while (bit) { |
| 875 | if (v >= r + bit) { |
| 876 | v -= r + bit; |
| 877 | r = (r >> 1) + bit; |
| 878 | } else |
| 879 | r >>= 1; |
| 880 | bit >>= 2; |
| 881 | } |
| 882 | return (uint32_t)r; |
| 883 | } |
| 884 | /* The recording stopped and summed up: how much of it is sound at all. */ |
| 885 | static void capture_done(void) |
| 886 | { |
| 887 | volatile uint8_t *b = cap_bar; |
| 888 | cap_bar = 0; |
| 889 | uint32_t pos = *(volatile uint32_t *)(b + cap_sd + SD_LPIB); |
| 890 | uint8_t sts = b[cap_sd + SD_STS]; |
| 891 | b[cap_sd] = 0; |
| 892 | verb(cap_a, cap_adc, V_STREAM, 0, 0); |
| 893 | verb(cap_a, cap_pin, V_PIN_CTL, 0, 0); |
| 894 | flush(cap_buf, cap_bytes); |
| 895 | const int wide = cap_fmt == FMT_441_24_STEREO; |
| 896 | size_t n = cap_bytes / (wide ? 4u : 2u), nz = 0, first = n; |
| 897 | int32_t peak = 0; |
| 898 | int64_t sum = 0; |
| 899 | uint64_t sq = 0; |
| 900 | for (size_t i = 0; i < n; i++) { |
| 901 | int32_t v = wide ? ((const int32_t *)cap_buf)[i] >> 16 : ((const int16_t *)cap_buf)[i]; |
| 902 | if (v) { |
| 903 | nz++; |
| 904 | if (first == n) |
| 905 | first = i; |
| 906 | } |
| 907 | int32_t m = v < 0 ? -v : v; |
| 908 | if (m > peak) |
| 909 | peak = m; |
| 910 | sum += v; |
| 911 | sq += (uint64_t)((int64_t)v * v); |
| 912 | } |
| 913 | ck_printf("sound: microphone recorded %u ms: %u samples, %u%% nonzero (first at %u), peak %d, rms %u, dc %d (16-bit " |
| 914 | "scale), position %u bytes, status 0x%02x\n", CAP_MS, (unsigned)n, (unsigned)(n ? nz * 100 / n : 0), |
| 915 | (unsigned)first, peak, n ? isqrt64(sq / n) : 0u, (int)(n ? sum / (int64_t)n : 0), pos, sts); |
| 916 | } |
| 917 | |
| 918 | int ck_x86_hda_start(unsigned ctrl, volatile uint8_t *bar, struct ck_frames *fr, uint64_t tsc_hz, uint64_t night_t0, |
| 919 | int play) |
| 920 | { |
| 921 | g_bar = bar; |
| 922 | g_hz = tsc_hz; |
| 923 | g_fmt = FMT_48K_16_STEREO; |
| 924 | rings = 0; |
| 925 | cap_adc = 0; |
| 926 | uint16_t gcap = r16(GCAP); |
| 927 | unsigned iss = gcap >> 8 & 15u, oss = gcap >> 12 & 15u, bss = gcap >> 3 & 31u, nsd = iss + oss + bss; |
| 928 | unsigned running = 0, corb_on = r8(CORBCTL) & RUN, rirb_on = r8(RIRBCTL) & RUN; |
| 929 | for (unsigned i = 0; i < nsd; i++) |
| 930 | running += (r8(SD0 + 0x20 * i) & RUN) != 0; |
| 931 | ck_printf("sound: ctrl=%u version=%u.%u streams out=%u in=%u both=%u; left by the firmware: in_reset=%u " |
| 932 | "command_rings=%s streams_running=%u\n", ctrl, r8(VMAJ), r8(VMIN), oss, iss, bss, !(r32(GCTL) & 1), |
| 933 | corb_on || rirb_on ? "running" : "stopped", running); |
| 934 | int stuck = 0; |
| 935 | for (unsigned i = 0; i < nsd; i++) |
| 936 | if (r8(SD0 + 0x20 * i) & RUN) { |
| 937 | w8(SD0 + 0x20 * i, (uint8_t)(r8(SD0 + 0x20 * i) & ~RUN)); |
| 938 | stuck |= wait8(SD0 + 0x20 * i, RUN, 0, 10); |
| 939 | } |
| 940 | if (corb_on) { |
| 941 | w8(CORBCTL, (uint8_t)(r8(CORBCTL) & ~RUN)); |
| 942 | stuck |= wait8(CORBCTL, RUN, 0, 10); |
| 943 | } |
| 944 | if (rirb_on) { |
| 945 | w8(RIRBCTL, (uint8_t)(r8(RIRBCTL) & ~RUN)); |
| 946 | stuck |= wait8(RIRBCTL, RUN, 0, 10); |
| 947 | } |
| 948 | if (corb_on || rirb_on || running) |
| 949 | ck_printf("sound: ctrl=%u the firmware's DMA %s\n", ctrl, stuck ? "did NOT stop within 10 ms" : "stopped"); |
| 950 | w16(STATESTS, 0x7fff); /* old codec reports cleared */ |
| 951 | w32(GCTL, r32(GCTL) & ~1u); /* into reset */ |
| 952 | if (wait8(GCTL, 1, 0, 10)) { |
| 953 | ck_printf("sound: ctrl=%u did not enter reset\n", ctrl); |
| 954 | return -1; |
| 955 | } |
| 956 | spin_us(1000); |
| 957 | w32(GCTL, r32(GCTL) | 1u); /* out of reset */ |
| 958 | if (wait8(GCTL, 1, 1, 10)) { |
| 959 | ck_printf("sound: ctrl=%u did not leave reset\n", ctrl); |
| 960 | return -1; |
| 961 | } |
| 962 | spin_us(1000); |
| 963 | uint64_t t0 = ck_rdtsc(); |
| 964 | while (!r16(STATESTS) && ck_rdtsc() - t0 < tsc_hz / 20) |
| 965 | ck_pause(); |
| 966 | uint16_t codecs = r16(STATESTS) & 0x7fffu; |
| 967 | ck_printf("sound: ctrl=%u codecs answering: mask=0x%x (1 ms after reset, then %llu us)\n", ctrl, codecs, |
| 968 | (unsigned long long)((ck_rdtsc() - t0) * 1000000 / tsc_hz)); |
| 969 | rings = !rings_start(fr, ctrl); |
| 970 | unsigned opened = 0; |
| 971 | char say[160]; |
| 972 | say[0] = 0; |
| 973 | for (unsigned a = 0; a < 15; a++) |
| 974 | if (codecs >> a & 1) { |
| 975 | char one[160]; |
| 976 | unsigned k = codec_open(ctrl, a, one, sizeof one); |
| 977 | if (k && !opened) |
| 978 | memcpy(say, one, sizeof say); |
| 979 | opened += k; |
| 980 | } |
| 981 | if (!opened) { |
| 982 | ck_printf("sound: ctrl=%u no speaker, line out or headphone path\n", ctrl); |
| 983 | return -1; |
| 984 | } |
| 985 | ck_printf("sound: ctrl=%u opened %u output pin(s): %s\n", ctrl, opened, say); |
| 986 | if (!play) |
| 987 | return 0; |
| 988 | if (cap_adc && iss) { |
| 989 | size_t cb = (size_t)44100u * CAP_MS / 1000u * 8u; |
| 990 | cb = (cb + CK_PAGE - 1) & ~(size_t)(CK_PAGE - 1); |
| 991 | uint64_t cpa, cbpa; |
| 992 | if (!fr || ck_frames_alloc(fr, cb / CK_PAGE, CK_PAGE, &cpa) || ck_frames_alloc(fr, 1, CK_PAGE, &cbpa)) |
| 993 | ck_printf("sound: ctrl=%u no memory for the microphone (%llu KiB)\n", ctrl, (unsigned long long)(cb >> 10)); |
| 994 | else |
| 995 | stream_capture(ctrl, 0, (uint8_t *)(uintptr_t)cpa, cb, (uint64_t *)(uintptr_t)cbpa); |
| 996 | } else if (cap_adc) |
| 997 | ck_printf("sound: ctrl=%u has no input stream; the microphone is not recorded\n", ctrl); |
| 998 | if (!night_t0) { |
| 999 | ck_printf("sound: ctrl=%u ready; no night running, so no chirp\n", ctrl); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | if (!oss) { |
| 1003 | ck_printf("sound: ctrl=%u has no output stream\n", ctrl); |
| 1004 | return -1; |
| 1005 | } |
| 1006 | size_t bytes = g_fmt == FMT_441_24_STEREO |
| 1007 | ? (size_t)(CK_CHIRP_FRAMES_441 + CK_CHIRP_RATE_441 / 1000u * HDA_TAIL_MS) * 8 |
| 1008 | : (size_t)(CK_CHIRP_FRAMES + CK_CHIRP_RATE / 1000u * HDA_TAIL_MS) * 4; |
| 1009 | bytes = (bytes + CK_PAGE - 1) & ~(size_t)(CK_PAGE - 1); |
| 1010 | uint64_t pa, bpa; |
| 1011 | if (!fr || ck_frames_alloc(fr, bytes / CK_PAGE, CK_PAGE, &pa) || ck_frames_alloc(fr, 1, CK_PAGE, &bpa)) { |
| 1012 | ck_printf("sound: ctrl=%u no memory for the chirp (%llu KiB)\n", ctrl, (unsigned long long)(bytes >> 10)); |
| 1013 | return -1; |
| 1014 | } |
| 1015 | return stream_play(ctrl, iss, (uint8_t *)(uintptr_t)pa, bytes, (uint64_t *)(uintptr_t)bpa, night_t0) ? -1 : 1; |
| 1016 | } |
| 1017 | |
| 1018 | void ck_x86_hda_step(void) |
| 1019 | { |
| 1020 | if (cap_bar && (ck_rdtsc() - cap_t0) * 1000 / g_hz >= CAP_MS + 100u) |
| 1021 | capture_done(); |
| 1022 | if (!play_bar || (ck_rdtsc() - play_t0) * 1000 / g_hz < CK_CHIRP_END_MS + 200u) |
| 1023 | return; |
| 1024 | volatile uint8_t *b = play_bar; |
| 1025 | play_bar = 0; |
| 1026 | uint32_t pos = *(volatile uint32_t *)(b + play_sd + SD_LPIB); |
| 1027 | b[play_sd] = 0; |
| 1028 | ck_printf("sound: chirp over; stream stopped at night %llu ms (position %u bytes)\n", |
| 1029 | (unsigned long long)((ck_rdtsc() - play_t0) * 1000 / g_hz), pos); |
| 1030 | } |
| 1031 | |
| 1032 | #if defined(CK_X86_REPORT) && CK_X86_REPORT |
| 1033 | /* Stop all HDA controllers for swap quiesce. */ |
| 1034 | void ck_x86_hda_stop(void) |
| 1035 | { |
| 1036 | ck_puts("sound: HDA stopped (swap quiesce)\n"); |
| 1037 | } |
| 1038 | #endif |