Konstantin Grudnev | 3d8868c | 2019-07-23 00:48:54 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2019 Konstantin Grudnev |
| 5 | * Copyright (C) 2019 Nikolay Nikolaev |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, |
| 10 | * or any later version. |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contains SPI chip driver functions related to ST95XXX series (SPI EEPROM) |
| 19 | */ |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include "flashchips.h" |
| 23 | #include "chipdrivers.h" |
Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 24 | #include "spi_command.h" |
Konstantin Grudnev | 3d8868c | 2019-07-23 00:48:54 +0300 | [diff] [blame] | 25 | #include "spi.h" |
| 26 | |
| 27 | /* For ST95XXX chips which have RDID */ |
| 28 | int probe_spi_st95(struct flashctx *flash) |
| 29 | { |
| 30 | /* |
| 31 | * ST_M95_RDID_OUTSIZE depends on size of the flash and |
| 32 | * not all ST_M95XXX have RDID. |
| 33 | */ |
| 34 | static const unsigned char cmd[ST_M95_RDID_OUTSIZE_MAX] = { ST_M95_RDID }; |
| 35 | unsigned char readarr[ST_M95_RDID_INSIZE]; |
| 36 | uint32_t id1, id2; |
Patrick Georgi | b57f48f | 2020-05-02 16:07:11 +0200 | [diff] [blame] | 37 | int ret; |
Konstantin Grudnev | 3d8868c | 2019-07-23 00:48:54 +0300 | [diff] [blame] | 38 | |
| 39 | uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address |
| 40 | if (flash->chip->total_size * KiB > 64 * KiB) |
| 41 | rdid_outsize = ST_M95_RDID_3BA_OUTSIZE; // 24 bit address |
| 42 | |
Patrick Georgi | b57f48f | 2020-05-02 16:07:11 +0200 | [diff] [blame] | 43 | ret = spi_send_command(flash, rdid_outsize, sizeof(readarr), cmd, readarr); |
| 44 | if (ret) |
Nico Huber | 6be0655 | 2023-02-12 00:18:09 +0100 | [diff] [blame] | 45 | return 0; |
Konstantin Grudnev | 3d8868c | 2019-07-23 00:48:54 +0300 | [diff] [blame] | 46 | |
| 47 | id1 = readarr[0]; // manufacture id |
| 48 | id2 = (readarr[1] << 8) | readarr[2]; // SPI family code + model id |
| 49 | |
| 50 | msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
| 51 | |
| 52 | if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id) |
| 53 | return 1; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* ST95XXX chips don't have erase operation and erase is made as part of write command */ |
| 59 | int spi_block_erase_emulation(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 60 | { |
| 61 | uint8_t *erased_contents = NULL; |
| 62 | int result = 0; |
| 63 | |
| 64 | erased_contents = (uint8_t *)malloc(blocklen * sizeof(uint8_t)); |
| 65 | if (!erased_contents) { |
| 66 | msg_cerr("Out of memory!\n"); |
| 67 | return 1; |
| 68 | } |
| 69 | memset(erased_contents, ERASED_VALUE(flash), blocklen * sizeof(uint8_t)); |
| 70 | result = spi_write_chunked(flash, erased_contents, 0, blocklen, flash->chip->page_size); |
| 71 | free(erased_contents); |
| 72 | return result; |
| 73 | } |