blob: d723be35214827edbada0ecf0d175c8ead3a62bd [file] [log] [blame]
Jason Robertsce082592010-05-13 15:57:33 +01001/*
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 Robertsce082592010-05-13 15:57:33 +010019#include <linux/interrupt.h>
20#include <linux/delay.h>
Jamie Iles84457942011-05-06 15:28:55 +010021#include <linux/dma-mapping.h>
Jason Robertsce082592010-05-13 15:57:33 +010022#include <linux/wait.h>
23#include <linux/mutex.h>
Jason Robertsce082592010-05-13 15:57:33 +010024#include <linux/mtd/mtd.h>
25#include <linux/module.h>
Masahiro Yamada7d370b22017-06-13 22:45:48 +090026#include <linux/slab.h>
Jason Robertsce082592010-05-13 15:57:33 +010027
28#include "denali.h"
29
30MODULE_LICENSE("GPL");
31
Jason Robertsce082592010-05-13 15:57:33 +010032#define DENALI_NAND_NAME "denali-nand"
33
Masahiro Yamada0d3a9662017-06-16 14:36:39 +090034/* Host Data/Command Interface */
35#define DENALI_HOST_ADDR 0x00
36#define DENALI_HOST_DATA 0x10
Jason Robertsce082592010-05-13 15:57:33 +010037
Masahiro Yamada0d3a9662017-06-16 14:36:39 +090038#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 Yamadac19e31d2017-06-13 22:45:38 +090054#define DENALI_NR_BANKS 4
55
Masahiro Yamada43914a22014-09-09 11:01:51 +090056/*
Masahiro Yamada1bb88662017-06-13 22:45:37 +090057 * 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 Yamada43914a22014-09-09 11:01:51 +090061 */
Masahiro Yamada1bb88662017-06-13 22:45:37 +090062#define DENALI_CLK_X_MULT 6
Jason Robertsce082592010-05-13 15:57:33 +010063
Masahiro Yamada43914a22014-09-09 11:01:51 +090064/*
65 * this macro allows us to convert from an MTD structure to our own
Jason Robertsce082592010-05-13 15:57:33 +010066 * device context (denali) structure.
67 */
Boris BREZILLON442f201b2015-12-11 15:06:00 +010068static 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 Robertsce082592010-05-13 15:57:33 +010072
Masahiro Yamada0d3a9662017-06-16 14:36:39 +090073static void denali_host_write(struct denali_nand_info *denali,
74 uint32_t addr, uint32_t data)
Jason Robertsce082592010-05-13 15:57:33 +010075{
Masahiro Yamada0d3a9662017-06-16 14:36:39 +090076 iowrite32(addr, denali->host + DENALI_HOST_ADDR);
77 iowrite32(data, denali->host + DENALI_HOST_DATA);
Jason Robertsce082592010-05-13 15:57:33 +010078}
79
Masahiro Yamada43914a22014-09-09 11:01:51 +090080/*
Jamie Ilesc89eeda2011-05-06 15:28:57 +010081 * Use the configuration feature register to determine the maximum number of
82 * banks that the hardware supports.
83 */
84static void detect_max_banks(struct denali_nand_info *denali)
85{
Masahiro Yamada0d3a9662017-06-16 14:36:39 +090086 uint32_t features = ioread32(denali->reg + FEATURES);
Jamie Ilesc89eeda2011-05-06 15:28:57 +010087
Masahiro Yamadae7beeee2017-03-30 15:45:57 +090088 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 Ilesc89eeda2011-05-06 15:28:57 +010093}
94
Masahiro Yamadac19e31d2017-06-13 22:45:38 +090095static void denali_enable_irq(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +010096{
Jamie Iles9589bf52011-05-06 15:28:56 +010097 int i;
98
Masahiro Yamadac19e31d2017-06-13 22:45:38 +090099 for (i = 0; i < DENALI_NR_BANKS; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900100 iowrite32(U32_MAX, denali->reg + INTR_EN(i));
101 iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100102}
103
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900104static void denali_disable_irq(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100105{
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900106 int i;
107
108 for (i = 0; i < DENALI_NR_BANKS; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900109 iowrite32(0, denali->reg + INTR_EN(i));
110 iowrite32(0, denali->reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100111}
112
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900113static void denali_clear_irq(struct denali_nand_info *denali,
114 int bank, uint32_t irq_status)
Jason Robertsce082592010-05-13 15:57:33 +0100115{
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900116 /* write one to clear bits */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900117 iowrite32(irq_status, denali->reg + INTR_STATUS(bank));
Jason Robertsce082592010-05-13 15:57:33 +0100118}
119
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900120static void denali_clear_irq_all(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100121{
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900122 int i;
Masahiro Yamada5637b692014-09-09 11:01:52 +0900123
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900124 for (i = 0; i < DENALI_NR_BANKS; i++)
125 denali_clear_irq(denali, i, U32_MAX);
Jason Robertsce082592010-05-13 15:57:33 +0100126}
127
Jason Robertsce082592010-05-13 15:57:33 +0100128static irqreturn_t denali_isr(int irq, void *dev_id)
129{
130 struct denali_nand_info *denali = dev_id;
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900131 irqreturn_t ret = IRQ_NONE;
Masahiro Yamada5637b692014-09-09 11:01:52 +0900132 uint32_t irq_status;
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900133 int i;
Jason Robertsce082592010-05-13 15:57:33 +0100134
135 spin_lock(&denali->irq_lock);
136
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900137 for (i = 0; i < DENALI_NR_BANKS; i++) {
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900138 irq_status = ioread32(denali->reg + INTR_STATUS(i));
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900139 if (irq_status)
140 ret = IRQ_HANDLED;
141
142 denali_clear_irq(denali, i, irq_status);
143
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900144 if (i != denali->active_bank)
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900145 continue;
146
147 denali->irq_status |= irq_status;
148
149 if (denali->irq_status & denali->irq_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100150 complete(&denali->complete);
Jason Robertsce082592010-05-13 15:57:33 +0100151 }
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900152
Jason Robertsce082592010-05-13 15:57:33 +0100153 spin_unlock(&denali->irq_lock);
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900154
155 return ret;
Jason Robertsce082592010-05-13 15:57:33 +0100156}
Jason Robertsce082592010-05-13 15:57:33 +0100157
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900158static void denali_reset_irq(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100159{
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900160 unsigned long flags;
Jason Robertsce082592010-05-13 15:57:33 +0100161
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900162 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 Robertsce082592010-05-13 15:57:33 +0100167
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900168static 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 Yamada81254502014-09-16 20:04:25 +0900173
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900174 spin_lock_irqsave(&denali->irq_lock, flags);
Jason Robertsce082592010-05-13 15:57:33 +0100175
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900176 irq_status = denali->irq_status;
Jason Robertsce082592010-05-13 15:57:33 +0100177
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900178 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 Robertsce082592010-05-13 15:57:33 +0100182 }
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900183
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 Yamadafa6134e2017-06-13 22:45:39 +0900199static uint32_t denali_check_irq(struct denali_nand_info *denali)
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900200{
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900201 unsigned long flags;
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900202 uint32_t irq_status;
203
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900204 spin_lock_irqsave(&denali->irq_lock, flags);
205 irq_status = denali->irq_status;
206 spin_unlock_irqrestore(&denali->irq_lock, flags);
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900207
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900208 return irq_status;
Jason Robertsce082592010-05-13 15:57:33 +0100209}
210
Masahiro Yamada43914a22014-09-09 11:01:51 +0900211/*
212 * This helper function setups the registers for ECC and whether or not
213 * the spare area will be transferred.
214 */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800215static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
Jason Robertsce082592010-05-13 15:57:33 +0100216 bool transfer_spare)
217{
Masahiro Yamada5637b692014-09-09 11:01:52 +0900218 int ecc_en_flag, transfer_spare_flag;
Jason Robertsce082592010-05-13 15:57:33 +0100219
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 Yamada0d3a9662017-06-16 14:36:39 +0900225 iowrite32(ecc_en_flag, denali->reg + ECC_ENABLE);
226 iowrite32(transfer_spare_flag, denali->reg + TRANSFER_SPARE_REG);
Jason Robertsce082592010-05-13 15:57:33 +0100227}
228
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900229static 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 Yamada0d3a9662017-06-16 14:36:39 +0900234 iowrite32(DENALI_MAP11_DATA | DENALI_BANK(denali),
235 denali->host + DENALI_HOST_ADDR);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900236
237 for (i = 0; i < len; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900238 buf[i] = ioread32(denali->host + DENALI_HOST_DATA);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900239}
240
241static 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 Yamada0d3a9662017-06-16 14:36:39 +0900246 iowrite32(DENALI_MAP11_DATA | DENALI_BANK(denali),
247 denali->host + DENALI_HOST_ADDR);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900248
249 for (i = 0; i < len; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900250 iowrite32(buf[i], denali->host + DENALI_HOST_DATA);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900251}
252
253static 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 Yamada0d3a9662017-06-16 14:36:39 +0900259 iowrite32(DENALI_MAP11_DATA | DENALI_BANK(denali),
260 denali->host + DENALI_HOST_ADDR);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900261
262 for (i = 0; i < len / 2; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900263 buf16[i] = ioread32(denali->host + DENALI_HOST_DATA);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900264}
265
266static 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 Yamada0d3a9662017-06-16 14:36:39 +0900273 iowrite32(DENALI_MAP11_DATA | DENALI_BANK(denali),
274 denali->host + DENALI_HOST_ADDR);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900275
276 for (i = 0; i < len / 2; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900277 iowrite32(buf16[i], denali->host + DENALI_HOST_DATA);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900278}
279
280static 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
289static void denali_write_byte(struct mtd_info *mtd, uint8_t byte)
290{
291 denali_write_buf(mtd, &byte, 1);
292}
293
294static 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
303static 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 Yamada0d3a9662017-06-16 14:36:39 +0900309 type = DENALI_MAP11_CMD;
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900310 else if (ctrl & NAND_ALE)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900311 type = DENALI_MAP11_ADDR;
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900312 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 Yamada0d3a9662017-06-16 14:36:39 +0900322 denali_host_write(denali, DENALI_BANK(denali) | type, dat);
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900323}
324
325static 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 Yamadad29109b2017-03-30 15:45:51 +0900332static 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 Robertsce082592010-05-13 15:57:33 +0100336{
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900337 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 Yamada81254502014-09-16 20:04:25 +0900342
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900343 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 Robertsce082592010-05-13 15:57:33 +0100368}
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900369
Masahiro Yamada24715c72017-03-30 15:45:52 +0900370static 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 Yamada0d3a9662017-06-16 14:36:39 +0900375 int bank = denali->active_bank;
Masahiro Yamada24715c72017-03-30 15:45:52 +0900376 uint32_t ecc_cor;
377 unsigned int max_bitflips;
378
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900379 ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank));
Masahiro Yamada24715c72017-03-30 15:45:52 +0900380 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 Robertsce082592010-05-13 15:57:33 +0100405#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 Yamada20d48592017-03-30 15:45:50 +0900408#define ECC_ERROR_UNCORRECTABLE(x) ((x) & ERR_CORRECTION_INFO__ERROR_TYPE)
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800409#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
Jason Robertsce082592010-05-13 15:57:33 +0100410#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
411
Masahiro Yamada24715c72017-03-30 15:45:52 +0900412static 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 Robertsce082592010-05-13 15:57:33 +0100415{
Masahiro Yamada7de117f2017-06-07 20:52:12 +0900416 unsigned int ecc_size = denali->nand.ecc.size;
Mike Dunn3f91e942012-04-25 12:06:09 -0700417 unsigned int bitflips = 0;
Masahiro Yamada20d48592017-03-30 15:45:50 +0900418 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 Yamadac19e31d2017-06-13 22:45:38 +0900423 uint32_t irq_status;
Jason Robertsce082592010-05-13 15:57:33 +0100424
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900425 denali_reset_irq(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100426
Masahiro Yamada20d48592017-03-30 15:45:50 +0900427 do {
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900428 err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS);
Masahiro Yamada20d48592017-03-30 15:45:50 +0900429 err_sector = ECC_SECTOR(err_addr);
430 err_byte = ECC_BYTE(err_addr);
Jason Robertsce082592010-05-13 15:57:33 +0100431
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900432 err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO);
Masahiro Yamada20d48592017-03-30 15:45:50 +0900433 err_cor_value = ECC_CORRECTION_VALUE(err_cor_info);
434 err_device = ECC_ERR_DEVICE(err_cor_info);
Jason Robertsce082592010-05-13 15:57:33 +0100435
Masahiro Yamada20d48592017-03-30 15:45:50 +0900436 /* reset the bitflip counter when crossing ECC sector */
437 if (err_sector != prev_sector)
438 bitflips = 0;
Masahiro Yamada81254502014-09-16 20:04:25 +0900439
Masahiro Yamada20d48592017-03-30 15:45:50 +0900440 if (ECC_ERROR_UNCORRECTABLE(err_cor_info)) {
441 /*
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900442 * Check later if this is a real ECC error, or
443 * an erased sector.
Masahiro Yamada20d48592017-03-30 15:45:50 +0900444 */
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900445 *uncor_ecc_flags |= BIT(err_sector);
Masahiro Yamada7de117f2017-06-07 20:52:12 +0900446 } else if (err_byte < ecc_size) {
Masahiro Yamada20d48592017-03-30 15:45:50 +0900447 /*
Masahiro Yamada7de117f2017-06-07 20:52:12 +0900448 * If err_byte is larger than ecc_size, means error
Masahiro Yamada20d48592017-03-30 15:45:50 +0900449 * 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 Yamada7de117f2017-06-07 20:52:12 +0900457 offset = (err_sector * ecc_size + err_byte) *
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900458 denali->devs_per_cs + err_device;
Masahiro Yamada20d48592017-03-30 15:45:50 +0900459
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 Yamadac19e31d2017-06-13 22:45:38 +0900477 irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE);
478 if (!(irq_status & INTR__ECC_TRANSACTION_DONE))
479 return -EIO;
Masahiro Yamada20d48592017-03-30 15:45:50 +0900480
481 return max_bitflips;
Jason Robertsce082592010-05-13 15:57:33 +0100482}
483
484/* programs the controller to either enable/disable DMA transfers */
David Woodhouseaadff492010-05-13 16:12:43 +0100485static void denali_enable_dma(struct denali_nand_info *denali, bool en)
Jason Robertsce082592010-05-13 15:57:33 +0100486{
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900487 iowrite32(en ? DMA_ENABLE__FLAG : 0, denali->reg + DMA_ENABLE);
488 ioread32(denali->reg + DMA_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100489}
490
Masahiro Yamada2291cb82017-06-13 22:45:42 +0900491static void denali_setup_dma64(struct denali_nand_info *denali,
Masahiro Yamada96a376b2017-06-13 22:45:44 +0900492 dma_addr_t dma_addr, int page, int write)
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900493{
494 uint32_t mode;
495 const int page_count = 1;
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900496
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900497 mode = DENALI_MAP10 | DENALI_BANK(denali) | page;
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900498
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 Yamada0d3a9662017-06-16 14:36:39 +0900505 denali_host_write(denali, mode,
506 0x01002000 | (64 << 16) | (write << 8) | page_count);
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900507
508 /* 2. set memory low address */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900509 denali_host_write(denali, mode, dma_addr);
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900510
511 /* 3. set memory high address */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900512 denali_host_write(denali, mode, (uint64_t)dma_addr >> 32);
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900513}
514
Masahiro Yamada2291cb82017-06-13 22:45:42 +0900515static void denali_setup_dma32(struct denali_nand_info *denali,
Masahiro Yamada96a376b2017-06-13 22:45:44 +0900516 dma_addr_t dma_addr, int page, int write)
Jason Robertsce082592010-05-13 15:57:33 +0100517{
Masahiro Yamada5637b692014-09-09 11:01:52 +0900518 uint32_t mode;
Jason Robertsce082592010-05-13 15:57:33 +0100519 const int page_count = 1;
Jason Robertsce082592010-05-13 15:57:33 +0100520
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900521 mode = DENALI_MAP10 | DENALI_BANK(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100522
523 /* DMA is a four step process */
524
525 /* 1. setup transfer type and # of pages */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900526 denali_host_write(denali, mode | page,
527 0x2000 | (write << 8) | page_count);
Jason Robertsce082592010-05-13 15:57:33 +0100528
529 /* 2. set memory high address bits 23:8 */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900530 denali_host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200);
Jason Robertsce082592010-05-13 15:57:33 +0100531
532 /* 3. set memory low address bits 23:8 */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900533 denali_host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300);
Jason Robertsce082592010-05-13 15:57:33 +0100534
Masahiro Yamada43914a22014-09-09 11:01:51 +0900535 /* 4. interrupt when complete, burst len = 64 bytes */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900536 denali_host_write(denali, mode | 0x14000, 0x2400);
Jason Robertsce082592010-05-13 15:57:33 +0100537}
538
Masahiro Yamada2291cb82017-06-13 22:45:42 +0900539static void denali_setup_dma(struct denali_nand_info *denali,
Masahiro Yamada96a376b2017-06-13 22:45:44 +0900540 dma_addr_t dma_addr, int page, int write)
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900541{
542 if (denali->caps & DENALI_CAP_DMA_64BIT)
Masahiro Yamada96a376b2017-06-13 22:45:44 +0900543 denali_setup_dma64(denali, dma_addr, page, write);
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900544 else
Masahiro Yamada96a376b2017-06-13 22:45:44 +0900545 denali_setup_dma32(denali, dma_addr, page, write);
Masahiro Yamada210a2c82017-03-30 15:45:54 +0900546}
547
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900548static int denali_pio_read(struct denali_nand_info *denali, void *buf,
549 size_t size, int page, int raw)
Jason Robertsce082592010-05-13 15:57:33 +0100550{
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900551 uint32_t addr = DENALI_BANK(denali) | page;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900552 uint32_t *buf32 = (uint32_t *)buf;
553 uint32_t irq_status, ecc_err_mask;
554 int i;
Masahiro Yamadab21ff822017-06-13 22:45:35 +0900555
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900556 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 Robertsce082592010-05-13 15:57:33 +0100560
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900561 denali_reset_irq(denali);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900562
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900563 iowrite32(DENALI_MAP01 | addr, denali->host + DENALI_HOST_ADDR);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900564 for (i = 0; i < size / 4; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900565 *buf32++ = ioread32(denali->host + DENALI_HOST_DATA);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900566
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 Yamada57a4d8b2017-06-13 22:45:46 +0900571 if (irq_status & INTR__ERASED_PAGE)
572 memset(buf, 0xff, size);
573
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900574 return irq_status & ecc_err_mask ? -EBADMSG : 0;
575}
576
577static int denali_pio_write(struct denali_nand_info *denali,
578 const void *buf, size_t size, int page, int raw)
579{
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900580 uint32_t addr = DENALI_BANK(denali) | page;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900581 const uint32_t *buf32 = (uint32_t *)buf;
582 uint32_t irq_status;
583 int i;
584
585 denali_reset_irq(denali);
586
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900587 iowrite32(DENALI_MAP01 | addr, denali->host + DENALI_HOST_ADDR);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900588 for (i = 0; i < size / 4; i++)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900589 iowrite32(*buf32++, denali->host + DENALI_HOST_DATA);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900590
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
599static 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
608static int denali_dma_xfer(struct denali_nand_info *denali, void *buf,
609 size_t size, int page, int raw, int write)
610{
Masahiro Yamada997cde22017-06-13 22:45:47 +0900611 dma_addr_t dma_addr;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900612 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 Yamada997cde22017-06-13 22:45:47 +0900616 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 Yamada26d266e2017-06-13 22:45:45 +0900621
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
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800638 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +0100639
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900640 denali_reset_irq(denali);
641 denali_setup_dma(denali, dma_addr, page, write);
Jason Robertsce082592010-05-13 15:57:33 +0100642
643 /* wait for operation to complete */
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900644 irq_status = denali_wait_for_irq(denali, irq_mask);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900645 if (!(irq_status & INTR__DMA_CMD_COMP))
Masahiro Yamadab21ff822017-06-13 22:45:35 +0900646 ret = -EIO;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900647 else if (irq_status & ecc_err_mask)
648 ret = -EBADMSG;
Jason Robertsce082592010-05-13 15:57:33 +0100649
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800650 denali_enable_dma(denali, false);
Masahiro Yamada997cde22017-06-13 22:45:47 +0900651 dma_unmap_single(denali->dev, dma_addr, size, dir);
Josh Wufdbad98d2012-06-25 18:07:45 +0800652
Masahiro Yamada57a4d8b2017-06-13 22:45:46 +0900653 if (irq_status & INTR__ERASED_PAGE)
654 memset(buf, 0xff, size);
655
Masahiro Yamadab21ff822017-06-13 22:45:35 +0900656 return ret;
Jason Robertsce082592010-05-13 15:57:33 +0100657}
658
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900659static int denali_data_xfer(struct denali_nand_info *denali, void *buf,
660 size_t size, int page, int raw, int write)
Jason Robertsce082592010-05-13 15:57:33 +0100661{
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900662 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 Robertsce082592010-05-13 15:57:33 +0100668}
669
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900670static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip,
671 int page, int write)
Jason Robertsce082592010-05-13 15:57:33 +0100672{
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900673 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 Yamada0d3a9662017-06-16 14:36:39 +0900682 int oob_skip = denali->oob_skip_bytes;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900683 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 Robertsce082592010-05-13 15:57:33 +0100728}
729
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900730static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
731 uint8_t *buf, int oob_required, int page)
Jason Robertsce082592010-05-13 15:57:33 +0100732{
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900733 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 Yamada0d3a9662017-06-16 14:36:39 +0900740 int oob_skip = denali->oob_skip_bytes;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900741 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 Robertsce082592010-05-13 15:57:33 +0100803}
804
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800805static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +0300806 int page)
Jason Robertsce082592010-05-13 15:57:33 +0100807{
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900808 denali_oob_xfer(mtd, chip, page, 0);
Jason Robertsce082592010-05-13 15:57:33 +0100809
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +0300810 return 0;
Jason Robertsce082592010-05-13 15:57:33 +0100811}
812
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900813static 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 Robertsce082592010-05-13 15:57:33 +0100829static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700830 uint8_t *buf, int oob_required, int page)
Jason Robertsce082592010-05-13 15:57:33 +0100831{
832 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900833 unsigned long uncor_ecc_flags = 0;
834 int stat = 0;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900835 int ret;
Jason Robertsce082592010-05-13 15:57:33 +0100836
Masahiro Yamada997cde22017-06-13 22:45:47 +0900837 ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900838 if (ret && ret != -EBADMSG)
839 return ret;
Jason Robertsce082592010-05-13 15:57:33 +0100840
Masahiro Yamada24715c72017-03-30 15:45:52 +0900841 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
842 stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900843 else if (ret == -EBADMSG)
Masahiro Yamada24715c72017-03-30 15:45:52 +0900844 stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf);
Jason Robertsce082592010-05-13 15:57:33 +0100845
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900846 if (stat < 0)
847 return stat;
848
849 if (uncor_ecc_flags) {
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900850 ret = denali_read_oob(mtd, chip, page);
851 if (ret)
852 return ret;
Jason Robertsce082592010-05-13 15:57:33 +0100853
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900854 stat = denali_check_erased_page(mtd, chip, buf,
855 uncor_ecc_flags, stat);
Jason Robertsce082592010-05-13 15:57:33 +0100856 }
Masahiro Yamadad29109b2017-03-30 15:45:51 +0900857
858 return stat;
Jason Robertsce082592010-05-13 15:57:33 +0100859}
860
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900861static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
862 const uint8_t *buf, int oob_required, int page)
Jason Robertsce082592010-05-13 15:57:33 +0100863{
864 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900865 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 Yamada0d3a9662017-06-16 14:36:39 +0900871 int oob_skip = denali->oob_skip_bytes;
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900872 size_t size = writesize + oobsize;
873 int i, pos, len;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800874
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900875 /*
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 Robertsce082592010-05-13 15:57:33 +0100881
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900882 /* 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 Robertsce082592010-05-13 15:57:33 +0100887
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900888 if (pos >= writesize)
889 pos += oob_skip;
890 else if (pos + len > writesize)
891 len = writesize - pos;
Jason Robertsce082592010-05-13 15:57:33 +0100892
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900893 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 Robertsce082592010-05-13 15:57:33 +0100903
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900904 if (oob_required) {
905 const uint8_t *oob = chip->oob_poi;
Jason Robertsce082592010-05-13 15:57:33 +0100906
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900907 /* BBM at the beginning of the OOB area */
908 memcpy(dma_buf + writesize, oob, oob_skip);
909 oob += oob_skip;
Jason Robertsce082592010-05-13 15:57:33 +0100910
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900911 /* OOB ECC */
912 for (i = 0; i < ecc_steps; i++) {
913 pos = ecc_size + i * (ecc_size + ecc_bytes);
914 len = ecc_bytes;
Jason Robertsce082592010-05-13 15:57:33 +0100915
Masahiro Yamada26d266e2017-06-13 22:45:45 +0900916 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
939static 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 Yamada997cde22017-06-13 22:45:47 +0900944 return denali_data_xfer(denali, (void *)buf, mtd->writesize,
945 page, 0, 1);
Jason Robertsce082592010-05-13 15:57:33 +0100946}
947
Jason Robertsce082592010-05-13 15:57:33 +0100948static void denali_select_chip(struct mtd_info *mtd, int chip)
949{
950 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800951
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900952 denali->active_bank = chip;
Jason Robertsce082592010-05-13 15:57:33 +0100953}
954
955static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
956{
Masahiro Yamadafa6134e2017-06-13 22:45:39 +0900957 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 Robertsce082592010-05-13 15:57:33 +0100964}
965
Brian Norris49c50b92014-05-06 16:02:19 -0700966static int denali_erase(struct mtd_info *mtd, int page)
Jason Robertsce082592010-05-13 15:57:33 +0100967{
968 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900969 uint32_t irq_status;
Jason Robertsce082592010-05-13 15:57:33 +0100970
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900971 denali_reset_irq(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100972
Masahiro Yamada0d3a9662017-06-16 14:36:39 +0900973 denali_host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page,
974 DENALI_ERASE);
Jason Robertsce082592010-05-13 15:57:33 +0100975
976 /* wait for erase to complete or failure to occur */
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900977 irq_status = denali_wait_for_irq(denali,
978 INTR__ERASE_COMP | INTR__ERASE_FAIL);
Jason Robertsce082592010-05-13 15:57:33 +0100979
Masahiro Yamadac19e31d2017-06-13 22:45:38 +0900980 return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL;
Jason Robertsce082592010-05-13 15:57:33 +0100981}
982
Masahiro Yamada1bb88662017-06-13 22:45:37 +0900983#define DIV_ROUND_DOWN_ULL(ll, d) \
984 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
985
986static 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 Yamada0d3a9662017-06-16 14:36:39 +09001013 tmp = ioread32(denali->reg + ACC_CLKS);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001014 tmp &= ~ACC_CLKS__VALUE;
1015 tmp |= acc_clks;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001016 iowrite32(tmp, denali->reg + ACC_CLKS);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001017
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 Yamada0d3a9662017-06-16 14:36:39 +09001022 tmp = ioread32(denali->reg + RE_2_WE);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001023 tmp &= ~RE_2_WE__VALUE;
1024 tmp |= re_2_we;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001025 iowrite32(tmp, denali->reg + RE_2_WE);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001026
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 Yamada0d3a9662017-06-16 14:36:39 +09001031 tmp = ioread32(denali->reg + RE_2_RE);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001032 tmp &= ~RE_2_RE__VALUE;
1033 tmp |= re_2_re;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001034 iowrite32(tmp, denali->reg + RE_2_RE);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001035
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 Yamada0d3a9662017-06-16 14:36:39 +09001040 tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001041 tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE;
1042 tmp |= we_2_re;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001043 iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001044
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 Yamada0d3a9662017-06-16 14:36:39 +09001055 tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001056 tmp &= ~addr_2_data_mask;
1057 tmp |= addr_2_data;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001058 iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001059
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 Yamada0d3a9662017-06-16 14:36:39 +09001065 tmp = ioread32(denali->reg + RDWR_EN_HI_CNT);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001066 tmp &= ~RDWR_EN_HI_CNT__VALUE;
1067 tmp |= rdwr_en_hi;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001068 iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001069
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 Yamada0d3a9662017-06-16 14:36:39 +09001079 tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001080 tmp &= ~RDWR_EN_LO_CNT__VALUE;
1081 tmp |= rdwr_en_lo;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001082 iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001083
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 Yamada0d3a9662017-06-16 14:36:39 +09001090 tmp = ioread32(denali->reg + CS_SETUP_CNT);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001091 tmp &= ~CS_SETUP_CNT__VALUE;
1092 tmp |= cs_setup;
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001093 iowrite32(tmp, denali->reg + CS_SETUP_CNT);
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001094
1095 return 0;
1096}
Jason Robertsce082592010-05-13 15:57:33 +01001097
Masahiro Yamadaf4862872017-06-13 22:45:40 +09001098static void denali_reset_banks(struct denali_nand_info *denali)
1099{
Masahiro Yamadad49f5792017-06-13 22:45:41 +09001100 u32 irq_status;
Masahiro Yamadaf4862872017-06-13 22:45:40 +09001101 int i;
1102
Masahiro Yamadaf4862872017-06-13 22:45:40 +09001103 for (i = 0; i < denali->max_banks; i++) {
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001104 denali->active_bank = i;
Masahiro Yamadad49f5792017-06-13 22:45:41 +09001105
1106 denali_reset_irq(denali);
1107
1108 iowrite32(DEVICE_RESET__BANK(i),
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001109 denali->reg + DEVICE_RESET);
Masahiro Yamadad49f5792017-06-13 22:45:41 +09001110
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 Yamadaf4862872017-06-13 22:45:40 +09001114 break;
1115 }
1116
1117 dev_dbg(denali->dev, "%d chips connected\n", i);
1118 denali->max_banks = i;
Masahiro Yamadaf4862872017-06-13 22:45:40 +09001119}
1120
Jason Robertsce082592010-05-13 15:57:33 +01001121static void denali_hw_init(struct denali_nand_info *denali)
1122{
Masahiro Yamada43914a22014-09-09 11:01:51 +09001123 /*
Masahiro Yamadae7beeee2017-03-30 15:45:57 +09001124 * The REVISION register may not be reliable. Platforms are allowed to
1125 * override it.
1126 */
1127 if (!denali->revision)
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001128 denali->revision = swab16(ioread32(denali->reg + REVISION));
Masahiro Yamadae7beeee2017-03-30 15:45:57 +09001129
1130 /*
Masahiro Yamada43914a22014-09-09 11:01:51 +09001131 * tell driver how many bit controller will skip before
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001132 * 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 Yamada43914a22014-09-09 11:01:51 +09001135 */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001136 denali->oob_skip_bytes = ioread32(denali->reg + SPARE_AREA_SKIP_BYTES);
Jamie Ilesbc27ede2011-06-06 17:11:34 +01001137 detect_max_banks(denali);
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001138 iowrite32(0x0F, denali->reg + RB_PIN_ENABLED);
1139 iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE);
Jason Robertsce082592010-05-13 15:57:33 +01001140
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001141 iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER);
Jason Robertsce082592010-05-13 15:57:33 +01001142
1143 /* Should set value for these registers when init */
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001144 iowrite32(0, denali->reg + TWO_ROW_ADDR_CYCLES);
1145 iowrite32(1, denali->reg + ECC_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +01001146}
1147
Masahiro Yamada7de117f2017-06-07 20:52:12 +09001148int 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}
1153EXPORT_SYMBOL(denali_calc_ecc_bytes);
1154
1155static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip,
1156 struct denali_nand_info *denali)
1157{
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001158 int oobavail = mtd->oobsize - denali->oob_skip_bytes;
Masahiro Yamada7de117f2017-06-07 20:52:12 +09001159 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 Brezillon14fad622016-02-03 20:00:11 +01001181
1182static 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 Yamada0d3a9662017-06-16 14:36:39 +09001191 oobregion->offset = denali->oob_skip_bytes;
Boris Brezillon14fad622016-02-03 20:00:11 +01001192 oobregion->length = chip->ecc.total;
1193
1194 return 0;
1195}
1196
1197static 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 Yamada0d3a9662017-06-16 14:36:39 +09001206 oobregion->offset = chip->ecc.total + denali->oob_skip_bytes;
Boris Brezillon14fad622016-02-03 20:00:11 +01001207 oobregion->length = mtd->oobsize - oobregion->offset;
1208
1209 return 0;
1210}
1211
1212static const struct mtd_ooblayout_ops denali_ooblayout_ops = {
1213 .ecc = denali_ooblayout_ecc,
1214 .free = denali_ooblayout_free,
Jason Robertsce082592010-05-13 15:57:33 +01001215};
1216
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001217/* initialize driver data structures */
Brian Norris8c519432013-08-10 22:57:30 -07001218static void denali_drv_init(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +01001219{
Masahiro Yamada43914a22014-09-09 11:01:51 +09001220 /*
1221 * the completion object will be used to notify
1222 * the callee that the interrupt is done
1223 */
Jason Robertsce082592010-05-13 15:57:33 +01001224 init_completion(&denali->complete);
1225
Masahiro Yamada43914a22014-09-09 11:01:51 +09001226 /*
1227 * the spinlock will be used to synchronize the ISR with any
1228 * element that might be access shared data (interrupt status)
1229 */
Jason Robertsce082592010-05-13 15:57:33 +01001230 spin_lock_init(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +01001231}
1232
Masahiro Yamadae93c1642017-03-23 05:07:21 +09001233static int denali_multidev_fixup(struct denali_nand_info *denali)
Masahiro Yamada6da27b42017-03-23 05:07:20 +09001234{
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 Yamada0d3a9662017-06-16 14:36:39 +09001245 denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED);
Masahiro Yamada6da27b42017-03-23 05:07:20 +09001246
Masahiro Yamadacc5d8032017-03-23 05:07:22 +09001247 /*
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 Yamada0d3a9662017-06-16 14:36:39 +09001251 if (denali->devs_per_cs == 0) {
1252 denali->devs_per_cs = 1;
1253 iowrite32(1, denali->reg + DEVICES_CONNECTED);
Masahiro Yamadacc5d8032017-03-23 05:07:22 +09001254 }
1255
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001256 if (denali->devs_per_cs == 1)
Masahiro Yamadae93c1642017-03-23 05:07:21 +09001257 return 0;
1258
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001259 if (denali->devs_per_cs != 2) {
Masahiro Yamadae93c1642017-03-23 05:07:21 +09001260 dev_err(denali->dev, "unsupported number of devices %d\n",
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001261 denali->devs_per_cs);
Masahiro Yamadae93c1642017-03-23 05:07:21 +09001262 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 Yamada0d3a9662017-06-16 14:36:39 +09001279 denali->oob_skip_bytes <<= 1;
Masahiro Yamadae93c1642017-03-23 05:07:21 +09001280
1281 return 0;
Masahiro Yamada6da27b42017-03-23 05:07:20 +09001282}
1283
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001284int denali_init(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +01001285{
Masahiro Yamada1394a722017-03-23 05:07:17 +09001286 struct nand_chip *chip = &denali->nand;
1287 struct mtd_info *mtd = nand_to_mtd(chip);
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001288 int ret;
Jason Robertsce082592010-05-13 15:57:33 +01001289
Boris BREZILLON442f201b2015-12-11 15:06:00 +01001290 mtd->dev.parent = denali->dev;
Jason Robertsce082592010-05-13 15:57:33 +01001291 denali_hw_init(denali);
1292 denali_drv_init(denali);
1293
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001294 denali_clear_irq_all(denali);
1295
Masahiro Yamada7ebb8d02016-11-09 13:35:27 +09001296 /* 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 Yamada789ccf12016-11-09 13:35:24 +09001300 dev_err(denali->dev, "Unable to request IRQ\n");
Masahiro Yamada7ebb8d02016-11-09 13:35:27 +09001301 return ret;
Jason Robertsce082592010-05-13 15:57:33 +01001302 }
1303
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001304 denali_enable_irq(denali);
Masahiro Yamadad49f5792017-06-13 22:45:41 +09001305 denali_reset_banks(denali);
1306
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001307 denali->active_bank = DENALI_INVALID_BANK;
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001308
Masahiro Yamada63757d42017-03-23 05:07:18 +09001309 nand_set_flash_node(chip, denali->dev->of_node);
Masahiro Yamada8aabdf32017-03-30 15:45:48 +09001310 /* Fallback to the default name if DT did not give "label" property */
1311 if (!mtd->name)
1312 mtd->name = "denali-nand";
Jason Robertsce082592010-05-13 15:57:33 +01001313
1314 /* register the driver with the NAND core subsystem */
Masahiro Yamada1394a722017-03-23 05:07:17 +09001315 chip->select_chip = denali_select_chip;
Masahiro Yamada1394a722017-03-23 05:07:17 +09001316 chip->read_byte = denali_read_byte;
Masahiro Yamadafa6134e2017-06-13 22:45:39 +09001317 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 Yamada1394a722017-03-23 05:07:17 +09001321 chip->waitfunc = denali_waitfunc;
Jason Robertsce082592010-05-13 15:57:33 +01001322
Masahiro Yamada1bb88662017-06-13 22:45:37 +09001323 /* 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 Yamada43914a22014-09-09 11:01:51 +09001327 /*
1328 * scan for NAND devices attached to the controller
Jason Robertsce082592010-05-13 15:57:33 +01001329 * this is the first stage in a two step process to register
Masahiro Yamada43914a22014-09-09 11:01:51 +09001330 * with the nand subsystem
1331 */
Masahiro Yamadaa227d4e2016-11-09 13:35:28 +09001332 ret = nand_scan_ident(mtd, denali->max_banks, NULL);
1333 if (ret)
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001334 goto disable_irq;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001335
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001336 if (ioread32(denali->reg + FEATURES) & FEATURES__DMA)
Masahiro Yamada26d266e2017-06-13 22:45:45 +09001337 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 Shijiee07caa32013-12-21 00:02:28 +08001348 }
1349
Masahiro Yamada26d266e2017-06-13 22:45:45 +09001350 if (denali->dma_avail) {
Masahiro Yamada997cde22017-06-13 22:45:47 +09001351 chip->options |= NAND_USE_BOUNCE_BUFFER;
1352 chip->buf_align = 16;
Chuanxiao.Dong664065242010-08-06 18:48:21 +08001353 }
1354
Masahiro Yamada43914a22014-09-09 11:01:51 +09001355 /*
Masahiro Yamada43914a22014-09-09 11:01:51 +09001356 * second stage of the NAND scan
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001357 * this stage requires information regarding ECC and
Masahiro Yamada43914a22014-09-09 11:01:51 +09001358 * bad block management.
1359 */
Jason Robertsce082592010-05-13 15:57:33 +01001360
Masahiro Yamada1394a722017-03-23 05:07:17 +09001361 chip->bbt_options |= NAND_BBT_USE_FLASH;
Masahiro Yamada777f2d42017-06-13 22:45:49 +09001362 chip->bbt_options |= NAND_BBT_NO_OOB;
1363
Masahiro Yamada1394a722017-03-23 05:07:17 +09001364 chip->ecc.mode = NAND_ECC_HW_SYNDROME;
Jason Robertsce082592010-05-13 15:57:33 +01001365
Graham Moored99d7282015-01-14 09:38:50 -06001366 /* no subpage writes on denali */
Masahiro Yamada1394a722017-03-23 05:07:17 +09001367 chip->options |= NAND_NO_SUBPAGE_WRITE;
Graham Moored99d7282015-01-14 09:38:50 -06001368
Masahiro Yamada7de117f2017-06-07 20:52:12 +09001369 ret = denali_ecc_setup(mtd, chip, denali);
1370 if (ret) {
1371 dev_err(denali->dev, "Failed to setup ECC settings.\n");
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001372 goto disable_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001373 }
1374
Masahiro Yamada7de117f2017-06-07 20:52:12 +09001375 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 Yamada57a4d8b2017-06-13 22:45:46 +09001379 iowrite32(MAKE_ECC_CORRECTION(chip->ecc.strength, 1),
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001380 denali->reg + ECC_CORRECTION);
Masahiro Yamada0615e7a2017-06-07 20:52:13 +09001381 iowrite32(mtd->erasesize / mtd->writesize,
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001382 denali->reg + PAGES_PER_BLOCK);
Masahiro Yamada0615e7a2017-06-07 20:52:13 +09001383 iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0,
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001384 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 Yamada7de117f2017-06-07 20:52:12 +09001387
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001388 iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE);
1389 iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE);
Masahiro Yamada7de117f2017-06-07 20:52:12 +09001390 /* chip->ecc.steps is set by nand_scan_tail(); not available here */
1391 iowrite32(mtd->writesize / chip->ecc.size,
Masahiro Yamada0d3a9662017-06-16 14:36:39 +09001392 denali->reg + CFG_NUM_DATA_BLOCKS);
Masahiro Yamada7de117f2017-06-07 20:52:12 +09001393
Boris Brezillon14fad622016-02-03 20:00:11 +01001394 mtd_set_ooblayout(mtd, &denali_ooblayout_ops);
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001395
Masahiro Yamadafa6134e2017-06-13 22:45:39 +09001396 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 Yamadab21ff822017-06-13 22:45:35 +09001403 chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
Masahiro Yamada1394a722017-03-23 05:07:17 +09001404 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 Robertsce082592010-05-13 15:57:33 +01001411
Masahiro Yamadae93c1642017-03-23 05:07:21 +09001412 ret = denali_multidev_fixup(denali);
1413 if (ret)
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001414 goto disable_irq;
Masahiro Yamada6da27b42017-03-23 05:07:20 +09001415
Masahiro Yamada7d370b22017-06-13 22:45:48 +09001416 /*
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 Yamadaa227d4e2016-11-09 13:35:28 +09001427 ret = nand_scan_tail(mtd);
1428 if (ret)
Masahiro Yamada7d370b22017-06-13 22:45:48 +09001429 goto free_buf;
Jason Robertsce082592010-05-13 15:57:33 +01001430
Boris BREZILLON442f201b2015-12-11 15:06:00 +01001431 ret = mtd_device_register(mtd, NULL, 0);
Jason Robertsce082592010-05-13 15:57:33 +01001432 if (ret) {
Masahiro Yamada789ccf12016-11-09 13:35:24 +09001433 dev_err(denali->dev, "Failed to register MTD: %d\n", ret);
Masahiro Yamada7d370b22017-06-13 22:45:48 +09001434 goto free_buf;
Jason Robertsce082592010-05-13 15:57:33 +01001435 }
1436 return 0;
1437
Masahiro Yamada7d370b22017-06-13 22:45:48 +09001438free_buf:
1439 kfree(denali->buf);
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001440disable_irq:
1441 denali_disable_irq(denali);
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001442
Jason Robertsce082592010-05-13 15:57:33 +01001443 return ret;
1444}
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001445EXPORT_SYMBOL(denali_init);
Jason Robertsce082592010-05-13 15:57:33 +01001446
1447/* driver exit point */
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001448void denali_remove(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +01001449{
Boris BREZILLON442f201b2015-12-11 15:06:00 +01001450 struct mtd_info *mtd = nand_to_mtd(&denali->nand);
Boris BREZILLON320092a2015-12-11 15:02:34 +01001451
Boris BREZILLON442f201b2015-12-11 15:06:00 +01001452 nand_release(mtd);
Masahiro Yamada7d370b22017-06-13 22:45:48 +09001453 kfree(denali->buf);
Masahiro Yamadac19e31d2017-06-13 22:45:38 +09001454 denali_disable_irq(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001455}
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001456EXPORT_SYMBOL(denali_remove);