Add functions which return DDR width and CAS latency, use them in printing board...
[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 ");
111                 break;
112         case RAM_MEMORY_TYPE_DDR1:
113                 puts(" DDR1 ");
114                 break;
115         case RAM_MEMORY_TYPE_DDR2:
116                 puts(" DDR2 ");
117                 break;
118         default:
119                 break;
120         }
121
122         /* DDR interface width */
123         printf("%d-bit ", qca_dram_ddr_width());
124
125         /* CAS latency */
126         printf("CL%d\n", qca_dram_cas_lat());
127
128         /* SPI NOR FLASH sizes and types */
129         printf("%" ALIGN_SIZE "s ", "FLASH:");
130
131         for (bank = 0; bank < CFG_MAX_FLASH_BANKS; bank++) {
132                 if (flash_info[bank].size == 0)
133                         continue;
134
135                 if (bank > 0)
136                         printf("%" ALIGN_SIZE "s ", " ");
137
138                 print_size(flash_info[bank].size, "");
139
140                 if (flash_info[bank].manuf_name != NULL)
141                         printf(" %s", flash_info[bank].manuf_name);
142
143                 if (flash_info[bank].model_name != NULL)
144                         printf(" %s", flash_info[bank].model_name);
145
146                 puts("\n");
147         }
148
149         /* MAC address */
150         printf("%" ALIGN_SIZE "s %02X:%02X:%02X:%02X:%02X:%02X", "MAC:",
151                 bd->bi_enetaddr[0],bd->bi_enetaddr[1], bd->bi_enetaddr[2],
152                 bd->bi_enetaddr[3], bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
153
154         if (mac_is_not_valid) {
155                 puts(" (fixed)\n");
156         } else {
157                 puts("\n");
158         }
159
160         /* System clocks */
161         printf("%" ALIGN_SIZE "s CPU/RAM/AHB/SPI/REF\n", "CLOCKS:");
162
163         qca_sys_clocks(&cpu_clk, &ddr_clk, &ahb_clk, &spi_clk, &ref_clk);
164         cpu_clk = cpu_clk / 1000000;
165         ddr_clk = ddr_clk / 1000000;
166         ahb_clk = ahb_clk / 1000000;
167         spi_clk = spi_clk / 1000000;
168         ref_clk = ref_clk / 1000000;
169
170         printf("%" ALIGN_SIZE "s %3d/%3d/%3d/%3d/%3d MHz\n",
171                 " ", cpu_clk, ddr_clk, ahb_clk, spi_clk, ref_clk);
172
173         puts("\n");
174 }
175
176 /*
177  * Reads MAC address if available or uses fixed one
178  */
179 void macaddr_init(u8 *mac_addr)
180 {
181         u8 buffer[6];
182         u8 fixed_mac[6] = {0x00, 0x03, 0x7F, 0x09, 0x0B, 0xAD};
183
184 #if defined(OFFSET_MAC_ADDRESS)
185         memcpy(buffer, (void *)(CFG_FLASH_BASE
186                 + OFFSET_MAC_DATA_BLOCK + OFFSET_MAC_ADDRESS), 6);
187
188         /*
189          * Check first LSBit (I/G bit) and second LSBit (U/L bit) in MSByte of vendor part
190          * both of them should be 0:
191          * I/G bit == 0 -> Individual MAC address (unicast address)
192          * U/L bit == 0 -> Burned-In-Address (BIA) MAC address
193          */
194         if (CHECK_BIT((buffer[0] & 0xFF), 0) != 0 ||
195                 CHECK_BIT((buffer[0] & 0xFF), 1) != 0) {
196                 memcpy(buffer, fixed_mac, 6);
197         } else {
198                 mac_is_not_valid = 0;
199         }
200 #else
201         memcpy(buffer, fixed_mac, 6);
202 #endif
203
204         memcpy(mac_addr, buffer, 6);
205 }
206
207 /*
208  * Returns "reset button" status:
209  * 1 -> button is pressed
210  * 0 -> button is not pressed
211  */
212 int reset_button_status(void)
213 {
214 #ifdef CONFIG_GPIO_RESET_BTN
215         u32 gpio;
216
217         gpio = qca_soc_reg_read(QCA_GPIO_IN_REG);
218
219         if (gpio & (1 << CONFIG_GPIO_RESET_BTN)) {
220         #if defined(CONFIG_GPIO_RESET_BTN_ACTIVE_LOW)
221                 return 0;
222         #else
223                 return 1;
224         #endif
225         } else {
226         #if defined(CONFIG_GPIO_RESET_BTN_ACTIVE_LOW)
227                 return 1;
228         #else
229                 return 0;
230         #endif
231         }
232 #else
233         return 0;
234 #endif
235 }
236
237 /*
238  * Returns main CPU clock in Hz
239  */
240 u32 main_cpu_clk(void)
241 {
242         u32 cpu_clk;
243
244         qca_sys_clocks(&cpu_clk, NULL, NULL, NULL, NULL);
245
246         return cpu_clk;
247 }
248
249 /*
250  * Calls full chip reset
251  */
252 void full_reset(void)
253 {
254         qca_full_chip_reset();
255 }