summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sparc/shared/1553/gr1553b.c
blob: c715e4fa4045bc2a73a35bd77177da2495ca8f81 (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
/*  GR1553B driver, used by BC, RT and/or BM driver
 *
 *  COPYRIGHT (c) 2010.
 *  Cobham Gaisler AB.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#include <stdlib.h>
#include <drvmgr/ambapp_bus.h>

#include <gr1553b.h>

/* Driver Manager interface for BC, RT, BM, BRM, BC-BM and RT-BM */

#define GR1553B_WRITE_REG(adr, val) *(volatile uint32_t *)(adr) = (val)
#define GR1553B_READ_REG(adr) (*(volatile uint32_t *)(adr))

#define FEAT_BC 0x1
#define FEAT_RT 0x2
#define FEAT_BM 0x4

#define ALLOC_BC 0x1
#define ALLOC_RT 0x2
#define ALLOC_BM 0x4

struct gr1553_device {
	struct drvmgr_dev *dev;
	int features;
	int alloc;
};

struct gr1553_device_feature {
	struct gr1553_device_feature *next;
	struct gr1553_device *dev;
	int minor;
};

/* Device lists */
static struct gr1553_device_feature *gr1553_bm_root = NULL;
static struct gr1553_device_feature *gr1553_rt_root = NULL;
static struct gr1553_device_feature *gr1553_bc_root = NULL;

/* Driver registered */
static int gr1553_driver_registerd = 0;

/* Add 'feat' to linked list pointed to by 'root'. A minor is also assigned. */
static void gr1553_list_add
	(
	struct gr1553_device_feature **root,
	struct gr1553_device_feature *feat
	)
{
	int minor;
	struct gr1553_device_feature *curr;

	if ( *root == NULL ) {
		*root = feat;
		feat->next = NULL;
		feat->minor = 0;
		return;
	}

	minor = 0;
retry_new_minor:
	curr = *root;
	while ( curr->next ) {
		if ( curr->minor == minor ) {
			minor++;
			goto retry_new_minor;
		}
		curr = curr->next;
	}

	feat->next = NULL;
	feat->minor = minor;
	curr->next = feat;
}

static struct gr1553_device_feature *gr1553_list_find
	(
	struct gr1553_device_feature *root,
	int minor
	)
{
	struct gr1553_device_feature *curr = root;
	while ( curr ) {
		if ( curr->minor == minor ) {
			return curr;
		}
		curr = curr->next;
	}
	return NULL;
}

struct drvmgr_dev **gr1553_bc_open(int minor)
{
	struct gr1553_device_feature *feat;
	
	feat = gr1553_list_find(gr1553_bc_root, minor);
	if ( feat == NULL )
		return NULL;

	/* Only possible to allocate is RT and BC is free,
	 * this is beacuse it is not possible to use the
	 * RT and the BC at the same time.
	 */
	if ( feat->dev->alloc & (ALLOC_BC|ALLOC_RT) )
		return NULL;

	/* Alloc BC device */
	feat->dev->alloc |= ALLOC_BC;

	return &feat->dev->dev;
}

void gr1553_bc_close(struct drvmgr_dev **dev)
{
	struct gr1553_device *d = (struct gr1553_device *)dev;

	d->alloc &= ~ALLOC_BC;
}

struct drvmgr_dev **gr1553_rt_open(int minor)
{
	struct gr1553_device_feature *feat;

	feat = gr1553_list_find(gr1553_rt_root, minor);
	if ( feat == NULL )
		return NULL;

	/* Only possible to allocate is RT and BC is free,
	 * this is beacuse it is not possible to use the
	 * RT and the BC at the same time.
	 */
	if ( feat->dev->alloc & (ALLOC_BC|ALLOC_RT) )
		return NULL;

	/* Alloc RT device */
	feat->dev->alloc |= ALLOC_RT;

	return &feat->dev->dev;
}

void gr1553_rt_close(struct drvmgr_dev **dev)
{
	struct gr1553_device *d = (struct gr1553_device *)dev;

	d->alloc &= ~ALLOC_RT;
}

struct drvmgr_dev **gr1553_bm_open(int minor)
{
	struct gr1553_device_feature *feat;
	
	feat = gr1553_list_find(gr1553_bm_root, minor);
	if ( feat == NULL )
		return NULL;

	/* Only possible to allocate is RT and BC is free,
	 * this is beacuse it is not possible to use the
	 * RT and the BC at the same time.
	 */
	if ( feat->dev->alloc & ALLOC_BM )
		return NULL;

	/* Alloc BM device */
	feat->dev->alloc |= ALLOC_BM;

	return &feat->dev->dev;
}

void gr1553_bm_close(struct drvmgr_dev **dev)
{
	struct gr1553_device *d = (struct gr1553_device *)dev;

	d->alloc &= ~ALLOC_BM;
}

static int gr1553_init2(struct drvmgr_dev *dev)
{
	struct amba_dev_info *ambadev;
	struct ambapp_core *pnpinfo;
	struct gr1553b_regs *regs;

	/* Get device information from AMBA PnP information */
	ambadev = (struct amba_dev_info *)dev->businfo;
	if ( ambadev == NULL ) {
		return DRVMGR_FAIL;
	}
	pnpinfo = &ambadev->info;
	regs = (struct gr1553b_regs *)pnpinfo->apb_slv->start;

	/* Stop IRQ */
	GR1553B_WRITE_REG(&regs->imask, 0);
	GR1553B_WRITE_REG(&regs->irq, 0xffffffff);
	/* Stop BC if not already stopped (just in case) */
	GR1553B_WRITE_REG(&regs->bc_ctrl, 0x15520204);
	/* Stop RT rx (just in case) */
	GR1553B_WRITE_REG(&regs->rt_cfg, 0x15530000);
	/* Stop BM logging (just in case) */
	GR1553B_WRITE_REG(&regs->bm_ctrl, 0);

	return DRVMGR_OK;
}

/* Register the different functionalities that the
 * core supports.
 */
static int gr1553_init3(struct drvmgr_dev *dev)
{
	struct amba_dev_info *ambadev;
	struct ambapp_core *pnpinfo;
	struct gr1553_device *priv;
	struct gr1553_device_feature *feat;
	struct gr1553b_regs *regs;

	priv = malloc(sizeof(struct gr1553_device));
	if ( priv == NULL )
		return DRVMGR_NOMEM;
	priv->dev = dev;
	priv->alloc = 0;
	priv->features = 0;
	dev->priv = NULL; /* Let higher level driver handle this */

	/* Get device information from AMBA PnP information */
	ambadev = (struct amba_dev_info *)dev->businfo;
	if ( ambadev == NULL ) {
		return DRVMGR_FAIL;
	}
	pnpinfo = &ambadev->info;
	regs = (struct gr1553b_regs *)pnpinfo->apb_slv->start;

	if ( GR1553B_READ_REG(&regs->bm_stat) & GR1553B_BM_STAT_BMSUP ) {
		priv->features |= FEAT_BM;
		feat = malloc(sizeof(struct gr1553_device_feature));
		feat->dev = priv;
		/* Init Minor and Next */
		gr1553_list_add(&gr1553_bm_root, feat);
	}

	if ( GR1553B_READ_REG(&regs->bc_stat) & GR1553B_BC_STAT_BCSUP ) {
		priv->features |= FEAT_BC;
		feat = malloc(sizeof(struct gr1553_device_feature));
		feat->dev = priv;
		/* Init Minor and Next */
		gr1553_list_add(&gr1553_bc_root, feat);
	}

	if ( GR1553B_READ_REG(&regs->rt_stat) & GR1553B_RT_STAT_RTSUP ) {
		priv->features |= FEAT_RT;
		feat = malloc(sizeof(struct gr1553_device_feature));
		feat->dev = priv;
		/* Init Minor and Next */
		gr1553_list_add(&gr1553_rt_root, feat);
	}

	return DRVMGR_OK;
}

struct drvmgr_drv_ops gr1553_ops =
{
	{NULL, gr1553_init2, gr1553_init3, NULL},
	NULL,
	NULL
};

struct amba_dev_id gr1553_ids[] =
{
	{VENDOR_GAISLER, GAISLER_GR1553B},
	{0, 0}	/* Mark end of table */
};

struct amba_drv_info gr1553_drv_info =
{
	{
		DRVMGR_OBJ_DRV,			/* Driver */
		NULL,				/* Next driver */
		NULL,				/* Device list */
		DRIVER_AMBAPP_GAISLER_GR1553B_ID,/* Driver ID */
		"GR1553_DRV",			/* Driver Name */
		DRVMGR_BUS_TYPE_AMBAPP,		/* Bus Type */
		&gr1553_ops,
		NULL,				/* Funcs */
		0,				/* No devices yet */
		0,
	},
	&gr1553_ids[0]
};

/* Multiple drivers may call this function. The drivers that depends on
 * this driver:
 *  - BM driver
 *  - BC driver
 *  - RT driver
 */
void gr1553_register(void)
{
	if ( gr1553_driver_registerd == 0 ) {
		gr1553_driver_registerd = 1;
	        drvmgr_drv_register(&gr1553_drv_info.general);
	}
}