Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 3 | |
| 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 Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 15 | static int spi_acpi_get_bus(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 16 | { |
| 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 Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 33 | static 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 | |
| 48 | static 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 Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 62 | static void spi_acpi_fill_ssdt_generator(struct device *dev) |
| 63 | { |
| 64 | struct drivers_spi_acpi_config *config = dev->chip_info; |
| 65 | const char *scope = acpi_device_scope(dev); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 66 | const char *path = acpi_device_path(dev); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 67 | struct acpi_spi spi = { |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 68 | .device_select = dev->path.spi.cs, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 69 | .speed = config->speed ? : 1 * MHz, |
| 70 | .resource = scope, |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 71 | .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 Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 76 | }; |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 77 | int curr_index = 0; |
| 78 | int irq_gpio_index = -1; |
| 79 | int reset_gpio_index = -1; |
| 80 | int enable_gpio_index = -1; |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 81 | |
| 82 | if (!dev->enabled || !scope) |
| 83 | return; |
| 84 | |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 85 | if (spi_acpi_get_bus(dev) == -1) { |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 86 | printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n", |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 87 | dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (!config->hid) { |
| 92 | printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev)); |
| 93 | return; |
| 94 | } |
| 95 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 96 | /* 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 Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 105 | acpigen_write_STA(acpi_device_status(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 106 | |
| 107 | /* Resources */ |
| 108 | acpigen_write_name("_CRS"); |
| 109 | acpigen_write_resourcetemplate_header(); |
| 110 | acpi_device_write_spi(&spi); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 111 | |
| 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 Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 126 | acpigen_write_resourcetemplate_footer(); |
| 127 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 128 | /* 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 Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 137 | struct acpi_dp *dsd = acpi_dp_new_table("_DSD"); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 138 | 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 Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 153 | acpi_dp_write(dsd); |
| 154 | } |
| 155 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 156 | /* Power Resource */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 157 | 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 Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 171 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 172 | acpigen_pop_len(); /* Device */ |
| 173 | acpigen_pop_len(); /* Scope */ |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 174 | |
| 175 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
| 176 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 179 | static const char *spi_acpi_name(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 180 | { |
| 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 | |
| 192 | static struct device_operations spi_acpi_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 193 | .read_resources = noop_read_resources, |
| 194 | .set_resources = noop_set_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 195 | .acpi_name = spi_acpi_name, |
| 196 | .acpi_fill_ssdt = spi_acpi_fill_ssdt_generator, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | static void spi_acpi_enable(struct device *dev) |
| 200 | { |
| 201 | dev->ops = &spi_acpi_ops; |
| 202 | } |
| 203 | |
| 204 | struct chip_operations drivers_spi_acpi_ops = { |
| 205 | CHIP_NAME("SPI Device") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 206 | .enable_dev = spi_acpi_enable |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 207 | }; |