18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2016, Mellanox Technologies. All rights reserved. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 58c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 118c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 128c2ecf20Sopenharmony_ci * conditions are met: 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 158c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 168c2ecf20Sopenharmony_ci * disclaimer. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 198c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 208c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 218c2ecf20Sopenharmony_ci * provided with the distribution. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 308c2ecf20Sopenharmony_ci * SOFTWARE. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 348c2ecf20Sopenharmony_ci#include <linux/clocksource.h> 358c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 368c2ecf20Sopenharmony_ci#include <linux/clk.h> 378c2ecf20Sopenharmony_ci#include <linux/of.h> 388c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 398c2ecf20Sopenharmony_ci#include <linux/cpu.h> 408c2ecf20Sopenharmony_ci#include <soc/nps/common.h> 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define NPS_MSU_TICK_LOW 0xC8 438c2ecf20Sopenharmony_ci#define NPS_CLUSTER_OFFSET 8 448c2ecf20Sopenharmony_ci#define NPS_CLUSTER_NUM 16 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* This array is per cluster of CPUs (Each NPS400 cluster got 256 CPUs) */ 478c2ecf20Sopenharmony_cistatic void *nps_msu_reg_low_addr[NPS_CLUSTER_NUM] __read_mostly; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic int __init nps_get_timer_clk(struct device_node *node, 508c2ecf20Sopenharmony_ci unsigned long *timer_freq, 518c2ecf20Sopenharmony_ci struct clk **clk) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci int ret; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci *clk = of_clk_get(node, 0); 568c2ecf20Sopenharmony_ci ret = PTR_ERR_OR_ZERO(*clk); 578c2ecf20Sopenharmony_ci if (ret) { 588c2ecf20Sopenharmony_ci pr_err("timer missing clk\n"); 598c2ecf20Sopenharmony_ci return ret; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci ret = clk_prepare_enable(*clk); 638c2ecf20Sopenharmony_ci if (ret) { 648c2ecf20Sopenharmony_ci pr_err("Couldn't enable parent clk\n"); 658c2ecf20Sopenharmony_ci clk_put(*clk); 668c2ecf20Sopenharmony_ci return ret; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci *timer_freq = clk_get_rate(*clk); 708c2ecf20Sopenharmony_ci if (!(*timer_freq)) { 718c2ecf20Sopenharmony_ci pr_err("Couldn't get clk rate\n"); 728c2ecf20Sopenharmony_ci clk_disable_unprepare(*clk); 738c2ecf20Sopenharmony_ci clk_put(*clk); 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci return 0; 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic u64 nps_clksrc_read(struct clocksource *clksrc) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci int cluster = raw_smp_processor_id() >> NPS_CLUSTER_OFFSET; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci return (u64)ioread32be(nps_msu_reg_low_addr[cluster]); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic int __init nps_setup_clocksource(struct device_node *node) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci int ret, cluster; 908c2ecf20Sopenharmony_ci struct clk *clk; 918c2ecf20Sopenharmony_ci unsigned long nps_timer1_freq; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci for (cluster = 0; cluster < NPS_CLUSTER_NUM; cluster++) 958c2ecf20Sopenharmony_ci nps_msu_reg_low_addr[cluster] = 968c2ecf20Sopenharmony_ci nps_host_reg((cluster << NPS_CLUSTER_OFFSET), 978c2ecf20Sopenharmony_ci NPS_MSU_BLKID, NPS_MSU_TICK_LOW); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci ret = nps_get_timer_clk(node, &nps_timer1_freq, &clk); 1008c2ecf20Sopenharmony_ci if (ret) 1018c2ecf20Sopenharmony_ci return ret; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci ret = clocksource_mmio_init(nps_msu_reg_low_addr, "nps-tick", 1048c2ecf20Sopenharmony_ci nps_timer1_freq, 300, 32, nps_clksrc_read); 1058c2ecf20Sopenharmony_ci if (ret) { 1068c2ecf20Sopenharmony_ci pr_err("Couldn't register clock source.\n"); 1078c2ecf20Sopenharmony_ci clk_disable_unprepare(clk); 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci return ret; 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(ezchip_nps400_clksrc, "ezchip,nps400-timer", 1148c2ecf20Sopenharmony_ci nps_setup_clocksource); 1158c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(ezchip_nps400_clk_src, "ezchip,nps400-timer1", 1168c2ecf20Sopenharmony_ci nps_setup_clocksource); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci#ifdef CONFIG_EZNPS_MTM_EXT 1198c2ecf20Sopenharmony_ci#include <soc/nps/mtm.h> 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* Timer related Aux registers */ 1228c2ecf20Sopenharmony_ci#define NPS_REG_TIMER0_TSI 0xFFFFF850 1238c2ecf20Sopenharmony_ci#define NPS_REG_TIMER0_LIMIT 0x23 1248c2ecf20Sopenharmony_ci#define NPS_REG_TIMER0_CTRL 0x22 1258c2ecf20Sopenharmony_ci#define NPS_REG_TIMER0_CNT 0x21 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci/* 1288c2ecf20Sopenharmony_ci * Interrupt Enabled (IE) - re-arm the timer 1298c2ecf20Sopenharmony_ci * Not Halted (NH) - is cleared when working with JTAG (for debug) 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_ci#define TIMER0_CTRL_IE BIT(0) 1328c2ecf20Sopenharmony_ci#define TIMER0_CTRL_NH BIT(1) 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic unsigned long nps_timer0_freq; 1358c2ecf20Sopenharmony_cistatic unsigned long nps_timer0_irq; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic void nps_clkevent_rm_thread(void) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci int thread; 1408c2ecf20Sopenharmony_ci unsigned int cflags, enabled_threads; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci hw_schd_save(&cflags); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci enabled_threads = read_aux_reg(NPS_REG_TIMER0_TSI); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci /* remove thread from TSI1 */ 1478c2ecf20Sopenharmony_ci thread = read_aux_reg(CTOP_AUX_THREAD_ID); 1488c2ecf20Sopenharmony_ci enabled_threads &= ~(1 << thread); 1498c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_TSI, enabled_threads); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* Acknowledge and if needed re-arm the timer */ 1528c2ecf20Sopenharmony_ci if (!enabled_threads) 1538c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_CTRL, TIMER0_CTRL_NH); 1548c2ecf20Sopenharmony_ci else 1558c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_CTRL, 1568c2ecf20Sopenharmony_ci TIMER0_CTRL_IE | TIMER0_CTRL_NH); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci hw_schd_restore(cflags); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic void nps_clkevent_add_thread(unsigned long delta) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci int thread; 1648c2ecf20Sopenharmony_ci unsigned int cflags, enabled_threads; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci hw_schd_save(&cflags); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci /* add thread to TSI1 */ 1698c2ecf20Sopenharmony_ci thread = read_aux_reg(CTOP_AUX_THREAD_ID); 1708c2ecf20Sopenharmony_ci enabled_threads = read_aux_reg(NPS_REG_TIMER0_TSI); 1718c2ecf20Sopenharmony_ci enabled_threads |= (1 << thread); 1728c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_TSI, enabled_threads); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci /* set next timer event */ 1758c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_LIMIT, delta); 1768c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_CNT, 0); 1778c2ecf20Sopenharmony_ci write_aux_reg(NPS_REG_TIMER0_CTRL, 1788c2ecf20Sopenharmony_ci TIMER0_CTRL_IE | TIMER0_CTRL_NH); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci hw_schd_restore(cflags); 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* 1848c2ecf20Sopenharmony_ci * Whenever anyone tries to change modes, we just mask interrupts 1858c2ecf20Sopenharmony_ci * and wait for the next event to get set. 1868c2ecf20Sopenharmony_ci */ 1878c2ecf20Sopenharmony_cistatic int nps_clkevent_set_state(struct clock_event_device *dev) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci nps_clkevent_rm_thread(); 1908c2ecf20Sopenharmony_ci disable_percpu_irq(nps_timer0_irq); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci return 0; 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic int nps_clkevent_set_next_event(unsigned long delta, 1968c2ecf20Sopenharmony_ci struct clock_event_device *dev) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci nps_clkevent_add_thread(delta); 1998c2ecf20Sopenharmony_ci enable_percpu_irq(nps_timer0_irq, IRQ_TYPE_NONE); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci return 0; 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct clock_event_device, nps_clockevent_device) = { 2058c2ecf20Sopenharmony_ci .name = "NPS Timer0", 2068c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_ONESHOT, 2078c2ecf20Sopenharmony_ci .rating = 300, 2088c2ecf20Sopenharmony_ci .set_next_event = nps_clkevent_set_next_event, 2098c2ecf20Sopenharmony_ci .set_state_oneshot = nps_clkevent_set_state, 2108c2ecf20Sopenharmony_ci .set_state_oneshot_stopped = nps_clkevent_set_state, 2118c2ecf20Sopenharmony_ci .set_state_shutdown = nps_clkevent_set_state, 2128c2ecf20Sopenharmony_ci .tick_resume = nps_clkevent_set_state, 2138c2ecf20Sopenharmony_ci}; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_cistatic irqreturn_t timer_irq_handler(int irq, void *dev_id) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci struct clock_event_device *evt = dev_id; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci nps_clkevent_rm_thread(); 2208c2ecf20Sopenharmony_ci evt->event_handler(evt); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_cistatic int nps_timer_starting_cpu(unsigned int cpu) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci struct clock_event_device *evt = this_cpu_ptr(&nps_clockevent_device); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci evt->cpumask = cpumask_of(smp_processor_id()); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci clockevents_config_and_register(evt, nps_timer0_freq, 0, ULONG_MAX); 2328c2ecf20Sopenharmony_ci enable_percpu_irq(nps_timer0_irq, IRQ_TYPE_NONE); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci return 0; 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic int nps_timer_dying_cpu(unsigned int cpu) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci disable_percpu_irq(nps_timer0_irq); 2408c2ecf20Sopenharmony_ci return 0; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic int __init nps_setup_clockevent(struct device_node *node) 2448c2ecf20Sopenharmony_ci{ 2458c2ecf20Sopenharmony_ci struct clk *clk; 2468c2ecf20Sopenharmony_ci int ret; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci nps_timer0_irq = irq_of_parse_and_map(node, 0); 2498c2ecf20Sopenharmony_ci if (nps_timer0_irq <= 0) { 2508c2ecf20Sopenharmony_ci pr_err("clockevent: missing irq\n"); 2518c2ecf20Sopenharmony_ci return -EINVAL; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci ret = nps_get_timer_clk(node, &nps_timer0_freq, &clk); 2558c2ecf20Sopenharmony_ci if (ret) 2568c2ecf20Sopenharmony_ci return ret; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci /* Needs apriori irq_set_percpu_devid() done in intc map function */ 2598c2ecf20Sopenharmony_ci ret = request_percpu_irq(nps_timer0_irq, timer_irq_handler, 2608c2ecf20Sopenharmony_ci "Timer0 (per-cpu-tick)", 2618c2ecf20Sopenharmony_ci &nps_clockevent_device); 2628c2ecf20Sopenharmony_ci if (ret) { 2638c2ecf20Sopenharmony_ci pr_err("Couldn't request irq\n"); 2648c2ecf20Sopenharmony_ci clk_disable_unprepare(clk); 2658c2ecf20Sopenharmony_ci return ret; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING, 2698c2ecf20Sopenharmony_ci "clockevents/nps:starting", 2708c2ecf20Sopenharmony_ci nps_timer_starting_cpu, 2718c2ecf20Sopenharmony_ci nps_timer_dying_cpu); 2728c2ecf20Sopenharmony_ci if (ret) { 2738c2ecf20Sopenharmony_ci pr_err("Failed to setup hotplug state\n"); 2748c2ecf20Sopenharmony_ci clk_disable_unprepare(clk); 2758c2ecf20Sopenharmony_ci free_percpu_irq(nps_timer0_irq, &nps_clockevent_device); 2768c2ecf20Sopenharmony_ci return ret; 2778c2ecf20Sopenharmony_ci } 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci return 0; 2808c2ecf20Sopenharmony_ci} 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(ezchip_nps400_clk_evt, "ezchip,nps400-timer0", 2838c2ecf20Sopenharmony_ci nps_setup_clockevent); 2848c2ecf20Sopenharmony_ci#endif /* CONFIG_EZNPS_MTM_EXT */ 285