summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-bsp/u-boot/files/0027-CPLD-u-boot-commands-support-for-PFR.patch
blob: b5f7ccf074ca3cb39949888fb48a42180c639308 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
From cdb62eb60de0b99276ff755b896fc51c8ed2606d Mon Sep 17 00:00:00 2001
From: AppaRao Puli <apparao.puli@linux.intel.com>
Date: Tue, 7 May 2019 11:26:35 +0530
Subject: [PATCH] CPLD u-boot commands support for PFR

Implemented the cpld command in u-boot for
communicating with PFR CPLD.

Tested:
Simulated test on different I2C bus and slave
as we don't have hardware available yet.
ast# cpld dump
*** Dumping CPLD Registers ***
0x0000 |  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
----------------------------------------------------------
0x0000 |  03 00 00 00 01 09 00 f5 01 09 19 cb 46 4c 45 --
0x0001 |  -- 52 4f 4e 49 43 53 cf 53 2d 31 31 30 30 41 44
0x0002 |  55 -- 30 2d 32 30 31 ca 47 38 34 30 32 -- 2d --
0x0003 |  30 37 c2 30 31 cc 45 58 57 44 36 34 39 30 30 38
.............................
ast# cpld read 0x00
CPLD read successful. Reg:0x00 Val:0x03
ast# cpld write 0x00 0x04
CPLD write successful. Reg:0x00 Val:0x04
ast# cpld read 0x00
CPLD read successful. Reg:0x00 Val:0x04
ast#

Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>

---
 cmd/Makefile |   1 +
 cmd/cpld.c   | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 245 insertions(+)
 create mode 100644 cmd/cpld.c

diff --git a/cmd/Makefile b/cmd/Makefile
index a1731be..c8ac0af 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -66,6 +66,7 @@ obj-$(CONFIG_CMD_FUSE) += fuse.o
 obj-$(CONFIG_CMD_GETTIME) += gettime.o
 obj-$(CONFIG_CMD_GPIO) += gpio.o
 obj-$(CONFIG_CMD_I2C) += i2c.o
+obj-$(CONFIG_CMD_I2C) += cpld.o
 obj-$(CONFIG_CMD_IOTRACE) += iotrace.o
 obj-$(CONFIG_CMD_HASH) += hash.o
 obj-$(CONFIG_CMD_IDE) += ide.o
diff --git a/cmd/cpld.c b/cmd/cpld.c
new file mode 100644
index 0000000..06b2e98
--- /dev/null
+++ b/cmd/cpld.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (c) 2018-2019 Intel Corporation
+ * Written by AppaRao Puli <apparao.puli@intel.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <command.h>
+#include <cli.h>
+#include <i2c.h>
+#include <errno.h>
+#include <linux/compiler.h>
+
+#define PFR_CPLD_I2C_BUSNO		4
+#define PFR_CPLD_SLAVE_ADDR		0xE0
+
+#define CPLD_READ_TIMEOUT_ATTEMPTS	5
+
+/* Some CPLD registers are self cleared after read.
+ * We should skip them reading to avoid functionality impact.*/
+/* TODO: Need to get this list from CPLD team. */
+static uchar cpld_reg_skip_read[] = {};
+
+static bool skip_cpld_reg_read(u32 reg)
+{
+	int size = ARRAY_SIZE(cpld_reg_skip_read);
+	for (int i = 0; i < size; i++) {
+		if (reg == cpld_reg_skip_read[i])
+			return true;
+	}
+
+	return false;
+}
+
+static int do_cpld_write(cmd_tbl_t *cmdtp, int flag, int argc,
+			 char *const argv[])
+{
+	int ret = 0;
+	int current_bus_no;
+	u32 reg_addr;
+	uchar value;
+	int chip = (PFR_CPLD_SLAVE_ADDR >> 1);
+
+	if (argc != 3)
+		return CMD_RET_USAGE;
+
+	reg_addr = simple_strtoul(argv[1], NULL, 16);
+	if (reg_addr > 0xFF) {
+		printf("Invalid register. Valid range[0x00-0xFF].");
+		return CMD_RET_FAILURE;
+	}
+	value = simple_strtoul(argv[2], NULL, 16);
+
+	/* Get current I2C bus number to restore later. */
+	current_bus_no = i2c_get_bus_num();
+
+	/* Set I2C bus number to PFR CPLD I2C bus. */
+	ret = i2c_set_bus_num(PFR_CPLD_I2C_BUSNO);
+	if (ret) {
+		printf("Failure changing bus number (%d)\n", ret);
+		ret = CMD_RET_FAILURE;
+		goto done;
+	}
+
+	ret = i2c_write(chip, reg_addr, 1, &value, 1);
+	if (ret) {
+		printf("Error writing the chip: %d\n", ret);
+		ret = CMD_RET_FAILURE;
+		goto done;
+	}
+
+	printf("CPLD write successful. Reg:0x%02x Val:0x%02x\n", reg_addr,
+	       value);
+
+done:
+	/* Restore I2C bus number */
+	if (i2c_set_bus_num(current_bus_no))
+		printf("Error in restoring bus number.\n");
+
+	return ret;
+}
+
+static int do_cpld_read(cmd_tbl_t *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	int ret = 0;
+	int current_bus_no;
+	u32 reg_addr;
+	uchar value[1];
+	int chip = (PFR_CPLD_SLAVE_ADDR >> 1);
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+
+	reg_addr = simple_strtoul(argv[1], NULL, 16);
+	if (reg_addr > 0xFF) {
+		printf("Invalid register. Valid range[0x00-0xFF].");
+		return CMD_RET_FAILURE;
+	}
+
+	/* Get current I2C bus number to restore later. */
+	current_bus_no = i2c_get_bus_num();
+
+	/* Set I2C bus number to PFR CPLD I2C bus. */
+	ret = i2c_set_bus_num(PFR_CPLD_I2C_BUSNO);
+	if (ret) {
+		printf("Failure changing bus number (%d)\n", ret);
+		ret = CMD_RET_FAILURE;
+		goto done;
+	}
+
+	if (skip_cpld_reg_read(reg_addr)) {
+		printf("CPLD register(0x%02x) reading is not allowed.\n",
+		       reg_addr);
+		ret = 0;
+		goto done;
+	}
+
+	ret = i2c_read(chip, reg_addr, 1, value, 1);
+	if (ret) {
+		printf("Error reading the chip: %d\n", ret);
+		ret = CMD_RET_FAILURE;
+		goto done;
+	}
+
+	printf("CPLD read successful. Reg:0x%02x Val:0x%02x\n", reg_addr,
+	       value[0]);
+
+done:
+	/* Restore I2C bus number */
+	if (i2c_set_bus_num(current_bus_no))
+		printf("Error in restoring bus number.\n");
+
+	return ret;
+}
+
+static int do_cpld_dump(cmd_tbl_t *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	int ret = 0;
+	int current_bus_no;
+	u32 reg_addr = 0x00;
+	uchar value[1];
+	int chip = (PFR_CPLD_SLAVE_ADDR >> 1);
+
+	if (argc != 1)
+		return CMD_RET_USAGE;
+
+	/* Get current I2C bus number to restore later. */
+	current_bus_no = i2c_get_bus_num();
+
+	/* Set I2C bus number to PFR CPLD I2C bus. */
+	ret = i2c_set_bus_num(PFR_CPLD_I2C_BUSNO);
+	if (ret) {
+		printf("Failure changing bus number (%d)\n", ret);
+		ret = CMD_RET_FAILURE;
+		goto done;
+	}
+
+	printf("*** Dumping CPLD Registers ***\n", reg_addr, value);
+	printf("0x%04x | ", reg_addr);
+	for (int i = 0; i < 0x10; i++)
+		printf(" %02x", i);
+	printf("\n----------------------------------------------------------\n");
+
+	while (reg_addr <= 0xFF) {
+		if ((reg_addr % 16) == 0)
+			printf("0x%04x | ", (reg_addr / 16));
+
+		if (skip_cpld_reg_read(reg_addr)) {
+			printf(" --");
+		} else {
+			int timeout = 0;
+			while (i2c_read(chip, reg_addr, 1, value, 1) != 0) {
+				if (timeout++ >= CPLD_READ_TIMEOUT_ATTEMPTS) {
+					printf("\nERROR: Reading the chip: %d\n",
+					       ret);
+					ret = CMD_RET_FAILURE;
+					goto done;
+				}
+				/* Need delay for I2C devices continous read */
+				mdelay(3 * timeout);
+			}
+			printf(" %02x", value[0]);
+		}
+
+		reg_addr++;
+		if ((reg_addr % 16) == 0)
+			printf("\n");
+	}
+
+done:
+	/* Restore I2C bus number */
+	if (i2c_set_bus_num(current_bus_no))
+		printf("Error in restoring bus number.\n");
+
+	return ret;
+}
+static cmd_tbl_t cmd_cpld_sub[] = {
+	U_BOOT_CMD_MKENT(dump, 1, 1, do_cpld_dump, "", ""),
+	U_BOOT_CMD_MKENT(read, 2, 1, do_cpld_read, "", ""),
+	U_BOOT_CMD_MKENT(write, 3, 1, do_cpld_write, "", "")
+};
+
+/**
+ * do_cpld() - Handle the "cpld" command-line command
+ * @cmdtp:	Command data struct pointer
+ * @flag:	Command flag
+ * @argc:	Command-line argument count
+ * @argv:	Array of command-line arguments
+ *
+ * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
+ * on error.
+ */
+static int do_cpld(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+	cmd_tbl_t *c = NULL;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	/* Strip off leading 'cpld' command argument */
+	argc--;
+	argv++;
+
+	if (argc)
+		c = find_cmd_tbl(argv[0], cmd_cpld_sub,
+				 ARRAY_SIZE(cmd_cpld_sub));
+
+	if (c)
+		return c->cmd(cmdtp, flag, argc, argv);
+	else
+		return CMD_RET_USAGE;
+}
+
+#ifdef CONFIG_SYS_LONGHELP
+static char cpld_help_text[] =
+	"cpld dump - Dump all CPLD registers.\n"
+	"cpld read <reg> - Read CPLD register.\n"
+	"cpld write <reg> <val> - Write CPLD register.\n";
+#endif
+
+U_BOOT_CMD(cpld, 4, 1, do_cpld, "PFR CPLD information", cpld_help_text);
-- 
2.7.4