blob: 70a2074e0c59dbbfb0ab2c195520000cf35117ae [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 Huberdae90222026-03-09 20:36:56 +010028struct found_id *probe_spi_st95(const struct bus_probe *probe,
29 const struct master_common *mst,
30 const struct flashchip *chip)
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030031{
32 /*
33 * ST_M95_RDID_OUTSIZE depends on size of the flash and
34 * not all ST_M95XXX have RDID.
35 */
36 static const unsigned char cmd[ST_M95_RDID_OUTSIZE_MAX] = { ST_M95_RDID };
Nico Huberb9e47cc2026-02-21 13:25:17 +010037 const struct spi_master *const spi = (const struct spi_master *)mst;
38 const size_t address_len = (uintptr_t)probe->arg;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030039 unsigned char readarr[ST_M95_RDID_INSIZE];
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030040
Nico Huberb9e47cc2026-02-21 13:25:17 +010041 if (spi->command(spi, 1 + address_len, sizeof(readarr), cmd, readarr))
42 return NULL;
43 if (flashprog_no_data(readarr, sizeof(readarr)))
44 return NULL;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030045
Nico Huberb9e47cc2026-02-21 13:25:17 +010046 struct found_id *const found = calloc(1, sizeof(*found));
47 if (!found) {
48 msg_cerr("Out of memory!\n");
49 return NULL;
50 }
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030051
Nico Huberb9e47cc2026-02-21 13:25:17 +010052 struct id_info *const id = &found->info.id;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030053
Nico Huberb9e47cc2026-02-21 13:25:17 +010054 id->manufacture = readarr[0];
55 id->model = (readarr[1] << 8) | readarr[2];
56 id->type = ID_SPI_ST95;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030057
Nico Huberb9e47cc2026-02-21 13:25:17 +010058 msg_cdbg("%s: id1 0x%02x, id2 0x%04x\n", __func__, id->id1, id->id2);
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030059
Nico Huberb9e47cc2026-02-21 13:25:17 +010060 return found;
Konstantin Grudnev3d8868c2019-07-23 00:48:54 +030061}
62
63/* ST95XXX chips don't have erase operation and erase is made as part of write command */
64int spi_block_erase_emulation(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
65{
66 uint8_t *erased_contents = NULL;
67 int result = 0;
68
69 erased_contents = (uint8_t *)malloc(blocklen * sizeof(uint8_t));
70 if (!erased_contents) {
71 msg_cerr("Out of memory!\n");
72 return 1;
73 }
74 memset(erased_contents, ERASED_VALUE(flash), blocklen * sizeof(uint8_t));
75 result = spi_write_chunked(flash, erased_contents, 0, blocklen, flash->chip->page_size);
76 free(erased_contents);
77 return result;
78}