blob: 1ffa00f9e02009b269db0d7154f9500d5a94b297 [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>
Nico Huberfbc41d22026-02-22 23:04:01 +010022
Nico Huberfbc41d22026-02-22 23:04:01 +010023#include "chipdrivers/spi.h"
Nico Huberb9e47cc2026-02-21 13:25:17 +010024#include "programmer.h"
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030025#include "spi.h"
26
27/* For ST95XXX chips which have RDID */
Nico Huberb9e47cc2026-02-21 13:25:17 +010028struct found_id *probe_spi_st95(const struct bus_probe *probe, const struct master_common *mst)
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030029{
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 };
Nico Huberb9e47cc2026-02-21 13:25:17 +010035 const struct spi_master *const spi = (const struct spi_master *)mst;
36 const size_t address_len = (uintptr_t)probe->arg;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030037 unsigned char readarr[ST_M95_RDID_INSIZE];
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030038
Nico Huberb9e47cc2026-02-21 13:25:17 +010039 if (spi->command(spi, 1 + address_len, sizeof(readarr), cmd, readarr))
40 return NULL;
41 if (flashprog_no_data(readarr, sizeof(readarr)))
42 return NULL;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030043
Nico Huberb9e47cc2026-02-21 13:25:17 +010044 struct found_id *const found = calloc(1, sizeof(*found));
45 if (!found) {
46 msg_cerr("Out of memory!\n");
47 return NULL;
48 }
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030049
Nico Huberb9e47cc2026-02-21 13:25:17 +010050 struct id_info *const id = &found->info.id;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030051
Nico Huberb9e47cc2026-02-21 13:25:17 +010052 id->manufacture = readarr[0];
53 id->model = (readarr[1] << 8) | readarr[2];
54 id->type = ID_SPI_ST95;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030055
Nico Huberb9e47cc2026-02-21 13:25:17 +010056 msg_cdbg("%s: id1 0x%02x, id2 0x%04x\n", __func__, id->id1, id->id2);
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030057
Nico Huberb9e47cc2026-02-21 13:25:17 +010058 return found;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030059}
60
61/* ST95XXX chips don't have erase operation and erase is made as part of write command */
62int spi_block_erase_emulation(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
63{
64 uint8_t *erased_contents = NULL;
65 int result = 0;
66
67 erased_contents = (uint8_t *)malloc(blocklen * sizeof(uint8_t));
68 if (!erased_contents) {
69 msg_cerr("Out of memory!\n");
70 return 1;
71 }
72 memset(erased_contents, ERASED_VALUE(flash), blocklen * sizeof(uint8_t));
73 result = spi_write_chunked(flash, erased_contents, 0, blocklen, flash->chip->page_size);
74 free(erased_contents);
75 return result;
76}