1*2cd457f3SChristophe Kerello // SPDX-License-Identifier: GPL-2.0 2*2cd457f3SChristophe Kerello /* 3*2cd457f3SChristophe Kerello * Copyright (C) STMicroelectronics 2018 4*2cd457f3SChristophe Kerello * Author: Christophe Kerello <christophe.kerello@st.com> 5*2cd457f3SChristophe Kerello */ 6*2cd457f3SChristophe Kerello 7*2cd457f3SChristophe Kerello #include <linux/clk.h> 8*2cd457f3SChristophe Kerello #include <linux/dmaengine.h> 9*2cd457f3SChristophe Kerello #include <linux/dma-mapping.h> 10*2cd457f3SChristophe Kerello #include <linux/errno.h> 11*2cd457f3SChristophe Kerello #include <linux/interrupt.h> 12*2cd457f3SChristophe Kerello #include <linux/iopoll.h> 13*2cd457f3SChristophe Kerello #include <linux/module.h> 14*2cd457f3SChristophe Kerello #include <linux/mtd/rawnand.h> 15*2cd457f3SChristophe Kerello #include <linux/pinctrl/consumer.h> 16*2cd457f3SChristophe Kerello #include <linux/platform_device.h> 17*2cd457f3SChristophe Kerello #include <linux/reset.h> 18*2cd457f3SChristophe Kerello 19*2cd457f3SChristophe Kerello /* Bad block marker length */ 20*2cd457f3SChristophe Kerello #define FMC2_BBM_LEN 2 21*2cd457f3SChristophe Kerello 22*2cd457f3SChristophe Kerello /* ECC step size */ 23*2cd457f3SChristophe Kerello #define FMC2_ECC_STEP_SIZE 512 24*2cd457f3SChristophe Kerello 25*2cd457f3SChristophe Kerello /* BCHDSRx registers length */ 26*2cd457f3SChristophe Kerello #define FMC2_BCHDSRS_LEN 20 27*2cd457f3SChristophe Kerello 28*2cd457f3SChristophe Kerello /* HECCR length */ 29*2cd457f3SChristophe Kerello #define FMC2_HECCR_LEN 4 30*2cd457f3SChristophe Kerello 31*2cd457f3SChristophe Kerello /* Max requests done for a 8k nand page size */ 32*2cd457f3SChristophe Kerello #define FMC2_MAX_SG 16 33*2cd457f3SChristophe Kerello 34*2cd457f3SChristophe Kerello /* Max chip enable */ 35*2cd457f3SChristophe Kerello #define FMC2_MAX_CE 2 36*2cd457f3SChristophe Kerello 37*2cd457f3SChristophe Kerello /* Max ECC buffer length */ 38*2cd457f3SChristophe Kerello #define FMC2_MAX_ECC_BUF_LEN (FMC2_BCHDSRS_LEN * FMC2_MAX_SG) 39*2cd457f3SChristophe Kerello 40*2cd457f3SChristophe Kerello /* Timings */ 41*2cd457f3SChristophe Kerello #define FMC2_THIZ 1 42*2cd457f3SChristophe Kerello #define FMC2_TIO 8000 43*2cd457f3SChristophe Kerello #define FMC2_TSYNC 3000 44*2cd457f3SChristophe Kerello #define FMC2_PCR_TIMING_MASK 0xf 45*2cd457f3SChristophe Kerello #define FMC2_PMEM_PATT_TIMING_MASK 0xff 46*2cd457f3SChristophe Kerello 47*2cd457f3SChristophe Kerello /* FMC2 Controller Registers */ 48*2cd457f3SChristophe Kerello #define FMC2_BCR1 0x0 49*2cd457f3SChristophe Kerello #define FMC2_PCR 0x80 50*2cd457f3SChristophe Kerello #define FMC2_SR 0x84 51*2cd457f3SChristophe Kerello #define FMC2_PMEM 0x88 52*2cd457f3SChristophe Kerello #define FMC2_PATT 0x8c 53*2cd457f3SChristophe Kerello #define FMC2_HECCR 0x94 54*2cd457f3SChristophe Kerello #define FMC2_CSQCR 0x200 55*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1 0x204 56*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2 0x208 57*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3 0x20c 58*2cd457f3SChristophe Kerello #define FMC2_CSQAR1 0x210 59*2cd457f3SChristophe Kerello #define FMC2_CSQAR2 0x214 60*2cd457f3SChristophe Kerello #define FMC2_CSQIER 0x220 61*2cd457f3SChristophe Kerello #define FMC2_CSQISR 0x224 62*2cd457f3SChristophe Kerello #define FMC2_CSQICR 0x228 63*2cd457f3SChristophe Kerello #define FMC2_CSQEMSR 0x230 64*2cd457f3SChristophe Kerello #define FMC2_BCHIER 0x250 65*2cd457f3SChristophe Kerello #define FMC2_BCHISR 0x254 66*2cd457f3SChristophe Kerello #define FMC2_BCHICR 0x258 67*2cd457f3SChristophe Kerello #define FMC2_BCHPBR1 0x260 68*2cd457f3SChristophe Kerello #define FMC2_BCHPBR2 0x264 69*2cd457f3SChristophe Kerello #define FMC2_BCHPBR3 0x268 70*2cd457f3SChristophe Kerello #define FMC2_BCHPBR4 0x26c 71*2cd457f3SChristophe Kerello #define FMC2_BCHDSR0 0x27c 72*2cd457f3SChristophe Kerello #define FMC2_BCHDSR1 0x280 73*2cd457f3SChristophe Kerello #define FMC2_BCHDSR2 0x284 74*2cd457f3SChristophe Kerello #define FMC2_BCHDSR3 0x288 75*2cd457f3SChristophe Kerello #define FMC2_BCHDSR4 0x28c 76*2cd457f3SChristophe Kerello 77*2cd457f3SChristophe Kerello /* Register: FMC2_BCR1 */ 78*2cd457f3SChristophe Kerello #define FMC2_BCR1_FMC2EN BIT(31) 79*2cd457f3SChristophe Kerello 80*2cd457f3SChristophe Kerello /* Register: FMC2_PCR */ 81*2cd457f3SChristophe Kerello #define FMC2_PCR_PWAITEN BIT(1) 82*2cd457f3SChristophe Kerello #define FMC2_PCR_PBKEN BIT(2) 83*2cd457f3SChristophe Kerello #define FMC2_PCR_PWID_MASK GENMASK(5, 4) 84*2cd457f3SChristophe Kerello #define FMC2_PCR_PWID(x) (((x) & 0x3) << 4) 85*2cd457f3SChristophe Kerello #define FMC2_PCR_PWID_BUSWIDTH_8 0 86*2cd457f3SChristophe Kerello #define FMC2_PCR_PWID_BUSWIDTH_16 1 87*2cd457f3SChristophe Kerello #define FMC2_PCR_ECCEN BIT(6) 88*2cd457f3SChristophe Kerello #define FMC2_PCR_ECCALG BIT(8) 89*2cd457f3SChristophe Kerello #define FMC2_PCR_TCLR_MASK GENMASK(12, 9) 90*2cd457f3SChristophe Kerello #define FMC2_PCR_TCLR(x) (((x) & 0xf) << 9) 91*2cd457f3SChristophe Kerello #define FMC2_PCR_TCLR_DEFAULT 0xf 92*2cd457f3SChristophe Kerello #define FMC2_PCR_TAR_MASK GENMASK(16, 13) 93*2cd457f3SChristophe Kerello #define FMC2_PCR_TAR(x) (((x) & 0xf) << 13) 94*2cd457f3SChristophe Kerello #define FMC2_PCR_TAR_DEFAULT 0xf 95*2cd457f3SChristophe Kerello #define FMC2_PCR_ECCSS_MASK GENMASK(19, 17) 96*2cd457f3SChristophe Kerello #define FMC2_PCR_ECCSS(x) (((x) & 0x7) << 17) 97*2cd457f3SChristophe Kerello #define FMC2_PCR_ECCSS_512 1 98*2cd457f3SChristophe Kerello #define FMC2_PCR_ECCSS_2048 3 99*2cd457f3SChristophe Kerello #define FMC2_PCR_BCHECC BIT(24) 100*2cd457f3SChristophe Kerello #define FMC2_PCR_WEN BIT(25) 101*2cd457f3SChristophe Kerello 102*2cd457f3SChristophe Kerello /* Register: FMC2_SR */ 103*2cd457f3SChristophe Kerello #define FMC2_SR_NWRF BIT(6) 104*2cd457f3SChristophe Kerello 105*2cd457f3SChristophe Kerello /* Register: FMC2_PMEM */ 106*2cd457f3SChristophe Kerello #define FMC2_PMEM_MEMSET(x) (((x) & 0xff) << 0) 107*2cd457f3SChristophe Kerello #define FMC2_PMEM_MEMWAIT(x) (((x) & 0xff) << 8) 108*2cd457f3SChristophe Kerello #define FMC2_PMEM_MEMHOLD(x) (((x) & 0xff) << 16) 109*2cd457f3SChristophe Kerello #define FMC2_PMEM_MEMHIZ(x) (((x) & 0xff) << 24) 110*2cd457f3SChristophe Kerello #define FMC2_PMEM_DEFAULT 0x0a0a0a0a 111*2cd457f3SChristophe Kerello 112*2cd457f3SChristophe Kerello /* Register: FMC2_PATT */ 113*2cd457f3SChristophe Kerello #define FMC2_PATT_ATTSET(x) (((x) & 0xff) << 0) 114*2cd457f3SChristophe Kerello #define FMC2_PATT_ATTWAIT(x) (((x) & 0xff) << 8) 115*2cd457f3SChristophe Kerello #define FMC2_PATT_ATTHOLD(x) (((x) & 0xff) << 16) 116*2cd457f3SChristophe Kerello #define FMC2_PATT_ATTHIZ(x) (((x) & 0xff) << 24) 117*2cd457f3SChristophe Kerello #define FMC2_PATT_DEFAULT 0x0a0a0a0a 118*2cd457f3SChristophe Kerello 119*2cd457f3SChristophe Kerello /* Register: FMC2_CSQCR */ 120*2cd457f3SChristophe Kerello #define FMC2_CSQCR_CSQSTART BIT(0) 121*2cd457f3SChristophe Kerello 122*2cd457f3SChristophe Kerello /* Register: FMC2_CSQCFGR1 */ 123*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_CMD2EN BIT(1) 124*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_DMADEN BIT(2) 125*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_ACYNBR(x) (((x) & 0x7) << 4) 126*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_CMD1(x) (((x) & 0xff) << 8) 127*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_CMD2(x) (((x) & 0xff) << 16) 128*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_CMD1T BIT(24) 129*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR1_CMD2T BIT(25) 130*2cd457f3SChristophe Kerello 131*2cd457f3SChristophe Kerello /* Register: FMC2_CSQCFGR2 */ 132*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_SQSDTEN BIT(0) 133*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_RCMD2EN BIT(1) 134*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_DMASEN BIT(2) 135*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_RCMD1(x) (((x) & 0xff) << 8) 136*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_RCMD2(x) (((x) & 0xff) << 16) 137*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_RCMD1T BIT(24) 138*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR2_RCMD2T BIT(25) 139*2cd457f3SChristophe Kerello 140*2cd457f3SChristophe Kerello /* Register: FMC2_CSQCFGR3 */ 141*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_SNBR(x) (((x) & 0x1f) << 8) 142*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_AC1T BIT(16) 143*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_AC2T BIT(17) 144*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_AC3T BIT(18) 145*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_AC4T BIT(19) 146*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_AC5T BIT(20) 147*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_SDT BIT(21) 148*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_RAC1T BIT(22) 149*2cd457f3SChristophe Kerello #define FMC2_CSQCFGR3_RAC2T BIT(23) 150*2cd457f3SChristophe Kerello 151*2cd457f3SChristophe Kerello /* Register: FMC2_CSQCAR1 */ 152*2cd457f3SChristophe Kerello #define FMC2_CSQCAR1_ADDC1(x) (((x) & 0xff) << 0) 153*2cd457f3SChristophe Kerello #define FMC2_CSQCAR1_ADDC2(x) (((x) & 0xff) << 8) 154*2cd457f3SChristophe Kerello #define FMC2_CSQCAR1_ADDC3(x) (((x) & 0xff) << 16) 155*2cd457f3SChristophe Kerello #define FMC2_CSQCAR1_ADDC4(x) (((x) & 0xff) << 24) 156*2cd457f3SChristophe Kerello 157*2cd457f3SChristophe Kerello /* Register: FMC2_CSQCAR2 */ 158*2cd457f3SChristophe Kerello #define FMC2_CSQCAR2_ADDC5(x) (((x) & 0xff) << 0) 159*2cd457f3SChristophe Kerello #define FMC2_CSQCAR2_NANDCEN(x) (((x) & 0x3) << 10) 160*2cd457f3SChristophe Kerello #define FMC2_CSQCAR2_SAO(x) (((x) & 0xffff) << 16) 161*2cd457f3SChristophe Kerello 162*2cd457f3SChristophe Kerello /* Register: FMC2_CSQIER */ 163*2cd457f3SChristophe Kerello #define FMC2_CSQIER_TCIE BIT(0) 164*2cd457f3SChristophe Kerello 165*2cd457f3SChristophe Kerello /* Register: FMC2_CSQICR */ 166*2cd457f3SChristophe Kerello #define FMC2_CSQICR_CLEAR_IRQ GENMASK(4, 0) 167*2cd457f3SChristophe Kerello 168*2cd457f3SChristophe Kerello /* Register: FMC2_CSQEMSR */ 169*2cd457f3SChristophe Kerello #define FMC2_CSQEMSR_SEM GENMASK(15, 0) 170*2cd457f3SChristophe Kerello 171*2cd457f3SChristophe Kerello /* Register: FMC2_BCHIER */ 172*2cd457f3SChristophe Kerello #define FMC2_BCHIER_DERIE BIT(1) 173*2cd457f3SChristophe Kerello #define FMC2_BCHIER_EPBRIE BIT(4) 174*2cd457f3SChristophe Kerello 175*2cd457f3SChristophe Kerello /* Register: FMC2_BCHICR */ 176*2cd457f3SChristophe Kerello #define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0) 177*2cd457f3SChristophe Kerello 178*2cd457f3SChristophe Kerello /* Register: FMC2_BCHDSR0 */ 179*2cd457f3SChristophe Kerello #define FMC2_BCHDSR0_DUE BIT(0) 180*2cd457f3SChristophe Kerello #define FMC2_BCHDSR0_DEF BIT(1) 181*2cd457f3SChristophe Kerello #define FMC2_BCHDSR0_DEN_MASK GENMASK(7, 4) 182*2cd457f3SChristophe Kerello #define FMC2_BCHDSR0_DEN_SHIFT 4 183*2cd457f3SChristophe Kerello 184*2cd457f3SChristophe Kerello /* Register: FMC2_BCHDSR1 */ 185*2cd457f3SChristophe Kerello #define FMC2_BCHDSR1_EBP1_MASK GENMASK(12, 0) 186*2cd457f3SChristophe Kerello #define FMC2_BCHDSR1_EBP2_MASK GENMASK(28, 16) 187*2cd457f3SChristophe Kerello #define FMC2_BCHDSR1_EBP2_SHIFT 16 188*2cd457f3SChristophe Kerello 189*2cd457f3SChristophe Kerello /* Register: FMC2_BCHDSR2 */ 190*2cd457f3SChristophe Kerello #define FMC2_BCHDSR2_EBP3_MASK GENMASK(12, 0) 191*2cd457f3SChristophe Kerello #define FMC2_BCHDSR2_EBP4_MASK GENMASK(28, 16) 192*2cd457f3SChristophe Kerello #define FMC2_BCHDSR2_EBP4_SHIFT 16 193*2cd457f3SChristophe Kerello 194*2cd457f3SChristophe Kerello /* Register: FMC2_BCHDSR3 */ 195*2cd457f3SChristophe Kerello #define FMC2_BCHDSR3_EBP5_MASK GENMASK(12, 0) 196*2cd457f3SChristophe Kerello #define FMC2_BCHDSR3_EBP6_MASK GENMASK(28, 16) 197*2cd457f3SChristophe Kerello #define FMC2_BCHDSR3_EBP6_SHIFT 16 198*2cd457f3SChristophe Kerello 199*2cd457f3SChristophe Kerello /* Register: FMC2_BCHDSR4 */ 200*2cd457f3SChristophe Kerello #define FMC2_BCHDSR4_EBP7_MASK GENMASK(12, 0) 201*2cd457f3SChristophe Kerello #define FMC2_BCHDSR4_EBP8_MASK GENMASK(28, 16) 202*2cd457f3SChristophe Kerello #define FMC2_BCHDSR4_EBP8_SHIFT 16 203*2cd457f3SChristophe Kerello 204*2cd457f3SChristophe Kerello enum stm32_fmc2_ecc { 205*2cd457f3SChristophe Kerello FMC2_ECC_HAM = 1, 206*2cd457f3SChristophe Kerello FMC2_ECC_BCH4 = 4, 207*2cd457f3SChristophe Kerello FMC2_ECC_BCH8 = 8 208*2cd457f3SChristophe Kerello }; 209*2cd457f3SChristophe Kerello 210*2cd457f3SChristophe Kerello struct stm32_fmc2_timings { 211*2cd457f3SChristophe Kerello u8 tclr; 212*2cd457f3SChristophe Kerello u8 tar; 213*2cd457f3SChristophe Kerello u8 thiz; 214*2cd457f3SChristophe Kerello u8 twait; 215*2cd457f3SChristophe Kerello u8 thold_mem; 216*2cd457f3SChristophe Kerello u8 tset_mem; 217*2cd457f3SChristophe Kerello u8 thold_att; 218*2cd457f3SChristophe Kerello u8 tset_att; 219*2cd457f3SChristophe Kerello }; 220*2cd457f3SChristophe Kerello 221*2cd457f3SChristophe Kerello struct stm32_fmc2_nand { 222*2cd457f3SChristophe Kerello struct nand_chip chip; 223*2cd457f3SChristophe Kerello struct stm32_fmc2_timings timings; 224*2cd457f3SChristophe Kerello int ncs; 225*2cd457f3SChristophe Kerello int cs_used[FMC2_MAX_CE]; 226*2cd457f3SChristophe Kerello }; 227*2cd457f3SChristophe Kerello 228*2cd457f3SChristophe Kerello static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip) 229*2cd457f3SChristophe Kerello { 230*2cd457f3SChristophe Kerello return container_of(chip, struct stm32_fmc2_nand, chip); 231*2cd457f3SChristophe Kerello } 232*2cd457f3SChristophe Kerello 233*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc { 234*2cd457f3SChristophe Kerello struct nand_controller base; 235*2cd457f3SChristophe Kerello struct stm32_fmc2_nand nand; 236*2cd457f3SChristophe Kerello struct device *dev; 237*2cd457f3SChristophe Kerello void __iomem *io_base; 238*2cd457f3SChristophe Kerello void __iomem *data_base[FMC2_MAX_CE]; 239*2cd457f3SChristophe Kerello void __iomem *cmd_base[FMC2_MAX_CE]; 240*2cd457f3SChristophe Kerello void __iomem *addr_base[FMC2_MAX_CE]; 241*2cd457f3SChristophe Kerello phys_addr_t io_phys_addr; 242*2cd457f3SChristophe Kerello phys_addr_t data_phys_addr[FMC2_MAX_CE]; 243*2cd457f3SChristophe Kerello struct clk *clk; 244*2cd457f3SChristophe Kerello 245*2cd457f3SChristophe Kerello struct dma_chan *dma_tx_ch; 246*2cd457f3SChristophe Kerello struct dma_chan *dma_rx_ch; 247*2cd457f3SChristophe Kerello struct dma_chan *dma_ecc_ch; 248*2cd457f3SChristophe Kerello struct sg_table dma_data_sg; 249*2cd457f3SChristophe Kerello struct sg_table dma_ecc_sg; 250*2cd457f3SChristophe Kerello u8 *ecc_buf; 251*2cd457f3SChristophe Kerello int dma_ecc_len; 252*2cd457f3SChristophe Kerello 253*2cd457f3SChristophe Kerello struct completion complete; 254*2cd457f3SChristophe Kerello struct completion dma_data_complete; 255*2cd457f3SChristophe Kerello struct completion dma_ecc_complete; 256*2cd457f3SChristophe Kerello 257*2cd457f3SChristophe Kerello u8 cs_assigned; 258*2cd457f3SChristophe Kerello int cs_sel; 259*2cd457f3SChristophe Kerello }; 260*2cd457f3SChristophe Kerello 261*2cd457f3SChristophe Kerello static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_controller *base) 262*2cd457f3SChristophe Kerello { 263*2cd457f3SChristophe Kerello return container_of(base, struct stm32_fmc2_nfc, base); 264*2cd457f3SChristophe Kerello } 265*2cd457f3SChristophe Kerello 266*2cd457f3SChristophe Kerello /* Timings configuration */ 267*2cd457f3SChristophe Kerello static void stm32_fmc2_timings_init(struct nand_chip *chip) 268*2cd457f3SChristophe Kerello { 269*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 270*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand = to_fmc2_nand(chip); 271*2cd457f3SChristophe Kerello struct stm32_fmc2_timings *timings = &nand->timings; 272*2cd457f3SChristophe Kerello u32 pcr = readl_relaxed(fmc2->io_base + FMC2_PCR); 273*2cd457f3SChristophe Kerello u32 pmem, patt; 274*2cd457f3SChristophe Kerello 275*2cd457f3SChristophe Kerello /* Set tclr/tar timings */ 276*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_TCLR_MASK; 277*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_TCLR(timings->tclr); 278*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_TAR_MASK; 279*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_TAR(timings->tar); 280*2cd457f3SChristophe Kerello 281*2cd457f3SChristophe Kerello /* Set tset/twait/thold/thiz timings in common bank */ 282*2cd457f3SChristophe Kerello pmem = FMC2_PMEM_MEMSET(timings->tset_mem); 283*2cd457f3SChristophe Kerello pmem |= FMC2_PMEM_MEMWAIT(timings->twait); 284*2cd457f3SChristophe Kerello pmem |= FMC2_PMEM_MEMHOLD(timings->thold_mem); 285*2cd457f3SChristophe Kerello pmem |= FMC2_PMEM_MEMHIZ(timings->thiz); 286*2cd457f3SChristophe Kerello 287*2cd457f3SChristophe Kerello /* Set tset/twait/thold/thiz timings in attribut bank */ 288*2cd457f3SChristophe Kerello patt = FMC2_PATT_ATTSET(timings->tset_att); 289*2cd457f3SChristophe Kerello patt |= FMC2_PATT_ATTWAIT(timings->twait); 290*2cd457f3SChristophe Kerello patt |= FMC2_PATT_ATTHOLD(timings->thold_att); 291*2cd457f3SChristophe Kerello patt |= FMC2_PATT_ATTHIZ(timings->thiz); 292*2cd457f3SChristophe Kerello 293*2cd457f3SChristophe Kerello writel_relaxed(pcr, fmc2->io_base + FMC2_PCR); 294*2cd457f3SChristophe Kerello writel_relaxed(pmem, fmc2->io_base + FMC2_PMEM); 295*2cd457f3SChristophe Kerello writel_relaxed(patt, fmc2->io_base + FMC2_PATT); 296*2cd457f3SChristophe Kerello } 297*2cd457f3SChristophe Kerello 298*2cd457f3SChristophe Kerello /* Controller configuration */ 299*2cd457f3SChristophe Kerello static void stm32_fmc2_setup(struct nand_chip *chip) 300*2cd457f3SChristophe Kerello { 301*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 302*2cd457f3SChristophe Kerello u32 pcr = readl_relaxed(fmc2->io_base + FMC2_PCR); 303*2cd457f3SChristophe Kerello 304*2cd457f3SChristophe Kerello /* Configure ECC algorithm (default configuration is Hamming) */ 305*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_ECCALG; 306*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_BCHECC; 307*2cd457f3SChristophe Kerello if (chip->ecc.strength == FMC2_ECC_BCH8) { 308*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_ECCALG; 309*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_BCHECC; 310*2cd457f3SChristophe Kerello } else if (chip->ecc.strength == FMC2_ECC_BCH4) { 311*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_ECCALG; 312*2cd457f3SChristophe Kerello } 313*2cd457f3SChristophe Kerello 314*2cd457f3SChristophe Kerello /* Set buswidth */ 315*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_PWID_MASK; 316*2cd457f3SChristophe Kerello if (chip->options & NAND_BUSWIDTH_16) 317*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_PWID(FMC2_PCR_PWID_BUSWIDTH_16); 318*2cd457f3SChristophe Kerello 319*2cd457f3SChristophe Kerello /* Set ECC sector size */ 320*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_ECCSS_MASK; 321*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_ECCSS(FMC2_PCR_ECCSS_512); 322*2cd457f3SChristophe Kerello 323*2cd457f3SChristophe Kerello writel_relaxed(pcr, fmc2->io_base + FMC2_PCR); 324*2cd457f3SChristophe Kerello } 325*2cd457f3SChristophe Kerello 326*2cd457f3SChristophe Kerello /* Select target */ 327*2cd457f3SChristophe Kerello static int stm32_fmc2_select_chip(struct nand_chip *chip, int chipnr) 328*2cd457f3SChristophe Kerello { 329*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 330*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand = to_fmc2_nand(chip); 331*2cd457f3SChristophe Kerello struct dma_slave_config dma_cfg; 332*2cd457f3SChristophe Kerello int ret; 333*2cd457f3SChristophe Kerello 334*2cd457f3SChristophe Kerello if (nand->cs_used[chipnr] == fmc2->cs_sel) 335*2cd457f3SChristophe Kerello return 0; 336*2cd457f3SChristophe Kerello 337*2cd457f3SChristophe Kerello fmc2->cs_sel = nand->cs_used[chipnr]; 338*2cd457f3SChristophe Kerello 339*2cd457f3SChristophe Kerello /* FMC2 setup routine */ 340*2cd457f3SChristophe Kerello stm32_fmc2_setup(chip); 341*2cd457f3SChristophe Kerello 342*2cd457f3SChristophe Kerello /* Apply timings */ 343*2cd457f3SChristophe Kerello stm32_fmc2_timings_init(chip); 344*2cd457f3SChristophe Kerello 345*2cd457f3SChristophe Kerello if (fmc2->dma_tx_ch && fmc2->dma_rx_ch) { 346*2cd457f3SChristophe Kerello memset(&dma_cfg, 0, sizeof(dma_cfg)); 347*2cd457f3SChristophe Kerello dma_cfg.src_addr = fmc2->data_phys_addr[fmc2->cs_sel]; 348*2cd457f3SChristophe Kerello dma_cfg.dst_addr = fmc2->data_phys_addr[fmc2->cs_sel]; 349*2cd457f3SChristophe Kerello dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 350*2cd457f3SChristophe Kerello dma_cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 351*2cd457f3SChristophe Kerello dma_cfg.src_maxburst = 32; 352*2cd457f3SChristophe Kerello dma_cfg.dst_maxburst = 32; 353*2cd457f3SChristophe Kerello 354*2cd457f3SChristophe Kerello ret = dmaengine_slave_config(fmc2->dma_tx_ch, &dma_cfg); 355*2cd457f3SChristophe Kerello if (ret) { 356*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "tx DMA engine slave config failed\n"); 357*2cd457f3SChristophe Kerello return ret; 358*2cd457f3SChristophe Kerello } 359*2cd457f3SChristophe Kerello 360*2cd457f3SChristophe Kerello ret = dmaengine_slave_config(fmc2->dma_rx_ch, &dma_cfg); 361*2cd457f3SChristophe Kerello if (ret) { 362*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "rx DMA engine slave config failed\n"); 363*2cd457f3SChristophe Kerello return ret; 364*2cd457f3SChristophe Kerello } 365*2cd457f3SChristophe Kerello } 366*2cd457f3SChristophe Kerello 367*2cd457f3SChristophe Kerello if (fmc2->dma_ecc_ch) { 368*2cd457f3SChristophe Kerello /* 369*2cd457f3SChristophe Kerello * Hamming: we read HECCR register 370*2cd457f3SChristophe Kerello * BCH4/BCH8: we read BCHDSRSx registers 371*2cd457f3SChristophe Kerello */ 372*2cd457f3SChristophe Kerello memset(&dma_cfg, 0, sizeof(dma_cfg)); 373*2cd457f3SChristophe Kerello dma_cfg.src_addr = fmc2->io_phys_addr; 374*2cd457f3SChristophe Kerello dma_cfg.src_addr += chip->ecc.strength == FMC2_ECC_HAM ? 375*2cd457f3SChristophe Kerello FMC2_HECCR : FMC2_BCHDSR0; 376*2cd457f3SChristophe Kerello dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 377*2cd457f3SChristophe Kerello 378*2cd457f3SChristophe Kerello ret = dmaengine_slave_config(fmc2->dma_ecc_ch, &dma_cfg); 379*2cd457f3SChristophe Kerello if (ret) { 380*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "ECC DMA engine slave config failed\n"); 381*2cd457f3SChristophe Kerello return ret; 382*2cd457f3SChristophe Kerello } 383*2cd457f3SChristophe Kerello 384*2cd457f3SChristophe Kerello /* Calculate ECC length needed for one sector */ 385*2cd457f3SChristophe Kerello fmc2->dma_ecc_len = chip->ecc.strength == FMC2_ECC_HAM ? 386*2cd457f3SChristophe Kerello FMC2_HECCR_LEN : FMC2_BCHDSRS_LEN; 387*2cd457f3SChristophe Kerello } 388*2cd457f3SChristophe Kerello 389*2cd457f3SChristophe Kerello return 0; 390*2cd457f3SChristophe Kerello } 391*2cd457f3SChristophe Kerello 392*2cd457f3SChristophe Kerello /* Set bus width to 16-bit or 8-bit */ 393*2cd457f3SChristophe Kerello static void stm32_fmc2_set_buswidth_16(struct stm32_fmc2_nfc *fmc2, bool set) 394*2cd457f3SChristophe Kerello { 395*2cd457f3SChristophe Kerello u32 pcr = readl_relaxed(fmc2->io_base + FMC2_PCR); 396*2cd457f3SChristophe Kerello 397*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_PWID_MASK; 398*2cd457f3SChristophe Kerello if (set) 399*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_PWID(FMC2_PCR_PWID_BUSWIDTH_16); 400*2cd457f3SChristophe Kerello writel_relaxed(pcr, fmc2->io_base + FMC2_PCR); 401*2cd457f3SChristophe Kerello } 402*2cd457f3SChristophe Kerello 403*2cd457f3SChristophe Kerello /* Enable irq sources in case of the sequencer is used */ 404*2cd457f3SChristophe Kerello static inline void stm32_fmc2_enable_seq_irq(struct stm32_fmc2_nfc *fmc2) 405*2cd457f3SChristophe Kerello { 406*2cd457f3SChristophe Kerello u32 csqier = readl_relaxed(fmc2->io_base + FMC2_CSQIER); 407*2cd457f3SChristophe Kerello 408*2cd457f3SChristophe Kerello csqier |= FMC2_CSQIER_TCIE; 409*2cd457f3SChristophe Kerello 410*2cd457f3SChristophe Kerello writel_relaxed(csqier, fmc2->io_base + FMC2_CSQIER); 411*2cd457f3SChristophe Kerello } 412*2cd457f3SChristophe Kerello 413*2cd457f3SChristophe Kerello /* Disable irq sources in case of the sequencer is used */ 414*2cd457f3SChristophe Kerello static inline void stm32_fmc2_disable_seq_irq(struct stm32_fmc2_nfc *fmc2) 415*2cd457f3SChristophe Kerello { 416*2cd457f3SChristophe Kerello u32 csqier = readl_relaxed(fmc2->io_base + FMC2_CSQIER); 417*2cd457f3SChristophe Kerello 418*2cd457f3SChristophe Kerello csqier &= ~FMC2_CSQIER_TCIE; 419*2cd457f3SChristophe Kerello 420*2cd457f3SChristophe Kerello writel_relaxed(csqier, fmc2->io_base + FMC2_CSQIER); 421*2cd457f3SChristophe Kerello } 422*2cd457f3SChristophe Kerello 423*2cd457f3SChristophe Kerello /* Clear irq sources in case of the sequencer is used */ 424*2cd457f3SChristophe Kerello static inline void stm32_fmc2_clear_seq_irq(struct stm32_fmc2_nfc *fmc2) 425*2cd457f3SChristophe Kerello { 426*2cd457f3SChristophe Kerello writel_relaxed(FMC2_CSQICR_CLEAR_IRQ, fmc2->io_base + FMC2_CSQICR); 427*2cd457f3SChristophe Kerello } 428*2cd457f3SChristophe Kerello 429*2cd457f3SChristophe Kerello /* 430*2cd457f3SChristophe Kerello * ECC Hamming calculation 431*2cd457f3SChristophe Kerello * ECC is 3 bytes for 512 bytes of data (supports error correction up to 432*2cd457f3SChristophe Kerello * max of 1-bit) 433*2cd457f3SChristophe Kerello */ 434*2cd457f3SChristophe Kerello static inline void stm32_fmc2_ham_set_ecc(const u32 ecc_sta, u8 *ecc) 435*2cd457f3SChristophe Kerello { 436*2cd457f3SChristophe Kerello ecc[0] = ecc_sta; 437*2cd457f3SChristophe Kerello ecc[1] = ecc_sta >> 8; 438*2cd457f3SChristophe Kerello ecc[2] = ecc_sta >> 16; 439*2cd457f3SChristophe Kerello } 440*2cd457f3SChristophe Kerello 441*2cd457f3SChristophe Kerello static int stm32_fmc2_ham_correct(struct nand_chip *chip, u8 *dat, 442*2cd457f3SChristophe Kerello u8 *read_ecc, u8 *calc_ecc) 443*2cd457f3SChristophe Kerello { 444*2cd457f3SChristophe Kerello u8 bit_position = 0, b0, b1, b2; 445*2cd457f3SChristophe Kerello u32 byte_addr = 0, b; 446*2cd457f3SChristophe Kerello u32 i, shifting = 1; 447*2cd457f3SChristophe Kerello 448*2cd457f3SChristophe Kerello /* Indicate which bit and byte is faulty (if any) */ 449*2cd457f3SChristophe Kerello b0 = read_ecc[0] ^ calc_ecc[0]; 450*2cd457f3SChristophe Kerello b1 = read_ecc[1] ^ calc_ecc[1]; 451*2cd457f3SChristophe Kerello b2 = read_ecc[2] ^ calc_ecc[2]; 452*2cd457f3SChristophe Kerello b = b0 | (b1 << 8) | (b2 << 16); 453*2cd457f3SChristophe Kerello 454*2cd457f3SChristophe Kerello /* No errors */ 455*2cd457f3SChristophe Kerello if (likely(!b)) 456*2cd457f3SChristophe Kerello return 0; 457*2cd457f3SChristophe Kerello 458*2cd457f3SChristophe Kerello /* Calculate bit position */ 459*2cd457f3SChristophe Kerello for (i = 0; i < 3; i++) { 460*2cd457f3SChristophe Kerello switch (b % 4) { 461*2cd457f3SChristophe Kerello case 2: 462*2cd457f3SChristophe Kerello bit_position += shifting; 463*2cd457f3SChristophe Kerello case 1: 464*2cd457f3SChristophe Kerello break; 465*2cd457f3SChristophe Kerello default: 466*2cd457f3SChristophe Kerello return -EBADMSG; 467*2cd457f3SChristophe Kerello } 468*2cd457f3SChristophe Kerello shifting <<= 1; 469*2cd457f3SChristophe Kerello b >>= 2; 470*2cd457f3SChristophe Kerello } 471*2cd457f3SChristophe Kerello 472*2cd457f3SChristophe Kerello /* Calculate byte position */ 473*2cd457f3SChristophe Kerello shifting = 1; 474*2cd457f3SChristophe Kerello for (i = 0; i < 9; i++) { 475*2cd457f3SChristophe Kerello switch (b % 4) { 476*2cd457f3SChristophe Kerello case 2: 477*2cd457f3SChristophe Kerello byte_addr += shifting; 478*2cd457f3SChristophe Kerello case 1: 479*2cd457f3SChristophe Kerello break; 480*2cd457f3SChristophe Kerello default: 481*2cd457f3SChristophe Kerello return -EBADMSG; 482*2cd457f3SChristophe Kerello } 483*2cd457f3SChristophe Kerello shifting <<= 1; 484*2cd457f3SChristophe Kerello b >>= 2; 485*2cd457f3SChristophe Kerello } 486*2cd457f3SChristophe Kerello 487*2cd457f3SChristophe Kerello /* Flip the bit */ 488*2cd457f3SChristophe Kerello dat[byte_addr] ^= (1 << bit_position); 489*2cd457f3SChristophe Kerello 490*2cd457f3SChristophe Kerello return 1; 491*2cd457f3SChristophe Kerello } 492*2cd457f3SChristophe Kerello 493*2cd457f3SChristophe Kerello /* BCH algorithm correction */ 494*2cd457f3SChristophe Kerello static int stm32_fmc2_bch_decode(int eccsize, u8 *dat, u32 *ecc_sta) 495*2cd457f3SChristophe Kerello { 496*2cd457f3SChristophe Kerello u32 bchdsr0 = ecc_sta[0]; 497*2cd457f3SChristophe Kerello u32 bchdsr1 = ecc_sta[1]; 498*2cd457f3SChristophe Kerello u32 bchdsr2 = ecc_sta[2]; 499*2cd457f3SChristophe Kerello u32 bchdsr3 = ecc_sta[3]; 500*2cd457f3SChristophe Kerello u32 bchdsr4 = ecc_sta[4]; 501*2cd457f3SChristophe Kerello u16 pos[8]; 502*2cd457f3SChristophe Kerello int i, den; 503*2cd457f3SChristophe Kerello unsigned int nb_errs = 0; 504*2cd457f3SChristophe Kerello 505*2cd457f3SChristophe Kerello /* No errors found */ 506*2cd457f3SChristophe Kerello if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF))) 507*2cd457f3SChristophe Kerello return 0; 508*2cd457f3SChristophe Kerello 509*2cd457f3SChristophe Kerello /* Too many errors detected */ 510*2cd457f3SChristophe Kerello if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE)) 511*2cd457f3SChristophe Kerello return -EBADMSG; 512*2cd457f3SChristophe Kerello 513*2cd457f3SChristophe Kerello pos[0] = bchdsr1 & FMC2_BCHDSR1_EBP1_MASK; 514*2cd457f3SChristophe Kerello pos[1] = (bchdsr1 & FMC2_BCHDSR1_EBP2_MASK) >> FMC2_BCHDSR1_EBP2_SHIFT; 515*2cd457f3SChristophe Kerello pos[2] = bchdsr2 & FMC2_BCHDSR2_EBP3_MASK; 516*2cd457f3SChristophe Kerello pos[3] = (bchdsr2 & FMC2_BCHDSR2_EBP4_MASK) >> FMC2_BCHDSR2_EBP4_SHIFT; 517*2cd457f3SChristophe Kerello pos[4] = bchdsr3 & FMC2_BCHDSR3_EBP5_MASK; 518*2cd457f3SChristophe Kerello pos[5] = (bchdsr3 & FMC2_BCHDSR3_EBP6_MASK) >> FMC2_BCHDSR3_EBP6_SHIFT; 519*2cd457f3SChristophe Kerello pos[6] = bchdsr4 & FMC2_BCHDSR4_EBP7_MASK; 520*2cd457f3SChristophe Kerello pos[7] = (bchdsr4 & FMC2_BCHDSR4_EBP8_MASK) >> FMC2_BCHDSR4_EBP8_SHIFT; 521*2cd457f3SChristophe Kerello 522*2cd457f3SChristophe Kerello den = (bchdsr0 & FMC2_BCHDSR0_DEN_MASK) >> FMC2_BCHDSR0_DEN_SHIFT; 523*2cd457f3SChristophe Kerello for (i = 0; i < den; i++) { 524*2cd457f3SChristophe Kerello if (pos[i] < eccsize * 8) { 525*2cd457f3SChristophe Kerello change_bit(pos[i], (unsigned long *)dat); 526*2cd457f3SChristophe Kerello nb_errs++; 527*2cd457f3SChristophe Kerello } 528*2cd457f3SChristophe Kerello } 529*2cd457f3SChristophe Kerello 530*2cd457f3SChristophe Kerello return nb_errs; 531*2cd457f3SChristophe Kerello } 532*2cd457f3SChristophe Kerello 533*2cd457f3SChristophe Kerello /* Sequencer read/write configuration */ 534*2cd457f3SChristophe Kerello static void stm32_fmc2_rw_page_init(struct nand_chip *chip, int page, 535*2cd457f3SChristophe Kerello int raw, bool write_data) 536*2cd457f3SChristophe Kerello { 537*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 538*2cd457f3SChristophe Kerello struct mtd_info *mtd = nand_to_mtd(chip); 539*2cd457f3SChristophe Kerello u32 csqcfgr1, csqcfgr2, csqcfgr3; 540*2cd457f3SChristophe Kerello u32 csqar1, csqar2; 541*2cd457f3SChristophe Kerello u32 ecc_offset = mtd->writesize + FMC2_BBM_LEN; 542*2cd457f3SChristophe Kerello u32 pcr = readl_relaxed(fmc2->io_base + FMC2_PCR); 543*2cd457f3SChristophe Kerello 544*2cd457f3SChristophe Kerello if (write_data) 545*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_WEN; 546*2cd457f3SChristophe Kerello else 547*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_WEN; 548*2cd457f3SChristophe Kerello writel_relaxed(pcr, fmc2->io_base + FMC2_PCR); 549*2cd457f3SChristophe Kerello 550*2cd457f3SChristophe Kerello /* 551*2cd457f3SChristophe Kerello * - Set Program Page/Page Read command 552*2cd457f3SChristophe Kerello * - Enable DMA request data 553*2cd457f3SChristophe Kerello * - Set timings 554*2cd457f3SChristophe Kerello */ 555*2cd457f3SChristophe Kerello csqcfgr1 = FMC2_CSQCFGR1_DMADEN | FMC2_CSQCFGR1_CMD1T; 556*2cd457f3SChristophe Kerello if (write_data) 557*2cd457f3SChristophe Kerello csqcfgr1 |= FMC2_CSQCFGR1_CMD1(NAND_CMD_SEQIN); 558*2cd457f3SChristophe Kerello else 559*2cd457f3SChristophe Kerello csqcfgr1 |= FMC2_CSQCFGR1_CMD1(NAND_CMD_READ0) | 560*2cd457f3SChristophe Kerello FMC2_CSQCFGR1_CMD2EN | 561*2cd457f3SChristophe Kerello FMC2_CSQCFGR1_CMD2(NAND_CMD_READSTART) | 562*2cd457f3SChristophe Kerello FMC2_CSQCFGR1_CMD2T; 563*2cd457f3SChristophe Kerello 564*2cd457f3SChristophe Kerello /* 565*2cd457f3SChristophe Kerello * - Set Random Data Input/Random Data Read command 566*2cd457f3SChristophe Kerello * - Enable the sequencer to access the Spare data area 567*2cd457f3SChristophe Kerello * - Enable DMA request status decoding for read 568*2cd457f3SChristophe Kerello * - Set timings 569*2cd457f3SChristophe Kerello */ 570*2cd457f3SChristophe Kerello if (write_data) 571*2cd457f3SChristophe Kerello csqcfgr2 = FMC2_CSQCFGR2_RCMD1(NAND_CMD_RNDIN); 572*2cd457f3SChristophe Kerello else 573*2cd457f3SChristophe Kerello csqcfgr2 = FMC2_CSQCFGR2_RCMD1(NAND_CMD_RNDOUT) | 574*2cd457f3SChristophe Kerello FMC2_CSQCFGR2_RCMD2EN | 575*2cd457f3SChristophe Kerello FMC2_CSQCFGR2_RCMD2(NAND_CMD_RNDOUTSTART) | 576*2cd457f3SChristophe Kerello FMC2_CSQCFGR2_RCMD1T | 577*2cd457f3SChristophe Kerello FMC2_CSQCFGR2_RCMD2T; 578*2cd457f3SChristophe Kerello if (!raw) { 579*2cd457f3SChristophe Kerello csqcfgr2 |= write_data ? 0 : FMC2_CSQCFGR2_DMASEN; 580*2cd457f3SChristophe Kerello csqcfgr2 |= FMC2_CSQCFGR2_SQSDTEN; 581*2cd457f3SChristophe Kerello } 582*2cd457f3SChristophe Kerello 583*2cd457f3SChristophe Kerello /* 584*2cd457f3SChristophe Kerello * - Set the number of sectors to be written 585*2cd457f3SChristophe Kerello * - Set timings 586*2cd457f3SChristophe Kerello */ 587*2cd457f3SChristophe Kerello csqcfgr3 = FMC2_CSQCFGR3_SNBR(chip->ecc.steps - 1); 588*2cd457f3SChristophe Kerello if (write_data) { 589*2cd457f3SChristophe Kerello csqcfgr3 |= FMC2_CSQCFGR3_RAC2T; 590*2cd457f3SChristophe Kerello if (chip->options & NAND_ROW_ADDR_3) 591*2cd457f3SChristophe Kerello csqcfgr3 |= FMC2_CSQCFGR3_AC5T; 592*2cd457f3SChristophe Kerello else 593*2cd457f3SChristophe Kerello csqcfgr3 |= FMC2_CSQCFGR3_AC4T; 594*2cd457f3SChristophe Kerello } 595*2cd457f3SChristophe Kerello 596*2cd457f3SChristophe Kerello /* 597*2cd457f3SChristophe Kerello * Set the fourth first address cycles 598*2cd457f3SChristophe Kerello * Byte 1 and byte 2 => column, we start at 0x0 599*2cd457f3SChristophe Kerello * Byte 3 and byte 4 => page 600*2cd457f3SChristophe Kerello */ 601*2cd457f3SChristophe Kerello csqar1 = FMC2_CSQCAR1_ADDC3(page); 602*2cd457f3SChristophe Kerello csqar1 |= FMC2_CSQCAR1_ADDC4(page >> 8); 603*2cd457f3SChristophe Kerello 604*2cd457f3SChristophe Kerello /* 605*2cd457f3SChristophe Kerello * - Set chip enable number 606*2cd457f3SChristophe Kerello * - Set ECC byte offset in the spare area 607*2cd457f3SChristophe Kerello * - Calculate the number of address cycles to be issued 608*2cd457f3SChristophe Kerello * - Set byte 5 of address cycle if needed 609*2cd457f3SChristophe Kerello */ 610*2cd457f3SChristophe Kerello csqar2 = FMC2_CSQCAR2_NANDCEN(fmc2->cs_sel); 611*2cd457f3SChristophe Kerello if (chip->options & NAND_BUSWIDTH_16) 612*2cd457f3SChristophe Kerello csqar2 |= FMC2_CSQCAR2_SAO(ecc_offset >> 1); 613*2cd457f3SChristophe Kerello else 614*2cd457f3SChristophe Kerello csqar2 |= FMC2_CSQCAR2_SAO(ecc_offset); 615*2cd457f3SChristophe Kerello if (chip->options & NAND_ROW_ADDR_3) { 616*2cd457f3SChristophe Kerello csqcfgr1 |= FMC2_CSQCFGR1_ACYNBR(5); 617*2cd457f3SChristophe Kerello csqar2 |= FMC2_CSQCAR2_ADDC5(page >> 16); 618*2cd457f3SChristophe Kerello } else { 619*2cd457f3SChristophe Kerello csqcfgr1 |= FMC2_CSQCFGR1_ACYNBR(4); 620*2cd457f3SChristophe Kerello } 621*2cd457f3SChristophe Kerello 622*2cd457f3SChristophe Kerello writel_relaxed(csqcfgr1, fmc2->io_base + FMC2_CSQCFGR1); 623*2cd457f3SChristophe Kerello writel_relaxed(csqcfgr2, fmc2->io_base + FMC2_CSQCFGR2); 624*2cd457f3SChristophe Kerello writel_relaxed(csqcfgr3, fmc2->io_base + FMC2_CSQCFGR3); 625*2cd457f3SChristophe Kerello writel_relaxed(csqar1, fmc2->io_base + FMC2_CSQAR1); 626*2cd457f3SChristophe Kerello writel_relaxed(csqar2, fmc2->io_base + FMC2_CSQAR2); 627*2cd457f3SChristophe Kerello } 628*2cd457f3SChristophe Kerello 629*2cd457f3SChristophe Kerello static void stm32_fmc2_dma_callback(void *arg) 630*2cd457f3SChristophe Kerello { 631*2cd457f3SChristophe Kerello complete((struct completion *)arg); 632*2cd457f3SChristophe Kerello } 633*2cd457f3SChristophe Kerello 634*2cd457f3SChristophe Kerello /* Read/write data from/to a page */ 635*2cd457f3SChristophe Kerello static int stm32_fmc2_xfer(struct nand_chip *chip, const u8 *buf, 636*2cd457f3SChristophe Kerello int raw, bool write_data) 637*2cd457f3SChristophe Kerello { 638*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 639*2cd457f3SChristophe Kerello struct dma_async_tx_descriptor *desc_data, *desc_ecc; 640*2cd457f3SChristophe Kerello struct scatterlist *sg; 641*2cd457f3SChristophe Kerello struct dma_chan *dma_ch = fmc2->dma_rx_ch; 642*2cd457f3SChristophe Kerello enum dma_data_direction dma_data_dir = DMA_FROM_DEVICE; 643*2cd457f3SChristophe Kerello enum dma_transfer_direction dma_transfer_dir = DMA_DEV_TO_MEM; 644*2cd457f3SChristophe Kerello u32 csqcr = readl_relaxed(fmc2->io_base + FMC2_CSQCR); 645*2cd457f3SChristophe Kerello int eccsteps = chip->ecc.steps; 646*2cd457f3SChristophe Kerello int eccsize = chip->ecc.size; 647*2cd457f3SChristophe Kerello const u8 *p = buf; 648*2cd457f3SChristophe Kerello int s, ret; 649*2cd457f3SChristophe Kerello 650*2cd457f3SChristophe Kerello /* Configure DMA data */ 651*2cd457f3SChristophe Kerello if (write_data) { 652*2cd457f3SChristophe Kerello dma_data_dir = DMA_TO_DEVICE; 653*2cd457f3SChristophe Kerello dma_transfer_dir = DMA_MEM_TO_DEV; 654*2cd457f3SChristophe Kerello dma_ch = fmc2->dma_tx_ch; 655*2cd457f3SChristophe Kerello } 656*2cd457f3SChristophe Kerello 657*2cd457f3SChristophe Kerello for_each_sg(fmc2->dma_data_sg.sgl, sg, eccsteps, s) { 658*2cd457f3SChristophe Kerello sg_set_buf(sg, p, eccsize); 659*2cd457f3SChristophe Kerello p += eccsize; 660*2cd457f3SChristophe Kerello } 661*2cd457f3SChristophe Kerello 662*2cd457f3SChristophe Kerello ret = dma_map_sg(fmc2->dev, fmc2->dma_data_sg.sgl, 663*2cd457f3SChristophe Kerello eccsteps, dma_data_dir); 664*2cd457f3SChristophe Kerello if (ret < 0) 665*2cd457f3SChristophe Kerello return ret; 666*2cd457f3SChristophe Kerello 667*2cd457f3SChristophe Kerello desc_data = dmaengine_prep_slave_sg(dma_ch, fmc2->dma_data_sg.sgl, 668*2cd457f3SChristophe Kerello eccsteps, dma_transfer_dir, 669*2cd457f3SChristophe Kerello DMA_PREP_INTERRUPT); 670*2cd457f3SChristophe Kerello if (!desc_data) { 671*2cd457f3SChristophe Kerello ret = -ENOMEM; 672*2cd457f3SChristophe Kerello goto err_unmap_data; 673*2cd457f3SChristophe Kerello } 674*2cd457f3SChristophe Kerello 675*2cd457f3SChristophe Kerello reinit_completion(&fmc2->dma_data_complete); 676*2cd457f3SChristophe Kerello reinit_completion(&fmc2->complete); 677*2cd457f3SChristophe Kerello desc_data->callback = stm32_fmc2_dma_callback; 678*2cd457f3SChristophe Kerello desc_data->callback_param = &fmc2->dma_data_complete; 679*2cd457f3SChristophe Kerello ret = dma_submit_error(dmaengine_submit(desc_data)); 680*2cd457f3SChristophe Kerello if (ret) 681*2cd457f3SChristophe Kerello goto err_unmap_data; 682*2cd457f3SChristophe Kerello 683*2cd457f3SChristophe Kerello dma_async_issue_pending(dma_ch); 684*2cd457f3SChristophe Kerello 685*2cd457f3SChristophe Kerello if (!write_data && !raw) { 686*2cd457f3SChristophe Kerello /* Configure DMA ECC status */ 687*2cd457f3SChristophe Kerello p = fmc2->ecc_buf; 688*2cd457f3SChristophe Kerello for_each_sg(fmc2->dma_ecc_sg.sgl, sg, eccsteps, s) { 689*2cd457f3SChristophe Kerello sg_set_buf(sg, p, fmc2->dma_ecc_len); 690*2cd457f3SChristophe Kerello p += fmc2->dma_ecc_len; 691*2cd457f3SChristophe Kerello } 692*2cd457f3SChristophe Kerello 693*2cd457f3SChristophe Kerello ret = dma_map_sg(fmc2->dev, fmc2->dma_ecc_sg.sgl, 694*2cd457f3SChristophe Kerello eccsteps, dma_data_dir); 695*2cd457f3SChristophe Kerello if (ret < 0) 696*2cd457f3SChristophe Kerello goto err_unmap_data; 697*2cd457f3SChristophe Kerello 698*2cd457f3SChristophe Kerello desc_ecc = dmaengine_prep_slave_sg(fmc2->dma_ecc_ch, 699*2cd457f3SChristophe Kerello fmc2->dma_ecc_sg.sgl, 700*2cd457f3SChristophe Kerello eccsteps, dma_transfer_dir, 701*2cd457f3SChristophe Kerello DMA_PREP_INTERRUPT); 702*2cd457f3SChristophe Kerello if (!desc_ecc) { 703*2cd457f3SChristophe Kerello ret = -ENOMEM; 704*2cd457f3SChristophe Kerello goto err_unmap_ecc; 705*2cd457f3SChristophe Kerello } 706*2cd457f3SChristophe Kerello 707*2cd457f3SChristophe Kerello reinit_completion(&fmc2->dma_ecc_complete); 708*2cd457f3SChristophe Kerello desc_ecc->callback = stm32_fmc2_dma_callback; 709*2cd457f3SChristophe Kerello desc_ecc->callback_param = &fmc2->dma_ecc_complete; 710*2cd457f3SChristophe Kerello ret = dma_submit_error(dmaengine_submit(desc_ecc)); 711*2cd457f3SChristophe Kerello if (ret) 712*2cd457f3SChristophe Kerello goto err_unmap_ecc; 713*2cd457f3SChristophe Kerello 714*2cd457f3SChristophe Kerello dma_async_issue_pending(fmc2->dma_ecc_ch); 715*2cd457f3SChristophe Kerello } 716*2cd457f3SChristophe Kerello 717*2cd457f3SChristophe Kerello stm32_fmc2_clear_seq_irq(fmc2); 718*2cd457f3SChristophe Kerello stm32_fmc2_enable_seq_irq(fmc2); 719*2cd457f3SChristophe Kerello 720*2cd457f3SChristophe Kerello /* Start the transfer */ 721*2cd457f3SChristophe Kerello csqcr |= FMC2_CSQCR_CSQSTART; 722*2cd457f3SChristophe Kerello writel_relaxed(csqcr, fmc2->io_base + FMC2_CSQCR); 723*2cd457f3SChristophe Kerello 724*2cd457f3SChristophe Kerello /* Wait end of sequencer transfer */ 725*2cd457f3SChristophe Kerello if (!wait_for_completion_timeout(&fmc2->complete, 726*2cd457f3SChristophe Kerello msecs_to_jiffies(1000))) { 727*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "seq timeout\n"); 728*2cd457f3SChristophe Kerello stm32_fmc2_disable_seq_irq(fmc2); 729*2cd457f3SChristophe Kerello dmaengine_terminate_all(dma_ch); 730*2cd457f3SChristophe Kerello if (!write_data && !raw) 731*2cd457f3SChristophe Kerello dmaengine_terminate_all(fmc2->dma_ecc_ch); 732*2cd457f3SChristophe Kerello ret = -ETIMEDOUT; 733*2cd457f3SChristophe Kerello goto err_unmap_ecc; 734*2cd457f3SChristophe Kerello } 735*2cd457f3SChristophe Kerello 736*2cd457f3SChristophe Kerello /* Wait DMA data transfer completion */ 737*2cd457f3SChristophe Kerello if (!wait_for_completion_timeout(&fmc2->dma_data_complete, 738*2cd457f3SChristophe Kerello msecs_to_jiffies(100))) { 739*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "data DMA timeout\n"); 740*2cd457f3SChristophe Kerello dmaengine_terminate_all(dma_ch); 741*2cd457f3SChristophe Kerello ret = -ETIMEDOUT; 742*2cd457f3SChristophe Kerello } 743*2cd457f3SChristophe Kerello 744*2cd457f3SChristophe Kerello /* Wait DMA ECC transfer completion */ 745*2cd457f3SChristophe Kerello if (!write_data && !raw) { 746*2cd457f3SChristophe Kerello if (!wait_for_completion_timeout(&fmc2->dma_ecc_complete, 747*2cd457f3SChristophe Kerello msecs_to_jiffies(100))) { 748*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "ECC DMA timeout\n"); 749*2cd457f3SChristophe Kerello dmaengine_terminate_all(fmc2->dma_ecc_ch); 750*2cd457f3SChristophe Kerello ret = -ETIMEDOUT; 751*2cd457f3SChristophe Kerello } 752*2cd457f3SChristophe Kerello } 753*2cd457f3SChristophe Kerello 754*2cd457f3SChristophe Kerello err_unmap_ecc: 755*2cd457f3SChristophe Kerello if (!write_data && !raw) 756*2cd457f3SChristophe Kerello dma_unmap_sg(fmc2->dev, fmc2->dma_ecc_sg.sgl, 757*2cd457f3SChristophe Kerello eccsteps, dma_data_dir); 758*2cd457f3SChristophe Kerello 759*2cd457f3SChristophe Kerello err_unmap_data: 760*2cd457f3SChristophe Kerello dma_unmap_sg(fmc2->dev, fmc2->dma_data_sg.sgl, eccsteps, dma_data_dir); 761*2cd457f3SChristophe Kerello 762*2cd457f3SChristophe Kerello return ret; 763*2cd457f3SChristophe Kerello } 764*2cd457f3SChristophe Kerello 765*2cd457f3SChristophe Kerello static int stm32_fmc2_sequencer_write(struct nand_chip *chip, 766*2cd457f3SChristophe Kerello const u8 *buf, int oob_required, 767*2cd457f3SChristophe Kerello int page, int raw) 768*2cd457f3SChristophe Kerello { 769*2cd457f3SChristophe Kerello struct mtd_info *mtd = nand_to_mtd(chip); 770*2cd457f3SChristophe Kerello int ret; 771*2cd457f3SChristophe Kerello 772*2cd457f3SChristophe Kerello /* Configure the sequencer */ 773*2cd457f3SChristophe Kerello stm32_fmc2_rw_page_init(chip, page, raw, true); 774*2cd457f3SChristophe Kerello 775*2cd457f3SChristophe Kerello /* Write the page */ 776*2cd457f3SChristophe Kerello ret = stm32_fmc2_xfer(chip, buf, raw, true); 777*2cd457f3SChristophe Kerello if (ret) 778*2cd457f3SChristophe Kerello return ret; 779*2cd457f3SChristophe Kerello 780*2cd457f3SChristophe Kerello /* Write oob */ 781*2cd457f3SChristophe Kerello if (oob_required) { 782*2cd457f3SChristophe Kerello ret = nand_change_write_column_op(chip, mtd->writesize, 783*2cd457f3SChristophe Kerello chip->oob_poi, mtd->oobsize, 784*2cd457f3SChristophe Kerello false); 785*2cd457f3SChristophe Kerello if (ret) 786*2cd457f3SChristophe Kerello return ret; 787*2cd457f3SChristophe Kerello } 788*2cd457f3SChristophe Kerello 789*2cd457f3SChristophe Kerello return nand_prog_page_end_op(chip); 790*2cd457f3SChristophe Kerello } 791*2cd457f3SChristophe Kerello 792*2cd457f3SChristophe Kerello static int stm32_fmc2_sequencer_write_page(struct nand_chip *chip, 793*2cd457f3SChristophe Kerello const u8 *buf, 794*2cd457f3SChristophe Kerello int oob_required, 795*2cd457f3SChristophe Kerello int page) 796*2cd457f3SChristophe Kerello { 797*2cd457f3SChristophe Kerello int ret; 798*2cd457f3SChristophe Kerello 799*2cd457f3SChristophe Kerello /* Select the target */ 800*2cd457f3SChristophe Kerello ret = stm32_fmc2_select_chip(chip, chip->cur_cs); 801*2cd457f3SChristophe Kerello if (ret) 802*2cd457f3SChristophe Kerello return ret; 803*2cd457f3SChristophe Kerello 804*2cd457f3SChristophe Kerello return stm32_fmc2_sequencer_write(chip, buf, oob_required, page, false); 805*2cd457f3SChristophe Kerello } 806*2cd457f3SChristophe Kerello 807*2cd457f3SChristophe Kerello static int stm32_fmc2_sequencer_write_page_raw(struct nand_chip *chip, 808*2cd457f3SChristophe Kerello const u8 *buf, 809*2cd457f3SChristophe Kerello int oob_required, 810*2cd457f3SChristophe Kerello int page) 811*2cd457f3SChristophe Kerello { 812*2cd457f3SChristophe Kerello int ret; 813*2cd457f3SChristophe Kerello 814*2cd457f3SChristophe Kerello /* Select the target */ 815*2cd457f3SChristophe Kerello ret = stm32_fmc2_select_chip(chip, chip->cur_cs); 816*2cd457f3SChristophe Kerello if (ret) 817*2cd457f3SChristophe Kerello return ret; 818*2cd457f3SChristophe Kerello 819*2cd457f3SChristophe Kerello return stm32_fmc2_sequencer_write(chip, buf, oob_required, page, true); 820*2cd457f3SChristophe Kerello } 821*2cd457f3SChristophe Kerello 822*2cd457f3SChristophe Kerello /* Get a status indicating which sectors have errors */ 823*2cd457f3SChristophe Kerello static inline u16 stm32_fmc2_get_mapping_status(struct stm32_fmc2_nfc *fmc2) 824*2cd457f3SChristophe Kerello { 825*2cd457f3SChristophe Kerello u32 csqemsr = readl_relaxed(fmc2->io_base + FMC2_CSQEMSR); 826*2cd457f3SChristophe Kerello 827*2cd457f3SChristophe Kerello return csqemsr & FMC2_CSQEMSR_SEM; 828*2cd457f3SChristophe Kerello } 829*2cd457f3SChristophe Kerello 830*2cd457f3SChristophe Kerello static int stm32_fmc2_sequencer_correct(struct nand_chip *chip, u8 *dat, 831*2cd457f3SChristophe Kerello u8 *read_ecc, u8 *calc_ecc) 832*2cd457f3SChristophe Kerello { 833*2cd457f3SChristophe Kerello struct mtd_info *mtd = nand_to_mtd(chip); 834*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 835*2cd457f3SChristophe Kerello int eccbytes = chip->ecc.bytes; 836*2cd457f3SChristophe Kerello int eccsteps = chip->ecc.steps; 837*2cd457f3SChristophe Kerello int eccstrength = chip->ecc.strength; 838*2cd457f3SChristophe Kerello int i, s, eccsize = chip->ecc.size; 839*2cd457f3SChristophe Kerello u32 *ecc_sta = (u32 *)fmc2->ecc_buf; 840*2cd457f3SChristophe Kerello u16 sta_map = stm32_fmc2_get_mapping_status(fmc2); 841*2cd457f3SChristophe Kerello unsigned int max_bitflips = 0; 842*2cd457f3SChristophe Kerello 843*2cd457f3SChristophe Kerello for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, dat += eccsize) { 844*2cd457f3SChristophe Kerello int stat = 0; 845*2cd457f3SChristophe Kerello 846*2cd457f3SChristophe Kerello if (eccstrength == FMC2_ECC_HAM) { 847*2cd457f3SChristophe Kerello /* Ecc_sta = FMC2_HECCR */ 848*2cd457f3SChristophe Kerello if (sta_map & BIT(s)) { 849*2cd457f3SChristophe Kerello stm32_fmc2_ham_set_ecc(*ecc_sta, &calc_ecc[i]); 850*2cd457f3SChristophe Kerello stat = stm32_fmc2_ham_correct(chip, dat, 851*2cd457f3SChristophe Kerello &read_ecc[i], 852*2cd457f3SChristophe Kerello &calc_ecc[i]); 853*2cd457f3SChristophe Kerello } 854*2cd457f3SChristophe Kerello ecc_sta++; 855*2cd457f3SChristophe Kerello } else { 856*2cd457f3SChristophe Kerello /* 857*2cd457f3SChristophe Kerello * Ecc_sta[0] = FMC2_BCHDSR0 858*2cd457f3SChristophe Kerello * Ecc_sta[1] = FMC2_BCHDSR1 859*2cd457f3SChristophe Kerello * Ecc_sta[2] = FMC2_BCHDSR2 860*2cd457f3SChristophe Kerello * Ecc_sta[3] = FMC2_BCHDSR3 861*2cd457f3SChristophe Kerello * Ecc_sta[4] = FMC2_BCHDSR4 862*2cd457f3SChristophe Kerello */ 863*2cd457f3SChristophe Kerello if (sta_map & BIT(s)) 864*2cd457f3SChristophe Kerello stat = stm32_fmc2_bch_decode(eccsize, dat, 865*2cd457f3SChristophe Kerello ecc_sta); 866*2cd457f3SChristophe Kerello ecc_sta += 5; 867*2cd457f3SChristophe Kerello } 868*2cd457f3SChristophe Kerello 869*2cd457f3SChristophe Kerello if (stat == -EBADMSG) 870*2cd457f3SChristophe Kerello /* Check for empty pages with bitflips */ 871*2cd457f3SChristophe Kerello stat = nand_check_erased_ecc_chunk(dat, eccsize, 872*2cd457f3SChristophe Kerello &read_ecc[i], 873*2cd457f3SChristophe Kerello eccbytes, 874*2cd457f3SChristophe Kerello NULL, 0, 875*2cd457f3SChristophe Kerello eccstrength); 876*2cd457f3SChristophe Kerello 877*2cd457f3SChristophe Kerello if (stat < 0) { 878*2cd457f3SChristophe Kerello mtd->ecc_stats.failed++; 879*2cd457f3SChristophe Kerello } else { 880*2cd457f3SChristophe Kerello mtd->ecc_stats.corrected += stat; 881*2cd457f3SChristophe Kerello max_bitflips = max_t(unsigned int, max_bitflips, stat); 882*2cd457f3SChristophe Kerello } 883*2cd457f3SChristophe Kerello } 884*2cd457f3SChristophe Kerello 885*2cd457f3SChristophe Kerello return max_bitflips; 886*2cd457f3SChristophe Kerello } 887*2cd457f3SChristophe Kerello 888*2cd457f3SChristophe Kerello static int stm32_fmc2_sequencer_read_page(struct nand_chip *chip, u8 *buf, 889*2cd457f3SChristophe Kerello int oob_required, int page) 890*2cd457f3SChristophe Kerello { 891*2cd457f3SChristophe Kerello struct mtd_info *mtd = nand_to_mtd(chip); 892*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 893*2cd457f3SChristophe Kerello u8 *ecc_calc = chip->ecc.calc_buf; 894*2cd457f3SChristophe Kerello u8 *ecc_code = chip->ecc.code_buf; 895*2cd457f3SChristophe Kerello u16 sta_map; 896*2cd457f3SChristophe Kerello int ret; 897*2cd457f3SChristophe Kerello 898*2cd457f3SChristophe Kerello /* Select the target */ 899*2cd457f3SChristophe Kerello ret = stm32_fmc2_select_chip(chip, chip->cur_cs); 900*2cd457f3SChristophe Kerello if (ret) 901*2cd457f3SChristophe Kerello return ret; 902*2cd457f3SChristophe Kerello 903*2cd457f3SChristophe Kerello /* Configure the sequencer */ 904*2cd457f3SChristophe Kerello stm32_fmc2_rw_page_init(chip, page, 0, false); 905*2cd457f3SChristophe Kerello 906*2cd457f3SChristophe Kerello /* Read the page */ 907*2cd457f3SChristophe Kerello ret = stm32_fmc2_xfer(chip, buf, 0, false); 908*2cd457f3SChristophe Kerello if (ret) 909*2cd457f3SChristophe Kerello return ret; 910*2cd457f3SChristophe Kerello 911*2cd457f3SChristophe Kerello sta_map = stm32_fmc2_get_mapping_status(fmc2); 912*2cd457f3SChristophe Kerello 913*2cd457f3SChristophe Kerello /* Check if errors happen */ 914*2cd457f3SChristophe Kerello if (likely(!sta_map)) { 915*2cd457f3SChristophe Kerello if (oob_required) 916*2cd457f3SChristophe Kerello return nand_change_read_column_op(chip, mtd->writesize, 917*2cd457f3SChristophe Kerello chip->oob_poi, 918*2cd457f3SChristophe Kerello mtd->oobsize, false); 919*2cd457f3SChristophe Kerello 920*2cd457f3SChristophe Kerello return 0; 921*2cd457f3SChristophe Kerello } 922*2cd457f3SChristophe Kerello 923*2cd457f3SChristophe Kerello /* Read oob */ 924*2cd457f3SChristophe Kerello ret = nand_change_read_column_op(chip, mtd->writesize, 925*2cd457f3SChristophe Kerello chip->oob_poi, mtd->oobsize, false); 926*2cd457f3SChristophe Kerello if (ret) 927*2cd457f3SChristophe Kerello return ret; 928*2cd457f3SChristophe Kerello 929*2cd457f3SChristophe Kerello ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, 930*2cd457f3SChristophe Kerello chip->ecc.total); 931*2cd457f3SChristophe Kerello if (ret) 932*2cd457f3SChristophe Kerello return ret; 933*2cd457f3SChristophe Kerello 934*2cd457f3SChristophe Kerello /* Correct data */ 935*2cd457f3SChristophe Kerello return chip->ecc.correct(chip, buf, ecc_code, ecc_calc); 936*2cd457f3SChristophe Kerello } 937*2cd457f3SChristophe Kerello 938*2cd457f3SChristophe Kerello static int stm32_fmc2_sequencer_read_page_raw(struct nand_chip *chip, u8 *buf, 939*2cd457f3SChristophe Kerello int oob_required, int page) 940*2cd457f3SChristophe Kerello { 941*2cd457f3SChristophe Kerello struct mtd_info *mtd = nand_to_mtd(chip); 942*2cd457f3SChristophe Kerello int ret; 943*2cd457f3SChristophe Kerello 944*2cd457f3SChristophe Kerello /* Select the target */ 945*2cd457f3SChristophe Kerello ret = stm32_fmc2_select_chip(chip, chip->cur_cs); 946*2cd457f3SChristophe Kerello if (ret) 947*2cd457f3SChristophe Kerello return ret; 948*2cd457f3SChristophe Kerello 949*2cd457f3SChristophe Kerello /* Configure the sequencer */ 950*2cd457f3SChristophe Kerello stm32_fmc2_rw_page_init(chip, page, 1, false); 951*2cd457f3SChristophe Kerello 952*2cd457f3SChristophe Kerello /* Read the page */ 953*2cd457f3SChristophe Kerello ret = stm32_fmc2_xfer(chip, buf, 1, false); 954*2cd457f3SChristophe Kerello if (ret) 955*2cd457f3SChristophe Kerello return ret; 956*2cd457f3SChristophe Kerello 957*2cd457f3SChristophe Kerello /* Read oob */ 958*2cd457f3SChristophe Kerello if (oob_required) 959*2cd457f3SChristophe Kerello return nand_change_read_column_op(chip, mtd->writesize, 960*2cd457f3SChristophe Kerello chip->oob_poi, mtd->oobsize, 961*2cd457f3SChristophe Kerello false); 962*2cd457f3SChristophe Kerello 963*2cd457f3SChristophe Kerello return 0; 964*2cd457f3SChristophe Kerello } 965*2cd457f3SChristophe Kerello 966*2cd457f3SChristophe Kerello static irqreturn_t stm32_fmc2_irq(int irq, void *dev_id) 967*2cd457f3SChristophe Kerello { 968*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = (struct stm32_fmc2_nfc *)dev_id; 969*2cd457f3SChristophe Kerello 970*2cd457f3SChristophe Kerello stm32_fmc2_disable_seq_irq(fmc2); 971*2cd457f3SChristophe Kerello 972*2cd457f3SChristophe Kerello complete(&fmc2->complete); 973*2cd457f3SChristophe Kerello 974*2cd457f3SChristophe Kerello return IRQ_HANDLED; 975*2cd457f3SChristophe Kerello } 976*2cd457f3SChristophe Kerello 977*2cd457f3SChristophe Kerello static void stm32_fmc2_read_data(struct nand_chip *chip, void *buf, 978*2cd457f3SChristophe Kerello unsigned int len, bool force_8bit) 979*2cd457f3SChristophe Kerello { 980*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 981*2cd457f3SChristophe Kerello void __iomem *io_addr_r = fmc2->data_base[fmc2->cs_sel]; 982*2cd457f3SChristophe Kerello 983*2cd457f3SChristophe Kerello if (force_8bit && chip->options & NAND_BUSWIDTH_16) 984*2cd457f3SChristophe Kerello /* Reconfigure bus width to 8-bit */ 985*2cd457f3SChristophe Kerello stm32_fmc2_set_buswidth_16(fmc2, false); 986*2cd457f3SChristophe Kerello 987*2cd457f3SChristophe Kerello if (!IS_ALIGNED((uintptr_t)buf, sizeof(u32))) { 988*2cd457f3SChristophe Kerello if (!IS_ALIGNED((uintptr_t)buf, sizeof(u16)) && len) { 989*2cd457f3SChristophe Kerello *(u8 *)buf = readb_relaxed(io_addr_r); 990*2cd457f3SChristophe Kerello buf += sizeof(u8); 991*2cd457f3SChristophe Kerello len -= sizeof(u8); 992*2cd457f3SChristophe Kerello } 993*2cd457f3SChristophe Kerello 994*2cd457f3SChristophe Kerello if (!IS_ALIGNED((uintptr_t)buf, sizeof(u32)) && 995*2cd457f3SChristophe Kerello len >= sizeof(u16)) { 996*2cd457f3SChristophe Kerello *(u16 *)buf = readw_relaxed(io_addr_r); 997*2cd457f3SChristophe Kerello buf += sizeof(u16); 998*2cd457f3SChristophe Kerello len -= sizeof(u16); 999*2cd457f3SChristophe Kerello } 1000*2cd457f3SChristophe Kerello } 1001*2cd457f3SChristophe Kerello 1002*2cd457f3SChristophe Kerello /* Buf is aligned */ 1003*2cd457f3SChristophe Kerello while (len >= sizeof(u32)) { 1004*2cd457f3SChristophe Kerello *(u32 *)buf = readl_relaxed(io_addr_r); 1005*2cd457f3SChristophe Kerello buf += sizeof(u32); 1006*2cd457f3SChristophe Kerello len -= sizeof(u32); 1007*2cd457f3SChristophe Kerello } 1008*2cd457f3SChristophe Kerello 1009*2cd457f3SChristophe Kerello /* Read remaining bytes */ 1010*2cd457f3SChristophe Kerello if (len >= sizeof(u16)) { 1011*2cd457f3SChristophe Kerello *(u16 *)buf = readw_relaxed(io_addr_r); 1012*2cd457f3SChristophe Kerello buf += sizeof(u16); 1013*2cd457f3SChristophe Kerello len -= sizeof(u16); 1014*2cd457f3SChristophe Kerello } 1015*2cd457f3SChristophe Kerello 1016*2cd457f3SChristophe Kerello if (len) 1017*2cd457f3SChristophe Kerello *(u8 *)buf = readb_relaxed(io_addr_r); 1018*2cd457f3SChristophe Kerello 1019*2cd457f3SChristophe Kerello if (force_8bit && chip->options & NAND_BUSWIDTH_16) 1020*2cd457f3SChristophe Kerello /* Reconfigure bus width to 16-bit */ 1021*2cd457f3SChristophe Kerello stm32_fmc2_set_buswidth_16(fmc2, true); 1022*2cd457f3SChristophe Kerello } 1023*2cd457f3SChristophe Kerello 1024*2cd457f3SChristophe Kerello static void stm32_fmc2_write_data(struct nand_chip *chip, const void *buf, 1025*2cd457f3SChristophe Kerello unsigned int len, bool force_8bit) 1026*2cd457f3SChristophe Kerello { 1027*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 1028*2cd457f3SChristophe Kerello void __iomem *io_addr_w = fmc2->data_base[fmc2->cs_sel]; 1029*2cd457f3SChristophe Kerello 1030*2cd457f3SChristophe Kerello if (force_8bit && chip->options & NAND_BUSWIDTH_16) 1031*2cd457f3SChristophe Kerello /* Reconfigure bus width to 8-bit */ 1032*2cd457f3SChristophe Kerello stm32_fmc2_set_buswidth_16(fmc2, false); 1033*2cd457f3SChristophe Kerello 1034*2cd457f3SChristophe Kerello if (!IS_ALIGNED((uintptr_t)buf, sizeof(u32))) { 1035*2cd457f3SChristophe Kerello if (!IS_ALIGNED((uintptr_t)buf, sizeof(u16)) && len) { 1036*2cd457f3SChristophe Kerello writeb_relaxed(*(u8 *)buf, io_addr_w); 1037*2cd457f3SChristophe Kerello buf += sizeof(u8); 1038*2cd457f3SChristophe Kerello len -= sizeof(u8); 1039*2cd457f3SChristophe Kerello } 1040*2cd457f3SChristophe Kerello 1041*2cd457f3SChristophe Kerello if (!IS_ALIGNED((uintptr_t)buf, sizeof(u32)) && 1042*2cd457f3SChristophe Kerello len >= sizeof(u16)) { 1043*2cd457f3SChristophe Kerello writew_relaxed(*(u16 *)buf, io_addr_w); 1044*2cd457f3SChristophe Kerello buf += sizeof(u16); 1045*2cd457f3SChristophe Kerello len -= sizeof(u16); 1046*2cd457f3SChristophe Kerello } 1047*2cd457f3SChristophe Kerello } 1048*2cd457f3SChristophe Kerello 1049*2cd457f3SChristophe Kerello /* Buf is aligned */ 1050*2cd457f3SChristophe Kerello while (len >= sizeof(u32)) { 1051*2cd457f3SChristophe Kerello writel_relaxed(*(u32 *)buf, io_addr_w); 1052*2cd457f3SChristophe Kerello buf += sizeof(u32); 1053*2cd457f3SChristophe Kerello len -= sizeof(u32); 1054*2cd457f3SChristophe Kerello } 1055*2cd457f3SChristophe Kerello 1056*2cd457f3SChristophe Kerello /* Write remaining bytes */ 1057*2cd457f3SChristophe Kerello if (len >= sizeof(u16)) { 1058*2cd457f3SChristophe Kerello writew_relaxed(*(u16 *)buf, io_addr_w); 1059*2cd457f3SChristophe Kerello buf += sizeof(u16); 1060*2cd457f3SChristophe Kerello len -= sizeof(u16); 1061*2cd457f3SChristophe Kerello } 1062*2cd457f3SChristophe Kerello 1063*2cd457f3SChristophe Kerello if (len) 1064*2cd457f3SChristophe Kerello writeb_relaxed(*(u8 *)buf, io_addr_w); 1065*2cd457f3SChristophe Kerello 1066*2cd457f3SChristophe Kerello if (force_8bit && chip->options & NAND_BUSWIDTH_16) 1067*2cd457f3SChristophe Kerello /* Reconfigure bus width to 16-bit */ 1068*2cd457f3SChristophe Kerello stm32_fmc2_set_buswidth_16(fmc2, true); 1069*2cd457f3SChristophe Kerello } 1070*2cd457f3SChristophe Kerello 1071*2cd457f3SChristophe Kerello static int stm32_fmc2_exec_op(struct nand_chip *chip, 1072*2cd457f3SChristophe Kerello const struct nand_operation *op, 1073*2cd457f3SChristophe Kerello bool check_only) 1074*2cd457f3SChristophe Kerello { 1075*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 1076*2cd457f3SChristophe Kerello const struct nand_op_instr *instr = NULL; 1077*2cd457f3SChristophe Kerello unsigned int op_id, i; 1078*2cd457f3SChristophe Kerello int ret; 1079*2cd457f3SChristophe Kerello 1080*2cd457f3SChristophe Kerello ret = stm32_fmc2_select_chip(chip, op->cs); 1081*2cd457f3SChristophe Kerello if (ret) 1082*2cd457f3SChristophe Kerello return ret; 1083*2cd457f3SChristophe Kerello 1084*2cd457f3SChristophe Kerello if (check_only) 1085*2cd457f3SChristophe Kerello return ret; 1086*2cd457f3SChristophe Kerello 1087*2cd457f3SChristophe Kerello for (op_id = 0; op_id < op->ninstrs; op_id++) { 1088*2cd457f3SChristophe Kerello instr = &op->instrs[op_id]; 1089*2cd457f3SChristophe Kerello 1090*2cd457f3SChristophe Kerello switch (instr->type) { 1091*2cd457f3SChristophe Kerello case NAND_OP_CMD_INSTR: 1092*2cd457f3SChristophe Kerello writeb_relaxed(instr->ctx.cmd.opcode, 1093*2cd457f3SChristophe Kerello fmc2->cmd_base[fmc2->cs_sel]); 1094*2cd457f3SChristophe Kerello break; 1095*2cd457f3SChristophe Kerello 1096*2cd457f3SChristophe Kerello case NAND_OP_ADDR_INSTR: 1097*2cd457f3SChristophe Kerello for (i = 0; i < instr->ctx.addr.naddrs; i++) 1098*2cd457f3SChristophe Kerello writeb_relaxed(instr->ctx.addr.addrs[i], 1099*2cd457f3SChristophe Kerello fmc2->addr_base[fmc2->cs_sel]); 1100*2cd457f3SChristophe Kerello break; 1101*2cd457f3SChristophe Kerello 1102*2cd457f3SChristophe Kerello case NAND_OP_DATA_IN_INSTR: 1103*2cd457f3SChristophe Kerello stm32_fmc2_read_data(chip, instr->ctx.data.buf.in, 1104*2cd457f3SChristophe Kerello instr->ctx.data.len, 1105*2cd457f3SChristophe Kerello instr->ctx.data.force_8bit); 1106*2cd457f3SChristophe Kerello break; 1107*2cd457f3SChristophe Kerello 1108*2cd457f3SChristophe Kerello case NAND_OP_DATA_OUT_INSTR: 1109*2cd457f3SChristophe Kerello stm32_fmc2_write_data(chip, instr->ctx.data.buf.out, 1110*2cd457f3SChristophe Kerello instr->ctx.data.len, 1111*2cd457f3SChristophe Kerello instr->ctx.data.force_8bit); 1112*2cd457f3SChristophe Kerello break; 1113*2cd457f3SChristophe Kerello 1114*2cd457f3SChristophe Kerello case NAND_OP_WAITRDY_INSTR: 1115*2cd457f3SChristophe Kerello ret = nand_soft_waitrdy(chip, 1116*2cd457f3SChristophe Kerello instr->ctx.waitrdy.timeout_ms); 1117*2cd457f3SChristophe Kerello break; 1118*2cd457f3SChristophe Kerello } 1119*2cd457f3SChristophe Kerello } 1120*2cd457f3SChristophe Kerello 1121*2cd457f3SChristophe Kerello return ret; 1122*2cd457f3SChristophe Kerello } 1123*2cd457f3SChristophe Kerello 1124*2cd457f3SChristophe Kerello /* Controller initialization */ 1125*2cd457f3SChristophe Kerello static void stm32_fmc2_init(struct stm32_fmc2_nfc *fmc2) 1126*2cd457f3SChristophe Kerello { 1127*2cd457f3SChristophe Kerello u32 pcr = readl_relaxed(fmc2->io_base + FMC2_PCR); 1128*2cd457f3SChristophe Kerello u32 bcr1 = readl_relaxed(fmc2->io_base + FMC2_BCR1); 1129*2cd457f3SChristophe Kerello 1130*2cd457f3SChristophe Kerello /* Set CS used to undefined */ 1131*2cd457f3SChristophe Kerello fmc2->cs_sel = -1; 1132*2cd457f3SChristophe Kerello 1133*2cd457f3SChristophe Kerello /* Enable wait feature and nand flash memory bank */ 1134*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_PWAITEN; 1135*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_PBKEN; 1136*2cd457f3SChristophe Kerello 1137*2cd457f3SChristophe Kerello /* Set buswidth to 8 bits mode for identification */ 1138*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_PWID_MASK; 1139*2cd457f3SChristophe Kerello 1140*2cd457f3SChristophe Kerello /* ECC logic is disabled */ 1141*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_ECCEN; 1142*2cd457f3SChristophe Kerello 1143*2cd457f3SChristophe Kerello /* Default mode */ 1144*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_ECCALG; 1145*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_BCHECC; 1146*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_WEN; 1147*2cd457f3SChristophe Kerello 1148*2cd457f3SChristophe Kerello /* Set default ECC sector size */ 1149*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_ECCSS_MASK; 1150*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_ECCSS(FMC2_PCR_ECCSS_2048); 1151*2cd457f3SChristophe Kerello 1152*2cd457f3SChristophe Kerello /* Set default tclr/tar timings */ 1153*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_TCLR_MASK; 1154*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_TCLR(FMC2_PCR_TCLR_DEFAULT); 1155*2cd457f3SChristophe Kerello pcr &= ~FMC2_PCR_TAR_MASK; 1156*2cd457f3SChristophe Kerello pcr |= FMC2_PCR_TAR(FMC2_PCR_TAR_DEFAULT); 1157*2cd457f3SChristophe Kerello 1158*2cd457f3SChristophe Kerello /* Enable FMC2 controller */ 1159*2cd457f3SChristophe Kerello bcr1 |= FMC2_BCR1_FMC2EN; 1160*2cd457f3SChristophe Kerello 1161*2cd457f3SChristophe Kerello writel_relaxed(bcr1, fmc2->io_base + FMC2_BCR1); 1162*2cd457f3SChristophe Kerello writel_relaxed(pcr, fmc2->io_base + FMC2_PCR); 1163*2cd457f3SChristophe Kerello writel_relaxed(FMC2_PMEM_DEFAULT, fmc2->io_base + FMC2_PMEM); 1164*2cd457f3SChristophe Kerello writel_relaxed(FMC2_PATT_DEFAULT, fmc2->io_base + FMC2_PATT); 1165*2cd457f3SChristophe Kerello } 1166*2cd457f3SChristophe Kerello 1167*2cd457f3SChristophe Kerello /* Controller timings */ 1168*2cd457f3SChristophe Kerello static void stm32_fmc2_calc_timings(struct nand_chip *chip, 1169*2cd457f3SChristophe Kerello const struct nand_sdr_timings *sdrt) 1170*2cd457f3SChristophe Kerello { 1171*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 1172*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand = to_fmc2_nand(chip); 1173*2cd457f3SChristophe Kerello struct stm32_fmc2_timings *tims = &nand->timings; 1174*2cd457f3SChristophe Kerello unsigned long hclk = clk_get_rate(fmc2->clk); 1175*2cd457f3SChristophe Kerello unsigned long hclkp = NSEC_PER_SEC / (hclk / 1000); 1176*2cd457f3SChristophe Kerello int tar, tclr, thiz, twait, tset_mem, tset_att, thold_mem, thold_att; 1177*2cd457f3SChristophe Kerello 1178*2cd457f3SChristophe Kerello tar = hclkp; 1179*2cd457f3SChristophe Kerello if (tar < sdrt->tAR_min) 1180*2cd457f3SChristophe Kerello tar = sdrt->tAR_min; 1181*2cd457f3SChristophe Kerello tims->tar = DIV_ROUND_UP(tar, hclkp) - 1; 1182*2cd457f3SChristophe Kerello if (tims->tar > FMC2_PCR_TIMING_MASK) 1183*2cd457f3SChristophe Kerello tims->tar = FMC2_PCR_TIMING_MASK; 1184*2cd457f3SChristophe Kerello 1185*2cd457f3SChristophe Kerello tclr = hclkp; 1186*2cd457f3SChristophe Kerello if (tclr < sdrt->tCLR_min) 1187*2cd457f3SChristophe Kerello tclr = sdrt->tCLR_min; 1188*2cd457f3SChristophe Kerello tims->tclr = DIV_ROUND_UP(tclr, hclkp) - 1; 1189*2cd457f3SChristophe Kerello if (tims->tclr > FMC2_PCR_TIMING_MASK) 1190*2cd457f3SChristophe Kerello tims->tclr = FMC2_PCR_TIMING_MASK; 1191*2cd457f3SChristophe Kerello 1192*2cd457f3SChristophe Kerello tims->thiz = FMC2_THIZ; 1193*2cd457f3SChristophe Kerello thiz = (tims->thiz + 1) * hclkp; 1194*2cd457f3SChristophe Kerello 1195*2cd457f3SChristophe Kerello /* 1196*2cd457f3SChristophe Kerello * tWAIT > tRP 1197*2cd457f3SChristophe Kerello * tWAIT > tWP 1198*2cd457f3SChristophe Kerello * tWAIT > tREA + tIO 1199*2cd457f3SChristophe Kerello */ 1200*2cd457f3SChristophe Kerello twait = hclkp; 1201*2cd457f3SChristophe Kerello if (twait < sdrt->tRP_min) 1202*2cd457f3SChristophe Kerello twait = sdrt->tRP_min; 1203*2cd457f3SChristophe Kerello if (twait < sdrt->tWP_min) 1204*2cd457f3SChristophe Kerello twait = sdrt->tWP_min; 1205*2cd457f3SChristophe Kerello if (twait < sdrt->tREA_max + FMC2_TIO) 1206*2cd457f3SChristophe Kerello twait = sdrt->tREA_max + FMC2_TIO; 1207*2cd457f3SChristophe Kerello tims->twait = DIV_ROUND_UP(twait, hclkp); 1208*2cd457f3SChristophe Kerello if (tims->twait == 0) 1209*2cd457f3SChristophe Kerello tims->twait = 1; 1210*2cd457f3SChristophe Kerello else if (tims->twait > FMC2_PMEM_PATT_TIMING_MASK) 1211*2cd457f3SChristophe Kerello tims->twait = FMC2_PMEM_PATT_TIMING_MASK; 1212*2cd457f3SChristophe Kerello 1213*2cd457f3SChristophe Kerello /* 1214*2cd457f3SChristophe Kerello * tSETUP_MEM > tCS - tWAIT 1215*2cd457f3SChristophe Kerello * tSETUP_MEM > tALS - tWAIT 1216*2cd457f3SChristophe Kerello * tSETUP_MEM > tDS - (tWAIT - tHIZ) 1217*2cd457f3SChristophe Kerello */ 1218*2cd457f3SChristophe Kerello tset_mem = hclkp; 1219*2cd457f3SChristophe Kerello if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait)) 1220*2cd457f3SChristophe Kerello tset_mem = sdrt->tCS_min - twait; 1221*2cd457f3SChristophe Kerello if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait)) 1222*2cd457f3SChristophe Kerello tset_mem = sdrt->tALS_min - twait; 1223*2cd457f3SChristophe Kerello if (twait > thiz && (sdrt->tDS_min > twait - thiz) && 1224*2cd457f3SChristophe Kerello (tset_mem < sdrt->tDS_min - (twait - thiz))) 1225*2cd457f3SChristophe Kerello tset_mem = sdrt->tDS_min - (twait - thiz); 1226*2cd457f3SChristophe Kerello tims->tset_mem = DIV_ROUND_UP(tset_mem, hclkp); 1227*2cd457f3SChristophe Kerello if (tims->tset_mem == 0) 1228*2cd457f3SChristophe Kerello tims->tset_mem = 1; 1229*2cd457f3SChristophe Kerello else if (tims->tset_mem > FMC2_PMEM_PATT_TIMING_MASK) 1230*2cd457f3SChristophe Kerello tims->tset_mem = FMC2_PMEM_PATT_TIMING_MASK; 1231*2cd457f3SChristophe Kerello 1232*2cd457f3SChristophe Kerello /* 1233*2cd457f3SChristophe Kerello * tHOLD_MEM > tCH 1234*2cd457f3SChristophe Kerello * tHOLD_MEM > tREH - tSETUP_MEM 1235*2cd457f3SChristophe Kerello * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT) 1236*2cd457f3SChristophe Kerello */ 1237*2cd457f3SChristophe Kerello thold_mem = hclkp; 1238*2cd457f3SChristophe Kerello if (thold_mem < sdrt->tCH_min) 1239*2cd457f3SChristophe Kerello thold_mem = sdrt->tCH_min; 1240*2cd457f3SChristophe Kerello if (sdrt->tREH_min > tset_mem && 1241*2cd457f3SChristophe Kerello (thold_mem < sdrt->tREH_min - tset_mem)) 1242*2cd457f3SChristophe Kerello thold_mem = sdrt->tREH_min - tset_mem; 1243*2cd457f3SChristophe Kerello if ((sdrt->tRC_min > tset_mem + twait) && 1244*2cd457f3SChristophe Kerello (thold_mem < sdrt->tRC_min - (tset_mem + twait))) 1245*2cd457f3SChristophe Kerello thold_mem = sdrt->tRC_min - (tset_mem + twait); 1246*2cd457f3SChristophe Kerello if ((sdrt->tWC_min > tset_mem + twait) && 1247*2cd457f3SChristophe Kerello (thold_mem < sdrt->tWC_min - (tset_mem + twait))) 1248*2cd457f3SChristophe Kerello thold_mem = sdrt->tWC_min - (tset_mem + twait); 1249*2cd457f3SChristophe Kerello tims->thold_mem = DIV_ROUND_UP(thold_mem, hclkp); 1250*2cd457f3SChristophe Kerello if (tims->thold_mem == 0) 1251*2cd457f3SChristophe Kerello tims->thold_mem = 1; 1252*2cd457f3SChristophe Kerello else if (tims->thold_mem > FMC2_PMEM_PATT_TIMING_MASK) 1253*2cd457f3SChristophe Kerello tims->thold_mem = FMC2_PMEM_PATT_TIMING_MASK; 1254*2cd457f3SChristophe Kerello 1255*2cd457f3SChristophe Kerello /* 1256*2cd457f3SChristophe Kerello * tSETUP_ATT > tCS - tWAIT 1257*2cd457f3SChristophe Kerello * tSETUP_ATT > tCLS - tWAIT 1258*2cd457f3SChristophe Kerello * tSETUP_ATT > tALS - tWAIT 1259*2cd457f3SChristophe Kerello * tSETUP_ATT > tRHW - tHOLD_MEM 1260*2cd457f3SChristophe Kerello * tSETUP_ATT > tDS - (tWAIT - tHIZ) 1261*2cd457f3SChristophe Kerello */ 1262*2cd457f3SChristophe Kerello tset_att = hclkp; 1263*2cd457f3SChristophe Kerello if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait)) 1264*2cd457f3SChristophe Kerello tset_att = sdrt->tCS_min - twait; 1265*2cd457f3SChristophe Kerello if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait)) 1266*2cd457f3SChristophe Kerello tset_att = sdrt->tCLS_min - twait; 1267*2cd457f3SChristophe Kerello if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait)) 1268*2cd457f3SChristophe Kerello tset_att = sdrt->tALS_min - twait; 1269*2cd457f3SChristophe Kerello if (sdrt->tRHW_min > thold_mem && 1270*2cd457f3SChristophe Kerello (tset_att < sdrt->tRHW_min - thold_mem)) 1271*2cd457f3SChristophe Kerello tset_att = sdrt->tRHW_min - thold_mem; 1272*2cd457f3SChristophe Kerello if (twait > thiz && (sdrt->tDS_min > twait - thiz) && 1273*2cd457f3SChristophe Kerello (tset_att < sdrt->tDS_min - (twait - thiz))) 1274*2cd457f3SChristophe Kerello tset_att = sdrt->tDS_min - (twait - thiz); 1275*2cd457f3SChristophe Kerello tims->tset_att = DIV_ROUND_UP(tset_att, hclkp); 1276*2cd457f3SChristophe Kerello if (tims->tset_att == 0) 1277*2cd457f3SChristophe Kerello tims->tset_att = 1; 1278*2cd457f3SChristophe Kerello else if (tims->tset_att > FMC2_PMEM_PATT_TIMING_MASK) 1279*2cd457f3SChristophe Kerello tims->tset_att = FMC2_PMEM_PATT_TIMING_MASK; 1280*2cd457f3SChristophe Kerello 1281*2cd457f3SChristophe Kerello /* 1282*2cd457f3SChristophe Kerello * tHOLD_ATT > tALH 1283*2cd457f3SChristophe Kerello * tHOLD_ATT > tCH 1284*2cd457f3SChristophe Kerello * tHOLD_ATT > tCLH 1285*2cd457f3SChristophe Kerello * tHOLD_ATT > tCOH 1286*2cd457f3SChristophe Kerello * tHOLD_ATT > tDH 1287*2cd457f3SChristophe Kerello * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM 1288*2cd457f3SChristophe Kerello * tHOLD_ATT > tADL - tSETUP_MEM 1289*2cd457f3SChristophe Kerello * tHOLD_ATT > tWH - tSETUP_MEM 1290*2cd457f3SChristophe Kerello * tHOLD_ATT > tWHR - tSETUP_MEM 1291*2cd457f3SChristophe Kerello * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT) 1292*2cd457f3SChristophe Kerello * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT) 1293*2cd457f3SChristophe Kerello */ 1294*2cd457f3SChristophe Kerello thold_att = hclkp; 1295*2cd457f3SChristophe Kerello if (thold_att < sdrt->tALH_min) 1296*2cd457f3SChristophe Kerello thold_att = sdrt->tALH_min; 1297*2cd457f3SChristophe Kerello if (thold_att < sdrt->tCH_min) 1298*2cd457f3SChristophe Kerello thold_att = sdrt->tCH_min; 1299*2cd457f3SChristophe Kerello if (thold_att < sdrt->tCLH_min) 1300*2cd457f3SChristophe Kerello thold_att = sdrt->tCLH_min; 1301*2cd457f3SChristophe Kerello if (thold_att < sdrt->tCOH_min) 1302*2cd457f3SChristophe Kerello thold_att = sdrt->tCOH_min; 1303*2cd457f3SChristophe Kerello if (thold_att < sdrt->tDH_min) 1304*2cd457f3SChristophe Kerello thold_att = sdrt->tDH_min; 1305*2cd457f3SChristophe Kerello if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) && 1306*2cd457f3SChristophe Kerello (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem)) 1307*2cd457f3SChristophe Kerello thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem; 1308*2cd457f3SChristophe Kerello if (sdrt->tADL_min > tset_mem && 1309*2cd457f3SChristophe Kerello (thold_att < sdrt->tADL_min - tset_mem)) 1310*2cd457f3SChristophe Kerello thold_att = sdrt->tADL_min - tset_mem; 1311*2cd457f3SChristophe Kerello if (sdrt->tWH_min > tset_mem && 1312*2cd457f3SChristophe Kerello (thold_att < sdrt->tWH_min - tset_mem)) 1313*2cd457f3SChristophe Kerello thold_att = sdrt->tWH_min - tset_mem; 1314*2cd457f3SChristophe Kerello if (sdrt->tWHR_min > tset_mem && 1315*2cd457f3SChristophe Kerello (thold_att < sdrt->tWHR_min - tset_mem)) 1316*2cd457f3SChristophe Kerello thold_att = sdrt->tWHR_min - tset_mem; 1317*2cd457f3SChristophe Kerello if ((sdrt->tRC_min > tset_att + twait) && 1318*2cd457f3SChristophe Kerello (thold_att < sdrt->tRC_min - (tset_att + twait))) 1319*2cd457f3SChristophe Kerello thold_att = sdrt->tRC_min - (tset_att + twait); 1320*2cd457f3SChristophe Kerello if ((sdrt->tWC_min > tset_att + twait) && 1321*2cd457f3SChristophe Kerello (thold_att < sdrt->tWC_min - (tset_att + twait))) 1322*2cd457f3SChristophe Kerello thold_att = sdrt->tWC_min - (tset_att + twait); 1323*2cd457f3SChristophe Kerello tims->thold_att = DIV_ROUND_UP(thold_att, hclkp); 1324*2cd457f3SChristophe Kerello if (tims->thold_att == 0) 1325*2cd457f3SChristophe Kerello tims->thold_att = 1; 1326*2cd457f3SChristophe Kerello else if (tims->thold_att > FMC2_PMEM_PATT_TIMING_MASK) 1327*2cd457f3SChristophe Kerello tims->thold_att = FMC2_PMEM_PATT_TIMING_MASK; 1328*2cd457f3SChristophe Kerello } 1329*2cd457f3SChristophe Kerello 1330*2cd457f3SChristophe Kerello static int stm32_fmc2_setup_interface(struct nand_chip *chip, int chipnr, 1331*2cd457f3SChristophe Kerello const struct nand_data_interface *conf) 1332*2cd457f3SChristophe Kerello { 1333*2cd457f3SChristophe Kerello const struct nand_sdr_timings *sdrt; 1334*2cd457f3SChristophe Kerello 1335*2cd457f3SChristophe Kerello sdrt = nand_get_sdr_timings(conf); 1336*2cd457f3SChristophe Kerello if (IS_ERR(sdrt)) 1337*2cd457f3SChristophe Kerello return PTR_ERR(sdrt); 1338*2cd457f3SChristophe Kerello 1339*2cd457f3SChristophe Kerello if (chipnr == NAND_DATA_IFACE_CHECK_ONLY) 1340*2cd457f3SChristophe Kerello return 0; 1341*2cd457f3SChristophe Kerello 1342*2cd457f3SChristophe Kerello stm32_fmc2_calc_timings(chip, sdrt); 1343*2cd457f3SChristophe Kerello 1344*2cd457f3SChristophe Kerello /* Apply timings */ 1345*2cd457f3SChristophe Kerello stm32_fmc2_timings_init(chip); 1346*2cd457f3SChristophe Kerello 1347*2cd457f3SChristophe Kerello return 0; 1348*2cd457f3SChristophe Kerello } 1349*2cd457f3SChristophe Kerello 1350*2cd457f3SChristophe Kerello /* DMA configuration */ 1351*2cd457f3SChristophe Kerello static int stm32_fmc2_dma_setup(struct stm32_fmc2_nfc *fmc2) 1352*2cd457f3SChristophe Kerello { 1353*2cd457f3SChristophe Kerello int ret; 1354*2cd457f3SChristophe Kerello 1355*2cd457f3SChristophe Kerello fmc2->dma_tx_ch = dma_request_slave_channel(fmc2->dev, "tx"); 1356*2cd457f3SChristophe Kerello fmc2->dma_rx_ch = dma_request_slave_channel(fmc2->dev, "rx"); 1357*2cd457f3SChristophe Kerello fmc2->dma_ecc_ch = dma_request_slave_channel(fmc2->dev, "ecc"); 1358*2cd457f3SChristophe Kerello 1359*2cd457f3SChristophe Kerello if (fmc2->dma_ecc_ch) { 1360*2cd457f3SChristophe Kerello ret = sg_alloc_table(&fmc2->dma_ecc_sg, FMC2_MAX_SG, 1361*2cd457f3SChristophe Kerello GFP_KERNEL); 1362*2cd457f3SChristophe Kerello if (ret) 1363*2cd457f3SChristophe Kerello return ret; 1364*2cd457f3SChristophe Kerello 1365*2cd457f3SChristophe Kerello /* Allocate a buffer to store ECC status registers */ 1366*2cd457f3SChristophe Kerello fmc2->ecc_buf = devm_kzalloc(fmc2->dev, 1367*2cd457f3SChristophe Kerello FMC2_MAX_ECC_BUF_LEN, 1368*2cd457f3SChristophe Kerello GFP_KERNEL); 1369*2cd457f3SChristophe Kerello if (!fmc2->ecc_buf) 1370*2cd457f3SChristophe Kerello return -ENOMEM; 1371*2cd457f3SChristophe Kerello } else { 1372*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "ECC DMA not defined in the device tree\n"); 1373*2cd457f3SChristophe Kerello return -ENOENT; 1374*2cd457f3SChristophe Kerello } 1375*2cd457f3SChristophe Kerello 1376*2cd457f3SChristophe Kerello if (fmc2->dma_tx_ch && fmc2->dma_rx_ch) { 1377*2cd457f3SChristophe Kerello ret = sg_alloc_table(&fmc2->dma_data_sg, FMC2_MAX_SG, 1378*2cd457f3SChristophe Kerello GFP_KERNEL); 1379*2cd457f3SChristophe Kerello if (ret) 1380*2cd457f3SChristophe Kerello return ret; 1381*2cd457f3SChristophe Kerello 1382*2cd457f3SChristophe Kerello init_completion(&fmc2->dma_data_complete); 1383*2cd457f3SChristophe Kerello init_completion(&fmc2->dma_ecc_complete); 1384*2cd457f3SChristophe Kerello } else { 1385*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "rx/tx DMA not defined in the device tree\n"); 1386*2cd457f3SChristophe Kerello return -ENOENT; 1387*2cd457f3SChristophe Kerello } 1388*2cd457f3SChristophe Kerello 1389*2cd457f3SChristophe Kerello return 0; 1390*2cd457f3SChristophe Kerello } 1391*2cd457f3SChristophe Kerello 1392*2cd457f3SChristophe Kerello /* NAND callbacks setup */ 1393*2cd457f3SChristophe Kerello static void stm32_fmc2_nand_callbacks_setup(struct nand_chip *chip) 1394*2cd457f3SChristophe Kerello { 1395*2cd457f3SChristophe Kerello /* Specific callbacks to read/write a page */ 1396*2cd457f3SChristophe Kerello chip->ecc.correct = stm32_fmc2_sequencer_correct; 1397*2cd457f3SChristophe Kerello chip->ecc.write_page = stm32_fmc2_sequencer_write_page; 1398*2cd457f3SChristophe Kerello chip->ecc.read_page = stm32_fmc2_sequencer_read_page; 1399*2cd457f3SChristophe Kerello chip->ecc.write_page_raw = stm32_fmc2_sequencer_write_page_raw; 1400*2cd457f3SChristophe Kerello chip->ecc.read_page_raw = stm32_fmc2_sequencer_read_page_raw; 1401*2cd457f3SChristophe Kerello 1402*2cd457f3SChristophe Kerello /* Specific configurations depending on the algo used */ 1403*2cd457f3SChristophe Kerello if (chip->ecc.strength == FMC2_ECC_HAM) 1404*2cd457f3SChristophe Kerello chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3; 1405*2cd457f3SChristophe Kerello else if (chip->ecc.strength == FMC2_ECC_BCH8) 1406*2cd457f3SChristophe Kerello chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13; 1407*2cd457f3SChristophe Kerello else 1408*2cd457f3SChristophe Kerello chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7; 1409*2cd457f3SChristophe Kerello } 1410*2cd457f3SChristophe Kerello 1411*2cd457f3SChristophe Kerello /* FMC2 layout */ 1412*2cd457f3SChristophe Kerello static int stm32_fmc2_nand_ooblayout_ecc(struct mtd_info *mtd, int section, 1413*2cd457f3SChristophe Kerello struct mtd_oob_region *oobregion) 1414*2cd457f3SChristophe Kerello { 1415*2cd457f3SChristophe Kerello struct nand_chip *chip = mtd_to_nand(mtd); 1416*2cd457f3SChristophe Kerello struct nand_ecc_ctrl *ecc = &chip->ecc; 1417*2cd457f3SChristophe Kerello 1418*2cd457f3SChristophe Kerello if (section) 1419*2cd457f3SChristophe Kerello return -ERANGE; 1420*2cd457f3SChristophe Kerello 1421*2cd457f3SChristophe Kerello oobregion->length = ecc->total; 1422*2cd457f3SChristophe Kerello oobregion->offset = FMC2_BBM_LEN; 1423*2cd457f3SChristophe Kerello 1424*2cd457f3SChristophe Kerello return 0; 1425*2cd457f3SChristophe Kerello } 1426*2cd457f3SChristophe Kerello 1427*2cd457f3SChristophe Kerello static int stm32_fmc2_nand_ooblayout_free(struct mtd_info *mtd, int section, 1428*2cd457f3SChristophe Kerello struct mtd_oob_region *oobregion) 1429*2cd457f3SChristophe Kerello { 1430*2cd457f3SChristophe Kerello struct nand_chip *chip = mtd_to_nand(mtd); 1431*2cd457f3SChristophe Kerello struct nand_ecc_ctrl *ecc = &chip->ecc; 1432*2cd457f3SChristophe Kerello 1433*2cd457f3SChristophe Kerello if (section) 1434*2cd457f3SChristophe Kerello return -ERANGE; 1435*2cd457f3SChristophe Kerello 1436*2cd457f3SChristophe Kerello oobregion->length = mtd->oobsize - ecc->total - FMC2_BBM_LEN; 1437*2cd457f3SChristophe Kerello oobregion->offset = ecc->total + FMC2_BBM_LEN; 1438*2cd457f3SChristophe Kerello 1439*2cd457f3SChristophe Kerello return 0; 1440*2cd457f3SChristophe Kerello } 1441*2cd457f3SChristophe Kerello 1442*2cd457f3SChristophe Kerello static const struct mtd_ooblayout_ops stm32_fmc2_nand_ooblayout_ops = { 1443*2cd457f3SChristophe Kerello .ecc = stm32_fmc2_nand_ooblayout_ecc, 1444*2cd457f3SChristophe Kerello .free = stm32_fmc2_nand_ooblayout_free, 1445*2cd457f3SChristophe Kerello }; 1446*2cd457f3SChristophe Kerello 1447*2cd457f3SChristophe Kerello /* FMC2 caps */ 1448*2cd457f3SChristophe Kerello static int stm32_fmc2_calc_ecc_bytes(int step_size, int strength) 1449*2cd457f3SChristophe Kerello { 1450*2cd457f3SChristophe Kerello /* Hamming */ 1451*2cd457f3SChristophe Kerello if (strength == FMC2_ECC_HAM) 1452*2cd457f3SChristophe Kerello return 4; 1453*2cd457f3SChristophe Kerello 1454*2cd457f3SChristophe Kerello /* BCH8 */ 1455*2cd457f3SChristophe Kerello if (strength == FMC2_ECC_BCH8) 1456*2cd457f3SChristophe Kerello return 14; 1457*2cd457f3SChristophe Kerello 1458*2cd457f3SChristophe Kerello /* BCH4 */ 1459*2cd457f3SChristophe Kerello return 8; 1460*2cd457f3SChristophe Kerello } 1461*2cd457f3SChristophe Kerello 1462*2cd457f3SChristophe Kerello NAND_ECC_CAPS_SINGLE(stm32_fmc2_ecc_caps, stm32_fmc2_calc_ecc_bytes, 1463*2cd457f3SChristophe Kerello FMC2_ECC_STEP_SIZE, 1464*2cd457f3SChristophe Kerello FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8); 1465*2cd457f3SChristophe Kerello 1466*2cd457f3SChristophe Kerello /* FMC2 controller ops */ 1467*2cd457f3SChristophe Kerello static int stm32_fmc2_attach_chip(struct nand_chip *chip) 1468*2cd457f3SChristophe Kerello { 1469*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = to_stm32_nfc(chip->controller); 1470*2cd457f3SChristophe Kerello struct mtd_info *mtd = nand_to_mtd(chip); 1471*2cd457f3SChristophe Kerello int ret; 1472*2cd457f3SChristophe Kerello 1473*2cd457f3SChristophe Kerello /* 1474*2cd457f3SChristophe Kerello * Only NAND_ECC_HW mode is actually supported 1475*2cd457f3SChristophe Kerello * Hamming => ecc.strength = 1 1476*2cd457f3SChristophe Kerello * BCH4 => ecc.strength = 4 1477*2cd457f3SChristophe Kerello * BCH8 => ecc.strength = 8 1478*2cd457f3SChristophe Kerello * ECC sector size = 512 1479*2cd457f3SChristophe Kerello */ 1480*2cd457f3SChristophe Kerello if (chip->ecc.mode != NAND_ECC_HW) { 1481*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "nand_ecc_mode is not well defined in the DT\n"); 1482*2cd457f3SChristophe Kerello return -EINVAL; 1483*2cd457f3SChristophe Kerello } 1484*2cd457f3SChristophe Kerello 1485*2cd457f3SChristophe Kerello ret = nand_ecc_choose_conf(chip, &stm32_fmc2_ecc_caps, 1486*2cd457f3SChristophe Kerello mtd->oobsize - FMC2_BBM_LEN); 1487*2cd457f3SChristophe Kerello if (ret) { 1488*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "no valid ECC settings set\n"); 1489*2cd457f3SChristophe Kerello return ret; 1490*2cd457f3SChristophe Kerello } 1491*2cd457f3SChristophe Kerello 1492*2cd457f3SChristophe Kerello if (mtd->writesize / chip->ecc.size > FMC2_MAX_SG) { 1493*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "nand page size is not supported\n"); 1494*2cd457f3SChristophe Kerello return -EINVAL; 1495*2cd457f3SChristophe Kerello } 1496*2cd457f3SChristophe Kerello 1497*2cd457f3SChristophe Kerello if (chip->bbt_options & NAND_BBT_USE_FLASH) 1498*2cd457f3SChristophe Kerello chip->bbt_options |= NAND_BBT_NO_OOB; 1499*2cd457f3SChristophe Kerello 1500*2cd457f3SChristophe Kerello /* NAND callbacks setup */ 1501*2cd457f3SChristophe Kerello stm32_fmc2_nand_callbacks_setup(chip); 1502*2cd457f3SChristophe Kerello 1503*2cd457f3SChristophe Kerello /* Define ECC layout */ 1504*2cd457f3SChristophe Kerello mtd_set_ooblayout(mtd, &stm32_fmc2_nand_ooblayout_ops); 1505*2cd457f3SChristophe Kerello 1506*2cd457f3SChristophe Kerello /* Configure bus width to 16-bit */ 1507*2cd457f3SChristophe Kerello if (chip->options & NAND_BUSWIDTH_16) 1508*2cd457f3SChristophe Kerello stm32_fmc2_set_buswidth_16(fmc2, true); 1509*2cd457f3SChristophe Kerello 1510*2cd457f3SChristophe Kerello return 0; 1511*2cd457f3SChristophe Kerello } 1512*2cd457f3SChristophe Kerello 1513*2cd457f3SChristophe Kerello static const struct nand_controller_ops stm32_fmc2_nand_controller_ops = { 1514*2cd457f3SChristophe Kerello .attach_chip = stm32_fmc2_attach_chip, 1515*2cd457f3SChristophe Kerello .exec_op = stm32_fmc2_exec_op, 1516*2cd457f3SChristophe Kerello .setup_data_interface = stm32_fmc2_setup_interface, 1517*2cd457f3SChristophe Kerello }; 1518*2cd457f3SChristophe Kerello 1519*2cd457f3SChristophe Kerello /* FMC2 probe */ 1520*2cd457f3SChristophe Kerello static int stm32_fmc2_parse_child(struct stm32_fmc2_nfc *fmc2, 1521*2cd457f3SChristophe Kerello struct device_node *dn) 1522*2cd457f3SChristophe Kerello { 1523*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand = &fmc2->nand; 1524*2cd457f3SChristophe Kerello u32 cs; 1525*2cd457f3SChristophe Kerello int ret, i; 1526*2cd457f3SChristophe Kerello 1527*2cd457f3SChristophe Kerello if (!of_get_property(dn, "reg", &nand->ncs)) 1528*2cd457f3SChristophe Kerello return -EINVAL; 1529*2cd457f3SChristophe Kerello 1530*2cd457f3SChristophe Kerello nand->ncs /= sizeof(u32); 1531*2cd457f3SChristophe Kerello if (!nand->ncs) { 1532*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "invalid reg property size\n"); 1533*2cd457f3SChristophe Kerello return -EINVAL; 1534*2cd457f3SChristophe Kerello } 1535*2cd457f3SChristophe Kerello 1536*2cd457f3SChristophe Kerello for (i = 0; i < nand->ncs; i++) { 1537*2cd457f3SChristophe Kerello ret = of_property_read_u32_index(dn, "reg", i, &cs); 1538*2cd457f3SChristophe Kerello if (ret) { 1539*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "could not retrieve reg property: %d\n", 1540*2cd457f3SChristophe Kerello ret); 1541*2cd457f3SChristophe Kerello return ret; 1542*2cd457f3SChristophe Kerello } 1543*2cd457f3SChristophe Kerello 1544*2cd457f3SChristophe Kerello if (cs > FMC2_MAX_CE) { 1545*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "invalid reg value: %d\n", cs); 1546*2cd457f3SChristophe Kerello return -EINVAL; 1547*2cd457f3SChristophe Kerello } 1548*2cd457f3SChristophe Kerello 1549*2cd457f3SChristophe Kerello if (fmc2->cs_assigned & BIT(cs)) { 1550*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "cs already assigned: %d\n", cs); 1551*2cd457f3SChristophe Kerello return -EINVAL; 1552*2cd457f3SChristophe Kerello } 1553*2cd457f3SChristophe Kerello 1554*2cd457f3SChristophe Kerello fmc2->cs_assigned |= BIT(cs); 1555*2cd457f3SChristophe Kerello nand->cs_used[i] = cs; 1556*2cd457f3SChristophe Kerello } 1557*2cd457f3SChristophe Kerello 1558*2cd457f3SChristophe Kerello nand_set_flash_node(&nand->chip, dn); 1559*2cd457f3SChristophe Kerello 1560*2cd457f3SChristophe Kerello return 0; 1561*2cd457f3SChristophe Kerello } 1562*2cd457f3SChristophe Kerello 1563*2cd457f3SChristophe Kerello static int stm32_fmc2_parse_dt(struct stm32_fmc2_nfc *fmc2) 1564*2cd457f3SChristophe Kerello { 1565*2cd457f3SChristophe Kerello struct device_node *dn = fmc2->dev->of_node; 1566*2cd457f3SChristophe Kerello struct device_node *child; 1567*2cd457f3SChristophe Kerello int nchips = of_get_child_count(dn); 1568*2cd457f3SChristophe Kerello int ret = 0; 1569*2cd457f3SChristophe Kerello 1570*2cd457f3SChristophe Kerello if (!nchips) { 1571*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "NAND chip not defined\n"); 1572*2cd457f3SChristophe Kerello return -EINVAL; 1573*2cd457f3SChristophe Kerello } 1574*2cd457f3SChristophe Kerello 1575*2cd457f3SChristophe Kerello if (nchips > 1) { 1576*2cd457f3SChristophe Kerello dev_err(fmc2->dev, "too many NAND chips defined\n"); 1577*2cd457f3SChristophe Kerello return -EINVAL; 1578*2cd457f3SChristophe Kerello } 1579*2cd457f3SChristophe Kerello 1580*2cd457f3SChristophe Kerello for_each_child_of_node(dn, child) { 1581*2cd457f3SChristophe Kerello ret = stm32_fmc2_parse_child(fmc2, child); 1582*2cd457f3SChristophe Kerello if (ret < 0) { 1583*2cd457f3SChristophe Kerello of_node_put(child); 1584*2cd457f3SChristophe Kerello return ret; 1585*2cd457f3SChristophe Kerello } 1586*2cd457f3SChristophe Kerello } 1587*2cd457f3SChristophe Kerello 1588*2cd457f3SChristophe Kerello return ret; 1589*2cd457f3SChristophe Kerello } 1590*2cd457f3SChristophe Kerello 1591*2cd457f3SChristophe Kerello static int stm32_fmc2_probe(struct platform_device *pdev) 1592*2cd457f3SChristophe Kerello { 1593*2cd457f3SChristophe Kerello struct device *dev = &pdev->dev; 1594*2cd457f3SChristophe Kerello struct reset_control *rstc; 1595*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2; 1596*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand; 1597*2cd457f3SChristophe Kerello struct resource *res; 1598*2cd457f3SChristophe Kerello struct mtd_info *mtd; 1599*2cd457f3SChristophe Kerello struct nand_chip *chip; 1600*2cd457f3SChristophe Kerello int chip_cs, mem_region, ret, irq; 1601*2cd457f3SChristophe Kerello 1602*2cd457f3SChristophe Kerello fmc2 = devm_kzalloc(dev, sizeof(*fmc2), GFP_KERNEL); 1603*2cd457f3SChristophe Kerello if (!fmc2) 1604*2cd457f3SChristophe Kerello return -ENOMEM; 1605*2cd457f3SChristophe Kerello 1606*2cd457f3SChristophe Kerello fmc2->dev = dev; 1607*2cd457f3SChristophe Kerello nand_controller_init(&fmc2->base); 1608*2cd457f3SChristophe Kerello fmc2->base.ops = &stm32_fmc2_nand_controller_ops; 1609*2cd457f3SChristophe Kerello 1610*2cd457f3SChristophe Kerello ret = stm32_fmc2_parse_dt(fmc2); 1611*2cd457f3SChristophe Kerello if (ret) 1612*2cd457f3SChristophe Kerello return ret; 1613*2cd457f3SChristophe Kerello 1614*2cd457f3SChristophe Kerello res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1615*2cd457f3SChristophe Kerello fmc2->io_base = devm_ioremap_resource(dev, res); 1616*2cd457f3SChristophe Kerello if (IS_ERR(fmc2->io_base)) 1617*2cd457f3SChristophe Kerello return PTR_ERR(fmc2->io_base); 1618*2cd457f3SChristophe Kerello 1619*2cd457f3SChristophe Kerello fmc2->io_phys_addr = res->start; 1620*2cd457f3SChristophe Kerello 1621*2cd457f3SChristophe Kerello for (chip_cs = 0, mem_region = 1; chip_cs < FMC2_MAX_CE; 1622*2cd457f3SChristophe Kerello chip_cs++, mem_region += 3) { 1623*2cd457f3SChristophe Kerello if (!(fmc2->cs_assigned & BIT(chip_cs))) 1624*2cd457f3SChristophe Kerello continue; 1625*2cd457f3SChristophe Kerello 1626*2cd457f3SChristophe Kerello res = platform_get_resource(pdev, IORESOURCE_MEM, mem_region); 1627*2cd457f3SChristophe Kerello fmc2->data_base[chip_cs] = devm_ioremap_resource(dev, res); 1628*2cd457f3SChristophe Kerello if (IS_ERR(fmc2->data_base[chip_cs])) 1629*2cd457f3SChristophe Kerello return PTR_ERR(fmc2->data_base[chip_cs]); 1630*2cd457f3SChristophe Kerello 1631*2cd457f3SChristophe Kerello fmc2->data_phys_addr[chip_cs] = res->start; 1632*2cd457f3SChristophe Kerello 1633*2cd457f3SChristophe Kerello res = platform_get_resource(pdev, IORESOURCE_MEM, 1634*2cd457f3SChristophe Kerello mem_region + 1); 1635*2cd457f3SChristophe Kerello fmc2->cmd_base[chip_cs] = devm_ioremap_resource(dev, res); 1636*2cd457f3SChristophe Kerello if (IS_ERR(fmc2->cmd_base[chip_cs])) 1637*2cd457f3SChristophe Kerello return PTR_ERR(fmc2->cmd_base[chip_cs]); 1638*2cd457f3SChristophe Kerello 1639*2cd457f3SChristophe Kerello res = platform_get_resource(pdev, IORESOURCE_MEM, 1640*2cd457f3SChristophe Kerello mem_region + 2); 1641*2cd457f3SChristophe Kerello fmc2->addr_base[chip_cs] = devm_ioremap_resource(dev, res); 1642*2cd457f3SChristophe Kerello if (IS_ERR(fmc2->addr_base[chip_cs])) 1643*2cd457f3SChristophe Kerello return PTR_ERR(fmc2->addr_base[chip_cs]); 1644*2cd457f3SChristophe Kerello } 1645*2cd457f3SChristophe Kerello 1646*2cd457f3SChristophe Kerello irq = platform_get_irq(pdev, 0); 1647*2cd457f3SChristophe Kerello ret = devm_request_irq(dev, irq, stm32_fmc2_irq, 0, 1648*2cd457f3SChristophe Kerello dev_name(dev), fmc2); 1649*2cd457f3SChristophe Kerello if (ret) { 1650*2cd457f3SChristophe Kerello dev_err(dev, "failed to request irq\n"); 1651*2cd457f3SChristophe Kerello return ret; 1652*2cd457f3SChristophe Kerello } 1653*2cd457f3SChristophe Kerello 1654*2cd457f3SChristophe Kerello init_completion(&fmc2->complete); 1655*2cd457f3SChristophe Kerello 1656*2cd457f3SChristophe Kerello fmc2->clk = devm_clk_get(dev, NULL); 1657*2cd457f3SChristophe Kerello if (IS_ERR(fmc2->clk)) 1658*2cd457f3SChristophe Kerello return PTR_ERR(fmc2->clk); 1659*2cd457f3SChristophe Kerello 1660*2cd457f3SChristophe Kerello ret = clk_prepare_enable(fmc2->clk); 1661*2cd457f3SChristophe Kerello if (ret) { 1662*2cd457f3SChristophe Kerello dev_err(dev, "can not enable the clock\n"); 1663*2cd457f3SChristophe Kerello return ret; 1664*2cd457f3SChristophe Kerello } 1665*2cd457f3SChristophe Kerello 1666*2cd457f3SChristophe Kerello rstc = devm_reset_control_get(dev, NULL); 1667*2cd457f3SChristophe Kerello if (!IS_ERR(rstc)) { 1668*2cd457f3SChristophe Kerello reset_control_assert(rstc); 1669*2cd457f3SChristophe Kerello reset_control_deassert(rstc); 1670*2cd457f3SChristophe Kerello } 1671*2cd457f3SChristophe Kerello 1672*2cd457f3SChristophe Kerello /* DMA setup */ 1673*2cd457f3SChristophe Kerello ret = stm32_fmc2_dma_setup(fmc2); 1674*2cd457f3SChristophe Kerello if (ret) 1675*2cd457f3SChristophe Kerello return ret; 1676*2cd457f3SChristophe Kerello 1677*2cd457f3SChristophe Kerello /* FMC2 init routine */ 1678*2cd457f3SChristophe Kerello stm32_fmc2_init(fmc2); 1679*2cd457f3SChristophe Kerello 1680*2cd457f3SChristophe Kerello nand = &fmc2->nand; 1681*2cd457f3SChristophe Kerello chip = &nand->chip; 1682*2cd457f3SChristophe Kerello mtd = nand_to_mtd(chip); 1683*2cd457f3SChristophe Kerello mtd->dev.parent = dev; 1684*2cd457f3SChristophe Kerello 1685*2cd457f3SChristophe Kerello chip->controller = &fmc2->base; 1686*2cd457f3SChristophe Kerello chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE | 1687*2cd457f3SChristophe Kerello NAND_USE_BOUNCE_BUFFER; 1688*2cd457f3SChristophe Kerello 1689*2cd457f3SChristophe Kerello /* Default ECC settings */ 1690*2cd457f3SChristophe Kerello chip->ecc.mode = NAND_ECC_HW; 1691*2cd457f3SChristophe Kerello chip->ecc.size = FMC2_ECC_STEP_SIZE; 1692*2cd457f3SChristophe Kerello chip->ecc.strength = FMC2_ECC_BCH8; 1693*2cd457f3SChristophe Kerello 1694*2cd457f3SChristophe Kerello /* Scan to find existence of the device */ 1695*2cd457f3SChristophe Kerello ret = nand_scan(chip, nand->ncs); 1696*2cd457f3SChristophe Kerello if (ret) 1697*2cd457f3SChristophe Kerello goto err_scan; 1698*2cd457f3SChristophe Kerello 1699*2cd457f3SChristophe Kerello ret = mtd_device_register(mtd, NULL, 0); 1700*2cd457f3SChristophe Kerello if (ret) 1701*2cd457f3SChristophe Kerello goto err_device_register; 1702*2cd457f3SChristophe Kerello 1703*2cd457f3SChristophe Kerello platform_set_drvdata(pdev, fmc2); 1704*2cd457f3SChristophe Kerello 1705*2cd457f3SChristophe Kerello return 0; 1706*2cd457f3SChristophe Kerello 1707*2cd457f3SChristophe Kerello err_device_register: 1708*2cd457f3SChristophe Kerello nand_cleanup(chip); 1709*2cd457f3SChristophe Kerello 1710*2cd457f3SChristophe Kerello err_scan: 1711*2cd457f3SChristophe Kerello if (fmc2->dma_ecc_ch) 1712*2cd457f3SChristophe Kerello dma_release_channel(fmc2->dma_ecc_ch); 1713*2cd457f3SChristophe Kerello if (fmc2->dma_tx_ch) 1714*2cd457f3SChristophe Kerello dma_release_channel(fmc2->dma_tx_ch); 1715*2cd457f3SChristophe Kerello if (fmc2->dma_rx_ch) 1716*2cd457f3SChristophe Kerello dma_release_channel(fmc2->dma_rx_ch); 1717*2cd457f3SChristophe Kerello 1718*2cd457f3SChristophe Kerello sg_free_table(&fmc2->dma_data_sg); 1719*2cd457f3SChristophe Kerello sg_free_table(&fmc2->dma_ecc_sg); 1720*2cd457f3SChristophe Kerello 1721*2cd457f3SChristophe Kerello clk_disable_unprepare(fmc2->clk); 1722*2cd457f3SChristophe Kerello 1723*2cd457f3SChristophe Kerello return ret; 1724*2cd457f3SChristophe Kerello } 1725*2cd457f3SChristophe Kerello 1726*2cd457f3SChristophe Kerello static int stm32_fmc2_remove(struct platform_device *pdev) 1727*2cd457f3SChristophe Kerello { 1728*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = platform_get_drvdata(pdev); 1729*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand = &fmc2->nand; 1730*2cd457f3SChristophe Kerello 1731*2cd457f3SChristophe Kerello nand_release(&nand->chip); 1732*2cd457f3SChristophe Kerello 1733*2cd457f3SChristophe Kerello if (fmc2->dma_ecc_ch) 1734*2cd457f3SChristophe Kerello dma_release_channel(fmc2->dma_ecc_ch); 1735*2cd457f3SChristophe Kerello if (fmc2->dma_tx_ch) 1736*2cd457f3SChristophe Kerello dma_release_channel(fmc2->dma_tx_ch); 1737*2cd457f3SChristophe Kerello if (fmc2->dma_rx_ch) 1738*2cd457f3SChristophe Kerello dma_release_channel(fmc2->dma_rx_ch); 1739*2cd457f3SChristophe Kerello 1740*2cd457f3SChristophe Kerello sg_free_table(&fmc2->dma_data_sg); 1741*2cd457f3SChristophe Kerello sg_free_table(&fmc2->dma_ecc_sg); 1742*2cd457f3SChristophe Kerello 1743*2cd457f3SChristophe Kerello clk_disable_unprepare(fmc2->clk); 1744*2cd457f3SChristophe Kerello 1745*2cd457f3SChristophe Kerello return 0; 1746*2cd457f3SChristophe Kerello } 1747*2cd457f3SChristophe Kerello 1748*2cd457f3SChristophe Kerello static int __maybe_unused stm32_fmc2_suspend(struct device *dev) 1749*2cd457f3SChristophe Kerello { 1750*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = dev_get_drvdata(dev); 1751*2cd457f3SChristophe Kerello 1752*2cd457f3SChristophe Kerello clk_disable_unprepare(fmc2->clk); 1753*2cd457f3SChristophe Kerello 1754*2cd457f3SChristophe Kerello pinctrl_pm_select_sleep_state(dev); 1755*2cd457f3SChristophe Kerello 1756*2cd457f3SChristophe Kerello return 0; 1757*2cd457f3SChristophe Kerello } 1758*2cd457f3SChristophe Kerello 1759*2cd457f3SChristophe Kerello static int __maybe_unused stm32_fmc2_resume(struct device *dev) 1760*2cd457f3SChristophe Kerello { 1761*2cd457f3SChristophe Kerello struct stm32_fmc2_nfc *fmc2 = dev_get_drvdata(dev); 1762*2cd457f3SChristophe Kerello struct stm32_fmc2_nand *nand = &fmc2->nand; 1763*2cd457f3SChristophe Kerello int chip_cs, ret; 1764*2cd457f3SChristophe Kerello 1765*2cd457f3SChristophe Kerello pinctrl_pm_select_default_state(dev); 1766*2cd457f3SChristophe Kerello 1767*2cd457f3SChristophe Kerello ret = clk_prepare_enable(fmc2->clk); 1768*2cd457f3SChristophe Kerello if (ret) { 1769*2cd457f3SChristophe Kerello dev_err(dev, "can not enable the clock\n"); 1770*2cd457f3SChristophe Kerello return ret; 1771*2cd457f3SChristophe Kerello } 1772*2cd457f3SChristophe Kerello 1773*2cd457f3SChristophe Kerello stm32_fmc2_init(fmc2); 1774*2cd457f3SChristophe Kerello 1775*2cd457f3SChristophe Kerello for (chip_cs = 0; chip_cs < FMC2_MAX_CE; chip_cs++) { 1776*2cd457f3SChristophe Kerello if (!(fmc2->cs_assigned & BIT(chip_cs))) 1777*2cd457f3SChristophe Kerello continue; 1778*2cd457f3SChristophe Kerello 1779*2cd457f3SChristophe Kerello nand_reset(&nand->chip, chip_cs); 1780*2cd457f3SChristophe Kerello } 1781*2cd457f3SChristophe Kerello 1782*2cd457f3SChristophe Kerello return 0; 1783*2cd457f3SChristophe Kerello } 1784*2cd457f3SChristophe Kerello 1785*2cd457f3SChristophe Kerello static SIMPLE_DEV_PM_OPS(stm32_fmc2_pm_ops, stm32_fmc2_suspend, 1786*2cd457f3SChristophe Kerello stm32_fmc2_resume); 1787*2cd457f3SChristophe Kerello 1788*2cd457f3SChristophe Kerello static const struct of_device_id stm32_fmc2_match[] = { 1789*2cd457f3SChristophe Kerello {.compatible = "st,stm32mp15-fmc2"}, 1790*2cd457f3SChristophe Kerello {} 1791*2cd457f3SChristophe Kerello }; 1792*2cd457f3SChristophe Kerello MODULE_DEVICE_TABLE(of, stm32_fmc2_match); 1793*2cd457f3SChristophe Kerello 1794*2cd457f3SChristophe Kerello static struct platform_driver stm32_fmc2_driver = { 1795*2cd457f3SChristophe Kerello .probe = stm32_fmc2_probe, 1796*2cd457f3SChristophe Kerello .remove = stm32_fmc2_remove, 1797*2cd457f3SChristophe Kerello .driver = { 1798*2cd457f3SChristophe Kerello .name = "stm32_fmc2_nand", 1799*2cd457f3SChristophe Kerello .of_match_table = stm32_fmc2_match, 1800*2cd457f3SChristophe Kerello .pm = &stm32_fmc2_pm_ops, 1801*2cd457f3SChristophe Kerello }, 1802*2cd457f3SChristophe Kerello }; 1803*2cd457f3SChristophe Kerello module_platform_driver(stm32_fmc2_driver); 1804*2cd457f3SChristophe Kerello 1805*2cd457f3SChristophe Kerello MODULE_ALIAS("platform:stm32_fmc2_nand"); 1806*2cd457f3SChristophe Kerello MODULE_AUTHOR("Christophe Kerello <christophe.kerello@st.com>"); 1807*2cd457f3SChristophe Kerello MODULE_DESCRIPTION("STMicroelectronics STM32 FMC2 nand driver"); 1808*2cd457f3SChristophe Kerello MODULE_LICENSE("GPL v2"); 1809