2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2009-2012 Semihalf
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/geom_disk.h>
42 #include <dev/nand/nand.h>
43 #include <dev/nand/nandbus.h>
44 #include <dev/nand/nand_dev.h>
46 #include "nandbus_if.h"
48 #define BIO_NAND_STD ((void *)1)
49 #define BIO_NAND_RAW ((void *)2)
51 static disk_ioctl_t nand_ioctl;
52 static disk_getattr_t nand_getattr;
53 static disk_strategy_t nand_strategy;
54 static disk_strategy_t nand_strategy_raw;
57 nand_read(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
60 nand_debug(NDBG_GEOM, "Read from chip %d [%p] at %d", chip->num, chip,
63 return (nand_read_pages(chip, offset, buf, len));
67 nand_write(struct nand_chip *chip, uint32_t offset, void* buf, uint32_t len)
70 nand_debug(NDBG_GEOM, "Write to chip %d [%p] at %d", chip->num, chip,
73 return (nand_prog_pages(chip, offset, buf, len));
77 nand_read_raw(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
79 nand_debug(NDBG_GEOM, "Raw read from chip %d [%p] at %d", chip->num,
82 return (nand_read_pages_raw(chip, offset, buf, len));
86 nand_write_raw(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
89 nand_debug(NDBG_GEOM, "Raw write to chip %d [%p] at %d", chip->num,
92 return (nand_prog_pages_raw(chip, offset, buf, len));
96 nand_strategy(struct bio *bp)
98 struct nand_chip *chip;
100 chip = (struct nand_chip *)bp->bio_disk->d_drv1;
102 bp->bio_driver1 = BIO_NAND_STD;
104 nand_debug(NDBG_GEOM, "Strategy %s on chip %d [%p]",
105 bp->bio_cmd == BIO_READ ? "READ" :
106 (bp->bio_cmd == BIO_WRITE ? "WRITE" :
107 (bp->bio_cmd == BIO_DELETE ? "DELETE" : "UNKNOWN")),
110 mtx_lock(&chip->qlock);
111 bioq_insert_tail(&chip->bioq, bp);
112 mtx_unlock(&chip->qlock);
113 taskqueue_enqueue(chip->tq, &chip->iotask);
117 nand_strategy_raw(struct bio *bp)
119 struct nand_chip *chip;
121 chip = (struct nand_chip *)bp->bio_disk->d_drv1;
123 /* Inform taskqueue that it's a raw access */
124 bp->bio_driver1 = BIO_NAND_RAW;
126 nand_debug(NDBG_GEOM, "Strategy %s on chip %d [%p]",
127 bp->bio_cmd == BIO_READ ? "READ" :
128 (bp->bio_cmd == BIO_WRITE ? "WRITE" :
129 (bp->bio_cmd == BIO_DELETE ? "DELETE" : "UNKNOWN")),
132 mtx_lock(&chip->qlock);
133 bioq_insert_tail(&chip->bioq, bp);
134 mtx_unlock(&chip->qlock);
135 taskqueue_enqueue(chip->tq, &chip->iotask);
139 nand_oob_access(struct nand_chip *chip, uint32_t page, uint32_t offset,
140 uint32_t len, uint8_t *data, uint8_t write)
142 struct chip_geom *cg;
145 cg = &chip->chip_geom;
148 ret = nand_read_oob(chip, page, data, cg->oob_size);
150 ret = nand_prog_oob(chip, page, data, cg->oob_size);
156 nand_getattr(struct bio *bp)
158 struct nand_chip *chip;
159 struct chip_geom *cg;
163 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
166 chip = (struct nand_chip *)bp->bio_disk->d_drv1;
167 cg = &(chip->chip_geom);
169 dev = device_get_parent(chip->dev);
170 dev = device_get_parent(dev);
172 if (strcmp(bp->bio_attribute, "NAND::device") == 0) {
173 if (bp->bio_length != sizeof(dev))
175 bcopy(&dev, bp->bio_data, sizeof(dev));
177 if (strcmp(bp->bio_attribute, "NAND::oobsize") == 0)
179 else if (strcmp(bp->bio_attribute, "NAND::pagesize") == 0)
181 else if (strcmp(bp->bio_attribute, "NAND::blocksize") == 0)
182 val = cg->block_size;
185 if (bp->bio_length != sizeof(val))
187 bcopy(&val, bp->bio_data, sizeof(val));
189 bp->bio_completed = bp->bio_length;
194 nand_ioctl(struct disk *ndisk, u_long cmd, void *data, int fflag,
197 struct nand_chip *chip;
198 struct chip_geom *cg;
199 struct nand_oob_rw *oob_rw = NULL;
200 struct nand_raw_rw *raw_rw = NULL;
202 size_t bufsize = 0, len = 0;
209 chip = (struct nand_chip *)ndisk->d_drv1;
210 cg = &chip->chip_geom;
211 nandbus = device_get_parent(chip->dev);
213 if ((cmd == NAND_IO_RAW_READ) || (cmd == NAND_IO_RAW_PROG)) {
214 raw_rw = (struct nand_raw_rw *)data;
215 raw_size = cg->pgs_per_blk * (cg->page_size + cg->oob_size);
217 /* Check if len is not bigger than chip size */
218 if (raw_rw->len > raw_size)
222 * Do not ask for too much memory, in case of large transfers
223 * read/write in 16-pages chunks
225 bufsize = 16 * (cg->page_size + cg->oob_size);
226 if (raw_rw->len < bufsize)
227 bufsize = raw_rw->len;
229 buf = malloc(bufsize, M_NAND, M_WAITOK);
236 ret = nand_erase_blocks(chip, ((off_t *)data)[0],
240 case NAND_IO_OOB_READ:
241 oob_rw = (struct nand_oob_rw *)data;
242 ret = nand_oob_access(chip, oob_rw->page, 0,
243 oob_rw->len, oob_rw->data, 0);
246 case NAND_IO_OOB_PROG:
247 oob_rw = (struct nand_oob_rw *)data;
248 ret = nand_oob_access(chip, oob_rw->page, 0,
249 oob_rw->len, oob_rw->data, 1);
252 case NAND_IO_GET_STATUS:
253 NANDBUS_LOCK(nandbus);
254 ret = NANDBUS_GET_STATUS(nandbus, &status);
256 *(uint8_t *)data = status;
257 NANDBUS_UNLOCK(nandbus);
260 case NAND_IO_RAW_PROG:
265 ret = copyin(raw_rw->data + off, buf, bufsize);
268 ret = nand_prog_pages_raw(chip, raw_rw->off + off, buf,
277 case NAND_IO_RAW_READ:
282 ret = nand_read_pages_raw(chip, raw_rw->off + off, buf,
287 ret = copyout(buf, raw_rw->data + off, bufsize);
295 case NAND_IO_GET_CHIP_PARAM:
296 nand_get_chip_param(chip, (struct chip_param_io *)data);
300 printf("Unknown nand_ioctl request \n");
311 nand_io_proc(void *arg, int pending)
313 struct nand_chip *chip = arg;
318 mtx_lock(&chip->qlock);
319 bp = bioq_takefirst(&chip->bioq);
320 mtx_unlock(&chip->qlock);
324 if (bp->bio_driver1 == BIO_NAND_STD) {
325 if (bp->bio_cmd == BIO_READ) {
326 err = nand_read(chip,
327 bp->bio_offset & 0xffffffff,
328 bp->bio_data, bp->bio_bcount);
329 } else if (bp->bio_cmd == BIO_WRITE) {
330 err = nand_write(chip,
331 bp->bio_offset & 0xffffffff,
332 bp->bio_data, bp->bio_bcount);
334 } else if (bp->bio_driver1 == BIO_NAND_RAW) {
335 if (bp->bio_cmd == BIO_READ) {
336 err = nand_read_raw(chip,
337 bp->bio_offset & 0xffffffff,
338 bp->bio_data, bp->bio_bcount);
339 } else if (bp->bio_cmd == BIO_WRITE) {
340 err = nand_write_raw(chip,
341 bp->bio_offset & 0xffffffff,
342 bp->bio_data, bp->bio_bcount);
345 panic("Unknown access type in bio->bio_driver1\n");
347 if (bp->bio_cmd == BIO_DELETE) {
348 nand_debug(NDBG_GEOM, "Delete on chip%d offset %lld "
349 "length %ld\n", chip->num, bp->bio_offset,
351 err = nand_erase_blocks(chip,
352 bp->bio_offset & 0xffffffff,
356 if (err == 0 || err == ECC_CORRECTABLE)
359 nand_debug(NDBG_GEOM,"nand_[read|write|erase_blocks] "
363 bp->bio_flags |= BIO_ERROR;
364 bp->bio_resid = bp->bio_bcount;
371 create_geom_disk(struct nand_chip *chip)
373 struct disk *ndisk, *rdisk;
375 /* Create the disk device */
376 ndisk = disk_alloc();
377 ndisk->d_strategy = nand_strategy;
378 ndisk->d_ioctl = nand_ioctl;
379 ndisk->d_getattr = nand_getattr;
380 ndisk->d_name = "gnand";
381 ndisk->d_drv1 = chip;
382 ndisk->d_maxsize = chip->chip_geom.block_size;
383 ndisk->d_sectorsize = chip->chip_geom.page_size;
384 ndisk->d_mediasize = chip->chip_geom.chip_size;
385 ndisk->d_unit = chip->num +
386 10 * device_get_unit(device_get_parent(chip->dev));
389 * When using BBT, make two last blocks of device unavailable
390 * to user (because those are used to store BBT table).
392 if (chip->bbt != NULL)
393 ndisk->d_mediasize -= (2 * chip->chip_geom.block_size);
395 ndisk->d_flags = DISKFLAG_CANDELETE;
397 snprintf(ndisk->d_ident, sizeof(ndisk->d_ident),
398 "nand: Man:0x%02x Dev:0x%02x", chip->id.man_id, chip->id.dev_id);
399 ndisk->d_rotation_rate = DISK_RR_NON_ROTATING;
401 disk_create(ndisk, DISK_VERSION);
403 /* Create the RAW disk device */
404 rdisk = disk_alloc();
405 rdisk->d_strategy = nand_strategy_raw;
406 rdisk->d_ioctl = nand_ioctl;
407 rdisk->d_getattr = nand_getattr;
408 rdisk->d_name = "gnand.raw";
409 rdisk->d_drv1 = chip;
410 rdisk->d_maxsize = chip->chip_geom.block_size;
411 rdisk->d_sectorsize = chip->chip_geom.page_size;
412 rdisk->d_mediasize = chip->chip_geom.chip_size;
413 rdisk->d_unit = chip->num +
414 10 * device_get_unit(device_get_parent(chip->dev));
416 rdisk->d_flags = DISKFLAG_CANDELETE;
418 snprintf(rdisk->d_ident, sizeof(rdisk->d_ident),
419 "nand_raw: Man:0x%02x Dev:0x%02x", chip->id.man_id,
421 rdisk->d_rotation_rate = DISK_RR_NON_ROTATING;
423 disk_create(rdisk, DISK_VERSION);
428 mtx_init(&chip->qlock, "NAND I/O lock", NULL, MTX_DEF);
429 bioq_init(&chip->bioq);
431 TASK_INIT(&chip->iotask, 0, nand_io_proc, chip);
432 chip->tq = taskqueue_create("nand_taskq", M_WAITOK,
433 taskqueue_thread_enqueue, &chip->tq);
434 taskqueue_start_threads(&chip->tq, 1, PI_DISK, "nand taskq");
437 device_printf(chip->dev, "Created gnand%d for chip [0x%0x, "
438 "0x%0x]\n", ndisk->d_unit, chip->id.man_id,
445 destroy_geom_disk(struct nand_chip *chip)
449 taskqueue_free(chip->tq);
450 disk_destroy(chip->ndisk);
451 disk_destroy(chip->rdisk);
453 mtx_lock(&chip->qlock);
455 bp = bioq_takefirst(&chip->bioq);
459 bp->bio_flags |= BIO_ERROR;
460 bp->bio_resid = bp->bio_bcount;
464 mtx_unlock(&chip->qlock);
466 mtx_destroy(&chip->qlock);