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