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_read(struct hwrng *rng, void *buf, size_t max, 42 bool wait) 43 { 44 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); 45 u64 *data = buf; 46 u32 t; 47 unsigned int timeout = RNG_TIMEOUT; 48 49 do { 50 t = readl(priv->base + RNGRCNT) & RCNT_MASK; 51 if (t == 64) { 52 /* TRNG value comes through the seed registers */ 53 *data = ((u64)readl(priv->base + RNGSEED2) << 32) + 54 readl(priv->base + RNGSEED1); 55 return 8; 56 } 57 } while (wait && --timeout); 58 59 return -EIO; 60 } 61 62 static int pic32_rng_probe(struct platform_device *pdev) 63 { 64 struct pic32_rng *priv; 65 struct clk *clk; 66 u32 v; 67 int ret; 68 69 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 70 if (!priv) 71 return -ENOMEM; 72 73 priv->base = devm_platform_ioremap_resource(pdev, 0); 74 if (IS_ERR(priv->base)) 75 return PTR_ERR(priv->base); 76 77 clk = devm_clk_get_enabled(&pdev->dev, NULL); 78 if (IS_ERR(clk)) 79 return PTR_ERR(clk); 80 81 /* enable TRNG in enhanced mode */ 82 v = TRNGEN | TRNGMOD; 83 writel(v, priv->base + RNGCON); 84 85 priv->rng.name = pdev->name; 86 priv->rng.read = pic32_rng_read; 87 88 ret = devm_hwrng_register(&pdev->dev, &priv->rng); 89 if (ret) 90 return ret; 91 92 platform_set_drvdata(pdev, priv); 93 94 return 0; 95 } 96 97 static int pic32_rng_remove(struct platform_device *pdev) 98 { 99 struct pic32_rng *rng = platform_get_drvdata(pdev); 100 101 writel(0, rng->base + RNGCON); 102 return 0; 103 } 104 105 static const struct of_device_id pic32_rng_of_match[] __maybe_unused = { 106 { .compatible = "microchip,pic32mzda-rng", }, 107 { /* sentinel */ } 108 }; 109 MODULE_DEVICE_TABLE(of, pic32_rng_of_match); 110 111 static struct platform_driver pic32_rng_driver = { 112 .probe = pic32_rng_probe, 113 .remove = pic32_rng_remove, 114 .driver = { 115 .name = "pic32-rng", 116 .of_match_table = of_match_ptr(pic32_rng_of_match), 117 }, 118 }; 119 120 module_platform_driver(pic32_rng_driver); 121 122 MODULE_LICENSE("GPL"); 123 MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>"); 124 MODULE_DESCRIPTION("Microchip PIC32 RNG Driver"); 125