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