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