blob: 3a61e609812bda36a20ee43ddcb1903355824d2c [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));
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +000060#if defined (__OpenBSD__)
61 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
62 "and reboot, or reboot into \n");
63 msg_perr("single user mode.\n");
64#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000065 exit(1);
66 }
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000067#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000068}
69
70void release_io_perms(void)
71{
72#if defined(__FreeBSD__) || defined(__DragonFly__)
73 close(io_fd);
74#endif
75}
76
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000077#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
78
79static inline void sync_primitive(void)
80{
81 /* Prevent reordering and/or merging of reads/writes to hardware.
82 * Such reordering and/or merging would break device accesses which
83 * depend on the exact access order.
84 */
85 asm("eieio" : : : "memory");
86}
87
88/* PCI port I/O is not yet implemented on PowerPC. */
89void get_io_perms(void)
90{
91}
92
93/* PCI port I/O is not yet implemented on PowerPC. */
94void release_io_perms(void)
95{
96}
97
98#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
99
100/* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses
101 * in mode 2 which has a strongly ordered memory model.
102 */
103static inline void sync_primitive(void)
104{
105}
106
107/* PCI port I/O is not yet implemented on MIPS. */
108void get_io_perms(void)
109{
110}
111
112/* PCI port I/O is not yet implemented on MIPS. */
113void release_io_perms(void)
114{
115}
116
117#else
118
119#error Unknown architecture
120
121#endif
122
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000123void mmio_writeb(uint8_t val, void *addr)
124{
125 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000126 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000127}
128
129void mmio_writew(uint16_t val, void *addr)
130{
131 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000132 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000133}
134
135void mmio_writel(uint32_t val, void *addr)
136{
137 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000138 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000139}
140
141uint8_t mmio_readb(void *addr)
142{
143 return *(volatile uint8_t *) addr;
144}
145
146uint16_t mmio_readw(void *addr)
147{
148 return *(volatile uint16_t *) addr;
149}
150
151uint32_t mmio_readl(void *addr)
152{
153 return *(volatile uint32_t *) addr;
154}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000155
156void mmio_le_writeb(uint8_t val, void *addr)
157{
158 mmio_writeb(cpu_to_le8(val), addr);
159}
160
161void mmio_le_writew(uint16_t val, void *addr)
162{
163 mmio_writew(cpu_to_le16(val), addr);
164}
165
166void mmio_le_writel(uint32_t val, void *addr)
167{
168 mmio_writel(cpu_to_le32(val), addr);
169}
170
171uint8_t mmio_le_readb(void *addr)
172{
173 return le_to_cpu8(mmio_readb(addr));
174}
175
176uint16_t mmio_le_readw(void *addr)
177{
178 return le_to_cpu16(mmio_readw(addr));
179}
180
181uint32_t mmio_le_readl(void *addr)
182{
183 return le_to_cpu32(mmio_readl(addr));
184}