Debian Codegraph @

src/driver/spi/spi_probe.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /**
3 ******************************************************************************
4 * @file spi_probe.c
5 * @author StarFive Technology
6 * @version V1.0
7 * @date 07/24/2020
8 * @brief
9 ******************************************************************************
10 * @copy
11 *
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14 * TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE FOR ANY
15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16 * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *
19 * COPYRIGHT 2020 Shanghai StarFive Technology Co., Ltd.
20 */
21
22 #include <comdef.h>
23 #include <sys.h>
24 #include <spi_flash.h>
25 #include <spi_flash_internal.h>
26 #include <spi.h>
27 #include <uart.h>
28
29 #define IDCODE_CONT_LEN 0
30 #define IDCODE_PART_LEN 3
31 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
32
33 #define CMD_READ_ID 0x9f
34
35 #define NOR 0
36 #define GIGANAND 1
37
38 //#define CONFIG_SPI_FLASH_ATMEL
39 //#define CONFIG_SPI_FLASH_GIGADEVICE
40 //#define CONFIG_SPI_FLASH_EON
41 //#define CONFIG_SPI_FLASH_MACRONIX
42 //#define CONFIG_SPI_FLASH_SPANSION
43 //#define CONFIG_SPI_FLASH_STMICRO
44 //#define CONFIG_SPI_FLASH_SST
45 //#define CONFIG_SPI_FLASH_WINBOND
46 //#define TEST_GD25Q64B
47 //#define TEST_GD25Q64C
48 //#define TEST_GD25LB64C
49
50 static struct spi_flash g_spi_flash[1];
51
52 static const struct spi_flash_params spi_flash_table[] =
53 {
54 {"Common_flash", 0x534654, 8,16,16,256, NOR},
55 };
56
57 struct spi_flash *spi_flash_probe_nor(struct spi_slave *spi, u8 *idcode)
58 {
59 struct spi_flash_params *params;
60 struct spi_flash *flash;
61 u32 id = 0;
62 static int i = 0;
63
64 #if 0
65 id = ((idcode[2] << 16) | (idcode[1] << 8) | idcode[0]);
66 if(id == 0x0)
67 {
68 return NULL;
69 }
70
71 params = spi_flash_table;
72 for (i = 0; spi_flash_table[i].name != NULL; i++)
73 {
74 if ((spi_flash_table[i].id & 0xFFFFFF) == id)
75 {
76 break;
77 }
78 }
79 #endif
80 flash = &g_spi_flash[0];
81 if (!flash)
82 {
83 //uart_printf("SF: Failed to allocate memory\r\n");
84 return NULL;
85 }
86
87 flash->name = spi_flash_table[i].name;
88 if(spi_flash_table[i].flags == NOR)
89 {
90 /* Assuming power-of-two page size initially. */
91 flash->write = spi_flash_cmd_write_mode;
92 flash->erase = spi_flash_erase_mode;
93 flash->read = spi_flash_read_mode;
94 flash->page_size = 1 << spi_flash_table[i].l2_page_size;
95 flash->sector_size = flash->page_size * spi_flash_table[i].pages_per_sector;
96 flash->block_size = flash->sector_size * spi_flash_table[i].sectors_per_block;
97 flash->size = flash->page_size * spi_flash_table[i].pages_per_sector
98 * spi_flash_table[i].sectors_per_block
99 * spi_flash_table[i].nr_blocks;
100 }
101
102 //uart_printf("spi probe complete\r\n");
103
104 return flash;
105 }
106 #if 1
107 static int print_id(u8 *idcode, int len)
108 {
109 int i;
110
111 uart_printf("idcode:0x");
112 for (i=len-1; i>=0; i--)
113 print_ubyte_hex(idcode[i]);
114 uart_printf("\r\n");
115 return 0;
116 }
117 #endif
118 static int spi_read_id(struct spi_slave *spi, unsigned char cmd, void *response, u32 len)
119 {
120 int ret = -1,ret1 = -1,ret2 = -1;
121 unsigned char buf[4] = {0};// = {(u8)cmd, 0x00, 0x00, 0x00};
122 unsigned char buf_nor[1]; // = {(u8)cmd};
123 u8 *idcode = (u8 *)response;
124
125 buf[0] = cmd;
126 buf[1] = 0;
127 buf[2] = 0;
128 buf[3] = 0;
129
130 buf_nor[0] = cmd;
131
132 ret1 = spi_xfer(spi, 1*8, &buf[0], NULL, SPI_XFER_BEGIN, 8);
133 ret2 = spi_xfer(spi, len*8, NULL, response, SPI_XFER_END, 8);
134 return 0;
135 }
136
137 static struct spi_flash aic_flash;
138
139 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
140 unsigned int max_hz, unsigned int mode, unsigned int bus_width)
141 {
142 struct spi_slave *spi;
143 struct spi_flash *flash = &aic_flash;
144 int ret = 0;
145 u8 idcode[IDCODE_LEN];
146
147 spi = spi_setup_slave(bus, cs, max_hz, mode, bus_width);
148 if (!spi) {
149 //uart_printf("SF: Failed to set up slave\n");
150 return NULL;
151 }
152
153 /* Read the ID codes */
154 ret = spi_read_id(spi, CMD_READ_ID, idcode, sizeof(idcode));
155 if (ret)
156 {
157 //uart_printf("SF: Failed to read ID : %d\n", ret);
158 goto err_read_id;
159 }
160
161 print_id(idcode, sizeof(idcode));
162
163 flash = spi_flash_probe_nor(spi,idcode);
164 if (!flash)
165 {
166 goto err_manufacturer_probe;
167 }
168
169 flash->spi = spi;
170 return flash;
171
172 err_manufacturer_probe:
173 err_read_id:
174
175 return NULL;
176 }