3f9ed0e38a6ff5da6eb62e9ec18be78a7e721752
[oweals/u-boot_mod.git] / u-boot / board / ar7240 / common / common.c
1 /*
2  * Common functions for QC/A WiSoCs based boards support
3  *
4  * Copyright (C) 2016 Piotr Dymacz <piotr@dymacz.pl>
5  *
6  * Partially based on:
7  * Linux/arch/mips/ath79/setup.c
8  *
9  * SPDX-License-Identifier: GPL-2.0
10  */
11
12 #include <config.h>
13 #include <common.h>
14 #include <flash.h>
15 #include <asm/mipsregs.h>
16 #include <asm/addrspace.h>
17 #include <soc/qca_soc_common.h>
18
19 #ifndef CONFIG_BOARD_CUSTOM_STRING
20         #define CONFIG_BOARD_CUSTOM_STRING      "Unknown/OEM"
21 #endif
22
23 #define ALIGN_SIZE              "8"
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 static u32 mac_is_not_valid = 1;
28
29 /*
30  * Put QCA SOC name, version and revision in buffer
31  */
32 void qca_soc_name_rev(char *buf)
33 {
34         u32 id;
35         u32 major;
36         u32 rev = 0;
37
38         if (buf == NULL)
39                 return;
40
41         /* Get revision ID value */
42         id = qca_soc_reg_read(QCA_RST_REVISION_ID_REG);
43
44         major = id & QCA_RST_REVISION_ID_MAJOR_MASK;
45         rev = id & QCA_RST_REVISION_ID_REV_MASK;
46
47         switch (major) {
48 #if (SOC_TYPE & QCA_AR933X_SOC)
49         case QCA_RST_REVISION_ID_MAJOR_AR9330_VAL:
50                 sprintf(buf, "AR9330 rev. %d", rev);
51                 break;
52         case QCA_RST_REVISION_ID_MAJOR_AR9331_VAL:
53                 sprintf(buf, "AR9331 rev. %d", rev);
54                 break;
55 #endif
56 #if (SOC_TYPE & QCA_AR934X_SOC)
57         case QCA_RST_REVISION_ID_MAJOR_AR9341_VAL:
58                 sprintf(buf, "AR9341 rev. %d", rev);
59                 break;
60         case QCA_RST_REVISION_ID_MAJOR_AR9344_VAL:
61                 sprintf(buf, "AR9344 rev. %d", rev);
62                 break;
63 #endif
64 #if (SOC_TYPE & QCA_QCA953X_SOC)
65         case QCA_RST_REVISION_ID_MAJOR_QCA953X_VAL:
66                 sprintf(buf, "QCA953x ver. 1 rev. %d", rev);
67                 break;
68         case QCA_RST_REVISION_ID_MAJOR_QCA953X_V2_VAL:
69                 sprintf(buf, "QCA953x ver. 2 rev. %d", rev);
70                 break;
71 #endif
72 #if (SOC_TYPE & QCA_QCA955X_SOC)
73         case QCA_RST_REVISION_ID_MAJOR_QCA9558_VAL:
74                 sprintf(buf, "QCA9558 rev. %d", rev);
75                 break;
76 #endif
77         default:
78                 sprintf(buf, "Unknown");
79                 break;
80         }
81 }
82
83 /*
84  * Prints available information about the board
85  */
86 void print_board_info(void)
87 {
88         u32 ahb_clk, cpu_clk, ddr_clk, spi_clk, ref_clk;
89         u32 bank;
90         bd_t *bd = gd->bd;
91         char buffer[24];
92
93         /* Board name */
94         printf("%" ALIGN_SIZE "s %s\n", "BOARD:", CONFIG_BOARD_CUSTOM_STRING);
95
96         /* SOC name, version and revision */
97         qca_soc_name_rev(buffer);
98         printf("%" ALIGN_SIZE "s %s\n", "SOC:", buffer);
99
100         /* MIPS CPU type */
101         cpu_name(buffer);
102         printf("%" ALIGN_SIZE "s %s\n", "CPU:", buffer);
103
104         /* RAM size and type */
105         printf("%" ALIGN_SIZE "s ", "RAM:");
106         print_size(bd->bi_memsize, "");
107
108         switch (qca_dram_type()) {
109         case RAM_MEMORY_TYPE_SDR:
110                 puts(" SDR\n");
111                 break;
112         case RAM_MEMORY_TYPE_DDR1:
113                 puts(" DDR1\n");
114                 break;
115         case RAM_MEMORY_TYPE_DDR2:
116                 puts(" DDR2\n");
117                 break;
118         default:
119                 puts("\n");
120                 break;
121         }
122
123         /* SPI NOR FLASH sizes and types */
124         printf("%" ALIGN_SIZE "s ", "FLASH:");
125
126         for (bank = 0; bank < CFG_MAX_FLASH_BANKS; bank++) {
127                 if (flash_info[bank].size == 0)
128                         continue;
129
130                 if (bank > 0)
131                         printf("%" ALIGN_SIZE "s ", " ");
132
133                 print_size(flash_info[bank].size, "");
134
135                 if (flash_info[bank].manuf_name != NULL)
136                         printf(" %s", flash_info[bank].manuf_name);
137
138                 if (flash_info[bank].model_name != NULL)
139                         printf(" %s", flash_info[bank].model_name);
140
141                 puts("\n");
142         }
143
144         /* MAC address */
145         printf("%" ALIGN_SIZE "s %02X:%02X:%02X:%02X:%02X:%02X", "MAC:",
146                 bd->bi_enetaddr[0],bd->bi_enetaddr[1], bd->bi_enetaddr[2],
147                 bd->bi_enetaddr[3], bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
148
149         if (mac_is_not_valid) {
150                 puts(" (fixed)\n");
151         } else {
152                 puts("\n");
153         }
154
155         /* System clocks */
156         printf("%" ALIGN_SIZE "s CPU/RAM/AHB/SPI/REF\n", "CLOCKS:");
157
158         qca_sys_clocks(&cpu_clk, &ddr_clk, &ahb_clk, &spi_clk, &ref_clk);
159         cpu_clk = cpu_clk / 1000000;
160         ddr_clk = ddr_clk / 1000000;
161         ahb_clk = ahb_clk / 1000000;
162         spi_clk = spi_clk / 1000000;
163         ref_clk = ref_clk / 1000000;
164
165         printf("%" ALIGN_SIZE "s %3d/%3d/%3d/%3d/%3d MHz\n",
166                 " ", cpu_clk, ddr_clk, ahb_clk, spi_clk, ref_clk);
167
168         puts("\n");
169 }
170
171 /*
172  * Reads MAC address if available or uses fixed one
173  */
174 void macaddr_init(u8 *mac_addr)
175 {
176         u8 buffer[6];
177         u8 fixed_mac[6] = {0x00, 0x03, 0x7F, 0x09, 0x0B, 0xAD};
178
179 #if defined(OFFSET_MAC_ADDRESS)
180         memcpy(buffer, (void *)(CFG_FLASH_BASE
181                 + OFFSET_MAC_DATA_BLOCK + OFFSET_MAC_ADDRESS), 6);
182
183         /*
184          * Check first LSBit (I/G bit) and second LSBit (U/L bit) in MSByte of vendor part
185          * both of them should be 0:
186          * I/G bit == 0 -> Individual MAC address (unicast address)
187          * U/L bit == 0 -> Burned-In-Address (BIA) MAC address
188          */
189         if (CHECK_BIT((buffer[0] & 0xFF), 0) != 0 ||
190                 CHECK_BIT((buffer[0] & 0xFF), 1) != 0) {
191                 memcpy(buffer, fixed_mac, 6);
192         } else {
193                 mac_is_not_valid = 0;
194         }
195 #else
196         memcpy(buffer, fixed_mac, 6);
197 #endif
198
199         memcpy(mac_addr, buffer, 6);
200 }
201
202 /*
203  * Returns "reset button" status:
204  * 1 -> button is pressed
205  * 0 -> button is not pressed
206  */
207 int reset_button_status(void)
208 {
209 #ifdef CONFIG_GPIO_RESET_BTN
210         u32 gpio;
211
212         gpio = qca_soc_reg_read(QCA_GPIO_IN_REG);
213
214         if (gpio & (1 << CONFIG_GPIO_RESET_BTN)) {
215         #if defined(CONFIG_GPIO_RESET_BTN_ACTIVE_LOW)
216                 return 0;
217         #else
218                 return 1;
219         #endif
220         } else {
221         #if defined(CONFIG_GPIO_RESET_BTN_ACTIVE_LOW)
222                 return 1;
223         #else
224                 return 0;
225         #endif
226         }
227 #else
228         return 0;
229 #endif
230 }
231
232 /*
233  * Returns main CPU clock in Hz
234  */
235 u32 main_cpu_clk(void)
236 {
237         u32 cpu_clk;
238
239         qca_sys_clocks(&cpu_clk, NULL, NULL, NULL, NULL);
240
241         return cpu_clk;
242 }
243
244 /*
245  * Calls full chip reset
246  */
247 void full_reset(void)
248 {
249         qca_full_chip_reset();
250 }