summaryrefslogtreecommitdiff
path: root/gpxe/src/drivers/bus/mca.c
blob: 2815603efbb582575cdcbe50ce691dd631b9f4e2 (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
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
/*
 * MCA bus driver code
 *
 * Abstracted from 3c509.c.
 *
 */

FILE_LICENCE ( BSD2 );

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <gpxe/io.h>
#include <gpxe/mca.h>

static void mcabus_remove ( struct root_device *rootdev );

/**
 * Probe an MCA device
 *
 * @v mca		MCA device
 * @ret rc		Return status code
 *
 * Searches for a driver for the MCA device.  If a driver is found,
 * its probe() routine is called.
 */
static int mca_probe ( struct mca_device *mca ) {
	struct mca_driver *driver;
	struct mca_device_id *id;
	unsigned int i;
	int rc;

	DBG ( "Adding MCA slot %02x (ID %04x POS "
	      "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x)\n",
	      mca->slot, MCA_ID ( mca ),
	      mca->pos[0], mca->pos[1], mca->pos[2], mca->pos[3],
	      mca->pos[4], mca->pos[5], mca->pos[6], mca->pos[7] );

	for_each_table_entry ( driver, MCA_DRIVERS ) {
		for ( i = 0 ; i < driver->id_count ; i++ ) {
			id = &driver->ids[i];
			if ( id->id != MCA_ID ( mca ) )
				continue;
			mca->driver = driver;
			mca->driver_name = id->name;
			DBG ( "...using driver %s\n", mca->driver_name );
			if ( ( rc = driver->probe ( mca, id ) ) != 0 ) {
				DBG ( "......probe failed\n" );
				continue;
			}
			return 0;
		}
	}

	DBG ( "...no driver found\n" );
	return -ENOTTY;
}

/**
 * Remove an MCA device
 *
 * @v mca		MCA device
 */
static void mca_remove ( struct mca_device *mca ) {
	mca->driver->remove ( mca );
	DBG ( "Removed MCA device %02x\n", mca->slot );
}

/**
 * Probe MCA root bus
 *
 * @v rootdev		MCA bus root device
 *
 * Scans the MCA bus for devices and registers all devices it can
 * find.
 */
static int mcabus_probe ( struct root_device *rootdev ) {
	struct mca_device *mca = NULL;
	unsigned int slot;
	int seen_non_ff;
	unsigned int i;
	int rc;

	for ( slot = 0 ; slot <= MCA_MAX_SLOT_NR ; slot++ ) {
		/* Allocate struct mca_device */
		if ( ! mca )
			mca = malloc ( sizeof ( *mca ) );
		if ( ! mca ) {
			rc = -ENOMEM;
			goto err;
		}
		memset ( mca, 0, sizeof ( *mca ) );
		mca->slot = slot;

		/* Make sure motherboard setup is off */
		outb_p ( 0xff, MCA_MOTHERBOARD_SETUP_REG );

		/* Select the slot */
		outb_p ( 0x8 | ( mca->slot & 0xf ), MCA_ADAPTER_SETUP_REG );

		/* Read the POS registers */
		seen_non_ff = 0;
		for ( i = 0 ; i < ( sizeof ( mca->pos ) /
				    sizeof ( mca->pos[0] ) ) ; i++ ) {
			mca->pos[i] = inb_p ( MCA_POS_REG ( i ) );
			if ( mca->pos[i] != 0xff )
				seen_non_ff = 1;
		}
	
		/* Kill all setup modes */
		outb_p ( 0, MCA_ADAPTER_SETUP_REG );

		/* If all POS registers are 0xff, this means there's no device
		 * present
		 */
		if ( ! seen_non_ff )
			continue;

		/* Add to device hierarchy */
		snprintf ( mca->dev.name, sizeof ( mca->dev.name ),
			   "MCA%02x", slot );
		mca->dev.desc.bus_type = BUS_TYPE_MCA;
		mca->dev.desc.vendor = GENERIC_MCA_VENDOR;
		mca->dev.desc.device = MCA_ID ( mca );
		mca->dev.parent = &rootdev->dev;
		list_add ( &mca->dev.siblings, &rootdev->dev.children );
		INIT_LIST_HEAD ( &mca->dev.children );

		/* Look for a driver */
		if ( mca_probe ( mca ) == 0 ) {
			/* mcadev registered, we can drop our ref */
			mca = NULL;
		} else {
			/* Not registered; re-use struct */
			list_del ( &mca->dev.siblings );
		}
	}

	free ( mca );
	return 0;

 err:
	free ( mca );
	mcabus_remove ( rootdev );
	return rc;
}

/**
 * Remove MCA root bus
 *
 * @v rootdev		MCA bus root device
 */
static void mcabus_remove ( struct root_device *rootdev ) {
	struct mca_device *mca;
	struct mca_device *tmp;

	list_for_each_entry_safe ( mca, tmp, &rootdev->dev.children,
				   dev.siblings ) {
		mca_remove ( mca );
		list_del ( &mca->dev.siblings );
		free ( mca );
	}
}

/** MCA bus root device driver */
static struct root_driver mca_root_driver = {
	.probe = mcabus_probe,
	.remove = mcabus_remove,
};

/** MCA bus root device */
struct root_device mca_root_device __root_device = {
	.dev = { .name = "MCA" },
	.driver = &mca_root_driver,
};