blob: 6d4dd84a9d4865f0b8865e69dd40f6d8ca1e52a0 [file] [log] [blame]
York Sunea2eb9a2016-08-11 13:15:18 -07001/*
2 * Freescale Memory Controller kernel module
3 *
4 * Support Power-based SoCs including MPC85xx, MPC86xx, MPC83xx and
5 * ARM-based Layerscape SoCs including LS2xxx. Originally split
6 * out from mpc85xx_edac EDAC driver.
7 *
8 * Parts Copyrighted (c) 2013 by Freescale Semiconductor, Inc.
9 *
10 * Author: Dave Jiang <djiang@mvista.com>
11 *
12 * 2006-2007 (c) MontaVista Software, Inc. This file is licensed under
13 * the terms of the GNU General Public License version 2. This program
14 * is licensed "as is" without any warranty of any kind, whether express
15 * or implied.
York Sunea2eb9a2016-08-11 13:15:18 -070016 */
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/ctype.h>
21#include <linux/io.h>
22#include <linux/mod_devicetable.h>
23#include <linux/edac.h>
24#include <linux/smp.h>
25#include <linux/gfp.h>
26
27#include <linux/of_platform.h>
28#include <linux/of_device.h>
29#include "edac_module.h"
30#include "edac_core.h"
31#include "fsl_ddr_edac.h"
32
33#define EDAC_MOD_STR "fsl_ddr_edac"
34
35static int edac_mc_idx;
36
37static u32 orig_ddr_err_disable;
38static u32 orig_ddr_err_sbe;
York Sun339fdff2016-08-09 14:55:43 -070039static bool little_endian;
40
41static inline u32 ddr_in32(void __iomem *addr)
42{
43 return little_endian ? ioread32(addr) : ioread32be(addr);
44}
45
46static inline void ddr_out32(void __iomem *addr, u32 value)
47{
48 if (little_endian)
49 iowrite32(value, addr);
50 else
51 iowrite32be(value, addr);
52}
York Sunea2eb9a2016-08-11 13:15:18 -070053
54/************************ MC SYSFS parts ***********************************/
55
56#define to_mci(k) container_of(k, struct mem_ctl_info, dev)
57
York Sund43a9fb2016-08-09 14:55:41 -070058static ssize_t fsl_mc_inject_data_hi_show(struct device *dev,
59 struct device_attribute *mattr,
60 char *data)
61{
62 struct mem_ctl_info *mci = to_mci(dev);
63 struct fsl_mc_pdata *pdata = mci->pvt_info;
64 return sprintf(data, "0x%08x",
York Sun339fdff2016-08-09 14:55:43 -070065 ddr_in32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_HI));
York Sund43a9fb2016-08-09 14:55:41 -070066}
67
68static ssize_t fsl_mc_inject_data_lo_show(struct device *dev,
69 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070070 char *data)
71{
72 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070073 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070074 return sprintf(data, "0x%08x",
York Sun339fdff2016-08-09 14:55:43 -070075 ddr_in32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_LO));
York Sunea2eb9a2016-08-11 13:15:18 -070076}
77
York Sund43a9fb2016-08-09 14:55:41 -070078static ssize_t fsl_mc_inject_ctrl_show(struct device *dev,
79 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070080 char *data)
81{
82 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070083 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070084 return sprintf(data, "0x%08x",
York Sun339fdff2016-08-09 14:55:43 -070085 ddr_in32(pdata->mc_vbase + FSL_MC_ECC_ERR_INJECT));
York Sunea2eb9a2016-08-11 13:15:18 -070086}
87
York Sund43a9fb2016-08-09 14:55:41 -070088static ssize_t fsl_mc_inject_data_hi_store(struct device *dev,
89 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070090 const char *data, size_t count)
91{
92 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070093 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070094 if (isdigit(*data)) {
York Sun339fdff2016-08-09 14:55:43 -070095 ddr_out32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_HI,
96 simple_strtoul(data, NULL, 0));
York Sunea2eb9a2016-08-11 13:15:18 -070097 return count;
98 }
99 return 0;
100}
101
York Sund43a9fb2016-08-09 14:55:41 -0700102static ssize_t fsl_mc_inject_data_lo_store(struct device *dev,
103 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -0700104 const char *data, size_t count)
105{
106 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -0700107 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700108 if (isdigit(*data)) {
York Sun339fdff2016-08-09 14:55:43 -0700109 ddr_out32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_LO,
110 simple_strtoul(data, NULL, 0));
York Sunea2eb9a2016-08-11 13:15:18 -0700111 return count;
112 }
113 return 0;
114}
115
York Sund43a9fb2016-08-09 14:55:41 -0700116static ssize_t fsl_mc_inject_ctrl_store(struct device *dev,
117 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -0700118 const char *data, size_t count)
119{
120 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -0700121 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700122 if (isdigit(*data)) {
York Sun339fdff2016-08-09 14:55:43 -0700123 ddr_out32(pdata->mc_vbase + FSL_MC_ECC_ERR_INJECT,
124 simple_strtoul(data, NULL, 0));
York Sunea2eb9a2016-08-11 13:15:18 -0700125 return count;
126 }
127 return 0;
128}
129
130DEVICE_ATTR(inject_data_hi, S_IRUGO | S_IWUSR,
York Sund43a9fb2016-08-09 14:55:41 -0700131 fsl_mc_inject_data_hi_show, fsl_mc_inject_data_hi_store);
York Sunea2eb9a2016-08-11 13:15:18 -0700132DEVICE_ATTR(inject_data_lo, S_IRUGO | S_IWUSR,
York Sund43a9fb2016-08-09 14:55:41 -0700133 fsl_mc_inject_data_lo_show, fsl_mc_inject_data_lo_store);
York Sunea2eb9a2016-08-11 13:15:18 -0700134DEVICE_ATTR(inject_ctrl, S_IRUGO | S_IWUSR,
York Sund43a9fb2016-08-09 14:55:41 -0700135 fsl_mc_inject_ctrl_show, fsl_mc_inject_ctrl_store);
York Sunea2eb9a2016-08-11 13:15:18 -0700136
York Sund43a9fb2016-08-09 14:55:41 -0700137static struct attribute *fsl_ddr_dev_attrs[] = {
York Sunea2eb9a2016-08-11 13:15:18 -0700138 &dev_attr_inject_data_hi.attr,
139 &dev_attr_inject_data_lo.attr,
140 &dev_attr_inject_ctrl.attr,
141 NULL
142};
143
York Sund43a9fb2016-08-09 14:55:41 -0700144ATTRIBUTE_GROUPS(fsl_ddr_dev);
York Sunea2eb9a2016-08-11 13:15:18 -0700145
146/**************************** MC Err device ***************************/
147
148/*
149 * Taken from table 8-55 in the MPC8641 User's Manual and/or 9-61 in the
150 * MPC8572 User's Manual. Each line represents a syndrome bit column as a
151 * 64-bit value, but split into an upper and lower 32-bit chunk. The labels
152 * below correspond to Freescale's manuals.
153 */
154static unsigned int ecc_table[16] = {
155 /* MSB LSB */
156 /* [0:31] [32:63] */
157 0xf00fe11e, 0xc33c0ff7, /* Syndrome bit 7 */
158 0x00ff00ff, 0x00fff0ff,
159 0x0f0f0f0f, 0x0f0fff00,
160 0x11113333, 0x7777000f,
161 0x22224444, 0x8888222f,
162 0x44448888, 0xffff4441,
163 0x8888ffff, 0x11118882,
164 0xffff1111, 0x22221114, /* Syndrome bit 0 */
165};
166
167/*
168 * Calculate the correct ECC value for a 64-bit value specified by high:low
169 */
170static u8 calculate_ecc(u32 high, u32 low)
171{
172 u32 mask_low;
173 u32 mask_high;
174 int bit_cnt;
175 u8 ecc = 0;
176 int i;
177 int j;
178
179 for (i = 0; i < 8; i++) {
180 mask_high = ecc_table[i * 2];
181 mask_low = ecc_table[i * 2 + 1];
182 bit_cnt = 0;
183
184 for (j = 0; j < 32; j++) {
185 if ((mask_high >> j) & 1)
186 bit_cnt ^= (high >> j) & 1;
187 if ((mask_low >> j) & 1)
188 bit_cnt ^= (low >> j) & 1;
189 }
190
191 ecc |= bit_cnt << i;
192 }
193
194 return ecc;
195}
196
197/*
198 * Create the syndrome code which is generated if the data line specified by
199 * 'bit' failed. Eg generate an 8-bit codes seen in Table 8-55 in the MPC8641
200 * User's Manual and 9-61 in the MPC8572 User's Manual.
201 */
202static u8 syndrome_from_bit(unsigned int bit) {
203 int i;
204 u8 syndrome = 0;
205
206 /*
207 * Cycle through the upper or lower 32-bit portion of each value in
208 * ecc_table depending on if 'bit' is in the upper or lower half of
209 * 64-bit data.
210 */
211 for (i = bit < 32; i < 16; i += 2)
212 syndrome |= ((ecc_table[i] >> (bit % 32)) & 1) << (i / 2);
213
214 return syndrome;
215}
216
217/*
218 * Decode data and ecc syndrome to determine what went wrong
219 * Note: This can only decode single-bit errors
220 */
221static void sbe_ecc_decode(u32 cap_high, u32 cap_low, u32 cap_ecc,
222 int *bad_data_bit, int *bad_ecc_bit)
223{
224 int i;
225 u8 syndrome;
226
227 *bad_data_bit = -1;
228 *bad_ecc_bit = -1;
229
230 /*
231 * Calculate the ECC of the captured data and XOR it with the captured
232 * ECC to find an ECC syndrome value we can search for
233 */
234 syndrome = calculate_ecc(cap_high, cap_low) ^ cap_ecc;
235
236 /* Check if a data line is stuck... */
237 for (i = 0; i < 64; i++) {
238 if (syndrome == syndrome_from_bit(i)) {
239 *bad_data_bit = i;
240 return;
241 }
242 }
243
244 /* If data is correct, check ECC bits for errors... */
245 for (i = 0; i < 8; i++) {
246 if ((syndrome >> i) & 0x1) {
247 *bad_ecc_bit = i;
248 return;
249 }
250 }
251}
252
253#define make64(high, low) (((u64)(high) << 32) | (low))
254
York Sund43a9fb2016-08-09 14:55:41 -0700255static void fsl_mc_check(struct mem_ctl_info *mci)
York Sunea2eb9a2016-08-11 13:15:18 -0700256{
York Sund43a9fb2016-08-09 14:55:41 -0700257 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700258 struct csrow_info *csrow;
259 u32 bus_width;
260 u32 err_detect;
261 u32 syndrome;
262 u64 err_addr;
263 u32 pfn;
264 int row_index;
265 u32 cap_high;
266 u32 cap_low;
267 int bad_data_bit;
268 int bad_ecc_bit;
269
York Sun339fdff2016-08-09 14:55:43 -0700270 err_detect = ddr_in32(pdata->mc_vbase + FSL_MC_ERR_DETECT);
York Sunea2eb9a2016-08-11 13:15:18 -0700271 if (!err_detect)
272 return;
273
York Sund43a9fb2016-08-09 14:55:41 -0700274 fsl_mc_printk(mci, KERN_ERR, "Err Detect Register: %#8.8x\n",
275 err_detect);
York Sunea2eb9a2016-08-11 13:15:18 -0700276
277 /* no more processing if not ECC bit errors */
278 if (!(err_detect & (DDR_EDE_SBE | DDR_EDE_MBE))) {
York Sun339fdff2016-08-09 14:55:43 -0700279 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DETECT, err_detect);
York Sunea2eb9a2016-08-11 13:15:18 -0700280 return;
281 }
282
York Sun339fdff2016-08-09 14:55:43 -0700283 syndrome = ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_ECC);
York Sunea2eb9a2016-08-11 13:15:18 -0700284
285 /* Mask off appropriate bits of syndrome based on bus width */
York Sun339fdff2016-08-09 14:55:43 -0700286 bus_width = (ddr_in32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG) &
287 DSC_DBW_MASK) ? 32 : 64;
York Sunea2eb9a2016-08-11 13:15:18 -0700288 if (bus_width == 64)
289 syndrome &= 0xff;
290 else
291 syndrome &= 0xffff;
292
293 err_addr = make64(
York Sun339fdff2016-08-09 14:55:43 -0700294 ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_EXT_ADDRESS),
295 ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_ADDRESS));
York Sunea2eb9a2016-08-11 13:15:18 -0700296 pfn = err_addr >> PAGE_SHIFT;
297
298 for (row_index = 0; row_index < mci->nr_csrows; row_index++) {
299 csrow = mci->csrows[row_index];
300 if ((pfn >= csrow->first_page) && (pfn <= csrow->last_page))
301 break;
302 }
303
York Sun339fdff2016-08-09 14:55:43 -0700304 cap_high = ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_DATA_HI);
305 cap_low = ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_DATA_LO);
York Sunea2eb9a2016-08-11 13:15:18 -0700306
307 /*
308 * Analyze single-bit errors on 64-bit wide buses
309 * TODO: Add support for 32-bit wide buses
310 */
311 if ((err_detect & DDR_EDE_SBE) && (bus_width == 64)) {
312 sbe_ecc_decode(cap_high, cap_low, syndrome,
313 &bad_data_bit, &bad_ecc_bit);
314
315 if (bad_data_bit != -1)
York Sund43a9fb2016-08-09 14:55:41 -0700316 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700317 "Faulty Data bit: %d\n", bad_data_bit);
318 if (bad_ecc_bit != -1)
York Sund43a9fb2016-08-09 14:55:41 -0700319 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700320 "Faulty ECC bit: %d\n", bad_ecc_bit);
321
York Sund43a9fb2016-08-09 14:55:41 -0700322 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700323 "Expected Data / ECC:\t%#8.8x_%08x / %#2.2x\n",
324 cap_high ^ (1 << (bad_data_bit - 32)),
325 cap_low ^ (1 << bad_data_bit),
326 syndrome ^ (1 << bad_ecc_bit));
327 }
328
York Sund43a9fb2016-08-09 14:55:41 -0700329 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700330 "Captured Data / ECC:\t%#8.8x_%08x / %#2.2x\n",
331 cap_high, cap_low, syndrome);
York Sund43a9fb2016-08-09 14:55:41 -0700332 fsl_mc_printk(mci, KERN_ERR, "Err addr: %#8.8llx\n", err_addr);
333 fsl_mc_printk(mci, KERN_ERR, "PFN: %#8.8x\n", pfn);
York Sunea2eb9a2016-08-11 13:15:18 -0700334
335 /* we are out of range */
336 if (row_index == mci->nr_csrows)
York Sund43a9fb2016-08-09 14:55:41 -0700337 fsl_mc_printk(mci, KERN_ERR, "PFN out of range!\n");
York Sunea2eb9a2016-08-11 13:15:18 -0700338
339 if (err_detect & DDR_EDE_SBE)
340 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
341 pfn, err_addr & ~PAGE_MASK, syndrome,
342 row_index, 0, -1,
343 mci->ctl_name, "");
344
345 if (err_detect & DDR_EDE_MBE)
346 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
347 pfn, err_addr & ~PAGE_MASK, syndrome,
348 row_index, 0, -1,
349 mci->ctl_name, "");
350
York Sun339fdff2016-08-09 14:55:43 -0700351 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DETECT, err_detect);
York Sunea2eb9a2016-08-11 13:15:18 -0700352}
353
York Sund43a9fb2016-08-09 14:55:41 -0700354static irqreturn_t fsl_mc_isr(int irq, void *dev_id)
York Sunea2eb9a2016-08-11 13:15:18 -0700355{
356 struct mem_ctl_info *mci = dev_id;
York Sund43a9fb2016-08-09 14:55:41 -0700357 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700358 u32 err_detect;
359
York Sun339fdff2016-08-09 14:55:43 -0700360 err_detect = ddr_in32(pdata->mc_vbase + FSL_MC_ERR_DETECT);
York Sunea2eb9a2016-08-11 13:15:18 -0700361 if (!err_detect)
362 return IRQ_NONE;
363
York Sund43a9fb2016-08-09 14:55:41 -0700364 fsl_mc_check(mci);
York Sunea2eb9a2016-08-11 13:15:18 -0700365
366 return IRQ_HANDLED;
367}
368
York Sund43a9fb2016-08-09 14:55:41 -0700369static void fsl_ddr_init_csrows(struct mem_ctl_info *mci)
York Sunea2eb9a2016-08-11 13:15:18 -0700370{
York Sund43a9fb2016-08-09 14:55:41 -0700371 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700372 struct csrow_info *csrow;
373 struct dimm_info *dimm;
374 u32 sdram_ctl;
375 u32 sdtype;
376 enum mem_type mtype;
377 u32 cs_bnds;
378 int index;
379
York Sun339fdff2016-08-09 14:55:43 -0700380 sdram_ctl = ddr_in32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG);
York Sunea2eb9a2016-08-11 13:15:18 -0700381
382 sdtype = sdram_ctl & DSC_SDTYPE_MASK;
383 if (sdram_ctl & DSC_RD_EN) {
384 switch (sdtype) {
York Sun4e2c3252016-08-09 14:55:42 -0700385 case 0x02000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700386 mtype = MEM_RDDR;
387 break;
York Sun4e2c3252016-08-09 14:55:42 -0700388 case 0x03000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700389 mtype = MEM_RDDR2;
390 break;
York Sun4e2c3252016-08-09 14:55:42 -0700391 case 0x07000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700392 mtype = MEM_RDDR3;
393 break;
York Sun4e2c3252016-08-09 14:55:42 -0700394 case 0x05000000:
395 mtype = MEM_RDDR4;
396 break;
York Sunea2eb9a2016-08-11 13:15:18 -0700397 default:
398 mtype = MEM_UNKNOWN;
399 break;
400 }
401 } else {
402 switch (sdtype) {
York Sun4e2c3252016-08-09 14:55:42 -0700403 case 0x02000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700404 mtype = MEM_DDR;
405 break;
York Sun4e2c3252016-08-09 14:55:42 -0700406 case 0x03000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700407 mtype = MEM_DDR2;
408 break;
York Sun4e2c3252016-08-09 14:55:42 -0700409 case 0x07000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700410 mtype = MEM_DDR3;
411 break;
York Sun4e2c3252016-08-09 14:55:42 -0700412 case 0x05000000:
413 mtype = MEM_DDR4;
414 break;
York Sunea2eb9a2016-08-11 13:15:18 -0700415 default:
416 mtype = MEM_UNKNOWN;
417 break;
418 }
419 }
420
421 for (index = 0; index < mci->nr_csrows; index++) {
422 u32 start;
423 u32 end;
424
425 csrow = mci->csrows[index];
426 dimm = csrow->channels[0]->dimm;
427
York Sun339fdff2016-08-09 14:55:43 -0700428 cs_bnds = ddr_in32(pdata->mc_vbase + FSL_MC_CS_BNDS_0 +
429 (index * FSL_MC_CS_BNDS_OFS));
York Sunea2eb9a2016-08-11 13:15:18 -0700430
431 start = (cs_bnds & 0xffff0000) >> 16;
432 end = (cs_bnds & 0x0000ffff);
433
434 if (start == end)
435 continue; /* not populated */
436
437 start <<= (24 - PAGE_SHIFT);
438 end <<= (24 - PAGE_SHIFT);
439 end |= (1 << (24 - PAGE_SHIFT)) - 1;
440
441 csrow->first_page = start;
442 csrow->last_page = end;
443
444 dimm->nr_pages = end + 1 - start;
445 dimm->grain = 8;
446 dimm->mtype = mtype;
447 dimm->dtype = DEV_UNKNOWN;
448 if (sdram_ctl & DSC_X32_EN)
449 dimm->dtype = DEV_X32;
450 dimm->edac_mode = EDAC_SECDED;
451 }
452}
453
York Sund43a9fb2016-08-09 14:55:41 -0700454int fsl_mc_err_probe(struct platform_device *op)
York Sunea2eb9a2016-08-11 13:15:18 -0700455{
456 struct mem_ctl_info *mci;
457 struct edac_mc_layer layers[2];
York Sund43a9fb2016-08-09 14:55:41 -0700458 struct fsl_mc_pdata *pdata;
York Sunea2eb9a2016-08-11 13:15:18 -0700459 struct resource r;
460 u32 sdram_ctl;
461 int res;
462
York Sund43a9fb2016-08-09 14:55:41 -0700463 if (!devres_open_group(&op->dev, fsl_mc_err_probe, GFP_KERNEL))
York Sunea2eb9a2016-08-11 13:15:18 -0700464 return -ENOMEM;
465
466 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
467 layers[0].size = 4;
468 layers[0].is_virt_csrow = true;
469 layers[1].type = EDAC_MC_LAYER_CHANNEL;
470 layers[1].size = 1;
471 layers[1].is_virt_csrow = false;
472 mci = edac_mc_alloc(edac_mc_idx, ARRAY_SIZE(layers), layers,
473 sizeof(*pdata));
474 if (!mci) {
York Sund43a9fb2016-08-09 14:55:41 -0700475 devres_release_group(&op->dev, fsl_mc_err_probe);
York Sunea2eb9a2016-08-11 13:15:18 -0700476 return -ENOMEM;
477 }
478
479 pdata = mci->pvt_info;
York Sund43a9fb2016-08-09 14:55:41 -0700480 pdata->name = "fsl_mc_err";
York Sunea2eb9a2016-08-11 13:15:18 -0700481 pdata->irq = NO_IRQ;
482 mci->pdev = &op->dev;
483 pdata->edac_idx = edac_mc_idx++;
484 dev_set_drvdata(mci->pdev, mci);
485 mci->ctl_name = pdata->name;
486 mci->dev_name = pdata->name;
487
York Sun339fdff2016-08-09 14:55:43 -0700488 /*
489 * Get the endianness of DDR controller registers.
490 * Default is big endian.
491 */
492 little_endian = of_property_read_bool(op->dev.of_node, "little-endian");
493
York Sunea2eb9a2016-08-11 13:15:18 -0700494 res = of_address_to_resource(op->dev.of_node, 0, &r);
495 if (res) {
496 pr_err("%s: Unable to get resource for MC err regs\n",
497 __func__);
498 goto err;
499 }
500
501 if (!devm_request_mem_region(&op->dev, r.start, resource_size(&r),
502 pdata->name)) {
503 pr_err("%s: Error while requesting mem region\n",
504 __func__);
505 res = -EBUSY;
506 goto err;
507 }
508
509 pdata->mc_vbase = devm_ioremap(&op->dev, r.start, resource_size(&r));
510 if (!pdata->mc_vbase) {
511 pr_err("%s: Unable to setup MC err regs\n", __func__);
512 res = -ENOMEM;
513 goto err;
514 }
515
York Sun339fdff2016-08-09 14:55:43 -0700516 sdram_ctl = ddr_in32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG);
York Sunea2eb9a2016-08-11 13:15:18 -0700517 if (!(sdram_ctl & DSC_ECC_EN)) {
518 /* no ECC */
519 pr_warn("%s: No ECC DIMMs discovered\n", __func__);
520 res = -ENODEV;
521 goto err;
522 }
523
524 edac_dbg(3, "init mci\n");
York Sun4e2c3252016-08-09 14:55:42 -0700525 mci->mtype_cap = MEM_FLAG_DDR | MEM_FLAG_RDDR |
526 MEM_FLAG_DDR2 | MEM_FLAG_RDDR2 |
527 MEM_FLAG_DDR3 | MEM_FLAG_RDDR3 |
528 MEM_FLAG_DDR4 | MEM_FLAG_RDDR4;
York Sunea2eb9a2016-08-11 13:15:18 -0700529 mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
530 mci->edac_cap = EDAC_FLAG_SECDED;
531 mci->mod_name = EDAC_MOD_STR;
532
533 if (edac_op_state == EDAC_OPSTATE_POLL)
York Sund43a9fb2016-08-09 14:55:41 -0700534 mci->edac_check = fsl_mc_check;
York Sunea2eb9a2016-08-11 13:15:18 -0700535
536 mci->ctl_page_to_phys = NULL;
537
538 mci->scrub_mode = SCRUB_SW_SRC;
539
York Sund43a9fb2016-08-09 14:55:41 -0700540 fsl_ddr_init_csrows(mci);
York Sunea2eb9a2016-08-11 13:15:18 -0700541
542 /* store the original error disable bits */
York Sun339fdff2016-08-09 14:55:43 -0700543 orig_ddr_err_disable = ddr_in32(pdata->mc_vbase + FSL_MC_ERR_DISABLE);
544 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DISABLE, 0);
York Sunea2eb9a2016-08-11 13:15:18 -0700545
546 /* clear all error bits */
York Sun339fdff2016-08-09 14:55:43 -0700547 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DETECT, ~0);
York Sunea2eb9a2016-08-11 13:15:18 -0700548
York Sund43a9fb2016-08-09 14:55:41 -0700549 if (edac_mc_add_mc_with_groups(mci, fsl_ddr_dev_groups)) {
York Sunea2eb9a2016-08-11 13:15:18 -0700550 edac_dbg(3, "failed edac_mc_add_mc()\n");
551 goto err;
552 }
553
554 if (edac_op_state == EDAC_OPSTATE_INT) {
York Sun339fdff2016-08-09 14:55:43 -0700555 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_INT_EN,
556 DDR_EIE_MBEE | DDR_EIE_SBEE);
York Sunea2eb9a2016-08-11 13:15:18 -0700557
558 /* store the original error management threshold */
York Sun339fdff2016-08-09 14:55:43 -0700559 orig_ddr_err_sbe = ddr_in32(pdata->mc_vbase +
560 FSL_MC_ERR_SBE) & 0xff0000;
York Sunea2eb9a2016-08-11 13:15:18 -0700561
562 /* set threshold to 1 error per interrupt */
York Sun339fdff2016-08-09 14:55:43 -0700563 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_SBE, 0x10000);
York Sunea2eb9a2016-08-11 13:15:18 -0700564
565 /* register interrupts */
566 pdata->irq = irq_of_parse_and_map(op->dev.of_node, 0);
567 res = devm_request_irq(&op->dev, pdata->irq,
York Sund43a9fb2016-08-09 14:55:41 -0700568 fsl_mc_isr,
York Sunea2eb9a2016-08-11 13:15:18 -0700569 IRQF_SHARED,
570 "[EDAC] MC err", mci);
571 if (res < 0) {
York Sund43a9fb2016-08-09 14:55:41 -0700572 pr_err("%s: Unable to request irq %d for FSL DDR DRAM ERR\n",
York Sunea2eb9a2016-08-11 13:15:18 -0700573 __func__, pdata->irq);
574 irq_dispose_mapping(pdata->irq);
575 res = -ENODEV;
576 goto err2;
577 }
578
579 pr_info(EDAC_MOD_STR " acquired irq %d for MC\n",
580 pdata->irq);
581 }
582
York Sund43a9fb2016-08-09 14:55:41 -0700583 devres_remove_group(&op->dev, fsl_mc_err_probe);
York Sunea2eb9a2016-08-11 13:15:18 -0700584 edac_dbg(3, "success\n");
585 pr_info(EDAC_MOD_STR " MC err registered\n");
586
587 return 0;
588
589err2:
590 edac_mc_del_mc(&op->dev);
591err:
York Sund43a9fb2016-08-09 14:55:41 -0700592 devres_release_group(&op->dev, fsl_mc_err_probe);
York Sunea2eb9a2016-08-11 13:15:18 -0700593 edac_mc_free(mci);
594 return res;
595}
596
York Sund43a9fb2016-08-09 14:55:41 -0700597int fsl_mc_err_remove(struct platform_device *op)
York Sunea2eb9a2016-08-11 13:15:18 -0700598{
599 struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);
York Sund43a9fb2016-08-09 14:55:41 -0700600 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700601
602 edac_dbg(0, "\n");
603
604 if (edac_op_state == EDAC_OPSTATE_INT) {
York Sunea2eb9a2016-08-11 13:15:18 -0700605 irq_dispose_mapping(pdata->irq);
York Sun339fdff2016-08-09 14:55:43 -0700606 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_INT_EN, 0);
York Sunea2eb9a2016-08-11 13:15:18 -0700607 }
608
York Sun339fdff2016-08-09 14:55:43 -0700609 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DISABLE,
610 orig_ddr_err_disable);
611 ddr_out32(pdata->mc_vbase + FSL_MC_ERR_SBE, orig_ddr_err_sbe);
York Sunea2eb9a2016-08-11 13:15:18 -0700612
613 edac_mc_del_mc(&op->dev);
614 edac_mc_free(mci);
615 return 0;
616}