blob: 76b2d390f6ec95aeedf1596e751f7ad055509c65 [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 Richter7429b172010-07-22 11:56:38 +020020#include <linux/device.h>
Stefan Richter28646822010-07-27 10:26:33 +020021#include <linux/errno.h>
Stefan Richter28646822010-07-27 10:26:33 +020022#include <linux/fs.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020023#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/kernel.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020027#include <linux/kref.h>
Stefan Richter28646822010-07-27 10:26:33 +020028#include <linux/miscdevice.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020029#include <linux/module.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020030#include <linux/mutex.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020031#include <linux/pci.h>
32#include <linux/poll.h>
33#include <linux/sched.h> /* required for linux/wait.h */
34#include <linux/slab.h>
35#include <linux/spinlock.h>
36#include <linux/timex.h>
37#include <linux/uaccess.h>
38#include <linux/wait.h>
santosh nayake894d1d2012-02-21 15:38:13 +053039#include <linux/dma-mapping.h>
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/atomic.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020041#include <asm/byteorder.h>
Stefan Richter28646822010-07-27 10:26:33 +020042
43#include "nosy.h"
44#include "nosy-user.h"
45
46#define TCODE_PHY_PACKET 0x10
47#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
48
Stefan Richterb5e47722010-07-27 10:28:30 +020049static char driver_name[] = KBUILD_MODNAME;
Stefan Richter28646822010-07-27 10:26:33 +020050
Stefan Richter28646822010-07-27 10:26:33 +020051/* this is the physical layout of a PCL, its size is 128 bytes */
52struct pcl {
Stefan Richterfd8c8d42010-07-22 11:56:38 +020053 __le32 next;
54 __le32 async_error_next;
55 u32 user_data;
56 __le32 pcl_status;
57 __le32 remaining_transfer_count;
58 __le32 next_data_buffer;
59 struct {
60 __le32 control;
61 __le32 pointer;
62 } buffer[13];
63};
Stefan Richter28646822010-07-27 10:26:33 +020064
65struct packet {
Stefan Richterb5e47722010-07-27 10:28:30 +020066 unsigned int length;
Stefan Richter28646822010-07-27 10:26:33 +020067 char data[0];
68};
69
70struct packet_buffer {
71 char *data;
72 size_t capacity;
73 long total_packet_count, lost_packet_count;
74 atomic_t size;
Stefan Richterb5e47722010-07-27 10:28:30 +020075 struct packet *head, *tail;
76 wait_queue_head_t wait;
Stefan Richter28646822010-07-27 10:26:33 +020077};
78
79struct pcilynx {
80 struct pci_dev *pci_device;
Stefan Richterc89db7b2010-07-22 11:56:38 +020081 __iomem char *registers;
Stefan Richter28646822010-07-27 10:26:33 +020082
83 struct pcl *rcv_start_pcl, *rcv_pcl;
Stefan Richterfd8c8d42010-07-22 11:56:38 +020084 __le32 *rcv_buffer;
Stefan Richter28646822010-07-27 10:26:33 +020085
86 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
87
88 spinlock_t client_list_lock;
89 struct list_head client_list;
90
91 struct miscdevice misc;
Stefan Richter424d66c2010-07-22 11:56:38 +020092 struct list_head link;
93 struct kref kref;
Stefan Richter28646822010-07-27 10:26:33 +020094};
95
Stefan Richter424d66c2010-07-22 11:56:38 +020096static inline struct pcilynx *
97lynx_get(struct pcilynx *lynx)
98{
99 kref_get(&lynx->kref);
100
101 return lynx;
102}
103
104static void
105lynx_release(struct kref *kref)
106{
107 kfree(container_of(kref, struct pcilynx, kref));
108}
109
110static inline void
111lynx_put(struct pcilynx *lynx)
112{
113 kref_put(&lynx->kref, lynx_release);
114}
115
Stefan Richter28646822010-07-27 10:26:33 +0200116struct client {
117 struct pcilynx *lynx;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200118 u32 tcode_mask;
Stefan Richter28646822010-07-27 10:26:33 +0200119 struct packet_buffer buffer;
120 struct list_head link;
121};
122
Stefan Richter424d66c2010-07-22 11:56:38 +0200123static DEFINE_MUTEX(card_mutex);
124static LIST_HEAD(card_list);
Stefan Richter28646822010-07-27 10:26:33 +0200125
126static int
127packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
128{
129 buffer->data = kmalloc(capacity, GFP_KERNEL);
130 if (buffer->data == NULL)
131 return -ENOMEM;
132 buffer->head = (struct packet *) buffer->data;
133 buffer->tail = (struct packet *) buffer->data;
134 buffer->capacity = capacity;
135 buffer->lost_packet_count = 0;
136 atomic_set(&buffer->size, 0);
Stefan Richterb5e47722010-07-27 10:28:30 +0200137 init_waitqueue_head(&buffer->wait);
Stefan Richter28646822010-07-27 10:26:33 +0200138
139 return 0;
140}
141
142static void
143packet_buffer_destroy(struct packet_buffer *buffer)
144{
145 kfree(buffer->data);
146}
147
148static int
Stefan Richterc89db7b2010-07-22 11:56:38 +0200149packet_buffer_get(struct client *client, char __user *data, size_t user_length)
Stefan Richter28646822010-07-27 10:26:33 +0200150{
Stefan Richter424d66c2010-07-22 11:56:38 +0200151 struct packet_buffer *buffer = &client->buffer;
Stefan Richter28646822010-07-27 10:26:33 +0200152 size_t length;
153 char *end;
154
155 if (wait_event_interruptible(buffer->wait,
Stefan Richter424d66c2010-07-22 11:56:38 +0200156 atomic_read(&buffer->size) > 0) ||
157 list_empty(&client->lynx->link))
Stefan Richter28646822010-07-27 10:26:33 +0200158 return -ERESTARTSYS;
159
Stefan Richter424d66c2010-07-22 11:56:38 +0200160 if (atomic_read(&buffer->size) == 0)
161 return -ENODEV;
162
Stefan Richter28646822010-07-27 10:26:33 +0200163 /* FIXME: Check length <= user_length. */
164
165 end = buffer->data + buffer->capacity;
166 length = buffer->head->length;
167
168 if (&buffer->head->data[length] < end) {
169 if (copy_to_user(data, buffer->head->data, length))
170 return -EFAULT;
171 buffer->head = (struct packet *) &buffer->head->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200172 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200173 size_t split = end - buffer->head->data;
174
175 if (copy_to_user(data, buffer->head->data, split))
176 return -EFAULT;
177 if (copy_to_user(data + split, buffer->data, length - split))
178 return -EFAULT;
179 buffer->head = (struct packet *) &buffer->data[length - split];
180 }
181
Stefan Richterb5e47722010-07-27 10:28:30 +0200182 /*
183 * Decrease buffer->size as the last thing, since this is what
Stefan Richter28646822010-07-27 10:26:33 +0200184 * keeps the interrupt from overwriting the packet we are
Stefan Richterb5e47722010-07-27 10:28:30 +0200185 * retrieving from the buffer.
186 */
187 atomic_sub(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200188
189 return length;
190}
191
192static void
193packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
194{
195 char *end;
196
197 buffer->total_packet_count++;
198
Stefan Richterb5e47722010-07-27 10:28:30 +0200199 if (buffer->capacity <
200 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
Stefan Richter28646822010-07-27 10:26:33 +0200201 buffer->lost_packet_count++;
202 return;
203 }
204
205 end = buffer->data + buffer->capacity;
206 buffer->tail->length = length;
207
208 if (&buffer->tail->data[length] < end) {
209 memcpy(buffer->tail->data, data, length);
210 buffer->tail = (struct packet *) &buffer->tail->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200211 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200212 size_t split = end - buffer->tail->data;
213
214 memcpy(buffer->tail->data, data, split);
215 memcpy(buffer->data, data + split, length - split);
216 buffer->tail = (struct packet *) &buffer->data[length - split];
217 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200218
Stefan Richter28646822010-07-27 10:26:33 +0200219 /* Finally, adjust buffer size and wake up userspace reader. */
220
Stefan Richterb5e47722010-07-27 10:28:30 +0200221 atomic_add(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200222 wake_up_interruptible(&buffer->wait);
223}
224
225static inline void
226reg_write(struct pcilynx *lynx, int offset, u32 data)
227{
Stefan Richterb5e47722010-07-27 10:28:30 +0200228 writel(data, lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200229}
230
231static inline u32
232reg_read(struct pcilynx *lynx, int offset)
233{
Stefan Richterb5e47722010-07-27 10:28:30 +0200234 return readl(lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200235}
236
237static inline void
238reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
239{
Stefan Richterb5e47722010-07-27 10:28:30 +0200240 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
Stefan Richter28646822010-07-27 10:26:33 +0200241}
242
Stefan Richterb5e47722010-07-27 10:28:30 +0200243/*
244 * Maybe the pcl programs could be set up to just append data instead
245 * of using a whole packet.
246 */
247static inline void
248run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
249 int dmachan)
Stefan Richter28646822010-07-27 10:26:33 +0200250{
Stefan Richterb5e47722010-07-27 10:28:30 +0200251 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
252 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
253 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
Stefan Richter28646822010-07-27 10:26:33 +0200254}
255
256static int
257set_phy_reg(struct pcilynx *lynx, int addr, int val)
258{
Stefan Richterb5e47722010-07-27 10:28:30 +0200259 if (addr > 15) {
Stefan Richter7429b172010-07-22 11:56:38 +0200260 dev_err(&lynx->pci_device->dev,
261 "PHY register address %d out of range\n", addr);
Stefan Richterb5e47722010-07-27 10:28:30 +0200262 return -1;
263 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200264 if (val > 0xff) {
Stefan Richter7429b172010-07-22 11:56:38 +0200265 dev_err(&lynx->pci_device->dev,
266 "PHY register value %d out of range\n", val);
Stefan Richterb5e47722010-07-27 10:28:30 +0200267 return -1;
268 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200269 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
Stefan Richter28646822010-07-27 10:26:33 +0200270 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
271
Stefan Richterb5e47722010-07-27 10:28:30 +0200272 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200273}
274
Stefan Richter28646822010-07-27 10:26:33 +0200275static int
276nosy_open(struct inode *inode, struct file *file)
277{
278 int minor = iminor(inode);
Stefan Richter55e77c02010-07-22 11:56:38 +0200279 struct client *client;
Stefan Richter424d66c2010-07-22 11:56:38 +0200280 struct pcilynx *tmp, *lynx = NULL;
Stefan Richter28646822010-07-27 10:26:33 +0200281
Stefan Richter424d66c2010-07-22 11:56:38 +0200282 mutex_lock(&card_mutex);
283 list_for_each_entry(tmp, &card_list, link)
284 if (tmp->misc.minor == minor) {
285 lynx = lynx_get(tmp);
286 break;
287 }
288 mutex_unlock(&card_mutex);
289 if (lynx == NULL)
Stefan Richter28646822010-07-27 10:26:33 +0200290 return -ENODEV;
291
Stefan Richter55e77c02010-07-22 11:56:38 +0200292 client = kmalloc(sizeof *client, GFP_KERNEL);
293 if (client == NULL)
Stefan Richter424d66c2010-07-22 11:56:38 +0200294 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200295
296 client->tcode_mask = ~0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200297 client->lynx = lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200298 INIT_LIST_HEAD(&client->link);
299
Stefan Richter424d66c2010-07-22 11:56:38 +0200300 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
301 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200302
303 file->private_data = client;
304
Stefan Richter60a74a62010-10-23 13:18:56 +0200305 return nonseekable_open(inode, file);
Stefan Richter424d66c2010-07-22 11:56:38 +0200306fail:
307 kfree(client);
308 lynx_put(lynx);
309
310 return -ENOMEM;
Stefan Richter28646822010-07-27 10:26:33 +0200311}
312
313static int
314nosy_release(struct inode *inode, struct file *file)
315{
Stefan Richter55e77c02010-07-22 11:56:38 +0200316 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200317 struct pcilynx *lynx = client->lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200318
Stefan Richter424d66c2010-07-22 11:56:38 +0200319 spin_lock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200320 list_del_init(&client->link);
Stefan Richter424d66c2010-07-22 11:56:38 +0200321 spin_unlock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200322
323 packet_buffer_destroy(&client->buffer);
324 kfree(client);
Stefan Richter424d66c2010-07-22 11:56:38 +0200325 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200326
Stefan Richterb5e47722010-07-27 10:28:30 +0200327 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200328}
329
330static unsigned int
331nosy_poll(struct file *file, poll_table *pt)
332{
333 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200334 unsigned int ret = 0;
Stefan Richter28646822010-07-27 10:26:33 +0200335
336 poll_wait(file, &client->buffer.wait, pt);
337
338 if (atomic_read(&client->buffer.size) > 0)
Stefan Richter424d66c2010-07-22 11:56:38 +0200339 ret = POLLIN | POLLRDNORM;
340
341 if (list_empty(&client->lynx->link))
342 ret |= POLLHUP;
343
344 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200345}
346
347static ssize_t
Stefan Richterc89db7b2010-07-22 11:56:38 +0200348nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
Stefan Richter28646822010-07-27 10:26:33 +0200349{
350 struct client *client = file->private_data;
351
Stefan Richter424d66c2010-07-22 11:56:38 +0200352 return packet_buffer_get(client, buffer, count);
Stefan Richter28646822010-07-27 10:26:33 +0200353}
354
Stefan Richterc7b2a992010-07-22 11:56:38 +0200355static long
356nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Stefan Richter28646822010-07-27 10:26:33 +0200357{
358 struct client *client = file->private_data;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200359 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
Stefan Richterb5e47722010-07-27 10:28:30 +0200360 struct nosy_stats stats;
Stefan Richter28646822010-07-27 10:26:33 +0200361
362 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200363 case NOSY_IOC_GET_STATS:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200364 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200365 stats.total_packet_count = client->buffer.total_packet_count;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200366 stats.lost_packet_count = client->buffer.lost_packet_count;
367 spin_unlock_irq(client_list_lock);
368
Stefan Richterc89db7b2010-07-22 11:56:38 +0200369 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
Stefan Richter28646822010-07-27 10:26:33 +0200370 return -EFAULT;
371 else
372 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200373
Stefan Richter28646822010-07-27 10:26:33 +0200374 case NOSY_IOC_START:
Stefan Richter55e77c02010-07-22 11:56:38 +0200375 spin_lock_irq(client_list_lock);
376 list_add_tail(&client->link, &client->lynx->client_list);
377 spin_unlock_irq(client_list_lock);
378
Stefan Richter28646822010-07-27 10:26:33 +0200379 return 0;
380
381 case NOSY_IOC_STOP:
Stefan Richter55e77c02010-07-22 11:56:38 +0200382 spin_lock_irq(client_list_lock);
383 list_del_init(&client->link);
384 spin_unlock_irq(client_list_lock);
385
Stefan Richter28646822010-07-27 10:26:33 +0200386 return 0;
387
388 case NOSY_IOC_FILTER:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200389 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200390 client->tcode_mask = arg;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200391 spin_unlock_irq(client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200392
Stefan Richter28646822010-07-27 10:26:33 +0200393 return 0;
394
395 default:
396 return -EINVAL;
397 /* Flush buffer, configure filter. */
398 }
399}
400
Stefan Richterb5e47722010-07-27 10:28:30 +0200401static const struct file_operations nosy_ops = {
Stefan Richterc7b2a992010-07-22 11:56:38 +0200402 .owner = THIS_MODULE,
403 .read = nosy_read,
404 .unlocked_ioctl = nosy_ioctl,
405 .poll = nosy_poll,
406 .open = nosy_open,
407 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200408};
409
410#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
411
Stefan Richter28646822010-07-27 10:26:33 +0200412static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200413packet_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200414{
Stefan Richter28646822010-07-27 10:26:33 +0200415 struct client *client;
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200416 u32 tcode_mask, tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200417 size_t length;
Stefan Richter28646822010-07-27 10:26:33 +0200418 struct timeval tv;
419
420 /* FIXME: Also report rcv_speed. */
421
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200422 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
423 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
Stefan Richter28646822010-07-27 10:26:33 +0200424
425 do_gettimeofday(&tv);
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200426 lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec;
Stefan Richter28646822010-07-27 10:26:33 +0200427
428 if (length == PHY_PACKET_SIZE)
429 tcode_mask = 1 << TCODE_PHY_PACKET;
430 else
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200431 tcode_mask = 1 << tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200432
Stefan Richter685c3f82010-07-22 11:56:38 +0200433 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200434
Stefan Richterb5e47722010-07-27 10:28:30 +0200435 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200436 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200437 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200438 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200439
Stefan Richter685c3f82010-07-22 11:56:38 +0200440 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200441}
442
443static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200444bus_reset_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200445{
Stefan Richter28646822010-07-27 10:26:33 +0200446 struct client *client;
447 struct timeval tv;
448
449 do_gettimeofday(&tv);
450
Stefan Richter685c3f82010-07-22 11:56:38 +0200451 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200452
Stefan Richterb5e47722010-07-27 10:28:30 +0200453 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200454 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200455
Stefan Richter685c3f82010-07-22 11:56:38 +0200456 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200457}
458
Stefan Richter28646822010-07-27 10:26:33 +0200459static irqreturn_t
460irq_handler(int irq, void *device)
461{
Stefan Richterb5e47722010-07-27 10:28:30 +0200462 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200463 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200464
465 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200466
Stefan Richter16547662010-07-22 11:56:38 +0200467 if (pci_int_status == ~0)
468 /* Card was ejected. */
469 return IRQ_NONE;
470
Stefan Richter28646822010-07-27 10:26:33 +0200471 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
472 /* Not our interrupt, bail out quickly. */
473 return IRQ_NONE;
474
475 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
476 u32 link_int_status;
477
478 link_int_status = reg_read(lynx, LINK_INT_STATUS);
479 reg_write(lynx, LINK_INT_STATUS, link_int_status);
480
481 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
Stefan Richter685c3f82010-07-22 11:56:38 +0200482 bus_reset_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200483 }
484
485 /* Clear the PCI_INT_STATUS register only after clearing the
486 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
487 * be set again immediately. */
488
489 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
490
491 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
Stefan Richter685c3f82010-07-22 11:56:38 +0200492 packet_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200493 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
494 }
495
496 return IRQ_HANDLED;
497}
498
499static void
500remove_card(struct pci_dev *dev)
501{
Stefan Richter424d66c2010-07-22 11:56:38 +0200502 struct pcilynx *lynx = pci_get_drvdata(dev);
503 struct client *client;
Stefan Richter28646822010-07-27 10:26:33 +0200504
Stefan Richter424d66c2010-07-22 11:56:38 +0200505 mutex_lock(&card_mutex);
506 list_del_init(&lynx->link);
507 misc_deregister(&lynx->misc);
508 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200509
510 reg_write(lynx, PCI_INT_ENABLE, 0);
511 free_irq(lynx->pci_device->irq, lynx);
512
Stefan Richter424d66c2010-07-22 11:56:38 +0200513 spin_lock_irq(&lynx->client_list_lock);
514 list_for_each_entry(client, &lynx->client_list, link)
515 wake_up_interruptible(&client->buffer.wait);
516 spin_unlock_irq(&lynx->client_list_lock);
517
Stefan Richterb5e47722010-07-27 10:28:30 +0200518 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200519 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
Stefan Richterb5e47722010-07-27 10:28:30 +0200520 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200521 lynx->rcv_pcl, lynx->rcv_pcl_bus);
522 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
523 lynx->rcv_buffer, lynx->rcv_buffer_bus);
524
525 iounmap(lynx->registers);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200526 pci_disable_device(dev);
Stefan Richter424d66c2010-07-22 11:56:38 +0200527 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200528}
529
530#define RCV_BUFFER_SIZE (16 * 1024)
531
Bill Pemberton03f94c02012-11-19 13:22:57 -0500532static int
Stefan Richter28646822010-07-27 10:26:33 +0200533add_card(struct pci_dev *dev, const struct pci_device_id *unused)
534{
Stefan Richterb5e47722010-07-27 10:28:30 +0200535 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200536 u32 p, end;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200537 int ret, i;
Stefan Richter28646822010-07-27 10:26:33 +0200538
santosh nayake894d1d2012-02-21 15:38:13 +0530539 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Stefan Richter7429b172010-07-22 11:56:38 +0200540 dev_err(&dev->dev,
541 "DMA address limits not supported for PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200542 return -ENXIO;
543 }
544 if (pci_enable_device(dev)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200545 dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200546 return -ENXIO;
547 }
548 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200549
550 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200551 if (lynx == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200552 dev_err(&dev->dev, "Failed to allocate control structure\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200553 ret = -ENOMEM;
554 goto fail_disable;
Stefan Richterb5e47722010-07-27 10:28:30 +0200555 }
556 lynx->pci_device = dev;
557 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200558
559 spin_lock_init(&lynx->client_list_lock);
560 INIT_LIST_HEAD(&lynx->client_list);
Stefan Richter424d66c2010-07-22 11:56:38 +0200561 kref_init(&lynx->kref);
Stefan Richter28646822010-07-27 10:26:33 +0200562
Stefan Richterb5e47722010-07-27 10:28:30 +0200563 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
564 PCILYNX_MAX_REGISTER);
Stefan Richter28646822010-07-27 10:26:33 +0200565
Stefan Richterb5e47722010-07-27 10:28:30 +0200566 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
567 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
568 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
569 sizeof(struct pcl), &lynx->rcv_pcl_bus);
570 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
571 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
572 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200573 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200574 lynx->rcv_buffer == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200575 dev_err(&dev->dev, "Failed to allocate receive buffer\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200576 ret = -ENOMEM;
577 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200578 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200579 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
580 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
581 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
Stefan Richter28646822010-07-27 10:26:33 +0200582
Stefan Richterb5e47722010-07-27 10:28:30 +0200583 lynx->rcv_pcl->buffer[0].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200584 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
585 lynx->rcv_pcl->buffer[0].pointer =
586 cpu_to_le32(lynx->rcv_buffer_bus + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200587 p = lynx->rcv_buffer_bus + 2048;
588 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
589 for (i = 1; p < end; i++, p += 2048) {
590 lynx->rcv_pcl->buffer[i].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200591 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
592 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
Stefan Richter28646822010-07-27 10:26:33 +0200593 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200594 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
Stefan Richter28646822010-07-27 10:26:33 +0200595
Stefan Richterb5e47722010-07-27 10:28:30 +0200596 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
597 /* Fix buggy cards with autoboot pin not tied low: */
598 reg_write(lynx, DMA0_CHAN_CTRL, 0);
599 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200600
601#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200602 /* now, looking for PHY register set */
603 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
604 lynx->phyic.reg_1394a = 1;
605 PRINT(KERN_INFO, lynx->id,
606 "found 1394a conform PHY (using extended register set)");
607 lynx->phyic.vendor = get_phy_vendorid(lynx);
608 lynx->phyic.product = get_phy_productid(lynx);
609 } else {
610 lynx->phyic.reg_1394a = 0;
611 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
612 }
Stefan Richter28646822010-07-27 10:26:33 +0200613#endif
614
615 /* Setup the general receive FIFO max size. */
616 reg_write(lynx, FIFO_SIZES, 255);
617
Stefan Richterb5e47722010-07-27 10:28:30 +0200618 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200619
Stefan Richterb5e47722010-07-27 10:28:30 +0200620 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200621 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
622 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
623 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
624 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
625 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
626
627 /* Disable the L flag in self ID packets. */
628 set_phy_reg(lynx, 4, 0);
629
630 /* Put this baby into snoop mode */
631 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
632
633 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
634
Stefan Richterb5e47722010-07-27 10:28:30 +0200635 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
636 driver_name, lynx)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200637 dev_err(&dev->dev,
638 "Failed to allocate shared interrupt %d\n", dev->irq);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200639 ret = -EIO;
640 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200641 }
Stefan Richter28646822010-07-27 10:26:33 +0200642
643 lynx->misc.parent = &dev->dev;
644 lynx->misc.minor = MISC_DYNAMIC_MINOR;
645 lynx->misc.name = "nosy";
646 lynx->misc.fops = &nosy_ops;
Stefan Richter424d66c2010-07-22 11:56:38 +0200647
648 mutex_lock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200649 ret = misc_register(&lynx->misc);
650 if (ret) {
Stefan Richter7429b172010-07-22 11:56:38 +0200651 dev_err(&dev->dev, "Failed to register misc char device\n");
Stefan Richter424d66c2010-07-22 11:56:38 +0200652 mutex_unlock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200653 goto fail_free_irq;
Stefan Richterb5e47722010-07-27 10:28:30 +0200654 }
Stefan Richter424d66c2010-07-22 11:56:38 +0200655 list_add_tail(&lynx->link, &card_list);
656 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200657
Stefan Richter7429b172010-07-22 11:56:38 +0200658 dev_info(&dev->dev,
659 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
Stefan Richter28646822010-07-27 10:26:33 +0200660
Stefan Richterb5e47722010-07-27 10:28:30 +0200661 return 0;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200662
663fail_free_irq:
664 reg_write(lynx, PCI_INT_ENABLE, 0);
665 free_irq(lynx->pci_device->irq, lynx);
666
667fail_deallocate:
668 if (lynx->rcv_start_pcl)
669 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
670 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
671 if (lynx->rcv_pcl)
672 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
673 lynx->rcv_pcl, lynx->rcv_pcl_bus);
674 if (lynx->rcv_buffer)
675 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
676 lynx->rcv_buffer, lynx->rcv_buffer_bus);
677 iounmap(lynx->registers);
678 kfree(lynx);
679
680fail_disable:
681 pci_disable_device(dev);
682
683 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200684}
685
Bill Pemberton7eeb7412012-11-19 13:24:13 -0500686static struct pci_device_id pci_table[] = {
Stefan Richter28646822010-07-27 10:26:33 +0200687 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200688 .vendor = PCI_VENDOR_ID_TI,
689 .device = PCI_DEVICE_ID_TI_PCILYNX,
690 .subvendor = PCI_ANY_ID,
691 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200692 },
693 { } /* Terminating entry */
694};
695
Axel Linfe2af112012-04-03 10:07:01 +0800696MODULE_DEVICE_TABLE(pci, pci_table);
697
Stefan Richter28646822010-07-27 10:26:33 +0200698static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200699 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200700 .id_table = pci_table,
701 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200702 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200703};
704
Axel Linfe2af112012-04-03 10:07:01 +0800705module_pci_driver(lynx_pci_driver);
706
Stefan Richterb5e47722010-07-27 10:28:30 +0200707MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200708MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
709MODULE_LICENSE("GPL");