blob: e462e786a96e574e8cbaf9e4a661b6e02b9b7bd5 [file] [log] [blame]
Eugen Hristevb879c842018-09-18 10:35:33 +03001/* SPDX-License-Identifier: GPL-2.0+
2 *
3 * (C) Copyright 2018
4 * Microchip Technology, Inc.
5 * Eugen Hristev <eugen.hristev@microchip.com>
6 */
Eugen Hristevb879c842018-09-18 10:35:33 +03007#include <command.h>
Simon Glass614a9a22020-07-19 10:15:42 -06008#include <dm.h>
Eugen Hristevb879c842018-09-18 10:35:33 +03009#include <w1.h>
10#include <w1-eeprom.h>
11#include <dm/device-internal.h>
12
13static int w1_bus(void)
14{
15 struct udevice *bus, *dev;
16 int ret;
17
18 ret = w1_get_bus(0, &bus);
19 if (ret) {
20 printf("one wire interface not found\n");
21 return CMD_RET_FAILURE;
22 }
Simon Glass75e534b2020-12-16 21:20:07 -070023 printf("Bus %d:\t%s", dev_seq(bus), bus->name);
Eugen Hristevb879c842018-09-18 10:35:33 +030024 if (device_active(bus))
25 printf(" (active)");
26 printf("\n");
27
28 for (device_find_first_child(bus, &dev);
29 dev;
30 device_find_next_child(&dev)) {
31 ret = device_probe(dev);
32
Simon Glass75e534b2020-12-16 21:20:07 -070033 printf("\t%s (%d) uclass %s : ", dev->name, dev_seq(dev),
Eugen Hristevb879c842018-09-18 10:35:33 +030034 dev->uclass->uc_drv->name);
35
36 if (ret)
37 printf("device error\n");
38 else
39 printf("family 0x%x\n", w1_get_device_family(dev));
40 }
41 return CMD_RET_SUCCESS;
42}
43
44static int w1_read(int argc, char *const argv[])
45{
46 int bus_n = 0, dev_n = 0, offset = 0, len = 512;
47 int i;
48 struct udevice *bus, *dev;
49 int ret;
50 u8 buf[512];
51
52 if (argc > 2)
Simon Glassff9b9032021-07-24 09:03:30 -060053 bus_n = dectoul(argv[2], NULL);
Eugen Hristevb879c842018-09-18 10:35:33 +030054
55 if (argc > 3)
Simon Glassff9b9032021-07-24 09:03:30 -060056 dev_n = dectoul(argv[3], NULL);
Eugen Hristevb879c842018-09-18 10:35:33 +030057
58 if (argc > 4)
Simon Glassff9b9032021-07-24 09:03:30 -060059 offset = dectoul(argv[4], NULL);
Eugen Hristevb879c842018-09-18 10:35:33 +030060
61 if (argc > 5)
Simon Glassff9b9032021-07-24 09:03:30 -060062 len = dectoul(argv[5], NULL);
Eugen Hristevb879c842018-09-18 10:35:33 +030063
64 if (len > 512) {
65 printf("len needs to be <= 512\n");
66 return CMD_RET_FAILURE;
67 }
68
69 ret = w1_get_bus(bus_n, &bus);
70 if (ret) {
71 printf("one wire interface not found\n");
72 return CMD_RET_FAILURE;
73 }
74
75 for (device_find_first_child(bus, &dev), i = 0;
76 dev && i <= dev_n;
77 device_find_next_child(&dev), i++) {
78 ret = device_probe(dev);
79 if (!ret && i == dev_n)
80 break;
81 }
82
83 if (i != dev_n || ret || !dev) {
84 printf("invalid dev\n");
85 return CMD_RET_FAILURE;
86 }
87
88 if (strcmp(dev->uclass->uc_drv->name, "w1_eeprom")) {
89 printf("the device present on the interface is of unknown device class\n");
90 return CMD_RET_FAILURE;
91 }
92
93 ret = w1_eeprom_read_buf(dev, offset, (u8 *)buf, len);
94 if (ret) {
95 printf("error reading device %s\n", dev->name);
96 return CMD_RET_FAILURE;
97 }
98
99 for (i = 0; i < len; i++)
100 printf("%x", buf[i]);
101 printf("\n");
102
103 return CMD_RET_SUCCESS;
104}
105
Simon Glassed38aef2020-05-10 11:40:03 -0600106int do_w1(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Eugen Hristevb879c842018-09-18 10:35:33 +0300107{
108 if (argc < 2)
109 return CMD_RET_USAGE;
110
111 if (!strcmp(argv[1], "bus"))
112 return w1_bus();
113
114 if (!strcmp(argv[1], "read"))
115 return w1_read(argc, argv);
116
117 return CMD_RET_SUCCESS;
118}
119
120U_BOOT_CMD(w1, 6, 0, do_w1,
121 "onewire interface utility commands",
122 "bus - show onewire bus info (all)\n"
123 "w1 read [<bus> [<dev> [offset [length]]]]"
124 " - read from onewire device 'dev' on onewire bus 'bus'"
125 " starting from offset 'offset' and length 'length'\n"
126 " defaults: bus 0, dev 0, offset 0, length 512 bytes.");