blob: 942fe6e5719889eb139a6148d83d2a852a9ae179 [file] [log] [blame]
Nikolai Artemiev702c58a2020-09-16 14:12:22 +10001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2020 Google LLC
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
Edward O'Callaghan41f48c72020-05-22 16:46:26 +100016#include <include/test.h>
17
18#include "programmer.h"
19#include "flashchips.h"
20#include "chipdrivers.h"
21#include "spi.h"
22
23int __wrap_spi_send_command(const struct flashctx *flash,
24 unsigned int writecnt, unsigned int readcnt,
25 const unsigned char *writearr, unsigned char *readarr)
26{
27 check_expected_ptr(flash);
28 assert_int_equal(writecnt, mock_type(int));
29 assert_int_equal(writearr[0], mock_type(int));
30
31 int rcnt = mock_type(int);
32 assert_int_equal(readcnt, rcnt);
33 for (int i = 0; i < rcnt; i++)
34 readarr[i] = i;
35
36 return 0;
37}
38
39struct flashchip mock_chip = {
40 .vendor = "Generic",
41 .name = "unknown SPI chip (RDID)",
42 .bustype = BUS_SPI,
43 .manufacture_id = GENERIC_MANUF_ID,
44 .model_id = GENERIC_DEVICE_ID,
45 .total_size = 0,
46 .page_size = 256,
47 .tested = TEST_BAD_PREW,
48 .probe = probe_spi_rdid,
49 .write = NULL,
50};
51
52void spi_write_enable_test_success(void **state)
53{
54 (void) state; /* unused */
55
56 /* setup initial test state. */
57 struct flashctx flashctx = { .chip = &mock_chip };
58 expect_memory(__wrap_spi_send_command, flash,
59 &flashctx, sizeof(flashctx));
60
61 will_return(__wrap_spi_send_command, JEDEC_WREN_OUTSIZE);
62 will_return(__wrap_spi_send_command, JEDEC_WREN);
63 will_return(__wrap_spi_send_command, JEDEC_WREN_INSIZE);
64 assert_int_equal(0, spi_write_enable(&flashctx));
65}
66
67void spi_write_disable_test_success(void **state)
68{
69 (void) state; /* unused */
70
71 /* setup initial test state. */
72 struct flashctx flashctx = { .chip = &mock_chip };
73 expect_memory(__wrap_spi_send_command, flash,
74 &flashctx, sizeof(flashctx));
75
76 will_return(__wrap_spi_send_command, JEDEC_WRDI_OUTSIZE);
77 will_return(__wrap_spi_send_command, JEDEC_WRDI);
78 will_return(__wrap_spi_send_command, JEDEC_WRDI_INSIZE);
79 assert_int_equal(0, spi_write_disable(&flashctx));
80}
81
82void probe_spi_rdid_test_success(void **state)
83{
84 (void) state; /* unused */
85
86 /* setup initial test state. */
87 struct flashctx flashctx = { .chip = &mock_chip };
88 expect_memory(__wrap_spi_send_command, flash,
89 &flashctx, sizeof(flashctx));
90
91 will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE);
92 will_return(__wrap_spi_send_command, JEDEC_RDID);
93 will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE);
94 assert_int_equal(0, probe_spi_rdid(&flashctx));
95}
96
97void probe_spi_rdid4_test_success(void **state)
98{
99 (void) state; /* unused */
100
101 /* setup initial test state. */
102 struct flashctx flashctx = { .chip = &mock_chip };
103 expect_memory(__wrap_spi_send_command, flash,
104 &flashctx, sizeof(flashctx));
105
106 will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE);
107 will_return(__wrap_spi_send_command, JEDEC_RDID);
108 will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE + 1);
109 assert_int_equal(0, probe_spi_rdid4(&flashctx));
110}
111
112void probe_spi_rems_test_success(void **state)
113{
114 (void) state; /* unused */
115
116 /* setup initial test state. */
117 struct flashctx flashctx = { .chip = &mock_chip };
118 expect_memory(__wrap_spi_send_command, flash,
119 &flashctx, sizeof(flashctx));
120
121 will_return(__wrap_spi_send_command, JEDEC_REMS_OUTSIZE);
122 will_return(__wrap_spi_send_command, JEDEC_REMS);
123 will_return(__wrap_spi_send_command, JEDEC_REMS_INSIZE);
124 assert_int_equal(0, probe_spi_rems(&flashctx));
125}
126
127void probe_spi_res1_test_success(void **state)
128{
129 (void) state; /* unused */
130
131 /* setup initial test state. */
132 struct flashctx flashctx = { .chip = &mock_chip };
133 expect_memory(__wrap_spi_send_command, flash,
134 &flashctx, sizeof(flashctx));
135
136 will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE);
137 will_return(__wrap_spi_send_command, JEDEC_RES);
138 will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1);
139 assert_int_equal(0, probe_spi_res2(&flashctx));
140}
141
142void probe_spi_res2_test_success(void **state)
143{
144 (void) state; /* unused */
145
146 /* setup initial test state. */
Patrick Georgi0bb86012020-09-15 17:42:42 +0200147 clear_spi_id_cache();
Edward O'Callaghan41f48c72020-05-22 16:46:26 +1000148 struct flashctx flashctx = { .chip = &mock_chip };
149 expect_memory(__wrap_spi_send_command, flash,
150 &flashctx, sizeof(flashctx));
151
152 will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE);
153 will_return(__wrap_spi_send_command, JEDEC_RES);
154 will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1);
155 assert_int_equal(0, probe_spi_res2(&flashctx));
156}
157
158void probe_spi_res3_test_success(void **state)
159{
160 (void) state; /* unused */
161
162 /* setup initial test state. */
163 struct flashctx flashctx = { .chip = &mock_chip };
164 expect_memory(__wrap_spi_send_command, flash,
165 &flashctx, sizeof(flashctx));
166
167 will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE);
168 will_return(__wrap_spi_send_command, JEDEC_RES);
169 will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 2);
170 assert_int_equal(0, probe_spi_res3(&flashctx));
171}
172
173void probe_spi_at25f_test_success(void **state)
174{
175 (void) state; /* unused */
176
177 /* setup initial test state. */
178 struct flashctx flashctx = { .chip = &mock_chip };
179 expect_memory(__wrap_spi_send_command, flash,
180 &flashctx, sizeof(flashctx));
181
182 will_return(__wrap_spi_send_command, AT25F_RDID_OUTSIZE);
183 will_return(__wrap_spi_send_command, AT25F_RDID);
184 will_return(__wrap_spi_send_command, AT25F_RDID_INSIZE);
185 assert_int_equal(0, probe_spi_at25f(&flashctx));
186}
Edward O'Callaghan76d24452020-05-22 19:09:49 +1000187
188/* spi95.c */
189void probe_spi_st95_test_success(void **state)
190{
191 (void) state; /* unused */
192
193 /* setup initial test state. */
194 struct flashctx flashctx = { .chip = &mock_chip };
195 expect_memory(__wrap_spi_send_command, flash,
196 &flashctx, sizeof(flashctx));
197
198 /* chip total size < 64K. */
199 uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address
200
201 will_return(__wrap_spi_send_command, rdid_outsize);
202 will_return(__wrap_spi_send_command, ST_M95_RDID);
203 will_return(__wrap_spi_send_command, ST_M95_RDID_INSIZE);
204 assert_int_equal(0, probe_spi_st95(&flashctx));
205}