Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Timer Support - CMT |
| 3 | * |
| 4 | * Copyright (C) 2008 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> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/irq.h> |
| 28 | #include <linux/err.h> |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 29 | #include <linux/delay.h> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 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 | 615a445 | 2012-03-13 22:40:06 +0100 | [diff] [blame] | 35 | #include <linux/pm_domain.h> |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 36 | #include <linux/pm_runtime.h> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 37 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 38 | struct sh_cmt_device; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 39 | |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 40 | /* |
| 41 | * The CMT comes in 5 different identified flavours, depending not only on the |
| 42 | * SoC but also on the particular instance. The following table lists the main |
| 43 | * characteristics of those flavours. |
| 44 | * |
| 45 | * 16B 32B 32B-F 48B 48B-2 |
| 46 | * ----------------------------------------------------------------------------- |
| 47 | * Channels 2 1/4 1 6 2/8 |
| 48 | * Control Width 16 16 16 16 32 |
| 49 | * Counter Width 16 32 32 32/48 32/48 |
| 50 | * Shared Start/Stop Y Y Y Y N |
| 51 | * |
| 52 | * The 48-bit gen2 version has a per-channel start/stop register located in the |
| 53 | * channel registers block. All other versions have a shared start/stop register |
| 54 | * located in the global space. |
| 55 | * |
| 56 | * Note that CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit |
| 57 | * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable. |
| 58 | */ |
| 59 | |
| 60 | enum sh_cmt_model { |
| 61 | SH_CMT_16BIT, |
| 62 | SH_CMT_32BIT, |
| 63 | SH_CMT_32BIT_FAST, |
| 64 | SH_CMT_48BIT, |
| 65 | SH_CMT_48BIT_GEN2, |
| 66 | }; |
| 67 | |
| 68 | struct sh_cmt_info { |
| 69 | enum sh_cmt_model model; |
| 70 | |
| 71 | unsigned long width; /* 16 or 32 bit version of hardware block */ |
| 72 | unsigned long overflow_bit; |
| 73 | unsigned long clear_bits; |
| 74 | |
| 75 | /* callbacks for CMSTR and CMCSR access */ |
| 76 | unsigned long (*read_control)(void __iomem *base, unsigned long offs); |
| 77 | void (*write_control)(void __iomem *base, unsigned long offs, |
| 78 | unsigned long value); |
| 79 | |
| 80 | /* callbacks for CMCNT and CMCOR access */ |
| 81 | unsigned long (*read_count)(void __iomem *base, unsigned long offs); |
| 82 | void (*write_count)(void __iomem *base, unsigned long offs, |
| 83 | unsigned long value); |
| 84 | }; |
| 85 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 86 | struct sh_cmt_channel { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 87 | struct sh_cmt_device *cmt; |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 88 | unsigned int index; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 89 | |
Laurent Pinchart | c924d2d | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 90 | void __iomem *base; |
| 91 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 92 | unsigned long flags; |
| 93 | unsigned long match_value; |
| 94 | unsigned long next_match_value; |
| 95 | unsigned long max_match_value; |
| 96 | unsigned long rate; |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 97 | raw_spinlock_t lock; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 98 | struct clock_event_device ced; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 99 | struct clocksource cs; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 100 | unsigned long total_cycles; |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 101 | bool cs_enabled; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 104 | struct sh_cmt_device { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 105 | struct platform_device *pdev; |
| 106 | |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 107 | const struct sh_cmt_info *info; |
| 108 | |
Laurent Pinchart | 36f1ac9 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 109 | void __iomem *mapbase_ch; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 110 | void __iomem *mapbase; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 111 | struct clk *clk; |
| 112 | |
Laurent Pinchart | f5ec9b1 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 113 | struct sh_cmt_channel *channels; |
| 114 | unsigned int num_channels; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 117 | static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs) |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 118 | { |
| 119 | return ioread16(base + (offs << 1)); |
| 120 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 121 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 122 | static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs) |
| 123 | { |
| 124 | return ioread32(base + (offs << 2)); |
| 125 | } |
| 126 | |
| 127 | static void sh_cmt_write16(void __iomem *base, unsigned long offs, |
| 128 | unsigned long value) |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 129 | { |
| 130 | iowrite16(value, base + (offs << 1)); |
| 131 | } |
| 132 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 133 | static void sh_cmt_write32(void __iomem *base, unsigned long offs, |
| 134 | unsigned long value) |
| 135 | { |
| 136 | iowrite32(value, base + (offs << 2)); |
| 137 | } |
| 138 | |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 139 | static const struct sh_cmt_info sh_cmt_info[] = { |
| 140 | [SH_CMT_16BIT] = { |
| 141 | .model = SH_CMT_16BIT, |
| 142 | .width = 16, |
| 143 | .overflow_bit = 0x80, |
| 144 | .clear_bits = ~0x80, |
| 145 | .read_control = sh_cmt_read16, |
| 146 | .write_control = sh_cmt_write16, |
| 147 | .read_count = sh_cmt_read16, |
| 148 | .write_count = sh_cmt_write16, |
| 149 | }, |
| 150 | [SH_CMT_32BIT] = { |
| 151 | .model = SH_CMT_32BIT, |
| 152 | .width = 32, |
| 153 | .overflow_bit = 0x8000, |
| 154 | .clear_bits = ~0xc000, |
| 155 | .read_control = sh_cmt_read16, |
| 156 | .write_control = sh_cmt_write16, |
| 157 | .read_count = sh_cmt_read32, |
| 158 | .write_count = sh_cmt_write32, |
| 159 | }, |
| 160 | [SH_CMT_32BIT_FAST] = { |
| 161 | .model = SH_CMT_32BIT_FAST, |
| 162 | .width = 32, |
| 163 | .overflow_bit = 0x8000, |
| 164 | .clear_bits = ~0xc000, |
| 165 | .read_control = sh_cmt_read16, |
| 166 | .write_control = sh_cmt_write16, |
| 167 | .read_count = sh_cmt_read32, |
| 168 | .write_count = sh_cmt_write32, |
| 169 | }, |
| 170 | [SH_CMT_48BIT] = { |
| 171 | .model = SH_CMT_48BIT, |
| 172 | .width = 32, |
| 173 | .overflow_bit = 0x8000, |
| 174 | .clear_bits = ~0xc000, |
| 175 | .read_control = sh_cmt_read32, |
| 176 | .write_control = sh_cmt_write32, |
| 177 | .read_count = sh_cmt_read32, |
| 178 | .write_count = sh_cmt_write32, |
| 179 | }, |
| 180 | [SH_CMT_48BIT_GEN2] = { |
| 181 | .model = SH_CMT_48BIT_GEN2, |
| 182 | .width = 32, |
| 183 | .overflow_bit = 0x8000, |
| 184 | .clear_bits = ~0xc000, |
| 185 | .read_control = sh_cmt_read32, |
| 186 | .write_control = sh_cmt_write32, |
| 187 | .read_count = sh_cmt_read32, |
| 188 | .write_count = sh_cmt_write32, |
| 189 | }, |
| 190 | }; |
| 191 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 192 | #define CMCSR 0 /* channel register */ |
| 193 | #define CMCNT 1 /* channel register */ |
| 194 | #define CMCOR 2 /* channel register */ |
| 195 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 196 | static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch) |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 197 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 198 | return ch->cmt->info->read_control(ch->cmt->mapbase, 0); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 201 | static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch) |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 202 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 203 | return ch->cmt->info->read_control(ch->base, CMCSR); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 204 | } |
| 205 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 206 | static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch) |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 207 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 208 | return ch->cmt->info->read_count(ch->base, CMCNT); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 211 | static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch, |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 212 | unsigned long value) |
| 213 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 214 | ch->cmt->info->write_control(ch->cmt->mapbase, 0, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 217 | static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch, |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 218 | unsigned long value) |
| 219 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 220 | ch->cmt->info->write_control(ch->base, CMCSR, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 221 | } |
| 222 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 223 | static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch, |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 224 | unsigned long value) |
| 225 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 226 | ch->cmt->info->write_count(ch->base, CMCNT, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 229 | static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch, |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 230 | unsigned long value) |
| 231 | { |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 232 | ch->cmt->info->write_count(ch->base, CMCOR, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 233 | } |
| 234 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 235 | static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch, |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 236 | int *has_wrapped) |
| 237 | { |
| 238 | unsigned long v1, v2, v3; |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 239 | int o1, o2; |
| 240 | |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 241 | o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 242 | |
| 243 | /* Make sure the timer value is stable. Stolen from acpi_pm.c */ |
| 244 | do { |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 245 | o2 = o1; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 246 | v1 = sh_cmt_read_cmcnt(ch); |
| 247 | v2 = sh_cmt_read_cmcnt(ch); |
| 248 | v3 = sh_cmt_read_cmcnt(ch); |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 249 | o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit; |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 250 | } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3) |
| 251 | || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2))); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 252 | |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 253 | *has_wrapped = o1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 254 | return v2; |
| 255 | } |
| 256 | |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 257 | static DEFINE_RAW_SPINLOCK(sh_cmt_lock); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 258 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 259 | static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 260 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 261 | struct sh_timer_config *cfg = ch->cmt->pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 262 | unsigned long flags, value; |
| 263 | |
| 264 | /* start stop register shared by multiple timer channels */ |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 265 | raw_spin_lock_irqsave(&sh_cmt_lock, flags); |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 266 | value = sh_cmt_read_cmstr(ch); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 267 | |
| 268 | if (start) |
| 269 | value |= 1 << cfg->timer_bit; |
| 270 | else |
| 271 | value &= ~(1 << cfg->timer_bit); |
| 272 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 273 | sh_cmt_write_cmstr(ch, value); |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 274 | raw_spin_unlock_irqrestore(&sh_cmt_lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 277 | static int sh_cmt_enable(struct sh_cmt_channel *ch, unsigned long *rate) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 278 | { |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 279 | int k, ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 280 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 281 | pm_runtime_get_sync(&ch->cmt->pdev->dev); |
| 282 | dev_pm_syscore_device(&ch->cmt->pdev->dev, true); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 283 | |
Paul Mundt | 9436b4a | 2011-05-31 15:26:42 +0900 | [diff] [blame] | 284 | /* enable clock */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 285 | ret = clk_enable(ch->cmt->clk); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 286 | if (ret) { |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 287 | dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n", |
| 288 | ch->index); |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 289 | goto err0; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 290 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 291 | |
| 292 | /* make sure channel is disabled */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 293 | sh_cmt_start_stop_ch(ch, 0); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 294 | |
| 295 | /* configure channel, periodic mode and maximum timeout */ |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 296 | if (ch->cmt->info->width == 16) { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 297 | *rate = clk_get_rate(ch->cmt->clk) / 512; |
| 298 | sh_cmt_write_cmcsr(ch, 0x43); |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 299 | } else { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 300 | *rate = clk_get_rate(ch->cmt->clk) / 8; |
| 301 | sh_cmt_write_cmcsr(ch, 0x01a4); |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 302 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 303 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 304 | sh_cmt_write_cmcor(ch, 0xffffffff); |
| 305 | sh_cmt_write_cmcnt(ch, 0); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 306 | |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 307 | /* |
| 308 | * According to the sh73a0 user's manual, as CMCNT can be operated |
| 309 | * only by the RCLK (Pseudo 32 KHz), there's one restriction on |
| 310 | * modifying CMCNT register; two RCLK cycles are necessary before |
| 311 | * this register is either read or any modification of the value |
| 312 | * it holds is reflected in the LSI's actual operation. |
| 313 | * |
| 314 | * While at it, we're supposed to clear out the CMCNT as of this |
| 315 | * moment, so make sure it's processed properly here. This will |
| 316 | * take RCLKx2 at maximum. |
| 317 | */ |
| 318 | for (k = 0; k < 100; k++) { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 319 | if (!sh_cmt_read_cmcnt(ch)) |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 320 | break; |
| 321 | udelay(1); |
| 322 | } |
| 323 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 324 | if (sh_cmt_read_cmcnt(ch)) { |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 325 | dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n", |
| 326 | ch->index); |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 327 | ret = -ETIMEDOUT; |
| 328 | goto err1; |
| 329 | } |
| 330 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 331 | /* enable channel */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 332 | sh_cmt_start_stop_ch(ch, 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 333 | return 0; |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 334 | err1: |
| 335 | /* stop clock */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 336 | clk_disable(ch->cmt->clk); |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 337 | |
| 338 | err0: |
| 339 | return ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 342 | static void sh_cmt_disable(struct sh_cmt_channel *ch) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 343 | { |
| 344 | /* disable channel */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 345 | sh_cmt_start_stop_ch(ch, 0); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 346 | |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 347 | /* disable interrupts in CMT block */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 348 | sh_cmt_write_cmcsr(ch, 0); |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 349 | |
Paul Mundt | 9436b4a | 2011-05-31 15:26:42 +0900 | [diff] [blame] | 350 | /* stop clock */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 351 | clk_disable(ch->cmt->clk); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 352 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 353 | dev_pm_syscore_device(&ch->cmt->pdev->dev, false); |
| 354 | pm_runtime_put(&ch->cmt->pdev->dev); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* private flags */ |
| 358 | #define FLAG_CLOCKEVENT (1 << 0) |
| 359 | #define FLAG_CLOCKSOURCE (1 << 1) |
| 360 | #define FLAG_REPROGRAM (1 << 2) |
| 361 | #define FLAG_SKIPEVENT (1 << 3) |
| 362 | #define FLAG_IRQCONTEXT (1 << 4) |
| 363 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 364 | static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch, |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 365 | int absolute) |
| 366 | { |
| 367 | unsigned long new_match; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 368 | unsigned long value = ch->next_match_value; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 369 | unsigned long delay = 0; |
| 370 | unsigned long now = 0; |
| 371 | int has_wrapped; |
| 372 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 373 | now = sh_cmt_get_counter(ch, &has_wrapped); |
| 374 | ch->flags |= FLAG_REPROGRAM; /* force reprogram */ |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 375 | |
| 376 | if (has_wrapped) { |
| 377 | /* we're competing with the interrupt handler. |
| 378 | * -> let the interrupt handler reprogram the timer. |
| 379 | * -> interrupt number two handles the event. |
| 380 | */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 381 | ch->flags |= FLAG_SKIPEVENT; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | |
| 385 | if (absolute) |
| 386 | now = 0; |
| 387 | |
| 388 | do { |
| 389 | /* reprogram the timer hardware, |
| 390 | * but don't save the new match value yet. |
| 391 | */ |
| 392 | new_match = now + value + delay; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 393 | if (new_match > ch->max_match_value) |
| 394 | new_match = ch->max_match_value; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 395 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 396 | sh_cmt_write_cmcor(ch, new_match); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 397 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 398 | now = sh_cmt_get_counter(ch, &has_wrapped); |
| 399 | if (has_wrapped && (new_match > ch->match_value)) { |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 400 | /* we are changing to a greater match value, |
| 401 | * so this wrap must be caused by the counter |
| 402 | * matching the old value. |
| 403 | * -> first interrupt reprograms the timer. |
| 404 | * -> interrupt number two handles the event. |
| 405 | */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 406 | ch->flags |= FLAG_SKIPEVENT; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | |
| 410 | if (has_wrapped) { |
| 411 | /* we are changing to a smaller match value, |
| 412 | * so the wrap must be caused by the counter |
| 413 | * matching the new value. |
| 414 | * -> save programmed match value. |
| 415 | * -> let isr handle the event. |
| 416 | */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 417 | ch->match_value = new_match; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | |
| 421 | /* be safe: verify hardware settings */ |
| 422 | if (now < new_match) { |
| 423 | /* timer value is below match value, all good. |
| 424 | * this makes sure we won't miss any match events. |
| 425 | * -> save programmed match value. |
| 426 | * -> let isr handle the event. |
| 427 | */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 428 | ch->match_value = new_match; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | |
| 432 | /* the counter has reached a value greater |
| 433 | * than our new match value. and since the |
| 434 | * has_wrapped flag isn't set we must have |
| 435 | * programmed a too close event. |
| 436 | * -> increase delay and retry. |
| 437 | */ |
| 438 | if (delay) |
| 439 | delay <<= 1; |
| 440 | else |
| 441 | delay = 1; |
| 442 | |
| 443 | if (!delay) |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 444 | dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n", |
| 445 | ch->index); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 446 | |
| 447 | } while (delay); |
| 448 | } |
| 449 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 450 | static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta) |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 451 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 452 | if (delta > ch->max_match_value) |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 453 | dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n", |
| 454 | ch->index); |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 455 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 456 | ch->next_match_value = delta; |
| 457 | sh_cmt_clock_event_program_verify(ch, 0); |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 460 | static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 461 | { |
| 462 | unsigned long flags; |
| 463 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 464 | raw_spin_lock_irqsave(&ch->lock, flags); |
| 465 | __sh_cmt_set_next(ch, delta); |
| 466 | raw_spin_unlock_irqrestore(&ch->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id) |
| 470 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 471 | struct sh_cmt_channel *ch = dev_id; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 472 | |
| 473 | /* clear flags */ |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 474 | sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) & |
| 475 | ch->cmt->info->clear_bits); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 476 | |
| 477 | /* update clock source counter to begin with if enabled |
| 478 | * the wrap flag should be cleared by the timer specific |
| 479 | * isr before we end up here. |
| 480 | */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 481 | if (ch->flags & FLAG_CLOCKSOURCE) |
| 482 | ch->total_cycles += ch->match_value + 1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 483 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 484 | if (!(ch->flags & FLAG_REPROGRAM)) |
| 485 | ch->next_match_value = ch->max_match_value; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 486 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 487 | ch->flags |= FLAG_IRQCONTEXT; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 488 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 489 | if (ch->flags & FLAG_CLOCKEVENT) { |
| 490 | if (!(ch->flags & FLAG_SKIPEVENT)) { |
| 491 | if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT) { |
| 492 | ch->next_match_value = ch->max_match_value; |
| 493 | ch->flags |= FLAG_REPROGRAM; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 496 | ch->ced.event_handler(&ch->ced); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 500 | ch->flags &= ~FLAG_SKIPEVENT; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 501 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 502 | if (ch->flags & FLAG_REPROGRAM) { |
| 503 | ch->flags &= ~FLAG_REPROGRAM; |
| 504 | sh_cmt_clock_event_program_verify(ch, 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 505 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 506 | if (ch->flags & FLAG_CLOCKEVENT) |
| 507 | if ((ch->ced.mode == CLOCK_EVT_MODE_SHUTDOWN) |
| 508 | || (ch->match_value == ch->next_match_value)) |
| 509 | ch->flags &= ~FLAG_REPROGRAM; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 512 | ch->flags &= ~FLAG_IRQCONTEXT; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 513 | |
| 514 | return IRQ_HANDLED; |
| 515 | } |
| 516 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 517 | static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 518 | { |
| 519 | int ret = 0; |
| 520 | unsigned long flags; |
| 521 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 522 | raw_spin_lock_irqsave(&ch->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 523 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 524 | if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) |
| 525 | ret = sh_cmt_enable(ch, &ch->rate); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 526 | |
| 527 | if (ret) |
| 528 | goto out; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 529 | ch->flags |= flag; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 530 | |
| 531 | /* setup timeout if no clockevent */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 532 | if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT))) |
| 533 | __sh_cmt_set_next(ch, ch->max_match_value); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 534 | out: |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 535 | raw_spin_unlock_irqrestore(&ch->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 536 | |
| 537 | return ret; |
| 538 | } |
| 539 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 540 | static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 541 | { |
| 542 | unsigned long flags; |
| 543 | unsigned long f; |
| 544 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 545 | raw_spin_lock_irqsave(&ch->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 546 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 547 | f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE); |
| 548 | ch->flags &= ~flag; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 549 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 550 | if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) |
| 551 | sh_cmt_disable(ch); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 552 | |
| 553 | /* adjust the timeout to maximum if only clocksource left */ |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 554 | if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE)) |
| 555 | __sh_cmt_set_next(ch, ch->max_match_value); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 556 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 557 | raw_spin_unlock_irqrestore(&ch->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 560 | static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs) |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 561 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 562 | return container_of(cs, struct sh_cmt_channel, cs); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static cycle_t sh_cmt_clocksource_read(struct clocksource *cs) |
| 566 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 567 | struct sh_cmt_channel *ch = cs_to_sh_cmt(cs); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 568 | unsigned long flags, raw; |
| 569 | unsigned long value; |
| 570 | int has_wrapped; |
| 571 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 572 | raw_spin_lock_irqsave(&ch->lock, flags); |
| 573 | value = ch->total_cycles; |
| 574 | raw = sh_cmt_get_counter(ch, &has_wrapped); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 575 | |
| 576 | if (unlikely(has_wrapped)) |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 577 | raw += ch->match_value + 1; |
| 578 | raw_spin_unlock_irqrestore(&ch->lock, flags); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 579 | |
| 580 | return value + raw; |
| 581 | } |
| 582 | |
| 583 | static int sh_cmt_clocksource_enable(struct clocksource *cs) |
| 584 | { |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 585 | int ret; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 586 | struct sh_cmt_channel *ch = cs_to_sh_cmt(cs); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 587 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 588 | WARN_ON(ch->cs_enabled); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 589 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 590 | ch->total_cycles = 0; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 591 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 592 | ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 593 | if (!ret) { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 594 | __clocksource_updatefreq_hz(cs, ch->rate); |
| 595 | ch->cs_enabled = true; |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 596 | } |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 597 | return ret; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void sh_cmt_clocksource_disable(struct clocksource *cs) |
| 601 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 602 | struct sh_cmt_channel *ch = cs_to_sh_cmt(cs); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 603 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 604 | WARN_ON(!ch->cs_enabled); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 605 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 606 | sh_cmt_stop(ch, FLAG_CLOCKSOURCE); |
| 607 | ch->cs_enabled = false; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 610 | static void sh_cmt_clocksource_suspend(struct clocksource *cs) |
| 611 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 612 | struct sh_cmt_channel *ch = cs_to_sh_cmt(cs); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 613 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 614 | sh_cmt_stop(ch, FLAG_CLOCKSOURCE); |
| 615 | pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 616 | } |
| 617 | |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 618 | static void sh_cmt_clocksource_resume(struct clocksource *cs) |
| 619 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 620 | struct sh_cmt_channel *ch = cs_to_sh_cmt(cs); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 621 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 622 | pm_genpd_syscore_poweron(&ch->cmt->pdev->dev); |
| 623 | sh_cmt_start(ch, FLAG_CLOCKSOURCE); |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 626 | static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch, |
Laurent Pinchart | 1d053e1 | 2014-02-17 16:04:16 +0100 | [diff] [blame] | 627 | const char *name, unsigned long rating) |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 628 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 629 | struct clocksource *cs = &ch->cs; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 630 | |
| 631 | cs->name = name; |
| 632 | cs->rating = rating; |
| 633 | cs->read = sh_cmt_clocksource_read; |
| 634 | cs->enable = sh_cmt_clocksource_enable; |
| 635 | cs->disable = sh_cmt_clocksource_disable; |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 636 | cs->suspend = sh_cmt_clocksource_suspend; |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 637 | cs->resume = sh_cmt_clocksource_resume; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 638 | cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8); |
| 639 | cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; |
Paul Mundt | f4d7c35 | 2010-06-02 17:10:44 +0900 | [diff] [blame] | 640 | |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 641 | dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n", |
| 642 | ch->index); |
Paul Mundt | f4d7c35 | 2010-06-02 17:10:44 +0900 | [diff] [blame] | 643 | |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 644 | /* Register with dummy 1 Hz value, gets updated in ->enable() */ |
| 645 | clocksource_register_hz(cs, 1); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 649 | static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 650 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 651 | return container_of(ced, struct sh_cmt_channel, ced); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 654 | static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 655 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 656 | struct clock_event_device *ced = &ch->ced; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 657 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 658 | sh_cmt_start(ch, FLAG_CLOCKEVENT); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 659 | |
| 660 | /* TODO: calculate good shift from rate and counter bit width */ |
| 661 | |
| 662 | ced->shift = 32; |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 663 | ced->mult = div_sc(ch->rate, NSEC_PER_SEC, ced->shift); |
| 664 | ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 665 | ced->min_delta_ns = clockevent_delta2ns(0x1f, ced); |
| 666 | |
| 667 | if (periodic) |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 668 | sh_cmt_set_next(ch, ((ch->rate + HZ/2) / HZ) - 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 669 | else |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 670 | sh_cmt_set_next(ch, ch->max_match_value); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static void sh_cmt_clock_event_mode(enum clock_event_mode mode, |
| 674 | struct clock_event_device *ced) |
| 675 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 676 | struct sh_cmt_channel *ch = ced_to_sh_cmt(ced); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 677 | |
| 678 | /* deal with old setting first */ |
| 679 | switch (ced->mode) { |
| 680 | case CLOCK_EVT_MODE_PERIODIC: |
| 681 | case CLOCK_EVT_MODE_ONESHOT: |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 682 | sh_cmt_stop(ch, FLAG_CLOCKEVENT); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 683 | break; |
| 684 | default: |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | switch (mode) { |
| 689 | case CLOCK_EVT_MODE_PERIODIC: |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 690 | dev_info(&ch->cmt->pdev->dev, |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 691 | "ch%u: used for periodic clock events\n", ch->index); |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 692 | sh_cmt_clock_event_start(ch, 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 693 | break; |
| 694 | case CLOCK_EVT_MODE_ONESHOT: |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 695 | dev_info(&ch->cmt->pdev->dev, |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 696 | "ch%u: used for oneshot clock events\n", ch->index); |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 697 | sh_cmt_clock_event_start(ch, 0); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 698 | break; |
| 699 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 700 | case CLOCK_EVT_MODE_UNUSED: |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 701 | sh_cmt_stop(ch, FLAG_CLOCKEVENT); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 702 | break; |
| 703 | default: |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | static int sh_cmt_clock_event_next(unsigned long delta, |
| 709 | struct clock_event_device *ced) |
| 710 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 711 | struct sh_cmt_channel *ch = ced_to_sh_cmt(ced); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 712 | |
| 713 | BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT); |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 714 | if (likely(ch->flags & FLAG_IRQCONTEXT)) |
| 715 | ch->next_match_value = delta - 1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 716 | else |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 717 | sh_cmt_set_next(ch, delta - 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 722 | static void sh_cmt_clock_event_suspend(struct clock_event_device *ced) |
| 723 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 724 | struct sh_cmt_channel *ch = ced_to_sh_cmt(ced); |
Laurent Pinchart | 57dee99 | 2013-12-14 15:07:32 +0900 | [diff] [blame] | 725 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 726 | pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev); |
| 727 | clk_unprepare(ch->cmt->clk); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | static void sh_cmt_clock_event_resume(struct clock_event_device *ced) |
| 731 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 732 | struct sh_cmt_channel *ch = ced_to_sh_cmt(ced); |
Laurent Pinchart | 57dee99 | 2013-12-14 15:07:32 +0900 | [diff] [blame] | 733 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 734 | clk_prepare(ch->cmt->clk); |
| 735 | pm_genpd_syscore_poweron(&ch->cmt->pdev->dev); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 738 | static void sh_cmt_register_clockevent(struct sh_cmt_channel *ch, |
Laurent Pinchart | 1d053e1 | 2014-02-17 16:04:16 +0100 | [diff] [blame] | 739 | const char *name, unsigned long rating) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 740 | { |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 741 | struct clock_event_device *ced = &ch->ced; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 742 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 743 | ced->name = name; |
| 744 | ced->features = CLOCK_EVT_FEAT_PERIODIC; |
| 745 | ced->features |= CLOCK_EVT_FEAT_ONESHOT; |
| 746 | ced->rating = rating; |
| 747 | ced->cpumask = cpumask_of(0); |
| 748 | ced->set_next_event = sh_cmt_clock_event_next; |
| 749 | ced->set_mode = sh_cmt_clock_event_mode; |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 750 | ced->suspend = sh_cmt_clock_event_suspend; |
| 751 | ced->resume = sh_cmt_clock_event_resume; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 752 | |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 753 | dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n", |
| 754 | ch->index); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 755 | clockevents_register_device(ced); |
| 756 | } |
| 757 | |
Laurent Pinchart | 1d053e1 | 2014-02-17 16:04:16 +0100 | [diff] [blame] | 758 | static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name, |
Paul Mundt | d1fcc0a | 2009-05-03 18:05:42 +0900 | [diff] [blame] | 759 | unsigned long clockevent_rating, |
| 760 | unsigned long clocksource_rating) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 761 | { |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 762 | if (clockevent_rating) |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 763 | sh_cmt_register_clockevent(ch, name, clockevent_rating); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 764 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 765 | if (clocksource_rating) |
Laurent Pinchart | 7269f93 | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 766 | sh_cmt_register_clocksource(ch, name, clocksource_rating); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 767 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 768 | return 0; |
| 769 | } |
| 770 | |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 771 | static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index, |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 772 | struct sh_cmt_device *cmt) |
| 773 | { |
| 774 | struct sh_timer_config *cfg = cmt->pdev->dev.platform_data; |
| 775 | int irq; |
| 776 | int ret; |
| 777 | |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 778 | ch->cmt = cmt; |
Laurent Pinchart | c924d2d | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 779 | ch->base = cmt->mapbase_ch; |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 780 | ch->index = index; |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 781 | |
| 782 | irq = platform_get_irq(cmt->pdev, 0); |
| 783 | if (irq < 0) { |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 784 | dev_err(&cmt->pdev->dev, "ch%u: failed to get irq\n", |
| 785 | ch->index); |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 786 | return irq; |
| 787 | } |
| 788 | |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 789 | if (cmt->info->width == (sizeof(ch->max_match_value) * 8)) |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 790 | ch->max_match_value = ~0; |
| 791 | else |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 792 | ch->max_match_value = (1 << cmt->info->width) - 1; |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 793 | |
| 794 | ch->match_value = ch->max_match_value; |
| 795 | raw_spin_lock_init(&ch->lock); |
| 796 | |
Laurent Pinchart | 1d053e1 | 2014-02-17 16:04:16 +0100 | [diff] [blame] | 797 | ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev), |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 798 | cfg->clockevent_rating, |
| 799 | cfg->clocksource_rating); |
| 800 | if (ret) { |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 801 | dev_err(&cmt->pdev->dev, "ch%u: registration failed\n", |
| 802 | ch->index); |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 803 | return ret; |
| 804 | } |
| 805 | ch->cs_enabled = false; |
| 806 | |
| 807 | ret = request_irq(irq, sh_cmt_interrupt, |
| 808 | IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING, |
| 809 | dev_name(&cmt->pdev->dev), ch); |
| 810 | if (ret) { |
Laurent Pinchart | 740a951 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 811 | dev_err(&cmt->pdev->dev, "ch%u: failed to request irq %d\n", |
| 812 | ch->index, irq); |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 819 | static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 820 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 821 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 822 | struct resource *res, *res2; |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 823 | int ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 824 | ret = -ENXIO; |
| 825 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 826 | cmt->pdev = pdev; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 827 | |
| 828 | if (!cfg) { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 829 | dev_err(&cmt->pdev->dev, "missing platform data\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 830 | goto err0; |
| 831 | } |
| 832 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 833 | res = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 834 | if (!res) { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 835 | dev_err(&cmt->pdev->dev, "failed to get I/O memory\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 836 | goto err0; |
| 837 | } |
| 838 | |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 839 | /* optional resource for the shared timer start/stop register */ |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 840 | res2 = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 1); |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 841 | |
Laurent Pinchart | 36f1ac9 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 842 | /* map memory, let mapbase_ch point to our channel */ |
| 843 | cmt->mapbase_ch = ioremap_nocache(res->start, resource_size(res)); |
| 844 | if (cmt->mapbase_ch == NULL) { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 845 | dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 846 | goto err0; |
| 847 | } |
| 848 | |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 849 | /* map second resource for CMSTR */ |
Laurent Pinchart | 36f1ac9 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 850 | cmt->mapbase = ioremap_nocache(res2 ? res2->start : |
| 851 | res->start - cfg->channel_offset, |
| 852 | res2 ? resource_size(res2) : 2); |
| 853 | if (cmt->mapbase == NULL) { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 854 | dev_err(&cmt->pdev->dev, "failed to remap I/O second memory\n"); |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 855 | goto err1; |
| 856 | } |
| 857 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 858 | /* get hold of clock */ |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 859 | cmt->clk = clk_get(&cmt->pdev->dev, "cmt_fck"); |
| 860 | if (IS_ERR(cmt->clk)) { |
| 861 | dev_err(&cmt->pdev->dev, "cannot get clock\n"); |
| 862 | ret = PTR_ERR(cmt->clk); |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 863 | goto err2; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 866 | ret = clk_prepare(cmt->clk); |
Laurent Pinchart | 57dee99 | 2013-12-14 15:07:32 +0900 | [diff] [blame] | 867 | if (ret < 0) |
| 868 | goto err3; |
| 869 | |
Laurent Pinchart | 2cda3ac | 2014-02-11 23:46:48 +0100 | [diff] [blame^] | 870 | /* identify the model based on the resources */ |
| 871 | if (resource_size(res) == 6) |
| 872 | cmt->info = &sh_cmt_info[SH_CMT_16BIT]; |
| 873 | else if (res2 && (resource_size(res2) == 4)) |
| 874 | cmt->info = &sh_cmt_info[SH_CMT_48BIT_GEN2]; |
| 875 | else |
| 876 | cmt->info = &sh_cmt_info[SH_CMT_32BIT]; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 877 | |
Laurent Pinchart | f5ec9b1 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 878 | cmt->channels = kzalloc(sizeof(*cmt->channels), GFP_KERNEL); |
| 879 | if (cmt->channels == NULL) { |
| 880 | ret = -ENOMEM; |
| 881 | goto err4; |
| 882 | } |
| 883 | |
| 884 | cmt->num_channels = 1; |
| 885 | |
| 886 | ret = sh_cmt_setup_channel(&cmt->channels[0], cfg->timer_bit, cmt); |
Laurent Pinchart | b882e7b | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 887 | if (ret < 0) |
Laurent Pinchart | 57dee99 | 2013-12-14 15:07:32 +0900 | [diff] [blame] | 888 | goto err4; |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 889 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 890 | platform_set_drvdata(pdev, cmt); |
Magnus Damm | adccc69 | 2012-12-14 14:53:51 +0900 | [diff] [blame] | 891 | |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 892 | return 0; |
Laurent Pinchart | 57dee99 | 2013-12-14 15:07:32 +0900 | [diff] [blame] | 893 | err4: |
Laurent Pinchart | f5ec9b1 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 894 | kfree(cmt->channels); |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 895 | clk_unprepare(cmt->clk); |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 896 | err3: |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 897 | clk_put(cmt->clk); |
Magnus Damm | 8874c5e | 2013-06-17 15:40:52 +0900 | [diff] [blame] | 898 | err2: |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 899 | iounmap(cmt->mapbase); |
Laurent Pinchart | 36f1ac9 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 900 | err1: |
| 901 | iounmap(cmt->mapbase_ch); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 902 | err0: |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 903 | return ret; |
| 904 | } |
| 905 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 906 | static int sh_cmt_probe(struct platform_device *pdev) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 907 | { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 908 | struct sh_cmt_device *cmt = platform_get_drvdata(pdev); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 909 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 910 | int ret; |
| 911 | |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 912 | if (!is_early_platform_device(pdev)) { |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 913 | pm_runtime_set_active(&pdev->dev); |
| 914 | pm_runtime_enable(&pdev->dev); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 915 | } |
Rafael J. Wysocki | 615a445 | 2012-03-13 22:40:06 +0100 | [diff] [blame] | 916 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 917 | if (cmt) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 918 | dev_info(&pdev->dev, "kept as earlytimer\n"); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 919 | goto out; |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Laurent Pinchart | b262bc7 | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 922 | cmt = kzalloc(sizeof(*cmt), GFP_KERNEL); |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 923 | if (cmt == NULL) { |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 924 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 925 | return -ENOMEM; |
| 926 | } |
| 927 | |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 928 | ret = sh_cmt_setup(cmt, pdev); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 929 | if (ret) { |
Laurent Pinchart | 2653caf | 2014-01-27 22:04:17 +0100 | [diff] [blame] | 930 | kfree(cmt); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 931 | pm_runtime_idle(&pdev->dev); |
| 932 | return ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 933 | } |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 934 | if (is_early_platform_device(pdev)) |
| 935 | return 0; |
| 936 | |
| 937 | out: |
| 938 | if (cfg->clockevent_rating || cfg->clocksource_rating) |
| 939 | pm_runtime_irq_safe(&pdev->dev); |
| 940 | else |
| 941 | pm_runtime_idle(&pdev->dev); |
| 942 | |
| 943 | return 0; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 946 | static int sh_cmt_remove(struct platform_device *pdev) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 947 | { |
| 948 | return -EBUSY; /* cannot unregister clockevent and clocksource */ |
| 949 | } |
| 950 | |
| 951 | static struct platform_driver sh_cmt_device_driver = { |
| 952 | .probe = sh_cmt_probe, |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 953 | .remove = sh_cmt_remove, |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 954 | .driver = { |
| 955 | .name = "sh_cmt", |
| 956 | } |
| 957 | }; |
| 958 | |
| 959 | static int __init sh_cmt_init(void) |
| 960 | { |
| 961 | return platform_driver_register(&sh_cmt_device_driver); |
| 962 | } |
| 963 | |
| 964 | static void __exit sh_cmt_exit(void) |
| 965 | { |
| 966 | platform_driver_unregister(&sh_cmt_device_driver); |
| 967 | } |
| 968 | |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 969 | early_platform_init("earlytimer", &sh_cmt_device_driver); |
Simon Horman | e903a03 | 2013-03-05 15:40:42 +0900 | [diff] [blame] | 970 | subsys_initcall(sh_cmt_init); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 971 | module_exit(sh_cmt_exit); |
| 972 | |
| 973 | MODULE_AUTHOR("Magnus Damm"); |
| 974 | MODULE_DESCRIPTION("SuperH CMT Timer Driver"); |
| 975 | MODULE_LICENSE("GPL v2"); |