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