1 // SPDX-License-Identifier: GPL-2.0
3 * Generation of tables for particular device types
5 * Copyright 2019 Google LLC
6 * Mostly taken from coreboot file of the same name
13 #include <acpi/acpi_device.h>
14 #include <acpi/acpigen.h>
15 #include <asm-generic/gpio.h>
19 * acpi_device_path_fill() - Find the root device and build a path from there
21 * This recursively reaches back to the root device and progressively adds path
22 * elements until the device is reached.
24 * @dev: Device to return path of
25 * @buf: Buffer to hold the path
26 * @buf_len: Length of buffer
27 * @cur: Current position in the buffer
28 * @return new position in buffer after adding @dev, or -ve on error
30 static int acpi_device_path_fill(const struct udevice *dev, char *buf,
31 size_t buf_len, int cur)
33 char name[ACPI_NAME_MAX];
37 ret = acpi_get_name(dev, name);
42 * Make sure this name segment will fit, including the path segment
43 * separator and possible NULL terminator, if this is the last segment.
45 if (cur + strlen(name) + 2 > buf_len)
48 /* Walk up the tree to the root device */
49 if (dev_get_parent(dev)) {
50 next = acpi_device_path_fill(dev_get_parent(dev), buf, buf_len,
56 /* Fill in the path from the root device */
57 next += snprintf(buf + next, buf_len - next, "%s%s",
58 dev_get_parent(dev) && *name ? "." : "", name);
63 int acpi_device_path(const struct udevice *dev, char *buf, int maxlen)
67 ret = acpi_device_path_fill(dev, buf, maxlen, 0);
74 int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen)
78 if (!dev_get_parent(dev))
79 return log_msg_ret("noparent", -EINVAL);
81 ret = acpi_device_path_fill(dev_get_parent(dev), scope, maxlen, 0);
83 return log_msg_ret("fill", ret);
88 enum acpi_dev_status acpi_device_status(const struct udevice *dev)
90 return ACPI_DSTATUS_ALL_ON;
94 * largeres_write_len_f() - Write a placeholder word value
96 * Write a forward length for a large resource (2 bytes)
98 * @return pointer to the zero word (for fixing up later)
100 static void *largeres_write_len_f(struct acpi_ctx *ctx)
102 u8 *p = acpigen_get_current(ctx);
104 acpigen_emit_word(ctx, 0);
110 * largeres_fill_from_len() - Fill in a length value
112 * This calculated the number of bytes since the provided @start and writes it
113 * to @ptr, which was previous returned by largeres_write_len_f().
115 * @ptr: Word to update
116 * @start: Start address to count from to calculated the length
118 static void largeres_fill_from_len(struct acpi_ctx *ctx, char *ptr, u8 *start)
120 u16 len = acpigen_get_current(ctx) - start;
123 ptr[1] = (len >> 8) & 0xff;
127 * largeres_fill_len() - Fill in a length value, excluding the length itself
129 * Fill in the length field with the value calculated from after the 16bit
130 * field to acpigen current. This is useful since the length value does not
131 * include the length field itself.
133 * This calls acpi_device_largeres_fill_len() passing @ptr + 2 as @start
135 * @ptr: Word to update.
137 static void largeres_fill_len(struct acpi_ctx *ctx, void *ptr)
139 largeres_fill_from_len(ctx, ptr, ptr + sizeof(u16));
142 /* ACPI 6.3 section 6.4.3.6: Extended Interrupt Descriptor */
143 static int acpi_device_write_interrupt(struct acpi_ctx *ctx,
144 const struct acpi_irq *irq)
152 /* This is supported by GpioInt() but not Interrupt() */
153 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
156 /* Byte 0: Descriptor Type */
157 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_INTERRUPT);
159 /* Byte 1-2: Length (filled in later) */
160 desc_length = largeres_write_len_f(ctx);
165 * [4]: Wake (0=NO_WAKE 1=WAKE)
166 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
167 * [2]: Polarity (0=HIGH 1=LOW)
168 * [1]: Mode (0=LEVEL 1=EDGE)
169 * [0]: Resource (0=PRODUCER 1=CONSUMER)
171 flags = BIT(0); /* ResourceConsumer */
172 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
174 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
176 if (irq->shared == ACPI_IRQ_SHARED)
178 if (irq->wake == ACPI_IRQ_WAKE)
180 acpigen_emit_byte(ctx, flags);
182 /* Byte 4: Interrupt Table Entry Count */
183 acpigen_emit_byte(ctx, 1);
185 /* Byte 5-8: Interrupt Number */
186 acpigen_emit_dword(ctx, irq->pin);
188 /* Fill in Descriptor Length (account for len word) */
189 largeres_fill_len(ctx, desc_length);
194 int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
195 const struct irq *req_irq)
200 ret = irq_get_acpi(req_irq, &irq);
202 return log_msg_ret("get", ret);
203 ret = acpi_device_write_interrupt(ctx, &irq);
205 return log_msg_ret("write", ret);
210 /* ACPI 6.3 section 6.4.3.8.1 - GPIO Interrupt or I/O */
211 int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio)
213 void *start, *desc_length;
214 void *pin_table_offset, *vendor_data_offset, *resource_offset;
218 if (gpio->type > ACPI_GPIO_TYPE_IO)
221 start = acpigen_get_current(ctx);
223 /* Byte 0: Descriptor Type */
224 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_GPIO);
226 /* Byte 1-2: Length (fill in later) */
227 desc_length = largeres_write_len_f(ctx);
229 /* Byte 3: Revision ID */
230 acpigen_emit_byte(ctx, ACPI_GPIO_REVISION_ID);
232 /* Byte 4: GpioIo or GpioInt */
233 acpigen_emit_byte(ctx, gpio->type);
236 * Byte 5-6: General Flags
237 * [15:1]: 0 => Reserved
238 * [0]: 1 => ResourceConsumer
240 acpigen_emit_word(ctx, 1 << 0);
242 switch (gpio->type) {
243 case ACPI_GPIO_TYPE_INTERRUPT:
245 * Byte 7-8: GPIO Interrupt Flags
246 * [15:5]: 0 => Reserved
247 * [4]: Wake (0=NO_WAKE 1=WAKE)
248 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
249 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
250 * [0]: Mode (0=LEVEL 1=EDGE)
252 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
254 if (gpio->irq.shared == ACPI_IRQ_SHARED)
256 if (gpio->irq.wake == ACPI_IRQ_WAKE)
259 switch (gpio->irq.polarity) {
260 case ACPI_IRQ_ACTIVE_HIGH:
263 case ACPI_IRQ_ACTIVE_LOW:
266 case ACPI_IRQ_ACTIVE_BOTH:
272 case ACPI_GPIO_TYPE_IO:
274 * Byte 7-8: GPIO IO Flags
275 * [15:4]: 0 => Reserved
276 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
278 * [1:0]: IO Restriction
279 * 0 => IoRestrictionNone
280 * 1 => IoRestrictionInputOnly
281 * 2 => IoRestrictionOutputOnly
282 * 3 => IoRestrictionNoneAndPreserve
284 flags |= gpio->io_restrict & 3;
289 acpigen_emit_word(ctx, flags);
292 * Byte 9: Pin Configuration
293 * 0x01 => Default (no configuration applied)
296 * 0x04-0x7F => Reserved
297 * 0x80-0xff => Vendor defined
299 acpigen_emit_byte(ctx, gpio->pull);
301 /* Byte 10-11: Output Drive Strength in 1/100 mA */
302 acpigen_emit_word(ctx, gpio->output_drive_strength);
304 /* Byte 12-13: Debounce Timeout in 1/100 ms */
305 acpigen_emit_word(ctx, gpio->interrupt_debounce_timeout);
307 /* Byte 14-15: Pin Table Offset, relative to start */
308 pin_table_offset = largeres_write_len_f(ctx);
310 /* Byte 16: Reserved */
311 acpigen_emit_byte(ctx, 0);
313 /* Byte 17-18: Resource Source Name Offset, relative to start */
314 resource_offset = largeres_write_len_f(ctx);
316 /* Byte 19-20: Vendor Data Offset, relative to start */
317 vendor_data_offset = largeres_write_len_f(ctx);
319 /* Byte 21-22: Vendor Data Length */
320 acpigen_emit_word(ctx, 0);
322 /* Fill in Pin Table Offset */
323 largeres_fill_from_len(ctx, pin_table_offset, start);
325 /* Pin Table, one word for each pin */
326 for (pin = 0; pin < gpio->pin_count; pin++)
327 acpigen_emit_word(ctx, gpio->pins[pin]);
329 /* Fill in Resource Source Name Offset */
330 largeres_fill_from_len(ctx, resource_offset, start);
332 /* Resource Source Name String */
333 acpigen_emit_string(ctx, gpio->resource);
335 /* Fill in Vendor Data Offset */
336 largeres_fill_from_len(ctx, vendor_data_offset, start);
338 /* Fill in GPIO Descriptor Length (account for len word) */
339 largeres_fill_len(ctx, desc_length);
341 return gpio->pins[0];
344 int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
345 const struct gpio_desc *desc)
347 struct acpi_gpio gpio;
350 ret = gpio_get_acpi(desc, &gpio);
352 return log_msg_ret("desc", ret);
353 ret = acpi_device_write_gpio(ctx, &gpio);
355 return log_msg_ret("gpio", ret);
360 int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
361 struct udevice *dev, const char *prop)
367 ret = irq_get_by_index(dev, 0, &req_irq);
369 ret = acpi_device_write_interrupt_irq(ctx, &req_irq);
371 return log_msg_ret("irq", ret);
374 struct gpio_desc req_gpio;
376 ret = gpio_request_by_name(dev, prop, 0, &req_gpio,
379 return log_msg_ret("no gpio", ret);
380 ret = acpi_device_write_gpio_desc(ctx, &req_gpio);
382 return log_msg_ret("gpio", ret);
389 /* ACPI 6.3 section 6.4.3.8.2.1 - I2cSerialBus() */
390 static void acpi_device_write_i2c(struct acpi_ctx *ctx,
391 const struct acpi_i2c *i2c)
393 void *desc_length, *type_length;
395 /* Byte 0: Descriptor Type */
396 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
398 /* Byte 1+2: Length (filled in later) */
399 desc_length = largeres_write_len_f(ctx);
401 /* Byte 3: Revision ID */
402 acpigen_emit_byte(ctx, ACPI_I2C_SERIAL_BUS_REVISION_ID);
404 /* Byte 4: Resource Source Index is Reserved */
405 acpigen_emit_byte(ctx, 0);
407 /* Byte 5: Serial Bus Type is I2C */
408 acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_I2C);
412 * [7:2]: 0 => Reserved
413 * [1]: 1 => ResourceConsumer
414 * [0]: 0 => ControllerInitiated
416 acpigen_emit_byte(ctx, 1 << 1);
419 * Byte 7-8: Type Specific Flags
420 * [15:1]: 0 => Reserved
421 * [0]: 0 => 7bit, 1 => 10bit
423 acpigen_emit_word(ctx, i2c->mode_10bit);
425 /* Byte 9: Type Specific Revision ID */
426 acpigen_emit_byte(ctx, ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
428 /* Byte 10-11: I2C Type Data Length */
429 type_length = largeres_write_len_f(ctx);
431 /* Byte 12-15: I2C Bus Speed */
432 acpigen_emit_dword(ctx, i2c->speed);
434 /* Byte 16-17: I2C Slave Address */
435 acpigen_emit_word(ctx, i2c->address);
437 /* Fill in Type Data Length */
438 largeres_fill_len(ctx, type_length);
440 /* Byte 18+: ResourceSource */
441 acpigen_emit_string(ctx, i2c->resource);
443 /* Fill in I2C Descriptor Length */
444 largeres_fill_len(ctx, desc_length);
448 * acpi_device_set_i2c() - Set up an ACPI I2C struct from a device
450 * The value of @scope is not copied, but only referenced. This implies the
451 * caller has to ensure it stays valid for the lifetime of @i2c.
453 * @dev: I2C device to convert
454 * @i2c: Place to put the new structure
455 * @scope: Scope of the I2C device (this is the controller path)
456 * @return chip address of device
458 static int acpi_device_set_i2c(const struct udevice *dev, struct acpi_i2c *i2c,
461 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
462 struct udevice *bus = dev_get_parent(dev);
464 memset(i2c, '\0', sizeof(*i2c));
465 i2c->address = chip->chip_addr;
469 * i2c_bus->speed_hz is set if this device is probed, but if not we
470 * must use the device tree
472 i2c->speed = dev_read_u32_default(bus, "clock-frequency",
473 I2C_SPEED_STANDARD_RATE);
474 i2c->resource = scope;
479 int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev)
481 char scope[ACPI_PATH_MAX];
485 ret = acpi_device_scope(dev, scope, sizeof(scope));
487 return log_msg_ret("scope", ret);
488 ret = acpi_device_set_i2c(dev, &i2c, scope);
490 return log_msg_ret("set", ret);
491 acpi_device_write_i2c(ctx, &i2c);
497 /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
498 static void acpi_device_write_spi(struct acpi_ctx *ctx, const struct acpi_spi *spi)
500 void *desc_length, *type_length;
503 /* Byte 0: Descriptor Type */
504 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
506 /* Byte 1+2: Length (filled in later) */
507 desc_length = largeres_write_len_f(ctx);
509 /* Byte 3: Revision ID */
510 acpigen_emit_byte(ctx, ACPI_SPI_SERIAL_BUS_REVISION_ID);
512 /* Byte 4: Resource Source Index is Reserved */
513 acpigen_emit_byte(ctx, 0);
515 /* Byte 5: Serial Bus Type is SPI */
516 acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_SPI);
520 * [7:2]: 0 => Reserved
521 * [1]: 1 => ResourceConsumer
522 * [0]: 0 => ControllerInitiated
524 acpigen_emit_byte(ctx, BIT(1));
527 * Byte 7-8: Type Specific Flags
528 * [15:2]: 0 => Reserveda
529 * [1]: 0 => ActiveLow, 1 => ActiveHigh
530 * [0]: 0 => FourWire, 1 => ThreeWire
532 if (spi->wire_mode == SPI_3_WIRE_MODE)
534 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
536 acpigen_emit_word(ctx, flags);
538 /* Byte 9: Type Specific Revision ID */
539 acpigen_emit_byte(ctx, ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
541 /* Byte 10-11: SPI Type Data Length */
542 type_length = largeres_write_len_f(ctx);
544 /* Byte 12-15: Connection Speed */
545 acpigen_emit_dword(ctx, spi->speed);
547 /* Byte 16: Data Bit Length */
548 acpigen_emit_byte(ctx, spi->data_bit_length);
550 /* Byte 17: Clock Phase */
551 acpigen_emit_byte(ctx, spi->clock_phase);
553 /* Byte 18: Clock Polarity */
554 acpigen_emit_byte(ctx, spi->clock_polarity);
556 /* Byte 19-20: Device Selection */
557 acpigen_emit_word(ctx, spi->device_select);
559 /* Fill in Type Data Length */
560 largeres_fill_len(ctx, type_length);
562 /* Byte 21+: ResourceSource String */
563 acpigen_emit_string(ctx, spi->resource);
565 /* Fill in SPI Descriptor Length */
566 largeres_fill_len(ctx, desc_length);
570 * acpi_device_set_spi() - Set up an ACPI SPI struct from a device
572 * The value of @scope is not copied, but only referenced. This implies the
573 * caller has to ensure it stays valid for the lifetime of @spi.
575 * @dev: SPI device to convert
576 * @spi: Place to put the new structure
577 * @scope: Scope of the SPI device (this is the controller path)
580 static int acpi_device_set_spi(const struct udevice *dev, struct acpi_spi *spi,
583 struct dm_spi_slave_platdata *plat;
584 struct spi_slave *slave = dev_get_parent_priv(dev);
586 plat = dev_get_parent_platdata(slave->dev);
587 memset(spi, '\0', sizeof(*spi));
588 spi->device_select = plat->cs;
589 spi->device_select_polarity = SPI_POLARITY_LOW;
590 spi->wire_mode = SPI_4_WIRE_MODE;
591 spi->speed = plat->max_hz;
592 spi->data_bit_length = slave->wordlen;
593 spi->clock_phase = plat->mode & SPI_CPHA ?
594 SPI_CLOCK_PHASE_SECOND : SPI_CLOCK_PHASE_FIRST;
595 spi->clock_polarity = plat->mode & SPI_CPOL ?
596 SPI_POLARITY_HIGH : SPI_POLARITY_LOW;
597 spi->resource = scope;
602 int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev)
604 char scope[ACPI_PATH_MAX];
608 ret = acpi_device_scope(dev, scope, sizeof(scope));
610 return log_msg_ret("scope", ret);
611 ret = acpi_device_set_spi(dev, &spi, scope);
613 return log_msg_ret("set", ret);
614 acpi_device_write_spi(ctx, &spi);
618 #endif /* CONFIG_SPI */