nand_boot_fsl_elbc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * NAND boot for Freescale Enhanced Local Bus Controller, Flash Control Machine
  3. *
  4. * (C) Copyright 2006-2008
  5. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  6. *
  7. * Copyright (c) 2008 Freescale Semiconductor, Inc.
  8. * Author: Scott Wood <scottwood@freescale.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #include <asm/fsl_lbc.h>
  28. #include <linux/mtd/nand.h>
  29. #define WINDOW_SIZE 8192
  30. static void nand_wait(void)
  31. {
  32. fsl_lbc_t *regs = LBC_BASE_ADDR;
  33. for (;;) {
  34. uint32_t status = in_be32(&regs->ltesr);
  35. if (status == 1)
  36. return;
  37. if (status & 1) {
  38. puts("read failed (ltesr)\n");
  39. for (;;);
  40. }
  41. }
  42. }
  43. static void nand_load(unsigned int offs, int uboot_size, uchar *dst)
  44. {
  45. fsl_lbc_t *regs = LBC_BASE_ADDR;
  46. uchar *buf = (uchar *)CONFIG_SYS_NAND_BASE;
  47. const int large = CONFIG_SYS_NAND_OR_PRELIM & OR_FCM_PGS;
  48. const int block_shift = large ? 17 : 14;
  49. const int block_size = 1 << block_shift;
  50. const int page_size = large ? 2048 : 512;
  51. const int bad_marker = large ? page_size + 0 : page_size + 5;
  52. int fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT) | 2;
  53. int pos = 0;
  54. if (offs & (block_size - 1)) {
  55. puts("bad offset\n");
  56. for (;;);
  57. }
  58. if (large) {
  59. fmr |= FMR_ECCM;
  60. __raw_writel((NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
  61. (NAND_CMD_READSTART << FCR_CMD1_SHIFT),
  62. &regs->fcr);
  63. __raw_writel(
  64. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  65. (FIR_OP_CA << FIR_OP1_SHIFT) |
  66. (FIR_OP_PA << FIR_OP2_SHIFT) |
  67. (FIR_OP_CW1 << FIR_OP3_SHIFT) |
  68. (FIR_OP_RBW << FIR_OP4_SHIFT),
  69. &regs->fir);
  70. } else {
  71. __raw_writel(NAND_CMD_READ0 << FCR_CMD0_SHIFT, &regs->fcr);
  72. __raw_writel(
  73. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  74. (FIR_OP_CA << FIR_OP1_SHIFT) |
  75. (FIR_OP_PA << FIR_OP2_SHIFT) |
  76. (FIR_OP_RBW << FIR_OP3_SHIFT),
  77. &regs->fir);
  78. }
  79. __raw_writel(0, &regs->fbcr);
  80. while (pos < uboot_size) {
  81. int i = 0;
  82. __raw_writel(offs >> block_shift, &regs->fbar);
  83. do {
  84. int j;
  85. unsigned int page_offs = (offs & (block_size - 1)) << 1;
  86. __raw_writel(~0, &regs->ltesr);
  87. __raw_writel(0, &regs->lteatr);
  88. __raw_writel(page_offs, &regs->fpar);
  89. __raw_writel(fmr, &regs->fmr);
  90. sync();
  91. __raw_writel(0, &regs->lsor);
  92. nand_wait();
  93. page_offs %= WINDOW_SIZE;
  94. /*
  95. * If either of the first two pages are marked bad,
  96. * continue to the next block.
  97. */
  98. if (i++ < 2 && buf[page_offs + bad_marker] != 0xff) {
  99. puts("skipping\n");
  100. offs = (offs + block_size) & ~(block_size - 1);
  101. pos &= ~(block_size - 1);
  102. break;
  103. }
  104. for (j = 0; j < page_size; j++)
  105. dst[pos + j] = buf[page_offs + j];
  106. pos += page_size;
  107. offs += page_size;
  108. } while ((offs & (block_size - 1)) && (pos < uboot_size));
  109. }
  110. }
  111. /*
  112. * The main entry for NAND booting. It's necessary that SDRAM is already
  113. * configured and available since this code loads the main U-Boot image
  114. * from NAND into SDRAM and starts it from there.
  115. */
  116. void nand_boot(void)
  117. {
  118. __attribute__((noreturn)) void (*uboot)(void);
  119. /*
  120. * Load U-Boot image from NAND into RAM
  121. */
  122. nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
  123. (uchar *)CONFIG_SYS_NAND_U_BOOT_DST);
  124. /*
  125. * Jump to U-Boot image
  126. */
  127. puts("transfering control\n");
  128. /*
  129. * Clean d-cache and invalidate i-cache, to
  130. * make sure that no stale data is executed.
  131. */
  132. flush_cache(CONFIG_SYS_NAND_U_BOOT_DST, CONFIG_SYS_NAND_U_BOOT_SIZE);
  133. uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
  134. uboot();
  135. }