1*5184f4bfSNick Hawkins // SPDX-License-Identifier: GPL-2.0 2*5184f4bfSNick Hawkins /* Copyright (C) 2022 Hewlett-Packard Enterprise Development Company, L.P. */ 3*5184f4bfSNick Hawkins 4*5184f4bfSNick Hawkins #include <linux/clk.h> 5*5184f4bfSNick Hawkins #include <linux/clockchips.h> 6*5184f4bfSNick Hawkins #include <linux/clocksource.h> 7*5184f4bfSNick Hawkins #include <linux/interrupt.h> 8*5184f4bfSNick Hawkins #include <linux/of_address.h> 9*5184f4bfSNick Hawkins #include <linux/of_irq.h> 10*5184f4bfSNick Hawkins #include <linux/of_platform.h> 11*5184f4bfSNick Hawkins #include <linux/sched_clock.h> 12*5184f4bfSNick Hawkins 13*5184f4bfSNick Hawkins #define TIMER0_FREQ 1000000 14*5184f4bfSNick Hawkins #define GXP_TIMER_CNT_OFS 0x00 15*5184f4bfSNick Hawkins #define GXP_TIMESTAMP_OFS 0x08 16*5184f4bfSNick Hawkins #define GXP_TIMER_CTRL_OFS 0x14 17*5184f4bfSNick Hawkins 18*5184f4bfSNick Hawkins /* TCS Stands for Timer Control/Status: these are masks to be used in */ 19*5184f4bfSNick Hawkins /* the Timer Count Registers */ 20*5184f4bfSNick Hawkins #define MASK_TCS_ENABLE 0x01 21*5184f4bfSNick Hawkins #define MASK_TCS_PERIOD 0x02 22*5184f4bfSNick Hawkins #define MASK_TCS_RELOAD 0x04 23*5184f4bfSNick Hawkins #define MASK_TCS_TC 0x80 24*5184f4bfSNick Hawkins 25*5184f4bfSNick Hawkins struct gxp_timer { 26*5184f4bfSNick Hawkins void __iomem *counter; 27*5184f4bfSNick Hawkins void __iomem *control; 28*5184f4bfSNick Hawkins struct clock_event_device evt; 29*5184f4bfSNick Hawkins }; 30*5184f4bfSNick Hawkins 31*5184f4bfSNick Hawkins static struct gxp_timer *gxp_timer; 32*5184f4bfSNick Hawkins 33*5184f4bfSNick Hawkins static void __iomem *system_clock __ro_after_init; 34*5184f4bfSNick Hawkins 35*5184f4bfSNick Hawkins static inline struct gxp_timer *to_gxp_timer(struct clock_event_device *evt_dev) 36*5184f4bfSNick Hawkins { 37*5184f4bfSNick Hawkins return container_of(evt_dev, struct gxp_timer, evt); 38*5184f4bfSNick Hawkins } 39*5184f4bfSNick Hawkins 40*5184f4bfSNick Hawkins static u64 notrace gxp_sched_read(void) 41*5184f4bfSNick Hawkins { 42*5184f4bfSNick Hawkins return readl_relaxed(system_clock); 43*5184f4bfSNick Hawkins } 44*5184f4bfSNick Hawkins 45*5184f4bfSNick Hawkins static int gxp_time_set_next_event(unsigned long event, struct clock_event_device *evt_dev) 46*5184f4bfSNick Hawkins { 47*5184f4bfSNick Hawkins struct gxp_timer *timer = to_gxp_timer(evt_dev); 48*5184f4bfSNick Hawkins 49*5184f4bfSNick Hawkins /* Stop counting and disable interrupt before updating */ 50*5184f4bfSNick Hawkins writeb_relaxed(MASK_TCS_TC, timer->control); 51*5184f4bfSNick Hawkins writel_relaxed(event, timer->counter); 52*5184f4bfSNick Hawkins writeb_relaxed(MASK_TCS_TC | MASK_TCS_ENABLE, timer->control); 53*5184f4bfSNick Hawkins 54*5184f4bfSNick Hawkins return 0; 55*5184f4bfSNick Hawkins } 56*5184f4bfSNick Hawkins 57*5184f4bfSNick Hawkins static irqreturn_t gxp_timer_interrupt(int irq, void *dev_id) 58*5184f4bfSNick Hawkins { 59*5184f4bfSNick Hawkins struct gxp_timer *timer = (struct gxp_timer *)dev_id; 60*5184f4bfSNick Hawkins 61*5184f4bfSNick Hawkins if (!(readb_relaxed(timer->control) & MASK_TCS_TC)) 62*5184f4bfSNick Hawkins return IRQ_NONE; 63*5184f4bfSNick Hawkins 64*5184f4bfSNick Hawkins writeb_relaxed(MASK_TCS_TC, timer->control); 65*5184f4bfSNick Hawkins 66*5184f4bfSNick Hawkins timer->evt.event_handler(&timer->evt); 67*5184f4bfSNick Hawkins 68*5184f4bfSNick Hawkins return IRQ_HANDLED; 69*5184f4bfSNick Hawkins } 70*5184f4bfSNick Hawkins 71*5184f4bfSNick Hawkins static int __init gxp_timer_init(struct device_node *node) 72*5184f4bfSNick Hawkins { 73*5184f4bfSNick Hawkins void __iomem *base; 74*5184f4bfSNick Hawkins struct clk *clk; 75*5184f4bfSNick Hawkins u32 freq; 76*5184f4bfSNick Hawkins int ret, irq; 77*5184f4bfSNick Hawkins 78*5184f4bfSNick Hawkins gxp_timer = kzalloc(sizeof(*gxp_timer), GFP_KERNEL); 79*5184f4bfSNick Hawkins if (!gxp_timer) { 80*5184f4bfSNick Hawkins ret = -ENOMEM; 81*5184f4bfSNick Hawkins pr_err("Can't allocate gxp_timer"); 82*5184f4bfSNick Hawkins return ret; 83*5184f4bfSNick Hawkins } 84*5184f4bfSNick Hawkins 85*5184f4bfSNick Hawkins clk = of_clk_get(node, 0); 86*5184f4bfSNick Hawkins if (IS_ERR(clk)) { 87*5184f4bfSNick Hawkins ret = (int)PTR_ERR(clk); 88*5184f4bfSNick Hawkins pr_err("%pOFn clock not found: %d\n", node, ret); 89*5184f4bfSNick Hawkins goto err_free; 90*5184f4bfSNick Hawkins } 91*5184f4bfSNick Hawkins 92*5184f4bfSNick Hawkins ret = clk_prepare_enable(clk); 93*5184f4bfSNick Hawkins if (ret) { 94*5184f4bfSNick Hawkins pr_err("%pOFn clock enable failed: %d\n", node, ret); 95*5184f4bfSNick Hawkins goto err_clk_enable; 96*5184f4bfSNick Hawkins } 97*5184f4bfSNick Hawkins 98*5184f4bfSNick Hawkins base = of_iomap(node, 0); 99*5184f4bfSNick Hawkins if (!base) { 100*5184f4bfSNick Hawkins ret = -ENXIO; 101*5184f4bfSNick Hawkins pr_err("Can't map timer base registers"); 102*5184f4bfSNick Hawkins goto err_iomap; 103*5184f4bfSNick Hawkins } 104*5184f4bfSNick Hawkins 105*5184f4bfSNick Hawkins /* Set the offsets to the clock register and timer registers */ 106*5184f4bfSNick Hawkins gxp_timer->counter = base + GXP_TIMER_CNT_OFS; 107*5184f4bfSNick Hawkins gxp_timer->control = base + GXP_TIMER_CTRL_OFS; 108*5184f4bfSNick Hawkins system_clock = base + GXP_TIMESTAMP_OFS; 109*5184f4bfSNick Hawkins 110*5184f4bfSNick Hawkins gxp_timer->evt.name = node->name; 111*5184f4bfSNick Hawkins gxp_timer->evt.rating = 300; 112*5184f4bfSNick Hawkins gxp_timer->evt.features = CLOCK_EVT_FEAT_ONESHOT; 113*5184f4bfSNick Hawkins gxp_timer->evt.set_next_event = gxp_time_set_next_event; 114*5184f4bfSNick Hawkins gxp_timer->evt.cpumask = cpumask_of(0); 115*5184f4bfSNick Hawkins 116*5184f4bfSNick Hawkins irq = irq_of_parse_and_map(node, 0); 117*5184f4bfSNick Hawkins if (irq <= 0) { 118*5184f4bfSNick Hawkins ret = -EINVAL; 119*5184f4bfSNick Hawkins pr_err("GXP Timer Can't parse IRQ %d", irq); 120*5184f4bfSNick Hawkins goto err_exit; 121*5184f4bfSNick Hawkins } 122*5184f4bfSNick Hawkins 123*5184f4bfSNick Hawkins freq = clk_get_rate(clk); 124*5184f4bfSNick Hawkins 125*5184f4bfSNick Hawkins ret = clocksource_mmio_init(system_clock, node->name, freq, 126*5184f4bfSNick Hawkins 300, 32, clocksource_mmio_readl_up); 127*5184f4bfSNick Hawkins if (ret) { 128*5184f4bfSNick Hawkins pr_err("%pOFn init clocksource failed: %d", node, ret); 129*5184f4bfSNick Hawkins goto err_exit; 130*5184f4bfSNick Hawkins } 131*5184f4bfSNick Hawkins 132*5184f4bfSNick Hawkins sched_clock_register(gxp_sched_read, 32, freq); 133*5184f4bfSNick Hawkins 134*5184f4bfSNick Hawkins irq = irq_of_parse_and_map(node, 0); 135*5184f4bfSNick Hawkins if (irq <= 0) { 136*5184f4bfSNick Hawkins ret = -EINVAL; 137*5184f4bfSNick Hawkins pr_err("%pOFn Can't parse IRQ %d", node, irq); 138*5184f4bfSNick Hawkins goto err_exit; 139*5184f4bfSNick Hawkins } 140*5184f4bfSNick Hawkins 141*5184f4bfSNick Hawkins clockevents_config_and_register(&gxp_timer->evt, TIMER0_FREQ, 142*5184f4bfSNick Hawkins 0xf, 0xffffffff); 143*5184f4bfSNick Hawkins 144*5184f4bfSNick Hawkins ret = request_irq(irq, gxp_timer_interrupt, IRQF_TIMER | IRQF_SHARED, 145*5184f4bfSNick Hawkins node->name, gxp_timer); 146*5184f4bfSNick Hawkins if (ret) { 147*5184f4bfSNick Hawkins pr_err("%pOFn request_irq() failed: %d", node, ret); 148*5184f4bfSNick Hawkins goto err_exit; 149*5184f4bfSNick Hawkins } 150*5184f4bfSNick Hawkins 151*5184f4bfSNick Hawkins pr_debug("gxp: system timer (irq = %d)\n", irq); 152*5184f4bfSNick Hawkins return 0; 153*5184f4bfSNick Hawkins 154*5184f4bfSNick Hawkins err_exit: 155*5184f4bfSNick Hawkins iounmap(base); 156*5184f4bfSNick Hawkins err_iomap: 157*5184f4bfSNick Hawkins clk_disable_unprepare(clk); 158*5184f4bfSNick Hawkins err_clk_enable: 159*5184f4bfSNick Hawkins clk_put(clk); 160*5184f4bfSNick Hawkins err_free: 161*5184f4bfSNick Hawkins kfree(gxp_timer); 162*5184f4bfSNick Hawkins return ret; 163*5184f4bfSNick Hawkins } 164*5184f4bfSNick Hawkins 165*5184f4bfSNick Hawkins /* 166*5184f4bfSNick Hawkins * This probe gets called after the timer is already up and running. This will create 167*5184f4bfSNick Hawkins * the watchdog device as a child since the registers are shared. 168*5184f4bfSNick Hawkins */ 169*5184f4bfSNick Hawkins 170*5184f4bfSNick Hawkins static int gxp_timer_probe(struct platform_device *pdev) 171*5184f4bfSNick Hawkins { 172*5184f4bfSNick Hawkins struct platform_device *gxp_watchdog_device; 173*5184f4bfSNick Hawkins struct device *dev = &pdev->dev; 174*5184f4bfSNick Hawkins 175*5184f4bfSNick Hawkins if (!gxp_timer) { 176*5184f4bfSNick Hawkins pr_err("Gxp Timer not initialized, cannot create watchdog"); 177*5184f4bfSNick Hawkins return -ENOMEM; 178*5184f4bfSNick Hawkins } 179*5184f4bfSNick Hawkins 180*5184f4bfSNick Hawkins gxp_watchdog_device = platform_device_alloc("gxp-wdt", -1); 181*5184f4bfSNick Hawkins if (!gxp_watchdog_device) { 182*5184f4bfSNick Hawkins pr_err("Timer failed to allocate gxp-wdt"); 183*5184f4bfSNick Hawkins return -ENOMEM; 184*5184f4bfSNick Hawkins } 185*5184f4bfSNick Hawkins 186*5184f4bfSNick Hawkins /* Pass the base address (counter) as platform data and nothing else */ 187*5184f4bfSNick Hawkins gxp_watchdog_device->dev.platform_data = gxp_timer->counter; 188*5184f4bfSNick Hawkins gxp_watchdog_device->dev.parent = dev; 189*5184f4bfSNick Hawkins 190*5184f4bfSNick Hawkins return platform_device_add(gxp_watchdog_device); 191*5184f4bfSNick Hawkins } 192*5184f4bfSNick Hawkins 193*5184f4bfSNick Hawkins static const struct of_device_id gxp_timer_of_match[] = { 194*5184f4bfSNick Hawkins { .compatible = "hpe,gxp-timer", }, 195*5184f4bfSNick Hawkins {}, 196*5184f4bfSNick Hawkins }; 197*5184f4bfSNick Hawkins 198*5184f4bfSNick Hawkins static struct platform_driver gxp_timer_driver = { 199*5184f4bfSNick Hawkins .probe = gxp_timer_probe, 200*5184f4bfSNick Hawkins .driver = { 201*5184f4bfSNick Hawkins .name = "gxp-timer", 202*5184f4bfSNick Hawkins .of_match_table = gxp_timer_of_match, 203*5184f4bfSNick Hawkins .suppress_bind_attrs = true, 204*5184f4bfSNick Hawkins }, 205*5184f4bfSNick Hawkins }; 206*5184f4bfSNick Hawkins 207*5184f4bfSNick Hawkins builtin_platform_driver(gxp_timer_driver); 208*5184f4bfSNick Hawkins 209*5184f4bfSNick Hawkins TIMER_OF_DECLARE(gxp, "hpe,gxp-timer", gxp_timer_init); 210