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>
26 #define DRV_MODULE_NAME "chmc"
27 #define PFX DRV_MODULE_NAME ": "
28 #define DRV_MODULE_VERSION "0.2"
30 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
31 MODULE_DESCRIPTION("UltraSPARC-III memory controller driver");
32 MODULE_LICENSE("GPL");
33 MODULE_VERSION(DRV_MODULE_VERSION
);
35 #define CHMCTRL_NDGRPS 2
36 #define CHMCTRL_NDIMMS 4
38 #define DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
40 /* OBP memory-layout property format. */
42 unsigned char dimm_map
[144];
43 unsigned char pin_map
[576];
46 #define DIMM_LABEL_SZ 8
48 struct obp_mem_layout
{
49 /* One max 8-byte string label per DIMM. Usually
50 * this matches the label on the motherboard where
53 char dimm_labels
[DIMMS_PER_MC
][DIMM_LABEL_SZ
];
55 /* If symmetric use map[0], else it is
56 * asymmetric and map[1] should be used.
60 struct obp_map map
[2];
63 #define CHMCTRL_NBANKS 4
66 struct mctrl_info
*mp
;
81 struct list_head list
;
84 struct obp_mem_layout layout_prop
;
95 struct bank_info logical_banks
[CHMCTRL_NBANKS
];
98 static LIST_HEAD(mctrl_list
);
100 /* Does BANK decode PHYS_ADDR? */
101 static int bank_match(struct bank_info
*bp
, unsigned long phys_addr
)
103 unsigned long upper_bits
= (phys_addr
& PA_UPPER_BITS
) >> PA_UPPER_BITS_SHIFT
;
104 unsigned long lower_bits
= (phys_addr
& PA_LOWER_BITS
) >> PA_LOWER_BITS_SHIFT
;
106 /* Bank must be enabled to match. */
110 /* Would BANK match upper bits? */
111 upper_bits
^= bp
->um
; /* What bits are different? */
112 upper_bits
= ~upper_bits
; /* Invert. */
113 upper_bits
|= bp
->uk
; /* What bits don't matter for matching? */
114 upper_bits
= ~upper_bits
; /* Invert. */
119 /* Would BANK match lower bits? */
120 lower_bits
^= bp
->lm
; /* What bits are different? */
121 lower_bits
= ~lower_bits
; /* Invert. */
122 lower_bits
|= bp
->lk
; /* What bits don't matter for matching? */
123 lower_bits
= ~lower_bits
; /* Invert. */
128 /* I always knew you'd be the one. */
132 /* Given PHYS_ADDR, search memory controller banks for a match. */
133 static struct bank_info
*find_bank(unsigned long phys_addr
)
135 struct list_head
*mctrl_head
= &mctrl_list
;
136 struct list_head
*mctrl_entry
= mctrl_head
->next
;
139 struct mctrl_info
*mp
=
140 list_entry(mctrl_entry
, struct mctrl_info
, list
);
143 if (mctrl_entry
== mctrl_head
)
145 mctrl_entry
= mctrl_entry
->next
;
147 for (bank_no
= 0; bank_no
< CHMCTRL_NBANKS
; bank_no
++) {
148 struct bank_info
*bp
;
150 bp
= &mp
->logical_banks
[bank_no
];
151 if (bank_match(bp
, phys_addr
))
159 /* This is the main purpose of this driver. */
160 #define SYNDROME_MIN -1
161 #define SYNDROME_MAX 144
162 int chmc_getunumber(int syndrome_code
,
163 unsigned long phys_addr
,
164 char *buf
, int buflen
)
166 struct bank_info
*bp
;
167 struct obp_mem_layout
*prop
;
168 int bank_in_controller
, first_dimm
;
170 bp
= find_bank(phys_addr
);
172 syndrome_code
< SYNDROME_MIN
||
173 syndrome_code
> SYNDROME_MAX
) {
181 prop
= &bp
->mp
->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
;
186 if (syndrome_code
!= SYNDROME_MIN
) {
188 int qword
, where_in_line
, where
, map_index
, map_offset
;
189 unsigned int map_val
;
191 /* Yaay, single bit error so we can figure out
199 /* Covert syndrome code into the way the bits are
200 * positioned on the bus.
202 if (syndrome_code
< 144 - 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);
209 syndrome_code
-= 144 + 3;
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.
218 #define LINE_ADDR_MSK (LINE_SIZE - 1)
219 #define QW_PER_LINE 4
220 #define QW_BYTES (LINE_SIZE / QW_PER_LINE)
222 #define LAST_BIT (576 - 1)
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));
232 sprintf(buf
, "%s, pin %3d",
233 prop
->dimm_labels
[first_dimm
+ map_val
],
234 map
->pin_map
[where_in_line
]);
238 /* Multi-bit error, we just dump out all the
239 * dimm labels associated with this bank.
241 for (dimm
= 0; dimm
< CHMCTRL_NDIMMS
; dimm
++) {
243 prop
->dimm_labels
[first_dimm
+ dimm
]);
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.
255 static u64
read_mcreg(struct mctrl_info
*mp
, unsigned long offset
)
257 unsigned long ret
, this_cpu
;
261 this_cpu
= real_hard_smp_processor_id();
263 if (mp
->portid
== this_cpu
) {
264 __asm__
__volatile__("ldxa [%1] %2, %0"
266 : "r" (offset
), "i" (ASI_MCU_CTRL_REG
));
268 __asm__
__volatile__("ldxa [%1] %2, %0"
270 : "r" (mp
->regs
+ offset
),
271 "i" (ASI_PHYS_BYPASS_EC_E
));
279 #if 0 /* currently unused */
280 static void write_mcreg(struct mctrl_info
*mp
, unsigned long offset
, u64 val
)
282 if (mp
->portid
== smp_processor_id()) {
283 __asm__
__volatile__("stxa %0, [%1] %2"
285 "r" (offset
), "i" (ASI_MCU_CTRL_REG
));
287 __asm__
__volatile__("ldxa %0, [%1] %2"
289 "r" (mp
->regs
+ offset
),
290 "i" (ASI_PHYS_BYPASS_EC_E
));
295 static void interpret_one_decode_reg(struct mctrl_info
*mp
, int which_bank
, u64 val
)
297 struct bank_info
*p
= &mp
->logical_banks
[which_bank
];
300 p
->bank_id
= (CHMCTRL_NBANKS
* mp
->portid
) + which_bank
;
302 p
->valid
= (val
& MEM_DECODE_VALID
) >> MEM_DECODE_VALID_SHIFT
;
303 p
->uk
= (val
& MEM_DECODE_UK
) >> MEM_DECODE_UK_SHIFT
;
304 p
->um
= (val
& MEM_DECODE_UM
) >> MEM_DECODE_UM_SHIFT
;
305 p
->lk
= (val
& MEM_DECODE_LK
) >> MEM_DECODE_LK_SHIFT
;
306 p
->lm
= (val
& MEM_DECODE_LM
) >> MEM_DECODE_LM_SHIFT
;
310 p
->base
<<= PA_UPPER_BITS_SHIFT
;
335 /* UK[10] is reserved, and UK[11] is not set for the SDRAM
336 * bank size definition.
338 p
->size
= (((unsigned long)p
->uk
&
339 ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT
;
340 p
->size
/= p
->interleave
;
343 static void fetch_decode_regs(struct mctrl_info
*mp
)
345 if (mp
->layout_size
== 0)
348 interpret_one_decode_reg(mp
, 0,
349 read_mcreg(mp
, CHMCTRL_DECODE1
));
350 interpret_one_decode_reg(mp
, 1,
351 read_mcreg(mp
, CHMCTRL_DECODE2
));
352 interpret_one_decode_reg(mp
, 2,
353 read_mcreg(mp
, CHMCTRL_DECODE3
));
354 interpret_one_decode_reg(mp
, 3,
355 read_mcreg(mp
, CHMCTRL_DECODE4
));
358 static int __devinit
chmc_probe(struct of_device
*op
,
359 const struct of_device_id
*match
)
361 struct device_node
*dp
= op
->node
;
362 struct mctrl_info
*mp
;
367 __asm__ ("rdpr %%ver, %0" : "=r" (ver
));
368 if ((ver
>> 32UL) == __JALAPENO_ID
||
369 (ver
>> 32UL) == __SERRANO_ID
)
372 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
376 portid
= of_getintprop_default(dp
, "portid", -1);
381 pval
= of_get_property(dp
, "memory-layout", &len
);
382 mp
->layout_size
= len
;
386 if (mp
->layout_size
> sizeof(mp
->layout_prop
)) {
387 printk(KERN_ERR PFX
"Unexpected memory-layout property "
388 "size %d.\n", mp
->layout_size
);
391 memcpy(&mp
->layout_prop
, pval
, len
);
394 mp
->regs
= of_ioremap(&op
->resource
[0], 0, 0x48, "chmc");
396 printk(KERN_ERR PFX
"Could not map registers.\n");
400 if (mp
->layout_size
!= 0UL) {
401 mp
->timing_control1
= read_mcreg(mp
, CHMCTRL_TCTRL1
);
402 mp
->timing_control2
= read_mcreg(mp
, CHMCTRL_TCTRL2
);
403 mp
->timing_control3
= read_mcreg(mp
, CHMCTRL_TCTRL3
);
404 mp
->timing_control4
= read_mcreg(mp
, CHMCTRL_TCTRL4
);
405 mp
->memaddr_control
= read_mcreg(mp
, CHMCTRL_MACTRL
);
408 fetch_decode_regs(mp
);
410 list_add(&mp
->list
, &mctrl_list
);
412 /* Report the device. */
413 printk(KERN_INFO PFX
"UltraSPARC-III memory controller at %s [%s]\n",
415 (mp
->layout_size
? "ACTIVE" : "INACTIVE"));
417 dev_set_drvdata(&op
->dev
, mp
);
423 if (mp
->regs
!= NULL
)
424 of_iounmap(&op
->resource
[0], mp
->regs
, 0x48);
430 static int __devexit
chmc_remove(struct of_device
*op
)
432 struct mctrl_info
*mp
= dev_get_drvdata(&op
->dev
);
436 of_iounmap(&op
->resource
[0], mp
->regs
, 0x48);
442 static struct of_device_id chmc_match
[] = {
444 .name
= "memory-controller",
448 MODULE_DEVICE_TABLE(of
, chmc_match
);
450 static struct of_platform_driver chmc_driver
= {
452 .match_table
= chmc_match
,
454 .remove
= __devexit_p(chmc_remove
),
457 static inline bool chmc_platform(void)
459 if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
464 static int __init
chmc_init(void)
466 if (!chmc_platform())
469 return of_register_driver(&chmc_driver
, &of_bus_type
);
472 static void __exit
chmc_cleanup(void)
475 of_unregister_driver(&chmc_driver
);
478 module_init(chmc_init
);
479 module_exit(chmc_cleanup
);