blob: 26980d209f4e8dede78d0d861f95a10ce0dd8232 [file] [log] [blame]
Simon Glass06811952014-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
Simon Glassfffa24d2014-07-23 06:55:10 -060019/**
20 * dm_display_line() - Display information about a single device
21 *
22 * Displays a single line of information with an option prefix
23 *
24 * @dev: Device to display
25 * @buf: Prefix to display at the start of the line
26 */
27static void dm_display_line(struct udevice *dev, char *buf)
28{
29 printf("%s- %c %s @ %08lx", buf,
30 dev->flags & DM_FLAG_ACTIVATED ? '*' : ' ',
31 dev->name, (ulong)map_to_sysmem(dev));
Simon Glassb7d66572014-07-23 06:55:13 -060032 if (dev->req_seq != -1)
33 printf(", %d", dev->req_seq);
Simon Glassfffa24d2014-07-23 06:55:10 -060034 puts("\n");
35}
36
Heiko Schocher54c5d082014-05-22 12:43:05 +020037static int display_succ(struct udevice *in, char *buf)
Simon Glass06811952014-02-26 15:59:22 -070038{
39 int len;
40 int ip = 0;
41 char local[16];
Heiko Schocher54c5d082014-05-22 12:43:05 +020042 struct udevice *pos, *n, *prev = NULL;
Simon Glass06811952014-02-26 15:59:22 -070043
Simon Glassfffa24d2014-07-23 06:55:10 -060044 dm_display_line(in, buf);
Simon Glass06811952014-02-26 15:59:22 -070045
46 if (list_empty(&in->child_head))
47 return 0;
48
49 len = strlen(buf);
50 strncpy(local, buf, sizeof(local));
51 snprintf(local + len, 2, "|");
52 if (len && local[len - 1] == '`')
53 local[len - 1] = ' ';
54
55 list_for_each_entry_safe(pos, n, &in->child_head, sibling_node) {
56 if (ip++)
57 display_succ(prev, local);
58 prev = pos;
59 }
60
61 snprintf(local + len, 2, "`");
62 display_succ(prev, local);
63
64 return 0;
65}
66
Heiko Schocher54c5d082014-05-22 12:43:05 +020067static int dm_dump(struct udevice *dev)
Simon Glass06811952014-02-26 15:59:22 -070068{
69 if (!dev)
70 return -EINVAL;
71 return display_succ(dev, "");
72}
73
74static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc,
75 char * const argv[])
76{
Heiko Schocher54c5d082014-05-22 12:43:05 +020077 struct udevice *root;
Simon Glass06811952014-02-26 15:59:22 -070078
79 root = dm_root();
Simon Glass184b1b72014-06-11 23:29:51 -060080 printf("ROOT %08lx\n", (ulong)map_to_sysmem(root));
Simon Glass06811952014-02-26 15:59:22 -070081 return dm_dump(root);
82}
83
84static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc,
85 char * const argv[])
86{
87 struct uclass *uc;
88 int ret;
89 int id;
90
91 for (id = 0; id < UCLASS_COUNT; id++) {
Heiko Schocher54c5d082014-05-22 12:43:05 +020092 struct udevice *dev;
Simon Glass06811952014-02-26 15:59:22 -070093
94 ret = uclass_get(id, &uc);
95 if (ret)
96 continue;
97
98 printf("uclass %d: %s\n", id, uc->uc_drv->name);
Simon Glass4e8bc212014-07-23 06:55:11 -060099 if (list_empty(&uc->dev_head))
100 continue;
101 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glassfffa24d2014-07-23 06:55:10 -0600102 dm_display_line(dev, "");
Simon Glass06811952014-02-26 15:59:22 -0700103 }
104 puts("\n");
105 }
106
107 return 0;
108}
109
Simon Glassb6a49a72014-06-11 23:29:50 -0600110#ifdef CONFIG_DM_TEST
Simon Glass06811952014-02-26 15:59:22 -0700111static int do_dm_test(cmd_tbl_t *cmdtp, int flag, int argc,
112 char * const argv[])
113{
114 return dm_test_main();
115}
Simon Glassb6a49a72014-06-11 23:29:50 -0600116#define TEST_HELP "\ndm test Run tests"
117#else
118#define TEST_HELP
119#endif
Simon Glass06811952014-02-26 15:59:22 -0700120
121static cmd_tbl_t test_commands[] = {
122 U_BOOT_CMD_MKENT(tree, 0, 1, do_dm_dump_all, "", ""),
123 U_BOOT_CMD_MKENT(uclass, 1, 1, do_dm_dump_uclass, "", ""),
Simon Glassb6a49a72014-06-11 23:29:50 -0600124#ifdef CONFIG_DM_TEST
Simon Glass06811952014-02-26 15:59:22 -0700125 U_BOOT_CMD_MKENT(test, 1, 1, do_dm_test, "", ""),
Simon Glassb6a49a72014-06-11 23:29:50 -0600126#endif
Simon Glass06811952014-02-26 15:59:22 -0700127};
128
129static int do_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
130{
131 cmd_tbl_t *test_cmd;
132 int ret;
133
134 if (argc != 2)
135 return CMD_RET_USAGE;
136 test_cmd = find_cmd_tbl(argv[1], test_commands,
137 ARRAY_SIZE(test_commands));
138 argc -= 2;
139 argv += 2;
140 if (!test_cmd || argc > test_cmd->maxargs)
141 return CMD_RET_USAGE;
142
143 ret = test_cmd->cmd(test_cmd, flag, argc, argv);
144
145 return cmd_process_error(test_cmd, ret);
146}
147
148U_BOOT_CMD(
149 dm, 2, 1, do_dm,
150 "Driver model low level access",
Simon Glassfffa24d2014-07-23 06:55:10 -0600151 "tree Dump driver model tree ('*' = activated)\n"
Simon Glassb6a49a72014-06-11 23:29:50 -0600152 "dm uclass Dump list of instances for each uclass"
153 TEST_HELP
Simon Glass06811952014-02-26 15:59:22 -0700154);