1/*
2 * Copyright 2006, Marcus Overhagen. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "pci.h"
8#include "pci_private.h"
9#include "arch_cpu.h"
10
11
12//#define TRACE_PCI_IO
13#undef TRACE
14#ifdef TRACE_PCI_IO
15#	define TRACE(x...) dprintf("PCI_IO: " x)
16#else
17#	define TRACE(x...) ;
18#endif
19
20
21#if defined(__i386__) || defined(__x86_64__)
22
23uint8
24pci_read_io_8(int mapped_io_addr)
25{
26	return in8(mapped_io_addr);
27}
28
29
30void
31pci_write_io_8(int mapped_io_addr, uint8 value)
32{
33	out8(value, mapped_io_addr);
34}
35
36
37uint16
38pci_read_io_16(int mapped_io_addr)
39{
40	return in16(mapped_io_addr);
41}
42
43
44void
45pci_write_io_16(int mapped_io_addr, uint16 value)
46{
47	out16(value, mapped_io_addr);
48}
49
50
51uint32
52pci_read_io_32(int mapped_io_addr)
53{
54	return in32(mapped_io_addr);
55}
56
57
58void
59pci_write_io_32(int mapped_io_addr, uint32 value)
60{
61	out32(value, mapped_io_addr);
62}
63
64#else
65
66static uint8*
67get_io_port_address(int ioPort)
68{
69	uint8 domain;
70	pci_resource_range range;
71	uint8 *mappedAdr;
72
73	if (gPCI->LookupRange(B_IO_PORT, ioPort, domain, range, &mappedAdr) < B_OK)
74		return NULL;
75
76	return mappedAdr + ioPort;
77}
78
79
80uint8
81pci_read_io_8(int mapped_io_addr)
82{
83	TRACE("pci_read_io_8(%d)\n", mapped_io_addr);
84	vuint8* ptr = get_io_port_address(mapped_io_addr);
85	if (ptr == NULL)
86		return 0;
87
88	return *ptr;
89}
90
91
92void
93pci_write_io_8(int mapped_io_addr, uint8 value)
94{
95	TRACE("pci_write_io_8(%d)\n", mapped_io_addr);
96	vuint8* ptr = get_io_port_address(mapped_io_addr);
97	if (ptr == NULL)
98		return;
99
100	*ptr = value;
101}
102
103
104uint16
105pci_read_io_16(int mapped_io_addr)
106{
107	TRACE("pci_read_io_16(%d)\n", mapped_io_addr);
108	vuint16* ptr = (uint16*)get_io_port_address(mapped_io_addr);
109	if (ptr == NULL)
110		return 0;
111
112	return *ptr;
113}
114
115
116void
117pci_write_io_16(int mapped_io_addr, uint16 value)
118{
119	TRACE("pci_write_io_16(%d)\n", mapped_io_addr);
120	vuint16* ptr = (uint16*)get_io_port_address(mapped_io_addr);
121	if (ptr == NULL)
122		return;
123
124	*ptr = value;
125}
126
127
128uint32
129pci_read_io_32(int mapped_io_addr)
130{
131	TRACE("pci_read_io_32(%d)\n", mapped_io_addr);
132	vuint32* ptr = (uint32*)get_io_port_address(mapped_io_addr);
133	if (ptr == NULL)
134		return 0;
135
136	return *ptr;
137}
138
139
140void
141pci_write_io_32(int mapped_io_addr, uint32 value)
142{
143	TRACE("pci_write_io_32(%d)\n", mapped_io_addr);
144	vuint32* ptr = (vuint32*)get_io_port_address(mapped_io_addr);
145	if (ptr == NULL)
146		return;
147
148	*ptr = value;
149}
150
151#endif
152