1 /* memctrlr.c: Driver for UltraSPARC-III memory controller.
3 * Copyright (C) 2001, 2007 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>
16 #include <asm/spitfire.h>
17 #include <asm/chmctrl.h>
18 #include <asm/cpudata.h>
19 #include <asm/oplib.h>
23 #define CHMCTRL_NDGRPS 2
24 #define CHMCTRL_NDIMMS 4
26 #define DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
28 /* OBP memory-layout property format. */
30 unsigned char dimm_map
[144];
31 unsigned char pin_map
[576];
34 #define DIMM_LABEL_SZ 8
36 struct obp_mem_layout
{
37 /* One max 8-byte string label per DIMM. Usually
38 * this matches the label on the motherboard where
41 char dimm_labels
[DIMMS_PER_MC
][DIMM_LABEL_SZ
];
43 /* If symmetric use map[0], else it is
44 * asymmetric and map[1] should be used.
48 struct obp_map map
[2];
51 #define CHMCTRL_NBANKS 4
54 struct mctrl_info
*mp
;
69 struct list_head list
;
72 struct obp_mem_layout layout_prop
;
83 struct bank_info logical_banks
[CHMCTRL_NBANKS
];
86 static LIST_HEAD(mctrl_list
);
88 /* Does BANK decode PHYS_ADDR? */
89 static int bank_match(struct bank_info
*bp
, unsigned long phys_addr
)
91 unsigned long upper_bits
= (phys_addr
& PA_UPPER_BITS
) >> PA_UPPER_BITS_SHIFT
;
92 unsigned long lower_bits
= (phys_addr
& PA_LOWER_BITS
) >> PA_LOWER_BITS_SHIFT
;
94 /* Bank must be enabled to match. */
98 /* Would BANK match upper bits? */
99 upper_bits
^= bp
->um
; /* What bits are different? */
100 upper_bits
= ~upper_bits
; /* Invert. */
101 upper_bits
|= bp
->uk
; /* What bits don't matter for matching? */
102 upper_bits
= ~upper_bits
; /* Invert. */
107 /* Would BANK match lower bits? */
108 lower_bits
^= bp
->lm
; /* What bits are different? */
109 lower_bits
= ~lower_bits
; /* Invert. */
110 lower_bits
|= bp
->lk
; /* What bits don't matter for matching? */
111 lower_bits
= ~lower_bits
; /* Invert. */
116 /* I always knew you'd be the one. */
120 /* Given PHYS_ADDR, search memory controller banks for a match. */
121 static struct bank_info
*find_bank(unsigned long phys_addr
)
123 struct list_head
*mctrl_head
= &mctrl_list
;
124 struct list_head
*mctrl_entry
= mctrl_head
->next
;
127 struct mctrl_info
*mp
=
128 list_entry(mctrl_entry
, struct mctrl_info
, list
);
131 if (mctrl_entry
== mctrl_head
)
133 mctrl_entry
= mctrl_entry
->next
;
135 for (bank_no
= 0; bank_no
< CHMCTRL_NBANKS
; bank_no
++) {
136 struct bank_info
*bp
;
138 bp
= &mp
->logical_banks
[bank_no
];
139 if (bank_match(bp
, phys_addr
))
147 /* This is the main purpose of this driver. */
148 #define SYNDROME_MIN -1
149 #define SYNDROME_MAX 144
150 int chmc_getunumber(int syndrome_code
,
151 unsigned long phys_addr
,
152 char *buf
, int buflen
)
154 struct bank_info
*bp
;
155 struct obp_mem_layout
*prop
;
156 int bank_in_controller
, first_dimm
;
158 bp
= find_bank(phys_addr
);
160 syndrome_code
< SYNDROME_MIN
||
161 syndrome_code
> SYNDROME_MAX
) {
169 prop
= &bp
->mp
->layout_prop
;
170 bank_in_controller
= bp
->bank_id
& (CHMCTRL_NBANKS
- 1);
171 first_dimm
= (bank_in_controller
& (CHMCTRL_NDGRPS
- 1));
172 first_dimm
*= CHMCTRL_NDIMMS
;
174 if (syndrome_code
!= SYNDROME_MIN
) {
176 int qword
, where_in_line
, where
, map_index
, map_offset
;
177 unsigned int map_val
;
179 /* Yaay, single bit error so we can figure out
187 /* Covert syndrome code into the way the bits are
188 * positioned on the bus.
190 if (syndrome_code
< 144 - 16)
192 else if (syndrome_code
< 144)
193 syndrome_code
-= (144 - 7);
194 else if (syndrome_code
< (144 + 3))
195 syndrome_code
-= (144 + 3 - 4);
197 syndrome_code
-= 144 + 3;
199 /* All this magic has to do with how a cache line
200 * comes over the wire on Safari. A 64-bit line
201 * comes over in 4 quadword cycles, each of which
202 * transmit ECC/MTAG info as well as the actual
203 * data. 144 bits per quadword, 576 total.
206 #define LINE_ADDR_MSK (LINE_SIZE - 1)
207 #define QW_PER_LINE 4
208 #define QW_BYTES (LINE_SIZE / QW_PER_LINE)
210 #define LAST_BIT (576 - 1)
212 qword
= (phys_addr
& LINE_ADDR_MSK
) / QW_BYTES
;
213 where_in_line
= ((3 - qword
) * QW_BITS
) + syndrome_code
;
214 where
= (LAST_BIT
- where_in_line
);
215 map_index
= where
>> 2;
216 map_offset
= where
& 0x3;
217 map_val
= map
->dimm_map
[map_index
];
218 map_val
= ((map_val
>> ((3 - map_offset
) << 1)) & (2 - 1));
220 sprintf(buf
, "%s, pin %3d",
221 prop
->dimm_labels
[first_dimm
+ map_val
],
222 map
->pin_map
[where_in_line
]);
226 /* Multi-bit error, we just dump out all the
227 * dimm labels associated with this bank.
229 for (dimm
= 0; dimm
< CHMCTRL_NDIMMS
; dimm
++) {
231 prop
->dimm_labels
[first_dimm
+ dimm
]);
238 /* Accessing the registers is slightly complicated. If you want
239 * to get at the memory controller which is on the same processor
240 * the code is executing, you must use special ASI load/store else
241 * you go through the global mapping.
243 static u64
read_mcreg(struct mctrl_info
*mp
, unsigned long offset
)
245 unsigned long ret
, this_cpu
;
249 this_cpu
= real_hard_smp_processor_id();
251 if (mp
->portid
== this_cpu
) {
252 __asm__
__volatile__("ldxa [%1] %2, %0"
254 : "r" (offset
), "i" (ASI_MCU_CTRL_REG
));
256 __asm__
__volatile__("ldxa [%1] %2, %0"
258 : "r" (mp
->regs
+ offset
),
259 "i" (ASI_PHYS_BYPASS_EC_E
));
267 #if 0 /* currently unused */
268 static void write_mcreg(struct mctrl_info
*mp
, unsigned long offset
, u64 val
)
270 if (mp
->portid
== smp_processor_id()) {
271 __asm__
__volatile__("stxa %0, [%1] %2"
273 "r" (offset
), "i" (ASI_MCU_CTRL_REG
));
275 __asm__
__volatile__("ldxa %0, [%1] %2"
277 "r" (mp
->regs
+ offset
),
278 "i" (ASI_PHYS_BYPASS_EC_E
));
283 static void interpret_one_decode_reg(struct mctrl_info
*mp
, int which_bank
, u64 val
)
285 struct bank_info
*p
= &mp
->logical_banks
[which_bank
];
288 p
->bank_id
= (CHMCTRL_NBANKS
* mp
->portid
) + which_bank
;
290 p
->valid
= (val
& MEM_DECODE_VALID
) >> MEM_DECODE_VALID_SHIFT
;
291 p
->uk
= (val
& MEM_DECODE_UK
) >> MEM_DECODE_UK_SHIFT
;
292 p
->um
= (val
& MEM_DECODE_UM
) >> MEM_DECODE_UM_SHIFT
;
293 p
->lk
= (val
& MEM_DECODE_LK
) >> MEM_DECODE_LK_SHIFT
;
294 p
->lm
= (val
& MEM_DECODE_LM
) >> MEM_DECODE_LM_SHIFT
;
298 p
->base
<<= PA_UPPER_BITS_SHIFT
;
323 /* UK[10] is reserved, and UK[11] is not set for the SDRAM
324 * bank size definition.
326 p
->size
= (((unsigned long)p
->uk
&
327 ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT
;
328 p
->size
/= p
->interleave
;
331 static void fetch_decode_regs(struct mctrl_info
*mp
)
333 if (mp
->layout_size
== 0)
336 interpret_one_decode_reg(mp
, 0,
337 read_mcreg(mp
, CHMCTRL_DECODE1
));
338 interpret_one_decode_reg(mp
, 1,
339 read_mcreg(mp
, CHMCTRL_DECODE2
));
340 interpret_one_decode_reg(mp
, 2,
341 read_mcreg(mp
, CHMCTRL_DECODE3
));
342 interpret_one_decode_reg(mp
, 3,
343 read_mcreg(mp
, CHMCTRL_DECODE4
));
346 static int init_one_mctrl(struct device_node
*dp
)
348 struct mctrl_info
*mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
349 int portid
= of_getintprop_default(dp
, "portid", -1);
350 const struct linux_prom64_registers
*regs
;
360 pval
= of_get_property(dp
, "memory-layout", &len
);
361 mp
->layout_size
= len
;
365 if (mp
->layout_size
> sizeof(mp
->layout_prop
))
367 memcpy(&mp
->layout_prop
, pval
, len
);
370 regs
= of_get_property(dp
, "reg", NULL
);
371 if (!regs
|| regs
->reg_size
!= 0x48)
374 mp
->regs
= ioremap(regs
->phys_addr
, regs
->reg_size
);
375 if (mp
->regs
== NULL
)
378 if (mp
->layout_size
!= 0UL) {
379 mp
->timing_control1
= read_mcreg(mp
, CHMCTRL_TCTRL1
);
380 mp
->timing_control2
= read_mcreg(mp
, CHMCTRL_TCTRL2
);
381 mp
->timing_control3
= read_mcreg(mp
, CHMCTRL_TCTRL3
);
382 mp
->timing_control4
= read_mcreg(mp
, CHMCTRL_TCTRL4
);
383 mp
->memaddr_control
= read_mcreg(mp
, CHMCTRL_MACTRL
);
386 fetch_decode_regs(mp
);
388 list_add(&mp
->list
, &mctrl_list
);
390 /* Report the device. */
391 printk(KERN_INFO
"%s: US3 memory controller at %p [%s]\n",
393 mp
->regs
, (mp
->layout_size
? "ACTIVE" : "INACTIVE"));
399 if (mp
->regs
!= NULL
)
406 static int __init
chmc_init(void)
408 struct device_node
*dp
;
410 /* This driver is only for cheetah platforms. */
411 if (tlb_type
!= cheetah
&& tlb_type
!= cheetah_plus
)
414 for_each_node_by_name(dp
, "memory-controller")
417 for_each_node_by_name(dp
, "mc-us3")
423 static void __exit
chmc_cleanup(void)
425 struct list_head
*head
= &mctrl_list
;
426 struct list_head
*tmp
= head
->next
;
429 struct mctrl_info
*p
=
430 list_entry(tmp
, struct mctrl_info
, list
);
441 module_init(chmc_init
);
442 module_exit(chmc_cleanup
);