/*******************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only
 * intended for use with Renesas products. No other uses are authorized. This
 * software is owned by Renesas Electronics Corporation and is protected under
 * all applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software
 * and to discontinue the availability of this software. By using this software,
 * you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer
 * Copyright 2021-2024 Renesas Electronics Corporation All rights reserved.
 *******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : CPG initialize
 ******************************************************************************/
 /******************************************************************************
 * @file          cpg.c
 * - Version      : 0.03
 * @brief         Initial setting process of CPG.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 28.07.2021 0.01     First Release
 *         : 03.09.2021 0.02     Move cpg_reg_write function to cpg.h.
 *         : 27.12.2024 0.03     Add set_srcr function.
 *****************************************************************************/

#include <cpg.h>
#include <cnf_tbl.h>
#if (RCAR_LSI == RCAR_V4H)
#include <cpg_register.h>
#endif /* RCAR_LSI == RCAR_V4H */

/* CPG write protect value */
#define CPGWPCR_PASSWORD    (0xA5A50000U)
#define CPGWPCR_WPE         ((uint32_t)1U << 0U)
#define CPGWPCR_WPE_VALID   (0U)

#if (RCAR_LSI == RCAR_V4H)
static void set_srcr(void);
#endif /* RCAR_LSI == RCAR_V4H */

void cpg_init(void)
{
    /* Release CPG write protect */
    if((mem_read32(CPG_CPGWPCR) & CPGWPCR_WPE) != CPGWPCR_WPE_VALID)
    {
        mem_write32(CPG_CPGWPR, ~(uint32_t)(CPGWPCR_PASSWORD));
        mem_write32(CPG_CPGWPCR, CPGWPCR_PASSWORD);

        /* bit in WPE = 0? */
        while ((mem_read32(CPG_CPGWPCR) & CPGWPCR_WPE) != CPGWPCR_WPE_VALID)
        {
            ;
        }
    }

#if (RCAR_LSI == RCAR_V4H)
    set_srcr();
#endif /* RCAR_LSI == RCAR_V4H */
}
/* End of function cpg_init(void) */

#if (RCAR_LSI == RCAR_V4H)
static void set_srcr(void)
{
    mem_write32(CPG_SRCR28, CPGSRCR28_VAL);
    mem_write32(CPG_SRCR29, CPGSRCR29_VAL);
}
/* End of function set_srcr(void) */
#endif /* RCAR_LSI == RCAR_V4H */
