ppc4xx_edac.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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. /* Initialize controller capabilities and configuration */
  909. mci->mtype_cap = (MEM_FLAG_DDR | MEM_FLAG_RDDR |
  910. MEM_FLAG_DDR2 | MEM_FLAG_RDDR2);
  911. mci->edac_ctl_cap = (EDAC_FLAG_NONE |
  912. EDAC_FLAG_EC |
  913. EDAC_FLAG_SECDED);
  914. mci->scrub_cap = SCRUB_NONE;
  915. mci->scrub_mode = SCRUB_NONE;
  916. /*
  917. * Update the actual capabilites based on the MCOPT1[MCHK]
  918. * settings. Scrubbing is only useful if reporting is enabled.
  919. */
  920. switch (memcheck) {
  921. case SDRAM_MCOPT1_MCHK_CHK:
  922. mci->edac_cap = EDAC_FLAG_EC;
  923. break;
  924. case SDRAM_MCOPT1_MCHK_CHK_REP:
  925. mci->edac_cap = (EDAC_FLAG_EC | EDAC_FLAG_SECDED);
  926. mci->scrub_mode = SCRUB_SW_SRC;
  927. break;
  928. default:
  929. mci->edac_cap = EDAC_FLAG_NONE;
  930. break;
  931. }
  932. /* Initialize strings */
  933. mci->mod_name = PPC4XX_EDAC_MODULE_NAME;
  934. mci->mod_ver = PPC4XX_EDAC_MODULE_REVISION;
  935. mci->ctl_name = ppc4xx_edac_match->compatible,
  936. mci->dev_name = np->full_name;
  937. /* Initialize callbacks */
  938. mci->edac_check = ppc4xx_edac_check;
  939. mci->ctl_page_to_phys = NULL;
  940. /* Initialize chip select rows */
  941. status = ppc4xx_edac_init_csrows(mci, mcopt1);
  942. if (status)
  943. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  944. "Failed to initialize rows!\n");
  945. return status;
  946. }
  947. /**
  948. * ppc4xx_edac_register_irq - setup and register controller interrupts
  949. * @op: A pointer to the OpenFirmware device tree node associated
  950. * with the controller this EDAC instance is bound to.
  951. * @mci: A pointer to the EDAC memory controller instance
  952. * associated with the ibm,sdram-4xx-ddr2 controller for which
  953. * interrupts are being registered.
  954. *
  955. * This routine parses the correctable (CE) and uncorrectable error (UE)
  956. * interrupts from the device tree node and maps and assigns them to
  957. * the associated EDAC memory controller instance.
  958. *
  959. * Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be
  960. * mapped and assigned.
  961. */
  962. static int ppc4xx_edac_register_irq(struct platform_device *op,
  963. struct mem_ctl_info *mci)
  964. {
  965. int status = 0;
  966. int ded_irq, sec_irq;
  967. struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  968. struct device_node *np = op->dev.of_node;
  969. ded_irq = irq_of_parse_and_map(np, INTMAP_ECCDED_INDEX);
  970. sec_irq = irq_of_parse_and_map(np, INTMAP_ECCSEC_INDEX);
  971. if (!ded_irq || !sec_irq) {
  972. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  973. "Unable to map interrupts.\n");
  974. status = -ENODEV;
  975. goto fail;
  976. }
  977. status = request_irq(ded_irq,
  978. ppc4xx_edac_isr,
  979. 0,
  980. "[EDAC] MC ECCDED",
  981. mci);
  982. if (status < 0) {
  983. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  984. "Unable to request irq %d for ECC DED",
  985. ded_irq);
  986. status = -ENODEV;
  987. goto fail1;
  988. }
  989. status = request_irq(sec_irq,
  990. ppc4xx_edac_isr,
  991. 0,
  992. "[EDAC] MC ECCSEC",
  993. mci);
  994. if (status < 0) {
  995. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  996. "Unable to request irq %d for ECC SEC",
  997. sec_irq);
  998. status = -ENODEV;
  999. goto fail2;
  1000. }
  1001. ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCDED irq is %d\n", ded_irq);
  1002. ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCSEC irq is %d\n", sec_irq);
  1003. pdata->irqs.ded = ded_irq;
  1004. pdata->irqs.sec = sec_irq;
  1005. return 0;
  1006. fail2:
  1007. free_irq(sec_irq, mci);
  1008. fail1:
  1009. free_irq(ded_irq, mci);
  1010. fail:
  1011. return status;
  1012. }
  1013. /**
  1014. * ppc4xx_edac_map_dcrs - locate and map controller registers
  1015. * @np: A pointer to the device tree node containing the DCR
  1016. * resources to map.
  1017. * @dcr_host: A pointer to the DCR data to populate with the
  1018. * DCR mapping.
  1019. *
  1020. * This routine attempts to locate in the device tree and map the DCR
  1021. * register resources associated with the controller's indirect DCR
  1022. * address and data windows.
  1023. *
  1024. * Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on
  1025. * error.
  1026. */
  1027. static int ppc4xx_edac_map_dcrs(const struct device_node *np,
  1028. dcr_host_t *dcr_host)
  1029. {
  1030. unsigned int dcr_base, dcr_len;
  1031. if (np == NULL || dcr_host == NULL)
  1032. return -EINVAL;
  1033. /* Get the DCR resource extent and sanity check the values. */
  1034. dcr_base = dcr_resource_start(np, 0);
  1035. dcr_len = dcr_resource_len(np, 0);
  1036. if (dcr_base == 0 || dcr_len == 0) {
  1037. ppc4xx_edac_printk(KERN_ERR,
  1038. "Failed to obtain DCR property.\n");
  1039. return -ENODEV;
  1040. }
  1041. if (dcr_len != SDRAM_DCR_RESOURCE_LEN) {
  1042. ppc4xx_edac_printk(KERN_ERR,
  1043. "Unexpected DCR length %d, expected %d.\n",
  1044. dcr_len, SDRAM_DCR_RESOURCE_LEN);
  1045. return -ENODEV;
  1046. }
  1047. /* Attempt to map the DCR extent. */
  1048. *dcr_host = dcr_map(np, dcr_base, dcr_len);
  1049. if (!DCR_MAP_OK(*dcr_host)) {
  1050. ppc4xx_edac_printk(KERN_INFO, "Failed to map DCRs.\n");
  1051. return -ENODEV;
  1052. }
  1053. return 0;
  1054. }
  1055. /**
  1056. * ppc4xx_edac_probe - check controller and bind driver
  1057. * @op: A pointer to the OpenFirmware device tree node associated
  1058. * with the controller being probed for driver binding.
  1059. *
  1060. * This routine probes a specific ibm,sdram-4xx-ddr2 controller
  1061. * instance for binding with the driver.
  1062. *
  1063. * Returns 0 if the controller instance was successfully bound to the
  1064. * driver; otherwise, < 0 on error.
  1065. */
  1066. static int ppc4xx_edac_probe(struct platform_device *op)
  1067. {
  1068. int status = 0;
  1069. u32 mcopt1, memcheck;
  1070. dcr_host_t dcr_host;
  1071. const struct device_node *np = op->dev.of_node;
  1072. struct mem_ctl_info *mci = NULL;
  1073. struct edac_mc_layer layers[2];
  1074. static int ppc4xx_edac_instance;
  1075. /*
  1076. * At this point, we only support the controller realized on
  1077. * the AMCC PPC 405EX[r]. Reject anything else.
  1078. */
  1079. if (!of_device_is_compatible(np, "ibm,sdram-405ex") &&
  1080. !of_device_is_compatible(np, "ibm,sdram-405exr")) {
  1081. ppc4xx_edac_printk(KERN_NOTICE,
  1082. "Only the PPC405EX[r] is supported.\n");
  1083. return -ENODEV;
  1084. }
  1085. /*
  1086. * Next, get the DCR property and attempt to map it so that we
  1087. * can probe the controller.
  1088. */
  1089. status = ppc4xx_edac_map_dcrs(np, &dcr_host);
  1090. if (status)
  1091. return status;
  1092. /*
  1093. * First determine whether ECC is enabled at all. If not,
  1094. * there is no useful checking or monitoring that can be done
  1095. * for this controller.
  1096. */
  1097. mcopt1 = mfsdram(&dcr_host, SDRAM_MCOPT1);
  1098. memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);
  1099. if (memcheck == SDRAM_MCOPT1_MCHK_NON) {
  1100. ppc4xx_edac_printk(KERN_INFO, "%s: No ECC memory detected or "
  1101. "ECC is disabled.\n", np->full_name);
  1102. status = -ENODEV;
  1103. goto done;
  1104. }
  1105. /*
  1106. * At this point, we know ECC is enabled, allocate an EDAC
  1107. * controller instance and perform the appropriate
  1108. * initialization.
  1109. */
  1110. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  1111. layers[0].size = ppc4xx_edac_nr_csrows;
  1112. layers[0].is_virt_csrow = true;
  1113. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  1114. layers[1].size = ppc4xx_edac_nr_chans;
  1115. layers[1].is_virt_csrow = false;
  1116. mci = edac_mc_alloc(ppc4xx_edac_instance, ARRAY_SIZE(layers), layers,
  1117. sizeof(struct ppc4xx_edac_pdata));
  1118. if (mci == NULL) {
  1119. ppc4xx_edac_printk(KERN_ERR, "%s: "
  1120. "Failed to allocate EDAC MC instance!\n",
  1121. np->full_name);
  1122. status = -ENOMEM;
  1123. goto done;
  1124. }
  1125. status = ppc4xx_edac_mc_init(mci, op, &dcr_host, mcopt1);
  1126. if (status) {
  1127. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  1128. "Failed to initialize instance!\n");
  1129. goto fail;
  1130. }
  1131. /*
  1132. * We have a valid, initialized EDAC instance bound to the
  1133. * controller. Attempt to register it with the EDAC subsystem
  1134. * and, if necessary, register interrupts.
  1135. */
  1136. if (edac_mc_add_mc(mci)) {
  1137. ppc4xx_edac_mc_printk(KERN_ERR, mci,
  1138. "Failed to add instance!\n");
  1139. status = -ENODEV;
  1140. goto fail;
  1141. }
  1142. if (edac_op_state == EDAC_OPSTATE_INT) {
  1143. status = ppc4xx_edac_register_irq(op, mci);
  1144. if (status)
  1145. goto fail1;
  1146. }
  1147. ppc4xx_edac_instance++;
  1148. return 0;
  1149. fail1:
  1150. edac_mc_del_mc(mci->pdev);
  1151. fail:
  1152. edac_mc_free(mci);
  1153. done:
  1154. return status;
  1155. }
  1156. /**
  1157. * ppc4xx_edac_remove - unbind driver from controller
  1158. * @op: A pointer to the OpenFirmware device tree node associated
  1159. * with the controller this EDAC instance is to be unbound/removed
  1160. * from.
  1161. *
  1162. * This routine unbinds the EDAC memory controller instance associated
  1163. * with the specified ibm,sdram-4xx-ddr2 controller described by the
  1164. * OpenFirmware device tree node passed as a parameter.
  1165. *
  1166. * Unconditionally returns 0.
  1167. */
  1168. static int
  1169. ppc4xx_edac_remove(struct platform_device *op)
  1170. {
  1171. struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);
  1172. struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
  1173. if (edac_op_state == EDAC_OPSTATE_INT) {
  1174. free_irq(pdata->irqs.sec, mci);
  1175. free_irq(pdata->irqs.ded, mci);
  1176. }
  1177. dcr_unmap(pdata->dcr_host, SDRAM_DCR_RESOURCE_LEN);
  1178. edac_mc_del_mc(mci->pdev);
  1179. edac_mc_free(mci);
  1180. return 0;
  1181. }
  1182. /**
  1183. * ppc4xx_edac_opstate_init - initialize EDAC reporting method
  1184. *
  1185. * This routine ensures that the EDAC memory controller reporting
  1186. * method is mapped to a sane value as the EDAC core defines the value
  1187. * to EDAC_OPSTATE_INVAL by default. We don't call the global
  1188. * opstate_init as that defaults to polling and we want interrupt as
  1189. * the default.
  1190. */
  1191. static inline void __init
  1192. ppc4xx_edac_opstate_init(void)
  1193. {
  1194. switch (edac_op_state) {
  1195. case EDAC_OPSTATE_POLL:
  1196. case EDAC_OPSTATE_INT:
  1197. break;
  1198. default:
  1199. edac_op_state = EDAC_OPSTATE_INT;
  1200. break;
  1201. }
  1202. ppc4xx_edac_printk(KERN_INFO, "Reporting type: %s\n",
  1203. ((edac_op_state == EDAC_OPSTATE_POLL) ?
  1204. EDAC_OPSTATE_POLL_STR :
  1205. ((edac_op_state == EDAC_OPSTATE_INT) ?
  1206. EDAC_OPSTATE_INT_STR :
  1207. EDAC_OPSTATE_UNKNOWN_STR)));
  1208. }
  1209. /**
  1210. * ppc4xx_edac_init - driver/module insertion entry point
  1211. *
  1212. * This routine is the driver/module insertion entry point. It
  1213. * initializes the EDAC memory controller reporting state and
  1214. * registers the driver as an OpenFirmware device tree platform
  1215. * driver.
  1216. */
  1217. static int __init
  1218. ppc4xx_edac_init(void)
  1219. {
  1220. ppc4xx_edac_printk(KERN_INFO, PPC4XX_EDAC_MODULE_REVISION "\n");
  1221. ppc4xx_edac_opstate_init();
  1222. return platform_driver_register(&ppc4xx_edac_driver);
  1223. }
  1224. /**
  1225. * ppc4xx_edac_exit - driver/module removal entry point
  1226. *
  1227. * This routine is the driver/module removal entry point. It
  1228. * unregisters the driver as an OpenFirmware device tree platform
  1229. * driver.
  1230. */
  1231. static void __exit
  1232. ppc4xx_edac_exit(void)
  1233. {
  1234. platform_driver_unregister(&ppc4xx_edac_driver);
  1235. }
  1236. module_init(ppc4xx_edac_init);
  1237. module_exit(ppc4xx_edac_exit);
  1238. MODULE_LICENSE("GPL v2");
  1239. MODULE_AUTHOR("Grant Erickson <gerickson@nuovations.com>");
  1240. MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");
  1241. module_param(edac_op_state, int, 0444);
  1242. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting State: "
  1243. "0=" EDAC_OPSTATE_POLL_STR ", 2=" EDAC_OPSTATE_INT_STR);