chmc.c 10 KB

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