blob: c276a69be691025d3f2ed118dddd5dd7970e8eed [file] [log] [blame]
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00004 * Copyright (C) 2009,2010,2011 Carl-Daniel Hailfinger
Carl-Daniel Hailfingercc1802d2010-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.
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000015 */
16
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000017#include "flash.h"
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000018#include "programmer.h"
19
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000020/* Fallback map() for programmers which don't need special handling */
Stefan Tauner305e0b92013-07-17 23:46:44 +000021void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000022{
23 /* FIXME: Should return phys_addr. */
Peter Huewe73f8ec82011-01-24 19:15:51 +000024 return NULL;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000025}
26
27/* No-op/fallback unmap() for programmers which don't need special handling */
28void fallback_unmap(void *virt_addr, size_t len)
29{
30}
31
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000032/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000033void fallback_chip_writew(const struct flashctx *flash, uint16_t val,
34 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000035{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000036 chip_writeb(flash, val & 0xff, addr);
37 chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000038}
39
40/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000041uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000042{
43 uint16_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000044 val = chip_readb(flash, addr);
45 val |= chip_readb(flash, addr + 1) << 8;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000046 return val;
47}
48
49/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000050void fallback_chip_writel(const struct flashctx *flash, uint32_t val,
51 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000052{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000053 chip_writew(flash, val & 0xffff, addr);
54 chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000055}
56
57/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000059{
60 uint32_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000061 val = chip_readw(flash, addr);
62 val |= chip_readw(flash, addr + 2) << 16;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000063 return val;
64}
65
Mark Marshallf20b7be2014-05-09 21:16:21 +000066void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000067{
68 size_t i;
69 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000070 chip_writeb(flash, buf[i], addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000071 return;
72}
73
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000074void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf,
75 chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000076{
77 size_t i;
78 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000079 buf[i] = chip_readb(flash, addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000080 return;
81}
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000082
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000083int register_par_master(const struct par_master *mst,
Anastasia Klimchuk6a5db262021-05-21 09:40:58 +100084 const enum chipbustype buses,
85 void *data)
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000086{
Edward O'Callaghan4eef6512021-02-03 11:19:41 +110087 struct registered_master rmst = {0};
88
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000089 if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel ||
90 !mst->chip_writen || !mst->chip_readb || !mst->chip_readw ||
91 !mst->chip_readl || !mst->chip_readn) {
92 msg_perr("%s called with incomplete master definition. "
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000093 "Please report a bug at flashrom@flashrom.org\n",
94 __func__);
95 return ERROR_FLASHROM_BUG;
96 }
97
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000098 rmst.buses_supported = buses;
99 rmst.par = *mst;
Anastasia Klimchuk6a5db262021-05-21 09:40:58 +1000100 if (data)
101 rmst.par.data = data;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000102 return register_master(&rmst);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000103}
104
105/* The limit of 4 is totally arbitrary. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000106#define MASTERS_MAX 4
107struct registered_master registered_masters[MASTERS_MAX];
108int registered_master_count = 0;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000109
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000110/* This function copies the struct registered_master parameter. */
Stefan Tauner5c316f92015-02-08 21:57:52 +0000111int register_master(const struct registered_master *mst)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000112{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000113 if (registered_master_count >= MASTERS_MAX) {
114 msg_perr("Tried to register more than %i master "
115 "interfaces.\n", MASTERS_MAX);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000116 return ERROR_FLASHROM_LIMIT;
117 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000118 registered_masters[registered_master_count] = *mst;
119 registered_master_count++;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000120
121 return 0;
122}
123
124enum chipbustype get_buses_supported(void)
125{
126 int i;
127 enum chipbustype ret = BUS_NONE;
128
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000129 for (i = 0; i < registered_master_count; i++)
130 ret |= registered_masters[i].buses_supported;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000131
132 return ret;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000133}