developer | d982792 | 2022-06-23 18:53:01 +0800 | [diff] [blame] | 1 | From a84c53fce4ccd67c147dcbb2dcf4fdeceab05981 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] 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 | diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c |
| 13 | index e562735a3..d548036b1 100644 |
| 14 | --- a/drivers/spi/spi.c |
| 15 | +++ b/drivers/spi/spi.c |
| 16 | @@ -1109,6 +1109,70 @@ static int spi_transfer_wait(struct spi_controller *ctlr, |
| 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 | +{ |
| 23 | + int datalen = ctlr->cal_rule->datalen; |
| 24 | + int addrlen = ctlr->cal_rule->addrlen; |
| 25 | + u8 *buf; |
| 26 | + int ret; |
| 27 | + int i; |
| 28 | + struct list_head *cal_head, *listptr; |
| 29 | + struct spi_cal_target *target; |
| 30 | + |
| 31 | + /* Calculate calibration result */ |
| 32 | + int hit_val, total_hit, origin; |
| 33 | + bool hit; |
| 34 | + |
| 35 | + /* Make sure we can start calibration */ |
| 36 | + if(!ctlr->cal_target || !ctlr->cal_rule || !ctlr->append_caldata) |
| 37 | + return 0; |
| 38 | + |
| 39 | + buf = kzalloc(datalen * sizeof(u8), GFP_KERNEL); |
| 40 | + if(!buf) |
| 41 | + return -ENOMEM; |
| 42 | + |
| 43 | + ret = ctlr->append_caldata(ctlr); |
| 44 | + if (ret) |
| 45 | + goto cal_end; |
| 46 | + |
| 47 | + cal_head = ctlr->cal_target; |
| 48 | + list_for_each(listptr, cal_head) { |
| 49 | + target = list_entry(listptr, struct spi_cal_target, list); |
| 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); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | +cal_end: |
| 79 | + kfree(buf); |
| 80 | + return ret? ret: 0; |
| 81 | +} |
| 82 | +EXPORT_SYMBOL_GPL(spi_do_calibration); |
| 83 | + |
| 84 | static void _spi_transfer_delay_ns(u32 ns) |
| 85 | { |
| 86 | if (!ns) |
| 87 | @@ -1720,6 +1784,75 @@ void spi_flush_queue(struct spi_controller *ctlr) |
| 88 | /*-------------------------------------------------------------------------*/ |
| 89 | |
| 90 | #if defined(CONFIG_OF) |
| 91 | +static inline void alloc_cal_data(struct list_head **cal_target, |
| 92 | + struct spi_cal_rule **cal_rule, bool enable) |
| 93 | +{ |
| 94 | + if(enable) { |
| 95 | + *cal_target = kmalloc(sizeof(struct list_head), GFP_KERNEL); |
| 96 | + INIT_LIST_HEAD(*cal_target); |
| 97 | + *cal_rule = kmalloc(sizeof(struct spi_cal_rule), GFP_KERNEL); |
| 98 | + } else { |
| 99 | + kfree(*cal_target); |
| 100 | + kfree(*cal_rule); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +static int of_spi_parse_cal_dt(struct spi_controller *ctlr, struct spi_device *spi, |
| 105 | + struct device_node *nc) |
| 106 | +{ |
| 107 | + u32 value; |
| 108 | + int rc; |
| 109 | + const char *cal_mode; |
| 110 | + |
| 111 | + rc = of_property_read_bool(nc, "spi-cal-enable"); |
| 112 | + if (rc) |
| 113 | + alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, true); |
| 114 | + else |
| 115 | + return 0; |
| 116 | + |
| 117 | + rc = of_property_read_string(nc, "spi-cal-mode", &cal_mode); |
| 118 | + if(!rc) { |
| 119 | + if(strcmp("read-data", cal_mode) == 0){ |
| 120 | + ctlr->cal_rule->mode = SPI_CAL_READ_DATA; |
| 121 | + } else if(strcmp("read-pp", cal_mode) == 0) { |
| 122 | + ctlr->cal_rule->mode = SPI_CAL_READ_PP; |
| 123 | + return 0; |
| 124 | + } else if(strcmp("read-sfdp", cal_mode) == 0){ |
| 125 | + ctlr->cal_rule->mode = SPI_CAL_READ_SFDP; |
| 126 | + return 0; |
| 127 | + } |
| 128 | + } else |
| 129 | + goto err; |
| 130 | + |
| 131 | + ctlr->cal_rule->datalen = 0; |
| 132 | + rc = of_property_read_u32(nc, "spi-cal-datalen", &value); |
| 133 | + if(!rc && value > 0) { |
| 134 | + ctlr->cal_rule->datalen = value; |
| 135 | + |
| 136 | + ctlr->cal_rule->match_data = kzalloc(value * sizeof(u8), GFP_KERNEL); |
| 137 | + rc = of_property_read_u8_array(nc, "spi-cal-data", |
| 138 | + ctlr->cal_rule->match_data, value); |
| 139 | + if(rc) |
| 140 | + kfree(ctlr->cal_rule->match_data); |
| 141 | + } |
| 142 | + |
| 143 | + rc = of_property_read_u32(nc, "spi-cal-addrlen", &value); |
| 144 | + if(!rc && value > 0) { |
| 145 | + ctlr->cal_rule->addrlen = value; |
| 146 | + |
| 147 | + ctlr->cal_rule->addr = kzalloc(value * sizeof(u32), GFP_KERNEL); |
| 148 | + rc = of_property_read_u32_array(nc, "spi-cal-addr", |
| 149 | + ctlr->cal_rule->addr, value); |
| 150 | + if(rc) |
| 151 | + kfree(ctlr->cal_rule->addr); |
| 152 | + } |
| 153 | + return 0; |
| 154 | + |
| 155 | +err: |
| 156 | + alloc_cal_data(&ctlr->cal_target, &ctlr->cal_rule, false); |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, |
| 161 | struct device_node *nc) |
| 162 | { |
| 163 | @@ -1841,6 +1974,10 @@ of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) |
| 164 | if (rc) |
| 165 | goto err_out; |
| 166 | |
| 167 | + rc = of_spi_parse_cal_dt(ctlr, spi, nc); |
| 168 | + if (rc) |
| 169 | + goto err_out; |
| 170 | + |
| 171 | /* Store a pointer to the node in the device structure */ |
| 172 | of_node_get(nc); |
| 173 | spi->dev.of_node = nc; |
| 174 | diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h |
| 175 | index 7067f85ce..5330cd9b0 100644 |
| 176 | --- 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 | |
| 231 | @@ -1369,6 +1408,9 @@ spi_register_board_info(struct spi_board_info const *info, unsigned n) |
| 232 | { 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 |
| 241 | -- |
| 242 | 2.18.0 |
| 243 | |