chmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/prom.h>
  20. #include <asm/io.h>
  21. #define CHMCTRL_NDGRPS 2
  22. #define CHMCTRL_NDIMMS 4
  23. #define DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
  24. /* OBP memory-layout property format. */
  25. struct obp_map {
  26. unsigned char dimm_map[144];
  27. unsigned char pin_map[576];
  28. };
  29. #define DIMM_LABEL_SZ 8
  30. struct obp_mem_layout {
  31. /* One max 8-byte string label per DIMM. Usually
  32. * this matches the label on the motherboard where
  33. * that DIMM resides.
  34. */
  35. char dimm_labels[DIMMS_PER_MC][DIMM_LABEL_SZ];
  36. /* If symmetric use map[0], else it is
  37. * asymmetric and map[1] should be used.
  38. */
  39. char symmetric;
  40. struct obp_map map[2];
  41. };
  42. #define CHMCTRL_NBANKS 4
  43. struct bank_info {
  44. struct mctrl_info *mp;
  45. int bank_id;
  46. u64 raw_reg;
  47. int valid;
  48. int uk;
  49. int um;
  50. int lk;
  51. int lm;
  52. int interleave;
  53. unsigned long base;
  54. unsigned long size;
  55. };
  56. struct mctrl_info {
  57. struct list_head list;
  58. int portid;
  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(struct device_node *dp)
  287. {
  288. struct mctrl_info *mp = kmalloc(sizeof(*mp), GFP_KERNEL);
  289. int portid = of_getintprop_default(dp, "portid", -1);
  290. struct linux_prom64_registers *regs;
  291. void *pval;
  292. int len;
  293. if (!mp)
  294. return -1;
  295. memset(mp, 0, sizeof(*mp));
  296. if (portid == -1)
  297. goto fail;
  298. mp->portid = portid;
  299. pval = of_get_property(dp, "memory-layout", &len);
  300. mp->layout_size = len;
  301. if (!pval)
  302. mp->layout_size = 0;
  303. else {
  304. if (mp->layout_size > sizeof(mp->layout_prop))
  305. goto fail;
  306. memcpy(&mp->layout_prop, pval, len);
  307. }
  308. regs = of_get_property(dp, "reg", NULL);
  309. if (!regs || regs->reg_size != 0x48)
  310. goto fail;
  311. mp->regs = ioremap(regs->phys_addr, regs->reg_size);
  312. if (mp->regs == NULL)
  313. goto fail;
  314. if (mp->layout_size != 0UL) {
  315. mp->timing_control1 = read_mcreg(mp, CHMCTRL_TCTRL1);
  316. mp->timing_control2 = read_mcreg(mp, CHMCTRL_TCTRL2);
  317. mp->timing_control3 = read_mcreg(mp, CHMCTRL_TCTRL3);
  318. mp->timing_control4 = read_mcreg(mp, CHMCTRL_TCTRL4);
  319. mp->memaddr_control = read_mcreg(mp, CHMCTRL_MACTRL);
  320. }
  321. fetch_decode_regs(mp);
  322. list_add(&mp->list, &mctrl_list);
  323. /* Report the device. */
  324. printk(KERN_INFO "%s: US3 memory controller at %p [%s]\n",
  325. dp->full_name,
  326. mp->regs, (mp->layout_size ? "ACTIVE" : "INACTIVE"));
  327. return 0;
  328. fail:
  329. if (mp) {
  330. if (mp->regs != NULL)
  331. iounmap(mp->regs);
  332. kfree(mp);
  333. }
  334. return -1;
  335. }
  336. static int __init chmc_init(void)
  337. {
  338. struct device_node *dp;
  339. /* This driver is only for cheetah platforms. */
  340. if (tlb_type != cheetah && tlb_type != cheetah_plus)
  341. return -ENODEV;
  342. for_each_node_by_name(dp, "memory-controller")
  343. init_one_mctrl(dp);
  344. for_each_node_by_name(dp, "mc-us3")
  345. init_one_mctrl(dp);
  346. return 0;
  347. }
  348. static void __exit chmc_cleanup(void)
  349. {
  350. struct list_head *head = &mctrl_list;
  351. struct list_head *tmp = head->next;
  352. for (;;) {
  353. struct mctrl_info *p =
  354. list_entry(tmp, struct mctrl_info, list);
  355. if (tmp == head)
  356. break;
  357. tmp = tmp->next;
  358. list_del(&p->list);
  359. iounmap(p->regs);
  360. kfree(p);
  361. }
  362. }
  363. module_init(chmc_init);
  364. module_exit(chmc_cleanup);