1 /*
2  * ARM CMSDK APB timer emulation
3  *
4  * Copyright (c) 2017 Linaro Limited
5  * Written by Peter Maydell
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 or
9  *  (at your option) any later version.
10  */
11 
12 #ifndef CMSDK_APB_TIMER_H
13 #define CMSDK_APB_TIMER_H
14 
15 #include "hw/qdev-properties.h"
16 #include "hw/sysbus.h"
17 #include "hw/ptimer.h"
18 
19 #define TYPE_CMSDK_APB_TIMER "cmsdk-apb-timer"
20 #define CMSDK_APB_TIMER(obj) OBJECT_CHECK(CMSDKAPBTIMER, (obj), \
21                                          TYPE_CMSDK_APB_TIMER)
22 
23 typedef struct {
24     /*< private >*/
25     SysBusDevice parent_obj;
26 
27     /*< public >*/
28     MemoryRegion iomem;
29     qemu_irq timerint;
30     uint32_t pclk_frq;
31     struct ptimer_state *timer;
32 
33     uint32_t ctrl;
34     uint32_t value;
35     uint32_t reload;
36     uint32_t intstatus;
37 } CMSDKAPBTIMER;
38 
39 /**
40  * cmsdk_apb_timer_create - convenience function to create TYPE_CMSDK_APB_TIMER
41  * @addr: location in system memory to map registers
42  * @pclk_frq: frequency in Hz of the PCLK clock (used for calculating baud rate)
43  */
44 static inline DeviceState *cmsdk_apb_timer_create(hwaddr addr,
45                                                  qemu_irq timerint,
46                                                  uint32_t pclk_frq)
47 {
48     DeviceState *dev;
49     SysBusDevice *s;
50 
51     dev = qdev_create(NULL, TYPE_CMSDK_APB_TIMER);
52     s = SYS_BUS_DEVICE(dev);
53     qdev_prop_set_uint32(dev, "pclk-frq", pclk_frq);
54     qdev_init_nofail(dev);
55     sysbus_mmio_map(s, 0, addr);
56     sysbus_connect_irq(s, 0, timerint);
57     return dev;
58 }
59 
60 #endif
61