FreeNOS
IntelIO.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_INTELIO_H
19 #define __LIBARCH_INTELIO_H
20 
21 #include <Types.h>
22 #include <IO.h>
23 
38 class IntelIO : public IO
39 {
40  public:
41 
46  : IO()
47  , m_portBase(0)
48  {
49  }
50 
56  void setPortBase(const u16 base)
57  {
58  m_portBase = base;
59  }
60 
68  inline u8 inb(u16 port) const
69  {
70  u8 b;
71  port += m_portBase;
72  asm volatile ("inb %%dx, %%al" : "=a" (b) : "d" (port));
73  return b;
74  }
75 
83  inline u16 inw(u16 port) const
84  {
85  u16 w;
86  port += m_portBase;
87  asm volatile ("inw %%dx, %%ax" : "=a" (w) : "d" (port));
88  return w;
89  }
90 
97  inline void outb(u16 port, u8 byte)
98  {
99  port += m_portBase;
100  asm volatile ("outb %%al,%%dx"::"a" (byte),"d" (port));
101  }
102 
109  inline void outw(u16 port, u16 word)
110  {
111  port += m_portBase;
112  asm volatile ("outw %%ax,%%dx"::"a" (word),"d" (port));
113  }
114 
121  inline void outl(u16 port, u32 l)
122  {
123  port += m_portBase;
124  asm volatile ("outl %%eax,%%dx"::"a" (l),"d" (port));
125  }
126 
134  inline u32 read(const Address addr) const
135  {
136  const volatile u32 *ptr = (const volatile u32 *)((const volatile u8 *)m_base + addr);
137  return *ptr;
138  }
139 
147  inline void read(Address addr, Size count, void *buf) const
148  {
149  for (Size i = 0; i < count; i+= sizeof(u32))
150  {
151  *(u32 *)(((u8 *)buf) + i) = read(addr + i);
152  }
153  }
154 
161  inline void write(const Address addr, const u32 data)
162  {
163  volatile u32 *ptr = (volatile u32 *)((volatile u8 *)m_base + addr);
164  *ptr = data;
165  }
166 
174  inline void write(Address addr, Size count, const void *buf)
175  {
176  for (Size i = 0; i < count; i+= sizeof(u32))
177  {
178  write(addr + i, *(u32 *) (((u8 *)buf) + i));
179  }
180  }
181 
188  inline void set(Address addr, u32 data)
189  {
190  u32 current = read(addr);
191  current |= data;
192  write(addr, current);
193  }
194 
201  inline void unset(Address addr, u32 data)
202  {
203  u32 current = read(addr);
204  current &= ~(data);
205  write(addr, current);
206  }
207 
208  private:
209 
212 };
213 
214 namespace Arch
215 {
216  typedef IntelIO IO;
217 };
218 
225 #endif /* __LIBARCH_INTELIO_H */
IntelIO::read
void read(Address addr, Size count, void *buf) const
Read a number of 32-bit values.
Definition: IntelIO.h:147
Types.h
IntelIO::setPortBase
void setPortBase(const u16 base)
Set port I/O base address.
Definition: IntelIO.h:56
IntelIO::set
void set(Address addr, u32 data)
Set bits in memory mapped register.
Definition: IntelIO.h:188
IntelIO::m_portBase
u16 m_portBase
Port I/O base address.
Definition: IntelIO.h:211
IO::m_base
Address m_base
memory I/O base offset is added to each I/O address.
Definition: IO.h:90
Arch
Definition: ARMCacheV6.h:103
IntelIO::inb
u8 inb(u16 port) const
Read a byte from a port.
Definition: IntelIO.h:68
Address
unsigned long Address
A memory address.
Definition: Types.h:131
Arch::IO
ARMIO IO
Definition: ARMIO.h:132
IntelIO::outb
void outb(u16 port, u8 byte)
Output a byte to a port.
Definition: IntelIO.h:97
IntelIO::read
u32 read(const Address addr) const
Read memory mapped register.
Definition: IntelIO.h:134
IntelIO::inw
u16 inw(u16 port) const
Read a word from a port.
Definition: IntelIO.h:83
IO
Generic I/O functions.
Definition: IO.h:35
u16
unsigned short u16
Unsigned 16-bit number.
Definition: Types.h:56
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
IntelIO::unset
void unset(Address addr, u32 data)
Unset bits in memory mapped register.
Definition: IntelIO.h:201
IntelIO::outw
void outw(u16 port, u16 word)
Output a word to a port.
Definition: IntelIO.h:109
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
IntelIO
Intel I/O functions.
Definition: IntelIO.h:38
IntelIO::outl
void outl(u16 port, u32 l)
Output a long to a I/O port.
Definition: IntelIO.h:121
IntelIO::IntelIO
IntelIO()
Constructor.
Definition: IntelIO.h:45
IntelIO::write
void write(const Address addr, const u32 data)
Write memory mapped register.
Definition: IntelIO.h:161
IntelIO::write
void write(Address addr, Size count, const void *buf)
Write a number of 32-bit values.
Definition: IntelIO.h:174
IO.h