arm: Move to upstream udelay via timer implementation

This is a squash of a handful of changes and reverts of the
Qualcomm specific implementation:

  Revert "arm: Implement a timer based __delay() loop"

  This reverts commit 976eafa8b18252876e15f861944acf693b07ce7e.

  Revert "arm: Allow machines to override __delay()"

  This reverts commit bc0ef8ab167272890f1aab62928b04a9aeb87ce9.

  Revert "arm: Translate delay.S into (mostly) C"

  This reverts commit 8d5868d8205d10a0a8e423f53e9cc9bb3e9d1a34.

  ARM: 7451/1: arch timer: implement read_current_timer and get_cycles

  This patch implements read_current_timer using the architected timers
  when they are selected via CONFIG_ARM_ARCH_TIMER. If they are detected
  not to be usable at runtime, we return -ENXIO to the caller.

  Furthermore, if read_current_timer is exported then we can implement
  get_cycles in terms of it for use as both an entropy source and for
  implementing __udelay and friends.

  Tested-by: Shinya Kuribayashi <shinya.kuribayashi.px@renesas.com>
  Reviewed-by: Stephen Boyd <sboyd@codeaurora.org>
  Signed-off-by: Will Deacon <will.deacon@arm.com>
  Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

  ARM: 7452/1: delay: allow timer-based delay implementation to be
  selected

  This patch allows a timer-based delay implementation to be selected by
  switching the delay routines over to use get_cycles, which is
  implemented in terms of read_current_timer. This further allows us to
  skip the loop calibration and have a consistent delay function in the
  face of core frequency scaling.

  To avoid the pain of dealing with memory-mapped counters, this
  implementation uses the co-processor interface to the architected timers
  when they are available. The previous loop-based implementation is
  kept around for CPUs without the architected timers and we retain both
  the maximum delay (2ms) and the corresponding conversion factors for
  determining the number of loops required for a given interval. Since the
  indirection of the timer routines will only work when called from C,
  the sa1100 sleep routines are modified to branch to the loop-based delay
  functions directly.

  Tested-by: Shinya Kuribayashi <shinya.kuribayashi.px@renesas.com>
  Reviewed-by: Stephen Boyd <sboyd@codeaurora.org>
  Signed-off-by: Will Deacon <will.deacon@arm.com>
  Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

  ARM: delay: set loops_per_jiffy when moving to timer-based loop

  The delay functions may be called by some platforms between switching to
  the timer-based delay loop but before calibration. In this case, the
  initial loops_per_jiffy may not be suitable for the timer (although a
  compromise may be achievable) and delay times may be considered too
  inaccurate.

  This patch updates loops_per_jiffy when switching to the timer-based
  delay loop so that delays are consistent prior to calibration.

  Signed-off-by: Will Deacon <will.deacon@arm.com>

  ARM: delay: add registration mechanism for delay timer sources

  The current timer-based delay loop relies on the architected timer to
  initiate the switch away from the polling-based implementation. This is
  unfortunate for platforms without the architected timers but with a
  suitable delay source (that is, constant frequency, always powered-up
  and ticking as long as the CPUs are online).

  This patch introduces a registration mechanism for the delay timer
  (which provides an unconditional read_current_timer implementation) and
  updates the architected timer code to use the new interface.

  Signed-off-by: Jonathan Austin <jonathan.austin@arm.com>
  Signed-off-by: Will Deacon <will.deacon@arm.com>

  ARM: export default read_current_timer

  read_current_timer is used by get_cycles since "ARM: 7538/1: delay:
  add registration mechanism for delay timer sources", and get_cycles
  can be used by device drivers in loadable modules, so it has to
  be exported.

  Without this patch, building imote2_defconfig fails with

  ERROR: "read_current_timer" [crypto/tcrypt.ko] undefined!

  Signed-off-by: Arnd Bergmann <arnd@arndb.de>
  Cc: Stephen Boyd <sboyd@codeaurora.org>
  Cc: Jonathan Austin <jonathan.austin@arm.com>
  Cc: Will Deacon <will.deacon@arm.com>
  Cc: Russell King <rmk+kernel@arm.linux.org.uk>

Change-Id: If1ad095d6852f5966ea995856103e06de6ab2f59
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>

Conflicts:
	arch/arm/lib/Makefile
	arch/arm/lib/delay.c
ARM: add cpufreq transiton notifier to adjust loops_per_jiffy for smp

If CONFIG_SMP, cpufreq skips loops_per_jiffy update, because different
arch has different per-cpu loops_per_jiffy definition.

Change-Id: I3bfdf800784c9c494f873de096e3a1972f14f358
Signed-off-by: Richard Zhao <richard.zhao@linaro.org>
Acked-by: Russell King <rmk+kernel@arm.linux.org.uk>
Acked-by: Santosh Shilimkar <santosh.shilimkar@ti.com>
Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
ARM: 7653/2: do not scale loops_per_jiffy when using a constant delay clock

When udelay() is implemented using an architected timer, it is wrong
to scale loops_per_jiffy when changing the CPU clock frequency since
the timer clock remains constant.

The lpj should probably become an implementation detail relevant to
the CPU loop based delay routine only and more confined to it. In the
mean time this is the minimal fix needed to have expected delays with
the timer based implementation when cpufreq is also in use.

Change-Id: I75f66afe0cd4851031c71c1ebd522b402bb0a788
Reported-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Nicolas Pitre <nico@linaro.org>
Tested-by: Viresh Kumar <viresh.kumar@linaro.org>
Acked-by: Liviu Dudau <Liviu.Dudau@arm.com>
Cc: stable@vger.kernel.org
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
ARM: 7685/1: delay: use private ticks_per_jiffy field for timer-based delay ops

Commit 70264367a243 ("ARM: 7653/2: do not scale loops_per_jiffy when
using a constant delay clock") fixed a problem with our timer-based
delay loop, where loops_per_jiffy is scaled by cpufreq yet used directly
by the timer delay ops.

This patch fixes the problem in a more elegant way by keeping a private
ticks_per_jiffy field in the delay ops, independent of loops_per_jiffy
and therefore not subject to scaling. The loop-based delay continues to
use loops_per_jiffy directly, as it should.

Change-Id: I675a5089c2156c01e7c2e151c7be2795cf965091
Acked-by: Nicolas Pitre <nico@linaro.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
iopoll: Make readl_poll_timeout() and friends preemption-safe

With the previous implementation, pre-emption was possible
between reading the register value and evaluating the break
condition for the timeout. If jiffies advanced sufficiently to
expire the timeout before the polling thread was rescheduled,
the macro would return -ETIMEDOUT incorrectly.

Fix this by re-reading the register value again after evaluating
the timeout condition as true.

Change-Id: I58a42798235db3606373a2f689f7d6c95475702e
Signed-off-by: Matt Wagantall <mattw@codeaurora.org>
iopoll: Use ktime_get() instead of jiffies for timeout calculations

Presently, small timeout values will round up to 1 jiffy which,
on CONFIG_HZ=100 systems, corresponds to a full 10 milliseconds.
This is undesirable for drivers specifying timeouts of only a
few 10's of microseconds (which is common), since they may end
up tight-loop spinning for hundreds or thousands of times longer
than expected before reporting a timeout.

Additionally, jiffies cannot be reliably used with time_after()
when the value of jiffies is small (like 1). In rare but real
scenarios, jiffies may actually increment by more than 1 at a
time. Specifically, this will occur if interrupts are disabled
on for more than 1 jiffy (10 milliseconds for CONFIG_HZ=100) on
the CPU responsible for incrementing jiffies.

If interrupts are re-enabled on that CPU between the time the
iopoll code (on another CPU) calculates the timeout value of
jiffies and when the time_after() comparison is made, then the
iopoll APIs may return -ETIMEDOUT prematurely, even though the
specified timeout has not actually expired.

Using ktime_get() avoid this problem (which arguably also needs
a generic fix to avoid similar problems for other code which use
jiffies to calculate timeouts).

CRs-Fixed: 587801
Change-Id: I19150f41965b918c59c3fb98e29a8bd2e2c9609f
Signed-off-by: Matt Wagantall <mattw@codeaurora.org>