PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches
Flash

NOR flash class. More...

Modules

 NOR flash parts
 Description of the SPI NOR parts boards are fitted with.
 
 Flash partitions
 Access to areas of the board's flash.
 
 POSIX
 Flash driver for a native build: the flash is a file mapped into memory.
 
 QEMU
 Flash driver for the QEMU external flash, read through its memory window.
 
 SPI NOR
 Flash driver for a JEDEC NOR part on a SPI memory controller.
 

Data Structures

struct  pbl_flash_geometry
 Flash geometry. More...
 
struct  pbl_flash_otp_info
 Organization of the user one-time programmable (OTP) area. More...
 
struct  pbl_flash_otp_cell
 A named range of the OTP area. More...
 
struct  pbl_flash_otp_layout
 Named ranges of the OTP area, as laid out by the board. More...
 
struct  pbl_flash_ops
 Flash driver operations. More...
 
struct  pbl_flash
 A flash. More...
 

Macros

#define PBL_FLASH_OTP_CELL(_name, _offset, _len)    {.name = (_name), .offset = (_offset), .len = (_len)}
 Initializer for a struct pbl_flash_otp_cell.
 
#define PBL_FLASH_STATE_DEFINE(sym)
 Define the class state of flash sym, for the PBL_FLASH_*_DEFINE() macro of a driver.
 
#define PBL_FLASH_INIT(sym, _name, _parent, _ops, _geometry, _otp, _otp_layout)
 Initializer for the struct pbl_flash of flash sym.
 

Typedefs

typedef void(* pbl_flash_erase_cb_t) (int status, void *arg)
 Completion callback of pbl_flash_erase_async().
 
typedef void(* pbl_flash_pm_fn_t) (void *arg)
 Function run while the flash is powered down, see pbl_flash_power_down().
 

Functions

static const struct pbl_flash_geometry * pbl_flash_get_geometry (const struct pbl_flash *flash)
 Get the geometry of a flash.
 
static bool pbl_flash_in_coredump (const struct pbl_flash *flash)
 Check whether the flash runs in coredump mode, for drivers.
 
void pbl_flash_set_map (const struct pbl_flash *flash, const void *ptr, uint32_t len)
 Declare where the CPU reads the flash directly, for drivers.
 
int pbl_flash_read (const struct pbl_flash *flash, uint32_t addr, void *buf, size_t len)
 Read data.
 
int pbl_flash_write (const struct pbl_flash *flash, uint32_t addr, const void *buf, size_t len)
 Program data into an erased range.
 
int pbl_flash_erase (const struct pbl_flash *flash, uint32_t addr, size_t len)
 Erase a range.
 
int pbl_flash_erase_async (const struct pbl_flash *flash, uint32_t addr, size_t len, pbl_flash_erase_cb_t cb, void *arg)
 Erase a range, returning once the erase has started.
 
bool pbl_flash_is_erased (const struct pbl_flash *flash, uint32_t addr, size_t len)
 Check whether a range is erased.
 
int pbl_flash_protect (const struct pbl_flash *flash, uint32_t addr, size_t len)
 Refuse writes and erases in a range, replacing the previous one.
 
int pbl_flash_unprotect (const struct pbl_flash *flash)
 Allow writes and erases everywhere.
 
void pbl_flash_stop (const struct pbl_flash *flash)
 Wait for the erase in progress, if any, and return the hardware to the state the boot code expects, before a reset.
 
int pbl_flash_coredump_init (const struct pbl_flash *flash)
 Re-initialize the flash for use from a fault handler.
 
void pbl_flash_power_down (const struct pbl_flash *flash, pbl_flash_pm_fn_t fn, void *arg)
 Power the flash down while a function runs, with interrupts disabled.
 
int pbl_flash_pm_suspend (const struct pbl_flash *flash)
 Power the flash down until pbl_flash_pm_resume(), with interrupts disabled.
 
void pbl_flash_pm_resume (const struct pbl_flash *flash)
 Power the flash back up after pbl_flash_pm_suspend().
 
uint32_t pbl_flash_crc32 (const struct pbl_flash *flash, uint32_t addr, size_t len)
 CRC-32 of a range.
 
uint32_t pbl_flash_crc32_legacy (const struct pbl_flash *flash, uint32_t addr, size_t len)
 Legacy checksum of a range, as used by firmware images and resources.
 

Variables

const struct pbl_flash *const FLASH
 The board's storage flash.
 

One-time programmable area

The user OTP area of drivers that select FLASH_OTP: n_regions regions of region_len bytes, region i starting at offset i * region_len.

OTP bits are programmed like the main array, from erased (1) to 0, and a locked region can no longer be programmed or erased. Locking is irreversible: only the recovery firmware builds it, and it is never done implicitly. The board names the ranges it stores data in as cells.

int pbl_flash_otp_get_info (const struct pbl_flash *flash, struct pbl_flash_otp_info *info)
 Get the OTP organization.
 
int pbl_flash_otp_read (const struct pbl_flash *flash, uint32_t offs, void *buf, size_t len)
 Read from the OTP area.
 
int pbl_flash_otp_write (const struct pbl_flash *flash, uint32_t offs, const void *buf, size_t len)
 Program the OTP area.
 
int pbl_flash_otp_is_locked (const struct pbl_flash *flash, uint8_t region, bool *locked)
 Check whether an OTP region is locked.
 
int pbl_flash_otp_lock (const struct pbl_flash *flash, uint8_t region)
 Lock an OTP region forever.
 
int pbl_flash_otp_erase (const struct pbl_flash *flash, uint8_t region)
 Erase an OTP region.
 
const struct pbl_flash_otp_cell * pbl_flash_otp_cell_get (const struct pbl_flash *flash, const char *name)
 Find an OTP cell by name.
 

Detailed Description

NOR flash class.

A flash is a struct pbl_flash device. Addresses are offsets from its first byte, wherever the SoC maps it. Writes only clear bits, so the target range must be erased first. Erases work on whole subsectors: the class erases a range with as few sector and subsector erases as it can, skipping the blank ones.

static void prv_erased(int status, void *arg) {
// Runs on the high priority system work queue: keep it short
}
ret = pbl_flash_erase(FLASH, addr, len);
if (ret != 0) {
return ret;
}
ret = pbl_flash_write(FLASH, addr, data, sizeof(data));
if (ret != 0) {
return ret;
}
ret = pbl_flash_erase_async(FLASH, other_addr, other_len, prv_erased, nullptr);
const struct pbl_flash *const FLASH
The board's storage flash.
int pbl_flash_erase(const struct pbl_flash *flash, uint32_t addr, size_t len)
Erase a range.
int pbl_flash_write(const struct pbl_flash *flash, uint32_t addr, const void *buf, size_t len)
Program data into an erased range.
int pbl_flash_erase_async(const struct pbl_flash *flash, uint32_t addr, size_t len, pbl_flash_erase_cb_t cb, void *arg)
Erase a range, returning once the erase has started.

Calls are serialized per device and must not be issued from ISRs. A read or write issued while an erase is in progress suspends the erase, if the driver can, and the class resumes it shortly after. Only one erase runs at a time: starting another one blocks until the first is done.

Calls return 0 or a negative errno: -EINVAL for a range outside the flash or an unaligned erase, -EACCES for a write or erase in the protected range and -ENODEV if the flash is not ready. A hardware failure while reading, writing or erasing is fatal.

After pbl_flash_coredump_init() the class runs without OS services: no locking, no timers, no sleeping, and erases are synchronous. That is not reversible.


Data Structure Documentation

◆ pbl_flash_geometry

struct pbl_flash_geometry

Flash geometry.

Data Fields
uint32_t page_size Program page size, in bytes.
uint16_t sector_erase_ms Typical sector erase time, to pace erase polling.
uint32_t sector_size Large erase unit, in bytes.
uint32_t size Size, in bytes.
uint16_t subsector_erase_ms Typical subsector erase time, to pace erase polling.
uint32_t subsector_size Small erase unit, in bytes.

◆ pbl_flash_otp_info

struct pbl_flash_otp_info

Organization of the user one-time programmable (OTP) area.

Data Fields
uint8_t n_regions Number of regions, 0 without OTP.
uint32_t region_len Size of each region, in bytes.

◆ pbl_flash_otp_cell

struct pbl_flash_otp_cell

A named range of the OTP area.

Data Fields
uint32_t len Size, in bytes.
const char * name Name.
uint32_t offset Offset in the OTP area.

◆ pbl_flash_otp_layout

struct pbl_flash_otp_layout

Named ranges of the OTP area, as laid out by the board.

Data Fields
const struct pbl_flash_otp_cell * cells Cells.
uint8_t num_cells Number of cells.

◆ pbl_flash

struct pbl_flash

A flash.

Data Fields
struct pbl_device dev Device.
const struct pbl_flash_geometry * geometry Geometry.
const struct pbl_flash_ops * ops Driver operations.
const struct pbl_flash_otp_info * otp OTP organization, or NULL.
const struct pbl_flash_otp_layout * otp_layout OTP cells, or NULL.
struct pbl_flash_state * state Runtime state.

Macro Definition Documentation

◆ PBL_FLASH_INIT

#define PBL_FLASH_INIT (   sym,
  _name,
  _parent,
  _ops,
  _geometry,
  _otp,
  _otp_layout 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, pbl_flash_dev_init, _parent, &pbl_flash_class), \
.ops = (_ops), \
.state = &sym##_flash_state, \
.geometry = (_geometry), \
.otp = (_otp), \
.otp_layout = (_otp_layout), \
}
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153

Initializer for the struct pbl_flash of flash sym.

Parameters
symSymbol of the flash instance, with its state defined by PBL_FLASH_STATE_DEFINE().
_nameName.
_parentParent device, e.g. the memory controller, or NULL.
_opsDriver operations.
_geometryGeometry.
_otpOTP organization, or NULL.
_otp_layoutOTP cells, or NULL.

◆ PBL_FLASH_OTP_CELL

#define PBL_FLASH_OTP_CELL (   _name,
  _offset,
  _len 
)     {.name = (_name), .offset = (_offset), .len = (_len)}

Initializer for a struct pbl_flash_otp_cell.

◆ PBL_FLASH_STATE_DEFINE

#define PBL_FLASH_STATE_DEFINE (   sym)
Value:
static struct pbl_flash_state sym##_flash_state
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132

Define the class state of flash sym, for the PBL_FLASH_*_DEFINE() macro of a driver.

Parameters
symSymbol of the flash instance.

Typedef Documentation

◆ pbl_flash_erase_cb_t

typedef void(* pbl_flash_erase_cb_t) (int status, void *arg)

Completion callback of pbl_flash_erase_async().

Runs on pbl_sys_workq_high, or from the caller if there is nothing to erase, with no lock held: it may start another erase.

Parameters
status0 or a negative errno.
argCallback argument.

◆ pbl_flash_pm_fn_t

typedef void(* pbl_flash_pm_fn_t) (void *arg)

Function run while the flash is powered down, see pbl_flash_power_down().

Function Documentation

◆ pbl_flash_coredump_init()

int pbl_flash_coredump_init ( const struct pbl_flash *  flash)

Re-initialize the flash for use from a fault handler.

Parameters
flashFlash.
Returns
0 or a negative errno.

◆ pbl_flash_crc32()

uint32_t pbl_flash_crc32 ( const struct pbl_flash *  flash,
uint32_t  addr,
size_t  len 
)

CRC-32 of a range.

Parameters
flashFlash.
addrAddress.
lenNumber of bytes.
Returns
CRC-32.

◆ pbl_flash_crc32_legacy()

uint32_t pbl_flash_crc32_legacy ( const struct pbl_flash *  flash,
uint32_t  addr,
size_t  len 
)

Legacy checksum of a range, as used by firmware images and resources.

Parameters
flashFlash.
addrAddress.
lenNumber of bytes.
Returns
Checksum.

◆ pbl_flash_erase()

int pbl_flash_erase ( const struct pbl_flash *  flash,
uint32_t  addr,
size_t  len 
)

Erase a range.

Feeds the caller's task watchdog while it waits, unless a single erase unit takes unreasonably long.

Parameters
flashFlash.
addrAddress, aligned to a subsector.
lenNumber of bytes, rounded up to a subsector.
Returns
0 or a negative errno.

Referenced by pbl_flash_partition_erase().

◆ pbl_flash_erase_async()

int pbl_flash_erase_async ( const struct pbl_flash *  flash,
uint32_t  addr,
size_t  len,
pbl_flash_erase_cb_t  cb,
void *  arg 
)

Erase a range, returning once the erase has started.

Parameters
flashFlash.
addrAddress, aligned to a subsector.
lenNumber of bytes, rounded up to a subsector.
cbCompletion callback.
argCallback argument.
Returns
0 if cb will be called, or a negative errno.

◆ pbl_flash_get_geometry()

static const struct pbl_flash_geometry * pbl_flash_get_geometry ( const struct pbl_flash *  flash)
inlinestatic

Get the geometry of a flash.

Parameters
flashFlash.
Returns
Its geometry.

References pbl_flash::geometry.

◆ pbl_flash_in_coredump()

static bool pbl_flash_in_coredump ( const struct pbl_flash *  flash)
inlinestatic

Check whether the flash runs in coredump mode, for drivers.

Parameters
flashFlash.
Returns
True after pbl_flash_coredump_init().

References pbl_flash::state.

◆ pbl_flash_is_erased()

bool pbl_flash_is_erased ( const struct pbl_flash *  flash,
uint32_t  addr,
size_t  len 
)

Check whether a range is erased.

Parameters
flashFlash.
addrAddress.
lenNumber of bytes.
Returns
True if the flash is ready and every byte reads as erased.

Referenced by pbl_flash_partition_is_erased().

◆ pbl_flash_otp_cell_get()

const struct pbl_flash_otp_cell * pbl_flash_otp_cell_get ( const struct pbl_flash *  flash,
const char *  name 
)

Find an OTP cell by name.

Parameters
flashFlash.
nameName.
Returns
Cell, or NULL if the board has no such cell.

◆ pbl_flash_otp_erase()

int pbl_flash_otp_erase ( const struct pbl_flash *  flash,
uint8_t  region 
)

Erase an OTP region.

Parameters
flashFlash.
regionRegion.
Returns
0, -ENOTSUP if the flash cannot erase it, -EROFS if the region is locked, or another negative errno.

◆ pbl_flash_otp_get_info()

int pbl_flash_otp_get_info ( const struct pbl_flash *  flash,
struct pbl_flash_otp_info *  info 
)

Get the OTP organization.

Parameters
flashFlash.
[out]infoOrganization.
Returns
0, or -ENOTSUP without OTP.

◆ pbl_flash_otp_is_locked()

int pbl_flash_otp_is_locked ( const struct pbl_flash *  flash,
uint8_t  region,
bool *  locked 
)

Check whether an OTP region is locked.

Parameters
flashFlash.
regionRegion.
[out]lockedLock state.
Returns
0 or a negative errno.

◆ pbl_flash_otp_lock()

int pbl_flash_otp_lock ( const struct pbl_flash *  flash,
uint8_t  region 
)

Lock an OTP region forever.

Parameters
flashFlash.
regionRegion.
Returns
0, -ENOTSUP if the flash cannot lock it, or another negative errno.

◆ pbl_flash_otp_read()

int pbl_flash_otp_read ( const struct pbl_flash *  flash,
uint32_t  offs,
void *  buf,
size_t  len 
)

Read from the OTP area.

Parameters
flashFlash.
offsOffset in the OTP area.
[out]bufBuffer.
lenNumber of bytes.
Returns
0 or a negative errno.

◆ pbl_flash_otp_write()

int pbl_flash_otp_write ( const struct pbl_flash *  flash,
uint32_t  offs,
const void *  buf,
size_t  len 
)

Program the OTP area.

Parameters
flashFlash.
offsOffset in the OTP area.
bufData.
lenNumber of bytes.
Returns
0, -EROFS if a region in the range is locked, or another negative errno.

◆ pbl_flash_pm_resume()

void pbl_flash_pm_resume ( const struct pbl_flash *  flash)

Power the flash back up after pbl_flash_pm_suspend().

Parameters
flashFlash.

◆ pbl_flash_pm_suspend()

int pbl_flash_pm_suspend ( const struct pbl_flash *  flash)

Power the flash down until pbl_flash_pm_resume(), with interrupts disabled.

For a flash the CPU does not execute from, see pbl_flash_power_down() otherwise.

Parameters
flashFlash.
Return values
0Powered down.
-EBUSYAn erase or a transfer is in progress.
-ENOTSUPThe driver cannot.

◆ pbl_flash_power_down()

void pbl_flash_power_down ( const struct pbl_flash *  flash,
pbl_flash_pm_fn_t  fn,
void *  arg 
)

Power the flash down while a function runs, with interrupts disabled.

The flash stays powered while an erase is in progress, but fn always runs. When the CPU executes from the flash, fn must run from RAM.

Parameters
flashFlash.
fnFunction to run.
argArgument of fn.

◆ pbl_flash_protect()

int pbl_flash_protect ( const struct pbl_flash *  flash,
uint32_t  addr,
size_t  len 
)

Refuse writes and erases in a range, replacing the previous one.

Parameters
flashFlash.
addrAddress.
lenNumber of bytes.
Returns
0 or a negative errno.

Referenced by pbl_flash_partition_protect().

◆ pbl_flash_read()

int pbl_flash_read ( const struct pbl_flash *  flash,
uint32_t  addr,
void *  buf,
size_t  len 
)

Read data.

Parameters
flashFlash.
addrAddress.
[out]bufBuffer.
lenNumber of bytes.
Returns
0 or a negative errno.

Referenced by pbl_flash_partition_read().

◆ pbl_flash_set_map()

void pbl_flash_set_map ( const struct pbl_flash *  flash,
const void *  ptr,
uint32_t  len 
)

Declare where the CPU reads the flash directly, for drivers.

Reads inside the window then copy from memory instead of going through the driver. The driver keeps the window coherent after it writes or erases.

Parameters
flashFlash.
ptrCPU address of the first byte, or NULL to remove the window.
lenSize of the window in bytes.

◆ pbl_flash_stop()

void pbl_flash_stop ( const struct pbl_flash *  flash)

Wait for the erase in progress, if any, and return the hardware to the state the boot code expects, before a reset.

The flash then refuses every call with -ENODEV, until pbl_flash_coredump_init().

Parameters
flashFlash.

◆ pbl_flash_unprotect()

int pbl_flash_unprotect ( const struct pbl_flash *  flash)

Allow writes and erases everywhere.

Parameters
flashFlash.
Returns
0 or a negative errno.

◆ pbl_flash_write()

int pbl_flash_write ( const struct pbl_flash *  flash,
uint32_t  addr,
const void *  buf,
size_t  len 
)

Program data into an erased range.

Parameters
flashFlash.
addrAddress.
bufData.
lenNumber of bytes.
Returns
0 or a negative errno.

Referenced by pbl_flash_partition_write().

Variable Documentation

◆ FLASH