1 /* SPDX-License-Identifier: GPL-2.0+
2 *
3 * Copyright (c) 2018 Microchip Technology, Inc.
4 *
5 */
6
7 #include <linux/err.h>
8 #include <dm.h>
9 #include <w1-eeprom.h>
10 #include <w1.h>
11
12 #define W1_F2D_READ_EEPROM 0xf0
13
14 #define EEP_SANDBOX_SAMPLE_MEM "this is a sample EEPROM memory string."
15
eep_sandbox_read_buf(struct udevice * dev,unsigned int offset,u8 * buf,unsigned int count)16 static int eep_sandbox_read_buf(struct udevice *dev, unsigned int offset,
17 u8 *buf, unsigned int count)
18 {
19 /* do not allow to copy more than our maximum sample string */
20 if (offset + count < strlen(EEP_SANDBOX_SAMPLE_MEM)) {
21 offset = 0;
22 count = strlen(EEP_SANDBOX_SAMPLE_MEM);
23 }
24 strncpy((char *)buf, EEP_SANDBOX_SAMPLE_MEM, count);
25
26 /*
27 * in case the w1 subsystem uses some different kind of sandbox testing,
28 * like randomized gpio values , we take the buffer from there
29 */
30
31 w1_reset_select(dev);
32
33 w1_write_byte(dev, W1_F2D_READ_EEPROM);
34 w1_write_byte(dev, offset & 0xff);
35 w1_write_byte(dev, offset >> 8);
36
37 w1_read_buf(dev, buf, count);
38
39 /*
40 * even if read buf from w1 fails, return success as we hardcoded
41 * the buffer.
42 */
43 return 0;
44 }
45
46 static const struct w1_eeprom_ops eep_sandbox_ops = {
47 .read_buf = eep_sandbox_read_buf,
48 };
49
50 static const struct udevice_id eep_sandbox_id[] = {
51 { .compatible = "sandbox,w1-eeprom", .data = W1_FAMILY_EEP_SANDBOX },
52 { },
53 };
54
55 U_BOOT_DRIVER(eep_sandbox) = {
56 .name = "eep_sandbox",
57 .id = UCLASS_W1_EEPROM,
58 .of_match = eep_sandbox_id,
59 .ops = &eep_sandbox_ops,
60 };
61