blob: 750c87b9f13d3c48c919f782f23bcef2099aa7c7 [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
developer9ee52d52023-05-29 14:26:07 +080014@@ -1109,6 +1109,74 @@ 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;
developer9ee52d52023-05-29 14:26:07 +080025+ struct list_head *pos, *tmp;
developerd9827922022-06-23 18:53:01 +080026+ 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+
developer9ee52d52023-05-29 14:26:07 +080046+ list_for_each_safe(pos, tmp, ctlr->cal_target) {
47+ target = list_entry(pos, struct spi_cal_target, list);
developerd9827922022-06-23 18:53:01 +080048+
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+ }
developer9ee52d52023-05-29 14:26:07 +080075+
76+ list_del(pos);
77+ kfree(target);
developerd9827922022-06-23 18:53:01 +080078+ }
79+
80+cal_end:
81+ kfree(buf);
82+ return ret? ret: 0;
83+}
84+EXPORT_SYMBOL_GPL(spi_do_calibration);
85+
86 static void _spi_transfer_delay_ns(u32 ns)
87 {
88 if (!ns)
developer9ee52d52023-05-29 14:26:07 +080089@@ -1720,6 +1788,75 @@ void spi_flush_queue(struct spi_controll
developerd9827922022-06-23 18:53:01 +080090 /*-------------------------------------------------------------------------*/
91
92 #if defined(CONFIG_OF)
93+static inline void alloc_cal_data(struct list_head **cal_target,
94+ struct spi_cal_rule **cal_rule, bool enable)
95+{
96+ if(enable) {
97+ *cal_target = kmalloc(sizeof(struct list_head), GFP_KERNEL);
98+ INIT_LIST_HEAD(*cal_target);
99+ *cal_rule = kmalloc(sizeof(struct spi_cal_rule), GFP_KERNEL);
100+ } else {
101+ kfree(*cal_target);
102+ kfree(*cal_rule);
103+ }
104+}
105+
106+static int of_spi_parse_cal_dt(struct spi_controller *ctlr, struct spi_device *spi,
107+ struct device_node *nc)
108+{
109+ u32 value;
110+ int rc;
111+ const char *cal_mode;
112+
113+ rc = of_property_read_bool(nc, "spi-cal-enable");
114+ if (rc)
115+ alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, true);
116+ else
117+ return 0;
118+
119+ rc = of_property_read_string(nc, "spi-cal-mode", &cal_mode);
120+ if(!rc) {
121+ if(strcmp("read-data", cal_mode) == 0){
122+ ctlr->cal_rule->mode = SPI_CAL_READ_DATA;
123+ } else if(strcmp("read-pp", cal_mode) == 0) {
124+ ctlr->cal_rule->mode = SPI_CAL_READ_PP;
125+ return 0;
126+ } else if(strcmp("read-sfdp", cal_mode) == 0){
127+ ctlr->cal_rule->mode = SPI_CAL_READ_SFDP;
128+ return 0;
129+ }
130+ } else
131+ goto err;
132+
133+ ctlr->cal_rule->datalen = 0;
134+ rc = of_property_read_u32(nc, "spi-cal-datalen", &value);
135+ if(!rc && value > 0) {
136+ ctlr->cal_rule->datalen = value;
137+
138+ ctlr->cal_rule->match_data = kzalloc(value * sizeof(u8), GFP_KERNEL);
139+ rc = of_property_read_u8_array(nc, "spi-cal-data",
140+ ctlr->cal_rule->match_data, value);
141+ if(rc)
142+ kfree(ctlr->cal_rule->match_data);
143+ }
144+
145+ rc = of_property_read_u32(nc, "spi-cal-addrlen", &value);
146+ if(!rc && value > 0) {
147+ ctlr->cal_rule->addrlen = value;
148+
149+ ctlr->cal_rule->addr = kzalloc(value * sizeof(u32), GFP_KERNEL);
150+ rc = of_property_read_u32_array(nc, "spi-cal-addr",
151+ ctlr->cal_rule->addr, value);
152+ if(rc)
153+ kfree(ctlr->cal_rule->addr);
154+ }
155+ return 0;
156+
157+err:
158+ alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, false);
159+ return 0;
160+}
161+
162 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
163 struct device_node *nc)
164 {
developer9ee52d52023-05-29 14:26:07 +0800165@@ -1841,6 +1978,10 @@ of_register_spi_device(struct spi_contro
developerd9827922022-06-23 18:53:01 +0800166 if (rc)
167 goto err_out;
168
169+ rc = of_spi_parse_cal_dt(ctlr, spi, nc);
170+ if (rc)
171+ goto err_out;
172+
173 /* Store a pointer to the node in the device structure */
174 of_node_get(nc);
175 spi->dev.of_node = nc;
developerd9827922022-06-23 18:53:01 +0800176--- a/include/linux/spi/spi.h
177+++ b/include/linux/spi/spi.h
178@@ -264,6 +264,40 @@ struct spi_driver {
179 struct device_driver driver;
180 };
181
182+enum {
183+ SPI_CAL_READ_DATA = 0,
184+ SPI_CAL_READ_PP = 1, /* only for SPI-NAND */
185+ SPI_CAL_READ_SFDP = 2, /* only for SPI-NOR */
186+};
187+
188+struct nand_addr {
189+ unsigned int lun;
190+ unsigned int plane;
191+ unsigned int eraseblock;
192+ unsigned int page;
193+ unsigned int dataoffs;
194+};
195+
196+/**
197+ * Read calibration rule from device dts node.
198+ * Once calibration result matches the rule, we regard is as success.
199+ */
200+struct spi_cal_rule {
201+ int datalen;
202+ u8 *match_data;
203+ int addrlen;
204+ u32 *addr;
205+ int mode;
206+};
207+
208+struct spi_cal_target {
209+ u32 *cal_item;
210+ int cal_min; /* min of cal_item */
211+ int cal_max; /* max of cal_item */
212+ int step; /* Increase/decrease cal_item */
213+ struct list_head list;
214+};
215+
216 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
217 {
218 return drv ? container_of(drv, struct spi_driver, driver) : NULL;
219@@ -606,6 +640,11 @@ struct spi_controller {
220 void *dummy_rx;
221 void *dummy_tx;
222
223+ /* For calibration */
224+ int (*append_caldata)(struct spi_controller *ctlr);
225+ struct list_head *cal_target;
226+ struct spi_cal_rule *cal_rule;
227+
228 int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
229 };
230
developer2460ee72023-02-07 18:16:20 +0800231@@ -1369,6 +1408,9 @@ spi_register_board_info(struct spi_board
developerd9827922022-06-23 18:53:01 +0800232 { return 0; }
233 #endif
234
235+extern int spi_do_calibration(struct spi_controller *ctlr,
236+ struct spi_device *spi, int (*cal_read)(void *, u32 *, int, u8 *, int), void *drv_priv);
237+
238 /* If you're hotplugging an adapter with devices (parport, usb, etc)
239 * use spi_new_device() to describe each device. You can also call
240 * spi_unregister_device() to start making that device vanish, but