cpc925_edac.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * cpc925_edac.c, EDAC driver for IBM CPC925 Bridge and Memory Controller.
  3. *
  4. * Copyright (c) 2008 Wind River Systems, Inc.
  5. *
  6. * Authors: Cao Qingtao <qingtao.cao@windriver.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  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.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/edac.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include "edac_core.h"
  28. #include "edac_module.h"
  29. #define CPC925_EDAC_REVISION " Ver: 1.0.0 " __DATE__
  30. #define CPC925_EDAC_MOD_STR "cpc925_edac"
  31. #define cpc925_printk(level, fmt, arg...) \
  32. edac_printk(level, "CPC925", fmt, ##arg)
  33. #define cpc925_mc_printk(mci, level, fmt, arg...) \
  34. edac_mc_chipset_printk(mci, level, "CPC925", fmt, ##arg)
  35. /*
  36. * CPC925 registers are of 32 bits with bit0 defined at the
  37. * most significant bit and bit31 at that of least significant.
  38. */
  39. #define CPC925_BITS_PER_REG 32
  40. #define CPC925_BIT(nr) (1UL << (CPC925_BITS_PER_REG - 1 - nr))
  41. /*
  42. * EDAC device names for the error detections of
  43. * CPU Interface and Hypertransport Link.
  44. */
  45. #define CPC925_CPU_ERR_DEV "cpu"
  46. #define CPC925_HT_LINK_DEV "htlink"
  47. /* Suppose DDR Refresh cycle is 15.6 microsecond */
  48. #define CPC925_REF_FREQ 0xFA69
  49. #define CPC925_SCRUB_BLOCK_SIZE 64 /* bytes */
  50. #define CPC925_NR_CSROWS 8
  51. /*
  52. * All registers and bits definitions are taken from
  53. * "CPC925 Bridge and Memory Controller User Manual, SA14-2761-02".
  54. */
  55. /*
  56. * CPU and Memory Controller Registers
  57. */
  58. /************************************************************
  59. * Processor Interface Exception Mask Register (APIMASK)
  60. ************************************************************/
  61. #define REG_APIMASK_OFFSET 0x30070
  62. enum apimask_bits {
  63. APIMASK_DART = CPC925_BIT(0), /* DART Exception */
  64. APIMASK_ADI0 = CPC925_BIT(1), /* Handshake Error on PI0_ADI */
  65. APIMASK_ADI1 = CPC925_BIT(2), /* Handshake Error on PI1_ADI */
  66. APIMASK_STAT = CPC925_BIT(3), /* Status Exception */
  67. APIMASK_DERR = CPC925_BIT(4), /* Data Error Exception */
  68. APIMASK_ADRS0 = CPC925_BIT(5), /* Addressing Exception on PI0 */
  69. APIMASK_ADRS1 = CPC925_BIT(6), /* Addressing Exception on PI1 */
  70. /* BIT(7) Reserved */
  71. APIMASK_ECC_UE_H = CPC925_BIT(8), /* UECC upper */
  72. APIMASK_ECC_CE_H = CPC925_BIT(9), /* CECC upper */
  73. APIMASK_ECC_UE_L = CPC925_BIT(10), /* UECC lower */
  74. APIMASK_ECC_CE_L = CPC925_BIT(11), /* CECC lower */
  75. CPU_MASK_ENABLE = (APIMASK_DART | APIMASK_ADI0 | APIMASK_ADI1 |
  76. APIMASK_STAT | APIMASK_DERR | APIMASK_ADRS0 |
  77. APIMASK_ADRS1),
  78. ECC_MASK_ENABLE = (APIMASK_ECC_UE_H | APIMASK_ECC_CE_H |
  79. APIMASK_ECC_UE_L | APIMASK_ECC_CE_L),
  80. };
  81. /************************************************************
  82. * Processor Interface Exception Register (APIEXCP)
  83. ************************************************************/
  84. #define REG_APIEXCP_OFFSET 0x30060
  85. enum apiexcp_bits {
  86. APIEXCP_DART = CPC925_BIT(0), /* DART Exception */
  87. APIEXCP_ADI0 = CPC925_BIT(1), /* Handshake Error on PI0_ADI */
  88. APIEXCP_ADI1 = CPC925_BIT(2), /* Handshake Error on PI1_ADI */
  89. APIEXCP_STAT = CPC925_BIT(3), /* Status Exception */
  90. APIEXCP_DERR = CPC925_BIT(4), /* Data Error Exception */
  91. APIEXCP_ADRS0 = CPC925_BIT(5), /* Addressing Exception on PI0 */
  92. APIEXCP_ADRS1 = CPC925_BIT(6), /* Addressing Exception on PI1 */
  93. /* BIT(7) Reserved */
  94. APIEXCP_ECC_UE_H = CPC925_BIT(8), /* UECC upper */
  95. APIEXCP_ECC_CE_H = CPC925_BIT(9), /* CECC upper */
  96. APIEXCP_ECC_UE_L = CPC925_BIT(10), /* UECC lower */
  97. APIEXCP_ECC_CE_L = CPC925_BIT(11), /* CECC lower */
  98. CPU_EXCP_DETECTED = (APIEXCP_DART | APIEXCP_ADI0 | APIEXCP_ADI1 |
  99. APIEXCP_STAT | APIEXCP_DERR | APIEXCP_ADRS0 |
  100. APIEXCP_ADRS1),
  101. UECC_EXCP_DETECTED = (APIEXCP_ECC_UE_H | APIEXCP_ECC_UE_L),
  102. CECC_EXCP_DETECTED = (APIEXCP_ECC_CE_H | APIEXCP_ECC_CE_L),
  103. ECC_EXCP_DETECTED = (UECC_EXCP_DETECTED | CECC_EXCP_DETECTED),
  104. };
  105. /************************************************************
  106. * Memory Bus Configuration Register (MBCR)
  107. ************************************************************/
  108. #define REG_MBCR_OFFSET 0x2190
  109. #define MBCR_64BITCFG_SHIFT 23
  110. #define MBCR_64BITCFG_MASK (1UL << MBCR_64BITCFG_SHIFT)
  111. #define MBCR_64BITBUS_SHIFT 22
  112. #define MBCR_64BITBUS_MASK (1UL << MBCR_64BITBUS_SHIFT)
  113. /************************************************************
  114. * Memory Bank Mode Register (MBMR)
  115. ************************************************************/
  116. #define REG_MBMR_OFFSET 0x21C0
  117. #define MBMR_MODE_MAX_VALUE 0xF
  118. #define MBMR_MODE_SHIFT 25
  119. #define MBMR_MODE_MASK (MBMR_MODE_MAX_VALUE << MBMR_MODE_SHIFT)
  120. #define MBMR_BBA_SHIFT 24
  121. #define MBMR_BBA_MASK (1UL << MBMR_BBA_SHIFT)
  122. /************************************************************
  123. * Memory Bank Boundary Address Register (MBBAR)
  124. ************************************************************/
  125. #define REG_MBBAR_OFFSET 0x21D0
  126. #define MBBAR_BBA_MAX_VALUE 0xFF
  127. #define MBBAR_BBA_SHIFT 24
  128. #define MBBAR_BBA_MASK (MBBAR_BBA_MAX_VALUE << MBBAR_BBA_SHIFT)
  129. /************************************************************
  130. * Memory Scrub Control Register (MSCR)
  131. ************************************************************/
  132. #define REG_MSCR_OFFSET 0x2400
  133. #define MSCR_SCRUB_MOD_MASK 0xC0000000 /* scrub_mod - bit0:1*/
  134. #define MSCR_BACKGR_SCRUB 0x40000000 /* 01 */
  135. #define MSCR_SI_SHIFT 16 /* si - bit8:15*/
  136. #define MSCR_SI_MAX_VALUE 0xFF
  137. #define MSCR_SI_MASK (MSCR_SI_MAX_VALUE << MSCR_SI_SHIFT)
  138. /************************************************************
  139. * Memory Scrub Range Start Register (MSRSR)
  140. ************************************************************/
  141. #define REG_MSRSR_OFFSET 0x2410
  142. /************************************************************
  143. * Memory Scrub Range End Register (MSRER)
  144. ************************************************************/
  145. #define REG_MSRER_OFFSET 0x2420
  146. /************************************************************
  147. * Memory Scrub Pattern Register (MSPR)
  148. ************************************************************/
  149. #define REG_MSPR_OFFSET 0x2430
  150. /************************************************************
  151. * Memory Check Control Register (MCCR)
  152. ************************************************************/
  153. #define REG_MCCR_OFFSET 0x2440
  154. enum mccr_bits {
  155. MCCR_ECC_EN = CPC925_BIT(0), /* ECC high and low check */
  156. };
  157. /************************************************************
  158. * Memory Check Range End Register (MCRER)
  159. ************************************************************/
  160. #define REG_MCRER_OFFSET 0x2450
  161. /************************************************************
  162. * Memory Error Address Register (MEAR)
  163. ************************************************************/
  164. #define REG_MEAR_OFFSET 0x2460
  165. #define MEAR_BCNT_MAX_VALUE 0x3
  166. #define MEAR_BCNT_SHIFT 30
  167. #define MEAR_BCNT_MASK (MEAR_BCNT_MAX_VALUE << MEAR_BCNT_SHIFT)
  168. #define MEAR_RANK_MAX_VALUE 0x7
  169. #define MEAR_RANK_SHIFT 27
  170. #define MEAR_RANK_MASK (MEAR_RANK_MAX_VALUE << MEAR_RANK_SHIFT)
  171. #define MEAR_COL_MAX_VALUE 0x7FF
  172. #define MEAR_COL_SHIFT 16
  173. #define MEAR_COL_MASK (MEAR_COL_MAX_VALUE << MEAR_COL_SHIFT)
  174. #define MEAR_BANK_MAX_VALUE 0x3
  175. #define MEAR_BANK_SHIFT 14
  176. #define MEAR_BANK_MASK (MEAR_BANK_MAX_VALUE << MEAR_BANK_SHIFT)
  177. #define MEAR_ROW_MASK 0x00003FFF
  178. /************************************************************
  179. * Memory Error Syndrome Register (MESR)
  180. ************************************************************/
  181. #define REG_MESR_OFFSET 0x2470
  182. #define MESR_ECC_SYN_H_MASK 0xFF00
  183. #define MESR_ECC_SYN_L_MASK 0x00FF
  184. /************************************************************
  185. * Memory Mode Control Register (MMCR)
  186. ************************************************************/
  187. #define REG_MMCR_OFFSET 0x2500
  188. enum mmcr_bits {
  189. MMCR_REG_DIMM_MODE = CPC925_BIT(3),
  190. };
  191. /*
  192. * HyperTransport Link Registers
  193. */
  194. /************************************************************
  195. * Error Handling/Enumeration Scratch Pad Register (ERRCTRL)
  196. ************************************************************/
  197. #define REG_ERRCTRL_OFFSET 0x70140
  198. enum errctrl_bits { /* nonfatal interrupts for */
  199. ERRCTRL_SERR_NF = CPC925_BIT(0), /* system error */
  200. ERRCTRL_CRC_NF = CPC925_BIT(1), /* CRC error */
  201. ERRCTRL_RSP_NF = CPC925_BIT(2), /* Response error */
  202. ERRCTRL_EOC_NF = CPC925_BIT(3), /* End-Of-Chain error */
  203. ERRCTRL_OVF_NF = CPC925_BIT(4), /* Overflow error */
  204. ERRCTRL_PROT_NF = CPC925_BIT(5), /* Protocol error */
  205. ERRCTRL_RSP_ERR = CPC925_BIT(6), /* Response error received */
  206. ERRCTRL_CHN_FAL = CPC925_BIT(7), /* Sync flooding detected */
  207. HT_ERRCTRL_ENABLE = (ERRCTRL_SERR_NF | ERRCTRL_CRC_NF |
  208. ERRCTRL_RSP_NF | ERRCTRL_EOC_NF |
  209. ERRCTRL_OVF_NF | ERRCTRL_PROT_NF),
  210. HT_ERRCTRL_DETECTED = (ERRCTRL_RSP_ERR | ERRCTRL_CHN_FAL),
  211. };
  212. /************************************************************
  213. * Link Configuration and Link Control Register (LINKCTRL)
  214. ************************************************************/
  215. #define REG_LINKCTRL_OFFSET 0x70110
  216. enum linkctrl_bits {
  217. LINKCTRL_CRC_ERR = (CPC925_BIT(22) | CPC925_BIT(23)),
  218. LINKCTRL_LINK_FAIL = CPC925_BIT(27),
  219. HT_LINKCTRL_DETECTED = (LINKCTRL_CRC_ERR | LINKCTRL_LINK_FAIL),
  220. };
  221. /************************************************************
  222. * Link FreqCap/Error/Freq/Revision ID Register (LINKERR)
  223. ************************************************************/
  224. #define REG_LINKERR_OFFSET 0x70120
  225. enum linkerr_bits {
  226. LINKERR_EOC_ERR = CPC925_BIT(17), /* End-Of-Chain error */
  227. LINKERR_OVF_ERR = CPC925_BIT(18), /* Receive Buffer Overflow */
  228. LINKERR_PROT_ERR = CPC925_BIT(19), /* Protocol error */
  229. HT_LINKERR_DETECTED = (LINKERR_EOC_ERR | LINKERR_OVF_ERR |
  230. LINKERR_PROT_ERR),
  231. };
  232. /************************************************************
  233. * Bridge Control Register (BRGCTRL)
  234. ************************************************************/
  235. #define REG_BRGCTRL_OFFSET 0x70300
  236. enum brgctrl_bits {
  237. BRGCTRL_DETSERR = CPC925_BIT(0), /* SERR on Secondary Bus */
  238. BRGCTRL_SECBUSRESET = CPC925_BIT(9), /* Secondary Bus Reset */
  239. };
  240. /* Private structure for edac memory controller */
  241. struct cpc925_mc_pdata {
  242. void __iomem *vbase;
  243. unsigned long total_mem;
  244. const char *name;
  245. int edac_idx;
  246. };
  247. /* Private structure for common edac device */
  248. struct cpc925_dev_info {
  249. void __iomem *vbase;
  250. struct platform_device *pdev;
  251. char *ctl_name;
  252. int edac_idx;
  253. struct edac_device_ctl_info *edac_dev;
  254. void (*init)(struct cpc925_dev_info *dev_info);
  255. void (*exit)(struct cpc925_dev_info *dev_info);
  256. void (*check)(struct edac_device_ctl_info *edac_dev);
  257. };
  258. /* Get total memory size from Open Firmware DTB */
  259. static void get_total_mem(struct cpc925_mc_pdata *pdata)
  260. {
  261. struct device_node *np = NULL;
  262. const unsigned int *reg, *reg_end;
  263. int len, sw, aw;
  264. unsigned long start, size;
  265. np = of_find_node_by_type(NULL, "memory");
  266. if (!np)
  267. return;
  268. aw = of_n_addr_cells(np);
  269. sw = of_n_size_cells(np);
  270. reg = (const unsigned int *)of_get_property(np, "reg", &len);
  271. reg_end = reg + len/4;
  272. pdata->total_mem = 0;
  273. do {
  274. start = of_read_number(reg, aw);
  275. reg += aw;
  276. size = of_read_number(reg, sw);
  277. reg += sw;
  278. debugf1("%s: start 0x%lx, size 0x%lx\n", __func__,
  279. start, size);
  280. pdata->total_mem += size;
  281. } while (reg < reg_end);
  282. of_node_put(np);
  283. debugf0("%s: total_mem 0x%lx\n", __func__, pdata->total_mem);
  284. }
  285. static void cpc925_init_csrows(struct mem_ctl_info *mci)
  286. {
  287. struct cpc925_mc_pdata *pdata = mci->pvt_info;
  288. struct csrow_info *csrow;
  289. int index;
  290. u32 mbmr, mbbar, bba;
  291. unsigned long row_size, last_nr_pages = 0;
  292. get_total_mem(pdata);
  293. for (index = 0; index < mci->nr_csrows; index++) {
  294. mbmr = __raw_readl(pdata->vbase + REG_MBMR_OFFSET +
  295. 0x20 * index);
  296. mbbar = __raw_readl(pdata->vbase + REG_MBBAR_OFFSET +
  297. 0x20 + index);
  298. bba = (((mbmr & MBMR_BBA_MASK) >> MBMR_BBA_SHIFT) << 8) |
  299. ((mbbar & MBBAR_BBA_MASK) >> MBBAR_BBA_SHIFT);
  300. if (bba == 0)
  301. continue; /* not populated */
  302. csrow = &mci->csrows[index];
  303. row_size = bba * (1UL << 28); /* 256M */
  304. csrow->first_page = last_nr_pages;
  305. csrow->nr_pages = row_size >> PAGE_SHIFT;
  306. csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
  307. last_nr_pages = csrow->last_page + 1;
  308. csrow->mtype = MEM_RDDR;
  309. csrow->edac_mode = EDAC_SECDED;
  310. switch (csrow->nr_channels) {
  311. case 1: /* Single channel */
  312. csrow->grain = 32; /* four-beat burst of 32 bytes */
  313. break;
  314. case 2: /* Dual channel */
  315. default:
  316. csrow->grain = 64; /* four-beat burst of 64 bytes */
  317. break;
  318. }
  319. switch ((mbmr & MBMR_MODE_MASK) >> MBMR_MODE_SHIFT) {
  320. case 6: /* 0110, no way to differentiate X8 VS X16 */
  321. case 5: /* 0101 */
  322. case 8: /* 1000 */
  323. csrow->dtype = DEV_X16;
  324. break;
  325. case 7: /* 0111 */
  326. case 9: /* 1001 */
  327. csrow->dtype = DEV_X8;
  328. break;
  329. default:
  330. csrow->dtype = DEV_UNKNOWN;
  331. break;
  332. }
  333. }
  334. }
  335. /* Enable memory controller ECC detection */
  336. static void cpc925_mc_init(struct mem_ctl_info *mci)
  337. {
  338. struct cpc925_mc_pdata *pdata = mci->pvt_info;
  339. u32 apimask;
  340. u32 mccr;
  341. /* Enable various ECC error exceptions */
  342. apimask = __raw_readl(pdata->vbase + REG_APIMASK_OFFSET);
  343. if ((apimask & ECC_MASK_ENABLE) == 0) {
  344. apimask |= ECC_MASK_ENABLE;
  345. __raw_writel(apimask, pdata->vbase + REG_APIMASK_OFFSET);
  346. }
  347. /* Enable ECC detection */
  348. mccr = __raw_readl(pdata->vbase + REG_MCCR_OFFSET);
  349. if ((mccr & MCCR_ECC_EN) == 0) {
  350. mccr |= MCCR_ECC_EN;
  351. __raw_writel(mccr, pdata->vbase + REG_MCCR_OFFSET);
  352. }
  353. }
  354. /* Disable memory controller ECC detection */
  355. static void cpc925_mc_exit(struct mem_ctl_info *mci)
  356. {
  357. /*
  358. * WARNING:
  359. * We are supposed to clear the ECC error detection bits,
  360. * and it will be no problem to do so. However, once they
  361. * are cleared here if we want to re-install CPC925 EDAC
  362. * module later, setting them up in cpc925_mc_init() will
  363. * trigger machine check exception.
  364. * Also, it's ok to leave ECC error detection bits enabled,
  365. * since they are reset to 1 by default or by boot loader.
  366. */
  367. return;
  368. }
  369. /*
  370. * Revert DDR column/row/bank addresses into page frame number and
  371. * offset in page.
  372. *
  373. * Suppose memory mode is 0x0111(128-bit mode, identical DIMM pairs),
  374. * physical address(PA) bits to column address(CA) bits mappings are:
  375. * CA 0 1 2 3 4 5 6 7 8 9 10
  376. * PA 59 58 57 56 55 54 53 52 51 50 49
  377. *
  378. * physical address(PA) bits to bank address(BA) bits mappings are:
  379. * BA 0 1
  380. * PA 43 44
  381. *
  382. * physical address(PA) bits to row address(RA) bits mappings are:
  383. * RA 0 1 2 3 4 5 6 7 8 9 10 11 12
  384. * PA 36 35 34 48 47 46 45 40 41 42 39 38 37
  385. */
  386. static void cpc925_mc_get_pfn(struct mem_ctl_info *mci, u32 mear,
  387. unsigned long *pfn, unsigned long *offset, int *csrow)
  388. {
  389. u32 bcnt, rank, col, bank, row;
  390. u32 c;
  391. unsigned long pa;
  392. int i;
  393. bcnt = (mear & MEAR_BCNT_MASK) >> MEAR_BCNT_SHIFT;
  394. rank = (mear & MEAR_RANK_MASK) >> MEAR_RANK_SHIFT;
  395. col = (mear & MEAR_COL_MASK) >> MEAR_COL_SHIFT;
  396. bank = (mear & MEAR_BANK_MASK) >> MEAR_BANK_SHIFT;
  397. row = mear & MEAR_ROW_MASK;
  398. *csrow = rank;
  399. #ifdef CONFIG_EDAC_DEBUG
  400. if (mci->csrows[rank].first_page == 0) {
  401. cpc925_mc_printk(mci, KERN_ERR, "ECC occurs in a "
  402. "non-populated csrow, broken hardware?\n");
  403. return;
  404. }
  405. #endif
  406. /* Revert csrow number */
  407. pa = mci->csrows[rank].first_page << PAGE_SHIFT;
  408. /* Revert column address */
  409. col += bcnt;
  410. for (i = 0; i < 11; i++) {
  411. c = col & 0x1;
  412. col >>= 1;
  413. pa |= c << (14 - i);
  414. }
  415. /* Revert bank address */
  416. pa |= bank << 19;
  417. /* Revert row address, in 4 steps */
  418. for (i = 0; i < 3; i++) {
  419. c = row & 0x1;
  420. row >>= 1;
  421. pa |= c << (26 - i);
  422. }
  423. for (i = 0; i < 3; i++) {
  424. c = row & 0x1;
  425. row >>= 1;
  426. pa |= c << (21 + i);
  427. }
  428. for (i = 0; i < 4; i++) {
  429. c = row & 0x1;
  430. row >>= 1;
  431. pa |= c << (18 - i);
  432. }
  433. for (i = 0; i < 3; i++) {
  434. c = row & 0x1;
  435. row >>= 1;
  436. pa |= c << (29 - i);
  437. }
  438. *offset = pa & (PAGE_SIZE - 1);
  439. *pfn = pa >> PAGE_SHIFT;
  440. debugf0("%s: ECC physical address 0x%lx\n", __func__, pa);
  441. }
  442. static int cpc925_mc_find_channel(struct mem_ctl_info *mci, u16 syndrome)
  443. {
  444. if ((syndrome & MESR_ECC_SYN_H_MASK) == 0)
  445. return 0;
  446. if ((syndrome & MESR_ECC_SYN_L_MASK) == 0)
  447. return 1;
  448. cpc925_mc_printk(mci, KERN_INFO, "Unexpected syndrome value: 0x%x\n",
  449. syndrome);
  450. return 1;
  451. }
  452. /* Check memory controller registers for ECC errors */
  453. static void cpc925_mc_check(struct mem_ctl_info *mci)
  454. {
  455. struct cpc925_mc_pdata *pdata = mci->pvt_info;
  456. u32 apiexcp;
  457. u32 mear;
  458. u32 mesr;
  459. u16 syndrome;
  460. unsigned long pfn = 0, offset = 0;
  461. int csrow = 0, channel = 0;
  462. /* APIEXCP is cleared when read */
  463. apiexcp = __raw_readl(pdata->vbase + REG_APIEXCP_OFFSET);
  464. if ((apiexcp & ECC_EXCP_DETECTED) == 0)
  465. return;
  466. mesr = __raw_readl(pdata->vbase + REG_MESR_OFFSET);
  467. syndrome = mesr | (MESR_ECC_SYN_H_MASK | MESR_ECC_SYN_L_MASK);
  468. mear = __raw_readl(pdata->vbase + REG_MEAR_OFFSET);
  469. /* Revert column/row addresses into page frame number, etc */
  470. cpc925_mc_get_pfn(mci, mear, &pfn, &offset, &csrow);
  471. if (apiexcp & CECC_EXCP_DETECTED) {
  472. cpc925_mc_printk(mci, KERN_INFO, "DRAM CECC Fault\n");
  473. channel = cpc925_mc_find_channel(mci, syndrome);
  474. edac_mc_handle_ce(mci, pfn, offset, syndrome,
  475. csrow, channel, mci->ctl_name);
  476. }
  477. if (apiexcp & UECC_EXCP_DETECTED) {
  478. cpc925_mc_printk(mci, KERN_INFO, "DRAM UECC Fault\n");
  479. edac_mc_handle_ue(mci, pfn, offset, csrow, mci->ctl_name);
  480. }
  481. cpc925_mc_printk(mci, KERN_INFO, "Dump registers:\n");
  482. cpc925_mc_printk(mci, KERN_INFO, "APIMASK 0x%08x\n",
  483. __raw_readl(pdata->vbase + REG_APIMASK_OFFSET));
  484. cpc925_mc_printk(mci, KERN_INFO, "APIEXCP 0x%08x\n",
  485. apiexcp);
  486. cpc925_mc_printk(mci, KERN_INFO, "Mem Scrub Ctrl 0x%08x\n",
  487. __raw_readl(pdata->vbase + REG_MSCR_OFFSET));
  488. cpc925_mc_printk(mci, KERN_INFO, "Mem Scrub Rge Start 0x%08x\n",
  489. __raw_readl(pdata->vbase + REG_MSRSR_OFFSET));
  490. cpc925_mc_printk(mci, KERN_INFO, "Mem Scrub Rge End 0x%08x\n",
  491. __raw_readl(pdata->vbase + REG_MSRER_OFFSET));
  492. cpc925_mc_printk(mci, KERN_INFO, "Mem Scrub Pattern 0x%08x\n",
  493. __raw_readl(pdata->vbase + REG_MSPR_OFFSET));
  494. cpc925_mc_printk(mci, KERN_INFO, "Mem Chk Ctrl 0x%08x\n",
  495. __raw_readl(pdata->vbase + REG_MCCR_OFFSET));
  496. cpc925_mc_printk(mci, KERN_INFO, "Mem Chk Rge End 0x%08x\n",
  497. __raw_readl(pdata->vbase + REG_MCRER_OFFSET));
  498. cpc925_mc_printk(mci, KERN_INFO, "Mem Err Address 0x%08x\n",
  499. mesr);
  500. cpc925_mc_printk(mci, KERN_INFO, "Mem Err Syndrome 0x%08x\n",
  501. syndrome);
  502. }
  503. /******************** CPU err device********************************/
  504. /* Enable CPU Errors detection */
  505. static void cpc925_cpu_init(struct cpc925_dev_info *dev_info)
  506. {
  507. u32 apimask;
  508. apimask = __raw_readl(dev_info->vbase + REG_APIMASK_OFFSET);
  509. if ((apimask & CPU_MASK_ENABLE) == 0) {
  510. apimask |= CPU_MASK_ENABLE;
  511. __raw_writel(apimask, dev_info->vbase + REG_APIMASK_OFFSET);
  512. }
  513. }
  514. /* Disable CPU Errors detection */
  515. static void cpc925_cpu_exit(struct cpc925_dev_info *dev_info)
  516. {
  517. /*
  518. * WARNING:
  519. * We are supposed to clear the CPU error detection bits,
  520. * and it will be no problem to do so. However, once they
  521. * are cleared here if we want to re-install CPC925 EDAC
  522. * module later, setting them up in cpc925_cpu_init() will
  523. * trigger machine check exception.
  524. * Also, it's ok to leave CPU error detection bits enabled,
  525. * since they are reset to 1 by default.
  526. */
  527. return;
  528. }
  529. /* Check for CPU Errors */
  530. static void cpc925_cpu_check(struct edac_device_ctl_info *edac_dev)
  531. {
  532. struct cpc925_dev_info *dev_info = edac_dev->pvt_info;
  533. u32 apiexcp;
  534. u32 apimask;
  535. /* APIEXCP is cleared when read */
  536. apiexcp = __raw_readl(dev_info->vbase + REG_APIEXCP_OFFSET);
  537. if ((apiexcp & CPU_EXCP_DETECTED) == 0)
  538. return;
  539. apimask = __raw_readl(dev_info->vbase + REG_APIMASK_OFFSET);
  540. cpc925_printk(KERN_INFO, "Processor Interface Fault\n"
  541. "Processor Interface register dump:\n");
  542. cpc925_printk(KERN_INFO, "APIMASK 0x%08x\n", apimask);
  543. cpc925_printk(KERN_INFO, "APIEXCP 0x%08x\n", apiexcp);
  544. edac_device_handle_ue(edac_dev, 0, 0, edac_dev->ctl_name);
  545. }
  546. /******************** HT Link err device****************************/
  547. /* Enable HyperTransport Link Error detection */
  548. static void cpc925_htlink_init(struct cpc925_dev_info *dev_info)
  549. {
  550. u32 ht_errctrl;
  551. ht_errctrl = __raw_readl(dev_info->vbase + REG_ERRCTRL_OFFSET);
  552. if ((ht_errctrl & HT_ERRCTRL_ENABLE) == 0) {
  553. ht_errctrl |= HT_ERRCTRL_ENABLE;
  554. __raw_writel(ht_errctrl, dev_info->vbase + REG_ERRCTRL_OFFSET);
  555. }
  556. }
  557. /* Disable HyperTransport Link Error detection */
  558. static void cpc925_htlink_exit(struct cpc925_dev_info *dev_info)
  559. {
  560. u32 ht_errctrl;
  561. ht_errctrl = __raw_readl(dev_info->vbase + REG_ERRCTRL_OFFSET);
  562. ht_errctrl &= ~HT_ERRCTRL_ENABLE;
  563. __raw_writel(ht_errctrl, dev_info->vbase + REG_ERRCTRL_OFFSET);
  564. }
  565. /* Check for HyperTransport Link errors */
  566. static void cpc925_htlink_check(struct edac_device_ctl_info *edac_dev)
  567. {
  568. struct cpc925_dev_info *dev_info = edac_dev->pvt_info;
  569. u32 brgctrl = __raw_readl(dev_info->vbase + REG_BRGCTRL_OFFSET);
  570. u32 linkctrl = __raw_readl(dev_info->vbase + REG_LINKCTRL_OFFSET);
  571. u32 errctrl = __raw_readl(dev_info->vbase + REG_ERRCTRL_OFFSET);
  572. u32 linkerr = __raw_readl(dev_info->vbase + REG_LINKERR_OFFSET);
  573. if (!((brgctrl & BRGCTRL_DETSERR) ||
  574. (linkctrl & HT_LINKCTRL_DETECTED) ||
  575. (errctrl & HT_ERRCTRL_DETECTED) ||
  576. (linkerr & HT_LINKERR_DETECTED)))
  577. return;
  578. cpc925_printk(KERN_INFO, "HT Link Fault\n"
  579. "HT register dump:\n");
  580. cpc925_printk(KERN_INFO, "Bridge Ctrl 0x%08x\n",
  581. brgctrl);
  582. cpc925_printk(KERN_INFO, "Link Config Ctrl 0x%08x\n",
  583. linkctrl);
  584. cpc925_printk(KERN_INFO, "Error Enum and Ctrl 0x%08x\n",
  585. errctrl);
  586. cpc925_printk(KERN_INFO, "Link Error 0x%08x\n",
  587. linkerr);
  588. /* Clear by write 1 */
  589. if (brgctrl & BRGCTRL_DETSERR)
  590. __raw_writel(BRGCTRL_DETSERR,
  591. dev_info->vbase + REG_BRGCTRL_OFFSET);
  592. if (linkctrl & HT_LINKCTRL_DETECTED)
  593. __raw_writel(HT_LINKCTRL_DETECTED,
  594. dev_info->vbase + REG_LINKCTRL_OFFSET);
  595. /* Initiate Secondary Bus Reset to clear the chain failure */
  596. if (errctrl & ERRCTRL_CHN_FAL)
  597. __raw_writel(BRGCTRL_SECBUSRESET,
  598. dev_info->vbase + REG_BRGCTRL_OFFSET);
  599. if (errctrl & ERRCTRL_RSP_ERR)
  600. __raw_writel(ERRCTRL_RSP_ERR,
  601. dev_info->vbase + REG_ERRCTRL_OFFSET);
  602. if (linkerr & HT_LINKERR_DETECTED)
  603. __raw_writel(HT_LINKERR_DETECTED,
  604. dev_info->vbase + REG_LINKERR_OFFSET);
  605. edac_device_handle_ce(edac_dev, 0, 0, edac_dev->ctl_name);
  606. }
  607. static struct cpc925_dev_info cpc925_devs[] = {
  608. {
  609. .ctl_name = CPC925_CPU_ERR_DEV,
  610. .init = cpc925_cpu_init,
  611. .exit = cpc925_cpu_exit,
  612. .check = cpc925_cpu_check,
  613. },
  614. {
  615. .ctl_name = CPC925_HT_LINK_DEV,
  616. .init = cpc925_htlink_init,
  617. .exit = cpc925_htlink_exit,
  618. .check = cpc925_htlink_check,
  619. },
  620. {0}, /* Terminated by NULL */
  621. };
  622. /*
  623. * Add CPU Err detection and HyperTransport Link Err detection
  624. * as common "edac_device", they have no corresponding device
  625. * nodes in the Open Firmware DTB and we have to add platform
  626. * devices for them. Also, they will share the MMIO with that
  627. * of memory controller.
  628. */
  629. static void cpc925_add_edac_devices(void __iomem *vbase)
  630. {
  631. struct cpc925_dev_info *dev_info;
  632. if (!vbase) {
  633. cpc925_printk(KERN_ERR, "MMIO not established yet\n");
  634. return;
  635. }
  636. for (dev_info = &cpc925_devs[0]; dev_info->init; dev_info++) {
  637. dev_info->vbase = vbase;
  638. dev_info->pdev = platform_device_register_simple(
  639. dev_info->ctl_name, 0, NULL, 0);
  640. if (IS_ERR(dev_info->pdev)) {
  641. cpc925_printk(KERN_ERR,
  642. "Can't register platform device for %s\n",
  643. dev_info->ctl_name);
  644. continue;
  645. }
  646. /*
  647. * Don't have to allocate private structure but
  648. * make use of cpc925_devs[] instead.
  649. */
  650. dev_info->edac_idx = edac_device_alloc_index();
  651. dev_info->edac_dev =
  652. edac_device_alloc_ctl_info(0, dev_info->ctl_name,
  653. 1, NULL, 0, 0, NULL, 0, dev_info->edac_idx);
  654. if (!dev_info->edac_dev) {
  655. cpc925_printk(KERN_ERR, "No memory for edac device\n");
  656. goto err1;
  657. }
  658. dev_info->edac_dev->pvt_info = dev_info;
  659. dev_info->edac_dev->dev = &dev_info->pdev->dev;
  660. dev_info->edac_dev->ctl_name = dev_info->ctl_name;
  661. dev_info->edac_dev->mod_name = CPC925_EDAC_MOD_STR;
  662. dev_info->edac_dev->dev_name = dev_name(&dev_info->pdev->dev);
  663. if (edac_op_state == EDAC_OPSTATE_POLL)
  664. dev_info->edac_dev->edac_check = dev_info->check;
  665. if (dev_info->init)
  666. dev_info->init(dev_info);
  667. if (edac_device_add_device(dev_info->edac_dev) > 0) {
  668. cpc925_printk(KERN_ERR,
  669. "Unable to add edac device for %s\n",
  670. dev_info->ctl_name);
  671. goto err2;
  672. }
  673. debugf0("%s: Successfully added edac device for %s\n",
  674. __func__, dev_info->ctl_name);
  675. continue;
  676. err2:
  677. if (dev_info->exit)
  678. dev_info->exit(dev_info);
  679. edac_device_free_ctl_info(dev_info->edac_dev);
  680. err1:
  681. platform_device_unregister(dev_info->pdev);
  682. }
  683. }
  684. /*
  685. * Delete the common "edac_device" for CPU Err Detection
  686. * and HyperTransport Link Err Detection
  687. */
  688. static void cpc925_del_edac_devices(void)
  689. {
  690. struct cpc925_dev_info *dev_info;
  691. for (dev_info = &cpc925_devs[0]; dev_info->init; dev_info++) {
  692. if (dev_info->edac_dev) {
  693. edac_device_del_device(dev_info->edac_dev->dev);
  694. edac_device_free_ctl_info(dev_info->edac_dev);
  695. platform_device_unregister(dev_info->pdev);
  696. }
  697. if (dev_info->exit)
  698. dev_info->exit(dev_info);
  699. debugf0("%s: Successfully deleted edac device for %s\n",
  700. __func__, dev_info->ctl_name);
  701. }
  702. }
  703. /* Convert current back-ground scrub rate into byte/sec bandwith */
  704. static int cpc925_get_sdram_scrub_rate(struct mem_ctl_info *mci, u32 *bw)
  705. {
  706. struct cpc925_mc_pdata *pdata = mci->pvt_info;
  707. u32 mscr;
  708. u8 si;
  709. mscr = __raw_readl(pdata->vbase + REG_MSCR_OFFSET);
  710. si = (mscr & MSCR_SI_MASK) >> MSCR_SI_SHIFT;
  711. debugf0("%s, Mem Scrub Ctrl Register 0x%x\n", __func__, mscr);
  712. if (((mscr & MSCR_SCRUB_MOD_MASK) != MSCR_BACKGR_SCRUB) ||
  713. (si == 0)) {
  714. cpc925_mc_printk(mci, KERN_INFO, "Scrub mode not enabled\n");
  715. *bw = 0;
  716. } else
  717. *bw = CPC925_SCRUB_BLOCK_SIZE * 0xFA67 / si;
  718. return 0;
  719. }
  720. /* Return 0 for single channel; 1 for dual channel */
  721. static int cpc925_mc_get_channels(void __iomem *vbase)
  722. {
  723. int dual = 0;
  724. u32 mbcr;
  725. mbcr = __raw_readl(vbase + REG_MBCR_OFFSET);
  726. /*
  727. * Dual channel only when 128-bit wide physical bus
  728. * and 128-bit configuration.
  729. */
  730. if (((mbcr & MBCR_64BITCFG_MASK) == 0) &&
  731. ((mbcr & MBCR_64BITBUS_MASK) == 0))
  732. dual = 1;
  733. debugf0("%s: %s channel\n", __func__,
  734. (dual > 0) ? "Dual" : "Single");
  735. return dual;
  736. }
  737. static int __devinit cpc925_probe(struct platform_device *pdev)
  738. {
  739. static int edac_mc_idx;
  740. struct mem_ctl_info *mci;
  741. void __iomem *vbase;
  742. struct cpc925_mc_pdata *pdata;
  743. struct resource *r;
  744. int res = 0, nr_channels;
  745. debugf0("%s: %s platform device found!\n", __func__, pdev->name);
  746. if (!devres_open_group(&pdev->dev, cpc925_probe, GFP_KERNEL)) {
  747. res = -ENOMEM;
  748. goto out;
  749. }
  750. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  751. if (!r) {
  752. cpc925_printk(KERN_ERR, "Unable to get resource\n");
  753. res = -ENOENT;
  754. goto err1;
  755. }
  756. if (!devm_request_mem_region(&pdev->dev,
  757. r->start,
  758. resource_size(r),
  759. pdev->name)) {
  760. cpc925_printk(KERN_ERR, "Unable to request mem region\n");
  761. res = -EBUSY;
  762. goto err1;
  763. }
  764. vbase = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  765. if (!vbase) {
  766. cpc925_printk(KERN_ERR, "Unable to ioremap device\n");
  767. res = -ENOMEM;
  768. goto err2;
  769. }
  770. nr_channels = cpc925_mc_get_channels(vbase);
  771. mci = edac_mc_alloc(sizeof(struct cpc925_mc_pdata),
  772. CPC925_NR_CSROWS, nr_channels + 1, edac_mc_idx);
  773. if (!mci) {
  774. cpc925_printk(KERN_ERR, "No memory for mem_ctl_info\n");
  775. res = -ENOMEM;
  776. goto err2;
  777. }
  778. pdata = mci->pvt_info;
  779. pdata->vbase = vbase;
  780. pdata->edac_idx = edac_mc_idx++;
  781. pdata->name = pdev->name;
  782. mci->dev = &pdev->dev;
  783. platform_set_drvdata(pdev, mci);
  784. mci->dev_name = dev_name(&pdev->dev);
  785. mci->mtype_cap = MEM_FLAG_RDDR | MEM_FLAG_DDR;
  786. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
  787. mci->edac_cap = EDAC_FLAG_SECDED;
  788. mci->mod_name = CPC925_EDAC_MOD_STR;
  789. mci->mod_ver = CPC925_EDAC_REVISION;
  790. mci->ctl_name = pdev->name;
  791. if (edac_op_state == EDAC_OPSTATE_POLL)
  792. mci->edac_check = cpc925_mc_check;
  793. mci->ctl_page_to_phys = NULL;
  794. mci->scrub_mode = SCRUB_SW_SRC;
  795. mci->set_sdram_scrub_rate = NULL;
  796. mci->get_sdram_scrub_rate = cpc925_get_sdram_scrub_rate;
  797. cpc925_init_csrows(mci);
  798. /* Setup memory controller registers */
  799. cpc925_mc_init(mci);
  800. if (edac_mc_add_mc(mci) > 0) {
  801. cpc925_mc_printk(mci, KERN_ERR, "Failed edac_mc_add_mc()\n");
  802. goto err3;
  803. }
  804. cpc925_add_edac_devices(vbase);
  805. /* get this far and it's successful */
  806. debugf0("%s: success\n", __func__);
  807. res = 0;
  808. goto out;
  809. err3:
  810. cpc925_mc_exit(mci);
  811. edac_mc_free(mci);
  812. err2:
  813. devm_release_mem_region(&pdev->dev, r->start, resource_size(r));
  814. err1:
  815. devres_release_group(&pdev->dev, cpc925_probe);
  816. out:
  817. return res;
  818. }
  819. static int cpc925_remove(struct platform_device *pdev)
  820. {
  821. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  822. /*
  823. * Delete common edac devices before edac mc, because
  824. * the former share the MMIO of the latter.
  825. */
  826. cpc925_del_edac_devices();
  827. cpc925_mc_exit(mci);
  828. edac_mc_del_mc(&pdev->dev);
  829. edac_mc_free(mci);
  830. return 0;
  831. }
  832. static struct platform_driver cpc925_edac_driver = {
  833. .probe = cpc925_probe,
  834. .remove = cpc925_remove,
  835. .driver = {
  836. .name = "cpc925_edac",
  837. }
  838. };
  839. static int __init cpc925_edac_init(void)
  840. {
  841. int ret = 0;
  842. printk(KERN_INFO "IBM CPC925 EDAC driver " CPC925_EDAC_REVISION "\n");
  843. printk(KERN_INFO "\t(c) 2008 Wind River Systems, Inc\n");
  844. /* Only support POLL mode so far */
  845. edac_op_state = EDAC_OPSTATE_POLL;
  846. ret = platform_driver_register(&cpc925_edac_driver);
  847. if (ret) {
  848. printk(KERN_WARNING "Failed to register %s\n",
  849. CPC925_EDAC_MOD_STR);
  850. }
  851. return ret;
  852. }
  853. static void __exit cpc925_edac_exit(void)
  854. {
  855. platform_driver_unregister(&cpc925_edac_driver);
  856. }
  857. module_init(cpc925_edac_init);
  858. module_exit(cpc925_edac_exit);
  859. MODULE_LICENSE("GPL");
  860. MODULE_AUTHOR("Cao Qingtao <qingtao.cao@windriver.com>");
  861. MODULE_DESCRIPTION("IBM CPC925 Bridge and MC EDAC kernel module");