Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
91e8d0c
Documentation
arch
block
certs
crypto
drivers
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cdrom
char
clk
clocksource
Kconfig
Makefile
acpi_pm.c
arm_arch_timer.c
arm_global_timer.c
armv7m_systick.c
asm9260_timer.c
bcm2835_timer.c
bcm_kona_timer.c
cadence_ttc_timer.c
clksrc-dbx500-prcmu.c
clksrc-probe.c
clksrc_st_lpc.c
clps711x-timer.c
cs5535-clockevt.c
dummy_timer.c
dw_apb_timer.c
dw_apb_timer_of.c
em_sti.c
exynos_mct.c
fsl_ftm_timer.c
h8300_timer16.c
h8300_timer8.c
h8300_tpu.c
i8253.c
meson6_timer.c
metag_generic.c
mips-gic-timer.c
mmio.c
moxart_timer.c
mps2-timer.c
mtk_timer.c
mxs_timer.c
nomadik-mtu.c
numachip.c
pxa_timer.c
qcom-timer.c
rockchip_timer.c
samsung_pwm_timer.c
scx200_hrt.c
sh_cmt.c
sh_mtu2.c
sh_tmu.c
sun4i_timer.c
tango_xtal.c
tcb_clksrc.c
tegra20_timer.c
time-armada-370-xp.c
time-efm32.c
time-lpc32xx.c
time-orion.c
time-pistachio.c
timer-atlas7.c
timer-atmel-pit.c
timer-atmel-st.c
timer-digicolor.c
timer-imx-gpt.c
timer-integrator-ap.c
timer-keystone.c
timer-prima2.c
timer-sp.h
timer-sp804.c
timer-stm32.c
timer-sun5i.c
timer-ti-32k.c
timer-u300.c
versatile.c
vf_pit_timer.c
vt8500_timer.c
zevio-timer.c
connector
cpufreq
cpuidle
crypto
dca
devfreq
dio
dma-buf
dma
edac
eisa
extcon
firewire
firmware
fmc
fpga
gpio
gpu
hid
hsi
hv
hwmon
hwspinlock
hwtracing
i2c
ide
idle
iio
infiniband
input
iommu
ipack
irqchip
isdn
leds
lguest
lightnvm
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
mmc
mtd
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
oprofile
parisc
parport
pci
pcmcia
perf
phy
pinctrl
platform
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
s390
sbus
scsi
sfi
sh
sn
soc
spi
spmi
ssb
staging
target
tc
thermal
thunderbolt
tty
uio
usb
uwb
vfio
vhost
video
virt
virtio
vlynq
vme
w1
watchdog
xen
zorro
Kconfig
Makefile
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.get_maintainer.ignore
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
drivers
/
clocksource
/
mtk_timer.c
Blame
Blame
Latest commit
History
History
266 lines (215 loc) · 7.05 KB
Breadcrumbs
linux
/
drivers
/
clocksource
/
mtk_timer.c
Top
File metadata and controls
Code
Blame
266 lines (215 loc) · 7.05 KB
Raw
/* * Mediatek SoCs General-Purpose Timer handling. * * Copyright (C) 2014 Matthias Brugger * * Matthias Brugger <matthias.bgg@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/clk.h> #include <linux/clockchips.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/irqreturn.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/of_irq.h> #include <linux/sched_clock.h> #include <linux/slab.h> #define GPT_IRQ_EN_REG 0x00 #define GPT_IRQ_ENABLE(val) BIT((val) - 1) #define GPT_IRQ_ACK_REG 0x08 #define GPT_IRQ_ACK(val) BIT((val) - 1) #define TIMER_CTRL_REG(val) (0x10 * (val)) #define TIMER_CTRL_OP(val) (((val) & 0x3) << 4) #define TIMER_CTRL_OP_ONESHOT (0) #define TIMER_CTRL_OP_REPEAT (1) #define TIMER_CTRL_OP_FREERUN (3) #define TIMER_CTRL_CLEAR (2) #define TIMER_CTRL_ENABLE (1) #define TIMER_CTRL_DISABLE (0) #define TIMER_CLK_REG(val) (0x04 + (0x10 * (val))) #define TIMER_CLK_SRC(val) (((val) & 0x1) << 4) #define TIMER_CLK_SRC_SYS13M (0) #define TIMER_CLK_SRC_RTC32K (1) #define TIMER_CLK_DIV1 (0x0) #define TIMER_CLK_DIV2 (0x1) #define TIMER_CNT_REG(val) (0x08 + (0x10 * (val))) #define TIMER_CMP_REG(val) (0x0C + (0x10 * (val))) #define GPT_CLK_EVT 1 #define GPT_CLK_SRC 2 struct mtk_clock_event_device { void __iomem *gpt_base; u32 ticks_per_jiffy; struct clock_event_device dev; }; static void __iomem *gpt_sched_reg __read_mostly; static u64 notrace mtk_read_sched_clock(void) { return readl_relaxed(gpt_sched_reg); } static inline struct mtk_clock_event_device *to_mtk_clk( struct clock_event_device *c) { return container_of(c, struct mtk_clock_event_device, dev); } static void mtk_clkevt_time_stop(struct mtk_clock_event_device *evt, u8 timer) { u32 val; val = readl(evt->gpt_base + TIMER_CTRL_REG(timer)); writel(val & ~TIMER_CTRL_ENABLE, evt->gpt_base + TIMER_CTRL_REG(timer)); } static void mtk_clkevt_time_setup(struct mtk_clock_event_device *evt, unsigned long delay, u8 timer) { writel(delay, evt->gpt_base + TIMER_CMP_REG(timer)); } static void mtk_clkevt_time_start(struct mtk_clock_event_device *evt, bool periodic, u8 timer) { u32 val; /* Acknowledge interrupt */ writel(GPT_IRQ_ACK(timer), evt->gpt_base + GPT_IRQ_ACK_REG); val = readl(evt->gpt_base + TIMER_CTRL_REG(timer)); /* Clear 2 bit timer operation mode field */ val &= ~TIMER_CTRL_OP(0x3); if (periodic) val |= TIMER_CTRL_OP(TIMER_CTRL_OP_REPEAT); else val |= TIMER_CTRL_OP(TIMER_CTRL_OP_ONESHOT); writel(val | TIMER_CTRL_ENABLE | TIMER_CTRL_CLEAR, evt->gpt_base + TIMER_CTRL_REG(timer)); } static int mtk_clkevt_shutdown(struct clock_event_device *clk) { mtk_clkevt_time_stop(to_mtk_clk(clk), GPT_CLK_EVT); return 0; } static int mtk_clkevt_set_periodic(struct clock_event_device *clk) { struct mtk_clock_event_device *evt = to_mtk_clk(clk); mtk_clkevt_time_stop(evt, GPT_CLK_EVT); mtk_clkevt_time_setup(evt, evt->ticks_per_jiffy, GPT_CLK_EVT); mtk_clkevt_time_start(evt, true, GPT_CLK_EVT); return 0; } static int mtk_clkevt_next_event(unsigned long event, struct clock_event_device *clk) { struct mtk_clock_event_device *evt = to_mtk_clk(clk); mtk_clkevt_time_stop(evt, GPT_CLK_EVT); mtk_clkevt_time_setup(evt, event, GPT_CLK_EVT); mtk_clkevt_time_start(evt, false, GPT_CLK_EVT); return 0; } static irqreturn_t mtk_timer_interrupt(int irq, void *dev_id) { struct mtk_clock_event_device *evt = dev_id; /* Acknowledge timer0 irq */ writel(GPT_IRQ_ACK(GPT_CLK_EVT), evt->gpt_base + GPT_IRQ_ACK_REG); evt->dev.event_handler(&evt->dev); return IRQ_HANDLED; } static void __init mtk_timer_setup(struct mtk_clock_event_device *evt, u8 timer, u8 option) { writel(TIMER_CTRL_CLEAR | TIMER_CTRL_DISABLE, evt->gpt_base + TIMER_CTRL_REG(timer)); writel(TIMER_CLK_SRC(TIMER_CLK_SRC_SYS13M) | TIMER_CLK_DIV1, evt->gpt_base + TIMER_CLK_REG(timer)); writel(0x0, evt->gpt_base + TIMER_CMP_REG(timer)); writel(TIMER_CTRL_OP(option) | TIMER_CTRL_ENABLE, evt->gpt_base + TIMER_CTRL_REG(timer)); } static void mtk_timer_enable_irq(struct mtk_clock_event_device *evt, u8 timer) { u32 val; /* Disable all interrupts */ writel(0x0, evt->gpt_base + GPT_IRQ_EN_REG); /* Acknowledge all spurious pending interrupts */ writel(0x3f, evt->gpt_base + GPT_IRQ_ACK_REG); val = readl(evt->gpt_base + GPT_IRQ_EN_REG); writel(val | GPT_IRQ_ENABLE(timer), evt->gpt_base + GPT_IRQ_EN_REG); } static void __init mtk_timer_init(struct device_node *node) { struct mtk_clock_event_device *evt; struct resource res; unsigned long rate = 0; struct clk *clk; evt = kzalloc(sizeof(*evt), GFP_KERNEL); if (!evt) return; evt->dev.name = "mtk_tick"; evt->dev.rating = 300; evt->dev.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; evt->dev.set_state_shutdown = mtk_clkevt_shutdown; evt->dev.set_state_periodic = mtk_clkevt_set_periodic; evt->dev.set_state_oneshot = mtk_clkevt_shutdown; evt->dev.tick_resume = mtk_clkevt_shutdown; evt->dev.set_next_event = mtk_clkevt_next_event; evt->dev.cpumask = cpu_possible_mask; evt->gpt_base = of_io_request_and_map(node, 0, "mtk-timer"); if (IS_ERR(evt->gpt_base)) { pr_err("Can't get resource\n"); goto err_kzalloc; } evt->dev.irq = irq_of_parse_and_map(node, 0); if (evt->dev.irq <= 0) { pr_err("Can't parse IRQ\n"); goto err_mem; } clk = of_clk_get(node, 0); if (IS_ERR(clk)) { pr_err("Can't get timer clock\n"); goto err_irq; } if (clk_prepare_enable(clk)) { pr_err("Can't prepare clock\n"); goto err_clk_put; } rate = clk_get_rate(clk); if (request_irq(evt->dev.irq, mtk_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL, "mtk_timer", evt)) { pr_err("failed to setup irq %d\n", evt->dev.irq); goto err_clk_disable; } evt->ticks_per_jiffy = DIV_ROUND_UP(rate, HZ); /* Configure clock source */ mtk_timer_setup(evt, GPT_CLK_SRC, TIMER_CTRL_OP_FREERUN); clocksource_mmio_init(evt->gpt_base + TIMER_CNT_REG(GPT_CLK_SRC), node->name, rate, 300, 32, clocksource_mmio_readl_up); gpt_sched_reg = evt->gpt_base + TIMER_CNT_REG(GPT_CLK_SRC); sched_clock_register(mtk_read_sched_clock, 32, rate); /* Configure clock event */ mtk_timer_setup(evt, GPT_CLK_EVT, TIMER_CTRL_OP_REPEAT); clockevents_config_and_register(&evt->dev, rate, 0x3, 0xffffffff); mtk_timer_enable_irq(evt, GPT_CLK_EVT); return; err_clk_disable: clk_disable_unprepare(clk); err_clk_put: clk_put(clk); err_irq: irq_dispose_mapping(evt->dev.irq); err_mem: iounmap(evt->gpt_base); of_address_to_resource(node, 0, &res); release_mem_region(res.start, resource_size(&res)); err_kzalloc: kfree(evt); } CLOCKSOURCE_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_timer_init);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
You can’t perform that action at this time.