1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Actions Semi Owl Smart Power System (SPS) shared helpers
4 *
5 * Copyright 2012 Actions Semi Inc.
6 * Author: Actions Semi, Inc.
7 *
8 * Copyright (c) 2017 Andreas Färber
9 */
10
11#include <linux/delay.h>
12#include <linux/io.h>
13#include <linux/soc/actions/owl-sps.h>
14
15#define OWL_SPS_PG_CTL	0x0
16
17int owl_sps_set_pg(void __iomem *base, u32 pwr_mask, u32 ack_mask, bool enable)
18{
19	u32 val;
20	bool ack;
21	int timeout;
22
23	val = readl(base + OWL_SPS_PG_CTL);
24	ack = val & ack_mask;
25	if (ack == enable)
26		return 0;
27
28	if (enable)
29		val |= pwr_mask;
30	else
31		val &= ~pwr_mask;
32
33	writel(val, base + OWL_SPS_PG_CTL);
34
35	for (timeout = 5000; timeout > 0; timeout -= 50) {
36		val = readl(base + OWL_SPS_PG_CTL);
37		if ((val & ack_mask) == (enable ? ack_mask : 0))
38			break;
39		udelay(50);
40	}
41	if (timeout <= 0)
42		return -ETIMEDOUT;
43
44	udelay(10);
45
46	return 0;
47}
48EXPORT_SYMBOL_GPL(owl_sps_set_pg);
49