1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * JZ47xx ECC common code
4  *
5  * Copyright (c) 2015 Imagination Technologies
6  * Author: Alex Smith <alex.smith@imgtec.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 
15 #include "ingenic_ecc.h"
16 
17 /**
18  * ingenic_ecc_calculate() - calculate ECC for a data buffer
19  * @ecc: ECC device.
20  * @params: ECC parameters.
21  * @buf: input buffer with raw data.
22  * @ecc_code: output buffer with ECC.
23  *
24  * Return: 0 on success, -ETIMEDOUT if timed out while waiting for ECC
25  * controller.
26  */
27 int ingenic_ecc_calculate(struct ingenic_ecc *ecc,
28 			  struct ingenic_ecc_params *params,
29 			  const u8 *buf, u8 *ecc_code)
30 {
31 	return ecc->ops->calculate(ecc, params, buf, ecc_code);
32 }
33 EXPORT_SYMBOL(ingenic_ecc_calculate);
34 
35 /**
36  * ingenic_ecc_correct() - detect and correct bit errors
37  * @ecc: ECC device.
38  * @params: ECC parameters.
39  * @buf: raw data read from the chip.
40  * @ecc_code: ECC read from the chip.
41  *
42  * Given the raw data and the ECC read from the NAND device, detects and
43  * corrects errors in the data.
44  *
45  * Return: the number of bit errors corrected, -EBADMSG if there are too many
46  * errors to correct or -ETIMEDOUT if we timed out waiting for the controller.
47  */
48 int ingenic_ecc_correct(struct ingenic_ecc *ecc,
49 			struct ingenic_ecc_params *params,
50 			u8 *buf, u8 *ecc_code)
51 {
52 	return ecc->ops->correct(ecc, params, buf, ecc_code);
53 }
54 EXPORT_SYMBOL(ingenic_ecc_correct);
55 
56 /**
57  * ingenic_ecc_get() - get the ECC controller device
58  * @np: ECC device tree node.
59  *
60  * Gets the ECC controller device from the specified device tree node. The
61  * device must be released with ingenic_ecc_release() when it is no longer being
62  * used.
63  *
64  * Return: a pointer to ingenic_ecc, errors are encoded into the pointer.
65  * PTR_ERR(-EPROBE_DEFER) if the device hasn't been initialised yet.
66  */
67 static struct ingenic_ecc *ingenic_ecc_get(struct device_node *np)
68 {
69 	struct platform_device *pdev;
70 	struct ingenic_ecc *ecc;
71 
72 	pdev = of_find_device_by_node(np);
73 	if (!pdev || !platform_get_drvdata(pdev))
74 		return ERR_PTR(-EPROBE_DEFER);
75 
76 	get_device(&pdev->dev);
77 
78 	ecc = platform_get_drvdata(pdev);
79 	clk_prepare_enable(ecc->clk);
80 
81 	return ecc;
82 }
83 
84 /**
85  * of_ingenic_ecc_get() - get the ECC controller from a DT node
86  * @of_node: the node that contains an ecc-engine property.
87  *
88  * Get the ecc-engine property from the given device tree
89  * node and pass it to ingenic_ecc_get to do the work.
90  *
91  * Return: a pointer to ingenic_ecc, errors are encoded into the pointer.
92  * PTR_ERR(-EPROBE_DEFER) if the device hasn't been initialised yet.
93  */
94 struct ingenic_ecc *of_ingenic_ecc_get(struct device_node *of_node)
95 {
96 	struct ingenic_ecc *ecc = NULL;
97 	struct device_node *np;
98 
99 	np = of_parse_phandle(of_node, "ecc-engine", 0);
100 
101 	/*
102 	 * If the ecc-engine property is not found, check for the deprecated
103 	 * ingenic,bch-controller property
104 	 */
105 	if (!np)
106 		np = of_parse_phandle(of_node, "ingenic,bch-controller", 0);
107 
108 	if (np) {
109 		ecc = ingenic_ecc_get(np);
110 		of_node_put(np);
111 	}
112 	return ecc;
113 }
114 EXPORT_SYMBOL(of_ingenic_ecc_get);
115 
116 /**
117  * ingenic_ecc_release() - release the ECC controller device
118  * @ecc: ECC device.
119  */
120 void ingenic_ecc_release(struct ingenic_ecc *ecc)
121 {
122 	clk_disable_unprepare(ecc->clk);
123 	put_device(ecc->dev);
124 }
125 EXPORT_SYMBOL(ingenic_ecc_release);
126 
127 int ingenic_ecc_probe(struct platform_device *pdev)
128 {
129 	struct device *dev = &pdev->dev;
130 	struct ingenic_ecc *ecc;
131 	struct resource *res;
132 
133 	ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
134 	if (!ecc)
135 		return -ENOMEM;
136 
137 	ecc->ops = device_get_match_data(dev);
138 	if (!ecc->ops)
139 		return -EINVAL;
140 
141 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 	ecc->base = devm_ioremap_resource(dev, res);
143 	if (IS_ERR(ecc->base))
144 		return PTR_ERR(ecc->base);
145 
146 	ecc->ops->disable(ecc);
147 
148 	ecc->clk = devm_clk_get(dev, NULL);
149 	if (IS_ERR(ecc->clk)) {
150 		dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk));
151 		return PTR_ERR(ecc->clk);
152 	}
153 
154 	mutex_init(&ecc->lock);
155 
156 	ecc->dev = dev;
157 	platform_set_drvdata(pdev, ecc);
158 
159 	return 0;
160 }
161 EXPORT_SYMBOL(ingenic_ecc_probe);
162 
163 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
164 MODULE_AUTHOR("Harvey Hunt <harveyhuntnexus@gmail.com>");
165 MODULE_DESCRIPTION("Ingenic ECC common driver");
166 MODULE_LICENSE("GPL v2");
167