|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (C) 2021 BAIKAL ELECTRONICS, JSC |
| 4 | + * |
| 5 | + * Authors: |
| 6 | + * Serge Semin <Sergey.Semin@baikalelectronics.ru> |
| 7 | + * |
| 8 | + * Baikal-T1 CCU Resets interface driver |
| 9 | + */ |
| 10 | + |
| 11 | +#define pr_fmt(fmt) "bt1-ccu-rst: " fmt |
| 12 | + |
| 13 | +#include <linux/bits.h> |
| 14 | +#include <linux/delay.h> |
| 15 | +#include <linux/kernel.h> |
| 16 | +#include <linux/of.h> |
| 17 | +#include <linux/printk.h> |
| 18 | +#include <linux/regmap.h> |
| 19 | +#include <linux/reset-controller.h> |
| 20 | +#include <linux/slab.h> |
| 21 | + |
| 22 | +#include <dt-bindings/reset/bt1-ccu.h> |
| 23 | + |
| 24 | +#include "ccu-rst.h" |
| 25 | + |
| 26 | +#define CCU_AXI_MAIN_BASE 0x030 |
| 27 | +#define CCU_AXI_DDR_BASE 0x034 |
| 28 | +#define CCU_AXI_SATA_BASE 0x038 |
| 29 | +#define CCU_AXI_GMAC0_BASE 0x03C |
| 30 | +#define CCU_AXI_GMAC1_BASE 0x040 |
| 31 | +#define CCU_AXI_XGMAC_BASE 0x044 |
| 32 | +#define CCU_AXI_PCIE_M_BASE 0x048 |
| 33 | +#define CCU_AXI_PCIE_S_BASE 0x04C |
| 34 | +#define CCU_AXI_USB_BASE 0x050 |
| 35 | +#define CCU_AXI_HWA_BASE 0x054 |
| 36 | +#define CCU_AXI_SRAM_BASE 0x058 |
| 37 | + |
| 38 | +#define CCU_SYS_SATA_REF_BASE 0x060 |
| 39 | +#define CCU_SYS_APB_BASE 0x064 |
| 40 | + |
| 41 | +#define CCU_RST_DELAY_US 1 |
| 42 | + |
| 43 | +#define CCU_RST_TRIG(_base, _ofs) \ |
| 44 | + { \ |
| 45 | + .base = _base, \ |
| 46 | + .mask = BIT(_ofs), \ |
| 47 | + } |
| 48 | + |
| 49 | +struct ccu_rst_info { |
| 50 | + unsigned int base; |
| 51 | + unsigned int mask; |
| 52 | +}; |
| 53 | + |
| 54 | +/* |
| 55 | + * Each AXI-bus clock divider is equipped with the corresponding clock-consumer |
| 56 | + * domain reset (it's self-deasserted reset control). |
| 57 | + */ |
| 58 | +static const struct ccu_rst_info axi_rst_info[] = { |
| 59 | + [CCU_AXI_MAIN_RST] = CCU_RST_TRIG(CCU_AXI_MAIN_BASE, 1), |
| 60 | + [CCU_AXI_DDR_RST] = CCU_RST_TRIG(CCU_AXI_DDR_BASE, 1), |
| 61 | + [CCU_AXI_SATA_RST] = CCU_RST_TRIG(CCU_AXI_SATA_BASE, 1), |
| 62 | + [CCU_AXI_GMAC0_RST] = CCU_RST_TRIG(CCU_AXI_GMAC0_BASE, 1), |
| 63 | + [CCU_AXI_GMAC1_RST] = CCU_RST_TRIG(CCU_AXI_GMAC1_BASE, 1), |
| 64 | + [CCU_AXI_XGMAC_RST] = CCU_RST_TRIG(CCU_AXI_XGMAC_BASE, 1), |
| 65 | + [CCU_AXI_PCIE_M_RST] = CCU_RST_TRIG(CCU_AXI_PCIE_M_BASE, 1), |
| 66 | + [CCU_AXI_PCIE_S_RST] = CCU_RST_TRIG(CCU_AXI_PCIE_S_BASE, 1), |
| 67 | + [CCU_AXI_USB_RST] = CCU_RST_TRIG(CCU_AXI_USB_BASE, 1), |
| 68 | + [CCU_AXI_HWA_RST] = CCU_RST_TRIG(CCU_AXI_HWA_BASE, 1), |
| 69 | + [CCU_AXI_SRAM_RST] = CCU_RST_TRIG(CCU_AXI_SRAM_BASE, 1), |
| 70 | +}; |
| 71 | + |
| 72 | +/* |
| 73 | + * SATA reference clock domain and APB-bus domain are connected with the |
| 74 | + * sefl-deasserted reset control, which can be activated via the corresponding |
| 75 | + * clock divider register. DDR and PCIe sub-domains can be reset with directly |
| 76 | + * controlled reset signals. Resetting the DDR controller though won't end up |
| 77 | + * well while the Linux kernel is working. |
| 78 | + */ |
| 79 | +static const struct ccu_rst_info sys_rst_info[] = { |
| 80 | + [CCU_SYS_SATA_REF_RST] = CCU_RST_TRIG(CCU_SYS_SATA_REF_BASE, 1), |
| 81 | + [CCU_SYS_APB_RST] = CCU_RST_TRIG(CCU_SYS_APB_BASE, 1), |
| 82 | +}; |
| 83 | + |
| 84 | +static int ccu_rst_reset(struct reset_controller_dev *rcdev, unsigned long idx) |
| 85 | +{ |
| 86 | + struct ccu_rst *rst = to_ccu_rst(rcdev); |
| 87 | + const struct ccu_rst_info *info = &rst->rsts_info[idx]; |
| 88 | + |
| 89 | + regmap_update_bits(rst->sys_regs, info->base, info->mask, info->mask); |
| 90 | + |
| 91 | + /* The next delay must be enough to cover all the resets. */ |
| 92 | + udelay(CCU_RST_DELAY_US); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +static const struct reset_control_ops ccu_rst_ops = { |
| 98 | + .reset = ccu_rst_reset, |
| 99 | +}; |
| 100 | + |
| 101 | +struct ccu_rst *ccu_rst_hw_register(const struct ccu_rst_init_data *rst_init) |
| 102 | +{ |
| 103 | + struct ccu_rst *rst; |
| 104 | + int ret; |
| 105 | + |
| 106 | + if (!rst_init) |
| 107 | + return ERR_PTR(-EINVAL); |
| 108 | + |
| 109 | + rst = kzalloc(sizeof(*rst), GFP_KERNEL); |
| 110 | + if (!rst) |
| 111 | + return ERR_PTR(-ENOMEM); |
| 112 | + |
| 113 | + rst->sys_regs = rst_init->sys_regs; |
| 114 | + if (of_device_is_compatible(rst_init->np, "baikal,bt1-ccu-axi")) { |
| 115 | + rst->rcdev.nr_resets = ARRAY_SIZE(axi_rst_info); |
| 116 | + rst->rsts_info = axi_rst_info; |
| 117 | + } else if (of_device_is_compatible(rst_init->np, "baikal,bt1-ccu-sys")) { |
| 118 | + rst->rcdev.nr_resets = ARRAY_SIZE(sys_rst_info); |
| 119 | + rst->rsts_info = sys_rst_info; |
| 120 | + } else { |
| 121 | + pr_err("Incompatible DT node '%s' specified\n", |
| 122 | + of_node_full_name(rst_init->np)); |
| 123 | + ret = -EINVAL; |
| 124 | + goto err_kfree_rst; |
| 125 | + } |
| 126 | + |
| 127 | + rst->rcdev.owner = THIS_MODULE; |
| 128 | + rst->rcdev.ops = &ccu_rst_ops; |
| 129 | + rst->rcdev.of_node = rst_init->np; |
| 130 | + |
| 131 | + ret = reset_controller_register(&rst->rcdev); |
| 132 | + if (ret) { |
| 133 | + pr_err("Couldn't register '%s' reset controller\n", |
| 134 | + of_node_full_name(rst_init->np)); |
| 135 | + goto err_kfree_rst; |
| 136 | + } |
| 137 | + |
| 138 | + return rst; |
| 139 | + |
| 140 | +err_kfree_rst: |
| 141 | + kfree(rst); |
| 142 | + |
| 143 | + return ERR_PTR(ret); |
| 144 | +} |
| 145 | + |
| 146 | +void ccu_rst_hw_unregister(struct ccu_rst *rst) |
| 147 | +{ |
| 148 | + reset_controller_unregister(&rst->rcdev); |
| 149 | + |
| 150 | + kfree(rst); |
| 151 | +} |
0 commit comments