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