1 // SPDX-License-Identifier: GPL-2.0
3 * Hardware Random Number Generator support.
4 * Cavium Thunder, Marvell OcteonTx/Tx2 processor families.
6 * Copyright (C) 2016 Cavium, Inc.
9 #include <linux/hw_random.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/pci_ids.h>
15 #define THUNDERX_RNM_ENT_EN 0x1
16 #define THUNDERX_RNM_RNG_EN 0x2
18 struct cavium_rng_pf
{
19 void __iomem
*control_status
;
22 /* Enable the RNG hardware and activate the VF */
23 static int cavium_rng_probe(struct pci_dev
*pdev
,
24 const struct pci_device_id
*id
)
26 struct cavium_rng_pf
*rng
;
29 rng
= devm_kzalloc(&pdev
->dev
, sizeof(*rng
), GFP_KERNEL
);
33 /*Map the RNG control */
34 rng
->control_status
= pcim_iomap(pdev
, 0, 0);
35 if (!rng
->control_status
) {
37 "Error iomap failed retrieving control_status.\n");
41 /* Enable the RNG hardware and entropy source */
42 writeq(THUNDERX_RNM_RNG_EN
| THUNDERX_RNM_ENT_EN
,
45 pci_set_drvdata(pdev
, rng
);
47 /* Enable the Cavium RNG as a VF */
48 iov_err
= pci_enable_sriov(pdev
, 1);
50 /* Disable the RNG hardware and entropy source */
51 writeq(0, rng
->control_status
);
53 "Error initializing RNG virtual function,(%i).\n",
61 /* Disable VF and RNG Hardware */
62 static void cavium_rng_remove(struct pci_dev
*pdev
)
64 struct cavium_rng_pf
*rng
;
66 rng
= pci_get_drvdata(pdev
);
69 pci_disable_sriov(pdev
);
71 /* Disable the RNG hardware and entropy source */
72 writeq(0, rng
->control_status
);
75 static const struct pci_device_id cavium_rng_pf_id_table
[] = {
76 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, 0xa018), 0, 0, 0}, /* Thunder RNM */
80 MODULE_DEVICE_TABLE(pci
, cavium_rng_pf_id_table
);
82 static struct pci_driver cavium_rng_pf_driver
= {
83 .name
= "cavium_rng_pf",
84 .id_table
= cavium_rng_pf_id_table
,
85 .probe
= cavium_rng_probe
,
86 .remove
= cavium_rng_remove
,
89 module_pci_driver(cavium_rng_pf_driver
);
90 MODULE_AUTHOR("Omer Khaliq <okhaliq@caviumnetworks.com>");
91 MODULE_DESCRIPTION("Cavium ThunderX Random Number Generator support");
92 MODULE_LICENSE("GPL v2");