blob: 98166b056f7d1f0f267f369e54663b162b9d74a2 [file] [log] [blame]
David Hendricks8cbd5692017-12-01 20:49:48 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2018 Facebook, Inc.
5 * Copyright 2003-2017 Cavium Inc. <support@cavium.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * Derived from Cavium's BSD-3 Clause OCTEONTX-SDK-6.2.0.
17 */
18
19#include <bootmode.h>
20#include <console/console.h>
David Hendricks8cbd5692017-12-01 20:49:48 -080021#include <device/device.h>
22#include <soc/addressmap.h>
23#include <soc/clock.h>
24#include <soc/sdram.h>
25#include <soc/timer.h>
Jens Drenhausfe66a072018-10-09 13:02:49 +020026#include <soc/uart.h>
David Hendricks8cbd5692017-12-01 20:49:48 -080027#include <stddef.h>
28#include <stdlib.h>
29#include <string.h>
30#include <symbols.h>
31#include <libbdk-boot/bdk-boot.h>
Patrick Rudolphd0c67972018-04-17 13:47:55 +020032#include <soc/ecam0.h>
Patrick Rudolphde4410c2018-03-27 12:01:40 +020033#include <console/uart.h>
34#include <libbdk-hal/bdk-pcie.h>
Patrick Rudolphde4410c2018-03-27 12:01:40 +020035#include <device/pci.h>
36#include <libbdk-hal/bdk-qlm.h>
37#include <libbdk-hal/bdk-config.h>
38#include <libbdk-arch/bdk-csrs-bgx.h>
Patrick Rudolph5cdaa332018-04-20 14:43:21 +020039#include <bootmem.h>
40#include <soc/bl31_plat_params.h>
41#include <cbfs.h>
42#include <cbmem.h>
Patrick Rudolphde4410c2018-03-27 12:01:40 +020043#include <fit.h>
44
45static const char *QLM_BGX_MODE_MAP[BDK_QLM_MODE_LAST] = {
46 [BDK_QLM_MODE_SGMII_4X1] = "sgmii",
47 [BDK_QLM_MODE_SGMII_2X1] = "sgmii",
48 [BDK_QLM_MODE_SGMII_1X1] = "sgmii",
49 [BDK_QLM_MODE_XAUI_1X4] = "xaui",
50 [BDK_QLM_MODE_RXAUI_2X2] = "rxaui",
51 [BDK_QLM_MODE_RXAUI_1X2] = "rxaui",
52 [BDK_QLM_MODE_XFI_4X1] = "xfi",
53 [BDK_QLM_MODE_XFI_2X1] = "xfi",
54 [BDK_QLM_MODE_XFI_1X1] = "xfi",
55 [BDK_QLM_MODE_XLAUI_1X4] = "xlaui",
56 [BDK_QLM_MODE_10G_KR_4X1] = "xfi-10g-kr",
57 [BDK_QLM_MODE_10G_KR_2X1] = "xfi-10g-kr",
58 [BDK_QLM_MODE_10G_KR_1X1] = "xfi-10g-kr",
59 [BDK_QLM_MODE_40G_KR4_1X4] = "xlaui-40g-kr",
60 [BDK_QLM_MODE_QSGMII_4X1] = "qsgmii",
61};
62
63static void dt_platform_fixup_phy(struct device_tree_node *node, char *path,
64 int64_t phy_address, bdk_qlm_modes_t qlm_mode)
65{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020066 const char *data = NULL;
Patrick Rudolphde4410c2018-03-27 12:01:40 +020067 size_t size = 0;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020068 dt_find_bin_prop(node, "qlm-mode", (const void **)&data, &size);
Patrick Rudolphde4410c2018-03-27 12:01:40 +020069
70 if (!data || strncmp(data, path, 6) != 0)
71 return; /* No key prefix match. */
72 printk(BIOS_INFO, "%s: Node %s = %s\n", __func__, node->name, data);
73
74 if (strlen(path) == strlen(data) && strcmp(data, path) == 0) {
75 /* Keep node, remove "qlm-mode" property */
76 dt_delete_prop(node, "qlm-mode");
77 printk(BIOS_INFO, "%s: Removing qlm-mode on "
78 "node %s\n", __func__, node->name);
79 /* Linux only access the Phy via MDIO.
80 Remove 'phy-handle' if this option is not available */
81 switch (qlm_mode) {
82 case BDK_QLM_MODE_SGMII_4X1:
83 case BDK_QLM_MODE_SGMII_2X1:
84 case BDK_QLM_MODE_SGMII_1X1:
85 case BDK_QLM_MODE_QSGMII_4X1:
86 if ((phy_address & BDK_IF_PHY_TYPE_MASK) !=
87 BDK_IF_PHY_MDIO) {
88 dt_delete_prop(node, "phy-handle");
89 printk(BIOS_INFO, "%s: Removing phy-handle on "
90 "node %s\n", __func__, node->name);
91 }
92 break;
93 default:
94 break;
95 }
96 } else {
97 printk(BIOS_INFO, "%s: Removing node %s\n", __func__,
98 node->name);
99 /* No match, remove node */
100 list_remove(&node->list_node);
101 }
102}
103
104static void dt_iterate_phy(struct device_tree_node *parent,
105 const char *name,
106 char *path,
107 int64_t phy_address,
108 bdk_qlm_modes_t qlm_mode)
109{
110 struct device_tree_property *prop;
111
112 /* Check if parent itself has the required property value. */
113 list_for_each(prop, parent->properties, list_node) {
114 if (!strcmp(name, prop->prop.name)) {
115 dt_platform_fixup_phy(parent, path, phy_address,
116 qlm_mode);
117 }
118 }
119
120 struct device_tree_node *child;
121 list_for_each(child, parent->children, list_node) {
122 dt_iterate_phy(child, name, path, phy_address, qlm_mode);
123 }
124}
125
126static void dt_platform_fixup_mac(struct device_tree_node *node)
127{
128 const char *name = "local-mac-address";
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200129 const u64 *localmac = NULL;
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200130 size_t size = 0;
131
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200132 dt_find_bin_prop(node, name, (const void **)&localmac, &size);
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200133
134 if (!localmac)
135 return;
136 static size_t used_mac;
137
138 /* Extract our MAC address info so we can assign them */
139 size_t next_free_mac_address =
140 bdk_config_get_int(BDK_CONFIG_MAC_ADDRESS);
141 size_t num_free_mac_addresses =
142 bdk_config_get_int(BDK_CONFIG_MAC_ADDRESS_NUM);
143 size_t num_free_override =
144 bdk_config_get_int(BDK_CONFIG_MAC_ADDRESS_NUM_OVERRIDE);
145 if (num_free_override != -1)
146 num_free_mac_addresses = num_free_override;
147
148 if (size == 6) {
149 if (*localmac)
150 return;
151 if (used_mac < num_free_mac_addresses) {
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200152 const u64 genmac = next_free_mac_address + used_mac;
153 dt_add_bin_prop(node, name, &genmac, 6);
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200154 used_mac++;
155 return;
156 }
157 }
158
159 printk(BIOS_INFO, "%s: Removing node %s\n", __func__, node->name);
160 list_remove(&node->list_node);
161}
162
163static void dt_iterate_mac(struct device_tree_node *parent)
164{
165 struct device_tree_property *prop;
166 const char *name = "local-mac-address";
167
168 /* Check if parent itself has the required property value. */
169 list_for_each(prop, parent->properties, list_node) {
170 if (!strcmp(name, prop->prop.name))
171 dt_platform_fixup_mac(parent);
172 }
173
174 struct device_tree_node *child;
175 list_for_each(child, parent->children, list_node) {
176 dt_iterate_mac(child);
177 }
178}
179
180/* Do additional device_tree modifications. */
181static int dt_platform_fixup(struct device_tree_fixup *fixup,
182 struct device_tree *tree)
183{
184 struct device_tree_node *dt_node;
185 size_t i;
186
187 /* Set the sclk clock rate. */
188 dt_node = dt_find_node_by_path(tree->root, "soc@0/sclk", NULL, NULL, 0);
189 if (dt_node) {
190 const u32 freq = thunderx_get_io_clock();
191 printk(BIOS_INFO, "%s: Set SCLK to %u Hz\n", __func__, freq);
192 dt_add_u32_prop(dt_node, "clock-frequency", freq);
193 } else
194 printk(BIOS_ERR, "%s: Node not found. OS might miss-behave !\n",
195 __func__);
196
197 /* Set refclkuaa clock rate. */
198 dt_node = dt_find_node_by_path(tree->root, "soc@0/refclkuaa", NULL,
199 NULL, 0);
200 if (dt_node) {
201 const u32 freq = uart_platform_refclk();
202 printk(BIOS_INFO, "%s: Set REFCLKUAA to %u Hz\n", __func__,
203 freq);
204 dt_add_u32_prop(dt_node, "clock-frequency", freq);
205 } else
206 printk(BIOS_ERR, "%s: Node not found. OS might miss-behave !\n",
207 __func__);
208
Jens Drenhausfe66a072018-10-09 13:02:49 +0200209 /* Remove unused UART entries */
210 for (i = 0; i < 4; i++) {
211 char path[32];
212 const uint64_t addr = UAAx_PF_BAR0(i);
213 /* Remove the node */
214 snprintf(path, sizeof(path), "soc@0/serial@%llx", addr);
215 dt_node = dt_find_node_by_path(tree->root, path, NULL, NULL, 0);
216 if (!dt_node || uart_is_enabled(i)) {
217 printk(BIOS_INFO, "%s: ignoring %s\n", __func__, path);
218 continue;
219 }
220 printk(BIOS_INFO, "%s: Removing node %s\n", __func__, path);
221 list_remove(&dt_node->list_node);
222 }
223
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200224 /* Remove unused PEM entries */
225 for (i = 0; i < 8; i++) {
226 char path[32];
227 u32 phandle = 0;
228 const uint64_t addr = PEM_PEMX_PF_BAR0(i);
229 /* Remove the node */
230 snprintf(path, sizeof(path), "soc@0/pci@%llx", addr);
231 dt_node = dt_find_node_by_path(tree->root, path, NULL, NULL, 0);
232 if (!dt_node || bdk_pcie_is_running(0, i)) {
233 printk(BIOS_INFO, "%s: ignoring %s\n", __func__, path);
234 continue;
235 }
236 /* Store the phandle */
237 phandle = dt_get_phandle(dt_node);
238 printk(BIOS_INFO, "%s: Removing node %s\n", __func__, path);
239 list_remove(&dt_node->list_node);
240
241 /* Remove phandle to non existing nodes */
242 snprintf(path, sizeof(path), "soc@0/smmu0@%llx", SMMU_PF_BAR0);
243 dt_node = dt_find_node_by_path(tree->root, path, NULL, NULL, 0);
244 if (!dt_node) {
245 printk(BIOS_ERR, "%s: SMMU entry not found\n",
246 __func__);
247 continue;
248 }
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200249 const u32 *data = NULL;
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200250 size_t size = 0;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200251 dt_find_bin_prop(dt_node, "mmu-masters", (const void **)&data,
252 &size);
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200253 if (!size) {
254 printk(BIOS_ERR, "%s: mmu-masters entry not found\n",
255 __func__);
256 continue;
257 }
Patrick Rudolph70866e92018-08-22 09:52:42 +0200258
259 u32 *data_cleaned = malloc(size);
260 if (!data_cleaned)
261 continue;
262
263 size_t n = 0;
264 /* Remove phandle from mmu-masters list */
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200265 for (size_t j = 0; j < size / (sizeof(u32) * 2); j++)
Patrick Rudolph70866e92018-08-22 09:52:42 +0200266 if (be32_to_cpu(data[j * 2]) != phandle) {
267 data_cleaned[n * 2] = data[j * 2];
268 data_cleaned[n * 2 + 1] = data[j * 2 + 1];
269 n++;
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200270 }
Patrick Rudolph70866e92018-08-22 09:52:42 +0200271
272 dt_add_bin_prop(dt_node, "mmu-masters", data_cleaned,
273 n * sizeof(u32) * 2);
274
275 free(data_cleaned);
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200276 }
277
278 /* Remove QLM mode entries */
279 size_t bgx_index, bgx_iface;
280 for (bgx_iface = 0; bgx_iface < 4; bgx_iface++) {
281 for (bgx_index = 0; bgx_index < 4; bgx_index++) {
282 char path[32];
283 int qlm = bdk_qlm_get_qlm_num(0, BDK_IF_BGX,
284 bgx_iface, bgx_index);
285 bdk_qlm_modes_t qlm_mode = (qlm == -1) ?
286 BDK_QLM_MODE_DISABLED :
287 bdk_qlm_get_mode(0, qlm);
288
289 /* BGXX_CMRX_RX_DMAC_CTL is used to mark ports as
290 * disabled that would otherwise be enabled */
291 if (qlm_mode != BDK_QLM_MODE_DISABLED) {
292 BDK_CSR_INIT(rx_dmac_ctl, 0,
293 BDK_BGXX_CMRX_RX_DMAC_CTL(bgx_iface,
294 bgx_index));
295 if (rx_dmac_ctl.u == 0)
296 qlm_mode = BDK_QLM_MODE_DISABLED;
297 }
298
299 if (qlm_mode == BDK_QLM_MODE_DISABLED)
300 snprintf(path, sizeof(path), "0x0%x%x,disabled",
301 bgx_iface, bgx_index);
302 else
303 snprintf(path, sizeof(path), "0x0%x%x,%s",
304 bgx_iface, bgx_index,
305 QLM_BGX_MODE_MAP[qlm_mode]);
306
307 int64_t phy_address =
308 bdk_config_get_int(BDK_CONFIG_PHY_ADDRESS, 0,
309 bgx_iface, bgx_index);
310
311 dt_iterate_phy(tree->root, "qlm-mode", path,
312 phy_address, qlm_mode);
313 }
314 }
315
316 /* Set local MAC address */
317 dt_iterate_mac(tree->root);
318
319 return 0;
320}
David Hendricks8cbd5692017-12-01 20:49:48 -0800321
Patrick Rudolph5cdaa332018-04-20 14:43:21 +0200322extern u8 _sff8104[];
323extern u8 _esff8104[];
324
325void bootmem_platform_add_ranges(void)
326{
Patrick Rudolph5cdaa332018-04-20 14:43:21 +0200327 bootmem_add_range((uintptr_t)_sff8104,
328 ((uintptr_t)_esff8104 - (uintptr_t)_sff8104),
329 BM_MEM_RESERVED);
330
331 /* Scratchpad for ATF SATA quirks */
Patrick Rudolph52acef12018-08-08 12:46:18 +0200332 bootmem_add_range((sdram_size_mb() - 1) * MiB, 1 * MiB,
333 BM_MEM_RESERVED);
Patrick Rudolph5cdaa332018-04-20 14:43:21 +0200334}
335
Elyes HAOUAS27667a22018-09-16 17:40:40 +0200336static void soc_read_resources(struct device *dev)
David Hendricks8cbd5692017-12-01 20:49:48 -0800337{
Patrick Rudolpheead8792018-08-10 13:53:04 +0200338 // HACK: Don't advertise bootblock romstage CAR region, it's broken...
339 ram_resource(dev, 0, 2 * KiB, sdram_size_mb() * KiB - 2 * KiB);
David Hendricks8cbd5692017-12-01 20:49:48 -0800340}
341
Patrick Rudolph5cdaa332018-04-20 14:43:21 +0200342static void soc_init_atf(void)
343{
344 static struct bl31_fdt_param fdt_param = {
345 .h = { .type = PARAM_FDT, },
346 };
347
348 size_t size = 0;
349
350 void *ptr = cbfs_boot_map_with_leak("sff8104-linux.dtb",
351 CBFS_TYPE_RAW, &size);
352 if (ptr)
353 memcpy(_sff8104, ptr, size);
354 /* Point to devicetree in secure memory */
355 fdt_param.fdt_ptr = (uintptr_t)_sff8104;
356
357 register_bl31_param(&fdt_param.h);
358
359 static struct bl31_u64_param cbtable_param = {
360 .h = { .type = PARAM_COREBOOT_TABLE, },
361 };
362 /* Point to coreboot tables */
363 cbtable_param.value = (uint64_t)cbmem_find(CBMEM_ID_CBTABLE);
364 if (cbtable_param.value)
365 register_bl31_param(&cbtable_param.h);
366}
367
Elyes HAOUAS27667a22018-09-16 17:40:40 +0200368static void soc_init(struct device *dev)
David Hendricks8cbd5692017-12-01 20:49:48 -0800369{
370 /* Init ECAM, MDIO, PEM, PHY, QLM ... */
371 bdk_boot();
372
Julius Wernercd49cce2019-03-05 16:53:33 -0800373 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Patrick Rudolphde4410c2018-03-27 12:01:40 +0200374 struct device_tree_fixup *dt_fixup;
375
376 dt_fixup = malloc(sizeof(*dt_fixup));
377 if (dt_fixup) {
378 dt_fixup->fixup = dt_platform_fixup;
379 list_insert_after(&dt_fixup->list_node,
380 &device_tree_fixups);
381 }
382 }
Patrick Rudolph5cdaa332018-04-20 14:43:21 +0200383
Julius Wernercd49cce2019-03-05 16:53:33 -0800384 if (CONFIG(ARM64_USE_ARM_TRUSTED_FIRMWARE))
Patrick Rudolph5cdaa332018-04-20 14:43:21 +0200385 soc_init_atf();
David Hendricks8cbd5692017-12-01 20:49:48 -0800386}
387
Elyes HAOUAS27667a22018-09-16 17:40:40 +0200388static void soc_final(struct device *dev)
David Hendricks8cbd5692017-12-01 20:49:48 -0800389{
390 watchdog_disable(0);
391}
392
393static struct device_operations soc_ops = {
Patrick Rudolph88f81af2018-04-11 11:40:55 +0200394 .read_resources = soc_read_resources,
395 .set_resources = DEVICE_NOOP,
396 .enable_resources = DEVICE_NOOP,
397 .init = soc_init,
398 .final = soc_final,
399 .scan_bus = NULL,
David Hendricks8cbd5692017-12-01 20:49:48 -0800400};
401
Elyes HAOUAS27667a22018-09-16 17:40:40 +0200402static void enable_soc_dev(struct device *dev)
David Hendricks8cbd5692017-12-01 20:49:48 -0800403{
Patrick Rudolphd0c67972018-04-17 13:47:55 +0200404 if (dev->path.type == DEVICE_PATH_DOMAIN &&
405 dev->path.domain.domain == 0) {
406 dev->ops = &pci_domain_ops_ecam0;
407 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
408 dev->ops = &soc_ops;
409 }
David Hendricks8cbd5692017-12-01 20:49:48 -0800410}
411
412struct chip_operations soc_cavium_cn81xx_ops = {
413 CHIP_NAME("SOC Cavium CN81XX")
414 .enable_dev = enable_soc_dev,
415};