nandflash.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <asm/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. /* Bitmask for control pins */
  34. #define PIN_BITMASK ((1 << CE_BIT) | (1 << CLE_BIT) | (1 << ALE_BIT))
  35. static struct mtd_info *crisv32_mtd;
  36. /*
  37. * hardware specific access to control-lines
  38. */
  39. static void crisv32_hwcontrol(struct mtd_info *mtd, int cmd,
  40. unsigned int ctrl)
  41. {
  42. unsigned long flags;
  43. reg_pio_rw_dout dout;
  44. struct nand_chip *this = mtd->priv;
  45. local_irq_save(flags);
  46. /* control bits change */
  47. if (ctrl & NAND_CTRL_CHANGE) {
  48. dout = REG_RD(pio, regi_pio, rw_dout);
  49. dout.regf_NCE = (ctrl & NAND_NCE) ? 0 : 1;
  50. #if !MANUAL_ALE_CLE_CONTROL
  51. if (ctrl & NAND_ALE) {
  52. /* A0 = ALE high */
  53. this->IO_ADDR_W = (void __iomem *)REG_ADDR(pio,
  54. regi_pio, rw_io_access1);
  55. } else if (ctrl & NAND_CLE) {
  56. /* A1 = CLE high */
  57. this->IO_ADDR_W = (void __iomem *)REG_ADDR(pio,
  58. regi_pio, rw_io_access2);
  59. } else {
  60. /* A1 = CLE and A0 = ALE low */
  61. this->IO_ADDR_W = (void __iomem *)REG_ADDR(pio,
  62. regi_pio, rw_io_access0);
  63. }
  64. #else
  65. dout.regf_CLE = (ctrl & NAND_CLE) ? 1 : 0;
  66. dout.regf_ALE = (ctrl & NAND_ALE) ? 1 : 0;
  67. #endif
  68. REG_WR(pio, regi_pio, rw_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. int crisv32_device_ready(struct mtd_info *mtd)
  79. {
  80. reg_pio_r_din din = REG_RD(pio, regi_pio, r_din);
  81. return din.rdy;
  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. struct nand_chip *this;
  91. int err = 0;
  92. reg_pio_rw_man_ctrl man_ctrl = {
  93. .regf_NCE = regk_pio_yes,
  94. #if MANUAL_ALE_CLE_CONTROL
  95. .regf_ALE = regk_pio_yes,
  96. .regf_CLE = regk_pio_yes
  97. #endif
  98. };
  99. reg_pio_rw_oe oe = {
  100. .regf_NCE = regk_pio_yes,
  101. #if MANUAL_ALE_CLE_CONTROL
  102. .regf_ALE = regk_pio_yes,
  103. .regf_CLE = regk_pio_yes
  104. #endif
  105. };
  106. reg_pio_rw_dout dout = { .regf_NCE = 1 };
  107. /* Allocate pio pins to pio */
  108. crisv32_pinmux_alloc_fixed(pinmux_pio);
  109. /* Set up CE, ALE, CLE (ce0_n, a0, a1) for manual control and output */
  110. REG_WR(pio, regi_pio, rw_man_ctrl, man_ctrl);
  111. REG_WR(pio, regi_pio, rw_dout, dout);
  112. REG_WR(pio, regi_pio, rw_oe, oe);
  113. /* Allocate memory for MTD device structure and private data */
  114. crisv32_mtd = kmalloc(sizeof(struct mtd_info) +
  115. sizeof(struct nand_chip), GFP_KERNEL);
  116. if (!crisv32_mtd) {
  117. printk(KERN_ERR "Unable to allocate CRISv32 NAND MTD "
  118. "device structure.\n");
  119. err = -ENOMEM;
  120. return NULL;
  121. }
  122. read_cs = write_cs = (void __iomem *)REG_ADDR(pio, regi_pio,
  123. rw_io_access0);
  124. /* Get pointer to private data */
  125. this = (struct nand_chip *) (&crisv32_mtd[1]);
  126. /* Initialize structures */
  127. memset((char *) crisv32_mtd, 0, sizeof(struct mtd_info));
  128. memset((char *) this, 0, sizeof(struct nand_chip));
  129. /* Link the private data with the MTD structure */
  130. crisv32_mtd->priv = this;
  131. /* Set address of NAND IO lines */
  132. this->IO_ADDR_R = read_cs;
  133. this->IO_ADDR_W = write_cs;
  134. this->cmd_ctrl = crisv32_hwcontrol;
  135. this->dev_ready = crisv32_device_ready;
  136. /* 20 us command delay time */
  137. this->chip_delay = 20;
  138. this->ecc.mode = NAND_ECC_SOFT;
  139. /* Enable the following for a flash based bad block table */
  140. /* this->options = NAND_USE_FLASH_BBT; */
  141. /* Scan to find existance of the device */
  142. if (nand_scan(crisv32_mtd, 1)) {
  143. err = -ENXIO;
  144. goto out_mtd;
  145. }
  146. return crisv32_mtd;
  147. out_mtd:
  148. kfree(crisv32_mtd);
  149. return NULL;
  150. }