nand.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * (C) Copyright 2008
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <ioports.h>
  25. #include <mpc8260.h>
  26. #include "tqm8272.h"
  27. /* UPM pattern for bus clock = 66.7 MHz */
  28. static const uint upmTable67[] =
  29. {
  30. /* Offset UPM Read Single RAM array entry -> NAND Read Data */
  31. /* 0x00 */ 0x0fa3f100, 0x0fa3b000, 0x0fa33100, 0x0fa33000,
  32. /* 0x04 */ 0x0fa33000, 0x0fa33004, 0xfffffc01, 0xfffffc00,
  33. /* UPM Read Burst RAM array entry -> unused */
  34. /* 0x08 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  35. /* 0x0C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  36. /* UPM Read Burst RAM array entry -> unused */
  37. /* 0x10 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  38. /* 0x14 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  39. /* UPM Write Single RAM array entry -> NAND Write Data, ADDR and CMD */
  40. /* 0x18 */ 0x00a3fc00, 0x00a3fc00, 0x00a3fc00, 0x00a3fc00,
  41. /* 0x1C */ 0x0fa3fc00, 0x0fa3fc04, 0xfffffc01, 0xfffffc00,
  42. /* UPM Write Burst RAM array entry -> unused */
  43. /* 0x20 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  44. /* 0x24 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  45. /* 0x28 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  46. /* 0x2C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  47. /* UPM Refresh Timer RAM array entry -> unused */
  48. /* 0x30 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  49. /* 0x34 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  50. /* 0x38 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  51. /* UPM Exception RAM array entry -> unsused */
  52. /* 0x3C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  53. };
  54. /* UPM pattern for bus clock = 100 MHz */
  55. static const uint upmTable100[] =
  56. {
  57. /* Offset UPM Read Single RAM array entry -> NAND Read Data */
  58. /* 0x00 */ 0x0fa3f200, 0x0fa3b000, 0x0fa33300, 0x0fa33000,
  59. /* 0x04 */ 0x0fa33000, 0x0fa33004, 0xfffffc01, 0xfffffc00,
  60. /* UPM Read Burst RAM array entry -> unused */
  61. /* 0x08 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  62. /* 0x0C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  63. /* UPM Read Burst RAM array entry -> unused */
  64. /* 0x10 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  65. /* 0x14 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  66. /* UPM Write Single RAM array entry -> NAND Write Data, ADDR and CMD */
  67. /* 0x18 */ 0x00a3ff00, 0x00a3fc00, 0x00a3fc00, 0x0fa3fc00,
  68. /* 0x1C */ 0x0fa3fc00, 0x0fa3fc04, 0xfffffc01, 0xfffffc00,
  69. /* UPM Write Burst RAM array entry -> unused */
  70. /* 0x20 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  71. /* 0x24 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  72. /* 0x28 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  73. /* 0x2C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  74. /* UPM Refresh Timer RAM array entry -> unused */
  75. /* 0x30 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  76. /* 0x34 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  77. /* 0x38 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  78. /* UPM Exception RAM array entry -> unsused */
  79. /* 0x3C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  80. };
  81. /* UPM pattern for bus clock = 133.3 MHz */
  82. static const uint upmTable133[] =
  83. {
  84. /* Offset UPM Read Single RAM array entry -> NAND Read Data */
  85. /* 0x00 */ 0x0fa3f300, 0x0fa3b000, 0x0fa33300, 0x0fa33000,
  86. /* 0x04 */ 0x0fa33200, 0x0fa33004, 0xfffffc01, 0xfffffc00,
  87. /* UPM Read Burst RAM array entry -> unused */
  88. /* 0x08 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  89. /* 0x0C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  90. /* UPM Read Burst RAM array entry -> unused */
  91. /* 0x10 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  92. /* 0x14 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  93. /* UPM Write Single RAM array entry -> NAND Write Data, ADDR and CMD */
  94. /* 0x18 */ 0x00a3ff00, 0x00a3fc00, 0x00a3fd00, 0x0fa3fc00,
  95. /* 0x1C */ 0x0fa3fd00, 0x0fa3fc04, 0xfffffc01, 0xfffffc00,
  96. /* UPM Write Burst RAM array entry -> unused */
  97. /* 0x20 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  98. /* 0x24 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  99. /* 0x28 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  100. /* 0x2C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  101. /* UPM Refresh Timer RAM array entry -> unused */
  102. /* 0x30 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  103. /* 0x34 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc00,
  104. /* 0x38 */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  105. /* UPM Exception RAM array entry -> unsused */
  106. /* 0x3C */ 0xfffffc00, 0xfffffc00, 0xfffffc00, 0xfffffc01,
  107. };
  108. static int chipsel = 0;
  109. #if defined(CONFIG_CMD_NAND)
  110. #include <nand.h>
  111. #include <linux/mtd/mtd.h>
  112. static u8 hwctl = 0;
  113. static void upmnand_write_byte(struct mtd_info *mtdinfo, u_char byte)
  114. {
  115. struct nand_chip *this = mtdinfo->priv;
  116. ulong base = (ulong) (this->IO_ADDR_W + chipsel * CONFIG_SYS_NAND_CS_DIST);
  117. if (hwctl & 0x1) {
  118. WRITE_NAND_UPM(byte, base, CONFIG_SYS_NAND_UPM_WRITE_CMD_OFS);
  119. } else if (hwctl & 0x2) {
  120. WRITE_NAND_UPM(byte, base, CONFIG_SYS_NAND_UPM_WRITE_ADDR_OFS);
  121. } else {
  122. WRITE_NAND(byte, base);
  123. }
  124. }
  125. static void upmnand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  126. {
  127. if (ctrl & NAND_CTRL_CHANGE) {
  128. if ( ctrl & NAND_CLE )
  129. hwctl |= 0x1;
  130. else
  131. hwctl &= ~0x1;
  132. if ( ctrl & NAND_ALE )
  133. hwctl |= 0x2;
  134. else
  135. hwctl &= ~0x2;
  136. }
  137. if (cmd != NAND_CMD_NONE)
  138. upmnand_write_byte (mtd, cmd);
  139. }
  140. static u_char upmnand_read_byte(struct mtd_info *mtdinfo)
  141. {
  142. struct nand_chip *this = mtdinfo->priv;
  143. ulong base = (ulong) (this->IO_ADDR_W + chipsel * CONFIG_SYS_NAND_CS_DIST);
  144. return READ_NAND(base);
  145. }
  146. static int tqm8272_dev_ready(struct mtd_info *mtdinfo)
  147. {
  148. /* constant delay (see also tR in the datasheet) */
  149. udelay(12); \
  150. return 1;
  151. }
  152. #ifndef CONFIG_NAND_SPL
  153. static void tqm8272_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  154. {
  155. struct nand_chip *this = mtdinfo->priv;
  156. unsigned char *base = (unsigned char *) (this->IO_ADDR_W + chipsel * CONFIG_SYS_NAND_CS_DIST);
  157. int i;
  158. for (i = 0; i< len; i++)
  159. buf[i] = *base;
  160. }
  161. static void tqm8272_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  162. {
  163. struct nand_chip *this = mtdinfo->priv;
  164. unsigned char *base = (unsigned char *) (this->IO_ADDR_W + chipsel * CONFIG_SYS_NAND_CS_DIST);
  165. int i;
  166. for (i = 0; i< len; i++)
  167. *base = buf[i];
  168. }
  169. static int tqm8272_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  170. {
  171. struct nand_chip *this = mtdinfo->priv;
  172. unsigned char *base = (unsigned char *) (this->IO_ADDR_W + chipsel * CONFIG_SYS_NAND_CS_DIST);
  173. int i;
  174. for (i = 0; i < len; i++)
  175. if (buf[i] != *base)
  176. return -1;
  177. return 0;
  178. }
  179. #endif /* #ifndef CONFIG_NAND_SPL */
  180. void board_nand_select_device(struct nand_chip *nand, int chip)
  181. {
  182. chipsel = chip;
  183. }
  184. int board_nand_init(struct nand_chip *nand)
  185. {
  186. static int UpmInit = 0;
  187. volatile immap_t * immr = (immap_t *)CONFIG_SYS_IMMR;
  188. volatile memctl8260_t *memctl = &immr->im_memctl;
  189. if (hwinf.nand == 0) return -1;
  190. /* Setup the UPM */
  191. if (UpmInit == 0) {
  192. switch (hwinf.busclk_real) {
  193. case 100000000:
  194. upmconfig (UPMB, (uint *) upmTable100,
  195. sizeof (upmTable100) / sizeof (uint));
  196. break;
  197. case 133333333:
  198. upmconfig (UPMB, (uint *) upmTable133,
  199. sizeof (upmTable133) / sizeof (uint));
  200. break;
  201. default:
  202. upmconfig (UPMB, (uint *) upmTable67,
  203. sizeof (upmTable67) / sizeof (uint));
  204. break;
  205. }
  206. UpmInit = 1;
  207. }
  208. /* Setup the memctrl */
  209. memctl->memc_or3 = CONFIG_SYS_NAND_OR;
  210. memctl->memc_br3 = CONFIG_SYS_NAND_BR;
  211. memctl->memc_mbmr = (MxMR_OP_NORM);
  212. nand->ecc.mode = NAND_ECC_SOFT;
  213. nand->cmd_ctrl = upmnand_hwcontrol;
  214. nand->read_byte = upmnand_read_byte;
  215. nand->dev_ready = tqm8272_dev_ready;
  216. #ifndef CONFIG_NAND_SPL
  217. nand->write_buf = tqm8272_write_buf;
  218. nand->read_buf = tqm8272_read_buf;
  219. nand->verify_buf = tqm8272_verify_buf;
  220. #endif
  221. /*
  222. * Select required NAND chip
  223. */
  224. board_nand_select_device(nand, 0);
  225. return 0;
  226. }
  227. #endif