mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v2 07/16] ARM: at91: import early_udelay from at91bootstrap
Date: Tue, 19 Feb 2019 18:21:41 +0100	[thread overview]
Message-ID: <20190219172150.11901-8-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20190219172150.11901-1-a.fatoum@pengutronix.de>

For use by the incoming at91bootstrap DDRAMC initialization code,
this commit provides an early_udelay function usable in PBL imported from
https://github.com/linux4sam/at91bootstrap/blob/v3.8.12/driver/at91_pit.c

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/mach-at91/Makefile                   |  1 +
 arch/arm/mach-at91/early_udelay.c             | 53 +++++++++++++++++++
 .../include/mach/at91_lowlevel_clock.h        |  9 ++++
 .../arm/mach-at91/include/mach/early_udelay.h | 13 +++++
 4 files changed, 76 insertions(+)
 create mode 100644 arch/arm/mach-at91/early_udelay.c
 create mode 100644 arch/arm/mach-at91/include/mach/early_udelay.h

diff --git a/arch/arm/mach-at91/Makefile b/arch/arm/mach-at91/Makefile
index 7c4c58b080a1..b8ff6428d05a 100644
--- a/arch/arm/mach-at91/Makefile
+++ b/arch/arm/mach-at91/Makefile
@@ -1,5 +1,6 @@
 obj-y += setup.o
 pbl-y += lowlevel_clock.o
+pbl-$(CONFIG_CLOCKSOURCE_ATMEL_PIT) += early_udelay.o
 
 ifeq ($(CONFIG_COMMON_CLK_OF_PROVIDER),)
 obj-y += clock.o
diff --git a/arch/arm/mach-at91/early_udelay.c b/arch/arm/mach-at91/early_udelay.c
new file mode 100644
index 000000000000..fb8d8bba5434
--- /dev/null
+++ b/arch/arm/mach-at91/early_udelay.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: BSD-1-Clause
+/*
+ * Copyright (c) 2012, Atmel Corporation
+ */
+
+#include <mach/hardware.h>
+#include <asm/io.h>
+#include <mach/at91_lowlevel_clock.h>
+#include <mach/at91_pit.h>
+#include <mach/early_udelay.h>
+
+static unsigned int master_clock;
+static void __iomem *pmc, *pit;
+
+/* Because the below statement is used in the function:
+ *	((MASTER_CLOCK >> 10) * usec) is used,
+ * to our 32-bit system. the argu "usec" maximum value is:
+ * supposed "MASTER_CLOCK" is 132M.
+ *	132000000 / 1024 = 128906
+ *	(0xffffffff) / 128906 = 33318.
+ * So the maximum delay time is 33318 us.
+ */
+/* requires PIT to be initialized, but not the clocksource framework */
+void early_udelay(unsigned int usec)
+{
+	unsigned int delay;
+	unsigned int current;
+	unsigned int base = readl(pit + AT91_PIT_PIIR);
+
+	if (at91_pmc_check_mck_h32mxdiv(pmc))
+		master_clock /= 2;
+
+	delay = ((master_clock >> 10) * usec) >> 14;
+
+	do {
+		current = readl(pit + AT91_PIT_PIIR);
+		current -= base;
+	} while (current < delay);
+}
+
+void early_udelay_init(void __iomem *pmc_base,
+		       void __iomem *pit_base,
+		       unsigned clock,
+		       unsigned int master_clock_rate)
+{
+	master_clock = master_clock_rate;
+	pmc = pmc_base;
+	pit = pit_base;
+
+	writel(AT91_PIT_PIV | AT91_PIT_PITEN, pit + AT91_PIT_MR);
+
+	at91_pmc_enable_periph_clock(pmc_base, clock);
+}
diff --git a/arch/arm/mach-at91/include/mach/at91_lowlevel_clock.h b/arch/arm/mach-at91/include/mach/at91_lowlevel_clock.h
index 8d04e30b644b..e4bdaade740c 100644
--- a/arch/arm/mach-at91/include/mach/at91_lowlevel_clock.h
+++ b/arch/arm/mach-at91/include/mach/at91_lowlevel_clock.h
@@ -9,6 +9,7 @@
 #include <errno.h>
 #include <asm/io.h>
 #include <mach/at91_pmc.h>
+#include <linux/types.h>
 
 void at91_lowlevel_clock_init(void __iomem *pmc_base);
 void at91_pmc_cfg_mck(void __iomem *pmc_base, u32 pmc_mckr);
@@ -40,4 +41,12 @@ static inline int at91_pmc_enable_periph_clock(void __iomem *pmc_base,
 	return 0;
 }
 
+static inline bool at91_pmc_check_mck_h32mxdiv(void __iomem *pmc_base)
+{
+	if (IS_ENABLED(CONFIG_HAVE_AT91_H32MX))
+		return readl(pmc_base + AT91_PMC_MCKR) & AT91_PMC_H32MXDIV;
+
+	return false;
+}
+
 #endif
diff --git a/arch/arm/mach-at91/include/mach/early_udelay.h b/arch/arm/mach-at91/include/mach/early_udelay.h
new file mode 100644
index 000000000000..120b586310f0
--- /dev/null
+++ b/arch/arm/mach-at91/include/mach/early_udelay.h
@@ -0,0 +1,13 @@
+#ifndef __EARLY_UDELAY_H__
+#define __EARLY_UDELAY_H__
+
+#include <linux/compiler.h>
+
+/* requires PIT to be initialized, but not the clocksource framework */
+void early_udelay(unsigned int usec);
+void early_udelay_init(void __iomem *pmc_base,
+		       void __iomem *pit_base,
+		       unsigned clock,
+		       unsigned int master_clock_rate);
+
+#endif
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-02-19 17:21 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-19 17:21 [PATCH v2 00/16] ARM: at91: microchip-kz9477-evb: support first stage boot Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 01/16] LICENSES: add BSD-1-Clause license Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 02/16] ARM: at91: import at91bootstrap's at91_ddrsdrc.h Ahmad Fatoum
2019-02-22 13:33   ` Roland Hieber
2019-02-26  9:04     ` Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 03/16] ARM: at91: migrate at91sam9_ddrsdr.h to use " Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 04/16] ARM: at91: replace at91sam9_ddrsdr.h with " Ahmad Fatoum
2019-02-20  9:07   ` Sascha Hauer
2019-02-19 17:21 ` [PATCH v2 05/16] ARM: at91: watchdog: implement at91_wdt_disable Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 06/16] ARM: at91: import lowlevel clock initialization from at91bootstrap Ahmad Fatoum
2019-02-20  9:11   ` Sascha Hauer
2019-02-19 17:21 ` Ahmad Fatoum [this message]
2019-02-20  9:20   ` [PATCH v2 07/16] ARM: at91: import early_udelay " Sascha Hauer
2019-02-20  9:23     ` Sascha Hauer
2019-02-19 17:21 ` [PATCH v2 08/16] ARM: at91: import low level DDRAMC initialization code " Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 09/16] ARM: at91: import lowlevel dbgu UART init " Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 10/16] images: at91: differentiate between first and second stage images Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 11/16] ARM: at91: sama5: specify 0x10000 as first stage max size Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 12/16] ARM: at91: microchip-ksz9477-evb: use compressed DTB Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 13/16] ARM: dts: microchip-ksz9477-evb: add dummy first stage device tree Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 14/16] ARM: at91: microchip-ksz9477-evb: implement first stage Ahmad Fatoum
2019-02-22 13:45   ` Roland Hieber
2019-02-26  9:05     ` Ahmad Fatoum
2019-02-19 17:21 ` [PATCH v2 15/16] ARM: at91: microchip-ksz9477: provide board code fallback Ahmad Fatoum
2019-02-20  9:27   ` Sascha Hauer
2019-02-19 17:21 ` [PATCH v2 16/16] doc: microchip-ksz9477-evb: add documentation Ahmad Fatoum
2019-02-24 12:42 ` [PATCH v2 00/16] ARM: at91: microchip-kz9477-evb: support first stage boot Sam Ravnborg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190219172150.11901-8-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox