blob: 56a2c39e4f63894b25c5dac3c82f1ccec82d5213 [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
15#include <common.h>
16#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090017#include <linux/errno.h>
pekon guptac77f2842013-11-22 16:53:28 +053018#include <asm/arch/hardware.h>
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000019
Roger Quadrosa42b7702022-12-20 12:22:03 +020020#include <dm.h>
21#include <linux/ioport.h>
22#include <linux/io.h>
23
24#include "omap_elm.h"
25
pekon guptacfe6b8a2014-04-11 12:55:35 +053026#define DRIVER_NAME "omap-elm"
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000027#define ELM_DEFAULT_POLY (0)
28
29struct elm *elm_cfg;
30
31/**
pekon guptadcd24112014-04-11 12:55:30 +053032 * elm_load_syndromes - Load BCH syndromes based on bch_type selection
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000033 * @syndrome: BCH syndrome
pekon guptadcd24112014-04-11 12:55:30 +053034 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000035 * @poly: Syndrome Polynomial set to use
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000036 */
pekon guptadcd24112014-04-11 12:55:30 +053037static void elm_load_syndromes(u8 *syndrome, enum bch_level bch_type, u8 poly)
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000038{
39 u32 *ptr;
40 u32 val;
41
42 /* reg 0 */
43 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
44 val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
45 (syndrome[3] << 24);
46 writel(val, ptr);
47 /* reg 1 */
48 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
49 val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
50 (syndrome[7] << 24);
51 writel(val, ptr);
52
pekon guptadcd24112014-04-11 12:55:30 +053053 if (bch_type == BCH_8_BIT || bch_type == BCH_16_BIT) {
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000054 /* reg 2 */
55 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
56 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
57 (syndrome[11] << 24);
58 writel(val, ptr);
59 /* reg 3 */
60 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
61 val = syndrome[12] | (syndrome[13] << 8) |
62 (syndrome[14] << 16) | (syndrome[15] << 24);
63 writel(val, ptr);
64 }
65
pekon guptadcd24112014-04-11 12:55:30 +053066 if (bch_type == BCH_16_BIT) {
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000067 /* reg 4 */
68 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
69 val = syndrome[16] | (syndrome[17] << 8) |
70 (syndrome[18] << 16) | (syndrome[19] << 24);
71 writel(val, ptr);
72
73 /* reg 5 */
74 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
75 val = syndrome[20] | (syndrome[21] << 8) |
76 (syndrome[22] << 16) | (syndrome[23] << 24);
77 writel(val, ptr);
78
79 /* reg 6 */
80 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
81 val = syndrome[24] | (syndrome[25] << 8) |
82 (syndrome[26] << 16) | (syndrome[27] << 24);
83 writel(val, ptr);
84 }
85}
86
87/**
88 * elm_check_errors - Check for BCH errors and return error locations
89 * @syndrome: BCH syndrome
pekon guptadcd24112014-04-11 12:55:30 +053090 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000091 * @error_count: Returns number of errrors in the syndrome
92 * @error_locations: Returns error locations (in decimal) in this array
93 *
94 * Check the provided syndrome for BCH errors and return error count
95 * and locations in the array passed. Returns -1 if error is not correctable,
96 * else returns 0
97 */
pekon guptadcd24112014-04-11 12:55:30 +053098int elm_check_error(u8 *syndrome, enum bch_level bch_type, u32 *error_count,
Mansoor Ahamed59e38b42012-11-06 13:06:32 +000099 u32 *error_locations)
100{
101 u8 poly = ELM_DEFAULT_POLY;
102 s8 i;
103 u32 location_status;
104
pekon guptadcd24112014-04-11 12:55:30 +0530105 elm_load_syndromes(syndrome, bch_type, poly);
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000106
107 /* start processing */
108 writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
109 | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
110 &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
111
112 /* wait for processing to complete */
113 while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
114 ;
115 /* clear status */
116 writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
117 &elm_cfg->irqstatus);
118
119 /* check if correctable */
120 location_status = readl(&elm_cfg->error_location[poly].location_status);
pekon guptacfe6b8a2014-04-11 12:55:35 +0530121 if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK)) {
122 printf("%s: uncorrectable ECC errors\n", DRIVER_NAME);
123 return -EBADMSG;
124 }
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000125
126 /* get error count */
127 *error_count = readl(&elm_cfg->error_location[poly].location_status) &
128 ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
129
130 for (i = 0; i < *error_count; i++) {
131 error_locations[i] =
pekon gupta89ad1dc2013-11-18 19:02:59 +0530132 readl(&elm_cfg->error_location[poly].error_location_x[i]);
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000133 }
134
135 return 0;
136}
137
138
139/**
140 * elm_config - Configure ELM module
141 * @level: 4 / 8 / 16 bit BCH
142 *
143 * Configure ELM module based on BCH level.
144 * Set mode as continuous mode.
145 * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
146 * Also, the mode is set only for syndrome 0
147 */
148int elm_config(enum bch_level level)
149{
150 u32 val;
151 u8 poly = ELM_DEFAULT_POLY;
152 u32 buffer_size = 0x7FF;
153
154 /* config size and level */
155 val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
156 val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
157 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
158 writel(val, &elm_cfg->location_config);
159
160 /* config continous mode */
161 /* enable interrupt generation for syndrome polynomial set */
162 writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
163 &elm_cfg->irqenable);
164 /* set continuous mode for the syndrome polynomial set */
165 writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
166 &elm_cfg->page_ctrl);
167
168 return 0;
169}
170
171/**
172 * elm_reset - Do a soft reset of ELM
173 *
174 * Perform a soft reset of ELM and return after reset is done.
175 */
176void elm_reset(void)
177{
178 /* initiate reset */
179 writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
pekon gupta89ad1dc2013-11-18 19:02:59 +0530180 &elm_cfg->sysconfig);
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000181
182 /* wait for reset complete and normal operation */
183 while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
184 ELM_SYSSTATUS_RESETDONE)
185 ;
186}
187
Roger Quadrosa42b7702022-12-20 12:22:03 +0200188#ifdef ELM_BASE
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000189/**
190 * elm_init - Initialize ELM module
191 *
192 * Initialize ELM support. Currently it does only base address init
193 * and ELM reset.
194 */
195void elm_init(void)
196{
197 elm_cfg = (struct elm *)ELM_BASE;
198 elm_reset();
Roger Quadrosa42b7702022-12-20 12:22:03 +0200199}
200#endif
201
202#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
203
204static int elm_probe(struct udevice *dev)
205{
206#ifndef ELM_BASE
207 struct resource res;
208
209 dev_read_resource(dev, 0, &res);
210 elm_cfg = devm_ioremap(dev, res.start, resource_size(&res));
211 elm_reset();
212#endif
213
214 return 0;
Mansoor Ahamed59e38b42012-11-06 13:06:32 +0000215}
Roger Quadrosa42b7702022-12-20 12:22:03 +0200216
217static const struct udevice_id elm_ids[] = {
218 { .compatible = "ti,am3352-elm" },
219 { .compatible = "ti,am64-elm" },
220 { }
221};
222
223U_BOOT_DRIVER(gpmc_elm) = {
224 .name = DRIVER_NAME,
225 .id = UCLASS_MTD,
226 .of_match = elm_ids,
227 .probe = elm_probe,
228};
229#endif /* CONFIG_SYS_NAND_SELF_INIT */