sunxi_nand_spl.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2014, Antmicro Ltd <www.antmicro.com>
  3. * Copyright (c) 2015, Turtle Solutions <www.turtle-solutions.eu>
  4. * Copyright (c) 2015, Roy Spliet <rspliet@ultimaker.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * \todo Detect chip parameters (page size, ECC mode, randomisation...)
  9. */
  10. #include <common.h>
  11. #include <config.h>
  12. #include <asm/io.h>
  13. #include <nand.h>
  14. #include <asm/arch/cpu.h>
  15. #include <asm/arch/clock.h>
  16. #include <asm/arch/dma.h>
  17. #include <asm/arch/nand.h>
  18. void
  19. nand_init(void)
  20. {
  21. struct sunxi_ccm_reg * const ccm =
  22. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  23. struct sunxi_nand * const nand = (struct sunxi_nand *)SUNXI_NFC_BASE;
  24. u32 val;
  25. board_nand_init();
  26. /* "un-gate" NAND clock and clock source
  27. * This assumes that the clock was already correctly configured by
  28. * BootROM */
  29. setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_NAND0));
  30. #ifdef CONFIG_MACH_SUN9I
  31. setbits_le32(&ccm->ahb_gate1, (1 << AHB_GATE_OFFSET_DMA));
  32. #else
  33. setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_DMA));
  34. #endif
  35. setbits_le32(&ccm->nand0_clk_cfg, 0x80000000);
  36. val = readl(&nand->ctl);
  37. val |= SUNXI_NAND_CTL_RST;
  38. writel(val, &nand->ctl);
  39. /* Wait until reset pin is deasserted */
  40. do {
  41. val = readl(&nand->ctl);
  42. if (!(val & SUNXI_NAND_CTL_RST))
  43. break;
  44. } while (1);
  45. /** \todo Chip select, currently kind of static */
  46. val = readl(&nand->ctl);
  47. val &= 0xf0fff0f2;
  48. val |= SUNXI_NAND_CTL_EN;
  49. val |= SUNXI_NAND_CTL_PAGE_SIZE(CONFIG_NAND_SUNXI_PAGE_SIZE);
  50. writel(val, &nand->ctl);
  51. writel(0x100, &nand->timing_ctl);
  52. writel(0x7ff, &nand->timing_cfg);
  53. /* reset CMD */
  54. val = SUNXI_NAND_CMD_SEND_CMD1 | SUNXI_NAND_CMD_WAIT_FLAG |
  55. NAND_CMD_RESET;
  56. writel(val, &nand->cmd);
  57. do {
  58. val = readl(&nand->st);
  59. if (val & (1<<1))
  60. break;
  61. udelay(1000);
  62. } while (1);
  63. printf("Nand initialised\n");
  64. }
  65. int
  66. nand_wait_timeout(u32 *reg, u32 mask, u32 val)
  67. {
  68. unsigned long tmo = timer_get_us() + 1000000; /* 1s */
  69. while ((readl(reg) & mask) != val) {
  70. if (timer_get_us() > tmo)
  71. return -ETIMEDOUT;
  72. }
  73. return 0;
  74. }
  75. /* random seed */
  76. static const uint16_t random_seed[128] = {
  77. 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
  78. 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
  79. 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
  80. 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
  81. 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
  82. 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
  83. 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
  84. 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
  85. 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
  86. 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
  87. 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
  88. 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
  89. 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
  90. 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
  91. 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
  92. 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
  93. };
  94. uint32_t ecc_errors = 0;
  95. static void
  96. nand_config_ecc(struct sunxi_nand *nand, uint32_t page, int syndrome)
  97. {
  98. static u8 strength[] = {16, 24, 28, 32, 40, 48, 56, 60, 64};
  99. int i;
  100. uint32_t ecc_mode;
  101. u32 ecc;
  102. u16 seed = 0;
  103. for (i = 0; i < ARRAY_SIZE(strength); i++) {
  104. if (CONFIG_NAND_SUNXI_ECC_STRENGTH == strength[i]) {
  105. ecc_mode = i;
  106. break;
  107. }
  108. }
  109. if (i == ARRAY_SIZE(strength)) {
  110. printf("ECC strength unsupported\n");
  111. return;
  112. }
  113. ecc = SUNXI_NAND_ECC_CTL_ECC_EN |
  114. SUNXI_NAND_ECC_CTL_PIPELINE |
  115. SUNXI_NAND_ECC_CTL_RND_EN |
  116. SUNXI_NAND_ECC_CTL_MODE(ecc_mode);
  117. if (CONFIG_NAND_SUNXI_ECC_STEP == 512)
  118. ecc |= SUNXI_NAND_ECC_CTL_BS_512B;
  119. if (syndrome)
  120. seed = 0x4A80;
  121. else
  122. seed = random_seed[page % ARRAY_SIZE(random_seed)];
  123. ecc |= SUNXI_NAND_ECC_CTL_RND_SEED(seed);
  124. writel(ecc, &nand->ecc_ctl);
  125. }
  126. /* read CONFIG_NAND_SUNXI_ECC_STEP bytes from real_addr to temp_buf */
  127. void
  128. nand_read_block(struct sunxi_nand *nand, phys_addr_t src, dma_addr_t dst,
  129. int syndrome)
  130. {
  131. struct sunxi_dma * const dma = (struct sunxi_dma *)SUNXI_DMA_BASE;
  132. struct sunxi_dma_cfg * const dma_cfg = &dma->ddma[0];
  133. uint32_t shift;
  134. uint32_t page;
  135. uint32_t addr;
  136. uint32_t oob_offset;
  137. uint32_t ecc_bytes;
  138. u32 val;
  139. u32 cmd;
  140. page = src / CONFIG_NAND_SUNXI_PAGE_SIZE;
  141. if (page > 0xFFFF) {
  142. /* TODO: currently this is not supported */
  143. printf("Reading from address >= %08X is not allowed.\n",
  144. 0xFFFF * CONFIG_NAND_SUNXI_PAGE_SIZE);
  145. return;
  146. }
  147. shift = src % CONFIG_NAND_SUNXI_PAGE_SIZE;
  148. writel(0, &nand->ecc_st);
  149. /* ECC_CTL, randomization */
  150. ecc_bytes = CONFIG_NAND_SUNXI_ECC_STRENGTH *
  151. fls(CONFIG_NAND_SUNXI_ECC_STEP * 8);
  152. ecc_bytes = DIV_ROUND_UP(ecc_bytes, 8);
  153. ecc_bytes += (ecc_bytes & 1); /* Align to 2-bytes */
  154. ecc_bytes += 4;
  155. nand_config_ecc(nand, page, syndrome);
  156. if (syndrome) {
  157. /* shift every 1kB in syndrome */
  158. shift += (shift / CONFIG_NAND_SUNXI_ECC_STEP) * ecc_bytes;
  159. oob_offset = CONFIG_NAND_SUNXI_ECC_STEP + shift;
  160. } else {
  161. oob_offset = CONFIG_NAND_SUNXI_PAGE_SIZE +
  162. (shift / CONFIG_NAND_SUNXI_ECC_STEP) * ecc_bytes;
  163. }
  164. addr = (page << 16) | shift;
  165. /* DMA */
  166. val = readl(&nand->ctl);
  167. writel(val | SUNXI_NAND_CTL_RAM_METHOD_DMA, &nand->ctl);
  168. writel(oob_offset, &nand->spare_area);
  169. /* DMAC
  170. * \todo Separate this into a tidy driver */
  171. writel(0x0, &dma->irq_en); /* clear dma interrupts */
  172. writel((uint32_t) &nand->io_data , &dma_cfg->src_addr);
  173. writel(dst , &dma_cfg->dst_addr);
  174. writel(0x00007F0F , &dma_cfg->ddma_para);
  175. writel(CONFIG_NAND_SUNXI_ECC_STEP, &dma_cfg->bc);
  176. val = SUNXI_DMA_CTL_SRC_DRQ(DDMA_SRC_DRQ_NAND) |
  177. SUNXI_DMA_CTL_MODE_IO |
  178. SUNXI_DMA_CTL_SRC_DATA_WIDTH_32 |
  179. SUNXI_DMA_CTL_DST_DRQ(DDMA_DST_DRQ_SDRAM) |
  180. SUNXI_DMA_CTL_DST_DATA_WIDTH_32 |
  181. SUNXI_DMA_CTL_TRIGGER;
  182. writel(val, &dma_cfg->ctl);
  183. writel(0x00E00530, &nand->rcmd_set);
  184. nand_wait_timeout(&nand->st, SUNXI_NAND_ST_FIFO_FULL, 0);
  185. writel(1 , &nand->block_num);
  186. writel(addr, &nand->addr_low);
  187. writel(0 , &nand->addr_high);
  188. /* CMD (PAGE READ) */
  189. cmd = 0x85E80000;
  190. cmd |= SUNXI_NAND_CMD_ADDR_CYCLES(CONFIG_NAND_SUNXI_ADDR_CYCLES);
  191. cmd |= (syndrome ? SUNXI_NAND_CMD_ORDER_SEQ :
  192. SUNXI_NAND_CMD_ORDER_INTERLEAVE);
  193. writel(cmd, &nand->cmd);
  194. if(nand_wait_timeout(&nand->st, SUNXI_NAND_ST_DMA_INT,
  195. SUNXI_NAND_ST_DMA_INT)) {
  196. printf("NAND timeout reading data\n");
  197. return;
  198. }
  199. if(nand_wait_timeout(&dma_cfg->ctl, SUNXI_DMA_CTL_TRIGGER, 0)) {
  200. printf("NAND timeout reading data\n");
  201. return;
  202. }
  203. if (readl(&nand->ecc_st))
  204. ecc_errors++;
  205. }
  206. int
  207. nand_spl_load_image(uint32_t offs, unsigned int size, void *dest)
  208. {
  209. struct sunxi_nand * const nand = (struct sunxi_nand *)SUNXI_NFC_BASE;
  210. dma_addr_t dst_block;
  211. dma_addr_t dst_end;
  212. phys_addr_t addr = offs;
  213. dst_end = ((dma_addr_t) dest) + size;
  214. memset((void *)dest, 0x0, size);
  215. ecc_errors = 0;
  216. for (dst_block = (dma_addr_t) dest; dst_block < dst_end;
  217. dst_block += CONFIG_NAND_SUNXI_ECC_STEP,
  218. addr += CONFIG_NAND_SUNXI_ECC_STEP) {
  219. /* syndrome read first 4MiB to match Allwinner BootROM */
  220. nand_read_block(nand, addr, dst_block, addr < 0x400000);
  221. }
  222. if (ecc_errors)
  223. printf("Error: %d ECC failures detected\n", ecc_errors);
  224. return ecc_errors == 0;
  225. }
  226. void
  227. nand_deselect(void)
  228. {}