2022-09-26 00:53:54 +03:00
|
|
|
/* $NetBSD: flash.c,v 1.19 2022/09/25 21:56:12 thorpej Exp $ */
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 Department of Software Engineering,
|
|
|
|
* University of Szeged, Hungary
|
|
|
|
* Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org>
|
|
|
|
* Copyright (c) 2010 David Tengeri <dtengeri@inf.u-szeged.hu>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by the Department of Software Engineering, University of Szeged, Hungary
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Framework for storage devices based on Flash technology
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2022-09-26 00:53:54 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.19 2022/09/25 21:56:12 thorpej Exp $");
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
|
|
|
|
#include <sys/atomic.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/bufq.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
|
2017-10-28 07:53:54 +03:00
|
|
|
#include "ioconf.h"
|
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
#include <sys/flashio.h>
|
|
|
|
#include "flash.h"
|
|
|
|
|
|
|
|
#ifdef FLASH_DEBUG
|
2011-06-28 22:14:11 +04:00
|
|
|
int flashdebug = FLASH_DEBUG;
|
2011-02-26 21:07:13 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
dev_type_open(flashopen);
|
|
|
|
dev_type_close(flashclose);
|
|
|
|
dev_type_read(flashread);
|
|
|
|
dev_type_write(flashwrite);
|
|
|
|
dev_type_ioctl(flashioctl);
|
|
|
|
dev_type_strategy(flashstrategy);
|
|
|
|
dev_type_dump(flashdump);
|
|
|
|
|
|
|
|
int flash_print(void *aux, const char *pnp);
|
|
|
|
|
|
|
|
bool flash_shutdown(device_t dev, int how);
|
|
|
|
int flash_nsectors(struct buf *bp);
|
|
|
|
int flash_sector(struct buf *bp);
|
|
|
|
|
|
|
|
int flash_match(device_t parent, cfdata_t match, void *aux);
|
|
|
|
void flash_attach(device_t parent, device_t self, void *aux);
|
|
|
|
int flash_detach(device_t device, int flags);
|
|
|
|
|
|
|
|
CFATTACH_DECL_NEW(flash, sizeof(struct flash_softc),
|
|
|
|
flash_match, flash_attach, flash_detach, NULL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block device's operation
|
|
|
|
*/
|
|
|
|
const struct bdevsw flash_bdevsw = {
|
|
|
|
.d_open = flashopen,
|
|
|
|
.d_close = flashclose,
|
|
|
|
.d_strategy = flashstrategy,
|
|
|
|
.d_ioctl = flashioctl,
|
|
|
|
.d_dump = flashdump,
|
2011-06-28 11:00:17 +04:00
|
|
|
.d_psize = nosize,
|
2014-07-25 12:02:18 +04:00
|
|
|
.d_discard = nodiscard, /* XXX this driver probably wants a discard */
|
2011-02-26 21:07:13 +03:00
|
|
|
.d_flag = D_DISK | D_MPSAFE
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Character device's operations
|
|
|
|
*/
|
|
|
|
const struct cdevsw flash_cdevsw = {
|
|
|
|
.d_open = flashopen,
|
|
|
|
.d_close = flashclose,
|
|
|
|
.d_read = flashread,
|
|
|
|
.d_write = flashwrite,
|
|
|
|
.d_ioctl = flashioctl,
|
|
|
|
.d_stop = nostop,
|
|
|
|
.d_tty = notty,
|
|
|
|
.d_poll = nopoll,
|
|
|
|
.d_mmap = nommap,
|
|
|
|
.d_kqfilter = nokqfilter,
|
2014-07-25 12:10:31 +04:00
|
|
|
.d_discard = nodiscard,
|
2011-02-26 21:07:13 +03:00
|
|
|
.d_flag = D_DISK | D_MPSAFE
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
|
|
|
flash_match(device_t parent, cfdata_t match, void *aux)
|
|
|
|
{
|
|
|
|
/* pseudo device, always attaches */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
|
|
|
flash_attach(device_t parent, device_t self, void *aux)
|
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
|
|
|
struct flash_attach_args * const faa = aux;
|
2011-02-26 21:07:13 +03:00
|
|
|
char pbuf[2][sizeof("9999 KB")];
|
|
|
|
|
|
|
|
sc->sc_dev = self;
|
|
|
|
sc->sc_parent_dev = parent;
|
|
|
|
sc->flash_if = faa->flash_if;
|
2011-06-28 22:14:11 +04:00
|
|
|
sc->sc_partinfo = faa->partinfo;
|
2011-02-26 21:07:13 +03:00
|
|
|
sc->hw_softc = device_private(parent);
|
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
format_bytes(pbuf[0], sizeof(pbuf[0]), sc->sc_partinfo.part_size);
|
2011-02-26 21:07:13 +03:00
|
|
|
format_bytes(pbuf[1], sizeof(pbuf[1]), sc->flash_if->erasesize);
|
|
|
|
|
|
|
|
aprint_naive("\n");
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2017-11-13 20:35:58 +03:00
|
|
|
aprint_normal(": partition");
|
|
|
|
if (sc->sc_partinfo.part_name != NULL)
|
|
|
|
aprint_normal(" \"%s\"", sc->sc_partinfo.part_name);
|
2011-02-26 21:07:13 +03:00
|
|
|
|
2017-11-13 20:35:58 +03:00
|
|
|
aprint_normal(", size %s, offset %#jx",
|
|
|
|
pbuf[0], (uintmax_t)sc->sc_partinfo.part_offset);
|
2011-02-26 21:07:13 +03:00
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
if (sc->sc_partinfo.part_flags & FLASH_PART_READONLY) {
|
2011-02-26 21:07:13 +03:00
|
|
|
sc->sc_readonly = true;
|
|
|
|
aprint_normal(", read only");
|
|
|
|
} else {
|
|
|
|
sc->sc_readonly = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aprint_normal("\n");
|
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
if (sc->sc_partinfo.part_size == 0) {
|
2011-02-26 21:07:13 +03:00
|
|
|
aprint_error_dev(self,
|
|
|
|
"partition size must be larger than 0\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
switch (sc->flash_if->type) {
|
|
|
|
case FLASH_TYPE_NOR:
|
|
|
|
aprint_normal_dev(sc->sc_dev,
|
|
|
|
"erase size %s bytes, write size %d bytes\n",
|
|
|
|
pbuf[1], sc->flash_if->writesize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLASH_TYPE_NAND:
|
|
|
|
default:
|
|
|
|
aprint_normal_dev(sc->sc_dev,
|
|
|
|
"erase size %s, page size %d bytes, write size %d bytes\n",
|
|
|
|
pbuf[1], sc->flash_if->page_size,
|
|
|
|
sc->flash_if->writesize);
|
|
|
|
break;
|
|
|
|
}
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
if (!pmf_device_register1(sc->sc_dev, NULL, NULL, flash_shutdown))
|
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"couldn't establish power handler\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
flash_detach(device_t device, int flags)
|
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(device);
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
pmf_device_deregister(sc->sc_dev);
|
|
|
|
|
|
|
|
/* freeing flash_if is our responsibility */
|
|
|
|
kmem_free(sc->flash_if, sizeof(*sc->flash_if));
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
flash_print(void *aux, const char *pnp)
|
|
|
|
{
|
|
|
|
struct flash_attach_args *arg;
|
|
|
|
const char *type;
|
|
|
|
|
|
|
|
if (pnp != NULL) {
|
|
|
|
arg = aux;
|
|
|
|
switch (arg->flash_if->type) {
|
|
|
|
case FLASH_TYPE_NOR:
|
|
|
|
type = "NOR";
|
|
|
|
break;
|
|
|
|
case FLASH_TYPE_NAND:
|
|
|
|
type = "NAND";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("flash_print: unknown type %d",
|
|
|
|
arg->flash_if->type);
|
|
|
|
}
|
|
|
|
aprint_normal("%s flash at %s", type, pnp);
|
|
|
|
}
|
|
|
|
return UNCONF;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_t
|
2011-07-15 23:19:55 +04:00
|
|
|
flash_attach_mi(struct flash_interface * const flash_if, device_t device)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
struct flash_attach_args arg;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (flash_if == NULL) {
|
|
|
|
aprint_error("flash_attach_mi: NULL\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
arg.flash_if = flash_if;
|
|
|
|
|
Merge thorpej-cfargs branch:
Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.
Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)
Remove unnecessary or redundant interface attributes where they're not
needed.
There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)
...and a sentinel value CFARG_EOL.
Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.
Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
2021-04-25 02:36:23 +03:00
|
|
|
return config_found(device, &arg, flash_print,
|
2021-08-07 19:18:40 +03:00
|
|
|
CFARGS(.iattr = "flashbus"));
|
2011-02-26 21:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* flash_open - open the character device
|
|
|
|
* Checks if there is a driver registered to the minor number of the open
|
|
|
|
* request.
|
|
|
|
*/
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flashopen(dev_t dev, int flags, int fmt, lwp_t *l)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
int unit = minor(dev);
|
|
|
|
struct flash_softc *sc;
|
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
FLDPRINTFN(1, ("flash: opening device unit %d\n", unit));
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
|
|
|
|
/* TODO return eperm if want to open for writing a read only dev */
|
|
|
|
|
|
|
|
/* reset buffer length */
|
|
|
|
// sc->sc_cache->fc_len = 0;
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* flash_close - close device
|
|
|
|
* We don't have to release any resources, so just return 0.
|
|
|
|
*/
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flashclose(dev_t dev, int flags, int fmt, lwp_t *l)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
int unit = minor(dev);
|
|
|
|
struct flash_softc *sc;
|
|
|
|
int err;
|
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
FLDPRINTFN(1, ("flash: closing flash device unit %d\n", unit));
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
|
|
|
|
if (!sc->sc_readonly) {
|
|
|
|
err = flash_sync(sc->sc_dev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* flash_read - read from character device
|
2011-07-15 23:19:55 +04:00
|
|
|
* This function uses the registered driver's read function to read the
|
|
|
|
* requested length to * a buffer and then moves this buffer to userspace.
|
2011-02-26 21:07:13 +03:00
|
|
|
*/
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flashread(dev_t dev, struct uio * const uio, int flag)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
return physio(flashstrategy, NULL, dev, B_READ, minphys, uio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* flash_write - write to character device
|
|
|
|
* This function moves the data into a buffer from userspace to kernel space,
|
|
|
|
* then uses the registered driver's write function to write out the data to
|
|
|
|
* the media.
|
|
|
|
*/
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flashwrite(dev_t dev, struct uio * const uio, int flag)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-15 23:19:55 +04:00
|
|
|
flashstrategy(struct buf * const bp)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
struct flash_softc *sc;
|
|
|
|
const struct flash_interface *flash_if;
|
|
|
|
const struct flash_partition *part;
|
|
|
|
int unit, device_blks;
|
|
|
|
|
|
|
|
unit = minor(bp->b_dev);
|
|
|
|
sc = device_lookup_private(&flash_cd, unit);
|
|
|
|
if (sc == NULL) {
|
|
|
|
bp->b_error = ENXIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
flash_if = sc->flash_if;
|
2011-06-28 22:14:11 +04:00
|
|
|
part = &sc->sc_partinfo;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
/* divider */
|
|
|
|
KASSERT(flash_if->writesize != 0);
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
aprint_debug_dev(sc->sc_dev, "flash_strategy()\n");
|
|
|
|
|
|
|
|
if (!(bp->b_flags & B_READ) && sc->sc_readonly) {
|
|
|
|
bp->b_error = EACCES;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if length is not negative */
|
|
|
|
if (bp->b_blkno < 0) {
|
|
|
|
bp->b_error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-12-26 07:53:11 +03:00
|
|
|
/* zero length i/o */
|
2011-02-26 21:07:13 +03:00
|
|
|
if (bp->b_bcount == 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
device_blks = sc->sc_partinfo.part_size / DEV_BSIZE;
|
2011-02-26 21:07:13 +03:00
|
|
|
KASSERT(part->part_offset % DEV_BSIZE == 0);
|
|
|
|
bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE);
|
|
|
|
|
|
|
|
if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
flash_if->submit(sc->sc_parent_dev, bp);
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
return;
|
|
|
|
done:
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle the ioctl for the device
|
|
|
|
*/
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flashioctl(dev_t dev, u_long command, void * const data, int flags, lwp_t *l)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
|
|
|
struct flash_erase_params *ep;
|
|
|
|
struct flash_info_params *ip;
|
|
|
|
struct flash_dump_params *dp;
|
|
|
|
struct flash_badblock_params *bbp;
|
|
|
|
struct flash_erase_instruction ei;
|
|
|
|
struct flash_softc *sc;
|
|
|
|
int unit, err;
|
|
|
|
size_t retlen;
|
2011-04-04 18:25:09 +04:00
|
|
|
flash_off_t offset;
|
|
|
|
bool bad;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
unit = minor(dev);
|
|
|
|
if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
switch (command) {
|
|
|
|
case FLASH_ERASE_BLOCK:
|
|
|
|
/**
|
|
|
|
* Set up an erase instruction then call the registered
|
|
|
|
* driver's erase operation.
|
|
|
|
*/
|
|
|
|
ep = data;
|
|
|
|
|
|
|
|
if (sc->sc_readonly) {
|
|
|
|
return EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
ei.ei_addr = ep->ep_addr;
|
|
|
|
ei.ei_len = ep->ep_len;
|
|
|
|
ei.ei_callback = NULL;
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
err = flash_erase(sc->sc_dev, &ei);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case FLASH_BLOCK_ISBAD:
|
|
|
|
/**
|
|
|
|
* Set up an erase instruction then call the registered
|
|
|
|
* driver's erase operation.
|
|
|
|
*/
|
|
|
|
bbp = data;
|
|
|
|
|
2011-04-04 18:25:09 +04:00
|
|
|
err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad);
|
|
|
|
if (err) {
|
2011-02-26 21:07:13 +03:00
|
|
|
return err;
|
|
|
|
}
|
2011-04-04 18:25:09 +04:00
|
|
|
bbp->bbp_isbad = bad;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
case FLASH_BLOCK_MARKBAD:
|
|
|
|
bbp = data;
|
|
|
|
|
|
|
|
err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr);
|
|
|
|
|
2011-06-28 11:00:17 +04:00
|
|
|
break;
|
2011-02-26 21:07:13 +03:00
|
|
|
case FLASH_DUMP:
|
|
|
|
dp = data;
|
|
|
|
offset = dp->dp_block * sc->flash_if->erasesize;
|
2011-06-28 22:14:11 +04:00
|
|
|
FLDPRINTF(("Reading from block: %jd len: %jd\n",
|
2011-02-26 21:07:13 +03:00
|
|
|
(intmax_t )dp->dp_block, (intmax_t )dp->dp_len));
|
|
|
|
err = flash_read(sc->sc_parent_dev, offset, dp->dp_len,
|
|
|
|
&retlen, dp->dp_buf);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (retlen != dp->dp_len) {
|
|
|
|
dp->dp_len = -1;
|
|
|
|
dp->dp_buf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case FLASH_GET_INFO:
|
|
|
|
ip = data;
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
ip->ip_page_size = sc->flash_if->page_size;
|
|
|
|
ip->ip_erase_size = sc->flash_if->erasesize;
|
|
|
|
ip->ip_flash_type = sc->flash_if->type;
|
2011-06-28 22:14:11 +04:00
|
|
|
ip->ip_flash_size = sc->sc_partinfo.part_size;
|
2011-02-26 21:07:13 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2011-03-30 18:34:26 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
int
|
|
|
|
flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
|
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
return EACCES;
|
2011-02-26 21:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
flash_shutdown(device_t self, int how)
|
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly)
|
|
|
|
flash_sync(self);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct flash_interface *
|
|
|
|
flash_get_interface(dev_t dev)
|
|
|
|
{
|
|
|
|
struct flash_softc *sc;
|
|
|
|
int unit;
|
|
|
|
|
|
|
|
unit = minor(dev);
|
|
|
|
if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return sc->flash_if;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct flash_softc *
|
|
|
|
flash_get_softc(dev_t dev)
|
|
|
|
{
|
|
|
|
struct flash_softc *sc;
|
|
|
|
int unit;
|
|
|
|
|
|
|
|
unit = minor(dev);
|
|
|
|
sc = device_lookup_private(&flash_cd, unit);
|
|
|
|
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_t
|
|
|
|
flash_get_device(dev_t dev)
|
|
|
|
{
|
|
|
|
struct flash_softc *sc;
|
|
|
|
int unit;
|
|
|
|
|
|
|
|
unit = minor(dev);
|
|
|
|
sc = device_lookup_private(&flash_cd, unit);
|
|
|
|
|
|
|
|
return sc->sc_dev;
|
|
|
|
}
|
|
|
|
|
2011-07-30 00:48:33 +04:00
|
|
|
flash_size_t
|
|
|
|
flash_get_size(dev_t dev)
|
|
|
|
{
|
|
|
|
const struct flash_softc *sc;
|
|
|
|
|
|
|
|
sc = flash_get_softc(dev);
|
|
|
|
|
|
|
|
return sc->sc_partinfo.part_size;
|
|
|
|
}
|
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flash_erase(device_t self, struct flash_erase_instruction * const ei)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-02-26 21:07:13 +03:00
|
|
|
KASSERT(ei != NULL);
|
|
|
|
struct flash_erase_instruction e = *ei;
|
|
|
|
|
|
|
|
if (sc->sc_readonly)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
/* adjust for flash partition */
|
2011-06-28 22:14:11 +04:00
|
|
|
e.ei_addr += sc->sc_partinfo.part_offset;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
/* bounds check for flash partition */
|
2011-06-28 22:14:11 +04:00
|
|
|
if (e.ei_addr + e.ei_len > sc->sc_partinfo.part_size +
|
|
|
|
sc->sc_partinfo.part_offset)
|
2011-02-26 21:07:13 +03:00
|
|
|
return EINVAL;
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
return sc->flash_if->erase(device_parent(self), &e);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flash_read(device_t self, flash_off_t offset, size_t len, size_t * const retlen,
|
|
|
|
uint8_t * const buf)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-02-26 21:07:13 +03:00
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
offset += sc->sc_partinfo.part_offset;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
if (offset + len > sc->sc_partinfo.part_size +
|
|
|
|
sc->sc_partinfo.part_offset)
|
2011-02-26 21:07:13 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return sc->flash_if->read(device_parent(self),
|
|
|
|
offset, len, retlen, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flash_write(device_t self, flash_off_t offset, size_t len,
|
|
|
|
size_t * const retlen, const uint8_t * const buf)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
if (sc->sc_readonly)
|
|
|
|
return EACCES;
|
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
offset += sc->sc_partinfo.part_offset;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
if (offset + len > sc->sc_partinfo.part_size +
|
|
|
|
sc->sc_partinfo.part_offset)
|
2011-02-26 21:07:13 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return sc->flash_if->write(device_parent(self),
|
|
|
|
offset, len, retlen, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-04-04 18:25:09 +04:00
|
|
|
flash_block_markbad(device_t self, flash_off_t offset)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
if (sc->sc_readonly)
|
|
|
|
return EACCES;
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
offset += sc->sc_partinfo.part_offset;
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
if (offset + sc->flash_if->erasesize >=
|
2011-06-28 22:14:11 +04:00
|
|
|
sc->sc_partinfo.part_size +
|
|
|
|
sc->sc_partinfo.part_offset)
|
2011-02-26 21:07:13 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return sc->flash_if->block_markbad(device_parent(self), offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-07-15 23:19:55 +04:00
|
|
|
flash_block_isbad(device_t self, flash_off_t offset, bool * const bad)
|
2011-02-26 21:07:13 +03:00
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-06-28 22:14:11 +04:00
|
|
|
offset += sc->sc_partinfo.part_offset;
|
2011-02-26 21:07:13 +03:00
|
|
|
|
|
|
|
if (offset + sc->flash_if->erasesize >
|
2011-06-28 22:14:11 +04:00
|
|
|
sc->sc_partinfo.part_size +
|
|
|
|
sc->sc_partinfo.part_offset)
|
2011-02-26 21:07:13 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
2011-04-04 18:25:09 +04:00
|
|
|
return sc->flash_if->block_isbad(device_parent(self), offset, bad);
|
2011-02-26 21:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
flash_sync(device_t self)
|
|
|
|
{
|
2011-07-15 23:19:55 +04:00
|
|
|
struct flash_softc * const sc = device_private(self);
|
2011-06-28 11:00:17 +04:00
|
|
|
|
2011-02-26 21:07:13 +03:00
|
|
|
if (sc->sc_readonly)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
/* noop now TODO: implement */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE(MODULE_CLASS_DRIVER, flash, NULL);
|
|
|
|
|
|
|
|
#ifdef _MODULE
|
|
|
|
#include "ioconf.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
flash_modcmd(modcmd_t cmd, void *opaque)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
#ifdef _MODULE
|
|
|
|
int bmaj = -1, cmaj = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
#ifdef _MODULE
|
|
|
|
error = devsw_attach("flash", &flash_bdevsw, &bmaj,
|
|
|
|
&flash_cdevsw, &cmaj);
|
|
|
|
if (error)
|
2022-03-31 22:30:15 +03:00
|
|
|
return error;
|
|
|
|
error = config_init_component(cfdriver_ioconf_flash,
|
|
|
|
cfattach_ioconf_flash, cfdata_ioconf_flash);
|
|
|
|
if (error)
|
|
|
|
devsw_detach(&flash_bdevsw, &flash_cdevsw);
|
2011-02-26 21:07:13 +03:00
|
|
|
#endif
|
|
|
|
return error;
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
#ifdef _MODULE
|
|
|
|
error = config_fini_component(cfdriver_ioconf_flash,
|
|
|
|
cfattach_ioconf_flash, cfdata_ioconf_flash);
|
2022-03-31 22:30:15 +03:00
|
|
|
devsw_detach(&flash_bdevsw, &flash_cdevsw);
|
2011-02-26 21:07:13 +03:00
|
|
|
#endif
|
|
|
|
return error;
|
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
}
|