blob: b525b1be54963c6fec2d2e1b4191aacbbf776ff9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chin Liang See03534df2014-09-12 00:42:17 -05002/*
3 * Copyright (C) 2014 Panasonic Corporation
4 * Copyright (C) 2013-2014, Altera Corporation <www.altera.com>
5 * Copyright (C) 2009-2010, Intel Corporation and its suppliers.
Chin Liang See03534df2014-09-12 00:42:17 -05006 */
7
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09008#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Masahiro Yamada5beb1b32017-11-30 13:45:27 +090010#include <nand.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070012#include <dm/devres.h>
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090013#include <linux/bitfield.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090014#include <linux/dma-mapping.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090017#include <linux/io.h>
Masahiro Yamada5beb1b32017-11-30 13:45:27 +090018#include <linux/mtd/mtd.h>
19#include <linux/mtd/rawnand.h>
Chin Liang See03534df2014-09-12 00:42:17 -050020
21#include "denali.h"
22
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090023#define DENALI_NAND_NAME "denali-nand"
Chin Liang See03534df2014-09-12 00:42:17 -050024
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090025/* for Indexed Addressing */
26#define DENALI_INDEXED_CTRL 0x00
27#define DENALI_INDEXED_DATA 0x10
Chin Liang See03534df2014-09-12 00:42:17 -050028
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090029#define DENALI_MAP00 (0 << 26) /* direct access to buffer */
30#define DENALI_MAP01 (1 << 26) /* read/write pages in PIO */
31#define DENALI_MAP10 (2 << 26) /* high-level control plane */
32#define DENALI_MAP11 (3 << 26) /* direct controller access */
Chin Liang See03534df2014-09-12 00:42:17 -050033
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090034/* MAP11 access cycle type */
35#define DENALI_MAP11_CMD ((DENALI_MAP11) | 0) /* command cycle */
36#define DENALI_MAP11_ADDR ((DENALI_MAP11) | 1) /* address cycle */
37#define DENALI_MAP11_DATA ((DENALI_MAP11) | 2) /* data cycle */
Chin Liang See03534df2014-09-12 00:42:17 -050038
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090039/* MAP10 commands */
40#define DENALI_ERASE 0x01
Chin Liang See03534df2014-09-12 00:42:17 -050041
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090042#define DENALI_BANK(denali) ((denali)->active_bank << 24)
Chin Liang See03534df2014-09-12 00:42:17 -050043
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090044#define DENALI_INVALID_BANK -1
45#define DENALI_NR_BANKS 4
Chin Liang See03534df2014-09-12 00:42:17 -050046
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090047static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -050048{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090049 return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand);
Chin Liang See03534df2014-09-12 00:42:17 -050050}
51
52/*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090053 * Direct Addressing - the slave address forms the control information (command
54 * type, bank, block, and page address). The slave data is the actual data to
55 * be transferred. This mode requires 28 bits of address region allocated.
Scott Wood3ea94ed2015-06-26 19:03:26 -050056 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090057static u32 denali_direct_read(struct denali_nand_info *denali, u32 addr)
Chin Liang See03534df2014-09-12 00:42:17 -050058{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090059 return ioread32(denali->host + addr);
Chin Liang See03534df2014-09-12 00:42:17 -050060}
61
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090062static void denali_direct_write(struct denali_nand_info *denali, u32 addr,
63 u32 data)
Chin Liang See03534df2014-09-12 00:42:17 -050064{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090065 iowrite32(data, denali->host + addr);
Chin Liang See03534df2014-09-12 00:42:17 -050066}
67
Scott Wood3ea94ed2015-06-26 19:03:26 -050068/*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090069 * Indexed Addressing - address translation module intervenes in passing the
70 * control information. This mode reduces the required address range. The
71 * control information and transferred data are latched by the registers in
72 * the translation module.
Scott Wood3ea94ed2015-06-26 19:03:26 -050073 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090074static u32 denali_indexed_read(struct denali_nand_info *denali, u32 addr)
Chin Liang See03534df2014-09-12 00:42:17 -050075{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090076 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
77 return ioread32(denali->host + DENALI_INDEXED_DATA);
Chin Liang See03534df2014-09-12 00:42:17 -050078}
79
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090080static void denali_indexed_write(struct denali_nand_info *denali, u32 addr,
81 u32 data)
Chin Liang See03534df2014-09-12 00:42:17 -050082{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090083 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
84 iowrite32(data, denali->host + DENALI_INDEXED_DATA);
Chin Liang See03534df2014-09-12 00:42:17 -050085}
86
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090087/*
88 * Use the configuration feature register to determine the maximum number of
89 * banks that the hardware supports.
90 */
91static void denali_detect_max_banks(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -050092{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090093 uint32_t features = ioread32(denali->reg + FEATURES);
Chin Liang See03534df2014-09-12 00:42:17 -050094
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090095 denali->max_banks = 1 << FIELD_GET(FEATURES__N_BANKS, features);
Chin Liang See03534df2014-09-12 00:42:17 -050096
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090097 /* the encoding changed from rev 5.0 to 5.1 */
98 if (denali->revision < 0x0501)
99 denali->max_banks <<= 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500100}
101
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900102static void __maybe_unused denali_enable_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500103{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500104 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500105
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900106 for (i = 0; i < DENALI_NR_BANKS; i++)
107 iowrite32(U32_MAX, denali->reg + INTR_EN(i));
108 iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500109}
110
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900111static void __maybe_unused denali_disable_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500112{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900113 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500114
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900115 for (i = 0; i < DENALI_NR_BANKS; i++)
116 iowrite32(0, denali->reg + INTR_EN(i));
117 iowrite32(0, denali->reg + GLOBAL_INT_ENABLE);
118}
Chin Liang See03534df2014-09-12 00:42:17 -0500119
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900120static void denali_clear_irq(struct denali_nand_info *denali,
121 int bank, uint32_t irq_status)
122{
123 /* write one to clear bits */
124 iowrite32(irq_status, denali->reg + INTR_STATUS(bank));
125}
Chin Liang See03534df2014-09-12 00:42:17 -0500126
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900127static void denali_clear_irq_all(struct denali_nand_info *denali)
128{
129 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500130
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900131 for (i = 0; i < DENALI_NR_BANKS; i++)
132 denali_clear_irq(denali, i, U32_MAX);
133}
Chin Liang See03534df2014-09-12 00:42:17 -0500134
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900135static void __denali_check_irq(struct denali_nand_info *denali)
136{
137 uint32_t irq_status;
138 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500139
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900140 for (i = 0; i < DENALI_NR_BANKS; i++) {
141 irq_status = ioread32(denali->reg + INTR_STATUS(i));
142 denali_clear_irq(denali, i, irq_status);
Chin Liang See03534df2014-09-12 00:42:17 -0500143
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900144 if (i != denali->active_bank)
145 continue;
Chin Liang See03534df2014-09-12 00:42:17 -0500146
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900147 denali->irq_status |= irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500148 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900149}
Chin Liang See03534df2014-09-12 00:42:17 -0500150
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900151static void denali_reset_irq(struct denali_nand_info *denali)
152{
153 denali->irq_status = 0;
154 denali->irq_mask = 0;
155}
Chin Liang See03534df2014-09-12 00:42:17 -0500156
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900157static uint32_t denali_wait_for_irq(struct denali_nand_info *denali,
158 uint32_t irq_mask)
159{
160 unsigned long time_left = 1000000;
Chin Liang See03534df2014-09-12 00:42:17 -0500161
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900162 while (time_left) {
163 __denali_check_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500164
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900165 if (irq_mask & denali->irq_status)
166 return denali->irq_status;
167 udelay(1);
168 time_left--;
Chin Liang See03534df2014-09-12 00:42:17 -0500169 }
170
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900171 if (!time_left) {
172 dev_err(denali->dev, "timeout while waiting for irq 0x%x\n",
173 irq_mask);
174 return 0;
175 }
Chin Liang See03534df2014-09-12 00:42:17 -0500176
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900177 return denali->irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500178}
179
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900180static uint32_t denali_check_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500181{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900182 __denali_check_irq(denali);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500183
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900184 return denali->irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500185}
186
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900187static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500188{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900189 struct denali_nand_info *denali = mtd_to_denali(mtd);
190 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
191 int i;
192
193 for (i = 0; i < len; i++)
194 buf[i] = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500195}
196
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900197static void denali_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500198{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900199 struct denali_nand_info *denali = mtd_to_denali(mtd);
200 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
201 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500202
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900203 for (i = 0; i < len; i++)
204 denali->host_write(denali, addr, buf[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500205}
206
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900207static void denali_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500208{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900209 struct denali_nand_info *denali = mtd_to_denali(mtd);
210 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
211 uint16_t *buf16 = (uint16_t *)buf;
212 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500213
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900214 for (i = 0; i < len / 2; i++)
215 buf16[i] = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500216}
217
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900218static void denali_write_buf16(struct mtd_info *mtd, const uint8_t *buf,
219 int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500220{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900221 struct denali_nand_info *denali = mtd_to_denali(mtd);
222 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
223 const uint16_t *buf16 = (const uint16_t *)buf;
Chin Liang See03534df2014-09-12 00:42:17 -0500224 int i;
225
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900226 for (i = 0; i < len / 2; i++)
227 denali->host_write(denali, addr, buf16[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500228}
229
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900230static uint8_t denali_read_byte(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500231{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900232 uint8_t byte;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900233
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900234 denali_read_buf(mtd, &byte, 1);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900235
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900236 return byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500237}
238
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900239static void denali_write_byte(struct mtd_info *mtd, uint8_t byte)
Chin Liang See03534df2014-09-12 00:42:17 -0500240{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900241 denali_write_buf(mtd, &byte, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500242}
243
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900244static uint16_t denali_read_word(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500245{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900246 uint16_t word;
Chin Liang See03534df2014-09-12 00:42:17 -0500247
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900248 denali_read_buf16(mtd, (uint8_t *)&word, 2);
Chin Liang See03534df2014-09-12 00:42:17 -0500249
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900250 return word;
251}
Chin Liang See03534df2014-09-12 00:42:17 -0500252
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900253static void denali_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
254{
255 struct denali_nand_info *denali = mtd_to_denali(mtd);
256 uint32_t type;
Chin Liang See03534df2014-09-12 00:42:17 -0500257
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900258 if (ctrl & NAND_CLE)
259 type = DENALI_MAP11_CMD;
260 else if (ctrl & NAND_ALE)
261 type = DENALI_MAP11_ADDR;
262 else
263 return;
Chin Liang See03534df2014-09-12 00:42:17 -0500264
Scott Wood3ea94ed2015-06-26 19:03:26 -0500265 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900266 * Some commands are followed by chip->dev_ready or chip->waitfunc.
267 * irq_status must be cleared here to catch the R/B# interrupt later.
Chin Liang See03534df2014-09-12 00:42:17 -0500268 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900269 if (ctrl & NAND_CTRL_CHANGE)
270 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500271
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900272 denali->host_write(denali, DENALI_BANK(denali) | type, dat);
Chin Liang See03534df2014-09-12 00:42:17 -0500273}
274
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900275static int denali_dev_ready(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500276{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900277 struct denali_nand_info *denali = mtd_to_denali(mtd);
278
279 return !!(denali_check_irq(denali) & INTR__INT_ACT);
Chin Liang See03534df2014-09-12 00:42:17 -0500280}
281
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900282static int denali_check_erased_page(struct mtd_info *mtd,
283 struct nand_chip *chip, uint8_t *buf,
284 unsigned long uncor_ecc_flags,
285 unsigned int max_bitflips)
Chin Liang See03534df2014-09-12 00:42:17 -0500286{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900287 uint8_t *ecc_code = chip->buffers->ecccode;
288 int ecc_steps = chip->ecc.steps;
289 int ecc_size = chip->ecc.size;
290 int ecc_bytes = chip->ecc.bytes;
291 int i, ret, stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500292
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900293 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
294 chip->ecc.total);
295 if (ret)
296 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500297
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900298 for (i = 0; i < ecc_steps; i++) {
299 if (!(uncor_ecc_flags & BIT(i)))
300 continue;
Chin Liang See03534df2014-09-12 00:42:17 -0500301
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900302 stat = nand_check_erased_ecc_chunk(buf, ecc_size,
303 ecc_code, ecc_bytes,
304 NULL, 0,
305 chip->ecc.strength);
306 if (stat < 0) {
307 mtd->ecc_stats.failed++;
308 } else {
309 mtd->ecc_stats.corrected += stat;
310 max_bitflips = max_t(unsigned int, max_bitflips, stat);
311 }
Chin Liang See03534df2014-09-12 00:42:17 -0500312
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900313 buf += ecc_size;
314 ecc_code += ecc_bytes;
315 }
316
317 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500318}
319
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900320static int denali_hw_ecc_fixup(struct mtd_info *mtd,
321 struct denali_nand_info *denali,
322 unsigned long *uncor_ecc_flags)
Chin Liang See03534df2014-09-12 00:42:17 -0500323{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900324 struct nand_chip *chip = mtd_to_nand(mtd);
325 int bank = denali->active_bank;
326 uint32_t ecc_cor;
327 unsigned int max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500328
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900329 ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank));
330 ecc_cor >>= ECC_COR_INFO__SHIFT(bank);
331
332 if (ecc_cor & ECC_COR_INFO__UNCOR_ERR) {
333 /*
334 * This flag is set when uncorrectable error occurs at least in
335 * one ECC sector. We can not know "how many sectors", or
336 * "which sector(s)". We need erase-page check for all sectors.
337 */
338 *uncor_ecc_flags = GENMASK(chip->ecc.steps - 1, 0);
339 return 0;
340 }
Chin Liang See03534df2014-09-12 00:42:17 -0500341
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900342 max_bitflips = FIELD_GET(ECC_COR_INFO__MAX_ERRORS, ecc_cor);
343
344 /*
345 * The register holds the maximum of per-sector corrected bitflips.
346 * This is suitable for the return value of the ->read_page() callback.
347 * Unfortunately, we can not know the total number of corrected bits in
348 * the page. Increase the stats by max_bitflips. (compromised solution)
349 */
350 mtd->ecc_stats.corrected += max_bitflips;
351
352 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500353}
354
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900355static int denali_sw_ecc_fixup(struct mtd_info *mtd,
356 struct denali_nand_info *denali,
357 unsigned long *uncor_ecc_flags, uint8_t *buf)
Chin Liang See03534df2014-09-12 00:42:17 -0500358{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900359 unsigned int ecc_size = denali->nand.ecc.size;
360 unsigned int bitflips = 0;
361 unsigned int max_bitflips = 0;
362 uint32_t err_addr, err_cor_info;
363 unsigned int err_byte, err_sector, err_device;
364 uint8_t err_cor_value;
365 unsigned int prev_sector = 0;
366 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500367
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900368 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500369
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900370 do {
371 err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS);
372 err_sector = FIELD_GET(ECC_ERROR_ADDRESS__SECTOR, err_addr);
373 err_byte = FIELD_GET(ECC_ERROR_ADDRESS__OFFSET, err_addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500374
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900375 err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO);
376 err_cor_value = FIELD_GET(ERR_CORRECTION_INFO__BYTE,
377 err_cor_info);
378 err_device = FIELD_GET(ERR_CORRECTION_INFO__DEVICE,
379 err_cor_info);
Chin Liang See03534df2014-09-12 00:42:17 -0500380
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900381 /* reset the bitflip counter when crossing ECC sector */
382 if (err_sector != prev_sector)
383 bitflips = 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500384
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900385 if (err_cor_info & ERR_CORRECTION_INFO__UNCOR) {
386 /*
387 * Check later if this is a real ECC error, or
388 * an erased sector.
389 */
390 *uncor_ecc_flags |= BIT(err_sector);
391 } else if (err_byte < ecc_size) {
392 /*
393 * If err_byte is larger than ecc_size, means error
394 * happened in OOB, so we ignore it. It's no need for
395 * us to correct it err_device is represented the NAND
396 * error bits are happened in if there are more than
397 * one NAND connected.
398 */
399 int offset;
400 unsigned int flips_in_byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500401
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900402 offset = (err_sector * ecc_size + err_byte) *
403 denali->devs_per_cs + err_device;
Chin Liang See03534df2014-09-12 00:42:17 -0500404
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900405 /* correct the ECC error */
406 flips_in_byte = hweight8(buf[offset] ^ err_cor_value);
407 buf[offset] ^= err_cor_value;
408 mtd->ecc_stats.corrected += flips_in_byte;
409 bitflips += flips_in_byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500410
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900411 max_bitflips = max(max_bitflips, bitflips);
412 }
Chin Liang See03534df2014-09-12 00:42:17 -0500413
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900414 prev_sector = err_sector;
415 } while (!(err_cor_info & ERR_CORRECTION_INFO__LAST_ERR));
Chin Liang See03534df2014-09-12 00:42:17 -0500416
Scott Wood3ea94ed2015-06-26 19:03:26 -0500417 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900418 * Once handle all ECC errors, controller will trigger an
419 * ECC_TRANSACTION_DONE interrupt.
Scott Wood3ea94ed2015-06-26 19:03:26 -0500420 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900421 irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE);
422 if (!(irq_status & INTR__ECC_TRANSACTION_DONE))
423 return -EIO;
Chin Liang See03534df2014-09-12 00:42:17 -0500424
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900425 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500426}
427
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900428static void denali_setup_dma64(struct denali_nand_info *denali,
429 dma_addr_t dma_addr, int page, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500430{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900431 uint32_t mode;
432 const int page_count = 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500433
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900434 mode = DENALI_MAP10 | DENALI_BANK(denali) | page;
Chin Liang See03534df2014-09-12 00:42:17 -0500435
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900436 /* DMA is a three step process */
Chin Liang See03534df2014-09-12 00:42:17 -0500437
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900438 /*
439 * 1. setup transfer type, interrupt when complete,
440 * burst len = 64 bytes, the number of pages
441 */
442 denali->host_write(denali, mode,
443 0x01002000 | (64 << 16) | (write << 8) | page_count);
Chin Liang See03534df2014-09-12 00:42:17 -0500444
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900445 /* 2. set memory low address */
446 denali->host_write(denali, mode, lower_32_bits(dma_addr));
Chin Liang See03534df2014-09-12 00:42:17 -0500447
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900448 /* 3. set memory high address */
449 denali->host_write(denali, mode, upper_32_bits(dma_addr));
Chin Liang See03534df2014-09-12 00:42:17 -0500450}
451
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900452static void denali_setup_dma32(struct denali_nand_info *denali,
453 dma_addr_t dma_addr, int page, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500454{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900455 uint32_t mode;
456 const int page_count = 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500457
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900458 mode = DENALI_MAP10 | DENALI_BANK(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500459
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900460 /* DMA is a four step process */
Chin Liang See03534df2014-09-12 00:42:17 -0500461
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900462 /* 1. setup transfer type and # of pages */
463 denali->host_write(denali, mode | page,
464 0x2000 | (write << 8) | page_count);
Chin Liang See03534df2014-09-12 00:42:17 -0500465
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900466 /* 2. set memory high address bits 23:8 */
467 denali->host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200);
Chin Liang See03534df2014-09-12 00:42:17 -0500468
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900469 /* 3. set memory low address bits 23:8 */
470 denali->host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300);
Chin Liang See03534df2014-09-12 00:42:17 -0500471
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900472 /* 4. interrupt when complete, burst len = 64 bytes */
473 denali->host_write(denali, mode | 0x14000, 0x2400);
Chin Liang See03534df2014-09-12 00:42:17 -0500474}
475
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900476static int denali_pio_read(struct denali_nand_info *denali, void *buf,
477 size_t size, int page, int raw)
Chin Liang See03534df2014-09-12 00:42:17 -0500478{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900479 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
480 uint32_t *buf32 = (uint32_t *)buf;
481 uint32_t irq_status, ecc_err_mask;
482 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500483
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900484 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
485 ecc_err_mask = INTR__ECC_UNCOR_ERR;
486 else
487 ecc_err_mask = INTR__ECC_ERR;
Chin Liang See03534df2014-09-12 00:42:17 -0500488
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900489 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500490
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900491 for (i = 0; i < size / 4; i++)
492 *buf32++ = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500493
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900494 irq_status = denali_wait_for_irq(denali, INTR__PAGE_XFER_INC);
495 if (!(irq_status & INTR__PAGE_XFER_INC))
496 return -EIO;
Chin Liang See03534df2014-09-12 00:42:17 -0500497
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900498 if (irq_status & INTR__ERASED_PAGE)
499 memset(buf, 0xff, size);
500
501 return irq_status & ecc_err_mask ? -EBADMSG : 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500502}
503
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900504static int denali_pio_write(struct denali_nand_info *denali,
505 const void *buf, size_t size, int page, int raw)
Chin Liang See03534df2014-09-12 00:42:17 -0500506{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900507 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
508 const uint32_t *buf32 = (uint32_t *)buf;
509 uint32_t irq_status;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500510 int i;
511
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900512 denali_reset_irq(denali);
513
514 for (i = 0; i < size / 4; i++)
515 denali->host_write(denali, addr, *buf32++);
516
517 irq_status = denali_wait_for_irq(denali,
518 INTR__PROGRAM_COMP | INTR__PROGRAM_FAIL);
519 if (!(irq_status & INTR__PROGRAM_COMP))
520 return -EIO;
521
522 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500523}
524
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900525static int denali_pio_xfer(struct denali_nand_info *denali, void *buf,
526 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500527{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900528 if (write)
529 return denali_pio_write(denali, buf, size, page, raw);
530 else
531 return denali_pio_read(denali, buf, size, page, raw);
Chin Liang See03534df2014-09-12 00:42:17 -0500532}
533
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900534static int denali_dma_xfer(struct denali_nand_info *denali, void *buf,
535 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500536{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900537 dma_addr_t dma_addr;
538 uint32_t irq_mask, irq_status, ecc_err_mask;
539 enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
540 int ret = 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500541
Vignesh Raghavendradd62b9a2020-01-16 14:23:47 +0530542 dma_addr = dma_map_single(buf, size, dir);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900543 if (dma_mapping_error(denali->dev, dma_addr)) {
544 dev_dbg(denali->dev, "Failed to DMA-map buffer. Trying PIO.\n");
545 return denali_pio_xfer(denali, buf, size, page, raw, write);
546 }
Chin Liang See03534df2014-09-12 00:42:17 -0500547
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900548 if (write) {
549 /*
550 * INTR__PROGRAM_COMP is never asserted for the DMA transfer.
551 * We can use INTR__DMA_CMD_COMP instead. This flag is asserted
552 * when the page program is completed.
553 */
554 irq_mask = INTR__DMA_CMD_COMP | INTR__PROGRAM_FAIL;
555 ecc_err_mask = 0;
556 } else if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) {
557 irq_mask = INTR__DMA_CMD_COMP;
558 ecc_err_mask = INTR__ECC_UNCOR_ERR;
559 } else {
560 irq_mask = INTR__DMA_CMD_COMP;
561 ecc_err_mask = INTR__ECC_ERR;
562 }
Chin Liang See03534df2014-09-12 00:42:17 -0500563
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900564 iowrite32(DMA_ENABLE__FLAG, denali->reg + DMA_ENABLE);
Masahiro Yamada9ea23972018-12-19 20:03:19 +0900565 /*
566 * The ->setup_dma() hook kicks DMA by using the data/command
567 * interface, which belongs to a different AXI port from the
568 * register interface. Read back the register to avoid a race.
569 */
570 ioread32(denali->reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500571
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900572 denali_reset_irq(denali);
573 denali->setup_dma(denali, dma_addr, page, write);
Chin Liang See03534df2014-09-12 00:42:17 -0500574
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900575 irq_status = denali_wait_for_irq(denali, irq_mask);
576 if (!(irq_status & INTR__DMA_CMD_COMP))
577 ret = -EIO;
578 else if (irq_status & ecc_err_mask)
579 ret = -EBADMSG;
Chin Liang See03534df2014-09-12 00:42:17 -0500580
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900581 iowrite32(0, denali->reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500582
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900583 dma_unmap_single(dma_addr, size, dir);
Chin Liang See03534df2014-09-12 00:42:17 -0500584
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900585 if (irq_status & INTR__ERASED_PAGE)
586 memset(buf, 0xff, size);
Chin Liang See03534df2014-09-12 00:42:17 -0500587
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900588 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500589}
590
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900591static int denali_data_xfer(struct denali_nand_info *denali, void *buf,
592 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500593{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900594 iowrite32(raw ? 0 : ECC_ENABLE__FLAG, denali->reg + ECC_ENABLE);
595 iowrite32(raw ? TRANSFER_SPARE_REG__FLAG : 0,
596 denali->reg + TRANSFER_SPARE_REG);
Chin Liang See03534df2014-09-12 00:42:17 -0500597
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900598 if (denali->dma_avail)
599 return denali_dma_xfer(denali, buf, size, page, raw, write);
600 else
601 return denali_pio_xfer(denali, buf, size, page, raw, write);
602}
Chin Liang See03534df2014-09-12 00:42:17 -0500603
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900604static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip,
605 int page, int write)
606{
607 struct denali_nand_info *denali = mtd_to_denali(mtd);
608 unsigned int start_cmd = write ? NAND_CMD_SEQIN : NAND_CMD_READ0;
609 unsigned int rnd_cmd = write ? NAND_CMD_RNDIN : NAND_CMD_RNDOUT;
610 int writesize = mtd->writesize;
611 int oobsize = mtd->oobsize;
612 uint8_t *bufpoi = chip->oob_poi;
613 int ecc_steps = chip->ecc.steps;
614 int ecc_size = chip->ecc.size;
615 int ecc_bytes = chip->ecc.bytes;
616 int oob_skip = denali->oob_skip_bytes;
617 size_t size = writesize + oobsize;
618 int i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500619
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900620 /* BBM at the beginning of the OOB area */
621 chip->cmdfunc(mtd, start_cmd, writesize, page);
622 if (write)
623 chip->write_buf(mtd, bufpoi, oob_skip);
624 else
625 chip->read_buf(mtd, bufpoi, oob_skip);
626 bufpoi += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500627
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900628 /* OOB ECC */
629 for (i = 0; i < ecc_steps; i++) {
630 pos = ecc_size + i * (ecc_size + ecc_bytes);
631 len = ecc_bytes;
Chin Liang See03534df2014-09-12 00:42:17 -0500632
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900633 if (pos >= writesize)
634 pos += oob_skip;
635 else if (pos + len > writesize)
636 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500637
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900638 chip->cmdfunc(mtd, rnd_cmd, pos, -1);
639 if (write)
640 chip->write_buf(mtd, bufpoi, len);
641 else
642 chip->read_buf(mtd, bufpoi, len);
643 bufpoi += len;
644 if (len < ecc_bytes) {
645 len = ecc_bytes - len;
646 chip->cmdfunc(mtd, rnd_cmd, writesize + oob_skip, -1);
647 if (write)
648 chip->write_buf(mtd, bufpoi, len);
649 else
650 chip->read_buf(mtd, bufpoi, len);
651 bufpoi += len;
652 }
653 }
Chin Liang See03534df2014-09-12 00:42:17 -0500654
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900655 /* OOB free */
656 len = oobsize - (bufpoi - chip->oob_poi);
657 chip->cmdfunc(mtd, rnd_cmd, size - len, -1);
658 if (write)
659 chip->write_buf(mtd, bufpoi, len);
660 else
661 chip->read_buf(mtd, bufpoi, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500662}
663
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900664static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
665 uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500666{
667 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900668 int writesize = mtd->writesize;
669 int oobsize = mtd->oobsize;
670 int ecc_steps = chip->ecc.steps;
671 int ecc_size = chip->ecc.size;
672 int ecc_bytes = chip->ecc.bytes;
673 void *tmp_buf = denali->buf;
674 int oob_skip = denali->oob_skip_bytes;
675 size_t size = writesize + oobsize;
676 int ret, i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500677
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900678 ret = denali_data_xfer(denali, tmp_buf, size, page, 1, 0);
679 if (ret)
680 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500681
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900682 /* Arrange the buffer for syndrome payload/ecc layout */
683 if (buf) {
684 for (i = 0; i < ecc_steps; i++) {
685 pos = i * (ecc_size + ecc_bytes);
686 len = ecc_size;
Chin Liang See03534df2014-09-12 00:42:17 -0500687
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900688 if (pos >= writesize)
689 pos += oob_skip;
690 else if (pos + len > writesize)
691 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500692
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900693 memcpy(buf, tmp_buf + pos, len);
694 buf += len;
695 if (len < ecc_size) {
696 len = ecc_size - len;
697 memcpy(buf, tmp_buf + writesize + oob_skip,
698 len);
699 buf += len;
700 }
701 }
702 }
Chin Liang See03534df2014-09-12 00:42:17 -0500703
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900704 if (oob_required) {
705 uint8_t *oob = chip->oob_poi;
Chin Liang See03534df2014-09-12 00:42:17 -0500706
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900707 /* BBM at the beginning of the OOB area */
708 memcpy(oob, tmp_buf + writesize, oob_skip);
709 oob += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500710
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900711 /* OOB ECC */
712 for (i = 0; i < ecc_steps; i++) {
713 pos = ecc_size + i * (ecc_size + ecc_bytes);
714 len = ecc_bytes;
715
716 if (pos >= writesize)
717 pos += oob_skip;
718 else if (pos + len > writesize)
719 len = writesize - pos;
720
721 memcpy(oob, tmp_buf + pos, len);
722 oob += len;
723 if (len < ecc_bytes) {
724 len = ecc_bytes - len;
725 memcpy(oob, tmp_buf + writesize + oob_skip,
726 len);
727 oob += len;
728 }
729 }
730
731 /* OOB free */
732 len = oobsize - (oob - chip->oob_poi);
733 memcpy(oob, tmp_buf + size - len, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500734 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900735
Chin Liang See03534df2014-09-12 00:42:17 -0500736 return 0;
737}
738
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900739static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
740 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500741{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900742 denali_oob_xfer(mtd, chip, page, 0);
Chin Liang See03534df2014-09-12 00:42:17 -0500743
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900744 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500745}
746
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900747static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
748 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500749{
750 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900751 int status;
Chin Liang See03534df2014-09-12 00:42:17 -0500752
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900753 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500754
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900755 denali_oob_xfer(mtd, chip, page, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500756
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900757 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
758 status = chip->waitfunc(mtd, chip);
Chin Liang See03534df2014-09-12 00:42:17 -0500759
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900760 return status & NAND_STATUS_FAIL ? -EIO : 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500761}
762
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900763static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
764 uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500765{
766 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900767 unsigned long uncor_ecc_flags = 0;
768 int stat = 0;
769 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500770
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900771 ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0);
772 if (ret && ret != -EBADMSG)
773 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500774
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900775 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
776 stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags);
777 else if (ret == -EBADMSG)
778 stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf);
Chin Liang See03534df2014-09-12 00:42:17 -0500779
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900780 if (stat < 0)
781 return stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500782
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900783 if (uncor_ecc_flags) {
784 ret = denali_read_oob(mtd, chip, page);
785 if (ret)
786 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500787
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900788 stat = denali_check_erased_page(mtd, chip, buf,
789 uncor_ecc_flags, stat);
Chin Liang See03534df2014-09-12 00:42:17 -0500790 }
791
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900792 return stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500793}
794
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900795static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
796 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500797{
798 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900799 int writesize = mtd->writesize;
800 int oobsize = mtd->oobsize;
801 int ecc_steps = chip->ecc.steps;
802 int ecc_size = chip->ecc.size;
803 int ecc_bytes = chip->ecc.bytes;
804 void *tmp_buf = denali->buf;
805 int oob_skip = denali->oob_skip_bytes;
806 size_t size = writesize + oobsize;
807 int i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500808
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900809 /*
810 * Fill the buffer with 0xff first except the full page transfer.
811 * This simplifies the logic.
812 */
813 if (!buf || !oob_required)
814 memset(tmp_buf, 0xff, size);
815
816 /* Arrange the buffer for syndrome payload/ecc layout */
817 if (buf) {
818 for (i = 0; i < ecc_steps; i++) {
819 pos = i * (ecc_size + ecc_bytes);
820 len = ecc_size;
821
822 if (pos >= writesize)
823 pos += oob_skip;
824 else if (pos + len > writesize)
825 len = writesize - pos;
826
827 memcpy(tmp_buf + pos, buf, len);
828 buf += len;
829 if (len < ecc_size) {
830 len = ecc_size - len;
831 memcpy(tmp_buf + writesize + oob_skip, buf,
832 len);
833 buf += len;
834 }
835 }
Chin Liang See03534df2014-09-12 00:42:17 -0500836 }
837
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900838 if (oob_required) {
839 const uint8_t *oob = chip->oob_poi;
Chin Liang See03534df2014-09-12 00:42:17 -0500840
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900841 /* BBM at the beginning of the OOB area */
842 memcpy(tmp_buf + writesize, oob, oob_skip);
843 oob += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500844
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900845 /* OOB ECC */
846 for (i = 0; i < ecc_steps; i++) {
847 pos = ecc_size + i * (ecc_size + ecc_bytes);
848 len = ecc_bytes;
Chin Liang See03534df2014-09-12 00:42:17 -0500849
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900850 if (pos >= writesize)
851 pos += oob_skip;
852 else if (pos + len > writesize)
853 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500854
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900855 memcpy(tmp_buf + pos, oob, len);
856 oob += len;
857 if (len < ecc_bytes) {
858 len = ecc_bytes - len;
859 memcpy(tmp_buf + writesize + oob_skip, oob,
860 len);
861 oob += len;
862 }
Chin Liang See03534df2014-09-12 00:42:17 -0500863 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900864
865 /* OOB free */
866 len = oobsize - (oob - chip->oob_poi);
867 memcpy(tmp_buf + size - len, oob, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500868 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900869
870 return denali_data_xfer(denali, tmp_buf, size, page, 1, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500871}
872
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900873static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
874 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500875{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900876 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chin Liang See03534df2014-09-12 00:42:17 -0500877
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900878 return denali_data_xfer(denali, (void *)buf, mtd->writesize,
879 page, 0, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500880}
881
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900882static void denali_select_chip(struct mtd_info *mtd, int chip)
Chin Liang See03534df2014-09-12 00:42:17 -0500883{
884 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chin Liang See03534df2014-09-12 00:42:17 -0500885
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900886 denali->active_bank = chip;
Chin Liang See03534df2014-09-12 00:42:17 -0500887}
888
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900889static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
Chin Liang See03534df2014-09-12 00:42:17 -0500890{
891 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900892 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500893
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900894 /* R/B# pin transitioned from low to high? */
895 irq_status = denali_wait_for_irq(denali, INTR__INT_ACT);
Chin Liang See03534df2014-09-12 00:42:17 -0500896
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900897 return irq_status & INTR__INT_ACT ? 0 : NAND_STATUS_FAIL;
Chin Liang See03534df2014-09-12 00:42:17 -0500898}
899
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900900static int denali_erase(struct mtd_info *mtd, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500901{
902 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900903 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500904
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900905 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500906
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900907 denali->host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page,
908 DENALI_ERASE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500909
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900910 /* wait for erase to complete or failure to occur */
911 irq_status = denali_wait_for_irq(denali,
912 INTR__ERASE_COMP | INTR__ERASE_FAIL);
Chin Liang See03534df2014-09-12 00:42:17 -0500913
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900914 return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL;
Chin Liang See03534df2014-09-12 00:42:17 -0500915}
916
Masahiro Yamada59a1f3e2017-11-29 19:18:18 +0900917static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900918 const struct nand_data_interface *conf)
Chin Liang See03534df2014-09-12 00:42:17 -0500919{
920 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900921 const struct nand_sdr_timings *timings;
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900922 unsigned long t_x, mult_x;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900923 int acc_clks, re_2_we, re_2_re, we_2_re, addr_2_data;
924 int rdwr_en_lo, rdwr_en_hi, rdwr_en_lo_hi, cs_setup;
925 int addr_2_data_mask;
926 uint32_t tmp;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500927
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900928 timings = nand_get_sdr_timings(conf);
929 if (IS_ERR(timings))
930 return PTR_ERR(timings);
Chin Liang See03534df2014-09-12 00:42:17 -0500931
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900932 /* clk_x period in picoseconds */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900933 t_x = DIV_ROUND_DOWN_ULL(1000000000000ULL, denali->clk_x_rate);
934 if (!t_x)
935 return -EINVAL;
936
937 /*
938 * The bus interface clock, clk_x, is phase aligned with the core clock.
939 * The clk_x is an integral multiple N of the core clk. The value N is
940 * configured at IP delivery time, and its available value is 4, 5, 6.
941 */
942 mult_x = DIV_ROUND_CLOSEST_ULL(denali->clk_x_rate, denali->clk_rate);
943 if (mult_x < 4 || mult_x > 6)
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900944 return -EINVAL;
Chin Liang See03534df2014-09-12 00:42:17 -0500945
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900946 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
947 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500948
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900949 /* tREA -> ACC_CLKS */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900950 acc_clks = DIV_ROUND_UP(timings->tREA_max, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900951 acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
952
953 tmp = ioread32(denali->reg + ACC_CLKS);
954 tmp &= ~ACC_CLKS__VALUE;
955 tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
956 iowrite32(tmp, denali->reg + ACC_CLKS);
957
958 /* tRWH -> RE_2_WE */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900959 re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900960 re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE);
Chin Liang See03534df2014-09-12 00:42:17 -0500961
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900962 tmp = ioread32(denali->reg + RE_2_WE);
963 tmp &= ~RE_2_WE__VALUE;
964 tmp |= FIELD_PREP(RE_2_WE__VALUE, re_2_we);
965 iowrite32(tmp, denali->reg + RE_2_WE);
966
967 /* tRHZ -> RE_2_RE */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900968 re_2_re = DIV_ROUND_UP(timings->tRHZ_max, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900969 re_2_re = min_t(int, re_2_re, RE_2_RE__VALUE);
970
971 tmp = ioread32(denali->reg + RE_2_RE);
972 tmp &= ~RE_2_RE__VALUE;
973 tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re);
974 iowrite32(tmp, denali->reg + RE_2_RE);
975
976 /*
977 * tCCS, tWHR -> WE_2_RE
978 *
979 * With WE_2_RE properly set, the Denali controller automatically takes
980 * care of the delay; the driver need not set NAND_WAIT_TCCS.
981 */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900982 we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min), t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900983 we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE);
984
985 tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
986 tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE;
987 tmp |= FIELD_PREP(TWHR2_AND_WE_2_RE__WE_2_RE, we_2_re);
988 iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE);
989
990 /* tADL -> ADDR_2_DATA */
991
992 /* for older versions, ADDR_2_DATA is only 6 bit wide */
993 addr_2_data_mask = TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
994 if (denali->revision < 0x0501)
995 addr_2_data_mask >>= 1;
996
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900997 addr_2_data = DIV_ROUND_UP(timings->tADL_min, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900998 addr_2_data = min_t(int, addr_2_data, addr_2_data_mask);
999
1000 tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA);
1001 tmp &= ~TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1002 tmp |= FIELD_PREP(TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA, addr_2_data);
1003 iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA);
1004
1005 /* tREH, tWH -> RDWR_EN_HI_CNT */
1006 rdwr_en_hi = DIV_ROUND_UP(max(timings->tREH_min, timings->tWH_min),
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001007 t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001008 rdwr_en_hi = min_t(int, rdwr_en_hi, RDWR_EN_HI_CNT__VALUE);
1009
1010 tmp = ioread32(denali->reg + RDWR_EN_HI_CNT);
1011 tmp &= ~RDWR_EN_HI_CNT__VALUE;
1012 tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi);
1013 iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT);
1014
1015 /* tRP, tWP -> RDWR_EN_LO_CNT */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001016 rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min), t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001017 rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min),
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001018 t_x);
1019 rdwr_en_lo_hi = max_t(int, rdwr_en_lo_hi, mult_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001020 rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
1021 rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
1022
1023 tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
1024 tmp &= ~RDWR_EN_LO_CNT__VALUE;
1025 tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo);
1026 iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT);
1027
1028 /* tCS, tCEA -> CS_SETUP_CNT */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001029 cs_setup = max3((int)DIV_ROUND_UP(timings->tCS_min, t_x) - rdwr_en_lo,
1030 (int)DIV_ROUND_UP(timings->tCEA_max, t_x) - acc_clks,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001031 0);
1032 cs_setup = min_t(int, cs_setup, CS_SETUP_CNT__VALUE);
1033
1034 tmp = ioread32(denali->reg + CS_SETUP_CNT);
1035 tmp &= ~CS_SETUP_CNT__VALUE;
1036 tmp |= FIELD_PREP(CS_SETUP_CNT__VALUE, cs_setup);
1037 iowrite32(tmp, denali->reg + CS_SETUP_CNT);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001038
1039 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -05001040}
1041
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001042static void denali_reset_banks(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001043{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001044 u32 irq_status;
1045 int i;
Chin Liang See03534df2014-09-12 00:42:17 -05001046
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001047 for (i = 0; i < denali->max_banks; i++) {
1048 denali->active_bank = i;
1049
1050 denali_reset_irq(denali);
1051
1052 iowrite32(DEVICE_RESET__BANK(i),
1053 denali->reg + DEVICE_RESET);
1054
1055 irq_status = denali_wait_for_irq(denali,
1056 INTR__RST_COMP | INTR__INT_ACT | INTR__TIME_OUT);
1057 if (!(irq_status & INTR__INT_ACT))
1058 break;
Chin Liang See03534df2014-09-12 00:42:17 -05001059 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001060
1061 dev_dbg(denali->dev, "%d chips connected\n", i);
1062 denali->max_banks = i;
Chin Liang See03534df2014-09-12 00:42:17 -05001063}
Chin Liang See03534df2014-09-12 00:42:17 -05001064
Chin Liang See03534df2014-09-12 00:42:17 -05001065static void denali_hw_init(struct denali_nand_info *denali)
1066{
1067 /*
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001068 * The REVISION register may not be reliable. Platforms are allowed to
1069 * override it.
1070 */
1071 if (!denali->revision)
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001072 denali->revision = swab16(ioread32(denali->reg + REVISION));
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001073
1074 /*
Masahiro Yamada6be38732020-01-30 00:55:55 +09001075 * Set how many bytes should be skipped before writing data in OOB.
1076 * If a platform requests a non-zero value, set it to the register.
1077 * Otherwise, read the value out, expecting it has already been set up
1078 * by firmware.
Chin Liang See03534df2014-09-12 00:42:17 -05001079 */
Masahiro Yamada6be38732020-01-30 00:55:55 +09001080 if (denali->oob_skip_bytes)
1081 iowrite32(denali->oob_skip_bytes,
1082 denali->reg + SPARE_AREA_SKIP_BYTES);
1083 else
1084 denali->oob_skip_bytes = ioread32(denali->reg +
1085 SPARE_AREA_SKIP_BYTES);
1086
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001087 denali_detect_max_banks(denali);
1088 iowrite32(0x0F, denali->reg + RB_PIN_ENABLED);
1089 iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE);
Chin Liang See03534df2014-09-12 00:42:17 -05001090
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001091 iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER);
Chin Liang See03534df2014-09-12 00:42:17 -05001092}
1093
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001094int denali_calc_ecc_bytes(int step_size, int strength)
1095{
1096 /* BCH code. Denali requires ecc.bytes to be multiple of 2 */
1097 return DIV_ROUND_UP(strength * fls(step_size * 8), 16) * 2;
1098}
1099EXPORT_SYMBOL(denali_calc_ecc_bytes);
Chin Liang See03534df2014-09-12 00:42:17 -05001100
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001101static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip,
1102 struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001103{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001104 int oobavail = mtd->oobsize - denali->oob_skip_bytes;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001105 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001106
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001107 /*
1108 * If .size and .strength are already set (usually by DT),
1109 * check if they are supported by this controller.
1110 */
1111 if (chip->ecc.size && chip->ecc.strength)
1112 return nand_check_ecc_caps(chip, denali->ecc_caps, oobavail);
1113
1114 /*
1115 * We want .size and .strength closest to the chip's requirement
1116 * unless NAND_ECC_MAXIMIZE is requested.
1117 */
1118 if (!(chip->ecc.options & NAND_ECC_MAXIMIZE)) {
1119 ret = nand_match_ecc_req(chip, denali->ecc_caps, oobavail);
1120 if (!ret)
1121 return 0;
1122 }
1123
1124 /* Max ECC strength is the last thing we can do */
1125 return nand_maximize_ecc(chip, denali->ecc_caps, oobavail);
1126}
1127
1128static struct nand_ecclayout nand_oob;
1129
1130static int denali_ooblayout_ecc(struct mtd_info *mtd, int section,
1131 struct mtd_oob_region *oobregion)
1132{
1133 struct denali_nand_info *denali = mtd_to_denali(mtd);
1134 struct nand_chip *chip = mtd_to_nand(mtd);
1135
1136 if (section)
1137 return -ERANGE;
1138
1139 oobregion->offset = denali->oob_skip_bytes;
1140 oobregion->length = chip->ecc.total;
1141
1142 return 0;
1143}
1144
1145static int denali_ooblayout_free(struct mtd_info *mtd, int section,
1146 struct mtd_oob_region *oobregion)
1147{
1148 struct denali_nand_info *denali = mtd_to_denali(mtd);
1149 struct nand_chip *chip = mtd_to_nand(mtd);
1150
1151 if (section)
1152 return -ERANGE;
1153
1154 oobregion->offset = chip->ecc.total + denali->oob_skip_bytes;
1155 oobregion->length = mtd->oobsize - oobregion->offset;
1156
1157 return 0;
1158}
Chin Liang See03534df2014-09-12 00:42:17 -05001159
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001160static const struct mtd_ooblayout_ops denali_ooblayout_ops = {
1161 .ecc = denali_ooblayout_ecc,
Simon Glass62fd1a42020-02-03 07:35:56 -07001162 .rfree = denali_ooblayout_free,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001163};
Chin Liang See03534df2014-09-12 00:42:17 -05001164
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001165static int denali_multidev_fixup(struct denali_nand_info *denali)
1166{
1167 struct nand_chip *chip = &denali->nand;
1168 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001169
1170 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001171 * Support for multi device:
1172 * When the IP configuration is x16 capable and two x8 chips are
1173 * connected in parallel, DEVICES_CONNECTED should be set to 2.
1174 * In this case, the core framework knows nothing about this fact,
1175 * so we should tell it the _logical_ pagesize and anything necessary.
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001176 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001177 denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED);
Chin Liang See03534df2014-09-12 00:42:17 -05001178
Chin Liang See03534df2014-09-12 00:42:17 -05001179 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001180 * On some SoCs, DEVICES_CONNECTED is not auto-detected.
1181 * For those, DEVICES_CONNECTED is left to 0. Set 1 if it is the case.
Chin Liang See03534df2014-09-12 00:42:17 -05001182 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001183 if (denali->devs_per_cs == 0) {
1184 denali->devs_per_cs = 1;
1185 iowrite32(1, denali->reg + DEVICES_CONNECTED);
1186 }
1187
1188 if (denali->devs_per_cs == 1)
1189 return 0;
1190
1191 if (denali->devs_per_cs != 2) {
1192 dev_err(denali->dev, "unsupported number of devices %d\n",
1193 denali->devs_per_cs);
1194 return -EINVAL;
1195 }
1196
1197 /* 2 chips in parallel */
1198 mtd->size <<= 1;
1199 mtd->erasesize <<= 1;
1200 mtd->writesize <<= 1;
1201 mtd->oobsize <<= 1;
1202 chip->chipsize <<= 1;
1203 chip->page_shift += 1;
1204 chip->phys_erase_shift += 1;
1205 chip->bbt_erase_shift += 1;
1206 chip->chip_shift += 1;
1207 chip->pagemask <<= 1;
1208 chip->ecc.size <<= 1;
1209 chip->ecc.bytes <<= 1;
1210 chip->ecc.strength <<= 1;
1211 denali->oob_skip_bytes <<= 1;
1212
1213 return 0;
1214}
1215
1216int denali_init(struct denali_nand_info *denali)
1217{
1218 struct nand_chip *chip = &denali->nand;
1219 struct mtd_info *mtd = nand_to_mtd(chip);
1220 u32 features = ioread32(denali->reg + FEATURES);
1221 int ret;
1222
1223 denali_hw_init(denali);
1224
1225 denali_clear_irq_all(denali);
1226
1227 denali_reset_banks(denali);
1228
1229 denali->active_bank = DENALI_INVALID_BANK;
1230
1231 chip->flash_node = dev_of_offset(denali->dev);
1232 /* Fallback to the default name if DT did not give "label" property */
1233 if (!mtd->name)
1234 mtd->name = "denali-nand";
Chin Liang See03534df2014-09-12 00:42:17 -05001235
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001236 chip->select_chip = denali_select_chip;
1237 chip->read_byte = denali_read_byte;
1238 chip->write_byte = denali_write_byte;
1239 chip->read_word = denali_read_word;
1240 chip->cmd_ctrl = denali_cmd_ctrl;
1241 chip->dev_ready = denali_dev_ready;
1242 chip->waitfunc = denali_waitfunc;
1243
1244 if (features & FEATURES__INDEX_ADDR) {
1245 denali->host_read = denali_indexed_read;
1246 denali->host_write = denali_indexed_write;
1247 } else {
1248 denali->host_read = denali_direct_read;
1249 denali->host_write = denali_direct_write;
1250 }
1251
1252 /* clk rate info is needed for setup_data_interface */
1253 if (denali->clk_x_rate)
1254 chip->setup_data_interface = denali_setup_data_interface;
1255
1256 ret = nand_scan_ident(mtd, denali->max_banks, NULL);
1257 if (ret)
1258 return ret;
1259
1260 if (ioread32(denali->reg + FEATURES) & FEATURES__DMA)
1261 denali->dma_avail = 1;
1262
1263 if (denali->dma_avail) {
Masahiro Yamadae83725e2018-07-19 10:13:23 +09001264 chip->buf_align = ARCH_DMA_MINALIGN;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001265 if (denali->caps & DENALI_CAP_DMA_64BIT)
1266 denali->setup_dma = denali_setup_dma64;
1267 else
1268 denali->setup_dma = denali_setup_dma32;
1269 } else {
1270 chip->buf_align = 4;
1271 }
1272
1273 chip->options |= NAND_USE_BOUNCE_BUFFER;
1274 chip->bbt_options |= NAND_BBT_USE_FLASH;
1275 chip->bbt_options |= NAND_BBT_NO_OOB;
1276 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001277
Scott Wood3ea94ed2015-06-26 19:03:26 -05001278 /* no subpage writes on denali */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001279 chip->options |= NAND_NO_SUBPAGE_WRITE;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001280
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001281 ret = denali_ecc_setup(mtd, chip, denali);
1282 if (ret) {
1283 dev_err(denali->dev, "Failed to setup ECC settings.\n");
1284 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001285 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001286
1287 dev_dbg(denali->dev,
1288 "chosen ECC settings: step=%d, strength=%d, bytes=%d\n",
1289 chip->ecc.size, chip->ecc.strength, chip->ecc.bytes);
1290
1291 iowrite32(FIELD_PREP(ECC_CORRECTION__ERASE_THRESHOLD, 1) |
1292 FIELD_PREP(ECC_CORRECTION__VALUE, chip->ecc.strength),
1293 denali->reg + ECC_CORRECTION);
1294 iowrite32(mtd->erasesize / mtd->writesize,
1295 denali->reg + PAGES_PER_BLOCK);
1296 iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0,
1297 denali->reg + DEVICE_WIDTH);
1298 iowrite32(chip->options & NAND_ROW_ADDR_3 ? 0 : TWO_ROW_ADDR_CYCLES__FLAG,
1299 denali->reg + TWO_ROW_ADDR_CYCLES);
1300 iowrite32(mtd->writesize, denali->reg + DEVICE_MAIN_AREA_SIZE);
1301 iowrite32(mtd->oobsize, denali->reg + DEVICE_SPARE_AREA_SIZE);
1302
1303 iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE);
1304 iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE);
1305 /* chip->ecc.steps is set by nand_scan_tail(); not available here */
1306 iowrite32(mtd->writesize / chip->ecc.size,
1307 denali->reg + CFG_NUM_DATA_BLOCKS);
1308
1309 mtd_set_ooblayout(mtd, &denali_ooblayout_ops);
1310
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001311 nand_oob.eccbytes = denali->nand.ecc.bytes;
1312 denali->nand.ecc.layout = &nand_oob;
Chin Liang See03534df2014-09-12 00:42:17 -05001313
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001314 if (chip->options & NAND_BUSWIDTH_16) {
1315 chip->read_buf = denali_read_buf16;
1316 chip->write_buf = denali_write_buf16;
1317 } else {
1318 chip->read_buf = denali_read_buf;
1319 chip->write_buf = denali_write_buf;
1320 }
1321 chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
1322 chip->ecc.read_page = denali_read_page;
1323 chip->ecc.read_page_raw = denali_read_page_raw;
1324 chip->ecc.write_page = denali_write_page;
1325 chip->ecc.write_page_raw = denali_write_page_raw;
1326 chip->ecc.read_oob = denali_read_oob;
1327 chip->ecc.write_oob = denali_write_oob;
1328 chip->erase = denali_erase;
Masahiro Yamada628ee1e2014-11-13 20:31:51 +09001329
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001330 ret = denali_multidev_fixup(denali);
1331 if (ret)
1332 return ret;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001333
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001334 /*
1335 * This buffer is DMA-mapped by denali_{read,write}_page_raw. Do not
1336 * use devm_kmalloc() because the memory allocated by devm_ does not
1337 * guarantee DMA-safe alignment.
1338 */
1339 denali->buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
1340 if (!denali->buf)
1341 return -ENOMEM;
1342
1343 ret = nand_scan_tail(mtd);
1344 if (ret)
1345 goto free_buf;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001346
Scott Wood52ab7ce2016-05-30 13:57:58 -05001347 ret = nand_register(0, mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001348 if (ret) {
1349 dev_err(denali->dev, "Failed to register MTD: %d\n", ret);
1350 goto free_buf;
1351 }
1352 return 0;
1353
1354free_buf:
1355 kfree(denali->buf);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001356
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001357 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001358}