Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2006 Atmel Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/sysdev.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/cpu.h> |
Haavard Skinnemoen | 4ffabef | 2007-02-09 15:23:46 +0100 | [diff] [blame] | 12 | #include <linux/module.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 13 | #include <linux/percpu.h> |
| 14 | #include <linux/param.h> |
| 15 | #include <linux/errno.h> |
| 16 | |
| 17 | #include <asm/setup.h> |
| 18 | #include <asm/sysreg.h> |
| 19 | |
| 20 | static DEFINE_PER_CPU(struct cpu, cpu_devices); |
| 21 | |
| 22 | #ifdef CONFIG_PERFORMANCE_COUNTERS |
| 23 | |
| 24 | /* |
| 25 | * XXX: If/when a SMP-capable implementation of AVR32 will ever be |
| 26 | * made, we must make sure that the code executes on the correct CPU. |
| 27 | */ |
| 28 | static ssize_t show_pc0event(struct sys_device *dev, char *buf) |
| 29 | { |
| 30 | unsigned long pccr; |
| 31 | |
| 32 | pccr = sysreg_read(PCCR); |
| 33 | return sprintf(buf, "0x%lx\n", (pccr >> 12) & 0x3f); |
| 34 | } |
| 35 | static ssize_t store_pc0event(struct sys_device *dev, const char *buf, |
| 36 | size_t count) |
| 37 | { |
| 38 | unsigned long val; |
| 39 | char *endp; |
| 40 | |
| 41 | val = simple_strtoul(buf, &endp, 0); |
| 42 | if (endp == buf || val > 0x3f) |
| 43 | return -EINVAL; |
| 44 | val = (val << 12) | (sysreg_read(PCCR) & 0xfffc0fff); |
| 45 | sysreg_write(PCCR, val); |
| 46 | return count; |
| 47 | } |
| 48 | static ssize_t show_pc0count(struct sys_device *dev, char *buf) |
| 49 | { |
| 50 | unsigned long pcnt0; |
| 51 | |
| 52 | pcnt0 = sysreg_read(PCNT0); |
| 53 | return sprintf(buf, "%lu\n", pcnt0); |
| 54 | } |
| 55 | static ssize_t store_pc0count(struct sys_device *dev, const char *buf, |
| 56 | size_t count) |
| 57 | { |
| 58 | unsigned long val; |
| 59 | char *endp; |
| 60 | |
| 61 | val = simple_strtoul(buf, &endp, 0); |
| 62 | if (endp == buf) |
| 63 | return -EINVAL; |
| 64 | sysreg_write(PCNT0, val); |
| 65 | |
| 66 | return count; |
| 67 | } |
| 68 | |
| 69 | static ssize_t show_pc1event(struct sys_device *dev, char *buf) |
| 70 | { |
| 71 | unsigned long pccr; |
| 72 | |
| 73 | pccr = sysreg_read(PCCR); |
| 74 | return sprintf(buf, "0x%lx\n", (pccr >> 18) & 0x3f); |
| 75 | } |
| 76 | static ssize_t store_pc1event(struct sys_device *dev, const char *buf, |
| 77 | size_t count) |
| 78 | { |
| 79 | unsigned long val; |
| 80 | char *endp; |
| 81 | |
| 82 | val = simple_strtoul(buf, &endp, 0); |
| 83 | if (endp == buf || val > 0x3f) |
| 84 | return -EINVAL; |
| 85 | val = (val << 18) | (sysreg_read(PCCR) & 0xff03ffff); |
| 86 | sysreg_write(PCCR, val); |
| 87 | return count; |
| 88 | } |
| 89 | static ssize_t show_pc1count(struct sys_device *dev, char *buf) |
| 90 | { |
| 91 | unsigned long pcnt1; |
| 92 | |
| 93 | pcnt1 = sysreg_read(PCNT1); |
| 94 | return sprintf(buf, "%lu\n", pcnt1); |
| 95 | } |
| 96 | static ssize_t store_pc1count(struct sys_device *dev, const char *buf, |
| 97 | size_t count) |
| 98 | { |
| 99 | unsigned long val; |
| 100 | char *endp; |
| 101 | |
| 102 | val = simple_strtoul(buf, &endp, 0); |
| 103 | if (endp == buf) |
| 104 | return -EINVAL; |
| 105 | sysreg_write(PCNT1, val); |
| 106 | |
| 107 | return count; |
| 108 | } |
| 109 | |
| 110 | static ssize_t show_pccycles(struct sys_device *dev, char *buf) |
| 111 | { |
| 112 | unsigned long pccnt; |
| 113 | |
| 114 | pccnt = sysreg_read(PCCNT); |
| 115 | return sprintf(buf, "%lu\n", pccnt); |
| 116 | } |
| 117 | static ssize_t store_pccycles(struct sys_device *dev, const char *buf, |
| 118 | size_t count) |
| 119 | { |
| 120 | unsigned long val; |
| 121 | char *endp; |
| 122 | |
| 123 | val = simple_strtoul(buf, &endp, 0); |
| 124 | if (endp == buf) |
| 125 | return -EINVAL; |
| 126 | sysreg_write(PCCNT, val); |
| 127 | |
| 128 | return count; |
| 129 | } |
| 130 | |
| 131 | static ssize_t show_pcenable(struct sys_device *dev, char *buf) |
| 132 | { |
| 133 | unsigned long pccr; |
| 134 | |
| 135 | pccr = sysreg_read(PCCR); |
| 136 | return sprintf(buf, "%c\n", (pccr & 1)?'1':'0'); |
| 137 | } |
| 138 | static ssize_t store_pcenable(struct sys_device *dev, const char *buf, |
| 139 | size_t count) |
| 140 | { |
| 141 | unsigned long pccr, val; |
| 142 | char *endp; |
| 143 | |
| 144 | val = simple_strtoul(buf, &endp, 0); |
| 145 | if (endp == buf) |
| 146 | return -EINVAL; |
| 147 | if (val) |
| 148 | val = 1; |
| 149 | |
| 150 | pccr = sysreg_read(PCCR); |
| 151 | pccr = (pccr & ~1UL) | val; |
| 152 | sysreg_write(PCCR, pccr); |
| 153 | |
| 154 | return count; |
| 155 | } |
| 156 | |
| 157 | static SYSDEV_ATTR(pc0event, 0600, show_pc0event, store_pc0event); |
| 158 | static SYSDEV_ATTR(pc0count, 0600, show_pc0count, store_pc0count); |
| 159 | static SYSDEV_ATTR(pc1event, 0600, show_pc1event, store_pc1event); |
| 160 | static SYSDEV_ATTR(pc1count, 0600, show_pc1count, store_pc1count); |
| 161 | static SYSDEV_ATTR(pccycles, 0600, show_pccycles, store_pccycles); |
| 162 | static SYSDEV_ATTR(pcenable, 0600, show_pcenable, store_pcenable); |
| 163 | |
| 164 | #endif /* CONFIG_PERFORMANCE_COUNTERS */ |
| 165 | |
| 166 | static int __init topology_init(void) |
| 167 | { |
| 168 | int cpu; |
| 169 | |
| 170 | for_each_possible_cpu(cpu) { |
| 171 | struct cpu *c = &per_cpu(cpu_devices, cpu); |
| 172 | |
| 173 | register_cpu(c, cpu); |
| 174 | |
| 175 | #ifdef CONFIG_PERFORMANCE_COUNTERS |
| 176 | sysdev_create_file(&c->sysdev, &attr_pc0event); |
| 177 | sysdev_create_file(&c->sysdev, &attr_pc0count); |
| 178 | sysdev_create_file(&c->sysdev, &attr_pc1event); |
| 179 | sysdev_create_file(&c->sysdev, &attr_pc1count); |
| 180 | sysdev_create_file(&c->sysdev, &attr_pccycles); |
| 181 | sysdev_create_file(&c->sysdev, &attr_pcenable); |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | subsys_initcall(topology_init); |
| 189 | |
| 190 | static const char *cpu_names[] = { |
| 191 | "Morgan", |
| 192 | "AP7000", |
| 193 | }; |
| 194 | #define NR_CPU_NAMES ARRAY_SIZE(cpu_names) |
| 195 | |
| 196 | static const char *arch_names[] = { |
| 197 | "AVR32A", |
| 198 | "AVR32B", |
| 199 | }; |
| 200 | #define NR_ARCH_NAMES ARRAY_SIZE(arch_names) |
| 201 | |
| 202 | static const char *mmu_types[] = { |
| 203 | "No MMU", |
| 204 | "ITLB and DTLB", |
| 205 | "Shared TLB", |
| 206 | "MPU" |
| 207 | }; |
| 208 | |
| 209 | void __init setup_processor(void) |
| 210 | { |
| 211 | unsigned long config0, config1; |
| 212 | unsigned cpu_id, cpu_rev, arch_id, arch_rev, mmu_type; |
| 213 | unsigned tmp; |
| 214 | |
| 215 | config0 = sysreg_read(CONFIG0); /* 0x0000013e; */ |
| 216 | config1 = sysreg_read(CONFIG1); /* 0x01f689a2; */ |
| 217 | cpu_id = config0 >> 24; |
| 218 | cpu_rev = (config0 >> 16) & 0xff; |
| 219 | arch_id = (config0 >> 13) & 0x07; |
| 220 | arch_rev = (config0 >> 10) & 0x07; |
| 221 | mmu_type = (config0 >> 7) & 0x03; |
| 222 | |
| 223 | boot_cpu_data.arch_type = arch_id; |
| 224 | boot_cpu_data.cpu_type = cpu_id; |
| 225 | boot_cpu_data.arch_revision = arch_rev; |
| 226 | boot_cpu_data.cpu_revision = cpu_rev; |
| 227 | boot_cpu_data.tlb_config = mmu_type; |
| 228 | |
| 229 | tmp = (config1 >> 13) & 0x07; |
| 230 | if (tmp) { |
| 231 | boot_cpu_data.icache.ways = 1 << ((config1 >> 10) & 0x07); |
| 232 | boot_cpu_data.icache.sets = 1 << ((config1 >> 16) & 0x0f); |
| 233 | boot_cpu_data.icache.linesz = 1 << (tmp + 1); |
| 234 | } |
| 235 | tmp = (config1 >> 3) & 0x07; |
| 236 | if (tmp) { |
| 237 | boot_cpu_data.dcache.ways = 1 << (config1 & 0x07); |
| 238 | boot_cpu_data.dcache.sets = 1 << ((config1 >> 6) & 0x0f); |
| 239 | boot_cpu_data.dcache.linesz = 1 << (tmp + 1); |
| 240 | } |
| 241 | |
| 242 | if ((cpu_id >= NR_CPU_NAMES) || (arch_id >= NR_ARCH_NAMES)) { |
| 243 | printk ("Unknown CPU configuration (ID %02x, arch %02x), " |
| 244 | "continuing anyway...\n", |
| 245 | cpu_id, arch_id); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | printk ("CPU: %s [%02x] revision %d (%s revision %d)\n", |
| 250 | cpu_names[cpu_id], cpu_id, cpu_rev, |
| 251 | arch_names[arch_id], arch_rev); |
| 252 | printk ("CPU: MMU configuration: %s\n", mmu_types[mmu_type]); |
| 253 | printk ("CPU: features:"); |
| 254 | if (config0 & (1 << 6)) |
| 255 | printk(" fpu"); |
| 256 | if (config0 & (1 << 5)) |
| 257 | printk(" java"); |
| 258 | if (config0 & (1 << 4)) |
| 259 | printk(" perfctr"); |
| 260 | if (config0 & (1 << 3)) |
| 261 | printk(" ocd"); |
| 262 | printk("\n"); |
| 263 | } |
| 264 | |
| 265 | #ifdef CONFIG_PROC_FS |
| 266 | static int c_show(struct seq_file *m, void *v) |
| 267 | { |
| 268 | unsigned int icache_size, dcache_size; |
| 269 | unsigned int cpu = smp_processor_id(); |
| 270 | |
| 271 | icache_size = boot_cpu_data.icache.ways * |
| 272 | boot_cpu_data.icache.sets * |
| 273 | boot_cpu_data.icache.linesz; |
| 274 | dcache_size = boot_cpu_data.dcache.ways * |
| 275 | boot_cpu_data.dcache.sets * |
| 276 | boot_cpu_data.dcache.linesz; |
| 277 | |
| 278 | seq_printf(m, "processor\t: %d\n", cpu); |
| 279 | |
| 280 | if (boot_cpu_data.arch_type < NR_ARCH_NAMES) |
| 281 | seq_printf(m, "cpu family\t: %s revision %d\n", |
| 282 | arch_names[boot_cpu_data.arch_type], |
| 283 | boot_cpu_data.arch_revision); |
| 284 | if (boot_cpu_data.cpu_type < NR_CPU_NAMES) |
| 285 | seq_printf(m, "cpu type\t: %s revision %d\n", |
| 286 | cpu_names[boot_cpu_data.cpu_type], |
| 287 | boot_cpu_data.cpu_revision); |
| 288 | |
| 289 | seq_printf(m, "i-cache\t\t: %dK (%u ways x %u sets x %u)\n", |
| 290 | icache_size >> 10, |
| 291 | boot_cpu_data.icache.ways, |
| 292 | boot_cpu_data.icache.sets, |
| 293 | boot_cpu_data.icache.linesz); |
| 294 | seq_printf(m, "d-cache\t\t: %dK (%u ways x %u sets x %u)\n", |
| 295 | dcache_size >> 10, |
| 296 | boot_cpu_data.dcache.ways, |
| 297 | boot_cpu_data.dcache.sets, |
| 298 | boot_cpu_data.dcache.linesz); |
| 299 | seq_printf(m, "bogomips\t: %lu.%02lu\n", |
| 300 | boot_cpu_data.loops_per_jiffy / (500000/HZ), |
| 301 | (boot_cpu_data.loops_per_jiffy / (5000/HZ)) % 100); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void *c_start(struct seq_file *m, loff_t *pos) |
| 307 | { |
| 308 | return *pos < 1 ? (void *)1 : NULL; |
| 309 | } |
| 310 | |
| 311 | static void *c_next(struct seq_file *m, void *v, loff_t *pos) |
| 312 | { |
| 313 | ++*pos; |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | static void c_stop(struct seq_file *m, void *v) |
| 318 | { |
| 319 | |
| 320 | } |
| 321 | |
| 322 | struct seq_operations cpuinfo_op = { |
| 323 | .start = c_start, |
| 324 | .next = c_next, |
| 325 | .stop = c_stop, |
| 326 | .show = c_show |
| 327 | }; |
| 328 | #endif /* CONFIG_PROC_FS */ |