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>
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 static int nand_page_stat(struct nand_chip *, struct page_stat_io *);
47 static int nand_block_stat(struct nand_chip *, struct block_stat_io *);
49 static d_ioctl_t nand_ioctl;
50 static d_open_t nand_open;
51 static d_strategy_t nand_strategy;
53 static struct cdevsw nand_cdevsw = {
54 .d_version = D_VERSION,
59 .d_ioctl = nand_ioctl,
60 .d_strategy = nand_strategy,
64 offset_to_page(struct chip_geom *cg, uint32_t offset)
67 return (offset / cg->page_size);
71 offset_to_page_off(struct chip_geom *cg, uint32_t offset)
74 return (offset % cg->page_size);
78 nand_make_dev(struct nand_chip *chip)
80 struct nandbus_ivar *ivar;
81 device_t parent, nandbus;
82 int parent_unit, unit;
85 ivar = device_get_ivars(chip->dev);
86 nandbus = device_get_parent(chip->dev);
88 if (ivar->chip_cdev_name) {
89 name = ivar->chip_cdev_name;
92 * If we got distinct name for chip device we can enumarete it
93 * based on contoller number.
95 parent = device_get_parent(nandbus);
101 parent_unit = device_get_unit(parent);
102 unit = parent_unit * 4 + chip->num;
103 chip->cdev = make_dev(&nand_cdevsw, unit, UID_ROOT, GID_WHEEL,
104 0666, "%s%d.%d", name, parent_unit, chip->num);
106 if (chip->cdev == NULL)
110 device_printf(chip->dev, "Created cdev %s%d.%d for chip "
111 "[0x%0x, 0x%0x]\n", name, parent_unit, chip->num,
112 ivar->man_id, ivar->dev_id);
114 chip->cdev->si_drv1 = chip;
120 nand_destroy_dev(struct nand_chip *chip)
124 destroy_dev(chip->cdev);
128 nand_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
135 nand_read(struct nand_chip *chip, uint32_t offset, void *buf, uint32_t len)
137 struct chip_geom *cg;
139 int start_page, count, off, err = 0;
142 nand_debug(NDBG_CDEV, "Read from chip%d [%p] at %d\n", chip->num,
145 nandbus = device_get_parent(chip->dev);
146 NANDBUS_LOCK(nandbus);
147 NANDBUS_SELECT_CS(device_get_parent(chip->dev), chip->num);
149 cg = &chip->chip_geom;
150 start_page = offset_to_page(cg, offset);
151 off = offset_to_page_off(cg, offset);
152 count = (len > cg->page_size - off) ? cg->page_size - off : len;
154 ptr = (uint8_t *)buf;
156 if (len < cg->page_size) {
157 tmp = malloc(cg->page_size, M_NAND, M_WAITOK);
162 err = NAND_READ_PAGE(chip->dev, start_page,
163 tmp, cg->page_size, 0);
168 bcopy(tmp + off, ptr, count);
171 err = NAND_READ_PAGE(chip->dev, start_page,
172 ptr, cg->page_size, 0);
180 count = (len > cg->page_size) ? cg->page_size : len;
184 NANDBUS_UNLOCK(nandbus);
189 nand_write(struct nand_chip *chip, uint32_t offset, void* buf, uint32_t len)
191 struct chip_geom *cg;
193 int off, start_page, err = 0;
196 nand_debug(NDBG_CDEV, "Write to chip %d [%p] at %d\n", chip->num,
199 nandbus = device_get_parent(chip->dev);
200 NANDBUS_LOCK(nandbus);
201 NANDBUS_SELECT_CS(device_get_parent(chip->dev), chip->num);
203 cg = &chip->chip_geom;
204 start_page = offset_to_page(cg, offset);
205 off = offset_to_page_off(cg, offset);
207 if (off != 0 || (len % cg->page_size) != 0) {
208 printf("Not aligned write start [0x%08x] size [0x%08x]\n",
210 NANDBUS_UNLOCK(nandbus);
214 ptr = (uint8_t *)buf;
216 err = NAND_PROGRAM_PAGE(chip->dev, start_page, ptr,
221 len -= cg->page_size;
223 ptr += cg->page_size;
226 NANDBUS_UNLOCK(nandbus);
231 nand_strategy(struct bio *bp)
233 struct nand_chip *chip;
240 nand_debug(NDBG_CDEV, "Strategy %s on chip %d [%p]\n",
241 bp->bio_cmd == BIO_READ ? "READ" : "WRITE",
244 if (bp->bio_cmd == BIO_READ) {
245 err = nand_read(chip,
246 bp->bio_offset & 0xffffffff,
247 bp->bio_data, bp->bio_bcount);
249 err = nand_write(chip,
250 bp->bio_offset & 0xffffffff,
251 bp->bio_data, bp->bio_bcount);
258 bp->bio_flags |= BIO_ERROR;
259 bp->bio_resid = bp->bio_bcount;
266 nand_oob_access(struct nand_chip *chip, uint32_t page, uint32_t offset,
267 uint32_t len, uint8_t *data, uint8_t write)
269 struct chip_geom *cg;
273 cg = &chip->chip_geom;
275 buf = malloc(cg->oob_size, M_NAND, M_WAITOK);
279 memset(buf, 0xff, cg->oob_size);
282 ret = nand_read_oob(chip, page, buf, cg->oob_size);
283 copyout(buf, data, len);
285 copyin(data, buf, len);
286 ret = nand_prog_oob(chip, page, buf, cg->oob_size);
295 nand_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
298 struct nand_chip *chip;
299 struct chip_geom *cg;
300 struct nand_oob_rw *oob_rw = NULL;
301 struct nand_raw_rw *raw_rw = NULL;
303 size_t bufsize = 0, len = 0;
310 chip = (struct nand_chip *)dev->si_drv1;
311 cg = &chip->chip_geom;
312 nandbus = device_get_parent(chip->dev);
314 if ((cmd == NAND_IO_RAW_READ) || (cmd == NAND_IO_RAW_PROG)) {
315 raw_rw = (struct nand_raw_rw *)data;
316 raw_size = cg->pgs_per_blk * (cg->page_size + cg->oob_size);
318 /* Check if len is not bigger than chip size */
319 if (raw_rw->len > raw_size)
323 * Do not ask for too much memory, in case of large transfers
324 * read/write in 16-pages chunks
326 bufsize = 16 * (cg->page_size + cg->oob_size);
327 if (raw_rw->len < bufsize)
328 bufsize = raw_rw->len;
330 buf = malloc(bufsize, M_NAND, M_WAITOK);
336 ret = nand_erase_blocks(chip, ((off_t *)data)[0],
340 case NAND_IO_OOB_READ:
341 oob_rw = (struct nand_oob_rw *)data;
342 ret = nand_oob_access(chip, oob_rw->page, 0,
343 oob_rw->len, oob_rw->data, 0);
346 case NAND_IO_OOB_PROG:
347 oob_rw = (struct nand_oob_rw *)data;
348 ret = nand_oob_access(chip, oob_rw->page, 0,
349 oob_rw->len, oob_rw->data, 1);
352 case NAND_IO_GET_STATUS:
353 NANDBUS_LOCK(nandbus);
354 ret = NANDBUS_GET_STATUS(nandbus, &status);
356 *(uint8_t *)data = status;
357 NANDBUS_UNLOCK(nandbus);
360 case NAND_IO_RAW_PROG:
364 ret = copyin(raw_rw->data + off, buf, bufsize);
367 ret = nand_prog_pages_raw(chip, raw_rw->off + off, buf,
376 case NAND_IO_RAW_READ:
381 ret = nand_read_pages_raw(chip, raw_rw->off + off, buf,
386 ret = copyout(buf, raw_rw->data + off, bufsize);
394 case NAND_IO_PAGE_STAT:
395 ret = nand_page_stat(chip, (struct page_stat_io *)data);
398 case NAND_IO_BLOCK_STAT:
399 ret = nand_block_stat(chip, (struct block_stat_io *)data);
402 case NAND_IO_GET_CHIP_PARAM:
403 nand_get_chip_param(chip, (struct chip_param_io *)data);
407 printf("Unknown nand_ioctl request \n");
418 nand_page_stat(struct nand_chip *chip, struct page_stat_io *page_stat)
420 struct chip_geom *cg;
421 struct page_stat *stat;
424 cg = &chip->chip_geom;
425 num_pages = cg->pgs_per_blk * cg->blks_per_lun * cg->luns;
426 if (page_stat->page_num >= num_pages)
429 stat = &chip->pg_stat[page_stat->page_num];
430 page_stat->page_read = stat->page_read;
431 page_stat->page_written = stat->page_written;
432 page_stat->page_raw_read = stat->page_raw_read;
433 page_stat->page_raw_written = stat->page_raw_written;
434 page_stat->ecc_succeded = stat->ecc_stat.ecc_succeded;
435 page_stat->ecc_corrected = stat->ecc_stat.ecc_corrected;
436 page_stat->ecc_failed = stat->ecc_stat.ecc_failed;
442 nand_block_stat(struct nand_chip *chip, struct block_stat_io *block_stat)
444 struct chip_geom *cg;
445 uint32_t block_num = block_stat->block_num;
447 cg = &chip->chip_geom;
448 if (block_num >= cg->blks_per_lun * cg->luns)
451 block_stat->block_erased = chip->blk_stat[block_num].block_erased;