Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Panasonic Corporation |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 4 | * Copyright (C) 2014-2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <asm/unaligned.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 10 | #include <linux/mtd/rawnand.h> |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 11 | #include "denali.h" |
| 12 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 13 | #define DENALI_MAP01 (1 << 26) /* read/write pages in PIO */ |
| 14 | #define DENALI_MAP10 (2 << 26) /* high-level control plane */ |
| 15 | |
| 16 | #define INDEX_CTRL_REG 0x0 |
| 17 | #define INDEX_DATA_REG 0x10 |
| 18 | |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 19 | #define SPARE_ACCESS 0x41 |
| 20 | #define MAIN_ACCESS 0x42 |
| 21 | #define PIPELINE_ACCESS 0x2000 |
| 22 | |
| 23 | #define BANK(x) ((x) << 24) |
| 24 | |
| 25 | static void __iomem *denali_flash_mem = |
| 26 | (void __iomem *)CONFIG_SYS_NAND_DATA_BASE; |
| 27 | static void __iomem *denali_flash_reg = |
| 28 | (void __iomem *)CONFIG_SYS_NAND_REGS_BASE; |
| 29 | |
| 30 | static const int flash_bank; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 31 | static int page_size, oob_size, pages_per_block; |
| 32 | |
| 33 | static void index_addr(uint32_t address, uint32_t data) |
| 34 | { |
| 35 | writel(address, denali_flash_mem + INDEX_CTRL_REG); |
| 36 | writel(data, denali_flash_mem + INDEX_DATA_REG); |
| 37 | } |
| 38 | |
| 39 | static int wait_for_irq(uint32_t irq_mask) |
| 40 | { |
| 41 | unsigned long timeout = 1000000; |
| 42 | uint32_t intr_status; |
| 43 | |
| 44 | do { |
| 45 | intr_status = readl(denali_flash_reg + INTR_STATUS(flash_bank)); |
| 46 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 47 | if (intr_status & INTR__ECC_UNCOR_ERR) { |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 48 | debug("Uncorrected ECC detected\n"); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 49 | return -EBADMSG; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if (intr_status & irq_mask) |
| 53 | break; |
| 54 | |
| 55 | udelay(1); |
| 56 | timeout--; |
| 57 | } while (timeout); |
| 58 | |
| 59 | if (!timeout) { |
| 60 | debug("Timeout with interrupt status %08x\n", intr_status); |
| 61 | return -EIO; |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static void read_data_from_flash_mem(uint8_t *buf, int len) |
| 68 | { |
| 69 | int i; |
| 70 | uint32_t *buf32; |
| 71 | |
| 72 | /* transfer the data from the flash */ |
| 73 | buf32 = (uint32_t *)buf; |
| 74 | |
| 75 | /* |
| 76 | * Let's take care of unaligned access although it rarely happens. |
| 77 | * Avoid put_unaligned() for the normal use cases since it leads to |
| 78 | * a bit performance regression. |
| 79 | */ |
| 80 | if ((unsigned long)buf32 % 4) { |
| 81 | for (i = 0; i < len / 4; i++) |
| 82 | put_unaligned(readl(denali_flash_mem + INDEX_DATA_REG), |
| 83 | buf32++); |
| 84 | } else { |
| 85 | for (i = 0; i < len / 4; i++) |
| 86 | *buf32++ = readl(denali_flash_mem + INDEX_DATA_REG); |
| 87 | } |
| 88 | |
| 89 | if (len % 4) { |
| 90 | u32 tmp; |
| 91 | |
| 92 | tmp = cpu_to_le32(readl(denali_flash_mem + INDEX_DATA_REG)); |
| 93 | buf = (uint8_t *)buf32; |
| 94 | for (i = 0; i < len % 4; i++) { |
| 95 | *buf++ = tmp; |
| 96 | tmp >>= 8; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int denali_send_pipeline_cmd(int page, int ecc_en, int access_type) |
| 102 | { |
| 103 | uint32_t addr, cmd; |
| 104 | static uint32_t page_count = 1; |
| 105 | |
| 106 | writel(ecc_en, denali_flash_reg + ECC_ENABLE); |
| 107 | |
| 108 | /* clear all bits of intr_status. */ |
| 109 | writel(0xffff, denali_flash_reg + INTR_STATUS(flash_bank)); |
| 110 | |
| 111 | addr = BANK(flash_bank) | page; |
| 112 | |
| 113 | /* setup the acccess type */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 114 | cmd = DENALI_MAP10 | addr; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 115 | index_addr(cmd, access_type); |
| 116 | |
| 117 | /* setup the pipeline command */ |
| 118 | index_addr(cmd, PIPELINE_ACCESS | page_count); |
| 119 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 120 | cmd = DENALI_MAP01 | addr; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 121 | writel(cmd, denali_flash_mem + INDEX_CTRL_REG); |
| 122 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 123 | return wait_for_irq(INTR__LOAD_COMP); |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int nand_read_oob(void *buf, int page) |
| 127 | { |
| 128 | int ret; |
| 129 | |
| 130 | ret = denali_send_pipeline_cmd(page, 0, SPARE_ACCESS); |
| 131 | if (ret < 0) |
| 132 | return ret; |
| 133 | |
| 134 | read_data_from_flash_mem(buf, oob_size); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int nand_read_page(void *buf, int page) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | ret = denali_send_pipeline_cmd(page, 1, MAIN_ACCESS); |
| 144 | if (ret < 0) |
| 145 | return ret; |
| 146 | |
| 147 | read_data_from_flash_mem(buf, page_size); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 152 | static int nand_block_isbad(void *buf, int block) |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 153 | { |
| 154 | int ret; |
| 155 | |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 156 | ret = nand_read_oob(buf, block * pages_per_block); |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 160 | return *((uint8_t *)buf + CONFIG_SYS_NAND_BAD_BLOCK_POS) != 0xff; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* nand_init() - initialize data to make nand usable by SPL */ |
| 164 | void nand_init(void) |
| 165 | { |
| 166 | /* access to main area */ |
| 167 | writel(0, denali_flash_reg + TRANSFER_SPARE_REG); |
| 168 | |
| 169 | /* |
| 170 | * These registers are expected to be already set by the hardware |
| 171 | * or earlier boot code. So we read these values out. |
| 172 | */ |
| 173 | page_size = readl(denali_flash_reg + DEVICE_MAIN_AREA_SIZE); |
| 174 | oob_size = readl(denali_flash_reg + DEVICE_SPARE_AREA_SIZE); |
| 175 | pages_per_block = readl(denali_flash_reg + PAGES_PER_BLOCK); |
| 176 | } |
| 177 | |
| 178 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst) |
| 179 | { |
| 180 | int block, page, column, readlen; |
| 181 | int ret; |
| 182 | int force_bad_block_check = 1; |
| 183 | |
| 184 | page = offs / page_size; |
| 185 | column = offs % page_size; |
| 186 | |
| 187 | block = page / pages_per_block; |
| 188 | page = page % pages_per_block; |
| 189 | |
| 190 | while (size) { |
| 191 | if (force_bad_block_check || page == 0) { |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 192 | ret = nand_block_isbad(dst, block); |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 193 | if (ret < 0) |
| 194 | return ret; |
| 195 | |
| 196 | if (ret) { |
| 197 | block++; |
| 198 | continue; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | force_bad_block_check = 0; |
| 203 | |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 204 | ret = nand_read_page(dst, block * pages_per_block + page); |
| 205 | if (ret < 0) |
| 206 | return ret; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 207 | |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 208 | readlen = min(page_size - column, (int)size); |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 209 | |
Masahiro Yamada | 06b4781 | 2015-08-27 18:52:36 +0900 | [diff] [blame] | 210 | if (unlikely(column)) { |
| 211 | /* Partial page read */ |
| 212 | memmove(dst, dst + column, readlen); |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 213 | column = 0; |
Masahiro Yamada | d182d54 | 2014-10-03 19:21:04 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | size -= readlen; |
| 217 | dst += readlen; |
| 218 | page++; |
| 219 | if (page == pages_per_block) { |
| 220 | block++; |
| 221 | page = 0; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | void nand_deselect(void) {} |