18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SuperH Timer Support - MTU2 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Magnus Damm 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk.h> 98c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 108c2ecf20Sopenharmony_ci#include <linux/delay.h> 118c2ecf20Sopenharmony_ci#include <linux/err.h> 128c2ecf20Sopenharmony_ci#include <linux/init.h> 138c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 148c2ecf20Sopenharmony_ci#include <linux/io.h> 158c2ecf20Sopenharmony_ci#include <linux/ioport.h> 168c2ecf20Sopenharmony_ci#include <linux/irq.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/of.h> 198c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 208c2ecf20Sopenharmony_ci#include <linux/pm_domain.h> 218c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 228c2ecf20Sopenharmony_ci#include <linux/sh_timer.h> 238c2ecf20Sopenharmony_ci#include <linux/slab.h> 248c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#ifdef CONFIG_SUPERH 278c2ecf20Sopenharmony_ci#include <asm/platform_early.h> 288c2ecf20Sopenharmony_ci#endif 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct sh_mtu2_device; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistruct sh_mtu2_channel { 338c2ecf20Sopenharmony_ci struct sh_mtu2_device *mtu; 348c2ecf20Sopenharmony_ci unsigned int index; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci void __iomem *base; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci struct clock_event_device ced; 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct sh_mtu2_device { 428c2ecf20Sopenharmony_ci struct platform_device *pdev; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci void __iomem *mapbase; 458c2ecf20Sopenharmony_ci struct clk *clk; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci raw_spinlock_t lock; /* Protect the shared registers */ 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci struct sh_mtu2_channel *channels; 508c2ecf20Sopenharmony_ci unsigned int num_channels; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci bool has_clockevent; 538c2ecf20Sopenharmony_ci}; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define TSTR -1 /* shared register */ 568c2ecf20Sopenharmony_ci#define TCR 0 /* channel register */ 578c2ecf20Sopenharmony_ci#define TMDR 1 /* channel register */ 588c2ecf20Sopenharmony_ci#define TIOR 2 /* channel register */ 598c2ecf20Sopenharmony_ci#define TIER 3 /* channel register */ 608c2ecf20Sopenharmony_ci#define TSR 4 /* channel register */ 618c2ecf20Sopenharmony_ci#define TCNT 5 /* channel register */ 628c2ecf20Sopenharmony_ci#define TGR 6 /* channel register */ 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define TCR_CCLR_NONE (0 << 5) 658c2ecf20Sopenharmony_ci#define TCR_CCLR_TGRA (1 << 5) 668c2ecf20Sopenharmony_ci#define TCR_CCLR_TGRB (2 << 5) 678c2ecf20Sopenharmony_ci#define TCR_CCLR_SYNC (3 << 5) 688c2ecf20Sopenharmony_ci#define TCR_CCLR_TGRC (5 << 5) 698c2ecf20Sopenharmony_ci#define TCR_CCLR_TGRD (6 << 5) 708c2ecf20Sopenharmony_ci#define TCR_CCLR_MASK (7 << 5) 718c2ecf20Sopenharmony_ci#define TCR_CKEG_RISING (0 << 3) 728c2ecf20Sopenharmony_ci#define TCR_CKEG_FALLING (1 << 3) 738c2ecf20Sopenharmony_ci#define TCR_CKEG_BOTH (2 << 3) 748c2ecf20Sopenharmony_ci#define TCR_CKEG_MASK (3 << 3) 758c2ecf20Sopenharmony_ci/* Values 4 to 7 are channel-dependent */ 768c2ecf20Sopenharmony_ci#define TCR_TPSC_P1 (0 << 0) 778c2ecf20Sopenharmony_ci#define TCR_TPSC_P4 (1 << 0) 788c2ecf20Sopenharmony_ci#define TCR_TPSC_P16 (2 << 0) 798c2ecf20Sopenharmony_ci#define TCR_TPSC_P64 (3 << 0) 808c2ecf20Sopenharmony_ci#define TCR_TPSC_CH0_TCLKA (4 << 0) 818c2ecf20Sopenharmony_ci#define TCR_TPSC_CH0_TCLKB (5 << 0) 828c2ecf20Sopenharmony_ci#define TCR_TPSC_CH0_TCLKC (6 << 0) 838c2ecf20Sopenharmony_ci#define TCR_TPSC_CH0_TCLKD (7 << 0) 848c2ecf20Sopenharmony_ci#define TCR_TPSC_CH1_TCLKA (4 << 0) 858c2ecf20Sopenharmony_ci#define TCR_TPSC_CH1_TCLKB (5 << 0) 868c2ecf20Sopenharmony_ci#define TCR_TPSC_CH1_P256 (6 << 0) 878c2ecf20Sopenharmony_ci#define TCR_TPSC_CH1_TCNT2 (7 << 0) 888c2ecf20Sopenharmony_ci#define TCR_TPSC_CH2_TCLKA (4 << 0) 898c2ecf20Sopenharmony_ci#define TCR_TPSC_CH2_TCLKB (5 << 0) 908c2ecf20Sopenharmony_ci#define TCR_TPSC_CH2_TCLKC (6 << 0) 918c2ecf20Sopenharmony_ci#define TCR_TPSC_CH2_P1024 (7 << 0) 928c2ecf20Sopenharmony_ci#define TCR_TPSC_CH34_P256 (4 << 0) 938c2ecf20Sopenharmony_ci#define TCR_TPSC_CH34_P1024 (5 << 0) 948c2ecf20Sopenharmony_ci#define TCR_TPSC_CH34_TCLKA (6 << 0) 958c2ecf20Sopenharmony_ci#define TCR_TPSC_CH34_TCLKB (7 << 0) 968c2ecf20Sopenharmony_ci#define TCR_TPSC_MASK (7 << 0) 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci#define TMDR_BFE (1 << 6) 998c2ecf20Sopenharmony_ci#define TMDR_BFB (1 << 5) 1008c2ecf20Sopenharmony_ci#define TMDR_BFA (1 << 4) 1018c2ecf20Sopenharmony_ci#define TMDR_MD_NORMAL (0 << 0) 1028c2ecf20Sopenharmony_ci#define TMDR_MD_PWM_1 (2 << 0) 1038c2ecf20Sopenharmony_ci#define TMDR_MD_PWM_2 (3 << 0) 1048c2ecf20Sopenharmony_ci#define TMDR_MD_PHASE_1 (4 << 0) 1058c2ecf20Sopenharmony_ci#define TMDR_MD_PHASE_2 (5 << 0) 1068c2ecf20Sopenharmony_ci#define TMDR_MD_PHASE_3 (6 << 0) 1078c2ecf20Sopenharmony_ci#define TMDR_MD_PHASE_4 (7 << 0) 1088c2ecf20Sopenharmony_ci#define TMDR_MD_PWM_SYNC (8 << 0) 1098c2ecf20Sopenharmony_ci#define TMDR_MD_PWM_COMP_CREST (13 << 0) 1108c2ecf20Sopenharmony_ci#define TMDR_MD_PWM_COMP_TROUGH (14 << 0) 1118c2ecf20Sopenharmony_ci#define TMDR_MD_PWM_COMP_BOTH (15 << 0) 1128c2ecf20Sopenharmony_ci#define TMDR_MD_MASK (15 << 0) 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci#define TIOC_IOCH(n) ((n) << 4) 1158c2ecf20Sopenharmony_ci#define TIOC_IOCL(n) ((n) << 0) 1168c2ecf20Sopenharmony_ci#define TIOR_OC_RETAIN (0 << 0) 1178c2ecf20Sopenharmony_ci#define TIOR_OC_0_CLEAR (1 << 0) 1188c2ecf20Sopenharmony_ci#define TIOR_OC_0_SET (2 << 0) 1198c2ecf20Sopenharmony_ci#define TIOR_OC_0_TOGGLE (3 << 0) 1208c2ecf20Sopenharmony_ci#define TIOR_OC_1_CLEAR (5 << 0) 1218c2ecf20Sopenharmony_ci#define TIOR_OC_1_SET (6 << 0) 1228c2ecf20Sopenharmony_ci#define TIOR_OC_1_TOGGLE (7 << 0) 1238c2ecf20Sopenharmony_ci#define TIOR_IC_RISING (8 << 0) 1248c2ecf20Sopenharmony_ci#define TIOR_IC_FALLING (9 << 0) 1258c2ecf20Sopenharmony_ci#define TIOR_IC_BOTH (10 << 0) 1268c2ecf20Sopenharmony_ci#define TIOR_IC_TCNT (12 << 0) 1278c2ecf20Sopenharmony_ci#define TIOR_MASK (15 << 0) 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci#define TIER_TTGE (1 << 7) 1308c2ecf20Sopenharmony_ci#define TIER_TTGE2 (1 << 6) 1318c2ecf20Sopenharmony_ci#define TIER_TCIEU (1 << 5) 1328c2ecf20Sopenharmony_ci#define TIER_TCIEV (1 << 4) 1338c2ecf20Sopenharmony_ci#define TIER_TGIED (1 << 3) 1348c2ecf20Sopenharmony_ci#define TIER_TGIEC (1 << 2) 1358c2ecf20Sopenharmony_ci#define TIER_TGIEB (1 << 1) 1368c2ecf20Sopenharmony_ci#define TIER_TGIEA (1 << 0) 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci#define TSR_TCFD (1 << 7) 1398c2ecf20Sopenharmony_ci#define TSR_TCFU (1 << 5) 1408c2ecf20Sopenharmony_ci#define TSR_TCFV (1 << 4) 1418c2ecf20Sopenharmony_ci#define TSR_TGFD (1 << 3) 1428c2ecf20Sopenharmony_ci#define TSR_TGFC (1 << 2) 1438c2ecf20Sopenharmony_ci#define TSR_TGFB (1 << 1) 1448c2ecf20Sopenharmony_ci#define TSR_TGFA (1 << 0) 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic unsigned long mtu2_reg_offs[] = { 1478c2ecf20Sopenharmony_ci [TCR] = 0, 1488c2ecf20Sopenharmony_ci [TMDR] = 1, 1498c2ecf20Sopenharmony_ci [TIOR] = 2, 1508c2ecf20Sopenharmony_ci [TIER] = 4, 1518c2ecf20Sopenharmony_ci [TSR] = 5, 1528c2ecf20Sopenharmony_ci [TCNT] = 6, 1538c2ecf20Sopenharmony_ci [TGR] = 8, 1548c2ecf20Sopenharmony_ci}; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic inline unsigned long sh_mtu2_read(struct sh_mtu2_channel *ch, int reg_nr) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci unsigned long offs; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (reg_nr == TSTR) 1618c2ecf20Sopenharmony_ci return ioread8(ch->mtu->mapbase + 0x280); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci offs = mtu2_reg_offs[reg_nr]; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if ((reg_nr == TCNT) || (reg_nr == TGR)) 1668c2ecf20Sopenharmony_ci return ioread16(ch->base + offs); 1678c2ecf20Sopenharmony_ci else 1688c2ecf20Sopenharmony_ci return ioread8(ch->base + offs); 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_cistatic inline void sh_mtu2_write(struct sh_mtu2_channel *ch, int reg_nr, 1728c2ecf20Sopenharmony_ci unsigned long value) 1738c2ecf20Sopenharmony_ci{ 1748c2ecf20Sopenharmony_ci unsigned long offs; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (reg_nr == TSTR) 1778c2ecf20Sopenharmony_ci return iowrite8(value, ch->mtu->mapbase + 0x280); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci offs = mtu2_reg_offs[reg_nr]; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if ((reg_nr == TCNT) || (reg_nr == TGR)) 1828c2ecf20Sopenharmony_ci iowrite16(value, ch->base + offs); 1838c2ecf20Sopenharmony_ci else 1848c2ecf20Sopenharmony_ci iowrite8(value, ch->base + offs); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic void sh_mtu2_start_stop_ch(struct sh_mtu2_channel *ch, int start) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci unsigned long flags, value; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci /* start stop register shared by multiple timer channels */ 1928c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&ch->mtu->lock, flags); 1938c2ecf20Sopenharmony_ci value = sh_mtu2_read(ch, TSTR); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci if (start) 1968c2ecf20Sopenharmony_ci value |= 1 << ch->index; 1978c2ecf20Sopenharmony_ci else 1988c2ecf20Sopenharmony_ci value &= ~(1 << ch->index); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TSTR, value); 2018c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&ch->mtu->lock, flags); 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cistatic int sh_mtu2_enable(struct sh_mtu2_channel *ch) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci unsigned long periodic; 2078c2ecf20Sopenharmony_ci unsigned long rate; 2088c2ecf20Sopenharmony_ci int ret; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci pm_runtime_get_sync(&ch->mtu->pdev->dev); 2118c2ecf20Sopenharmony_ci dev_pm_syscore_device(&ch->mtu->pdev->dev, true); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci /* enable clock */ 2148c2ecf20Sopenharmony_ci ret = clk_enable(ch->mtu->clk); 2158c2ecf20Sopenharmony_ci if (ret) { 2168c2ecf20Sopenharmony_ci dev_err(&ch->mtu->pdev->dev, "ch%u: cannot enable clock\n", 2178c2ecf20Sopenharmony_ci ch->index); 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* make sure channel is disabled */ 2228c2ecf20Sopenharmony_ci sh_mtu2_start_stop_ch(ch, 0); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci rate = clk_get_rate(ch->mtu->clk) / 64; 2258c2ecf20Sopenharmony_ci periodic = (rate + HZ/2) / HZ; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci /* 2288c2ecf20Sopenharmony_ci * "Periodic Counter Operation" 2298c2ecf20Sopenharmony_ci * Clear on TGRA compare match, divide clock by 64. 2308c2ecf20Sopenharmony_ci */ 2318c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TCR, TCR_CCLR_TGRA | TCR_TPSC_P64); 2328c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TIOR, TIOC_IOCH(TIOR_OC_0_CLEAR) | 2338c2ecf20Sopenharmony_ci TIOC_IOCL(TIOR_OC_0_CLEAR)); 2348c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TGR, periodic); 2358c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TCNT, 0); 2368c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TMDR, TMDR_MD_NORMAL); 2378c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TIER, TIER_TGIEA); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci /* enable channel */ 2408c2ecf20Sopenharmony_ci sh_mtu2_start_stop_ch(ch, 1); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci return 0; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cistatic void sh_mtu2_disable(struct sh_mtu2_channel *ch) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci /* disable channel */ 2488c2ecf20Sopenharmony_ci sh_mtu2_start_stop_ch(ch, 0); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci /* stop clock */ 2518c2ecf20Sopenharmony_ci clk_disable(ch->mtu->clk); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci dev_pm_syscore_device(&ch->mtu->pdev->dev, false); 2548c2ecf20Sopenharmony_ci pm_runtime_put(&ch->mtu->pdev->dev); 2558c2ecf20Sopenharmony_ci} 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_cistatic irqreturn_t sh_mtu2_interrupt(int irq, void *dev_id) 2588c2ecf20Sopenharmony_ci{ 2598c2ecf20Sopenharmony_ci struct sh_mtu2_channel *ch = dev_id; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci /* acknowledge interrupt */ 2628c2ecf20Sopenharmony_ci sh_mtu2_read(ch, TSR); 2638c2ecf20Sopenharmony_ci sh_mtu2_write(ch, TSR, ~TSR_TGFA); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci /* notify clockevent layer */ 2668c2ecf20Sopenharmony_ci ch->ced.event_handler(&ch->ced); 2678c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cistatic struct sh_mtu2_channel *ced_to_sh_mtu2(struct clock_event_device *ced) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci return container_of(ced, struct sh_mtu2_channel, ced); 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_cistatic int sh_mtu2_clock_event_shutdown(struct clock_event_device *ced) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci struct sh_mtu2_channel *ch = ced_to_sh_mtu2(ced); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci if (clockevent_state_periodic(ced)) 2808c2ecf20Sopenharmony_ci sh_mtu2_disable(ch); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci return 0; 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cistatic int sh_mtu2_clock_event_set_periodic(struct clock_event_device *ced) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci struct sh_mtu2_channel *ch = ced_to_sh_mtu2(ced); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci if (clockevent_state_periodic(ced)) 2908c2ecf20Sopenharmony_ci sh_mtu2_disable(ch); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci dev_info(&ch->mtu->pdev->dev, "ch%u: used for periodic clock events\n", 2938c2ecf20Sopenharmony_ci ch->index); 2948c2ecf20Sopenharmony_ci sh_mtu2_enable(ch); 2958c2ecf20Sopenharmony_ci return 0; 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_cistatic void sh_mtu2_clock_event_suspend(struct clock_event_device *ced) 2998c2ecf20Sopenharmony_ci{ 3008c2ecf20Sopenharmony_ci pm_genpd_syscore_poweroff(&ced_to_sh_mtu2(ced)->mtu->pdev->dev); 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_cistatic void sh_mtu2_clock_event_resume(struct clock_event_device *ced) 3048c2ecf20Sopenharmony_ci{ 3058c2ecf20Sopenharmony_ci pm_genpd_syscore_poweron(&ced_to_sh_mtu2(ced)->mtu->pdev->dev); 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistatic void sh_mtu2_register_clockevent(struct sh_mtu2_channel *ch, 3098c2ecf20Sopenharmony_ci const char *name) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci struct clock_event_device *ced = &ch->ced; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci ced->name = name; 3148c2ecf20Sopenharmony_ci ced->features = CLOCK_EVT_FEAT_PERIODIC; 3158c2ecf20Sopenharmony_ci ced->rating = 200; 3168c2ecf20Sopenharmony_ci ced->cpumask = cpu_possible_mask; 3178c2ecf20Sopenharmony_ci ced->set_state_shutdown = sh_mtu2_clock_event_shutdown; 3188c2ecf20Sopenharmony_ci ced->set_state_periodic = sh_mtu2_clock_event_set_periodic; 3198c2ecf20Sopenharmony_ci ced->suspend = sh_mtu2_clock_event_suspend; 3208c2ecf20Sopenharmony_ci ced->resume = sh_mtu2_clock_event_resume; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci dev_info(&ch->mtu->pdev->dev, "ch%u: used for clock events\n", 3238c2ecf20Sopenharmony_ci ch->index); 3248c2ecf20Sopenharmony_ci clockevents_register_device(ced); 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic int sh_mtu2_register(struct sh_mtu2_channel *ch, const char *name) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci ch->mtu->has_clockevent = true; 3308c2ecf20Sopenharmony_ci sh_mtu2_register_clockevent(ch, name); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci return 0; 3338c2ecf20Sopenharmony_ci} 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_cistatic const unsigned int sh_mtu2_channel_offsets[] = { 3368c2ecf20Sopenharmony_ci 0x300, 0x380, 0x000, 3378c2ecf20Sopenharmony_ci}; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cistatic int sh_mtu2_setup_channel(struct sh_mtu2_channel *ch, unsigned int index, 3408c2ecf20Sopenharmony_ci struct sh_mtu2_device *mtu) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci char name[6]; 3438c2ecf20Sopenharmony_ci int irq; 3448c2ecf20Sopenharmony_ci int ret; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci ch->mtu = mtu; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci sprintf(name, "tgi%ua", index); 3498c2ecf20Sopenharmony_ci irq = platform_get_irq_byname(mtu->pdev, name); 3508c2ecf20Sopenharmony_ci if (irq < 0) { 3518c2ecf20Sopenharmony_ci /* Skip channels with no declared interrupt. */ 3528c2ecf20Sopenharmony_ci return 0; 3538c2ecf20Sopenharmony_ci } 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci ret = request_irq(irq, sh_mtu2_interrupt, 3568c2ecf20Sopenharmony_ci IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING, 3578c2ecf20Sopenharmony_ci dev_name(&ch->mtu->pdev->dev), ch); 3588c2ecf20Sopenharmony_ci if (ret) { 3598c2ecf20Sopenharmony_ci dev_err(&ch->mtu->pdev->dev, "ch%u: failed to request irq %d\n", 3608c2ecf20Sopenharmony_ci index, irq); 3618c2ecf20Sopenharmony_ci return ret; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci ch->base = mtu->mapbase + sh_mtu2_channel_offsets[index]; 3658c2ecf20Sopenharmony_ci ch->index = index; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci return sh_mtu2_register(ch, dev_name(&mtu->pdev->dev)); 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic int sh_mtu2_map_memory(struct sh_mtu2_device *mtu) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci struct resource *res; 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci res = platform_get_resource(mtu->pdev, IORESOURCE_MEM, 0); 3758c2ecf20Sopenharmony_ci if (!res) { 3768c2ecf20Sopenharmony_ci dev_err(&mtu->pdev->dev, "failed to get I/O memory\n"); 3778c2ecf20Sopenharmony_ci return -ENXIO; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci mtu->mapbase = ioremap(res->start, resource_size(res)); 3818c2ecf20Sopenharmony_ci if (mtu->mapbase == NULL) 3828c2ecf20Sopenharmony_ci return -ENXIO; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci return 0; 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_cistatic int sh_mtu2_setup(struct sh_mtu2_device *mtu, 3888c2ecf20Sopenharmony_ci struct platform_device *pdev) 3898c2ecf20Sopenharmony_ci{ 3908c2ecf20Sopenharmony_ci unsigned int i; 3918c2ecf20Sopenharmony_ci int ret; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci mtu->pdev = pdev; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci raw_spin_lock_init(&mtu->lock); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci /* Get hold of clock. */ 3988c2ecf20Sopenharmony_ci mtu->clk = clk_get(&mtu->pdev->dev, "fck"); 3998c2ecf20Sopenharmony_ci if (IS_ERR(mtu->clk)) { 4008c2ecf20Sopenharmony_ci dev_err(&mtu->pdev->dev, "cannot get clock\n"); 4018c2ecf20Sopenharmony_ci return PTR_ERR(mtu->clk); 4028c2ecf20Sopenharmony_ci } 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci ret = clk_prepare(mtu->clk); 4058c2ecf20Sopenharmony_ci if (ret < 0) 4068c2ecf20Sopenharmony_ci goto err_clk_put; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci /* Map the memory resource. */ 4098c2ecf20Sopenharmony_ci ret = sh_mtu2_map_memory(mtu); 4108c2ecf20Sopenharmony_ci if (ret < 0) { 4118c2ecf20Sopenharmony_ci dev_err(&mtu->pdev->dev, "failed to remap I/O memory\n"); 4128c2ecf20Sopenharmony_ci goto err_clk_unprepare; 4138c2ecf20Sopenharmony_ci } 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci /* Allocate and setup the channels. */ 4168c2ecf20Sopenharmony_ci ret = platform_irq_count(pdev); 4178c2ecf20Sopenharmony_ci if (ret < 0) 4188c2ecf20Sopenharmony_ci goto err_unmap; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci mtu->num_channels = min_t(unsigned int, ret, 4218c2ecf20Sopenharmony_ci ARRAY_SIZE(sh_mtu2_channel_offsets)); 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci mtu->channels = kcalloc(mtu->num_channels, sizeof(*mtu->channels), 4248c2ecf20Sopenharmony_ci GFP_KERNEL); 4258c2ecf20Sopenharmony_ci if (mtu->channels == NULL) { 4268c2ecf20Sopenharmony_ci ret = -ENOMEM; 4278c2ecf20Sopenharmony_ci goto err_unmap; 4288c2ecf20Sopenharmony_ci } 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci for (i = 0; i < mtu->num_channels; ++i) { 4318c2ecf20Sopenharmony_ci ret = sh_mtu2_setup_channel(&mtu->channels[i], i, mtu); 4328c2ecf20Sopenharmony_ci if (ret < 0) 4338c2ecf20Sopenharmony_ci goto err_unmap; 4348c2ecf20Sopenharmony_ci } 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, mtu); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci return 0; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_cierr_unmap: 4418c2ecf20Sopenharmony_ci kfree(mtu->channels); 4428c2ecf20Sopenharmony_ci iounmap(mtu->mapbase); 4438c2ecf20Sopenharmony_cierr_clk_unprepare: 4448c2ecf20Sopenharmony_ci clk_unprepare(mtu->clk); 4458c2ecf20Sopenharmony_cierr_clk_put: 4468c2ecf20Sopenharmony_ci clk_put(mtu->clk); 4478c2ecf20Sopenharmony_ci return ret; 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_cistatic int sh_mtu2_probe(struct platform_device *pdev) 4518c2ecf20Sopenharmony_ci{ 4528c2ecf20Sopenharmony_ci struct sh_mtu2_device *mtu = platform_get_drvdata(pdev); 4538c2ecf20Sopenharmony_ci int ret; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci if (!is_sh_early_platform_device(pdev)) { 4568c2ecf20Sopenharmony_ci pm_runtime_set_active(&pdev->dev); 4578c2ecf20Sopenharmony_ci pm_runtime_enable(&pdev->dev); 4588c2ecf20Sopenharmony_ci } 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci if (mtu) { 4618c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "kept as earlytimer\n"); 4628c2ecf20Sopenharmony_ci goto out; 4638c2ecf20Sopenharmony_ci } 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci mtu = kzalloc(sizeof(*mtu), GFP_KERNEL); 4668c2ecf20Sopenharmony_ci if (mtu == NULL) 4678c2ecf20Sopenharmony_ci return -ENOMEM; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci ret = sh_mtu2_setup(mtu, pdev); 4708c2ecf20Sopenharmony_ci if (ret) { 4718c2ecf20Sopenharmony_ci kfree(mtu); 4728c2ecf20Sopenharmony_ci pm_runtime_idle(&pdev->dev); 4738c2ecf20Sopenharmony_ci return ret; 4748c2ecf20Sopenharmony_ci } 4758c2ecf20Sopenharmony_ci if (is_sh_early_platform_device(pdev)) 4768c2ecf20Sopenharmony_ci return 0; 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci out: 4798c2ecf20Sopenharmony_ci if (mtu->has_clockevent) 4808c2ecf20Sopenharmony_ci pm_runtime_irq_safe(&pdev->dev); 4818c2ecf20Sopenharmony_ci else 4828c2ecf20Sopenharmony_ci pm_runtime_idle(&pdev->dev); 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci return 0; 4858c2ecf20Sopenharmony_ci} 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_cistatic int sh_mtu2_remove(struct platform_device *pdev) 4888c2ecf20Sopenharmony_ci{ 4898c2ecf20Sopenharmony_ci return -EBUSY; /* cannot unregister clockevent */ 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic const struct platform_device_id sh_mtu2_id_table[] = { 4938c2ecf20Sopenharmony_ci { "sh-mtu2", 0 }, 4948c2ecf20Sopenharmony_ci { }, 4958c2ecf20Sopenharmony_ci}; 4968c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, sh_mtu2_id_table); 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_cistatic const struct of_device_id sh_mtu2_of_table[] __maybe_unused = { 4998c2ecf20Sopenharmony_ci { .compatible = "renesas,mtu2" }, 5008c2ecf20Sopenharmony_ci { } 5018c2ecf20Sopenharmony_ci}; 5028c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sh_mtu2_of_table); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_cistatic struct platform_driver sh_mtu2_device_driver = { 5058c2ecf20Sopenharmony_ci .probe = sh_mtu2_probe, 5068c2ecf20Sopenharmony_ci .remove = sh_mtu2_remove, 5078c2ecf20Sopenharmony_ci .driver = { 5088c2ecf20Sopenharmony_ci .name = "sh_mtu2", 5098c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(sh_mtu2_of_table), 5108c2ecf20Sopenharmony_ci }, 5118c2ecf20Sopenharmony_ci .id_table = sh_mtu2_id_table, 5128c2ecf20Sopenharmony_ci}; 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_cistatic int __init sh_mtu2_init(void) 5158c2ecf20Sopenharmony_ci{ 5168c2ecf20Sopenharmony_ci return platform_driver_register(&sh_mtu2_device_driver); 5178c2ecf20Sopenharmony_ci} 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic void __exit sh_mtu2_exit(void) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci platform_driver_unregister(&sh_mtu2_device_driver); 5228c2ecf20Sopenharmony_ci} 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci#ifdef CONFIG_SUPERH 5258c2ecf20Sopenharmony_cish_early_platform_init("earlytimer", &sh_mtu2_device_driver); 5268c2ecf20Sopenharmony_ci#endif 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_cisubsys_initcall(sh_mtu2_init); 5298c2ecf20Sopenharmony_cimodule_exit(sh_mtu2_exit); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Magnus Damm"); 5328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SuperH MTU2 Timer Driver"); 5338c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 534