1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <[email protected]>
6 * Copyright (C) 2005-2008 coresystems GmbH
7 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
8 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <[email protected]> for secunet)
10 * Copyright (C) 2009,2010,2011 Carl-Daniel Hailfinger
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 */
21
22 #include "flash.h"
23 #include "programmer.h"
24
chip_writeb(const struct flashctx * flash,uint8_t val,chipaddr addr)25 void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
26 {
27 flash->mst->par.chip_writeb(flash, val, addr);
28 }
29
30 /* Little-endian fallback for drivers not supporting 16 bit accesses */
fallback_chip_writew(const struct flashctx * flash,uint16_t val,chipaddr addr)31 static void fallback_chip_writew(const struct flashctx *flash, uint16_t val,
32 chipaddr addr)
33 {
34 chip_writeb(flash, val & 0xff, addr);
35 chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
36 }
37
chip_writew(const struct flashctx * flash,uint16_t val,chipaddr addr)38 void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
39 {
40 if (flash->mst->par.chip_writew)
41 flash->mst->par.chip_writew(flash, val, addr);
42 fallback_chip_writew(flash, val, addr);
43 }
44
45 /* Little-endian fallback for drivers not supporting 32 bit accesses */
fallback_chip_writel(const struct flashctx * flash,uint32_t val,chipaddr addr)46 static void fallback_chip_writel(const struct flashctx *flash, uint32_t val,
47 chipaddr addr)
48 {
49 chip_writew(flash, val & 0xffff, addr);
50 chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
51 }
52
chip_writel(const struct flashctx * flash,uint32_t val,chipaddr addr)53 void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
54 {
55 if (flash->mst->par.chip_writel)
56 flash->mst->par.chip_writel(flash, val, addr);
57 fallback_chip_writel(flash, val, addr);
58 }
59
fallback_chip_writen(const struct flashctx * flash,const uint8_t * buf,chipaddr addr,size_t len)60 static void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
61 {
62 size_t i;
63 for (i = 0; i < len; i++)
64 chip_writeb(flash, buf[i], addr + i);
65 return;
66 }
67
chip_writen(const struct flashctx * flash,const uint8_t * buf,chipaddr addr,size_t len)68 void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
69 {
70 if (flash->mst->par.chip_writen)
71 flash->mst->par.chip_writen(flash, buf, addr, len);
72 fallback_chip_writen(flash, buf, addr, len);
73 }
74
chip_readb(const struct flashctx * flash,const chipaddr addr)75 uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
76 {
77 return flash->mst->par.chip_readb(flash, addr);
78 }
79
80 /* Little-endian fallback for drivers not supporting 16 bit accesses */
fallback_chip_readw(const struct flashctx * flash,const chipaddr addr)81 static uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
82 {
83 uint16_t val;
84 val = chip_readb(flash, addr);
85 val |= chip_readb(flash, addr + 1) << 8;
86 return val;
87 }
88
chip_readw(const struct flashctx * flash,const chipaddr addr)89 uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
90 {
91 if (flash->mst->par.chip_readw)
92 return flash->mst->par.chip_readw(flash, addr);
93 return fallback_chip_readw(flash, addr);
94 }
95
96 /* Little-endian fallback for drivers not supporting 32 bit accesses */
fallback_chip_readl(const struct flashctx * flash,const chipaddr addr)97 static uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
98 {
99 uint32_t val;
100 val = chip_readw(flash, addr);
101 val |= chip_readw(flash, addr + 2) << 16;
102 return val;
103 }
104
chip_readl(const struct flashctx * flash,const chipaddr addr)105 uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
106 {
107 if (flash->mst->par.chip_readl)
108 return flash->mst->par.chip_readl(flash, addr);
109 return fallback_chip_readl(flash, addr);
110 }
111
fallback_chip_readn(const struct flashctx * flash,uint8_t * buf,chipaddr addr,size_t len)112 static void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf,
113 chipaddr addr, size_t len)
114 {
115 size_t i;
116 for (i = 0; i < len; i++)
117 buf[i] = chip_readb(flash, addr + i);
118 return;
119 }
120
chip_readn(const struct flashctx * flash,uint8_t * buf,chipaddr addr,size_t len)121 void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
122 size_t len)
123 {
124 if (flash->mst->par.chip_readn)
125 flash->mst->par.chip_readn(flash, buf, addr, len);
126 fallback_chip_readn(flash, buf, addr, len);
127 }
128
register_par_master(const struct par_master * mst,const enum chipbustype buses,void * data)129 int register_par_master(const struct par_master *mst,
130 const enum chipbustype buses,
131 void *data)
132 {
133 struct registered_master rmst = {0};
134
135 if (mst->shutdown) {
136 if (register_shutdown(mst->shutdown, data)) {
137 mst->shutdown(data); /* cleanup */
138 return 1;
139 }
140 }
141
142 /* Bus masters supporting FWH/LPC cannot use chip physical maps, distinct
143 * mappings are needed to support chips with FEATURE_REGISTERMAP
144 */
145 if ((buses & (BUS_FWH | BUS_LPC)) && !mst->map_flash_region) {
146 msg_perr("%s called with incomplete master definition. "
147 "FWH/LPC masters must provide memory mappings. "
148 "Please report a bug at [email protected]\n",
149 __func__);
150 return ERROR_FLASHROM_BUG;
151 }
152
153 if (!mst->chip_writeb || !mst->chip_readb) {
154 msg_perr("%s called with incomplete master definition. "
155 "Please report a bug at [email protected]\n",
156 __func__);
157 return ERROR_FLASHROM_BUG;
158 }
159
160 rmst.buses_supported = buses;
161 rmst.par = *mst;
162 if (data)
163 rmst.par.data = data;
164 return register_master(&rmst);
165 }
166