1 // SPDX-License-Identifier: GPL-2.0
3 * CZ.NIC's Turris Omnia MCU driver
8 #include <linux/array_size.h>
9 #include <linux/bits.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/hex.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/sysfs.h>
17 #include <linux/types.h>
19 #include <linux/turris-omnia-mcu-interface.h>
20 #include "turris-omnia-mcu.h"
22 #define OMNIA_FW_VERSION_LEN 20
23 #define OMNIA_FW_VERSION_HEX_LEN (2 * OMNIA_FW_VERSION_LEN + 1)
24 #define OMNIA_BOARD_INFO_LEN 16
26 int omnia_cmd_write_read(const struct i2c_client *client,
27 void *cmd, unsigned int cmd_len,
28 void *reply, unsigned int reply_len)
30 struct i2c_msg msgs[2];
33 msgs[0].addr = client->addr;
35 msgs[0].len = cmd_len;
40 msgs[1].addr = client->addr;
41 msgs[1].flags = I2C_M_RD;
42 msgs[1].len = reply_len;
47 ret = i2c_transfer(client->adapter, msgs, num);
56 static int omnia_get_version_hash(struct omnia_mcu *mcu, bool bootloader,
57 char version[static OMNIA_FW_VERSION_HEX_LEN])
59 u8 reply[OMNIA_FW_VERSION_LEN];
63 err = omnia_cmd_read(mcu->client,
64 bootloader ? OMNIA_CMD_GET_FW_VERSION_BOOT
65 : OMNIA_CMD_GET_FW_VERSION_APP,
66 reply, sizeof(reply));
70 p = bin2hex(version, reply, OMNIA_FW_VERSION_LEN);
76 static ssize_t fw_version_hash_show(struct device *dev, char *buf,
79 struct omnia_mcu *mcu = dev_get_drvdata(dev);
80 char version[OMNIA_FW_VERSION_HEX_LEN];
83 err = omnia_get_version_hash(mcu, bootloader, version);
87 return sysfs_emit(buf, "%s\n", version);
90 static ssize_t fw_version_hash_application_show(struct device *dev,
91 struct device_attribute *a,
94 return fw_version_hash_show(dev, buf, false);
96 static DEVICE_ATTR_RO(fw_version_hash_application);
98 static ssize_t fw_version_hash_bootloader_show(struct device *dev,
99 struct device_attribute *a,
102 return fw_version_hash_show(dev, buf, true);
104 static DEVICE_ATTR_RO(fw_version_hash_bootloader);
106 static ssize_t fw_features_show(struct device *dev, struct device_attribute *a,
109 struct omnia_mcu *mcu = dev_get_drvdata(dev);
111 return sysfs_emit(buf, "0x%x\n", mcu->features);
113 static DEVICE_ATTR_RO(fw_features);
115 static ssize_t mcu_type_show(struct device *dev, struct device_attribute *a,
118 struct omnia_mcu *mcu = dev_get_drvdata(dev);
120 return sysfs_emit(buf, "%s\n", mcu->type);
122 static DEVICE_ATTR_RO(mcu_type);
124 static ssize_t reset_selector_show(struct device *dev,
125 struct device_attribute *a, char *buf)
130 err = omnia_cmd_read_u8(to_i2c_client(dev), OMNIA_CMD_GET_RESET,
135 return sysfs_emit(buf, "%d\n", reply);
137 static DEVICE_ATTR_RO(reset_selector);
139 static ssize_t serial_number_show(struct device *dev,
140 struct device_attribute *a, char *buf)
142 struct omnia_mcu *mcu = dev_get_drvdata(dev);
144 return sysfs_emit(buf, "%016llX\n", mcu->board_serial_number);
146 static DEVICE_ATTR_RO(serial_number);
148 static ssize_t first_mac_address_show(struct device *dev,
149 struct device_attribute *a, char *buf)
151 struct omnia_mcu *mcu = dev_get_drvdata(dev);
153 return sysfs_emit(buf, "%pM\n", mcu->board_first_mac);
155 static DEVICE_ATTR_RO(first_mac_address);
157 static ssize_t board_revision_show(struct device *dev,
158 struct device_attribute *a, char *buf)
160 struct omnia_mcu *mcu = dev_get_drvdata(dev);
162 return sysfs_emit(buf, "%u\n", mcu->board_revision);
164 static DEVICE_ATTR_RO(board_revision);
166 static struct attribute *omnia_mcu_base_attrs[] = {
167 &dev_attr_fw_version_hash_application.attr,
168 &dev_attr_fw_version_hash_bootloader.attr,
169 &dev_attr_fw_features.attr,
170 &dev_attr_mcu_type.attr,
171 &dev_attr_reset_selector.attr,
172 &dev_attr_serial_number.attr,
173 &dev_attr_first_mac_address.attr,
174 &dev_attr_board_revision.attr,
178 static umode_t omnia_mcu_base_attrs_visible(struct kobject *kobj,
179 struct attribute *a, int n)
181 struct device *dev = kobj_to_dev(kobj);
182 struct omnia_mcu *mcu = dev_get_drvdata(dev);
184 if ((a == &dev_attr_serial_number.attr ||
185 a == &dev_attr_first_mac_address.attr ||
186 a == &dev_attr_board_revision.attr) &&
187 !(mcu->features & OMNIA_FEAT_BOARD_INFO))
193 static const struct attribute_group omnia_mcu_base_group = {
194 .attrs = omnia_mcu_base_attrs,
195 .is_visible = omnia_mcu_base_attrs_visible,
198 static const struct attribute_group *omnia_mcu_groups[] = {
199 &omnia_mcu_base_group,
200 #ifdef CONFIG_TURRIS_OMNIA_MCU_GPIO
201 &omnia_mcu_gpio_group,
203 #ifdef CONFIG_TURRIS_OMNIA_MCU_SYSOFF_WAKEUP
204 &omnia_mcu_poweroff_group,
209 static void omnia_mcu_print_version_hash(struct omnia_mcu *mcu, bool bootloader)
211 const char *type = bootloader ? "bootloader" : "application";
212 struct device *dev = &mcu->client->dev;
213 char version[OMNIA_FW_VERSION_HEX_LEN];
216 err = omnia_get_version_hash(mcu, bootloader, version);
218 dev_err(dev, "Cannot read MCU %s firmware version: %d\n",
223 dev_info(dev, "MCU %s firmware version hash: %s\n", type, version);
226 static const char *omnia_status_to_mcu_type(u16 status)
228 switch (status & OMNIA_STS_MCU_TYPE_MASK) {
229 case OMNIA_STS_MCU_TYPE_STM32:
231 case OMNIA_STS_MCU_TYPE_GD32:
233 case OMNIA_STS_MCU_TYPE_MKL:
240 static void omnia_info_missing_feature(struct device *dev, const char *feature)
243 "Your board's MCU firmware does not support the %s feature.\n",
247 static int omnia_mcu_read_features(struct omnia_mcu *mcu)
249 static const struct {
253 #define _DEF_FEAT(_n, _m) { OMNIA_FEAT_ ## _n, _m }
254 _DEF_FEAT(EXT_CMDS, "extended control and status"),
255 _DEF_FEAT(WDT_PING, "watchdog pinging"),
256 _DEF_FEAT(LED_STATE_EXT_MASK, "peripheral LED pins reading"),
257 _DEF_FEAT(NEW_INT_API, "new interrupt API"),
258 _DEF_FEAT(POWEROFF_WAKEUP, "poweroff and wakeup"),
259 _DEF_FEAT(TRNG, "true random number generator"),
262 struct i2c_client *client = mcu->client;
263 struct device *dev = &client->dev;
264 bool suggest_fw_upgrade = false;
268 /* status word holds MCU type, which we need below */
269 err = omnia_cmd_read_u16(client, OMNIA_CMD_GET_STATUS_WORD, &status);
274 * Check whether MCU firmware supports the OMNIA_CMD_GET_FEATURES
277 if (status & OMNIA_STS_FEATURES_SUPPORTED) {
278 /* try read 32-bit features */
279 err = omnia_cmd_read_u32(client, OMNIA_CMD_GET_FEATURES,
282 /* try read 16-bit features */
285 err = omnia_cmd_read_u16(client, OMNIA_CMD_GET_FEATURES,
290 mcu->features = features16;
292 if (mcu->features & OMNIA_FEAT_FROM_BIT_16_INVALID)
293 mcu->features &= GENMASK(15, 0);
297 "Your board's MCU firmware does not support feature reading.\n");
298 suggest_fw_upgrade = true;
301 mcu->type = omnia_status_to_mcu_type(status);
302 dev_info(dev, "MCU type %s%s\n", mcu->type,
303 (mcu->features & OMNIA_FEAT_PERIPH_MCU) ?
304 ", with peripheral resets wired" : "");
306 omnia_mcu_print_version_hash(mcu, true);
308 if (mcu->features & OMNIA_FEAT_BOOTLOADER)
310 "MCU is running bootloader firmware. Was firmware upgrade interrupted?\n");
312 omnia_mcu_print_version_hash(mcu, false);
314 for (unsigned int i = 0; i < ARRAY_SIZE(features); i++) {
315 if (mcu->features & features[i].mask)
318 omnia_info_missing_feature(dev, features[i].name);
319 suggest_fw_upgrade = true;
322 if (suggest_fw_upgrade)
324 "Consider upgrading MCU firmware with the omnia-mcutool utility.\n");
329 static int omnia_mcu_read_board_info(struct omnia_mcu *mcu)
331 u8 reply[1 + OMNIA_BOARD_INFO_LEN];
334 err = omnia_cmd_read(mcu->client, OMNIA_CMD_BOARD_INFO_GET, reply,
339 if (reply[0] != OMNIA_BOARD_INFO_LEN)
342 mcu->board_serial_number = get_unaligned_le64(&reply[1]);
344 /* we can't use ether_addr_copy() because reply is not u16-aligned */
345 memcpy(mcu->board_first_mac, &reply[9], sizeof(mcu->board_first_mac));
347 mcu->board_revision = reply[15];
352 static int omnia_mcu_probe(struct i2c_client *client)
354 struct device *dev = &client->dev;
355 struct omnia_mcu *mcu;
359 return dev_err_probe(dev, -EINVAL, "IRQ resource not found\n");
361 mcu = devm_kzalloc(dev, sizeof(*mcu), GFP_KERNEL);
365 mcu->client = client;
366 i2c_set_clientdata(client, mcu);
368 err = omnia_mcu_read_features(mcu);
370 return dev_err_probe(dev, err,
371 "Cannot determine MCU supported features\n");
373 if (mcu->features & OMNIA_FEAT_BOARD_INFO) {
374 err = omnia_mcu_read_board_info(mcu);
376 return dev_err_probe(dev, err,
377 "Cannot read board info\n");
380 err = omnia_mcu_register_sys_off_and_wakeup(mcu);
384 err = omnia_mcu_register_watchdog(mcu);
388 err = omnia_mcu_register_gpiochip(mcu);
392 return omnia_mcu_register_trng(mcu);
395 static const struct of_device_id of_omnia_mcu_match[] = {
396 { .compatible = "cznic,turris-omnia-mcu" },
400 static struct i2c_driver omnia_mcu_driver = {
401 .probe = omnia_mcu_probe,
403 .name = "turris-omnia-mcu",
404 .of_match_table = of_omnia_mcu_match,
405 .dev_groups = omnia_mcu_groups,
408 module_i2c_driver(omnia_mcu_driver);
411 MODULE_DESCRIPTION("CZ.NIC's Turris Omnia MCU");
412 MODULE_LICENSE("GPL");