1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PIC32 RNG driver 4 * 5 * Joshua Henderson <joshua.henderson@microchip.com> 6 * Copyright (C) 2016 Microchip Technology Inc. All rights reserved. 7 */ 8 9 #include <linux/clk.h> 10 #include <linux/clkdev.h> 11 #include <linux/err.h> 12 #include <linux/hw_random.h> 13 #include <linux/io.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_device.h> 18 #include <linux/platform_device.h> 19 #include <linux/slab.h> 20 21 #define RNGCON 0x04 22 #define TRNGEN BIT(8) 23 #define TRNGMOD BIT(11) 24 #define RNGSEED1 0x18 25 #define RNGSEED2 0x1C 26 #define RNGRCNT 0x20 27 #define RCNT_MASK 0x7F 28 29 struct pic32_rng { 30 void __iomem *base; 31 struct hwrng rng; 32 }; 33 34 /* 35 * The TRNG can generate up to 24Mbps. This is a timeout that should be safe 36 * enough given the instructions in the loop and that the TRNG may not always 37 * be at maximum rate. 38 */ 39 #define RNG_TIMEOUT 500 40 41 static int pic32_rng_init(struct hwrng *rng) 42 { 43 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); 44 45 /* enable TRNG in enhanced mode */ 46 writel(TRNGEN | TRNGMOD, priv->base + RNGCON); 47 return 0; 48 } 49 50 static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max, 51 bool wait) 52 { 53 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); 54 u64 *data = buf; 55 u32 t; 56 unsigned int timeout = RNG_TIMEOUT; 57 58 do { 59 t = readl(priv->base + RNGRCNT) & RCNT_MASK; 60 if (t == 64) { 61 /* TRNG value comes through the seed registers */ 62 *data = ((u64)readl(priv->base + RNGSEED2) << 32) + 63 readl(priv->base + RNGSEED1); 64 return 8; 65 } 66 } while (wait && --timeout); 67 68 return -EIO; 69 } 70 71 static void pic32_rng_cleanup(struct hwrng *rng) 72 { 73 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); 74 75 writel(0, priv->base + RNGCON); 76 } 77 78 static int pic32_rng_probe(struct platform_device *pdev) 79 { 80 struct pic32_rng *priv; 81 struct clk *clk; 82 83 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 84 if (!priv) 85 return -ENOMEM; 86 87 priv->base = devm_platform_ioremap_resource(pdev, 0); 88 if (IS_ERR(priv->base)) 89 return PTR_ERR(priv->base); 90 91 clk = devm_clk_get_enabled(&pdev->dev, NULL); 92 if (IS_ERR(clk)) 93 return PTR_ERR(clk); 94 95 priv->rng.name = pdev->name; 96 priv->rng.init = pic32_rng_init; 97 priv->rng.read = pic32_rng_read; 98 priv->rng.cleanup = pic32_rng_cleanup; 99 100 return devm_hwrng_register(&pdev->dev, &priv->rng); 101 } 102 103 static const struct of_device_id pic32_rng_of_match[] __maybe_unused = { 104 { .compatible = "microchip,pic32mzda-rng", }, 105 { /* sentinel */ } 106 }; 107 MODULE_DEVICE_TABLE(of, pic32_rng_of_match); 108 109 static struct platform_driver pic32_rng_driver = { 110 .probe = pic32_rng_probe, 111 .driver = { 112 .name = "pic32-rng", 113 .of_match_table = of_match_ptr(pic32_rng_of_match), 114 }, 115 }; 116 117 module_platform_driver(pic32_rng_driver); 118 119 MODULE_LICENSE("GPL"); 120 MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>"); 121 MODULE_DESCRIPTION("Microchip PIC32 RNG Driver"); 122