summaryrefslogtreecommitdiff
path: root/sys/arch/playstation2/dev/ohci_sbus.c
blob: bccd2c855feedc8a5933a31a48ecb023e96413af (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
/*	$NetBSD: ohci_sbus.c,v 1.16 2021/08/07 16:19:02 thorpej Exp $	*/

/*-
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by UCHIYAMA Yasushi.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 THE FOUNDATION OR CONTRIBUTORS
 * 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>
__KERNEL_RCSID(0, "$NetBSD: ohci_sbus.c,v 1.16 2021/08/07 16:19:02 thorpej Exp $");

#include <sys/param.h>
#include <sys/kmem.h>

/* bus_dma */
#include <sys/mbuf.h>
#include <uvm/uvm_extern.h>

#define _PLAYSTATION2_BUS_DMA_PRIVATE
#include <machine/bus.h>
#include <machine/autoconf.h>

#include <mips/cpuregs.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>

#include <dev/usb/ohcireg.h>
#include <dev/usb/ohcivar.h>

#include <playstation2/ee/sifvar.h>	/* DMA staff */
#include <playstation2/ee/dmacvar.h>
#include <playstation2/dev/sbusvar.h>

#ifdef DEBUG
#define STATIC
#else
#define STATIC static
#endif

#define	SBUS_OHCI_REGBASE	MIPS_PHYS_TO_KSEG1(0x1f801600)
#define SBUS_OHCI_REGSIZE	0x1000

STATIC int ohci_sbus_match(struct device *, struct cfdata *, void *);
STATIC void ohci_sbus_attach(struct device *, struct device *, void *);

STATIC void _ohci_sbus_map_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    bus_size_t, int);
STATIC int _ohci_sbus_mem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
    bus_size_t, bus_dma_segment_t *, int, int *, int);
STATIC void _ohci_sbus_mem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
STATIC int _ohci_sbus_mem_map(bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
    void **, int);
STATIC void _ohci_sbus_mem_unmap(bus_dma_tag_t, void *, size_t);

struct playstation2_bus_dma_tag ohci_bus_dma_tag = {
	_bus_dmamap_create,
	_bus_dmamap_destroy,
	_bus_dmamap_load,
	_bus_dmamap_load_mbuf,
	_bus_dmamap_load_uio,
	_bus_dmamap_load_raw,
	_bus_dmamap_unload,
	_ohci_sbus_map_sync,
	_ohci_sbus_mem_alloc,
	_ohci_sbus_mem_free,
	_ohci_sbus_mem_map,
	_ohci_sbus_mem_unmap,
	_bus_dmamem_mmap,
};

struct ohci_dma_segment {
	struct iopdma_segment ds_iopdma_seg;

	LIST_ENTRY(ohci_dma_segment) ds_link;
};

struct ohci_sbus_softc {
	struct ohci_softc sc;

	LIST_HEAD(, ohci_dma_segment) sc_dmaseg_head;
};

CFATTACH_DECL_NEW(ohci_sbus, sizeof(struct ohci_sbus_softc),
    ohci_sbus_match, ohci_sbus_attach, NULL, NULL);

int
ohci_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
{

	return 1;
}

void
ohci_sbus_attach(struct device *parent, struct device *self, void *aux)
{
	struct ohci_sbus_softc *sc = device_private(self);

	printf("\n");

	sc->sc.sc_dev = self;
	sc->sc.sc_bus.ub_hcpriv = sc;

	sc->sc.iot = bus_space_create(0, "OHCI I/O space", SBUS_OHCI_REGBASE,
	    SBUS_OHCI_REGSIZE);
	sc->sc.ioh = SBUS_OHCI_REGBASE;

	ohci_bus_dma_tag._dmachip_cookie = sc;
	sc->sc.sc_bus.ub_dmatag = &ohci_bus_dma_tag;

	/* Disable interrupts, so we don't can any spurious ones. */
	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
	    OHCI_ALL_INTRS);

	sbus_intr_establish(SBUS_IRQ_USB, ohci_intr, sc);

	/* IOP/EE DMA relay segment list */
	LIST_INIT(&sc->sc_dmaseg_head);

	int err = ohci_init(&sc->sc);

	if (err) {
		printf(": init failed. error=%d\n", err);
		return;
	}

	/* Attach usb device. */
	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint,
	    CFARGS_NONE);
}

void
_ohci_sbus_map_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    bus_size_t len, int ops)
{

	dmac_sync_buffer(); /* XXX over flush */
}

int
_ohci_sbus_mem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    int flags)
{
	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
	struct ohci_dma_segment *ds;
	struct iopdma_segment *iopdma_seg;
	int error;

	KDASSERT(sc);
	ds = kmem_intr_alloc(sizeof(struct ohci_dma_segment), KM_NOSLEEP);
	if (ds == NULL)
		return 1;
	/*
	 * Allocate DMA Area (IOP DMA Area <-> SIF DMA <-> EE DMA Area)
	 */
	iopdma_seg = &ds->ds_iopdma_seg;
	error = iopdma_allocate_buffer(iopdma_seg, size);

	if (error) {
		kmem_intr_free(ds, sizeof(*ds));
		return 1;
	}

	segs[0].ds_len	  = iopdma_seg->size;
	segs[0].ds_addr	  = iopdma_seg->iop_paddr;
	segs[0]._ds_vaddr = iopdma_seg->ee_vaddr;

	LIST_INSERT_HEAD(&sc->sc_dmaseg_head, ds, ds_link);

	*rsegs = 1;

	return 0;
}

void
_ohci_sbus_mem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
{
	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
	struct ohci_dma_segment *ds;
	paddr_t addr = segs[0].ds_addr;

	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
	    ds = LIST_NEXT(ds, ds_link)) {
		if (ds->ds_iopdma_seg.iop_paddr == addr) {
			iopdma_free_buffer(&ds->ds_iopdma_seg);

			LIST_REMOVE(ds, ds_link);
			kmem_intr_free(ds, sizeof(*ds));
			return;
		}
	}

	panic("_dmamem_free: can't find corresponding handle.");
	/* NOTREACHED */
}

int
_ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
    void **kvap, int flags)
{
	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
	struct ohci_dma_segment *ds;
	paddr_t addr = segs[0].ds_addr;

	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
	    ds = LIST_NEXT(ds, ds_link)) {
		if (ds->ds_iopdma_seg.iop_paddr == addr) {

			*kvap = (void *)ds->ds_iopdma_seg.ee_vaddr;

			return 0;
		}
	}

	return 1;
}

void
_ohci_sbus_mem_unmap(bus_dma_tag_t t, void *kva, size_t size)
{
	/* nothing to do */
}