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