moveconfig: replace unsafe eval with asteval
[oweals/u-boot.git] / drivers / misc / microchip_flexcom.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2019, Microchip Technology, Inc.
4  * Author: Eugen Hristev <eugen.hristev@microchip.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <misc.h>
11 #include <asm/io.h>
12 #include <linux/err.h>
13
14 struct microchip_flexcom_regs {
15         u32 cr;
16 };
17
18 struct microchip_flexcom_platdata {
19         struct microchip_flexcom_regs *regs;
20         u32 flexcom_mode;
21 };
22
23 static int microchip_flexcom_ofdata_to_platdata(struct udevice *dev)
24 {
25         struct microchip_flexcom_platdata *plat = dev_get_platdata(dev);
26         int ret;
27
28         plat->regs = map_physmem(devfdt_get_addr(dev),
29                                  sizeof(struct microchip_flexcom_regs),
30                                 MAP_NOCACHE);
31
32         ret = dev_read_u32(dev, "atmel,flexcom-mode", &plat->flexcom_mode);
33
34         if (IS_ERR_VALUE(ret)) {
35                 debug("Missing atmel,flexcom-mode property\n");
36                 return ret;
37         }
38
39         /*
40          * The mode must have only 2 bits. If any other bits are set,
41          * the value is not supported.
42          */
43         if (plat->flexcom_mode & 0xfffffffc) {
44                 debug("Wrong atmel,flexcom-mode property\n");
45                 return -EINVAL;
46         }
47
48         writel(plat->flexcom_mode, &plat->regs->cr);
49
50         return 0;
51 }
52
53 static const struct udevice_id microchip_flexcom_ids[] = {
54         { .compatible = "atmel,sama5d2-flexcom" },
55         { .compatible = "microchip,flexcom" },
56         {}
57 };
58
59 U_BOOT_DRIVER(microchip_flexcom) = {
60         .name   = "microchip_flexcom",
61         .id     = UCLASS_MISC,
62         .of_match = microchip_flexcom_ids,
63         .ofdata_to_platdata = microchip_flexcom_ofdata_to_platdata,
64         .platdata_auto_alloc_size = sizeof(struct microchip_flexcom_platdata),
65 };