]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/ieee1394/pcilynx.c
ieee1394: pcilynx: superfluous local variables
[mv-sheeva.git] / drivers / ieee1394 / pcilynx.c
1 /*
2  * pcilynx.c - Texas Instruments PCILynx driver
3  * Copyright (C) 1999,2000 Andreas Bombe <andreas.bombe@munich.netsurf.de>,
4  *                         Stephan Linz <linz@mazet.de>
5  *                         Manfred Weihs <weihs@ict.tuwien.ac.at>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 /*
23  * Contributions:
24  *
25  * Manfred Weihs <weihs@ict.tuwien.ac.at>
26  *        reading bus info block (containing GUID) from serial
27  *            eeprom via i2c and storing it in config ROM
28  *        Reworked code for initiating bus resets
29  *            (long, short, with or without hold-off)
30  *        Enhancements in async and iso send code
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/interrupt.h>
36 #include <linux/wait.h>
37 #include <linux/errno.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/fs.h>
43 #include <linux/poll.h>
44 #include <linux/kdev_t.h>
45 #include <linux/dma-mapping.h>
46 #include <asm/byteorder.h>
47 #include <asm/atomic.h>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
50 #include <asm/irq.h>
51
52 #include "csr1212.h"
53 #include "ieee1394.h"
54 #include "ieee1394_types.h"
55 #include "hosts.h"
56 #include "ieee1394_core.h"
57 #include "highlevel.h"
58 #include "pcilynx.h"
59
60 #include <linux/i2c.h>
61 #include <linux/i2c-algo-bit.h>
62
63 /* print general (card independent) information */
64 #define PRINT_G(level, fmt, args...) printk(level "pcilynx: " fmt "\n" , ## args)
65 /* print card specific information */
66 #define PRINT(level, card, fmt, args...) printk(level "pcilynx%d: " fmt "\n" , card , ## args)
67
68 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
69 #define PRINT_GD(level, fmt, args...) printk(level "pcilynx: " fmt "\n" , ## args)
70 #define PRINTD(level, card, fmt, args...) printk(level "pcilynx%d: " fmt "\n" , card , ## args)
71 #else
72 #define PRINT_GD(level, fmt, args...) do {} while (0)
73 #define PRINTD(level, card, fmt, args...) do {} while (0)
74 #endif
75
76
77 /* Module Parameters */
78 static int skip_eeprom;
79 module_param(skip_eeprom, int, 0444);
80 MODULE_PARM_DESC(skip_eeprom, "Use generic bus info block instead of serial eeprom (default = 0).");
81
82
83 static struct hpsb_host_driver lynx_driver;
84 static unsigned int card_id;
85
86
87
88 /*
89  * I2C stuff
90  */
91
92 /* the i2c stuff was inspired by i2c-philips-par.c */
93
94 static void bit_setscl(void *data, int state)
95 {
96         if (state) {
97                   ((struct ti_lynx *) data)->i2c_driven_state |= 0x00000040;
98         } else {
99                   ((struct ti_lynx *) data)->i2c_driven_state &= ~0x00000040;
100         }
101         reg_write((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL, ((struct ti_lynx *) data)->i2c_driven_state);
102 }
103
104 static void bit_setsda(void *data, int state)
105 {
106         if (state) {
107                   ((struct ti_lynx *) data)->i2c_driven_state |= 0x00000010;
108         } else {
109                   ((struct ti_lynx *) data)->i2c_driven_state &= ~0x00000010;
110         }
111         reg_write((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL, ((struct ti_lynx *) data)->i2c_driven_state);
112 }
113
114 static int bit_getscl(void *data)
115 {
116         return reg_read((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL) & 0x00000040;
117 }
118
119 static int bit_getsda(void *data)
120 {
121         return reg_read((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL) & 0x00000010;
122 }
123
124 static int bit_reg(struct i2c_client *client)
125 {
126         return 0;
127 }
128
129 static int bit_unreg(struct i2c_client *client)
130 {
131         return 0;
132 }
133
134 static struct i2c_algo_bit_data bit_data = {
135         .setsda                 = bit_setsda,
136         .setscl                 = bit_setscl,
137         .getsda                 = bit_getsda,
138         .getscl                 = bit_getscl,
139         .udelay                 = 5,
140         .timeout                = 100,
141 };
142
143 static struct i2c_adapter bit_ops = {
144         .id                     = 0xAA, //FIXME: probably we should get an id in i2c-id.h
145         .client_register        = bit_reg,
146         .client_unregister      = bit_unreg,
147         .name                   = "PCILynx I2C",
148 };
149
150
151
152 /*
153  * PCL handling functions.
154  */
155
156 static pcl_t alloc_pcl(struct ti_lynx *lynx)
157 {
158         u8 m;
159         int i, j;
160
161         spin_lock(&lynx->lock);
162         /* FIXME - use ffz() to make this readable */
163         for (i = 0; i < (LOCALRAM_SIZE / 1024); i++) {
164                 m = lynx->pcl_bmap[i];
165                 for (j = 0; j < 8; j++) {
166                         if (m & 1<<j) {
167                                 continue;
168                         }
169                         m |= 1<<j;
170                         lynx->pcl_bmap[i] = m;
171                         spin_unlock(&lynx->lock);
172                         return 8 * i + j;
173                 }
174         }
175         spin_unlock(&lynx->lock);
176
177         return -1;
178 }
179
180
181 #if 0
182 static void free_pcl(struct ti_lynx *lynx, pcl_t pclid)
183 {
184         int off, bit;
185
186         off = pclid / 8;
187         bit = pclid % 8;
188
189         if (pclid < 0) {
190                 return;
191         }
192
193         spin_lock(&lynx->lock);
194         if (lynx->pcl_bmap[off] & 1<<bit) {
195                 lynx->pcl_bmap[off] &= ~(1<<bit);
196         } else {
197                 PRINT(KERN_ERR, lynx->id,
198                       "attempted to free unallocated PCL %d", pclid);
199         }
200         spin_unlock(&lynx->lock);
201 }
202
203 /* functions useful for debugging */
204 static void pretty_print_pcl(const struct ti_pcl *pcl)
205 {
206         int i;
207
208         printk("PCL next %08x, userdata %08x, status %08x, remtrans %08x, nextbuf %08x\n",
209                pcl->next, pcl->user_data, pcl->pcl_status,
210                pcl->remaining_transfer_count, pcl->next_data_buffer);
211
212         printk("PCL");
213         for (i=0; i<13; i++) {
214                 printk(" c%x:%08x d%x:%08x",
215                        i, pcl->buffer[i].control, i, pcl->buffer[i].pointer);
216                 if (!(i & 0x3) && (i != 12)) printk("\nPCL");
217         }
218         printk("\n");
219 }
220
221 static void print_pcl(const struct ti_lynx *lynx, pcl_t pclid)
222 {
223         struct ti_pcl pcl;
224
225         get_pcl(lynx, pclid, &pcl);
226         pretty_print_pcl(&pcl);
227 }
228 #endif
229
230
231
232 /***********************************
233  * IEEE-1394 functionality section *
234  ***********************************/
235
236
237 static int get_phy_reg(struct ti_lynx *lynx, int addr)
238 {
239         int retval;
240         int i = 0;
241
242         unsigned long flags;
243
244         if (addr > 15) {
245                 PRINT(KERN_ERR, lynx->id,
246                       "%s: PHY register address %d out of range",
247                       __FUNCTION__, addr);
248                 return -1;
249         }
250
251         spin_lock_irqsave(&lynx->phy_reg_lock, flags);
252
253         reg_write(lynx, LINK_PHY, LINK_PHY_READ | LINK_PHY_ADDR(addr));
254         do {
255                 retval = reg_read(lynx, LINK_PHY);
256
257                 if (i > 10000) {
258                         PRINT(KERN_ERR, lynx->id, "%s: runaway loop, aborting",
259                               __FUNCTION__);
260                         retval = -1;
261                         break;
262                 }
263                 i++;
264         } while ((retval & 0xf00) != LINK_PHY_RADDR(addr));
265
266         reg_write(lynx, LINK_INT_STATUS, LINK_INT_PHY_REG_RCVD);
267         spin_unlock_irqrestore(&lynx->phy_reg_lock, flags);
268
269         if (retval != -1) {
270                 return retval & 0xff;
271         } else {
272                 return -1;
273         }
274 }
275
276 static int set_phy_reg(struct ti_lynx *lynx, int addr, int val)
277 {
278         unsigned long flags;
279
280         if (addr > 15) {
281                 PRINT(KERN_ERR, lynx->id,
282                       "%s: PHY register address %d out of range", __FUNCTION__, addr);
283                 return -1;
284         }
285
286         if (val > 0xff) {
287                 PRINT(KERN_ERR, lynx->id,
288                       "%s: PHY register value %d out of range", __FUNCTION__, val);
289                 return -1;
290         }
291
292         spin_lock_irqsave(&lynx->phy_reg_lock, flags);
293
294         reg_write(lynx, LINK_PHY, LINK_PHY_WRITE | LINK_PHY_ADDR(addr)
295                   | LINK_PHY_WDATA(val));
296
297         spin_unlock_irqrestore(&lynx->phy_reg_lock, flags);
298
299         return 0;
300 }
301
302 static int sel_phy_reg_page(struct ti_lynx *lynx, int page)
303 {
304         int reg;
305
306         if (page > 7) {
307                 PRINT(KERN_ERR, lynx->id,
308                       "%s: PHY page %d out of range", __FUNCTION__, page);
309                 return -1;
310         }
311
312         reg = get_phy_reg(lynx, 7);
313         if (reg != -1) {
314                 reg &= 0x1f;
315                 reg |= (page << 5);
316                 set_phy_reg(lynx, 7, reg);
317                 return 0;
318         } else {
319                 return -1;
320         }
321 }
322
323 #if 0 /* not needed at this time */
324 static int sel_phy_reg_port(struct ti_lynx *lynx, int port)
325 {
326         int reg;
327
328         if (port > 15) {
329                 PRINT(KERN_ERR, lynx->id,
330                       "%s: PHY port %d out of range", __FUNCTION__, port);
331                 return -1;
332         }
333
334         reg = get_phy_reg(lynx, 7);
335         if (reg != -1) {
336                 reg &= 0xf0;
337                 reg |= port;
338                 set_phy_reg(lynx, 7, reg);
339                 return 0;
340         } else {
341                 return -1;
342         }
343 }
344 #endif
345
346 static u32 get_phy_vendorid(struct ti_lynx *lynx)
347 {
348         u32 pvid = 0;
349         sel_phy_reg_page(lynx, 1);
350         pvid |= (get_phy_reg(lynx, 10) << 16);
351         pvid |= (get_phy_reg(lynx, 11) << 8);
352         pvid |= get_phy_reg(lynx, 12);
353         PRINT(KERN_INFO, lynx->id, "PHY vendor id 0x%06x", pvid);
354         return pvid;
355 }
356
357 static u32 get_phy_productid(struct ti_lynx *lynx)
358 {
359         u32 id = 0;
360         sel_phy_reg_page(lynx, 1);
361         id |= (get_phy_reg(lynx, 13) << 16);
362         id |= (get_phy_reg(lynx, 14) << 8);
363         id |= get_phy_reg(lynx, 15);
364         PRINT(KERN_INFO, lynx->id, "PHY product id 0x%06x", id);
365         return id;
366 }
367
368 static quadlet_t generate_own_selfid(struct ti_lynx *lynx,
369                                      struct hpsb_host *host)
370 {
371         quadlet_t lsid;
372         char phyreg[7];
373         int i;
374
375         phyreg[0] = lynx->phy_reg0;
376         for (i = 1; i < 7; i++) {
377                 phyreg[i] = get_phy_reg(lynx, i);
378         }
379
380         /* FIXME? We assume a TSB21LV03A phy here.  This code doesn't support
381            more than 3 ports on the PHY anyway. */
382
383         lsid = 0x80400000 | ((phyreg[0] & 0xfc) << 22);
384         lsid |= (phyreg[1] & 0x3f) << 16; /* gap count */
385         lsid |= (phyreg[2] & 0xc0) << 8; /* max speed */
386         if (!hpsb_disable_irm)
387                 lsid |= (phyreg[6] & 0x01) << 11; /* contender (phy dependent) */
388         /* lsid |= 1 << 11; *//* set contender (hack) */
389         lsid |= (phyreg[6] & 0x10) >> 3; /* initiated reset */
390
391         for (i = 0; i < (phyreg[2] & 0xf); i++) { /* ports */
392                 if (phyreg[3 + i] & 0x4) {
393                         lsid |= (((phyreg[3 + i] & 0x8) | 0x10) >> 3)
394                                 << (6 - i*2);
395                 } else {
396                         lsid |= 1 << (6 - i*2);
397                 }
398         }
399
400         cpu_to_be32s(&lsid);
401         PRINT(KERN_DEBUG, lynx->id, "generated own selfid 0x%x", lsid);
402         return lsid;
403 }
404
405 static void handle_selfid(struct ti_lynx *lynx, struct hpsb_host *host)
406 {
407         quadlet_t *q = lynx->rcv_page;
408         int phyid, isroot, size;
409         quadlet_t lsid = 0;
410         int i;
411
412         if (lynx->phy_reg0 == -1 || lynx->selfid_size == -1) return;
413
414         size = lynx->selfid_size;
415         phyid = lynx->phy_reg0;
416
417         i = (size > 16 ? 16 : size) / 4 - 1;
418         while (i >= 0) {
419                 cpu_to_be32s(&q[i]);
420                 i--;
421         }
422
423         if (!lynx->phyic.reg_1394a) {
424                 lsid = generate_own_selfid(lynx, host);
425         }
426
427         isroot = (phyid & 2) != 0;
428         phyid >>= 2;
429         PRINT(KERN_INFO, lynx->id, "SelfID process finished (phyid %d, %s)",
430               phyid, (isroot ? "root" : "not root"));
431         reg_write(lynx, LINK_ID, (0xffc0 | phyid) << 16);
432
433         if (!lynx->phyic.reg_1394a && !size) {
434                 hpsb_selfid_received(host, lsid);
435         }
436
437         while (size > 0) {
438                 struct selfid *sid = (struct selfid *)q;
439
440                 if (!lynx->phyic.reg_1394a && !sid->extended
441                     && (sid->phy_id == (phyid + 1))) {
442                         hpsb_selfid_received(host, lsid);
443                 }
444
445                 if (q[0] == ~q[1]) {
446                         PRINT(KERN_DEBUG, lynx->id, "SelfID packet 0x%x rcvd",
447                               q[0]);
448                         hpsb_selfid_received(host, q[0]);
449                 } else {
450                         PRINT(KERN_INFO, lynx->id,
451                               "inconsistent selfid 0x%x/0x%x", q[0], q[1]);
452                 }
453                 q += 2;
454                 size -= 8;
455         }
456
457         if (!lynx->phyic.reg_1394a && isroot && phyid != 0) {
458                 hpsb_selfid_received(host, lsid);
459         }
460
461         hpsb_selfid_complete(host, phyid, isroot);
462
463         if (host->in_bus_reset) return; /* in bus reset again */
464
465         if (isroot) reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_CYCMASTER); //FIXME: I do not think, we need this here
466         reg_set_bits(lynx, LINK_CONTROL,
467                      LINK_CONTROL_RCV_CMP_VALID | LINK_CONTROL_TX_ASYNC_EN
468                      | LINK_CONTROL_RX_ASYNC_EN | LINK_CONTROL_CYCTIMEREN);
469 }
470
471
472
473 /* This must be called with the respective queue_lock held. */
474 static void send_next(struct ti_lynx *lynx, int what)
475 {
476         struct ti_pcl pcl;
477         struct lynx_send_data *d;
478         struct hpsb_packet *packet;
479
480 #if 0 /* has been removed from ieee1394 core */
481         d = (what == hpsb_iso ? &lynx->iso_send : &lynx->async);
482 #else
483         d = &lynx->async;
484 #endif
485         if (!list_empty(&d->pcl_queue)) {
486                 PRINT(KERN_ERR, lynx->id, "trying to queue a new packet in nonempty fifo");
487                 BUG();
488         }
489
490         packet = driver_packet(d->queue.next);
491         list_move_tail(&packet->driver_list, &d->pcl_queue);
492
493         d->header_dma = pci_map_single(lynx->dev, packet->header,
494                                        packet->header_size, PCI_DMA_TODEVICE);
495         if (packet->data_size) {
496                 d->data_dma = pci_map_single(lynx->dev, packet->data,
497                                              packet->data_size,
498                                              PCI_DMA_TODEVICE);
499         } else {
500                 d->data_dma = 0;
501         }
502
503         pcl.next = PCL_NEXT_INVALID;
504         pcl.async_error_next = PCL_NEXT_INVALID;
505         pcl.pcl_status = 0;
506         pcl.buffer[0].control = packet->speed_code << 14 | packet->header_size;
507 #ifndef __BIG_ENDIAN
508         pcl.buffer[0].control |= PCL_BIGENDIAN;
509 #endif
510         pcl.buffer[0].pointer = d->header_dma;
511         pcl.buffer[1].control = PCL_LAST_BUFF | packet->data_size;
512         pcl.buffer[1].pointer = d->data_dma;
513
514         switch (packet->type) {
515         case hpsb_async:
516                 pcl.buffer[0].control |= PCL_CMD_XMT;
517                 break;
518 #if 0 /* has been removed from ieee1394 core */
519         case hpsb_iso:
520                 pcl.buffer[0].control |= PCL_CMD_XMT | PCL_ISOMODE;
521                 break;
522 #endif
523         case hpsb_raw:
524                 pcl.buffer[0].control |= PCL_CMD_UNFXMT;
525                 break;
526         }
527
528         put_pcl(lynx, d->pcl, &pcl);
529         run_pcl(lynx, d->pcl_start, d->channel);
530 }
531
532
533 /* called from subsystem core */
534 static int lynx_transmit(struct hpsb_host *host, struct hpsb_packet *packet)
535 {
536         struct ti_lynx *lynx = host->hostdata;
537         struct lynx_send_data *d;
538         unsigned long flags;
539
540         if (packet->data_size >= 4096) {
541                 PRINT(KERN_ERR, lynx->id, "transmit packet data too big (%Zd)",
542                       packet->data_size);
543                 return -EOVERFLOW;
544         }
545
546         switch (packet->type) {
547         case hpsb_async:
548         case hpsb_raw:
549                 d = &lynx->async;
550                 break;
551 #if 0 /* has been removed from ieee1394 core */
552         case hpsb_iso:
553                 d = &lynx->iso_send;
554                 break;
555 #endif
556         default:
557                 PRINT(KERN_ERR, lynx->id, "invalid packet type %d",
558                       packet->type);
559                 return -EINVAL;
560         }
561
562         if (packet->tcode == TCODE_WRITEQ
563             || packet->tcode == TCODE_READQ_RESPONSE) {
564                 cpu_to_be32s(&packet->header[3]);
565         }
566
567         spin_lock_irqsave(&d->queue_lock, flags);
568
569         list_add_tail(&packet->driver_list, &d->queue);
570         if (list_empty(&d->pcl_queue))
571                 send_next(lynx, packet->type);
572
573         spin_unlock_irqrestore(&d->queue_lock, flags);
574
575         return 0;
576 }
577
578
579 /* called from subsystem core */
580 static int lynx_devctl(struct hpsb_host *host, enum devctl_cmd cmd, int arg)
581 {
582         struct ti_lynx *lynx = host->hostdata;
583         int retval = 0;
584         struct hpsb_packet *packet;
585         LIST_HEAD(packet_list);
586         unsigned long flags;
587         int phy_reg;
588
589         switch (cmd) {
590         case RESET_BUS:
591                 if (reg_read(lynx, LINK_INT_STATUS) & LINK_INT_PHY_BUSRESET) {
592                         retval = 0;
593                         break;
594                 }
595
596                 switch (arg) {
597                 case SHORT_RESET:
598                         if (lynx->phyic.reg_1394a) {
599                                 phy_reg = get_phy_reg(lynx, 5);
600                                 if (phy_reg == -1) {
601                                         PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
602                                         retval = -1;
603                                         break;
604                                 }
605                                 phy_reg |= 0x40;
606
607                                 PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset) on request");
608
609                                 lynx->selfid_size = -1;
610                                 lynx->phy_reg0 = -1;
611                                 set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
612                                 break;
613                         } else {
614                                 PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
615                                 /* fall through to long bus reset */
616                         }
617                 case LONG_RESET:
618                         phy_reg = get_phy_reg(lynx, 1);
619                         if (phy_reg == -1) {
620                                 PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
621                                 retval = -1;
622                                 break;
623                         }
624                         phy_reg |= 0x40;
625
626                         PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset) on request");
627
628                         lynx->selfid_size = -1;
629                         lynx->phy_reg0 = -1;
630                         set_phy_reg(lynx, 1, phy_reg); /* clear RHB, set IBR */
631                         break;
632                 case SHORT_RESET_NO_FORCE_ROOT:
633                         if (lynx->phyic.reg_1394a) {
634                                 phy_reg = get_phy_reg(lynx, 1);
635                                 if (phy_reg == -1) {
636                                         PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
637                                         retval = -1;
638                                         break;
639                                 }
640                                 if (phy_reg & 0x80) {
641                                         phy_reg &= ~0x80;
642                                         set_phy_reg(lynx, 1, phy_reg); /* clear RHB */
643                                 }
644
645                                 phy_reg = get_phy_reg(lynx, 5);
646                                 if (phy_reg == -1) {
647                                         PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
648                                         retval = -1;
649                                         break;
650                                 }
651                                 phy_reg |= 0x40;
652
653                                 PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset, no force_root) on request");
654
655                                 lynx->selfid_size = -1;
656                                 lynx->phy_reg0 = -1;
657                                 set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
658                                 break;
659                         } else {
660                                 PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
661                                 /* fall through to long bus reset */
662                         }
663                 case LONG_RESET_NO_FORCE_ROOT:
664                         phy_reg = get_phy_reg(lynx, 1);
665                         if (phy_reg == -1) {
666                                 PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
667                                 retval = -1;
668                                 break;
669                         }
670                         phy_reg &= ~0x80;
671                         phy_reg |= 0x40;
672
673                         PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset, no force_root) on request");
674
675                         lynx->selfid_size = -1;
676                         lynx->phy_reg0 = -1;
677                         set_phy_reg(lynx, 1, phy_reg); /* clear RHB, set IBR */
678                         break;
679                 case SHORT_RESET_FORCE_ROOT:
680                         if (lynx->phyic.reg_1394a) {
681                                 phy_reg = get_phy_reg(lynx, 1);
682                                 if (phy_reg == -1) {
683                                         PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
684                                         retval = -1;
685                                         break;
686                                 }
687                                 if (!(phy_reg & 0x80)) {
688                                         phy_reg |= 0x80;
689                                         set_phy_reg(lynx, 1, phy_reg); /* set RHB */
690                                 }
691
692                                 phy_reg = get_phy_reg(lynx, 5);
693                                 if (phy_reg == -1) {
694                                         PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
695                                         retval = -1;
696                                         break;
697                                 }
698                                 phy_reg |= 0x40;
699
700                                 PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset, force_root set) on request");
701
702                                 lynx->selfid_size = -1;
703                                 lynx->phy_reg0 = -1;
704                                 set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
705                                 break;
706                         } else {
707                                 PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
708                                 /* fall through to long bus reset */
709                         }
710                 case LONG_RESET_FORCE_ROOT:
711                         phy_reg = get_phy_reg(lynx, 1);
712                         if (phy_reg == -1) {
713                                 PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
714                                 retval = -1;
715                                 break;
716                         }
717                         phy_reg |= 0xc0;
718
719                         PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset, force_root set) on request");
720
721                         lynx->selfid_size = -1;
722                         lynx->phy_reg0 = -1;
723                         set_phy_reg(lynx, 1, phy_reg); /* set IBR and RHB */
724                         break;
725                 default:
726                         PRINT(KERN_ERR, lynx->id, "unknown argument for reset_bus command %d", arg);
727                         retval = -1;
728                 }
729
730                 break;
731
732         case GET_CYCLE_COUNTER:
733                 retval = reg_read(lynx, CYCLE_TIMER);
734                 break;
735
736         case SET_CYCLE_COUNTER:
737                 reg_write(lynx, CYCLE_TIMER, arg);
738                 break;
739
740         case SET_BUS_ID:
741                 reg_write(lynx, LINK_ID,
742                           (arg << 22) | (reg_read(lynx, LINK_ID) & 0x003f0000));
743                 break;
744
745         case ACT_CYCLE_MASTER:
746                 if (arg) {
747                         reg_set_bits(lynx, LINK_CONTROL,
748                                      LINK_CONTROL_CYCMASTER);
749                 } else {
750                         reg_clear_bits(lynx, LINK_CONTROL,
751                                        LINK_CONTROL_CYCMASTER);
752                 }
753                 break;
754
755         case CANCEL_REQUESTS:
756                 spin_lock_irqsave(&lynx->async.queue_lock, flags);
757
758                 reg_write(lynx, DMA_CHAN_CTRL(CHANNEL_ASYNC_SEND), 0);
759                 list_splice(&lynx->async.queue, &packet_list);
760                 INIT_LIST_HEAD(&lynx->async.queue);
761
762                 if (list_empty(&lynx->async.pcl_queue)) {
763                         spin_unlock_irqrestore(&lynx->async.queue_lock, flags);
764                         PRINTD(KERN_DEBUG, lynx->id, "no async packet in PCL to cancel");
765                 } else {
766                         struct ti_pcl pcl;
767                         u32 ack;
768
769                         PRINT(KERN_INFO, lynx->id, "cancelling async packet, that was already in PCL");
770
771                         get_pcl(lynx, lynx->async.pcl, &pcl);
772
773                         packet = driver_packet(lynx->async.pcl_queue.next);
774                         list_del_init(&packet->driver_list);
775
776                         pci_unmap_single(lynx->dev, lynx->async.header_dma,
777                                          packet->header_size, PCI_DMA_TODEVICE);
778                         if (packet->data_size) {
779                                 pci_unmap_single(lynx->dev, lynx->async.data_dma,
780                                                  packet->data_size, PCI_DMA_TODEVICE);
781                         }
782
783                         spin_unlock_irqrestore(&lynx->async.queue_lock, flags);
784
785                         if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
786                                 if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
787                                         ack = (pcl.pcl_status >> 15) & 0xf;
788                                         PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
789                                         ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
790                                 } else {
791                                         ack = (pcl.pcl_status >> 15) & 0xf;
792                                 }
793                         } else {
794                                 PRINT(KERN_INFO, lynx->id, "async packet was not completed");
795                                 ack = ACKX_ABORTED;
796                         }
797                         hpsb_packet_sent(host, packet, ack);
798                 }
799
800                 while (!list_empty(&packet_list)) {
801                         packet = driver_packet(packet_list.next);
802                         list_del_init(&packet->driver_list);
803                         hpsb_packet_sent(host, packet, ACKX_ABORTED);
804                 }
805
806                 break;
807 #if 0 /* has been removed from ieee1394 core */
808         case ISO_LISTEN_CHANNEL:
809                 spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
810
811                 if (lynx->iso_rcv.chan_count++ == 0) {
812                         reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
813                                   DMA_WORD1_CMP_ENABLE_MASTER);
814                 }
815
816                 spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
817                 break;
818
819         case ISO_UNLISTEN_CHANNEL:
820                 spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
821
822                 if (--lynx->iso_rcv.chan_count == 0) {
823                         reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
824                                   0);
825                 }
826
827                 spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
828                 break;
829 #endif
830         default:
831                 PRINT(KERN_ERR, lynx->id, "unknown devctl command %d", cmd);
832                 retval = -1;
833         }
834
835         return retval;
836 }
837
838
839 /***************************************
840  * IEEE-1394 functionality section END *
841  ***************************************/
842
843
844 /********************************************************
845  * Global stuff (interrupt handler, init/shutdown code) *
846  ********************************************************/
847
848
849 static irqreturn_t lynx_irq_handler(int irq, void *dev_id)
850 {
851         struct ti_lynx *lynx = (struct ti_lynx *)dev_id;
852         struct hpsb_host *host = lynx->host;
853         u32 intmask;
854         u32 linkint;
855
856         linkint = reg_read(lynx, LINK_INT_STATUS);
857         intmask = reg_read(lynx, PCI_INT_STATUS);
858
859         if (!(intmask & PCI_INT_INT_PEND))
860                 return IRQ_NONE;
861
862         PRINTD(KERN_DEBUG, lynx->id, "interrupt: 0x%08x / 0x%08x", intmask,
863                linkint);
864
865         reg_write(lynx, LINK_INT_STATUS, linkint);
866         reg_write(lynx, PCI_INT_STATUS, intmask);
867
868         if (intmask & PCI_INT_1394) {
869                 if (linkint & LINK_INT_PHY_TIMEOUT) {
870                         PRINT(KERN_INFO, lynx->id, "PHY timeout occurred");
871                 }
872                 if (linkint & LINK_INT_PHY_BUSRESET) {
873                         PRINT(KERN_INFO, lynx->id, "bus reset interrupt");
874                         lynx->selfid_size = -1;
875                         lynx->phy_reg0 = -1;
876                         if (!host->in_bus_reset)
877                                 hpsb_bus_reset(host);
878                 }
879                 if (linkint & LINK_INT_PHY_REG_RCVD) {
880                         u32 reg;
881
882                         spin_lock(&lynx->phy_reg_lock);
883                         reg = reg_read(lynx, LINK_PHY);
884                         spin_unlock(&lynx->phy_reg_lock);
885
886                         if (!host->in_bus_reset) {
887                                 PRINT(KERN_INFO, lynx->id,
888                                       "phy reg received without reset");
889                         } else if (reg & 0xf00) {
890                                 PRINT(KERN_INFO, lynx->id,
891                                       "unsolicited phy reg %d received",
892                                       (reg >> 8) & 0xf);
893                         } else {
894                                 lynx->phy_reg0 = reg & 0xff;
895                                 handle_selfid(lynx, host);
896                         }
897                 }
898                 if (linkint & LINK_INT_ISO_STUCK) {
899                         PRINT(KERN_INFO, lynx->id, "isochronous transmitter stuck");
900                 }
901                 if (linkint & LINK_INT_ASYNC_STUCK) {
902                         PRINT(KERN_INFO, lynx->id, "asynchronous transmitter stuck");
903                 }
904                 if (linkint & LINK_INT_SENT_REJECT) {
905                         PRINT(KERN_INFO, lynx->id, "sent reject");
906                 }
907                 if (linkint & LINK_INT_TX_INVALID_TC) {
908                         PRINT(KERN_INFO, lynx->id, "invalid transaction code");
909                 }
910                 if (linkint & LINK_INT_GRF_OVERFLOW) {
911                         /* flush FIFO if overflow happens during reset */
912                         if (host->in_bus_reset)
913                                 reg_write(lynx, FIFO_CONTROL,
914                                           FIFO_CONTROL_GRF_FLUSH);
915                         PRINT(KERN_INFO, lynx->id, "GRF overflow");
916                 }
917                 if (linkint & LINK_INT_ITF_UNDERFLOW) {
918                         PRINT(KERN_INFO, lynx->id, "ITF underflow");
919                 }
920                 if (linkint & LINK_INT_ATF_UNDERFLOW) {
921                         PRINT(KERN_INFO, lynx->id, "ATF underflow");
922                 }
923         }
924
925         if (intmask & PCI_INT_DMA_HLT(CHANNEL_ISO_RCV)) {
926                 PRINTD(KERN_DEBUG, lynx->id, "iso receive");
927
928                 spin_lock(&lynx->iso_rcv.lock);
929
930                 lynx->iso_rcv.stat[lynx->iso_rcv.next] =
931                         reg_read(lynx, DMA_CHAN_STAT(CHANNEL_ISO_RCV));
932
933                 lynx->iso_rcv.used++;
934                 lynx->iso_rcv.next = (lynx->iso_rcv.next + 1) % NUM_ISORCV_PCL;
935
936                 if ((lynx->iso_rcv.next == lynx->iso_rcv.last)
937                     || !lynx->iso_rcv.chan_count) {
938                         PRINTD(KERN_DEBUG, lynx->id, "stopped");
939                         reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV), 0);
940                 }
941
942                 run_sub_pcl(lynx, lynx->iso_rcv.pcl_start, lynx->iso_rcv.next,
943                             CHANNEL_ISO_RCV);
944
945                 spin_unlock(&lynx->iso_rcv.lock);
946
947                 tasklet_schedule(&lynx->iso_rcv.tq);
948         }
949
950         if (intmask & PCI_INT_DMA_HLT(CHANNEL_ASYNC_SEND)) {
951                 PRINTD(KERN_DEBUG, lynx->id, "async sent");
952                 spin_lock(&lynx->async.queue_lock);
953
954                 if (list_empty(&lynx->async.pcl_queue)) {
955                         spin_unlock(&lynx->async.queue_lock);
956                         PRINT(KERN_WARNING, lynx->id, "async dma halted, but no queued packet (maybe it was cancelled)");
957                 } else {
958                         struct ti_pcl pcl;
959                         u32 ack;
960                         struct hpsb_packet *packet;
961
962                         get_pcl(lynx, lynx->async.pcl, &pcl);
963
964                         packet = driver_packet(lynx->async.pcl_queue.next);
965                         list_del_init(&packet->driver_list);
966
967                         pci_unmap_single(lynx->dev, lynx->async.header_dma,
968                                          packet->header_size, PCI_DMA_TODEVICE);
969                         if (packet->data_size) {
970                                 pci_unmap_single(lynx->dev, lynx->async.data_dma,
971                                                  packet->data_size, PCI_DMA_TODEVICE);
972                         }
973
974                         if (!list_empty(&lynx->async.queue)) {
975                                 send_next(lynx, hpsb_async);
976                         }
977
978                         spin_unlock(&lynx->async.queue_lock);
979
980                         if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
981                                 if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
982                                         ack = (pcl.pcl_status >> 15) & 0xf;
983                                         PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
984                                         ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
985                                 } else {
986                                         ack = (pcl.pcl_status >> 15) & 0xf;
987                                 }
988                         } else {
989                                 PRINT(KERN_INFO, lynx->id, "async packet was not completed");
990                                 ack = ACKX_SEND_ERROR;
991                         }
992                         hpsb_packet_sent(host, packet, ack);
993                 }
994         }
995
996         if (intmask & PCI_INT_DMA_HLT(CHANNEL_ISO_SEND)) {
997                 PRINTD(KERN_DEBUG, lynx->id, "iso sent");
998                 spin_lock(&lynx->iso_send.queue_lock);
999
1000                 if (list_empty(&lynx->iso_send.pcl_queue)) {
1001                         spin_unlock(&lynx->iso_send.queue_lock);
1002                         PRINT(KERN_ERR, lynx->id, "iso send dma halted, but no queued packet");
1003                 } else {
1004                         struct ti_pcl pcl;
1005                         u32 ack;
1006                         struct hpsb_packet *packet;
1007
1008                         get_pcl(lynx, lynx->iso_send.pcl, &pcl);
1009
1010                         packet = driver_packet(lynx->iso_send.pcl_queue.next);
1011                         list_del_init(&packet->driver_list);
1012
1013                         pci_unmap_single(lynx->dev, lynx->iso_send.header_dma,
1014                                          packet->header_size, PCI_DMA_TODEVICE);
1015                         if (packet->data_size) {
1016                                 pci_unmap_single(lynx->dev, lynx->iso_send.data_dma,
1017                                                  packet->data_size, PCI_DMA_TODEVICE);
1018                         }
1019 #if 0 /* has been removed from ieee1394 core */
1020                         if (!list_empty(&lynx->iso_send.queue)) {
1021                                 send_next(lynx, hpsb_iso);
1022                         }
1023 #endif
1024                         spin_unlock(&lynx->iso_send.queue_lock);
1025
1026                         if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
1027                                 if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
1028                                         ack = (pcl.pcl_status >> 15) & 0xf;
1029                                         PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
1030                                         ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
1031                                 } else {
1032                                         ack = (pcl.pcl_status >> 15) & 0xf;
1033                                 }
1034                         } else {
1035                                 PRINT(KERN_INFO, lynx->id, "iso send packet was not completed");
1036                                 ack = ACKX_SEND_ERROR;
1037                         }
1038
1039                         hpsb_packet_sent(host, packet, ack); //FIXME: maybe we should just use ACK_COMPLETE and ACKX_SEND_ERROR
1040                 }
1041         }
1042
1043         if (intmask & PCI_INT_DMA_HLT(CHANNEL_ASYNC_RCV)) {
1044                 /* general receive DMA completed */
1045                 int stat = reg_read(lynx, DMA_CHAN_STAT(CHANNEL_ASYNC_RCV));
1046
1047                 PRINTD(KERN_DEBUG, lynx->id, "received packet size %d",
1048                        stat & 0x1fff);
1049
1050                 if (stat & DMA_CHAN_STAT_SELFID) {
1051                         lynx->selfid_size = stat & 0x1fff;
1052                         handle_selfid(lynx, host);
1053                 } else {
1054                         quadlet_t *q_data = lynx->rcv_page;
1055                         if ((*q_data >> 4 & 0xf) == TCODE_READQ_RESPONSE
1056                             || (*q_data >> 4 & 0xf) == TCODE_WRITEQ) {
1057                                 cpu_to_be32s(q_data + 3);
1058                         }
1059                         hpsb_packet_received(host, q_data, stat & 0x1fff, 0);
1060                 }
1061
1062                 run_pcl(lynx, lynx->rcv_pcl_start, CHANNEL_ASYNC_RCV);
1063         }
1064
1065         return IRQ_HANDLED;
1066 }
1067
1068
1069 static void iso_rcv_bh(struct ti_lynx *lynx)
1070 {
1071         unsigned int idx;
1072         quadlet_t *data;
1073         unsigned long flags;
1074
1075         spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
1076
1077         while (lynx->iso_rcv.used) {
1078                 idx = lynx->iso_rcv.last;
1079                 spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
1080
1081                 data = lynx->iso_rcv.page[idx / ISORCV_PER_PAGE]
1082                         + (idx % ISORCV_PER_PAGE) * MAX_ISORCV_SIZE;
1083
1084                 if ((*data >> 16) + 4 != (lynx->iso_rcv.stat[idx] & 0x1fff)) {
1085                         PRINT(KERN_ERR, lynx->id,
1086                               "iso length mismatch 0x%08x/0x%08x", *data,
1087                               lynx->iso_rcv.stat[idx]);
1088                 }
1089
1090                 if (lynx->iso_rcv.stat[idx]
1091                     & (DMA_CHAN_STAT_PCIERR | DMA_CHAN_STAT_PKTERR)) {
1092                         PRINT(KERN_INFO, lynx->id,
1093                               "iso receive error on %d to 0x%p", idx, data);
1094                 } else {
1095                         hpsb_packet_received(lynx->host, data,
1096                                              lynx->iso_rcv.stat[idx] & 0x1fff,
1097                                              0);
1098                 }
1099
1100                 spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
1101                 lynx->iso_rcv.last = (idx + 1) % NUM_ISORCV_PCL;
1102                 lynx->iso_rcv.used--;
1103         }
1104
1105         if (lynx->iso_rcv.chan_count) {
1106                 reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
1107                           DMA_WORD1_CMP_ENABLE_MASTER);
1108         }
1109         spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
1110 }
1111
1112
1113 static void remove_card(struct pci_dev *dev)
1114 {
1115         struct ti_lynx *lynx;
1116         struct device *lynx_dev;
1117         int i;
1118
1119         lynx = pci_get_drvdata(dev);
1120         if (!lynx) return;
1121         pci_set_drvdata(dev, NULL);
1122
1123         lynx_dev = get_device(&lynx->host->device);
1124
1125         switch (lynx->state) {
1126         case is_host:
1127                 reg_write(lynx, PCI_INT_ENABLE, 0);
1128                 hpsb_remove_host(lynx->host);
1129         case have_intr:
1130                 reg_write(lynx, PCI_INT_ENABLE, 0);
1131                 free_irq(lynx->dev->irq, lynx);
1132
1133                 /* Disable IRM Contender and LCtrl */
1134                 if (lynx->phyic.reg_1394a)
1135                         set_phy_reg(lynx, 4, ~0xc0 & get_phy_reg(lynx, 4));
1136
1137                 /* Let all other nodes know to ignore us */
1138                 lynx_devctl(lynx->host, RESET_BUS, LONG_RESET_NO_FORCE_ROOT);
1139
1140         case have_iomappings:
1141                 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
1142                 /* Fix buggy cards with autoboot pin not tied low: */
1143                 reg_write(lynx, DMA0_CHAN_CTRL, 0);
1144                 iounmap(lynx->registers);
1145                 iounmap(lynx->local_rom);
1146                 iounmap(lynx->local_ram);
1147                 iounmap(lynx->aux_port);
1148         case have_1394_buffers:
1149                 for (i = 0; i < ISORCV_PAGES; i++) {
1150                         if (lynx->iso_rcv.page[i]) {
1151                                 pci_free_consistent(lynx->dev, PAGE_SIZE,
1152                                                     lynx->iso_rcv.page[i],
1153                                                     lynx->iso_rcv.page_dma[i]);
1154                         }
1155                 }
1156                 pci_free_consistent(lynx->dev, PAGE_SIZE, lynx->rcv_page,
1157                                     lynx->rcv_page_dma);
1158         case have_aux_buf:
1159         case have_pcl_mem:
1160                 pci_free_consistent(lynx->dev, LOCALRAM_SIZE, lynx->pcl_mem,
1161                                     lynx->pcl_mem_dma);
1162         case clear:
1163                 /* do nothing - already freed */
1164                 ;
1165         }
1166
1167         tasklet_kill(&lynx->iso_rcv.tq);
1168
1169         if (lynx_dev)
1170                 put_device(lynx_dev);
1171 }
1172
1173
1174 static int __devinit add_card(struct pci_dev *dev,
1175                               const struct pci_device_id *devid_is_unused)
1176 {
1177 #define FAIL(fmt, args...) do { \
1178         PRINT_G(KERN_ERR, fmt , ## args); \
1179         remove_card(dev); \
1180         return error; \
1181         } while (0)
1182
1183         char irq_buf[16];
1184         struct hpsb_host *host;
1185         struct ti_lynx *lynx; /* shortcut to currently handled device */
1186         struct ti_pcl pcl;
1187         u32 *pcli;
1188         int i;
1189         int error;
1190
1191         error = -ENXIO;
1192
1193         if (pci_set_dma_mask(dev, DMA_32BIT_MASK))
1194                 FAIL("DMA address limits not supported for PCILynx hardware");
1195         if (pci_enable_device(dev))
1196                 FAIL("failed to enable PCILynx hardware");
1197         pci_set_master(dev);
1198
1199         error = -ENOMEM;
1200
1201         host = hpsb_alloc_host(&lynx_driver, sizeof(struct ti_lynx), &dev->dev);
1202         if (!host) FAIL("failed to allocate control structure memory");
1203
1204         lynx = host->hostdata;
1205         lynx->id = card_id++;
1206         lynx->dev = dev;
1207         lynx->state = clear;
1208         lynx->host = host;
1209         host->pdev = dev;
1210         pci_set_drvdata(dev, lynx);
1211
1212         spin_lock_init(&lynx->lock);
1213         spin_lock_init(&lynx->phy_reg_lock);
1214
1215         lynx->pcl_mem = pci_alloc_consistent(dev, LOCALRAM_SIZE,
1216                                              &lynx->pcl_mem_dma);
1217
1218         if (lynx->pcl_mem != NULL) {
1219                 lynx->state = have_pcl_mem;
1220                 PRINT(KERN_INFO, lynx->id,
1221                       "allocated PCL memory %d Bytes @ 0x%p", LOCALRAM_SIZE,
1222                       lynx->pcl_mem);
1223         } else {
1224                 FAIL("failed to allocate PCL memory area");
1225         }
1226
1227         lynx->rcv_page = pci_alloc_consistent(dev, PAGE_SIZE,
1228                                               &lynx->rcv_page_dma);
1229         if (lynx->rcv_page == NULL) {
1230                 FAIL("failed to allocate receive buffer");
1231         }
1232         lynx->state = have_1394_buffers;
1233
1234         for (i = 0; i < ISORCV_PAGES; i++) {
1235                 lynx->iso_rcv.page[i] =
1236                         pci_alloc_consistent(dev, PAGE_SIZE,
1237                                              &lynx->iso_rcv.page_dma[i]);
1238                 if (lynx->iso_rcv.page[i] == NULL) {
1239                         FAIL("failed to allocate iso receive buffers");
1240                 }
1241         }
1242
1243         lynx->registers = ioremap_nocache(pci_resource_start(dev,0),
1244                                           PCILYNX_MAX_REGISTER);
1245         lynx->local_ram = ioremap(pci_resource_start(dev,1), PCILYNX_MAX_MEMORY);
1246         lynx->aux_port  = ioremap(pci_resource_start(dev,2), PCILYNX_MAX_MEMORY);
1247         lynx->local_rom = ioremap(pci_resource_start(dev,PCI_ROM_RESOURCE),
1248                                   PCILYNX_MAX_MEMORY);
1249         lynx->state = have_iomappings;
1250
1251         if (lynx->registers == NULL) {
1252                 FAIL("failed to remap registers - card not accessible");
1253         }
1254
1255         reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
1256         /* Fix buggy cards with autoboot pin not tied low: */
1257         reg_write(lynx, DMA0_CHAN_CTRL, 0);
1258
1259         sprintf (irq_buf, "%d", dev->irq);
1260
1261         if (!request_irq(dev->irq, lynx_irq_handler, IRQF_SHARED,
1262                          PCILYNX_DRIVER_NAME, lynx)) {
1263                 PRINT(KERN_INFO, lynx->id, "allocated interrupt %s", irq_buf);
1264                 lynx->state = have_intr;
1265         } else {
1266                 FAIL("failed to allocate shared interrupt %s", irq_buf);
1267         }
1268
1269         /* alloc_pcl return values are not checked, it is expected that the
1270          * provided PCL space is sufficient for the initial allocations */
1271         lynx->rcv_pcl = alloc_pcl(lynx);
1272         lynx->rcv_pcl_start = alloc_pcl(lynx);
1273         lynx->async.pcl = alloc_pcl(lynx);
1274         lynx->async.pcl_start = alloc_pcl(lynx);
1275         lynx->iso_send.pcl = alloc_pcl(lynx);
1276         lynx->iso_send.pcl_start = alloc_pcl(lynx);
1277
1278         for (i = 0; i < NUM_ISORCV_PCL; i++) {
1279                 lynx->iso_rcv.pcl[i] = alloc_pcl(lynx);
1280         }
1281         lynx->iso_rcv.pcl_start = alloc_pcl(lynx);
1282
1283         /* all allocations successful - simple init stuff follows */
1284
1285         reg_write(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
1286
1287         tasklet_init(&lynx->iso_rcv.tq, (void (*)(unsigned long))iso_rcv_bh,
1288                      (unsigned long)lynx);
1289
1290         spin_lock_init(&lynx->iso_rcv.lock);
1291
1292         spin_lock_init(&lynx->async.queue_lock);
1293         lynx->async.channel = CHANNEL_ASYNC_SEND;
1294         spin_lock_init(&lynx->iso_send.queue_lock);
1295         lynx->iso_send.channel = CHANNEL_ISO_SEND;
1296
1297         PRINT(KERN_INFO, lynx->id, "remapped memory spaces reg 0x%p, rom 0x%p, "
1298               "ram 0x%p, aux 0x%p", lynx->registers, lynx->local_rom,
1299               lynx->local_ram, lynx->aux_port);
1300
1301         /* now, looking for PHY register set */
1302         if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
1303                 lynx->phyic.reg_1394a = 1;
1304                 PRINT(KERN_INFO, lynx->id,
1305                       "found 1394a conform PHY (using extended register set)");
1306                 lynx->phyic.vendor = get_phy_vendorid(lynx);
1307                 lynx->phyic.product = get_phy_productid(lynx);
1308         } else {
1309                 lynx->phyic.reg_1394a = 0;
1310                 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
1311         }
1312
1313         lynx->selfid_size = -1;
1314         lynx->phy_reg0 = -1;
1315
1316         INIT_LIST_HEAD(&lynx->async.queue);
1317         INIT_LIST_HEAD(&lynx->async.pcl_queue);
1318         INIT_LIST_HEAD(&lynx->iso_send.queue);
1319         INIT_LIST_HEAD(&lynx->iso_send.pcl_queue);
1320
1321         pcl.next = pcl_bus(lynx, lynx->rcv_pcl);
1322         put_pcl(lynx, lynx->rcv_pcl_start, &pcl);
1323
1324         pcl.next = PCL_NEXT_INVALID;
1325         pcl.async_error_next = PCL_NEXT_INVALID;
1326
1327         pcl.buffer[0].control = PCL_CMD_RCV | 16;
1328 #ifndef __BIG_ENDIAN
1329         pcl.buffer[0].control |= PCL_BIGENDIAN;
1330 #endif
1331         pcl.buffer[1].control = PCL_LAST_BUFF | 4080;
1332
1333         pcl.buffer[0].pointer = lynx->rcv_page_dma;
1334         pcl.buffer[1].pointer = lynx->rcv_page_dma + 16;
1335         put_pcl(lynx, lynx->rcv_pcl, &pcl);
1336
1337         pcl.next = pcl_bus(lynx, lynx->async.pcl);
1338         pcl.async_error_next = pcl_bus(lynx, lynx->async.pcl);
1339         put_pcl(lynx, lynx->async.pcl_start, &pcl);
1340
1341         pcl.next = pcl_bus(lynx, lynx->iso_send.pcl);
1342         pcl.async_error_next = PCL_NEXT_INVALID;
1343         put_pcl(lynx, lynx->iso_send.pcl_start, &pcl);
1344
1345         pcl.next = PCL_NEXT_INVALID;
1346         pcl.async_error_next = PCL_NEXT_INVALID;
1347         pcl.buffer[0].control = PCL_CMD_RCV | 4;
1348 #ifndef __BIG_ENDIAN
1349         pcl.buffer[0].control |= PCL_BIGENDIAN;
1350 #endif
1351         pcl.buffer[1].control = PCL_LAST_BUFF | 2044;
1352
1353         for (i = 0; i < NUM_ISORCV_PCL; i++) {
1354                 int page = i / ISORCV_PER_PAGE;
1355                 int sec = i % ISORCV_PER_PAGE;
1356
1357                 pcl.buffer[0].pointer = lynx->iso_rcv.page_dma[page]
1358                         + sec * MAX_ISORCV_SIZE;
1359                 pcl.buffer[1].pointer = pcl.buffer[0].pointer + 4;
1360                 put_pcl(lynx, lynx->iso_rcv.pcl[i], &pcl);
1361         }
1362
1363         pcli = (u32 *)&pcl;
1364         for (i = 0; i < NUM_ISORCV_PCL; i++) {
1365                 pcli[i] = pcl_bus(lynx, lynx->iso_rcv.pcl[i]);
1366         }
1367         put_pcl(lynx, lynx->iso_rcv.pcl_start, &pcl);
1368
1369         /* FIFO sizes from left to right: ITF=48 ATF=48 GRF=160 */
1370         reg_write(lynx, FIFO_SIZES, 0x003030a0);
1371         /* 20 byte threshold before triggering PCI transfer */
1372         reg_write(lynx, DMA_GLOBAL_REGISTER, 0x2<<24);
1373         /* threshold on both send FIFOs before transmitting:
1374            FIFO size - cache line size - 1 */
1375         i = reg_read(lynx, PCI_LATENCY_CACHELINE) & 0xff;
1376         i = 0x30 - i - 1;
1377         reg_write(lynx, FIFO_XMIT_THRESHOLD, (i << 8) | i);
1378
1379         reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_1394);
1380
1381         reg_write(lynx, LINK_INT_ENABLE, LINK_INT_PHY_TIMEOUT
1382                   | LINK_INT_PHY_REG_RCVD  | LINK_INT_PHY_BUSRESET
1383                   | LINK_INT_ISO_STUCK     | LINK_INT_ASYNC_STUCK
1384                   | LINK_INT_SENT_REJECT   | LINK_INT_TX_INVALID_TC
1385                   | LINK_INT_GRF_OVERFLOW  | LINK_INT_ITF_UNDERFLOW
1386                   | LINK_INT_ATF_UNDERFLOW);
1387
1388         reg_write(lynx, DMA_WORD0_CMP_VALUE(CHANNEL_ASYNC_RCV), 0);
1389         reg_write(lynx, DMA_WORD0_CMP_ENABLE(CHANNEL_ASYNC_RCV), 0xa<<4);
1390         reg_write(lynx, DMA_WORD1_CMP_VALUE(CHANNEL_ASYNC_RCV), 0);
1391         reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ASYNC_RCV),
1392                   DMA_WORD1_CMP_MATCH_LOCAL_NODE | DMA_WORD1_CMP_MATCH_BROADCAST
1393                   | DMA_WORD1_CMP_MATCH_EXACT    | DMA_WORD1_CMP_MATCH_BUS_BCAST
1394                   | DMA_WORD1_CMP_ENABLE_SELF_ID | DMA_WORD1_CMP_ENABLE_MASTER);
1395
1396         run_pcl(lynx, lynx->rcv_pcl_start, CHANNEL_ASYNC_RCV);
1397
1398         reg_write(lynx, DMA_WORD0_CMP_VALUE(CHANNEL_ISO_RCV), 0);
1399         reg_write(lynx, DMA_WORD0_CMP_ENABLE(CHANNEL_ISO_RCV), 0x9<<4);
1400         reg_write(lynx, DMA_WORD1_CMP_VALUE(CHANNEL_ISO_RCV), 0);
1401         reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV), 0);
1402
1403         run_sub_pcl(lynx, lynx->iso_rcv.pcl_start, 0, CHANNEL_ISO_RCV);
1404
1405         reg_write(lynx, LINK_CONTROL, LINK_CONTROL_RCV_CMP_VALID
1406                   | LINK_CONTROL_TX_ISO_EN   | LINK_CONTROL_RX_ISO_EN
1407                   | LINK_CONTROL_TX_ASYNC_EN | LINK_CONTROL_RX_ASYNC_EN
1408                   | LINK_CONTROL_RESET_TX    | LINK_CONTROL_RESET_RX);
1409
1410         if (!lynx->phyic.reg_1394a) {
1411                 if (!hpsb_disable_irm) {
1412                         /* attempt to enable contender bit -FIXME- would this
1413                          * work elsewhere? */
1414                         reg_set_bits(lynx, GPIO_CTRL_A, 0x1);
1415                         reg_write(lynx, GPIO_DATA_BASE + 0x3c, 0x1);
1416                 }
1417         } else {
1418                 /* set the contender (if appropriate) and LCtrl bit in the
1419                  * extended PHY register set. (Should check that PHY_02_EXTENDED
1420                  * is set in register 2?)
1421                  */
1422                 i = get_phy_reg(lynx, 4);
1423                 i |= PHY_04_LCTRL;
1424                 if (hpsb_disable_irm)
1425                         i &= ~PHY_04_CONTENDER;
1426                 else
1427                         i |= PHY_04_CONTENDER;
1428                 if (i != -1) set_phy_reg(lynx, 4, i);
1429         }
1430         
1431         if (!skip_eeprom)
1432         {
1433                 /* needed for i2c communication with serial eeprom */
1434                 struct i2c_adapter *i2c_ad;
1435                 struct i2c_algo_bit_data i2c_adapter_data;
1436
1437                 error = -ENOMEM;
1438                 i2c_ad = kmemdup(&bit_ops, sizeof(*i2c_ad), GFP_KERNEL);
1439                 if (!i2c_ad) FAIL("failed to allocate I2C adapter memory");
1440
1441                 i2c_adapter_data = bit_data;
1442                 i2c_ad->algo_data = &i2c_adapter_data;
1443                 i2c_adapter_data.data = lynx;
1444                 i2c_ad->dev.parent = &dev->dev;
1445
1446                 PRINTD(KERN_DEBUG, lynx->id,"original eeprom control: %d",
1447                        reg_read(lynx, SERIAL_EEPROM_CONTROL));
1448
1449                 /* reset hardware to sane state */
1450                 lynx->i2c_driven_state = 0x00000070;
1451                 reg_write(lynx, SERIAL_EEPROM_CONTROL, lynx->i2c_driven_state);
1452
1453                 if (i2c_bit_add_bus(i2c_ad) < 0)
1454                 {
1455                         kfree(i2c_ad);
1456                         error = -ENXIO;
1457                         FAIL("unable to register i2c");
1458                 }
1459                 else
1460                 {
1461                         /* do i2c stuff */
1462                         unsigned char i2c_cmd = 0x10;
1463                         struct i2c_msg msg[2] = { { 0x50, 0, 1, &i2c_cmd },
1464                                                   { 0x50, I2C_M_RD, 20, (unsigned char*) lynx->bus_info_block }
1465                                                 };
1466
1467                         /* we use i2c_transfer, because i2c_smbus_read_block_data does not work properly and we
1468                            do it more efficiently in one transaction rather then using several reads */
1469                         if (i2c_transfer(i2c_ad, msg, 2) < 0) {
1470                                 PRINT(KERN_ERR, lynx->id, "unable to read bus info block from i2c");
1471                         } else {
1472                                 PRINT(KERN_INFO, lynx->id, "got bus info block from serial eeprom");
1473                                 /* FIXME: probably we shoud rewrite the max_rec, max_ROM(1394a),
1474                                  * generation(1394a) and link_spd(1394a) field and recalculate
1475                                  * the CRC */
1476
1477                                 for (i = 0; i < 5 ; i++)
1478                                         PRINTD(KERN_DEBUG, lynx->id, "Businfo block quadlet %i: %08x",
1479                                                i, be32_to_cpu(lynx->bus_info_block[i]));
1480
1481                                 /* info_length, crc_length and 1394 magic number to check, if it is really a bus info block */
1482                                 if (((be32_to_cpu(lynx->bus_info_block[0]) & 0xffff0000) == 0x04040000) &&
1483                                     (lynx->bus_info_block[1] == __constant_cpu_to_be32(0x31333934)))
1484                                 {
1485                                         PRINT(KERN_DEBUG, lynx->id, "read a valid bus info block from");
1486                                 } else {
1487                                         kfree(i2c_ad);
1488                                         error = -ENXIO;
1489                                         FAIL("read something from serial eeprom, but it does not seem to be a valid bus info block");
1490                                 }
1491
1492                         }
1493
1494                         i2c_del_adapter(i2c_ad);
1495                         kfree(i2c_ad);
1496                 }
1497         }
1498
1499         host->csr.guid_hi = be32_to_cpu(lynx->bus_info_block[3]);
1500         host->csr.guid_lo = be32_to_cpu(lynx->bus_info_block[4]);
1501         host->csr.cyc_clk_acc = (be32_to_cpu(lynx->bus_info_block[2]) >> 16) & 0xff;
1502         host->csr.max_rec = (be32_to_cpu(lynx->bus_info_block[2]) >> 12) & 0xf;
1503         if (!lynx->phyic.reg_1394a)
1504                 host->csr.lnk_spd = (get_phy_reg(lynx, 2) & 0xc0) >> 6;
1505         else
1506                 host->csr.lnk_spd = be32_to_cpu(lynx->bus_info_block[2]) & 0x7;
1507
1508         if (hpsb_add_host(host)) {
1509                 error = -ENOMEM;
1510                 FAIL("Failed to register host with highlevel");
1511         }
1512
1513         lynx->state = is_host;
1514
1515         return 0;
1516 #undef FAIL
1517 }
1518
1519
1520 static struct pci_device_id pci_table[] = {
1521         {
1522                 .vendor =    PCI_VENDOR_ID_TI,
1523                 .device =    PCI_DEVICE_ID_TI_PCILYNX,
1524                 .subvendor = PCI_ANY_ID,
1525                 .subdevice = PCI_ANY_ID,
1526         },
1527         { }                     /* Terminating entry */
1528 };
1529
1530 static struct pci_driver lynx_pci_driver = {
1531         .name =     PCILYNX_DRIVER_NAME,
1532         .id_table = pci_table,
1533         .probe =    add_card,
1534         .remove =   remove_card,
1535 };
1536
1537 static struct hpsb_host_driver lynx_driver = {
1538         .owner =           THIS_MODULE,
1539         .name =            PCILYNX_DRIVER_NAME,
1540         .set_hw_config_rom = NULL,
1541         .transmit_packet = lynx_transmit,
1542         .devctl =          lynx_devctl,
1543         .isoctl =          NULL,
1544 };
1545
1546 MODULE_AUTHOR("Andreas E. Bombe <andreas.bombe@munich.netsurf.de>");
1547 MODULE_DESCRIPTION("driver for Texas Instruments PCI Lynx IEEE-1394 controller");
1548 MODULE_LICENSE("GPL");
1549 MODULE_SUPPORTED_DEVICE("pcilynx");
1550 MODULE_DEVICE_TABLE(pci, pci_table);
1551
1552 static int __init pcilynx_init(void)
1553 {
1554         int ret;
1555
1556         ret = pci_register_driver(&lynx_pci_driver);
1557         if (ret < 0) {
1558                 PRINT_G(KERN_ERR, "PCI module init failed");
1559                 return ret;
1560         }
1561
1562         return 0;
1563 }
1564
1565 static void __exit pcilynx_cleanup(void)
1566 {
1567         pci_unregister_driver(&lynx_pci_driver);
1568 }
1569
1570
1571 module_init(pcilynx_init);
1572 module_exit(pcilynx_cleanup);