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