ppc4xx_edac.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * Copyright (c) 2008 Nuovation System Designs, LLC
  3. * Grant Erickson <gerickson@nuovations.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; version 2 of the
  8. * License.
  9. *
  10. */
  11. #include <linux/edac.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/types.h>
  20. #include <asm/dcr.h>
  21. #include "edac_module.h"
  22. #include "ppc4xx_edac.h"
  23. /*
  24. * This file implements a driver for monitoring and handling events
  25. * associated with the IMB DDR2 ECC controller found in the AMCC/IBM
  26. * 405EX[r], 440SP, 440SPe, 460EX, 460GT and 460SX.
  27. *
  28. * As realized in the 405EX[r], this controller features:
  29. *
  30. * - Support for registered- and non-registered DDR1 and DDR2 memory.
  31. * - 32-bit or 16-bit memory interface with optional ECC.
  32. *
  33. * o ECC support includes:
  34. *
  35. * - 4-bit SEC/DED
  36. * - Aligned-nibble error detect
  37. * - Bypass mode
  38. *
  39. * - Two (2) memory banks/ranks.
  40. * - Up to 1 GiB per bank/rank in 32-bit mode and up to 512 MiB per
  41. * bank/rank in 16-bit mode.
  42. *
  43. * As realized in the 440SP and 440SPe, this controller changes/adds:
  44. *
  45. * - 64-bit or 32-bit memory interface with optional ECC.
  46. *
  47. * o ECC support includes:
  48. *
  49. * - 8-bit SEC/DED
  50. * - Aligned-nibble error detect
  51. * - Bypass mode
  52. *
  53. * - Up to 4 GiB per bank/rank in 64-bit mode and up to 2 GiB
  54. * per bank/rank in 32-bit mode.
  55. *
  56. * As realized in the 460EX and 460GT, this controller changes/adds:
  57. *
  58. * - 64-bit or 32-bit memory interface with optional ECC.
  59. *
  60. * o ECC support includes:
  61. *
  62. * - 8-bit SEC/DED
  63. * - Aligned-nibble error detect
  64. * - Bypass mode
  65. *
  66. * - Four (4) memory banks/ranks.
  67. * - Up to 16 GiB per bank/rank in 64-bit mode and up to 8 GiB
  68. * per bank/rank in 32-bit mode.
  69. *
  70. * At present, this driver has ONLY been tested against the controller
  71. * realization in the 405EX[r] on the AMCC Kilauea and Haleakala
  72. * boards (256 MiB w/o ECC memory soldered onto the board) and a
  73. * proprietary board based on those designs (128 MiB ECC memory, also
  74. * soldered onto the board).
  75. *
  76. * Dynamic feature detection and handling needs to be added for the
  77. * other realizations of this controller listed above.
  78. *
  79. * Eventually, this driver will likely be adapted to the above variant
  80. * realizations of this controller as well as broken apart to handle
  81. * the other known ECC-capable controllers prevalent in other 4xx
  82. * processors:
  83. *
  84. * - IBM SDRAM (405GP, 405CR and 405EP) "ibm,sdram-4xx"
  85. * - IBM DDR1 (440GP, 440GX, 440EP and 440GR) "ibm,sdram-4xx-ddr"
  86. * - Denali DDR1/DDR2 (440EPX and 440GRX) "denali,sdram-4xx-ddr2"
  87. *
  88. * For this controller, unfortunately, correctable errors report
  89. * nothing more than the beat/cycle and byte/lane the correction
  90. * occurred on and the check bit group that covered the error.
  91. *
  92. * In contrast, uncorrectable errors also report the failing address,
  93. * the bus master and the transaction direction (i.e. read or write)
  94. *
  95. * Regardless of whether the error is a CE or a UE, we report the
  96. * following pieces of information in the driver-unique message to the
  97. * EDAC subsystem:
  98. *
  99. * - Device tree path
  100. * - Bank(s)
  101. * - Check bit error group
  102. * - Beat(s)/lane(s)
  103. */
  104. /* Preprocessor Definitions */
  105. #define EDAC_OPSTATE_INT_STR "interrupt"
  106. #define EDAC_OPSTATE_POLL_STR "polled"
  107. #define EDAC_OPSTATE_UNKNOWN_STR "unknown"
  108. #define PPC4XX_EDAC_MODULE_NAME "ppc4xx_edac"
  109. #define PPC4XX_EDAC_MODULE_REVISION "v1.0.0"
  110. #define PPC4XX_EDAC_MESSAGE_SIZE 256
  111. /*
  112. * Kernel logging without an EDAC instance
  113. */
  114. #define ppc4xx_edac_printk(level, fmt, arg...) \
  115. edac_printk(level, "PPC4xx MC", fmt, ##arg)
  116. /*
  117. * Kernel logging with an EDAC instance
  118. */
  119. #define ppc4xx_edac_mc_printk(level, mci, fmt, arg...) \
  120. edac_mc_chipset_printk(mci, level, "PPC4xx", fmt, ##arg)
  121. /*
  122. * Macros to convert bank configuration size enumerations into MiB and
  123. * page values.
  124. */
  125. #define SDRAM_MBCF_SZ_MiB_MIN 4
  126. #define SDRAM_MBCF_SZ_TO_MiB(n) (SDRAM_MBCF_SZ_MiB_MIN \
  127. << (SDRAM_MBCF_SZ_DECODE(n)))
  128. #define SDRAM_MBCF_SZ_TO_PAGES(n) (SDRAM_MBCF_SZ_MiB_MIN \
  129. << (20 - PAGE_SHIFT + \
  130. SDRAM_MBCF_SZ_DECODE(n)))
  131. /*
  132. * The ibm,sdram-4xx-ddr2 Device Control Registers (DCRs) are
  133. * indirectly accessed and have a base and length defined by the
  134. * device tree. The base can be anything; however, we expect the
  135. * length to be precisely two registers, the first for the address
  136. * window and the second for the data window.
  137. */
  138. #define SDRAM_DCR_RESOURCE_LEN 2
  139. #define SDRAM_DCR_ADDR_OFFSET 0
  140. #define SDRAM_DCR_DATA_OFFSET 1
  141. /*
  142. * Device tree interrupt indices
  143. */
  144. #define INTMAP_ECCDED_INDEX 0 /* Double-bit Error Detect */
  145. #define INTMAP_ECCSEC_INDEX 1 /* Single-bit Error Correct */
  146. /* Type Definitions */
  147. /*
  148. * PPC4xx SDRAM memory controller private instance data
  149. */
  150. struct ppc4xx_edac_pdata {
  151. dcr_host_t dcr_host; /* Indirect DCR address/data window mapping */
  152. struct {
  153. int sec; /* Single-bit correctable error IRQ assigned */
  154. int ded; /* Double-bit detectable error IRQ assigned */
  155. } irqs;
  156. };
  157. /*
  158. * Various status data gathered and manipulated when checking and
  159. * reporting ECC status.
  160. */
  161. struct ppc4xx_ecc_status {
  162. u32 ecces;
  163. u32 besr;
  164. u32 bearh;
  165. u32 bearl;
  166. u32 wmirq;
  167. };
  168. /* Function Prototypes */
  169. static int ppc4xx_edac_probe(struct platform_device *device);
  170. static int ppc4xx_edac_remove(struct platform_device *device);
  171. /* Global Variables */
  172. /*
  173. * Device tree node type and compatible tuples this driver can match
  174. * on.
  175. */
  176. static const struct of_device_id ppc4xx_edac_match[] = {
  177. {
  178. .compatible = "ibm,sdram-4xx-ddr2"
  179. },
  180. { }
  181. };
  182. MODULE_DEVICE_TABLE(of, ppc4xx_edac_match);
  183. static struct platform_driver ppc4xx_edac_driver = {
  184. .probe = ppc4xx_edac_probe,
  185. .remove = ppc4xx_edac_remove,
  186. .driver = {
  187. .name = PPC4XX_EDAC_MODULE_NAME,
  188. .of_match_table = ppc4xx_edac_match,
  189. },
  190. };
  191. /*
  192. * TODO: The row and channel parameters likely need to be dynamically
  193. * set based on the aforementioned variant controller realizations.
  194. */
  195. static const unsigned ppc4xx_edac_nr_csrows = 2;
  196. static const unsigned ppc4xx_edac_nr_chans = 1;
  197. /*
  198. * Strings associated with PLB master IDs capable of being posted in
  199. * SDRAM_BESR or SDRAM_WMIRQ on uncorrectable ECC errors.
  200. */
  201. static const char * const ppc4xx_plb_masters[9] = {
  202. [SDRAM_PLB_M0ID_ICU] = "ICU",
  203. [SDRAM_PLB_M0ID_PCIE0] = "PCI-E 0",
  204. [SDRAM_PLB_M0ID_PCIE1] = "PCI-E 1",
  205. [SDRAM_PLB_M0ID_DMA] = "DMA",
  206. [SDRAM_PLB_M0ID_DCU] = "DCU",
  207. [SDRAM_PLB_M0ID_OPB] = "OPB",
  208. [SDRAM_PLB_M0ID_MAL] = "MAL",
  209. [SDRAM_PLB_M0ID_SEC] = "SEC",
  210. [SDRAM_PLB_M0ID_AHB] = "AHB"
  211. };
  212. /**
  213. * mfsdram - read and return controller register data
  214. * @dcr_host: A pointer to the DCR mapping.
  215. * @idcr_n: The indirect DCR register to read.
  216. *
  217. * This routine reads and returns the data associated with the
  218. * controller's specified indirect DCR register.
  219. *
  220. * Returns the read data.
  221. */
  222. static inline u32
  223. mfsdram(const dcr_host_t *dcr_host, unsigned int idcr_n)
  224. {
  225. return __mfdcri(dcr_host->base + SDRAM_DCR_ADDR_OFFSET,
  226. dcr_host->base + SDRAM_DCR_DATA_OFFSET,
  227. idcr_n);
  228. }
  229. /**
  230. * mtsdram - write controller register data
  231. * @dcr_host: A pointer to the DCR mapping.
  232. * @idcr_n: The indirect DCR register to write.
  233. * @value: The data to write.
  234. *
  235. * This routine writes the provided data to the controller's specified
  236. * indirect DCR register.
  237. */
  238. static inline void
  239. mtsdram(const dcr_host_t *dcr_host, unsigned int idcr_n, u32 value)
  240. {
  241. return __mtdcri(dcr_host->base + SDRAM_DCR_ADDR_OFFSET,
  242. dcr_host->base + SDRAM_DCR_DATA_OFFSET,
  243. idcr_n,
  244. value);
  245. }
  246. /**
  247. * ppc4xx_edac_check_bank_error - check a bank for an ECC bank error
  248. * @status: A pointer to the ECC status structure to check for an
  249. * ECC bank error.
  250. * @bank: The bank to check for an ECC error.
  251. *
  252. * This routine determines whether the specified bank has an ECC
  253. * error.
  254. *
  255. * Returns true if the specified bank has an ECC error; otherwise,
  256. * false.
  257. */
  258. static bool
  259. ppc4xx_edac_check_bank_error(const struct ppc4xx_ecc_status *status,
  260. unsigned int bank)
  261. {
  262. switch (bank) {
  263. case 0:
  264. return status->ecces & SDRAM_ECCES_BK0ER;
  265. case 1:
  266. return status->ecces & SDRAM_ECCES_BK1ER;
  267. default:
  268. return false;
  269. }
  270. }
  271. /**
  272. * ppc4xx_edac_generate_bank_message - generate interpretted bank status message
  273. * @mci: A pointer to the EDAC memory controller instance associated
  274. * with the bank message being generated.
  275. * @status: A pointer to the ECC status structure to generate the
  276. * message from.
  277. * @buffer: A pointer to the buffer in which to generate the
  278. * message.
  279. * @size: The size, in bytes, of space available in buffer.
  280. *
  281. * This routine generates to the provided buffer the portion of the
  282. * driver-unique report message associated with the ECCESS[BKNER]
  283. * field of the specified ECC status.
  284. *
  285. * Returns the number of characters generated on success; otherwise, <
  286. * 0 on error.
  287. */
  288. static int
  289. ppc4xx_edac_generate_bank_message(const struct mem_ctl_info *mci,
  290. const struct ppc4xx_ecc_status *status,
  291. char *buffer,
  292. size_t size)
  293. {
  294. int n, total = 0;
  295. unsigned int row, rows;
  296. n = snprintf(buffer, size, "%s: Banks: ", mci->dev_name);
  297. if (n < 0 || n >= size)
  298. goto fail;
  299. buffer += n;
  300. size -= n;
  301. total += n;
  302. for (rows = 0, row = 0; row < mci->nr_csrows; row++) {
  303. if (ppc4xx_edac_check_bank_error(status, row)) {
  304. n = snprintf(buffer, size, "%s%u",
  305. (rows++ ? ", " : ""), row);
  306. if (n < 0 || n >= size)
  307. goto fail;
  308. buffer += n;
  309. size -= n;
  310. total += n;
  311. }
  312. }
  313. n = snprintf(buffer, size, "%s; ", rows ? "" : "None");
  314. if (n < 0 || n >= size)
  315. goto fail;
  316. buffer += n;
  317. size -= n;
  318. total += n;
  319. fail:
  320. return total;
  321. }
  322. /**
  323. * ppc4xx_edac_generate_checkbit_message - generate interpretted checkbit message
  324. * @mci: A pointer to the EDAC memory controller instance associated
  325. * with the checkbit message being generated.
  326. * @status: A pointer to the ECC status structure to generate the
  327. * message from.
  328. * @buffer: A pointer to the buffer in which to generate the
  329. * message.
  330. * @size: The size, in bytes, of space available in buffer.
  331. *
  332. * This routine generates to the provided buffer the portion of the
  333. * driver-unique report message associated with the ECCESS[CKBER]
  334. * field of the specified ECC status.
  335. *
  336. * Returns the number of characters generated on success; otherwise, <
  337. * 0 on error.
  338. */
  339. static int
  340. ppc4xx_edac_generate_checkbit_message(const struct mem_ctl_info *mci,
  341. const struct ppc4xx_ecc_status *status,
  342. char *buffer,
  343. size_t size)
  344. {
  345. const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  346. const char *ckber = NULL;
  347. switch (status->ecces & SDRAM_ECCES_CKBER_MASK) {
  348. case SDRAM_ECCES_CKBER_NONE:
  349. ckber = "None";
  350. break;
  351. case SDRAM_ECCES_CKBER_32_ECC_0_3:
  352. ckber = "ECC0:3";
  353. break;
  354. case SDRAM_ECCES_CKBER_32_ECC_4_8:
  355. switch (mfsdram(&pdata->dcr_host, SDRAM_MCOPT1) &
  356. SDRAM_MCOPT1_WDTH_MASK) {
  357. case SDRAM_MCOPT1_WDTH_16:
  358. ckber = "ECC0:3";
  359. break;
  360. case SDRAM_MCOPT1_WDTH_32:
  361. ckber = "ECC4:8";
  362. break;
  363. default:
  364. ckber = "Unknown";
  365. break;
  366. }
  367. break;
  368. case SDRAM_ECCES_CKBER_32_ECC_0_8:
  369. ckber = "ECC0:8";
  370. break;
  371. default:
  372. ckber = "Unknown";
  373. break;
  374. }
  375. return snprintf(buffer, size, "Checkbit Error: %s", ckber);
  376. }
  377. /**
  378. * ppc4xx_edac_generate_lane_message - generate interpretted byte lane message
  379. * @mci: A pointer to the EDAC memory controller instance associated
  380. * with the byte lane message being generated.
  381. * @status: A pointer to the ECC status structure to generate the
  382. * message from.
  383. * @buffer: A pointer to the buffer in which to generate the
  384. * message.
  385. * @size: The size, in bytes, of space available in buffer.
  386. *
  387. * This routine generates to the provided buffer the portion of the
  388. * driver-unique report message associated with the ECCESS[BNCE]
  389. * field of the specified ECC status.
  390. *
  391. * Returns the number of characters generated on success; otherwise, <
  392. * 0 on error.
  393. */
  394. static int
  395. ppc4xx_edac_generate_lane_message(const struct mem_ctl_info *mci,
  396. const struct ppc4xx_ecc_status *status,
  397. char *buffer,
  398. size_t size)
  399. {
  400. int n, total = 0;
  401. unsigned int lane, lanes;
  402. const unsigned int first_lane = 0;
  403. const unsigned int lane_count = 16;
  404. n = snprintf(buffer, size, "; Byte Lane Errors: ");
  405. if (n < 0 || n >= size)
  406. goto fail;
  407. buffer += n;
  408. size -= n;
  409. total += n;
  410. for (lanes = 0, lane = first_lane; lane < lane_count; lane++) {
  411. if ((status->ecces & SDRAM_ECCES_BNCE_ENCODE(lane)) != 0) {
  412. n = snprintf(buffer, size,
  413. "%s%u",
  414. (lanes++ ? ", " : ""), lane);
  415. if (n < 0 || n >= size)
  416. goto fail;
  417. buffer += n;
  418. size -= n;
  419. total += n;
  420. }
  421. }
  422. n = snprintf(buffer, size, "%s; ", lanes ? "" : "None");
  423. if (n < 0 || n >= size)
  424. goto fail;
  425. buffer += n;
  426. size -= n;
  427. total += n;
  428. fail:
  429. return total;
  430. }
  431. /**
  432. * ppc4xx_edac_generate_ecc_message - generate interpretted ECC status message
  433. * @mci: A pointer to the EDAC memory controller instance associated
  434. * with the ECCES message being generated.
  435. * @status: A pointer to the ECC status structure to generate the
  436. * message from.
  437. * @buffer: A pointer to the buffer in which to generate the
  438. * message.
  439. * @size: The size, in bytes, of space available in buffer.
  440. *
  441. * This routine generates to the provided buffer the portion of the
  442. * driver-unique report message associated with the ECCESS register of
  443. * the specified ECC status.
  444. *
  445. * Returns the number of characters generated on success; otherwise, <
  446. * 0 on error.
  447. */
  448. static int
  449. ppc4xx_edac_generate_ecc_message(const struct mem_ctl_info *mci,
  450. const struct ppc4xx_ecc_status *status,
  451. char *buffer,
  452. size_t size)
  453. {
  454. int n, total = 0;
  455. n = ppc4xx_edac_generate_bank_message(mci, status, buffer, size);
  456. if (n < 0 || n >= size)
  457. goto fail;
  458. buffer += n;
  459. size -= n;
  460. total += n;
  461. n = ppc4xx_edac_generate_checkbit_message(mci, status, buffer, size);
  462. if (n < 0 || n >= size)
  463. goto fail;
  464. buffer += n;
  465. size -= n;
  466. total += n;
  467. n = ppc4xx_edac_generate_lane_message(mci, status, buffer, size);
  468. if (n < 0 || n >= size)
  469. goto fail;
  470. buffer += n;
  471. size -= n;
  472. total += n;
  473. fail:
  474. return total;
  475. }
  476. /**
  477. * ppc4xx_edac_generate_plb_message - generate interpretted PLB status message
  478. * @mci: A pointer to the EDAC memory controller instance associated
  479. * with the PLB message being generated.
  480. * @status: A pointer to the ECC status structure to generate the
  481. * message from.
  482. * @buffer: A pointer to the buffer in which to generate the
  483. * message.
  484. * @size: The size, in bytes, of space available in buffer.
  485. *
  486. * This routine generates to the provided buffer the portion of the
  487. * driver-unique report message associated with the PLB-related BESR
  488. * and/or WMIRQ registers of the specified ECC status.
  489. *
  490. * Returns the number of characters generated on success; otherwise, <
  491. * 0 on error.
  492. */
  493. static int
  494. ppc4xx_edac_generate_plb_message(const struct mem_ctl_info *mci,
  495. const struct ppc4xx_ecc_status *status,
  496. char *buffer,
  497. size_t size)
  498. {
  499. unsigned int master;
  500. bool read;
  501. if ((status->besr & SDRAM_BESR_MASK) == 0)
  502. return 0;
  503. if ((status->besr & SDRAM_BESR_M0ET_MASK) == SDRAM_BESR_M0ET_NONE)
  504. return 0;
  505. read = ((status->besr & SDRAM_BESR_M0RW_MASK) == SDRAM_BESR_M0RW_READ);
  506. master = SDRAM_BESR_M0ID_DECODE(status->besr);
  507. return snprintf(buffer, size,
  508. "%s error w/ PLB master %u \"%s\"; ",
  509. (read ? "Read" : "Write"),
  510. master,
  511. (((master >= SDRAM_PLB_M0ID_FIRST) &&
  512. (master <= SDRAM_PLB_M0ID_LAST)) ?
  513. ppc4xx_plb_masters[master] : "UNKNOWN"));
  514. }
  515. /**
  516. * ppc4xx_edac_generate_message - generate interpretted status message
  517. * @mci: A pointer to the EDAC memory controller instance associated
  518. * with the driver-unique message being generated.
  519. * @status: A pointer to the ECC status structure to generate the
  520. * message from.
  521. * @buffer: A pointer to the buffer in which to generate the
  522. * message.
  523. * @size: The size, in bytes, of space available in buffer.
  524. *
  525. * This routine generates to the provided buffer the driver-unique
  526. * EDAC report message from the specified ECC status.
  527. */
  528. static void
  529. ppc4xx_edac_generate_message(const struct mem_ctl_info *mci,
  530. const struct ppc4xx_ecc_status *status,
  531. char *buffer,
  532. size_t size)
  533. {
  534. int n;
  535. if (buffer == NULL || size == 0)
  536. return;
  537. n = ppc4xx_edac_generate_ecc_message(mci, status, buffer, size);
  538. if (n < 0 || n >= size)
  539. return;
  540. buffer += n;
  541. size -= n;
  542. ppc4xx_edac_generate_plb_message(mci, status, buffer, size);
  543. }
  544. #ifdef DEBUG
  545. /**
  546. * ppc4xx_ecc_dump_status - dump controller ECC status registers
  547. * @mci: A pointer to the EDAC memory controller instance
  548. * associated with the status being dumped.
  549. * @status: A pointer to the ECC status structure to generate the
  550. * dump from.
  551. *
  552. * This routine dumps to the kernel log buffer the raw and
  553. * interpretted specified ECC status.
  554. */
  555. static void
  556. ppc4xx_ecc_dump_status(const struct mem_ctl_info *mci,
  557. const struct ppc4xx_ecc_status *status)
  558. {
  559. char message[PPC4XX_EDAC_MESSAGE_SIZE];
  560. ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
  561. ppc4xx_edac_mc_printk(KERN_INFO, mci,
  562. "\n"
  563. "\tECCES: 0x%08x\n"
  564. "\tWMIRQ: 0x%08x\n"
  565. "\tBESR: 0x%08x\n"
  566. "\tBEAR: 0x%08x%08x\n"
  567. "\t%s\n",
  568. status->ecces,
  569. status->wmirq,
  570. status->besr,
  571. status->bearh,
  572. status->bearl,
  573. message);
  574. }
  575. #endif /* DEBUG */
  576. /**
  577. * ppc4xx_ecc_get_status - get controller ECC status
  578. * @mci: A pointer to the EDAC memory controller instance
  579. * associated with the status being retrieved.
  580. * @status: A pointer to the ECC status structure to populate the
  581. * ECC status with.
  582. *
  583. * This routine reads and masks, as appropriate, all the relevant
  584. * status registers that deal with ibm,sdram-4xx-ddr2 ECC errors.
  585. * While we read all of them, for correctable errors, we only expect
  586. * to deal with ECCES. For uncorrectable errors, we expect to deal
  587. * with all of them.
  588. */
  589. static void
  590. ppc4xx_ecc_get_status(const struct mem_ctl_info *mci,
  591. struct ppc4xx_ecc_status *status)
  592. {
  593. const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  594. const dcr_host_t *dcr_host = &pdata->dcr_host;
  595. status->ecces = mfsdram(dcr_host, SDRAM_ECCES) & SDRAM_ECCES_MASK;
  596. status->wmirq = mfsdram(dcr_host, SDRAM_WMIRQ) & SDRAM_WMIRQ_MASK;
  597. status->besr = mfsdram(dcr_host, SDRAM_BESR) & SDRAM_BESR_MASK;
  598. status->bearl = mfsdram(dcr_host, SDRAM_BEARL);
  599. status->bearh = mfsdram(dcr_host, SDRAM_BEARH);
  600. }
  601. /**
  602. * ppc4xx_ecc_clear_status - clear controller ECC status
  603. * @mci: A pointer to the EDAC memory controller instance
  604. * associated with the status being cleared.
  605. * @status: A pointer to the ECC status structure containing the
  606. * values to write to clear the ECC status.
  607. *
  608. * This routine clears--by writing the masked (as appropriate) status
  609. * values back to--the status registers that deal with
  610. * ibm,sdram-4xx-ddr2 ECC errors.
  611. */
  612. static void
  613. ppc4xx_ecc_clear_status(const struct mem_ctl_info *mci,
  614. const struct ppc4xx_ecc_status *status)
  615. {
  616. const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  617. const dcr_host_t *dcr_host = &pdata->dcr_host;
  618. mtsdram(dcr_host, SDRAM_ECCES, status->ecces & SDRAM_ECCES_MASK);
  619. mtsdram(dcr_host, SDRAM_WMIRQ, status->wmirq & SDRAM_WMIRQ_MASK);
  620. mtsdram(dcr_host, SDRAM_BESR, status->besr & SDRAM_BESR_MASK);
  621. mtsdram(dcr_host, SDRAM_BEARL, 0);
  622. mtsdram(dcr_host, SDRAM_BEARH, 0);
  623. }
  624. /**
  625. * ppc4xx_edac_handle_ce - handle controller correctable ECC error (CE)
  626. * @mci: A pointer to the EDAC memory controller instance
  627. * associated with the correctable error being handled and reported.
  628. * @status: A pointer to the ECC status structure associated with
  629. * the correctable error being handled and reported.
  630. *
  631. * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
  632. * correctable error. Per the aforementioned discussion, there's not
  633. * enough status available to use the full EDAC correctable error
  634. * interface, so we just pass driver-unique message to the "no info"
  635. * interface.
  636. */
  637. static void
  638. ppc4xx_edac_handle_ce(struct mem_ctl_info *mci,
  639. const struct ppc4xx_ecc_status *status)
  640. {
  641. int row;
  642. char message[PPC4XX_EDAC_MESSAGE_SIZE];
  643. ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
  644. for (row = 0; row < mci->nr_csrows; row++)
  645. if (ppc4xx_edac_check_bank_error(status, row))
  646. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
  647. 0, 0, 0,
  648. row, 0, -1,
  649. message, "");
  650. }
  651. /**
  652. * ppc4xx_edac_handle_ue - handle controller uncorrectable ECC error (UE)
  653. * @mci: A pointer to the EDAC memory controller instance
  654. * associated with the uncorrectable error being handled and
  655. * reported.
  656. * @status: A pointer to the ECC status structure associated with
  657. * the uncorrectable error being handled and reported.
  658. *
  659. * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
  660. * uncorrectable error.
  661. */
  662. static void
  663. ppc4xx_edac_handle_ue(struct mem_ctl_info *mci,
  664. const struct ppc4xx_ecc_status *status)
  665. {
  666. const u64 bear = ((u64)status->bearh << 32 | status->bearl);
  667. const unsigned long page = bear >> PAGE_SHIFT;
  668. const unsigned long offset = bear & ~PAGE_MASK;
  669. int row;
  670. char message[PPC4XX_EDAC_MESSAGE_SIZE];
  671. ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
  672. for (row = 0; row < mci->nr_csrows; row++)
  673. if (ppc4xx_edac_check_bank_error(status, row))
  674. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
  675. page, offset, 0,
  676. row, 0, -1,
  677. message, "");
  678. }
  679. /**
  680. * ppc4xx_edac_check - check controller for ECC errors
  681. * @mci: A pointer to the EDAC memory controller instance
  682. * associated with the ibm,sdram-4xx-ddr2 controller being
  683. * checked.
  684. *
  685. * This routine is used to check and post ECC errors and is called by
  686. * both the EDAC polling thread and this driver's CE and UE interrupt
  687. * handler.
  688. */
  689. static void
  690. ppc4xx_edac_check(struct mem_ctl_info *mci)
  691. {
  692. #ifdef DEBUG
  693. static unsigned int count;
  694. #endif
  695. struct ppc4xx_ecc_status status;
  696. ppc4xx_ecc_get_status(mci, &status);
  697. #ifdef DEBUG
  698. if (count++ % 30 == 0)
  699. ppc4xx_ecc_dump_status(mci, &status);
  700. #endif
  701. if (status.ecces & SDRAM_ECCES_UE)
  702. ppc4xx_edac_handle_ue(mci, &status);
  703. if (status.ecces & SDRAM_ECCES_CE)
  704. ppc4xx_edac_handle_ce(mci, &status);
  705. ppc4xx_ecc_clear_status(mci, &status);
  706. }
  707. /**
  708. * ppc4xx_edac_isr - SEC (CE) and DED (UE) interrupt service routine
  709. * @irq: The virtual interrupt number being serviced.
  710. * @dev_id: A pointer to the EDAC memory controller instance
  711. * associated with the interrupt being handled.
  712. *
  713. * This routine implements the interrupt handler for both correctable
  714. * (CE) and uncorrectable (UE) ECC errors for the ibm,sdram-4xx-ddr2
  715. * controller. It simply calls through to the same routine used during
  716. * polling to check, report and clear the ECC status.
  717. *
  718. * Unconditionally returns IRQ_HANDLED.
  719. */
  720. static irqreturn_t
  721. ppc4xx_edac_isr(int irq, void *dev_id)
  722. {
  723. struct mem_ctl_info *mci = dev_id;
  724. ppc4xx_edac_check(mci);
  725. return IRQ_HANDLED;
  726. }
  727. /**
  728. * ppc4xx_edac_get_dtype - return the controller memory width
  729. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  730. * currently set for the controller, from which the width
  731. * is derived.
  732. *
  733. * This routine returns the EDAC device type width appropriate for the
  734. * current controller configuration.
  735. *
  736. * TODO: This needs to be conditioned dynamically through feature
  737. * flags or some such when other controller variants are supported as
  738. * the 405EX[r] is 16-/32-bit and the others are 32-/64-bit with the
  739. * 16- and 64-bit field definition/value/enumeration (b1) overloaded
  740. * among them.
  741. *
  742. * Returns a device type width enumeration.
  743. */
  744. static enum dev_type ppc4xx_edac_get_dtype(u32 mcopt1)
  745. {
  746. switch (mcopt1 & SDRAM_MCOPT1_WDTH_MASK) {
  747. case SDRAM_MCOPT1_WDTH_16:
  748. return DEV_X2;
  749. case SDRAM_MCOPT1_WDTH_32:
  750. return DEV_X4;
  751. default:
  752. return DEV_UNKNOWN;
  753. }
  754. }
  755. /**
  756. * ppc4xx_edac_get_mtype - return controller memory type
  757. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  758. * currently set for the controller, from which the memory type
  759. * is derived.
  760. *
  761. * This routine returns the EDAC memory type appropriate for the
  762. * current controller configuration.
  763. *
  764. * Returns a memory type enumeration.
  765. */
  766. static enum mem_type ppc4xx_edac_get_mtype(u32 mcopt1)
  767. {
  768. bool rden = ((mcopt1 & SDRAM_MCOPT1_RDEN_MASK) == SDRAM_MCOPT1_RDEN);
  769. switch (mcopt1 & SDRAM_MCOPT1_DDR_TYPE_MASK) {
  770. case SDRAM_MCOPT1_DDR2_TYPE:
  771. return rden ? MEM_RDDR2 : MEM_DDR2;
  772. case SDRAM_MCOPT1_DDR1_TYPE:
  773. return rden ? MEM_RDDR : MEM_DDR;
  774. default:
  775. return MEM_UNKNOWN;
  776. }
  777. }
  778. /**
  779. * ppc4xx_edac_init_csrows - initialize driver instance rows
  780. * @mci: A pointer to the EDAC memory controller instance
  781. * associated with the ibm,sdram-4xx-ddr2 controller for which
  782. * the csrows (i.e. banks/ranks) are being initialized.
  783. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  784. * currently set for the controller, from which bank width
  785. * and memory typ information is derived.
  786. *
  787. * This routine initializes the virtual "chip select rows" associated
  788. * with the EDAC memory controller instance. An ibm,sdram-4xx-ddr2
  789. * controller bank/rank is mapped to a row.
  790. *
  791. * Returns 0 if OK; otherwise, -EINVAL if the memory bank size
  792. * configuration cannot be determined.
  793. */
  794. static int ppc4xx_edac_init_csrows(struct mem_ctl_info *mci, u32 mcopt1)
  795. {
  796. const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  797. int status = 0;
  798. enum mem_type mtype;
  799. enum dev_type dtype;
  800. enum edac_type edac_mode;
  801. int row, j;
  802. u32 mbxcf, size, nr_pages;
  803. /* Establish the memory type and width */
  804. mtype = ppc4xx_edac_get_mtype(mcopt1);
  805. dtype = ppc4xx_edac_get_dtype(mcopt1);
  806. /* Establish EDAC mode */
  807. if (mci->edac_cap & EDAC_FLAG_SECDED)
  808. edac_mode = EDAC_SECDED;
  809. else if (mci->edac_cap & EDAC_FLAG_EC)
  810. edac_mode = EDAC_EC;
  811. else
  812. edac_mode = EDAC_NONE;
  813. /*
  814. * Initialize each chip select row structure which correspond
  815. * 1:1 with a controller bank/rank.
  816. */
  817. for (row = 0; row < mci->nr_csrows; row++) {
  818. struct csrow_info *csi = mci->csrows[row];
  819. /*
  820. * Get the configuration settings for this
  821. * row/bank/rank and skip disabled banks.
  822. */
  823. mbxcf = mfsdram(&pdata->dcr_host, SDRAM_MBXCF(row));
  824. if ((mbxcf & SDRAM_MBCF_BE_MASK) != SDRAM_MBCF_BE_ENABLE)
  825. continue;
  826. /* Map the bank configuration size setting to pages. */
  827. size = mbxcf & SDRAM_MBCF_SZ_MASK;
  828. switch (size) {
  829. case SDRAM_MBCF_SZ_4MB:
  830. case SDRAM_MBCF_SZ_8MB:
  831. case SDRAM_MBCF_SZ_16MB:
  832. case SDRAM_MBCF_SZ_32MB:
  833. case SDRAM_MBCF_SZ_64MB:
  834. case SDRAM_MBCF_SZ_128MB:
  835. case SDRAM_MBCF_SZ_256MB:
  836. case SDRAM_MBCF_SZ_512MB:
  837. case SDRAM_MBCF_SZ_1GB:
  838. case SDRAM_MBCF_SZ_2GB:
  839. case SDRAM_MBCF_SZ_4GB:
  840. case SDRAM_MBCF_SZ_8GB:
  841. nr_pages = SDRAM_MBCF_SZ_TO_PAGES(size);
  842. break;
  843. default:
  844. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  845. "Unrecognized memory bank %d "
  846. "size 0x%08x\n",
  847. row, SDRAM_MBCF_SZ_DECODE(size));
  848. status = -EINVAL;
  849. goto done;
  850. }
  851. /*
  852. * It's unclear exactly what grain should be set to
  853. * here. The SDRAM_ECCES register allows resolution of
  854. * an error down to a nibble which would potentially
  855. * argue for a grain of '1' byte, even though we only
  856. * know the associated address for uncorrectable
  857. * errors. This value is not used at present for
  858. * anything other than error reporting so getting it
  859. * wrong should be of little consequence. Other
  860. * possible values would be the PLB width (16), the
  861. * page size (PAGE_SIZE) or the memory width (2 or 4).
  862. */
  863. for (j = 0; j < csi->nr_channels; j++) {
  864. struct dimm_info *dimm = csi->channels[j]->dimm;
  865. dimm->nr_pages = nr_pages / csi->nr_channels;
  866. dimm->grain = 1;
  867. dimm->mtype = mtype;
  868. dimm->dtype = dtype;
  869. dimm->edac_mode = edac_mode;
  870. }
  871. }
  872. done:
  873. return status;
  874. }
  875. /**
  876. * ppc4xx_edac_mc_init - initialize driver instance
  877. * @mci: A pointer to the EDAC memory controller instance being
  878. * initialized.
  879. * @op: A pointer to the OpenFirmware device tree node associated
  880. * with the controller this EDAC instance is bound to.
  881. * @dcr_host: A pointer to the DCR data containing the DCR mapping
  882. * for this controller instance.
  883. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  884. * currently set for the controller, from which ECC capabilities
  885. * and scrub mode are derived.
  886. *
  887. * This routine performs initialization of the EDAC memory controller
  888. * instance and related driver-private data associated with the
  889. * ibm,sdram-4xx-ddr2 memory controller the instance is bound to.
  890. *
  891. * Returns 0 if OK; otherwise, < 0 on error.
  892. */
  893. static int ppc4xx_edac_mc_init(struct mem_ctl_info *mci,
  894. struct platform_device *op,
  895. const dcr_host_t *dcr_host, u32 mcopt1)
  896. {
  897. int status = 0;
  898. const u32 memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);
  899. struct ppc4xx_edac_pdata *pdata = NULL;
  900. const struct device_node *np = op->dev.of_node;
  901. if (of_match_device(ppc4xx_edac_match, &op->dev) == NULL)
  902. return -EINVAL;
  903. /* Initial driver pointers and private data */
  904. mci->pdev = &op->dev;
  905. dev_set_drvdata(mci->pdev, mci);
  906. pdata = mci->pvt_info;
  907. pdata->dcr_host = *dcr_host;
  908. /* Initialize controller capabilities and configuration */
  909. mci->mtype_cap = (MEM_FLAG_DDR | MEM_FLAG_RDDR |
  910. MEM_FLAG_DDR2 | MEM_FLAG_RDDR2);
  911. mci->edac_ctl_cap = (EDAC_FLAG_NONE |
  912. EDAC_FLAG_EC |
  913. EDAC_FLAG_SECDED);
  914. mci->scrub_cap = SCRUB_NONE;
  915. mci->scrub_mode = SCRUB_NONE;
  916. /*
  917. * Update the actual capabilites based on the MCOPT1[MCHK]
  918. * settings. Scrubbing is only useful if reporting is enabled.
  919. */
  920. switch (memcheck) {
  921. case SDRAM_MCOPT1_MCHK_CHK:
  922. mci->edac_cap = EDAC_FLAG_EC;
  923. break;
  924. case SDRAM_MCOPT1_MCHK_CHK_REP:
  925. mci->edac_cap = (EDAC_FLAG_EC | EDAC_FLAG_SECDED);
  926. mci->scrub_mode = SCRUB_SW_SRC;
  927. break;
  928. default:
  929. mci->edac_cap = EDAC_FLAG_NONE;
  930. break;
  931. }
  932. /* Initialize strings */
  933. mci->mod_name = PPC4XX_EDAC_MODULE_NAME;
  934. mci->ctl_name = ppc4xx_edac_match->compatible,
  935. mci->dev_name = np->full_name;
  936. /* Initialize callbacks */
  937. mci->edac_check = ppc4xx_edac_check;
  938. mci->ctl_page_to_phys = NULL;
  939. /* Initialize chip select rows */
  940. status = ppc4xx_edac_init_csrows(mci, mcopt1);
  941. if (status)
  942. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  943. "Failed to initialize rows!\n");
  944. return status;
  945. }
  946. /**
  947. * ppc4xx_edac_register_irq - setup and register controller interrupts
  948. * @op: A pointer to the OpenFirmware device tree node associated
  949. * with the controller this EDAC instance is bound to.
  950. * @mci: A pointer to the EDAC memory controller instance
  951. * associated with the ibm,sdram-4xx-ddr2 controller for which
  952. * interrupts are being registered.
  953. *
  954. * This routine parses the correctable (CE) and uncorrectable error (UE)
  955. * interrupts from the device tree node and maps and assigns them to
  956. * the associated EDAC memory controller instance.
  957. *
  958. * Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be
  959. * mapped and assigned.
  960. */
  961. static int ppc4xx_edac_register_irq(struct platform_device *op,
  962. struct mem_ctl_info *mci)
  963. {
  964. int status = 0;
  965. int ded_irq, sec_irq;
  966. struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  967. struct device_node *np = op->dev.of_node;
  968. ded_irq = irq_of_parse_and_map(np, INTMAP_ECCDED_INDEX);
  969. sec_irq = irq_of_parse_and_map(np, INTMAP_ECCSEC_INDEX);
  970. if (!ded_irq || !sec_irq) {
  971. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  972. "Unable to map interrupts.\n");
  973. status = -ENODEV;
  974. goto fail;
  975. }
  976. status = request_irq(ded_irq,
  977. ppc4xx_edac_isr,
  978. 0,
  979. "[EDAC] MC ECCDED",
  980. mci);
  981. if (status < 0) {
  982. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  983. "Unable to request irq %d for ECC DED",
  984. ded_irq);
  985. status = -ENODEV;
  986. goto fail1;
  987. }
  988. status = request_irq(sec_irq,
  989. ppc4xx_edac_isr,
  990. 0,
  991. "[EDAC] MC ECCSEC",
  992. mci);
  993. if (status < 0) {
  994. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  995. "Unable to request irq %d for ECC SEC",
  996. sec_irq);
  997. status = -ENODEV;
  998. goto fail2;
  999. }
  1000. ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCDED irq is %d\n", ded_irq);
  1001. ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCSEC irq is %d\n", sec_irq);
  1002. pdata->irqs.ded = ded_irq;
  1003. pdata->irqs.sec = sec_irq;
  1004. return 0;
  1005. fail2:
  1006. free_irq(sec_irq, mci);
  1007. fail1:
  1008. free_irq(ded_irq, mci);
  1009. fail:
  1010. return status;
  1011. }
  1012. /**
  1013. * ppc4xx_edac_map_dcrs - locate and map controller registers
  1014. * @np: A pointer to the device tree node containing the DCR
  1015. * resources to map.
  1016. * @dcr_host: A pointer to the DCR data to populate with the
  1017. * DCR mapping.
  1018. *
  1019. * This routine attempts to locate in the device tree and map the DCR
  1020. * register resources associated with the controller's indirect DCR
  1021. * address and data windows.
  1022. *
  1023. * Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on
  1024. * error.
  1025. */
  1026. static int ppc4xx_edac_map_dcrs(const struct device_node *np,
  1027. dcr_host_t *dcr_host)
  1028. {
  1029. unsigned int dcr_base, dcr_len;
  1030. if (np == NULL || dcr_host == NULL)
  1031. return -EINVAL;
  1032. /* Get the DCR resource extent and sanity check the values. */
  1033. dcr_base = dcr_resource_start(np, 0);
  1034. dcr_len = dcr_resource_len(np, 0);
  1035. if (dcr_base == 0 || dcr_len == 0) {
  1036. ppc4xx_edac_printk(KERN_ERR,
  1037. "Failed to obtain DCR property.\n");
  1038. return -ENODEV;
  1039. }
  1040. if (dcr_len != SDRAM_DCR_RESOURCE_LEN) {
  1041. ppc4xx_edac_printk(KERN_ERR,
  1042. "Unexpected DCR length %d, expected %d.\n",
  1043. dcr_len, SDRAM_DCR_RESOURCE_LEN);
  1044. return -ENODEV;
  1045. }
  1046. /* Attempt to map the DCR extent. */
  1047. *dcr_host = dcr_map(np, dcr_base, dcr_len);
  1048. if (!DCR_MAP_OK(*dcr_host)) {
  1049. ppc4xx_edac_printk(KERN_INFO, "Failed to map DCRs.\n");
  1050. return -ENODEV;
  1051. }
  1052. return 0;
  1053. }
  1054. /**
  1055. * ppc4xx_edac_probe - check controller and bind driver
  1056. * @op: A pointer to the OpenFirmware device tree node associated
  1057. * with the controller being probed for driver binding.
  1058. *
  1059. * This routine probes a specific ibm,sdram-4xx-ddr2 controller
  1060. * instance for binding with the driver.
  1061. *
  1062. * Returns 0 if the controller instance was successfully bound to the
  1063. * driver; otherwise, < 0 on error.
  1064. */
  1065. static int ppc4xx_edac_probe(struct platform_device *op)
  1066. {
  1067. int status = 0;
  1068. u32 mcopt1, memcheck;
  1069. dcr_host_t dcr_host;
  1070. const struct device_node *np = op->dev.of_node;
  1071. struct mem_ctl_info *mci = NULL;
  1072. struct edac_mc_layer layers[2];
  1073. static int ppc4xx_edac_instance;
  1074. /*
  1075. * At this point, we only support the controller realized on
  1076. * the AMCC PPC 405EX[r]. Reject anything else.
  1077. */
  1078. if (!of_device_is_compatible(np, "ibm,sdram-405ex") &&
  1079. !of_device_is_compatible(np, "ibm,sdram-405exr")) {
  1080. ppc4xx_edac_printk(KERN_NOTICE,
  1081. "Only the PPC405EX[r] is supported.\n");
  1082. return -ENODEV;
  1083. }
  1084. /*
  1085. * Next, get the DCR property and attempt to map it so that we
  1086. * can probe the controller.
  1087. */
  1088. status = ppc4xx_edac_map_dcrs(np, &dcr_host);
  1089. if (status)
  1090. return status;
  1091. /*
  1092. * First determine whether ECC is enabled at all. If not,
  1093. * there is no useful checking or monitoring that can be done
  1094. * for this controller.
  1095. */
  1096. mcopt1 = mfsdram(&dcr_host, SDRAM_MCOPT1);
  1097. memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);
  1098. if (memcheck == SDRAM_MCOPT1_MCHK_NON) {
  1099. ppc4xx_edac_printk(KERN_INFO, "%pOF: No ECC memory detected or "
  1100. "ECC is disabled.\n", np);
  1101. status = -ENODEV;
  1102. goto done;
  1103. }
  1104. /*
  1105. * At this point, we know ECC is enabled, allocate an EDAC
  1106. * controller instance and perform the appropriate
  1107. * initialization.
  1108. */
  1109. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  1110. layers[0].size = ppc4xx_edac_nr_csrows;
  1111. layers[0].is_virt_csrow = true;
  1112. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  1113. layers[1].size = ppc4xx_edac_nr_chans;
  1114. layers[1].is_virt_csrow = false;
  1115. mci = edac_mc_alloc(ppc4xx_edac_instance, ARRAY_SIZE(layers), layers,
  1116. sizeof(struct ppc4xx_edac_pdata));
  1117. if (mci == NULL) {
  1118. ppc4xx_edac_printk(KERN_ERR, "%pOF: "
  1119. "Failed to allocate EDAC MC instance!\n",
  1120. np);
  1121. status = -ENOMEM;
  1122. goto done;
  1123. }
  1124. status = ppc4xx_edac_mc_init(mci, op, &dcr_host, mcopt1);
  1125. if (status) {
  1126. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  1127. "Failed to initialize instance!\n");
  1128. goto fail;
  1129. }
  1130. /*
  1131. * We have a valid, initialized EDAC instance bound to the
  1132. * controller. Attempt to register it with the EDAC subsystem
  1133. * and, if necessary, register interrupts.
  1134. */
  1135. if (edac_mc_add_mc(mci)) {
  1136. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  1137. "Failed to add instance!\n");
  1138. status = -ENODEV;
  1139. goto fail;
  1140. }
  1141. if (edac_op_state == EDAC_OPSTATE_INT) {
  1142. status = ppc4xx_edac_register_irq(op, mci);
  1143. if (status)
  1144. goto fail1;
  1145. }
  1146. ppc4xx_edac_instance++;
  1147. return 0;
  1148. fail1:
  1149. edac_mc_del_mc(mci->pdev);
  1150. fail:
  1151. edac_mc_free(mci);
  1152. done:
  1153. return status;
  1154. }
  1155. /**
  1156. * ppc4xx_edac_remove - unbind driver from controller
  1157. * @op: A pointer to the OpenFirmware device tree node associated
  1158. * with the controller this EDAC instance is to be unbound/removed
  1159. * from.
  1160. *
  1161. * This routine unbinds the EDAC memory controller instance associated
  1162. * with the specified ibm,sdram-4xx-ddr2 controller described by the
  1163. * OpenFirmware device tree node passed as a parameter.
  1164. *
  1165. * Unconditionally returns 0.
  1166. */
  1167. static int
  1168. ppc4xx_edac_remove(struct platform_device *op)
  1169. {
  1170. struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);
  1171. struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  1172. if (edac_op_state == EDAC_OPSTATE_INT) {
  1173. free_irq(pdata->irqs.sec, mci);
  1174. free_irq(pdata->irqs.ded, mci);
  1175. }
  1176. dcr_unmap(pdata->dcr_host, SDRAM_DCR_RESOURCE_LEN);
  1177. edac_mc_del_mc(mci->pdev);
  1178. edac_mc_free(mci);
  1179. return 0;
  1180. }
  1181. /**
  1182. * ppc4xx_edac_opstate_init - initialize EDAC reporting method
  1183. *
  1184. * This routine ensures that the EDAC memory controller reporting
  1185. * method is mapped to a sane value as the EDAC core defines the value
  1186. * to EDAC_OPSTATE_INVAL by default. We don't call the global
  1187. * opstate_init as that defaults to polling and we want interrupt as
  1188. * the default.
  1189. */
  1190. static inline void __init
  1191. ppc4xx_edac_opstate_init(void)
  1192. {
  1193. switch (edac_op_state) {
  1194. case EDAC_OPSTATE_POLL:
  1195. case EDAC_OPSTATE_INT:
  1196. break;
  1197. default:
  1198. edac_op_state = EDAC_OPSTATE_INT;
  1199. break;
  1200. }
  1201. ppc4xx_edac_printk(KERN_INFO, "Reporting type: %s\n",
  1202. ((edac_op_state == EDAC_OPSTATE_POLL) ?
  1203. EDAC_OPSTATE_POLL_STR :
  1204. ((edac_op_state == EDAC_OPSTATE_INT) ?
  1205. EDAC_OPSTATE_INT_STR :
  1206. EDAC_OPSTATE_UNKNOWN_STR)));
  1207. }
  1208. /**
  1209. * ppc4xx_edac_init - driver/module insertion entry point
  1210. *
  1211. * This routine is the driver/module insertion entry point. It
  1212. * initializes the EDAC memory controller reporting state and
  1213. * registers the driver as an OpenFirmware device tree platform
  1214. * driver.
  1215. */
  1216. static int __init
  1217. ppc4xx_edac_init(void)
  1218. {
  1219. ppc4xx_edac_printk(KERN_INFO, PPC4XX_EDAC_MODULE_REVISION "\n");
  1220. ppc4xx_edac_opstate_init();
  1221. return platform_driver_register(&ppc4xx_edac_driver);
  1222. }
  1223. /**
  1224. * ppc4xx_edac_exit - driver/module removal entry point
  1225. *
  1226. * This routine is the driver/module removal entry point. It
  1227. * unregisters the driver as an OpenFirmware device tree platform
  1228. * driver.
  1229. */
  1230. static void __exit
  1231. ppc4xx_edac_exit(void)
  1232. {
  1233. platform_driver_unregister(&ppc4xx_edac_driver);
  1234. }
  1235. module_init(ppc4xx_edac_init);
  1236. module_exit(ppc4xx_edac_exit);
  1237. MODULE_LICENSE("GPL v2");
  1238. MODULE_AUTHOR("Grant Erickson <gerickson@nuovations.com>");
  1239. MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");
  1240. module_param(edac_op_state, int, 0444);
  1241. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting State: "
  1242. "0=" EDAC_OPSTATE_POLL_STR ", 2=" EDAC_OPSTATE_INT_STR);