^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2006-2007 PA Semi, Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Maintained by: Olof Johansson <olof@lixom.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Driver for the PWRficient onchip rng
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define SDCRNG_CTL_REG 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define SDCRNG_CTL_FVLD_M 0x0000f000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define SDCRNG_CTL_FVLD_S 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define SDCRNG_CTL_KSZ 0x00000800
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define SDCRNG_CTL_RSRC_CRG 0x00000010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SDCRNG_CTL_RSRC_RRG 0x00000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define SDCRNG_CTL_CE 0x00000004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define SDCRNG_CTL_RE 0x00000002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define SDCRNG_CTL_DR 0x00000001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define SDCRNG_VAL_REG 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define MODULE_NAME "pasemi_rng"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int pasemi_rng_data_present(struct hwrng *rng, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) void __iomem *rng_regs = (void __iomem *)rng->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int data, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) for (i = 0; i < 20; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) data = (in_le32(rng_regs + SDCRNG_CTL_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) & SDCRNG_CTL_FVLD_M) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (data || !wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) udelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void __iomem *rng_regs = (void __iomem *)rng->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *data = in_le32(rng_regs + SDCRNG_VAL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int pasemi_rng_init(struct hwrng *rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void __iomem *rng_regs = (void __iomem *)rng->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u32 ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void pasemi_rng_cleanup(struct hwrng *rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) void __iomem *rng_regs = (void __iomem *)rng->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u32 ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) out_le32(rng_regs + SDCRNG_CTL_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static struct hwrng pasemi_rng = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .name = MODULE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .init = pasemi_rng_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .cleanup = pasemi_rng_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .data_present = pasemi_rng_data_present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .data_read = pasemi_rng_data_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int rng_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void __iomem *rng_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) rng_regs = devm_platform_ioremap_resource(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (IS_ERR(rng_regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return PTR_ERR(rng_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pasemi_rng.priv = (unsigned long)rng_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pr_info("Registering PA Semi RNG\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return devm_hwrng_register(&pdev->dev, &pasemi_rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static const struct of_device_id rng_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) { .compatible = "1682m-rng", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) { .compatible = "pasemi,pwrficient-rng", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) MODULE_DEVICE_TABLE(of, rng_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static struct platform_driver rng_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .name = "pasemi-rng",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .of_match_table = rng_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .probe = rng_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) module_platform_driver(rng_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");