mtd: nand: implement the default mtd_ooblayout_ops
[deliverable/linux.git] / drivers / of / of_mtd.c
1 /*
2 * Copyright 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
3 *
4 * OF helpers for mtd.
5 *
6 * This file is released under the GPLv2
7 *
8 */
9 #include <linux/kernel.h>
10 #include <linux/of_mtd.h>
11 #include <linux/mtd/nand.h>
12 #include <linux/export.h>
13
14 /**
15 * It maps 'enum nand_ecc_modes_t' found in include/linux/mtd/nand.h
16 * into the device tree binding of 'nand-ecc', so that MTD
17 * device driver can get nand ecc from device tree.
18 */
19 static const char *nand_ecc_modes[] = {
20 [NAND_ECC_NONE] = "none",
21 [NAND_ECC_SOFT] = "soft",
22 [NAND_ECC_HW] = "hw",
23 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
24 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
25 [NAND_ECC_SOFT_BCH] = "soft_bch",
26 };
27
28 /**
29 * of_get_nand_ecc_mode - Get nand ecc mode for given device_node
30 * @np: Pointer to the given device_node
31 *
32 * The function gets ecc mode string from property 'nand-ecc-mode',
33 * and return its index in nand_ecc_modes table, or errno in error case.
34 */
35 int of_get_nand_ecc_mode(struct device_node *np)
36 {
37 const char *pm;
38 int err, i;
39
40 err = of_property_read_string(np, "nand-ecc-mode", &pm);
41 if (err < 0)
42 return err;
43
44 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
45 if (!strcasecmp(pm, nand_ecc_modes[i]))
46 return i;
47
48 return -ENODEV;
49 }
50 EXPORT_SYMBOL_GPL(of_get_nand_ecc_mode);
51
52 /**
53 * of_get_nand_ecc_algo - Get nand ecc algorithm for given device_node
54 * @np: Pointer to the given device_node
55 *
56 * The function gets ecc algorithm and returns its enum value, or errno in error
57 * case.
58 */
59 int of_get_nand_ecc_algo(struct device_node *np)
60 {
61 const char *pm;
62 int err;
63
64 /*
65 * TODO: Read ECC algo OF property and map it to enum nand_ecc_algo.
66 * It's not implemented yet as currently NAND subsystem ignores
67 * algorithm explicitly set this way. Once it's handled we should
68 * document & support new property.
69 */
70
71 /*
72 * For backward compatibility we also read "nand-ecc-mode" checking
73 * for some obsoleted values that were specifying ECC algorithm.
74 */
75 err = of_property_read_string(np, "nand-ecc-mode", &pm);
76 if (err < 0)
77 return err;
78
79 if (!strcasecmp(pm, "soft"))
80 return NAND_ECC_HAMMING;
81 else if (!strcasecmp(pm, "soft_bch"))
82 return NAND_ECC_BCH;
83
84 return -ENODEV;
85 }
86 EXPORT_SYMBOL_GPL(of_get_nand_ecc_algo);
87
88 /**
89 * of_get_nand_ecc_step_size - Get ECC step size associated to
90 * the required ECC strength (see below).
91 * @np: Pointer to the given device_node
92 *
93 * return the ECC step size, or errno in error case.
94 */
95 int of_get_nand_ecc_step_size(struct device_node *np)
96 {
97 int ret;
98 u32 val;
99
100 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
101 return ret ? ret : val;
102 }
103 EXPORT_SYMBOL_GPL(of_get_nand_ecc_step_size);
104
105 /**
106 * of_get_nand_ecc_strength - Get required ECC strength over the
107 * correspnding step size as defined by 'nand-ecc-size'
108 * @np: Pointer to the given device_node
109 *
110 * return the ECC strength, or errno in error case.
111 */
112 int of_get_nand_ecc_strength(struct device_node *np)
113 {
114 int ret;
115 u32 val;
116
117 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
118 return ret ? ret : val;
119 }
120 EXPORT_SYMBOL_GPL(of_get_nand_ecc_strength);
121
122 /**
123 * of_get_nand_bus_width - Get nand bus witdh for given device_node
124 * @np: Pointer to the given device_node
125 *
126 * return bus width option, or errno in error case.
127 */
128 int of_get_nand_bus_width(struct device_node *np)
129 {
130 u32 val;
131
132 if (of_property_read_u32(np, "nand-bus-width", &val))
133 return 8;
134
135 switch(val) {
136 case 8:
137 case 16:
138 return val;
139 default:
140 return -EIO;
141 }
142 }
143 EXPORT_SYMBOL_GPL(of_get_nand_bus_width);
144
145 /**
146 * of_get_nand_on_flash_bbt - Get nand on flash bbt for given device_node
147 * @np: Pointer to the given device_node
148 *
149 * return true if present false other wise
150 */
151 bool of_get_nand_on_flash_bbt(struct device_node *np)
152 {
153 return of_property_read_bool(np, "nand-on-flash-bbt");
154 }
155 EXPORT_SYMBOL_GPL(of_get_nand_on_flash_bbt);
This page took 0.045556 seconds and 5 git commands to generate.