blob: e1821679797d646231b7ff824ab163bcba67d57e [file] [log] [blame]
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009,2010 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 * 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
21#include <stdint.h>
22#include <string.h>
23#include <stdlib.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000024#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000025#if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000026#include <unistd.h>
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000027#include <fcntl.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000028#endif
29#if !defined (__DJGPP__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000030#include <errno.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000031#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000032#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000033#include "hwaccess.h"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000034
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000035#if defined(__i386__) || defined(__x86_64__)
36
37/* sync primitive is not needed because x86 uses uncached accesses
38 * which have a strongly ordered memory model.
39 */
40static inline void sync_primitive(void)
41{
42}
43
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +000044#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000045int io_fd;
46#endif
47
48void get_io_perms(void)
49{
Patrick Georgia9095a92010-09-30 17:03:32 +000050#if defined(__DJGPP__) || defined(__LIBPAYLOAD__)
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000051 /* We have full permissions by default. */
52 return;
53#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000054#if defined (__sun) && (defined(__i386) || defined(__amd64))
55 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +000056#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000057 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000058#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000059 if (iopl(3) != 0) {
60#endif
Sean Nelson316a29f2010-05-07 20:09:04 +000061 msg_perr("ERROR: Could not get I/O privileges (%s).\n"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000062 "You need to be root.\n", strerror(errno));
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +000063#if defined (__OpenBSD__)
64 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
65 "and reboot, or reboot into \n");
66 msg_perr("single user mode.\n");
67#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000068 exit(1);
69 }
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000070#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000071}
72
73void release_io_perms(void)
74{
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +000075#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000076 close(io_fd);
77#endif
78}
79
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000080#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
81
82static inline void sync_primitive(void)
83{
84 /* Prevent reordering and/or merging of reads/writes to hardware.
85 * Such reordering and/or merging would break device accesses which
86 * depend on the exact access order.
87 */
88 asm("eieio" : : : "memory");
89}
90
91/* PCI port I/O is not yet implemented on PowerPC. */
92void get_io_perms(void)
93{
94}
95
96/* PCI port I/O is not yet implemented on PowerPC. */
97void release_io_perms(void)
98{
99}
100
101#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
102
103/* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses
104 * in mode 2 which has a strongly ordered memory model.
105 */
106static inline void sync_primitive(void)
107{
108}
109
110/* PCI port I/O is not yet implemented on MIPS. */
111void get_io_perms(void)
112{
113}
114
115/* PCI port I/O is not yet implemented on MIPS. */
116void release_io_perms(void)
117{
118}
119
David Hendricksb286da72012-02-13 00:35:35 +0000120#elif defined (__arm__)
121
122static inline void sync_primitive(void)
123{
124}
125
126void get_io_perms(void)
127{
128}
129
130void release_io_perms(void)
131{
132}
133
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000134#else
135
136#error Unknown architecture
137
138#endif
139
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000140void mmio_writeb(uint8_t val, void *addr)
141{
142 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000143 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000144}
145
146void mmio_writew(uint16_t val, void *addr)
147{
148 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000149 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000150}
151
152void mmio_writel(uint32_t val, void *addr)
153{
154 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000155 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000156}
157
158uint8_t mmio_readb(void *addr)
159{
160 return *(volatile uint8_t *) addr;
161}
162
163uint16_t mmio_readw(void *addr)
164{
165 return *(volatile uint16_t *) addr;
166}
167
168uint32_t mmio_readl(void *addr)
169{
170 return *(volatile uint32_t *) addr;
171}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000172
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000173void mmio_readn(void *addr, uint8_t *buf, size_t len)
174{
175 memcpy(buf, addr, len);
176 return;
177}
178
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000179void mmio_le_writeb(uint8_t val, void *addr)
180{
181 mmio_writeb(cpu_to_le8(val), addr);
182}
183
184void mmio_le_writew(uint16_t val, void *addr)
185{
186 mmio_writew(cpu_to_le16(val), addr);
187}
188
189void mmio_le_writel(uint32_t val, void *addr)
190{
191 mmio_writel(cpu_to_le32(val), addr);
192}
193
194uint8_t mmio_le_readb(void *addr)
195{
196 return le_to_cpu8(mmio_readb(addr));
197}
198
199uint16_t mmio_le_readw(void *addr)
200{
201 return le_to_cpu16(mmio_readw(addr));
202}
203
204uint32_t mmio_le_readl(void *addr)
205{
206 return le_to_cpu32(mmio_readl(addr));
207}
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000208
209enum mmio_write_type {
210 mmio_write_type_b,
211 mmio_write_type_w,
212 mmio_write_type_l,
213};
214
215struct undo_mmio_write_data {
216 void *addr;
217 int reg;
218 enum mmio_write_type type;
219 union {
220 uint8_t bdata;
221 uint16_t wdata;
222 uint32_t ldata;
223 };
224};
225
David Hendricks8bb20212011-06-14 01:35:36 +0000226int undo_mmio_write(void *p)
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000227{
228 struct undo_mmio_write_data *data = p;
229 msg_pdbg("Restoring MMIO space at %p\n", data->addr);
230 switch (data->type) {
231 case mmio_write_type_b:
232 mmio_writeb(data->bdata, data->addr);
233 break;
234 case mmio_write_type_w:
235 mmio_writew(data->wdata, data->addr);
236 break;
237 case mmio_write_type_l:
238 mmio_writel(data->ldata, data->addr);
239 break;
240 }
241 /* p was allocated in register_undo_mmio_write. */
242 free(p);
David Hendricks8bb20212011-06-14 01:35:36 +0000243 return 0;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000244}
245
246#define register_undo_mmio_write(a, c) \
247{ \
248 struct undo_mmio_write_data *undo_mmio_write_data; \
249 undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
Stefan Tauner269de352011-07-12 22:35:21 +0000250 if (!undo_mmio_write_data) { \
251 msg_gerr("Out of memory!\n"); \
252 exit(1); \
253 } \
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000254 undo_mmio_write_data->addr = a; \
255 undo_mmio_write_data->type = mmio_write_type_##c; \
256 undo_mmio_write_data->c##data = mmio_read##c(a); \
257 register_shutdown(undo_mmio_write, undo_mmio_write_data); \
258}
259
260#define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b)
261#define register_undo_mmio_writew(a) register_undo_mmio_write(a, w)
262#define register_undo_mmio_writel(a) register_undo_mmio_write(a, l)
263
264void rmmio_writeb(uint8_t val, void *addr)
265{
266 register_undo_mmio_writeb(addr);
267 mmio_writeb(val, addr);
268}
269
270void rmmio_writew(uint16_t val, void *addr)
271{
272 register_undo_mmio_writew(addr);
273 mmio_writew(val, addr);
274}
275
276void rmmio_writel(uint32_t val, void *addr)
277{
278 register_undo_mmio_writel(addr);
279 mmio_writel(val, addr);
280}
281
282void rmmio_le_writeb(uint8_t val, void *addr)
283{
284 register_undo_mmio_writeb(addr);
285 mmio_le_writeb(val, addr);
286}
287
288void rmmio_le_writew(uint16_t val, void *addr)
289{
290 register_undo_mmio_writew(addr);
291 mmio_le_writew(val, addr);
292}
293
294void rmmio_le_writel(uint32_t val, void *addr)
295{
296 register_undo_mmio_writel(addr);
297 mmio_le_writel(val, addr);
298}
299
300void rmmio_valb(void *addr)
301{
302 register_undo_mmio_writeb(addr);
303}
304
305void rmmio_valw(void *addr)
306{
307 register_undo_mmio_writew(addr);
308}
309
310void rmmio_vall(void *addr)
311{
312 register_undo_mmio_writel(addr);
313}