1 /* 2 * Copyright (C) 2013-2014 Altera Corporation 3 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch> 4 * Copyright (C) 2004 Microtronix Datacom Ltd. 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file "COPYING" in the main directory of this archive 8 * for more details. 9 */ 10 11 #include <linux/interrupt.h> 12 #include <linux/clockchips.h> 13 #include <linux/clocksource.h> 14 #include <linux/delay.h> 15 #include <linux/of.h> 16 #include <linux/of_address.h> 17 #include <linux/of_irq.h> 18 #include <linux/io.h> 19 #include <linux/slab.h> 20 21 #define ALTR_TIMER_COMPATIBLE "altr,timer-1.0" 22 23 #define ALTERA_TIMER_STATUS_REG 0 24 #define ALTERA_TIMER_CONTROL_REG 4 25 #define ALTERA_TIMER_PERIODL_REG 8 26 #define ALTERA_TIMER_PERIODH_REG 12 27 #define ALTERA_TIMER_SNAPL_REG 16 28 #define ALTERA_TIMER_SNAPH_REG 20 29 30 #define ALTERA_TIMER_CONTROL_ITO_MSK (0x1) 31 #define ALTERA_TIMER_CONTROL_CONT_MSK (0x2) 32 #define ALTERA_TIMER_CONTROL_START_MSK (0x4) 33 #define ALTERA_TIMER_CONTROL_STOP_MSK (0x8) 34 35 struct nios2_timer { 36 void __iomem *base; 37 unsigned long freq; 38 }; 39 40 struct nios2_clockevent_dev { 41 struct nios2_timer timer; 42 struct clock_event_device ced; 43 }; 44 45 struct nios2_clocksource { 46 struct nios2_timer timer; 47 struct clocksource cs; 48 }; 49 50 static inline struct nios2_clockevent_dev * 51 to_nios2_clkevent(struct clock_event_device *evt) 52 { 53 return container_of(evt, struct nios2_clockevent_dev, ced); 54 } 55 56 static inline struct nios2_clocksource * 57 to_nios2_clksource(struct clocksource *cs) 58 { 59 return container_of(cs, struct nios2_clocksource, cs); 60 } 61 62 static u16 timer_readw(struct nios2_timer *timer, u32 offs) 63 { 64 return readw(timer->base + offs); 65 } 66 67 static void timer_writew(struct nios2_timer *timer, u16 val, u32 offs) 68 { 69 writew(val, timer->base + offs); 70 } 71 72 static inline unsigned long read_timersnapshot(struct nios2_timer *timer) 73 { 74 unsigned long count; 75 76 timer_writew(timer, 0, ALTERA_TIMER_SNAPL_REG); 77 count = timer_readw(timer, ALTERA_TIMER_SNAPH_REG) << 16 | 78 timer_readw(timer, ALTERA_TIMER_SNAPL_REG); 79 80 return count; 81 } 82 83 static cycle_t nios2_timer_read(struct clocksource *cs) 84 { 85 struct nios2_clocksource *nios2_cs = to_nios2_clksource(cs); 86 unsigned long flags; 87 u32 count; 88 89 local_irq_save(flags); 90 count = read_timersnapshot(&nios2_cs->timer); 91 local_irq_restore(flags); 92 93 /* Counter is counting down */ 94 return ~count; 95 } 96 97 static struct nios2_clocksource nios2_cs = { 98 .cs = { 99 .name = "nios2-clksrc", 100 .rating = 250, 101 .read = nios2_timer_read, 102 .mask = CLOCKSOURCE_MASK(32), 103 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 104 }, 105 }; 106 107 cycles_t get_cycles(void) 108 { 109 return nios2_timer_read(&nios2_cs.cs); 110 } 111 112 static void nios2_timer_start(struct nios2_timer *timer) 113 { 114 u16 ctrl; 115 116 ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG); 117 ctrl |= ALTERA_TIMER_CONTROL_START_MSK; 118 timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG); 119 } 120 121 static void nios2_timer_stop(struct nios2_timer *timer) 122 { 123 u16 ctrl; 124 125 ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG); 126 ctrl |= ALTERA_TIMER_CONTROL_STOP_MSK; 127 timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG); 128 } 129 130 static void nios2_timer_config(struct nios2_timer *timer, unsigned long period, 131 enum clock_event_mode mode) 132 { 133 u16 ctrl; 134 135 /* The timer's actual period is one cycle greater than the value 136 * stored in the period register. */ 137 period--; 138 139 ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG); 140 /* stop counter */ 141 timer_writew(timer, ctrl | ALTERA_TIMER_CONTROL_STOP_MSK, 142 ALTERA_TIMER_CONTROL_REG); 143 144 /* write new count */ 145 timer_writew(timer, period, ALTERA_TIMER_PERIODL_REG); 146 timer_writew(timer, period >> 16, ALTERA_TIMER_PERIODH_REG); 147 148 ctrl |= ALTERA_TIMER_CONTROL_START_MSK | ALTERA_TIMER_CONTROL_ITO_MSK; 149 if (mode == CLOCK_EVT_MODE_PERIODIC) 150 ctrl |= ALTERA_TIMER_CONTROL_CONT_MSK; 151 else 152 ctrl &= ~ALTERA_TIMER_CONTROL_CONT_MSK; 153 timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG); 154 } 155 156 static int nios2_timer_set_next_event(unsigned long delta, 157 struct clock_event_device *evt) 158 { 159 struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt); 160 161 nios2_timer_config(&nios2_ced->timer, delta, evt->mode); 162 163 return 0; 164 } 165 166 static void nios2_timer_set_mode(enum clock_event_mode mode, 167 struct clock_event_device *evt) 168 { 169 unsigned long period; 170 struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt); 171 struct nios2_timer *timer = &nios2_ced->timer; 172 173 switch (mode) { 174 case CLOCK_EVT_MODE_PERIODIC: 175 period = DIV_ROUND_UP(timer->freq, HZ); 176 nios2_timer_config(timer, period, CLOCK_EVT_MODE_PERIODIC); 177 break; 178 case CLOCK_EVT_MODE_ONESHOT: 179 case CLOCK_EVT_MODE_UNUSED: 180 case CLOCK_EVT_MODE_SHUTDOWN: 181 nios2_timer_stop(timer); 182 break; 183 case CLOCK_EVT_MODE_RESUME: 184 nios2_timer_start(timer); 185 break; 186 } 187 } 188 189 irqreturn_t timer_interrupt(int irq, void *dev_id) 190 { 191 struct clock_event_device *evt = (struct clock_event_device *) dev_id; 192 struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt); 193 194 /* Clear the interrupt condition */ 195 timer_writew(&nios2_ced->timer, 0, ALTERA_TIMER_STATUS_REG); 196 evt->event_handler(evt); 197 198 return IRQ_HANDLED; 199 } 200 201 static void __init nios2_timer_get_base_and_freq(struct device_node *np, 202 void __iomem **base, u32 *freq) 203 { 204 *base = of_iomap(np, 0); 205 if (!*base) 206 panic("Unable to map reg for %s\n", np->name); 207 208 if (of_property_read_u32(np, "clock-frequency", freq)) 209 panic("Unable to get %s clock frequency\n", np->name); 210 } 211 212 static struct nios2_clockevent_dev nios2_ce = { 213 .ced = { 214 .name = "nios2-clkevent", 215 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, 216 .rating = 250, 217 .shift = 32, 218 .set_next_event = nios2_timer_set_next_event, 219 .set_mode = nios2_timer_set_mode, 220 }, 221 }; 222 223 static __init void nios2_clockevent_init(struct device_node *timer) 224 { 225 void __iomem *iobase; 226 u32 freq; 227 int irq; 228 229 nios2_timer_get_base_and_freq(timer, &iobase, &freq); 230 231 irq = irq_of_parse_and_map(timer, 0); 232 if (!irq) 233 panic("Unable to parse timer irq\n"); 234 235 nios2_ce.timer.base = iobase; 236 nios2_ce.timer.freq = freq; 237 238 nios2_ce.ced.cpumask = cpumask_of(0); 239 nios2_ce.ced.irq = irq; 240 241 nios2_timer_stop(&nios2_ce.timer); 242 /* clear pending interrupt */ 243 timer_writew(&nios2_ce.timer, 0, ALTERA_TIMER_STATUS_REG); 244 245 if (request_irq(irq, timer_interrupt, IRQF_TIMER, timer->name, 246 &nios2_ce.ced)) 247 panic("Unable to setup timer irq\n"); 248 249 clockevents_config_and_register(&nios2_ce.ced, freq, 1, ULONG_MAX); 250 } 251 252 static __init void nios2_clocksource_init(struct device_node *timer) 253 { 254 unsigned int ctrl; 255 void __iomem *iobase; 256 u32 freq; 257 258 nios2_timer_get_base_and_freq(timer, &iobase, &freq); 259 260 nios2_cs.timer.base = iobase; 261 nios2_cs.timer.freq = freq; 262 263 clocksource_register_hz(&nios2_cs.cs, freq); 264 265 timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODL_REG); 266 timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODH_REG); 267 268 /* interrupt disable + continuous + start */ 269 ctrl = ALTERA_TIMER_CONTROL_CONT_MSK | ALTERA_TIMER_CONTROL_START_MSK; 270 timer_writew(&nios2_cs.timer, ctrl, ALTERA_TIMER_CONTROL_REG); 271 272 /* Calibrate the delay loop directly */ 273 lpj_fine = freq / HZ; 274 } 275 276 /* 277 * The first timer instance will use as a clockevent. If there are two or 278 * more instances, the second one gets used as clocksource and all 279 * others are unused. 280 */ 281 static void __init nios2_time_init(struct device_node *timer) 282 { 283 static int num_called; 284 285 switch (num_called) { 286 case 0: 287 nios2_clockevent_init(timer); 288 break; 289 case 1: 290 nios2_clocksource_init(timer); 291 break; 292 default: 293 break; 294 } 295 296 num_called++; 297 } 298 299 void read_persistent_clock(struct timespec *ts) 300 { 301 ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0); 302 ts->tv_nsec = 0; 303 } 304 305 void __init time_init(void) 306 { 307 struct device_node *np; 308 int count = 0; 309 310 for_each_compatible_node(np, NULL, ALTR_TIMER_COMPATIBLE) 311 count++; 312 313 if (count < 2) 314 panic("%d timer is found, it needs 2 timers in system\n", count); 315 316 clocksource_of_init(); 317 } 318 319 CLOCKSOURCE_OF_DECLARE(nios2_timer, ALTR_TIMER_COMPATIBLE, nios2_time_init); 320