chmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 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 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 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 jbusmc_probe(struct platform_device *op)
  331. {
  332. const struct linux_prom64_registers *mem_regs;
  333. struct device_node *mem_node;
  334. int err, len, num_mem_regs;
  335. struct jbusmc *p;
  336. const u32 *prop;
  337. const void *ml;
  338. err = -ENODEV;
  339. mem_node = of_find_node_by_path("/memory");
  340. if (!mem_node) {
  341. printk(KERN_ERR PFX "Cannot find /memory node.\n");
  342. goto out;
  343. }
  344. mem_regs = of_get_property(mem_node, "reg", &len);
  345. if (!mem_regs) {
  346. printk(KERN_ERR PFX "Cannot get reg property of /memory node.\n");
  347. goto out;
  348. }
  349. num_mem_regs = len / sizeof(*mem_regs);
  350. err = -ENOMEM;
  351. p = kzalloc(sizeof(*p), GFP_KERNEL);
  352. if (!p) {
  353. printk(KERN_ERR PFX "Cannot allocate struct jbusmc.\n");
  354. goto out;
  355. }
  356. INIT_LIST_HEAD(&p->list);
  357. err = -ENODEV;
  358. prop = of_get_property(op->dev.of_node, "portid", &len);
  359. if (!prop || len != 4) {
  360. printk(KERN_ERR PFX "Cannot find portid.\n");
  361. goto out_free;
  362. }
  363. p->portid = *prop;
  364. prop = of_get_property(op->dev.of_node, "memory-control-register-1", &len);
  365. if (!prop || len != 8) {
  366. printk(KERN_ERR PFX "Cannot get memory control register 1.\n");
  367. goto out_free;
  368. }
  369. p->mc_reg_1 = ((u64)prop[0] << 32) | (u64) prop[1];
  370. err = -ENOMEM;
  371. p->regs = of_ioremap(&op->resource[0], 0, JBUSMC_REGS_SIZE, "jbusmc");
  372. if (!p->regs) {
  373. printk(KERN_ERR PFX "Cannot map jbusmc regs.\n");
  374. goto out_free;
  375. }
  376. err = -ENODEV;
  377. ml = of_get_property(op->dev.of_node, "memory-layout", &p->layout_len);
  378. if (!ml) {
  379. printk(KERN_ERR PFX "Cannot get memory layout property.\n");
  380. goto out_iounmap;
  381. }
  382. if (p->layout_len > sizeof(p->layout)) {
  383. printk(KERN_ERR PFX "Unexpected memory-layout size %d\n",
  384. p->layout_len);
  385. goto out_iounmap;
  386. }
  387. memcpy(&p->layout, ml, p->layout_len);
  388. jbusmc_construct_dimm_groups(p, mem_regs, num_mem_regs);
  389. mc_list_add(&p->list);
  390. printk(KERN_INFO PFX "UltraSPARC-IIIi memory controller at %s\n",
  391. op->dev.of_node->full_name);
  392. dev_set_drvdata(&op->dev, p);
  393. err = 0;
  394. out:
  395. return err;
  396. out_iounmap:
  397. of_iounmap(&op->resource[0], p->regs, JBUSMC_REGS_SIZE);
  398. out_free:
  399. kfree(p);
  400. goto out;
  401. }
  402. /* Does BANK decode PHYS_ADDR? */
  403. static int chmc_bank_match(struct chmc_bank_info *bp, unsigned long phys_addr)
  404. {
  405. unsigned long upper_bits = (phys_addr & PA_UPPER_BITS) >> PA_UPPER_BITS_SHIFT;
  406. unsigned long lower_bits = (phys_addr & PA_LOWER_BITS) >> PA_LOWER_BITS_SHIFT;
  407. /* Bank must be enabled to match. */
  408. if (bp->valid == 0)
  409. return 0;
  410. /* Would BANK match upper bits? */
  411. upper_bits ^= bp->um; /* What bits are different? */
  412. upper_bits = ~upper_bits; /* Invert. */
  413. upper_bits |= bp->uk; /* What bits don't matter for matching? */
  414. upper_bits = ~upper_bits; /* Invert. */
  415. if (upper_bits)
  416. return 0;
  417. /* Would BANK match lower bits? */
  418. lower_bits ^= bp->lm; /* What bits are different? */
  419. lower_bits = ~lower_bits; /* Invert. */
  420. lower_bits |= bp->lk; /* What bits don't matter for matching? */
  421. lower_bits = ~lower_bits; /* Invert. */
  422. if (lower_bits)
  423. return 0;
  424. /* I always knew you'd be the one. */
  425. return 1;
  426. }
  427. /* Given PHYS_ADDR, search memory controller banks for a match. */
  428. static struct chmc_bank_info *chmc_find_bank(unsigned long phys_addr)
  429. {
  430. struct chmc *p;
  431. list_for_each_entry(p, &mctrl_list, list) {
  432. int bank_no;
  433. for (bank_no = 0; bank_no < CHMCTRL_NBANKS; bank_no++) {
  434. struct chmc_bank_info *bp;
  435. bp = &p->logical_banks[bank_no];
  436. if (chmc_bank_match(bp, phys_addr))
  437. return bp;
  438. }
  439. }
  440. return NULL;
  441. }
  442. /* This is the main purpose of this driver. */
  443. static int chmc_print_dimm(int syndrome_code,
  444. unsigned long phys_addr,
  445. char *buf, int buflen)
  446. {
  447. struct chmc_bank_info *bp;
  448. struct chmc_obp_mem_layout *prop;
  449. int bank_in_controller, first_dimm;
  450. bp = chmc_find_bank(phys_addr);
  451. if (bp == NULL ||
  452. syndrome_code < SYNDROME_MIN ||
  453. syndrome_code > SYNDROME_MAX) {
  454. buf[0] = '?';
  455. buf[1] = '?';
  456. buf[2] = '?';
  457. buf[3] = '\0';
  458. return 0;
  459. }
  460. prop = &bp->p->layout_prop;
  461. bank_in_controller = bp->bank_id & (CHMCTRL_NBANKS - 1);
  462. first_dimm = (bank_in_controller & (CHMCTRL_NDGRPS - 1));
  463. first_dimm *= CHMCTRL_NDIMMS;
  464. if (syndrome_code != SYNDROME_MIN) {
  465. char *dimm_str;
  466. int pin;
  467. get_pin_and_dimm_str(syndrome_code, phys_addr, &pin,
  468. &dimm_str, prop, first_dimm);
  469. sprintf(buf, "%s, pin %3d", dimm_str, pin);
  470. } else {
  471. int dimm;
  472. /* Multi-bit error, we just dump out all the
  473. * dimm labels associated with this bank.
  474. */
  475. for (dimm = 0; dimm < CHMCTRL_NDIMMS; dimm++) {
  476. sprintf(buf, "%s ",
  477. prop->dimm_labels[first_dimm + dimm]);
  478. buf += strlen(buf);
  479. }
  480. }
  481. return 0;
  482. }
  483. /* Accessing the registers is slightly complicated. If you want
  484. * to get at the memory controller which is on the same processor
  485. * the code is executing, you must use special ASI load/store else
  486. * you go through the global mapping.
  487. */
  488. static u64 chmc_read_mcreg(struct chmc *p, unsigned long offset)
  489. {
  490. unsigned long ret, this_cpu;
  491. preempt_disable();
  492. this_cpu = real_hard_smp_processor_id();
  493. if (p->portid == this_cpu) {
  494. __asm__ __volatile__("ldxa [%1] %2, %0"
  495. : "=r" (ret)
  496. : "r" (offset), "i" (ASI_MCU_CTRL_REG));
  497. } else {
  498. __asm__ __volatile__("ldxa [%1] %2, %0"
  499. : "=r" (ret)
  500. : "r" (p->regs + offset),
  501. "i" (ASI_PHYS_BYPASS_EC_E));
  502. }
  503. preempt_enable();
  504. return ret;
  505. }
  506. #if 0 /* currently unused */
  507. static void chmc_write_mcreg(struct chmc *p, unsigned long offset, u64 val)
  508. {
  509. if (p->portid == smp_processor_id()) {
  510. __asm__ __volatile__("stxa %0, [%1] %2"
  511. : : "r" (val),
  512. "r" (offset), "i" (ASI_MCU_CTRL_REG));
  513. } else {
  514. __asm__ __volatile__("ldxa %0, [%1] %2"
  515. : : "r" (val),
  516. "r" (p->regs + offset),
  517. "i" (ASI_PHYS_BYPASS_EC_E));
  518. }
  519. }
  520. #endif
  521. static void chmc_interpret_one_decode_reg(struct chmc *p, int which_bank, u64 val)
  522. {
  523. struct chmc_bank_info *bp = &p->logical_banks[which_bank];
  524. bp->p = p;
  525. bp->bank_id = (CHMCTRL_NBANKS * p->portid) + which_bank;
  526. bp->raw_reg = val;
  527. bp->valid = (val & MEM_DECODE_VALID) >> MEM_DECODE_VALID_SHIFT;
  528. bp->uk = (val & MEM_DECODE_UK) >> MEM_DECODE_UK_SHIFT;
  529. bp->um = (val & MEM_DECODE_UM) >> MEM_DECODE_UM_SHIFT;
  530. bp->lk = (val & MEM_DECODE_LK) >> MEM_DECODE_LK_SHIFT;
  531. bp->lm = (val & MEM_DECODE_LM) >> MEM_DECODE_LM_SHIFT;
  532. bp->base = (bp->um);
  533. bp->base &= ~(bp->uk);
  534. bp->base <<= PA_UPPER_BITS_SHIFT;
  535. switch(bp->lk) {
  536. case 0xf:
  537. default:
  538. bp->interleave = 1;
  539. break;
  540. case 0xe:
  541. bp->interleave = 2;
  542. break;
  543. case 0xc:
  544. bp->interleave = 4;
  545. break;
  546. case 0x8:
  547. bp->interleave = 8;
  548. break;
  549. case 0x0:
  550. bp->interleave = 16;
  551. break;
  552. }
  553. /* UK[10] is reserved, and UK[11] is not set for the SDRAM
  554. * bank size definition.
  555. */
  556. bp->size = (((unsigned long)bp->uk &
  557. ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT;
  558. bp->size /= bp->interleave;
  559. }
  560. static void chmc_fetch_decode_regs(struct chmc *p)
  561. {
  562. if (p->layout_size == 0)
  563. return;
  564. chmc_interpret_one_decode_reg(p, 0,
  565. chmc_read_mcreg(p, CHMCTRL_DECODE1));
  566. chmc_interpret_one_decode_reg(p, 1,
  567. chmc_read_mcreg(p, CHMCTRL_DECODE2));
  568. chmc_interpret_one_decode_reg(p, 2,
  569. chmc_read_mcreg(p, CHMCTRL_DECODE3));
  570. chmc_interpret_one_decode_reg(p, 3,
  571. chmc_read_mcreg(p, CHMCTRL_DECODE4));
  572. }
  573. static int chmc_probe(struct platform_device *op)
  574. {
  575. struct device_node *dp = op->dev.of_node;
  576. unsigned long ver;
  577. const void *pval;
  578. int len, portid;
  579. struct chmc *p;
  580. int err;
  581. err = -ENODEV;
  582. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  583. if ((ver >> 32UL) == __JALAPENO_ID ||
  584. (ver >> 32UL) == __SERRANO_ID)
  585. goto out;
  586. portid = of_getintprop_default(dp, "portid", -1);
  587. if (portid == -1)
  588. goto out;
  589. pval = of_get_property(dp, "memory-layout", &len);
  590. if (pval && len > sizeof(p->layout_prop)) {
  591. printk(KERN_ERR PFX "Unexpected memory-layout property "
  592. "size %d.\n", len);
  593. goto out;
  594. }
  595. err = -ENOMEM;
  596. p = kzalloc(sizeof(*p), GFP_KERNEL);
  597. if (!p) {
  598. printk(KERN_ERR PFX "Could not allocate struct chmc.\n");
  599. goto out;
  600. }
  601. p->portid = portid;
  602. p->layout_size = len;
  603. if (!pval)
  604. p->layout_size = 0;
  605. else
  606. memcpy(&p->layout_prop, pval, len);
  607. p->regs = of_ioremap(&op->resource[0], 0, 0x48, "chmc");
  608. if (!p->regs) {
  609. printk(KERN_ERR PFX "Could not map registers.\n");
  610. goto out_free;
  611. }
  612. if (p->layout_size != 0UL) {
  613. p->timing_control1 = chmc_read_mcreg(p, CHMCTRL_TCTRL1);
  614. p->timing_control2 = chmc_read_mcreg(p, CHMCTRL_TCTRL2);
  615. p->timing_control3 = chmc_read_mcreg(p, CHMCTRL_TCTRL3);
  616. p->timing_control4 = chmc_read_mcreg(p, CHMCTRL_TCTRL4);
  617. p->memaddr_control = chmc_read_mcreg(p, CHMCTRL_MACTRL);
  618. }
  619. chmc_fetch_decode_regs(p);
  620. mc_list_add(&p->list);
  621. printk(KERN_INFO PFX "UltraSPARC-III memory controller at %s [%s]\n",
  622. dp->full_name,
  623. (p->layout_size ? "ACTIVE" : "INACTIVE"));
  624. dev_set_drvdata(&op->dev, p);
  625. err = 0;
  626. out:
  627. return err;
  628. out_free:
  629. kfree(p);
  630. goto out;
  631. }
  632. static int us3mc_probe(struct platform_device *op)
  633. {
  634. if (mc_type == MC_TYPE_SAFARI)
  635. return chmc_probe(op);
  636. else if (mc_type == MC_TYPE_JBUS)
  637. return jbusmc_probe(op);
  638. return -ENODEV;
  639. }
  640. static void chmc_destroy(struct platform_device *op, struct chmc *p)
  641. {
  642. list_del(&p->list);
  643. of_iounmap(&op->resource[0], p->regs, 0x48);
  644. kfree(p);
  645. }
  646. static void jbusmc_destroy(struct platform_device *op, struct jbusmc *p)
  647. {
  648. mc_list_del(&p->list);
  649. of_iounmap(&op->resource[0], p->regs, JBUSMC_REGS_SIZE);
  650. kfree(p);
  651. }
  652. static int us3mc_remove(struct platform_device *op)
  653. {
  654. void *p = dev_get_drvdata(&op->dev);
  655. if (p) {
  656. if (mc_type == MC_TYPE_SAFARI)
  657. chmc_destroy(op, p);
  658. else if (mc_type == MC_TYPE_JBUS)
  659. jbusmc_destroy(op, p);
  660. }
  661. return 0;
  662. }
  663. static const struct of_device_id us3mc_match[] = {
  664. {
  665. .name = "memory-controller",
  666. },
  667. {},
  668. };
  669. MODULE_DEVICE_TABLE(of, us3mc_match);
  670. static struct platform_driver us3mc_driver = {
  671. .driver = {
  672. .name = "us3mc",
  673. .of_match_table = us3mc_match,
  674. },
  675. .probe = us3mc_probe,
  676. .remove = us3mc_remove,
  677. };
  678. static inline bool us3mc_platform(void)
  679. {
  680. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  681. return true;
  682. return false;
  683. }
  684. static int __init us3mc_init(void)
  685. {
  686. unsigned long ver;
  687. int ret;
  688. if (!us3mc_platform())
  689. return -ENODEV;
  690. __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
  691. if ((ver >> 32UL) == __JALAPENO_ID ||
  692. (ver >> 32UL) == __SERRANO_ID) {
  693. mc_type = MC_TYPE_JBUS;
  694. us3mc_dimm_printer = jbusmc_print_dimm;
  695. } else {
  696. mc_type = MC_TYPE_SAFARI;
  697. us3mc_dimm_printer = chmc_print_dimm;
  698. }
  699. ret = register_dimm_printer(us3mc_dimm_printer);
  700. if (!ret) {
  701. ret = platform_driver_register(&us3mc_driver);
  702. if (ret)
  703. unregister_dimm_printer(us3mc_dimm_printer);
  704. }
  705. return ret;
  706. }
  707. static void __exit us3mc_cleanup(void)
  708. {
  709. if (us3mc_platform()) {
  710. unregister_dimm_printer(us3mc_dimm_printer);
  711. platform_driver_unregister(&us3mc_driver);
  712. }
  713. }
  714. module_init(us3mc_init);
  715. module_exit(us3mc_cleanup);