1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*******************************************************************************
8  * NOTICE
9  * The ll is not public api, don't use in application code.
10  * See readme.md in hal/include/hal/readme.md
11  ******************************************************************************/
12 
13 // The Lowlevel layer for SPI Flash
14 
15 #pragma once
16 
17 #include <stdlib.h>
18 #include <sys/param.h> // For MIN/MAX
19 #include <stdbool.h>
20 #include <string.h>
21 
22 #include "soc/spi_periph.h"
23 #include "soc/spi_mem_struct.h"
24 #include "hal/assert.h"
25 #include "hal/spi_types.h"
26 #include "hal/spi_flash_types.h"
27 #include "hal/misc.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define spimem_flash_ll_get_hw(host_id) (((host_id)==SPI1_HOST ?  &SPIMEM1 : NULL ))
34 #define spimem_flash_ll_hw_get_id(dev)  ((dev) == (void*)&SPIMEM1? SPI1_HOST: -1)
35 
36 typedef typeof(SPIMEM1.clock.val) spimem_flash_ll_clock_reg_t;
37 
38 /*------------------------------------------------------------------------------
39  * Control
40  *----------------------------------------------------------------------------*/
41 /**
42  * Reset peripheral registers before configuration and starting control
43  *
44  * @param dev Beginning address of the peripheral registers.
45  */
spimem_flash_ll_reset(spi_mem_dev_t * dev)46 static inline void spimem_flash_ll_reset(spi_mem_dev_t *dev)
47 {
48     dev->user.val = 0;
49     dev->ctrl.val = 0;
50 }
51 
52 /**
53  * Check whether the previous operation is done.
54  *
55  * @param dev Beginning address of the peripheral registers.
56  *
57  * @return true if last command is done, otherwise false.
58  */
spimem_flash_ll_cmd_is_done(const spi_mem_dev_t * dev)59 static inline bool spimem_flash_ll_cmd_is_done(const spi_mem_dev_t *dev)
60 {
61     return (dev->cmd.val == 0);
62 }
63 
64 /**
65  * Erase the flash chip.
66  *
67  * @param dev Beginning address of the peripheral registers.
68  */
spimem_flash_ll_erase_chip(spi_mem_dev_t * dev)69 static inline void spimem_flash_ll_erase_chip(spi_mem_dev_t *dev)
70 {
71     dev->cmd.flash_ce = 1;
72 }
73 
74 /**
75  * Erase the sector, the address should be set by spimem_flash_ll_set_address.
76  *
77  * @param dev Beginning address of the peripheral registers.
78  */
spimem_flash_ll_erase_sector(spi_mem_dev_t * dev)79 static inline void spimem_flash_ll_erase_sector(spi_mem_dev_t *dev)
80 {
81     dev->ctrl.val = 0;
82     dev->cmd.flash_se = 1;
83 }
84 
85 /**
86  * Erase the block, the address should be set by spimem_flash_ll_set_address.
87  *
88  * @param dev Beginning address of the peripheral registers.
89  */
spimem_flash_ll_erase_block(spi_mem_dev_t * dev)90 static inline void spimem_flash_ll_erase_block(spi_mem_dev_t *dev)
91 {
92     dev->cmd.flash_be = 1;
93 }
94 
95 /**
96  * Suspend erase/program operation.
97  *
98  * @param dev Beginning address of the peripheral registers.
99  */
spimem_flash_ll_suspend(spi_mem_dev_t * dev)100 static inline void spimem_flash_ll_suspend(spi_mem_dev_t *dev)
101 {
102     dev->flash_sus_cmd.flash_pes = 1;
103 }
104 
105 /**
106  * Resume suspended erase/program operation.
107  *
108  * @param dev Beginning address of the peripheral registers.
109  */
spimem_flash_ll_resume(spi_mem_dev_t * dev)110 static inline void spimem_flash_ll_resume(spi_mem_dev_t *dev)
111 {
112     dev->flash_sus_cmd.flash_per = 1;
113 }
114 
115 /**
116  * Initialize auto suspend mode
117  *
118  * @param dev Beginning address of the peripheral registers.
119  * @param auto_sus Enable/disable Flash Auto-Suspend.
120  */
spimem_flash_ll_auto_suspend_init(spi_mem_dev_t * dev,bool auto_sus)121 static inline void spimem_flash_ll_auto_suspend_init(spi_mem_dev_t *dev, bool auto_sus)
122 {
123     dev->flash_sus_ctrl.flash_pes_en = auto_sus;
124 }
125 
126 /**
127  * Initialize auto resume mode
128  *
129  * @param dev Beginning address of the peripheral registers.
130  * @param auto_res Enable/Disable Flash Auto-Resume.
131  *
132  */
spimem_flash_ll_auto_resume_init(spi_mem_dev_t * dev,bool auto_res)133 static inline void spimem_flash_ll_auto_resume_init(spi_mem_dev_t *dev, bool auto_res)
134 {
135     dev->misc.auto_per = 0; // Must disable Hardware Auto-Resume (should not be enabled, ESP32-S2 has bugs).
136 }
137 
138 /**
139  * Set 8 bit command to read suspend status
140  *
141  * @param dev Beginning address of the peripheral registers.
142  */
spimem_flash_ll_set_read_sus_status(spi_mem_dev_t * dev,uint32_t sus_mask)143 static inline void spimem_flash_ll_set_read_sus_status(spi_mem_dev_t *dev, uint32_t sus_mask)
144 {
145     abort();// Not supported on esp32s2
146 }
147 
148 /**
149  * Setup the flash suspend command, may vary from chips to chips.
150  *
151  * @param dev Beginning address of the peripheral registers.
152  * @param sus_cmd Flash suspend command.
153  *
154  */
spimem_flash_ll_suspend_cmd_setup(spi_mem_dev_t * dev,uint32_t sus_cmd)155 static inline void spimem_flash_ll_suspend_cmd_setup(spi_mem_dev_t *dev, uint32_t sus_cmd)
156 {
157     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->flash_sus_ctrl, flash_pes_command, sus_cmd);
158 }
159 
160 /**
161  * Setup the flash resume command, may vary from chips to chips.
162  *
163  * @param dev Beginning address of the peripheral registers.
164  * @param res_cmd Flash resume command.
165  *
166  */
spimem_flash_ll_resume_cmd_setup(spi_mem_dev_t * dev,uint32_t res_cmd)167 static inline void spimem_flash_ll_resume_cmd_setup(spi_mem_dev_t *dev, uint32_t res_cmd)
168 {
169     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->flash_sus_ctrl, flash_per_command, res_cmd);
170 }
171 
172 /**
173  * Setup the flash read suspend status command, may vary from chips to chips.
174  *
175  * @param dev Beginning address of the peripheral registers.
176  * @param pesr_cmd Flash read suspend status command.
177  *
178  */
spimem_flash_ll_rd_sus_cmd_setup(spi_mem_dev_t * dev,uint32_t pesr_cmd)179 static inline void spimem_flash_ll_rd_sus_cmd_setup(spi_mem_dev_t *dev, uint32_t pesr_cmd)
180 {
181     abort();// Not supported on esp32s2
182 }
183 
184 /**
185  * Setup to check SUS/SUS1/SUS2 to ensure the suspend status of flashs.
186  *
187  * @param dev Beginning address of the peripheral registers.
188  * @param sus_check_sus_en 1: enable, 0: disable.
189  *
190  */
spimem_flash_ll_sus_check_sus_setup(spi_mem_dev_t * dev,bool sus_check_sus_en)191 static inline void spimem_flash_ll_sus_check_sus_setup(spi_mem_dev_t *dev, bool sus_check_sus_en)
192 {
193     abort();// Not supported on esp32s2
194 }
195 
196 /**
197  * Setup to check SUS/SUS1/SUS2 to ensure the resume status of flashs.
198  *
199  * @param dev Beginning address of the peripheral registers.
200  * @param sus_check_sus_en 1: enable, 0: disable.
201  *
202  */
spimem_flash_ll_res_check_sus_setup(spi_mem_dev_t * dev,bool res_check_sus_en)203 static inline void spimem_flash_ll_res_check_sus_setup(spi_mem_dev_t *dev, bool res_check_sus_en)
204 {
205     abort();// Not supported on esp32s2
206 }
207 
208 /**
209  * Initialize auto wait idle mode
210  *
211  * @param dev Beginning address of the peripheral registers.
212  * @param auto_waiti Enable/disable auto wait-idle function
213  */
spimem_flash_ll_auto_wait_idle_init(spi_mem_dev_t * dev,bool auto_waiti)214 static inline void spimem_flash_ll_auto_wait_idle_init(spi_mem_dev_t *dev, bool auto_waiti)
215 {
216     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->flash_waiti_ctrl, waiti_cmd, 0x05); // Set the command to send, to fetch flash status reg value.
217     dev->flash_waiti_ctrl.waiti_en = auto_waiti;  // enable auto wait-idle function.
218 }
219 
220 /**
221  * Return the suspend status of erase or program operations.
222  *
223  * @param dev Beginning address of the peripheral registers.
224  *
225  * @return true if suspended, otherwise false.
226  */
spimem_flash_ll_sus_status(spi_mem_dev_t * dev)227 static inline bool spimem_flash_ll_sus_status(spi_mem_dev_t *dev)
228 {
229     return dev->sus_status.flash_sus;
230 }
231 
232 /**
233  * Enable/disable write protection for the flash chip.
234  *
235  * @param dev Beginning address of the peripheral registers.
236  * @param wp true to enable the protection, false to disable (write enable).
237  */
spimem_flash_ll_set_write_protect(spi_mem_dev_t * dev,bool wp)238 static inline void spimem_flash_ll_set_write_protect(spi_mem_dev_t *dev, bool wp)
239 {
240     if (wp) {
241         dev->cmd.flash_wrdi = 1;
242     } else {
243         dev->cmd.flash_wren = 1;
244     }
245 }
246 
247 /**
248  * Get the read data from the buffer after ``spimem_flash_ll_read`` is done.
249  *
250  * @param dev Beginning address of the peripheral registers.
251  * @param buffer Buffer to hold the output data
252  * @param read_len Length to get out of the buffer
253  */
spimem_flash_ll_get_buffer_data(spi_mem_dev_t * dev,void * buffer,uint32_t read_len)254 static inline void spimem_flash_ll_get_buffer_data(spi_mem_dev_t *dev, void *buffer, uint32_t read_len)
255 {
256     if (((intptr_t)buffer % 4 == 0) && (read_len % 4 == 0)) {
257         // If everything is word-aligned, do a faster memcpy
258         memcpy(buffer, (void *)dev->data_buf, read_len);
259     } else {
260         // Otherwise, slow(er) path copies word by word
261         int copy_len = read_len;
262         for (size_t i = 0; i < (read_len + 3) / 4; i++) {
263             int word_len = MIN(sizeof(uint32_t), copy_len);
264             uint32_t word = dev->data_buf[i];
265             memcpy(buffer, &word, word_len);
266             buffer = (void *)((intptr_t)buffer + word_len);
267             copy_len -= word_len;
268         }
269     }
270 }
271 
272 /**
273  * Set the data to be written in the data buffer.
274  *
275  * @param dev Beginning address of the peripheral registers.
276  * @param buffer Buffer holding the data
277  * @param length Length of data in bytes.
278  */
spimem_flash_ll_set_buffer_data(spi_mem_dev_t * dev,const void * buffer,uint32_t length)279 static inline void spimem_flash_ll_set_buffer_data(spi_mem_dev_t *dev, const void *buffer, uint32_t length)
280 {
281     // Load data registers, word at a time
282     int num_words = (length + 3) / 4;
283     for (int i = 0; i < num_words; i++) {
284         uint32_t word = 0;
285         uint32_t word_len = MIN(length, sizeof(word));
286         memcpy(&word, buffer, word_len);
287         dev->data_buf[i] = word;
288         length -= word_len;
289         buffer = (void *)((intptr_t)buffer + word_len);
290     }
291 }
292 
293 
294 /**
295  * Program a page of the flash chip. Call ``spimem_flash_ll_set_address`` before
296  * this to set the address to program.
297  *
298  * @param dev Beginning address of the peripheral registers.
299  * @param buffer Buffer holding the data to program
300  * @param length Length to program.
301  */
spimem_flash_ll_program_page(spi_mem_dev_t * dev,const void * buffer,uint32_t length)302 static inline void spimem_flash_ll_program_page(spi_mem_dev_t *dev, const void *buffer, uint32_t length)
303 {
304     dev->user.usr_dummy = 0;
305     spimem_flash_ll_set_buffer_data(dev, buffer, length);
306     dev->cmd.flash_pp = 1;
307 }
308 
309 /**
310  * Trigger a user defined transaction. All phases, including command, address, dummy, and the data phases,
311  * should be configured before this is called.
312  *
313  * @param dev Beginning address of the peripheral registers.
314  */
spimem_flash_ll_user_start(spi_mem_dev_t * dev)315 static inline void spimem_flash_ll_user_start(spi_mem_dev_t *dev)
316 {
317     dev->cmd.usr = 1;
318 }
319 
320 /**
321  * Check whether the host is idle to perform new commands.
322  *
323  * @param dev Beginning address of the peripheral registers.
324  *
325  * @return true if the host is idle, otherwise false
326  */
spimem_flash_ll_host_idle(const spi_mem_dev_t * dev)327 static inline bool spimem_flash_ll_host_idle(const spi_mem_dev_t *dev)
328 {
329     return dev->fsm.st == 0;
330 }
331 
332 /**
333  * Set phases for user-defined transaction to read
334  *
335  * @param dev Beginning address of the peripheral registers.
336  */
spimem_flash_ll_read_phase(spi_mem_dev_t * dev)337 static inline void spimem_flash_ll_read_phase(spi_mem_dev_t *dev)
338 {
339     typeof (dev->user) user = {
340         .usr_command = 1,
341         .usr_mosi = 0,
342         .usr_miso = 1,
343         .usr_addr = 1,
344     };
345     dev->user = user;
346 }
347 /*------------------------------------------------------------------------------
348  * Configs
349  *----------------------------------------------------------------------------*/
350 /**
351  * Select which pin to use for the flash
352  *
353  * @param dev Beginning address of the peripheral registers.
354  * @param pin Pin ID to use, 0-1. Set to other values to disable all the CS pins.
355  */
spimem_flash_ll_set_cs_pin(spi_mem_dev_t * dev,int pin)356 static inline void spimem_flash_ll_set_cs_pin(spi_mem_dev_t *dev, int pin)
357 {
358     dev->misc.cs0_dis = (pin != 0);
359     dev->misc.cs1_dis = (pin != 1);
360 }
361 
362 /**
363  * Set the read io mode.
364  *
365  * @param dev Beginning address of the peripheral registers.
366  * @param read_mode I/O mode to use in the following transactions.
367  */
spimem_flash_ll_set_read_mode(spi_mem_dev_t * dev,esp_flash_io_mode_t read_mode)368 static inline void spimem_flash_ll_set_read_mode(spi_mem_dev_t *dev, esp_flash_io_mode_t read_mode)
369 {
370     typeof (dev->ctrl) ctrl = dev->ctrl;
371     ctrl.val &= ~(SPI_MEM_FREAD_QIO_M | SPI_MEM_FREAD_QUAD_M | SPI_MEM_FREAD_DIO_M | SPI_MEM_FREAD_DUAL_M);
372     ctrl.val |= SPI_MEM_FASTRD_MODE_M;
373     switch (read_mode) {
374     case SPI_FLASH_FASTRD:
375         //the default option
376         break;
377     case SPI_FLASH_QIO:
378         ctrl.fread_qio = 1;
379         break;
380     case SPI_FLASH_QOUT:
381         ctrl.fread_quad = 1;
382         break;
383     case SPI_FLASH_DIO:
384         ctrl.fread_dio = 1;
385         break;
386     case SPI_FLASH_DOUT:
387         ctrl.fread_dual = 1;
388         break;
389     case SPI_FLASH_SLOWRD:
390         ctrl.fastrd_mode = 0;
391         break;
392     default:
393         abort();
394     }
395     dev->ctrl = ctrl;
396 }
397 
398 /**
399  * Set clock frequency to work at.
400  *
401  * @param dev Beginning address of the peripheral registers.
402  * @param clock_val pointer to the clock value to set
403  */
spimem_flash_ll_set_clock(spi_mem_dev_t * dev,spimem_flash_ll_clock_reg_t * clock_val)404 static inline void spimem_flash_ll_set_clock(spi_mem_dev_t *dev, spimem_flash_ll_clock_reg_t *clock_val)
405 {
406     dev->clock.val = *clock_val;
407 }
408 
409 /**
410  * Set the input length, in bits.
411  *
412  * @param dev Beginning address of the peripheral registers.
413  * @param bitlen Length of input, in bits.
414  */
spimem_flash_ll_set_miso_bitlen(spi_mem_dev_t * dev,uint32_t bitlen)415 static inline void spimem_flash_ll_set_miso_bitlen(spi_mem_dev_t *dev, uint32_t bitlen)
416 {
417     dev->user.usr_miso = bitlen > 0;
418     dev->miso_dlen.usr_miso_bit_len = bitlen ? (bitlen - 1) : 0;
419 }
420 
421 /**
422  * Set the output length, in bits (not including command, address and dummy
423  * phases)
424  *
425  * @param dev Beginning address of the peripheral registers.
426  * @param bitlen Length of output, in bits.
427  */
spimem_flash_ll_set_mosi_bitlen(spi_mem_dev_t * dev,uint32_t bitlen)428 static inline void spimem_flash_ll_set_mosi_bitlen(spi_mem_dev_t *dev, uint32_t bitlen)
429 {
430     dev->user.usr_mosi = bitlen > 0;
431     dev->mosi_dlen.usr_mosi_bit_len = bitlen ? (bitlen - 1) : 0;
432 }
433 
434 /**
435  * Set the command.
436  *
437  * @param dev Beginning address of the peripheral registers.
438  * @param command Command to send
439  * @param bitlen Length of the command
440  */
spimem_flash_ll_set_command(spi_mem_dev_t * dev,uint32_t command,uint32_t bitlen)441 static inline void spimem_flash_ll_set_command(spi_mem_dev_t *dev, uint32_t command, uint32_t bitlen)
442 {
443     dev->user.usr_command = 1;
444     typeof(dev->user2) user2 = {
445         .usr_command_value = command,
446         .usr_command_bitlen = (bitlen - 1),
447     };
448     dev->user2 = user2;
449 }
450 
451 /**
452  * Get the address length that is set in register, in bits.
453  *
454  * @param dev Beginning address of the peripheral registers.
455  *
456  */
spimem_flash_ll_get_addr_bitlen(spi_mem_dev_t * dev)457 static inline int spimem_flash_ll_get_addr_bitlen(spi_mem_dev_t *dev)
458 {
459     return dev->user.usr_addr ? dev->user1.usr_addr_bitlen + 1 : 0;
460 }
461 
462 /**
463  * Set the address length to send, in bits. Should be called before commands that requires the address e.g. erase sector, read, write...
464  *
465  * @param dev Beginning address of the peripheral registers.
466  * @param bitlen Length of the address, in bits
467  */
spimem_flash_ll_set_addr_bitlen(spi_mem_dev_t * dev,uint32_t bitlen)468 static inline void spimem_flash_ll_set_addr_bitlen(spi_mem_dev_t *dev, uint32_t bitlen)
469 {
470     dev->user1.usr_addr_bitlen = (bitlen - 1);
471     dev->user.usr_addr = bitlen ? 1 : 0;
472 }
473 
474 /**
475  * Set the address to send. Should be called before commands that requires the address e.g. erase sector, read, write...
476  *
477  * @param dev Beginning address of the peripheral registers.
478  * @param addr Address to send
479  */
spimem_flash_ll_set_address(spi_mem_dev_t * dev,uint32_t addr)480 static inline void spimem_flash_ll_set_address(spi_mem_dev_t *dev, uint32_t addr)
481 {
482     dev->addr = addr;
483 }
484 
485 /**
486  * Set the address to send in user mode. Should be called before commands that requires the address e.g. erase sector, read, write...
487  *
488  * @param dev Beginning address of the peripheral registers.
489  * @param addr Address to send
490  */
spimem_flash_ll_set_usr_address(spi_mem_dev_t * dev,uint32_t addr,uint32_t bitlen)491 static inline void spimem_flash_ll_set_usr_address(spi_mem_dev_t *dev, uint32_t addr, uint32_t bitlen)
492 {
493     (void)bitlen;
494     spimem_flash_ll_set_address(dev, addr);
495 }
496 
497 /**
498  * Set the length of dummy cycles.
499  *
500  * @param dev Beginning address of the peripheral registers.
501  * @param dummy_n Cycles of dummy phases
502  */
spimem_flash_ll_set_dummy(spi_mem_dev_t * dev,uint32_t dummy_n)503 static inline void spimem_flash_ll_set_dummy(spi_mem_dev_t *dev, uint32_t dummy_n)
504 {
505     dev->user.usr_dummy = dummy_n ? 1 : 0;
506     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->user1, usr_dummy_cyclelen, dummy_n - 1);
507 }
508 
509 /**
510  * Set D/Q output level during dummy phase
511  *
512  * @param dev Beginning address of the peripheral registers.
513  * @param out_en whether to enable IO output for dummy phase
514  * @param out_level dummy output level
515  */
spimem_flash_ll_set_dummy_out(spi_mem_dev_t * dev,uint32_t out_en,uint32_t out_lev)516 static inline void spimem_flash_ll_set_dummy_out(spi_mem_dev_t *dev, uint32_t out_en, uint32_t out_lev)
517 {
518     dev->ctrl.fdummy_out = out_en;
519     dev->ctrl.q_pol = out_lev;
520     dev->ctrl.d_pol = out_lev;
521 }
522 
spimem_flash_ll_set_hold(spi_mem_dev_t * dev,uint32_t hold_n)523 static inline void spimem_flash_ll_set_hold(spi_mem_dev_t *dev, uint32_t hold_n)
524 {
525     dev->ctrl2.cs_hold_time = hold_n - 1;
526     dev->user.cs_hold = (hold_n > 0? 1: 0);
527 }
528 
spimem_flash_ll_set_cs_setup(spi_mem_dev_t * dev,uint32_t cs_setup_time)529 static inline void spimem_flash_ll_set_cs_setup(spi_mem_dev_t *dev, uint32_t cs_setup_time)
530 {
531     dev->user.cs_setup = (cs_setup_time > 0 ? 1 : 0);
532     dev->ctrl2.cs_setup_time = cs_setup_time - 1;
533 }
534 
535 /**
536  * Get the spi flash source clock frequency. Used for calculating
537  * the divider parameters.
538  *
539  * @param None
540  *
541  * @return the frequency of spi flash clock source.(MHz)
542  */
spimem_flash_ll_get_source_freq_mhz(void)543 static inline uint8_t spimem_flash_ll_get_source_freq_mhz(void)
544 {
545     // Default is PLL480M, this is hard-coded.
546     // In the future, we can get the CPU clock source by calling interface.
547     uint8_t clock_val = 0;
548     switch (SPIMEM0.spi_core_clk_sel.spi01_clk_sel) {
549         case 0:
550             clock_val = 80;
551             break;
552         case 1:
553             clock_val = 120;
554             break;
555         case 2:
556             clock_val = 160;
557             break;
558         default:
559             abort();
560     }
561     return clock_val;
562 }
563 
564 /**
565  * Calculate spi_flash clock frequency division parameters for register.
566  *
567  * @param clkdiv frequency division factor
568  *
569  * @return Register setting for the given clock division factor.
570  */
spimem_flash_ll_calculate_clock_reg(uint8_t clkdiv)571 static inline uint32_t spimem_flash_ll_calculate_clock_reg(uint8_t clkdiv)
572 {
573     uint32_t div_parameter;
574     // See comments of `clock` in `spi_mem_struct.h`
575     if (clkdiv == 1) {
576         div_parameter = (1 << 31);
577     } else {
578         div_parameter = ((clkdiv - 1) | (((clkdiv - 1) / 2 & 0xff) << 8 ) | (((clkdiv - 1) & 0xff) << 16));
579     }
580     return div_parameter;
581 }
582 
583 
584 #ifdef __cplusplus
585 }
586 #endif
587