0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/clk.h>
0018 #include <linux/device.h>
0019 #include <linux/io.h>
0020 #include <linux/module.h>
0021 #include <linux/nvmem-provider.h>
0022 #include <linux/of.h>
0023 #include <linux/of_device.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/slab.h>
0026 #include <linux/delay.h>
0027
0028 #define IMX_OCOTP_OFFSET_B0W0 0x400
0029
0030
0031 #define IMX_OCOTP_OFFSET_PER_WORD 0x10
0032
0033
0034
0035 #define IMX_OCOTP_ADDR_CTRL 0x0000
0036 #define IMX_OCOTP_ADDR_CTRL_SET 0x0004
0037 #define IMX_OCOTP_ADDR_CTRL_CLR 0x0008
0038 #define IMX_OCOTP_ADDR_TIMING 0x0010
0039 #define IMX_OCOTP_ADDR_DATA0 0x0020
0040 #define IMX_OCOTP_ADDR_DATA1 0x0030
0041 #define IMX_OCOTP_ADDR_DATA2 0x0040
0042 #define IMX_OCOTP_ADDR_DATA3 0x0050
0043
0044 #define IMX_OCOTP_BM_CTRL_ADDR 0x000000FF
0045 #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
0046 #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
0047 #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
0048
0049 #define IMX_OCOTP_BM_CTRL_ADDR_8MP 0x000001FF
0050 #define IMX_OCOTP_BM_CTRL_BUSY_8MP 0x00000200
0051 #define IMX_OCOTP_BM_CTRL_ERROR_8MP 0x00000400
0052 #define IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP 0x00000800
0053
0054 #define IMX_OCOTP_BM_CTRL_DEFAULT \
0055 { \
0056 .bm_addr = IMX_OCOTP_BM_CTRL_ADDR, \
0057 .bm_busy = IMX_OCOTP_BM_CTRL_BUSY, \
0058 .bm_error = IMX_OCOTP_BM_CTRL_ERROR, \
0059 .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS,\
0060 }
0061
0062 #define IMX_OCOTP_BM_CTRL_8MP \
0063 { \
0064 .bm_addr = IMX_OCOTP_BM_CTRL_ADDR_8MP, \
0065 .bm_busy = IMX_OCOTP_BM_CTRL_BUSY_8MP, \
0066 .bm_error = IMX_OCOTP_BM_CTRL_ERROR_8MP, \
0067 .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP,\
0068 }
0069
0070 #define TIMING_STROBE_PROG_US 10
0071 #define TIMING_STROBE_READ_NS 37
0072 #define TIMING_RELAX_NS 17
0073 #define DEF_FSOURCE 1001
0074 #define DEF_STROBE_PROG 10000
0075 #define IMX_OCOTP_WR_UNLOCK 0x3E770000
0076 #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
0077
0078 static DEFINE_MUTEX(ocotp_mutex);
0079
0080 struct ocotp_priv {
0081 struct device *dev;
0082 struct clk *clk;
0083 void __iomem *base;
0084 const struct ocotp_params *params;
0085 struct nvmem_config *config;
0086 };
0087
0088 struct ocotp_ctrl_reg {
0089 u32 bm_addr;
0090 u32 bm_busy;
0091 u32 bm_error;
0092 u32 bm_rel_shadows;
0093 };
0094
0095 struct ocotp_params {
0096 unsigned int nregs;
0097 unsigned int bank_address_words;
0098 void (*set_timing)(struct ocotp_priv *priv);
0099 struct ocotp_ctrl_reg ctrl;
0100 bool reverse_mac_address;
0101 };
0102
0103 static int imx_ocotp_wait_for_busy(struct ocotp_priv *priv, u32 flags)
0104 {
0105 int count;
0106 u32 c, mask;
0107 u32 bm_ctrl_busy, bm_ctrl_error;
0108 void __iomem *base = priv->base;
0109
0110 bm_ctrl_busy = priv->params->ctrl.bm_busy;
0111 bm_ctrl_error = priv->params->ctrl.bm_error;
0112
0113 mask = bm_ctrl_busy | bm_ctrl_error | flags;
0114
0115 for (count = 10000; count >= 0; count--) {
0116 c = readl(base + IMX_OCOTP_ADDR_CTRL);
0117 if (!(c & mask))
0118 break;
0119 cpu_relax();
0120 }
0121
0122 if (count < 0) {
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 if (c & bm_ctrl_error)
0138 return -EPERM;
0139 return -ETIMEDOUT;
0140 }
0141
0142 return 0;
0143 }
0144
0145 static void imx_ocotp_clr_err_if_set(struct ocotp_priv *priv)
0146 {
0147 u32 c, bm_ctrl_error;
0148 void __iomem *base = priv->base;
0149
0150 bm_ctrl_error = priv->params->ctrl.bm_error;
0151
0152 c = readl(base + IMX_OCOTP_ADDR_CTRL);
0153 if (!(c & bm_ctrl_error))
0154 return;
0155
0156 writel(bm_ctrl_error, base + IMX_OCOTP_ADDR_CTRL_CLR);
0157 }
0158
0159 static int imx_ocotp_read(void *context, unsigned int offset,
0160 void *val, size_t bytes)
0161 {
0162 struct ocotp_priv *priv = context;
0163 unsigned int count;
0164 u8 *buf, *p;
0165 int i, ret;
0166 u32 index, num_bytes;
0167
0168 index = offset >> 2;
0169 num_bytes = round_up((offset % 4) + bytes, 4);
0170 count = num_bytes >> 2;
0171
0172 if (count > (priv->params->nregs - index))
0173 count = priv->params->nregs - index;
0174
0175 p = kzalloc(num_bytes, GFP_KERNEL);
0176 if (!p)
0177 return -ENOMEM;
0178
0179 mutex_lock(&ocotp_mutex);
0180
0181 buf = p;
0182
0183 ret = clk_prepare_enable(priv->clk);
0184 if (ret < 0) {
0185 mutex_unlock(&ocotp_mutex);
0186 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
0187 kfree(p);
0188 return ret;
0189 }
0190
0191 ret = imx_ocotp_wait_for_busy(priv, 0);
0192 if (ret < 0) {
0193 dev_err(priv->dev, "timeout during read setup\n");
0194 goto read_end;
0195 }
0196
0197 for (i = index; i < (index + count); i++) {
0198 *(u32 *)buf = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
0199 i * IMX_OCOTP_OFFSET_PER_WORD);
0200
0201
0202
0203
0204
0205
0206
0207 if (*((u32 *)buf) == IMX_OCOTP_READ_LOCKED_VAL)
0208 imx_ocotp_clr_err_if_set(priv);
0209
0210 buf += 4;
0211 }
0212
0213 index = offset % 4;
0214 memcpy(val, &p[index], bytes);
0215
0216 read_end:
0217 clk_disable_unprepare(priv->clk);
0218 mutex_unlock(&ocotp_mutex);
0219
0220 kfree(p);
0221
0222 return ret;
0223 }
0224
0225 static int imx_ocotp_cell_pp(void *context, const char *id, unsigned int offset,
0226 void *data, size_t bytes)
0227 {
0228 struct ocotp_priv *priv = context;
0229
0230
0231 if (id && !strcmp(id, "mac-address")) {
0232 if (priv->params->reverse_mac_address) {
0233 u8 *buf = data;
0234 int i;
0235
0236 for (i = 0; i < bytes/2; i++)
0237 swap(buf[i], buf[bytes - i - 1]);
0238 }
0239 }
0240
0241 return 0;
0242 }
0243
0244 static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
0245 {
0246 unsigned long clk_rate;
0247 unsigned long strobe_read, relax, strobe_prog;
0248 u32 timing;
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278 clk_rate = clk_get_rate(priv->clk);
0279
0280 relax = DIV_ROUND_UP(clk_rate * TIMING_RELAX_NS, 1000000000) - 1;
0281 strobe_read = DIV_ROUND_UP(clk_rate * TIMING_STROBE_READ_NS,
0282 1000000000);
0283 strobe_read += 2 * (relax + 1) - 1;
0284 strobe_prog = DIV_ROUND_CLOSEST(clk_rate * TIMING_STROBE_PROG_US,
0285 1000000);
0286 strobe_prog += 2 * (relax + 1) - 1;
0287
0288 timing = readl(priv->base + IMX_OCOTP_ADDR_TIMING) & 0x0FC00000;
0289 timing |= strobe_prog & 0x00000FFF;
0290 timing |= (relax << 12) & 0x0000F000;
0291 timing |= (strobe_read << 16) & 0x003F0000;
0292
0293 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
0294 }
0295
0296 static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
0297 {
0298 unsigned long clk_rate;
0299 u64 fsource, strobe_prog;
0300 u32 timing;
0301
0302
0303
0304
0305 clk_rate = clk_get_rate(priv->clk);
0306 fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
0307 NSEC_PER_SEC) + 1;
0308 strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
0309 NSEC_PER_SEC) + 1;
0310
0311 timing = strobe_prog & 0x00000FFF;
0312 timing |= (fsource << 12) & 0x000FF000;
0313
0314 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
0315 }
0316
0317 static int imx_ocotp_write(void *context, unsigned int offset, void *val,
0318 size_t bytes)
0319 {
0320 struct ocotp_priv *priv = context;
0321 u32 *buf = val;
0322 int ret;
0323
0324 u32 ctrl;
0325 u8 waddr;
0326 u8 word = 0;
0327
0328
0329 if ((bytes != priv->config->word_size) ||
0330 (offset % priv->config->word_size))
0331 return -EINVAL;
0332
0333 mutex_lock(&ocotp_mutex);
0334
0335 ret = clk_prepare_enable(priv->clk);
0336 if (ret < 0) {
0337 mutex_unlock(&ocotp_mutex);
0338 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
0339 return ret;
0340 }
0341
0342
0343 priv->params->set_timing(priv);
0344
0345
0346
0347
0348
0349
0350
0351 ret = imx_ocotp_wait_for_busy(priv, 0);
0352 if (ret < 0) {
0353 dev_err(priv->dev, "timeout during timing setup\n");
0354 goto write_end;
0355 }
0356
0357
0358
0359
0360
0361
0362
0363
0364 if (priv->params->bank_address_words != 0) {
0365
0366
0367
0368
0369
0370 offset = offset / priv->config->word_size;
0371 waddr = offset / priv->params->bank_address_words;
0372 word = offset & (priv->params->bank_address_words - 1);
0373 } else {
0374
0375
0376
0377
0378
0379 waddr = offset / 4;
0380 }
0381
0382 ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
0383 ctrl &= ~priv->params->ctrl.bm_addr;
0384 ctrl |= waddr & priv->params->ctrl.bm_addr;
0385 ctrl |= IMX_OCOTP_WR_UNLOCK;
0386
0387 writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411 if (priv->params->bank_address_words != 0) {
0412
0413 switch (word) {
0414 case 0:
0415 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
0416 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
0417 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
0418 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
0419 break;
0420 case 1:
0421 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1);
0422 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
0423 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
0424 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
0425 break;
0426 case 2:
0427 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
0428 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
0429 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
0430 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
0431 break;
0432 case 3:
0433 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
0434 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
0435 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
0436 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
0437 break;
0438 }
0439 } else {
0440
0441 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
0442 }
0443
0444
0445
0446
0447
0448
0449
0450
0451 ret = imx_ocotp_wait_for_busy(priv, 0);
0452 if (ret < 0) {
0453 if (ret == -EPERM) {
0454 dev_err(priv->dev, "failed write to locked region");
0455 imx_ocotp_clr_err_if_set(priv);
0456 } else {
0457 dev_err(priv->dev, "timeout during data write\n");
0458 }
0459 goto write_end;
0460 }
0461
0462
0463
0464
0465
0466
0467
0468 udelay(2);
0469
0470
0471 writel(priv->params->ctrl.bm_rel_shadows,
0472 priv->base + IMX_OCOTP_ADDR_CTRL_SET);
0473 ret = imx_ocotp_wait_for_busy(priv,
0474 priv->params->ctrl.bm_rel_shadows);
0475 if (ret < 0)
0476 dev_err(priv->dev, "timeout during shadow register reload\n");
0477
0478 write_end:
0479 clk_disable_unprepare(priv->clk);
0480 mutex_unlock(&ocotp_mutex);
0481 return ret < 0 ? ret : bytes;
0482 }
0483
0484 static struct nvmem_config imx_ocotp_nvmem_config = {
0485 .name = "imx-ocotp",
0486 .read_only = false,
0487 .word_size = 4,
0488 .stride = 1,
0489 .reg_read = imx_ocotp_read,
0490 .reg_write = imx_ocotp_write,
0491 .cell_post_process = imx_ocotp_cell_pp,
0492 };
0493
0494 static const struct ocotp_params imx6q_params = {
0495 .nregs = 128,
0496 .bank_address_words = 0,
0497 .set_timing = imx_ocotp_set_imx6_timing,
0498 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0499 };
0500
0501 static const struct ocotp_params imx6sl_params = {
0502 .nregs = 64,
0503 .bank_address_words = 0,
0504 .set_timing = imx_ocotp_set_imx6_timing,
0505 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0506 };
0507
0508 static const struct ocotp_params imx6sll_params = {
0509 .nregs = 128,
0510 .bank_address_words = 0,
0511 .set_timing = imx_ocotp_set_imx6_timing,
0512 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0513 };
0514
0515 static const struct ocotp_params imx6sx_params = {
0516 .nregs = 128,
0517 .bank_address_words = 0,
0518 .set_timing = imx_ocotp_set_imx6_timing,
0519 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0520 };
0521
0522 static const struct ocotp_params imx6ul_params = {
0523 .nregs = 128,
0524 .bank_address_words = 0,
0525 .set_timing = imx_ocotp_set_imx6_timing,
0526 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0527 };
0528
0529 static const struct ocotp_params imx6ull_params = {
0530 .nregs = 64,
0531 .bank_address_words = 0,
0532 .set_timing = imx_ocotp_set_imx6_timing,
0533 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0534 };
0535
0536 static const struct ocotp_params imx7d_params = {
0537 .nregs = 64,
0538 .bank_address_words = 4,
0539 .set_timing = imx_ocotp_set_imx7_timing,
0540 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0541 };
0542
0543 static const struct ocotp_params imx7ulp_params = {
0544 .nregs = 256,
0545 .bank_address_words = 0,
0546 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0547 };
0548
0549 static const struct ocotp_params imx8mq_params = {
0550 .nregs = 256,
0551 .bank_address_words = 0,
0552 .set_timing = imx_ocotp_set_imx6_timing,
0553 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0554 .reverse_mac_address = true,
0555 };
0556
0557 static const struct ocotp_params imx8mm_params = {
0558 .nregs = 256,
0559 .bank_address_words = 0,
0560 .set_timing = imx_ocotp_set_imx6_timing,
0561 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0562 .reverse_mac_address = true,
0563 };
0564
0565 static const struct ocotp_params imx8mn_params = {
0566 .nregs = 256,
0567 .bank_address_words = 0,
0568 .set_timing = imx_ocotp_set_imx6_timing,
0569 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
0570 .reverse_mac_address = true,
0571 };
0572
0573 static const struct ocotp_params imx8mp_params = {
0574 .nregs = 384,
0575 .bank_address_words = 0,
0576 .set_timing = imx_ocotp_set_imx6_timing,
0577 .ctrl = IMX_OCOTP_BM_CTRL_8MP,
0578 .reverse_mac_address = true,
0579 };
0580
0581 static const struct of_device_id imx_ocotp_dt_ids[] = {
0582 { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
0583 { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
0584 { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
0585 { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
0586 { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params },
0587 { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
0588 { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
0589 { .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
0590 { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params },
0591 { .compatible = "fsl,imx8mm-ocotp", .data = &imx8mm_params },
0592 { .compatible = "fsl,imx8mn-ocotp", .data = &imx8mn_params },
0593 { .compatible = "fsl,imx8mp-ocotp", .data = &imx8mp_params },
0594 { },
0595 };
0596 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
0597
0598 static int imx_ocotp_probe(struct platform_device *pdev)
0599 {
0600 struct device *dev = &pdev->dev;
0601 struct ocotp_priv *priv;
0602 struct nvmem_device *nvmem;
0603
0604 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0605 if (!priv)
0606 return -ENOMEM;
0607
0608 priv->dev = dev;
0609
0610 priv->base = devm_platform_ioremap_resource(pdev, 0);
0611 if (IS_ERR(priv->base))
0612 return PTR_ERR(priv->base);
0613
0614 priv->clk = devm_clk_get(dev, NULL);
0615 if (IS_ERR(priv->clk))
0616 return PTR_ERR(priv->clk);
0617
0618 priv->params = of_device_get_match_data(&pdev->dev);
0619 imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
0620 imx_ocotp_nvmem_config.dev = dev;
0621 imx_ocotp_nvmem_config.priv = priv;
0622 priv->config = &imx_ocotp_nvmem_config;
0623
0624 clk_prepare_enable(priv->clk);
0625 imx_ocotp_clr_err_if_set(priv);
0626 clk_disable_unprepare(priv->clk);
0627
0628 nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
0629
0630 return PTR_ERR_OR_ZERO(nvmem);
0631 }
0632
0633 static struct platform_driver imx_ocotp_driver = {
0634 .probe = imx_ocotp_probe,
0635 .driver = {
0636 .name = "imx_ocotp",
0637 .of_match_table = imx_ocotp_dt_ids,
0638 },
0639 };
0640 module_platform_driver(imx_ocotp_driver);
0641
0642 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
0643 MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
0644 MODULE_LICENSE("GPL v2");