summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/lpci/pci.cpp
blob: 90e13e9eb22709a084bf990c7d5409e66ae8e160 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// license:BSD-3-Clause
// copyright-holders:Miodrag Milanovic
/***************************************************************************

    pci.c

    PCI bus

    The PCI bus is a 32-bit bus introduced by Intel, so it is little endian

    Control word:
        bit 31:         Enable bit
        bits 30-24:     Reserved
        bits 23-16:     PCI bus number
        bits 15-11:     PCI device number
        bits 10- 8:     PCI function number
        bits  7- 0:     Offset address

    Standard PCI registers:
        0x00    2   Vendor ID
        0x02    2   Device ID
        0x04    2   PCI Command
        0x06    2   PCI Status
        0x08    1   Revision ID
        0x09    1   Programming Interface
        0x0A    1   Subclass Code
        0x0B    1   Class Code

    Class Code/Subclass Code/Programming Interface
        0x00XXXX    Pre-PCI 2.0 devices
        0x000000        Non-VGA device
        0x000101        VGA device
        0x01XXXX    Storage Controller
        0x010000        SCSI
        0x0101XX        IDE
        0x0102XX        Floppy
        0x0103XX        IPI
        0x0104XX        RAID
        0x0180XX        Other
        0x02XXXX    Network Card
        0x020000        Ethernet
        0x020100        Tokenring
        0x020200        FDDI
        0x020300        ATM
        0x028000        Other
        0x03XXXX    Display Controller
        0x030000        VGA
        0x030001        8514 Compatible
        0x030100        XGA
        0x038000        Other
        0x04XXXX    Multimedia
        0x040000        Video
        0x040100        Audio
        0x048000        Other
        0x05XXXX    Memory Controller
        0x050000        RAM
        0x050100        Flash
        0x058000        Other
        0x06XXXX    Bridge
        0x060000        Host/PCI
        0x060100        PCI/ISA
        0x060200        PCI/EISA
        0x060300        PCI/Micro Channel
        0x060400        PCI/PCI
        0x060500        PCI/PCMCIA
        0x060600        PCI/NuBus
        0x060700        PCI/CardBus
        0x068000        Other

    Information on PCI vendors can be found at http://www.pcidatabase.com/

***************************************************************************/

#include "emu.h"
#include "pci.h"

#define LOG_PCI 0

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

const device_type PCI_BUS = &device_creator<pci_bus_device>;

//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  pci_bus_device - constructor
//-------------------------------------------------
pci_bus_device::pci_bus_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
		device_t(mconfig, PCI_BUS, "PCI Bus", tag, owner, clock, "pci_bus", __FILE__), m_busnum(0),
		m_father(nullptr), m_address(0), m_devicenum(0), m_busnumber(0), m_busnumaddr(nullptr)
{
	for (int i = 0; i < ARRAY_LENGTH(m_devtag); i++) {
		m_devtag[i]= nullptr;
	}
	m_siblings_count = 0;
}

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

READ32_MEMBER( pci_bus_device::read )
{
	UINT32 result = 0xffffffff;
	int function, reg;

	offset %= 2;

	switch (offset)
	{
		case 0:
			result = m_address;
			break;

		case 1:
			if (m_devicenum != -1)
			{
				if (m_busnumaddr->m_device[m_devicenum] != nullptr)
				{
					function = (m_address >> 8) & 0x07;
					reg = (m_address >> 0) & 0xfc;
					result = m_busnumaddr->m_device[m_devicenum]->pci_read(m_busnumaddr, function, reg, mem_mask);
				}
			}
			break;
	}

	if (LOG_PCI)
		logerror("read('%s'): offset=%d result=0x%08X\n", tag(), offset, result);

	return result;
}



pci_bus_device *pci_bus_device::pci_search_bustree(int busnum, int devicenum, pci_bus_device *pcibus)
{
	int a;
	pci_bus_device *ret;

	if (pcibus->m_busnum == busnum)
	{
		return pcibus;
	}
	for (a = 0; a < pcibus->m_siblings_count; a++)
	{
		ret = pci_search_bustree(busnum, devicenum, pcibus->m_siblings[a]);
		if (ret != nullptr)
			return ret;
	}
	return nullptr;
}



WRITE32_MEMBER( pci_bus_device::write )
{
	offset %= 2;

	if (LOG_PCI)
		logerror("write('%s'): offset=%d data=0x%08X\n", tag(), offset, data);

	switch (offset)
	{
		case 0:
			m_address = data;

			/* lookup current device */
			if (m_address & 0x80000000)
			{
				int busnum = (m_address >> 16) & 0xff;
				int devicenum = (m_address >> 11) & 0x1f;
				m_busnumaddr = pci_search_bustree(busnum, devicenum, this);
				if (m_busnumaddr != nullptr)
				{
					m_busnumber = busnum;
					m_devicenum = devicenum;
				}
				else
					m_devicenum = -1;
				if (LOG_PCI)
					logerror("  bus:%d device:%d\n", busnum, devicenum);
			}
			break;

		case 1:
			if (m_devicenum != -1)
			{
				if (m_busnumaddr->m_device[m_devicenum] != nullptr)
				{
					int function = (m_address >> 8) & 0x07;
					int reg = (m_address >> 0) & 0xfc;
					m_busnumaddr->m_device[m_devicenum]->pci_write(m_busnumaddr, function, reg, data, mem_mask);
				}
				if (LOG_PCI)
					logerror("  function:%d register:%d\n", (m_address >> 8) & 0x07, (m_address >> 0) & 0xfc);
			}
			break;
	}
}



READ64_MEMBER(pci_bus_device::read_64be)
{
	UINT64 result = 0;
	mem_mask = FLIPENDIAN_INT64(mem_mask);
	if (ACCESSING_BITS_0_31)
		result |= (UINT64)read(space, offset * 2 + 0, mem_mask >> 0) << 0;
	if (ACCESSING_BITS_32_63)
		result |= (UINT64)read(space, offset * 2 + 1, mem_mask >> 32) << 32;
	return FLIPENDIAN_INT64(result);
}

WRITE64_MEMBER(pci_bus_device::write_64be)
{
	data = FLIPENDIAN_INT64(data);
	mem_mask = FLIPENDIAN_INT64(mem_mask);
	if (ACCESSING_BITS_0_31)
		write(space, offset * 2 + 0, data >> 0, mem_mask >> 0);
	if (ACCESSING_BITS_32_63)
		write(space, offset * 2 + 1, data >> 32, mem_mask >> 32);
}


void pci_bus_device::add_sibling(pci_bus_device *sibling, int busnum)
{
	m_siblings[m_siblings_count] = sibling;
	m_siblings_busnum[m_siblings_count] = busnum;
	m_siblings_count++;
}


//-------------------------------------------------
//  device_post_load - handle updating after a
//  restore
//-------------------------------------------------

void pci_bus_device::device_post_load()
{
	if (m_devicenum != -1)
	{
		m_busnumaddr = pci_search_bustree(m_busnumber, m_devicenum, this);
	}
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void pci_bus_device::device_start()
{
	/* store a pointer back to the device */
	m_devicenum = -1;

	char id[3];
	/* find all our devices */
	for (int i = 0; i < ARRAY_LENGTH(m_devtag); i++)
	{
		sprintf(id, "%d", i);
		pci_connector *conn = downcast<pci_connector *>(subdevice(id));
		if (conn!=nullptr)
			m_device[i] = conn->get_device();
		else
			m_device[i] = nullptr;
	}

	if (m_father != nullptr) {
		pci_bus_device *father = machine().device<pci_bus_device>(m_father);
		if (father)
			father->add_sibling(this, m_busnum);
	}

	/* register pci states */
	save_item(NAME(m_address));
	save_item(NAME(m_devicenum));
	save_item(NAME(m_busnum));
}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void pci_bus_device::device_reset()
{
	/* reset the drive state */
	m_devicenum = -1;
	m_address = 0;
}

//-------------------------------------------------
//  pci_device_interface - constructor
//-------------------------------------------------

pci_device_interface::pci_device_interface(const machine_config &mconfig, device_t &device)
	: device_slot_card_interface(mconfig, device)
{
}

//-------------------------------------------------
//  ~pci_device_interface - destructor
//-------------------------------------------------

pci_device_interface::~pci_device_interface()
{
}


const device_type PCI_CONNECTOR = &device_creator<pci_connector>;


pci_connector::pci_connector(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
	device_t(mconfig, PCI_CONNECTOR, "PCI device connector abstraction", tag, owner, clock, "pci_connector", __FILE__),
	device_slot_interface(mconfig, *this)
{
}

pci_connector::~pci_connector()
{
}

void pci_connector::device_start()
{
}

pci_device_interface *pci_connector::get_device()
{
	return dynamic_cast<pci_device_interface *>(get_card_device());
}