developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 1 | From a7d7d3c4d6a949135a34dff1e987a52de9f227b7 Mon Sep 17 00:00:00 2001 |
| 2 | From: Sam Shih <sam.shih@mediatek.com> |
| 3 | Date: Fri, 2 Jun 2023 13:06:21 +0800 |
| 4 | Subject: [PATCH] |
| 5 | [spi-and-storage][999-2372-drivers-spi-Add-support-for-dynamic-calibration.patch] |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 6 | |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 7 | --- |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 8 | drivers/spi/spi.c | 141 ++++++++++++++++++++++++++++++++++++++++ |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 9 | include/linux/spi/spi.h | 42 ++++++++++++ |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 10 | 2 files changed, 183 insertions(+) |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 11 | |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 12 | diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c |
| 13 | index e562735a3..28bad4a8b 100644 |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 14 | --- a/drivers/spi/spi.c |
| 15 | +++ b/drivers/spi/spi.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 16 | @@ -1109,6 +1109,74 @@ static int spi_transfer_wait(struct spi_controller *ctlr, |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 17 | 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 | +{ |
developer | bb5f67b | 2023-04-13 16:33:48 +0800 | [diff] [blame] | 23 | + int datalen, addrlen; |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 24 | + u8 *buf; |
| 25 | + int ret; |
| 26 | + int i; |
developer | 9ee52d5 | 2023-05-29 14:26:07 +0800 | [diff] [blame] | 27 | + struct list_head *pos, *tmp; |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 28 | + 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) |
developer | 2460ee7 | 2023-02-07 18:16:20 +0800 | [diff] [blame] | 36 | + return -EINVAL; |
developer | bb5f67b | 2023-04-13 16:33:48 +0800 | [diff] [blame] | 37 | + datalen = ctlr->cal_rule->datalen; |
| 38 | + addrlen = ctlr->cal_rule->addrlen; |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 39 | + |
| 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 | + |
developer | 9ee52d5 | 2023-05-29 14:26:07 +0800 | [diff] [blame] | 48 | + list_for_each_safe(pos, tmp, ctlr->cal_target) { |
| 49 | + target = list_entry(pos, struct spi_cal_target, list); |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 50 | + |
| 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); |
developer | 2460ee7 | 2023-02-07 18:16:20 +0800 | [diff] [blame] | 75 | + ret = -EIO; |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 76 | + } |
developer | 9ee52d5 | 2023-05-29 14:26:07 +0800 | [diff] [blame] | 77 | + |
| 78 | + list_del(pos); |
| 79 | + kfree(target); |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 80 | + } |
| 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) |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 91 | @@ -1720,6 +1788,75 @@ void spi_flush_queue(struct spi_controller *ctlr) |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 92 | /*-------------------------------------------------------------------------*/ |
| 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 | { |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 167 | @@ -1841,6 +1978,10 @@ of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 168 | 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; |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 178 | diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h |
| 179 | index 7067f85ce..5330cd9b0 100644 |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 180 | --- 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 | |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 235 | @@ -1369,6 +1408,9 @@ spi_register_board_info(struct spi_board_info const *info, unsigned n) |
developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 236 | { 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 |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 245 | -- |
| 246 | 2.34.1 |
| 247 | |