sparc64: Add generic interface for registering a dimm printing handler.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / sparc64 / kernel / chmc.c
1 /* chmc.c: Driver for UltraSPARC-III memory controller.
2 *
3 * Copyright (C) 2001, 2007, 2008 David S. Miller (davem@davemloft.net)
4 */
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 <linux/of.h>
17 #include <linux/of_device.h>
18 #include <asm/spitfire.h>
19 #include <asm/chmctrl.h>
20 #include <asm/cpudata.h>
21 #include <asm/oplib.h>
22 #include <asm/prom.h>
23 #include <asm/head.h>
24 #include <asm/io.h>
25 #include <asm/memctrl.h>
26
27 #define DRV_MODULE_NAME "chmc"
28 #define PFX DRV_MODULE_NAME ": "
29 #define DRV_MODULE_VERSION "0.2"
30
31 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32 MODULE_DESCRIPTION("UltraSPARC-III memory controller driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(DRV_MODULE_VERSION);
35
36 #define CHMCTRL_NDGRPS 2
37 #define CHMCTRL_NDIMMS 4
38
39 #define CHMC_DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
40
41 /* OBP memory-layout property format. */
42 struct chmc_obp_map {
43 unsigned char dimm_map[144];
44 unsigned char pin_map[576];
45 };
46
47 #define DIMM_LABEL_SZ 8
48
49 struct chmc_obp_mem_layout {
50 /* One max 8-byte string label per DIMM. Usually
51 * this matches the label on the motherboard where
52 * that DIMM resides.
53 */
54 char dimm_labels[CHMC_DIMMS_PER_MC][DIMM_LABEL_SZ];
55
56 /* If symmetric use map[0], else it is
57 * asymmetric and map[1] should be used.
58 */
59 char symmetric;
60
61 struct chmc_obp_map map[2];
62 };
63
64 #define CHMCTRL_NBANKS 4
65
66 struct chmc_bank_info {
67 struct chmc *p;
68 int bank_id;
69
70 u64 raw_reg;
71 int valid;
72 int uk;
73 int um;
74 int lk;
75 int lm;
76 int interleave;
77 unsigned long base;
78 unsigned long size;
79 };
80
81 struct chmc {
82 struct list_head list;
83 int portid;
84
85 struct chmc_obp_mem_layout layout_prop;
86 int layout_size;
87
88 void __iomem *regs;
89
90 u64 timing_control1;
91 u64 timing_control2;
92 u64 timing_control3;
93 u64 timing_control4;
94 u64 memaddr_control;
95
96 struct chmc_bank_info logical_banks[CHMCTRL_NBANKS];
97 };
98
99 static LIST_HEAD(mctrl_list);
100
101 /* Does BANK decode PHYS_ADDR? */
102 static int chmc_bank_match(struct chmc_bank_info *bp, unsigned long phys_addr)
103 {
104 unsigned long upper_bits = (phys_addr & PA_UPPER_BITS) >> PA_UPPER_BITS_SHIFT;
105 unsigned long lower_bits = (phys_addr & PA_LOWER_BITS) >> PA_LOWER_BITS_SHIFT;
106
107 /* Bank must be enabled to match. */
108 if (bp->valid == 0)
109 return 0;
110
111 /* Would BANK match upper bits? */
112 upper_bits ^= bp->um; /* What bits are different? */
113 upper_bits = ~upper_bits; /* Invert. */
114 upper_bits |= bp->uk; /* What bits don't matter for matching? */
115 upper_bits = ~upper_bits; /* Invert. */
116
117 if (upper_bits)
118 return 0;
119
120 /* Would BANK match lower bits? */
121 lower_bits ^= bp->lm; /* What bits are different? */
122 lower_bits = ~lower_bits; /* Invert. */
123 lower_bits |= bp->lk; /* What bits don't matter for matching? */
124 lower_bits = ~lower_bits; /* Invert. */
125
126 if (lower_bits)
127 return 0;
128
129 /* I always knew you'd be the one. */
130 return 1;
131 }
132
133 /* Given PHYS_ADDR, search memory controller banks for a match. */
134 static struct chmc_bank_info *chmc_find_bank(unsigned long phys_addr)
135 {
136 struct list_head *mctrl_head = &mctrl_list;
137 struct list_head *mctrl_entry = mctrl_head->next;
138
139 for (;;) {
140 struct chmc *p = list_entry(mctrl_entry, struct chmc, list);
141 int bank_no;
142
143 if (mctrl_entry == mctrl_head)
144 break;
145 mctrl_entry = mctrl_entry->next;
146
147 for (bank_no = 0; bank_no < CHMCTRL_NBANKS; bank_no++) {
148 struct chmc_bank_info *bp;
149
150 bp = &p->logical_banks[bank_no];
151 if (chmc_bank_match(bp, phys_addr))
152 return bp;
153 }
154 }
155
156 return NULL;
157 }
158
159 /* This is the main purpose of this driver. */
160 #define SYNDROME_MIN -1
161 #define SYNDROME_MAX 144
162 static int chmc_print_dimm(int syndrome_code,
163 unsigned long phys_addr,
164 char *buf, int buflen)
165 {
166 struct chmc_bank_info *bp;
167 struct chmc_obp_mem_layout *prop;
168 int bank_in_controller, first_dimm;
169
170 bp = chmc_find_bank(phys_addr);
171 if (bp == NULL ||
172 syndrome_code < SYNDROME_MIN ||
173 syndrome_code > SYNDROME_MAX) {
174 buf[0] = '?';
175 buf[1] = '?';
176 buf[2] = '?';
177 buf[3] = '\0';
178 return 0;
179 }
180
181 prop = &bp->p->layout_prop;
182 bank_in_controller = bp->bank_id & (CHMCTRL_NBANKS - 1);
183 first_dimm = (bank_in_controller & (CHMCTRL_NDGRPS - 1));
184 first_dimm *= CHMCTRL_NDIMMS;
185
186 if (syndrome_code != SYNDROME_MIN) {
187 struct chmc_obp_map *map;
188 int qword, where_in_line, where, map_index, map_offset;
189 unsigned int map_val;
190
191 /* Yaay, single bit error so we can figure out
192 * the exact dimm.
193 */
194 if (prop->symmetric)
195 map = &prop->map[0];
196 else
197 map = &prop->map[1];
198
199 /* Covert syndrome code into the way the bits are
200 * positioned on the bus.
201 */
202 if (syndrome_code < 144 - 16)
203 syndrome_code += 16;
204 else if (syndrome_code < 144)
205 syndrome_code -= (144 - 7);
206 else if (syndrome_code < (144 + 3))
207 syndrome_code -= (144 + 3 - 4);
208 else
209 syndrome_code -= 144 + 3;
210
211 /* All this magic has to do with how a cache line
212 * comes over the wire on Safari. A 64-bit line
213 * comes over in 4 quadword cycles, each of which
214 * transmit ECC/MTAG info as well as the actual
215 * data. 144 bits per quadword, 576 total.
216 */
217 #define LINE_SIZE 64
218 #define LINE_ADDR_MSK (LINE_SIZE - 1)
219 #define QW_PER_LINE 4
220 #define QW_BYTES (LINE_SIZE / QW_PER_LINE)
221 #define QW_BITS 144
222 #define LAST_BIT (576 - 1)
223
224 qword = (phys_addr & LINE_ADDR_MSK) / QW_BYTES;
225 where_in_line = ((3 - qword) * QW_BITS) + syndrome_code;
226 where = (LAST_BIT - where_in_line);
227 map_index = where >> 2;
228 map_offset = where & 0x3;
229 map_val = map->dimm_map[map_index];
230 map_val = ((map_val >> ((3 - map_offset) << 1)) & (2 - 1));
231
232 sprintf(buf, "%s, pin %3d",
233 prop->dimm_labels[first_dimm + map_val],
234 map->pin_map[where_in_line]);
235 } else {
236 int dimm;
237
238 /* Multi-bit error, we just dump out all the
239 * dimm labels associated with this bank.
240 */
241 for (dimm = 0; dimm < CHMCTRL_NDIMMS; dimm++) {
242 sprintf(buf, "%s ",
243 prop->dimm_labels[first_dimm + dimm]);
244 buf += strlen(buf);
245 }
246 }
247 return 0;
248 }
249
250 /* Accessing the registers is slightly complicated. If you want
251 * to get at the memory controller which is on the same processor
252 * the code is executing, you must use special ASI load/store else
253 * you go through the global mapping.
254 */
255 static u64 chmc_read_mcreg(struct chmc *p, unsigned long offset)
256 {
257 unsigned long ret, this_cpu;
258
259 preempt_disable();
260
261 this_cpu = real_hard_smp_processor_id();
262
263 if (p->portid == this_cpu) {
264 __asm__ __volatile__("ldxa [%1] %2, %0"
265 : "=r" (ret)
266 : "r" (offset), "i" (ASI_MCU_CTRL_REG));
267 } else {
268 __asm__ __volatile__("ldxa [%1] %2, %0"
269 : "=r" (ret)
270 : "r" (p->regs + offset),
271 "i" (ASI_PHYS_BYPASS_EC_E));
272 }
273
274 preempt_enable();
275
276 return ret;
277 }
278
279 #if 0 /* currently unused */
280 static void chmc_write_mcreg(struct chmc *p, unsigned long offset, u64 val)
281 {
282 if (p->portid == smp_processor_id()) {
283 __asm__ __volatile__("stxa %0, [%1] %2"
284 : : "r" (val),
285 "r" (offset), "i" (ASI_MCU_CTRL_REG));
286 } else {
287 __asm__ __volatile__("ldxa %0, [%1] %2"
288 : : "r" (val),
289 "r" (p->regs + offset),
290 "i" (ASI_PHYS_BYPASS_EC_E));
291 }
292 }
293 #endif
294
295 static void chmc_interpret_one_decode_reg(struct chmc *p, int which_bank, u64 val)
296 {
297 struct chmc_bank_info *bp = &p->logical_banks[which_bank];
298
299 bp->p = p;
300 bp->bank_id = (CHMCTRL_NBANKS * p->portid) + which_bank;
301 bp->raw_reg = val;
302 bp->valid = (val & MEM_DECODE_VALID) >> MEM_DECODE_VALID_SHIFT;
303 bp->uk = (val & MEM_DECODE_UK) >> MEM_DECODE_UK_SHIFT;
304 bp->um = (val & MEM_DECODE_UM) >> MEM_DECODE_UM_SHIFT;
305 bp->lk = (val & MEM_DECODE_LK) >> MEM_DECODE_LK_SHIFT;
306 bp->lm = (val & MEM_DECODE_LM) >> MEM_DECODE_LM_SHIFT;
307
308 bp->base = (bp->um);
309 bp->base &= ~(bp->uk);
310 bp->base <<= PA_UPPER_BITS_SHIFT;
311
312 switch(bp->lk) {
313 case 0xf:
314 default:
315 bp->interleave = 1;
316 break;
317
318 case 0xe:
319 bp->interleave = 2;
320 break;
321
322 case 0xc:
323 bp->interleave = 4;
324 break;
325
326 case 0x8:
327 bp->interleave = 8;
328 break;
329
330 case 0x0:
331 bp->interleave = 16;
332 break;
333 };
334
335 /* UK[10] is reserved, and UK[11] is not set for the SDRAM
336 * bank size definition.
337 */
338 bp->size = (((unsigned long)bp->uk &
339 ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT;
340 bp->size /= bp->interleave;
341 }
342
343 static void chmc_fetch_decode_regs(struct chmc *p)
344 {
345 if (p->layout_size == 0)
346 return;
347
348 chmc_interpret_one_decode_reg(p, 0,
349 chmc_read_mcreg(p, CHMCTRL_DECODE1));
350 chmc_interpret_one_decode_reg(p, 1,
351 chmc_read_mcreg(p, CHMCTRL_DECODE2));
352 chmc_interpret_one_decode_reg(p, 2,
353 chmc_read_mcreg(p, CHMCTRL_DECODE3));
354 chmc_interpret_one_decode_reg(p, 3,
355 chmc_read_mcreg(p, CHMCTRL_DECODE4));
356 }
357
358 static int __devinit chmc_probe(struct of_device *op,
359 const struct of_device_id *match)
360 {
361 struct device_node *dp = op->node;
362 unsigned long ver;
363 const void *pval;
364 int len, portid;
365 struct chmc *p;
366 int err;
367
368 err = -ENODEV;
369 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
370 if ((ver >> 32UL) == __JALAPENO_ID ||
371 (ver >> 32UL) == __SERRANO_ID)
372 goto out;
373
374 portid = of_getintprop_default(dp, "portid", -1);
375 if (portid == -1)
376 goto out;
377
378 pval = of_get_property(dp, "memory-layout", &len);
379 if (pval && len > sizeof(p->layout_prop)) {
380 printk(KERN_ERR PFX "Unexpected memory-layout property "
381 "size %d.\n", len);
382 goto out;
383 }
384
385 err = -ENOMEM;
386 p = kzalloc(sizeof(*p), GFP_KERNEL);
387 if (!p) {
388 printk(KERN_ERR PFX "Could not allocate struct chmc.\n");
389 goto out;
390 }
391
392 p->portid = portid;
393 p->layout_size = len;
394 if (!pval)
395 p->layout_size = 0;
396 else
397 memcpy(&p->layout_prop, pval, len);
398
399 p->regs = of_ioremap(&op->resource[0], 0, 0x48, "chmc");
400 if (!p->regs) {
401 printk(KERN_ERR PFX "Could not map registers.\n");
402 goto out_free;
403 }
404
405 if (p->layout_size != 0UL) {
406 p->timing_control1 = chmc_read_mcreg(p, CHMCTRL_TCTRL1);
407 p->timing_control2 = chmc_read_mcreg(p, CHMCTRL_TCTRL2);
408 p->timing_control3 = chmc_read_mcreg(p, CHMCTRL_TCTRL3);
409 p->timing_control4 = chmc_read_mcreg(p, CHMCTRL_TCTRL4);
410 p->memaddr_control = chmc_read_mcreg(p, CHMCTRL_MACTRL);
411 }
412
413 chmc_fetch_decode_regs(p);
414
415 list_add(&p->list, &mctrl_list);
416
417 /* Report the device. */
418 printk(KERN_INFO PFX "UltraSPARC-III memory controller at %s [%s]\n",
419 dp->full_name,
420 (p->layout_size ? "ACTIVE" : "INACTIVE"));
421
422 dev_set_drvdata(&op->dev, p);
423
424 err = 0;
425
426 out:
427 return err;
428
429 out_free:
430 kfree(p);
431 goto out;
432 }
433
434 static int __devexit chmc_remove(struct of_device *op)
435 {
436 struct chmc *p = dev_get_drvdata(&op->dev);
437
438 if (p) {
439 list_del(&p->list);
440 of_iounmap(&op->resource[0], p->regs, 0x48);
441 kfree(p);
442 }
443 return 0;
444 }
445
446 static struct of_device_id chmc_match[] = {
447 {
448 .name = "memory-controller",
449 },
450 {},
451 };
452 MODULE_DEVICE_TABLE(of, chmc_match);
453
454 static struct of_platform_driver chmc_driver = {
455 .name = "chmc",
456 .match_table = chmc_match,
457 .probe = chmc_probe,
458 .remove = __devexit_p(chmc_remove),
459 };
460
461 static inline bool chmc_platform(void)
462 {
463 if (tlb_type == cheetah || tlb_type == cheetah_plus)
464 return true;
465 return false;
466 }
467
468 static int __init chmc_init(void)
469 {
470 int ret;
471
472 if (!chmc_platform())
473 return -ENODEV;
474
475 ret = register_dimm_printer(chmc_print_dimm);
476 if (!ret) {
477 ret = of_register_driver(&chmc_driver, &of_bus_type);
478 if (ret)
479 unregister_dimm_printer(chmc_print_dimm);
480 }
481 return ret;
482 }
483
484 static void __exit chmc_cleanup(void)
485 {
486 if (chmc_platform()) {
487 unregister_dimm_printer(chmc_print_dimm);
488 of_unregister_driver(&chmc_driver);
489 }
490 }
491
492 module_init(chmc_init);
493 module_exit(chmc_cleanup);