nandflash.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * arch/cris/arch-v32/drivers/nandflash.c
  3. *
  4. * Copyright (c) 2004
  5. *
  6. * Derived from drivers/mtd/nand/spia.c
  7. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/nand.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <arch/memmap.h>
  21. #include <hwregs/reg_map.h>
  22. #include <hwregs/reg_rdwr.h>
  23. #include <hwregs/gio_defs.h>
  24. #include <hwregs/bif_core_defs.h>
  25. #include <asm/io.h>
  26. #define CE_BIT 4
  27. #define CLE_BIT 5
  28. #define ALE_BIT 6
  29. #define BY_BIT 7
  30. struct mtd_info_wrapper {
  31. struct nand_chip chip;
  32. };
  33. /* Bitmask for control pins */
  34. #define PIN_BITMASK ((1 << CE_BIT) | (1 << CLE_BIT) | (1 << ALE_BIT))
  35. /* Bitmask for mtd nand control bits */
  36. #define CTRL_BITMASK (NAND_NCE | NAND_CLE | NAND_ALE)
  37. static struct mtd_info *crisv32_mtd;
  38. /*
  39. * hardware specific access to control-lines
  40. */
  41. static void crisv32_hwcontrol(struct mtd_info *mtd, int cmd,
  42. unsigned int ctrl)
  43. {
  44. unsigned long flags;
  45. reg_gio_rw_pa_dout dout;
  46. struct nand_chip *this = mtd_to_nand(mtd);
  47. local_irq_save(flags);
  48. /* control bits change */
  49. if (ctrl & NAND_CTRL_CHANGE) {
  50. dout = REG_RD(gio, regi_gio, rw_pa_dout);
  51. dout.data &= ~PIN_BITMASK;
  52. #if (CE_BIT == 4 && NAND_NCE == 1 && \
  53. CLE_BIT == 5 && NAND_CLE == 2 && \
  54. ALE_BIT == 6 && NAND_ALE == 4)
  55. /* Pins in same order as control bits, but shifted.
  56. * Optimize for this case; works for 2.6.18 */
  57. dout.data |= ((ctrl & CTRL_BITMASK) ^ NAND_NCE) << CE_BIT;
  58. #else
  59. /* the slow way */
  60. if (!(ctrl & NAND_NCE))
  61. dout.data |= (1 << CE_BIT);
  62. if (ctrl & NAND_CLE)
  63. dout.data |= (1 << CLE_BIT);
  64. if (ctrl & NAND_ALE)
  65. dout.data |= (1 << ALE_BIT);
  66. #endif
  67. REG_WR(gio, regi_gio, rw_pa_dout, dout);
  68. }
  69. /* command to chip */
  70. if (cmd != NAND_CMD_NONE)
  71. writeb(cmd, this->IO_ADDR_W);
  72. local_irq_restore(flags);
  73. }
  74. /*
  75. * read device ready pin
  76. */
  77. static int crisv32_device_ready(struct mtd_info *mtd)
  78. {
  79. reg_gio_r_pa_din din = REG_RD(gio, regi_gio, r_pa_din);
  80. return ((din.data & (1 << BY_BIT)) >> BY_BIT);
  81. }
  82. /*
  83. * Main initialization routine
  84. */
  85. struct mtd_info *__init crisv32_nand_flash_probe(void)
  86. {
  87. void __iomem *read_cs;
  88. void __iomem *write_cs;
  89. reg_bif_core_rw_grp3_cfg bif_cfg = REG_RD(bif_core, regi_bif_core,
  90. rw_grp3_cfg);
  91. reg_gio_rw_pa_oe pa_oe = REG_RD(gio, regi_gio, rw_pa_oe);
  92. struct mtd_info_wrapper *wrapper;
  93. struct nand_chip *this;
  94. int err = 0;
  95. /* Allocate memory for MTD device structure and private data */
  96. wrapper = kzalloc(sizeof(struct mtd_info_wrapper), GFP_KERNEL);
  97. if (!wrapper) {
  98. printk(KERN_ERR "Unable to allocate CRISv32 NAND MTD "
  99. "device structure.\n");
  100. err = -ENOMEM;
  101. return NULL;
  102. }
  103. read_cs = ioremap(MEM_CSP0_START | MEM_NON_CACHEABLE, 8192);
  104. write_cs = ioremap(MEM_CSP1_START | MEM_NON_CACHEABLE, 8192);
  105. if (!read_cs || !write_cs) {
  106. printk(KERN_ERR "CRISv32 NAND ioremap failed\n");
  107. err = -EIO;
  108. goto out_mtd;
  109. }
  110. /* Get pointer to private data */
  111. this = &wrapper->chip;
  112. crisv32_mtd = nand_to_mtd(this);
  113. pa_oe.oe |= 1 << CE_BIT;
  114. pa_oe.oe |= 1 << ALE_BIT;
  115. pa_oe.oe |= 1 << CLE_BIT;
  116. pa_oe.oe &= ~(1 << BY_BIT);
  117. REG_WR(gio, regi_gio, rw_pa_oe, pa_oe);
  118. bif_cfg.gated_csp0 = regk_bif_core_rd;
  119. bif_cfg.gated_csp1 = regk_bif_core_wr;
  120. REG_WR(bif_core, regi_bif_core, rw_grp3_cfg, bif_cfg);
  121. /* Set address of NAND IO lines */
  122. this->IO_ADDR_R = read_cs;
  123. this->IO_ADDR_W = write_cs;
  124. this->cmd_ctrl = crisv32_hwcontrol;
  125. this->dev_ready = crisv32_device_ready;
  126. /* 20 us command delay time */
  127. this->chip_delay = 20;
  128. this->ecc.mode = NAND_ECC_SOFT;
  129. this->ecc.algo = NAND_ECC_HAMMING;
  130. /* Enable the following for a flash based bad block table */
  131. /* this->bbt_options = NAND_BBT_USE_FLASH; */
  132. /* Scan to find existence of the device */
  133. if (nand_scan(crisv32_mtd, 1)) {
  134. err = -ENXIO;
  135. goto out_ior;
  136. }
  137. return crisv32_mtd;
  138. out_ior:
  139. iounmap((void *)read_cs);
  140. iounmap((void *)write_cs);
  141. out_mtd:
  142. kfree(wrapper);
  143. return NULL;
  144. }