kernel: bump 6.1 to 6.1.66
[openwrt/openwrt.git] / target / linux / mediatek / patches-6.1 / 432-drivers-spi-Add-support-for-dynamic-calibration.patch
1 From 2ade0172154e50c8a2bfd8634c6eff943cffea29 Mon Sep 17 00:00:00 2001
2 From: "SkyLake.Huang" <skylake.huang@mediatek.com>
3 Date: Thu, 23 Jun 2022 18:35:52 +0800
4 Subject: [PATCH 2/6] drivers: spi: Add support for dynamic calibration
5
6 Signed-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
12 --- a/drivers/spi/spi.c
13 +++ b/drivers/spi/spi.c
14 @@ -1374,6 +1374,70 @@ static int spi_transfer_wait(struct spi_
15 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)
35 + return 0;
36 +
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);
73 + }
74 + }
75 +
76 +cal_end:
77 + kfree(buf);
78 + return ret? ret: 0;
79 +}
80 +EXPORT_SYMBOL_GPL(spi_do_calibration);
81 +
82 static void _spi_transfer_delay_ns(u32 ns)
83 {
84 if (!ns)
85 @@ -2208,6 +2272,75 @@ void spi_flush_queue(struct spi_controll
86 /*-------------------------------------------------------------------------*/
87
88 #if defined(CONFIG_OF)
89 +static inline void alloc_cal_data(struct list_head **cal_target,
90 + struct spi_cal_rule **cal_rule, bool enable)
91 +{
92 + if(enable) {
93 + *cal_target = kmalloc(sizeof(struct list_head), GFP_KERNEL);
94 + INIT_LIST_HEAD(*cal_target);
95 + *cal_rule = kmalloc(sizeof(struct spi_cal_rule), GFP_KERNEL);
96 + } else {
97 + kfree(*cal_target);
98 + kfree(*cal_rule);
99 + }
100 +}
101 +
102 +static int of_spi_parse_cal_dt(struct spi_controller *ctlr, struct spi_device *spi,
103 + struct device_node *nc)
104 +{
105 + u32 value;
106 + int rc;
107 + const char *cal_mode;
108 +
109 + rc = of_property_read_bool(nc, "spi-cal-enable");
110 + if (rc)
111 + alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, true);
112 + else
113 + return 0;
114 +
115 + rc = of_property_read_string(nc, "spi-cal-mode", &cal_mode);
116 + if(!rc) {
117 + if(strcmp("read-data", cal_mode) == 0){
118 + ctlr->cal_rule->mode = SPI_CAL_READ_DATA;
119 + } else if(strcmp("read-pp", cal_mode) == 0) {
120 + ctlr->cal_rule->mode = SPI_CAL_READ_PP;
121 + return 0;
122 + } else if(strcmp("read-sfdp", cal_mode) == 0){
123 + ctlr->cal_rule->mode = SPI_CAL_READ_SFDP;
124 + return 0;
125 + }
126 + } else
127 + goto err;
128 +
129 + ctlr->cal_rule->datalen = 0;
130 + rc = of_property_read_u32(nc, "spi-cal-datalen", &value);
131 + if(!rc && value > 0) {
132 + ctlr->cal_rule->datalen = value;
133 +
134 + ctlr->cal_rule->match_data = kzalloc(value * sizeof(u8), GFP_KERNEL);
135 + rc = of_property_read_u8_array(nc, "spi-cal-data",
136 + ctlr->cal_rule->match_data, value);
137 + if(rc)
138 + kfree(ctlr->cal_rule->match_data);
139 + }
140 +
141 + rc = of_property_read_u32(nc, "spi-cal-addrlen", &value);
142 + if(!rc && value > 0) {
143 + ctlr->cal_rule->addrlen = value;
144 +
145 + ctlr->cal_rule->addr = kzalloc(value * sizeof(u32), GFP_KERNEL);
146 + rc = of_property_read_u32_array(nc, "spi-cal-addr",
147 + ctlr->cal_rule->addr, value);
148 + if(rc)
149 + kfree(ctlr->cal_rule->addr);
150 + }
151 + return 0;
152 +
153 +err:
154 + alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, false);
155 + return 0;
156 +}
157 +
158 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
159 struct device_node *nc)
160 {
161 @@ -2326,6 +2459,10 @@ of_register_spi_device(struct spi_contro
162 if (rc)
163 goto err_out;
164
165 + rc = of_spi_parse_cal_dt(ctlr, spi, nc);
166 + if (rc)
167 + goto err_out;
168 +
169 /* Store a pointer to the node in the device structure */
170 of_node_get(nc);
171 spi->dev.of_node = nc;
172 --- a/include/linux/spi/spi.h
173 +++ b/include/linux/spi/spi.h
174 @@ -298,6 +298,40 @@ struct spi_driver {
175 struct device_driver driver;
176 };
177
178 +enum {
179 + SPI_CAL_READ_DATA = 0,
180 + SPI_CAL_READ_PP = 1, /* only for SPI-NAND */
181 + SPI_CAL_READ_SFDP = 2, /* only for SPI-NOR */
182 +};
183 +
184 +struct nand_addr {
185 + unsigned int lun;
186 + unsigned int plane;
187 + unsigned int eraseblock;
188 + unsigned int page;
189 + unsigned int dataoffs;
190 +};
191 +
192 +/**
193 + * Read calibration rule from device dts node.
194 + * Once calibration result matches the rule, we regard is as success.
195 + */
196 +struct spi_cal_rule {
197 + int datalen;
198 + u8 *match_data;
199 + int addrlen;
200 + u32 *addr;
201 + int mode;
202 +};
203 +
204 +struct spi_cal_target {
205 + u32 *cal_item;
206 + int cal_min; /* min of cal_item */
207 + int cal_max; /* max of cal_item */
208 + int step; /* Increase/decrease cal_item */
209 + struct list_head list;
210 +};
211 +
212 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
213 {
214 return drv ? container_of(drv, struct spi_driver, driver) : NULL;
215 @@ -683,6 +717,11 @@ struct spi_controller {
216 void *dummy_rx;
217 void *dummy_tx;
218
219 + /* For calibration */
220 + int (*append_caldata)(struct spi_controller *ctlr);
221 + struct list_head *cal_target;
222 + struct spi_cal_rule *cal_rule;
223 +
224 int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
225
226 /*
227 @@ -1490,6 +1529,9 @@ spi_register_board_info(struct spi_board
228 { return 0; }
229 #endif
230
231 +extern int spi_do_calibration(struct spi_controller *ctlr,
232 + struct spi_device *spi, int (*cal_read)(void *, u32 *, int, u8 *, int), void *drv_priv);
233 +
234 /* If you're hotplugging an adapter with devices (parport, usb, etc)
235 * use spi_new_device() to describe each device. You can also call
236 * spi_unregister_device() to start making that device vanish, but