summaryrefslogtreecommitdiff
path: root/device.h
blob: cf84e05d7ba71c6d3cf093bb3ff456b812c8b6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
 * Copyright (C) 2010 chysun2000@gmail.com
 *
 * This file is part of nios2sim-ng.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#ifndef _DEVICE_H_
#define _DEVICE_H_

/* IRQs for all used devices shall be allocated here */
#define IRQ_TIMER		1
#define IRQ_UART		2
#define IRQ_JTAG_UART		3

#define IRQ_TO_MASK(irq)	(1UL << (irq))

struct device {
	const char *name;

	uint32_t base;
	size_t size;

	uint32_t irq_mask;

	int (*init)(struct device *dev);
	bool (*is_dev_addr)(struct device *dev, uint32_t addr);
	size_t (*read)(struct device *dev, uint32_t addr, uint32_t *data, size_t count);
	size_t (*write)(struct device *dev, uint32_t addr, uint32_t *data, size_t count);
	bool (*has_irq)(struct device *dev);
	void (*simulate)(struct device *dev);

	/* Private data */
	void *priv;
};

extern bool device_generic_is_dev_addr(struct device *dev, uint32_t addr);
extern int device_init_all(void);
extern struct device *device_get_by_addr(uint32_t addr);
extern void device_simulate_all(void);

/*
 * Device registers
 */

struct io_register {
	uint32_t addr;
	uint32_t valid_mask;
	uint32_t readonly_mask;
	uint32_t value;
};

#define REG_TO_OFF(reg)	((reg) * sizeof(uint32_t))

extern void io_register_init(struct io_register *reg, uint32_t addr,
			     uint32_t valid, uint32_t readonly, uint32_t value);
extern uint32_t io_register_read(struct io_register *reg);
extern void io_register_write(struct io_register *reg, uint32_t value);

#endif /* _DEVICE_H_ */