2 * ohci1394.c - driver for OHCI 1394 boards
3 * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>
4 * Gord Peters <GordPeters@smarttech.com>
5 * 2001 Ben Collins <bcollins@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Things known to be working:
24 * . Async Request Transmit
25 * . Async Response Receive
26 * . Async Request Receive
27 * . Async Response Transmit
29 * . DMA mmap for iso receive
30 * . Config ROM generation
32 * Things implemented, but still in test phase:
34 * . Async Stream Packets Transmit (Receive done via Iso interface)
36 * Things not implemented:
37 * . DMA error recovery
40 * . devctl BUS_RESET arg confusion (reset type or root holdoff?)
41 * added LONG_RESET_ROOT and SHORT_RESET_ROOT for root holdoff --kk
47 * Adam J Richter <adam@yggdrasil.com>
48 * . Use of pci_class to find device
50 * Emilie Chung <emilie.chung@axis.com>
51 * . Tip on Async Request Filter
53 * Pascal Drolet <pascal.drolet@informission.ca>
54 * . Various tips for optimization and functionnalities
56 * Robert Ficklin <rficklin@westengineering.com>
57 * . Loop in irq_handler
59 * James Goodwin <jamesg@Filanet.com>
60 * . Various tips on initialization, self-id reception, etc.
62 * Albrecht Dress <ad@mpifr-bonn.mpg.de>
63 * . Apple PowerBook detection
65 * Daniel Kobras <daniel.kobras@student.uni-tuebingen.de>
66 * . Reset the board properly before leaving + misc cleanups
68 * Leon van Stuivenberg <leonvs@iae.nl>
71 * Ben Collins <bcollins@debian.org>
72 * . Working big-endian support
73 * . Updated to 2.4.x module scheme (PCI aswell)
74 * . Config ROM generation
76 * Manfred Weihs <weihs@ict.tuwien.ac.at>
77 * . Reworked code for initiating bus resets
78 * (long, short, with or without hold-off)
80 * Nandu Santhi <contactnandu@users.sourceforge.net>
81 * . Added support for nVidia nForce2 onboard Firewire chipset
85 #include <linux/config.h>
86 #include <linux/kernel.h>
87 #include <linux/list.h>
88 #include <linux/slab.h>
89 #include <linux/interrupt.h>
90 #include <linux/wait.h>
91 #include <linux/errno.h>
92 #include <linux/module.h>
93 #include <linux/moduleparam.h>
94 #include <linux/pci.h>
96 #include <linux/poll.h>
97 #include <asm/byteorder.h>
98 #include <asm/atomic.h>
99 #include <asm/uaccess.h>
100 #include <linux/delay.h>
101 #include <linux/spinlock.h>
103 #include <asm/pgtable.h>
104 #include <asm/page.h>
106 #include <linux/sched.h>
107 #include <linux/types.h>
108 #include <linux/vmalloc.h>
109 #include <linux/init.h>
111 #ifdef CONFIG_PPC_PMAC
112 #include <asm/machdep.h>
113 #include <asm/pmac_feature.h>
114 #include <asm/prom.h>
115 #include <asm/pci-bridge.h>
119 #include "ieee1394.h"
120 #include "ieee1394_types.h"
124 #include "ieee1394_core.h"
125 #include "highlevel.h"
126 #include "ohci1394.h"
128 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
129 #define OHCI1394_DEBUG
136 #ifdef OHCI1394_DEBUG
137 #define DBGMSG(fmt, args...) \
138 printk(KERN_INFO "%s: fw-host%d: " fmt "\n" , OHCI1394_DRIVER_NAME, ohci->host->id , ## args)
140 #define DBGMSG(fmt, args...)
143 #ifdef CONFIG_IEEE1394_OHCI_DMA_DEBUG
144 #define OHCI_DMA_ALLOC(fmt, args...) \
145 HPSB_ERR("%s(%s)alloc(%d): "fmt, OHCI1394_DRIVER_NAME, __FUNCTION__, \
146 ++global_outstanding_dmas, ## args)
147 #define OHCI_DMA_FREE(fmt, args...) \
148 HPSB_ERR("%s(%s)free(%d): "fmt, OHCI1394_DRIVER_NAME, __FUNCTION__, \
149 --global_outstanding_dmas, ## args)
150 static int global_outstanding_dmas
= 0;
152 #define OHCI_DMA_ALLOC(fmt, args...)
153 #define OHCI_DMA_FREE(fmt, args...)
156 /* print general (card independent) information */
157 #define PRINT_G(level, fmt, args...) \
158 printk(level "%s: " fmt "\n" , OHCI1394_DRIVER_NAME , ## args)
160 /* print card specific information */
161 #define PRINT(level, fmt, args...) \
162 printk(level "%s: fw-host%d: " fmt "\n" , OHCI1394_DRIVER_NAME, ohci->host->id , ## args)
164 /* Module Parameters */
165 static int phys_dma
= 1;
166 module_param(phys_dma
, int, 0444);
167 MODULE_PARM_DESC(phys_dma
, "Enable physical dma (default = 1).");
169 static void dma_trm_tasklet(unsigned long data
);
170 static void dma_trm_reset(struct dma_trm_ctx
*d
);
172 static int alloc_dma_rcv_ctx(struct ti_ohci
*ohci
, struct dma_rcv_ctx
*d
,
173 enum context_type type
, int ctx
, int num_desc
,
174 int buf_size
, int split_buf_size
, int context_base
);
175 static void stop_dma_rcv_ctx(struct dma_rcv_ctx
*d
);
176 static void free_dma_rcv_ctx(struct dma_rcv_ctx
*d
);
178 static int alloc_dma_trm_ctx(struct ti_ohci
*ohci
, struct dma_trm_ctx
*d
,
179 enum context_type type
, int ctx
, int num_desc
,
182 static void ohci1394_pci_remove(struct pci_dev
*pdev
);
184 #ifndef __LITTLE_ENDIAN
185 static unsigned hdr_sizes
[] =
187 3, /* TCODE_WRITEQ */
188 4, /* TCODE_WRITEB */
189 3, /* TCODE_WRITE_RESPONSE */
193 3, /* TCODE_READQ_RESPONSE */
194 4, /* TCODE_READB_RESPONSE */
195 1, /* TCODE_CYCLE_START (???) */
196 4, /* TCODE_LOCK_REQUEST */
197 2, /* TCODE_ISO_DATA */
198 4, /* TCODE_LOCK_RESPONSE */
202 static inline void packet_swab(quadlet_t
*data
, int tcode
)
204 size_t size
= hdr_sizes
[tcode
];
206 if (tcode
> TCODE_LOCK_RESPONSE
|| hdr_sizes
[tcode
] == 0)
210 data
[size
] = swab32(data
[size
]);
213 /* Don't waste cycles on same sex byte swaps */
214 #define packet_swab(w,x)
215 #endif /* !LITTLE_ENDIAN */
217 /***********************************
218 * IEEE-1394 functionality section *
219 ***********************************/
221 static u8
get_phy_reg(struct ti_ohci
*ohci
, u8 addr
)
227 spin_lock_irqsave (&ohci
->phy_reg_lock
, flags
);
229 reg_write(ohci
, OHCI1394_PhyControl
, (addr
<< 8) | 0x00008000);
231 for (i
= 0; i
< OHCI_LOOP_COUNT
; i
++) {
232 if (reg_read(ohci
, OHCI1394_PhyControl
) & 0x80000000)
238 r
= reg_read(ohci
, OHCI1394_PhyControl
);
240 if (i
>= OHCI_LOOP_COUNT
)
241 PRINT (KERN_ERR
, "Get PHY Reg timeout [0x%08x/0x%08x/%d]",
242 r
, r
& 0x80000000, i
);
244 spin_unlock_irqrestore (&ohci
->phy_reg_lock
, flags
);
246 return (r
& 0x00ff0000) >> 16;
249 static void set_phy_reg(struct ti_ohci
*ohci
, u8 addr
, u8 data
)
255 spin_lock_irqsave (&ohci
->phy_reg_lock
, flags
);
257 reg_write(ohci
, OHCI1394_PhyControl
, (addr
<< 8) | data
| 0x00004000);
259 for (i
= 0; i
< OHCI_LOOP_COUNT
; i
++) {
260 r
= reg_read(ohci
, OHCI1394_PhyControl
);
261 if (!(r
& 0x00004000))
267 if (i
== OHCI_LOOP_COUNT
)
268 PRINT (KERN_ERR
, "Set PHY Reg timeout [0x%08x/0x%08x/%d]",
269 r
, r
& 0x00004000, i
);
271 spin_unlock_irqrestore (&ohci
->phy_reg_lock
, flags
);
276 /* Or's our value into the current value */
277 static void set_phy_reg_mask(struct ti_ohci
*ohci
, u8 addr
, u8 data
)
281 old
= get_phy_reg (ohci
, addr
);
283 set_phy_reg (ohci
, addr
, old
);
288 static void handle_selfid(struct ti_ohci
*ohci
, struct hpsb_host
*host
,
289 int phyid
, int isroot
)
291 quadlet_t
*q
= ohci
->selfid_buf_cpu
;
292 quadlet_t self_id_count
=reg_read(ohci
, OHCI1394_SelfIDCount
);
296 /* Check status of self-id reception */
298 if (ohci
->selfid_swap
)
299 q0
= le32_to_cpu(q
[0]);
303 if ((self_id_count
& 0x80000000) ||
304 ((self_id_count
& 0x00FF0000) != (q0
& 0x00FF0000))) {
306 "Error in reception of SelfID packets [0x%08x/0x%08x] (count: %d)",
307 self_id_count
, q0
, ohci
->self_id_errors
);
309 /* Tip by James Goodwin <jamesg@Filanet.com>:
310 * We had an error, generate another bus reset in response. */
311 if (ohci
->self_id_errors
<OHCI1394_MAX_SELF_ID_ERRORS
) {
312 set_phy_reg_mask (ohci
, 1, 0x40);
313 ohci
->self_id_errors
++;
316 "Too many errors on SelfID error reception, giving up!");
321 /* SelfID Ok, reset error counter. */
322 ohci
->self_id_errors
= 0;
324 size
= ((self_id_count
& 0x00001FFC) >> 2) - 1;
328 if (ohci
->selfid_swap
) {
329 q0
= le32_to_cpu(q
[0]);
330 q1
= le32_to_cpu(q
[1]);
337 DBGMSG ("SelfID packet 0x%x received", q0
);
338 hpsb_selfid_received(host
, cpu_to_be32(q0
));
339 if (((q0
& 0x3f000000) >> 24) == phyid
)
340 DBGMSG ("SelfID for this node is 0x%08x", q0
);
343 "SelfID is inconsistent [0x%08x/0x%08x]", q0
, q1
);
349 DBGMSG("SelfID complete");
354 static void ohci_soft_reset(struct ti_ohci
*ohci
) {
357 reg_write(ohci
, OHCI1394_HCControlSet
, OHCI1394_HCControl_softReset
);
359 for (i
= 0; i
< OHCI_LOOP_COUNT
; i
++) {
360 if (!(reg_read(ohci
, OHCI1394_HCControlSet
) & OHCI1394_HCControl_softReset
))
364 DBGMSG ("Soft reset finished");
368 /* Generate the dma receive prgs and start the context */
369 static void initialize_dma_rcv_ctx(struct dma_rcv_ctx
*d
, int generate_irq
)
371 struct ti_ohci
*ohci
= (struct ti_ohci
*)(d
->ohci
);
374 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
376 for (i
=0; i
<d
->num_desc
; i
++) {
379 c
= DMA_CTL_INPUT_MORE
| DMA_CTL_UPDATE
| DMA_CTL_BRANCH
;
383 d
->prg_cpu
[i
]->control
= cpu_to_le32(c
| d
->buf_size
);
385 /* End of descriptor list? */
386 if (i
+ 1 < d
->num_desc
) {
387 d
->prg_cpu
[i
]->branchAddress
=
388 cpu_to_le32((d
->prg_bus
[i
+1] & 0xfffffff0) | 0x1);
390 d
->prg_cpu
[i
]->branchAddress
=
391 cpu_to_le32((d
->prg_bus
[0] & 0xfffffff0));
394 d
->prg_cpu
[i
]->address
= cpu_to_le32(d
->buf_bus
[i
]);
395 d
->prg_cpu
[i
]->status
= cpu_to_le32(d
->buf_size
);
401 if (d
->type
== DMA_CTX_ISO
) {
402 /* Clear contextControl */
403 reg_write(ohci
, d
->ctrlClear
, 0xffffffff);
405 /* Set bufferFill, isochHeader, multichannel for IR context */
406 reg_write(ohci
, d
->ctrlSet
, 0xd0000000);
408 /* Set the context match register to match on all tags */
409 reg_write(ohci
, d
->ctxtMatch
, 0xf0000000);
411 /* Clear the multi channel mask high and low registers */
412 reg_write(ohci
, OHCI1394_IRMultiChanMaskHiClear
, 0xffffffff);
413 reg_write(ohci
, OHCI1394_IRMultiChanMaskLoClear
, 0xffffffff);
415 /* Set up isoRecvIntMask to generate interrupts */
416 reg_write(ohci
, OHCI1394_IsoRecvIntMaskSet
, 1 << d
->ctx
);
419 /* Tell the controller where the first AR program is */
420 reg_write(ohci
, d
->cmdPtr
, d
->prg_bus
[0] | 0x1);
423 reg_write(ohci
, d
->ctrlSet
, 0x00008000);
425 DBGMSG("Receive DMA ctx=%d initialized", d
->ctx
);
428 /* Initialize the dma transmit context */
429 static void initialize_dma_trm_ctx(struct dma_trm_ctx
*d
)
431 struct ti_ohci
*ohci
= (struct ti_ohci
*)(d
->ohci
);
433 /* Stop the context */
434 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
438 d
->free_prgs
= d
->num_desc
;
439 d
->branchAddrPtr
= NULL
;
440 INIT_LIST_HEAD(&d
->fifo_list
);
441 INIT_LIST_HEAD(&d
->pending_list
);
443 if (d
->type
== DMA_CTX_ISO
) {
444 /* enable interrupts */
445 reg_write(ohci
, OHCI1394_IsoXmitIntMaskSet
, 1 << d
->ctx
);
448 DBGMSG("Transmit DMA ctx=%d initialized", d
->ctx
);
451 /* Count the number of available iso contexts */
452 static int get_nb_iso_ctx(struct ti_ohci
*ohci
, int reg
)
457 reg_write(ohci
, reg
, 0xffffffff);
458 tmp
= reg_read(ohci
, reg
);
460 DBGMSG("Iso contexts reg: %08x implemented: %08x", reg
, tmp
);
462 /* Count the number of contexts */
463 for (i
=0; i
<32; i
++) {
470 /* Global initialization */
471 static void ohci_initialize(struct ti_ohci
*ohci
)
477 spin_lock_init(&ohci
->phy_reg_lock
);
479 /* Put some defaults to these undefined bus options */
480 buf
= reg_read(ohci
, OHCI1394_BusOptions
);
481 buf
|= 0x60000000; /* Enable CMC and ISC */
482 if (hpsb_disable_irm
)
485 buf
|= 0x80000000; /* Enable IRMC */
486 buf
&= ~0x00ff0000; /* XXX: Set cyc_clk_acc to zero for now */
487 buf
&= ~0x18000000; /* Disable PMC and BMC */
488 reg_write(ohci
, OHCI1394_BusOptions
, buf
);
490 /* Set the bus number */
491 reg_write(ohci
, OHCI1394_NodeID
, 0x0000ffc0);
493 /* Enable posted writes */
494 reg_write(ohci
, OHCI1394_HCControlSet
, OHCI1394_HCControl_postedWriteEnable
);
496 /* Clear link control register */
497 reg_write(ohci
, OHCI1394_LinkControlClear
, 0xffffffff);
499 /* Enable cycle timer and cycle master and set the IRM
500 * contender bit in our self ID packets if appropriate. */
501 reg_write(ohci
, OHCI1394_LinkControlSet
,
502 OHCI1394_LinkControl_CycleTimerEnable
|
503 OHCI1394_LinkControl_CycleMaster
);
504 i
= get_phy_reg(ohci
, 4) | PHY_04_LCTRL
;
505 if (hpsb_disable_irm
)
506 i
&= ~PHY_04_CONTENDER
;
508 i
|= PHY_04_CONTENDER
;
509 set_phy_reg(ohci
, 4, i
);
511 /* Set up self-id dma buffer */
512 reg_write(ohci
, OHCI1394_SelfIDBuffer
, ohci
->selfid_buf_bus
);
514 /* enable self-id and phys */
515 reg_write(ohci
, OHCI1394_LinkControlSet
, OHCI1394_LinkControl_RcvSelfID
|
516 OHCI1394_LinkControl_RcvPhyPkt
);
518 /* Set the Config ROM mapping register */
519 reg_write(ohci
, OHCI1394_ConfigROMmap
, ohci
->csr_config_rom_bus
);
521 /* Now get our max packet size */
522 ohci
->max_packet_size
=
523 1<<(((reg_read(ohci
, OHCI1394_BusOptions
)>>12)&0xf)+1);
525 /* Don't accept phy packets into AR request context */
526 reg_write(ohci
, OHCI1394_LinkControlClear
, 0x00000400);
528 /* Clear the interrupt mask */
529 reg_write(ohci
, OHCI1394_IsoRecvIntMaskClear
, 0xffffffff);
530 reg_write(ohci
, OHCI1394_IsoRecvIntEventClear
, 0xffffffff);
532 /* Clear the interrupt mask */
533 reg_write(ohci
, OHCI1394_IsoXmitIntMaskClear
, 0xffffffff);
534 reg_write(ohci
, OHCI1394_IsoXmitIntEventClear
, 0xffffffff);
536 /* Initialize AR dma */
537 initialize_dma_rcv_ctx(&ohci
->ar_req_context
, 0);
538 initialize_dma_rcv_ctx(&ohci
->ar_resp_context
, 0);
540 /* Initialize AT dma */
541 initialize_dma_trm_ctx(&ohci
->at_req_context
);
542 initialize_dma_trm_ctx(&ohci
->at_resp_context
);
544 /* Initialize IR Legacy DMA channel mask */
545 ohci
->ir_legacy_channels
= 0;
547 /* Accept AR requests from all nodes */
548 reg_write(ohci
, OHCI1394_AsReqFilterHiSet
, 0x80000000);
550 /* Set the address range of the physical response unit.
551 * Most controllers do not implement it as a writable register though.
552 * They will keep a hardwired offset of 0x00010000 and show 0x0 as
554 * To actually enable physical responses is the job of our interrupt
555 * handler which programs the physical request filter. */
556 reg_write(ohci
, OHCI1394_PhyUpperBound
,
557 OHCI1394_PHYS_UPPER_BOUND_PROGRAMMED
>> 16);
559 DBGMSG("physUpperBoundOffset=%08x",
560 reg_read(ohci
, OHCI1394_PhyUpperBound
));
562 /* Specify AT retries */
563 reg_write(ohci
, OHCI1394_ATRetries
,
564 OHCI1394_MAX_AT_REQ_RETRIES
|
565 (OHCI1394_MAX_AT_RESP_RETRIES
<<4) |
566 (OHCI1394_MAX_PHYS_RESP_RETRIES
<<8));
568 /* We don't want hardware swapping */
569 reg_write(ohci
, OHCI1394_HCControlClear
, OHCI1394_HCControl_noByteSwap
);
571 /* Enable interrupts */
572 reg_write(ohci
, OHCI1394_IntMaskSet
,
573 OHCI1394_unrecoverableError
|
574 OHCI1394_masterIntEnable
|
576 OHCI1394_selfIDComplete
|
579 OHCI1394_respTxComplete
|
580 OHCI1394_reqTxComplete
|
583 OHCI1394_postedWriteErr
|
584 OHCI1394_cycleTooLong
|
585 OHCI1394_cycleInconsistent
);
588 reg_write(ohci
, OHCI1394_HCControlSet
, OHCI1394_HCControl_linkEnable
);
590 buf
= reg_read(ohci
, OHCI1394_Version
);
591 sprintf (irq_buf
, "%d", ohci
->dev
->irq
);
592 PRINT(KERN_INFO
, "OHCI-1394 %d.%d (PCI): IRQ=[%s] "
593 "MMIO=[%lx-%lx] Max Packet=[%d] IR/IT contexts=[%d/%d]",
594 ((((buf
) >> 16) & 0xf) + (((buf
) >> 20) & 0xf) * 10),
595 ((((buf
) >> 4) & 0xf) + ((buf
) & 0xf) * 10), irq_buf
,
596 pci_resource_start(ohci
->dev
, 0),
597 pci_resource_start(ohci
->dev
, 0) + OHCI1394_REGISTER_SIZE
- 1,
598 ohci
->max_packet_size
,
599 ohci
->nb_iso_rcv_ctx
, ohci
->nb_iso_xmit_ctx
);
601 /* Check all of our ports to make sure that if anything is
602 * connected, we enable that port. */
603 num_ports
= get_phy_reg(ohci
, 2) & 0xf;
604 for (i
= 0; i
< num_ports
; i
++) {
607 set_phy_reg(ohci
, 7, i
);
608 status
= get_phy_reg(ohci
, 8);
611 set_phy_reg(ohci
, 8, status
& ~1);
614 /* Serial EEPROM Sanity check. */
615 if ((ohci
->max_packet_size
< 512) ||
616 (ohci
->max_packet_size
> 4096)) {
617 /* Serial EEPROM contents are suspect, set a sane max packet
618 * size and print the raw contents for bug reports if verbose
619 * debug is enabled. */
620 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
624 PRINT(KERN_DEBUG
, "Serial EEPROM has suspicious values, "
625 "attempting to setting max_packet_size to 512 bytes");
626 reg_write(ohci
, OHCI1394_BusOptions
,
627 (reg_read(ohci
, OHCI1394_BusOptions
) & 0xf007) | 0x8002);
628 ohci
->max_packet_size
= 512;
629 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
630 PRINT(KERN_DEBUG
, " EEPROM Present: %d",
631 (reg_read(ohci
, OHCI1394_Version
) >> 24) & 0x1);
632 reg_write(ohci
, OHCI1394_GUID_ROM
, 0x80000000);
636 (reg_read(ohci
, OHCI1394_GUID_ROM
) & 0x80000000)); i
++)
639 for (i
= 0; i
< 0x20; i
++) {
640 reg_write(ohci
, OHCI1394_GUID_ROM
, 0x02000000);
641 PRINT(KERN_DEBUG
, " EEPROM %02x: %02x", i
,
642 (reg_read(ohci
, OHCI1394_GUID_ROM
) >> 16) & 0xff);
649 * Insert a packet in the DMA fifo and generate the DMA prg
650 * FIXME: rewrite the program in order to accept packets crossing
652 * check also that a single dma descriptor doesn't cross a
655 static void insert_packet(struct ti_ohci
*ohci
,
656 struct dma_trm_ctx
*d
, struct hpsb_packet
*packet
)
659 int idx
= d
->prg_ind
;
661 DBGMSG("Inserting packet for node " NODE_BUS_FMT
662 ", tlabel=%d, tcode=0x%x, speed=%d",
663 NODE_BUS_ARGS(ohci
->host
, packet
->node_id
), packet
->tlabel
,
664 packet
->tcode
, packet
->speed_code
);
666 d
->prg_cpu
[idx
]->begin
.address
= 0;
667 d
->prg_cpu
[idx
]->begin
.branchAddress
= 0;
669 if (d
->type
== DMA_CTX_ASYNC_RESP
) {
671 * For response packets, we need to put a timeout value in
672 * the 16 lower bits of the status... let's try 1 sec timeout
674 cycleTimer
= reg_read(ohci
, OHCI1394_IsochronousCycleTimer
);
675 d
->prg_cpu
[idx
]->begin
.status
= cpu_to_le32(
676 (((((cycleTimer
>>25)&0x7)+1)&0x7)<<13) |
677 ((cycleTimer
&0x01fff000)>>12));
679 DBGMSG("cycleTimer: %08x timeStamp: %08x",
680 cycleTimer
, d
->prg_cpu
[idx
]->begin
.status
);
682 d
->prg_cpu
[idx
]->begin
.status
= 0;
684 if ( (packet
->type
== hpsb_async
) || (packet
->type
== hpsb_raw
) ) {
686 if (packet
->type
== hpsb_raw
) {
687 d
->prg_cpu
[idx
]->data
[0] = cpu_to_le32(OHCI1394_TCODE_PHY
<<4);
688 d
->prg_cpu
[idx
]->data
[1] = cpu_to_le32(packet
->header
[0]);
689 d
->prg_cpu
[idx
]->data
[2] = cpu_to_le32(packet
->header
[1]);
691 d
->prg_cpu
[idx
]->data
[0] = packet
->speed_code
<<16 |
692 (packet
->header
[0] & 0xFFFF);
694 if (packet
->tcode
== TCODE_ISO_DATA
) {
695 /* Sending an async stream packet */
696 d
->prg_cpu
[idx
]->data
[1] = packet
->header
[0] & 0xFFFF0000;
698 /* Sending a normal async request or response */
699 d
->prg_cpu
[idx
]->data
[1] =
700 (packet
->header
[1] & 0xFFFF) |
701 (packet
->header
[0] & 0xFFFF0000);
702 d
->prg_cpu
[idx
]->data
[2] = packet
->header
[2];
703 d
->prg_cpu
[idx
]->data
[3] = packet
->header
[3];
705 packet_swab(d
->prg_cpu
[idx
]->data
, packet
->tcode
);
708 if (packet
->data_size
) { /* block transmit */
709 if (packet
->tcode
== TCODE_STREAM_DATA
){
710 d
->prg_cpu
[idx
]->begin
.control
=
711 cpu_to_le32(DMA_CTL_OUTPUT_MORE
|
712 DMA_CTL_IMMEDIATE
| 0x8);
714 d
->prg_cpu
[idx
]->begin
.control
=
715 cpu_to_le32(DMA_CTL_OUTPUT_MORE
|
716 DMA_CTL_IMMEDIATE
| 0x10);
718 d
->prg_cpu
[idx
]->end
.control
=
719 cpu_to_le32(DMA_CTL_OUTPUT_LAST
|
724 * Check that the packet data buffer
725 * does not cross a page boundary.
727 * XXX Fix this some day. eth1394 seems to trigger
728 * it, but ignoring it doesn't seem to cause a
732 if (cross_bound((unsigned long)packet
->data
,
733 packet
->data_size
)>0) {
734 /* FIXME: do something about it */
736 "%s: packet data addr: %p size %Zd bytes "
737 "cross page boundary", __FUNCTION__
,
738 packet
->data
, packet
->data_size
);
741 d
->prg_cpu
[idx
]->end
.address
= cpu_to_le32(
742 pci_map_single(ohci
->dev
, packet
->data
,
745 OHCI_DMA_ALLOC("single, block transmit packet");
747 d
->prg_cpu
[idx
]->end
.branchAddress
= 0;
748 d
->prg_cpu
[idx
]->end
.status
= 0;
749 if (d
->branchAddrPtr
)
750 *(d
->branchAddrPtr
) =
751 cpu_to_le32(d
->prg_bus
[idx
] | 0x3);
753 &(d
->prg_cpu
[idx
]->end
.branchAddress
);
754 } else { /* quadlet transmit */
755 if (packet
->type
== hpsb_raw
)
756 d
->prg_cpu
[idx
]->begin
.control
=
757 cpu_to_le32(DMA_CTL_OUTPUT_LAST
|
761 (packet
->header_size
+ 4));
763 d
->prg_cpu
[idx
]->begin
.control
=
764 cpu_to_le32(DMA_CTL_OUTPUT_LAST
|
768 packet
->header_size
);
770 if (d
->branchAddrPtr
)
771 *(d
->branchAddrPtr
) =
772 cpu_to_le32(d
->prg_bus
[idx
] | 0x2);
774 &(d
->prg_cpu
[idx
]->begin
.branchAddress
);
777 } else { /* iso packet */
778 d
->prg_cpu
[idx
]->data
[0] = packet
->speed_code
<<16 |
779 (packet
->header
[0] & 0xFFFF);
780 d
->prg_cpu
[idx
]->data
[1] = packet
->header
[0] & 0xFFFF0000;
781 packet_swab(d
->prg_cpu
[idx
]->data
, packet
->tcode
);
783 d
->prg_cpu
[idx
]->begin
.control
=
784 cpu_to_le32(DMA_CTL_OUTPUT_MORE
|
785 DMA_CTL_IMMEDIATE
| 0x8);
786 d
->prg_cpu
[idx
]->end
.control
=
787 cpu_to_le32(DMA_CTL_OUTPUT_LAST
|
792 d
->prg_cpu
[idx
]->end
.address
= cpu_to_le32(
793 pci_map_single(ohci
->dev
, packet
->data
,
794 packet
->data_size
, PCI_DMA_TODEVICE
));
795 OHCI_DMA_ALLOC("single, iso transmit packet");
797 d
->prg_cpu
[idx
]->end
.branchAddress
= 0;
798 d
->prg_cpu
[idx
]->end
.status
= 0;
799 DBGMSG("Iso xmit context info: header[%08x %08x]\n"
800 " begin=%08x %08x %08x %08x\n"
801 " %08x %08x %08x %08x\n"
802 " end =%08x %08x %08x %08x",
803 d
->prg_cpu
[idx
]->data
[0], d
->prg_cpu
[idx
]->data
[1],
804 d
->prg_cpu
[idx
]->begin
.control
,
805 d
->prg_cpu
[idx
]->begin
.address
,
806 d
->prg_cpu
[idx
]->begin
.branchAddress
,
807 d
->prg_cpu
[idx
]->begin
.status
,
808 d
->prg_cpu
[idx
]->data
[0],
809 d
->prg_cpu
[idx
]->data
[1],
810 d
->prg_cpu
[idx
]->data
[2],
811 d
->prg_cpu
[idx
]->data
[3],
812 d
->prg_cpu
[idx
]->end
.control
,
813 d
->prg_cpu
[idx
]->end
.address
,
814 d
->prg_cpu
[idx
]->end
.branchAddress
,
815 d
->prg_cpu
[idx
]->end
.status
);
816 if (d
->branchAddrPtr
)
817 *(d
->branchAddrPtr
) = cpu_to_le32(d
->prg_bus
[idx
] | 0x3);
818 d
->branchAddrPtr
= &(d
->prg_cpu
[idx
]->end
.branchAddress
);
822 /* queue the packet in the appropriate context queue */
823 list_add_tail(&packet
->driver_list
, &d
->fifo_list
);
824 d
->prg_ind
= (d
->prg_ind
+ 1) % d
->num_desc
;
828 * This function fills the FIFO with the (eventual) pending packets
829 * and runs or wakes up the DMA prg if necessary.
831 * The function MUST be called with the d->lock held.
833 static void dma_trm_flush(struct ti_ohci
*ohci
, struct dma_trm_ctx
*d
)
835 struct hpsb_packet
*packet
, *ptmp
;
836 int idx
= d
->prg_ind
;
839 /* insert the packets into the dma fifo */
840 list_for_each_entry_safe(packet
, ptmp
, &d
->pending_list
, driver_list
) {
844 /* For the first packet only */
846 z
= (packet
->data_size
) ? 3 : 2;
848 /* Insert the packet */
849 list_del_init(&packet
->driver_list
);
850 insert_packet(ohci
, d
, packet
);
853 /* Nothing must have been done, either no free_prgs or no packets */
857 /* Is the context running ? (should be unless it is
858 the first packet to be sent in this context) */
859 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x8000)) {
860 u32 nodeId
= reg_read(ohci
, OHCI1394_NodeID
);
862 DBGMSG("Starting transmit DMA ctx=%d",d
->ctx
);
863 reg_write(ohci
, d
->cmdPtr
, d
->prg_bus
[idx
] | z
);
865 /* Check that the node id is valid, and not 63 */
866 if (!(nodeId
& 0x80000000) || (nodeId
& 0x3f) == 63)
867 PRINT(KERN_ERR
, "Running dma failed because Node ID is not valid");
869 reg_write(ohci
, d
->ctrlSet
, 0x8000);
871 /* Wake up the dma context if necessary */
872 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x400))
873 DBGMSG("Waking transmit DMA ctx=%d",d
->ctx
);
875 /* do this always, to avoid race condition */
876 reg_write(ohci
, d
->ctrlSet
, 0x1000);
882 /* Transmission of an async or iso packet */
883 static int ohci_transmit(struct hpsb_host
*host
, struct hpsb_packet
*packet
)
885 struct ti_ohci
*ohci
= host
->hostdata
;
886 struct dma_trm_ctx
*d
;
889 if (packet
->data_size
> ohci
->max_packet_size
) {
891 "Transmit packet size %Zd is too big",
896 /* Decide whether we have an iso, a request, or a response packet */
897 if (packet
->type
== hpsb_raw
)
898 d
= &ohci
->at_req_context
;
899 else if ((packet
->tcode
== TCODE_ISO_DATA
) && (packet
->type
== hpsb_iso
)) {
900 /* The legacy IT DMA context is initialized on first
901 * use. However, the alloc cannot be run from
902 * interrupt context, so we bail out if that is the
903 * case. I don't see anyone sending ISO packets from
904 * interrupt context anyway... */
906 if (ohci
->it_legacy_context
.ohci
== NULL
) {
907 if (in_interrupt()) {
909 "legacy IT context cannot be initialized during interrupt");
913 if (alloc_dma_trm_ctx(ohci
, &ohci
->it_legacy_context
,
914 DMA_CTX_ISO
, 0, IT_NUM_DESC
,
915 OHCI1394_IsoXmitContextBase
) < 0) {
917 "error initializing legacy IT context");
921 initialize_dma_trm_ctx(&ohci
->it_legacy_context
);
924 d
= &ohci
->it_legacy_context
;
925 } else if ((packet
->tcode
& 0x02) && (packet
->tcode
!= TCODE_ISO_DATA
))
926 d
= &ohci
->at_resp_context
;
928 d
= &ohci
->at_req_context
;
930 spin_lock_irqsave(&d
->lock
,flags
);
932 list_add_tail(&packet
->driver_list
, &d
->pending_list
);
934 dma_trm_flush(ohci
, d
);
936 spin_unlock_irqrestore(&d
->lock
,flags
);
941 static int ohci_devctl(struct hpsb_host
*host
, enum devctl_cmd cmd
, int arg
)
943 struct ti_ohci
*ohci
= host
->hostdata
;
952 phy_reg
= get_phy_reg(ohci
, 5);
954 set_phy_reg(ohci
, 5, phy_reg
); /* set ISBR */
957 phy_reg
= get_phy_reg(ohci
, 1);
959 set_phy_reg(ohci
, 1, phy_reg
); /* set IBR */
961 case SHORT_RESET_NO_FORCE_ROOT
:
962 phy_reg
= get_phy_reg(ohci
, 1);
963 if (phy_reg
& 0x80) {
965 set_phy_reg(ohci
, 1, phy_reg
); /* clear RHB */
968 phy_reg
= get_phy_reg(ohci
, 5);
970 set_phy_reg(ohci
, 5, phy_reg
); /* set ISBR */
972 case LONG_RESET_NO_FORCE_ROOT
:
973 phy_reg
= get_phy_reg(ohci
, 1);
976 set_phy_reg(ohci
, 1, phy_reg
); /* clear RHB, set IBR */
978 case SHORT_RESET_FORCE_ROOT
:
979 phy_reg
= get_phy_reg(ohci
, 1);
980 if (!(phy_reg
& 0x80)) {
982 set_phy_reg(ohci
, 1, phy_reg
); /* set RHB */
985 phy_reg
= get_phy_reg(ohci
, 5);
987 set_phy_reg(ohci
, 5, phy_reg
); /* set ISBR */
989 case LONG_RESET_FORCE_ROOT
:
990 phy_reg
= get_phy_reg(ohci
, 1);
992 set_phy_reg(ohci
, 1, phy_reg
); /* set RHB and IBR */
999 case GET_CYCLE_COUNTER
:
1000 retval
= reg_read(ohci
, OHCI1394_IsochronousCycleTimer
);
1003 case SET_CYCLE_COUNTER
:
1004 reg_write(ohci
, OHCI1394_IsochronousCycleTimer
, arg
);
1008 PRINT(KERN_ERR
, "devctl command SET_BUS_ID err");
1011 case ACT_CYCLE_MASTER
:
1013 /* check if we are root and other nodes are present */
1014 u32 nodeId
= reg_read(ohci
, OHCI1394_NodeID
);
1015 if ((nodeId
& (1<<30)) && (nodeId
& 0x3f)) {
1017 * enable cycleTimer, cycleMaster
1019 DBGMSG("Cycle master enabled");
1020 reg_write(ohci
, OHCI1394_LinkControlSet
,
1021 OHCI1394_LinkControl_CycleTimerEnable
|
1022 OHCI1394_LinkControl_CycleMaster
);
1025 /* disable cycleTimer, cycleMaster, cycleSource */
1026 reg_write(ohci
, OHCI1394_LinkControlClear
,
1027 OHCI1394_LinkControl_CycleTimerEnable
|
1028 OHCI1394_LinkControl_CycleMaster
|
1029 OHCI1394_LinkControl_CycleSource
);
1033 case CANCEL_REQUESTS
:
1034 DBGMSG("Cancel request received");
1035 dma_trm_reset(&ohci
->at_req_context
);
1036 dma_trm_reset(&ohci
->at_resp_context
);
1039 case ISO_LISTEN_CHANNEL
:
1042 struct dma_rcv_ctx
*d
= &ohci
->ir_legacy_context
;
1043 int ir_legacy_active
;
1045 if (arg
<0 || arg
>63) {
1047 "%s: IS0 listen channel %d is out of range",
1052 mask
= (u64
)0x1<<arg
;
1054 spin_lock_irqsave(&ohci
->IR_channel_lock
, flags
);
1056 if (ohci
->ISO_channel_usage
& mask
) {
1058 "%s: IS0 listen channel %d is already used",
1060 spin_unlock_irqrestore(&ohci
->IR_channel_lock
, flags
);
1064 ir_legacy_active
= ohci
->ir_legacy_channels
;
1066 ohci
->ISO_channel_usage
|= mask
;
1067 ohci
->ir_legacy_channels
|= mask
;
1069 spin_unlock_irqrestore(&ohci
->IR_channel_lock
, flags
);
1071 if (!ir_legacy_active
) {
1072 if (ohci1394_register_iso_tasklet(ohci
,
1073 &ohci
->ir_legacy_tasklet
) < 0) {
1074 PRINT(KERN_ERR
, "No IR DMA context available");
1078 /* the IR context can be assigned to any DMA context
1079 * by ohci1394_register_iso_tasklet */
1080 d
->ctx
= ohci
->ir_legacy_tasklet
.context
;
1081 d
->ctrlSet
= OHCI1394_IsoRcvContextControlSet
+
1083 d
->ctrlClear
= OHCI1394_IsoRcvContextControlClear
+
1085 d
->cmdPtr
= OHCI1394_IsoRcvCommandPtr
+ 32*d
->ctx
;
1086 d
->ctxtMatch
= OHCI1394_IsoRcvContextMatch
+ 32*d
->ctx
;
1088 initialize_dma_rcv_ctx(&ohci
->ir_legacy_context
, 1);
1090 if (printk_ratelimit())
1091 DBGMSG("IR legacy activated");
1094 spin_lock_irqsave(&ohci
->IR_channel_lock
, flags
);
1097 reg_write(ohci
, OHCI1394_IRMultiChanMaskHiSet
,
1100 reg_write(ohci
, OHCI1394_IRMultiChanMaskLoSet
,
1103 spin_unlock_irqrestore(&ohci
->IR_channel_lock
, flags
);
1104 DBGMSG("Listening enabled on channel %d", arg
);
1107 case ISO_UNLISTEN_CHANNEL
:
1111 if (arg
<0 || arg
>63) {
1113 "%s: IS0 unlisten channel %d is out of range",
1118 mask
= (u64
)0x1<<arg
;
1120 spin_lock_irqsave(&ohci
->IR_channel_lock
, flags
);
1122 if (!(ohci
->ISO_channel_usage
& mask
)) {
1124 "%s: IS0 unlisten channel %d is not used",
1126 spin_unlock_irqrestore(&ohci
->IR_channel_lock
, flags
);
1130 ohci
->ISO_channel_usage
&= ~mask
;
1131 ohci
->ir_legacy_channels
&= ~mask
;
1134 reg_write(ohci
, OHCI1394_IRMultiChanMaskHiClear
,
1137 reg_write(ohci
, OHCI1394_IRMultiChanMaskLoClear
,
1140 spin_unlock_irqrestore(&ohci
->IR_channel_lock
, flags
);
1141 DBGMSG("Listening disabled on channel %d", arg
);
1143 if (ohci
->ir_legacy_channels
== 0) {
1144 stop_dma_rcv_ctx(&ohci
->ir_legacy_context
);
1145 DBGMSG("ISO legacy receive context stopped");
1151 PRINT_G(KERN_ERR
, "ohci_devctl cmd %d not implemented yet",
1158 /***********************************
1159 * rawiso ISO reception *
1160 ***********************************/
1163 We use either buffer-fill or packet-per-buffer DMA mode. The DMA
1164 buffer is split into "blocks" (regions described by one DMA
1165 descriptor). Each block must be one page or less in size, and
1166 must not cross a page boundary.
1168 There is one little wrinkle with buffer-fill mode: a packet that
1169 starts in the final block may wrap around into the first block. But
1170 the user API expects all packets to be contiguous. Our solution is
1171 to keep the very last page of the DMA buffer in reserve - if a
1172 packet spans the gap, we copy its tail into this page.
1175 struct ohci_iso_recv
{
1176 struct ti_ohci
*ohci
;
1178 struct ohci1394_iso_tasklet task
;
1181 enum { BUFFER_FILL_MODE
= 0,
1182 PACKET_PER_BUFFER_MODE
= 1 } dma_mode
;
1184 /* memory and PCI mapping for the DMA descriptors */
1185 struct dma_prog_region prog
;
1186 struct dma_cmd
*block
; /* = (struct dma_cmd*) prog.virt */
1188 /* how many DMA blocks fit in the buffer */
1189 unsigned int nblocks
;
1191 /* stride of DMA blocks */
1192 unsigned int buf_stride
;
1194 /* number of blocks to batch between interrupts */
1195 int block_irq_interval
;
1197 /* block that DMA will finish next */
1200 /* (buffer-fill only) block that the reader will release next */
1203 /* (buffer-fill only) bytes of buffer the reader has released,
1204 less than one block */
1207 /* (buffer-fill only) buffer offset at which the next packet will appear */
1210 /* OHCI DMA context control registers */
1211 u32 ContextControlSet
;
1212 u32 ContextControlClear
;
1217 static void ohci_iso_recv_task(unsigned long data
);
1218 static void ohci_iso_recv_stop(struct hpsb_iso
*iso
);
1219 static void ohci_iso_recv_shutdown(struct hpsb_iso
*iso
);
1220 static int ohci_iso_recv_start(struct hpsb_iso
*iso
, int cycle
, int tag_mask
, int sync
);
1221 static void ohci_iso_recv_program(struct hpsb_iso
*iso
);
1223 static int ohci_iso_recv_init(struct hpsb_iso
*iso
)
1225 struct ti_ohci
*ohci
= iso
->host
->hostdata
;
1226 struct ohci_iso_recv
*recv
;
1230 recv
= kmalloc(sizeof(*recv
), SLAB_KERNEL
);
1234 iso
->hostdata
= recv
;
1236 recv
->task_active
= 0;
1237 dma_prog_region_init(&recv
->prog
);
1240 /* use buffer-fill mode, unless irq_interval is 1
1241 (note: multichannel requires buffer-fill) */
1243 if (((iso
->irq_interval
== 1 && iso
->dma_mode
== HPSB_ISO_DMA_OLD_ABI
) ||
1244 iso
->dma_mode
== HPSB_ISO_DMA_PACKET_PER_BUFFER
) && iso
->channel
!= -1) {
1245 recv
->dma_mode
= PACKET_PER_BUFFER_MODE
;
1247 recv
->dma_mode
= BUFFER_FILL_MODE
;
1250 /* set nblocks, buf_stride, block_irq_interval */
1252 if (recv
->dma_mode
== BUFFER_FILL_MODE
) {
1253 recv
->buf_stride
= PAGE_SIZE
;
1255 /* one block per page of data in the DMA buffer, minus the final guard page */
1256 recv
->nblocks
= iso
->buf_size
/PAGE_SIZE
- 1;
1257 if (recv
->nblocks
< 3) {
1258 DBGMSG("ohci_iso_recv_init: DMA buffer too small");
1262 /* iso->irq_interval is in packets - translate that to blocks */
1263 if (iso
->irq_interval
== 1)
1264 recv
->block_irq_interval
= 1;
1266 recv
->block_irq_interval
= iso
->irq_interval
*
1267 ((recv
->nblocks
+1)/iso
->buf_packets
);
1268 if (recv
->block_irq_interval
*4 > recv
->nblocks
)
1269 recv
->block_irq_interval
= recv
->nblocks
/4;
1270 if (recv
->block_irq_interval
< 1)
1271 recv
->block_irq_interval
= 1;
1274 int max_packet_size
;
1276 recv
->nblocks
= iso
->buf_packets
;
1277 recv
->block_irq_interval
= iso
->irq_interval
;
1278 if (recv
->block_irq_interval
* 4 > iso
->buf_packets
)
1279 recv
->block_irq_interval
= iso
->buf_packets
/ 4;
1280 if (recv
->block_irq_interval
< 1)
1281 recv
->block_irq_interval
= 1;
1283 /* choose a buffer stride */
1284 /* must be a power of 2, and <= PAGE_SIZE */
1286 max_packet_size
= iso
->buf_size
/ iso
->buf_packets
;
1288 for (recv
->buf_stride
= 8; recv
->buf_stride
< max_packet_size
;
1289 recv
->buf_stride
*= 2);
1291 if (recv
->buf_stride
*iso
->buf_packets
> iso
->buf_size
||
1292 recv
->buf_stride
> PAGE_SIZE
) {
1293 /* this shouldn't happen, but anyway... */
1294 DBGMSG("ohci_iso_recv_init: problem choosing a buffer stride");
1299 recv
->block_reader
= 0;
1300 recv
->released_bytes
= 0;
1301 recv
->block_dma
= 0;
1302 recv
->dma_offset
= 0;
1304 /* size of DMA program = one descriptor per block */
1305 if (dma_prog_region_alloc(&recv
->prog
,
1306 sizeof(struct dma_cmd
) * recv
->nblocks
,
1310 recv
->block
= (struct dma_cmd
*) recv
->prog
.kvirt
;
1312 ohci1394_init_iso_tasklet(&recv
->task
,
1313 iso
->channel
== -1 ? OHCI_ISO_MULTICHANNEL_RECEIVE
:
1315 ohci_iso_recv_task
, (unsigned long) iso
);
1317 if (ohci1394_register_iso_tasklet(recv
->ohci
, &recv
->task
) < 0) {
1322 recv
->task_active
= 1;
1324 /* recv context registers are spaced 32 bytes apart */
1325 ctx
= recv
->task
.context
;
1326 recv
->ContextControlSet
= OHCI1394_IsoRcvContextControlSet
+ 32 * ctx
;
1327 recv
->ContextControlClear
= OHCI1394_IsoRcvContextControlClear
+ 32 * ctx
;
1328 recv
->CommandPtr
= OHCI1394_IsoRcvCommandPtr
+ 32 * ctx
;
1329 recv
->ContextMatch
= OHCI1394_IsoRcvContextMatch
+ 32 * ctx
;
1331 if (iso
->channel
== -1) {
1332 /* clear multi-channel selection mask */
1333 reg_write(recv
->ohci
, OHCI1394_IRMultiChanMaskHiClear
, 0xFFFFFFFF);
1334 reg_write(recv
->ohci
, OHCI1394_IRMultiChanMaskLoClear
, 0xFFFFFFFF);
1337 /* write the DMA program */
1338 ohci_iso_recv_program(iso
);
1340 DBGMSG("ohci_iso_recv_init: %s mode, DMA buffer is %lu pages"
1341 " (%u bytes), using %u blocks, buf_stride %u, block_irq_interval %d",
1342 recv
->dma_mode
== BUFFER_FILL_MODE
?
1343 "buffer-fill" : "packet-per-buffer",
1344 iso
->buf_size
/PAGE_SIZE
, iso
->buf_size
,
1345 recv
->nblocks
, recv
->buf_stride
, recv
->block_irq_interval
);
1350 ohci_iso_recv_shutdown(iso
);
1354 static void ohci_iso_recv_stop(struct hpsb_iso
*iso
)
1356 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1358 /* disable interrupts */
1359 reg_write(recv
->ohci
, OHCI1394_IsoRecvIntMaskClear
, 1 << recv
->task
.context
);
1362 ohci1394_stop_context(recv
->ohci
, recv
->ContextControlClear
, NULL
);
1365 static void ohci_iso_recv_shutdown(struct hpsb_iso
*iso
)
1367 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1369 if (recv
->task_active
) {
1370 ohci_iso_recv_stop(iso
);
1371 ohci1394_unregister_iso_tasklet(recv
->ohci
, &recv
->task
);
1372 recv
->task_active
= 0;
1375 dma_prog_region_free(&recv
->prog
);
1377 iso
->hostdata
= NULL
;
1380 /* set up a "gapped" ring buffer DMA program */
1381 static void ohci_iso_recv_program(struct hpsb_iso
*iso
)
1383 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1386 /* address of 'branch' field in previous DMA descriptor */
1387 u32
*prev_branch
= NULL
;
1389 for (blk
= 0; blk
< recv
->nblocks
; blk
++) {
1392 /* the DMA descriptor */
1393 struct dma_cmd
*cmd
= &recv
->block
[blk
];
1395 /* offset of the DMA descriptor relative to the DMA prog buffer */
1396 unsigned long prog_offset
= blk
* sizeof(struct dma_cmd
);
1398 /* offset of this packet's data within the DMA buffer */
1399 unsigned long buf_offset
= blk
* recv
->buf_stride
;
1401 if (recv
->dma_mode
== BUFFER_FILL_MODE
) {
1402 control
= 2 << 28; /* INPUT_MORE */
1404 control
= 3 << 28; /* INPUT_LAST */
1407 control
|= 8 << 24; /* s = 1, update xferStatus and resCount */
1409 /* interrupt on last block, and at intervals */
1410 if (blk
== recv
->nblocks
-1 || (blk
% recv
->block_irq_interval
) == 0) {
1411 control
|= 3 << 20; /* want interrupt */
1414 control
|= 3 << 18; /* enable branch to address */
1415 control
|= recv
->buf_stride
;
1417 cmd
->control
= cpu_to_le32(control
);
1418 cmd
->address
= cpu_to_le32(dma_region_offset_to_bus(&iso
->data_buf
, buf_offset
));
1419 cmd
->branchAddress
= 0; /* filled in on next loop */
1420 cmd
->status
= cpu_to_le32(recv
->buf_stride
);
1422 /* link the previous descriptor to this one */
1424 *prev_branch
= cpu_to_le32(dma_prog_region_offset_to_bus(&recv
->prog
, prog_offset
) | 1);
1427 prev_branch
= &cmd
->branchAddress
;
1430 /* the final descriptor's branch address and Z should be left at 0 */
1433 /* listen or unlisten to a specific channel (multi-channel mode only) */
1434 static void ohci_iso_recv_change_channel(struct hpsb_iso
*iso
, unsigned char channel
, int listen
)
1436 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1440 reg
= listen
? OHCI1394_IRMultiChanMaskLoSet
: OHCI1394_IRMultiChanMaskLoClear
;
1443 reg
= listen
? OHCI1394_IRMultiChanMaskHiSet
: OHCI1394_IRMultiChanMaskHiClear
;
1447 reg_write(recv
->ohci
, reg
, (1 << i
));
1449 /* issue a dummy read to force all PCI writes to be posted immediately */
1451 reg_read(recv
->ohci
, OHCI1394_IsochronousCycleTimer
);
1454 static void ohci_iso_recv_set_channel_mask(struct hpsb_iso
*iso
, u64 mask
)
1456 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1459 for (i
= 0; i
< 64; i
++) {
1460 if (mask
& (1ULL << i
)) {
1462 reg_write(recv
->ohci
, OHCI1394_IRMultiChanMaskLoSet
, (1 << i
));
1464 reg_write(recv
->ohci
, OHCI1394_IRMultiChanMaskHiSet
, (1 << (i
-32)));
1467 reg_write(recv
->ohci
, OHCI1394_IRMultiChanMaskLoClear
, (1 << i
));
1469 reg_write(recv
->ohci
, OHCI1394_IRMultiChanMaskHiClear
, (1 << (i
-32)));
1473 /* issue a dummy read to force all PCI writes to be posted immediately */
1475 reg_read(recv
->ohci
, OHCI1394_IsochronousCycleTimer
);
1478 static int ohci_iso_recv_start(struct hpsb_iso
*iso
, int cycle
, int tag_mask
, int sync
)
1480 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1481 struct ti_ohci
*ohci
= recv
->ohci
;
1482 u32 command
, contextMatch
;
1484 reg_write(recv
->ohci
, recv
->ContextControlClear
, 0xFFFFFFFF);
1487 /* always keep ISO headers */
1488 command
= (1 << 30);
1490 if (recv
->dma_mode
== BUFFER_FILL_MODE
)
1491 command
|= (1 << 31);
1493 reg_write(recv
->ohci
, recv
->ContextControlSet
, command
);
1495 /* match on specified tags */
1496 contextMatch
= tag_mask
<< 28;
1498 if (iso
->channel
== -1) {
1499 /* enable multichannel reception */
1500 reg_write(recv
->ohci
, recv
->ContextControlSet
, (1 << 28));
1502 /* listen on channel */
1503 contextMatch
|= iso
->channel
;
1509 /* enable cycleMatch */
1510 reg_write(recv
->ohci
, recv
->ContextControlSet
, (1 << 29));
1512 /* set starting cycle */
1515 /* 'cycle' is only mod 8000, but we also need two 'seconds' bits -
1516 just snarf them from the current time */
1517 seconds
= reg_read(recv
->ohci
, OHCI1394_IsochronousCycleTimer
) >> 25;
1519 /* advance one second to give some extra time for DMA to start */
1522 cycle
|= (seconds
& 3) << 13;
1524 contextMatch
|= cycle
<< 12;
1528 /* set sync flag on first DMA descriptor */
1529 struct dma_cmd
*cmd
= &recv
->block
[recv
->block_dma
];
1530 cmd
->control
|= cpu_to_le32(DMA_CTL_WAIT
);
1532 /* match sync field */
1533 contextMatch
|= (sync
&0xf)<<8;
1536 reg_write(recv
->ohci
, recv
->ContextMatch
, contextMatch
);
1538 /* address of first descriptor block */
1539 command
= dma_prog_region_offset_to_bus(&recv
->prog
,
1540 recv
->block_dma
* sizeof(struct dma_cmd
));
1541 command
|= 1; /* Z=1 */
1543 reg_write(recv
->ohci
, recv
->CommandPtr
, command
);
1545 /* enable interrupts */
1546 reg_write(recv
->ohci
, OHCI1394_IsoRecvIntMaskSet
, 1 << recv
->task
.context
);
1551 reg_write(recv
->ohci
, recv
->ContextControlSet
, 0x8000);
1553 /* issue a dummy read of the cycle timer register to force
1554 all PCI writes to be posted immediately */
1556 reg_read(recv
->ohci
, OHCI1394_IsochronousCycleTimer
);
1559 if (!(reg_read(recv
->ohci
, recv
->ContextControlSet
) & 0x8000)) {
1561 "Error starting IR DMA (ContextControl 0x%08x)\n",
1562 reg_read(recv
->ohci
, recv
->ContextControlSet
));
1569 static void ohci_iso_recv_release_block(struct ohci_iso_recv
*recv
, int block
)
1571 /* re-use the DMA descriptor for the block */
1572 /* by linking the previous descriptor to it */
1575 int prev_i
= (next_i
== 0) ? (recv
->nblocks
- 1) : (next_i
- 1);
1577 struct dma_cmd
*next
= &recv
->block
[next_i
];
1578 struct dma_cmd
*prev
= &recv
->block
[prev_i
];
1580 /* ignore out-of-range requests */
1581 if ((block
< 0) || (block
> recv
->nblocks
))
1584 /* 'next' becomes the new end of the DMA chain,
1585 so disable branch and enable interrupt */
1586 next
->branchAddress
= 0;
1587 next
->control
|= cpu_to_le32(3 << 20);
1588 next
->status
= cpu_to_le32(recv
->buf_stride
);
1590 /* link prev to next */
1591 prev
->branchAddress
= cpu_to_le32(dma_prog_region_offset_to_bus(&recv
->prog
,
1592 sizeof(struct dma_cmd
) * next_i
)
1595 /* disable interrupt on previous DMA descriptor, except at intervals */
1596 if ((prev_i
% recv
->block_irq_interval
) == 0) {
1597 prev
->control
|= cpu_to_le32(3 << 20); /* enable interrupt */
1599 prev
->control
&= cpu_to_le32(~(3<<20)); /* disable interrupt */
1603 /* wake up DMA in case it fell asleep */
1604 reg_write(recv
->ohci
, recv
->ContextControlSet
, (1 << 12));
1607 static void ohci_iso_recv_bufferfill_release(struct ohci_iso_recv
*recv
,
1608 struct hpsb_iso_packet_info
*info
)
1610 /* release the memory where the packet was */
1611 recv
->released_bytes
+= info
->total_len
;
1613 /* have we released enough memory for one block? */
1614 while (recv
->released_bytes
> recv
->buf_stride
) {
1615 ohci_iso_recv_release_block(recv
, recv
->block_reader
);
1616 recv
->block_reader
= (recv
->block_reader
+ 1) % recv
->nblocks
;
1617 recv
->released_bytes
-= recv
->buf_stride
;
1621 static inline void ohci_iso_recv_release(struct hpsb_iso
*iso
, struct hpsb_iso_packet_info
*info
)
1623 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1624 if (recv
->dma_mode
== BUFFER_FILL_MODE
) {
1625 ohci_iso_recv_bufferfill_release(recv
, info
);
1627 ohci_iso_recv_release_block(recv
, info
- iso
->infos
);
1631 /* parse all packets from blocks that have been fully received */
1632 static void ohci_iso_recv_bufferfill_parse(struct hpsb_iso
*iso
, struct ohci_iso_recv
*recv
)
1636 struct ti_ohci
*ohci
= recv
->ohci
;
1639 /* we expect the next parsable packet to begin at recv->dma_offset */
1640 /* note: packet layout is as shown in section 10.6.1.1 of the OHCI spec */
1642 unsigned int offset
;
1643 unsigned short len
, cycle
, total_len
;
1644 unsigned char channel
, tag
, sy
;
1646 unsigned char *p
= iso
->data_buf
.kvirt
;
1648 unsigned int this_block
= recv
->dma_offset
/recv
->buf_stride
;
1650 /* don't loop indefinitely */
1651 if (runaway
++ > 100000) {
1652 atomic_inc(&iso
->overflows
);
1654 "IR DMA error - Runaway during buffer parsing!\n");
1658 /* stop parsing once we arrive at block_dma (i.e. don't get ahead of DMA) */
1659 if (this_block
== recv
->block_dma
)
1664 /* parse data length, tag, channel, and sy */
1666 /* note: we keep our own local copies of 'len' and 'offset'
1667 so the user can't mess with them by poking in the mmap area */
1669 len
= p
[recv
->dma_offset
+2] | (p
[recv
->dma_offset
+3] << 8);
1673 "IR DMA error - bogus 'len' value %u\n", len
);
1676 channel
= p
[recv
->dma_offset
+1] & 0x3F;
1677 tag
= p
[recv
->dma_offset
+1] >> 6;
1678 sy
= p
[recv
->dma_offset
+0] & 0xF;
1680 /* advance to data payload */
1681 recv
->dma_offset
+= 4;
1683 /* check for wrap-around */
1684 if (recv
->dma_offset
>= recv
->buf_stride
*recv
->nblocks
) {
1685 recv
->dma_offset
-= recv
->buf_stride
*recv
->nblocks
;
1688 /* dma_offset now points to the first byte of the data payload */
1689 offset
= recv
->dma_offset
;
1691 /* advance to xferStatus/timeStamp */
1692 recv
->dma_offset
+= len
;
1694 total_len
= len
+ 8; /* 8 bytes header+trailer in OHCI packet */
1695 /* payload is padded to 4 bytes */
1697 recv
->dma_offset
+= 4 - (len
%4);
1698 total_len
+= 4 - (len
%4);
1701 /* check for wrap-around */
1702 if (recv
->dma_offset
>= recv
->buf_stride
*recv
->nblocks
) {
1703 /* uh oh, the packet data wraps from the last
1704 to the first DMA block - make the packet
1705 contiguous by copying its "tail" into the
1708 int guard_off
= recv
->buf_stride
*recv
->nblocks
;
1709 int tail_len
= len
- (guard_off
- offset
);
1711 if (tail_len
> 0 && tail_len
< recv
->buf_stride
) {
1712 memcpy(iso
->data_buf
.kvirt
+ guard_off
,
1713 iso
->data_buf
.kvirt
,
1717 recv
->dma_offset
-= recv
->buf_stride
*recv
->nblocks
;
1720 /* parse timestamp */
1721 cycle
= p
[recv
->dma_offset
+0] | (p
[recv
->dma_offset
+1]<<8);
1724 /* advance to next packet */
1725 recv
->dma_offset
+= 4;
1727 /* check for wrap-around */
1728 if (recv
->dma_offset
>= recv
->buf_stride
*recv
->nblocks
) {
1729 recv
->dma_offset
-= recv
->buf_stride
*recv
->nblocks
;
1732 hpsb_iso_packet_received(iso
, offset
, len
, total_len
, cycle
, channel
, tag
, sy
);
1739 static void ohci_iso_recv_bufferfill_task(struct hpsb_iso
*iso
, struct ohci_iso_recv
*recv
)
1742 struct ti_ohci
*ohci
= recv
->ohci
;
1744 /* loop over all blocks */
1745 for (loop
= 0; loop
< recv
->nblocks
; loop
++) {
1747 /* check block_dma to see if it's done */
1748 struct dma_cmd
*im
= &recv
->block
[recv
->block_dma
];
1750 /* check the DMA descriptor for new writes to xferStatus */
1751 u16 xferstatus
= le32_to_cpu(im
->status
) >> 16;
1753 /* rescount is the number of bytes *remaining to be written* in the block */
1754 u16 rescount
= le32_to_cpu(im
->status
) & 0xFFFF;
1756 unsigned char event
= xferstatus
& 0x1F;
1759 /* nothing has happened to this block yet */
1763 if (event
!= 0x11) {
1764 atomic_inc(&iso
->overflows
);
1766 "IR DMA error - OHCI error code 0x%02x\n", event
);
1769 if (rescount
!= 0) {
1770 /* the card is still writing to this block;
1771 we can't touch it until it's done */
1775 /* OK, the block is finished... */
1777 /* sync our view of the block */
1778 dma_region_sync_for_cpu(&iso
->data_buf
, recv
->block_dma
*recv
->buf_stride
, recv
->buf_stride
);
1780 /* reset the DMA descriptor */
1781 im
->status
= recv
->buf_stride
;
1783 /* advance block_dma */
1784 recv
->block_dma
= (recv
->block_dma
+ 1) % recv
->nblocks
;
1786 if ((recv
->block_dma
+1) % recv
->nblocks
== recv
->block_reader
) {
1787 atomic_inc(&iso
->overflows
);
1788 DBGMSG("ISO reception overflow - "
1789 "ran out of DMA blocks");
1793 /* parse any packets that have arrived */
1794 ohci_iso_recv_bufferfill_parse(iso
, recv
);
1797 static void ohci_iso_recv_packetperbuf_task(struct hpsb_iso
*iso
, struct ohci_iso_recv
*recv
)
1801 struct ti_ohci
*ohci
= recv
->ohci
;
1803 /* loop over the entire buffer */
1804 for (count
= 0; count
< recv
->nblocks
; count
++) {
1807 /* pointer to the DMA descriptor */
1808 struct dma_cmd
*il
= ((struct dma_cmd
*) recv
->prog
.kvirt
) + iso
->pkt_dma
;
1810 /* check the DMA descriptor for new writes to xferStatus */
1811 u16 xferstatus
= le32_to_cpu(il
->status
) >> 16;
1812 u16 rescount
= le32_to_cpu(il
->status
) & 0xFFFF;
1814 unsigned char event
= xferstatus
& 0x1F;
1817 /* this packet hasn't come in yet; we are done for now */
1821 if (event
== 0x11) {
1822 /* packet received successfully! */
1824 /* rescount is the number of bytes *remaining* in the packet buffer,
1825 after the packet was written */
1826 packet_len
= recv
->buf_stride
- rescount
;
1828 } else if (event
== 0x02) {
1829 PRINT(KERN_ERR
, "IR DMA error - packet too long for buffer\n");
1831 PRINT(KERN_ERR
, "IR DMA error - OHCI error code 0x%02x\n", event
);
1834 /* sync our view of the buffer */
1835 dma_region_sync_for_cpu(&iso
->data_buf
, iso
->pkt_dma
* recv
->buf_stride
, recv
->buf_stride
);
1837 /* record the per-packet info */
1839 /* iso header is 8 bytes ahead of the data payload */
1842 unsigned int offset
;
1843 unsigned short cycle
;
1844 unsigned char channel
, tag
, sy
;
1846 offset
= iso
->pkt_dma
* recv
->buf_stride
;
1847 hdr
= iso
->data_buf
.kvirt
+ offset
;
1849 /* skip iso header */
1853 cycle
= (hdr
[0] | (hdr
[1] << 8)) & 0x1FFF;
1854 channel
= hdr
[5] & 0x3F;
1858 hpsb_iso_packet_received(iso
, offset
, packet_len
,
1859 recv
->buf_stride
, cycle
, channel
, tag
, sy
);
1862 /* reset the DMA descriptor */
1863 il
->status
= recv
->buf_stride
;
1866 recv
->block_dma
= iso
->pkt_dma
;
1874 static void ohci_iso_recv_task(unsigned long data
)
1876 struct hpsb_iso
*iso
= (struct hpsb_iso
*) data
;
1877 struct ohci_iso_recv
*recv
= iso
->hostdata
;
1879 if (recv
->dma_mode
== BUFFER_FILL_MODE
)
1880 ohci_iso_recv_bufferfill_task(iso
, recv
);
1882 ohci_iso_recv_packetperbuf_task(iso
, recv
);
1885 /***********************************
1886 * rawiso ISO transmission *
1887 ***********************************/
1889 struct ohci_iso_xmit
{
1890 struct ti_ohci
*ohci
;
1891 struct dma_prog_region prog
;
1892 struct ohci1394_iso_tasklet task
;
1895 u32 ContextControlSet
;
1896 u32 ContextControlClear
;
1900 /* transmission DMA program:
1901 one OUTPUT_MORE_IMMEDIATE for the IT header
1902 one OUTPUT_LAST for the buffer data */
1904 struct iso_xmit_cmd
{
1905 struct dma_cmd output_more_immediate
;
1908 struct dma_cmd output_last
;
1911 static int ohci_iso_xmit_init(struct hpsb_iso
*iso
);
1912 static int ohci_iso_xmit_start(struct hpsb_iso
*iso
, int cycle
);
1913 static void ohci_iso_xmit_shutdown(struct hpsb_iso
*iso
);
1914 static void ohci_iso_xmit_task(unsigned long data
);
1916 static int ohci_iso_xmit_init(struct hpsb_iso
*iso
)
1918 struct ohci_iso_xmit
*xmit
;
1919 unsigned int prog_size
;
1923 xmit
= kmalloc(sizeof(*xmit
), SLAB_KERNEL
);
1927 iso
->hostdata
= xmit
;
1928 xmit
->ohci
= iso
->host
->hostdata
;
1929 xmit
->task_active
= 0;
1931 dma_prog_region_init(&xmit
->prog
);
1933 prog_size
= sizeof(struct iso_xmit_cmd
) * iso
->buf_packets
;
1935 if (dma_prog_region_alloc(&xmit
->prog
, prog_size
, xmit
->ohci
->dev
))
1938 ohci1394_init_iso_tasklet(&xmit
->task
, OHCI_ISO_TRANSMIT
,
1939 ohci_iso_xmit_task
, (unsigned long) iso
);
1941 if (ohci1394_register_iso_tasklet(xmit
->ohci
, &xmit
->task
) < 0) {
1946 xmit
->task_active
= 1;
1948 /* xmit context registers are spaced 16 bytes apart */
1949 ctx
= xmit
->task
.context
;
1950 xmit
->ContextControlSet
= OHCI1394_IsoXmitContextControlSet
+ 16 * ctx
;
1951 xmit
->ContextControlClear
= OHCI1394_IsoXmitContextControlClear
+ 16 * ctx
;
1952 xmit
->CommandPtr
= OHCI1394_IsoXmitCommandPtr
+ 16 * ctx
;
1957 ohci_iso_xmit_shutdown(iso
);
1961 static void ohci_iso_xmit_stop(struct hpsb_iso
*iso
)
1963 struct ohci_iso_xmit
*xmit
= iso
->hostdata
;
1964 struct ti_ohci
*ohci
= xmit
->ohci
;
1966 /* disable interrupts */
1967 reg_write(xmit
->ohci
, OHCI1394_IsoXmitIntMaskClear
, 1 << xmit
->task
.context
);
1970 if (ohci1394_stop_context(xmit
->ohci
, xmit
->ContextControlClear
, NULL
)) {
1971 /* XXX the DMA context will lock up if you try to send too much data! */
1973 "you probably exceeded the OHCI card's bandwidth limit - "
1974 "reload the module and reduce xmit bandwidth");
1978 static void ohci_iso_xmit_shutdown(struct hpsb_iso
*iso
)
1980 struct ohci_iso_xmit
*xmit
= iso
->hostdata
;
1982 if (xmit
->task_active
) {
1983 ohci_iso_xmit_stop(iso
);
1984 ohci1394_unregister_iso_tasklet(xmit
->ohci
, &xmit
->task
);
1985 xmit
->task_active
= 0;
1988 dma_prog_region_free(&xmit
->prog
);
1990 iso
->hostdata
= NULL
;
1993 static void ohci_iso_xmit_task(unsigned long data
)
1995 struct hpsb_iso
*iso
= (struct hpsb_iso
*) data
;
1996 struct ohci_iso_xmit
*xmit
= iso
->hostdata
;
1997 struct ti_ohci
*ohci
= xmit
->ohci
;
2001 /* check the whole buffer if necessary, starting at pkt_dma */
2002 for (count
= 0; count
< iso
->buf_packets
; count
++) {
2005 /* DMA descriptor */
2006 struct iso_xmit_cmd
*cmd
= dma_region_i(&xmit
->prog
, struct iso_xmit_cmd
, iso
->pkt_dma
);
2008 /* check for new writes to xferStatus */
2009 u16 xferstatus
= le32_to_cpu(cmd
->output_last
.status
) >> 16;
2010 u8 event
= xferstatus
& 0x1F;
2013 /* packet hasn't been sent yet; we are done for now */
2019 "IT DMA error - OHCI error code 0x%02x\n", event
);
2021 /* at least one packet went out, so wake up the writer */
2025 cycle
= le32_to_cpu(cmd
->output_last
.status
) & 0x1FFF;
2027 /* tell the subsystem the packet has gone out */
2028 hpsb_iso_packet_sent(iso
, cycle
, event
!= 0x11);
2030 /* reset the DMA descriptor for next time */
2031 cmd
->output_last
.status
= 0;
2038 static int ohci_iso_xmit_queue(struct hpsb_iso
*iso
, struct hpsb_iso_packet_info
*info
)
2040 struct ohci_iso_xmit
*xmit
= iso
->hostdata
;
2041 struct ti_ohci
*ohci
= xmit
->ohci
;
2044 struct iso_xmit_cmd
*next
, *prev
;
2046 unsigned int offset
;
2048 unsigned char tag
, sy
;
2050 /* check that the packet doesn't cross a page boundary
2051 (we could allow this if we added OUTPUT_MORE descriptor support) */
2052 if (cross_bound(info
->offset
, info
->len
)) {
2054 "rawiso xmit: packet %u crosses a page boundary",
2059 offset
= info
->offset
;
2064 /* sync up the card's view of the buffer */
2065 dma_region_sync_for_device(&iso
->data_buf
, offset
, len
);
2067 /* append first_packet to the DMA chain */
2068 /* by linking the previous descriptor to it */
2069 /* (next will become the new end of the DMA chain) */
2071 next_i
= iso
->first_packet
;
2072 prev_i
= (next_i
== 0) ? (iso
->buf_packets
- 1) : (next_i
- 1);
2074 next
= dma_region_i(&xmit
->prog
, struct iso_xmit_cmd
, next_i
);
2075 prev
= dma_region_i(&xmit
->prog
, struct iso_xmit_cmd
, prev_i
);
2077 /* set up the OUTPUT_MORE_IMMEDIATE descriptor */
2078 memset(next
, 0, sizeof(struct iso_xmit_cmd
));
2079 next
->output_more_immediate
.control
= cpu_to_le32(0x02000008);
2081 /* ISO packet header is embedded in the OUTPUT_MORE_IMMEDIATE */
2083 /* tcode = 0xA, and sy */
2084 next
->iso_hdr
[0] = 0xA0 | (sy
& 0xF);
2086 /* tag and channel number */
2087 next
->iso_hdr
[1] = (tag
<< 6) | (iso
->channel
& 0x3F);
2089 /* transmission speed */
2090 next
->iso_hdr
[2] = iso
->speed
& 0x7;
2093 next
->iso_hdr
[6] = len
& 0xFF;
2094 next
->iso_hdr
[7] = len
>> 8;
2096 /* set up the OUTPUT_LAST */
2097 next
->output_last
.control
= cpu_to_le32(1 << 28);
2098 next
->output_last
.control
|= cpu_to_le32(1 << 27); /* update timeStamp */
2099 next
->output_last
.control
|= cpu_to_le32(3 << 20); /* want interrupt */
2100 next
->output_last
.control
|= cpu_to_le32(3 << 18); /* enable branch */
2101 next
->output_last
.control
|= cpu_to_le32(len
);
2103 /* payload bus address */
2104 next
->output_last
.address
= cpu_to_le32(dma_region_offset_to_bus(&iso
->data_buf
, offset
));
2106 /* leave branchAddress at zero for now */
2108 /* re-write the previous DMA descriptor to chain to this one */
2110 /* set prev branch address to point to next (Z=3) */
2111 prev
->output_last
.branchAddress
= cpu_to_le32(
2112 dma_prog_region_offset_to_bus(&xmit
->prog
, sizeof(struct iso_xmit_cmd
) * next_i
) | 3);
2114 /* disable interrupt, unless required by the IRQ interval */
2115 if (prev_i
% iso
->irq_interval
) {
2116 prev
->output_last
.control
&= cpu_to_le32(~(3 << 20)); /* no interrupt */
2118 prev
->output_last
.control
|= cpu_to_le32(3 << 20); /* enable interrupt */
2123 /* wake DMA in case it is sleeping */
2124 reg_write(xmit
->ohci
, xmit
->ContextControlSet
, 1 << 12);
2126 /* issue a dummy read of the cycle timer to force all PCI
2127 writes to be posted immediately */
2129 reg_read(xmit
->ohci
, OHCI1394_IsochronousCycleTimer
);
2134 static int ohci_iso_xmit_start(struct hpsb_iso
*iso
, int cycle
)
2136 struct ohci_iso_xmit
*xmit
= iso
->hostdata
;
2137 struct ti_ohci
*ohci
= xmit
->ohci
;
2139 /* clear out the control register */
2140 reg_write(xmit
->ohci
, xmit
->ContextControlClear
, 0xFFFFFFFF);
2143 /* address and length of first descriptor block (Z=3) */
2144 reg_write(xmit
->ohci
, xmit
->CommandPtr
,
2145 dma_prog_region_offset_to_bus(&xmit
->prog
, iso
->pkt_dma
* sizeof(struct iso_xmit_cmd
)) | 3);
2149 u32 start
= cycle
& 0x1FFF;
2151 /* 'cycle' is only mod 8000, but we also need two 'seconds' bits -
2152 just snarf them from the current time */
2153 u32 seconds
= reg_read(xmit
->ohci
, OHCI1394_IsochronousCycleTimer
) >> 25;
2155 /* advance one second to give some extra time for DMA to start */
2158 start
|= (seconds
& 3) << 13;
2160 reg_write(xmit
->ohci
, xmit
->ContextControlSet
, 0x80000000 | (start
<< 16));
2163 /* enable interrupts */
2164 reg_write(xmit
->ohci
, OHCI1394_IsoXmitIntMaskSet
, 1 << xmit
->task
.context
);
2167 reg_write(xmit
->ohci
, xmit
->ContextControlSet
, 0x8000);
2170 /* wait 100 usec to give the card time to go active */
2173 /* check the RUN bit */
2174 if (!(reg_read(xmit
->ohci
, xmit
->ContextControlSet
) & 0x8000)) {
2175 PRINT(KERN_ERR
, "Error starting IT DMA (ContextControl 0x%08x)\n",
2176 reg_read(xmit
->ohci
, xmit
->ContextControlSet
));
2183 static int ohci_isoctl(struct hpsb_iso
*iso
, enum isoctl_cmd cmd
, unsigned long arg
)
2188 return ohci_iso_xmit_init(iso
);
2190 return ohci_iso_xmit_start(iso
, arg
);
2192 ohci_iso_xmit_stop(iso
);
2195 return ohci_iso_xmit_queue(iso
, (struct hpsb_iso_packet_info
*) arg
);
2197 ohci_iso_xmit_shutdown(iso
);
2201 return ohci_iso_recv_init(iso
);
2203 int *args
= (int*) arg
;
2204 return ohci_iso_recv_start(iso
, args
[0], args
[1], args
[2]);
2207 ohci_iso_recv_stop(iso
);
2210 ohci_iso_recv_release(iso
, (struct hpsb_iso_packet_info
*) arg
);
2213 ohci_iso_recv_task((unsigned long) iso
);
2216 ohci_iso_recv_shutdown(iso
);
2218 case RECV_LISTEN_CHANNEL
:
2219 ohci_iso_recv_change_channel(iso
, arg
, 1);
2221 case RECV_UNLISTEN_CHANNEL
:
2222 ohci_iso_recv_change_channel(iso
, arg
, 0);
2224 case RECV_SET_CHANNEL_MASK
:
2225 ohci_iso_recv_set_channel_mask(iso
, *((u64
*) arg
));
2229 PRINT_G(KERN_ERR
, "ohci_isoctl cmd %d not implemented yet",
2236 /***************************************
2237 * IEEE-1394 functionality section END *
2238 ***************************************/
2241 /********************************************************
2242 * Global stuff (interrupt handler, init/shutdown code) *
2243 ********************************************************/
2245 static void dma_trm_reset(struct dma_trm_ctx
*d
)
2247 unsigned long flags
;
2248 LIST_HEAD(packet_list
);
2249 struct ti_ohci
*ohci
= d
->ohci
;
2250 struct hpsb_packet
*packet
, *ptmp
;
2252 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
2254 /* Lock the context, reset it and release it. Move the packets
2255 * that were pending in the context to packet_list and free
2256 * them after releasing the lock. */
2258 spin_lock_irqsave(&d
->lock
, flags
);
2260 list_splice(&d
->fifo_list
, &packet_list
);
2261 list_splice(&d
->pending_list
, &packet_list
);
2262 INIT_LIST_HEAD(&d
->fifo_list
);
2263 INIT_LIST_HEAD(&d
->pending_list
);
2265 d
->branchAddrPtr
= NULL
;
2266 d
->sent_ind
= d
->prg_ind
;
2267 d
->free_prgs
= d
->num_desc
;
2269 spin_unlock_irqrestore(&d
->lock
, flags
);
2271 if (list_empty(&packet_list
))
2274 PRINT(KERN_INFO
, "AT dma reset ctx=%d, aborting transmission", d
->ctx
);
2276 /* Now process subsystem callbacks for the packets from this
2278 list_for_each_entry_safe(packet
, ptmp
, &packet_list
, driver_list
) {
2279 list_del_init(&packet
->driver_list
);
2280 hpsb_packet_sent(ohci
->host
, packet
, ACKX_ABORTED
);
2284 static void ohci_schedule_iso_tasklets(struct ti_ohci
*ohci
,
2288 struct ohci1394_iso_tasklet
*t
;
2290 unsigned long flags
;
2292 spin_lock_irqsave(&ohci
->iso_tasklet_list_lock
, flags
);
2294 list_for_each_entry(t
, &ohci
->iso_tasklet_list
, link
) {
2295 mask
= 1 << t
->context
;
2297 if (t
->type
== OHCI_ISO_TRANSMIT
&& tx_event
& mask
)
2298 tasklet_schedule(&t
->tasklet
);
2299 else if (rx_event
& mask
)
2300 tasklet_schedule(&t
->tasklet
);
2303 spin_unlock_irqrestore(&ohci
->iso_tasklet_list_lock
, flags
);
2306 static irqreturn_t
ohci_irq_handler(int irq
, void *dev_id
,
2307 struct pt_regs
*regs_are_unused
)
2309 quadlet_t event
, node_id
;
2310 struct ti_ohci
*ohci
= (struct ti_ohci
*)dev_id
;
2311 struct hpsb_host
*host
= ohci
->host
;
2312 int phyid
= -1, isroot
= 0;
2313 unsigned long flags
;
2315 /* Read and clear the interrupt event register. Don't clear
2316 * the busReset event, though. This is done when we get the
2317 * selfIDComplete interrupt. */
2318 spin_lock_irqsave(&ohci
->event_lock
, flags
);
2319 event
= reg_read(ohci
, OHCI1394_IntEventClear
);
2320 reg_write(ohci
, OHCI1394_IntEventClear
, event
& ~OHCI1394_busReset
);
2321 spin_unlock_irqrestore(&ohci
->event_lock
, flags
);
2326 /* If event is ~(u32)0 cardbus card was ejected. In this case
2327 * we just return, and clean up in the ohci1394_pci_remove
2329 if (event
== ~(u32
) 0) {
2330 DBGMSG("Device removed.");
2334 DBGMSG("IntEvent: %08x", event
);
2336 if (event
& OHCI1394_unrecoverableError
) {
2338 PRINT(KERN_ERR
, "Unrecoverable error!");
2340 if (reg_read(ohci
, OHCI1394_AsReqTrContextControlSet
) & 0x800)
2341 PRINT(KERN_ERR
, "Async Req Tx Context died: "
2342 "ctrl[%08x] cmdptr[%08x]",
2343 reg_read(ohci
, OHCI1394_AsReqTrContextControlSet
),
2344 reg_read(ohci
, OHCI1394_AsReqTrCommandPtr
));
2346 if (reg_read(ohci
, OHCI1394_AsRspTrContextControlSet
) & 0x800)
2347 PRINT(KERN_ERR
, "Async Rsp Tx Context died: "
2348 "ctrl[%08x] cmdptr[%08x]",
2349 reg_read(ohci
, OHCI1394_AsRspTrContextControlSet
),
2350 reg_read(ohci
, OHCI1394_AsRspTrCommandPtr
));
2352 if (reg_read(ohci
, OHCI1394_AsReqRcvContextControlSet
) & 0x800)
2353 PRINT(KERN_ERR
, "Async Req Rcv Context died: "
2354 "ctrl[%08x] cmdptr[%08x]",
2355 reg_read(ohci
, OHCI1394_AsReqRcvContextControlSet
),
2356 reg_read(ohci
, OHCI1394_AsReqRcvCommandPtr
));
2358 if (reg_read(ohci
, OHCI1394_AsRspRcvContextControlSet
) & 0x800)
2359 PRINT(KERN_ERR
, "Async Rsp Rcv Context died: "
2360 "ctrl[%08x] cmdptr[%08x]",
2361 reg_read(ohci
, OHCI1394_AsRspRcvContextControlSet
),
2362 reg_read(ohci
, OHCI1394_AsRspRcvCommandPtr
));
2364 for (ctx
= 0; ctx
< ohci
->nb_iso_xmit_ctx
; ctx
++) {
2365 if (reg_read(ohci
, OHCI1394_IsoXmitContextControlSet
+ (16 * ctx
)) & 0x800)
2366 PRINT(KERN_ERR
, "Iso Xmit %d Context died: "
2367 "ctrl[%08x] cmdptr[%08x]", ctx
,
2368 reg_read(ohci
, OHCI1394_IsoXmitContextControlSet
+ (16 * ctx
)),
2369 reg_read(ohci
, OHCI1394_IsoXmitCommandPtr
+ (16 * ctx
)));
2372 for (ctx
= 0; ctx
< ohci
->nb_iso_rcv_ctx
; ctx
++) {
2373 if (reg_read(ohci
, OHCI1394_IsoRcvContextControlSet
+ (32 * ctx
)) & 0x800)
2374 PRINT(KERN_ERR
, "Iso Recv %d Context died: "
2375 "ctrl[%08x] cmdptr[%08x] match[%08x]", ctx
,
2376 reg_read(ohci
, OHCI1394_IsoRcvContextControlSet
+ (32 * ctx
)),
2377 reg_read(ohci
, OHCI1394_IsoRcvCommandPtr
+ (32 * ctx
)),
2378 reg_read(ohci
, OHCI1394_IsoRcvContextMatch
+ (32 * ctx
)));
2381 event
&= ~OHCI1394_unrecoverableError
;
2383 if (event
& OHCI1394_postedWriteErr
) {
2384 PRINT(KERN_ERR
, "physical posted write error");
2385 /* no recovery strategy yet, had to involve protocol drivers */
2387 if (event
& OHCI1394_cycleTooLong
) {
2388 if(printk_ratelimit())
2389 PRINT(KERN_WARNING
, "isochronous cycle too long");
2391 DBGMSG("OHCI1394_cycleTooLong");
2392 reg_write(ohci
, OHCI1394_LinkControlSet
,
2393 OHCI1394_LinkControl_CycleMaster
);
2394 event
&= ~OHCI1394_cycleTooLong
;
2396 if (event
& OHCI1394_cycleInconsistent
) {
2397 /* We subscribe to the cycleInconsistent event only to
2398 * clear the corresponding event bit... otherwise,
2399 * isochronous cycleMatch DMA won't work. */
2400 DBGMSG("OHCI1394_cycleInconsistent");
2401 event
&= ~OHCI1394_cycleInconsistent
;
2403 if (event
& OHCI1394_busReset
) {
2404 /* The busReset event bit can't be cleared during the
2405 * selfID phase, so we disable busReset interrupts, to
2406 * avoid burying the cpu in interrupt requests. */
2407 spin_lock_irqsave(&ohci
->event_lock
, flags
);
2408 reg_write(ohci
, OHCI1394_IntMaskClear
, OHCI1394_busReset
);
2410 if (ohci
->check_busreset
) {
2415 while (reg_read(ohci
, OHCI1394_IntEventSet
) & OHCI1394_busReset
) {
2416 reg_write(ohci
, OHCI1394_IntEventClear
, OHCI1394_busReset
);
2418 spin_unlock_irqrestore(&ohci
->event_lock
, flags
);
2420 spin_lock_irqsave(&ohci
->event_lock
, flags
);
2422 /* The loop counter check is to prevent the driver
2423 * from remaining in this state forever. For the
2424 * initial bus reset, the loop continues for ever
2425 * and the system hangs, until some device is plugged-in
2426 * or out manually into a port! The forced reset seems
2427 * to solve this problem. This mainly effects nForce2. */
2428 if (loop_count
> 10000) {
2429 ohci_devctl(host
, RESET_BUS
, LONG_RESET
);
2430 DBGMSG("Detected bus-reset loop. Forced a bus reset!");
2437 spin_unlock_irqrestore(&ohci
->event_lock
, flags
);
2438 if (!host
->in_bus_reset
) {
2439 DBGMSG("irq_handler: Bus reset requested");
2441 /* Subsystem call */
2442 hpsb_bus_reset(ohci
->host
);
2444 event
&= ~OHCI1394_busReset
;
2446 if (event
& OHCI1394_reqTxComplete
) {
2447 struct dma_trm_ctx
*d
= &ohci
->at_req_context
;
2448 DBGMSG("Got reqTxComplete interrupt "
2449 "status=0x%08X", reg_read(ohci
, d
->ctrlSet
));
2450 if (reg_read(ohci
, d
->ctrlSet
) & 0x800)
2451 ohci1394_stop_context(ohci
, d
->ctrlClear
,
2454 dma_trm_tasklet((unsigned long)d
);
2455 //tasklet_schedule(&d->task);
2456 event
&= ~OHCI1394_reqTxComplete
;
2458 if (event
& OHCI1394_respTxComplete
) {
2459 struct dma_trm_ctx
*d
= &ohci
->at_resp_context
;
2460 DBGMSG("Got respTxComplete interrupt "
2461 "status=0x%08X", reg_read(ohci
, d
->ctrlSet
));
2462 if (reg_read(ohci
, d
->ctrlSet
) & 0x800)
2463 ohci1394_stop_context(ohci
, d
->ctrlClear
,
2466 tasklet_schedule(&d
->task
);
2467 event
&= ~OHCI1394_respTxComplete
;
2469 if (event
& OHCI1394_RQPkt
) {
2470 struct dma_rcv_ctx
*d
= &ohci
->ar_req_context
;
2471 DBGMSG("Got RQPkt interrupt status=0x%08X",
2472 reg_read(ohci
, d
->ctrlSet
));
2473 if (reg_read(ohci
, d
->ctrlSet
) & 0x800)
2474 ohci1394_stop_context(ohci
, d
->ctrlClear
, "RQPkt");
2476 tasklet_schedule(&d
->task
);
2477 event
&= ~OHCI1394_RQPkt
;
2479 if (event
& OHCI1394_RSPkt
) {
2480 struct dma_rcv_ctx
*d
= &ohci
->ar_resp_context
;
2481 DBGMSG("Got RSPkt interrupt status=0x%08X",
2482 reg_read(ohci
, d
->ctrlSet
));
2483 if (reg_read(ohci
, d
->ctrlSet
) & 0x800)
2484 ohci1394_stop_context(ohci
, d
->ctrlClear
, "RSPkt");
2486 tasklet_schedule(&d
->task
);
2487 event
&= ~OHCI1394_RSPkt
;
2489 if (event
& OHCI1394_isochRx
) {
2492 rx_event
= reg_read(ohci
, OHCI1394_IsoRecvIntEventSet
);
2493 reg_write(ohci
, OHCI1394_IsoRecvIntEventClear
, rx_event
);
2494 ohci_schedule_iso_tasklets(ohci
, rx_event
, 0);
2495 event
&= ~OHCI1394_isochRx
;
2497 if (event
& OHCI1394_isochTx
) {
2500 tx_event
= reg_read(ohci
, OHCI1394_IsoXmitIntEventSet
);
2501 reg_write(ohci
, OHCI1394_IsoXmitIntEventClear
, tx_event
);
2502 ohci_schedule_iso_tasklets(ohci
, 0, tx_event
);
2503 event
&= ~OHCI1394_isochTx
;
2505 if (event
& OHCI1394_selfIDComplete
) {
2506 if (host
->in_bus_reset
) {
2507 node_id
= reg_read(ohci
, OHCI1394_NodeID
);
2509 if (!(node_id
& 0x80000000)) {
2511 "SelfID received, but NodeID invalid "
2512 "(probably new bus reset occurred): %08X",
2514 goto selfid_not_valid
;
2517 phyid
= node_id
& 0x0000003f;
2518 isroot
= (node_id
& 0x40000000) != 0;
2520 DBGMSG("SelfID interrupt received "
2521 "(phyid %d, %s)", phyid
,
2522 (isroot
? "root" : "not root"));
2524 handle_selfid(ohci
, host
, phyid
, isroot
);
2526 /* Clear the bus reset event and re-enable the
2527 * busReset interrupt. */
2528 spin_lock_irqsave(&ohci
->event_lock
, flags
);
2529 reg_write(ohci
, OHCI1394_IntEventClear
, OHCI1394_busReset
);
2530 reg_write(ohci
, OHCI1394_IntMaskSet
, OHCI1394_busReset
);
2531 spin_unlock_irqrestore(&ohci
->event_lock
, flags
);
2533 /* Turn on phys dma reception.
2535 * TODO: Enable some sort of filtering management.
2538 reg_write(ohci
, OHCI1394_PhyReqFilterHiSet
,
2540 reg_write(ohci
, OHCI1394_PhyReqFilterLoSet
,
2544 DBGMSG("PhyReqFilter=%08x%08x",
2545 reg_read(ohci
, OHCI1394_PhyReqFilterHiSet
),
2546 reg_read(ohci
, OHCI1394_PhyReqFilterLoSet
));
2548 hpsb_selfid_complete(host
, phyid
, isroot
);
2551 "SelfID received outside of bus reset sequence");
2554 event
&= ~OHCI1394_selfIDComplete
;
2557 /* Make sure we handle everything, just in case we accidentally
2558 * enabled an interrupt that we didn't write a handler for. */
2560 PRINT(KERN_ERR
, "Unhandled interrupt(s) 0x%08x",
2566 /* Put the buffer back into the dma context */
2567 static void insert_dma_buffer(struct dma_rcv_ctx
*d
, int idx
)
2569 struct ti_ohci
*ohci
= (struct ti_ohci
*)(d
->ohci
);
2570 DBGMSG("Inserting dma buf ctx=%d idx=%d", d
->ctx
, idx
);
2572 d
->prg_cpu
[idx
]->status
= cpu_to_le32(d
->buf_size
);
2573 d
->prg_cpu
[idx
]->branchAddress
&= le32_to_cpu(0xfffffff0);
2574 idx
= (idx
+ d
->num_desc
- 1 ) % d
->num_desc
;
2575 d
->prg_cpu
[idx
]->branchAddress
|= le32_to_cpu(0x00000001);
2577 /* To avoid a race, ensure 1394 interface hardware sees the inserted
2578 * context program descriptors before it sees the wakeup bit set. */
2581 /* wake up the dma context if necessary */
2582 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x400)) {
2584 "Waking dma ctx=%d ... processing is probably too slow",
2588 /* do this always, to avoid race condition */
2589 reg_write(ohci
, d
->ctrlSet
, 0x1000);
2592 #define cond_le32_to_cpu(data, noswap) \
2593 (noswap ? data : le32_to_cpu(data))
2595 static const int TCODE_SIZE
[16] = {20, 0, 16, -1, 16, 20, 20, 0,
2596 -1, 0, -1, 0, -1, -1, 16, -1};
2599 * Determine the length of a packet in the buffer
2600 * Optimization suggested by Pascal Drolet <pascal.drolet@informission.ca>
2602 static __inline__
int packet_length(struct dma_rcv_ctx
*d
, int idx
, quadlet_t
*buf_ptr
,
2603 int offset
, unsigned char tcode
, int noswap
)
2607 if (d
->type
== DMA_CTX_ASYNC_REQ
|| d
->type
== DMA_CTX_ASYNC_RESP
) {
2608 length
= TCODE_SIZE
[tcode
];
2610 if (offset
+ 12 >= d
->buf_size
) {
2611 length
= (cond_le32_to_cpu(d
->buf_cpu
[(idx
+ 1) % d
->num_desc
]
2612 [3 - ((d
->buf_size
- offset
) >> 2)], noswap
) >> 16);
2614 length
= (cond_le32_to_cpu(buf_ptr
[3], noswap
) >> 16);
2618 } else if (d
->type
== DMA_CTX_ISO
) {
2619 /* Assumption: buffer fill mode with header/trailer */
2620 length
= (cond_le32_to_cpu(buf_ptr
[0], noswap
) >> 16) + 8;
2623 if (length
> 0 && length
% 4)
2624 length
+= 4 - (length
% 4);
2629 /* Tasklet that processes dma receive buffers */
2630 static void dma_rcv_tasklet (unsigned long data
)
2632 struct dma_rcv_ctx
*d
= (struct dma_rcv_ctx
*)data
;
2633 struct ti_ohci
*ohci
= (struct ti_ohci
*)(d
->ohci
);
2634 unsigned int split_left
, idx
, offset
, rescount
;
2635 unsigned char tcode
;
2636 int length
, bytes_left
, ack
;
2637 unsigned long flags
;
2642 spin_lock_irqsave(&d
->lock
, flags
);
2645 offset
= d
->buf_offset
;
2646 buf_ptr
= d
->buf_cpu
[idx
] + offset
/4;
2648 rescount
= le32_to_cpu(d
->prg_cpu
[idx
]->status
) & 0xffff;
2649 bytes_left
= d
->buf_size
- rescount
- offset
;
2651 while (bytes_left
> 0) {
2652 tcode
= (cond_le32_to_cpu(buf_ptr
[0], ohci
->no_swap_incoming
) >> 4) & 0xf;
2654 /* packet_length() will return < 4 for an error */
2655 length
= packet_length(d
, idx
, buf_ptr
, offset
, tcode
, ohci
->no_swap_incoming
);
2657 if (length
< 4) { /* something is wrong */
2658 sprintf(msg
,"Unexpected tcode 0x%x(0x%08x) in AR ctx=%d, length=%d",
2659 tcode
, cond_le32_to_cpu(buf_ptr
[0], ohci
->no_swap_incoming
),
2661 ohci1394_stop_context(ohci
, d
->ctrlClear
, msg
);
2662 spin_unlock_irqrestore(&d
->lock
, flags
);
2666 /* The first case is where we have a packet that crosses
2667 * over more than one descriptor. The next case is where
2668 * it's all in the first descriptor. */
2669 if ((offset
+ length
) > d
->buf_size
) {
2670 DBGMSG("Split packet rcv'd");
2671 if (length
> d
->split_buf_size
) {
2672 ohci1394_stop_context(ohci
, d
->ctrlClear
,
2673 "Split packet size exceeded");
2675 d
->buf_offset
= offset
;
2676 spin_unlock_irqrestore(&d
->lock
, flags
);
2680 if (le32_to_cpu(d
->prg_cpu
[(idx
+1)%d
->num_desc
]->status
)
2682 /* Other part of packet not written yet.
2683 * this should never happen I think
2684 * anyway we'll get it on the next call. */
2686 "Got only half a packet!");
2688 d
->buf_offset
= offset
;
2689 spin_unlock_irqrestore(&d
->lock
, flags
);
2693 split_left
= length
;
2694 split_ptr
= (char *)d
->spb
;
2695 memcpy(split_ptr
,buf_ptr
,d
->buf_size
-offset
);
2696 split_left
-= d
->buf_size
-offset
;
2697 split_ptr
+= d
->buf_size
-offset
;
2698 insert_dma_buffer(d
, idx
);
2699 idx
= (idx
+1) % d
->num_desc
;
2700 buf_ptr
= d
->buf_cpu
[idx
];
2703 while (split_left
>= d
->buf_size
) {
2704 memcpy(split_ptr
,buf_ptr
,d
->buf_size
);
2705 split_ptr
+= d
->buf_size
;
2706 split_left
-= d
->buf_size
;
2707 insert_dma_buffer(d
, idx
);
2708 idx
= (idx
+1) % d
->num_desc
;
2709 buf_ptr
= d
->buf_cpu
[idx
];
2712 if (split_left
> 0) {
2713 memcpy(split_ptr
, buf_ptr
, split_left
);
2714 offset
= split_left
;
2715 buf_ptr
+= offset
/4;
2718 DBGMSG("Single packet rcv'd");
2719 memcpy(d
->spb
, buf_ptr
, length
);
2721 buf_ptr
+= length
/4;
2722 if (offset
==d
->buf_size
) {
2723 insert_dma_buffer(d
, idx
);
2724 idx
= (idx
+1) % d
->num_desc
;
2725 buf_ptr
= d
->buf_cpu
[idx
];
2730 /* We get one phy packet to the async descriptor for each
2731 * bus reset. We always ignore it. */
2732 if (tcode
!= OHCI1394_TCODE_PHY
) {
2733 if (!ohci
->no_swap_incoming
)
2734 packet_swab(d
->spb
, tcode
);
2735 DBGMSG("Packet received from node"
2736 " %d ack=0x%02X spd=%d tcode=0x%X"
2737 " length=%d ctx=%d tlabel=%d",
2738 (d
->spb
[1]>>16)&0x3f,
2739 (cond_le32_to_cpu(d
->spb
[length
/4-1], ohci
->no_swap_incoming
)>>16)&0x1f,
2740 (cond_le32_to_cpu(d
->spb
[length
/4-1], ohci
->no_swap_incoming
)>>21)&0x3,
2741 tcode
, length
, d
->ctx
,
2742 (cond_le32_to_cpu(d
->spb
[0], ohci
->no_swap_incoming
)>>10)&0x3f);
2744 ack
= (((cond_le32_to_cpu(d
->spb
[length
/4-1], ohci
->no_swap_incoming
)>>16)&0x1f)
2747 hpsb_packet_received(ohci
->host
, d
->spb
,
2750 #ifdef OHCI1394_DEBUG
2752 PRINT (KERN_DEBUG
, "Got phy packet ctx=%d ... discarded",
2756 rescount
= le32_to_cpu(d
->prg_cpu
[idx
]->status
) & 0xffff;
2758 bytes_left
= d
->buf_size
- rescount
- offset
;
2763 d
->buf_offset
= offset
;
2765 spin_unlock_irqrestore(&d
->lock
, flags
);
2768 /* Bottom half that processes sent packets */
2769 static void dma_trm_tasklet (unsigned long data
)
2771 struct dma_trm_ctx
*d
= (struct dma_trm_ctx
*)data
;
2772 struct ti_ohci
*ohci
= (struct ti_ohci
*)(d
->ohci
);
2773 struct hpsb_packet
*packet
, *ptmp
;
2774 unsigned long flags
;
2778 spin_lock_irqsave(&d
->lock
, flags
);
2780 list_for_each_entry_safe(packet
, ptmp
, &d
->fifo_list
, driver_list
) {
2781 datasize
= packet
->data_size
;
2782 if (datasize
&& packet
->type
!= hpsb_raw
)
2783 status
= le32_to_cpu(
2784 d
->prg_cpu
[d
->sent_ind
]->end
.status
) >> 16;
2786 status
= le32_to_cpu(
2787 d
->prg_cpu
[d
->sent_ind
]->begin
.status
) >> 16;
2790 /* this packet hasn't been sent yet*/
2793 #ifdef OHCI1394_DEBUG
2795 if (((le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])>>4)&0xf) == 0xa)
2796 DBGMSG("Stream packet sent to channel %d tcode=0x%X "
2797 "ack=0x%X spd=%d dataLength=%d ctx=%d",
2798 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])>>8)&0x3f,
2799 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])>>4)&0xf,
2800 status
&0x1f, (status
>>5)&0x3,
2801 le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[1])>>16,
2804 DBGMSG("Packet sent to node %d tcode=0x%X tLabel="
2805 "%d ack=0x%X spd=%d dataLength=%d ctx=%d",
2806 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[1])>>16)&0x3f,
2807 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])>>4)&0xf,
2808 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])>>10)&0x3f,
2809 status
&0x1f, (status
>>5)&0x3,
2810 le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[3])>>16,
2813 DBGMSG("Packet sent to node %d tcode=0x%X tLabel="
2814 "%d ack=0x%X spd=%d data=0x%08X ctx=%d",
2815 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[1])
2817 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])
2819 (le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[0])
2821 status
&0x1f, (status
>>5)&0x3,
2822 le32_to_cpu(d
->prg_cpu
[d
->sent_ind
]->data
[3]),
2826 if (status
& 0x10) {
2829 switch (status
& 0x1f) {
2830 case EVT_NO_STATUS
: /* that should never happen */
2831 case EVT_RESERVED_A
: /* that should never happen */
2832 case EVT_LONG_PACKET
: /* that should never happen */
2833 PRINT(KERN_WARNING
, "Received OHCI evt_* error 0x%x", status
& 0x1f);
2834 ack
= ACKX_SEND_ERROR
;
2836 case EVT_MISSING_ACK
:
2840 ack
= ACKX_SEND_ERROR
;
2842 case EVT_OVERRUN
: /* that should never happen */
2843 PRINT(KERN_WARNING
, "Received OHCI evt_* error 0x%x", status
& 0x1f);
2844 ack
= ACKX_SEND_ERROR
;
2846 case EVT_DESCRIPTOR_READ
:
2848 case EVT_DATA_WRITE
:
2849 ack
= ACKX_SEND_ERROR
;
2851 case EVT_BUS_RESET
: /* that should never happen */
2852 PRINT(KERN_WARNING
, "Received OHCI evt_* error 0x%x", status
& 0x1f);
2853 ack
= ACKX_SEND_ERROR
;
2859 ack
= ACKX_SEND_ERROR
;
2861 case EVT_RESERVED_B
: /* that should never happen */
2862 case EVT_RESERVED_C
: /* that should never happen */
2863 PRINT(KERN_WARNING
, "Received OHCI evt_* error 0x%x", status
& 0x1f);
2864 ack
= ACKX_SEND_ERROR
;
2868 ack
= ACKX_SEND_ERROR
;
2871 PRINT(KERN_ERR
, "Unhandled OHCI evt_* error 0x%x", status
& 0x1f);
2872 ack
= ACKX_SEND_ERROR
;
2877 list_del_init(&packet
->driver_list
);
2878 hpsb_packet_sent(ohci
->host
, packet
, ack
);
2881 pci_unmap_single(ohci
->dev
,
2882 cpu_to_le32(d
->prg_cpu
[d
->sent_ind
]->end
.address
),
2883 datasize
, PCI_DMA_TODEVICE
);
2884 OHCI_DMA_FREE("single Xmit data packet");
2887 d
->sent_ind
= (d
->sent_ind
+1)%d
->num_desc
;
2891 dma_trm_flush(ohci
, d
);
2893 spin_unlock_irqrestore(&d
->lock
, flags
);
2896 static void stop_dma_rcv_ctx(struct dma_rcv_ctx
*d
)
2899 ohci1394_stop_context(d
->ohci
, d
->ctrlClear
, NULL
);
2901 if (d
->type
== DMA_CTX_ISO
) {
2902 /* disable interrupts */
2903 reg_write(d
->ohci
, OHCI1394_IsoRecvIntMaskClear
, 1 << d
->ctx
);
2904 ohci1394_unregister_iso_tasklet(d
->ohci
, &d
->ohci
->ir_legacy_tasklet
);
2906 tasklet_kill(&d
->task
);
2912 static void free_dma_rcv_ctx(struct dma_rcv_ctx
*d
)
2915 struct ti_ohci
*ohci
= d
->ohci
;
2920 DBGMSG("Freeing dma_rcv_ctx %d", d
->ctx
);
2923 for (i
=0; i
<d
->num_desc
; i
++)
2924 if (d
->buf_cpu
[i
] && d
->buf_bus
[i
]) {
2925 pci_free_consistent(
2926 ohci
->dev
, d
->buf_size
,
2927 d
->buf_cpu
[i
], d
->buf_bus
[i
]);
2928 OHCI_DMA_FREE("consistent dma_rcv buf[%d]", i
);
2934 for (i
=0; i
<d
->num_desc
; i
++)
2935 if (d
->prg_cpu
[i
] && d
->prg_bus
[i
]) {
2936 pci_pool_free(d
->prg_pool
, d
->prg_cpu
[i
], d
->prg_bus
[i
]);
2937 OHCI_DMA_FREE("consistent dma_rcv prg[%d]", i
);
2939 pci_pool_destroy(d
->prg_pool
);
2940 OHCI_DMA_FREE("dma_rcv prg pool");
2946 /* Mark this context as freed. */
2951 alloc_dma_rcv_ctx(struct ti_ohci
*ohci
, struct dma_rcv_ctx
*d
,
2952 enum context_type type
, int ctx
, int num_desc
,
2953 int buf_size
, int split_buf_size
, int context_base
)
2956 static int num_allocs
;
2957 static char pool_name
[20];
2963 d
->num_desc
= num_desc
;
2964 d
->buf_size
= buf_size
;
2965 d
->split_buf_size
= split_buf_size
;
2971 d
->buf_cpu
= kzalloc(d
->num_desc
* sizeof(*d
->buf_cpu
), GFP_ATOMIC
);
2972 d
->buf_bus
= kzalloc(d
->num_desc
* sizeof(*d
->buf_bus
), GFP_ATOMIC
);
2974 if (d
->buf_cpu
== NULL
|| d
->buf_bus
== NULL
) {
2975 PRINT(KERN_ERR
, "Failed to allocate dma buffer");
2976 free_dma_rcv_ctx(d
);
2980 d
->prg_cpu
= kzalloc(d
->num_desc
* sizeof(*d
->prg_cpu
), GFP_ATOMIC
);
2981 d
->prg_bus
= kzalloc(d
->num_desc
* sizeof(*d
->prg_bus
), GFP_ATOMIC
);
2983 if (d
->prg_cpu
== NULL
|| d
->prg_bus
== NULL
) {
2984 PRINT(KERN_ERR
, "Failed to allocate dma prg");
2985 free_dma_rcv_ctx(d
);
2989 d
->spb
= kmalloc(d
->split_buf_size
, GFP_ATOMIC
);
2991 if (d
->spb
== NULL
) {
2992 PRINT(KERN_ERR
, "Failed to allocate split buffer");
2993 free_dma_rcv_ctx(d
);
2997 len
= sprintf(pool_name
, "ohci1394_rcv_prg");
2998 sprintf(pool_name
+len
, "%d", num_allocs
);
2999 d
->prg_pool
= pci_pool_create(pool_name
, ohci
->dev
,
3000 sizeof(struct dma_cmd
), 4, 0);
3001 if(d
->prg_pool
== NULL
)
3003 PRINT(KERN_ERR
, "pci_pool_create failed for %s", pool_name
);
3004 free_dma_rcv_ctx(d
);
3009 OHCI_DMA_ALLOC("dma_rcv prg pool");
3011 for (i
=0; i
<d
->num_desc
; i
++) {
3012 d
->buf_cpu
[i
] = pci_alloc_consistent(ohci
->dev
,
3015 OHCI_DMA_ALLOC("consistent dma_rcv buf[%d]", i
);
3017 if (d
->buf_cpu
[i
] != NULL
) {
3018 memset(d
->buf_cpu
[i
], 0, d
->buf_size
);
3021 "Failed to allocate dma buffer");
3022 free_dma_rcv_ctx(d
);
3026 d
->prg_cpu
[i
] = pci_pool_alloc(d
->prg_pool
, SLAB_KERNEL
, d
->prg_bus
+i
);
3027 OHCI_DMA_ALLOC("pool dma_rcv prg[%d]", i
);
3029 if (d
->prg_cpu
[i
] != NULL
) {
3030 memset(d
->prg_cpu
[i
], 0, sizeof(struct dma_cmd
));
3033 "Failed to allocate dma prg");
3034 free_dma_rcv_ctx(d
);
3039 spin_lock_init(&d
->lock
);
3041 if (type
== DMA_CTX_ISO
) {
3042 ohci1394_init_iso_tasklet(&ohci
->ir_legacy_tasklet
,
3043 OHCI_ISO_MULTICHANNEL_RECEIVE
,
3044 dma_rcv_tasklet
, (unsigned long) d
);
3046 d
->ctrlSet
= context_base
+ OHCI1394_ContextControlSet
;
3047 d
->ctrlClear
= context_base
+ OHCI1394_ContextControlClear
;
3048 d
->cmdPtr
= context_base
+ OHCI1394_ContextCommandPtr
;
3050 tasklet_init (&d
->task
, dma_rcv_tasklet
, (unsigned long) d
);
3056 static void free_dma_trm_ctx(struct dma_trm_ctx
*d
)
3059 struct ti_ohci
*ohci
= d
->ohci
;
3064 DBGMSG("Freeing dma_trm_ctx %d", d
->ctx
);
3067 for (i
=0; i
<d
->num_desc
; i
++)
3068 if (d
->prg_cpu
[i
] && d
->prg_bus
[i
]) {
3069 pci_pool_free(d
->prg_pool
, d
->prg_cpu
[i
], d
->prg_bus
[i
]);
3070 OHCI_DMA_FREE("pool dma_trm prg[%d]", i
);
3072 pci_pool_destroy(d
->prg_pool
);
3073 OHCI_DMA_FREE("dma_trm prg pool");
3078 /* Mark this context as freed. */
3083 alloc_dma_trm_ctx(struct ti_ohci
*ohci
, struct dma_trm_ctx
*d
,
3084 enum context_type type
, int ctx
, int num_desc
,
3088 static char pool_name
[20];
3089 static int num_allocs
=0;
3094 d
->num_desc
= num_desc
;
3099 d
->prg_cpu
= kzalloc(d
->num_desc
* sizeof(*d
->prg_cpu
), GFP_KERNEL
);
3100 d
->prg_bus
= kzalloc(d
->num_desc
* sizeof(*d
->prg_bus
), GFP_KERNEL
);
3102 if (d
->prg_cpu
== NULL
|| d
->prg_bus
== NULL
) {
3103 PRINT(KERN_ERR
, "Failed to allocate at dma prg");
3104 free_dma_trm_ctx(d
);
3108 len
= sprintf(pool_name
, "ohci1394_trm_prg");
3109 sprintf(pool_name
+len
, "%d", num_allocs
);
3110 d
->prg_pool
= pci_pool_create(pool_name
, ohci
->dev
,
3111 sizeof(struct at_dma_prg
), 4, 0);
3112 if (d
->prg_pool
== NULL
) {
3113 PRINT(KERN_ERR
, "pci_pool_create failed for %s", pool_name
);
3114 free_dma_trm_ctx(d
);
3119 OHCI_DMA_ALLOC("dma_rcv prg pool");
3121 for (i
= 0; i
< d
->num_desc
; i
++) {
3122 d
->prg_cpu
[i
] = pci_pool_alloc(d
->prg_pool
, SLAB_KERNEL
, d
->prg_bus
+i
);
3123 OHCI_DMA_ALLOC("pool dma_trm prg[%d]", i
);
3125 if (d
->prg_cpu
[i
] != NULL
) {
3126 memset(d
->prg_cpu
[i
], 0, sizeof(struct at_dma_prg
));
3129 "Failed to allocate at dma prg");
3130 free_dma_trm_ctx(d
);
3135 spin_lock_init(&d
->lock
);
3137 /* initialize tasklet */
3138 if (type
== DMA_CTX_ISO
) {
3139 ohci1394_init_iso_tasklet(&ohci
->it_legacy_tasklet
, OHCI_ISO_TRANSMIT
,
3140 dma_trm_tasklet
, (unsigned long) d
);
3141 if (ohci1394_register_iso_tasklet(ohci
,
3142 &ohci
->it_legacy_tasklet
) < 0) {
3143 PRINT(KERN_ERR
, "No IT DMA context available");
3144 free_dma_trm_ctx(d
);
3148 /* IT can be assigned to any context by register_iso_tasklet */
3149 d
->ctx
= ohci
->it_legacy_tasklet
.context
;
3150 d
->ctrlSet
= OHCI1394_IsoXmitContextControlSet
+ 16 * d
->ctx
;
3151 d
->ctrlClear
= OHCI1394_IsoXmitContextControlClear
+ 16 * d
->ctx
;
3152 d
->cmdPtr
= OHCI1394_IsoXmitCommandPtr
+ 16 * d
->ctx
;
3154 d
->ctrlSet
= context_base
+ OHCI1394_ContextControlSet
;
3155 d
->ctrlClear
= context_base
+ OHCI1394_ContextControlClear
;
3156 d
->cmdPtr
= context_base
+ OHCI1394_ContextCommandPtr
;
3157 tasklet_init (&d
->task
, dma_trm_tasklet
, (unsigned long)d
);
3163 static void ohci_set_hw_config_rom(struct hpsb_host
*host
, quadlet_t
*config_rom
)
3165 struct ti_ohci
*ohci
= host
->hostdata
;
3167 reg_write(ohci
, OHCI1394_ConfigROMhdr
, be32_to_cpu(config_rom
[0]));
3168 reg_write(ohci
, OHCI1394_BusOptions
, be32_to_cpu(config_rom
[2]));
3170 memcpy(ohci
->csr_config_rom_cpu
, config_rom
, OHCI_CONFIG_ROM_LEN
);
3174 static quadlet_t
ohci_hw_csr_reg(struct hpsb_host
*host
, int reg
,
3175 quadlet_t data
, quadlet_t compare
)
3177 struct ti_ohci
*ohci
= host
->hostdata
;
3180 reg_write(ohci
, OHCI1394_CSRData
, data
);
3181 reg_write(ohci
, OHCI1394_CSRCompareData
, compare
);
3182 reg_write(ohci
, OHCI1394_CSRControl
, reg
& 0x3);
3184 for (i
= 0; i
< OHCI_LOOP_COUNT
; i
++) {
3185 if (reg_read(ohci
, OHCI1394_CSRControl
) & 0x80000000)
3191 return reg_read(ohci
, OHCI1394_CSRData
);
3194 static struct hpsb_host_driver ohci1394_driver
= {
3195 .owner
= THIS_MODULE
,
3196 .name
= OHCI1394_DRIVER_NAME
,
3197 .set_hw_config_rom
= ohci_set_hw_config_rom
,
3198 .transmit_packet
= ohci_transmit
,
3199 .devctl
= ohci_devctl
,
3200 .isoctl
= ohci_isoctl
,
3201 .hw_csr_reg
= ohci_hw_csr_reg
,
3204 /***********************************
3205 * PCI Driver Interface functions *
3206 ***********************************/
3208 #define FAIL(err, fmt, args...) \
3210 PRINT_G(KERN_ERR, fmt , ## args); \
3211 ohci1394_pci_remove(dev); \
3215 static int __devinit
ohci1394_pci_probe(struct pci_dev
*dev
,
3216 const struct pci_device_id
*ent
)
3218 struct hpsb_host
*host
;
3219 struct ti_ohci
*ohci
; /* shortcut to currently handled device */
3220 unsigned long ohci_base
;
3222 if (pci_enable_device(dev
))
3223 FAIL(-ENXIO
, "Failed to enable OHCI hardware");
3224 pci_set_master(dev
);
3226 host
= hpsb_alloc_host(&ohci1394_driver
, sizeof(struct ti_ohci
), &dev
->dev
);
3227 if (!host
) FAIL(-ENOMEM
, "Failed to allocate host structure");
3229 ohci
= host
->hostdata
;
3232 ohci
->init_state
= OHCI_INIT_ALLOC_HOST
;
3234 pci_set_drvdata(dev
, ohci
);
3236 /* We don't want hardware swapping */
3237 pci_write_config_dword(dev
, OHCI1394_PCI_HCI_Control
, 0);
3239 /* Some oddball Apple controllers do not order the selfid
3240 * properly, so we make up for it here. */
3241 #ifndef __LITTLE_ENDIAN
3242 /* XXX: Need a better way to check this. I'm wondering if we can
3243 * read the values of the OHCI1394_PCI_HCI_Control and the
3244 * noByteSwapData registers to see if they were not cleared to
3245 * zero. Should this work? Obviously it's not defined what these
3246 * registers will read when they aren't supported. Bleh! */
3247 if (dev
->vendor
== PCI_VENDOR_ID_APPLE
&&
3248 dev
->device
== PCI_DEVICE_ID_APPLE_UNI_N_FW
) {
3249 ohci
->no_swap_incoming
= 1;
3250 ohci
->selfid_swap
= 0;
3252 ohci
->selfid_swap
= 1;
3256 #ifndef PCI_DEVICE_ID_NVIDIA_NFORCE2_FW
3257 #define PCI_DEVICE_ID_NVIDIA_NFORCE2_FW 0x006e
3260 /* These chipsets require a bit of extra care when checking after
3262 if ((dev
->vendor
== PCI_VENDOR_ID_APPLE
&&
3263 dev
->device
== PCI_DEVICE_ID_APPLE_UNI_N_FW
) ||
3264 (dev
->vendor
== PCI_VENDOR_ID_NVIDIA
&&
3265 dev
->device
== PCI_DEVICE_ID_NVIDIA_NFORCE2_FW
))
3266 ohci
->check_busreset
= 1;
3268 /* We hardwire the MMIO length, since some CardBus adaptors
3269 * fail to report the right length. Anyway, the ohci spec
3270 * clearly says it's 2kb, so this shouldn't be a problem. */
3271 ohci_base
= pci_resource_start(dev
, 0);
3272 if (pci_resource_len(dev
, 0) < OHCI1394_REGISTER_SIZE
)
3273 PRINT(KERN_WARNING
, "PCI resource length of %lx too small!",
3274 pci_resource_len(dev
, 0));
3276 /* Seems PCMCIA handles this internally. Not sure why. Seems
3277 * pretty bogus to force a driver to special case this. */
3279 if (!request_mem_region (ohci_base
, OHCI1394_REGISTER_SIZE
, OHCI1394_DRIVER_NAME
))
3280 FAIL(-ENOMEM
, "MMIO resource (0x%lx - 0x%lx) unavailable",
3281 ohci_base
, ohci_base
+ OHCI1394_REGISTER_SIZE
);
3283 ohci
->init_state
= OHCI_INIT_HAVE_MEM_REGION
;
3285 ohci
->registers
= ioremap(ohci_base
, OHCI1394_REGISTER_SIZE
);
3286 if (ohci
->registers
== NULL
)
3287 FAIL(-ENXIO
, "Failed to remap registers - card not accessible");
3288 ohci
->init_state
= OHCI_INIT_HAVE_IOMAPPING
;
3289 DBGMSG("Remapped memory spaces reg 0x%p", ohci
->registers
);
3291 /* csr_config rom allocation */
3292 ohci
->csr_config_rom_cpu
=
3293 pci_alloc_consistent(ohci
->dev
, OHCI_CONFIG_ROM_LEN
,
3294 &ohci
->csr_config_rom_bus
);
3295 OHCI_DMA_ALLOC("consistent csr_config_rom");
3296 if (ohci
->csr_config_rom_cpu
== NULL
)
3297 FAIL(-ENOMEM
, "Failed to allocate buffer config rom");
3298 ohci
->init_state
= OHCI_INIT_HAVE_CONFIG_ROM_BUFFER
;
3300 /* self-id dma buffer allocation */
3301 ohci
->selfid_buf_cpu
=
3302 pci_alloc_consistent(ohci
->dev
, OHCI1394_SI_DMA_BUF_SIZE
,
3303 &ohci
->selfid_buf_bus
);
3304 OHCI_DMA_ALLOC("consistent selfid_buf");
3306 if (ohci
->selfid_buf_cpu
== NULL
)
3307 FAIL(-ENOMEM
, "Failed to allocate DMA buffer for self-id packets");
3308 ohci
->init_state
= OHCI_INIT_HAVE_SELFID_BUFFER
;
3310 if ((unsigned long)ohci
->selfid_buf_cpu
& 0x1fff)
3311 PRINT(KERN_INFO
, "SelfID buffer %p is not aligned on "
3312 "8Kb boundary... may cause problems on some CXD3222 chip",
3313 ohci
->selfid_buf_cpu
);
3315 /* No self-id errors at startup */
3316 ohci
->self_id_errors
= 0;
3318 ohci
->init_state
= OHCI_INIT_HAVE_TXRX_BUFFERS__MAYBE
;
3319 /* AR DMA request context allocation */
3320 if (alloc_dma_rcv_ctx(ohci
, &ohci
->ar_req_context
,
3321 DMA_CTX_ASYNC_REQ
, 0, AR_REQ_NUM_DESC
,
3322 AR_REQ_BUF_SIZE
, AR_REQ_SPLIT_BUF_SIZE
,
3323 OHCI1394_AsReqRcvContextBase
) < 0)
3324 FAIL(-ENOMEM
, "Failed to allocate AR Req context");
3326 /* AR DMA response context allocation */
3327 if (alloc_dma_rcv_ctx(ohci
, &ohci
->ar_resp_context
,
3328 DMA_CTX_ASYNC_RESP
, 0, AR_RESP_NUM_DESC
,
3329 AR_RESP_BUF_SIZE
, AR_RESP_SPLIT_BUF_SIZE
,
3330 OHCI1394_AsRspRcvContextBase
) < 0)
3331 FAIL(-ENOMEM
, "Failed to allocate AR Resp context");
3333 /* AT DMA request context */
3334 if (alloc_dma_trm_ctx(ohci
, &ohci
->at_req_context
,
3335 DMA_CTX_ASYNC_REQ
, 0, AT_REQ_NUM_DESC
,
3336 OHCI1394_AsReqTrContextBase
) < 0)
3337 FAIL(-ENOMEM
, "Failed to allocate AT Req context");
3339 /* AT DMA response context */
3340 if (alloc_dma_trm_ctx(ohci
, &ohci
->at_resp_context
,
3341 DMA_CTX_ASYNC_RESP
, 1, AT_RESP_NUM_DESC
,
3342 OHCI1394_AsRspTrContextBase
) < 0)
3343 FAIL(-ENOMEM
, "Failed to allocate AT Resp context");
3345 /* Start off with a soft reset, to clear everything to a sane
3347 ohci_soft_reset(ohci
);
3349 /* Now enable LPS, which we need in order to start accessing
3350 * most of the registers. In fact, on some cards (ALI M5251),
3351 * accessing registers in the SClk domain without LPS enabled
3352 * will lock up the machine. Wait 50msec to make sure we have
3353 * full link enabled. */
3354 reg_write(ohci
, OHCI1394_HCControlSet
, OHCI1394_HCControl_LPS
);
3356 /* Disable and clear interrupts */
3357 reg_write(ohci
, OHCI1394_IntEventClear
, 0xffffffff);
3358 reg_write(ohci
, OHCI1394_IntMaskClear
, 0xffffffff);
3362 /* Determine the number of available IR and IT contexts. */
3363 ohci
->nb_iso_rcv_ctx
=
3364 get_nb_iso_ctx(ohci
, OHCI1394_IsoRecvIntMaskSet
);
3365 ohci
->nb_iso_xmit_ctx
=
3366 get_nb_iso_ctx(ohci
, OHCI1394_IsoXmitIntMaskSet
);
3368 /* Set the usage bits for non-existent contexts so they can't
3370 ohci
->ir_ctx_usage
= ~0 << ohci
->nb_iso_rcv_ctx
;
3371 ohci
->it_ctx_usage
= ~0 << ohci
->nb_iso_xmit_ctx
;
3373 INIT_LIST_HEAD(&ohci
->iso_tasklet_list
);
3374 spin_lock_init(&ohci
->iso_tasklet_list_lock
);
3375 ohci
->ISO_channel_usage
= 0;
3376 spin_lock_init(&ohci
->IR_channel_lock
);
3378 /* Allocate the IR DMA context right here so we don't have
3379 * to do it in interrupt path - note that this doesn't
3380 * waste much memory and avoids the jugglery required to
3381 * allocate it in IRQ path. */
3382 if (alloc_dma_rcv_ctx(ohci
, &ohci
->ir_legacy_context
,
3383 DMA_CTX_ISO
, 0, IR_NUM_DESC
,
3384 IR_BUF_SIZE
, IR_SPLIT_BUF_SIZE
,
3385 OHCI1394_IsoRcvContextBase
) < 0) {
3386 FAIL(-ENOMEM
, "Cannot allocate IR Legacy DMA context");
3389 /* We hopefully don't have to pre-allocate IT DMA like we did
3390 * for IR DMA above. Allocate it on-demand and mark inactive. */
3391 ohci
->it_legacy_context
.ohci
= NULL
;
3392 spin_lock_init(&ohci
->event_lock
);
3395 * interrupts are disabled, all right, but... due to SA_SHIRQ we
3396 * might get called anyway. We'll see no event, of course, but
3397 * we need to get to that "no event", so enough should be initialized
3400 if (request_irq(dev
->irq
, ohci_irq_handler
, SA_SHIRQ
,
3401 OHCI1394_DRIVER_NAME
, ohci
))
3402 FAIL(-ENOMEM
, "Failed to allocate shared interrupt %d", dev
->irq
);
3404 ohci
->init_state
= OHCI_INIT_HAVE_IRQ
;
3405 ohci_initialize(ohci
);
3407 /* Set certain csr values */
3408 host
->csr
.guid_hi
= reg_read(ohci
, OHCI1394_GUIDHi
);
3409 host
->csr
.guid_lo
= reg_read(ohci
, OHCI1394_GUIDLo
);
3410 host
->csr
.cyc_clk_acc
= 100; /* how do we determine clk accuracy? */
3411 host
->csr
.max_rec
= (reg_read(ohci
, OHCI1394_BusOptions
) >> 12) & 0xf;
3412 host
->csr
.lnk_spd
= reg_read(ohci
, OHCI1394_BusOptions
) & 0x7;
3415 host
->low_addr_space
=
3416 (u64
) reg_read(ohci
, OHCI1394_PhyUpperBound
) << 16;
3417 if (!host
->low_addr_space
)
3418 host
->low_addr_space
= OHCI1394_PHYS_UPPER_BOUND_FIXED
;
3420 host
->middle_addr_space
= OHCI1394_MIDDLE_ADDRESS_SPACE
;
3422 /* Tell the highlevel this host is ready */
3423 if (hpsb_add_host(host
))
3424 FAIL(-ENOMEM
, "Failed to register host with highlevel");
3426 ohci
->init_state
= OHCI_INIT_DONE
;
3432 static void ohci1394_pci_remove(struct pci_dev
*pdev
)
3434 struct ti_ohci
*ohci
;
3437 ohci
= pci_get_drvdata(pdev
);
3441 dev
= get_device(&ohci
->host
->device
);
3443 switch (ohci
->init_state
) {
3444 case OHCI_INIT_DONE
:
3445 hpsb_remove_host(ohci
->host
);
3447 /* Clear out BUS Options */
3448 reg_write(ohci
, OHCI1394_ConfigROMhdr
, 0);
3449 reg_write(ohci
, OHCI1394_BusOptions
,
3450 (reg_read(ohci
, OHCI1394_BusOptions
) & 0x0000f007) |
3452 memset(ohci
->csr_config_rom_cpu
, 0, OHCI_CONFIG_ROM_LEN
);
3454 case OHCI_INIT_HAVE_IRQ
:
3455 /* Clear interrupt registers */
3456 reg_write(ohci
, OHCI1394_IntMaskClear
, 0xffffffff);
3457 reg_write(ohci
, OHCI1394_IntEventClear
, 0xffffffff);
3458 reg_write(ohci
, OHCI1394_IsoXmitIntMaskClear
, 0xffffffff);
3459 reg_write(ohci
, OHCI1394_IsoXmitIntEventClear
, 0xffffffff);
3460 reg_write(ohci
, OHCI1394_IsoRecvIntMaskClear
, 0xffffffff);
3461 reg_write(ohci
, OHCI1394_IsoRecvIntEventClear
, 0xffffffff);
3463 /* Disable IRM Contender */
3464 set_phy_reg(ohci
, 4, ~0xc0 & get_phy_reg(ohci
, 4));
3466 /* Clear link control register */
3467 reg_write(ohci
, OHCI1394_LinkControlClear
, 0xffffffff);
3469 /* Let all other nodes know to ignore us */
3470 ohci_devctl(ohci
->host
, RESET_BUS
, LONG_RESET_NO_FORCE_ROOT
);
3472 /* Soft reset before we start - this disables
3473 * interrupts and clears linkEnable and LPS. */
3474 ohci_soft_reset(ohci
);
3475 free_irq(ohci
->dev
->irq
, ohci
);
3477 case OHCI_INIT_HAVE_TXRX_BUFFERS__MAYBE
:
3478 /* The ohci_soft_reset() stops all DMA contexts, so we
3479 * dont need to do this. */
3480 free_dma_rcv_ctx(&ohci
->ar_req_context
);
3481 free_dma_rcv_ctx(&ohci
->ar_resp_context
);
3482 free_dma_trm_ctx(&ohci
->at_req_context
);
3483 free_dma_trm_ctx(&ohci
->at_resp_context
);
3484 free_dma_rcv_ctx(&ohci
->ir_legacy_context
);
3485 free_dma_trm_ctx(&ohci
->it_legacy_context
);
3487 case OHCI_INIT_HAVE_SELFID_BUFFER
:
3488 pci_free_consistent(ohci
->dev
, OHCI1394_SI_DMA_BUF_SIZE
,
3489 ohci
->selfid_buf_cpu
,
3490 ohci
->selfid_buf_bus
);
3491 OHCI_DMA_FREE("consistent selfid_buf");
3493 case OHCI_INIT_HAVE_CONFIG_ROM_BUFFER
:
3494 pci_free_consistent(ohci
->dev
, OHCI_CONFIG_ROM_LEN
,
3495 ohci
->csr_config_rom_cpu
,
3496 ohci
->csr_config_rom_bus
);
3497 OHCI_DMA_FREE("consistent csr_config_rom");
3499 case OHCI_INIT_HAVE_IOMAPPING
:
3500 iounmap(ohci
->registers
);
3502 case OHCI_INIT_HAVE_MEM_REGION
:
3504 release_mem_region(pci_resource_start(ohci
->dev
, 0),
3505 OHCI1394_REGISTER_SIZE
);
3508 #ifdef CONFIG_PPC_PMAC
3509 /* On UniNorth, power down the cable and turn off the chip
3510 * clock when the module is removed to save power on
3511 * laptops. Turning it back ON is done by the arch code when
3512 * pci_enable_device() is called */
3514 struct device_node
* of_node
;
3516 of_node
= pci_device_to_OF_node(ohci
->dev
);
3518 pmac_call_feature(PMAC_FTR_1394_ENABLE
, of_node
, 0, 0);
3519 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER
, of_node
, 0, 0);
3522 #endif /* CONFIG_PPC_PMAC */
3524 case OHCI_INIT_ALLOC_HOST
:
3525 pci_set_drvdata(ohci
->dev
, NULL
);
3533 static int ohci1394_pci_resume (struct pci_dev
*pdev
)
3535 #ifdef CONFIG_PPC_PMAC
3536 if (machine_is(powermac
)) {
3537 struct device_node
*of_node
;
3539 /* Re-enable 1394 */
3540 of_node
= pci_device_to_OF_node (pdev
);
3542 pmac_call_feature (PMAC_FTR_1394_ENABLE
, of_node
, 0, 1);
3544 #endif /* CONFIG_PPC_PMAC */
3546 pci_restore_state(pdev
);
3547 pci_enable_device(pdev
);
3553 static int ohci1394_pci_suspend (struct pci_dev
*pdev
, pm_message_t state
)
3555 #ifdef CONFIG_PPC_PMAC
3556 if (machine_is(powermac
)) {
3557 struct device_node
*of_node
;
3560 of_node
= pci_device_to_OF_node (pdev
);
3562 pmac_call_feature(PMAC_FTR_1394_ENABLE
, of_node
, 0, 0);
3566 pci_save_state(pdev
);
3572 #define PCI_CLASS_FIREWIRE_OHCI ((PCI_CLASS_SERIAL_FIREWIRE << 8) | 0x10)
3574 static struct pci_device_id ohci1394_pci_tbl
[] = {
3576 .class = PCI_CLASS_FIREWIRE_OHCI
,
3577 .class_mask
= PCI_ANY_ID
,
3578 .vendor
= PCI_ANY_ID
,
3579 .device
= PCI_ANY_ID
,
3580 .subvendor
= PCI_ANY_ID
,
3581 .subdevice
= PCI_ANY_ID
,
3586 MODULE_DEVICE_TABLE(pci
, ohci1394_pci_tbl
);
3588 static struct pci_driver ohci1394_pci_driver
= {
3589 .name
= OHCI1394_DRIVER_NAME
,
3590 .id_table
= ohci1394_pci_tbl
,
3591 .probe
= ohci1394_pci_probe
,
3592 .remove
= ohci1394_pci_remove
,
3593 .resume
= ohci1394_pci_resume
,
3594 .suspend
= ohci1394_pci_suspend
,
3597 /***********************************
3598 * OHCI1394 Video Interface *
3599 ***********************************/
3601 /* essentially the only purpose of this code is to allow another
3602 module to hook into ohci's interrupt handler */
3604 int ohci1394_stop_context(struct ti_ohci
*ohci
, int reg
, char *msg
)
3608 /* stop the channel program if it's still running */
3609 reg_write(ohci
, reg
, 0x8000);
3611 /* Wait until it effectively stops */
3612 while (reg_read(ohci
, reg
) & 0x400) {
3616 "Runaway loop while stopping context: %s...", msg
? msg
: "");
3623 if (msg
) PRINT(KERN_ERR
, "%s: dma prg stopped", msg
);
3627 void ohci1394_init_iso_tasklet(struct ohci1394_iso_tasklet
*tasklet
, int type
,
3628 void (*func
)(unsigned long), unsigned long data
)
3630 tasklet_init(&tasklet
->tasklet
, func
, data
);
3631 tasklet
->type
= type
;
3632 /* We init the tasklet->link field, so we can list_del() it
3633 * without worrying whether it was added to the list or not. */
3634 INIT_LIST_HEAD(&tasklet
->link
);
3637 int ohci1394_register_iso_tasklet(struct ti_ohci
*ohci
,
3638 struct ohci1394_iso_tasklet
*tasklet
)
3640 unsigned long flags
, *usage
;
3641 int n
, i
, r
= -EBUSY
;
3643 if (tasklet
->type
== OHCI_ISO_TRANSMIT
) {
3644 n
= ohci
->nb_iso_xmit_ctx
;
3645 usage
= &ohci
->it_ctx_usage
;
3648 n
= ohci
->nb_iso_rcv_ctx
;
3649 usage
= &ohci
->ir_ctx_usage
;
3651 /* only one receive context can be multichannel (OHCI sec 10.4.1) */
3652 if (tasklet
->type
== OHCI_ISO_MULTICHANNEL_RECEIVE
) {
3653 if (test_and_set_bit(0, &ohci
->ir_multichannel_used
)) {
3659 spin_lock_irqsave(&ohci
->iso_tasklet_list_lock
, flags
);
3661 for (i
= 0; i
< n
; i
++)
3662 if (!test_and_set_bit(i
, usage
)) {
3663 tasklet
->context
= i
;
3664 list_add_tail(&tasklet
->link
, &ohci
->iso_tasklet_list
);
3669 spin_unlock_irqrestore(&ohci
->iso_tasklet_list_lock
, flags
);
3674 void ohci1394_unregister_iso_tasklet(struct ti_ohci
*ohci
,
3675 struct ohci1394_iso_tasklet
*tasklet
)
3677 unsigned long flags
;
3679 tasklet_kill(&tasklet
->tasklet
);
3681 spin_lock_irqsave(&ohci
->iso_tasklet_list_lock
, flags
);
3683 if (tasklet
->type
== OHCI_ISO_TRANSMIT
)
3684 clear_bit(tasklet
->context
, &ohci
->it_ctx_usage
);
3686 clear_bit(tasklet
->context
, &ohci
->ir_ctx_usage
);
3688 if (tasklet
->type
== OHCI_ISO_MULTICHANNEL_RECEIVE
) {
3689 clear_bit(0, &ohci
->ir_multichannel_used
);
3693 list_del(&tasklet
->link
);
3695 spin_unlock_irqrestore(&ohci
->iso_tasklet_list_lock
, flags
);
3698 EXPORT_SYMBOL(ohci1394_stop_context
);
3699 EXPORT_SYMBOL(ohci1394_init_iso_tasklet
);
3700 EXPORT_SYMBOL(ohci1394_register_iso_tasklet
);
3701 EXPORT_SYMBOL(ohci1394_unregister_iso_tasklet
);
3703 /***********************************
3704 * General module initialization *
3705 ***********************************/
3707 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
3708 MODULE_DESCRIPTION("Driver for PCI OHCI IEEE-1394 controllers");
3709 MODULE_LICENSE("GPL");
3711 static void __exit
ohci1394_cleanup (void)
3713 pci_unregister_driver(&ohci1394_pci_driver
);
3716 static int __init
ohci1394_init(void)
3718 return pci_register_driver(&ohci1394_pci_driver
);
3721 module_init(ohci1394_init
);
3722 module_exit(ohci1394_cleanup
);