blob: f5c66dde43a22eee8097900b063764145b7511a1 [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053012#include <reset-uclass.h>
13#include <asm/io.h>
Samuel Holland12e3faa2021-09-12 11:48:43 -050014#include <clk/sunxi.h>
Simon Glass95588622020-12-22 19:30:28 -070015#include <dm/device-internal.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053016#include <dm/lists.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053018#include <linux/log2.h>
Jagan Teki7f6c2a82019-01-18 22:18:13 +053019
Samuel Holland24a870c2022-05-09 00:29:36 -050020struct sunxi_reset_plat {
Jagan Teki7f6c2a82019-01-18 22:18:13 +053021 void *base;
Jagan Teki7f6c2a82019-01-18 22:18:13 +053022 const struct ccu_desc *desc;
23};
24
Samuel Holland24a870c2022-05-09 00:29:36 -050025static const struct ccu_reset *plat_to_reset(struct sunxi_reset_plat *plat,
Jagan Teki7f6c2a82019-01-18 22:18:13 +053026 unsigned long id)
27{
Samuel Holland24a870c2022-05-09 00:29:36 -050028 return &plat->desc->resets[id];
Jagan Teki7f6c2a82019-01-18 22:18:13 +053029}
30
31static int sunxi_reset_request(struct reset_ctl *reset_ctl)
32{
Samuel Holland24a870c2022-05-09 00:29:36 -050033 struct sunxi_reset_plat *plat = dev_get_plat(reset_ctl->dev);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053034
35 debug("%s: (RST#%ld)\n", __func__, reset_ctl->id);
36
Samuel Holland24a870c2022-05-09 00:29:36 -050037 if (reset_ctl->id >= plat->desc->num_resets)
Jagan Teki7f6c2a82019-01-18 22:18:13 +053038 return -EINVAL;
39
40 return 0;
41}
42
Jagan Teki7f6c2a82019-01-18 22:18:13 +053043static int sunxi_set_reset(struct reset_ctl *reset_ctl, bool on)
44{
Samuel Holland24a870c2022-05-09 00:29:36 -050045 struct sunxi_reset_plat *plat = dev_get_plat(reset_ctl->dev);
46 const struct ccu_reset *reset = plat_to_reset(plat, reset_ctl->id);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053047 u32 reg;
48
49 if (!(reset->flags & CCU_RST_F_IS_VALID)) {
50 printf("%s: (RST#%ld) unhandled\n", __func__, reset_ctl->id);
51 return 0;
52 }
53
54 debug("%s: (RST#%ld) off#0x%x, BIT(%d)\n", __func__,
55 reset_ctl->id, reset->off, ilog2(reset->bit));
56
Samuel Holland24a870c2022-05-09 00:29:36 -050057 reg = readl(plat->base + reset->off);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053058 if (on)
59 reg |= reset->bit;
60 else
61 reg &= ~reset->bit;
62
Samuel Holland24a870c2022-05-09 00:29:36 -050063 writel(reg, plat->base + reset->off);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053064
65 return 0;
66}
67
68static int sunxi_reset_assert(struct reset_ctl *reset_ctl)
69{
70 return sunxi_set_reset(reset_ctl, false);
71}
72
73static int sunxi_reset_deassert(struct reset_ctl *reset_ctl)
74{
75 return sunxi_set_reset(reset_ctl, true);
76}
77
78struct reset_ops sunxi_reset_ops = {
79 .request = sunxi_reset_request,
Jagan Teki7f6c2a82019-01-18 22:18:13 +053080 .rst_assert = sunxi_reset_assert,
81 .rst_deassert = sunxi_reset_deassert,
82};
83
Samuel Holland24a870c2022-05-09 00:29:36 -050084static int sunxi_reset_of_to_plat(struct udevice *dev)
Jagan Teki7f6c2a82019-01-18 22:18:13 +053085{
Samuel Holland24a870c2022-05-09 00:29:36 -050086 struct sunxi_reset_plat *plat = dev_get_plat(dev);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053087
Samuel Holland24a870c2022-05-09 00:29:36 -050088 plat->base = dev_read_addr_ptr(dev);
Jagan Teki7f6c2a82019-01-18 22:18:13 +053089
90 return 0;
91}
92
Samuel Holland1567fdf2022-05-09 00:29:33 -050093int sunxi_reset_bind(struct udevice *dev)
Jagan Teki7f6c2a82019-01-18 22:18:13 +053094{
95 struct udevice *rst_dev;
Samuel Holland24a870c2022-05-09 00:29:36 -050096 struct sunxi_reset_plat *plat;
Jagan Teki7f6c2a82019-01-18 22:18:13 +053097 int ret;
98
99 ret = device_bind_driver_to_node(dev, "sunxi_reset", "reset",
100 dev_ofnode(dev), &rst_dev);
101 if (ret) {
102 debug("failed to bind sunxi_reset driver (ret=%d)\n", ret);
103 return ret;
104 }
Samuel Holland24a870c2022-05-09 00:29:36 -0500105 plat = malloc(sizeof(struct sunxi_reset_plat));
106 plat->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
107 dev_set_plat(rst_dev, plat);
Jagan Teki7f6c2a82019-01-18 22:18:13 +0530108
109 return 0;
110}
111
112U_BOOT_DRIVER(sunxi_reset) = {
113 .name = "sunxi_reset",
114 .id = UCLASS_RESET,
115 .ops = &sunxi_reset_ops,
Samuel Holland24a870c2022-05-09 00:29:36 -0500116 .of_to_plat = sunxi_reset_of_to_plat,
117 .plat_auto = sizeof(struct sunxi_reset_plat),
Jagan Teki7f6c2a82019-01-18 22:18:13 +0530118};