blob: 9f1710e35fb43ad444c3f1943eb140d7ce827d18 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Duncan Lauried9af3ce2016-05-08 18:15:25 -07003
4#include <string.h>
5#include <arch/acpi.h>
6#include <arch/acpi_device.h>
7#include <arch/acpigen.h>
8#include <device/device.h>
9#include <device/path.h>
Elyes HAOUAS70a03dd2019-12-02 20:47:50 +010010#include <stdlib.h>
Patrick Rudolphc83bab62019-12-13 12:16:06 +010011#include <crc_byte.h>
12
Julius Wernercd49cce2019-03-05 16:53:33 -080013#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -070014#include <gpio.h>
15#endif
Duncan Lauried9af3ce2016-05-08 18:15:25 -070016
Duncan Laurieffc99902016-07-02 19:56:06 -070017#define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301"
18#define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b"
Duncan Laurie559e9472016-05-10 13:18:17 -070019
Duncan Laurie6b7c1f62016-05-09 15:38:36 -070020/* Write empty word value and return pointer to it */
21static void *acpi_device_write_zero_len(void)
22{
23 char *p = acpigen_get_current();
24 acpigen_emit_word(0);
25 return p;
26}
27
28/* Fill in length value from start to current at specified location */
29static void acpi_device_fill_from_len(char *ptr, char *start)
30{
31 uint16_t len = acpigen_get_current() - start;
32 ptr[0] = len & 0xff;
33 ptr[1] = (len >> 8) & 0xff;
34}
35
36/*
37 * Fill in the length field with the value calculated from after
38 * the 16bit field to acpigen current as this length value does
39 * not include the length field itself.
40 */
41static void acpi_device_fill_len(void *ptr)
42{
43 acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t));
44}
45
Duncan Lauried9af3ce2016-05-08 18:15:25 -070046/* Locate and return the ACPI name for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +053047const char *acpi_device_name(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -070048{
Aamir Bohrae825d3f2019-07-30 10:11:41 +053049 const struct device *pdev = dev;
Duncan Laurie47029142018-05-07 14:28:53 -070050 const char *name = NULL;
51
Duncan Lauried9af3ce2016-05-08 18:15:25 -070052 if (!dev)
53 return NULL;
54
55 /* Check for device specific handler */
56 if (dev->ops->acpi_name)
57 return dev->ops->acpi_name(dev);
58
Duncan Laurie47029142018-05-07 14:28:53 -070059 /* Walk up the tree to find if any parent can identify this device */
60 while (pdev->bus) {
61 pdev = pdev->bus->dev;
62 if (!pdev)
63 break;
64 if (pdev->path.type == DEVICE_PATH_ROOT)
65 break;
66 if (pdev->ops && pdev->ops->acpi_name)
67 name = pdev->ops->acpi_name(dev);
68 if (name)
69 return name;
70 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -070071
72 return NULL;
73}
74
Patrick Rudolpheeb8e742019-08-20 08:20:01 +020075/* Locate and return the ACPI _HID (Hardware ID) for this device */
76const char *acpi_device_hid(const struct device *dev)
77{
78 if (!dev)
79 return NULL;
80
81 /* Check for device specific handler */
82 if (dev->ops->acpi_hid)
83 return dev->ops->acpi_hid(dev);
84
85 /*
86 * Don't walk up the tree to find any parent that can identify this device, as
87 * PNP devices are hard to identify.
88 */
89
90 return NULL;
91}
92
Patrick Rudolphc83bab62019-12-13 12:16:06 +010093/*
94 * Generate unique ID based on the ACPI path.
95 * Collisions on the same _HID are possible but very unlikely.
96 */
97uint32_t acpi_device_uid(struct device *dev)
98{
99 const char *path = acpi_device_path(dev);
100 if (!path)
101 return 0;
102
103 return CRC(path, strlen(path), crc32_byte);
104}
105
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700106/* Recursive function to find the root device and print a path from there */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530107static ssize_t acpi_device_path_fill(const struct device *dev, char *buf,
Duncan Laurie47029142018-05-07 14:28:53 -0700108 size_t buf_len, size_t cur)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700109{
110 const char *name = acpi_device_name(dev);
Duncan Laurie47029142018-05-07 14:28:53 -0700111 ssize_t next = 0;
112
113 if (!name)
114 return -1;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700115
116 /*
117 * Make sure this name segment will fit, including the path segment
118 * separator and possible NUL terminator if this is the last segment.
119 */
Duncan Laurie47029142018-05-07 14:28:53 -0700120 if (!dev || (cur + strlen(name) + 2) > buf_len)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700121 return cur;
122
123 /* Walk up the tree to the root device */
124 if (dev->path.type != DEVICE_PATH_ROOT && dev->bus && dev->bus->dev)
125 next = acpi_device_path_fill(dev->bus->dev, buf, buf_len, cur);
Duncan Laurie47029142018-05-07 14:28:53 -0700126 if (next < 0)
127 return next;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700128
129 /* Fill in the path from the root device */
130 next += snprintf(buf + next, buf_len - next, "%s%s",
Timothy Pearsonaeb61012017-04-13 17:06:16 -0500131 (dev->path.type == DEVICE_PATH_ROOT
132 || (strlen(name) == 0)) ?
133 "" : ".", name);
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700134
135 return next;
136}
137
138/*
139 * Warning: just as with dev_path() this uses a static buffer
140 * so should not be called mulitple times in one statement
141 */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530142const char *acpi_device_path(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700143{
144 static char buf[DEVICE_PATH_MAX] = {};
145
146 if (!dev)
147 return NULL;
148
149 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
150 return NULL;
151
152 return buf;
153}
154
155/* Return the path of the parent device as the ACPI Scope for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530156const char *acpi_device_scope(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700157{
Duncan Lauried02685b2016-06-30 14:37:37 -0700158 static char buf[DEVICE_PATH_MAX] = {};
159
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700160 if (!dev || !dev->bus || !dev->bus->dev)
161 return NULL;
162
Duncan Lauried02685b2016-06-30 14:37:37 -0700163 if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0)
164 return NULL;
165
166 return buf;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700167}
168
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100169/* Concatenate the device path and provided name suffix */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530170const char *acpi_device_path_join(const struct device *dev, const char *name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700171{
172 static char buf[DEVICE_PATH_MAX] = {};
Jacob Garberf2ba2d92019-07-02 10:50:10 -0600173 ssize_t len;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700174
175 if (!dev)
176 return NULL;
177
178 /* Build the path of this device */
179 len = acpi_device_path_fill(dev, buf, sizeof(buf), 0);
180 if (len <= 0)
181 return NULL;
182
183 /* Ensure there is room for the added name, separator, and NUL */
184 if ((len + strlen(name) + 2) > sizeof(buf))
185 return NULL;
186 snprintf(buf + len, sizeof(buf) - len, ".%s", name);
187
188 return buf;
189}
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700190
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800191int acpi_device_status(const struct device *dev)
192{
193 if (!dev->enabled)
194 return ACPI_STATUS_DEVICE_ALL_OFF;
195 if (dev->hidden)
196 return ACPI_STATUS_DEVICE_HIDDEN_ON;
197 return ACPI_STATUS_DEVICE_ALL_ON;
198}
199
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100200
201/* Write the unique _UID based on ACPI device path. */
202void acpi_device_write_uid(struct device *dev)
203{
204 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
205}
206
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700207/* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
208void acpi_device_write_interrupt(const struct acpi_irq *irq)
209{
210 void *desc_length;
211 uint8_t flags;
212
213 if (!irq || !irq->pin)
214 return;
215
216 /* This is supported by GpioInt() but not Interrupt() */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800217 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700218 return;
219
220 /* Byte 0: Descriptor Type */
221 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
222
223 /* Byte 1-2: Length (filled in later) */
224 desc_length = acpi_device_write_zero_len();
225
226 /*
227 * Byte 3: Flags
228 * [7:5]: Reserved
229 * [4]: Wake (0=NO_WAKE 1=WAKE)
230 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
231 * [2]: Polarity (0=HIGH 1=LOW)
232 * [1]: Mode (0=LEVEL 1=EDGE)
233 * [0]: Resource (0=PRODUCER 1=CONSUMER)
234 */
235 flags = 1 << 0; /* ResourceConsumer */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800236 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700237 flags |= 1 << 1;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800238 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700239 flags |= 1 << 2;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800240 if (irq->shared == ACPI_IRQ_SHARED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700241 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800242 if (irq->wake == ACPI_IRQ_WAKE)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700243 flags |= 1 << 4;
244 acpigen_emit_byte(flags);
245
246 /* Byte 4: Interrupt Table Entry Count */
247 acpigen_emit_byte(1);
248
249 /* Byte 5-8: Interrupt Number */
250 acpigen_emit_dword(irq->pin);
251
252 /* Fill in Descriptor Length (account for len word) */
253 acpi_device_fill_len(desc_length);
254}
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700255
256/* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
257void acpi_device_write_gpio(const struct acpi_gpio *gpio)
258{
259 void *start, *desc_length;
260 void *pin_table_offset, *vendor_data_offset, *resource_offset;
261 uint16_t flags = 0;
262 int pin;
263
264 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
265 return;
266
267 start = acpigen_get_current();
268
269 /* Byte 0: Descriptor Type */
270 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
271
272 /* Byte 1-2: Length (fill in later) */
273 desc_length = acpi_device_write_zero_len();
274
275 /* Byte 3: Revision ID */
276 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
277
278 /* Byte 4: GpioIo or GpioInt */
279 acpigen_emit_byte(gpio->type);
280
281 /*
282 * Byte 5-6: General Flags
283 * [15:1]: 0 => Reserved
284 * [0]: 1 => ResourceConsumer
285 */
286 acpigen_emit_word(1 << 0);
287
288 switch (gpio->type) {
289 case ACPI_GPIO_TYPE_INTERRUPT:
290 /*
291 * Byte 7-8: GPIO Interrupt Flags
292 * [15:5]: 0 => Reserved
293 * [4]: Wake (0=NO_WAKE 1=WAKE)
294 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
295 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
296 * [0]: Mode (0=LEVEL 1=EDGE)
297 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800298 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700299 flags |= 1 << 0;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800300 if (gpio->irq.shared == ACPI_IRQ_SHARED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700301 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800302 if (gpio->irq.wake == ACPI_IRQ_WAKE)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700303 flags |= 1 << 4;
304
305 switch (gpio->irq.polarity) {
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800306 case ACPI_IRQ_ACTIVE_HIGH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700307 flags |= 0 << 1;
308 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800309 case ACPI_IRQ_ACTIVE_LOW:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700310 flags |= 1 << 1;
311 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800312 case ACPI_IRQ_ACTIVE_BOTH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700313 flags |= 2 << 1;
314 break;
315 }
316 break;
317
318 case ACPI_GPIO_TYPE_IO:
319 /*
320 * Byte 7-8: GPIO IO Flags
321 * [15:4]: 0 => Reserved
322 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
323 * [2]: 0 => Reserved
324 * [1:0]: IO Restriction
325 * 0 => IoRestrictionNone
326 * 1 => IoRestrictionInputOnly
327 * 2 => IoRestrictionOutputOnly
328 * 3 => IoRestrictionNoneAndPreserve
329 */
330 flags |= gpio->io_restrict & 3;
331 if (gpio->io_shared)
332 flags |= 1 << 3;
333 break;
334 }
335 acpigen_emit_word(flags);
336
337 /*
338 * Byte 9: Pin Configuration
339 * 0x01 => Default (no configuration applied)
340 * 0x02 => Pull-up
341 * 0x03 => Pull-down
342 * 0x04-0x7F => Reserved
343 * 0x80-0xff => Vendor defined
344 */
345 acpigen_emit_byte(gpio->pull);
346
347 /* Byte 10-11: Output Drive Strength in 1/100 mA */
348 acpigen_emit_word(gpio->output_drive_strength);
349
350 /* Byte 12-13: Debounce Timeout in 1/100 ms */
351 acpigen_emit_word(gpio->interrupt_debounce_timeout);
352
353 /* Byte 14-15: Pin Table Offset, relative to start */
354 pin_table_offset = acpi_device_write_zero_len();
355
356 /* Byte 16: Reserved */
357 acpigen_emit_byte(0);
358
359 /* Byte 17-18: Resource Source Name Offset, relative to start */
360 resource_offset = acpi_device_write_zero_len();
361
362 /* Byte 19-20: Vendor Data Offset, relative to start */
363 vendor_data_offset = acpi_device_write_zero_len();
364
365 /* Byte 21-22: Vendor Data Length */
366 acpigen_emit_word(0);
367
368 /* Fill in Pin Table Offset */
369 acpi_device_fill_from_len(pin_table_offset, start);
370
371 /* Pin Table, one word for each pin */
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700372 for (pin = 0; pin < gpio->pin_count; pin++) {
373 uint16_t acpi_pin = gpio->pins[pin];
Julius Wernercd49cce2019-03-05 16:53:33 -0800374#if CONFIG(GENERIC_GPIO_LIB)
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700375 acpi_pin = gpio_acpi_pin(acpi_pin);
376#endif
377 acpigen_emit_word(acpi_pin);
378 }
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700379
380 /* Fill in Resource Source Name Offset */
381 acpi_device_fill_from_len(resource_offset, start);
382
383 /* Resource Source Name String */
Julius Wernercd49cce2019-03-05 16:53:33 -0800384#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700385 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
386#else
387 acpigen_emit_string(gpio->resource);
388#endif
389
390 /* Fill in Vendor Data Offset */
391 acpi_device_fill_from_len(vendor_data_offset, start);
392
393 /* Fill in GPIO Descriptor Length (account for len word) */
394 acpi_device_fill_len(desc_length);
395}
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700396
397/* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
398void acpi_device_write_i2c(const struct acpi_i2c *i2c)
399{
400 void *desc_length, *type_length;
401
402 /* Byte 0: Descriptor Type */
403 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
404
405 /* Byte 1+2: Length (filled in later) */
406 desc_length = acpi_device_write_zero_len();
407
408 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200409 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700410
411 /* Byte 4: Resource Source Index is Reserved */
412 acpigen_emit_byte(0);
413
414 /* Byte 5: Serial Bus Type is I2C */
415 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
416
417 /*
418 * Byte 6: Flags
419 * [7:2]: 0 => Reserved
420 * [1]: 1 => ResourceConsumer
421 * [0]: 0 => ControllerInitiated
422 */
423 acpigen_emit_byte(1 << 1);
424
425 /*
426 * Byte 7-8: Type Specific Flags
427 * [15:1]: 0 => Reserved
428 * [0]: 0 => 7bit, 1 => 10bit
429 */
430 acpigen_emit_word(i2c->mode_10bit);
431
432 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200433 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700434
435 /* Byte 10-11: I2C Type Data Length */
436 type_length = acpi_device_write_zero_len();
437
438 /* Byte 12-15: I2C Bus Speed */
439 acpigen_emit_dword(i2c->speed);
440
441 /* Byte 16-17: I2C Slave Address */
442 acpigen_emit_word(i2c->address);
443
444 /* Fill in Type Data Length */
445 acpi_device_fill_len(type_length);
446
447 /* Byte 18+: ResourceSource */
448 acpigen_emit_string(i2c->resource);
449
450 /* Fill in I2C Descriptor Length */
451 acpi_device_fill_len(desc_length);
452}
Duncan Laurie70c86d92016-05-10 07:26:34 -0700453
454/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
455void acpi_device_write_spi(const struct acpi_spi *spi)
456{
457 void *desc_length, *type_length;
458 uint16_t flags = 0;
459
460 /* Byte 0: Descriptor Type */
461 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
462
463 /* Byte 1+2: Length (filled in later) */
464 desc_length = acpi_device_write_zero_len();
465
466 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200467 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700468
469 /* Byte 4: Resource Source Index is Reserved */
470 acpigen_emit_byte(0);
471
472 /* Byte 5: Serial Bus Type is SPI */
473 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
474
475 /*
476 * Byte 6: Flags
477 * [7:2]: 0 => Reserved
478 * [1]: 1 => ResourceConsumer
479 * [0]: 0 => ControllerInitiated
480 */
481 acpigen_emit_byte(1 << 1);
482
483 /*
484 * Byte 7-8: Type Specific Flags
485 * [15:2]: 0 => Reserved
486 * [1]: 0 => ActiveLow, 1 => ActiveHigh
487 * [0]: 0 => FourWire, 1 => ThreeWire
488 */
489 if (spi->wire_mode == SPI_3_WIRE_MODE)
490 flags |= 1 << 0;
491 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
492 flags |= 1 << 1;
493 acpigen_emit_word(flags);
494
495 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200496 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700497
498 /* Byte 10-11: SPI Type Data Length */
499 type_length = acpi_device_write_zero_len();
500
501 /* Byte 12-15: Connection Speed */
502 acpigen_emit_dword(spi->speed);
503
504 /* Byte 16: Data Bit Length */
505 acpigen_emit_byte(spi->data_bit_length);
506
507 /* Byte 17: Clock Phase */
508 acpigen_emit_byte(spi->clock_phase);
509
510 /* Byte 18: Clock Polarity */
511 acpigen_emit_byte(spi->clock_polarity);
512
513 /* Byte 19-20: Device Selection */
514 acpigen_emit_word(spi->device_select);
515
516 /* Fill in Type Data Length */
517 acpi_device_fill_len(type_length);
518
519 /* Byte 21+: ResourceSource String */
520 acpigen_emit_string(spi->resource);
521
522 /* Fill in SPI Descriptor Length */
523 acpi_device_fill_len(desc_length);
524}
Duncan Laurie559e9472016-05-10 13:18:17 -0700525
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800526/* PowerResource() with Enable and/or Reset control */
Shelley Chena0603392018-04-26 13:52:30 -0700527void acpi_device_add_power_res(const struct acpi_power_res_params *params)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800528{
Lee Leahy0b5678f2017-03-16 16:01:40 -0700529 static const char *power_res_dev_states[] = { "_PR0", "_PR3" };
Edward O'Callaghan7e262552020-01-23 10:32:33 +1100530 unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0;
531 unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0;
532 unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0;
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800533
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700534 if (!reset_gpio && !enable_gpio && !stop_gpio)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800535 return;
536
537 /* PowerResource (PRIC, 0, 0) */
538 acpigen_write_power_res("PRIC", 0, 0, power_res_dev_states,
539 ARRAY_SIZE(power_res_dev_states));
540
541 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
542 acpigen_write_STA(0x1);
543
544 /* Method (_ON, 0, Serialized) */
545 acpigen_write_method_serialized("_ON", 0);
546 if (reset_gpio)
Shelley Chena0603392018-04-26 13:52:30 -0700547 acpigen_enable_tx_gpio(params->reset_gpio);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800548 if (enable_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700549 acpigen_enable_tx_gpio(params->enable_gpio);
550 if (params->enable_delay_ms)
551 acpigen_write_sleep(params->enable_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800552 }
553 if (reset_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700554 acpigen_disable_tx_gpio(params->reset_gpio);
555 if (params->reset_delay_ms)
556 acpigen_write_sleep(params->reset_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800557 }
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700558 if (stop_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700559 acpigen_disable_tx_gpio(params->stop_gpio);
560 if (params->stop_delay_ms)
561 acpigen_write_sleep(params->stop_delay_ms);
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700562 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800563 acpigen_pop_len(); /* _ON method */
564
565 /* Method (_OFF, 0, Serialized) */
566 acpigen_write_method_serialized("_OFF", 0);
Shelley Chena0603392018-04-26 13:52:30 -0700567 if (stop_gpio) {
568 acpigen_enable_tx_gpio(params->stop_gpio);
569 if (params->stop_off_delay_ms)
570 acpigen_write_sleep(params->stop_off_delay_ms);
571 }
572 if (reset_gpio) {
573 acpigen_enable_tx_gpio(params->reset_gpio);
574 if (params->reset_off_delay_ms)
575 acpigen_write_sleep(params->reset_off_delay_ms);
576 }
577 if (enable_gpio) {
578 acpigen_disable_tx_gpio(params->enable_gpio);
579 if (params->enable_off_delay_ms)
580 acpigen_write_sleep(params->enable_off_delay_ms);
581 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800582 acpigen_pop_len(); /* _OFF method */
583
584 acpigen_pop_len(); /* PowerResource PRIC */
585}
586
Duncan Laurieffc99902016-07-02 19:56:06 -0700587static void acpi_dp_write_array(const struct acpi_dp *array);
588static void acpi_dp_write_value(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700589{
590 switch (prop->type) {
591 case ACPI_DP_TYPE_INTEGER:
592 acpigen_write_integer(prop->integer);
593 break;
594 case ACPI_DP_TYPE_STRING:
Harsha Priya3a96ac42016-07-15 17:31:43 -0700595 case ACPI_DP_TYPE_CHILD:
Duncan Laurie559e9472016-05-10 13:18:17 -0700596 acpigen_write_string(prop->string);
597 break;
598 case ACPI_DP_TYPE_REFERENCE:
599 acpigen_emit_namestring(prop->string);
600 break;
Duncan Laurieffc99902016-07-02 19:56:06 -0700601 case ACPI_DP_TYPE_ARRAY:
602 acpi_dp_write_array(prop->array);
603 break;
604 default:
605 break;
Duncan Laurie559e9472016-05-10 13:18:17 -0700606 }
607}
608
Duncan Laurieffc99902016-07-02 19:56:06 -0700609/* Package (2) { "prop->name", VALUE } */
610static void acpi_dp_write_property(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700611{
612 acpigen_write_package(2);
Duncan Laurieffc99902016-07-02 19:56:06 -0700613 acpigen_write_string(prop->name);
Duncan Laurie559e9472016-05-10 13:18:17 -0700614 acpi_dp_write_value(prop);
615 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700616}
617
618/* Write array of Device Properties */
Duncan Laurieffc99902016-07-02 19:56:06 -0700619static void acpi_dp_write_array(const struct acpi_dp *array)
Duncan Laurie559e9472016-05-10 13:18:17 -0700620{
Duncan Laurieffc99902016-07-02 19:56:06 -0700621 const struct acpi_dp *dp;
622 char *pkg_count;
623
624 /* Package element count determined as it is populated */
625 pkg_count = acpigen_write_package(0);
626
Furquan Shaikh35c01bc2016-10-03 23:30:14 -0700627 /*
628 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
629 * DP_TYPE_TABLE does not have a value to be written. Thus, start
630 * the loop from next type in the array.
631 */
632 for (dp = array->next; dp; dp = dp->next) {
Duncan Laurieffc99902016-07-02 19:56:06 -0700633 acpi_dp_write_value(dp);
634 (*pkg_count)++;
635 }
636
Duncan Laurie559e9472016-05-10 13:18:17 -0700637 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700638}
639
Duncan Laurieffc99902016-07-02 19:56:06 -0700640static void acpi_dp_free(struct acpi_dp *dp)
Duncan Laurie559e9472016-05-10 13:18:17 -0700641{
Duncan Laurieffc99902016-07-02 19:56:06 -0700642 while (dp) {
643 struct acpi_dp *p = dp->next;
644
645 switch (dp->type) {
646 case ACPI_DP_TYPE_CHILD:
647 acpi_dp_free(dp->child);
648 break;
649 case ACPI_DP_TYPE_ARRAY:
650 acpi_dp_free(dp->array);
651 break;
652 default:
653 break;
654 }
655
656 free(dp);
657 dp = p;
658 }
Duncan Laurie559e9472016-05-10 13:18:17 -0700659}
660
Duncan Laurieffc99902016-07-02 19:56:06 -0700661void acpi_dp_write(struct acpi_dp *table)
Duncan Laurie559e9472016-05-10 13:18:17 -0700662{
Duncan Laurieffc99902016-07-02 19:56:06 -0700663 struct acpi_dp *dp, *prop;
Matt Delco08258882019-01-30 11:16:08 -0800664 char *dp_count, *prop_count = NULL;
Duncan Laurieffc99902016-07-02 19:56:06 -0700665 int child_count = 0;
666
667 if (!table || table->type != ACPI_DP_TYPE_TABLE)
668 return;
669
670 /* Name (name) */
671 acpigen_write_name(table->name);
672
673 /* Device Property list starts with the next entry */
674 prop = table->next;
675
Matt Delco08258882019-01-30 11:16:08 -0800676 /* Package (DP), default to assuming no properties or children */
677 dp_count = acpigen_write_package(0);
Duncan Laurieffc99902016-07-02 19:56:06 -0700678
679 /* Print base properties */
680 for (dp = prop; dp; dp = dp->next) {
681 if (dp->type == ACPI_DP_TYPE_CHILD) {
682 child_count++;
683 } else {
Matt Delco08258882019-01-30 11:16:08 -0800684 /*
685 * The UUID and package is only added when
686 * we come across the first property. This
687 * is to avoid creating a zero-length package
688 * in situations where there are only children.
689 */
690 if (!prop_count) {
691 *dp_count += 2;
692 /* ToUUID (ACPI_DP_UUID) */
693 acpigen_write_uuid(ACPI_DP_UUID);
694 /*
695 * Package (PROP), element count determined as
696 * it is populated
697 */
698 prop_count = acpigen_write_package(0);
699 }
Duncan Laurieffc99902016-07-02 19:56:06 -0700700 (*prop_count)++;
701 acpi_dp_write_property(dp);
702 }
703 }
Matt Delco08258882019-01-30 11:16:08 -0800704 if (prop_count) {
705 /* Package (PROP) length, if a package was written */
706 acpigen_pop_len();
707 }
Duncan Laurieffc99902016-07-02 19:56:06 -0700708
709 if (child_count) {
Matt Delco08258882019-01-30 11:16:08 -0800710 /* Update DP package count to 2 or 4 */
711 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700712 /* ToUUID (ACPI_DP_CHILD_UUID) */
713 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
714
715 /* Print child pointer properties */
716 acpigen_write_package(child_count);
717
718 for (dp = prop; dp; dp = dp->next)
719 if (dp->type == ACPI_DP_TYPE_CHILD)
720 acpi_dp_write_property(dp);
Matt Delco08258882019-01-30 11:16:08 -0800721 /* Package (CHILD) length */
Duncan Laurieffc99902016-07-02 19:56:06 -0700722 acpigen_pop_len();
723 }
724
725 /* Package (DP) length */
726 acpigen_pop_len();
727
728 /* Recursively parse children into separate tables */
729 for (dp = prop; dp; dp = dp->next)
730 if (dp->type == ACPI_DP_TYPE_CHILD)
731 acpi_dp_write(dp->child);
732
733 /* Clean up */
734 acpi_dp_free(table);
735}
736
737static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
738 const char *name)
739{
740 struct acpi_dp *new;
741
742 new = malloc(sizeof(struct acpi_dp));
743 if (!new)
744 return NULL;
745
746 memset(new, 0, sizeof(*new));
747 new->type = type;
748 new->name = name;
749
750 if (dp) {
751 /* Add to end of property list */
752 while (dp->next)
753 dp = dp->next;
754 dp->next = new;
755 }
756
757 return new;
758}
759
760struct acpi_dp *acpi_dp_new_table(const char *name)
761{
762 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
763}
764
Duncan Laurieb3023b62017-08-29 08:26:50 -0700765size_t acpi_dp_add_property_list(struct acpi_dp *dp,
766 const struct acpi_dp *property_list,
767 size_t property_count)
768{
769 const struct acpi_dp *prop;
770 size_t i, properties_added = 0;
771
Jacob Garberc30e5902019-05-23 14:34:58 -0600772 if (!dp || !property_list)
773 return 0;
774
Duncan Laurieb3023b62017-08-29 08:26:50 -0700775 for (i = 0; i < property_count; i++) {
776 prop = &property_list[i];
777
778 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
779 continue;
780
781 switch (prop->type) {
782 case ACPI_DP_TYPE_INTEGER:
783 acpi_dp_add_integer(dp, prop->name, prop->integer);
784 break;
785 case ACPI_DP_TYPE_STRING:
786 acpi_dp_add_string(dp, prop->name, prop->string);
787 break;
788 case ACPI_DP_TYPE_REFERENCE:
789 acpi_dp_add_reference(dp, prop->name, prop->string);
790 break;
791 case ACPI_DP_TYPE_ARRAY:
792 acpi_dp_add_array(dp, prop->array);
793 break;
794 case ACPI_DP_TYPE_CHILD:
795 acpi_dp_add_child(dp, prop->name, prop->child);
796 break;
797 default:
798 continue;
799 }
800
801 ++properties_added;
802 }
803
804 return properties_added;
805}
806
Duncan Laurieffc99902016-07-02 19:56:06 -0700807struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
808 uint64_t value)
809{
Jacob Garberc30e5902019-05-23 14:34:58 -0600810 if (!dp)
811 return NULL;
812
Duncan Laurieffc99902016-07-02 19:56:06 -0700813 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
814
815 if (new)
816 new->integer = value;
817
818 return new;
819}
820
821struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
822 const char *string)
823{
Jacob Garberc30e5902019-05-23 14:34:58 -0600824 if (!dp)
825 return NULL;
826
Duncan Laurieffc99902016-07-02 19:56:06 -0700827 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
828
829 if (new)
830 new->string = string;
831
832 return new;
833}
834
835struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
836 const char *reference)
837{
Jacob Garberc30e5902019-05-23 14:34:58 -0600838 if (!dp)
839 return NULL;
840
Duncan Laurieffc99902016-07-02 19:56:06 -0700841 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
842
843 if (new)
844 new->string = reference;
845
846 return new;
847}
848
849struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
850 struct acpi_dp *child)
851{
852 struct acpi_dp *new;
853
Jacob Garberc30e5902019-05-23 14:34:58 -0600854 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -0700855 return NULL;
856
857 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
858 if (new) {
859 new->child = child;
860 new->string = child->name;
861 }
862
863 return new;
864}
865
866struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
867{
868 struct acpi_dp *new;
869
Jacob Garberc30e5902019-05-23 14:34:58 -0600870 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -0700871 return NULL;
872
873 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
874 if (new)
875 new->array = array;
876
877 return new;
878}
879
880struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
881 uint64_t *array, int len)
882{
883 struct acpi_dp *dp_array;
884 int i;
885
Jacob Garberc30e5902019-05-23 14:34:58 -0600886 if (!dp || len <= 0)
Duncan Laurieffc99902016-07-02 19:56:06 -0700887 return NULL;
888
889 dp_array = acpi_dp_new_table(name);
890 if (!dp_array)
891 return NULL;
892
893 for (i = 0; i < len; i++)
894 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
895 break;
896
897 acpi_dp_add_array(dp, dp_array);
898
899 return dp_array;
900}
901
902struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
903 const char *ref, int index, int pin,
904 int active_low)
905{
Jacob Garberc30e5902019-05-23 14:34:58 -0600906 if (!dp)
907 return NULL;
908
Duncan Laurieffc99902016-07-02 19:56:06 -0700909 struct acpi_dp *gpio = acpi_dp_new_table(name);
910
911 if (!gpio)
912 return NULL;
913
914 /* The device that has _CRS containing GpioIO()/GpioInt() */
915 acpi_dp_add_reference(gpio, NULL, ref);
916
917 /* Index of the GPIO resource in _CRS starting from zero */
918 acpi_dp_add_integer(gpio, NULL, index);
919
920 /* Pin in the GPIO resource, typically zero */
921 acpi_dp_add_integer(gpio, NULL, pin);
922
923 /* Set if pin is active low */
924 acpi_dp_add_integer(gpio, NULL, active_low);
925
926 acpi_dp_add_array(dp, gpio);
927
928 return gpio;
Duncan Laurie559e9472016-05-10 13:18:17 -0700929}