Home | History | Annotate | Line # | Download | only in spi
      1 /* $NetBSD: spivar.h,v 1.26 2025/10/10 18:36:17 brad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5  * Copyright (c) 2006 Garrett D'Amore.
      6  * All rights reserved.
      7  *
      8  * Portions of this code were written by Garrett D'Amore for the
      9  * Champaign-Urbana Community Wireless Network Project.
     10  *
     11  * Redistribution and use in source and binary forms, with or
     12  * without modification, are permitted provided that the following
     13  * conditions are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above
     17  *    copyright notice, this list of conditions and the following
     18  *    disclaimer in the documentation and/or other materials provided
     19  *    with the distribution.
     20  * 3. All advertising materials mentioning features or use of this
     21  *    software must display the following acknowledgements:
     22  *      This product includes software developed by the Urbana-Champaign
     23  *      Independent Media Center.
     24  *	This product includes software developed by Garrett D'Amore.
     25  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26  *    D'Amore's name may not be used to endorse or promote products
     27  *    derived from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 #ifndef	_DEV_SPI_SPIVAR_H_
     45 #define	_DEV_SPI_SPIVAR_H_
     46 
     47 #include <sys/device.h>
     48 #include <sys/queue.h>
     49 #include <sys/uio.h> /* for iovec */
     50 
     51 /*
     52  * Serial Peripheral Interface bus.  This is a 4-wire bus common for
     53  * connecting flash, clocks, sensors, and various other low-speed
     54  * peripherals.
     55  */
     56 
     57 struct spi_handle;
     58 struct spi_transfer;
     59 
     60 typedef struct spi_handle *spi_handle_t;
     61 
     62 #define	SPI_MODE_CPHA	__BIT(0)
     63 #define	SPI_MODE_CPOL	__BIT(1)
     64 
     65 /*
     66  * De facto standard latching modes.
     67  */
     68 #define	SPI_MODE_0	0
     69 #define	SPI_MODE_1	SPI_MODE_CPHA
     70 #define	SPI_MODE_2	SPI_MODE_CPOL
     71 #define	SPI_MODE_3	(SPI_MODE_CPHA | SPI_MODE_CPOL)
     72 
     73 /* Philips' Microwire is just Mode 0 */
     74 #define	SPI_MODE_MICROWIRE	SPI_MODE_0
     75 
     76 /* SPI transfer speed helper macros -- converts to Hz for spi_configure(). */
     77 #define	SPI_FREQ_kHz(x)	((x) * 1000)
     78 #define	SPI_FREQ_MHz(x)	((x) * 1000000)
     79 
     80 /* Additional transfer mode flags. */
     81 #define	SPI_F_3WIRE	__BIT(0)	/* requires 3-wire mode */
     82 #define	SPI_F_CS_HIGH	__BIT(1)	/* chip select is active-high */
     83 #define	SPI_F_LSB	__BIT(2)	/* transfer LSB first */
     84 
     85 struct spi_controller {
     86 	void	*sct_cookie;	/* controller private data */
     87 	int	sct_nslaves;
     88 	int	(*sct_configure)(void *, int, int, int);
     89 	int	(*sct_transfer)(void *, struct spi_transfer *);
     90 };
     91 
     92 int	spibus_print(void *, const char *);
     93 
     94 struct spibus_attach_args {
     95 	const struct spi_controller *sba_controller;
     96 };
     97 
     98 struct spi_attach_args {
     99 	spi_handle_t	sa_handle;
    100 
    101 	/* only set if using direct config */
    102 	const char	*sa_name;	/* name of the device */
    103 	const char	*sa_clist;	/* compatible strlist */
    104 	size_t		sa_clist_size;	/* size of compatible strlist */
    105 	devhandle_t	sa_devhandle;	/* device handle for the device */
    106 };
    107 
    108 /*
    109  * This is similar in some respects to struct buf, but we cannot use
    110  * that structure because it was not designed to support full-duplex
    111  * IO.
    112  */
    113 struct spi_chunk {
    114 	struct spi_chunk *chunk_next;
    115 	int		chunk_count;
    116 	uint8_t		*chunk_read;
    117 	const uint8_t	*chunk_write;
    118 	/* for private use by framework and bus driver */
    119 	uint8_t		*chunk_rptr;
    120 	const uint8_t	*chunk_wptr;
    121 	int		chunk_rresid;
    122 	int		chunk_wresid;
    123 };
    124 
    125 struct spi_transfer {
    126 	struct spi_chunk *st_chunks;		/* chained bufs */
    127 	SIMPLEQ_ENTRY(spi_transfer) st_chain;	/* chain of submitted jobs */
    128 	volatile int	st_flags;
    129 	int		st_errno;
    130 	int		st_slave;
    131 	void		*st_private;
    132 	void		(*st_done)(struct spi_transfer *);
    133 	kmutex_t	st_lock;
    134 	kcondvar_t	st_cv;
    135 	void		*st_busprivate;
    136 	void		*st_spiprivate;
    137 };
    138 
    139 /* declare a list of transfers */
    140 SIMPLEQ_HEAD(spi_transq, spi_transfer);
    141 
    142 #define	spi_transq_init(q)	\
    143 	SIMPLEQ_INIT(q)
    144 
    145 #define	spi_transq_enqueue(q, trans)	\
    146 	SIMPLEQ_INSERT_TAIL(q, trans, st_chain)
    147 
    148 #define	spi_transq_dequeue(q)		\
    149 	SIMPLEQ_REMOVE_HEAD(q, st_chain)
    150 
    151 #define	spi_transq_first(q)		\
    152 	SIMPLEQ_FIRST(q)
    153 
    154 #define	SPI_F_DONE		0x0001
    155 #define	SPI_F_ERROR		0x0002
    156 
    157 static inline device_t
    158 spibus_attach(device_t dev, const struct spi_controller *sct)
    159 {
    160 	struct spibus_attach_args sba = {
    161 		.sba_controller = sct,
    162 	};
    163 	return config_found(dev, &sba, spibus_print,
    164 	    CFARGS(.iattr = "spibus",
    165 		   .devhandle = device_handle(dev)));
    166 }
    167 
    168 /*
    169  * Constants to indicate the quality of a match made by a driver's
    170  * match routine, from lowest to highest:
    171  *
    172  *	-- Default indirect match; rely on kernel config file.
    173  *
    174  *	-- Direct-config match by "compatible" string.
    175  */
    176 #define	SPI_MATCH_DEFAULT		1
    177 #define	SPI_MATCH_DIRECT_COMPATIBLE	10	/* ...and up */
    178 
    179 bool	spi_use_direct_match(const struct spi_attach_args *,
    180 	    const struct device_compatible_entry *, int *);
    181 int	spi_compatible_match(const struct spi_attach_args *,
    182 	    const struct device_compatible_entry *);
    183 const struct device_compatible_entry *
    184 	spi_compatible_lookup(const struct spi_attach_args *,
    185 	    const struct device_compatible_entry *);
    186 
    187 int	spi_configure(device_t, spi_handle_t, int, int);
    188 int	spi_transfer(spi_handle_t, struct spi_transfer *);
    189 void	spi_transfer_init(struct spi_transfer *);
    190 void	spi_chunk_init(struct spi_chunk *, int, const uint8_t *, uint8_t *);
    191 void	spi_transfer_add(struct spi_transfer *, struct spi_chunk *);
    192 void	spi_wait(struct spi_transfer *);
    193 void	spi_done(struct spi_transfer *, int);
    194 
    195 /* convenience wrappers */
    196 struct spi_chunk_q {
    197 	struct spi_chunk chunk;
    198 	SIMPLEQ_ENTRY(spi_chunk_q) chunk_q;
    199 };
    200 
    201 int	spi_send(spi_handle_t, int, const uint8_t *);
    202 int	spi_recv(spi_handle_t, int, uint8_t *);
    203 int	spi_send_recv(spi_handle_t, int, const uint8_t *, int, uint8_t *);
    204 int	spi_sendv(spi_handle_t, const struct iovec *, int);
    205 
    206 #endif	/* _DEV_SPI_SPIVAR_H_ */
    207