1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
259a2e613SViresh Kumar /*
359a2e613SViresh Kumar * linux/arch/arm/mach-sa1100/cpu-sa1110.c
459a2e613SViresh Kumar *
559a2e613SViresh Kumar * Copyright (C) 2001 Russell King
659a2e613SViresh Kumar *
759a2e613SViresh Kumar * Note: there are two erratas that apply to the SA1110 here:
859a2e613SViresh Kumar * 7 - SDRAM auto-power-up failure (rev A0)
959a2e613SViresh Kumar * 13 - Corruption of internal register reads/writes following
1059a2e613SViresh Kumar * SDRAM reads (rev A0, B0, B1)
1159a2e613SViresh Kumar *
1259a2e613SViresh Kumar * We ignore rev. A0 and B0 devices; I don't think they're worth supporting.
1359a2e613SViresh Kumar *
1459a2e613SViresh Kumar * The SDRAM type can be passed on the command line as cpu_sa1110.sdram=type
1559a2e613SViresh Kumar */
1659a2e613SViresh Kumar #include <linux/cpufreq.h>
1759a2e613SViresh Kumar #include <linux/delay.h>
1859a2e613SViresh Kumar #include <linux/init.h>
1959a2e613SViresh Kumar #include <linux/io.h>
2059a2e613SViresh Kumar #include <linux/kernel.h>
2159a2e613SViresh Kumar #include <linux/moduleparam.h>
2259a2e613SViresh Kumar #include <linux/types.h>
2359a2e613SViresh Kumar
2459a2e613SViresh Kumar #include <asm/cputype.h>
2559a2e613SViresh Kumar #include <asm/mach-types.h>
2659a2e613SViresh Kumar
2759a2e613SViresh Kumar #include <mach/generic.h>
2859a2e613SViresh Kumar #include <mach/hardware.h>
2959a2e613SViresh Kumar
3059a2e613SViresh Kumar #undef DEBUG
3159a2e613SViresh Kumar
3259a2e613SViresh Kumar struct sdram_params {
3359a2e613SViresh Kumar const char name[20];
3459a2e613SViresh Kumar u_char rows; /* bits */
3559a2e613SViresh Kumar u_char cas_latency; /* cycles */
3659a2e613SViresh Kumar u_char tck; /* clock cycle time (ns) */
3759a2e613SViresh Kumar u_char trcd; /* activate to r/w (ns) */
3859a2e613SViresh Kumar u_char trp; /* precharge to activate (ns) */
3959a2e613SViresh Kumar u_char twr; /* write recovery time (ns) */
4059a2e613SViresh Kumar u_short refresh; /* refresh time for array (us) */
4159a2e613SViresh Kumar };
4259a2e613SViresh Kumar
4359a2e613SViresh Kumar struct sdram_info {
4459a2e613SViresh Kumar u_int mdcnfg;
4559a2e613SViresh Kumar u_int mdrefr;
4659a2e613SViresh Kumar u_int mdcas[3];
4759a2e613SViresh Kumar };
4859a2e613SViresh Kumar
4959a2e613SViresh Kumar static struct sdram_params sdram_tbl[] __initdata = {
5059a2e613SViresh Kumar { /* Toshiba TC59SM716 CL2 */
5159a2e613SViresh Kumar .name = "TC59SM716-CL2",
5259a2e613SViresh Kumar .rows = 12,
5359a2e613SViresh Kumar .tck = 10,
5459a2e613SViresh Kumar .trcd = 20,
5559a2e613SViresh Kumar .trp = 20,
5659a2e613SViresh Kumar .twr = 10,
5759a2e613SViresh Kumar .refresh = 64000,
5859a2e613SViresh Kumar .cas_latency = 2,
5959a2e613SViresh Kumar }, { /* Toshiba TC59SM716 CL3 */
6059a2e613SViresh Kumar .name = "TC59SM716-CL3",
6159a2e613SViresh Kumar .rows = 12,
6259a2e613SViresh Kumar .tck = 8,
6359a2e613SViresh Kumar .trcd = 20,
6459a2e613SViresh Kumar .trp = 20,
6559a2e613SViresh Kumar .twr = 8,
6659a2e613SViresh Kumar .refresh = 64000,
6759a2e613SViresh Kumar .cas_latency = 3,
6859a2e613SViresh Kumar }, { /* Samsung K4S641632D TC75 */
6959a2e613SViresh Kumar .name = "K4S641632D",
7059a2e613SViresh Kumar .rows = 14,
7159a2e613SViresh Kumar .tck = 9,
7259a2e613SViresh Kumar .trcd = 27,
7359a2e613SViresh Kumar .trp = 20,
7459a2e613SViresh Kumar .twr = 9,
7559a2e613SViresh Kumar .refresh = 64000,
7659a2e613SViresh Kumar .cas_latency = 3,
7759a2e613SViresh Kumar }, { /* Samsung K4S281632B-1H */
7859a2e613SViresh Kumar .name = "K4S281632B-1H",
7959a2e613SViresh Kumar .rows = 12,
8059a2e613SViresh Kumar .tck = 10,
8159a2e613SViresh Kumar .trp = 20,
8259a2e613SViresh Kumar .twr = 10,
8359a2e613SViresh Kumar .refresh = 64000,
8459a2e613SViresh Kumar .cas_latency = 3,
8559a2e613SViresh Kumar }, { /* Samsung KM416S4030CT */
8659a2e613SViresh Kumar .name = "KM416S4030CT",
8759a2e613SViresh Kumar .rows = 13,
8859a2e613SViresh Kumar .tck = 8,
8959a2e613SViresh Kumar .trcd = 24, /* 3 CLKs */
9059a2e613SViresh Kumar .trp = 24, /* 3 CLKs */
9159a2e613SViresh Kumar .twr = 16, /* Trdl: 2 CLKs */
9259a2e613SViresh Kumar .refresh = 64000,
9359a2e613SViresh Kumar .cas_latency = 3,
9459a2e613SViresh Kumar }, { /* Winbond W982516AH75L CL3 */
9559a2e613SViresh Kumar .name = "W982516AH75L",
9659a2e613SViresh Kumar .rows = 16,
9759a2e613SViresh Kumar .tck = 8,
9859a2e613SViresh Kumar .trcd = 20,
9959a2e613SViresh Kumar .trp = 20,
10059a2e613SViresh Kumar .twr = 8,
10159a2e613SViresh Kumar .refresh = 64000,
10259a2e613SViresh Kumar .cas_latency = 3,
10359a2e613SViresh Kumar }, { /* Micron MT48LC8M16A2TG-75 */
10459a2e613SViresh Kumar .name = "MT48LC8M16A2TG-75",
10559a2e613SViresh Kumar .rows = 12,
10659a2e613SViresh Kumar .tck = 8,
10759a2e613SViresh Kumar .trcd = 20,
10859a2e613SViresh Kumar .trp = 20,
10959a2e613SViresh Kumar .twr = 8,
11059a2e613SViresh Kumar .refresh = 64000,
11159a2e613SViresh Kumar .cas_latency = 3,
11259a2e613SViresh Kumar },
11359a2e613SViresh Kumar };
11459a2e613SViresh Kumar
11559a2e613SViresh Kumar static struct sdram_params sdram_params;
11659a2e613SViresh Kumar
11759a2e613SViresh Kumar /*
11859a2e613SViresh Kumar * Given a period in ns and frequency in khz, calculate the number of
11959a2e613SViresh Kumar * cycles of frequency in period. Note that we round up to the next
12059a2e613SViresh Kumar * cycle, even if we are only slightly over.
12159a2e613SViresh Kumar */
ns_to_cycles(u_int ns,u_int khz)12259a2e613SViresh Kumar static inline u_int ns_to_cycles(u_int ns, u_int khz)
12359a2e613SViresh Kumar {
12459a2e613SViresh Kumar return (ns * khz + 999999) / 1000000;
12559a2e613SViresh Kumar }
12659a2e613SViresh Kumar
12759a2e613SViresh Kumar /*
12859a2e613SViresh Kumar * Create the MDCAS register bit pattern.
12959a2e613SViresh Kumar */
set_mdcas(u_int * mdcas,int delayed,u_int rcd)13059a2e613SViresh Kumar static inline void set_mdcas(u_int *mdcas, int delayed, u_int rcd)
13159a2e613SViresh Kumar {
13259a2e613SViresh Kumar u_int shift;
13359a2e613SViresh Kumar
13459a2e613SViresh Kumar rcd = 2 * rcd - 1;
13559a2e613SViresh Kumar shift = delayed + 1 + rcd;
13659a2e613SViresh Kumar
13759a2e613SViresh Kumar mdcas[0] = (1 << rcd) - 1;
13859a2e613SViresh Kumar mdcas[0] |= 0x55555555 << shift;
13959a2e613SViresh Kumar mdcas[1] = mdcas[2] = 0x55555555 << (shift & 1);
14059a2e613SViresh Kumar }
14159a2e613SViresh Kumar
14259a2e613SViresh Kumar static void
sdram_calculate_timing(struct sdram_info * sd,u_int cpu_khz,struct sdram_params * sdram)14359a2e613SViresh Kumar sdram_calculate_timing(struct sdram_info *sd, u_int cpu_khz,
14459a2e613SViresh Kumar struct sdram_params *sdram)
14559a2e613SViresh Kumar {
14659a2e613SViresh Kumar u_int mem_khz, sd_khz, trp, twr;
14759a2e613SViresh Kumar
14859a2e613SViresh Kumar mem_khz = cpu_khz / 2;
14959a2e613SViresh Kumar sd_khz = mem_khz;
15059a2e613SViresh Kumar
15159a2e613SViresh Kumar /*
15259a2e613SViresh Kumar * If SDCLK would invalidate the SDRAM timings,
15359a2e613SViresh Kumar * run SDCLK at half speed.
15459a2e613SViresh Kumar *
15559a2e613SViresh Kumar * CPU steppings prior to B2 must either run the memory at
15659a2e613SViresh Kumar * half speed or use delayed read latching (errata 13).
15759a2e613SViresh Kumar */
15859a2e613SViresh Kumar if ((ns_to_cycles(sdram->tck, sd_khz) > 1) ||
15983809b90SRussell King (read_cpuid_revision() < ARM_CPU_REV_SA1110_B2 && sd_khz < 62000))
16059a2e613SViresh Kumar sd_khz /= 2;
16159a2e613SViresh Kumar
16259a2e613SViresh Kumar sd->mdcnfg = MDCNFG & 0x007f007f;
16359a2e613SViresh Kumar
16459a2e613SViresh Kumar twr = ns_to_cycles(sdram->twr, mem_khz);
16559a2e613SViresh Kumar
16659a2e613SViresh Kumar /* trp should always be >1 */
16759a2e613SViresh Kumar trp = ns_to_cycles(sdram->trp, mem_khz) - 1;
16859a2e613SViresh Kumar if (trp < 1)
16959a2e613SViresh Kumar trp = 1;
17059a2e613SViresh Kumar
17159a2e613SViresh Kumar sd->mdcnfg |= trp << 8;
17259a2e613SViresh Kumar sd->mdcnfg |= trp << 24;
17359a2e613SViresh Kumar sd->mdcnfg |= sdram->cas_latency << 12;
17459a2e613SViresh Kumar sd->mdcnfg |= sdram->cas_latency << 28;
17559a2e613SViresh Kumar sd->mdcnfg |= twr << 14;
17659a2e613SViresh Kumar sd->mdcnfg |= twr << 30;
17759a2e613SViresh Kumar
17859a2e613SViresh Kumar sd->mdrefr = MDREFR & 0xffbffff0;
17959a2e613SViresh Kumar sd->mdrefr |= 7;
18059a2e613SViresh Kumar
18159a2e613SViresh Kumar if (sd_khz != mem_khz)
18259a2e613SViresh Kumar sd->mdrefr |= MDREFR_K1DB2;
18359a2e613SViresh Kumar
18459a2e613SViresh Kumar /* initial number of '1's in MDCAS + 1 */
18559a2e613SViresh Kumar set_mdcas(sd->mdcas, sd_khz >= 62000,
18659a2e613SViresh Kumar ns_to_cycles(sdram->trcd, mem_khz));
18759a2e613SViresh Kumar
18859a2e613SViresh Kumar #ifdef DEBUG
18959a2e613SViresh Kumar printk(KERN_DEBUG "MDCNFG: %08x MDREFR: %08x MDCAS0: %08x MDCAS1: %08x MDCAS2: %08x\n",
19059a2e613SViresh Kumar sd->mdcnfg, sd->mdrefr, sd->mdcas[0], sd->mdcas[1],
19159a2e613SViresh Kumar sd->mdcas[2]);
19259a2e613SViresh Kumar #endif
19359a2e613SViresh Kumar }
19459a2e613SViresh Kumar
19559a2e613SViresh Kumar /*
19659a2e613SViresh Kumar * Set the SDRAM refresh rate.
19759a2e613SViresh Kumar */
sdram_set_refresh(u_int dri)19859a2e613SViresh Kumar static inline void sdram_set_refresh(u_int dri)
19959a2e613SViresh Kumar {
20059a2e613SViresh Kumar MDREFR = (MDREFR & 0xffff000f) | (dri << 4);
20159a2e613SViresh Kumar (void) MDREFR;
20259a2e613SViresh Kumar }
20359a2e613SViresh Kumar
20459a2e613SViresh Kumar /*
20559a2e613SViresh Kumar * Update the refresh period. We do this such that we always refresh
20659a2e613SViresh Kumar * the SDRAMs within their permissible period. The refresh period is
20759a2e613SViresh Kumar * always a multiple of the memory clock (fixed at cpu_clock / 2).
20859a2e613SViresh Kumar *
20959a2e613SViresh Kumar * FIXME: we don't currently take account of burst accesses here,
21059a2e613SViresh Kumar * but neither do Intels DM nor Angel.
21159a2e613SViresh Kumar */
21259a2e613SViresh Kumar static void
sdram_update_refresh(u_int cpu_khz,struct sdram_params * sdram)21359a2e613SViresh Kumar sdram_update_refresh(u_int cpu_khz, struct sdram_params *sdram)
21459a2e613SViresh Kumar {
21559a2e613SViresh Kumar u_int ns_row = (sdram->refresh * 1000) >> sdram->rows;
21659a2e613SViresh Kumar u_int dri = ns_to_cycles(ns_row, cpu_khz / 2) / 32;
21759a2e613SViresh Kumar
21859a2e613SViresh Kumar #ifdef DEBUG
21959a2e613SViresh Kumar mdelay(250);
22059a2e613SViresh Kumar printk(KERN_DEBUG "new dri value = %d\n", dri);
22159a2e613SViresh Kumar #endif
22259a2e613SViresh Kumar
22359a2e613SViresh Kumar sdram_set_refresh(dri);
22459a2e613SViresh Kumar }
22559a2e613SViresh Kumar
22659a2e613SViresh Kumar /*
22759a2e613SViresh Kumar * Ok, set the CPU frequency.
22859a2e613SViresh Kumar */
sa1110_target(struct cpufreq_policy * policy,unsigned int ppcr)2299c0ebcf7SViresh Kumar static int sa1110_target(struct cpufreq_policy *policy, unsigned int ppcr)
23059a2e613SViresh Kumar {
23159a2e613SViresh Kumar struct sdram_params *sdram = &sdram_params;
23259a2e613SViresh Kumar struct sdram_info sd;
23359a2e613SViresh Kumar unsigned long flags;
2349c0ebcf7SViresh Kumar unsigned int unused;
23559a2e613SViresh Kumar
236d4019f0aSViresh Kumar sdram_calculate_timing(&sd, sa11x0_freq_table[ppcr].frequency, sdram);
23759a2e613SViresh Kumar
23859a2e613SViresh Kumar #if 0
23959a2e613SViresh Kumar /*
24059a2e613SViresh Kumar * These values are wrong according to the SA1110 documentation
24159a2e613SViresh Kumar * and errata, but they seem to work. Need to get a storage
24259a2e613SViresh Kumar * scope on to the SDRAM signals to work out why.
24359a2e613SViresh Kumar */
24459a2e613SViresh Kumar if (policy->max < 147500) {
24559a2e613SViresh Kumar sd.mdrefr |= MDREFR_K1DB2;
24659a2e613SViresh Kumar sd.mdcas[0] = 0xaaaaaa7f;
24759a2e613SViresh Kumar } else {
24859a2e613SViresh Kumar sd.mdrefr &= ~MDREFR_K1DB2;
24959a2e613SViresh Kumar sd.mdcas[0] = 0xaaaaaa9f;
25059a2e613SViresh Kumar }
25159a2e613SViresh Kumar sd.mdcas[1] = 0xaaaaaaaa;
25259a2e613SViresh Kumar sd.mdcas[2] = 0xaaaaaaaa;
25359a2e613SViresh Kumar #endif
25459a2e613SViresh Kumar
25559a2e613SViresh Kumar /*
25659a2e613SViresh Kumar * The clock could be going away for some time. Set the SDRAMs
25759a2e613SViresh Kumar * to refresh rapidly (every 64 memory clock cycles). To get
25859a2e613SViresh Kumar * through the whole array, we need to wait 262144 mclk cycles.
25959a2e613SViresh Kumar * We wait 20ms to be safe.
26059a2e613SViresh Kumar */
26159a2e613SViresh Kumar sdram_set_refresh(2);
26259a2e613SViresh Kumar if (!irqs_disabled())
26359a2e613SViresh Kumar msleep(20);
26459a2e613SViresh Kumar else
26559a2e613SViresh Kumar mdelay(20);
26659a2e613SViresh Kumar
26759a2e613SViresh Kumar /*
26859a2e613SViresh Kumar * Reprogram the DRAM timings with interrupts disabled, and
26959a2e613SViresh Kumar * ensure that we are doing this within a complete cache line.
27059a2e613SViresh Kumar * This means that we won't access SDRAM for the duration of
27159a2e613SViresh Kumar * the programming.
27259a2e613SViresh Kumar */
27359a2e613SViresh Kumar local_irq_save(flags);
27459a2e613SViresh Kumar asm("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
27559a2e613SViresh Kumar udelay(10);
27659a2e613SViresh Kumar __asm__ __volatile__("\n\
27759a2e613SViresh Kumar b 2f \n\
27859a2e613SViresh Kumar .align 5 \n\
27959a2e613SViresh Kumar 1: str %3, [%1, #0] @ MDCNFG \n\
28059a2e613SViresh Kumar str %4, [%1, #28] @ MDREFR \n\
28159a2e613SViresh Kumar str %5, [%1, #4] @ MDCAS0 \n\
28259a2e613SViresh Kumar str %6, [%1, #8] @ MDCAS1 \n\
28359a2e613SViresh Kumar str %7, [%1, #12] @ MDCAS2 \n\
28459a2e613SViresh Kumar str %8, [%2, #0] @ PPCR \n\
28559a2e613SViresh Kumar ldr %0, [%1, #0] \n\
28659a2e613SViresh Kumar b 3f \n\
28759a2e613SViresh Kumar 2: b 1b \n\
28859a2e613SViresh Kumar 3: nop \n\
28959a2e613SViresh Kumar nop"
29059a2e613SViresh Kumar : "=&r" (unused)
29159a2e613SViresh Kumar : "r" (&MDCNFG), "r" (&PPCR), "0" (sd.mdcnfg),
29259a2e613SViresh Kumar "r" (sd.mdrefr), "r" (sd.mdcas[0]),
29359a2e613SViresh Kumar "r" (sd.mdcas[1]), "r" (sd.mdcas[2]), "r" (ppcr));
29459a2e613SViresh Kumar local_irq_restore(flags);
29559a2e613SViresh Kumar
29659a2e613SViresh Kumar /*
29759a2e613SViresh Kumar * Now, return the SDRAM refresh back to normal.
29859a2e613SViresh Kumar */
299d4019f0aSViresh Kumar sdram_update_refresh(sa11x0_freq_table[ppcr].frequency, sdram);
30059a2e613SViresh Kumar
30159a2e613SViresh Kumar return 0;
30259a2e613SViresh Kumar }
30359a2e613SViresh Kumar
sa1110_cpu_init(struct cpufreq_policy * policy)30459a2e613SViresh Kumar static int __init sa1110_cpu_init(struct cpufreq_policy *policy)
30559a2e613SViresh Kumar {
306c4dcc8a1SViresh Kumar cpufreq_generic_init(policy, sa11x0_freq_table, 0);
307c4dcc8a1SViresh Kumar return 0;
30859a2e613SViresh Kumar }
30959a2e613SViresh Kumar
31059a2e613SViresh Kumar /* sa1110_driver needs __refdata because it must remain after init registers
31159a2e613SViresh Kumar * it with cpufreq_register_driver() */
31259a2e613SViresh Kumar static struct cpufreq_driver sa1110_driver __refdata = {
313*5ae4a4b4SViresh Kumar .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
314fe829ed8SViresh Kumar CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
315dd9f2639SViresh Kumar .verify = cpufreq_generic_frequency_table_verify,
3169c0ebcf7SViresh Kumar .target_index = sa1110_target,
31759a2e613SViresh Kumar .get = sa11x0_getspeed,
31859a2e613SViresh Kumar .init = sa1110_cpu_init,
31959a2e613SViresh Kumar .name = "sa1110",
32059a2e613SViresh Kumar };
32159a2e613SViresh Kumar
sa1110_find_sdram(const char * name)32259a2e613SViresh Kumar static struct sdram_params *sa1110_find_sdram(const char *name)
32359a2e613SViresh Kumar {
32459a2e613SViresh Kumar struct sdram_params *sdram;
32559a2e613SViresh Kumar
32659a2e613SViresh Kumar for (sdram = sdram_tbl; sdram < sdram_tbl + ARRAY_SIZE(sdram_tbl);
32759a2e613SViresh Kumar sdram++)
32859a2e613SViresh Kumar if (strcmp(name, sdram->name) == 0)
32959a2e613SViresh Kumar return sdram;
33059a2e613SViresh Kumar
33159a2e613SViresh Kumar return NULL;
33259a2e613SViresh Kumar }
33359a2e613SViresh Kumar
33459a2e613SViresh Kumar static char sdram_name[16];
33559a2e613SViresh Kumar
sa1110_clk_init(void)33659a2e613SViresh Kumar static int __init sa1110_clk_init(void)
33759a2e613SViresh Kumar {
33859a2e613SViresh Kumar struct sdram_params *sdram;
33959a2e613SViresh Kumar const char *name = sdram_name;
34059a2e613SViresh Kumar
34159a2e613SViresh Kumar if (!cpu_is_sa1110())
34259a2e613SViresh Kumar return -ENODEV;
34359a2e613SViresh Kumar
34459a2e613SViresh Kumar if (!name[0]) {
34559a2e613SViresh Kumar if (machine_is_assabet())
34659a2e613SViresh Kumar name = "TC59SM716-CL3";
34797d496bfSLinus Walleij if (machine_is_jornada720() || machine_is_h3600())
34859a2e613SViresh Kumar name = "K4S281632B-1H";
34959a2e613SViresh Kumar }
35059a2e613SViresh Kumar
35159a2e613SViresh Kumar sdram = sa1110_find_sdram(name);
35259a2e613SViresh Kumar if (sdram) {
35359a2e613SViresh Kumar printk(KERN_DEBUG "SDRAM: tck: %d trcd: %d trp: %d"
35459a2e613SViresh Kumar " twr: %d refresh: %d cas_latency: %d\n",
35559a2e613SViresh Kumar sdram->tck, sdram->trcd, sdram->trp,
35659a2e613SViresh Kumar sdram->twr, sdram->refresh, sdram->cas_latency);
35759a2e613SViresh Kumar
35859a2e613SViresh Kumar memcpy(&sdram_params, sdram, sizeof(sdram_params));
35959a2e613SViresh Kumar
36059a2e613SViresh Kumar return cpufreq_register_driver(&sa1110_driver);
36159a2e613SViresh Kumar }
36259a2e613SViresh Kumar
36359a2e613SViresh Kumar return 0;
36459a2e613SViresh Kumar }
36559a2e613SViresh Kumar
36659a2e613SViresh Kumar module_param_string(sdram, sdram_name, sizeof(sdram_name), 0);
36759a2e613SViresh Kumar arch_initcall(sa1110_clk_init);
368