blob: 63b869921943200cf85b5cd5379c4843878e1115 [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
6#include <common.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 /*
398 * SPI NOR "dtb" partition offset & size hardcoded for now because the
399 * 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
Marek Behún84ee2cd2021-10-09 19:33:40 +0200565int show_board_info(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ún1b010112023-10-20 16:29:16 +0200568 const char *pub_key, *model;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200569 const u8 *topology;
570 u64 serial_number;
571
Marek Behún1b010112023-10-20 16:29:16 +0200572 model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
573 printf("Model: %s\n", model);
Marek Behún35572c92018-12-17 16:10:08 +0100574
575 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
Marek Behún1b010112023-10-20 16:29:16 +0200576 &ram_size, NULL);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200577 if (ret < 0) {
578 printf(" Cannot read board info: %i\n", ret);
579 } else {
580 printf(" Board version: %i\n", board_version);
581 printf(" RAM size: %i MiB\n", ram_size);
582 printf(" Serial Number: %016llX\n", serial_number);
583 }
Marek Behún35572c92018-12-17 16:10:08 +0100584
585 pub_key = mox_sp_get_ecdsa_public_key();
586 if (pub_key)
587 printf(" ECDSA Public Key: %s\n", pub_key);
588 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200589 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100590
Marek Behún1b010112023-10-20 16:29:16 +0200591 if (gd->board_type != BOARD_TURRIS_MOX)
592 return 0;
593
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100594 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200595 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100596 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100597
Marek Behún35572c92018-12-17 16:10:08 +0100598 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
599
600 if (module_count)
601 printf("Module Topology:\n");
602
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100603 for (i = 0; i < module_count; ++i) {
604 switch (topology[i]) {
605 case MOX_MODULE_SFP:
606 printf("% 4i: SFP 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_PCI:
609 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200610 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100611 case MOX_MODULE_TOPAZ:
612 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
613 break;
614 case MOX_MODULE_PERIDOT:
615 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
616 break;
617 case MOX_MODULE_USB3:
618 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
619 break;
620 case MOX_MODULE_PASSPCI:
621 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200622 break;
623 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100624 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200625 }
626 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200627
Marek Behún84ee2cd2021-10-09 19:33:40 +0200628 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100629 for (i = 0; i < module_count; ++i) {
630 switch (topology[i]) {
631 case MOX_MODULE_SFP:
632 if (sfp) {
633 printf("Error: Only one SFP module is supported!\n");
634 } else if (topaz) {
635 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
636 } else {
637 sfp_pos = i;
638 ++sfp;
639 }
640 break;
641 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200642 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100643 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200644 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100645 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200646 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100647 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 +0200648 else
Marek Behún35b35132018-12-17 16:10:03 +0100649 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100650 break;
651 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200652 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100653 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200654 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100655 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200656 else
Marek Behún35b35132018-12-17 16:10:03 +0100657 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100658 break;
659 case MOX_MODULE_PERIDOT:
660 if (sfp || topaz) {
661 printf("Error: Peridot module must come before SFP or Topaz module!\n");
662 } else if (peridot >= 3) {
663 printf("Error: At most three Peridot modules are supported!\n");
664 } else {
665 peridot_pos[peridot] = i;
666 ++peridot;
667 }
668 break;
669 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200670 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100671 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200672 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100673 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200674 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100675 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 +0200676 else
Marek Behún35b35132018-12-17 16:10:03 +0100677 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100678 break;
679 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200680 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100681 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200682 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100683 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200684 else
Marek Behún35b35132018-12-17 16:10:03 +0100685 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100686 }
687 }
688
Marek Behún84ee2cd2021-10-09 19:33:40 +0200689 return 0;
690}
Marek Behún35b35132018-12-17 16:10:03 +0100691
Marek Behún1c8eb902022-04-27 12:41:50 +0200692static struct udevice *mox_mdio_bus(void)
693{
694 struct udevice *bus;
695 ofnode node;
696
697 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
698 if (!ofnode_valid(node))
699 goto err;
700
701 dm_mdio_probe_devices();
702
703 if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus))
704 goto err;
705
706 return bus;
707err:
708 printf("Cannot get MDIO bus device!\n");
709 return NULL;
710}
711
Marek Behún1b010112023-10-20 16:29:16 +0200712enum env_location env_get_location(enum env_operation op, int prio)
713{
714 if (prio > 0)
715 return ENVL_UNKNOWN;
716
717 if (gd->board_type == BOARD_RIPE_ATLAS)
718 return ENVL_MMC;
719
720 return ENVL_SPI_FLASH;
721}
722
Simon Glass1cedca12023-08-21 21:17:01 -0600723static int last_stage_init(void)
Marek Behún84ee2cd2021-10-09 19:33:40 +0200724{
725 struct gpio_desc reset_gpio = {};
726
Marek Behún1b010112023-10-20 16:29:16 +0200727 if (gd->board_type != BOARD_TURRIS_MOX)
728 return 0;
729
Marek Behún84ee2cd2021-10-09 19:33:40 +0200730 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100731 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200732 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100733
734 if (peridot > 0) {
735 if (configure_peridots(&reset_gpio) < 0) {
736 printf("Cannot configure Peridot modules!\n");
737 peridot = 0;
738 }
739 } else {
740 dm_gpio_set_value(&reset_gpio, 1);
741 mdelay(50);
742 dm_gpio_set_value(&reset_gpio, 0);
743 mdelay(50);
744 }
745
Marek Behún84ee2cd2021-10-09 19:33:40 +0200746 /*
747 * check if the addresses are set by reading Scratch & Misc register
748 * 0x70 of Peridot (and potentially Topaz) modules
749 */
Marek Behún35b35132018-12-17 16:10:03 +0100750 if (peridot || topaz) {
Marek Behún1c8eb902022-04-27 12:41:50 +0200751 struct udevice *bus = mox_mdio_bus();
Marek Behún35b35132018-12-17 16:10:03 +0100752
Marek Behún1c8eb902022-04-27 12:41:50 +0200753 if (bus) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200754 int i;
755
Marek Behún35b35132018-12-17 16:10:03 +0100756 for (i = 0; i < peridot; ++i)
757 check_switch_address(bus, 0x10 + i);
758
759 if (topaz)
760 check_switch_address(bus, 0x2);
761
762 sw_blink_leds(bus, peridot, topaz);
763 }
764 }
765
Marek Behún84d67ac2021-10-09 19:33:41 +0200766handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200767 handle_reset_button();
768
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100769 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200770}
Simon Glass1cedca12023-08-21 21:17:01 -0600771EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
Marek Behún4529fce2020-04-08 12:02:05 +0200772
773#if defined(CONFIG_OF_BOARD_SETUP)
774
Marek Behúnd050eab2021-11-26 14:57:11 +0100775static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200776{
Marek Behúnd050eab2021-11-26 14:57:11 +0100777 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200778}
779
Marek Behúnd050eab2021-11-26 14:57:11 +0100780static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200781{
Marek Behúnd050eab2021-11-26 14:57:11 +0100782 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200783}
784
Marek Behúnd050eab2021-11-26 14:57:11 +0100785static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200786{
Marek Behúnd050eab2021-11-26 14:57:11 +0100787 int res, addr, i, node;
788 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200789
Marek Behúnd050eab2021-11-26 14:57:11 +0100790 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200791 if (node < 0)
792 return node;
793
Marek Behúnd050eab2021-11-26 14:57:11 +0100794 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200795 if (res < 0)
796 return res;
797
Marek Behún4529fce2020-04-08 12:02:05 +0200798 addr = switch_addr(id);
799
800 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100801 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
802 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200803 if (res < 0)
804 return res;
805
806 /*
807 * now if there are more switches or a SFP module coming after,
808 * enable corresponding ports
809 */
810 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100811 res = fdt_status_okay_by_pathf(blob,
812 "%s/switch%i@%x/ports/port@a",
813 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200814 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100815 res = fdt_status_okay_by_pathf(blob,
816 "%s/switch%i@%x/ports/port-sfp@a",
817 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200818 } else {
819 res = 0;
820 }
821 if (res < 0)
822 return res;
823
824 if (id >= peridot + topaz - 1)
825 return 0;
826
827 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100828 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
829 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200830 if (node < 0)
831 return node;
832
833 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100834 unsigned int phandle;
835
836 phandle = fdt_create_phandle_by_pathf(blob,
837 "%s/switch%i@%x/ports/port@%x",
838 mdio_path, i,
839 switch_addr(i),
840 is_topaz(i) ? 5 : 9);
841 if (!phandle)
842 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200843
844 if (i == id + 1)
845 res = fdt_setprop_u32(blob, node, "link", phandle);
846 else
847 res = fdt_appendprop_u32(blob, node, "link", phandle);
848 if (res < 0)
849 return res;
850 }
851
852 return 0;
853}
854
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900855int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200856{
Marek Behúnd050eab2021-11-26 14:57:11 +0100857 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200858
Marek Behún1b010112023-10-20 16:29:16 +0200859 if (gd->board_type != BOARD_TURRIS_MOX)
860 return 0;
861
Marek Behún4529fce2020-04-08 12:02:05 +0200862 /*
863 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
864 * connected, enable the PCIe node.
865 */
866 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100867 res = fdt_status_okay_by_compatible(blob,
868 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200869 if (res < 0)
870 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200871
872 /* Fix PCIe regions for devices with 4 GB RAM */
873 res = a3700_fdt_fix_pcie_regions(blob);
874 if (res < 0)
875 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200876 }
877
878 /*
879 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
880 * enable the eth1 node and setup the switches.
881 */
882 if (peridot || topaz) {
883 int i;
884
Marek Behúnd050eab2021-11-26 14:57:11 +0100885 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200886 if (res < 0)
887 return res;
888
889 for (i = 0; i < peridot + topaz; ++i) {
890 res = setup_switch(blob, i);
891 if (res < 0)
892 return res;
893 }
894 }
895
896 /*
897 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
898 * node. If there is no Peridot switch between MOX A and MOX D, add link
899 * to the SFP node to eth1 node.
900 * Also enable and configure SFP GPIO controller node.
901 */
902 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100903 int node;
904
905 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200906 if (res < 0)
907 return res;
908
Marek Behúnd050eab2021-11-26 14:57:11 +0100909 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200910 if (res < 0)
911 return res;
912
913 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100914 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200915
Marek Behúnd050eab2021-11-26 14:57:11 +0100916 phandle = fdt_create_phandle_by_compatible(blob,
917 "sff,sfp");
918 if (!phandle)
919 return -FDT_ERR_NOPHANDLES;
920
921 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200922 if (node < 0)
923 return node;
924
925 res = fdt_setprop_u32(blob, node, "sfp", phandle);
926 if (res < 0)
927 return res;
928
929 res = fdt_setprop_string(blob, node, "phy-mode",
930 "sgmii");
931 if (res < 0)
932 return res;
Pali Rohárdb69aef2022-08-10 12:54:11 +0200933
934 res = fdt_setprop_string(blob, node, "label",
935 "sfp");
936 if (res < 0)
937 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200938 }
939
Marek Behúnd050eab2021-11-26 14:57:11 +0100940 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200941 if (res < 0)
942 return res;
943
944 if (sfp_pos) {
945 char newname[16];
946
947 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100948 node = fdt_node_offset_by_compatible(blob, -1,
949 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200950 if (node < 0)
951 return node;
952
953 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
954 if (res < 0)
955 return res;
956
957 sprintf(newname, "gpio@%x", sfp_pos);
958
959 res = fdt_set_name(blob, node, newname);
960 if (res < 0)
961 return res;
962 }
963 }
964
965 fdt_fixup_ethernet(blob);
966
967 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100968 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200969
970 return 0;
971}
972
973#endif