Home | History | Annotate | Line # | Download | only in flash
flash.c revision 1.6
      1  1.6     ahoka /*	$NetBSD: flash.c,v 1.6 2011/06/28 07:00:17 ahoka Exp $	*/
      2  1.1     ahoka 
      3  1.1     ahoka /*-
      4  1.1     ahoka  * Copyright (c) 2011 Department of Software Engineering,
      5  1.1     ahoka  *		      University of Szeged, Hungary
      6  1.1     ahoka  * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
      7  1.1     ahoka  * Copyright (c) 2010 David Tengeri <dtengeri (at) inf.u-szeged.hu>
      8  1.1     ahoka  * All rights reserved.
      9  1.1     ahoka  *
     10  1.1     ahoka  * This code is derived from software contributed to The NetBSD Foundation
     11  1.1     ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     12  1.1     ahoka  *
     13  1.1     ahoka  * Redistribution and use in source and binary forms, with or without
     14  1.1     ahoka  * modification, are permitted provided that the following conditions
     15  1.1     ahoka  * are met:
     16  1.1     ahoka  * 1. Redistributions of source code must retain the above copyright
     17  1.1     ahoka  *    notice, this list of conditions and the following disclaimer.
     18  1.1     ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1     ahoka  *    notice, this list of conditions and the following disclaimer in the
     20  1.1     ahoka  *    documentation and/or other materials provided with the distribution.
     21  1.1     ahoka  *
     22  1.1     ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1     ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1     ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1     ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1     ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  1.1     ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  1.1     ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  1.1     ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.1     ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1     ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1     ahoka  * SUCH DAMAGE.
     33  1.1     ahoka  */
     34  1.1     ahoka 
     35  1.1     ahoka /*-
     36  1.1     ahoka  * Framework for storage devices based on Flash technology
     37  1.1     ahoka  */
     38  1.1     ahoka 
     39  1.1     ahoka #include <sys/cdefs.h>
     40  1.6     ahoka __KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.6 2011/06/28 07:00:17 ahoka Exp $");
     41  1.1     ahoka 
     42  1.1     ahoka #include <sys/param.h>
     43  1.1     ahoka #include <sys/types.h>
     44  1.1     ahoka #include <sys/proc.h>
     45  1.1     ahoka #include <sys/errno.h>
     46  1.1     ahoka #include <sys/ioctl.h>
     47  1.1     ahoka #include <sys/device.h>
     48  1.1     ahoka #include <sys/conf.h>
     49  1.1     ahoka #include <sys/kmem.h>
     50  1.1     ahoka #include <sys/uio.h>
     51  1.1     ahoka #include <sys/kernel.h>
     52  1.1     ahoka 
     53  1.1     ahoka #include <sys/atomic.h>
     54  1.1     ahoka #include <sys/buf.h>
     55  1.1     ahoka #include <sys/bufq.h>
     56  1.1     ahoka #include <sys/disk.h>
     57  1.1     ahoka #include <sys/disklabel.h>
     58  1.1     ahoka #include <sys/malloc.h>
     59  1.1     ahoka #include <sys/reboot.h>
     60  1.1     ahoka 
     61  1.1     ahoka #include <sys/flashio.h>
     62  1.1     ahoka #include "flash.h"
     63  1.1     ahoka 
     64  1.1     ahoka #define FLASH_DEBUG 1
     65  1.1     ahoka #ifdef FLASH_DEBUG
     66  1.1     ahoka #define DPRINTF(x)	if (flashdebug) printf x
     67  1.1     ahoka #define DPRINTFN(n,x)	if (flashdebug>(n)) printf x
     68  1.1     ahoka int	flashdebug = FLASH_DEBUG;
     69  1.1     ahoka #else
     70  1.1     ahoka #define DPRINTF(x)
     71  1.1     ahoka #define DPRINTFN(n,x)
     72  1.1     ahoka #endif
     73  1.1     ahoka 
     74  1.1     ahoka extern struct cfdriver flash_cd;
     75  1.1     ahoka 
     76  1.1     ahoka dev_type_open(flashopen);
     77  1.1     ahoka dev_type_close(flashclose);
     78  1.1     ahoka dev_type_read(flashread);
     79  1.1     ahoka dev_type_write(flashwrite);
     80  1.1     ahoka dev_type_ioctl(flashioctl);
     81  1.1     ahoka dev_type_strategy(flashstrategy);
     82  1.1     ahoka dev_type_dump(flashdump);
     83  1.1     ahoka 
     84  1.1     ahoka int flash_print(void *aux, const char *pnp);
     85  1.1     ahoka 
     86  1.1     ahoka bool flash_shutdown(device_t dev, int how);
     87  1.1     ahoka int flash_nsectors(struct buf *bp);
     88  1.1     ahoka int flash_sector(struct buf *bp);
     89  1.1     ahoka 
     90  1.3     ahoka static inline flash_off_t flash_get_part_offset(struct flash_softc *fl,
     91  1.1     ahoka     size_t poffset);
     92  1.1     ahoka 
     93  1.1     ahoka int flash_match(device_t parent, cfdata_t match, void *aux);
     94  1.1     ahoka void flash_attach(device_t parent, device_t self, void *aux);
     95  1.1     ahoka int flash_detach(device_t device, int flags);
     96  1.1     ahoka 
     97  1.1     ahoka CFATTACH_DECL_NEW(flash, sizeof(struct flash_softc),
     98  1.1     ahoka     flash_match, flash_attach, flash_detach, NULL);
     99  1.1     ahoka 
    100  1.1     ahoka /**
    101  1.1     ahoka  * Block device's operation
    102  1.1     ahoka  */
    103  1.1     ahoka const struct bdevsw flash_bdevsw = {
    104  1.1     ahoka 	.d_open = flashopen,
    105  1.1     ahoka 	.d_close = flashclose,
    106  1.1     ahoka 	.d_strategy = flashstrategy,
    107  1.1     ahoka 	.d_ioctl = flashioctl,
    108  1.1     ahoka 	.d_dump = flashdump,
    109  1.6     ahoka 	.d_psize = nosize,
    110  1.1     ahoka 	.d_flag = D_DISK | D_MPSAFE
    111  1.1     ahoka };
    112  1.1     ahoka 
    113  1.1     ahoka /**
    114  1.1     ahoka  * Character device's operations
    115  1.1     ahoka  */
    116  1.1     ahoka const struct cdevsw flash_cdevsw = {
    117  1.1     ahoka 	.d_open = flashopen,
    118  1.1     ahoka 	.d_close = flashclose,
    119  1.1     ahoka 	.d_read = flashread,
    120  1.1     ahoka 	.d_write = flashwrite,
    121  1.1     ahoka 	.d_ioctl = flashioctl,
    122  1.1     ahoka 	.d_stop = nostop,
    123  1.1     ahoka 	.d_tty = notty,
    124  1.1     ahoka 	.d_poll = nopoll,
    125  1.1     ahoka 	.d_mmap = nommap,
    126  1.1     ahoka 	.d_kqfilter = nokqfilter,
    127  1.1     ahoka 	.d_flag = D_DISK | D_MPSAFE
    128  1.1     ahoka };
    129  1.1     ahoka 
    130  1.1     ahoka /* ARGSUSED */
    131  1.1     ahoka int
    132  1.1     ahoka flash_match(device_t parent, cfdata_t match, void *aux)
    133  1.1     ahoka {
    134  1.1     ahoka 	/* pseudo device, always attaches */
    135  1.1     ahoka 	return 1;
    136  1.1     ahoka }
    137  1.1     ahoka 
    138  1.1     ahoka /* ARGSUSED */
    139  1.1     ahoka void
    140  1.1     ahoka flash_attach(device_t parent, device_t self, void *aux)
    141  1.1     ahoka {
    142  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    143  1.1     ahoka 	struct flash_attach_args *faa = aux;
    144  1.1     ahoka 	char pbuf[2][sizeof("9999 KB")];
    145  1.1     ahoka 
    146  1.1     ahoka 	sc->sc_dev = self;
    147  1.1     ahoka 	sc->sc_parent_dev = parent;
    148  1.1     ahoka 	sc->flash_if = faa->flash_if;
    149  1.1     ahoka 	sc->hw_softc = device_private(parent);
    150  1.1     ahoka 
    151  1.1     ahoka 	format_bytes(pbuf[0], sizeof(pbuf[0]), sc->flash_if->size);
    152  1.1     ahoka 	format_bytes(pbuf[1], sizeof(pbuf[1]), sc->flash_if->erasesize);
    153  1.1     ahoka 
    154  1.1     ahoka 	aprint_naive("\n");
    155  1.6     ahoka 
    156  1.1     ahoka 	switch (sc->flash_if->type) {
    157  1.1     ahoka 	case FLASH_TYPE_NOR:
    158  1.1     ahoka 		aprint_normal(": %s NOR flash\n", pbuf[0]);
    159  1.1     ahoka 		break;
    160  1.1     ahoka 
    161  1.1     ahoka 	case FLASH_TYPE_NAND:
    162  1.1     ahoka 		aprint_normal(": %s NAND flash\n", pbuf[0]);
    163  1.1     ahoka 		break;
    164  1.6     ahoka 
    165  1.1     ahoka 	default:
    166  1.1     ahoka 		aprint_normal(": %s unknown flash\n", pbuf[0]);
    167  1.1     ahoka 	}
    168  1.1     ahoka 
    169  1.1     ahoka 	aprint_normal_dev(sc->sc_dev,
    170  1.5      matt 	    "size: %#jx, offset: %#jx",
    171  1.1     ahoka 	    (uintmax_t )sc->flash_if->partition.part_size,
    172  1.1     ahoka 	    (uintmax_t )sc->flash_if->partition.part_offset);
    173  1.1     ahoka 
    174  1.1     ahoka 	if (sc->flash_if->partition.part_flags & FLASH_PART_READONLY) {
    175  1.1     ahoka 		sc->sc_readonly = true;
    176  1.1     ahoka 		aprint_normal(", read only");
    177  1.1     ahoka 	} else {
    178  1.1     ahoka 		sc->sc_readonly = false;
    179  1.1     ahoka 	}
    180  1.1     ahoka 
    181  1.1     ahoka 	aprint_normal("\n");
    182  1.1     ahoka 
    183  1.1     ahoka 	if (sc->flash_if->partition.part_size == 0) {
    184  1.1     ahoka 		aprint_error_dev(self,
    185  1.1     ahoka 		    "partition size must be larger than 0\n");
    186  1.1     ahoka 		return;
    187  1.1     ahoka 	}
    188  1.6     ahoka 
    189  1.1     ahoka 	switch (sc->flash_if->type) {
    190  1.1     ahoka 	case FLASH_TYPE_NOR:
    191  1.1     ahoka 		aprint_normal_dev(sc->sc_dev,
    192  1.1     ahoka 		    "erase size %s bytes, write size %d bytes\n",
    193  1.1     ahoka 		    pbuf[1], sc->flash_if->writesize);
    194  1.1     ahoka 		break;
    195  1.1     ahoka 
    196  1.1     ahoka 	case FLASH_TYPE_NAND:
    197  1.1     ahoka 	default:
    198  1.1     ahoka 		aprint_normal_dev(sc->sc_dev,
    199  1.1     ahoka 		    "erase size %s, page size %d bytes, write size %d bytes\n",
    200  1.1     ahoka 		    pbuf[1], sc->flash_if->page_size,
    201  1.1     ahoka 		    sc->flash_if->writesize);
    202  1.1     ahoka 		break;
    203  1.1     ahoka 	}
    204  1.6     ahoka 
    205  1.1     ahoka 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, flash_shutdown))
    206  1.1     ahoka 		aprint_error_dev(sc->sc_dev,
    207  1.1     ahoka 		    "couldn't establish power handler\n");
    208  1.1     ahoka }
    209  1.1     ahoka 
    210  1.1     ahoka int
    211  1.1     ahoka flash_detach(device_t device, int flags)
    212  1.1     ahoka {
    213  1.1     ahoka 	struct flash_softc *sc = device_private(device);
    214  1.1     ahoka 
    215  1.1     ahoka 	pmf_device_deregister(sc->sc_dev);
    216  1.1     ahoka 
    217  1.1     ahoka 	/* freeing flash_if is our responsibility */
    218  1.1     ahoka 	kmem_free(sc->flash_if, sizeof(*sc->flash_if));
    219  1.6     ahoka 
    220  1.1     ahoka 	return 0;
    221  1.1     ahoka }
    222  1.1     ahoka 
    223  1.1     ahoka int
    224  1.1     ahoka flash_print(void *aux, const char *pnp)
    225  1.1     ahoka {
    226  1.1     ahoka 	struct flash_attach_args *arg;
    227  1.1     ahoka 	const char *type;
    228  1.1     ahoka 
    229  1.1     ahoka 	if (pnp != NULL) {
    230  1.1     ahoka 		arg = aux;
    231  1.1     ahoka 		switch (arg->flash_if->type) {
    232  1.1     ahoka 		case FLASH_TYPE_NOR:
    233  1.1     ahoka 			type = "NOR";
    234  1.1     ahoka 			break;
    235  1.1     ahoka 		case FLASH_TYPE_NAND:
    236  1.1     ahoka 			type = "NAND";
    237  1.1     ahoka 			break;
    238  1.1     ahoka 		default:
    239  1.1     ahoka 			panic("flash_print: unknown type %d",
    240  1.1     ahoka 			    arg->flash_if->type);
    241  1.1     ahoka 		}
    242  1.1     ahoka 		aprint_normal("%s flash at %s", type, pnp);
    243  1.1     ahoka 	}
    244  1.1     ahoka 	return UNCONF;
    245  1.1     ahoka }
    246  1.1     ahoka 
    247  1.1     ahoka device_t
    248  1.1     ahoka flash_attach_mi(struct flash_interface *flash_if, device_t device)
    249  1.1     ahoka {
    250  1.1     ahoka 	struct flash_attach_args arg;
    251  1.1     ahoka 
    252  1.1     ahoka #ifdef DIAGNOSTIC
    253  1.1     ahoka 	if (flash_if == NULL) {
    254  1.1     ahoka 		aprint_error("flash_attach_mi: NULL\n");
    255  1.1     ahoka 		return 0;
    256  1.1     ahoka 	}
    257  1.1     ahoka #endif
    258  1.1     ahoka 	arg.flash_if = flash_if;
    259  1.1     ahoka 
    260  1.1     ahoka 	return config_found_ia(device, "flashbus", &arg, flash_print);
    261  1.1     ahoka }
    262  1.1     ahoka 
    263  1.1     ahoka /**
    264  1.1     ahoka  * flash_open - open the character device
    265  1.1     ahoka  * Checks if there is a driver registered to the minor number of the open
    266  1.1     ahoka  * request.
    267  1.1     ahoka  */
    268  1.1     ahoka int
    269  1.1     ahoka flashopen(dev_t dev, int flags, int fmt, struct lwp *l)
    270  1.1     ahoka {
    271  1.1     ahoka 	int unit = minor(dev);
    272  1.1     ahoka 	struct flash_softc *sc;
    273  1.1     ahoka 
    274  1.1     ahoka 	DPRINTFN(1, ("flash: opening device unit %d\n", unit));
    275  1.1     ahoka 
    276  1.1     ahoka 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    277  1.1     ahoka 		return ENXIO;
    278  1.1     ahoka 
    279  1.1     ahoka 	/* TODO return eperm if want to open for writing a read only dev */
    280  1.1     ahoka 
    281  1.1     ahoka 	/* reset buffer length */
    282  1.1     ahoka //	sc->sc_cache->fc_len = 0;
    283  1.6     ahoka 
    284  1.1     ahoka 	return 0;
    285  1.1     ahoka }
    286  1.1     ahoka 
    287  1.1     ahoka /**
    288  1.1     ahoka  * flash_close - close device
    289  1.1     ahoka  * We don't have to release any resources, so just return 0.
    290  1.1     ahoka  */
    291  1.1     ahoka int
    292  1.1     ahoka flashclose(dev_t dev, int flags, int fmt, struct lwp *l)
    293  1.1     ahoka {
    294  1.1     ahoka 	int unit = minor(dev);
    295  1.1     ahoka 	struct flash_softc *sc;
    296  1.1     ahoka 	int err;
    297  1.1     ahoka 
    298  1.1     ahoka 	DPRINTFN(1, ("flash: closing flash device unit %d\n", unit));
    299  1.1     ahoka 
    300  1.1     ahoka 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    301  1.1     ahoka 		return ENXIO;
    302  1.1     ahoka 
    303  1.1     ahoka 	if (!sc->sc_readonly) {
    304  1.1     ahoka 		err = flash_sync(sc->sc_dev);
    305  1.1     ahoka 		if (err)
    306  1.1     ahoka 			return err;
    307  1.1     ahoka 	}
    308  1.6     ahoka 
    309  1.1     ahoka 	return 0;
    310  1.1     ahoka }
    311  1.1     ahoka 
    312  1.1     ahoka /**
    313  1.1     ahoka  * flash_read - read from character device
    314  1.1     ahoka  * This function uses the registered driver's read function to read the requested length to
    315  1.1     ahoka  * a buffer and then moves this buffer to userspace.
    316  1.1     ahoka  */
    317  1.1     ahoka int
    318  1.1     ahoka flashread(dev_t dev, struct uio *uio, int flag)
    319  1.1     ahoka {
    320  1.1     ahoka 	return physio(flashstrategy, NULL, dev, B_READ, minphys, uio);
    321  1.1     ahoka }
    322  1.1     ahoka 
    323  1.1     ahoka /**
    324  1.1     ahoka  * flash_write - write to character device
    325  1.1     ahoka  * This function moves the data into a buffer from userspace to kernel space,
    326  1.1     ahoka  * then uses the registered driver's write function to write out the data to
    327  1.1     ahoka  * the media.
    328  1.1     ahoka  */
    329  1.1     ahoka int
    330  1.1     ahoka flashwrite(dev_t dev, struct uio *uio, int flag)
    331  1.1     ahoka {
    332  1.1     ahoka 	return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio);
    333  1.1     ahoka }
    334  1.1     ahoka 
    335  1.1     ahoka void
    336  1.1     ahoka flashstrategy(struct buf *bp)
    337  1.1     ahoka {
    338  1.1     ahoka 	struct flash_softc *sc;
    339  1.1     ahoka 	const struct flash_interface *flash_if;
    340  1.1     ahoka 	const struct flash_partition *part;
    341  1.1     ahoka 	int unit, device_blks;
    342  1.1     ahoka 
    343  1.1     ahoka 	unit = minor(bp->b_dev);
    344  1.1     ahoka 	sc = device_lookup_private(&flash_cd, unit);
    345  1.1     ahoka 	if (sc == NULL) {
    346  1.1     ahoka 		bp->b_error = ENXIO;
    347  1.1     ahoka 		goto done;
    348  1.1     ahoka 	}
    349  1.1     ahoka 
    350  1.1     ahoka 	flash_if = sc->flash_if;
    351  1.1     ahoka 	part = &flash_if->partition;
    352  1.1     ahoka 
    353  1.1     ahoka 	/* divider */
    354  1.1     ahoka 	KASSERT(flash_if->writesize != 0);
    355  1.6     ahoka 
    356  1.1     ahoka 	aprint_debug_dev(sc->sc_dev, "flash_strategy()\n");
    357  1.1     ahoka 
    358  1.1     ahoka 	if (!(bp->b_flags & B_READ) && sc->sc_readonly) {
    359  1.1     ahoka 		bp->b_error = EACCES;
    360  1.1     ahoka 		goto done;
    361  1.1     ahoka 	}
    362  1.1     ahoka 
    363  1.1     ahoka 	/* check if length is not negative */
    364  1.1     ahoka 	if (bp->b_blkno < 0) {
    365  1.1     ahoka 		bp->b_error = EINVAL;
    366  1.1     ahoka 		goto done;
    367  1.1     ahoka 	}
    368  1.1     ahoka 
    369  1.1     ahoka 	/* zero lenght i/o */
    370  1.1     ahoka 	if (bp->b_bcount == 0) {
    371  1.1     ahoka 		goto done;
    372  1.1     ahoka 	}
    373  1.1     ahoka 
    374  1.1     ahoka 	device_blks = sc->flash_if->size / DEV_BSIZE;
    375  1.1     ahoka 	KASSERT(part->part_offset % DEV_BSIZE == 0);
    376  1.1     ahoka 	bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE);
    377  1.1     ahoka 
    378  1.1     ahoka 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) {
    379  1.1     ahoka 		goto done;
    380  1.1     ahoka 	}
    381  1.1     ahoka 
    382  1.1     ahoka 	bp->b_resid = bp->b_bcount;
    383  1.1     ahoka 	flash_if->submit(sc->sc_parent_dev, bp);
    384  1.6     ahoka 
    385  1.1     ahoka 	return;
    386  1.1     ahoka done:
    387  1.1     ahoka 	bp->b_resid = bp->b_bcount;
    388  1.1     ahoka 	biodone(bp);
    389  1.1     ahoka }
    390  1.1     ahoka 
    391  1.1     ahoka /*
    392  1.1     ahoka  * Handle the ioctl for the device
    393  1.1     ahoka  */
    394  1.1     ahoka int
    395  1.1     ahoka flashioctl(dev_t dev, u_long command, void *data, int flags, struct lwp *l)
    396  1.1     ahoka {
    397  1.1     ahoka 	struct flash_erase_params *ep;
    398  1.1     ahoka 	struct flash_info_params *ip;
    399  1.1     ahoka 	struct flash_dump_params *dp;
    400  1.1     ahoka 	struct flash_badblock_params *bbp;
    401  1.1     ahoka 	struct flash_erase_instruction ei;
    402  1.1     ahoka 	struct flash_softc *sc;
    403  1.1     ahoka 	int unit, err;
    404  1.1     ahoka 	size_t retlen;
    405  1.3     ahoka 	flash_off_t offset;
    406  1.3     ahoka 	bool bad;
    407  1.1     ahoka 
    408  1.1     ahoka 	unit = minor(dev);
    409  1.1     ahoka 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    410  1.1     ahoka 		return ENXIO;
    411  1.1     ahoka 
    412  1.1     ahoka 	err = 0;
    413  1.1     ahoka 	switch (command) {
    414  1.1     ahoka 	case FLASH_ERASE_BLOCK:
    415  1.1     ahoka 		/**
    416  1.1     ahoka 		 * Set up an erase instruction then call the registered
    417  1.1     ahoka 		 * driver's erase operation.
    418  1.1     ahoka 		 */
    419  1.1     ahoka 		ep = data;
    420  1.1     ahoka 
    421  1.1     ahoka 		if (sc->sc_readonly) {
    422  1.1     ahoka 			return EACCES;
    423  1.1     ahoka 		}
    424  1.1     ahoka 
    425  1.1     ahoka 		ei.ei_addr = ep->ep_addr;
    426  1.1     ahoka 		ei.ei_len = ep->ep_len;
    427  1.1     ahoka 		ei.ei_callback = NULL;
    428  1.6     ahoka 
    429  1.1     ahoka 		err = flash_erase(sc->sc_dev, &ei);
    430  1.1     ahoka 		if (err) {
    431  1.1     ahoka 			return err;
    432  1.1     ahoka 		}
    433  1.1     ahoka 
    434  1.1     ahoka 		break;
    435  1.1     ahoka 	case FLASH_BLOCK_ISBAD:
    436  1.1     ahoka 		/**
    437  1.1     ahoka 		 * Set up an erase instruction then call the registered
    438  1.1     ahoka 		 * driver's erase operation.
    439  1.1     ahoka 		 */
    440  1.1     ahoka 		bbp = data;
    441  1.1     ahoka 
    442  1.3     ahoka 		err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad);
    443  1.3     ahoka 		if (err) {
    444  1.1     ahoka 			return err;
    445  1.1     ahoka 		}
    446  1.3     ahoka 		bbp->bbp_isbad = bad;
    447  1.1     ahoka 
    448  1.1     ahoka 		break;
    449  1.1     ahoka 	case FLASH_BLOCK_MARKBAD:
    450  1.1     ahoka 		bbp = data;
    451  1.1     ahoka 
    452  1.1     ahoka 		err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr);
    453  1.1     ahoka 
    454  1.6     ahoka 		break;
    455  1.1     ahoka 	case FLASH_DUMP:
    456  1.1     ahoka 		dp = data;
    457  1.1     ahoka 		offset = dp->dp_block * sc->flash_if->erasesize;
    458  1.1     ahoka 		DPRINTF(("Reading from block: %jd len: %jd\n",
    459  1.1     ahoka 			(intmax_t )dp->dp_block, (intmax_t )dp->dp_len));
    460  1.1     ahoka 		err = flash_read(sc->sc_parent_dev, offset, dp->dp_len,
    461  1.1     ahoka 		    &retlen, dp->dp_buf);
    462  1.1     ahoka 		if (err)
    463  1.1     ahoka 			return err;
    464  1.1     ahoka 		if (retlen != dp->dp_len) {
    465  1.1     ahoka 			dp->dp_len = -1;
    466  1.1     ahoka 			dp->dp_buf = NULL;
    467  1.1     ahoka 		}
    468  1.1     ahoka 
    469  1.1     ahoka 		break;
    470  1.1     ahoka 	case FLASH_GET_INFO:
    471  1.1     ahoka 		ip = data;
    472  1.6     ahoka 
    473  1.1     ahoka 		ip->ip_page_size = sc->flash_if->page_size;
    474  1.1     ahoka 		ip->ip_erase_size = sc->flash_if->erasesize;
    475  1.1     ahoka 		ip->ip_flash_type = sc->flash_if->type;
    476  1.1     ahoka 		ip->ip_flash_size = sc->flash_if->size;
    477  1.1     ahoka 		break;
    478  1.1     ahoka 	default:
    479  1.1     ahoka 		err = ENODEV;
    480  1.1     ahoka 	}
    481  1.1     ahoka 
    482  1.1     ahoka 	return err;
    483  1.1     ahoka }
    484  1.2  uebayasi 
    485  1.1     ahoka int
    486  1.1     ahoka flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
    487  1.1     ahoka {
    488  1.1     ahoka     return EACCES;
    489  1.1     ahoka }
    490  1.1     ahoka 
    491  1.1     ahoka bool
    492  1.1     ahoka flash_shutdown(device_t self, int how)
    493  1.1     ahoka {
    494  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    495  1.6     ahoka 
    496  1.1     ahoka 	if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly)
    497  1.1     ahoka 		flash_sync(self);
    498  1.1     ahoka 
    499  1.1     ahoka 	return true;
    500  1.1     ahoka }
    501  1.1     ahoka 
    502  1.1     ahoka const struct flash_interface *
    503  1.1     ahoka flash_get_interface(dev_t dev)
    504  1.1     ahoka {
    505  1.1     ahoka 	struct flash_softc *sc;
    506  1.1     ahoka 	int unit;
    507  1.1     ahoka 
    508  1.1     ahoka 	unit = minor(dev);
    509  1.1     ahoka 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
    510  1.1     ahoka 		return NULL;
    511  1.1     ahoka 
    512  1.1     ahoka 	return sc->flash_if;
    513  1.1     ahoka }
    514  1.1     ahoka 
    515  1.1     ahoka const struct flash_softc *
    516  1.1     ahoka flash_get_softc(dev_t dev)
    517  1.1     ahoka {
    518  1.1     ahoka 	struct flash_softc *sc;
    519  1.1     ahoka 	int unit;
    520  1.1     ahoka 
    521  1.1     ahoka 	unit = minor(dev);
    522  1.1     ahoka 	sc = device_lookup_private(&flash_cd, unit);
    523  1.1     ahoka 
    524  1.1     ahoka 	return sc;
    525  1.1     ahoka }
    526  1.1     ahoka 
    527  1.1     ahoka device_t
    528  1.1     ahoka flash_get_device(dev_t dev)
    529  1.1     ahoka {
    530  1.1     ahoka 	struct flash_softc *sc;
    531  1.1     ahoka 	int unit;
    532  1.1     ahoka 
    533  1.1     ahoka 	unit = minor(dev);
    534  1.1     ahoka 	sc = device_lookup_private(&flash_cd, unit);
    535  1.1     ahoka 
    536  1.1     ahoka 	return sc->sc_dev;
    537  1.1     ahoka }
    538  1.1     ahoka 
    539  1.3     ahoka static inline flash_off_t
    540  1.1     ahoka flash_get_part_offset(struct flash_softc *fl, size_t poffset)
    541  1.1     ahoka {
    542  1.1     ahoka 	return fl->flash_if->partition.part_offset + poffset;
    543  1.1     ahoka }
    544  1.1     ahoka 
    545  1.1     ahoka int
    546  1.1     ahoka flash_erase(device_t self, struct flash_erase_instruction *ei)
    547  1.1     ahoka {
    548  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    549  1.1     ahoka 	KASSERT(ei != NULL);
    550  1.1     ahoka 	struct flash_erase_instruction e = *ei;
    551  1.1     ahoka 
    552  1.1     ahoka 	if (sc->sc_readonly)
    553  1.1     ahoka 		return EACCES;
    554  1.1     ahoka 
    555  1.1     ahoka 	/* adjust for flash partition */
    556  1.1     ahoka 	e.ei_addr += sc->flash_if->partition.part_offset;
    557  1.1     ahoka 
    558  1.1     ahoka 	/* bounds check for flash partition */
    559  1.1     ahoka 	if (e.ei_addr + e.ei_len > sc->flash_if->partition.part_size +
    560  1.1     ahoka 	    sc->flash_if->partition.part_offset)
    561  1.1     ahoka 		return EINVAL;
    562  1.6     ahoka 
    563  1.1     ahoka 	return sc->flash_if->erase(device_parent(self), &e);
    564  1.1     ahoka }
    565  1.1     ahoka 
    566  1.1     ahoka int
    567  1.1     ahoka flash_read(device_t self,
    568  1.3     ahoka     flash_off_t offset, size_t len, size_t *retlen, uint8_t *buf)
    569  1.1     ahoka {
    570  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    571  1.1     ahoka 
    572  1.1     ahoka 	offset += sc->flash_if->partition.part_offset;
    573  1.1     ahoka 
    574  1.1     ahoka 	if (offset + len > sc->flash_if->partition.part_size +
    575  1.1     ahoka 	    sc->flash_if->partition.part_offset)
    576  1.1     ahoka 		return EINVAL;
    577  1.1     ahoka 
    578  1.1     ahoka 	return sc->flash_if->read(device_parent(self),
    579  1.1     ahoka 	    offset, len, retlen, buf);
    580  1.1     ahoka }
    581  1.1     ahoka 
    582  1.1     ahoka int
    583  1.1     ahoka flash_write(device_t self,
    584  1.3     ahoka     flash_off_t offset, size_t len, size_t *retlen, const uint8_t *buf)
    585  1.1     ahoka {
    586  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    587  1.1     ahoka 
    588  1.1     ahoka 	if (sc->sc_readonly)
    589  1.1     ahoka 		return EACCES;
    590  1.1     ahoka 
    591  1.1     ahoka 	offset += sc->flash_if->partition.part_offset;
    592  1.1     ahoka 
    593  1.1     ahoka 	if (offset + len > sc->flash_if->partition.part_size +
    594  1.1     ahoka 	    sc->flash_if->partition.part_offset)
    595  1.1     ahoka 		return EINVAL;
    596  1.1     ahoka 
    597  1.1     ahoka 	return sc->flash_if->write(device_parent(self),
    598  1.1     ahoka 	    offset, len, retlen, buf);
    599  1.1     ahoka }
    600  1.1     ahoka 
    601  1.1     ahoka int
    602  1.3     ahoka flash_block_markbad(device_t self, flash_off_t offset)
    603  1.1     ahoka {
    604  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    605  1.1     ahoka 
    606  1.1     ahoka 	if (sc->sc_readonly)
    607  1.1     ahoka 		return EACCES;
    608  1.6     ahoka 
    609  1.1     ahoka 	offset += sc->flash_if->partition.part_offset;
    610  1.6     ahoka 
    611  1.1     ahoka 	if (offset + sc->flash_if->erasesize >=
    612  1.1     ahoka 	    sc->flash_if->partition.part_size +
    613  1.1     ahoka 	    sc->flash_if->partition.part_offset)
    614  1.1     ahoka 		return EINVAL;
    615  1.1     ahoka 
    616  1.1     ahoka 	return sc->flash_if->block_markbad(device_parent(self), offset);
    617  1.1     ahoka }
    618  1.1     ahoka 
    619  1.1     ahoka int
    620  1.3     ahoka flash_block_isbad(device_t self, flash_off_t offset, bool *bad)
    621  1.1     ahoka {
    622  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    623  1.6     ahoka 
    624  1.1     ahoka 	offset += sc->flash_if->partition.part_offset;
    625  1.1     ahoka 
    626  1.1     ahoka 	if (offset + sc->flash_if->erasesize >
    627  1.1     ahoka 	    sc->flash_if->partition.part_size +
    628  1.1     ahoka 	    sc->flash_if->partition.part_offset)
    629  1.1     ahoka 		return EINVAL;
    630  1.1     ahoka 
    631  1.3     ahoka 	return sc->flash_if->block_isbad(device_parent(self), offset, bad);
    632  1.1     ahoka }
    633  1.1     ahoka 
    634  1.1     ahoka int
    635  1.1     ahoka flash_sync(device_t self)
    636  1.1     ahoka {
    637  1.1     ahoka 	struct flash_softc *sc = device_private(self);
    638  1.6     ahoka 
    639  1.1     ahoka 	if (sc->sc_readonly)
    640  1.1     ahoka 		return EACCES;
    641  1.1     ahoka 
    642  1.1     ahoka 	/* noop now TODO: implement */
    643  1.1     ahoka 	return 0;
    644  1.1     ahoka }
    645  1.1     ahoka 
    646  1.1     ahoka MODULE(MODULE_CLASS_DRIVER, flash, NULL);
    647  1.1     ahoka 
    648  1.1     ahoka #ifdef _MODULE
    649  1.1     ahoka #include "ioconf.c"
    650  1.1     ahoka #endif
    651  1.1     ahoka 
    652  1.1     ahoka static int
    653  1.1     ahoka flash_modcmd(modcmd_t cmd, void *opaque)
    654  1.1     ahoka {
    655  1.1     ahoka 	int error = 0;
    656  1.1     ahoka #ifdef _MODULE
    657  1.1     ahoka 	int bmaj = -1, cmaj = -1;
    658  1.1     ahoka #endif
    659  1.1     ahoka 
    660  1.1     ahoka 	switch (cmd) {
    661  1.1     ahoka 	case MODULE_CMD_INIT:
    662  1.1     ahoka #ifdef _MODULE
    663  1.1     ahoka 		error = config_init_component(cfdriver_ioconf_flash,
    664  1.1     ahoka 		    cfattach_ioconf_flash, cfdata_ioconf_flash);
    665  1.1     ahoka 		if (error)
    666  1.1     ahoka 			return error;
    667  1.1     ahoka 		error = devsw_attach("flash", &flash_bdevsw, &bmaj,
    668  1.1     ahoka 		    &flash_cdevsw, &cmaj);
    669  1.1     ahoka 		if (error)
    670  1.1     ahoka 			config_fini_component(cfdriver_ioconf_flash,
    671  1.1     ahoka 			    cfattach_ioconf_flash, cfdata_ioconf_flash);
    672  1.1     ahoka #endif
    673  1.1     ahoka 		return error;
    674  1.1     ahoka 	case MODULE_CMD_FINI:
    675  1.1     ahoka #ifdef _MODULE
    676  1.1     ahoka 		devsw_detach(&flash_bdevsw, &flash_cdevsw);
    677  1.1     ahoka 		error = config_fini_component(cfdriver_ioconf_flash,
    678  1.1     ahoka 		    cfattach_ioconf_flash, cfdata_ioconf_flash);
    679  1.1     ahoka #endif
    680  1.1     ahoka 		return error;
    681  1.1     ahoka 	default:
    682  1.1     ahoka 		return ENOTTY;
    683  1.1     ahoka 	}
    684  1.1     ahoka }
    685