1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds #include <linux/module.h>
31da177e4SLinus Torvalds #include <linux/smp.h>
41da177e4SLinus Torvalds #include <linux/time.h>
51da177e4SLinus Torvalds #include <linux/errno.h>
64e57b681STim Schmielau #include <linux/timex.h>
70aa366f3STony Luck #include <linux/clocksource.h>
82584cf83SDan Williams #include <linux/io.h>
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds /* IBM Summit (EXA) Cyclone counter code*/
111da177e4SLinus Torvalds #define CYCLONE_CBAR_ADDR 0xFEB00CD0
121da177e4SLinus Torvalds #define CYCLONE_PMCC_OFFSET 0x51A0
131da177e4SLinus Torvalds #define CYCLONE_MPMC_OFFSET 0x51D0
141da177e4SLinus Torvalds #define CYCLONE_MPCS_OFFSET 0x51A8
151da177e4SLinus Torvalds #define CYCLONE_TIMER_FREQ 100000000
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds int use_cyclone;
cyclone_setup(void)181da177e4SLinus Torvalds void __init cyclone_setup(void)
191da177e4SLinus Torvalds {
201da177e4SLinus Torvalds use_cyclone = 1;
211da177e4SLinus Torvalds }
221da177e4SLinus Torvalds
230aa366f3STony Luck static void __iomem *cyclone_mc;
241da177e4SLinus Torvalds
read_cyclone(struct clocksource * cs)25a5a1d1c2SThomas Gleixner static u64 read_cyclone(struct clocksource *cs)
260aa366f3STony Luck {
27a5a1d1c2SThomas Gleixner return (u64)readq((void __iomem *)cyclone_mc);
280aa366f3STony Luck }
290aa366f3STony Luck
300aa366f3STony Luck static struct clocksource clocksource_cyclone = {
310aa366f3STony Luck .name = "cyclone",
320aa366f3STony Luck .rating = 300,
330aa366f3STony Luck .read = read_cyclone,
340aa366f3STony Luck .mask = (1LL << 40) - 1,
350aa366f3STony Luck .flags = CLOCK_SOURCE_IS_CONTINUOUS,
361da177e4SLinus Torvalds };
371da177e4SLinus Torvalds
init_cyclone_clock(void)381da177e4SLinus Torvalds int __init init_cyclone_clock(void)
391da177e4SLinus Torvalds {
406aa8b049SAl Viro u64 __iomem *reg;
411da177e4SLinus Torvalds u64 base; /* saved cyclone base address */
421da177e4SLinus Torvalds u64 offset; /* offset from pageaddr to cyclone_timer register */
431da177e4SLinus Torvalds int i;
446aa8b049SAl Viro u32 __iomem *cyclone_timer; /* Cyclone MPMC0 register */
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds if (!use_cyclone)
476c5e6215SBjorn Helgaas return 0;
481da177e4SLinus Torvalds
491da177e4SLinus Torvalds printk(KERN_INFO "Summit chipset: Starting Cyclone Counter.\n");
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds /* find base address */
521da177e4SLinus Torvalds offset = (CYCLONE_CBAR_ADDR);
53*4bdc0d67SChristoph Hellwig reg = ioremap(offset, sizeof(u64));
541da177e4SLinus Torvalds if(!reg){
550aa366f3STony Luck printk(KERN_ERR "Summit chipset: Could not find valid CBAR"
560aa366f3STony Luck " register.\n");
571da177e4SLinus Torvalds use_cyclone = 0;
581da177e4SLinus Torvalds return -ENODEV;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds base = readq(reg);
61ddad53eeSJulia Lawall iounmap(reg);
621da177e4SLinus Torvalds if(!base){
630aa366f3STony Luck printk(KERN_ERR "Summit chipset: Could not find valid CBAR"
640aa366f3STony Luck " value.\n");
651da177e4SLinus Torvalds use_cyclone = 0;
661da177e4SLinus Torvalds return -ENODEV;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds /* setup PMCC */
701da177e4SLinus Torvalds offset = (base + CYCLONE_PMCC_OFFSET);
71*4bdc0d67SChristoph Hellwig reg = ioremap(offset, sizeof(u64));
721da177e4SLinus Torvalds if(!reg){
730aa366f3STony Luck printk(KERN_ERR "Summit chipset: Could not find valid PMCC"
740aa366f3STony Luck " register.\n");
751da177e4SLinus Torvalds use_cyclone = 0;
761da177e4SLinus Torvalds return -ENODEV;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds writel(0x00000001,reg);
791da177e4SLinus Torvalds iounmap(reg);
801da177e4SLinus Torvalds
811da177e4SLinus Torvalds /* setup MPCS */
821da177e4SLinus Torvalds offset = (base + CYCLONE_MPCS_OFFSET);
83*4bdc0d67SChristoph Hellwig reg = ioremap(offset, sizeof(u64));
841da177e4SLinus Torvalds if(!reg){
850aa366f3STony Luck printk(KERN_ERR "Summit chipset: Could not find valid MPCS"
860aa366f3STony Luck " register.\n");
871da177e4SLinus Torvalds use_cyclone = 0;
881da177e4SLinus Torvalds return -ENODEV;
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds writel(0x00000001,reg);
911da177e4SLinus Torvalds iounmap(reg);
921da177e4SLinus Torvalds
931da177e4SLinus Torvalds /* map in cyclone_timer */
941da177e4SLinus Torvalds offset = (base + CYCLONE_MPMC_OFFSET);
95*4bdc0d67SChristoph Hellwig cyclone_timer = ioremap(offset, sizeof(u32));
961da177e4SLinus Torvalds if(!cyclone_timer){
970aa366f3STony Luck printk(KERN_ERR "Summit chipset: Could not find valid MPMC"
980aa366f3STony Luck " register.\n");
991da177e4SLinus Torvalds use_cyclone = 0;
1001da177e4SLinus Torvalds return -ENODEV;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds /*quick test to make sure its ticking*/
1041da177e4SLinus Torvalds for(i=0; i<3; i++){
1051da177e4SLinus Torvalds u32 old = readl(cyclone_timer);
1061da177e4SLinus Torvalds int stall = 100;
1071da177e4SLinus Torvalds while(stall--) barrier();
1081da177e4SLinus Torvalds if(readl(cyclone_timer) == old){
1090aa366f3STony Luck printk(KERN_ERR "Summit chipset: Counter not counting!"
1100aa366f3STony Luck " DISABLED\n");
1111da177e4SLinus Torvalds iounmap(cyclone_timer);
1126aa8b049SAl Viro cyclone_timer = NULL;
1131da177e4SLinus Torvalds use_cyclone = 0;
1141da177e4SLinus Torvalds return -ENODEV;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds /* initialize last tick */
1180aa366f3STony Luck cyclone_mc = cyclone_timer;
119574c44faSAndy Lutomirski clocksource_cyclone.archdata.fsys_mmio = cyclone_timer;
120d60c3041SJohn Stultz clocksource_register_hz(&clocksource_cyclone, CYCLONE_TIMER_FREQ);
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds return 0;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds
1251da177e4SLinus Torvalds __initcall(init_cyclone_clock);
126