Weijie Gao | 3e066bc | 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 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Weijie Gao | 3e066bc | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 13 | #include <dm/lists.h> |
| 14 | #include <regmap.h> |
| 15 | #include <reset-uclass.h> |
| 16 | #include <syscon.h> |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Weijie Gao | 3e066bc | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 20 | |
| 21 | struct mediatek_reset_priv { |
| 22 | struct regmap *regmap; |
| 23 | u32 regofs; |
| 24 | u32 nr_resets; |
| 25 | }; |
| 26 | |
| 27 | static int mediatek_reset_request(struct reset_ctl *reset_ctl) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static int mediatek_reset_free(struct reset_ctl *reset_ctl) |
| 33 | { |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static int mediatek_reset_assert(struct reset_ctl *reset_ctl) |
| 38 | { |
| 39 | struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 40 | int id = reset_ctl->id; |
| 41 | |
| 42 | if (id >= priv->nr_resets) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | return regmap_update_bits(priv->regmap, |
| 46 | priv->regofs + ((id / 32) << 2), BIT(id % 32), BIT(id % 32)); |
| 47 | } |
| 48 | |
| 49 | static int mediatek_reset_deassert(struct reset_ctl *reset_ctl) |
| 50 | { |
| 51 | struct mediatek_reset_priv *priv = dev_get_priv(reset_ctl->dev); |
| 52 | int id = reset_ctl->id; |
| 53 | |
| 54 | if (id >= priv->nr_resets) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | return regmap_update_bits(priv->regmap, |
| 58 | priv->regofs + ((id / 32) << 2), BIT(id % 32), 0); |
| 59 | } |
| 60 | |
| 61 | struct reset_ops mediatek_reset_ops = { |
| 62 | .request = mediatek_reset_request, |
Simon Glass | 94474b2 | 2020-02-03 07:35:52 -0700 | [diff] [blame] | 63 | .rfree = mediatek_reset_free, |
Weijie Gao | 3e066bc | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 64 | .rst_assert = mediatek_reset_assert, |
| 65 | .rst_deassert = mediatek_reset_deassert, |
| 66 | }; |
| 67 | |
| 68 | static int mediatek_reset_probe(struct udevice *dev) |
| 69 | { |
| 70 | struct mediatek_reset_priv *priv = dev_get_priv(dev); |
| 71 | |
| 72 | if (!priv->regofs && !priv->nr_resets) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | priv->regmap = syscon_node_to_regmap(dev_ofnode(dev)); |
| 76 | if (IS_ERR(priv->regmap)) |
| 77 | return PTR_ERR(priv->regmap); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int mediatek_reset_bind(struct udevice *pdev, u32 regofs, u32 num_regs) |
| 83 | { |
| 84 | struct udevice *rst_dev; |
| 85 | struct mediatek_reset_priv *priv; |
| 86 | int ret; |
| 87 | |
| 88 | ret = device_bind_driver_to_node(pdev, "mediatek_reset", "reset", |
| 89 | dev_ofnode(pdev), &rst_dev); |
| 90 | if (ret) |
| 91 | return ret; |
| 92 | |
| 93 | priv = malloc(sizeof(struct mediatek_reset_priv)); |
| 94 | priv->regofs = regofs; |
| 95 | priv->nr_resets = num_regs * 32; |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 96 | dev_set_priv(rst_dev, priv); |
Weijie Gao | 3e066bc | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | U_BOOT_DRIVER(mediatek_reset) = { |
| 102 | .name = "mediatek_reset", |
| 103 | .id = UCLASS_RESET, |
| 104 | .probe = mediatek_reset_probe, |
| 105 | .ops = &mediatek_reset_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 106 | .priv_auto = sizeof(struct mediatek_reset_priv), |
Weijie Gao | 3e066bc | 2018-12-20 16:12:51 +0800 | [diff] [blame] | 107 | }; |