20 class Io_register_block
26 virtual unsigned char read8(
unsigned long reg)
const = 0;
31 virtual unsigned short read16(
unsigned long reg)
const = 0;
36 virtual unsigned int read32(
unsigned long reg)
const = 0;
46 virtual void write8(
unsigned long reg,
unsigned char value)
const = 0;
51 virtual void write16(
unsigned long reg,
unsigned short value)
const = 0;
56 virtual void write32(
unsigned long reg,
unsigned int value)
const = 0;
66 virtual unsigned long addr(
unsigned long reg)
const = 0;
73 virtual void delay()
const = 0;
75 virtual ~Io_register_block() = 0;
84 template<
typename R >
85 R read(
unsigned long reg)
const
89 case 1:
return read8(reg);
90 case 2:
return read16(reg);
91 case 4:
return read32(reg);
92 default:
static_assert(
sizeof(R) == 1 ||
sizeof(R) == 2 ||
sizeof(R) == 4,
104 template<
typename R >
105 void write(
unsigned long reg, R value)
const
109 case 1: write8(reg, value);
return;
110 case 2: write16(reg, value);
return;
111 case 4: write32(reg, value);
return;
112 default:
static_assert(
sizeof(R) == 1 ||
sizeof(R) == 2 ||
sizeof(R) == 4,
127 template<
typename R >
128 R modify(
unsigned long reg, R clear_bits, R set_bits)
const
130 R r = (read<R>(reg) & ~clear_bits) | set_bits;
141 template<
typename R >
142 R set(
unsigned long reg, R set_bits)
const
144 return modify<R>(reg, 0, set_bits);
153 template<
typename R >
154 R clear(
unsigned long reg, R clear_bits)
const
156 return modify<R>(reg, clear_bits, 0);
161 inline Io_register_block::~Io_register_block() {}
164 class Io_register_block_mmio :
public Io_register_block
167 template<
typename R >
168 R _read(
unsigned long reg)
const
169 {
return *
reinterpret_cast<volatile R *
>(_base + (reg << _shift)); }
171 template<
typename R >
172 void _write(
unsigned long reg, R val)
const
173 { *
reinterpret_cast<volatile R *
>(_base + (reg << _shift)) = val; }
176 Io_register_block_mmio(
unsigned long base,
unsigned char shift = 0)
177 : _base(base), _shift(shift)
180 unsigned long addr(
unsigned long reg)
const override
181 {
return _base + (reg << _shift); }
183 unsigned char read8(
unsigned long reg)
const override
184 {
return _read<unsigned char>(reg); }
185 unsigned short read16(
unsigned long reg)
const override
186 {
return _read<unsigned short>(reg); }
187 unsigned int read32(
unsigned long reg)
const override
188 {
return _read<unsigned int>(reg); }
190 void write8(
unsigned long reg,
unsigned char val)
const override
191 { _write(reg, val); }
192 void write16(
unsigned long reg,
unsigned short val)
const override
193 { _write(reg, val); }
194 void write32(
unsigned long reg,
unsigned int val)
const override
195 { _write(reg, val); }
197 void delay()
const override
202 unsigned char _shift;
205 template<
typename ACCESS_TYPE>
206 class Io_register_block_mmio_fixed_width :
public Io_register_block
209 template<
typename R >
210 R _read(
unsigned long reg)
const
211 {
return *
reinterpret_cast<volatile ACCESS_TYPE *
>(_base + (reg << _shift)); }
213 template<
typename R >
214 void _write(
unsigned long reg, R val)
const
215 { *
reinterpret_cast<volatile ACCESS_TYPE *
>(_base + (reg << _shift)) = val; }
218 Io_register_block_mmio_fixed_width(
unsigned long base,
unsigned char shift = 0)
219 : _base(base), _shift(shift)
222 unsigned long addr(
unsigned long reg)
const
223 {
return _base + (reg << _shift); }
225 unsigned char read8(
unsigned long reg)
const override
226 {
return _read<unsigned char>(reg); }
227 unsigned short read16(
unsigned long reg)
const override
228 {
return _read<unsigned short>(reg); }
229 unsigned int read32(
unsigned long reg)
const override
230 {
return _read<unsigned int>(reg); }
232 void write8(
unsigned long reg,
unsigned char val)
const override
233 { _write(reg, val); }
234 void write16(
unsigned long reg,
unsigned short val)
const override
235 { _write(reg, val); }
236 void write32(
unsigned long reg,
unsigned int val)
const override
237 { _write(reg, val); }
239 void delay()
const override
244 unsigned char _shift;