xref: /linux/drivers/crypto/amcc/crypto4xx_trng.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * Generic PowerPC 44x RNG driver
3  *
4  * Copyright 2011 IBM Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; version 2 of the License.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <linux/hw_random.h>
16 #include <linux/delay.h>
17 #include <linux/of_address.h>
18 #include <linux/of_platform.h>
19 #include <linux/io.h>
20 
21 #include "crypto4xx_core.h"
22 #include "crypto4xx_trng.h"
23 #include "crypto4xx_reg_def.h"
24 
25 #define PPC4XX_TRNG_CTRL	0x0008
26 #define PPC4XX_TRNG_CTRL_DALM	0x20
27 #define PPC4XX_TRNG_STAT	0x0004
28 #define PPC4XX_TRNG_STAT_B	0x1
29 #define PPC4XX_TRNG_DATA	0x0000
30 
31 static int ppc4xx_trng_data_present(struct hwrng *rng, int wait)
32 {
33 	struct crypto4xx_device *dev = (void *)rng->priv;
34 	int busy, i, present = 0;
35 
36 	for (i = 0; i < 20; i++) {
37 		busy = (in_le32(dev->trng_base + PPC4XX_TRNG_STAT) &
38 			PPC4XX_TRNG_STAT_B);
39 		if (!busy || !wait) {
40 			present = 1;
41 			break;
42 		}
43 		udelay(10);
44 	}
45 	return present;
46 }
47 
48 static int ppc4xx_trng_data_read(struct hwrng *rng, u32 *data)
49 {
50 	struct crypto4xx_device *dev = (void *)rng->priv;
51 	*data = in_le32(dev->trng_base + PPC4XX_TRNG_DATA);
52 	return 4;
53 }
54 
55 static void ppc4xx_trng_enable(struct crypto4xx_device *dev, bool enable)
56 {
57 	u32 device_ctrl;
58 
59 	device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
60 	if (enable)
61 		device_ctrl |= PPC4XX_TRNG_EN;
62 	else
63 		device_ctrl &= ~PPC4XX_TRNG_EN;
64 	writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
65 }
66 
67 static const struct of_device_id ppc4xx_trng_match[] = {
68 	{ .compatible = "ppc4xx-rng", },
69 	{ .compatible = "amcc,ppc460ex-rng", },
70 	{ .compatible = "amcc,ppc440epx-rng", },
71 	{},
72 };
73 
74 void ppc4xx_trng_probe(struct crypto4xx_core_device *core_dev)
75 {
76 	struct crypto4xx_device *dev = core_dev->dev;
77 	struct device_node *trng = NULL;
78 	struct hwrng *rng = NULL;
79 	int err;
80 
81 	/* Find the TRNG device node and map it */
82 	trng = of_find_matching_node(NULL, ppc4xx_trng_match);
83 	if (!trng || !of_device_is_available(trng))
84 		return;
85 
86 	dev->trng_base = of_iomap(trng, 0);
87 	of_node_put(trng);
88 	if (!dev->trng_base)
89 		goto err_out;
90 
91 	rng = kzalloc(sizeof(*rng), GFP_KERNEL);
92 	if (!rng)
93 		goto err_out;
94 
95 	rng->name = KBUILD_MODNAME;
96 	rng->data_present = ppc4xx_trng_data_present;
97 	rng->data_read = ppc4xx_trng_data_read;
98 	rng->priv = (unsigned long) dev;
99 	core_dev->trng = rng;
100 	ppc4xx_trng_enable(dev, true);
101 	out_le32(dev->trng_base + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
102 	err = devm_hwrng_register(core_dev->device, core_dev->trng);
103 	if (err) {
104 		ppc4xx_trng_enable(dev, false);
105 		dev_err(core_dev->device, "failed to register hwrng (%d).\n",
106 			err);
107 		goto err_out;
108 	}
109 	return;
110 
111 err_out:
112 	of_node_put(trng);
113 	iounmap(dev->trng_base);
114 	kfree(rng);
115 	dev->trng_base = NULL;
116 	core_dev->trng = NULL;
117 }
118 
119 void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev)
120 {
121 	if (core_dev && core_dev->trng) {
122 		struct crypto4xx_device *dev = core_dev->dev;
123 
124 		devm_hwrng_unregister(core_dev->device, core_dev->trng);
125 		ppc4xx_trng_enable(dev, false);
126 		iounmap(dev->trng_base);
127 		kfree(core_dev->trng);
128 	}
129 }
130 
131 MODULE_ALIAS("ppc4xx_rng");
132