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