0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/clockchips.h>
0014 #include <linux/clocksource.h>
0015 #include <linux/sched_clock.h>
0016 #include <linux/slab.h>
0017 #include <linux/bitops.h>
0018 #include <linux/delay.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_irq.h>
0021 #include <linux/platform_device.h>
0022
0023
0024
0025
0026 #define IXP4XX_OSTS_OFFSET 0x00
0027 #define IXP4XX_OST1_OFFSET 0x04
0028 #define IXP4XX_OSRT1_OFFSET 0x08
0029 #define IXP4XX_OST2_OFFSET 0x0C
0030 #define IXP4XX_OSRT2_OFFSET 0x10
0031 #define IXP4XX_OSST_OFFSET 0x20
0032
0033
0034
0035
0036 #define IXP4XX_OST_ENABLE 0x00000001
0037 #define IXP4XX_OST_ONE_SHOT 0x00000002
0038
0039 #define IXP4XX_OST_RELOAD_MASK 0x00000003
0040 #define IXP4XX_OST_DISABLED 0x00000000
0041 #define IXP4XX_OSST_TIMER_1_PEND 0x00000001
0042 #define IXP4XX_OSST_TIMER_2_PEND 0x00000002
0043 #define IXP4XX_OSST_TIMER_TS_PEND 0x00000004
0044
0045
0046 struct ixp4xx_timer {
0047 void __iomem *base;
0048 u32 latch;
0049 struct clock_event_device clkevt;
0050 #ifdef CONFIG_ARM
0051 struct delay_timer delay_timer;
0052 #endif
0053 };
0054
0055
0056
0057
0058
0059 static struct ixp4xx_timer *local_ixp4xx_timer;
0060
0061 static inline struct ixp4xx_timer *
0062 to_ixp4xx_timer(struct clock_event_device *evt)
0063 {
0064 return container_of(evt, struct ixp4xx_timer, clkevt);
0065 }
0066
0067 static unsigned long ixp4xx_read_timer(void)
0068 {
0069 return __raw_readl(local_ixp4xx_timer->base + IXP4XX_OSTS_OFFSET);
0070 }
0071
0072 static u64 notrace ixp4xx_read_sched_clock(void)
0073 {
0074 return ixp4xx_read_timer();
0075 }
0076
0077 static u64 ixp4xx_clocksource_read(struct clocksource *c)
0078 {
0079 return ixp4xx_read_timer();
0080 }
0081
0082 static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
0083 {
0084 struct ixp4xx_timer *tmr = dev_id;
0085 struct clock_event_device *evt = &tmr->clkevt;
0086
0087
0088 __raw_writel(IXP4XX_OSST_TIMER_1_PEND,
0089 tmr->base + IXP4XX_OSST_OFFSET);
0090
0091 evt->event_handler(evt);
0092
0093 return IRQ_HANDLED;
0094 }
0095
0096 static int ixp4xx_set_next_event(unsigned long cycles,
0097 struct clock_event_device *evt)
0098 {
0099 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
0100 u32 val;
0101
0102 val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
0103
0104 val &= IXP4XX_OST_RELOAD_MASK;
0105 __raw_writel((cycles & ~IXP4XX_OST_RELOAD_MASK) | val,
0106 tmr->base + IXP4XX_OSRT1_OFFSET);
0107
0108 return 0;
0109 }
0110
0111 static int ixp4xx_shutdown(struct clock_event_device *evt)
0112 {
0113 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
0114 u32 val;
0115
0116 val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
0117 val &= ~IXP4XX_OST_ENABLE;
0118 __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
0119
0120 return 0;
0121 }
0122
0123 static int ixp4xx_set_oneshot(struct clock_event_device *evt)
0124 {
0125 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
0126
0127 __raw_writel(IXP4XX_OST_ENABLE | IXP4XX_OST_ONE_SHOT,
0128 tmr->base + IXP4XX_OSRT1_OFFSET);
0129
0130 return 0;
0131 }
0132
0133 static int ixp4xx_set_periodic(struct clock_event_device *evt)
0134 {
0135 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
0136 u32 val;
0137
0138 val = tmr->latch & ~IXP4XX_OST_RELOAD_MASK;
0139 val |= IXP4XX_OST_ENABLE;
0140 __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
0141
0142 return 0;
0143 }
0144
0145 static int ixp4xx_resume(struct clock_event_device *evt)
0146 {
0147 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
0148 u32 val;
0149
0150 val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
0151 val |= IXP4XX_OST_ENABLE;
0152 __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
0153
0154 return 0;
0155 }
0156
0157
0158
0159
0160
0161
0162 static __init int ixp4xx_timer_register(void __iomem *base,
0163 int timer_irq,
0164 unsigned int timer_freq)
0165 {
0166 struct ixp4xx_timer *tmr;
0167 int ret;
0168
0169 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
0170 if (!tmr)
0171 return -ENOMEM;
0172 tmr->base = base;
0173
0174
0175
0176
0177
0178
0179
0180 tmr->latch = DIV_ROUND_CLOSEST(timer_freq,
0181 (IXP4XX_OST_RELOAD_MASK + 1) * HZ)
0182 * (IXP4XX_OST_RELOAD_MASK + 1);
0183
0184 local_ixp4xx_timer = tmr;
0185
0186
0187 __raw_writel(0, tmr->base + IXP4XX_OSRT1_OFFSET);
0188
0189
0190 __raw_writel(IXP4XX_OSST_TIMER_1_PEND,
0191 tmr->base + IXP4XX_OSST_OFFSET);
0192
0193
0194 __raw_writel(0, tmr->base + IXP4XX_OSTS_OFFSET);
0195
0196 clocksource_mmio_init(NULL, "OSTS", timer_freq, 200, 32,
0197 ixp4xx_clocksource_read);
0198
0199 tmr->clkevt.name = "ixp4xx timer1";
0200 tmr->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
0201 tmr->clkevt.rating = 200;
0202 tmr->clkevt.set_state_shutdown = ixp4xx_shutdown;
0203 tmr->clkevt.set_state_periodic = ixp4xx_set_periodic;
0204 tmr->clkevt.set_state_oneshot = ixp4xx_set_oneshot;
0205 tmr->clkevt.tick_resume = ixp4xx_resume;
0206 tmr->clkevt.set_next_event = ixp4xx_set_next_event;
0207 tmr->clkevt.cpumask = cpumask_of(0);
0208 tmr->clkevt.irq = timer_irq;
0209 ret = request_irq(timer_irq, ixp4xx_timer_interrupt,
0210 IRQF_TIMER, "IXP4XX-TIMER1", tmr);
0211 if (ret) {
0212 pr_crit("no timer IRQ\n");
0213 return -ENODEV;
0214 }
0215 clockevents_config_and_register(&tmr->clkevt, timer_freq,
0216 0xf, 0xfffffffe);
0217
0218 sched_clock_register(ixp4xx_read_sched_clock, 32, timer_freq);
0219
0220 #ifdef CONFIG_ARM
0221
0222 tmr->delay_timer.read_current_timer = ixp4xx_read_timer;
0223 tmr->delay_timer.freq = timer_freq;
0224 register_current_timer_delay(&tmr->delay_timer);
0225 #endif
0226
0227 return 0;
0228 }
0229
0230 static struct platform_device ixp4xx_watchdog_device = {
0231 .name = "ixp4xx-watchdog",
0232 .id = -1,
0233 };
0234
0235
0236
0237
0238
0239 static int ixp4xx_timer_probe(struct platform_device *pdev)
0240 {
0241 struct device *dev = &pdev->dev;
0242
0243
0244 ixp4xx_watchdog_device.dev.platform_data = local_ixp4xx_timer->base;
0245 ixp4xx_watchdog_device.dev.parent = dev;
0246 return platform_device_register(&ixp4xx_watchdog_device);
0247 }
0248
0249 static const struct of_device_id ixp4xx_timer_dt_id[] = {
0250 { .compatible = "intel,ixp4xx-timer", },
0251 { },
0252 };
0253
0254 static struct platform_driver ixp4xx_timer_driver = {
0255 .probe = ixp4xx_timer_probe,
0256 .driver = {
0257 .name = "ixp4xx-timer",
0258 .of_match_table = ixp4xx_timer_dt_id,
0259 .suppress_bind_attrs = true,
0260 },
0261 };
0262 builtin_platform_driver(ixp4xx_timer_driver);
0263
0264 static __init int ixp4xx_of_timer_init(struct device_node *np)
0265 {
0266 void __iomem *base;
0267 int irq;
0268 int ret;
0269
0270 base = of_iomap(np, 0);
0271 if (!base) {
0272 pr_crit("IXP4xx: can't remap timer\n");
0273 return -ENODEV;
0274 }
0275
0276 irq = irq_of_parse_and_map(np, 0);
0277 if (irq <= 0) {
0278 pr_err("Can't parse IRQ\n");
0279 ret = -EINVAL;
0280 goto out_unmap;
0281 }
0282
0283
0284 ret = ixp4xx_timer_register(base, irq, 66666000);
0285 if (ret)
0286 goto out_unmap;
0287 return 0;
0288
0289 out_unmap:
0290 iounmap(base);
0291 return ret;
0292 }
0293 TIMER_OF_DECLARE(ixp4xx, "intel,ixp4xx-timer", ixp4xx_of_timer_init);