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