summaryrefslogtreecommitdiffstats
path: root/sys/arm/xscale/ixp425/ixp425_pci.c
blob: c17eb1e45b02f4e2acd3e0d3208b02775b6b85a3 (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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*	$NetBSD: ixp425_pci.c,v 1.5 2006/04/10 03:36:03 simonb Exp $ */

/*
 * Copyright (c) 2003
 *	Ichiro FUKUHARA <ichiro@ichiro.org>.
 * All rights reserved.
 *
 * 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 by Ichiro FUKUHARA.
 * 4. The name of the company nor the name of the author may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``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 ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#define _ARM32_BUS_DMA_PRIVATE
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/rman.h>

#include <dev/pci/pcivar.h>

#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/pcb.h>

#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#include <machine/pmap.h>

#include <arm/xscale/ixp425/ixp425reg.h>
#include <arm/xscale/ixp425/ixp425var.h>

#include <dev/pci/pcib_private.h>
#include "pcib_if.h"

#include <dev/pci/pcireg.h>
extern struct ixp425_softc *ixp425_softc;

#define	PCI_CSR_WRITE_4(sc, reg, data)	\
	bus_write_4(sc->sc_csr, reg, data)

#define	PCI_CSR_READ_4(sc, reg)	\
	bus_read_4(sc->sc_csr, reg)

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

static device_probe_t ixppcib_probe;
static device_attach_t ixppcib_attach;
static bus_read_ivar_t ixppcib_read_ivar;
static bus_write_ivar_t ixppcib_write_ivar;
static bus_setup_intr_t ixppcib_setup_intr;
static bus_teardown_intr_t ixppcib_teardown_intr;
static bus_alloc_resource_t ixppcib_alloc_resource;
static bus_activate_resource_t ixppcib_activate_resource;
static bus_deactivate_resource_t ixppcib_deactivate_resource;
static bus_release_resource_t ixppcib_release_resource;
static pcib_maxslots_t ixppcib_maxslots;
static pcib_read_config_t ixppcib_read_config;
static pcib_write_config_t ixppcib_write_config;
static pcib_route_interrupt_t ixppcib_route_interrupt;

static int
ixppcib_probe(device_t dev)
{
	device_set_desc(dev, "IXP4XX PCI Bus");
        return (0);
}

static void
ixp425_pci_conf_reg_write(struct ixppcib_softc *sc, uint32_t reg,
    uint32_t data)
{
	PCI_CSR_WRITE_4(sc, PCI_CRP_AD_CBE, ((reg & ~3) | COMMAND_CRP_WRITE));
	PCI_CSR_WRITE_4(sc, PCI_CRP_AD_WDATA, data);
}

static int
ixppcib_attach(device_t dev)
{
	int rid;
	struct ixppcib_softc *sc;

	sc = device_get_softc(dev);

	rid = 0;
	sc->sc_csr = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
	    IXP425_PCI_HWBASE, IXP425_PCI_HWBASE + IXP425_PCI_SIZE,
	    IXP425_PCI_SIZE, RF_ACTIVE);
	if (sc->sc_csr == NULL)
		panic("cannot allocate PCI CSR registers");

	ixp425_md_attach(dev);
	/* always setup the base, incase another OS messes w/ it */
	PCI_CSR_WRITE_4(sc, PCI_PCIMEMBASE, 0x48494a4b);

	rid = 0;
	sc->sc_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
	    IXP425_PCI_MEM_HWBASE, IXP425_PCI_MEM_HWBASE + IXP425_PCI_MEM_SIZE,
	    IXP425_PCI_MEM_SIZE, RF_ACTIVE);
	if (sc->sc_mem == NULL)
		panic("cannot allocate PCI MEM space");

	/* NB: PCI dma window is 64M so anything above must be bounced */
	if (bus_dma_tag_create(NULL, 1, 0, IXP425_AHB_OFFSET + 64 * 1024 * 1024,
	    BUS_SPACE_MAXADDR, NULL, NULL,  0xffffffff, 0xff, 0xffffffff, 0,
	    NULL, NULL, &sc->sc_dmat))
		panic("couldn't create the PCI dma tag !");
	/*
	 * The PCI bus can only address 64MB. However, due to the way our
	 * implementation of busdma works, busdma can't tell if a device
	 * is a PCI device or not. So defaults to the PCI dma tag, which
	 * restrict the DMA'able memory to the first 64MB, and explicitely
	 * create less restrictive tags for non-PCI devices.
	 */
	arm_root_dma_tag = sc->sc_dmat;
	/*
	 * Initialize the bus space tags.
	 */
	ixp425_io_bs_init(&sc->sc_pci_iot, sc);
	ixp425_mem_bs_init(&sc->sc_pci_memt, sc);

	sc->sc_dev = dev;

	/* Initialize memory and i/o rmans. */
	sc->sc_io_rman.rm_type = RMAN_ARRAY;
	sc->sc_io_rman.rm_descr = "IXP4XX PCI I/O Ports";
	if (rman_init(&sc->sc_io_rman) != 0 ||
		rman_manage_region(&sc->sc_io_rman, 0,
	    	    IXP425_PCI_IO_SIZE) != 0) {
		panic("ixppcib_probe: failed to set up I/O rman");
	}

	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
	sc->sc_mem_rman.rm_descr = "IXP4XX PCI Memory";
	if (rman_init(&sc->sc_mem_rman) != 0 ||
		rman_manage_region(&sc->sc_mem_rman, IXP425_PCI_MEM_HWBASE,
		    IXP425_PCI_MEM_HWBASE + IXP425_PCI_MEM_SIZE) != 0) {
		panic("ixppcib_probe: failed to set up memory rman");
	}

	/*
	 * PCI->AHB address translation
	 * 	begin at the physical memory start + OFFSET
	 */
	PCI_CSR_WRITE_4(sc, PCI_AHBMEMBASE,
	    (IXP425_AHB_OFFSET & 0xFF000000) +
	    ((IXP425_AHB_OFFSET & 0xFF000000) >> 8) +
	    ((IXP425_AHB_OFFSET & 0xFF000000) >> 16) +
	    ((IXP425_AHB_OFFSET & 0xFF000000) >> 24) +
	    0x00010203);
	
#define IXPPCIB_WRITE_CONF(sc, reg, val) \
	ixp425_pci_conf_reg_write(sc, reg, val)
	/* Write Mapping registers PCI Configuration Registers */
	/* Base Address 0 - 3 */
	IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR0, IXP425_AHB_OFFSET + 0x00000000);
	IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR1, IXP425_AHB_OFFSET + 0x01000000);
	IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR2, IXP425_AHB_OFFSET + 0x02000000);
	IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR3, IXP425_AHB_OFFSET + 0x03000000);
	
	/* Base Address 4 */
	IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR4, 0xffffffff);
	
	/* Base Address 5 */
	IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR5, 0x00000000);
	
	/* Assert some PCI errors */
	PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_AHBE | ISR_PPE | ISR_PFE | ISR_PSE);
	
#ifdef __ARMEB__
	/*
	 * Set up byte lane swapping between little-endian PCI
	 * and the big-endian AHB bus
	 */
	PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE | CSR_PDS);
#else
	PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE);
#endif
	
	/*
	 * Enable bus mastering and I/O,memory access
	 */
	IXPPCIB_WRITE_CONF(sc, PCIR_COMMAND,
	    PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
	
	/*
	 * Wait some more to ensure PCI devices have stabilised.
	 */
	DELAY(50000);

	device_add_child(dev, "pci", -1);
	return (bus_generic_attach(dev));
}

static int
ixppcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
{
	struct ixppcib_softc *sc;

	sc = device_get_softc(dev);
	switch (which) {
	case PCIB_IVAR_DOMAIN:
		*result = 0;
		return (0);
	case PCIB_IVAR_BUS:
		*result = sc->sc_bus;
		return (0);
	}

	return (ENOENT);
}

static int
ixppcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
{
	struct ixppcib_softc *sc;

	sc = device_get_softc(dev);
	switch (which) {
	case PCIB_IVAR_DOMAIN:
		return (EINVAL);
	case PCIB_IVAR_BUS:
		sc->sc_bus = value;
		return (0);
	}

	return (ENOENT);
}

static int
ixppcib_setup_intr(device_t dev, device_t child, struct resource *ires,
    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
    void **cookiep)
{

	return (BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
	    filt, intr, arg, cookiep));
}

static int
ixppcib_teardown_intr(device_t dev, device_t child, struct resource *vec,
     void *cookie)
{

	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec, cookie));
}

static struct resource *
ixppcib_alloc_resource(device_t bus, device_t child, int type, int *rid,
    u_long start, u_long end, u_long count, u_int flags)
{
	struct ixppcib_softc *sc = device_get_softc(bus);
	struct rman *rmanp;
	struct resource *rv;

	rv = NULL;
	switch (type) {
	case SYS_RES_IRQ:
		rmanp = &sc->sc_irq_rman;
		break;

	case SYS_RES_IOPORT:
		rmanp = &sc->sc_io_rman;
		break;

	case SYS_RES_MEMORY:
		rmanp = &sc->sc_mem_rman;
		break;

	default:
		return (rv);
	}

	rv = rman_reserve_resource(rmanp, start, end, count, flags & ~RF_ACTIVE,
	    child);
	if (rv == NULL)
		return (NULL);
	rman_set_rid(rv, *rid);
	if (flags & RF_ACTIVE) {
		if (bus_activate_resource(child, type, *rid, rv)) {
			rman_release_resource(rv);
			return (NULL);
		}
	}

	return (rv);
}

static int
ixppcib_activate_resource(device_t bus, device_t child, int type, int rid,
    struct resource *r)
{

	struct ixppcib_softc *sc = device_get_softc(bus);

	switch (type) {
	case SYS_RES_IOPORT:
		rman_set_bustag(r, &sc->sc_pci_iot);
		rman_set_bushandle(r, rman_get_start(r));
		break;
	case SYS_RES_MEMORY:
		rman_set_bustag(r, &sc->sc_pci_memt);
		rman_set_bushandle(r, rman_get_bushandle(sc->sc_mem) +
		    (rman_get_start(r) - IXP425_PCI_MEM_HWBASE));
		break;
	}
		
	return (rman_activate_resource(r));
}

static int
ixppcib_deactivate_resource(device_t bus, device_t child, int type, int rid,
    struct resource *r)
{

	device_printf(bus, "%s called deactivate_resource (unexpected)\n",
	    device_get_nameunit(child));
	return (ENXIO);
}

static int
ixppcib_release_resource(device_t bus, device_t child, int type, int rid,
    struct resource *r)
{

	device_printf(bus, "%s called release_resource (unexpected)\n",
	    device_get_nameunit(child));
	return (ENXIO);
}

static void
ixppcib_conf_setup(struct ixppcib_softc *sc, int bus, int slot, int func,
    int reg)
{
	if (bus == 0) {
		/* configuration type 0 */
		PCI_CSR_WRITE_4(sc, PCI_NP_AD,
		    (1U << (32 - (slot & 0x1f))) |
		    ((func & 0x7) << 8) | (reg & ~3));
	} else {
		/* configuration type 1 */
		PCI_CSR_WRITE_4(sc, PCI_NP_AD,
		    (bus << 16) | (slot << 11) |
		    (func << 8) | (reg & ~3) | 1);
	}

}

static int
ixppcib_maxslots(device_t dev)
{

	return (PCI_SLOTMAX);
}

static u_int32_t
ixppcib_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
    int bytes)
{
	struct ixppcib_softc *sc = device_get_softc(dev);
	u_int32_t data, ret;

	ixppcib_conf_setup(sc, bus, slot, func, reg & ~3);

	PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_READ);
	ret = PCI_CSR_READ_4(sc, PCI_NP_RDATA);
	ret >>= (reg & 3) * 8;
	ret &= 0xffffffff >> ((4 - bytes) * 8);
#if 0
	device_printf(dev, "%s: %u:%u:%u %#x(%d) = %#x\n",
	    __func__, bus, slot, func, reg, bytes, ret);
#endif
	/* check & clear PCI abort */
	data = PCI_CSR_READ_4(sc, PCI_ISR);
	if (data & ISR_PFE) {
		PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_PFE);
		return (-1);
	}
	return (ret);
}

static const int byteenables[] = { 0, 0x10, 0x30, 0x70, 0xf0 };

static void
ixppcib_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
    u_int32_t val, int bytes)
{
	struct ixppcib_softc *sc = device_get_softc(dev);
	u_int32_t data;

#if 0
	device_printf(dev, "%s: %u:%u:%u %#x(%d) = %#x\n",
	    __func__, bus, slot, func, reg, bytes, val);
#endif
	ixppcib_conf_setup(sc, bus, slot, func, reg & ~3);

	/* Byte enables are active low, so not them first */
	PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_WRITE |
	    (~(byteenables[bytes] << (reg & 3)) & 0xf0));
	PCI_CSR_WRITE_4(sc, PCI_NP_WDATA, val << ((reg & 3) * 8));

	/* check & clear PCI abort */
	data = PCI_CSR_READ_4(sc, PCI_ISR);
	if (data & ISR_PFE)
		PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_PFE);
}

static int
ixppcib_route_interrupt(device_t bridge, device_t device, int pin)
{

	return (ixp425_md_route_interrupt(bridge, device, pin));
}

static device_method_t ixppcib_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,			ixppcib_probe),
	DEVMETHOD(device_attach,		ixppcib_attach),

	/* Bus interface */
	DEVMETHOD(bus_read_ivar,		ixppcib_read_ivar),
	DEVMETHOD(bus_write_ivar,		ixppcib_write_ivar),
	DEVMETHOD(bus_setup_intr,		ixppcib_setup_intr),
	DEVMETHOD(bus_teardown_intr,		ixppcib_teardown_intr),
	DEVMETHOD(bus_alloc_resource,		ixppcib_alloc_resource),
	DEVMETHOD(bus_activate_resource,	ixppcib_activate_resource),
	DEVMETHOD(bus_deactivate_resource,	ixppcib_deactivate_resource),
	DEVMETHOD(bus_release_resource,		ixppcib_release_resource),
	/* DEVMETHOD(bus_get_dma_tag,		ixppcib_get_dma_tag), */

	/* pcib interface */
	DEVMETHOD(pcib_maxslots,		ixppcib_maxslots),
	DEVMETHOD(pcib_read_config,		ixppcib_read_config),
	DEVMETHOD(pcib_write_config,		ixppcib_write_config),
	DEVMETHOD(pcib_route_interrupt,		ixppcib_route_interrupt),

	DEVMETHOD_END
};

static driver_t ixppcib_driver = {
	"pcib",
	ixppcib_methods,
	sizeof(struct ixppcib_softc),
};
static devclass_t ixppcib_devclass;

DRIVER_MODULE(ixppcib, ixp, ixppcib_driver, ixppcib_devclass, 0, 0);
OpenPOWER on IntegriCloud