nandflash.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * arch/cris/arch-v32/drivers/nandflash.c
  3. *
  4. * Copyright (c) 2007
  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/pio_defs.h>
  24. #include <pinmux.h>
  25. #include <asm/io.h>
  26. #define MANUAL_ALE_CLE_CONTROL 1
  27. #define regf_ALE a0
  28. #define regf_CLE a1
  29. #define regf_NCE ce0_n
  30. #define CLE_BIT 10
  31. #define ALE_BIT 11
  32. #define CE_BIT 12
  33. struct mtd_info_wrapper {
  34. struct mtd_info info;
  35. struct nand_chip chip;
  36. };
  37. /* Bitmask for control pins */
  38. #define PIN_BITMASK ((1 << CE_BIT) | (1 << CLE_BIT) | (1 << ALE_BIT))
  39. static struct mtd_info *crisv32_mtd;
  40. /*
  41. * hardware specific access to control-lines
  42. */
  43. static void crisv32_hwcontrol(struct mtd_info *mtd, int cmd,
  44. unsigned int ctrl)
  45. {
  46. unsigned long flags;
  47. reg_pio_rw_dout dout;
  48. struct nand_chip *this = mtd->priv;
  49. local_irq_save(flags);
  50. /* control bits change */
  51. if (ctrl & NAND_CTRL_CHANGE) {
  52. dout = REG_RD(pio, regi_pio, rw_dout);
  53. dout.regf_NCE = (ctrl & NAND_NCE) ? 0 : 1;
  54. #if !MANUAL_ALE_CLE_CONTROL
  55. if (ctrl & NAND_ALE) {
  56. /* A0 = ALE high */
  57. this->IO_ADDR_W = (void __iomem *)REG_ADDR(pio,
  58. regi_pio, rw_io_access1);
  59. } else if (ctrl & NAND_CLE) {
  60. /* A1 = CLE high */
  61. this->IO_ADDR_W = (void __iomem *)REG_ADDR(pio,
  62. regi_pio, rw_io_access2);
  63. } else {
  64. /* A1 = CLE and A0 = ALE low */
  65. this->IO_ADDR_W = (void __iomem *)REG_ADDR(pio,
  66. regi_pio, rw_io_access0);
  67. }
  68. #else
  69. dout.regf_CLE = (ctrl & NAND_CLE) ? 1 : 0;
  70. dout.regf_ALE = (ctrl & NAND_ALE) ? 1 : 0;
  71. #endif
  72. REG_WR(pio, regi_pio, rw_dout, dout);
  73. }
  74. /* command to chip */
  75. if (cmd != NAND_CMD_NONE)
  76. writeb(cmd, this->IO_ADDR_W);
  77. local_irq_restore(flags);
  78. }
  79. /*
  80. * read device ready pin
  81. */
  82. static int crisv32_device_ready(struct mtd_info *mtd)
  83. {
  84. reg_pio_r_din din = REG_RD(pio, regi_pio, r_din);
  85. return din.rdy;
  86. }
  87. /*
  88. * Main initialization routine
  89. */
  90. struct mtd_info *__init crisv32_nand_flash_probe(void)
  91. {
  92. void __iomem *read_cs;
  93. void __iomem *write_cs;
  94. struct mtd_info_wrapper *wrapper;
  95. struct nand_chip *this;
  96. int err = 0;
  97. reg_pio_rw_man_ctrl man_ctrl = {
  98. .regf_NCE = regk_pio_yes,
  99. #if MANUAL_ALE_CLE_CONTROL
  100. .regf_ALE = regk_pio_yes,
  101. .regf_CLE = regk_pio_yes
  102. #endif
  103. };
  104. reg_pio_rw_oe oe = {
  105. .regf_NCE = regk_pio_yes,
  106. #if MANUAL_ALE_CLE_CONTROL
  107. .regf_ALE = regk_pio_yes,
  108. .regf_CLE = regk_pio_yes
  109. #endif
  110. };
  111. reg_pio_rw_dout dout = { .regf_NCE = 1 };
  112. /* Allocate pio pins to pio */
  113. crisv32_pinmux_alloc_fixed(pinmux_pio);
  114. /* Set up CE, ALE, CLE (ce0_n, a0, a1) for manual control and output */
  115. REG_WR(pio, regi_pio, rw_man_ctrl, man_ctrl);
  116. REG_WR(pio, regi_pio, rw_dout, dout);
  117. REG_WR(pio, regi_pio, rw_oe, oe);
  118. /* Allocate memory for MTD device structure and private data */
  119. wrapper = kzalloc(sizeof(struct mtd_info_wrapper), GFP_KERNEL);
  120. if (!wrapper) {
  121. printk(KERN_ERR "Unable to allocate CRISv32 NAND MTD "
  122. "device structure.\n");
  123. err = -ENOMEM;
  124. return NULL;
  125. }
  126. read_cs = write_cs = (void __iomem *)REG_ADDR(pio, regi_pio,
  127. rw_io_access0);
  128. /* Get pointer to private data */
  129. this = &wrapper->chip;
  130. crisv32_mtd = &wrapper->info;
  131. /* Link the private data with the MTD structure */
  132. crisv32_mtd->priv = this;
  133. /* Set address of NAND IO lines */
  134. this->IO_ADDR_R = read_cs;
  135. this->IO_ADDR_W = write_cs;
  136. this->cmd_ctrl = crisv32_hwcontrol;
  137. this->dev_ready = crisv32_device_ready;
  138. /* 20 us command delay time */
  139. this->chip_delay = 20;
  140. this->ecc.mode = NAND_ECC_SOFT;
  141. /* Enable the following for a flash based bad block table */
  142. /* this->bbt_options = NAND_BBT_USE_FLASH; */
  143. /* Scan to find existence of the device */
  144. if (nand_scan(crisv32_mtd, 1)) {
  145. err = -ENXIO;
  146. goto out_mtd;
  147. }
  148. return crisv32_mtd;
  149. out_mtd:
  150. kfree(wrapper);
  151. return NULL;
  152. }