blob: 61751b9ae2db043106178eccc29cca10cc5d80d5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mansoor Ahamed59e38b42012-11-06 13:06:32 +00002/*
3 * (C) Copyright 2010-2011 Texas Instruments, <www.ti.com>
4 * Mansoor Ahamed <mansoor.ahamed@ti.com>
5 *
6 * BCH Error Location Module (ELM) support.
7 *
8 * NOTE:
9 * 1. Supports only continuous mode. Dont see need for page mode in uboot
10 * 2. Supports only syndrome polynomial 0. i.e. poly local variable is
11 * always set to ELM_DEFAULT_POLY. Dont see need for other polynomial
12 * sets in uboot
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000013 */
14
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000015#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
pekon guptac77f2842013-11-22 16:53:28 +053017#include <asm/arch/hardware.h>
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000018
Roger Quadrosa42b7702022-12-20 12:22:03 +020019#include <dm.h>
20#include <linux/ioport.h>
21#include <linux/io.h>
22
23#include "omap_elm.h"
24
pekon guptacfe6b8a2014-04-11 12:55:35 +053025#define DRIVER_NAME "omap-elm"
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000026#define ELM_DEFAULT_POLY (0)
27
28struct elm *elm_cfg;
29
30/**
pekon guptadcd24112014-04-11 12:55:30 +053031 * elm_load_syndromes - Load BCH syndromes based on bch_type selection
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000032 * @syndrome: BCH syndrome
pekon guptadcd24112014-04-11 12:55:30 +053033 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000034 * @poly: Syndrome Polynomial set to use
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000035 */
pekon guptadcd24112014-04-11 12:55:30 +053036static void elm_load_syndromes(u8 *syndrome, enum bch_level bch_type, u8 poly)
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000037{
38 u32 *ptr;
39 u32 val;
40
41 /* reg 0 */
42 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
43 val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
44 (syndrome[3] << 24);
45 writel(val, ptr);
46 /* reg 1 */
47 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
48 val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
49 (syndrome[7] << 24);
50 writel(val, ptr);
51
pekon guptadcd24112014-04-11 12:55:30 +053052 if (bch_type == BCH_8_BIT || bch_type == BCH_16_BIT) {
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000053 /* reg 2 */
54 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
55 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
56 (syndrome[11] << 24);
57 writel(val, ptr);
58 /* reg 3 */
59 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
60 val = syndrome[12] | (syndrome[13] << 8) |
61 (syndrome[14] << 16) | (syndrome[15] << 24);
62 writel(val, ptr);
63 }
64
pekon guptadcd24112014-04-11 12:55:30 +053065 if (bch_type == BCH_16_BIT) {
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000066 /* reg 4 */
67 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
68 val = syndrome[16] | (syndrome[17] << 8) |
69 (syndrome[18] << 16) | (syndrome[19] << 24);
70 writel(val, ptr);
71
72 /* reg 5 */
73 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
74 val = syndrome[20] | (syndrome[21] << 8) |
75 (syndrome[22] << 16) | (syndrome[23] << 24);
76 writel(val, ptr);
77
78 /* reg 6 */
79 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
80 val = syndrome[24] | (syndrome[25] << 8) |
81 (syndrome[26] << 16) | (syndrome[27] << 24);
82 writel(val, ptr);
83 }
84}
85
86/**
87 * elm_check_errors - Check for BCH errors and return error locations
88 * @syndrome: BCH syndrome
pekon guptadcd24112014-04-11 12:55:30 +053089 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000090 * @error_count: Returns number of errrors in the syndrome
91 * @error_locations: Returns error locations (in decimal) in this array
92 *
93 * Check the provided syndrome for BCH errors and return error count
94 * and locations in the array passed. Returns -1 if error is not correctable,
95 * else returns 0
96 */
pekon guptadcd24112014-04-11 12:55:30 +053097int elm_check_error(u8 *syndrome, enum bch_level bch_type, u32 *error_count,
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000098 u32 *error_locations)
99{
100 u8 poly = ELM_DEFAULT_POLY;
101 s8 i;
102 u32 location_status;
103
pekon guptadcd24112014-04-11 12:55:30 +0530104 elm_load_syndromes(syndrome, bch_type, poly);
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000105
106 /* start processing */
107 writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
108 | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
109 &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
110
111 /* wait for processing to complete */
112 while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
113 ;
114 /* clear status */
115 writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
116 &elm_cfg->irqstatus);
117
118 /* check if correctable */
119 location_status = readl(&elm_cfg->error_location[poly].location_status);
pekon guptacfe6b8a2014-04-11 12:55:35 +0530120 if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK)) {
121 printf("%s: uncorrectable ECC errors\n", DRIVER_NAME);
122 return -EBADMSG;
123 }
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000124
125 /* get error count */
126 *error_count = readl(&elm_cfg->error_location[poly].location_status) &
127 ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
128
129 for (i = 0; i < *error_count; i++) {
130 error_locations[i] =
pekon gupta89ad1dc2013-11-18 19:02:59 +0530131 readl(&elm_cfg->error_location[poly].error_location_x[i]);
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000132 }
133
134 return 0;
135}
136
137
138/**
139 * elm_config - Configure ELM module
140 * @level: 4 / 8 / 16 bit BCH
141 *
142 * Configure ELM module based on BCH level.
143 * Set mode as continuous mode.
144 * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
145 * Also, the mode is set only for syndrome 0
146 */
147int elm_config(enum bch_level level)
148{
149 u32 val;
150 u8 poly = ELM_DEFAULT_POLY;
151 u32 buffer_size = 0x7FF;
152
153 /* config size and level */
154 val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
155 val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
156 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
157 writel(val, &elm_cfg->location_config);
158
159 /* config continous mode */
160 /* enable interrupt generation for syndrome polynomial set */
161 writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
162 &elm_cfg->irqenable);
163 /* set continuous mode for the syndrome polynomial set */
164 writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
165 &elm_cfg->page_ctrl);
166
167 return 0;
168}
169
170/**
171 * elm_reset - Do a soft reset of ELM
172 *
173 * Perform a soft reset of ELM and return after reset is done.
174 */
175void elm_reset(void)
176{
177 /* initiate reset */
178 writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
pekon gupta89ad1dc2013-11-18 19:02:59 +0530179 &elm_cfg->sysconfig);
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000180
181 /* wait for reset complete and normal operation */
182 while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
183 ELM_SYSSTATUS_RESETDONE)
184 ;
185}
186
187/**
188 * elm_init - Initialize ELM module
189 *
190 * Initialize ELM support. Currently it does only base address init
191 * and ELM reset.
192 */
193void elm_init(void)
194{
Roger Quadros1c82a232023-12-11 13:46:00 +0200195#ifdef ELM_BASE
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000196 elm_cfg = (struct elm *)ELM_BASE;
197 elm_reset();
Roger Quadrosa42b7702022-12-20 12:22:03 +0200198#endif
Roger Quadros1c82a232023-12-11 13:46:00 +0200199}
Roger Quadrosa42b7702022-12-20 12:22:03 +0200200
201#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
202
203static int elm_probe(struct udevice *dev)
204{
205#ifndef ELM_BASE
206 struct resource res;
207
208 dev_read_resource(dev, 0, &res);
209 elm_cfg = devm_ioremap(dev, res.start, resource_size(&res));
210 elm_reset();
211#endif
212
213 return 0;
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000214}
Roger Quadrosa42b7702022-12-20 12:22:03 +0200215
216static const struct udevice_id elm_ids[] = {
217 { .compatible = "ti,am3352-elm" },
218 { .compatible = "ti,am64-elm" },
219 { }
220};
221
222U_BOOT_DRIVER(gpmc_elm) = {
223 .name = DRIVER_NAME,
224 .id = UCLASS_MTD,
225 .of_match = elm_ids,
226 .probe = elm_probe,
227};
228#endif /* CONFIG_SYS_NAND_SELF_INIT */