blob: 36ea6f4ec58ac1a7e6e38ed79ad6db7995f8dbbe [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) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000039#elif __DJGPP__
40 if (0) {
41#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000042 if (iopl(3) != 0) {
43#endif
44 fprintf(stderr, "ERROR: Could not get I/O privileges (%s).\n"
45 "You need to be root.\n", strerror(errno));
46 exit(1);
47 }
48}
49
50void release_io_perms(void)
51{
52#if defined(__FreeBSD__) || defined(__DragonFly__)
53 close(io_fd);
54#endif
55}
56
57void mmio_writeb(uint8_t val, void *addr)
58{
59 *(volatile uint8_t *) addr = val;
60}
61
62void mmio_writew(uint16_t val, void *addr)
63{
64 *(volatile uint16_t *) addr = val;
65}
66
67void mmio_writel(uint32_t val, void *addr)
68{
69 *(volatile uint32_t *) addr = val;
70}
71
72uint8_t mmio_readb(void *addr)
73{
74 return *(volatile uint8_t *) addr;
75}
76
77uint16_t mmio_readw(void *addr)
78{
79 return *(volatile uint16_t *) addr;
80}
81
82uint32_t mmio_readl(void *addr)
83{
84 return *(volatile uint32_t *) addr;
85}