xref: /rk3399_rockchip-uboot/board/egnite/ethernut5/ethernut5.c (revision 94db5120d874821e5fd1847e1825759aaf74290b)
114c32614STim Schendekehl /*
214c32614STim Schendekehl  * (C) Copyright 2011
314c32614STim Schendekehl  * egnite GmbH <info@egnite.de>
414c32614STim Schendekehl  *
514c32614STim Schendekehl  * (C) Copyright 2010
614c32614STim Schendekehl  * Ole Reinhardt <ole.reinhardt@thermotemp.de>
714c32614STim Schendekehl  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
914c32614STim Schendekehl  */
1014c32614STim Schendekehl 
1114c32614STim Schendekehl /*
1214c32614STim Schendekehl  * Ethernut 5 general board support
1314c32614STim Schendekehl  *
1414c32614STim Schendekehl  * Ethernut is an open source hardware and software project for
1514c32614STim Schendekehl  * embedded Ethernet devices. Hardware layouts and CAD files are
1614c32614STim Schendekehl  * freely available under BSD-like license.
1714c32614STim Schendekehl  *
1814c32614STim Schendekehl  * Ethernut 5 is the first member of the Ethernut board family
1914c32614STim Schendekehl  * with U-Boot and Linux support. This implementation is based
2014c32614STim Schendekehl  * on the original work done by Ole Reinhardt, but heavily modified
2114c32614STim Schendekehl  * to support additional features and the latest board revision 5.0F.
2214c32614STim Schendekehl  *
2314c32614STim Schendekehl  * Main board components are by default:
2414c32614STim Schendekehl  *
2514c32614STim Schendekehl  * Atmel AT91SAM9XE512 CPU with 512 kBytes NOR Flash
2614c32614STim Schendekehl  * 2 x 64 MBytes Micron MT48LC32M16A2P SDRAM
2714c32614STim Schendekehl  * 512 MBytes Micron MT29F4G08ABADA NAND Flash
2814c32614STim Schendekehl  * 4 MBytes Atmel AT45DB321D DataFlash
2914c32614STim Schendekehl  * SMSC LAN8710 Ethernet PHY
3014c32614STim Schendekehl  * Atmel ATmega168 MCU used for power management
3114c32614STim Schendekehl  * Linear Technology LTC4411 PoE controller
3214c32614STim Schendekehl  *
3314c32614STim Schendekehl  * U-Boot relevant board interfaces are:
3414c32614STim Schendekehl  *
3514c32614STim Schendekehl  * 100 Mbit Ethernet with IEEE 802.3af PoE
3614c32614STim Schendekehl  * RS-232 serial port
3714c32614STim Schendekehl  * USB host and device
3814c32614STim Schendekehl  * MMC/SD-Card slot
3914c32614STim Schendekehl  * Expansion port with I2C, SPI and more...
4014c32614STim Schendekehl  *
4114c32614STim Schendekehl  * Typically the U-Boot image is loaded from serial DataFlash into
4214c32614STim Schendekehl  * SDRAM by the samboot boot loader, which is located in internal
4314c32614STim Schendekehl  * NOR Flash and provides all essential initializations like CPU
4414c32614STim Schendekehl  * and peripheral clocks and, of course, the SDRAM configuration.
4514c32614STim Schendekehl  *
4614c32614STim Schendekehl  * For testing purposes it is also possibly to directly transfer
4714c32614STim Schendekehl  * the image into SDRAM via JTAG. A tested configuration exists
4814c32614STim Schendekehl  * for the Turtelizer 2 hardware dongle and the OpenOCD software.
4914c32614STim Schendekehl  * In this case the latter will do the basic hardware configuration
5014c32614STim Schendekehl  * via its reset-init script.
5114c32614STim Schendekehl  *
5214c32614STim Schendekehl  * For additional information visit the project home page at
5314c32614STim Schendekehl  * http://www.ethernut.de/
5414c32614STim Schendekehl  */
5514c32614STim Schendekehl 
5614c32614STim Schendekehl #include <common.h>
5714c32614STim Schendekehl #include <net.h>
5814c32614STim Schendekehl #include <netdev.h>
5914c32614STim Schendekehl #include <miiphy.h>
6014c32614STim Schendekehl #include <i2c.h>
6114c32614STim Schendekehl #include <mmc.h>
6277c3d844SAndreas Bießmann #include <atmel_mci.h>
6314c32614STim Schendekehl 
6414c32614STim Schendekehl #include <asm/arch/at91sam9260.h>
6514c32614STim Schendekehl #include <asm/arch/at91sam9260_matrix.h>
6614c32614STim Schendekehl #include <asm/arch/at91sam9_smc.h>
6714c32614STim Schendekehl #include <asm/arch/at91_common.h>
68*70341e2eSWenyou Yang #include <asm/arch/clk.h>
6914c32614STim Schendekehl #include <asm/arch/gpio.h>
7014c32614STim Schendekehl #include <asm/io.h>
71ac45bb16SAndreas Bießmann #include <asm/gpio.h>
7214c32614STim Schendekehl 
7314c32614STim Schendekehl #include "ethernut5_pwrman.h"
7414c32614STim Schendekehl 
7514c32614STim Schendekehl DECLARE_GLOBAL_DATA_PTR;
7614c32614STim Schendekehl 
7714c32614STim Schendekehl /*
7814c32614STim Schendekehl  * This is called last during early initialization. Most of the basic
7914c32614STim Schendekehl  * hardware interfaces are up and running.
8014c32614STim Schendekehl  *
8114c32614STim Schendekehl  * The SDRAM hardware has been configured by the first stage boot loader.
8214c32614STim Schendekehl  * We only need to announce its size, using u-boot's memory check.
8314c32614STim Schendekehl  */
dram_init(void)8414c32614STim Schendekehl int dram_init(void)
8514c32614STim Schendekehl {
8614c32614STim Schendekehl 	gd->ram_size = get_ram_size(
8714c32614STim Schendekehl 			(void *)CONFIG_SYS_SDRAM_BASE,
8814c32614STim Schendekehl 			CONFIG_SYS_SDRAM_SIZE);
8914c32614STim Schendekehl 	return 0;
9014c32614STim Schendekehl }
9114c32614STim Schendekehl 
9214c32614STim Schendekehl #ifdef CONFIG_CMD_NAND
ethernut5_nand_hw_init(void)9314c32614STim Schendekehl static void ethernut5_nand_hw_init(void)
9414c32614STim Schendekehl {
9514c32614STim Schendekehl 	struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
9614c32614STim Schendekehl 	struct at91_matrix *matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
9714c32614STim Schendekehl 	unsigned long csa;
9814c32614STim Schendekehl 
9914c32614STim Schendekehl 	/* Assign CS3 to NAND/SmartMedia Interface */
10014c32614STim Schendekehl 	csa = readl(&matrix->ebicsa);
10114c32614STim Schendekehl 	csa |= AT91_MATRIX_CS3A_SMC_SMARTMEDIA;
10214c32614STim Schendekehl 	writel(csa, &matrix->ebicsa);
10314c32614STim Schendekehl 
10414c32614STim Schendekehl 	/* Configure SMC CS3 for NAND/SmartMedia */
10514c32614STim Schendekehl 	writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
10614c32614STim Schendekehl 		AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(0),
10714c32614STim Schendekehl 		&smc->cs[3].setup);
10814c32614STim Schendekehl 	writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(3) |
10914c32614STim Schendekehl 		AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(3),
11014c32614STim Schendekehl 		&smc->cs[3].pulse);
11114c32614STim Schendekehl 	writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
11214c32614STim Schendekehl 		&smc->cs[3].cycle);
11314c32614STim Schendekehl 	writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
11414c32614STim Schendekehl 		AT91_SMC_MODE_EXNW_DISABLE |
11514c32614STim Schendekehl 		AT91_SMC_MODE_DBW_8 |
11614c32614STim Schendekehl 		AT91_SMC_MODE_TDF_CYCLE(2),
11714c32614STim Schendekehl 		&smc->cs[3].mode);
11814c32614STim Schendekehl 
11914c32614STim Schendekehl #ifdef CONFIG_SYS_NAND_READY_PIN
12014c32614STim Schendekehl 	/* Ready pin is optional. */
12114c32614STim Schendekehl 	at91_set_pio_input(CONFIG_SYS_NAND_READY_PIN, 1);
12214c32614STim Schendekehl #endif
123ac45bb16SAndreas Bießmann 	gpio_direction_output(CONFIG_SYS_NAND_ENABLE_PIN, 1);
12414c32614STim Schendekehl }
12514c32614STim Schendekehl #endif
12614c32614STim Schendekehl 
12714c32614STim Schendekehl /*
12814c32614STim Schendekehl  * This is called first during late initialization.
12914c32614STim Schendekehl  */
board_init(void)13014c32614STim Schendekehl int board_init(void)
13114c32614STim Schendekehl {
132*70341e2eSWenyou Yang 	at91_periph_clk_enable(ATMEL_ID_PIOA);
133*70341e2eSWenyou Yang 	at91_periph_clk_enable(ATMEL_ID_PIOB);
134*70341e2eSWenyou Yang 	at91_periph_clk_enable(ATMEL_ID_PIOC);
13514c32614STim Schendekehl 
13614c32614STim Schendekehl 	/* Set adress of boot parameters. */
13714c32614STim Schendekehl 	gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
13814c32614STim Schendekehl 	/* Initialize UARTs and power management. */
13914c32614STim Schendekehl 	ethernut5_power_init();
14014c32614STim Schendekehl #ifdef CONFIG_CMD_NAND
14114c32614STim Schendekehl 	ethernut5_nand_hw_init();
14214c32614STim Schendekehl #endif
14314c32614STim Schendekehl 	return 0;
14414c32614STim Schendekehl }
14514c32614STim Schendekehl 
14614c32614STim Schendekehl #ifdef CONFIG_MACB
14714c32614STim Schendekehl /*
14814c32614STim Schendekehl  * This is optionally called last during late initialization.
14914c32614STim Schendekehl  */
board_eth_init(bd_t * bis)15014c32614STim Schendekehl int board_eth_init(bd_t *bis)
15114c32614STim Schendekehl {
15214c32614STim Schendekehl 	const char *devname;
15314c32614STim Schendekehl 	unsigned short mode;
15414c32614STim Schendekehl 
155*70341e2eSWenyou Yang 	at91_periph_clk_enable(ATMEL_ID_EMAC0);
156*70341e2eSWenyou Yang 
15714c32614STim Schendekehl 	/* Need to reset PHY via power management. */
15814c32614STim Schendekehl 	ethernut5_phy_reset();
15914c32614STim Schendekehl 	/* Set peripheral pins. */
16014c32614STim Schendekehl 	at91_macb_hw_init();
16114c32614STim Schendekehl 	/* Basic EMAC initialization. */
16214c32614STim Schendekehl 	if (macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC0, CONFIG_PHY_ID))
16314c32614STim Schendekehl 		return -1;
16414c32614STim Schendekehl 	/*
16514c32614STim Schendekehl 	 * Early board revisions have a pull-down at the PHY's MODE0
16614c32614STim Schendekehl 	 * strap pin, which forces the PHY into power down. Here we
16714c32614STim Schendekehl 	 * switch to all-capable mode.
16814c32614STim Schendekehl 	 */
16914c32614STim Schendekehl 	devname = miiphy_get_current_dev();
17014c32614STim Schendekehl 	if (miiphy_read(devname, 0, 18, &mode) == 0) {
17114c32614STim Schendekehl 		/* Set mode[2:0] to 0b111. */
17214c32614STim Schendekehl 		mode |= 0x00E0;
17314c32614STim Schendekehl 		miiphy_write(devname, 0, 18, mode);
17414c32614STim Schendekehl 		/* Soft reset overrides strap pins. */
17514c32614STim Schendekehl 		miiphy_write(devname, 0, MII_BMCR, BMCR_RESET);
17614c32614STim Schendekehl 	}
17714c32614STim Schendekehl 	/* Sync environment with network devices, needed for nfsroot. */
178d2eaec60SJoe Hershberger 	return eth_init();
17914c32614STim Schendekehl }
18014c32614STim Schendekehl #endif
18114c32614STim Schendekehl 
18214c32614STim Schendekehl #ifdef CONFIG_GENERIC_ATMEL_MCI
board_mmc_init(bd_t * bd)18314c32614STim Schendekehl int board_mmc_init(bd_t *bd)
18414c32614STim Schendekehl {
185*70341e2eSWenyou Yang 	at91_periph_clk_enable(ATMEL_ID_MCI);
18614c32614STim Schendekehl 
18714c32614STim Schendekehl 	/* Initialize MCI hardware. */
18814c32614STim Schendekehl 	at91_mci_hw_init();
18914c32614STim Schendekehl 	/* Register the device. */
19014c32614STim Schendekehl 	return atmel_mci_init((void *)ATMEL_BASE_MCI);
19114c32614STim Schendekehl }
19214c32614STim Schendekehl 
board_mmc_getcd(struct mmc * mmc)1932271e7c6Sprabhakar.csengg@gmail.com int board_mmc_getcd(struct mmc *mmc)
19414c32614STim Schendekehl {
1952271e7c6Sprabhakar.csengg@gmail.com 	return !at91_get_pio_value(CONFIG_SYS_MMC_CD_PIN);
19614c32614STim Schendekehl }
19714c32614STim Schendekehl #endif
198