blob: 15044c7d0edf5e89796c93969cc86a21b0ffed45 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher44a93442015-06-29 09:10:48 +02002/*
3 * (C) Copyright 2007-2008
4 * Stelian Pop <stelian@popies.net>
5 * Lead Tech Design <www.leadtechdesign.com>
6 *
7 * Achim Ehrlich <aehrlich@taskit.de>
8 * taskit GmbH <www.taskit.de>
9 *
10 * (C) Copyright 2012-
11 * Markus Hubig <mhubig@imko.de>
12 * IMKO GmbH <www.imko.de>
13 * (C) Copyright 2014
14 * Heiko Schocher <hs@denx.de>
15 * DENX Software Engineering GmbH
Heiko Schocher44a93442015-06-29 09:10:48 +020016 */
17
18#include <common.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020019#include <dm.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070020#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <net.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020023#include <asm/io.h>
24#include <asm/arch/at91sam9_sdramc.h>
25#include <asm/arch/at91sam9260_matrix.h>
26#include <asm/arch/at91sam9_smc.h>
27#include <asm/arch/at91_common.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020028#include <asm/arch/atmel_serial.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020029#include <asm/arch/at91_spi.h>
30#include <spi.h>
Heiko Schochercf5137c2015-09-08 11:52:52 +020031#include <asm/arch/clk.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020032#include <asm/arch/gpio.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020033#include <asm/gpio.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020034#include <watchdog.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020035# include <net.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020036#ifndef CONFIG_DM_ETH
Heiko Schocher44a93442015-06-29 09:10:48 +020037# include <netdev.h>
38#endif
Heiko Schocher01f078b2017-06-26 13:26:14 +020039#include <g_dnl.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020040
41DECLARE_GLOBAL_DATA_PTR;
42
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020043static void smartweb_request_gpio(void)
44{
Tom Rinib4213492022-11-12 17:36:51 -050045 gpio_request(CFG_SYS_NAND_ENABLE_PIN, "nand ena");
46 gpio_request(CFG_SYS_NAND_READY_PIN, "nand rdy");
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020047 gpio_request(AT91_PIN_PA26, "ena PHY");
48}
49
Heiko Schocher44a93442015-06-29 09:10:48 +020050static void smartweb_nand_hw_init(void)
51{
52 struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
53 struct at91_matrix *matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
54 unsigned long csa;
55
56 /* Assign CS3 to NAND/SmartMedia Interface */
57 csa = readl(&matrix->ebicsa);
58 csa |= AT91_MATRIX_CS3A_SMC_SMARTMEDIA;
59 writel(csa, &matrix->ebicsa);
60
61 /* Configure SMC CS3 for NAND/SmartMedia */
62 writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
63 AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(0),
64 &smc->cs[3].setup);
65 writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(3) |
66 AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(3),
67 &smc->cs[3].pulse);
68 writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
69 &smc->cs[3].cycle);
70 writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
71 AT91_SMC_MODE_TDF_CYCLE(2),
72 &smc->cs[3].mode);
73
74 /* Configure RDY/BSY */
Tom Rinib4213492022-11-12 17:36:51 -050075 at91_set_gpio_input(CFG_SYS_NAND_READY_PIN, 1);
Heiko Schocher44a93442015-06-29 09:10:48 +020076
77 /* Enable NandFlash */
Tom Rinib4213492022-11-12 17:36:51 -050078 at91_set_gpio_output(CFG_SYS_NAND_ENABLE_PIN, 1);
Heiko Schocher44a93442015-06-29 09:10:48 +020079}
80
Heiko Schocher44a93442015-06-29 09:10:48 +020081static void smartweb_macb_hw_init(void)
82{
83 struct at91_port *pioa = (struct at91_port *)ATMEL_BASE_PIOA;
84
85 /* Enable the PHY Chip via PA26 on the Stamp 2 Adaptor */
86 at91_set_gpio_output(AT91_PIN_PA26, 0);
87
88 /*
89 * Disable pull-up on:
90 * RXDV (PA17) => PHY normal mode (not Test mode)
91 * ERX0 (PA14) => PHY ADDR0
92 * ERX1 (PA15) => PHY ADDR1
93 * ERX2 (PA25) => PHY ADDR2
94 * ERX3 (PA26) => PHY ADDR3
95 * ECRS (PA28) => PHY ADDR4 => PHYADDR = 0x0
96 *
97 * PHY has internal pull-down
98 */
99 writel(pin_to_mask(AT91_PIN_PA14) |
100 pin_to_mask(AT91_PIN_PA15) |
101 pin_to_mask(AT91_PIN_PA17) |
102 pin_to_mask(AT91_PIN_PA25) |
103 pin_to_mask(AT91_PIN_PA26) |
Heiko Schocherb32f3912015-09-28 11:36:05 +0200104 pin_to_mask(AT91_PIN_PA28) |
105 pin_to_mask(AT91_PIN_PA29),
Heiko Schocher44a93442015-06-29 09:10:48 +0200106 &pioa->pudr);
107
108 at91_phy_reset();
109
110 /* Re-enable pull-up */
111 writel(pin_to_mask(AT91_PIN_PA14) |
112 pin_to_mask(AT91_PIN_PA15) |
113 pin_to_mask(AT91_PIN_PA17) |
114 pin_to_mask(AT91_PIN_PA25) |
115 pin_to_mask(AT91_PIN_PA26) |
Heiko Schocherb32f3912015-09-28 11:36:05 +0200116 pin_to_mask(AT91_PIN_PA28) |
117 pin_to_mask(AT91_PIN_PA29),
Heiko Schocher44a93442015-06-29 09:10:48 +0200118 &pioa->puer);
119
120 /* Initialize EMAC=MACB hardware */
121 at91_macb_hw_init();
122}
Heiko Schocher44a93442015-06-29 09:10:48 +0200123
Heiko Schochercf5137c2015-09-08 11:52:52 +0200124#ifdef CONFIG_USB_GADGET_AT91
125#include <linux/usb/at91_udc.h>
126
127void at91_udp_hw_init(void)
128{
Heiko Schochercf5137c2015-09-08 11:52:52 +0200129 /* Enable PLLB */
Wenyou Yangc5c6efe2016-02-03 10:20:45 +0800130 at91_pllb_clk_enable(get_pllb_init());
Heiko Schochercf5137c2015-09-08 11:52:52 +0200131
132 /* Enable UDPCK clock, MCK is enabled in at91_clock_init() */
133 at91_periph_clk_enable(ATMEL_ID_UDP);
134
Wenyou Yang78f89762016-02-03 10:16:50 +0800135 at91_system_clk_enable(AT91SAM926x_PMC_UDP);
Heiko Schochercf5137c2015-09-08 11:52:52 +0200136}
137
138struct at91_udc_data board_udc_data = {
139 .baseaddr = ATMEL_BASE_UDP0,
140};
141#endif
142
Heiko Schocher44a93442015-06-29 09:10:48 +0200143int board_early_init_f(void)
144{
145 /* enable this here, as we have SPL without serial support */
146 at91_seriald_hw_init();
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200147 smartweb_request_gpio();
Heiko Schocher44a93442015-06-29 09:10:48 +0200148 return 0;
149}
150
151int board_init(void)
152{
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200153 smartweb_request_gpio();
Heiko Schocher44a93442015-06-29 09:10:48 +0200154 /* power LED red */
155 at91_set_gpio_output(AT91_PIN_PC6, 0);
156 at91_set_gpio_output(AT91_PIN_PC7, 1);
157 /* alarm LED off */
158 at91_set_gpio_output(AT91_PIN_PC8, 0);
159 at91_set_gpio_output(AT91_PIN_PC9, 0);
160 /* prog LED red */
161 at91_set_gpio_output(AT91_PIN_PC10, 0);
162 at91_set_gpio_output(AT91_PIN_PC11, 1);
163
Heiko Schochercf5137c2015-09-08 11:52:52 +0200164#ifdef CONFIG_USB_GADGET_AT91
165 at91_udp_hw_init();
166 at91_udc_probe(&board_udc_data);
167#endif
168
Heiko Schocherb32f3912015-09-28 11:36:05 +0200169 /* Adress of boot parameters */
Tom Rinibb4dd962022-11-16 13:10:37 -0500170 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Heiko Schocherb32f3912015-09-28 11:36:05 +0200171
172 smartweb_nand_hw_init();
Heiko Schocherb32f3912015-09-28 11:36:05 +0200173 smartweb_macb_hw_init();
Heiko Schocher44a93442015-06-29 09:10:48 +0200174 return 0;
175}
176
177int dram_init(void)
178{
179 gd->ram_size = get_ram_size(
Tom Rinibb4dd962022-11-16 13:10:37 -0500180 (void *)CFG_SYS_SDRAM_BASE,
181 CFG_SYS_SDRAM_SIZE);
Heiko Schocher44a93442015-06-29 09:10:48 +0200182 return 0;
183}
184
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200185#ifndef CONFIG_DM_ETH
Heiko Schocher44a93442015-06-29 09:10:48 +0200186#ifdef CONFIG_MACB
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900187int board_eth_init(struct bd_info *bis)
Heiko Schocher44a93442015-06-29 09:10:48 +0200188{
189 return macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC0, 0x00);
190}
191#endif /* CONFIG_MACB */
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200192#endif
Heiko Schocher44a93442015-06-29 09:10:48 +0200193
194#if defined(CONFIG_SPL_BUILD)
195#include <spl.h>
196#include <nand.h>
197#include <spi_flash.h>
198
199void matrix_init(void)
200{
201 struct at91_matrix *mat = (struct at91_matrix *)ATMEL_BASE_MATRIX;
202
203 writel((readl(&mat->scfg[3]) & (~AT91_MATRIX_SLOT_CYCLE))
204 | AT91_MATRIX_SLOT_CYCLE_(0x40),
205 &mat->scfg[3]);
206}
207
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200208void at91_spl_board_init(void)
Heiko Schocher44a93442015-06-29 09:10:48 +0200209{
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200210 smartweb_request_gpio();
Heiko Schocherb32f3912015-09-28 11:36:05 +0200211 /* power LED orange */
Heiko Schocher44a93442015-06-29 09:10:48 +0200212 at91_set_gpio_output(AT91_PIN_PC6, 1);
213 at91_set_gpio_output(AT91_PIN_PC7, 1);
214 /* alarm LED orange */
215 at91_set_gpio_output(AT91_PIN_PC8, 1);
216 at91_set_gpio_output(AT91_PIN_PC9, 1);
217 /* prog LED red */
218 at91_set_gpio_output(AT91_PIN_PC10, 0);
219 at91_set_gpio_output(AT91_PIN_PC11, 1);
220
221 smartweb_nand_hw_init();
222 at91_set_gpio_input(AT91_PIN_PA28, 1);
223 at91_set_gpio_input(AT91_PIN_PA29, 1);
224
225 /* check if both button are pressed */
226 if (at91_get_gpio_value(AT91_PIN_PA28) == 0 &&
Heiko Schocherb32f3912015-09-28 11:36:05 +0200227 at91_get_gpio_value(AT91_PIN_PA29) == 0) {
228 smartweb_nand_hw_init();
Heiko Schocher44a93442015-06-29 09:10:48 +0200229 nand_init();
230 spl_nand_erase_one(0, 0);
231 }
232}
233
234#define SDRAM_BASE_CONF (AT91_SDRAMC_NC_9 | AT91_SDRAMC_NR_13 \
235 | AT91_SDRAMC_CAS_2 \
236 | AT91_SDRAMC_NB_4 | AT91_SDRAMC_DBW_32 \
237 | AT91_SDRAMC_TWR_VAL(2) | AT91_SDRAMC_TRC_VAL(7) \
238 | AT91_SDRAMC_TRP_VAL(2) | AT91_SDRAMC_TRCD_VAL(2) \
239 | AT91_SDRAMC_TRAS_VAL(5) | AT91_SDRAMC_TXSR_VAL(8))
240
241void mem_init(void)
242{
243 struct at91_matrix *ma = (struct at91_matrix *)ATMEL_BASE_MATRIX;
244 struct at91_port *port = (struct at91_port *)ATMEL_BASE_PIOC;
245 struct sdramc_reg setting;
246
247 setting.cr = SDRAM_BASE_CONF;
248 setting.mdr = AT91_SDRAMC_MD_SDRAM;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500249 setting.tr = (CFG_SYS_MASTER_CLOCK * 7) / 1000000;
Heiko Schocher44a93442015-06-29 09:10:48 +0200250
251 /*
252 * I write here directly in this register, because this
253 * approach is smaller than calling at91_set_a_periph() in a
254 * for loop. This saved me 96 bytes.
255 */
256 writel(0xffff0000, &port->pdr);
257
258 writel(readl(&ma->ebicsa) | AT91_MATRIX_CS1A_SDRAMC, &ma->ebicsa);
259 sdramc_initialize(ATMEL_BASE_CS1, &setting);
260}
261#endif
Heiko Schocher01f078b2017-06-26 13:26:14 +0200262
263int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
264{
265 g_dnl_set_serialnumber("1");
266 return 0;
267}