aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/mediatek/patches-5.15/432-drivers-spi-Add-support-for-dynamic-calibration.patch
blob: 4c980e9438d1eec11586d53fd5fedc0c94b2ef7a (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
From 2ade0172154e50c8a2bfd8634c6eff943cffea29 Mon Sep 17 00:00:00 2001
From: "SkyLake.Huang" <skylake.huang@mediatek.com>
Date: Thu, 23 Jun 2022 18:35:52 +0800
Subject: [PATCH 2/6] drivers: spi: Add support for dynamic calibration

Signed-off-by: SkyLake.Huang <skylake.huang@mediatek.com>
---
 drivers/spi/spi.c       | 137 ++++++++++++++++++++++++++++++++++++++++
 include/linux/spi/spi.h |  42 ++++++++++++
 2 files changed, 179 insertions(+)

--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1234,6 +1234,70 @@ static int spi_transfer_wait(struct spi_
 	return 0;
 }
 
+int spi_do_calibration(struct spi_controller *ctlr, struct spi_device *spi,
+	int (*cal_read)(void *priv, u32 *addr, int addrlen, u8 *buf, int readlen), void *drv_priv)
+{
+	int datalen = ctlr->cal_rule->datalen;
+	int addrlen = ctlr->cal_rule->addrlen;
+	u8 *buf;
+	int ret;
+	int i;
+	struct list_head *cal_head, *listptr;
+	struct spi_cal_target *target;
+
+	/* Calculate calibration result */
+	int hit_val, total_hit, origin;
+	bool hit;
+
+	/* Make sure we can start calibration */
+	if(!ctlr->cal_target || !ctlr->cal_rule || !ctlr->append_caldata)
+		return 0;
+
+	buf = kzalloc(datalen * sizeof(u8), GFP_KERNEL);
+	if(!buf)
+		return -ENOMEM;
+
+	ret = ctlr->append_caldata(ctlr);
+	if (ret)
+		goto cal_end;
+
+	cal_head = ctlr->cal_target;
+	list_for_each(listptr, cal_head) {
+		target = list_entry(listptr, struct spi_cal_target, list);
+
+		hit = false;
+		hit_val = 0;
+		total_hit = 0;
+		origin = *target->cal_item;
+
+		for(i=target->cal_min; i<=target->cal_max; i+=target->step) {
+			*target->cal_item = i;
+			ret = (*cal_read)(drv_priv, ctlr->cal_rule->addr, addrlen, buf, datalen);
+			if(ret)
+				break;
+			dev_dbg(&spi->dev, "controller cal item value: 0x%x\n", i);
+			if(memcmp(ctlr->cal_rule->match_data, buf, datalen * sizeof(u8)) == 0) {
+				hit = true;
+				hit_val += i;
+				total_hit++;
+				dev_dbg(&spi->dev, "golden data matches data read!\n");
+			}
+		}
+		if(hit) {
+			*target->cal_item = DIV_ROUND_CLOSEST(hit_val, total_hit);
+			dev_info(&spi->dev, "calibration result: 0x%x", *target->cal_item);
+		} else {
+			*target->cal_item = origin;
+			dev_warn(&spi->dev, "calibration failed, fallback to default: 0x%x", origin);
+		}
+	}
+
+cal_end:
+	kfree(buf);
+	return ret? ret: 0;
+}
+EXPORT_SYMBOL_GPL(spi_do_calibration);
+
 static void _spi_transfer_delay_ns(u32 ns)
 {
 	if (!ns)
@@ -2021,6 +2085,75 @@ void spi_flush_queue(struct spi_controll
 /*-------------------------------------------------------------------------*/
 
 #if defined(CONFIG_OF)
+static inline void alloc_cal_data(struct list_head **cal_target,
+	struct spi_cal_rule **cal_rule, bool enable)
+{
+	if(enable) {
+		*cal_target = kmalloc(sizeof(struct list_head), GFP_KERNEL);
+		INIT_LIST_HEAD(*cal_target);
+		*cal_rule = kmalloc(sizeof(struct spi_cal_rule), GFP_KERNEL);
+	} else {
+		kfree(*cal_target);
+		kfree(*cal_rule);
+	}
+}
+
+static int of_spi_parse_cal_dt(struct spi_controller *ctlr, struct spi_device *spi,
+			   struct device_node *nc)
+{
+	u32 value;
+	int rc;
+	const char *cal_mode;
+
+	rc = of_property_read_bool(nc, "spi-cal-enable");
+	if (rc)
+		alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, true);
+	else
+		return 0;
+
+	rc = of_property_read_string(nc, "spi-cal-mode", &cal_mode);
+	if(!rc) {
+		if(strcmp("read-data", cal_mode) == 0){
+			ctlr->cal_rule->mode = SPI_CAL_READ_DATA;
+		} else if(strcmp("read-pp", cal_mode) == 0) {
+			ctlr->cal_rule->mode = SPI_CAL_READ_PP;
+			return 0;
+		} else if(strcmp("read-sfdp", cal_mode) == 0){
+			ctlr->cal_rule->mode = SPI_CAL_READ_SFDP;
+			return 0;
+		}
+	} else
+		goto err;
+
+	ctlr->cal_rule->datalen = 0;
+	rc = of_property_read_u32(nc, "spi-cal-datalen", &value);
+	if(!rc && value > 0) {
+		ctlr->cal_rule->datalen = value;
+
+		ctlr->cal_rule->match_data = kzalloc(value * sizeof(u8), GFP_KERNEL);
+		rc = of_property_read_u8_array(nc, "spi-cal-data",
+				ctlr->cal_rule->match_data, value);
+		if(rc)
+			kfree(ctlr->cal_rule->match_data);
+	}
+
+	rc = of_property_read_u32(nc, "spi-cal-addrlen", &value);
+	if(!rc && value > 0) {
+		ctlr->cal_rule->addrlen = value;
+
+		ctlr->cal_rule->addr = kzalloc(value * sizeof(u32), GFP_KERNEL);
+		rc = of_property_read_u32_array(nc, "spi-cal-addr",
+				ctlr->cal_rule->addr, value);
+		if(rc)
+			kfree(ctlr->cal_rule->addr);
+	}
+	return 0;
+
+err:
+	alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, false);
+	return 0;
+}
+
 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
 			   struct device_node *nc)
 {
@@ -2139,6 +2272,10 @@ of_register_spi_device(struct spi_contro
 	if (rc)
 		goto err_out;
 
+	rc = of_spi_parse_cal_dt(ctlr, spi, nc);
+	if (rc)
+		goto err_out;
+
 	/* Store a pointer to the node in the device structure */
 	of_node_get(nc);
 	spi->dev.of_node = nc;
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -290,6 +290,40 @@ struct spi_driver {
 	struct device_driver	driver;
 };
 
+enum {
+	SPI_CAL_READ_DATA = 0,
+	SPI_CAL_READ_PP = 1, /* only for SPI-NAND */
+	SPI_CAL_READ_SFDP = 2, /* only for SPI-NOR */
+};
+
+struct nand_addr {
+	unsigned int lun;
+	unsigned int plane;
+	unsigned int eraseblock;
+	unsigned int page;
+	unsigned int dataoffs;
+};
+
+/**
+ * Read calibration rule from device dts node.
+ * Once calibration result matches the rule, we regard is as success.
+ */
+struct spi_cal_rule {
+	int datalen;
+	u8 *match_data;
+	int addrlen;
+	u32 *addr;
+	int mode;
+};
+
+struct spi_cal_target {
+	u32 *cal_item;
+	int cal_min; /* min of cal_item */
+	int cal_max; /* max of cal_item */
+	int step; /* Increase/decrease cal_item */
+	struct list_head list;
+};
+
 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
 {
 	return drv ? container_of(drv, struct spi_driver, driver) : NULL;
@@ -665,6 +699,11 @@ struct spi_controller {
 	void			*dummy_rx;
 	void			*dummy_tx;
 
+	/* For calibration */
+	int (*append_caldata)(struct spi_controller *ctlr);
+	struct list_head *cal_target;
+	struct spi_cal_rule *cal_rule;
+
 	int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
 
 	/*
@@ -1477,6 +1516,9 @@ spi_register_board_info(struct spi_board
 	{ return 0; }
 #endif
 
+extern int spi_do_calibration(struct spi_controller *ctlr,
+	struct spi_device *spi, int (*cal_read)(void *, u32 *, int, u8 *, int), void *drv_priv);
+
 /* If you're hotplugging an adapter with devices (parport, usb, etc)
  * use spi_new_device() to describe each device.  You can also call
  * spi_unregister_device() to start making that device vanish, but