/*
 * Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <drivers/microchip/emmc.h>
#include "lan966x_private.h"

/* Logical block address inside eMMC device (memory offset = 0 * 512 bytes) */
#define logic_block_addr 0u

/* Create read buffer for the eMMC values */
static uint8_t test_buffer[MMC_BUF_SIZE] __aligned(16);

static uint16_t error_counter;

/* This random test pattern should be made available inside the eMMC device */
static const uint8_t test_pattern[MMC_BUF_SIZE] = {
	0xE2, 0x9C, 0xE4, 0x0E, 0x20, 0x08, 0xF0, 0xE3, 0x61, 0x5E, 0xCF, 0x31,
	0xC5, 0xA9, 0xF4, 0x4B, 0x86, 0x37, 0x80, 0x94, 0xEF, 0xAF, 0x8A, 0x94,
	0x1E, 0x91, 0x34, 0xC0, 0xD0, 0xA1, 0x37, 0x29, 0xDF, 0x37, 0xD3, 0x48,
	0x30, 0x9E, 0x9F, 0x5D, 0x15, 0xDA, 0x10, 0x6E, 0xE2, 0xF6, 0xD7, 0xC7,
	0x04, 0xD4, 0x1B, 0xFE, 0x17, 0x45, 0xF5, 0xB7, 0xCD, 0x3A, 0x47, 0xCA,
	0x28, 0x1E, 0x44, 0xBE, 0x47, 0x8D, 0x79, 0x73, 0xD1, 0xF6, 0xC4, 0x91,
	0x20, 0xA4, 0x9E, 0xB5, 0xA3, 0x5F, 0x4C, 0x17, 0x45, 0x83, 0x4B, 0x6B,
	0xA3, 0x47, 0x71, 0x38, 0x38, 0xE3, 0xFE, 0x97, 0xAD, 0xB0, 0xA1, 0x7F,
	0x1A, 0x25, 0x88, 0xE6, 0x66, 0xEE, 0xA2, 0x9A, 0x0B, 0xA0, 0x18, 0xD5,
	0x77, 0xE4, 0x45, 0xDC, 0x0E, 0x93, 0x97, 0x3C, 0xAD, 0x46, 0x81, 0x12,
	0x59, 0xF5, 0x9E, 0xE6, 0x97, 0x49, 0x00, 0x4D, 0x12, 0x97, 0x0D, 0xC3,
	0x50, 0x1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0F, 0x55, 0x56, 0x0C, 0x7F, 0x5D, 0xE6, 0x4E, 0xB2, 0x2D, 0xB1,
	0x1D, 0x35, 0x7D, 0x2F, 0xAA, 0x87, 0x1E, 0x15
};

static const struct _test_params {
	int clk_rate;
	int bus_width;
} test_params[] = {
	{MMC_DEFAULT_SPEED, MMC_BUS_WIDTH_1,},	// 10MHz
	{MMC_DEFAULT_SPEED, MMC_BUS_WIDTH_4,},	// 10MHz
	{MMC_DEFAULT_SPEED, MMC_BUS_WIDTH_8,},	// 10MHz
	{EMMC_NORM_SPEED, MMC_BUS_WIDTH_1,},	// 26MHz
	{EMMC_NORM_SPEED, MMC_BUS_WIDTH_4,},	// 26MHz
	{EMMC_NORM_SPEED, MMC_BUS_WIDTH_8,},	// 26MHz
	{EMMC_HIGH_SPEED, MMC_BUS_WIDTH_1,},	// 52MHz
	{EMMC_HIGH_SPEED, MMC_BUS_WIDTH_4,},	// 52MHz
	{EMMC_HIGH_SPEED, MMC_BUS_WIDTH_8,},	// 52MHz
};

/* Return 0 if compare successful, otherwise 1 */
static int memory_compare(uintptr_t dest, uintptr_t src, size_t size)
{
	return memcmp(test_buffer, test_pattern, sizeof(test_buffer));
}

static void emmc_test_prepare(int test_number)
{
	struct mmc_device_info info;
	lan966x_mmc_params_t params;

	memset(&params, 0, sizeof(lan966x_mmc_params_t));
	memset(&info, 0, sizeof(struct mmc_device_info));

	params.reg_base = LAN966X_SDMMC_BASE;
	params.desc_base = LAN966X_SRAM_BASE;
	params.desc_size = LAN966X_SRAM_SIZE;
	params.mmc_dev_type = MMC_IS_EMMC;
	params.clk_rate = test_params[test_number].clk_rate;
	params.bus_width = test_params[test_number].bus_width;

	info.mmc_dev_type = MMC_IS_EMMC;
	info.max_bus_freq = EMMC_HIGH_SPEED;
	info.block_size = MMC_BLOCK_SIZE;

	lan966x_mmc_init(&params, &info);
}

static void emmc_test_init(void)
{
	assert(sizeof(test_buffer) == sizeof(test_pattern));
	lan966x_mmc_plat_config(BOOT_SOURCE_EMMC);
	error_counter = 0u;
}

static void emmc_test_start(void)
{
	int i, rc;

	for (i = 0; i < ARRAY_SIZE(test_params); i++) {
		VERBOSE("MMC Test: Execute test #%d\n", i);

		/* clear buffer after each test cycle */
		memset(test_buffer, 0u, sizeof(test_buffer));

		/* Configure MMC host and device controller */
		emmc_test_prepare(i);

		/* Read 512 bytes from LBA 0x0 of the eMMC device */
		mmc_read_blocks(logic_block_addr, (uintptr_t) test_buffer,
				MMC_BUF_SIZE);

		/* Compare content of test_buffer with expected test_pattern */
		rc = memory_compare((uintptr_t) test_buffer,
				    (uintptr_t) test_pattern,
				    sizeof(test_pattern));
		if (rc) {
			WARN("MMC test: Test data compare failed in test #%d\n",
			     i);
			error_counter++;
		}
	}

	/* Provide test result */
	VERBOSE("\n");
	VERBOSE("Number of failed tests: %d\n", error_counter);

	assert(error_counter == 0u);
}

/* Internal eMMC test function */
static void emmc_test(void)
{
	/* Initialize platform configuration and local test buffer */
	emmc_test_init();

	INFO("MMC Test: Running the eMMC build-in-self-test\n");
	emmc_test_start();
}

/* Public function for running the MMC test suite */
void lan966x_emmc_tests(void)
{
	emmc_test();
}
