Kuninori Morimoto | 0b9294f | 2018-08-22 02:26:20 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * SuperH Timer Support - TMU |
| 4 | * |
| 5 | * Copyright (C) 2009 Magnus Damm |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 8 | #include <linux/clk.h> |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 9 | #include <linux/clockchips.h> |
Laurent Pinchart | 13931f8 | 2014-02-12 16:56:44 +0100 | [diff] [blame] | 10 | #include <linux/clocksource.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/ioport.h> |
| 17 | #include <linux/irq.h> |
Paul Gortmaker | 7deeab5 | 2011-07-03 13:36:22 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Laurent Pinchart | 3e29b55 | 2014-04-11 16:23:40 +0200 | [diff] [blame] | 19 | #include <linux/of.h> |
Laurent Pinchart | 13931f8 | 2014-02-12 16:56:44 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Rafael J. Wysocki | 2ee619f | 2012-03-13 22:40:00 +0100 | [diff] [blame] | 21 | #include <linux/pm_domain.h> |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
Laurent Pinchart | 13931f8 | 2014-02-12 16:56:44 +0100 | [diff] [blame] | 23 | #include <linux/sh_timer.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/spinlock.h> |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 26 | |
Bartosz Golaszewski | 507fd01 | 2019-10-03 11:29:12 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_SUPERH |
| 28 | #include <asm/platform_early.h> |
| 29 | #endif |
| 30 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 31 | enum sh_tmu_model { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 32 | SH_TMU, |
| 33 | SH_TMU_SH3, |
| 34 | }; |
| 35 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 36 | struct sh_tmu_device; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 37 | |
| 38 | struct sh_tmu_channel { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 39 | struct sh_tmu_device *tmu; |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 40 | unsigned int index; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 41 | |
Laurent Pinchart | de69346 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 42 | void __iomem *base; |
Laurent Pinchart | 1c56cf6 | 2014-02-17 11:27:49 +0100 | [diff] [blame] | 43 | int irq; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 44 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 45 | unsigned long periodic; |
| 46 | struct clock_event_device ced; |
| 47 | struct clocksource cs; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 48 | bool cs_enabled; |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 49 | unsigned int enable_count; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 52 | struct sh_tmu_device { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 53 | struct platform_device *pdev; |
| 54 | |
| 55 | void __iomem *mapbase; |
| 56 | struct clk *clk; |
Nicolai Stange | c3c0a20 | 2017-02-06 22:12:00 +0100 | [diff] [blame] | 57 | unsigned long rate; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 58 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 59 | enum sh_tmu_model model; |
| 60 | |
Laurent Pinchart | 2b027f1 | 2014-02-17 16:49:05 +0100 | [diff] [blame] | 61 | raw_spinlock_t lock; /* Protect the shared start/stop register */ |
| 62 | |
Laurent Pinchart | a5de49f | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 63 | struct sh_tmu_channel *channels; |
| 64 | unsigned int num_channels; |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 65 | |
| 66 | bool has_clockevent; |
| 67 | bool has_clocksource; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 70 | #define TSTR -1 /* shared register */ |
| 71 | #define TCOR 0 /* channel register */ |
| 72 | #define TCNT 1 /* channel register */ |
| 73 | #define TCR 2 /* channel register */ |
| 74 | |
Laurent Pinchart | 5cfe2d1 | 2014-01-29 00:33:08 +0100 | [diff] [blame] | 75 | #define TCR_UNF (1 << 8) |
| 76 | #define TCR_UNIE (1 << 5) |
| 77 | #define TCR_TPSC_CLK4 (0 << 0) |
| 78 | #define TCR_TPSC_CLK16 (1 << 0) |
| 79 | #define TCR_TPSC_CLK64 (2 << 0) |
| 80 | #define TCR_TPSC_CLK256 (3 << 0) |
| 81 | #define TCR_TPSC_CLK1024 (4 << 0) |
| 82 | #define TCR_TPSC_MASK (7 << 0) |
| 83 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 84 | static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 85 | { |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 86 | unsigned long offs; |
| 87 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 88 | if (reg_nr == TSTR) { |
| 89 | switch (ch->tmu->model) { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 90 | case SH_TMU_SH3: |
| 91 | return ioread8(ch->tmu->mapbase + 2); |
| 92 | case SH_TMU: |
| 93 | return ioread8(ch->tmu->mapbase + 4); |
| 94 | } |
| 95 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 96 | |
| 97 | offs = reg_nr << 2; |
| 98 | |
| 99 | if (reg_nr == TCR) |
Laurent Pinchart | de69346 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 100 | return ioread16(ch->base + offs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 101 | else |
Laurent Pinchart | de69346 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 102 | return ioread32(ch->base + offs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 105 | static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 106 | unsigned long value) |
| 107 | { |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 108 | unsigned long offs; |
| 109 | |
| 110 | if (reg_nr == TSTR) { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 111 | switch (ch->tmu->model) { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 112 | case SH_TMU_SH3: |
| 113 | return iowrite8(value, ch->tmu->mapbase + 2); |
| 114 | case SH_TMU: |
| 115 | return iowrite8(value, ch->tmu->mapbase + 4); |
| 116 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | offs = reg_nr << 2; |
| 120 | |
| 121 | if (reg_nr == TCR) |
Laurent Pinchart | de69346 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 122 | iowrite16(value, ch->base + offs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 123 | else |
Laurent Pinchart | de69346 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 124 | iowrite32(value, ch->base + offs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 127 | static void sh_tmu_start_stop_ch(struct sh_tmu_channel *ch, int start) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 128 | { |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 129 | unsigned long flags, value; |
| 130 | |
| 131 | /* start stop register shared by multiple timer channels */ |
Laurent Pinchart | 2b027f1 | 2014-02-17 16:49:05 +0100 | [diff] [blame] | 132 | raw_spin_lock_irqsave(&ch->tmu->lock, flags); |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 133 | value = sh_tmu_read(ch, TSTR); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 134 | |
| 135 | if (start) |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 136 | value |= 1 << ch->index; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 137 | else |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 138 | value &= ~(1 << ch->index); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 139 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 140 | sh_tmu_write(ch, TSTR, value); |
Laurent Pinchart | 2b027f1 | 2014-02-17 16:49:05 +0100 | [diff] [blame] | 141 | raw_spin_unlock_irqrestore(&ch->tmu->lock, flags); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 144 | static int __sh_tmu_enable(struct sh_tmu_channel *ch) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 145 | { |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 146 | int ret; |
| 147 | |
Paul Mundt | d4905ce | 2011-05-31 15:23:20 +0900 | [diff] [blame] | 148 | /* enable clock */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 149 | ret = clk_enable(ch->tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 150 | if (ret) { |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 151 | dev_err(&ch->tmu->pdev->dev, "ch%u: cannot enable clock\n", |
| 152 | ch->index); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /* make sure channel is disabled */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 157 | sh_tmu_start_stop_ch(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 158 | |
| 159 | /* maximum timeout */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 160 | sh_tmu_write(ch, TCOR, 0xffffffff); |
| 161 | sh_tmu_write(ch, TCNT, 0xffffffff); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 162 | |
| 163 | /* configure channel to parent clock / 4, irq off */ |
Laurent Pinchart | 5cfe2d1 | 2014-01-29 00:33:08 +0100 | [diff] [blame] | 164 | sh_tmu_write(ch, TCR, TCR_TPSC_CLK4); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 165 | |
| 166 | /* enable channel */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 167 | sh_tmu_start_stop_ch(ch, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 172 | static int sh_tmu_enable(struct sh_tmu_channel *ch) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 173 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 174 | if (ch->enable_count++ > 0) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 175 | return 0; |
| 176 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 177 | pm_runtime_get_sync(&ch->tmu->pdev->dev); |
| 178 | dev_pm_syscore_device(&ch->tmu->pdev->dev, true); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 179 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 180 | return __sh_tmu_enable(ch); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 183 | static void __sh_tmu_disable(struct sh_tmu_channel *ch) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 184 | { |
| 185 | /* disable channel */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 186 | sh_tmu_start_stop_ch(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 187 | |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 188 | /* disable interrupts in TMU block */ |
Laurent Pinchart | 5cfe2d1 | 2014-01-29 00:33:08 +0100 | [diff] [blame] | 189 | sh_tmu_write(ch, TCR, TCR_TPSC_CLK4); |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 190 | |
Paul Mundt | d4905ce | 2011-05-31 15:23:20 +0900 | [diff] [blame] | 191 | /* stop clock */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 192 | clk_disable(ch->tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 195 | static void sh_tmu_disable(struct sh_tmu_channel *ch) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 196 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 197 | if (WARN_ON(ch->enable_count == 0)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 198 | return; |
| 199 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 200 | if (--ch->enable_count > 0) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 201 | return; |
| 202 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 203 | __sh_tmu_disable(ch); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 204 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 205 | dev_pm_syscore_device(&ch->tmu->pdev->dev, false); |
| 206 | pm_runtime_put(&ch->tmu->pdev->dev); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 209 | static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 210 | int periodic) |
| 211 | { |
| 212 | /* stop timer */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 213 | sh_tmu_start_stop_ch(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 214 | |
| 215 | /* acknowledge interrupt */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 216 | sh_tmu_read(ch, TCR); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 217 | |
| 218 | /* enable interrupt */ |
Laurent Pinchart | 5cfe2d1 | 2014-01-29 00:33:08 +0100 | [diff] [blame] | 219 | sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 220 | |
| 221 | /* reload delta value in case of periodic timer */ |
| 222 | if (periodic) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 223 | sh_tmu_write(ch, TCOR, delta); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 224 | else |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 225 | sh_tmu_write(ch, TCOR, 0xffffffff); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 226 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 227 | sh_tmu_write(ch, TCNT, delta); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 228 | |
| 229 | /* start timer */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 230 | sh_tmu_start_stop_ch(ch, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id) |
| 234 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 235 | struct sh_tmu_channel *ch = dev_id; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 236 | |
| 237 | /* disable or acknowledge interrupt */ |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 238 | if (clockevent_state_oneshot(&ch->ced)) |
Laurent Pinchart | 5cfe2d1 | 2014-01-29 00:33:08 +0100 | [diff] [blame] | 239 | sh_tmu_write(ch, TCR, TCR_TPSC_CLK4); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 240 | else |
Laurent Pinchart | 5cfe2d1 | 2014-01-29 00:33:08 +0100 | [diff] [blame] | 241 | sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 242 | |
| 243 | /* notify clockevent layer */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 244 | ch->ced.event_handler(&ch->ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 245 | return IRQ_HANDLED; |
| 246 | } |
| 247 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 248 | static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 249 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 250 | return container_of(cs, struct sh_tmu_channel, cs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 253 | static u64 sh_tmu_clocksource_read(struct clocksource *cs) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 254 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 255 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 256 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 257 | return sh_tmu_read(ch, TCNT) ^ 0xffffffff; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int sh_tmu_clocksource_enable(struct clocksource *cs) |
| 261 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 262 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Magnus Damm | 0aeac45 | 2011-04-25 22:38:37 +0900 | [diff] [blame] | 263 | int ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 264 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 265 | if (WARN_ON(ch->cs_enabled)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 266 | return 0; |
| 267 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 268 | ret = sh_tmu_enable(ch); |
Nicolai Stange | c3c0a20 | 2017-02-06 22:12:00 +0100 | [diff] [blame] | 269 | if (!ret) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 270 | ch->cs_enabled = true; |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 271 | |
Magnus Damm | 0aeac45 | 2011-04-25 22:38:37 +0900 | [diff] [blame] | 272 | return ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void sh_tmu_clocksource_disable(struct clocksource *cs) |
| 276 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 277 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 278 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 279 | if (WARN_ON(!ch->cs_enabled)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 280 | return; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 281 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 282 | sh_tmu_disable(ch); |
| 283 | ch->cs_enabled = false; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void sh_tmu_clocksource_suspend(struct clocksource *cs) |
| 287 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 288 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 289 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 290 | if (!ch->cs_enabled) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 291 | return; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 292 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 293 | if (--ch->enable_count == 0) { |
| 294 | __sh_tmu_disable(ch); |
Ulf Hansson | fc51989 | 2020-11-03 16:06:25 +0100 | [diff] [blame] | 295 | dev_pm_genpd_suspend(&ch->tmu->pdev->dev); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 296 | } |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void sh_tmu_clocksource_resume(struct clocksource *cs) |
| 300 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 301 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 302 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 303 | if (!ch->cs_enabled) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 304 | return; |
| 305 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 306 | if (ch->enable_count++ == 0) { |
Ulf Hansson | fc51989 | 2020-11-03 16:06:25 +0100 | [diff] [blame] | 307 | dev_pm_genpd_resume(&ch->tmu->pdev->dev); |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 308 | __sh_tmu_enable(ch); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 309 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 312 | static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch, |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 313 | const char *name) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 314 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 315 | struct clocksource *cs = &ch->cs; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 316 | |
| 317 | cs->name = name; |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 318 | cs->rating = 200; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 319 | cs->read = sh_tmu_clocksource_read; |
| 320 | cs->enable = sh_tmu_clocksource_enable; |
| 321 | cs->disable = sh_tmu_clocksource_disable; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 322 | cs->suspend = sh_tmu_clocksource_suspend; |
| 323 | cs->resume = sh_tmu_clocksource_resume; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 324 | cs->mask = CLOCKSOURCE_MASK(32); |
| 325 | cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; |
Aurelien Jarno | 66f4912 | 2010-05-31 21:45:48 +0000 | [diff] [blame] | 326 | |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 327 | dev_info(&ch->tmu->pdev->dev, "ch%u: used as clock source\n", |
| 328 | ch->index); |
Magnus Damm | 0aeac45 | 2011-04-25 22:38:37 +0900 | [diff] [blame] | 329 | |
Nicolai Stange | c3c0a20 | 2017-02-06 22:12:00 +0100 | [diff] [blame] | 330 | clocksource_register_hz(cs, ch->tmu->rate); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 334 | static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 335 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 336 | return container_of(ced, struct sh_tmu_channel, ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 339 | static void sh_tmu_clock_event_start(struct sh_tmu_channel *ch, int periodic) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 340 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 341 | sh_tmu_enable(ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 342 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 343 | if (periodic) { |
Nicolai Stange | c3c0a20 | 2017-02-06 22:12:00 +0100 | [diff] [blame] | 344 | ch->periodic = (ch->tmu->rate + HZ/2) / HZ; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 345 | sh_tmu_set_next(ch, ch->periodic, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 349 | static int sh_tmu_clock_event_shutdown(struct clock_event_device *ced) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 350 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 351 | struct sh_tmu_channel *ch = ced_to_sh_tmu(ced); |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 352 | |
Viresh Kumar | 452b132 | 2015-07-21 08:01:14 +0530 | [diff] [blame] | 353 | if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced)) |
| 354 | sh_tmu_disable(ch); |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int sh_tmu_clock_event_set_state(struct clock_event_device *ced, |
| 359 | int periodic) |
| 360 | { |
| 361 | struct sh_tmu_channel *ch = ced_to_sh_tmu(ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 362 | |
| 363 | /* deal with old setting first */ |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 364 | if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced)) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 365 | sh_tmu_disable(ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 366 | |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 367 | dev_info(&ch->tmu->pdev->dev, "ch%u: used for %s clock events\n", |
| 368 | ch->index, periodic ? "periodic" : "oneshot"); |
| 369 | sh_tmu_clock_event_start(ch, periodic); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int sh_tmu_clock_event_set_oneshot(struct clock_event_device *ced) |
| 374 | { |
| 375 | return sh_tmu_clock_event_set_state(ced, 0); |
| 376 | } |
| 377 | |
| 378 | static int sh_tmu_clock_event_set_periodic(struct clock_event_device *ced) |
| 379 | { |
| 380 | return sh_tmu_clock_event_set_state(ced, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static int sh_tmu_clock_event_next(unsigned long delta, |
| 384 | struct clock_event_device *ced) |
| 385 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 386 | struct sh_tmu_channel *ch = ced_to_sh_tmu(ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 387 | |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 388 | BUG_ON(!clockevent_state_oneshot(ced)); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 389 | |
| 390 | /* program new delta value */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 391 | sh_tmu_set_next(ch, delta, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 395 | static void sh_tmu_clock_event_suspend(struct clock_event_device *ced) |
| 396 | { |
Ulf Hansson | fc51989 | 2020-11-03 16:06:25 +0100 | [diff] [blame] | 397 | dev_pm_genpd_suspend(&ced_to_sh_tmu(ced)->tmu->pdev->dev); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static void sh_tmu_clock_event_resume(struct clock_event_device *ced) |
| 401 | { |
Ulf Hansson | fc51989 | 2020-11-03 16:06:25 +0100 | [diff] [blame] | 402 | dev_pm_genpd_resume(&ced_to_sh_tmu(ced)->tmu->pdev->dev); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 405 | static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch, |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 406 | const char *name) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 407 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 408 | struct clock_event_device *ced = &ch->ced; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 409 | int ret; |
| 410 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 411 | ced->name = name; |
| 412 | ced->features = CLOCK_EVT_FEAT_PERIODIC; |
| 413 | ced->features |= CLOCK_EVT_FEAT_ONESHOT; |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 414 | ced->rating = 200; |
Magnus Damm | f2a5473 | 2014-12-16 18:48:54 +0900 | [diff] [blame] | 415 | ced->cpumask = cpu_possible_mask; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 416 | ced->set_next_event = sh_tmu_clock_event_next; |
Viresh Kumar | 2bcc4da | 2015-06-18 16:24:36 +0530 | [diff] [blame] | 417 | ced->set_state_shutdown = sh_tmu_clock_event_shutdown; |
| 418 | ced->set_state_periodic = sh_tmu_clock_event_set_periodic; |
| 419 | ced->set_state_oneshot = sh_tmu_clock_event_set_oneshot; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 420 | ced->suspend = sh_tmu_clock_event_suspend; |
| 421 | ced->resume = sh_tmu_clock_event_resume; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 422 | |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 423 | dev_info(&ch->tmu->pdev->dev, "ch%u: used for clock events\n", |
| 424 | ch->index); |
Paul Mundt | 3977407 | 2012-06-11 17:10:16 +0900 | [diff] [blame] | 425 | |
Nicolai Stange | c3c0a20 | 2017-02-06 22:12:00 +0100 | [diff] [blame] | 426 | clockevents_config_and_register(ced, ch->tmu->rate, 0x300, 0xffffffff); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 427 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 428 | ret = request_irq(ch->irq, sh_tmu_interrupt, |
Laurent Pinchart | 1c56cf6 | 2014-02-17 11:27:49 +0100 | [diff] [blame] | 429 | IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING, |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 430 | dev_name(&ch->tmu->pdev->dev), ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 431 | if (ret) { |
Laurent Pinchart | fe68eb8 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 432 | dev_err(&ch->tmu->pdev->dev, "ch%u: failed to request irq %d\n", |
| 433 | ch->index, ch->irq); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Laurent Pinchart | 84876d0 | 2014-02-17 16:04:16 +0100 | [diff] [blame] | 438 | static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name, |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 439 | bool clockevent, bool clocksource) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 440 | { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 441 | if (clockevent) { |
| 442 | ch->tmu->has_clockevent = true; |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 443 | sh_tmu_register_clockevent(ch, name); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 444 | } else if (clocksource) { |
| 445 | ch->tmu->has_clocksource = true; |
Laurent Pinchart | f1010ed | 2014-02-19 17:00:31 +0100 | [diff] [blame] | 446 | sh_tmu_register_clocksource(ch, name); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 447 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 452 | static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int index, |
| 453 | bool clockevent, bool clocksource, |
Laurent Pinchart | a94ddaa | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 454 | struct sh_tmu_device *tmu) |
| 455 | { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 456 | /* Skip unused channels. */ |
| 457 | if (!clockevent && !clocksource) |
| 458 | return 0; |
Laurent Pinchart | a94ddaa | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 459 | |
Laurent Pinchart | a94ddaa | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 460 | ch->tmu = tmu; |
Laurent Pinchart | 681b9e8 | 2014-01-28 15:52:46 +0100 | [diff] [blame] | 461 | ch->index = index; |
Laurent Pinchart | a94ddaa | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 462 | |
Laurent Pinchart | 681b9e8 | 2014-01-28 15:52:46 +0100 | [diff] [blame] | 463 | if (tmu->model == SH_TMU_SH3) |
| 464 | ch->base = tmu->mapbase + 4 + ch->index * 12; |
| 465 | else |
| 466 | ch->base = tmu->mapbase + 8 + ch->index * 12; |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 467 | |
Laurent Pinchart | c54697a | 2014-05-16 14:44:23 +0200 | [diff] [blame] | 468 | ch->irq = platform_get_irq(tmu->pdev, index); |
Stephen Boyd | 9f475d0 | 2019-07-30 11:15:04 -0700 | [diff] [blame] | 469 | if (ch->irq < 0) |
Laurent Pinchart | a94ddaa | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 470 | return ch->irq; |
Laurent Pinchart | a94ddaa | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 471 | |
| 472 | ch->cs_enabled = false; |
| 473 | ch->enable_count = 0; |
| 474 | |
Laurent Pinchart | 84876d0 | 2014-02-17 16:04:16 +0100 | [diff] [blame] | 475 | return sh_tmu_register(ch, dev_name(&tmu->pdev->dev), |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 476 | clockevent, clocksource); |
| 477 | } |
| 478 | |
| 479 | static int sh_tmu_map_memory(struct sh_tmu_device *tmu) |
| 480 | { |
| 481 | struct resource *res; |
| 482 | |
| 483 | res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0); |
| 484 | if (!res) { |
| 485 | dev_err(&tmu->pdev->dev, "failed to get I/O memory\n"); |
| 486 | return -ENXIO; |
| 487 | } |
| 488 | |
Christoph Hellwig | 4bdc0d6 | 2020-01-06 09:43:50 +0100 | [diff] [blame] | 489 | tmu->mapbase = ioremap(res->start, resource_size(res)); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 490 | if (tmu->mapbase == NULL) |
| 491 | return -ENXIO; |
| 492 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Laurent Pinchart | 3e29b55 | 2014-04-11 16:23:40 +0200 | [diff] [blame] | 496 | static int sh_tmu_parse_dt(struct sh_tmu_device *tmu) |
| 497 | { |
| 498 | struct device_node *np = tmu->pdev->dev.of_node; |
| 499 | |
| 500 | tmu->model = SH_TMU; |
| 501 | tmu->num_channels = 3; |
| 502 | |
| 503 | of_property_read_u32(np, "#renesas,channels", &tmu->num_channels); |
| 504 | |
| 505 | if (tmu->num_channels != 2 && tmu->num_channels != 3) { |
| 506 | dev_err(&tmu->pdev->dev, "invalid number of channels %u\n", |
| 507 | tmu->num_channels); |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 514 | static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 515 | { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 516 | unsigned int i; |
Laurent Pinchart | 1c56cf6 | 2014-02-17 11:27:49 +0100 | [diff] [blame] | 517 | int ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 518 | |
Laurent Pinchart | 3e29b55 | 2014-04-11 16:23:40 +0200 | [diff] [blame] | 519 | tmu->pdev = pdev; |
| 520 | |
| 521 | raw_spin_lock_init(&tmu->lock); |
| 522 | |
| 523 | if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { |
| 524 | ret = sh_tmu_parse_dt(tmu); |
| 525 | if (ret < 0) |
| 526 | return ret; |
| 527 | } else if (pdev->dev.platform_data) { |
| 528 | const struct platform_device_id *id = pdev->id_entry; |
| 529 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
| 530 | |
| 531 | tmu->model = id->driver_data; |
| 532 | tmu->num_channels = hweight8(cfg->channels_mask); |
| 533 | } else { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 534 | dev_err(&tmu->pdev->dev, "missing platform data\n"); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 535 | return -ENXIO; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 538 | /* Get hold of clock. */ |
Laurent Pinchart | 681b9e8 | 2014-01-28 15:52:46 +0100 | [diff] [blame] | 539 | tmu->clk = clk_get(&tmu->pdev->dev, "fck"); |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 540 | if (IS_ERR(tmu->clk)) { |
| 541 | dev_err(&tmu->pdev->dev, "cannot get clock\n"); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 542 | return PTR_ERR(tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 543 | } |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 544 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 545 | ret = clk_prepare(tmu->clk); |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 546 | if (ret < 0) |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 547 | goto err_clk_put; |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 548 | |
Nicolai Stange | c3c0a20 | 2017-02-06 22:12:00 +0100 | [diff] [blame] | 549 | /* Determine clock rate. */ |
| 550 | ret = clk_enable(tmu->clk); |
| 551 | if (ret < 0) |
| 552 | goto err_clk_unprepare; |
| 553 | |
| 554 | tmu->rate = clk_get_rate(tmu->clk) / 4; |
| 555 | clk_disable(tmu->clk); |
| 556 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 557 | /* Map the memory resource. */ |
| 558 | ret = sh_tmu_map_memory(tmu); |
| 559 | if (ret < 0) { |
| 560 | dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n"); |
| 561 | goto err_clk_unprepare; |
Laurent Pinchart | a5de49f | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 564 | /* Allocate and setup the channels. */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 565 | tmu->channels = kcalloc(tmu->num_channels, sizeof(*tmu->channels), |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 566 | GFP_KERNEL); |
| 567 | if (tmu->channels == NULL) { |
| 568 | ret = -ENOMEM; |
| 569 | goto err_unmap; |
| 570 | } |
Laurent Pinchart | a5de49f | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 571 | |
Laurent Pinchart | 681b9e8 | 2014-01-28 15:52:46 +0100 | [diff] [blame] | 572 | /* |
| 573 | * Use the first channel as a clock event device and the second channel |
| 574 | * as a clock source. |
| 575 | */ |
| 576 | for (i = 0; i < tmu->num_channels; ++i) { |
| 577 | ret = sh_tmu_channel_setup(&tmu->channels[i], i, |
| 578 | i == 0, i == 1, tmu); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 579 | if (ret < 0) |
| 580 | goto err_unmap; |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | platform_set_drvdata(pdev, tmu); |
Laurent Pinchart | 394a448 | 2013-11-08 11:07:59 +0100 | [diff] [blame] | 584 | |
| 585 | return 0; |
| 586 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 587 | err_unmap: |
Laurent Pinchart | a5de49f | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 588 | kfree(tmu->channels); |
Laurent Pinchart | 681b9e8 | 2014-01-28 15:52:46 +0100 | [diff] [blame] | 589 | iounmap(tmu->mapbase); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 590 | err_clk_unprepare: |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 591 | clk_unprepare(tmu->clk); |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 592 | err_clk_put: |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 593 | clk_put(tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 594 | return ret; |
| 595 | } |
| 596 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 597 | static int sh_tmu_probe(struct platform_device *pdev) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 598 | { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 599 | struct sh_tmu_device *tmu = platform_get_drvdata(pdev); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 600 | int ret; |
| 601 | |
Bartosz Golaszewski | 201e910 | 2019-10-03 11:29:13 +0200 | [diff] [blame] | 602 | if (!is_sh_early_platform_device(pdev)) { |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 603 | pm_runtime_set_active(&pdev->dev); |
| 604 | pm_runtime_enable(&pdev->dev); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 605 | } |
Rafael J. Wysocki | 2ee619f | 2012-03-13 22:40:00 +0100 | [diff] [blame] | 606 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 607 | if (tmu) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 608 | dev_info(&pdev->dev, "kept as earlytimer\n"); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 609 | goto out; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Laurent Pinchart | 3b77a83 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 612 | tmu = kzalloc(sizeof(*tmu), GFP_KERNEL); |
Jingoo Han | 814876b | 2014-05-22 14:05:07 +0200 | [diff] [blame] | 613 | if (tmu == NULL) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 614 | return -ENOMEM; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 615 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 616 | ret = sh_tmu_setup(tmu, pdev); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 617 | if (ret) { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 618 | kfree(tmu); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 619 | pm_runtime_idle(&pdev->dev); |
| 620 | return ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 621 | } |
Bartosz Golaszewski | 507fd01 | 2019-10-03 11:29:12 +0200 | [diff] [blame] | 622 | |
Bartosz Golaszewski | 201e910 | 2019-10-03 11:29:13 +0200 | [diff] [blame] | 623 | if (is_sh_early_platform_device(pdev)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 624 | return 0; |
| 625 | |
| 626 | out: |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 627 | if (tmu->has_clockevent || tmu->has_clocksource) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 628 | pm_runtime_irq_safe(&pdev->dev); |
| 629 | else |
| 630 | pm_runtime_idle(&pdev->dev); |
| 631 | |
| 632 | return 0; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 635 | static int sh_tmu_remove(struct platform_device *pdev) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 636 | { |
| 637 | return -EBUSY; /* cannot unregister clockevent and clocksource */ |
| 638 | } |
| 639 | |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 640 | static const struct platform_device_id sh_tmu_id_table[] = { |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 641 | { "sh-tmu", SH_TMU }, |
| 642 | { "sh-tmu-sh3", SH_TMU_SH3 }, |
| 643 | { } |
| 644 | }; |
| 645 | MODULE_DEVICE_TABLE(platform, sh_tmu_id_table); |
| 646 | |
Laurent Pinchart | 3e29b55 | 2014-04-11 16:23:40 +0200 | [diff] [blame] | 647 | static const struct of_device_id sh_tmu_of_table[] __maybe_unused = { |
| 648 | { .compatible = "renesas,tmu" }, |
| 649 | { } |
| 650 | }; |
| 651 | MODULE_DEVICE_TABLE(of, sh_tmu_of_table); |
| 652 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 653 | static struct platform_driver sh_tmu_device_driver = { |
| 654 | .probe = sh_tmu_probe, |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 655 | .remove = sh_tmu_remove, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 656 | .driver = { |
| 657 | .name = "sh_tmu", |
Laurent Pinchart | 3e29b55 | 2014-04-11 16:23:40 +0200 | [diff] [blame] | 658 | .of_match_table = of_match_ptr(sh_tmu_of_table), |
Laurent Pinchart | 8c7f21e | 2014-01-28 12:36:48 +0100 | [diff] [blame] | 659 | }, |
| 660 | .id_table = sh_tmu_id_table, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | static int __init sh_tmu_init(void) |
| 664 | { |
| 665 | return platform_driver_register(&sh_tmu_device_driver); |
| 666 | } |
| 667 | |
| 668 | static void __exit sh_tmu_exit(void) |
| 669 | { |
| 670 | platform_driver_unregister(&sh_tmu_device_driver); |
| 671 | } |
| 672 | |
Bartosz Golaszewski | 507fd01 | 2019-10-03 11:29:12 +0200 | [diff] [blame] | 673 | #ifdef CONFIG_SUPERH |
Bartosz Golaszewski | 201e910 | 2019-10-03 11:29:13 +0200 | [diff] [blame] | 674 | sh_early_platform_init("earlytimer", &sh_tmu_device_driver); |
Bartosz Golaszewski | 507fd01 | 2019-10-03 11:29:12 +0200 | [diff] [blame] | 675 | #endif |
| 676 | |
Simon Horman | b9773c3 | 2013-03-05 15:40:42 +0900 | [diff] [blame] | 677 | subsys_initcall(sh_tmu_init); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 678 | module_exit(sh_tmu_exit); |
| 679 | |
| 680 | MODULE_AUTHOR("Magnus Damm"); |
| 681 | MODULE_DESCRIPTION("SuperH TMU Timer Driver"); |
| 682 | MODULE_LICENSE("GPL v2"); |