xref: /linux/drivers/isdn/hardware/mISDN/iohelper.h (revision 9e56ff53b4115875667760445b028357848b4748)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * iohelper.h
4  *		helper for define functions to access ISDN hardware
5  *              supported are memory mapped IO
6  *		indirect port IO (one port for address, one for data)
7  *
8  * Author       Karsten Keil <keil@isdn4linux.de>
9  *
10  * Copyright 2009  by Karsten Keil <keil@isdn4linux.de>
11  */
12 
13 #ifndef _IOHELPER_H
14 #define _IOHELPER_H
15 
16 typedef u8 (read_reg_func)(void *hwp, u8 offset);
17 typedef void (write_reg_func)(void *hwp, u8 offset, u8 value);
18 typedef void (fifo_func)(void *hwp, u8 offset, u8 *datap, int size);
19 
20 struct _ioport {
21 	u32 port;
22 	u32 ale;
23 };
24 
25 #define IOFUNC_IO(name, hws, ap)					\
26 	static u8 Read##name##_IO(void *p, u8 off) {			\
27 		struct hws *hw = p;					\
28 		return inb(hw->ap.port + off);				\
29 	}								\
30 	static void Write##name##_IO(void *p, u8 off, u8 val) {		\
31 		struct hws *hw = p;					\
32 		outb(val, hw->ap.port + off);				\
33 	}								\
34 	static void ReadFiFo##name##_IO(void *p, u8 off, u8 *dp, int size) { \
35 		struct hws *hw = p;					\
36 		insb(hw->ap.port + off, dp, size);			\
37 	}								\
38 	static void WriteFiFo##name##_IO(void *p, u8 off, u8 *dp, int size) { \
39 		struct hws *hw = p;					\
40 		outsb(hw->ap.port + off, dp, size);			\
41 	}
42 
43 #define IOFUNC_IND(name, hws, ap)					\
44 	static u8 Read##name##_IND(void *p, u8 off) {			\
45 		struct hws *hw = p;					\
46 		outb(off, hw->ap.ale);					\
47 		return inb(hw->ap.port);				\
48 	}								\
49 	static void Write##name##_IND(void *p, u8 off, u8 val) {	\
50 		struct hws *hw = p;					\
51 		outb(off, hw->ap.ale);					\
52 		outb(val, hw->ap.port);					\
53 	}								\
54 	static void ReadFiFo##name##_IND(void *p, u8 off, u8 *dp, int size) { \
55 		struct hws *hw = p;					\
56 		outb(off, hw->ap.ale);					\
57 		insb(hw->ap.port, dp, size);				\
58 	}								\
59 	static void WriteFiFo##name##_IND(void *p, u8 off, u8 *dp, int size) { \
60 		struct hws *hw = p;					\
61 		outb(off, hw->ap.ale);					\
62 		outsb(hw->ap.port, dp, size);				\
63 	}
64 
65 #define IOFUNC_MEMIO(name, hws, typ, adr)				\
66 	static u8 Read##name##_MIO(void *p, u8 off) {			\
67 		struct hws *hw = p;					\
68 		return readb(((typ *)hw->adr) + off);			\
69 	}								\
70 	static void Write##name##_MIO(void *p, u8 off, u8 val) {	\
71 		struct hws *hw = p;					\
72 		writeb(val, ((typ *)hw->adr) + off);			\
73 	}								\
74 	static void ReadFiFo##name##_MIO(void *p, u8 off, u8 *dp, int size) { \
75 		struct hws *hw = p;					\
76 		while (size--)						\
77 			*dp++ = readb(((typ *)hw->adr) + off);		\
78 	}								\
79 	static void WriteFiFo##name##_MIO(void *p, u8 off, u8 *dp, int size) { \
80 		struct hws *hw = p;					\
81 		while (size--)						\
82 			writeb(*dp++, ((typ *)hw->adr) + off);		\
83 	}
84 
85 #define ASSIGN_FUNC(typ, name, dest)	do {			\
86 		dest.read_reg = &Read##name##_##typ;		\
87 		dest.write_reg = &Write##name##_##typ;		\
88 		dest.read_fifo = &ReadFiFo##name##_##typ;	\
89 		dest.write_fifo = &WriteFiFo##name##_##typ;	\
90 	} while (0)
91 #define ASSIGN_FUNC_IPAC(typ, target)	do {		\
92 		ASSIGN_FUNC(typ, ISAC, target.isac);	\
93 		ASSIGN_FUNC(typ, IPAC, target);		\
94 	} while (0)
95 
96 #endif
97