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