Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2017 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <arch/acpi_device.h> |
| 17 | #include <arch/acpigen.h> |
| 18 | #include <console/console.h> |
| 19 | #include <device/device.h> |
| 20 | #include <device/path.h> |
| 21 | #include <device/spi.h> |
| 22 | #include <spi-generic.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include "chip.h" |
| 26 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 27 | static int spi_acpi_get_bus(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 28 | { |
| 29 | struct device *spi_dev; |
| 30 | struct device_operations *ops; |
| 31 | |
| 32 | if (!dev->bus || !dev->bus->dev) |
| 33 | return -1; |
| 34 | |
| 35 | spi_dev = dev->bus->dev; |
| 36 | ops = spi_dev->ops; |
| 37 | |
| 38 | if (ops && ops->ops_spi_bus && |
| 39 | ops->ops_spi_bus->dev_to_bus) |
| 40 | return ops->ops_spi_bus->dev_to_bus(spi_dev); |
| 41 | |
| 42 | return -1; |
| 43 | } |
| 44 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 45 | static bool spi_acpi_add_gpios_to_crs(struct drivers_spi_acpi_config *config) |
| 46 | { |
| 47 | /* |
| 48 | * Return false if: |
| 49 | * 1. Request to explicitly disable export of GPIOs in CRS, or |
| 50 | * 2. Both reset and enable GPIOs are not provided. |
| 51 | */ |
| 52 | if (config->disable_gpio_export_in_crs || |
| 53 | ((config->reset_gpio.pin_count == 0) && |
| 54 | (config->enable_gpio.pin_count == 0))) |
| 55 | return false; |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | static int spi_acpi_write_gpio(struct acpi_gpio *gpio, int *curr_index) |
| 61 | { |
| 62 | int ret = -1; |
| 63 | |
| 64 | if (gpio->pin_count == 0) |
| 65 | return ret; |
| 66 | |
| 67 | acpi_device_write_gpio(gpio); |
| 68 | ret = *curr_index; |
| 69 | (*curr_index)++; |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 74 | static void spi_acpi_fill_ssdt_generator(struct device *dev) |
| 75 | { |
| 76 | struct drivers_spi_acpi_config *config = dev->chip_info; |
| 77 | const char *scope = acpi_device_scope(dev); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 78 | const char *path = acpi_device_path(dev); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 79 | struct acpi_spi spi = { |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 80 | .device_select = dev->path.spi.cs, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 81 | .speed = config->speed ? : 1 * MHz, |
| 82 | .resource = scope, |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 83 | .device_select_polarity = SPI_POLARITY_LOW, |
| 84 | .wire_mode = SPI_4_WIRE_MODE, |
| 85 | .data_bit_length = 8, |
| 86 | .clock_phase = SPI_CLOCK_PHASE_FIRST, |
| 87 | .clock_polarity = SPI_POLARITY_LOW, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 88 | }; |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 89 | int curr_index = 0; |
| 90 | int irq_gpio_index = -1; |
| 91 | int reset_gpio_index = -1; |
| 92 | int enable_gpio_index = -1; |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 93 | |
| 94 | if (!dev->enabled || !scope) |
| 95 | return; |
| 96 | |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 97 | if (spi_acpi_get_bus(dev) == -1) { |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 98 | printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n", |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 99 | dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (!config->hid) { |
| 104 | printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev)); |
| 105 | return; |
| 106 | } |
| 107 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 108 | /* Device */ |
| 109 | acpigen_write_scope(scope); |
| 110 | acpigen_write_device(acpi_device_name(dev)); |
| 111 | acpigen_write_name_string("_HID", config->hid); |
| 112 | if (config->cid) |
| 113 | acpigen_write_name_string("_CID", config->cid); |
| 114 | acpigen_write_name_integer("_UID", config->uid); |
| 115 | if (config->desc) |
| 116 | acpigen_write_name_string("_DDN", config->desc); |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame^] | 117 | acpigen_write_STA(acpi_device_status(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 118 | |
| 119 | /* Resources */ |
| 120 | acpigen_write_name("_CRS"); |
| 121 | acpigen_write_resourcetemplate_header(); |
| 122 | acpi_device_write_spi(&spi); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 123 | |
| 124 | /* Use either Interrupt() or GpioInt() */ |
| 125 | if (config->irq_gpio.pin_count) |
| 126 | irq_gpio_index = spi_acpi_write_gpio(&config->irq_gpio, |
| 127 | &curr_index); |
| 128 | else |
| 129 | acpi_device_write_interrupt(&config->irq); |
| 130 | |
| 131 | /* Add enable/reset GPIOs if needed */ |
| 132 | if (spi_acpi_add_gpios_to_crs(config)) { |
| 133 | reset_gpio_index = spi_acpi_write_gpio(&config->reset_gpio, |
| 134 | &curr_index); |
| 135 | enable_gpio_index = spi_acpi_write_gpio(&config->enable_gpio, |
| 136 | &curr_index); |
| 137 | } |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 138 | acpigen_write_resourcetemplate_footer(); |
| 139 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 140 | /* Wake capabilities */ |
| 141 | if (config->wake) { |
| 142 | acpigen_write_name_integer("_S0W", 4); |
| 143 | acpigen_write_PRW(config->wake, 3); |
| 144 | }; |
| 145 | |
| 146 | /* Write device properties if needed */ |
| 147 | if (config->compat_string || irq_gpio_index >= 0 || |
| 148 | reset_gpio_index >= 0 || enable_gpio_index >= 0) { |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 149 | struct acpi_dp *dsd = acpi_dp_new_table("_DSD"); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 150 | if (config->compat_string) |
| 151 | acpi_dp_add_string(dsd, "compatible", |
| 152 | config->compat_string); |
| 153 | if (irq_gpio_index >= 0) |
| 154 | acpi_dp_add_gpio(dsd, "irq-gpios", path, |
| 155 | irq_gpio_index, 0, |
| 156 | config->irq_gpio.polarity); |
| 157 | if (reset_gpio_index >= 0) |
| 158 | acpi_dp_add_gpio(dsd, "reset-gpios", path, |
| 159 | reset_gpio_index, 0, |
| 160 | config->reset_gpio.polarity); |
| 161 | if (enable_gpio_index >= 0) |
| 162 | acpi_dp_add_gpio(dsd, "enable-gpios", path, |
| 163 | enable_gpio_index, 0, |
| 164 | config->enable_gpio.polarity); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 165 | acpi_dp_write(dsd); |
| 166 | } |
| 167 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 168 | /* Power Resource */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 169 | if (config->has_power_resource) { |
| 170 | const struct acpi_power_res_params power_res_params = { |
| 171 | &config->reset_gpio, |
| 172 | config->reset_delay_ms, |
| 173 | config->reset_off_delay_ms, |
| 174 | &config->enable_gpio, |
| 175 | config->enable_delay_ms, |
| 176 | config->enable_off_delay_ms, |
| 177 | &config->stop_gpio, |
| 178 | config->stop_delay_ms, |
| 179 | config->stop_off_delay_ms |
| 180 | }; |
| 181 | acpi_device_add_power_res(&power_res_params); |
| 182 | } |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 183 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 184 | acpigen_pop_len(); /* Device */ |
| 185 | acpigen_pop_len(); /* Scope */ |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 186 | |
| 187 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
| 188 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 191 | static const char *spi_acpi_name(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 192 | { |
| 193 | struct drivers_spi_acpi_config *config = dev->chip_info; |
| 194 | static char name[5]; |
| 195 | |
| 196 | if (config->name) |
| 197 | return config->name; |
| 198 | |
| 199 | snprintf(name, sizeof(name), "S%03.3X", spi_acpi_get_bus(dev)); |
| 200 | name[4] = '\0'; |
| 201 | return name; |
| 202 | } |
| 203 | |
| 204 | static struct device_operations spi_acpi_ops = { |
| 205 | .read_resources = DEVICE_NOOP, |
| 206 | .set_resources = DEVICE_NOOP, |
| 207 | .enable_resources = DEVICE_NOOP, |
| 208 | .acpi_name = &spi_acpi_name, |
| 209 | .acpi_fill_ssdt_generator = &spi_acpi_fill_ssdt_generator, |
| 210 | }; |
| 211 | |
| 212 | static void spi_acpi_enable(struct device *dev) |
| 213 | { |
| 214 | dev->ops = &spi_acpi_ops; |
| 215 | } |
| 216 | |
| 217 | struct chip_operations drivers_spi_acpi_ops = { |
| 218 | CHIP_NAME("SPI Device") |
| 219 | .enable_dev = &spi_acpi_enable |
| 220 | }; |