blob: 5c841effcbee02867c3016efe628c0329eae5c38 [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/* No-op shutdown() for programmers which don't need special handling */
21int noop_shutdown(void)
22{
23 return 0;
24}
25
26/* Fallback map() for programmers which don't need special handling */
Stefan Tauner305e0b92013-07-17 23:46:44 +000027void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000028{
29 /* FIXME: Should return phys_addr. */
Peter Huewe73f8ec82011-01-24 19:15:51 +000030 return NULL;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000031}
32
33/* No-op/fallback unmap() for programmers which don't need special handling */
34void fallback_unmap(void *virt_addr, size_t len)
35{
36}
37
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000038/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000039void fallback_chip_writew(const struct flashctx *flash, uint16_t val,
40 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000041{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000042 chip_writeb(flash, val & 0xff, addr);
43 chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000044}
45
46/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000047uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000048{
49 uint16_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000050 val = chip_readb(flash, addr);
51 val |= chip_readb(flash, addr + 1) << 8;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000052 return val;
53}
54
55/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000056void fallback_chip_writel(const struct flashctx *flash, uint32_t val,
57 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000058{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000059 chip_writew(flash, val & 0xffff, addr);
60 chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000061}
62
63/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000064uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000065{
66 uint32_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000067 val = chip_readw(flash, addr);
68 val |= chip_readw(flash, addr + 2) << 16;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000069 return val;
70}
71
Mark Marshallf20b7be2014-05-09 21:16:21 +000072void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000073{
74 size_t i;
75 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000076 chip_writeb(flash, buf[i], addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000077 return;
78}
79
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000080void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf,
81 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 buf[i] = chip_readb(flash, addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000086 return;
87}
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000088
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000089int register_par_master(const struct par_master *mst,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000090 const enum chipbustype buses)
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000091{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000092 struct registered_master rmst;
93 if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel ||
94 !mst->chip_writen || !mst->chip_readb || !mst->chip_readw ||
95 !mst->chip_readl || !mst->chip_readn) {
96 msg_perr("%s called with incomplete master definition. "
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000097 "Please report a bug at flashrom@flashrom.org\n",
98 __func__);
99 return ERROR_FLASHROM_BUG;
100 }
101
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000102 rmst.buses_supported = buses;
103 rmst.par = *mst;
104 return register_master(&rmst);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000105}
106
107/* The limit of 4 is totally arbitrary. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000108#define MASTERS_MAX 4
109struct registered_master registered_masters[MASTERS_MAX];
110int registered_master_count = 0;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000111
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000112/* This function copies the struct registered_master parameter. */
Stefan Tauner5c316f92015-02-08 21:57:52 +0000113int register_master(const struct registered_master *mst)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000114{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000115 if (registered_master_count >= MASTERS_MAX) {
116 msg_perr("Tried to register more than %i master "
117 "interfaces.\n", MASTERS_MAX);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000118 return ERROR_FLASHROM_LIMIT;
119 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000120 registered_masters[registered_master_count] = *mst;
121 registered_master_count++;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000122
123 return 0;
124}
125
126enum chipbustype get_buses_supported(void)
127{
128 int i;
129 enum chipbustype ret = BUS_NONE;
130
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000131 for (i = 0; i < registered_master_count; i++)
132 ret |= registered_masters[i].buses_supported;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000133
134 return ret;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000135}