blob: d7f19684ab613f4887a7e779e42dd810e4afaa29 [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,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000084 const enum chipbustype buses)
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000085{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000086 struct registered_master rmst;
87 if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel ||
88 !mst->chip_writen || !mst->chip_readb || !mst->chip_readw ||
89 !mst->chip_readl || !mst->chip_readn) {
Nico Huberac90af62022-12-18 00:22:47 +000090 msg_perr("%s called with incomplete master definition.\n"
91 "Please report a bug at flashrom-stable@flashrom.org\n",
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000092 __func__);
93 return ERROR_FLASHROM_BUG;
94 }
95
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000096 rmst.buses_supported = buses;
97 rmst.par = *mst;
98 return register_master(&rmst);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000099}
100
101/* The limit of 4 is totally arbitrary. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000102#define MASTERS_MAX 4
103struct registered_master registered_masters[MASTERS_MAX];
104int registered_master_count = 0;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000105
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000106/* This function copies the struct registered_master parameter. */
Stefan Tauner5c316f92015-02-08 21:57:52 +0000107int register_master(const struct registered_master *mst)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000108{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000109 if (registered_master_count >= MASTERS_MAX) {
110 msg_perr("Tried to register more than %i master "
111 "interfaces.\n", MASTERS_MAX);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000112 return ERROR_FLASHROM_LIMIT;
113 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000114 registered_masters[registered_master_count] = *mst;
115 registered_master_count++;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000116
117 return 0;
118}
119
120enum chipbustype get_buses_supported(void)
121{
122 int i;
123 enum chipbustype ret = BUS_NONE;
124
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000125 for (i = 0; i < registered_master_count; i++)
126 ret |= registered_masters[i].buses_supported;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000127
128 return ret;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000129}