coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
spi_internal.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef __SOC_SIFIVE_HIFIVE_U_SPI_INTERNAL_H__
3 #define __SOC_SIFIVE_HIFIVE_U_SPI_INTERNAL_H__
4 
5 #include <stdint.h>
6 
7 #define _ASSERT_SIZEOF(type, size) _Static_assert( \
8  sizeof(type) == (size), \
9  #type " must be " #size " bytes wide")
10 
11 #define FU540_SPI_CSMODE_AUTO 0
12 #define FU540_SPI_CSMODE_HOLD 2
13 #define FU540_SPI_CSMODE_OFF 3
14 
15 typedef union {
16  struct {
20  };
24 
25 typedef union {
26  struct {
29  };
33 
34 typedef union {
35  struct {
40  };
44 
45 typedef union {
46  struct {
51  };
55 
56 typedef union {
57  struct {
64  };
66 } spi_reg_fmt;
68 
69 typedef union {
70  struct {
74  };
78 
79 typedef union {
80  struct {
84  };
88 
89 typedef union {
90  struct {
93  };
97 
98 typedef union {
99  struct {
102  };
106 
107 typedef union {
108  struct {
111  };
113 } spi_reg_fctrl;
115 
116 typedef union {
117  struct {
127  };
129 } spi_reg_ffmt;
131 
132 typedef union {
133  struct {
137  };
139 } spi_reg_ie;
143 
144 #undef _ASSERT_SIZEOF
145 
146 /**
147  * SPI control register memory map.
148  *
149  * All functions take a pointer to a SPI device's control registers.
150  */
151 struct spi_ctrl {
156 
161 
166 
171 
176 
181 
186 
189 };
190 
191 /**
192  * Get smallest clock divisor that divides input_khz to a quotient less than or
193  * equal to max_target_khz;
194  */
195 static inline unsigned int
196 spi_min_clk_divisor(unsigned int input_khz, unsigned int max_target_khz)
197 {
198  // f_sck = f_in / (2 * (div + 1)) => div = (f_in / (2*f_sck)) - 1
199  //
200  // The nearest integer solution for div requires rounding up as to not
201  // exceed max_target_khz.
202  //
203  // div = ceil(f_in / (2*f_sck)) - 1
204  // = floor((f_in - 1 + 2*f_sck) / (2*f_sck)) - 1
205  //
206  // This should not overflow as long as (f_in - 1 + 2*f_sck) does not
207  // exceed 2^32 - 1, which is unlikely since we represent frequencies
208  // in kHz.
209  unsigned int quotient =
210  (input_khz + 2 * max_target_khz - 1) / (2 * max_target_khz);
211  // Avoid underflow
212  if (quotient == 0)
213  return 0;
214  return quotient - 1;
215 }
216 
217 #endif /* __SOC_SIFIVE_HIFIVE_U_SPI_INTERNAL_H__ */
spi_reg_ie spi_reg_ip
Definition: spi_internal.h:140
#define _ASSERT_SIZEOF(type, size)
Definition: spi_internal.h:7
static unsigned int spi_min_clk_divisor(unsigned int input_khz, unsigned int max_target_khz)
Get smallest clock divisor that divides input_khz to a quotient less than or equal to max_target_khz;...
Definition: spi_internal.h:196
unsigned int uint32_t
Definition: stdint.h:14
SPI control register memory map.
Definition: spi_internal.h:151
uint32_t sckdiv
Definition: spi_internal.h:152
spi_reg_ip ip
Definition: spi_internal.h:188
spi_reg_ie ie
Definition: spi_internal.h:187
spi_reg_txmark txmark
Definition: spi_internal.h:177
uint32_t reserved3c
Definition: spi_internal.h:170
uint32_t reserved34
Definition: spi_internal.h:168
spi_reg_delay1 delay1
Definition: spi_internal.h:165
uint32_t reserved38
Definition: spi_internal.h:169
spi_reg_ffmt ffmt
Definition: spi_internal.h:183
spi_reg_rxmark rxmark
Definition: spi_internal.h:178
uint32_t reserved1c
Definition: spi_internal.h:160
spi_reg_rxdata rxdata
Definition: spi_internal.h:175
uint32_t csdef
Definition: spi_internal.h:158
uint32_t reserved08
Definition: spi_internal.h:154
uint32_t csid
Definition: spi_internal.h:157
uint32_t reserved30
Definition: spi_internal.h:167
uint32_t reserved24
Definition: spi_internal.h:163
spi_reg_txdata txdata
Definition: spi_internal.h:174
uint32_t reserved68
Definition: spi_internal.h:184
uint32_t reserved44
Definition: spi_internal.h:173
uint32_t reserved58
Definition: spi_internal.h:179
spi_reg_csmode csmode
Definition: spi_internal.h:159
uint32_t reserved0c
Definition: spi_internal.h:155
spi_reg_fmt fmt
Definition: spi_internal.h:172
uint32_t reserved20
Definition: spi_internal.h:162
spi_reg_delay0 delay0
Definition: spi_internal.h:164
spi_reg_fctrl fctrl
Definition: spi_internal.h:182
uint32_t reserved6c
Definition: spi_internal.h:185
spi_reg_sckmode sckmode
Definition: spi_internal.h:153
uint32_t reserved5c
Definition: spi_internal.h:180
uint32_t reserved
Definition: spi_internal.h:28
uint32_t mode
Definition: spi_internal.h:27
uint32_t raw_bits
Definition: spi_internal.h:30
uint32_t sckcs
Definition: spi_internal.h:38
uint32_t cssck
Definition: spi_internal.h:36
uint32_t reserved0
Definition: spi_internal.h:37
uint32_t reserved1
Definition: spi_internal.h:39
uint32_t raw_bits
Definition: spi_internal.h:41
uint32_t raw_bits
Definition: spi_internal.h:52
uint32_t interxfr
Definition: spi_internal.h:49
uint32_t reserved1
Definition: spi_internal.h:50
uint32_t intercs
Definition: spi_internal.h:47
uint32_t reserved0
Definition: spi_internal.h:48
uint32_t reserved
Definition: spi_internal.h:110
uint32_t raw_bits
Definition: spi_internal.h:112
uint32_t cmd_en
Definition: spi_internal.h:118
uint32_t raw_bits
Definition: spi_internal.h:128
uint32_t command_proto
Definition: spi_internal.h:121
uint32_t pad_code
Definition: spi_internal.h:126
uint32_t command_code
Definition: spi_internal.h:125
uint32_t reserved
Definition: spi_internal.h:124
uint32_t addr_len
Definition: spi_internal.h:119
uint32_t addr_proto
Definition: spi_internal.h:122
uint32_t pad_cnt
Definition: spi_internal.h:120
uint32_t data_proto
Definition: spi_internal.h:123
uint32_t endian
Definition: spi_internal.h:59
uint32_t proto
Definition: spi_internal.h:58
uint32_t len
Definition: spi_internal.h:62
uint32_t reserved1
Definition: spi_internal.h:63
uint32_t reserved0
Definition: spi_internal.h:61
uint32_t dir
Definition: spi_internal.h:60
uint32_t raw_bits
Definition: spi_internal.h:65
uint32_t txwm
Definition: spi_internal.h:134
uint32_t reserved
Definition: spi_internal.h:136
uint32_t raw_bits
Definition: spi_internal.h:138
uint32_t rxwm
Definition: spi_internal.h:135
uint32_t raw_bits
Definition: spi_internal.h:85
uint32_t data
Definition: spi_internal.h:81
uint32_t empty
Definition: spi_internal.h:83
uint32_t reserved
Definition: spi_internal.h:82
uint32_t rxmark
Definition: spi_internal.h:100
uint32_t reserved
Definition: spi_internal.h:101
uint32_t raw_bits
Definition: spi_internal.h:103
uint32_t reserved
Definition: spi_internal.h:19
uint32_t raw_bits
Definition: spi_internal.h:21
uint32_t raw_bits
Definition: spi_internal.h:75
uint32_t data
Definition: spi_internal.h:71
uint32_t reserved
Definition: spi_internal.h:72
uint32_t full
Definition: spi_internal.h:73
uint32_t raw_bits
Definition: spi_internal.h:94
uint32_t reserved
Definition: spi_internal.h:92
uint32_t txmark
Definition: spi_internal.h:91