Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) STMicroelectronics 2019 |
| 4 | * Author: Christophe Kerello <christophe.kerello@st.com> |
| 5 | */ |
| 6 | |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_MTD |
| 8 | |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 13 | #include <nand.h> |
| 14 | #include <reset.h> |
Christophe Kerello | e389a15 | 2022-02-22 17:38:49 +0100 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 17 | #include <linux/bitfield.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 21 | #include <linux/iopoll.h> |
| 22 | #include <linux/ioport.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 23 | #include <linux/mtd/rawnand.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 24 | #include <linux/printk.h> |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 25 | |
| 26 | /* Bad block marker length */ |
| 27 | #define FMC2_BBM_LEN 2 |
| 28 | |
| 29 | /* ECC step size */ |
| 30 | #define FMC2_ECC_STEP_SIZE 512 |
| 31 | |
| 32 | /* Command delay */ |
| 33 | #define FMC2_RB_DELAY_US 30 |
| 34 | |
| 35 | /* Max chip enable */ |
| 36 | #define FMC2_MAX_CE 2 |
| 37 | |
| 38 | /* Timings */ |
| 39 | #define FMC2_THIZ 1 |
| 40 | #define FMC2_TIO 8000 |
| 41 | #define FMC2_TSYNC 3000 |
| 42 | #define FMC2_PCR_TIMING_MASK 0xf |
| 43 | #define FMC2_PMEM_PATT_TIMING_MASK 0xff |
| 44 | |
| 45 | /* FMC2 Controller Registers */ |
| 46 | #define FMC2_BCR1 0x0 |
| 47 | #define FMC2_PCR 0x80 |
| 48 | #define FMC2_SR 0x84 |
| 49 | #define FMC2_PMEM 0x88 |
| 50 | #define FMC2_PATT 0x8c |
| 51 | #define FMC2_HECCR 0x94 |
| 52 | #define FMC2_BCHISR 0x254 |
| 53 | #define FMC2_BCHICR 0x258 |
| 54 | #define FMC2_BCHPBR1 0x260 |
| 55 | #define FMC2_BCHPBR2 0x264 |
| 56 | #define FMC2_BCHPBR3 0x268 |
| 57 | #define FMC2_BCHPBR4 0x26c |
| 58 | #define FMC2_BCHDSR0 0x27c |
| 59 | #define FMC2_BCHDSR1 0x280 |
| 60 | #define FMC2_BCHDSR2 0x284 |
| 61 | #define FMC2_BCHDSR3 0x288 |
| 62 | #define FMC2_BCHDSR4 0x28c |
| 63 | |
| 64 | /* Register: FMC2_BCR1 */ |
| 65 | #define FMC2_BCR1_FMC2EN BIT(31) |
| 66 | |
| 67 | /* Register: FMC2_PCR */ |
| 68 | #define FMC2_PCR_PWAITEN BIT(1) |
| 69 | #define FMC2_PCR_PBKEN BIT(2) |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 70 | #define FMC2_PCR_PWID GENMASK(5, 4) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 71 | #define FMC2_PCR_PWID_BUSWIDTH_8 0 |
| 72 | #define FMC2_PCR_PWID_BUSWIDTH_16 1 |
| 73 | #define FMC2_PCR_ECCEN BIT(6) |
| 74 | #define FMC2_PCR_ECCALG BIT(8) |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 75 | #define FMC2_PCR_TCLR GENMASK(12, 9) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 76 | #define FMC2_PCR_TCLR_DEFAULT 0xf |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 77 | #define FMC2_PCR_TAR GENMASK(16, 13) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 78 | #define FMC2_PCR_TAR_DEFAULT 0xf |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 79 | #define FMC2_PCR_ECCSS GENMASK(19, 17) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 80 | #define FMC2_PCR_ECCSS_512 1 |
| 81 | #define FMC2_PCR_ECCSS_2048 3 |
| 82 | #define FMC2_PCR_BCHECC BIT(24) |
| 83 | #define FMC2_PCR_WEN BIT(25) |
| 84 | |
| 85 | /* Register: FMC2_SR */ |
| 86 | #define FMC2_SR_NWRF BIT(6) |
| 87 | |
| 88 | /* Register: FMC2_PMEM */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 89 | #define FMC2_PMEM_MEMSET GENMASK(7, 0) |
| 90 | #define FMC2_PMEM_MEMWAIT GENMASK(15, 8) |
| 91 | #define FMC2_PMEM_MEMHOLD GENMASK(23, 16) |
| 92 | #define FMC2_PMEM_MEMHIZ GENMASK(31, 24) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 93 | #define FMC2_PMEM_DEFAULT 0x0a0a0a0a |
| 94 | |
| 95 | /* Register: FMC2_PATT */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 96 | #define FMC2_PATT_ATTSET GENMASK(7, 0) |
| 97 | #define FMC2_PATT_ATTWAIT GENMASK(15, 8) |
| 98 | #define FMC2_PATT_ATTHOLD GENMASK(23, 16) |
| 99 | #define FMC2_PATT_ATTHIZ GENMASK(31, 24) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 100 | #define FMC2_PATT_DEFAULT 0x0a0a0a0a |
| 101 | |
| 102 | /* Register: FMC2_BCHISR */ |
| 103 | #define FMC2_BCHISR_DERF BIT(1) |
| 104 | #define FMC2_BCHISR_EPBRF BIT(4) |
| 105 | |
| 106 | /* Register: FMC2_BCHICR */ |
| 107 | #define FMC2_BCHICR_CLEAR_IRQ GENMASK(4, 0) |
| 108 | |
| 109 | /* Register: FMC2_BCHDSR0 */ |
| 110 | #define FMC2_BCHDSR0_DUE BIT(0) |
| 111 | #define FMC2_BCHDSR0_DEF BIT(1) |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 112 | #define FMC2_BCHDSR0_DEN GENMASK(7, 4) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 113 | |
| 114 | /* Register: FMC2_BCHDSR1 */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 115 | #define FMC2_BCHDSR1_EBP1 GENMASK(12, 0) |
| 116 | #define FMC2_BCHDSR1_EBP2 GENMASK(28, 16) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 117 | |
| 118 | /* Register: FMC2_BCHDSR2 */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 119 | #define FMC2_BCHDSR2_EBP3 GENMASK(12, 0) |
| 120 | #define FMC2_BCHDSR2_EBP4 GENMASK(28, 16) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 121 | |
| 122 | /* Register: FMC2_BCHDSR3 */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 123 | #define FMC2_BCHDSR3_EBP5 GENMASK(12, 0) |
| 124 | #define FMC2_BCHDSR3_EBP6 GENMASK(28, 16) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 125 | |
| 126 | /* Register: FMC2_BCHDSR4 */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 127 | #define FMC2_BCHDSR4_EBP7 GENMASK(12, 0) |
| 128 | #define FMC2_BCHDSR4_EBP8 GENMASK(28, 16) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 129 | |
| 130 | #define FMC2_NSEC_PER_SEC 1000000000L |
| 131 | |
Christophe Kerello | 92693e3 | 2020-07-31 09:53:36 +0200 | [diff] [blame] | 132 | #define FMC2_TIMEOUT_5S 5000000 |
| 133 | |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 134 | enum stm32_fmc2_ecc { |
| 135 | FMC2_ECC_HAM = 1, |
| 136 | FMC2_ECC_BCH4 = 4, |
| 137 | FMC2_ECC_BCH8 = 8 |
| 138 | }; |
| 139 | |
| 140 | struct stm32_fmc2_timings { |
| 141 | u8 tclr; |
| 142 | u8 tar; |
| 143 | u8 thiz; |
| 144 | u8 twait; |
| 145 | u8 thold_mem; |
| 146 | u8 tset_mem; |
| 147 | u8 thold_att; |
| 148 | u8 tset_att; |
| 149 | }; |
| 150 | |
| 151 | struct stm32_fmc2_nand { |
| 152 | struct nand_chip chip; |
| 153 | struct stm32_fmc2_timings timings; |
Christophe Kerello | e389a15 | 2022-02-22 17:38:49 +0100 | [diff] [blame] | 154 | struct gpio_desc wp_gpio; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 155 | int ncs; |
| 156 | int cs_used[FMC2_MAX_CE]; |
| 157 | }; |
| 158 | |
| 159 | static inline struct stm32_fmc2_nand *to_fmc2_nand(struct nand_chip *chip) |
| 160 | { |
| 161 | return container_of(chip, struct stm32_fmc2_nand, chip); |
| 162 | } |
| 163 | |
| 164 | struct stm32_fmc2_nfc { |
| 165 | struct nand_hw_control base; |
| 166 | struct stm32_fmc2_nand nand; |
| 167 | struct nand_ecclayout ecclayout; |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 168 | fdt_addr_t io_base; |
| 169 | fdt_addr_t data_base[FMC2_MAX_CE]; |
| 170 | fdt_addr_t cmd_base[FMC2_MAX_CE]; |
| 171 | fdt_addr_t addr_base[FMC2_MAX_CE]; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 172 | struct clk clk; |
| 173 | |
| 174 | u8 cs_assigned; |
| 175 | int cs_sel; |
| 176 | }; |
| 177 | |
| 178 | static inline struct stm32_fmc2_nfc *to_stm32_nfc(struct nand_hw_control *base) |
| 179 | { |
| 180 | return container_of(base, struct stm32_fmc2_nfc, base); |
| 181 | } |
| 182 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 183 | static void stm32_fmc2_nfc_timings_init(struct nand_chip *chip) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 184 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 185 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 186 | struct stm32_fmc2_nand *nand = to_fmc2_nand(chip); |
| 187 | struct stm32_fmc2_timings *timings = &nand->timings; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 188 | u32 pmem, patt; |
| 189 | |
| 190 | /* Set tclr/tar timings */ |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 191 | clrsetbits_le32(nfc->io_base + FMC2_PCR, |
| 192 | FMC2_PCR_TCLR | FMC2_PCR_TAR, |
| 193 | FIELD_PREP(FMC2_PCR_TCLR, timings->tclr) | |
| 194 | FIELD_PREP(FMC2_PCR_TAR, timings->tar)); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 195 | |
| 196 | /* Set tset/twait/thold/thiz timings in common bank */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 197 | pmem = FIELD_PREP(FMC2_PMEM_MEMSET, timings->tset_mem); |
| 198 | pmem |= FIELD_PREP(FMC2_PMEM_MEMWAIT, timings->twait); |
| 199 | pmem |= FIELD_PREP(FMC2_PMEM_MEMHOLD, timings->thold_mem); |
| 200 | pmem |= FIELD_PREP(FMC2_PMEM_MEMHIZ, timings->thiz); |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 201 | writel(pmem, nfc->io_base + FMC2_PMEM); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 202 | |
| 203 | /* Set tset/twait/thold/thiz timings in attribut bank */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 204 | patt = FIELD_PREP(FMC2_PATT_ATTSET, timings->tset_att); |
| 205 | patt |= FIELD_PREP(FMC2_PATT_ATTWAIT, timings->twait); |
| 206 | patt |= FIELD_PREP(FMC2_PATT_ATTHOLD, timings->thold_att); |
| 207 | patt |= FIELD_PREP(FMC2_PATT_ATTHIZ, timings->thiz); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 208 | writel(patt, nfc->io_base + FMC2_PATT); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 211 | static void stm32_fmc2_nfc_setup(struct nand_chip *chip) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 212 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 213 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 214 | u32 pcr = 0, pcr_mask; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 215 | |
| 216 | /* Configure ECC algorithm (default configuration is Hamming) */ |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 217 | pcr_mask = FMC2_PCR_ECCALG; |
| 218 | pcr_mask |= FMC2_PCR_BCHECC; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 219 | if (chip->ecc.strength == FMC2_ECC_BCH8) { |
| 220 | pcr |= FMC2_PCR_ECCALG; |
| 221 | pcr |= FMC2_PCR_BCHECC; |
| 222 | } else if (chip->ecc.strength == FMC2_ECC_BCH4) { |
| 223 | pcr |= FMC2_PCR_ECCALG; |
| 224 | } |
| 225 | |
| 226 | /* Set buswidth */ |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 227 | pcr_mask |= FMC2_PCR_PWID; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 228 | if (chip->options & NAND_BUSWIDTH_16) |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 229 | pcr |= FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 230 | |
| 231 | /* Set ECC sector size */ |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 232 | pcr_mask |= FMC2_PCR_ECCSS; |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 233 | pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_512); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 234 | |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 235 | clrsetbits_le32(nfc->io_base + FMC2_PCR, pcr_mask, pcr); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 238 | static void stm32_fmc2_nfc_select_chip(struct mtd_info *mtd, int chipnr) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 239 | { |
| 240 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 241 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 242 | struct stm32_fmc2_nand *nand = to_fmc2_nand(chip); |
| 243 | |
| 244 | if (chipnr < 0 || chipnr >= nand->ncs) |
| 245 | return; |
| 246 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 247 | if (nand->cs_used[chipnr] == nfc->cs_sel) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 248 | return; |
| 249 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 250 | nfc->cs_sel = nand->cs_used[chipnr]; |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 251 | chip->IO_ADDR_R = (void __iomem *)nfc->data_base[nfc->cs_sel]; |
| 252 | chip->IO_ADDR_W = (void __iomem *)nfc->data_base[nfc->cs_sel]; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 253 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 254 | stm32_fmc2_nfc_setup(chip); |
| 255 | stm32_fmc2_nfc_timings_init(chip); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 258 | static void stm32_fmc2_nfc_set_buswidth_16(struct stm32_fmc2_nfc *nfc, |
| 259 | bool set) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 260 | { |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 261 | u32 pcr; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 262 | |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 263 | pcr = set ? FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_16) : |
| 264 | FIELD_PREP(FMC2_PCR_PWID, FMC2_PCR_PWID_BUSWIDTH_8); |
| 265 | |
| 266 | clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_PWID, pcr); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 269 | static void stm32_fmc2_nfc_set_ecc(struct stm32_fmc2_nfc *nfc, bool enable) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 270 | { |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 271 | clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_ECCEN, |
| 272 | enable ? FMC2_PCR_ECCEN : 0); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 275 | static void stm32_fmc2_nfc_clear_bch_irq(struct stm32_fmc2_nfc *nfc) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 276 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 277 | writel(FMC2_BCHICR_CLEAR_IRQ, nfc->io_base + FMC2_BCHICR); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 280 | static void stm32_fmc2_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd, |
| 281 | unsigned int ctrl) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 282 | { |
| 283 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 284 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 285 | |
| 286 | if (cmd == NAND_CMD_NONE) |
| 287 | return; |
| 288 | |
| 289 | if (ctrl & NAND_CLE) { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 290 | writeb(cmd, nfc->cmd_base[nfc->cs_sel]); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 294 | writeb(cmd, nfc->addr_base[nfc->cs_sel]); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Enable ECC logic and reset syndrome/parity bits previously calculated |
| 299 | * Syndrome/parity bits is cleared by setting the ECCEN bit to 0 |
| 300 | */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 301 | static void stm32_fmc2_nfc_hwctl(struct mtd_info *mtd, int mode) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 302 | { |
| 303 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 304 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 305 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 306 | stm32_fmc2_nfc_set_ecc(nfc, false); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 307 | |
| 308 | if (chip->ecc.strength != FMC2_ECC_HAM) { |
Christophe Kerello | 9de081d | 2020-07-31 09:53:39 +0200 | [diff] [blame] | 309 | clrsetbits_le32(nfc->io_base + FMC2_PCR, FMC2_PCR_WEN, |
| 310 | mode == NAND_ECC_WRITE ? FMC2_PCR_WEN : 0); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 311 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 312 | stm32_fmc2_nfc_clear_bch_irq(nfc); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 315 | stm32_fmc2_nfc_set_ecc(nfc, true); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
| 319 | * ECC Hamming calculation |
| 320 | * ECC is 3 bytes for 512 bytes of data (supports error correction up to |
| 321 | * max of 1-bit) |
| 322 | */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 323 | static int stm32_fmc2_nfc_ham_calculate(struct mtd_info *mtd, const u8 *data, |
| 324 | u8 *ecc) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 325 | { |
| 326 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 327 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 328 | u32 heccr, sr; |
| 329 | int ret; |
| 330 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 331 | ret = readl_poll_timeout(nfc->io_base + FMC2_SR, sr, |
Christophe Kerello | 92693e3 | 2020-07-31 09:53:36 +0200 | [diff] [blame] | 332 | sr & FMC2_SR_NWRF, FMC2_TIMEOUT_5S); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 333 | if (ret < 0) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 334 | log_err("Ham timeout\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 335 | return ret; |
| 336 | } |
| 337 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 338 | heccr = readl(nfc->io_base + FMC2_HECCR); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 339 | |
| 340 | ecc[0] = heccr; |
| 341 | ecc[1] = heccr >> 8; |
| 342 | ecc[2] = heccr >> 16; |
| 343 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 344 | stm32_fmc2_nfc_set_ecc(nfc, false); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 349 | static int stm32_fmc2_nfc_ham_correct(struct mtd_info *mtd, u8 *dat, |
| 350 | u8 *read_ecc, u8 *calc_ecc) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 351 | { |
| 352 | u8 bit_position = 0, b0, b1, b2; |
| 353 | u32 byte_addr = 0, b; |
| 354 | u32 i, shifting = 1; |
| 355 | |
| 356 | /* Indicate which bit and byte is faulty (if any) */ |
| 357 | b0 = read_ecc[0] ^ calc_ecc[0]; |
| 358 | b1 = read_ecc[1] ^ calc_ecc[1]; |
| 359 | b2 = read_ecc[2] ^ calc_ecc[2]; |
| 360 | b = b0 | (b1 << 8) | (b2 << 16); |
| 361 | |
| 362 | /* No errors */ |
| 363 | if (likely(!b)) |
| 364 | return 0; |
| 365 | |
| 366 | /* Calculate bit position */ |
| 367 | for (i = 0; i < 3; i++) { |
| 368 | switch (b % 4) { |
| 369 | case 2: |
| 370 | bit_position += shifting; |
| 371 | case 1: |
| 372 | break; |
| 373 | default: |
| 374 | return -EBADMSG; |
| 375 | } |
| 376 | shifting <<= 1; |
| 377 | b >>= 2; |
| 378 | } |
| 379 | |
| 380 | /* Calculate byte position */ |
| 381 | shifting = 1; |
| 382 | for (i = 0; i < 9; i++) { |
| 383 | switch (b % 4) { |
| 384 | case 2: |
| 385 | byte_addr += shifting; |
| 386 | case 1: |
| 387 | break; |
| 388 | default: |
| 389 | return -EBADMSG; |
| 390 | } |
| 391 | shifting <<= 1; |
| 392 | b >>= 2; |
| 393 | } |
| 394 | |
| 395 | /* Flip the bit */ |
| 396 | dat[byte_addr] ^= (1 << bit_position); |
| 397 | |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * ECC BCH calculation and correction |
| 403 | * ECC is 7/13 bytes for 512 bytes of data (supports error correction up to |
| 404 | * max of 4-bit/8-bit) |
| 405 | */ |
| 406 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 407 | static int stm32_fmc2_nfc_bch_calculate(struct mtd_info *mtd, const u8 *data, |
| 408 | u8 *ecc) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 409 | { |
| 410 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 411 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 412 | u32 bchpbr, bchisr; |
| 413 | int ret; |
| 414 | |
| 415 | /* Wait until the BCH code is ready */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 416 | ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr, |
Christophe Kerello | 92693e3 | 2020-07-31 09:53:36 +0200 | [diff] [blame] | 417 | bchisr & FMC2_BCHISR_EPBRF, FMC2_TIMEOUT_5S); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 418 | if (ret < 0) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 419 | log_err("Bch timeout\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | /* Read parity bits */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 424 | bchpbr = readl(nfc->io_base + FMC2_BCHPBR1); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 425 | ecc[0] = bchpbr; |
| 426 | ecc[1] = bchpbr >> 8; |
| 427 | ecc[2] = bchpbr >> 16; |
| 428 | ecc[3] = bchpbr >> 24; |
| 429 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 430 | bchpbr = readl(nfc->io_base + FMC2_BCHPBR2); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 431 | ecc[4] = bchpbr; |
| 432 | ecc[5] = bchpbr >> 8; |
| 433 | ecc[6] = bchpbr >> 16; |
| 434 | |
| 435 | if (chip->ecc.strength == FMC2_ECC_BCH8) { |
| 436 | ecc[7] = bchpbr >> 24; |
| 437 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 438 | bchpbr = readl(nfc->io_base + FMC2_BCHPBR3); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 439 | ecc[8] = bchpbr; |
| 440 | ecc[9] = bchpbr >> 8; |
| 441 | ecc[10] = bchpbr >> 16; |
| 442 | ecc[11] = bchpbr >> 24; |
| 443 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 444 | bchpbr = readl(nfc->io_base + FMC2_BCHPBR4); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 445 | ecc[12] = bchpbr; |
| 446 | } |
| 447 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 448 | stm32_fmc2_nfc_set_ecc(nfc, false); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 453 | static int stm32_fmc2_nfc_bch_correct(struct mtd_info *mtd, u8 *dat, |
| 454 | u8 *read_ecc, u8 *calc_ecc) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 455 | { |
| 456 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 457 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 458 | u32 bchdsr0, bchdsr1, bchdsr2, bchdsr3, bchdsr4, bchisr; |
| 459 | u16 pos[8]; |
| 460 | int i, ret, den, eccsize = chip->ecc.size; |
| 461 | unsigned int nb_errs = 0; |
| 462 | |
| 463 | /* Wait until the decoding error is ready */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 464 | ret = readl_poll_timeout(nfc->io_base + FMC2_BCHISR, bchisr, |
Christophe Kerello | 92693e3 | 2020-07-31 09:53:36 +0200 | [diff] [blame] | 465 | bchisr & FMC2_BCHISR_DERF, FMC2_TIMEOUT_5S); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 466 | if (ret < 0) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 467 | log_err("Bch timeout\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 468 | return ret; |
| 469 | } |
| 470 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 471 | bchdsr0 = readl(nfc->io_base + FMC2_BCHDSR0); |
| 472 | bchdsr1 = readl(nfc->io_base + FMC2_BCHDSR1); |
| 473 | bchdsr2 = readl(nfc->io_base + FMC2_BCHDSR2); |
| 474 | bchdsr3 = readl(nfc->io_base + FMC2_BCHDSR3); |
| 475 | bchdsr4 = readl(nfc->io_base + FMC2_BCHDSR4); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 476 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 477 | stm32_fmc2_nfc_set_ecc(nfc, false); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 478 | |
| 479 | /* No errors found */ |
| 480 | if (likely(!(bchdsr0 & FMC2_BCHDSR0_DEF))) |
| 481 | return 0; |
| 482 | |
| 483 | /* Too many errors detected */ |
| 484 | if (unlikely(bchdsr0 & FMC2_BCHDSR0_DUE)) |
| 485 | return -EBADMSG; |
| 486 | |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 487 | pos[0] = FIELD_GET(FMC2_BCHDSR1_EBP1, bchdsr1); |
| 488 | pos[1] = FIELD_GET(FMC2_BCHDSR1_EBP2, bchdsr1); |
| 489 | pos[2] = FIELD_GET(FMC2_BCHDSR2_EBP3, bchdsr2); |
| 490 | pos[3] = FIELD_GET(FMC2_BCHDSR2_EBP4, bchdsr2); |
| 491 | pos[4] = FIELD_GET(FMC2_BCHDSR3_EBP5, bchdsr3); |
| 492 | pos[5] = FIELD_GET(FMC2_BCHDSR3_EBP6, bchdsr3); |
| 493 | pos[6] = FIELD_GET(FMC2_BCHDSR4_EBP7, bchdsr4); |
| 494 | pos[7] = FIELD_GET(FMC2_BCHDSR4_EBP8, bchdsr4); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 495 | |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 496 | den = FIELD_GET(FMC2_BCHDSR0_DEN, bchdsr0); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 497 | for (i = 0; i < den; i++) { |
| 498 | if (pos[i] < eccsize * 8) { |
| 499 | __change_bit(pos[i], (unsigned long *)dat); |
| 500 | nb_errs++; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return nb_errs; |
| 505 | } |
| 506 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 507 | static int stm32_fmc2_nfc_read_page(struct mtd_info *mtd, |
| 508 | struct nand_chip *chip, u8 *buf, |
| 509 | int oob_required, int page) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 510 | { |
| 511 | int i, s, stat, eccsize = chip->ecc.size; |
| 512 | int eccbytes = chip->ecc.bytes; |
| 513 | int eccsteps = chip->ecc.steps; |
| 514 | int eccstrength = chip->ecc.strength; |
| 515 | u8 *p = buf; |
| 516 | u8 *ecc_calc = chip->buffers->ecccalc; |
| 517 | u8 *ecc_code = chip->buffers->ecccode; |
| 518 | unsigned int max_bitflips = 0; |
| 519 | |
| 520 | for (i = mtd->writesize + FMC2_BBM_LEN, s = 0; s < eccsteps; |
| 521 | s++, i += eccbytes, p += eccsize) { |
| 522 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 523 | |
| 524 | /* Read the nand page sector (512 bytes) */ |
| 525 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, s * eccsize, -1); |
| 526 | chip->read_buf(mtd, p, eccsize); |
| 527 | |
| 528 | /* Read the corresponding ECC bytes */ |
| 529 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i, -1); |
| 530 | chip->read_buf(mtd, ecc_code, eccbytes); |
| 531 | |
| 532 | /* Correct the data */ |
| 533 | stat = chip->ecc.correct(mtd, p, ecc_code, ecc_calc); |
| 534 | if (stat == -EBADMSG) |
| 535 | /* Check for empty pages with bitflips */ |
| 536 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 537 | ecc_code, eccbytes, |
| 538 | NULL, 0, |
| 539 | eccstrength); |
| 540 | |
| 541 | if (stat < 0) { |
| 542 | mtd->ecc_stats.failed++; |
| 543 | } else { |
| 544 | mtd->ecc_stats.corrected += stat; |
| 545 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /* Read oob */ |
| 550 | if (oob_required) { |
| 551 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 552 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 553 | } |
| 554 | |
| 555 | return max_bitflips; |
| 556 | } |
| 557 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 558 | static void stm32_fmc2_nfc_init(struct stm32_fmc2_nfc *nfc, bool has_parent) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 559 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 560 | u32 pcr = readl(nfc->io_base + FMC2_PCR); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 561 | |
| 562 | /* Set CS used to undefined */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 563 | nfc->cs_sel = -1; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 564 | |
| 565 | /* Enable wait feature and nand flash memory bank */ |
| 566 | pcr |= FMC2_PCR_PWAITEN; |
| 567 | pcr |= FMC2_PCR_PBKEN; |
| 568 | |
| 569 | /* Set buswidth to 8 bits mode for identification */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 570 | pcr &= ~FMC2_PCR_PWID; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 571 | |
| 572 | /* ECC logic is disabled */ |
| 573 | pcr &= ~FMC2_PCR_ECCEN; |
| 574 | |
| 575 | /* Default mode */ |
| 576 | pcr &= ~FMC2_PCR_ECCALG; |
| 577 | pcr &= ~FMC2_PCR_BCHECC; |
| 578 | pcr &= ~FMC2_PCR_WEN; |
| 579 | |
| 580 | /* Set default ECC sector size */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 581 | pcr &= ~FMC2_PCR_ECCSS; |
| 582 | pcr |= FIELD_PREP(FMC2_PCR_ECCSS, FMC2_PCR_ECCSS_2048); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 583 | |
| 584 | /* Set default tclr/tar timings */ |
Christophe Kerello | f4aca87 | 2020-07-31 09:53:38 +0200 | [diff] [blame] | 585 | pcr &= ~FMC2_PCR_TCLR; |
| 586 | pcr |= FIELD_PREP(FMC2_PCR_TCLR, FMC2_PCR_TCLR_DEFAULT); |
| 587 | pcr &= ~FMC2_PCR_TAR; |
| 588 | pcr |= FIELD_PREP(FMC2_PCR_TAR, FMC2_PCR_TAR_DEFAULT); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 589 | |
| 590 | /* Enable FMC2 controller */ |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 591 | if (!has_parent) |
| 592 | setbits_le32(nfc->io_base + FMC2_BCR1, FMC2_BCR1_FMC2EN); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 593 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 594 | writel(pcr, nfc->io_base + FMC2_PCR); |
| 595 | writel(FMC2_PMEM_DEFAULT, nfc->io_base + FMC2_PMEM); |
| 596 | writel(FMC2_PATT_DEFAULT, nfc->io_base + FMC2_PATT); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 599 | static void stm32_fmc2_nfc_calc_timings(struct nand_chip *chip, |
| 600 | const struct nand_sdr_timings *sdrt) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 601 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 602 | struct stm32_fmc2_nfc *nfc = to_stm32_nfc(chip->controller); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 603 | struct stm32_fmc2_nand *nand = to_fmc2_nand(chip); |
| 604 | struct stm32_fmc2_timings *tims = &nand->timings; |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 605 | unsigned long hclk = clk_get_rate(&nfc->clk); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 606 | unsigned long hclkp = FMC2_NSEC_PER_SEC / (hclk / 1000); |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 607 | unsigned long timing, tar, tclr, thiz, twait; |
| 608 | unsigned long tset_mem, tset_att, thold_mem, thold_att; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 609 | |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 610 | tar = max_t(unsigned long, hclkp, sdrt->tAR_min); |
| 611 | timing = DIV_ROUND_UP(tar, hclkp) - 1; |
| 612 | tims->tar = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 613 | |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 614 | tclr = max_t(unsigned long, hclkp, sdrt->tCLR_min); |
| 615 | timing = DIV_ROUND_UP(tclr, hclkp) - 1; |
| 616 | tims->tclr = min_t(unsigned long, timing, FMC2_PCR_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 617 | |
| 618 | tims->thiz = FMC2_THIZ; |
| 619 | thiz = (tims->thiz + 1) * hclkp; |
| 620 | |
| 621 | /* |
| 622 | * tWAIT > tRP |
| 623 | * tWAIT > tWP |
| 624 | * tWAIT > tREA + tIO |
| 625 | */ |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 626 | twait = max_t(unsigned long, hclkp, sdrt->tRP_min); |
| 627 | twait = max_t(unsigned long, twait, sdrt->tWP_min); |
| 628 | twait = max_t(unsigned long, twait, sdrt->tREA_max + FMC2_TIO); |
| 629 | timing = DIV_ROUND_UP(twait, hclkp); |
| 630 | tims->twait = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 631 | |
| 632 | /* |
| 633 | * tSETUP_MEM > tCS - tWAIT |
| 634 | * tSETUP_MEM > tALS - tWAIT |
| 635 | * tSETUP_MEM > tDS - (tWAIT - tHIZ) |
| 636 | */ |
| 637 | tset_mem = hclkp; |
| 638 | if (sdrt->tCS_min > twait && (tset_mem < sdrt->tCS_min - twait)) |
| 639 | tset_mem = sdrt->tCS_min - twait; |
| 640 | if (sdrt->tALS_min > twait && (tset_mem < sdrt->tALS_min - twait)) |
| 641 | tset_mem = sdrt->tALS_min - twait; |
| 642 | if (twait > thiz && (sdrt->tDS_min > twait - thiz) && |
| 643 | (tset_mem < sdrt->tDS_min - (twait - thiz))) |
| 644 | tset_mem = sdrt->tDS_min - (twait - thiz); |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 645 | timing = DIV_ROUND_UP(tset_mem, hclkp); |
| 646 | tims->tset_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * tHOLD_MEM > tCH |
| 650 | * tHOLD_MEM > tREH - tSETUP_MEM |
| 651 | * tHOLD_MEM > max(tRC, tWC) - (tSETUP_MEM + tWAIT) |
| 652 | */ |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 653 | thold_mem = max_t(unsigned long, hclkp, sdrt->tCH_min); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 654 | if (sdrt->tREH_min > tset_mem && |
| 655 | (thold_mem < sdrt->tREH_min - tset_mem)) |
| 656 | thold_mem = sdrt->tREH_min - tset_mem; |
| 657 | if ((sdrt->tRC_min > tset_mem + twait) && |
| 658 | (thold_mem < sdrt->tRC_min - (tset_mem + twait))) |
| 659 | thold_mem = sdrt->tRC_min - (tset_mem + twait); |
| 660 | if ((sdrt->tWC_min > tset_mem + twait) && |
| 661 | (thold_mem < sdrt->tWC_min - (tset_mem + twait))) |
| 662 | thold_mem = sdrt->tWC_min - (tset_mem + twait); |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 663 | timing = DIV_ROUND_UP(thold_mem, hclkp); |
| 664 | tims->thold_mem = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 665 | |
| 666 | /* |
| 667 | * tSETUP_ATT > tCS - tWAIT |
| 668 | * tSETUP_ATT > tCLS - tWAIT |
| 669 | * tSETUP_ATT > tALS - tWAIT |
| 670 | * tSETUP_ATT > tRHW - tHOLD_MEM |
| 671 | * tSETUP_ATT > tDS - (tWAIT - tHIZ) |
| 672 | */ |
| 673 | tset_att = hclkp; |
| 674 | if (sdrt->tCS_min > twait && (tset_att < sdrt->tCS_min - twait)) |
| 675 | tset_att = sdrt->tCS_min - twait; |
| 676 | if (sdrt->tCLS_min > twait && (tset_att < sdrt->tCLS_min - twait)) |
| 677 | tset_att = sdrt->tCLS_min - twait; |
| 678 | if (sdrt->tALS_min > twait && (tset_att < sdrt->tALS_min - twait)) |
| 679 | tset_att = sdrt->tALS_min - twait; |
| 680 | if (sdrt->tRHW_min > thold_mem && |
| 681 | (tset_att < sdrt->tRHW_min - thold_mem)) |
| 682 | tset_att = sdrt->tRHW_min - thold_mem; |
| 683 | if (twait > thiz && (sdrt->tDS_min > twait - thiz) && |
| 684 | (tset_att < sdrt->tDS_min - (twait - thiz))) |
| 685 | tset_att = sdrt->tDS_min - (twait - thiz); |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 686 | timing = DIV_ROUND_UP(tset_att, hclkp); |
| 687 | tims->tset_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 688 | |
| 689 | /* |
| 690 | * tHOLD_ATT > tALH |
| 691 | * tHOLD_ATT > tCH |
| 692 | * tHOLD_ATT > tCLH |
| 693 | * tHOLD_ATT > tCOH |
| 694 | * tHOLD_ATT > tDH |
| 695 | * tHOLD_ATT > tWB + tIO + tSYNC - tSETUP_MEM |
| 696 | * tHOLD_ATT > tADL - tSETUP_MEM |
| 697 | * tHOLD_ATT > tWH - tSETUP_MEM |
| 698 | * tHOLD_ATT > tWHR - tSETUP_MEM |
| 699 | * tHOLD_ATT > tRC - (tSETUP_ATT + tWAIT) |
| 700 | * tHOLD_ATT > tWC - (tSETUP_ATT + tWAIT) |
| 701 | */ |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 702 | thold_att = max_t(unsigned long, hclkp, sdrt->tALH_min); |
| 703 | thold_att = max_t(unsigned long, thold_att, sdrt->tCH_min); |
| 704 | thold_att = max_t(unsigned long, thold_att, sdrt->tCLH_min); |
| 705 | thold_att = max_t(unsigned long, thold_att, sdrt->tCOH_min); |
| 706 | thold_att = max_t(unsigned long, thold_att, sdrt->tDH_min); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 707 | if ((sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC > tset_mem) && |
| 708 | (thold_att < sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem)) |
| 709 | thold_att = sdrt->tWB_max + FMC2_TIO + FMC2_TSYNC - tset_mem; |
| 710 | if (sdrt->tADL_min > tset_mem && |
| 711 | (thold_att < sdrt->tADL_min - tset_mem)) |
| 712 | thold_att = sdrt->tADL_min - tset_mem; |
| 713 | if (sdrt->tWH_min > tset_mem && |
| 714 | (thold_att < sdrt->tWH_min - tset_mem)) |
| 715 | thold_att = sdrt->tWH_min - tset_mem; |
| 716 | if (sdrt->tWHR_min > tset_mem && |
| 717 | (thold_att < sdrt->tWHR_min - tset_mem)) |
| 718 | thold_att = sdrt->tWHR_min - tset_mem; |
| 719 | if ((sdrt->tRC_min > tset_att + twait) && |
| 720 | (thold_att < sdrt->tRC_min - (tset_att + twait))) |
| 721 | thold_att = sdrt->tRC_min - (tset_att + twait); |
| 722 | if ((sdrt->tWC_min > tset_att + twait) && |
| 723 | (thold_att < sdrt->tWC_min - (tset_att + twait))) |
| 724 | thold_att = sdrt->tWC_min - (tset_att + twait); |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 725 | timing = DIV_ROUND_UP(thold_att, hclkp); |
| 726 | tims->thold_att = clamp_val(timing, 1, FMC2_PMEM_PATT_TIMING_MASK); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 729 | static int stm32_fmc2_nfc_setup_interface(struct mtd_info *mtd, int chipnr, |
| 730 | const struct nand_data_interface *cf) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 731 | { |
| 732 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 733 | const struct nand_sdr_timings *sdrt; |
| 734 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 735 | sdrt = nand_get_sdr_timings(cf); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 736 | if (IS_ERR(sdrt)) |
| 737 | return PTR_ERR(sdrt); |
| 738 | |
Christophe Kerello | 1b4662a | 2023-03-30 11:16:21 +0200 | [diff] [blame] | 739 | if (sdrt->tRC_min < 30000) |
| 740 | return -EOPNOTSUPP; |
| 741 | |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 742 | if (chipnr == NAND_DATA_IFACE_CHECK_ONLY) |
| 743 | return 0; |
| 744 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 745 | stm32_fmc2_nfc_calc_timings(chip, sdrt); |
| 746 | stm32_fmc2_nfc_timings_init(chip); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 751 | static void stm32_fmc2_nfc_nand_callbacks_setup(struct nand_chip *chip) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 752 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 753 | chip->ecc.hwctl = stm32_fmc2_nfc_hwctl; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 754 | |
| 755 | /* |
| 756 | * Specific callbacks to read/write a page depending on |
| 757 | * the algo used (Hamming, BCH). |
| 758 | */ |
| 759 | if (chip->ecc.strength == FMC2_ECC_HAM) { |
| 760 | /* Hamming is used */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 761 | chip->ecc.calculate = stm32_fmc2_nfc_ham_calculate; |
| 762 | chip->ecc.correct = stm32_fmc2_nfc_ham_correct; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 763 | chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 4 : 3; |
| 764 | chip->ecc.options |= NAND_ECC_GENERIC_ERASED_CHECK; |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | /* BCH is used */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 769 | chip->ecc.read_page = stm32_fmc2_nfc_read_page; |
| 770 | chip->ecc.calculate = stm32_fmc2_nfc_bch_calculate; |
| 771 | chip->ecc.correct = stm32_fmc2_nfc_bch_correct; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 772 | |
| 773 | if (chip->ecc.strength == FMC2_ECC_BCH8) |
| 774 | chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 14 : 13; |
| 775 | else |
| 776 | chip->ecc.bytes = chip->options & NAND_BUSWIDTH_16 ? 8 : 7; |
| 777 | } |
| 778 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 779 | static int stm32_fmc2_nfc_calc_ecc_bytes(int step_size, int strength) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 780 | { |
| 781 | /* Hamming */ |
| 782 | if (strength == FMC2_ECC_HAM) |
| 783 | return 4; |
| 784 | |
| 785 | /* BCH8 */ |
| 786 | if (strength == FMC2_ECC_BCH8) |
| 787 | return 14; |
| 788 | |
| 789 | /* BCH4 */ |
| 790 | return 8; |
| 791 | } |
| 792 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 793 | NAND_ECC_CAPS_SINGLE(stm32_fmc2_nfc_ecc_caps, stm32_fmc2_nfc_calc_ecc_bytes, |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 794 | FMC2_ECC_STEP_SIZE, |
| 795 | FMC2_ECC_HAM, FMC2_ECC_BCH4, FMC2_ECC_BCH8); |
| 796 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 797 | static int stm32_fmc2_nfc_parse_child(struct stm32_fmc2_nfc *nfc, ofnode node) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 798 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 799 | struct stm32_fmc2_nand *nand = &nfc->nand; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 800 | u32 cs[FMC2_MAX_CE]; |
| 801 | int ret, i; |
| 802 | |
| 803 | if (!ofnode_get_property(node, "reg", &nand->ncs)) |
| 804 | return -EINVAL; |
| 805 | |
| 806 | nand->ncs /= sizeof(u32); |
| 807 | if (!nand->ncs) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 808 | log_err("Invalid reg property size\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 809 | return -EINVAL; |
| 810 | } |
| 811 | |
| 812 | ret = ofnode_read_u32_array(node, "reg", cs, nand->ncs); |
| 813 | if (ret < 0) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 814 | log_err("Could not retrieve reg property\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 815 | return -EINVAL; |
| 816 | } |
| 817 | |
| 818 | for (i = 0; i < nand->ncs; i++) { |
Christophe Kerello | 45dd1ee | 2020-07-31 09:53:34 +0200 | [diff] [blame] | 819 | if (cs[i] >= FMC2_MAX_CE) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 820 | log_err("Invalid reg value: %d\n", nand->cs_used[i]); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 821 | return -EINVAL; |
| 822 | } |
| 823 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 824 | if (nfc->cs_assigned & BIT(cs[i])) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 825 | log_err("Cs already assigned: %d\n", nand->cs_used[i]); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 826 | return -EINVAL; |
| 827 | } |
| 828 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 829 | nfc->cs_assigned |= BIT(cs[i]); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 830 | nand->cs_used[i] = cs[i]; |
| 831 | } |
| 832 | |
Christophe Kerello | e389a15 | 2022-02-22 17:38:49 +0100 | [diff] [blame] | 833 | gpio_request_by_name_nodev(node, "wp-gpios", 0, &nand->wp_gpio, |
| 834 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 835 | |
Patrice Chotard | 33d2cf9 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 836 | nand->chip.flash_node = node; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 841 | static int stm32_fmc2_nfc_parse_dt(struct udevice *dev, |
| 842 | struct stm32_fmc2_nfc *nfc) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 843 | { |
| 844 | ofnode child; |
| 845 | int ret, nchips = 0; |
| 846 | |
| 847 | dev_for_each_subnode(child, dev) |
| 848 | nchips++; |
| 849 | |
| 850 | if (!nchips) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 851 | log_err("NAND chip not defined\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 852 | return -EINVAL; |
| 853 | } |
| 854 | |
| 855 | if (nchips > 1) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 856 | log_err("Too many NAND chips defined\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 857 | return -EINVAL; |
| 858 | } |
| 859 | |
| 860 | dev_for_each_subnode(child, dev) { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 861 | ret = stm32_fmc2_nfc_parse_child(nfc, child); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 862 | if (ret) |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 869 | static struct udevice *stm32_fmc2_nfc_get_cdev(struct udevice *dev) |
| 870 | { |
| 871 | struct udevice *pdev = dev_get_parent(dev); |
| 872 | struct udevice *cdev = NULL; |
| 873 | bool ebi_found = false; |
| 874 | |
| 875 | if (pdev && ofnode_device_is_compatible(dev_ofnode(pdev), |
| 876 | "st,stm32mp1-fmc2-ebi")) |
| 877 | ebi_found = true; |
| 878 | |
| 879 | if (ofnode_device_is_compatible(dev_ofnode(dev), |
| 880 | "st,stm32mp1-fmc2-nfc")) { |
| 881 | if (ebi_found) |
| 882 | cdev = pdev; |
| 883 | |
| 884 | return cdev; |
| 885 | } |
| 886 | |
| 887 | if (!ebi_found) |
| 888 | cdev = dev; |
| 889 | |
| 890 | return cdev; |
| 891 | } |
| 892 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 893 | static int stm32_fmc2_nfc_probe(struct udevice *dev) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 894 | { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 895 | struct stm32_fmc2_nfc *nfc = dev_get_priv(dev); |
| 896 | struct stm32_fmc2_nand *nand = &nfc->nand; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 897 | struct nand_chip *chip = &nand->chip; |
| 898 | struct mtd_info *mtd = &chip->mtd; |
| 899 | struct nand_ecclayout *ecclayout; |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 900 | struct udevice *cdev; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 901 | struct reset_ctl reset; |
Patrick Delaunay | 804858a | 2019-06-21 15:26:54 +0200 | [diff] [blame] | 902 | int oob_index, chip_cs, mem_region, ret; |
| 903 | unsigned int i; |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 904 | int start_region = 0; |
| 905 | fdt_addr_t addr; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 906 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 907 | spin_lock_init(&nfc->controller.lock); |
| 908 | init_waitqueue_head(&nfc->controller.wq); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 909 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 910 | cdev = stm32_fmc2_nfc_get_cdev(dev); |
| 911 | if (!cdev) |
| 912 | return -EINVAL; |
| 913 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 914 | ret = stm32_fmc2_nfc_parse_dt(dev, nfc); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 915 | if (ret) |
| 916 | return ret; |
| 917 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 918 | nfc->io_base = dev_read_addr(cdev); |
| 919 | if (nfc->io_base == FDT_ADDR_T_NONE) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | if (dev == cdev) |
| 923 | start_region = 1; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 924 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 925 | for (chip_cs = 0, mem_region = start_region; chip_cs < FMC2_MAX_CE; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 926 | chip_cs++, mem_region += 3) { |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 927 | if (!(nfc->cs_assigned & BIT(chip_cs))) |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 928 | continue; |
| 929 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 930 | addr = dev_read_addr_index(dev, mem_region); |
| 931 | if (addr == FDT_ADDR_T_NONE) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 932 | dev_err(dev, "Resource data_base not found for cs%d", chip_cs); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 933 | return ret; |
| 934 | } |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 935 | nfc->data_base[chip_cs] = addr; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 936 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 937 | addr = dev_read_addr_index(dev, mem_region + 1); |
| 938 | if (addr == FDT_ADDR_T_NONE) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 939 | dev_err(dev, "Resource cmd_base not found for cs%d", chip_cs); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 940 | return ret; |
| 941 | } |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 942 | nfc->cmd_base[chip_cs] = addr; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 943 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 944 | addr = dev_read_addr_index(dev, mem_region + 2); |
| 945 | if (addr == FDT_ADDR_T_NONE) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 946 | dev_err(dev, "Resource addr_base not found for cs%d", chip_cs); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 947 | return ret; |
| 948 | } |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 949 | nfc->addr_base[chip_cs] = addr; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* Enable the clock */ |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 953 | ret = clk_get_by_index(cdev, 0, &nfc->clk); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 954 | if (ret) |
| 955 | return ret; |
| 956 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 957 | ret = clk_enable(&nfc->clk); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 958 | if (ret) |
| 959 | return ret; |
| 960 | |
| 961 | /* Reset */ |
| 962 | ret = reset_get_by_index(dev, 0, &reset); |
| 963 | if (!ret) { |
| 964 | reset_assert(&reset); |
| 965 | udelay(2); |
| 966 | reset_deassert(&reset); |
| 967 | } |
| 968 | |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 969 | stm32_fmc2_nfc_init(nfc, dev != cdev); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 970 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 971 | chip->controller = &nfc->base; |
| 972 | chip->select_chip = stm32_fmc2_nfc_select_chip; |
| 973 | chip->setup_data_interface = stm32_fmc2_nfc_setup_interface; |
| 974 | chip->cmd_ctrl = stm32_fmc2_nfc_cmd_ctrl; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 975 | chip->chip_delay = FMC2_RB_DELAY_US; |
| 976 | chip->options |= NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE | |
| 977 | NAND_USE_BOUNCE_BUFFER; |
| 978 | |
| 979 | /* Default ECC settings */ |
| 980 | chip->ecc.mode = NAND_ECC_HW; |
| 981 | chip->ecc.size = FMC2_ECC_STEP_SIZE; |
| 982 | chip->ecc.strength = FMC2_ECC_BCH8; |
| 983 | |
Christophe Kerello | e389a15 | 2022-02-22 17:38:49 +0100 | [diff] [blame] | 984 | /* Disable Write Protect */ |
| 985 | if (dm_gpio_is_valid(&nand->wp_gpio)) |
| 986 | dm_gpio_set_value(&nand->wp_gpio, 0); |
| 987 | |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 988 | ret = nand_scan_ident(mtd, nand->ncs, NULL); |
| 989 | if (ret) |
| 990 | return ret; |
| 991 | |
| 992 | /* |
| 993 | * Only NAND_ECC_HW mode is actually supported |
| 994 | * Hamming => ecc.strength = 1 |
| 995 | * BCH4 => ecc.strength = 4 |
| 996 | * BCH8 => ecc.strength = 8 |
| 997 | * ECC sector size = 512 |
| 998 | */ |
| 999 | if (chip->ecc.mode != NAND_ECC_HW) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 1000 | dev_err(dev, "Nand_ecc_mode is not well defined in the DT\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1001 | return -EINVAL; |
| 1002 | } |
| 1003 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1004 | ret = nand_check_ecc_caps(chip, &stm32_fmc2_nfc_ecc_caps, |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1005 | mtd->oobsize - FMC2_BBM_LEN); |
| 1006 | if (ret) { |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 1007 | dev_err(dev, "No valid ECC settings set\n"); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | if (chip->bbt_options & NAND_BBT_USE_FLASH) |
| 1012 | chip->bbt_options |= NAND_BBT_NO_OOB; |
| 1013 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1014 | stm32_fmc2_nfc_nand_callbacks_setup(chip); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1015 | |
| 1016 | /* Define ECC layout */ |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1017 | ecclayout = &nfc->ecclayout; |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1018 | ecclayout->eccbytes = chip->ecc.bytes * |
| 1019 | (mtd->writesize / chip->ecc.size); |
| 1020 | oob_index = FMC2_BBM_LEN; |
| 1021 | for (i = 0; i < ecclayout->eccbytes; i++, oob_index++) |
| 1022 | ecclayout->eccpos[i] = oob_index; |
| 1023 | ecclayout->oobfree->offset = oob_index; |
| 1024 | ecclayout->oobfree->length = mtd->oobsize - ecclayout->oobfree->offset; |
| 1025 | chip->ecc.layout = ecclayout; |
| 1026 | |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1027 | if (chip->options & NAND_BUSWIDTH_16) |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1028 | stm32_fmc2_nfc_set_buswidth_16(nfc, true); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1029 | |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1030 | ret = nand_scan_tail(mtd); |
| 1031 | if (ret) |
| 1032 | return ret; |
| 1033 | |
| 1034 | return nand_register(0, mtd); |
| 1035 | } |
| 1036 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1037 | static const struct udevice_id stm32_fmc2_nfc_match[] = { |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1038 | { .compatible = "st,stm32mp15-fmc2" }, |
Christophe Kerello | 6276f86 | 2020-07-31 09:53:41 +0200 | [diff] [blame] | 1039 | { .compatible = "st,stm32mp1-fmc2-nfc" }, |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1040 | { /* Sentinel */ } |
| 1041 | }; |
| 1042 | |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1043 | U_BOOT_DRIVER(stm32_fmc2_nfc) = { |
| 1044 | .name = "stm32_fmc2_nfc", |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1045 | .id = UCLASS_MTD, |
Christophe Kerello | d1a2587 | 2020-07-31 09:53:37 +0200 | [diff] [blame] | 1046 | .of_match = stm32_fmc2_nfc_match, |
| 1047 | .probe = stm32_fmc2_nfc_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1048 | .priv_auto = sizeof(struct stm32_fmc2_nfc), |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | void board_nand_init(void) |
| 1052 | { |
| 1053 | struct udevice *dev; |
| 1054 | int ret; |
| 1055 | |
| 1056 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1057 | DM_DRIVER_GET(stm32_fmc2_nfc), |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1058 | &dev); |
| 1059 | if (ret && ret != -ENODEV) |
Patrick Delaunay | b18ccfa | 2020-11-06 19:01:54 +0100 | [diff] [blame] | 1060 | log_err("Failed to initialize STM32 FMC2 NFC controller. (error %d)\n", |
| 1061 | ret); |
Christophe Kerello | da14168 | 2019-04-05 11:41:50 +0200 | [diff] [blame] | 1062 | } |