blob: 40c9c7e21daea6f7ee0d4302a8990d9aef3f7d13 [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>
Heiko Schocher44a93442015-06-29 09:10:48 +020022#include <asm/io.h>
23#include <asm/arch/at91sam9_sdramc.h>
24#include <asm/arch/at91sam9260_matrix.h>
25#include <asm/arch/at91sam9_smc.h>
26#include <asm/arch/at91_common.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020027#include <asm/arch/atmel_serial.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020028#include <asm/arch/at91_spi.h>
29#include <spi.h>
Heiko Schochercf5137c2015-09-08 11:52:52 +020030#include <asm/arch/clk.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020031#include <asm/arch/gpio.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020032#include <asm/gpio.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020033#include <watchdog.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020034# include <net.h>
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020035#ifndef CONFIG_DM_ETH
Heiko Schocher44a93442015-06-29 09:10:48 +020036# include <netdev.h>
37#endif
Heiko Schocher01f078b2017-06-26 13:26:14 +020038#include <g_dnl.h>
Heiko Schocher44a93442015-06-29 09:10:48 +020039
40DECLARE_GLOBAL_DATA_PTR;
41
Heiko Schocherd1b8ea82016-05-25 07:23:47 +020042static void smartweb_request_gpio(void)
43{
44 gpio_request(CONFIG_SYS_NAND_ENABLE_PIN, "nand ena");
45 gpio_request(CONFIG_SYS_NAND_READY_PIN, "nand rdy");
46 gpio_request(AT91_PIN_PA26, "ena PHY");
47}
48
Heiko Schocher44a93442015-06-29 09:10:48 +020049static void smartweb_nand_hw_init(void)
50{
51 struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
52 struct at91_matrix *matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
53 unsigned long csa;
54
55 /* Assign CS3 to NAND/SmartMedia Interface */
56 csa = readl(&matrix->ebicsa);
57 csa |= AT91_MATRIX_CS3A_SMC_SMARTMEDIA;
58 writel(csa, &matrix->ebicsa);
59
60 /* Configure SMC CS3 for NAND/SmartMedia */
61 writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
62 AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(0),
63 &smc->cs[3].setup);
64 writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(3) |
65 AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(3),
66 &smc->cs[3].pulse);
67 writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
68 &smc->cs[3].cycle);
69 writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
70 AT91_SMC_MODE_TDF_CYCLE(2),
71 &smc->cs[3].mode);
72
73 /* Configure RDY/BSY */
74 at91_set_gpio_input(CONFIG_SYS_NAND_READY_PIN, 1);
75
76 /* Enable NandFlash */
77 at91_set_gpio_output(CONFIG_SYS_NAND_ENABLE_PIN, 1);
78}
79
Heiko Schocher44a93442015-06-29 09:10:48 +020080static void smartweb_macb_hw_init(void)
81{
82 struct at91_port *pioa = (struct at91_port *)ATMEL_BASE_PIOA;
83
84 /* Enable the PHY Chip via PA26 on the Stamp 2 Adaptor */
85 at91_set_gpio_output(AT91_PIN_PA26, 0);
86
87 /*
88 * Disable pull-up on:
89 * RXDV (PA17) => PHY normal mode (not Test mode)
90 * ERX0 (PA14) => PHY ADDR0
91 * ERX1 (PA15) => PHY ADDR1
92 * ERX2 (PA25) => PHY ADDR2
93 * ERX3 (PA26) => PHY ADDR3
94 * ECRS (PA28) => PHY ADDR4 => PHYADDR = 0x0
95 *
96 * PHY has internal pull-down
97 */
98 writel(pin_to_mask(AT91_PIN_PA14) |
99 pin_to_mask(AT91_PIN_PA15) |
100 pin_to_mask(AT91_PIN_PA17) |
101 pin_to_mask(AT91_PIN_PA25) |
102 pin_to_mask(AT91_PIN_PA26) |
Heiko Schocherb32f3912015-09-28 11:36:05 +0200103 pin_to_mask(AT91_PIN_PA28) |
104 pin_to_mask(AT91_PIN_PA29),
Heiko Schocher44a93442015-06-29 09:10:48 +0200105 &pioa->pudr);
106
107 at91_phy_reset();
108
109 /* Re-enable pull-up */
110 writel(pin_to_mask(AT91_PIN_PA14) |
111 pin_to_mask(AT91_PIN_PA15) |
112 pin_to_mask(AT91_PIN_PA17) |
113 pin_to_mask(AT91_PIN_PA25) |
114 pin_to_mask(AT91_PIN_PA26) |
Heiko Schocherb32f3912015-09-28 11:36:05 +0200115 pin_to_mask(AT91_PIN_PA28) |
116 pin_to_mask(AT91_PIN_PA29),
Heiko Schocher44a93442015-06-29 09:10:48 +0200117 &pioa->puer);
118
119 /* Initialize EMAC=MACB hardware */
120 at91_macb_hw_init();
121}
Heiko Schocher44a93442015-06-29 09:10:48 +0200122
Heiko Schochercf5137c2015-09-08 11:52:52 +0200123#ifdef CONFIG_USB_GADGET_AT91
124#include <linux/usb/at91_udc.h>
125
126void at91_udp_hw_init(void)
127{
Heiko Schochercf5137c2015-09-08 11:52:52 +0200128 /* Enable PLLB */
Wenyou Yangc5c6efe2016-02-03 10:20:45 +0800129 at91_pllb_clk_enable(get_pllb_init());
Heiko Schochercf5137c2015-09-08 11:52:52 +0200130
131 /* Enable UDPCK clock, MCK is enabled in at91_clock_init() */
132 at91_periph_clk_enable(ATMEL_ID_UDP);
133
Wenyou Yang78f89762016-02-03 10:16:50 +0800134 at91_system_clk_enable(AT91SAM926x_PMC_UDP);
Heiko Schochercf5137c2015-09-08 11:52:52 +0200135}
136
137struct at91_udc_data board_udc_data = {
138 .baseaddr = ATMEL_BASE_UDP0,
139};
140#endif
141
Heiko Schocher44a93442015-06-29 09:10:48 +0200142int board_early_init_f(void)
143{
144 /* enable this here, as we have SPL without serial support */
145 at91_seriald_hw_init();
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200146 smartweb_request_gpio();
Heiko Schocher44a93442015-06-29 09:10:48 +0200147 return 0;
148}
149
150int board_init(void)
151{
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200152 smartweb_request_gpio();
Heiko Schocher44a93442015-06-29 09:10:48 +0200153 /* power LED red */
154 at91_set_gpio_output(AT91_PIN_PC6, 0);
155 at91_set_gpio_output(AT91_PIN_PC7, 1);
156 /* alarm LED off */
157 at91_set_gpio_output(AT91_PIN_PC8, 0);
158 at91_set_gpio_output(AT91_PIN_PC9, 0);
159 /* prog LED red */
160 at91_set_gpio_output(AT91_PIN_PC10, 0);
161 at91_set_gpio_output(AT91_PIN_PC11, 1);
162
Heiko Schochercf5137c2015-09-08 11:52:52 +0200163#ifdef CONFIG_USB_GADGET_AT91
164 at91_udp_hw_init();
165 at91_udc_probe(&board_udc_data);
166#endif
167
Heiko Schocherb32f3912015-09-28 11:36:05 +0200168 /* Adress of boot parameters */
169 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
170
171 smartweb_nand_hw_init();
Heiko Schocherb32f3912015-09-28 11:36:05 +0200172 smartweb_macb_hw_init();
Heiko Schocher44a93442015-06-29 09:10:48 +0200173 return 0;
174}
175
176int dram_init(void)
177{
178 gd->ram_size = get_ram_size(
179 (void *)CONFIG_SYS_SDRAM_BASE,
180 CONFIG_SYS_SDRAM_SIZE);
181 return 0;
182}
183
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200184#ifndef CONFIG_DM_ETH
Heiko Schocher44a93442015-06-29 09:10:48 +0200185#ifdef CONFIG_MACB
186int board_eth_init(bd_t *bis)
187{
188 return macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC0, 0x00);
189}
190#endif /* CONFIG_MACB */
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200191#endif
Heiko Schocher44a93442015-06-29 09:10:48 +0200192
193#if defined(CONFIG_SPL_BUILD)
194#include <spl.h>
195#include <nand.h>
196#include <spi_flash.h>
197
198void matrix_init(void)
199{
200 struct at91_matrix *mat = (struct at91_matrix *)ATMEL_BASE_MATRIX;
201
202 writel((readl(&mat->scfg[3]) & (~AT91_MATRIX_SLOT_CYCLE))
203 | AT91_MATRIX_SLOT_CYCLE_(0x40),
204 &mat->scfg[3]);
205}
206
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200207void at91_spl_board_init(void)
Heiko Schocher44a93442015-06-29 09:10:48 +0200208{
Heiko Schocherd1b8ea82016-05-25 07:23:47 +0200209 smartweb_request_gpio();
Heiko Schocherb32f3912015-09-28 11:36:05 +0200210 /* power LED orange */
Heiko Schocher44a93442015-06-29 09:10:48 +0200211 at91_set_gpio_output(AT91_PIN_PC6, 1);
212 at91_set_gpio_output(AT91_PIN_PC7, 1);
213 /* alarm LED orange */
214 at91_set_gpio_output(AT91_PIN_PC8, 1);
215 at91_set_gpio_output(AT91_PIN_PC9, 1);
216 /* prog LED red */
217 at91_set_gpio_output(AT91_PIN_PC10, 0);
218 at91_set_gpio_output(AT91_PIN_PC11, 1);
219
220 smartweb_nand_hw_init();
221 at91_set_gpio_input(AT91_PIN_PA28, 1);
222 at91_set_gpio_input(AT91_PIN_PA29, 1);
223
224 /* check if both button are pressed */
225 if (at91_get_gpio_value(AT91_PIN_PA28) == 0 &&
Heiko Schocherb32f3912015-09-28 11:36:05 +0200226 at91_get_gpio_value(AT91_PIN_PA29) == 0) {
227 smartweb_nand_hw_init();
Heiko Schocher44a93442015-06-29 09:10:48 +0200228 nand_init();
229 spl_nand_erase_one(0, 0);
230 }
231}
232
233#define SDRAM_BASE_CONF (AT91_SDRAMC_NC_9 | AT91_SDRAMC_NR_13 \
234 | AT91_SDRAMC_CAS_2 \
235 | AT91_SDRAMC_NB_4 | AT91_SDRAMC_DBW_32 \
236 | AT91_SDRAMC_TWR_VAL(2) | AT91_SDRAMC_TRC_VAL(7) \
237 | AT91_SDRAMC_TRP_VAL(2) | AT91_SDRAMC_TRCD_VAL(2) \
238 | AT91_SDRAMC_TRAS_VAL(5) | AT91_SDRAMC_TXSR_VAL(8))
239
240void mem_init(void)
241{
242 struct at91_matrix *ma = (struct at91_matrix *)ATMEL_BASE_MATRIX;
243 struct at91_port *port = (struct at91_port *)ATMEL_BASE_PIOC;
244 struct sdramc_reg setting;
245
246 setting.cr = SDRAM_BASE_CONF;
247 setting.mdr = AT91_SDRAMC_MD_SDRAM;
248 setting.tr = (CONFIG_SYS_MASTER_CLOCK * 7) / 1000000;
249
250 /*
251 * I write here directly in this register, because this
252 * approach is smaller than calling at91_set_a_periph() in a
253 * for loop. This saved me 96 bytes.
254 */
255 writel(0xffff0000, &port->pdr);
256
257 writel(readl(&ma->ebicsa) | AT91_MATRIX_CS1A_SDRAMC, &ma->ebicsa);
258 sdramc_initialize(ATMEL_BASE_CS1, &setting);
259}
260#endif
Heiko Schocher01f078b2017-06-26 13:26:14 +0200261
262int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
263{
264 g_dnl_set_serialnumber("1");
265 return 0;
266}