Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009,2010,2011 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 5 | * |
| 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. |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 17 | #include "flash.h" |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 18 | #include "programmer.h" |
| 19 | |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 20 | /* Fallback map() for programmers which don't need special handling */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 21 | void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 22 | { |
| 23 | /* FIXME: Should return phys_addr. */ |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 24 | return NULL; |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /* No-op/fallback unmap() for programmers which don't need special handling */ |
| 28 | void fallback_unmap(void *virt_addr, size_t len) |
| 29 | { |
| 30 | } |
| 31 | |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 32 | /* Little-endian fallback for drivers not supporting 16 bit accesses */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 33 | void fallback_chip_writew(const struct flashctx *flash, uint16_t val, |
| 34 | chipaddr addr) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 35 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 36 | chip_writeb(flash, val & 0xff, addr); |
| 37 | chip_writeb(flash, (val >> 8) & 0xff, addr + 1); |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* Little-endian fallback for drivers not supporting 16 bit accesses */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 41 | uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 42 | { |
| 43 | uint16_t val; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 44 | val = chip_readb(flash, addr); |
| 45 | val |= chip_readb(flash, addr + 1) << 8; |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 46 | return val; |
| 47 | } |
| 48 | |
| 49 | /* Little-endian fallback for drivers not supporting 32 bit accesses */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 50 | void fallback_chip_writel(const struct flashctx *flash, uint32_t val, |
| 51 | chipaddr addr) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 52 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 53 | chip_writew(flash, val & 0xffff, addr); |
| 54 | chip_writew(flash, (val >> 16) & 0xffff, addr + 2); |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* Little-endian fallback for drivers not supporting 32 bit accesses */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 58 | uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 59 | { |
| 60 | uint32_t val; |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 61 | val = chip_readw(flash, addr); |
| 62 | val |= chip_readw(flash, addr + 2) << 16; |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 63 | return val; |
| 64 | } |
| 65 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 66 | void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 67 | { |
| 68 | size_t i; |
| 69 | for (i = 0; i < len; i++) |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 70 | chip_writeb(flash, buf[i], addr + i); |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 74 | void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf, |
| 75 | chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 76 | { |
| 77 | size_t i; |
| 78 | for (i = 0; i < len; i++) |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 79 | buf[i] = chip_readb(flash, addr + i); |
Carl-Daniel Hailfinger | cc1802d | 2010-01-06 10:21:00 +0000 | [diff] [blame] | 80 | return; |
| 81 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 82 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 83 | int register_par_master(const struct par_master *mst, |
Anastasia Klimchuk | b91a203 | 2021-05-21 09:40:58 +1000 | [diff] [blame] | 84 | const enum chipbustype buses, |
| 85 | void *data) |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 86 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 87 | struct registered_master rmst; |
Anastasia Klimchuk | e6953e5 | 2021-08-26 10:10:32 +1000 | [diff] [blame^] | 88 | |
| 89 | if (mst->shutdown) { |
| 90 | if (register_shutdown(mst->shutdown, data)) { |
| 91 | mst->shutdown(data); /* cleanup */ |
| 92 | return 1; |
| 93 | } |
| 94 | } |
| 95 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 96 | if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel || |
| 97 | !mst->chip_writen || !mst->chip_readb || !mst->chip_readw || |
| 98 | !mst->chip_readl || !mst->chip_readn) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 99 | msg_perr("%s called with incomplete master definition.\n" |
| 100 | "Please report a bug at flashrom-stable@flashrom.org\n", |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 101 | __func__); |
| 102 | return ERROR_FLASHROM_BUG; |
| 103 | } |
| 104 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 105 | rmst.buses_supported = buses; |
| 106 | rmst.par = *mst; |
Anastasia Klimchuk | b91a203 | 2021-05-21 09:40:58 +1000 | [diff] [blame] | 107 | if (data) |
| 108 | rmst.par.data = data; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 109 | return register_master(&rmst); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* The limit of 4 is totally arbitrary. */ |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 113 | #define MASTERS_MAX 4 |
| 114 | struct registered_master registered_masters[MASTERS_MAX]; |
| 115 | int registered_master_count = 0; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 116 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 117 | /* This function copies the struct registered_master parameter. */ |
Stefan Tauner | 5c316f9 | 2015-02-08 21:57:52 +0000 | [diff] [blame] | 118 | int register_master(const struct registered_master *mst) |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 119 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 120 | if (registered_master_count >= MASTERS_MAX) { |
| 121 | msg_perr("Tried to register more than %i master " |
| 122 | "interfaces.\n", MASTERS_MAX); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 123 | return ERROR_FLASHROM_LIMIT; |
| 124 | } |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 125 | registered_masters[registered_master_count] = *mst; |
| 126 | registered_master_count++; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | enum chipbustype get_buses_supported(void) |
| 132 | { |
| 133 | int i; |
| 134 | enum chipbustype ret = BUS_NONE; |
| 135 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 136 | for (i = 0; i < registered_master_count; i++) |
| 137 | ret |= registered_masters[i].buses_supported; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 138 | |
| 139 | return ret; |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 140 | } |