nand.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * (C) Copyright 2006
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de
  4. *
  5. * (C) Copyright 2006
  6. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #if defined(CONFIG_CMD_NAND)
  12. #include <asm/processor.h>
  13. #include <nand.h>
  14. struct alpr_ndfc_regs {
  15. u8 cmd[4];
  16. u8 addr_wait;
  17. u8 term;
  18. u8 dummy;
  19. u8 dummy2;
  20. u8 data;
  21. };
  22. static u8 hwctl;
  23. static struct alpr_ndfc_regs *alpr_ndfc = NULL;
  24. #define readb(addr) (u8)(*(volatile u8 *)(addr))
  25. #define writeb(d,addr) *(volatile u8 *)(addr) = ((u8)(d))
  26. /*
  27. * The ALPR has a NAND Flash Controller (NDFC) that handles all accesses to
  28. * the NAND devices. The NDFC has command, address and data registers that
  29. * when accessed will set up the NAND flash pins appropriately. We'll use the
  30. * hwcontrol function to save the configuration in a global variable.
  31. * We can then use this information in the read and write functions to
  32. * determine which NDFC register to access.
  33. *
  34. * There are 2 NAND devices on the board, a Hynix HY27US08561A (1 GByte).
  35. */
  36. static void alpr_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  37. {
  38. struct nand_chip *this = mtd->priv;
  39. if (ctrl & NAND_CTRL_CHANGE) {
  40. if ( ctrl & NAND_CLE )
  41. hwctl |= 0x1;
  42. else
  43. hwctl &= ~0x1;
  44. if ( ctrl & NAND_ALE )
  45. hwctl |= 0x2;
  46. else
  47. hwctl &= ~0x2;
  48. if ( (ctrl & NAND_NCE) != NAND_NCE)
  49. writeb(0x00, &(alpr_ndfc->term));
  50. }
  51. if (cmd != NAND_CMD_NONE)
  52. writeb(cmd, this->IO_ADDR_W);
  53. }
  54. static u_char alpr_nand_read_byte(struct mtd_info *mtd)
  55. {
  56. return readb(&(alpr_ndfc->data));
  57. }
  58. static void alpr_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  59. {
  60. struct nand_chip *nand = mtd->priv;
  61. int i;
  62. for (i = 0; i < len; i++) {
  63. if (hwctl & 0x1)
  64. /*
  65. * IO_ADDR_W used as CMD[i] reg to support multiple NAND
  66. * chips.
  67. */
  68. writeb(buf[i], nand->IO_ADDR_W);
  69. else if (hwctl & 0x2)
  70. writeb(buf[i], &(alpr_ndfc->addr_wait));
  71. else
  72. writeb(buf[i], &(alpr_ndfc->data));
  73. }
  74. }
  75. static void alpr_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  76. {
  77. int i;
  78. for (i = 0; i < len; i++) {
  79. buf[i] = readb(&(alpr_ndfc->data));
  80. }
  81. }
  82. static int alpr_nand_dev_ready(struct mtd_info *mtd)
  83. {
  84. /*
  85. * Blocking read to wait for NAND to be ready
  86. */
  87. (void)readb(&(alpr_ndfc->addr_wait));
  88. /*
  89. * Return always true
  90. */
  91. return 1;
  92. }
  93. int board_nand_init(struct nand_chip *nand)
  94. {
  95. alpr_ndfc = (struct alpr_ndfc_regs *)CONFIG_SYS_NAND_BASE;
  96. nand->ecc.mode = NAND_ECC_SOFT;
  97. /* Reference hardware control function */
  98. nand->cmd_ctrl = alpr_nand_hwcontrol;
  99. nand->read_byte = alpr_nand_read_byte;
  100. nand->write_buf = alpr_nand_write_buf;
  101. nand->read_buf = alpr_nand_read_buf;
  102. nand->dev_ready = alpr_nand_dev_ready;
  103. return 0;
  104. }
  105. #endif