developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 1 | // 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 | |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 12 | #include <dm/lists.h> |
| 13 | #include <regmap.h> |
| 14 | #include <reset-uclass.h> |
| 15 | #include <syscon.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 19 | |
| 20 | struct mediatek_reset_priv { |
| 21 | struct regmap *regmap; |
| 22 | u32 regofs; |
| 23 | u32 nr_resets; |
| 24 | }; |
| 25 | |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 26 | static 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 | |
| 38 | static 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 | |
| 50 | struct reset_ops mediatek_reset_ops = { |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 51 | .rst_assert = mediatek_reset_assert, |
| 52 | .rst_deassert = mediatek_reset_deassert, |
| 53 | }; |
| 54 | |
| 55 | static 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 | |
| 69 | int 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)); |
developer | 7f67957 | 2023-07-19 17:16:11 +0800 | [diff] [blame] | 81 | if (!priv) |
| 82 | return -ENOMEM; |
| 83 | |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 84 | priv->regofs = regofs; |
| 85 | priv->nr_resets = num_regs * 32; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 86 | dev_set_priv(rst_dev, priv); |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | U_BOOT_DRIVER(mediatek_reset) = { |
| 92 | .name = "mediatek_reset", |
| 93 | .id = UCLASS_RESET, |
| 94 | .probe = mediatek_reset_probe, |
| 95 | .ops = &mediatek_reset_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 96 | .priv_auto = sizeof(struct mediatek_reset_priv), |
developer | d48dd9a | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 97 | }; |