r_nfma_bare.c 17 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
/*
****************************************************************************
PROJECT : NFMA driver
FILE    : $Id: r_nfma_bare.c 13286 2017-04-12 15:36:16Z tobyas.hennig $
============================================================================
DESCRIPTION
Driver for NFMA macro - main driver functions
============================================================================
                            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

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 (C) 2011 Renesas Electronics Corporation. All rights reserved.

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

#include "r_typedefs.h"
#include "r_dev_api.h"

//#include "r_bsp_api.h"
//#include "r_bsp_stdio_api.h"

#include "r_nfma_bare_api.h"
#include "r_nfma_bare_sys.h"
#include "r_nfma_bare_reg.h"


static const r_onfi_timing_mode_t timing_modes[ONFI_NUM_TIMING_MODES] =
{
/*  tADL tALH tALS tAR tCEA tCH tCHZ tCLH tCLR tCLS tCOH tCS tDH tDS tFEAT tIR tRC  tREA tREH tRHOH tRHW tRHZ tRLOH tRP  tRR tRST  tWB  tWC tWH tWHR  tWP tWW */
/*    ns   ns   ns  ns   ns  ns   ns  ns   ns   ns   ns  ns  ns  ns  us    ns  ns   ns   ns   ns    ns   ns   ns    ns   ns   us   ns   ns   ns   ns  ns   ns */
    {200, 20, 50,  25, 100, 20, 100, 20,  20,  50,   0,  70, 20, 40, 1,    10, 100, 40,  30,   0,   200, 200, 0,    50,  40, 1000, 200, 100, 30, 120, 50, 100},  /* ONFi1.0 mode 0 */
    {100, 10, 25,  10,  45, 10,  50, 10,  10,  25,  15,  35, 10, 20, 1,    0,  50,  30,  15,  15,   100, 100, 0,    25,  20,  500, 100,  45, 15,  80, 25, 100},  /* ONFi1.0 mode 1 (Fastest mode possible w/ RH850/D1M1A) */
    {100, 10, 15,  10,  30, 10,  50, 10,  10,  15,  15,  25,  5, 15, 1,    0,  35,  25,  15,  15,   100, 100, 0,    17,  20,  500, 100,  35, 15,  80, 17, 100},
    {100, 5,  10,  10,  25,  5,  50,  5,  10,  10,  15,  25,  5, 10, 1,    0,  30,  20,  10,  15,   100, 100, 0,    15,  20,  500, 100,  30, 10,  60, 15, 100},
    { 70, 5,  10,  10,  25,  5,  30,  5,  10,  10,  15,  20,  5, 10, 1,    0,  25,  20,  10,  15,   100, 100, 5,    12,  20,  500, 100,  25, 10,  60, 12, 100},
    { 70, 5,  10,  10,  25,  5,  30,  5,  10,  10,  15,  15,  5,  7, 1,    0,  20,  16,   7,  15,   100, 100, 5,    10,  20,  500, 100,  20,  7,  60, 10, 100}
};


/*******************************************************************************
  Section: Local Functions
*/
static void WaitNFMA_Flags(uint32_t instance, uint32_t statusreg, uint32_t flags);
static void WaitNFMA_Flags(uint32_t instance, uint32_t statusreg, uint32_t flags)
{
    uint32_t NFMAn_BASE;
  
    NFMAn_BASE = R_SYS_NFMA_GetBaseAddr(instance);
  
    while((flags & R_NFMA_READ_REG(32, NFMAn_BASE + statusreg)) != flags);
}

/* Convert a timing value in nanoseconds to counts of CLKB */
static uint32_t ConvNFMA_ClkB_NanoSec(uint32_t ns);
static uint32_t ConvNFMA_ClkB_NanoSec(uint32_t ns)
{
    uint32_t freq_clkb;
    
    freq_clkb = R_SYS_NFMA_GetClkFreqMHz(0);
    return ((freq_clkb * (ns))/1000);
}

/*******************************************************************************
  Section: Global Functions
*/

void R_NFMA_Init(uint32_t instance, r_nfma_param_t config)
{
  uint32_t NFMAn_BASE;
  uint32_t dataL;
  const r_onfi_timing_mode_t *mode = mode = &timing_modes[NFMA_DEFAULT_TIMING_MODE];

  NFMAn_BASE = R_SYS_NFMA_GetBaseAddr(instance);

  /* Clear and mask all interrupts */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_STATUS, 0x0);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_MASK, 0x0);

  dataL  = 0x1<<NFMA_RBFO_AUTO_READ_STAT_EN; /* Auto read status */
  dataL |= 0x0<<NFMA_RBFO_IO_WIDTH;          /* I/O width 8 bit */
  dataL |= 0x0<<NFMA_RBFO_BLOCK_SIZE;
  dataL |= 0x1<<NFMA_RBFO_INT_EN;
  dataL |= 0x1<<NFMA_RBFO_READ_STATUS_EN;
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_CONTROL, dataL);

  /* Is used when NFMA_RBFO_AUTO_READ_STAT_EN is used
     [15:8] is for the READ_STATUS data of the NandFlash (FAIL, FAILC bits)
      [7:0]  is for the READ_STATUS data of the NandFlash (RDY, ARDY bits) 
  */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_STATUS_MASK, 0x140);

  
  /* write MEM_CTRL */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_MEM_CTRL, 0x0|NFMA_RBF_MEM0_WP);

  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DATA_REG_SIZE, 0x3);

  dataL  = (ConvNFMA_ClkB_NanoSec(mode->tWHR) << NFMA_RBFO_TWHR);
  dataL |= (ConvNFMA_ClkB_NanoSec(mode->tRHW) << NFMA_RBFO_TRHW);
  dataL |= (ConvNFMA_ClkB_NanoSec(mode->tADL) << NFMA_RBFO_TADL);
  dataL |= (ConvNFMA_ClkB_NanoSec(ONFI_DEFAULT_TCCS_NS) << NFMA_RBFO_TCCS);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_TIME_SEQ_0, dataL);

  dataL  = (ConvNFMA_ClkB_NanoSec(mode->tWW) << NFMA_RBFO_TWW);
  dataL |= (ConvNFMA_ClkB_NanoSec(mode->tRR) << NFMA_RBFO_TRR);
  dataL |= (ConvNFMA_ClkB_NanoSec(mode->tWB) << NFMA_RBFO_TWB);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_TIME_SEQ_1, dataL);

  dataL  = (ConvNFMA_ClkB_NanoSec(mode->tREH) << NFMA_RBFO_ASYN_TRWH);
  dataL |= (ConvNFMA_ClkB_NanoSec(mode->tREA) << NFMA_RBFO_ASYN_TRWP);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_TIMINGS_ASYN, dataL);

NFMA_reset:
  /* STATUS.MEM_ST */
  WaitNFMA_Flags(instance, NFMA_STATUS, NFMA_RBF_MEM0_ST);

  /* FIFO_STATE.CF_EMPTY*/
  WaitNFMA_Flags(instance, NFMA_FIFO_STATE, NFMA_RBF_CF_EMPTY);

  /* RESET COMMAND 0xFF00 */
  dataL = 0xFF00;
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_COMMAND, dataL);

  /* STATUS.MEM_ST */
  WaitNFMA_Flags(instance, NFMA_STATUS, NFMA_RBF_MEM0_ST);

  dataL = R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_INT_STATUS);
  if (dataL & NFMA_RBF_TRANS_ERR_FL) {
    //R_BSP_STDIO_Printf("NFMA Reset Transfer Error");
    goto NFMA_reset;
  }
}


uint8_t R_NFMA_BlockErase(uint32_t instance, r_nfma_erase_param_t erase_config)
{
  uint32_t NFMAn_BASE;
  uint32_t dataL;
  uint32_t block;

  NFMAn_BASE = R_SYS_NFMA_GetBaseAddr(instance);

NFMA_erase:
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_STATUS, 0x0);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_MASK, 0x0);

  /* write ADDR0_ROW */
  block = (erase_config.AccessAddr * 4 / (NFMA_PAGE_SIZE_BYTE * NFMA_PAGES_PER_BLOCK));
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_ROW, ((block*NFMA_PAGES_PER_BLOCK) & NFMA_MASK_ROW_BLOCK_NO));
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_COL, (0x0 & NFMA_MASK_COL_PAGE_SIZE));  /* ADDR0_COL not used by PageErase (SEQ_14) */
  
  /* write MEM_CTRL */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_MEM_CTRL, 0x0);

  /* Erase BLOCK COMMAND 0x00D0600E */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_COMMAND, 0x00D0600E);

  /* STATUS.MEM_ST */
  WaitNFMA_Flags(instance, NFMA_STATUS, NFMA_RBF_MEM0_ST);

  dataL = R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_INT_STATUS);
  if (dataL & NFMA_RBF_TRANS_ERR_FL)
  {
    //R_BSP_STDIO_Printf("NFMA Erase Transfer Error");
    goto NFMA_erase;
  }

  if (dataL & NFMA_RBF_STAT_ERR_INT0_FL)
  {
    //R_BSP_STDIO_Printf("NFMA Erase Error");
    goto NFMA_erase;
  }
  return 0x0;
}

uint8_t R_NFMA_PageWrite(uint32_t instance, r_nfma_write_param_t write_config)
{
  uint32_t NFMAn_BASE;
  uint32_t dataL;
  uint32_t count;
  uint32_t page;

  NFMAn_BASE = R_SYS_NFMA_GetBaseAddr(instance);

  if( 1>write_config.WrCount ) return 0x1;

NFMA_write:
  /* FIFO flush */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_FIFO_INIT, 0x1);
  WaitNFMA_Flags(instance, NFMA_FIFO_STATE, NFMA_RBF_DF_W_EMPTY);

  /* Clear and mask all interrupts */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_STATUS, 0x0);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_MASK, 0x0);

  /* write ADDR0_COL , write ADDR0_ROW, write DATA_SIZE */
  page = (write_config.AccessAddr * 4 / NFMA_PAGE_SIZE_BYTE);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_ROW, (page & NFMA_MASK_ROW_PAGE_NO));
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_COL, (write_config.AccessAddr & NFMA_MASK_COL_PAGE_SIZE));

  /* Write the Number of Required Bytes */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DATA_SIZE, (write_config.WrCount * 4));
  
  
  /* write MEM_CTRL */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_MEM_CTRL, 0x0);

  /* Program PAGE COMMAND 0x0010800c */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_COMMAND, 0x0010800c);

  /* FIFO_STATE.CF_EMPTY*/
  WaitNFMA_Flags(instance, NFMA_FIFO_STATE, NFMA_RBF_CF_EMPTY);

  /* Write Data to FIFO */
  for (count = 0; count < write_config.WrCount; count++)
  {
    /* TODO: Add a timeout to this loop */
    while ((R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_FIFO_STATE) & NFMA_RBF_DF_W_FULL) != 0)
    {
        /* Nothing */
    }
    R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_FIFO_DATA, *(write_config.WrData+count));
  }

  /* STATUS.MEM_ST */
  WaitNFMA_Flags(instance, NFMA_STATUS, NFMA_RBF_MEM0_ST);

  dataL = R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_INT_STATUS);
  if (dataL & NFMA_RBF_TRANS_ERR_FL)
  {
    //R_BSP_STDIO_Printf("NFMA Write Transfer Error");
    goto NFMA_write;
  }

  if (dataL & NFMA_RBF_STAT_ERR_INT0_FL)
  {
    //R_BSP_STDIO_Printf("NFMA Write Error");
    goto NFMA_write;
  }

  return 0x0;
}

uint8_t R_NFMA_PageRead(uint32_t instance, r_nfma_read_param_t read_config)
{
  uint32_t NFMAn_BASE;
  uint32_t dataL;
  uint32_t count;
  uint32_t page;

  NFMAn_BASE = R_SYS_NFMA_GetBaseAddr(instance);

  if( 1>read_config.RdCount ) return 0x1;

NFMA_read:
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_FIFO_INIT, 0x1);
  WaitNFMA_Flags(instance, NFMA_FIFO_STATE, NFMA_RBF_DF_W_EMPTY);

  /* Clear and mask all interrupts */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_STATUS, 0x0);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_MASK, 0x0);

  /* write ADDR0_COL , write ADDR0_ROW */
  page = (read_config.AccessAddr * 4 / NFMA_PAGE_SIZE_BYTE);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_ROW, (page & NFMA_MASK_ROW_PAGE_NO));
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_COL, (read_config.AccessAddr & NFMA_MASK_COL_PAGE_SIZE));

  /* Read the Number of Required Bytes */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DATA_SIZE, (read_config.RdCount * 4));

  
  /* write MEM_CTRL */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_MEM_CTRL, 0x0|(0x1<<NFMA_RBFO_MEM0_WP));

  /* Read PAGE COMMAND 0x3000002A */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_COMMAND, 0x3000002A);

  /* FIFO_STATE.CF_EMPTY, FIFO_STATE.DF_R_EMPTY*/
  WaitNFMA_Flags(instance, NFMA_FIFO_STATE, NFMA_RBF_CF_EMPTY);

  /* STATUS.MEM_ST */
  WaitNFMA_Flags(instance, NFMA_STATUS, NFMA_RBF_MEM0_ST);

  dataL = R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_INT_STATUS);
  if (dataL & NFMA_RBF_TRANS_ERR_FL)
  {
    //R_BSP_STDIO_Printf("NFMA Read Transfer Error");
    goto NFMA_read;
  }

  /* Read Data from FIFO */
  for (count = 0; count < read_config.RdCount; count++)
  {
    while ((R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_FIFO_STATE) & NFMA_RBF_DF_R_EMPTY) != 0)
    {

    }
    dataL = R_NFMA_READ_REG(32, NFMAn_BASE + NFMA_FIFO_DATA);
    *(read_config.RdData+count) = dataL;
  }

  return 0x0;
}


uint8_t R_NFMA_PageRead_Dma(uint32_t instance, r_nfma_read_param_t read_config)
{
  uint32_t NFMAn_BASE;
  uint32_t page;

  NFMAn_BASE = R_SYS_NFMA_GetBaseAddr(instance);

  if( 1>read_config.RdCount ) return 0x1;

  /* Clear and mask all interrupts */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_STATUS, 0x0);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_INT_MASK, 0x0);

  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_FIFO_INIT, 0x1);
  WaitNFMA_Flags(instance, NFMA_FIFO_STATE, NFMA_RBF_DF_W_EMPTY);

  /* write ADDR0_COL , write ADDR0_ROW */
  page = (read_config.AccessAddr * 4 / NFMA_PAGE_SIZE_BYTE);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_ROW, (page & NFMA_MASK_ROW_PAGE_NO));
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_ADDR0_COL, (read_config.AccessAddr & NFMA_MASK_COL_PAGE_SIZE));

  /* Write the Number of Required Bytes */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DATA_SIZE, (read_config.RdCount * 4));

  /* write MEM_CTRL */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_MEM_CTRL, 0x0|(0x1<<NFMA_RBFO_MEM0_WP));

  /* setup DMA */
  /* Write the address of the first descriptor to the DMA_ADDR register.
     And the size of data to be transfered to the DMA_CNT register. */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DMA_ADDR_H, 0x0);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DMA_ADDR_L, (uint32_t)read_config.RdData);
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DMA_CNT,    (read_config.RdCount * 4));
  /* Set DMA_START bit to start DMA when the command sequence
     is sent to the NAND Flash memory.
     mode: register managed mode / single transfer(addr inc) */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_DMA_CTRL,   0x88);

  /* Read PAGE COMMAND 0x3000002A, but w/ DMA -> 0x3000006A */
  R_NFMA_WRITE_REG(32, NFMAn_BASE + NFMA_COMMAND, 0x3000006A);

  /* When the controller is ready for further work,
     the appropriate DMA_INT_FL bit is set and the interrupt is activated. */
  WaitNFMA_Flags(instance, NFMA_INT_STATUS, NFMA_INTSTAT_DMA_INT_FL);

  return 0x0;
}



uint8_t R_NFMA_Read(uint32_t instance, r_nfma_read_param_t read_config)
{
    r_nfma_read_param_t page_read_config;
    uint32_t byteCount = 0;
    uint32_t err = 0;
    
    /* Error check: Access Address to be aligned to one Page */
    if ((read_config.AccessAddr % NFMA_PAGE_SIZE_BYTE) != 0x0)
    {
        /* Access Address is not aligned to one Page */
        err = 1;
    }    
    
    /* Slit read data request into page read requests
        and process page read requests one after each other. */
    while (byteCount < read_config.RdCount)
    {
        page_read_config.AccessAddr = (read_config.AccessAddr + byteCount/4);
        page_read_config.RdData     = (read_config.RdData + byteCount/4);
        if ((read_config.RdCount - byteCount) > (NFMA_PAGE_SIZE_BYTE))
        {
            page_read_config.RdCount    = (NFMA_PAGE_SIZE_BYTE/4);
        } else {
            page_read_config.RdCount    = ((read_config.RdCount - byteCount)/4);
        }
        
        err = R_NFMA_PageRead(instance, page_read_config);
        if (0 != err)
        {
            return err;
        }
        
        byteCount += (page_read_config.RdCount * 4);
    }
    
    /* Error check: Number of read data */
    page_read_config.RdData     = (read_config.RdData + byteCount/4);
    if (page_read_config.RdData != (read_config.RdData + read_config.RdCount/4))
    {
        /* Size of read data does not match to read data size request.  */
        err = 1;
    }
    
    return err;
}


uint8_t R_NFMA_Read_Dma(uint32_t instance, r_nfma_read_param_t read_config)
{
    r_nfma_read_param_t page_read_config;
    uint32_t byteCount = 0;
    uint32_t err = 0;
    
    /* Error check: Access Address to be aligned to one Page */
    if ((read_config.AccessAddr % NFMA_PAGE_SIZE_BYTE) != 0x0)
    {
        /* Access Address is not aligned to one Page */
        err = 1;
    }    
    
    /* Slit read data request into page read requests
        and process page read requests one after each other. */
    while (byteCount < read_config.RdCount)
    {
        page_read_config.AccessAddr = (read_config.AccessAddr + byteCount/4);
        page_read_config.RdData     = (read_config.RdData + byteCount/4);
        if ((read_config.RdCount - byteCount) > (NFMA_PAGE_SIZE_BYTE))
        {
            page_read_config.RdCount    = (NFMA_PAGE_SIZE_BYTE/4);
        } else {
            page_read_config.RdCount    = ((read_config.RdCount - byteCount)/4);
        }
        
        err = R_NFMA_PageRead_Dma(instance, page_read_config);
        if (0 != err)
        {
            return err;
        }
        
        byteCount += (page_read_config.RdCount * 4);
    }
    
    /* Error check: Number of read data */
    page_read_config.RdData     = (read_config.RdData + byteCount/4);
    if (page_read_config.RdData != (read_config.RdData + read_config.RdCount/4))
    {
        /* Size of read data does not match to read data size request.  */
        err = 1;
    }
    
    return err;
}