blob: 44d787146d6f55174ead14a55a1cedf9067c4950 [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
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000029#if defined(__i386__) || defined(__x86_64__)
30
31/* sync primitive is not needed because x86 uses uncached accesses
32 * which have a strongly ordered memory model.
33 */
34static inline void sync_primitive(void)
35{
36}
37
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000038#if defined(__FreeBSD__) || defined(__DragonFly__)
39int io_fd;
40#endif
41
42void get_io_perms(void)
43{
44#if defined (__sun) && (defined(__i386) || defined(__amd64))
45 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
46#elif defined(__FreeBSD__) || defined (__DragonFly__)
47 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000048#elif __DJGPP__
49 if (0) {
50#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000051 if (iopl(3) != 0) {
52#endif
Sean Nelson316a29f2010-05-07 20:09:04 +000053 msg_perr("ERROR: Could not get I/O privileges (%s).\n"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000054 "You need to be root.\n", strerror(errno));
55 exit(1);
56 }
57}
58
59void release_io_perms(void)
60{
61#if defined(__FreeBSD__) || defined(__DragonFly__)
62 close(io_fd);
63#endif
64}
65
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000066#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
67
68static inline void sync_primitive(void)
69{
70 /* Prevent reordering and/or merging of reads/writes to hardware.
71 * Such reordering and/or merging would break device accesses which
72 * depend on the exact access order.
73 */
74 asm("eieio" : : : "memory");
75}
76
77/* PCI port I/O is not yet implemented on PowerPC. */
78void get_io_perms(void)
79{
80}
81
82/* PCI port I/O is not yet implemented on PowerPC. */
83void release_io_perms(void)
84{
85}
86
87#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
88
89/* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses
90 * in mode 2 which has a strongly ordered memory model.
91 */
92static inline void sync_primitive(void)
93{
94}
95
96/* PCI port I/O is not yet implemented on MIPS. */
97void get_io_perms(void)
98{
99}
100
101/* PCI port I/O is not yet implemented on MIPS. */
102void release_io_perms(void)
103{
104}
105
106#else
107
108#error Unknown architecture
109
110#endif
111
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000112void mmio_writeb(uint8_t val, void *addr)
113{
114 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000115 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000116}
117
118void mmio_writew(uint16_t val, void *addr)
119{
120 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000121 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000122}
123
124void mmio_writel(uint32_t val, void *addr)
125{
126 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000127 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000128}
129
130uint8_t mmio_readb(void *addr)
131{
132 return *(volatile uint8_t *) addr;
133}
134
135uint16_t mmio_readw(void *addr)
136{
137 return *(volatile uint16_t *) addr;
138}
139
140uint32_t mmio_readl(void *addr)
141{
142 return *(volatile uint32_t *) addr;
143}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000144
145void mmio_le_writeb(uint8_t val, void *addr)
146{
147 mmio_writeb(cpu_to_le8(val), addr);
148}
149
150void mmio_le_writew(uint16_t val, void *addr)
151{
152 mmio_writew(cpu_to_le16(val), addr);
153}
154
155void mmio_le_writel(uint32_t val, void *addr)
156{
157 mmio_writel(cpu_to_le32(val), addr);
158}
159
160uint8_t mmio_le_readb(void *addr)
161{
162 return le_to_cpu8(mmio_readb(addr));
163}
164
165uint16_t mmio_le_readw(void *addr)
166{
167 return le_to_cpu16(mmio_readw(addr));
168}
169
170uint32_t mmio_le_readl(void *addr)
171{
172 return le_to_cpu32(mmio_readl(addr));
173}