ndfc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Overview:
  3. * Platform independend driver for NDFC (NanD Flash Controller)
  4. * integrated into IBM/AMCC PPC4xx cores
  5. *
  6. * (C) Copyright 2006-2009
  7. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  8. *
  9. * Based on original work by
  10. * Thomas Gleixner
  11. * Copyright 2006 IBM
  12. *
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include <common.h>
  16. #include <nand.h>
  17. #include <linux/mtd/ndfc.h>
  18. #include <linux/mtd/nand_ecc.h>
  19. #include <asm/processor.h>
  20. #include <asm/io.h>
  21. #include <asm/ppc4xx.h>
  22. #ifndef CONFIG_SYS_NAND_BCR
  23. #define CONFIG_SYS_NAND_BCR 0x80002222
  24. #endif
  25. #ifndef CONFIG_SYS_NDFC_EBC0_CFG
  26. #define CONFIG_SYS_NDFC_EBC0_CFG 0xb8400000
  27. #endif
  28. /*
  29. * We need to store the info, which chip-select (CS) is used for the
  30. * chip number. For example on Sequoia NAND chip #0 uses
  31. * CS #3.
  32. */
  33. static int ndfc_cs[NDFC_MAX_BANKS];
  34. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  35. {
  36. struct nand_chip *this = mtd->priv;
  37. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  38. if (cmd == NAND_CMD_NONE)
  39. return;
  40. if (ctrl & NAND_CLE)
  41. out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
  42. else
  43. out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
  44. }
  45. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  46. {
  47. struct nand_chip *this = mtdinfo->priv;
  48. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  49. return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
  50. }
  51. static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
  52. {
  53. struct nand_chip *this = mtdinfo->priv;
  54. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  55. u32 ccr;
  56. ccr = in_be32((u32 *)(base + NDFC_CCR));
  57. ccr |= NDFC_CCR_RESET_ECC;
  58. out_be32((u32 *)(base + NDFC_CCR), ccr);
  59. }
  60. static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
  61. const u_char *dat, u_char *ecc_code)
  62. {
  63. struct nand_chip *this = mtdinfo->priv;
  64. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  65. u32 ecc;
  66. u8 *p = (u8 *)&ecc;
  67. ecc = in_be32((u32 *)(base + NDFC_ECC));
  68. /* The NDFC uses Smart Media (SMC) bytes order
  69. */
  70. ecc_code[0] = p[1];
  71. ecc_code[1] = p[2];
  72. ecc_code[2] = p[3];
  73. return 0;
  74. }
  75. /*
  76. * Speedups for buffer read/write/verify
  77. *
  78. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  79. * functions. No further checking, as nand_base will always read/write
  80. * page aligned.
  81. */
  82. static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  83. {
  84. struct nand_chip *this = mtdinfo->priv;
  85. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  86. uint32_t *p = (uint32_t *) buf;
  87. for (;len > 0; len -= 4)
  88. *p++ = in_be32((u32 *)(base + NDFC_DATA));
  89. }
  90. /*
  91. * Don't use these speedup functions in NAND boot image, since the image
  92. * has to fit into 4kByte.
  93. */
  94. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  95. {
  96. struct nand_chip *this = mtdinfo->priv;
  97. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  98. uint32_t *p = (uint32_t *) buf;
  99. for (; len > 0; len -= 4)
  100. out_be32((u32 *)(base + NDFC_DATA), *p++);
  101. }
  102. /*
  103. * Read a byte from the NDFC.
  104. */
  105. static uint8_t ndfc_read_byte(struct mtd_info *mtd)
  106. {
  107. struct nand_chip *chip = mtd->priv;
  108. #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
  109. return (uint8_t) readw(chip->IO_ADDR_R);
  110. #else
  111. return readb(chip->IO_ADDR_R);
  112. #endif
  113. }
  114. void board_nand_select_device(struct nand_chip *nand, int chip)
  115. {
  116. /*
  117. * Don't use "chip" to address the NAND device,
  118. * generate the cs from the address where it is encoded.
  119. */
  120. ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
  121. int cs = ndfc_cs[chip];
  122. /* Set NandFlash Core Configuration Register */
  123. /* 1 col x 2 rows */
  124. out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
  125. out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), CONFIG_SYS_NAND_BCR);
  126. }
  127. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  128. {
  129. /*
  130. * Nothing to do here!
  131. */
  132. }
  133. int board_nand_init(struct nand_chip *nand)
  134. {
  135. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  136. ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
  137. static int chip = 0;
  138. /*
  139. * Save chip-select for this chip #
  140. */
  141. ndfc_cs[chip] = cs;
  142. /*
  143. * Select required NAND chip in NDFC
  144. */
  145. board_nand_select_device(nand, chip);
  146. nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
  147. nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
  148. nand->cmd_ctrl = ndfc_hwcontrol;
  149. nand->chip_delay = 50;
  150. nand->read_buf = ndfc_read_buf;
  151. nand->dev_ready = ndfc_dev_ready;
  152. nand->ecc.correct = nand_correct_data;
  153. nand->ecc.hwctl = ndfc_enable_hwecc;
  154. nand->ecc.calculate = ndfc_calculate_ecc;
  155. nand->ecc.mode = NAND_ECC_HW;
  156. nand->ecc.size = 256;
  157. nand->ecc.bytes = 3;
  158. nand->ecc.strength = 1;
  159. nand->select_chip = ndfc_select_chip;
  160. #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
  161. nand->options |= NAND_BUSWIDTH_16;
  162. #endif
  163. nand->write_buf = ndfc_write_buf;
  164. nand->read_byte = ndfc_read_byte;
  165. chip++;
  166. return 0;
  167. }