blob: 155d0323ee78477d2f52ff9785b1ff90d05c523d [file] [log] [blame]
Sebastian Reichel40e77512020-09-02 19:31:38 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Collabora
4 * (C) Copyright 2019 GE
5 */
6
Sebastian Reichel40e77512020-09-02 19:31:38 +02007#include <bootcount.h>
8#include <dm.h>
9#include <spi_flash.h>
10
11static const u8 bootcount_magic = 0xbc;
12
13struct bootcount_spi_flash_priv {
14 struct udevice *spi_flash;
15 u32 offset;
16};
17
18static int bootcount_spi_flash_update(struct udevice *dev, u32 offset, u32 len, const void *buf)
19{
20 struct spi_flash *flash = dev_get_uclass_priv(dev);
21 u32 sector_size = flash->sector_size;
22 u32 sector_offset = offset % sector_size;
23 u32 sector = offset - sector_offset;
24 int err = 0;
25
26 /* code only supports updating a single sector */
27 if (sector_offset + len > sector_size)
28 return -ENOSYS;
29
30 u8 *buffer = malloc(sector_size);
31 if (!buffer)
32 return -ENOMEM;
33
34 err = spi_flash_read_dm(dev, sector, sector_size, buffer);
35 if (err < 0)
36 goto out;
37
38 memcpy(buffer + sector_offset, buf, len);
39
40 err = spi_flash_erase_dm(dev, sector, sector_size);
41 if (err < 0)
42 goto out;
43
44 err = spi_flash_write_dm(dev, sector, sector_size, buffer);
45 if (err < 0)
46 goto out;
47
48out:
49 free(buffer);
50 return err;
51}
52
53static int bootcount_spi_flash_set(struct udevice *dev, const u32 a)
54{
55 struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
56 const u16 val = bootcount_magic << 8 | (a & 0xff);
57
58 if (bootcount_spi_flash_update(priv->spi_flash, priv->offset, 2, &val) < 0) {
59 debug("%s: write failed\n", __func__);
60 return -EIO;
61 }
62
63 return 0;
64}
65
66static int bootcount_spi_flash_get(struct udevice *dev, u32 *a)
67{
68 struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
69 u16 val;
70
71 if (spi_flash_read_dm(priv->spi_flash, priv->offset, 2, &val) < 0) {
72 debug("%s: read failed\n", __func__);
73 return -EIO;
74 }
75
76 if (val >> 8 == bootcount_magic) {
77 *a = val & 0xff;
78 return 0;
79 }
80
81 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
82 return -EIO;
83}
84
85static int bootcount_spi_flash_probe(struct udevice *dev)
86{
87 struct ofnode_phandle_args phandle_args;
88 struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
89 struct udevice *spi_flash;
90
91 if (dev_read_phandle_with_args(dev, "spi-flash", NULL, 0, 0, &phandle_args)) {
92 debug("%s: spi-flash backing device not specified\n", dev->name);
93 return -ENOENT;
94 }
95
96 if (uclass_get_device_by_ofnode(UCLASS_SPI_FLASH, phandle_args.node, &spi_flash)) {
97 debug("%s: could not get backing device\n", dev->name);
98 return -ENODEV;
99 }
100
101 priv->spi_flash = spi_flash;
102 priv->offset = dev_read_u32_default(dev, "offset", 0);
103
104 return 0;
105}
106
107static const struct bootcount_ops bootcount_spi_flash_ops = {
108 .get = bootcount_spi_flash_get,
109 .set = bootcount_spi_flash_set,
110};
111
112static const struct udevice_id bootcount_spi_flash_ids[] = {
113 { .compatible = "u-boot,bootcount-spi-flash" },
114 { }
115};
116
117U_BOOT_DRIVER(bootcount_spi_flash) = {
118 .name = "bootcount-spi-flash",
119 .id = UCLASS_BOOTCOUNT,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700120 .priv_auto = sizeof(struct bootcount_spi_flash_priv),
Sebastian Reichel40e77512020-09-02 19:31:38 +0200121 .probe = bootcount_spi_flash_probe,
122 .of_match = bootcount_spi_flash_ids,
123 .ops = &bootcount_spi_flash_ops,
124};