blob: bbfa3b6112e8ba003963870db4d266ced663a908 [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>
Masahiro Yamada5beb1b32017-11-30 13:45:27 +09009#include <nand.h>
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090010#include <linux/bitfield.h>
11#include <linux/dma-direction.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090013#include <linux/io.h>
Masahiro Yamada5beb1b32017-11-30 13:45:27 +090014#include <linux/mtd/mtd.h>
15#include <linux/mtd/rawnand.h>
Chin Liang See03534df2014-09-12 00:42:17 -050016
17#include "denali.h"
18
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090019static dma_addr_t dma_map_single(void *dev, void *ptr, size_t size,
20 enum dma_data_direction dir)
21{
22 unsigned long addr = (unsigned long)ptr;
Chin Liang See03534df2014-09-12 00:42:17 -050023
Masahiro Yamada35333e92018-09-10 11:17:30 +090024 size = ALIGN(size, ARCH_DMA_MINALIGN);
25
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090026 if (dir == DMA_FROM_DEVICE)
27 invalidate_dcache_range(addr, addr + size);
28 else
29 flush_dcache_range(addr, addr + size);
Chin Liang See03534df2014-09-12 00:42:17 -050030
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090031 return addr;
32}
Chin Liang See03534df2014-09-12 00:42:17 -050033
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090034static void dma_unmap_single(void *dev, dma_addr_t addr, size_t size,
35 enum dma_data_direction dir)
Scott Wood52ab7ce2016-05-30 13:57:58 -050036{
Masahiro Yamada35333e92018-09-10 11:17:30 +090037 size = ALIGN(size, ARCH_DMA_MINALIGN);
38
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090039 if (dir != DMA_TO_DEVICE)
40 invalidate_dcache_range(addr, addr + size);
Scott Wood52ab7ce2016-05-30 13:57:58 -050041}
Chin Liang See03534df2014-09-12 00:42:17 -050042
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090043static int dma_mapping_error(void *dev, dma_addr_t addr)
Chin Liang See03534df2014-09-12 00:42:17 -050044{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090045 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -050046}
47
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090048#define DENALI_NAND_NAME "denali-nand"
Chin Liang See03534df2014-09-12 00:42:17 -050049
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090050/* for Indexed Addressing */
51#define DENALI_INDEXED_CTRL 0x00
52#define DENALI_INDEXED_DATA 0x10
Chin Liang See03534df2014-09-12 00:42:17 -050053
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090054#define DENALI_MAP00 (0 << 26) /* direct access to buffer */
55#define DENALI_MAP01 (1 << 26) /* read/write pages in PIO */
56#define DENALI_MAP10 (2 << 26) /* high-level control plane */
57#define DENALI_MAP11 (3 << 26) /* direct controller access */
Chin Liang See03534df2014-09-12 00:42:17 -050058
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090059/* MAP11 access cycle type */
60#define DENALI_MAP11_CMD ((DENALI_MAP11) | 0) /* command cycle */
61#define DENALI_MAP11_ADDR ((DENALI_MAP11) | 1) /* address cycle */
62#define DENALI_MAP11_DATA ((DENALI_MAP11) | 2) /* data cycle */
Chin Liang See03534df2014-09-12 00:42:17 -050063
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090064/* MAP10 commands */
65#define DENALI_ERASE 0x01
Chin Liang See03534df2014-09-12 00:42:17 -050066
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090067#define DENALI_BANK(denali) ((denali)->active_bank << 24)
Chin Liang See03534df2014-09-12 00:42:17 -050068
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090069#define DENALI_INVALID_BANK -1
70#define DENALI_NR_BANKS 4
Chin Liang See03534df2014-09-12 00:42:17 -050071
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090072static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -050073{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090074 return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand);
Chin Liang See03534df2014-09-12 00:42:17 -050075}
76
77/*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090078 * Direct Addressing - the slave address forms the control information (command
79 * type, bank, block, and page address). The slave data is the actual data to
80 * be transferred. This mode requires 28 bits of address region allocated.
Scott Wood3ea94ed2015-06-26 19:03:26 -050081 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090082static u32 denali_direct_read(struct denali_nand_info *denali, u32 addr)
Chin Liang See03534df2014-09-12 00:42:17 -050083{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090084 return ioread32(denali->host + addr);
Chin Liang See03534df2014-09-12 00:42:17 -050085}
86
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090087static void denali_direct_write(struct denali_nand_info *denali, u32 addr,
88 u32 data)
Chin Liang See03534df2014-09-12 00:42:17 -050089{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090090 iowrite32(data, denali->host + addr);
Chin Liang See03534df2014-09-12 00:42:17 -050091}
92
Scott Wood3ea94ed2015-06-26 19:03:26 -050093/*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090094 * Indexed Addressing - address translation module intervenes in passing the
95 * control information. This mode reduces the required address range. The
96 * control information and transferred data are latched by the registers in
97 * the translation module.
Scott Wood3ea94ed2015-06-26 19:03:26 -050098 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090099static u32 denali_indexed_read(struct denali_nand_info *denali, u32 addr)
Chin Liang See03534df2014-09-12 00:42:17 -0500100{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900101 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
102 return ioread32(denali->host + DENALI_INDEXED_DATA);
Chin Liang See03534df2014-09-12 00:42:17 -0500103}
104
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900105static void denali_indexed_write(struct denali_nand_info *denali, u32 addr,
106 u32 data)
Chin Liang See03534df2014-09-12 00:42:17 -0500107{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900108 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
109 iowrite32(data, denali->host + DENALI_INDEXED_DATA);
Chin Liang See03534df2014-09-12 00:42:17 -0500110}
111
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900112/*
113 * Use the configuration feature register to determine the maximum number of
114 * banks that the hardware supports.
115 */
116static void denali_detect_max_banks(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500117{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900118 uint32_t features = ioread32(denali->reg + FEATURES);
Chin Liang See03534df2014-09-12 00:42:17 -0500119
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900120 denali->max_banks = 1 << FIELD_GET(FEATURES__N_BANKS, features);
Chin Liang See03534df2014-09-12 00:42:17 -0500121
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900122 /* the encoding changed from rev 5.0 to 5.1 */
123 if (denali->revision < 0x0501)
124 denali->max_banks <<= 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500125}
126
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900127static void __maybe_unused denali_enable_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500128{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500129 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 iowrite32(U32_MAX, denali->reg + INTR_EN(i));
133 iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500134}
135
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900136static void __maybe_unused denali_disable_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500137{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900138 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 iowrite32(0, denali->reg + INTR_EN(i));
142 iowrite32(0, denali->reg + GLOBAL_INT_ENABLE);
143}
Chin Liang See03534df2014-09-12 00:42:17 -0500144
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900145static void denali_clear_irq(struct denali_nand_info *denali,
146 int bank, uint32_t irq_status)
147{
148 /* write one to clear bits */
149 iowrite32(irq_status, denali->reg + INTR_STATUS(bank));
150}
Chin Liang See03534df2014-09-12 00:42:17 -0500151
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900152static void denali_clear_irq_all(struct denali_nand_info *denali)
153{
154 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500155
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900156 for (i = 0; i < DENALI_NR_BANKS; i++)
157 denali_clear_irq(denali, i, U32_MAX);
158}
Chin Liang See03534df2014-09-12 00:42:17 -0500159
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900160static void __denali_check_irq(struct denali_nand_info *denali)
161{
162 uint32_t irq_status;
163 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500164
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900165 for (i = 0; i < DENALI_NR_BANKS; i++) {
166 irq_status = ioread32(denali->reg + INTR_STATUS(i));
167 denali_clear_irq(denali, i, irq_status);
Chin Liang See03534df2014-09-12 00:42:17 -0500168
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900169 if (i != denali->active_bank)
170 continue;
Chin Liang See03534df2014-09-12 00:42:17 -0500171
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900172 denali->irq_status |= irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500173 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900174}
Chin Liang See03534df2014-09-12 00:42:17 -0500175
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900176static void denali_reset_irq(struct denali_nand_info *denali)
177{
178 denali->irq_status = 0;
179 denali->irq_mask = 0;
180}
Chin Liang See03534df2014-09-12 00:42:17 -0500181
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900182static uint32_t denali_wait_for_irq(struct denali_nand_info *denali,
183 uint32_t irq_mask)
184{
185 unsigned long time_left = 1000000;
Chin Liang See03534df2014-09-12 00:42:17 -0500186
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900187 while (time_left) {
188 __denali_check_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500189
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900190 if (irq_mask & denali->irq_status)
191 return denali->irq_status;
192 udelay(1);
193 time_left--;
Chin Liang See03534df2014-09-12 00:42:17 -0500194 }
195
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900196 if (!time_left) {
197 dev_err(denali->dev, "timeout while waiting for irq 0x%x\n",
198 irq_mask);
199 return 0;
200 }
Chin Liang See03534df2014-09-12 00:42:17 -0500201
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900202 return denali->irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500203}
204
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900205static uint32_t denali_check_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500206{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900207 __denali_check_irq(denali);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500208
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900209 return denali->irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500210}
211
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900212static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500213{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900214 struct denali_nand_info *denali = mtd_to_denali(mtd);
215 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
216 int i;
217
218 for (i = 0; i < len; i++)
219 buf[i] = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500220}
221
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900222static void denali_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500223{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900224 struct denali_nand_info *denali = mtd_to_denali(mtd);
225 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
226 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500227
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900228 for (i = 0; i < len; i++)
229 denali->host_write(denali, addr, buf[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500230}
231
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900232static void denali_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500233{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900234 struct denali_nand_info *denali = mtd_to_denali(mtd);
235 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
236 uint16_t *buf16 = (uint16_t *)buf;
237 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500238
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900239 for (i = 0; i < len / 2; i++)
240 buf16[i] = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500241}
242
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900243static void denali_write_buf16(struct mtd_info *mtd, const uint8_t *buf,
244 int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500245{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900246 struct denali_nand_info *denali = mtd_to_denali(mtd);
247 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
248 const uint16_t *buf16 = (const uint16_t *)buf;
Chin Liang See03534df2014-09-12 00:42:17 -0500249 int i;
250
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900251 for (i = 0; i < len / 2; i++)
252 denali->host_write(denali, addr, buf16[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500253}
254
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900255static uint8_t denali_read_byte(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500256{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900257 uint8_t byte;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900258
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900259 denali_read_buf(mtd, &byte, 1);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900260
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900261 return byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500262}
263
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900264static void denali_write_byte(struct mtd_info *mtd, uint8_t byte)
Chin Liang See03534df2014-09-12 00:42:17 -0500265{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900266 denali_write_buf(mtd, &byte, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500267}
268
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900269static uint16_t denali_read_word(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500270{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900271 uint16_t word;
Chin Liang See03534df2014-09-12 00:42:17 -0500272
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900273 denali_read_buf16(mtd, (uint8_t *)&word, 2);
Chin Liang See03534df2014-09-12 00:42:17 -0500274
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900275 return word;
276}
Chin Liang See03534df2014-09-12 00:42:17 -0500277
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900278static void denali_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
279{
280 struct denali_nand_info *denali = mtd_to_denali(mtd);
281 uint32_t type;
Chin Liang See03534df2014-09-12 00:42:17 -0500282
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900283 if (ctrl & NAND_CLE)
284 type = DENALI_MAP11_CMD;
285 else if (ctrl & NAND_ALE)
286 type = DENALI_MAP11_ADDR;
287 else
288 return;
Chin Liang See03534df2014-09-12 00:42:17 -0500289
Scott Wood3ea94ed2015-06-26 19:03:26 -0500290 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900291 * Some commands are followed by chip->dev_ready or chip->waitfunc.
292 * irq_status must be cleared here to catch the R/B# interrupt later.
Chin Liang See03534df2014-09-12 00:42:17 -0500293 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900294 if (ctrl & NAND_CTRL_CHANGE)
295 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500296
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900297 denali->host_write(denali, DENALI_BANK(denali) | type, dat);
Chin Liang See03534df2014-09-12 00:42:17 -0500298}
299
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900300static int denali_dev_ready(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500301{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900302 struct denali_nand_info *denali = mtd_to_denali(mtd);
303
304 return !!(denali_check_irq(denali) & INTR__INT_ACT);
Chin Liang See03534df2014-09-12 00:42:17 -0500305}
306
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900307static int denali_check_erased_page(struct mtd_info *mtd,
308 struct nand_chip *chip, uint8_t *buf,
309 unsigned long uncor_ecc_flags,
310 unsigned int max_bitflips)
Chin Liang See03534df2014-09-12 00:42:17 -0500311{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900312 uint8_t *ecc_code = chip->buffers->ecccode;
313 int ecc_steps = chip->ecc.steps;
314 int ecc_size = chip->ecc.size;
315 int ecc_bytes = chip->ecc.bytes;
316 int i, ret, stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500317
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900318 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
319 chip->ecc.total);
320 if (ret)
321 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500322
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900323 for (i = 0; i < ecc_steps; i++) {
324 if (!(uncor_ecc_flags & BIT(i)))
325 continue;
Chin Liang See03534df2014-09-12 00:42:17 -0500326
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900327 stat = nand_check_erased_ecc_chunk(buf, ecc_size,
328 ecc_code, ecc_bytes,
329 NULL, 0,
330 chip->ecc.strength);
331 if (stat < 0) {
332 mtd->ecc_stats.failed++;
333 } else {
334 mtd->ecc_stats.corrected += stat;
335 max_bitflips = max_t(unsigned int, max_bitflips, stat);
336 }
Chin Liang See03534df2014-09-12 00:42:17 -0500337
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900338 buf += ecc_size;
339 ecc_code += ecc_bytes;
340 }
341
342 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500343}
344
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900345static int denali_hw_ecc_fixup(struct mtd_info *mtd,
346 struct denali_nand_info *denali,
347 unsigned long *uncor_ecc_flags)
Chin Liang See03534df2014-09-12 00:42:17 -0500348{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900349 struct nand_chip *chip = mtd_to_nand(mtd);
350 int bank = denali->active_bank;
351 uint32_t ecc_cor;
352 unsigned int max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500353
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900354 ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank));
355 ecc_cor >>= ECC_COR_INFO__SHIFT(bank);
356
357 if (ecc_cor & ECC_COR_INFO__UNCOR_ERR) {
358 /*
359 * This flag is set when uncorrectable error occurs at least in
360 * one ECC sector. We can not know "how many sectors", or
361 * "which sector(s)". We need erase-page check for all sectors.
362 */
363 *uncor_ecc_flags = GENMASK(chip->ecc.steps - 1, 0);
364 return 0;
365 }
Chin Liang See03534df2014-09-12 00:42:17 -0500366
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900367 max_bitflips = FIELD_GET(ECC_COR_INFO__MAX_ERRORS, ecc_cor);
368
369 /*
370 * The register holds the maximum of per-sector corrected bitflips.
371 * This is suitable for the return value of the ->read_page() callback.
372 * Unfortunately, we can not know the total number of corrected bits in
373 * the page. Increase the stats by max_bitflips. (compromised solution)
374 */
375 mtd->ecc_stats.corrected += max_bitflips;
376
377 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500378}
379
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900380static int denali_sw_ecc_fixup(struct mtd_info *mtd,
381 struct denali_nand_info *denali,
382 unsigned long *uncor_ecc_flags, uint8_t *buf)
Chin Liang See03534df2014-09-12 00:42:17 -0500383{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900384 unsigned int ecc_size = denali->nand.ecc.size;
385 unsigned int bitflips = 0;
386 unsigned int max_bitflips = 0;
387 uint32_t err_addr, err_cor_info;
388 unsigned int err_byte, err_sector, err_device;
389 uint8_t err_cor_value;
390 unsigned int prev_sector = 0;
391 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500392
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900393 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500394
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900395 do {
396 err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS);
397 err_sector = FIELD_GET(ECC_ERROR_ADDRESS__SECTOR, err_addr);
398 err_byte = FIELD_GET(ECC_ERROR_ADDRESS__OFFSET, err_addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500399
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900400 err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO);
401 err_cor_value = FIELD_GET(ERR_CORRECTION_INFO__BYTE,
402 err_cor_info);
403 err_device = FIELD_GET(ERR_CORRECTION_INFO__DEVICE,
404 err_cor_info);
Chin Liang See03534df2014-09-12 00:42:17 -0500405
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900406 /* reset the bitflip counter when crossing ECC sector */
407 if (err_sector != prev_sector)
408 bitflips = 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500409
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900410 if (err_cor_info & ERR_CORRECTION_INFO__UNCOR) {
411 /*
412 * Check later if this is a real ECC error, or
413 * an erased sector.
414 */
415 *uncor_ecc_flags |= BIT(err_sector);
416 } else if (err_byte < ecc_size) {
417 /*
418 * If err_byte is larger than ecc_size, means error
419 * happened in OOB, so we ignore it. It's no need for
420 * us to correct it err_device is represented the NAND
421 * error bits are happened in if there are more than
422 * one NAND connected.
423 */
424 int offset;
425 unsigned int flips_in_byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500426
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900427 offset = (err_sector * ecc_size + err_byte) *
428 denali->devs_per_cs + err_device;
Chin Liang See03534df2014-09-12 00:42:17 -0500429
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900430 /* correct the ECC error */
431 flips_in_byte = hweight8(buf[offset] ^ err_cor_value);
432 buf[offset] ^= err_cor_value;
433 mtd->ecc_stats.corrected += flips_in_byte;
434 bitflips += flips_in_byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500435
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900436 max_bitflips = max(max_bitflips, bitflips);
437 }
Chin Liang See03534df2014-09-12 00:42:17 -0500438
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900439 prev_sector = err_sector;
440 } while (!(err_cor_info & ERR_CORRECTION_INFO__LAST_ERR));
Chin Liang See03534df2014-09-12 00:42:17 -0500441
Scott Wood3ea94ed2015-06-26 19:03:26 -0500442 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900443 * Once handle all ECC errors, controller will trigger an
444 * ECC_TRANSACTION_DONE interrupt.
Scott Wood3ea94ed2015-06-26 19:03:26 -0500445 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900446 irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE);
447 if (!(irq_status & INTR__ECC_TRANSACTION_DONE))
448 return -EIO;
Chin Liang See03534df2014-09-12 00:42:17 -0500449
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900450 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500451}
452
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900453static void denali_setup_dma64(struct denali_nand_info *denali,
454 dma_addr_t dma_addr, int page, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500455{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900456 uint32_t mode;
457 const int page_count = 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500458
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900459 mode = DENALI_MAP10 | DENALI_BANK(denali) | page;
Chin Liang See03534df2014-09-12 00:42:17 -0500460
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900461 /* DMA is a three step process */
Chin Liang See03534df2014-09-12 00:42:17 -0500462
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900463 /*
464 * 1. setup transfer type, interrupt when complete,
465 * burst len = 64 bytes, the number of pages
466 */
467 denali->host_write(denali, mode,
468 0x01002000 | (64 << 16) | (write << 8) | page_count);
Chin Liang See03534df2014-09-12 00:42:17 -0500469
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900470 /* 2. set memory low address */
471 denali->host_write(denali, mode, lower_32_bits(dma_addr));
Chin Liang See03534df2014-09-12 00:42:17 -0500472
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900473 /* 3. set memory high address */
474 denali->host_write(denali, mode, upper_32_bits(dma_addr));
Chin Liang See03534df2014-09-12 00:42:17 -0500475}
476
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900477static void denali_setup_dma32(struct denali_nand_info *denali,
478 dma_addr_t dma_addr, int page, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500479{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900480 uint32_t mode;
481 const int page_count = 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500482
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900483 mode = DENALI_MAP10 | DENALI_BANK(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500484
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900485 /* DMA is a four step process */
Chin Liang See03534df2014-09-12 00:42:17 -0500486
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900487 /* 1. setup transfer type and # of pages */
488 denali->host_write(denali, mode | page,
489 0x2000 | (write << 8) | page_count);
Chin Liang See03534df2014-09-12 00:42:17 -0500490
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900491 /* 2. set memory high address bits 23:8 */
492 denali->host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200);
Chin Liang See03534df2014-09-12 00:42:17 -0500493
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900494 /* 3. set memory low address bits 23:8 */
495 denali->host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300);
Chin Liang See03534df2014-09-12 00:42:17 -0500496
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900497 /* 4. interrupt when complete, burst len = 64 bytes */
498 denali->host_write(denali, mode | 0x14000, 0x2400);
Chin Liang See03534df2014-09-12 00:42:17 -0500499}
500
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900501static int denali_pio_read(struct denali_nand_info *denali, void *buf,
502 size_t size, int page, int raw)
Chin Liang See03534df2014-09-12 00:42:17 -0500503{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900504 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
505 uint32_t *buf32 = (uint32_t *)buf;
506 uint32_t irq_status, ecc_err_mask;
507 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500508
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900509 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
510 ecc_err_mask = INTR__ECC_UNCOR_ERR;
511 else
512 ecc_err_mask = INTR__ECC_ERR;
Chin Liang See03534df2014-09-12 00:42:17 -0500513
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900514 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500515
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900516 for (i = 0; i < size / 4; i++)
517 *buf32++ = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500518
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900519 irq_status = denali_wait_for_irq(denali, INTR__PAGE_XFER_INC);
520 if (!(irq_status & INTR__PAGE_XFER_INC))
521 return -EIO;
Chin Liang See03534df2014-09-12 00:42:17 -0500522
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900523 if (irq_status & INTR__ERASED_PAGE)
524 memset(buf, 0xff, size);
525
526 return irq_status & ecc_err_mask ? -EBADMSG : 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500527}
528
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900529static int denali_pio_write(struct denali_nand_info *denali,
530 const void *buf, size_t size, int page, int raw)
Chin Liang See03534df2014-09-12 00:42:17 -0500531{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900532 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
533 const uint32_t *buf32 = (uint32_t *)buf;
534 uint32_t irq_status;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500535 int i;
536
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900537 denali_reset_irq(denali);
538
539 for (i = 0; i < size / 4; i++)
540 denali->host_write(denali, addr, *buf32++);
541
542 irq_status = denali_wait_for_irq(denali,
543 INTR__PROGRAM_COMP | INTR__PROGRAM_FAIL);
544 if (!(irq_status & INTR__PROGRAM_COMP))
545 return -EIO;
546
547 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500548}
549
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900550static int denali_pio_xfer(struct denali_nand_info *denali, void *buf,
551 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500552{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900553 if (write)
554 return denali_pio_write(denali, buf, size, page, raw);
555 else
556 return denali_pio_read(denali, buf, size, page, raw);
Chin Liang See03534df2014-09-12 00:42:17 -0500557}
558
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900559static int denali_dma_xfer(struct denali_nand_info *denali, void *buf,
560 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500561{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900562 dma_addr_t dma_addr;
563 uint32_t irq_mask, irq_status, ecc_err_mask;
564 enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
565 int ret = 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500566
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900567 dma_addr = dma_map_single(denali->dev, buf, size, dir);
568 if (dma_mapping_error(denali->dev, dma_addr)) {
569 dev_dbg(denali->dev, "Failed to DMA-map buffer. Trying PIO.\n");
570 return denali_pio_xfer(denali, buf, size, page, raw, write);
571 }
Chin Liang See03534df2014-09-12 00:42:17 -0500572
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900573 if (write) {
574 /*
575 * INTR__PROGRAM_COMP is never asserted for the DMA transfer.
576 * We can use INTR__DMA_CMD_COMP instead. This flag is asserted
577 * when the page program is completed.
578 */
579 irq_mask = INTR__DMA_CMD_COMP | INTR__PROGRAM_FAIL;
580 ecc_err_mask = 0;
581 } else if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) {
582 irq_mask = INTR__DMA_CMD_COMP;
583 ecc_err_mask = INTR__ECC_UNCOR_ERR;
584 } else {
585 irq_mask = INTR__DMA_CMD_COMP;
586 ecc_err_mask = INTR__ECC_ERR;
587 }
Chin Liang See03534df2014-09-12 00:42:17 -0500588
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900589 iowrite32(DMA_ENABLE__FLAG, denali->reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500590
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900591 denali_reset_irq(denali);
592 denali->setup_dma(denali, dma_addr, page, write);
Chin Liang See03534df2014-09-12 00:42:17 -0500593
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900594 irq_status = denali_wait_for_irq(denali, irq_mask);
595 if (!(irq_status & INTR__DMA_CMD_COMP))
596 ret = -EIO;
597 else if (irq_status & ecc_err_mask)
598 ret = -EBADMSG;
Chin Liang See03534df2014-09-12 00:42:17 -0500599
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900600 iowrite32(0, denali->reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500601
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900602 dma_unmap_single(denali->dev, dma_addr, size, dir);
Chin Liang See03534df2014-09-12 00:42:17 -0500603
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900604 if (irq_status & INTR__ERASED_PAGE)
605 memset(buf, 0xff, size);
Chin Liang See03534df2014-09-12 00:42:17 -0500606
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900607 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500608}
609
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900610static int denali_data_xfer(struct denali_nand_info *denali, void *buf,
611 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500612{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900613 iowrite32(raw ? 0 : ECC_ENABLE__FLAG, denali->reg + ECC_ENABLE);
614 iowrite32(raw ? TRANSFER_SPARE_REG__FLAG : 0,
615 denali->reg + TRANSFER_SPARE_REG);
Chin Liang See03534df2014-09-12 00:42:17 -0500616
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900617 if (denali->dma_avail)
618 return denali_dma_xfer(denali, buf, size, page, raw, write);
619 else
620 return denali_pio_xfer(denali, buf, size, page, raw, write);
621}
Chin Liang See03534df2014-09-12 00:42:17 -0500622
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900623static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip,
624 int page, int write)
625{
626 struct denali_nand_info *denali = mtd_to_denali(mtd);
627 unsigned int start_cmd = write ? NAND_CMD_SEQIN : NAND_CMD_READ0;
628 unsigned int rnd_cmd = write ? NAND_CMD_RNDIN : NAND_CMD_RNDOUT;
629 int writesize = mtd->writesize;
630 int oobsize = mtd->oobsize;
631 uint8_t *bufpoi = chip->oob_poi;
632 int ecc_steps = chip->ecc.steps;
633 int ecc_size = chip->ecc.size;
634 int ecc_bytes = chip->ecc.bytes;
635 int oob_skip = denali->oob_skip_bytes;
636 size_t size = writesize + oobsize;
637 int i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500638
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900639 /* BBM at the beginning of the OOB area */
640 chip->cmdfunc(mtd, start_cmd, writesize, page);
641 if (write)
642 chip->write_buf(mtd, bufpoi, oob_skip);
643 else
644 chip->read_buf(mtd, bufpoi, oob_skip);
645 bufpoi += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500646
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900647 /* OOB ECC */
648 for (i = 0; i < ecc_steps; i++) {
649 pos = ecc_size + i * (ecc_size + ecc_bytes);
650 len = ecc_bytes;
Chin Liang See03534df2014-09-12 00:42:17 -0500651
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900652 if (pos >= writesize)
653 pos += oob_skip;
654 else if (pos + len > writesize)
655 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500656
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900657 chip->cmdfunc(mtd, rnd_cmd, pos, -1);
658 if (write)
659 chip->write_buf(mtd, bufpoi, len);
660 else
661 chip->read_buf(mtd, bufpoi, len);
662 bufpoi += len;
663 if (len < ecc_bytes) {
664 len = ecc_bytes - len;
665 chip->cmdfunc(mtd, rnd_cmd, writesize + oob_skip, -1);
666 if (write)
667 chip->write_buf(mtd, bufpoi, len);
668 else
669 chip->read_buf(mtd, bufpoi, len);
670 bufpoi += len;
671 }
672 }
Chin Liang See03534df2014-09-12 00:42:17 -0500673
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900674 /* OOB free */
675 len = oobsize - (bufpoi - chip->oob_poi);
676 chip->cmdfunc(mtd, rnd_cmd, size - len, -1);
677 if (write)
678 chip->write_buf(mtd, bufpoi, len);
679 else
680 chip->read_buf(mtd, bufpoi, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500681}
682
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900683static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
684 uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500685{
686 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900687 int writesize = mtd->writesize;
688 int oobsize = mtd->oobsize;
689 int ecc_steps = chip->ecc.steps;
690 int ecc_size = chip->ecc.size;
691 int ecc_bytes = chip->ecc.bytes;
692 void *tmp_buf = denali->buf;
693 int oob_skip = denali->oob_skip_bytes;
694 size_t size = writesize + oobsize;
695 int ret, i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500696
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900697 ret = denali_data_xfer(denali, tmp_buf, size, page, 1, 0);
698 if (ret)
699 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500700
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900701 /* Arrange the buffer for syndrome payload/ecc layout */
702 if (buf) {
703 for (i = 0; i < ecc_steps; i++) {
704 pos = i * (ecc_size + ecc_bytes);
705 len = ecc_size;
Chin Liang See03534df2014-09-12 00:42:17 -0500706
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900707 if (pos >= writesize)
708 pos += oob_skip;
709 else if (pos + len > writesize)
710 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500711
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900712 memcpy(buf, tmp_buf + pos, len);
713 buf += len;
714 if (len < ecc_size) {
715 len = ecc_size - len;
716 memcpy(buf, tmp_buf + writesize + oob_skip,
717 len);
718 buf += len;
719 }
720 }
721 }
Chin Liang See03534df2014-09-12 00:42:17 -0500722
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900723 if (oob_required) {
724 uint8_t *oob = chip->oob_poi;
Chin Liang See03534df2014-09-12 00:42:17 -0500725
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900726 /* BBM at the beginning of the OOB area */
727 memcpy(oob, tmp_buf + writesize, oob_skip);
728 oob += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500729
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900730 /* OOB ECC */
731 for (i = 0; i < ecc_steps; i++) {
732 pos = ecc_size + i * (ecc_size + ecc_bytes);
733 len = ecc_bytes;
734
735 if (pos >= writesize)
736 pos += oob_skip;
737 else if (pos + len > writesize)
738 len = writesize - pos;
739
740 memcpy(oob, tmp_buf + pos, len);
741 oob += len;
742 if (len < ecc_bytes) {
743 len = ecc_bytes - len;
744 memcpy(oob, tmp_buf + writesize + oob_skip,
745 len);
746 oob += len;
747 }
748 }
749
750 /* OOB free */
751 len = oobsize - (oob - chip->oob_poi);
752 memcpy(oob, tmp_buf + size - len, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500753 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900754
Chin Liang See03534df2014-09-12 00:42:17 -0500755 return 0;
756}
757
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900758static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
759 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500760{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900761 denali_oob_xfer(mtd, chip, page, 0);
Chin Liang See03534df2014-09-12 00:42:17 -0500762
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900763 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500764}
765
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900766static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
767 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500768{
769 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900770 int status;
Chin Liang See03534df2014-09-12 00:42:17 -0500771
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900772 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500773
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900774 denali_oob_xfer(mtd, chip, page, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500775
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900776 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
777 status = chip->waitfunc(mtd, chip);
Chin Liang See03534df2014-09-12 00:42:17 -0500778
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900779 return status & NAND_STATUS_FAIL ? -EIO : 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500780}
781
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900782static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
783 uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500784{
785 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900786 unsigned long uncor_ecc_flags = 0;
787 int stat = 0;
788 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500789
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900790 ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0);
791 if (ret && ret != -EBADMSG)
792 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500793
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900794 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
795 stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags);
796 else if (ret == -EBADMSG)
797 stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf);
Chin Liang See03534df2014-09-12 00:42:17 -0500798
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900799 if (stat < 0)
800 return stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500801
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900802 if (uncor_ecc_flags) {
803 ret = denali_read_oob(mtd, chip, page);
804 if (ret)
805 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500806
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900807 stat = denali_check_erased_page(mtd, chip, buf,
808 uncor_ecc_flags, stat);
Chin Liang See03534df2014-09-12 00:42:17 -0500809 }
810
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900811 return stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500812}
813
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900814static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
815 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500816{
817 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900818 int writesize = mtd->writesize;
819 int oobsize = mtd->oobsize;
820 int ecc_steps = chip->ecc.steps;
821 int ecc_size = chip->ecc.size;
822 int ecc_bytes = chip->ecc.bytes;
823 void *tmp_buf = denali->buf;
824 int oob_skip = denali->oob_skip_bytes;
825 size_t size = writesize + oobsize;
826 int i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500827
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900828 /*
829 * Fill the buffer with 0xff first except the full page transfer.
830 * This simplifies the logic.
831 */
832 if (!buf || !oob_required)
833 memset(tmp_buf, 0xff, size);
834
835 /* Arrange the buffer for syndrome payload/ecc layout */
836 if (buf) {
837 for (i = 0; i < ecc_steps; i++) {
838 pos = i * (ecc_size + ecc_bytes);
839 len = ecc_size;
840
841 if (pos >= writesize)
842 pos += oob_skip;
843 else if (pos + len > writesize)
844 len = writesize - pos;
845
846 memcpy(tmp_buf + pos, buf, len);
847 buf += len;
848 if (len < ecc_size) {
849 len = ecc_size - len;
850 memcpy(tmp_buf + writesize + oob_skip, buf,
851 len);
852 buf += len;
853 }
854 }
Chin Liang See03534df2014-09-12 00:42:17 -0500855 }
856
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900857 if (oob_required) {
858 const uint8_t *oob = chip->oob_poi;
Chin Liang See03534df2014-09-12 00:42:17 -0500859
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900860 /* BBM at the beginning of the OOB area */
861 memcpy(tmp_buf + writesize, oob, oob_skip);
862 oob += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500863
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900864 /* OOB ECC */
865 for (i = 0; i < ecc_steps; i++) {
866 pos = ecc_size + i * (ecc_size + ecc_bytes);
867 len = ecc_bytes;
Chin Liang See03534df2014-09-12 00:42:17 -0500868
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900869 if (pos >= writesize)
870 pos += oob_skip;
871 else if (pos + len > writesize)
872 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500873
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900874 memcpy(tmp_buf + pos, oob, len);
875 oob += len;
876 if (len < ecc_bytes) {
877 len = ecc_bytes - len;
878 memcpy(tmp_buf + writesize + oob_skip, oob,
879 len);
880 oob += len;
881 }
Chin Liang See03534df2014-09-12 00:42:17 -0500882 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900883
884 /* OOB free */
885 len = oobsize - (oob - chip->oob_poi);
886 memcpy(tmp_buf + size - len, oob, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500887 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900888
889 return denali_data_xfer(denali, tmp_buf, size, page, 1, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500890}
891
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900892static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
893 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500894{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900895 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chin Liang See03534df2014-09-12 00:42:17 -0500896
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900897 return denali_data_xfer(denali, (void *)buf, mtd->writesize,
898 page, 0, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500899}
900
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900901static void denali_select_chip(struct mtd_info *mtd, int chip)
Chin Liang See03534df2014-09-12 00:42:17 -0500902{
903 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chin Liang See03534df2014-09-12 00:42:17 -0500904
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900905 denali->active_bank = chip;
Chin Liang See03534df2014-09-12 00:42:17 -0500906}
907
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900908static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
Chin Liang See03534df2014-09-12 00:42:17 -0500909{
910 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900911 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500912
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900913 /* R/B# pin transitioned from low to high? */
914 irq_status = denali_wait_for_irq(denali, INTR__INT_ACT);
Chin Liang See03534df2014-09-12 00:42:17 -0500915
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900916 return irq_status & INTR__INT_ACT ? 0 : NAND_STATUS_FAIL;
Chin Liang See03534df2014-09-12 00:42:17 -0500917}
918
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900919static int denali_erase(struct mtd_info *mtd, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500920{
921 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900922 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500923
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900924 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500925
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900926 denali->host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page,
927 DENALI_ERASE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500928
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900929 /* wait for erase to complete or failure to occur */
930 irq_status = denali_wait_for_irq(denali,
931 INTR__ERASE_COMP | INTR__ERASE_FAIL);
Chin Liang See03534df2014-09-12 00:42:17 -0500932
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900933 return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL;
Chin Liang See03534df2014-09-12 00:42:17 -0500934}
935
Masahiro Yamada59a1f3e2017-11-29 19:18:18 +0900936static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900937 const struct nand_data_interface *conf)
Chin Liang See03534df2014-09-12 00:42:17 -0500938{
939 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900940 const struct nand_sdr_timings *timings;
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900941 unsigned long t_x, mult_x;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900942 int acc_clks, re_2_we, re_2_re, we_2_re, addr_2_data;
943 int rdwr_en_lo, rdwr_en_hi, rdwr_en_lo_hi, cs_setup;
944 int addr_2_data_mask;
945 uint32_t tmp;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500946
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900947 timings = nand_get_sdr_timings(conf);
948 if (IS_ERR(timings))
949 return PTR_ERR(timings);
Chin Liang See03534df2014-09-12 00:42:17 -0500950
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900951 /* clk_x period in picoseconds */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900952 t_x = DIV_ROUND_DOWN_ULL(1000000000000ULL, denali->clk_x_rate);
953 if (!t_x)
954 return -EINVAL;
955
956 /*
957 * The bus interface clock, clk_x, is phase aligned with the core clock.
958 * The clk_x is an integral multiple N of the core clk. The value N is
959 * configured at IP delivery time, and its available value is 4, 5, 6.
960 */
961 mult_x = DIV_ROUND_CLOSEST_ULL(denali->clk_x_rate, denali->clk_rate);
962 if (mult_x < 4 || mult_x > 6)
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900963 return -EINVAL;
Chin Liang See03534df2014-09-12 00:42:17 -0500964
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900965 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
966 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500967
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900968 /* tREA -> ACC_CLKS */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900969 acc_clks = DIV_ROUND_UP(timings->tREA_max, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900970 acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
971
972 tmp = ioread32(denali->reg + ACC_CLKS);
973 tmp &= ~ACC_CLKS__VALUE;
974 tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
975 iowrite32(tmp, denali->reg + ACC_CLKS);
976
977 /* tRWH -> RE_2_WE */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900978 re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900979 re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE);
Chin Liang See03534df2014-09-12 00:42:17 -0500980
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900981 tmp = ioread32(denali->reg + RE_2_WE);
982 tmp &= ~RE_2_WE__VALUE;
983 tmp |= FIELD_PREP(RE_2_WE__VALUE, re_2_we);
984 iowrite32(tmp, denali->reg + RE_2_WE);
985
986 /* tRHZ -> RE_2_RE */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +0900987 re_2_re = DIV_ROUND_UP(timings->tRHZ_max, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900988 re_2_re = min_t(int, re_2_re, RE_2_RE__VALUE);
989
990 tmp = ioread32(denali->reg + RE_2_RE);
991 tmp &= ~RE_2_RE__VALUE;
992 tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re);
993 iowrite32(tmp, denali->reg + RE_2_RE);
994
995 /*
996 * tCCS, tWHR -> WE_2_RE
997 *
998 * With WE_2_RE properly set, the Denali controller automatically takes
999 * care of the delay; the driver need not set NAND_WAIT_TCCS.
1000 */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001001 we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min), t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001002 we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE);
1003
1004 tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
1005 tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE;
1006 tmp |= FIELD_PREP(TWHR2_AND_WE_2_RE__WE_2_RE, we_2_re);
1007 iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE);
1008
1009 /* tADL -> ADDR_2_DATA */
1010
1011 /* for older versions, ADDR_2_DATA is only 6 bit wide */
1012 addr_2_data_mask = TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1013 if (denali->revision < 0x0501)
1014 addr_2_data_mask >>= 1;
1015
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001016 addr_2_data = DIV_ROUND_UP(timings->tADL_min, t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001017 addr_2_data = min_t(int, addr_2_data, addr_2_data_mask);
1018
1019 tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA);
1020 tmp &= ~TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1021 tmp |= FIELD_PREP(TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA, addr_2_data);
1022 iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA);
1023
1024 /* tREH, tWH -> RDWR_EN_HI_CNT */
1025 rdwr_en_hi = DIV_ROUND_UP(max(timings->tREH_min, timings->tWH_min),
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001026 t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001027 rdwr_en_hi = min_t(int, rdwr_en_hi, RDWR_EN_HI_CNT__VALUE);
1028
1029 tmp = ioread32(denali->reg + RDWR_EN_HI_CNT);
1030 tmp &= ~RDWR_EN_HI_CNT__VALUE;
1031 tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi);
1032 iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT);
1033
1034 /* tRP, tWP -> RDWR_EN_LO_CNT */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001035 rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min), t_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001036 rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min),
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001037 t_x);
1038 rdwr_en_lo_hi = max_t(int, rdwr_en_lo_hi, mult_x);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001039 rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
1040 rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
1041
1042 tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
1043 tmp &= ~RDWR_EN_LO_CNT__VALUE;
1044 tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo);
1045 iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT);
1046
1047 /* tCS, tCEA -> CS_SETUP_CNT */
Masahiro Yamada2d1fbc82018-12-19 20:03:18 +09001048 cs_setup = max3((int)DIV_ROUND_UP(timings->tCS_min, t_x) - rdwr_en_lo,
1049 (int)DIV_ROUND_UP(timings->tCEA_max, t_x) - acc_clks,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001050 0);
1051 cs_setup = min_t(int, cs_setup, CS_SETUP_CNT__VALUE);
1052
1053 tmp = ioread32(denali->reg + CS_SETUP_CNT);
1054 tmp &= ~CS_SETUP_CNT__VALUE;
1055 tmp |= FIELD_PREP(CS_SETUP_CNT__VALUE, cs_setup);
1056 iowrite32(tmp, denali->reg + CS_SETUP_CNT);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001057
1058 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -05001059}
1060
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001061static void denali_reset_banks(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001062{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001063 u32 irq_status;
1064 int i;
Chin Liang See03534df2014-09-12 00:42:17 -05001065
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001066 for (i = 0; i < denali->max_banks; i++) {
1067 denali->active_bank = i;
1068
1069 denali_reset_irq(denali);
1070
1071 iowrite32(DEVICE_RESET__BANK(i),
1072 denali->reg + DEVICE_RESET);
1073
1074 irq_status = denali_wait_for_irq(denali,
1075 INTR__RST_COMP | INTR__INT_ACT | INTR__TIME_OUT);
1076 if (!(irq_status & INTR__INT_ACT))
1077 break;
Chin Liang See03534df2014-09-12 00:42:17 -05001078 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001079
1080 dev_dbg(denali->dev, "%d chips connected\n", i);
1081 denali->max_banks = i;
Chin Liang See03534df2014-09-12 00:42:17 -05001082}
Chin Liang See03534df2014-09-12 00:42:17 -05001083
Chin Liang See03534df2014-09-12 00:42:17 -05001084static void denali_hw_init(struct denali_nand_info *denali)
1085{
1086 /*
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001087 * The REVISION register may not be reliable. Platforms are allowed to
1088 * override it.
1089 */
1090 if (!denali->revision)
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001091 denali->revision = swab16(ioread32(denali->reg + REVISION));
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001092
1093 /*
Chin Liang See03534df2014-09-12 00:42:17 -05001094 * tell driver how many bit controller will skip before writing
1095 * ECC code in OOB. This is normally used for bad block marker
1096 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001097 denali->oob_skip_bytes = CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES;
1098 iowrite32(denali->oob_skip_bytes, denali->reg + SPARE_AREA_SKIP_BYTES);
1099 denali_detect_max_banks(denali);
1100 iowrite32(0x0F, denali->reg + RB_PIN_ENABLED);
1101 iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE);
Chin Liang See03534df2014-09-12 00:42:17 -05001102
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001103 iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER);
Chin Liang See03534df2014-09-12 00:42:17 -05001104}
1105
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001106int denali_calc_ecc_bytes(int step_size, int strength)
1107{
1108 /* BCH code. Denali requires ecc.bytes to be multiple of 2 */
1109 return DIV_ROUND_UP(strength * fls(step_size * 8), 16) * 2;
1110}
1111EXPORT_SYMBOL(denali_calc_ecc_bytes);
Chin Liang See03534df2014-09-12 00:42:17 -05001112
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001113static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip,
1114 struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001115{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001116 int oobavail = mtd->oobsize - denali->oob_skip_bytes;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001117 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001118
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001119 /*
1120 * If .size and .strength are already set (usually by DT),
1121 * check if they are supported by this controller.
1122 */
1123 if (chip->ecc.size && chip->ecc.strength)
1124 return nand_check_ecc_caps(chip, denali->ecc_caps, oobavail);
1125
1126 /*
1127 * We want .size and .strength closest to the chip's requirement
1128 * unless NAND_ECC_MAXIMIZE is requested.
1129 */
1130 if (!(chip->ecc.options & NAND_ECC_MAXIMIZE)) {
1131 ret = nand_match_ecc_req(chip, denali->ecc_caps, oobavail);
1132 if (!ret)
1133 return 0;
1134 }
1135
1136 /* Max ECC strength is the last thing we can do */
1137 return nand_maximize_ecc(chip, denali->ecc_caps, oobavail);
1138}
1139
1140static struct nand_ecclayout nand_oob;
1141
1142static int denali_ooblayout_ecc(struct mtd_info *mtd, int section,
1143 struct mtd_oob_region *oobregion)
1144{
1145 struct denali_nand_info *denali = mtd_to_denali(mtd);
1146 struct nand_chip *chip = mtd_to_nand(mtd);
1147
1148 if (section)
1149 return -ERANGE;
1150
1151 oobregion->offset = denali->oob_skip_bytes;
1152 oobregion->length = chip->ecc.total;
1153
1154 return 0;
1155}
1156
1157static int denali_ooblayout_free(struct mtd_info *mtd, int section,
1158 struct mtd_oob_region *oobregion)
1159{
1160 struct denali_nand_info *denali = mtd_to_denali(mtd);
1161 struct nand_chip *chip = mtd_to_nand(mtd);
1162
1163 if (section)
1164 return -ERANGE;
1165
1166 oobregion->offset = chip->ecc.total + denali->oob_skip_bytes;
1167 oobregion->length = mtd->oobsize - oobregion->offset;
1168
1169 return 0;
1170}
Chin Liang See03534df2014-09-12 00:42:17 -05001171
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001172static const struct mtd_ooblayout_ops denali_ooblayout_ops = {
1173 .ecc = denali_ooblayout_ecc,
1174 .free = denali_ooblayout_free,
1175};
Chin Liang See03534df2014-09-12 00:42:17 -05001176
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001177static int denali_multidev_fixup(struct denali_nand_info *denali)
1178{
1179 struct nand_chip *chip = &denali->nand;
1180 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001181
1182 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001183 * Support for multi device:
1184 * When the IP configuration is x16 capable and two x8 chips are
1185 * connected in parallel, DEVICES_CONNECTED should be set to 2.
1186 * In this case, the core framework knows nothing about this fact,
1187 * so we should tell it the _logical_ pagesize and anything necessary.
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001188 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001189 denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED);
Chin Liang See03534df2014-09-12 00:42:17 -05001190
Chin Liang See03534df2014-09-12 00:42:17 -05001191 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001192 * On some SoCs, DEVICES_CONNECTED is not auto-detected.
1193 * For those, DEVICES_CONNECTED is left to 0. Set 1 if it is the case.
Chin Liang See03534df2014-09-12 00:42:17 -05001194 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001195 if (denali->devs_per_cs == 0) {
1196 denali->devs_per_cs = 1;
1197 iowrite32(1, denali->reg + DEVICES_CONNECTED);
1198 }
1199
1200 if (denali->devs_per_cs == 1)
1201 return 0;
1202
1203 if (denali->devs_per_cs != 2) {
1204 dev_err(denali->dev, "unsupported number of devices %d\n",
1205 denali->devs_per_cs);
1206 return -EINVAL;
1207 }
1208
1209 /* 2 chips in parallel */
1210 mtd->size <<= 1;
1211 mtd->erasesize <<= 1;
1212 mtd->writesize <<= 1;
1213 mtd->oobsize <<= 1;
1214 chip->chipsize <<= 1;
1215 chip->page_shift += 1;
1216 chip->phys_erase_shift += 1;
1217 chip->bbt_erase_shift += 1;
1218 chip->chip_shift += 1;
1219 chip->pagemask <<= 1;
1220 chip->ecc.size <<= 1;
1221 chip->ecc.bytes <<= 1;
1222 chip->ecc.strength <<= 1;
1223 denali->oob_skip_bytes <<= 1;
1224
1225 return 0;
1226}
1227
1228int denali_init(struct denali_nand_info *denali)
1229{
1230 struct nand_chip *chip = &denali->nand;
1231 struct mtd_info *mtd = nand_to_mtd(chip);
1232 u32 features = ioread32(denali->reg + FEATURES);
1233 int ret;
1234
1235 denali_hw_init(denali);
1236
1237 denali_clear_irq_all(denali);
1238
1239 denali_reset_banks(denali);
1240
1241 denali->active_bank = DENALI_INVALID_BANK;
1242
1243 chip->flash_node = dev_of_offset(denali->dev);
1244 /* Fallback to the default name if DT did not give "label" property */
1245 if (!mtd->name)
1246 mtd->name = "denali-nand";
Chin Liang See03534df2014-09-12 00:42:17 -05001247
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001248 chip->select_chip = denali_select_chip;
1249 chip->read_byte = denali_read_byte;
1250 chip->write_byte = denali_write_byte;
1251 chip->read_word = denali_read_word;
1252 chip->cmd_ctrl = denali_cmd_ctrl;
1253 chip->dev_ready = denali_dev_ready;
1254 chip->waitfunc = denali_waitfunc;
1255
1256 if (features & FEATURES__INDEX_ADDR) {
1257 denali->host_read = denali_indexed_read;
1258 denali->host_write = denali_indexed_write;
1259 } else {
1260 denali->host_read = denali_direct_read;
1261 denali->host_write = denali_direct_write;
1262 }
1263
1264 /* clk rate info is needed for setup_data_interface */
1265 if (denali->clk_x_rate)
1266 chip->setup_data_interface = denali_setup_data_interface;
1267
1268 ret = nand_scan_ident(mtd, denali->max_banks, NULL);
1269 if (ret)
1270 return ret;
1271
1272 if (ioread32(denali->reg + FEATURES) & FEATURES__DMA)
1273 denali->dma_avail = 1;
1274
1275 if (denali->dma_avail) {
Masahiro Yamadae83725e2018-07-19 10:13:23 +09001276 chip->buf_align = ARCH_DMA_MINALIGN;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001277 if (denali->caps & DENALI_CAP_DMA_64BIT)
1278 denali->setup_dma = denali_setup_dma64;
1279 else
1280 denali->setup_dma = denali_setup_dma32;
1281 } else {
1282 chip->buf_align = 4;
1283 }
1284
1285 chip->options |= NAND_USE_BOUNCE_BUFFER;
1286 chip->bbt_options |= NAND_BBT_USE_FLASH;
1287 chip->bbt_options |= NAND_BBT_NO_OOB;
1288 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001289
Scott Wood3ea94ed2015-06-26 19:03:26 -05001290 /* no subpage writes on denali */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001291 chip->options |= NAND_NO_SUBPAGE_WRITE;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001292
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001293 ret = denali_ecc_setup(mtd, chip, denali);
1294 if (ret) {
1295 dev_err(denali->dev, "Failed to setup ECC settings.\n");
1296 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001297 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001298
1299 dev_dbg(denali->dev,
1300 "chosen ECC settings: step=%d, strength=%d, bytes=%d\n",
1301 chip->ecc.size, chip->ecc.strength, chip->ecc.bytes);
1302
1303 iowrite32(FIELD_PREP(ECC_CORRECTION__ERASE_THRESHOLD, 1) |
1304 FIELD_PREP(ECC_CORRECTION__VALUE, chip->ecc.strength),
1305 denali->reg + ECC_CORRECTION);
1306 iowrite32(mtd->erasesize / mtd->writesize,
1307 denali->reg + PAGES_PER_BLOCK);
1308 iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0,
1309 denali->reg + DEVICE_WIDTH);
1310 iowrite32(chip->options & NAND_ROW_ADDR_3 ? 0 : TWO_ROW_ADDR_CYCLES__FLAG,
1311 denali->reg + TWO_ROW_ADDR_CYCLES);
1312 iowrite32(mtd->writesize, denali->reg + DEVICE_MAIN_AREA_SIZE);
1313 iowrite32(mtd->oobsize, denali->reg + DEVICE_SPARE_AREA_SIZE);
1314
1315 iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE);
1316 iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE);
1317 /* chip->ecc.steps is set by nand_scan_tail(); not available here */
1318 iowrite32(mtd->writesize / chip->ecc.size,
1319 denali->reg + CFG_NUM_DATA_BLOCKS);
1320
1321 mtd_set_ooblayout(mtd, &denali_ooblayout_ops);
1322
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001323 nand_oob.eccbytes = denali->nand.ecc.bytes;
1324 denali->nand.ecc.layout = &nand_oob;
Chin Liang See03534df2014-09-12 00:42:17 -05001325
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001326 if (chip->options & NAND_BUSWIDTH_16) {
1327 chip->read_buf = denali_read_buf16;
1328 chip->write_buf = denali_write_buf16;
1329 } else {
1330 chip->read_buf = denali_read_buf;
1331 chip->write_buf = denali_write_buf;
1332 }
1333 chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
1334 chip->ecc.read_page = denali_read_page;
1335 chip->ecc.read_page_raw = denali_read_page_raw;
1336 chip->ecc.write_page = denali_write_page;
1337 chip->ecc.write_page_raw = denali_write_page_raw;
1338 chip->ecc.read_oob = denali_read_oob;
1339 chip->ecc.write_oob = denali_write_oob;
1340 chip->erase = denali_erase;
Masahiro Yamada628ee1e2014-11-13 20:31:51 +09001341
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001342 ret = denali_multidev_fixup(denali);
1343 if (ret)
1344 return ret;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001345
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001346 /*
1347 * This buffer is DMA-mapped by denali_{read,write}_page_raw. Do not
1348 * use devm_kmalloc() because the memory allocated by devm_ does not
1349 * guarantee DMA-safe alignment.
1350 */
1351 denali->buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
1352 if (!denali->buf)
1353 return -ENOMEM;
1354
1355 ret = nand_scan_tail(mtd);
1356 if (ret)
1357 goto free_buf;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001358
Scott Wood52ab7ce2016-05-30 13:57:58 -05001359 ret = nand_register(0, mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001360 if (ret) {
1361 dev_err(denali->dev, "Failed to register MTD: %d\n", ret);
1362 goto free_buf;
1363 }
1364 return 0;
1365
1366free_buf:
1367 kfree(denali->buf);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001368
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001369 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001370}