xref: /linux-6.15/arch/powerpc/sysdev/mpic_timer.c (revision 81d7cac4)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
236ca09beS[email protected] /*
336ca09beS[email protected]  * MPIC timer driver
436ca09beS[email protected]  *
536ca09beS[email protected]  * Copyright 2013 Freescale Semiconductor, Inc.
636ca09beS[email protected]  * Author: Dongsheng Wang <[email protected]>
736ca09beS[email protected]  *	   Li Yang <[email protected]>
836ca09beS[email protected]  */
936ca09beS[email protected] 
1036ca09beS[email protected] #include <linux/kernel.h>
1136ca09beS[email protected] #include <linux/init.h>
1236ca09beS[email protected] #include <linux/module.h>
1336ca09beS[email protected] #include <linux/errno.h>
1436ca09beS[email protected] #include <linux/mm.h>
1536ca09beS[email protected] #include <linux/interrupt.h>
1636ca09beS[email protected] #include <linux/slab.h>
1736ca09beS[email protected] #include <linux/of.h>
1826a2056eSRob Herring #include <linux/of_address.h>
1926a2056eSRob Herring #include <linux/of_irq.h>
2036ca09beS[email protected] #include <linux/syscore_ops.h>
2136ca09beS[email protected] #include <sysdev/fsl_soc.h>
2236ca09beS[email protected] #include <asm/io.h>
2336ca09beS[email protected] 
2436ca09beS[email protected] #include <asm/mpic_timer.h>
2536ca09beS[email protected] 
2636ca09beS[email protected] #define FSL_GLOBAL_TIMER		0x1
2736ca09beS[email protected] 
2836ca09beS[email protected] /* Clock Ratio
2936ca09beS[email protected]  * Divide by 64 0x00000300
3036ca09beS[email protected]  * Divide by 32 0x00000200
3136ca09beS[email protected]  * Divide by 16 0x00000100
3236ca09beS[email protected]  * Divide by  8 0x00000000 (Hardware default div)
3336ca09beS[email protected]  */
3436ca09beS[email protected] #define MPIC_TIMER_TCR_CLKDIV		0x00000300
3536ca09beS[email protected] 
3636ca09beS[email protected] #define MPIC_TIMER_TCR_ROVR_OFFSET	24
3736ca09beS[email protected] 
3836ca09beS[email protected] #define TIMER_STOP			0x80000000
390fd79588SWang Dongsheng #define GTCCR_TOG			0x80000000
4036ca09beS[email protected] #define TIMERS_PER_GROUP		4
4136ca09beS[email protected] #define MAX_TICKS			(~0U >> 1)
4236ca09beS[email protected] #define MAX_TICKS_CASCADE		(~0U)
4336ca09beS[email protected] #define TIMER_OFFSET(num)		(1 << (TIMERS_PER_GROUP - 1 - num))
4436ca09beS[email protected] 
4536ca09beS[email protected] struct timer_regs {
4636ca09beS[email protected] 	u32	gtccr;
4736ca09beS[email protected] 	u32	res0[3];
4836ca09beS[email protected] 	u32	gtbcr;
4936ca09beS[email protected] 	u32	res1[3];
5036ca09beS[email protected] 	u32	gtvpr;
5136ca09beS[email protected] 	u32	res2[3];
5236ca09beS[email protected] 	u32	gtdr;
5336ca09beS[email protected] 	u32	res3[3];
5436ca09beS[email protected] };
5536ca09beS[email protected] 
5636ca09beS[email protected] struct cascade_priv {
5736ca09beS[email protected] 	u32 tcr_value;			/* TCR register: CASC & ROVR value */
5836ca09beS[email protected] 	unsigned int cascade_map;	/* cascade map */
5936ca09beS[email protected] 	unsigned int timer_num;		/* cascade control timer */
6036ca09beS[email protected] };
6136ca09beS[email protected] 
6236ca09beS[email protected] struct timer_group_priv {
6336ca09beS[email protected] 	struct timer_regs __iomem	*regs;
6436ca09beS[email protected] 	struct mpic_timer		timer[TIMERS_PER_GROUP];
6536ca09beS[email protected] 	struct list_head		node;
6636ca09beS[email protected] 	unsigned int			timerfreq;
6736ca09beS[email protected] 	unsigned int			idle;
6836ca09beS[email protected] 	unsigned int			flags;
6936ca09beS[email protected] 	spinlock_t			lock;
7036ca09beS[email protected] 	void __iomem			*group_tcr;
7136ca09beS[email protected] };
7236ca09beS[email protected] 
7336ca09beS[email protected] static struct cascade_priv cascade_timer[] = {
7436ca09beS[email protected] 	/* cascade timer 0 and 1 */
7536ca09beS[email protected] 	{0x1, 0xc, 0x1},
7636ca09beS[email protected] 	/* cascade timer 1 and 2 */
7736ca09beS[email protected] 	{0x2, 0x6, 0x2},
7836ca09beS[email protected] 	/* cascade timer 2 and 3 */
7936ca09beS[email protected] 	{0x4, 0x3, 0x3}
8036ca09beS[email protected] };
8136ca09beS[email protected] 
8236ca09beS[email protected] static LIST_HEAD(timer_group_list);
8336ca09beS[email protected] 
convert_ticks_to_time(struct timer_group_priv * priv,const u64 ticks,time64_t * time)8436ca09beS[email protected] static void convert_ticks_to_time(struct timer_group_priv *priv,
8511ed8c55SArnd Bergmann 		const u64 ticks, time64_t *time)
8636ca09beS[email protected] {
8711ed8c55SArnd Bergmann 	*time = (u64)div_u64(ticks, priv->timerfreq);
8836ca09beS[email protected] }
8936ca09beS[email protected] 
9036ca09beS[email protected] /* the time set by the user is converted to "ticks" */
convert_time_to_ticks(struct timer_group_priv * priv,time64_t time,u64 * ticks)9136ca09beS[email protected] static int convert_time_to_ticks(struct timer_group_priv *priv,
9211ed8c55SArnd Bergmann 		time64_t time, u64 *ticks)
9336ca09beS[email protected] {
9436ca09beS[email protected] 	u64 max_value;		/* prevent u64 overflow */
9536ca09beS[email protected] 
9636ca09beS[email protected] 	max_value = div_u64(ULLONG_MAX, priv->timerfreq);
9736ca09beS[email protected] 
9811ed8c55SArnd Bergmann 	if (time > max_value)
9936ca09beS[email protected] 		return -EINVAL;
10036ca09beS[email protected] 
10111ed8c55SArnd Bergmann 	*ticks = (u64)time * (u64)priv->timerfreq;
10236ca09beS[email protected] 
10336ca09beS[email protected] 	return 0;
10436ca09beS[email protected] }
10536ca09beS[email protected] 
10636ca09beS[email protected] /* detect whether there is a cascade timer available */
detect_idle_cascade_timer(struct timer_group_priv * priv)10736ca09beS[email protected] static struct mpic_timer *detect_idle_cascade_timer(
10836ca09beS[email protected] 					struct timer_group_priv *priv)
10936ca09beS[email protected] {
11036ca09beS[email protected] 	struct cascade_priv *casc_priv;
11136ca09beS[email protected] 	unsigned int map;
11236ca09beS[email protected] 	unsigned int array_size = ARRAY_SIZE(cascade_timer);
11336ca09beS[email protected] 	unsigned int num;
11436ca09beS[email protected] 	unsigned int i;
11536ca09beS[email protected] 	unsigned long flags;
11636ca09beS[email protected] 
11736ca09beS[email protected] 	casc_priv = cascade_timer;
11836ca09beS[email protected] 	for (i = 0; i < array_size; i++) {
11936ca09beS[email protected] 		spin_lock_irqsave(&priv->lock, flags);
12036ca09beS[email protected] 		map = casc_priv->cascade_map & priv->idle;
12136ca09beS[email protected] 		if (map == casc_priv->cascade_map) {
12236ca09beS[email protected] 			num = casc_priv->timer_num;
12336ca09beS[email protected] 			priv->timer[num].cascade_handle = casc_priv;
12436ca09beS[email protected] 
12536ca09beS[email protected] 			/* set timer busy */
12636ca09beS[email protected] 			priv->idle &= ~casc_priv->cascade_map;
12736ca09beS[email protected] 			spin_unlock_irqrestore(&priv->lock, flags);
12836ca09beS[email protected] 			return &priv->timer[num];
12936ca09beS[email protected] 		}
13036ca09beS[email protected] 		spin_unlock_irqrestore(&priv->lock, flags);
13136ca09beS[email protected] 		casc_priv++;
13236ca09beS[email protected] 	}
13336ca09beS[email protected] 
13436ca09beS[email protected] 	return NULL;
13536ca09beS[email protected] }
13636ca09beS[email protected] 
set_cascade_timer(struct timer_group_priv * priv,u64 ticks,unsigned int num)13736ca09beS[email protected] static int set_cascade_timer(struct timer_group_priv *priv, u64 ticks,
13836ca09beS[email protected] 		unsigned int num)
13936ca09beS[email protected] {
14036ca09beS[email protected] 	struct cascade_priv *casc_priv;
14136ca09beS[email protected] 	u32 tcr;
14236ca09beS[email protected] 	u32 tmp_ticks;
14336ca09beS[email protected] 	u32 rem_ticks;
14436ca09beS[email protected] 
14536ca09beS[email protected] 	/* set group tcr reg for cascade */
14636ca09beS[email protected] 	casc_priv = priv->timer[num].cascade_handle;
14736ca09beS[email protected] 	if (!casc_priv)
14836ca09beS[email protected] 		return -EINVAL;
14936ca09beS[email protected] 
15036ca09beS[email protected] 	tcr = casc_priv->tcr_value |
15136ca09beS[email protected] 		(casc_priv->tcr_value << MPIC_TIMER_TCR_ROVR_OFFSET);
15236ca09beS[email protected] 	setbits32(priv->group_tcr, tcr);
15336ca09beS[email protected] 
15436ca09beS[email protected] 	tmp_ticks = div_u64_rem(ticks, MAX_TICKS_CASCADE, &rem_ticks);
15536ca09beS[email protected] 
15636ca09beS[email protected] 	out_be32(&priv->regs[num].gtccr, 0);
15736ca09beS[email protected] 	out_be32(&priv->regs[num].gtbcr, tmp_ticks | TIMER_STOP);
15836ca09beS[email protected] 
15936ca09beS[email protected] 	out_be32(&priv->regs[num - 1].gtccr, 0);
16036ca09beS[email protected] 	out_be32(&priv->regs[num - 1].gtbcr, rem_ticks);
16136ca09beS[email protected] 
16236ca09beS[email protected] 	return 0;
16336ca09beS[email protected] }
16436ca09beS[email protected] 
get_cascade_timer(struct timer_group_priv * priv,u64 ticks)16536ca09beS[email protected] static struct mpic_timer *get_cascade_timer(struct timer_group_priv *priv,
16636ca09beS[email protected] 					u64 ticks)
16736ca09beS[email protected] {
16836ca09beS[email protected] 	struct mpic_timer *allocated_timer;
16936ca09beS[email protected] 
17036ca09beS[email protected] 	/* Two cascade timers: Support the maximum time */
17136ca09beS[email protected] 	const u64 max_ticks = (u64)MAX_TICKS * (u64)MAX_TICKS_CASCADE;
17236ca09beS[email protected] 	int ret;
17336ca09beS[email protected] 
17436ca09beS[email protected] 	if (ticks > max_ticks)
17536ca09beS[email protected] 		return NULL;
17636ca09beS[email protected] 
17736ca09beS[email protected] 	/* detect idle timer */
17836ca09beS[email protected] 	allocated_timer = detect_idle_cascade_timer(priv);
17936ca09beS[email protected] 	if (!allocated_timer)
18036ca09beS[email protected] 		return NULL;
18136ca09beS[email protected] 
18236ca09beS[email protected] 	/* set ticks to timer */
18336ca09beS[email protected] 	ret = set_cascade_timer(priv, ticks, allocated_timer->num);
18436ca09beS[email protected] 	if (ret < 0)
18536ca09beS[email protected] 		return NULL;
18636ca09beS[email protected] 
18736ca09beS[email protected] 	return allocated_timer;
18836ca09beS[email protected] }
18936ca09beS[email protected] 
get_timer(time64_t time)19011ed8c55SArnd Bergmann static struct mpic_timer *get_timer(time64_t time)
19136ca09beS[email protected] {
19236ca09beS[email protected] 	struct timer_group_priv *priv;
19336ca09beS[email protected] 	struct mpic_timer *timer;
19436ca09beS[email protected] 
19536ca09beS[email protected] 	u64 ticks;
19636ca09beS[email protected] 	unsigned int num;
19736ca09beS[email protected] 	unsigned int i;
19836ca09beS[email protected] 	unsigned long flags;
19936ca09beS[email protected] 	int ret;
20036ca09beS[email protected] 
20136ca09beS[email protected] 	list_for_each_entry(priv, &timer_group_list, node) {
20236ca09beS[email protected] 		ret = convert_time_to_ticks(priv, time, &ticks);
20336ca09beS[email protected] 		if (ret < 0)
20436ca09beS[email protected] 			return NULL;
20536ca09beS[email protected] 
20636ca09beS[email protected] 		if (ticks > MAX_TICKS) {
20736ca09beS[email protected] 			if (!(priv->flags & FSL_GLOBAL_TIMER))
20836ca09beS[email protected] 				return NULL;
20936ca09beS[email protected] 
21036ca09beS[email protected] 			timer = get_cascade_timer(priv, ticks);
21136ca09beS[email protected] 			if (!timer)
21236ca09beS[email protected] 				continue;
21336ca09beS[email protected] 
21436ca09beS[email protected] 			return timer;
21536ca09beS[email protected] 		}
21636ca09beS[email protected] 
21736ca09beS[email protected] 		for (i = 0; i < TIMERS_PER_GROUP; i++) {
21836ca09beS[email protected] 			/* one timer: Reverse allocation */
21936ca09beS[email protected] 			num = TIMERS_PER_GROUP - 1 - i;
22036ca09beS[email protected] 			spin_lock_irqsave(&priv->lock, flags);
22136ca09beS[email protected] 			if (priv->idle & (1 << i)) {
22236ca09beS[email protected] 				/* set timer busy */
22336ca09beS[email protected] 				priv->idle &= ~(1 << i);
22436ca09beS[email protected] 				/* set ticks & stop timer */
22536ca09beS[email protected] 				out_be32(&priv->regs[num].gtbcr,
22636ca09beS[email protected] 					ticks | TIMER_STOP);
22736ca09beS[email protected] 				out_be32(&priv->regs[num].gtccr, 0);
22836ca09beS[email protected] 				priv->timer[num].cascade_handle = NULL;
22936ca09beS[email protected] 				spin_unlock_irqrestore(&priv->lock, flags);
23036ca09beS[email protected] 				return &priv->timer[num];
23136ca09beS[email protected] 			}
23236ca09beS[email protected] 			spin_unlock_irqrestore(&priv->lock, flags);
23336ca09beS[email protected] 		}
23436ca09beS[email protected] 	}
23536ca09beS[email protected] 
23636ca09beS[email protected] 	return NULL;
23736ca09beS[email protected] }
23836ca09beS[email protected] 
23936ca09beS[email protected] /**
24036ca09beS[email protected]  * mpic_start_timer - start hardware timer
24136ca09beS[email protected]  * @handle: the timer to be started.
24236ca09beS[email protected]  *
24336ca09beS[email protected]  * It will do ->fn(->dev) callback from the hardware interrupt at
24411ed8c55SArnd Bergmann  * the 'time64_t' point in the future.
24536ca09beS[email protected]  */
mpic_start_timer(struct mpic_timer * handle)24636ca09beS[email protected] void mpic_start_timer(struct mpic_timer *handle)
24736ca09beS[email protected] {
24836ca09beS[email protected] 	struct timer_group_priv *priv = container_of(handle,
24936ca09beS[email protected] 			struct timer_group_priv, timer[handle->num]);
25036ca09beS[email protected] 
25136ca09beS[email protected] 	clrbits32(&priv->regs[handle->num].gtbcr, TIMER_STOP);
25236ca09beS[email protected] }
25336ca09beS[email protected] EXPORT_SYMBOL(mpic_start_timer);
25436ca09beS[email protected] 
25536ca09beS[email protected] /**
25636ca09beS[email protected]  * mpic_stop_timer - stop hardware timer
257*1fd02f66SJulia Lawall  * @handle: the timer to be stopped
25836ca09beS[email protected]  *
25936ca09beS[email protected]  * The timer periodically generates an interrupt. Unless user stops the timer.
26036ca09beS[email protected]  */
mpic_stop_timer(struct mpic_timer * handle)26136ca09beS[email protected] void mpic_stop_timer(struct mpic_timer *handle)
26236ca09beS[email protected] {
26336ca09beS[email protected] 	struct timer_group_priv *priv = container_of(handle,
26436ca09beS[email protected] 			struct timer_group_priv, timer[handle->num]);
26536ca09beS[email protected] 	struct cascade_priv *casc_priv;
26636ca09beS[email protected] 
26736ca09beS[email protected] 	setbits32(&priv->regs[handle->num].gtbcr, TIMER_STOP);
26836ca09beS[email protected] 
26936ca09beS[email protected] 	casc_priv = priv->timer[handle->num].cascade_handle;
27036ca09beS[email protected] 	if (casc_priv) {
27136ca09beS[email protected] 		out_be32(&priv->regs[handle->num].gtccr, 0);
27236ca09beS[email protected] 		out_be32(&priv->regs[handle->num - 1].gtccr, 0);
27336ca09beS[email protected] 	} else {
27436ca09beS[email protected] 		out_be32(&priv->regs[handle->num].gtccr, 0);
27536ca09beS[email protected] 	}
27636ca09beS[email protected] }
27736ca09beS[email protected] EXPORT_SYMBOL(mpic_stop_timer);
27836ca09beS[email protected] 
27936ca09beS[email protected] /**
28036ca09beS[email protected]  * mpic_get_remain_time - get timer time
28136ca09beS[email protected]  * @handle: the timer to be selected.
28236ca09beS[email protected]  * @time: time for timer
28336ca09beS[email protected]  *
28436ca09beS[email protected]  * Query timer remaining time.
28536ca09beS[email protected]  */
mpic_get_remain_time(struct mpic_timer * handle,time64_t * time)28611ed8c55SArnd Bergmann void mpic_get_remain_time(struct mpic_timer *handle, time64_t *time)
28736ca09beS[email protected] {
28836ca09beS[email protected] 	struct timer_group_priv *priv = container_of(handle,
28936ca09beS[email protected] 			struct timer_group_priv, timer[handle->num]);
29036ca09beS[email protected] 	struct cascade_priv *casc_priv;
29136ca09beS[email protected] 
29236ca09beS[email protected] 	u64 ticks;
29336ca09beS[email protected] 	u32 tmp_ticks;
29436ca09beS[email protected] 
29536ca09beS[email protected] 	casc_priv = priv->timer[handle->num].cascade_handle;
29636ca09beS[email protected] 	if (casc_priv) {
29736ca09beS[email protected] 		tmp_ticks = in_be32(&priv->regs[handle->num].gtccr);
2980fd79588SWang Dongsheng 		tmp_ticks &= ~GTCCR_TOG;
29936ca09beS[email protected] 		ticks = ((u64)tmp_ticks & UINT_MAX) * (u64)MAX_TICKS_CASCADE;
30036ca09beS[email protected] 		tmp_ticks = in_be32(&priv->regs[handle->num - 1].gtccr);
30136ca09beS[email protected] 		ticks += tmp_ticks;
30236ca09beS[email protected] 	} else {
30336ca09beS[email protected] 		ticks = in_be32(&priv->regs[handle->num].gtccr);
3040fd79588SWang Dongsheng 		ticks &= ~GTCCR_TOG;
30536ca09beS[email protected] 	}
30636ca09beS[email protected] 
30736ca09beS[email protected] 	convert_ticks_to_time(priv, ticks, time);
30836ca09beS[email protected] }
30936ca09beS[email protected] EXPORT_SYMBOL(mpic_get_remain_time);
31036ca09beS[email protected] 
31136ca09beS[email protected] /**
31236ca09beS[email protected]  * mpic_free_timer - free hardware timer
31336ca09beS[email protected]  * @handle: the timer to be removed.
31436ca09beS[email protected]  *
31536ca09beS[email protected]  * Free the timer.
31636ca09beS[email protected]  *
31736ca09beS[email protected]  * Note: can not be used in interrupt context.
31836ca09beS[email protected]  */
mpic_free_timer(struct mpic_timer * handle)31936ca09beS[email protected] void mpic_free_timer(struct mpic_timer *handle)
32036ca09beS[email protected] {
32136ca09beS[email protected] 	struct timer_group_priv *priv = container_of(handle,
32236ca09beS[email protected] 			struct timer_group_priv, timer[handle->num]);
32336ca09beS[email protected] 
32436ca09beS[email protected] 	struct cascade_priv *casc_priv;
32536ca09beS[email protected] 	unsigned long flags;
32636ca09beS[email protected] 
32736ca09beS[email protected] 	mpic_stop_timer(handle);
32836ca09beS[email protected] 
32936ca09beS[email protected] 	casc_priv = priv->timer[handle->num].cascade_handle;
33036ca09beS[email protected] 
33136ca09beS[email protected] 	free_irq(priv->timer[handle->num].irq, priv->timer[handle->num].dev);
33236ca09beS[email protected] 
33336ca09beS[email protected] 	spin_lock_irqsave(&priv->lock, flags);
33436ca09beS[email protected] 	if (casc_priv) {
33536ca09beS[email protected] 		u32 tcr;
33636ca09beS[email protected] 		tcr = casc_priv->tcr_value | (casc_priv->tcr_value <<
33736ca09beS[email protected] 					MPIC_TIMER_TCR_ROVR_OFFSET);
33836ca09beS[email protected] 		clrbits32(priv->group_tcr, tcr);
33936ca09beS[email protected] 		priv->idle |= casc_priv->cascade_map;
34036ca09beS[email protected] 		priv->timer[handle->num].cascade_handle = NULL;
34136ca09beS[email protected] 	} else {
34236ca09beS[email protected] 		priv->idle |= TIMER_OFFSET(handle->num);
34336ca09beS[email protected] 	}
34436ca09beS[email protected] 	spin_unlock_irqrestore(&priv->lock, flags);
34536ca09beS[email protected] }
34636ca09beS[email protected] EXPORT_SYMBOL(mpic_free_timer);
34736ca09beS[email protected] 
34836ca09beS[email protected] /**
34936ca09beS[email protected]  * mpic_request_timer - get a hardware timer
35036ca09beS[email protected]  * @fn: interrupt handler function
35136ca09beS[email protected]  * @dev: callback function of the data
35236ca09beS[email protected]  * @time: time for timer
35336ca09beS[email protected]  *
35436ca09beS[email protected]  * This executes the "request_irq", returning NULL
35536ca09beS[email protected]  * else "handle" on success.
35636ca09beS[email protected]  */
mpic_request_timer(irq_handler_t fn,void * dev,time64_t time)35736ca09beS[email protected] struct mpic_timer *mpic_request_timer(irq_handler_t fn, void *dev,
35811ed8c55SArnd Bergmann 				      time64_t time)
35936ca09beS[email protected] {
36036ca09beS[email protected] 	struct mpic_timer *allocated_timer;
36136ca09beS[email protected] 	int ret;
36236ca09beS[email protected] 
36336ca09beS[email protected] 	if (list_empty(&timer_group_list))
36436ca09beS[email protected] 		return NULL;
36536ca09beS[email protected] 
36611ed8c55SArnd Bergmann 	if (time < 0)
36736ca09beS[email protected] 		return NULL;
36836ca09beS[email protected] 
36936ca09beS[email protected] 	allocated_timer = get_timer(time);
37036ca09beS[email protected] 	if (!allocated_timer)
37136ca09beS[email protected] 		return NULL;
37236ca09beS[email protected] 
37336ca09beS[email protected] 	ret = request_irq(allocated_timer->irq, fn,
37436ca09beS[email protected] 			IRQF_TRIGGER_LOW, "global-timer", dev);
37536ca09beS[email protected] 	if (ret) {
37636ca09beS[email protected] 		mpic_free_timer(allocated_timer);
37736ca09beS[email protected] 		return NULL;
37836ca09beS[email protected] 	}
37936ca09beS[email protected] 
38036ca09beS[email protected] 	allocated_timer->dev = dev;
38136ca09beS[email protected] 
38236ca09beS[email protected] 	return allocated_timer;
38336ca09beS[email protected] }
38436ca09beS[email protected] EXPORT_SYMBOL(mpic_request_timer);
38536ca09beS[email protected] 
timer_group_get_freq(struct device_node * np,struct timer_group_priv * priv)3866c552983SNick Child static int __init timer_group_get_freq(struct device_node *np,
38736ca09beS[email protected] 			struct timer_group_priv *priv)
38836ca09beS[email protected] {
38936ca09beS[email protected] 	u32 div;
39036ca09beS[email protected] 
39136ca09beS[email protected] 	if (priv->flags & FSL_GLOBAL_TIMER) {
39236ca09beS[email protected] 		struct device_node *dn;
39336ca09beS[email protected] 
39436ca09beS[email protected] 		dn = of_find_compatible_node(NULL, NULL, "fsl,mpic");
39536ca09beS[email protected] 		if (dn) {
39636ca09beS[email protected] 			of_property_read_u32(dn, "clock-frequency",
39736ca09beS[email protected] 					&priv->timerfreq);
39836ca09beS[email protected] 			of_node_put(dn);
39936ca09beS[email protected] 		}
40036ca09beS[email protected] 	}
40136ca09beS[email protected] 
40236ca09beS[email protected] 	if (priv->timerfreq <= 0)
40336ca09beS[email protected] 		return -EINVAL;
40436ca09beS[email protected] 
40536ca09beS[email protected] 	if (priv->flags & FSL_GLOBAL_TIMER) {
40636ca09beS[email protected] 		div = (1 << (MPIC_TIMER_TCR_CLKDIV >> 8)) * 8;
40736ca09beS[email protected] 		priv->timerfreq /= div;
40836ca09beS[email protected] 	}
40936ca09beS[email protected] 
41036ca09beS[email protected] 	return 0;
41136ca09beS[email protected] }
41236ca09beS[email protected] 
timer_group_get_irq(struct device_node * np,struct timer_group_priv * priv)4136c552983SNick Child static int __init timer_group_get_irq(struct device_node *np,
41436ca09beS[email protected] 		struct timer_group_priv *priv)
41536ca09beS[email protected] {
41636ca09beS[email protected] 	const u32 all_timer[] = { 0, TIMERS_PER_GROUP };
41736ca09beS[email protected] 	const u32 *p;
41836ca09beS[email protected] 	u32 offset;
41936ca09beS[email protected] 	u32 count;
42036ca09beS[email protected] 
42136ca09beS[email protected] 	unsigned int i;
42236ca09beS[email protected] 	unsigned int j;
42336ca09beS[email protected] 	unsigned int irq_index = 0;
42436ca09beS[email protected] 	unsigned int irq;
42536ca09beS[email protected] 	int len;
42636ca09beS[email protected] 
42736ca09beS[email protected] 	p = of_get_property(np, "fsl,available-ranges", &len);
42836ca09beS[email protected] 	if (p && len % (2 * sizeof(u32)) != 0) {
429b7c670d6SRob Herring 		pr_err("%pOF: malformed available-ranges property.\n", np);
43036ca09beS[email protected] 		return -EINVAL;
43136ca09beS[email protected] 	}
43236ca09beS[email protected] 
43336ca09beS[email protected] 	if (!p) {
43436ca09beS[email protected] 		p = all_timer;
43536ca09beS[email protected] 		len = sizeof(all_timer);
43636ca09beS[email protected] 	}
43736ca09beS[email protected] 
43836ca09beS[email protected] 	len /= 2 * sizeof(u32);
43936ca09beS[email protected] 
44036ca09beS[email protected] 	for (i = 0; i < len; i++) {
44136ca09beS[email protected] 		offset = p[i * 2];
44236ca09beS[email protected] 		count = p[i * 2 + 1];
44336ca09beS[email protected] 		for (j = 0; j < count; j++) {
44436ca09beS[email protected] 			irq = irq_of_parse_and_map(np, irq_index);
44536ca09beS[email protected] 			if (!irq) {
446b7c670d6SRob Herring 				pr_err("%pOF: irq parse and map failed.\n", np);
44736ca09beS[email protected] 				return -EINVAL;
44836ca09beS[email protected] 			}
44936ca09beS[email protected] 
45036ca09beS[email protected] 			/* Set timer idle */
45136ca09beS[email protected] 			priv->idle |= TIMER_OFFSET((offset + j));
45236ca09beS[email protected] 			priv->timer[offset + j].irq = irq;
45336ca09beS[email protected] 			priv->timer[offset + j].num = offset + j;
45436ca09beS[email protected] 			irq_index++;
45536ca09beS[email protected] 		}
45636ca09beS[email protected] 	}
45736ca09beS[email protected] 
45836ca09beS[email protected] 	return 0;
45936ca09beS[email protected] }
46036ca09beS[email protected] 
timer_group_init(struct device_node * np)4616c552983SNick Child static void __init timer_group_init(struct device_node *np)
46236ca09beS[email protected] {
46336ca09beS[email protected] 	struct timer_group_priv *priv;
46436ca09beS[email protected] 	unsigned int i = 0;
46536ca09beS[email protected] 	int ret;
46636ca09beS[email protected] 
46736ca09beS[email protected] 	priv = kzalloc(sizeof(struct timer_group_priv), GFP_KERNEL);
46836ca09beS[email protected] 	if (!priv) {
469b7c670d6SRob Herring 		pr_err("%pOF: cannot allocate memory for group.\n", np);
47036ca09beS[email protected] 		return;
47136ca09beS[email protected] 	}
47236ca09beS[email protected] 
47336ca09beS[email protected] 	if (of_device_is_compatible(np, "fsl,mpic-global-timer"))
47436ca09beS[email protected] 		priv->flags |= FSL_GLOBAL_TIMER;
47536ca09beS[email protected] 
47636ca09beS[email protected] 	priv->regs = of_iomap(np, i++);
47736ca09beS[email protected] 	if (!priv->regs) {
478b7c670d6SRob Herring 		pr_err("%pOF: cannot ioremap timer register address.\n", np);
47936ca09beS[email protected] 		goto out;
48036ca09beS[email protected] 	}
48136ca09beS[email protected] 
48236ca09beS[email protected] 	if (priv->flags & FSL_GLOBAL_TIMER) {
48336ca09beS[email protected] 		priv->group_tcr = of_iomap(np, i++);
48436ca09beS[email protected] 		if (!priv->group_tcr) {
485b7c670d6SRob Herring 			pr_err("%pOF: cannot ioremap tcr address.\n", np);
48636ca09beS[email protected] 			goto out;
48736ca09beS[email protected] 		}
48836ca09beS[email protected] 	}
48936ca09beS[email protected] 
49036ca09beS[email protected] 	ret = timer_group_get_freq(np, priv);
49136ca09beS[email protected] 	if (ret < 0) {
492b7c670d6SRob Herring 		pr_err("%pOF: cannot get timer frequency.\n", np);
49336ca09beS[email protected] 		goto out;
49436ca09beS[email protected] 	}
49536ca09beS[email protected] 
49636ca09beS[email protected] 	ret = timer_group_get_irq(np, priv);
49736ca09beS[email protected] 	if (ret < 0) {
498b7c670d6SRob Herring 		pr_err("%pOF: cannot get timer irqs.\n", np);
49936ca09beS[email protected] 		goto out;
50036ca09beS[email protected] 	}
50136ca09beS[email protected] 
50236ca09beS[email protected] 	spin_lock_init(&priv->lock);
50336ca09beS[email protected] 
50436ca09beS[email protected] 	/* Init FSL timer hardware */
50536ca09beS[email protected] 	if (priv->flags & FSL_GLOBAL_TIMER)
50636ca09beS[email protected] 		setbits32(priv->group_tcr, MPIC_TIMER_TCR_CLKDIV);
50736ca09beS[email protected] 
50836ca09beS[email protected] 	list_add_tail(&priv->node, &timer_group_list);
50936ca09beS[email protected] 
51036ca09beS[email protected] 	return;
51136ca09beS[email protected] 
51236ca09beS[email protected] out:
51336ca09beS[email protected] 	if (priv->regs)
51436ca09beS[email protected] 		iounmap(priv->regs);
51536ca09beS[email protected] 
51636ca09beS[email protected] 	if (priv->group_tcr)
51736ca09beS[email protected] 		iounmap(priv->group_tcr);
51836ca09beS[email protected] 
51936ca09beS[email protected] 	kfree(priv);
52036ca09beS[email protected] }
52136ca09beS[email protected] 
mpic_timer_resume(void)52236ca09beS[email protected] static void mpic_timer_resume(void)
52336ca09beS[email protected] {
52436ca09beS[email protected] 	struct timer_group_priv *priv;
52536ca09beS[email protected] 
52636ca09beS[email protected] 	list_for_each_entry(priv, &timer_group_list, node) {
52736ca09beS[email protected] 		/* Init FSL timer hardware */
52836ca09beS[email protected] 		if (priv->flags & FSL_GLOBAL_TIMER)
52936ca09beS[email protected] 			setbits32(priv->group_tcr, MPIC_TIMER_TCR_CLKDIV);
53036ca09beS[email protected] 	}
53136ca09beS[email protected] }
53236ca09beS[email protected] 
53336ca09beS[email protected] static const struct of_device_id mpic_timer_ids[] = {
53436ca09beS[email protected] 	{ .compatible = "fsl,mpic-global-timer", },
53536ca09beS[email protected] 	{},
53636ca09beS[email protected] };
53736ca09beS[email protected] 
53836ca09beS[email protected] static struct syscore_ops mpic_timer_syscore_ops = {
53936ca09beS[email protected] 	.resume = mpic_timer_resume,
54036ca09beS[email protected] };
54136ca09beS[email protected] 
mpic_timer_init(void)54236ca09beS[email protected] static int __init mpic_timer_init(void)
54336ca09beS[email protected] {
54436ca09beS[email protected] 	struct device_node *np = NULL;
54536ca09beS[email protected] 
54636ca09beS[email protected] 	for_each_matching_node(np, mpic_timer_ids)
54736ca09beS[email protected] 		timer_group_init(np);
54836ca09beS[email protected] 
54936ca09beS[email protected] 	register_syscore_ops(&mpic_timer_syscore_ops);
55036ca09beS[email protected] 
55136ca09beS[email protected] 	if (list_empty(&timer_group_list))
55236ca09beS[email protected] 		return -ENODEV;
55336ca09beS[email protected] 
55436ca09beS[email protected] 	return 0;
55536ca09beS[email protected] }
55636ca09beS[email protected] subsys_initcall(mpic_timer_init);
557