/*
 * Copyright (c) 2015-2017, Renesas Electronics Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Renesas nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdint.h>
#include <mmio.h>
#include "rcar_def.h"
#include "rom_api.h"


typedef uint32_t (*ROM_SECURE_BOOT_API)(uint32_t *pKeyCert,
					uint32_t *pContentCert,
					ROM_FuncReadFlash pFuncReadFlash);
typedef uint32_t (*ROM_GETLCS_API)(uint32_t *pLcs);

static uint32_t get_table_index(void);

#define V3M_API_TABLE	(0U)		/* V3M ES1.0 */
#define NEW_API_TABLE	(1U)		/* V3M ES2.0 */
#define API_TABLE_MAX	(2U)		/* table max */

uint32_t ROM_SecureBootAPI( uint32_t *pKeyCert,
			   uint32_t *pContentCert,
			   ROM_FuncReadFlash pFuncReadFlash)
{
	/* Secure Boot API address table */
	static const uintptr_t ROM_SecureBootAPI_table[API_TABLE_MAX] = {
		0xEB110128U,		/* V3M ES1.0 */
		0xEB100180U		/* V3M ES2.0 or later */
	};

	ROM_SECURE_BOOT_API func;
	uint32_t index;

	index = get_table_index();
	func = (ROM_SECURE_BOOT_API)ROM_SecureBootAPI_table[index];

	return func(pKeyCert, pContentCert, pFuncReadFlash);
}


uint32_t ROM_GetLcs(uint32_t *pLcs)
{
	/* Get LCS stete API address table */
	static const uintptr_t ROM_GetLcs_table[API_TABLE_MAX] = {
		0xEB1103A4U,		/* V3M ES1.0 */
		0xEB10018CU		/* V3M ES2.0 or later */
	};

	ROM_GETLCS_API func;
	uint32_t index;

	index = get_table_index();
	func = (ROM_GETLCS_API)ROM_GetLcs_table[index];

	return func(pLcs);
}


static uint32_t get_table_index(void)
{
	uint32_t cut_ver;
	uint32_t index;

	cut_ver = mmio_read_32(RCAR_PRR) & RCAR_CUT_MASK;

	if (cut_ver == RCAR_CUT_ES10) {
		index = V3M_API_TABLE;	/* V3M ES1.0 */
	} else {
		index = NEW_API_TABLE;	/* V3M ES2.0 or later */
	}

	return index;
}
