xref: /netbsd-src/sys/dev/nor/nor.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: nor.c,v 1.1 2011/06/22 21:59:15 ahoka 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.1 2011/06/22 21:59:15 ahoka Exp $");
38 
39 #include "locators.h"
40 
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/device.h>
44 #include <sys/kmem.h>
45 #include <sys/sysctl.h>
46 #include <sys/atomic.h>
47 
48 #include <dev/flash/flash.h>
49 #include <dev/nor/nor.h>
50 #include <dev/nor/cfi.h>
51 
52 //#include "opt_nor.h"
53 
54 int nor_match(device_t, cfdata_t, void *);
55 void nor_attach(device_t, device_t, void *);
56 int nor_detach(device_t, int);
57 bool nor_shutdown(device_t, int);
58 
59 int nor_print(void *, const char *);
60 static int nor_search(device_t, cfdata_t, const int *, void *);
61 
62 CFATTACH_DECL_NEW(nor, sizeof(struct nor_softc),
63     nor_match, nor_attach, nor_detach, NULL);
64 
65 #ifdef NOR_DEBUG
66 int	nordebug = NOR_DEBUG;
67 #endif
68 
69 int nor_cachesync_timeout = 1;
70 int nor_cachesync_nodenum;
71 
72 #ifdef NOR_VERBOSE
73 const struct nor_manufacturer nor_mfrs[] = {
74 	{ NOR_MFR_AMD,		"AMD" },
75 	{ NOR_MFR_FUJITSU,	"Fujitsu" },
76 	{ NOR_MFR_RENESAS,	"Renesas" },
77 	{ NOR_MFR_STMICRO,	"ST Micro" },
78 	{ NOR_MFR_MICRON,	"Micron" },
79 	{ NOR_MFR_NATIONAL,	"National" },
80 	{ NOR_MFR_TOSHIBA,	"Toshiba" },
81 	{ NOR_MFR_HYNIX,	"Hynix" },
82 	{ NOR_MFR_SAMSUNG,	"Samsung" },
83 	{ NOR_MFR_UNKNOWN,	"Unknown" }
84 };
85 
86 static const char *
87 nor_midtoname(int id)
88 {
89 	int i;
90 
91 	for (i = 0; nor_mfrs[i].id != 0; i++) {
92 		if (nor_mfrs[i].id == id)
93 			return nor_mfrs[i].name;
94 	}
95 
96 	KASSERT(nor_mfrs[i].id == 0);
97 
98 	return nor_mfrs[i].name;
99 }
100 #endif
101 
102 /* ARGSUSED */
103 int
104 nor_match(device_t parent, cfdata_t match, void *aux)
105 {
106 	/* pseudo device, always attaches */
107 	return 1;
108 }
109 
110 void
111 nor_attach(device_t parent, device_t self, void *aux)
112 {
113 	struct nor_softc *sc = device_private(self);
114 	struct nor_attach_args *naa = aux;
115 	struct nor_chip *chip = &sc->sc_chip;
116 
117 	sc->sc_dev = self;
118 	sc->controller_dev = parent;
119 	sc->nor_if = naa->naa_nor_if;
120 
121 	aprint_naive("\n");
122 
123 	/* allocate cache */
124 	chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
125 	chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
126 
127 	mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
128 
129 	if (nor_sync_thread_start(self)) {
130 		goto error;
131 	}
132 
133 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nor_shutdown))
134 		aprint_error_dev(sc->sc_dev,
135 		    "couldn't establish power handler\n");
136 
137 #ifdef NOR_BBT
138 	nor_bbt_init(self);
139 	nor_bbt_scan(self);
140 #endif
141 
142 	/*
143 	 * Attach all our devices
144 	 */
145 	config_search_ia(nor_search, self, NULL, NULL);
146 
147 	return;
148 error:
149 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
150 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
151 	mutex_destroy(&sc->sc_device_lock);
152 }
153 
154 static int
155 nor_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
156 {
157 	struct nor_softc *sc = device_private(parent);
158 	struct nor_chip *chip = &sc->sc_chip;
159 	struct flash_interface *flash_if;
160 	struct flash_attach_args faa;
161 
162 	flash_if = kmem_alloc(sizeof(*flash_if), KM_SLEEP);
163 
164 	flash_if->type = FLASH_TYPE_NOR;
165 
166 	flash_if->read = nor_flash_read;
167 	flash_if->write = nor_flash_write;
168 	flash_if->erase = nor_flash_erase;
169 	flash_if->block_isbad = nor_flash_isbad;
170 	flash_if->block_markbad = nor_flash_markbad;
171 
172 	flash_if->submit = nor_io_submit;
173 
174 	flash_if->erasesize = chip->nc_block_size;
175 	flash_if->page_size = chip->nc_page_size;
176 	flash_if->writesize = chip->nc_page_size;
177 
178 	flash_if->partition.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
179 
180 	if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
181 		flash_if->size = chip->nc_size -
182 		    flash_if->partition.part_offset;
183 		flash_if->partition.part_size = flash_if->size;
184 	} else {
185 		flash_if->size = cf->cf_loc[FLASHBUSCF_SIZE];
186 		flash_if->partition.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
187 	}
188 
189 	if (cf->cf_loc[FLASHBUSCF_READONLY])
190 		flash_if->partition.part_flags = FLASH_PART_READONLY;
191 	else
192 		flash_if->partition.part_flags = 0;
193 
194 	faa.flash_if = flash_if;
195 
196 	if (config_match(parent, cf, &faa)) {
197 		if (config_attach(parent, cf, &faa, nor_print) != NULL) {
198 			return 0;
199 		} else {
200 			return 1;
201 		}
202 	} else {
203 		kmem_free(flash_if, sizeof(*flash_if));
204 	}
205 
206 	return 1;
207 }
208 
209 int
210 nor_detach(device_t self, int flags)
211 {
212 	struct nor_softc *sc = device_private(self);
213 	struct nor_chip *chip = &sc->sc_chip;
214 	int error = 0;
215 
216 	error = config_detach_children(self, flags);
217 	if (error) {
218 		return error;
219 	}
220 
221 	nor_sync_thread_stop(self);
222 #ifdef NOR_BBT
223 	nor_bbt_detach(self);
224 #endif
225 	/* free oob cache */
226 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
227 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
228 
229 	mutex_destroy(&sc->sc_device_lock);
230 
231 	pmf_device_deregister(sc->sc_dev);
232 
233 	return error;
234 }
235 
236 int
237 nor_print(void *aux, const char *pnp)
238 {
239 	if (pnp != NULL)
240 		aprint_normal("nor at %s\n", pnp);
241 
242 	return UNCONF;
243 }
244 
245 /* ask for a nor driver to attach to the controller */
246 device_t
247 nor_attach_mi(struct nor_interface *nor_if, device_t parent)
248 {
249 	struct nor_attach_args arg;
250 
251 	KASSERT(nor_if != NULL);
252 
253 	arg.naa_nor_if = nor_if;
254 	return config_found_ia(parent, "norbus", &arg, nor_print);
255 }
256 
257 /* default everything to reasonable values, to ease future api changes */
258 void
259 nor_init_interface(struct nor_interface *interface)
260 {
261 	interface->select = &nor_default_select;
262 	interface->read_1 = NULL;
263 	interface->read_2 = NULL;
264 	interface->read_4 = NULL;
265 	interface->read_buf_1 = NULL;
266 	interface->read_buf_2 = NULL;
267 	interface->read_buf_4 = NULL;
268 	interface->write_1 = NULL;
269 	interface->write_2 = NULL;
270 	interface->write_4 = NULL;
271 	interface->write_buf_1 = NULL;
272 	interface->write_buf_2 = NULL;
273 	interface->write_buf_4 = NULL;
274 	interface->busy = NULL;
275 }
276 
277 #if 0
278 /* handle quirks here */
279 static void
280 nor_quirks(device_t self, struct nor_chip *chip)
281 {
282 	/* this is an example only! */
283 	switch (chip->nc_manf_id) {
284 	case NOR_MFR_SAMSUNG:
285 		if (chip->nc_dev_id == 0x00) {
286 			/* do something only samsung chips need */
287 			/* or */
288 			/* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
289 		}
290 	}
291 
292 	return;
293 }
294 #endif
295 
296 #if 0
297 /**
298  * scan media to determine the chip's properties
299  * this function resets the device
300  */
301 static int
302 nor_scan_media(device_t self, struct nor_chip *chip)
303 {
304 	struct nor_softc *sc = device_private(self);
305 	uint8_t onfi_signature[4];
306 
307 
308 	/* TODO: enter query mode, check for signature */
309 
310 	/* TODO: get parameters in query mode */
311 
312 #ifdef NOR_VERBOSE
313 	aprint_normal_dev(self,
314 	    "manufacturer id: 0x%.2x (%s), device id: 0x%.2x\n",
315 	    chip->nc_manf_id,
316 	    nor_midtoname(chip->nc_manf_id),
317 	    chip->nc_dev_id);
318 #endif
319 
320 	aprint_normal_dev(self,
321 	    "page size: %zu bytes, spare size: %zu bytes, "
322 	    "block size: %zu bytes\n",
323 	    chip->nc_page_size, chip->nc_spare_size, chip->nc_block_size);
324 
325 	aprint_normal_dev(self,
326 	    "LUN size: %" PRIu32 " blocks, LUNs: %" PRIu8
327 	    ", total storage size: %zu MB\n",
328 	    chip->nc_lun_blocks, chip->nc_num_luns,
329 	    chip->nc_size / 1024 / 1024);
330 
331 #ifdef NOR_VERBOSE
332 	aprint_normal_dev(self, "column cycles: %" PRIu8 ", row cycles: %"
333 	    PRIu8 "\n",
334 	    chip->nc_addr_cycles_column, chip->nc_addr_cycles_row);
335 #endif
336 
337 	/* XXX does this apply to nor? */
338 	/*
339 	 * calculate badblock marker offset in oob
340 	 * we try to be compatible with linux here
341 	 */
342 	if (chip->nc_page_size > 512)
343 		chip->nc_badmarker_offs = 0;
344 	else
345 		chip->nc_badmarker_offs = 5;
346 
347 	/* Calculate page shift and mask */
348 	chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
349 	chip->nc_page_mask = ~(chip->nc_page_size - 1);
350 	/* same for block */
351 	chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
352 	chip->nc_block_mask = ~(chip->nc_block_size - 1);
353 
354 	/* look for quirks here if needed in future */
355 	/* nor_quirks(self, chip); */
356 
357 	return 0;
358 }
359 #endif
360 
361 /* ARGSUSED */
362 bool
363 nor_shutdown(device_t self, int howto)
364 {
365 	return true;
366 }
367 
368 /* implementation of the block device API */
369 
370 int
371 nor_flash_write(device_t self, flash_off_t offset, size_t len, size_t *retlen,
372     const uint8_t *buf)
373 {
374 	/* TODO: implement this based on nand.c */
375 	return 0;
376 }
377 
378 int
379 nor_flash_read(device_t self, flash_off_t offset, size_t len, size_t *retlen,
380     uint8_t *buf)
381 {
382 	/* TODO: implement this based on nand.c */
383 	return 0;
384 }
385 
386 int
387 nor_flash_isbad(device_t self, flash_off_t ofs, bool *isbad)
388 {
389 	struct nor_softc *sc = device_private(self);
390 	struct nor_chip *chip = &sc->sc_chip;
391 //	bool result;
392 
393 	if (ofs > chip->nc_size) {
394 		DPRINTF(("nor_flash_isbad: offset 0x%jx is larger than"
395 			" device size (0x%jx)\n", (uintmax_t)ofs,
396 			(uintmax_t)chip->nc_size));
397 		return EINVAL;
398 	}
399 
400 	if (ofs % chip->nc_block_size != 0) {
401 		DPRINTF(("offset (0x%jx) is not the multiple of block size "
402 			"(%ju)",
403 			(uintmax_t)ofs, (uintmax_t)chip->nc_block_size));
404 		return EINVAL;
405 	}
406 
407 	mutex_enter(&sc->sc_device_lock);
408 //	result = nor_isbad(self, ofs);
409 	mutex_exit(&sc->sc_device_lock);
410 
411 //	*isbad = result;
412 	*isbad = false;
413 
414 	return 0;
415 }
416 
417 int
418 nor_flash_markbad(device_t self, flash_off_t ofs)
419 {
420 	struct nor_softc *sc = device_private(self);
421 	struct nor_chip *chip = &sc->sc_chip;
422 
423 	if (ofs > chip->nc_size) {
424 		DPRINTF(("nor_flash_markbad: offset 0x%jx is larger than"
425 			" device size (0x%jx)\n", ofs,
426 			(uintmax_t)chip->nc_size));
427 		return EINVAL;
428 	}
429 
430 	if (ofs % chip->nc_block_size != 0) {
431 		panic("offset (%ju) is not the multiple of block size (%ju)",
432 		    (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
433 	}
434 
435 	/* TODO: implement this */
436 
437 	return 0;
438 }
439 
440 int
441 nor_flash_erase(device_t self,
442     struct flash_erase_instruction *ei)
443 {
444 	struct nor_softc *sc = device_private(self);
445 	struct nor_chip *chip = &sc->sc_chip;
446 //	flash_off_t addr;
447 //	int error = 0;
448 
449 	if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
450 		return EINVAL;
451 
452 	if (ei->ei_addr + ei->ei_len > chip->nc_size) {
453 		DPRINTF(("nor_flash_erase: erase address is over the end"
454 			" of the device\n"));
455 		return EINVAL;
456 	}
457 
458 	if (ei->ei_addr % chip->nc_block_size != 0) {
459 		aprint_error_dev(self,
460 		    "nor_flash_erase: ei_addr (%ju) is not"
461 		    "the multiple of block size (%ju)",
462 		    (uintmax_t)ei->ei_addr,
463 		    (uintmax_t)chip->nc_block_size);
464 		return EINVAL;
465 	}
466 
467 	if (ei->ei_len % chip->nc_block_size != 0) {
468 		aprint_error_dev(self,
469 		    "nor_flash_erase: ei_len (%ju) is not"
470 		    "the multiple of block size (%ju)",
471 		    (uintmax_t)ei->ei_addr,
472 		    (uintmax_t)chip->nc_block_size);
473 		return EINVAL;
474 	}
475 
476 	/* TODO: do erase here */
477 
478 	ei->ei_state = FLASH_ERASE_DONE;
479 	if (ei->ei_callback != NULL) {
480 		ei->ei_callback(ei);
481 	}
482 
483 	return 0;
484 #if 0
485 out:
486 	mutex_exit(&sc->sc_device_lock);
487 
488 	return error;
489 #endif
490 }
491 
492 static int
493 sysctl_nor_verify(SYSCTLFN_ARGS)
494 {
495 	int error, t;
496 	struct sysctlnode node;
497 
498 	node = *rnode;
499 	t = *(int *)rnode->sysctl_data;
500 	node.sysctl_data = &t;
501 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
502 	if (error || newp == NULL)
503 		return error;
504 
505 	if (node.sysctl_num == nor_cachesync_nodenum) {
506 		if (t <= 0 || t > 60)
507 			return EINVAL;
508 	} else {
509 		return EINVAL;
510 	}
511 
512 	*(int *)rnode->sysctl_data = t;
513 
514 	return 0;
515 }
516 
517 SYSCTL_SETUP(sysctl_nor, "sysctl nor subtree setup")
518 {
519 	int rc, nor_root_num;
520 	const struct sysctlnode *node;
521 
522 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
523 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
524 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
525 		goto error;
526 	}
527 
528 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
529 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nor",
530 	    SYSCTL_DESCR("NOR driver controls"),
531 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
532 		goto error;
533 	}
534 
535 	nor_root_num = node->sysctl_num;
536 
537 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
538 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
539 	    CTLTYPE_INT, "cache_sync_timeout",
540 	    SYSCTL_DESCR("NOR write cache sync timeout in seconds"),
541 	    sysctl_nor_verify, 0, &nor_cachesync_timeout,
542 	    0, CTL_HW, nor_root_num, CTL_CREATE,
543 	    CTL_EOL)) != 0) {
544 		goto error;
545 	}
546 
547 	nor_cachesync_nodenum = node->sysctl_num;
548 
549 	return;
550 
551 error:
552 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
553 }
554 
555 MODULE(MODULE_CLASS_DRIVER, nor, "flash");
556 
557 #ifdef _MODULE
558 #include "ioconf.c"
559 #endif
560 
561 static int
562 nor_modcmd(modcmd_t cmd, void *opaque)
563 {
564 	switch (cmd) {
565 	case MODULE_CMD_INIT:
566 #ifdef _MODULE
567 		return config_init_component(cfdriver_ioconf_nor,
568 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
569 #else
570 		return 0;
571 #endif
572 	case MODULE_CMD_FINI:
573 #ifdef _MODULE
574 		return config_fini_component(cfdriver_ioconf_nor,
575 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
576 #else
577 		return 0;
578 #endif
579 	default:
580 		return ENOTTY;
581 	}
582 }
583