2 * Copyright (C) 2009-2012 Semihalf
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/malloc.h>
37 #include <geom/geom.h>
38 #include <geom/geom_disk.h>
40 #include <dev/nand/nand.h>
41 #include <dev/nand/nandbus.h>
42 #include <dev/nand/nand_dev.h>
44 #include "nandbus_if.h"
46 #define BIO_NAND_STD ((void *)1)
47 #define BIO_NAND_RAW ((void *)2)
49 static disk_ioctl_t nand_ioctl;
50 static disk_getattr_t nand_getattr;
51 static disk_strategy_t nand_strategy;
52 static disk_strategy_t nand_strategy_raw;
55 nand_read(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
58 nand_debug(NDBG_GEOM, "Read from chip %d [%p] at %d", chip->num, chip,
61 return (nand_read_pages(chip, offset, buf, len));
65 nand_write(struct nand_chip *chip, uint32_t offset, void* buf, uint32_t len)
68 nand_debug(NDBG_GEOM, "Write to chip %d [%p] at %d", chip->num, chip,
71 return (nand_prog_pages(chip, offset, buf, len));
75 nand_read_raw(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
77 nand_debug(NDBG_GEOM, "Raw read from chip %d [%p] at %d", chip->num,
80 return (nand_read_pages_raw(chip, offset, buf, len));
84 nand_write_raw(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
87 nand_debug(NDBG_GEOM, "Raw write to chip %d [%p] at %d", chip->num,
90 return (nand_prog_pages_raw(chip, offset, buf, len));
94 nand_strategy(struct bio *bp)
96 struct nand_chip *chip;
98 chip = (struct nand_chip *)bp->bio_disk->d_drv1;
100 bp->bio_driver1 = BIO_NAND_STD;
102 nand_debug(NDBG_GEOM, "Strategy %s on chip %d [%p]",
103 (bp->bio_cmd & BIO_READ) == BIO_READ ? "READ" :
104 ((bp->bio_cmd & BIO_WRITE) == BIO_WRITE ? "WRITE" :
105 ((bp->bio_cmd & BIO_DELETE) == BIO_DELETE ? "DELETE" : "UNKNOWN")),
108 mtx_lock(&chip->qlock);
109 bioq_insert_tail(&chip->bioq, bp);
110 mtx_unlock(&chip->qlock);
111 taskqueue_enqueue(chip->tq, &chip->iotask);
115 nand_strategy_raw(struct bio *bp)
117 struct nand_chip *chip;
119 chip = (struct nand_chip *)bp->bio_disk->d_drv1;
121 /* Inform taskqueue that it's a raw access */
122 bp->bio_driver1 = BIO_NAND_RAW;
124 nand_debug(NDBG_GEOM, "Strategy %s on chip %d [%p]",
125 (bp->bio_cmd & BIO_READ) == BIO_READ ? "READ" :
126 ((bp->bio_cmd & BIO_WRITE) == BIO_WRITE ? "WRITE" :
127 ((bp->bio_cmd & BIO_DELETE) == BIO_DELETE ? "DELETE" : "UNKNOWN")),
130 mtx_lock(&chip->qlock);
131 bioq_insert_tail(&chip->bioq, bp);
132 mtx_unlock(&chip->qlock);
133 taskqueue_enqueue(chip->tq, &chip->iotask);
137 nand_oob_access(struct nand_chip *chip, uint32_t page, uint32_t offset,
138 uint32_t len, uint8_t *data, uint8_t write)
140 struct chip_geom *cg;
143 cg = &chip->chip_geom;
146 ret = nand_read_oob(chip, page, data, cg->oob_size);
148 ret = nand_prog_oob(chip, page, data, cg->oob_size);
154 nand_getattr(struct bio *bp)
156 struct nand_chip *chip;
157 struct chip_geom *cg;
160 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
163 chip = (struct nand_chip *)bp->bio_disk->d_drv1;
164 cg = &(chip->chip_geom);
166 dev = device_get_parent(chip->dev);
167 dev = device_get_parent(dev);
170 if (g_handleattr_int(bp, "NAND::oobsize", cg->oob_size))
172 else if (g_handleattr_int(bp, "NAND::pagesize", cg->page_size))
174 else if (g_handleattr_int(bp, "NAND::blocksize",
177 else if (g_handleattr(bp, "NAND::device", &(dev),
184 return (EJUSTRETURN);
188 nand_ioctl(struct disk *ndisk, u_long cmd, void *data, int fflag,
191 struct nand_chip *chip;
192 struct nand_oob_rw *oob_rw = NULL;
193 struct nand_raw_rw *raw_rw = NULL;
199 chip = (struct nand_chip *)ndisk->d_drv1;
200 nandbus = device_get_parent(chip->dev);
202 if ((cmd == NAND_IO_RAW_READ) || (cmd == NAND_IO_RAW_PROG)) {
203 raw_rw = (struct nand_raw_rw *)data;
204 buf = malloc(raw_rw->len, M_NAND, M_WAITOK);
208 ret = nand_erase_blocks(chip, ((off_t *)data)[0],
212 case NAND_IO_OOB_READ:
213 oob_rw = (struct nand_oob_rw *)data;
214 ret = nand_oob_access(chip, oob_rw->page, 0,
215 oob_rw->len, oob_rw->data, 0);
218 case NAND_IO_OOB_PROG:
219 oob_rw = (struct nand_oob_rw *)data;
220 ret = nand_oob_access(chip, oob_rw->page, 0,
221 oob_rw->len, oob_rw->data, 1);
224 case NAND_IO_GET_STATUS:
225 NANDBUS_LOCK(nandbus);
226 ret = NANDBUS_GET_STATUS(nandbus, &status);
228 *(uint8_t *)data = status;
229 NANDBUS_UNLOCK(nandbus);
232 case NAND_IO_RAW_PROG:
233 copyin(raw_rw->data, buf, raw_rw->len);
234 ret = nand_prog_pages_raw(chip, raw_rw->off, buf,
238 case NAND_IO_RAW_READ:
239 ret = nand_read_pages_raw(chip, raw_rw->off, buf,
241 copyout(buf, raw_rw->data, raw_rw->len);
244 case NAND_IO_GET_CHIP_PARAM:
245 nand_get_chip_param(chip, (struct chip_param_io *)data);
249 printf("Unknown nand_ioctl request \n");
260 nand_io_proc(void *arg, int pending)
262 struct nand_chip *chip = arg;
267 mtx_lock(&chip->qlock);
268 bp = bioq_takefirst(&chip->bioq);
269 mtx_unlock(&chip->qlock);
273 if (bp->bio_driver1 == BIO_NAND_STD) {
274 if ((bp->bio_cmd & BIO_READ) == BIO_READ) {
275 err = nand_read(chip,
276 bp->bio_offset & 0xffffffff,
277 bp->bio_data, bp->bio_bcount);
278 } else if ((bp->bio_cmd & BIO_WRITE) == BIO_WRITE) {
279 err = nand_write(chip,
280 bp->bio_offset & 0xffffffff,
281 bp->bio_data, bp->bio_bcount);
283 } else if (bp->bio_driver1 == BIO_NAND_RAW) {
284 if ((bp->bio_cmd & BIO_READ) == BIO_READ) {
285 err = nand_read_raw(chip,
286 bp->bio_offset & 0xffffffff,
287 bp->bio_data, bp->bio_bcount);
288 } else if ((bp->bio_cmd & BIO_WRITE) == BIO_WRITE) {
289 err = nand_write_raw(chip,
290 bp->bio_offset & 0xffffffff,
291 bp->bio_data, bp->bio_bcount);
294 panic("Unknown access type in bio->bio_driver1\n");
296 if ((bp->bio_cmd & BIO_DELETE) == BIO_DELETE) {
297 nand_debug(NDBG_GEOM, "Delete on chip%d offset %lld "
298 "length %ld\n", chip->num, bp->bio_offset,
300 err = nand_erase_blocks(chip,
301 bp->bio_offset & 0xffffffff,
305 if (err == 0 || err == ECC_CORRECTABLE)
308 nand_debug(NDBG_GEOM,"nand_[read|write|erase_blocks] "
312 bp->bio_flags |= BIO_ERROR;
313 bp->bio_resid = bp->bio_bcount;
320 create_geom_disk(struct nand_chip *chip)
322 struct disk *ndisk, *rdisk;
324 /* Create the disk device */
325 ndisk = disk_alloc();
326 ndisk->d_strategy = nand_strategy;
327 ndisk->d_ioctl = nand_ioctl;
328 ndisk->d_getattr = nand_getattr;
329 ndisk->d_name = "gnand";
330 ndisk->d_drv1 = chip;
331 ndisk->d_maxsize = chip->chip_geom.block_size;
332 ndisk->d_sectorsize = chip->chip_geom.page_size;
333 ndisk->d_mediasize = chip->chip_geom.chip_size;
334 ndisk->d_unit = chip->num +
335 10 * device_get_unit(device_get_parent(chip->dev));
338 * When using BBT, make two last blocks of device unavailable
339 * to user (because those are used to store BBT table).
341 if (chip->bbt != NULL)
342 ndisk->d_mediasize -= (2 * chip->chip_geom.block_size);
344 ndisk->d_flags = DISKFLAG_CANDELETE;
346 snprintf(ndisk->d_ident, sizeof(ndisk->d_ident),
347 "nand: Man:0x%02x Dev:0x%02x", chip->id.man_id, chip->id.dev_id);
349 disk_create(ndisk, DISK_VERSION);
351 /* Create the RAW disk device */
352 rdisk = disk_alloc();
353 rdisk->d_strategy = nand_strategy_raw;
354 rdisk->d_ioctl = nand_ioctl;
355 rdisk->d_getattr = nand_getattr;
356 rdisk->d_name = "gnand.raw";
357 rdisk->d_drv1 = chip;
358 rdisk->d_maxsize = chip->chip_geom.block_size;
359 rdisk->d_sectorsize = chip->chip_geom.page_size;
360 rdisk->d_mediasize = chip->chip_geom.chip_size;
361 rdisk->d_unit = chip->num +
362 10 * device_get_unit(device_get_parent(chip->dev));
364 rdisk->d_flags = DISKFLAG_CANDELETE;
366 snprintf(rdisk->d_ident, sizeof(rdisk->d_ident),
367 "nand_raw: Man:0x%02x Dev:0x%02x", chip->id.man_id,
370 disk_create(rdisk, DISK_VERSION);
375 mtx_init(&chip->qlock, "NAND I/O lock", NULL, MTX_DEF);
376 bioq_init(&chip->bioq);
378 TASK_INIT(&chip->iotask, 0, nand_io_proc, chip);
379 chip->tq = taskqueue_create("nand_taskq", M_WAITOK,
380 taskqueue_thread_enqueue, &chip->tq);
381 taskqueue_start_threads(&chip->tq, 1, PI_DISK, "nand taskq");
384 device_printf(chip->dev, "Created gnand%d for chip [0x%0x, "
385 "0x%0x]\n", ndisk->d_unit, chip->id.man_id,
392 destroy_geom_disk(struct nand_chip *chip)
396 taskqueue_free(chip->tq);
397 disk_destroy(chip->ndisk);
398 disk_destroy(chip->rdisk);
400 mtx_lock(&chip->qlock);
402 bp = bioq_takefirst(&chip->bioq);
406 bp->bio_flags |= BIO_ERROR;
407 bp->bio_resid = bp->bio_bcount;
411 mtx_unlock(&chip->qlock);
413 mtx_destroy(&chip->qlock);