blob: 9ef1b2169e03999ffebbd768d4146c7a411c117f [file] [log] [blame]
Mansoor Ahamed04c37572012-11-06 13:06:32 +00001/*
2 * (C) Copyright 2010-2011 Texas Instruments, <www.ti.com>
3 * Mansoor Ahamed <mansoor.ahamed@ti.com>
4 *
5 * BCH Error Location Module (ELM) support.
6 *
7 * NOTE:
8 * 1. Supports only continuous mode. Dont see need for page mode in uboot
9 * 2. Supports only syndrome polynomial 0. i.e. poly local variable is
10 * always set to ELM_DEFAULT_POLY. Dont see need for other polynomial
11 * sets in uboot
12 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020013 * SPDX-License-Identifier: GPL-2.0+
Mansoor Ahamed04c37572012-11-06 13:06:32 +000014 */
15
16#include <common.h>
17#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090018#include <linux/errno.h>
pekon gupta2eda8922013-11-22 16:53:30 +053019#include <linux/mtd/omap_elm.h>
pekon gupta51d192c2013-11-22 16:53:28 +053020#include <asm/arch/hardware.h>
Mansoor Ahamed04c37572012-11-06 13:06:32 +000021
pekon gupta3f990dc2014-04-11 12:55:35 +053022#define DRIVER_NAME "omap-elm"
Mansoor Ahamed04c37572012-11-06 13:06:32 +000023#define ELM_DEFAULT_POLY (0)
24
25struct elm *elm_cfg;
26
27/**
pekon gupta41bbe4d2014-04-11 12:55:30 +053028 * elm_load_syndromes - Load BCH syndromes based on bch_type selection
Mansoor Ahamed04c37572012-11-06 13:06:32 +000029 * @syndrome: BCH syndrome
pekon gupta41bbe4d2014-04-11 12:55:30 +053030 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed04c37572012-11-06 13:06:32 +000031 * @poly: Syndrome Polynomial set to use
Mansoor Ahamed04c37572012-11-06 13:06:32 +000032 */
pekon gupta41bbe4d2014-04-11 12:55:30 +053033static void elm_load_syndromes(u8 *syndrome, enum bch_level bch_type, u8 poly)
Mansoor Ahamed04c37572012-11-06 13:06:32 +000034{
35 u32 *ptr;
36 u32 val;
37
38 /* reg 0 */
39 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
40 val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
41 (syndrome[3] << 24);
42 writel(val, ptr);
43 /* reg 1 */
44 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
45 val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
46 (syndrome[7] << 24);
47 writel(val, ptr);
48
pekon gupta41bbe4d2014-04-11 12:55:30 +053049 if (bch_type == BCH_8_BIT || bch_type == BCH_16_BIT) {
Mansoor Ahamed04c37572012-11-06 13:06:32 +000050 /* reg 2 */
51 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
52 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
53 (syndrome[11] << 24);
54 writel(val, ptr);
55 /* reg 3 */
56 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
57 val = syndrome[12] | (syndrome[13] << 8) |
58 (syndrome[14] << 16) | (syndrome[15] << 24);
59 writel(val, ptr);
60 }
61
pekon gupta41bbe4d2014-04-11 12:55:30 +053062 if (bch_type == BCH_16_BIT) {
Mansoor Ahamed04c37572012-11-06 13:06:32 +000063 /* reg 4 */
64 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
65 val = syndrome[16] | (syndrome[17] << 8) |
66 (syndrome[18] << 16) | (syndrome[19] << 24);
67 writel(val, ptr);
68
69 /* reg 5 */
70 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
71 val = syndrome[20] | (syndrome[21] << 8) |
72 (syndrome[22] << 16) | (syndrome[23] << 24);
73 writel(val, ptr);
74
75 /* reg 6 */
76 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
77 val = syndrome[24] | (syndrome[25] << 8) |
78 (syndrome[26] << 16) | (syndrome[27] << 24);
79 writel(val, ptr);
80 }
81}
82
83/**
84 * elm_check_errors - Check for BCH errors and return error locations
85 * @syndrome: BCH syndrome
pekon gupta41bbe4d2014-04-11 12:55:30 +053086 * @bch_type: BCH4/BCH8/BCH16
Mansoor Ahamed04c37572012-11-06 13:06:32 +000087 * @error_count: Returns number of errrors in the syndrome
88 * @error_locations: Returns error locations (in decimal) in this array
89 *
90 * Check the provided syndrome for BCH errors and return error count
91 * and locations in the array passed. Returns -1 if error is not correctable,
92 * else returns 0
93 */
pekon gupta41bbe4d2014-04-11 12:55:30 +053094int elm_check_error(u8 *syndrome, enum bch_level bch_type, u32 *error_count,
Mansoor Ahamed04c37572012-11-06 13:06:32 +000095 u32 *error_locations)
96{
97 u8 poly = ELM_DEFAULT_POLY;
98 s8 i;
99 u32 location_status;
100
pekon gupta41bbe4d2014-04-11 12:55:30 +0530101 elm_load_syndromes(syndrome, bch_type, poly);
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000102
103 /* start processing */
104 writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
105 | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
106 &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
107
108 /* wait for processing to complete */
109 while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
110 ;
111 /* clear status */
112 writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
113 &elm_cfg->irqstatus);
114
115 /* check if correctable */
116 location_status = readl(&elm_cfg->error_location[poly].location_status);
pekon gupta3f990dc2014-04-11 12:55:35 +0530117 if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK)) {
118 printf("%s: uncorrectable ECC errors\n", DRIVER_NAME);
119 return -EBADMSG;
120 }
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000121
122 /* get error count */
123 *error_count = readl(&elm_cfg->error_location[poly].location_status) &
124 ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
125
126 for (i = 0; i < *error_count; i++) {
127 error_locations[i] =
pekon guptabeba5f02013-11-18 19:02:59 +0530128 readl(&elm_cfg->error_location[poly].error_location_x[i]);
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000129 }
130
131 return 0;
132}
133
134
135/**
136 * elm_config - Configure ELM module
137 * @level: 4 / 8 / 16 bit BCH
138 *
139 * Configure ELM module based on BCH level.
140 * Set mode as continuous mode.
141 * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
142 * Also, the mode is set only for syndrome 0
143 */
144int elm_config(enum bch_level level)
145{
146 u32 val;
147 u8 poly = ELM_DEFAULT_POLY;
148 u32 buffer_size = 0x7FF;
149
150 /* config size and level */
151 val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
152 val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
153 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
154 writel(val, &elm_cfg->location_config);
155
156 /* config continous mode */
157 /* enable interrupt generation for syndrome polynomial set */
158 writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
159 &elm_cfg->irqenable);
160 /* set continuous mode for the syndrome polynomial set */
161 writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
162 &elm_cfg->page_ctrl);
163
164 return 0;
165}
166
167/**
168 * elm_reset - Do a soft reset of ELM
169 *
170 * Perform a soft reset of ELM and return after reset is done.
171 */
172void elm_reset(void)
173{
174 /* initiate reset */
175 writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
pekon guptabeba5f02013-11-18 19:02:59 +0530176 &elm_cfg->sysconfig);
Mansoor Ahamed04c37572012-11-06 13:06:32 +0000177
178 /* wait for reset complete and normal operation */
179 while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
180 ELM_SYSSTATUS_RESETDONE)
181 ;
182}
183
184/**
185 * elm_init - Initialize ELM module
186 *
187 * Initialize ELM support. Currently it does only base address init
188 * and ELM reset.
189 */
190void elm_init(void)
191{
192 elm_cfg = (struct elm *)ELM_BASE;
193 elm_reset();
194}