From 3c0e0436d831843b1dd5d857327a735345e63eab Mon Sep 17 00:00:00 2001
From: Thomas Chou <thomas@wytron.com.tw>
Date: Wed, 20 Feb 2008 11:10:18 +0800
Subject: [PATCH] nios2: system timer input clock frequency

frd66 identified a bug that the frequency declared in nios2_system.h was
wrong, which was the external one that go to the input of in-system PLL.
He submitted a patch to use internal cpu clock.

But the system timer input does not always come from cpu clock. I
refined his patch and look for the real clock input to the system timer.

na_cpu_clock_freq is added to display correct /proc/cpuinfo.

Signed-off-by: Thomas Chou <thomas@wytron.com.tw>
---
 linux-2.6.x/arch/nios2nommu/kernel/setup.c         |    4 ++--
 .../arch/nios2nommu/scripts/PTF/SystemPTF.pm       |   16 +++++++++++++---
 .../arch/nios2nommu/scripts/gen_nios2_system.h.pl  |    5 +----
 .../scripts/nios2_system.h/altera_avalon_timer.pm  |    6 ++++++
 4 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/linux-2.6.x/arch/nios2nommu/kernel/setup.c b/linux-2.6.x/arch/nios2nommu/kernel/setup.c
index c772f79..9bf1a07 100644
--- a/linux-2.6.x/arch/nios2nommu/kernel/setup.c
+++ b/linux-2.6.x/arch/nios2nommu/kernel/setup.c
@@ -354,7 +354,7 @@ int get_cpuinfo(char * buffer)
     mmu = "none";
     fpu = "none";
 
-    clockfreq = nasys_clock_freq;
+    clockfreq = na_cpu_clock_freq;
 
     return(sprintf(buffer, "CPU:\t\t%s\n"
 		   "MMU:\t\t%s\n"
@@ -382,7 +382,7 @@ static int show_cpuinfo(struct seq_file *m, void *v)
     mmu = "none";
     fpu = "none";
 
-    clockfreq = nasys_clock_freq;
+    clockfreq = na_cpu_clock_freq;
 
     seq_printf(m, "CPU:\t\t%s\n"
 		   "MMU:\t\t%s\n"
diff --git a/linux-2.6.x/arch/nios2nommu/scripts/PTF/SystemPTF.pm b/linux-2.6.x/arch/nios2nommu/scripts/PTF/SystemPTF.pm
index 9f44cfe..4ca77df 100644
--- a/linux-2.6.x/arch/nios2nommu/scripts/PTF/SystemPTF.pm
+++ b/linux-2.6.x/arch/nios2nommu/scripts/PTF/SystemPTF.pm
@@ -124,13 +124,23 @@ sub getSlaveModules {
 	return sort module_comparison keys (%connected); 
 }
 
+# get the clock frequency of a module
 sub getClockFreq () {
-	my ($self) = @_;
+	my ($self, $name) = @_;
 	
+	my $module = $self->{root}->getSection ('MODULE', $name);
+	$module or return;
+	my $sbi = $module->getSection ('SYSTEM_BUILDER_INFO', '');
+	$sbi or return;
+	my $clk_src = $sbi->getAssignment ('Clock_Source');
+	$clk_src or return;
 	my $wsa = $self->{root}->getSection ('WIZARD_SCRIPT_ARGUMENTS', '');
 	$wsa or return;
-	
-	my $result = $wsa->getAssignment ('clock_freq');
+	my $clocks = $wsa->getSection ('CLOCKS', '');
+	$clocks or return;
+	my $clock = $clocks->getSection ('CLOCK', $clk_src);
+	$clock or return;
+	my $result = $clock->getAssignment ('frequency');
 	return $result;
 }
 
diff --git a/linux-2.6.x/arch/nios2nommu/scripts/gen_nios2_system.h.pl b/linux-2.6.x/arch/nios2nommu/scripts/gen_nios2_system.h.pl
index 4394c74..c8c190a 100644
--- a/linux-2.6.x/arch/nios2nommu/scripts/gen_nios2_system.h.pl
+++ b/linux-2.6.x/arch/nios2nommu/scripts/gen_nios2_system.h.pl
@@ -287,11 +287,8 @@ if (!$upload_location || ($upload_location eq "flash_kernel")) {
 }
 
 print "\n";
-
-printf ("#define %-33s %30s\n", 
-	("nasys_clock_freq", $system->getClockFreq()));
 printf ("#define %-33s %30s\n", 
-	("nasys_clock_freq_1000", int ($system->getClockFreq()) / 1000));
+	("na_cpu_clock_freq", $system->getClockFreq($target_cpu)));
 	
 {	
 	my ($reset_location, $reset_offset) = $cpu->getResetLocationOffset();
diff --git a/linux-2.6.x/arch/nios2nommu/scripts/nios2_system.h/altera_avalon_timer.pm b/linux-2.6.x/arch/nios2nommu/scripts/nios2_system.h/altera_avalon_timer.pm
index 495ccdc..dc01e4f 100644
--- a/linux-2.6.x/arch/nios2nommu/scripts/nios2_system.h/altera_avalon_timer.pm
+++ b/linux-2.6.x/arch/nios2nommu/scripts/nios2_system.h/altera_avalon_timer.pm
@@ -33,6 +33,12 @@ sub is_module_valid {
 	my $fixed_period = $module->getWSAAssignment ('fixed_period');	
 
 	if ($fixed_period eq '0') {
+	    printf ("\n");
+	    printf ("/* system timer input clock frequency */\n");
+	    printf ("#define %-33s %30s\n", 
+		    ("nasys_clock_freq", $system->getClockFreq($module_name)));
+	    printf ("#define %-33s %30s\n", 
+		    ("nasys_clock_freq_1000", int ($system->getClockFreq($module_name)) / 1000));
 		return 1;
 	} else {
 		return 0;
-- 
1.5.3.3

