115de8c6eSPaul Cercueil // SPDX-License-Identifier: GPL-2.0
215de8c6eSPaul Cercueil /*
315de8c6eSPaul Cercueil * JZ47xx ECC common code
415de8c6eSPaul Cercueil *
515de8c6eSPaul Cercueil * Copyright (c) 2015 Imagination Technologies
615de8c6eSPaul Cercueil * Author: Alex Smith <alex.smith@imgtec.com>
715de8c6eSPaul Cercueil */
815de8c6eSPaul Cercueil
915de8c6eSPaul Cercueil #include <linux/clk.h>
1015de8c6eSPaul Cercueil #include <linux/init.h>
1115de8c6eSPaul Cercueil #include <linux/module.h>
12*c2fc6b69SRob Herring #include <linux/of.h>
1315de8c6eSPaul Cercueil #include <linux/of_platform.h>
1415de8c6eSPaul Cercueil #include <linux/platform_device.h>
1515de8c6eSPaul Cercueil
1615de8c6eSPaul Cercueil #include "ingenic_ecc.h"
1715de8c6eSPaul Cercueil
1815de8c6eSPaul Cercueil /**
1915de8c6eSPaul Cercueil * ingenic_ecc_calculate() - calculate ECC for a data buffer
2015de8c6eSPaul Cercueil * @ecc: ECC device.
2115de8c6eSPaul Cercueil * @params: ECC parameters.
2215de8c6eSPaul Cercueil * @buf: input buffer with raw data.
2315de8c6eSPaul Cercueil * @ecc_code: output buffer with ECC.
2415de8c6eSPaul Cercueil *
2515de8c6eSPaul Cercueil * Return: 0 on success, -ETIMEDOUT if timed out while waiting for ECC
2615de8c6eSPaul Cercueil * controller.
2715de8c6eSPaul Cercueil */
ingenic_ecc_calculate(struct ingenic_ecc * ecc,struct ingenic_ecc_params * params,const u8 * buf,u8 * ecc_code)2815de8c6eSPaul Cercueil int ingenic_ecc_calculate(struct ingenic_ecc *ecc,
2915de8c6eSPaul Cercueil struct ingenic_ecc_params *params,
3015de8c6eSPaul Cercueil const u8 *buf, u8 *ecc_code)
3115de8c6eSPaul Cercueil {
3215de8c6eSPaul Cercueil return ecc->ops->calculate(ecc, params, buf, ecc_code);
3315de8c6eSPaul Cercueil }
3415de8c6eSPaul Cercueil
3515de8c6eSPaul Cercueil /**
3615de8c6eSPaul Cercueil * ingenic_ecc_correct() - detect and correct bit errors
3715de8c6eSPaul Cercueil * @ecc: ECC device.
3815de8c6eSPaul Cercueil * @params: ECC parameters.
3915de8c6eSPaul Cercueil * @buf: raw data read from the chip.
4015de8c6eSPaul Cercueil * @ecc_code: ECC read from the chip.
4115de8c6eSPaul Cercueil *
4215de8c6eSPaul Cercueil * Given the raw data and the ECC read from the NAND device, detects and
4315de8c6eSPaul Cercueil * corrects errors in the data.
4415de8c6eSPaul Cercueil *
4515de8c6eSPaul Cercueil * Return: the number of bit errors corrected, -EBADMSG if there are too many
4615de8c6eSPaul Cercueil * errors to correct or -ETIMEDOUT if we timed out waiting for the controller.
4715de8c6eSPaul Cercueil */
ingenic_ecc_correct(struct ingenic_ecc * ecc,struct ingenic_ecc_params * params,u8 * buf,u8 * ecc_code)4815de8c6eSPaul Cercueil int ingenic_ecc_correct(struct ingenic_ecc *ecc,
4915de8c6eSPaul Cercueil struct ingenic_ecc_params *params,
5015de8c6eSPaul Cercueil u8 *buf, u8 *ecc_code)
5115de8c6eSPaul Cercueil {
5215de8c6eSPaul Cercueil return ecc->ops->correct(ecc, params, buf, ecc_code);
5315de8c6eSPaul Cercueil }
5415de8c6eSPaul Cercueil
5515de8c6eSPaul Cercueil /**
5615de8c6eSPaul Cercueil * ingenic_ecc_get() - get the ECC controller device
5715de8c6eSPaul Cercueil * @np: ECC device tree node.
5815de8c6eSPaul Cercueil *
5915de8c6eSPaul Cercueil * Gets the ECC controller device from the specified device tree node. The
6015de8c6eSPaul Cercueil * device must be released with ingenic_ecc_release() when it is no longer being
6115de8c6eSPaul Cercueil * used.
6215de8c6eSPaul Cercueil *
6315de8c6eSPaul Cercueil * Return: a pointer to ingenic_ecc, errors are encoded into the pointer.
6415de8c6eSPaul Cercueil * PTR_ERR(-EPROBE_DEFER) if the device hasn't been initialised yet.
6515de8c6eSPaul Cercueil */
ingenic_ecc_get(struct device_node * np)6615de8c6eSPaul Cercueil static struct ingenic_ecc *ingenic_ecc_get(struct device_node *np)
6715de8c6eSPaul Cercueil {
6815de8c6eSPaul Cercueil struct platform_device *pdev;
6915de8c6eSPaul Cercueil struct ingenic_ecc *ecc;
7015de8c6eSPaul Cercueil
7115de8c6eSPaul Cercueil pdev = of_find_device_by_node(np);
72ba1b71b0SMiaoqian Lin if (!pdev)
7315de8c6eSPaul Cercueil return ERR_PTR(-EPROBE_DEFER);
7415de8c6eSPaul Cercueil
75ba1b71b0SMiaoqian Lin if (!platform_get_drvdata(pdev)) {
76ba1b71b0SMiaoqian Lin put_device(&pdev->dev);
77ba1b71b0SMiaoqian Lin return ERR_PTR(-EPROBE_DEFER);
78ba1b71b0SMiaoqian Lin }
79ba1b71b0SMiaoqian Lin
8015de8c6eSPaul Cercueil ecc = platform_get_drvdata(pdev);
8115de8c6eSPaul Cercueil clk_prepare_enable(ecc->clk);
8215de8c6eSPaul Cercueil
8315de8c6eSPaul Cercueil return ecc;
8415de8c6eSPaul Cercueil }
8515de8c6eSPaul Cercueil
8615de8c6eSPaul Cercueil /**
8715de8c6eSPaul Cercueil * of_ingenic_ecc_get() - get the ECC controller from a DT node
88f838154aSPaul Cercueil * @of_node: the node that contains an ecc-engine property.
8915de8c6eSPaul Cercueil *
90f838154aSPaul Cercueil * Get the ecc-engine property from the given device tree
9115de8c6eSPaul Cercueil * node and pass it to ingenic_ecc_get to do the work.
9215de8c6eSPaul Cercueil *
9315de8c6eSPaul Cercueil * Return: a pointer to ingenic_ecc, errors are encoded into the pointer.
9415de8c6eSPaul Cercueil * PTR_ERR(-EPROBE_DEFER) if the device hasn't been initialised yet.
9515de8c6eSPaul Cercueil */
of_ingenic_ecc_get(struct device_node * of_node)9615de8c6eSPaul Cercueil struct ingenic_ecc *of_ingenic_ecc_get(struct device_node *of_node)
9715de8c6eSPaul Cercueil {
9815de8c6eSPaul Cercueil struct ingenic_ecc *ecc = NULL;
9915de8c6eSPaul Cercueil struct device_node *np;
10015de8c6eSPaul Cercueil
101f838154aSPaul Cercueil np = of_parse_phandle(of_node, "ecc-engine", 0);
102f838154aSPaul Cercueil
103f838154aSPaul Cercueil /*
104f838154aSPaul Cercueil * If the ecc-engine property is not found, check for the deprecated
105f838154aSPaul Cercueil * ingenic,bch-controller property
106f838154aSPaul Cercueil */
107f838154aSPaul Cercueil if (!np)
10815de8c6eSPaul Cercueil np = of_parse_phandle(of_node, "ingenic,bch-controller", 0);
10915de8c6eSPaul Cercueil
11015de8c6eSPaul Cercueil if (np) {
11115de8c6eSPaul Cercueil ecc = ingenic_ecc_get(np);
11215de8c6eSPaul Cercueil of_node_put(np);
11315de8c6eSPaul Cercueil }
11415de8c6eSPaul Cercueil return ecc;
11515de8c6eSPaul Cercueil }
11615de8c6eSPaul Cercueil
11715de8c6eSPaul Cercueil /**
11815de8c6eSPaul Cercueil * ingenic_ecc_release() - release the ECC controller device
11915de8c6eSPaul Cercueil * @ecc: ECC device.
12015de8c6eSPaul Cercueil */
ingenic_ecc_release(struct ingenic_ecc * ecc)12115de8c6eSPaul Cercueil void ingenic_ecc_release(struct ingenic_ecc *ecc)
12215de8c6eSPaul Cercueil {
12315de8c6eSPaul Cercueil clk_disable_unprepare(ecc->clk);
12415de8c6eSPaul Cercueil put_device(ecc->dev);
12515de8c6eSPaul Cercueil }
12615de8c6eSPaul Cercueil
ingenic_ecc_probe(struct platform_device * pdev)12715de8c6eSPaul Cercueil int ingenic_ecc_probe(struct platform_device *pdev)
12815de8c6eSPaul Cercueil {
12915de8c6eSPaul Cercueil struct device *dev = &pdev->dev;
13015de8c6eSPaul Cercueil struct ingenic_ecc *ecc;
13115de8c6eSPaul Cercueil
13215de8c6eSPaul Cercueil ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
13315de8c6eSPaul Cercueil if (!ecc)
13415de8c6eSPaul Cercueil return -ENOMEM;
13515de8c6eSPaul Cercueil
13615de8c6eSPaul Cercueil ecc->ops = device_get_match_data(dev);
13715de8c6eSPaul Cercueil if (!ecc->ops)
13815de8c6eSPaul Cercueil return -EINVAL;
13915de8c6eSPaul Cercueil
140c4b7dd35SPaul Cercueil ecc->base = devm_platform_ioremap_resource(pdev, 0);
14115de8c6eSPaul Cercueil if (IS_ERR(ecc->base))
14215de8c6eSPaul Cercueil return PTR_ERR(ecc->base);
14315de8c6eSPaul Cercueil
14415de8c6eSPaul Cercueil ecc->ops->disable(ecc);
14515de8c6eSPaul Cercueil
14615de8c6eSPaul Cercueil ecc->clk = devm_clk_get(dev, NULL);
14715de8c6eSPaul Cercueil if (IS_ERR(ecc->clk)) {
14815de8c6eSPaul Cercueil dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk));
14915de8c6eSPaul Cercueil return PTR_ERR(ecc->clk);
15015de8c6eSPaul Cercueil }
15115de8c6eSPaul Cercueil
15215de8c6eSPaul Cercueil mutex_init(&ecc->lock);
15315de8c6eSPaul Cercueil
15415de8c6eSPaul Cercueil ecc->dev = dev;
15515de8c6eSPaul Cercueil platform_set_drvdata(pdev, ecc);
15615de8c6eSPaul Cercueil
15715de8c6eSPaul Cercueil return 0;
15815de8c6eSPaul Cercueil }
15915de8c6eSPaul Cercueil EXPORT_SYMBOL(ingenic_ecc_probe);
160