blob: 0eeaffbddd824110077776a36c2bace20aa0611c [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Furquan Shaikh20a91c92017-02-11 11:16:18 -08003
4#include <arch/acpi_device.h>
5#include <arch/acpigen.h>
6#include <console/console.h>
7#include <device/device.h>
8#include <device/path.h>
9#include <device/spi.h>
10#include <spi-generic.h>
11#include <stdint.h>
12#include <string.h>
13#include "chip.h"
14
Aaron Durbinaa090cb2017-09-13 16:01:52 -060015static int spi_acpi_get_bus(const struct device *dev)
Furquan Shaikh20a91c92017-02-11 11:16:18 -080016{
17 struct device *spi_dev;
18 struct device_operations *ops;
19
20 if (!dev->bus || !dev->bus->dev)
21 return -1;
22
23 spi_dev = dev->bus->dev;
24 ops = spi_dev->ops;
25
26 if (ops && ops->ops_spi_bus &&
27 ops->ops_spi_bus->dev_to_bus)
28 return ops->ops_spi_bus->dev_to_bus(spi_dev);
29
30 return -1;
31}
32
Duncan Lauriec9db3842017-02-17 17:14:35 -080033static bool spi_acpi_add_gpios_to_crs(struct drivers_spi_acpi_config *config)
34{
35 /*
36 * Return false if:
37 * 1. Request to explicitly disable export of GPIOs in CRS, or
38 * 2. Both reset and enable GPIOs are not provided.
39 */
40 if (config->disable_gpio_export_in_crs ||
41 ((config->reset_gpio.pin_count == 0) &&
42 (config->enable_gpio.pin_count == 0)))
43 return false;
44
45 return true;
46}
47
48static int spi_acpi_write_gpio(struct acpi_gpio *gpio, int *curr_index)
49{
50 int ret = -1;
51
52 if (gpio->pin_count == 0)
53 return ret;
54
55 acpi_device_write_gpio(gpio);
56 ret = *curr_index;
57 (*curr_index)++;
58
59 return ret;
60}
61
Furquan Shaikh7536a392020-04-24 21:59:21 -070062static void spi_acpi_fill_ssdt_generator(const struct device *dev)
Furquan Shaikh20a91c92017-02-11 11:16:18 -080063{
64 struct drivers_spi_acpi_config *config = dev->chip_info;
65 const char *scope = acpi_device_scope(dev);
Duncan Lauriec9db3842017-02-17 17:14:35 -080066 const char *path = acpi_device_path(dev);
Furquan Shaikh20a91c92017-02-11 11:16:18 -080067 struct acpi_spi spi = {
Furquan Shaikh5bda6422017-04-07 07:13:24 -070068 .device_select = dev->path.spi.cs,
Furquan Shaikh20a91c92017-02-11 11:16:18 -080069 .speed = config->speed ? : 1 * MHz,
70 .resource = scope,
Furquan Shaikh5bda6422017-04-07 07:13:24 -070071 .device_select_polarity = SPI_POLARITY_LOW,
72 .wire_mode = SPI_4_WIRE_MODE,
73 .data_bit_length = 8,
74 .clock_phase = SPI_CLOCK_PHASE_FIRST,
75 .clock_polarity = SPI_POLARITY_LOW,
Furquan Shaikh20a91c92017-02-11 11:16:18 -080076 };
Duncan Lauriec9db3842017-02-17 17:14:35 -080077 int curr_index = 0;
78 int irq_gpio_index = -1;
79 int reset_gpio_index = -1;
80 int enable_gpio_index = -1;
Furquan Shaikh20a91c92017-02-11 11:16:18 -080081
82 if (!dev->enabled || !scope)
83 return;
84
Furquan Shaikh5bda6422017-04-07 07:13:24 -070085 if (spi_acpi_get_bus(dev) == -1) {
Furquan Shaikh20a91c92017-02-11 11:16:18 -080086 printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n",
Furquan Shaikh5bda6422017-04-07 07:13:24 -070087 dev_path(dev));
Furquan Shaikh20a91c92017-02-11 11:16:18 -080088 return;
89 }
90
91 if (!config->hid) {
92 printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev));
93 return;
94 }
95
Furquan Shaikh20a91c92017-02-11 11:16:18 -080096 /* Device */
97 acpigen_write_scope(scope);
98 acpigen_write_device(acpi_device_name(dev));
99 acpigen_write_name_string("_HID", config->hid);
100 if (config->cid)
101 acpigen_write_name_string("_CID", config->cid);
102 acpigen_write_name_integer("_UID", config->uid);
103 if (config->desc)
104 acpigen_write_name_string("_DDN", config->desc);
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800105 acpigen_write_STA(acpi_device_status(dev));
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800106
107 /* Resources */
108 acpigen_write_name("_CRS");
109 acpigen_write_resourcetemplate_header();
110 acpi_device_write_spi(&spi);
Duncan Lauriec9db3842017-02-17 17:14:35 -0800111
112 /* Use either Interrupt() or GpioInt() */
113 if (config->irq_gpio.pin_count)
114 irq_gpio_index = spi_acpi_write_gpio(&config->irq_gpio,
115 &curr_index);
116 else
117 acpi_device_write_interrupt(&config->irq);
118
119 /* Add enable/reset GPIOs if needed */
120 if (spi_acpi_add_gpios_to_crs(config)) {
121 reset_gpio_index = spi_acpi_write_gpio(&config->reset_gpio,
122 &curr_index);
123 enable_gpio_index = spi_acpi_write_gpio(&config->enable_gpio,
124 &curr_index);
125 }
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800126 acpigen_write_resourcetemplate_footer();
127
Duncan Lauriec9db3842017-02-17 17:14:35 -0800128 /* Wake capabilities */
129 if (config->wake) {
130 acpigen_write_name_integer("_S0W", 4);
131 acpigen_write_PRW(config->wake, 3);
132 };
133
134 /* Write device properties if needed */
135 if (config->compat_string || irq_gpio_index >= 0 ||
136 reset_gpio_index >= 0 || enable_gpio_index >= 0) {
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800137 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
Duncan Lauriec9db3842017-02-17 17:14:35 -0800138 if (config->compat_string)
139 acpi_dp_add_string(dsd, "compatible",
140 config->compat_string);
141 if (irq_gpio_index >= 0)
142 acpi_dp_add_gpio(dsd, "irq-gpios", path,
143 irq_gpio_index, 0,
144 config->irq_gpio.polarity);
145 if (reset_gpio_index >= 0)
146 acpi_dp_add_gpio(dsd, "reset-gpios", path,
147 reset_gpio_index, 0,
148 config->reset_gpio.polarity);
149 if (enable_gpio_index >= 0)
150 acpi_dp_add_gpio(dsd, "enable-gpios", path,
151 enable_gpio_index, 0,
152 config->enable_gpio.polarity);
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800153 acpi_dp_write(dsd);
154 }
155
Duncan Lauriec9db3842017-02-17 17:14:35 -0800156 /* Power Resource */
Shelley Chena0603392018-04-26 13:52:30 -0700157 if (config->has_power_resource) {
158 const struct acpi_power_res_params power_res_params = {
159 &config->reset_gpio,
160 config->reset_delay_ms,
161 config->reset_off_delay_ms,
162 &config->enable_gpio,
163 config->enable_delay_ms,
164 config->enable_off_delay_ms,
165 &config->stop_gpio,
166 config->stop_delay_ms,
167 config->stop_off_delay_ms
168 };
169 acpi_device_add_power_res(&power_res_params);
170 }
Duncan Lauriec9db3842017-02-17 17:14:35 -0800171
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800172 acpigen_pop_len(); /* Device */
173 acpigen_pop_len(); /* Scope */
Duncan Lauriec9db3842017-02-17 17:14:35 -0800174
175 printk(BIOS_INFO, "%s: %s at %s\n", path,
176 config->desc ? : dev->chip_ops->name, dev_path(dev));
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800177}
178
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600179static const char *spi_acpi_name(const struct device *dev)
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800180{
181 struct drivers_spi_acpi_config *config = dev->chip_info;
182 static char name[5];
183
184 if (config->name)
185 return config->name;
186
187 snprintf(name, sizeof(name), "S%03.3X", spi_acpi_get_bus(dev));
188 name[4] = '\0';
189 return name;
190}
191
192static struct device_operations spi_acpi_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200193 .read_resources = noop_read_resources,
194 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200195 .acpi_name = spi_acpi_name,
196 .acpi_fill_ssdt = spi_acpi_fill_ssdt_generator,
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800197};
198
199static void spi_acpi_enable(struct device *dev)
200{
201 dev->ops = &spi_acpi_ops;
202}
203
204struct chip_operations drivers_spi_acpi_ops = {
205 CHIP_NAME("SPI Device")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100206 .enable_dev = spi_acpi_enable
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800207};