blob: 8c5946dd627cdaab91b87d9d1166cb70b89efc21 [file] [log] [blame]
hailfingerec022272010-01-06 10:21:00 +00001/*
2 * This file is part of the flashrom project.
3 *
Souvik Ghoshd75cd672016-06-17 14:21:39 -07004 * Copyright (C) 2009,2010,2011 Carl-Daniel Hailfinger
hailfingerec022272010-01-06 10:21:00 +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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
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.
hailfingerec022272010-01-06 10:21:00 +000015 */
16
hailfingerec022272010-01-06 10:21:00 +000017#include "flash.h"
hailfinger76bb7e92011-11-09 23:40:00 +000018#include "programmer.h"
19
Vadim Bendebury066143d2018-07-16 18:20:33 -070020/*
21 * The following two variables are used in the code even if the ich support is
22 * not compiled in, this is why they are exported through programmer.h and
23 * defined here.
24 */
Edward O'Callaghane3e30562019-09-03 13:10:58 +100025enum ich_chipset g_ich_generation = CHIPSET_ICH_UNKNOWN;
Vadim Bendebury066143d2018-07-16 18:20:33 -070026
hailfingerec022272010-01-06 10:21:00 +000027/* No-op shutdown() for programmers which don't need special handling */
28int noop_shutdown(void)
29{
30 return 0;
31}
32
33/* Fallback map() for programmers which don't need special handling */
Patrick Georgi4befc162017-02-03 18:32:01 +010034void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len)
hailfingerec022272010-01-06 10:21:00 +000035{
36 /* FIXME: Should return phys_addr. */
stepand0d220f2011-01-24 19:15:51 +000037 return NULL;
hailfingerec022272010-01-06 10:21:00 +000038}
39
40/* No-op/fallback unmap() for programmers which don't need special handling */
41void fallback_unmap(void *virt_addr, size_t len)
42{
43}
44
Craig Hesling65eb8812019-08-01 09:33:56 -070045/* No-op chip_writeb() for drivers not supporting addr/data pair accesses */
46uint8_t noop_chip_readb(const struct flashctx *flash, const chipaddr addr)
47{
48 return 0xff;
49}
50
51/* No-op chip_writeb() for drivers not supporting addr/data pair accesses */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070052void noop_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfingerec022272010-01-06 10:21:00 +000053{
54}
55
56/* Little-endian fallback for drivers not supporting 16 bit accesses */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070057void fallback_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
hailfingerec022272010-01-06 10:21:00 +000058{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070059 chip_writeb(flash, val & 0xff, addr);
60 chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
hailfingerec022272010-01-06 10:21:00 +000061}
62
63/* Little-endian fallback for drivers not supporting 16 bit accesses */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070064uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
hailfingerec022272010-01-06 10:21:00 +000065{
66 uint16_t val;
Souvik Ghoshd75cd672016-06-17 14:21:39 -070067 val = chip_readb(flash, addr);
68 val |= chip_readb(flash, addr + 1) << 8;
hailfingerec022272010-01-06 10:21:00 +000069 return val;
70}
71
72/* Little-endian fallback for drivers not supporting 32 bit accesses */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070073void fallback_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
hailfingerec022272010-01-06 10:21:00 +000074{
Souvik Ghoshd75cd672016-06-17 14:21:39 -070075 chip_writew(flash, val & 0xffff, addr);
76 chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
hailfingerec022272010-01-06 10:21:00 +000077}
78
79/* Little-endian fallback for drivers not supporting 32 bit accesses */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070080uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
hailfingerec022272010-01-06 10:21:00 +000081{
82 uint32_t val;
Souvik Ghoshd75cd672016-06-17 14:21:39 -070083 val = chip_readw(flash, addr);
84 val |= chip_readw(flash, addr + 2) << 16;
hailfingerec022272010-01-06 10:21:00 +000085 return val;
86}
87
Stuart langleyc98e43f2020-03-26 20:27:36 +110088void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
hailfingerec022272010-01-06 10:21:00 +000089{
90 size_t i;
91 for (i = 0; i < len; i++)
Souvik Ghoshd75cd672016-06-17 14:21:39 -070092 chip_writeb(flash, buf[i], addr + i);
hailfingerec022272010-01-06 10:21:00 +000093 return;
94}
95
Souvik Ghoshd75cd672016-06-17 14:21:39 -070096void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfingerec022272010-01-06 10:21:00 +000097{
98 size_t i;
99 for (i = 0; i < len; i++)
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700100 buf[i] = chip_readb(flash, addr + i);
hailfingerec022272010-01-06 10:21:00 +0000101 return;
102}
hailfinger76bb7e92011-11-09 23:40:00 +0000103
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100104int register_par_master(const struct par_master *mst,
105 const enum chipbustype buses)
hailfinger76bb7e92011-11-09 23:40:00 +0000106{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100107 struct registered_master rmst;
108 if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel ||
109 !mst->chip_writen || !mst->chip_readb || !mst->chip_readw ||
110 !mst->chip_readl || !mst->chip_readn) {
111 msg_perr("%s called with incomplete master definition. "
112 "Please report a bug at flashrom@flashrom.org\n",
113 __func__);
114 return ERROR_FLASHROM_BUG;
115 }
116
117 rmst.buses_supported = buses;
118 rmst.par = *mst;
119 return register_master(&rmst);
hailfinger76bb7e92011-11-09 23:40:00 +0000120}
David Hendricksba0827a2013-05-03 20:25:40 -0700121
Edward O'Callaghan20596a82019-06-13 14:47:03 +1000122/* The limit of 4 is totally arbitrary. */
123#define MASTERS_MAX 4
124struct registered_master registered_masters[MASTERS_MAX];
125int registered_master_count = 0;
126
127/* This function copies the struct registered_master parameter. */
128int register_master(const struct registered_master *mst)
129{
130 if (registered_master_count >= MASTERS_MAX) {
131 msg_perr("Tried to register more than %i master "
132 "interfaces.\n", MASTERS_MAX);
133 return ERROR_FLASHROM_LIMIT;
134 }
135 registered_masters[registered_master_count] = *mst;
136 registered_master_count++;
137
138 return 0;
139}
140
Edward O'Callaghan4b940572019-08-02 01:44:47 +1000141enum chipbustype get_buses_supported(void)
142{
143 int i;
144 enum chipbustype ret = BUS_NONE;
145
146 for (i = 0; i < registered_master_count; i++)
147 ret |= registered_masters[i].buses_supported;
148
149 return ret;
150}