ppc4xx_edac.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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_core.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 acccessed 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 struct of_device_id ppc4xx_edac_match[] = {
  177. {
  178. .compatible = "ibm,sdram-4xx-ddr2"
  179. },
  180. { }
  181. };
  182. static struct platform_driver ppc4xx_edac_driver = {
  183. .probe = ppc4xx_edac_probe,
  184. .remove = ppc4xx_edac_remove,
  185. .driver = {
  186. .owner = THIS_MODULE,
  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_ce_no_info(mci, message);
  647. }
  648. /**
  649. * ppc4xx_edac_handle_ue - handle controller uncorrectable ECC error (UE)
  650. * @mci: A pointer to the EDAC memory controller instance
  651. * associated with the uncorrectable error being handled and
  652. * reported.
  653. * @status: A pointer to the ECC status structure associated with
  654. * the uncorrectable error being handled and reported.
  655. *
  656. * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
  657. * uncorrectable error.
  658. */
  659. static void
  660. ppc4xx_edac_handle_ue(struct mem_ctl_info *mci,
  661. const struct ppc4xx_ecc_status *status)
  662. {
  663. const u64 bear = ((u64)status->bearh << 32 | status->bearl);
  664. const unsigned long page = bear >> PAGE_SHIFT;
  665. const unsigned long offset = bear & ~PAGE_MASK;
  666. int row;
  667. char message[PPC4XX_EDAC_MESSAGE_SIZE];
  668. ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
  669. for (row = 0; row < mci->nr_csrows; row++)
  670. if (ppc4xx_edac_check_bank_error(status, row))
  671. edac_mc_handle_ue(mci, page, offset, row, message);
  672. }
  673. /**
  674. * ppc4xx_edac_check - check controller for ECC errors
  675. * @mci: A pointer to the EDAC memory controller instance
  676. * associated with the ibm,sdram-4xx-ddr2 controller being
  677. * checked.
  678. *
  679. * This routine is used to check and post ECC errors and is called by
  680. * both the EDAC polling thread and this driver's CE and UE interrupt
  681. * handler.
  682. */
  683. static void
  684. ppc4xx_edac_check(struct mem_ctl_info *mci)
  685. {
  686. #ifdef DEBUG
  687. static unsigned int count;
  688. #endif
  689. struct ppc4xx_ecc_status status;
  690. ppc4xx_ecc_get_status(mci, &status);
  691. #ifdef DEBUG
  692. if (count++ % 30 == 0)
  693. ppc4xx_ecc_dump_status(mci, &status);
  694. #endif
  695. if (status.ecces & SDRAM_ECCES_UE)
  696. ppc4xx_edac_handle_ue(mci, &status);
  697. if (status.ecces & SDRAM_ECCES_CE)
  698. ppc4xx_edac_handle_ce(mci, &status);
  699. ppc4xx_ecc_clear_status(mci, &status);
  700. }
  701. /**
  702. * ppc4xx_edac_isr - SEC (CE) and DED (UE) interrupt service routine
  703. * @irq: The virtual interrupt number being serviced.
  704. * @dev_id: A pointer to the EDAC memory controller instance
  705. * associated with the interrupt being handled.
  706. *
  707. * This routine implements the interrupt handler for both correctable
  708. * (CE) and uncorrectable (UE) ECC errors for the ibm,sdram-4xx-ddr2
  709. * controller. It simply calls through to the same routine used during
  710. * polling to check, report and clear the ECC status.
  711. *
  712. * Unconditionally returns IRQ_HANDLED.
  713. */
  714. static irqreturn_t
  715. ppc4xx_edac_isr(int irq, void *dev_id)
  716. {
  717. struct mem_ctl_info *mci = dev_id;
  718. ppc4xx_edac_check(mci);
  719. return IRQ_HANDLED;
  720. }
  721. /**
  722. * ppc4xx_edac_get_dtype - return the controller memory width
  723. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  724. * currently set for the controller, from which the width
  725. * is derived.
  726. *
  727. * This routine returns the EDAC device type width appropriate for the
  728. * current controller configuration.
  729. *
  730. * TODO: This needs to be conditioned dynamically through feature
  731. * flags or some such when other controller variants are supported as
  732. * the 405EX[r] is 16-/32-bit and the others are 32-/64-bit with the
  733. * 16- and 64-bit field definition/value/enumeration (b1) overloaded
  734. * among them.
  735. *
  736. * Returns a device type width enumeration.
  737. */
  738. static enum dev_type __devinit
  739. ppc4xx_edac_get_dtype(u32 mcopt1)
  740. {
  741. switch (mcopt1 & SDRAM_MCOPT1_WDTH_MASK) {
  742. case SDRAM_MCOPT1_WDTH_16:
  743. return DEV_X2;
  744. case SDRAM_MCOPT1_WDTH_32:
  745. return DEV_X4;
  746. default:
  747. return DEV_UNKNOWN;
  748. }
  749. }
  750. /**
  751. * ppc4xx_edac_get_mtype - return controller memory type
  752. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  753. * currently set for the controller, from which the memory type
  754. * is derived.
  755. *
  756. * This routine returns the EDAC memory type appropriate for the
  757. * current controller configuration.
  758. *
  759. * Returns a memory type enumeration.
  760. */
  761. static enum mem_type __devinit
  762. ppc4xx_edac_get_mtype(u32 mcopt1)
  763. {
  764. bool rden = ((mcopt1 & SDRAM_MCOPT1_RDEN_MASK) == SDRAM_MCOPT1_RDEN);
  765. switch (mcopt1 & SDRAM_MCOPT1_DDR_TYPE_MASK) {
  766. case SDRAM_MCOPT1_DDR2_TYPE:
  767. return rden ? MEM_RDDR2 : MEM_DDR2;
  768. case SDRAM_MCOPT1_DDR1_TYPE:
  769. return rden ? MEM_RDDR : MEM_DDR;
  770. default:
  771. return MEM_UNKNOWN;
  772. }
  773. }
  774. /**
  775. * ppc4xx_edac_init_csrows - initialize driver instance rows
  776. * @mci: A pointer to the EDAC memory controller instance
  777. * associated with the ibm,sdram-4xx-ddr2 controller for which
  778. * the csrows (i.e. banks/ranks) are being initialized.
  779. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  780. * currently set for the controller, from which bank width
  781. * and memory typ information is derived.
  782. *
  783. * This routine initializes the virtual "chip select rows" associated
  784. * with the EDAC memory controller instance. An ibm,sdram-4xx-ddr2
  785. * controller bank/rank is mapped to a row.
  786. *
  787. * Returns 0 if OK; otherwise, -EINVAL if the memory bank size
  788. * configuration cannot be determined.
  789. */
  790. static int __devinit
  791. ppc4xx_edac_init_csrows(struct mem_ctl_info *mci, u32 mcopt1)
  792. {
  793. const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  794. int status = 0;
  795. enum mem_type mtype;
  796. enum dev_type dtype;
  797. enum edac_type edac_mode;
  798. int row;
  799. u32 mbxcf, size;
  800. static u32 ppc4xx_last_page;
  801. /* Establish the memory type and width */
  802. mtype = ppc4xx_edac_get_mtype(mcopt1);
  803. dtype = ppc4xx_edac_get_dtype(mcopt1);
  804. /* Establish EDAC mode */
  805. if (mci->edac_cap & EDAC_FLAG_SECDED)
  806. edac_mode = EDAC_SECDED;
  807. else if (mci->edac_cap & EDAC_FLAG_EC)
  808. edac_mode = EDAC_EC;
  809. else
  810. edac_mode = EDAC_NONE;
  811. /*
  812. * Initialize each chip select row structure which correspond
  813. * 1:1 with a controller bank/rank.
  814. */
  815. for (row = 0; row < mci->nr_csrows; row++) {
  816. struct csrow_info *csi = &mci->csrows[row];
  817. /*
  818. * Get the configuration settings for this
  819. * row/bank/rank and skip disabled banks.
  820. */
  821. mbxcf = mfsdram(&pdata->dcr_host, SDRAM_MBXCF(row));
  822. if ((mbxcf & SDRAM_MBCF_BE_MASK) != SDRAM_MBCF_BE_ENABLE)
  823. continue;
  824. /* Map the bank configuration size setting to pages. */
  825. size = mbxcf & SDRAM_MBCF_SZ_MASK;
  826. switch (size) {
  827. case SDRAM_MBCF_SZ_4MB:
  828. case SDRAM_MBCF_SZ_8MB:
  829. case SDRAM_MBCF_SZ_16MB:
  830. case SDRAM_MBCF_SZ_32MB:
  831. case SDRAM_MBCF_SZ_64MB:
  832. case SDRAM_MBCF_SZ_128MB:
  833. case SDRAM_MBCF_SZ_256MB:
  834. case SDRAM_MBCF_SZ_512MB:
  835. case SDRAM_MBCF_SZ_1GB:
  836. case SDRAM_MBCF_SZ_2GB:
  837. case SDRAM_MBCF_SZ_4GB:
  838. case SDRAM_MBCF_SZ_8GB:
  839. csi->nr_pages = SDRAM_MBCF_SZ_TO_PAGES(size);
  840. break;
  841. default:
  842. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  843. "Unrecognized memory bank %d "
  844. "size 0x%08x\n",
  845. row, SDRAM_MBCF_SZ_DECODE(size));
  846. status = -EINVAL;
  847. goto done;
  848. }
  849. csi->first_page = ppc4xx_last_page;
  850. csi->last_page = csi->first_page + csi->nr_pages - 1;
  851. csi->page_mask = 0;
  852. /*
  853. * It's unclear exactly what grain should be set to
  854. * here. The SDRAM_ECCES register allows resolution of
  855. * an error down to a nibble which would potentially
  856. * argue for a grain of '1' byte, even though we only
  857. * know the associated address for uncorrectable
  858. * errors. This value is not used at present for
  859. * anything other than error reporting so getting it
  860. * wrong should be of little consequence. Other
  861. * possible values would be the PLB width (16), the
  862. * page size (PAGE_SIZE) or the memory width (2 or 4).
  863. */
  864. csi->grain = 1;
  865. csi->mtype = mtype;
  866. csi->dtype = dtype;
  867. csi->edac_mode = edac_mode;
  868. ppc4xx_last_page += csi->nr_pages;
  869. }
  870. done:
  871. return status;
  872. }
  873. /**
  874. * ppc4xx_edac_mc_init - initialize driver instance
  875. * @mci: A pointer to the EDAC memory controller instance being
  876. * initialized.
  877. * @op: A pointer to the OpenFirmware device tree node associated
  878. * with the controller this EDAC instance is bound to.
  879. * @dcr_host: A pointer to the DCR data containing the DCR mapping
  880. * for this controller instance.
  881. * @mcopt1: The 32-bit Memory Controller Option 1 register value
  882. * currently set for the controller, from which ECC capabilities
  883. * and scrub mode are derived.
  884. *
  885. * This routine performs initialization of the EDAC memory controller
  886. * instance and related driver-private data associated with the
  887. * ibm,sdram-4xx-ddr2 memory controller the instance is bound to.
  888. *
  889. * Returns 0 if OK; otherwise, < 0 on error.
  890. */
  891. static int __devinit
  892. ppc4xx_edac_mc_init(struct mem_ctl_info *mci,
  893. struct platform_device *op,
  894. const dcr_host_t *dcr_host,
  895. 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->dev = &op->dev;
  905. dev_set_drvdata(mci->dev, mci);
  906. pdata = mci->pvt_info;
  907. pdata->dcr_host = *dcr_host;
  908. pdata->irqs.sec = NO_IRQ;
  909. pdata->irqs.ded = NO_IRQ;
  910. /* Initialize controller capabilities and configuration */
  911. mci->mtype_cap = (MEM_FLAG_DDR | MEM_FLAG_RDDR |
  912. MEM_FLAG_DDR2 | MEM_FLAG_RDDR2);
  913. mci->edac_ctl_cap = (EDAC_FLAG_NONE |
  914. EDAC_FLAG_EC |
  915. EDAC_FLAG_SECDED);
  916. mci->scrub_cap = SCRUB_NONE;
  917. mci->scrub_mode = SCRUB_NONE;
  918. /*
  919. * Update the actual capabilites based on the MCOPT1[MCHK]
  920. * settings. Scrubbing is only useful if reporting is enabled.
  921. */
  922. switch (memcheck) {
  923. case SDRAM_MCOPT1_MCHK_CHK:
  924. mci->edac_cap = EDAC_FLAG_EC;
  925. break;
  926. case SDRAM_MCOPT1_MCHK_CHK_REP:
  927. mci->edac_cap = (EDAC_FLAG_EC | EDAC_FLAG_SECDED);
  928. mci->scrub_mode = SCRUB_SW_SRC;
  929. break;
  930. default:
  931. mci->edac_cap = EDAC_FLAG_NONE;
  932. break;
  933. }
  934. /* Initialize strings */
  935. mci->mod_name = PPC4XX_EDAC_MODULE_NAME;
  936. mci->mod_ver = PPC4XX_EDAC_MODULE_REVISION;
  937. mci->ctl_name = match->compatible,
  938. mci->dev_name = np->full_name;
  939. /* Initialize callbacks */
  940. mci->edac_check = ppc4xx_edac_check;
  941. mci->ctl_page_to_phys = NULL;
  942. /* Initialize chip select rows */
  943. status = ppc4xx_edac_init_csrows(mci, mcopt1);
  944. if (status)
  945. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  946. "Failed to initialize rows!\n");
  947. return status;
  948. }
  949. /**
  950. * ppc4xx_edac_register_irq - setup and register controller interrupts
  951. * @op: A pointer to the OpenFirmware device tree node associated
  952. * with the controller this EDAC instance is bound to.
  953. * @mci: A pointer to the EDAC memory controller instance
  954. * associated with the ibm,sdram-4xx-ddr2 controller for which
  955. * interrupts are being registered.
  956. *
  957. * This routine parses the correctable (CE) and uncorrectable error (UE)
  958. * interrupts from the device tree node and maps and assigns them to
  959. * the associated EDAC memory controller instance.
  960. *
  961. * Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be
  962. * mapped and assigned.
  963. */
  964. static int __devinit
  965. ppc4xx_edac_register_irq(struct platform_device *op, struct mem_ctl_info *mci)
  966. {
  967. int status = 0;
  968. int ded_irq, sec_irq;
  969. struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  970. struct device_node *np = op->dev.of_node;
  971. ded_irq = irq_of_parse_and_map(np, INTMAP_ECCDED_INDEX);
  972. sec_irq = irq_of_parse_and_map(np, INTMAP_ECCSEC_INDEX);
  973. if (ded_irq == NO_IRQ || sec_irq == NO_IRQ) {
  974. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  975. "Unable to map interrupts.\n");
  976. status = -ENODEV;
  977. goto fail;
  978. }
  979. status = request_irq(ded_irq,
  980. ppc4xx_edac_isr,
  981. IRQF_DISABLED,
  982. "[EDAC] MC ECCDED",
  983. mci);
  984. if (status < 0) {
  985. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  986. "Unable to request irq %d for ECC DED",
  987. ded_irq);
  988. status = -ENODEV;
  989. goto fail1;
  990. }
  991. status = request_irq(sec_irq,
  992. ppc4xx_edac_isr,
  993. IRQF_DISABLED,
  994. "[EDAC] MC ECCSEC",
  995. mci);
  996. if (status < 0) {
  997. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  998. "Unable to request irq %d for ECC SEC",
  999. sec_irq);
  1000. status = -ENODEV;
  1001. goto fail2;
  1002. }
  1003. ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCDED irq is %d\n", ded_irq);
  1004. ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCSEC irq is %d\n", sec_irq);
  1005. pdata->irqs.ded = ded_irq;
  1006. pdata->irqs.sec = sec_irq;
  1007. return 0;
  1008. fail2:
  1009. free_irq(sec_irq, mci);
  1010. fail1:
  1011. free_irq(ded_irq, mci);
  1012. fail:
  1013. return status;
  1014. }
  1015. /**
  1016. * ppc4xx_edac_map_dcrs - locate and map controller registers
  1017. * @np: A pointer to the device tree node containing the DCR
  1018. * resources to map.
  1019. * @dcr_host: A pointer to the DCR data to populate with the
  1020. * DCR mapping.
  1021. *
  1022. * This routine attempts to locate in the device tree and map the DCR
  1023. * register resources associated with the controller's indirect DCR
  1024. * address and data windows.
  1025. *
  1026. * Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on
  1027. * error.
  1028. */
  1029. static int __devinit
  1030. ppc4xx_edac_map_dcrs(const struct device_node *np, dcr_host_t *dcr_host)
  1031. {
  1032. unsigned int dcr_base, dcr_len;
  1033. if (np == NULL || dcr_host == NULL)
  1034. return -EINVAL;
  1035. /* Get the DCR resource extent and sanity check the values. */
  1036. dcr_base = dcr_resource_start(np, 0);
  1037. dcr_len = dcr_resource_len(np, 0);
  1038. if (dcr_base == 0 || dcr_len == 0) {
  1039. ppc4xx_edac_printk(KERN_ERR,
  1040. "Failed to obtain DCR property.\n");
  1041. return -ENODEV;
  1042. }
  1043. if (dcr_len != SDRAM_DCR_RESOURCE_LEN) {
  1044. ppc4xx_edac_printk(KERN_ERR,
  1045. "Unexpected DCR length %d, expected %d.\n",
  1046. dcr_len, SDRAM_DCR_RESOURCE_LEN);
  1047. return -ENODEV;
  1048. }
  1049. /* Attempt to map the DCR extent. */
  1050. *dcr_host = dcr_map(np, dcr_base, dcr_len);
  1051. if (!DCR_MAP_OK(*dcr_host)) {
  1052. ppc4xx_edac_printk(KERN_INFO, "Failed to map DCRs.\n");
  1053. return -ENODEV;
  1054. }
  1055. return 0;
  1056. }
  1057. /**
  1058. * ppc4xx_edac_probe - check controller and bind driver
  1059. * @op: A pointer to the OpenFirmware device tree node associated
  1060. * with the controller being probed for driver binding.
  1061. *
  1062. * This routine probes a specific ibm,sdram-4xx-ddr2 controller
  1063. * instance for binding with the driver.
  1064. *
  1065. * Returns 0 if the controller instance was successfully bound to the
  1066. * driver; otherwise, < 0 on error.
  1067. */
  1068. static int __devinit ppc4xx_edac_probe(struct platform_device *op)
  1069. {
  1070. int status = 0;
  1071. u32 mcopt1, memcheck;
  1072. dcr_host_t dcr_host;
  1073. const struct device_node *np = op->dev.of_node;
  1074. struct mem_ctl_info *mci = NULL;
  1075. static int ppc4xx_edac_instance;
  1076. /*
  1077. * At this point, we only support the controller realized on
  1078. * the AMCC PPC 405EX[r]. Reject anything else.
  1079. */
  1080. if (!of_device_is_compatible(np, "ibm,sdram-405ex") &&
  1081. !of_device_is_compatible(np, "ibm,sdram-405exr")) {
  1082. ppc4xx_edac_printk(KERN_NOTICE,
  1083. "Only the PPC405EX[r] is supported.\n");
  1084. return -ENODEV;
  1085. }
  1086. /*
  1087. * Next, get the DCR property and attempt to map it so that we
  1088. * can probe the controller.
  1089. */
  1090. status = ppc4xx_edac_map_dcrs(np, &dcr_host);
  1091. if (status)
  1092. return status;
  1093. /*
  1094. * First determine whether ECC is enabled at all. If not,
  1095. * there is no useful checking or monitoring that can be done
  1096. * for this controller.
  1097. */
  1098. mcopt1 = mfsdram(&dcr_host, SDRAM_MCOPT1);
  1099. memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);
  1100. if (memcheck == SDRAM_MCOPT1_MCHK_NON) {
  1101. ppc4xx_edac_printk(KERN_INFO, "%s: No ECC memory detected or "
  1102. "ECC is disabled.\n", np->full_name);
  1103. status = -ENODEV;
  1104. goto done;
  1105. }
  1106. /*
  1107. * At this point, we know ECC is enabled, allocate an EDAC
  1108. * controller instance and perform the appropriate
  1109. * initialization.
  1110. */
  1111. mci = edac_mc_alloc(sizeof(struct ppc4xx_edac_pdata),
  1112. ppc4xx_edac_nr_csrows,
  1113. ppc4xx_edac_nr_chans,
  1114. ppc4xx_edac_instance);
  1115. if (mci == NULL) {
  1116. ppc4xx_edac_printk(KERN_ERR, "%s: "
  1117. "Failed to allocate EDAC MC instance!\n",
  1118. np->full_name);
  1119. status = -ENOMEM;
  1120. goto done;
  1121. }
  1122. status = ppc4xx_edac_mc_init(mci, op, &dcr_host, mcopt1);
  1123. if (status) {
  1124. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  1125. "Failed to initialize instance!\n");
  1126. goto fail;
  1127. }
  1128. /*
  1129. * We have a valid, initialized EDAC instance bound to the
  1130. * controller. Attempt to register it with the EDAC subsystem
  1131. * and, if necessary, register interrupts.
  1132. */
  1133. if (edac_mc_add_mc(mci)) {
  1134. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  1135. "Failed to add instance!\n");
  1136. status = -ENODEV;
  1137. goto fail;
  1138. }
  1139. if (edac_op_state == EDAC_OPSTATE_INT) {
  1140. status = ppc4xx_edac_register_irq(op, mci);
  1141. if (status)
  1142. goto fail1;
  1143. }
  1144. ppc4xx_edac_instance++;
  1145. return 0;
  1146. fail1:
  1147. edac_mc_del_mc(mci->dev);
  1148. fail:
  1149. edac_mc_free(mci);
  1150. done:
  1151. return status;
  1152. }
  1153. /**
  1154. * ppc4xx_edac_remove - unbind driver from controller
  1155. * @op: A pointer to the OpenFirmware device tree node associated
  1156. * with the controller this EDAC instance is to be unbound/removed
  1157. * from.
  1158. *
  1159. * This routine unbinds the EDAC memory controller instance associated
  1160. * with the specified ibm,sdram-4xx-ddr2 controller described by the
  1161. * OpenFirmware device tree node passed as a parameter.
  1162. *
  1163. * Unconditionally returns 0.
  1164. */
  1165. static int
  1166. ppc4xx_edac_remove(struct platform_device *op)
  1167. {
  1168. struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);
  1169. struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  1170. if (edac_op_state == EDAC_OPSTATE_INT) {
  1171. free_irq(pdata->irqs.sec, mci);
  1172. free_irq(pdata->irqs.ded, mci);
  1173. }
  1174. dcr_unmap(pdata->dcr_host, SDRAM_DCR_RESOURCE_LEN);
  1175. edac_mc_del_mc(mci->dev);
  1176. edac_mc_free(mci);
  1177. return 0;
  1178. }
  1179. /**
  1180. * ppc4xx_edac_opstate_init - initialize EDAC reporting method
  1181. *
  1182. * This routine ensures that the EDAC memory controller reporting
  1183. * method is mapped to a sane value as the EDAC core defines the value
  1184. * to EDAC_OPSTATE_INVAL by default. We don't call the global
  1185. * opstate_init as that defaults to polling and we want interrupt as
  1186. * the default.
  1187. */
  1188. static inline void __init
  1189. ppc4xx_edac_opstate_init(void)
  1190. {
  1191. switch (edac_op_state) {
  1192. case EDAC_OPSTATE_POLL:
  1193. case EDAC_OPSTATE_INT:
  1194. break;
  1195. default:
  1196. edac_op_state = EDAC_OPSTATE_INT;
  1197. break;
  1198. }
  1199. ppc4xx_edac_printk(KERN_INFO, "Reporting type: %s\n",
  1200. ((edac_op_state == EDAC_OPSTATE_POLL) ?
  1201. EDAC_OPSTATE_POLL_STR :
  1202. ((edac_op_state == EDAC_OPSTATE_INT) ?
  1203. EDAC_OPSTATE_INT_STR :
  1204. EDAC_OPSTATE_UNKNOWN_STR)));
  1205. }
  1206. /**
  1207. * ppc4xx_edac_init - driver/module insertion entry point
  1208. *
  1209. * This routine is the driver/module insertion entry point. It
  1210. * initializes the EDAC memory controller reporting state and
  1211. * registers the driver as an OpenFirmware device tree platform
  1212. * driver.
  1213. */
  1214. static int __init
  1215. ppc4xx_edac_init(void)
  1216. {
  1217. ppc4xx_edac_printk(KERN_INFO, PPC4XX_EDAC_MODULE_REVISION "\n");
  1218. ppc4xx_edac_opstate_init();
  1219. return platform_driver_register(&ppc4xx_edac_driver);
  1220. }
  1221. /**
  1222. * ppc4xx_edac_exit - driver/module removal entry point
  1223. *
  1224. * This routine is the driver/module removal entry point. It
  1225. * unregisters the driver as an OpenFirmware device tree platform
  1226. * driver.
  1227. */
  1228. static void __exit
  1229. ppc4xx_edac_exit(void)
  1230. {
  1231. platform_driver_unregister(&ppc4xx_edac_driver);
  1232. }
  1233. module_init(ppc4xx_edac_init);
  1234. module_exit(ppc4xx_edac_exit);
  1235. MODULE_LICENSE("GPL v2");
  1236. MODULE_AUTHOR("Grant Erickson <gerickson@nuovations.com>");
  1237. MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");
  1238. module_param(edac_op_state, int, 0444);
  1239. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting State: "
  1240. "0=" EDAC_OPSTATE_POLL_STR ", 2=" EDAC_OPSTATE_INT_STR);