blob: a2ad7abd86514eccb83c5d0f760ac842de86a006 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Rothcddd6002019-09-23 17:38:27 -06002
3/*
Patrick Rudolphffbc3b52019-06-06 15:45:51 +02004 * Place in devicetree.cb:
5 *
6 * chip drivers/ipmi
7 * device pnp ca2.0 on end # IPMI KCS
8 * end
9 */
10
Elyes HAOUAS92f46aa2020-09-15 08:42:17 +020011#include <arch/io.h>
Michael Niewöhner548a3dc2020-11-24 12:45:07 +010012#include <bootstate.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020013#include <console/console.h>
14#include <device/device.h>
Michael Niewöhner31830d32020-11-23 13:24:40 +010015#include <device/gpio.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020016#include <device/pnp.h>
17#if CONFIG(HAVE_ACPI_TABLES)
Furquan Shaikh76cedd22020-05-02 10:24:23 -070018#include <acpi/acpi.h>
19#include <acpi/acpigen.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020020#endif
21#if CONFIG(GENERATE_SMBIOS_TABLES)
22#include <smbios.h>
23#endif
24#include <version.h>
25#include <delay.h>
Patrick Rudolph3d41a132019-07-22 16:31:35 +020026#include <timer.h>
Sergii Dmytrukef7dd5d2021-10-22 01:02:32 +030027#include "ipmi_if.h"
Patrick Rudolph09cdeba2019-12-30 14:40:04 +010028#include "ipmi_supermicro_oem.h"
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020029#include "chip.h"
30
31/* 4 bit encoding */
32static u8 ipmi_revision_major = 0x1;
33static u8 ipmi_revision_minor = 0x0;
34
Tim Chu278ad212020-10-06 01:40:53 -070035static u8 bmc_revision_major = 0x0;
36static u8 bmc_revision_minor = 0x0;
37
Michael Niewöhner548a3dc2020-11-24 12:45:07 +010038static struct boot_state_callback bscb_post_complete;
39
Michael Niewöhner548a3dc2020-11-24 12:45:07 +010040static void bmc_set_post_complete_gpio_callback(void *arg)
41{
42 struct drivers_ipmi_config *conf = arg;
43 const struct gpio_operations *gpio_ops;
44
45 if (!conf || !conf->post_complete_gpio)
46 return;
47
48 gpio_ops = dev_get_gpio_ops(conf->gpio_dev);
49 if (!gpio_ops) {
50 printk(BIOS_WARNING, "IPMI: specified gpio device is missing gpio ops!\n");
51 return;
52 }
53
54 /* Set POST Complete pin. The `invert` field controls the polarity. */
55 gpio_ops->output(conf->post_complete_gpio, conf->post_complete_invert ^ 1);
56
57 printk(BIOS_DEBUG, "BMC: POST complete gpio set\n");
58}
59
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020060static void ipmi_kcs_init(struct device *dev)
61{
62 struct ipmi_devid_rsp rsp;
63 uint32_t man_id = 0, prod_id = 0;
Bill XIEf880eb02020-11-10 15:20:24 +080064 struct drivers_ipmi_config *conf = dev->chip_info;
Michael Niewöhner31830d32020-11-23 13:24:40 +010065 const struct gpio_operations *gpio_ops;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020066
Bill XIEf880eb02020-11-10 15:20:24 +080067 if (!conf) {
68 printk(BIOS_WARNING, "IPMI: chip_info is missing! Skip init.\n");
69 return;
70 }
Patrick Rudolph3d41a132019-07-22 16:31:35 +020071
Michael Niewöhner31830d32020-11-23 13:24:40 +010072 if (conf->bmc_jumper_gpio) {
73 gpio_ops = dev_get_gpio_ops(conf->gpio_dev);
74 if (!gpio_ops) {
75 printk(BIOS_WARNING, "IPMI: gpio device is missing gpio ops!\n");
76 } else {
77 /* Get jumper value and set device state accordingly */
78 dev->enabled = gpio_ops->get(conf->bmc_jumper_gpio);
79 if (!dev->enabled)
80 printk(BIOS_INFO, "IPMI: Disabled by jumper\n");
81 }
82 }
83
84 if (!dev->enabled)
85 return;
86
87 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
88
Michael Niewöhner548a3dc2020-11-24 12:45:07 +010089 /* Set up boot state callback for POST_COMPLETE# */
90 if (conf->post_complete_gpio) {
91 bscb_post_complete.callback = bmc_set_post_complete_gpio_callback;
92 bscb_post_complete.arg = conf;
93 boot_state_sched_on_entry(&bscb_post_complete, BS_PAYLOAD_BOOT);
94 }
95
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020096 /* Get IPMI version for ACPI and SMBIOS */
Bill XIEf880eb02020-11-10 15:20:24 +080097 if (conf->wait_for_bmc && conf->bmc_boot_timeout) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +020098 struct stopwatch sw;
99 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
Paul Menzel01c18152020-06-02 11:12:30 +0200100 printk(BIOS_INFO, "IPMI: Waiting for BMC...\n");
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200101
102 while (!stopwatch_expired(&sw)) {
103 if (inb(dev->path.pnp.port) != 0xff)
104 break;
105 mdelay(100);
106 }
107 if (stopwatch_expired(&sw)) {
108 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
109 /* Don't write tables if communication failed */
110 dev->enabled = 0;
111 return;
112 }
113 }
114
Sergii Dmytrukef7dd5d2021-10-22 01:02:32 +0300115 if (ipmi_process_self_test_result(dev))
Morgan Jang50155022019-11-06 10:24:47 +0800116 /* Don't write tables if communication failed */
117 dev->enabled = 0;
Morgan Jang50155022019-11-06 10:24:47 +0800118
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200119 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200120 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200121 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
122 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
123
Tim Chu278ad212020-10-06 01:40:53 -0700124 bmc_revision_major = rsp.fw_rev1;
125 bmc_revision_minor = rsp.fw_rev2;
126
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200127 memcpy(&man_id, rsp.manufacturer_id,
128 sizeof(rsp.manufacturer_id));
129
130 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
131
132 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
133 man_id, prod_id);
134
135 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
136 ipmi_revision_major, ipmi_revision_minor);
137 } else {
138 /* Don't write tables if communication failed */
139 dev->enabled = 0;
140 }
Patrick Rudolph09cdeba2019-12-30 14:40:04 +0100141
142 if (!dev->enabled)
143 return;
144
145 if (CONFIG(DRIVERS_IPMI_SUPERMICRO_OEM))
146 supermicro_ipmi_oem(dev->path.pnp.port);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200147}
148
149#if CONFIG(HAVE_ACPI_TABLES)
150static uint32_t uid_cnt = 0;
151
152static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700153ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200154 struct acpi_rsdp *rsdp)
155{
Bill XIEf880eb02020-11-10 15:20:24 +0800156 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200157 struct acpi_spmi *spmi;
158 s8 gpe_interrupt = -1;
159 u32 apic_interrupt = 0;
160 acpi_addr_t addr = {
161 .space_id = ACPI_ADDRESS_SPACE_IO,
162 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
163 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200164 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200165 };
166
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200167 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
168 case 4:
169 addr.bit_offset = 32;
170 break;
171 case 16:
172 addr.bit_offset = 128;
173 break;
174 default:
175 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
Arthur Heymansfff20212021-03-15 14:56:16 +0100176 __fallthrough;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200177 case 1:
178 addr.bit_offset = 8;
179 break;
180 }
181
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200182 current = ALIGN_UP(current, 8);
183 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
184 spmi = (struct acpi_spmi *)current;
185
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200186 if (conf) {
187 if (conf->have_gpe)
188 gpe_interrupt = conf->gpe_interrupt;
189 if (conf->have_apic)
190 apic_interrupt = conf->apic_interrupt;
Bill XIEf880eb02020-11-10 15:20:24 +0800191
192 /* Use command to get UID from ipmi_ssdt */
193 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
194 (ipmi_revision_minor << 4), &addr,
195 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
196 conf->uid);
197
198 acpi_add_table(rsdp, spmi);
199
200 current += spmi->header.length;
201 } else {
202 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200203 }
204
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200205 return current;
206}
207
Furquan Shaikh7536a392020-04-24 21:59:21 -0700208static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200209{
210 const char *scope = acpi_device_scope(dev);
Bill XIEf880eb02020-11-10 15:20:24 +0800211 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200212
213 if (!scope) {
214 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
215 dev_path(dev));
216 return;
217 }
218
Bill XIEf880eb02020-11-10 15:20:24 +0800219 if (!conf) {
220 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
221 return;
222 }
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200223
224 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700225 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200226
227 /* write SPMI device */
228 acpigen_write_scope(scope);
229 acpigen_write_device("SPMI");
230 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100231 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700232 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200233 acpigen_write_STA(0xf);
234 acpigen_write_name("_CRS");
235 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200236 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
237 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
238 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200239
Bill XIEf880eb02020-11-10 15:20:24 +0800240 // FIXME: is that correct?
241 if (conf->have_apic)
242 acpigen_write_irq(1 << conf->apic_interrupt);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200243
244 acpigen_write_resourcetemplate_footer();
245
246 acpigen_write_method("_IFT", 0);
247 acpigen_write_return_byte(1); // KCS
248 acpigen_pop_len();
249
250 acpigen_write_method("_SRV", 0);
251 acpigen_write_return_integer((ipmi_revision_major << 8) |
252 (ipmi_revision_minor << 4));
253 acpigen_pop_len();
254
255 acpigen_pop_len(); /* pop device */
256 acpigen_pop_len(); /* pop scope */
257}
258#endif
259
Tim Chu278ad212020-10-06 01:40:53 -0700260void ipmi_bmc_version(uint8_t *ipmi_bmc_major_revision, uint8_t *ipmi_bmc_minor_revision)
261{
262 if (!bmc_revision_major || !bmc_revision_minor) {
263 printk(BIOS_ERR, "IPMI: BMC revision missing\n");
264 *ipmi_bmc_major_revision = 0;
265 *ipmi_bmc_minor_revision = 0;
266 } else {
267 *ipmi_bmc_major_revision = bmc_revision_major;
268 *ipmi_bmc_minor_revision = bmc_revision_minor;
269 }
270}
271
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200272#if CONFIG(GENERATE_SMBIOS_TABLES)
273static int ipmi_smbios_data(struct device *dev, int *handle,
274 unsigned long *current)
275{
Bill XIEf880eb02020-11-10 15:20:24 +0800276 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200277 u8 nv_storage = 0xff;
278 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200279 u8 register_spacing;
280
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200281 int len = 0;
282
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200283 if (conf) {
284 if (conf->have_nv_storage)
285 nv_storage = conf->nv_storage_device_address;
286 i2c_address = conf->bmc_i2c_address;
287 }
288
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200289 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
290 case 4:
291 register_spacing = 1 << 6;
292 break;
293 case 16:
294 register_spacing = 2 << 6;
295 break;
296 default:
297 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
Arthur Heymansfff20212021-03-15 14:56:16 +0100298 __fallthrough;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200299 case 1:
300 register_spacing = 0 << 6;
301 break;
302 }
303
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200304 // add IPMI Device Information
305 len += smbios_write_type38(
306 current, handle,
307 SMBIOS_BMC_INTERFACE_KCS,
308 ipmi_revision_minor | (ipmi_revision_major << 4),
309 i2c_address, // I2C address
310 nv_storage, // NV storage
311 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200312 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200313 0); // no IRQ
314
Angel Pons6a73b242021-09-03 12:18:10 +0200315 len += get_smbios_data(dev, handle, current);
316
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200317 return len;
318}
319#endif
320
321static void ipmi_set_resources(struct device *dev)
322{
323 struct resource *res;
324
325 for (res = dev->resource_list; res; res = res->next) {
326 if (!(res->flags & IORESOURCE_ASSIGNED))
327 continue;
328
329 res->flags |= IORESOURCE_STORED;
330 report_resource_stored(dev, res, "");
331 }
332}
333
334static void ipmi_read_resources(struct device *dev)
335{
336 struct resource *res = new_resource(dev, 0);
337 res->base = dev->path.pnp.port;
338 res->size = 2;
339 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
340}
341
342static struct device_operations ops = {
343 .read_resources = ipmi_read_resources,
344 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200345 .init = ipmi_kcs_init,
346#if CONFIG(HAVE_ACPI_TABLES)
347 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200348 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200349#endif
350#if CONFIG(GENERATE_SMBIOS_TABLES)
351 .get_smbios_data = ipmi_smbios_data,
352#endif
353};
354
355static void enable_dev(struct device *dev)
356{
357 if (dev->path.type != DEVICE_PATH_PNP)
358 printk(BIOS_ERR, "%s: Unsupported device type\n",
359 dev_path(dev));
360 else if (dev->path.pnp.port & 1)
361 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
362 dev_path(dev));
363 else
364 dev->ops = &ops;
365}
366
367struct chip_operations drivers_ipmi_ops = {
368 CHIP_NAME("IPMI KCS")
369 .enable_dev = enable_dev,
370};