blob: ee576e23abb658ed7eae5f4673662ba9ef433665 [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"
Nico Huberd5185632024-01-05 18:44:41 +010024#include "spi_command.h"
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030025#include "spi.h"
26
27/* For ST95XXX chips which have RDID */
28int 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 Georgib57f48f2020-05-02 16:07:11 +020037 int ret;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030038
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 Georgib57f48f2020-05-02 16:07:11 +020043 ret = spi_send_command(flash, rdid_outsize, sizeof(readarr), cmd, readarr);
44 if (ret)
Nico Huber6be06552023-02-12 00:18:09 +010045 return 0;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030046
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 */
59int 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}