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 | |
| 27 | static int spi_acpi_get_bus(struct device *dev) |
| 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 spi_cfg spi_cfg; |
| 80 | struct spi_slave slave; |
| 81 | int bus = -1, cs = dev->path.spi.cs; |
| 82 | struct acpi_spi spi = { |
| 83 | .device_select = cs, |
| 84 | .speed = config->speed ? : 1 * MHz, |
| 85 | .resource = scope, |
| 86 | }; |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 87 | int curr_index = 0; |
| 88 | int irq_gpio_index = -1; |
| 89 | int reset_gpio_index = -1; |
| 90 | int enable_gpio_index = -1; |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 91 | |
| 92 | if (!dev->enabled || !scope) |
| 93 | return; |
| 94 | |
| 95 | bus = spi_acpi_get_bus(dev); |
| 96 | if (bus == -1) { |
| 97 | printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n", |
| 98 | dev_path(dev)); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (!config->hid) { |
| 103 | printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev)); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (spi_setup_slave(bus, cs, &slave)) { |
| 108 | printk(BIOS_ERR, "%s: ERROR: SPI setup failed.\n", |
| 109 | dev_path(dev)); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (spi_get_config(&slave, &spi_cfg)) { |
| 114 | printk(BIOS_ERR, "%s: ERROR: SPI get config failed.\n", |
| 115 | dev_path(dev)); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | spi.device_select_polarity = spi_cfg.cs_polarity; |
| 120 | spi.wire_mode = spi_cfg.wire_mode; |
| 121 | spi.data_bit_length = spi_cfg.data_bit_length; |
| 122 | spi.clock_phase = spi_cfg.clk_phase; |
| 123 | |
| 124 | /* Device */ |
| 125 | acpigen_write_scope(scope); |
| 126 | acpigen_write_device(acpi_device_name(dev)); |
| 127 | acpigen_write_name_string("_HID", config->hid); |
| 128 | if (config->cid) |
| 129 | acpigen_write_name_string("_CID", config->cid); |
| 130 | acpigen_write_name_integer("_UID", config->uid); |
| 131 | if (config->desc) |
| 132 | acpigen_write_name_string("_DDN", config->desc); |
| 133 | |
| 134 | /* Resources */ |
| 135 | acpigen_write_name("_CRS"); |
| 136 | acpigen_write_resourcetemplate_header(); |
| 137 | acpi_device_write_spi(&spi); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 138 | |
| 139 | /* Use either Interrupt() or GpioInt() */ |
| 140 | if (config->irq_gpio.pin_count) |
| 141 | irq_gpio_index = spi_acpi_write_gpio(&config->irq_gpio, |
| 142 | &curr_index); |
| 143 | else |
| 144 | acpi_device_write_interrupt(&config->irq); |
| 145 | |
| 146 | /* Add enable/reset GPIOs if needed */ |
| 147 | if (spi_acpi_add_gpios_to_crs(config)) { |
| 148 | reset_gpio_index = spi_acpi_write_gpio(&config->reset_gpio, |
| 149 | &curr_index); |
| 150 | enable_gpio_index = spi_acpi_write_gpio(&config->enable_gpio, |
| 151 | &curr_index); |
| 152 | } |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 153 | acpigen_write_resourcetemplate_footer(); |
| 154 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 155 | /* Wake capabilities */ |
| 156 | if (config->wake) { |
| 157 | acpigen_write_name_integer("_S0W", 4); |
| 158 | acpigen_write_PRW(config->wake, 3); |
| 159 | }; |
| 160 | |
| 161 | /* Write device properties if needed */ |
| 162 | if (config->compat_string || irq_gpio_index >= 0 || |
| 163 | reset_gpio_index >= 0 || enable_gpio_index >= 0) { |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 164 | struct acpi_dp *dsd = acpi_dp_new_table("_DSD"); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 165 | if (config->compat_string) |
| 166 | acpi_dp_add_string(dsd, "compatible", |
| 167 | config->compat_string); |
| 168 | if (irq_gpio_index >= 0) |
| 169 | acpi_dp_add_gpio(dsd, "irq-gpios", path, |
| 170 | irq_gpio_index, 0, |
| 171 | config->irq_gpio.polarity); |
| 172 | if (reset_gpio_index >= 0) |
| 173 | acpi_dp_add_gpio(dsd, "reset-gpios", path, |
| 174 | reset_gpio_index, 0, |
| 175 | config->reset_gpio.polarity); |
| 176 | if (enable_gpio_index >= 0) |
| 177 | acpi_dp_add_gpio(dsd, "enable-gpios", path, |
| 178 | enable_gpio_index, 0, |
| 179 | config->enable_gpio.polarity); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 180 | acpi_dp_write(dsd); |
| 181 | } |
| 182 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 183 | /* Power Resource */ |
| 184 | if (config->has_power_resource) |
| 185 | acpi_device_add_power_res( |
| 186 | &config->reset_gpio, config->reset_delay_ms, |
| 187 | &config->enable_gpio, config->enable_delay_ms); |
| 188 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 189 | acpigen_pop_len(); /* Device */ |
| 190 | acpigen_pop_len(); /* Scope */ |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 191 | |
| 192 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
| 193 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static const char *spi_acpi_name(struct device *dev) |
| 197 | { |
| 198 | struct drivers_spi_acpi_config *config = dev->chip_info; |
| 199 | static char name[5]; |
| 200 | |
| 201 | if (config->name) |
| 202 | return config->name; |
| 203 | |
| 204 | snprintf(name, sizeof(name), "S%03.3X", spi_acpi_get_bus(dev)); |
| 205 | name[4] = '\0'; |
| 206 | return name; |
| 207 | } |
| 208 | |
| 209 | static struct device_operations spi_acpi_ops = { |
| 210 | .read_resources = DEVICE_NOOP, |
| 211 | .set_resources = DEVICE_NOOP, |
| 212 | .enable_resources = DEVICE_NOOP, |
| 213 | .acpi_name = &spi_acpi_name, |
| 214 | .acpi_fill_ssdt_generator = &spi_acpi_fill_ssdt_generator, |
| 215 | }; |
| 216 | |
| 217 | static void spi_acpi_enable(struct device *dev) |
| 218 | { |
| 219 | dev->ops = &spi_acpi_ops; |
| 220 | } |
| 221 | |
| 222 | struct chip_operations drivers_spi_acpi_ops = { |
| 223 | CHIP_NAME("SPI Device") |
| 224 | .enable_dev = &spi_acpi_enable |
| 225 | }; |