blob: de68e23fcd948af0da0a4707e46b5bd44ac8c1e2 [file] [log] [blame]
Christophe Leroy3155b0a2023-04-04 12:42:15 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2010-2020 CS Group
4 * Charles Frey <charles.frey@c-s.fr>
5 * Florent Trinh Thai <florent.trinh-thai@c-s.fr>
6 * Christophe Leroy <christophe.leroy@c-s.fr>
7 *
8 * Common specific routines for the CS Group boards
9 */
10
Christophe Leroy4b6a5382023-04-05 16:05:36 +020011#include <dm.h>
Christophe Leroy3155b0a2023-04-04 12:42:15 +020012#include <fdt_support.h>
Christophe Leroy4b6a5382023-04-05 16:05:36 +020013#include <spi.h>
14
15#define SPI_EEPROM_READ 0x03
Christophe Leroy3155b0a2023-04-04 12:42:15 +020016
17static int fdt_set_node_and_value(void *blob, char *node, const char *prop,
18 void *var, int size)
19{
20 int ret, off;
21
22 off = fdt_path_offset(blob, node);
23
24 if (off < 0) {
25 printf("Cannot find %s node err:%s\n", node, fdt_strerror(off));
26
27 return off;
28 }
29
30 ret = fdt_setprop(blob, off, prop, var, size);
31
32 if (ret < 0)
33 printf("Cannot set %s/%s prop err: %s\n", node, prop, fdt_strerror(ret));
34
35 return ret;
36}
37
38/* Checks front/rear id and remove unneeded nodes from the blob */
39void ft_cleanup(void *blob, unsigned long id, const char *prop, const char *compatible)
40{
41 int off;
42
43 off = fdt_node_offset_by_compatible(blob, -1, compatible);
44
45 while (off != -FDT_ERR_NOTFOUND) {
46 const struct fdt_property *ids;
47 int nb_ids, idx;
48 int tmp = -1;
49
50 ids = fdt_get_property(blob, off, prop, &nb_ids);
51
52 for (idx = 0; idx < nb_ids; idx += 4) {
53 if (*((uint32_t *)&ids->data[idx]) == id)
54 break;
55 }
56
57 if (idx >= nb_ids)
58 fdt_del_node(blob, off);
59 else
60 tmp = off;
61
62 off = fdt_node_offset_by_compatible(blob, tmp, compatible);
63 }
64
65 fdt_set_node_and_value(blob, "/", prop, &id, sizeof(uint32_t));
66}
Christophe Leroy4b6a5382023-04-05 16:05:36 +020067
68int read_eeprom(u8 *din, int len)
69{
70 struct udevice *eeprom;
71 struct spi_slave *slave;
72 uchar dout[3] = {SPI_EEPROM_READ, 0, 0};
73 int ret;
74
75 ret = uclass_get_device(UCLASS_SPI, 0, &eeprom);
76 if (ret)
77 return ret;
78
79 ret = _spi_get_bus_and_cs(0, 0, 1000000, 0, "spi_generic_drv",
80 "generic_0:0", &eeprom, &slave);
81 if (ret)
82 return ret;
83
84 ret = spi_claim_bus(slave);
85
86 ret = spi_xfer(slave, sizeof(dout) << 3, dout, NULL, SPI_XFER_BEGIN);
87 if (ret)
88 return ret;
89
90 ret = spi_xfer(slave, len << 3, NULL, din, SPI_XFER_END);
91 if (ret)
92 return ret;
93
94 spi_release_bus(slave);
95
96 return 0;
97}