nand.c 3.0 KB

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