km_arm.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * (C) Copyright 2009
 * Marvell Semiconductor <www.marvell.com>
 * Prafulla Wadaskar <prafulla@marvell.com>
 *
 * (C) Copyright 2009
 * Stefan Roese, DENX Software Engineering, sr@denx.de.
 *
 * (C) Copyright 2010
 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include <common.h>
#include <i2c.h>
#include <nand.h>
#include <netdev.h>
#include <miiphy.h>
36
#include <spi.h>
37
#include <asm/io.h>
38
#include <asm/arch/cpu.h>
39 40 41 42 43 44 45
#include <asm/arch/kirkwood.h>
#include <asm/arch/mpp.h>

#include "../common/common.h"

DECLARE_GLOBAL_DATA_PTR;

46 47 48 49 50 51 52 53 54 55
/*
 * BOCO FPGA definitions
 */
#define BOCO		0x10
#define REG_CTRL_H		0x02
#define MASK_WRL_UNITRUN	0x01
#define MASK_RBX_PGY_PRESENT	0x40
#define REG_IRQ_CIRQ2		0x2d
#define MASK_RBI_DEFECT_16	0x01

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/* Multi-Purpose Pins Functionality configuration */
u32 kwmpp_config[] = {
	MPP0_NF_IO2,
	MPP1_NF_IO3,
	MPP2_NF_IO4,
	MPP3_NF_IO5,
	MPP4_NF_IO6,
	MPP5_NF_IO7,
	MPP6_SYSRST_OUTn,
	MPP7_PEX_RST_OUTn,
#if defined(CONFIG_SOFT_I2C)
	MPP8_GPIO,		/* SDA */
	MPP9_GPIO,		/* SCL */
#endif
#if defined(CONFIG_HARD_I2C)
	MPP8_TW_SDA,
	MPP9_TW_SCK,
#endif
	MPP10_UART0_TXD,
	MPP11_UART0_RXD,
	MPP12_GPO,		/* Reserved */
	MPP13_UART1_TXD,
	MPP14_UART1_RXD,
	MPP15_GPIO,		/* Not used */
	MPP16_GPIO,		/* Not used */
	MPP17_GPIO,		/* Reserved */
	MPP18_NF_IO0,
	MPP19_NF_IO1,
	MPP20_GPIO,
	MPP21_GPIO,
	MPP22_GPIO,
	MPP23_GPIO,
	MPP24_GPIO,
	MPP25_GPIO,
	MPP26_GPIO,
	MPP27_GPIO,
	MPP28_GPIO,
	MPP29_GPIO,
	MPP30_GPIO,
	MPP31_GPIO,
	MPP32_GPIO,
	MPP33_GPIO,
	MPP34_GPIO,		/* CDL1 (input) */
	MPP35_GPIO,		/* CDL2 (input) */
	MPP36_GPIO,		/* MAIN_IRQ (input) */
	MPP37_GPIO,		/* BOARD_LED */
	MPP38_GPIO,		/* Piggy3 LED[1] */
	MPP39_GPIO,		/* Piggy3 LED[2] */
	MPP40_GPIO,		/* Piggy3 LED[3] */
	MPP41_GPIO,		/* Piggy3 LED[4] */
	MPP42_GPIO,		/* Piggy3 LED[5] */
	MPP43_GPIO,		/* Piggy3 LED[6] */
108
	MPP44_GPIO,		/* Piggy3 LED[7], BIST_EN_L */
109 110 111 112 113 114 115 116
	MPP45_GPIO,		/* Piggy3 LED[8] */
	MPP46_GPIO,		/* Reserved */
	MPP47_GPIO,		/* Reserved */
	MPP48_GPIO,		/* Reserved */
	MPP49_GPIO,		/* SW_INTOUTn */
	0
};

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#if defined(CONFIG_MGCOGE3UN)
/*
 * Wait for startup OK from mgcoge3ne
 */
int startup_allowed(void)
{
	unsigned char buf;

	/*
	 * Read CIRQ16 bit (bit 0)
	 */
	if (i2c_read(BOCO, REG_IRQ_CIRQ2, 1, &buf, 1) != 0)
		printf("%s: Error reading Boco\n", __func__);
	else
		if ((buf & MASK_RBI_DEFECT_16) == MASK_RBI_DEFECT_16)
			return 1;
	return 0;
}
135
#endif
136

H
Holger Brunck 已提交
137 138
#if (defined(CONFIG_MGCOGE3UN)|defined(CONFIG_PORTL2)| \
	defined(CONFIG_KM_PIGGY4_88E6352))
139
/*
H
Holger Brunck 已提交
140 141
 * All boards with PIGGY4 connected via a simple switch have ethernet always
 * present.
142 143 144 145 146 147
 */
int ethernet_present(void)
{
	return 1;
}
#else
148 149 150 151 152
int ethernet_present(void)
{
	uchar	buf;
	int	ret = 0;

153
	if (i2c_read(BOCO, REG_CTRL_H, 1, &buf, 1) != 0) {
154
		printf("%s: Error reading Boco\n", __func__);
155 156
		return -1;
	}
157
	if ((buf & MASK_RBX_PGY_PRESENT) == MASK_RBX_PGY_PRESENT)
158
		ret = 1;
159

160 161
	return ret;
}
162
#endif
163

164 165 166
int initialize_unit_leds(void)
{
	/*
167
	 * Init the unit LEDs per default they all are
168 169 170 171
	 * ok apart from bootstat
	 */
	uchar buf;

172
	if (i2c_read(BOCO, REG_CTRL_H, 1, &buf, 1) != 0) {
173 174 175
		printf("%s: Error reading Boco\n", __func__);
		return -1;
	}
176 177
	buf |= MASK_WRL_UNITRUN;
	if (i2c_write(BOCO, REG_CTRL_H, 1, &buf, 1) != 0) {
178 179 180 181 182 183
		printf("%s: Error writing Boco\n", __func__);
		return -1;
	}
	return 0;
}

184 185 186 187 188 189 190 191 192 193 194
#if defined(CONFIG_BOOTCOUNT_LIMIT)
void set_bootcount_addr(void)
{
	uchar buf[32];
	unsigned int bootcountaddr;
	bootcountaddr = gd->ram_size - BOOTCOUNT_ADDR;
	sprintf((char *)buf, "0x%x", bootcountaddr);
	setenv("bootcountaddr", (char *)buf);
}
#endif

195 196 197 198 199 200 201 202 203 204 205
int misc_init_r(void)
{
	char *str;
	int mach_type;

	str = getenv("mach_type");
	if (str != NULL) {
		mach_type = simple_strtoul(str, NULL, 10);
		printf("Overwriting MACH_TYPE with %d!!!\n", mach_type);
		gd->bd->bi_arch_number = mach_type;
	}
206 207 208 209 210 211
#if defined(CONFIG_MGCOGE3UN)
	char *wait_for_ne;
	wait_for_ne = getenv("waitforne");
	if (wait_for_ne != NULL) {
		if (strcmp(wait_for_ne, "true") == 0) {
			int cnt = 0;
212
			int abort = 0;
213 214
			puts("NE go: ");
			while (startup_allowed() == 0) {
215 216 217 218 219
				if (tstc()) {
					(void) getc(); /* consume input */
					abort = 1;
					break;
				}
220 221 222 223 224 225 226 227 228
				udelay(200000);
				cnt++;
				if (cnt == 5)
					puts("wait\b\b\b\b");
				if (cnt == 10) {
					cnt = 0;
					puts("    \b\b\b\b");
				}
			}
229 230 231 232
			if (abort == 1)
				printf("\nAbort waiting for ne\n");
			else
				puts("OK\n");
233 234 235
		}
	}
#endif
236 237

	initialize_unit_leds();
238 239 240 241
	set_km_env();
#if defined(CONFIG_BOOTCOUNT_LIMIT)
	set_bootcount_addr();
#endif
242 243 244
	return 0;
}

245
int board_early_init_f(void)
246 247 248
{
	u32 tmp;

249
	kirkwood_mpp_conf(kwmpp_config, NULL);
250 251 252 253 254 255 256 257 258 259 260 261 262

	/*
	 * The FLASH_GPIO_PIN switches between using a
	 * NAND or a SPI FLASH. Set this pin on start
	 * to NAND mode.
	 */
	tmp = readl(KW_GPIO0_BASE);
	writel(tmp | FLASH_GPIO_PIN , KW_GPIO0_BASE);
	tmp = readl(KW_GPIO0_BASE + 4);
	writel(tmp & (~FLASH_GPIO_PIN) , KW_GPIO0_BASE + 4);

#if defined(CONFIG_SOFT_I2C)
	/* init the GPIO for I2C Bitbang driver */
263 264 265 266
	kw_gpio_set_valid(KM_KIRKWOOD_SDA_PIN, 1);
	kw_gpio_set_valid(KM_KIRKWOOD_SCL_PIN, 1);
	kw_gpio_direction_output(KM_KIRKWOOD_SDA_PIN, 0);
	kw_gpio_direction_output(KM_KIRKWOOD_SCL_PIN, 0);
267 268
#endif
#if defined(CONFIG_SYS_EEPROM_WREN)
269 270
	kw_gpio_set_valid(KM_KIRKWOOD_ENV_WP, 38);
	kw_gpio_direction_output(KM_KIRKWOOD_ENV_WP, 1);
271
#endif
272 273 274 275 276 277
#if defined(CONFIG_KM_RECONFIG_XLX)
	/* trigger the reconfiguration of the xilinx fpga */
	kw_gpio_set_valid(KM_XLX_PROGRAM_B_PIN, 1);
	kw_gpio_direction_output(KM_XLX_PROGRAM_B_PIN, 0);
	kw_gpio_direction_input(KM_XLX_PROGRAM_B_PIN);
#endif
278 279 280 281 282 283 284 285
	return 0;
}

int board_init(void)
{
	/* address of boot parameters */
	gd->bd->bi_boot_params = kw_sdram_bar(0) + 0x100;

286 287 288
	return 0;
}

289
int board_spi_claim_bus(struct spi_slave *slave)
290
{
291
	kw_gpio_set_value(KM_FLASH_GPIO_PIN, 0);
292 293 294 295

	return 0;
}

296 297 298 299
void board_spi_release_bus(struct spi_slave *slave)
{
	kw_gpio_set_value(KM_FLASH_GPIO_PIN, 1);
}
300

301 302 303 304
int dram_init(void)
{
	/* dram_init must store complete ramsize in gd->ram_size */
	/* Fix this */
305
	gd->ram_size = get_ram_size((void *)kw_sdram_bar(0),
306
				kw_sdram_bs(0));
307 308 309
	return 0;
}

310 311 312 313 314 315 316 317 318 319 320
void dram_init_banksize(void)
{
	int i;

	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
		gd->bd->bi_dram[i].start = kw_sdram_bar(i);
		gd->bd->bi_dram[i].size = get_ram_size((long *)kw_sdram_bar(i),
						       kw_sdram_bs(i));
	}
}

321
#if (defined(CONFIG_MGCOGE3UN)|defined(CONFIG_PORTL2))
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

#define	PHY_LED_SEL	0x18
#define PHY_LED0_LINK	(0x5)
#define PHY_LED1_ACT	(0x8<<4)
#define PHY_LED2_INT	(0xe<<8)
#define	PHY_SPEC_CTRL	0x1c
#define PHY_RGMII_CLK_STABLE	(0x1<<10)
#define PHY_CLSA	(0x1<<1)

/* Configure and enable MV88E3018 PHY */
void reset_phy(void)
{
	char *name = "egiga0";
	unsigned short reg;

	if (miiphy_set_current_dev(name))
		return;

	/* RGMII clk transition on data stable */
	if (miiphy_read(name, CONFIG_PHY_BASE_ADR, PHY_SPEC_CTRL, &reg) != 0)
		printf("Error reading PHY spec ctrl reg\n");
	if (miiphy_write(name, CONFIG_PHY_BASE_ADR, PHY_SPEC_CTRL,
		reg | PHY_RGMII_CLK_STABLE | PHY_CLSA) != 0)
		printf("Error writing PHY spec ctrl reg\n");

	/* leds setup */
	if (miiphy_write(name, CONFIG_PHY_BASE_ADR, PHY_LED_SEL,
		PHY_LED0_LINK | PHY_LED1_ACT | PHY_LED2_INT) != 0)
		printf("Error writing PHY LED reg\n");

	/* reset the phy */
	miiphy_reset(name, CONFIG_PHY_BASE_ADR);
}
#else
/* Configure and enable MV88E1118 PHY on the piggy*/
357 358 359 360 361 362 363 364 365 366
void reset_phy(void)
{
	char *name = "egiga0";

	if (miiphy_set_current_dev(name))
		return;

	/* reset the phy */
	miiphy_reset(name, CONFIG_PHY_BASE_ADR);
}
367 368
#endif

369 370

#if defined(CONFIG_HUSH_INIT_VAR)
371
int hush_init_var(void)
372
{
373
	ivm_read_eeprom();
374 375 376 377 378
	return 0;
}
#endif

#if defined(CONFIG_BOOTCOUNT_LIMIT)
379 380 381 382 383
const ulong patterns[]      = {	0x00000000,
				0xFFFFFFFF,
				0xFF00FF00,
				0x0F0F0F0F,
				0xF0F0F0F0};
H
Holger Brunck 已提交
384
const ulong NBR_OF_PATTERNS = ARRAY_SIZE(patterns);
385 386 387
const ulong OFFS_PATTERN    = 3;
const ulong REPEAT_PATTERN  = 1000;

388
void bootcount_store(ulong a)
389
{
H
Holger Brunck 已提交
390 391
	ulong *save_addr;
	ulong size = 0;
392
	int i;
H
Holger Brunck 已提交
393 394

	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
395
		size += gd->bd->bi_dram[i].size;
H
Holger Brunck 已提交
396
	save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
397 398
	writel(a, save_addr);
	writel(BOOTCOUNT_MAGIC, &save_addr[1]);
399 400 401 402 403

	for (i = 0; i < REPEAT_PATTERN; i++)
		writel(patterns[i % NBR_OF_PATTERNS],
			&save_addr[i+OFFS_PATTERN]);

404 405
}

406
ulong bootcount_load(void)
407
{
H
Holger Brunck 已提交
408 409
	ulong *save_addr;
	ulong size = 0;
410 411 412
	ulong counter = 0;
	int i, tmp;

H
Holger Brunck 已提交
413
	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
414
		size += gd->bd->bi_dram[i].size;
H
Holger Brunck 已提交
415
	save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
416 417 418 419 420 421 422 423 424 425

	counter = readl(&save_addr[0]);

	/* Is the counter reliable, check in the big pattern for bit errors */
	for (i = 0; (i < REPEAT_PATTERN) && (counter != 0); i++) {
		tmp = readl(&save_addr[i+OFFS_PATTERN]);
		if (tmp != patterns[i % NBR_OF_PATTERNS])
			counter = 0;
	}
	return counter;
426 427 428 429
}
#endif

#if defined(CONFIG_SOFT_I2C)
430
void set_sda(int state)
431 432 433 434 435
{
	I2C_ACTIVE;
	I2C_SDA(state);
}

436
void set_scl(int state)
437 438 439 440
{
	I2C_SCL(state);
}

441
int get_sda(void)
442 443 444 445 446
{
	I2C_TRISTATE;
	return I2C_READ;
}

447
int get_scl(void)
448
{
449
	return kw_gpio_get_value(KM_KIRKWOOD_SCL_PIN) ? 1 : 0;
450 451 452
}
#endif

453 454 455 456 457 458 459
#if defined(CONFIG_POST)

#define KM_POST_EN_L	44
#define POST_WORD_OFF	8

int post_hotkeys_pressed(void)
{
460 461 462
#if defined(CONFIG_KM_COGE5UN)
	return kw_gpio_get_value(KM_POST_EN_L);
#else
463
	return !kw_gpio_get_value(KM_POST_EN_L);
464
#endif
465 466 467 468
}

ulong post_word_load(void)
{
H
Holger Brunck 已提交
469
	void* addr = (void *) (gd->ram_size - BOOTCOUNT_ADDR + POST_WORD_OFF);
470 471 472 473 474
	return in_le32(addr);

}
void post_word_store(ulong value)
{
H
Holger Brunck 已提交
475
	void* addr = (void *) (gd->ram_size - BOOTCOUNT_ADDR + POST_WORD_OFF);
476 477 478 479 480 481 482 483 484 485 486 487 488 489
	out_le32(addr, value);
}

int arch_memory_test_prepare(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
{
	*vstart = CONFIG_SYS_SDRAM_BASE;

	/* we go up to relocation plus a 1 MB margin */
	*size = CONFIG_SYS_TEXT_BASE - (1<<20);

	return 0;
}
#endif

490
#if defined(CONFIG_SYS_EEPROM_WREN)
491
int eeprom_write_enable(unsigned dev_addr, int state)
492
{
493
	kw_gpio_set_value(KM_KIRKWOOD_ENV_WP, !state);
494

495
	return !kw_gpio_get_value(KM_KIRKWOOD_ENV_WP);
496 497
}
#endif