Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*	$NetBSD: becc_pci.c,v 1.22 2022/09/27 06:36:43 skrll Exp $	*/

/*
 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * PCI configuration support for the ADI Engineering Big Endian Companion
 * Chip.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: becc_pci.c,v 1.22 2022/09/27 06:36:43 skrll Exp $");

#include "opt_pci.h"
#include "pci.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/bus.h>

#include <uvm/uvm_extern.h>

#include <dev/pci/ppbreg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pciconf.h>

#include <arm/locore.h>

#include <arm/xscale/beccreg.h>
#include <arm/xscale/beccvar.h>

void		becc_pci_attach_hook(device_t, device_t,
		    struct pcibus_attach_args *);
int		becc_pci_bus_maxdevs(void *, int);
pcitag_t	becc_pci_make_tag(void *, int, int, int);
void		becc_pci_decompose_tag(void *, pcitag_t, int *, int *,
		    int *);
pcireg_t	becc_pci_conf_read(void *, pcitag_t, int);
void		becc_pci_conf_write(void *, pcitag_t, int, pcireg_t);
void		becc_pci_conf_interrupt(void *, int, int, int, int, int *);

int		becc_pci_intr_map(const struct pci_attach_args *,
		    pci_intr_handle_t *);
const char	*becc_pci_intr_string(void *, pci_intr_handle_t,
		    char *, size_t);
const struct evcnt *becc_pci_intr_evcnt(void *, pci_intr_handle_t);
void		*becc_pci_intr_establish(void *, pci_intr_handle_t,
		    int, int (*)(void *), void *, const char *);
void		becc_pci_intr_disestablish(void *, void *);

#define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
#define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))

#if 0
#define DPRINTF(x) printf(x)
#else
#define DPRINTF(x)
#endif

void
becc_pci_init(pci_chipset_tag_t pc, void *cookie)
{
#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
	struct becc_softc *sc = cookie;
	struct pciconf_resources *pcires;
#endif

	pc->pc_conf_v = cookie;
	pc->pc_attach_hook = becc_pci_attach_hook;
	pc->pc_bus_maxdevs = becc_pci_bus_maxdevs;
	pc->pc_make_tag = becc_pci_make_tag;
	pc->pc_decompose_tag = becc_pci_decompose_tag;
	pc->pc_conf_read = becc_pci_conf_read;
	pc->pc_conf_write = becc_pci_conf_write;
	pc->pc_conf_interrupt = becc_pci_conf_interrupt;

	pc->pc_intr_v = cookie;
	pc->pc_intr_map = becc_pci_intr_map;
	pc->pc_intr_string = becc_pci_intr_string;
	pc->pc_intr_evcnt = becc_pci_intr_evcnt;
	pc->pc_intr_establish = becc_pci_intr_establish;
	pc->pc_intr_disestablish = becc_pci_intr_disestablish;

#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
	/*
	 * Configure the PCI bus.
	 *
	 * XXX We need to revisit this.  We only configure the Secondary
	 * bus (and its children).  The bus configure code needs changes
	 * to support how the busses are arranged on this chip.  We also
	 * need to only configure devices in the private device space on
	 * the Secondary bus.
	 */

	pcires = pciconf_resource_init();

	/* Reserve the bottom 32K of the PCI address space. */
	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
	    sc->sc_ioout_xlate + (32 * 1024), (32 * 1024));
	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
	    sc->sc_owin_xlate[0], BECC_PCI_MEM1_SIZE);

	aprint_normal("%s: configuring PCI bus\n", device_xname(sc->sc_dev));
	pci_configure_bus(pc, pcires, 0, arm_dcache_align);

	pciconf_resource_fini(pcires);
#endif
}

void
becc_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
{
}

void
becc_pci_attach_hook(device_t parent, device_t self,
    struct pcibus_attach_args *pba)
{

	/* Nothing to do. */
}

int
becc_pci_bus_maxdevs(void *v, int busno)
{

	return (32);
}

pcitag_t
becc_pci_make_tag(void *v, int b, int d, int f)
{

	return ((b << 16) | (d << 11) | (f << 8));
}

void
becc_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
{

	if (bp != NULL)
		*bp = (tag >> 16) & 0xff;
	if (dp != NULL)
		*dp = (tag >> 11) & 0x1f;
	if (fp != NULL)
		*fp = (tag >> 8) & 0x7;
}

struct pciconf_state {
	uint32_t ps_offset;

	int ps_b, ps_d, ps_f;
	int ps_type;
};

static int
becc_pci_conf_setup(struct becc_softc *sc, pcitag_t tag, int offset,
    struct pciconf_state *ps)
{

	if ((unsigned int)offset >= PCI_CONF_SIZE)
		return (1);

	becc_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);

	/*
	 * If the bus # is the same as our own, then use Type 0 cycles,
	 * else use Type 1.
	 */
	if (ps->ps_b == 0) {
		/* XXX This is a platform-specific parameter. */
		if (ps->ps_d > (14 - BECC_IDSEL_BIT))
			return (1);
		ps->ps_offset = (1U << (ps->ps_d + BECC_IDSEL_BIT)) |
		    (ps->ps_f << 8) | offset;
		ps->ps_type = 0;
	} else {
		/* The tag is already in the correct format. */
		ps->ps_offset = tag | offset | 1;
		ps->ps_type = 1;
	}

	return (0);
}

static int becc_pci_conf_cleanup(struct becc_softc *sc);
static int
becc_pci_conf_cleanup(struct becc_softc *sc)
{
	uint32_t reg;
	int	err=0;

	BECC_CSR_WRITE(BECC_POCR, 0);

	reg = becc_pcicore_read(sc, PCI_COMMAND_STATUS_REG);
	if (reg & 0xf9000000) {
		DPRINTF((" ** pci status error: %08x (%08x) **\n",
		    reg, reg & 0xf9000000));

		err = 1;
		becc_pcicore_write(sc, PCI_COMMAND_STATUS_REG,
		    reg & 0xf900ffff);
		reg = becc_pcicore_read(sc, PCI_COMMAND_STATUS_REG);

		DPRINTF((" ** pci status after clearing: %08x (%08x) **\n",
		    reg, reg & 0xf9000000));
	}
	reg = BECC_CSR_READ(BECC_PMISR);
	if (reg & 0x000f000d) {
		DPRINTF((" ** pci master isr: %08x (%08x) **\n",
		    reg, reg & 0x000f000d));

		err = 1;
		BECC_CSR_WRITE(BECC_PMISR, reg & 0x000f000d);
		reg = BECC_CSR_READ(BECC_PMISR);

		DPRINTF((" ** pci master isr after clearing: %08x (%08x) **\n",
		    reg, reg & 0x000f000d));
	}
	reg = BECC_CSR_READ(BECC_PSISR);
	if (reg & 0x000f0210) {
		DPRINTF((" ** pci slave isr: %08x (%08x) **\n",
		    reg, reg & 0x000f0210));

		err = 1;
		BECC_CSR_WRITE(BECC_PSISR, reg & 0x000f0210);
		reg = BECC_CSR_READ(BECC_PSISR);

		DPRINTF((" ** pci slave isr after clearing: %08x (%08x) **\n",
		    reg, reg & 0x000f0210));
	}

	return err;
}

pcireg_t
becc_pci_conf_read(void *v, pcitag_t tag, int offset)
{
	struct becc_softc *sc = v;
	struct pciconf_state ps;
	vaddr_t va;
	pcireg_t rv;
	u_int s;

	if (becc_pci_conf_setup(sc, tag, offset, &ps))
		return ((pcireg_t) -1);

	/*
	 * Skip device 0 (the BECC itself).  We don't want it
	 * to appear as part of the PCI device space.
	 */
	if (ps.ps_b == 0 && ps.ps_d == 0)
		return ((pcireg_t) -1);

	PCI_CONF_LOCK(s);

	va = sc->sc_pci_cfg_base + ps.ps_offset;
	BECC_CSR_WRITE(BECC_POCR, ps.ps_type);

	if (badaddr_read((void *) va, sizeof(rv), &rv)) {
		/* XXX Check master/target abort? */
#if 0
		printf("conf_read: %d/%d/%d bad address\n",
		    ps.ps_b, ps.ps_d, ps.ps_f);
#endif
		rv = (pcireg_t) -1;
	}

	if (becc_pci_conf_cleanup(sc))
		rv = (pcireg_t) -1;

	PCI_CONF_UNLOCK(s);

	return (rv);
}

void
becc_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
{
	struct becc_softc *sc = v;
	struct pciconf_state ps;
	vaddr_t va;
	u_int s;

	if (becc_pci_conf_setup(sc, tag, offset, &ps))
		return;

	PCI_CONF_LOCK(s);
	BECC_CSR_WRITE(BECC_POCR, ps.ps_type);

	va = sc->sc_pci_cfg_base + ps.ps_offset;

	*(volatile pcireg_t *)va = val;

	becc_pci_conf_cleanup(sc);

	PCI_CONF_UNLOCK(s);
}

int
becc_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
	int irq;

	if (pa->pa_bus == 0) {
		switch (pa->pa_device) {
		case 1: irq = ICU_PCI_INTB; break; /* Ethernet #0 */
		case 2: irq = ICU_PCI_INTC; break; /* Ethernet #1 */
		case 3:				   /* Card slot */
			switch (pa->pa_intrpin) {
			case 1:		irq = ICU_PCI_INTA; break;
			case 2:		irq = ICU_PCI_INTB; break;
			case 3:		irq = ICU_PCI_INTC; break;
			case 4:		irq = ICU_PCI_INTD; break;
			default:
				printf("becc_pci_intr_map: bogus pin: %d\n",
				    pa->pa_intrpin);
				return (1);
			}
			break;
		default:
			printf("becc_pci_intr_map: bogus device: %d\n",
			    pa->pa_device);
			return (1);
		}
	} else {
		switch (pa->pa_intrpin) {
		case 1:		irq = ICU_PCI_INTA; break;
		case 2:		irq = ICU_PCI_INTB; break;
		case 3:		irq = ICU_PCI_INTC; break;
		case 4:		irq = ICU_PCI_INTD; break;
		default:
			printf("becc_pci_intr_map: bogus pin: %d\n",
			    pa->pa_intrpin);
			return (1);
		}
	}

	*ihp = irq;
	return (0);
}

const char *
becc_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
{

	strlcpy(buf, becc_irqnames[ih], len);
	return buf;
}

const struct evcnt *
becc_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
{

	/* XXX For now. */
	return (NULL);
}

void *
becc_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    int (*func)(void *), void *arg, const char *xname)
{

	return (becc_intr_establish(ih, ipl, func, arg));
}

void
becc_pci_intr_disestablish(void *v, void *cookie)
{

	becc_intr_disestablish(cookie);
}