blob: 85e13edcae6447515b3f5b27c15cc44f738bfe17 [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>
25#if !defined (__DJGPP__)
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000026#include <unistd.h>
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000027#include <fcntl.h>
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000028#include <errno.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000029#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000030#include "flash.h"
31
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000032#if defined(__i386__) || defined(__x86_64__)
33
34/* sync primitive is not needed because x86 uses uncached accesses
35 * which have a strongly ordered memory model.
36 */
37static inline void sync_primitive(void)
38{
39}
40
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000041#if defined(__FreeBSD__) || defined(__DragonFly__)
42int io_fd;
43#endif
44
45void get_io_perms(void)
46{
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000047#if defined(__DJGPP__)
48 /* We have full permissions by default. */
49 return;
50#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000051#if defined (__sun) && (defined(__i386) || defined(__amd64))
52 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
53#elif defined(__FreeBSD__) || defined (__DragonFly__)
54 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000055#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000056 if (iopl(3) != 0) {
57#endif
Sean Nelson316a29f2010-05-07 20:09:04 +000058 msg_perr("ERROR: Could not get I/O privileges (%s).\n"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000059 "You need to be root.\n", strerror(errno));
60 exit(1);
61 }
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000062#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000063}
64
65void release_io_perms(void)
66{
67#if defined(__FreeBSD__) || defined(__DragonFly__)
68 close(io_fd);
69#endif
70}
71
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000072#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
73
74static inline void sync_primitive(void)
75{
76 /* Prevent reordering and/or merging of reads/writes to hardware.
77 * Such reordering and/or merging would break device accesses which
78 * depend on the exact access order.
79 */
80 asm("eieio" : : : "memory");
81}
82
83/* PCI port I/O is not yet implemented on PowerPC. */
84void get_io_perms(void)
85{
86}
87
88/* PCI port I/O is not yet implemented on PowerPC. */
89void release_io_perms(void)
90{
91}
92
93#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
94
95/* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses
96 * in mode 2 which has a strongly ordered memory model.
97 */
98static inline void sync_primitive(void)
99{
100}
101
102/* PCI port I/O is not yet implemented on MIPS. */
103void get_io_perms(void)
104{
105}
106
107/* PCI port I/O is not yet implemented on MIPS. */
108void release_io_perms(void)
109{
110}
111
112#else
113
114#error Unknown architecture
115
116#endif
117
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000118void mmio_writeb(uint8_t val, void *addr)
119{
120 *(volatile uint8_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_writew(uint16_t val, void *addr)
125{
126 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000127 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000128}
129
130void mmio_writel(uint32_t val, void *addr)
131{
132 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000133 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000134}
135
136uint8_t mmio_readb(void *addr)
137{
138 return *(volatile uint8_t *) addr;
139}
140
141uint16_t mmio_readw(void *addr)
142{
143 return *(volatile uint16_t *) addr;
144}
145
146uint32_t mmio_readl(void *addr)
147{
148 return *(volatile uint32_t *) addr;
149}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000150
151void mmio_le_writeb(uint8_t val, void *addr)
152{
153 mmio_writeb(cpu_to_le8(val), addr);
154}
155
156void mmio_le_writew(uint16_t val, void *addr)
157{
158 mmio_writew(cpu_to_le16(val), addr);
159}
160
161void mmio_le_writel(uint32_t val, void *addr)
162{
163 mmio_writel(cpu_to_le32(val), addr);
164}
165
166uint8_t mmio_le_readb(void *addr)
167{
168 return le_to_cpu8(mmio_readb(addr));
169}
170
171uint16_t mmio_le_readw(void *addr)
172{
173 return le_to_cpu16(mmio_readw(addr));
174}
175
176uint32_t mmio_le_readl(void *addr)
177{
178 return le_to_cpu32(mmio_readl(addr));
179}