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 #include "hw/clock.h" 19 #include "qom/object.h" 20 21 #define TYPE_CMSDK_APB_TIMER "cmsdk-apb-timer" 22 OBJECT_DECLARE_SIMPLE_TYPE(CMSDKAPBTimer, CMSDK_APB_TIMER) 23 24 /* 25 * QEMU interface: 26 * + QOM property "pclk-frq": frequency at which the timer is clocked 27 * + Clock input "pclk": clock for the timer 28 * + sysbus MMIO region 0: the register bank 29 * + sysbus IRQ 0: timer interrupt TIMERINT 30 */ 31 struct CMSDKAPBTimer { 32 /*< private >*/ 33 SysBusDevice parent_obj; 34 35 /*< public >*/ 36 MemoryRegion iomem; 37 qemu_irq timerint; 38 uint32_t pclk_frq; 39 struct ptimer_state *timer; 40 Clock *pclk; 41 42 uint32_t ctrl; 43 uint32_t value; 44 uint32_t reload; 45 uint32_t intstatus; 46 }; 47 48 /** 49 * cmsdk_apb_timer_create - convenience function to create TYPE_CMSDK_APB_TIMER 50 * @addr: location in system memory to map registers 51 * @pclk_frq: frequency in Hz of the PCLK clock (used for calculating baud rate) 52 */ 53 static inline DeviceState *cmsdk_apb_timer_create(hwaddr addr, 54 qemu_irq timerint, 55 uint32_t pclk_frq) 56 { 57 DeviceState *dev; 58 SysBusDevice *s; 59 60 dev = qdev_new(TYPE_CMSDK_APB_TIMER); 61 s = SYS_BUS_DEVICE(dev); 62 qdev_prop_set_uint32(dev, "pclk-frq", pclk_frq); 63 sysbus_realize_and_unref(s, &error_fatal); 64 sysbus_mmio_map(s, 0, addr); 65 sysbus_connect_irq(s, 0, timerint); 66 return dev; 67 } 68 69 #endif 70