r_sys_hypb.c 10.6 KB
Newer Older
hu's avatar
hu committed
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 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 357 358 359 360
/*
****************************************************************************
PROJECT : HYPB driver
FILE    : $Id: r_sys_hypb.c 13439 2017-05-10 08:43:53Z shinya.tomari $
============================================================================
DESCRIPTION
HYPB support functions for d1x
============================================================================
                            C O P Y R I G H T
============================================================================
                           Copyright (c) 2016
                                  by
                       Renesas Electronics (Europe) GmbH.
                           Arcadiastrasse 10
                          D-40472 Duesseldorf
                               Germany
                          All rights reserved.
============================================================================
Purpose: only for testing, not for mass production

DISCLAIMER

LICENSEE has read, understood and accepted the terms and conditions defined in
the license agreement, especially the usage rights. In any case, it is
LICENSEE's responsibility to make sure that any user of the software complies
with the terms and conditions of the signed license agreement.

SAMPLE CODE is not part of the licensed software, as such it must not be used in
mass-production applications. It can only be used for evaluation and
demonstration purposes at customer's premises listed in the signed license
agreement.

****************************************************************************
*/


/***************************************************************************
  Title: HYPB Driver support functions

  Implementation of the HYPB support functions for the D1x device.

*/

/***************************************************************************
  Section: Includes
*/
#include "r_typedefs.h"
#include "r_hypb_api.h"
#include "r_hypb_sys.h"
#include "r_config_hypb.h"
#include "r_dev_api.h"


/***************************************************************************
  Section: Local Defines
*/

/***************************************************************************
  Constant: LOC_REAC_CFG_CHECK_~

  Definitions to check the reading failure of the configuration register.
  
  LOC_READ_CFG_CHECK_RAM   - Check value of Hyper RAM.
  LOC_READ_CFG_CHECK_FLASH - Check value of Hyper Flash.
*/
#define LOC_READ_CFG_CHECK_RAM      (0x8F08uL)
#define LOC_READ_CFG_CHECK_FLASH    (0x8008uL)

/***************************************************************************
  Constant: LOC_WRITE_CFG_DEFAULT_~

  Definitions for configuration register default value.
  
  LOC_WRITE_CFG_DEFAULT_RAM   - Default value of Hyper RAM.
  LOC_WRITE_CFG_DEFAULT_FLASH - Default value of Hyper Flash.
*/
#define LOC_WRITE_CFG_DEFAULT_RAM   (0x8F1FuL)
#define LOC_WRITE_CFG_DEFAULT_FLASH (0x8EBBuL)

/***************************************************************************
  Constant: LOC_LATENCY_CODE_~

  Definitions for latency code field.
  
  LOC_LATENCY_CODE_MASK  - Mask of latency code field.
  LOC_LATENCY_CODE_SHIFT - Bit shift for latency code field.
*/
#define LOC_LATENCY_CODE_MASK       (0x00F0uL)
#define LOC_LATENCY_CODE_SHIFT      (4uL)

/***************************************************************************
  Constant: LOC_PARAM_SEC_~

  Definitions for parameter sector field.
  
  LOC_PARAM_SEC_MASK   - Mask of parameter sector field.
  LOC_PARAM_SEC_TOP    - Parameter sector value of Top.
  LOC_PARAM_SEC_BOTTOM - Parameter sector value of Bottom.
  LOC_PARAM_SEC_UNUSED - Parameter sector value of Unused.
  
*/
#define LOC_PARAM_SEC_MASK          (0x0300uL)
#define LOC_PARAM_SEC_TOP           (0x0000uL)
#define LOC_PARAM_SEC_BOTTOM        (0x0100uL)
#define LOC_PARAM_SEC_UNUSED        (0x0300uL)

/***************************************************************************
  Constant: LOC_STATUS_~

  Definitions for status register.
  
  LOC_STATUS_DEVICE_READY  - Bit of device ready.
  LOC_STATUS_ERASE_ERROR   - Bit of erase error.
  LOC_STATUS_PROGRAM_ERROR - Bit of program error.
  LOC_STATUS_LOCK_ERR      - Bit of lock error.
  
*/
#define LOC_STATUS_DEVICE_READY     (0x0080uL)
#define LOC_STATUS_ERASE_ERROR      (0x0020uL)
#define LOC_STATUS_PROGRAM_ERROR    (0x0010uL)
#define LOC_STATUS_LOCK_ERR         (0x0002uL)

/***************************************************************************
  Section: HYPB driver support functions

  Comments see: <r_hypb_sys.h>
*/

/***************************************************************************
  Function: R_HYPB_Sys_Init

  see: <r_hypb_sys.h>
*/
r_hypb_Error_t R_HYPB_Sys_Init(const uint32_t Unit)
{
    r_hypb_Error_t      sys_err = R_HYPB_ERR_OK;
    r_dev_Device_t      device;
    
    /* Check device version */
    device = R_DEV_GetDev();
    if ((device != R_DEV_R7F701441) /* RH850/D1M1A */
     && (device != R_DEV_R7F701442) /* RH850/D1M1-V2 */
     && (device != R_DEV_R7F701418))
    {
        sys_err = R_HYPB_ERR_DEVICE;
    }
    
    return sys_err;
}


/***************************************************************************
  Function: R_HYPB_Sys_DeInit

  see: <r_hypb_sys.h>
*/

r_hypb_Error_t R_HYPB_Sys_DeInit(const uint32_t Unit)
{
    return R_HYPB_ERR_OK;
}

/***************************************************************************
  Function: R_HYPB_Sys_BaseAddrGet

  see: <r_hypb_sys.h>
*/
uint32_t R_HYPB_Sys_BaseAddrGet(const uint32_t Unit)
{
    uint32_t addr = 0;

    if (Unit == 0)
    {
        addr = R_HYPB_BASE_ADD;
    }
    return addr;
}

/***************************************************************************
  Function: R_HYPB_Sys_ConfigRegSet

  see: <r_hypb_sys.h>
*/
uint16_t R_HYPB_Sys_ConfigRegSet(const uint32_t                Unit, 
                                 const r_hypb_Config_t * const Config, 
                                 const uint16_t                RegVal)
{
    /* Sample for IS26KL512S and IS66WVH16M8BLL */
    
    uint16_t    out_reg_val;
    uint16_t    latency_code;
    uint16_t    param_sec;
    
    out_reg_val = RegVal;

    if (Config != R_NULL)
    {
        if (Config->DeviceType == R_HYPB_DEVICE_RAM)
        {
            /* Check miss read */
            if ((RegVal & LOC_READ_CFG_CHECK_RAM) != LOC_READ_CFG_CHECK_RAM)
            {
                out_reg_val = LOC_WRITE_CFG_DEFAULT_RAM;
            }
        }
        else
        {
            /* Check miss read */
            if ((RegVal & LOC_READ_CFG_CHECK_FLASH) != LOC_READ_CFG_CHECK_FLASH)
            {
                out_reg_val = LOC_WRITE_CFG_DEFAULT_FLASH;
            }
        }

        /* Set Latency Code */
        if (Config->Command != R_NULL)
        {
            latency_code = (uint16_t)((uint16_t)Config->Command->Latency.Code << LOC_LATENCY_CODE_SHIFT) 
                           & LOC_LATENCY_CODE_MASK;
            out_reg_val = (out_reg_val & ~LOC_LATENCY_CODE_MASK) | latency_code;
        }
        
        /* Set parameter sector */
        if (Config->DeviceType == R_HYPB_DEVICE_FLASH)
        {
            if ((Config->ParamSec.Size != 0) && (Config->ParamSec.Num != 0))
            {
                if (Config->ParamSec.Pos == R_HYPB_PARAM_SEC_TOP)
                {
                    param_sec = LOC_PARAM_SEC_TOP;
                }
                else
                {
                    param_sec = LOC_PARAM_SEC_BOTTOM;
                }
            }
            else
            {
                param_sec = LOC_PARAM_SEC_UNUSED;
            }
            out_reg_val = (out_reg_val & ~LOC_PARAM_SEC_MASK) | param_sec;
        }
    }
    return out_reg_val;
}


/***************************************************************************
  Function: R_HYPB_Sys_StatusRegCheck

  see: <r_hypb_sys.h>
*/
r_hypb_Error_t R_HYPB_Sys_StatusRegCheck(const uint32_t         Unit,
                                         const r_hypb_CmdType_t CmdType,
                                         const uint16_t         RegVal,
                                         const uint32_t         Count)
{
    /* Sample for IS26KL512S */
    
    r_hypb_Error_t sys_err = R_HYPB_ERR_OK;
    
    /* Check time-out */
    if (Count >= R_HYPB_STATUS_TIMEOUT)
    {
        sys_err = R_HYPB_ERR_TIMEOUT;
    }
    else
    {
        /* 0xFFFF is sometimes returned at first if Read status command is executed during programing. */
        /* Check device ready  0:Busy 1:Ready */
        if ((RegVal == 0xFFFF) || ((RegVal & LOC_STATUS_DEVICE_READY) == 0))
        {
            sys_err = R_HYPB_ERR_NOT_ACCEPTABLE;
        }
        else
        {
            if (CmdType == R_HYPB_CMD_TYPE_ERASE)
            {
                /* Check Error  0:Normal 1:Error */
                if ((RegVal & LOC_STATUS_ERASE_ERROR) != 0)
                {
                    /* Check Lock status 0:Normal 1:Lock Error */
                    if ((RegVal & LOC_STATUS_LOCK_ERR) != 0)
                    {
                        sys_err = R_HYPB_ERR_PROTECTED;
                    }
                    else
                    {
                        sys_err = R_HYPB_ERR_ABORTED;
                    }
                }
            }
            else if ((CmdType == R_HYPB_CMD_TYPE_WRITE) ||
                     (CmdType == R_HYPB_CMD_TYPE_WRITE_BUF_FLASH))
            {
                /* Check Error  0:Normal 1:Error */
                if ((RegVal & LOC_STATUS_PROGRAM_ERROR) != 0)
                {
                    /* Check Lock status  0:Normal 1:Lock Error */
                    if ((RegVal & LOC_STATUS_LOCK_ERR) != 0)
                    {
                        sys_err = R_HYPB_ERR_PROTECTED;
                    }
                    else
                    {
                        sys_err = R_HYPB_ERR_ABORTED;
                    }
                }
            }
            else
            {
                /* Do nothing */
            }
        }
    }
    return sys_err;
}


/***************************************************************************
  Function: R_HYPB_Sys_SyncPeripheral

  see: <r_hypb_sys.h>
*/
void R_HYPB_Sys_SyncPeripheral(void)
{
    R_DEV_SyncP();
}

/***************************************************************************
  Function: R_HYPB_Sys_Lock

  see: <r_hypb_sys.h>
*/
r_hypb_Error_t R_HYPB_Sys_Lock(const uint32_t Unit)
{
    return R_HYPB_ERR_OK;
}

/***************************************************************************
  Function: R_HYPB_Sys_Unlock

  see: <r_hypb_sys.h>
*/
r_hypb_Error_t R_HYPB_Sys_Unlock(const uint32_t Unit)
{
    return R_HYPB_ERR_OK;
}

/***************************************************************************
  Function: R_HYPB_Sys_Relax

  see: <r_hypb_sys.h>
*/
r_hypb_Error_t R_HYPB_Sys_Relax(const uint32_t Unit)
{
    return R_HYPB_ERR_OK;
}