blob: 5c891d128316ac533d4f7172cd2b8d2662719d00 [file] [log] [blame]
Andre Schwarzc82fdea2011-04-14 15:11:44 +02001/*
2 * Copyright (C) 2007 Freescale Semiconductor, Inc.
3 *
4 * Copyright (C) 2011 Matrix Vision GmbH
5 * Andre Schwarz <andre.schwarz@matrix-vision.de>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Andre Schwarzc82fdea2011-04-14 15:11:44 +02008 */
9
10#include <common.h>
11#include <hwconfig.h>
12#include <i2c.h>
13#include <spi.h>
14#include <asm/io.h>
15#include <asm/fsl_mpc83xx_serdes.h>
16#include <fdt_support.h>
17#include <spd_sdram.h>
18#include "mergerbox.h"
19#include "fpga.h"
20#include "../common/mv_common.h"
21
22static void setup_serdes(void)
23{
24 fsl_setup_serdes(CONFIG_FSL_SERDES1, FSL_SERDES_PROTO_SATA,
25 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
26 fsl_setup_serdes(CONFIG_FSL_SERDES2, FSL_SERDES_PROTO_PEX,
27 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
28}
29
30#if defined(CONFIG_SYS_DRAM_TEST)
31int testdram(void)
32{
33 uint *pstart = (uint *) CONFIG_SYS_MEMTEST_START;
34 uint *pend = (uint *) CONFIG_SYS_MEMTEST_END;
35 uint *p;
36
37 printf("Testing DRAM from 0x%08x to 0x%08x\n",
38 CONFIG_SYS_MEMTEST_START, CONFIG_SYS_MEMTEST_END);
39
40 printf("DRAM test phase 1:\n");
41 for (p = pstart; p < pend; p++)
42 *p = 0xaaaaaaaa;
43
44 for (p = pstart; p < pend; p++) {
45 if (*p != 0xaaaaaaaa) {
46 printf("DRAM test fails at: %08x\n", (uint) p);
47 return 1;
48 }
49 }
50
51 printf("DRAM test phase 2:\n");
52 for (p = pstart; p < pend; p++)
53 *p = 0x55555555;
54
55 for (p = pstart; p < pend; p++) {
56 if (*p != 0x55555555) {
57 printf("DRAM test fails at: %08x\n", (uint) p);
58 return 1;
59 }
60 }
61
62 printf("DRAM test passed.\n");
63 return 0;
64}
65#endif
66
67phys_size_t initdram(int board_type)
68{
69 u32 msize;
70
71 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
72 volatile clk83xx_t *clk = (clk83xx_t *)&immr->clk;
73
74 /* Enable PCI_CLK[0:1] */
75 clk->occr |= 0xc0000000;
76 udelay(2000);
77
78#if defined(CONFIG_SPD_EEPROM)
79 msize = spd_sdram();
80#else
81 immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
82 u32 msize_log2;
83
84 msize = CONFIG_SYS_DDR_SIZE;
85 msize_log2 = __ilog2(msize);
86
87 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_DDR_SDRAM_BASE & 0xfffff000;
88 im->sysconf.ddrlaw[0].ar = LBLAWAR_EN | (msize_log2 - 1);
89
90 im->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
91 udelay(50000);
92
93 im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
94 udelay(1000);
95
96 im->ddr.csbnds[0].csbnds = CONFIG_SYS_DDR_CS0_BNDS;
97 im->ddr.cs_config[0] = CONFIG_SYS_DDR_CS0_CONFIG;
98 udelay(1000);
99
100 im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
101 im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
102 im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
103 im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
104 im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG;
105 im->ddr.sdram_cfg2 = CONFIG_SYS_DDR_SDRAM_CFG2;
106 im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
107 im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE2;
108 im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
109 __asm__ __volatile__("sync");
110 udelay(1000);
111
112 im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN;
113 udelay(2000);
114#endif
115 setup_serdes();
116
117 return msize << 20;
118}
119
120int checkboard(void)
121{
122 puts("Board: Matrix Vision MergerBox\n");
123
124 return 0;
125}
126
127int misc_init_r(void)
128{
129 u16 dim;
130 int result;
131 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
132 volatile gpio83xx_t *gpio = (gpio83xx_t *)&immr->gpio[1];
133 unsigned char mac[6], mac_verify[6];
134 char *s = getenv("reset_env");
135
136 for (dim = 10; dim < 180; dim += 5) {
137 mergerbox_tft_dim(dim);
138 udelay(100000);
139 }
140
141 if (s)
142 mv_reset_environment();
143
144 i2c_read(SPD_EEPROM_ADDRESS, 0x80, 2, mac, sizeof(mac));
145
146 /* check if Matrix Vision prefix present and export to env */
147 if (mac[0] == 0x00 && mac[1] == 0x0c && mac[2] == 0x8d) {
148 printf("valid MAC found in eeprom: %pM\n", mac);
149 eth_setenv_enetaddr("ethaddr", mac);
150 } else {
151 printf("no valid MAC found in eeprom.\n");
152
153 /* no: check the env */
154 if (!eth_getenv_enetaddr("ethaddr", mac)) {
155 printf("no valid MAC found in env either.\n");
156 /* TODO: ask for valid MAC */
157 } else {
158 printf("valid MAC found in env: %pM\n", mac);
159 printf("updating MAC in eeprom.\n");
160
161 do {
162 result = test_and_clear_bit(20, &gpio->dat);
163 if (result)
164 printf("unprotect EEPROM failed !\n");
165 udelay(20000);
166 } while(result);
167
168 i2c_write(SPD_EEPROM_ADDRESS, 0x80, 2, mac, 6);
169 udelay(20000);
170
171 do {
172 result = test_and_set_bit(20, &gpio->dat);
173 if (result)
174 printf("protect EEPROM failed !\n");
175 udelay(20000);
176 } while(result);
177
178 printf("verify MAC %pM ... ", mac);
179 i2c_read(SPD_EEPROM_ADDRESS, 0x80, 2, mac_verify, 6);
180
181 if (!strncmp((char *)mac, (char *)mac_verify, 6))
182 printf("ok.\n");
183 else
184 /* TODO: retry or do something useful */
185 printf("FAILED (got %pM) !\n", mac_verify);
186 }
187 }
188
189 return 0;
190}
191
192int spi_cs_is_valid(unsigned int bus, unsigned int cs)
193{
194 return bus == 0 && cs == 0;
195}
196
197void spi_cs_activate(struct spi_slave *slave)
198{
199 volatile gpio83xx_t *iopd = &((immap_t *)CONFIG_SYS_IMMR)->gpio[0];
200
201 iopd->dat &= ~TFT_SPI_CPLD_CS;
202}
203
204void spi_cs_deactivate(struct spi_slave *slave)
205{
206 volatile gpio83xx_t *iopd = &((immap_t *)CONFIG_SYS_IMMR)->gpio[0];
207
208 iopd->dat |= TFT_SPI_CPLD_CS;
209}
210
211/* control backlight pwm (display brightness).
212 * allow values 0-250 with 0 = turn off and 250 = max brightness
213 */
214void mergerbox_tft_dim(u16 value)
215{
216 struct spi_slave *slave;
217 u16 din;
218 u16 dout = 0;
219
220 if (value > 0 && value < 250)
221 dout = 0x4000 | value;
222
223 slave = spi_setup_slave(0, 0, 1000000, SPI_MODE_0 | SPI_CS_HIGH);
224 spi_claim_bus(slave);
225 spi_xfer(slave, 16, &dout, &din, SPI_XFER_BEGIN | SPI_XFER_END);
226 spi_release_bus(slave);
227 spi_free_slave(slave);
228}
229
230void ft_board_setup(void *blob, bd_t *bd)
231{
232 ft_cpu_setup(blob, bd);
233 fdt_fixup_dr_usb(blob, bd);
234 ft_pci_setup(blob, bd);
235}