acpi: Support writing a string
[platform/kernel/u-boot.git] / lib / acpi / acpi_device.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generation of tables for particular device types
4  *
5  * Copyright 2019 Google LLC
6  * Mostly taken from coreboot file of the same name
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <irq.h>
12 #include <log.h>
13 #include <acpi/acpi_device.h>
14 #include <acpi/acpigen.h>
15 #include <asm-generic/gpio.h>
16 #include <dm/acpi.h>
17
18 /**
19  * acpi_device_path_fill() - Find the root device and build a path from there
20  *
21  * This recursively reaches back to the root device and progressively adds path
22  * elements until the device is reached.
23  *
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
29  */
30 static int acpi_device_path_fill(const struct udevice *dev, char *buf,
31                                  size_t buf_len, int cur)
32 {
33         char name[ACPI_NAME_MAX];
34         int next = 0;
35         int ret;
36
37         ret = acpi_get_name(dev, name);
38         if (ret)
39                 return ret;
40
41         /*
42          * Make sure this name segment will fit, including the path segment
43          * separator and possible NULL terminator, if this is the last segment.
44          */
45         if (cur + strlen(name) + 2 > buf_len)
46                 return -ENOSPC;
47
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,
51                                              cur);
52                 if (next < 0)
53                         return next;
54         }
55
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);
59
60         return next;
61 }
62
63 int acpi_device_path(const struct udevice *dev, char *buf, int maxlen)
64 {
65         int ret;
66
67         ret = acpi_device_path_fill(dev, buf, maxlen, 0);
68         if (ret < 0)
69                 return ret;
70
71         return 0;
72 }
73
74 int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen)
75 {
76         int ret;
77
78         if (!dev_get_parent(dev))
79                 return log_msg_ret("noparent", -EINVAL);
80
81         ret = acpi_device_path_fill(dev_get_parent(dev), scope, maxlen, 0);
82         if (ret < 0)
83                 return log_msg_ret("fill", ret);
84
85         return 0;
86 }
87
88 enum acpi_dev_status acpi_device_status(const struct udevice *dev)
89 {
90         return ACPI_DSTATUS_ALL_ON;
91 }
92
93 /**
94  * largeres_write_len_f() - Write a placeholder word value
95  *
96  * Write a forward length for a large resource (2 bytes)
97  *
98  * @return pointer to the zero word (for fixing up later)
99  */
100 static void *largeres_write_len_f(struct acpi_ctx *ctx)
101 {
102         u8 *p = acpigen_get_current(ctx);
103
104         acpigen_emit_word(ctx, 0);
105
106         return p;
107 }
108
109 /**
110  * largeres_fill_from_len() - Fill in a length value
111  *
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().
114  *
115  * @ptr: Word to update
116  * @start: Start address to count from to calculated the length
117  */
118 static void largeres_fill_from_len(struct acpi_ctx *ctx, char *ptr, u8 *start)
119 {
120         u16 len = acpigen_get_current(ctx) - start;
121
122         ptr[0] = len & 0xff;
123         ptr[1] = (len >> 8) & 0xff;
124 }
125
126 /**
127  * largeres_fill_len() - Fill in a length value, excluding the length itself
128  *
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.
132  *
133  * This calls acpi_device_largeres_fill_len() passing @ptr + 2 as @start
134  *
135  * @ptr: Word to update.
136  */
137 static void largeres_fill_len(struct acpi_ctx *ctx, void *ptr)
138 {
139         largeres_fill_from_len(ctx, ptr, ptr + sizeof(u16));
140 }
141
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)
145 {
146         void *desc_length;
147         u8 flags;
148
149         if (!irq->pin)
150                 return -ENOENT;
151
152         /* This is supported by GpioInt() but not Interrupt() */
153         if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
154                 return -EINVAL;
155
156         /* Byte 0: Descriptor Type */
157         acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_INTERRUPT);
158
159         /* Byte 1-2: Length (filled in later) */
160         desc_length = largeres_write_len_f(ctx);
161
162         /*
163          * Byte 3: Flags
164          *  [7:5]: Reserved
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)
170          */
171         flags = BIT(0); /* ResourceConsumer */
172         if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
173                 flags |= BIT(1);
174         if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
175                 flags |= BIT(2);
176         if (irq->shared == ACPI_IRQ_SHARED)
177                 flags |= BIT(3);
178         if (irq->wake == ACPI_IRQ_WAKE)
179                 flags |= BIT(4);
180         acpigen_emit_byte(ctx, flags);
181
182         /* Byte 4: Interrupt Table Entry Count */
183         acpigen_emit_byte(ctx, 1);
184
185         /* Byte 5-8: Interrupt Number */
186         acpigen_emit_dword(ctx, irq->pin);
187
188         /* Fill in Descriptor Length (account for len word) */
189         largeres_fill_len(ctx, desc_length);
190
191         return 0;
192 }
193
194 int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
195                                     const struct irq *req_irq)
196 {
197         struct acpi_irq irq;
198         int ret;
199
200         ret = irq_get_acpi(req_irq, &irq);
201         if (ret)
202                 return log_msg_ret("get", ret);
203         ret = acpi_device_write_interrupt(ctx, &irq);
204         if (ret)
205                 return log_msg_ret("write", ret);
206
207         return irq.pin;
208 }
209
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)
212 {
213         void *start, *desc_length;
214         void *pin_table_offset, *vendor_data_offset, *resource_offset;
215         u16 flags = 0;
216         int pin;
217
218         if (gpio->type > ACPI_GPIO_TYPE_IO)
219                 return -EINVAL;
220
221         start = acpigen_get_current(ctx);
222
223         /* Byte 0: Descriptor Type */
224         acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_GPIO);
225
226         /* Byte 1-2: Length (fill in later) */
227         desc_length = largeres_write_len_f(ctx);
228
229         /* Byte 3: Revision ID */
230         acpigen_emit_byte(ctx, ACPI_GPIO_REVISION_ID);
231
232         /* Byte 4: GpioIo or GpioInt */
233         acpigen_emit_byte(ctx, gpio->type);
234
235         /*
236          * Byte 5-6: General Flags
237          *   [15:1]: 0 => Reserved
238          *      [0]: 1 => ResourceConsumer
239          */
240         acpigen_emit_word(ctx, 1 << 0);
241
242         switch (gpio->type) {
243         case ACPI_GPIO_TYPE_INTERRUPT:
244                 /*
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)
251                  */
252                 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
253                         flags |= 1 << 0;
254                 if (gpio->irq.shared == ACPI_IRQ_SHARED)
255                         flags |= 1 << 3;
256                 if (gpio->irq.wake == ACPI_IRQ_WAKE)
257                         flags |= 1 << 4;
258
259                 switch (gpio->irq.polarity) {
260                 case ACPI_IRQ_ACTIVE_HIGH:
261                         flags |= 0 << 1;
262                         break;
263                 case ACPI_IRQ_ACTIVE_LOW:
264                         flags |= 1 << 1;
265                         break;
266                 case ACPI_IRQ_ACTIVE_BOTH:
267                         flags |= 2 << 1;
268                         break;
269                 }
270                 break;
271
272         case ACPI_GPIO_TYPE_IO:
273                 /*
274                  * Byte 7-8: GPIO IO Flags
275                  *   [15:4]: 0 => Reserved
276                  *      [3]: Sharing  (0=EXCLUSIVE 1=SHARED)
277                  *      [2]: 0 => Reserved
278                  *    [1:0]: IO Restriction
279                  *           0 => IoRestrictionNone
280                  *           1 => IoRestrictionInputOnly
281                  *           2 => IoRestrictionOutputOnly
282                  *           3 => IoRestrictionNoneAndPreserve
283                  */
284                 flags |= gpio->io_restrict & 3;
285                 if (gpio->io_shared)
286                         flags |= 1 << 3;
287                 break;
288         }
289         acpigen_emit_word(ctx, flags);
290
291         /*
292          * Byte 9: Pin Configuration
293          *  0x01 => Default (no configuration applied)
294          *  0x02 => Pull-up
295          *  0x03 => Pull-down
296          *  0x04-0x7F => Reserved
297          *  0x80-0xff => Vendor defined
298          */
299         acpigen_emit_byte(ctx, gpio->pull);
300
301         /* Byte 10-11: Output Drive Strength in 1/100 mA */
302         acpigen_emit_word(ctx, gpio->output_drive_strength);
303
304         /* Byte 12-13: Debounce Timeout in 1/100 ms */
305         acpigen_emit_word(ctx, gpio->interrupt_debounce_timeout);
306
307         /* Byte 14-15: Pin Table Offset, relative to start */
308         pin_table_offset = largeres_write_len_f(ctx);
309
310         /* Byte 16: Reserved */
311         acpigen_emit_byte(ctx, 0);
312
313         /* Byte 17-18: Resource Source Name Offset, relative to start */
314         resource_offset = largeres_write_len_f(ctx);
315
316         /* Byte 19-20: Vendor Data Offset, relative to start */
317         vendor_data_offset = largeres_write_len_f(ctx);
318
319         /* Byte 21-22: Vendor Data Length */
320         acpigen_emit_word(ctx, 0);
321
322         /* Fill in Pin Table Offset */
323         largeres_fill_from_len(ctx, pin_table_offset, start);
324
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]);
328
329         /* Fill in Resource Source Name Offset */
330         largeres_fill_from_len(ctx, resource_offset, start);
331
332         /* Resource Source Name String */
333         acpigen_emit_string(ctx, gpio->resource);
334
335         /* Fill in Vendor Data Offset */
336         largeres_fill_from_len(ctx, vendor_data_offset, start);
337
338         /* Fill in GPIO Descriptor Length (account for len word) */
339         largeres_fill_len(ctx, desc_length);
340
341         return gpio->pins[0];
342 }
343
344 int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
345                                 const struct gpio_desc *desc)
346 {
347         struct acpi_gpio gpio;
348         int ret;
349
350         ret = gpio_get_acpi(desc, &gpio);
351         if (ret)
352                 return log_msg_ret("desc", ret);
353         ret = acpi_device_write_gpio(ctx, &gpio);
354         if (ret < 0)
355                 return log_msg_ret("gpio", ret);
356
357         return ret;
358 }
359
360 int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
361                                         struct udevice *dev, const char *prop)
362 {
363         struct irq req_irq;
364         int pin;
365         int ret;
366
367         ret = irq_get_by_index(dev, 0, &req_irq);
368         if (!ret) {
369                 ret = acpi_device_write_interrupt_irq(ctx, &req_irq);
370                 if (ret < 0)
371                         return log_msg_ret("irq", ret);
372                 pin = ret;
373         } else {
374                 struct gpio_desc req_gpio;
375
376                 ret = gpio_request_by_name(dev, prop, 0, &req_gpio,
377                                            GPIOD_IS_IN);
378                 if (ret)
379                         return log_msg_ret("no gpio", ret);
380                 ret = acpi_device_write_gpio_desc(ctx, &req_gpio);
381                 if (ret < 0)
382                         return log_msg_ret("gpio", ret);
383                 pin = ret;
384         }
385
386         return pin;
387 }
388
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)
392 {
393         void *desc_length, *type_length;
394
395         /* Byte 0: Descriptor Type */
396         acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
397
398         /* Byte 1+2: Length (filled in later) */
399         desc_length = largeres_write_len_f(ctx);
400
401         /* Byte 3: Revision ID */
402         acpigen_emit_byte(ctx, ACPI_I2C_SERIAL_BUS_REVISION_ID);
403
404         /* Byte 4: Resource Source Index is Reserved */
405         acpigen_emit_byte(ctx, 0);
406
407         /* Byte 5: Serial Bus Type is I2C */
408         acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_I2C);
409
410         /*
411          * Byte 6: Flags
412          *  [7:2]: 0 => Reserved
413          *    [1]: 1 => ResourceConsumer
414          *    [0]: 0 => ControllerInitiated
415          */
416         acpigen_emit_byte(ctx, 1 << 1);
417
418         /*
419          * Byte 7-8: Type Specific Flags
420          *   [15:1]: 0 => Reserved
421          *      [0]: 0 => 7bit, 1 => 10bit
422          */
423         acpigen_emit_word(ctx, i2c->mode_10bit);
424
425         /* Byte 9: Type Specific Revision ID */
426         acpigen_emit_byte(ctx, ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
427
428         /* Byte 10-11: I2C Type Data Length */
429         type_length = largeres_write_len_f(ctx);
430
431         /* Byte 12-15: I2C Bus Speed */
432         acpigen_emit_dword(ctx, i2c->speed);
433
434         /* Byte 16-17: I2C Slave Address */
435         acpigen_emit_word(ctx, i2c->address);
436
437         /* Fill in Type Data Length */
438         largeres_fill_len(ctx, type_length);
439
440         /* Byte 18+: ResourceSource */
441         acpigen_emit_string(ctx, i2c->resource);
442
443         /* Fill in I2C Descriptor Length */
444         largeres_fill_len(ctx, desc_length);
445 }
446
447 /**
448  * acpi_device_set_i2c() - Set up an ACPI I2C struct from a device
449  *
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.
452  *
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
457  */
458 static int acpi_device_set_i2c(const struct udevice *dev, struct acpi_i2c *i2c,
459                                const char *scope)
460 {
461         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
462         struct udevice *bus = dev_get_parent(dev);
463
464         memset(i2c, '\0', sizeof(*i2c));
465         i2c->address = chip->chip_addr;
466         i2c->mode_10bit = 0;
467
468         /*
469          * i2c_bus->speed_hz is set if this device is probed, but if not we
470          * must use the device tree
471          */
472         i2c->speed = dev_read_u32_default(bus, "clock-frequency",
473                                           I2C_SPEED_STANDARD_RATE);
474         i2c->resource = scope;
475
476         return i2c->address;
477 }
478
479 int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev)
480 {
481         char scope[ACPI_PATH_MAX];
482         struct acpi_i2c i2c;
483         int ret;
484
485         ret = acpi_device_scope(dev, scope, sizeof(scope));
486         if (ret)
487                 return log_msg_ret("scope", ret);
488         ret = acpi_device_set_i2c(dev, &i2c, scope);
489         if (ret < 0)
490                 return log_msg_ret("set", ret);
491         acpi_device_write_i2c(ctx, &i2c);
492
493         return ret;
494 }
495
496 #ifdef CONFIG_SPI
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)
499 {
500         void *desc_length, *type_length;
501         u16 flags = 0;
502
503         /* Byte 0: Descriptor Type */
504         acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
505
506         /* Byte 1+2: Length (filled in later) */
507         desc_length = largeres_write_len_f(ctx);
508
509         /* Byte 3: Revision ID */
510         acpigen_emit_byte(ctx, ACPI_SPI_SERIAL_BUS_REVISION_ID);
511
512         /* Byte 4: Resource Source Index is Reserved */
513         acpigen_emit_byte(ctx, 0);
514
515         /* Byte 5: Serial Bus Type is SPI */
516         acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_SPI);
517
518         /*
519          * Byte 6: Flags
520          *  [7:2]: 0 => Reserved
521          *    [1]: 1 => ResourceConsumer
522          *    [0]: 0 => ControllerInitiated
523          */
524         acpigen_emit_byte(ctx, BIT(1));
525
526         /*
527          * Byte 7-8: Type Specific Flags
528          *   [15:2]: 0 => Reserveda
529          *      [1]: 0 => ActiveLow, 1 => ActiveHigh
530          *      [0]: 0 => FourWire, 1 => ThreeWire
531          */
532         if (spi->wire_mode == SPI_3_WIRE_MODE)
533                 flags |= BIT(0);
534         if (spi->device_select_polarity == SPI_POLARITY_HIGH)
535                 flags |= BIT(1);
536         acpigen_emit_word(ctx, flags);
537
538         /* Byte 9: Type Specific Revision ID */
539         acpigen_emit_byte(ctx, ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
540
541         /* Byte 10-11: SPI Type Data Length */
542         type_length = largeres_write_len_f(ctx);
543
544         /* Byte 12-15: Connection Speed */
545         acpigen_emit_dword(ctx, spi->speed);
546
547         /* Byte 16: Data Bit Length */
548         acpigen_emit_byte(ctx, spi->data_bit_length);
549
550         /* Byte 17: Clock Phase */
551         acpigen_emit_byte(ctx, spi->clock_phase);
552
553         /* Byte 18: Clock Polarity */
554         acpigen_emit_byte(ctx, spi->clock_polarity);
555
556         /* Byte 19-20: Device Selection */
557         acpigen_emit_word(ctx, spi->device_select);
558
559         /* Fill in Type Data Length */
560         largeres_fill_len(ctx, type_length);
561
562         /* Byte 21+: ResourceSource String */
563         acpigen_emit_string(ctx, spi->resource);
564
565         /* Fill in SPI Descriptor Length */
566         largeres_fill_len(ctx, desc_length);
567 }
568
569 /**
570  * acpi_device_set_spi() - Set up an ACPI SPI struct from a device
571  *
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.
574  *
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)
578  * @return 0 (always)
579  */
580 static int acpi_device_set_spi(const struct udevice *dev, struct acpi_spi *spi,
581                                const char *scope)
582 {
583         struct dm_spi_slave_platdata *plat;
584         struct spi_slave *slave = dev_get_parent_priv(dev);
585
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;
598
599         return 0;
600 }
601
602 int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev)
603 {
604         char scope[ACPI_PATH_MAX];
605         struct acpi_spi spi;
606         int ret;
607
608         ret = acpi_device_scope(dev, scope, sizeof(scope));
609         if (ret)
610                 return log_msg_ret("scope", ret);
611         ret = acpi_device_set_spi(dev, &spi, scope);
612         if (ret)
613                 return log_msg_ret("set", ret);
614         acpi_device_write_spi(ctx, &spi);
615
616         return 0;
617 }
618 #endif /* CONFIG_SPI */