blob: 187492902aefbd14374df8976112b9304ea5ce79 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000021#include "flash.h"
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000022#include "programmer.h"
23
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000024/* No-op shutdown() for programmers which don't need special handling */
25int noop_shutdown(void)
26{
27 return 0;
28}
29
30/* Fallback map() for programmers which don't need special handling */
Stefan Tauner305e0b92013-07-17 23:46:44 +000031void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000032{
33 /* FIXME: Should return phys_addr. */
Peter Huewe73f8ec82011-01-24 19:15:51 +000034 return NULL;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000035}
36
37/* No-op/fallback unmap() for programmers which don't need special handling */
38void fallback_unmap(void *virt_addr, size_t len)
39{
40}
41
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000042/* No-op chip_writeb() for parallel style drivers not supporting writes */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000043void noop_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000044{
45}
46
47/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000048void fallback_chip_writew(const struct flashctx *flash, uint16_t val,
49 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000050{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000051 chip_writeb(flash, val & 0xff, addr);
52 chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000053}
54
55/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000056uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000057{
58 uint16_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000059 val = chip_readb(flash, addr);
60 val |= chip_readb(flash, addr + 1) << 8;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000061 return val;
62}
63
64/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000065void fallback_chip_writel(const struct flashctx *flash, uint32_t val,
66 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000067{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000068 chip_writew(flash, val & 0xffff, addr);
69 chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000070}
71
72/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000073uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000074{
75 uint32_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000076 val = chip_readw(flash, addr);
77 val |= chip_readw(flash, addr + 2) << 16;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000078 return val;
79}
80
Mark Marshallf20b7be2014-05-09 21:16:21 +000081void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000082{
83 size_t i;
84 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000085 chip_writeb(flash, buf[i], addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000086 return;
87}
88
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000089void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf,
90 chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000091{
92 size_t i;
93 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000094 buf[i] = chip_readb(flash, addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000095 return;
96}
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000097
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000098int register_par_programmer(const struct par_programmer *pgm,
99 const enum chipbustype buses)
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000100{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000101 struct registered_programmer rpgm;
102 if (!pgm->chip_writeb || !pgm->chip_writew || !pgm->chip_writel ||
103 !pgm->chip_writen || !pgm->chip_readb || !pgm->chip_readw ||
104 !pgm->chip_readl || !pgm->chip_readn) {
105 msg_perr("%s called with incomplete programmer definition. "
106 "Please report a bug at flashrom@flashrom.org\n",
107 __func__);
108 return ERROR_FLASHROM_BUG;
109 }
110
111 rpgm.buses_supported = buses;
112 rpgm.par = *pgm;
113 return register_programmer(&rpgm);
114}
115
116/* The limit of 4 is totally arbitrary. */
117#define PROGRAMMERS_MAX 4
118struct registered_programmer registered_programmers[PROGRAMMERS_MAX];
119int registered_programmer_count = 0;
120
121/* This function copies the struct registered_programmer parameter. */
122int register_programmer(struct registered_programmer *pgm)
123{
124 if (registered_programmer_count >= PROGRAMMERS_MAX) {
125 msg_perr("Tried to register more than %i programmer "
126 "interfaces.\n", PROGRAMMERS_MAX);
127 return ERROR_FLASHROM_LIMIT;
128 }
129 registered_programmers[registered_programmer_count] = *pgm;
130 registered_programmer_count++;
131
132 return 0;
133}
134
135enum chipbustype get_buses_supported(void)
136{
137 int i;
138 enum chipbustype ret = BUS_NONE;
139
140 for (i = 0; i < registered_programmer_count; i++)
141 ret |= registered_programmers[i].buses_supported;
142
143 return ret;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000144}