Received: from mail.onstor.com (66.201.51.107) by exch1.onstor.net
 (10.0.0.225) with Microsoft SMTP Server id 8.1.311.2; Sun, 21 Dec 2008
 00:30:10 -0800
Received: from ausesmta2-1.messageone.com ([64.20.241.45]) by mail.onstor.com
 with Microsoft SMTPSVC(6.0.3790.3959);	 Sun, 21 Dec 2008 00:30:10 -0800
Received: from ftp.linux-mips.org (ftp.linux-mips.org [213.58.128.207])	by
 ausesmta2-1.messageone.com (8.13.8/8.13.8) with ESMTP id mBL8U9fX003098	for
 <andy.sharp@onstor.com>; Sun, 21 Dec 2008 02:30:09 -0600
Received: from localhost.localdomain ([127.0.0.1]:7085 "EHLO
	ftp.linux-mips.org") by ftp.linux-mips.org with ESMTP	id S24126022AbYLUIaF
 (ORCPT <rfc822;andy.sharp@onstor.com>);	Sun, 21 Dec 2008 08:30:05 +0000
Received: with ECARTIS (v1.0.0; list linux-mips); Sun, 21 Dec 2008 08:29:48
 +0000 (GMT)
Received: from fnoeppeil48.netpark.at ([217.175.205.176]:12229 "EHLO
	roarinelk.homelinux.net") by ftp.linux-mips.org with ESMTP	id
 S24139441AbYLUI0b (ORCPT <rfc822;linux-mips@linux-mips.org>);	Sun, 21 Dec
 2008 08:26:31 +0000
Received: (qmail 3975 invoked from network); 21 Dec 2008 09:24:59 +0100
Received: from scarran.roarinelk.net (HELO localhost.localdomain)
 (192.168.0.242)  by 192.168.0.1 with SMTP; 21 Dec 2008 09:24:59 +0100
From: Manuel Lauss <mano@roarinelk.homelinux.net>
To: Linux-MIPS <linux-mips@linux-mips.org>
CC: Manuel Lauss <mano@roarinelk.homelinux.net>
Sender: "linux-mips-bounce@linux-mips.org" <linux-mips-bounce@linux-mips.org>
Date: Sun, 21 Dec 2008 00:26:23 -0800
Subject: [PATCH 10/14] Alchemy: RTC counter clocksource / clockevent support.
Thread-Topic: [PATCH 10/14] Alchemy: RTC counter clocksource / clockevent
 support.
Thread-Index: AcljRla4pfZpnEC2RUiJKZdghr1N0Q==
Message-ID:
 <cc0520393daa157516ae4f2cc6a69bc7b60a2a39.1229846414.git.mano@roarinelk.homelinux.net>
References: <cover.1229846410.git.mano@roarinelk.homelinux.net>
In-Reply-To: <cover.1229846410.git.mano@roarinelk.homelinux.net>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 0b
X-MS-Exchange-Organization-AuthSource: exch1.onstor.net
X-MS-Has-Attach:
X-Auto-Response-Suppress: All
X-MS-TNEF-Correlator:
x-originalarrivaltime: 21 Dec 2008 08:30:10.0418 (UTC)
 FILETIME=[56571D20:01C96346]
errors-to: linux-mips-bounce@linux-mips.org
x-ems-proccessed: 2K3Xl1OQTInXD6xxuA8z3Q==
x-ems-stamp: xfbn1P8XZzKuF/CB5AY4eA==
x-messageone-virus-version: vendor=fsecure
 engine=4.65.7400:2.4.4,1.2.40,4.0.164
 definitions=2008-12-21_01:2008-12-18,2008-12-21,2008-12-20 signatures=0
x-messageone-virus-scanned: Clean
x-messageone-envelope-sender: linux-mips-bounce@linux-mips.org
x-messageone-spam-details: rule=m773emszm_notspam policy=m773emszm score=0
 spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam
 adjust=0 reason=mlx engine=3.1.0-0810130000 definitions=main-0812200189
x-messageone-spam-score: 0
x-messageone-spam-bar:
x-list: linux-mips
x-archive-position: 21644
x-ecartis-version: Ecartis v1.0.0
x-original-sender: mano@roarinelk.homelinux.net
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

Add support for the 32 kHz counter1 (RTC) as clocksource / clockevent
device.  As a nice side effect, this also enables use of the 'wait'
instruction for runtime idle power savings.

If the counters aren't enabled/working properly, fall back on the
cp0 counter clock code.

Signed-off-by: Manuel Lauss <mano@roarinelk.homelinux.net>
---
 arch/mips/alchemy/Kconfig        |    4 +-
 arch/mips/alchemy/common/power.c |   16 ++-
 arch/mips/alchemy/common/setup.c |    6 -
 arch/mips/alchemy/common/time.c  |  292 ++++++++++++++++------------------=
----
 arch/mips/kernel/cpu-probe.c     |    6 +-
 5 files changed, 143 insertions(+), 181 deletions(-)

diff --git a/arch/mips/alchemy/Kconfig b/arch/mips/alchemy/Kconfig
index 4397d94..7f8ef13 100644
--- a/arch/mips/alchemy/Kconfig
+++ b/arch/mips/alchemy/Kconfig
@@ -128,8 +128,8 @@ config SOC_AU1200
 config SOC_AU1X00
        bool
        select 64BIT_PHYS_ADDR
-       select CEVT_R4K
-       select CSRC_R4K
+       select CEVT_R4K_LIB
+       select CSRC_R4K_LIB
        select IRQ_CPU
        select SYS_HAS_CPU_MIPS32_R1
        select SYS_SUPPORTS_32BIT_KERNEL
diff --git a/arch/mips/alchemy/common/power.c b/arch/mips/alchemy/common/po=
wer.c
index 33a3cdb..997dd56 100644
--- a/arch/mips/alchemy/common/power.c
+++ b/arch/mips/alchemy/common/power.c
@@ -85,7 +85,11 @@ static unsigned int  sleep_static_memctlr[4][3];
 #define SLEEP_TEST_TIMEOUT 1
 #ifdef SLEEP_TEST_TIMEOUT
 static int sleep_ticks;
-void wakeup_counter0_set(int ticks);
+static void wakeup_counter0_set(int ticks)
+{
+       au_writel(au_readl(SYS_TOYREAD) + ticks, SYS_TOYMATCH2);
+       au_sync();
+}
 #endif

 static void save_core_regs(void)
@@ -183,7 +187,6 @@ static void restore_core_regs(void)
        }

        restore_au1xxx_intctl();
-       wakeup_counter0_adjust();
 }

 unsigned long suspend_mode;
@@ -411,6 +414,15 @@ static struct ctl_table pm_dir_table[] =3D {
  */
 static int __init pm_init(void)
 {
+       /* init TOY to tick at 1Hz. No need to wait for access bits
+        * since there's plenty of time between here and the first
+        * suspend cycle.
+        */
+       if (au_readl(SYS_TOYTRIM) !=3D 32767) {
+               au_writel(32767, SYS_TOYTRIM);
+               au_sync();
+       }
+
        register_sysctl_table(pm_dir_table);
        return 0;
 }
diff --git a/arch/mips/alchemy/common/setup.c b/arch/mips/alchemy/common/se=
tup.c
index 4d42be8..8ad453a 100644
--- a/arch/mips/alchemy/common/setup.c
+++ b/arch/mips/alchemy/common/setup.c
@@ -63,12 +63,6 @@ void __init plat_mem_setup(void)
        ioport_resource.end =3D IOPORT_RESOURCE_END;
        iomem_resource.start =3D IOMEM_RESOURCE_START;
        iomem_resource.end =3D IOMEM_RESOURCE_END;
-
-       while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_E0S);
-       au_writel(SYS_CNTRL_E0 | SYS_CNTRL_EN0, SYS_COUNTER_CNTRL);
-       au_sync();
-       while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T0S);
-       au_writel(0, SYS_TOYTRIM);
 }

 #if defined(CONFIG_64BIT_PHYS_ADDR)
diff --git a/arch/mips/alchemy/common/time.c b/arch/mips/alchemy/common/tim=
e.c
index 1518570..57f0aec 100644
--- a/arch/mips/alchemy/common/time.c
+++ b/arch/mips/alchemy/common/time.c
@@ -1,5 +1,7 @@
 /*
+ * Copyright (C) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
  *
+ * Previous incarnations were:
  * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
  * Copied and modified Carsten Langgaard's time.c
  *
@@ -23,131 +25,27 @@
  *
  * #######################################################################=
#
  *
- * Setting up the clock on the MIPS boards.
- *
- * We provide the clock interrupt processing and the timer offset compute
- * functions.  If CONFIG_PM is selected, we also ensure the 32KHz timer is
- * available.  -- Dan
+ * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
+ * databooks).  Firmware/Board init code must enable the counters in the
+ * counter control register, otherwise the CP0 counter clocksource/event
+ * will be installed instead (and use of 'wait' instruction is prohibited)=
.
  */

-#include <linux/types.h>
-#include <linux/init.h>
+#include <linux/clockchips.h>
+#include <linux/clocksource.h>
+#include <linux/interrupt.h>
 #include <linux/spinlock.h>

-#include <asm/mipsregs.h>
 #include <asm/time.h>
 #include <asm/mach-au1x00/au1000.h>

-static int no_au1xxx_32khz;
-extern int allow_au1k_wait; /* default off for CP0 Counter */
+/* 32kHz clock enabled and detected */
+#define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)

-#ifdef CONFIG_PM
-#if HZ < 100 || HZ > 1000
-#error "unsupported HZ value! Must be in [100,1000]"
-#endif
-#define MATCH20_INC (328 * 100 / HZ) /* magic number 328 is for HZ=3D100..=
. */
-static unsigned long last_pc0, last_match20;
-#endif
+extern int allow_au1k_wait; /* default off for CP0 Counter */

 static DEFINE_SPINLOCK(time_lock);

-unsigned long wtimer;
-
-#ifdef CONFIG_PM
-static irqreturn_t counter0_irq(int irq, void *dev_id)
-{
-       unsigned long pc0;
-       int time_elapsed;
-       static int jiffie_drift;
-
-       if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
-               /* should never happen! */
-               printk(KERN_WARNING "counter 0 w status error\n");
-               return IRQ_NONE;
-       }
-
-       pc0 =3D au_readl(SYS_TOYREAD);
-       if (pc0 < last_match20)
-               /* counter overflowed */
-               time_elapsed =3D (0xffffffff - last_match20) + pc0;
-       else
-               time_elapsed =3D pc0 - last_match20;
-
-       while (time_elapsed > 0) {
-               do_timer(1);
-#ifndef CONFIG_SMP
-               update_process_times(user_mode(get_irq_regs()));
-#endif
-               time_elapsed -=3D MATCH20_INC;
-               last_match20 +=3D MATCH20_INC;
-               jiffie_drift++;
-       }
-
-       last_pc0 =3D pc0;
-       au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
-       au_sync();
-
-       /*
-        * Our counter ticks at 10.009765625 ms/tick, we we're running
-        * almost 10 uS too slow per tick.
-        */
-
-       if (jiffie_drift >=3D 999) {
-               jiffie_drift -=3D 999;
-               do_timer(1); /* increment jiffies by one */
-#ifndef CONFIG_SMP
-               update_process_times(user_mode(get_irq_regs()));
-#endif
-       }
-
-       return IRQ_HANDLED;
-}
-
-struct irqaction counter0_action =3D {
-       .handler        =3D counter0_irq,
-       .flags          =3D IRQF_DISABLED,
-       .name           =3D "alchemy-toy",
-       .dev_id         =3D NULL,
-};
-
-/* When we wakeup from sleep, we have to "catch up" on all of the
- * timer ticks we have missed.
- */
-void wakeup_counter0_adjust(void)
-{
-       unsigned long pc0;
-       int time_elapsed;
-
-       pc0 =3D au_readl(SYS_TOYREAD);
-       if (pc0 < last_match20)
-               /* counter overflowed */
-               time_elapsed =3D (0xffffffff - last_match20) + pc0;
-       else
-               time_elapsed =3D pc0 - last_match20;
-
-       while (time_elapsed > 0) {
-               time_elapsed -=3D MATCH20_INC;
-               last_match20 +=3D MATCH20_INC;
-       }
-
-       last_pc0 =3D pc0;
-       au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
-       au_sync();
-
-}
-
-/* This is just for debugging to set the timer for a sleep delay. */
-void wakeup_counter0_set(int ticks)
-{
-       unsigned long pc0;
-
-       pc0 =3D au_readl(SYS_TOYREAD);
-       last_pc0 =3D pc0;
-       au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
-       au_sync();
-}
-#endif
-
 /*
  * I haven't found anyone that doesn't use a 12 MHz source clock,
  * but just in case.....
@@ -162,37 +60,15 @@ void wakeup_counter0_set(int ticks)
  * this advertised speed will introduce error and sometimes not work
  * properly.  This function is futher convoluted to still allow configurat=
ions
  * to do that in case they have really, really old silicon with a
- * write-only PLL register, that we need the 32 KHz when power management
- * "wait" is enabled, and we need to detect if the 32 KHz isn't present
- * but requested......got it? :-)              -- Dan
+ * write-only PLL register.                    -- Dan
  */
 unsigned long calc_clock(void)
 {
        unsigned long cpu_speed;
        unsigned long flags;
-       unsigned long counter;

        spin_lock_irqsave(&time_lock, flags);

-       /* Power management cares if we don't have a 32 KHz counter. */
-       no_au1xxx_32khz =3D 0;
-       counter =3D au_readl(SYS_COUNTER_CNTRL);
-       if (counter & SYS_CNTRL_E0) {
-               int trim_divide =3D 16;
-
-               au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
-
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
-               /* RTC now ticks at 32.768/16 kHz */
-               au_writel(trim_divide - 1, SYS_RTCTRIM);
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
-
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
-               au_writel(0, SYS_TOYWRITE);
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
-       } else
-               no_au1xxx_32khz =3D 1;
-
        /*
         * On early Au1000, sys_cpupll was write-only. Since these
         * silicon versions of Au1000 are not sold by AMD, we don't bend
@@ -215,8 +91,65 @@ unsigned long calc_clock(void)
        return cpu_speed;
 }

+static cycle_t au1x_counter1_read(void)
+{
+       return au_readl(SYS_RTCREAD);
+}
+
+static struct clocksource au1x_counter1_clocksource =3D {
+       .name           =3D "alchemy-counter1",
+       .read           =3D au1x_counter1_read,
+       .mask           =3D CLOCKSOURCE_MASK(32),
+       .flags          =3D CLOCK_SOURCE_IS_CONTINUOUS,
+       .rating         =3D 100,
+};
+
+static int au1x_rtcmatch2_set_next_event(unsigned long delta,
+                                        struct clock_event_device *cd)
+{
+       delta +=3D au_readl(SYS_RTCREAD);
+       /* wait for register access */
+       while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M21)
+               ;
+       au_writel(delta, SYS_RTCMATCH2);
+       au_sync();
+
+       return 0;
+}
+
+static void au1x_rtcmatch2_set_mode(enum clock_event_mode mode,
+                                   struct clock_event_device *cd)
+{
+}
+
+static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
+{
+       struct clock_event_device *cd =3D dev_id;
+       cd->event_handler(cd);
+       return IRQ_HANDLED;
+}
+
+static struct clock_event_device au1x_rtcmatch2_clockdev =3D {
+       .name           =3D "rtcmatch2",
+       .features       =3D CLOCK_EVT_FEAT_ONESHOT,
+       .rating         =3D 100,
+       .irq            =3D AU1000_RTC_MATCH2_INT,
+       .set_next_event =3D au1x_rtcmatch2_set_next_event,
+       .set_mode       =3D au1x_rtcmatch2_set_mode,
+       .cpumask        =3D CPU_MASK_ALL,
+};
+
+static struct irqaction au1x_rtcmatch2_irqaction =3D {
+       .handler        =3D au1x_rtcmatch2_irq,
+       .flags          =3D IRQF_DISABLED | IRQF_TIMER,
+       .name           =3D "timer",
+       .dev_id         =3D &au1x_rtcmatch2_clockdev,
+};
+
 void __init plat_time_init(void)
 {
+       struct clock_event_device *cd =3D &au1x_rtcmatch2_clockdev;
+       unsigned long t;
        unsigned int est_freq =3D calc_clock();

        est_freq +=3D 5000;    /* round */
@@ -225,41 +158,62 @@ void __init plat_time_init(void)
               est_freq / 1000000, ((est_freq % 1000000) * 100) / 1000000);
        set_au1x00_speed(est_freq);

-#ifdef CONFIG_PM
-       /*
-        * setup counter 0, since it keeps ticking after a
-        * 'wait' instruction has been executed. The CP0 timer and
-        * counter 1 do NOT continue running after 'wait'
-        *
-        * It's too early to call request_irq() here, so we handle
-        * counter 0 interrupt as a special irq and it doesn't show
-        * up under /proc/interrupts.
-        *
-        * Check to ensure we really have a 32 KHz oscillator before
-        * we do this.
+       /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
+        * has been detected.  If so install the rtcmatch2 clocksource,
+        * otherwise don't bother.  Note that both bits being set is by
+        * no means a definite guarantee that the counters actually work
+        * (the 32S bit seems to be stuck set to 1 once a single clock-
+        * edge is detected, hence the timeouts).
         */
-       if (no_au1xxx_32khz)
-               printk(KERN_WARNING "WARNING: no 32KHz clock found.\n");
-       else {
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
-               au_writel(0, SYS_TOYWRITE);
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
+       if (CNTR_OK !=3D (au_readl(SYS_COUNTER_CNTRL) & CNTR_OK))
+               goto cntr_err;

-               au_writel(au_readl(SYS_WAKEMSK) | (1 << 8), SYS_WAKEMSK);
-               au_writel(~0, SYS_WAKESRC);
-               au_sync();
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
+       /*
+        * setup counter 1 (RTC) to tick at full speed
+        */
+       t =3D 0xffffff;
+       while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S) && t--)
+               asm volatile ("nop");
+       if (!t)
+               goto cntr_err;

-               /* Setup match20 to interrupt once every HZ */
-               last_pc0 =3D last_match20 =3D au_readl(SYS_TOYREAD);
-               au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
-               au_sync();
-               while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
-               setup_irq(AU1000_TOY_MATCH2_INT, &counter0_action);
+       au_writel(0, SYS_RTCTRIM);      /* 32.768 kHz */
+       au_sync();

-               /* We can use the real 'wait' instruction. */
-               allow_au1k_wait =3D 1;
-       }
+       t =3D 0xffffff;
+       while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && t--)
+               asm volatile ("nop");
+       if (!t)
+               goto cntr_err;
+       au_writel(0, SYS_RTCWRITE);
+       au_sync();

-#endif
+       t =3D 0xffffff;
+       while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && t--)
+               asm volatile ("nop");
+       if (!t)
+               goto cntr_err;
+
+       /* register counter1 clocksource and event device */
+       clocksource_set_clock(&au1x_counter1_clocksource, 32768);
+       clocksource_register(&au1x_counter1_clocksource);
+
+       cd->shift =3D 32;
+       cd->mult =3D div_sc(32768, NSEC_PER_SEC, cd->shift);
+       cd->max_delta_ns =3D clockevent_delta2ns(0xffffffff, cd);
+       cd->min_delta_ns =3D clockevent_delta2ns(8, cd);  /* ~0.25ms */
+       clockevents_register_device(cd);
+       setup_irq(AU1000_RTC_MATCH2_INT, &au1x_rtcmatch2_irqaction);
+
+       printk(KERN_INFO "Alchemy clocksource installed\n");
+
+       /* can now use 'wait' */
+       allow_au1k_wait =3D 1;
+       return;
+
+cntr_err:
+       /* counters unusable, use C0 counter */
+       r4k_clockevent_init();
+       init_r4k_clocksource();
+       allow_au1k_wait =3D 0;
 }
diff --git a/arch/mips/kernel/cpu-probe.c b/arch/mips/kernel/cpu-probe.c
index c9207b5..bf41ca7 100644
--- a/arch/mips/kernel/cpu-probe.c
+++ b/arch/mips/kernel/cpu-probe.c
@@ -96,6 +96,9 @@ int allow_au1k_wait;

 static void au1k_wait(void)
 {
+       if (!allow_au1k_wait)
+               return;
+
        /* using the wait instruction makes CP0 counter unusable */
        __asm__("       .set    mips3                   \n"
                "       cache   0x14, 0(%0)             \n"
@@ -185,8 +188,7 @@ void __init check_wait(void)
        case CPU_AU1200:
        case CPU_AU1210:
        case CPU_AU1250:
-               if (allow_au1k_wait)
-                       cpu_wait =3D au1k_wait;
+               cpu_wait =3D au1k_wait;
                break;
        case CPU_20KC:
                /*
--
1.6.0.4


