omap2_nand.c revision 1.1 1 1.1 jmcneill /* $NetBSD: omap2_nand.c,v 1.1 2019/11/01 11:53:35 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2010 Department of Software Engineering,
5 1.1 jmcneill * University of Szeged, Hungary
6 1.1 jmcneill * Copyright (c) 2010 Adam Hoka <ahoka (at) NetBSD.org>
7 1.1 jmcneill * All rights reserved.
8 1.1 jmcneill *
9 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
10 1.1 jmcneill * by the Department of Software Engineering, University of Szeged, Hungary
11 1.1 jmcneill *
12 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
13 1.1 jmcneill * modification, are permitted provided that the following conditions
14 1.1 jmcneill * are met:
15 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
16 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
17 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
19 1.1 jmcneill * documentation and/or other materials provided with the distribution.
20 1.1 jmcneill *
21 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 jmcneill * SUCH DAMAGE.
32 1.1 jmcneill */
33 1.1 jmcneill
34 1.1 jmcneill /* Device driver for the NAND controller found in Texas Instruments OMAP2
35 1.1 jmcneill * and later SOCs.
36 1.1 jmcneill */
37 1.1 jmcneill
38 1.1 jmcneill #include <sys/cdefs.h>
39 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: omap2_nand.c,v 1.1 2019/11/01 11:53:35 jmcneill Exp $");
40 1.1 jmcneill
41 1.1 jmcneill /* TODO move to opt_* */
42 1.1 jmcneill #undef OMAP2_NAND_HARDWARE_ECC
43 1.1 jmcneill
44 1.1 jmcneill #include <sys/param.h>
45 1.1 jmcneill #include <sys/systm.h>
46 1.1 jmcneill #include <sys/cdefs.h>
47 1.1 jmcneill #include <sys/device.h>
48 1.1 jmcneill
49 1.1 jmcneill #include <sys/bus.h>
50 1.1 jmcneill
51 1.1 jmcneill #include <arm/ti/omap2_gpmcreg.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <dev/nand/nand.h>
54 1.1 jmcneill #include <dev/nand/onfi.h>
55 1.1 jmcneill
56 1.1 jmcneill #include <dev/fdt/fdtvar.h>
57 1.1 jmcneill
58 1.1 jmcneill extern struct flash_interface nand_flash_if;
59 1.1 jmcneill extern int flash_print(void *, const char *);
60 1.1 jmcneill
61 1.1 jmcneill /* GPMC_STATUS */
62 1.1 jmcneill #define WAIT0 __BIT(8) /* active low */
63 1.1 jmcneill
64 1.1 jmcneill /* GPMC_ECC_CONTROL */
65 1.1 jmcneill #define ECCCLEAR __BIT(8)
66 1.1 jmcneill #define ECCPOINTER __BITS(3,0)
67 1.1 jmcneill
68 1.1 jmcneill /* GPMC_ECC_CONFIG */
69 1.1 jmcneill #define ECCALGORITHM __BIT(16)
70 1.1 jmcneill #define ECCCS __BITS(3,1)
71 1.1 jmcneill #define ECC16B __BIT(7)
72 1.1 jmcneill #define ECCENABLE __BIT(0)
73 1.1 jmcneill /* GPMC_ECC_SIZE_CONFIG */
74 1.1 jmcneill #define ECCSIZE1 __BITS(29,22)
75 1.1 jmcneill
76 1.1 jmcneill /* GPMC_CONFIG1_i */
77 1.1 jmcneill #define DEVICETYPE __BITS(11,10)
78 1.1 jmcneill #define DEVICESIZE __BITS(13,12)
79 1.1 jmcneill
80 1.1 jmcneill #define MASKEDINT(mask, integer) ((integer) << (ffs(mask) - 1) & mask)
81 1.1 jmcneill
82 1.1 jmcneill /* NAND status register */
83 1.1 jmcneill #define NAND_WP_BIT __BIT(4)
84 1.1 jmcneill
85 1.1 jmcneill static int omap2_nand_match(device_t, cfdata_t, void *);
86 1.1 jmcneill static void omap2_nand_attach(device_t, device_t, void *);
87 1.1 jmcneill
88 1.1 jmcneill static void omap2_nand_command(device_t self, uint8_t command);
89 1.1 jmcneill static void omap2_nand_address(device_t self, uint8_t address);
90 1.1 jmcneill static void omap2_nand_busy(device_t self);
91 1.1 jmcneill static void omap2_nand_read_1(device_t self, uint8_t *data);
92 1.1 jmcneill static void omap2_nand_write_1(device_t self, uint8_t data);
93 1.1 jmcneill static void omap2_nand_read_2(device_t self, uint16_t *data);
94 1.1 jmcneill static void omap2_nand_write_2(device_t self, uint16_t data);
95 1.1 jmcneill bool omap2_nand_isbusy(device_t self);
96 1.1 jmcneill static void omap2_nand_read_buf_1(device_t self, void *buf, size_t len);
97 1.1 jmcneill static void omap2_nand_read_buf_2(device_t self, void *buf, size_t len);
98 1.1 jmcneill static void omap2_nand_write_buf_1(device_t self, const void *buf, size_t len);
99 1.1 jmcneill static void omap2_nand_write_buf_2(device_t self, const void *buf, size_t len);
100 1.1 jmcneill
101 1.1 jmcneill #ifdef OMAP2_NAND_HARDWARE_ECC
102 1.1 jmcneill static int omap2_nand_ecc_init(device_t self);
103 1.1 jmcneill static int omap2_nand_ecc_prepare(device_t self, int mode);
104 1.1 jmcneill static int omap2_nand_ecc_compute(device_t self, const uint8_t *data, uint8_t *ecc);
105 1.1 jmcneill static int omap2_nand_ecc_correct(device_t self, uint8_t *data, const uint8_t *oldecc,
106 1.1 jmcneill const uint8_t *calcecc);
107 1.1 jmcneill #endif
108 1.1 jmcneill
109 1.1 jmcneill struct omap2_nand_softc {
110 1.1 jmcneill device_t sc_dev;
111 1.1 jmcneill device_t sc_nanddev;
112 1.1 jmcneill
113 1.1 jmcneill int sc_cs;
114 1.1 jmcneill int sc_buswidth; /* 0: 8bit, 1: 16bit */
115 1.1 jmcneill
116 1.1 jmcneill struct nand_interface sc_nand_if;
117 1.1 jmcneill
118 1.1 jmcneill bus_space_tag_t sc_iot;
119 1.1 jmcneill bus_space_handle_t sc_ioh;
120 1.1 jmcneill bus_space_handle_t sc_gpmc_ioh;
121 1.1 jmcneill
122 1.1 jmcneill bus_size_t sc_cmd_reg;
123 1.1 jmcneill bus_size_t sc_addr_reg;
124 1.1 jmcneill bus_size_t sc_data_reg;
125 1.1 jmcneill };
126 1.1 jmcneill
127 1.1 jmcneill static const char * compatible[] = {
128 1.1 jmcneill "ti,omap2-nand",
129 1.1 jmcneill NULL
130 1.1 jmcneill };
131 1.1 jmcneill
132 1.1 jmcneill CFATTACH_DECL_NEW(omapnand, sizeof(struct omap2_nand_softc), omap2_nand_match,
133 1.1 jmcneill omap2_nand_attach, NULL, NULL);
134 1.1 jmcneill
135 1.1 jmcneill static inline uint32_t
136 1.1 jmcneill gpmc_register_read(struct omap2_nand_softc *sc, bus_size_t reg)
137 1.1 jmcneill {
138 1.1 jmcneill return bus_space_read_4(sc->sc_iot, sc->sc_gpmc_ioh, reg);
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static inline void
142 1.1 jmcneill gpmc_register_write(struct omap2_nand_softc *sc, bus_size_t reg, const uint32_t data)
143 1.1 jmcneill {
144 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_gpmc_ioh, reg, data);
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill static void
148 1.1 jmcneill omap2_nand_command(device_t self, uint8_t command)
149 1.1 jmcneill {
150 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
151 1.1 jmcneill
152 1.1 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_cmd_reg, command);
153 1.1 jmcneill };
154 1.1 jmcneill
155 1.1 jmcneill static void
156 1.1 jmcneill omap2_nand_address(device_t self, uint8_t address)
157 1.1 jmcneill {
158 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
159 1.1 jmcneill
160 1.1 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_addr_reg, address);
161 1.1 jmcneill };
162 1.1 jmcneill
163 1.1 jmcneill bool
164 1.1 jmcneill omap2_nand_isbusy(device_t self)
165 1.1 jmcneill {
166 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
167 1.1 jmcneill uint8_t status;
168 1.1 jmcneill
169 1.1 jmcneill DELAY(1); /* just to be sure we are not early */
170 1.1 jmcneill
171 1.1 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
172 1.1 jmcneill sc->sc_cmd_reg, ONFI_READ_STATUS);
173 1.1 jmcneill
174 1.1 jmcneill DELAY(1);
175 1.1 jmcneill
176 1.1 jmcneill status = bus_space_read_1(sc->sc_iot,
177 1.1 jmcneill sc->sc_ioh, sc->sc_data_reg);
178 1.1 jmcneill
179 1.1 jmcneill return !(status & ONFI_STATUS_RDY);
180 1.1 jmcneill };
181 1.1 jmcneill
182 1.1 jmcneill static int
183 1.1 jmcneill omap2_nand_match(device_t parent, cfdata_t match, void *aux)
184 1.1 jmcneill {
185 1.1 jmcneill struct fdt_attach_args * const faa = aux;
186 1.1 jmcneill
187 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill static void
191 1.1 jmcneill omap2_nand_attach(device_t parent, device_t self, void *aux)
192 1.1 jmcneill {
193 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
194 1.1 jmcneill struct fdt_attach_args * const faa = aux;
195 1.1 jmcneill const int phandle = faa->faa_phandle;
196 1.1 jmcneill struct flash_attach_args flash;
197 1.1 jmcneill bus_addr_t addr, part_addr;
198 1.1 jmcneill bus_size_t size, part_size;
199 1.1 jmcneill const u_int *prop;
200 1.1 jmcneill uint32_t val;
201 1.1 jmcneill int len, child;
202 1.1 jmcneill
203 1.1 jmcneill if (fdtbus_get_reg(OF_parent(phandle), 0, &addr, &size) != 0) {
204 1.1 jmcneill aprint_error(": couldn't get registers\n");
205 1.1 jmcneill return;
206 1.1 jmcneill }
207 1.1 jmcneill
208 1.1 jmcneill sc->sc_iot = faa->faa_bst;
209 1.1 jmcneill sc->sc_dev = self;
210 1.1 jmcneill
211 1.1 jmcneill prop = fdtbus_get_prop(phandle, "reg", &len);
212 1.1 jmcneill if (prop == NULL || len < 4) {
213 1.1 jmcneill aprint_error(": couldn't read reg property\n");
214 1.1 jmcneill return;
215 1.1 jmcneill }
216 1.1 jmcneill
217 1.1 jmcneill sc->sc_cs = be32toh(prop[0]);
218 1.1 jmcneill
219 1.1 jmcneill /* map i/o space */
220 1.1 jmcneill if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_gpmc_ioh) != 0) {
221 1.1 jmcneill aprint_error(": couldn't map registers\n");
222 1.1 jmcneill return;
223 1.1 jmcneill }
224 1.1 jmcneill if (bus_space_subregion(sc->sc_iot, sc->sc_gpmc_ioh, GPMC_CS_CONFIG(sc->sc_cs), 0x30, &sc->sc_ioh) != 0) {
225 1.1 jmcneill aprint_error(": couldn't map cs registers\n");
226 1.1 jmcneill return;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill aprint_naive("\n");
230 1.1 jmcneill aprint_normal(": CS%d\n", sc->sc_cs);
231 1.1 jmcneill
232 1.1 jmcneill sc->sc_cmd_reg = GPMC_NAND_COMMAND_0 - GPMC_CONFIG1_0;
233 1.1 jmcneill sc->sc_addr_reg = GPMC_NAND_ADDRESS_0 - GPMC_CONFIG1_0;
234 1.1 jmcneill sc->sc_data_reg = GPMC_NAND_DATA_0 - GPMC_CONFIG1_0;
235 1.1 jmcneill
236 1.1 jmcneill /* turn off write protection if enabled */
237 1.1 jmcneill val = gpmc_register_read(sc, GPMC_CONFIG);
238 1.1 jmcneill val |= NAND_WP_BIT;
239 1.1 jmcneill gpmc_register_write(sc, GPMC_CONFIG, val);
240 1.1 jmcneill
241 1.1 jmcneill /*
242 1.1 jmcneill * do the reset dance for NAND
243 1.1 jmcneill */
244 1.1 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
245 1.1 jmcneill sc->sc_cmd_reg, ONFI_RESET);
246 1.1 jmcneill
247 1.1 jmcneill omap2_nand_busy(self);
248 1.1 jmcneill
249 1.1 jmcneill /* read GPMC_CONFIG1_i to get buswidth */
250 1.1 jmcneill val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPMC_CONFIG1_i);
251 1.1 jmcneill
252 1.1 jmcneill if ((val & DEVICESIZE) == MASKEDINT(DEVICESIZE, 0x01)) {
253 1.1 jmcneill /* 16bit */
254 1.1 jmcneill sc->sc_buswidth = 1;
255 1.1 jmcneill } else if ((val & DEVICESIZE) == MASKEDINT(DEVICESIZE, 0x00)) {
256 1.1 jmcneill /* 8bit */
257 1.1 jmcneill sc->sc_buswidth = 0;
258 1.1 jmcneill } else {
259 1.1 jmcneill panic("invalid buswidth reported by config1");
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill nand_init_interface(&sc->sc_nand_if);
263 1.1 jmcneill
264 1.1 jmcneill sc->sc_nand_if.command = &omap2_nand_command;
265 1.1 jmcneill sc->sc_nand_if.address = &omap2_nand_address;
266 1.1 jmcneill sc->sc_nand_if.read_buf_1 = &omap2_nand_read_buf_1;
267 1.1 jmcneill sc->sc_nand_if.read_buf_2 = &omap2_nand_read_buf_2;
268 1.1 jmcneill sc->sc_nand_if.read_1 = &omap2_nand_read_1;
269 1.1 jmcneill sc->sc_nand_if.read_2 = &omap2_nand_read_2;
270 1.1 jmcneill sc->sc_nand_if.write_buf_1 = &omap2_nand_write_buf_1;
271 1.1 jmcneill sc->sc_nand_if.write_buf_2 = &omap2_nand_write_buf_2;
272 1.1 jmcneill sc->sc_nand_if.write_1 = &omap2_nand_write_1;
273 1.1 jmcneill sc->sc_nand_if.write_2 = &omap2_nand_write_2;
274 1.1 jmcneill sc->sc_nand_if.busy = &omap2_nand_busy;
275 1.1 jmcneill
276 1.1 jmcneill #ifdef OMAP2_NAND_HARDWARE_ECC
277 1.1 jmcneill omap2_nand_ecc_init(self);
278 1.1 jmcneill sc->sc_nand_if.ecc_compute = &omap2_nand_ecc_compute;
279 1.1 jmcneill sc->sc_nand_if.ecc_correct = &omap2_nand_ecc_correct;
280 1.1 jmcneill sc->sc_nand_if.ecc_prepare = &omap2_nand_ecc_prepare;
281 1.1 jmcneill sc->sc_nand_if.ecc.necc_code_size = 3;
282 1.1 jmcneill sc->sc_nand_if.ecc.necc_block_size = 512;
283 1.1 jmcneill sc->sc_nand_if.ecc.necc_type = NAND_ECC_TYPE_HW;
284 1.1 jmcneill #else
285 1.1 jmcneill sc->sc_nand_if.ecc.necc_code_size = 3;
286 1.1 jmcneill sc->sc_nand_if.ecc.necc_block_size = 256;
287 1.1 jmcneill #endif /* OMAP2_NAND_HARDWARE_ECC */
288 1.1 jmcneill
289 1.1 jmcneill if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL))
290 1.1 jmcneill aprint_error_dev(sc->sc_dev,
291 1.1 jmcneill "couldn't establish power handler\n");
292 1.1 jmcneill
293 1.1 jmcneill sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev);
294 1.1 jmcneill if (sc->sc_nanddev == NULL)
295 1.1 jmcneill return;
296 1.1 jmcneill
297 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
298 1.1 jmcneill if (!fdtbus_status_okay(child))
299 1.1 jmcneill continue;
300 1.1 jmcneill
301 1.1 jmcneill if (fdtbus_get_reg(child, 0, &part_addr, &part_size) != 0) {
302 1.1 jmcneill aprint_error_dev(self, "couldn't parse partition %s\n",
303 1.1 jmcneill fdtbus_get_string(child, "name"));
304 1.1 jmcneill continue;
305 1.1 jmcneill }
306 1.1 jmcneill
307 1.1 jmcneill memset(&flash, 0, sizeof(flash));
308 1.1 jmcneill flash.flash_if = &nand_flash_if;
309 1.1 jmcneill flash.partinfo.part_offset = part_addr;
310 1.1 jmcneill flash.partinfo.part_size = part_size;
311 1.1 jmcneill flash.partinfo.part_flags = 0;
312 1.1 jmcneill flash.partinfo.part_name = fdtbus_get_string(child, "label");
313 1.1 jmcneill if (flash.partinfo.part_name == NULL)
314 1.1 jmcneill flash.partinfo.part_name = fdtbus_get_string(child, "name");
315 1.1 jmcneill
316 1.1 jmcneill config_found_ia(sc->sc_nanddev, "flashbus", &flash, flash_print);
317 1.1 jmcneill }
318 1.1 jmcneill }
319 1.1 jmcneill
320 1.1 jmcneill static void
321 1.1 jmcneill omap2_nand_busy(device_t self)
322 1.1 jmcneill {
323 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
324 1.1 jmcneill
325 1.1 jmcneill while (!(gpmc_register_read(sc, GPMC_STATUS) & WAIT0)) {
326 1.1 jmcneill DELAY(1);
327 1.1 jmcneill }
328 1.1 jmcneill }
329 1.1 jmcneill
330 1.1 jmcneill static void
331 1.1 jmcneill omap2_nand_read_1(device_t self, uint8_t *data)
332 1.1 jmcneill {
333 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
334 1.1 jmcneill
335 1.1 jmcneill *data = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_data_reg);
336 1.1 jmcneill }
337 1.1 jmcneill
338 1.1 jmcneill static void
339 1.1 jmcneill omap2_nand_write_1(device_t self, uint8_t data)
340 1.1 jmcneill {
341 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
342 1.1 jmcneill
343 1.1 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_data_reg, data);
344 1.1 jmcneill }
345 1.1 jmcneill
346 1.1 jmcneill static void
347 1.1 jmcneill omap2_nand_read_2(device_t self, uint16_t *data)
348 1.1 jmcneill {
349 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
350 1.1 jmcneill
351 1.1 jmcneill *data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, sc->sc_data_reg);
352 1.1 jmcneill }
353 1.1 jmcneill
354 1.1 jmcneill static void
355 1.1 jmcneill omap2_nand_write_2(device_t self, uint16_t data)
356 1.1 jmcneill {
357 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
358 1.1 jmcneill
359 1.1 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh, sc->sc_data_reg, data);
360 1.1 jmcneill }
361 1.1 jmcneill
362 1.1 jmcneill static void
363 1.1 jmcneill omap2_nand_read_buf_1(device_t self, void *buf, size_t len)
364 1.1 jmcneill {
365 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
366 1.1 jmcneill
367 1.1 jmcneill KASSERT(buf != NULL);
368 1.1 jmcneill KASSERT(len >= 1);
369 1.1 jmcneill
370 1.1 jmcneill bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh,
371 1.1 jmcneill sc->sc_data_reg, buf, len);
372 1.1 jmcneill }
373 1.1 jmcneill
374 1.1 jmcneill static void
375 1.1 jmcneill omap2_nand_read_buf_2(device_t self, void *buf, size_t len)
376 1.1 jmcneill {
377 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
378 1.1 jmcneill
379 1.1 jmcneill KASSERT(buf != NULL);
380 1.1 jmcneill KASSERT(len >= 2);
381 1.1 jmcneill KASSERT(!(len & 0x01));
382 1.1 jmcneill
383 1.1 jmcneill bus_space_read_multi_2(sc->sc_iot, sc->sc_ioh,
384 1.1 jmcneill sc->sc_data_reg, buf, len / 2);
385 1.1 jmcneill }
386 1.1 jmcneill
387 1.1 jmcneill static void
388 1.1 jmcneill omap2_nand_write_buf_1(device_t self, const void *buf, size_t len)
389 1.1 jmcneill {
390 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
391 1.1 jmcneill
392 1.1 jmcneill KASSERT(buf != NULL);
393 1.1 jmcneill KASSERT(len >= 1);
394 1.1 jmcneill
395 1.1 jmcneill bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh,
396 1.1 jmcneill sc->sc_data_reg, buf, len);
397 1.1 jmcneill }
398 1.1 jmcneill
399 1.1 jmcneill static void
400 1.1 jmcneill omap2_nand_write_buf_2(device_t self, const void *buf, size_t len)
401 1.1 jmcneill {
402 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
403 1.1 jmcneill
404 1.1 jmcneill KASSERT(buf != NULL);
405 1.1 jmcneill KASSERT(len >= 2);
406 1.1 jmcneill KASSERT(!(len & 0x01));
407 1.1 jmcneill
408 1.1 jmcneill bus_space_write_multi_2(sc->sc_iot, sc->sc_ioh,
409 1.1 jmcneill sc->sc_data_reg, buf, len / 2);
410 1.1 jmcneill }
411 1.1 jmcneill
412 1.1 jmcneill #ifdef OMAP2_NAND_HARDWARE_ECC
413 1.1 jmcneill static uint32_t
414 1.1 jmcneill convert_ecc(const uint8_t *ecc)
415 1.1 jmcneill {
416 1.1 jmcneill return ecc[0] | (ecc[1] << 16) | ((ecc[2] & 0xf0) << 20) |
417 1.1 jmcneill ((ecc[2] & 0x0f) << 8);
418 1.1 jmcneill }
419 1.1 jmcneill
420 1.1 jmcneill static int
421 1.1 jmcneill omap2_nand_ecc_init(device_t self)
422 1.1 jmcneill {
423 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
424 1.1 jmcneill uint32_t val;
425 1.1 jmcneill
426 1.1 jmcneill val = gpmc_register_read(sc, GPMC_ECC_CONTROL);
427 1.1 jmcneill /* clear ecc, select ecc register 1 */
428 1.1 jmcneill val &= ~ECCPOINTER;
429 1.1 jmcneill val |= ECCCLEAR | MASKEDINT(ECCPOINTER, 1);
430 1.1 jmcneill gpmc_register_write(sc, GPMC_ECC_CONTROL, val);
431 1.1 jmcneill
432 1.1 jmcneill /* XXX too many MAGIC */
433 1.1 jmcneill /* set ecc size to 512, set all regs to eccsize1*/
434 1.1 jmcneill val = gpmc_register_read(sc, GPMC_ECC_SIZE_CONFIG);
435 1.1 jmcneill val &= ~ECCSIZE1;
436 1.1 jmcneill val |= MASKEDINT(ECCSIZE1, 512) | 0x0f;
437 1.1 jmcneill gpmc_register_write(sc, GPMC_ECC_CONTROL, val);
438 1.1 jmcneill
439 1.1 jmcneill return 0;
440 1.1 jmcneill }
441 1.1 jmcneill
442 1.1 jmcneill static int
443 1.1 jmcneill omap2_nand_ecc_compute(device_t self, const uint8_t *data, uint8_t *ecc)
444 1.1 jmcneill {
445 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
446 1.1 jmcneill uint32_t val;
447 1.1 jmcneill
448 1.1 jmcneill /* read ecc result register */
449 1.1 jmcneill val = gpmc_register_read(sc, GPMC_ECC1_RESULT);
450 1.1 jmcneill
451 1.1 jmcneill ecc[0] = val & 0xff;
452 1.1 jmcneill ecc[1] = (val >> 16) & 0xff;
453 1.1 jmcneill ecc[2] = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
454 1.1 jmcneill
455 1.1 jmcneill /* disable ecc engine */
456 1.1 jmcneill val = gpmc_register_read(sc, GPMC_ECC_CONFIG);
457 1.1 jmcneill val &= ~ECCENABLE;
458 1.1 jmcneill gpmc_register_write(sc, GPMC_ECC_CONFIG, val);
459 1.1 jmcneill
460 1.1 jmcneill return 0;
461 1.1 jmcneill }
462 1.1 jmcneill
463 1.1 jmcneill static int
464 1.1 jmcneill omap2_nand_ecc_prepare(device_t self, int mode)
465 1.1 jmcneill {
466 1.1 jmcneill struct omap2_nand_softc *sc = device_private(self);
467 1.1 jmcneill uint32_t val;
468 1.1 jmcneill
469 1.1 jmcneill /* same for read/write */
470 1.1 jmcneill switch (mode) {
471 1.1 jmcneill case NAND_ECC_READ:
472 1.1 jmcneill case NAND_ECC_WRITE:
473 1.1 jmcneill val = gpmc_register_read(sc, GPMC_ECC_CONTROL);
474 1.1 jmcneill /* clear ecc, select ecc register 1 */
475 1.1 jmcneill val &= ~ECCPOINTER;
476 1.1 jmcneill val |= ECCCLEAR | MASKEDINT(ECCPOINTER, 1);
477 1.1 jmcneill gpmc_register_write(sc, GPMC_ECC_CONTROL, val);
478 1.1 jmcneill
479 1.1 jmcneill val = gpmc_register_read(sc, GPMC_ECC_CONFIG);
480 1.1 jmcneill val &= ~ECCCS;
481 1.1 jmcneill val |= ECCENABLE | MASKEDINT(ECCCS, sc->sc_cs);
482 1.1 jmcneill if (sc->sc_buswidth == 1)
483 1.1 jmcneill val |= ECC16B;
484 1.1 jmcneill else
485 1.1 jmcneill val &= ~ECC16B;
486 1.1 jmcneill gpmc_register_write(sc, GPMC_ECC_CONFIG, val);
487 1.1 jmcneill
488 1.1 jmcneill break;
489 1.1 jmcneill default:
490 1.1 jmcneill aprint_error_dev(self, "invalid i/o mode for ecc prepare\n");
491 1.1 jmcneill return -1;
492 1.1 jmcneill }
493 1.1 jmcneill
494 1.1 jmcneill return 0;
495 1.1 jmcneill }
496 1.1 jmcneill
497 1.1 jmcneill static int
498 1.1 jmcneill omap2_nand_ecc_correct(device_t self, uint8_t *data, const uint8_t *oldecc,
499 1.1 jmcneill const uint8_t *calcecc)
500 1.1 jmcneill {
501 1.1 jmcneill uint32_t oecc, cecc, xor;
502 1.1 jmcneill uint16_t parity, offset;
503 1.1 jmcneill uint8_t bit;
504 1.1 jmcneill
505 1.1 jmcneill oecc = convert_ecc(oldecc);
506 1.1 jmcneill cecc = convert_ecc(calcecc);
507 1.1 jmcneill
508 1.1 jmcneill /* get the difference */
509 1.1 jmcneill xor = oecc ^ cecc;
510 1.1 jmcneill
511 1.1 jmcneill /* the data was correct if all bits are zero */
512 1.1 jmcneill if (xor == 0x00)
513 1.1 jmcneill return NAND_ECC_OK;
514 1.1 jmcneill
515 1.1 jmcneill switch (popcount32(xor)) {
516 1.1 jmcneill case 12:
517 1.1 jmcneill /* single byte error */
518 1.1 jmcneill parity = xor >> 16;
519 1.1 jmcneill bit = (parity & 0x07);
520 1.1 jmcneill offset = (parity >> 3) & 0x01ff;
521 1.1 jmcneill /* correct bit */
522 1.1 jmcneill data[offset] ^= (0x01 << bit);
523 1.1 jmcneill return NAND_ECC_CORRECTED;
524 1.1 jmcneill case 1:
525 1.1 jmcneill return NAND_ECC_INVALID;
526 1.1 jmcneill default:
527 1.1 jmcneill /* erased page! */
528 1.1 jmcneill if ((oecc == 0x0fff0fff) && (cecc == 0x00000000))
529 1.1 jmcneill return NAND_ECC_OK;
530 1.1 jmcneill
531 1.1 jmcneill return NAND_ECC_TWOBIT;
532 1.1 jmcneill }
533 1.1 jmcneill }
534 1.1 jmcneill #endif /* !OMAP2_NAND_HARDWARE_ECC */
535