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.h> 13 #include <linux/of_platform.h> 14 #include <linux/platform_device.h> 15 16 #include "ingenic_ecc.h" 17 18 /** 19 * ingenic_ecc_calculate() - calculate ECC for a data buffer 20 * @ecc: ECC device. 21 * @params: ECC parameters. 22 * @buf: input buffer with raw data. 23 * @ecc_code: output buffer with ECC. 24 * 25 * Return: 0 on success, -ETIMEDOUT if timed out while waiting for ECC 26 * controller. 27 */ 28 int ingenic_ecc_calculate(struct ingenic_ecc *ecc, 29 struct ingenic_ecc_params *params, 30 const u8 *buf, u8 *ecc_code) 31 { 32 return ecc->ops->calculate(ecc, params, buf, ecc_code); 33 } 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 55 /** 56 * ingenic_ecc_get() - get the ECC controller device 57 * @np: ECC device tree node. 58 * 59 * Gets the ECC controller device from the specified device tree node. The 60 * device must be released with ingenic_ecc_release() when it is no longer being 61 * used. 62 * 63 * Return: a pointer to ingenic_ecc, errors are encoded into the pointer. 64 * PTR_ERR(-EPROBE_DEFER) if the device hasn't been initialised yet. 65 */ 66 static struct ingenic_ecc *ingenic_ecc_get(struct device_node *np) 67 { 68 struct platform_device *pdev; 69 struct ingenic_ecc *ecc; 70 71 pdev = of_find_device_by_node(np); 72 if (!pdev) 73 return ERR_PTR(-EPROBE_DEFER); 74 75 if (!platform_get_drvdata(pdev)) { 76 put_device(&pdev->dev); 77 return ERR_PTR(-EPROBE_DEFER); 78 } 79 80 ecc = platform_get_drvdata(pdev); 81 clk_prepare_enable(ecc->clk); 82 83 return ecc; 84 } 85 86 /** 87 * of_ingenic_ecc_get() - get the ECC controller from a DT node 88 * @of_node: the node that contains an ecc-engine property. 89 * 90 * Get the ecc-engine property from the given device tree 91 * node and pass it to ingenic_ecc_get to do the work. 92 * 93 * Return: a pointer to ingenic_ecc, errors are encoded into the pointer. 94 * PTR_ERR(-EPROBE_DEFER) if the device hasn't been initialised yet. 95 */ 96 struct ingenic_ecc *of_ingenic_ecc_get(struct device_node *of_node) 97 { 98 struct ingenic_ecc *ecc = NULL; 99 struct device_node *np; 100 101 np = of_parse_phandle(of_node, "ecc-engine", 0); 102 103 /* 104 * If the ecc-engine property is not found, check for the deprecated 105 * ingenic,bch-controller property 106 */ 107 if (!np) 108 np = of_parse_phandle(of_node, "ingenic,bch-controller", 0); 109 110 if (np) { 111 ecc = ingenic_ecc_get(np); 112 of_node_put(np); 113 } 114 return ecc; 115 } 116 117 /** 118 * ingenic_ecc_release() - release the ECC controller device 119 * @ecc: ECC device. 120 */ 121 void ingenic_ecc_release(struct ingenic_ecc *ecc) 122 { 123 clk_disable_unprepare(ecc->clk); 124 put_device(ecc->dev); 125 } 126 127 int ingenic_ecc_probe(struct platform_device *pdev) 128 { 129 struct device *dev = &pdev->dev; 130 struct ingenic_ecc *ecc; 131 132 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL); 133 if (!ecc) 134 return -ENOMEM; 135 136 ecc->ops = device_get_match_data(dev); 137 if (!ecc->ops) 138 return -EINVAL; 139 140 ecc->base = devm_platform_ioremap_resource(pdev, 0); 141 if (IS_ERR(ecc->base)) 142 return PTR_ERR(ecc->base); 143 144 ecc->ops->disable(ecc); 145 146 ecc->clk = devm_clk_get(dev, NULL); 147 if (IS_ERR(ecc->clk)) { 148 dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk)); 149 return PTR_ERR(ecc->clk); 150 } 151 152 mutex_init(&ecc->lock); 153 154 ecc->dev = dev; 155 platform_set_drvdata(pdev, ecc); 156 157 return 0; 158 } 159 EXPORT_SYMBOL(ingenic_ecc_probe); 160