blob: eacc09f2e0a53a828596ed9c320ca7b261f09a5c [file] [log] [blame]
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00004 * Copyright (C) 2011,2013,2014 Carl-Daniel Hailfinger
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000014 */
15
16/*
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000017 * Contains the opaque master framework.
18 * An opaque master is a master which does not provide direct access
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000019 * to the flash chip and which abstracts all flash chip properties into a
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000020 * master specific interface.
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000021 */
22
23#include <stdint.h>
24#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
27#include "programmer.h"
28
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000029int probe_opaque(struct flashctx *flash)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000030{
Nico Huber9a11cbf2023-01-13 01:19:07 +010031 return flash->mst.opaque->probe(flash);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000032}
33
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000034int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000035{
Nico Huber9a11cbf2023-01-13 01:19:07 +010036 return flash->mst.opaque->read(flash, buf, start, len);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000037}
38
Mark Marshallf20b7be2014-05-09 21:16:21 +000039int write_opaque(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000040{
Nico Huber9a11cbf2023-01-13 01:19:07 +010041 return flash->mst.opaque->write(flash, buf, start, len);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000042}
43
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000044int erase_opaque(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000045{
Nico Huber9a11cbf2023-01-13 01:19:07 +010046 return flash->mst.opaque->erase(flash, blockaddr, blocklen);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000047}
48
Anastasia Klimchuk21b20212021-05-13 12:28:47 +100049int register_opaque_master(const struct opaque_master *mst, void *data)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000050{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000051 struct registered_master rmst;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000052
Anastasia Klimchuka1fed9f2021-08-03 14:08:02 +100053 if (mst->shutdown) {
54 if (register_shutdown(mst->shutdown, data)) {
55 mst->shutdown(data); /* cleanup */
56 return 1;
57 }
58 }
59
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000060 if (!mst->probe || !mst->read || !mst->write || !mst->erase) {
Nico Huberac90af62022-12-18 00:22:47 +000061 msg_perr("%s called with incomplete master definition.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +020062 "Please report a bug at flashprog@flashprog.org\n",
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000063 __func__);
Nico Huberc3b02dc2023-08-12 01:13:45 +020064 return ERROR_FLASHPROG_BUG;
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000065 }
Nico Huber89569d62023-01-12 23:31:40 +010066 rmst.max_rom_decode = MAX_ROM_DECODE_UNLIMITED;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000067 rmst.buses_supported = BUS_PROG;
68 rmst.opaque = *mst;
Anastasia Klimchuk21b20212021-05-13 12:28:47 +100069 if (data)
70 rmst.opaque.data = data;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000071 return register_master(&rmst);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000072}