blob: 9d1cac8715960be5c5778a120b04d44d2027cc52 [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>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020027#include "ipmi_kcs.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
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020040static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
41{
42 int ret;
43
44 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
45 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
46 sizeof(*rsp));
47 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
48 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
49 __func__, ret, rsp->resp.completion_code);
50 return 1;
51 }
52 if (ret != sizeof(*rsp)) {
53 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
54 return 1;
55 }
56 return 0;
57}
58
Morgan Jang50155022019-11-06 10:24:47 +080059static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
60{
61 int ret;
62
63 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
64 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
65 sizeof(*rsp));
66
67 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
68 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
69 __func__, ret, rsp->resp.completion_code);
70 return 1;
71 }
72 if (ret != sizeof(*rsp)) {
73 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
74 return 1;
75 }
76
77 return 0;
78}
79
Michael Niewöhner548a3dc2020-11-24 12:45:07 +010080static void bmc_set_post_complete_gpio_callback(void *arg)
81{
82 struct drivers_ipmi_config *conf = arg;
83 const struct gpio_operations *gpio_ops;
84
85 if (!conf || !conf->post_complete_gpio)
86 return;
87
88 gpio_ops = dev_get_gpio_ops(conf->gpio_dev);
89 if (!gpio_ops) {
90 printk(BIOS_WARNING, "IPMI: specified gpio device is missing gpio ops!\n");
91 return;
92 }
93
94 /* Set POST Complete pin. The `invert` field controls the polarity. */
95 gpio_ops->output(conf->post_complete_gpio, conf->post_complete_invert ^ 1);
96
97 printk(BIOS_DEBUG, "BMC: POST complete gpio set\n");
98}
99
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200100static void ipmi_kcs_init(struct device *dev)
101{
102 struct ipmi_devid_rsp rsp;
103 uint32_t man_id = 0, prod_id = 0;
Bill XIEf880eb02020-11-10 15:20:24 +0800104 struct drivers_ipmi_config *conf = dev->chip_info;
Michael Niewöhner31830d32020-11-23 13:24:40 +0100105 const struct gpio_operations *gpio_ops;
Bill XIEf880eb02020-11-10 15:20:24 +0800106 struct ipmi_selftest_rsp selftestrsp = {0};
Morgan Jang50155022019-11-06 10:24:47 +0800107 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200108
Bill XIEf880eb02020-11-10 15:20:24 +0800109 if (!conf) {
110 printk(BIOS_WARNING, "IPMI: chip_info is missing! Skip init.\n");
111 return;
112 }
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200113
Michael Niewöhner31830d32020-11-23 13:24:40 +0100114 if (conf->bmc_jumper_gpio) {
115 gpio_ops = dev_get_gpio_ops(conf->gpio_dev);
116 if (!gpio_ops) {
117 printk(BIOS_WARNING, "IPMI: gpio device is missing gpio ops!\n");
118 } else {
119 /* Get jumper value and set device state accordingly */
120 dev->enabled = gpio_ops->get(conf->bmc_jumper_gpio);
121 if (!dev->enabled)
122 printk(BIOS_INFO, "IPMI: Disabled by jumper\n");
123 }
124 }
125
126 if (!dev->enabled)
127 return;
128
129 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
130
Michael Niewöhner548a3dc2020-11-24 12:45:07 +0100131 /* Set up boot state callback for POST_COMPLETE# */
132 if (conf->post_complete_gpio) {
133 bscb_post_complete.callback = bmc_set_post_complete_gpio_callback;
134 bscb_post_complete.arg = conf;
135 boot_state_sched_on_entry(&bscb_post_complete, BS_PAYLOAD_BOOT);
136 }
137
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200138 /* Get IPMI version for ACPI and SMBIOS */
Bill XIEf880eb02020-11-10 15:20:24 +0800139 if (conf->wait_for_bmc && conf->bmc_boot_timeout) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200140 struct stopwatch sw;
141 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
Paul Menzel01c18152020-06-02 11:12:30 +0200142 printk(BIOS_INFO, "IPMI: Waiting for BMC...\n");
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200143
144 while (!stopwatch_expired(&sw)) {
145 if (inb(dev->path.pnp.port) != 0xff)
146 break;
147 mdelay(100);
148 }
149 if (stopwatch_expired(&sw)) {
150 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
151 /* Don't write tables if communication failed */
152 dev->enabled = 0;
153 return;
154 }
155 }
156
Morgan Jang50155022019-11-06 10:24:47 +0800157 printk(BIOS_INFO, "Get BMC self test result...");
158 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
159 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
160 break;
161
162 mdelay(1000);
163 }
164
165 switch (selftestrsp.result) {
166 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
167 printk(BIOS_DEBUG, "No Error\n");
168 break;
169 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
170 printk(BIOS_DEBUG, "Function Not Implemented\n");
171 break;
172 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
173 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
174 /* Don't write tables if communication failed */
175 dev->enabled = 0;
176 break;
177 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
178 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
179 /* Don't write tables if communication failed */
180 dev->enabled = 0;
181 break;
182 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
183 printk(BIOS_DEBUG, "Reserved\n");
184 break;
185
186 default: /* Other Device Specific Hardware Error */
187 printk(BIOS_ERR, "BMC: Device Specific Error\n");
188 /* Don't write tables if communication failed */
189 dev->enabled = 0;
190 break;
191 }
192
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200193 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200194 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200195 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
196 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
197
Tim Chu278ad212020-10-06 01:40:53 -0700198 bmc_revision_major = rsp.fw_rev1;
199 bmc_revision_minor = rsp.fw_rev2;
200
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200201 memcpy(&man_id, rsp.manufacturer_id,
202 sizeof(rsp.manufacturer_id));
203
204 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
205
206 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
207 man_id, prod_id);
208
209 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
210 ipmi_revision_major, ipmi_revision_minor);
211 } else {
212 /* Don't write tables if communication failed */
213 dev->enabled = 0;
214 }
Patrick Rudolph09cdeba2019-12-30 14:40:04 +0100215
216 if (!dev->enabled)
217 return;
218
219 if (CONFIG(DRIVERS_IPMI_SUPERMICRO_OEM))
220 supermicro_ipmi_oem(dev->path.pnp.port);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200221}
222
223#if CONFIG(HAVE_ACPI_TABLES)
224static uint32_t uid_cnt = 0;
225
226static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700227ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200228 struct acpi_rsdp *rsdp)
229{
Bill XIEf880eb02020-11-10 15:20:24 +0800230 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200231 struct acpi_spmi *spmi;
232 s8 gpe_interrupt = -1;
233 u32 apic_interrupt = 0;
234 acpi_addr_t addr = {
235 .space_id = ACPI_ADDRESS_SPACE_IO,
236 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
237 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200238 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200239 };
240
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200241 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
242 case 4:
243 addr.bit_offset = 32;
244 break;
245 case 16:
246 addr.bit_offset = 128;
247 break;
248 default:
249 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
250 /* fall through */
251 case 1:
252 addr.bit_offset = 8;
253 break;
254 }
255
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200256 current = ALIGN_UP(current, 8);
257 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
258 spmi = (struct acpi_spmi *)current;
259
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200260 if (conf) {
261 if (conf->have_gpe)
262 gpe_interrupt = conf->gpe_interrupt;
263 if (conf->have_apic)
264 apic_interrupt = conf->apic_interrupt;
Bill XIEf880eb02020-11-10 15:20:24 +0800265
266 /* Use command to get UID from ipmi_ssdt */
267 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
268 (ipmi_revision_minor << 4), &addr,
269 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
270 conf->uid);
271
272 acpi_add_table(rsdp, spmi);
273
274 current += spmi->header.length;
275 } else {
276 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200277 }
278
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200279 return current;
280}
281
Furquan Shaikh7536a392020-04-24 21:59:21 -0700282static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200283{
284 const char *scope = acpi_device_scope(dev);
Bill XIEf880eb02020-11-10 15:20:24 +0800285 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200286
287 if (!scope) {
288 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
289 dev_path(dev));
290 return;
291 }
292
Bill XIEf880eb02020-11-10 15:20:24 +0800293 if (!conf) {
294 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
295 return;
296 }
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200297
298 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700299 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200300
301 /* write SPMI device */
302 acpigen_write_scope(scope);
303 acpigen_write_device("SPMI");
304 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100305 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700306 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200307 acpigen_write_STA(0xf);
308 acpigen_write_name("_CRS");
309 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200310 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
311 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
312 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200313
Bill XIEf880eb02020-11-10 15:20:24 +0800314 // FIXME: is that correct?
315 if (conf->have_apic)
316 acpigen_write_irq(1 << conf->apic_interrupt);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200317
318 acpigen_write_resourcetemplate_footer();
319
320 acpigen_write_method("_IFT", 0);
321 acpigen_write_return_byte(1); // KCS
322 acpigen_pop_len();
323
324 acpigen_write_method("_SRV", 0);
325 acpigen_write_return_integer((ipmi_revision_major << 8) |
326 (ipmi_revision_minor << 4));
327 acpigen_pop_len();
328
329 acpigen_pop_len(); /* pop device */
330 acpigen_pop_len(); /* pop scope */
331}
332#endif
333
Tim Chu278ad212020-10-06 01:40:53 -0700334void ipmi_bmc_version(uint8_t *ipmi_bmc_major_revision, uint8_t *ipmi_bmc_minor_revision)
335{
336 if (!bmc_revision_major || !bmc_revision_minor) {
337 printk(BIOS_ERR, "IPMI: BMC revision missing\n");
338 *ipmi_bmc_major_revision = 0;
339 *ipmi_bmc_minor_revision = 0;
340 } else {
341 *ipmi_bmc_major_revision = bmc_revision_major;
342 *ipmi_bmc_minor_revision = bmc_revision_minor;
343 }
344}
345
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200346#if CONFIG(GENERATE_SMBIOS_TABLES)
347static int ipmi_smbios_data(struct device *dev, int *handle,
348 unsigned long *current)
349{
Bill XIEf880eb02020-11-10 15:20:24 +0800350 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200351 u8 nv_storage = 0xff;
352 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200353 u8 register_spacing;
354
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200355 int len = 0;
356
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200357 if (conf) {
358 if (conf->have_nv_storage)
359 nv_storage = conf->nv_storage_device_address;
360 i2c_address = conf->bmc_i2c_address;
361 }
362
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200363 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
364 case 4:
365 register_spacing = 1 << 6;
366 break;
367 case 16:
368 register_spacing = 2 << 6;
369 break;
370 default:
371 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
372 /* fall through */
373 case 1:
374 register_spacing = 0 << 6;
375 break;
376 }
377
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200378 // add IPMI Device Information
379 len += smbios_write_type38(
380 current, handle,
381 SMBIOS_BMC_INTERFACE_KCS,
382 ipmi_revision_minor | (ipmi_revision_major << 4),
383 i2c_address, // I2C address
384 nv_storage, // NV storage
385 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200386 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200387 0); // no IRQ
388
389 return len;
390}
391#endif
392
393static void ipmi_set_resources(struct device *dev)
394{
395 struct resource *res;
396
397 for (res = dev->resource_list; res; res = res->next) {
398 if (!(res->flags & IORESOURCE_ASSIGNED))
399 continue;
400
401 res->flags |= IORESOURCE_STORED;
402 report_resource_stored(dev, res, "");
403 }
404}
405
406static void ipmi_read_resources(struct device *dev)
407{
408 struct resource *res = new_resource(dev, 0);
409 res->base = dev->path.pnp.port;
410 res->size = 2;
411 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
412}
413
414static struct device_operations ops = {
415 .read_resources = ipmi_read_resources,
416 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200417 .init = ipmi_kcs_init,
418#if CONFIG(HAVE_ACPI_TABLES)
419 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200420 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200421#endif
422#if CONFIG(GENERATE_SMBIOS_TABLES)
423 .get_smbios_data = ipmi_smbios_data,
424#endif
425};
426
427static void enable_dev(struct device *dev)
428{
429 if (dev->path.type != DEVICE_PATH_PNP)
430 printk(BIOS_ERR, "%s: Unsupported device type\n",
431 dev_path(dev));
432 else if (dev->path.pnp.port & 1)
433 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
434 dev_path(dev));
435 else
436 dev->ops = &ops;
437}
438
439struct chip_operations drivers_ipmi_ops = {
440 CHIP_NAME("IPMI KCS")
441 .enable_dev = enable_dev,
442};