xref: /freebsd/sys/dev/qcom_qup/qcom_spi_var.h (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1d27ba308SAdrian Chadd /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d27ba308SAdrian Chadd  *
4d27ba308SAdrian Chadd  * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.org>
5d27ba308SAdrian Chadd  *
6d27ba308SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
7d27ba308SAdrian Chadd  * modification, are permitted provided that the following conditions
8d27ba308SAdrian Chadd  * are met:
9d27ba308SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
10d27ba308SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
11d27ba308SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
12d27ba308SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
13d27ba308SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
14d27ba308SAdrian Chadd  *
15d27ba308SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d27ba308SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d27ba308SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d27ba308SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d27ba308SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d27ba308SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d27ba308SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d27ba308SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d27ba308SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d27ba308SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d27ba308SAdrian Chadd  * SUCH DAMAGE.
26d27ba308SAdrian Chadd  *
27d27ba308SAdrian Chadd  * $FreeBSD$
28d27ba308SAdrian Chadd  */
29d27ba308SAdrian Chadd 
30d27ba308SAdrian Chadd #ifndef	__QCOM_SPI_VAR_H__
31d27ba308SAdrian Chadd #define	__QCOM_SPI_VAR_H__
32d27ba308SAdrian Chadd 
33d27ba308SAdrian Chadd typedef enum {
34d27ba308SAdrian Chadd 	QCOM_SPI_HW_QPI_V1_1 = 1,
35d27ba308SAdrian Chadd 	QCOM_SPI_HW_QPI_V2_1 = 2,
36d27ba308SAdrian Chadd 	QCOM_SPI_HW_QPI_V2_2 = 3,
37d27ba308SAdrian Chadd } qcom_spi_hw_version_t;
38d27ba308SAdrian Chadd 
39d27ba308SAdrian Chadd #define	CS_MAX				4
40d27ba308SAdrian Chadd 
41d27ba308SAdrian Chadd struct qcom_spi_softc {
42d27ba308SAdrian Chadd 	device_t		sc_dev;
43d27ba308SAdrian Chadd 	device_t		spibus;
44d27ba308SAdrian Chadd 
45d27ba308SAdrian Chadd 	uint32_t		sc_debug;
46d27ba308SAdrian Chadd 
47d27ba308SAdrian Chadd 	struct resource		*sc_mem_res;
48d27ba308SAdrian Chadd 	struct resource		*sc_irq_res;
49d27ba308SAdrian Chadd 	void			*sc_irq_h;
50d27ba308SAdrian Chadd 
51d27ba308SAdrian Chadd 	struct mtx		sc_mtx;
52d27ba308SAdrian Chadd 	bool			sc_busy; /* an SPI transfer (cmd+data)
53d27ba308SAdrian Chadd 					  * is active */
54d27ba308SAdrian Chadd 
55d27ba308SAdrian Chadd 	qcom_spi_hw_version_t	hw_version;
56d27ba308SAdrian Chadd 
57d27ba308SAdrian Chadd 	clk_t			clk_core;	/* QUP/SPI core */
58d27ba308SAdrian Chadd 	clk_t			clk_iface;	/* SPI interface */
59d27ba308SAdrian Chadd 
60d27ba308SAdrian Chadd 	/* For GPIO chip selects .. */
61d27ba308SAdrian Chadd 	gpio_pin_t		cs_pins[CS_MAX];
62d27ba308SAdrian Chadd 
63d27ba308SAdrian Chadd 	struct {
64d27ba308SAdrian Chadd 		/*
65d27ba308SAdrian Chadd 		 * FIFO size / block size in bytes.
66d27ba308SAdrian Chadd 		 *
67d27ba308SAdrian Chadd 		 * The FIFO slots are DWORD sized, not byte sized.
68d27ba308SAdrian Chadd 		 * So if the transfer size is set to 8 bits per
69d27ba308SAdrian Chadd 		 * word (which is what we'll support initially)
70d27ba308SAdrian Chadd 		 * the effective available FIFO is
71d27ba308SAdrian Chadd 		 * fifo_size / sizeof(uint32_t).
72d27ba308SAdrian Chadd 		 */
73d27ba308SAdrian Chadd 		uint32_t input_block_size;
74d27ba308SAdrian Chadd 		uint32_t output_block_size;
75d27ba308SAdrian Chadd 		uint32_t input_fifo_size;
76d27ba308SAdrian Chadd 		uint32_t output_fifo_size;
77d27ba308SAdrian Chadd 
78d27ba308SAdrian Chadd 		uint32_t cs_select;
79d27ba308SAdrian Chadd 		uint32_t num_cs;
80d27ba308SAdrian Chadd 		uint32_t max_frequency;
81d27ba308SAdrian Chadd 	} config;
82d27ba308SAdrian Chadd 
83d27ba308SAdrian Chadd 	struct {
84d27ba308SAdrian Chadd 		uint32_t transfer_mode; /* QUP_IO_M_MODE_* */
85d27ba308SAdrian Chadd 		uint32_t transfer_word_size; /* how many bytes in a transfer word */
86d27ba308SAdrian Chadd 		uint32_t frequency;
87d27ba308SAdrian Chadd 		bool cs_high; /* true if CS is high for active */
88d27ba308SAdrian Chadd 	} state;
89d27ba308SAdrian Chadd 
90d27ba308SAdrian Chadd 	struct {
91d27ba308SAdrian Chadd 		bool tx_dma_done;
92d27ba308SAdrian Chadd 		bool rx_dma_done;
93d27ba308SAdrian Chadd 		bool done;
94d27ba308SAdrian Chadd 		bool do_tx;
95d27ba308SAdrian Chadd 		bool do_rx;
96d27ba308SAdrian Chadd 		bool error;
97d27ba308SAdrian Chadd 	} intr;
98d27ba308SAdrian Chadd 
99d27ba308SAdrian Chadd 	struct {
100d27ba308SAdrian Chadd 		bool active; /* a (sub) transfer is active */
101d27ba308SAdrian Chadd 		uint32_t num_words; /* number of word_size words to transfer */
102d27ba308SAdrian Chadd 		const char *tx_buf;
103d27ba308SAdrian Chadd 		int tx_len;
104d27ba308SAdrian Chadd 		int tx_offset;
105d27ba308SAdrian Chadd 		char *rx_buf;
106d27ba308SAdrian Chadd 		int rx_len;
107d27ba308SAdrian Chadd 		int rx_offset;
108d27ba308SAdrian Chadd 		bool done;
109d27ba308SAdrian Chadd 	} transfer;
110d27ba308SAdrian Chadd };
111d27ba308SAdrian Chadd 
112d27ba308SAdrian Chadd #define	QCOM_SPI_QUP_VERSION_V1(sc)	\
113d27ba308SAdrian Chadd 	    ((sc)->hw_version == QCOM_SPI_HW_QPI_V1_1)
114d27ba308SAdrian Chadd 
115d27ba308SAdrian Chadd #define	QCOM_SPI_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
116d27ba308SAdrian Chadd #define	QCOM_SPI_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
117d27ba308SAdrian Chadd #define	QCOM_SPI_ASSERT_LOCKED(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
118d27ba308SAdrian Chadd #define	QCOM_SPI_READ_4(sc, reg)	bus_read_4((sc)->sc_mem_res, (reg))
119d27ba308SAdrian Chadd #define	QCOM_SPI_WRITE_4(sc, reg, val)	bus_write_4((sc)->sc_mem_res, \
120d27ba308SAdrian Chadd 	    (reg), (val))
121d27ba308SAdrian Chadd 
122d27ba308SAdrian Chadd /* XXX TODO: the region size should be in the tag or softc */
123d27ba308SAdrian Chadd #define	QCOM_SPI_BARRIER_WRITE(sc)	bus_barrier((sc)->sc_mem_res,	\
124d27ba308SAdrian Chadd 	    0, 0x600, BUS_SPACE_BARRIER_WRITE)
125d27ba308SAdrian Chadd #define	QCOM_SPI_BARRIER_READ(sc)	bus_barrier((sc)->sc_mem_res,	\
126d27ba308SAdrian Chadd 	    0, 0x600, BUS_SPACE_BARRIER_READ)
127d27ba308SAdrian Chadd #define	QCOM_SPI_BARRIER_RW(sc)		bus_barrier((sc)->sc_mem_res,	\
128d27ba308SAdrian Chadd 	     0, 0x600, BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE)
129d27ba308SAdrian Chadd 
130d27ba308SAdrian Chadd extern	int qcom_spi_hw_read_controller_transfer_sizes(
131d27ba308SAdrian Chadd 	    struct qcom_spi_softc *sc);
132d27ba308SAdrian Chadd extern	int qcom_spi_hw_qup_set_state_locked(struct qcom_spi_softc *sc,
133d27ba308SAdrian Chadd 	    uint32_t state);
134d27ba308SAdrian Chadd extern	int qcom_spi_hw_qup_init_locked(struct qcom_spi_softc *sc);
135d27ba308SAdrian Chadd extern	int qcom_spi_hw_spi_init_locked(struct qcom_spi_softc *sc);
136d27ba308SAdrian Chadd extern	int qcom_spi_hw_spi_cs_force(struct qcom_spi_softc *sc, int cs,
137d27ba308SAdrian Chadd 	    bool enable);
138d27ba308SAdrian Chadd extern	int qcom_spi_hw_interrupt_handle(struct qcom_spi_softc *sc);
139d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_transfer_selection(struct qcom_spi_softc *sc,
140d27ba308SAdrian Chadd 	    uint32_t len);
141d27ba308SAdrian Chadd extern	int qcom_spi_hw_complete_transfer(struct qcom_spi_softc *sc);
142d27ba308SAdrian Chadd 
143d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_current_transfer(struct qcom_spi_softc *sc);
144d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_pio_transfer_cnt(struct qcom_spi_softc *sc);
145d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_block_transfer_cnt(struct qcom_spi_softc *sc);
146d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_io_modes(struct qcom_spi_softc *sc);
147d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_spi_io_clock_polarity(
148d27ba308SAdrian Chadd 	    struct qcom_spi_softc *sc, bool cpol);
149d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_spi_config(struct qcom_spi_softc *sc,
150d27ba308SAdrian Chadd 	    uint32_t clock_val, bool cpha);
151d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_qup_config(struct qcom_spi_softc *sc,
152d27ba308SAdrian Chadd 	    bool is_tx, bool is_rx);
153d27ba308SAdrian Chadd extern	int qcom_spi_hw_setup_operational_mask(struct qcom_spi_softc *sc);
154d27ba308SAdrian Chadd extern	int qcom_spi_hw_ack_write_pio_fifo(struct qcom_spi_softc *sc);
155d27ba308SAdrian Chadd extern	int qcom_spi_hw_ack_opmode(struct qcom_spi_softc *sc);
156d27ba308SAdrian Chadd extern	int qcom_spi_hw_write_pio_fifo(struct qcom_spi_softc *sc);
157d27ba308SAdrian Chadd extern	int qcom_spi_hw_write_pio_block(struct qcom_spi_softc *sc);
158d27ba308SAdrian Chadd extern	int qcom_spi_hw_read_pio_fifo(struct qcom_spi_softc *sc);
159d27ba308SAdrian Chadd extern	int qcom_spi_hw_read_pio_block(struct qcom_spi_softc *sc);
160d27ba308SAdrian Chadd extern	int qcom_spi_hw_do_full_reset(struct qcom_spi_softc *sc);
161d27ba308SAdrian Chadd 
162d27ba308SAdrian Chadd #endif	/* __QCOM_SPI_VAR_H__ */
163