1 /* This file is part of the coreboot project. */
2 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <spi-generic.h>
6 #include <arch/cache.h>
7 #include <device/mmio.h>
8 #include <soc/addressmap.h>
11 #include <soc/clock.h>
17 #define CACHE_LINE_SIZE 64
19 static int curr_desc_idx
= -1;
22 uint32_t data_address
;
23 uint32_t next_descriptor
;
25 uint32_t multi_io_mode
:3;
30 //------------------------//
33 uint32_t bounce_length
;
55 enum bus_xfer_direction
{
61 struct cmd_desc descriptors
[3];
62 uint8_t buffers
[3][CACHE_LINE_SIZE
];
63 } *dma
= (void *)_dma_coherent
;
65 static void dma_transfer_chain(struct cmd_desc
*chain
)
67 uint32_t mstr_int_status
;
69 write32(&sdm845_qspi
->mstr_int_sts
, 0xFFFFFFFF);
70 write32(&sdm845_qspi
->next_dma_desc_addr
, (uint32_t)(uintptr_t) chain
);
73 mstr_int_status
= read32(&sdm845_qspi
->mstr_int_sts
);
74 if (mstr_int_status
& DMA_CHAIN_DONE
)
79 static void flush_chain(void)
81 struct cmd_desc
*desc
= &dma
->descriptors
[0];
85 dma_transfer_chain(desc
);
88 if (desc
->direction
== MASTER_READ
) {
89 if (desc
->bounce_length
== 0)
90 dcache_invalidate_by_mva(
91 (void *)(uintptr_t) desc
->data_address
,
94 src
= (void *)(uintptr_t) desc
->bounce_src
;
95 dst
= (void *)(uintptr_t) desc
->bounce_dst
;
96 memcpy(dst
, src
, desc
->bounce_length
);
99 desc
= (void *)(uintptr_t) desc
->next_descriptor
;
104 static struct cmd_desc
*allocate_descriptor(void)
106 struct cmd_desc
*current
;
107 struct cmd_desc
*next
;
110 current
= (curr_desc_idx
== -1) ?
111 NULL
: &dma
->descriptors
[curr_desc_idx
];
113 index
= ++curr_desc_idx
;
114 next
= &dma
->descriptors
[index
];
116 next
->data_address
= (uint32_t) (uintptr_t) dma
->buffers
[index
];
118 next
->next_descriptor
= 0;
119 next
->direction
= MASTER_READ
;
120 next
->multi_io_mode
= 0;
125 next
->bounce_src
= 0;
126 next
->bounce_dst
= 0;
127 next
->bounce_length
= 0;
130 current
->next_descriptor
= (uint32_t)(uintptr_t) next
;
131 current
->fragment
= 1;
137 static void cs_change(enum cs_state state
)
139 gpio_set(GPIO(90), state
== CS_DEASSERT
);
142 static void configure_gpios(void)
144 gpio_output(GPIO(90), 1);
146 gpio_configure(GPIO(91), GPIO91_FUNC_QSPI_DATA
,
147 GPIO_NO_PULL
, GPIO_2MA
, GPIO_ENABLE
);
149 gpio_configure(GPIO(92), GPIO92_FUNC_QSPI_DATA
,
150 GPIO_NO_PULL
, GPIO_2MA
, GPIO_ENABLE
);
152 gpio_configure(GPIO(95), GPIO95_FUNC_QSPI_CLK
,
153 GPIO_NO_PULL
, GPIO_2MA
, GPIO_ENABLE
);
156 static void queue_bounce_data(uint8_t *data
, uint32_t data_bytes
,
157 enum qspi_mode data_mode
, bool write
)
159 struct cmd_desc
*desc
;
162 desc
= allocate_descriptor();
163 desc
->direction
= write
;
164 desc
->multi_io_mode
= data_mode
;
165 ptr
= (void *)(uintptr_t) desc
->data_address
;
168 memcpy(ptr
, data
, data_bytes
);
170 desc
->bounce_src
= (uint32_t)(uintptr_t) ptr
;
171 desc
->bounce_dst
= (uint32_t)(uintptr_t) data
;
172 desc
->bounce_length
= data_bytes
;
175 desc
->length
= data_bytes
;
178 static void queue_direct_data(uint8_t *data
, uint32_t data_bytes
,
179 enum qspi_mode data_mode
, bool write
)
181 struct cmd_desc
*desc
;
183 desc
= allocate_descriptor();
184 desc
->direction
= write
;
185 desc
->multi_io_mode
= data_mode
;
186 desc
->data_address
= (uint32_t)(uintptr_t) data
;
187 desc
->length
= data_bytes
;
190 dcache_clean_by_mva(data
, data_bytes
);
192 dcache_invalidate_by_mva(data
, data_bytes
);
195 static void queue_data(uint8_t *data
, uint32_t data_bytes
,
196 enum qspi_mode data_mode
, bool write
)
198 uint8_t *aligned_ptr
;
200 uint32_t prolog_bytes
, aligned_bytes
, epilog_bytes
;
206 (uint8_t *)ALIGN_UP((uintptr_t)data
, CACHE_LINE_SIZE
);
208 prolog_bytes
= MIN(data_bytes
, aligned_ptr
- data
);
209 aligned_bytes
= ALIGN_DOWN(data_bytes
- prolog_bytes
, CACHE_LINE_SIZE
);
210 epilog_bytes
= data_bytes
- prolog_bytes
- aligned_bytes
;
212 epilog_ptr
= data
+ prolog_bytes
+ aligned_bytes
;
215 queue_bounce_data(data
, prolog_bytes
, data_mode
, write
);
217 queue_direct_data(aligned_ptr
, aligned_bytes
, data_mode
, write
);
219 queue_bounce_data(epilog_ptr
, epilog_bytes
, data_mode
, write
);
222 static void reg_init(void)
225 uint32_t tx_data_oe_delay
, tx_data_delay
;
226 uint32_t mstr_config
;
230 tx_data_oe_delay
= 0;
233 mstr_config
= (tx_data_oe_delay
<< TX_DATA_OE_DELAY_SHIFT
) |
234 (tx_data_delay
<< TX_DATA_DELAY_SHIFT
) | (SBL_EN
) |
235 (spi_mode
<< SPI_MODE_SHIFT
) |
241 write32(&sdm845_qspi
->mstr_cfg
, mstr_config
);
242 write32(&sdm845_qspi
->ahb_mstr_cfg
, 0xA42);
243 write32(&sdm845_qspi
->mstr_int_en
, 0x0);
244 write32(&sdm845_qspi
->mstr_int_sts
, 0xFFFFFFFF);
245 write32(&sdm845_qspi
->rd_fifo_cfg
, 0x0);
246 write32(&sdm845_qspi
->rd_fifo_rst
, RESET_FIFO
);
249 void quadspi_init(uint32_t hz
)
251 assert(dcache_line_bytes() == CACHE_LINE_SIZE
);
252 clock_configure_qspi(hz
* 4);
257 int sdm845_claim_bus(const struct spi_slave
*slave
)
259 cs_change(CS_ASSERT
);
263 void sdm845_release_bus(const struct spi_slave
*slave
)
265 cs_change(CS_DEASSERT
);
268 static int xfer(enum qspi_mode mode
, const void *dout
, size_t out_bytes
,
269 void *din
, size_t in_bytes
)
271 if ((out_bytes
&& !dout
) || (in_bytes
&& !din
) ||
272 (in_bytes
&& out_bytes
)) {
276 queue_data((uint8_t *) (out_bytes
? dout
: din
),
277 in_bytes
| out_bytes
, mode
, !!out_bytes
);
284 int sdm845_xfer(const struct spi_slave
*slave
, const void *dout
,
285 size_t out_bytes
, void *din
, size_t in_bytes
)
287 return xfer(SDR_1BIT
, dout
, out_bytes
, din
, in_bytes
);
290 int sdm845_xfer_dual(const struct spi_slave
*slave
, const void *dout
,
291 size_t out_bytes
, void *din
, size_t in_bytes
)
293 return xfer(SDR_2BIT
, dout
, out_bytes
, din
, in_bytes
);