/*
 * arch/arm/mach-discovery/mpp.c
 *
 * MPP functions for Marvell Discovery SoCs
 * Referenced from Linux kernel source
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2.  This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

/* based on cpu/arm926ejs/kirkwood/mpp.h */

#include <common.h>
#include <asm/arch/discovery.h>
#include <asm/arch/mpp.h>

static u32 discovery_variant(void)
{
	unsigned int devrev;

	devrev = readl(DISCO_REG_DEVVEN_ID) & 0xffff0000;

	switch (devrev) {
	case 0x78100000:
		return MPP_SV571_MASK;
	default:
		debug("MPP setup: unknown discovery variant\n");
		return 0;
	}
}

#define MPP_CTRL(i)	(DISCO_MPP_BASE + (i * 4))
#define MPP_NR_REGS	(1 + MPP_MAX/8)

void discovery_mpp_conf(u32 *mpp_list)
{
	u32 mpp_ctrl[MPP_NR_REGS];
	unsigned int variant_mask;
	int i;

	variant_mask = discovery_variant();
	if (!variant_mask)
		return;

	debug( "initial MPP regs:");
	for (i = 0; i < MPP_NR_REGS; i++) {
		mpp_ctrl[i] = readl(MPP_CTRL(i));
		debug(" %08x", mpp_ctrl[i]);
	}
	debug("\n");


	while (*mpp_list) {
		unsigned int num = MPP_NUM(*mpp_list);
		unsigned int sel = MPP_SEL(*mpp_list);
		int shift;

		if (num > MPP_MAX) {
			debug("discovery_mpp_conf: invalid MPP "
					"number (%u)\n", num);
			continue;
		}
		if (!(*mpp_list & variant_mask)) {
			debug("discovery_mpp_conf: requested MPP%u config "
				"unavailable on this hardware\n", num);
			continue;
		}

		shift = (num & 7) << 2;
		mpp_ctrl[num / 8] &= ~(0xf << shift);
		mpp_ctrl[num / 8] |= sel << shift;

		mpp_list++;
	}

	debug("  final MPP regs:");
	for (i = 0; i < MPP_NR_REGS; i++) {
		writel(mpp_ctrl[i], MPP_CTRL(i));
		debug(" %08x", mpp_ctrl[i]);
	}
	debug("\n");

}
