r_sys_octa.c 11.4 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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
/*
****************************************************************************
PROJECT : OCTA driver
FILE    : $Id: r_sys_octa.c 15447 2017-12-20 00:25:00Z shinobu.uehara $
============================================================================
DESCRIPTION
OCTA support functions for d1x
============================================================================
                            C O P Y R I G H T
============================================================================
                           Copyright (c) 2017
                                  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: OCTA Driver support functions

  Implementation of the OCTA support functions for the D1x device.

*/

/***************************************************************************
  Section: Includes
*/
#include "r_typedefs.h"
#include "r_octa_api.h"
#include "r_octa_sys.h"
#include "r_config_octa.h"
#include "r_dev_api.h"


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

/***************************************************************************
  Constant: LOC_EXTERNAL_BASE_~

  Definitions to the base address of external address space.
  
  LOC_EXTERNAL_BASE_ADDR0   - Base address for Channel0.
  LOC_EXTERNAL_BASE_ADDR1   - Base address for Channel1.
*/
#define LOC_EXTERNAL_BASE_ADDR0      (0x60000000uL)
#define LOC_EXTERNAL_BASE_ADDR1      (0x70000000uL)

/***************************************************************************
  Constant: LOC_DEFAULT_DL_~

  Definitions to the default Dummy Length.
  
  LOC_DEFAULT_DL_FLASH   - Default Dummy Length for Flash memory.
  LOC_DEFAULT_DL_RAM     - Default Dummy Length for RAM.
*/
#define LOC_DEFAULT_DL_FLASH    (20uL)
#define LOC_DEFAULT_DL_RAM      (5uL)

/***************************************************************************
  Constant: LOC_BANK_SIZE

  Definitions to the bank size.
  
  LOC_BANK_SIZE   - bank size for Flash memory.
*/
#define LOC_BANK_SIZE    (0x1000000uL)

/*******************************************************************************
  Section: Local functions
*/
static uint32_t loc_OCTA_CmdBitPosShiftGet(const uint16_t    Pos);

/***************************************************************************
  Function: loc_OCTA_CmdBitPosShiftGet

  Description:
  This function gives back the shift value of the specified bit position.
  The shift value gives priority to the lowest bit position.
  
  Parameter:
    pos      - Bit position.

  Returns:
    shift value.
*/
static uint32_t loc_OCTA_CmdBitPosShiftGet(const uint16_t    Pos)
{
    uint32_t    shift = 0;
    uint32_t    val;

    for (val = Pos; val > 0; val >>= 1)
    {
        if ((val & 1uL) == 0)
        {
            shift++;
        }
    }
    return shift;
}

/***************************************************************************
  Section: OCTA driver support functions

  Comments see: <r_octa_sys.h>
*/

/***************************************************************************
  Function: R_OCTA_Sys_Init

  see: <r_octa_sys.h>
*/
r_octa_Error_t R_OCTA_Sys_Init(const uint32_t Unit)
{
    r_octa_Error_t      sys_err = R_OCTA_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_OCTA_ERR_DEVICE;
    }
    
    return sys_err;
}


/***************************************************************************
  Function: R_OCTA_Sys_DeInit

  see: <r_octa_sys.h>
*/

r_octa_Error_t R_OCTA_Sys_DeInit(const uint32_t Unit)
{
    return R_OCTA_ERR_OK;
}

/***************************************************************************
  Function: R_OCTA_Sys_BaseAddrGet

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

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

/***************************************************************************
  Function: R_OCTA_Sys_ExternalAddrSpaceBaseAddrGet

  see: <r_octa_sys.h>
*/
uint32_t R_OCTA_Sys_ExternalAddrSpaceBaseAddrGet(const uint32_t Unit, const uint32_t Channel)
{
    uint32_t addr = 0;

    if (Unit == 0)
    {
        if (Channel == 0)
        {
            addr = LOC_EXTERNAL_BASE_ADDR0;
        }
        else
        {
            addr = LOC_EXTERNAL_BASE_ADDR1;
        }
    }
    return addr;
}

/***************************************************************************
  Function: R_OCTA_Sys_DefaultDummyLengthGet

  see: <r_octa_sys.h>
*/
uint32_t R_OCTA_Sys_DefaultDummyLengthGet(const uint32_t Unit, const uint32_t Channel,
                                          const uint8_t DeviceType)
{
    uint32_t df_dummylength;

    if (DeviceType == 0)
    {
        df_dummylength = LOC_DEFAULT_DL_FLASH; /* OCTA FLASH */
    }
    else
    {
        df_dummylength = LOC_DEFAULT_DL_RAM; /* OCTA RAM */
    }

    return df_dummylength;
}

/***************************************************************************
  Function: R_OCTA_Sys_BankSizeGet

  see: <r_octa_sys.h>
*/
uint32_t R_OCTA_Sys_BankSizeGet(void)
{
    return LOC_BANK_SIZE;
}

/***************************************************************************
  Function: R_OCTA_Sys_DummyDataRead

  see: <r_octa_sys.h>
*/
void R_OCTA_Sys_DummyDataRead(const uint32_t    Base,
                              const uint32_t    Addr,
                              const uint32_t    BankSize)
{
    uint32_t        dread = 0;
    uint32_t        bank_adr;

    /* Synchronize the Write process */
    /* Dummy Read */
    bank_adr = (Addr & ~(BankSize - 1));
    if (bank_adr == 0)
    {
        dread = BankSize;
    }

    R_DEV_READ_REG(32, (Base + dread));
}

/***************************************************************************
  Function: R_OCTA_Sys_ConfigRegSet

  see: <r_octa_sys.h>
*/
uint16_t R_OCTA_Sys_ConfigRegSet(const uint32_t                  Unit,
                                 const uint32_t                  Channel,
                                 const r_octa_Config_t * const   Config,
                                 const uint16_t                  RegVal,
                                 const r_octa_StateType_t        Type)
{
    r_octa_Command_t*           cmd;
    uint32_t                    shift;
    uint16_t                    reg_val;

    cmd = Config->Command;
    reg_val = RegVal;

    if (Type == R_OCTA_TYPE_PCM)
    {
        /* PCM */
        if (Config->PreCycle == 0)
        {
            reg_val &= (uint16_t)~cmd->PreCycle.BitPosition;
        }
        else
        {
            reg_val |= cmd->PreCycle.BitPosition;
        }
    }
    else if (Type == R_OCTA_TYPE_DTM)
    {
        shift = loc_OCTA_CmdBitPosShiftGet(cmd->FlashTransferType.BitPosition);
        /* DTM */
        reg_val &= (uint16_t)~cmd->FlashTransferType.BitPosition;
        reg_val |= (uint16_t)((uint32_t)Config->DataTransferMode << shift);
    }
    else if (Type == R_OCTA_TYPE_DOS)
    {
        /* DOS */
        if (Config->DataTransferMode == R_OCTA_MODE_OPI)
        {
            reg_val |= cmd->DOS.BitPosition;
        }
        else
        {
            reg_val &= (uint16_t)~cmd->DOS.BitPosition;
        }
    }
    else if (Type == R_OCTA_TYPE_DL)
    {
        /* DL */
        reg_val &= (uint16_t)~cmd->DummyCycle.BitMask;
        reg_val |= cmd->DummyCycle.BitSet;
    }
    else
    {
        /* check end */
    }

    return reg_val;
}

/***************************************************************************
  Function: R_OCTA_Sys_StatusRegCheck

  see: <r_octa_sys.h>
*/
r_octa_Error_t R_OCTA_Sys_StatusRegCheck(const uint32_t           Unit, 
                                         const uint32_t           Channel,
                                         const uint16_t           RegPos,
                                         const uint16_t           RegVal,
                                         const r_octa_StateType_t Type,
                                         const uint32_t           Count)
{
    r_octa_Error_t      sys_err = R_OCTA_ERR_OK;
    
    /* Check Timeout */
    if (Count >= R_OCTA_STATUS_TIMEOUT)
    {
        sys_err = R_OCTA_ERR_TIMEOUT;
    }
    else
    {
        if (Type == R_OCTA_TYPE_WEL)
        {
            /** Read Status Register (repeat until SR[1]=1) **/
            if ((RegVal & RegPos) == 0)
            {
                sys_err = R_OCTA_ERR_NOT_ACCEPTABLE;
            }
            else
            {
                sys_err = R_OCTA_ERR_OK;
            }
        }
        else if (Type == R_OCTA_TYPE_WIP)
        {
            /** Read Status Register (repeat until SR[0]=0) **/
            if ((RegVal & RegPos) != 0)
            {
                sys_err = R_OCTA_ERR_NOT_ACCEPTABLE;
            }
            else
            {
                sys_err = R_OCTA_ERR_OK;
            }
        }
        else
        {
            /* Nothing */
        }
    }
    return sys_err;
}

/***************************************************************************
  Function: R_OCTA_Sys_StatusRegSet

  see: <r_octa_sys.h>
*/
uint16_t R_OCTA_Sys_StatusRegSet(const uint32_t Unit,
                                 const uint32_t Channel,
                                 const uint16_t RegPos,
                                 const uint16_t RegVal,
                                 const uint8_t  Mode)
{
    uint16_t                    reg_val;

    reg_val = RegVal;

    /* Set Block protection */
    if (Mode == R_OCTA_MODE_UNPROTECT)
    {
        reg_val &= (uint16_t)~RegPos;
    }
    else
    {
        reg_val |= RegPos;
    } 

    return reg_val;
}

/***************************************************************************
  Function: R_OCTA_Sys_SyncPeripheral

  see: <r_octa_sys.h>
*/
void R_OCTA_Sys_SyncPeripheral(void)
{
    R_DEV_SyncP();
}

/***************************************************************************
  Function: R_OCTA_Sys_Lock

  see: <r_octa_sys.h>
*/
r_octa_Error_t R_OCTA_Sys_Lock(const uint32_t Unit)
{
    return R_OCTA_ERR_OK;
}

/***************************************************************************
  Function: R_OCTA_Sys_Unlock

  see: <r_octa_sys.h>
*/
r_octa_Error_t R_OCTA_Sys_Unlock(const uint32_t Unit)
{
    return R_OCTA_ERR_OK;
}

/***************************************************************************
  Function: R_OCTA_Sys_Relax

  see: <r_octa_sys.h>
*/
r_octa_Error_t R_OCTA_Sys_Relax(const uint32_t Unit)
{
    return R_OCTA_ERR_OK;
}