blob: 6266c8aa925476fe24cb9c173ab972e79c3922f7 [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 Yamada8b0c16f2017-11-22 02:38:32 +090024 if (dir == DMA_FROM_DEVICE)
25 invalidate_dcache_range(addr, addr + size);
26 else
27 flush_dcache_range(addr, addr + size);
Chin Liang See03534df2014-09-12 00:42:17 -050028
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090029 return addr;
30}
Chin Liang See03534df2014-09-12 00:42:17 -050031
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090032static void dma_unmap_single(void *dev, dma_addr_t addr, size_t size,
33 enum dma_data_direction dir)
Scott Wood52ab7ce2016-05-30 13:57:58 -050034{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090035 if (dir != DMA_TO_DEVICE)
36 invalidate_dcache_range(addr, addr + size);
Scott Wood52ab7ce2016-05-30 13:57:58 -050037}
Chin Liang See03534df2014-09-12 00:42:17 -050038
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090039static int dma_mapping_error(void *dev, dma_addr_t addr)
Chin Liang See03534df2014-09-12 00:42:17 -050040{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090041 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -050042}
43
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090044#define DENALI_NAND_NAME "denali-nand"
Chin Liang See03534df2014-09-12 00:42:17 -050045
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090046/* for Indexed Addressing */
47#define DENALI_INDEXED_CTRL 0x00
48#define DENALI_INDEXED_DATA 0x10
Chin Liang See03534df2014-09-12 00:42:17 -050049
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090050#define DENALI_MAP00 (0 << 26) /* direct access to buffer */
51#define DENALI_MAP01 (1 << 26) /* read/write pages in PIO */
52#define DENALI_MAP10 (2 << 26) /* high-level control plane */
53#define DENALI_MAP11 (3 << 26) /* direct controller access */
Chin Liang See03534df2014-09-12 00:42:17 -050054
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090055/* MAP11 access cycle type */
56#define DENALI_MAP11_CMD ((DENALI_MAP11) | 0) /* command cycle */
57#define DENALI_MAP11_ADDR ((DENALI_MAP11) | 1) /* address cycle */
58#define DENALI_MAP11_DATA ((DENALI_MAP11) | 2) /* data cycle */
Chin Liang See03534df2014-09-12 00:42:17 -050059
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090060/* MAP10 commands */
61#define DENALI_ERASE 0x01
Chin Liang See03534df2014-09-12 00:42:17 -050062
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090063#define DENALI_BANK(denali) ((denali)->active_bank << 24)
Chin Liang See03534df2014-09-12 00:42:17 -050064
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090065#define DENALI_INVALID_BANK -1
66#define DENALI_NR_BANKS 4
Chin Liang See03534df2014-09-12 00:42:17 -050067
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090068/*
69 * The bus interface clock, clk_x, is phase aligned with the core clock. The
70 * clk_x is an integral multiple N of the core clk. The value N is configured
71 * at IP delivery time, and its available value is 4, 5, or 6. We need to align
72 * to the largest value to make it work with any possible configuration.
73 */
74#define DENALI_CLK_X_MULT 6
Chin Liang See03534df2014-09-12 00:42:17 -050075
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090076static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -050077{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090078 return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand);
Chin Liang See03534df2014-09-12 00:42:17 -050079}
80
81/*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090082 * Direct Addressing - the slave address forms the control information (command
83 * type, bank, block, and page address). The slave data is the actual data to
84 * be transferred. This mode requires 28 bits of address region allocated.
Scott Wood3ea94ed2015-06-26 19:03:26 -050085 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090086static u32 denali_direct_read(struct denali_nand_info *denali, u32 addr)
Chin Liang See03534df2014-09-12 00:42:17 -050087{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090088 return ioread32(denali->host + addr);
Chin Liang See03534df2014-09-12 00:42:17 -050089}
90
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090091static void denali_direct_write(struct denali_nand_info *denali, u32 addr,
92 u32 data)
Chin Liang See03534df2014-09-12 00:42:17 -050093{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090094 iowrite32(data, denali->host + addr);
Chin Liang See03534df2014-09-12 00:42:17 -050095}
96
Scott Wood3ea94ed2015-06-26 19:03:26 -050097/*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090098 * Indexed Addressing - address translation module intervenes in passing the
99 * control information. This mode reduces the required address range. The
100 * control information and transferred data are latched by the registers in
101 * the translation module.
Scott Wood3ea94ed2015-06-26 19:03:26 -0500102 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900103static u32 denali_indexed_read(struct denali_nand_info *denali, u32 addr)
Chin Liang See03534df2014-09-12 00:42:17 -0500104{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900105 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
106 return ioread32(denali->host + DENALI_INDEXED_DATA);
Chin Liang See03534df2014-09-12 00:42:17 -0500107}
108
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900109static void denali_indexed_write(struct denali_nand_info *denali, u32 addr,
110 u32 data)
Chin Liang See03534df2014-09-12 00:42:17 -0500111{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900112 iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
113 iowrite32(data, denali->host + DENALI_INDEXED_DATA);
Chin Liang See03534df2014-09-12 00:42:17 -0500114}
115
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900116/*
117 * Use the configuration feature register to determine the maximum number of
118 * banks that the hardware supports.
119 */
120static void denali_detect_max_banks(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500121{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900122 uint32_t features = ioread32(denali->reg + FEATURES);
Chin Liang See03534df2014-09-12 00:42:17 -0500123
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900124 denali->max_banks = 1 << FIELD_GET(FEATURES__N_BANKS, features);
Chin Liang See03534df2014-09-12 00:42:17 -0500125
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900126 /* the encoding changed from rev 5.0 to 5.1 */
127 if (denali->revision < 0x0501)
128 denali->max_banks <<= 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500129}
130
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900131static void __maybe_unused denali_enable_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500132{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500133 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500134
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900135 for (i = 0; i < DENALI_NR_BANKS; i++)
136 iowrite32(U32_MAX, denali->reg + INTR_EN(i));
137 iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500138}
139
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900140static void __maybe_unused denali_disable_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500141{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900142 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500143
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900144 for (i = 0; i < DENALI_NR_BANKS; i++)
145 iowrite32(0, denali->reg + INTR_EN(i));
146 iowrite32(0, denali->reg + GLOBAL_INT_ENABLE);
147}
Chin Liang See03534df2014-09-12 00:42:17 -0500148
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900149static void denali_clear_irq(struct denali_nand_info *denali,
150 int bank, uint32_t irq_status)
151{
152 /* write one to clear bits */
153 iowrite32(irq_status, denali->reg + INTR_STATUS(bank));
154}
Chin Liang See03534df2014-09-12 00:42:17 -0500155
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900156static void denali_clear_irq_all(struct denali_nand_info *denali)
157{
158 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500159
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900160 for (i = 0; i < DENALI_NR_BANKS; i++)
161 denali_clear_irq(denali, i, U32_MAX);
162}
Chin Liang See03534df2014-09-12 00:42:17 -0500163
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900164static void __denali_check_irq(struct denali_nand_info *denali)
165{
166 uint32_t irq_status;
167 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500168
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900169 for (i = 0; i < DENALI_NR_BANKS; i++) {
170 irq_status = ioread32(denali->reg + INTR_STATUS(i));
171 denali_clear_irq(denali, i, irq_status);
Chin Liang See03534df2014-09-12 00:42:17 -0500172
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900173 if (i != denali->active_bank)
174 continue;
Chin Liang See03534df2014-09-12 00:42:17 -0500175
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900176 denali->irq_status |= irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500177 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900178}
Chin Liang See03534df2014-09-12 00:42:17 -0500179
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900180static void denali_reset_irq(struct denali_nand_info *denali)
181{
182 denali->irq_status = 0;
183 denali->irq_mask = 0;
184}
Chin Liang See03534df2014-09-12 00:42:17 -0500185
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900186static uint32_t denali_wait_for_irq(struct denali_nand_info *denali,
187 uint32_t irq_mask)
188{
189 unsigned long time_left = 1000000;
Chin Liang See03534df2014-09-12 00:42:17 -0500190
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900191 while (time_left) {
192 __denali_check_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500193
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900194 if (irq_mask & denali->irq_status)
195 return denali->irq_status;
196 udelay(1);
197 time_left--;
Chin Liang See03534df2014-09-12 00:42:17 -0500198 }
199
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900200 if (!time_left) {
201 dev_err(denali->dev, "timeout while waiting for irq 0x%x\n",
202 irq_mask);
203 return 0;
204 }
Chin Liang See03534df2014-09-12 00:42:17 -0500205
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900206 return denali->irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500207}
208
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900209static uint32_t denali_check_irq(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -0500210{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900211 __denali_check_irq(denali);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500212
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900213 return denali->irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500214}
215
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900216static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500217{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900218 struct denali_nand_info *denali = mtd_to_denali(mtd);
219 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
220 int i;
221
222 for (i = 0; i < len; i++)
223 buf[i] = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500224}
225
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900226static void denali_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500227{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900228 struct denali_nand_info *denali = mtd_to_denali(mtd);
229 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
230 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500231
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900232 for (i = 0; i < len; i++)
233 denali->host_write(denali, addr, buf[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500234}
235
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900236static void denali_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500237{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900238 struct denali_nand_info *denali = mtd_to_denali(mtd);
239 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
240 uint16_t *buf16 = (uint16_t *)buf;
241 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500242
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900243 for (i = 0; i < len / 2; i++)
244 buf16[i] = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500245}
246
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900247static void denali_write_buf16(struct mtd_info *mtd, const uint8_t *buf,
248 int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500249{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900250 struct denali_nand_info *denali = mtd_to_denali(mtd);
251 u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
252 const uint16_t *buf16 = (const uint16_t *)buf;
Chin Liang See03534df2014-09-12 00:42:17 -0500253 int i;
254
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900255 for (i = 0; i < len / 2; i++)
256 denali->host_write(denali, addr, buf16[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500257}
258
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900259static uint8_t denali_read_byte(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500260{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900261 uint8_t byte;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900262
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900263 denali_read_buf(mtd, &byte, 1);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900264
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900265 return byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500266}
267
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900268static void denali_write_byte(struct mtd_info *mtd, uint8_t byte)
Chin Liang See03534df2014-09-12 00:42:17 -0500269{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900270 denali_write_buf(mtd, &byte, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500271}
272
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900273static uint16_t denali_read_word(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500274{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900275 uint16_t word;
Chin Liang See03534df2014-09-12 00:42:17 -0500276
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900277 denali_read_buf16(mtd, (uint8_t *)&word, 2);
Chin Liang See03534df2014-09-12 00:42:17 -0500278
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900279 return word;
280}
Chin Liang See03534df2014-09-12 00:42:17 -0500281
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900282static void denali_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
283{
284 struct denali_nand_info *denali = mtd_to_denali(mtd);
285 uint32_t type;
Chin Liang See03534df2014-09-12 00:42:17 -0500286
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900287 if (ctrl & NAND_CLE)
288 type = DENALI_MAP11_CMD;
289 else if (ctrl & NAND_ALE)
290 type = DENALI_MAP11_ADDR;
291 else
292 return;
Chin Liang See03534df2014-09-12 00:42:17 -0500293
Scott Wood3ea94ed2015-06-26 19:03:26 -0500294 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900295 * Some commands are followed by chip->dev_ready or chip->waitfunc.
296 * irq_status must be cleared here to catch the R/B# interrupt later.
Chin Liang See03534df2014-09-12 00:42:17 -0500297 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900298 if (ctrl & NAND_CTRL_CHANGE)
299 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500300
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900301 denali->host_write(denali, DENALI_BANK(denali) | type, dat);
Chin Liang See03534df2014-09-12 00:42:17 -0500302}
303
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900304static int denali_dev_ready(struct mtd_info *mtd)
Chin Liang See03534df2014-09-12 00:42:17 -0500305{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900306 struct denali_nand_info *denali = mtd_to_denali(mtd);
307
308 return !!(denali_check_irq(denali) & INTR__INT_ACT);
Chin Liang See03534df2014-09-12 00:42:17 -0500309}
310
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900311static int denali_check_erased_page(struct mtd_info *mtd,
312 struct nand_chip *chip, uint8_t *buf,
313 unsigned long uncor_ecc_flags,
314 unsigned int max_bitflips)
Chin Liang See03534df2014-09-12 00:42:17 -0500315{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900316 uint8_t *ecc_code = chip->buffers->ecccode;
317 int ecc_steps = chip->ecc.steps;
318 int ecc_size = chip->ecc.size;
319 int ecc_bytes = chip->ecc.bytes;
320 int i, ret, stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500321
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900322 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
323 chip->ecc.total);
324 if (ret)
325 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500326
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900327 for (i = 0; i < ecc_steps; i++) {
328 if (!(uncor_ecc_flags & BIT(i)))
329 continue;
Chin Liang See03534df2014-09-12 00:42:17 -0500330
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900331 stat = nand_check_erased_ecc_chunk(buf, ecc_size,
332 ecc_code, ecc_bytes,
333 NULL, 0,
334 chip->ecc.strength);
335 if (stat < 0) {
336 mtd->ecc_stats.failed++;
337 } else {
338 mtd->ecc_stats.corrected += stat;
339 max_bitflips = max_t(unsigned int, max_bitflips, stat);
340 }
Chin Liang See03534df2014-09-12 00:42:17 -0500341
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900342 buf += ecc_size;
343 ecc_code += ecc_bytes;
344 }
345
346 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500347}
348
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900349static int denali_hw_ecc_fixup(struct mtd_info *mtd,
350 struct denali_nand_info *denali,
351 unsigned long *uncor_ecc_flags)
Chin Liang See03534df2014-09-12 00:42:17 -0500352{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900353 struct nand_chip *chip = mtd_to_nand(mtd);
354 int bank = denali->active_bank;
355 uint32_t ecc_cor;
356 unsigned int max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500357
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900358 ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank));
359 ecc_cor >>= ECC_COR_INFO__SHIFT(bank);
360
361 if (ecc_cor & ECC_COR_INFO__UNCOR_ERR) {
362 /*
363 * This flag is set when uncorrectable error occurs at least in
364 * one ECC sector. We can not know "how many sectors", or
365 * "which sector(s)". We need erase-page check for all sectors.
366 */
367 *uncor_ecc_flags = GENMASK(chip->ecc.steps - 1, 0);
368 return 0;
369 }
Chin Liang See03534df2014-09-12 00:42:17 -0500370
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900371 max_bitflips = FIELD_GET(ECC_COR_INFO__MAX_ERRORS, ecc_cor);
372
373 /*
374 * The register holds the maximum of per-sector corrected bitflips.
375 * This is suitable for the return value of the ->read_page() callback.
376 * Unfortunately, we can not know the total number of corrected bits in
377 * the page. Increase the stats by max_bitflips. (compromised solution)
378 */
379 mtd->ecc_stats.corrected += max_bitflips;
380
381 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500382}
383
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900384static int denali_sw_ecc_fixup(struct mtd_info *mtd,
385 struct denali_nand_info *denali,
386 unsigned long *uncor_ecc_flags, uint8_t *buf)
Chin Liang See03534df2014-09-12 00:42:17 -0500387{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900388 unsigned int ecc_size = denali->nand.ecc.size;
389 unsigned int bitflips = 0;
390 unsigned int max_bitflips = 0;
391 uint32_t err_addr, err_cor_info;
392 unsigned int err_byte, err_sector, err_device;
393 uint8_t err_cor_value;
394 unsigned int prev_sector = 0;
395 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500396
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900397 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500398
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900399 do {
400 err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS);
401 err_sector = FIELD_GET(ECC_ERROR_ADDRESS__SECTOR, err_addr);
402 err_byte = FIELD_GET(ECC_ERROR_ADDRESS__OFFSET, err_addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500403
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900404 err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO);
405 err_cor_value = FIELD_GET(ERR_CORRECTION_INFO__BYTE,
406 err_cor_info);
407 err_device = FIELD_GET(ERR_CORRECTION_INFO__DEVICE,
408 err_cor_info);
Chin Liang See03534df2014-09-12 00:42:17 -0500409
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900410 /* reset the bitflip counter when crossing ECC sector */
411 if (err_sector != prev_sector)
412 bitflips = 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500413
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900414 if (err_cor_info & ERR_CORRECTION_INFO__UNCOR) {
415 /*
416 * Check later if this is a real ECC error, or
417 * an erased sector.
418 */
419 *uncor_ecc_flags |= BIT(err_sector);
420 } else if (err_byte < ecc_size) {
421 /*
422 * If err_byte is larger than ecc_size, means error
423 * happened in OOB, so we ignore it. It's no need for
424 * us to correct it err_device is represented the NAND
425 * error bits are happened in if there are more than
426 * one NAND connected.
427 */
428 int offset;
429 unsigned int flips_in_byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500430
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900431 offset = (err_sector * ecc_size + err_byte) *
432 denali->devs_per_cs + err_device;
Chin Liang See03534df2014-09-12 00:42:17 -0500433
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900434 /* correct the ECC error */
435 flips_in_byte = hweight8(buf[offset] ^ err_cor_value);
436 buf[offset] ^= err_cor_value;
437 mtd->ecc_stats.corrected += flips_in_byte;
438 bitflips += flips_in_byte;
Chin Liang See03534df2014-09-12 00:42:17 -0500439
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900440 max_bitflips = max(max_bitflips, bitflips);
441 }
Chin Liang See03534df2014-09-12 00:42:17 -0500442
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900443 prev_sector = err_sector;
444 } while (!(err_cor_info & ERR_CORRECTION_INFO__LAST_ERR));
Chin Liang See03534df2014-09-12 00:42:17 -0500445
Scott Wood3ea94ed2015-06-26 19:03:26 -0500446 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900447 * Once handle all ECC errors, controller will trigger an
448 * ECC_TRANSACTION_DONE interrupt.
Scott Wood3ea94ed2015-06-26 19:03:26 -0500449 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900450 irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE);
451 if (!(irq_status & INTR__ECC_TRANSACTION_DONE))
452 return -EIO;
Chin Liang See03534df2014-09-12 00:42:17 -0500453
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900454 return max_bitflips;
Chin Liang See03534df2014-09-12 00:42:17 -0500455}
456
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900457static void denali_setup_dma64(struct denali_nand_info *denali,
458 dma_addr_t dma_addr, int page, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500459{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900460 uint32_t mode;
461 const int page_count = 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500462
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900463 mode = DENALI_MAP10 | DENALI_BANK(denali) | page;
Chin Liang See03534df2014-09-12 00:42:17 -0500464
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900465 /* DMA is a three step process */
Chin Liang See03534df2014-09-12 00:42:17 -0500466
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900467 /*
468 * 1. setup transfer type, interrupt when complete,
469 * burst len = 64 bytes, the number of pages
470 */
471 denali->host_write(denali, mode,
472 0x01002000 | (64 << 16) | (write << 8) | page_count);
Chin Liang See03534df2014-09-12 00:42:17 -0500473
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900474 /* 2. set memory low address */
475 denali->host_write(denali, mode, lower_32_bits(dma_addr));
Chin Liang See03534df2014-09-12 00:42:17 -0500476
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900477 /* 3. set memory high address */
478 denali->host_write(denali, mode, upper_32_bits(dma_addr));
Chin Liang See03534df2014-09-12 00:42:17 -0500479}
480
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900481static void denali_setup_dma32(struct denali_nand_info *denali,
482 dma_addr_t dma_addr, int page, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500483{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900484 uint32_t mode;
485 const int page_count = 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500486
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900487 mode = DENALI_MAP10 | DENALI_BANK(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500488
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900489 /* DMA is a four step process */
Chin Liang See03534df2014-09-12 00:42:17 -0500490
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900491 /* 1. setup transfer type and # of pages */
492 denali->host_write(denali, mode | page,
493 0x2000 | (write << 8) | page_count);
Chin Liang See03534df2014-09-12 00:42:17 -0500494
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900495 /* 2. set memory high address bits 23:8 */
496 denali->host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200);
Chin Liang See03534df2014-09-12 00:42:17 -0500497
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900498 /* 3. set memory low address bits 23:8 */
499 denali->host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300);
Chin Liang See03534df2014-09-12 00:42:17 -0500500
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900501 /* 4. interrupt when complete, burst len = 64 bytes */
502 denali->host_write(denali, mode | 0x14000, 0x2400);
Chin Liang See03534df2014-09-12 00:42:17 -0500503}
504
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900505static int denali_pio_read(struct denali_nand_info *denali, void *buf,
506 size_t size, int page, int raw)
Chin Liang See03534df2014-09-12 00:42:17 -0500507{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900508 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
509 uint32_t *buf32 = (uint32_t *)buf;
510 uint32_t irq_status, ecc_err_mask;
511 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500512
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900513 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
514 ecc_err_mask = INTR__ECC_UNCOR_ERR;
515 else
516 ecc_err_mask = INTR__ECC_ERR;
Chin Liang See03534df2014-09-12 00:42:17 -0500517
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900518 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500519
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900520 for (i = 0; i < size / 4; i++)
521 *buf32++ = denali->host_read(denali, addr);
Chin Liang See03534df2014-09-12 00:42:17 -0500522
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900523 irq_status = denali_wait_for_irq(denali, INTR__PAGE_XFER_INC);
524 if (!(irq_status & INTR__PAGE_XFER_INC))
525 return -EIO;
Chin Liang See03534df2014-09-12 00:42:17 -0500526
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900527 if (irq_status & INTR__ERASED_PAGE)
528 memset(buf, 0xff, size);
529
530 return irq_status & ecc_err_mask ? -EBADMSG : 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500531}
532
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900533static int denali_pio_write(struct denali_nand_info *denali,
534 const void *buf, size_t size, int page, int raw)
Chin Liang See03534df2014-09-12 00:42:17 -0500535{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900536 u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
537 const uint32_t *buf32 = (uint32_t *)buf;
538 uint32_t irq_status;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500539 int i;
540
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900541 denali_reset_irq(denali);
542
543 for (i = 0; i < size / 4; i++)
544 denali->host_write(denali, addr, *buf32++);
545
546 irq_status = denali_wait_for_irq(denali,
547 INTR__PROGRAM_COMP | INTR__PROGRAM_FAIL);
548 if (!(irq_status & INTR__PROGRAM_COMP))
549 return -EIO;
550
551 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500552}
553
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900554static int denali_pio_xfer(struct denali_nand_info *denali, void *buf,
555 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500556{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900557 if (write)
558 return denali_pio_write(denali, buf, size, page, raw);
559 else
560 return denali_pio_read(denali, buf, size, page, raw);
Chin Liang See03534df2014-09-12 00:42:17 -0500561}
562
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900563static int denali_dma_xfer(struct denali_nand_info *denali, void *buf,
564 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500565{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900566 dma_addr_t dma_addr;
567 uint32_t irq_mask, irq_status, ecc_err_mask;
568 enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
569 int ret = 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500570
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900571 dma_addr = dma_map_single(denali->dev, buf, size, dir);
572 if (dma_mapping_error(denali->dev, dma_addr)) {
573 dev_dbg(denali->dev, "Failed to DMA-map buffer. Trying PIO.\n");
574 return denali_pio_xfer(denali, buf, size, page, raw, write);
575 }
Chin Liang See03534df2014-09-12 00:42:17 -0500576
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900577 if (write) {
578 /*
579 * INTR__PROGRAM_COMP is never asserted for the DMA transfer.
580 * We can use INTR__DMA_CMD_COMP instead. This flag is asserted
581 * when the page program is completed.
582 */
583 irq_mask = INTR__DMA_CMD_COMP | INTR__PROGRAM_FAIL;
584 ecc_err_mask = 0;
585 } else if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) {
586 irq_mask = INTR__DMA_CMD_COMP;
587 ecc_err_mask = INTR__ECC_UNCOR_ERR;
588 } else {
589 irq_mask = INTR__DMA_CMD_COMP;
590 ecc_err_mask = INTR__ECC_ERR;
591 }
Chin Liang See03534df2014-09-12 00:42:17 -0500592
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900593 iowrite32(DMA_ENABLE__FLAG, denali->reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500594
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900595 denali_reset_irq(denali);
596 denali->setup_dma(denali, dma_addr, page, write);
Chin Liang See03534df2014-09-12 00:42:17 -0500597
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900598 irq_status = denali_wait_for_irq(denali, irq_mask);
599 if (!(irq_status & INTR__DMA_CMD_COMP))
600 ret = -EIO;
601 else if (irq_status & ecc_err_mask)
602 ret = -EBADMSG;
Chin Liang See03534df2014-09-12 00:42:17 -0500603
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900604 iowrite32(0, denali->reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500605
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900606 dma_unmap_single(denali->dev, dma_addr, size, dir);
Chin Liang See03534df2014-09-12 00:42:17 -0500607
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900608 if (irq_status & INTR__ERASED_PAGE)
609 memset(buf, 0xff, size);
Chin Liang See03534df2014-09-12 00:42:17 -0500610
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900611 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500612}
613
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900614static int denali_data_xfer(struct denali_nand_info *denali, void *buf,
615 size_t size, int page, int raw, int write)
Chin Liang See03534df2014-09-12 00:42:17 -0500616{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900617 iowrite32(raw ? 0 : ECC_ENABLE__FLAG, denali->reg + ECC_ENABLE);
618 iowrite32(raw ? TRANSFER_SPARE_REG__FLAG : 0,
619 denali->reg + TRANSFER_SPARE_REG);
Chin Liang See03534df2014-09-12 00:42:17 -0500620
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900621 if (denali->dma_avail)
622 return denali_dma_xfer(denali, buf, size, page, raw, write);
623 else
624 return denali_pio_xfer(denali, buf, size, page, raw, write);
625}
Chin Liang See03534df2014-09-12 00:42:17 -0500626
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900627static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip,
628 int page, int write)
629{
630 struct denali_nand_info *denali = mtd_to_denali(mtd);
631 unsigned int start_cmd = write ? NAND_CMD_SEQIN : NAND_CMD_READ0;
632 unsigned int rnd_cmd = write ? NAND_CMD_RNDIN : NAND_CMD_RNDOUT;
633 int writesize = mtd->writesize;
634 int oobsize = mtd->oobsize;
635 uint8_t *bufpoi = chip->oob_poi;
636 int ecc_steps = chip->ecc.steps;
637 int ecc_size = chip->ecc.size;
638 int ecc_bytes = chip->ecc.bytes;
639 int oob_skip = denali->oob_skip_bytes;
640 size_t size = writesize + oobsize;
641 int i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500642
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900643 /* BBM at the beginning of the OOB area */
644 chip->cmdfunc(mtd, start_cmd, writesize, page);
645 if (write)
646 chip->write_buf(mtd, bufpoi, oob_skip);
647 else
648 chip->read_buf(mtd, bufpoi, oob_skip);
649 bufpoi += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500650
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900651 /* OOB ECC */
652 for (i = 0; i < ecc_steps; i++) {
653 pos = ecc_size + i * (ecc_size + ecc_bytes);
654 len = ecc_bytes;
Chin Liang See03534df2014-09-12 00:42:17 -0500655
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900656 if (pos >= writesize)
657 pos += oob_skip;
658 else if (pos + len > writesize)
659 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500660
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900661 chip->cmdfunc(mtd, rnd_cmd, pos, -1);
662 if (write)
663 chip->write_buf(mtd, bufpoi, len);
664 else
665 chip->read_buf(mtd, bufpoi, len);
666 bufpoi += len;
667 if (len < ecc_bytes) {
668 len = ecc_bytes - len;
669 chip->cmdfunc(mtd, rnd_cmd, writesize + oob_skip, -1);
670 if (write)
671 chip->write_buf(mtd, bufpoi, len);
672 else
673 chip->read_buf(mtd, bufpoi, len);
674 bufpoi += len;
675 }
676 }
Chin Liang See03534df2014-09-12 00:42:17 -0500677
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900678 /* OOB free */
679 len = oobsize - (bufpoi - chip->oob_poi);
680 chip->cmdfunc(mtd, rnd_cmd, size - len, -1);
681 if (write)
682 chip->write_buf(mtd, bufpoi, len);
683 else
684 chip->read_buf(mtd, bufpoi, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500685}
686
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900687static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
688 uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500689{
690 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900691 int writesize = mtd->writesize;
692 int oobsize = mtd->oobsize;
693 int ecc_steps = chip->ecc.steps;
694 int ecc_size = chip->ecc.size;
695 int ecc_bytes = chip->ecc.bytes;
696 void *tmp_buf = denali->buf;
697 int oob_skip = denali->oob_skip_bytes;
698 size_t size = writesize + oobsize;
699 int ret, i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500700
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900701 ret = denali_data_xfer(denali, tmp_buf, size, page, 1, 0);
702 if (ret)
703 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500704
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900705 /* Arrange the buffer for syndrome payload/ecc layout */
706 if (buf) {
707 for (i = 0; i < ecc_steps; i++) {
708 pos = i * (ecc_size + ecc_bytes);
709 len = ecc_size;
Chin Liang See03534df2014-09-12 00:42:17 -0500710
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900711 if (pos >= writesize)
712 pos += oob_skip;
713 else if (pos + len > writesize)
714 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500715
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900716 memcpy(buf, tmp_buf + pos, len);
717 buf += len;
718 if (len < ecc_size) {
719 len = ecc_size - len;
720 memcpy(buf, tmp_buf + writesize + oob_skip,
721 len);
722 buf += len;
723 }
724 }
725 }
Chin Liang See03534df2014-09-12 00:42:17 -0500726
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900727 if (oob_required) {
728 uint8_t *oob = chip->oob_poi;
Chin Liang See03534df2014-09-12 00:42:17 -0500729
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900730 /* BBM at the beginning of the OOB area */
731 memcpy(oob, tmp_buf + writesize, oob_skip);
732 oob += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500733
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900734 /* OOB ECC */
735 for (i = 0; i < ecc_steps; i++) {
736 pos = ecc_size + i * (ecc_size + ecc_bytes);
737 len = ecc_bytes;
738
739 if (pos >= writesize)
740 pos += oob_skip;
741 else if (pos + len > writesize)
742 len = writesize - pos;
743
744 memcpy(oob, tmp_buf + pos, len);
745 oob += len;
746 if (len < ecc_bytes) {
747 len = ecc_bytes - len;
748 memcpy(oob, tmp_buf + writesize + oob_skip,
749 len);
750 oob += len;
751 }
752 }
753
754 /* OOB free */
755 len = oobsize - (oob - chip->oob_poi);
756 memcpy(oob, tmp_buf + size - len, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500757 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900758
Chin Liang See03534df2014-09-12 00:42:17 -0500759 return 0;
760}
761
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900762static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
763 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500764{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900765 denali_oob_xfer(mtd, chip, page, 0);
Chin Liang See03534df2014-09-12 00:42:17 -0500766
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900767 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500768}
769
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900770static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
771 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500772{
773 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900774 int status;
Chin Liang See03534df2014-09-12 00:42:17 -0500775
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900776 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500777
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900778 denali_oob_xfer(mtd, chip, page, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500779
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900780 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
781 status = chip->waitfunc(mtd, chip);
Chin Liang See03534df2014-09-12 00:42:17 -0500782
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900783 return status & NAND_STATUS_FAIL ? -EIO : 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500784}
785
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900786static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
787 uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500788{
789 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900790 unsigned long uncor_ecc_flags = 0;
791 int stat = 0;
792 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500793
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900794 ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0);
795 if (ret && ret != -EBADMSG)
796 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500797
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900798 if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
799 stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags);
800 else if (ret == -EBADMSG)
801 stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf);
Chin Liang See03534df2014-09-12 00:42:17 -0500802
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900803 if (stat < 0)
804 return stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500805
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900806 if (uncor_ecc_flags) {
807 ret = denali_read_oob(mtd, chip, page);
808 if (ret)
809 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -0500810
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900811 stat = denali_check_erased_page(mtd, chip, buf,
812 uncor_ecc_flags, stat);
Chin Liang See03534df2014-09-12 00:42:17 -0500813 }
814
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900815 return stat;
Chin Liang See03534df2014-09-12 00:42:17 -0500816}
817
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900818static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
819 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500820{
821 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900822 int writesize = mtd->writesize;
823 int oobsize = mtd->oobsize;
824 int ecc_steps = chip->ecc.steps;
825 int ecc_size = chip->ecc.size;
826 int ecc_bytes = chip->ecc.bytes;
827 void *tmp_buf = denali->buf;
828 int oob_skip = denali->oob_skip_bytes;
829 size_t size = writesize + oobsize;
830 int i, pos, len;
Chin Liang See03534df2014-09-12 00:42:17 -0500831
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900832 /*
833 * Fill the buffer with 0xff first except the full page transfer.
834 * This simplifies the logic.
835 */
836 if (!buf || !oob_required)
837 memset(tmp_buf, 0xff, size);
838
839 /* Arrange the buffer for syndrome payload/ecc layout */
840 if (buf) {
841 for (i = 0; i < ecc_steps; i++) {
842 pos = i * (ecc_size + ecc_bytes);
843 len = ecc_size;
844
845 if (pos >= writesize)
846 pos += oob_skip;
847 else if (pos + len > writesize)
848 len = writesize - pos;
849
850 memcpy(tmp_buf + pos, buf, len);
851 buf += len;
852 if (len < ecc_size) {
853 len = ecc_size - len;
854 memcpy(tmp_buf + writesize + oob_skip, buf,
855 len);
856 buf += len;
857 }
858 }
Chin Liang See03534df2014-09-12 00:42:17 -0500859 }
860
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900861 if (oob_required) {
862 const uint8_t *oob = chip->oob_poi;
Chin Liang See03534df2014-09-12 00:42:17 -0500863
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900864 /* BBM at the beginning of the OOB area */
865 memcpy(tmp_buf + writesize, oob, oob_skip);
866 oob += oob_skip;
Chin Liang See03534df2014-09-12 00:42:17 -0500867
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900868 /* OOB ECC */
869 for (i = 0; i < ecc_steps; i++) {
870 pos = ecc_size + i * (ecc_size + ecc_bytes);
871 len = ecc_bytes;
Chin Liang See03534df2014-09-12 00:42:17 -0500872
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900873 if (pos >= writesize)
874 pos += oob_skip;
875 else if (pos + len > writesize)
876 len = writesize - pos;
Chin Liang See03534df2014-09-12 00:42:17 -0500877
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900878 memcpy(tmp_buf + pos, oob, len);
879 oob += len;
880 if (len < ecc_bytes) {
881 len = ecc_bytes - len;
882 memcpy(tmp_buf + writesize + oob_skip, oob,
883 len);
884 oob += len;
885 }
Chin Liang See03534df2014-09-12 00:42:17 -0500886 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900887
888 /* OOB free */
889 len = oobsize - (oob - chip->oob_poi);
890 memcpy(tmp_buf + size - len, oob, len);
Chin Liang See03534df2014-09-12 00:42:17 -0500891 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900892
893 return denali_data_xfer(denali, tmp_buf, size, page, 1, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500894}
895
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900896static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
897 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500898{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900899 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chin Liang See03534df2014-09-12 00:42:17 -0500900
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900901 return denali_data_xfer(denali, (void *)buf, mtd->writesize,
902 page, 0, 1);
Chin Liang See03534df2014-09-12 00:42:17 -0500903}
904
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900905static void denali_select_chip(struct mtd_info *mtd, int chip)
Chin Liang See03534df2014-09-12 00:42:17 -0500906{
907 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chin Liang See03534df2014-09-12 00:42:17 -0500908
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900909 denali->active_bank = chip;
Chin Liang See03534df2014-09-12 00:42:17 -0500910}
911
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900912static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
Chin Liang See03534df2014-09-12 00:42:17 -0500913{
914 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900915 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500916
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900917 /* R/B# pin transitioned from low to high? */
918 irq_status = denali_wait_for_irq(denali, INTR__INT_ACT);
Chin Liang See03534df2014-09-12 00:42:17 -0500919
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900920 return irq_status & INTR__INT_ACT ? 0 : NAND_STATUS_FAIL;
Chin Liang See03534df2014-09-12 00:42:17 -0500921}
922
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900923static int denali_erase(struct mtd_info *mtd, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500924{
925 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900926 uint32_t irq_status;
Chin Liang See03534df2014-09-12 00:42:17 -0500927
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900928 denali_reset_irq(denali);
Chin Liang See03534df2014-09-12 00:42:17 -0500929
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900930 denali->host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page,
931 DENALI_ERASE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500932
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900933 /* wait for erase to complete or failure to occur */
934 irq_status = denali_wait_for_irq(denali,
935 INTR__ERASE_COMP | INTR__ERASE_FAIL);
Chin Liang See03534df2014-09-12 00:42:17 -0500936
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900937 return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL;
Chin Liang See03534df2014-09-12 00:42:17 -0500938}
939
Masahiro Yamada59a1f3e2017-11-29 19:18:18 +0900940static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900941 const struct nand_data_interface *conf)
Chin Liang See03534df2014-09-12 00:42:17 -0500942{
943 struct denali_nand_info *denali = mtd_to_denali(mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900944 const struct nand_sdr_timings *timings;
945 unsigned long t_clk;
946 int acc_clks, re_2_we, re_2_re, we_2_re, addr_2_data;
947 int rdwr_en_lo, rdwr_en_hi, rdwr_en_lo_hi, cs_setup;
948 int addr_2_data_mask;
949 uint32_t tmp;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500950
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900951 timings = nand_get_sdr_timings(conf);
952 if (IS_ERR(timings))
953 return PTR_ERR(timings);
Chin Liang See03534df2014-09-12 00:42:17 -0500954
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900955 /* clk_x period in picoseconds */
956 t_clk = DIV_ROUND_DOWN_ULL(1000000000000ULL, denali->clk_x_rate);
957 if (!t_clk)
958 return -EINVAL;
Chin Liang See03534df2014-09-12 00:42:17 -0500959
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900960 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
961 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -0500962
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900963 /* tREA -> ACC_CLKS */
964 acc_clks = DIV_ROUND_UP(timings->tREA_max, t_clk);
965 acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
966
967 tmp = ioread32(denali->reg + ACC_CLKS);
968 tmp &= ~ACC_CLKS__VALUE;
969 tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
970 iowrite32(tmp, denali->reg + ACC_CLKS);
971
972 /* tRWH -> RE_2_WE */
973 re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_clk);
974 re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE);
Chin Liang See03534df2014-09-12 00:42:17 -0500975
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +0900976 tmp = ioread32(denali->reg + RE_2_WE);
977 tmp &= ~RE_2_WE__VALUE;
978 tmp |= FIELD_PREP(RE_2_WE__VALUE, re_2_we);
979 iowrite32(tmp, denali->reg + RE_2_WE);
980
981 /* tRHZ -> RE_2_RE */
982 re_2_re = DIV_ROUND_UP(timings->tRHZ_max, t_clk);
983 re_2_re = min_t(int, re_2_re, RE_2_RE__VALUE);
984
985 tmp = ioread32(denali->reg + RE_2_RE);
986 tmp &= ~RE_2_RE__VALUE;
987 tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re);
988 iowrite32(tmp, denali->reg + RE_2_RE);
989
990 /*
991 * tCCS, tWHR -> WE_2_RE
992 *
993 * With WE_2_RE properly set, the Denali controller automatically takes
994 * care of the delay; the driver need not set NAND_WAIT_TCCS.
995 */
996 we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min),
997 t_clk);
998 we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE);
999
1000 tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
1001 tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE;
1002 tmp |= FIELD_PREP(TWHR2_AND_WE_2_RE__WE_2_RE, we_2_re);
1003 iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE);
1004
1005 /* tADL -> ADDR_2_DATA */
1006
1007 /* for older versions, ADDR_2_DATA is only 6 bit wide */
1008 addr_2_data_mask = TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1009 if (denali->revision < 0x0501)
1010 addr_2_data_mask >>= 1;
1011
1012 addr_2_data = DIV_ROUND_UP(timings->tADL_min, t_clk);
1013 addr_2_data = min_t(int, addr_2_data, addr_2_data_mask);
1014
1015 tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA);
1016 tmp &= ~TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1017 tmp |= FIELD_PREP(TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA, addr_2_data);
1018 iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA);
1019
1020 /* tREH, tWH -> RDWR_EN_HI_CNT */
1021 rdwr_en_hi = DIV_ROUND_UP(max(timings->tREH_min, timings->tWH_min),
1022 t_clk);
1023 rdwr_en_hi = min_t(int, rdwr_en_hi, RDWR_EN_HI_CNT__VALUE);
1024
1025 tmp = ioread32(denali->reg + RDWR_EN_HI_CNT);
1026 tmp &= ~RDWR_EN_HI_CNT__VALUE;
1027 tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi);
1028 iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT);
1029
1030 /* tRP, tWP -> RDWR_EN_LO_CNT */
1031 rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min),
1032 t_clk);
1033 rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min),
1034 t_clk);
1035 rdwr_en_lo_hi = max(rdwr_en_lo_hi, DENALI_CLK_X_MULT);
1036 rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
1037 rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
1038
1039 tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
1040 tmp &= ~RDWR_EN_LO_CNT__VALUE;
1041 tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo);
1042 iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT);
1043
1044 /* tCS, tCEA -> CS_SETUP_CNT */
1045 cs_setup = max3((int)DIV_ROUND_UP(timings->tCS_min, t_clk) - rdwr_en_lo,
1046 (int)DIV_ROUND_UP(timings->tCEA_max, t_clk) - acc_clks,
1047 0);
1048 cs_setup = min_t(int, cs_setup, CS_SETUP_CNT__VALUE);
1049
1050 tmp = ioread32(denali->reg + CS_SETUP_CNT);
1051 tmp &= ~CS_SETUP_CNT__VALUE;
1052 tmp |= FIELD_PREP(CS_SETUP_CNT__VALUE, cs_setup);
1053 iowrite32(tmp, denali->reg + CS_SETUP_CNT);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001054
1055 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -05001056}
1057
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001058static void denali_reset_banks(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001059{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001060 u32 irq_status;
1061 int i;
Chin Liang See03534df2014-09-12 00:42:17 -05001062
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001063 for (i = 0; i < denali->max_banks; i++) {
1064 denali->active_bank = i;
1065
1066 denali_reset_irq(denali);
1067
1068 iowrite32(DEVICE_RESET__BANK(i),
1069 denali->reg + DEVICE_RESET);
1070
1071 irq_status = denali_wait_for_irq(denali,
1072 INTR__RST_COMP | INTR__INT_ACT | INTR__TIME_OUT);
1073 if (!(irq_status & INTR__INT_ACT))
1074 break;
Chin Liang See03534df2014-09-12 00:42:17 -05001075 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001076
1077 dev_dbg(denali->dev, "%d chips connected\n", i);
1078 denali->max_banks = i;
Chin Liang See03534df2014-09-12 00:42:17 -05001079}
Chin Liang See03534df2014-09-12 00:42:17 -05001080
Chin Liang See03534df2014-09-12 00:42:17 -05001081static void denali_hw_init(struct denali_nand_info *denali)
1082{
1083 /*
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001084 * The REVISION register may not be reliable. Platforms are allowed to
1085 * override it.
1086 */
1087 if (!denali->revision)
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001088 denali->revision = swab16(ioread32(denali->reg + REVISION));
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001089
1090 /*
Chin Liang See03534df2014-09-12 00:42:17 -05001091 * tell driver how many bit controller will skip before writing
1092 * ECC code in OOB. This is normally used for bad block marker
1093 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001094 denali->oob_skip_bytes = CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES;
1095 iowrite32(denali->oob_skip_bytes, denali->reg + SPARE_AREA_SKIP_BYTES);
1096 denali_detect_max_banks(denali);
1097 iowrite32(0x0F, denali->reg + RB_PIN_ENABLED);
1098 iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE);
Chin Liang See03534df2014-09-12 00:42:17 -05001099
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001100 iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER);
Chin Liang See03534df2014-09-12 00:42:17 -05001101}
1102
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001103int denali_calc_ecc_bytes(int step_size, int strength)
1104{
1105 /* BCH code. Denali requires ecc.bytes to be multiple of 2 */
1106 return DIV_ROUND_UP(strength * fls(step_size * 8), 16) * 2;
1107}
1108EXPORT_SYMBOL(denali_calc_ecc_bytes);
Chin Liang See03534df2014-09-12 00:42:17 -05001109
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001110static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip,
1111 struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001112{
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001113 int oobavail = mtd->oobsize - denali->oob_skip_bytes;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001114 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001115
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001116 /*
1117 * If .size and .strength are already set (usually by DT),
1118 * check if they are supported by this controller.
1119 */
1120 if (chip->ecc.size && chip->ecc.strength)
1121 return nand_check_ecc_caps(chip, denali->ecc_caps, oobavail);
1122
1123 /*
1124 * We want .size and .strength closest to the chip's requirement
1125 * unless NAND_ECC_MAXIMIZE is requested.
1126 */
1127 if (!(chip->ecc.options & NAND_ECC_MAXIMIZE)) {
1128 ret = nand_match_ecc_req(chip, denali->ecc_caps, oobavail);
1129 if (!ret)
1130 return 0;
1131 }
1132
1133 /* Max ECC strength is the last thing we can do */
1134 return nand_maximize_ecc(chip, denali->ecc_caps, oobavail);
1135}
1136
1137static struct nand_ecclayout nand_oob;
1138
1139static int denali_ooblayout_ecc(struct mtd_info *mtd, int section,
1140 struct mtd_oob_region *oobregion)
1141{
1142 struct denali_nand_info *denali = mtd_to_denali(mtd);
1143 struct nand_chip *chip = mtd_to_nand(mtd);
1144
1145 if (section)
1146 return -ERANGE;
1147
1148 oobregion->offset = denali->oob_skip_bytes;
1149 oobregion->length = chip->ecc.total;
1150
1151 return 0;
1152}
1153
1154static int denali_ooblayout_free(struct mtd_info *mtd, int section,
1155 struct mtd_oob_region *oobregion)
1156{
1157 struct denali_nand_info *denali = mtd_to_denali(mtd);
1158 struct nand_chip *chip = mtd_to_nand(mtd);
1159
1160 if (section)
1161 return -ERANGE;
1162
1163 oobregion->offset = chip->ecc.total + denali->oob_skip_bytes;
1164 oobregion->length = mtd->oobsize - oobregion->offset;
1165
1166 return 0;
1167}
Chin Liang See03534df2014-09-12 00:42:17 -05001168
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001169static const struct mtd_ooblayout_ops denali_ooblayout_ops = {
1170 .ecc = denali_ooblayout_ecc,
1171 .free = denali_ooblayout_free,
1172};
Chin Liang See03534df2014-09-12 00:42:17 -05001173
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001174static int denali_multidev_fixup(struct denali_nand_info *denali)
1175{
1176 struct nand_chip *chip = &denali->nand;
1177 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001178
1179 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001180 * Support for multi device:
1181 * When the IP configuration is x16 capable and two x8 chips are
1182 * connected in parallel, DEVICES_CONNECTED should be set to 2.
1183 * In this case, the core framework knows nothing about this fact,
1184 * so we should tell it the _logical_ pagesize and anything necessary.
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001185 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001186 denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED);
Chin Liang See03534df2014-09-12 00:42:17 -05001187
Chin Liang See03534df2014-09-12 00:42:17 -05001188 /*
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001189 * On some SoCs, DEVICES_CONNECTED is not auto-detected.
1190 * For those, DEVICES_CONNECTED is left to 0. Set 1 if it is the case.
Chin Liang See03534df2014-09-12 00:42:17 -05001191 */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001192 if (denali->devs_per_cs == 0) {
1193 denali->devs_per_cs = 1;
1194 iowrite32(1, denali->reg + DEVICES_CONNECTED);
1195 }
1196
1197 if (denali->devs_per_cs == 1)
1198 return 0;
1199
1200 if (denali->devs_per_cs != 2) {
1201 dev_err(denali->dev, "unsupported number of devices %d\n",
1202 denali->devs_per_cs);
1203 return -EINVAL;
1204 }
1205
1206 /* 2 chips in parallel */
1207 mtd->size <<= 1;
1208 mtd->erasesize <<= 1;
1209 mtd->writesize <<= 1;
1210 mtd->oobsize <<= 1;
1211 chip->chipsize <<= 1;
1212 chip->page_shift += 1;
1213 chip->phys_erase_shift += 1;
1214 chip->bbt_erase_shift += 1;
1215 chip->chip_shift += 1;
1216 chip->pagemask <<= 1;
1217 chip->ecc.size <<= 1;
1218 chip->ecc.bytes <<= 1;
1219 chip->ecc.strength <<= 1;
1220 denali->oob_skip_bytes <<= 1;
1221
1222 return 0;
1223}
1224
1225int denali_init(struct denali_nand_info *denali)
1226{
1227 struct nand_chip *chip = &denali->nand;
1228 struct mtd_info *mtd = nand_to_mtd(chip);
1229 u32 features = ioread32(denali->reg + FEATURES);
1230 int ret;
1231
1232 denali_hw_init(denali);
1233
1234 denali_clear_irq_all(denali);
1235
1236 denali_reset_banks(denali);
1237
1238 denali->active_bank = DENALI_INVALID_BANK;
1239
1240 chip->flash_node = dev_of_offset(denali->dev);
1241 /* Fallback to the default name if DT did not give "label" property */
1242 if (!mtd->name)
1243 mtd->name = "denali-nand";
Chin Liang See03534df2014-09-12 00:42:17 -05001244
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001245 chip->select_chip = denali_select_chip;
1246 chip->read_byte = denali_read_byte;
1247 chip->write_byte = denali_write_byte;
1248 chip->read_word = denali_read_word;
1249 chip->cmd_ctrl = denali_cmd_ctrl;
1250 chip->dev_ready = denali_dev_ready;
1251 chip->waitfunc = denali_waitfunc;
1252
1253 if (features & FEATURES__INDEX_ADDR) {
1254 denali->host_read = denali_indexed_read;
1255 denali->host_write = denali_indexed_write;
1256 } else {
1257 denali->host_read = denali_direct_read;
1258 denali->host_write = denali_direct_write;
1259 }
1260
1261 /* clk rate info is needed for setup_data_interface */
1262 if (denali->clk_x_rate)
1263 chip->setup_data_interface = denali_setup_data_interface;
1264
1265 ret = nand_scan_ident(mtd, denali->max_banks, NULL);
1266 if (ret)
1267 return ret;
1268
1269 if (ioread32(denali->reg + FEATURES) & FEATURES__DMA)
1270 denali->dma_avail = 1;
1271
1272 if (denali->dma_avail) {
1273 chip->buf_align = 16;
1274 if (denali->caps & DENALI_CAP_DMA_64BIT)
1275 denali->setup_dma = denali_setup_dma64;
1276 else
1277 denali->setup_dma = denali_setup_dma32;
1278 } else {
1279 chip->buf_align = 4;
1280 }
1281
1282 chip->options |= NAND_USE_BOUNCE_BUFFER;
1283 chip->bbt_options |= NAND_BBT_USE_FLASH;
1284 chip->bbt_options |= NAND_BBT_NO_OOB;
1285 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001286
Scott Wood3ea94ed2015-06-26 19:03:26 -05001287 /* no subpage writes on denali */
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001288 chip->options |= NAND_NO_SUBPAGE_WRITE;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001289
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001290 ret = denali_ecc_setup(mtd, chip, denali);
1291 if (ret) {
1292 dev_err(denali->dev, "Failed to setup ECC settings.\n");
1293 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001294 }
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001295
1296 dev_dbg(denali->dev,
1297 "chosen ECC settings: step=%d, strength=%d, bytes=%d\n",
1298 chip->ecc.size, chip->ecc.strength, chip->ecc.bytes);
1299
1300 iowrite32(FIELD_PREP(ECC_CORRECTION__ERASE_THRESHOLD, 1) |
1301 FIELD_PREP(ECC_CORRECTION__VALUE, chip->ecc.strength),
1302 denali->reg + ECC_CORRECTION);
1303 iowrite32(mtd->erasesize / mtd->writesize,
1304 denali->reg + PAGES_PER_BLOCK);
1305 iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0,
1306 denali->reg + DEVICE_WIDTH);
1307 iowrite32(chip->options & NAND_ROW_ADDR_3 ? 0 : TWO_ROW_ADDR_CYCLES__FLAG,
1308 denali->reg + TWO_ROW_ADDR_CYCLES);
1309 iowrite32(mtd->writesize, denali->reg + DEVICE_MAIN_AREA_SIZE);
1310 iowrite32(mtd->oobsize, denali->reg + DEVICE_SPARE_AREA_SIZE);
1311
1312 iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE);
1313 iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE);
1314 /* chip->ecc.steps is set by nand_scan_tail(); not available here */
1315 iowrite32(mtd->writesize / chip->ecc.size,
1316 denali->reg + CFG_NUM_DATA_BLOCKS);
1317
1318 mtd_set_ooblayout(mtd, &denali_ooblayout_ops);
1319
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001320 nand_oob.eccbytes = denali->nand.ecc.bytes;
1321 denali->nand.ecc.layout = &nand_oob;
Chin Liang See03534df2014-09-12 00:42:17 -05001322
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001323 if (chip->options & NAND_BUSWIDTH_16) {
1324 chip->read_buf = denali_read_buf16;
1325 chip->write_buf = denali_write_buf16;
1326 } else {
1327 chip->read_buf = denali_read_buf;
1328 chip->write_buf = denali_write_buf;
1329 }
1330 chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
1331 chip->ecc.read_page = denali_read_page;
1332 chip->ecc.read_page_raw = denali_read_page_raw;
1333 chip->ecc.write_page = denali_write_page;
1334 chip->ecc.write_page_raw = denali_write_page_raw;
1335 chip->ecc.read_oob = denali_read_oob;
1336 chip->ecc.write_oob = denali_write_oob;
1337 chip->erase = denali_erase;
Masahiro Yamada628ee1e2014-11-13 20:31:51 +09001338
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001339 ret = denali_multidev_fixup(denali);
1340 if (ret)
1341 return ret;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001342
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001343 /*
1344 * This buffer is DMA-mapped by denali_{read,write}_page_raw. Do not
1345 * use devm_kmalloc() because the memory allocated by devm_ does not
1346 * guarantee DMA-safe alignment.
1347 */
1348 denali->buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
1349 if (!denali->buf)
1350 return -ENOMEM;
1351
1352 ret = nand_scan_tail(mtd);
1353 if (ret)
1354 goto free_buf;
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001355
Scott Wood52ab7ce2016-05-30 13:57:58 -05001356 ret = nand_register(0, mtd);
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +09001357 if (ret) {
1358 dev_err(denali->dev, "Failed to register MTD: %d\n", ret);
1359 goto free_buf;
1360 }
1361 return 0;
1362
1363free_buf:
1364 kfree(denali->buf);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001365
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001366 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001367}