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