blob: 35c6dd1f1bcc3a9dc45dcf568d42efcb2e85a798 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mansoor Ahamed04c37572012-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 Ahamed04c37572012-11-06 13:06:32 +000013 */
14
15#include <common.h>
16#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090017#include <linux/errno.h>
pekon gupta2eda8922013-11-22 16:53:30 +053018#include <linux/mtd/omap_elm.h>
pekon gupta51d192c2013-11-22 16:53:28 +053019#include <asm/arch/hardware.h>
Mansoor Ahamed04c37572012-11-06 13:06:32 +000020
pekon gupta3f990dc2014-04-11 12:55:35 +053021#define DRIVER_NAME "omap-elm"
Mansoor Ahamed04c37572012-11-06 13:06:32 +000022#define ELM_DEFAULT_POLY (0)
23
24struct elm *elm_cfg;
25
26/**
pekon gupta41bbe4d2014-04-11 12:55:30 +053027 * elm_load_syndromes - Load BCH syndromes based on bch_type selection
Mansoor Ahamed04c37572012-11-06 13:06:32 +000028 * @syndrome: BCH syndrome
pekon gupta41bbe4d2014-04-11 12:55:30 +053029 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed04c37572012-11-06 13:06:32 +000030 * @poly: Syndrome Polynomial set to use
Mansoor Ahamed04c37572012-11-06 13:06:32 +000031 */
pekon gupta41bbe4d2014-04-11 12:55:30 +053032static void elm_load_syndromes(u8 *syndrome, enum bch_level bch_type, u8 poly)
Mansoor Ahamed04c37572012-11-06 13:06:32 +000033{
34 u32 *ptr;
35 u32 val;
36
37 /* reg 0 */
38 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
39 val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
40 (syndrome[3] << 24);
41 writel(val, ptr);
42 /* reg 1 */
43 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
44 val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
45 (syndrome[7] << 24);
46 writel(val, ptr);
47
pekon gupta41bbe4d2014-04-11 12:55:30 +053048 if (bch_type == BCH_8_BIT || bch_type == BCH_16_BIT) {
Mansoor Ahamed04c37572012-11-06 13:06:32 +000049 /* reg 2 */
50 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
51 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
52 (syndrome[11] << 24);
53 writel(val, ptr);
54 /* reg 3 */
55 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
56 val = syndrome[12] | (syndrome[13] << 8) |
57 (syndrome[14] << 16) | (syndrome[15] << 24);
58 writel(val, ptr);
59 }
60
pekon gupta41bbe4d2014-04-11 12:55:30 +053061 if (bch_type == BCH_16_BIT) {
Mansoor Ahamed04c37572012-11-06 13:06:32 +000062 /* reg 4 */
63 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
64 val = syndrome[16] | (syndrome[17] << 8) |
65 (syndrome[18] << 16) | (syndrome[19] << 24);
66 writel(val, ptr);
67
68 /* reg 5 */
69 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
70 val = syndrome[20] | (syndrome[21] << 8) |
71 (syndrome[22] << 16) | (syndrome[23] << 24);
72 writel(val, ptr);
73
74 /* reg 6 */
75 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
76 val = syndrome[24] | (syndrome[25] << 8) |
77 (syndrome[26] << 16) | (syndrome[27] << 24);
78 writel(val, ptr);
79 }
80}
81
82/**
83 * elm_check_errors - Check for BCH errors and return error locations
84 * @syndrome: BCH syndrome
pekon gupta41bbe4d2014-04-11 12:55:30 +053085 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed04c37572012-11-06 13:06:32 +000086 * @error_count: Returns number of errrors in the syndrome
87 * @error_locations: Returns error locations (in decimal) in this array
88 *
89 * Check the provided syndrome for BCH errors and return error count
90 * and locations in the array passed. Returns -1 if error is not correctable,
91 * else returns 0
92 */
pekon gupta41bbe4d2014-04-11 12:55:30 +053093int elm_check_error(u8 *syndrome, enum bch_level bch_type, u32 *error_count,
Mansoor Ahamed04c37572012-11-06 13:06:32 +000094 u32 *error_locations)
95{
96 u8 poly = ELM_DEFAULT_POLY;
97 s8 i;
98 u32 location_status;
99
pekon gupta41bbe4d2014-04-11 12:55:30 +0530100 elm_load_syndromes(syndrome, bch_type, poly);
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000101
102 /* start processing */
103 writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
104 | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
105 &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
106
107 /* wait for processing to complete */
108 while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
109 ;
110 /* clear status */
111 writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
112 &elm_cfg->irqstatus);
113
114 /* check if correctable */
115 location_status = readl(&elm_cfg->error_location[poly].location_status);
pekon gupta3f990dc2014-04-11 12:55:35 +0530116 if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK)) {
117 printf("%s: uncorrectable ECC errors\n", DRIVER_NAME);
118 return -EBADMSG;
119 }
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000120
121 /* get error count */
122 *error_count = readl(&elm_cfg->error_location[poly].location_status) &
123 ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
124
125 for (i = 0; i < *error_count; i++) {
126 error_locations[i] =
pekon guptabeba5f02013-11-18 19:02:59 +0530127 readl(&elm_cfg->error_location[poly].error_location_x[i]);
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000128 }
129
130 return 0;
131}
132
133
134/**
135 * elm_config - Configure ELM module
136 * @level: 4 / 8 / 16 bit BCH
137 *
138 * Configure ELM module based on BCH level.
139 * Set mode as continuous mode.
140 * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
141 * Also, the mode is set only for syndrome 0
142 */
143int elm_config(enum bch_level level)
144{
145 u32 val;
146 u8 poly = ELM_DEFAULT_POLY;
147 u32 buffer_size = 0x7FF;
148
149 /* config size and level */
150 val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
151 val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
152 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
153 writel(val, &elm_cfg->location_config);
154
155 /* config continous mode */
156 /* enable interrupt generation for syndrome polynomial set */
157 writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
158 &elm_cfg->irqenable);
159 /* set continuous mode for the syndrome polynomial set */
160 writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
161 &elm_cfg->page_ctrl);
162
163 return 0;
164}
165
166/**
167 * elm_reset - Do a soft reset of ELM
168 *
169 * Perform a soft reset of ELM and return after reset is done.
170 */
171void elm_reset(void)
172{
173 /* initiate reset */
174 writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
pekon guptabeba5f02013-11-18 19:02:59 +0530175 &elm_cfg->sysconfig);
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000176
177 /* wait for reset complete and normal operation */
178 while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
179 ELM_SYSSTATUS_RESETDONE)
180 ;
181}
182
183/**
184 * elm_init - Initialize ELM module
185 *
186 * Initialize ELM support. Currently it does only base address init
187 * and ELM reset.
188 */
189void elm_init(void)
190{
191 elm_cfg = (struct elm *)ELM_BASE;
192 elm_reset();
193}