xref: /netbsd-src/sys/dev/nor/nor.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: nor.c,v 1.6 2021/04/24 23:36:56 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 Department of Software Engineering,
5  *		      University of Szeged, Hungary
6  * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org>
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by the Department of Software Engineering, University of Szeged, Hungary
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /* Common driver for NOR chips implementing the ONFI CFI specification */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: nor.c,v 1.6 2021/04/24 23:36:56 thorpej Exp $");
38 
39 #include "locators.h"
40 #include "opt_nor.h"
41 
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/device.h>
45 #include <sys/kmem.h>
46 #include <sys/sysctl.h>
47 #include <sys/atomic.h>
48 
49 #include <dev/flash/flash.h>
50 #include <dev/flash/flash_io.h>
51 #include <dev/nor/nor.h>
52 
53 
54 static int nor_match(device_t, cfdata_t, void *);
55 static void nor_attach(device_t, device_t, void *);
56 static int nor_detach(device_t, int);
57 static bool nor_shutdown(device_t, int);
58 static int nor_print(void *, const char *);
59 static int nor_search(device_t, cfdata_t, const int *, void *);
60 
61 /* flash interface implementation */
62 static int nor_flash_isbad(device_t, flash_off_t, bool *);
63 static int nor_flash_markbad(device_t, flash_off_t);
64 static int nor_flash_write(device_t, flash_off_t, size_t, size_t *,
65 	const u_char *);
66 static int nor_flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
67 static int nor_flash_erase_all(device_t);
68 static int nor_flash_erase(device_t, struct flash_erase_instruction *);
69 static int nor_flash_submit(device_t, buf_t *);
70 
71 /* default functions for driver development */
72 static void nor_default_select(device_t, bool);
73 static int  nor_default_read_page(device_t, flash_off_t, uint8_t *);
74 static int  nor_default_program_page(device_t, flash_off_t, const uint8_t *);
75 
76 static int nor_scan_media(device_t, struct nor_chip *);
77 
78 CFATTACH_DECL_NEW(nor, sizeof(struct nor_softc),
79     nor_match, nor_attach, nor_detach, NULL);
80 
81 #ifdef NOR_DEBUG
82 int	nordebug = NOR_DEBUG;
83 #endif
84 
85 int nor_cachesync_timeout = 1;
86 int nor_cachesync_nodenum;
87 
88 struct flash_interface nor_flash_if = {
89 	.type = FLASH_TYPE_NOR,
90 
91 	.read = nor_flash_read,
92 	.write = nor_flash_write,
93 	.erase = nor_flash_erase,
94 	.block_isbad = nor_flash_isbad,
95 	.block_markbad = nor_flash_markbad,
96 
97 	.submit = nor_flash_submit
98 };
99 
100 #ifdef NOR_VERBOSE
101 const struct nor_manufacturer nor_mfrs[] = {
102 	{ NOR_MFR_AMD,		"AMD" },
103 	{ NOR_MFR_FUJITSU,	"Fujitsu" },
104 	{ NOR_MFR_RENESAS,	"Renesas" },
105 	{ NOR_MFR_STMICRO,	"ST Micro" },
106 	{ NOR_MFR_MICRON,	"Micron" },
107 	{ NOR_MFR_NATIONAL,	"National" },
108 	{ NOR_MFR_TOSHIBA,	"Toshiba" },
109 	{ NOR_MFR_HYNIX,	"Hynix" },
110 	{ NOR_MFGR_MACRONIX,	"Macronix" },
111 	{ NOR_MFR_SAMSUNG,	"Samsung" },
112 	{ NOR_MFR_UNKNOWN,	"Unknown" }
113 };
114 
115 static const char *
116 nor_midtoname(int id)
117 {
118 	int i;
119 
120 	for (i = 0; nor_mfrs[i].id != 0; i++) {
121 		if (nor_mfrs[i].id == id)
122 			return nor_mfrs[i].name;
123 	}
124 
125 	KASSERT(nor_mfrs[i].id == 0);
126 
127 	return nor_mfrs[i].name;
128 }
129 #endif
130 
131 /* ARGSUSED */
132 static int
133 nor_match(device_t parent, cfdata_t match, void *aux)
134 {
135 	/* pseudo device, always attaches */
136 	return 1;
137 }
138 
139 static void
140 nor_attach(device_t parent, device_t self, void *aux)
141 {
142 	struct nor_softc * const sc = device_private(self);
143 	struct nor_attach_args * const naa = aux;
144 	struct nor_chip * const chip = &sc->sc_chip;
145 
146 	sc->sc_dev = self;
147 	sc->sc_controller_dev = parent;
148 	sc->sc_nor_if = naa->naa_nor_if;
149 
150 	aprint_naive("\n");
151 	aprint_normal("\n");
152 
153 	if (nor_scan_media(self, chip))
154 		return;
155 
156 	sc->sc_flash_if = nor_flash_if;
157 	sc->sc_flash_if.erasesize = chip->nc_block_size;
158 	sc->sc_flash_if.page_size = chip->nc_page_size;
159 	sc->sc_flash_if.writesize = chip->nc_page_size;
160 
161 	/* allocate cache */
162 #ifdef NOTYET
163 	chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
164 #endif
165 	chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
166 
167 	mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
168 
169 	if (flash_sync_thread_init(&sc->sc_flash_io, self, &sc->sc_flash_if)) {
170 		goto error;
171 	}
172 
173 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nor_shutdown))
174 		aprint_error_dev(sc->sc_dev,
175 		    "couldn't establish power handler\n");
176 
177 #ifdef NOR_BBT
178 	nor_bbt_init(self);
179 	nor_bbt_scan(self);
180 #endif
181 
182 	/*
183 	 * Attach all our devices
184 	 */
185 	config_search(self, NULL,
186 	    CFARG_SEARCH, nor_search,
187 	    CFARG_EOL);
188 
189 	return;
190 
191 error:
192 #ifdef NOTET
193 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
194 #endif
195 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
196 	mutex_destroy(&sc->sc_device_lock);
197 }
198 
199 static int
200 nor_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
201 {
202 	struct nor_softc * const sc = device_private(parent);
203 	struct nor_chip * const chip = &sc->sc_chip;
204 	struct flash_attach_args faa;
205 
206 	faa.partinfo.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
207 
208 	if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
209 		faa.partinfo.part_size =
210 		    chip->nc_size - faa.partinfo.part_offset;
211 	} else {
212 		faa.partinfo.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
213 	}
214 
215 	if (cf->cf_loc[FLASHBUSCF_READONLY])
216 		faa.partinfo.part_flags = FLASH_PART_READONLY;
217 	else
218 		faa.partinfo.part_flags = 0;
219 
220 	faa.flash_if = &sc->sc_flash_if;
221 
222 	if (config_probe(parent, cf, &faa)) {
223 		if (config_attach(parent, cf, &faa, nor_print,
224 				  CFARG_EOL) != NULL) {
225 			return 0;
226 		} else {
227 			return 1;
228 		}
229 	}
230 
231 	return 1;
232 }
233 
234 static int
235 nor_detach(device_t self, int flags)
236 {
237 	struct nor_softc * const sc = device_private(self);
238 	struct nor_chip * const chip = &sc->sc_chip;
239 	int error = 0;
240 
241 	error = config_detach_children(self, flags);
242 	if (error) {
243 		return error;
244 	}
245 
246 	flash_sync_thread_destroy(&sc->sc_flash_io);
247 #ifdef NOR_BBT
248 	nor_bbt_detach(self);
249 #endif
250 #ifdef NOTET
251 	/* free oob cache */
252 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
253 #endif
254 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
255 
256 	mutex_destroy(&sc->sc_device_lock);
257 
258 	pmf_device_deregister(sc->sc_dev);
259 
260 	return error;
261 }
262 
263 static int
264 nor_print(void *aux, const char *pnp)
265 {
266 	if (pnp != NULL)
267 		aprint_normal("nor at %s\n", pnp);
268 
269 	return UNCONF;
270 }
271 
272 /* ask for a nor driver to attach to the controller */
273 device_t
274 nor_attach_mi(struct nor_interface * const nor_if, device_t parent)
275 {
276 	struct nor_attach_args arg;
277 
278 	KASSERT(nor_if != NULL);
279 
280 	if (nor_if->select == NULL)
281 		nor_if->select = &nor_default_select;
282 	if (nor_if->read_page == NULL)
283 		nor_if->read_page = &nor_default_read_page;
284 	if (nor_if->program_page == NULL)
285 		nor_if->program_page = &nor_default_program_page;
286 
287 	arg.naa_nor_if = nor_if;
288 
289 	device_t dev = config_found(parent, &arg, nor_print,
290 	    CFARG_IATTR, "norbus",
291 	    CFARG_EOL);
292 
293 	return dev;
294 }
295 
296 static void
297 nor_default_select(device_t self, bool n)
298 {
299 	/* do nothing */
300 	return;
301 }
302 
303 static int
304 nor_flash_submit(device_t self, buf_t * const bp)
305 {
306 	struct nor_softc * const sc = device_private(self);
307 
308 	return flash_io_submit(&sc->sc_flash_io, bp);
309 }
310 
311 
312 /* default everything to reasonable values, to ease future api changes */
313 void
314 nor_init_interface(struct nor_interface * const nor_if)
315 {
316 	nor_if->select = &nor_default_select;
317 	nor_if->read_1 = NULL;
318 	nor_if->read_2 = NULL;
319 	nor_if->read_4 = NULL;
320 	nor_if->read_buf_1 = NULL;
321 	nor_if->read_buf_2 = NULL;
322 	nor_if->read_buf_4 = NULL;
323 	nor_if->write_1 = NULL;
324 	nor_if->write_2 = NULL;
325 	nor_if->write_4 = NULL;
326 	nor_if->write_buf_1 = NULL;
327 	nor_if->write_buf_2 = NULL;
328 	nor_if->write_buf_4 = NULL;
329 	nor_if->busy = NULL;
330 }
331 
332 #ifdef NOTYET
333 /* handle quirks here */
334 static void
335 nor_quirks(device_t self, struct nor_chip * const chip)
336 {
337 	/* this is an example only! */
338 	switch (chip->nc_manf_id) {
339 	case NOR_MFR_SAMSUNG:
340 		if (chip->nc_dev_id == 0x00) {
341 			/* do something only samsung chips need */
342 			/* or */
343 			/* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
344 		}
345 	}
346 
347 	return;
348 }
349 #endif
350 
351 /**
352  * scan media to determine the chip's properties
353  * this function resets the device
354  */
355 static int
356 nor_scan_media(device_t self, struct nor_chip * const chip)
357 {
358 	struct nor_softc * const sc = device_private(self);
359 	char pbuf[3][sizeof("XXXX MB")];
360 
361 	KASSERT(sc->sc_nor_if != NULL);
362 	KASSERT(sc->sc_nor_if->scan_media != NULL);
363 	int error = sc->sc_nor_if->scan_media(self, chip);
364 	if (error != 0)
365 		return error;
366 
367 #ifdef NOR_VERBOSE
368 	aprint_normal_dev(self,
369 	    "manufacturer id: 0x%.4x (%s), device id: 0x%.4x\n",
370 	    chip->nc_manf_id,
371 	    nor_midtoname(chip->nc_manf_id),
372 	    chip->nc_dev_id);
373 #endif
374 
375 	format_bytes(pbuf[0], sizeof(pbuf[0]), chip->nc_page_size);
376 	format_bytes(pbuf[1], sizeof(pbuf[1]), chip->nc_spare_size);
377 	format_bytes(pbuf[2], sizeof(pbuf[2]), chip->nc_block_size);
378 	aprint_normal_dev(self,
379 	    "page size: %s, spare size: %s, block size: %s\n",
380 	    pbuf[0], pbuf[1], pbuf[2]);
381 
382 	format_bytes(pbuf[0], sizeof(pbuf[0]), chip->nc_size);
383 	aprint_normal_dev(self,
384 	    "LUN size: %" PRIu32 " blocks, LUNs: %" PRIu8
385 	    ", total storage size: %s\n",
386 	    chip->nc_lun_blocks, chip->nc_num_luns, pbuf[0]);
387 
388 #ifdef NOTYET
389 	/* XXX does this apply to nor? */
390 	/*
391 	 * calculate badblock marker offset in oob
392 	 * we try to be compatible with linux here
393 	 */
394 	if (chip->nc_page_size > 512)
395 		chip->nc_badmarker_offs = 0;
396 	else
397 		chip->nc_badmarker_offs = 5;
398 #endif
399 
400 	/* Calculate page shift and mask */
401 	chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
402 	chip->nc_page_mask = ~(chip->nc_page_size - 1);
403 	/* same for block */
404 	chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
405 	chip->nc_block_mask = ~(chip->nc_block_size - 1);
406 
407 #ifdef NOTYET
408 	/* look for quirks here if needed in future */
409 	nor_quirks(self, chip);
410 #endif
411 
412 	return 0;
413 }
414 
415 /* ARGSUSED */
416 static bool
417 nor_shutdown(device_t self, int howto)
418 {
419 	return true;
420 }
421 
422 /* implementation of the block device API */
423 
424 /* read a page, default implementation */
425 static int
426 nor_default_read_page(device_t self, flash_off_t offset, uint8_t * const data)
427 {
428 	struct nor_softc * const sc = device_private(self);
429 	struct nor_chip * const chip = &sc->sc_chip;
430 
431 	/*
432 	 * access by specified access_width
433 	 * note: #bits == 1 << width
434 	 */
435 	switch(sc->sc_nor_if->access_width) {
436 	case 0:
437 		nor_read_buf_1(self, offset, data, chip->nc_page_size);
438 		break;
439 	case 1:
440 		nor_read_buf_2(self, offset, data, chip->nc_page_size);
441 		break;
442 	case 2:
443 		nor_read_buf_4(self, offset, data, chip->nc_page_size);
444 		break;
445 #ifdef NOTYET
446 	case 3:
447 		nor_read_buf_8(self, offset, data, chip->nc_page_size);
448 		break;
449 #endif
450 	default:
451 		panic("%s: bad width %d\n", __func__, sc->sc_nor_if->access_width);
452 	}
453 
454 #if 0
455 	/* for debugging new drivers */
456 	nor_dump_data("page", data, chip->nc_page_size);
457 #endif
458 
459 	return 0;
460 }
461 
462 /* write a page, default implementation */
463 static int
464 nor_default_program_page(device_t self, flash_off_t offset,
465     const uint8_t * const data)
466 {
467 	struct nor_softc * const sc = device_private(self);
468 	struct nor_chip * const chip = &sc->sc_chip;
469 
470 	/*
471 	 * access by specified width
472 	 * #bits == 1 << access_width
473 	 */
474 	switch(sc->sc_nor_if->access_width) {
475 	case 0:
476 		nor_write_buf_1(self, offset, data, chip->nc_page_size);
477 		break;
478 	case 1:
479 		nor_write_buf_2(self, offset, data, chip->nc_page_size);
480 		break;
481 	case 2:
482 		nor_write_buf_4(self, offset, data, chip->nc_page_size);
483 		break;
484 #ifdef NOTYET
485 	case 3:
486 		nor_write_buf_8(self, offset, data, chip->nc_page_size);
487 		break;
488 #endif
489 	default:
490 		panic("%s: bad width %d\n", __func__,
491 			sc->sc_nor_if->access_width);
492 	}
493 
494 #if 0
495 	/* for debugging new drivers */
496 	nor_dump_data("page", data, chip->nc_page_size);
497 #endif
498 
499 	return 0;
500 }
501 
502 /*
503  * nor_flash_erase_all - erase the entire chip
504  *
505  * XXX a good way to brick your system
506  */
507 static int
508 nor_flash_erase_all(device_t self)
509 {
510 	struct nor_softc * const sc = device_private(self);
511 	int error;
512 
513 	mutex_enter(&sc->sc_device_lock);
514 	error = nor_erase_all(self);
515 	mutex_exit(&sc->sc_device_lock);
516 
517 	return error;
518 }
519 
520 static int
521 nor_flash_erase(device_t self, struct flash_erase_instruction * const ei)
522 {
523 	struct nor_softc * const sc = device_private(self);
524 	struct nor_chip * const chip = &sc->sc_chip;
525 	flash_off_t addr;
526 	int error = 0;
527 
528 	if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
529 		return EINVAL;
530 
531 	if (ei->ei_addr + ei->ei_len > chip->nc_size) {
532 		DPRINTF(("%s: erase address is past the end"
533 			" of the device\n", __func__));
534 		return EINVAL;
535 	}
536 
537 	if ((ei->ei_addr == 0) && (ei->ei_len == chip->nc_size)
538 	&&  (sc->sc_nor_if->erase_all != NULL)) {
539 		return nor_flash_erase_all(self);
540 	}
541 
542 	if (ei->ei_addr % chip->nc_block_size != 0) {
543 		aprint_error_dev(self,
544 		    "nor_flash_erase: ei_addr (%ju) is not"
545 		    " a multiple of block size (%ju)\n",
546 		    (uintmax_t)ei->ei_addr,
547 		    (uintmax_t)chip->nc_block_size);
548 		return EINVAL;
549 	}
550 
551 	if (ei->ei_len % chip->nc_block_size != 0) {
552 		aprint_error_dev(self,
553 		    "nor_flash_erase: ei_len (%ju) is not"
554 		    " a multiple of block size (%ju)",
555 		    (uintmax_t)ei->ei_len,
556 		    (uintmax_t)chip->nc_block_size);
557 		return EINVAL;
558 	}
559 
560 	mutex_enter(&sc->sc_device_lock);
561 	addr = ei->ei_addr;
562 	while (addr < ei->ei_addr + ei->ei_len) {
563 #ifdef NOTYET
564 		if (nor_isbad(self, addr)) {
565 			aprint_error_dev(self, "bad block encountered\n");
566 			ei->ei_state = FLASH_ERASE_FAILED;
567 			error = EIO;
568 			goto out;
569 		}
570 #endif
571 
572 		error = nor_erase_block(self, addr);
573 		if (error) {
574 			ei->ei_state = FLASH_ERASE_FAILED;
575 			goto out;
576 		}
577 
578 		addr += chip->nc_block_size;
579 	}
580 	mutex_exit(&sc->sc_device_lock);
581 
582 	ei->ei_state = FLASH_ERASE_DONE;
583 	if (ei->ei_callback != NULL) {
584 		ei->ei_callback(ei);
585 	}
586 
587 	return 0;
588 out:
589 	mutex_exit(&sc->sc_device_lock);
590 
591 	return error;
592 }
593 
594 /*
595  * handle (page) unaligned write to nor
596  */
597 static int
598 nor_flash_write_unaligned(device_t self, flash_off_t offset, size_t len,
599     size_t * const retlen, const uint8_t * const buf)
600 {
601 	struct nor_softc * const sc = device_private(self);
602 	struct nor_chip * const chip = &sc->sc_chip;
603 	flash_off_t first, last, firstoff;
604 	const uint8_t *bufp;
605 	flash_off_t addr;
606 	size_t left, count;
607 	int error = 0, i;
608 
609 	first = offset & chip->nc_page_mask;
610 	firstoff = offset & ~chip->nc_page_mask;
611 	/* XXX check if this should be len - 1 */
612 	last = (offset + len) & chip->nc_page_mask;
613 	count = last - first + 1;
614 
615 	addr = first;
616 	*retlen = 0;
617 
618 	mutex_enter(&sc->sc_device_lock);
619 	if (count == 1) {
620 #ifdef NOTYET
621 		if (nor_isbad(self, addr)) {
622 			aprint_error_dev(self,
623 			    "nor_flash_write_unaligned: "
624 			    "bad block encountered\n");
625 			error = EIO;
626 			goto out;
627 		}
628 #endif
629 
630 		error = nor_read_page(self, addr, chip->nc_page_cache);
631 		if (error) {
632 			goto out;
633 		}
634 
635 		memcpy(chip->nc_page_cache + firstoff, buf, len);
636 
637 		error = nor_program_page(self, addr, chip->nc_page_cache);
638 		if (error) {
639 			goto out;
640 		}
641 
642 		*retlen = len;
643 		goto out;
644 	}
645 
646 	bufp = buf;
647 	left = len;
648 
649 	for (i = 0; i < count && left != 0; i++) {
650 #ifdef NOTYET
651 		if (nor_isbad(self, addr)) {
652 			aprint_error_dev(self,
653 			    "nor_flash_write_unaligned: "
654 			    "bad block encountered\n");
655 			error = EIO;
656 			goto out;
657 		}
658 #endif
659 
660 		if (i == 0) {
661 			error = nor_read_page(self, addr, chip->nc_page_cache);
662 			if (error) {
663 				goto out;
664 			}
665 
666 			memcpy(chip->nc_page_cache + firstoff,
667 			    bufp, chip->nc_page_size - firstoff);
668 
669 			printf("write page: %s: %d\n", __FILE__, __LINE__);
670 			error = nor_program_page(self, addr,
671 				chip->nc_page_cache);
672 			if (error) {
673 				goto out;
674 			}
675 
676 			bufp += chip->nc_page_size - firstoff;
677 			left -= chip->nc_page_size - firstoff;
678 			*retlen += chip->nc_page_size - firstoff;
679 
680 		} else if (i == count - 1) {
681 			error = nor_read_page(self, addr, chip->nc_page_cache);
682 			if (error) {
683 				goto out;
684 			}
685 
686 			memcpy(chip->nc_page_cache, bufp, left);
687 
688 			error = nor_program_page(self, addr,
689 				chip->nc_page_cache);
690 			if (error) {
691 				goto out;
692 			}
693 
694 			*retlen += left;
695 			KASSERT(left < chip->nc_page_size);
696 
697 		} else {
698 			/* XXX debug */
699 			if (left > chip->nc_page_size) {
700 				printf("left: %zu, i: %d, count: %zu\n",
701 				    (size_t )left, i, count);
702 			}
703 			KASSERT(left > chip->nc_page_size);
704 
705 			error = nor_program_page(self, addr, bufp);
706 			if (error) {
707 				goto out;
708 			}
709 
710 			bufp += chip->nc_page_size;
711 			left -= chip->nc_page_size;
712 			*retlen += chip->nc_page_size;
713 		}
714 
715 		addr += chip->nc_page_size;
716 	}
717 
718 	KASSERT(*retlen == len);
719 out:
720 	mutex_exit(&sc->sc_device_lock);
721 
722 	return error;
723 }
724 
725 static int
726 nor_flash_write(device_t self, flash_off_t offset, size_t len,
727     size_t * const retlen, const uint8_t * const buf)
728 {
729 	struct nor_softc * const sc = device_private(self);
730 	struct nor_chip * const chip = &sc->sc_chip;
731 	const uint8_t *bufp;
732 	size_t pages, page;
733 	daddr_t addr;
734 	int error = 0;
735 
736 	if ((offset + len) > chip->nc_size) {
737 		DPRINTF(("%s: write (off: 0x%jx, len: %ju),"
738 			" exceeds device size (0x%jx)\n", __func__,
739 			(uintmax_t)offset, (uintmax_t)len,
740 			(uintmax_t)chip->nc_size));
741 		return EINVAL;
742 	}
743 
744 	if (len % chip->nc_page_size != 0 ||
745 	    offset % chip->nc_page_size != 0) {
746 		return nor_flash_write_unaligned(self,
747 		    offset, len, retlen, buf);
748 	}
749 
750 	pages = len / chip->nc_page_size;
751 	KASSERT(pages != 0);
752 	*retlen = 0;
753 
754 	addr = offset;
755 	bufp = buf;
756 
757 	mutex_enter(&sc->sc_device_lock);
758 	for (page = 0; page < pages; page++) {
759 #ifdef NOTYET
760 		/* do we need this check here? */
761 		if (nor_isbad(self, addr)) {
762 			aprint_error_dev(self,
763 			    "nor_flash_write: bad block encountered\n");
764 
765 			error = EIO;
766 			goto out;
767 		}
768 #endif
769 
770 		error = nor_program_page(self, addr, bufp);
771 		if (error) {
772 			goto out;
773 		}
774 
775 		addr += chip->nc_page_size;
776 		bufp += chip->nc_page_size;
777 		*retlen += chip->nc_page_size;
778 	}
779 out:
780 	mutex_exit(&sc->sc_device_lock);
781 	DPRINTF(("%s: retlen: %zu, len: %zu\n", __func__, *retlen, len));
782 
783 	return error;
784 }
785 
786 /*
787  * handle (page) unaligned read from nor
788  */
789 static int
790 nor_flash_read_unaligned(device_t self, flash_off_t offset, size_t len,
791     size_t * const retlen, uint8_t * const buf)
792 {
793 	struct nor_softc * const sc = device_private(self);
794 	struct nor_chip * const chip = &sc->sc_chip;
795 	daddr_t first, last, count, firstoff;
796 	uint8_t *bufp;
797 	daddr_t addr;
798 	size_t left;
799 	int error = 0, i;
800 
801 	first = offset & chip->nc_page_mask;
802 	firstoff = offset & ~chip->nc_page_mask;
803 	last = (offset + len) & chip->nc_page_mask;
804 	count = (last - first) / chip->nc_page_size + 1;
805 
806 	addr = first;
807 	bufp = buf;
808 	left = len;
809 	*retlen = 0;
810 
811 	mutex_enter(&sc->sc_device_lock);
812 	if (count == 1) {
813 		error = nor_read_page(self, addr, chip->nc_page_cache);
814 		if (error) {
815 			goto out;
816 		}
817 
818 		memcpy(bufp, chip->nc_page_cache + firstoff, len);
819 
820 		*retlen = len;
821 		goto out;
822 	}
823 
824 	for (i = 0; i < count && left != 0; i++) {
825 		/* XXX Why use the page cache here ? */
826 		error = nor_read_page(self, addr, chip->nc_page_cache);
827 		if (error) {
828 			goto out;
829 		}
830 
831 		if (i == 0) {
832 			memcpy(bufp, chip->nc_page_cache + firstoff,
833 			    chip->nc_page_size - firstoff);
834 
835 			bufp += chip->nc_page_size - firstoff;
836 			left -= chip->nc_page_size - firstoff;
837 			*retlen += chip->nc_page_size - firstoff;
838 
839 		} else if (i == count - 1) {
840 			memcpy(bufp, chip->nc_page_cache, left);
841 			*retlen += left;
842 			KASSERT(left < chip->nc_page_size);
843 
844 		} else {
845 			memcpy(bufp, chip->nc_page_cache, chip->nc_page_size);
846 
847 			bufp += chip->nc_page_size;
848 			left -= chip->nc_page_size;
849 			*retlen += chip->nc_page_size;
850 		}
851 
852 		addr += chip->nc_page_size;
853 	}
854 	KASSERT(*retlen == len);
855 out:
856 	mutex_exit(&sc->sc_device_lock);
857 
858 	return error;
859 }
860 
861 static int
862 nor_flash_read(device_t self, flash_off_t offset, size_t len,
863     size_t * const retlen, uint8_t * const buf)
864 {
865 	struct nor_softc * const sc = device_private(self);
866 	struct nor_chip * const chip = &sc->sc_chip;
867 	uint8_t *bufp;
868 	size_t addr;
869 	size_t i, pages;
870 	int error = 0;
871 
872 	*retlen = 0;
873 
874 	DPRINTF(("%s: off: 0x%jx, len: %zu\n",
875 		__func__, (uintmax_t)offset, len));
876 
877 	if (__predict_false((offset + len) > chip->nc_size)) {
878 		DPRINTF(("%s: read (off: 0x%jx, len: %zu),"
879 			" exceeds device size (%ju)\n", __func__,
880 			(uintmax_t)offset, len, (uintmax_t)chip->nc_size));
881 		return EINVAL;
882 	}
883 
884 	/* Handle unaligned access, shouldnt be needed when using the
885 	 * block device, as strategy handles it, so only low level
886 	 * accesses will use this path
887 	 */
888 	/* XXX^2 */
889 #if 0
890 	if (len < chip->nc_page_size)
891 		panic("TODO page size is larger than read size");
892 #endif
893 
894 	if (len % chip->nc_page_size != 0 ||
895 	    offset % chip->nc_page_size != 0) {
896 		return nor_flash_read_unaligned(self,
897 		    offset, len, retlen, buf);
898 	}
899 
900 	bufp = buf;
901 	addr = offset;
902 	pages = len / chip->nc_page_size;
903 
904 	mutex_enter(&sc->sc_device_lock);
905 	for (i = 0; i < pages; i++) {
906 #ifdef NOTYET
907 		/* XXX do we need this check here? */
908 		if (nor_isbad(self, addr)) {
909 			aprint_error_dev(self, "bad block encountered\n");
910 			error = EIO;
911 			goto out;
912 		}
913 #endif
914 		error = nor_read_page(self, addr, bufp);
915 		if (error)
916 			goto out;
917 
918 		bufp += chip->nc_page_size;
919 		addr += chip->nc_page_size;
920 		*retlen += chip->nc_page_size;
921 	}
922 out:
923 	mutex_exit(&sc->sc_device_lock);
924 
925 	return error;
926 }
927 
928 static int
929 nor_flash_isbad(device_t self, flash_off_t ofs, bool * const isbad)
930 {
931 	struct nor_softc * const sc = device_private(self);
932 	struct nor_chip * const chip = &sc->sc_chip;
933 #ifdef NOTYET
934 	bool result;
935 #endif
936 
937 	if (ofs > chip->nc_size) {
938 		DPRINTF(("%s: offset 0x%jx is larger than"
939 			" device size (0x%jx)\n", __func__,
940 			(uintmax_t)ofs, (uintmax_t)chip->nc_size));
941 		return EINVAL;
942 	}
943 
944 	if (ofs % chip->nc_block_size != 0) {
945 		DPRINTF(("offset (0x%jx) is not the multiple of block size "
946 			"(%ju)",
947 			(uintmax_t)ofs, (uintmax_t)chip->nc_block_size));
948 		return EINVAL;
949 	}
950 
951 #ifdef NOTYET
952 	mutex_enter(&sc->sc_device_lock);
953 	result = nor_isbad(self, ofs);
954 	mutex_exit(&sc->sc_device_lock);
955 
956 	*isbad = result;
957 #else
958 	*isbad = false;
959 #endif
960 
961 	return 0;
962 }
963 
964 static int
965 nor_flash_markbad(device_t self, flash_off_t ofs)
966 {
967 	struct nor_softc * const sc = device_private(self);
968 	struct nor_chip * const chip = &sc->sc_chip;
969 
970 	if (ofs > chip->nc_size) {
971 		DPRINTF(("%s: offset 0x%jx is larger than"
972 			" device size (0x%jx)\n", __func__,
973 			ofs, (uintmax_t)chip->nc_size));
974 		return EINVAL;
975 	}
976 
977 	if (ofs % chip->nc_block_size != 0) {
978 		panic("offset (%ju) is not the multiple of block size (%ju)",
979 		    (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
980 	}
981 
982 	/* TODO: implement this */
983 
984 	return 0;
985 }
986 
987 static int
988 sysctl_nor_verify(SYSCTLFN_ARGS)
989 {
990 	int error, t;
991 	struct sysctlnode node;
992 
993 	node = *rnode;
994 	t = *(int *)rnode->sysctl_data;
995 	node.sysctl_data = &t;
996 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
997 	if (error || newp == NULL)
998 		return error;
999 
1000 	if (node.sysctl_num == nor_cachesync_nodenum) {
1001 		if (t <= 0 || t > 60)
1002 			return EINVAL;
1003 	} else {
1004 		return EINVAL;
1005 	}
1006 
1007 	*(int *)rnode->sysctl_data = t;
1008 
1009 	return 0;
1010 }
1011 
1012 SYSCTL_SETUP(sysctl_nor, "sysctl nor subtree setup")
1013 {
1014 	int rc, nor_root_num;
1015 	const struct sysctlnode *node;
1016 
1017 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
1018 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nor",
1019 	    SYSCTL_DESCR("NOR driver controls"),
1020 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
1021 		goto error;
1022 	}
1023 
1024 	nor_root_num = node->sysctl_num;
1025 
1026 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
1027 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1028 	    CTLTYPE_INT, "cache_sync_timeout",
1029 	    SYSCTL_DESCR("NOR write cache sync timeout in seconds"),
1030 	    sysctl_nor_verify, 0, &nor_cachesync_timeout,
1031 	    0, CTL_HW, nor_root_num, CTL_CREATE,
1032 	    CTL_EOL)) != 0) {
1033 		goto error;
1034 	}
1035 
1036 	nor_cachesync_nodenum = node->sysctl_num;
1037 
1038 	return;
1039 
1040 error:
1041 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
1042 }
1043 
1044 MODULE(MODULE_CLASS_DRIVER, nor, "flash");
1045 
1046 #ifdef _MODULE
1047 #include "ioconf.c"
1048 #endif
1049 
1050 static int
1051 nor_modcmd(modcmd_t cmd, void *opaque)
1052 {
1053 	switch (cmd) {
1054 	case MODULE_CMD_INIT:
1055 #ifdef _MODULE
1056 		return config_init_component(cfdriver_ioconf_nor,
1057 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
1058 #else
1059 		return 0;
1060 #endif
1061 	case MODULE_CMD_FINI:
1062 #ifdef _MODULE
1063 		return config_fini_component(cfdriver_ioconf_nor,
1064 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
1065 #else
1066 		return 0;
1067 #endif
1068 	default:
1069 		return ENOTTY;
1070 	}
1071 }
1072