| .. | .. |
|---|
| 21 | 21 | #include <linux/platform_device.h> |
|---|
| 22 | 22 | #include <linux/regmap.h> |
|---|
| 23 | 23 | #include <linux/mfd/rk628.h> |
|---|
| 24 | | -#include <linux/mfd/rk630.h> |
|---|
| 25 | 24 | |
|---|
| 26 | 25 | #define EFUSE_SIZE 64 |
|---|
| 27 | 26 | |
|---|
| .. | .. |
|---|
| 59 | 58 | #define EFUSE_REVISION 0x50 |
|---|
| 60 | 59 | |
|---|
| 61 | 60 | #define RK628_EFUSE_BASE 0xb0000 |
|---|
| 62 | | -#define RK630_EFUSE_BASE 0x50000 |
|---|
| 63 | 61 | #define RK628_MOD 0x00 |
|---|
| 64 | 62 | #define RK628_INT_STATUS 0x0018 |
|---|
| 65 | 63 | #define RK628_DOUT 0x0020 |
|---|
| .. | .. |
|---|
| 77 | 75 | #define REG_EFUSE_CTRL 0x0000 |
|---|
| 78 | 76 | #define REG_EFUSE_DOUT 0x0004 |
|---|
| 79 | 77 | |
|---|
| 80 | | -enum { |
|---|
| 81 | | - RK628_EFUSE, |
|---|
| 82 | | - RK630_EFUSE, |
|---|
| 83 | | -}; |
|---|
| 84 | | - |
|---|
| 85 | | -struct rk6xx_efuse_plat_data { |
|---|
| 86 | | - int device_type; |
|---|
| 87 | | - struct nvmem_config *econfig; |
|---|
| 88 | | -}; |
|---|
| 89 | | - |
|---|
| 90 | 78 | struct rk628_efuse_chip { |
|---|
| 91 | 79 | struct device *dev; |
|---|
| 92 | 80 | u32 base; |
|---|
| 93 | 81 | struct clk *clk; |
|---|
| 94 | 82 | struct regmap *regmap; |
|---|
| 95 | | - struct regmap *cru; |
|---|
| 96 | 83 | struct gpio_desc *avdd_gpio; |
|---|
| 97 | 84 | }; |
|---|
| 98 | 85 | |
|---|
| 99 | | -static int rk628_read(struct rk628_efuse_chip *efuse, u32 reg) |
|---|
| 86 | +static int rk628_read(struct regmap *regmap, u32 reg) |
|---|
| 100 | 87 | { |
|---|
| 101 | 88 | int ret; |
|---|
| 102 | 89 | u32 val; |
|---|
| 103 | | - struct regmap *regmap = efuse->regmap; |
|---|
| 90 | + struct rk628_efuse_chip *efuse = container_of(regmap, struct rk628_efuse_chip, regmap); |
|---|
| 104 | 91 | |
|---|
| 105 | 92 | ret = regmap_read(regmap, reg, &val); |
|---|
| 106 | 93 | if (ret) { |
|---|
| 107 | | - dev_err(efuse->dev, "failed to read reg 0x%x\n", reg); |
|---|
| 94 | + dev_err(efuse->dev, "rk628-efuse:failed to read reg 0x%x\n", reg); |
|---|
| 108 | 95 | return ret; |
|---|
| 109 | 96 | } |
|---|
| 110 | 97 | |
|---|
| 111 | 98 | return val; |
|---|
| 112 | 99 | } |
|---|
| 113 | 100 | |
|---|
| 114 | | -static int rk628_write(struct rk628_efuse_chip *efuse, u32 val, u32 reg) |
|---|
| 101 | +static int rk628_write(struct regmap *regmap, u32 val, u32 reg) |
|---|
| 115 | 102 | { |
|---|
| 116 | 103 | int ret; |
|---|
| 117 | | - struct regmap *regmap = efuse->regmap; |
|---|
| 104 | + struct rk628_efuse_chip *efuse = container_of(regmap, struct rk628_efuse_chip, regmap); |
|---|
| 118 | 105 | |
|---|
| 119 | 106 | ret = regmap_write(regmap, reg, val); |
|---|
| 120 | 107 | if (ret) |
|---|
| 121 | | - dev_err(efuse->dev, "failed to write reg 0x%x\n", reg); |
|---|
| 108 | + dev_err(efuse->dev, "rk628-efuse:failed to write reg 0x%x\n", reg); |
|---|
| 122 | 109 | |
|---|
| 123 | 110 | return ret; |
|---|
| 124 | 111 | } |
|---|
| .. | .. |
|---|
| 127 | 114 | { |
|---|
| 128 | 115 | u32 base = efuse->base; |
|---|
| 129 | 116 | /* enable auto mode */ |
|---|
| 130 | | - rk628_write(efuse, |
|---|
| 131 | | - rk628_read(efuse, base + RK628_MOD) & (~RK628_USER_MODE), |
|---|
| 117 | + rk628_write(efuse->regmap, |
|---|
| 118 | + rk628_read(efuse->regmap, base + RK628_MOD) & (~RK628_USER_MODE), |
|---|
| 132 | 119 | base + RK628_MOD); |
|---|
| 133 | 120 | |
|---|
| 134 | 121 | /* setup efuse timing */ |
|---|
| 135 | | - rk628_write(efuse, (T_CSB_P_S << 16) | T_CSB_P_L, base + T_CSB_P); |
|---|
| 136 | | - rk628_write(efuse, (T_PGENB_P_S << 16) | T_PGENB_P_L, base + T_PGENB_P); |
|---|
| 137 | | - rk628_write(efuse, (T_LOAD_P_S << 16) | T_LOAD_P_L, base + T_LOAD_P); |
|---|
| 138 | | - rk628_write(efuse, (T_ADDR_P_S << 16) | T_ADDR_P_L, base + T_ADDR_P); |
|---|
| 139 | | - rk628_write(efuse, (T_STROBE_P_S << 16) | T_STROBE_P_L, base + T_STROBE_P); |
|---|
| 140 | | - rk628_write(efuse, (T_CSB_R_S << 16) | T_CSB_R_L, base + T_CSB_R); |
|---|
| 141 | | - rk628_write(efuse, (T_PGENB_R_S << 16) | T_PGENB_R_L, base + T_PGENB_R); |
|---|
| 142 | | - rk628_write(efuse, (T_LOAD_R_S << 16) | T_LOAD_R_L, base + T_LOAD_R); |
|---|
| 143 | | - rk628_write(efuse, (T_ADDR_R_S << 16) | T_ADDR_R_L, base + T_ADDR_R); |
|---|
| 144 | | - rk628_write(efuse, (T_STROBE_R_S << 16) | T_STROBE_R_L, base + T_STROBE_R); |
|---|
| 122 | + rk628_write(efuse->regmap, (T_CSB_P_S << 16) | T_CSB_P_L, base + T_CSB_P); |
|---|
| 123 | + rk628_write(efuse->regmap, (T_PGENB_P_S << 16) | T_PGENB_P_L, base + T_PGENB_P); |
|---|
| 124 | + rk628_write(efuse->regmap, (T_LOAD_P_S << 16) | T_LOAD_P_L, base + T_LOAD_P); |
|---|
| 125 | + rk628_write(efuse->regmap, (T_ADDR_P_S << 16) | T_ADDR_P_L, base + T_ADDR_P); |
|---|
| 126 | + rk628_write(efuse->regmap, (T_STROBE_P_S << 16) | T_STROBE_P_L, base + T_STROBE_P); |
|---|
| 127 | + rk628_write(efuse->regmap, (T_CSB_R_S << 16) | T_CSB_R_L, base + T_CSB_R); |
|---|
| 128 | + rk628_write(efuse->regmap, (T_PGENB_R_S << 16) | T_PGENB_R_L, base + T_PGENB_R); |
|---|
| 129 | + rk628_write(efuse->regmap, (T_LOAD_R_S << 16) | T_LOAD_R_L, base + T_LOAD_R); |
|---|
| 130 | + rk628_write(efuse->regmap, (T_ADDR_R_S << 16) | T_ADDR_R_L, base + T_ADDR_R); |
|---|
| 131 | + rk628_write(efuse->regmap, (T_STROBE_R_S << 16) | T_STROBE_R_L, base + T_STROBE_R); |
|---|
| 145 | 132 | } |
|---|
| 146 | 133 | |
|---|
| 147 | 134 | static void rk628_efuse_timing_deinit(struct rk628_efuse_chip *efuse) |
|---|
| 148 | 135 | { |
|---|
| 149 | 136 | u32 base = efuse->base; |
|---|
| 150 | 137 | /* disable auto mode */ |
|---|
| 151 | | - rk628_write(efuse, |
|---|
| 152 | | - rk628_read(efuse, base + RK628_MOD) | RK628_USER_MODE, base + RK628_MOD); |
|---|
| 138 | + rk628_write(efuse->regmap, |
|---|
| 139 | + rk628_read(efuse->regmap, base + RK628_MOD) | RK628_USER_MODE, base + RK628_MOD); |
|---|
| 153 | 140 | |
|---|
| 154 | 141 | /* clear efuse timing */ |
|---|
| 155 | | - rk628_write(efuse, 0, base + T_CSB_P); |
|---|
| 156 | | - rk628_write(efuse, 0, base + T_PGENB_P); |
|---|
| 157 | | - rk628_write(efuse, 0, base + T_LOAD_P); |
|---|
| 158 | | - rk628_write(efuse, 0, base + T_ADDR_P); |
|---|
| 159 | | - rk628_write(efuse, 0, base + T_STROBE_P); |
|---|
| 160 | | - rk628_write(efuse, 0, base + T_CSB_R); |
|---|
| 161 | | - rk628_write(efuse, 0, base + T_PGENB_R); |
|---|
| 162 | | - rk628_write(efuse, 0, base + T_LOAD_R); |
|---|
| 163 | | - rk628_write(efuse, 0, base + T_ADDR_R); |
|---|
| 164 | | - rk628_write(efuse, 0, base + T_STROBE_R); |
|---|
| 142 | + rk628_write(efuse->regmap, 0, base + T_CSB_P); |
|---|
| 143 | + rk628_write(efuse->regmap, 0, base + T_PGENB_P); |
|---|
| 144 | + rk628_write(efuse->regmap, 0, base + T_LOAD_P); |
|---|
| 145 | + rk628_write(efuse->regmap, 0, base + T_ADDR_P); |
|---|
| 146 | + rk628_write(efuse->regmap, 0, base + T_STROBE_P); |
|---|
| 147 | + rk628_write(efuse->regmap, 0, base + T_CSB_R); |
|---|
| 148 | + rk628_write(efuse->regmap, 0, base + T_PGENB_R); |
|---|
| 149 | + rk628_write(efuse->regmap, 0, base + T_LOAD_R); |
|---|
| 150 | + rk628_write(efuse->regmap, 0, base + T_ADDR_R); |
|---|
| 151 | + rk628_write(efuse->regmap, 0, base + T_STROBE_R); |
|---|
| 165 | 152 | } |
|---|
| 166 | 153 | |
|---|
| 167 | 154 | static int rk628_efuse_read(void *context, unsigned int offset, |
|---|
| .. | .. |
|---|
| 171 | 158 | unsigned int addr_start, addr_end, addr_offset, addr_len; |
|---|
| 172 | 159 | u32 out_value, status; |
|---|
| 173 | 160 | u8 *buf; |
|---|
| 174 | | - int ret = 0, i = 0; |
|---|
| 161 | + int ret, i = 0; |
|---|
| 175 | 162 | |
|---|
| 176 | | - if (efuse->clk) { |
|---|
| 177 | | - ret = clk_prepare_enable(efuse->clk); |
|---|
| 178 | | - if (ret < 0) { |
|---|
| 179 | | - dev_err(efuse->dev, "failed to prepare/enable efuse pclk\n"); |
|---|
| 180 | | - return ret; |
|---|
| 181 | | - } |
|---|
| 182 | | - } else { |
|---|
| 183 | | - regmap_write(efuse->cru, CRU_GATE_CON0, PCLK_EFUSE_EN_MASK); |
|---|
| 163 | + ret = clk_prepare_enable(efuse->clk); |
|---|
| 164 | + if (ret < 0) { |
|---|
| 165 | + dev_err(efuse->dev, "failed to prepare/enable efuse pclk\n"); |
|---|
| 166 | + return ret; |
|---|
| 184 | 167 | } |
|---|
| 185 | 168 | |
|---|
| 186 | 169 | addr_start = rounddown(offset, RK628_NBYTES) / RK628_NBYTES; |
|---|
| .. | .. |
|---|
| 197 | 180 | rk628_efuse_timing_init(efuse); |
|---|
| 198 | 181 | |
|---|
| 199 | 182 | while (addr_len--) { |
|---|
| 200 | | - rk628_write(efuse, RK628_AUTO_RD | RK628_AUTO_ENB | |
|---|
| 183 | + rk628_write(efuse->regmap, RK628_AUTO_RD | RK628_AUTO_ENB | |
|---|
| 201 | 184 | ((addr_start++ & RK628_A_MASK) << RK628_A_SHIFT), |
|---|
| 202 | 185 | efuse->base + RK628_AUTO_CTRL); |
|---|
| 203 | 186 | udelay(2); |
|---|
| 204 | | - status = rk628_read(efuse, efuse->base + RK628_INT_STATUS); |
|---|
| 187 | + status = rk628_read(efuse->regmap, efuse->base + RK628_INT_STATUS); |
|---|
| 205 | 188 | if (!(status & RK628_INT_FINISH)) { |
|---|
| 206 | 189 | ret = -EIO; |
|---|
| 207 | 190 | goto err; |
|---|
| 208 | 191 | } |
|---|
| 209 | | - out_value = rk628_read(efuse, efuse->base + RK628_DOUT); |
|---|
| 210 | | - rk628_write(efuse, RK628_INT_FINISH, efuse->base + RK628_INT_STATUS); |
|---|
| 192 | + out_value = rk628_read(efuse->regmap, efuse->base + RK628_DOUT); |
|---|
| 193 | + rk628_write(efuse->regmap, RK628_INT_FINISH, efuse->base + RK628_INT_STATUS); |
|---|
| 211 | 194 | |
|---|
| 212 | 195 | memcpy(&buf[i], &out_value, RK628_NBYTES); |
|---|
| 213 | 196 | i += RK628_NBYTES; |
|---|
| .. | .. |
|---|
| 217 | 200 | rk628_efuse_timing_deinit(efuse); |
|---|
| 218 | 201 | kfree(buf); |
|---|
| 219 | 202 | nomem: |
|---|
| 220 | | - if (efuse->clk) |
|---|
| 221 | | - clk_disable_unprepare(efuse->clk); |
|---|
| 222 | | - else |
|---|
| 223 | | - regmap_write(efuse->cru, CRU_GATE_CON0, PCLK_EFUSE_EN_MASK | PCLK_EFUSE_DISABLE); |
|---|
| 203 | + clk_disable_unprepare(efuse->clk); |
|---|
| 224 | 204 | |
|---|
| 225 | 205 | return ret; |
|---|
| 226 | 206 | } |
|---|
| 227 | 207 | |
|---|
| 228 | | -static struct nvmem_config rk628_econfig = { |
|---|
| 208 | +static struct nvmem_config econfig = { |
|---|
| 229 | 209 | .name = "rk628-efuse", |
|---|
| 230 | | - .owner = THIS_MODULE, |
|---|
| 231 | | - .stride = 1, |
|---|
| 232 | | - .word_size = 1, |
|---|
| 233 | | - .read_only = true, |
|---|
| 234 | | -}; |
|---|
| 235 | | - |
|---|
| 236 | | -static struct nvmem_config rk630_econfig = { |
|---|
| 237 | | - .name = "rk630-efuse", |
|---|
| 238 | 210 | .owner = THIS_MODULE, |
|---|
| 239 | 211 | .stride = 1, |
|---|
| 240 | 212 | .word_size = 1, |
|---|
| .. | .. |
|---|
| 261 | 233 | .rd_table = &rk628_efuse_readable_table, |
|---|
| 262 | 234 | }; |
|---|
| 263 | 235 | |
|---|
| 264 | | -static const struct regmap_range rk630_efuse_readable_ranges[] = { |
|---|
| 265 | | - regmap_reg_range(RK630_EFUSE_BASE, RK630_EFUSE_BASE + EFUSE_REVISION), |
|---|
| 266 | | -}; |
|---|
| 267 | | - |
|---|
| 268 | | -static const struct regmap_access_table rk630_efuse_readable_table = { |
|---|
| 269 | | - .yes_ranges = rk630_efuse_readable_ranges, |
|---|
| 270 | | - .n_yes_ranges = ARRAY_SIZE(rk630_efuse_readable_ranges), |
|---|
| 271 | | -}; |
|---|
| 272 | | - |
|---|
| 273 | | -const struct regmap_config rk630_efuse_regmap_config = { |
|---|
| 274 | | - .name = "rk630-efuse", |
|---|
| 275 | | - .reg_bits = 32, |
|---|
| 276 | | - .val_bits = 32, |
|---|
| 277 | | - .reg_stride = 4, |
|---|
| 278 | | - .max_register = RK630_EFUSE_BASE + EFUSE_REVISION, |
|---|
| 279 | | - .reg_format_endian = REGMAP_ENDIAN_LITTLE, |
|---|
| 280 | | - .val_format_endian = REGMAP_ENDIAN_LITTLE, |
|---|
| 281 | | - .rd_table = &rk630_efuse_readable_table, |
|---|
| 282 | | -}; |
|---|
| 283 | | -EXPORT_SYMBOL_GPL(rk630_efuse_regmap_config); |
|---|
| 284 | | - |
|---|
| 285 | | -static const struct rk6xx_efuse_plat_data rk628_efuse_drv_data = { |
|---|
| 286 | | - .device_type = RK628_EFUSE, |
|---|
| 287 | | - .econfig = &rk628_econfig, |
|---|
| 288 | | -}; |
|---|
| 289 | | - |
|---|
| 290 | | -static const struct rk6xx_efuse_plat_data rk630_efuse_drv_data = { |
|---|
| 291 | | - .device_type = RK630_EFUSE, |
|---|
| 292 | | - .econfig = &rk630_econfig, |
|---|
| 293 | | -}; |
|---|
| 294 | | - |
|---|
| 295 | 236 | static const struct of_device_id rk628_efuse_match[] = { |
|---|
| 296 | 237 | { |
|---|
| 297 | 238 | .compatible = "rockchip,rk628-efuse", |
|---|
| 298 | | - .data = &rk628_efuse_drv_data |
|---|
| 299 | | - }, |
|---|
| 300 | | - { |
|---|
| 301 | | - .compatible = "rockchip,rk630-efuse", |
|---|
| 302 | | - .data = &rk630_efuse_drv_data |
|---|
| 303 | 239 | }, |
|---|
| 304 | 240 | { /* sentinel */ }, |
|---|
| 305 | 241 | }; |
|---|
| 306 | 242 | MODULE_DEVICE_TABLE(of, rk628_efuse_match); |
|---|
| 307 | 243 | |
|---|
| 308 | | -static int rk628_efuse_probe(struct platform_device *pdev) |
|---|
| 244 | +static int __init rk628_efuse_probe(struct platform_device *pdev) |
|---|
| 309 | 245 | { |
|---|
| 310 | 246 | struct nvmem_device *nvmem; |
|---|
| 311 | 247 | struct rk628_efuse_chip *efuse; |
|---|
| 312 | 248 | struct device *dev = &pdev->dev; |
|---|
| 313 | | - struct rk6xx_efuse_plat_data *plat_data; |
|---|
| 314 | | - const struct of_device_id *match; |
|---|
| 249 | + struct rk628 *rk628 = dev_get_drvdata(pdev->dev.parent); |
|---|
| 315 | 250 | int ret; |
|---|
| 316 | 251 | |
|---|
| 317 | 252 | efuse = devm_kzalloc(&pdev->dev, sizeof(struct rk628_efuse_chip), |
|---|
| .. | .. |
|---|
| 319 | 254 | if (!efuse) |
|---|
| 320 | 255 | return -ENOMEM; |
|---|
| 321 | 256 | |
|---|
| 322 | | - match = of_match_node(rk628_efuse_match, pdev->dev.of_node); |
|---|
| 323 | | - plat_data = (struct rk6xx_efuse_plat_data *)match->data; |
|---|
| 324 | | - if (!plat_data) |
|---|
| 325 | | - return -ENOMEM; |
|---|
| 257 | + efuse->regmap = devm_regmap_init_i2c(rk628->client, |
|---|
| 258 | + &rk628_efuse_regmap_config); |
|---|
| 259 | + if (IS_ERR(efuse->regmap)) { |
|---|
| 260 | + ret = PTR_ERR(efuse->regmap); |
|---|
| 261 | + dev_err(dev, "failed to allocate register map: %d\n", |
|---|
| 262 | + ret); |
|---|
| 263 | + return ret; |
|---|
| 264 | + } |
|---|
| 326 | 265 | |
|---|
| 327 | | - if (plat_data->device_type == RK628_EFUSE) { |
|---|
| 328 | | - struct rk628 *rk628 = dev_get_drvdata(pdev->dev.parent); |
|---|
| 329 | | - |
|---|
| 330 | | - efuse->regmap = devm_regmap_init_i2c(rk628->client, |
|---|
| 331 | | - &rk628_efuse_regmap_config); |
|---|
| 332 | | - if (IS_ERR(efuse->regmap)) { |
|---|
| 333 | | - ret = PTR_ERR(efuse->regmap); |
|---|
| 334 | | - dev_err(dev, "failed to allocate register map: %d\n", |
|---|
| 335 | | - ret); |
|---|
| 336 | | - return ret; |
|---|
| 337 | | - } |
|---|
| 338 | | - |
|---|
| 339 | | - efuse->clk = devm_clk_get(&pdev->dev, "pclk"); |
|---|
| 340 | | - if (IS_ERR(efuse->clk)) { |
|---|
| 341 | | - dev_err(dev, "failed to get pclk: %ld\n", PTR_ERR(efuse->clk)); |
|---|
| 342 | | - return PTR_ERR(efuse->clk); |
|---|
| 343 | | - } |
|---|
| 344 | | - |
|---|
| 345 | | - efuse->base = RK628_EFUSE_BASE; |
|---|
| 346 | | - } else { |
|---|
| 347 | | - struct rk630 *rk630 = dev_get_drvdata(pdev->dev.parent); |
|---|
| 348 | | - |
|---|
| 349 | | - efuse->regmap = rk630->efuse; |
|---|
| 350 | | - efuse->cru = rk630->cru; |
|---|
| 351 | | - efuse->base = RK630_EFUSE_BASE; |
|---|
| 352 | | - |
|---|
| 353 | | - if (!efuse->regmap | !efuse->cru) |
|---|
| 354 | | - return -ENODEV; |
|---|
| 355 | | - |
|---|
| 356 | | - efuse->clk = NULL; |
|---|
| 266 | + efuse->clk = devm_clk_get(&pdev->dev, "pclk"); |
|---|
| 267 | + if (IS_ERR(efuse->clk)) { |
|---|
| 268 | + dev_err(dev, "failed to get pclk: %ld\n", PTR_ERR(efuse->clk)); |
|---|
| 269 | + return PTR_ERR(efuse->clk); |
|---|
| 357 | 270 | } |
|---|
| 358 | 271 | |
|---|
| 359 | 272 | efuse->avdd_gpio = devm_gpiod_get_optional(dev, "efuse", GPIOD_OUT_LOW); |
|---|
| 273 | + efuse->base = RK628_EFUSE_BASE; |
|---|
| 360 | 274 | efuse->dev = &pdev->dev; |
|---|
| 361 | | - plat_data->econfig->size = EFUSE_SIZE; |
|---|
| 362 | | - plat_data->econfig->reg_read = (void *)&rk628_efuse_read; |
|---|
| 363 | | - plat_data->econfig->priv = efuse; |
|---|
| 364 | | - plat_data->econfig->dev = efuse->dev; |
|---|
| 365 | | - nvmem = devm_nvmem_register(dev, plat_data->econfig); |
|---|
| 275 | + econfig.size = EFUSE_SIZE; |
|---|
| 276 | + econfig.reg_read = (void *)&rk628_efuse_read; |
|---|
| 277 | + econfig.priv = efuse; |
|---|
| 278 | + econfig.dev = efuse->dev; |
|---|
| 279 | + nvmem = devm_nvmem_register(&econfig); |
|---|
| 366 | 280 | if (IS_ERR(nvmem)) |
|---|
| 367 | 281 | return PTR_ERR(nvmem); |
|---|
| 368 | 282 | |
|---|