Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Socfpga Reset Controller Driver |
| 4 | * |
| 5 | * Copyright 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de> |
| 6 | * |
| 7 | * based on |
| 8 | * Allwinner SoCs Reset Controller driver |
| 9 | * |
| 10 | * Copyright 2013 Maxime Ripard |
| 11 | * |
| 12 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <dm.h> |
Simon Goldschmidt | fc82466 | 2019-07-15 21:47:55 +0200 | [diff] [blame] | 17 | #include <dm/lists.h> |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 18 | #include <dm/of_access.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 19 | #include <env.h> |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 20 | #include <reset-uclass.h> |
Ley Foon Tan | 30f1775 | 2020-01-10 13:48:37 +0800 | [diff] [blame^] | 21 | #include <wait_bit.h> |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 22 | #include <linux/bitops.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/sizes.h> |
| 25 | |
| 26 | #define BANK_INCREMENT 4 |
| 27 | #define NR_BANKS 8 |
| 28 | |
| 29 | struct socfpga_reset_data { |
Simon Goldschmidt | 53a0cf5 | 2019-03-01 20:12:30 +0100 | [diff] [blame] | 30 | void __iomem *modrst_base; |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 31 | }; |
| 32 | |
Simon Goldschmidt | e568bb7 | 2019-03-01 20:12:32 +0100 | [diff] [blame] | 33 | /* |
| 34 | * For compatibility with Kernels that don't support peripheral reset, this |
| 35 | * driver can keep the old behaviour of not asserting peripheral reset before |
| 36 | * starting the OS and deasserting all peripheral resets (enabling all |
| 37 | * peripherals). |
| 38 | * |
| 39 | * For that, the reset driver checks the environment variable |
| 40 | * "socfpga_legacy_reset_compat". If this variable is '1', perihperals are not |
| 41 | * reset again once taken out of reset and all peripherals in 'permodrst' are |
| 42 | * taken out of reset before booting into the OS. |
| 43 | * Note that this should be required for gen5 systems only that are running |
| 44 | * Linux kernels without proper peripheral reset support for all drivers used. |
| 45 | */ |
| 46 | static bool socfpga_reset_keep_enabled(void) |
| 47 | { |
| 48 | #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT) |
| 49 | const char *env_str; |
| 50 | long val; |
| 51 | |
| 52 | env_str = env_get("socfpga_legacy_reset_compat"); |
| 53 | if (env_str) { |
| 54 | val = simple_strtol(env_str, NULL, 0); |
| 55 | if (val == 1) |
| 56 | return true; |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 63 | static int socfpga_reset_assert(struct reset_ctl *reset_ctl) |
| 64 | { |
| 65 | struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev); |
| 66 | int id = reset_ctl->id; |
| 67 | int reg_width = sizeof(u32); |
| 68 | int bank = id / (reg_width * BITS_PER_BYTE); |
| 69 | int offset = id % (reg_width * BITS_PER_BYTE); |
| 70 | |
Simon Goldschmidt | 53a0cf5 | 2019-03-01 20:12:30 +0100 | [diff] [blame] | 71 | setbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset)); |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int socfpga_reset_deassert(struct reset_ctl *reset_ctl) |
| 76 | { |
| 77 | struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev); |
| 78 | int id = reset_ctl->id; |
| 79 | int reg_width = sizeof(u32); |
| 80 | int bank = id / (reg_width * BITS_PER_BYTE); |
| 81 | int offset = id % (reg_width * BITS_PER_BYTE); |
| 82 | |
Simon Goldschmidt | 53a0cf5 | 2019-03-01 20:12:30 +0100 | [diff] [blame] | 83 | clrbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset)); |
Ley Foon Tan | 30f1775 | 2020-01-10 13:48:37 +0800 | [diff] [blame^] | 84 | |
| 85 | return wait_for_bit_le32(data->modrst_base + (bank * BANK_INCREMENT), |
| 86 | BIT(offset), |
| 87 | false, 500, false); |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int socfpga_reset_request(struct reset_ctl *reset_ctl) |
| 91 | { |
| 92 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, |
| 93 | reset_ctl, reset_ctl->dev, reset_ctl->id); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int socfpga_reset_free(struct reset_ctl *reset_ctl) |
| 99 | { |
| 100 | debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, reset_ctl, |
| 101 | reset_ctl->dev, reset_ctl->id); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static const struct reset_ops socfpga_reset_ops = { |
| 107 | .request = socfpga_reset_request, |
| 108 | .free = socfpga_reset_free, |
| 109 | .rst_assert = socfpga_reset_assert, |
| 110 | .rst_deassert = socfpga_reset_deassert, |
| 111 | }; |
| 112 | |
| 113 | static int socfpga_reset_probe(struct udevice *dev) |
| 114 | { |
| 115 | struct socfpga_reset_data *data = dev_get_priv(dev); |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 116 | u32 modrst_offset; |
Simon Goldschmidt | 53a0cf5 | 2019-03-01 20:12:30 +0100 | [diff] [blame] | 117 | void __iomem *membase; |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 118 | |
Simon Goldschmidt | 53a0cf5 | 2019-03-01 20:12:30 +0100 | [diff] [blame] | 119 | membase = devfdt_get_addr_ptr(dev); |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 120 | |
Simon Goldschmidt | e5291ad | 2019-05-09 22:11:59 +0200 | [diff] [blame] | 121 | modrst_offset = dev_read_u32_default(dev, "altr,modrst-offset", 0x10); |
Simon Goldschmidt | 53a0cf5 | 2019-03-01 20:12:30 +0100 | [diff] [blame] | 122 | data->modrst_base = membase + modrst_offset; |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Simon Goldschmidt | e568bb7 | 2019-03-01 20:12:32 +0100 | [diff] [blame] | 127 | static int socfpga_reset_remove(struct udevice *dev) |
| 128 | { |
| 129 | struct socfpga_reset_data *data = dev_get_priv(dev); |
| 130 | |
| 131 | if (socfpga_reset_keep_enabled()) { |
| 132 | puts("Deasserting all peripheral resets\n"); |
| 133 | writel(0, data->modrst_base + 4); |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Simon Goldschmidt | fc82466 | 2019-07-15 21:47:55 +0200 | [diff] [blame] | 139 | static int socfpga_reset_bind(struct udevice *dev) |
| 140 | { |
| 141 | int ret; |
| 142 | struct udevice *sys_child; |
| 143 | |
| 144 | /* |
| 145 | * The sysreset driver does not have a device node, so bind it here. |
| 146 | * Bind it to the node, too, so that it can get its base address. |
| 147 | */ |
| 148 | ret = device_bind_driver_to_node(dev, "socfpga_sysreset", "sysreset", |
| 149 | dev->node, &sys_child); |
| 150 | if (ret) |
| 151 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 156 | static const struct udevice_id socfpga_reset_match[] = { |
| 157 | { .compatible = "altr,rst-mgr" }, |
| 158 | { /* sentinel */ }, |
| 159 | }; |
| 160 | |
| 161 | U_BOOT_DRIVER(socfpga_reset) = { |
| 162 | .name = "socfpga-reset", |
| 163 | .id = UCLASS_RESET, |
| 164 | .of_match = socfpga_reset_match, |
Simon Goldschmidt | fc82466 | 2019-07-15 21:47:55 +0200 | [diff] [blame] | 165 | .bind = socfpga_reset_bind, |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 166 | .probe = socfpga_reset_probe, |
| 167 | .priv_auto_alloc_size = sizeof(struct socfpga_reset_data), |
| 168 | .ops = &socfpga_reset_ops, |
Simon Goldschmidt | e568bb7 | 2019-03-01 20:12:32 +0100 | [diff] [blame] | 169 | .remove = socfpga_reset_remove, |
| 170 | .flags = DM_FLAG_OS_PREPARE, |
Dinh Nguyen | 5427f5a | 2018-04-04 17:18:20 -0500 | [diff] [blame] | 171 | }; |