iothread: add polling parameters
[qemu/ar7.git] / tests / m25p80-test.c
blobcb7ec81f1a6d2050b5bb3e4d8cca8cae3ec79a8f
1 /*
2 * QTest testcase for the M25P80 Flash (Using the Aspeed SPI
3 * Controller)
5 * Copyright (C) 2016 IBM Corp.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qemu/bswap.h"
28 #include "libqtest.h"
31 * ASPEED SPI Controller registers
33 #define R_CONF 0x00
34 #define CONF_ENABLE_W0 (1 << 16)
35 #define R_CE_CTRL 0x04
36 #define CRTL_EXTENDED0 0 /* 32 bit addressing for SPI */
37 #define R_CTRL0 0x10
38 #define CTRL_CE_STOP_ACTIVE (1 << 2)
39 #define CTRL_USERMODE 0x3
41 #define ASPEED_FMC_BASE 0x1E620000
42 #define ASPEED_FLASH_BASE 0x20000000
45 * Flash commands
47 enum {
48 JEDEC_READ = 0x9f,
49 BULK_ERASE = 0xc7,
50 READ = 0x03,
51 PP = 0x02,
52 WREN = 0x6,
53 EN_4BYTE_ADDR = 0xB7,
54 ERASE_SECTOR = 0xd8,
57 #define FLASH_JEDEC 0x20ba19 /* n25q256a */
58 #define FLASH_SIZE (32 * 1024 * 1024)
60 #define PAGE_SIZE 256
63 * Use an explicit bswap for the values read/wrote to the flash region
64 * as they are BE and the Aspeed CPU is LE.
66 static inline uint32_t make_be32(uint32_t data)
68 return bswap32(data);
71 static void spi_conf(uint32_t value)
73 uint32_t conf = readl(ASPEED_FMC_BASE + R_CONF);
75 conf |= value;
76 writel(ASPEED_FMC_BASE + R_CONF, conf);
79 static void spi_ctrl_start_user(void)
81 uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
83 ctrl |= CTRL_USERMODE | CTRL_CE_STOP_ACTIVE;
84 writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
86 ctrl &= ~CTRL_CE_STOP_ACTIVE;
87 writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
90 static void spi_ctrl_stop_user(void)
92 uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
94 ctrl |= CTRL_USERMODE | CTRL_CE_STOP_ACTIVE;
95 writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
98 static void test_read_jedec(void)
100 uint32_t jedec = 0x0;
102 spi_conf(CONF_ENABLE_W0);
104 spi_ctrl_start_user();
105 writeb(ASPEED_FLASH_BASE, JEDEC_READ);
106 jedec |= readb(ASPEED_FLASH_BASE) << 16;
107 jedec |= readb(ASPEED_FLASH_BASE) << 8;
108 jedec |= readb(ASPEED_FLASH_BASE);
109 spi_ctrl_stop_user();
111 g_assert_cmphex(jedec, ==, FLASH_JEDEC);
114 static void read_page(uint32_t addr, uint32_t *page)
116 int i;
118 spi_ctrl_start_user();
120 writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
121 writeb(ASPEED_FLASH_BASE, READ);
122 writel(ASPEED_FLASH_BASE, make_be32(addr));
124 /* Continuous read are supported */
125 for (i = 0; i < PAGE_SIZE / 4; i++) {
126 page[i] = make_be32(readl(ASPEED_FLASH_BASE));
128 spi_ctrl_stop_user();
131 static void test_erase_sector(void)
133 uint32_t some_page_addr = 0x600 * PAGE_SIZE;
134 uint32_t page[PAGE_SIZE / 4];
135 int i;
137 spi_conf(CONF_ENABLE_W0);
139 spi_ctrl_start_user();
140 writeb(ASPEED_FLASH_BASE, WREN);
141 writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
142 writeb(ASPEED_FLASH_BASE, ERASE_SECTOR);
143 writel(ASPEED_FLASH_BASE, make_be32(some_page_addr));
144 spi_ctrl_stop_user();
146 /* Previous page should be full of zeroes as backend is not
147 * initialized */
148 read_page(some_page_addr - PAGE_SIZE, page);
149 for (i = 0; i < PAGE_SIZE / 4; i++) {
150 g_assert_cmphex(page[i], ==, 0x0);
153 /* But this one was erased */
154 read_page(some_page_addr, page);
155 for (i = 0; i < PAGE_SIZE / 4; i++) {
156 g_assert_cmphex(page[i], ==, 0xffffffff);
160 static void test_erase_all(void)
162 uint32_t some_page_addr = 0x15000 * PAGE_SIZE;
163 uint32_t page[PAGE_SIZE / 4];
164 int i;
166 spi_conf(CONF_ENABLE_W0);
168 /* Check some random page. Should be full of zeroes as backend is
169 * not initialized */
170 read_page(some_page_addr, page);
171 for (i = 0; i < PAGE_SIZE / 4; i++) {
172 g_assert_cmphex(page[i], ==, 0x0);
175 spi_ctrl_start_user();
176 writeb(ASPEED_FLASH_BASE, WREN);
177 writeb(ASPEED_FLASH_BASE, BULK_ERASE);
178 spi_ctrl_stop_user();
180 /* Recheck that some random page */
181 read_page(some_page_addr, page);
182 for (i = 0; i < PAGE_SIZE / 4; i++) {
183 g_assert_cmphex(page[i], ==, 0xffffffff);
187 static void test_write_page(void)
189 uint32_t my_page_addr = 0x14000 * PAGE_SIZE; /* beyond 16MB */
190 uint32_t some_page_addr = 0x15000 * PAGE_SIZE;
191 uint32_t page[PAGE_SIZE / 4];
192 int i;
194 spi_conf(CONF_ENABLE_W0);
196 spi_ctrl_start_user();
197 writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
198 writeb(ASPEED_FLASH_BASE, PP);
199 writel(ASPEED_FLASH_BASE, make_be32(my_page_addr));
201 /* Fill the page with its own addresses */
202 for (i = 0; i < PAGE_SIZE / 4; i++) {
203 writel(ASPEED_FLASH_BASE, make_be32(my_page_addr + i * 4));
205 spi_ctrl_stop_user();
207 /* Check what was written */
208 read_page(my_page_addr, page);
209 for (i = 0; i < PAGE_SIZE / 4; i++) {
210 g_assert_cmphex(page[i], ==, my_page_addr + i * 4);
213 /* Check some other page. It should be full of 0xff */
214 read_page(some_page_addr, page);
215 for (i = 0; i < PAGE_SIZE / 4; i++) {
216 g_assert_cmphex(page[i], ==, 0xffffffff);
220 static char tmp_path[] = "/tmp/qtest.m25p80.XXXXXX";
222 int main(int argc, char **argv)
224 int ret;
225 int fd;
226 char *args;
228 g_test_init(&argc, &argv, NULL);
230 fd = mkstemp(tmp_path);
231 g_assert(fd >= 0);
232 ret = ftruncate(fd, FLASH_SIZE);
233 g_assert(ret == 0);
234 close(fd);
236 args = g_strdup_printf("-m 256 -machine palmetto-bmc "
237 "-drive file=%s,format=raw,if=mtd",
238 tmp_path);
239 qtest_start(args);
241 qtest_add_func("/m25p80/read_jedec", test_read_jedec);
242 qtest_add_func("/m25p80/erase_sector", test_erase_sector);
243 qtest_add_func("/m25p80/erase_all", test_erase_all);
244 qtest_add_func("/m25p80/write_page", test_write_page);
246 ret = g_test_run();
248 qtest_quit(global_qtest);
249 unlink(tmp_path);
250 g_free(args);
251 return ret;