14fd62f15SChuanhong Guo // SPDX-License-Identifier: GPL-2.0 OR MIT 24fd62f15SChuanhong Guo /* 34fd62f15SChuanhong Guo * MTK ECC controller driver. 44fd62f15SChuanhong Guo * Copyright (C) 2016 MediaTek Inc. 54fd62f15SChuanhong Guo * Authors: Xiaolei Li <xiaolei.li@mediatek.com> 64fd62f15SChuanhong Guo * Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org> 74fd62f15SChuanhong Guo */ 84fd62f15SChuanhong Guo 94fd62f15SChuanhong Guo #include <linux/platform_device.h> 104fd62f15SChuanhong Guo #include <linux/dma-mapping.h> 114fd62f15SChuanhong Guo #include <linux/interrupt.h> 124fd62f15SChuanhong Guo #include <linux/clk.h> 134fd62f15SChuanhong Guo #include <linux/module.h> 144fd62f15SChuanhong Guo #include <linux/iopoll.h> 154fd62f15SChuanhong Guo #include <linux/of.h> 164fd62f15SChuanhong Guo #include <linux/of_platform.h> 174fd62f15SChuanhong Guo #include <linux/mutex.h> 184fd62f15SChuanhong Guo #include <linux/mtd/nand-ecc-mtk.h> 194fd62f15SChuanhong Guo 204fd62f15SChuanhong Guo #define ECC_IDLE_MASK BIT(0) 214fd62f15SChuanhong Guo #define ECC_IRQ_EN BIT(0) 224fd62f15SChuanhong Guo #define ECC_PG_IRQ_SEL BIT(1) 234fd62f15SChuanhong Guo #define ECC_OP_ENABLE (1) 244fd62f15SChuanhong Guo #define ECC_OP_DISABLE (0) 254fd62f15SChuanhong Guo 264fd62f15SChuanhong Guo #define ECC_ENCCON (0x00) 274fd62f15SChuanhong Guo #define ECC_ENCCNFG (0x04) 284fd62f15SChuanhong Guo #define ECC_MS_SHIFT (16) 294fd62f15SChuanhong Guo #define ECC_ENCDIADDR (0x08) 304fd62f15SChuanhong Guo #define ECC_ENCIDLE (0x0C) 314fd62f15SChuanhong Guo #define ECC_DECCON (0x100) 324fd62f15SChuanhong Guo #define ECC_DECCNFG (0x104) 334fd62f15SChuanhong Guo #define DEC_EMPTY_EN BIT(31) 344fd62f15SChuanhong Guo #define DEC_CNFG_CORRECT (0x3 << 12) 354fd62f15SChuanhong Guo #define ECC_DECIDLE (0x10C) 364fd62f15SChuanhong Guo #define ECC_DECENUM0 (0x114) 374fd62f15SChuanhong Guo 384fd62f15SChuanhong Guo #define ECC_TIMEOUT (500000) 394fd62f15SChuanhong Guo 404fd62f15SChuanhong Guo #define ECC_IDLE_REG(op) ((op) == ECC_ENCODE ? ECC_ENCIDLE : ECC_DECIDLE) 414fd62f15SChuanhong Guo #define ECC_CTL_REG(op) ((op) == ECC_ENCODE ? ECC_ENCCON : ECC_DECCON) 424fd62f15SChuanhong Guo 434fd62f15SChuanhong Guo struct mtk_ecc_caps { 444fd62f15SChuanhong Guo u32 err_mask; 45d3353719SLinus Torvalds u32 err_shift; 464fd62f15SChuanhong Guo const u8 *ecc_strength; 474fd62f15SChuanhong Guo const u32 *ecc_regs; 484fd62f15SChuanhong Guo u8 num_ecc_strength; 494fd62f15SChuanhong Guo u8 ecc_mode_shift; 504fd62f15SChuanhong Guo u32 parity_bits; 514fd62f15SChuanhong Guo int pg_irq_sel; 524fd62f15SChuanhong Guo }; 534fd62f15SChuanhong Guo 544fd62f15SChuanhong Guo struct mtk_ecc { 554fd62f15SChuanhong Guo struct device *dev; 564fd62f15SChuanhong Guo const struct mtk_ecc_caps *caps; 574fd62f15SChuanhong Guo void __iomem *regs; 584fd62f15SChuanhong Guo struct clk *clk; 594fd62f15SChuanhong Guo 604fd62f15SChuanhong Guo struct completion done; 614fd62f15SChuanhong Guo struct mutex lock; 624fd62f15SChuanhong Guo u32 sectors; 634fd62f15SChuanhong Guo 644fd62f15SChuanhong Guo u8 *eccdata; 654fd62f15SChuanhong Guo }; 664fd62f15SChuanhong Guo 674fd62f15SChuanhong Guo /* ecc strength that each IP supports */ 684fd62f15SChuanhong Guo static const u8 ecc_strength_mt2701[] = { 694fd62f15SChuanhong Guo 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36, 704fd62f15SChuanhong Guo 40, 44, 48, 52, 56, 60 714fd62f15SChuanhong Guo }; 724fd62f15SChuanhong Guo 734fd62f15SChuanhong Guo static const u8 ecc_strength_mt2712[] = { 744fd62f15SChuanhong Guo 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36, 754fd62f15SChuanhong Guo 40, 44, 48, 52, 56, 60, 68, 72, 80 764fd62f15SChuanhong Guo }; 774fd62f15SChuanhong Guo 784fd62f15SChuanhong Guo static const u8 ecc_strength_mt7622[] = { 79d3353719SLinus Torvalds 4, 6, 8, 10, 12 804fd62f15SChuanhong Guo }; 814fd62f15SChuanhong Guo 824fd62f15SChuanhong Guo enum mtk_ecc_regs { 834fd62f15SChuanhong Guo ECC_ENCPAR00, 844fd62f15SChuanhong Guo ECC_ENCIRQ_EN, 854fd62f15SChuanhong Guo ECC_ENCIRQ_STA, 864fd62f15SChuanhong Guo ECC_DECDONE, 874fd62f15SChuanhong Guo ECC_DECIRQ_EN, 884fd62f15SChuanhong Guo ECC_DECIRQ_STA, 894fd62f15SChuanhong Guo }; 904fd62f15SChuanhong Guo 914fd62f15SChuanhong Guo static int mt2701_ecc_regs[] = { 924fd62f15SChuanhong Guo [ECC_ENCPAR00] = 0x10, 934fd62f15SChuanhong Guo [ECC_ENCIRQ_EN] = 0x80, 944fd62f15SChuanhong Guo [ECC_ENCIRQ_STA] = 0x84, 954fd62f15SChuanhong Guo [ECC_DECDONE] = 0x124, 964fd62f15SChuanhong Guo [ECC_DECIRQ_EN] = 0x200, 974fd62f15SChuanhong Guo [ECC_DECIRQ_STA] = 0x204, 984fd62f15SChuanhong Guo }; 994fd62f15SChuanhong Guo 1004fd62f15SChuanhong Guo static int mt2712_ecc_regs[] = { 1014fd62f15SChuanhong Guo [ECC_ENCPAR00] = 0x300, 1024fd62f15SChuanhong Guo [ECC_ENCIRQ_EN] = 0x80, 1034fd62f15SChuanhong Guo [ECC_ENCIRQ_STA] = 0x84, 1044fd62f15SChuanhong Guo [ECC_DECDONE] = 0x124, 1054fd62f15SChuanhong Guo [ECC_DECIRQ_EN] = 0x200, 1064fd62f15SChuanhong Guo [ECC_DECIRQ_STA] = 0x204, 1074fd62f15SChuanhong Guo }; 1084fd62f15SChuanhong Guo 1094fd62f15SChuanhong Guo static int mt7622_ecc_regs[] = { 1104fd62f15SChuanhong Guo [ECC_ENCPAR00] = 0x10, 1114fd62f15SChuanhong Guo [ECC_ENCIRQ_EN] = 0x30, 1124fd62f15SChuanhong Guo [ECC_ENCIRQ_STA] = 0x34, 1134fd62f15SChuanhong Guo [ECC_DECDONE] = 0x11c, 1144fd62f15SChuanhong Guo [ECC_DECIRQ_EN] = 0x140, 1154fd62f15SChuanhong Guo [ECC_DECIRQ_STA] = 0x144, 1164fd62f15SChuanhong Guo }; 1174fd62f15SChuanhong Guo 1184fd62f15SChuanhong Guo static inline void mtk_ecc_wait_idle(struct mtk_ecc *ecc, 1194fd62f15SChuanhong Guo enum mtk_ecc_operation op) 1204fd62f15SChuanhong Guo { 1214fd62f15SChuanhong Guo struct device *dev = ecc->dev; 1224fd62f15SChuanhong Guo u32 val; 1234fd62f15SChuanhong Guo int ret; 1244fd62f15SChuanhong Guo 1254fd62f15SChuanhong Guo ret = readl_poll_timeout_atomic(ecc->regs + ECC_IDLE_REG(op), val, 1264fd62f15SChuanhong Guo val & ECC_IDLE_MASK, 1274fd62f15SChuanhong Guo 10, ECC_TIMEOUT); 1284fd62f15SChuanhong Guo if (ret) 1294fd62f15SChuanhong Guo dev_warn(dev, "%s NOT idle\n", 1304fd62f15SChuanhong Guo op == ECC_ENCODE ? "encoder" : "decoder"); 1314fd62f15SChuanhong Guo } 1324fd62f15SChuanhong Guo 1334fd62f15SChuanhong Guo static irqreturn_t mtk_ecc_irq(int irq, void *id) 1344fd62f15SChuanhong Guo { 1354fd62f15SChuanhong Guo struct mtk_ecc *ecc = id; 1364fd62f15SChuanhong Guo u32 dec, enc; 1374fd62f15SChuanhong Guo 1384fd62f15SChuanhong Guo dec = readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_STA]) 1394fd62f15SChuanhong Guo & ECC_IRQ_EN; 1404fd62f15SChuanhong Guo if (dec) { 1414fd62f15SChuanhong Guo dec = readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECDONE]); 1424fd62f15SChuanhong Guo if (dec & ecc->sectors) { 1434fd62f15SChuanhong Guo /* 1444fd62f15SChuanhong Guo * Clear decode IRQ status once again to ensure that 1454fd62f15SChuanhong Guo * there will be no extra IRQ. 1464fd62f15SChuanhong Guo */ 1474fd62f15SChuanhong Guo readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_STA]); 1484fd62f15SChuanhong Guo ecc->sectors = 0; 1494fd62f15SChuanhong Guo complete(&ecc->done); 1504fd62f15SChuanhong Guo } else { 1514fd62f15SChuanhong Guo return IRQ_HANDLED; 1524fd62f15SChuanhong Guo } 1534fd62f15SChuanhong Guo } else { 1544fd62f15SChuanhong Guo enc = readl(ecc->regs + ecc->caps->ecc_regs[ECC_ENCIRQ_STA]) 1554fd62f15SChuanhong Guo & ECC_IRQ_EN; 1564fd62f15SChuanhong Guo if (enc) 1574fd62f15SChuanhong Guo complete(&ecc->done); 1584fd62f15SChuanhong Guo else 1594fd62f15SChuanhong Guo return IRQ_NONE; 1604fd62f15SChuanhong Guo } 1614fd62f15SChuanhong Guo 1624fd62f15SChuanhong Guo return IRQ_HANDLED; 1634fd62f15SChuanhong Guo } 1644fd62f15SChuanhong Guo 1654fd62f15SChuanhong Guo static int mtk_ecc_config(struct mtk_ecc *ecc, struct mtk_ecc_config *config) 1664fd62f15SChuanhong Guo { 1674fd62f15SChuanhong Guo u32 ecc_bit, dec_sz, enc_sz; 1684fd62f15SChuanhong Guo u32 reg, i; 1694fd62f15SChuanhong Guo 1704fd62f15SChuanhong Guo for (i = 0; i < ecc->caps->num_ecc_strength; i++) { 1714fd62f15SChuanhong Guo if (ecc->caps->ecc_strength[i] == config->strength) 1724fd62f15SChuanhong Guo break; 1734fd62f15SChuanhong Guo } 1744fd62f15SChuanhong Guo 1754fd62f15SChuanhong Guo if (i == ecc->caps->num_ecc_strength) { 1764fd62f15SChuanhong Guo dev_err(ecc->dev, "invalid ecc strength %d\n", 1774fd62f15SChuanhong Guo config->strength); 1784fd62f15SChuanhong Guo return -EINVAL; 1794fd62f15SChuanhong Guo } 1804fd62f15SChuanhong Guo 1814fd62f15SChuanhong Guo ecc_bit = i; 1824fd62f15SChuanhong Guo 1834fd62f15SChuanhong Guo if (config->op == ECC_ENCODE) { 1844fd62f15SChuanhong Guo /* configure ECC encoder (in bits) */ 1854fd62f15SChuanhong Guo enc_sz = config->len << 3; 1864fd62f15SChuanhong Guo 1874fd62f15SChuanhong Guo reg = ecc_bit | (config->mode << ecc->caps->ecc_mode_shift); 1884fd62f15SChuanhong Guo reg |= (enc_sz << ECC_MS_SHIFT); 1894fd62f15SChuanhong Guo writel(reg, ecc->regs + ECC_ENCCNFG); 1904fd62f15SChuanhong Guo 1914fd62f15SChuanhong Guo if (config->mode != ECC_NFI_MODE) 1924fd62f15SChuanhong Guo writel(lower_32_bits(config->addr), 1934fd62f15SChuanhong Guo ecc->regs + ECC_ENCDIADDR); 1944fd62f15SChuanhong Guo 1954fd62f15SChuanhong Guo } else { 1964fd62f15SChuanhong Guo /* configure ECC decoder (in bits) */ 1974fd62f15SChuanhong Guo dec_sz = (config->len << 3) + 1984fd62f15SChuanhong Guo config->strength * ecc->caps->parity_bits; 1994fd62f15SChuanhong Guo 2004fd62f15SChuanhong Guo reg = ecc_bit | (config->mode << ecc->caps->ecc_mode_shift); 2014fd62f15SChuanhong Guo reg |= (dec_sz << ECC_MS_SHIFT) | DEC_CNFG_CORRECT; 2024fd62f15SChuanhong Guo reg |= DEC_EMPTY_EN; 2034fd62f15SChuanhong Guo writel(reg, ecc->regs + ECC_DECCNFG); 2044fd62f15SChuanhong Guo 2054fd62f15SChuanhong Guo if (config->sectors) 2064fd62f15SChuanhong Guo ecc->sectors = 1 << (config->sectors - 1); 2074fd62f15SChuanhong Guo } 2084fd62f15SChuanhong Guo 2094fd62f15SChuanhong Guo return 0; 2104fd62f15SChuanhong Guo } 2114fd62f15SChuanhong Guo 2124fd62f15SChuanhong Guo void mtk_ecc_get_stats(struct mtk_ecc *ecc, struct mtk_ecc_stats *stats, 2134fd62f15SChuanhong Guo int sectors) 2144fd62f15SChuanhong Guo { 2154fd62f15SChuanhong Guo u32 offset, i, err; 2164fd62f15SChuanhong Guo u32 bitflips = 0; 2174fd62f15SChuanhong Guo 2184fd62f15SChuanhong Guo stats->corrected = 0; 2194fd62f15SChuanhong Guo stats->failed = 0; 2204fd62f15SChuanhong Guo 2214fd62f15SChuanhong Guo for (i = 0; i < sectors; i++) { 2224fd62f15SChuanhong Guo offset = (i >> 2) << 2; 2234fd62f15SChuanhong Guo err = readl(ecc->regs + ECC_DECENUM0 + offset); 224d3353719SLinus Torvalds err = err >> ((i % 4) * ecc->caps->err_shift); 2254fd62f15SChuanhong Guo err &= ecc->caps->err_mask; 2264fd62f15SChuanhong Guo if (err == ecc->caps->err_mask) { 2274fd62f15SChuanhong Guo /* uncorrectable errors */ 2284fd62f15SChuanhong Guo stats->failed++; 2294fd62f15SChuanhong Guo continue; 2304fd62f15SChuanhong Guo } 2314fd62f15SChuanhong Guo 2324fd62f15SChuanhong Guo stats->corrected += err; 2334fd62f15SChuanhong Guo bitflips = max_t(u32, bitflips, err); 2344fd62f15SChuanhong Guo } 2354fd62f15SChuanhong Guo 2364fd62f15SChuanhong Guo stats->bitflips = bitflips; 2374fd62f15SChuanhong Guo } 2384fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_get_stats); 2394fd62f15SChuanhong Guo 2404fd62f15SChuanhong Guo void mtk_ecc_release(struct mtk_ecc *ecc) 2414fd62f15SChuanhong Guo { 2424fd62f15SChuanhong Guo clk_disable_unprepare(ecc->clk); 2434fd62f15SChuanhong Guo put_device(ecc->dev); 2444fd62f15SChuanhong Guo } 2454fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_release); 2464fd62f15SChuanhong Guo 2474fd62f15SChuanhong Guo static void mtk_ecc_hw_init(struct mtk_ecc *ecc) 2484fd62f15SChuanhong Guo { 2494fd62f15SChuanhong Guo mtk_ecc_wait_idle(ecc, ECC_ENCODE); 2504fd62f15SChuanhong Guo writew(ECC_OP_DISABLE, ecc->regs + ECC_ENCCON); 2514fd62f15SChuanhong Guo 2524fd62f15SChuanhong Guo mtk_ecc_wait_idle(ecc, ECC_DECODE); 2534fd62f15SChuanhong Guo writel(ECC_OP_DISABLE, ecc->regs + ECC_DECCON); 2544fd62f15SChuanhong Guo } 2554fd62f15SChuanhong Guo 2564fd62f15SChuanhong Guo static struct mtk_ecc *mtk_ecc_get(struct device_node *np) 2574fd62f15SChuanhong Guo { 2584fd62f15SChuanhong Guo struct platform_device *pdev; 2594fd62f15SChuanhong Guo struct mtk_ecc *ecc; 2604fd62f15SChuanhong Guo 2614fd62f15SChuanhong Guo pdev = of_find_device_by_node(np); 2624fd62f15SChuanhong Guo if (!pdev) 2634fd62f15SChuanhong Guo return ERR_PTR(-EPROBE_DEFER); 2644fd62f15SChuanhong Guo 2654fd62f15SChuanhong Guo ecc = platform_get_drvdata(pdev); 2664fd62f15SChuanhong Guo if (!ecc) { 2674fd62f15SChuanhong Guo put_device(&pdev->dev); 2684fd62f15SChuanhong Guo return ERR_PTR(-EPROBE_DEFER); 2694fd62f15SChuanhong Guo } 2704fd62f15SChuanhong Guo 2714fd62f15SChuanhong Guo clk_prepare_enable(ecc->clk); 2724fd62f15SChuanhong Guo mtk_ecc_hw_init(ecc); 2734fd62f15SChuanhong Guo 2744fd62f15SChuanhong Guo return ecc; 2754fd62f15SChuanhong Guo } 2764fd62f15SChuanhong Guo 2774fd62f15SChuanhong Guo struct mtk_ecc *of_mtk_ecc_get(struct device_node *of_node) 2784fd62f15SChuanhong Guo { 2794fd62f15SChuanhong Guo struct mtk_ecc *ecc = NULL; 2804fd62f15SChuanhong Guo struct device_node *np; 2814fd62f15SChuanhong Guo 282*4c5bf4b5SChuanhong Guo np = of_parse_phandle(of_node, "nand-ecc-engine", 0); 283*4c5bf4b5SChuanhong Guo /* for backward compatibility */ 284*4c5bf4b5SChuanhong Guo if (!np) 2854fd62f15SChuanhong Guo np = of_parse_phandle(of_node, "ecc-engine", 0); 2864fd62f15SChuanhong Guo if (np) { 2874fd62f15SChuanhong Guo ecc = mtk_ecc_get(np); 2884fd62f15SChuanhong Guo of_node_put(np); 2894fd62f15SChuanhong Guo } 2904fd62f15SChuanhong Guo 2914fd62f15SChuanhong Guo return ecc; 2924fd62f15SChuanhong Guo } 2934fd62f15SChuanhong Guo EXPORT_SYMBOL(of_mtk_ecc_get); 2944fd62f15SChuanhong Guo 2954fd62f15SChuanhong Guo int mtk_ecc_enable(struct mtk_ecc *ecc, struct mtk_ecc_config *config) 2964fd62f15SChuanhong Guo { 2974fd62f15SChuanhong Guo enum mtk_ecc_operation op = config->op; 2984fd62f15SChuanhong Guo u16 reg_val; 2994fd62f15SChuanhong Guo int ret; 3004fd62f15SChuanhong Guo 3014fd62f15SChuanhong Guo ret = mutex_lock_interruptible(&ecc->lock); 3024fd62f15SChuanhong Guo if (ret) { 3034fd62f15SChuanhong Guo dev_err(ecc->dev, "interrupted when attempting to lock\n"); 3044fd62f15SChuanhong Guo return ret; 3054fd62f15SChuanhong Guo } 3064fd62f15SChuanhong Guo 3074fd62f15SChuanhong Guo mtk_ecc_wait_idle(ecc, op); 3084fd62f15SChuanhong Guo 3094fd62f15SChuanhong Guo ret = mtk_ecc_config(ecc, config); 3104fd62f15SChuanhong Guo if (ret) { 3114fd62f15SChuanhong Guo mutex_unlock(&ecc->lock); 3124fd62f15SChuanhong Guo return ret; 3134fd62f15SChuanhong Guo } 3144fd62f15SChuanhong Guo 3154fd62f15SChuanhong Guo if (config->mode != ECC_NFI_MODE || op != ECC_ENCODE) { 3164fd62f15SChuanhong Guo init_completion(&ecc->done); 3174fd62f15SChuanhong Guo reg_val = ECC_IRQ_EN; 3184fd62f15SChuanhong Guo /* 3194fd62f15SChuanhong Guo * For ECC_NFI_MODE, if ecc->caps->pg_irq_sel is 1, then it 3204fd62f15SChuanhong Guo * means this chip can only generate one ecc irq during page 3214fd62f15SChuanhong Guo * read / write. If is 0, generate one ecc irq each ecc step. 3224fd62f15SChuanhong Guo */ 3234fd62f15SChuanhong Guo if (ecc->caps->pg_irq_sel && config->mode == ECC_NFI_MODE) 3244fd62f15SChuanhong Guo reg_val |= ECC_PG_IRQ_SEL; 3254fd62f15SChuanhong Guo if (op == ECC_ENCODE) 3264fd62f15SChuanhong Guo writew(reg_val, ecc->regs + 3274fd62f15SChuanhong Guo ecc->caps->ecc_regs[ECC_ENCIRQ_EN]); 3284fd62f15SChuanhong Guo else 3294fd62f15SChuanhong Guo writew(reg_val, ecc->regs + 3304fd62f15SChuanhong Guo ecc->caps->ecc_regs[ECC_DECIRQ_EN]); 3314fd62f15SChuanhong Guo } 3324fd62f15SChuanhong Guo 3334fd62f15SChuanhong Guo writew(ECC_OP_ENABLE, ecc->regs + ECC_CTL_REG(op)); 3344fd62f15SChuanhong Guo 3354fd62f15SChuanhong Guo return 0; 3364fd62f15SChuanhong Guo } 3374fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_enable); 3384fd62f15SChuanhong Guo 3394fd62f15SChuanhong Guo void mtk_ecc_disable(struct mtk_ecc *ecc) 3404fd62f15SChuanhong Guo { 3414fd62f15SChuanhong Guo enum mtk_ecc_operation op = ECC_ENCODE; 3424fd62f15SChuanhong Guo 3434fd62f15SChuanhong Guo /* find out the running operation */ 3444fd62f15SChuanhong Guo if (readw(ecc->regs + ECC_CTL_REG(op)) != ECC_OP_ENABLE) 3454fd62f15SChuanhong Guo op = ECC_DECODE; 3464fd62f15SChuanhong Guo 3474fd62f15SChuanhong Guo /* disable it */ 3484fd62f15SChuanhong Guo mtk_ecc_wait_idle(ecc, op); 3494fd62f15SChuanhong Guo if (op == ECC_DECODE) { 3504fd62f15SChuanhong Guo /* 3514fd62f15SChuanhong Guo * Clear decode IRQ status in case there is a timeout to wait 3524fd62f15SChuanhong Guo * decode IRQ. 3534fd62f15SChuanhong Guo */ 3544fd62f15SChuanhong Guo readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECDONE]); 3554fd62f15SChuanhong Guo writew(0, ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_EN]); 3564fd62f15SChuanhong Guo } else { 3574fd62f15SChuanhong Guo writew(0, ecc->regs + ecc->caps->ecc_regs[ECC_ENCIRQ_EN]); 3584fd62f15SChuanhong Guo } 3594fd62f15SChuanhong Guo 3604fd62f15SChuanhong Guo writew(ECC_OP_DISABLE, ecc->regs + ECC_CTL_REG(op)); 3614fd62f15SChuanhong Guo 3624fd62f15SChuanhong Guo mutex_unlock(&ecc->lock); 3634fd62f15SChuanhong Guo } 3644fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_disable); 3654fd62f15SChuanhong Guo 3664fd62f15SChuanhong Guo int mtk_ecc_wait_done(struct mtk_ecc *ecc, enum mtk_ecc_operation op) 3674fd62f15SChuanhong Guo { 3684fd62f15SChuanhong Guo int ret; 3694fd62f15SChuanhong Guo 3704fd62f15SChuanhong Guo ret = wait_for_completion_timeout(&ecc->done, msecs_to_jiffies(500)); 3714fd62f15SChuanhong Guo if (!ret) { 3724fd62f15SChuanhong Guo dev_err(ecc->dev, "%s timeout - interrupt did not arrive)\n", 3734fd62f15SChuanhong Guo (op == ECC_ENCODE) ? "encoder" : "decoder"); 3744fd62f15SChuanhong Guo return -ETIMEDOUT; 3754fd62f15SChuanhong Guo } 3764fd62f15SChuanhong Guo 3774fd62f15SChuanhong Guo return 0; 3784fd62f15SChuanhong Guo } 3794fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_wait_done); 3804fd62f15SChuanhong Guo 3814fd62f15SChuanhong Guo int mtk_ecc_encode(struct mtk_ecc *ecc, struct mtk_ecc_config *config, 3824fd62f15SChuanhong Guo u8 *data, u32 bytes) 3834fd62f15SChuanhong Guo { 3844fd62f15SChuanhong Guo dma_addr_t addr; 3854fd62f15SChuanhong Guo u32 len; 3864fd62f15SChuanhong Guo int ret; 3874fd62f15SChuanhong Guo 3884fd62f15SChuanhong Guo addr = dma_map_single(ecc->dev, data, bytes, DMA_TO_DEVICE); 3894fd62f15SChuanhong Guo ret = dma_mapping_error(ecc->dev, addr); 3904fd62f15SChuanhong Guo if (ret) { 3914fd62f15SChuanhong Guo dev_err(ecc->dev, "dma mapping error\n"); 3924fd62f15SChuanhong Guo return -EINVAL; 3934fd62f15SChuanhong Guo } 3944fd62f15SChuanhong Guo 3954fd62f15SChuanhong Guo config->op = ECC_ENCODE; 3964fd62f15SChuanhong Guo config->addr = addr; 3974fd62f15SChuanhong Guo ret = mtk_ecc_enable(ecc, config); 3984fd62f15SChuanhong Guo if (ret) { 3994fd62f15SChuanhong Guo dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE); 4004fd62f15SChuanhong Guo return ret; 4014fd62f15SChuanhong Guo } 4024fd62f15SChuanhong Guo 4034fd62f15SChuanhong Guo ret = mtk_ecc_wait_done(ecc, ECC_ENCODE); 4044fd62f15SChuanhong Guo if (ret) 4054fd62f15SChuanhong Guo goto timeout; 4064fd62f15SChuanhong Guo 4074fd62f15SChuanhong Guo mtk_ecc_wait_idle(ecc, ECC_ENCODE); 4084fd62f15SChuanhong Guo 4094fd62f15SChuanhong Guo /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */ 4104fd62f15SChuanhong Guo len = (config->strength * ecc->caps->parity_bits + 7) >> 3; 4114fd62f15SChuanhong Guo 4124fd62f15SChuanhong Guo /* write the parity bytes generated by the ECC back to temp buffer */ 4134fd62f15SChuanhong Guo __ioread32_copy(ecc->eccdata, 4144fd62f15SChuanhong Guo ecc->regs + ecc->caps->ecc_regs[ECC_ENCPAR00], 4154fd62f15SChuanhong Guo round_up(len, 4)); 4164fd62f15SChuanhong Guo 4174fd62f15SChuanhong Guo /* copy into possibly unaligned OOB region with actual length */ 4184fd62f15SChuanhong Guo memcpy(data + bytes, ecc->eccdata, len); 4194fd62f15SChuanhong Guo timeout: 4204fd62f15SChuanhong Guo 4214fd62f15SChuanhong Guo dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE); 4224fd62f15SChuanhong Guo mtk_ecc_disable(ecc); 4234fd62f15SChuanhong Guo 4244fd62f15SChuanhong Guo return ret; 4254fd62f15SChuanhong Guo } 4264fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_encode); 4274fd62f15SChuanhong Guo 4284fd62f15SChuanhong Guo void mtk_ecc_adjust_strength(struct mtk_ecc *ecc, u32 *p) 4294fd62f15SChuanhong Guo { 4304fd62f15SChuanhong Guo const u8 *ecc_strength = ecc->caps->ecc_strength; 4314fd62f15SChuanhong Guo int i; 4324fd62f15SChuanhong Guo 4334fd62f15SChuanhong Guo for (i = 0; i < ecc->caps->num_ecc_strength; i++) { 4344fd62f15SChuanhong Guo if (*p <= ecc_strength[i]) { 4354fd62f15SChuanhong Guo if (!i) 4364fd62f15SChuanhong Guo *p = ecc_strength[i]; 4374fd62f15SChuanhong Guo else if (*p != ecc_strength[i]) 4384fd62f15SChuanhong Guo *p = ecc_strength[i - 1]; 4394fd62f15SChuanhong Guo return; 4404fd62f15SChuanhong Guo } 4414fd62f15SChuanhong Guo } 4424fd62f15SChuanhong Guo 4434fd62f15SChuanhong Guo *p = ecc_strength[ecc->caps->num_ecc_strength - 1]; 4444fd62f15SChuanhong Guo } 4454fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_adjust_strength); 4464fd62f15SChuanhong Guo 4474fd62f15SChuanhong Guo unsigned int mtk_ecc_get_parity_bits(struct mtk_ecc *ecc) 4484fd62f15SChuanhong Guo { 4494fd62f15SChuanhong Guo return ecc->caps->parity_bits; 4504fd62f15SChuanhong Guo } 4514fd62f15SChuanhong Guo EXPORT_SYMBOL(mtk_ecc_get_parity_bits); 4524fd62f15SChuanhong Guo 4534fd62f15SChuanhong Guo static const struct mtk_ecc_caps mtk_ecc_caps_mt2701 = { 4544fd62f15SChuanhong Guo .err_mask = 0x3f, 455d3353719SLinus Torvalds .err_shift = 8, 4564fd62f15SChuanhong Guo .ecc_strength = ecc_strength_mt2701, 4574fd62f15SChuanhong Guo .ecc_regs = mt2701_ecc_regs, 4584fd62f15SChuanhong Guo .num_ecc_strength = 20, 4594fd62f15SChuanhong Guo .ecc_mode_shift = 5, 4604fd62f15SChuanhong Guo .parity_bits = 14, 4614fd62f15SChuanhong Guo .pg_irq_sel = 0, 4624fd62f15SChuanhong Guo }; 4634fd62f15SChuanhong Guo 4644fd62f15SChuanhong Guo static const struct mtk_ecc_caps mtk_ecc_caps_mt2712 = { 4654fd62f15SChuanhong Guo .err_mask = 0x7f, 466d3353719SLinus Torvalds .err_shift = 8, 4674fd62f15SChuanhong Guo .ecc_strength = ecc_strength_mt2712, 4684fd62f15SChuanhong Guo .ecc_regs = mt2712_ecc_regs, 4694fd62f15SChuanhong Guo .num_ecc_strength = 23, 4704fd62f15SChuanhong Guo .ecc_mode_shift = 5, 4714fd62f15SChuanhong Guo .parity_bits = 14, 4724fd62f15SChuanhong Guo .pg_irq_sel = 1, 4734fd62f15SChuanhong Guo }; 4744fd62f15SChuanhong Guo 4754fd62f15SChuanhong Guo static const struct mtk_ecc_caps mtk_ecc_caps_mt7622 = { 476d3353719SLinus Torvalds .err_mask = 0x1f, 477d3353719SLinus Torvalds .err_shift = 5, 4784fd62f15SChuanhong Guo .ecc_strength = ecc_strength_mt7622, 4794fd62f15SChuanhong Guo .ecc_regs = mt7622_ecc_regs, 480d3353719SLinus Torvalds .num_ecc_strength = 5, 4814fd62f15SChuanhong Guo .ecc_mode_shift = 4, 4824fd62f15SChuanhong Guo .parity_bits = 13, 4834fd62f15SChuanhong Guo .pg_irq_sel = 0, 4844fd62f15SChuanhong Guo }; 4854fd62f15SChuanhong Guo 4864fd62f15SChuanhong Guo static const struct of_device_id mtk_ecc_dt_match[] = { 4874fd62f15SChuanhong Guo { 4884fd62f15SChuanhong Guo .compatible = "mediatek,mt2701-ecc", 4894fd62f15SChuanhong Guo .data = &mtk_ecc_caps_mt2701, 4904fd62f15SChuanhong Guo }, { 4914fd62f15SChuanhong Guo .compatible = "mediatek,mt2712-ecc", 4924fd62f15SChuanhong Guo .data = &mtk_ecc_caps_mt2712, 4934fd62f15SChuanhong Guo }, { 4944fd62f15SChuanhong Guo .compatible = "mediatek,mt7622-ecc", 4954fd62f15SChuanhong Guo .data = &mtk_ecc_caps_mt7622, 4964fd62f15SChuanhong Guo }, 4974fd62f15SChuanhong Guo {}, 4984fd62f15SChuanhong Guo }; 4994fd62f15SChuanhong Guo 5004fd62f15SChuanhong Guo static int mtk_ecc_probe(struct platform_device *pdev) 5014fd62f15SChuanhong Guo { 5024fd62f15SChuanhong Guo struct device *dev = &pdev->dev; 5034fd62f15SChuanhong Guo struct mtk_ecc *ecc; 5044fd62f15SChuanhong Guo u32 max_eccdata_size; 5054fd62f15SChuanhong Guo int irq, ret; 5064fd62f15SChuanhong Guo 5074fd62f15SChuanhong Guo ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL); 5084fd62f15SChuanhong Guo if (!ecc) 5094fd62f15SChuanhong Guo return -ENOMEM; 5104fd62f15SChuanhong Guo 5114fd62f15SChuanhong Guo ecc->caps = of_device_get_match_data(dev); 5124fd62f15SChuanhong Guo 5134fd62f15SChuanhong Guo max_eccdata_size = ecc->caps->num_ecc_strength - 1; 5144fd62f15SChuanhong Guo max_eccdata_size = ecc->caps->ecc_strength[max_eccdata_size]; 5154fd62f15SChuanhong Guo max_eccdata_size = (max_eccdata_size * ecc->caps->parity_bits + 7) >> 3; 5164fd62f15SChuanhong Guo max_eccdata_size = round_up(max_eccdata_size, 4); 5174fd62f15SChuanhong Guo ecc->eccdata = devm_kzalloc(dev, max_eccdata_size, GFP_KERNEL); 5184fd62f15SChuanhong Guo if (!ecc->eccdata) 5194fd62f15SChuanhong Guo return -ENOMEM; 5204fd62f15SChuanhong Guo 5214fd62f15SChuanhong Guo ecc->regs = devm_platform_ioremap_resource(pdev, 0); 5224fd62f15SChuanhong Guo if (IS_ERR(ecc->regs)) 5234fd62f15SChuanhong Guo return PTR_ERR(ecc->regs); 5244fd62f15SChuanhong Guo 5254fd62f15SChuanhong Guo ecc->clk = devm_clk_get(dev, NULL); 5264fd62f15SChuanhong Guo if (IS_ERR(ecc->clk)) { 5274fd62f15SChuanhong Guo dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk)); 5284fd62f15SChuanhong Guo return PTR_ERR(ecc->clk); 5294fd62f15SChuanhong Guo } 5304fd62f15SChuanhong Guo 5314fd62f15SChuanhong Guo irq = platform_get_irq(pdev, 0); 5324fd62f15SChuanhong Guo if (irq < 0) 5334fd62f15SChuanhong Guo return irq; 5344fd62f15SChuanhong Guo 5354fd62f15SChuanhong Guo ret = dma_set_mask(dev, DMA_BIT_MASK(32)); 5364fd62f15SChuanhong Guo if (ret) { 5374fd62f15SChuanhong Guo dev_err(dev, "failed to set DMA mask\n"); 5384fd62f15SChuanhong Guo return ret; 5394fd62f15SChuanhong Guo } 5404fd62f15SChuanhong Guo 5414fd62f15SChuanhong Guo ret = devm_request_irq(dev, irq, mtk_ecc_irq, 0x0, "mtk-ecc", ecc); 5424fd62f15SChuanhong Guo if (ret) { 5434fd62f15SChuanhong Guo dev_err(dev, "failed to request irq\n"); 5444fd62f15SChuanhong Guo return -EINVAL; 5454fd62f15SChuanhong Guo } 5464fd62f15SChuanhong Guo 5474fd62f15SChuanhong Guo ecc->dev = dev; 5484fd62f15SChuanhong Guo mutex_init(&ecc->lock); 5494fd62f15SChuanhong Guo platform_set_drvdata(pdev, ecc); 5504fd62f15SChuanhong Guo dev_info(dev, "probed\n"); 5514fd62f15SChuanhong Guo 5524fd62f15SChuanhong Guo return 0; 5534fd62f15SChuanhong Guo } 5544fd62f15SChuanhong Guo 5554fd62f15SChuanhong Guo #ifdef CONFIG_PM_SLEEP 5564fd62f15SChuanhong Guo static int mtk_ecc_suspend(struct device *dev) 5574fd62f15SChuanhong Guo { 5584fd62f15SChuanhong Guo struct mtk_ecc *ecc = dev_get_drvdata(dev); 5594fd62f15SChuanhong Guo 5604fd62f15SChuanhong Guo clk_disable_unprepare(ecc->clk); 5614fd62f15SChuanhong Guo 5624fd62f15SChuanhong Guo return 0; 5634fd62f15SChuanhong Guo } 5644fd62f15SChuanhong Guo 5654fd62f15SChuanhong Guo static int mtk_ecc_resume(struct device *dev) 5664fd62f15SChuanhong Guo { 5674fd62f15SChuanhong Guo struct mtk_ecc *ecc = dev_get_drvdata(dev); 5684fd62f15SChuanhong Guo int ret; 5694fd62f15SChuanhong Guo 5704fd62f15SChuanhong Guo ret = clk_prepare_enable(ecc->clk); 5714fd62f15SChuanhong Guo if (ret) { 5724fd62f15SChuanhong Guo dev_err(dev, "failed to enable clk\n"); 5734fd62f15SChuanhong Guo return ret; 5744fd62f15SChuanhong Guo } 5754fd62f15SChuanhong Guo 5764fd62f15SChuanhong Guo return 0; 5774fd62f15SChuanhong Guo } 5784fd62f15SChuanhong Guo 5794fd62f15SChuanhong Guo static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops, mtk_ecc_suspend, mtk_ecc_resume); 5804fd62f15SChuanhong Guo #endif 5814fd62f15SChuanhong Guo 5824fd62f15SChuanhong Guo MODULE_DEVICE_TABLE(of, mtk_ecc_dt_match); 5834fd62f15SChuanhong Guo 5844fd62f15SChuanhong Guo static struct platform_driver mtk_ecc_driver = { 5854fd62f15SChuanhong Guo .probe = mtk_ecc_probe, 5864fd62f15SChuanhong Guo .driver = { 5874fd62f15SChuanhong Guo .name = "mtk-ecc", 5884fd62f15SChuanhong Guo .of_match_table = mtk_ecc_dt_match, 5894fd62f15SChuanhong Guo #ifdef CONFIG_PM_SLEEP 5904fd62f15SChuanhong Guo .pm = &mtk_ecc_pm_ops, 5914fd62f15SChuanhong Guo #endif 5924fd62f15SChuanhong Guo }, 5934fd62f15SChuanhong Guo }; 5944fd62f15SChuanhong Guo 5954fd62f15SChuanhong Guo module_platform_driver(mtk_ecc_driver); 5964fd62f15SChuanhong Guo 5974fd62f15SChuanhong Guo MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>"); 5984fd62f15SChuanhong Guo MODULE_DESCRIPTION("MTK Nand ECC Driver"); 5994fd62f15SChuanhong Guo MODULE_LICENSE("Dual MIT/GPL"); 600