1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
12 #include <linux/delay.h>
15 #ifdef CONFIG_MISC_INIT_R
17 #define FU540_OTP_BASE_ADDR 0x10070000
19 struct fu540_otp_regs {
20 u32 pa; /* Address input */
21 u32 paio; /* Program address input */
22 u32 pas; /* Program redundancy cell selection input */
23 u32 pce; /* OTP Macro enable input */
24 u32 pclk; /* Clock input */
25 u32 pdin; /* Write data input */
26 u32 pdout; /* Read data output */
27 u32 pdstb; /* Deep standby mode enable input (active low) */
28 u32 pprog; /* Program mode enable input */
29 u32 ptc; /* Test column enable input */
30 u32 ptm; /* Test mode enable input */
31 u32 ptm_rep;/* Repair function test mode enable input */
32 u32 ptr; /* Test row enable input */
33 u32 ptrim; /* Repair function enable input */
34 u32 pwe; /* Write enable input (defines program cycle) */
37 #define BYTES_PER_FUSE 4
38 #define NUM_FUSES 0x1000
40 static int fu540_otp_read(int offset, void *buf, int size)
42 struct fu540_otp_regs *regs = (void __iomem *)FU540_OTP_BASE_ADDR;
44 int fuseidx = offset / BYTES_PER_FUSE;
45 int fusecount = size / BYTES_PER_FUSE;
46 u32 fusebuf[fusecount];
49 if (offset < 0 || size < 0)
51 if (fuseidx >= NUM_FUSES)
53 if ((fuseidx + fusecount) > NUM_FUSES)
57 writel(0x01, ®s->pdstb); /* wake up from stand-by */
58 writel(0x01, ®s->ptrim); /* enable repair function */
59 writel(0x01, ®s->pce); /* enable input */
61 /* read all requested fuses */
62 for (i = 0; i < fusecount; i++, fuseidx++) {
63 writel(fuseidx, ®s->pa);
65 /* cycle clock to read */
66 writel(0x01, ®s->pclk);
68 writel(0x00, ®s->pclk);
72 fusebuf[i] = readl(®s->pdout);
76 writel(0, ®s->pce);
77 writel(0, ®s->ptrim);
78 writel(0, ®s->pdstb);
81 memcpy(buf, fusebuf, size);
86 static u32 fu540_read_serialnum(void)
91 for (int i = 0xfe * 4; i > 0; i -= 8) {
92 ret = fu540_otp_read(i, serial, sizeof(serial));
94 printf("%s: error reading from OTP\n", __func__);
97 if (serial[0] == ~serial[1])
104 static void fu540_setup_macaddr(u32 serialnum)
106 /* Default MAC address */
107 unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
110 * We derive our board MAC address by ORing last three bytes
111 * of board serial number to above default MAC address.
113 * This logic of deriving board MAC address is taken from
114 * SiFive FSBL and is kept unchanged.
116 mac[5] |= (serialnum >> 0) & 0xff;
117 mac[4] |= (serialnum >> 8) & 0xff;
118 mac[3] |= (serialnum >> 16) & 0xff;
120 /* Update environment variable */
121 eth_env_set_enetaddr("ethaddr", mac);
124 int misc_init_r(void)
129 /* Set ethaddr environment variable from board serial number */
130 if (!env_get("serial#")) {
131 serial_num = fu540_read_serialnum();
133 WARN(true, "Board serial number should not be 0 !!\n");
136 snprintf(buf, sizeof(buf), "%08x", serial_num);
137 env_set("serial#", buf);
138 fu540_setup_macaddr(serial_num);
147 /* For now nothing to do here. */