xref: /qemu/tests/qtest/aspeed_smc-test.c (revision 188052a13377ff1f88b433255005eb9082d048e5)
17a2334f7SCédric Le Goater /*
27a2334f7SCédric Le Goater  * QTest testcase for the M25P80 Flash (Using the Aspeed SPI
37a2334f7SCédric Le Goater  * Controller)
47a2334f7SCédric Le Goater  *
57a2334f7SCédric Le Goater  * Copyright (C) 2016 IBM Corp.
67a2334f7SCédric Le Goater  *
77a2334f7SCédric Le Goater  * Permission is hereby granted, free of charge, to any person obtaining a copy
87a2334f7SCédric Le Goater  * of this software and associated documentation files (the "Software"), to deal
97a2334f7SCédric Le Goater  * in the Software without restriction, including without limitation the rights
107a2334f7SCédric Le Goater  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
117a2334f7SCédric Le Goater  * copies of the Software, and to permit persons to whom the Software is
127a2334f7SCédric Le Goater  * furnished to do so, subject to the following conditions:
137a2334f7SCédric Le Goater  *
147a2334f7SCédric Le Goater  * The above copyright notice and this permission notice shall be included in
157a2334f7SCédric Le Goater  * all copies or substantial portions of the Software.
167a2334f7SCédric Le Goater  *
177a2334f7SCédric Le Goater  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187a2334f7SCédric Le Goater  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197a2334f7SCédric Le Goater  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
207a2334f7SCédric Le Goater  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
217a2334f7SCédric Le Goater  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
227a2334f7SCédric Le Goater  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
237a2334f7SCédric Le Goater  * THE SOFTWARE.
247a2334f7SCédric Le Goater  */
257a2334f7SCédric Le Goater 
267a2334f7SCédric Le Goater #include "qemu/osdep.h"
277a2334f7SCédric Le Goater #include "qemu/bswap.h"
28dd210749SThomas Huth #include "libqtest-single.h"
29*188052a1SIris Chen #include "qemu/bitops.h"
307a2334f7SCédric Le Goater 
317a2334f7SCédric Le Goater /*
327a2334f7SCédric Le Goater  * ASPEED SPI Controller registers
337a2334f7SCédric Le Goater  */
347a2334f7SCédric Le Goater #define R_CONF              0x00
357a2334f7SCédric Le Goater #define   CONF_ENABLE_W0       (1 << 16)
367a2334f7SCédric Le Goater #define R_CE_CTRL           0x04
377a2334f7SCédric Le Goater #define   CRTL_EXTENDED0       0  /* 32 bit addressing for SPI */
387a2334f7SCédric Le Goater #define R_CTRL0             0x10
397a2334f7SCédric Le Goater #define   CTRL_CE_STOP_ACTIVE  (1 << 2)
40371a3dd2SCédric Le Goater #define   CTRL_READMODE        0x0
41371a3dd2SCédric Le Goater #define   CTRL_FREADMODE       0x1
42371a3dd2SCédric Le Goater #define   CTRL_WRITEMODE       0x2
437a2334f7SCédric Le Goater #define   CTRL_USERMODE        0x3
44*188052a1SIris Chen #define SR_WEL BIT(1)
457a2334f7SCédric Le Goater 
467a2334f7SCédric Le Goater #define ASPEED_FMC_BASE    0x1E620000
477a2334f7SCédric Le Goater #define ASPEED_FLASH_BASE  0x20000000
487a2334f7SCédric Le Goater 
497a2334f7SCédric Le Goater /*
507a2334f7SCédric Le Goater  * Flash commands
517a2334f7SCédric Le Goater  */
527a2334f7SCédric Le Goater enum {
537a2334f7SCédric Le Goater     JEDEC_READ = 0x9f,
54*188052a1SIris Chen     RDSR = 0x5,
55*188052a1SIris Chen     WRDI = 0x4,
567a2334f7SCédric Le Goater     BULK_ERASE = 0xc7,
577a2334f7SCédric Le Goater     READ = 0x03,
587a2334f7SCédric Le Goater     PP = 0x02,
597a2334f7SCédric Le Goater     WREN = 0x6,
60bd9f5052SCédric Le Goater     RESET_ENABLE = 0x66,
61bd9f5052SCédric Le Goater     RESET_MEMORY = 0x99,
627a2334f7SCédric Le Goater     EN_4BYTE_ADDR = 0xB7,
637a2334f7SCédric Le Goater     ERASE_SECTOR = 0xd8,
647a2334f7SCédric Le Goater };
657a2334f7SCédric Le Goater 
667a2334f7SCédric Le Goater #define FLASH_JEDEC         0x20ba19  /* n25q256a */
677a2334f7SCédric Le Goater #define FLASH_SIZE          (32 * 1024 * 1024)
687a2334f7SCédric Le Goater 
69d2c4f384SJiaxun Yang #define FLASH_PAGE_SIZE           256
707a2334f7SCédric Le Goater 
717a2334f7SCédric Le Goater /*
727a2334f7SCédric Le Goater  * Use an explicit bswap for the values read/wrote to the flash region
737a2334f7SCédric Le Goater  * as they are BE and the Aspeed CPU is LE.
747a2334f7SCédric Le Goater  */
757a2334f7SCédric Le Goater static inline uint32_t make_be32(uint32_t data)
767a2334f7SCédric Le Goater {
777a2334f7SCédric Le Goater     return bswap32(data);
787a2334f7SCédric Le Goater }
797a2334f7SCédric Le Goater 
807a2334f7SCédric Le Goater static void spi_conf(uint32_t value)
817a2334f7SCédric Le Goater {
827a2334f7SCédric Le Goater     uint32_t conf = readl(ASPEED_FMC_BASE + R_CONF);
837a2334f7SCédric Le Goater 
847a2334f7SCédric Le Goater     conf |= value;
857a2334f7SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CONF, conf);
867a2334f7SCédric Le Goater }
877a2334f7SCédric Le Goater 
88bd9f5052SCédric Le Goater static void spi_conf_remove(uint32_t value)
89bd9f5052SCédric Le Goater {
90bd9f5052SCédric Le Goater     uint32_t conf = readl(ASPEED_FMC_BASE + R_CONF);
91bd9f5052SCédric Le Goater 
92bd9f5052SCédric Le Goater     conf &= ~value;
93bd9f5052SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CONF, conf);
94bd9f5052SCédric Le Goater }
95bd9f5052SCédric Le Goater 
96371a3dd2SCédric Le Goater static void spi_ce_ctrl(uint32_t value)
97371a3dd2SCédric Le Goater {
98371a3dd2SCédric Le Goater     uint32_t conf = readl(ASPEED_FMC_BASE + R_CE_CTRL);
99371a3dd2SCédric Le Goater 
100371a3dd2SCédric Le Goater     conf |= value;
101371a3dd2SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CE_CTRL, conf);
102371a3dd2SCédric Le Goater }
103371a3dd2SCédric Le Goater 
104371a3dd2SCédric Le Goater static void spi_ctrl_setmode(uint8_t mode, uint8_t cmd)
105371a3dd2SCédric Le Goater {
106371a3dd2SCédric Le Goater     uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
107371a3dd2SCédric Le Goater     ctrl &= ~(CTRL_USERMODE | 0xff << 16);
108371a3dd2SCédric Le Goater     ctrl |= mode | (cmd << 16);
109371a3dd2SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
110371a3dd2SCédric Le Goater }
111371a3dd2SCédric Le Goater 
1127a2334f7SCédric Le Goater static void spi_ctrl_start_user(void)
1137a2334f7SCédric Le Goater {
1147a2334f7SCédric Le Goater     uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
1157a2334f7SCédric Le Goater 
1167a2334f7SCédric Le Goater     ctrl |= CTRL_USERMODE | CTRL_CE_STOP_ACTIVE;
1177a2334f7SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
1187a2334f7SCédric Le Goater 
1197a2334f7SCédric Le Goater     ctrl &= ~CTRL_CE_STOP_ACTIVE;
1207a2334f7SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
1217a2334f7SCédric Le Goater }
1227a2334f7SCédric Le Goater 
1237a2334f7SCédric Le Goater static void spi_ctrl_stop_user(void)
1247a2334f7SCédric Le Goater {
1257a2334f7SCédric Le Goater     uint32_t ctrl = readl(ASPEED_FMC_BASE + R_CTRL0);
1267a2334f7SCédric Le Goater 
1277a2334f7SCédric Le Goater     ctrl |= CTRL_USERMODE | CTRL_CE_STOP_ACTIVE;
1287a2334f7SCédric Le Goater     writel(ASPEED_FMC_BASE + R_CTRL0, ctrl);
1297a2334f7SCédric Le Goater }
1307a2334f7SCédric Le Goater 
131bd9f5052SCédric Le Goater static void flash_reset(void)
132bd9f5052SCédric Le Goater {
133bd9f5052SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
134bd9f5052SCédric Le Goater 
135bd9f5052SCédric Le Goater     spi_ctrl_start_user();
136bd9f5052SCédric Le Goater     writeb(ASPEED_FLASH_BASE, RESET_ENABLE);
137bd9f5052SCédric Le Goater     writeb(ASPEED_FLASH_BASE, RESET_MEMORY);
138bd9f5052SCédric Le Goater     spi_ctrl_stop_user();
139bd9f5052SCédric Le Goater 
140bd9f5052SCédric Le Goater     spi_conf_remove(CONF_ENABLE_W0);
141bd9f5052SCédric Le Goater }
142bd9f5052SCédric Le Goater 
1437a2334f7SCédric Le Goater static void test_read_jedec(void)
1447a2334f7SCédric Le Goater {
1457a2334f7SCédric Le Goater     uint32_t jedec = 0x0;
1467a2334f7SCédric Le Goater 
1477a2334f7SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
1487a2334f7SCédric Le Goater 
1497a2334f7SCédric Le Goater     spi_ctrl_start_user();
1507a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, JEDEC_READ);
1517a2334f7SCédric Le Goater     jedec |= readb(ASPEED_FLASH_BASE) << 16;
1527a2334f7SCédric Le Goater     jedec |= readb(ASPEED_FLASH_BASE) << 8;
1537a2334f7SCédric Le Goater     jedec |= readb(ASPEED_FLASH_BASE);
1547a2334f7SCédric Le Goater     spi_ctrl_stop_user();
1557a2334f7SCédric Le Goater 
156bd9f5052SCédric Le Goater     flash_reset();
157bd9f5052SCédric Le Goater 
1587a2334f7SCédric Le Goater     g_assert_cmphex(jedec, ==, FLASH_JEDEC);
1597a2334f7SCédric Le Goater }
1607a2334f7SCédric Le Goater 
1617a2334f7SCédric Le Goater static void read_page(uint32_t addr, uint32_t *page)
1627a2334f7SCédric Le Goater {
1637a2334f7SCédric Le Goater     int i;
1647a2334f7SCédric Le Goater 
1657a2334f7SCédric Le Goater     spi_ctrl_start_user();
1667a2334f7SCédric Le Goater 
1677a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
1687a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, READ);
1697a2334f7SCédric Le Goater     writel(ASPEED_FLASH_BASE, make_be32(addr));
1707a2334f7SCédric Le Goater 
1717a2334f7SCédric Le Goater     /* Continuous read are supported */
172d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
1737a2334f7SCédric Le Goater         page[i] = make_be32(readl(ASPEED_FLASH_BASE));
1747a2334f7SCédric Le Goater     }
1757a2334f7SCédric Le Goater     spi_ctrl_stop_user();
1767a2334f7SCédric Le Goater }
1777a2334f7SCédric Le Goater 
178371a3dd2SCédric Le Goater static void read_page_mem(uint32_t addr, uint32_t *page)
179371a3dd2SCédric Le Goater {
180371a3dd2SCédric Le Goater     int i;
181371a3dd2SCédric Le Goater 
182371a3dd2SCédric Le Goater     /* move out USER mode to use direct reads from the AHB bus */
183371a3dd2SCédric Le Goater     spi_ctrl_setmode(CTRL_READMODE, READ);
184371a3dd2SCédric Le Goater 
185d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
186371a3dd2SCédric Le Goater         page[i] = make_be32(readl(ASPEED_FLASH_BASE + addr + i * 4));
187371a3dd2SCédric Le Goater     }
188371a3dd2SCédric Le Goater }
189371a3dd2SCédric Le Goater 
1907a2334f7SCédric Le Goater static void test_erase_sector(void)
1917a2334f7SCédric Le Goater {
192d2c4f384SJiaxun Yang     uint32_t some_page_addr = 0x600 * FLASH_PAGE_SIZE;
193d2c4f384SJiaxun Yang     uint32_t page[FLASH_PAGE_SIZE / 4];
1947a2334f7SCédric Le Goater     int i;
1957a2334f7SCédric Le Goater 
1967a2334f7SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
1977a2334f7SCédric Le Goater 
1987a2334f7SCédric Le Goater     spi_ctrl_start_user();
1997a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, WREN);
2007a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
2017a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, ERASE_SECTOR);
2027a2334f7SCédric Le Goater     writel(ASPEED_FLASH_BASE, make_be32(some_page_addr));
2037a2334f7SCédric Le Goater     spi_ctrl_stop_user();
2047a2334f7SCédric Le Goater 
2057a2334f7SCédric Le Goater     /* Previous page should be full of zeroes as backend is not
2067a2334f7SCédric Le Goater      * initialized */
207d2c4f384SJiaxun Yang     read_page(some_page_addr - FLASH_PAGE_SIZE, page);
208d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2097a2334f7SCédric Le Goater         g_assert_cmphex(page[i], ==, 0x0);
2107a2334f7SCédric Le Goater     }
2117a2334f7SCédric Le Goater 
2127a2334f7SCédric Le Goater     /* But this one was erased */
2137a2334f7SCédric Le Goater     read_page(some_page_addr, page);
214d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2157a2334f7SCédric Le Goater         g_assert_cmphex(page[i], ==, 0xffffffff);
2167a2334f7SCédric Le Goater     }
217bd9f5052SCédric Le Goater 
218bd9f5052SCédric Le Goater     flash_reset();
2197a2334f7SCédric Le Goater }
2207a2334f7SCédric Le Goater 
2217a2334f7SCédric Le Goater static void test_erase_all(void)
2227a2334f7SCédric Le Goater {
223d2c4f384SJiaxun Yang     uint32_t some_page_addr = 0x15000 * FLASH_PAGE_SIZE;
224d2c4f384SJiaxun Yang     uint32_t page[FLASH_PAGE_SIZE / 4];
2257a2334f7SCédric Le Goater     int i;
2267a2334f7SCédric Le Goater 
2277a2334f7SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
2287a2334f7SCédric Le Goater 
2297a2334f7SCédric Le Goater     /* Check some random page. Should be full of zeroes as backend is
2307a2334f7SCédric Le Goater      * not initialized */
2317a2334f7SCédric Le Goater     read_page(some_page_addr, page);
232d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2337a2334f7SCédric Le Goater         g_assert_cmphex(page[i], ==, 0x0);
2347a2334f7SCédric Le Goater     }
2357a2334f7SCédric Le Goater 
2367a2334f7SCédric Le Goater     spi_ctrl_start_user();
2377a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, WREN);
2387a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, BULK_ERASE);
2397a2334f7SCédric Le Goater     spi_ctrl_stop_user();
2407a2334f7SCédric Le Goater 
2417a2334f7SCédric Le Goater     /* Recheck that some random page */
2427a2334f7SCédric Le Goater     read_page(some_page_addr, page);
243d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2447a2334f7SCédric Le Goater         g_assert_cmphex(page[i], ==, 0xffffffff);
2457a2334f7SCédric Le Goater     }
246bd9f5052SCédric Le Goater 
247bd9f5052SCédric Le Goater     flash_reset();
2487a2334f7SCédric Le Goater }
2497a2334f7SCédric Le Goater 
2507a2334f7SCédric Le Goater static void test_write_page(void)
2517a2334f7SCédric Le Goater {
252d2c4f384SJiaxun Yang     uint32_t my_page_addr = 0x14000 * FLASH_PAGE_SIZE; /* beyond 16MB */
253d2c4f384SJiaxun Yang     uint32_t some_page_addr = 0x15000 * FLASH_PAGE_SIZE;
254d2c4f384SJiaxun Yang     uint32_t page[FLASH_PAGE_SIZE / 4];
2557a2334f7SCédric Le Goater     int i;
2567a2334f7SCédric Le Goater 
2577a2334f7SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
2587a2334f7SCédric Le Goater 
2597a2334f7SCédric Le Goater     spi_ctrl_start_user();
2607a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
261bd9f5052SCédric Le Goater     writeb(ASPEED_FLASH_BASE, WREN);
2627a2334f7SCédric Le Goater     writeb(ASPEED_FLASH_BASE, PP);
2637a2334f7SCédric Le Goater     writel(ASPEED_FLASH_BASE, make_be32(my_page_addr));
2647a2334f7SCédric Le Goater 
2657a2334f7SCédric Le Goater     /* Fill the page with its own addresses */
266d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2677a2334f7SCédric Le Goater         writel(ASPEED_FLASH_BASE, make_be32(my_page_addr + i * 4));
2687a2334f7SCédric Le Goater     }
2697a2334f7SCédric Le Goater     spi_ctrl_stop_user();
2707a2334f7SCédric Le Goater 
2717a2334f7SCédric Le Goater     /* Check what was written */
2727a2334f7SCédric Le Goater     read_page(my_page_addr, page);
273d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2747a2334f7SCédric Le Goater         g_assert_cmphex(page[i], ==, my_page_addr + i * 4);
2757a2334f7SCédric Le Goater     }
2767a2334f7SCédric Le Goater 
2777a2334f7SCédric Le Goater     /* Check some other page. It should be full of 0xff */
2787a2334f7SCédric Le Goater     read_page(some_page_addr, page);
279d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
2807a2334f7SCédric Le Goater         g_assert_cmphex(page[i], ==, 0xffffffff);
2817a2334f7SCédric Le Goater     }
282bd9f5052SCédric Le Goater 
283bd9f5052SCédric Le Goater     flash_reset();
2847a2334f7SCédric Le Goater }
2857a2334f7SCédric Le Goater 
286371a3dd2SCédric Le Goater static void test_read_page_mem(void)
287371a3dd2SCédric Le Goater {
288d2c4f384SJiaxun Yang     uint32_t my_page_addr = 0x14000 * FLASH_PAGE_SIZE; /* beyond 16MB */
289d2c4f384SJiaxun Yang     uint32_t some_page_addr = 0x15000 * FLASH_PAGE_SIZE;
290d2c4f384SJiaxun Yang     uint32_t page[FLASH_PAGE_SIZE / 4];
291371a3dd2SCédric Le Goater     int i;
292371a3dd2SCédric Le Goater 
293371a3dd2SCédric Le Goater     /* Enable 4BYTE mode for controller. This is should be strapped by
294371a3dd2SCédric Le Goater      * HW for CE0 anyhow.
295371a3dd2SCédric Le Goater      */
296371a3dd2SCédric Le Goater     spi_ce_ctrl(1 << CRTL_EXTENDED0);
297371a3dd2SCédric Le Goater 
298371a3dd2SCédric Le Goater     /* Enable 4BYTE mode for flash. */
299371a3dd2SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
300371a3dd2SCédric Le Goater     spi_ctrl_start_user();
301371a3dd2SCédric Le Goater     writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
302371a3dd2SCédric Le Goater     spi_ctrl_stop_user();
303371a3dd2SCédric Le Goater     spi_conf_remove(CONF_ENABLE_W0);
304371a3dd2SCédric Le Goater 
305371a3dd2SCédric Le Goater     /* Check what was written */
306371a3dd2SCédric Le Goater     read_page_mem(my_page_addr, page);
307d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
308371a3dd2SCédric Le Goater         g_assert_cmphex(page[i], ==, my_page_addr + i * 4);
309371a3dd2SCédric Le Goater     }
310371a3dd2SCédric Le Goater 
311371a3dd2SCédric Le Goater     /* Check some other page. It should be full of 0xff */
312371a3dd2SCédric Le Goater     read_page_mem(some_page_addr, page);
313d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
314371a3dd2SCédric Le Goater         g_assert_cmphex(page[i], ==, 0xffffffff);
315371a3dd2SCédric Le Goater     }
316371a3dd2SCédric Le Goater 
317371a3dd2SCédric Le Goater     flash_reset();
318371a3dd2SCédric Le Goater }
319371a3dd2SCédric Le Goater 
320371a3dd2SCédric Le Goater static void test_write_page_mem(void)
321371a3dd2SCédric Le Goater {
322d2c4f384SJiaxun Yang     uint32_t my_page_addr = 0x15000 * FLASH_PAGE_SIZE;
323d2c4f384SJiaxun Yang     uint32_t page[FLASH_PAGE_SIZE / 4];
324371a3dd2SCédric Le Goater     int i;
325371a3dd2SCédric Le Goater 
326371a3dd2SCédric Le Goater     /* Enable 4BYTE mode for controller. This is should be strapped by
327371a3dd2SCédric Le Goater      * HW for CE0 anyhow.
328371a3dd2SCédric Le Goater      */
329371a3dd2SCédric Le Goater     spi_ce_ctrl(1 << CRTL_EXTENDED0);
330371a3dd2SCédric Le Goater 
331371a3dd2SCédric Le Goater     /* Enable 4BYTE mode for flash. */
332371a3dd2SCédric Le Goater     spi_conf(CONF_ENABLE_W0);
333371a3dd2SCédric Le Goater     spi_ctrl_start_user();
334371a3dd2SCédric Le Goater     writeb(ASPEED_FLASH_BASE, EN_4BYTE_ADDR);
335371a3dd2SCédric Le Goater     writeb(ASPEED_FLASH_BASE, WREN);
336371a3dd2SCédric Le Goater     spi_ctrl_stop_user();
337371a3dd2SCédric Le Goater 
338371a3dd2SCédric Le Goater     /* move out USER mode to use direct writes to the AHB bus */
339371a3dd2SCédric Le Goater     spi_ctrl_setmode(CTRL_WRITEMODE, PP);
340371a3dd2SCédric Le Goater 
341d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
342371a3dd2SCédric Le Goater         writel(ASPEED_FLASH_BASE + my_page_addr + i * 4,
343371a3dd2SCédric Le Goater                make_be32(my_page_addr + i * 4));
344371a3dd2SCédric Le Goater     }
345371a3dd2SCédric Le Goater 
346371a3dd2SCédric Le Goater     /* Check what was written */
347371a3dd2SCédric Le Goater     read_page_mem(my_page_addr, page);
348d2c4f384SJiaxun Yang     for (i = 0; i < FLASH_PAGE_SIZE / 4; i++) {
349371a3dd2SCédric Le Goater         g_assert_cmphex(page[i], ==, my_page_addr + i * 4);
350371a3dd2SCédric Le Goater     }
351371a3dd2SCédric Le Goater 
352371a3dd2SCédric Le Goater     flash_reset();
353371a3dd2SCédric Le Goater }
354371a3dd2SCédric Le Goater 
355*188052a1SIris Chen static void test_read_status_reg(void)
356*188052a1SIris Chen {
357*188052a1SIris Chen     uint8_t r;
358*188052a1SIris Chen 
359*188052a1SIris Chen     spi_conf(CONF_ENABLE_W0);
360*188052a1SIris Chen 
361*188052a1SIris Chen     spi_ctrl_start_user();
362*188052a1SIris Chen     writeb(ASPEED_FLASH_BASE, RDSR);
363*188052a1SIris Chen     r = readb(ASPEED_FLASH_BASE);
364*188052a1SIris Chen     spi_ctrl_stop_user();
365*188052a1SIris Chen 
366*188052a1SIris Chen     g_assert_cmphex(r & SR_WEL, ==, 0);
367*188052a1SIris Chen     g_assert(!qtest_qom_get_bool
368*188052a1SIris Chen             (global_qtest, "/machine/soc/fmc/ssi.0/child[0]", "write-enable"));
369*188052a1SIris Chen 
370*188052a1SIris Chen     spi_ctrl_start_user();
371*188052a1SIris Chen     writeb(ASPEED_FLASH_BASE, WREN);
372*188052a1SIris Chen     writeb(ASPEED_FLASH_BASE, RDSR);
373*188052a1SIris Chen     r = readb(ASPEED_FLASH_BASE);
374*188052a1SIris Chen     spi_ctrl_stop_user();
375*188052a1SIris Chen 
376*188052a1SIris Chen     g_assert_cmphex(r & SR_WEL, ==, SR_WEL);
377*188052a1SIris Chen     g_assert(qtest_qom_get_bool
378*188052a1SIris Chen             (global_qtest, "/machine/soc/fmc/ssi.0/child[0]", "write-enable"));
379*188052a1SIris Chen 
380*188052a1SIris Chen     spi_ctrl_start_user();
381*188052a1SIris Chen     writeb(ASPEED_FLASH_BASE, WRDI);
382*188052a1SIris Chen     writeb(ASPEED_FLASH_BASE, RDSR);
383*188052a1SIris Chen     r = readb(ASPEED_FLASH_BASE);
384*188052a1SIris Chen     spi_ctrl_stop_user();
385*188052a1SIris Chen 
386*188052a1SIris Chen     g_assert_cmphex(r & SR_WEL, ==, 0);
387*188052a1SIris Chen     g_assert(!qtest_qom_get_bool
388*188052a1SIris Chen             (global_qtest, "/machine/soc/fmc/ssi.0/child[0]", "write-enable"));
389*188052a1SIris Chen 
390*188052a1SIris Chen     flash_reset();
391*188052a1SIris Chen }
392*188052a1SIris Chen 
3937a2334f7SCédric Le Goater static char tmp_path[] = "/tmp/qtest.m25p80.XXXXXX";
3947a2334f7SCédric Le Goater 
3957a2334f7SCédric Le Goater int main(int argc, char **argv)
3967a2334f7SCédric Le Goater {
3977a2334f7SCédric Le Goater     int ret;
3987a2334f7SCédric Le Goater     int fd;
3997a2334f7SCédric Le Goater 
4007a2334f7SCédric Le Goater     g_test_init(&argc, &argv, NULL);
4017a2334f7SCédric Le Goater 
4027a2334f7SCédric Le Goater     fd = mkstemp(tmp_path);
4037a2334f7SCédric Le Goater     g_assert(fd >= 0);
4047a2334f7SCédric Le Goater     ret = ftruncate(fd, FLASH_SIZE);
4057a2334f7SCédric Le Goater     g_assert(ret == 0);
4067a2334f7SCédric Le Goater     close(fd);
4077a2334f7SCédric Le Goater 
40888b988c8SMarkus Armbruster     global_qtest = qtest_initf("-m 256 -machine palmetto-bmc "
4097a2334f7SCédric Le Goater                                "-drive file=%s,format=raw,if=mtd",
4107a2334f7SCédric Le Goater                                tmp_path);
4117a2334f7SCédric Le Goater 
4125fde7f10SCédric Le Goater     qtest_add_func("/ast2400/smc/read_jedec", test_read_jedec);
4135fde7f10SCédric Le Goater     qtest_add_func("/ast2400/smc/erase_sector", test_erase_sector);
4145fde7f10SCédric Le Goater     qtest_add_func("/ast2400/smc/erase_all",  test_erase_all);
4155fde7f10SCédric Le Goater     qtest_add_func("/ast2400/smc/write_page", test_write_page);
4165fde7f10SCédric Le Goater     qtest_add_func("/ast2400/smc/read_page_mem", test_read_page_mem);
4175fde7f10SCédric Le Goater     qtest_add_func("/ast2400/smc/write_page_mem", test_write_page_mem);
418*188052a1SIris Chen     qtest_add_func("/ast2400/smc/read_status_reg", test_read_status_reg);
4197a2334f7SCédric Le Goater 
4207a2334f7SCédric Le Goater     ret = g_test_run();
4217a2334f7SCédric Le Goater 
4227a2334f7SCédric Le Goater     qtest_quit(global_qtest);
4237a2334f7SCédric Le Goater     unlink(tmp_path);
4247a2334f7SCédric Le Goater     return ret;
4257a2334f7SCédric Le Goater }
426