Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Panasonic Corporation |
| 4 | * Copyright (C) 2013-2014, Altera Corporation <www.altera.com> |
| 5 | * Copyright (C) 2009-2010, Intel Corporation and its suppliers. |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 8 | #include <dm.h> |
Masahiro Yamada | 5beb1b3 | 2017-11-30 13:45:27 +0900 | [diff] [blame] | 9 | #include <nand.h> |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 10 | #include <linux/bitfield.h> |
| 11 | #include <linux/dma-direction.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 13 | #include <linux/io.h> |
Masahiro Yamada | 5beb1b3 | 2017-11-30 13:45:27 +0900 | [diff] [blame] | 14 | #include <linux/mtd/mtd.h> |
| 15 | #include <linux/mtd/rawnand.h> |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 16 | |
| 17 | #include "denali.h" |
| 18 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 19 | static dma_addr_t dma_map_single(void *dev, void *ptr, size_t size, |
| 20 | enum dma_data_direction dir) |
| 21 | { |
| 22 | unsigned long addr = (unsigned long)ptr; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 23 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 24 | if (dir == DMA_FROM_DEVICE) |
| 25 | invalidate_dcache_range(addr, addr + size); |
| 26 | else |
| 27 | flush_dcache_range(addr, addr + size); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 28 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 29 | return addr; |
| 30 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 31 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 32 | static void dma_unmap_single(void *dev, dma_addr_t addr, size_t size, |
| 33 | enum dma_data_direction dir) |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 34 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 35 | if (dir != DMA_TO_DEVICE) |
| 36 | invalidate_dcache_range(addr, addr + size); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 37 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 38 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 39 | static int dma_mapping_error(void *dev, dma_addr_t addr) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 40 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 41 | return 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 44 | #define DENALI_NAND_NAME "denali-nand" |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 45 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 46 | /* for Indexed Addressing */ |
| 47 | #define DENALI_INDEXED_CTRL 0x00 |
| 48 | #define DENALI_INDEXED_DATA 0x10 |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 49 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 50 | #define DENALI_MAP00 (0 << 26) /* direct access to buffer */ |
| 51 | #define DENALI_MAP01 (1 << 26) /* read/write pages in PIO */ |
| 52 | #define DENALI_MAP10 (2 << 26) /* high-level control plane */ |
| 53 | #define DENALI_MAP11 (3 << 26) /* direct controller access */ |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 54 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 55 | /* MAP11 access cycle type */ |
| 56 | #define DENALI_MAP11_CMD ((DENALI_MAP11) | 0) /* command cycle */ |
| 57 | #define DENALI_MAP11_ADDR ((DENALI_MAP11) | 1) /* address cycle */ |
| 58 | #define DENALI_MAP11_DATA ((DENALI_MAP11) | 2) /* data cycle */ |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 59 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 60 | /* MAP10 commands */ |
| 61 | #define DENALI_ERASE 0x01 |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 62 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 63 | #define DENALI_BANK(denali) ((denali)->active_bank << 24) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 64 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 65 | #define DENALI_INVALID_BANK -1 |
| 66 | #define DENALI_NR_BANKS 4 |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 67 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 68 | /* |
| 69 | * The bus interface clock, clk_x, is phase aligned with the core clock. The |
| 70 | * clk_x is an integral multiple N of the core clk. The value N is configured |
| 71 | * at IP delivery time, and its available value is 4, 5, or 6. We need to align |
| 72 | * to the largest value to make it work with any possible configuration. |
| 73 | */ |
| 74 | #define DENALI_CLK_X_MULT 6 |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 75 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 76 | static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 77 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 78 | return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 82 | * Direct Addressing - the slave address forms the control information (command |
| 83 | * type, bank, block, and page address). The slave data is the actual data to |
| 84 | * be transferred. This mode requires 28 bits of address region allocated. |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 85 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 86 | static u32 denali_direct_read(struct denali_nand_info *denali, u32 addr) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 87 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 88 | return ioread32(denali->host + addr); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 91 | static void denali_direct_write(struct denali_nand_info *denali, u32 addr, |
| 92 | u32 data) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 93 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 94 | iowrite32(data, denali->host + addr); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 97 | /* |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 98 | * Indexed Addressing - address translation module intervenes in passing the |
| 99 | * control information. This mode reduces the required address range. The |
| 100 | * control information and transferred data are latched by the registers in |
| 101 | * the translation module. |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 102 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 103 | static u32 denali_indexed_read(struct denali_nand_info *denali, u32 addr) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 104 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 105 | iowrite32(addr, denali->host + DENALI_INDEXED_CTRL); |
| 106 | return ioread32(denali->host + DENALI_INDEXED_DATA); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 109 | static void denali_indexed_write(struct denali_nand_info *denali, u32 addr, |
| 110 | u32 data) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 111 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 112 | iowrite32(addr, denali->host + DENALI_INDEXED_CTRL); |
| 113 | iowrite32(data, denali->host + DENALI_INDEXED_DATA); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 116 | /* |
| 117 | * Use the configuration feature register to determine the maximum number of |
| 118 | * banks that the hardware supports. |
| 119 | */ |
| 120 | static void denali_detect_max_banks(struct denali_nand_info *denali) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 121 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 122 | uint32_t features = ioread32(denali->reg + FEATURES); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 123 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 124 | denali->max_banks = 1 << FIELD_GET(FEATURES__N_BANKS, features); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 125 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 126 | /* the encoding changed from rev 5.0 to 5.1 */ |
| 127 | if (denali->revision < 0x0501) |
| 128 | denali->max_banks <<= 1; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 131 | static void __maybe_unused denali_enable_irq(struct denali_nand_info *denali) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 132 | { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 133 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 134 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 135 | for (i = 0; i < DENALI_NR_BANKS; i++) |
| 136 | iowrite32(U32_MAX, denali->reg + INTR_EN(i)); |
| 137 | iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 140 | static void __maybe_unused denali_disable_irq(struct denali_nand_info *denali) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 141 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 142 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 143 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 144 | for (i = 0; i < DENALI_NR_BANKS; i++) |
| 145 | iowrite32(0, denali->reg + INTR_EN(i)); |
| 146 | iowrite32(0, denali->reg + GLOBAL_INT_ENABLE); |
| 147 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 148 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 149 | static void denali_clear_irq(struct denali_nand_info *denali, |
| 150 | int bank, uint32_t irq_status) |
| 151 | { |
| 152 | /* write one to clear bits */ |
| 153 | iowrite32(irq_status, denali->reg + INTR_STATUS(bank)); |
| 154 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 155 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 156 | static void denali_clear_irq_all(struct denali_nand_info *denali) |
| 157 | { |
| 158 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 159 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 160 | for (i = 0; i < DENALI_NR_BANKS; i++) |
| 161 | denali_clear_irq(denali, i, U32_MAX); |
| 162 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 163 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 164 | static void __denali_check_irq(struct denali_nand_info *denali) |
| 165 | { |
| 166 | uint32_t irq_status; |
| 167 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 168 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 169 | for (i = 0; i < DENALI_NR_BANKS; i++) { |
| 170 | irq_status = ioread32(denali->reg + INTR_STATUS(i)); |
| 171 | denali_clear_irq(denali, i, irq_status); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 172 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 173 | if (i != denali->active_bank) |
| 174 | continue; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 175 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 176 | denali->irq_status |= irq_status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 177 | } |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 178 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 179 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 180 | static void denali_reset_irq(struct denali_nand_info *denali) |
| 181 | { |
| 182 | denali->irq_status = 0; |
| 183 | denali->irq_mask = 0; |
| 184 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 185 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 186 | static uint32_t denali_wait_for_irq(struct denali_nand_info *denali, |
| 187 | uint32_t irq_mask) |
| 188 | { |
| 189 | unsigned long time_left = 1000000; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 190 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 191 | while (time_left) { |
| 192 | __denali_check_irq(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 193 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 194 | if (irq_mask & denali->irq_status) |
| 195 | return denali->irq_status; |
| 196 | udelay(1); |
| 197 | time_left--; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 200 | if (!time_left) { |
| 201 | dev_err(denali->dev, "timeout while waiting for irq 0x%x\n", |
| 202 | irq_mask); |
| 203 | return 0; |
| 204 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 205 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 206 | return denali->irq_status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 209 | static uint32_t denali_check_irq(struct denali_nand_info *denali) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 210 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 211 | __denali_check_irq(denali); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 212 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 213 | return denali->irq_status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 216 | static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 217 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 218 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 219 | u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali); |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < len; i++) |
| 223 | buf[i] = denali->host_read(denali, addr); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 226 | static void denali_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 227 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 228 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 229 | u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali); |
| 230 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 231 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 232 | for (i = 0; i < len; i++) |
| 233 | denali->host_write(denali, addr, buf[i]); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 236 | static void denali_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 237 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 238 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 239 | u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali); |
| 240 | uint16_t *buf16 = (uint16_t *)buf; |
| 241 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 242 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 243 | for (i = 0; i < len / 2; i++) |
| 244 | buf16[i] = denali->host_read(denali, addr); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 247 | static void denali_write_buf16(struct mtd_info *mtd, const uint8_t *buf, |
| 248 | int len) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 249 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 250 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 251 | u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali); |
| 252 | const uint16_t *buf16 = (const uint16_t *)buf; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 253 | int i; |
| 254 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 255 | for (i = 0; i < len / 2; i++) |
| 256 | denali->host_write(denali, addr, buf16[i]); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 259 | static uint8_t denali_read_byte(struct mtd_info *mtd) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 260 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 261 | uint8_t byte; |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 262 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 263 | denali_read_buf(mtd, &byte, 1); |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 264 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 265 | return byte; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 268 | static void denali_write_byte(struct mtd_info *mtd, uint8_t byte) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 269 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 270 | denali_write_buf(mtd, &byte, 1); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 273 | static uint16_t denali_read_word(struct mtd_info *mtd) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 274 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 275 | uint16_t word; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 276 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 277 | denali_read_buf16(mtd, (uint8_t *)&word, 2); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 278 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 279 | return word; |
| 280 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 281 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 282 | static void denali_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl) |
| 283 | { |
| 284 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 285 | uint32_t type; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 286 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 287 | if (ctrl & NAND_CLE) |
| 288 | type = DENALI_MAP11_CMD; |
| 289 | else if (ctrl & NAND_ALE) |
| 290 | type = DENALI_MAP11_ADDR; |
| 291 | else |
| 292 | return; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 293 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 294 | /* |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 295 | * Some commands are followed by chip->dev_ready or chip->waitfunc. |
| 296 | * irq_status must be cleared here to catch the R/B# interrupt later. |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 297 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 298 | if (ctrl & NAND_CTRL_CHANGE) |
| 299 | denali_reset_irq(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 300 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 301 | denali->host_write(denali, DENALI_BANK(denali) | type, dat); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 304 | static int denali_dev_ready(struct mtd_info *mtd) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 305 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 306 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 307 | |
| 308 | return !!(denali_check_irq(denali) & INTR__INT_ACT); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 311 | static int denali_check_erased_page(struct mtd_info *mtd, |
| 312 | struct nand_chip *chip, uint8_t *buf, |
| 313 | unsigned long uncor_ecc_flags, |
| 314 | unsigned int max_bitflips) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 315 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 316 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 317 | int ecc_steps = chip->ecc.steps; |
| 318 | int ecc_size = chip->ecc.size; |
| 319 | int ecc_bytes = chip->ecc.bytes; |
| 320 | int i, ret, stat; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 321 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 322 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 323 | chip->ecc.total); |
| 324 | if (ret) |
| 325 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 326 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 327 | for (i = 0; i < ecc_steps; i++) { |
| 328 | if (!(uncor_ecc_flags & BIT(i))) |
| 329 | continue; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 330 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 331 | stat = nand_check_erased_ecc_chunk(buf, ecc_size, |
| 332 | ecc_code, ecc_bytes, |
| 333 | NULL, 0, |
| 334 | chip->ecc.strength); |
| 335 | if (stat < 0) { |
| 336 | mtd->ecc_stats.failed++; |
| 337 | } else { |
| 338 | mtd->ecc_stats.corrected += stat; |
| 339 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 340 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 341 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 342 | buf += ecc_size; |
| 343 | ecc_code += ecc_bytes; |
| 344 | } |
| 345 | |
| 346 | return max_bitflips; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 349 | static int denali_hw_ecc_fixup(struct mtd_info *mtd, |
| 350 | struct denali_nand_info *denali, |
| 351 | unsigned long *uncor_ecc_flags) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 352 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 353 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 354 | int bank = denali->active_bank; |
| 355 | uint32_t ecc_cor; |
| 356 | unsigned int max_bitflips; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 357 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 358 | ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank)); |
| 359 | ecc_cor >>= ECC_COR_INFO__SHIFT(bank); |
| 360 | |
| 361 | if (ecc_cor & ECC_COR_INFO__UNCOR_ERR) { |
| 362 | /* |
| 363 | * This flag is set when uncorrectable error occurs at least in |
| 364 | * one ECC sector. We can not know "how many sectors", or |
| 365 | * "which sector(s)". We need erase-page check for all sectors. |
| 366 | */ |
| 367 | *uncor_ecc_flags = GENMASK(chip->ecc.steps - 1, 0); |
| 368 | return 0; |
| 369 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 370 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 371 | max_bitflips = FIELD_GET(ECC_COR_INFO__MAX_ERRORS, ecc_cor); |
| 372 | |
| 373 | /* |
| 374 | * The register holds the maximum of per-sector corrected bitflips. |
| 375 | * This is suitable for the return value of the ->read_page() callback. |
| 376 | * Unfortunately, we can not know the total number of corrected bits in |
| 377 | * the page. Increase the stats by max_bitflips. (compromised solution) |
| 378 | */ |
| 379 | mtd->ecc_stats.corrected += max_bitflips; |
| 380 | |
| 381 | return max_bitflips; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 384 | static int denali_sw_ecc_fixup(struct mtd_info *mtd, |
| 385 | struct denali_nand_info *denali, |
| 386 | unsigned long *uncor_ecc_flags, uint8_t *buf) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 387 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 388 | unsigned int ecc_size = denali->nand.ecc.size; |
| 389 | unsigned int bitflips = 0; |
| 390 | unsigned int max_bitflips = 0; |
| 391 | uint32_t err_addr, err_cor_info; |
| 392 | unsigned int err_byte, err_sector, err_device; |
| 393 | uint8_t err_cor_value; |
| 394 | unsigned int prev_sector = 0; |
| 395 | uint32_t irq_status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 396 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 397 | denali_reset_irq(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 398 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 399 | do { |
| 400 | err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS); |
| 401 | err_sector = FIELD_GET(ECC_ERROR_ADDRESS__SECTOR, err_addr); |
| 402 | err_byte = FIELD_GET(ECC_ERROR_ADDRESS__OFFSET, err_addr); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 403 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 404 | err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO); |
| 405 | err_cor_value = FIELD_GET(ERR_CORRECTION_INFO__BYTE, |
| 406 | err_cor_info); |
| 407 | err_device = FIELD_GET(ERR_CORRECTION_INFO__DEVICE, |
| 408 | err_cor_info); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 409 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 410 | /* reset the bitflip counter when crossing ECC sector */ |
| 411 | if (err_sector != prev_sector) |
| 412 | bitflips = 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 413 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 414 | if (err_cor_info & ERR_CORRECTION_INFO__UNCOR) { |
| 415 | /* |
| 416 | * Check later if this is a real ECC error, or |
| 417 | * an erased sector. |
| 418 | */ |
| 419 | *uncor_ecc_flags |= BIT(err_sector); |
| 420 | } else if (err_byte < ecc_size) { |
| 421 | /* |
| 422 | * If err_byte is larger than ecc_size, means error |
| 423 | * happened in OOB, so we ignore it. It's no need for |
| 424 | * us to correct it err_device is represented the NAND |
| 425 | * error bits are happened in if there are more than |
| 426 | * one NAND connected. |
| 427 | */ |
| 428 | int offset; |
| 429 | unsigned int flips_in_byte; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 430 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 431 | offset = (err_sector * ecc_size + err_byte) * |
| 432 | denali->devs_per_cs + err_device; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 433 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 434 | /* correct the ECC error */ |
| 435 | flips_in_byte = hweight8(buf[offset] ^ err_cor_value); |
| 436 | buf[offset] ^= err_cor_value; |
| 437 | mtd->ecc_stats.corrected += flips_in_byte; |
| 438 | bitflips += flips_in_byte; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 439 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 440 | max_bitflips = max(max_bitflips, bitflips); |
| 441 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 442 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 443 | prev_sector = err_sector; |
| 444 | } while (!(err_cor_info & ERR_CORRECTION_INFO__LAST_ERR)); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 445 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 446 | /* |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 447 | * Once handle all ECC errors, controller will trigger an |
| 448 | * ECC_TRANSACTION_DONE interrupt. |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 449 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 450 | irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE); |
| 451 | if (!(irq_status & INTR__ECC_TRANSACTION_DONE)) |
| 452 | return -EIO; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 453 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 454 | return max_bitflips; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 457 | static void denali_setup_dma64(struct denali_nand_info *denali, |
| 458 | dma_addr_t dma_addr, int page, int write) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 459 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 460 | uint32_t mode; |
| 461 | const int page_count = 1; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 462 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 463 | mode = DENALI_MAP10 | DENALI_BANK(denali) | page; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 464 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 465 | /* DMA is a three step process */ |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 466 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 467 | /* |
| 468 | * 1. setup transfer type, interrupt when complete, |
| 469 | * burst len = 64 bytes, the number of pages |
| 470 | */ |
| 471 | denali->host_write(denali, mode, |
| 472 | 0x01002000 | (64 << 16) | (write << 8) | page_count); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 473 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 474 | /* 2. set memory low address */ |
| 475 | denali->host_write(denali, mode, lower_32_bits(dma_addr)); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 476 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 477 | /* 3. set memory high address */ |
| 478 | denali->host_write(denali, mode, upper_32_bits(dma_addr)); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 481 | static void denali_setup_dma32(struct denali_nand_info *denali, |
| 482 | dma_addr_t dma_addr, int page, int write) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 483 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 484 | uint32_t mode; |
| 485 | const int page_count = 1; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 486 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 487 | mode = DENALI_MAP10 | DENALI_BANK(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 488 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 489 | /* DMA is a four step process */ |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 490 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 491 | /* 1. setup transfer type and # of pages */ |
| 492 | denali->host_write(denali, mode | page, |
| 493 | 0x2000 | (write << 8) | page_count); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 494 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 495 | /* 2. set memory high address bits 23:8 */ |
| 496 | denali->host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 497 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 498 | /* 3. set memory low address bits 23:8 */ |
| 499 | denali->host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 500 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 501 | /* 4. interrupt when complete, burst len = 64 bytes */ |
| 502 | denali->host_write(denali, mode | 0x14000, 0x2400); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 503 | } |
| 504 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 505 | static int denali_pio_read(struct denali_nand_info *denali, void *buf, |
| 506 | size_t size, int page, int raw) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 507 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 508 | u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page; |
| 509 | uint32_t *buf32 = (uint32_t *)buf; |
| 510 | uint32_t irq_status, ecc_err_mask; |
| 511 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 512 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 513 | if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) |
| 514 | ecc_err_mask = INTR__ECC_UNCOR_ERR; |
| 515 | else |
| 516 | ecc_err_mask = INTR__ECC_ERR; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 517 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 518 | denali_reset_irq(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 519 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 520 | for (i = 0; i < size / 4; i++) |
| 521 | *buf32++ = denali->host_read(denali, addr); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 522 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 523 | irq_status = denali_wait_for_irq(denali, INTR__PAGE_XFER_INC); |
| 524 | if (!(irq_status & INTR__PAGE_XFER_INC)) |
| 525 | return -EIO; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 526 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 527 | if (irq_status & INTR__ERASED_PAGE) |
| 528 | memset(buf, 0xff, size); |
| 529 | |
| 530 | return irq_status & ecc_err_mask ? -EBADMSG : 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 531 | } |
| 532 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 533 | static int denali_pio_write(struct denali_nand_info *denali, |
| 534 | const void *buf, size_t size, int page, int raw) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 535 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 536 | u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page; |
| 537 | const uint32_t *buf32 = (uint32_t *)buf; |
| 538 | uint32_t irq_status; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 539 | int i; |
| 540 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 541 | denali_reset_irq(denali); |
| 542 | |
| 543 | for (i = 0; i < size / 4; i++) |
| 544 | denali->host_write(denali, addr, *buf32++); |
| 545 | |
| 546 | irq_status = denali_wait_for_irq(denali, |
| 547 | INTR__PROGRAM_COMP | INTR__PROGRAM_FAIL); |
| 548 | if (!(irq_status & INTR__PROGRAM_COMP)) |
| 549 | return -EIO; |
| 550 | |
| 551 | return 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 552 | } |
| 553 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 554 | static int denali_pio_xfer(struct denali_nand_info *denali, void *buf, |
| 555 | size_t size, int page, int raw, int write) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 556 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 557 | if (write) |
| 558 | return denali_pio_write(denali, buf, size, page, raw); |
| 559 | else |
| 560 | return denali_pio_read(denali, buf, size, page, raw); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 561 | } |
| 562 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 563 | static int denali_dma_xfer(struct denali_nand_info *denali, void *buf, |
| 564 | size_t size, int page, int raw, int write) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 565 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 566 | dma_addr_t dma_addr; |
| 567 | uint32_t irq_mask, irq_status, ecc_err_mask; |
| 568 | enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
| 569 | int ret = 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 570 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 571 | dma_addr = dma_map_single(denali->dev, buf, size, dir); |
| 572 | if (dma_mapping_error(denali->dev, dma_addr)) { |
| 573 | dev_dbg(denali->dev, "Failed to DMA-map buffer. Trying PIO.\n"); |
| 574 | return denali_pio_xfer(denali, buf, size, page, raw, write); |
| 575 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 576 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 577 | if (write) { |
| 578 | /* |
| 579 | * INTR__PROGRAM_COMP is never asserted for the DMA transfer. |
| 580 | * We can use INTR__DMA_CMD_COMP instead. This flag is asserted |
| 581 | * when the page program is completed. |
| 582 | */ |
| 583 | irq_mask = INTR__DMA_CMD_COMP | INTR__PROGRAM_FAIL; |
| 584 | ecc_err_mask = 0; |
| 585 | } else if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) { |
| 586 | irq_mask = INTR__DMA_CMD_COMP; |
| 587 | ecc_err_mask = INTR__ECC_UNCOR_ERR; |
| 588 | } else { |
| 589 | irq_mask = INTR__DMA_CMD_COMP; |
| 590 | ecc_err_mask = INTR__ECC_ERR; |
| 591 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 592 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 593 | iowrite32(DMA_ENABLE__FLAG, denali->reg + DMA_ENABLE); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 594 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 595 | denali_reset_irq(denali); |
| 596 | denali->setup_dma(denali, dma_addr, page, write); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 597 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 598 | irq_status = denali_wait_for_irq(denali, irq_mask); |
| 599 | if (!(irq_status & INTR__DMA_CMD_COMP)) |
| 600 | ret = -EIO; |
| 601 | else if (irq_status & ecc_err_mask) |
| 602 | ret = -EBADMSG; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 603 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 604 | iowrite32(0, denali->reg + DMA_ENABLE); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 605 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 606 | dma_unmap_single(denali->dev, dma_addr, size, dir); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 607 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 608 | if (irq_status & INTR__ERASED_PAGE) |
| 609 | memset(buf, 0xff, size); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 610 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 611 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 614 | static int denali_data_xfer(struct denali_nand_info *denali, void *buf, |
| 615 | size_t size, int page, int raw, int write) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 616 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 617 | iowrite32(raw ? 0 : ECC_ENABLE__FLAG, denali->reg + ECC_ENABLE); |
| 618 | iowrite32(raw ? TRANSFER_SPARE_REG__FLAG : 0, |
| 619 | denali->reg + TRANSFER_SPARE_REG); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 620 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 621 | if (denali->dma_avail) |
| 622 | return denali_dma_xfer(denali, buf, size, page, raw, write); |
| 623 | else |
| 624 | return denali_pio_xfer(denali, buf, size, page, raw, write); |
| 625 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 626 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 627 | static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip, |
| 628 | int page, int write) |
| 629 | { |
| 630 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 631 | unsigned int start_cmd = write ? NAND_CMD_SEQIN : NAND_CMD_READ0; |
| 632 | unsigned int rnd_cmd = write ? NAND_CMD_RNDIN : NAND_CMD_RNDOUT; |
| 633 | int writesize = mtd->writesize; |
| 634 | int oobsize = mtd->oobsize; |
| 635 | uint8_t *bufpoi = chip->oob_poi; |
| 636 | int ecc_steps = chip->ecc.steps; |
| 637 | int ecc_size = chip->ecc.size; |
| 638 | int ecc_bytes = chip->ecc.bytes; |
| 639 | int oob_skip = denali->oob_skip_bytes; |
| 640 | size_t size = writesize + oobsize; |
| 641 | int i, pos, len; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 642 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 643 | /* BBM at the beginning of the OOB area */ |
| 644 | chip->cmdfunc(mtd, start_cmd, writesize, page); |
| 645 | if (write) |
| 646 | chip->write_buf(mtd, bufpoi, oob_skip); |
| 647 | else |
| 648 | chip->read_buf(mtd, bufpoi, oob_skip); |
| 649 | bufpoi += oob_skip; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 650 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 651 | /* OOB ECC */ |
| 652 | for (i = 0; i < ecc_steps; i++) { |
| 653 | pos = ecc_size + i * (ecc_size + ecc_bytes); |
| 654 | len = ecc_bytes; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 655 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 656 | if (pos >= writesize) |
| 657 | pos += oob_skip; |
| 658 | else if (pos + len > writesize) |
| 659 | len = writesize - pos; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 660 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 661 | chip->cmdfunc(mtd, rnd_cmd, pos, -1); |
| 662 | if (write) |
| 663 | chip->write_buf(mtd, bufpoi, len); |
| 664 | else |
| 665 | chip->read_buf(mtd, bufpoi, len); |
| 666 | bufpoi += len; |
| 667 | if (len < ecc_bytes) { |
| 668 | len = ecc_bytes - len; |
| 669 | chip->cmdfunc(mtd, rnd_cmd, writesize + oob_skip, -1); |
| 670 | if (write) |
| 671 | chip->write_buf(mtd, bufpoi, len); |
| 672 | else |
| 673 | chip->read_buf(mtd, bufpoi, len); |
| 674 | bufpoi += len; |
| 675 | } |
| 676 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 677 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 678 | /* OOB free */ |
| 679 | len = oobsize - (bufpoi - chip->oob_poi); |
| 680 | chip->cmdfunc(mtd, rnd_cmd, size - len, -1); |
| 681 | if (write) |
| 682 | chip->write_buf(mtd, bufpoi, len); |
| 683 | else |
| 684 | chip->read_buf(mtd, bufpoi, len); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 685 | } |
| 686 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 687 | static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 688 | uint8_t *buf, int oob_required, int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 689 | { |
| 690 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 691 | int writesize = mtd->writesize; |
| 692 | int oobsize = mtd->oobsize; |
| 693 | int ecc_steps = chip->ecc.steps; |
| 694 | int ecc_size = chip->ecc.size; |
| 695 | int ecc_bytes = chip->ecc.bytes; |
| 696 | void *tmp_buf = denali->buf; |
| 697 | int oob_skip = denali->oob_skip_bytes; |
| 698 | size_t size = writesize + oobsize; |
| 699 | int ret, i, pos, len; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 700 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 701 | ret = denali_data_xfer(denali, tmp_buf, size, page, 1, 0); |
| 702 | if (ret) |
| 703 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 704 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 705 | /* Arrange the buffer for syndrome payload/ecc layout */ |
| 706 | if (buf) { |
| 707 | for (i = 0; i < ecc_steps; i++) { |
| 708 | pos = i * (ecc_size + ecc_bytes); |
| 709 | len = ecc_size; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 710 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 711 | if (pos >= writesize) |
| 712 | pos += oob_skip; |
| 713 | else if (pos + len > writesize) |
| 714 | len = writesize - pos; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 715 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 716 | memcpy(buf, tmp_buf + pos, len); |
| 717 | buf += len; |
| 718 | if (len < ecc_size) { |
| 719 | len = ecc_size - len; |
| 720 | memcpy(buf, tmp_buf + writesize + oob_skip, |
| 721 | len); |
| 722 | buf += len; |
| 723 | } |
| 724 | } |
| 725 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 726 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 727 | if (oob_required) { |
| 728 | uint8_t *oob = chip->oob_poi; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 729 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 730 | /* BBM at the beginning of the OOB area */ |
| 731 | memcpy(oob, tmp_buf + writesize, oob_skip); |
| 732 | oob += oob_skip; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 733 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 734 | /* OOB ECC */ |
| 735 | for (i = 0; i < ecc_steps; i++) { |
| 736 | pos = ecc_size + i * (ecc_size + ecc_bytes); |
| 737 | len = ecc_bytes; |
| 738 | |
| 739 | if (pos >= writesize) |
| 740 | pos += oob_skip; |
| 741 | else if (pos + len > writesize) |
| 742 | len = writesize - pos; |
| 743 | |
| 744 | memcpy(oob, tmp_buf + pos, len); |
| 745 | oob += len; |
| 746 | if (len < ecc_bytes) { |
| 747 | len = ecc_bytes - len; |
| 748 | memcpy(oob, tmp_buf + writesize + oob_skip, |
| 749 | len); |
| 750 | oob += len; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /* OOB free */ |
| 755 | len = oobsize - (oob - chip->oob_poi); |
| 756 | memcpy(oob, tmp_buf + size - len, len); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 757 | } |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 758 | |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 762 | static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip, |
| 763 | int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 764 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 765 | denali_oob_xfer(mtd, chip, page, 0); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 766 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 767 | return 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 768 | } |
| 769 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 770 | static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip, |
| 771 | int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 772 | { |
| 773 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 774 | int status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 775 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 776 | denali_reset_irq(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 777 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 778 | denali_oob_xfer(mtd, chip, page, 1); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 779 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 780 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 781 | status = chip->waitfunc(mtd, chip); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 782 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 783 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 784 | } |
| 785 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 786 | static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 787 | uint8_t *buf, int oob_required, int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 788 | { |
| 789 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 790 | unsigned long uncor_ecc_flags = 0; |
| 791 | int stat = 0; |
| 792 | int ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 793 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 794 | ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0); |
| 795 | if (ret && ret != -EBADMSG) |
| 796 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 797 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 798 | if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) |
| 799 | stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags); |
| 800 | else if (ret == -EBADMSG) |
| 801 | stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 802 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 803 | if (stat < 0) |
| 804 | return stat; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 805 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 806 | if (uncor_ecc_flags) { |
| 807 | ret = denali_read_oob(mtd, chip, page); |
| 808 | if (ret) |
| 809 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 810 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 811 | stat = denali_check_erased_page(mtd, chip, buf, |
| 812 | uncor_ecc_flags, stat); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 815 | return stat; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 816 | } |
| 817 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 818 | static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 819 | const uint8_t *buf, int oob_required, int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 820 | { |
| 821 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 822 | int writesize = mtd->writesize; |
| 823 | int oobsize = mtd->oobsize; |
| 824 | int ecc_steps = chip->ecc.steps; |
| 825 | int ecc_size = chip->ecc.size; |
| 826 | int ecc_bytes = chip->ecc.bytes; |
| 827 | void *tmp_buf = denali->buf; |
| 828 | int oob_skip = denali->oob_skip_bytes; |
| 829 | size_t size = writesize + oobsize; |
| 830 | int i, pos, len; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 831 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 832 | /* |
| 833 | * Fill the buffer with 0xff first except the full page transfer. |
| 834 | * This simplifies the logic. |
| 835 | */ |
| 836 | if (!buf || !oob_required) |
| 837 | memset(tmp_buf, 0xff, size); |
| 838 | |
| 839 | /* Arrange the buffer for syndrome payload/ecc layout */ |
| 840 | if (buf) { |
| 841 | for (i = 0; i < ecc_steps; i++) { |
| 842 | pos = i * (ecc_size + ecc_bytes); |
| 843 | len = ecc_size; |
| 844 | |
| 845 | if (pos >= writesize) |
| 846 | pos += oob_skip; |
| 847 | else if (pos + len > writesize) |
| 848 | len = writesize - pos; |
| 849 | |
| 850 | memcpy(tmp_buf + pos, buf, len); |
| 851 | buf += len; |
| 852 | if (len < ecc_size) { |
| 853 | len = ecc_size - len; |
| 854 | memcpy(tmp_buf + writesize + oob_skip, buf, |
| 855 | len); |
| 856 | buf += len; |
| 857 | } |
| 858 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 859 | } |
| 860 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 861 | if (oob_required) { |
| 862 | const uint8_t *oob = chip->oob_poi; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 863 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 864 | /* BBM at the beginning of the OOB area */ |
| 865 | memcpy(tmp_buf + writesize, oob, oob_skip); |
| 866 | oob += oob_skip; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 867 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 868 | /* OOB ECC */ |
| 869 | for (i = 0; i < ecc_steps; i++) { |
| 870 | pos = ecc_size + i * (ecc_size + ecc_bytes); |
| 871 | len = ecc_bytes; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 872 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 873 | if (pos >= writesize) |
| 874 | pos += oob_skip; |
| 875 | else if (pos + len > writesize) |
| 876 | len = writesize - pos; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 877 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 878 | memcpy(tmp_buf + pos, oob, len); |
| 879 | oob += len; |
| 880 | if (len < ecc_bytes) { |
| 881 | len = ecc_bytes - len; |
| 882 | memcpy(tmp_buf + writesize + oob_skip, oob, |
| 883 | len); |
| 884 | oob += len; |
| 885 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 886 | } |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 887 | |
| 888 | /* OOB free */ |
| 889 | len = oobsize - (oob - chip->oob_poi); |
| 890 | memcpy(tmp_buf + size - len, oob, len); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 891 | } |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 892 | |
| 893 | return denali_data_xfer(denali, tmp_buf, size, page, 1, 1); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 894 | } |
| 895 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 896 | static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 897 | const uint8_t *buf, int oob_required, int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 898 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 899 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 900 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 901 | return denali_data_xfer(denali, (void *)buf, mtd->writesize, |
| 902 | page, 0, 1); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 903 | } |
| 904 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 905 | static void denali_select_chip(struct mtd_info *mtd, int chip) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 906 | { |
| 907 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 908 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 909 | denali->active_bank = chip; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 910 | } |
| 911 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 912 | static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 913 | { |
| 914 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 915 | uint32_t irq_status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 916 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 917 | /* R/B# pin transitioned from low to high? */ |
| 918 | irq_status = denali_wait_for_irq(denali, INTR__INT_ACT); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 919 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 920 | return irq_status & INTR__INT_ACT ? 0 : NAND_STATUS_FAIL; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 921 | } |
| 922 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 923 | static int denali_erase(struct mtd_info *mtd, int page) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 924 | { |
| 925 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 926 | uint32_t irq_status; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 927 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 928 | denali_reset_irq(denali); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 929 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 930 | denali->host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page, |
| 931 | DENALI_ERASE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 932 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 933 | /* wait for erase to complete or failure to occur */ |
| 934 | irq_status = denali_wait_for_irq(denali, |
| 935 | INTR__ERASE_COMP | INTR__ERASE_FAIL); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 936 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 937 | return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 938 | } |
| 939 | |
Masahiro Yamada | 59a1f3e | 2017-11-29 19:18:18 +0900 | [diff] [blame] | 940 | static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr, |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 941 | const struct nand_data_interface *conf) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 942 | { |
| 943 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 944 | const struct nand_sdr_timings *timings; |
| 945 | unsigned long t_clk; |
| 946 | int acc_clks, re_2_we, re_2_re, we_2_re, addr_2_data; |
| 947 | int rdwr_en_lo, rdwr_en_hi, rdwr_en_lo_hi, cs_setup; |
| 948 | int addr_2_data_mask; |
| 949 | uint32_t tmp; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 950 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 951 | timings = nand_get_sdr_timings(conf); |
| 952 | if (IS_ERR(timings)) |
| 953 | return PTR_ERR(timings); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 954 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 955 | /* clk_x period in picoseconds */ |
| 956 | t_clk = DIV_ROUND_DOWN_ULL(1000000000000ULL, denali->clk_x_rate); |
| 957 | if (!t_clk) |
| 958 | return -EINVAL; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 959 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 960 | if (chipnr == NAND_DATA_IFACE_CHECK_ONLY) |
| 961 | return 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 962 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 963 | /* tREA -> ACC_CLKS */ |
| 964 | acc_clks = DIV_ROUND_UP(timings->tREA_max, t_clk); |
| 965 | acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE); |
| 966 | |
| 967 | tmp = ioread32(denali->reg + ACC_CLKS); |
| 968 | tmp &= ~ACC_CLKS__VALUE; |
| 969 | tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks); |
| 970 | iowrite32(tmp, denali->reg + ACC_CLKS); |
| 971 | |
| 972 | /* tRWH -> RE_2_WE */ |
| 973 | re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_clk); |
| 974 | re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 975 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 976 | tmp = ioread32(denali->reg + RE_2_WE); |
| 977 | tmp &= ~RE_2_WE__VALUE; |
| 978 | tmp |= FIELD_PREP(RE_2_WE__VALUE, re_2_we); |
| 979 | iowrite32(tmp, denali->reg + RE_2_WE); |
| 980 | |
| 981 | /* tRHZ -> RE_2_RE */ |
| 982 | re_2_re = DIV_ROUND_UP(timings->tRHZ_max, t_clk); |
| 983 | re_2_re = min_t(int, re_2_re, RE_2_RE__VALUE); |
| 984 | |
| 985 | tmp = ioread32(denali->reg + RE_2_RE); |
| 986 | tmp &= ~RE_2_RE__VALUE; |
| 987 | tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re); |
| 988 | iowrite32(tmp, denali->reg + RE_2_RE); |
| 989 | |
| 990 | /* |
| 991 | * tCCS, tWHR -> WE_2_RE |
| 992 | * |
| 993 | * With WE_2_RE properly set, the Denali controller automatically takes |
| 994 | * care of the delay; the driver need not set NAND_WAIT_TCCS. |
| 995 | */ |
| 996 | we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min), |
| 997 | t_clk); |
| 998 | we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE); |
| 999 | |
| 1000 | tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE); |
| 1001 | tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE; |
| 1002 | tmp |= FIELD_PREP(TWHR2_AND_WE_2_RE__WE_2_RE, we_2_re); |
| 1003 | iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE); |
| 1004 | |
| 1005 | /* tADL -> ADDR_2_DATA */ |
| 1006 | |
| 1007 | /* for older versions, ADDR_2_DATA is only 6 bit wide */ |
| 1008 | addr_2_data_mask = TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA; |
| 1009 | if (denali->revision < 0x0501) |
| 1010 | addr_2_data_mask >>= 1; |
| 1011 | |
| 1012 | addr_2_data = DIV_ROUND_UP(timings->tADL_min, t_clk); |
| 1013 | addr_2_data = min_t(int, addr_2_data, addr_2_data_mask); |
| 1014 | |
| 1015 | tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA); |
| 1016 | tmp &= ~TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA; |
| 1017 | tmp |= FIELD_PREP(TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA, addr_2_data); |
| 1018 | iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA); |
| 1019 | |
| 1020 | /* tREH, tWH -> RDWR_EN_HI_CNT */ |
| 1021 | rdwr_en_hi = DIV_ROUND_UP(max(timings->tREH_min, timings->tWH_min), |
| 1022 | t_clk); |
| 1023 | rdwr_en_hi = min_t(int, rdwr_en_hi, RDWR_EN_HI_CNT__VALUE); |
| 1024 | |
| 1025 | tmp = ioread32(denali->reg + RDWR_EN_HI_CNT); |
| 1026 | tmp &= ~RDWR_EN_HI_CNT__VALUE; |
| 1027 | tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi); |
| 1028 | iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT); |
| 1029 | |
| 1030 | /* tRP, tWP -> RDWR_EN_LO_CNT */ |
| 1031 | rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min), |
| 1032 | t_clk); |
| 1033 | rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min), |
| 1034 | t_clk); |
| 1035 | rdwr_en_lo_hi = max(rdwr_en_lo_hi, DENALI_CLK_X_MULT); |
| 1036 | rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi); |
| 1037 | rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE); |
| 1038 | |
| 1039 | tmp = ioread32(denali->reg + RDWR_EN_LO_CNT); |
| 1040 | tmp &= ~RDWR_EN_LO_CNT__VALUE; |
| 1041 | tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo); |
| 1042 | iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT); |
| 1043 | |
| 1044 | /* tCS, tCEA -> CS_SETUP_CNT */ |
| 1045 | cs_setup = max3((int)DIV_ROUND_UP(timings->tCS_min, t_clk) - rdwr_en_lo, |
| 1046 | (int)DIV_ROUND_UP(timings->tCEA_max, t_clk) - acc_clks, |
| 1047 | 0); |
| 1048 | cs_setup = min_t(int, cs_setup, CS_SETUP_CNT__VALUE); |
| 1049 | |
| 1050 | tmp = ioread32(denali->reg + CS_SETUP_CNT); |
| 1051 | tmp &= ~CS_SETUP_CNT__VALUE; |
| 1052 | tmp |= FIELD_PREP(CS_SETUP_CNT__VALUE, cs_setup); |
| 1053 | iowrite32(tmp, denali->reg + CS_SETUP_CNT); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1054 | |
| 1055 | return 0; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1056 | } |
| 1057 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1058 | static void denali_reset_banks(struct denali_nand_info *denali) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1059 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1060 | u32 irq_status; |
| 1061 | int i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1062 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1063 | for (i = 0; i < denali->max_banks; i++) { |
| 1064 | denali->active_bank = i; |
| 1065 | |
| 1066 | denali_reset_irq(denali); |
| 1067 | |
| 1068 | iowrite32(DEVICE_RESET__BANK(i), |
| 1069 | denali->reg + DEVICE_RESET); |
| 1070 | |
| 1071 | irq_status = denali_wait_for_irq(denali, |
| 1072 | INTR__RST_COMP | INTR__INT_ACT | INTR__TIME_OUT); |
| 1073 | if (!(irq_status & INTR__INT_ACT)) |
| 1074 | break; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1075 | } |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1076 | |
| 1077 | dev_dbg(denali->dev, "%d chips connected\n", i); |
| 1078 | denali->max_banks = i; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1079 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1080 | |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1081 | static void denali_hw_init(struct denali_nand_info *denali) |
| 1082 | { |
| 1083 | /* |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 1084 | * The REVISION register may not be reliable. Platforms are allowed to |
| 1085 | * override it. |
| 1086 | */ |
| 1087 | if (!denali->revision) |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1088 | denali->revision = swab16(ioread32(denali->reg + REVISION)); |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 1089 | |
| 1090 | /* |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1091 | * tell driver how many bit controller will skip before writing |
| 1092 | * ECC code in OOB. This is normally used for bad block marker |
| 1093 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1094 | denali->oob_skip_bytes = CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES; |
| 1095 | iowrite32(denali->oob_skip_bytes, denali->reg + SPARE_AREA_SKIP_BYTES); |
| 1096 | denali_detect_max_banks(denali); |
| 1097 | iowrite32(0x0F, denali->reg + RB_PIN_ENABLED); |
| 1098 | iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1099 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1100 | iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1103 | int denali_calc_ecc_bytes(int step_size, int strength) |
| 1104 | { |
| 1105 | /* BCH code. Denali requires ecc.bytes to be multiple of 2 */ |
| 1106 | return DIV_ROUND_UP(strength * fls(step_size * 8), 16) * 2; |
| 1107 | } |
| 1108 | EXPORT_SYMBOL(denali_calc_ecc_bytes); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1109 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1110 | static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip, |
| 1111 | struct denali_nand_info *denali) |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1112 | { |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1113 | int oobavail = mtd->oobsize - denali->oob_skip_bytes; |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1114 | int ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1115 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1116 | /* |
| 1117 | * If .size and .strength are already set (usually by DT), |
| 1118 | * check if they are supported by this controller. |
| 1119 | */ |
| 1120 | if (chip->ecc.size && chip->ecc.strength) |
| 1121 | return nand_check_ecc_caps(chip, denali->ecc_caps, oobavail); |
| 1122 | |
| 1123 | /* |
| 1124 | * We want .size and .strength closest to the chip's requirement |
| 1125 | * unless NAND_ECC_MAXIMIZE is requested. |
| 1126 | */ |
| 1127 | if (!(chip->ecc.options & NAND_ECC_MAXIMIZE)) { |
| 1128 | ret = nand_match_ecc_req(chip, denali->ecc_caps, oobavail); |
| 1129 | if (!ret) |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /* Max ECC strength is the last thing we can do */ |
| 1134 | return nand_maximize_ecc(chip, denali->ecc_caps, oobavail); |
| 1135 | } |
| 1136 | |
| 1137 | static struct nand_ecclayout nand_oob; |
| 1138 | |
| 1139 | static int denali_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 1140 | struct mtd_oob_region *oobregion) |
| 1141 | { |
| 1142 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1143 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1144 | |
| 1145 | if (section) |
| 1146 | return -ERANGE; |
| 1147 | |
| 1148 | oobregion->offset = denali->oob_skip_bytes; |
| 1149 | oobregion->length = chip->ecc.total; |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static int denali_ooblayout_free(struct mtd_info *mtd, int section, |
| 1155 | struct mtd_oob_region *oobregion) |
| 1156 | { |
| 1157 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1158 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1159 | |
| 1160 | if (section) |
| 1161 | return -ERANGE; |
| 1162 | |
| 1163 | oobregion->offset = chip->ecc.total + denali->oob_skip_bytes; |
| 1164 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1168 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1169 | static const struct mtd_ooblayout_ops denali_ooblayout_ops = { |
| 1170 | .ecc = denali_ooblayout_ecc, |
| 1171 | .free = denali_ooblayout_free, |
| 1172 | }; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1173 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1174 | static int denali_multidev_fixup(struct denali_nand_info *denali) |
| 1175 | { |
| 1176 | struct nand_chip *chip = &denali->nand; |
| 1177 | struct mtd_info *mtd = nand_to_mtd(chip); |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1178 | |
| 1179 | /* |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1180 | * Support for multi device: |
| 1181 | * When the IP configuration is x16 capable and two x8 chips are |
| 1182 | * connected in parallel, DEVICES_CONNECTED should be set to 2. |
| 1183 | * In this case, the core framework knows nothing about this fact, |
| 1184 | * so we should tell it the _logical_ pagesize and anything necessary. |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1185 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1186 | denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED); |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1187 | |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1188 | /* |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1189 | * On some SoCs, DEVICES_CONNECTED is not auto-detected. |
| 1190 | * For those, DEVICES_CONNECTED is left to 0. Set 1 if it is the case. |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1191 | */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1192 | if (denali->devs_per_cs == 0) { |
| 1193 | denali->devs_per_cs = 1; |
| 1194 | iowrite32(1, denali->reg + DEVICES_CONNECTED); |
| 1195 | } |
| 1196 | |
| 1197 | if (denali->devs_per_cs == 1) |
| 1198 | return 0; |
| 1199 | |
| 1200 | if (denali->devs_per_cs != 2) { |
| 1201 | dev_err(denali->dev, "unsupported number of devices %d\n", |
| 1202 | denali->devs_per_cs); |
| 1203 | return -EINVAL; |
| 1204 | } |
| 1205 | |
| 1206 | /* 2 chips in parallel */ |
| 1207 | mtd->size <<= 1; |
| 1208 | mtd->erasesize <<= 1; |
| 1209 | mtd->writesize <<= 1; |
| 1210 | mtd->oobsize <<= 1; |
| 1211 | chip->chipsize <<= 1; |
| 1212 | chip->page_shift += 1; |
| 1213 | chip->phys_erase_shift += 1; |
| 1214 | chip->bbt_erase_shift += 1; |
| 1215 | chip->chip_shift += 1; |
| 1216 | chip->pagemask <<= 1; |
| 1217 | chip->ecc.size <<= 1; |
| 1218 | chip->ecc.bytes <<= 1; |
| 1219 | chip->ecc.strength <<= 1; |
| 1220 | denali->oob_skip_bytes <<= 1; |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | int denali_init(struct denali_nand_info *denali) |
| 1226 | { |
| 1227 | struct nand_chip *chip = &denali->nand; |
| 1228 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1229 | u32 features = ioread32(denali->reg + FEATURES); |
| 1230 | int ret; |
| 1231 | |
| 1232 | denali_hw_init(denali); |
| 1233 | |
| 1234 | denali_clear_irq_all(denali); |
| 1235 | |
| 1236 | denali_reset_banks(denali); |
| 1237 | |
| 1238 | denali->active_bank = DENALI_INVALID_BANK; |
| 1239 | |
| 1240 | chip->flash_node = dev_of_offset(denali->dev); |
| 1241 | /* Fallback to the default name if DT did not give "label" property */ |
| 1242 | if (!mtd->name) |
| 1243 | mtd->name = "denali-nand"; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1244 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1245 | chip->select_chip = denali_select_chip; |
| 1246 | chip->read_byte = denali_read_byte; |
| 1247 | chip->write_byte = denali_write_byte; |
| 1248 | chip->read_word = denali_read_word; |
| 1249 | chip->cmd_ctrl = denali_cmd_ctrl; |
| 1250 | chip->dev_ready = denali_dev_ready; |
| 1251 | chip->waitfunc = denali_waitfunc; |
| 1252 | |
| 1253 | if (features & FEATURES__INDEX_ADDR) { |
| 1254 | denali->host_read = denali_indexed_read; |
| 1255 | denali->host_write = denali_indexed_write; |
| 1256 | } else { |
| 1257 | denali->host_read = denali_direct_read; |
| 1258 | denali->host_write = denali_direct_write; |
| 1259 | } |
| 1260 | |
| 1261 | /* clk rate info is needed for setup_data_interface */ |
| 1262 | if (denali->clk_x_rate) |
| 1263 | chip->setup_data_interface = denali_setup_data_interface; |
| 1264 | |
| 1265 | ret = nand_scan_ident(mtd, denali->max_banks, NULL); |
| 1266 | if (ret) |
| 1267 | return ret; |
| 1268 | |
| 1269 | if (ioread32(denali->reg + FEATURES) & FEATURES__DMA) |
| 1270 | denali->dma_avail = 1; |
| 1271 | |
| 1272 | if (denali->dma_avail) { |
| 1273 | chip->buf_align = 16; |
| 1274 | if (denali->caps & DENALI_CAP_DMA_64BIT) |
| 1275 | denali->setup_dma = denali_setup_dma64; |
| 1276 | else |
| 1277 | denali->setup_dma = denali_setup_dma32; |
| 1278 | } else { |
| 1279 | chip->buf_align = 4; |
| 1280 | } |
| 1281 | |
| 1282 | chip->options |= NAND_USE_BOUNCE_BUFFER; |
| 1283 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 1284 | chip->bbt_options |= NAND_BBT_NO_OOB; |
| 1285 | denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME; |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1286 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1287 | /* no subpage writes on denali */ |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1288 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1289 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1290 | ret = denali_ecc_setup(mtd, chip, denali); |
| 1291 | if (ret) { |
| 1292 | dev_err(denali->dev, "Failed to setup ECC settings.\n"); |
| 1293 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1294 | } |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1295 | |
| 1296 | dev_dbg(denali->dev, |
| 1297 | "chosen ECC settings: step=%d, strength=%d, bytes=%d\n", |
| 1298 | chip->ecc.size, chip->ecc.strength, chip->ecc.bytes); |
| 1299 | |
| 1300 | iowrite32(FIELD_PREP(ECC_CORRECTION__ERASE_THRESHOLD, 1) | |
| 1301 | FIELD_PREP(ECC_CORRECTION__VALUE, chip->ecc.strength), |
| 1302 | denali->reg + ECC_CORRECTION); |
| 1303 | iowrite32(mtd->erasesize / mtd->writesize, |
| 1304 | denali->reg + PAGES_PER_BLOCK); |
| 1305 | iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0, |
| 1306 | denali->reg + DEVICE_WIDTH); |
| 1307 | iowrite32(chip->options & NAND_ROW_ADDR_3 ? 0 : TWO_ROW_ADDR_CYCLES__FLAG, |
| 1308 | denali->reg + TWO_ROW_ADDR_CYCLES); |
| 1309 | iowrite32(mtd->writesize, denali->reg + DEVICE_MAIN_AREA_SIZE); |
| 1310 | iowrite32(mtd->oobsize, denali->reg + DEVICE_SPARE_AREA_SIZE); |
| 1311 | |
| 1312 | iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE); |
| 1313 | iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE); |
| 1314 | /* chip->ecc.steps is set by nand_scan_tail(); not available here */ |
| 1315 | iowrite32(mtd->writesize / chip->ecc.size, |
| 1316 | denali->reg + CFG_NUM_DATA_BLOCKS); |
| 1317 | |
| 1318 | mtd_set_ooblayout(mtd, &denali_ooblayout_ops); |
| 1319 | |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1320 | nand_oob.eccbytes = denali->nand.ecc.bytes; |
| 1321 | denali->nand.ecc.layout = &nand_oob; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1322 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1323 | if (chip->options & NAND_BUSWIDTH_16) { |
| 1324 | chip->read_buf = denali_read_buf16; |
| 1325 | chip->write_buf = denali_write_buf16; |
| 1326 | } else { |
| 1327 | chip->read_buf = denali_read_buf; |
| 1328 | chip->write_buf = denali_write_buf; |
| 1329 | } |
| 1330 | chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS; |
| 1331 | chip->ecc.read_page = denali_read_page; |
| 1332 | chip->ecc.read_page_raw = denali_read_page_raw; |
| 1333 | chip->ecc.write_page = denali_write_page; |
| 1334 | chip->ecc.write_page_raw = denali_write_page_raw; |
| 1335 | chip->ecc.read_oob = denali_read_oob; |
| 1336 | chip->ecc.write_oob = denali_write_oob; |
| 1337 | chip->erase = denali_erase; |
Masahiro Yamada | 628ee1e | 2014-11-13 20:31:51 +0900 | [diff] [blame] | 1338 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1339 | ret = denali_multidev_fixup(denali); |
| 1340 | if (ret) |
| 1341 | return ret; |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1342 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1343 | /* |
| 1344 | * This buffer is DMA-mapped by denali_{read,write}_page_raw. Do not |
| 1345 | * use devm_kmalloc() because the memory allocated by devm_ does not |
| 1346 | * guarantee DMA-safe alignment. |
| 1347 | */ |
| 1348 | denali->buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
| 1349 | if (!denali->buf) |
| 1350 | return -ENOMEM; |
| 1351 | |
| 1352 | ret = nand_scan_tail(mtd); |
| 1353 | if (ret) |
| 1354 | goto free_buf; |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1355 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1356 | ret = nand_register(0, mtd); |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 1357 | if (ret) { |
| 1358 | dev_err(denali->dev, "Failed to register MTD: %d\n", ret); |
| 1359 | goto free_buf; |
| 1360 | } |
| 1361 | return 0; |
| 1362 | |
| 1363 | free_buf: |
| 1364 | kfree(denali->buf); |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1365 | |
Masahiro Yamada | da0763d | 2014-11-13 20:31:50 +0900 | [diff] [blame] | 1366 | return ret; |
Chin Liang See | 03534df | 2014-09-12 00:42:17 -0500 | [diff] [blame] | 1367 | } |