-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsram.cpp
479 lines (388 loc) · 16.6 KB
/
psram.cpp
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Originally from https://github.com/sparkfun/sparkfun-pico
/**
@file sfe_psram.c
@brief This file contains a function that is used to detect and initialize PSRAM on
SparkFun rp2350 boards.
*/
/*
The MIT License (MIT)
Copyright (c) 2024 SparkFun Electronics
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions: The
above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Hacked by Earle Philhower to work with the Arduino-Pico core
#include <../../psram_board.h>
//#define PIMORONI_PICO_PLUS2_PSRAM_CS_PIN 47
//#define RP2350_PSRAM_CS PIMORONI_PICO_PLUS2_PSRAM_CS_PIN
#define RP2350_PSRAM_CS 47
#define PSRAM_HEAP 0
#ifdef RP2350_PSRAM_CS
#include <stdio.h>
#include <hardware/address_mapped.h>
#include <hardware/clocks.h>
#include <hardware/gpio.h>
#include <hardware/regs/addressmap.h>
#include <hardware/spi.h>
#include <hardware/structs/qmi.h>
#include <hardware/structs/xip_ctrl.h>
#include <pico/runtime_init.h>
#if PSRAM_HEAP
// Include TLSF in this compilation unit
#include "tlsf/tlsf.c"
static tlsf_t _mem_heap = nullptr;
static pool_t _mem_psram_pool = nullptr;
// PSRAM heap minus PSRAM global/static variables from the linker
/*
extern "C" {
extern uint8_t __psram_start__;
extern uint8_t __psram_heap_start__;
}
*/
#define __psram_start__ 0x11000000
uint32_t __psram_heap_start__;
// PSRAM Start : 0x11000000
// Heap Start : + 4096
static bool _bInitalized = false;
size_t ps_heap_size = 0;
#endif //PSRAM_HEAP
size_t ps_size = 0;
//#define RP2350_PSRAM_MAX_SELECT_FS64 (100'000'000)
//#define RP2350_PSRAM_MAX_SCK_HZ (100'000'000)
#define PICO_RUNTIME_INIT_PSRAM "11001" // Towards the end, after alarms
#ifndef RP2350_PSRAM_MAX_SELECT_FS64
#define RP2350_PSRAM_MAX_SELECT_FS64 (125'000'000)
#endif
#ifndef RP2350_PSRAM_MIN_DESELECT_FS
#define RP2350_PSRAM_MIN_DESELECT_FS (50'000'000)
#endif
#ifndef RP2350_PSRAM_MAX_SCK_HZ
#define RP2350_PSRAM_MAX_SCK_HZ (109'000'000)
#endif
#ifndef RP2350_PSRAM_ID
#define RP2350_PSRAM_ID (0x5D)
#endif
// DETAILS/
//
// SparkFun RP2350 boards use the following PSRAM IC:
//
// apmemory APS6404L-3SQR-ZR
// https://www.mouser.com/ProductDetail/AP-Memory/APS6404L-3SQR-ZR?qs=IS%252B4QmGtzzpDOdsCIglviw%3D%3D
//
// The origin of this logic is from the Circuit Python code that was downloaded from:
// https://github.com/raspberrypi/pico-sdk-rp2350/issues/12#issuecomment-2055274428
//
// Details on the PSRAM IC that are used during setup/configuration of PSRAM on SparkFun RP2350 boards.
// For PSRAM timing calculations - to use int math, we work in femto seconds (fs) (1e-15),
// NOTE: This idea is from micro python work on psram..
#define SFE_SEC_TO_FS 1000000000000000ll
// max select pulse width = 8us => 8e6 ns => 8000 ns => 8000 * 1e6 fs => 8000e6 fs
// Additionally, the MAX select is in units of 64 clock cycles - will use a constant that
// takes this into account - so 8000e6 fs / 64 = 125e6 fs
const uint32_t SFE_PSRAM_MAX_SELECT_FS64 = RP2350_PSRAM_MAX_SELECT_FS64;
// min deselect pulse width = 50ns => 50 * 1e6 fs => 50e7 fs
const uint32_t SFE_PSRAM_MIN_DESELECT_FS = RP2350_PSRAM_MIN_DESELECT_FS;
// from psram datasheet - max Freq with VDDat 3.3v - SparkFun RP2350 boards run at 3.3v.
// If VDD = 3.0 Max Freq is 133 Mhz
const uint32_t SFE_PSRAM_MAX_SCK_HZ = RP2350_PSRAM_MAX_SCK_HZ;
// PSRAM SPI command codes
const uint8_t PSRAM_CMD_QUAD_END = 0xF5;
const uint8_t PSRAM_CMD_QUAD_ENABLE = 0x35;
const uint8_t PSRAM_CMD_READ_ID = 0x9F;
const uint8_t PSRAM_CMD_RSTEN = 0x66;
const uint8_t PSRAM_CMD_RST = 0x99;
const uint8_t PSRAM_CMD_QUAD_READ = 0xEB;
const uint8_t PSRAM_CMD_QUAD_WRITE = 0x38;
const uint8_t PSRAM_CMD_NOOP = 0xFF;
const uint8_t PSRAM_ID = RP2350_PSRAM_ID;
//-----------------------------------------------------------------------------
/// @brief Communicate directly with the PSRAM IC - validate it is present and return the size
///
/// @return size_t The size of the PSRAM
///
/// @note This function expects the CS pin set
static size_t __no_inline_not_in_flash_func(get_psram_size)(void) {
size_t psram_size = 0;
// uint32_t intr_stash = save_and_disable_interrupts();
// Try and read the PSRAM ID via direct_csr.
qmi_hw->direct_csr = 30 << QMI_DIRECT_CSR_CLKDIV_LSB | QMI_DIRECT_CSR_EN_BITS;
// Need to poll for the cooldown on the last XIP transfer to expire
// (via direct-mode BUSY flag) before it is safe to perform the first
// direct-mode operation
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
}
// Exit out of QMI in case we've inited already
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
// Transmit the command to exit QPI quad mode - read ID as standard SPI
qmi_hw->direct_tx =
QMI_DIRECT_TX_OE_BITS | QMI_DIRECT_TX_IWIDTH_VALUE_Q << QMI_DIRECT_TX_IWIDTH_LSB | PSRAM_CMD_QUAD_END;
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
}
(void)qmi_hw->direct_rx;
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS);
// Read the id
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
uint8_t kgd = 0;
uint8_t eid = 0;
for (size_t i = 0; i < 7; i++) {
qmi_hw->direct_tx = (i == 0 ? PSRAM_CMD_READ_ID : PSRAM_CMD_NOOP);
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_TXEMPTY_BITS) == 0) {
}
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
}
if (i == 5) {
kgd = qmi_hw->direct_rx;
} else if (i == 6) {
eid = qmi_hw->direct_rx;
} else {
(void)qmi_hw->direct_rx; // just read and discard
}
}
// Disable direct csr.
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS | QMI_DIRECT_CSR_EN_BITS);
// is this the PSRAM we're looking for obi-wan?
if (kgd == PSRAM_ID) {
// PSRAM size
psram_size = 1024 * 1024; // 1 MiB
uint8_t size_id = eid >> 5;
if (eid == 0x26 || size_id == 2) {
psram_size *= 8;
} else if (size_id == 0) {
psram_size *= 2;
} else if (size_id == 1) {
psram_size *= 4;
}
}
// restore_interrupts(intr_stash);
return psram_size;
}
//-----------------------------------------------------------------------------
/// @brief Update the PSRAM timing configuration based on system clock
///
/// @note This function expects interrupts to be enabled on entry
static void __no_inline_not_in_flash_func(set_psram_timing)(void) {
// Get secs / cycle for the system clock - get before disabling interrupts.
uint32_t sysHz = (uint32_t)clock_get_hz(clk_sys);
// Calculate the clock divider - goal to get clock used for PSRAM <= what
// the PSRAM IC can handle - which is defined in SFE_PSRAM_MAX_SCK_HZ
volatile uint8_t clockDivider = (sysHz + SFE_PSRAM_MAX_SCK_HZ - 1) / SFE_PSRAM_MAX_SCK_HZ;
// uint32_t intr_stash = save_and_disable_interrupts();
// Get the clock femto seconds per cycle.
uint32_t fsPerCycle = SFE_SEC_TO_FS / sysHz;
// the maxSelect value is defined in units of 64 clock cycles
// So maxFS / (64 * fsPerCycle) = maxSelect = SFE_PSRAM_MAX_SELECT_FS64/fsPerCycle
volatile uint8_t maxSelect = SFE_PSRAM_MAX_SELECT_FS64 / fsPerCycle;
// minDeselect time - in system clock cycle
// Must be higher than 50ns (min deselect time for PSRAM) so add a fsPerCycle - 1 to round up
// So minFS/fsPerCycle = minDeselect = SFE_PSRAM_MIN_DESELECT_FS/fsPerCycle
volatile uint8_t minDeselect = (SFE_PSRAM_MIN_DESELECT_FS + fsPerCycle - 1) / fsPerCycle;
// Adjust / Test
// https://forums.raspberrypi.com/viewtopic.php?t=375975
// For Flash (Same with PSRAM ? )
//2 = RXDELAY (you have to be careful, usually for QSPI>100MHz it works with RXDELAY= CLKDIV)
//4 = CLKDIV (can be 1,2,3,4,... in contrast with 2040 which accepst only odd values 2,4,6 ...)
volatile uint8_t cooldown = 1;
volatile uint8_t rxdelay = 1;
if (sysHz>266000000) rxdelay = 3;
printf("System Clk %d, cooldown %d, rxdelay %d, Max Select: %d, Min Deselect: %d, clock divider: %d\n ",sysHz,cooldown,rxdelay,maxSelect,minDeselect,clockDivider);
qmi_hw->m[1].timing = QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB | // Break between pages.
3 << QMI_M1_TIMING_SELECT_HOLD_LSB | // Delay releasing CS for 3 extra system cycles.
cooldown << QMI_M1_TIMING_COOLDOWN_LSB |
rxdelay << QMI_M1_TIMING_RXDELAY_LSB |
maxSelect << QMI_M1_TIMING_MAX_SELECT_LSB |
minDeselect << QMI_M1_TIMING_MIN_DESELECT_LSB |
clockDivider << QMI_M1_TIMING_CLKDIV_LSB;
// restore_interrupts(intr_stash);
}
//-----------------------------------------------------------------------------
/// @brief The setup_psram function - note that this is not in flash
///
/// Fail if ps_size=0
void __no_inline_not_in_flash_func(setup_psram)(bool useheap,uint32_t heapstart)
{
// Set the PSRAM CS pin in the SDK
gpio_set_function(RP2350_PSRAM_CS, GPIO_FUNC_XIP_CS1);
// start with zero size
size_t psram_size = get_psram_size();
// No PSRAM - no dice
if (psram_size == 0) {
return;
}
// uint32_t intr_stash = save_and_disable_interrupts();
// Enable quad mode.
qmi_hw->direct_csr = 30 << QMI_DIRECT_CSR_CLKDIV_LSB | QMI_DIRECT_CSR_EN_BITS;
// Need to poll for the cooldown on the last XIP transfer to expire
// (via direct-mode BUSY flag) before it is safe to perform the first
// direct-mode operation
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
}
// RESETEN, RESET and quad enable
for (uint8_t i = 0; i < 3; i++) {
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
if (i == 0) {
qmi_hw->direct_tx = PSRAM_CMD_RSTEN;
} else if (i == 1) {
qmi_hw->direct_tx = PSRAM_CMD_RST;
} else {
qmi_hw->direct_tx = PSRAM_CMD_QUAD_ENABLE;
}
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
}
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS);
for (size_t j = 0; j < 20; j++) {
asm("nop"); // Maybe need to be changed if bigger overclocking
}
(void)qmi_hw->direct_rx;
}
// Disable direct csr.
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS | QMI_DIRECT_CSR_EN_BITS);
// check our interrupts and setup the timing
// restore_interrupts(intr_stash);
set_psram_timing();
// and now stash interrupts again
// intr_stash = save_and_disable_interrupts();
qmi_hw->m[1].rfmt = (QMI_M1_RFMT_PREFIX_WIDTH_VALUE_Q << QMI_M1_RFMT_PREFIX_WIDTH_LSB |
QMI_M1_RFMT_ADDR_WIDTH_VALUE_Q << QMI_M1_RFMT_ADDR_WIDTH_LSB |
QMI_M1_RFMT_SUFFIX_WIDTH_VALUE_Q << QMI_M1_RFMT_SUFFIX_WIDTH_LSB |
QMI_M1_RFMT_DUMMY_WIDTH_VALUE_Q << QMI_M1_RFMT_DUMMY_WIDTH_LSB |
QMI_M1_RFMT_DUMMY_LEN_VALUE_24 << QMI_M1_RFMT_DUMMY_LEN_LSB |
QMI_M1_RFMT_DATA_WIDTH_VALUE_Q << QMI_M1_RFMT_DATA_WIDTH_LSB |
QMI_M1_RFMT_PREFIX_LEN_VALUE_8 << QMI_M1_RFMT_PREFIX_LEN_LSB |
QMI_M1_RFMT_SUFFIX_LEN_VALUE_NONE << QMI_M1_RFMT_SUFFIX_LEN_LSB);
qmi_hw->m[1].rcmd = PSRAM_CMD_QUAD_READ << QMI_M1_RCMD_PREFIX_LSB | 0 << QMI_M1_RCMD_SUFFIX_LSB;
qmi_hw->m[1].wfmt = (QMI_M1_WFMT_PREFIX_WIDTH_VALUE_Q << QMI_M1_WFMT_PREFIX_WIDTH_LSB |
QMI_M1_WFMT_ADDR_WIDTH_VALUE_Q << QMI_M1_WFMT_ADDR_WIDTH_LSB |
QMI_M1_WFMT_SUFFIX_WIDTH_VALUE_Q << QMI_M1_WFMT_SUFFIX_WIDTH_LSB |
QMI_M1_WFMT_DUMMY_WIDTH_VALUE_Q << QMI_M1_WFMT_DUMMY_WIDTH_LSB |
QMI_M1_WFMT_DUMMY_LEN_VALUE_NONE << QMI_M1_WFMT_DUMMY_LEN_LSB |
QMI_M1_WFMT_DATA_WIDTH_VALUE_Q << QMI_M1_WFMT_DATA_WIDTH_LSB |
QMI_M1_WFMT_PREFIX_LEN_VALUE_8 << QMI_M1_WFMT_PREFIX_LEN_LSB |
QMI_M1_WFMT_SUFFIX_LEN_VALUE_NONE << QMI_M1_WFMT_SUFFIX_LEN_LSB);
qmi_hw->m[1].wcmd = PSRAM_CMD_QUAD_WRITE << QMI_M1_WCMD_PREFIX_LSB | 0 << QMI_M1_WCMD_SUFFIX_LSB;
// Mark that we can write to PSRAM.
xip_ctrl_hw->ctrl |= XIP_CTRL_WRITABLE_M1_BITS;
// restore_interrupts(intr_stash);
ps_size = psram_size;
#if PSRAM_HEAP
if (useheap) // if useheap is false, heap is not initializes as ps_heap_size=0
{
__psram_heap_start__=__psram_start__+heapstart;
uint32_t used_psram_size = __psram_heap_start__ - __psram_start__;
ps_heap_size = ps_size - used_psram_size;
}
#endif
}
// update timing -- used if the system clock/timing was changed.
void psram_reinit_timing() {
set_psram_timing();
}
#if PSRAM_HEAP
static bool ps_heap_init() {
if (_bInitalized) {
return true;
}
if (!ps_heap_size) {
return false;
}
_mem_heap = NULL;
_mem_psram_pool = NULL;
//_mem_heap = tlsf_create_with_pool((void *)__psram_heap_start__, ps_heap_size, 16 * 1024 * 1024);
_mem_heap = tlsf_create_with_pool((void *)__psram_heap_start__, ps_heap_size);
if (!_mem_heap) {
return false;
}
_mem_psram_pool = tlsf_get_pool(_mem_heap);
if (!_mem_psram_pool) {
return false;
}
_bInitalized = true;
return true;
}
void *ps_malloc(size_t size) {
if (!ps_heap_init() || !_mem_heap) {
return NULL;
}
return tlsf_malloc(_mem_heap, size);
}
void ps_free(void *ptr) {
if (!ps_heap_init() || !_mem_heap) {
return;
}
tlsf_free(_mem_heap, ptr);
}
void *ps_realloc(void *ptr, size_t size) {
if (!ps_heap_init() || !_mem_heap) {
return NULL;
}
return tlsf_realloc(_mem_heap, ptr, size);
}
void *ps_calloc(size_t num, size_t size) {
if (!ps_heap_init() || !_mem_heap) {
return NULL;
}
void *ptr = tlsf_malloc(_mem_heap, num * size);
if (ptr) {
bzero(ptr, num * size);
}
return ptr;
}
static bool max_free_walker(void *ptr, size_t size, int used, void *user) {
size_t *max_size = (size_t *)user;
if (!used && *max_size < size) {
*max_size = size;
}
return true;
}
size_t ps_largest_free_block() {
if (!ps_heap_init() || !_mem_heap) {
return 0;
}
size_t max_free = 0;
if (_mem_psram_pool) {
tlsf_walk_pool(_mem_psram_pool, (tlsf_walker) max_free_walker, &max_free);
}
return max_free;
}
static bool memory_size_walker(void *ptr, size_t size, int used, void *user) {
*((size_t *)user) += size;
return true;
}
size_t ps_total_space() {
if (!ps_heap_init() || !_mem_heap) {
return 0;
}
size_t total_size = 0;
if (_mem_psram_pool) {
tlsf_walk_pool(_mem_psram_pool, (tlsf_walker) memory_size_walker, &total_size);
}
return total_size;
}
static bool memory_used_walker(void *ptr, size_t size, int used, void *user) {
if (used) {
*((size_t *)user) += size;
}
return true;
}
size_t ps_total_used() {
if (!ps_heap_init() || !_mem_heap) {
return 0;
}
size_t total_size = 0;
if (_mem_psram_pool) {
tlsf_walk_pool(_mem_psram_pool, (tlsf_walker) memory_used_walker, &total_size);
}
return total_size;
}
#endif
#endif // RP2350_PSRAM_CS