1 /*
2  * (C) Copyright 2007-2008
3  * Stelian Pop <stelian@popies.net>
4  * Lead Tech Design <www.leadtechdesign.com>
5  *
6  * Achim Ehrlich <aehrlich@taskit.de>
7  * taskit GmbH <www.taskit.de>
8  *
9  * (C) Copyright 2012-
10  * Markus Hubig <mhubig@imko.de>
11  * IMKO GmbH <www.imko.de>
12  * (C) Copyright 2014
13  * Heiko Schocher <hs@denx.de>
14  * DENX Software Engineering GmbH
15  *
16  * SPDX-License-Identifier:	GPL-2.0+
17  */
18 
19 #include <common.h>
20 #include <asm/io.h>
21 #include <asm/arch/at91sam9_sdramc.h>
22 #include <asm/arch/at91sam9260_matrix.h>
23 #include <asm/arch/at91sam9_smc.h>
24 #include <asm/arch/at91_common.h>
25 #include <asm/arch/at91_spi.h>
26 #include <spi.h>
27 #include <asm/arch/clk.h>
28 #include <asm/arch/gpio.h>
29 #include <watchdog.h>
30 #ifdef CONFIG_MACB
31 # include <net.h>
32 # include <netdev.h>
33 #endif
34 
35 DECLARE_GLOBAL_DATA_PTR;
36 
37 static void smartweb_nand_hw_init(void)
38 {
39 	struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
40 	struct at91_matrix *matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
41 	unsigned long csa;
42 
43 	/* Assign CS3 to NAND/SmartMedia Interface */
44 	csa = readl(&matrix->ebicsa);
45 	csa |= AT91_MATRIX_CS3A_SMC_SMARTMEDIA;
46 	writel(csa, &matrix->ebicsa);
47 
48 	/* Configure SMC CS3 for NAND/SmartMedia */
49 	writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
50 		AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(0),
51 		&smc->cs[3].setup);
52 	writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(3) |
53 		AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(3),
54 		&smc->cs[3].pulse);
55 	writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
56 	       &smc->cs[3].cycle);
57 	writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
58 		AT91_SMC_MODE_TDF_CYCLE(2),
59 		&smc->cs[3].mode);
60 
61 	/* Configure RDY/BSY */
62 	at91_set_gpio_input(CONFIG_SYS_NAND_READY_PIN, 1);
63 
64 	/* Enable NandFlash */
65 	at91_set_gpio_output(CONFIG_SYS_NAND_ENABLE_PIN, 1);
66 }
67 
68 #ifdef CONFIG_MACB
69 static void smartweb_macb_hw_init(void)
70 {
71 	struct at91_port *pioa = (struct at91_port *)ATMEL_BASE_PIOA;
72 
73 	/* Enable the PHY Chip via PA26 on the Stamp 2 Adaptor */
74 	at91_set_gpio_output(AT91_PIN_PA26, 0);
75 
76 	/*
77 	 * Disable pull-up on:
78 	 *	RXDV (PA17) => PHY normal mode (not Test mode)
79 	 *	ERX0 (PA14) => PHY ADDR0
80 	 *	ERX1 (PA15) => PHY ADDR1
81 	 *	ERX2 (PA25) => PHY ADDR2
82 	 *	ERX3 (PA26) => PHY ADDR3
83 	 *	ECRS (PA28) => PHY ADDR4  => PHYADDR = 0x0
84 	 *
85 	 * PHY has internal pull-down
86 	 */
87 	writel(pin_to_mask(AT91_PIN_PA14) |
88 		pin_to_mask(AT91_PIN_PA15) |
89 		pin_to_mask(AT91_PIN_PA17) |
90 		pin_to_mask(AT91_PIN_PA25) |
91 		pin_to_mask(AT91_PIN_PA26) |
92 		pin_to_mask(AT91_PIN_PA28) |
93 		pin_to_mask(AT91_PIN_PA29),
94 		&pioa->pudr);
95 
96 	at91_phy_reset();
97 
98 	/* Re-enable pull-up */
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) |
104 		pin_to_mask(AT91_PIN_PA28) |
105 		pin_to_mask(AT91_PIN_PA29),
106 		&pioa->puer);
107 
108 	/* Initialize EMAC=MACB hardware */
109 	at91_macb_hw_init();
110 }
111 #endif /* CONFIG_MACB */
112 
113 #ifdef CONFIG_USB_GADGET_AT91
114 #include <linux/usb/at91_udc.h>
115 
116 void at91_udp_hw_init(void)
117 {
118 	/* Enable PLLB */
119 	at91_pllb_clk_enable(get_pllb_init());
120 
121 	/* Enable UDPCK clock, MCK is enabled in at91_clock_init() */
122 	at91_periph_clk_enable(ATMEL_ID_UDP);
123 
124 	at91_system_clk_enable(AT91SAM926x_PMC_UDP);
125 }
126 
127 struct at91_udc_data board_udc_data  = {
128 	.baseaddr = ATMEL_BASE_UDP0,
129 };
130 #endif
131 
132 int board_early_init_f(void)
133 {
134 	/* enable this here, as we have SPL without serial support */
135 	at91_seriald_hw_init();
136 	return 0;
137 }
138 
139 int board_init(void)
140 {
141 	/* power LED red */
142 	at91_set_gpio_output(AT91_PIN_PC6, 0);
143 	at91_set_gpio_output(AT91_PIN_PC7, 1);
144 	/* alarm LED off */
145 	at91_set_gpio_output(AT91_PIN_PC8, 0);
146 	at91_set_gpio_output(AT91_PIN_PC9, 0);
147 	/* prog LED red */
148 	at91_set_gpio_output(AT91_PIN_PC10, 0);
149 	at91_set_gpio_output(AT91_PIN_PC11, 1);
150 
151 #ifdef CONFIG_USB_GADGET_AT91
152 	at91_udp_hw_init();
153 	at91_udc_probe(&board_udc_data);
154 #endif
155 
156 	/* Adress of boot parameters */
157 	gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
158 
159 	smartweb_nand_hw_init();
160 #ifdef CONFIG_MACB
161 	smartweb_macb_hw_init();
162 #endif
163 	return 0;
164 }
165 
166 int dram_init(void)
167 {
168 	gd->ram_size = get_ram_size(
169 		(void *)CONFIG_SYS_SDRAM_BASE,
170 		CONFIG_SYS_SDRAM_SIZE);
171 	return 0;
172 }
173 
174 #ifdef CONFIG_MACB
175 int board_eth_init(bd_t *bis)
176 {
177 	return macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC0, 0x00);
178 }
179 #endif /* CONFIG_MACB */
180 
181 #if defined(CONFIG_SPL_BUILD)
182 #include <spl.h>
183 #include <nand.h>
184 #include <spi_flash.h>
185 
186 void matrix_init(void)
187 {
188 	struct at91_matrix *mat = (struct at91_matrix *)ATMEL_BASE_MATRIX;
189 
190 	writel((readl(&mat->scfg[3]) & (~AT91_MATRIX_SLOT_CYCLE))
191 			| AT91_MATRIX_SLOT_CYCLE_(0x40),
192 			&mat->scfg[3]);
193 }
194 
195 void spl_board_init(void)
196 {
197 	/* power LED orange */
198 	at91_set_gpio_output(AT91_PIN_PC6, 1);
199 	at91_set_gpio_output(AT91_PIN_PC7, 1);
200 	/* alarm LED orange */
201 	at91_set_gpio_output(AT91_PIN_PC8, 1);
202 	at91_set_gpio_output(AT91_PIN_PC9, 1);
203 	/* prog LED red */
204 	at91_set_gpio_output(AT91_PIN_PC10, 0);
205 	at91_set_gpio_output(AT91_PIN_PC11, 1);
206 
207 	smartweb_nand_hw_init();
208 	at91_set_gpio_input(AT91_PIN_PA28, 1);
209 	at91_set_gpio_input(AT91_PIN_PA29, 1);
210 
211 	/* check if both  button are pressed */
212 	if (at91_get_gpio_value(AT91_PIN_PA28) == 0 &&
213 		at91_get_gpio_value(AT91_PIN_PA29) == 0) {
214 		smartweb_nand_hw_init();
215 		nand_init();
216 		spl_nand_erase_one(0, 0);
217 	}
218 }
219 
220 #define SDRAM_BASE_CONF	(AT91_SDRAMC_NC_9 | AT91_SDRAMC_NR_13 \
221 			 | AT91_SDRAMC_CAS_2 \
222 			 | AT91_SDRAMC_NB_4 | AT91_SDRAMC_DBW_32 \
223 			 | AT91_SDRAMC_TWR_VAL(2) | AT91_SDRAMC_TRC_VAL(7) \
224 			 | AT91_SDRAMC_TRP_VAL(2) | AT91_SDRAMC_TRCD_VAL(2) \
225 			 | AT91_SDRAMC_TRAS_VAL(5) | AT91_SDRAMC_TXSR_VAL(8))
226 
227 void mem_init(void)
228 {
229 	struct at91_matrix *ma = (struct at91_matrix *)ATMEL_BASE_MATRIX;
230 	struct at91_port *port = (struct at91_port *)ATMEL_BASE_PIOC;
231 	struct sdramc_reg setting;
232 
233 	setting.cr = SDRAM_BASE_CONF;
234 	setting.mdr = AT91_SDRAMC_MD_SDRAM;
235 	setting.tr = (CONFIG_SYS_MASTER_CLOCK * 7) / 1000000;
236 
237 	/*
238 	 * I write here directly in this register, because this
239 	 * approach is smaller than calling at91_set_a_periph() in a
240 	 * for loop. This saved me 96 bytes.
241 	 */
242 	writel(0xffff0000, &port->pdr);
243 
244 	writel(readl(&ma->ebicsa) | AT91_MATRIX_CS1A_SDRAMC, &ma->ebicsa);
245 	sdramc_initialize(ATMEL_BASE_CS1, &setting);
246 }
247 #endif
248