blob: 7f7b805a31bfc00d16babc325eb1e5cb40e21951 [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>
24#include <fcntl.h>
25#include <sys/types.h>
26#include <errno.h>
27#include "flash.h"
28
29#if defined(__FreeBSD__) || defined(__DragonFly__)
30int io_fd;
31#endif
32
33void get_io_perms(void)
34{
35#if defined (__sun) && (defined(__i386) || defined(__amd64))
36 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
37#elif defined(__FreeBSD__) || defined (__DragonFly__)
38 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
39#else
40 if (iopl(3) != 0) {
41#endif
42 fprintf(stderr, "ERROR: Could not get I/O privileges (%s).\n"
43 "You need to be root.\n", strerror(errno));
44 exit(1);
45 }
46}
47
48void release_io_perms(void)
49{
50#if defined(__FreeBSD__) || defined(__DragonFly__)
51 close(io_fd);
52#endif
53}
54
55void mmio_writeb(uint8_t val, void *addr)
56{
57 *(volatile uint8_t *) addr = val;
58}
59
60void mmio_writew(uint16_t val, void *addr)
61{
62 *(volatile uint16_t *) addr = val;
63}
64
65void mmio_writel(uint32_t val, void *addr)
66{
67 *(volatile uint32_t *) addr = val;
68}
69
70uint8_t mmio_readb(void *addr)
71{
72 return *(volatile uint8_t *) addr;
73}
74
75uint16_t mmio_readw(void *addr)
76{
77 return *(volatile uint16_t *) addr;
78}
79
80uint32_t mmio_readl(void *addr)
81{
82 return *(volatile uint32_t *) addr;
83}