blob: ecb2c1dbc49c8b85bfc964b6e322c09ea805bd8c [file] [log] [blame]
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +03001/*
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"
24#include "spi.h"
25
26/* For ST95XXX chips which have RDID */
27int probe_spi_st95(struct flashctx *flash)
28{
29 /*
30 * ST_M95_RDID_OUTSIZE depends on size of the flash and
31 * not all ST_M95XXX have RDID.
32 */
33 static const unsigned char cmd[ST_M95_RDID_OUTSIZE_MAX] = { ST_M95_RDID };
34 unsigned char readarr[ST_M95_RDID_INSIZE];
35 uint32_t id1, id2;
36
37 uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address
38 if (flash->chip->total_size * KiB > 64 * KiB)
39 rdid_outsize = ST_M95_RDID_3BA_OUTSIZE; // 24 bit address
40
41 spi_send_command(flash, rdid_outsize, sizeof(readarr), cmd, readarr);
42
43 id1 = readarr[0]; // manufacture id
44 id2 = (readarr[1] << 8) | readarr[2]; // SPI family code + model id
45
46 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
47
48 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
49 return 1;
50
51 return 0;
52}
53
54/* ST95XXX chips don't have erase operation and erase is made as part of write command */
55int spi_block_erase_emulation(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
56{
57 uint8_t *erased_contents = NULL;
58 int result = 0;
59
60 erased_contents = (uint8_t *)malloc(blocklen * sizeof(uint8_t));
61 if (!erased_contents) {
62 msg_cerr("Out of memory!\n");
63 return 1;
64 }
65 memset(erased_contents, ERASED_VALUE(flash), blocklen * sizeof(uint8_t));
66 result = spi_write_chunked(flash, erased_contents, 0, blocklen, flash->chip->page_size);
67 free(erased_contents);
68 return result;
69}