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