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 		of_node_put(trng);
85 		return;
86 	}
87 
88 	dev->trng_base = of_iomap(trng, 0);
89 	of_node_put(trng);
90 	if (!dev->trng_base)
91 		goto err_out;
92 
93 	rng = kzalloc(sizeof(*rng), GFP_KERNEL);
94 	if (!rng)
95 		goto err_out;
96 
97 	rng->name = KBUILD_MODNAME;
98 	rng->data_present = ppc4xx_trng_data_present;
99 	rng->data_read = ppc4xx_trng_data_read;
100 	rng->priv = (unsigned long) dev;
101 	core_dev->trng = rng;
102 	ppc4xx_trng_enable(dev, true);
103 	out_le32(dev->trng_base + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
104 	err = devm_hwrng_register(core_dev->device, core_dev->trng);
105 	if (err) {
106 		ppc4xx_trng_enable(dev, false);
107 		dev_err(core_dev->device, "failed to register hwrng (%d).\n",
108 			err);
109 		goto err_out;
110 	}
111 	return;
112 
113 err_out:
114 	of_node_put(trng);
115 	iounmap(dev->trng_base);
116 	kfree(rng);
117 	dev->trng_base = NULL;
118 	core_dev->trng = NULL;
119 }
120 
121 void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev)
122 {
123 	if (core_dev && core_dev->trng) {
124 		struct crypto4xx_device *dev = core_dev->dev;
125 
126 		devm_hwrng_unregister(core_dev->device, core_dev->trng);
127 		ppc4xx_trng_enable(dev, false);
128 		iounmap(dev->trng_base);
129 		kfree(core_dev->trng);
130 	}
131 }
132 
133 MODULE_ALIAS("ppc4xx_rng");
134