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>
38 #include <dev/nand/nand.h>
39 #include <dev/nand/nandbus.h>
40 #include <dev/nand/nand_dev.h>
42 #include "nandbus_if.h"
44 static int nand_page_stat(struct nand_chip *, struct page_stat_io *);
45 static int nand_block_stat(struct nand_chip *, struct block_stat_io *);
47 static d_ioctl_t nand_ioctl;
48 static d_open_t nand_open;
49 static d_strategy_t nand_strategy;
51 static struct cdevsw nand_cdevsw = {
52 .d_version = D_VERSION,
57 .d_ioctl = nand_ioctl,
58 .d_strategy = nand_strategy,
62 offset_to_page(struct chip_geom *cg, uint32_t offset)
65 return (offset / cg->page_size);
69 offset_to_page_off(struct chip_geom *cg, uint32_t offset)
72 return (offset % cg->page_size);
76 nand_make_dev(struct nand_chip *chip)
78 struct nandbus_ivar *ivar;
79 device_t parent, nandbus;
80 int parent_unit, unit;
83 ivar = device_get_ivars(chip->dev);
84 nandbus = device_get_parent(chip->dev);
86 if (ivar->chip_cdev_name) {
87 name = ivar->chip_cdev_name;
90 * If we got distinct name for chip device we can enumarete it
91 * based on contoller number.
93 parent = device_get_parent(nandbus);
99 parent_unit = device_get_unit(parent);
100 unit = parent_unit * 4 + chip->num;
101 chip->cdev = make_dev(&nand_cdevsw, unit, UID_ROOT, GID_WHEEL,
102 0666, "%s%d.%d", name, parent_unit, chip->num);
104 if (chip->cdev == NULL)
108 device_printf(chip->dev, "Created cdev %s%d.%d for chip "
109 "[0x%0x, 0x%0x]\n", name, parent_unit, chip->num,
110 ivar->man_id, ivar->dev_id);
112 chip->cdev->si_drv1 = chip;
118 nand_destroy_dev(struct nand_chip *chip)
122 destroy_dev(chip->cdev);
126 nand_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
133 nand_read(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
135 struct chip_geom *cg;
137 int start_page, count, off, err = 0;
140 nand_debug(NDBG_CDEV, "Read from chip%d [%p] at %d\n", chip->num,
143 nandbus = device_get_parent(chip->dev);
144 NANDBUS_LOCK(nandbus);
145 NANDBUS_SELECT_CS(device_get_parent(chip->dev), chip->num);
147 cg = &chip->chip_geom;
148 start_page = offset_to_page(cg, offset);
149 off = offset_to_page_off(cg, offset);
150 count = (len > cg->page_size - off) ? cg->page_size - off : len;
152 ptr = (uint8_t *)buf;
154 if (len < cg->page_size) {
155 tmp = malloc(cg->page_size, M_NAND, M_WAITOK);
160 err = NAND_READ_PAGE(chip->dev, start_page,
161 tmp, cg->page_size, 0);
166 bcopy(tmp + off, ptr, count);
169 err = NAND_READ_PAGE(chip->dev, start_page,
170 ptr, cg->page_size, 0);
178 count = (len > cg->page_size) ? cg->page_size : len;
182 NANDBUS_UNLOCK(nandbus);
187 nand_write(struct nand_chip *chip, uint32_t offset, void* buf, uint32_t len)
189 struct chip_geom *cg;
191 int off, start_page, err = 0;
194 nand_debug(NDBG_CDEV, "Write to chip %d [%p] at %d\n", chip->num,
197 nandbus = device_get_parent(chip->dev);
198 NANDBUS_LOCK(nandbus);
199 NANDBUS_SELECT_CS(device_get_parent(chip->dev), chip->num);
201 cg = &chip->chip_geom;
202 start_page = offset_to_page(cg, offset);
203 off = offset_to_page_off(cg, offset);
205 if (off != 0 || (len % cg->page_size) != 0) {
206 printf("Not aligned write start [0x%08x] size [0x%08x]\n",
208 NANDBUS_UNLOCK(nandbus);
212 ptr = (uint8_t *)buf;
214 err = NAND_PROGRAM_PAGE(chip->dev, start_page, ptr,
219 len -= cg->page_size;
221 ptr += cg->page_size;
224 NANDBUS_UNLOCK(nandbus);
229 nand_strategy(struct bio *bp)
231 struct nand_chip *chip;
238 nand_debug(NDBG_CDEV, "Strategy %s on chip %d [%p]\n",
239 bp->bio_cmd == BIO_READ ? "READ" : "WRITE",
242 if (bp->bio_cmd == BIO_READ) {
243 err = nand_read(chip,
244 bp->bio_offset & 0xffffffff,
245 bp->bio_data, bp->bio_bcount);
247 err = nand_write(chip,
248 bp->bio_offset & 0xffffffff,
249 bp->bio_data, bp->bio_bcount);
256 bp->bio_flags |= BIO_ERROR;
257 bp->bio_resid = bp->bio_bcount;
264 nand_oob_access(struct nand_chip *chip, uint32_t page, uint32_t offset,
265 uint32_t len, uint8_t *data, uint8_t write)
267 struct chip_geom *cg;
271 cg = &chip->chip_geom;
273 buf = malloc(cg->oob_size, M_NAND, M_WAITOK);
277 memset(buf, 0xff, cg->oob_size);
280 ret = nand_read_oob(chip, page, buf, cg->oob_size);
281 copyout(buf, data, len);
283 copyin(data, buf, len);
284 ret = nand_prog_oob(chip, page, buf, cg->oob_size);
293 nand_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
296 struct nand_chip *chip;
297 struct chip_geom *cg;
298 struct nand_oob_rw *oob_rw = NULL;
299 struct nand_raw_rw *raw_rw = NULL;
301 size_t bufsize = 0, len = 0;
308 chip = (struct nand_chip *)dev->si_drv1;
309 cg = &chip->chip_geom;
310 nandbus = device_get_parent(chip->dev);
312 if ((cmd == NAND_IO_RAW_READ) || (cmd == NAND_IO_RAW_PROG)) {
313 raw_rw = (struct nand_raw_rw *)data;
314 raw_size = cg->pgs_per_blk * (cg->page_size + cg->oob_size);
316 /* Check if len is not bigger than chip size */
317 if (raw_rw->len > raw_size)
321 * Do not ask for too much memory, in case of large transfers
322 * read/write in 16-pages chunks
324 bufsize = 16 * (cg->page_size + cg->oob_size);
325 if (raw_rw->len < bufsize)
326 bufsize = raw_rw->len;
328 buf = malloc(bufsize, M_NAND, M_WAITOK);
334 ret = nand_erase_blocks(chip, ((off_t *)data)[0],
338 case NAND_IO_OOB_READ:
339 oob_rw = (struct nand_oob_rw *)data;
340 ret = nand_oob_access(chip, oob_rw->page, 0,
341 oob_rw->len, oob_rw->data, 0);
344 case NAND_IO_OOB_PROG:
345 oob_rw = (struct nand_oob_rw *)data;
346 ret = nand_oob_access(chip, oob_rw->page, 0,
347 oob_rw->len, oob_rw->data, 1);
350 case NAND_IO_GET_STATUS:
351 NANDBUS_LOCK(nandbus);
352 ret = NANDBUS_GET_STATUS(nandbus, &status);
354 *(uint8_t *)data = status;
355 NANDBUS_UNLOCK(nandbus);
358 case NAND_IO_RAW_PROG:
362 ret = copyin(raw_rw->data + off, buf, bufsize);
365 ret = nand_prog_pages_raw(chip, raw_rw->off + off, buf,
374 case NAND_IO_RAW_READ:
379 ret = nand_read_pages_raw(chip, raw_rw->off + off, buf,
384 ret = copyout(buf, raw_rw->data + off, bufsize);
392 case NAND_IO_PAGE_STAT:
393 ret = nand_page_stat(chip, (struct page_stat_io *)data);
396 case NAND_IO_BLOCK_STAT:
397 ret = nand_block_stat(chip, (struct block_stat_io *)data);
400 case NAND_IO_GET_CHIP_PARAM:
401 nand_get_chip_param(chip, (struct chip_param_io *)data);
405 printf("Unknown nand_ioctl request \n");
416 nand_page_stat(struct nand_chip *chip, struct page_stat_io *page_stat)
418 struct chip_geom *cg;
419 struct page_stat *stat;
422 cg = &chip->chip_geom;
423 num_pages = cg->pgs_per_blk * cg->blks_per_lun * cg->luns;
424 if (page_stat->page_num >= num_pages)
427 stat = &chip->pg_stat[page_stat->page_num];
428 page_stat->page_read = stat->page_read;
429 page_stat->page_written = stat->page_written;
430 page_stat->page_raw_read = stat->page_raw_read;
431 page_stat->page_raw_written = stat->page_raw_written;
432 page_stat->ecc_succeded = stat->ecc_stat.ecc_succeded;
433 page_stat->ecc_corrected = stat->ecc_stat.ecc_corrected;
434 page_stat->ecc_failed = stat->ecc_stat.ecc_failed;
440 nand_block_stat(struct nand_chip *chip, struct block_stat_io *block_stat)
442 struct chip_geom *cg;
443 uint32_t block_num = block_stat->block_num;
445 cg = &chip->chip_geom;
446 if (block_num >= cg->blks_per_lun * cg->luns)
449 block_stat->block_erased = chip->blk_stat[block_num].block_erased;