blob: f261934c96e9b49f4436c266b46529c5d83a4202 [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>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020012#include <console/console.h>
13#include <device/device.h>
Michael Niewöhner31830d32020-11-23 13:24:40 +010014#include <device/gpio.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020015#include <device/pnp.h>
16#if CONFIG(HAVE_ACPI_TABLES)
Furquan Shaikh76cedd22020-05-02 10:24:23 -070017#include <acpi/acpi.h>
18#include <acpi/acpigen.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020019#endif
20#if CONFIG(GENERATE_SMBIOS_TABLES)
21#include <smbios.h>
22#endif
23#include <version.h>
24#include <delay.h>
Patrick Rudolph3d41a132019-07-22 16:31:35 +020025#include <timer.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020026#include "ipmi_kcs.h"
Patrick Rudolph09cdeba2019-12-30 14:40:04 +010027#include "ipmi_supermicro_oem.h"
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020028#include "chip.h"
29
30/* 4 bit encoding */
31static u8 ipmi_revision_major = 0x1;
32static u8 ipmi_revision_minor = 0x0;
33
Tim Chu278ad212020-10-06 01:40:53 -070034static u8 bmc_revision_major = 0x0;
35static u8 bmc_revision_minor = 0x0;
36
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020037static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
38{
39 int ret;
40
41 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
42 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
43 sizeof(*rsp));
44 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
45 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
46 __func__, ret, rsp->resp.completion_code);
47 return 1;
48 }
49 if (ret != sizeof(*rsp)) {
50 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
51 return 1;
52 }
53 return 0;
54}
55
Morgan Jang50155022019-11-06 10:24:47 +080056static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
57{
58 int ret;
59
60 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
61 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
62 sizeof(*rsp));
63
64 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
65 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
66 __func__, ret, rsp->resp.completion_code);
67 return 1;
68 }
69 if (ret != sizeof(*rsp)) {
70 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
71 return 1;
72 }
73
74 return 0;
75}
76
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020077static void ipmi_kcs_init(struct device *dev)
78{
79 struct ipmi_devid_rsp rsp;
80 uint32_t man_id = 0, prod_id = 0;
Bill XIEf880eb02020-11-10 15:20:24 +080081 struct drivers_ipmi_config *conf = dev->chip_info;
Michael Niewöhner31830d32020-11-23 13:24:40 +010082 const struct gpio_operations *gpio_ops;
Bill XIEf880eb02020-11-10 15:20:24 +080083 struct ipmi_selftest_rsp selftestrsp = {0};
Morgan Jang50155022019-11-06 10:24:47 +080084 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020085
Bill XIEf880eb02020-11-10 15:20:24 +080086 if (!conf) {
87 printk(BIOS_WARNING, "IPMI: chip_info is missing! Skip init.\n");
88 return;
89 }
Patrick Rudolph3d41a132019-07-22 16:31:35 +020090
Michael Niewöhner31830d32020-11-23 13:24:40 +010091 if (conf->bmc_jumper_gpio) {
92 gpio_ops = dev_get_gpio_ops(conf->gpio_dev);
93 if (!gpio_ops) {
94 printk(BIOS_WARNING, "IPMI: gpio device is missing gpio ops!\n");
95 } else {
96 /* Get jumper value and set device state accordingly */
97 dev->enabled = gpio_ops->get(conf->bmc_jumper_gpio);
98 if (!dev->enabled)
99 printk(BIOS_INFO, "IPMI: Disabled by jumper\n");
100 }
101 }
102
103 if (!dev->enabled)
104 return;
105
106 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
107
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200108 /* Get IPMI version for ACPI and SMBIOS */
Bill XIEf880eb02020-11-10 15:20:24 +0800109 if (conf->wait_for_bmc && conf->bmc_boot_timeout) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200110 struct stopwatch sw;
111 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
Paul Menzel01c18152020-06-02 11:12:30 +0200112 printk(BIOS_INFO, "IPMI: Waiting for BMC...\n");
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200113
114 while (!stopwatch_expired(&sw)) {
115 if (inb(dev->path.pnp.port) != 0xff)
116 break;
117 mdelay(100);
118 }
119 if (stopwatch_expired(&sw)) {
120 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
121 /* Don't write tables if communication failed */
122 dev->enabled = 0;
123 return;
124 }
125 }
126
Morgan Jang50155022019-11-06 10:24:47 +0800127 printk(BIOS_INFO, "Get BMC self test result...");
128 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
129 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
130 break;
131
132 mdelay(1000);
133 }
134
135 switch (selftestrsp.result) {
136 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
137 printk(BIOS_DEBUG, "No Error\n");
138 break;
139 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
140 printk(BIOS_DEBUG, "Function Not Implemented\n");
141 break;
142 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
143 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
144 /* Don't write tables if communication failed */
145 dev->enabled = 0;
146 break;
147 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
148 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
149 /* Don't write tables if communication failed */
150 dev->enabled = 0;
151 break;
152 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
153 printk(BIOS_DEBUG, "Reserved\n");
154 break;
155
156 default: /* Other Device Specific Hardware Error */
157 printk(BIOS_ERR, "BMC: Device Specific Error\n");
158 /* Don't write tables if communication failed */
159 dev->enabled = 0;
160 break;
161 }
162
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200163 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200164 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200165 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
166 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
167
Tim Chu278ad212020-10-06 01:40:53 -0700168 bmc_revision_major = rsp.fw_rev1;
169 bmc_revision_minor = rsp.fw_rev2;
170
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200171 memcpy(&man_id, rsp.manufacturer_id,
172 sizeof(rsp.manufacturer_id));
173
174 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
175
176 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
177 man_id, prod_id);
178
179 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
180 ipmi_revision_major, ipmi_revision_minor);
181 } else {
182 /* Don't write tables if communication failed */
183 dev->enabled = 0;
184 }
Patrick Rudolph09cdeba2019-12-30 14:40:04 +0100185
186 if (!dev->enabled)
187 return;
188
189 if (CONFIG(DRIVERS_IPMI_SUPERMICRO_OEM))
190 supermicro_ipmi_oem(dev->path.pnp.port);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200191}
192
193#if CONFIG(HAVE_ACPI_TABLES)
194static uint32_t uid_cnt = 0;
195
196static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700197ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200198 struct acpi_rsdp *rsdp)
199{
Bill XIEf880eb02020-11-10 15:20:24 +0800200 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200201 struct acpi_spmi *spmi;
202 s8 gpe_interrupt = -1;
203 u32 apic_interrupt = 0;
204 acpi_addr_t addr = {
205 .space_id = ACPI_ADDRESS_SPACE_IO,
206 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
207 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200208 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200209 };
210
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200211 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
212 case 4:
213 addr.bit_offset = 32;
214 break;
215 case 16:
216 addr.bit_offset = 128;
217 break;
218 default:
219 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
220 /* fall through */
221 case 1:
222 addr.bit_offset = 8;
223 break;
224 }
225
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200226 current = ALIGN_UP(current, 8);
227 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
228 spmi = (struct acpi_spmi *)current;
229
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200230 if (conf) {
231 if (conf->have_gpe)
232 gpe_interrupt = conf->gpe_interrupt;
233 if (conf->have_apic)
234 apic_interrupt = conf->apic_interrupt;
Bill XIEf880eb02020-11-10 15:20:24 +0800235
236 /* Use command to get UID from ipmi_ssdt */
237 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
238 (ipmi_revision_minor << 4), &addr,
239 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
240 conf->uid);
241
242 acpi_add_table(rsdp, spmi);
243
244 current += spmi->header.length;
245 } else {
246 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200247 }
248
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200249 return current;
250}
251
Furquan Shaikh7536a392020-04-24 21:59:21 -0700252static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200253{
254 const char *scope = acpi_device_scope(dev);
Bill XIEf880eb02020-11-10 15:20:24 +0800255 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200256
257 if (!scope) {
258 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
259 dev_path(dev));
260 return;
261 }
262
Bill XIEf880eb02020-11-10 15:20:24 +0800263 if (!conf) {
264 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
265 return;
266 }
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200267
268 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700269 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200270
271 /* write SPMI device */
272 acpigen_write_scope(scope);
273 acpigen_write_device("SPMI");
274 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100275 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700276 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200277 acpigen_write_STA(0xf);
278 acpigen_write_name("_CRS");
279 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200280 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
281 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
282 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200283
Bill XIEf880eb02020-11-10 15:20:24 +0800284 // FIXME: is that correct?
285 if (conf->have_apic)
286 acpigen_write_irq(1 << conf->apic_interrupt);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200287
288 acpigen_write_resourcetemplate_footer();
289
290 acpigen_write_method("_IFT", 0);
291 acpigen_write_return_byte(1); // KCS
292 acpigen_pop_len();
293
294 acpigen_write_method("_SRV", 0);
295 acpigen_write_return_integer((ipmi_revision_major << 8) |
296 (ipmi_revision_minor << 4));
297 acpigen_pop_len();
298
299 acpigen_pop_len(); /* pop device */
300 acpigen_pop_len(); /* pop scope */
301}
302#endif
303
Tim Chu278ad212020-10-06 01:40:53 -0700304void ipmi_bmc_version(uint8_t *ipmi_bmc_major_revision, uint8_t *ipmi_bmc_minor_revision)
305{
306 if (!bmc_revision_major || !bmc_revision_minor) {
307 printk(BIOS_ERR, "IPMI: BMC revision missing\n");
308 *ipmi_bmc_major_revision = 0;
309 *ipmi_bmc_minor_revision = 0;
310 } else {
311 *ipmi_bmc_major_revision = bmc_revision_major;
312 *ipmi_bmc_minor_revision = bmc_revision_minor;
313 }
314}
315
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200316#if CONFIG(GENERATE_SMBIOS_TABLES)
317static int ipmi_smbios_data(struct device *dev, int *handle,
318 unsigned long *current)
319{
Bill XIEf880eb02020-11-10 15:20:24 +0800320 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200321 u8 nv_storage = 0xff;
322 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200323 u8 register_spacing;
324
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200325 int len = 0;
326
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200327 if (conf) {
328 if (conf->have_nv_storage)
329 nv_storage = conf->nv_storage_device_address;
330 i2c_address = conf->bmc_i2c_address;
331 }
332
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200333 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
334 case 4:
335 register_spacing = 1 << 6;
336 break;
337 case 16:
338 register_spacing = 2 << 6;
339 break;
340 default:
341 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
342 /* fall through */
343 case 1:
344 register_spacing = 0 << 6;
345 break;
346 }
347
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200348 // add IPMI Device Information
349 len += smbios_write_type38(
350 current, handle,
351 SMBIOS_BMC_INTERFACE_KCS,
352 ipmi_revision_minor | (ipmi_revision_major << 4),
353 i2c_address, // I2C address
354 nv_storage, // NV storage
355 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200356 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200357 0); // no IRQ
358
359 return len;
360}
361#endif
362
363static void ipmi_set_resources(struct device *dev)
364{
365 struct resource *res;
366
367 for (res = dev->resource_list; res; res = res->next) {
368 if (!(res->flags & IORESOURCE_ASSIGNED))
369 continue;
370
371 res->flags |= IORESOURCE_STORED;
372 report_resource_stored(dev, res, "");
373 }
374}
375
376static void ipmi_read_resources(struct device *dev)
377{
378 struct resource *res = new_resource(dev, 0);
379 res->base = dev->path.pnp.port;
380 res->size = 2;
381 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
382}
383
384static struct device_operations ops = {
385 .read_resources = ipmi_read_resources,
386 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200387 .init = ipmi_kcs_init,
388#if CONFIG(HAVE_ACPI_TABLES)
389 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200390 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200391#endif
392#if CONFIG(GENERATE_SMBIOS_TABLES)
393 .get_smbios_data = ipmi_smbios_data,
394#endif
395};
396
397static void enable_dev(struct device *dev)
398{
399 if (dev->path.type != DEVICE_PATH_PNP)
400 printk(BIOS_ERR, "%s: Unsupported device type\n",
401 dev_path(dev));
402 else if (dev->path.pnp.port & 1)
403 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
404 dev_path(dev));
405 else
406 dev->ops = &ops;
407}
408
409struct chip_operations drivers_ipmi_ops = {
410 CHIP_NAME("IPMI KCS")
411 .enable_dev = enable_dev,
412};