Edward O'Callaghan | 2125edb | 2020-05-22 16:46:26 +1000 | [diff] [blame] | 1 | #include <include/test.h> |
| 2 | |
| 3 | #include "programmer.h" |
| 4 | #include "flashchips.h" |
| 5 | #include "chipdrivers.h" |
| 6 | #include "spi.h" |
| 7 | |
| 8 | int __wrap_spi_send_command(const struct flashctx *flash, |
| 9 | unsigned int writecnt, unsigned int readcnt, |
| 10 | const unsigned char *writearr, unsigned char *readarr) |
| 11 | { |
| 12 | check_expected_ptr(flash); |
| 13 | assert_int_equal(writecnt, mock_type(int)); |
| 14 | assert_int_equal(writearr[0], mock_type(int)); |
| 15 | |
| 16 | int rcnt = mock_type(int); |
| 17 | assert_int_equal(readcnt, rcnt); |
| 18 | for (int i = 0; i < rcnt; i++) |
| 19 | readarr[i] = i; |
| 20 | |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | struct flashchip mock_chip = { |
| 25 | .vendor = "Generic", |
| 26 | .name = "unknown SPI chip (RDID)", |
| 27 | .bustype = BUS_SPI, |
| 28 | .manufacture_id = GENERIC_MANUF_ID, |
| 29 | .model_id = GENERIC_DEVICE_ID, |
| 30 | .total_size = 0, |
| 31 | .page_size = 256, |
| 32 | .tested = TEST_BAD_PREW, |
| 33 | .probe = probe_spi_rdid, |
| 34 | .write = NULL, |
| 35 | }; |
| 36 | |
| 37 | void spi_write_enable_test_success(void **state) |
| 38 | { |
| 39 | (void) state; /* unused */ |
| 40 | |
| 41 | /* setup initial test state. */ |
| 42 | struct flashctx flashctx = { .chip = &mock_chip }; |
| 43 | expect_memory(__wrap_spi_send_command, flash, |
| 44 | &flashctx, sizeof(flashctx)); |
| 45 | |
| 46 | will_return(__wrap_spi_send_command, JEDEC_WREN_OUTSIZE); |
| 47 | will_return(__wrap_spi_send_command, JEDEC_WREN); |
| 48 | will_return(__wrap_spi_send_command, JEDEC_WREN_INSIZE); |
| 49 | assert_int_equal(0, spi_write_enable(&flashctx)); |
| 50 | } |
| 51 | |
| 52 | void spi_write_disable_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_WRDI_OUTSIZE); |
| 62 | will_return(__wrap_spi_send_command, JEDEC_WRDI); |
| 63 | will_return(__wrap_spi_send_command, JEDEC_WRDI_INSIZE); |
| 64 | assert_int_equal(0, spi_write_disable(&flashctx)); |
| 65 | } |
| 66 | |
| 67 | void probe_spi_rdid_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_RDID_OUTSIZE); |
| 77 | will_return(__wrap_spi_send_command, JEDEC_RDID); |
| 78 | will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE); |
| 79 | assert_int_equal(0, probe_spi_rdid(&flashctx)); |
| 80 | } |
| 81 | |
| 82 | void probe_spi_rdid4_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 + 1); |
| 94 | assert_int_equal(0, probe_spi_rdid4(&flashctx)); |
| 95 | } |
| 96 | |
| 97 | void probe_spi_rems_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_REMS_OUTSIZE); |
| 107 | will_return(__wrap_spi_send_command, JEDEC_REMS); |
| 108 | will_return(__wrap_spi_send_command, JEDEC_REMS_INSIZE); |
| 109 | assert_int_equal(0, probe_spi_rems(&flashctx)); |
| 110 | } |
| 111 | |
| 112 | void probe_spi_res1_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_RES_OUTSIZE); |
| 122 | will_return(__wrap_spi_send_command, JEDEC_RES); |
| 123 | will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1); |
| 124 | assert_int_equal(0, probe_spi_res2(&flashctx)); |
| 125 | } |
| 126 | |
| 127 | void probe_spi_res2_test_success(void **state) |
| 128 | { |
| 129 | (void) state; /* unused */ |
| 130 | |
| 131 | /* setup initial test state. */ |
Patrick Georgi | e16fdda | 2020-09-15 17:42:42 +0200 | [diff] [blame^] | 132 | clear_spi_id_cache(); |
Edward O'Callaghan | 2125edb | 2020-05-22 16:46:26 +1000 | [diff] [blame] | 133 | struct flashctx flashctx = { .chip = &mock_chip }; |
| 134 | expect_memory(__wrap_spi_send_command, flash, |
| 135 | &flashctx, sizeof(flashctx)); |
| 136 | |
| 137 | will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); |
| 138 | will_return(__wrap_spi_send_command, JEDEC_RES); |
| 139 | will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1); |
| 140 | assert_int_equal(0, probe_spi_res2(&flashctx)); |
| 141 | } |
| 142 | |
| 143 | void probe_spi_res3_test_success(void **state) |
| 144 | { |
| 145 | (void) state; /* unused */ |
| 146 | |
| 147 | /* setup initial test state. */ |
| 148 | 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 + 2); |
| 155 | assert_int_equal(0, probe_spi_res3(&flashctx)); |
| 156 | } |
| 157 | |
| 158 | void probe_spi_at25f_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, AT25F_RDID_OUTSIZE); |
| 168 | will_return(__wrap_spi_send_command, AT25F_RDID); |
| 169 | will_return(__wrap_spi_send_command, AT25F_RDID_INSIZE); |
| 170 | assert_int_equal(0, probe_spi_at25f(&flashctx)); |
| 171 | } |
Edward O'Callaghan | af4fed9 | 2020-05-22 19:09:49 +1000 | [diff] [blame] | 172 | |
| 173 | /* spi95.c */ |
| 174 | void probe_spi_st95_test_success(void **state) |
| 175 | { |
| 176 | (void) state; /* unused */ |
| 177 | |
| 178 | /* setup initial test state. */ |
| 179 | struct flashctx flashctx = { .chip = &mock_chip }; |
| 180 | expect_memory(__wrap_spi_send_command, flash, |
| 181 | &flashctx, sizeof(flashctx)); |
| 182 | |
| 183 | /* chip total size < 64K. */ |
| 184 | uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address |
| 185 | |
| 186 | will_return(__wrap_spi_send_command, rdid_outsize); |
| 187 | will_return(__wrap_spi_send_command, ST_M95_RDID); |
| 188 | will_return(__wrap_spi_send_command, ST_M95_RDID_INSIZE); |
| 189 | assert_int_equal(0, probe_spi_st95(&flashctx)); |
| 190 | } |