ppc4xx_edac.c 39 KB

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