| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Random Number Generator driver for the Keystone SOC |
|---|
| 3 | 4 | * |
|---|
| 4 | | - * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com |
|---|
| 5 | + * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Authors: Sandeep Nair |
|---|
| 7 | 8 | * Vitaly Andrianov |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is free software; you can redistribute it and/or |
|---|
| 10 | | - * modify it under the terms of the GNU General Public License |
|---|
| 11 | | - * version 2 as published by the Free Software Foundation. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is distributed in the hope that it will be useful, but |
|---|
| 14 | | - * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | | - * General Public License for more details. |
|---|
| 17 | 9 | */ |
|---|
| 18 | 10 | |
|---|
| 19 | 11 | #include <linux/hw_random.h> |
|---|
| .. | .. |
|---|
| 29 | 21 | #include <linux/of.h> |
|---|
| 30 | 22 | #include <linux/of_address.h> |
|---|
| 31 | 23 | #include <linux/delay.h> |
|---|
| 24 | +#include <linux/timekeeping.h> |
|---|
| 32 | 25 | |
|---|
| 33 | 26 | #define SA_CMD_STATUS_OFS 0x8 |
|---|
| 34 | 27 | |
|---|
| .. | .. |
|---|
| 92 | 85 | struct hwrng rng; |
|---|
| 93 | 86 | struct clk *clk; |
|---|
| 94 | 87 | struct regmap *regmap_cfg; |
|---|
| 95 | | - struct trng_regs *reg_rng; |
|---|
| 88 | + struct trng_regs __iomem *reg_rng; |
|---|
| 89 | + u64 ready_ts; |
|---|
| 90 | + unsigned int refill_delay_ns; |
|---|
| 96 | 91 | }; |
|---|
| 92 | + |
|---|
| 93 | +static unsigned int cycles_to_ns(unsigned long clk_rate, unsigned int cycles) |
|---|
| 94 | +{ |
|---|
| 95 | + return DIV_ROUND_UP_ULL((TRNG_DEF_CLK_DIV_CYCLES + 1) * 1000000000ull * |
|---|
| 96 | + cycles, clk_rate); |
|---|
| 97 | +} |
|---|
| 98 | + |
|---|
| 99 | +static unsigned int startup_delay_ns(unsigned long clk_rate) |
|---|
| 100 | +{ |
|---|
| 101 | + if (!TRNG_DEF_STARTUP_CYCLES) |
|---|
| 102 | + return cycles_to_ns(clk_rate, BIT(24)); |
|---|
| 103 | + return cycles_to_ns(clk_rate, 256 * TRNG_DEF_STARTUP_CYCLES); |
|---|
| 104 | +} |
|---|
| 105 | + |
|---|
| 106 | +static unsigned int refill_delay_ns(unsigned long clk_rate) |
|---|
| 107 | +{ |
|---|
| 108 | + if (!TRNG_DEF_MAX_REFILL_CYCLES) |
|---|
| 109 | + return cycles_to_ns(clk_rate, BIT(24)); |
|---|
| 110 | + return cycles_to_ns(clk_rate, 256 * TRNG_DEF_MAX_REFILL_CYCLES); |
|---|
| 111 | +} |
|---|
| 97 | 112 | |
|---|
| 98 | 113 | static int ks_sa_rng_init(struct hwrng *rng) |
|---|
| 99 | 114 | { |
|---|
| 100 | 115 | u32 value; |
|---|
| 101 | 116 | struct device *dev = (struct device *)rng->priv; |
|---|
| 102 | 117 | struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev); |
|---|
| 118 | + unsigned long clk_rate = clk_get_rate(ks_sa_rng->clk); |
|---|
| 103 | 119 | |
|---|
| 104 | 120 | /* Enable RNG module */ |
|---|
| 105 | 121 | regmap_write_bits(ks_sa_rng->regmap_cfg, SA_CMD_STATUS_OFS, |
|---|
| .. | .. |
|---|
| 128 | 144 | value |= TRNG_CNTL_REG_TRNG_ENABLE; |
|---|
| 129 | 145 | writel(value, &ks_sa_rng->reg_rng->control); |
|---|
| 130 | 146 | |
|---|
| 147 | + ks_sa_rng->refill_delay_ns = refill_delay_ns(clk_rate); |
|---|
| 148 | + ks_sa_rng->ready_ts = ktime_get_ns() + |
|---|
| 149 | + startup_delay_ns(clk_rate); |
|---|
| 150 | + |
|---|
| 131 | 151 | return 0; |
|---|
| 132 | 152 | } |
|---|
| 133 | 153 | |
|---|
| .. | .. |
|---|
| 152 | 172 | data[1] = readl(&ks_sa_rng->reg_rng->output_h); |
|---|
| 153 | 173 | |
|---|
| 154 | 174 | writel(TRNG_INTACK_REG_READY, &ks_sa_rng->reg_rng->intack); |
|---|
| 175 | + ks_sa_rng->ready_ts = ktime_get_ns() + ks_sa_rng->refill_delay_ns; |
|---|
| 155 | 176 | |
|---|
| 156 | 177 | return sizeof(u32) * 2; |
|---|
| 157 | 178 | } |
|---|
| .. | .. |
|---|
| 160 | 181 | { |
|---|
| 161 | 182 | struct device *dev = (struct device *)rng->priv; |
|---|
| 162 | 183 | struct ks_sa_rng *ks_sa_rng = dev_get_drvdata(dev); |
|---|
| 184 | + u64 now = ktime_get_ns(); |
|---|
| 163 | 185 | |
|---|
| 164 | 186 | u32 ready; |
|---|
| 165 | 187 | int j; |
|---|
| 188 | + |
|---|
| 189 | + if (wait && now < ks_sa_rng->ready_ts) { |
|---|
| 190 | + /* Max delay expected here is 81920000 ns */ |
|---|
| 191 | + unsigned long min_delay = |
|---|
| 192 | + DIV_ROUND_UP((u32)(ks_sa_rng->ready_ts - now), 1000); |
|---|
| 193 | + |
|---|
| 194 | + usleep_range(min_delay, min_delay + SA_RNG_DATA_RETRY_DELAY); |
|---|
| 195 | + } |
|---|
| 166 | 196 | |
|---|
| 167 | 197 | for (j = 0; j < SA_MAX_RNG_DATA_RETRIES; j++) { |
|---|
| 168 | 198 | ready = readl(&ks_sa_rng->reg_rng->status); |
|---|
| .. | .. |
|---|
| 182 | 212 | struct ks_sa_rng *ks_sa_rng; |
|---|
| 183 | 213 | struct device *dev = &pdev->dev; |
|---|
| 184 | 214 | int ret; |
|---|
| 185 | | - struct resource *mem; |
|---|
| 186 | 215 | |
|---|
| 187 | 216 | ks_sa_rng = devm_kzalloc(dev, sizeof(*ks_sa_rng), GFP_KERNEL); |
|---|
| 188 | 217 | if (!ks_sa_rng) |
|---|
| .. | .. |
|---|
| 198 | 227 | }; |
|---|
| 199 | 228 | ks_sa_rng->rng.priv = (unsigned long)dev; |
|---|
| 200 | 229 | |
|---|
| 201 | | - mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 202 | | - ks_sa_rng->reg_rng = devm_ioremap_resource(dev, mem); |
|---|
| 230 | + ks_sa_rng->reg_rng = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 203 | 231 | if (IS_ERR(ks_sa_rng->reg_rng)) |
|---|
| 204 | 232 | return PTR_ERR(ks_sa_rng->reg_rng); |
|---|
| 205 | 233 | |
|---|