^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Hardware Random Number Generator support for Cavium Networks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Octeon processor family.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2009 Cavium Networks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/octeon/octeon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/octeon/cvmx-rnm-defs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct octeon_rng {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct hwrng ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void __iomem *control_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void __iomem *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int octeon_rng_init(struct hwrng *rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) union cvmx_rnm_ctl_status ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ctl.u64 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ctl.s.ent_en = 1; /* Enable the entropy source. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ctl.s.rng_en = 1; /* Enable the RNG hardware. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) cvmx_write_csr((__force u64)p->control_status, ctl.u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static void octeon_rng_cleanup(struct hwrng *rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) union cvmx_rnm_ctl_status ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ctl.u64 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Disable everything. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) cvmx_write_csr((__force u64)p->control_status, ctl.u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int octeon_rng_data_read(struct hwrng *rng, u32 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *data = cvmx_read64_uint32((__force u64)p->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int octeon_rng_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct resource *res_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct resource *res_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct octeon_rng *rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct hwrng ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .name = "octeon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .init = octeon_rng_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .cleanup = octeon_rng_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .data_read = octeon_rng_data_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) res_ports = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!res_ports)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) res_result = platform_get_resource(pdev, IORESOURCE_MEM, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!res_result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) rng->control_status = devm_ioremap(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) res_ports->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!rng->control_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) rng->result = devm_ioremap(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) res_result->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!rng->result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) rng->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) platform_set_drvdata(pdev, &rng->ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = devm_hwrng_register(&pdev->dev, &rng->ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dev_info(&pdev->dev, "Octeon Random Number Generator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static struct platform_driver octeon_rng_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .name = "octeon_rng",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .probe = octeon_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(octeon_rng_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MODULE_AUTHOR("David Daney");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MODULE_LICENSE("GPL");