chmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* chmc.c: Driver for UltraSPARC-III memory controller.
  2. *
  3. * Copyright (C) 2001, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/slab.h>
  9. #include <linux/list.h>
  10. #include <linux/string.h>
  11. #include <linux/sched.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <asm/spitfire.h>
  18. #include <asm/chmctrl.h>
  19. #include <asm/cpudata.h>
  20. #include <asm/oplib.h>
  21. #include <asm/prom.h>
  22. #include <asm/head.h>
  23. #include <asm/io.h>
  24. #define DRV_MODULE_NAME "chmc"
  25. #define PFX DRV_MODULE_NAME ": "
  26. #define DRV_MODULE_VERSION "0.2"
  27. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  28. MODULE_DESCRIPTION("UltraSPARC-III memory controller driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION(DRV_MODULE_VERSION);
  31. #define CHMCTRL_NDGRPS 2
  32. #define CHMCTRL_NDIMMS 4
  33. #define CHMC_DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
  34. /* OBP memory-layout property format. */
  35. struct chmc_obp_map {
  36. unsigned char dimm_map[144];
  37. unsigned char pin_map[576];
  38. };
  39. #define DIMM_LABEL_SZ 8
  40. struct chmc_obp_mem_layout {
  41. /* One max 8-byte string label per DIMM. Usually
  42. * this matches the label on the motherboard where
  43. * that DIMM resides.
  44. */
  45. char dimm_labels[CHMC_DIMMS_PER_MC][DIMM_LABEL_SZ];
  46. /* If symmetric use map[0], else it is
  47. * asymmetric and map[1] should be used.
  48. */
  49. char symmetric;
  50. struct chmc_obp_map map[2];
  51. };
  52. #define CHMCTRL_NBANKS 4
  53. struct chmc_bank_info {
  54. struct chmc *p;
  55. int bank_id;
  56. u64 raw_reg;
  57. int valid;
  58. int uk;
  59. int um;
  60. int lk;
  61. int lm;
  62. int interleave;
  63. unsigned long base;
  64. unsigned long size;
  65. };
  66. struct chmc {
  67. struct list_head list;
  68. int portid;
  69. struct chmc_obp_mem_layout layout_prop;
  70. int layout_size;
  71. void __iomem *regs;
  72. u64 timing_control1;
  73. u64 timing_control2;
  74. u64 timing_control3;
  75. u64 timing_control4;
  76. u64 memaddr_control;
  77. struct chmc_bank_info logical_banks[CHMCTRL_NBANKS];
  78. };
  79. static LIST_HEAD(mctrl_list);
  80. /* Does BANK decode PHYS_ADDR? */
  81. static int chmc_bank_match(struct chmc_bank_info *bp, unsigned long phys_addr)
  82. {
  83. unsigned long upper_bits = (phys_addr & PA_UPPER_BITS) >> PA_UPPER_BITS_SHIFT;
  84. unsigned long lower_bits = (phys_addr & PA_LOWER_BITS) >> PA_LOWER_BITS_SHIFT;
  85. /* Bank must be enabled to match. */
  86. if (bp->valid == 0)
  87. return 0;
  88. /* Would BANK match upper bits? */
  89. upper_bits ^= bp->um; /* What bits are different? */
  90. upper_bits = ~upper_bits; /* Invert. */
  91. upper_bits |= bp->uk; /* What bits don't matter for matching? */
  92. upper_bits = ~upper_bits; /* Invert. */
  93. if (upper_bits)
  94. return 0;
  95. /* Would BANK match lower bits? */
  96. lower_bits ^= bp->lm; /* What bits are different? */
  97. lower_bits = ~lower_bits; /* Invert. */
  98. lower_bits |= bp->lk; /* What bits don't matter for matching? */
  99. lower_bits = ~lower_bits; /* Invert. */
  100. if (lower_bits)
  101. return 0;
  102. /* I always knew you'd be the one. */
  103. return 1;
  104. }
  105. /* Given PHYS_ADDR, search memory controller banks for a match. */
  106. static struct chmc_bank_info *chmc_find_bank(unsigned long phys_addr)
  107. {
  108. struct list_head *mctrl_head = &mctrl_list;
  109. struct list_head *mctrl_entry = mctrl_head->next;
  110. for (;;) {
  111. struct chmc *p = list_entry(mctrl_entry, struct chmc, list);
  112. int bank_no;
  113. if (mctrl_entry == mctrl_head)
  114. break;
  115. mctrl_entry = mctrl_entry->next;
  116. for (bank_no = 0; bank_no < CHMCTRL_NBANKS; bank_no++) {
  117. struct chmc_bank_info *bp;
  118. bp = &p->logical_banks[bank_no];
  119. if (chmc_bank_match(bp, phys_addr))
  120. return bp;
  121. }
  122. }
  123. return NULL;
  124. }
  125. /* This is the main purpose of this driver. */
  126. #define SYNDROME_MIN -1
  127. #define SYNDROME_MAX 144
  128. int chmc_getunumber(int syndrome_code,
  129. unsigned long phys_addr,
  130. char *buf, int buflen)
  131. {
  132. struct chmc_bank_info *bp;
  133. struct chmc_obp_mem_layout *prop;
  134. int bank_in_controller, first_dimm;
  135. bp = chmc_find_bank(phys_addr);
  136. if (bp == NULL ||
  137. syndrome_code < SYNDROME_MIN ||
  138. syndrome_code > SYNDROME_MAX) {
  139. buf[0] = '?';
  140. buf[1] = '?';
  141. buf[2] = '?';
  142. buf[3] = '\0';
  143. return 0;
  144. }
  145. prop = &bp->p->layout_prop;
  146. bank_in_controller = bp->bank_id & (CHMCTRL_NBANKS - 1);
  147. first_dimm = (bank_in_controller & (CHMCTRL_NDGRPS - 1));
  148. first_dimm *= CHMCTRL_NDIMMS;
  149. if (syndrome_code != SYNDROME_MIN) {
  150. struct chmc_obp_map *map;
  151. int qword, where_in_line, where, map_index, map_offset;
  152. unsigned int map_val;
  153. /* Yaay, single bit error so we can figure out
  154. * the exact dimm.
  155. */
  156. if (prop->symmetric)
  157. map = &prop->map[0];
  158. else
  159. map = &prop->map[1];
  160. /* Covert syndrome code into the way the bits are
  161. * positioned on the bus.
  162. */
  163. if (syndrome_code < 144 - 16)
  164. syndrome_code += 16;
  165. else if (syndrome_code < 144)
  166. syndrome_code -= (144 - 7);
  167. else if (syndrome_code < (144 + 3))
  168. syndrome_code -= (144 + 3 - 4);
  169. else
  170. syndrome_code -= 144 + 3;
  171. /* All this magic has to do with how a cache line
  172. * comes over the wire on Safari. A 64-bit line
  173. * comes over in 4 quadword cycles, each of which
  174. * transmit ECC/MTAG info as well as the actual
  175. * data. 144 bits per quadword, 576 total.
  176. */
  177. #define LINE_SIZE 64
  178. #define LINE_ADDR_MSK (LINE_SIZE - 1)
  179. #define QW_PER_LINE 4
  180. #define QW_BYTES (LINE_SIZE / QW_PER_LINE)
  181. #define QW_BITS 144
  182. #define LAST_BIT (576 - 1)
  183. qword = (phys_addr & LINE_ADDR_MSK) / QW_BYTES;
  184. where_in_line = ((3 - qword) * QW_BITS) + syndrome_code;
  185. where = (LAST_BIT - where_in_line);
  186. map_index = where >> 2;
  187. map_offset = where & 0x3;
  188. map_val = map->dimm_map[map_index];
  189. map_val = ((map_val >> ((3 - map_offset) << 1)) & (2 - 1));
  190. sprintf(buf, "%s, pin %3d",
  191. prop->dimm_labels[first_dimm + map_val],
  192. map->pin_map[where_in_line]);
  193. } else {
  194. int dimm;
  195. /* Multi-bit error, we just dump out all the
  196. * dimm labels associated with this bank.
  197. */
  198. for (dimm = 0; dimm < CHMCTRL_NDIMMS; dimm++) {
  199. sprintf(buf, "%s ",
  200. prop->dimm_labels[first_dimm + dimm]);
  201. buf += strlen(buf);
  202. }
  203. }
  204. return 0;
  205. }
  206. /* Accessing the registers is slightly complicated. If you want
  207. * to get at the memory controller which is on the same processor
  208. * the code is executing, you must use special ASI load/store else
  209. * you go through the global mapping.
  210. */
  211. static u64 chmc_read_mcreg(struct chmc *p, unsigned long offset)
  212. {
  213. unsigned long ret, this_cpu;
  214. preempt_disable();
  215. this_cpu = real_hard_smp_processor_id();
  216. if (p->portid == this_cpu) {
  217. __asm__ __volatile__("ldxa [%1] %2, %0"
  218. : "=r" (ret)
  219. : "r" (offset), "i" (ASI_MCU_CTRL_REG));
  220. } else {
  221. __asm__ __volatile__("ldxa [%1] %2, %0"
  222. : "=r" (ret)
  223. : "r" (p->regs + offset),
  224. "i" (ASI_PHYS_BYPASS_EC_E));
  225. }
  226. preempt_enable();
  227. return ret;
  228. }
  229. #if 0 /* currently unused */
  230. static void chmc_write_mcreg(struct chmc *p, unsigned long offset, u64 val)
  231. {
  232. if (p->portid == smp_processor_id()) {
  233. __asm__ __volatile__("stxa %0, [%1] %2"
  234. : : "r" (val),
  235. "r" (offset), "i" (ASI_MCU_CTRL_REG));
  236. } else {
  237. __asm__ __volatile__("ldxa %0, [%1] %2"
  238. : : "r" (val),
  239. "r" (p->regs + offset),
  240. "i" (ASI_PHYS_BYPASS_EC_E));
  241. }
  242. }
  243. #endif
  244. static void chmc_interpret_one_decode_reg(struct chmc *p, int which_bank, u64 val)
  245. {
  246. struct chmc_bank_info *bp = &p->logical_banks[which_bank];
  247. bp->p = p;
  248. bp->bank_id = (CHMCTRL_NBANKS * p->portid) + which_bank;
  249. bp->raw_reg = val;
  250. bp->valid = (val & MEM_DECODE_VALID) >> MEM_DECODE_VALID_SHIFT;
  251. bp->uk = (val & MEM_DECODE_UK) >> MEM_DECODE_UK_SHIFT;
  252. bp->um = (val & MEM_DECODE_UM) >> MEM_DECODE_UM_SHIFT;
  253. bp->lk = (val & MEM_DECODE_LK) >> MEM_DECODE_LK_SHIFT;
  254. bp->lm = (val & MEM_DECODE_LM) >> MEM_DECODE_LM_SHIFT;
  255. bp->base = (bp->um);
  256. bp->base &= ~(bp->uk);
  257. bp->base <<= PA_UPPER_BITS_SHIFT;
  258. switch(bp->lk) {
  259. case 0xf:
  260. default:
  261. bp->interleave = 1;
  262. break;
  263. case 0xe:
  264. bp->interleave = 2;
  265. break;
  266. case 0xc:
  267. bp->interleave = 4;
  268. break;
  269. case 0x8:
  270. bp->interleave = 8;
  271. break;
  272. case 0x0:
  273. bp->interleave = 16;
  274. break;
  275. };
  276. /* UK[10] is reserved, and UK[11] is not set for the SDRAM
  277. * bank size definition.
  278. */
  279. bp->size = (((unsigned long)bp->uk &
  280. ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT;
  281. bp->size /= bp->interleave;
  282. }
  283. static void chmc_fetch_decode_regs(struct chmc *p)
  284. {
  285. if (p->layout_size == 0)
  286. return;
  287. chmc_interpret_one_decode_reg(p, 0,
  288. chmc_read_mcreg(p, CHMCTRL_DECODE1));
  289. chmc_interpret_one_decode_reg(p, 1,
  290. chmc_read_mcreg(p, CHMCTRL_DECODE2));
  291. chmc_interpret_one_decode_reg(p, 2,
  292. chmc_read_mcreg(p, CHMCTRL_DECODE3));
  293. chmc_interpret_one_decode_reg(p, 3,
  294. chmc_read_mcreg(p, CHMCTRL_DECODE4));
  295. }
  296. static int __devinit chmc_probe(struct of_device *op,
  297. const struct of_device_id *match)
  298. {
  299. struct device_node *dp = op->node;
  300. unsigned long ver;
  301. const void *pval;
  302. int len, portid;
  303. struct chmc *p;
  304. int err;
  305. err = -ENODEV;
  306. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  307. if ((ver >> 32UL) == __JALAPENO_ID ||
  308. (ver >> 32UL) == __SERRANO_ID)
  309. goto out;
  310. portid = of_getintprop_default(dp, "portid", -1);
  311. if (portid == -1)
  312. goto out;
  313. pval = of_get_property(dp, "memory-layout", &len);
  314. if (pval && len > sizeof(p->layout_prop)) {
  315. printk(KERN_ERR PFX "Unexpected memory-layout property "
  316. "size %d.\n", len);
  317. goto out;
  318. }
  319. err = -ENOMEM;
  320. p = kzalloc(sizeof(*p), GFP_KERNEL);
  321. if (!p) {
  322. printk(KERN_ERR PFX "Could not allocate struct chmc.\n");
  323. goto out;
  324. }
  325. p->portid = portid;
  326. p->layout_size = len;
  327. if (!pval)
  328. p->layout_size = 0;
  329. else
  330. memcpy(&p->layout_prop, pval, len);
  331. p->regs = of_ioremap(&op->resource[0], 0, 0x48, "chmc");
  332. if (!p->regs) {
  333. printk(KERN_ERR PFX "Could not map registers.\n");
  334. goto out_free;
  335. }
  336. if (p->layout_size != 0UL) {
  337. p->timing_control1 = chmc_read_mcreg(p, CHMCTRL_TCTRL1);
  338. p->timing_control2 = chmc_read_mcreg(p, CHMCTRL_TCTRL2);
  339. p->timing_control3 = chmc_read_mcreg(p, CHMCTRL_TCTRL3);
  340. p->timing_control4 = chmc_read_mcreg(p, CHMCTRL_TCTRL4);
  341. p->memaddr_control = chmc_read_mcreg(p, CHMCTRL_MACTRL);
  342. }
  343. chmc_fetch_decode_regs(p);
  344. list_add(&p->list, &mctrl_list);
  345. /* Report the device. */
  346. printk(KERN_INFO PFX "UltraSPARC-III memory controller at %s [%s]\n",
  347. dp->full_name,
  348. (p->layout_size ? "ACTIVE" : "INACTIVE"));
  349. dev_set_drvdata(&op->dev, p);
  350. err = 0;
  351. out:
  352. return err;
  353. out_free:
  354. kfree(p);
  355. goto out;
  356. }
  357. static int __devexit chmc_remove(struct of_device *op)
  358. {
  359. struct chmc *p = dev_get_drvdata(&op->dev);
  360. if (p) {
  361. list_del(&p->list);
  362. of_iounmap(&op->resource[0], p->regs, 0x48);
  363. kfree(p);
  364. }
  365. return 0;
  366. }
  367. static struct of_device_id chmc_match[] = {
  368. {
  369. .name = "memory-controller",
  370. },
  371. {},
  372. };
  373. MODULE_DEVICE_TABLE(of, chmc_match);
  374. static struct of_platform_driver chmc_driver = {
  375. .name = "chmc",
  376. .match_table = chmc_match,
  377. .probe = chmc_probe,
  378. .remove = __devexit_p(chmc_remove),
  379. };
  380. static inline bool chmc_platform(void)
  381. {
  382. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  383. return true;
  384. return false;
  385. }
  386. static int __init chmc_init(void)
  387. {
  388. if (!chmc_platform())
  389. return -ENODEV;
  390. return of_register_driver(&chmc_driver, &of_bus_type);
  391. }
  392. static void __exit chmc_cleanup(void)
  393. {
  394. if (chmc_platform())
  395. of_unregister_driver(&chmc_driver);
  396. }
  397. module_init(chmc_init);
  398. module_exit(chmc_cleanup);