Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Generation of ACPI (Advanced Configuration and Power Interface) tables |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | * Mostly taken from coreboot |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY LOGC_ACPI |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 48342b0 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 14 | #include <uuid.h> |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 15 | #include <acpi/acpigen.h> |
Simon Glass | 8a20076 | 2020-07-07 13:12:01 -0600 | [diff] [blame] | 16 | #include <acpi/acpi_device.h> |
Simon Glass | 837127f | 2020-07-07 21:32:11 -0600 | [diff] [blame] | 17 | #include <acpi/acpi_table.h> |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 18 | #include <dm/acpi.h> |
| 19 | |
| 20 | u8 *acpigen_get_current(struct acpi_ctx *ctx) |
| 21 | { |
| 22 | return ctx->current; |
| 23 | } |
| 24 | |
| 25 | void acpigen_emit_byte(struct acpi_ctx *ctx, uint data) |
| 26 | { |
| 27 | *(u8 *)ctx->current++ = data; |
| 28 | } |
| 29 | |
| 30 | void acpigen_emit_word(struct acpi_ctx *ctx, uint data) |
| 31 | { |
| 32 | acpigen_emit_byte(ctx, data & 0xff); |
| 33 | acpigen_emit_byte(ctx, (data >> 8) & 0xff); |
| 34 | } |
| 35 | |
| 36 | void acpigen_emit_dword(struct acpi_ctx *ctx, uint data) |
| 37 | { |
| 38 | /* Output the value in little-endian format */ |
| 39 | acpigen_emit_byte(ctx, data & 0xff); |
| 40 | acpigen_emit_byte(ctx, (data >> 8) & 0xff); |
| 41 | acpigen_emit_byte(ctx, (data >> 16) & 0xff); |
| 42 | acpigen_emit_byte(ctx, (data >> 24) & 0xff); |
| 43 | } |
Simon Glass | 071c4a5 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 44 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 45 | /* |
| 46 | * Maximum length for an ACPI object generated by this code, |
| 47 | * |
| 48 | * If you need to change this, change acpigen_write_len_f(ctx) and |
| 49 | * acpigen_pop_len(ctx) |
| 50 | */ |
| 51 | #define ACPIGEN_MAXLEN 0xfffff |
| 52 | |
| 53 | void acpigen_write_len_f(struct acpi_ctx *ctx) |
| 54 | { |
| 55 | assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1)); |
| 56 | ctx->len_stack[ctx->ltop++] = ctx->current; |
| 57 | acpigen_emit_byte(ctx, 0); |
| 58 | acpigen_emit_byte(ctx, 0); |
| 59 | acpigen_emit_byte(ctx, 0); |
| 60 | } |
| 61 | |
| 62 | void acpigen_pop_len(struct acpi_ctx *ctx) |
| 63 | { |
| 64 | int len; |
| 65 | char *p; |
| 66 | |
| 67 | assert(ctx->ltop > 0); |
| 68 | p = ctx->len_stack[--ctx->ltop]; |
| 69 | len = ctx->current - (void *)p; |
| 70 | assert(len <= ACPIGEN_MAXLEN); |
| 71 | /* generate store length for 0xfffff max */ |
| 72 | p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf); |
| 73 | p[1] = len >> 4 & 0xff; |
| 74 | p[2] = len >> 12 & 0xff; |
| 75 | } |
| 76 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 77 | void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op) |
| 78 | { |
| 79 | acpigen_emit_byte(ctx, EXT_OP_PREFIX); |
| 80 | acpigen_emit_byte(ctx, op); |
| 81 | } |
| 82 | |
Simon Glass | edc2680 | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 83 | char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el) |
| 84 | { |
| 85 | char *p; |
| 86 | |
| 87 | acpigen_emit_byte(ctx, PACKAGE_OP); |
| 88 | acpigen_write_len_f(ctx); |
| 89 | p = ctx->current; |
| 90 | acpigen_emit_byte(ctx, nr_el); |
| 91 | |
| 92 | return p; |
| 93 | } |
| 94 | |
Simon Glass | 8715ce0 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 95 | void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data) |
| 96 | { |
| 97 | acpigen_emit_byte(ctx, BYTE_PREFIX); |
| 98 | acpigen_emit_byte(ctx, data & 0xff); |
| 99 | } |
| 100 | |
| 101 | void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data) |
| 102 | { |
| 103 | acpigen_emit_byte(ctx, WORD_PREFIX); |
| 104 | acpigen_emit_word(ctx, data); |
| 105 | } |
| 106 | |
| 107 | void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data) |
| 108 | { |
| 109 | acpigen_emit_byte(ctx, DWORD_PREFIX); |
| 110 | acpigen_emit_dword(ctx, data); |
| 111 | } |
| 112 | |
| 113 | void acpigen_write_qword(struct acpi_ctx *ctx, u64 data) |
| 114 | { |
| 115 | acpigen_emit_byte(ctx, QWORD_PREFIX); |
| 116 | acpigen_emit_dword(ctx, data & 0xffffffff); |
| 117 | acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff); |
| 118 | } |
| 119 | |
| 120 | void acpigen_write_zero(struct acpi_ctx *ctx) |
| 121 | { |
| 122 | acpigen_emit_byte(ctx, ZERO_OP); |
| 123 | } |
| 124 | |
| 125 | void acpigen_write_one(struct acpi_ctx *ctx) |
| 126 | { |
| 127 | acpigen_emit_byte(ctx, ONE_OP); |
| 128 | } |
| 129 | |
| 130 | void acpigen_write_integer(struct acpi_ctx *ctx, u64 data) |
| 131 | { |
| 132 | if (data == 0) |
| 133 | acpigen_write_zero(ctx); |
| 134 | else if (data == 1) |
| 135 | acpigen_write_one(ctx); |
| 136 | else if (data <= 0xff) |
| 137 | acpigen_write_byte(ctx, (unsigned char)data); |
| 138 | else if (data <= 0xffff) |
| 139 | acpigen_write_word(ctx, (unsigned int)data); |
| 140 | else if (data <= 0xffffffff) |
| 141 | acpigen_write_dword(ctx, (unsigned int)data); |
| 142 | else |
| 143 | acpigen_write_qword(ctx, data); |
| 144 | } |
| 145 | |
Simon Glass | b4df078 | 2020-07-07 21:32:15 -0600 | [diff] [blame] | 146 | void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name) |
| 147 | { |
| 148 | acpigen_write_name(ctx, name); |
| 149 | acpigen_write_zero(ctx); |
| 150 | } |
| 151 | |
| 152 | void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name) |
| 153 | { |
| 154 | acpigen_write_name(ctx, name); |
| 155 | acpigen_write_one(ctx); |
| 156 | } |
| 157 | |
| 158 | void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val) |
| 159 | { |
| 160 | acpigen_write_name(ctx, name); |
| 161 | acpigen_write_byte(ctx, val); |
| 162 | } |
| 163 | |
| 164 | void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val) |
| 165 | { |
| 166 | acpigen_write_name(ctx, name); |
| 167 | acpigen_write_word(ctx, val); |
| 168 | } |
| 169 | |
| 170 | void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val) |
| 171 | { |
| 172 | acpigen_write_name(ctx, name); |
| 173 | acpigen_write_dword(ctx, val); |
| 174 | } |
| 175 | |
| 176 | void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val) |
| 177 | { |
| 178 | acpigen_write_name(ctx, name); |
| 179 | acpigen_write_qword(ctx, val); |
| 180 | } |
| 181 | |
| 182 | void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val) |
| 183 | { |
| 184 | acpigen_write_name(ctx, name); |
| 185 | acpigen_write_integer(ctx, val); |
| 186 | } |
| 187 | |
| 188 | void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name, |
| 189 | const char *string) |
| 190 | { |
| 191 | acpigen_write_name(ctx, name); |
| 192 | acpigen_write_string(ctx, string); |
| 193 | } |
| 194 | |
Simon Glass | 071c4a5 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 195 | void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < size; i++) |
| 200 | acpigen_emit_byte(ctx, data[i]); |
| 201 | } |
| 202 | |
| 203 | void acpigen_emit_string(struct acpi_ctx *ctx, const char *str) |
| 204 | { |
| 205 | acpigen_emit_stream(ctx, str, str ? strlen(str) : 0); |
| 206 | acpigen_emit_byte(ctx, '\0'); |
| 207 | } |
Simon Glass | 45d6d95 | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 208 | |
| 209 | void acpigen_write_string(struct acpi_ctx *ctx, const char *str) |
| 210 | { |
| 211 | acpigen_emit_byte(ctx, STRING_PREFIX); |
| 212 | acpigen_emit_string(ctx, str); |
| 213 | } |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * The naming conventions for ACPI namespace names are a bit tricky as |
| 217 | * each element has to be 4 chars wide ("All names are a fixed 32 bits.") |
| 218 | * and "By convention, when an ASL compiler pads a name shorter than 4 |
| 219 | * characters, it is done so with trailing underscores ('_')". |
| 220 | * |
| 221 | * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details. |
| 222 | */ |
| 223 | static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx, |
| 224 | const char *name) |
| 225 | { |
| 226 | const char *ptr; |
| 227 | int i; |
| 228 | |
| 229 | for (i = 0, ptr = name; i < 4; i++) { |
| 230 | if (!*ptr || *ptr == '.') |
| 231 | acpigen_emit_byte(ctx, '_'); |
| 232 | else |
| 233 | acpigen_emit_byte(ctx, *ptr++); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static void acpigen_emit_double_namestring(struct acpi_ctx *ctx, |
| 238 | const char *name, int dotpos) |
| 239 | { |
| 240 | acpigen_emit_byte(ctx, DUAL_NAME_PREFIX); |
| 241 | acpigen_emit_simple_namestring(ctx, name); |
| 242 | acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]); |
| 243 | } |
| 244 | |
| 245 | static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx, |
| 246 | const char *name) |
| 247 | { |
| 248 | unsigned char *pathlen; |
| 249 | int count = 0; |
| 250 | |
| 251 | acpigen_emit_byte(ctx, MULTI_NAME_PREFIX); |
| 252 | pathlen = ctx->current; |
| 253 | acpigen_emit_byte(ctx, 0); |
| 254 | |
| 255 | while (*name) { |
| 256 | acpigen_emit_simple_namestring(ctx, name); |
| 257 | /* find end or next entity */ |
| 258 | while (*name != '.' && *name) |
| 259 | name++; |
| 260 | /* forward to next */ |
| 261 | if (*name == '.') |
| 262 | name++; |
| 263 | count++; |
| 264 | } |
| 265 | |
| 266 | *pathlen = count; |
| 267 | } |
| 268 | |
| 269 | void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath) |
| 270 | { |
| 271 | int dotcount; |
| 272 | int dotpos; |
| 273 | int i; |
| 274 | |
| 275 | /* We can start with a '\' */ |
| 276 | if (*namepath == '\\') { |
| 277 | acpigen_emit_byte(ctx, '\\'); |
| 278 | namepath++; |
| 279 | } |
| 280 | |
| 281 | /* And there can be any number of '^' */ |
| 282 | while (*namepath == '^') { |
| 283 | acpigen_emit_byte(ctx, '^'); |
| 284 | namepath++; |
| 285 | } |
| 286 | |
| 287 | for (i = 0, dotcount = 0; namepath[i]; i++) { |
| 288 | if (namepath[i] == '.') { |
| 289 | dotcount++; |
| 290 | dotpos = i; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* If we have only \\ or only ^* then we need to add a null name */ |
| 295 | if (!*namepath) |
| 296 | acpigen_emit_byte(ctx, ZERO_OP); |
| 297 | else if (dotcount == 0) |
| 298 | acpigen_emit_simple_namestring(ctx, namepath); |
| 299 | else if (dotcount == 1) |
| 300 | acpigen_emit_double_namestring(ctx, namepath, dotpos); |
| 301 | else |
| 302 | acpigen_emit_multi_namestring(ctx, namepath); |
| 303 | } |
| 304 | |
| 305 | void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath) |
| 306 | { |
| 307 | acpigen_emit_byte(ctx, NAME_OP); |
| 308 | acpigen_emit_namestring(ctx, namepath); |
| 309 | } |
Simon Glass | 48342b0 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 310 | |
Simon Glass | 6fcdaf1 | 2020-07-07 21:32:10 -0600 | [diff] [blame] | 311 | void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope) |
| 312 | { |
| 313 | acpigen_emit_byte(ctx, SCOPE_OP); |
| 314 | acpigen_write_len_f(ctx); |
| 315 | acpigen_emit_namestring(ctx, scope); |
| 316 | } |
| 317 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 318 | static void acpigen_write_method_internal(struct acpi_ctx *ctx, |
| 319 | const char *name, uint flags) |
| 320 | { |
| 321 | acpigen_emit_byte(ctx, METHOD_OP); |
| 322 | acpigen_write_len_f(ctx); |
| 323 | acpigen_emit_namestring(ctx, name); |
| 324 | acpigen_emit_byte(ctx, flags); |
| 325 | } |
| 326 | |
| 327 | /* Method (name, nargs, NotSerialized) */ |
| 328 | void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs) |
| 329 | { |
| 330 | acpigen_write_method_internal(ctx, name, |
| 331 | nargs & ACPI_METHOD_NARGS_MASK); |
| 332 | } |
| 333 | |
| 334 | /* Method (name, nargs, Serialized) */ |
| 335 | void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name, |
| 336 | int nargs) |
| 337 | { |
| 338 | acpigen_write_method_internal(ctx, name, |
| 339 | (nargs & ACPI_METHOD_NARGS_MASK) | |
| 340 | ACPI_METHOD_SERIALIZED_MASK); |
| 341 | } |
| 342 | |
Simon Glass | a186d93 | 2020-07-07 21:32:14 -0600 | [diff] [blame] | 343 | void acpigen_write_device(struct acpi_ctx *ctx, const char *name) |
| 344 | { |
| 345 | acpigen_emit_ext_op(ctx, DEVICE_OP); |
| 346 | acpigen_write_len_f(ctx); |
| 347 | acpigen_emit_namestring(ctx, name); |
| 348 | } |
| 349 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 350 | void acpigen_write_sta(struct acpi_ctx *ctx, uint status) |
| 351 | { |
| 352 | /* Method (_STA, 0, NotSerialized) { Return (status) } */ |
| 353 | acpigen_write_method(ctx, "_STA", 0); |
| 354 | acpigen_emit_byte(ctx, RETURN_OP); |
| 355 | acpigen_write_byte(ctx, status); |
| 356 | acpigen_pop_len(ctx); |
| 357 | } |
| 358 | |
Simon Glass | 837127f | 2020-07-07 21:32:11 -0600 | [diff] [blame] | 359 | static void acpigen_write_register(struct acpi_ctx *ctx, |
| 360 | const struct acpi_gen_regaddr *addr) |
| 361 | { |
| 362 | /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */ |
| 363 | acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER); |
| 364 | acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */ |
| 365 | acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */ |
| 366 | acpigen_emit_byte(ctx, addr->space_id); |
| 367 | acpigen_emit_byte(ctx, addr->bit_width); |
| 368 | acpigen_emit_byte(ctx, addr->bit_offset); |
| 369 | acpigen_emit_byte(ctx, addr->access_size); |
| 370 | acpigen_emit_dword(ctx, addr->addrl); |
| 371 | acpigen_emit_dword(ctx, addr->addrh); |
| 372 | } |
| 373 | |
| 374 | void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx) |
| 375 | { |
| 376 | /* |
| 377 | * A ResourceTemplate() is a Buffer() with a |
| 378 | * (Byte|Word|DWord) containing the length, followed by one or more |
| 379 | * resource items, terminated by the end tag. |
| 380 | * (small item 0xf, len 1) |
| 381 | */ |
| 382 | acpigen_emit_byte(ctx, BUFFER_OP); |
| 383 | acpigen_write_len_f(ctx); |
| 384 | acpigen_emit_byte(ctx, WORD_PREFIX); |
| 385 | ctx->len_stack[ctx->ltop++] = ctx->current; |
| 386 | |
| 387 | /* |
| 388 | * Add two dummy bytes for the ACPI word (keep aligned with the |
| 389 | * calculation in acpigen_write_resourcetemplate_footer() below) |
| 390 | */ |
| 391 | acpigen_emit_byte(ctx, 0x00); |
| 392 | acpigen_emit_byte(ctx, 0x00); |
| 393 | } |
| 394 | |
| 395 | void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx) |
| 396 | { |
| 397 | char *p = ctx->len_stack[--ctx->ltop]; |
| 398 | int len; |
| 399 | /* |
| 400 | * See ACPI v6.3 section 6.4.2.9: End Tag |
| 401 | * 0x79 <checksum> |
| 402 | * 0x00 is treated as a good checksum according to the spec |
| 403 | * and is what iasl generates. |
| 404 | */ |
| 405 | acpigen_emit_byte(ctx, ACPI_END_TAG); |
| 406 | acpigen_emit_byte(ctx, 0x00); |
| 407 | |
| 408 | /* |
| 409 | * Start counting past the 2-bytes length added in |
| 410 | * acpigen_write_resourcetemplate_header() above |
| 411 | */ |
| 412 | len = (char *)ctx->current - (p + 2); |
| 413 | |
| 414 | /* patch len word */ |
| 415 | p[0] = len & 0xff; |
| 416 | p[1] = (len >> 8) & 0xff; |
| 417 | |
| 418 | acpigen_pop_len(ctx); |
| 419 | } |
| 420 | |
| 421 | void acpigen_write_register_resource(struct acpi_ctx *ctx, |
| 422 | const struct acpi_gen_regaddr *addr) |
| 423 | { |
| 424 | acpigen_write_resourcetemplate_header(ctx); |
| 425 | acpigen_write_register(ctx, addr); |
| 426 | acpigen_write_resourcetemplate_footer(ctx); |
| 427 | } |
| 428 | |
Simon Glass | 48342b0 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 429 | /* |
| 430 | * ToUUID(uuid) |
| 431 | * |
| 432 | * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the |
| 433 | * bytes that make up a UUID Buffer object: |
| 434 | * |
| 435 | * UUID byte order for input to this function: |
| 436 | * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp |
| 437 | * |
| 438 | * UUID byte order output by this function: |
| 439 | * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp |
| 440 | */ |
| 441 | int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid) |
| 442 | { |
| 443 | u8 buf[UUID_BIN_LEN]; |
| 444 | int ret; |
| 445 | |
| 446 | /* Parse UUID string into bytes */ |
| 447 | ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID); |
| 448 | if (ret) |
| 449 | return log_msg_ret("bad hex", -EINVAL); |
| 450 | |
| 451 | /* BufferOp */ |
| 452 | acpigen_emit_byte(ctx, BUFFER_OP); |
| 453 | acpigen_write_len_f(ctx); |
| 454 | |
| 455 | /* Buffer length in bytes */ |
| 456 | acpigen_write_word(ctx, UUID_BIN_LEN); |
| 457 | |
| 458 | /* Output UUID in expected order */ |
| 459 | acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN); |
| 460 | |
| 461 | acpigen_pop_len(ctx); |
| 462 | |
| 463 | return 0; |
| 464 | } |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 465 | |
Simon Glass | d0f7d9b | 2020-07-07 13:12:00 -0600 | [diff] [blame] | 466 | void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level, |
| 467 | uint order, const char *const dev_states[], |
| 468 | size_t dev_states_count) |
| 469 | { |
| 470 | size_t i; |
| 471 | |
| 472 | for (i = 0; i < dev_states_count; i++) { |
| 473 | acpigen_write_name(ctx, dev_states[i]); |
| 474 | acpigen_write_package(ctx, 1); |
| 475 | acpigen_emit_simple_namestring(ctx, name); |
| 476 | acpigen_pop_len(ctx); /* Package */ |
| 477 | } |
| 478 | |
| 479 | acpigen_emit_ext_op(ctx, POWER_RES_OP); |
| 480 | |
| 481 | acpigen_write_len_f(ctx); |
| 482 | |
| 483 | acpigen_emit_simple_namestring(ctx, name); |
| 484 | acpigen_emit_byte(ctx, level); |
| 485 | acpigen_emit_word(ctx, order); |
| 486 | } |
| 487 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 488 | /* Sleep (ms) */ |
| 489 | void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms) |
| 490 | { |
| 491 | acpigen_emit_ext_op(ctx, SLEEP_OP); |
| 492 | acpigen_write_integer(ctx, sleep_ms); |
| 493 | } |
| 494 | |
| 495 | void acpigen_write_store(struct acpi_ctx *ctx) |
| 496 | { |
| 497 | acpigen_emit_byte(ctx, STORE_OP); |
| 498 | } |
| 499 | |
| 500 | /* Or (arg1, arg2, res) */ |
| 501 | void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res) |
| 502 | { |
| 503 | acpigen_emit_byte(ctx, OR_OP); |
| 504 | acpigen_emit_byte(ctx, arg1); |
| 505 | acpigen_emit_byte(ctx, arg2); |
| 506 | acpigen_emit_byte(ctx, res); |
| 507 | } |
| 508 | |
| 509 | /* And (arg1, arg2, res) */ |
| 510 | void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res) |
| 511 | { |
| 512 | acpigen_emit_byte(ctx, AND_OP); |
| 513 | acpigen_emit_byte(ctx, arg1); |
| 514 | acpigen_emit_byte(ctx, arg2); |
| 515 | acpigen_emit_byte(ctx, res); |
| 516 | } |
| 517 | |
| 518 | /* Not (arg, res) */ |
| 519 | void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res) |
| 520 | { |
| 521 | acpigen_emit_byte(ctx, NOT_OP); |
| 522 | acpigen_emit_byte(ctx, arg); |
| 523 | acpigen_emit_byte(ctx, res); |
| 524 | } |
| 525 | |
| 526 | /* Store (str, DEBUG) */ |
| 527 | void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str) |
| 528 | { |
| 529 | acpigen_write_store(ctx); |
| 530 | acpigen_write_string(ctx, str); |
| 531 | acpigen_emit_ext_op(ctx, DEBUG_OP); |
| 532 | } |
Simon Glass | 8a20076 | 2020-07-07 13:12:01 -0600 | [diff] [blame] | 533 | |
| 534 | /** |
| 535 | * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5 |
| 536 | * |
| 537 | * Store (\_SB.GPC0 (addr), Local5) |
| 538 | * |
| 539 | * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in |
| 540 | * the board's gpiolib.asl |
| 541 | * |
| 542 | * The value needs to be stored in a local variable so that it can be used in |
| 543 | * expressions in the ACPI code. |
| 544 | * |
| 545 | * @ctx: ACPI context pointer |
| 546 | * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0" |
| 547 | * @addr: GPIO pin configuration register address |
| 548 | * |
| 549 | */ |
| 550 | static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx, |
| 551 | const char *dw0_read, ulong addr) |
| 552 | { |
| 553 | acpigen_write_store(ctx); |
| 554 | acpigen_emit_namestring(ctx, dw0_read); |
| 555 | acpigen_write_integer(ctx, addr); |
| 556 | acpigen_emit_byte(ctx, LOCAL5_OP); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off |
| 561 | * |
| 562 | * @ctx: ACPI context pointer |
| 563 | * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0" |
| 564 | * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0" |
| 565 | * @gpio_num: GPIO number to adjust |
| 566 | * @vaL: true to set on, false to set off |
| 567 | */ |
| 568 | static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val, |
| 569 | const char *dw0_read, const char *dw0_write, |
| 570 | struct acpi_gpio *gpio, bool val) |
| 571 | { |
| 572 | acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr); |
| 573 | |
| 574 | /* Store (0x40, Local0) */ |
| 575 | acpigen_write_store(ctx); |
| 576 | acpigen_write_integer(ctx, tx_state_val); |
| 577 | acpigen_emit_byte(ctx, LOCAL0_OP); |
| 578 | |
| 579 | if (val) { |
| 580 | /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */ |
| 581 | acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP); |
| 582 | } else { |
| 583 | /* Not (PAD_CFG0_TX_STATE, Local6) */ |
| 584 | acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP); |
| 585 | |
| 586 | /* And (Local5, Local6, Local5) */ |
| 587 | acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * \_SB.SPC0 (addr, Local5) |
| 592 | * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in |
| 593 | * gpiolib.asl. |
| 594 | */ |
| 595 | acpigen_emit_namestring(ctx, dw0_write); |
| 596 | acpigen_write_integer(ctx, gpio->pin0_addr); |
| 597 | acpigen_emit_byte(ctx, LOCAL5_OP); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val, |
| 603 | const char *dw0_read, const char *dw0_write, |
| 604 | struct acpi_gpio *gpio, bool enable) |
| 605 | { |
| 606 | bool set; |
| 607 | int ret; |
| 608 | |
| 609 | set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable; |
| 610 | ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio, |
| 611 | set); |
| 612 | if (ret) |
| 613 | return log_msg_ret("call", ret); |
| 614 | |
| 615 | return 0; |
| 616 | } |