chmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. #include <asm/memctrl.h>
  25. #define DRV_MODULE_NAME "chmc"
  26. #define PFX DRV_MODULE_NAME ": "
  27. #define DRV_MODULE_VERSION "0.2"
  28. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  29. MODULE_DESCRIPTION("UltraSPARC-III memory controller driver");
  30. MODULE_LICENSE("GPL");
  31. MODULE_VERSION(DRV_MODULE_VERSION);
  32. static int mc_type;
  33. #define MC_TYPE_SAFARI 1
  34. #define MC_TYPE_JBUS 2
  35. static dimm_printer_t us3mc_dimm_printer;
  36. #define CHMCTRL_NDGRPS 2
  37. #define CHMCTRL_NDIMMS 4
  38. #define CHMC_DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
  39. /* OBP memory-layout property format. */
  40. struct chmc_obp_map {
  41. unsigned char dimm_map[144];
  42. unsigned char pin_map[576];
  43. };
  44. #define DIMM_LABEL_SZ 8
  45. struct chmc_obp_mem_layout {
  46. /* One max 8-byte string label per DIMM. Usually
  47. * this matches the label on the motherboard where
  48. * that DIMM resides.
  49. */
  50. char dimm_labels[CHMC_DIMMS_PER_MC][DIMM_LABEL_SZ];
  51. /* If symmetric use map[0], else it is
  52. * asymmetric and map[1] should be used.
  53. */
  54. char symmetric;
  55. struct chmc_obp_map map[2];
  56. };
  57. #define CHMCTRL_NBANKS 4
  58. struct chmc_bank_info {
  59. struct chmc *p;
  60. int bank_id;
  61. u64 raw_reg;
  62. int valid;
  63. int uk;
  64. int um;
  65. int lk;
  66. int lm;
  67. int interleave;
  68. unsigned long base;
  69. unsigned long size;
  70. };
  71. struct chmc {
  72. struct list_head list;
  73. int portid;
  74. struct chmc_obp_mem_layout layout_prop;
  75. int layout_size;
  76. void __iomem *regs;
  77. u64 timing_control1;
  78. u64 timing_control2;
  79. u64 timing_control3;
  80. u64 timing_control4;
  81. u64 memaddr_control;
  82. struct chmc_bank_info logical_banks[CHMCTRL_NBANKS];
  83. };
  84. #define JBUSMC_REGS_SIZE 8
  85. #define JB_MC_REG1_DIMM2_BANK3 0x8000000000000000UL
  86. #define JB_MC_REG1_DIMM1_BANK1 0x4000000000000000UL
  87. #define JB_MC_REG1_DIMM2_BANK2 0x2000000000000000UL
  88. #define JB_MC_REG1_DIMM1_BANK0 0x1000000000000000UL
  89. #define JB_MC_REG1_XOR 0x0000010000000000UL
  90. #define JB_MC_REG1_ADDR_GEN_2 0x000000e000000000UL
  91. #define JB_MC_REG1_ADDR_GEN_2_SHIFT 37
  92. #define JB_MC_REG1_ADDR_GEN_1 0x0000001c00000000UL
  93. #define JB_MC_REG1_ADDR_GEN_1_SHIFT 34
  94. #define JB_MC_REG1_INTERLEAVE 0x0000000001800000UL
  95. #define JB_MC_REG1_INTERLEAVE_SHIFT 23
  96. #define JB_MC_REG1_DIMM2_PTYPE 0x0000000000200000UL
  97. #define JB_MC_REG1_DIMM2_PTYPE_SHIFT 21
  98. #define JB_MC_REG1_DIMM1_PTYPE 0x0000000000100000UL
  99. #define JB_MC_REG1_DIMM1_PTYPE_SHIFT 20
  100. #define PART_TYPE_X8 0
  101. #define PART_TYPE_X4 1
  102. #define INTERLEAVE_NONE 0
  103. #define INTERLEAVE_SAME 1
  104. #define INTERLEAVE_INTERNAL 2
  105. #define INTERLEAVE_BOTH 3
  106. #define ADDR_GEN_128MB 0
  107. #define ADDR_GEN_256MB 1
  108. #define ADDR_GEN_512MB 2
  109. #define ADDR_GEN_1GB 3
  110. #define JB_NUM_DIMM_GROUPS 2
  111. #define JB_NUM_DIMMS_PER_GROUP 2
  112. #define JB_NUM_DIMMS (JB_NUM_DIMM_GROUPS * JB_NUM_DIMMS_PER_GROUP)
  113. struct jbusmc_obp_map {
  114. unsigned char dimm_map[18];
  115. unsigned char pin_map[144];
  116. };
  117. struct jbusmc_obp_mem_layout {
  118. /* One max 8-byte string label per DIMM. Usually
  119. * this matches the label on the motherboard where
  120. * that DIMM resides.
  121. */
  122. char dimm_labels[JB_NUM_DIMMS][DIMM_LABEL_SZ];
  123. /* If symmetric use map[0], else it is
  124. * asymmetric and map[1] should be used.
  125. */
  126. char symmetric;
  127. struct jbusmc_obp_map map;
  128. char _pad;
  129. };
  130. struct jbusmc_dimm_group {
  131. struct jbusmc *controller;
  132. int index;
  133. u64 base_addr;
  134. u64 size;
  135. };
  136. struct jbusmc {
  137. void __iomem *regs;
  138. u64 mc_reg_1;
  139. u32 portid;
  140. struct jbusmc_obp_mem_layout layout;
  141. int layout_len;
  142. int num_dimm_groups;
  143. struct jbusmc_dimm_group dimm_groups[JB_NUM_DIMM_GROUPS];
  144. struct list_head list;
  145. };
  146. static DEFINE_SPINLOCK(mctrl_list_lock);
  147. static LIST_HEAD(mctrl_list);
  148. static void mc_list_add(struct list_head *list)
  149. {
  150. spin_lock(&mctrl_list_lock);
  151. list_add(list, &mctrl_list);
  152. spin_unlock(&mctrl_list_lock);
  153. }
  154. static void mc_list_del(struct list_head *list)
  155. {
  156. spin_lock(&mctrl_list_lock);
  157. list_del_init(list);
  158. spin_unlock(&mctrl_list_lock);
  159. }
  160. #define SYNDROME_MIN -1
  161. #define SYNDROME_MAX 144
  162. /* Covert syndrome code into the way the bits are positioned
  163. * on the bus.
  164. */
  165. static int syndrome_to_qword_code(int syndrome_code)
  166. {
  167. if (syndrome_code < 128)
  168. syndrome_code += 16;
  169. else if (syndrome_code < 128 + 9)
  170. syndrome_code -= (128 - 7);
  171. else if (syndrome_code < (128 + 9 + 3))
  172. syndrome_code -= (128 + 9 - 4);
  173. else
  174. syndrome_code -= (128 + 9 + 3);
  175. return syndrome_code;
  176. }
  177. /* All this magic has to do with how a cache line comes over the wire
  178. * on Safari and JBUS. A 64-bit line comes over in 1 or more quadword
  179. * cycles, each of which transmit ECC/MTAG info as well as the actual
  180. * data.
  181. */
  182. #define L2_LINE_SIZE 64
  183. #define L2_LINE_ADDR_MSK (L2_LINE_SIZE - 1)
  184. #define QW_PER_LINE 4
  185. #define QW_BYTES (L2_LINE_SIZE / QW_PER_LINE)
  186. #define QW_BITS 144
  187. #define SAFARI_LAST_BIT (576 - 1)
  188. #define JBUS_LAST_BIT (144 - 1)
  189. static void get_pin_and_dimm_str(int syndrome_code, unsigned long paddr,
  190. int *pin_p, char **dimm_str_p, void *_prop,
  191. int base_dimm_offset)
  192. {
  193. int qword_code = syndrome_to_qword_code(syndrome_code);
  194. int cache_line_offset;
  195. int offset_inverse;
  196. int dimm_map_index;
  197. int map_val;
  198. if (mc_type == MC_TYPE_JBUS) {
  199. struct jbusmc_obp_mem_layout *p = _prop;
  200. /* JBUS */
  201. cache_line_offset = qword_code;
  202. offset_inverse = (JBUS_LAST_BIT - cache_line_offset);
  203. dimm_map_index = offset_inverse / 8;
  204. map_val = p->map.dimm_map[dimm_map_index];
  205. map_val = ((map_val >> ((7 - (offset_inverse & 7)))) & 1);
  206. *dimm_str_p = p->dimm_labels[base_dimm_offset + map_val];
  207. *pin_p = p->map.pin_map[cache_line_offset];
  208. } else {
  209. struct chmc_obp_mem_layout *p = _prop;
  210. struct chmc_obp_map *mp;
  211. int qword;
  212. /* Safari */
  213. if (p->symmetric)
  214. mp = &p->map[0];
  215. else
  216. mp = &p->map[1];
  217. qword = (paddr & L2_LINE_ADDR_MSK) / QW_BYTES;
  218. cache_line_offset = ((3 - qword) * QW_BITS) + qword_code;
  219. offset_inverse = (SAFARI_LAST_BIT - cache_line_offset);
  220. dimm_map_index = offset_inverse >> 2;
  221. map_val = mp->dimm_map[dimm_map_index];
  222. map_val = ((map_val >> ((3 - (offset_inverse & 3)) << 1)) & 0x3);
  223. *dimm_str_p = p->dimm_labels[base_dimm_offset + map_val];
  224. *pin_p = mp->pin_map[cache_line_offset];
  225. }
  226. }
  227. static struct jbusmc_dimm_group *jbusmc_find_dimm_group(unsigned long phys_addr)
  228. {
  229. struct jbusmc *p;
  230. list_for_each_entry(p, &mctrl_list, list) {
  231. int i;
  232. for (i = 0; i < p->num_dimm_groups; i++) {
  233. struct jbusmc_dimm_group *dp = &p->dimm_groups[i];
  234. if (phys_addr < dp->base_addr ||
  235. (dp->base_addr + dp->size) <= phys_addr)
  236. continue;
  237. return dp;
  238. }
  239. }
  240. return NULL;
  241. }
  242. static int jbusmc_print_dimm(int syndrome_code,
  243. unsigned long phys_addr,
  244. char *buf, int buflen)
  245. {
  246. struct jbusmc_obp_mem_layout *prop;
  247. struct jbusmc_dimm_group *dp;
  248. struct jbusmc *p;
  249. int first_dimm;
  250. dp = jbusmc_find_dimm_group(phys_addr);
  251. if (dp == NULL ||
  252. syndrome_code < SYNDROME_MIN ||
  253. syndrome_code > SYNDROME_MAX) {
  254. buf[0] = '?';
  255. buf[1] = '?';
  256. buf[2] = '?';
  257. buf[3] = '\0';
  258. return 0;
  259. }
  260. p = dp->controller;
  261. prop = &p->layout;
  262. first_dimm = dp->index * JB_NUM_DIMMS_PER_GROUP;
  263. if (syndrome_code != SYNDROME_MIN) {
  264. char *dimm_str;
  265. int pin;
  266. get_pin_and_dimm_str(syndrome_code, phys_addr, &pin,
  267. &dimm_str, prop, first_dimm);
  268. sprintf(buf, "%s, pin %3d", dimm_str, pin);
  269. } else {
  270. int dimm;
  271. /* Multi-bit error, we just dump out all the
  272. * dimm labels associated with this dimm group.
  273. */
  274. for (dimm = 0; dimm < JB_NUM_DIMMS_PER_GROUP; dimm++) {
  275. sprintf(buf, "%s ",
  276. prop->dimm_labels[first_dimm + dimm]);
  277. buf += strlen(buf);
  278. }
  279. }
  280. return 0;
  281. }
  282. static u64 __devinit jbusmc_dimm_group_size(u64 base,
  283. const struct linux_prom64_registers *mem_regs,
  284. int num_mem_regs)
  285. {
  286. u64 max = base + (8UL * 1024 * 1024 * 1024);
  287. u64 max_seen = base;
  288. int i;
  289. for (i = 0; i < num_mem_regs; i++) {
  290. const struct linux_prom64_registers *ent;
  291. u64 this_base;
  292. u64 this_end;
  293. ent = &mem_regs[i];
  294. this_base = ent->phys_addr;
  295. this_end = this_base + ent->reg_size;
  296. if (base < this_base || base >= this_end)
  297. continue;
  298. if (this_end > max)
  299. this_end = max;
  300. if (this_end > max_seen)
  301. max_seen = this_end;
  302. }
  303. return max_seen - base;
  304. }
  305. static void __devinit jbusmc_construct_one_dimm_group(struct jbusmc *p,
  306. unsigned long index,
  307. const struct linux_prom64_registers *mem_regs,
  308. int num_mem_regs)
  309. {
  310. struct jbusmc_dimm_group *dp = &p->dimm_groups[index];
  311. dp->controller = p;
  312. dp->index = index;
  313. dp->base_addr = (p->portid * (64UL * 1024 * 1024 * 1024));
  314. dp->base_addr += (index * (8UL * 1024 * 1024 * 1024));
  315. dp->size = jbusmc_dimm_group_size(dp->base_addr, mem_regs, num_mem_regs);
  316. }
  317. static void __devinit jbusmc_construct_dimm_groups(struct jbusmc *p,
  318. const struct linux_prom64_registers *mem_regs,
  319. int num_mem_regs)
  320. {
  321. if (p->mc_reg_1 & JB_MC_REG1_DIMM1_BANK0) {
  322. jbusmc_construct_one_dimm_group(p, 0, mem_regs, num_mem_regs);
  323. p->num_dimm_groups++;
  324. }
  325. if (p->mc_reg_1 & JB_MC_REG1_DIMM2_BANK2) {
  326. jbusmc_construct_one_dimm_group(p, 1, mem_regs, num_mem_regs);
  327. p->num_dimm_groups++;
  328. }
  329. }
  330. static int __devinit jbusmc_probe(struct of_device *op,
  331. const struct of_device_id *match)
  332. {
  333. const struct linux_prom64_registers *mem_regs;
  334. struct device_node *mem_node;
  335. int err, len, num_mem_regs;
  336. struct jbusmc *p;
  337. const u32 *prop;
  338. const void *ml;
  339. err = -ENODEV;
  340. mem_node = of_find_node_by_path("/memory");
  341. if (!mem_node) {
  342. printk(KERN_ERR PFX "Cannot find /memory node.\n");
  343. goto out;
  344. }
  345. mem_regs = of_get_property(mem_node, "reg", &len);
  346. if (!mem_regs) {
  347. printk(KERN_ERR PFX "Cannot get reg property of /memory node.\n");
  348. goto out;
  349. }
  350. num_mem_regs = len / sizeof(*mem_regs);
  351. err = -ENOMEM;
  352. p = kzalloc(sizeof(*p), GFP_KERNEL);
  353. if (!p) {
  354. printk(KERN_ERR PFX "Cannot allocate struct jbusmc.\n");
  355. goto out;
  356. }
  357. INIT_LIST_HEAD(&p->list);
  358. err = -ENODEV;
  359. prop = of_get_property(op->node, "portid", &len);
  360. if (!prop || len != 4) {
  361. printk(KERN_ERR PFX "Cannot find portid.\n");
  362. goto out_free;
  363. }
  364. p->portid = *prop;
  365. prop = of_get_property(op->node, "memory-control-register-1", &len);
  366. if (!prop || len != 8) {
  367. printk(KERN_ERR PFX "Cannot get memory control register 1.\n");
  368. goto out_free;
  369. }
  370. p->mc_reg_1 = ((u64)prop[0] << 32) | (u64) prop[1];
  371. err = -ENOMEM;
  372. p->regs = of_ioremap(&op->resource[0], 0, JBUSMC_REGS_SIZE, "jbusmc");
  373. if (!p->regs) {
  374. printk(KERN_ERR PFX "Cannot map jbusmc regs.\n");
  375. goto out_free;
  376. }
  377. err = -ENODEV;
  378. ml = of_get_property(op->node, "memory-layout", &p->layout_len);
  379. if (!ml) {
  380. printk(KERN_ERR PFX "Cannot get memory layout property.\n");
  381. goto out_iounmap;
  382. }
  383. if (p->layout_len > sizeof(p->layout)) {
  384. printk(KERN_ERR PFX "Unexpected memory-layout size %d\n",
  385. p->layout_len);
  386. goto out_iounmap;
  387. }
  388. memcpy(&p->layout, ml, p->layout_len);
  389. jbusmc_construct_dimm_groups(p, mem_regs, num_mem_regs);
  390. mc_list_add(&p->list);
  391. printk(KERN_INFO PFX "UltraSPARC-IIIi memory controller at %s\n",
  392. op->node->full_name);
  393. dev_set_drvdata(&op->dev, p);
  394. err = 0;
  395. out:
  396. return err;
  397. out_iounmap:
  398. of_iounmap(&op->resource[0], p->regs, JBUSMC_REGS_SIZE);
  399. out_free:
  400. kfree(p);
  401. goto out;
  402. }
  403. /* Does BANK decode PHYS_ADDR? */
  404. static int chmc_bank_match(struct chmc_bank_info *bp, unsigned long phys_addr)
  405. {
  406. unsigned long upper_bits = (phys_addr & PA_UPPER_BITS) >> PA_UPPER_BITS_SHIFT;
  407. unsigned long lower_bits = (phys_addr & PA_LOWER_BITS) >> PA_LOWER_BITS_SHIFT;
  408. /* Bank must be enabled to match. */
  409. if (bp->valid == 0)
  410. return 0;
  411. /* Would BANK match upper bits? */
  412. upper_bits ^= bp->um; /* What bits are different? */
  413. upper_bits = ~upper_bits; /* Invert. */
  414. upper_bits |= bp->uk; /* What bits don't matter for matching? */
  415. upper_bits = ~upper_bits; /* Invert. */
  416. if (upper_bits)
  417. return 0;
  418. /* Would BANK match lower bits? */
  419. lower_bits ^= bp->lm; /* What bits are different? */
  420. lower_bits = ~lower_bits; /* Invert. */
  421. lower_bits |= bp->lk; /* What bits don't matter for matching? */
  422. lower_bits = ~lower_bits; /* Invert. */
  423. if (lower_bits)
  424. return 0;
  425. /* I always knew you'd be the one. */
  426. return 1;
  427. }
  428. /* Given PHYS_ADDR, search memory controller banks for a match. */
  429. static struct chmc_bank_info *chmc_find_bank(unsigned long phys_addr)
  430. {
  431. struct chmc *p;
  432. list_for_each_entry(p, &mctrl_list, list) {
  433. int bank_no;
  434. for (bank_no = 0; bank_no < CHMCTRL_NBANKS; bank_no++) {
  435. struct chmc_bank_info *bp;
  436. bp = &p->logical_banks[bank_no];
  437. if (chmc_bank_match(bp, phys_addr))
  438. return bp;
  439. }
  440. }
  441. return NULL;
  442. }
  443. /* This is the main purpose of this driver. */
  444. static int chmc_print_dimm(int syndrome_code,
  445. unsigned long phys_addr,
  446. char *buf, int buflen)
  447. {
  448. struct chmc_bank_info *bp;
  449. struct chmc_obp_mem_layout *prop;
  450. int bank_in_controller, first_dimm;
  451. bp = chmc_find_bank(phys_addr);
  452. if (bp == NULL ||
  453. syndrome_code < SYNDROME_MIN ||
  454. syndrome_code > SYNDROME_MAX) {
  455. buf[0] = '?';
  456. buf[1] = '?';
  457. buf[2] = '?';
  458. buf[3] = '\0';
  459. return 0;
  460. }
  461. prop = &bp->p->layout_prop;
  462. bank_in_controller = bp->bank_id & (CHMCTRL_NBANKS - 1);
  463. first_dimm = (bank_in_controller & (CHMCTRL_NDGRPS - 1));
  464. first_dimm *= CHMCTRL_NDIMMS;
  465. if (syndrome_code != SYNDROME_MIN) {
  466. char *dimm_str;
  467. int pin;
  468. get_pin_and_dimm_str(syndrome_code, phys_addr, &pin,
  469. &dimm_str, prop, first_dimm);
  470. sprintf(buf, "%s, pin %3d", dimm_str, pin);
  471. } else {
  472. int dimm;
  473. /* Multi-bit error, we just dump out all the
  474. * dimm labels associated with this bank.
  475. */
  476. for (dimm = 0; dimm < CHMCTRL_NDIMMS; dimm++) {
  477. sprintf(buf, "%s ",
  478. prop->dimm_labels[first_dimm + dimm]);
  479. buf += strlen(buf);
  480. }
  481. }
  482. return 0;
  483. }
  484. /* Accessing the registers is slightly complicated. If you want
  485. * to get at the memory controller which is on the same processor
  486. * the code is executing, you must use special ASI load/store else
  487. * you go through the global mapping.
  488. */
  489. static u64 chmc_read_mcreg(struct chmc *p, unsigned long offset)
  490. {
  491. unsigned long ret, this_cpu;
  492. preempt_disable();
  493. this_cpu = real_hard_smp_processor_id();
  494. if (p->portid == this_cpu) {
  495. __asm__ __volatile__("ldxa [%1] %2, %0"
  496. : "=r" (ret)
  497. : "r" (offset), "i" (ASI_MCU_CTRL_REG));
  498. } else {
  499. __asm__ __volatile__("ldxa [%1] %2, %0"
  500. : "=r" (ret)
  501. : "r" (p->regs + offset),
  502. "i" (ASI_PHYS_BYPASS_EC_E));
  503. }
  504. preempt_enable();
  505. return ret;
  506. }
  507. #if 0 /* currently unused */
  508. static void chmc_write_mcreg(struct chmc *p, unsigned long offset, u64 val)
  509. {
  510. if (p->portid == smp_processor_id()) {
  511. __asm__ __volatile__("stxa %0, [%1] %2"
  512. : : "r" (val),
  513. "r" (offset), "i" (ASI_MCU_CTRL_REG));
  514. } else {
  515. __asm__ __volatile__("ldxa %0, [%1] %2"
  516. : : "r" (val),
  517. "r" (p->regs + offset),
  518. "i" (ASI_PHYS_BYPASS_EC_E));
  519. }
  520. }
  521. #endif
  522. static void chmc_interpret_one_decode_reg(struct chmc *p, int which_bank, u64 val)
  523. {
  524. struct chmc_bank_info *bp = &p->logical_banks[which_bank];
  525. bp->p = p;
  526. bp->bank_id = (CHMCTRL_NBANKS * p->portid) + which_bank;
  527. bp->raw_reg = val;
  528. bp->valid = (val & MEM_DECODE_VALID) >> MEM_DECODE_VALID_SHIFT;
  529. bp->uk = (val & MEM_DECODE_UK) >> MEM_DECODE_UK_SHIFT;
  530. bp->um = (val & MEM_DECODE_UM) >> MEM_DECODE_UM_SHIFT;
  531. bp->lk = (val & MEM_DECODE_LK) >> MEM_DECODE_LK_SHIFT;
  532. bp->lm = (val & MEM_DECODE_LM) >> MEM_DECODE_LM_SHIFT;
  533. bp->base = (bp->um);
  534. bp->base &= ~(bp->uk);
  535. bp->base <<= PA_UPPER_BITS_SHIFT;
  536. switch(bp->lk) {
  537. case 0xf:
  538. default:
  539. bp->interleave = 1;
  540. break;
  541. case 0xe:
  542. bp->interleave = 2;
  543. break;
  544. case 0xc:
  545. bp->interleave = 4;
  546. break;
  547. case 0x8:
  548. bp->interleave = 8;
  549. break;
  550. case 0x0:
  551. bp->interleave = 16;
  552. break;
  553. };
  554. /* UK[10] is reserved, and UK[11] is not set for the SDRAM
  555. * bank size definition.
  556. */
  557. bp->size = (((unsigned long)bp->uk &
  558. ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT;
  559. bp->size /= bp->interleave;
  560. }
  561. static void chmc_fetch_decode_regs(struct chmc *p)
  562. {
  563. if (p->layout_size == 0)
  564. return;
  565. chmc_interpret_one_decode_reg(p, 0,
  566. chmc_read_mcreg(p, CHMCTRL_DECODE1));
  567. chmc_interpret_one_decode_reg(p, 1,
  568. chmc_read_mcreg(p, CHMCTRL_DECODE2));
  569. chmc_interpret_one_decode_reg(p, 2,
  570. chmc_read_mcreg(p, CHMCTRL_DECODE3));
  571. chmc_interpret_one_decode_reg(p, 3,
  572. chmc_read_mcreg(p, CHMCTRL_DECODE4));
  573. }
  574. static int __devinit chmc_probe(struct of_device *op,
  575. const struct of_device_id *match)
  576. {
  577. struct device_node *dp = op->node;
  578. unsigned long ver;
  579. const void *pval;
  580. int len, portid;
  581. struct chmc *p;
  582. int err;
  583. err = -ENODEV;
  584. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  585. if ((ver >> 32UL) == __JALAPENO_ID ||
  586. (ver >> 32UL) == __SERRANO_ID)
  587. goto out;
  588. portid = of_getintprop_default(dp, "portid", -1);
  589. if (portid == -1)
  590. goto out;
  591. pval = of_get_property(dp, "memory-layout", &len);
  592. if (pval && len > sizeof(p->layout_prop)) {
  593. printk(KERN_ERR PFX "Unexpected memory-layout property "
  594. "size %d.\n", len);
  595. goto out;
  596. }
  597. err = -ENOMEM;
  598. p = kzalloc(sizeof(*p), GFP_KERNEL);
  599. if (!p) {
  600. printk(KERN_ERR PFX "Could not allocate struct chmc.\n");
  601. goto out;
  602. }
  603. p->portid = portid;
  604. p->layout_size = len;
  605. if (!pval)
  606. p->layout_size = 0;
  607. else
  608. memcpy(&p->layout_prop, pval, len);
  609. p->regs = of_ioremap(&op->resource[0], 0, 0x48, "chmc");
  610. if (!p->regs) {
  611. printk(KERN_ERR PFX "Could not map registers.\n");
  612. goto out_free;
  613. }
  614. if (p->layout_size != 0UL) {
  615. p->timing_control1 = chmc_read_mcreg(p, CHMCTRL_TCTRL1);
  616. p->timing_control2 = chmc_read_mcreg(p, CHMCTRL_TCTRL2);
  617. p->timing_control3 = chmc_read_mcreg(p, CHMCTRL_TCTRL3);
  618. p->timing_control4 = chmc_read_mcreg(p, CHMCTRL_TCTRL4);
  619. p->memaddr_control = chmc_read_mcreg(p, CHMCTRL_MACTRL);
  620. }
  621. chmc_fetch_decode_regs(p);
  622. mc_list_add(&p->list);
  623. printk(KERN_INFO PFX "UltraSPARC-III memory controller at %s [%s]\n",
  624. dp->full_name,
  625. (p->layout_size ? "ACTIVE" : "INACTIVE"));
  626. dev_set_drvdata(&op->dev, p);
  627. err = 0;
  628. out:
  629. return err;
  630. out_free:
  631. kfree(p);
  632. goto out;
  633. }
  634. static int __devinit us3mc_probe(struct of_device *op,
  635. const struct of_device_id *match)
  636. {
  637. if (mc_type == MC_TYPE_SAFARI)
  638. return chmc_probe(op, match);
  639. else if (mc_type == MC_TYPE_JBUS)
  640. return jbusmc_probe(op, match);
  641. return -ENODEV;
  642. }
  643. static void __devexit chmc_destroy(struct of_device *op, struct chmc *p)
  644. {
  645. list_del(&p->list);
  646. of_iounmap(&op->resource[0], p->regs, 0x48);
  647. kfree(p);
  648. }
  649. static void __devexit jbusmc_destroy(struct of_device *op, struct jbusmc *p)
  650. {
  651. mc_list_del(&p->list);
  652. of_iounmap(&op->resource[0], p->regs, JBUSMC_REGS_SIZE);
  653. kfree(p);
  654. }
  655. static int __devexit us3mc_remove(struct of_device *op)
  656. {
  657. void *p = dev_get_drvdata(&op->dev);
  658. if (p) {
  659. if (mc_type == MC_TYPE_SAFARI)
  660. chmc_destroy(op, p);
  661. else if (mc_type == MC_TYPE_JBUS)
  662. jbusmc_destroy(op, p);
  663. }
  664. return 0;
  665. }
  666. static const struct of_device_id us3mc_match[] = {
  667. {
  668. .name = "memory-controller",
  669. },
  670. {},
  671. };
  672. MODULE_DEVICE_TABLE(of, us3mc_match);
  673. static struct of_platform_driver us3mc_driver = {
  674. .name = "us3mc",
  675. .match_table = us3mc_match,
  676. .probe = us3mc_probe,
  677. .remove = __devexit_p(us3mc_remove),
  678. };
  679. static inline bool us3mc_platform(void)
  680. {
  681. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  682. return true;
  683. return false;
  684. }
  685. static int __init us3mc_init(void)
  686. {
  687. unsigned long ver;
  688. int ret;
  689. if (!us3mc_platform())
  690. return -ENODEV;
  691. __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
  692. if ((ver >> 32UL) == __JALAPENO_ID ||
  693. (ver >> 32UL) == __SERRANO_ID) {
  694. mc_type = MC_TYPE_JBUS;
  695. us3mc_dimm_printer = jbusmc_print_dimm;
  696. } else {
  697. mc_type = MC_TYPE_SAFARI;
  698. us3mc_dimm_printer = chmc_print_dimm;
  699. }
  700. ret = register_dimm_printer(us3mc_dimm_printer);
  701. if (!ret) {
  702. ret = of_register_driver(&us3mc_driver, &of_bus_type);
  703. if (ret)
  704. unregister_dimm_printer(us3mc_dimm_printer);
  705. }
  706. return ret;
  707. }
  708. static void __exit us3mc_cleanup(void)
  709. {
  710. if (us3mc_platform()) {
  711. unregister_dimm_printer(us3mc_dimm_printer);
  712. of_unregister_driver(&us3mc_driver);
  713. }
  714. }
  715. module_init(us3mc_init);
  716. module_exit(us3mc_cleanup);