Blame view

drivers/cpufreq/sa1110-cpufreq.c 8.83 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
  /*
   *  linux/arch/arm/mach-sa1100/cpu-sa1110.c
   *
   *  Copyright (C) 2001 Russell King
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
   * Note: there are two erratas that apply to the SA1110 here:
   *  7 - SDRAM auto-power-up failure (rev A0)
   * 13 - Corruption of internal register reads/writes following
   *      SDRAM reads (rev A0, B0, B1)
   *
   * We ignore rev. A0 and B0 devices; I don't think they're worth supporting.
ba5320118   Russell King   [ARM] Fix sa11x0 ...
13
14
   *
   * The SDRAM type can be passed on the command line as cpu_sa1110.sdram=type
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
18
  #include <linux/cpufreq.h>
  #include <linux/delay.h>
  #include <linux/init.h>
3169663ac   Russell King   ARM: sa11x0/pxa: ...
19
  #include <linux/io.h>
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
20
21
22
  #include <linux/kernel.h>
  #include <linux/moduleparam.h>
  #include <linux/types.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23

0ba8b9b27   Russell King   [ARM] cputype: se...
24
  #include <asm/cputype.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
  #include <asm/mach-types.h>
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
26

59a2e613d   Viresh Kumar   cpufreq: sa11x0: ...
27
  #include <mach/generic.h>
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
28
  #include <mach/hardware.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  #undef DEBUG
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
  struct sdram_params {
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
32
  	const char name[20];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  	u_char  rows;		/* bits				 */
  	u_char  cas_latency;	/* cycles			 */
  	u_char  tck;		/* clock cycle time (ns)	 */
  	u_char  trcd;		/* activate to r/w (ns)		 */
  	u_char  trp;		/* precharge to activate (ns)	 */
  	u_char  twr;		/* write recovery time (ns)	 */
  	u_short refresh;	/* refresh time for array (us)	 */
  };
  
  struct sdram_info {
  	u_int	mdcnfg;
  	u_int	mdrefr;
  	u_int	mdcas[3];
  };
ba5320118   Russell King   [ARM] Fix sa11x0 ...
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
  static struct sdram_params sdram_tbl[] __initdata = {
  	{	/* Toshiba TC59SM716 CL2 */
  		.name		= "TC59SM716-CL2",
  		.rows		= 12,
  		.tck		= 10,
  		.trcd		= 20,
  		.trp		= 20,
  		.twr		= 10,
  		.refresh	= 64000,
  		.cas_latency	= 2,
  	}, {	/* Toshiba TC59SM716 CL3 */
  		.name		= "TC59SM716-CL3",
  		.rows		= 12,
  		.tck		= 8,
  		.trcd		= 20,
  		.trp		= 20,
  		.twr		= 8,
  		.refresh	= 64000,
  		.cas_latency	= 3,
  	}, {	/* Samsung K4S641632D TC75 */
  		.name		= "K4S641632D",
  		.rows		= 14,
  		.tck		= 9,
  		.trcd		= 27,
  		.trp		= 20,
  		.twr		= 9,
  		.refresh	= 64000,
  		.cas_latency	= 3,
93982535a   Kristoffer Ericson   [ARM] 5336/1: For...
75
76
77
78
79
80
81
82
  	}, {	/* Samsung K4S281632B-1H */
  		.name           = "K4S281632B-1H",
  		.rows		= 12,
  		.tck		= 10,
  		.trp		= 20,
  		.twr		= 10,
  		.refresh	= 64000,
  		.cas_latency	= 3,
ba5320118   Russell King   [ARM] Fix sa11x0 ...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  	}, {	/* Samsung KM416S4030CT */
  		.name		= "KM416S4030CT",
  		.rows		= 13,
  		.tck		= 8,
  		.trcd		= 24,	/* 3 CLKs */
  		.trp		= 24,	/* 3 CLKs */
  		.twr		= 16,	/* Trdl: 2 CLKs */
  		.refresh	= 64000,
  		.cas_latency	= 3,
  	}, {	/* Winbond W982516AH75L CL3 */
  		.name		= "W982516AH75L",
  		.rows		= 16,
  		.tck		= 8,
  		.trcd		= 20,
  		.trp		= 20,
  		.twr		= 8,
  		.refresh	= 64000,
  		.cas_latency	= 3,
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
101
102
103
104
105
106
107
108
109
  	}, {	/* Micron MT48LC8M16A2TG-75 */
  		.name		= "MT48LC8M16A2TG-75",
  		.rows		= 12,
  		.tck		= 8,
  		.trcd		= 20,
  		.trp		= 20,
  		.twr		= 8,
  		.refresh	= 64000,
  		.cas_latency	= 3,
ba5320118   Russell King   [ARM] Fix sa11x0 ...
110
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  };
  
  static struct sdram_params sdram_params;
  
  /*
   * Given a period in ns and frequency in khz, calculate the number of
   * cycles of frequency in period.  Note that we round up to the next
   * cycle, even if we are only slightly over.
   */
  static inline u_int ns_to_cycles(u_int ns, u_int khz)
  {
  	return (ns * khz + 999999) / 1000000;
  }
  
  /*
   * Create the MDCAS register bit pattern.
   */
  static inline void set_mdcas(u_int *mdcas, int delayed, u_int rcd)
  {
  	u_int shift;
  
  	rcd = 2 * rcd - 1;
  	shift = delayed + 1 + rcd;
  
  	mdcas[0]  = (1 << rcd) - 1;
  	mdcas[0] |= 0x55555555 << shift;
  	mdcas[1]  = mdcas[2] = 0x55555555 << (shift & 1);
  }
  
  static void
  sdram_calculate_timing(struct sdram_info *sd, u_int cpu_khz,
  		       struct sdram_params *sdram)
  {
  	u_int mem_khz, sd_khz, trp, twr;
  
  	mem_khz = cpu_khz / 2;
  	sd_khz = mem_khz;
  
  	/*
  	 * If SDCLK would invalidate the SDRAM timings,
  	 * run SDCLK at half speed.
  	 *
  	 * CPU steppings prior to B2 must either run the memory at
  	 * half speed or use delayed read latching (errata 13).
  	 */
  	if ((ns_to_cycles(sdram->tck, sd_khz) > 1) ||
83809b90a   Russell King   ARM: sa1100: move...
157
  	    (read_cpuid_revision() < ARM_CPU_REV_SA1110_B2 && sd_khz < 62000))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  		sd_khz /= 2;
  
  	sd->mdcnfg = MDCNFG & 0x007f007f;
  
  	twr = ns_to_cycles(sdram->twr, mem_khz);
  
  	/* trp should always be >1 */
  	trp = ns_to_cycles(sdram->trp, mem_khz) - 1;
  	if (trp < 1)
  		trp = 1;
  
  	sd->mdcnfg |= trp << 8;
  	sd->mdcnfg |= trp << 24;
  	sd->mdcnfg |= sdram->cas_latency << 12;
  	sd->mdcnfg |= sdram->cas_latency << 28;
  	sd->mdcnfg |= twr << 14;
  	sd->mdcnfg |= twr << 30;
  
  	sd->mdrefr = MDREFR & 0xffbffff0;
  	sd->mdrefr |= 7;
  
  	if (sd_khz != mem_khz)
  		sd->mdrefr |= MDREFR_K1DB2;
  
  	/* initial number of '1's in MDCAS + 1 */
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
183
184
  	set_mdcas(sd->mdcas, sd_khz >= 62000,
  		ns_to_cycles(sdram->trcd, mem_khz));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
  
  #ifdef DEBUG
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
187
188
189
190
  	printk(KERN_DEBUG "MDCNFG: %08x MDREFR: %08x MDCAS0: %08x MDCAS1: %08x MDCAS2: %08x
  ",
  		sd->mdcnfg, sd->mdrefr, sd->mdcas[0], sd->mdcas[1],
  		sd->mdcas[2]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  #endif
  }
  
  /*
   * Set the SDRAM refresh rate.
   */
  static inline void sdram_set_refresh(u_int dri)
  {
  	MDREFR = (MDREFR & 0xffff000f) | (dri << 4);
  	(void) MDREFR;
  }
  
  /*
   * Update the refresh period.  We do this such that we always refresh
   * the SDRAMs within their permissible period.  The refresh period is
   * always a multiple of the memory clock (fixed at cpu_clock / 2).
   *
   * FIXME: we don't currently take account of burst accesses here,
   * but neither do Intels DM nor Angel.
   */
  static void
  sdram_update_refresh(u_int cpu_khz, struct sdram_params *sdram)
  {
  	u_int ns_row = (sdram->refresh * 1000) >> sdram->rows;
  	u_int dri = ns_to_cycles(ns_row, cpu_khz / 2) / 32;
  
  #ifdef DEBUG
  	mdelay(250);
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
219
220
  	printk(KERN_DEBUG "new dri value = %d
  ", dri);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
223
224
225
226
  #endif
  
  	sdram_set_refresh(dri);
  }
  
  /*
93982535a   Kristoffer Ericson   [ARM] 5336/1: For...
227
   * Ok, set the CPU frequency.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
   */
9c0ebcf78   Viresh Kumar   cpufreq: Implemen...
229
  static int sa1110_target(struct cpufreq_policy *policy, unsigned int ppcr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
  {
  	struct sdram_params *sdram = &sdram_params;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
  	struct sdram_info sd;
  	unsigned long flags;
9c0ebcf78   Viresh Kumar   cpufreq: Implemen...
234
  	unsigned int unused;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235

d4019f0a9   Viresh Kumar   cpufreq: move fre...
236
  	sdram_calculate_timing(&sd, sa11x0_freq_table[ppcr].frequency, sdram);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  
  #if 0
  	/*
  	 * These values are wrong according to the SA1110 documentation
  	 * and errata, but they seem to work.  Need to get a storage
  	 * scope on to the SDRAM signals to work out why.
  	 */
  	if (policy->max < 147500) {
  		sd.mdrefr |= MDREFR_K1DB2;
  		sd.mdcas[0] = 0xaaaaaa7f;
  	} else {
  		sd.mdrefr &= ~MDREFR_K1DB2;
  		sd.mdcas[0] = 0xaaaaaa9f;
  	}
  	sd.mdcas[1] = 0xaaaaaaaa;
  	sd.mdcas[2] = 0xaaaaaaaa;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
259
260
  	/*
  	 * The clock could be going away for some time.  Set the SDRAMs
  	 * to refresh rapidly (every 64 memory clock cycles).  To get
  	 * through the whole array, we need to wait 262144 mclk cycles.
  	 * We wait 20ms to be safe.
  	 */
  	sdram_set_refresh(2);
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
261
  	if (!irqs_disabled())
db5795547   Nishanth Aravamudan   [PATCH] ARM: repl...
262
  		msleep(20);
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
263
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  		mdelay(20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
270
271
272
273
274
  
  	/*
  	 * Reprogram the DRAM timings with interrupts disabled, and
  	 * ensure that we are doing this within a complete cache line.
  	 * This means that we won't access SDRAM for the duration of
  	 * the programming.
  	 */
  	local_irq_save(flags);
  	asm("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
  	udelay(10);
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
275
276
  	__asm__ __volatile__("
  \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  		b	2f					
  \
  		.align	5					
  \
  1:		str	%3, [%1, #0]		@ MDCNFG	
  \
  		str	%4, [%1, #28]		@ MDREFR	
  \
  		str	%5, [%1, #4]		@ MDCAS0	
  \
  		str	%6, [%1, #8]		@ MDCAS1	
  \
  		str	%7, [%1, #12]		@ MDCAS2	
  \
  		str	%8, [%2, #0]		@ PPCR		
  \
  		ldr	%0, [%1, #0]				
  \
  		b	3f					
  \
  2:		b	1b					
  \
  3:		nop						
  \
  		nop"
  		: "=&r" (unused)
  		: "r" (&MDCNFG), "r" (&PPCR), "0" (sd.mdcnfg),
  		  "r" (sd.mdrefr), "r" (sd.mdcas[0]),
  		  "r" (sd.mdcas[1]), "r" (sd.mdcas[2]), "r" (ppcr));
  	local_irq_restore(flags);
  
  	/*
  	 * Now, return the SDRAM refresh back to normal.
  	 */
d4019f0a9   Viresh Kumar   cpufreq: move fre...
311
  	sdram_update_refresh(sa11x0_freq_table[ppcr].frequency, sdram);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
314
315
316
317
  
  	return 0;
  }
  
  static int __init sa1110_cpu_init(struct cpufreq_policy *policy)
  {
c4dcc8a16   Viresh Kumar   cpufreq: Make cpu...
318
319
  	cpufreq_generic_init(policy, sa11x0_freq_table, 0);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
  }
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
321
322
323
  /* sa1110_driver needs __refdata because it must remain after init registers
   * it with cpufreq_register_driver() */
  static struct cpufreq_driver sa1110_driver __refdata = {
fe829ed8e   Viresh Kumar   cpufreq: Add CPUF...
324
325
  	.flags		= CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
  			  CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
dd9f26395   Viresh Kumar   cpufreq: sa11x0: ...
326
  	.verify		= cpufreq_generic_frequency_table_verify,
9c0ebcf78   Viresh Kumar   cpufreq: Implemen...
327
  	.target_index	= sa1110_target,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
  	.get		= sa11x0_getspeed,
  	.init		= sa1110_cpu_init,
  	.name		= "sa1110",
  };
ba5320118   Russell King   [ARM] Fix sa11x0 ...
332
333
334
  static struct sdram_params *sa1110_find_sdram(const char *name)
  {
  	struct sdram_params *sdram;
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
335
336
  	for (sdram = sdram_tbl; sdram < sdram_tbl + ARRAY_SIZE(sdram_tbl);
  	     sdram++)
ba5320118   Russell King   [ARM] Fix sa11x0 ...
337
338
339
340
341
342
343
  		if (strcmp(name, sdram->name) == 0)
  			return sdram;
  
  	return NULL;
  }
  
  static char sdram_name[16];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
  static int __init sa1110_clk_init(void)
  {
ba5320118   Russell King   [ARM] Fix sa11x0 ...
346
347
  	struct sdram_params *sdram;
  	const char *name = sdram_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348

e5992c05f   Dmitry Artamonow   ARM: 6076/1: SA11...
349
350
  	if (!cpu_is_sa1110())
  		return -ENODEV;
ba5320118   Russell King   [ARM] Fix sa11x0 ...
351
352
353
  	if (!name[0]) {
  		if (machine_is_assabet())
  			name = "TC59SM716-CL3";
ba5320118   Russell King   [ARM] Fix sa11x0 ...
354
355
  		if (machine_is_pt_system3())
  			name = "K4S641632D";
ba5320118   Russell King   [ARM] Fix sa11x0 ...
356
357
  		if (machine_is_h3100())
  			name = "KM416S4030CT";
97d496bf1   Linus Walleij   cpufreq: sa1110: ...
358
  		if (machine_is_jornada720() || machine_is_h3600())
47bb3b31a   Marcelo Roberto Jimenez   ARM: 6451/1: sa11...
359
  			name = "K4S281632B-1H";
9f15d2cac   Marcelo Roberto Jimenez   ARM: 6447/3: sa11...
360
361
  		if (machine_is_nanoengine())
  			name = "MT48LC8M16A2TG-75";
ba5320118   Russell King   [ARM] Fix sa11x0 ...
362
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363

ba5320118   Russell King   [ARM] Fix sa11x0 ...
364
  	sdram = sa1110_find_sdram(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  	if (sdram) {
  		printk(KERN_DEBUG "SDRAM: tck: %d trcd: %d trp: %d"
  			" twr: %d refresh: %d cas_latency: %d
  ",
  			sdram->tck, sdram->trcd, sdram->trp,
  			sdram->twr, sdram->refresh, sdram->cas_latency);
  
  		memcpy(&sdram_params, sdram, sizeof(sdram_params));
  
  		return cpufreq_register_driver(&sa1110_driver);
  	}
  
  	return 0;
  }
ba5320118   Russell King   [ARM] Fix sa11x0 ...
379
  module_param_string(sdram, sdram_name, sizeof(sdram_name), 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
  arch_initcall(sa1110_clk_init);