1 /* 2 * omap3-rom-rng.c - RNG driver for TI OMAP3 CPU family 3 * 4 * Copyright (C) 2009 Nokia Corporation 5 * Author: Juha Yrjola <juha.yrjola@solidboot.com> 6 * 7 * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com> 8 * 9 * This file is licensed under the terms of the GNU General Public 10 * License version 2. This program is licensed "as is" without any 11 * warranty of any kind, whether express or implied. 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/module.h> 17 #include <linux/init.h> 18 #include <linux/random.h> 19 #include <linux/hw_random.h> 20 #include <linux/workqueue.h> 21 #include <linux/clk.h> 22 #include <linux/err.h> 23 #include <linux/platform_device.h> 24 25 #define RNG_RESET 0x01 26 #define RNG_GEN_PRNG_HW_INIT 0x02 27 #define RNG_GEN_HW 0x08 28 29 /* param1: ptr, param2: count, param3: flag */ 30 static u32 (*omap3_rom_rng_call)(u32, u32, u32); 31 32 static struct delayed_work idle_work; 33 static int rng_idle; 34 static struct clk *rng_clk; 35 36 static void omap3_rom_rng_idle(struct work_struct *work) 37 { 38 int r; 39 40 r = omap3_rom_rng_call(0, 0, RNG_RESET); 41 if (r != 0) { 42 pr_err("reset failed: %d\n", r); 43 return; 44 } 45 clk_disable_unprepare(rng_clk); 46 rng_idle = 1; 47 } 48 49 static int omap3_rom_rng_get_random(void *buf, unsigned int count) 50 { 51 u32 r; 52 u32 ptr; 53 54 cancel_delayed_work_sync(&idle_work); 55 if (rng_idle) { 56 clk_prepare_enable(rng_clk); 57 r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT); 58 if (r != 0) { 59 clk_disable_unprepare(rng_clk); 60 pr_err("HW init failed: %d\n", r); 61 return -EIO; 62 } 63 rng_idle = 0; 64 } 65 66 ptr = virt_to_phys(buf); 67 r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW); 68 schedule_delayed_work(&idle_work, msecs_to_jiffies(500)); 69 if (r != 0) 70 return -EINVAL; 71 return 0; 72 } 73 74 static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w) 75 { 76 int r; 77 78 r = omap3_rom_rng_get_random(data, 4); 79 if (r < 0) 80 return r; 81 return 4; 82 } 83 84 static struct hwrng omap3_rom_rng_ops = { 85 .name = "omap3-rom", 86 .read = omap3_rom_rng_read, 87 }; 88 89 static int omap3_rom_rng_probe(struct platform_device *pdev) 90 { 91 pr_info("initializing\n"); 92 93 omap3_rom_rng_call = pdev->dev.platform_data; 94 if (!omap3_rom_rng_call) { 95 pr_err("omap3_rom_rng_call is NULL\n"); 96 return -EINVAL; 97 } 98 99 INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle); 100 rng_clk = devm_clk_get(&pdev->dev, "ick"); 101 if (IS_ERR(rng_clk)) { 102 pr_err("unable to get RNG clock\n"); 103 return PTR_ERR(rng_clk); 104 } 105 106 /* Leave the RNG in reset state. */ 107 clk_prepare_enable(rng_clk); 108 omap3_rom_rng_idle(0); 109 110 return hwrng_register(&omap3_rom_rng_ops); 111 } 112 113 static int omap3_rom_rng_remove(struct platform_device *pdev) 114 { 115 cancel_delayed_work_sync(&idle_work); 116 hwrng_unregister(&omap3_rom_rng_ops); 117 clk_disable_unprepare(rng_clk); 118 return 0; 119 } 120 121 static struct platform_driver omap3_rom_rng_driver = { 122 .driver = { 123 .name = "omap3-rom-rng", 124 }, 125 .probe = omap3_rom_rng_probe, 126 .remove = omap3_rom_rng_remove, 127 }; 128 129 module_platform_driver(omap3_rom_rng_driver); 130 131 MODULE_ALIAS("platform:omap3-rom-rng"); 132 MODULE_AUTHOR("Juha Yrjola"); 133 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); 134 MODULE_LICENSE("GPL"); 135