1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 293db446aSBoris Brezillon /* 393db446aSBoris Brezillon * Error Location Module 493db446aSBoris Brezillon * 593db446aSBoris Brezillon * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 693db446aSBoris Brezillon */ 793db446aSBoris Brezillon 893db446aSBoris Brezillon #define DRIVER_NAME "omap-elm" 993db446aSBoris Brezillon 1093db446aSBoris Brezillon #include <linux/platform_device.h> 1193db446aSBoris Brezillon #include <linux/module.h> 1293db446aSBoris Brezillon #include <linux/interrupt.h> 1393db446aSBoris Brezillon #include <linux/io.h> 1493db446aSBoris Brezillon #include <linux/of.h> 1593db446aSBoris Brezillon #include <linux/sched.h> 1693db446aSBoris Brezillon #include <linux/pm_runtime.h> 1793db446aSBoris Brezillon #include <linux/platform_data/elm.h> 1893db446aSBoris Brezillon 1993db446aSBoris Brezillon #define ELM_SYSCONFIG 0x010 2093db446aSBoris Brezillon #define ELM_IRQSTATUS 0x018 2193db446aSBoris Brezillon #define ELM_IRQENABLE 0x01c 2293db446aSBoris Brezillon #define ELM_LOCATION_CONFIG 0x020 2393db446aSBoris Brezillon #define ELM_PAGE_CTRL 0x080 2493db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_0 0x400 2593db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_1 0x404 2693db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_2 0x408 2793db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_3 0x40c 2893db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_4 0x410 2993db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_5 0x414 3093db446aSBoris Brezillon #define ELM_SYNDROME_FRAGMENT_6 0x418 3193db446aSBoris Brezillon #define ELM_LOCATION_STATUS 0x800 3293db446aSBoris Brezillon #define ELM_ERROR_LOCATION_0 0x880 3393db446aSBoris Brezillon 3493db446aSBoris Brezillon /* ELM Interrupt Status Register */ 3593db446aSBoris Brezillon #define INTR_STATUS_PAGE_VALID BIT(8) 3693db446aSBoris Brezillon 3793db446aSBoris Brezillon /* ELM Interrupt Enable Register */ 3893db446aSBoris Brezillon #define INTR_EN_PAGE_MASK BIT(8) 3993db446aSBoris Brezillon 4093db446aSBoris Brezillon /* ELM Location Configuration Register */ 4193db446aSBoris Brezillon #define ECC_BCH_LEVEL_MASK 0x3 4293db446aSBoris Brezillon 4393db446aSBoris Brezillon /* ELM syndrome */ 4493db446aSBoris Brezillon #define ELM_SYNDROME_VALID BIT(16) 4593db446aSBoris Brezillon 4693db446aSBoris Brezillon /* ELM_LOCATION_STATUS Register */ 4793db446aSBoris Brezillon #define ECC_CORRECTABLE_MASK BIT(8) 4893db446aSBoris Brezillon #define ECC_NB_ERRORS_MASK 0x1f 4993db446aSBoris Brezillon 5093db446aSBoris Brezillon /* ELM_ERROR_LOCATION_0-15 Registers */ 5193db446aSBoris Brezillon #define ECC_ERROR_LOCATION_MASK 0x1fff 5293db446aSBoris Brezillon 5393db446aSBoris Brezillon #define ELM_ECC_SIZE 0x7ff 5493db446aSBoris Brezillon 5593db446aSBoris Brezillon #define SYNDROME_FRAGMENT_REG_SIZE 0x40 5693db446aSBoris Brezillon #define ERROR_LOCATION_SIZE 0x100 5793db446aSBoris Brezillon 5893db446aSBoris Brezillon struct elm_registers { 5993db446aSBoris Brezillon u32 elm_irqenable; 6093db446aSBoris Brezillon u32 elm_sysconfig; 6193db446aSBoris Brezillon u32 elm_location_config; 6293db446aSBoris Brezillon u32 elm_page_ctrl; 6393db446aSBoris Brezillon u32 elm_syndrome_fragment_6[ERROR_VECTOR_MAX]; 6493db446aSBoris Brezillon u32 elm_syndrome_fragment_5[ERROR_VECTOR_MAX]; 6593db446aSBoris Brezillon u32 elm_syndrome_fragment_4[ERROR_VECTOR_MAX]; 6693db446aSBoris Brezillon u32 elm_syndrome_fragment_3[ERROR_VECTOR_MAX]; 6793db446aSBoris Brezillon u32 elm_syndrome_fragment_2[ERROR_VECTOR_MAX]; 6893db446aSBoris Brezillon u32 elm_syndrome_fragment_1[ERROR_VECTOR_MAX]; 6993db446aSBoris Brezillon u32 elm_syndrome_fragment_0[ERROR_VECTOR_MAX]; 7093db446aSBoris Brezillon }; 7193db446aSBoris Brezillon 7293db446aSBoris Brezillon struct elm_info { 7393db446aSBoris Brezillon struct device *dev; 7493db446aSBoris Brezillon void __iomem *elm_base; 7593db446aSBoris Brezillon struct completion elm_completion; 7693db446aSBoris Brezillon struct list_head list; 7793db446aSBoris Brezillon enum bch_ecc bch_type; 7893db446aSBoris Brezillon struct elm_registers elm_regs; 7993db446aSBoris Brezillon int ecc_steps; 8093db446aSBoris Brezillon int ecc_syndrome_size; 8193db446aSBoris Brezillon }; 8293db446aSBoris Brezillon 8393db446aSBoris Brezillon static LIST_HEAD(elm_devices); 8493db446aSBoris Brezillon 8593db446aSBoris Brezillon static void elm_write_reg(struct elm_info *info, int offset, u32 val) 8693db446aSBoris Brezillon { 8793db446aSBoris Brezillon writel(val, info->elm_base + offset); 8893db446aSBoris Brezillon } 8993db446aSBoris Brezillon 9093db446aSBoris Brezillon static u32 elm_read_reg(struct elm_info *info, int offset) 9193db446aSBoris Brezillon { 9293db446aSBoris Brezillon return readl(info->elm_base + offset); 9393db446aSBoris Brezillon } 9493db446aSBoris Brezillon 9593db446aSBoris Brezillon /** 9693db446aSBoris Brezillon * elm_config - Configure ELM module 9793db446aSBoris Brezillon * @dev: ELM device 9893db446aSBoris Brezillon * @bch_type: Type of BCH ecc 9993db446aSBoris Brezillon */ 10093db446aSBoris Brezillon int elm_config(struct device *dev, enum bch_ecc bch_type, 10193db446aSBoris Brezillon int ecc_steps, int ecc_step_size, int ecc_syndrome_size) 10293db446aSBoris Brezillon { 10393db446aSBoris Brezillon u32 reg_val; 10493db446aSBoris Brezillon struct elm_info *info = dev_get_drvdata(dev); 10593db446aSBoris Brezillon 10693db446aSBoris Brezillon if (!info) { 10793db446aSBoris Brezillon dev_err(dev, "Unable to configure elm - device not probed?\n"); 10893db446aSBoris Brezillon return -EPROBE_DEFER; 10993db446aSBoris Brezillon } 11093db446aSBoris Brezillon /* ELM cannot detect ECC errors for chunks > 1KB */ 11193db446aSBoris Brezillon if (ecc_step_size > ((ELM_ECC_SIZE + 1) / 2)) { 11293db446aSBoris Brezillon dev_err(dev, "unsupported config ecc-size=%d\n", ecc_step_size); 11393db446aSBoris Brezillon return -EINVAL; 11493db446aSBoris Brezillon } 11593db446aSBoris Brezillon /* ELM support 8 error syndrome process */ 11693db446aSBoris Brezillon if (ecc_steps > ERROR_VECTOR_MAX) { 11793db446aSBoris Brezillon dev_err(dev, "unsupported config ecc-step=%d\n", ecc_steps); 11893db446aSBoris Brezillon return -EINVAL; 11993db446aSBoris Brezillon } 12093db446aSBoris Brezillon 12193db446aSBoris Brezillon reg_val = (bch_type & ECC_BCH_LEVEL_MASK) | (ELM_ECC_SIZE << 16); 12293db446aSBoris Brezillon elm_write_reg(info, ELM_LOCATION_CONFIG, reg_val); 12393db446aSBoris Brezillon info->bch_type = bch_type; 12493db446aSBoris Brezillon info->ecc_steps = ecc_steps; 12593db446aSBoris Brezillon info->ecc_syndrome_size = ecc_syndrome_size; 12693db446aSBoris Brezillon 12793db446aSBoris Brezillon return 0; 12893db446aSBoris Brezillon } 12993db446aSBoris Brezillon EXPORT_SYMBOL(elm_config); 13093db446aSBoris Brezillon 13193db446aSBoris Brezillon /** 13293db446aSBoris Brezillon * elm_configure_page_mode - Enable/Disable page mode 13393db446aSBoris Brezillon * @info: elm info 13493db446aSBoris Brezillon * @index: index number of syndrome fragment vector 13593db446aSBoris Brezillon * @enable: enable/disable flag for page mode 13693db446aSBoris Brezillon * 13793db446aSBoris Brezillon * Enable page mode for syndrome fragment index 13893db446aSBoris Brezillon */ 13993db446aSBoris Brezillon static void elm_configure_page_mode(struct elm_info *info, int index, 14093db446aSBoris Brezillon bool enable) 14193db446aSBoris Brezillon { 14293db446aSBoris Brezillon u32 reg_val; 14393db446aSBoris Brezillon 14493db446aSBoris Brezillon reg_val = elm_read_reg(info, ELM_PAGE_CTRL); 14593db446aSBoris Brezillon if (enable) 14693db446aSBoris Brezillon reg_val |= BIT(index); /* enable page mode */ 14793db446aSBoris Brezillon else 14893db446aSBoris Brezillon reg_val &= ~BIT(index); /* disable page mode */ 14993db446aSBoris Brezillon 15093db446aSBoris Brezillon elm_write_reg(info, ELM_PAGE_CTRL, reg_val); 15193db446aSBoris Brezillon } 15293db446aSBoris Brezillon 15393db446aSBoris Brezillon /** 15493db446aSBoris Brezillon * elm_load_syndrome - Load ELM syndrome reg 15593db446aSBoris Brezillon * @info: elm info 15693db446aSBoris Brezillon * @err_vec: elm error vectors 15793db446aSBoris Brezillon * @ecc: buffer with calculated ecc 15893db446aSBoris Brezillon * 15993db446aSBoris Brezillon * Load syndrome fragment registers with calculated ecc in reverse order. 16093db446aSBoris Brezillon */ 16193db446aSBoris Brezillon static void elm_load_syndrome(struct elm_info *info, 16293db446aSBoris Brezillon struct elm_errorvec *err_vec, u8 *ecc) 16393db446aSBoris Brezillon { 16493db446aSBoris Brezillon int i, offset; 16593db446aSBoris Brezillon u32 val; 16693db446aSBoris Brezillon 16793db446aSBoris Brezillon for (i = 0; i < info->ecc_steps; i++) { 16893db446aSBoris Brezillon 16993db446aSBoris Brezillon /* Check error reported */ 17093db446aSBoris Brezillon if (err_vec[i].error_reported) { 17193db446aSBoris Brezillon elm_configure_page_mode(info, i, true); 17293db446aSBoris Brezillon offset = ELM_SYNDROME_FRAGMENT_0 + 17393db446aSBoris Brezillon SYNDROME_FRAGMENT_REG_SIZE * i; 17493db446aSBoris Brezillon switch (info->bch_type) { 17593db446aSBoris Brezillon case BCH8_ECC: 17693db446aSBoris Brezillon /* syndrome fragment 0 = ecc[9-12B] */ 17793db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[9]); 17893db446aSBoris Brezillon elm_write_reg(info, offset, val); 17993db446aSBoris Brezillon 18093db446aSBoris Brezillon /* syndrome fragment 1 = ecc[5-8B] */ 18193db446aSBoris Brezillon offset += 4; 18293db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[5]); 18393db446aSBoris Brezillon elm_write_reg(info, offset, val); 18493db446aSBoris Brezillon 18593db446aSBoris Brezillon /* syndrome fragment 2 = ecc[1-4B] */ 18693db446aSBoris Brezillon offset += 4; 18793db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[1]); 18893db446aSBoris Brezillon elm_write_reg(info, offset, val); 18993db446aSBoris Brezillon 19093db446aSBoris Brezillon /* syndrome fragment 3 = ecc[0B] */ 19193db446aSBoris Brezillon offset += 4; 19293db446aSBoris Brezillon val = ecc[0]; 19393db446aSBoris Brezillon elm_write_reg(info, offset, val); 19493db446aSBoris Brezillon break; 19593db446aSBoris Brezillon case BCH4_ECC: 19693db446aSBoris Brezillon /* syndrome fragment 0 = ecc[20-52b] bits */ 19793db446aSBoris Brezillon val = (cpu_to_be32(*(u32 *) &ecc[3]) >> 4) | 19893db446aSBoris Brezillon ((ecc[2] & 0xf) << 28); 19993db446aSBoris Brezillon elm_write_reg(info, offset, val); 20093db446aSBoris Brezillon 20193db446aSBoris Brezillon /* syndrome fragment 1 = ecc[0-20b] bits */ 20293db446aSBoris Brezillon offset += 4; 20393db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[0]) >> 12; 20493db446aSBoris Brezillon elm_write_reg(info, offset, val); 20593db446aSBoris Brezillon break; 20693db446aSBoris Brezillon case BCH16_ECC: 20793db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[22]); 20893db446aSBoris Brezillon elm_write_reg(info, offset, val); 20993db446aSBoris Brezillon offset += 4; 21093db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[18]); 21193db446aSBoris Brezillon elm_write_reg(info, offset, val); 21293db446aSBoris Brezillon offset += 4; 21393db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[14]); 21493db446aSBoris Brezillon elm_write_reg(info, offset, val); 21593db446aSBoris Brezillon offset += 4; 21693db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[10]); 21793db446aSBoris Brezillon elm_write_reg(info, offset, val); 21893db446aSBoris Brezillon offset += 4; 21993db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[6]); 22093db446aSBoris Brezillon elm_write_reg(info, offset, val); 22193db446aSBoris Brezillon offset += 4; 22293db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[2]); 22393db446aSBoris Brezillon elm_write_reg(info, offset, val); 22493db446aSBoris Brezillon offset += 4; 22593db446aSBoris Brezillon val = cpu_to_be32(*(u32 *) &ecc[0]) >> 16; 22693db446aSBoris Brezillon elm_write_reg(info, offset, val); 22793db446aSBoris Brezillon break; 22893db446aSBoris Brezillon default: 22993db446aSBoris Brezillon pr_err("invalid config bch_type\n"); 23093db446aSBoris Brezillon } 23193db446aSBoris Brezillon } 23293db446aSBoris Brezillon 23393db446aSBoris Brezillon /* Update ecc pointer with ecc byte size */ 23493db446aSBoris Brezillon ecc += info->ecc_syndrome_size; 23593db446aSBoris Brezillon } 23693db446aSBoris Brezillon } 23793db446aSBoris Brezillon 23893db446aSBoris Brezillon /** 23993db446aSBoris Brezillon * elm_start_processing - start elm syndrome processing 24093db446aSBoris Brezillon * @info: elm info 24193db446aSBoris Brezillon * @err_vec: elm error vectors 24293db446aSBoris Brezillon * 24393db446aSBoris Brezillon * Set syndrome valid bit for syndrome fragment registers for which 24493db446aSBoris Brezillon * elm syndrome fragment registers are loaded. This enables elm module 24593db446aSBoris Brezillon * to start processing syndrome vectors. 24693db446aSBoris Brezillon */ 24793db446aSBoris Brezillon static void elm_start_processing(struct elm_info *info, 24893db446aSBoris Brezillon struct elm_errorvec *err_vec) 24993db446aSBoris Brezillon { 25093db446aSBoris Brezillon int i, offset; 25193db446aSBoris Brezillon u32 reg_val; 25293db446aSBoris Brezillon 25393db446aSBoris Brezillon /* 25493db446aSBoris Brezillon * Set syndrome vector valid, so that ELM module 25593db446aSBoris Brezillon * will process it for vectors error is reported 25693db446aSBoris Brezillon */ 25793db446aSBoris Brezillon for (i = 0; i < info->ecc_steps; i++) { 25893db446aSBoris Brezillon if (err_vec[i].error_reported) { 25993db446aSBoris Brezillon offset = ELM_SYNDROME_FRAGMENT_6 + 26093db446aSBoris Brezillon SYNDROME_FRAGMENT_REG_SIZE * i; 26193db446aSBoris Brezillon reg_val = elm_read_reg(info, offset); 26293db446aSBoris Brezillon reg_val |= ELM_SYNDROME_VALID; 26393db446aSBoris Brezillon elm_write_reg(info, offset, reg_val); 26493db446aSBoris Brezillon } 26593db446aSBoris Brezillon } 26693db446aSBoris Brezillon } 26793db446aSBoris Brezillon 26893db446aSBoris Brezillon /** 26993db446aSBoris Brezillon * elm_error_correction - locate correctable error position 27093db446aSBoris Brezillon * @info: elm info 27193db446aSBoris Brezillon * @err_vec: elm error vectors 27293db446aSBoris Brezillon * 27393db446aSBoris Brezillon * On completion of processing by elm module, error location status 27493db446aSBoris Brezillon * register updated with correctable/uncorrectable error information. 27593db446aSBoris Brezillon * In case of correctable errors, number of errors located from 27693db446aSBoris Brezillon * elm location status register & read the positions from 27793db446aSBoris Brezillon * elm error location register. 27893db446aSBoris Brezillon */ 27993db446aSBoris Brezillon static void elm_error_correction(struct elm_info *info, 28093db446aSBoris Brezillon struct elm_errorvec *err_vec) 28193db446aSBoris Brezillon { 28293db446aSBoris Brezillon int i, j, errors = 0; 28393db446aSBoris Brezillon int offset; 28493db446aSBoris Brezillon u32 reg_val; 28593db446aSBoris Brezillon 28693db446aSBoris Brezillon for (i = 0; i < info->ecc_steps; i++) { 28793db446aSBoris Brezillon 28893db446aSBoris Brezillon /* Check error reported */ 28993db446aSBoris Brezillon if (err_vec[i].error_reported) { 29093db446aSBoris Brezillon offset = ELM_LOCATION_STATUS + ERROR_LOCATION_SIZE * i; 29193db446aSBoris Brezillon reg_val = elm_read_reg(info, offset); 29293db446aSBoris Brezillon 29393db446aSBoris Brezillon /* Check correctable error or not */ 29493db446aSBoris Brezillon if (reg_val & ECC_CORRECTABLE_MASK) { 29593db446aSBoris Brezillon offset = ELM_ERROR_LOCATION_0 + 29693db446aSBoris Brezillon ERROR_LOCATION_SIZE * i; 29793db446aSBoris Brezillon 29893db446aSBoris Brezillon /* Read count of correctable errors */ 29993db446aSBoris Brezillon err_vec[i].error_count = reg_val & 30093db446aSBoris Brezillon ECC_NB_ERRORS_MASK; 30193db446aSBoris Brezillon 30293db446aSBoris Brezillon /* Update the error locations in error vector */ 30393db446aSBoris Brezillon for (j = 0; j < err_vec[i].error_count; j++) { 30493db446aSBoris Brezillon 30593db446aSBoris Brezillon reg_val = elm_read_reg(info, offset); 30693db446aSBoris Brezillon err_vec[i].error_loc[j] = reg_val & 30793db446aSBoris Brezillon ECC_ERROR_LOCATION_MASK; 30893db446aSBoris Brezillon 30993db446aSBoris Brezillon /* Update error location register */ 31093db446aSBoris Brezillon offset += 4; 31193db446aSBoris Brezillon } 31293db446aSBoris Brezillon 31393db446aSBoris Brezillon errors += err_vec[i].error_count; 31493db446aSBoris Brezillon } else { 31593db446aSBoris Brezillon err_vec[i].error_uncorrectable = true; 31693db446aSBoris Brezillon } 31793db446aSBoris Brezillon 31893db446aSBoris Brezillon /* Clearing interrupts for processed error vectors */ 31993db446aSBoris Brezillon elm_write_reg(info, ELM_IRQSTATUS, BIT(i)); 32093db446aSBoris Brezillon 32193db446aSBoris Brezillon /* Disable page mode */ 32293db446aSBoris Brezillon elm_configure_page_mode(info, i, false); 32393db446aSBoris Brezillon } 32493db446aSBoris Brezillon } 32593db446aSBoris Brezillon } 32693db446aSBoris Brezillon 32793db446aSBoris Brezillon /** 32893db446aSBoris Brezillon * elm_decode_bch_error_page - Locate error position 32993db446aSBoris Brezillon * @dev: device pointer 33093db446aSBoris Brezillon * @ecc_calc: calculated ECC bytes from GPMC 33193db446aSBoris Brezillon * @err_vec: elm error vectors 33293db446aSBoris Brezillon * 33393db446aSBoris Brezillon * Called with one or more error reported vectors & vectors with 33493db446aSBoris Brezillon * error reported is updated in err_vec[].error_reported 33593db446aSBoris Brezillon */ 33693db446aSBoris Brezillon void elm_decode_bch_error_page(struct device *dev, u8 *ecc_calc, 33793db446aSBoris Brezillon struct elm_errorvec *err_vec) 33893db446aSBoris Brezillon { 33993db446aSBoris Brezillon struct elm_info *info = dev_get_drvdata(dev); 34093db446aSBoris Brezillon u32 reg_val; 34193db446aSBoris Brezillon 34293db446aSBoris Brezillon /* Enable page mode interrupt */ 34393db446aSBoris Brezillon reg_val = elm_read_reg(info, ELM_IRQSTATUS); 34493db446aSBoris Brezillon elm_write_reg(info, ELM_IRQSTATUS, reg_val & INTR_STATUS_PAGE_VALID); 34593db446aSBoris Brezillon elm_write_reg(info, ELM_IRQENABLE, INTR_EN_PAGE_MASK); 34693db446aSBoris Brezillon 34793db446aSBoris Brezillon /* Load valid ecc byte to syndrome fragment register */ 34893db446aSBoris Brezillon elm_load_syndrome(info, err_vec, ecc_calc); 34993db446aSBoris Brezillon 35093db446aSBoris Brezillon /* Enable syndrome processing for which syndrome fragment is updated */ 35193db446aSBoris Brezillon elm_start_processing(info, err_vec); 35293db446aSBoris Brezillon 35393db446aSBoris Brezillon /* Wait for ELM module to finish locating error correction */ 35493db446aSBoris Brezillon wait_for_completion(&info->elm_completion); 35593db446aSBoris Brezillon 35693db446aSBoris Brezillon /* Disable page mode interrupt */ 35793db446aSBoris Brezillon reg_val = elm_read_reg(info, ELM_IRQENABLE); 35893db446aSBoris Brezillon elm_write_reg(info, ELM_IRQENABLE, reg_val & ~INTR_EN_PAGE_MASK); 35993db446aSBoris Brezillon elm_error_correction(info, err_vec); 36093db446aSBoris Brezillon } 36193db446aSBoris Brezillon EXPORT_SYMBOL(elm_decode_bch_error_page); 36293db446aSBoris Brezillon 36393db446aSBoris Brezillon static irqreturn_t elm_isr(int this_irq, void *dev_id) 36493db446aSBoris Brezillon { 36593db446aSBoris Brezillon u32 reg_val; 36693db446aSBoris Brezillon struct elm_info *info = dev_id; 36793db446aSBoris Brezillon 36893db446aSBoris Brezillon reg_val = elm_read_reg(info, ELM_IRQSTATUS); 36993db446aSBoris Brezillon 37093db446aSBoris Brezillon /* All error vectors processed */ 37193db446aSBoris Brezillon if (reg_val & INTR_STATUS_PAGE_VALID) { 37293db446aSBoris Brezillon elm_write_reg(info, ELM_IRQSTATUS, 37393db446aSBoris Brezillon reg_val & INTR_STATUS_PAGE_VALID); 37493db446aSBoris Brezillon complete(&info->elm_completion); 37593db446aSBoris Brezillon return IRQ_HANDLED; 37693db446aSBoris Brezillon } 37793db446aSBoris Brezillon 37893db446aSBoris Brezillon return IRQ_NONE; 37993db446aSBoris Brezillon } 38093db446aSBoris Brezillon 38193db446aSBoris Brezillon static int elm_probe(struct platform_device *pdev) 38293db446aSBoris Brezillon { 38393db446aSBoris Brezillon int ret = 0; 38493db446aSBoris Brezillon struct resource *res, *irq; 38593db446aSBoris Brezillon struct elm_info *info; 38693db446aSBoris Brezillon 38793db446aSBoris Brezillon info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 38893db446aSBoris Brezillon if (!info) 38993db446aSBoris Brezillon return -ENOMEM; 39093db446aSBoris Brezillon 39193db446aSBoris Brezillon info->dev = &pdev->dev; 39293db446aSBoris Brezillon 39393db446aSBoris Brezillon irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 39493db446aSBoris Brezillon if (!irq) { 39593db446aSBoris Brezillon dev_err(&pdev->dev, "no irq resource defined\n"); 39693db446aSBoris Brezillon return -ENODEV; 39793db446aSBoris Brezillon } 39893db446aSBoris Brezillon 39993db446aSBoris Brezillon res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 40093db446aSBoris Brezillon info->elm_base = devm_ioremap_resource(&pdev->dev, res); 40193db446aSBoris Brezillon if (IS_ERR(info->elm_base)) 40293db446aSBoris Brezillon return PTR_ERR(info->elm_base); 40393db446aSBoris Brezillon 40493db446aSBoris Brezillon ret = devm_request_irq(&pdev->dev, irq->start, elm_isr, 0, 40593db446aSBoris Brezillon pdev->name, info); 40693db446aSBoris Brezillon if (ret) { 40793db446aSBoris Brezillon dev_err(&pdev->dev, "failure requesting %pr\n", irq); 40893db446aSBoris Brezillon return ret; 40993db446aSBoris Brezillon } 41093db446aSBoris Brezillon 41193db446aSBoris Brezillon pm_runtime_enable(&pdev->dev); 41293db446aSBoris Brezillon if (pm_runtime_get_sync(&pdev->dev) < 0) { 41393db446aSBoris Brezillon ret = -EINVAL; 414*37f72121SDinghao Liu pm_runtime_put_sync(&pdev->dev); 41593db446aSBoris Brezillon pm_runtime_disable(&pdev->dev); 41693db446aSBoris Brezillon dev_err(&pdev->dev, "can't enable clock\n"); 41793db446aSBoris Brezillon return ret; 41893db446aSBoris Brezillon } 41993db446aSBoris Brezillon 42093db446aSBoris Brezillon init_completion(&info->elm_completion); 42193db446aSBoris Brezillon INIT_LIST_HEAD(&info->list); 42293db446aSBoris Brezillon list_add(&info->list, &elm_devices); 42393db446aSBoris Brezillon platform_set_drvdata(pdev, info); 42493db446aSBoris Brezillon return ret; 42593db446aSBoris Brezillon } 42693db446aSBoris Brezillon 42793db446aSBoris Brezillon static int elm_remove(struct platform_device *pdev) 42893db446aSBoris Brezillon { 42993db446aSBoris Brezillon pm_runtime_put_sync(&pdev->dev); 43093db446aSBoris Brezillon pm_runtime_disable(&pdev->dev); 43193db446aSBoris Brezillon return 0; 43293db446aSBoris Brezillon } 43393db446aSBoris Brezillon 43493db446aSBoris Brezillon #ifdef CONFIG_PM_SLEEP 43593db446aSBoris Brezillon /** 43693db446aSBoris Brezillon * elm_context_save 43793db446aSBoris Brezillon * saves ELM configurations to preserve them across Hardware powered-down 43893db446aSBoris Brezillon */ 43993db446aSBoris Brezillon static int elm_context_save(struct elm_info *info) 44093db446aSBoris Brezillon { 44193db446aSBoris Brezillon struct elm_registers *regs = &info->elm_regs; 44293db446aSBoris Brezillon enum bch_ecc bch_type = info->bch_type; 44393db446aSBoris Brezillon u32 offset = 0, i; 44493db446aSBoris Brezillon 44593db446aSBoris Brezillon regs->elm_irqenable = elm_read_reg(info, ELM_IRQENABLE); 44693db446aSBoris Brezillon regs->elm_sysconfig = elm_read_reg(info, ELM_SYSCONFIG); 44793db446aSBoris Brezillon regs->elm_location_config = elm_read_reg(info, ELM_LOCATION_CONFIG); 44893db446aSBoris Brezillon regs->elm_page_ctrl = elm_read_reg(info, ELM_PAGE_CTRL); 44993db446aSBoris Brezillon for (i = 0; i < ERROR_VECTOR_MAX; i++) { 45093db446aSBoris Brezillon offset = i * SYNDROME_FRAGMENT_REG_SIZE; 45193db446aSBoris Brezillon switch (bch_type) { 45293db446aSBoris Brezillon case BCH16_ECC: 45393db446aSBoris Brezillon regs->elm_syndrome_fragment_6[i] = elm_read_reg(info, 45493db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_6 + offset); 45593db446aSBoris Brezillon regs->elm_syndrome_fragment_5[i] = elm_read_reg(info, 45693db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_5 + offset); 45793db446aSBoris Brezillon regs->elm_syndrome_fragment_4[i] = elm_read_reg(info, 45893db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_4 + offset); 459025a06c1SMiquel Raynal fallthrough; 46093db446aSBoris Brezillon case BCH8_ECC: 46193db446aSBoris Brezillon regs->elm_syndrome_fragment_3[i] = elm_read_reg(info, 46293db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_3 + offset); 46393db446aSBoris Brezillon regs->elm_syndrome_fragment_2[i] = elm_read_reg(info, 46493db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_2 + offset); 465025a06c1SMiquel Raynal fallthrough; 46693db446aSBoris Brezillon case BCH4_ECC: 46793db446aSBoris Brezillon regs->elm_syndrome_fragment_1[i] = elm_read_reg(info, 46893db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_1 + offset); 46993db446aSBoris Brezillon regs->elm_syndrome_fragment_0[i] = elm_read_reg(info, 47093db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_0 + offset); 47193db446aSBoris Brezillon break; 47293db446aSBoris Brezillon default: 47393db446aSBoris Brezillon return -EINVAL; 47493db446aSBoris Brezillon } 47593db446aSBoris Brezillon /* ELM SYNDROME_VALID bit in SYNDROME_FRAGMENT_6[] needs 47693db446aSBoris Brezillon * to be saved for all BCH schemes*/ 47793db446aSBoris Brezillon regs->elm_syndrome_fragment_6[i] = elm_read_reg(info, 47893db446aSBoris Brezillon ELM_SYNDROME_FRAGMENT_6 + offset); 47993db446aSBoris Brezillon } 48093db446aSBoris Brezillon return 0; 48193db446aSBoris Brezillon } 48293db446aSBoris Brezillon 48393db446aSBoris Brezillon /** 48493db446aSBoris Brezillon * elm_context_restore 48593db446aSBoris Brezillon * writes configurations saved duing power-down back into ELM registers 48693db446aSBoris Brezillon */ 48793db446aSBoris Brezillon static int elm_context_restore(struct elm_info *info) 48893db446aSBoris Brezillon { 48993db446aSBoris Brezillon struct elm_registers *regs = &info->elm_regs; 49093db446aSBoris Brezillon enum bch_ecc bch_type = info->bch_type; 49193db446aSBoris Brezillon u32 offset = 0, i; 49293db446aSBoris Brezillon 49393db446aSBoris Brezillon elm_write_reg(info, ELM_IRQENABLE, regs->elm_irqenable); 49493db446aSBoris Brezillon elm_write_reg(info, ELM_SYSCONFIG, regs->elm_sysconfig); 49593db446aSBoris Brezillon elm_write_reg(info, ELM_LOCATION_CONFIG, regs->elm_location_config); 49693db446aSBoris Brezillon elm_write_reg(info, ELM_PAGE_CTRL, regs->elm_page_ctrl); 49793db446aSBoris Brezillon for (i = 0; i < ERROR_VECTOR_MAX; i++) { 49893db446aSBoris Brezillon offset = i * SYNDROME_FRAGMENT_REG_SIZE; 49993db446aSBoris Brezillon switch (bch_type) { 50093db446aSBoris Brezillon case BCH16_ECC: 50193db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_6 + offset, 50293db446aSBoris Brezillon regs->elm_syndrome_fragment_6[i]); 50393db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_5 + offset, 50493db446aSBoris Brezillon regs->elm_syndrome_fragment_5[i]); 50593db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_4 + offset, 50693db446aSBoris Brezillon regs->elm_syndrome_fragment_4[i]); 507025a06c1SMiquel Raynal fallthrough; 50893db446aSBoris Brezillon case BCH8_ECC: 50993db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_3 + offset, 51093db446aSBoris Brezillon regs->elm_syndrome_fragment_3[i]); 51193db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_2 + offset, 51293db446aSBoris Brezillon regs->elm_syndrome_fragment_2[i]); 513025a06c1SMiquel Raynal fallthrough; 51493db446aSBoris Brezillon case BCH4_ECC: 51593db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_1 + offset, 51693db446aSBoris Brezillon regs->elm_syndrome_fragment_1[i]); 51793db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_0 + offset, 51893db446aSBoris Brezillon regs->elm_syndrome_fragment_0[i]); 51993db446aSBoris Brezillon break; 52093db446aSBoris Brezillon default: 52193db446aSBoris Brezillon return -EINVAL; 52293db446aSBoris Brezillon } 52393db446aSBoris Brezillon /* ELM_SYNDROME_VALID bit to be set in last to trigger FSM */ 52493db446aSBoris Brezillon elm_write_reg(info, ELM_SYNDROME_FRAGMENT_6 + offset, 52593db446aSBoris Brezillon regs->elm_syndrome_fragment_6[i] & 52693db446aSBoris Brezillon ELM_SYNDROME_VALID); 52793db446aSBoris Brezillon } 52893db446aSBoris Brezillon return 0; 52993db446aSBoris Brezillon } 53093db446aSBoris Brezillon 53193db446aSBoris Brezillon static int elm_suspend(struct device *dev) 53293db446aSBoris Brezillon { 53393db446aSBoris Brezillon struct elm_info *info = dev_get_drvdata(dev); 53493db446aSBoris Brezillon elm_context_save(info); 53593db446aSBoris Brezillon pm_runtime_put_sync(dev); 53693db446aSBoris Brezillon return 0; 53793db446aSBoris Brezillon } 53893db446aSBoris Brezillon 53993db446aSBoris Brezillon static int elm_resume(struct device *dev) 54093db446aSBoris Brezillon { 54193db446aSBoris Brezillon struct elm_info *info = dev_get_drvdata(dev); 54293db446aSBoris Brezillon pm_runtime_get_sync(dev); 54393db446aSBoris Brezillon elm_context_restore(info); 54493db446aSBoris Brezillon return 0; 54593db446aSBoris Brezillon } 54693db446aSBoris Brezillon #endif 54793db446aSBoris Brezillon 54893db446aSBoris Brezillon static SIMPLE_DEV_PM_OPS(elm_pm_ops, elm_suspend, elm_resume); 54993db446aSBoris Brezillon 55093db446aSBoris Brezillon #ifdef CONFIG_OF 55193db446aSBoris Brezillon static const struct of_device_id elm_of_match[] = { 55293db446aSBoris Brezillon { .compatible = "ti,am3352-elm" }, 55393db446aSBoris Brezillon {}, 55493db446aSBoris Brezillon }; 55593db446aSBoris Brezillon MODULE_DEVICE_TABLE(of, elm_of_match); 55693db446aSBoris Brezillon #endif 55793db446aSBoris Brezillon 55893db446aSBoris Brezillon static struct platform_driver elm_driver = { 55993db446aSBoris Brezillon .driver = { 56093db446aSBoris Brezillon .name = DRIVER_NAME, 56193db446aSBoris Brezillon .of_match_table = of_match_ptr(elm_of_match), 56293db446aSBoris Brezillon .pm = &elm_pm_ops, 56393db446aSBoris Brezillon }, 56493db446aSBoris Brezillon .probe = elm_probe, 56593db446aSBoris Brezillon .remove = elm_remove, 56693db446aSBoris Brezillon }; 56793db446aSBoris Brezillon 56893db446aSBoris Brezillon module_platform_driver(elm_driver); 56993db446aSBoris Brezillon 57093db446aSBoris Brezillon MODULE_DESCRIPTION("ELM driver for BCH error correction"); 57193db446aSBoris Brezillon MODULE_AUTHOR("Texas Instruments"); 57293db446aSBoris Brezillon MODULE_ALIAS("platform:" DRIVER_NAME); 57393db446aSBoris Brezillon MODULE_LICENSE("GPL v2"); 574