| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* Copyright (C) 2013 Boris BREZILLON <[email protected]> |
| 4 |
* |
| 5 |
* Derived from: |
| 6 |
* https://github.com/yuq/sunxi-nfc-mtd |
| 7 |
* Copyright (C) 2013 Qiang Yu <[email protected]> |
| 8 |
* |
| 9 |
* https://github.com/hno/Allwinner-Info |
| 10 |
* Copyright (C) 2013 Henrik Nordström <Henrik Nordström> |
| 11 |
* |
| 12 |
* Copyright (C) 2013 Dmitriy B. <[email protected]> |
| 13 |
* Copyright (C) 2013 Sergey Lapin <[email protected]> |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <linux/dma-mapping.h> |
| 17 |
#include <linux/slab.h> |
| 18 |
#include <linux/module.h> |
| 19 |
#include <linux/moduleparam.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/of.h> |
| 22 |
#include <linux/mtd/mtd.h> |
| 23 |
#include <linux/mtd/rawnand.h> |
| 24 |
#include <linux/mtd/partitions.h> |
| 25 |
#include <linux/clk.h> |
| 26 |
#include <linux/delay.h> |
| 27 |
#include <linux/dmaengine.h> |
| 28 |
#include <linux/interrupt.h> |
| 29 |
#include <linux/iopoll.h> |
| 30 |
#include <linux/reset.h> |
| 31 |
|
| 32 |
#define NFC_REG_CTL 0x0000 |
| 33 |
#define NFC_REG_ST 0x0004 |
| 34 |
#define NFC_REG_INT 0x0008 |
| 35 |
#define NFC_REG_TIMING_CTL 0x000C |
| 36 |
#define NFC_REG_TIMING_CFG 0x0010 |
| 37 |
#define NFC_REG_ADDR_LOW 0x0014 |
| 38 |
#define NFC_REG_ADDR_HIGH 0x0018 |
| 39 |
#define NFC_REG_SECTOR_NUM 0x001C |
| 40 |
#define NFC_REG_CNT 0x0020 |
| 41 |
#define NFC_REG_CMD 0x0024 |
| 42 |
#define NFC_REG_RCMD_SET 0x0028 |
| 43 |
#define NFC_REG_WCMD_SET 0x002C |
| 44 |
#define NFC_REG_A10_IO_DATA 0x0030 |
| 45 |
#define NFC_REG_A23_IO_DATA 0x0300 |
| 46 |
#define NFC_REG_ECC_CTL 0x0034 |
| 47 |
#define NFC_REG_ECC_ST 0x0038 |
| 48 |
#define NFC_REG_H6_PAT_FOUND 0x003C |
| 49 |
#define NFC_REG_A10_ECC_ERR_CNT 0x0040 |
| 50 |
#define NFC_REG_H6_ECC_ERR_CNT 0x0050 |
| 51 |
#define NFC_REG_ECC_ERR_CNT(nfc, x) ((nfc->caps->reg_ecc_err_cnt + (x)) & ~0x3) |
| 52 |
#define NFC_REG_H6_RDATA_CTL 0x0044 |
| 53 |
#define NFC_REG_H6_RDATA_0 0x0048 |
| 54 |
#define NFC_REG_H6_RDATA_1 0x004C |
| 55 |
#define NFC_REG_A10_USER_DATA 0x0050 |
| 56 |
#define NFC_REG_H6_USER_DATA 0x0080 |
| 57 |
#define NFC_REG_USER_DATA(nfc, x) (nfc->caps->reg_user_data + ((x) * 4)) |
| 58 |
#define NFC_REG_H6_USER_DATA_LEN 0x0070 |
| 59 |
/* A USER_DATA_LEN register can hold the length of 8 USER_DATA registers */ |
| 60 |
#define NFC_REG_USER_DATA_LEN_CAPACITY 8 |
| 61 |
#define NFC_REG_USER_DATA_LEN(nfc, step) \ |
| 62 |
(nfc->caps->reg_user_data_len + \ |
| 63 |
((step) / NFC_REG_USER_DATA_LEN_CAPACITY) * 4) |
| 64 |
#define NFC_REG_SPARE_AREA(nfc) (nfc->caps->reg_spare_area) |
| 65 |
#define NFC_REG_A10_SPARE_AREA 0x00A0 |
| 66 |
#define NFC_REG_PAT_ID(nfc) (nfc->caps->reg_pat_id) |
| 67 |
#define NFC_REG_A10_PAT_ID 0x00A4 |
| 68 |
#define NFC_REG_MDMA_ADDR 0x00C0 |
| 69 |
#define NFC_REG_MDMA_CNT 0x00C4 |
| 70 |
#define NFC_REG_H6_EFNAND_STATUS 0x0110 |
| 71 |
#define NFC_REG_H6_SPARE_AREA 0x0114 |
| 72 |
#define NFC_REG_H6_PAT_ID 0x0118 |
| 73 |
#define NFC_REG_H6_DDR2_SPEC_CTL 0x011C |
| 74 |
#define NFC_REG_H6_NDMA_MODE_CTL 0x0120 |
| 75 |
#define NFC_REG_H6_MDMA_DLBA_REG 0x0200 |
| 76 |
#define NFC_REG_H6_MDMA_STA 0x0204 |
| 77 |
#define NFC_REG_H6_MDMA_INT_MAS 0x0208 |
| 78 |
#define NFC_REG_H6_MDMA_DESC_ADDR 0x020C |
| 79 |
#define NFC_REG_H6_MDMA_BUF_ADDR 0x0210 |
| 80 |
#define NFC_REG_H6_MDMA_CNT 0x0214 |
| 81 |
|
| 82 |
#define NFC_RAM0_BASE 0x0400 |
| 83 |
#define NFC_RAM1_BASE 0x0800 |
| 84 |
|
| 85 |
/* define bit use in NFC_CTL */ |
| 86 |
#define NFC_EN BIT(0) |
| 87 |
#define NFC_RESET BIT(1) |
| 88 |
#define NFC_BUS_WIDTH_MSK BIT(2) |
| 89 |
#define NFC_BUS_WIDTH_8 (0 << 2) |
| 90 |
#define NFC_BUS_WIDTH_16 (1 << 2) |
| 91 |
#define NFC_RB_SEL_MSK BIT(3) |
| 92 |
#define NFC_RB_SEL(x) ((x) << 3) |
| 93 |
/* CE_SEL BIT 27 is meant to be used for GPIO chipselect */ |
| 94 |
#define NFC_CE_SEL_MSK GENMASK(26, 24) |
| 95 |
#define NFC_CE_SEL(x) ((x) << 24) |
| 96 |
#define NFC_CE_CTL BIT(6) |
| 97 |
#define NFC_PAGE_SHIFT_MSK GENMASK(11, 8) |
| 98 |
#define NFC_PAGE_SHIFT(x) (((x) < 10 ? 0 : (x) - 10) << 8) |
| 99 |
#define NFC_SAM BIT(12) |
| 100 |
#define NFC_RAM_METHOD BIT(14) |
| 101 |
#define NFC_DMA_TYPE_NORMAL BIT(15) |
| 102 |
#define NFC_DEBUG_CTL BIT(31) |
| 103 |
|
| 104 |
/* define bit use in NFC_ST */ |
| 105 |
#define NFC_RB_B2R BIT(0) |
| 106 |
#define NFC_CMD_INT_FLAG BIT(1) |
| 107 |
#define NFC_DMA_INT_FLAG BIT(2) |
| 108 |
#define NFC_CMD_FIFO_STATUS BIT(3) |
| 109 |
#define NFC_STA BIT(4) |
| 110 |
#define NFC_NATCH_INT_FLAG BIT(5) |
| 111 |
#define NFC_RB_STATE(x) BIT(x + 8) |
| 112 |
#define NFC_RB_STATE_MSK GENMASK(11, 8) |
| 113 |
#define NDFC_RDATA_STA_1 BIT(12) |
| 114 |
#define NDFC_RDATA_STA_0 BIT(13) |
| 115 |
|
| 116 |
/* define bit use in NFC_INT */ |
| 117 |
#define NFC_B2R_INT_ENABLE BIT(0) |
| 118 |
#define NFC_CMD_INT_ENABLE BIT(1) |
| 119 |
#define NFC_DMA_INT_ENABLE BIT(2) |
| 120 |
#define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \ |
| 121 |
NFC_CMD_INT_ENABLE | \ |
| 122 |
NFC_DMA_INT_ENABLE) |
| 123 |
|
| 124 |
/* define bit use in NFC_TIMING_CTL */ |
| 125 |
#define NFC_TIMING_CTL_EDO BIT(8) |
| 126 |
#define NFC_TIMING_CTL_E_EDO BIT(9) |
| 127 |
|
| 128 |
/* define NFC_TIMING_CFG register layout */ |
| 129 |
#define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \ |
| 130 |
(((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \ |
| 131 |
(((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \ |
| 132 |
(((tCAD) & 0x7) << 8)) |
| 133 |
|
| 134 |
#define NFC_TIMING_CFG2(tCDQSS, tSC, tCLHZ, tCSS, tWC) \ |
| 135 |
((((tCDQSS) & 0x1) << 11) | (((tSC) & 0x3) << 12) | \ |
| 136 |
(((tCLHZ) & 0x3) << 14) | (((tCSS) & 0x3) << 16) | \ |
| 137 |
(((tWC) & 0x3) << 18)) |
| 138 |
|
| 139 |
/* define bit use in NFC_CMD */ |
| 140 |
#define NFC_CMD_LOW_BYTE_MSK GENMASK(7, 0) |
| 141 |
#define NFC_CMD_HIGH_BYTE_MSK GENMASK(15, 8) /* 15-10 reserved on H6 */ |
| 142 |
#define NFC_CMD_ADR_NUM_MSK GENMASK(9, 8) |
| 143 |
#define NFC_CMD(x) (x) |
| 144 |
#define NFC_ADR_NUM_MSK GENMASK(18, 16) |
| 145 |
#define NFC_ADR_NUM(x) (((x) - 1) << 16) |
| 146 |
#define NFC_SEND_ADR BIT(19) |
| 147 |
#define NFC_ACCESS_DIR BIT(20) |
| 148 |
#define NFC_DATA_TRANS BIT(21) |
| 149 |
#define NFC_SEND_CMD1 BIT(22) |
| 150 |
#define NFC_WAIT_FLAG BIT(23) |
| 151 |
#define NFC_SEND_CMD2 BIT(24) |
| 152 |
#define NFC_SEQ BIT(25) |
| 153 |
#define NFC_DATA_SWAP_METHOD BIT(26) |
| 154 |
#define NFC_ROW_AUTO_INC BIT(27) |
| 155 |
#define NFC_H6_SEND_RND_CMD2 BIT(27) |
| 156 |
#define NFC_SEND_CMD3 BIT(28) |
| 157 |
#define NFC_SEND_CMD4 BIT(29) |
| 158 |
#define NFC_CMD_TYPE_MSK GENMASK(31, 30) |
| 159 |
#define NFC_NORMAL_OP (0 << 30) |
| 160 |
#define NFC_ECC_OP (1 << 30) |
| 161 |
#define NFC_PAGE_OP (2U << 30) |
| 162 |
|
| 163 |
/* define bit use in NFC_RCMD_SET */ |
| 164 |
#define NFC_READ_CMD_MSK GENMASK(7, 0) |
| 165 |
#define NFC_RND_READ_CMD0_MSK GENMASK(15, 8) |
| 166 |
#define NFC_RND_READ_CMD1_MSK GENMASK(23, 16) |
| 167 |
#define NFC_RND_READ_CMD2_MSK GENMASK(31, 24) |
| 168 |
|
| 169 |
/* define bit use in NFC_WCMD_SET */ |
| 170 |
#define NFC_PROGRAM_CMD_MSK GENMASK(7, 0) |
| 171 |
#define NFC_RND_WRITE_CMD_MSK GENMASK(15, 8) |
| 172 |
#define NFC_READ_CMD0_MSK GENMASK(23, 16) |
| 173 |
#define NFC_READ_CMD1_MSK GENMASK(31, 24) |
| 174 |
|
| 175 |
/* define bit use in NFC_ECC_CTL */ |
| 176 |
#define NFC_ECC_EN BIT(0) |
| 177 |
#define NFC_ECC_PIPELINE BIT(3) |
| 178 |
#define NFC_ECC_EXCEPTION BIT(4) |
| 179 |
#define NFC_ECC_BLOCK_SIZE_MSK BIT(5) |
| 180 |
#define NFC_ECC_BLOCK_512 BIT(5) |
| 181 |
#define NFC_RANDOM_EN(nfc) (nfc->caps->random_en_mask) |
| 182 |
#define NFC_RANDOM_DIRECTION(nfc) (nfc->caps->random_dir_mask) |
| 183 |
#define NFC_ECC_MODE_MSK(nfc) (nfc->caps->ecc_mode_mask) |
| 184 |
#define NFC_ECC_MODE(nfc, x) field_prep(NFC_ECC_MODE_MSK(nfc), (x)) |
| 185 |
/* RANDOM_PAGE_SIZE: 0: ECC block size 1: page size */ |
| 186 |
#define NFC_A23_RANDOM_PAGE_SIZE BIT(11) |
| 187 |
#define NFC_H6_RANDOM_PAGE_SIZE BIT(7) |
| 188 |
#define NFC_RANDOM_SEED_MSK GENMASK(30, 16) |
| 189 |
#define NFC_RANDOM_SEED(x) ((x) << 16) |
| 190 |
|
| 191 |
/* define bit use in NFC_ECC_ST */ |
| 192 |
#define NFC_ECC_ERR(x) BIT(x) |
| 193 |
#define NFC_ECC_ERR_MSK(nfc) (nfc->caps->ecc_err_mask) |
| 194 |
|
| 195 |
/* |
| 196 |
* define bit use in NFC_REG_PAT_FOUND |
| 197 |
* For A10/A23, NFC_REG_PAT_FOUND == NFC_ECC_ST register |
| 198 |
*/ |
| 199 |
#define NFC_ECC_PAT_FOUND_MSK(nfc) (nfc->caps->pat_found_mask) |
| 200 |
|
| 201 |
#define NFC_ECC_ERR_CNT(b, x) (((x) >> (((b) % 4) * 8)) & 0xff) |
| 202 |
|
| 203 |
#define NFC_USER_DATA_LEN_MSK(step) \ |
| 204 |
(0xf << (((step) % NFC_REG_USER_DATA_LEN_CAPACITY) * 4)) |
| 205 |
|
| 206 |
#define NFC_DEFAULT_TIMEOUT_MS 1000 |
| 207 |
|
| 208 |
#define NFC_MAX_CS 7 |
| 209 |
|
| 210 |
/* |
| 211 |
* On A10/A23, this is the size of the NDFC User Data Register, containing the |
| 212 |
* mandatory user data bytes following the ECC for each ECC step. |
| 213 |
* Thus, for each ECC step, we need the ECC bytes + USER_DATA_SZ. |
| 214 |
* Those bits are currently unsused, and kept as default value 0xffffffff. |
| 215 |
* |
| 216 |
* On H6/H616, this size became configurable, from 0 bytes to 32, via the |
| 217 |
* USER_DATA_LEN registers. |
| 218 |
*/ |
| 219 |
#define USER_DATA_SZ 4 |
| 220 |
|
| 221 |
/** |
| 222 |
* struct sunxi_nand_chip_sel - stores information related to NAND Chip Select |
| 223 |
* |
| 224 |
* @cs: the NAND CS id used to communicate with a NAND Chip |
| 225 |
* @rb: the Ready/Busy pin ID. -1 means no R/B pin connected to the NFC |
| 226 |
*/ |
| 227 |
struct sunxi_nand_chip_sel { |
| 228 |
u8 cs; |
| 229 |
s8 rb; |
| 230 |
}; |
| 231 |
|
| 232 |
/** |
| 233 |
* struct sunxi_nand_hw_ecc - stores information related to HW ECC support |
| 234 |
* |
| 235 |
* @ecc_ctl: ECC_CTL register value for this NAND chip |
| 236 |
*/ |
| 237 |
struct sunxi_nand_hw_ecc { |
| 238 |
u32 ecc_ctl; |
| 239 |
}; |
| 240 |
|
| 241 |
/** |
| 242 |
* struct sunxi_nand_chip - stores NAND chip device related information |
| 243 |
* |
| 244 |
* @node: used to store NAND chips into a list |
| 245 |
* @nand: base NAND chip structure |
| 246 |
* @ecc: ECC controller structure |
| 247 |
* @clk_rate: clk_rate required for this NAND chip |
| 248 |
* @timing_cfg: TIMING_CFG register value for this NAND chip |
| 249 |
* @timing_ctl: TIMING_CTL register value for this NAND chip |
| 250 |
* @nsels: number of CS lines required by the NAND chip |
| 251 |
* @sels: array of CS lines descriptions |
| 252 |
*/ |
| 253 |
struct sunxi_nand_chip { |
| 254 |
struct list_head node; |
| 255 |
struct nand_chip nand; |
| 256 |
struct sunxi_nand_hw_ecc ecc; |
| 257 |
unsigned long clk_rate; |
| 258 |
u32 timing_cfg; |
| 259 |
u32 timing_ctl; |
| 260 |
int nsels; |
| 261 |
struct sunxi_nand_chip_sel sels[] __counted_by(nsels); |
| 262 |
}; |
| 263 |
|
| 264 |
static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand) |
| 265 |
{ |
| 266 |
return container_of(nand, struct sunxi_nand_chip, nand); |
| 267 |
} |
| 268 |
|
| 269 |
/* |
| 270 |
* NAND Controller capabilities structure: stores NAND controller capabilities |
| 271 |
* for distinction between compatible strings. |
| 272 |
* |
| 273 |
* @has_mdma: Use mbus dma mode, otherwise general dma |
| 274 |
* through MBUS on A23/A33 needs extra configuration. |
| 275 |
* @has_ecc_block_512: If the ECC can handle 512B or only 1024B chuncks |
| 276 |
* @has_ecc_clk: If the controller needs an ECC clock. |
| 277 |
* @has_mbus_clk: If the controller needs a mbus clock. |
| 278 |
* @reg_io_data: I/O data register |
| 279 |
* @reg_ecc_err_cnt: ECC error counter register |
| 280 |
* @reg_user_data: User data register |
| 281 |
* @reg_user_data_len: User data length register |
| 282 |
* @reg_spare_area: Spare Area Register |
| 283 |
* @reg_pat_id: Pattern ID Register |
| 284 |
* @reg_pat_found: Data Pattern Status Register |
| 285 |
* @random_en_mask: RANDOM_EN mask in NFC_ECC_CTL register |
| 286 |
* @random_dir_mask: RANDOM_DIRECTION mask in NFC_ECC_CTL register |
| 287 |
* @ecc_mode_mask: ECC_MODE mask in NFC_ECC_CTL register |
| 288 |
* @ecc_err_mask: NFC_ECC_ERR mask in NFC_ECC_ST register |
| 289 |
* @pat_found_mask: ECC_PAT_FOUND mask in NFC_REG_PAT_FOUND register |
| 290 |
* @dma_maxburst: DMA maxburst |
| 291 |
* @ecc_strengths: Available ECC strengths array |
| 292 |
* @nstrengths: Size of @ecc_strengths |
| 293 |
* @max_ecc_steps: Maximum supported steps for ECC, this is also the |
| 294 |
* number of user data registers |
| 295 |
* @user_data_len_tab: Table of lenghts supported by USER_DATA_LEN register |
| 296 |
* The table index is the value to set in NFC_USER_DATA_LEN |
| 297 |
* registers, and the corresponding value is the number of |
| 298 |
* bytes to write |
| 299 |
* @nuser_data_tab: Size of @user_data_len_tab |
| 300 |
* @sram_size: Size of the NAND controller SRAM |
| 301 |
*/ |
| 302 |
struct sunxi_nfc_caps { |
| 303 |
bool has_mdma; |
| 304 |
bool has_ecc_block_512; |
| 305 |
bool has_ecc_clk; |
| 306 |
bool has_mbus_clk; |
| 307 |
unsigned int reg_io_data; |
| 308 |
unsigned int reg_ecc_err_cnt; |
| 309 |
unsigned int reg_user_data; |
| 310 |
unsigned int reg_user_data_len; |
| 311 |
unsigned int reg_spare_area; |
| 312 |
unsigned int reg_pat_id; |
| 313 |
unsigned int reg_pat_found; |
| 314 |
unsigned int random_en_mask; |
| 315 |
unsigned int random_dir_mask; |
| 316 |
unsigned int ecc_mode_mask; |
| 317 |
unsigned int ecc_err_mask; |
| 318 |
unsigned int pat_found_mask; |
| 319 |
unsigned int dma_maxburst; |
| 320 |
const u8 *ecc_strengths; |
| 321 |
unsigned int nstrengths; |
| 322 |
const u8 *user_data_len_tab; |
| 323 |
unsigned int nuser_data_tab; |
| 324 |
unsigned int max_ecc_steps; |
| 325 |
int sram_size; |
| 326 |
}; |
| 327 |
|
| 328 |
/** |
| 329 |
* struct sunxi_nfc - stores sunxi NAND controller information |
| 330 |
* |
| 331 |
* @controller: base controller structure |
| 332 |
* @dev: parent device (used to print error messages) |
| 333 |
* @regs: NAND controller registers |
| 334 |
* @ahb_clk: NAND controller AHB clock |
| 335 |
* @mod_clk: NAND controller mod clock |
| 336 |
* @ecc_clk: NAND controller ECC clock |
| 337 |
* @mbus_clk: NAND controller MBUS clock |
| 338 |
* @reset: NAND controller reset line |
| 339 |
* @assigned_cs: bitmask describing already assigned CS lines |
| 340 |
* @clk_rate: NAND controller current clock rate |
| 341 |
* @chips: a list containing all the NAND chips attached to this NAND |
| 342 |
* controller |
| 343 |
* @complete: a completion object used to wait for NAND controller events |
| 344 |
* @dmac: the DMA channel attached to the NAND controller |
| 345 |
* @caps: NAND Controller capabilities |
| 346 |
*/ |
| 347 |
struct sunxi_nfc { |
| 348 |
struct nand_controller controller; |
| 349 |
struct device *dev; |
| 350 |
void __iomem *regs; |
| 351 |
struct clk *ahb_clk; |
| 352 |
struct clk *mod_clk; |
| 353 |
struct clk *ecc_clk; |
| 354 |
struct clk *mbus_clk; |
| 355 |
struct reset_control *reset; |
| 356 |
unsigned long assigned_cs; |
| 357 |
unsigned long clk_rate; |
| 358 |
struct list_head chips; |
| 359 |
struct completion complete; |
| 360 |
struct dma_chan *dmac; |
| 361 |
const struct sunxi_nfc_caps *caps; |
| 362 |
}; |
| 363 |
|
| 364 |
static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_controller *ctrl) |
| 365 |
{ |
| 366 |
return container_of(ctrl, struct sunxi_nfc, controller); |
| 367 |
} |
| 368 |
|
| 369 |
static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id) |
| 370 |
{ |
| 371 |
struct sunxi_nfc *nfc = dev_id; |
| 372 |
u32 st = readl(nfc->regs + NFC_REG_ST); |
| 373 |
u32 ien = readl(nfc->regs + NFC_REG_INT); |
| 374 |
|
| 375 |
if (!(ien & st)) |
| 376 |
return IRQ_NONE; |
| 377 |
|
| 378 |
if ((ien & st) == ien) |
| 379 |
complete(&nfc->complete); |
| 380 |
|
| 381 |
writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST); |
| 382 |
writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT); |
| 383 |
|
| 384 |
return IRQ_HANDLED; |
| 385 |
} |
| 386 |
|
| 387 |
static int sunxi_nfc_wait_events(struct sunxi_nfc *nfc, u32 events, |
| 388 |
bool use_polling, unsigned int timeout_ms) |
| 389 |
{ |
| 390 |
int ret; |
| 391 |
|
| 392 |
if (events & ~NFC_INT_MASK) |
| 393 |
return -EINVAL; |
| 394 |
|
| 395 |
if (!timeout_ms) |
| 396 |
timeout_ms = NFC_DEFAULT_TIMEOUT_MS; |
| 397 |
|
| 398 |
if (!use_polling) { |
| 399 |
init_completion(&nfc->complete); |
| 400 |
|
| 401 |
writel(events, nfc->regs + NFC_REG_INT); |
| 402 |
|
| 403 |
ret = wait_for_completion_timeout(&nfc->complete, |
| 404 |
msecs_to_jiffies(timeout_ms)); |
| 405 |
if (!ret) |
| 406 |
ret = -ETIMEDOUT; |
| 407 |
else |
| 408 |
ret = 0; |
| 409 |
|
| 410 |
writel(0, nfc->regs + NFC_REG_INT); |
| 411 |
} else { |
| 412 |
u32 status; |
| 413 |
|
| 414 |
ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status, |
| 415 |
(status & events) == events, 1, |
| 416 |
timeout_ms * 1000); |
| 417 |
} |
| 418 |
|
| 419 |
writel(events & NFC_INT_MASK, nfc->regs + NFC_REG_ST); |
| 420 |
|
| 421 |
if (ret) |
| 422 |
dev_err(nfc->dev, "wait interrupt timedout\n"); |
| 423 |
|
| 424 |
return ret; |
| 425 |
} |
| 426 |
|
| 427 |
static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc) |
| 428 |
{ |
| 429 |
u32 status; |
| 430 |
int ret; |
| 431 |
|
| 432 |
ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status, |
| 433 |
!(status & NFC_CMD_FIFO_STATUS), 1, |
| 434 |
NFC_DEFAULT_TIMEOUT_MS * 1000); |
| 435 |
if (ret) |
| 436 |
dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n"); |
| 437 |
|
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
static int sunxi_nfc_rst(struct sunxi_nfc *nfc) |
| 442 |
{ |
| 443 |
u32 ctl; |
| 444 |
int ret; |
| 445 |
|
| 446 |
writel(0, nfc->regs + NFC_REG_ECC_CTL); |
| 447 |
writel(NFC_RESET, nfc->regs + NFC_REG_CTL); |
| 448 |
|
| 449 |
ret = readl_poll_timeout(nfc->regs + NFC_REG_CTL, ctl, |
| 450 |
!(ctl & NFC_RESET), 1, |
| 451 |
NFC_DEFAULT_TIMEOUT_MS * 1000); |
| 452 |
if (ret) |
| 453 |
dev_err(nfc->dev, "wait for NAND controller reset timedout\n"); |
| 454 |
|
| 455 |
return ret; |
| 456 |
} |
| 457 |
|
| 458 |
static int sunxi_nfc_dma_op_prepare(struct sunxi_nfc *nfc, const void *buf, |
| 459 |
int chunksize, int nchunks, |
| 460 |
enum dma_data_direction ddir, |
| 461 |
struct scatterlist *sg) |
| 462 |
{ |
| 463 |
struct dma_async_tx_descriptor *dmad; |
| 464 |
enum dma_transfer_direction tdir; |
| 465 |
dma_cookie_t dmat; |
| 466 |
int ret; |
| 467 |
|
| 468 |
if (ddir == DMA_FROM_DEVICE) |
| 469 |
tdir = DMA_DEV_TO_MEM; |
| 470 |
else |
| 471 |
tdir = DMA_MEM_TO_DEV; |
| 472 |
|
| 473 |
sg_init_one(sg, buf, nchunks * chunksize); |
| 474 |
ret = dma_map_sg(nfc->dev, sg, 1, ddir); |
| 475 |
if (!ret) |
| 476 |
return -ENOMEM; |
| 477 |
|
| 478 |
if (!nfc->caps->has_mdma) { |
| 479 |
dmad = dmaengine_prep_slave_sg(nfc->dmac, sg, 1, tdir, DMA_CTRL_ACK); |
| 480 |
if (!dmad) { |
| 481 |
ret = -EINVAL; |
| 482 |
goto err_unmap_buf; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
writel(readl(nfc->regs + NFC_REG_CTL) | NFC_RAM_METHOD, |
| 487 |
nfc->regs + NFC_REG_CTL); |
| 488 |
writel(nchunks, nfc->regs + NFC_REG_SECTOR_NUM); |
| 489 |
writel(chunksize, nfc->regs + NFC_REG_CNT); |
| 490 |
|
| 491 |
if (nfc->caps->has_mdma) { |
| 492 |
writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_DMA_TYPE_NORMAL, |
| 493 |
nfc->regs + NFC_REG_CTL); |
| 494 |
writel(chunksize * nchunks, nfc->regs + NFC_REG_MDMA_CNT); |
| 495 |
writel(sg_dma_address(sg), nfc->regs + NFC_REG_MDMA_ADDR); |
| 496 |
} else { |
| 497 |
dmat = dmaengine_submit(dmad); |
| 498 |
|
| 499 |
ret = dma_submit_error(dmat); |
| 500 |
if (ret) |
| 501 |
goto err_clr_dma_flag; |
| 502 |
} |
| 503 |
|
| 504 |
return 0; |
| 505 |
|
| 506 |
err_clr_dma_flag: |
| 507 |
writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD, |
| 508 |
nfc->regs + NFC_REG_CTL); |
| 509 |
|
| 510 |
err_unmap_buf: |
| 511 |
dma_unmap_sg(nfc->dev, sg, 1, ddir); |
| 512 |
return ret; |
| 513 |
} |
| 514 |
|
| 515 |
static void sunxi_nfc_dma_op_cleanup(struct sunxi_nfc *nfc, |
| 516 |
enum dma_data_direction ddir, |
| 517 |
struct scatterlist *sg) |
| 518 |
{ |
| 519 |
dma_unmap_sg(nfc->dev, sg, 1, ddir); |
| 520 |
writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD, |
| 521 |
nfc->regs + NFC_REG_CTL); |
| 522 |
} |
| 523 |
|
| 524 |
static void sunxi_nfc_select_chip(struct nand_chip *nand, unsigned int cs) |
| 525 |
{ |
| 526 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 527 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 528 |
struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 529 |
struct sunxi_nand_chip_sel *sel; |
| 530 |
u32 ctl; |
| 531 |
|
| 532 |
if (cs >= sunxi_nand->nsels) |
| 533 |
return; |
| 534 |
|
| 535 |
ctl = readl(nfc->regs + NFC_REG_CTL) & |
| 536 |
~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN); |
| 537 |
|
| 538 |
sel = &sunxi_nand->sels[cs]; |
| 539 |
ctl |= NFC_CE_SEL(sel->cs) | NFC_EN | NFC_PAGE_SHIFT(nand->page_shift); |
| 540 |
if (sel->rb >= 0) |
| 541 |
ctl |= NFC_RB_SEL(sel->rb); |
| 542 |
|
| 543 |
writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA(nfc)); |
| 544 |
|
| 545 |
if (nfc->clk_rate != sunxi_nand->clk_rate) { |
| 546 |
clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate); |
| 547 |
nfc->clk_rate = sunxi_nand->clk_rate; |
| 548 |
} |
| 549 |
|
| 550 |
writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL); |
| 551 |
writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG); |
| 552 |
writel(ctl, nfc->regs + NFC_REG_CTL); |
| 553 |
} |
| 554 |
|
| 555 |
static void sunxi_nfc_read_buf(struct nand_chip *nand, uint8_t *buf, int len) |
| 556 |
{ |
| 557 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 558 |
struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 559 |
int ret; |
| 560 |
int cnt; |
| 561 |
int offs = 0; |
| 562 |
u32 tmp; |
| 563 |
|
| 564 |
while (len > offs) { |
| 565 |
bool poll = false; |
| 566 |
|
| 567 |
cnt = min(len - offs, nfc->caps->sram_size); |
| 568 |
|
| 569 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 570 |
if (ret) |
| 571 |
break; |
| 572 |
|
| 573 |
writel(cnt, nfc->regs + NFC_REG_CNT); |
| 574 |
tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD; |
| 575 |
writel(tmp, nfc->regs + NFC_REG_CMD); |
| 576 |
|
| 577 |
/* Arbitrary limit for polling mode */ |
| 578 |
if (cnt < 64) |
| 579 |
poll = true; |
| 580 |
|
| 581 |
ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0); |
| 582 |
if (ret) |
| 583 |
break; |
| 584 |
|
| 585 |
if (buf) |
| 586 |
memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE, |
| 587 |
cnt); |
| 588 |
offs += cnt; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
static void sunxi_nfc_write_buf(struct nand_chip *nand, const uint8_t *buf, |
| 593 |
int len) |
| 594 |
{ |
| 595 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 596 |
struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 597 |
int ret; |
| 598 |
int cnt; |
| 599 |
int offs = 0; |
| 600 |
u32 tmp; |
| 601 |
|
| 602 |
while (len > offs) { |
| 603 |
bool poll = false; |
| 604 |
|
| 605 |
cnt = min(len - offs, nfc->caps->sram_size); |
| 606 |
|
| 607 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 608 |
if (ret) |
| 609 |
break; |
| 610 |
|
| 611 |
writel(cnt, nfc->regs + NFC_REG_CNT); |
| 612 |
memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt); |
| 613 |
tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | |
| 614 |
NFC_ACCESS_DIR; |
| 615 |
writel(tmp, nfc->regs + NFC_REG_CMD); |
| 616 |
|
| 617 |
/* Arbitrary limit for polling mode */ |
| 618 |
if (cnt < 64) |
| 619 |
poll = true; |
| 620 |
|
| 621 |
ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0); |
| 622 |
if (ret) |
| 623 |
break; |
| 624 |
|
| 625 |
offs += cnt; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
/* These seed values have been extracted from Allwinner's BSP */ |
| 630 |
static const u16 sunxi_nfc_randomizer_page_seeds[] = { |
| 631 |
0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72, |
| 632 |
0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436, |
| 633 |
0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d, |
| 634 |
0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130, |
| 635 |
0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56, |
| 636 |
0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55, |
| 637 |
0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb, |
| 638 |
0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17, |
| 639 |
0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62, |
| 640 |
0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064, |
| 641 |
0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126, |
| 642 |
0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e, |
| 643 |
0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3, |
| 644 |
0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b, |
| 645 |
0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d, |
| 646 |
0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db, |
| 647 |
}; |
| 648 |
|
| 649 |
/* |
| 650 |
* sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds |
| 651 |
* have been generated using |
| 652 |
* sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what |
| 653 |
* the randomizer engine does internally before de/scrambling OOB data. |
| 654 |
* |
| 655 |
* Those tables are statically defined to avoid calculating randomizer state |
| 656 |
* at runtime. |
| 657 |
*/ |
| 658 |
static const u16 sunxi_nfc_randomizer_ecc512_seeds[] = { |
| 659 |
0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64, |
| 660 |
0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409, |
| 661 |
0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617, |
| 662 |
0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d, |
| 663 |
0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91, |
| 664 |
0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d, |
| 665 |
0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab, |
| 666 |
0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8, |
| 667 |
0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8, |
| 668 |
0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b, |
| 669 |
0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5, |
| 670 |
0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a, |
| 671 |
0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891, |
| 672 |
0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36, |
| 673 |
0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd, |
| 674 |
0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0, |
| 675 |
}; |
| 676 |
|
| 677 |
static const u16 sunxi_nfc_randomizer_ecc1024_seeds[] = { |
| 678 |
0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6, |
| 679 |
0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982, |
| 680 |
0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9, |
| 681 |
0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07, |
| 682 |
0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e, |
| 683 |
0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2, |
| 684 |
0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c, |
| 685 |
0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f, |
| 686 |
0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc, |
| 687 |
0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e, |
| 688 |
0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8, |
| 689 |
0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68, |
| 690 |
0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d, |
| 691 |
0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179, |
| 692 |
0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601, |
| 693 |
0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd, |
| 694 |
}; |
| 695 |
|
| 696 |
static u16 sunxi_nfc_randomizer_step(u16 state, int count) |
| 697 |
{ |
| 698 |
state &= 0x7fff; |
| 699 |
|
| 700 |
/* |
| 701 |
* This loop is just a simple implementation of a Fibonacci LFSR using |
| 702 |
* the x16 + x15 + 1 polynomial. |
| 703 |
*/ |
| 704 |
while (count--) |
| 705 |
state = ((state >> 1) | |
| 706 |
(((state ^ (state >> 1)) & 1) << 14)) & 0x7fff; |
| 707 |
|
| 708 |
return state; |
| 709 |
} |
| 710 |
|
| 711 |
static u16 sunxi_nfc_randomizer_state(struct nand_chip *nand, int page, |
| 712 |
bool ecc) |
| 713 |
{ |
| 714 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 715 |
const u16 *seeds = sunxi_nfc_randomizer_page_seeds; |
| 716 |
int mod = mtd_div_by_ws(mtd->erasesize, mtd); |
| 717 |
|
| 718 |
if (mod > ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds)) |
| 719 |
mod = ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds); |
| 720 |
|
| 721 |
if (ecc) { |
| 722 |
if (mtd->ecc_step_size == 512) |
| 723 |
seeds = sunxi_nfc_randomizer_ecc512_seeds; |
| 724 |
else |
| 725 |
seeds = sunxi_nfc_randomizer_ecc1024_seeds; |
| 726 |
} |
| 727 |
|
| 728 |
return seeds[page % mod]; |
| 729 |
} |
| 730 |
|
| 731 |
static void sunxi_nfc_randomizer_config(struct nand_chip *nand, int page, |
| 732 |
bool ecc) |
| 733 |
{ |
| 734 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 735 |
u32 ecc_ctl; |
| 736 |
u16 state; |
| 737 |
|
| 738 |
if (!(nand->options & NAND_NEED_SCRAMBLING)) |
| 739 |
return; |
| 740 |
|
| 741 |
state = sunxi_nfc_randomizer_state(nand, page, ecc); |
| 742 |
ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK; |
| 743 |
writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL); |
| 744 |
} |
| 745 |
|
| 746 |
static void sunxi_nfc_randomizer_enable(struct nand_chip *nand) |
| 747 |
{ |
| 748 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 749 |
|
| 750 |
if (!(nand->options & NAND_NEED_SCRAMBLING)) |
| 751 |
return; |
| 752 |
|
| 753 |
writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN(nfc), |
| 754 |
nfc->regs + NFC_REG_ECC_CTL); |
| 755 |
} |
| 756 |
|
| 757 |
static void sunxi_nfc_randomizer_disable(struct nand_chip *nand) |
| 758 |
{ |
| 759 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 760 |
|
| 761 |
if (!(nand->options & NAND_NEED_SCRAMBLING)) |
| 762 |
return; |
| 763 |
|
| 764 |
writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN(nfc), |
| 765 |
nfc->regs + NFC_REG_ECC_CTL); |
| 766 |
} |
| 767 |
|
| 768 |
static void sunxi_nfc_randomize_bbm(struct nand_chip *nand, int page, u8 *bbm) |
| 769 |
{ |
| 770 |
u16 state = sunxi_nfc_randomizer_state(nand, page, true); |
| 771 |
|
| 772 |
bbm[0] ^= state; |
| 773 |
bbm[1] ^= sunxi_nfc_randomizer_step(state, 8); |
| 774 |
} |
| 775 |
|
| 776 |
static void sunxi_nfc_randomizer_write_buf(struct nand_chip *nand, |
| 777 |
const uint8_t *buf, int len, |
| 778 |
bool ecc, int page) |
| 779 |
{ |
| 780 |
sunxi_nfc_randomizer_config(nand, page, ecc); |
| 781 |
sunxi_nfc_randomizer_enable(nand); |
| 782 |
sunxi_nfc_write_buf(nand, buf, len); |
| 783 |
sunxi_nfc_randomizer_disable(nand); |
| 784 |
} |
| 785 |
|
| 786 |
static void sunxi_nfc_randomizer_read_buf(struct nand_chip *nand, uint8_t *buf, |
| 787 |
int len, bool ecc, int page) |
| 788 |
{ |
| 789 |
sunxi_nfc_randomizer_config(nand, page, ecc); |
| 790 |
sunxi_nfc_randomizer_enable(nand); |
| 791 |
sunxi_nfc_read_buf(nand, buf, len); |
| 792 |
sunxi_nfc_randomizer_disable(nand); |
| 793 |
} |
| 794 |
|
| 795 |
static void sunxi_nfc_hw_ecc_enable(struct nand_chip *nand) |
| 796 |
{ |
| 797 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 798 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 799 |
|
| 800 |
writel(sunxi_nand->ecc.ecc_ctl, nfc->regs + NFC_REG_ECC_CTL); |
| 801 |
} |
| 802 |
|
| 803 |
static void sunxi_nfc_hw_ecc_disable(struct nand_chip *nand) |
| 804 |
{ |
| 805 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 806 |
|
| 807 |
writel(0, nfc->regs + NFC_REG_ECC_CTL); |
| 808 |
} |
| 809 |
|
| 810 |
static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf) |
| 811 |
{ |
| 812 |
buf[0] = user_data; |
| 813 |
buf[1] = user_data >> 8; |
| 814 |
buf[2] = user_data >> 16; |
| 815 |
buf[3] = user_data >> 24; |
| 816 |
} |
| 817 |
|
| 818 |
static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf) |
| 819 |
{ |
| 820 |
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 821 |
} |
| 822 |
|
| 823 |
static void sunxi_nfc_hw_ecc_get_prot_oob_bytes(struct nand_chip *nand, u8 *oob, |
| 824 |
int step, bool bbm, int page) |
| 825 |
{ |
| 826 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 827 |
|
| 828 |
sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(nfc, step)), oob); |
| 829 |
|
| 830 |
/* De-randomize the Bad Block Marker. */ |
| 831 |
if (bbm && (nand->options & NAND_NEED_SCRAMBLING)) |
| 832 |
sunxi_nfc_randomize_bbm(nand, page, oob); |
| 833 |
} |
| 834 |
|
| 835 |
/* |
| 836 |
* On H6/H6 the user_data length has to be set in specific registers |
| 837 |
* before writing. |
| 838 |
*/ |
| 839 |
static void sunxi_nfc_reset_user_data_len(struct sunxi_nfc *nfc) |
| 840 |
{ |
| 841 |
int loop_step = NFC_REG_USER_DATA_LEN_CAPACITY; |
| 842 |
|
| 843 |
/* not all SoCs have this register */ |
| 844 |
if (!nfc->caps->reg_user_data_len) |
| 845 |
return; |
| 846 |
|
| 847 |
for (int i = 0; i < nfc->caps->max_ecc_steps; i += loop_step) |
| 848 |
writel(0, nfc->regs + NFC_REG_USER_DATA_LEN(nfc, i)); |
| 849 |
} |
| 850 |
|
| 851 |
static void sunxi_nfc_set_user_data_len(struct sunxi_nfc *nfc, |
| 852 |
int len, int step) |
| 853 |
{ |
| 854 |
bool found = false; |
| 855 |
u32 val; |
| 856 |
int i; |
| 857 |
|
| 858 |
/* not all SoCs have this register */ |
| 859 |
if (!nfc->caps->reg_user_data_len) |
| 860 |
return; |
| 861 |
|
| 862 |
for (i = 0; i < nfc->caps->nuser_data_tab; i++) { |
| 863 |
if (len == nfc->caps->user_data_len_tab[i]) { |
| 864 |
found = true; |
| 865 |
break; |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
if (!found) { |
| 870 |
dev_warn(nfc->dev, |
| 871 |
"Unsupported length for user data reg: %d\n", len); |
| 872 |
return; |
| 873 |
} |
| 874 |
|
| 875 |
val = readl(nfc->regs + NFC_REG_USER_DATA_LEN(nfc, step)); |
| 876 |
|
| 877 |
val &= ~NFC_USER_DATA_LEN_MSK(step); |
| 878 |
val |= field_prep(NFC_USER_DATA_LEN_MSK(step), i); |
| 879 |
writel(val, nfc->regs + NFC_REG_USER_DATA_LEN(nfc, step)); |
| 880 |
} |
| 881 |
|
| 882 |
static void sunxi_nfc_hw_ecc_set_prot_oob_bytes(struct nand_chip *nand, |
| 883 |
const u8 *oob, int step, |
| 884 |
bool bbm, int page) |
| 885 |
{ |
| 886 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 887 |
u8 user_data[USER_DATA_SZ]; |
| 888 |
|
| 889 |
/* Randomize the Bad Block Marker. */ |
| 890 |
if (bbm && (nand->options & NAND_NEED_SCRAMBLING)) { |
| 891 |
memcpy(user_data, oob, sizeof(user_data)); |
| 892 |
sunxi_nfc_randomize_bbm(nand, page, user_data); |
| 893 |
oob = user_data; |
| 894 |
} |
| 895 |
|
| 896 |
writel(sunxi_nfc_buf_to_user_data(oob), |
| 897 |
nfc->regs + NFC_REG_USER_DATA(nfc, step)); |
| 898 |
} |
| 899 |
|
| 900 |
static void sunxi_nfc_hw_ecc_update_stats(struct nand_chip *nand, |
| 901 |
unsigned int *max_bitflips, int ret) |
| 902 |
{ |
| 903 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 904 |
|
| 905 |
if (ret < 0) { |
| 906 |
mtd->ecc_stats.failed++; |
| 907 |
} else { |
| 908 |
mtd->ecc_stats.corrected += ret; |
| 909 |
*max_bitflips = max_t(unsigned int, *max_bitflips, ret); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
static int sunxi_nfc_hw_ecc_correct(struct nand_chip *nand, u8 *data, u8 *oob, |
| 914 |
int step, u32 status, u32 pattern_found, |
| 915 |
bool *erased) |
| 916 |
{ |
| 917 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 918 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 919 |
u32 tmp; |
| 920 |
|
| 921 |
*erased = false; |
| 922 |
|
| 923 |
if (status & NFC_ECC_ERR(step)) |
| 924 |
return -EBADMSG; |
| 925 |
|
| 926 |
if (pattern_found & BIT(step)) { |
| 927 |
u8 pattern; |
| 928 |
|
| 929 |
if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID(nfc)) & 0x1))) { |
| 930 |
pattern = 0x0; |
| 931 |
} else { |
| 932 |
pattern = 0xff; |
| 933 |
*erased = true; |
| 934 |
} |
| 935 |
|
| 936 |
if (data) |
| 937 |
memset(data, pattern, ecc->size); |
| 938 |
|
| 939 |
if (oob) |
| 940 |
memset(oob, pattern, ecc->bytes + USER_DATA_SZ); |
| 941 |
|
| 942 |
return 0; |
| 943 |
} |
| 944 |
|
| 945 |
tmp = readl(nfc->regs + NFC_REG_ECC_ERR_CNT(nfc, step)); |
| 946 |
|
| 947 |
return NFC_ECC_ERR_CNT(step, tmp); |
| 948 |
} |
| 949 |
|
| 950 |
static int sunxi_nfc_hw_ecc_read_chunk(struct nand_chip *nand, |
| 951 |
u8 *data, int data_off, |
| 952 |
u8 *oob, int oob_off, |
| 953 |
int *cur_off, |
| 954 |
unsigned int *max_bitflips, |
| 955 |
bool bbm, bool oob_required, int page) |
| 956 |
{ |
| 957 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 958 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 959 |
int raw_mode = 0; |
| 960 |
u32 pattern_found; |
| 961 |
bool erased; |
| 962 |
int ret; |
| 963 |
|
| 964 |
if (*cur_off != data_off) |
| 965 |
nand_change_read_column_op(nand, data_off, NULL, 0, false); |
| 966 |
|
| 967 |
sunxi_nfc_randomizer_read_buf(nand, NULL, ecc->size, false, page); |
| 968 |
|
| 969 |
if (data_off + ecc->size != oob_off) |
| 970 |
nand_change_read_column_op(nand, oob_off, NULL, 0, false); |
| 971 |
|
| 972 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 973 |
if (ret) |
| 974 |
return ret; |
| 975 |
|
| 976 |
sunxi_nfc_reset_user_data_len(nfc); |
| 977 |
sunxi_nfc_set_user_data_len(nfc, USER_DATA_SZ, 0); |
| 978 |
sunxi_nfc_randomizer_config(nand, page, false); |
| 979 |
sunxi_nfc_randomizer_enable(nand); |
| 980 |
writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP, |
| 981 |
nfc->regs + NFC_REG_CMD); |
| 982 |
|
| 983 |
ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0); |
| 984 |
sunxi_nfc_randomizer_disable(nand); |
| 985 |
if (ret) |
| 986 |
return ret; |
| 987 |
|
| 988 |
*cur_off = oob_off + ecc->bytes + USER_DATA_SZ; |
| 989 |
|
| 990 |
pattern_found = readl(nfc->regs + nfc->caps->reg_pat_found); |
| 991 |
pattern_found = field_get(NFC_ECC_PAT_FOUND_MSK(nfc), pattern_found); |
| 992 |
|
| 993 |
ret = sunxi_nfc_hw_ecc_correct(nand, data, oob_required ? oob : NULL, 0, |
| 994 |
readl(nfc->regs + NFC_REG_ECC_ST), |
| 995 |
pattern_found, |
| 996 |
&erased); |
| 997 |
if (erased) |
| 998 |
return 1; |
| 999 |
|
| 1000 |
if (ret < 0) { |
| 1001 |
/* |
| 1002 |
* Re-read the data with the randomizer disabled to identify |
| 1003 |
* bitflips in erased pages. |
| 1004 |
*/ |
| 1005 |
if (nand->options & NAND_NEED_SCRAMBLING) |
| 1006 |
nand_change_read_column_op(nand, data_off, data, |
| 1007 |
ecc->size, false); |
| 1008 |
else |
| 1009 |
memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, |
| 1010 |
ecc->size); |
| 1011 |
|
| 1012 |
nand_change_read_column_op(nand, oob_off, oob, |
| 1013 |
ecc->bytes + USER_DATA_SZ, false); |
| 1014 |
|
| 1015 |
ret = nand_check_erased_ecc_chunk(data, ecc->size, oob, |
| 1016 |
ecc->bytes + USER_DATA_SZ, |
| 1017 |
NULL, 0, ecc->strength); |
| 1018 |
if (ret >= 0) |
| 1019 |
raw_mode = 1; |
| 1020 |
} else { |
| 1021 |
memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size); |
| 1022 |
|
| 1023 |
if (oob_required) { |
| 1024 |
nand_change_read_column_op(nand, oob_off, NULL, 0, |
| 1025 |
false); |
| 1026 |
sunxi_nfc_randomizer_read_buf(nand, oob, ecc->bytes + USER_DATA_SZ, |
| 1027 |
true, page); |
| 1028 |
|
| 1029 |
sunxi_nfc_hw_ecc_get_prot_oob_bytes(nand, oob, 0, |
| 1030 |
bbm, page); |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
sunxi_nfc_hw_ecc_update_stats(nand, max_bitflips, ret); |
| 1035 |
|
| 1036 |
return raw_mode; |
| 1037 |
} |
| 1038 |
|
| 1039 |
static void sunxi_nfc_hw_ecc_read_extra_oob(struct nand_chip *nand, |
| 1040 |
u8 *oob, int *cur_off, |
| 1041 |
bool randomize, int page) |
| 1042 |
{ |
| 1043 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1044 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1045 |
int offset = ((ecc->bytes + 4) * ecc->steps); |
| 1046 |
int len = mtd->oobsize - offset; |
| 1047 |
|
| 1048 |
if (len <= 0) |
| 1049 |
return; |
| 1050 |
|
| 1051 |
if (!cur_off || *cur_off != (offset + mtd->writesize)) |
| 1052 |
nand_change_read_column_op(nand, mtd->writesize + offset, |
| 1053 |
NULL, 0, false); |
| 1054 |
|
| 1055 |
if (!randomize) |
| 1056 |
sunxi_nfc_read_buf(nand, oob + offset, len); |
| 1057 |
else |
| 1058 |
sunxi_nfc_randomizer_read_buf(nand, oob + offset, len, |
| 1059 |
false, page); |
| 1060 |
|
| 1061 |
if (cur_off) |
| 1062 |
*cur_off = mtd->oobsize + mtd->writesize; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, uint8_t *buf, |
| 1066 |
int oob_required, int page, |
| 1067 |
int nchunks) |
| 1068 |
{ |
| 1069 |
bool randomized = nand->options & NAND_NEED_SCRAMBLING; |
| 1070 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 1071 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1072 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1073 |
unsigned int max_bitflips = 0; |
| 1074 |
int ret, i, raw_mode = 0; |
| 1075 |
struct scatterlist sg; |
| 1076 |
u32 status, pattern_found, wait; |
| 1077 |
|
| 1078 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 1079 |
if (ret) |
| 1080 |
return ret; |
| 1081 |
|
| 1082 |
ret = sunxi_nfc_dma_op_prepare(nfc, buf, ecc->size, nchunks, |
| 1083 |
DMA_FROM_DEVICE, &sg); |
| 1084 |
if (ret) |
| 1085 |
return ret; |
| 1086 |
|
| 1087 |
sunxi_nfc_hw_ecc_enable(nand); |
| 1088 |
sunxi_nfc_reset_user_data_len(nfc); |
| 1089 |
sunxi_nfc_set_user_data_len(nfc, USER_DATA_SZ, 0); |
| 1090 |
sunxi_nfc_randomizer_config(nand, page, false); |
| 1091 |
sunxi_nfc_randomizer_enable(nand); |
| 1092 |
|
| 1093 |
writel((NAND_CMD_RNDOUTSTART << 16) | (NAND_CMD_RNDOUT << 8) | |
| 1094 |
NAND_CMD_READSTART, nfc->regs + NFC_REG_RCMD_SET); |
| 1095 |
|
| 1096 |
wait = NFC_CMD_INT_FLAG; |
| 1097 |
|
| 1098 |
if (nfc->caps->has_mdma) |
| 1099 |
wait |= NFC_DMA_INT_FLAG; |
| 1100 |
else |
| 1101 |
dma_async_issue_pending(nfc->dmac); |
| 1102 |
|
| 1103 |
writel(NFC_PAGE_OP | NFC_DATA_SWAP_METHOD | NFC_DATA_TRANS, |
| 1104 |
nfc->regs + NFC_REG_CMD); |
| 1105 |
|
| 1106 |
ret = sunxi_nfc_wait_events(nfc, wait, false, 0); |
| 1107 |
if (ret && !nfc->caps->has_mdma) |
| 1108 |
dmaengine_terminate_all(nfc->dmac); |
| 1109 |
|
| 1110 |
sunxi_nfc_randomizer_disable(nand); |
| 1111 |
sunxi_nfc_hw_ecc_disable(nand); |
| 1112 |
|
| 1113 |
sunxi_nfc_dma_op_cleanup(nfc, DMA_FROM_DEVICE, &sg); |
| 1114 |
|
| 1115 |
if (ret) |
| 1116 |
return ret; |
| 1117 |
|
| 1118 |
status = readl(nfc->regs + NFC_REG_ECC_ST); |
| 1119 |
pattern_found = readl(nfc->regs + nfc->caps->reg_pat_found); |
| 1120 |
pattern_found = field_get(NFC_ECC_PAT_FOUND_MSK(nfc), pattern_found); |
| 1121 |
|
| 1122 |
for (i = 0; i < nchunks; i++) { |
| 1123 |
int data_off = i * ecc->size; |
| 1124 |
int oob_off = i * (ecc->bytes + USER_DATA_SZ); |
| 1125 |
u8 *data = buf + data_off; |
| 1126 |
u8 *oob = nand->oob_poi + oob_off; |
| 1127 |
bool erased; |
| 1128 |
|
| 1129 |
ret = sunxi_nfc_hw_ecc_correct(nand, randomized ? data : NULL, |
| 1130 |
oob_required ? oob : NULL, |
| 1131 |
i, status, pattern_found, |
| 1132 |
&erased); |
| 1133 |
|
| 1134 |
/* ECC errors are handled in the second loop. */ |
| 1135 |
if (ret < 0) |
| 1136 |
continue; |
| 1137 |
|
| 1138 |
if (oob_required && !erased) { |
| 1139 |
/* TODO: use DMA to retrieve OOB */ |
| 1140 |
nand_change_read_column_op(nand, |
| 1141 |
mtd->writesize + oob_off, |
| 1142 |
oob, ecc->bytes + USER_DATA_SZ, false); |
| 1143 |
|
| 1144 |
sunxi_nfc_hw_ecc_get_prot_oob_bytes(nand, oob, i, |
| 1145 |
!i, page); |
| 1146 |
} |
| 1147 |
|
| 1148 |
if (erased) |
| 1149 |
raw_mode = 1; |
| 1150 |
|
| 1151 |
sunxi_nfc_hw_ecc_update_stats(nand, &max_bitflips, ret); |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (status & NFC_ECC_ERR_MSK(nfc)) { |
| 1155 |
for (i = 0; i < nchunks; i++) { |
| 1156 |
int data_off = i * ecc->size; |
| 1157 |
int oob_off = i * (ecc->bytes + USER_DATA_SZ); |
| 1158 |
u8 *data = buf + data_off; |
| 1159 |
u8 *oob = nand->oob_poi + oob_off; |
| 1160 |
|
| 1161 |
if (!(status & NFC_ECC_ERR(i))) |
| 1162 |
continue; |
| 1163 |
|
| 1164 |
/* |
| 1165 |
* Re-read the data with the randomizer disabled to |
| 1166 |
* identify bitflips in erased pages. |
| 1167 |
* TODO: use DMA to read page in raw mode |
| 1168 |
*/ |
| 1169 |
if (randomized) |
| 1170 |
nand_change_read_column_op(nand, data_off, |
| 1171 |
data, ecc->size, |
| 1172 |
false); |
| 1173 |
|
| 1174 |
/* TODO: use DMA to retrieve OOB */ |
| 1175 |
nand_change_read_column_op(nand, |
| 1176 |
mtd->writesize + oob_off, |
| 1177 |
oob, ecc->bytes + USER_DATA_SZ, false); |
| 1178 |
|
| 1179 |
ret = nand_check_erased_ecc_chunk(data, ecc->size, oob, |
| 1180 |
ecc->bytes + USER_DATA_SZ, |
| 1181 |
NULL, 0, |
| 1182 |
ecc->strength); |
| 1183 |
if (ret >= 0) |
| 1184 |
raw_mode = 1; |
| 1185 |
|
| 1186 |
sunxi_nfc_hw_ecc_update_stats(nand, &max_bitflips, ret); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
if (oob_required) |
| 1191 |
sunxi_nfc_hw_ecc_read_extra_oob(nand, nand->oob_poi, |
| 1192 |
NULL, !raw_mode, |
| 1193 |
page); |
| 1194 |
|
| 1195 |
return max_bitflips; |
| 1196 |
} |
| 1197 |
|
| 1198 |
static int sunxi_nfc_hw_ecc_write_chunk(struct nand_chip *nand, |
| 1199 |
const u8 *data, int data_off, |
| 1200 |
const u8 *oob, int oob_off, |
| 1201 |
int *cur_off, bool bbm, |
| 1202 |
int page) |
| 1203 |
{ |
| 1204 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 1205 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1206 |
int ret; |
| 1207 |
|
| 1208 |
if (data_off != *cur_off) |
| 1209 |
nand_change_write_column_op(nand, data_off, NULL, 0, false); |
| 1210 |
|
| 1211 |
sunxi_nfc_randomizer_write_buf(nand, data, ecc->size, false, page); |
| 1212 |
|
| 1213 |
if (data_off + ecc->size != oob_off) |
| 1214 |
nand_change_write_column_op(nand, oob_off, NULL, 0, false); |
| 1215 |
|
| 1216 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 1217 |
if (ret) |
| 1218 |
return ret; |
| 1219 |
|
| 1220 |
sunxi_nfc_randomizer_config(nand, page, false); |
| 1221 |
sunxi_nfc_randomizer_enable(nand); |
| 1222 |
sunxi_nfc_reset_user_data_len(nfc); |
| 1223 |
sunxi_nfc_set_user_data_len(nfc, USER_DATA_SZ, 0); |
| 1224 |
sunxi_nfc_hw_ecc_set_prot_oob_bytes(nand, oob, 0, bbm, page); |
| 1225 |
|
| 1226 |
writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | |
| 1227 |
NFC_ACCESS_DIR | NFC_ECC_OP, |
| 1228 |
nfc->regs + NFC_REG_CMD); |
| 1229 |
|
| 1230 |
ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0); |
| 1231 |
sunxi_nfc_randomizer_disable(nand); |
| 1232 |
if (ret) |
| 1233 |
return ret; |
| 1234 |
|
| 1235 |
*cur_off = oob_off + ecc->bytes + USER_DATA_SZ; |
| 1236 |
|
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
static void sunxi_nfc_hw_ecc_write_extra_oob(struct nand_chip *nand, |
| 1241 |
u8 *oob, int *cur_off, |
| 1242 |
int page) |
| 1243 |
{ |
| 1244 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1245 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1246 |
int offset = ((ecc->bytes + USER_DATA_SZ) * ecc->steps); |
| 1247 |
int len = mtd->oobsize - offset; |
| 1248 |
|
| 1249 |
if (len <= 0) |
| 1250 |
return; |
| 1251 |
|
| 1252 |
if (!cur_off || *cur_off != offset) |
| 1253 |
nand_change_write_column_op(nand, offset + mtd->writesize, |
| 1254 |
NULL, 0, false); |
| 1255 |
|
| 1256 |
sunxi_nfc_randomizer_write_buf(nand, oob + offset, len, false, page); |
| 1257 |
|
| 1258 |
if (cur_off) |
| 1259 |
*cur_off = mtd->oobsize + mtd->writesize; |
| 1260 |
} |
| 1261 |
|
| 1262 |
static int sunxi_nfc_hw_ecc_read_page(struct nand_chip *nand, uint8_t *buf, |
| 1263 |
int oob_required, int page) |
| 1264 |
{ |
| 1265 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1266 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1267 |
unsigned int max_bitflips = 0; |
| 1268 |
int ret, i, cur_off = 0; |
| 1269 |
bool raw_mode = false; |
| 1270 |
|
| 1271 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1272 |
|
| 1273 |
nand_read_page_op(nand, page, 0, NULL, 0); |
| 1274 |
|
| 1275 |
sunxi_nfc_hw_ecc_enable(nand); |
| 1276 |
|
| 1277 |
for (i = 0; i < ecc->steps; i++) { |
| 1278 |
int data_off = i * ecc->size; |
| 1279 |
int oob_off = i * (ecc->bytes + USER_DATA_SZ); |
| 1280 |
u8 *data = buf + data_off; |
| 1281 |
u8 *oob = nand->oob_poi + oob_off; |
| 1282 |
|
| 1283 |
ret = sunxi_nfc_hw_ecc_read_chunk(nand, data, data_off, oob, |
| 1284 |
oob_off + mtd->writesize, |
| 1285 |
&cur_off, &max_bitflips, |
| 1286 |
!i, oob_required, page); |
| 1287 |
if (ret < 0) |
| 1288 |
return ret; |
| 1289 |
else if (ret) |
| 1290 |
raw_mode = true; |
| 1291 |
} |
| 1292 |
|
| 1293 |
if (oob_required) |
| 1294 |
sunxi_nfc_hw_ecc_read_extra_oob(nand, nand->oob_poi, &cur_off, |
| 1295 |
!raw_mode, page); |
| 1296 |
|
| 1297 |
sunxi_nfc_hw_ecc_disable(nand); |
| 1298 |
|
| 1299 |
return max_bitflips; |
| 1300 |
} |
| 1301 |
|
| 1302 |
static int sunxi_nfc_hw_ecc_read_page_dma(struct nand_chip *nand, u8 *buf, |
| 1303 |
int oob_required, int page) |
| 1304 |
{ |
| 1305 |
int ret; |
| 1306 |
|
| 1307 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1308 |
|
| 1309 |
nand_read_page_op(nand, page, 0, NULL, 0); |
| 1310 |
|
| 1311 |
ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, oob_required, page, |
| 1312 |
nand->ecc.steps); |
| 1313 |
if (ret >= 0) |
| 1314 |
return ret; |
| 1315 |
|
| 1316 |
/* Fallback to PIO mode */ |
| 1317 |
return sunxi_nfc_hw_ecc_read_page(nand, buf, oob_required, page); |
| 1318 |
} |
| 1319 |
|
| 1320 |
static int sunxi_nfc_hw_ecc_read_subpage(struct nand_chip *nand, |
| 1321 |
u32 data_offs, u32 readlen, |
| 1322 |
u8 *bufpoi, int page) |
| 1323 |
{ |
| 1324 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1325 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1326 |
int ret, i, cur_off = 0; |
| 1327 |
unsigned int max_bitflips = 0; |
| 1328 |
|
| 1329 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1330 |
|
| 1331 |
nand_read_page_op(nand, page, 0, NULL, 0); |
| 1332 |
|
| 1333 |
sunxi_nfc_hw_ecc_enable(nand); |
| 1334 |
|
| 1335 |
for (i = data_offs / ecc->size; |
| 1336 |
i < DIV_ROUND_UP(data_offs + readlen, ecc->size); i++) { |
| 1337 |
int data_off = i * ecc->size; |
| 1338 |
int oob_off = i * (ecc->bytes + USER_DATA_SZ); |
| 1339 |
u8 *data = bufpoi + data_off; |
| 1340 |
u8 *oob = nand->oob_poi + oob_off; |
| 1341 |
|
| 1342 |
ret = sunxi_nfc_hw_ecc_read_chunk(nand, data, data_off, |
| 1343 |
oob, |
| 1344 |
oob_off + mtd->writesize, |
| 1345 |
&cur_off, &max_bitflips, !i, |
| 1346 |
false, page); |
| 1347 |
if (ret < 0) |
| 1348 |
return ret; |
| 1349 |
} |
| 1350 |
|
| 1351 |
sunxi_nfc_hw_ecc_disable(nand); |
| 1352 |
|
| 1353 |
return max_bitflips; |
| 1354 |
} |
| 1355 |
|
| 1356 |
static int sunxi_nfc_hw_ecc_read_subpage_dma(struct nand_chip *nand, |
| 1357 |
u32 data_offs, u32 readlen, |
| 1358 |
u8 *buf, int page) |
| 1359 |
{ |
| 1360 |
int nchunks = DIV_ROUND_UP(data_offs + readlen, nand->ecc.size); |
| 1361 |
int ret; |
| 1362 |
|
| 1363 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1364 |
|
| 1365 |
nand_read_page_op(nand, page, 0, NULL, 0); |
| 1366 |
|
| 1367 |
ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, false, page, nchunks); |
| 1368 |
if (ret >= 0) |
| 1369 |
return ret; |
| 1370 |
|
| 1371 |
/* Fallback to PIO mode */ |
| 1372 |
return sunxi_nfc_hw_ecc_read_subpage(nand, data_offs, readlen, |
| 1373 |
buf, page); |
| 1374 |
} |
| 1375 |
|
| 1376 |
static int sunxi_nfc_hw_ecc_write_page(struct nand_chip *nand, |
| 1377 |
const uint8_t *buf, int oob_required, |
| 1378 |
int page) |
| 1379 |
{ |
| 1380 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1381 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1382 |
int ret, i, cur_off = 0; |
| 1383 |
|
| 1384 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1385 |
|
| 1386 |
nand_prog_page_begin_op(nand, page, 0, NULL, 0); |
| 1387 |
|
| 1388 |
sunxi_nfc_hw_ecc_enable(nand); |
| 1389 |
|
| 1390 |
for (i = 0; i < ecc->steps; i++) { |
| 1391 |
int data_off = i * ecc->size; |
| 1392 |
int oob_off = i * (ecc->bytes + USER_DATA_SZ); |
| 1393 |
const u8 *data = buf + data_off; |
| 1394 |
const u8 *oob = nand->oob_poi + oob_off; |
| 1395 |
|
| 1396 |
ret = sunxi_nfc_hw_ecc_write_chunk(nand, data, data_off, oob, |
| 1397 |
oob_off + mtd->writesize, |
| 1398 |
&cur_off, !i, page); |
| 1399 |
if (ret) |
| 1400 |
return ret; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (oob_required || (nand->options & NAND_NEED_SCRAMBLING)) |
| 1404 |
sunxi_nfc_hw_ecc_write_extra_oob(nand, nand->oob_poi, |
| 1405 |
&cur_off, page); |
| 1406 |
|
| 1407 |
sunxi_nfc_hw_ecc_disable(nand); |
| 1408 |
|
| 1409 |
return nand_prog_page_end_op(nand); |
| 1410 |
} |
| 1411 |
|
| 1412 |
static int sunxi_nfc_hw_ecc_write_subpage(struct nand_chip *nand, |
| 1413 |
u32 data_offs, u32 data_len, |
| 1414 |
const u8 *buf, int oob_required, |
| 1415 |
int page) |
| 1416 |
{ |
| 1417 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1418 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1419 |
int ret, i, cur_off = 0; |
| 1420 |
|
| 1421 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1422 |
|
| 1423 |
nand_prog_page_begin_op(nand, page, 0, NULL, 0); |
| 1424 |
|
| 1425 |
sunxi_nfc_hw_ecc_enable(nand); |
| 1426 |
|
| 1427 |
for (i = data_offs / ecc->size; |
| 1428 |
i < DIV_ROUND_UP(data_offs + data_len, ecc->size); i++) { |
| 1429 |
int data_off = i * ecc->size; |
| 1430 |
int oob_off = i * (ecc->bytes + USER_DATA_SZ); |
| 1431 |
const u8 *data = buf + data_off; |
| 1432 |
const u8 *oob = nand->oob_poi + oob_off; |
| 1433 |
|
| 1434 |
ret = sunxi_nfc_hw_ecc_write_chunk(nand, data, data_off, oob, |
| 1435 |
oob_off + mtd->writesize, |
| 1436 |
&cur_off, !i, page); |
| 1437 |
if (ret) |
| 1438 |
return ret; |
| 1439 |
} |
| 1440 |
|
| 1441 |
sunxi_nfc_hw_ecc_disable(nand); |
| 1442 |
|
| 1443 |
return nand_prog_page_end_op(nand); |
| 1444 |
} |
| 1445 |
|
| 1446 |
static int sunxi_nfc_hw_ecc_write_page_dma(struct nand_chip *nand, |
| 1447 |
const u8 *buf, |
| 1448 |
int oob_required, |
| 1449 |
int page) |
| 1450 |
{ |
| 1451 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 1452 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1453 |
struct scatterlist sg; |
| 1454 |
u32 wait; |
| 1455 |
int ret, i; |
| 1456 |
|
| 1457 |
sunxi_nfc_select_chip(nand, nand->cur_cs); |
| 1458 |
|
| 1459 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 1460 |
if (ret) |
| 1461 |
return ret; |
| 1462 |
|
| 1463 |
ret = sunxi_nfc_dma_op_prepare(nfc, buf, ecc->size, ecc->steps, |
| 1464 |
DMA_TO_DEVICE, &sg); |
| 1465 |
if (ret) |
| 1466 |
goto pio_fallback; |
| 1467 |
|
| 1468 |
sunxi_nfc_reset_user_data_len(nfc); |
| 1469 |
for (i = 0; i < ecc->steps; i++) { |
| 1470 |
const u8 *oob = nand->oob_poi + (i * (ecc->bytes + USER_DATA_SZ)); |
| 1471 |
|
| 1472 |
sunxi_nfc_hw_ecc_set_prot_oob_bytes(nand, oob, i, !i, page); |
| 1473 |
sunxi_nfc_set_user_data_len(nfc, USER_DATA_SZ, i); |
| 1474 |
} |
| 1475 |
|
| 1476 |
nand_prog_page_begin_op(nand, page, 0, NULL, 0); |
| 1477 |
|
| 1478 |
sunxi_nfc_hw_ecc_enable(nand); |
| 1479 |
sunxi_nfc_randomizer_config(nand, page, false); |
| 1480 |
sunxi_nfc_randomizer_enable(nand); |
| 1481 |
|
| 1482 |
writel((NAND_CMD_RNDIN << 8) | NAND_CMD_PAGEPROG, |
| 1483 |
nfc->regs + NFC_REG_WCMD_SET); |
| 1484 |
|
| 1485 |
wait = NFC_CMD_INT_FLAG; |
| 1486 |
|
| 1487 |
if (nfc->caps->has_mdma) |
| 1488 |
wait |= NFC_DMA_INT_FLAG; |
| 1489 |
else |
| 1490 |
dma_async_issue_pending(nfc->dmac); |
| 1491 |
|
| 1492 |
writel(NFC_PAGE_OP | NFC_DATA_SWAP_METHOD | |
| 1493 |
NFC_DATA_TRANS | NFC_ACCESS_DIR, |
| 1494 |
nfc->regs + NFC_REG_CMD); |
| 1495 |
|
| 1496 |
ret = sunxi_nfc_wait_events(nfc, wait, false, 0); |
| 1497 |
if (ret && !nfc->caps->has_mdma) |
| 1498 |
dmaengine_terminate_all(nfc->dmac); |
| 1499 |
|
| 1500 |
sunxi_nfc_randomizer_disable(nand); |
| 1501 |
sunxi_nfc_hw_ecc_disable(nand); |
| 1502 |
|
| 1503 |
sunxi_nfc_dma_op_cleanup(nfc, DMA_TO_DEVICE, &sg); |
| 1504 |
|
| 1505 |
if (ret) |
| 1506 |
return ret; |
| 1507 |
|
| 1508 |
if (oob_required || (nand->options & NAND_NEED_SCRAMBLING)) |
| 1509 |
/* TODO: use DMA to transfer extra OOB bytes ? */ |
| 1510 |
sunxi_nfc_hw_ecc_write_extra_oob(nand, nand->oob_poi, |
| 1511 |
NULL, page); |
| 1512 |
|
| 1513 |
return nand_prog_page_end_op(nand); |
| 1514 |
|
| 1515 |
pio_fallback: |
| 1516 |
return sunxi_nfc_hw_ecc_write_page(nand, buf, oob_required, page); |
| 1517 |
} |
| 1518 |
|
| 1519 |
static int sunxi_nfc_hw_ecc_read_oob(struct nand_chip *nand, int page) |
| 1520 |
{ |
| 1521 |
u8 *buf = nand_get_data_buf(nand); |
| 1522 |
|
| 1523 |
return nand->ecc.read_page(nand, buf, 1, page); |
| 1524 |
} |
| 1525 |
|
| 1526 |
static int sunxi_nfc_hw_ecc_write_oob(struct nand_chip *nand, int page) |
| 1527 |
{ |
| 1528 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1529 |
u8 *buf = nand_get_data_buf(nand); |
| 1530 |
int ret; |
| 1531 |
|
| 1532 |
memset(buf, 0xff, mtd->writesize); |
| 1533 |
ret = nand->ecc.write_page(nand, buf, 1, page); |
| 1534 |
if (ret) |
| 1535 |
return ret; |
| 1536 |
|
| 1537 |
/* Send command to program the OOB data */ |
| 1538 |
return nand_prog_page_end_op(nand); |
| 1539 |
} |
| 1540 |
|
| 1541 |
static const s32 tWB_lut[] = {6, 12, 16, 20}; |
| 1542 |
static const s32 tRHW_lut[] = {4, 8, 12, 20}; |
| 1543 |
|
| 1544 |
static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration, |
| 1545 |
u32 clk_period) |
| 1546 |
{ |
| 1547 |
u32 clk_cycles = DIV_ROUND_UP(duration, clk_period); |
| 1548 |
int i; |
| 1549 |
|
| 1550 |
for (i = 0; i < lut_size; i++) { |
| 1551 |
if (clk_cycles <= lut[i]) |
| 1552 |
return i; |
| 1553 |
} |
| 1554 |
|
| 1555 |
/* Doesn't fit */ |
| 1556 |
return -EINVAL; |
| 1557 |
} |
| 1558 |
|
| 1559 |
#define sunxi_nand_lookup_timing(l, p, c) \ |
| 1560 |
_sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c) |
| 1561 |
|
| 1562 |
static int sunxi_nfc_setup_interface(struct nand_chip *nand, int csline, |
| 1563 |
const struct nand_interface_config *conf) |
| 1564 |
{ |
| 1565 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 1566 |
struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 1567 |
const struct nand_sdr_timings *timings; |
| 1568 |
u32 min_clk_period = 0; |
| 1569 |
s32 tWB, tADL, tWHR, tRHW, tCAD; |
| 1570 |
long real_clk_rate; |
| 1571 |
|
| 1572 |
timings = nand_get_sdr_timings(conf); |
| 1573 |
if (IS_ERR(timings)) |
| 1574 |
return -ENOTSUPP; |
| 1575 |
|
| 1576 |
/* T1 <=> tCLS */ |
| 1577 |
if (timings->tCLS_min > min_clk_period) |
| 1578 |
min_clk_period = timings->tCLS_min; |
| 1579 |
|
| 1580 |
/* T2 <=> tCLH */ |
| 1581 |
if (timings->tCLH_min > min_clk_period) |
| 1582 |
min_clk_period = timings->tCLH_min; |
| 1583 |
|
| 1584 |
/* T3 <=> tCS */ |
| 1585 |
if (timings->tCS_min > min_clk_period) |
| 1586 |
min_clk_period = timings->tCS_min; |
| 1587 |
|
| 1588 |
/* T4 <=> tCH */ |
| 1589 |
if (timings->tCH_min > min_clk_period) |
| 1590 |
min_clk_period = timings->tCH_min; |
| 1591 |
|
| 1592 |
/* T5 <=> tWP */ |
| 1593 |
if (timings->tWP_min > min_clk_period) |
| 1594 |
min_clk_period = timings->tWP_min; |
| 1595 |
|
| 1596 |
/* T6 <=> tWH */ |
| 1597 |
if (timings->tWH_min > min_clk_period) |
| 1598 |
min_clk_period = timings->tWH_min; |
| 1599 |
|
| 1600 |
/* T7 <=> tALS */ |
| 1601 |
if (timings->tALS_min > min_clk_period) |
| 1602 |
min_clk_period = timings->tALS_min; |
| 1603 |
|
| 1604 |
/* T8 <=> tDS */ |
| 1605 |
if (timings->tDS_min > min_clk_period) |
| 1606 |
min_clk_period = timings->tDS_min; |
| 1607 |
|
| 1608 |
/* T9 <=> tDH */ |
| 1609 |
if (timings->tDH_min > min_clk_period) |
| 1610 |
min_clk_period = timings->tDH_min; |
| 1611 |
|
| 1612 |
/* T10 <=> tRR */ |
| 1613 |
if (timings->tRR_min > (min_clk_period * 3)) |
| 1614 |
min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3); |
| 1615 |
|
| 1616 |
/* T11 <=> tALH */ |
| 1617 |
if (timings->tALH_min > min_clk_period) |
| 1618 |
min_clk_period = timings->tALH_min; |
| 1619 |
|
| 1620 |
/* T12 <=> tRP */ |
| 1621 |
if (timings->tRP_min > min_clk_period) |
| 1622 |
min_clk_period = timings->tRP_min; |
| 1623 |
|
| 1624 |
/* T13 <=> tREH */ |
| 1625 |
if (timings->tREH_min > min_clk_period) |
| 1626 |
min_clk_period = timings->tREH_min; |
| 1627 |
|
| 1628 |
/* T14 <=> tRC */ |
| 1629 |
if (timings->tRC_min > (min_clk_period * 2)) |
| 1630 |
min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2); |
| 1631 |
|
| 1632 |
/* T15 <=> tWC */ |
| 1633 |
if (timings->tWC_min > (min_clk_period * 2)) |
| 1634 |
min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2); |
| 1635 |
|
| 1636 |
/* T16 - T19 + tCAD */ |
| 1637 |
if (timings->tWB_max > (min_clk_period * 20)) |
| 1638 |
min_clk_period = DIV_ROUND_UP(timings->tWB_max, 20); |
| 1639 |
|
| 1640 |
if (timings->tADL_min > (min_clk_period * 32)) |
| 1641 |
min_clk_period = DIV_ROUND_UP(timings->tADL_min, 32); |
| 1642 |
|
| 1643 |
if (timings->tWHR_min > (min_clk_period * 32)) |
| 1644 |
min_clk_period = DIV_ROUND_UP(timings->tWHR_min, 32); |
| 1645 |
|
| 1646 |
if (timings->tRHW_min > (min_clk_period * 20)) |
| 1647 |
min_clk_period = DIV_ROUND_UP(timings->tRHW_min, 20); |
| 1648 |
|
| 1649 |
/* |
| 1650 |
* In non-EDO, tREA should be less than tRP to guarantee that the |
| 1651 |
* controller does not sample the IO lines too early. Unfortunately, |
| 1652 |
* the sunxi NAND controller does not allow us to have different |
| 1653 |
* values for tRP and tREH (tRP = tREH = tRW / 2). |
| 1654 |
* |
| 1655 |
* We have 2 options to overcome this limitation: |
| 1656 |
* |
| 1657 |
* 1/ Extend tRC to fulfil the tREA <= tRC / 2 constraint |
| 1658 |
* 2/ Use EDO mode (only works if timings->tRLOH > 0) |
| 1659 |
*/ |
| 1660 |
if (timings->tREA_max > min_clk_period && !timings->tRLOH_min) |
| 1661 |
min_clk_period = timings->tREA_max; |
| 1662 |
|
| 1663 |
tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max, |
| 1664 |
min_clk_period); |
| 1665 |
if (tWB < 0) { |
| 1666 |
dev_err(nfc->dev, "unsupported tWB\n"); |
| 1667 |
return tWB; |
| 1668 |
} |
| 1669 |
|
| 1670 |
tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3; |
| 1671 |
if (tADL > 3) { |
| 1672 |
dev_err(nfc->dev, "unsupported tADL\n"); |
| 1673 |
return -EINVAL; |
| 1674 |
} |
| 1675 |
|
| 1676 |
tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3; |
| 1677 |
if (tWHR > 3) { |
| 1678 |
dev_err(nfc->dev, "unsupported tWHR\n"); |
| 1679 |
return -EINVAL; |
| 1680 |
} |
| 1681 |
|
| 1682 |
tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min, |
| 1683 |
min_clk_period); |
| 1684 |
if (tRHW < 0) { |
| 1685 |
dev_err(nfc->dev, "unsupported tRHW\n"); |
| 1686 |
return tRHW; |
| 1687 |
} |
| 1688 |
|
| 1689 |
if (csline == NAND_DATA_IFACE_CHECK_ONLY) |
| 1690 |
return 0; |
| 1691 |
|
| 1692 |
/* |
| 1693 |
* TODO: according to ONFI specs this value only applies for DDR NAND, |
| 1694 |
* but Allwinner seems to set this to 0x7. Mimic them for now. |
| 1695 |
*/ |
| 1696 |
tCAD = 0x7; |
| 1697 |
|
| 1698 |
/* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */ |
| 1699 |
sunxi_nand->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD); |
| 1700 |
|
| 1701 |
/* Convert min_clk_period from picoseconds to nanoseconds */ |
| 1702 |
min_clk_period = DIV_ROUND_UP(min_clk_period, 1000); |
| 1703 |
|
| 1704 |
/* |
| 1705 |
* Unlike what is stated in Allwinner datasheet, the clk_rate should |
| 1706 |
* be set to (1 / min_clk_period), and not (2 / min_clk_period). |
| 1707 |
* This new formula was verified with a scope and validated by |
| 1708 |
* Allwinner engineers. |
| 1709 |
*/ |
| 1710 |
sunxi_nand->clk_rate = NSEC_PER_SEC / min_clk_period; |
| 1711 |
real_clk_rate = clk_round_rate(nfc->mod_clk, sunxi_nand->clk_rate); |
| 1712 |
if (real_clk_rate <= 0) { |
| 1713 |
dev_err(nfc->dev, "Unable to round clk %lu\n", |
| 1714 |
sunxi_nand->clk_rate); |
| 1715 |
return -EINVAL; |
| 1716 |
} |
| 1717 |
|
| 1718 |
sunxi_nand->timing_ctl = 0; |
| 1719 |
|
| 1720 |
/* |
| 1721 |
* ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data |
| 1722 |
* output cycle timings shall be used if the host drives tRC less than |
| 1723 |
* 30 ns. We should also use EDO mode if tREA is bigger than tRP. |
| 1724 |
*/ |
| 1725 |
min_clk_period = NSEC_PER_SEC / real_clk_rate; |
| 1726 |
if (min_clk_period * 2 < 30 || min_clk_period * 1000 < timings->tREA_max) |
| 1727 |
sunxi_nand->timing_ctl = NFC_TIMING_CTL_EDO; |
| 1728 |
|
| 1729 |
return 0; |
| 1730 |
} |
| 1731 |
|
| 1732 |
static int sunxi_nand_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 1733 |
struct mtd_oob_region *oobregion) |
| 1734 |
{ |
| 1735 |
struct nand_chip *nand = mtd_to_nand(mtd); |
| 1736 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1737 |
|
| 1738 |
if (section >= ecc->steps) |
| 1739 |
return -ERANGE; |
| 1740 |
|
| 1741 |
oobregion->offset = section * (ecc->bytes + USER_DATA_SZ) + 4; |
| 1742 |
oobregion->length = ecc->bytes; |
| 1743 |
|
| 1744 |
return 0; |
| 1745 |
} |
| 1746 |
|
| 1747 |
static int sunxi_nand_ooblayout_free(struct mtd_info *mtd, int section, |
| 1748 |
struct mtd_oob_region *oobregion) |
| 1749 |
{ |
| 1750 |
struct nand_chip *nand = mtd_to_nand(mtd); |
| 1751 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1752 |
|
| 1753 |
if (section > ecc->steps) |
| 1754 |
return -ERANGE; |
| 1755 |
|
| 1756 |
/* |
| 1757 |
* The first 2 bytes are used for BB markers, hence we |
| 1758 |
* only have 2 bytes available in the first user data |
| 1759 |
* section. |
| 1760 |
*/ |
| 1761 |
if (!section && ecc->engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) { |
| 1762 |
oobregion->offset = 2; |
| 1763 |
oobregion->length = 2; |
| 1764 |
|
| 1765 |
return 0; |
| 1766 |
} |
| 1767 |
|
| 1768 |
/* |
| 1769 |
* The controller does not provide access to OOB bytes |
| 1770 |
* past the end of the ECC data. |
| 1771 |
*/ |
| 1772 |
if (section == ecc->steps && ecc->engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) |
| 1773 |
return -ERANGE; |
| 1774 |
|
| 1775 |
oobregion->offset = section * (ecc->bytes + USER_DATA_SZ); |
| 1776 |
|
| 1777 |
if (section < ecc->steps) |
| 1778 |
oobregion->length = USER_DATA_SZ; |
| 1779 |
else |
| 1780 |
oobregion->length = mtd->oobsize - oobregion->offset; |
| 1781 |
|
| 1782 |
return 0; |
| 1783 |
} |
| 1784 |
|
| 1785 |
static const struct mtd_ooblayout_ops sunxi_nand_ooblayout_ops = { |
| 1786 |
.ecc = sunxi_nand_ooblayout_ecc, |
| 1787 |
.free = sunxi_nand_ooblayout_free, |
| 1788 |
}; |
| 1789 |
|
| 1790 |
static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand, |
| 1791 |
struct nand_ecc_ctrl *ecc, |
| 1792 |
struct device_node *np) |
| 1793 |
{ |
| 1794 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 1795 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 1796 |
const u8 *strengths = nfc->caps->ecc_strengths; |
| 1797 |
struct mtd_info *mtd = nand_to_mtd(nand); |
| 1798 |
struct nand_device *nanddev = mtd_to_nanddev(mtd); |
| 1799 |
int nsectors; |
| 1800 |
int i; |
| 1801 |
|
| 1802 |
if (nanddev->ecc.user_conf.flags & NAND_ECC_MAXIMIZE_STRENGTH) { |
| 1803 |
int bytes; |
| 1804 |
|
| 1805 |
ecc->size = 1024; |
| 1806 |
nsectors = mtd->writesize / ecc->size; |
| 1807 |
|
| 1808 |
/* Reserve 2 bytes for the BBM */ |
| 1809 |
bytes = (mtd->oobsize - 2) / nsectors; |
| 1810 |
|
| 1811 |
/* 4 non-ECC bytes are added before each ECC bytes section */ |
| 1812 |
bytes -= USER_DATA_SZ; |
| 1813 |
|
| 1814 |
/* and bytes has to be even. */ |
| 1815 |
if (bytes % 2) |
| 1816 |
bytes--; |
| 1817 |
|
| 1818 |
ecc->strength = bytes * 8 / fls(8 * ecc->size); |
| 1819 |
|
| 1820 |
for (i = 0; i < nfc->caps->nstrengths; i++) { |
| 1821 |
if (strengths[i] > ecc->strength) |
| 1822 |
break; |
| 1823 |
} |
| 1824 |
|
| 1825 |
if (!i) |
| 1826 |
ecc->strength = 0; |
| 1827 |
else |
| 1828 |
ecc->strength = strengths[i - 1]; |
| 1829 |
} |
| 1830 |
|
| 1831 |
if (ecc->size != 512 && ecc->size != 1024) |
| 1832 |
return -EINVAL; |
| 1833 |
|
| 1834 |
/* Prefer 1k ECC chunk over 512 ones */ |
| 1835 |
if (ecc->size == 512 && mtd->writesize > 512) { |
| 1836 |
ecc->size = 1024; |
| 1837 |
ecc->strength *= 2; |
| 1838 |
} |
| 1839 |
|
| 1840 |
/* Add ECC info retrieval from DT */ |
| 1841 |
for (i = 0; i < nfc->caps->nstrengths; i++) { |
| 1842 |
if (ecc->strength <= strengths[i]) { |
| 1843 |
/* |
| 1844 |
* Update ecc->strength value with the actual strength |
| 1845 |
* that will be used by the ECC engine. |
| 1846 |
*/ |
| 1847 |
ecc->strength = strengths[i]; |
| 1848 |
break; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
|
| 1852 |
if (i >= nfc->caps->nstrengths) { |
| 1853 |
dev_err(nfc->dev, "unsupported strength\n"); |
| 1854 |
return -ENOTSUPP; |
| 1855 |
} |
| 1856 |
|
| 1857 |
/* HW ECC always request ECC bytes for 1024 bytes blocks */ |
| 1858 |
ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8); |
| 1859 |
|
| 1860 |
/* HW ECC always work with even numbers of ECC bytes */ |
| 1861 |
ecc->bytes = ALIGN(ecc->bytes, 2); |
| 1862 |
|
| 1863 |
nsectors = mtd->writesize / ecc->size; |
| 1864 |
|
| 1865 |
if (mtd->oobsize < ((ecc->bytes + USER_DATA_SZ) * nsectors)) |
| 1866 |
return -EINVAL; |
| 1867 |
|
| 1868 |
ecc->read_oob = sunxi_nfc_hw_ecc_read_oob; |
| 1869 |
ecc->write_oob = sunxi_nfc_hw_ecc_write_oob; |
| 1870 |
mtd_set_ooblayout(mtd, &sunxi_nand_ooblayout_ops); |
| 1871 |
|
| 1872 |
if (nfc->dmac || nfc->caps->has_mdma) { |
| 1873 |
ecc->read_page = sunxi_nfc_hw_ecc_read_page_dma; |
| 1874 |
ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage_dma; |
| 1875 |
ecc->write_page = sunxi_nfc_hw_ecc_write_page_dma; |
| 1876 |
nand->options |= NAND_USES_DMA; |
| 1877 |
} else { |
| 1878 |
ecc->read_page = sunxi_nfc_hw_ecc_read_page; |
| 1879 |
ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage; |
| 1880 |
ecc->write_page = sunxi_nfc_hw_ecc_write_page; |
| 1881 |
} |
| 1882 |
|
| 1883 |
/* TODO: support DMA for raw accesses and subpage write */ |
| 1884 |
ecc->write_subpage = sunxi_nfc_hw_ecc_write_subpage; |
| 1885 |
ecc->read_oob_raw = nand_read_oob_std; |
| 1886 |
ecc->write_oob_raw = nand_write_oob_std; |
| 1887 |
|
| 1888 |
sunxi_nand->ecc.ecc_ctl = NFC_ECC_MODE(nfc, i) | NFC_ECC_EXCEPTION | |
| 1889 |
NFC_ECC_PIPELINE | NFC_ECC_EN; |
| 1890 |
|
| 1891 |
if (ecc->size == 512) { |
| 1892 |
if (nfc->caps->has_ecc_block_512) { |
| 1893 |
sunxi_nand->ecc.ecc_ctl |= NFC_ECC_BLOCK_512; |
| 1894 |
} else { |
| 1895 |
dev_err(nfc->dev, "512B ECC block not supported\n"); |
| 1896 |
return -EOPNOTSUPP; |
| 1897 |
} |
| 1898 |
} |
| 1899 |
|
| 1900 |
return 0; |
| 1901 |
} |
| 1902 |
|
| 1903 |
static int sunxi_nand_attach_chip(struct nand_chip *nand) |
| 1904 |
{ |
| 1905 |
const struct nand_ecc_props *requirements = |
| 1906 |
nanddev_get_ecc_requirements(&nand->base); |
| 1907 |
struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1908 |
struct device_node *np = nand_get_flash_node(nand); |
| 1909 |
int ret; |
| 1910 |
|
| 1911 |
if (nand->bbt_options & NAND_BBT_USE_FLASH) |
| 1912 |
nand->bbt_options |= NAND_BBT_NO_OOB; |
| 1913 |
|
| 1914 |
if (nand->options & NAND_NEED_SCRAMBLING) |
| 1915 |
nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 1916 |
|
| 1917 |
nand->options |= NAND_SUBPAGE_READ; |
| 1918 |
|
| 1919 |
if (!ecc->size) { |
| 1920 |
ecc->size = requirements->step_size; |
| 1921 |
ecc->strength = requirements->strength; |
| 1922 |
} |
| 1923 |
|
| 1924 |
if (!ecc->size || !ecc->strength) |
| 1925 |
return -EINVAL; |
| 1926 |
|
| 1927 |
switch (ecc->engine_type) { |
| 1928 |
case NAND_ECC_ENGINE_TYPE_ON_HOST: |
| 1929 |
ret = sunxi_nand_hw_ecc_ctrl_init(nand, ecc, np); |
| 1930 |
if (ret) |
| 1931 |
return ret; |
| 1932 |
break; |
| 1933 |
case NAND_ECC_ENGINE_TYPE_NONE: |
| 1934 |
case NAND_ECC_ENGINE_TYPE_SOFT: |
| 1935 |
break; |
| 1936 |
default: |
| 1937 |
return -EINVAL; |
| 1938 |
} |
| 1939 |
|
| 1940 |
return 0; |
| 1941 |
} |
| 1942 |
|
| 1943 |
static int sunxi_nfc_exec_subop(struct nand_chip *nand, |
| 1944 |
const struct nand_subop *subop) |
| 1945 |
{ |
| 1946 |
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 1947 |
u32 cmd = 0, extcmd = 0, cnt = 0, addrs[2] = { }; |
| 1948 |
unsigned int i, j, remaining, start; |
| 1949 |
void *inbuf = NULL; |
| 1950 |
int ret; |
| 1951 |
|
| 1952 |
for (i = 0; i < subop->ninstrs; i++) { |
| 1953 |
const struct nand_op_instr *instr = &subop->instrs[i]; |
| 1954 |
|
| 1955 |
switch (instr->type) { |
| 1956 |
case NAND_OP_CMD_INSTR: |
| 1957 |
if (cmd & NFC_SEND_CMD1) { |
| 1958 |
if (WARN_ON(cmd & NFC_SEND_CMD2)) |
| 1959 |
return -EINVAL; |
| 1960 |
|
| 1961 |
cmd |= NFC_SEND_CMD2; |
| 1962 |
extcmd |= instr->ctx.cmd.opcode; |
| 1963 |
} else { |
| 1964 |
cmd |= NFC_SEND_CMD1 | |
| 1965 |
NFC_CMD(instr->ctx.cmd.opcode); |
| 1966 |
} |
| 1967 |
break; |
| 1968 |
|
| 1969 |
case NAND_OP_ADDR_INSTR: |
| 1970 |
remaining = nand_subop_get_num_addr_cyc(subop, i); |
| 1971 |
start = nand_subop_get_addr_start_off(subop, i); |
| 1972 |
for (j = 0; j < 8 && j + start < remaining; j++) { |
| 1973 |
u32 addr = instr->ctx.addr.addrs[j + start]; |
| 1974 |
|
| 1975 |
addrs[j / 4] |= addr << (j % 4) * 8; |
| 1976 |
} |
| 1977 |
|
| 1978 |
if (j) |
| 1979 |
cmd |= NFC_SEND_ADR | NFC_ADR_NUM(j); |
| 1980 |
|
| 1981 |
break; |
| 1982 |
|
| 1983 |
case NAND_OP_DATA_IN_INSTR: |
| 1984 |
case NAND_OP_DATA_OUT_INSTR: |
| 1985 |
start = nand_subop_get_data_start_off(subop, i); |
| 1986 |
remaining = nand_subop_get_data_len(subop, i); |
| 1987 |
cnt = min_t(u32, remaining, nfc->caps->sram_size); |
| 1988 |
cmd |= NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD; |
| 1989 |
|
| 1990 |
if (instr->type == NAND_OP_DATA_OUT_INSTR) { |
| 1991 |
cmd |= NFC_ACCESS_DIR; |
| 1992 |
memcpy_toio(nfc->regs + NFC_RAM0_BASE, |
| 1993 |
instr->ctx.data.buf.out + start, |
| 1994 |
cnt); |
| 1995 |
} else { |
| 1996 |
inbuf = instr->ctx.data.buf.in + start; |
| 1997 |
} |
| 1998 |
|
| 1999 |
break; |
| 2000 |
|
| 2001 |
case NAND_OP_WAITRDY_INSTR: |
| 2002 |
cmd |= NFC_WAIT_FLAG; |
| 2003 |
break; |
| 2004 |
} |
| 2005 |
} |
| 2006 |
|
| 2007 |
ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 2008 |
if (ret) |
| 2009 |
return ret; |
| 2010 |
|
| 2011 |
if (cmd & NFC_SEND_ADR) { |
| 2012 |
writel(addrs[0], nfc->regs + NFC_REG_ADDR_LOW); |
| 2013 |
writel(addrs[1], nfc->regs + NFC_REG_ADDR_HIGH); |
| 2014 |
} |
| 2015 |
|
| 2016 |
if (cmd & NFC_SEND_CMD2) |
| 2017 |
writel(extcmd, |
| 2018 |
nfc->regs + |
| 2019 |
(cmd & NFC_ACCESS_DIR ? |
| 2020 |
NFC_REG_WCMD_SET : NFC_REG_RCMD_SET)); |
| 2021 |
|
| 2022 |
if (cmd & NFC_DATA_TRANS) |
| 2023 |
writel(cnt, nfc->regs + NFC_REG_CNT); |
| 2024 |
|
| 2025 |
writel(cmd, nfc->regs + NFC_REG_CMD); |
| 2026 |
|
| 2027 |
ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, |
| 2028 |
!(cmd & NFC_WAIT_FLAG) && cnt < 64, |
| 2029 |
0); |
| 2030 |
if (ret) |
| 2031 |
return ret; |
| 2032 |
|
| 2033 |
if (inbuf) |
| 2034 |
memcpy_fromio(inbuf, nfc->regs + NFC_RAM0_BASE, cnt); |
| 2035 |
|
| 2036 |
return 0; |
| 2037 |
} |
| 2038 |
|
| 2039 |
static int sunxi_nfc_soft_waitrdy(struct nand_chip *nand, |
| 2040 |
const struct nand_subop *subop) |
| 2041 |
{ |
| 2042 |
return nand_soft_waitrdy(nand, |
| 2043 |
subop->instrs[0].ctx.waitrdy.timeout_ms); |
| 2044 |
} |
| 2045 |
|
| 2046 |
static const struct nand_op_parser sunxi_nfc_op_parser = NAND_OP_PARSER( |
| 2047 |
NAND_OP_PARSER_PATTERN(sunxi_nfc_exec_subop, |
| 2048 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2049 |
NAND_OP_PARSER_PAT_ADDR_ELEM(true, 8), |
| 2050 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2051 |
NAND_OP_PARSER_PAT_WAITRDY_ELEM(true), |
| 2052 |
NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, 1024)), |
| 2053 |
NAND_OP_PARSER_PATTERN(sunxi_nfc_exec_subop, |
| 2054 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2055 |
NAND_OP_PARSER_PAT_ADDR_ELEM(true, 8), |
| 2056 |
NAND_OP_PARSER_PAT_DATA_OUT_ELEM(true, 1024), |
| 2057 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2058 |
NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)), |
| 2059 |
); |
| 2060 |
|
| 2061 |
static const struct nand_op_parser sunxi_nfc_norb_op_parser = NAND_OP_PARSER( |
| 2062 |
NAND_OP_PARSER_PATTERN(sunxi_nfc_exec_subop, |
| 2063 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2064 |
NAND_OP_PARSER_PAT_ADDR_ELEM(true, 8), |
| 2065 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2066 |
NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, 1024)), |
| 2067 |
NAND_OP_PARSER_PATTERN(sunxi_nfc_exec_subop, |
| 2068 |
NAND_OP_PARSER_PAT_CMD_ELEM(true), |
| 2069 |
NAND_OP_PARSER_PAT_ADDR_ELEM(true, 8), |
| 2070 |
NAND_OP_PARSER_PAT_DATA_OUT_ELEM(true, 1024), |
| 2071 |
NAND_OP_PARSER_PAT_CMD_ELEM(true)), |
| 2072 |
NAND_OP_PARSER_PATTERN(sunxi_nfc_soft_waitrdy, |
| 2073 |
NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), |
| 2074 |
); |
| 2075 |
|
| 2076 |
static int sunxi_nfc_exec_op(struct nand_chip *nand, |
| 2077 |
const struct nand_operation *op, bool check_only) |
| 2078 |
{ |
| 2079 |
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 2080 |
const struct nand_op_parser *parser; |
| 2081 |
|
| 2082 |
if (!check_only) |
| 2083 |
sunxi_nfc_select_chip(nand, op->cs); |
| 2084 |
|
| 2085 |
if (sunxi_nand->sels[op->cs].rb >= 0) |
| 2086 |
parser = &sunxi_nfc_op_parser; |
| 2087 |
else |
| 2088 |
parser = &sunxi_nfc_norb_op_parser; |
| 2089 |
|
| 2090 |
return nand_op_parser_exec_op(nand, parser, op, check_only); |
| 2091 |
} |
| 2092 |
|
| 2093 |
static const struct nand_controller_ops sunxi_nand_controller_ops = { |
| 2094 |
.attach_chip = sunxi_nand_attach_chip, |
| 2095 |
.setup_interface = sunxi_nfc_setup_interface, |
| 2096 |
.exec_op = sunxi_nfc_exec_op, |
| 2097 |
}; |
| 2098 |
|
| 2099 |
static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc) |
| 2100 |
{ |
| 2101 |
struct sunxi_nand_chip *sunxi_nand; |
| 2102 |
struct nand_chip *chip; |
| 2103 |
int ret; |
| 2104 |
|
| 2105 |
while (!list_empty(&nfc->chips)) { |
| 2106 |
sunxi_nand = list_first_entry(&nfc->chips, |
| 2107 |
struct sunxi_nand_chip, |
| 2108 |
node); |
| 2109 |
chip = &sunxi_nand->nand; |
| 2110 |
ret = mtd_device_unregister(nand_to_mtd(chip)); |
| 2111 |
WARN_ON(ret); |
| 2112 |
nand_cleanup(chip); |
| 2113 |
list_del(&sunxi_nand->node); |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc, |
| 2118 |
struct device_node *np) |
| 2119 |
{ |
| 2120 |
struct sunxi_nand_chip *sunxi_nand; |
| 2121 |
struct mtd_info *mtd; |
| 2122 |
struct nand_chip *nand; |
| 2123 |
int nsels; |
| 2124 |
int ret; |
| 2125 |
int i; |
| 2126 |
u32 tmp; |
| 2127 |
|
| 2128 |
if (!of_get_property(np, "reg", &nsels)) |
| 2129 |
return -EINVAL; |
| 2130 |
|
| 2131 |
nsels /= sizeof(u32); |
| 2132 |
if (!nsels) { |
| 2133 |
dev_err(dev, "invalid reg property size\n"); |
| 2134 |
return -EINVAL; |
| 2135 |
} |
| 2136 |
|
| 2137 |
sunxi_nand = devm_kzalloc(dev, struct_size(sunxi_nand, sels, nsels), |
| 2138 |
GFP_KERNEL); |
| 2139 |
if (!sunxi_nand) |
| 2140 |
return -ENOMEM; |
| 2141 |
|
| 2142 |
sunxi_nand->nsels = nsels; |
| 2143 |
|
| 2144 |
for (i = 0; i < nsels; i++) { |
| 2145 |
ret = of_property_read_u32_index(np, "reg", i, &tmp); |
| 2146 |
if (ret) { |
| 2147 |
dev_err(dev, "could not retrieve reg property: %d\n", |
| 2148 |
ret); |
| 2149 |
return ret; |
| 2150 |
} |
| 2151 |
|
| 2152 |
if (tmp > NFC_MAX_CS) { |
| 2153 |
dev_err(dev, |
| 2154 |
"invalid reg value: %u (max CS = 7)\n", |
| 2155 |
tmp); |
| 2156 |
return -EINVAL; |
| 2157 |
} |
| 2158 |
|
| 2159 |
if (test_and_set_bit(tmp, &nfc->assigned_cs)) { |
| 2160 |
dev_err(dev, "CS %d already assigned\n", tmp); |
| 2161 |
return -EINVAL; |
| 2162 |
} |
| 2163 |
|
| 2164 |
sunxi_nand->sels[i].cs = tmp; |
| 2165 |
|
| 2166 |
if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) && |
| 2167 |
tmp < 2) |
| 2168 |
sunxi_nand->sels[i].rb = tmp; |
| 2169 |
else |
| 2170 |
sunxi_nand->sels[i].rb = -1; |
| 2171 |
} |
| 2172 |
|
| 2173 |
nand = &sunxi_nand->nand; |
| 2174 |
/* Default tR value specified in the ONFI spec (chapter 4.15.1) */ |
| 2175 |
nand->controller = &nfc->controller; |
| 2176 |
nand->controller->ops = &sunxi_nand_controller_ops; |
| 2177 |
|
| 2178 |
/* |
| 2179 |
* Set the ECC mode to the default value in case nothing is specified |
| 2180 |
* in the DT. |
| 2181 |
*/ |
| 2182 |
nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; |
| 2183 |
nand_set_flash_node(nand, np); |
| 2184 |
|
| 2185 |
mtd = nand_to_mtd(nand); |
| 2186 |
mtd->dev.parent = dev; |
| 2187 |
|
| 2188 |
ret = nand_scan(nand, nsels); |
| 2189 |
if (ret) |
| 2190 |
return ret; |
| 2191 |
|
| 2192 |
ret = mtd_device_register(mtd, NULL, 0); |
| 2193 |
if (ret) { |
| 2194 |
dev_err(dev, "failed to register mtd device: %d\n", ret); |
| 2195 |
nand_cleanup(nand); |
| 2196 |
return ret; |
| 2197 |
} |
| 2198 |
|
| 2199 |
list_add_tail(&sunxi_nand->node, &nfc->chips); |
| 2200 |
|
| 2201 |
return 0; |
| 2202 |
} |
| 2203 |
|
| 2204 |
static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc) |
| 2205 |
{ |
| 2206 |
struct device_node *np = dev->of_node; |
| 2207 |
int ret; |
| 2208 |
|
| 2209 |
for_each_child_of_node_scoped(np, nand_np) { |
| 2210 |
ret = sunxi_nand_chip_init(dev, nfc, nand_np); |
| 2211 |
if (ret) { |
| 2212 |
sunxi_nand_chips_cleanup(nfc); |
| 2213 |
return ret; |
| 2214 |
} |
| 2215 |
} |
| 2216 |
|
| 2217 |
return 0; |
| 2218 |
} |
| 2219 |
|
| 2220 |
static int sunxi_nfc_dma_init(struct sunxi_nfc *nfc, struct resource *r) |
| 2221 |
{ |
| 2222 |
int ret; |
| 2223 |
|
| 2224 |
if (nfc->caps->has_mdma) |
| 2225 |
return 0; |
| 2226 |
|
| 2227 |
nfc->dmac = dma_request_chan(nfc->dev, "rxtx"); |
| 2228 |
if (IS_ERR(nfc->dmac)) { |
| 2229 |
ret = PTR_ERR(nfc->dmac); |
| 2230 |
if (ret == -EPROBE_DEFER) |
| 2231 |
return ret; |
| 2232 |
|
| 2233 |
/* Ignore errors to fall back to PIO mode */ |
| 2234 |
dev_warn(nfc->dev, "failed to request rxtx DMA channel: %d\n", ret); |
| 2235 |
nfc->dmac = NULL; |
| 2236 |
} else { |
| 2237 |
struct dma_slave_config dmac_cfg = { }; |
| 2238 |
|
| 2239 |
dmac_cfg.src_addr = r->start + nfc->caps->reg_io_data; |
| 2240 |
dmac_cfg.dst_addr = dmac_cfg.src_addr; |
| 2241 |
dmac_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 2242 |
dmac_cfg.dst_addr_width = dmac_cfg.src_addr_width; |
| 2243 |
dmac_cfg.src_maxburst = nfc->caps->dma_maxburst; |
| 2244 |
dmac_cfg.dst_maxburst = nfc->caps->dma_maxburst; |
| 2245 |
dmaengine_slave_config(nfc->dmac, &dmac_cfg); |
| 2246 |
} |
| 2247 |
return 0; |
| 2248 |
} |
| 2249 |
|
| 2250 |
static int sunxi_nfc_probe(struct platform_device *pdev) |
| 2251 |
{ |
| 2252 |
struct device *dev = &pdev->dev; |
| 2253 |
struct resource *r; |
| 2254 |
struct sunxi_nfc *nfc; |
| 2255 |
int irq; |
| 2256 |
int ret; |
| 2257 |
|
| 2258 |
nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL); |
| 2259 |
if (!nfc) |
| 2260 |
return -ENOMEM; |
| 2261 |
|
| 2262 |
nfc->dev = dev; |
| 2263 |
nand_controller_init(&nfc->controller); |
| 2264 |
INIT_LIST_HEAD(&nfc->chips); |
| 2265 |
|
| 2266 |
nfc->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &r); |
| 2267 |
if (IS_ERR(nfc->regs)) |
| 2268 |
return PTR_ERR(nfc->regs); |
| 2269 |
|
| 2270 |
irq = platform_get_irq(pdev, 0); |
| 2271 |
if (irq < 0) |
| 2272 |
return irq; |
| 2273 |
|
| 2274 |
nfc->caps = of_device_get_match_data(dev); |
| 2275 |
if (!nfc->caps) |
| 2276 |
return -EINVAL; |
| 2277 |
|
| 2278 |
nfc->ahb_clk = devm_clk_get_enabled(dev, "ahb"); |
| 2279 |
if (IS_ERR(nfc->ahb_clk)) { |
| 2280 |
dev_err(dev, "failed to retrieve ahb clk\n"); |
| 2281 |
return PTR_ERR(nfc->ahb_clk); |
| 2282 |
} |
| 2283 |
|
| 2284 |
nfc->mod_clk = devm_clk_get_enabled(dev, "mod"); |
| 2285 |
if (IS_ERR(nfc->mod_clk)) { |
| 2286 |
dev_err(dev, "failed to retrieve mod clk\n"); |
| 2287 |
return PTR_ERR(nfc->mod_clk); |
| 2288 |
} |
| 2289 |
|
| 2290 |
if (nfc->caps->has_ecc_clk) { |
| 2291 |
nfc->ecc_clk = devm_clk_get_enabled(dev, "ecc"); |
| 2292 |
if (IS_ERR(nfc->ecc_clk)) { |
| 2293 |
dev_err(dev, "failed to retrieve ecc clk\n"); |
| 2294 |
return PTR_ERR(nfc->ecc_clk); |
| 2295 |
} |
| 2296 |
} |
| 2297 |
|
| 2298 |
if (nfc->caps->has_mbus_clk) { |
| 2299 |
nfc->mbus_clk = devm_clk_get_enabled(dev, "mbus"); |
| 2300 |
if (IS_ERR(nfc->mbus_clk)) { |
| 2301 |
dev_err(dev, "failed to retrieve mbus clk\n"); |
| 2302 |
return PTR_ERR(nfc->mbus_clk); |
| 2303 |
} |
| 2304 |
} |
| 2305 |
|
| 2306 |
nfc->reset = devm_reset_control_get_optional_exclusive(dev, "ahb"); |
| 2307 |
if (IS_ERR(nfc->reset)) |
| 2308 |
return PTR_ERR(nfc->reset); |
| 2309 |
|
| 2310 |
ret = reset_control_deassert(nfc->reset); |
| 2311 |
if (ret) { |
| 2312 |
dev_err(dev, "reset err %d\n", ret); |
| 2313 |
return ret; |
| 2314 |
} |
| 2315 |
|
| 2316 |
ret = sunxi_nfc_rst(nfc); |
| 2317 |
if (ret) |
| 2318 |
goto out_ahb_reset_reassert; |
| 2319 |
|
| 2320 |
writel(0, nfc->regs + NFC_REG_INT); |
| 2321 |
ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt, |
| 2322 |
0, "sunxi-nand", nfc); |
| 2323 |
if (ret) |
| 2324 |
goto out_ahb_reset_reassert; |
| 2325 |
|
| 2326 |
ret = sunxi_nfc_dma_init(nfc, r); |
| 2327 |
|
| 2328 |
if (ret) |
| 2329 |
goto out_ahb_reset_reassert; |
| 2330 |
|
| 2331 |
platform_set_drvdata(pdev, nfc); |
| 2332 |
|
| 2333 |
ret = sunxi_nand_chips_init(dev, nfc); |
| 2334 |
if (ret) { |
| 2335 |
dev_err(dev, "failed to init nand chips\n"); |
| 2336 |
goto out_release_dmac; |
| 2337 |
} |
| 2338 |
|
| 2339 |
return 0; |
| 2340 |
|
| 2341 |
out_release_dmac: |
| 2342 |
if (nfc->dmac) |
| 2343 |
dma_release_channel(nfc->dmac); |
| 2344 |
out_ahb_reset_reassert: |
| 2345 |
reset_control_assert(nfc->reset); |
| 2346 |
|
| 2347 |
return ret; |
| 2348 |
} |
| 2349 |
|
| 2350 |
static void sunxi_nfc_remove(struct platform_device *pdev) |
| 2351 |
{ |
| 2352 |
struct sunxi_nfc *nfc = platform_get_drvdata(pdev); |
| 2353 |
|
| 2354 |
sunxi_nand_chips_cleanup(nfc); |
| 2355 |
|
| 2356 |
reset_control_assert(nfc->reset); |
| 2357 |
|
| 2358 |
if (nfc->dmac) |
| 2359 |
dma_release_channel(nfc->dmac); |
| 2360 |
} |
| 2361 |
|
| 2362 |
static const u8 sunxi_ecc_strengths_a10[] = { |
| 2363 |
16, 24, 28, 32, 40, 48, 56, 60, 64 |
| 2364 |
}; |
| 2365 |
|
| 2366 |
static const u8 sunxi_ecc_strengths_h6[] = { |
| 2367 |
16, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80 |
| 2368 |
}; |
| 2369 |
|
| 2370 |
static const u8 sunxi_user_data_len_h6[] = { |
| 2371 |
0, 4, 8, 12, 16, 20, 24, 28, 32 |
| 2372 |
}; |
| 2373 |
|
| 2374 |
static const struct sunxi_nfc_caps sunxi_nfc_a10_caps = { |
| 2375 |
.has_ecc_block_512 = true, |
| 2376 |
.reg_io_data = NFC_REG_A10_IO_DATA, |
| 2377 |
.reg_ecc_err_cnt = NFC_REG_A10_ECC_ERR_CNT, |
| 2378 |
.reg_user_data = NFC_REG_A10_USER_DATA, |
| 2379 |
.reg_spare_area = NFC_REG_A10_SPARE_AREA, |
| 2380 |
.reg_pat_id = NFC_REG_A10_PAT_ID, |
| 2381 |
.reg_pat_found = NFC_REG_ECC_ST, |
| 2382 |
.random_en_mask = BIT(9), |
| 2383 |
.random_dir_mask = BIT(10), |
| 2384 |
.ecc_mode_mask = GENMASK(15, 12), |
| 2385 |
.ecc_err_mask = GENMASK(15, 0), |
| 2386 |
.pat_found_mask = GENMASK(31, 16), |
| 2387 |
.dma_maxburst = 4, |
| 2388 |
.ecc_strengths = sunxi_ecc_strengths_a10, |
| 2389 |
.nstrengths = ARRAY_SIZE(sunxi_ecc_strengths_a10), |
| 2390 |
.max_ecc_steps = 16, |
| 2391 |
.sram_size = 1024, |
| 2392 |
}; |
| 2393 |
|
| 2394 |
static const struct sunxi_nfc_caps sunxi_nfc_a23_caps = { |
| 2395 |
.has_mdma = true, |
| 2396 |
.has_ecc_block_512 = true, |
| 2397 |
.reg_io_data = NFC_REG_A23_IO_DATA, |
| 2398 |
.reg_ecc_err_cnt = NFC_REG_A10_ECC_ERR_CNT, |
| 2399 |
.reg_user_data = NFC_REG_A10_USER_DATA, |
| 2400 |
.reg_spare_area = NFC_REG_A10_SPARE_AREA, |
| 2401 |
.reg_pat_id = NFC_REG_A10_PAT_ID, |
| 2402 |
.reg_pat_found = NFC_REG_ECC_ST, |
| 2403 |
.random_en_mask = BIT(9), |
| 2404 |
.random_dir_mask = BIT(10), |
| 2405 |
.ecc_mode_mask = GENMASK(15, 12), |
| 2406 |
.ecc_err_mask = GENMASK(15, 0), |
| 2407 |
.pat_found_mask = GENMASK(31, 16), |
| 2408 |
.dma_maxburst = 8, |
| 2409 |
.ecc_strengths = sunxi_ecc_strengths_a10, |
| 2410 |
.nstrengths = ARRAY_SIZE(sunxi_ecc_strengths_a10), |
| 2411 |
.max_ecc_steps = 16, |
| 2412 |
.sram_size = 1024, |
| 2413 |
}; |
| 2414 |
|
| 2415 |
static const struct sunxi_nfc_caps sunxi_nfc_h616_caps = { |
| 2416 |
.has_ecc_clk = true, |
| 2417 |
.has_mbus_clk = true, |
| 2418 |
.reg_io_data = NFC_REG_A23_IO_DATA, |
| 2419 |
.reg_ecc_err_cnt = NFC_REG_H6_ECC_ERR_CNT, |
| 2420 |
.reg_user_data = NFC_REG_H6_USER_DATA, |
| 2421 |
.reg_user_data_len = NFC_REG_H6_USER_DATA_LEN, |
| 2422 |
.reg_spare_area = NFC_REG_H6_SPARE_AREA, |
| 2423 |
.reg_pat_id = NFC_REG_H6_PAT_ID, |
| 2424 |
.reg_pat_found = NFC_REG_H6_PAT_FOUND, |
| 2425 |
.random_en_mask = BIT(5), |
| 2426 |
.random_dir_mask = BIT(6), |
| 2427 |
.ecc_mode_mask = GENMASK(15, 8), |
| 2428 |
.ecc_err_mask = GENMASK(31, 0), |
| 2429 |
.pat_found_mask = GENMASK(31, 0), |
| 2430 |
.dma_maxburst = 8, |
| 2431 |
.ecc_strengths = sunxi_ecc_strengths_h6, |
| 2432 |
.nstrengths = ARRAY_SIZE(sunxi_ecc_strengths_h6), |
| 2433 |
.user_data_len_tab = sunxi_user_data_len_h6, |
| 2434 |
.nuser_data_tab = ARRAY_SIZE(sunxi_user_data_len_h6), |
| 2435 |
.max_ecc_steps = 32, |
| 2436 |
.sram_size = 8192, |
| 2437 |
}; |
| 2438 |
|
| 2439 |
static const struct of_device_id sunxi_nfc_ids[] = { |
| 2440 |
{ |
| 2441 |
.compatible = "allwinner,sun4i-a10-nand", |
| 2442 |
.data = &sunxi_nfc_a10_caps, |
| 2443 |
}, |
| 2444 |
{ |
| 2445 |
.compatible = "allwinner,sun8i-a23-nand-controller", |
| 2446 |
.data = &sunxi_nfc_a23_caps, |
| 2447 |
}, |
| 2448 |
{ |
| 2449 |
.compatible = "allwinner,sun50i-h616-nand-controller", |
| 2450 |
.data = &sunxi_nfc_h616_caps, |
| 2451 |
}, |
| 2452 |
{ /* sentinel */ } |
| 2453 |
}; |
| 2454 |
MODULE_DEVICE_TABLE(of, sunxi_nfc_ids); |
| 2455 |
|
| 2456 |
static struct platform_driver sunxi_nfc_driver = { |
| 2457 |
.driver = { |
| 2458 |
.name = "sunxi_nand", |
| 2459 |
.of_match_table = sunxi_nfc_ids, |
| 2460 |
}, |
| 2461 |
.probe = sunxi_nfc_probe, |
| 2462 |
.remove = sunxi_nfc_remove, |
| 2463 |
}; |
| 2464 |
module_platform_driver(sunxi_nfc_driver); |
| 2465 |
|
| 2466 |
MODULE_LICENSE("GPL"); |
| 2467 |
MODULE_AUTHOR("Boris BREZILLON"); |
| 2468 |
MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver"); |