1 /* chmc.c: Driver for UltraSPARC-III memory controller.
3 * Copyright (C) 2001, 2007, 2008 David S. Miller (davem@davemloft.net)
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>
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>
25 #include <asm/memctrl.h>
27 #define DRV_MODULE_NAME "chmc"
28 #define PFX DRV_MODULE_NAME ": "
29 #define DRV_MODULE_VERSION "0.2"
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
);
37 #define MC_TYPE_SAFARI 1
38 #define MC_TYPE_JBUS 2
40 static dimm_printer_t us3mc_dimm_printer
;
42 #define CHMCTRL_NDGRPS 2
43 #define CHMCTRL_NDIMMS 4
45 #define CHMC_DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
47 /* OBP memory-layout property format. */
49 unsigned char dimm_map
[144];
50 unsigned char pin_map
[576];
53 #define DIMM_LABEL_SZ 8
55 struct chmc_obp_mem_layout
{
56 /* One max 8-byte string label per DIMM. Usually
57 * this matches the label on the motherboard where
60 char dimm_labels
[CHMC_DIMMS_PER_MC
][DIMM_LABEL_SZ
];
62 /* If symmetric use map[0], else it is
63 * asymmetric and map[1] should be used.
67 struct chmc_obp_map map
[2];
70 #define CHMCTRL_NBANKS 4
72 struct chmc_bank_info
{
88 struct list_head list
;
91 struct chmc_obp_mem_layout layout_prop
;
102 struct chmc_bank_info logical_banks
[CHMCTRL_NBANKS
];
105 #define JBUSMC_REGS_SIZE 8
107 #define JB_MC_REG1_DIMM2_BANK3 0x8000000000000000UL
108 #define JB_MC_REG1_DIMM1_BANK1 0x4000000000000000UL
109 #define JB_MC_REG1_DIMM2_BANK2 0x2000000000000000UL
110 #define JB_MC_REG1_DIMM1_BANK0 0x1000000000000000UL
111 #define JB_MC_REG1_XOR 0x0000010000000000UL
112 #define JB_MC_REG1_ADDR_GEN_2 0x000000e000000000UL
113 #define JB_MC_REG1_ADDR_GEN_2_SHIFT 37
114 #define JB_MC_REG1_ADDR_GEN_1 0x0000001c00000000UL
115 #define JB_MC_REG1_ADDR_GEN_1_SHIFT 34
116 #define JB_MC_REG1_INTERLEAVE 0x0000000001800000UL
117 #define JB_MC_REG1_INTERLEAVE_SHIFT 23
118 #define JB_MC_REG1_DIMM2_PTYPE 0x0000000000200000UL
119 #define JB_MC_REG1_DIMM2_PTYPE_SHIFT 21
120 #define JB_MC_REG1_DIMM1_PTYPE 0x0000000000100000UL
121 #define JB_MC_REG1_DIMM1_PTYPE_SHIFT 20
123 #define PART_TYPE_X8 0
124 #define PART_TYPE_X4 1
126 #define INTERLEAVE_NONE 0
127 #define INTERLEAVE_SAME 1
128 #define INTERLEAVE_INTERNAL 2
129 #define INTERLEAVE_BOTH 3
131 #define ADDR_GEN_128MB 0
132 #define ADDR_GEN_256MB 1
133 #define ADDR_GEN_512MB 2
134 #define ADDR_GEN_1GB 3
136 #define JB_NUM_DIMM_GROUPS 2
137 #define JB_NUM_DIMMS_PER_GROUP 2
138 #define JB_NUM_DIMMS (JB_NUM_DIMM_GROUPS * JB_NUM_DIMMS_PER_GROUP)
140 struct jbusmc_obp_map
{
141 unsigned char dimm_map
[18];
142 unsigned char pin_map
[144];
145 struct jbusmc_obp_mem_layout
{
146 /* One max 8-byte string label per DIMM. Usually
147 * this matches the label on the motherboard where
150 char dimm_labels
[JB_NUM_DIMMS
][DIMM_LABEL_SZ
];
152 /* If symmetric use map[0], else it is
153 * asymmetric and map[1] should be used.
157 struct jbusmc_obp_map map
;
162 struct jbusmc_dimm_group
{
163 struct jbusmc
*controller
;
173 struct jbusmc_obp_mem_layout layout
;
176 struct jbusmc_dimm_group dimm_groups
[JB_NUM_DIMM_GROUPS
];
177 struct list_head list
;
180 static DEFINE_SPINLOCK(mctrl_list_lock
);
181 static LIST_HEAD(mctrl_list
);
183 static void mc_list_add(struct list_head
*list
)
185 spin_lock(&mctrl_list_lock
);
186 list_add(list
, &mctrl_list
);
187 spin_unlock(&mctrl_list_lock
);
190 static void mc_list_del(struct list_head
*list
)
192 spin_lock(&mctrl_list_lock
);
194 spin_unlock(&mctrl_list_lock
);
197 #define SYNDROME_MIN -1
198 #define SYNDROME_MAX 144
200 /* Covert syndrome code into the way the bits are positioned
203 static int syndrome_to_qword_code(int syndrome_code
)
205 if (syndrome_code
< 128)
207 else if (syndrome_code
< 128 + 9)
208 syndrome_code
-= (128 - 7);
209 else if (syndrome_code
< (128 + 9 + 3))
210 syndrome_code
-= (128 + 9 - 4);
212 syndrome_code
-= (128 + 9 + 3);
213 return syndrome_code
;
216 /* All this magic has to do with how a cache line comes over the wire
217 * on Safari and JBUS. A 64-bit line comes over in 1 or more quadword
218 * cycles, each of which transmit ECC/MTAG info as well as the actual
221 #define L2_LINE_SIZE 64
222 #define L2_LINE_ADDR_MSK (L2_LINE_SIZE - 1)
223 #define QW_PER_LINE 4
224 #define QW_BYTES (L2_LINE_SIZE / QW_PER_LINE)
226 #define SAFARI_LAST_BIT (576 - 1)
227 #define JBUS_LAST_BIT (144 - 1)
229 static void get_pin_and_dimm_str(int syndrome_code
, unsigned long paddr
,
230 int *pin_p
, char **dimm_str_p
, void *_prop
,
231 int base_dimm_offset
)
233 int qword_code
= syndrome_to_qword_code(syndrome_code
);
234 int cache_line_offset
;
239 if (mc_type
== MC_TYPE_JBUS
) {
240 struct jbusmc_obp_mem_layout
*p
= _prop
;
243 cache_line_offset
= qword_code
;
244 offset_inverse
= (JBUS_LAST_BIT
- cache_line_offset
);
245 dimm_map_index
= offset_inverse
/ 8;
246 map_val
= p
->map
.dimm_map
[dimm_map_index
];
247 map_val
= ((map_val
>> ((7 - (offset_inverse
& 7)))) & 1);
248 *dimm_str_p
= p
->dimm_labels
[base_dimm_offset
+ map_val
];
249 *pin_p
= p
->map
.pin_map
[cache_line_offset
];
251 struct chmc_obp_mem_layout
*p
= _prop
;
252 struct chmc_obp_map
*mp
;
261 qword
= (paddr
& L2_LINE_ADDR_MSK
) / QW_BYTES
;
262 cache_line_offset
= ((3 - qword
) * QW_BITS
) + qword_code
;
263 offset_inverse
= (SAFARI_LAST_BIT
- cache_line_offset
);
264 dimm_map_index
= offset_inverse
>> 2;
265 map_val
= mp
->dimm_map
[dimm_map_index
];
266 map_val
= ((map_val
>> ((3 - (offset_inverse
& 3)) << 1)) & 0x3);
267 *dimm_str_p
= p
->dimm_labels
[base_dimm_offset
+ map_val
];
268 *pin_p
= mp
->pin_map
[cache_line_offset
];
272 static struct jbusmc_dimm_group
*jbusmc_find_dimm_group(unsigned long phys_addr
)
276 list_for_each_entry(p
, &mctrl_list
, list
) {
279 for (i
= 0; i
< p
->num_dimm_groups
; i
++) {
280 struct jbusmc_dimm_group
*dp
= &p
->dimm_groups
[i
];
282 if (phys_addr
< dp
->base_addr
||
283 (dp
->base_addr
+ dp
->size
) <= phys_addr
)
292 static int jbusmc_print_dimm(int syndrome_code
,
293 unsigned long phys_addr
,
294 char *buf
, int buflen
)
296 struct jbusmc_obp_mem_layout
*prop
;
297 struct jbusmc_dimm_group
*dp
;
301 dp
= jbusmc_find_dimm_group(phys_addr
);
303 syndrome_code
< SYNDROME_MIN
||
304 syndrome_code
> SYNDROME_MAX
) {
314 first_dimm
= dp
->index
* JB_NUM_DIMMS_PER_GROUP
;
316 if (syndrome_code
!= SYNDROME_MIN
) {
320 get_pin_and_dimm_str(syndrome_code
, phys_addr
, &pin
,
321 &dimm_str
, prop
, first_dimm
);
322 sprintf(buf
, "%s, pin %3d", dimm_str
, pin
);
326 /* Multi-bit error, we just dump out all the
327 * dimm labels associated with this dimm group.
329 for (dimm
= 0; dimm
< JB_NUM_DIMMS_PER_GROUP
; dimm
++) {
331 prop
->dimm_labels
[first_dimm
+ dimm
]);
339 static u64
jbusmc_dimm_group_size(u64 base
,
340 const struct linux_prom64_registers
*mem_regs
,
343 u64 max
= base
+ (8UL * 1024 * 1024 * 1024);
347 for (i
= 0; i
< num_mem_regs
; i
++) {
348 const struct linux_prom64_registers
*ent
;
353 this_base
= ent
->phys_addr
;
354 this_end
= this_base
+ ent
->reg_size
;
355 if (base
< this_base
|| base
>= this_end
)
359 if (this_end
> max_seen
)
363 return max_seen
- base
;
366 static void jbusmc_construct_one_dimm_group(struct jbusmc
*p
,
368 const struct linux_prom64_registers
*mem_regs
,
371 struct jbusmc_dimm_group
*dp
= &p
->dimm_groups
[index
];
376 dp
->base_addr
= (p
->portid
* (64UL * 1024 * 1024 * 1024));
377 dp
->base_addr
+= (index
* (8UL * 1024 * 1024 * 1024));
378 dp
->size
= jbusmc_dimm_group_size(dp
->base_addr
, mem_regs
, num_mem_regs
);
381 static void jbusmc_construct_dimm_groups(struct jbusmc
*p
,
382 const struct linux_prom64_registers
*mem_regs
,
385 if (p
->mc_reg_1
& JB_MC_REG1_DIMM1_BANK0
) {
386 jbusmc_construct_one_dimm_group(p
, 0, mem_regs
, num_mem_regs
);
387 p
->num_dimm_groups
++;
389 if (p
->mc_reg_1
& JB_MC_REG1_DIMM2_BANK2
) {
390 jbusmc_construct_one_dimm_group(p
, 1, mem_regs
, num_mem_regs
);
391 p
->num_dimm_groups
++;
395 static int jbusmc_probe(struct platform_device
*op
)
397 const struct linux_prom64_registers
*mem_regs
;
398 struct device_node
*mem_node
;
399 int err
, len
, num_mem_regs
;
405 mem_node
= of_find_node_by_path("/memory");
407 printk(KERN_ERR PFX
"Cannot find /memory node.\n");
410 mem_regs
= of_get_property(mem_node
, "reg", &len
);
412 printk(KERN_ERR PFX
"Cannot get reg property of /memory node.\n");
415 num_mem_regs
= len
/ sizeof(*mem_regs
);
418 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
420 printk(KERN_ERR PFX
"Cannot allocate struct jbusmc.\n");
424 INIT_LIST_HEAD(&p
->list
);
427 prop
= of_get_property(op
->dev
.of_node
, "portid", &len
);
428 if (!prop
|| len
!= 4) {
429 printk(KERN_ERR PFX
"Cannot find portid.\n");
435 prop
= of_get_property(op
->dev
.of_node
, "memory-control-register-1", &len
);
436 if (!prop
|| len
!= 8) {
437 printk(KERN_ERR PFX
"Cannot get memory control register 1.\n");
441 p
->mc_reg_1
= ((u64
)prop
[0] << 32) | (u64
) prop
[1];
444 p
->regs
= of_ioremap(&op
->resource
[0], 0, JBUSMC_REGS_SIZE
, "jbusmc");
446 printk(KERN_ERR PFX
"Cannot map jbusmc regs.\n");
451 ml
= of_get_property(op
->dev
.of_node
, "memory-layout", &p
->layout_len
);
453 printk(KERN_ERR PFX
"Cannot get memory layout property.\n");
456 if (p
->layout_len
> sizeof(p
->layout
)) {
457 printk(KERN_ERR PFX
"Unexpected memory-layout size %d\n",
461 memcpy(&p
->layout
, ml
, p
->layout_len
);
463 jbusmc_construct_dimm_groups(p
, mem_regs
, num_mem_regs
);
465 mc_list_add(&p
->list
);
467 printk(KERN_INFO PFX
"UltraSPARC-IIIi memory controller at %s\n",
468 op
->dev
.of_node
->full_name
);
470 dev_set_drvdata(&op
->dev
, p
);
478 of_iounmap(&op
->resource
[0], p
->regs
, JBUSMC_REGS_SIZE
);
485 /* Does BANK decode PHYS_ADDR? */
486 static int chmc_bank_match(struct chmc_bank_info
*bp
, unsigned long phys_addr
)
488 unsigned long upper_bits
= (phys_addr
& PA_UPPER_BITS
) >> PA_UPPER_BITS_SHIFT
;
489 unsigned long lower_bits
= (phys_addr
& PA_LOWER_BITS
) >> PA_LOWER_BITS_SHIFT
;
491 /* Bank must be enabled to match. */
495 /* Would BANK match upper bits? */
496 upper_bits
^= bp
->um
; /* What bits are different? */
497 upper_bits
= ~upper_bits
; /* Invert. */
498 upper_bits
|= bp
->uk
; /* What bits don't matter for matching? */
499 upper_bits
= ~upper_bits
; /* Invert. */
504 /* Would BANK match lower bits? */
505 lower_bits
^= bp
->lm
; /* What bits are different? */
506 lower_bits
= ~lower_bits
; /* Invert. */
507 lower_bits
|= bp
->lk
; /* What bits don't matter for matching? */
508 lower_bits
= ~lower_bits
; /* Invert. */
513 /* I always knew you'd be the one. */
517 /* Given PHYS_ADDR, search memory controller banks for a match. */
518 static struct chmc_bank_info
*chmc_find_bank(unsigned long phys_addr
)
522 list_for_each_entry(p
, &mctrl_list
, list
) {
525 for (bank_no
= 0; bank_no
< CHMCTRL_NBANKS
; bank_no
++) {
526 struct chmc_bank_info
*bp
;
528 bp
= &p
->logical_banks
[bank_no
];
529 if (chmc_bank_match(bp
, phys_addr
))
537 /* This is the main purpose of this driver. */
538 static int chmc_print_dimm(int syndrome_code
,
539 unsigned long phys_addr
,
540 char *buf
, int buflen
)
542 struct chmc_bank_info
*bp
;
543 struct chmc_obp_mem_layout
*prop
;
544 int bank_in_controller
, first_dimm
;
546 bp
= chmc_find_bank(phys_addr
);
548 syndrome_code
< SYNDROME_MIN
||
549 syndrome_code
> SYNDROME_MAX
) {
557 prop
= &bp
->p
->layout_prop
;
558 bank_in_controller
= bp
->bank_id
& (CHMCTRL_NBANKS
- 1);
559 first_dimm
= (bank_in_controller
& (CHMCTRL_NDGRPS
- 1));
560 first_dimm
*= CHMCTRL_NDIMMS
;
562 if (syndrome_code
!= SYNDROME_MIN
) {
566 get_pin_and_dimm_str(syndrome_code
, phys_addr
, &pin
,
567 &dimm_str
, prop
, first_dimm
);
568 sprintf(buf
, "%s, pin %3d", dimm_str
, pin
);
572 /* Multi-bit error, we just dump out all the
573 * dimm labels associated with this bank.
575 for (dimm
= 0; dimm
< CHMCTRL_NDIMMS
; dimm
++) {
577 prop
->dimm_labels
[first_dimm
+ dimm
]);
584 /* Accessing the registers is slightly complicated. If you want
585 * to get at the memory controller which is on the same processor
586 * the code is executing, you must use special ASI load/store else
587 * you go through the global mapping.
589 static u64
chmc_read_mcreg(struct chmc
*p
, unsigned long offset
)
591 unsigned long ret
, this_cpu
;
595 this_cpu
= real_hard_smp_processor_id();
597 if (p
->portid
== this_cpu
) {
598 __asm__
__volatile__("ldxa [%1] %2, %0"
600 : "r" (offset
), "i" (ASI_MCU_CTRL_REG
));
602 __asm__
__volatile__("ldxa [%1] %2, %0"
604 : "r" (p
->regs
+ offset
),
605 "i" (ASI_PHYS_BYPASS_EC_E
));
613 #if 0 /* currently unused */
614 static void chmc_write_mcreg(struct chmc
*p
, unsigned long offset
, u64 val
)
616 if (p
->portid
== smp_processor_id()) {
617 __asm__
__volatile__("stxa %0, [%1] %2"
619 "r" (offset
), "i" (ASI_MCU_CTRL_REG
));
621 __asm__
__volatile__("ldxa %0, [%1] %2"
623 "r" (p
->regs
+ offset
),
624 "i" (ASI_PHYS_BYPASS_EC_E
));
629 static void chmc_interpret_one_decode_reg(struct chmc
*p
, int which_bank
, u64 val
)
631 struct chmc_bank_info
*bp
= &p
->logical_banks
[which_bank
];
634 bp
->bank_id
= (CHMCTRL_NBANKS
* p
->portid
) + which_bank
;
636 bp
->valid
= (val
& MEM_DECODE_VALID
) >> MEM_DECODE_VALID_SHIFT
;
637 bp
->uk
= (val
& MEM_DECODE_UK
) >> MEM_DECODE_UK_SHIFT
;
638 bp
->um
= (val
& MEM_DECODE_UM
) >> MEM_DECODE_UM_SHIFT
;
639 bp
->lk
= (val
& MEM_DECODE_LK
) >> MEM_DECODE_LK_SHIFT
;
640 bp
->lm
= (val
& MEM_DECODE_LM
) >> MEM_DECODE_LM_SHIFT
;
643 bp
->base
&= ~(bp
->uk
);
644 bp
->base
<<= PA_UPPER_BITS_SHIFT
;
669 /* UK[10] is reserved, and UK[11] is not set for the SDRAM
670 * bank size definition.
672 bp
->size
= (((unsigned long)bp
->uk
&
673 ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT
;
674 bp
->size
/= bp
->interleave
;
677 static void chmc_fetch_decode_regs(struct chmc
*p
)
679 if (p
->layout_size
== 0)
682 chmc_interpret_one_decode_reg(p
, 0,
683 chmc_read_mcreg(p
, CHMCTRL_DECODE1
));
684 chmc_interpret_one_decode_reg(p
, 1,
685 chmc_read_mcreg(p
, CHMCTRL_DECODE2
));
686 chmc_interpret_one_decode_reg(p
, 2,
687 chmc_read_mcreg(p
, CHMCTRL_DECODE3
));
688 chmc_interpret_one_decode_reg(p
, 3,
689 chmc_read_mcreg(p
, CHMCTRL_DECODE4
));
692 static int chmc_probe(struct platform_device
*op
)
694 struct device_node
*dp
= op
->dev
.of_node
;
702 __asm__ ("rdpr %%ver, %0" : "=r" (ver
));
703 if ((ver
>> 32UL) == __JALAPENO_ID
||
704 (ver
>> 32UL) == __SERRANO_ID
)
707 portid
= of_getintprop_default(dp
, "portid", -1);
711 pval
= of_get_property(dp
, "memory-layout", &len
);
712 if (pval
&& len
> sizeof(p
->layout_prop
)) {
713 printk(KERN_ERR PFX
"Unexpected memory-layout property "
719 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
721 printk(KERN_ERR PFX
"Could not allocate struct chmc.\n");
726 p
->layout_size
= len
;
730 memcpy(&p
->layout_prop
, pval
, len
);
732 p
->regs
= of_ioremap(&op
->resource
[0], 0, 0x48, "chmc");
734 printk(KERN_ERR PFX
"Could not map registers.\n");
738 if (p
->layout_size
!= 0UL) {
739 p
->timing_control1
= chmc_read_mcreg(p
, CHMCTRL_TCTRL1
);
740 p
->timing_control2
= chmc_read_mcreg(p
, CHMCTRL_TCTRL2
);
741 p
->timing_control3
= chmc_read_mcreg(p
, CHMCTRL_TCTRL3
);
742 p
->timing_control4
= chmc_read_mcreg(p
, CHMCTRL_TCTRL4
);
743 p
->memaddr_control
= chmc_read_mcreg(p
, CHMCTRL_MACTRL
);
746 chmc_fetch_decode_regs(p
);
748 mc_list_add(&p
->list
);
750 printk(KERN_INFO PFX
"UltraSPARC-III memory controller at %s [%s]\n",
752 (p
->layout_size
? "ACTIVE" : "INACTIVE"));
754 dev_set_drvdata(&op
->dev
, p
);
766 static int us3mc_probe(struct platform_device
*op
)
768 if (mc_type
== MC_TYPE_SAFARI
)
769 return chmc_probe(op
);
770 else if (mc_type
== MC_TYPE_JBUS
)
771 return jbusmc_probe(op
);
775 static void chmc_destroy(struct platform_device
*op
, struct chmc
*p
)
778 of_iounmap(&op
->resource
[0], p
->regs
, 0x48);
782 static void jbusmc_destroy(struct platform_device
*op
, struct jbusmc
*p
)
784 mc_list_del(&p
->list
);
785 of_iounmap(&op
->resource
[0], p
->regs
, JBUSMC_REGS_SIZE
);
789 static int us3mc_remove(struct platform_device
*op
)
791 void *p
= dev_get_drvdata(&op
->dev
);
794 if (mc_type
== MC_TYPE_SAFARI
)
796 else if (mc_type
== MC_TYPE_JBUS
)
797 jbusmc_destroy(op
, p
);
802 static const struct of_device_id us3mc_match
[] = {
804 .name
= "memory-controller",
808 MODULE_DEVICE_TABLE(of
, us3mc_match
);
810 static struct platform_driver us3mc_driver
= {
813 .of_match_table
= us3mc_match
,
815 .probe
= us3mc_probe
,
816 .remove
= us3mc_remove
,
819 static inline bool us3mc_platform(void)
821 if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
826 static int __init
us3mc_init(void)
831 if (!us3mc_platform())
834 __asm__
__volatile__("rdpr %%ver, %0" : "=r" (ver
));
835 if ((ver
>> 32UL) == __JALAPENO_ID
||
836 (ver
>> 32UL) == __SERRANO_ID
) {
837 mc_type
= MC_TYPE_JBUS
;
838 us3mc_dimm_printer
= jbusmc_print_dimm
;
840 mc_type
= MC_TYPE_SAFARI
;
841 us3mc_dimm_printer
= chmc_print_dimm
;
844 ret
= register_dimm_printer(us3mc_dimm_printer
);
847 ret
= platform_driver_register(&us3mc_driver
);
849 unregister_dimm_printer(us3mc_dimm_printer
);
854 static void __exit
us3mc_cleanup(void)
856 if (us3mc_platform()) {
857 unregister_dimm_printer(us3mc_dimm_printer
);
858 platform_driver_unregister(&us3mc_driver
);
862 module_init(us3mc_init
);
863 module_exit(us3mc_cleanup
);