blob: e4ed7f258109762f570dce8df2b73e636d5e0604 [file] [log] [blame]
Marek Behúnf835bed2018-04-24 17:21:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Marek Behúnd63726e2022-06-01 17:17:06 +02003 * Copyright (C) 2018 Marek Behún <kabel@kernel.org>
Marek Behúnf835bed2018-04-24 17:21:31 +02004 */
5
Tom Rinie8e0b542024-04-30 20:40:55 -06006#include <config.h>
Marek Behún455ef5b2020-04-08 19:25:22 +02007#include <asm/arch/cpu.h>
Marek Behún1055ce12020-04-08 12:02:07 +02008#include <asm/arch/soc.h>
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <net.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020011#include <asm/io.h>
Marek Behún28e935f2020-04-08 12:02:08 +020012#include <asm/gpio.h>
Marek Behún859ada52021-06-07 16:34:47 +020013#include <button.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020014#include <clk.h>
Marek Behún28e935f2020-04-08 12:02:08 +020015#include <dm.h>
Marek Behún1c8eb902022-04-27 12:41:50 +020016#include <dm/of_extra.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060017#include <env.h>
Marek Behún1b010112023-10-20 16:29:16 +020018#include <env_internal.h>
Simon Glass1cedca12023-08-21 21:17:01 -060019#include <event.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020020#include <fdt_support.h>
Marek Behún28e935f2020-04-08 12:02:08 +020021#include <init.h>
Marek Behún859ada52021-06-07 16:34:47 +020022#include <led.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Marek Behún28e935f2020-04-08 12:02:08 +020024#include <linux/libfdt.h>
25#include <linux/string.h>
26#include <miiphy.h>
Marek Behún28e935f2020-04-08 12:02:08 +020027#include <spi.h>
Pali Rohár584256a2022-08-29 15:44:48 +020028#include <spi_flash.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020029
Marek Behún35572c92018-12-17 16:10:08 +010030#include "mox_sp.h"
31
Marek Behún9602a8c2018-08-21 12:22:09 +020032#define MAX_MOX_MODULES 10
33
34#define MOX_MODULE_SFP 0x1
35#define MOX_MODULE_PCI 0x2
36#define MOX_MODULE_TOPAZ 0x3
37#define MOX_MODULE_PERIDOT 0x4
38#define MOX_MODULE_USB3 0x5
39#define MOX_MODULE_PASSPCI 0x6
40
Marek Behún1055ce12020-04-08 12:02:07 +020041#define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
42#define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
43#define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
44#define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
45#define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
Marek Behún9602a8c2018-08-21 12:22:09 +020046
Marek Behúnf835bed2018-04-24 17:21:31 +020047DECLARE_GLOBAL_DATA_PTR;
48
Marek Behún1b010112023-10-20 16:29:16 +020049int board_fit_config_name_match(const char *name)
50{
51 if (!gd->board_type) {
52 enum cznic_a3720_board board;
53
54 if (mbox_sp_get_board_info(NULL, NULL, NULL, NULL, NULL,
55 &board) < 0) {
56 printf("Cannot determine board, defaulting to Turris MOX!\n");
57 board = BOARD_TURRIS_MOX;
58 }
59
60 gd->board_type = board;
61 }
62
63 return !((gd->board_type == BOARD_TURRIS_MOX &&
64 !strcmp(name, "armada-3720-turris-mox")) ||
65 (gd->board_type == BOARD_RIPE_ATLAS &&
66 !strcmp(name, "armada-3720-ripe-atlas")));
67}
68
Marek Behún9602a8c2018-08-21 12:22:09 +020069#if defined(CONFIG_OF_BOARD_FIXUP)
70int board_fix_fdt(void *blob)
71{
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010072 enum fdt_status status_pcie, status_eth1;
Marek Behún9602a8c2018-08-21 12:22:09 +020073 u8 topology[MAX_MOX_MODULES];
Marek Behúnd050eab2021-11-26 14:57:11 +010074 int i, size, ret;
Pali Rohár951b9822022-01-18 10:33:19 +010075 bool eth1_sgmii;
Marek Behún9602a8c2018-08-21 12:22:09 +020076
Marek Behún1b010112023-10-20 16:29:16 +020077 if (gd->board_type != BOARD_TURRIS_MOX)
78 return 0;
79
Marek Behún9602a8c2018-08-21 12:22:09 +020080 /*
81 * SPI driver is not loaded in driver model yet, but we have to find out
82 * if pcie should be enabled in U-Boot's device tree. Therefore we have
83 * to read SPI by reading/writing SPI registers directly
84 */
85
Marek Behúna4e697f2020-04-08 12:02:03 +020086 /* put pin from GPIO to SPI mode */
87 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
Marek Behún77d93122021-11-26 14:57:14 +010088 /* configure cpol, cpha, prescale */
89 writel(0x10df, ARMADA_37XX_SPI_CFG);
90 mdelay(1);
Marek Behúna4e697f2020-04-08 12:02:03 +020091 /* enable SPI CS1 */
92 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020093
94 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
95 udelay(1);
96
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010097 status_pcie = FDT_STATUS_DISABLED;
98 status_eth1 = FDT_STATUS_DISABLED;
Pali Rohár951b9822022-01-18 10:33:19 +010099 eth1_sgmii = false;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100100
Marek Behún9602a8c2018-08-21 12:22:09 +0200101 for (i = 0; i < MAX_MOX_MODULES; ++i) {
102 writel(0x0, ARMADA_37XX_SPI_DOUT);
103
104 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
105 udelay(1);
106
107 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
108 if (topology[i] == 0xff)
109 break;
110
111 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100112
Pali Rohár951b9822022-01-18 10:33:19 +0100113 if (topology[i] == MOX_MODULE_SFP &&
114 status_pcie == FDT_STATUS_DISABLED)
115 eth1_sgmii = true;
116
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100117 if (topology[i] == MOX_MODULE_SFP ||
118 topology[i] == MOX_MODULE_TOPAZ ||
119 topology[i] == MOX_MODULE_PERIDOT)
120 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200121 }
122
123 size = i;
124
Marek Behúna4e697f2020-04-08 12:02:03 +0200125 /* disable SPI CS1 */
126 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +0200127
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100128 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
129 if (ret < 0)
130 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
131 fdt_strerror(ret));
132
Pali Rohár951b9822022-01-18 10:33:19 +0100133 if (eth1_sgmii) {
134 ret = fdt_path_offset(blob, "ethernet1");
135 if (ret >= 0)
136 ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
137 if (ret < 0)
138 printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
139 fdt_strerror(ret));
140 }
141
Marek Behún9602a8c2018-08-21 12:22:09 +0200142 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
143 topology[1] == MOX_MODULE_USB3 ||
144 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100145 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200146
Marek Behúnd050eab2021-11-26 14:57:11 +0100147 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100148 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100149 if (ret < 0) {
150 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
151 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200152 return 0;
153 }
154
Marek Behún455ef5b2020-04-08 19:25:22 +0200155 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
156 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
157 return 0;
158 }
159
Marek Behún9602a8c2018-08-21 12:22:09 +0200160 return 0;
161}
162#endif
163
Marek Behúnf835bed2018-04-24 17:21:31 +0200164int board_init(void)
165{
166 /* address of boot parameters */
Tom Rinibb4dd962022-11-16 13:10:37 -0500167 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Marek Behúnf835bed2018-04-24 17:21:31 +0200168
Marek Behúnf835bed2018-04-24 17:21:31 +0200169 return 0;
170}
171
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100172static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200173{
174 struct spi_slave *slave;
175 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100176 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200177
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200178 ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
179 "spi_generic_drv", "moxtet@1", &dev,
180 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200181 if (ret)
182 goto fail;
183
184 ret = spi_claim_bus(slave);
185 if (ret)
186 goto fail_free;
187
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100188 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
189
190 spi_release_bus(slave);
191fail_free:
192 spi_free_slave(slave);
193fail:
194 return ret;
195}
196
197static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
198{
199 static int is_sd;
200 static u8 topology[MAX_MOX_MODULES - 1];
201 static int size;
202 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
203 int ret, i;
204
205 if (size) {
206 if (ptopology)
207 *ptopology = topology;
208 if (psize)
209 *psize = size;
210 if (pis_sd)
211 *pis_sd = is_sd;
212 return 0;
213 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200214
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100215 memset(din, 0, MAX_MOX_MODULES);
216 memset(dout, 0, MAX_MOX_MODULES);
217
218 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200219 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100220 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200221
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100222 if (din[0] == 0x10)
223 is_sd = 1;
224 else if (din[0] == 0x00)
225 is_sd = 0;
226 else
227 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200228
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100229 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
230 topology[i - 1] = din[i] & 0xf;
231 size = i - 1;
232
233 if (ptopology)
234 *ptopology = topology;
235 if (psize)
236 *psize = size;
237 if (pis_sd)
238 *pis_sd = is_sd;
239
240 return 0;
241}
242
Marek Behún35b35132018-12-17 16:10:03 +0100243#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
244#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
245
Marek Behún1c8eb902022-04-27 12:41:50 +0200246static int sw_multi_read(struct udevice *bus, int sw, int dev, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100247{
Marek Behún1c8eb902022-04-27 12:41:50 +0200248 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_R(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100249 mdelay(5);
Marek Behún1c8eb902022-04-27 12:41:50 +0200250 return dm_mdio_read(bus, sw, MDIO_DEVAD_NONE, 1);
Marek Behún35b35132018-12-17 16:10:03 +0100251}
252
Marek Behún1c8eb902022-04-27 12:41:50 +0200253static void sw_multi_write(struct udevice *bus, int sw, int dev, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100254 u16 val)
255{
Marek Behún1c8eb902022-04-27 12:41:50 +0200256 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 1, val);
257 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_W(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100258 mdelay(5);
259}
260
Marek Behún1c8eb902022-04-27 12:41:50 +0200261static int sw_scratch_read(struct udevice *bus, int sw, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100262{
263 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
264 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
265}
266
Marek Behún1c8eb902022-04-27 12:41:50 +0200267static void sw_led_write(struct udevice *bus, int sw, int port, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100268 u16 val)
269{
270 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
271 | (val & 0x7ff));
272}
273
Marek Behún1c8eb902022-04-27 12:41:50 +0200274static void sw_blink_leds(struct udevice *bus, int peridot, int topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100275{
276 int i, p;
277 struct {
278 int port;
279 u16 val;
280 int wait;
281 } regs[] = {
282 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
283 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
284 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
285 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
286 };
287
288 for (i = 0; i < 12; ++i) {
289 for (p = 0; p < peridot; ++p) {
290 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
291 regs[i].val);
292 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
293 regs[i].val);
294 }
295 if (topaz) {
296 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
297 regs[i].val);
298 }
299
300 if (regs[i].wait)
301 mdelay(75);
302 }
303}
304
Marek Behún1c8eb902022-04-27 12:41:50 +0200305static void check_switch_address(struct udevice *bus, int addr)
Marek Behún35b35132018-12-17 16:10:03 +0100306{
307 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
308 printf("Check of switch MDIO address failed for 0x%02x\n",
309 addr);
310}
311
312static int sfp, pci, topaz, peridot, usb, passpci;
313static int sfp_pos, peridot_pos[3];
314static int module_count;
315
316static int configure_peridots(struct gpio_desc *reset_gpio)
317{
318 int i, ret;
319 u8 dout[MAX_MOX_MODULES];
320
321 memset(dout, 0, MAX_MOX_MODULES);
322
323 /* set addresses of Peridot modules */
324 for (i = 0; i < peridot; ++i)
325 dout[module_count - peridot_pos[i]] = (~i) & 3;
326
327 /*
328 * if there is a SFP module connected to the last Peridot module, set
329 * the P10_SMODE to 1 for the Peridot module
330 */
331 if (sfp)
332 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
333
334 dm_gpio_set_value(reset_gpio, 1);
335 mdelay(10);
336
337 ret = mox_do_spi(NULL, dout, module_count + 1);
338
339 mdelay(10);
340 dm_gpio_set_value(reset_gpio, 0);
341
342 mdelay(50);
343
344 return ret;
345}
346
347static int get_reset_gpio(struct gpio_desc *reset_gpio)
348{
349 int node;
350
351 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
352 if (node < 0) {
353 printf("Cannot find Moxtet bus device node!\n");
354 return -1;
355 }
356
357 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
358 reset_gpio, GPIOD_IS_OUT);
359
360 if (!dm_gpio_is_valid(reset_gpio)) {
361 printf("Cannot find reset GPIO for Moxtet bus!\n");
362 return -1;
363 }
364
Marek Behún35572c92018-12-17 16:10:08 +0100365 return 0;
366}
367
Pali Rohár584256a2022-08-29 15:44:48 +0200368/* Load default system DTB binary to $fdr_addr */
369static void load_spi_dtb(void)
370{
371 const char *const env_name[1] = { "fdt_addr" };
372 unsigned long size, offset;
373 struct udevice *spi_dev;
374 struct spi_flash *flash;
375 const char *addr_str;
376 unsigned long addr;
377 void *buf;
378
379 addr_str = env_get(env_name[0]);
380 if (!addr_str) {
381 env_set_default_vars(1, (char * const *)env_name, 0);
382 addr_str = env_get(env_name[0]);
383 }
384
385 if (!addr_str)
386 return;
387
388 addr = hextoul(addr_str, NULL);
389 if (!addr)
390 return;
391
392 spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS, &spi_dev);
393 flash = dev_get_uclass_priv(spi_dev);
394 if (!flash)
395 return;
396
397 /*
Michal Simekcc046dc2024-04-16 08:55:19 +0200398 * SPI NOR "dtb" partition offset & size hardcoded for now because the
Pali Rohár584256a2022-08-29 15:44:48 +0200399 * mtd subsystem does not offer finding the partition yet and we do not
400 * want to reimplement OF partition parser here.
401 */
402 offset = 0x7f0000;
403 size = 0x10000;
404
405 buf = map_physmem(addr, size, MAP_WRBACK);
406 if (!buf)
407 return;
408
409 spi_flash_read(flash, offset, size, buf);
410 unmap_physmem(buf, size);
411}
412
Marek Behún35572c92018-12-17 16:10:08 +0100413int misc_init_r(void)
414{
Marek Behún1b010112023-10-20 16:29:16 +0200415 int i, ret, addrcnt;
Marek Behún98c86c42021-10-09 19:33:42 +0200416 u8 mac[2][6];
Marek Behún35572c92018-12-17 16:10:08 +0100417
Marek Behún1b010112023-10-20 16:29:16 +0200418 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100419 if (ret < 0) {
420 printf("Cannot read data from OTP!\n");
421 return 0;
422 }
423
Marek Behún1b010112023-10-20 16:29:16 +0200424 if (gd->board_type == BOARD_TURRIS_MOX)
425 addrcnt = 2;
426 else if (gd->board_type == BOARD_RIPE_ATLAS)
427 addrcnt = 1;
428 else
429 addrcnt = 0;
430
431 for (i = 0; i < addrcnt; ++i) {
Marek Behún98c86c42021-10-09 19:33:42 +0200432 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100433
Marek Behún98c86c42021-10-09 19:33:42 +0200434 if (is_valid_ethaddr(mac[i]) &&
435 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
436 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
437 }
Marek Behún35572c92018-12-17 16:10:08 +0100438
Marek Behún1b010112023-10-20 16:29:16 +0200439 if (gd->board_type == BOARD_RIPE_ATLAS) {
440 env_set("board", "ripe_atlas");
441 env_set("board_name", "ripe_atlas");
442 env_set("fdtfile", "marvell/armada-3720-ripe-atlas.dtb");
443 } else {
444 load_spi_dtb();
445 }
Pali Rohár584256a2022-08-29 15:44:48 +0200446
Marek Behún35b35132018-12-17 16:10:03 +0100447 return 0;
448}
449
Marek Behúnd1a30792021-06-07 16:34:48 +0200450static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
451 u16 mask, u16 set)
452{
453 int val;
454
455 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
456 val &= ~mask;
457 val |= set;
458 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
459}
460
461static void mox_phy_leds_start_blinking(void)
462{
463 struct phy_device *phydev;
Marek Behún1c8eb902022-04-27 12:41:50 +0200464 ofnode phy_node;
Pali Rohára524db02022-02-14 11:34:26 +0100465
Marek Behún1c8eb902022-04-27 12:41:50 +0200466 phy_node = ofnode_get_phy_node(ofnode_path("ethernet0"));
467 if (!ofnode_valid(phy_node))
468 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200469
Marek Behún1c8eb902022-04-27 12:41:50 +0200470 phydev = dm_phy_find_by_ofnode(phy_node);
471 if (!phydev)
472 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200473
474 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
475 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
Marek Behún1c8eb902022-04-27 12:41:50 +0200476
477 return;
478err:
479 printf("Cannot get ethernet PHY!\n");
Marek Behúnd1a30792021-06-07 16:34:48 +0200480}
481
Marek Behún859ada52021-06-07 16:34:47 +0200482static bool read_reset_button(void)
483{
484 struct udevice *button, *led;
485 int i;
486
Marek Behúnd050eab2021-11-26 14:57:11 +0100487 if (device_get_global_by_ofnode(
488 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
489 "gpio-keys")),
490 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200491 printf("Cannot find reset button!\n");
492 return false;
493 }
494
Marek Behúnd050eab2021-11-26 14:57:11 +0100495 if (device_get_global_by_ofnode(
496 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
497 "gpio-leds")),
498 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200499 printf("Cannot find status LED!\n");
500 return false;
501 }
502
503 led_set_state(led, LEDST_ON);
504
505 for (i = 0; i < 21; ++i) {
506 if (button_get_state(button) != BUTTON_ON)
507 return false;
508 if (i < 20)
509 mdelay(50);
510 }
511
512 led_set_state(led, LEDST_OFF);
513
514 return true;
515}
516
517static void handle_reset_button(void)
518{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200519 const char * const vars[1] = { "bootcmd_rescue", };
520
521 /*
522 * Ensure that bootcmd_rescue has always stock value, so that running
523 * run bootcmd_rescue
524 * always works correctly.
525 */
526 env_set_default_vars(1, (char * const *)vars, 0);
527
Marek Behún859ada52021-06-07 16:34:47 +0200528 if (read_reset_button()) {
Pali Rohár8adec652022-08-27 20:49:20 +0200529 const char * const vars[3] = {
Marek Behún859ada52021-06-07 16:34:47 +0200530 "bootcmd",
Pali Rohár8adec652022-08-27 20:49:20 +0200531 "bootdelay",
Marek Behún859ada52021-06-07 16:34:47 +0200532 "distro_bootcmd",
533 };
534
535 /*
536 * Set the above envs to their default values, in case the user
537 * managed to break them.
538 */
Pali Rohár8adec652022-08-27 20:49:20 +0200539 env_set_default_vars(3, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200540
541 /* Ensure bootcmd_rescue is used by distroboot */
542 env_set("boot_targets", "rescue");
543
Marek Behúnd1a30792021-06-07 16:34:48 +0200544 /* start blinking PHY LEDs */
545 mox_phy_leds_start_blinking();
546
Marek Behún859ada52021-06-07 16:34:47 +0200547 printf("RESET button was pressed, overwriting boot_targets!\n");
548 } else {
549 /*
550 * In case the user somehow managed to save environment with
551 * boot_targets=rescue, reset boot_targets to default value.
552 * This could happen in subsequent commands if bootcmd_rescue
553 * failed.
554 */
555 if (!strcmp(env_get("boot_targets"), "rescue")) {
556 const char * const vars[1] = {
557 "boot_targets",
558 };
559
560 env_set_default_vars(1, (char * const *)vars, 0);
561 }
562 }
563}
564
Simon Glass629d9b62023-11-12 19:58:23 -0700565int checkboard(void)
Marek Behún35572c92018-12-17 16:10:08 +0100566{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200567 int i, ret, board_version, ram_size, is_sd;
Marek Behúnf84cd222024-04-04 09:50:52 +0200568 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200569 const u8 *topology;
570 u64 serial_number;
571
Marek Behún35572c92018-12-17 16:10:08 +0100572 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
Marek Behún1b010112023-10-20 16:29:16 +0200573 &ram_size, NULL);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200574 if (ret < 0) {
575 printf(" Cannot read board info: %i\n", ret);
576 } else {
577 printf(" Board version: %i\n", board_version);
578 printf(" RAM size: %i MiB\n", ram_size);
579 printf(" Serial Number: %016llX\n", serial_number);
580 }
Marek Behún35572c92018-12-17 16:10:08 +0100581
582 pub_key = mox_sp_get_ecdsa_public_key();
583 if (pub_key)
584 printf(" ECDSA Public Key: %s\n", pub_key);
585 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200586 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100587
Marek Behún1b010112023-10-20 16:29:16 +0200588 if (gd->board_type != BOARD_TURRIS_MOX)
589 return 0;
590
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100591 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200592 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100593 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100594
Marek Behún35572c92018-12-17 16:10:08 +0100595 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
596
597 if (module_count)
598 printf("Module Topology:\n");
599
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100600 for (i = 0; i < module_count; ++i) {
601 switch (topology[i]) {
602 case MOX_MODULE_SFP:
603 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200604 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100605 case MOX_MODULE_PCI:
606 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200607 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100608 case MOX_MODULE_TOPAZ:
609 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
610 break;
611 case MOX_MODULE_PERIDOT:
612 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
613 break;
614 case MOX_MODULE_USB3:
615 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
616 break;
617 case MOX_MODULE_PASSPCI:
618 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200619 break;
620 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100621 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200622 }
623 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200624
Marek Behún84ee2cd2021-10-09 19:33:40 +0200625 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100626 for (i = 0; i < module_count; ++i) {
627 switch (topology[i]) {
628 case MOX_MODULE_SFP:
629 if (sfp) {
630 printf("Error: Only one SFP module is supported!\n");
631 } else if (topaz) {
632 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
633 } else {
634 sfp_pos = i;
635 ++sfp;
636 }
637 break;
638 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200639 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100640 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200641 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100642 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200643 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100644 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200645 else
Marek Behún35b35132018-12-17 16:10:03 +0100646 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100647 break;
648 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200649 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100650 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200651 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100652 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200653 else
Marek Behún35b35132018-12-17 16:10:03 +0100654 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100655 break;
656 case MOX_MODULE_PERIDOT:
657 if (sfp || topaz) {
658 printf("Error: Peridot module must come before SFP or Topaz module!\n");
659 } else if (peridot >= 3) {
660 printf("Error: At most three Peridot modules are supported!\n");
661 } else {
662 peridot_pos[peridot] = i;
663 ++peridot;
664 }
665 break;
666 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200667 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100668 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200669 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100670 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200671 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100672 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200673 else
Marek Behún35b35132018-12-17 16:10:03 +0100674 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100675 break;
676 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200677 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100678 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200679 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100680 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200681 else
Marek Behún35b35132018-12-17 16:10:03 +0100682 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100683 }
684 }
685
Marek Behún84ee2cd2021-10-09 19:33:40 +0200686 return 0;
687}
Marek Behún35b35132018-12-17 16:10:03 +0100688
Marek Behún1c8eb902022-04-27 12:41:50 +0200689static struct udevice *mox_mdio_bus(void)
690{
691 struct udevice *bus;
692 ofnode node;
693
694 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
695 if (!ofnode_valid(node))
696 goto err;
697
698 dm_mdio_probe_devices();
699
700 if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus))
701 goto err;
702
703 return bus;
704err:
705 printf("Cannot get MDIO bus device!\n");
706 return NULL;
707}
708
Marek Behún1b010112023-10-20 16:29:16 +0200709enum env_location env_get_location(enum env_operation op, int prio)
710{
711 if (prio > 0)
712 return ENVL_UNKNOWN;
713
714 if (gd->board_type == BOARD_RIPE_ATLAS)
715 return ENVL_MMC;
716
717 return ENVL_SPI_FLASH;
718}
719
Simon Glass1cedca12023-08-21 21:17:01 -0600720static int last_stage_init(void)
Marek Behún84ee2cd2021-10-09 19:33:40 +0200721{
722 struct gpio_desc reset_gpio = {};
723
Marek Behún1b010112023-10-20 16:29:16 +0200724 if (gd->board_type != BOARD_TURRIS_MOX)
725 return 0;
726
Marek Behún84ee2cd2021-10-09 19:33:40 +0200727 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100728 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200729 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100730
731 if (peridot > 0) {
732 if (configure_peridots(&reset_gpio) < 0) {
733 printf("Cannot configure Peridot modules!\n");
734 peridot = 0;
735 }
736 } else {
737 dm_gpio_set_value(&reset_gpio, 1);
738 mdelay(50);
739 dm_gpio_set_value(&reset_gpio, 0);
740 mdelay(50);
741 }
742
Marek Behún84ee2cd2021-10-09 19:33:40 +0200743 /*
744 * check if the addresses are set by reading Scratch & Misc register
745 * 0x70 of Peridot (and potentially Topaz) modules
746 */
Marek Behún35b35132018-12-17 16:10:03 +0100747 if (peridot || topaz) {
Marek Behún1c8eb902022-04-27 12:41:50 +0200748 struct udevice *bus = mox_mdio_bus();
Marek Behún35b35132018-12-17 16:10:03 +0100749
Marek Behún1c8eb902022-04-27 12:41:50 +0200750 if (bus) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200751 int i;
752
Marek Behún35b35132018-12-17 16:10:03 +0100753 for (i = 0; i < peridot; ++i)
754 check_switch_address(bus, 0x10 + i);
755
756 if (topaz)
757 check_switch_address(bus, 0x2);
758
759 sw_blink_leds(bus, peridot, topaz);
760 }
761 }
762
Marek Behún84d67ac2021-10-09 19:33:41 +0200763handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200764 handle_reset_button();
765
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100766 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200767}
Simon Glass1cedca12023-08-21 21:17:01 -0600768EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
Marek Behún4529fce2020-04-08 12:02:05 +0200769
770#if defined(CONFIG_OF_BOARD_SETUP)
771
Marek Behúnd050eab2021-11-26 14:57:11 +0100772static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200773{
Marek Behúnd050eab2021-11-26 14:57:11 +0100774 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200775}
776
Marek Behúnd050eab2021-11-26 14:57:11 +0100777static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200778{
Marek Behúnd050eab2021-11-26 14:57:11 +0100779 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200780}
781
Marek Behúnd050eab2021-11-26 14:57:11 +0100782static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200783{
Marek Behúnd050eab2021-11-26 14:57:11 +0100784 int res, addr, i, node;
785 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200786
Marek Behúnd050eab2021-11-26 14:57:11 +0100787 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200788 if (node < 0)
789 return node;
790
Marek Behúnd050eab2021-11-26 14:57:11 +0100791 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200792 if (res < 0)
793 return res;
794
Marek Behún4529fce2020-04-08 12:02:05 +0200795 addr = switch_addr(id);
796
797 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100798 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
799 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200800 if (res < 0)
801 return res;
802
803 /*
804 * now if there are more switches or a SFP module coming after,
805 * enable corresponding ports
806 */
807 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100808 res = fdt_status_okay_by_pathf(blob,
809 "%s/switch%i@%x/ports/port@a",
810 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200811 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100812 res = fdt_status_okay_by_pathf(blob,
813 "%s/switch%i@%x/ports/port-sfp@a",
814 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200815 } else {
816 res = 0;
817 }
818 if (res < 0)
819 return res;
820
821 if (id >= peridot + topaz - 1)
822 return 0;
823
824 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100825 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
826 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200827 if (node < 0)
828 return node;
829
830 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100831 unsigned int phandle;
832
833 phandle = fdt_create_phandle_by_pathf(blob,
834 "%s/switch%i@%x/ports/port@%x",
835 mdio_path, i,
836 switch_addr(i),
837 is_topaz(i) ? 5 : 9);
838 if (!phandle)
839 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200840
841 if (i == id + 1)
842 res = fdt_setprop_u32(blob, node, "link", phandle);
843 else
844 res = fdt_appendprop_u32(blob, node, "link", phandle);
845 if (res < 0)
846 return res;
847 }
848
849 return 0;
850}
851
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900852int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200853{
Marek Behúnd050eab2021-11-26 14:57:11 +0100854 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200855
Marek Behún1b010112023-10-20 16:29:16 +0200856 if (gd->board_type != BOARD_TURRIS_MOX)
857 return 0;
858
Marek Behún4529fce2020-04-08 12:02:05 +0200859 /*
860 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
861 * connected, enable the PCIe node.
862 */
863 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100864 res = fdt_status_okay_by_compatible(blob,
865 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200866 if (res < 0)
867 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200868
869 /* Fix PCIe regions for devices with 4 GB RAM */
870 res = a3700_fdt_fix_pcie_regions(blob);
871 if (res < 0)
872 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200873 }
874
875 /*
876 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
877 * enable the eth1 node and setup the switches.
878 */
879 if (peridot || topaz) {
880 int i;
881
Marek Behúnd050eab2021-11-26 14:57:11 +0100882 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200883 if (res < 0)
884 return res;
885
886 for (i = 0; i < peridot + topaz; ++i) {
887 res = setup_switch(blob, i);
888 if (res < 0)
889 return res;
890 }
891 }
892
893 /*
894 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
895 * node. If there is no Peridot switch between MOX A and MOX D, add link
896 * to the SFP node to eth1 node.
897 * Also enable and configure SFP GPIO controller node.
898 */
899 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100900 int node;
901
902 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200903 if (res < 0)
904 return res;
905
Marek Behúnd050eab2021-11-26 14:57:11 +0100906 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200907 if (res < 0)
908 return res;
909
910 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100911 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200912
Marek Behúnd050eab2021-11-26 14:57:11 +0100913 phandle = fdt_create_phandle_by_compatible(blob,
914 "sff,sfp");
915 if (!phandle)
916 return -FDT_ERR_NOPHANDLES;
917
918 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200919 if (node < 0)
920 return node;
921
922 res = fdt_setprop_u32(blob, node, "sfp", phandle);
923 if (res < 0)
924 return res;
925
926 res = fdt_setprop_string(blob, node, "phy-mode",
927 "sgmii");
928 if (res < 0)
929 return res;
Pali Rohárdb69aef2022-08-10 12:54:11 +0200930
931 res = fdt_setprop_string(blob, node, "label",
932 "sfp");
933 if (res < 0)
934 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200935 }
936
Marek Behúnd050eab2021-11-26 14:57:11 +0100937 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200938 if (res < 0)
939 return res;
940
941 if (sfp_pos) {
942 char newname[16];
943
944 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100945 node = fdt_node_offset_by_compatible(blob, -1,
946 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200947 if (node < 0)
948 return node;
949
950 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
951 if (res < 0)
952 return res;
953
954 sprintf(newname, "gpio@%x", sfp_pos);
955
956 res = fdt_set_name(blob, node, newname);
957 if (res < 0)
958 return res;
959 }
960 }
961
962 fdt_fixup_ethernet(blob);
963
964 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100965 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200966
967 return 0;
968}
969
970#endif