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