1 // SPDX-License-Identifier: GPL-2.0
3 * Intel MAX 10 Board Management Controller chip
5 * Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
7 #include <linux/bitfield.h>
8 #include <linux/init.h>
9 #include <linux/mfd/core.h>
10 #include <linux/mfd/intel-m10-bmc.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/regmap.h>
14 #include <linux/spi/spi.h>
22 static struct mfd_cell m10bmc_d5005_subdevs[] = {
23 { .name = "d5005bmc-hwmon" },
26 static struct mfd_cell m10bmc_pacn3000_subdevs[] = {
27 { .name = "n3000bmc-hwmon" },
28 { .name = "n3000bmc-retimer" },
29 { .name = "n3000bmc-secure" },
32 static struct mfd_cell m10bmc_n5010_subdevs[] = {
33 { .name = "n5010bmc-hwmon" },
36 static const struct regmap_range m10bmc_regmap_range[] = {
37 regmap_reg_range(M10BMC_LEGACY_BUILD_VER, M10BMC_LEGACY_BUILD_VER),
38 regmap_reg_range(M10BMC_SYS_BASE, M10BMC_SYS_END),
39 regmap_reg_range(M10BMC_FLASH_BASE, M10BMC_FLASH_END),
42 static const struct regmap_access_table m10bmc_access_table = {
43 .yes_ranges = m10bmc_regmap_range,
44 .n_yes_ranges = ARRAY_SIZE(m10bmc_regmap_range),
47 static struct regmap_config intel_m10bmc_regmap_config = {
51 .wr_table = &m10bmc_access_table,
52 .rd_table = &m10bmc_access_table,
53 .max_register = M10BMC_MEM_END,
56 static ssize_t bmc_version_show(struct device *dev,
57 struct device_attribute *attr, char *buf)
59 struct intel_m10bmc *ddata = dev_get_drvdata(dev);
63 ret = m10bmc_sys_read(ddata, M10BMC_BUILD_VER, &val);
67 return sprintf(buf, "0x%x\n", val);
69 static DEVICE_ATTR_RO(bmc_version);
71 static ssize_t bmcfw_version_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 struct intel_m10bmc *ddata = dev_get_drvdata(dev);
78 ret = m10bmc_sys_read(ddata, NIOS2_FW_VERSION, &val);
82 return sprintf(buf, "0x%x\n", val);
84 static DEVICE_ATTR_RO(bmcfw_version);
86 static ssize_t mac_address_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
89 struct intel_m10bmc *max10 = dev_get_drvdata(dev);
90 unsigned int macaddr_low, macaddr_high;
93 ret = m10bmc_sys_read(max10, M10BMC_MAC_LOW, &macaddr_low);
97 ret = m10bmc_sys_read(max10, M10BMC_MAC_HIGH, &macaddr_high);
101 return sysfs_emit(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
102 (u8)FIELD_GET(M10BMC_MAC_BYTE1, macaddr_low),
103 (u8)FIELD_GET(M10BMC_MAC_BYTE2, macaddr_low),
104 (u8)FIELD_GET(M10BMC_MAC_BYTE3, macaddr_low),
105 (u8)FIELD_GET(M10BMC_MAC_BYTE4, macaddr_low),
106 (u8)FIELD_GET(M10BMC_MAC_BYTE5, macaddr_high),
107 (u8)FIELD_GET(M10BMC_MAC_BYTE6, macaddr_high));
109 static DEVICE_ATTR_RO(mac_address);
111 static ssize_t mac_count_show(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 struct intel_m10bmc *max10 = dev_get_drvdata(dev);
115 unsigned int macaddr_high;
118 ret = m10bmc_sys_read(max10, M10BMC_MAC_HIGH, &macaddr_high);
122 return sysfs_emit(buf, "%u\n",
123 (u8)FIELD_GET(M10BMC_MAC_COUNT, macaddr_high));
125 static DEVICE_ATTR_RO(mac_count);
127 static struct attribute *m10bmc_attrs[] = {
128 &dev_attr_bmc_version.attr,
129 &dev_attr_bmcfw_version.attr,
130 &dev_attr_mac_address.attr,
131 &dev_attr_mac_count.attr,
134 ATTRIBUTE_GROUPS(m10bmc);
136 static int check_m10bmc_version(struct intel_m10bmc *ddata)
142 * This check is to filter out the very old legacy BMC versions. In the
143 * old BMC chips, the BMC version info is stored in the old version
144 * register (M10BMC_LEGACY_BUILD_VER), so its read out value would have
145 * not been M10BMC_VER_LEGACY_INVALID (0xffffffff). But in new BMC
146 * chips that the driver supports, the value of this register should be
147 * M10BMC_VER_LEGACY_INVALID.
149 ret = m10bmc_raw_read(ddata, M10BMC_LEGACY_BUILD_VER, &v);
153 if (v != M10BMC_VER_LEGACY_INVALID) {
154 dev_err(ddata->dev, "bad version M10BMC detected\n");
161 static int intel_m10_bmc_spi_probe(struct spi_device *spi)
163 const struct spi_device_id *id = spi_get_device_id(spi);
164 struct device *dev = &spi->dev;
165 struct mfd_cell *cells;
166 struct intel_m10bmc *ddata;
169 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
176 devm_regmap_init_spi_avmm(spi, &intel_m10bmc_regmap_config);
177 if (IS_ERR(ddata->regmap)) {
178 ret = PTR_ERR(ddata->regmap);
179 dev_err(dev, "Failed to allocate regmap: %d\n", ret);
183 spi_set_drvdata(spi, ddata);
185 ret = check_m10bmc_version(ddata);
187 dev_err(dev, "Failed to identify m10bmc hardware\n");
191 switch (id->driver_data) {
193 cells = m10bmc_pacn3000_subdevs;
194 n_cell = ARRAY_SIZE(m10bmc_pacn3000_subdevs);
197 cells = m10bmc_d5005_subdevs;
198 n_cell = ARRAY_SIZE(m10bmc_d5005_subdevs);
201 cells = m10bmc_n5010_subdevs;
202 n_cell = ARRAY_SIZE(m10bmc_n5010_subdevs);
208 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, cells, n_cell,
211 dev_err(dev, "Failed to register sub-devices: %d\n", ret);
216 static const struct spi_device_id m10bmc_spi_id[] = {
217 { "m10-n3000", M10_N3000 },
218 { "m10-d5005", M10_D5005 },
219 { "m10-n5010", M10_N5010 },
222 MODULE_DEVICE_TABLE(spi, m10bmc_spi_id);
224 static struct spi_driver intel_m10bmc_spi_driver = {
226 .name = "intel-m10-bmc",
227 .dev_groups = m10bmc_groups,
229 .probe = intel_m10_bmc_spi_probe,
230 .id_table = m10bmc_spi_id,
232 module_spi_driver(intel_m10bmc_spi_driver);
234 MODULE_DESCRIPTION("Intel MAX 10 BMC Device Driver");
235 MODULE_AUTHOR("Intel Corporation");
236 MODULE_LICENSE("GPL v2");
237 MODULE_ALIAS("spi:intel-m10-bmc");