synopsys_edac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Synopsys DDR ECC Driver
  3. * This driver is based on ppc4xx_edac.c drivers
  4. *
  5. * Copyright (C) 2012 - 2014 Xilinx, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * This file is subject to the terms and conditions of the GNU General Public
  18. * License. See the file "COPYING" in the main directory of this archive
  19. * for more details
  20. */
  21. #include <linux/edac.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include "edac_core.h"
  25. /* Number of cs_rows needed per memory controller */
  26. #define SYNPS_EDAC_NR_CSROWS 1
  27. /* Number of channels per memory controller */
  28. #define SYNPS_EDAC_NR_CHANS 1
  29. /* Granularity of reported error in bytes */
  30. #define SYNPS_EDAC_ERR_GRAIN 1
  31. #define SYNPS_EDAC_MSG_SIZE 256
  32. #define SYNPS_EDAC_MOD_STRING "synps_edac"
  33. #define SYNPS_EDAC_MOD_VER "1"
  34. /* Synopsys DDR memory controller registers that are relevant to ECC */
  35. #define CTRL_OFST 0x0
  36. #define T_ZQ_OFST 0xA4
  37. /* ECC control register */
  38. #define ECC_CTRL_OFST 0xC4
  39. /* ECC log register */
  40. #define CE_LOG_OFST 0xC8
  41. /* ECC address register */
  42. #define CE_ADDR_OFST 0xCC
  43. /* ECC data[31:0] register */
  44. #define CE_DATA_31_0_OFST 0xD0
  45. /* Uncorrectable error info registers */
  46. #define UE_LOG_OFST 0xDC
  47. #define UE_ADDR_OFST 0xE0
  48. #define UE_DATA_31_0_OFST 0xE4
  49. #define STAT_OFST 0xF0
  50. #define SCRUB_OFST 0xF4
  51. /* Control register bit field definitions */
  52. #define CTRL_BW_MASK 0xC
  53. #define CTRL_BW_SHIFT 2
  54. #define DDRCTL_WDTH_16 1
  55. #define DDRCTL_WDTH_32 0
  56. /* ZQ register bit field definitions */
  57. #define T_ZQ_DDRMODE_MASK 0x2
  58. /* ECC control register bit field definitions */
  59. #define ECC_CTRL_CLR_CE_ERR 0x2
  60. #define ECC_CTRL_CLR_UE_ERR 0x1
  61. /* ECC correctable/uncorrectable error log register definitions */
  62. #define LOG_VALID 0x1
  63. #define CE_LOG_BITPOS_MASK 0xFE
  64. #define CE_LOG_BITPOS_SHIFT 1
  65. /* ECC correctable/uncorrectable error address register definitions */
  66. #define ADDR_COL_MASK 0xFFF
  67. #define ADDR_ROW_MASK 0xFFFF000
  68. #define ADDR_ROW_SHIFT 12
  69. #define ADDR_BANK_MASK 0x70000000
  70. #define ADDR_BANK_SHIFT 28
  71. /* ECC statistic register definitions */
  72. #define STAT_UECNT_MASK 0xFF
  73. #define STAT_CECNT_MASK 0xFF00
  74. #define STAT_CECNT_SHIFT 8
  75. /* ECC scrub register definitions */
  76. #define SCRUB_MODE_MASK 0x7
  77. #define SCRUB_MODE_SECDED 0x4
  78. /**
  79. * struct ecc_error_info - ECC error log information
  80. * @row: Row number
  81. * @col: Column number
  82. * @bank: Bank number
  83. * @bitpos: Bit position
  84. * @data: Data causing the error
  85. */
  86. struct ecc_error_info {
  87. u32 row;
  88. u32 col;
  89. u32 bank;
  90. u32 bitpos;
  91. u32 data;
  92. };
  93. /**
  94. * struct synps_ecc_status - ECC status information to report
  95. * @ce_cnt: Correctable error count
  96. * @ue_cnt: Uncorrectable error count
  97. * @ceinfo: Correctable error log information
  98. * @ueinfo: Uncorrectable error log information
  99. */
  100. struct synps_ecc_status {
  101. u32 ce_cnt;
  102. u32 ue_cnt;
  103. struct ecc_error_info ceinfo;
  104. struct ecc_error_info ueinfo;
  105. };
  106. /**
  107. * struct synps_edac_priv - DDR memory controller private instance data
  108. * @baseaddr: Base address of the DDR controller
  109. * @message: Buffer for framing the event specific info
  110. * @stat: ECC status information
  111. * @ce_cnt: Correctable Error count
  112. * @ue_cnt: Uncorrectable Error count
  113. */
  114. struct synps_edac_priv {
  115. void __iomem *baseaddr;
  116. char message[SYNPS_EDAC_MSG_SIZE];
  117. struct synps_ecc_status stat;
  118. u32 ce_cnt;
  119. u32 ue_cnt;
  120. };
  121. /**
  122. * synps_edac_geterror_info - Get the current ecc error info
  123. * @base: Pointer to the base address of the ddr memory controller
  124. * @p: Pointer to the synopsys ecc status structure
  125. *
  126. * Determines there is any ecc error or not
  127. *
  128. * Return: one if there is no error otherwise returns zero
  129. */
  130. static int synps_edac_geterror_info(void __iomem *base,
  131. struct synps_ecc_status *p)
  132. {
  133. u32 regval, clearval = 0;
  134. regval = readl(base + STAT_OFST);
  135. if (!regval)
  136. return 1;
  137. p->ce_cnt = (regval & STAT_CECNT_MASK) >> STAT_CECNT_SHIFT;
  138. p->ue_cnt = regval & STAT_UECNT_MASK;
  139. regval = readl(base + CE_LOG_OFST);
  140. if (!(p->ce_cnt && (regval & LOG_VALID)))
  141. goto ue_err;
  142. p->ceinfo.bitpos = (regval & CE_LOG_BITPOS_MASK) >> CE_LOG_BITPOS_SHIFT;
  143. regval = readl(base + CE_ADDR_OFST);
  144. p->ceinfo.row = (regval & ADDR_ROW_MASK) >> ADDR_ROW_SHIFT;
  145. p->ceinfo.col = regval & ADDR_COL_MASK;
  146. p->ceinfo.bank = (regval & ADDR_BANK_MASK) >> ADDR_BANK_SHIFT;
  147. p->ceinfo.data = readl(base + CE_DATA_31_0_OFST);
  148. edac_dbg(3, "ce bit position: %d data: %d\n", p->ceinfo.bitpos,
  149. p->ceinfo.data);
  150. clearval = ECC_CTRL_CLR_CE_ERR;
  151. ue_err:
  152. regval = readl(base + UE_LOG_OFST);
  153. if (!(p->ue_cnt && (regval & LOG_VALID)))
  154. goto out;
  155. regval = readl(base + UE_ADDR_OFST);
  156. p->ueinfo.row = (regval & ADDR_ROW_MASK) >> ADDR_ROW_SHIFT;
  157. p->ueinfo.col = regval & ADDR_COL_MASK;
  158. p->ueinfo.bank = (regval & ADDR_BANK_MASK) >> ADDR_BANK_SHIFT;
  159. p->ueinfo.data = readl(base + UE_DATA_31_0_OFST);
  160. clearval |= ECC_CTRL_CLR_UE_ERR;
  161. out:
  162. writel(clearval, base + ECC_CTRL_OFST);
  163. writel(0x0, base + ECC_CTRL_OFST);
  164. return 0;
  165. }
  166. /**
  167. * synps_edac_handle_error - Handle controller error types CE and UE
  168. * @mci: Pointer to the edac memory controller instance
  169. * @p: Pointer to the synopsys ecc status structure
  170. *
  171. * Handles the controller ECC correctable and un correctable error.
  172. */
  173. static void synps_edac_handle_error(struct mem_ctl_info *mci,
  174. struct synps_ecc_status *p)
  175. {
  176. struct synps_edac_priv *priv = mci->pvt_info;
  177. struct ecc_error_info *pinf;
  178. if (p->ce_cnt) {
  179. pinf = &p->ceinfo;
  180. snprintf(priv->message, SYNPS_EDAC_MSG_SIZE,
  181. "DDR ECC error type :%s Row %d Bank %d Col %d ",
  182. "CE", pinf->row, pinf->bank, pinf->col);
  183. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci,
  184. p->ce_cnt, 0, 0, 0, 0, 0, -1,
  185. priv->message, "");
  186. }
  187. if (p->ue_cnt) {
  188. pinf = &p->ueinfo;
  189. snprintf(priv->message, SYNPS_EDAC_MSG_SIZE,
  190. "DDR ECC error type :%s Row %d Bank %d Col %d ",
  191. "UE", pinf->row, pinf->bank, pinf->col);
  192. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci,
  193. p->ue_cnt, 0, 0, 0, 0, 0, -1,
  194. priv->message, "");
  195. }
  196. memset(p, 0, sizeof(*p));
  197. }
  198. /**
  199. * synps_edac_check - Check controller for ECC errors
  200. * @mci: Pointer to the edac memory controller instance
  201. *
  202. * Used to check and post ECC errors. Called by the polling thread
  203. */
  204. static void synps_edac_check(struct mem_ctl_info *mci)
  205. {
  206. struct synps_edac_priv *priv = mci->pvt_info;
  207. int status;
  208. status = synps_edac_geterror_info(priv->baseaddr, &priv->stat);
  209. if (status)
  210. return;
  211. priv->ce_cnt += priv->stat.ce_cnt;
  212. priv->ue_cnt += priv->stat.ue_cnt;
  213. synps_edac_handle_error(mci, &priv->stat);
  214. edac_dbg(3, "Total error count ce %d ue %d\n",
  215. priv->ce_cnt, priv->ue_cnt);
  216. }
  217. /**
  218. * synps_edac_get_dtype - Return the controller memory width
  219. * @base: Pointer to the ddr memory controller base address
  220. *
  221. * Get the EDAC device type width appropriate for the current controller
  222. * configuration.
  223. *
  224. * Return: a device type width enumeration.
  225. */
  226. static enum dev_type synps_edac_get_dtype(const void __iomem *base)
  227. {
  228. enum dev_type dt;
  229. u32 width;
  230. width = readl(base + CTRL_OFST);
  231. width = (width & CTRL_BW_MASK) >> CTRL_BW_SHIFT;
  232. switch (width) {
  233. case DDRCTL_WDTH_16:
  234. dt = DEV_X2;
  235. break;
  236. case DDRCTL_WDTH_32:
  237. dt = DEV_X4;
  238. break;
  239. default:
  240. dt = DEV_UNKNOWN;
  241. }
  242. return dt;
  243. }
  244. /**
  245. * synps_edac_get_eccstate - Return the controller ecc enable/disable status
  246. * @base: Pointer to the ddr memory controller base address
  247. *
  248. * Get the ECC enable/disable status for the controller
  249. *
  250. * Return: a ecc status boolean i.e true/false - enabled/disabled.
  251. */
  252. static bool synps_edac_get_eccstate(void __iomem *base)
  253. {
  254. enum dev_type dt;
  255. u32 ecctype;
  256. bool state = false;
  257. dt = synps_edac_get_dtype(base);
  258. if (dt == DEV_UNKNOWN)
  259. return state;
  260. ecctype = readl(base + SCRUB_OFST) & SCRUB_MODE_MASK;
  261. if ((ecctype == SCRUB_MODE_SECDED) && (dt == DEV_X2))
  262. state = true;
  263. return state;
  264. }
  265. /**
  266. * synps_edac_get_memsize - reads the size of the attached memory device
  267. *
  268. * Return: the memory size in bytes
  269. */
  270. static u32 synps_edac_get_memsize(void)
  271. {
  272. struct sysinfo inf;
  273. si_meminfo(&inf);
  274. return inf.totalram * inf.mem_unit;
  275. }
  276. /**
  277. * synps_edac_get_mtype - Returns controller memory type
  278. * @base: pointer to the synopsys ecc status structure
  279. *
  280. * Get the EDAC memory type appropriate for the current controller
  281. * configuration.
  282. *
  283. * Return: a memory type enumeration.
  284. */
  285. static enum mem_type synps_edac_get_mtype(const void __iomem *base)
  286. {
  287. enum mem_type mt;
  288. u32 memtype;
  289. memtype = readl(base + T_ZQ_OFST);
  290. if (memtype & T_ZQ_DDRMODE_MASK)
  291. mt = MEM_DDR3;
  292. else
  293. mt = MEM_DDR2;
  294. return mt;
  295. }
  296. /**
  297. * synps_edac_init_csrows - Initialize the cs row data
  298. * @mci: Pointer to the edac memory controller instance
  299. *
  300. * Initializes the chip select rows associated with the EDAC memory
  301. * controller instance
  302. *
  303. * Return: Unconditionally 0.
  304. */
  305. static int synps_edac_init_csrows(struct mem_ctl_info *mci)
  306. {
  307. struct csrow_info *csi;
  308. struct dimm_info *dimm;
  309. struct synps_edac_priv *priv = mci->pvt_info;
  310. u32 size;
  311. int row, j;
  312. for (row = 0; row < mci->nr_csrows; row++) {
  313. csi = mci->csrows[row];
  314. size = synps_edac_get_memsize();
  315. for (j = 0; j < csi->nr_channels; j++) {
  316. dimm = csi->channels[j]->dimm;
  317. dimm->edac_mode = EDAC_FLAG_SECDED;
  318. dimm->mtype = synps_edac_get_mtype(priv->baseaddr);
  319. dimm->nr_pages = (size >> PAGE_SHIFT) / csi->nr_channels;
  320. dimm->grain = SYNPS_EDAC_ERR_GRAIN;
  321. dimm->dtype = synps_edac_get_dtype(priv->baseaddr);
  322. }
  323. }
  324. return 0;
  325. }
  326. /**
  327. * synps_edac_mc_init - Initialize driver instance
  328. * @mci: Pointer to the edac memory controller instance
  329. * @pdev: Pointer to the platform_device struct
  330. *
  331. * Performs initialization of the EDAC memory controller instance and
  332. * related driver-private data associated with the memory controller the
  333. * instance is bound to.
  334. *
  335. * Return: Always zero.
  336. */
  337. static int synps_edac_mc_init(struct mem_ctl_info *mci,
  338. struct platform_device *pdev)
  339. {
  340. int status;
  341. struct synps_edac_priv *priv;
  342. mci->pdev = &pdev->dev;
  343. priv = mci->pvt_info;
  344. platform_set_drvdata(pdev, mci);
  345. /* Initialize controller capabilities and configuration */
  346. mci->mtype_cap = MEM_FLAG_DDR3 | MEM_FLAG_DDR2;
  347. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
  348. mci->scrub_cap = SCRUB_HW_SRC;
  349. mci->scrub_mode = SCRUB_NONE;
  350. mci->edac_cap = EDAC_FLAG_SECDED;
  351. mci->ctl_name = "synps_ddr_controller";
  352. mci->dev_name = SYNPS_EDAC_MOD_STRING;
  353. mci->mod_name = SYNPS_EDAC_MOD_VER;
  354. mci->mod_ver = "1";
  355. edac_op_state = EDAC_OPSTATE_POLL;
  356. mci->edac_check = synps_edac_check;
  357. mci->ctl_page_to_phys = NULL;
  358. status = synps_edac_init_csrows(mci);
  359. return status;
  360. }
  361. /**
  362. * synps_edac_mc_probe - Check controller and bind driver
  363. * @pdev: Pointer to the platform_device struct
  364. *
  365. * Probes a specific controller instance for binding with the driver.
  366. *
  367. * Return: 0 if the controller instance was successfully bound to the
  368. * driver; otherwise, < 0 on error.
  369. */
  370. static int synps_edac_mc_probe(struct platform_device *pdev)
  371. {
  372. struct mem_ctl_info *mci;
  373. struct edac_mc_layer layers[2];
  374. struct synps_edac_priv *priv;
  375. int rc;
  376. struct resource *res;
  377. void __iomem *baseaddr;
  378. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  379. baseaddr = devm_ioremap_resource(&pdev->dev, res);
  380. if (IS_ERR(baseaddr))
  381. return PTR_ERR(baseaddr);
  382. if (!synps_edac_get_eccstate(baseaddr)) {
  383. edac_printk(KERN_INFO, EDAC_MC, "ECC not enabled\n");
  384. return -ENXIO;
  385. }
  386. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  387. layers[0].size = SYNPS_EDAC_NR_CSROWS;
  388. layers[0].is_virt_csrow = true;
  389. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  390. layers[1].size = SYNPS_EDAC_NR_CHANS;
  391. layers[1].is_virt_csrow = false;
  392. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  393. sizeof(struct synps_edac_priv));
  394. if (!mci) {
  395. edac_printk(KERN_ERR, EDAC_MC,
  396. "Failed memory allocation for mc instance\n");
  397. return -ENOMEM;
  398. }
  399. priv = mci->pvt_info;
  400. priv->baseaddr = baseaddr;
  401. rc = synps_edac_mc_init(mci, pdev);
  402. if (rc) {
  403. edac_printk(KERN_ERR, EDAC_MC,
  404. "Failed to initialize instance\n");
  405. goto free_edac_mc;
  406. }
  407. rc = edac_mc_add_mc(mci);
  408. if (rc) {
  409. edac_printk(KERN_ERR, EDAC_MC,
  410. "Failed to register with EDAC core\n");
  411. goto free_edac_mc;
  412. }
  413. /*
  414. * Start capturing the correctable and uncorrectable errors. A write of
  415. * 0 starts the counters.
  416. */
  417. writel(0x0, baseaddr + ECC_CTRL_OFST);
  418. return rc;
  419. free_edac_mc:
  420. edac_mc_free(mci);
  421. return rc;
  422. }
  423. /**
  424. * synps_edac_mc_remove - Unbind driver from controller
  425. * @pdev: Pointer to the platform_device struct
  426. *
  427. * Return: Unconditionally 0
  428. */
  429. static int synps_edac_mc_remove(struct platform_device *pdev)
  430. {
  431. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  432. edac_mc_del_mc(&pdev->dev);
  433. edac_mc_free(mci);
  434. return 0;
  435. }
  436. static const struct of_device_id synps_edac_match[] = {
  437. { .compatible = "xlnx,zynq-ddrc-a05", },
  438. { /* end of table */ }
  439. };
  440. MODULE_DEVICE_TABLE(of, synps_edac_match);
  441. static struct platform_driver synps_edac_mc_driver = {
  442. .driver = {
  443. .name = "synopsys-edac",
  444. .of_match_table = synps_edac_match,
  445. },
  446. .probe = synps_edac_mc_probe,
  447. .remove = synps_edac_mc_remove,
  448. };
  449. module_platform_driver(synps_edac_mc_driver);
  450. MODULE_AUTHOR("Xilinx Inc");
  451. MODULE_DESCRIPTION("Synopsys DDR ECC driver");
  452. MODULE_LICENSE("GPL v2");