blob: b8dcaa28e1ade4c161b8c5aaa7460c938c9e0416 [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 Richter424d66c2010-07-22 11:56:38 +020026#include <linux/kref.h>
Stefan Richter28646822010-07-27 10:26:33 +020027#include <linux/miscdevice.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020028#include <linux/module.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020029#include <linux/mutex.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020030#include <linux/pci.h>
31#include <linux/poll.h>
32#include <linux/sched.h> /* required for linux/wait.h */
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/timex.h>
36#include <linux/uaccess.h>
37#include <linux/wait.h>
38
Stefan Richter28646822010-07-27 10:26:33 +020039#include <asm/atomic.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020040#include <asm/byteorder.h>
Stefan Richter28646822010-07-27 10:26:33 +020041
42#include "nosy.h"
43#include "nosy-user.h"
44
45#define TCODE_PHY_PACKET 0x10
46#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
47
48#define notify(s, args...) printk(KERN_NOTICE s, ## args)
49#define error(s, args...) printk(KERN_ERR s, ## args)
50#define debug(s, args...) printk(KERN_DEBUG s, ## args)
51
Stefan Richterb5e47722010-07-27 10:28:30 +020052static char driver_name[] = KBUILD_MODNAME;
Stefan Richter28646822010-07-27 10:26:33 +020053
54struct pcl_status {
55 unsigned int transfer_count : 13;
56 unsigned int reserved0 : 1;
57 unsigned int ack_type : 1;
58 unsigned int ack : 4;
59 unsigned int rcv_speed : 2;
60 unsigned int rcv_dma_channel : 6;
61 unsigned int packet_complete : 1;
62 unsigned int packet_error : 1;
63 unsigned int master_error : 1;
64 unsigned int iso_mode : 1;
65 unsigned int self_id : 1;
66};
67
68/* this is the physical layout of a PCL, its size is 128 bytes */
69struct pcl {
70 u32 next;
71 u32 async_error_next;
72 u32 user_data;
73 struct pcl_status pcl_status;
74 u32 remaining_transfer_count;
75 u32 next_data_buffer;
76 struct {
77 u32 control;
78 u32 pointer;
79 } buffer[13] __attribute__ ((packed));
80} __attribute__ ((packed));
81
82struct packet {
Stefan Richterb5e47722010-07-27 10:28:30 +020083 unsigned int length;
Stefan Richter28646822010-07-27 10:26:33 +020084 char data[0];
85};
86
87struct packet_buffer {
88 char *data;
89 size_t capacity;
90 long total_packet_count, lost_packet_count;
91 atomic_t size;
Stefan Richterb5e47722010-07-27 10:28:30 +020092 struct packet *head, *tail;
93 wait_queue_head_t wait;
Stefan Richter28646822010-07-27 10:26:33 +020094};
95
96struct pcilynx {
97 struct pci_dev *pci_device;
Stefan Richterc89db7b2010-07-22 11:56:38 +020098 __iomem char *registers;
Stefan Richter28646822010-07-27 10:26:33 +020099
100 struct pcl *rcv_start_pcl, *rcv_pcl;
101 u32 *rcv_buffer;
102
103 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
104
105 spinlock_t client_list_lock;
106 struct list_head client_list;
107
108 struct miscdevice misc;
Stefan Richter424d66c2010-07-22 11:56:38 +0200109 struct list_head link;
110 struct kref kref;
Stefan Richter28646822010-07-27 10:26:33 +0200111};
112
Stefan Richter424d66c2010-07-22 11:56:38 +0200113static inline struct pcilynx *
114lynx_get(struct pcilynx *lynx)
115{
116 kref_get(&lynx->kref);
117
118 return lynx;
119}
120
121static void
122lynx_release(struct kref *kref)
123{
124 kfree(container_of(kref, struct pcilynx, kref));
125}
126
127static inline void
128lynx_put(struct pcilynx *lynx)
129{
130 kref_put(&lynx->kref, lynx_release);
131}
132
Stefan Richter28646822010-07-27 10:26:33 +0200133struct client {
134 struct pcilynx *lynx;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200135 u32 tcode_mask;
Stefan Richter28646822010-07-27 10:26:33 +0200136 struct packet_buffer buffer;
137 struct list_head link;
138};
139
Stefan Richter424d66c2010-07-22 11:56:38 +0200140static DEFINE_MUTEX(card_mutex);
141static LIST_HEAD(card_list);
Stefan Richter28646822010-07-27 10:26:33 +0200142
143static int
144packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
145{
146 buffer->data = kmalloc(capacity, GFP_KERNEL);
147 if (buffer->data == NULL)
148 return -ENOMEM;
149 buffer->head = (struct packet *) buffer->data;
150 buffer->tail = (struct packet *) buffer->data;
151 buffer->capacity = capacity;
152 buffer->lost_packet_count = 0;
153 atomic_set(&buffer->size, 0);
Stefan Richterb5e47722010-07-27 10:28:30 +0200154 init_waitqueue_head(&buffer->wait);
Stefan Richter28646822010-07-27 10:26:33 +0200155
156 return 0;
157}
158
159static void
160packet_buffer_destroy(struct packet_buffer *buffer)
161{
162 kfree(buffer->data);
163}
164
165static int
Stefan Richterc89db7b2010-07-22 11:56:38 +0200166packet_buffer_get(struct client *client, char __user *data, size_t user_length)
Stefan Richter28646822010-07-27 10:26:33 +0200167{
Stefan Richter424d66c2010-07-22 11:56:38 +0200168 struct packet_buffer *buffer = &client->buffer;
Stefan Richter28646822010-07-27 10:26:33 +0200169 size_t length;
170 char *end;
171
172 if (wait_event_interruptible(buffer->wait,
Stefan Richter424d66c2010-07-22 11:56:38 +0200173 atomic_read(&buffer->size) > 0) ||
174 list_empty(&client->lynx->link))
Stefan Richter28646822010-07-27 10:26:33 +0200175 return -ERESTARTSYS;
176
Stefan Richter424d66c2010-07-22 11:56:38 +0200177 if (atomic_read(&buffer->size) == 0)
178 return -ENODEV;
179
Stefan Richter28646822010-07-27 10:26:33 +0200180 /* FIXME: Check length <= user_length. */
181
182 end = buffer->data + buffer->capacity;
183 length = buffer->head->length;
184
185 if (&buffer->head->data[length] < end) {
186 if (copy_to_user(data, buffer->head->data, length))
187 return -EFAULT;
188 buffer->head = (struct packet *) &buffer->head->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200189 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200190 size_t split = end - buffer->head->data;
191
192 if (copy_to_user(data, buffer->head->data, split))
193 return -EFAULT;
194 if (copy_to_user(data + split, buffer->data, length - split))
195 return -EFAULT;
196 buffer->head = (struct packet *) &buffer->data[length - split];
197 }
198
Stefan Richterb5e47722010-07-27 10:28:30 +0200199 /*
200 * Decrease buffer->size as the last thing, since this is what
Stefan Richter28646822010-07-27 10:26:33 +0200201 * keeps the interrupt from overwriting the packet we are
Stefan Richterb5e47722010-07-27 10:28:30 +0200202 * retrieving from the buffer.
203 */
204 atomic_sub(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200205
206 return length;
207}
208
209static void
210packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
211{
212 char *end;
213
214 buffer->total_packet_count++;
215
Stefan Richterb5e47722010-07-27 10:28:30 +0200216 if (buffer->capacity <
217 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
Stefan Richter28646822010-07-27 10:26:33 +0200218 buffer->lost_packet_count++;
219 return;
220 }
221
222 end = buffer->data + buffer->capacity;
223 buffer->tail->length = length;
224
225 if (&buffer->tail->data[length] < end) {
226 memcpy(buffer->tail->data, data, length);
227 buffer->tail = (struct packet *) &buffer->tail->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200228 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200229 size_t split = end - buffer->tail->data;
230
231 memcpy(buffer->tail->data, data, split);
232 memcpy(buffer->data, data + split, length - split);
233 buffer->tail = (struct packet *) &buffer->data[length - split];
234 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200235
Stefan Richter28646822010-07-27 10:26:33 +0200236 /* Finally, adjust buffer size and wake up userspace reader. */
237
Stefan Richterb5e47722010-07-27 10:28:30 +0200238 atomic_add(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200239 wake_up_interruptible(&buffer->wait);
240}
241
242static inline void
243reg_write(struct pcilynx *lynx, int offset, u32 data)
244{
Stefan Richterb5e47722010-07-27 10:28:30 +0200245 writel(data, lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200246}
247
248static inline u32
249reg_read(struct pcilynx *lynx, int offset)
250{
Stefan Richterb5e47722010-07-27 10:28:30 +0200251 return readl(lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200252}
253
254static inline void
255reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
256{
Stefan Richterb5e47722010-07-27 10:28:30 +0200257 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
Stefan Richter28646822010-07-27 10:26:33 +0200258}
259
Stefan Richterb5e47722010-07-27 10:28:30 +0200260/*
261 * Maybe the pcl programs could be set up to just append data instead
262 * of using a whole packet.
263 */
264static inline void
265run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
266 int dmachan)
Stefan Richter28646822010-07-27 10:26:33 +0200267{
Stefan Richterb5e47722010-07-27 10:28:30 +0200268 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
269 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
270 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
Stefan Richter28646822010-07-27 10:26:33 +0200271}
272
273static int
274set_phy_reg(struct pcilynx *lynx, int addr, int val)
275{
Stefan Richterb5e47722010-07-27 10:28:30 +0200276 if (addr > 15) {
277 debug("PHY register address %d out of range\n", addr);
278 return -1;
279 }
Stefan Richter28646822010-07-27 10:26:33 +0200280
Stefan Richterb5e47722010-07-27 10:28:30 +0200281 if (val > 0xff) {
282 debug("PHY register value %d out of range\n", val);
283 return -1;
284 }
Stefan Richter28646822010-07-27 10:26:33 +0200285
Stefan Richterb5e47722010-07-27 10:28:30 +0200286 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
Stefan Richter28646822010-07-27 10:26:33 +0200287 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
288
Stefan Richterb5e47722010-07-27 10:28:30 +0200289 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200290}
291
Stefan Richter28646822010-07-27 10:26:33 +0200292static int
293nosy_open(struct inode *inode, struct file *file)
294{
295 int minor = iminor(inode);
Stefan Richter55e77c02010-07-22 11:56:38 +0200296 struct client *client;
Stefan Richter424d66c2010-07-22 11:56:38 +0200297 struct pcilynx *tmp, *lynx = NULL;
Stefan Richter28646822010-07-27 10:26:33 +0200298
Stefan Richter424d66c2010-07-22 11:56:38 +0200299 mutex_lock(&card_mutex);
300 list_for_each_entry(tmp, &card_list, link)
301 if (tmp->misc.minor == minor) {
302 lynx = lynx_get(tmp);
303 break;
304 }
305 mutex_unlock(&card_mutex);
306 if (lynx == NULL)
Stefan Richter28646822010-07-27 10:26:33 +0200307 return -ENODEV;
308
Stefan Richter55e77c02010-07-22 11:56:38 +0200309 client = kmalloc(sizeof *client, GFP_KERNEL);
310 if (client == NULL)
Stefan Richter424d66c2010-07-22 11:56:38 +0200311 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200312
313 client->tcode_mask = ~0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200314 client->lynx = lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200315 INIT_LIST_HEAD(&client->link);
316
Stefan Richter424d66c2010-07-22 11:56:38 +0200317 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
318 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200319
320 file->private_data = client;
321
322 return 0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200323fail:
324 kfree(client);
325 lynx_put(lynx);
326
327 return -ENOMEM;
Stefan Richter28646822010-07-27 10:26:33 +0200328}
329
330static int
331nosy_release(struct inode *inode, struct file *file)
332{
Stefan Richter55e77c02010-07-22 11:56:38 +0200333 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200334 struct pcilynx *lynx = client->lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200335
Stefan Richter424d66c2010-07-22 11:56:38 +0200336 spin_lock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200337 list_del_init(&client->link);
Stefan Richter424d66c2010-07-22 11:56:38 +0200338 spin_unlock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200339
340 packet_buffer_destroy(&client->buffer);
341 kfree(client);
Stefan Richter424d66c2010-07-22 11:56:38 +0200342 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200343
Stefan Richterb5e47722010-07-27 10:28:30 +0200344 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200345}
346
347static unsigned int
348nosy_poll(struct file *file, poll_table *pt)
349{
350 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200351 unsigned int ret = 0;
Stefan Richter28646822010-07-27 10:26:33 +0200352
353 poll_wait(file, &client->buffer.wait, pt);
354
355 if (atomic_read(&client->buffer.size) > 0)
Stefan Richter424d66c2010-07-22 11:56:38 +0200356 ret = POLLIN | POLLRDNORM;
357
358 if (list_empty(&client->lynx->link))
359 ret |= POLLHUP;
360
361 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200362}
363
364static ssize_t
Stefan Richterc89db7b2010-07-22 11:56:38 +0200365nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
Stefan Richter28646822010-07-27 10:26:33 +0200366{
367 struct client *client = file->private_data;
368
Stefan Richter424d66c2010-07-22 11:56:38 +0200369 return packet_buffer_get(client, buffer, count);
Stefan Richter28646822010-07-27 10:26:33 +0200370}
371
Stefan Richterc7b2a992010-07-22 11:56:38 +0200372static long
373nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Stefan Richter28646822010-07-27 10:26:33 +0200374{
375 struct client *client = file->private_data;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200376 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
Stefan Richterb5e47722010-07-27 10:28:30 +0200377 struct nosy_stats stats;
Stefan Richter28646822010-07-27 10:26:33 +0200378
379 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200380 case NOSY_IOC_GET_STATS:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200381 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200382 stats.total_packet_count = client->buffer.total_packet_count;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200383 stats.lost_packet_count = client->buffer.lost_packet_count;
384 spin_unlock_irq(client_list_lock);
385
Stefan Richterc89db7b2010-07-22 11:56:38 +0200386 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
Stefan Richter28646822010-07-27 10:26:33 +0200387 return -EFAULT;
388 else
389 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200390
Stefan Richter28646822010-07-27 10:26:33 +0200391 case NOSY_IOC_START:
Stefan Richter55e77c02010-07-22 11:56:38 +0200392 spin_lock_irq(client_list_lock);
393 list_add_tail(&client->link, &client->lynx->client_list);
394 spin_unlock_irq(client_list_lock);
395
Stefan Richter28646822010-07-27 10:26:33 +0200396 return 0;
397
398 case NOSY_IOC_STOP:
Stefan Richter55e77c02010-07-22 11:56:38 +0200399 spin_lock_irq(client_list_lock);
400 list_del_init(&client->link);
401 spin_unlock_irq(client_list_lock);
402
Stefan Richter28646822010-07-27 10:26:33 +0200403 return 0;
404
405 case NOSY_IOC_FILTER:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200406 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200407 client->tcode_mask = arg;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200408 spin_unlock_irq(client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200409
Stefan Richter28646822010-07-27 10:26:33 +0200410 return 0;
411
412 default:
413 return -EINVAL;
414 /* Flush buffer, configure filter. */
415 }
416}
417
Stefan Richterb5e47722010-07-27 10:28:30 +0200418static const struct file_operations nosy_ops = {
Stefan Richterc7b2a992010-07-22 11:56:38 +0200419 .owner = THIS_MODULE,
420 .read = nosy_read,
421 .unlocked_ioctl = nosy_ioctl,
422 .poll = nosy_poll,
423 .open = nosy_open,
424 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200425};
426
427#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
428
429struct link_packet {
430 unsigned int priority : 4;
431 unsigned int tcode : 4;
432 unsigned int rt : 2;
433 unsigned int tlabel : 6;
434 unsigned int destination : 16;
435};
436
437static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200438packet_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200439{
Stefan Richter28646822010-07-27 10:26:33 +0200440 struct client *client;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200441 u32 tcode_mask;
Stefan Richter28646822010-07-27 10:26:33 +0200442 size_t length;
443 struct link_packet *packet;
444 struct timeval tv;
445
446 /* FIXME: Also report rcv_speed. */
447
448 length = lynx->rcv_pcl->pcl_status.transfer_count;
449 packet = (struct link_packet *) &lynx->rcv_buffer[1];
450
451 do_gettimeofday(&tv);
452 lynx->rcv_buffer[0] = tv.tv_usec;
453
454 if (length == PHY_PACKET_SIZE)
455 tcode_mask = 1 << TCODE_PHY_PACKET;
456 else
457 tcode_mask = 1 << packet->tcode;
458
Stefan Richter685c3f82010-07-22 11:56:38 +0200459 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200460
Stefan Richterb5e47722010-07-27 10:28:30 +0200461 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200462 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200463 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200464 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200465
Stefan Richter685c3f82010-07-22 11:56:38 +0200466 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200467}
468
469static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200470bus_reset_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200471{
Stefan Richter28646822010-07-27 10:26:33 +0200472 struct client *client;
473 struct timeval tv;
474
475 do_gettimeofday(&tv);
476
Stefan Richter685c3f82010-07-22 11:56:38 +0200477 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200478
Stefan Richterb5e47722010-07-27 10:28:30 +0200479 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200480 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200481
Stefan Richter685c3f82010-07-22 11:56:38 +0200482 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200483}
484
Stefan Richter28646822010-07-27 10:26:33 +0200485static irqreturn_t
486irq_handler(int irq, void *device)
487{
Stefan Richterb5e47722010-07-27 10:28:30 +0200488 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200489 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200490
491 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200492
Stefan Richter16547662010-07-22 11:56:38 +0200493 if (pci_int_status == ~0)
494 /* Card was ejected. */
495 return IRQ_NONE;
496
Stefan Richter28646822010-07-27 10:26:33 +0200497 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
498 /* Not our interrupt, bail out quickly. */
499 return IRQ_NONE;
500
501 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
502 u32 link_int_status;
503
504 link_int_status = reg_read(lynx, LINK_INT_STATUS);
505 reg_write(lynx, LINK_INT_STATUS, link_int_status);
506
507 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
Stefan Richter685c3f82010-07-22 11:56:38 +0200508 bus_reset_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200509 }
510
511 /* Clear the PCI_INT_STATUS register only after clearing the
512 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
513 * be set again immediately. */
514
515 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
516
517 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
Stefan Richter685c3f82010-07-22 11:56:38 +0200518 packet_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200519 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
520 }
521
522 return IRQ_HANDLED;
523}
524
525static void
526remove_card(struct pci_dev *dev)
527{
Stefan Richter424d66c2010-07-22 11:56:38 +0200528 struct pcilynx *lynx = pci_get_drvdata(dev);
529 struct client *client;
Stefan Richter28646822010-07-27 10:26:33 +0200530
Stefan Richter424d66c2010-07-22 11:56:38 +0200531 mutex_lock(&card_mutex);
532 list_del_init(&lynx->link);
533 misc_deregister(&lynx->misc);
534 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200535
536 reg_write(lynx, PCI_INT_ENABLE, 0);
537 free_irq(lynx->pci_device->irq, lynx);
538
Stefan Richter424d66c2010-07-22 11:56:38 +0200539 spin_lock_irq(&lynx->client_list_lock);
540 list_for_each_entry(client, &lynx->client_list, link)
541 wake_up_interruptible(&client->buffer.wait);
542 spin_unlock_irq(&lynx->client_list_lock);
543
Stefan Richterb5e47722010-07-27 10:28:30 +0200544 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200545 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
Stefan Richterb5e47722010-07-27 10:28:30 +0200546 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200547 lynx->rcv_pcl, lynx->rcv_pcl_bus);
548 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
549 lynx->rcv_buffer, lynx->rcv_buffer_bus);
550
551 iounmap(lynx->registers);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200552 pci_disable_device(dev);
Stefan Richter424d66c2010-07-22 11:56:38 +0200553 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200554}
555
556#define RCV_BUFFER_SIZE (16 * 1024)
557
Stefan Richter28646822010-07-27 10:26:33 +0200558static int __devinit
559add_card(struct pci_dev *dev, const struct pci_device_id *unused)
560{
Stefan Richterb5e47722010-07-27 10:28:30 +0200561 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200562 u32 p, end;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200563 int ret, i;
Stefan Richter28646822010-07-27 10:26:33 +0200564
Stefan Richterb5e47722010-07-27 10:28:30 +0200565 if (pci_set_dma_mask(dev, 0xffffffff)) {
566 error("DMA address limits not supported "
567 "for PCILynx hardware\n");
568 return -ENXIO;
569 }
570 if (pci_enable_device(dev)) {
571 error("Failed to enable PCILynx hardware\n");
572 return -ENXIO;
573 }
574 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200575
576 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200577 if (lynx == NULL) {
578 error("Failed to allocate control structure memory\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200579 ret = -ENOMEM;
580 goto fail_disable;
Stefan Richterb5e47722010-07-27 10:28:30 +0200581 }
582 lynx->pci_device = dev;
583 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200584
585 spin_lock_init(&lynx->client_list_lock);
586 INIT_LIST_HEAD(&lynx->client_list);
Stefan Richter424d66c2010-07-22 11:56:38 +0200587 kref_init(&lynx->kref);
Stefan Richter28646822010-07-27 10:26:33 +0200588
Stefan Richterb5e47722010-07-27 10:28:30 +0200589 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
590 PCILYNX_MAX_REGISTER);
Stefan Richter28646822010-07-27 10:26:33 +0200591
Stefan Richterb5e47722010-07-27 10:28:30 +0200592 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
593 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
594 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
595 sizeof(struct pcl), &lynx->rcv_pcl_bus);
596 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
597 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
598 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200599 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200600 lynx->rcv_buffer == NULL) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200601 error("Failed to allocate receive buffer\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200602 ret = -ENOMEM;
603 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200604 }
Stefan Richter28646822010-07-27 10:26:33 +0200605 lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus;
Stefan Richterb5e47722010-07-27 10:28:30 +0200606 lynx->rcv_pcl->next = PCL_NEXT_INVALID;
607 lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID;
Stefan Richter28646822010-07-27 10:26:33 +0200608
Stefan Richterb5e47722010-07-27 10:28:30 +0200609 lynx->rcv_pcl->buffer[0].control =
Stefan Richter28646822010-07-27 10:26:33 +0200610 PCL_CMD_RCV | PCL_BIGENDIAN | 2044;
Stefan Richterb5e47722010-07-27 10:28:30 +0200611 lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4;
Stefan Richter28646822010-07-27 10:26:33 +0200612 p = lynx->rcv_buffer_bus + 2048;
613 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
614 for (i = 1; p < end; i++, p += 2048) {
615 lynx->rcv_pcl->buffer[i].control =
616 PCL_CMD_RCV | PCL_BIGENDIAN | 2048;
617 lynx->rcv_pcl->buffer[i].pointer = p;
618 }
619 lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF;
620
Stefan Richterb5e47722010-07-27 10:28:30 +0200621 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
622 /* Fix buggy cards with autoboot pin not tied low: */
623 reg_write(lynx, DMA0_CHAN_CTRL, 0);
624 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200625
626#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200627 /* now, looking for PHY register set */
628 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
629 lynx->phyic.reg_1394a = 1;
630 PRINT(KERN_INFO, lynx->id,
631 "found 1394a conform PHY (using extended register set)");
632 lynx->phyic.vendor = get_phy_vendorid(lynx);
633 lynx->phyic.product = get_phy_productid(lynx);
634 } else {
635 lynx->phyic.reg_1394a = 0;
636 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
637 }
Stefan Richter28646822010-07-27 10:26:33 +0200638#endif
639
640 /* Setup the general receive FIFO max size. */
641 reg_write(lynx, FIFO_SIZES, 255);
642
Stefan Richterb5e47722010-07-27 10:28:30 +0200643 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200644
Stefan Richterb5e47722010-07-27 10:28:30 +0200645 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200646 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
647 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
648 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
649 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
650 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
651
652 /* Disable the L flag in self ID packets. */
653 set_phy_reg(lynx, 4, 0);
654
655 /* Put this baby into snoop mode */
656 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
657
658 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
659
Stefan Richterb5e47722010-07-27 10:28:30 +0200660 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
661 driver_name, lynx)) {
662 error("Failed to allocate shared interrupt %d\n", dev->irq);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200663 ret = -EIO;
664 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200665 }
Stefan Richter28646822010-07-27 10:26:33 +0200666
667 lynx->misc.parent = &dev->dev;
668 lynx->misc.minor = MISC_DYNAMIC_MINOR;
669 lynx->misc.name = "nosy";
670 lynx->misc.fops = &nosy_ops;
Stefan Richter424d66c2010-07-22 11:56:38 +0200671
672 mutex_lock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200673 ret = misc_register(&lynx->misc);
674 if (ret) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200675 error("Failed to register misc char device\n");
Stefan Richter424d66c2010-07-22 11:56:38 +0200676 mutex_unlock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200677 goto fail_free_irq;
Stefan Richterb5e47722010-07-27 10:28:30 +0200678 }
Stefan Richter424d66c2010-07-22 11:56:38 +0200679 list_add_tail(&lynx->link, &card_list);
680 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200681
682 notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
683
Stefan Richterb5e47722010-07-27 10:28:30 +0200684 return 0;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200685
686fail_free_irq:
687 reg_write(lynx, PCI_INT_ENABLE, 0);
688 free_irq(lynx->pci_device->irq, lynx);
689
690fail_deallocate:
691 if (lynx->rcv_start_pcl)
692 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
693 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
694 if (lynx->rcv_pcl)
695 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
696 lynx->rcv_pcl, lynx->rcv_pcl_bus);
697 if (lynx->rcv_buffer)
698 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
699 lynx->rcv_buffer, lynx->rcv_buffer_bus);
700 iounmap(lynx->registers);
701 kfree(lynx);
702
703fail_disable:
704 pci_disable_device(dev);
705
706 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200707}
708
709static struct pci_device_id pci_table[] __devinitdata = {
710 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200711 .vendor = PCI_VENDOR_ID_TI,
712 .device = PCI_DEVICE_ID_TI_PCILYNX,
713 .subvendor = PCI_ANY_ID,
714 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200715 },
716 { } /* Terminating entry */
717};
718
719static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200720 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200721 .id_table = pci_table,
722 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200723 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200724};
725
Stefan Richterb5e47722010-07-27 10:28:30 +0200726MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200727MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
728MODULE_LICENSE("GPL");
729MODULE_DEVICE_TABLE(pci, pci_table);
730
731static int __init nosy_init(void)
732{
Stefan Richterb5e47722010-07-27 10:28:30 +0200733 return pci_register_driver(&lynx_pci_driver);
Stefan Richter28646822010-07-27 10:26:33 +0200734}
735
736static void __exit nosy_cleanup(void)
737{
Stefan Richterb5e47722010-07-27 10:28:30 +0200738 pci_unregister_driver(&lynx_pci_driver);
Stefan Richter28646822010-07-27 10:26:33 +0200739
740 notify("Unloaded %s.\n", driver_name);
741}
742
Stefan Richter28646822010-07-27 10:26:33 +0200743module_init(nosy_init);
744module_exit(nosy_cleanup);