forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmpfs250.h
More file actions
390 lines (329 loc) · 16.6 KB
/
Copy pathmpfs250.h
File metadata and controls
390 lines (329 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* mpfs250.h
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef MPFS250_DEF_INCLUDED
#define MPFS250_DEF_INCLUDED
/* Generic RISC-V definitions are included at the end of this file
* (after PLIC_BASE is defined) to enable PLIC function declarations */
/* PolarFire SoC MPFS250T board specific configuration */
/* APB/AHB Clock Frequency */
#define MSS_APB_AHB_CLK 150000000
/* Hardware Base Address */
#define SYSREG_BASE 0x20002000
/* Write "0xDEAD" to cause a full MSS reset*/
#define SYSREG_MSS_RESET_CR (*((volatile uint32_t*)(SYSREG_BASE + 0x18)))
/* Peripheral Subblock Clock Control Register (offset 0x84) */
#define SYSREG_SUBBLK_CLOCK_CR (*((volatile uint32_t*)(SYSREG_BASE + 0x84)))
#define SYSREG_SUBBLK_CLOCK_CR_ENVM (1U << 0)
#define SYSREG_SUBBLK_CLOCK_CR_MMC (1U << 3)
#define SYSREG_SUBBLK_CLOCK_CR_QSPI (1U << 19)
/* Peripheral Soft Reset Control Register (offset 0x88) */
#define SYSREG_SOFT_RESET_CR (*((volatile uint32_t*)(SYSREG_BASE + 0x88)))
#define SYSREG_SOFT_RESET_CR_ENVM (1U << 0)
#define SYSREG_SOFT_RESET_CR_MMC (1U << 3)
#define SYSREG_SOFT_RESET_CR_MMUART0 (1U << 5)
#define SYSREG_SOFT_RESET_CR_MMUART1 (1U << 6)
#define SYSREG_SOFT_RESET_CR_MMUART2 (1U << 7)
#define SYSREG_SOFT_RESET_CR_MMUART3 (1U << 8)
#define SYSREG_SOFT_RESET_CR_MMUART4 (1U << 9)
#define SYSREG_SOFT_RESET_CR_SPI0 (1U << 10)
#define SYSREG_SOFT_RESET_CR_SPI1 (1U << 11)
#define SYSREG_SOFT_RESET_CR_QSPI (1U << 19)
#define SYSREG_SOFT_RESET_CR_GPIO0 (1U << 20)
#define SYSREG_SOFT_RESET_CR_GPIO1 (1U << 21)
#define SYSREG_SOFT_RESET_CR_GPIO2 (1U << 22)
#define SYSREG_SOFT_RESET_CR_DDRC (1U << 23)
#define SYSREG_SOFT_RESET_CR_ATHENA (1U << 28) /* Crypto hardware accelerator */
/* UART */
#define MSS_UART0_LO_BASE 0x20000000UL
#define MSS_UART1_LO_BASE 0x20100000UL
#define MSS_UART2_LO_BASE 0x20102000UL
#define MSS_UART3_LO_BASE 0x20104000UL
#define MSS_UART4_LO_BASE 0x20106000UL
#define MSS_UART0_HI_BASE 0x28000000UL
#define MSS_UART1_HI_BASE 0x28100000UL
#define MSS_UART2_HI_BASE 0x28102000UL
#define MSS_UART3_HI_BASE 0x28104000UL
#define MSS_UART4_HI_BASE 0x28106000UL
#define MMUART_RBR(base) *((volatile uint8_t*)((base)) + 0x00) /* Receiver buffer register */
#define MMUART_IER(base) *((volatile uint8_t*)((base)) + 0x04) /* Interrupt enable register */
#define MMUART_IIR(base) *((volatile uint8_t*)((base)) + 0x08) /* Interrupt ID register */
#define MMUART_LCR(base) *((volatile uint8_t*)((base)) + 0x0C) /* Line control register */
#define MMUART_MCR(base) *((volatile uint8_t*)((base)) + 0x10) /* Modem control register */
#define MMUART_LSR(base) *((volatile uint8_t*)((base)) + 0x14) /* Line status register */
#define MMUART_MSR(base) *((volatile uint8_t*)((base)) + 0x18) /* Modem status register */
#define MMUART_SCR(base) *((volatile uint8_t*)((base)) + 0x1C) /* Scratch register */
#define MMUART_IEM(base) *((volatile uint8_t*)((base)) + 0x24) /* Interrupt enable mask */
#define MMUART_IIM(base) *((volatile uint8_t*)((base)) + 0x28) /* multi-mode Interrupt ID register */
#define MMUART_MM0(base) *((volatile uint8_t*)((base)) + 0x30) /* Mode register 0 */
#define MMUART_MM1(base) *((volatile uint8_t*)((base)) + 0x34) /* Mode register 1 */
#define MMUART_MM2(base) *((volatile uint8_t*)((base)) + 0x38) /* Mode register 2 */
#define MMUART_DFR(base) *((volatile uint8_t*)((base)) + 0x3C) /* Data frame register */
#define MMUART_GFR(base) *((volatile uint8_t*)((base)) + 0x44) /* Global filter register */
#define MMUART_TTG(base) *((volatile uint8_t*)((base)) + 0x48) /* TX time guard register */
#define MMUART_RTO(base) *((volatile uint8_t*)((base)) + 0x4C) /* RX timeout register */
#define MMUART_ADR(base) *((volatile uint8_t*)((base)) + 0x50) /* Address register */
#define MMUART_DLR(base) *((volatile uint8_t*)((base)) + 0x80) /* Divisor latch register */
#define MMUART_DMR(base) *((volatile uint8_t*)((base)) + 0x84) /* Divisor mode register */
#define MMUART_THR(base) *((volatile uint8_t*)((base)) + 0x100) /* Transmitter holding register */
#define MMUART_FCR(base) *((volatile uint8_t*)((base)) + 0x104) /* FIFO control register */
/* LCR (Line Control Register) */
#define MSS_UART_DATA_8_BITS ((uint8_t)0x03)
#define MSS_UART_NO_PARITY ((uint8_t)0x00)
#define MSS_UART_ONE_STOP_BIT ((uint8_t)0x00)
/* LSR (Line Status Register) */
#define MSS_UART_THRE ((uint8_t)0x20) /* Transmitter holding register empty */
#define MSS_UART_TEMT ((uint8_t)0x40) /* Transmit empty */
#define ELIN_MASK (1U << 3) /* Enable LIN header detection */
#define EIRD_MASK (1U << 2) /* Enable IrDA modem */
#define EERR_MASK (1U << 0) /* Enable ERR / NACK during stop time */
#define RXRDY_TXRDYN_EN_MASK (1U << 0) /* Enable TXRDY and RXRDY signals */
#define CLEAR_RX_FIFO_MASK (1U << 1) /* Clear receiver FIFO */
#define CLEAR_TX_FIFO_MASK (1U << 2) /* Clear transmitter FIFO */
#define LOOP_MASK (1U << 4) /* Local loopback */
#define RLOOP_MASK (1U << 5) /* Remote loopback & Automatic echo*/
#define E_MSB_RX_MASK (1U << 0) /* MSB / LSB first for receiver */
#define E_MSB_TX_MASK (1U << 1) /* MSB / LSB first for transmitter */
#define EAFM_MASK (1U << 1) /* Enable 9-bit address flag mode */
#define ETTG_MASK (1U << 5) /* Enable transmitter time guard */
#define ERTO_MASK (1U << 6) /* Enable receiver time-out */
#define ESWM_MASK (1U << 3) /* Enable single wire half-duplex mode */
#define EFBR_MASK (1U << 7) /* Enable fractional baud rate mode */
/* Line Control register bit masks */
#define SB_MASK (1U << 6) /* Set break */
#define DLAB_MASK (1U << 7) /* Divisor latch access bit */
/* ============================================================================
* EMMC/SD Card Controller (Cadence SD4HC)
* Base Address: 0x20008000
* ============================================================================ */
#define EMMC_SD_BASE 0x20008000UL
/* ============================================================================
* System Controller Mailbox
* Control Base: 0x37020000
* Mailbox RAM: 0x37020800
*
* Used for system services like reading the device serial number.
* ============================================================================ */
#define SCBCTRL_BASE 0x37020000UL
#define SCBMBOX_BASE 0x37020800UL
/* System Services Control and Status Register offsets (from SCBCTRL_BASE) */
#define SERVICES_CR_OFFSET 0x50u
#define SERVICES_SR_OFFSET 0x54u
/* Control Register bits */
#define SERVICES_CR_REQ_MASK 0x01u
#define SERVICES_CR_COMMAND_SHIFT 16
/* Status Register bits */
#define SERVICES_SR_BUSY_MASK 0x02u
#define SERVICES_SR_STATUS_SHIFT 16
/* System Service command opcodes */
#define SYS_SERV_CMD_SERIAL_NUMBER 0x00u
#define SYS_SERV_CMD_SPI_COPY 0x50u /* SCB mailbox SPI copy service */
/* Device serial number size in bytes */
#define DEVICE_SERIAL_NUMBER_SIZE 16
/* Timeout loop iteration counts (override at build time via CFLAGS) */
#ifndef MPFS_SCB_TIMEOUT
#define MPFS_SCB_TIMEOUT 10000 /* SCB mailbox polling */
#endif
#ifndef QSPI_TIMEOUT_TRIES
#define QSPI_TIMEOUT_TRIES 100000 /* QSPI controller/TX polling */
#endif
#ifndef QSPI_RX_TIMEOUT_TRIES
#define QSPI_RX_TIMEOUT_TRIES 1000000 /* QSPI RX polling (longer) */
#endif
/* System Controller register access */
#define SCBCTRL_REG(off) (*((volatile uint32_t*)(SCBCTRL_BASE + (off))))
#define SCBMBOX_REG(off) (*((volatile uint32_t*)(SCBMBOX_BASE + (off))))
#define SCBMBOX_BYTE(off) (*((volatile uint8_t*)(SCBMBOX_BASE + (off))))
/* System Controller Mailbox API */
#ifndef __ASSEMBLER__
int mpfs_scb_service_call(uint8_t opcode, const uint8_t *mb_data,
uint32_t mb_len, uint32_t timeout);
int mpfs_scb_read_mailbox(uint8_t *out, uint32_t len);
int mpfs_read_serial_number(uint8_t *serial);
#endif /* __ASSEMBLER__ */
/* Crypto Engine: Athena F5200 TeraFire Crypto Processor (1x), 200 MHz */
#define ATHENA_BASE (SYSREG_BASE + 0x125000)
/* ============================================================================
* PLIC - Platform-Level Interrupt Controller (MPFS250-specific configuration)
* Base Address: 0x0c000000, Size: 64MB
*
* Generic PLIC register access is provided by hal/riscv.h
* ============================================================================ */
#define PLIC_BASE 0x0C000000UL
#define PLIC_SIZE 0x04000000UL /* 64MB */
/* Number of interrupt sources and contexts */
#define PLIC_NUM_SOURCES 186 /* riscv,ndev = 0xBA = 186 */
#define PLIC_NUM_HARTS 5 /* 1x E51 + 4x U54 */
#define PLIC_NUM_CONTEXTS 10 /* 2 contexts per hart (M-mode + S-mode) */
/* MSS Global Interrupt offset - PLIC interrupts 0-12 are local, 13+ are MSS */
#define OFFSET_TO_MSS_GLOBAL_INTS 13
/* PLIC Interrupt Sources (PLIC IRQ numbers) */
#define PLIC_INT_MMC_MAIN 88 /* MMC/SD controller main interrupt */
#define PLIC_INT_MMC_WAKEUP 89 /* MMC/SD controller wakeup interrupt */
/* PLIC Context IDs for each hart
* Hart 0 (E51): Context 0 = M-mode (no S-mode on E51)
* Hart 1 (U54): Context 1 = M-mode, Context 2 = S-mode
* Hart 2 (U54): Context 3 = M-mode, Context 4 = S-mode
* Hart 3 (U54): Context 5 = M-mode, Context 6 = S-mode
* Hart 4 (U54): Context 7 = M-mode, Context 8 = S-mode
*/
#define PLIC_CONTEXT_E51_M 0
#define PLIC_CONTEXT_U54_1_M 1
#define PLIC_CONTEXT_U54_1_S 2
#define PLIC_CONTEXT_U54_2_M 3
#define PLIC_CONTEXT_U54_2_S 4
#define PLIC_CONTEXT_U54_3_M 5
#define PLIC_CONTEXT_U54_3_S 6
#define PLIC_CONTEXT_U54_4_M 7
#define PLIC_CONTEXT_U54_4_S 8
#ifdef EXT_FLASH
/* ==========================================================================
* QSPI Flash Controller Definitions
*
* PolarFire SoC has two CoreQSPI v2 controllers with identical registers:
*
* 1. System Controller QSPI (MPFS_SC_SPI=1, default):
* - SC QSPI at 0x37020100 (size 0x100)
* - For fabric-connected flash (design flash)
* - Direct register access (same register layout as MSS QSPI)
* - Supports read, write, and erase operations
* - Does NOT require MSS clock enable or soft reset
*
* 2. MSS QSPI Controller (MPFS_SC_SPI=0):
* - MSS QSPI at 0x21000000 (size 0x1000)
* - For external flash connected to MSS QSPI pins
* - Requires MSS QSPI clock enable and soft reset release
* - Supports read, write, and erase operations
*
* ========================================================================== */
/* QSPI Controller Base Address */
#ifndef QSPI_BASE
#ifdef MPFS_SC_SPI
#define QSPI_BASE 0x37020100u /* SC QSPI Controller (fabric-connected flash) */
#else
#define QSPI_BASE 0x21000000u /* MSS QSPI Controller (external flash) */
#endif
#endif
/* QSPI Register Offsets */
#define QSPI_CONTROL (*(volatile uint32_t *)(QSPI_BASE + 0x00))
#define QSPI_FRAMES (*(volatile uint32_t *)(QSPI_BASE + 0x04))
#define QSPI_IEN (*(volatile uint32_t *)(QSPI_BASE + 0x0C))
#define QSPI_STATUS (*(volatile uint32_t *)(QSPI_BASE + 0x10))
#define QSPI_DIRECT (*(volatile uint32_t *)(QSPI_BASE + 0x14))
#define QSPI_ADDRUP (*(volatile uint32_t *)(QSPI_BASE + 0x18))
#define QSPI_RX_DATA (*(volatile uint8_t *)(QSPI_BASE + 0x40))
#define QSPI_TX_DATA (*(volatile uint8_t *)(QSPI_BASE + 0x44))
#define QSPI_X4_RX_DATA (*(volatile uint32_t *)(QSPI_BASE + 0x48))
#define QSPI_X4_TX_DATA (*(volatile uint32_t *)(QSPI_BASE + 0x4C))
#define QSPI_FRAMESUP (*(volatile uint32_t *)(QSPI_BASE + 0x50))
/* QSPI Control Register Bits */
#define QSPI_CTRL_EN_OFFSET 0
#define QSPI_CTRL_XIP_OFFSET 2
#define QSPI_CTRL_XIPADDR_OFFSET 3
#define QSPI_CTRL_CLKIDLE_OFFSET 10
#define QSPI_CTRL_SAMPLE_OFFSET 11
#define QSPI_CTRL_QMODE0_OFFSET 13
#define QSPI_CTRL_QMODE12_OFFSET 14
#define QSPI_CTRL_FLAGSX4_OFFSET 16
#define QSPI_CTRL_CLKRATE_OFFSET 24
#define QSPI_CTRL_EN (1u << QSPI_CTRL_EN_OFFSET)
#define QSPI_CTRL_XIP (1u << QSPI_CTRL_XIP_OFFSET)
#define QSPI_CTRL_CLKIDLE (1u << QSPI_CTRL_CLKIDLE_OFFSET)
#define QSPI_CTRL_SAMPLE_MASK (0x3u << QSPI_CTRL_SAMPLE_OFFSET)
#define QSPI_CTRL_SAMPLE_SCK (0x0u << QSPI_CTRL_SAMPLE_OFFSET)
#define QSPI_CTRL_SAMPLE_HCLKF (0x1u << QSPI_CTRL_SAMPLE_OFFSET)
#define QSPI_CTRL_SAMPLE_HCLKR (0x2u << QSPI_CTRL_SAMPLE_OFFSET)
#define QSPI_CTRL_QMODE0 (1u << QSPI_CTRL_QMODE0_OFFSET)
#define QSPI_CTRL_QMODE12_MASK (0x3u << QSPI_CTRL_QMODE12_OFFSET)
#define QSPI_CTRL_CLKRATE_MASK (0xFu << QSPI_CTRL_CLKRATE_OFFSET)
/* QSPI Frames Register Bits */
#define QSPI_FRAMES_TOTALBYTES_OFFSET 0
#define QSPI_FRAMES_CMDBYTES_OFFSET 16
#define QSPI_FRAMES_QSPI_OFFSET 25
#define QSPI_FRAMES_IDLE_OFFSET 26
#define QSPI_FRAMES_FBYTE_OFFSET 30
#define QSPI_FRAMES_FWORD_OFFSET 31
#define QSPI_FRAMES_TOTALBYTES_MASK (0xFFFFu << QSPI_FRAMES_TOTALBYTES_OFFSET)
#define QSPI_FRAMES_CMDBYTES_MASK (0x1FFu << QSPI_FRAMES_CMDBYTES_OFFSET)
#define QSPI_FRAMES_QSPI (1u << QSPI_FRAMES_QSPI_OFFSET)
#define QSPI_FRAMES_IDLE_MASK (0xFu << QSPI_FRAMES_IDLE_OFFSET)
/* QSPI Status Register Bits */
#define QSPI_STATUS_TXDONE (1u << 0)
#define QSPI_STATUS_RXDONE (1u << 1)
#define QSPI_STATUS_RXAVAIL (1u << 2)
#define QSPI_STATUS_TXAVAIL (1u << 3)
#define QSPI_STATUS_RXEMPTY (1u << 4)
/* Bit 5 is reserved in CoreQSPI v2 */
#define QSPI_STATUS_READY (1u << 7)
#define QSPI_STATUS_FLAGSX4 (1u << 8)
/* QSPI Clock Configuration */
#define QSPI_CLK_DIV_2 0x01u
#define QSPI_CLK_DIV_4 0x02u
#define QSPI_CLK_DIV_6 0x03u
#define QSPI_CLK_DIV_8 0x04u
#define QSPI_CLK_DIV_10 0x05u
#define QSPI_CLK_DIV_12 0x06u
#define QSPI_CLK_DIV_30 0x0Fu /* Conservative: ~5MHz from 150MHz APB */
/* QSPI SPI Modes */
#define QSPI_SPI_MODE0 0 /* CPOL=0, CPHA=0 */
#define QSPI_SPI_MODE3 1 /* CPOL=1, CPHA=1 */
/* QSPI IO Formats */
#define QSPI_IO_FORMAT_NORMAL 0 /* 1-bit SPI */
#define QSPI_IO_FORMAT_DUAL_EX0 1 /* 2-bit with extended mode 0 */
#define QSPI_IO_FORMAT_QUAD_EX0 2 /* 4-bit with extended mode 0 */
#define QSPI_IO_FORMAT_DUAL_EX1 3 /* 2-bit with extended mode 1 */
#define QSPI_IO_FORMAT_QUAD_EX1 4 /* 4-bit with extended mode 1 */
#define QSPI_IO_FORMAT_DUAL_FULL 5 /* Full 2-bit mode */
#define QSPI_IO_FORMAT_QUAD_FULL 6 /* Full 4-bit mode */
/* Micron MT25QL01G Flash Commands */
#define QSPI_CMD_READ_ID_OPCODE 0x9Fu /* JEDEC ID Read */
#define QSPI_CMD_MIO_READ_ID_OPCODE 0xAFu /* Multiple IO Read ID */
#define QSPI_CMD_READ_STATUS_OPCODE 0x05u /* Read Status Register */
#define QSPI_CMD_WRITE_ENABLE_OPCODE 0x06u /* Write Enable */
#define QSPI_CMD_WRITE_DISABLE_OPCODE 0x04u /* Write Disable */
#define QSPI_CMD_4BYTE_READ_OPCODE 0x13u /* 4-byte address read */
#define QSPI_CMD_4BYTE_FAST_READ_OPCODE 0x0Cu /* 4-byte fast read */
#define QSPI_CMD_4BYTE_QUAD_READ_OPCODE 0xECu /* 4-byte quad I/O read */
#define QSPI_CMD_4BYTE_PAGE_PROG_OPCODE 0x12u /* 4-byte page program */
#define QSPI_CMD_4BYTE_SECTOR_ERASE 0xDCu /* 4-byte 64KB sector erase */
#define QSPI_CMD_ENTER_4BYTE_MODE 0xB7u /* Enter 4-byte address mode */
#define QSPI_CMD_EXIT_4BYTE_MODE 0xE9u /* Exit 4-byte address mode */
/* Flash Geometry - Micron MT25QL01GBBB (128MB) */
#ifndef FLASH_DEVICE_SIZE
#define FLASH_DEVICE_SIZE (128 * 1024 * 1024) /* 128MB (1Gb) */
#endif
#ifndef FLASH_PAGE_SIZE
#define FLASH_PAGE_SIZE 256 /* 256 bytes */
#endif
#ifndef FLASH_SECTOR_SIZE
#define FLASH_SECTOR_SIZE (64 * 1024) /* 64KB sectors */
#endif
/* QSPI Transfer Modes */
#define QSPI_MODE_WRITE 0
#define QSPI_MODE_READ 1
/* I/O fence: ensure MMIO store reaches the peripheral before the next read.
* Required on RISC-V RVWMO to prevent stale TXAVAIL reads after TX writes. */
#define QSPI_IO_FENCE() __asm__ __volatile__("fence iorw, iorw" ::: "memory")
/* Function declarations for QSPI (when EXT_FLASH enabled) */
#ifndef __ASSEMBLER__
int qspi_init(void);
#endif /* __ASSEMBLER__ */
#endif /* EXT_FLASH */
#endif /* MPFS250_DEF_INCLUDED */