1 /*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-12-31 BruceOu first implementation
9 * 2023-06-03 CX fixed sf probe error bug
10 * 2024-05-30 godmial refactor driver for multi-SPI bus auto-mount
11 */
12 #include <board.h>
13 #include "drv_spi.h"
14 #include "dev_spi_flash.h"
15
16 #ifdef RT_USING_SFUD
17 #include "dev_spi_flash_sfud.h"
18 #endif
19
20 #include <rthw.h>
21 #include <finsh.h>
22
23 #ifdef RT_USING_DFS
24 #include <dfs_fs.h>
25 #endif
26
27 struct spi_flash_config
28 {
29 const char *bus_name;
30 const char *device_name;
31 const char *flash_name;
32 rt_base_t cs_pin;
33 };
34
35 static const struct spi_flash_config flash_configs[] =
36 {
37 #ifdef BSP_USING_SPI0
38 {
39 .bus_name = "spi0",
40 .device_name = "spi00",
41 .flash_name = "gd25q_spi0",
42 .cs_pin = GET_PIN(A, 4),
43 },
44 #endif
45
46 #ifdef BSP_USING_SPI1
47 {
48 .bus_name = "spi1",
49 .device_name = "spi10",
50 .flash_name = "gd25q_spi1",
51 .cs_pin = GET_PIN(B, 9),
52 },
53 #endif
54
55 #ifdef BSP_USING_SPI2
56 {
57 .bus_name = "spi2",
58 .device_name = "spi20",
59 .flash_name = "gd25q_spi2",
60 .cs_pin = GET_PIN(B, 12),
61 },
62 #endif
63
64 #ifdef BSP_USING_SPI3
65 {
66 .bus_name = "spi3",
67 .device_name = "spi30",
68 .flash_name = "gd25q_spi3",
69 .cs_pin = GET_PIN(E, 4),
70 },
71 #endif
72
73 #ifdef BSP_USING_SPI4
74 {
75 .bus_name = "spi4",
76 .device_name = "spi40",
77 .flash_name = "gd25q_spi4",
78 .cs_pin = GET_PIN(F, 6),
79 },
80 #endif
81 };
82
83
spi_flash_init(void)84 static int spi_flash_init(void)
85 {
86 int result = RT_EOK;
87
88 for (size_t i = 0; i < sizeof(flash_configs) / sizeof(flash_configs[0]); i++)
89 {
90 const struct spi_flash_config *cfg = &flash_configs[i];
91
92 result = rt_hw_spi_device_attach(cfg->bus_name, cfg->device_name, cfg->cs_pin);
93 if (result != RT_EOK)
94 {
95 rt_kprintf("Failed to attach device %s on bus %s\n", cfg->device_name, cfg->bus_name);
96 continue;
97 }
98
99 #ifdef RT_USING_SFUD
100 if (RT_NULL == rt_sfud_flash_probe(cfg->flash_name, cfg->device_name))
101 {
102 rt_kprintf("SFUD probe failed: %s\n", cfg->flash_name);
103 continue;
104 }
105 #endif
106 }
107
108 return result;
109 }
110 INIT_COMPONENT_EXPORT(spi_flash_init);
111