// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (C) 2018 Amarula Solutions.
 * Author: Jagan Teki <jagan@amarulasolutions.com>
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <log.h>
#include <malloc.h>
#include <reset-uclass.h>
#include <asm/io.h>
#include <clk/sunxi.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <linux/bitops.h>
#include <linux/log2.h>

struct sunxi_reset_priv {
	void *base;
	ulong count;
	const struct ccu_desc *desc;
};

static const struct ccu_reset *priv_to_reset(struct sunxi_reset_priv *priv,
					     unsigned long id)
{
	return	&priv->desc->resets[id];
}

static int sunxi_reset_request(struct reset_ctl *reset_ctl)
{
	struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);

	debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);

	if (reset_ctl->id >= priv->count)
		return -EINVAL;

	return 0;
}

static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
{
	struct sunxi_reset_priv *priv = dev_get_priv(reset_ctl->dev);
	const struct ccu_reset *reset = priv_to_reset(priv, reset_ctl->id);
	u32 reg;

	if (!(reset->flags & CCU_RST_F_IS_VALID)) {
		printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
		return 0;
	}

	debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
	      reset_ctl->id, reset->off, ilog2(reset->bit));

	reg = readl(priv->base + reset->off);
	if (on)
		reg |= reset->bit;
	else
		reg &= ~reset->bit;

	writel(reg, priv->base + reset->off);

	return 0;
}

static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
{
	return sunxi_set_reset(reset_ctl, false);
}

static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
{
	return sunxi_set_reset(reset_ctl, true);
}

struct reset_ops sunxi_reset_ops = {
	.request = sunxi_reset_request,
	.rst_assert = sunxi_reset_assert,
	.rst_deassert = sunxi_reset_deassert,
};

static int sunxi_reset_probe(struct udevice *dev)
{
	struct sunxi_reset_priv *priv = dev_get_priv(dev);

	priv->base = dev_read_addr_ptr(dev);

	return 0;
}

int sunxi_reset_bind(struct udevice *dev, ulong count)
{
	struct udevice *rst_dev;
	struct sunxi_reset_priv *priv;
	int ret;

	ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
					 dev_ofnode(dev), &rst_dev);
	if (ret) {
		debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
		return ret;
	}
	priv = malloc(sizeof(struct sunxi_reset_priv));
	priv->count = count;
	priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
	dev_set_priv(rst_dev, priv);

	return 0;
}

U_BOOT_DRIVER(sunxi_reset) = {
	.name		= "sunxi_reset",
	.id		= UCLASS_RESET,
	.ops		= &sunxi_reset_ops,
	.probe		= sunxi_reset_probe,
	.priv_auto	= sizeof(struct sunxi_reset_priv),
};
