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