blob: d438084ada54808465334395b5e0c8dd6137c690 [file] [log] [blame]
developer5d148cb2023-06-02 13:08:11 +08001From a7d7d3c4d6a949135a34dff1e987a52de9f227b7 Mon Sep 17 00:00:00 2001
2From: Sam Shih <sam.shih@mediatek.com>
3Date: Fri, 2 Jun 2023 13:06:21 +0800
4Subject: [PATCH]
5 [spi-and-storage][999-2372-drivers-spi-Add-support-for-dynamic-calibration.patch]
developerd9827922022-06-23 18:53:01 +08006
developerd9827922022-06-23 18:53:01 +08007---
developer5d148cb2023-06-02 13:08:11 +08008 drivers/spi/spi.c | 141 ++++++++++++++++++++++++++++++++++++++++
developerd9827922022-06-23 18:53:01 +08009 include/linux/spi/spi.h | 42 ++++++++++++
developer5d148cb2023-06-02 13:08:11 +080010 2 files changed, 183 insertions(+)
developerd9827922022-06-23 18:53:01 +080011
developer5d148cb2023-06-02 13:08:11 +080012diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
13index e562735a3..28bad4a8b 100644
developerd9827922022-06-23 18:53:01 +080014--- a/drivers/spi/spi.c
15+++ b/drivers/spi/spi.c
developer5d148cb2023-06-02 13:08:11 +080016@@ -1109,6 +1109,74 @@ static int spi_transfer_wait(struct spi_controller *ctlr,
developerd9827922022-06-23 18:53:01 +080017 return 0;
18 }
19
20+int spi_do_calibration(struct spi_controller *ctlr, struct spi_device *spi,
21+ int (*cal_read)(void *priv, u32 *addr, int addrlen, u8 *buf, int readlen), void *drv_priv)
22+{
developerbb5f67b2023-04-13 16:33:48 +080023+ int datalen, addrlen;
developerd9827922022-06-23 18:53:01 +080024+ u8 *buf;
25+ int ret;
26+ int i;
developer9ee52d52023-05-29 14:26:07 +080027+ struct list_head *pos, *tmp;
developerd9827922022-06-23 18:53:01 +080028+ struct spi_cal_target *target;
29+
30+ /* Calculate calibration result */
31+ int hit_val, total_hit, origin;
32+ bool hit;
33+
34+ /* Make sure we can start calibration */
35+ if(!ctlr->cal_target || !ctlr->cal_rule || !ctlr->append_caldata)
developer2460ee72023-02-07 18:16:20 +080036+ return -EINVAL;
developerbb5f67b2023-04-13 16:33:48 +080037+ datalen = ctlr->cal_rule->datalen;
38+ addrlen = ctlr->cal_rule->addrlen;
developerd9827922022-06-23 18:53:01 +080039+
40+ buf = kzalloc(datalen * sizeof(u8), GFP_KERNEL);
41+ if(!buf)
42+ return -ENOMEM;
43+
44+ ret = ctlr->append_caldata(ctlr);
45+ if (ret)
46+ goto cal_end;
47+
developer9ee52d52023-05-29 14:26:07 +080048+ list_for_each_safe(pos, tmp, ctlr->cal_target) {
49+ target = list_entry(pos, struct spi_cal_target, list);
developerd9827922022-06-23 18:53:01 +080050+
51+ hit = false;
52+ hit_val = 0;
53+ total_hit = 0;
54+ origin = *target->cal_item;
55+
56+ for(i=target->cal_min; i<=target->cal_max; i+=target->step) {
57+ *target->cal_item = i;
58+ ret = (*cal_read)(drv_priv, ctlr->cal_rule->addr, addrlen, buf, datalen);
59+ if(ret)
60+ break;
61+ dev_dbg(&spi->dev, "controller cal item value: 0x%x\n", i);
62+ if(memcmp(ctlr->cal_rule->match_data, buf, datalen * sizeof(u8)) == 0) {
63+ hit = true;
64+ hit_val += i;
65+ total_hit++;
66+ dev_dbg(&spi->dev, "golden data matches data read!\n");
67+ }
68+ }
69+ if(hit) {
70+ *target->cal_item = DIV_ROUND_CLOSEST(hit_val, total_hit);
71+ dev_info(&spi->dev, "calibration result: 0x%x", *target->cal_item);
72+ } else {
73+ *target->cal_item = origin;
74+ dev_warn(&spi->dev, "calibration failed, fallback to default: 0x%x", origin);
developer2460ee72023-02-07 18:16:20 +080075+ ret = -EIO;
developerd9827922022-06-23 18:53:01 +080076+ }
developer9ee52d52023-05-29 14:26:07 +080077+
78+ list_del(pos);
79+ kfree(target);
developerd9827922022-06-23 18:53:01 +080080+ }
81+
82+cal_end:
83+ kfree(buf);
84+ return ret? ret: 0;
85+}
86+EXPORT_SYMBOL_GPL(spi_do_calibration);
87+
88 static void _spi_transfer_delay_ns(u32 ns)
89 {
90 if (!ns)
developer5d148cb2023-06-02 13:08:11 +080091@@ -1720,6 +1788,75 @@ void spi_flush_queue(struct spi_controller *ctlr)
developerd9827922022-06-23 18:53:01 +080092 /*-------------------------------------------------------------------------*/
93
94 #if defined(CONFIG_OF)
95+static inline void alloc_cal_data(struct list_head **cal_target,
96+ struct spi_cal_rule **cal_rule, bool enable)
97+{
98+ if(enable) {
99+ *cal_target = kmalloc(sizeof(struct list_head), GFP_KERNEL);
100+ INIT_LIST_HEAD(*cal_target);
101+ *cal_rule = kmalloc(sizeof(struct spi_cal_rule), GFP_KERNEL);
102+ } else {
103+ kfree(*cal_target);
104+ kfree(*cal_rule);
105+ }
106+}
107+
108+static int of_spi_parse_cal_dt(struct spi_controller *ctlr, struct spi_device *spi,
109+ struct device_node *nc)
110+{
111+ u32 value;
112+ int rc;
113+ const char *cal_mode;
114+
115+ rc = of_property_read_bool(nc, "spi-cal-enable");
116+ if (rc)
117+ alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, true);
118+ else
119+ return 0;
120+
121+ rc = of_property_read_string(nc, "spi-cal-mode", &cal_mode);
122+ if(!rc) {
123+ if(strcmp("read-data", cal_mode) == 0){
124+ ctlr->cal_rule->mode = SPI_CAL_READ_DATA;
125+ } else if(strcmp("read-pp", cal_mode) == 0) {
126+ ctlr->cal_rule->mode = SPI_CAL_READ_PP;
127+ return 0;
128+ } else if(strcmp("read-sfdp", cal_mode) == 0){
129+ ctlr->cal_rule->mode = SPI_CAL_READ_SFDP;
130+ return 0;
131+ }
132+ } else
133+ goto err;
134+
135+ ctlr->cal_rule->datalen = 0;
136+ rc = of_property_read_u32(nc, "spi-cal-datalen", &value);
137+ if(!rc && value > 0) {
138+ ctlr->cal_rule->datalen = value;
139+
140+ ctlr->cal_rule->match_data = kzalloc(value * sizeof(u8), GFP_KERNEL);
141+ rc = of_property_read_u8_array(nc, "spi-cal-data",
142+ ctlr->cal_rule->match_data, value);
143+ if(rc)
144+ kfree(ctlr->cal_rule->match_data);
145+ }
146+
147+ rc = of_property_read_u32(nc, "spi-cal-addrlen", &value);
148+ if(!rc && value > 0) {
149+ ctlr->cal_rule->addrlen = value;
150+
151+ ctlr->cal_rule->addr = kzalloc(value * sizeof(u32), GFP_KERNEL);
152+ rc = of_property_read_u32_array(nc, "spi-cal-addr",
153+ ctlr->cal_rule->addr, value);
154+ if(rc)
155+ kfree(ctlr->cal_rule->addr);
156+ }
157+ return 0;
158+
159+err:
160+ alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, false);
161+ return 0;
162+}
163+
164 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
165 struct device_node *nc)
166 {
developer5d148cb2023-06-02 13:08:11 +0800167@@ -1841,6 +1978,10 @@ of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
developerd9827922022-06-23 18:53:01 +0800168 if (rc)
169 goto err_out;
170
171+ rc = of_spi_parse_cal_dt(ctlr, spi, nc);
172+ if (rc)
173+ goto err_out;
174+
175 /* Store a pointer to the node in the device structure */
176 of_node_get(nc);
177 spi->dev.of_node = nc;
developer5d148cb2023-06-02 13:08:11 +0800178diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
179index 7067f85ce..5330cd9b0 100644
developerd9827922022-06-23 18:53:01 +0800180--- a/include/linux/spi/spi.h
181+++ b/include/linux/spi/spi.h
182@@ -264,6 +264,40 @@ struct spi_driver {
183 struct device_driver driver;
184 };
185
186+enum {
187+ SPI_CAL_READ_DATA = 0,
188+ SPI_CAL_READ_PP = 1, /* only for SPI-NAND */
189+ SPI_CAL_READ_SFDP = 2, /* only for SPI-NOR */
190+};
191+
192+struct nand_addr {
193+ unsigned int lun;
194+ unsigned int plane;
195+ unsigned int eraseblock;
196+ unsigned int page;
197+ unsigned int dataoffs;
198+};
199+
200+/**
201+ * Read calibration rule from device dts node.
202+ * Once calibration result matches the rule, we regard is as success.
203+ */
204+struct spi_cal_rule {
205+ int datalen;
206+ u8 *match_data;
207+ int addrlen;
208+ u32 *addr;
209+ int mode;
210+};
211+
212+struct spi_cal_target {
213+ u32 *cal_item;
214+ int cal_min; /* min of cal_item */
215+ int cal_max; /* max of cal_item */
216+ int step; /* Increase/decrease cal_item */
217+ struct list_head list;
218+};
219+
220 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
221 {
222 return drv ? container_of(drv, struct spi_driver, driver) : NULL;
223@@ -606,6 +640,11 @@ struct spi_controller {
224 void *dummy_rx;
225 void *dummy_tx;
226
227+ /* For calibration */
228+ int (*append_caldata)(struct spi_controller *ctlr);
229+ struct list_head *cal_target;
230+ struct spi_cal_rule *cal_rule;
231+
232 int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
233 };
234
developer5d148cb2023-06-02 13:08:11 +0800235@@ -1369,6 +1408,9 @@ spi_register_board_info(struct spi_board_info const *info, unsigned n)
developerd9827922022-06-23 18:53:01 +0800236 { return 0; }
237 #endif
238
239+extern int spi_do_calibration(struct spi_controller *ctlr,
240+ struct spi_device *spi, int (*cal_read)(void *, u32 *, int, u8 *, int), void *drv_priv);
241+
242 /* If you're hotplugging an adapter with devices (parport, usb, etc)
243 * use spi_new_device() to describe each device. You can also call
244 * spi_unregister_device() to start making that device vanish, but
developer5d148cb2023-06-02 13:08:11 +0800245--
2462.34.1
247