aboutsummaryrefslogtreecommitdiff
path: root/cmd/rkmtd.c
blob: a870c11911085e9c557016a06b4392f30fbc24a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: GPL-2.0+
/*
 *
 * Driver interface derived from:
 * /cmd/host.c
 * Copyright (c) 2012, Google Inc.
 *
 * Copyright (C) 2023 Johan Jonker <jbx6244@gmail.com>
 */

#include <blk.h>
#include <command.h>
#include <dm.h>
#include <rkmtd.h>
#include <stdio.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>

static int do_rkmtd_bind(struct cmd_tbl *cmdtp, int flag, int argc,
			 char *const argv[])
{
	struct udevice *dev;
	const char *label;
	int ret;

	argc--;
	argv++;

	if (argc < 1)
		return CMD_RET_USAGE;

	if (argc > 1)
		return CMD_RET_USAGE;

	label = argv[0];
	ret = rkmtd_create_attach_mtd(label, &dev);
	if (ret) {
		printf("Cannot create device / bind mtd\n");
		return CMD_RET_FAILURE;
	}

	return 0;
}

static struct udevice *parse_rkmtd_label(const char *label)
{
	struct udevice *dev;

	dev = rkmtd_find_by_label(label);
	if (!dev) {
		int devnum;
		char *ep;

		devnum = hextoul(label, &ep);
		if (*ep ||
		    uclass_find_device_by_seq(UCLASS_RKMTD, devnum, &dev)) {
			printf("No such device '%s'\n", label);
			return NULL;
		}
	}

	return dev;
}

static int do_rkmtd_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
			   char *const argv[])
{
	struct udevice *dev;
	const char *label;
	int ret;

	if (argc < 2)
		return CMD_RET_USAGE;

	label = argv[1];
	dev = parse_rkmtd_label(label);
	if (!dev)
		return CMD_RET_FAILURE;

	ret = rkmtd_detach(dev);
	if (ret) {
		printf("Cannot detach mtd\n");
		return CMD_RET_FAILURE;
	}

	ret = device_unbind(dev);
	if (ret) {
		printf("Cannot unbind device '%s'\n", dev->name);
		return CMD_RET_FAILURE;
	}

	return 0;
}

static void show_rkmtd_dev(struct udevice *dev)
{
	struct rkmtd_dev *plat = dev_get_plat(dev);
	struct blk_desc *desc;
	struct udevice *blk;
	int ret;

	printf("%3d ", dev_seq(dev));

	ret = blk_get_from_parent(dev, &blk);
	if (ret)
		return;

	desc = dev_get_uclass_plat(blk);
	printf("%12lu %-15s\n", (unsigned long)desc->lba, plat->label);
}

static int do_rkmtd_info(struct cmd_tbl *cmdtp, int flag, int argc,
			 char *const argv[])
{
	struct udevice *dev;

	if (argc < 1)
		return CMD_RET_USAGE;

	dev = NULL;
	if (argc >= 2) {
		dev = parse_rkmtd_label(argv[1]);
		if (!dev)
			return CMD_RET_FAILURE;
	}

	printf("%3s %12s %-15s\n", "dev", "blocks", "label");
	if (dev) {
		show_rkmtd_dev(dev);
	} else {
		struct uclass *uc;

		uclass_id_foreach_dev(UCLASS_RKMTD, dev, uc)
			show_rkmtd_dev(dev);
	}

	return 0;
}

static int do_rkmtd_dev(struct cmd_tbl *cmdtp, int flag, int argc,
			char *const argv[])
{
	struct udevice *dev;
	const char *label;

	if (argc < 1 || argc > 3)
		return CMD_RET_USAGE;

	if (argc == 1) {
		struct rkmtd_dev *plat;

		dev = rkmtd_get_cur_dev();
		if (!dev) {
			printf("No current rkmtd device\n");
			return CMD_RET_FAILURE;
		}
		plat = dev_get_plat(dev);
		printf("Current rkmtd device: %d: %s\n", dev_seq(dev),
		       plat->label);
		return 0;
	}

	label = argv[1];
	dev = parse_rkmtd_label(argv[1]);
	if (!dev)
		return CMD_RET_FAILURE;

	rkmtd_set_cur_dev(dev);

	return 0;
}

static struct cmd_tbl cmd_rkmtd_sub[] = {
	U_BOOT_CMD_MKENT(bind, 4, 0, do_rkmtd_bind, "", ""),
	U_BOOT_CMD_MKENT(unbind, 4, 0, do_rkmtd_unbind, "", ""),
	U_BOOT_CMD_MKENT(info, 3, 0, do_rkmtd_info, "", ""),
	U_BOOT_CMD_MKENT(dev, 0, 1, do_rkmtd_dev, "", ""),
};

static int do_rkmtd(struct cmd_tbl *cmdtp, int flag, int argc,
		    char *const argv[])
{
	struct cmd_tbl *c;

	argc--;
	argv++;

	c = find_cmd_tbl(argv[0], cmd_rkmtd_sub, ARRAY_SIZE(cmd_rkmtd_sub));

	if (c)
		return c->cmd(cmdtp, flag, argc, argv);
	else
		return CMD_RET_USAGE;
}

U_BOOT_CMD(
	rkmtd, 8, 1, do_rkmtd,
	"Rockchip MTD sub-system",
	"bind <label>      - bind RKMTD device\n"
	"rkmtd unbind <label>    - unbind RKMTD device\n"
	"rkmtd info [<label>]    - show all available RKMTD devices\n"
	"rkmtd dev [<label>]     - show or set current RKMTD device\n"
);