xref: /openbmc/linux/drivers/misc/cb710/debug.c (revision d6e0cbb1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  cb710/debug.c
4  *
5  *  Copyright by Michał Mirosław, 2008-2009
6  */
7 #include <linux/cb710.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 
11 #define CB710_REG_COUNT		0x80
12 
13 static const u16 allow[CB710_REG_COUNT/16] = {
14 	0xFFF0, 0xFFFF, 0xFFFF, 0xFFFF,
15 	0xFFF0, 0xFFFF, 0xFFFF, 0xFFFF,
16 };
17 static const char *const prefix[ARRAY_SIZE(allow)] = {
18 	"MMC", "MMC", "MMC", "MMC",
19 	"MS?", "MS?", "SM?", "SM?"
20 };
21 
22 static inline int allow_reg_read(unsigned block, unsigned offset, unsigned bits)
23 {
24 	unsigned mask = (1 << bits/8) - 1;
25 	offset *= bits/8;
26 	return ((allow[block] >> offset) & mask) == mask;
27 }
28 
29 #define CB710_READ_REGS_TEMPLATE(t)					\
30 static void cb710_read_regs_##t(void __iomem *iobase,			\
31 	u##t *reg, unsigned select)					\
32 {									\
33 	unsigned i, j;							\
34 									\
35 	for (i = 0; i < ARRAY_SIZE(allow); ++i, reg += 16/(t/8)) {	\
36 		if (!(select & (1 << i)))					\
37 			continue;					\
38 									\
39 		for (j = 0; j < 0x10/(t/8); ++j) {			\
40 			if (!allow_reg_read(i, j, t))			\
41 				continue;				\
42 			reg[j] = ioread##t(iobase			\
43 				+ (i << 4) + (j * (t/8)));		\
44 		}							\
45 	}								\
46 }
47 
48 static const char cb710_regf_8[] = "%02X";
49 static const char cb710_regf_16[] = "%04X";
50 static const char cb710_regf_32[] = "%08X";
51 static const char cb710_xes[] = "xxxxxxxx";
52 
53 #define CB710_DUMP_REGS_TEMPLATE(t)					\
54 static void cb710_dump_regs_##t(struct device *dev,			\
55 	const u##t *reg, unsigned select)				\
56 {									\
57 	const char *const xp = &cb710_xes[8 - t/4];			\
58 	const char *const format = cb710_regf_##t;			\
59 									\
60 	char msg[100], *p;						\
61 	unsigned i, j;							\
62 									\
63 	for (i = 0; i < ARRAY_SIZE(allow); ++i, reg += 16/(t/8)) {	\
64 		if (!(select & (1 << i)))				\
65 			continue;					\
66 		p = msg;						\
67 		for (j = 0; j < 0x10/(t/8); ++j) {			\
68 			*p++ = ' ';					\
69 			if (j == 8/(t/8))				\
70 				*p++ = ' ';				\
71 			if (allow_reg_read(i, j, t))			\
72 				p += sprintf(p, format, reg[j]);	\
73 			else						\
74 				p += sprintf(p, "%s", xp);		\
75 		}							\
76 		dev_dbg(dev, "%s 0x%02X %s\n", prefix[i], i << 4, msg);	\
77 	}								\
78 }
79 
80 #define CB710_READ_AND_DUMP_REGS_TEMPLATE(t)				\
81 static void cb710_read_and_dump_regs_##t(struct cb710_chip *chip,	\
82 	unsigned select)						\
83 {									\
84 	u##t regs[CB710_REG_COUNT/sizeof(u##t)];			\
85 									\
86 	memset(&regs, 0, sizeof(regs));					\
87 	cb710_read_regs_##t(chip->iobase, regs, select);		\
88 	cb710_dump_regs_##t(cb710_chip_dev(chip), regs, select);	\
89 }
90 
91 #define CB710_REG_ACCESS_TEMPLATES(t)		\
92   CB710_READ_REGS_TEMPLATE(t)			\
93   CB710_DUMP_REGS_TEMPLATE(t)			\
94   CB710_READ_AND_DUMP_REGS_TEMPLATE(t)
95 
96 CB710_REG_ACCESS_TEMPLATES(8)
97 CB710_REG_ACCESS_TEMPLATES(16)
98 CB710_REG_ACCESS_TEMPLATES(32)
99 
100 void cb710_dump_regs(struct cb710_chip *chip, unsigned select)
101 {
102 	if (!(select & CB710_DUMP_REGS_MASK))
103 		select = CB710_DUMP_REGS_ALL;
104 	if (!(select & CB710_DUMP_ACCESS_MASK))
105 		select |= CB710_DUMP_ACCESS_8;
106 
107 	if (select & CB710_DUMP_ACCESS_32)
108 		cb710_read_and_dump_regs_32(chip, select);
109 	if (select & CB710_DUMP_ACCESS_16)
110 		cb710_read_and_dump_regs_16(chip, select);
111 	if (select & CB710_DUMP_ACCESS_8)
112 		cb710_read_and_dump_regs_8(chip, select);
113 }
114 EXPORT_SYMBOL_GPL(cb710_dump_regs);
115 
116