chmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
  34. /* OBP memory-layout property format. */
  35. struct obp_map {
  36. unsigned char dimm_map[144];
  37. unsigned char pin_map[576];
  38. };
  39. #define DIMM_LABEL_SZ 8
  40. struct 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[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 obp_map map[2];
  51. };
  52. #define CHMCTRL_NBANKS 4
  53. struct bank_info {
  54. struct mctrl_info *mp;
  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 mctrl_info {
  67. struct list_head list;
  68. int portid;
  69. struct 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 bank_info logical_banks[CHMCTRL_NBANKS];
  78. };
  79. static LIST_HEAD(mctrl_list);
  80. /* Does BANK decode PHYS_ADDR? */
  81. static int bank_match(struct 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 bank_info *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 mctrl_info *mp =
  112. list_entry(mctrl_entry, struct mctrl_info, list);
  113. int bank_no;
  114. if (mctrl_entry == mctrl_head)
  115. break;
  116. mctrl_entry = mctrl_entry->next;
  117. for (bank_no = 0; bank_no < CHMCTRL_NBANKS; bank_no++) {
  118. struct bank_info *bp;
  119. bp = &mp->logical_banks[bank_no];
  120. if (bank_match(bp, phys_addr))
  121. return bp;
  122. }
  123. }
  124. return NULL;
  125. }
  126. /* This is the main purpose of this driver. */
  127. #define SYNDROME_MIN -1
  128. #define SYNDROME_MAX 144
  129. int chmc_getunumber(int syndrome_code,
  130. unsigned long phys_addr,
  131. char *buf, int buflen)
  132. {
  133. struct bank_info *bp;
  134. struct obp_mem_layout *prop;
  135. int bank_in_controller, first_dimm;
  136. bp = find_bank(phys_addr);
  137. if (bp == NULL ||
  138. syndrome_code < SYNDROME_MIN ||
  139. syndrome_code > SYNDROME_MAX) {
  140. buf[0] = '?';
  141. buf[1] = '?';
  142. buf[2] = '?';
  143. buf[3] = '\0';
  144. return 0;
  145. }
  146. prop = &bp->mp->layout_prop;
  147. bank_in_controller = bp->bank_id & (CHMCTRL_NBANKS - 1);
  148. first_dimm = (bank_in_controller & (CHMCTRL_NDGRPS - 1));
  149. first_dimm *= CHMCTRL_NDIMMS;
  150. if (syndrome_code != SYNDROME_MIN) {
  151. struct obp_map *map;
  152. int qword, where_in_line, where, map_index, map_offset;
  153. unsigned int map_val;
  154. /* Yaay, single bit error so we can figure out
  155. * the exact dimm.
  156. */
  157. if (prop->symmetric)
  158. map = &prop->map[0];
  159. else
  160. map = &prop->map[1];
  161. /* Covert syndrome code into the way the bits are
  162. * positioned on the bus.
  163. */
  164. if (syndrome_code < 144 - 16)
  165. syndrome_code += 16;
  166. else if (syndrome_code < 144)
  167. syndrome_code -= (144 - 7);
  168. else if (syndrome_code < (144 + 3))
  169. syndrome_code -= (144 + 3 - 4);
  170. else
  171. syndrome_code -= 144 + 3;
  172. /* All this magic has to do with how a cache line
  173. * comes over the wire on Safari. A 64-bit line
  174. * comes over in 4 quadword cycles, each of which
  175. * transmit ECC/MTAG info as well as the actual
  176. * data. 144 bits per quadword, 576 total.
  177. */
  178. #define LINE_SIZE 64
  179. #define LINE_ADDR_MSK (LINE_SIZE - 1)
  180. #define QW_PER_LINE 4
  181. #define QW_BYTES (LINE_SIZE / QW_PER_LINE)
  182. #define QW_BITS 144
  183. #define LAST_BIT (576 - 1)
  184. qword = (phys_addr & LINE_ADDR_MSK) / QW_BYTES;
  185. where_in_line = ((3 - qword) * QW_BITS) + syndrome_code;
  186. where = (LAST_BIT - where_in_line);
  187. map_index = where >> 2;
  188. map_offset = where & 0x3;
  189. map_val = map->dimm_map[map_index];
  190. map_val = ((map_val >> ((3 - map_offset) << 1)) & (2 - 1));
  191. sprintf(buf, "%s, pin %3d",
  192. prop->dimm_labels[first_dimm + map_val],
  193. map->pin_map[where_in_line]);
  194. } else {
  195. int dimm;
  196. /* Multi-bit error, we just dump out all the
  197. * dimm labels associated with this bank.
  198. */
  199. for (dimm = 0; dimm < CHMCTRL_NDIMMS; dimm++) {
  200. sprintf(buf, "%s ",
  201. prop->dimm_labels[first_dimm + dimm]);
  202. buf += strlen(buf);
  203. }
  204. }
  205. return 0;
  206. }
  207. /* Accessing the registers is slightly complicated. If you want
  208. * to get at the memory controller which is on the same processor
  209. * the code is executing, you must use special ASI load/store else
  210. * you go through the global mapping.
  211. */
  212. static u64 read_mcreg(struct mctrl_info *mp, unsigned long offset)
  213. {
  214. unsigned long ret, this_cpu;
  215. preempt_disable();
  216. this_cpu = real_hard_smp_processor_id();
  217. if (mp->portid == this_cpu) {
  218. __asm__ __volatile__("ldxa [%1] %2, %0"
  219. : "=r" (ret)
  220. : "r" (offset), "i" (ASI_MCU_CTRL_REG));
  221. } else {
  222. __asm__ __volatile__("ldxa [%1] %2, %0"
  223. : "=r" (ret)
  224. : "r" (mp->regs + offset),
  225. "i" (ASI_PHYS_BYPASS_EC_E));
  226. }
  227. preempt_enable();
  228. return ret;
  229. }
  230. #if 0 /* currently unused */
  231. static void write_mcreg(struct mctrl_info *mp, unsigned long offset, u64 val)
  232. {
  233. if (mp->portid == smp_processor_id()) {
  234. __asm__ __volatile__("stxa %0, [%1] %2"
  235. : : "r" (val),
  236. "r" (offset), "i" (ASI_MCU_CTRL_REG));
  237. } else {
  238. __asm__ __volatile__("ldxa %0, [%1] %2"
  239. : : "r" (val),
  240. "r" (mp->regs + offset),
  241. "i" (ASI_PHYS_BYPASS_EC_E));
  242. }
  243. }
  244. #endif
  245. static void interpret_one_decode_reg(struct mctrl_info *mp, int which_bank, u64 val)
  246. {
  247. struct bank_info *p = &mp->logical_banks[which_bank];
  248. p->mp = mp;
  249. p->bank_id = (CHMCTRL_NBANKS * mp->portid) + which_bank;
  250. p->raw_reg = val;
  251. p->valid = (val & MEM_DECODE_VALID) >> MEM_DECODE_VALID_SHIFT;
  252. p->uk = (val & MEM_DECODE_UK) >> MEM_DECODE_UK_SHIFT;
  253. p->um = (val & MEM_DECODE_UM) >> MEM_DECODE_UM_SHIFT;
  254. p->lk = (val & MEM_DECODE_LK) >> MEM_DECODE_LK_SHIFT;
  255. p->lm = (val & MEM_DECODE_LM) >> MEM_DECODE_LM_SHIFT;
  256. p->base = (p->um);
  257. p->base &= ~(p->uk);
  258. p->base <<= PA_UPPER_BITS_SHIFT;
  259. switch(p->lk) {
  260. case 0xf:
  261. default:
  262. p->interleave = 1;
  263. break;
  264. case 0xe:
  265. p->interleave = 2;
  266. break;
  267. case 0xc:
  268. p->interleave = 4;
  269. break;
  270. case 0x8:
  271. p->interleave = 8;
  272. break;
  273. case 0x0:
  274. p->interleave = 16;
  275. break;
  276. };
  277. /* UK[10] is reserved, and UK[11] is not set for the SDRAM
  278. * bank size definition.
  279. */
  280. p->size = (((unsigned long)p->uk &
  281. ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT;
  282. p->size /= p->interleave;
  283. }
  284. static void fetch_decode_regs(struct mctrl_info *mp)
  285. {
  286. if (mp->layout_size == 0)
  287. return;
  288. interpret_one_decode_reg(mp, 0,
  289. read_mcreg(mp, CHMCTRL_DECODE1));
  290. interpret_one_decode_reg(mp, 1,
  291. read_mcreg(mp, CHMCTRL_DECODE2));
  292. interpret_one_decode_reg(mp, 2,
  293. read_mcreg(mp, CHMCTRL_DECODE3));
  294. interpret_one_decode_reg(mp, 3,
  295. read_mcreg(mp, CHMCTRL_DECODE4));
  296. }
  297. static int __devinit chmc_probe(struct of_device *op,
  298. const struct of_device_id *match)
  299. {
  300. struct device_node *dp = op->node;
  301. struct mctrl_info *mp;
  302. unsigned long ver;
  303. const void *pval;
  304. int len, portid;
  305. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  306. if ((ver >> 32UL) == __JALAPENO_ID ||
  307. (ver >> 32UL) == __SERRANO_ID)
  308. return -ENODEV;
  309. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  310. if (!mp)
  311. return -ENOMEM;
  312. portid = of_getintprop_default(dp, "portid", -1);
  313. if (portid == -1)
  314. goto fail;
  315. mp->portid = portid;
  316. pval = of_get_property(dp, "memory-layout", &len);
  317. mp->layout_size = len;
  318. if (!pval)
  319. mp->layout_size = 0;
  320. else {
  321. if (mp->layout_size > sizeof(mp->layout_prop)) {
  322. printk(KERN_ERR PFX "Unexpected memory-layout property "
  323. "size %d.\n", mp->layout_size);
  324. goto fail;
  325. }
  326. memcpy(&mp->layout_prop, pval, len);
  327. }
  328. mp->regs = of_ioremap(&op->resource[0], 0, 0x48, "chmc");
  329. if (!mp->regs) {
  330. printk(KERN_ERR PFX "Could not map registers.\n");
  331. goto fail;
  332. }
  333. if (mp->layout_size != 0UL) {
  334. mp->timing_control1 = read_mcreg(mp, CHMCTRL_TCTRL1);
  335. mp->timing_control2 = read_mcreg(mp, CHMCTRL_TCTRL2);
  336. mp->timing_control3 = read_mcreg(mp, CHMCTRL_TCTRL3);
  337. mp->timing_control4 = read_mcreg(mp, CHMCTRL_TCTRL4);
  338. mp->memaddr_control = read_mcreg(mp, CHMCTRL_MACTRL);
  339. }
  340. fetch_decode_regs(mp);
  341. list_add(&mp->list, &mctrl_list);
  342. /* Report the device. */
  343. printk(KERN_INFO PFX "UltraSPARC-III memory controller at %s [%s]\n",
  344. dp->full_name,
  345. (mp->layout_size ? "ACTIVE" : "INACTIVE"));
  346. dev_set_drvdata(&op->dev, mp);
  347. return 0;
  348. fail:
  349. if (mp) {
  350. if (mp->regs != NULL)
  351. of_iounmap(&op->resource[0], mp->regs, 0x48);
  352. kfree(mp);
  353. }
  354. return -1;
  355. }
  356. static int __devexit chmc_remove(struct of_device *op)
  357. {
  358. struct mctrl_info *mp = dev_get_drvdata(&op->dev);
  359. if (mp) {
  360. list_del(&mp->list);
  361. of_iounmap(&op->resource[0], mp->regs, 0x48);
  362. kfree(mp);
  363. }
  364. return 0;
  365. }
  366. static struct of_device_id chmc_match[] = {
  367. {
  368. .name = "memory-controller",
  369. },
  370. {},
  371. };
  372. MODULE_DEVICE_TABLE(of, chmc_match);
  373. static struct of_platform_driver chmc_driver = {
  374. .name = "chmc",
  375. .match_table = chmc_match,
  376. .probe = chmc_probe,
  377. .remove = __devexit_p(chmc_remove),
  378. };
  379. static inline bool chmc_platform(void)
  380. {
  381. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  382. return true;
  383. return false;
  384. }
  385. static int __init chmc_init(void)
  386. {
  387. if (!chmc_platform())
  388. return -ENODEV;
  389. return of_register_driver(&chmc_driver, &of_bus_type);
  390. }
  391. static void __exit chmc_cleanup(void)
  392. {
  393. if (chmc_platform())
  394. of_unregister_driver(&chmc_driver);
  395. }
  396. module_init(chmc_init);
  397. module_exit(chmc_cleanup);