FreeNOS
ARMIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBARCH_ARMIO_H
19 #define __LIBARCH_ARMIO_H
20 
21 #include <Types.h>
22 #include <IO.h>
23 #include "ARMCore.h"
24 
39 class ARMIO : public IO
40 {
41  public:
42 
46  inline void write(u32 reg, u32 data)
47  {
48  dmb();
49  u32 addr = reg + m_base;
50  asm volatile("str %[data], [%[reg]]"
51  : : [reg]"r"(addr), [data]"r"(data));
52  dmb();
53  }
54 
62  inline u32 read(u32 reg) const
63  {
64  dmb();
65  u32 addr = reg + m_base;
66  u32 data;
67  asm volatile("ldr %[data], [%[reg]]"
68  : [data]"=r"(data) : [reg]"r"(addr));
69  dmb();
70  return data;
71  }
72 
80  inline void read(Address addr, Size count, void *buf) const
81  {
82  for (Size i = 0; i < count; i+= sizeof(u32))
83  {
84  *(u32 *)(((u8 *)buf) + i) = read(addr + i);
85  }
86  }
87 
95  inline void write(Address addr, Size count, const void *buf)
96  {
97  for (Size i = 0; i < count; i+= sizeof(u32))
98  {
99  write(addr + i, *(u32 *) (((u8 *)buf) + i));
100  }
101  }
102 
109  inline void set(Address addr, u32 data)
110  {
111  volatile u32 current = read(addr);
112  current |= data;
113  write(addr, current);
114  }
115 
122  inline void unset(Address addr, u32 data)
123  {
124  volatile u32 current = read(addr);
125  current &= ~(data);
126  write(addr, current);
127  }
128 };
129 
130 namespace Arch
131 {
132  typedef ARMIO IO;
133 };
134 
141 #endif /* __LIBARCH_ARMIO_H */
ARMIO::read
void read(Address addr, Size count, void *buf) const
Read a number of 32-bit values.
Definition: ARMIO.h:80
Types.h
ARMIO
Input/Output operations specific to the ARM architecture.
Definition: ARMIO.h:39
IO::m_base
Address m_base
memory I/O base offset is added to each I/O address.
Definition: IO.h:90
ARMIO::write
void write(u32 reg, u32 data)
write to memory mapped I/O register
Definition: ARMIO.h:46
Arch
Definition: ARMCacheV6.h:103
ARMCore.h
Address
unsigned long Address
A memory address.
Definition: Types.h:131
ARMIO::unset
void unset(Address addr, u32 data)
Unset bits in memory mapped register.
Definition: ARMIO.h:122
Arch::IO
ARMIO IO
Definition: ARMIO.h:132
IO
Generic I/O functions.
Definition: IO.h:35
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
ARMIO::read
u32 read(u32 reg) const
read from memory mapped I/O register
Definition: ARMIO.h:62
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
ARMIO::set
void set(Address addr, u32 data)
Set bits in memory mapped register.
Definition: ARMIO.h:109
ARMIO::write
void write(Address addr, Size count, const void *buf)
Write a number of 32-bit values.
Definition: ARMIO.h:95
IO.h
dmb
void dmb()
Data Memory Barrier.
Definition: ARMCore.h:180