Revert "libplatsupport: hifive ltimer refactor"

This reverts commit c0c8ddcfac95a09b50e82cfa88367145d7aeaa5e.
diff --git a/libplatsupport/CMakeLists.txt b/libplatsupport/CMakeLists.txt
index a6d845b..c1e67d2 100644
--- a/libplatsupport/CMakeLists.txt
+++ b/libplatsupport/CMakeLists.txt
@@ -65,7 +65,11 @@
     list(APPEND deps src/arch/x86/tsc.c)
 elseif(${KernelArch} STREQUAL "riscv")
     list(APPEND deps src/arch/riscv/irqchip/plic.c)
-    list(APPEND irqchip_modules "-Wl,--undefined=riscv_plic_ptr")
+    list(
+        APPEND
+            irqchip_modules
+            "-Wl,--undefined=riscv_plic_ptr"
+    )
 endif()
 
 if(KernelPlatformQEMUArmVirt)
diff --git a/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h b/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h
index 7af74c8..6ee8f3a 100644
--- a/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h
+++ b/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h
@@ -20,14 +20,18 @@
 
 /* The input frequence is 0.5 of the CPU frequency which is 1GHz by default */
 #define PWM_INPUT_FREQ (500*1000*1000)
+#define PWM0_PADDR   0x10020000
+#define PWM1_PADDR   0x10021000
 
-#define PWM0_PATH "/soc/pwm@10020000"
-#define PWM1_PATH "/soc/pwm@10021000"
+#define PWM0_INTERRUPT0 42
+#define PWM0_INTERRUPT1 43
+#define PWM0_INTERRUPT2 44
+#define PWM0_INTERRUPT3 45
 
-/* There is only one register map available in device tree */
-#define PWM_REG_CHOICE 0
-/* There are four interrupts for matches available, use the first one */
-#define PWM_IRQ_CHOICE 0
+#define PWM1_INTERRUPT0 46
+#define PWM1_INTERRUPT1 47
+#define PWM1_INTERRUPT2 48
+#define PWM1_INTERRUPT3 49
 
 /**
  * When used in UPCOUNTER, pwm_handle_irq needs to be called on each interrupt
@@ -36,14 +40,13 @@
  * if pwm_get_time is called before pwm_handle_irq the time could be incorrect.
  */
 typedef enum PWM_MODE {
-    TIMEOUT,
-    UPCOUNTER,
+	TIMEOUT,
+	UPCOUNTER,
 } pwm_mode_t;
 
 typedef struct {
-    char *fdt_path;
-    ltimer_callback_fn_t user_cb_fn;
-    void *user_cb_token;
+    /* vaddr pwm is mapped to */
+    void *vaddr;
     pwm_mode_t mode;
 } pwm_config_t;
 
@@ -64,19 +67,9 @@
 };
 
 typedef struct pwm {
-    /* set in init */
-    ps_io_ops_t ops;
-    pwm_mode_t mode;
-    ltimer_callback_fn_t user_cb_fn;
-    void *user_cb_token;
-
-    /* set in fdt helper */
     volatile struct pwm_map *pwm_map;
-    pmem_region_t pmem;
-    irq_id_t irq_id;
-
-    /* set in setup */
     uint64_t time_h;
+    pwm_mode_t mode;
 } pwm_t;
 
 static UNUSED timer_properties_t pwm_properties = {
@@ -89,9 +82,9 @@
     .timeouts = true,
 };
 
-void pwm_start(pwm_t *pwm);
-void pwm_stop(pwm_t *pwm);
+int pwm_start(pwm_t *pwm);
+int pwm_stop(pwm_t *pwm);
+void pwm_handle_irq(pwm_t *pwm, uint32_t irq);
 uint64_t pwm_get_time(pwm_t *pwm);
 int pwm_set_timeout(pwm_t *pwm, uint64_t ns, bool periodic);
-int pwm_init(pwm_t *pwm, ps_io_ops_t ops, pwm_config_t config);
-void pwm_destroy(pwm_t *pwm);
+int pwm_init(pwm_t *pwm, pwm_config_t config);
diff --git a/libplatsupport/src/fdt.c b/libplatsupport/src/fdt.c
index dbe4243..e5af135 100644
--- a/libplatsupport/src/fdt.c
+++ b/libplatsupport/src/fdt.c
@@ -298,7 +298,7 @@
 static int irq_index_helper_walker(ps_irq_t irq, unsigned curr_num, size_t num_irqs, void *token)
 {
     index_helper_token_t *helper_token = token;
-    if (helper_token->desired_offset >= num_irqs) {
+    if (helper_token->desired_offset > num_irqs) {
         /* Bail early if we will never find it */
         return -ENOENT;
     }
@@ -338,7 +338,6 @@
 
     int error = ps_fdt_walk_irqs(&io_ops->io_fdt, cookie, irq_index_helper_walker, &token);
     if (error) {
-        assert(error <= PS_INVALID_IRQ_ID);
         return error;
     }
 
diff --git a/libplatsupport/src/ltimer.h b/libplatsupport/src/ltimer.h
index 5651d07..c6e6ee1 100644
--- a/libplatsupport/src/ltimer.h
+++ b/libplatsupport/src/ltimer.h
@@ -12,7 +12,6 @@
 
 #include <platsupport/ltimer.h>
 #include <platsupport/irq.h>
-#include <platsupport/fdt.h>
 
 /* Per-platform ltimer IRQ handling function type */
 typedef int (*ltimer_handle_irq_fn_t)(void *data, ps_irq_t *irq);
@@ -46,89 +45,3 @@
     error = acknowledge_fn(ack_data);
     assert(!error);
 }
-
-static int helper_fdt_alloc_simple(
-    ps_io_ops_t *ops, char *fdt_path,
-    unsigned reg_choice, unsigned irq_choice,
-    void **vmap, pmem_region_t *pmem, irq_id_t *irq_id,
-    irq_callback_fn_t handler, void *handler_token
-)
-{
-    assert(fdt_path != NULL);
-    assert(vmap != NULL && pmem != NULL && irq_id != NULL);
-
-    int error;
-    void *temp_vmap;
-    pmem_region_t temp_pmem;
-    irq_id_t temp_irq_id;
-
-    *vmap = NULL;
-    *irq_id = PS_INVALID_IRQ_ID;
-
-    /* Gather FDT info */
-    ps_fdt_cookie_t *cookie = NULL;
-    error = ps_fdt_read_path(&ops->io_fdt, &ops->malloc_ops, fdt_path, &cookie);
-    if (error) {
-        ZF_LOGE("Simple FDT helper failed to read path (%d, %s)", error, fdt_path);
-        return error;
-    }
-
-    temp_vmap = ps_fdt_index_map_register(ops, cookie, reg_choice, &temp_pmem);
-    if (temp_vmap == NULL) {
-        ZF_LOGE("Simple FDT helper failed to map registers");
-        return ENODEV;
-    }
-
-    temp_irq_id = ps_fdt_index_register_irq(ops, cookie, irq_choice, handler, handler_token);
-    if (temp_irq_id <= PS_INVALID_IRQ_ID) {
-        ZF_LOGE("Simple FDT helper failed to register irqs (%d)", temp_irq_id);
-        return temp_irq_id;
-    }
-
-    error = ps_fdt_cleanup_cookie(&ops->malloc_ops, cookie);
-    if (error) {
-        ZF_LOGE("Simple FDT helper failed to clean up cookie (%d)", error);
-        return error;
-    }
-
-    *vmap = temp_vmap;
-    *pmem = temp_pmem;
-    *irq_id = temp_irq_id;
-
-    return 0;
-}
-
-static int get_resolution_dummy(void *data, uint64_t *resolution)
-{
-    return ENOSYS;
-}
-
-static int create_ltimer_simple(
-    ltimer_t *ltimer, ps_io_ops_t ops, size_t sz,
-    int (*get_time)(void *data, uint64_t *time),
-    int (*set_timeout)(void *data, uint64_t ns, timeout_type_t type),
-    int (*reset)(void *data),
-    void (*destroy)(void *data)
-)
-{
-    assert(ltimer != NULL);
-
-    ltimer->get_time = get_time;
-    ltimer->get_resolution = get_resolution_dummy;
-    ltimer->set_timeout = set_timeout;
-    ltimer->reset = reset;
-    ltimer->destroy = destroy;
-    ltimer->get_nth_irq = NULL;
-    ltimer->get_nth_pmem = NULL;
-    ltimer->get_num_irqs = NULL;
-    ltimer->get_num_pmems = NULL;
-
-    int error = ps_calloc(&ops.malloc_ops, 1, sz, &ltimer->data);
-    if (error) {
-        ZF_LOGE("Unable to allocate ltimer data");
-        return error;
-    }
-    assert(ltimer->data != NULL);
-
-    return 0;
-}
diff --git a/libplatsupport/src/plat/hifive/ltimer.c b/libplatsupport/src/plat/hifive/ltimer.c
index 5614886..b0ccbb7 100644
--- a/libplatsupport/src/plat/hifive/ltimer.c
+++ b/libplatsupport/src/plat/hifive/ltimer.c
@@ -9,6 +9,10 @@
  *
  * @TAG(DATA61_BSD)
  */
+/* Implementation of a logical timer for HiFive Unleashed platform.
+ *
+ * We use two pwms: one for the time and the other for timeouts.
+ */
 #include <platsupport/timer.h>
 #include <platsupport/ltimer.h>
 #include <platsupport/plat/pwm.h>
@@ -17,26 +21,109 @@
 
 #include "../../ltimer.h"
 
-/* Implementation of a logical timer for HiFive Unleashed platform.
- *
- * We use two pwms: one for the time and the other for timeouts.
- *
- * De-multiplex common interface operations into two separate timers.
- */
+enum {
+    COUNTER_TIMER,
+    TIMEOUT_TIMER,
+    NUM_TIMERS
+};
 
 typedef struct {
+    pwm_t pwm;
+    void *vaddr;
+} pwm_ltimer_t;
+
+typedef struct {
+    pwm_ltimer_t pwm_ltimers[NUM_TIMERS];
+    irq_id_t timer_irq_ids[NUM_TIMERS];
+    timer_callback_data_t callback_datas[NUM_TIMERS];
+    ltimer_callback_fn_t user_callback;
+    void *user_callback_token;
     ps_io_ops_t ops;
-    pwm_t pwm_counter;
-    pwm_t pwm_timeout;
 } hifive_timers_t;
 
+static ps_irq_t irqs[] = {
+    {
+        .type = PS_INTERRUPT,
+        .irq.number = PWM0_INTERRUPT0
+
+    },
+    {
+        .type = PS_INTERRUPT,
+        .irq.number = PWM1_INTERRUPT0
+    },
+};
+
+static pmem_region_t pmems[] = {
+    {
+        .type = PMEM_TYPE_DEVICE,
+        .base_addr = PWM0_PADDR,
+        .length = PAGE_SIZE_4K
+    },
+    {
+        .type = PMEM_TYPE_DEVICE,
+        .base_addr = PWM1_PADDR,
+        .length = PAGE_SIZE_4K
+    }
+};
+
+#define N_IRQS ARRAY_SIZE(irqs)
+#define N_PMEMS ARRAY_SIZE(pmems)
+
+ size_t get_num_irqs(void *data)
+{
+    return N_IRQS;
+}
+
+static int get_nth_irq(void *data, size_t n, ps_irq_t *irq)
+{
+    assert(n < N_IRQS);
+
+    *irq = irqs[n];
+    return 0;
+}
+
+static size_t get_num_pmems(void *data)
+{
+    return N_PMEMS;
+}
+
+static int get_nth_pmem(void *data, size_t n, pmem_region_t *paddr)
+{
+    assert(n < N_PMEMS);
+    *paddr = pmems[n];
+    return 0;
+}
+
+static int ltimer_handle_irq(void *data, ps_irq_t *irq)
+{
+    assert(data != NULL);
+    hifive_timers_t *timers = data;
+    long irq_number = irq->irq.number;
+    ltimer_event_t event;
+    if (irq_number == PWM0_INTERRUPT0) {
+        pwm_handle_irq(&timers->pwm_ltimers[COUNTER_TIMER].pwm, irq->irq.number);
+        event = LTIMER_OVERFLOW_EVENT;
+    } else if (irq_number == PWM1_INTERRUPT0) {
+        pwm_handle_irq(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, irq->irq.number);
+        event = LTIMER_TIMEOUT_EVENT;
+    } else {
+        ZF_LOGE("Invalid IRQ number: %d received.\n", irq_number);
+    }
+
+    if (timers->user_callback) {
+        timers->user_callback(timers->user_callback_token, event);
+    }
+
+    return 0;
+}
+
 static int get_time(void *data, uint64_t *time)
 {
     assert(data != NULL);
     assert(time != NULL);
     hifive_timers_t *timers = data;
 
-    *time = pwm_get_time(&timers->pwm_counter);
+    *time = pwm_get_time(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
     return 0;
 }
 
@@ -52,16 +139,16 @@
 
     switch (type) {
     case TIMEOUT_ABSOLUTE: {
-        uint64_t time = pwm_get_time(&timers->pwm_counter);
+        uint64_t time = pwm_get_time(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
         if (time >= ns) {
             return ETIME;
         }
-        return pwm_set_timeout(&timers->pwm_timeout, ns - time, false);
+        return pwm_set_timeout(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, ns - time, false);
     }
     case TIMEOUT_RELATIVE:
-        return pwm_set_timeout(&timers->pwm_timeout, ns, false);
+        return pwm_set_timeout(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, ns, false);
     case TIMEOUT_PERIODIC:
-        return pwm_set_timeout(&timers->pwm_timeout, ns, true);
+        return pwm_set_timeout(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, ns, true);
     }
 
     return EINVAL;
@@ -71,67 +158,113 @@
 {
     assert(data != NULL);
     hifive_timers_t *timers = data;
-    pwm_stop(&timers->pwm_counter);
-    pwm_stop(&timers->pwm_timeout);
-    pwm_start(&timers->pwm_counter);
-    pwm_start(&timers->pwm_timeout);
+    pwm_stop(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
+    pwm_start(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
+    pwm_stop(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm);
+    pwm_start(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm);
     return 0;
 }
 
 static void destroy(void *data)
 {
-    assert(data != NULL);
+    assert(data);
     hifive_timers_t *timers = data;
-    pwm_destroy(&timers->pwm_counter);
-    pwm_destroy(&timers->pwm_timeout);
-    ps_free(&timers->ops.malloc_ops, sizeof(hifive_timers_t), timers);
+    for (int i = 0; i < NUM_TIMERS; i++) {
+        if (timers->pwm_ltimers[i].vaddr) {
+            pwm_stop(&timers->pwm_ltimers[i].pwm);
+            ps_pmem_unmap(&timers->ops, pmems[i], timers->pwm_ltimers[i].vaddr);
+        }
+        if (timers->timer_irq_ids[i] > PS_INVALID_IRQ_ID) {
+            int error = ps_irq_unregister(&timers->ops.irq_ops, timers->timer_irq_ids[i]);
+            ZF_LOGF_IF(error, "Failed to unregister IRQ");
+        }
+    }
+    ps_free(&timers->ops.malloc_ops, sizeof(timers), timers);
+}
+
+static int create_ltimer(ltimer_t *ltimer, ps_io_ops_t ops)
+{
+    assert(ltimer != NULL);
+    ltimer->get_time = get_time;
+    ltimer->get_resolution = get_resolution;
+    ltimer->set_timeout = set_timeout;
+    ltimer->reset = reset;
+    ltimer->destroy = destroy;
+
+    int error = ps_calloc(&ops.malloc_ops, 1, sizeof(hifive_timers_t), &ltimer->data);
+    if (error) {
+        return error;
+    }
+    assert(ltimer->data != NULL);
+
+    return 0;
+}
+
+static int init_ltimer(ltimer_t *ltimer)
+{
+    assert(ltimer != NULL);
+    hifive_timers_t *timers = ltimer->data;
+
+    /* setup pwm */
+    pwm_config_t config_counter = {
+        .vaddr = timers->pwm_ltimers[COUNTER_TIMER].vaddr,
+        .mode = UPCOUNTER,
+    };
+    pwm_config_t config_timeout = {
+        .vaddr = timers->pwm_ltimers[TIMEOUT_TIMER].vaddr,
+        .mode = TIMEOUT,
+    };
+
+    pwm_init(&timers->pwm_ltimers[COUNTER_TIMER].pwm, config_counter);
+    pwm_init(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, config_timeout);
+    pwm_start(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
+    return 0;
 }
 
 int ltimer_default_init(ltimer_t *ltimer, ps_io_ops_t ops, ltimer_callback_fn_t callback, void *callback_token)
 {
-    int error;
 
-    error = create_ltimer_simple(
-                ltimer, ops, sizeof(hifive_timers_t),
-                get_time, set_timeout, reset, destroy
-            );
+    int error = ltimer_default_describe(ltimer, ops);
     if (error) {
-        /* failed to create, no need to destroy */
-        ZF_LOGE("Failed to create ltimer using simple helper");
+        return error;
+    }
+
+    error = create_ltimer(ltimer, ops);
+    if (error) {
         return error;
     }
 
     hifive_timers_t *timers = ltimer->data;
     timers->ops = ops;
-
-    pwm_config_t config = {
-        .user_cb_fn = callback,
-        .user_cb_token = callback_token,
-        .fdt_path = PWM0_PATH,
-        .mode = UPCOUNTER,
-    };
-
-    error = pwm_init(&timers->pwm_counter, ops, config);
-    if (error) {
-        ZF_LOGE("Failed to init pwm timer (UPCOUNTER)");
-        destroy(timers);
-        return error;
+    timers->user_callback = callback;
+    timers->user_callback_token = callback_token;
+    for (int i = 0; i < NUM_TIMERS; i++) {
+        timers->timer_irq_ids[i] = PS_INVALID_IRQ_ID;
     }
 
-    config.fdt_path = PWM1_PATH;
-    config.mode = TIMEOUT;
+    for (int i = 0; i < NUM_TIMERS; i++) {
+        /* map the registers we need */
+        timers->pwm_ltimers[i].vaddr = ps_pmem_map(&ops, pmems[i], false, PS_MEM_NORMAL);
+        if (timers->pwm_ltimers[i].vaddr == NULL) {
+            destroy(ltimer->data);
+            return ENOMEM;
+        }
 
-    error = pwm_init(&timers->pwm_timeout, ops, config);
-    if (error) {
-        ZF_LOGE("Failed to init pwm timer (TIMEOUT)");
-        destroy(timers);
-        return error;
+        /* register the IRQs we need */
+        timers->callback_datas[i].ltimer = ltimer;
+        timers->callback_datas[i].irq = &irqs[i];
+        timers->callback_datas[i].irq_handler = ltimer_handle_irq;
+        timers->timer_irq_ids[i] = ps_irq_register(&ops.irq_ops, irqs[i], handle_irq_wrapper,
+                                                   &timers->callback_datas[i]);
+        if (timers->timer_irq_ids[i] < 0) {
+            destroy(ltimer->data);
+            return EIO;
+        }
     }
 
-    error = reset(timers);
+    error = init_ltimer(ltimer);
     if (error) {
-        ZF_LOGE("Failed to reset pwm timers during init");
-        destroy(timers);
+        destroy(ltimer->data);
         return error;
     }
 
@@ -139,10 +272,16 @@
     return 0;
 }
 
-/* This function is intended to be deleted,
- * this is just left here for now so that stuff can compile */
 int ltimer_default_describe(ltimer_t *ltimer, ps_io_ops_t ops)
 {
-    ZF_LOGE("get_(nth/num)_(irqs/pmems) are not valid");
-    return EINVAL;
+    if (ltimer == NULL) {
+        ZF_LOGE("Timer is NULL!");
+        return EINVAL;
+    }
+
+    ltimer->get_num_irqs = get_num_irqs;
+    ltimer->get_nth_irq = get_nth_irq;
+    ltimer->get_num_pmems = get_num_pmems;
+    ltimer->get_nth_pmem = get_nth_pmem;
+    return 0;
 }
diff --git a/libplatsupport/src/plat/hifive/pwm.c b/libplatsupport/src/plat/hifive/pwm.c
index 1dc200b..87bee7f 100644
--- a/libplatsupport/src/plat/hifive/pwm.c
+++ b/libplatsupport/src/plat/hifive/pwm.c
@@ -18,10 +18,8 @@
 #include <utils/util.h>
 
 #include <platsupport/timer.h>
-#include <platsupport/ltimer.h>
 #include <platsupport/plat/pwm.h>
 
-#include "../../ltimer.h"
 
 #define PWMSCALE_MASK MASK(4)
 #define PWMSTICKY BIT(8)
@@ -39,24 +37,25 @@
 #define MAX_TIMEOUT_NS (BIT(31) * (NS_IN_S / PWM_INPUT_FREQ))
 
 
-void pwm_start(pwm_t *pwm)
+int pwm_start(pwm_t *pwm)
 {
-    assert(pwm != NULL && pwm->pwm_map != NULL);
     pwm->pwm_map->pwmcfg |= PWMENALWAYS;
+    return 0;
 }
 
-void pwm_stop(pwm_t *pwm)
+int pwm_stop(pwm_t *pwm)
 {
-    assert(pwm != NULL && pwm->pwm_map != NULL);
     /* Disable timer. */
     pwm->pwm_map->pwmcmp0 = PWMCMP_MASK;
-    pwm->pwm_map->pwmcfg &= ~(PWMENALWAYS | PWMENONESHOT | PWMCMP0IP | PWMCMP1IP | PWMCMP2IP | PWMCMP3IP);
+    pwm->pwm_map->pwmcfg &= ~(PWMENALWAYS|PWMENONESHOT|PWMCMP0IP|PWMCMP1IP|PWMCMP2IP|PWMCMP3IP);
     pwm->pwm_map->pwmcount = 0;
+
+    return 0;
 }
 
 int pwm_set_timeout(pwm_t *pwm, uint64_t ns, bool periodic)
 {
-    if (pwm->mode == UPCOUNTER) {
+    if(pwm->mode == UPCOUNTER) {
         ZF_LOGE("pwm is in UPCOUNTER mode and doesn't support setting timeouts.");
         return -1;
     }
@@ -76,7 +75,7 @@
      */
     size_t prescale = num_ticks >> (PWMCMP_WIDTH);
     size_t base_2 = LOG_BASE_2(prescale);
-    if (BIT(base_2) < prescale) {
+    if (BIT(base_2)< prescale) {
         base_2++;
     }
     assert(prescale < BIT(PWMSCALE_MASK));
@@ -84,9 +83,9 @@
     /* There will be a loss of resolution by this shift.
      * We add 1 so that we sleep for at least as long as needed.
      */
-    pwm->pwm_map->pwmcmp0 = (num_ticks >> base_2) + 1;
+    pwm->pwm_map->pwmcmp0 = (num_ticks >> base_2) +1;
     /* assert we didn't overflow... */
-    assert((num_ticks >> base_2) + 1);
+    assert((num_ticks >> base_2) +1);
     /* Reset the counter mode and prescaler, for some reason this doesn't work in pwm_stop */
     pwm->pwm_map->pwmcfg &= ~(PWMSCALE_MASK);
     if (periodic) {
@@ -98,20 +97,13 @@
     return 0;
 }
 
-static void pwm_handle_irq(void *data, ps_irq_acknowledge_fn_t acknowledge_fn, void *ack_data)
+void pwm_handle_irq(pwm_t *pwm, uint32_t irq)
 {
-    pwm_t *pwm = data;
-    if (pwm->mode == UPCOUNTER) {
+    if(pwm->mode == UPCOUNTER) {
         pwm->time_h++;
     }
-    pwm->pwm_map->pwmcfg &= ~(PWMCMP0IP | PWMCMP1IP | PWMCMP2IP | PWMCMP3IP);
-    ZF_LOGF_IF(acknowledge_fn(ack_data), "Failed to acknowledge the timer's interrupts");
-    if (pwm->user_cb_fn) {
-        pwm->user_cb_fn(
-            pwm->user_cb_token,
-            pwm->mode == UPCOUNTER ? LTIMER_OVERFLOW_EVENT : LTIMER_TIMEOUT_EVENT
-        );
-    }
+
+    pwm->pwm_map->pwmcfg &= ~(PWMCMP0IP|PWMCMP1IP|PWMCMP2IP|PWMCMP3IP);
 }
 
 uint64_t pwm_get_time(pwm_t *pwm)
@@ -127,61 +119,19 @@
     return time;
 }
 
-static void pwm_setup(pwm_t *pwm)
+int pwm_init(pwm_t *pwm, pwm_config_t config)
 {
-    assert(pwm != NULL);
-    pwm->time_h = 0;
+    pwm->pwm_map = (volatile struct pwm_map*) config.vaddr;
     uint8_t scale = 0;
-    if (pwm->mode == UPCOUNTER) {
+    if (config.mode == UPCOUNTER) {
         scale = PWMSCALE_MASK;
     }
+    pwm->mode = config.mode;
     pwm->pwm_map->pwmcmp0 = PWMCMP_MASK;
     pwm->pwm_map->pwmcmp1 = PWMCMP_MASK;
     pwm->pwm_map->pwmcmp2 = PWMCMP_MASK;
     pwm->pwm_map->pwmcmp3 = PWMCMP_MASK;
-    pwm->pwm_map->pwmcfg = (scale & PWMSCALE_MASK) | PWMZEROCMP | PWMSTICKY;
-}
-
-void pwm_destroy(pwm_t *pwm)
-{
-    int error;
-    if (pwm->irq_id != PS_INVALID_IRQ_ID) {
-        error = ps_irq_unregister(&pwm->ops.irq_ops, pwm->irq_id);
-        ZF_LOGF_IF(error, "Failed to unregister IRQ");
-    }
-    if (pwm->pwm_map != NULL) {
-        pwm_stop(pwm);
-        ps_pmem_unmap(&pwm->ops, pwm->pmem, (void *) pwm->pwm_map);
-    }
-}
-
-int pwm_init(pwm_t *pwm, ps_io_ops_t ops, pwm_config_t config)
-{
-    int error;
-
-    if (pwm == NULL) {
-        return EINVAL;
-    }
-
-    /* Set up pwm */
-    pwm->ops = ops;
-    pwm->mode = config.mode;
-    pwm->user_cb_fn = config.user_cb_fn;
-    pwm->user_cb_token = config.user_cb_token;
-
-    error = helper_fdt_alloc_simple(
-                &ops, config.fdt_path,
-                PWM_REG_CHOICE, PWM_IRQ_CHOICE,
-                (void *) &pwm->pwm_map, &pwm->pmem, &pwm->irq_id,
-                pwm_handle_irq, pwm
-            );
-    if (error) {
-        ZF_LOGE("Failed to init pwm timer using simple helper");
-        pwm_destroy(pwm);
-        return error;
-    }
-
-    pwm_setup(pwm);
+    pwm->pwm_map->pwmcfg =  (scale & PWMSCALE_MASK) | PWMZEROCMP | PWMSTICKY;
 
     return 0;
 }