| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * i.MX6 OCOTP fusebox driver |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 9 | 10 | * |
|---|
| 10 | 11 | * Write support based on the fsl_otp driver, |
|---|
| 11 | 12 | * Copyright (C) 2010-2013 Freescale Semiconductor, Inc |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 14 | | - * it under the terms of the GNU General Public License version 2 |
|---|
| 15 | | - * as published by the Free Software Foundation. |
|---|
| 16 | | - * |
|---|
| 17 | | - * http://www.opensource.org/licenses/gpl-license.html |
|---|
| 18 | | - * http://www.gnu.org/copyleft/gpl.html |
|---|
| 19 | 13 | */ |
|---|
| 20 | 14 | |
|---|
| 21 | 15 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 45 | 39 | #define IMX_OCOTP_ADDR_DATA2 0x0040 |
|---|
| 46 | 40 | #define IMX_OCOTP_ADDR_DATA3 0x0050 |
|---|
| 47 | 41 | |
|---|
| 48 | | -#define IMX_OCOTP_BM_CTRL_ADDR 0x0000007F |
|---|
| 42 | +#define IMX_OCOTP_BM_CTRL_ADDR 0x000000FF |
|---|
| 49 | 43 | #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100 |
|---|
| 50 | 44 | #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200 |
|---|
| 51 | 45 | #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400 |
|---|
| 46 | + |
|---|
| 47 | +#define IMX_OCOTP_BM_CTRL_ADDR_8MP 0x000001FF |
|---|
| 48 | +#define IMX_OCOTP_BM_CTRL_BUSY_8MP 0x00000200 |
|---|
| 49 | +#define IMX_OCOTP_BM_CTRL_ERROR_8MP 0x00000400 |
|---|
| 50 | +#define IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP 0x00000800 |
|---|
| 51 | + |
|---|
| 52 | +#define IMX_OCOTP_BM_CTRL_DEFAULT \ |
|---|
| 53 | + { \ |
|---|
| 54 | + .bm_addr = IMX_OCOTP_BM_CTRL_ADDR, \ |
|---|
| 55 | + .bm_busy = IMX_OCOTP_BM_CTRL_BUSY, \ |
|---|
| 56 | + .bm_error = IMX_OCOTP_BM_CTRL_ERROR, \ |
|---|
| 57 | + .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS,\ |
|---|
| 58 | + } |
|---|
| 59 | + |
|---|
| 60 | +#define IMX_OCOTP_BM_CTRL_8MP \ |
|---|
| 61 | + { \ |
|---|
| 62 | + .bm_addr = IMX_OCOTP_BM_CTRL_ADDR_8MP, \ |
|---|
| 63 | + .bm_busy = IMX_OCOTP_BM_CTRL_BUSY_8MP, \ |
|---|
| 64 | + .bm_error = IMX_OCOTP_BM_CTRL_ERROR_8MP, \ |
|---|
| 65 | + .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP,\ |
|---|
| 66 | + } |
|---|
| 52 | 67 | |
|---|
| 53 | 68 | #define TIMING_STROBE_PROG_US 10 /* Min time to blow a fuse */ |
|---|
| 54 | 69 | #define TIMING_STROBE_READ_NS 37 /* Min time before read */ |
|---|
| .. | .. |
|---|
| 68 | 83 | struct nvmem_config *config; |
|---|
| 69 | 84 | }; |
|---|
| 70 | 85 | |
|---|
| 86 | +struct ocotp_ctrl_reg { |
|---|
| 87 | + u32 bm_addr; |
|---|
| 88 | + u32 bm_busy; |
|---|
| 89 | + u32 bm_error; |
|---|
| 90 | + u32 bm_rel_shadows; |
|---|
| 91 | +}; |
|---|
| 92 | + |
|---|
| 71 | 93 | struct ocotp_params { |
|---|
| 72 | 94 | unsigned int nregs; |
|---|
| 73 | 95 | unsigned int bank_address_words; |
|---|
| 74 | 96 | void (*set_timing)(struct ocotp_priv *priv); |
|---|
| 97 | + struct ocotp_ctrl_reg ctrl; |
|---|
| 75 | 98 | }; |
|---|
| 76 | 99 | |
|---|
| 77 | | -static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags) |
|---|
| 100 | +static int imx_ocotp_wait_for_busy(struct ocotp_priv *priv, u32 flags) |
|---|
| 78 | 101 | { |
|---|
| 79 | 102 | int count; |
|---|
| 80 | 103 | u32 c, mask; |
|---|
| 104 | + u32 bm_ctrl_busy, bm_ctrl_error; |
|---|
| 105 | + void __iomem *base = priv->base; |
|---|
| 81 | 106 | |
|---|
| 82 | | - mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags; |
|---|
| 107 | + bm_ctrl_busy = priv->params->ctrl.bm_busy; |
|---|
| 108 | + bm_ctrl_error = priv->params->ctrl.bm_error; |
|---|
| 109 | + |
|---|
| 110 | + mask = bm_ctrl_busy | bm_ctrl_error | flags; |
|---|
| 83 | 111 | |
|---|
| 84 | 112 | for (count = 10000; count >= 0; count--) { |
|---|
| 85 | 113 | c = readl(base + IMX_OCOTP_ADDR_CTRL); |
|---|
| .. | .. |
|---|
| 103 | 131 | * - A read is performed to from a fuse word which has been read |
|---|
| 104 | 132 | * locked. |
|---|
| 105 | 133 | */ |
|---|
| 106 | | - if (c & IMX_OCOTP_BM_CTRL_ERROR) |
|---|
| 134 | + if (c & bm_ctrl_error) |
|---|
| 107 | 135 | return -EPERM; |
|---|
| 108 | 136 | return -ETIMEDOUT; |
|---|
| 109 | 137 | } |
|---|
| .. | .. |
|---|
| 111 | 139 | return 0; |
|---|
| 112 | 140 | } |
|---|
| 113 | 141 | |
|---|
| 114 | | -static void imx_ocotp_clr_err_if_set(void __iomem *base) |
|---|
| 142 | +static void imx_ocotp_clr_err_if_set(struct ocotp_priv *priv) |
|---|
| 115 | 143 | { |
|---|
| 116 | | - u32 c; |
|---|
| 144 | + u32 c, bm_ctrl_error; |
|---|
| 145 | + void __iomem *base = priv->base; |
|---|
| 146 | + |
|---|
| 147 | + bm_ctrl_error = priv->params->ctrl.bm_error; |
|---|
| 117 | 148 | |
|---|
| 118 | 149 | c = readl(base + IMX_OCOTP_ADDR_CTRL); |
|---|
| 119 | | - if (!(c & IMX_OCOTP_BM_CTRL_ERROR)) |
|---|
| 150 | + if (!(c & bm_ctrl_error)) |
|---|
| 120 | 151 | return; |
|---|
| 121 | 152 | |
|---|
| 122 | | - writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR); |
|---|
| 153 | + writel(bm_ctrl_error, base + IMX_OCOTP_ADDR_CTRL_CLR); |
|---|
| 123 | 154 | } |
|---|
| 124 | 155 | |
|---|
| 125 | 156 | static int imx_ocotp_read(void *context, unsigned int offset, |
|---|
| .. | .. |
|---|
| 146 | 177 | return ret; |
|---|
| 147 | 178 | } |
|---|
| 148 | 179 | |
|---|
| 149 | | - ret = imx_ocotp_wait_for_busy(priv->base, 0); |
|---|
| 180 | + ret = imx_ocotp_wait_for_busy(priv, 0); |
|---|
| 150 | 181 | if (ret < 0) { |
|---|
| 151 | 182 | dev_err(priv->dev, "timeout during read setup\n"); |
|---|
| 152 | 183 | goto read_end; |
|---|
| .. | .. |
|---|
| 163 | 194 | * issued |
|---|
| 164 | 195 | */ |
|---|
| 165 | 196 | if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL) |
|---|
| 166 | | - imx_ocotp_clr_err_if_set(priv->base); |
|---|
| 197 | + imx_ocotp_clr_err_if_set(priv); |
|---|
| 167 | 198 | } |
|---|
| 168 | | - ret = 0; |
|---|
| 169 | 199 | |
|---|
| 170 | 200 | read_end: |
|---|
| 171 | 201 | clk_disable_unprepare(priv->clk); |
|---|
| .. | .. |
|---|
| 175 | 205 | |
|---|
| 176 | 206 | static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv) |
|---|
| 177 | 207 | { |
|---|
| 178 | | - unsigned long clk_rate = 0; |
|---|
| 208 | + unsigned long clk_rate; |
|---|
| 179 | 209 | unsigned long strobe_read, relax, strobe_prog; |
|---|
| 180 | | - u32 timing = 0; |
|---|
| 210 | + u32 timing; |
|---|
| 181 | 211 | |
|---|
| 182 | 212 | /* 47.3.1.3.1 |
|---|
| 183 | 213 | * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX] |
|---|
| .. | .. |
|---|
| 227 | 257 | |
|---|
| 228 | 258 | static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv) |
|---|
| 229 | 259 | { |
|---|
| 230 | | - unsigned long clk_rate = 0; |
|---|
| 260 | + unsigned long clk_rate; |
|---|
| 231 | 261 | u64 fsource, strobe_prog; |
|---|
| 232 | | - u32 timing = 0; |
|---|
| 262 | + u32 timing; |
|---|
| 233 | 263 | |
|---|
| 234 | 264 | /* i.MX 7Solo Applications Processor Reference Manual, Rev. 0.1 |
|---|
| 235 | 265 | * 6.4.3.3 |
|---|
| .. | .. |
|---|
| 280 | 310 | * write or reload must be completed before a write access can be |
|---|
| 281 | 311 | * requested. |
|---|
| 282 | 312 | */ |
|---|
| 283 | | - ret = imx_ocotp_wait_for_busy(priv->base, 0); |
|---|
| 313 | + ret = imx_ocotp_wait_for_busy(priv, 0); |
|---|
| 284 | 314 | if (ret < 0) { |
|---|
| 285 | 315 | dev_err(priv->dev, "timeout during timing setup\n"); |
|---|
| 286 | 316 | goto write_end; |
|---|
| .. | .. |
|---|
| 312 | 342 | } |
|---|
| 313 | 343 | |
|---|
| 314 | 344 | ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL); |
|---|
| 315 | | - ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR; |
|---|
| 316 | | - ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR; |
|---|
| 345 | + ctrl &= ~priv->params->ctrl.bm_addr; |
|---|
| 346 | + ctrl |= waddr & priv->params->ctrl.bm_addr; |
|---|
| 317 | 347 | ctrl |= IMX_OCOTP_WR_UNLOCK; |
|---|
| 318 | 348 | |
|---|
| 319 | 349 | writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL); |
|---|
| .. | .. |
|---|
| 380 | 410 | * be set. It must be cleared by software before any new write access |
|---|
| 381 | 411 | * can be issued. |
|---|
| 382 | 412 | */ |
|---|
| 383 | | - ret = imx_ocotp_wait_for_busy(priv->base, 0); |
|---|
| 413 | + ret = imx_ocotp_wait_for_busy(priv, 0); |
|---|
| 384 | 414 | if (ret < 0) { |
|---|
| 385 | 415 | if (ret == -EPERM) { |
|---|
| 386 | 416 | dev_err(priv->dev, "failed write to locked region"); |
|---|
| 387 | | - imx_ocotp_clr_err_if_set(priv->base); |
|---|
| 417 | + imx_ocotp_clr_err_if_set(priv); |
|---|
| 388 | 418 | } else { |
|---|
| 389 | 419 | dev_err(priv->dev, "timeout during data write\n"); |
|---|
| 390 | 420 | } |
|---|
| .. | .. |
|---|
| 400 | 430 | udelay(2); |
|---|
| 401 | 431 | |
|---|
| 402 | 432 | /* reload all shadow registers */ |
|---|
| 403 | | - writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS, |
|---|
| 433 | + writel(priv->params->ctrl.bm_rel_shadows, |
|---|
| 404 | 434 | priv->base + IMX_OCOTP_ADDR_CTRL_SET); |
|---|
| 405 | | - ret = imx_ocotp_wait_for_busy(priv->base, |
|---|
| 406 | | - IMX_OCOTP_BM_CTRL_REL_SHADOWS); |
|---|
| 407 | | - if (ret < 0) { |
|---|
| 435 | + ret = imx_ocotp_wait_for_busy(priv, |
|---|
| 436 | + priv->params->ctrl.bm_rel_shadows); |
|---|
| 437 | + if (ret < 0) |
|---|
| 408 | 438 | dev_err(priv->dev, "timeout during shadow register reload\n"); |
|---|
| 409 | | - goto write_end; |
|---|
| 410 | | - } |
|---|
| 411 | 439 | |
|---|
| 412 | 440 | write_end: |
|---|
| 413 | 441 | clk_disable_unprepare(priv->clk); |
|---|
| 414 | 442 | mutex_unlock(&ocotp_mutex); |
|---|
| 415 | | - if (ret < 0) |
|---|
| 416 | | - return ret; |
|---|
| 417 | | - return bytes; |
|---|
| 443 | + return ret < 0 ? ret : bytes; |
|---|
| 418 | 444 | } |
|---|
| 419 | 445 | |
|---|
| 420 | 446 | static struct nvmem_config imx_ocotp_nvmem_config = { |
|---|
| .. | .. |
|---|
| 430 | 456 | .nregs = 128, |
|---|
| 431 | 457 | .bank_address_words = 0, |
|---|
| 432 | 458 | .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 459 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 433 | 460 | }; |
|---|
| 434 | 461 | |
|---|
| 435 | 462 | static const struct ocotp_params imx6sl_params = { |
|---|
| 436 | 463 | .nregs = 64, |
|---|
| 437 | 464 | .bank_address_words = 0, |
|---|
| 438 | 465 | .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 466 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 439 | 467 | }; |
|---|
| 440 | 468 | |
|---|
| 441 | 469 | static const struct ocotp_params imx6sll_params = { |
|---|
| 442 | 470 | .nregs = 128, |
|---|
| 443 | 471 | .bank_address_words = 0, |
|---|
| 444 | 472 | .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 473 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 445 | 474 | }; |
|---|
| 446 | 475 | |
|---|
| 447 | 476 | static const struct ocotp_params imx6sx_params = { |
|---|
| 448 | 477 | .nregs = 128, |
|---|
| 449 | 478 | .bank_address_words = 0, |
|---|
| 450 | 479 | .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 480 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 451 | 481 | }; |
|---|
| 452 | 482 | |
|---|
| 453 | 483 | static const struct ocotp_params imx6ul_params = { |
|---|
| 454 | 484 | .nregs = 128, |
|---|
| 455 | 485 | .bank_address_words = 0, |
|---|
| 456 | 486 | .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 487 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 488 | +}; |
|---|
| 489 | + |
|---|
| 490 | +static const struct ocotp_params imx6ull_params = { |
|---|
| 491 | + .nregs = 64, |
|---|
| 492 | + .bank_address_words = 0, |
|---|
| 493 | + .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 494 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 457 | 495 | }; |
|---|
| 458 | 496 | |
|---|
| 459 | 497 | static const struct ocotp_params imx7d_params = { |
|---|
| 460 | 498 | .nregs = 64, |
|---|
| 461 | 499 | .bank_address_words = 4, |
|---|
| 462 | 500 | .set_timing = imx_ocotp_set_imx7_timing, |
|---|
| 501 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 502 | +}; |
|---|
| 503 | + |
|---|
| 504 | +static const struct ocotp_params imx7ulp_params = { |
|---|
| 505 | + .nregs = 256, |
|---|
| 506 | + .bank_address_words = 0, |
|---|
| 507 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 508 | +}; |
|---|
| 509 | + |
|---|
| 510 | +static const struct ocotp_params imx8mq_params = { |
|---|
| 511 | + .nregs = 256, |
|---|
| 512 | + .bank_address_words = 0, |
|---|
| 513 | + .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 514 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 515 | +}; |
|---|
| 516 | + |
|---|
| 517 | +static const struct ocotp_params imx8mm_params = { |
|---|
| 518 | + .nregs = 256, |
|---|
| 519 | + .bank_address_words = 0, |
|---|
| 520 | + .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 521 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 522 | +}; |
|---|
| 523 | + |
|---|
| 524 | +static const struct ocotp_params imx8mn_params = { |
|---|
| 525 | + .nregs = 256, |
|---|
| 526 | + .bank_address_words = 0, |
|---|
| 527 | + .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 528 | + .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT, |
|---|
| 529 | +}; |
|---|
| 530 | + |
|---|
| 531 | +static const struct ocotp_params imx8mp_params = { |
|---|
| 532 | + .nregs = 384, |
|---|
| 533 | + .bank_address_words = 0, |
|---|
| 534 | + .set_timing = imx_ocotp_set_imx6_timing, |
|---|
| 535 | + .ctrl = IMX_OCOTP_BM_CTRL_8MP, |
|---|
| 463 | 536 | }; |
|---|
| 464 | 537 | |
|---|
| 465 | 538 | static const struct of_device_id imx_ocotp_dt_ids[] = { |
|---|
| .. | .. |
|---|
| 467 | 540 | { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params }, |
|---|
| 468 | 541 | { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params }, |
|---|
| 469 | 542 | { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params }, |
|---|
| 543 | + { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params }, |
|---|
| 470 | 544 | { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params }, |
|---|
| 471 | 545 | { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params }, |
|---|
| 546 | + { .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params }, |
|---|
| 547 | + { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params }, |
|---|
| 548 | + { .compatible = "fsl,imx8mm-ocotp", .data = &imx8mm_params }, |
|---|
| 549 | + { .compatible = "fsl,imx8mn-ocotp", .data = &imx8mn_params }, |
|---|
| 550 | + { .compatible = "fsl,imx8mp-ocotp", .data = &imx8mp_params }, |
|---|
| 472 | 551 | { }, |
|---|
| 473 | 552 | }; |
|---|
| 474 | 553 | MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids); |
|---|
| .. | .. |
|---|
| 476 | 555 | static int imx_ocotp_probe(struct platform_device *pdev) |
|---|
| 477 | 556 | { |
|---|
| 478 | 557 | struct device *dev = &pdev->dev; |
|---|
| 479 | | - struct resource *res; |
|---|
| 480 | 558 | struct ocotp_priv *priv; |
|---|
| 481 | 559 | struct nvmem_device *nvmem; |
|---|
| 482 | 560 | |
|---|
| .. | .. |
|---|
| 486 | 564 | |
|---|
| 487 | 565 | priv->dev = dev; |
|---|
| 488 | 566 | |
|---|
| 489 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 490 | | - priv->base = devm_ioremap_resource(dev, res); |
|---|
| 567 | + priv->base = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 491 | 568 | if (IS_ERR(priv->base)) |
|---|
| 492 | 569 | return PTR_ERR(priv->base); |
|---|
| 493 | 570 | |
|---|
| .. | .. |
|---|
| 495 | 572 | if (IS_ERR(priv->clk)) |
|---|
| 496 | 573 | return PTR_ERR(priv->clk); |
|---|
| 497 | 574 | |
|---|
| 498 | | - clk_prepare_enable(priv->clk); |
|---|
| 499 | | - imx_ocotp_clr_err_if_set(priv->base); |
|---|
| 500 | | - clk_disable_unprepare(priv->clk); |
|---|
| 501 | | - |
|---|
| 502 | 575 | priv->params = of_device_get_match_data(&pdev->dev); |
|---|
| 503 | 576 | imx_ocotp_nvmem_config.size = 4 * priv->params->nregs; |
|---|
| 504 | 577 | imx_ocotp_nvmem_config.dev = dev; |
|---|
| 505 | 578 | imx_ocotp_nvmem_config.priv = priv; |
|---|
| 506 | 579 | priv->config = &imx_ocotp_nvmem_config; |
|---|
| 507 | | - nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config); |
|---|
| 508 | 580 | |
|---|
| 581 | + clk_prepare_enable(priv->clk); |
|---|
| 582 | + imx_ocotp_clr_err_if_set(priv); |
|---|
| 583 | + clk_disable_unprepare(priv->clk); |
|---|
| 584 | + |
|---|
| 585 | + nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config); |
|---|
| 509 | 586 | |
|---|
| 510 | 587 | return PTR_ERR_OR_ZERO(nvmem); |
|---|
| 511 | 588 | } |
|---|