nandflash.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 mtd_info info;
  32. struct nand_chip chip;
  33. };
  34. /* Bitmask for control pins */
  35. #define PIN_BITMASK ((1 << CE_BIT) | (1 << CLE_BIT) | (1 << ALE_BIT))
  36. /* Bitmask for mtd nand control bits */
  37. #define CTRL_BITMASK (NAND_NCE | NAND_CLE | NAND_ALE)
  38. static struct mtd_info *crisv32_mtd;
  39. /*
  40. * hardware specific access to control-lines
  41. */
  42. static void crisv32_hwcontrol(struct mtd_info *mtd, int cmd,
  43. unsigned int ctrl)
  44. {
  45. unsigned long flags;
  46. reg_gio_rw_pa_dout dout;
  47. struct nand_chip *this = mtd->priv;
  48. local_irq_save(flags);
  49. /* control bits change */
  50. if (ctrl & NAND_CTRL_CHANGE) {
  51. dout = REG_RD(gio, regi_gio, rw_pa_dout);
  52. dout.data &= ~PIN_BITMASK;
  53. #if (CE_BIT == 4 && NAND_NCE == 1 && \
  54. CLE_BIT == 5 && NAND_CLE == 2 && \
  55. ALE_BIT == 6 && NAND_ALE == 4)
  56. /* Pins in same order as control bits, but shifted.
  57. * Optimize for this case; works for 2.6.18 */
  58. dout.data |= ((ctrl & CTRL_BITMASK) ^ NAND_NCE) << CE_BIT;
  59. #else
  60. /* the slow way */
  61. if (!(ctrl & NAND_NCE))
  62. dout.data |= (1 << CE_BIT);
  63. if (ctrl & NAND_CLE)
  64. dout.data |= (1 << CLE_BIT);
  65. if (ctrl & NAND_ALE)
  66. dout.data |= (1 << ALE_BIT);
  67. #endif
  68. REG_WR(gio, regi_gio, rw_pa_dout, dout);
  69. }
  70. /* command to chip */
  71. if (cmd != NAND_CMD_NONE)
  72. writeb(cmd, this->IO_ADDR_W);
  73. local_irq_restore(flags);
  74. }
  75. /*
  76. * read device ready pin
  77. */
  78. static int crisv32_device_ready(struct mtd_info *mtd)
  79. {
  80. reg_gio_r_pa_din din = REG_RD(gio, regi_gio, r_pa_din);
  81. return ((din.data & (1 << BY_BIT)) >> BY_BIT);
  82. }
  83. /*
  84. * Main initialization routine
  85. */
  86. struct mtd_info *__init crisv32_nand_flash_probe(void)
  87. {
  88. void __iomem *read_cs;
  89. void __iomem *write_cs;
  90. reg_bif_core_rw_grp3_cfg bif_cfg = REG_RD(bif_core, regi_bif_core,
  91. rw_grp3_cfg);
  92. reg_gio_rw_pa_oe pa_oe = REG_RD(gio, regi_gio, rw_pa_oe);
  93. struct mtd_info_wrapper *wrapper;
  94. struct nand_chip *this;
  95. int err = 0;
  96. /* Allocate memory for MTD device structure and private data */
  97. wrapper = kzalloc(sizeof(struct mtd_info_wrapper), GFP_KERNEL);
  98. if (!wrapper) {
  99. printk(KERN_ERR "Unable to allocate CRISv32 NAND MTD "
  100. "device structure.\n");
  101. err = -ENOMEM;
  102. return NULL;
  103. }
  104. read_cs = ioremap(MEM_CSP0_START | MEM_NON_CACHEABLE, 8192);
  105. write_cs = ioremap(MEM_CSP1_START | MEM_NON_CACHEABLE, 8192);
  106. if (!read_cs || !write_cs) {
  107. printk(KERN_ERR "CRISv32 NAND ioremap failed\n");
  108. err = -EIO;
  109. goto out_mtd;
  110. }
  111. /* Get pointer to private data */
  112. this = &wrapper->chip;
  113. crisv32_mtd = &wrapper->info;
  114. pa_oe.oe |= 1 << CE_BIT;
  115. pa_oe.oe |= 1 << ALE_BIT;
  116. pa_oe.oe |= 1 << CLE_BIT;
  117. pa_oe.oe &= ~(1 << BY_BIT);
  118. REG_WR(gio, regi_gio, rw_pa_oe, pa_oe);
  119. bif_cfg.gated_csp0 = regk_bif_core_rd;
  120. bif_cfg.gated_csp1 = regk_bif_core_wr;
  121. REG_WR(bif_core, regi_bif_core, rw_grp3_cfg, bif_cfg);
  122. /* Link the private data with the MTD structure */
  123. crisv32_mtd->priv = this;
  124. /* Set address of NAND IO lines */
  125. this->IO_ADDR_R = read_cs;
  126. this->IO_ADDR_W = write_cs;
  127. this->cmd_ctrl = crisv32_hwcontrol;
  128. this->dev_ready = crisv32_device_ready;
  129. /* 20 us command delay time */
  130. this->chip_delay = 20;
  131. this->ecc.mode = NAND_ECC_SOFT;
  132. /* Enable the following for a flash based bad block table */
  133. /* this->bbt_options = NAND_BBT_USE_FLASH; */
  134. /* Scan to find existence of the device */
  135. if (nand_scan(crisv32_mtd, 1)) {
  136. err = -ENXIO;
  137. goto out_ior;
  138. }
  139. return crisv32_mtd;
  140. out_ior:
  141. iounmap((void *)read_cs);
  142. iounmap((void *)write_cs);
  143. out_mtd:
  144. kfree(wrapper);
  145. return NULL;
  146. }