ebi_onenand.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <asm/io.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/onenand.h>
  23. #include "vct.h"
  24. #define BURST_SIZE_WORDS 4
  25. static u16 ebi_nand_read_word(void __iomem *addr)
  26. {
  27. reg_write(EBI_CPU_IO_ACCS(EBI_BASE), (EXT_DEVICE_CHANNEL_2 | (u32)addr));
  28. ebi_wait();
  29. return reg_read(EBI_IO_ACCS_DATA(EBI_BASE)) >> 16;
  30. }
  31. static void ebi_nand_write_word(u16 data, void __iomem * addr)
  32. {
  33. ebi_wait();
  34. reg_write(EBI_IO_ACCS_DATA(EBI_BASE), (data << 16));
  35. reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
  36. EXT_DEVICE_CHANNEL_2 | EBI_CPU_WRITE | (u32)addr);
  37. ebi_wait();
  38. }
  39. /*
  40. * EBI initialization for OneNAND FLASH access
  41. */
  42. int ebi_init_onenand(void)
  43. {
  44. reg_write(EBI_DEV1_CONFIG1(EBI_BASE), 0x83000);
  45. reg_write(EBI_DEV2_CONFIG1(EBI_BASE), 0x00403002);
  46. reg_write(EBI_DEV2_CONFIG2(EBI_BASE), 0x50);
  47. reg_write(EBI_DEV3_CONFIG1(EBI_BASE), 0x00403002);
  48. reg_write(EBI_DEV3_CONFIG2(EBI_BASE), 0x0); /* byte/word ordering */
  49. reg_write(EBI_DEV2_TIM1_RD1(EBI_BASE), 0x00504000);
  50. reg_write(EBI_DEV2_TIM1_RD2(EBI_BASE), 0x00001000);
  51. reg_write(EBI_DEV2_TIM1_WR1(EBI_BASE), 0x12002223);
  52. reg_write(EBI_DEV2_TIM1_WR2(EBI_BASE), 0x3FC02220);
  53. reg_write(EBI_DEV3_TIM1_RD1(EBI_BASE), 0x00504000);
  54. reg_write(EBI_DEV3_TIM1_RD2(EBI_BASE), 0x00001000);
  55. reg_write(EBI_DEV3_TIM1_WR1(EBI_BASE), 0x05001000);
  56. reg_write(EBI_DEV3_TIM1_WR2(EBI_BASE), 0x00010200);
  57. reg_write(EBI_DEV2_TIM_EXT(EBI_BASE), 0xFFF00000);
  58. reg_write(EBI_DEV2_EXT_ACC(EBI_BASE), 0x0FFFFFFF);
  59. reg_write(EBI_DEV3_TIM_EXT(EBI_BASE), 0xFFF00000);
  60. reg_write(EBI_DEV3_EXT_ACC(EBI_BASE), 0x0FFFFFFF);
  61. /* prepare DMA configuration for EBI */
  62. reg_write(EBI_DEV3_FIFO_CONFIG(EBI_BASE), 0x0101ff00);
  63. /* READ only no byte order change, TAG 1 used */
  64. reg_write(EBI_DEV3_DMA_CONFIG2(EBI_BASE), 0x00000004);
  65. reg_write(EBI_TAG1_SYS_ID(EBI_BASE), 0x0); /* SCC DMA channel 0 */
  66. reg_write(EBI_TAG2_SYS_ID(EBI_BASE), 0x1);
  67. reg_write(EBI_TAG3_SYS_ID(EBI_BASE), 0x2);
  68. reg_write(EBI_TAG4_SYS_ID(EBI_BASE), 0x3);
  69. return 0;
  70. }
  71. static void *memcpy_16_from_onenand(void *dst, const void *src, unsigned int len)
  72. {
  73. void *ret = dst;
  74. u16 *d = dst;
  75. u16 *s = (u16 *)src;
  76. len >>= 1;
  77. while (len-- > 0)
  78. *d++ = ebi_nand_read_word(s++);
  79. return ret;
  80. }
  81. static void *memcpy_32_from_onenand(void *dst, const void *src, unsigned int len)
  82. {
  83. void *ret = dst;
  84. u32 *d = (u32 *)dst;
  85. u32 s = (u32)src;
  86. u32 bytes_per_block = BURST_SIZE_WORDS * sizeof(int);
  87. u32 n_blocks = len / bytes_per_block;
  88. u32 block = 0;
  89. u32 burst_word;
  90. for (block = 0; block < n_blocks; block++) {
  91. /* Trigger read channel 3 */
  92. reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
  93. (EXT_DEVICE_CHANNEL_3 | (s + (block * bytes_per_block))));
  94. /* Poll status to see whether read has finished */
  95. ebi_wait();
  96. /* Squirrel the data away in a safe place */
  97. for (burst_word = 0; burst_word < BURST_SIZE_WORDS; burst_word++)
  98. *d++ = reg_read(EBI_IO_ACCS_DATA(EBI_BASE));
  99. }
  100. return ret;
  101. }
  102. static void *memcpy_16_to_onenand(void *dst, const void *src, unsigned int len)
  103. {
  104. void *ret = dst;
  105. u16 *d = dst;
  106. u16 *s = (u16 *)src;
  107. len >>= 1;
  108. while (len-- > 0)
  109. ebi_nand_write_word(*s++, d++);
  110. return ret;
  111. }
  112. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  113. {
  114. struct onenand_chip *this = mtd->priv;
  115. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  116. if (area == ONENAND_DATARAM)
  117. return mtd->writesize;
  118. if (area == ONENAND_SPARERAM)
  119. return mtd->oobsize;
  120. }
  121. return 0;
  122. }
  123. static int ebi_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
  124. unsigned char *buffer, int offset,
  125. size_t count)
  126. {
  127. struct onenand_chip *this = mtd->priv;
  128. void __iomem *bufferram;
  129. bufferram = this->base + area;
  130. bufferram += onenand_bufferram_offset(mtd, area);
  131. if (count < 4)
  132. memcpy_16_from_onenand(buffer, bufferram + offset, count);
  133. else
  134. memcpy_32_from_onenand(buffer, bufferram + offset, count);
  135. return 0;
  136. }
  137. static int ebi_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
  138. const unsigned char *buffer, int offset,
  139. size_t count)
  140. {
  141. struct onenand_chip *this = mtd->priv;
  142. void __iomem *bufferram;
  143. bufferram = this->base + area;
  144. bufferram += onenand_bufferram_offset(mtd, area);
  145. memcpy_16_to_onenand(bufferram + offset, buffer, count);
  146. return 0;
  147. }
  148. void onenand_board_init(struct mtd_info *mtd)
  149. {
  150. struct onenand_chip *chip = mtd->priv;
  151. /*
  152. * Insert board specific OneNAND access functions
  153. */
  154. chip->read_word = ebi_nand_read_word;
  155. chip->write_word = ebi_nand_write_word;
  156. chip->read_bufferram = ebi_read_bufferram;
  157. chip->read_spareram = ebi_read_bufferram;
  158. chip->write_bufferram = ebi_write_bufferram;
  159. }