command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / board / sifive / fu540 / fu540.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2019 Western Digital Corporation or its affiliates.
4  *
5  * Authors:
6  *   Anup Patel <anup.patel@wdc.com>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <init.h>
13 #include <linux/delay.h>
14 #include <linux/io.h>
15
16 #ifdef CONFIG_MISC_INIT_R
17
18 #define FU540_OTP_BASE_ADDR                     0x10070000
19
20 struct fu540_otp_regs {
21         u32 pa;     /* Address input */
22         u32 paio;   /* Program address input */
23         u32 pas;    /* Program redundancy cell selection input */
24         u32 pce;    /* OTP Macro enable input */
25         u32 pclk;   /* Clock input */
26         u32 pdin;   /* Write data input */
27         u32 pdout;  /* Read data output */
28         u32 pdstb;  /* Deep standby mode enable input (active low) */
29         u32 pprog;  /* Program mode enable input */
30         u32 ptc;    /* Test column enable input */
31         u32 ptm;    /* Test mode enable input */
32         u32 ptm_rep;/* Repair function test mode enable input */
33         u32 ptr;    /* Test row enable input */
34         u32 ptrim;  /* Repair function enable input */
35         u32 pwe;    /* Write enable input (defines program cycle) */
36 } __packed;
37
38 #define BYTES_PER_FUSE                          4
39 #define NUM_FUSES                               0x1000
40
41 static int fu540_otp_read(int offset, void *buf, int size)
42 {
43         struct fu540_otp_regs *regs = (void __iomem *)FU540_OTP_BASE_ADDR;
44         unsigned int i;
45         int fuseidx = offset / BYTES_PER_FUSE;
46         int fusecount = size / BYTES_PER_FUSE;
47         u32 fusebuf[fusecount];
48
49         /* check bounds */
50         if (offset < 0 || size < 0)
51                 return -EINVAL;
52         if (fuseidx >= NUM_FUSES)
53                 return -EINVAL;
54         if ((fuseidx + fusecount) > NUM_FUSES)
55                 return -EINVAL;
56
57         /* init OTP */
58         writel(0x01, &regs->pdstb); /* wake up from stand-by */
59         writel(0x01, &regs->ptrim); /* enable repair function */
60         writel(0x01, &regs->pce);   /* enable input */
61
62         /* read all requested fuses */
63         for (i = 0; i < fusecount; i++, fuseidx++) {
64                 writel(fuseidx, &regs->pa);
65
66                 /* cycle clock to read */
67                 writel(0x01, &regs->pclk);
68                 mdelay(1);
69                 writel(0x00, &regs->pclk);
70                 mdelay(1);
71
72                 /* read the value */
73                 fusebuf[i] = readl(&regs->pdout);
74         }
75
76         /* shut down */
77         writel(0, &regs->pce);
78         writel(0, &regs->ptrim);
79         writel(0, &regs->pdstb);
80
81         /* copy out */
82         memcpy(buf, fusebuf, size);
83
84         return 0;
85 }
86
87 static u32 fu540_read_serialnum(void)
88 {
89         int ret;
90         u32 serial[2] = {0};
91
92         for (int i = 0xfe * 4; i > 0; i -= 8) {
93                 ret = fu540_otp_read(i, serial, sizeof(serial));
94                 if (ret) {
95                         printf("%s: error reading from OTP\n", __func__);
96                         break;
97                 }
98                 if (serial[0] == ~serial[1])
99                         return serial[0];
100         }
101
102         return 0;
103 }
104
105 static void fu540_setup_macaddr(u32 serialnum)
106 {
107         /* Default MAC address */
108         unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
109
110         /*
111          * We derive our board MAC address by ORing last three bytes
112          * of board serial number to above default MAC address.
113          *
114          * This logic of deriving board MAC address is taken from
115          * SiFive FSBL and is kept unchanged.
116          */
117         mac[5] |= (serialnum >>  0) & 0xff;
118         mac[4] |= (serialnum >>  8) & 0xff;
119         mac[3] |= (serialnum >> 16) & 0xff;
120
121         /* Update environment variable */
122         eth_env_set_enetaddr("ethaddr", mac);
123 }
124
125 int misc_init_r(void)
126 {
127         u32 serial_num;
128         char buf[9] = {0};
129
130         /* Set ethaddr environment variable from board serial number */
131         if (!env_get("serial#")) {
132                 serial_num = fu540_read_serialnum();
133                 if (!serial_num) {
134                         WARN(true, "Board serial number should not be 0 !!\n");
135                         return 0;
136                 }
137                 snprintf(buf, sizeof(buf), "%08x", serial_num);
138                 env_set("serial#", buf);
139                 fu540_setup_macaddr(serial_num);
140         }
141         return 0;
142 }
143
144 #endif
145
146 int board_init(void)
147 {
148         /* For now nothing to do here. */
149
150         return 0;
151 }