Initial upstream commit

The history contained unlicensed code so everything got squashed, sorry.

Change-Id: Ie1335ecfcee7f740bb6de2e9887606be30a2deff
Signed-off-by: Nico Huber <nico.huber@secunet.com>
diff --git a/ada/Makefile.inc b/ada/Makefile.inc
new file mode 100644
index 0000000..e09f1a5
--- /dev/null
+++ b/ada/Makefile.inc
@@ -0,0 +1,3 @@
+hw-$(CONFIG_HWBASE_STATIC_MMIO) += static_mmio/hw-mmio_range.adb
+hw-$(CONFIG_HWBASE_DYNAMIC_MMIO) += dynamic_mmio/hw-mmio_range.adb
+hw-$(CONFIG_HWBASE_TIMER_CLOCK_GETTIME) += clock_gettime/hw-time-timer.adb
diff --git a/ada/clock_gettime/hw-time-timer.adb b/ada/clock_gettime/hw-time-timer.adb
new file mode 100644
index 0000000..914b201
--- /dev/null
+++ b/ada/clock_gettime/hw-time-timer.adb
@@ -0,0 +1,61 @@
+--
+-- Copyright (C) 2016 secunet Security Networks AG
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; version 2 of the License.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+
+with Interfaces.C;
+
+use type Interfaces.C.long;
+
+package body HW.Time.Timer
+with
+   Refined_State => (Timer_State => null,
+                     Abstract_Time => null)
+is
+   CLOCK_MONOTONIC_RAW : constant := 4;
+
+   subtype Clock_ID_T is Interfaces.C.int;
+   subtype Time_T is Interfaces.C.long;
+
+   type Struct_Timespec is record
+      TV_Sec   : aliased Time_T;
+      TV_NSec  : aliased Interfaces.C.long;
+   end record;
+   pragma Convention (C_Pass_By_Copy, Struct_Timespec);
+
+   function Clock_Gettime
+     (Clock_ID :        Clock_ID_T;
+      Timespec : access Struct_Timespec)
+      return Interfaces.C.int;
+   pragma Import (C, Clock_Gettime, "clock_gettime");
+
+   function Raw_Value_Min return T
+   is
+      Ignored : Interfaces.C.int;
+      Timespec : aliased Struct_Timespec;
+   begin
+      Ignored := Clock_Gettime (CLOCK_MONOTONIC_RAW, Timespec'Access);
+      return T (Timespec.TV_Sec * 1_000_000_000 + Timespec.TV_NSec);
+   end Raw_Value_Min;
+
+   function Raw_Value_Max return T
+   is
+   begin
+      return Raw_Value_Min + 1;
+   end Raw_Value_Max;
+
+   function Hz return T
+   is
+   begin
+      return 1_000_000_000; -- clock_gettime(2) is fixed to nanoseconds
+   end Hz;
+
+end HW.Time.Timer;
diff --git a/ada/dynamic_mmio/hw-mmio_range.adb b/ada/dynamic_mmio/hw-mmio_range.adb
new file mode 100644
index 0000000..78cc048
--- /dev/null
+++ b/ada/dynamic_mmio/hw-mmio_range.adb
@@ -0,0 +1,76 @@
+--
+-- Copyright (C) 2015-2016 secunet Security Networks AG
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; version 2 of the License.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+
+with HW.Debug;
+with GNAT.Source_Info;
+with System.Storage_Elements;
+with System.Address_To_Access_Conversions;
+
+package body HW.MMIO_Range
+with
+   Refined_State =>
+     (State => Range_A,       -- the contents accessed
+      Base_Address => null)   -- the address, so actually Range_A too
+is
+   pragma Warnings (Off, "implicit dereference",
+                    Reason => "This is what this package is about.");
+
+   Debug_Reads    : constant Boolean := False;
+   Debug_Writes   : constant Boolean := False;
+
+   type Range_Access is access all Array_T;
+   package Conv_Range is new System.Address_To_Access_Conversions (Array_T);
+
+   Range_A : Range_Access :=
+      Range_Access (Conv_Range.To_Pointer (System'To_Address (Base_Addr)))
+   with Volatile;
+
+   procedure Read (Value : out Element_T; Index : in Index_T)
+   is
+      use type Word32;
+   begin
+      Value := Range_A (Index);
+      pragma Debug (Debug_Reads, Debug.Put
+        (GNAT.Source_Info.Enclosing_Entity & ":  "));
+      pragma Debug (Debug_Reads, Debug.Put_Word32 (Word32 (Value)));
+      pragma Debug (Debug_Reads, Debug.Put (" <- "));
+      pragma Debug (Debug_Reads, Debug.Put_Word32
+        (Word32 (System.Storage_Elements.To_Integer
+           (Conv_Range.To_Address (Conv_Range.Object_Pointer (Range_A)))) +
+         Word32 (Index) * (Element_T'Size / 8)));
+      pragma Debug (Debug_Reads, Debug.New_Line);
+   end Read;
+
+   procedure Write (Index : in Index_T; Value : in Element_T)
+   is
+      use type Word32;
+   begin
+      pragma Debug (Debug_Writes, Debug.Put
+        (GNAT.Source_Info.Enclosing_Entity & ": "));
+      pragma Debug (Debug_Writes, Debug.Put_Word32 (Word32 (Value)));
+      pragma Debug (Debug_Writes, Debug.Put (" -> "));
+      pragma Debug (Debug_Writes, Debug.Put_Word32
+        (Word32 (System.Storage_Elements.To_Integer
+           (Conv_Range.To_Address (Conv_Range.Object_Pointer (Range_A)))) +
+         Word32 (Index) * (Element_T'Size / 8)));
+      pragma Debug (Debug_Writes, Debug.New_Line);
+      Range_A (Index) := Value;
+   end Write;
+
+   procedure Set_Base_Address (Base : Word64) is
+   begin
+      Range_A := Range_Access
+        (Conv_Range.To_Pointer (System'To_Address (Base)));
+   end Set_Base_Address;
+
+end HW.MMIO_Range;
diff --git a/ada/static_mmio/hw-mmio_range.adb b/ada/static_mmio/hw-mmio_range.adb
new file mode 100644
index 0000000..fb72152
--- /dev/null
+++ b/ada/static_mmio/hw-mmio_range.adb
@@ -0,0 +1,35 @@
+--
+-- Copyright (C) 2016 secunet Security Networks AG
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; version 2 of the License.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+
+package body HW.MMIO_Range
+with
+   Refined_State =>
+     (State => MMIO,
+      Base_Address => null)
+is
+
+   MMIO : Array_T with Volatile, Address => System'To_Address (Base_Addr);
+
+   procedure Read (Value : out Element_T; Index : in Index_T) is
+   begin
+      Value := MMIO (Index);
+   end Read;
+
+   procedure Write (Index : in Index_T; Value : in Element_T) is
+   begin
+      MMIO (Index) := Value;
+   end Write;
+
+   procedure Set_Base_Address (Base : Word64) is null;
+
+end HW.MMIO_Range;