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 PRNGEN BIT(9) 24 #define PRNGCONT BIT(10) 25 #define TRNGMOD BIT(11) 26 #define SEEDLOAD BIT(12) 27 #define RNGPOLY1 0x08 28 #define RNGPOLY2 0x0C 29 #define RNGNUMGEN1 0x10 30 #define RNGNUMGEN2 0x14 31 #define RNGSEED1 0x18 32 #define RNGSEED2 0x1C 33 #define RNGRCNT 0x20 34 #define RCNT_MASK 0x7F 35 36 struct pic32_rng { 37 void __iomem *base; 38 struct hwrng rng; 39 }; 40 41 /* 42 * The TRNG can generate up to 24Mbps. This is a timeout that should be safe 43 * enough given the instructions in the loop and that the TRNG may not always 44 * be at maximum rate. 45 */ 46 #define RNG_TIMEOUT 500 47 48 static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max, 49 bool wait) 50 { 51 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); 52 u64 *data = buf; 53 u32 t; 54 unsigned int timeout = RNG_TIMEOUT; 55 56 do { 57 t = readl(priv->base + RNGRCNT) & RCNT_MASK; 58 if (t == 64) { 59 /* TRNG value comes through the seed registers */ 60 *data = ((u64)readl(priv->base + RNGSEED2) << 32) + 61 readl(priv->base + RNGSEED1); 62 return 8; 63 } 64 } while (wait && --timeout); 65 66 return -EIO; 67 } 68 69 static int pic32_rng_probe(struct platform_device *pdev) 70 { 71 struct pic32_rng *priv; 72 struct clk *clk; 73 u32 v; 74 int ret; 75 76 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 77 if (!priv) 78 return -ENOMEM; 79 80 priv->base = devm_platform_ioremap_resource(pdev, 0); 81 if (IS_ERR(priv->base)) 82 return PTR_ERR(priv->base); 83 84 clk = devm_clk_get_enabled(&pdev->dev, NULL); 85 if (IS_ERR(clk)) 86 return PTR_ERR(clk); 87 88 /* enable TRNG in enhanced mode */ 89 v = TRNGEN | TRNGMOD; 90 writel(v, priv->base + RNGCON); 91 92 priv->rng.name = pdev->name; 93 priv->rng.read = pic32_rng_read; 94 95 ret = devm_hwrng_register(&pdev->dev, &priv->rng); 96 if (ret) 97 return ret; 98 99 platform_set_drvdata(pdev, priv); 100 101 return 0; 102 } 103 104 static int pic32_rng_remove(struct platform_device *pdev) 105 { 106 struct pic32_rng *rng = platform_get_drvdata(pdev); 107 108 writel(0, rng->base + RNGCON); 109 return 0; 110 } 111 112 static const struct of_device_id pic32_rng_of_match[] __maybe_unused = { 113 { .compatible = "microchip,pic32mzda-rng", }, 114 { /* sentinel */ } 115 }; 116 MODULE_DEVICE_TABLE(of, pic32_rng_of_match); 117 118 static struct platform_driver pic32_rng_driver = { 119 .probe = pic32_rng_probe, 120 .remove = pic32_rng_remove, 121 .driver = { 122 .name = "pic32-rng", 123 .of_match_table = of_match_ptr(pic32_rng_of_match), 124 }, 125 }; 126 127 module_platform_driver(pic32_rng_driver); 128 129 MODULE_LICENSE("GPL"); 130 MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>"); 131 MODULE_DESCRIPTION("Microchip PIC32 RNG Driver"); 132