blob: 180661b6287e3740e8f2f152a61d4337e7c7966b [file] [log] [blame]
Simon Glassc267cf72014-02-26 15:59:22 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * (C) Copyright 2012
5 * Marek Vasut <marex@denx.de>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <malloc.h>
13#include <errno.h>
14#include <asm/io.h>
15#include <dm/root.h>
16#include <dm/test.h>
17#include <dm/uclass-internal.h>
18
Heiko Schocherb74fcb42014-05-22 12:43:05 +020019static int display_succ(struct udevice *in, char *buf)
Simon Glassc267cf72014-02-26 15:59:22 -070020{
21 int len;
22 int ip = 0;
23 char local[16];
Heiko Schocherb74fcb42014-05-22 12:43:05 +020024 struct udevice *pos, *n, *prev = NULL;
Simon Glassc267cf72014-02-26 15:59:22 -070025
26 printf("%s- %s @ %08x", buf, in->name, map_to_sysmem(in));
27 if (in->flags & DM_FLAG_ACTIVATED)
28 puts(" - activated");
29 puts("\n");
30
31 if (list_empty(&in->child_head))
32 return 0;
33
34 len = strlen(buf);
35 strncpy(local, buf, sizeof(local));
36 snprintf(local + len, 2, "|");
37 if (len && local[len - 1] == '`')
38 local[len - 1] = ' ';
39
40 list_for_each_entry_safe(pos, n, &in->child_head, sibling_node) {
41 if (ip++)
42 display_succ(prev, local);
43 prev = pos;
44 }
45
46 snprintf(local + len, 2, "`");
47 display_succ(prev, local);
48
49 return 0;
50}
51
Heiko Schocherb74fcb42014-05-22 12:43:05 +020052static int dm_dump(struct udevice *dev)
Simon Glassc267cf72014-02-26 15:59:22 -070053{
54 if (!dev)
55 return -EINVAL;
56 return display_succ(dev, "");
57}
58
59static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc,
60 char * const argv[])
61{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020062 struct udevice *root;
Simon Glassc267cf72014-02-26 15:59:22 -070063
64 root = dm_root();
65 printf("ROOT %08x\n", map_to_sysmem(root));
66 return dm_dump(root);
67}
68
69static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc,
70 char * const argv[])
71{
72 struct uclass *uc;
73 int ret;
74 int id;
75
76 for (id = 0; id < UCLASS_COUNT; id++) {
Heiko Schocherb74fcb42014-05-22 12:43:05 +020077 struct udevice *dev;
Simon Glassc267cf72014-02-26 15:59:22 -070078
79 ret = uclass_get(id, &uc);
80 if (ret)
81 continue;
82
83 printf("uclass %d: %s\n", id, uc->uc_drv->name);
84 for (ret = uclass_first_device(id, &dev);
85 dev;
86 ret = uclass_next_device(&dev)) {
87 printf(" %s @ %08x:\n", dev->name,
88 map_to_sysmem(dev));
89 }
90 puts("\n");
91 }
92
93 return 0;
94}
95
Simon Glass17e9c782014-06-11 23:29:50 -060096#ifdef CONFIG_DM_TEST
Simon Glassc267cf72014-02-26 15:59:22 -070097static int do_dm_test(cmd_tbl_t *cmdtp, int flag, int argc,
98 char * const argv[])
99{
100 return dm_test_main();
101}
Simon Glass17e9c782014-06-11 23:29:50 -0600102#define TEST_HELP "\ndm test Run tests"
103#else
104#define TEST_HELP
105#endif
Simon Glassc267cf72014-02-26 15:59:22 -0700106
107static cmd_tbl_t test_commands[] = {
108 U_BOOT_CMD_MKENT(tree, 0, 1, do_dm_dump_all, "", ""),
109 U_BOOT_CMD_MKENT(uclass, 1, 1, do_dm_dump_uclass, "", ""),
Simon Glass17e9c782014-06-11 23:29:50 -0600110#ifdef CONFIG_DM_TEST
Simon Glassc267cf72014-02-26 15:59:22 -0700111 U_BOOT_CMD_MKENT(test, 1, 1, do_dm_test, "", ""),
Simon Glass17e9c782014-06-11 23:29:50 -0600112#endif
Simon Glassc267cf72014-02-26 15:59:22 -0700113};
114
115static int do_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
116{
117 cmd_tbl_t *test_cmd;
118 int ret;
119
120 if (argc != 2)
121 return CMD_RET_USAGE;
122 test_cmd = find_cmd_tbl(argv[1], test_commands,
123 ARRAY_SIZE(test_commands));
124 argc -= 2;
125 argv += 2;
126 if (!test_cmd || argc > test_cmd->maxargs)
127 return CMD_RET_USAGE;
128
129 ret = test_cmd->cmd(test_cmd, flag, argc, argv);
130
131 return cmd_process_error(test_cmd, ret);
132}
133
134U_BOOT_CMD(
135 dm, 2, 1, do_dm,
136 "Driver model low level access",
137 "tree Dump driver model tree\n"
Simon Glass17e9c782014-06-11 23:29:50 -0600138 "dm uclass Dump list of instances for each uclass"
139 TEST_HELP
Simon Glassc267cf72014-02-26 15:59:22 -0700140);