sparc-us2e-cpufreq.c 8.72 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/* us2e_cpufreq.c: UltraSPARC-IIe cpu frequency support
 *
 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
 *
 * Many thanks to Dominik Brodowski for fixing up the cpufreq
 * infrastructure in order to make this driver easier to implement.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/smp.h>
#include <linux/cpufreq.h>
#include <linux/threads.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/init.h>

#include <asm/asi.h>
#include <asm/timer.h>

static struct cpufreq_driver *cpufreq_us2e_driver;

struct us2e_freq_percpu_info {
	struct cpufreq_frequency_table table[6];
};

/* Indexed by cpu number. */
static struct us2e_freq_percpu_info *us2e_freq_table;

#define HBIRD_MEM_CNTL0_ADDR	0x1fe0000f010UL
#define HBIRD_ESTAR_MODE_ADDR	0x1fe0000f080UL

/* UltraSPARC-IIe has five dividers: 1, 2, 4, 6, and 8.  These are controlled
 * in the ESTAR mode control register.
 */
#define ESTAR_MODE_DIV_1	0x0000000000000000UL
#define ESTAR_MODE_DIV_2	0x0000000000000001UL
#define ESTAR_MODE_DIV_4	0x0000000000000003UL
#define ESTAR_MODE_DIV_6	0x0000000000000002UL
#define ESTAR_MODE_DIV_8	0x0000000000000004UL
#define ESTAR_MODE_DIV_MASK	0x0000000000000007UL

#define MCTRL0_SREFRESH_ENAB	0x0000000000010000UL
#define MCTRL0_REFR_COUNT_MASK	0x0000000000007f00UL
#define MCTRL0_REFR_COUNT_SHIFT	8
#define MCTRL0_REFR_INTERVAL	7800
#define MCTRL0_REFR_CLKS_P_CNT	64

static unsigned long read_hbreg(unsigned long addr)
{
	unsigned long ret;

	__asm__ __volatile__("ldxa	[%1] %2, %0"
			     : "=&r" (ret)
			     : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E));
	return ret;
}

static void write_hbreg(unsigned long addr, unsigned long val)
{
	__asm__ __volatile__("stxa	%0, [%1] %2\n\t"
			     "membar	#Sync"
			     : /* no outputs */
			     : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E)
			     : "memory");
	if (addr == HBIRD_ESTAR_MODE_ADDR) {
		/* Need to wait 16 clock cycles for the PLL to lock.  */
		udelay(1);
	}
}

static void self_refresh_ctl(int enable)
{
	unsigned long mctrl = read_hbreg(HBIRD_MEM_CNTL0_ADDR);

	if (enable)
		mctrl |= MCTRL0_SREFRESH_ENAB;
	else
		mctrl &= ~MCTRL0_SREFRESH_ENAB;
	write_hbreg(HBIRD_MEM_CNTL0_ADDR, mctrl);
	(void) read_hbreg(HBIRD_MEM_CNTL0_ADDR);
}

static void frob_mem_refresh(int cpu_slowing_down,
			     unsigned long clock_tick,
			     unsigned long old_divisor, unsigned long divisor)
{
	unsigned long old_refr_count, refr_count, mctrl;

	refr_count  = (clock_tick * MCTRL0_REFR_INTERVAL);
	refr_count /= (MCTRL0_REFR_CLKS_P_CNT * divisor * 1000000000UL);

	mctrl = read_hbreg(HBIRD_MEM_CNTL0_ADDR);
	old_refr_count = (mctrl & MCTRL0_REFR_COUNT_MASK)
		>> MCTRL0_REFR_COUNT_SHIFT;

	mctrl &= ~MCTRL0_REFR_COUNT_MASK;
	mctrl |= refr_count << MCTRL0_REFR_COUNT_SHIFT;
	write_hbreg(HBIRD_MEM_CNTL0_ADDR, mctrl);
	mctrl = read_hbreg(HBIRD_MEM_CNTL0_ADDR);

	if (cpu_slowing_down && !(mctrl & MCTRL0_SREFRESH_ENAB)) {
		unsigned long usecs;

		/* We have to wait for both refresh counts (old
		 * and new) to go to zero.
		 */
		usecs = (MCTRL0_REFR_CLKS_P_CNT *
			 (refr_count + old_refr_count) *
			 1000000UL *
			 old_divisor) / clock_tick;
		udelay(usecs + 1UL);
	}
}

static void us2e_transition(unsigned long estar, unsigned long new_bits,
			    unsigned long clock_tick,
			    unsigned long old_divisor, unsigned long divisor)
{
	estar &= ~ESTAR_MODE_DIV_MASK;

	/* This is based upon the state transition diagram in the IIe manual.  */
	if (old_divisor == 2 && divisor == 1) {
		self_refresh_ctl(0);
		write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
		frob_mem_refresh(0, clock_tick, old_divisor, divisor);
	} else if (old_divisor == 1 && divisor == 2) {
		frob_mem_refresh(1, clock_tick, old_divisor, divisor);
		write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
		self_refresh_ctl(1);
	} else if (old_divisor == 1 && divisor > 2) {
		us2e_transition(estar, ESTAR_MODE_DIV_2, clock_tick,
				1, 2);
		us2e_transition(estar, new_bits, clock_tick,
				2, divisor);
	} else if (old_divisor > 2 && divisor == 1) {
		us2e_transition(estar, ESTAR_MODE_DIV_2, clock_tick,
				old_divisor, 2);
		us2e_transition(estar, new_bits, clock_tick,
				2, divisor);
	} else if (old_divisor < divisor) {
		frob_mem_refresh(0, clock_tick, old_divisor, divisor);
		write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
	} else if (old_divisor > divisor) {
		write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
		frob_mem_refresh(1, clock_tick, old_divisor, divisor);
	} else {
		BUG();
	}
}

static unsigned long index_to_estar_mode(unsigned int index)
{
	switch (index) {
	case 0:
		return ESTAR_MODE_DIV_1;

	case 1:
		return ESTAR_MODE_DIV_2;

	case 2:
		return ESTAR_MODE_DIV_4;

	case 3:
		return ESTAR_MODE_DIV_6;

	case 4:
		return ESTAR_MODE_DIV_8;

	default:
		BUG();
173
	}
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

static unsigned long index_to_divisor(unsigned int index)
{
	switch (index) {
	case 0:
		return 1;

	case 1:
		return 2;

	case 2:
		return 4;

	case 3:
		return 6;

	case 4:
		return 8;

	default:
		BUG();
196
	}
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
}

static unsigned long estar_to_divisor(unsigned long estar)
{
	unsigned long ret;

	switch (estar & ESTAR_MODE_DIV_MASK) {
	case ESTAR_MODE_DIV_1:
		ret = 1;
		break;
	case ESTAR_MODE_DIV_2:
		ret = 2;
		break;
	case ESTAR_MODE_DIV_4:
		ret = 4;
		break;
	case ESTAR_MODE_DIV_6:
		ret = 6;
		break;
	case ESTAR_MODE_DIV_8:
		ret = 8;
		break;
	default:
		BUG();
221
	}
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225

	return ret;
}

226 227 228 229 230 231 232
static void __us2e_freq_get(void *arg)
{
	unsigned long *estar = arg;

	*estar = read_hbreg(HBIRD_ESTAR_MODE_ADDR);
}

233 234 235 236 237
static unsigned int us2e_freq_get(unsigned int cpu)
{
	unsigned long clock_tick, estar;

	clock_tick = sparc64_get_clock_tick(cpu) / 1000;
238 239
	if (smp_call_function_single(cpu, __us2e_freq_get, &estar, 1))
		return 0;
240 241 242 243

	return clock_tick / estar_to_divisor(estar);
}

244
static void __us2e_freq_target(void *arg)
Linus Torvalds's avatar
Linus Torvalds committed
245
{
246 247
	unsigned int cpu = smp_processor_id();
	unsigned int *index = arg;
Linus Torvalds's avatar
Linus Torvalds committed
248 249 250
	unsigned long new_bits, new_freq;
	unsigned long clock_tick, divisor, old_divisor, estar;

251
	new_freq = clock_tick = sparc64_get_clock_tick(cpu) / 1000;
252 253
	new_bits = index_to_estar_mode(*index);
	divisor = index_to_divisor(*index);
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256 257 258 259
	new_freq /= divisor;

	estar = read_hbreg(HBIRD_ESTAR_MODE_ADDR);

	old_divisor = estar_to_divisor(estar);

260
	if (old_divisor != divisor) {
261 262
		us2e_transition(estar, new_bits, clock_tick * 1000,
				old_divisor, divisor);
263 264
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
265

266 267 268
static int us2e_freq_target(struct cpufreq_policy *policy, unsigned int index)
{
	unsigned int cpu = policy->cpu;
Linus Torvalds's avatar
Linus Torvalds committed
269

270
	return smp_call_function_single(cpu, __us2e_freq_target, &index, 1);
Linus Torvalds's avatar
Linus Torvalds committed
271 272 273 274 275
}

static int __init us2e_freq_cpu_init(struct cpufreq_policy *policy)
{
	unsigned int cpu = policy->cpu;
276
	unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000;
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279
	struct cpufreq_frequency_table *table =
		&us2e_freq_table[cpu].table[0];

280
	table[0].driver_data = 0;
Linus Torvalds's avatar
Linus Torvalds committed
281
	table[0].frequency = clock_tick / 1;
282
	table[1].driver_data = 1;
Linus Torvalds's avatar
Linus Torvalds committed
283
	table[1].frequency = clock_tick / 2;
284
	table[2].driver_data = 2;
Linus Torvalds's avatar
Linus Torvalds committed
285
	table[2].frequency = clock_tick / 4;
286
	table[2].driver_data = 3;
Linus Torvalds's avatar
Linus Torvalds committed
287
	table[2].frequency = clock_tick / 6;
288
	table[2].driver_data = 4;
Linus Torvalds's avatar
Linus Torvalds committed
289
	table[2].frequency = clock_tick / 8;
290
	table[2].driver_data = 5;
Linus Torvalds's avatar
Linus Torvalds committed
291 292 293 294
	table[3].frequency = CPUFREQ_TABLE_END;

	policy->cpuinfo.transition_latency = 0;
	policy->cur = clock_tick;
295
	policy->freq_table = table;
Linus Torvalds's avatar
Linus Torvalds committed
296

297
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
298 299 300 301
}

static int us2e_freq_cpu_exit(struct cpufreq_policy *policy)
{
302
	if (cpufreq_us2e_driver)
303
		us2e_freq_target(policy, 0);
Linus Torvalds's avatar
Linus Torvalds committed
304 305 306 307 308 309 310 311 312

	return 0;
}

static int __init us2e_freq_init(void)
{
	unsigned long manuf, impl, ver;
	int ret;

313 314 315
	if (tlb_type != spitfire)
		return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319 320 321 322 323
	__asm__("rdpr %%ver, %0" : "=r" (ver));
	manuf = ((ver >> 48) & 0xffff);
	impl  = ((ver >> 32) & 0xffff);

	if (manuf == 0x17 && impl == 0x13) {
		struct cpufreq_driver *driver;

		ret = -ENOMEM;
324
		driver = kzalloc(sizeof(*driver), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
325 326 327
		if (!driver)
			goto err_out;

328
		us2e_freq_table = kzalloc((NR_CPUS * sizeof(*us2e_freq_table)),
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332
			GFP_KERNEL);
		if (!us2e_freq_table)
			goto err_out;

333
		driver->init = us2e_freq_cpu_init;
334
		driver->verify = cpufreq_generic_frequency_table_verify;
335
		driver->target_index = us2e_freq_target;
336
		driver->get = us2e_freq_get;
Linus Torvalds's avatar
Linus Torvalds committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		driver->exit = us2e_freq_cpu_exit;
		strcpy(driver->name, "UltraSPARC-IIe");

		cpufreq_us2e_driver = driver;
		ret = cpufreq_register_driver(driver);
		if (ret)
			goto err_out;

		return 0;

err_out:
		if (driver) {
			kfree(driver);
			cpufreq_us2e_driver = NULL;
		}
Jesper Juhl's avatar
Jesper Juhl committed
352 353
		kfree(us2e_freq_table);
		us2e_freq_table = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		return ret;
	}

	return -ENODEV;
}

static void __exit us2e_freq_exit(void)
{
	if (cpufreq_us2e_driver) {
		cpufreq_unregister_driver(cpufreq_us2e_driver);
		kfree(cpufreq_us2e_driver);
		cpufreq_us2e_driver = NULL;
		kfree(us2e_freq_table);
		us2e_freq_table = NULL;
	}
}

MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-IIe");
MODULE_LICENSE("GPL");

module_init(us2e_freq_init);
module_exit(us2e_freq_exit);