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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/*-
* Copyright (c) 2003 Hidetoshi Shimokawa
* Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
* 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 acknowledgement as bellow:
*
* This product includes software developed by K. Kobayashi and H. SHimokawa
*
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*
* $FreeBSD$
*/
#define BOUNCE_BUFFER_TEST 0
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/queue.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/malloc.h>
#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
#include <sys/lock.h>
#include <sys/mutex.h>
#endif
#include <machine/resource.h>
#if defined(__DragonFly__) || __FreeBSD_version < 500000
#include <machine/clock.h> /* for DELAY() */
#endif
#ifdef __DragonFly__
#include <bus/pci/pcivar.h>
#include <bus/pci/pcireg.h>
#include "firewire.h"
#include "firewirereg.h"
#include "fwdma.h"
#include "fwohcireg.h"
#include "fwohcivar.h"
#else
#if __FreeBSD_version < 500000
#include <pci/pcivar.h>
#include <pci/pcireg.h>
#else
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#endif
#include <dev/firewire/firewire.h>
#include <dev/firewire/firewirereg.h>
#include <dev/firewire/fwdma.h>
#include <dev/firewire/fwohcireg.h>
#include <dev/firewire/fwohcivar.h>
#endif
static int fwohci_pci_attach(device_t self);
static int fwohci_pci_detach(device_t self);
/*
* The probe routine.
*/
static int
fwohci_pci_probe( device_t dev )
{
#if 1
uint32_t id;
id = pci_get_devid(dev);
if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
device_set_desc(dev, "National Semiconductor CS4210");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
device_set_desc(dev, "NEC uPD72861");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
device_set_desc(dev, "NEC uPD72871/2");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
device_set_desc(dev, "NEC uPD72870");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72873)) {
device_set_desc(dev, "NEC uPD72873");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
device_set_desc(dev, "NEC uPD72874");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
/* It has no real identifier, using device id. */
device_set_desc(dev, "SiS 7007");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
device_set_desc(dev, "Texas Instruments TSB12LV22");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
device_set_desc(dev, "Texas Instruments TSB12LV23");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
device_set_desc(dev, "Texas Instruments TSB12LV26");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
device_set_desc(dev, "Texas Instruments TSB43AA22");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
device_set_desc(dev, "Texas Instruments TSB43AB22/A");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
device_set_desc(dev, "Texas Instruments TSB43AB23");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
device_set_desc(dev, "Texas Instruments TSB82AA2");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
device_set_desc(dev, "Texas Instruments PCI4450");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
device_set_desc(dev, "Texas Instruments PCI4410A");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
device_set_desc(dev, "Texas Instruments PCI4451");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
device_printf(dev, "Sony i.LINK (CXD1947) not supported\n");
return ENXIO;
}
if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
device_set_desc(dev, "Sony i.LINK (CXD3222)");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
device_set_desc(dev, "VIA Fire II (VT6306)");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
device_set_desc(dev, "Ricoh R5C551");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
device_set_desc(dev, "Ricoh R5C552");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
device_set_desc(dev, "Apple Pangea");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
device_set_desc(dev, "Apple UniNorth");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
device_set_desc(dev, "Lucent FW322/323");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
device_set_desc(dev, "Intel 82372FB");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
return BUS_PROBE_DEFAULT;
}
if (id == (FW_VENDORID_SUN | FW_DEVICE_PCIO2FW)) {
device_set_desc(dev, "Sun PCIO-2");
return BUS_PROBE_DEFAULT;
}
#endif
if (pci_get_class(dev) == PCIC_SERIALBUS
&& pci_get_subclass(dev) == PCIS_SERIALBUS_FW
&& pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
if (bootverbose)
device_printf(dev, "vendor=%x, dev=%x\n",
pci_get_vendor(dev), pci_get_device(dev));
device_set_desc(dev, "1394 Open Host Controller Interface");
return BUS_PROBE_DEFAULT;
}
return ENXIO;
}
#if defined(__DragonFly__) || __FreeBSD_version < 500000
static void
fwohci_dummy_intr(void *arg)
{
/* XXX do nothing */
}
#endif
static int
fwohci_pci_init(device_t self)
{
int olatency, latency, ocache_line, cache_line;
uint16_t cmd;
cmd = pci_read_config(self, PCIR_COMMAND, 2);
cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN |
PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN;
#if 1 /* for broken hardware */
cmd &= ~PCIM_CMD_MWRICEN;
cmd &= ~(PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN);
#endif
pci_write_config(self, PCIR_COMMAND, cmd, 2);
/*
* Some Sun PCIO-2 FireWire controllers have their intpin register
* bogusly set to 0, although it should be 3. Correct that.
*/
if (pci_get_devid(self) == (FW_VENDORID_SUN | FW_DEVICE_PCIO2FW) &&
pci_get_intpin(self) == 0)
pci_set_intpin(self, 3);
latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
#define DEF_LATENCY 0x20
if (olatency < DEF_LATENCY) {
latency = DEF_LATENCY;
pci_write_config(self, PCIR_LATTIMER, latency, 1);
}
cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
#define DEF_CACHE_LINE 8
if (ocache_line < DEF_CACHE_LINE) {
cache_line = DEF_CACHE_LINE;
pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
}
if (firewire_debug) {
device_printf(self, "latency timer %d -> %d.\n",
olatency, latency);
device_printf(self, "cache size %d -> %d.\n",
ocache_line, cache_line);
}
return 0;
}
static int
fwohci_pci_attach(device_t self)
{
fwohci_softc_t *sc = device_get_softc(self);
int err;
int rid;
#if defined(__DragonFly__) || __FreeBSD_version < 500000
int intr;
/* For the moment, put in a message stating what is wrong */
intr = pci_read_config(self, PCIR_INTLINE, 1);
if (intr == 0 || intr == 255) {
device_printf(self, "Invalid irq %d\n", intr);
#ifdef __i386__
device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
#endif
}
#endif
#if 0
if (bootverbose)
firewire_debug = bootverbose;
#endif
fwohci_pci_init(self);
rid = PCI_CBMEM;
#if __FreeBSD_version >= 502109
sc->bsr = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
#else
sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
0, ~0, 1, RF_ACTIVE);
#endif
if (!sc->bsr) {
device_printf(self, "Could not map memory\n");
return ENXIO;
}
sc->bst = rman_get_bustag(sc->bsr);
sc->bsh = rman_get_bushandle(sc->bsr);
rid = 0;
#if __FreeBSD_version >= 502109
sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
RF_SHAREABLE | RF_ACTIVE);
#else
sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
RF_SHAREABLE | RF_ACTIVE);
#endif
if (sc->irq_res == NULL) {
device_printf(self, "Could not allocate irq\n");
fwohci_pci_detach(self);
return ENXIO;
}
err = bus_setup_intr(self, sc->irq_res,
#if FWOHCI_TASKQUEUE
INTR_TYPE_NET | INTR_MPSAFE,
#else
INTR_TYPE_NET,
#endif
NULL, (driver_intr_t *) fwohci_intr, sc, &sc->ih);
#if defined(__DragonFly__) || __FreeBSD_version < 500000
/* XXX splcam() should mask this irq for sbp.c*/
err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
(driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
/* XXX splbio() should mask this irq for physio()/fwmem_strategy() */
err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
(driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio);
#endif
if (err) {
device_printf(self, "Could not setup irq, %d\n", err);
fwohci_pci_detach(self);
return ENXIO;
}
err = bus_dma_tag_create(
#if defined(__FreeBSD__) && __FreeBSD_version >= 700020
/*parent*/bus_get_dma_tag(self),
#else
/*parent*/NULL,
#endif
/*alignment*/1,
/*boundary*/0,
#if BOUNCE_BUFFER_TEST
/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
#else
/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
#endif
/*highaddr*/BUS_SPACE_MAXADDR,
/*filter*/NULL, /*filterarg*/NULL,
/*maxsize*/0x100000,
/*nsegments*/0x20,
/*maxsegsz*/0x8000,
/*flags*/BUS_DMA_ALLOCNOW,
#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
/*lockfunc*/busdma_lock_mutex,
/*lockarg*/&Giant,
#endif
&sc->fc.dmat);
if (err != 0) {
printf("fwohci_pci_attach: Could not allocate DMA tag "
"- error %d\n", err);
return (ENOMEM);
}
err = fwohci_init(sc, self);
if (err) {
device_printf(self, "fwohci_init failed with err=%d\n", err);
fwohci_pci_detach(self);
return EIO;
}
/* probe and attach a child device(firewire) */
bus_generic_probe(self);
bus_generic_attach(self);
return 0;
}
static int
fwohci_pci_detach(device_t self)
{
fwohci_softc_t *sc = device_get_softc(self);
int s;
s = splfw();
if (sc->bsr)
fwohci_stop(sc, self);
bus_generic_detach(self);
if (sc->fc.bdev) {
device_delete_child(self, sc->fc.bdev);
sc->fc.bdev = NULL;
}
/* disable interrupts that might have been switched on */
if (sc->bst && sc->bsh)
bus_space_write_4(sc->bst, sc->bsh,
FWOHCI_INTMASKCLR, OHCI_INT_EN);
if (sc->irq_res) {
int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
if (err)
/* XXX or should we panic? */
device_printf(self, "Could not tear down irq, %d\n",
err);
#if defined(__DragonFly__) || __FreeBSD_version < 500000
bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
bus_teardown_intr(self, sc->irq_res, sc->ih_bio);
#endif
sc->ih = NULL;
}
if (sc->irq_res) {
bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
sc->irq_res = NULL;
}
if (sc->bsr) {
bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
sc->bsr = NULL;
sc->bst = 0;
sc->bsh = 0;
}
fwohci_detach(sc, self);
splx(s);
return 0;
}
static int
fwohci_pci_suspend(device_t dev)
{
fwohci_softc_t *sc = device_get_softc(dev);
int err;
device_printf(dev, "fwohci_pci_suspend\n");
err = bus_generic_suspend(dev);
if (err)
return err;
fwohci_stop(sc, dev);
return 0;
}
static int
fwohci_pci_resume(device_t dev)
{
fwohci_softc_t *sc = device_get_softc(dev);
fwohci_pci_init(dev);
fwohci_resume(sc, dev);
return 0;
}
static int
fwohci_pci_shutdown(device_t dev)
{
fwohci_softc_t *sc = device_get_softc(dev);
bus_generic_shutdown(dev);
fwohci_stop(sc, dev);
return 0;
}
static device_t
fwohci_pci_add_child(device_t dev, int order, const char *name, int unit)
{
struct fwohci_softc *sc;
device_t child;
int s, err = 0;
sc = (struct fwohci_softc *)device_get_softc(dev);
child = device_add_child(dev, name, unit);
if (child == NULL)
return (child);
sc->fc.bdev = child;
device_set_ivars(child, (void *)&sc->fc);
err = device_probe_and_attach(child);
if (err) {
device_printf(dev, "probe_and_attach failed with err=%d\n",
err);
fwohci_pci_detach(dev);
device_delete_child(dev, child);
return NULL;
}
/* XXX
* Clear the bus reset event flag to start transactions even when
* interrupt is disabled during the boot process.
*/
DELAY(250); /* 2 cycles */
s = splfw();
fwohci_poll((void *)sc, 0, -1);
splx(s);
return (child);
}
static device_method_t fwohci_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, fwohci_pci_probe),
DEVMETHOD(device_attach, fwohci_pci_attach),
DEVMETHOD(device_detach, fwohci_pci_detach),
DEVMETHOD(device_suspend, fwohci_pci_suspend),
DEVMETHOD(device_resume, fwohci_pci_resume),
DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
/* Bus interface */
DEVMETHOD(bus_add_child, fwohci_pci_add_child),
DEVMETHOD(bus_print_child, bus_generic_print_child),
{ 0, 0 }
};
static driver_t fwohci_driver = {
"fwohci",
fwohci_methods,
sizeof(fwohci_softc_t),
};
static devclass_t fwohci_devclass;
#ifdef FWOHCI_MODULE
MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
#endif
DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);
|