blob: 4b3afab92ea7f2036264b4a56b4bb252aef7979f [file] [log] [blame]
developerd48dd9a2018-12-20 16:12:51 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 *
5 * Author: Ryder Lee <ryder.lee@mediatek.com>
6 * Weijie Gao <weijie.gao@mediatek.com>
7 */
8
developerd48dd9a2018-12-20 16:12:51 +08009#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
developerd48dd9a2018-12-20 16:12:51 +080012#include <dm/lists.h>
13#include <regmap.h>
14#include <reset-uclass.h>
15#include <syscon.h>
Simon Glass95588622020-12-22 19:30:28 -070016#include <dm/device-internal.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
developerd48dd9a2018-12-20 16:12:51 +080019
20struct mediatek_reset_priv {
21 struct regmap *regmap;
22 u32 regofs;
23 u32 nr_resets;
24};
25
developerd48dd9a2018-12-20 16:12:51 +080026static int mediatek_reset_assert(struct reset_ctl *reset_ctl)
27{
28 struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev);
29 int id = reset_ctl->id;
30
31 if (id >= priv->nr_resets)
32 return -EINVAL;
33
34 return regmap_update_bits(priv->regmap,
35 priv->regofs + ((id / 32) << 2), BIT(id % 32), BIT(id % 32));
36}
37
38static int mediatek_reset_deassert(struct reset_ctl *reset_ctl)
39{
40 struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev);
41 int id = reset_ctl->id;
42
43 if (id >= priv->nr_resets)
44 return -EINVAL;
45
46 return regmap_update_bits(priv->regmap,
47 priv->regofs + ((id / 32) << 2), BIT(id % 32), 0);
48}
49
50struct reset_ops mediatek_reset_ops = {
developerd48dd9a2018-12-20 16:12:51 +080051 .rst_assert = mediatek_reset_assert,
52 .rst_deassert = mediatek_reset_deassert,
53};
54
55static int mediatek_reset_probe(struct udevice *dev)
56{
57 struct mediatek_reset_priv *priv = dev_get_priv(dev);
58
59 if (!priv->regofs && !priv->nr_resets)
60 return -EINVAL;
61
62 priv->regmap = syscon_node_to_regmap(dev_ofnode(dev));
63 if (IS_ERR(priv->regmap))
64 return PTR_ERR(priv->regmap);
65
66 return 0;
67}
68
69int mediatek_reset_bind(struct udevice *pdev, u32 regofs, u32 num_regs)
70{
71 struct udevice *rst_dev;
72 struct mediatek_reset_priv *priv;
73 int ret;
74
75 ret = device_bind_driver_to_node(pdev, "mediatek_reset", "reset",
76 dev_ofnode(pdev), &rst_dev);
77 if (ret)
78 return ret;
79
80 priv = malloc(sizeof(struct mediatek_reset_priv));
developer7f679572023-07-19 17:16:11 +080081 if (!priv)
82 return -ENOMEM;
83
developerd48dd9a2018-12-20 16:12:51 +080084 priv->regofs = regofs;
85 priv->nr_resets = num_regs * 32;
Simon Glass95588622020-12-22 19:30:28 -070086 dev_set_priv(rst_dev, priv);
developerd48dd9a2018-12-20 16:12:51 +080087
88 return 0;
89}
90
91U_BOOT_DRIVER(mediatek_reset) = {
92 .name = "mediatek_reset",
93 .id = UCLASS_RESET,
94 .probe = mediatek_reset_probe,
95 .ops = &mediatek_reset_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -070096 .priv_auto = sizeof(struct mediatek_reset_priv),
developerd48dd9a2018-12-20 16:12:51 +080097};