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