blob: 8a71a9cf18f211854015048fa9fc47f91502fe80 [file] [log] [blame]
developerd9827922022-06-23 18:53:01 +08001From a84c53fce4ccd67c147dcbb2dcf4fdeceab05981 Mon Sep 17 00:00:00 2001
2From: "SkyLake.Huang" <skylake.huang@mediatek.com>
3Date: Thu, 23 Jun 2022 18:35:52 +0800
4Subject: [PATCH] drivers: spi: Add support for dynamic calibration
5
6Signed-off-by: SkyLake.Huang <skylake.huang@mediatek.com>
7---
8 drivers/spi/spi.c | 137 ++++++++++++++++++++++++++++++++++++++++
9 include/linux/spi/spi.h | 42 ++++++++++++
10 2 files changed, 179 insertions(+)
11
developerd9827922022-06-23 18:53:01 +080012--- a/drivers/spi/spi.c
13+++ b/drivers/spi/spi.c
developer2460ee72023-02-07 18:16:20 +080014@@ -1109,6 +1109,72 @@ static int spi_transfer_wait(struct spi_
developerd9827922022-06-23 18:53:01 +080015 return 0;
16 }
17
18+int spi_do_calibration(struct spi_controller *ctlr, struct spi_device *spi,
19+ int (*cal_read)(void *priv, u32 *addr, int addrlen, u8 *buf, int readlen), void *drv_priv)
20+{
21+ int datalen = ctlr->cal_rule->datalen;
22+ int addrlen = ctlr->cal_rule->addrlen;
23+ u8 *buf;
24+ int ret;
25+ int i;
26+ struct list_head *cal_head, *listptr;
27+ struct spi_cal_target *target;
28+
29+ /* Calculate calibration result */
30+ int hit_val, total_hit, origin;
31+ bool hit;
32+
33+ /* Make sure we can start calibration */
34+ if(!ctlr->cal_target || !ctlr->cal_rule || !ctlr->append_caldata)
developer2460ee72023-02-07 18:16:20 +080035+ return -EINVAL;
developerd9827922022-06-23 18:53:01 +080036+
37+ buf = kzalloc(datalen * sizeof(u8), GFP_KERNEL);
38+ if(!buf)
39+ return -ENOMEM;
40+
41+ ret = ctlr->append_caldata(ctlr);
42+ if (ret)
43+ goto cal_end;
44+
45+ cal_head = ctlr->cal_target;
46+ list_for_each(listptr, cal_head) {
47+ target = list_entry(listptr, struct spi_cal_target, list);
48+
49+ hit = false;
50+ hit_val = 0;
51+ total_hit = 0;
52+ origin = *target->cal_item;
53+
54+ for(i=target->cal_min; i<=target->cal_max; i+=target->step) {
55+ *target->cal_item = i;
56+ ret = (*cal_read)(drv_priv, ctlr->cal_rule->addr, addrlen, buf, datalen);
57+ if(ret)
58+ break;
59+ dev_dbg(&spi->dev, "controller cal item value: 0x%x\n", i);
60+ if(memcmp(ctlr->cal_rule->match_data, buf, datalen * sizeof(u8)) == 0) {
61+ hit = true;
62+ hit_val += i;
63+ total_hit++;
64+ dev_dbg(&spi->dev, "golden data matches data read!\n");
65+ }
66+ }
67+ if(hit) {
68+ *target->cal_item = DIV_ROUND_CLOSEST(hit_val, total_hit);
69+ dev_info(&spi->dev, "calibration result: 0x%x", *target->cal_item);
70+ } else {
71+ *target->cal_item = origin;
72+ dev_warn(&spi->dev, "calibration failed, fallback to default: 0x%x", origin);
developer2460ee72023-02-07 18:16:20 +080073+ ret = -EIO;
developerd9827922022-06-23 18:53:01 +080074+ }
75+ }
developer2460ee72023-02-07 18:16:20 +080076+ list_del(&target->list);
developerd9827922022-06-23 18:53:01 +080077+
78+cal_end:
79+ kfree(buf);
80+ return ret? ret: 0;
81+}
82+EXPORT_SYMBOL_GPL(spi_do_calibration);
83+
84 static void _spi_transfer_delay_ns(u32 ns)
85 {
86 if (!ns)
developer2460ee72023-02-07 18:16:20 +080087@@ -1720,6 +1786,75 @@ void spi_flush_queue(struct spi_controll
developerd9827922022-06-23 18:53:01 +080088 /*-------------------------------------------------------------------------*/
89
90 #if defined(CONFIG_OF)
91+static inline void alloc_cal_data(struct list_head **cal_target,
92+ struct spi_cal_rule **cal_rule, bool enable)
93+{
94+ if(enable) {
95+ *cal_target = kmalloc(sizeof(struct list_head), GFP_KERNEL);
96+ INIT_LIST_HEAD(*cal_target);
97+ *cal_rule = kmalloc(sizeof(struct spi_cal_rule), GFP_KERNEL);
98+ } else {
99+ kfree(*cal_target);
100+ kfree(*cal_rule);
101+ }
102+}
103+
104+static int of_spi_parse_cal_dt(struct spi_controller *ctlr, struct spi_device *spi,
105+ struct device_node *nc)
106+{
107+ u32 value;
108+ int rc;
109+ const char *cal_mode;
110+
111+ rc = of_property_read_bool(nc, "spi-cal-enable");
112+ if (rc)
113+ alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, true);
114+ else
115+ return 0;
116+
117+ rc = of_property_read_string(nc, "spi-cal-mode", &cal_mode);
118+ if(!rc) {
119+ if(strcmp("read-data", cal_mode) == 0){
120+ ctlr->cal_rule->mode = SPI_CAL_READ_DATA;
121+ } else if(strcmp("read-pp", cal_mode) == 0) {
122+ ctlr->cal_rule->mode = SPI_CAL_READ_PP;
123+ return 0;
124+ } else if(strcmp("read-sfdp", cal_mode) == 0){
125+ ctlr->cal_rule->mode = SPI_CAL_READ_SFDP;
126+ return 0;
127+ }
128+ } else
129+ goto err;
130+
131+ ctlr->cal_rule->datalen = 0;
132+ rc = of_property_read_u32(nc, "spi-cal-datalen", &value);
133+ if(!rc && value > 0) {
134+ ctlr->cal_rule->datalen = value;
135+
136+ ctlr->cal_rule->match_data = kzalloc(value * sizeof(u8), GFP_KERNEL);
137+ rc = of_property_read_u8_array(nc, "spi-cal-data",
138+ ctlr->cal_rule->match_data, value);
139+ if(rc)
140+ kfree(ctlr->cal_rule->match_data);
141+ }
142+
143+ rc = of_property_read_u32(nc, "spi-cal-addrlen", &value);
144+ if(!rc && value > 0) {
145+ ctlr->cal_rule->addrlen = value;
146+
147+ ctlr->cal_rule->addr = kzalloc(value * sizeof(u32), GFP_KERNEL);
148+ rc = of_property_read_u32_array(nc, "spi-cal-addr",
149+ ctlr->cal_rule->addr, value);
150+ if(rc)
151+ kfree(ctlr->cal_rule->addr);
152+ }
153+ return 0;
154+
155+err:
156+ alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, false);
157+ return 0;
158+}
159+
160 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
161 struct device_node *nc)
162 {
developer2460ee72023-02-07 18:16:20 +0800163@@ -1841,6 +1976,10 @@ of_register_spi_device(struct spi_contro
developerd9827922022-06-23 18:53:01 +0800164 if (rc)
165 goto err_out;
166
167+ rc = of_spi_parse_cal_dt(ctlr, spi, nc);
168+ if (rc)
169+ goto err_out;
170+
171 /* Store a pointer to the node in the device structure */
172 of_node_get(nc);
173 spi->dev.of_node = nc;
developerd9827922022-06-23 18:53:01 +0800174--- a/include/linux/spi/spi.h
175+++ b/include/linux/spi/spi.h
176@@ -264,6 +264,40 @@ struct spi_driver {
177 struct device_driver driver;
178 };
179
180+enum {
181+ SPI_CAL_READ_DATA = 0,
182+ SPI_CAL_READ_PP = 1, /* only for SPI-NAND */
183+ SPI_CAL_READ_SFDP = 2, /* only for SPI-NOR */
184+};
185+
186+struct nand_addr {
187+ unsigned int lun;
188+ unsigned int plane;
189+ unsigned int eraseblock;
190+ unsigned int page;
191+ unsigned int dataoffs;
192+};
193+
194+/**
195+ * Read calibration rule from device dts node.
196+ * Once calibration result matches the rule, we regard is as success.
197+ */
198+struct spi_cal_rule {
199+ int datalen;
200+ u8 *match_data;
201+ int addrlen;
202+ u32 *addr;
203+ int mode;
204+};
205+
206+struct spi_cal_target {
207+ u32 *cal_item;
208+ int cal_min; /* min of cal_item */
209+ int cal_max; /* max of cal_item */
210+ int step; /* Increase/decrease cal_item */
211+ struct list_head list;
212+};
213+
214 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
215 {
216 return drv ? container_of(drv, struct spi_driver, driver) : NULL;
217@@ -606,6 +640,11 @@ struct spi_controller {
218 void *dummy_rx;
219 void *dummy_tx;
220
221+ /* For calibration */
222+ int (*append_caldata)(struct spi_controller *ctlr);
223+ struct list_head *cal_target;
224+ struct spi_cal_rule *cal_rule;
225+
226 int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
227 };
228
developer2460ee72023-02-07 18:16:20 +0800229@@ -1369,6 +1408,9 @@ spi_register_board_info(struct spi_board
developerd9827922022-06-23 18:53:01 +0800230 { return 0; }
231 #endif
232
233+extern int spi_do_calibration(struct spi_controller *ctlr,
234+ struct spi_device *spi, int (*cal_read)(void *, u32 *, int, u8 *, int), void *drv_priv);
235+
236 /* If you're hotplugging an adapter with devices (parport, usb, etc)
237 * use spi_new_device() to describe each device. You can also call
238 * spi_unregister_device() to start making that device vanish, but