]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ipack/bridges/tpci200.c
Merge branch 'pm-acpi' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[karo-tx-linux.git] / drivers / staging / ipack / bridges / tpci200.c
1 /**
2  * tpci200.c
3  *
4  * driver for the TEWS TPCI-200 device
5  * Copyright (c) 2009 Nicolas Serafini, EIC2 SA
6  * Copyright (c) 2010,2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
7  * Copyright (c) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; version 2 of the License.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/module.h>
17 #include "tpci200.h"
18
19 static struct ipack_bus_ops tpci200_bus_ops;
20
21 /* TPCI200 controls registers */
22 static int control_reg[] = {
23         TPCI200_CONTROL_A_REG,
24         TPCI200_CONTROL_B_REG,
25         TPCI200_CONTROL_C_REG,
26         TPCI200_CONTROL_D_REG
27 };
28
29 /* Linked list to save the registered devices */
30 static LIST_HEAD(tpci200_list);
31
32 static int tpci200_slot_unregister(struct ipack_device *dev);
33
34 static struct tpci200_board *check_slot(struct ipack_device *dev)
35 {
36         struct tpci200_board *tpci200;
37         int found = 0;
38
39         if (dev == NULL) {
40                 pr_info("Slot doesn't exist.\n");
41                 return NULL;
42         }
43
44         list_for_each_entry(tpci200, &tpci200_list, list) {
45                 if (tpci200->number == dev->bus_nr) {
46                         found = 1;
47                         break;
48                 }
49         }
50
51         if (!found) {
52                 pr_err("Carrier not found\n");
53                 return NULL;
54         }
55
56         if (dev->slot >= TPCI200_NB_SLOT) {
57                 pr_info("Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
58                         dev->bus_nr, dev->slot, TPCI200_NB_SLOT-1);
59                 return NULL;
60         }
61
62         BUG_ON(tpci200->slots == NULL);
63         if (tpci200->slots[dev->slot].dev == NULL) {
64                 pr_info("Slot [%d:%d] is not registered !\n", dev->bus_nr,
65                         dev->slot);
66                 return NULL;
67         }
68
69         return tpci200;
70 }
71
72 static inline unsigned char __tpci200_read8(void __iomem *address,
73                                             unsigned long offset)
74 {
75         return ioread8(address + (offset^1));
76 }
77
78 static inline unsigned short __tpci200_read16(void __iomem *address,
79                                               unsigned long offset)
80 {
81         return ioread16(address + offset);
82 }
83
84 static inline unsigned int __tpci200_read32(void __iomem *address,
85                                             unsigned long offset)
86 {
87         return swahw32(ioread32(address + offset));
88 }
89
90 static inline void __tpci200_write8(unsigned char value,
91                                     void __iomem *address, unsigned long offset)
92 {
93         iowrite8(value, address+(offset^1));
94 }
95
96 static inline void __tpci200_write16(unsigned short value,
97                                      void __iomem *address,
98                                      unsigned long offset)
99 {
100         iowrite16(value, address+offset);
101 }
102
103 static inline void __tpci200_write32(unsigned int value,
104                                      void __iomem *address,
105                                      unsigned long offset)
106 {
107         iowrite32(swahw32(value), address+offset);
108 }
109
110 static struct ipack_addr_space *get_slot_address_space(struct ipack_device *dev,
111                                                        int space)
112 {
113         struct ipack_addr_space *addr;
114
115         switch (space) {
116         case IPACK_IO_SPACE:
117                 addr = &dev->io_space;
118                 break;
119         case IPACK_ID_SPACE:
120                 addr = &dev->id_space;
121                 break;
122         case IPACK_MEM_SPACE:
123                 addr = &dev->mem_space;
124                 break;
125         default:
126                 pr_err("Slot [%d:%d] space number %d doesn't exist !\n",
127                        dev->bus_nr, dev->slot, space);
128                 return NULL;
129                 break;
130         }
131
132         if ((addr->size == 0) || (addr->address == NULL)) {
133                 pr_err("Error, slot space not mapped !\n");
134                 return NULL;
135         }
136
137         return addr;
138 }
139
140 static int tpci200_read8(struct ipack_device *dev, int space,
141                          unsigned long offset, unsigned char *value)
142 {
143         struct ipack_addr_space *addr;
144         struct tpci200_board *tpci200;
145
146         tpci200 = check_slot(dev);
147         if (tpci200 == NULL)
148                 return -EINVAL;
149
150         addr = get_slot_address_space(dev, space);
151         if (addr == NULL)
152                 return -EINVAL;
153
154         if (offset >= addr->size) {
155                 pr_err("Error, slot space offset error !\n");
156                 return -EFAULT;
157         }
158
159         *value = __tpci200_read8(addr->address, offset);
160
161         return 0;
162 }
163
164 static int tpci200_read16(struct ipack_device *dev, int space,
165                           unsigned long offset, unsigned short *value)
166 {
167         struct ipack_addr_space *addr;
168         struct tpci200_board *tpci200;
169
170         tpci200 = check_slot(dev);
171         if (tpci200 == NULL)
172                 return -EINVAL;
173
174         addr = get_slot_address_space(dev, space);
175         if (addr == NULL)
176                 return -EINVAL;
177
178         if ((offset+2) >= addr->size) {
179                 pr_err("Error, slot space offset error !\n");
180                 return -EFAULT;
181         }
182         *value = __tpci200_read16(addr->address, offset);
183
184         return 0;
185 }
186
187 static int tpci200_read32(struct ipack_device *dev, int space,
188                           unsigned long offset, unsigned int *value)
189 {
190         struct ipack_addr_space *addr;
191         struct tpci200_board *tpci200;
192
193         tpci200 = check_slot(dev);
194         if (tpci200 == NULL)
195                 return -EINVAL;
196
197         addr = get_slot_address_space(dev, space);
198         if (addr == NULL)
199                 return -EINVAL;
200
201         if ((offset+4) >= addr->size) {
202                 pr_err("Error, slot space offset error !\n");
203                 return -EFAULT;
204         }
205
206         *value = __tpci200_read32(addr->address, offset);
207
208         return 0;
209 }
210
211 static int tpci200_write8(struct ipack_device *dev, int space,
212                           unsigned long offset, unsigned char value)
213 {
214         struct ipack_addr_space *addr;
215         struct tpci200_board *tpci200;
216
217         tpci200 = check_slot(dev);
218         if (tpci200 == NULL)
219                 return -EINVAL;
220
221         addr = get_slot_address_space(dev, space);
222         if (addr == NULL)
223                 return -EINVAL;
224
225         if (offset >= addr->size) {
226                 pr_err("Error, slot space offset error !\n");
227                 return -EFAULT;
228         }
229
230         __tpci200_write8(value, addr->address, offset);
231
232         return 0;
233 }
234
235 static int tpci200_write16(struct ipack_device *dev, int space,
236                            unsigned long offset, unsigned short value)
237 {
238         struct ipack_addr_space *addr;
239         struct tpci200_board *tpci200;
240
241         tpci200 = check_slot(dev);
242         if (tpci200 == NULL)
243                 return -EINVAL;
244
245         addr = get_slot_address_space(dev, space);
246         if (addr == NULL)
247                 return -EINVAL;
248
249         if ((offset+2) >= addr->size) {
250                 pr_err("Error, slot space offset error !\n");
251                 return -EFAULT;
252         }
253
254         __tpci200_write16(value, addr->address, offset);
255
256         return 0;
257 }
258
259 static int tpci200_write32(struct ipack_device *dev, int space,
260                            unsigned long offset, unsigned int value)
261 {
262         struct ipack_addr_space *addr;
263         struct tpci200_board *tpci200;
264
265         tpci200 = check_slot(dev);
266         if (tpci200 == NULL)
267                 return -EINVAL;
268
269         addr = get_slot_address_space(dev, space);
270         if (addr == NULL)
271                 return -EINVAL;
272
273         if ((offset+4) >= addr->size) {
274                 pr_err("Error, slot space offset error !\n");
275                 return -EFAULT;
276         }
277
278         __tpci200_write32(value, addr->address, offset);
279
280         return 0;
281 }
282
283 static void tpci200_unregister(struct tpci200_board *tpci200)
284 {
285         int i;
286
287         free_irq(tpci200->info->pdev->irq, (void *) tpci200);
288
289         pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
290         pci_iounmap(tpci200->info->pdev, tpci200->info->ioidint_space);
291         pci_iounmap(tpci200->info->pdev, tpci200->info->mem8_space);
292
293         pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
294         pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
295         pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
296
297         pci_disable_device(tpci200->info->pdev);
298         pci_dev_put(tpci200->info->pdev);
299
300         kfree(tpci200->info);
301
302         for (i = 0; i < TPCI200_NB_SLOT; i++) {
303                 tpci200->slots[i].io_phys.address = NULL;
304                 tpci200->slots[i].io_phys.size = 0;
305                 tpci200->slots[i].id_phys.address = NULL;
306                 tpci200->slots[i].id_phys.size = 0;
307                 tpci200->slots[i].mem_phys.address = NULL;
308                 tpci200->slots[i].mem_phys.size = 0;
309         }
310 }
311
312 static irqreturn_t tpci200_interrupt(int irq, void *dev_id)
313 {
314         struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id;
315         int i;
316         unsigned long flags;
317         unsigned short status_reg, reg_value;
318         unsigned short unhandled_ints = 0;
319         irqreturn_t ret = IRQ_NONE;
320
321         spin_lock_irqsave(&tpci200->info->access_lock, flags);
322
323         /* Read status register */
324         status_reg = readw(tpci200->info->interface_regs +
325                            TPCI200_STATUS_REG);
326
327         if (status_reg & TPCI200_SLOT_INT_MASK) {
328                 unhandled_ints = status_reg & TPCI200_SLOT_INT_MASK;
329                 /* callback to the IRQ handler for the corresponding slot */
330                 for (i = 0; i < TPCI200_NB_SLOT; i++) {
331                         if ((tpci200->slots[i].irq != NULL) &&
332                             (status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)))) {
333
334                                 ret = tpci200->slots[i].irq->handler(tpci200->slots[i].irq->arg);
335
336                                 /* Dummy reads */
337                                 readw(tpci200->slots[i].dev->io_space.address +
338                                       0xC0);
339                                 readw(tpci200->slots[i].dev->io_space.address +
340                                       0xC2);
341
342                                 unhandled_ints &= ~(((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)));
343                         }
344                 }
345         }
346         /* Interrupt not handled are disabled */
347         if (unhandled_ints) {
348                 for (i = 0; i < TPCI200_NB_SLOT; i++) {
349                         if (unhandled_ints & ((TPCI200_INT0_EN | TPCI200_INT1_EN) << (2*i))) {
350                                 pr_info("No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
351                                         tpci200->number, i);
352                                 reg_value = readw(
353                                         tpci200->info->interface_regs +
354                                         control_reg[i]);
355                                 reg_value &=
356                                         ~(TPCI200_INT0_EN | TPCI200_INT1_EN);
357                                 writew(reg_value,
358                                        (tpci200->info->interface_regs +
359                                         control_reg[i]));
360                         }
361                 }
362         }
363
364         spin_unlock_irqrestore(&tpci200->info->access_lock, flags);
365         return ret;
366 }
367
368 #ifdef CONFIG_SYSFS
369
370 static struct ipack_device *tpci200_slot_register(unsigned int tpci200_number,
371                                                   unsigned int slot_position)
372 {
373         int found = 0;
374         struct ipack_device  *dev;
375         struct tpci200_board *tpci200;
376
377         list_for_each_entry(tpci200, &tpci200_list, list) {
378                 if (tpci200->number == tpci200_number) {
379                         found = 1;
380                         break;
381                 }
382         }
383
384         if (!found) {
385                 pr_err("carrier board not found for the device\n");
386                 return NULL;
387         }
388
389         if (slot_position >= TPCI200_NB_SLOT) {
390                 pr_info("Slot [%d:%d] doesn't exist!\n", tpci200_number,
391                         slot_position);
392                 return NULL;
393         }
394
395         if (mutex_lock_interruptible(&tpci200->mutex))
396                 return NULL;
397
398         if (tpci200->slots[slot_position].dev != NULL) {
399                 pr_err("Slot [%d:%d] already installed !\n", tpci200_number,
400                        slot_position);
401                 goto err_unlock;
402         }
403
404         /*
405          * Give the same IRQ number as the slot number.
406          * The TPCI200 has assigned his own two IRQ by PCI bus driver
407          */
408         dev = ipack_device_register(tpci200->info->ipack_bus,
409                                     slot_position, slot_position);
410         if (dev == NULL) {
411                 pr_info("Slot [%d:%d] Unable to register an ipack device\n",
412                         tpci200_number, slot_position);
413                 goto err_unlock;
414         }
415
416         tpci200->slots[slot_position].dev = dev;
417         mutex_unlock(&tpci200->mutex);
418         return dev;
419
420 err_unlock:
421         mutex_unlock(&tpci200->mutex);
422         return NULL;
423 }
424
425 static ssize_t tpci200_store_board(struct device *pdev, const char *buf,
426                                    size_t count, int slot)
427 {
428         struct tpci200_board *card = dev_get_drvdata(pdev);
429         struct ipack_device *dev = card->slots[slot].dev;
430
431         if (dev != NULL)
432                 return -EBUSY;
433
434         dev = tpci200_slot_register(card->number, slot);
435         if (dev == NULL)
436                 return -ENODEV;
437
438         return count;
439 }
440
441 static ssize_t tpci200_show_board(struct device *pdev, char *buf, int slot)
442 {
443         struct tpci200_board *card = dev_get_drvdata(pdev);
444         struct ipack_device *dev = card->slots[slot].dev;
445
446         if (dev != NULL)
447                 return snprintf(buf, PAGE_SIZE, "%s\n", dev_name(&dev->dev));
448         else
449                 return snprintf(buf, PAGE_SIZE, "none\n");
450 }
451
452 static ssize_t tpci200_show_description(struct device *pdev,
453                                         struct device_attribute *attr,
454                                         char *buf)
455 {
456         return snprintf(buf, PAGE_SIZE,
457                         "TEWS tpci200 carrier PCI for Industry-pack mezzanines.\n");
458 }
459
460 static ssize_t tpci200_show_board_slot0(struct device *pdev,
461                                         struct device_attribute *attr,
462                                         char *buf)
463 {
464         return tpci200_show_board(pdev, buf, 0);
465 }
466
467 static ssize_t tpci200_store_board_slot0(struct device *pdev,
468                                          struct device_attribute *attr,
469                                          const char *buf, size_t count)
470 {
471         return tpci200_store_board(pdev, buf, count, 0);
472 }
473
474 static ssize_t tpci200_show_board_slot1(struct device *pdev,
475                                         struct device_attribute *attr,
476                                         char *buf)
477 {
478         return tpci200_show_board(pdev, buf, 1);
479 }
480
481 static ssize_t tpci200_store_board_slot1(struct device *pdev,
482                                          struct device_attribute *attr,
483                                          const char *buf, size_t count)
484 {
485         return tpci200_store_board(pdev, buf, count, 1);
486 }
487
488 static ssize_t tpci200_show_board_slot2(struct device *pdev,
489                                         struct device_attribute *attr,
490                                         char *buf)
491 {
492         return tpci200_show_board(pdev, buf, 2);
493 }
494
495 static ssize_t tpci200_store_board_slot2(struct device *pdev,
496                                          struct device_attribute *attr,
497                                          const char *buf, size_t count)
498 {
499         return tpci200_store_board(pdev, buf, count, 2);
500 }
501
502
503 static ssize_t tpci200_show_board_slot3(struct device *pdev,
504                                         struct device_attribute *attr,
505                                         char *buf)
506 {
507         return tpci200_show_board(pdev, buf, 3);
508 }
509
510 static ssize_t tpci200_store_board_slot3(struct device *pdev,
511                                          struct device_attribute *attr,
512                                          const char *buf, size_t count)
513 {
514         return tpci200_store_board(pdev, buf, count, 3);
515 }
516
517 /* Declaration of the device attributes for the TPCI200 */
518 static DEVICE_ATTR(description, S_IRUGO,
519                    tpci200_show_description, NULL);
520 static DEVICE_ATTR(board_slot0, S_IRUGO | S_IWUSR,
521                    tpci200_show_board_slot0, tpci200_store_board_slot0);
522 static DEVICE_ATTR(board_slot1, S_IRUGO | S_IWUSR,
523                    tpci200_show_board_slot1, tpci200_store_board_slot1);
524 static DEVICE_ATTR(board_slot2, S_IRUGO | S_IWUSR,
525                    tpci200_show_board_slot2, tpci200_store_board_slot2);
526 static DEVICE_ATTR(board_slot3, S_IRUGO | S_IWUSR,
527                    tpci200_show_board_slot3, tpci200_store_board_slot3);
528
529 static struct attribute *tpci200_attrs[] = {
530         &dev_attr_description.attr,
531         &dev_attr_board_slot0.attr,
532         &dev_attr_board_slot1.attr,
533         &dev_attr_board_slot2.attr,
534         &dev_attr_board_slot3.attr,
535         NULL,
536 };
537
538 static struct attribute_group tpci200_attr_group = {
539         .attrs = tpci200_attrs,
540 };
541
542 static int tpci200_create_sysfs_files(struct tpci200_board *card)
543 {
544         return sysfs_create_group(&card->info->pdev->dev.kobj,
545                                   &tpci200_attr_group);
546 }
547
548 static void tpci200_remove_sysfs_files(struct tpci200_board *card)
549 {
550         sysfs_remove_group(&card->info->pdev->dev.kobj, &tpci200_attr_group);
551 }
552
553 #else
554
555 static int tpci200_create_sysfs_files(struct tpci200_board *card)
556 {
557         return 0;
558 }
559
560 static void tpci200_remove_sysfs_files(struct tpci200_board *card)
561 {
562 }
563
564 #endif /* CONFIG_SYSFS */
565
566 static int tpci200_register(struct tpci200_board *tpci200)
567 {
568         int  i;
569         int res;
570         unsigned long ioidint_base;
571         unsigned long mem_base;
572         unsigned short slot_ctrl;
573
574         if (pci_enable_device(tpci200->info->pdev) < 0)
575                 return -ENODEV;
576
577         if (tpci200_create_sysfs_files(tpci200) < 0) {
578                 pr_err("failed creating sysfs files\n");
579                 res = -EFAULT;
580                 goto out_disable_pci;
581         }
582
583         /* Request IP interface register (Bar 2) */
584         res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR,
585                                  "Carrier IP interface registers");
586         if (res) {
587                 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
588                        tpci200->info->pdev->bus->number,
589                        tpci200->info->pdev->devfn);
590                 goto out_remove_sysfs;
591         }
592
593         /* Request IO ID INT space (Bar 3) */
594         res = pci_request_region(tpci200->info->pdev,
595                                  TPCI200_IO_ID_INT_SPACES_BAR,
596                                  "Carrier IO ID INT space");
597         if (res) {
598                 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
599                        tpci200->info->pdev->bus->number,
600                        tpci200->info->pdev->devfn);
601                 goto out_release_ip_space;
602         }
603
604         /* Request MEM space (Bar 4) */
605         res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR,
606                                  "Carrier MEM space");
607         if (res) {
608                 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
609                        tpci200->info->pdev->bus->number,
610                        tpci200->info->pdev->devfn);
611                 goto out_release_ioid_int_space;
612         }
613
614         /* Map internal tpci200 driver user space */
615         tpci200->info->interface_regs =
616                 ioremap(pci_resource_start(tpci200->info->pdev,
617                                            TPCI200_IP_INTERFACE_BAR),
618                         TPCI200_IFACE_SIZE);
619         tpci200->info->ioidint_space =
620                 ioremap(pci_resource_start(tpci200->info->pdev,
621                                            TPCI200_IO_ID_INT_SPACES_BAR),
622                         TPCI200_IOIDINT_SIZE);
623         tpci200->info->mem8_space =
624                 ioremap(pci_resource_start(tpci200->info->pdev,
625                                            TPCI200_MEM8_SPACE_BAR),
626                         TPCI200_MEM8_SIZE);
627
628         spin_lock_init(&tpci200->info->access_lock);
629         ioidint_base = pci_resource_start(tpci200->info->pdev,
630                                           TPCI200_IO_ID_INT_SPACES_BAR);
631         mem_base = pci_resource_start(tpci200->info->pdev,
632                                       TPCI200_MEM8_SPACE_BAR);
633
634         /* Set the default parameters of the slot
635          * INT0 disabled, level sensitive
636          * INT1 disabled, level sensitive
637          * error interrupt disabled
638          * timeout interrupt disabled
639          * recover time disabled
640          * clock rate 8 MHz
641          */
642         slot_ctrl = 0;
643
644         /* Set all slot physical address space */
645         for (i = 0; i < TPCI200_NB_SLOT; i++) {
646                 tpci200->slots[i].io_phys.address =
647                         (void __iomem *)ioidint_base +
648                         TPCI200_IO_SPACE_OFF + TPCI200_IO_SPACE_GAP*i;
649                 tpci200->slots[i].io_phys.size = TPCI200_IO_SPACE_SIZE;
650
651                 tpci200->slots[i].id_phys.address =
652                         (void __iomem *)ioidint_base +
653                         TPCI200_ID_SPACE_OFF + TPCI200_ID_SPACE_GAP*i;
654                 tpci200->slots[i].id_phys.size = TPCI200_ID_SPACE_SIZE;
655
656                 tpci200->slots[i].mem_phys.address =
657                         (void __iomem *)mem_base + TPCI200_MEM8_GAP*i;
658                 tpci200->slots[i].mem_phys.size = TPCI200_MEM8_SIZE;
659
660                 writew(slot_ctrl, (tpci200->info->interface_regs +
661                                    control_reg[i]));
662         }
663
664         res = request_irq(tpci200->info->pdev->irq,
665                           tpci200_interrupt, IRQF_SHARED,
666                           KBUILD_MODNAME, (void *) tpci200);
667         if (res) {
668                 pr_err("(bn 0x%X, sn 0x%X) unable to register IRQ !",
669                        tpci200->info->pdev->bus->number,
670                        tpci200->info->pdev->devfn);
671                 tpci200_unregister(tpci200);
672                 goto out_err;
673         }
674
675         return 0;
676
677 out_release_ioid_int_space:
678         pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
679 out_release_ip_space:
680         pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
681 out_remove_sysfs:
682         tpci200_remove_sysfs_files(tpci200);
683 out_disable_pci:
684         pci_disable_device(tpci200->info->pdev);
685 out_err:
686         return res;
687 }
688
689 static int __tpci200_request_irq(struct tpci200_board *tpci200,
690                                  struct ipack_device *dev)
691 {
692         unsigned short slot_ctrl;
693
694         /* Set the default parameters of the slot
695          * INT0 enabled, level sensitive
696          * INT1 enabled, level sensitive
697          * error interrupt disabled
698          * timeout interrupt disabled
699          * recover time disabled
700          * clock rate 8 MHz
701          */
702         slot_ctrl = TPCI200_INT0_EN | TPCI200_INT1_EN;
703         writew(slot_ctrl, (tpci200->info->interface_regs +
704                            control_reg[dev->slot]));
705
706         return 0;
707 }
708
709 static void __tpci200_free_irq(struct tpci200_board *tpci200,
710                                struct ipack_device *dev)
711 {
712         unsigned short slot_ctrl;
713
714         /* Set the default parameters of the slot
715          * INT0 disabled, level sensitive
716          * INT1 disabled, level sensitive
717          * error interrupt disabled
718          * timeout interrupt disabled
719          * recover time disabled
720          * clock rate 8 MHz
721          */
722         slot_ctrl = 0;
723         writew(slot_ctrl, (tpci200->info->interface_regs +
724                            control_reg[dev->slot]));
725 }
726
727 static int tpci200_free_irq(struct ipack_device *dev)
728 {
729         int res;
730         struct slot_irq *slot_irq;
731         struct tpci200_board *tpci200;
732
733         tpci200 = check_slot(dev);
734         if (tpci200 == NULL) {
735                 res = -EINVAL;
736                 goto out;
737         }
738
739         if (mutex_lock_interruptible(&tpci200->mutex)) {
740                 res = -ERESTARTSYS;
741                 goto out;
742         }
743
744         if (tpci200->slots[dev->slot].irq == NULL) {
745                 res = -EINVAL;
746                 goto out_unlock;
747         }
748
749         __tpci200_free_irq(tpci200, dev);
750         slot_irq = tpci200->slots[dev->slot].irq;
751         tpci200->slots[dev->slot].irq = NULL;
752         kfree(slot_irq);
753
754 out_unlock:
755         mutex_unlock(&tpci200->mutex);
756 out:
757         return res;
758 }
759
760 static int tpci200_slot_unmap_space(struct ipack_device *dev, int space)
761 {
762         int res;
763         struct ipack_addr_space *virt_addr_space;
764         struct tpci200_board *tpci200;
765
766         tpci200 = check_slot(dev);
767         if (tpci200 == NULL) {
768                 res = -EINVAL;
769                 goto out;
770         }
771
772         if (mutex_lock_interruptible(&tpci200->mutex)) {
773                 res = -ERESTARTSYS;
774                 goto out;
775         }
776
777         switch (space) {
778         case IPACK_IO_SPACE:
779                 if (dev->io_space.address == NULL) {
780                         pr_info("Slot [%d:%d] IO space not mapped !\n",
781                                 dev->bus_nr, dev->slot);
782                         goto out_unlock;
783                 }
784                 virt_addr_space = &dev->io_space;
785                 break;
786         case IPACK_ID_SPACE:
787                 if (dev->id_space.address == NULL) {
788                         pr_info("Slot [%d:%d] ID space not mapped !\n",
789                                 dev->bus_nr, dev->slot);
790                         goto out_unlock;
791                 }
792                 virt_addr_space = &dev->id_space;
793                 break;
794         case IPACK_MEM_SPACE:
795                 if (dev->mem_space.address == NULL) {
796                         pr_info("Slot [%d:%d] MEM space not mapped !\n",
797                                 dev->bus_nr, dev->slot);
798                 goto out_unlock;
799                 }
800                 virt_addr_space = &dev->mem_space;
801                 break;
802         default:
803                 pr_err("Slot [%d:%d] space number %d doesn't exist !\n",
804                        dev->bus_nr, dev->slot, space);
805                 res = -EINVAL;
806                 goto out_unlock;
807                 break;
808         }
809
810         iounmap(virt_addr_space->address);
811
812         virt_addr_space->address = NULL;
813         virt_addr_space->size = 0;
814 out_unlock:
815         mutex_unlock(&tpci200->mutex);
816 out:
817         return res;
818 }
819
820 static int tpci200_slot_unregister(struct ipack_device *dev)
821 {
822         struct tpci200_board *tpci200;
823
824         if (dev == NULL)
825                 return -ENODEV;
826
827         tpci200 = check_slot(dev);
828         if (tpci200 == NULL)
829                 return -EINVAL;
830
831         tpci200_free_irq(dev);
832
833         if (mutex_lock_interruptible(&tpci200->mutex))
834                 return -ERESTARTSYS;
835
836         ipack_device_unregister(dev);
837         tpci200->slots[dev->slot].dev = NULL;
838         mutex_unlock(&tpci200->mutex);
839
840         return 0;
841 }
842
843 static int tpci200_slot_map_space(struct ipack_device *dev,
844                                   unsigned int memory_size, int space)
845 {
846         int res;
847         unsigned int size_to_map;
848         void __iomem *phys_address;
849         struct ipack_addr_space *virt_addr_space;
850         struct tpci200_board *tpci200;
851
852         tpci200 = check_slot(dev);
853         if (tpci200 == NULL) {
854                 res = -EINVAL;
855                 goto out;
856         }
857
858         if (mutex_lock_interruptible(&tpci200->mutex)) {
859                 res = -ERESTARTSYS;
860                 goto out;
861         }
862
863         switch (space) {
864         case IPACK_IO_SPACE:
865                 if (dev->io_space.address != NULL) {
866                         pr_err("Slot [%d:%d] IO space already mapped !\n",
867                                tpci200->number, dev->slot);
868                         res = -EINVAL;
869                         goto out_unlock;
870                 }
871                 virt_addr_space = &dev->io_space;
872
873                 phys_address = tpci200->slots[dev->slot].io_phys.address;
874                 size_to_map = tpci200->slots[dev->slot].io_phys.size;
875                 break;
876         case IPACK_ID_SPACE:
877                 if (dev->id_space.address != NULL) {
878                         pr_err("Slot [%d:%d] ID space already mapped !\n",
879                                tpci200->number, dev->slot);
880                         res = -EINVAL;
881                         goto out_unlock;
882                 }
883                 virt_addr_space = &dev->id_space;
884
885                 phys_address = tpci200->slots[dev->slot].id_phys.address;
886                 size_to_map = tpci200->slots[dev->slot].id_phys.size;
887                 break;
888         case IPACK_MEM_SPACE:
889                 if (dev->mem_space.address != NULL) {
890                         pr_err("Slot [%d:%d] MEM space already mapped !\n",
891                                tpci200->number, dev->slot);
892                         res = -EINVAL;
893                         goto out_unlock;
894                 }
895                 virt_addr_space = &dev->mem_space;
896
897                 if (memory_size > tpci200->slots[dev->slot].mem_phys.size) {
898                         pr_err("Slot [%d:%d] request is 0x%X memory, only 0x%X available !\n",
899                                dev->bus_nr, dev->slot, memory_size,
900                                tpci200->slots[dev->slot].mem_phys.size);
901                         res = -EINVAL;
902                         goto out_unlock;
903                 }
904
905                 phys_address = tpci200->slots[dev->slot].mem_phys.address;
906                 size_to_map = memory_size;
907                 break;
908         default:
909                 pr_err("Slot [%d:%d] space %d doesn't exist !\n",
910                        tpci200->number, dev->slot, space);
911                 res = -EINVAL;
912                 goto out_unlock;
913                 break;
914         }
915
916         virt_addr_space->size = size_to_map;
917         virt_addr_space->address =
918                 ioremap((unsigned long)phys_address, size_to_map);
919
920 out_unlock:
921         mutex_unlock(&tpci200->mutex);
922 out:
923         return res;
924 }
925
926 static int tpci200_request_irq(struct ipack_device *dev, int vector,
927                                int (*handler)(void *), void *arg)
928 {
929         int res;
930         struct slot_irq *slot_irq;
931         struct tpci200_board *tpci200;
932
933         tpci200 = check_slot(dev);
934         if (tpci200 == NULL) {
935                 res = -EINVAL;
936                 goto out;
937         }
938
939         if (mutex_lock_interruptible(&tpci200->mutex)) {
940                 res = -ERESTARTSYS;
941                 goto out;
942         }
943
944         if (tpci200->slots[dev->slot].irq != NULL) {
945                 pr_err("Slot [%d:%d] IRQ already registered !\n", dev->bus_nr,
946                        dev->slot);
947                 res = -EINVAL;
948                 goto out_unlock;
949         }
950
951         slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL);
952         if (slot_irq == NULL) {
953                 pr_err("Slot [%d:%d] unable to allocate memory for IRQ !\n",
954                        dev->bus_nr, dev->slot);
955                 res = -ENOMEM;
956                 goto out_unlock;
957         }
958
959         /*
960          * WARNING: Setup Interrupt Vector in the IndustryPack device
961          * before an IRQ request.
962          * Read the User Manual of your IndustryPack device to know
963          * where to write the vector in memory.
964          */
965         slot_irq->vector = vector;
966         slot_irq->handler = handler;
967         slot_irq->arg = arg;
968         slot_irq->name = dev_name(&dev->dev);
969
970         tpci200->slots[dev->slot].irq = slot_irq;
971         res = __tpci200_request_irq(tpci200, dev);
972
973 out_unlock:
974         mutex_unlock(&tpci200->mutex);
975 out:
976         return res;
977 }
978
979 static void tpci200_slot_remove(struct tpci200_slot *slot)
980 {
981         if ((slot->dev == NULL) ||
982             (slot->dev->driver->ops->remove == NULL))
983                 return;
984
985         slot->dev->driver->ops->remove(slot->dev);
986 }
987
988 static void tpci200_uninstall(struct tpci200_board *tpci200)
989 {
990         int i;
991
992         for (i = 0; i < TPCI200_NB_SLOT; i++)
993                 tpci200_slot_remove(&tpci200->slots[i]);
994
995         tpci200_unregister(tpci200);
996         kfree(tpci200->slots);
997 }
998
999 static struct ipack_bus_ops tpci200_bus_ops = {
1000         .map_space = tpci200_slot_map_space,
1001         .unmap_space = tpci200_slot_unmap_space,
1002         .request_irq = tpci200_request_irq,
1003         .free_irq = tpci200_free_irq,
1004         .read8 = tpci200_read8,
1005         .read16 = tpci200_read16,
1006         .read32 = tpci200_read32,
1007         .write8 = tpci200_write8,
1008         .write16 = tpci200_write16,
1009         .write32 = tpci200_write32,
1010         .remove_device = tpci200_slot_unregister,
1011 };
1012
1013 static int tpci200_install(struct tpci200_board *tpci200)
1014 {
1015         int res;
1016
1017         tpci200->slots = kzalloc(
1018                 TPCI200_NB_SLOT * sizeof(struct tpci200_slot), GFP_KERNEL);
1019         if (tpci200->slots == NULL) {
1020                 res = -ENOMEM;
1021                 goto out_err;
1022         }
1023
1024         res = tpci200_register(tpci200);
1025         if (res)
1026                 goto out_free;
1027
1028         mutex_init(&tpci200->mutex);
1029         return 0;
1030
1031 out_free:
1032         kfree(tpci200->slots);
1033         tpci200->slots = NULL;
1034 out_err:
1035         return res;
1036 }
1037
1038 static int tpci200_pciprobe(struct pci_dev *pdev,
1039                             const struct pci_device_id *id)
1040 {
1041         int ret;
1042         struct tpci200_board *tpci200;
1043
1044         tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL);
1045         if (!tpci200)
1046                 return -ENOMEM;
1047
1048         tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL);
1049         if (!tpci200->info) {
1050                 kfree(tpci200);
1051                 return -ENOMEM;
1052         }
1053
1054         /* Save struct pci_dev pointer */
1055         tpci200->info->pdev = pdev;
1056         tpci200->info->id_table = (struct pci_device_id *)id;
1057
1058         /* register the device and initialize it */
1059         ret = tpci200_install(tpci200);
1060         if (ret) {
1061                 pr_err("Error during tpci200 install !\n");
1062                 kfree(tpci200->info);
1063                 kfree(tpci200);
1064                 return -ENODEV;
1065         }
1066
1067         /* Register the carrier in the industry pack bus driver */
1068         tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev,
1069                                                       TPCI200_NB_SLOT,
1070                                                       &tpci200_bus_ops);
1071         if (!tpci200->info->ipack_bus) {
1072                 pr_err("error registering the carrier on ipack driver\n");
1073                 tpci200_uninstall(tpci200);
1074                 kfree(tpci200->info);
1075                 kfree(tpci200);
1076                 return -EFAULT;
1077         }
1078
1079         /* save the bus number given by ipack to logging purpose */
1080         tpci200->number = tpci200->info->ipack_bus->bus_nr;
1081         dev_set_drvdata(&pdev->dev, tpci200);
1082         /* add the registered device in an internal linked list */
1083         list_add_tail(&tpci200->list, &tpci200_list);
1084         return ret;
1085 }
1086
1087 static void __tpci200_pci_remove(struct tpci200_board *tpci200)
1088 {
1089         tpci200_uninstall(tpci200);
1090         tpci200_remove_sysfs_files(tpci200);
1091         list_del(&tpci200->list);
1092         ipack_bus_unregister(tpci200->info->ipack_bus);
1093         kfree(tpci200->info);
1094         kfree(tpci200);
1095 }
1096
1097 static void __devexit tpci200_pci_remove(struct pci_dev *dev)
1098 {
1099         struct tpci200_board *tpci200, *next;
1100
1101         /* Search the registered device to uninstall it */
1102         list_for_each_entry_safe(tpci200, next, &tpci200_list, list) {
1103                 if (tpci200->info->pdev == dev) {
1104                         __tpci200_pci_remove(tpci200);
1105                         break;
1106                 }
1107         }
1108 }
1109
1110 static struct pci_device_id tpci200_idtable[2] = {
1111         { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID,
1112           TPCI200_SUBDEVICE_ID },
1113         { 0, },
1114 };
1115
1116 static struct pci_driver tpci200_pci_drv = {
1117         .name = "tpci200",
1118         .id_table = tpci200_idtable,
1119         .probe = tpci200_pciprobe,
1120         .remove = __devexit_p(tpci200_pci_remove),
1121 };
1122
1123 static int __init tpci200_drvr_init_module(void)
1124 {
1125         return pci_register_driver(&tpci200_pci_drv);
1126 }
1127
1128 static void __exit tpci200_drvr_exit_module(void)
1129 {
1130         struct tpci200_board *tpci200, *next;
1131
1132         list_for_each_entry_safe(tpci200, next, &tpci200_list, list)
1133                 __tpci200_pci_remove(tpci200);
1134
1135         pci_unregister_driver(&tpci200_pci_drv);
1136 }
1137
1138 MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
1139 MODULE_LICENSE("GPL");
1140 module_init(tpci200_drvr_init_module);
1141 module_exit(tpci200_drvr_exit_module);