xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_io.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
177cb4d3eSLandon J. Fuller /*-
277cb4d3eSLandon J. Fuller  * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
377cb4d3eSLandon J. Fuller  * All rights reserved.
477cb4d3eSLandon J. Fuller  *
577cb4d3eSLandon J. Fuller  * Redistribution and use in source and binary forms, with or without
677cb4d3eSLandon J. Fuller  * modification, are permitted provided that the following conditions
777cb4d3eSLandon J. Fuller  * are met:
877cb4d3eSLandon J. Fuller  * 1. Redistributions of source code must retain the above copyright
977cb4d3eSLandon J. Fuller  *    notice, this list of conditions and the following disclaimer,
1077cb4d3eSLandon J. Fuller  *    without modification.
1177cb4d3eSLandon J. Fuller  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1277cb4d3eSLandon J. Fuller  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1377cb4d3eSLandon J. Fuller  *    redistribution must be conditioned upon including a substantially
1477cb4d3eSLandon J. Fuller  *    similar Disclaimer requirement for further binary redistribution.
1577cb4d3eSLandon J. Fuller  *
1677cb4d3eSLandon J. Fuller  * NO WARRANTY
1777cb4d3eSLandon J. Fuller  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1877cb4d3eSLandon J. Fuller  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1977cb4d3eSLandon J. Fuller  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2077cb4d3eSLandon J. Fuller  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2177cb4d3eSLandon J. Fuller  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
2277cb4d3eSLandon J. Fuller  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2377cb4d3eSLandon J. Fuller  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2477cb4d3eSLandon J. Fuller  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2577cb4d3eSLandon J. Fuller  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2677cb4d3eSLandon J. Fuller  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2777cb4d3eSLandon J. Fuller  * THE POSSIBILITY OF SUCH DAMAGES.
2877cb4d3eSLandon J. Fuller  *
2977cb4d3eSLandon J. Fuller  */
3077cb4d3eSLandon J. Fuller 
3177cb4d3eSLandon J. Fuller #ifndef _BHND_NVRAM_BHND_NVRAM_IO_H_
3277cb4d3eSLandon J. Fuller #define _BHND_NVRAM_BHND_NVRAM_IO_H_
3377cb4d3eSLandon J. Fuller 
3477cb4d3eSLandon J. Fuller #ifdef _KERNEL
3577cb4d3eSLandon J. Fuller #include <sys/param.h>
3677cb4d3eSLandon J. Fuller 
3777cb4d3eSLandon J. Fuller #include <dev/bhnd/bhnd.h>
3877cb4d3eSLandon J. Fuller #else /* !_KERNEL */
3977cb4d3eSLandon J. Fuller #include <errno.h>
4077cb4d3eSLandon J. Fuller 
4177cb4d3eSLandon J. Fuller #include <stdbool.h>
4277cb4d3eSLandon J. Fuller #include <stdint.h>
4377cb4d3eSLandon J. Fuller #include <stdlib.h>
4477cb4d3eSLandon J. Fuller #endif /* _KERNEL */
4577cb4d3eSLandon J. Fuller 
4677cb4d3eSLandon J. Fuller struct bhnd_nvram_io;
4777cb4d3eSLandon J. Fuller 
4877cb4d3eSLandon J. Fuller struct bhnd_nvram_io	*bhnd_nvram_iobuf_new(const void *buffer, size_t size);
4977cb4d3eSLandon J. Fuller struct bhnd_nvram_io	*bhnd_nvram_iobuf_empty(size_t size, size_t capacity);
5077cb4d3eSLandon J. Fuller struct bhnd_nvram_io	*bhnd_nvram_iobuf_copy(struct bhnd_nvram_io *src);
5177cb4d3eSLandon J. Fuller struct bhnd_nvram_io	*bhnd_nvram_iobuf_copy_range(struct bhnd_nvram_io *src,
5277cb4d3eSLandon J. Fuller 			     size_t offset, size_t size);
5377cb4d3eSLandon J. Fuller 
54*f76db8deSLandon J. Fuller struct bhnd_nvram_io	*bhnd_nvram_ioptr_new(const void *ptr, size_t size,
55*f76db8deSLandon J. Fuller 			     size_t capacity, uint32_t flags);
56*f76db8deSLandon J. Fuller 
5777cb4d3eSLandon J. Fuller #ifdef _KERNEL
5877cb4d3eSLandon J. Fuller struct bhnd_nvram_io	*bhnd_nvram_iores_new(struct bhnd_resource *r,
5977cb4d3eSLandon J. Fuller 			     bus_size_t offset, bus_size_t size,
6077cb4d3eSLandon J. Fuller 			     u_int bus_width);
6177cb4d3eSLandon J. Fuller #endif /* _KERNEL */
6277cb4d3eSLandon J. Fuller 
6377cb4d3eSLandon J. Fuller size_t			 bhnd_nvram_io_getsize(struct bhnd_nvram_io *io);
6477cb4d3eSLandon J. Fuller int			 bhnd_nvram_io_setsize(struct bhnd_nvram_io *io,
6577cb4d3eSLandon J. Fuller 			     size_t size);
6677cb4d3eSLandon J. Fuller 
6777cb4d3eSLandon J. Fuller int			 bhnd_nvram_io_read(struct bhnd_nvram_io *io,
6877cb4d3eSLandon J. Fuller 			     size_t offset, void *buffer, size_t nbytes);
6977cb4d3eSLandon J. Fuller int			 bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io,
7077cb4d3eSLandon J. Fuller 			     size_t offset, const void **ptr, size_t nbytes,
7177cb4d3eSLandon J. Fuller 			     size_t *navail);
7277cb4d3eSLandon J. Fuller 
7377cb4d3eSLandon J. Fuller int			 bhnd_nvram_io_write(struct bhnd_nvram_io *io,
7477cb4d3eSLandon J. Fuller 			     size_t offset, void *buffer, size_t nbytes);
7577cb4d3eSLandon J. Fuller int			 bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io,
7677cb4d3eSLandon J. Fuller 			     size_t offset, void **ptr, size_t nbytes,
7777cb4d3eSLandon J. Fuller 			     size_t *navail);
7877cb4d3eSLandon J. Fuller 
7977cb4d3eSLandon J. Fuller void			 bhnd_nvram_io_free(struct bhnd_nvram_io *io);
8077cb4d3eSLandon J. Fuller 
81*f76db8deSLandon J. Fuller /**
82*f76db8deSLandon J. Fuller  * bhnd_nvram_ioptr flags
83*f76db8deSLandon J. Fuller  */
84*f76db8deSLandon J. Fuller enum {
85*f76db8deSLandon J. Fuller 	BHND_NVRAM_IOPTR_RDONLY	= (1<<0),	/**< read-only */
86*f76db8deSLandon J. Fuller 	BHND_NVRAM_IOPTR_RDWR	= (1<<1),	/**< read/write */
87*f76db8deSLandon J. Fuller };
88*f76db8deSLandon J. Fuller 
8977cb4d3eSLandon J. Fuller #endif /* _BHND_NVRAM_BHND_NVRAM_IO_H_ */
90