1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22bbcccf5SMarek Vasut /*
32bbcccf5SMarek Vasut * Freescale i.MX28 OCOTP Driver
42bbcccf5SMarek Vasut *
52bbcccf5SMarek Vasut * Copyright (C) 2014 Marek Vasut <marex@denx.de>
62bbcccf5SMarek Vasut *
72bbcccf5SMarek Vasut * Note: The i.MX23/i.MX28 OCOTP block is a predecessor to the OCOTP block
82bbcccf5SMarek Vasut * used in i.MX6 . While these blocks are very similar at the first
92bbcccf5SMarek Vasut * glance, by digging deeper, one will notice differences (like the
102bbcccf5SMarek Vasut * tight dependence on MXS power block, some completely new registers
112bbcccf5SMarek Vasut * etc.) which would make common driver an ifdef nightmare :-(
122bbcccf5SMarek Vasut */
132bbcccf5SMarek Vasut
142bbcccf5SMarek Vasut #include <common.h>
152bbcccf5SMarek Vasut #include <fuse.h>
161221ce45SMasahiro Yamada #include <linux/errno.h>
172bbcccf5SMarek Vasut #include <asm/io.h>
182bbcccf5SMarek Vasut #include <asm/arch/clock.h>
192bbcccf5SMarek Vasut #include <asm/arch/imx-regs.h>
202bbcccf5SMarek Vasut #include <asm/arch/sys_proto.h>
212bbcccf5SMarek Vasut
222bbcccf5SMarek Vasut #define MXS_OCOTP_TIMEOUT 100000
232bbcccf5SMarek Vasut
242bbcccf5SMarek Vasut static struct mxs_ocotp_regs *ocotp_regs =
252bbcccf5SMarek Vasut (struct mxs_ocotp_regs *)MXS_OCOTP_BASE;
262bbcccf5SMarek Vasut static struct mxs_power_regs *power_regs =
272bbcccf5SMarek Vasut (struct mxs_power_regs *)MXS_POWER_BASE;
282bbcccf5SMarek Vasut static struct mxs_clkctrl_regs *clkctrl_regs =
292bbcccf5SMarek Vasut (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
302bbcccf5SMarek Vasut
mxs_ocotp_wait_busy_clear(void)312bbcccf5SMarek Vasut static int mxs_ocotp_wait_busy_clear(void)
322bbcccf5SMarek Vasut {
332bbcccf5SMarek Vasut uint32_t reg;
342bbcccf5SMarek Vasut int timeout = MXS_OCOTP_TIMEOUT;
352bbcccf5SMarek Vasut
362bbcccf5SMarek Vasut while (--timeout) {
372bbcccf5SMarek Vasut reg = readl(&ocotp_regs->hw_ocotp_ctrl);
382bbcccf5SMarek Vasut if (!(reg & OCOTP_CTRL_BUSY))
392bbcccf5SMarek Vasut break;
402bbcccf5SMarek Vasut udelay(10);
412bbcccf5SMarek Vasut }
422bbcccf5SMarek Vasut
432bbcccf5SMarek Vasut if (!timeout)
442bbcccf5SMarek Vasut return -EINVAL;
452bbcccf5SMarek Vasut
462bbcccf5SMarek Vasut /* Wait a little as per FSL datasheet's 'write postamble' section. */
472bbcccf5SMarek Vasut udelay(10);
482bbcccf5SMarek Vasut
492bbcccf5SMarek Vasut return 0;
502bbcccf5SMarek Vasut }
512bbcccf5SMarek Vasut
mxs_ocotp_clear_error(void)522bbcccf5SMarek Vasut static void mxs_ocotp_clear_error(void)
532bbcccf5SMarek Vasut {
542bbcccf5SMarek Vasut writel(OCOTP_CTRL_ERROR, &ocotp_regs->hw_ocotp_ctrl_clr);
552bbcccf5SMarek Vasut }
562bbcccf5SMarek Vasut
mxs_ocotp_read_bank_open(bool open)572bbcccf5SMarek Vasut static int mxs_ocotp_read_bank_open(bool open)
582bbcccf5SMarek Vasut {
592bbcccf5SMarek Vasut int ret = 0;
602bbcccf5SMarek Vasut
612bbcccf5SMarek Vasut if (open) {
622bbcccf5SMarek Vasut writel(OCOTP_CTRL_RD_BANK_OPEN,
632bbcccf5SMarek Vasut &ocotp_regs->hw_ocotp_ctrl_set);
642bbcccf5SMarek Vasut
652bbcccf5SMarek Vasut /*
662bbcccf5SMarek Vasut * Wait before polling the BUSY bit, since the BUSY bit might
672bbcccf5SMarek Vasut * be asserted only after a few HCLK cycles and if we were to
682bbcccf5SMarek Vasut * poll immediatelly, we could miss the busy bit.
692bbcccf5SMarek Vasut */
702bbcccf5SMarek Vasut udelay(10);
712bbcccf5SMarek Vasut ret = mxs_ocotp_wait_busy_clear();
722bbcccf5SMarek Vasut } else {
732bbcccf5SMarek Vasut writel(OCOTP_CTRL_RD_BANK_OPEN,
742bbcccf5SMarek Vasut &ocotp_regs->hw_ocotp_ctrl_clr);
752bbcccf5SMarek Vasut }
762bbcccf5SMarek Vasut
772bbcccf5SMarek Vasut return ret;
782bbcccf5SMarek Vasut }
792bbcccf5SMarek Vasut
mxs_ocotp_scale_vddio(bool enter,uint32_t * val)802bbcccf5SMarek Vasut static void mxs_ocotp_scale_vddio(bool enter, uint32_t *val)
812bbcccf5SMarek Vasut {
822bbcccf5SMarek Vasut uint32_t scale_val;
832bbcccf5SMarek Vasut
842bbcccf5SMarek Vasut if (enter) {
852bbcccf5SMarek Vasut /*
862bbcccf5SMarek Vasut * Enter the fuse programming VDDIO voltage setup. We start
872bbcccf5SMarek Vasut * scaling the voltage from it's current value down to 2.8V
882bbcccf5SMarek Vasut * which is the one and only correct voltage for programming
892bbcccf5SMarek Vasut * the OCOTP fuses (according to datasheet).
902bbcccf5SMarek Vasut */
912bbcccf5SMarek Vasut scale_val = readl(&power_regs->hw_power_vddioctrl);
922bbcccf5SMarek Vasut scale_val &= POWER_VDDIOCTRL_TRG_MASK;
932bbcccf5SMarek Vasut
942bbcccf5SMarek Vasut /* Return the original voltage. */
952bbcccf5SMarek Vasut *val = scale_val;
962bbcccf5SMarek Vasut
972bbcccf5SMarek Vasut /*
982bbcccf5SMarek Vasut * Start scaling VDDIO down to 0x2, which is 2.8V . Actually,
992bbcccf5SMarek Vasut * the value 0x0 should be 2.8V, but that's not the case on
1002bbcccf5SMarek Vasut * most designs due to load etc., so we play safe. Undervolt
1012bbcccf5SMarek Vasut * can actually cause incorrect programming of the fuses and
1022bbcccf5SMarek Vasut * or reboots of the board.
1032bbcccf5SMarek Vasut */
1042bbcccf5SMarek Vasut while (scale_val > 2) {
1052bbcccf5SMarek Vasut clrsetbits_le32(&power_regs->hw_power_vddioctrl,
1062bbcccf5SMarek Vasut POWER_VDDIOCTRL_TRG_MASK, --scale_val);
1072bbcccf5SMarek Vasut udelay(500);
1082bbcccf5SMarek Vasut }
1092bbcccf5SMarek Vasut } else {
1102bbcccf5SMarek Vasut /* Start scaling VDDIO up to original value . */
1112bbcccf5SMarek Vasut for (scale_val = 2; scale_val <= *val; scale_val++) {
1122bbcccf5SMarek Vasut clrsetbits_le32(&power_regs->hw_power_vddioctrl,
1132bbcccf5SMarek Vasut POWER_VDDIOCTRL_TRG_MASK, scale_val);
1142bbcccf5SMarek Vasut udelay(500);
1152bbcccf5SMarek Vasut }
1162bbcccf5SMarek Vasut }
1172bbcccf5SMarek Vasut
1182bbcccf5SMarek Vasut mdelay(10);
1192bbcccf5SMarek Vasut }
1202bbcccf5SMarek Vasut
mxs_ocotp_wait_hclk_ready(void)1212bbcccf5SMarek Vasut static int mxs_ocotp_wait_hclk_ready(void)
1222bbcccf5SMarek Vasut {
1232bbcccf5SMarek Vasut uint32_t reg, timeout = MXS_OCOTP_TIMEOUT;
1242bbcccf5SMarek Vasut
1252bbcccf5SMarek Vasut while (--timeout) {
1262bbcccf5SMarek Vasut reg = readl(&clkctrl_regs->hw_clkctrl_hbus);
1272bbcccf5SMarek Vasut if (!(reg & CLKCTRL_HBUS_ASM_BUSY))
1282bbcccf5SMarek Vasut break;
1292bbcccf5SMarek Vasut }
1302bbcccf5SMarek Vasut
1312bbcccf5SMarek Vasut if (!timeout)
1322bbcccf5SMarek Vasut return -EINVAL;
1332bbcccf5SMarek Vasut
1342bbcccf5SMarek Vasut return 0;
1352bbcccf5SMarek Vasut }
1362bbcccf5SMarek Vasut
mxs_ocotp_scale_hclk(bool enter,uint32_t * val)1372bbcccf5SMarek Vasut static int mxs_ocotp_scale_hclk(bool enter, uint32_t *val)
1382bbcccf5SMarek Vasut {
1392bbcccf5SMarek Vasut uint32_t scale_val;
1402bbcccf5SMarek Vasut int ret;
1412bbcccf5SMarek Vasut
1422bbcccf5SMarek Vasut ret = mxs_ocotp_wait_hclk_ready();
1432bbcccf5SMarek Vasut if (ret)
1442bbcccf5SMarek Vasut return ret;
1452bbcccf5SMarek Vasut
1462bbcccf5SMarek Vasut /* Set CPU bypass */
1472bbcccf5SMarek Vasut writel(CLKCTRL_CLKSEQ_BYPASS_CPU,
1482bbcccf5SMarek Vasut &clkctrl_regs->hw_clkctrl_clkseq_set);
1492bbcccf5SMarek Vasut
1502bbcccf5SMarek Vasut if (enter) {
1512bbcccf5SMarek Vasut /* Return the original HCLK clock speed. */
1522bbcccf5SMarek Vasut *val = readl(&clkctrl_regs->hw_clkctrl_hbus);
1532bbcccf5SMarek Vasut *val &= CLKCTRL_HBUS_DIV_MASK;
154d4b8b5d4SChris Smith *val >>= CLKCTRL_HBUS_DIV_OFFSET;
1552bbcccf5SMarek Vasut
1562bbcccf5SMarek Vasut /* Scale the HCLK to 454/19 = 23.9 MHz . */
1572bbcccf5SMarek Vasut scale_val = (~19) << CLKCTRL_HBUS_DIV_OFFSET;
1582bbcccf5SMarek Vasut scale_val &= CLKCTRL_HBUS_DIV_MASK;
1592bbcccf5SMarek Vasut } else {
1602bbcccf5SMarek Vasut /* Scale the HCLK back to original frequency. */
1612bbcccf5SMarek Vasut scale_val = (~(*val)) << CLKCTRL_HBUS_DIV_OFFSET;
1622bbcccf5SMarek Vasut scale_val &= CLKCTRL_HBUS_DIV_MASK;
1632bbcccf5SMarek Vasut }
1642bbcccf5SMarek Vasut
1652bbcccf5SMarek Vasut writel(CLKCTRL_HBUS_DIV_MASK,
1662bbcccf5SMarek Vasut &clkctrl_regs->hw_clkctrl_hbus_set);
1672bbcccf5SMarek Vasut writel(scale_val,
1682bbcccf5SMarek Vasut &clkctrl_regs->hw_clkctrl_hbus_clr);
1692bbcccf5SMarek Vasut
1702bbcccf5SMarek Vasut mdelay(10);
1712bbcccf5SMarek Vasut
1722bbcccf5SMarek Vasut ret = mxs_ocotp_wait_hclk_ready();
1732bbcccf5SMarek Vasut if (ret)
1742bbcccf5SMarek Vasut return ret;
1752bbcccf5SMarek Vasut
1762bbcccf5SMarek Vasut /* Disable CPU bypass */
1772bbcccf5SMarek Vasut writel(CLKCTRL_CLKSEQ_BYPASS_CPU,
1782bbcccf5SMarek Vasut &clkctrl_regs->hw_clkctrl_clkseq_clr);
1792bbcccf5SMarek Vasut
1802bbcccf5SMarek Vasut mdelay(10);
1812bbcccf5SMarek Vasut
1822bbcccf5SMarek Vasut return 0;
1832bbcccf5SMarek Vasut }
1842bbcccf5SMarek Vasut
mxs_ocotp_write_fuse(uint32_t addr,uint32_t mask)1852bbcccf5SMarek Vasut static int mxs_ocotp_write_fuse(uint32_t addr, uint32_t mask)
1862bbcccf5SMarek Vasut {
1872bbcccf5SMarek Vasut uint32_t hclk_val, vddio_val;
1882bbcccf5SMarek Vasut int ret;
1892bbcccf5SMarek Vasut
1903d99fcbcSHector Palacios mxs_ocotp_clear_error();
1913d99fcbcSHector Palacios
1922bbcccf5SMarek Vasut /* Make sure the banks are closed for reading. */
1932bbcccf5SMarek Vasut ret = mxs_ocotp_read_bank_open(0);
1942bbcccf5SMarek Vasut if (ret) {
1952bbcccf5SMarek Vasut puts("Failed closing banks for reading!\n");
1962bbcccf5SMarek Vasut return ret;
1972bbcccf5SMarek Vasut }
1982bbcccf5SMarek Vasut
1992bbcccf5SMarek Vasut ret = mxs_ocotp_scale_hclk(1, &hclk_val);
2002bbcccf5SMarek Vasut if (ret) {
2012bbcccf5SMarek Vasut puts("Failed scaling down the HCLK!\n");
2022bbcccf5SMarek Vasut return ret;
2032bbcccf5SMarek Vasut }
2042bbcccf5SMarek Vasut mxs_ocotp_scale_vddio(1, &vddio_val);
2052bbcccf5SMarek Vasut
2062bbcccf5SMarek Vasut ret = mxs_ocotp_wait_busy_clear();
2072bbcccf5SMarek Vasut if (ret) {
2082bbcccf5SMarek Vasut puts("Failed waiting for ready state!\n");
2092bbcccf5SMarek Vasut goto fail;
2102bbcccf5SMarek Vasut }
2112bbcccf5SMarek Vasut
2122bbcccf5SMarek Vasut /* Program the fuse address */
2132bbcccf5SMarek Vasut writel(addr | OCOTP_CTRL_WR_UNLOCK_KEY, &ocotp_regs->hw_ocotp_ctrl);
2142bbcccf5SMarek Vasut
2152bbcccf5SMarek Vasut /* Program the data. */
2162bbcccf5SMarek Vasut writel(mask, &ocotp_regs->hw_ocotp_data);
2172bbcccf5SMarek Vasut
2182bbcccf5SMarek Vasut udelay(10);
2192bbcccf5SMarek Vasut
2202bbcccf5SMarek Vasut ret = mxs_ocotp_wait_busy_clear();
2212bbcccf5SMarek Vasut if (ret) {
2222bbcccf5SMarek Vasut puts("Failed waiting for ready state!\n");
2232bbcccf5SMarek Vasut goto fail;
2242bbcccf5SMarek Vasut }
2252bbcccf5SMarek Vasut
226ad5dd7aeSHector Palacios /* Check for errors */
227ad5dd7aeSHector Palacios if (readl(&ocotp_regs->hw_ocotp_ctrl) & OCOTP_CTRL_ERROR) {
228ad5dd7aeSHector Palacios puts("Failed writing fuses!\n");
229ad5dd7aeSHector Palacios ret = -EPERM;
230ad5dd7aeSHector Palacios goto fail;
231ad5dd7aeSHector Palacios }
232ad5dd7aeSHector Palacios
2332bbcccf5SMarek Vasut fail:
2342bbcccf5SMarek Vasut mxs_ocotp_scale_vddio(0, &vddio_val);
235d8d160e4SHector Palacios if (mxs_ocotp_scale_hclk(0, &hclk_val))
2362bbcccf5SMarek Vasut puts("Failed scaling up the HCLK!\n");
2372bbcccf5SMarek Vasut
2382bbcccf5SMarek Vasut return ret;
2392bbcccf5SMarek Vasut }
2402bbcccf5SMarek Vasut
mxs_ocotp_read_fuse(uint32_t reg,uint32_t * val)2412bbcccf5SMarek Vasut static int mxs_ocotp_read_fuse(uint32_t reg, uint32_t *val)
2422bbcccf5SMarek Vasut {
2432bbcccf5SMarek Vasut int ret;
2442bbcccf5SMarek Vasut
2452bbcccf5SMarek Vasut /* Register offset from CUST0 */
2462bbcccf5SMarek Vasut reg = ((uint32_t)&ocotp_regs->hw_ocotp_cust0) + (reg << 4);
2472bbcccf5SMarek Vasut
2482bbcccf5SMarek Vasut ret = mxs_ocotp_wait_busy_clear();
2492bbcccf5SMarek Vasut if (ret) {
2502bbcccf5SMarek Vasut puts("Failed waiting for ready state!\n");
2512bbcccf5SMarek Vasut return ret;
2522bbcccf5SMarek Vasut }
2532bbcccf5SMarek Vasut
2542bbcccf5SMarek Vasut mxs_ocotp_clear_error();
2552bbcccf5SMarek Vasut
2562bbcccf5SMarek Vasut ret = mxs_ocotp_read_bank_open(1);
2572bbcccf5SMarek Vasut if (ret) {
2582bbcccf5SMarek Vasut puts("Failed opening banks for reading!\n");
2592bbcccf5SMarek Vasut return ret;
2602bbcccf5SMarek Vasut }
2612bbcccf5SMarek Vasut
2622bbcccf5SMarek Vasut *val = readl(reg);
2632bbcccf5SMarek Vasut
2642bbcccf5SMarek Vasut ret = mxs_ocotp_read_bank_open(0);
2652bbcccf5SMarek Vasut if (ret) {
2662bbcccf5SMarek Vasut puts("Failed closing banks for reading!\n");
2672bbcccf5SMarek Vasut return ret;
2682bbcccf5SMarek Vasut }
2692bbcccf5SMarek Vasut
2702bbcccf5SMarek Vasut return ret;
2712bbcccf5SMarek Vasut }
2722bbcccf5SMarek Vasut
mxs_ocotp_valid(u32 bank,u32 word)2732bbcccf5SMarek Vasut static int mxs_ocotp_valid(u32 bank, u32 word)
2742bbcccf5SMarek Vasut {
2752bbcccf5SMarek Vasut if (bank > 4)
2762bbcccf5SMarek Vasut return -EINVAL;
2772bbcccf5SMarek Vasut if (word > 7)
2782bbcccf5SMarek Vasut return -EINVAL;
2792bbcccf5SMarek Vasut return 0;
2802bbcccf5SMarek Vasut }
2812bbcccf5SMarek Vasut
2822bbcccf5SMarek Vasut /*
2832bbcccf5SMarek Vasut * The 'fuse' command API
2842bbcccf5SMarek Vasut */
fuse_read(u32 bank,u32 word,u32 * val)2852bbcccf5SMarek Vasut int fuse_read(u32 bank, u32 word, u32 *val)
2862bbcccf5SMarek Vasut {
2872bbcccf5SMarek Vasut int ret;
2882bbcccf5SMarek Vasut
2892bbcccf5SMarek Vasut ret = mxs_ocotp_valid(bank, word);
2902bbcccf5SMarek Vasut if (ret)
2912bbcccf5SMarek Vasut return ret;
2922bbcccf5SMarek Vasut
2932bbcccf5SMarek Vasut return mxs_ocotp_read_fuse((bank << 3) | word, val);
2942bbcccf5SMarek Vasut }
2952bbcccf5SMarek Vasut
fuse_prog(u32 bank,u32 word,u32 val)2962bbcccf5SMarek Vasut int fuse_prog(u32 bank, u32 word, u32 val)
2972bbcccf5SMarek Vasut {
2982bbcccf5SMarek Vasut int ret;
2992bbcccf5SMarek Vasut
3002bbcccf5SMarek Vasut ret = mxs_ocotp_valid(bank, word);
3012bbcccf5SMarek Vasut if (ret)
3022bbcccf5SMarek Vasut return ret;
3032bbcccf5SMarek Vasut
3042bbcccf5SMarek Vasut return mxs_ocotp_write_fuse((bank << 3) | word, val);
3052bbcccf5SMarek Vasut }
3062bbcccf5SMarek Vasut
fuse_sense(u32 bank,u32 word,u32 * val)3072bbcccf5SMarek Vasut int fuse_sense(u32 bank, u32 word, u32 *val)
3082bbcccf5SMarek Vasut {
3092bbcccf5SMarek Vasut /* We do not support sensing :-( */
3102bbcccf5SMarek Vasut return -EINVAL;
3112bbcccf5SMarek Vasut }
3122bbcccf5SMarek Vasut
fuse_override(u32 bank,u32 word,u32 val)3132bbcccf5SMarek Vasut int fuse_override(u32 bank, u32 word, u32 val)
3142bbcccf5SMarek Vasut {
3152bbcccf5SMarek Vasut /* We do not support overriding :-( */
3162bbcccf5SMarek Vasut return -EINVAL;
3172bbcccf5SMarek Vasut }
318