blob: ea392d0985a5c992a9af3504f0ed974e9ad2e0d5 [file] [log] [blame]
Stefan Richterb5e47722010-07-27 10:28:30 +02001/*
2 * nosy - Snoop mode driver for TI PCILynx 1394 controllers
3 * Copyright (C) 2002-2007 Kristian Høgsberg
Stefan Richter28646822010-07-27 10:26:33 +02004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Stefan Richter28646822010-07-27 10:26:33 +020020#include <linux/errno.h>
Stefan Richter28646822010-07-27 10:26:33 +020021#include <linux/fs.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020022#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
Stefan Richter28646822010-07-27 10:26:33 +020026#include <linux/miscdevice.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020027#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/poll.h>
30#include <linux/sched.h> /* required for linux/wait.h */
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33#include <linux/timex.h>
34#include <linux/uaccess.h>
35#include <linux/wait.h>
36
Stefan Richter28646822010-07-27 10:26:33 +020037#include <asm/atomic.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020038#include <asm/byteorder.h>
Stefan Richter28646822010-07-27 10:26:33 +020039
40#include "nosy.h"
41#include "nosy-user.h"
42
43#define TCODE_PHY_PACKET 0x10
44#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
45
46#define notify(s, args...) printk(KERN_NOTICE s, ## args)
47#define error(s, args...) printk(KERN_ERR s, ## args)
48#define debug(s, args...) printk(KERN_DEBUG s, ## args)
49
Stefan Richterb5e47722010-07-27 10:28:30 +020050static char driver_name[] = KBUILD_MODNAME;
Stefan Richter28646822010-07-27 10:26:33 +020051
52struct pcl_status {
53 unsigned int transfer_count : 13;
54 unsigned int reserved0 : 1;
55 unsigned int ack_type : 1;
56 unsigned int ack : 4;
57 unsigned int rcv_speed : 2;
58 unsigned int rcv_dma_channel : 6;
59 unsigned int packet_complete : 1;
60 unsigned int packet_error : 1;
61 unsigned int master_error : 1;
62 unsigned int iso_mode : 1;
63 unsigned int self_id : 1;
64};
65
66/* this is the physical layout of a PCL, its size is 128 bytes */
67struct pcl {
68 u32 next;
69 u32 async_error_next;
70 u32 user_data;
71 struct pcl_status pcl_status;
72 u32 remaining_transfer_count;
73 u32 next_data_buffer;
74 struct {
75 u32 control;
76 u32 pointer;
77 } buffer[13] __attribute__ ((packed));
78} __attribute__ ((packed));
79
80struct packet {
Stefan Richterb5e47722010-07-27 10:28:30 +020081 unsigned int length;
Stefan Richter28646822010-07-27 10:26:33 +020082 char data[0];
83};
84
85struct packet_buffer {
86 char *data;
87 size_t capacity;
88 long total_packet_count, lost_packet_count;
89 atomic_t size;
Stefan Richterb5e47722010-07-27 10:28:30 +020090 struct packet *head, *tail;
91 wait_queue_head_t wait;
Stefan Richter28646822010-07-27 10:26:33 +020092};
93
94struct pcilynx {
95 struct pci_dev *pci_device;
96 unsigned char *registers;
97
98 struct pcl *rcv_start_pcl, *rcv_pcl;
99 u32 *rcv_buffer;
100
101 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
102
103 spinlock_t client_list_lock;
104 struct list_head client_list;
105
106 struct miscdevice misc;
107};
108
Stefan Richter28646822010-07-27 10:26:33 +0200109struct client {
110 struct pcilynx *lynx;
111 unsigned long tcode_mask;
112 struct packet_buffer buffer;
113 struct list_head link;
114};
115
116#define MAX_MINORS 64
Stefan Richterb5e47722010-07-27 10:28:30 +0200117static struct pcilynx *minors[MAX_MINORS];
Stefan Richter28646822010-07-27 10:26:33 +0200118
119static int
120packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
121{
122 buffer->data = kmalloc(capacity, GFP_KERNEL);
123 if (buffer->data == NULL)
124 return -ENOMEM;
125 buffer->head = (struct packet *) buffer->data;
126 buffer->tail = (struct packet *) buffer->data;
127 buffer->capacity = capacity;
128 buffer->lost_packet_count = 0;
129 atomic_set(&buffer->size, 0);
Stefan Richterb5e47722010-07-27 10:28:30 +0200130 init_waitqueue_head(&buffer->wait);
Stefan Richter28646822010-07-27 10:26:33 +0200131
132 return 0;
133}
134
135static void
136packet_buffer_destroy(struct packet_buffer *buffer)
137{
138 kfree(buffer->data);
139}
140
141static int
142packet_buffer_get(struct packet_buffer *buffer, void *data, size_t user_length)
143{
144 size_t length;
145 char *end;
146
147 if (wait_event_interruptible(buffer->wait,
148 atomic_read(&buffer->size) > 0))
149 return -ERESTARTSYS;
150
151 /* FIXME: Check length <= user_length. */
152
153 end = buffer->data + buffer->capacity;
154 length = buffer->head->length;
155
156 if (&buffer->head->data[length] < end) {
157 if (copy_to_user(data, buffer->head->data, length))
158 return -EFAULT;
159 buffer->head = (struct packet *) &buffer->head->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200160 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200161 size_t split = end - buffer->head->data;
162
163 if (copy_to_user(data, buffer->head->data, split))
164 return -EFAULT;
165 if (copy_to_user(data + split, buffer->data, length - split))
166 return -EFAULT;
167 buffer->head = (struct packet *) &buffer->data[length - split];
168 }
169
Stefan Richterb5e47722010-07-27 10:28:30 +0200170 /*
171 * Decrease buffer->size as the last thing, since this is what
Stefan Richter28646822010-07-27 10:26:33 +0200172 * keeps the interrupt from overwriting the packet we are
Stefan Richterb5e47722010-07-27 10:28:30 +0200173 * retrieving from the buffer.
174 */
175 atomic_sub(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200176
177 return length;
178}
179
180static void
181packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
182{
183 char *end;
184
185 buffer->total_packet_count++;
186
Stefan Richterb5e47722010-07-27 10:28:30 +0200187 if (buffer->capacity <
188 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
Stefan Richter28646822010-07-27 10:26:33 +0200189 buffer->lost_packet_count++;
190 return;
191 }
192
193 end = buffer->data + buffer->capacity;
194 buffer->tail->length = length;
195
196 if (&buffer->tail->data[length] < end) {
197 memcpy(buffer->tail->data, data, length);
198 buffer->tail = (struct packet *) &buffer->tail->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200199 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200200 size_t split = end - buffer->tail->data;
201
202 memcpy(buffer->tail->data, data, split);
203 memcpy(buffer->data, data + split, length - split);
204 buffer->tail = (struct packet *) &buffer->data[length - split];
205 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200206
Stefan Richter28646822010-07-27 10:26:33 +0200207 /* Finally, adjust buffer size and wake up userspace reader. */
208
Stefan Richterb5e47722010-07-27 10:28:30 +0200209 atomic_add(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200210 wake_up_interruptible(&buffer->wait);
211}
212
213static inline void
214reg_write(struct pcilynx *lynx, int offset, u32 data)
215{
Stefan Richterb5e47722010-07-27 10:28:30 +0200216 writel(data, lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200217}
218
219static inline u32
220reg_read(struct pcilynx *lynx, int offset)
221{
Stefan Richterb5e47722010-07-27 10:28:30 +0200222 return readl(lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200223}
224
225static inline void
226reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
227{
Stefan Richterb5e47722010-07-27 10:28:30 +0200228 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
Stefan Richter28646822010-07-27 10:26:33 +0200229}
230
Stefan Richterb5e47722010-07-27 10:28:30 +0200231/*
232 * Maybe the pcl programs could be set up to just append data instead
233 * of using a whole packet.
234 */
235static inline void
236run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
237 int dmachan)
Stefan Richter28646822010-07-27 10:26:33 +0200238{
Stefan Richterb5e47722010-07-27 10:28:30 +0200239 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
240 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
241 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
Stefan Richter28646822010-07-27 10:26:33 +0200242}
243
244static int
245set_phy_reg(struct pcilynx *lynx, int addr, int val)
246{
Stefan Richterb5e47722010-07-27 10:28:30 +0200247 if (addr > 15) {
248 debug("PHY register address %d out of range\n", addr);
249 return -1;
250 }
Stefan Richter28646822010-07-27 10:26:33 +0200251
Stefan Richterb5e47722010-07-27 10:28:30 +0200252 if (val > 0xff) {
253 debug("PHY register value %d out of range\n", val);
254 return -1;
255 }
Stefan Richter28646822010-07-27 10:26:33 +0200256
Stefan Richterb5e47722010-07-27 10:28:30 +0200257 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
Stefan Richter28646822010-07-27 10:26:33 +0200258 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
259
Stefan Richterb5e47722010-07-27 10:28:30 +0200260 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200261}
262
263static void
264nosy_start_snoop(struct client *client)
265{
266 unsigned long flags;
267
268 spin_lock_irqsave(&client->lynx->client_list_lock, flags);
269 list_add_tail(&client->link, &client->lynx->client_list);
270 spin_unlock_irqrestore(&client->lynx->client_list_lock, flags);
271}
272
273static void
274nosy_stop_snoop(struct client *client)
275{
276 unsigned long flags;
277
278 spin_lock_irqsave(&client->lynx->client_list_lock, flags);
279 list_del(&client->link);
280 spin_unlock_irqrestore(&client->lynx->client_list_lock, flags);
281}
282
283static struct client *
284nosy_add_client(struct pcilynx *lynx)
285{
286 struct client *client;
287
288 client = kmalloc(sizeof *client, GFP_KERNEL);
289 client->tcode_mask = ~0;
290 client->lynx = lynx;
291 INIT_LIST_HEAD(&client->link);
292
293 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) {
294 kfree(client);
295 debug("Failed to allocate packet buffer\n");
296 return NULL;
297 }
298
299 return client;
300}
301
302static void
303nosy_remove_client(struct client *client)
304{
305 nosy_stop_snoop(client);
306 packet_buffer_destroy(&client->buffer);
307 kfree(client);
308}
309
310static int
311nosy_open(struct inode *inode, struct file *file)
312{
313 int minor = iminor(inode);
314
315 if (minor > MAX_MINORS || minors[minor] == NULL)
316 return -ENODEV;
317
Stefan Richterb5e47722010-07-27 10:28:30 +0200318 file->private_data = nosy_add_client(minors[minor]);
Stefan Richter28646822010-07-27 10:26:33 +0200319 if (file->private_data == NULL)
320 return -ENOMEM;
321 else
322 return 0;
323}
324
325static int
326nosy_release(struct inode *inode, struct file *file)
327{
328 nosy_remove_client(file->private_data);
329
Stefan Richterb5e47722010-07-27 10:28:30 +0200330 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200331}
332
333static unsigned int
334nosy_poll(struct file *file, poll_table *pt)
335{
336 struct client *client = file->private_data;
337
338 poll_wait(file, &client->buffer.wait, pt);
339
340 if (atomic_read(&client->buffer.size) > 0)
341 return POLLIN | POLLRDNORM;
342 else
343 return 0;
344}
345
346static ssize_t
347nosy_read(struct file *file, char *buffer, size_t count, loff_t *offset)
348{
349 struct client *client = file->private_data;
350
351 return packet_buffer_get(&client->buffer, buffer, count);
352}
353
354static int
355nosy_ioctl(struct inode *inode, struct file *file,
356 unsigned int cmd, unsigned long arg)
357{
358 struct client *client = file->private_data;
Stefan Richterb5e47722010-07-27 10:28:30 +0200359 struct nosy_stats stats;
Stefan Richter28646822010-07-27 10:26:33 +0200360
361 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200362 case NOSY_IOC_GET_STATS:
Stefan Richter28646822010-07-27 10:26:33 +0200363 stats.total_packet_count = client->buffer.total_packet_count;
364 stats.lost_packet_count = client->buffer.lost_packet_count;
365 if (copy_to_user((void *) arg, &stats, sizeof stats))
366 return -EFAULT;
367 else
368 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200369
Stefan Richter28646822010-07-27 10:26:33 +0200370 case NOSY_IOC_START:
371 nosy_start_snoop(client);
372 return 0;
373
374 case NOSY_IOC_STOP:
375 nosy_stop_snoop(client);
376 return 0;
377
378 case NOSY_IOC_FILTER:
379 client->tcode_mask = arg;
380 return 0;
381
382 default:
383 return -EINVAL;
384 /* Flush buffer, configure filter. */
385 }
386}
387
Stefan Richterb5e47722010-07-27 10:28:30 +0200388static const struct file_operations nosy_ops = {
Stefan Richter28646822010-07-27 10:26:33 +0200389 .owner = THIS_MODULE,
Stefan Richterb5e47722010-07-27 10:28:30 +0200390 .read = nosy_read,
Stefan Richter28646822010-07-27 10:26:33 +0200391 .ioctl = nosy_ioctl,
Stefan Richterb5e47722010-07-27 10:28:30 +0200392 .poll = nosy_poll,
393 .open = nosy_open,
394 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200395};
396
397#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
398
399struct link_packet {
400 unsigned int priority : 4;
401 unsigned int tcode : 4;
402 unsigned int rt : 2;
403 unsigned int tlabel : 6;
404 unsigned int destination : 16;
405};
406
407static void
408packet_handler(struct pcilynx *lynx)
409{
410 unsigned long flags;
Stefan Richter28646822010-07-27 10:26:33 +0200411 struct client *client;
412 unsigned long tcode_mask;
413 size_t length;
414 struct link_packet *packet;
415 struct timeval tv;
416
417 /* FIXME: Also report rcv_speed. */
418
419 length = lynx->rcv_pcl->pcl_status.transfer_count;
420 packet = (struct link_packet *) &lynx->rcv_buffer[1];
421
422 do_gettimeofday(&tv);
423 lynx->rcv_buffer[0] = tv.tv_usec;
424
425 if (length == PHY_PACKET_SIZE)
426 tcode_mask = 1 << TCODE_PHY_PACKET;
427 else
428 tcode_mask = 1 << packet->tcode;
429
430 spin_lock_irqsave(&lynx->client_list_lock, flags);
431
Stefan Richterb5e47722010-07-27 10:28:30 +0200432 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200433 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200434 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200435 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200436
437 spin_unlock_irqrestore(&lynx->client_list_lock, flags);
438}
439
440static void
441bus_reset_handler(struct pcilynx *lynx)
442{
443 unsigned long flags;
Stefan Richter28646822010-07-27 10:26:33 +0200444 struct client *client;
445 struct timeval tv;
446
447 do_gettimeofday(&tv);
448
449 spin_lock_irqsave(&lynx->client_list_lock, flags);
450
Stefan Richterb5e47722010-07-27 10:28:30 +0200451 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200452 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200453
454 spin_unlock_irqrestore(&lynx->client_list_lock, flags);
455}
456
Stefan Richter28646822010-07-27 10:26:33 +0200457static irqreturn_t
458irq_handler(int irq, void *device)
459{
Stefan Richterb5e47722010-07-27 10:28:30 +0200460 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200461 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200462
463 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200464
465 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
466 /* Not our interrupt, bail out quickly. */
467 return IRQ_NONE;
468
469 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
470 u32 link_int_status;
471
472 link_int_status = reg_read(lynx, LINK_INT_STATUS);
473 reg_write(lynx, LINK_INT_STATUS, link_int_status);
474
475 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
476 bus_reset_handler(lynx);
477 }
478
479 /* Clear the PCI_INT_STATUS register only after clearing the
480 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
481 * be set again immediately. */
482
483 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
484
485 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
486 packet_handler(lynx);
487 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
488 }
489
490 return IRQ_HANDLED;
491}
492
493static void
494remove_card(struct pci_dev *dev)
495{
Stefan Richterb5e47722010-07-27 10:28:30 +0200496 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200497
Stefan Richterb5e47722010-07-27 10:28:30 +0200498 lynx = pci_get_drvdata(dev);
499 if (!lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200500 return;
Stefan Richterb5e47722010-07-27 10:28:30 +0200501 pci_set_drvdata(dev, NULL);
Stefan Richter28646822010-07-27 10:26:33 +0200502
503 reg_write(lynx, PCI_INT_ENABLE, 0);
504 free_irq(lynx->pci_device->irq, lynx);
505
Stefan Richterb5e47722010-07-27 10:28:30 +0200506 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200507 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
Stefan Richterb5e47722010-07-27 10:28:30 +0200508 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200509 lynx->rcv_pcl, lynx->rcv_pcl_bus);
510 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
511 lynx->rcv_buffer, lynx->rcv_buffer_bus);
512
513 iounmap(lynx->registers);
514
515 minors[lynx->misc.minor] = NULL;
516 misc_deregister(&lynx->misc);
517
518 kfree(lynx);
519}
520
521#define RCV_BUFFER_SIZE (16 * 1024)
522
Stefan Richter28646822010-07-27 10:26:33 +0200523static int __devinit
524add_card(struct pci_dev *dev, const struct pci_device_id *unused)
525{
Stefan Richterb5e47722010-07-27 10:28:30 +0200526 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200527 u32 p, end;
Stefan Richterb5e47722010-07-27 10:28:30 +0200528 int i;
Stefan Richter28646822010-07-27 10:26:33 +0200529
Stefan Richterb5e47722010-07-27 10:28:30 +0200530 if (pci_set_dma_mask(dev, 0xffffffff)) {
531 error("DMA address limits not supported "
532 "for PCILynx hardware\n");
533 return -ENXIO;
534 }
535 if (pci_enable_device(dev)) {
536 error("Failed to enable PCILynx hardware\n");
537 return -ENXIO;
538 }
539 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200540
541 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200542 if (lynx == NULL) {
543 error("Failed to allocate control structure memory\n");
544 return -ENOMEM;
545 }
546 lynx->pci_device = dev;
547 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200548
549 spin_lock_init(&lynx->client_list_lock);
550 INIT_LIST_HEAD(&lynx->client_list);
551
Stefan Richterb5e47722010-07-27 10:28:30 +0200552 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
553 PCILYNX_MAX_REGISTER);
Stefan Richter28646822010-07-27 10:26:33 +0200554
Stefan Richterb5e47722010-07-27 10:28:30 +0200555 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
556 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
557 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
558 sizeof(struct pcl), &lynx->rcv_pcl_bus);
559 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
560 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
561 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200562 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200563 lynx->rcv_buffer == NULL) {
Stefan Richter28646822010-07-27 10:26:33 +0200564 /* FIXME: do proper error handling. */
Stefan Richterb5e47722010-07-27 10:28:30 +0200565 error("Failed to allocate receive buffer\n");
566 return -ENOMEM;
567 }
Stefan Richter28646822010-07-27 10:26:33 +0200568 lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus;
Stefan Richterb5e47722010-07-27 10:28:30 +0200569 lynx->rcv_pcl->next = PCL_NEXT_INVALID;
570 lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID;
Stefan Richter28646822010-07-27 10:26:33 +0200571
Stefan Richterb5e47722010-07-27 10:28:30 +0200572 lynx->rcv_pcl->buffer[0].control =
Stefan Richter28646822010-07-27 10:26:33 +0200573 PCL_CMD_RCV | PCL_BIGENDIAN | 2044;
Stefan Richterb5e47722010-07-27 10:28:30 +0200574 lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4;
Stefan Richter28646822010-07-27 10:26:33 +0200575 p = lynx->rcv_buffer_bus + 2048;
576 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
577 for (i = 1; p < end; i++, p += 2048) {
578 lynx->rcv_pcl->buffer[i].control =
579 PCL_CMD_RCV | PCL_BIGENDIAN | 2048;
580 lynx->rcv_pcl->buffer[i].pointer = p;
581 }
582 lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF;
583
Stefan Richterb5e47722010-07-27 10:28:30 +0200584 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
585 /* Fix buggy cards with autoboot pin not tied low: */
586 reg_write(lynx, DMA0_CHAN_CTRL, 0);
587 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200588
589#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200590 /* now, looking for PHY register set */
591 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
592 lynx->phyic.reg_1394a = 1;
593 PRINT(KERN_INFO, lynx->id,
594 "found 1394a conform PHY (using extended register set)");
595 lynx->phyic.vendor = get_phy_vendorid(lynx);
596 lynx->phyic.product = get_phy_productid(lynx);
597 } else {
598 lynx->phyic.reg_1394a = 0;
599 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
600 }
Stefan Richter28646822010-07-27 10:26:33 +0200601#endif
602
603 /* Setup the general receive FIFO max size. */
604 reg_write(lynx, FIFO_SIZES, 255);
605
Stefan Richterb5e47722010-07-27 10:28:30 +0200606 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200607
Stefan Richterb5e47722010-07-27 10:28:30 +0200608 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200609 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
610 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
611 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
612 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
613 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
614
615 /* Disable the L flag in self ID packets. */
616 set_phy_reg(lynx, 4, 0);
617
618 /* Put this baby into snoop mode */
619 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
620
621 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
622
Stefan Richterb5e47722010-07-27 10:28:30 +0200623 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
624 driver_name, lynx)) {
625 error("Failed to allocate shared interrupt %d\n", dev->irq);
626 return -EIO;
627 }
Stefan Richter28646822010-07-27 10:26:33 +0200628
629 lynx->misc.parent = &dev->dev;
630 lynx->misc.minor = MISC_DYNAMIC_MINOR;
631 lynx->misc.name = "nosy";
632 lynx->misc.fops = &nosy_ops;
Stefan Richterb5e47722010-07-27 10:28:30 +0200633 if (misc_register(&lynx->misc)) {
634 error("Failed to register misc char device\n");
635 return -ENOMEM;
636 }
Stefan Richter28646822010-07-27 10:26:33 +0200637 minors[lynx->misc.minor] = lynx;
638
639 notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
640
Stefan Richterb5e47722010-07-27 10:28:30 +0200641 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200642}
643
644static struct pci_device_id pci_table[] __devinitdata = {
645 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200646 .vendor = PCI_VENDOR_ID_TI,
647 .device = PCI_DEVICE_ID_TI_PCILYNX,
648 .subvendor = PCI_ANY_ID,
649 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200650 },
651 { } /* Terminating entry */
652};
653
654static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200655 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200656 .id_table = pci_table,
657 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200658 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200659};
660
Stefan Richterb5e47722010-07-27 10:28:30 +0200661MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200662MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
663MODULE_LICENSE("GPL");
664MODULE_DEVICE_TABLE(pci, pci_table);
665
666static int __init nosy_init(void)
667{
Stefan Richterb5e47722010-07-27 10:28:30 +0200668 return pci_register_driver(&lynx_pci_driver);
Stefan Richter28646822010-07-27 10:26:33 +0200669}
670
671static void __exit nosy_cleanup(void)
672{
Stefan Richterb5e47722010-07-27 10:28:30 +0200673 pci_unregister_driver(&lynx_pci_driver);
Stefan Richter28646822010-07-27 10:26:33 +0200674
675 notify("Unloaded %s.\n", driver_name);
676}
677
Stefan Richter28646822010-07-27 10:26:33 +0200678module_init(nosy_init);
679module_exit(nosy_cleanup);