blob: fd47e1f9e377e4e782cdc9c731f885a0f7b613e8 [file] [log] [blame]
Jagan Teki7f6c2a82019-01-18 22:18:13 +05301// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (C) 2018 Amarula Solutions.
4 * Author: Jagan Teki <jagan@amarulasolutions.com>
5 */
6
Jagan Teki7f6c2a82019-01-18 22:18:13 +05307#include <dm.h>
8#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053011#include <reset-uclass.h>
12#include <asm/io.h>
Samuel Holland12e3faa2021-09-12 11:48:43 -050013#include <clk/sunxi.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053015#include <linux/log2.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053016
Samuel Holland86b561c2022-05-09 00:29:37 -050017static const struct ccu_reset *plat_to_reset(struct ccu_plat *plat,
Jagan Teki7f6c2a82019-01-18 22:18:13 +053018 unsigned long id)
19{
Samuel Holland24a870c2022-05-09 00:29:36 -050020 return &plat->desc->resets[id];
Jagan Teki7f6c2a82019-01-18 22:18:13 +053021}
22
23static int sunxi_reset_request(struct reset_ctl *reset_ctl)
24{
Samuel Holland86b561c2022-05-09 00:29:37 -050025 struct ccu_plat *plat = dev_get_plat(reset_ctl->dev);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053026
27 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
28
Samuel Holland24a870c2022-05-09 00:29:36 -050029 if (reset_ctl->id >= plat->desc->num_resets)
Jagan Teki7f6c2a82019-01-18 22:18:13 +053030 return -EINVAL;
31
32 return 0;
33}
34
Jagan Teki7f6c2a82019-01-18 22:18:13 +053035static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
36{
Samuel Holland86b561c2022-05-09 00:29:37 -050037 struct ccu_plat *plat = dev_get_plat(reset_ctl->dev);
Samuel Holland24a870c2022-05-09 00:29:36 -050038 const struct ccu_reset *reset = plat_to_reset(plat, reset_ctl->id);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053039 u32 reg;
40
41 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
42 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
43 return 0;
44 }
45
46 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
47 reset_ctl->id, reset->off, ilog2(reset->bit));
48
Samuel Holland24a870c2022-05-09 00:29:36 -050049 reg = readl(plat->base + reset->off);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053050 if (on)
51 reg |= reset->bit;
52 else
53 reg &= ~reset->bit;
54
Samuel Holland24a870c2022-05-09 00:29:36 -050055 writel(reg, plat->base + reset->off);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053056
57 return 0;
58}
59
60static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
61{
62 return sunxi_set_reset(reset_ctl, false);
63}
64
65static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
66{
67 return sunxi_set_reset(reset_ctl, true);
68}
69
70struct reset_ops sunxi_reset_ops = {
71 .request = sunxi_reset_request,
Jagan Teki7f6c2a82019-01-18 22:18:13 +053072 .rst_assert = sunxi_reset_assert,
73 .rst_deassert = sunxi_reset_deassert,
74};
75
Jagan Teki7f6c2a82019-01-18 22:18:13 +053076U_BOOT_DRIVER(sunxi_reset) = {
77 .name = "sunxi_reset",
78 .id = UCLASS_RESET,
79 .ops = &sunxi_reset_ops,
Jagan Teki7f6c2a82019-01-18 22:18:13 +053080};