blob: 2a39814c3d3727465875a2098321d5426807dfd2 [file] [log] [blame]
Furquan Shaikh20a91c92017-02-11 11:16:18 -08001/*
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
27static 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 Lauriec9db3842017-02-17 17:14:35 -080045static 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
60static 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 Shaikh20a91c92017-02-11 11:16:18 -080074static 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 Lauriec9db3842017-02-17 17:14:35 -080078 const char *path = acpi_device_path(dev);
Furquan Shaikh20a91c92017-02-11 11:16:18 -080079 struct acpi_spi spi = {
Furquan Shaikh5bda6422017-04-07 07:13:24 -070080 .device_select = dev->path.spi.cs,
Furquan Shaikh20a91c92017-02-11 11:16:18 -080081 .speed = config->speed ? : 1 * MHz,
82 .resource = scope,
Furquan Shaikh5bda6422017-04-07 07:13:24 -070083 .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 Shaikh20a91c92017-02-11 11:16:18 -080088 };
Duncan Lauriec9db3842017-02-17 17:14:35 -080089 int curr_index = 0;
90 int irq_gpio_index = -1;
91 int reset_gpio_index = -1;
92 int enable_gpio_index = -1;
Furquan Shaikh20a91c92017-02-11 11:16:18 -080093
94 if (!dev->enabled || !scope)
95 return;
96
Furquan Shaikh5bda6422017-04-07 07:13:24 -070097 if (spi_acpi_get_bus(dev) == -1) {
Furquan Shaikh20a91c92017-02-11 11:16:18 -080098 printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n",
Furquan Shaikh5bda6422017-04-07 07:13:24 -070099 dev_path(dev));
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800100 return;
101 }
102
103 if (!config->hid) {
104 printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev));
105 return;
106 }
107
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800108 /* 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);
117
118 /* Resources */
119 acpigen_write_name("_CRS");
120 acpigen_write_resourcetemplate_header();
121 acpi_device_write_spi(&spi);
Duncan Lauriec9db3842017-02-17 17:14:35 -0800122
123 /* Use either Interrupt() or GpioInt() */
124 if (config->irq_gpio.pin_count)
125 irq_gpio_index = spi_acpi_write_gpio(&config->irq_gpio,
126 &curr_index);
127 else
128 acpi_device_write_interrupt(&config->irq);
129
130 /* Add enable/reset GPIOs if needed */
131 if (spi_acpi_add_gpios_to_crs(config)) {
132 reset_gpio_index = spi_acpi_write_gpio(&config->reset_gpio,
133 &curr_index);
134 enable_gpio_index = spi_acpi_write_gpio(&config->enable_gpio,
135 &curr_index);
136 }
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800137 acpigen_write_resourcetemplate_footer();
138
Duncan Lauriec9db3842017-02-17 17:14:35 -0800139 /* Wake capabilities */
140 if (config->wake) {
141 acpigen_write_name_integer("_S0W", 4);
142 acpigen_write_PRW(config->wake, 3);
143 };
144
145 /* Write device properties if needed */
146 if (config->compat_string || irq_gpio_index >= 0 ||
147 reset_gpio_index >= 0 || enable_gpio_index >= 0) {
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800148 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
Duncan Lauriec9db3842017-02-17 17:14:35 -0800149 if (config->compat_string)
150 acpi_dp_add_string(dsd, "compatible",
151 config->compat_string);
152 if (irq_gpio_index >= 0)
153 acpi_dp_add_gpio(dsd, "irq-gpios", path,
154 irq_gpio_index, 0,
155 config->irq_gpio.polarity);
156 if (reset_gpio_index >= 0)
157 acpi_dp_add_gpio(dsd, "reset-gpios", path,
158 reset_gpio_index, 0,
159 config->reset_gpio.polarity);
160 if (enable_gpio_index >= 0)
161 acpi_dp_add_gpio(dsd, "enable-gpios", path,
162 enable_gpio_index, 0,
163 config->enable_gpio.polarity);
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800164 acpi_dp_write(dsd);
165 }
166
Duncan Lauriec9db3842017-02-17 17:14:35 -0800167 /* Power Resource */
168 if (config->has_power_resource)
169 acpi_device_add_power_res(
170 &config->reset_gpio, config->reset_delay_ms,
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700171 &config->enable_gpio, config->enable_delay_ms,
172 &config->stop_gpio, config->stop_delay_ms);
Duncan Lauriec9db3842017-02-17 17:14:35 -0800173
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800174 acpigen_pop_len(); /* Device */
175 acpigen_pop_len(); /* Scope */
Duncan Lauriec9db3842017-02-17 17:14:35 -0800176
177 printk(BIOS_INFO, "%s: %s at %s\n", path,
178 config->desc ? : dev->chip_ops->name, dev_path(dev));
Furquan Shaikh20a91c92017-02-11 11:16:18 -0800179}
180
181static const char *spi_acpi_name(struct device *dev)
182{
183 struct drivers_spi_acpi_config *config = dev->chip_info;
184 static char name[5];
185
186 if (config->name)
187 return config->name;
188
189 snprintf(name, sizeof(name), "S%03.3X", spi_acpi_get_bus(dev));
190 name[4] = '\0';
191 return name;
192}
193
194static struct device_operations spi_acpi_ops = {
195 .read_resources = DEVICE_NOOP,
196 .set_resources = DEVICE_NOOP,
197 .enable_resources = DEVICE_NOOP,
198 .acpi_name = &spi_acpi_name,
199 .acpi_fill_ssdt_generator = &spi_acpi_fill_ssdt_generator,
200};
201
202static void spi_acpi_enable(struct device *dev)
203{
204 dev->ops = &spi_acpi_ops;
205}
206
207struct chip_operations drivers_spi_acpi_ops = {
208 CHIP_NAME("SPI Device")
209 .enable_dev = &spi_acpi_enable
210};