blob: e692f1e13aa55461b40303b80990b98403235498 [file] [log] [blame]
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +02001/**
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
Samuel Iglesias Gonsalvezb9950692012-05-11 10:17:14 +020011 * Software Foundation; version 2 of the License.
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020012 */
13
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020014#include <linux/module.h>
15#include "tpci200.h"
16
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020017static struct ipack_bus_ops tpci200_bus_ops;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020018
19/* TPCI200 controls registers */
20static int control_reg[] = {
21 TPCI200_CONTROL_A_REG,
22 TPCI200_CONTROL_B_REG,
23 TPCI200_CONTROL_C_REG,
24 TPCI200_CONTROL_D_REG
25};
26
27/* Linked list to save the registered devices */
28static LIST_HEAD(tpci200_list);
29
30static int tpci200_slot_unregister(struct ipack_device *dev);
31
32static struct tpci200_board *check_slot(struct ipack_device *dev)
33{
34 struct tpci200_board *tpci200;
35 int found = 0;
36
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +020037 if (dev == NULL)
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020038 return NULL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020039
40 list_for_each_entry(tpci200, &tpci200_list, list) {
41 if (tpci200->number == dev->bus_nr) {
42 found = 1;
43 break;
44 }
45 }
46
47 if (!found) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +020048 dev_err(&dev->dev, "Carrier not found\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020049 return NULL;
50 }
51
52 if (dev->slot >= TPCI200_NB_SLOT) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +020053 dev_info(&dev->dev,
54 "Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
55 dev->bus_nr, dev->slot, TPCI200_NB_SLOT-1);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020056 return NULL;
57 }
58
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020059 return tpci200;
60}
61
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020062static inline unsigned char __tpci200_read8(void __iomem *address,
63 unsigned long offset)
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020064{
65 return ioread8(address + (offset^1));
66}
67
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020068static inline unsigned short __tpci200_read16(void __iomem *address,
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020069 unsigned long offset)
70{
71 return ioread16(address + offset);
72}
73
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020074static inline unsigned int __tpci200_read32(void __iomem *address,
75 unsigned long offset)
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020076{
77 return swahw32(ioread32(address + offset));
78}
79
80static inline void __tpci200_write8(unsigned char value,
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020081 void __iomem *address, unsigned long offset)
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020082{
83 iowrite8(value, address+(offset^1));
84}
85
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020086static inline void __tpci200_write16(unsigned short value,
87 void __iomem *address,
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020088 unsigned long offset)
89{
90 iowrite16(value, address+offset);
91}
92
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020093static inline void __tpci200_write32(unsigned int value,
94 void __iomem *address,
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +020095 unsigned long offset)
96{
97 iowrite32(swahw32(value), address+offset);
98}
99
100static struct ipack_addr_space *get_slot_address_space(struct ipack_device *dev,
101 int space)
102{
103 struct ipack_addr_space *addr;
104
105 switch (space) {
106 case IPACK_IO_SPACE:
107 addr = &dev->io_space;
108 break;
109 case IPACK_ID_SPACE:
110 addr = &dev->id_space;
111 break;
112 case IPACK_MEM_SPACE:
113 addr = &dev->mem_space;
114 break;
115 default:
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200116 dev_err(&dev->dev,
117 "Slot [%d:%d] space number %d doesn't exist !\n",
118 dev->bus_nr, dev->slot, space);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200119 return NULL;
120 break;
121 }
122
123 if ((addr->size == 0) || (addr->address == NULL)) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200124 dev_err(&dev->dev, "Error, slot space not mapped !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200125 return NULL;
126 }
127
128 return addr;
129}
130
131static int tpci200_read8(struct ipack_device *dev, int space,
132 unsigned long offset, unsigned char *value)
133{
134 struct ipack_addr_space *addr;
135 struct tpci200_board *tpci200;
136
137 tpci200 = check_slot(dev);
138 if (tpci200 == NULL)
139 return -EINVAL;
140
141 addr = get_slot_address_space(dev, space);
142 if (addr == NULL)
143 return -EINVAL;
144
145 if (offset >= addr->size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200146 dev_err(&dev->dev, "Error, slot space offset error !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200147 return -EFAULT;
148 }
149
150 *value = __tpci200_read8(addr->address, offset);
151
152 return 0;
153}
154
155static int tpci200_read16(struct ipack_device *dev, int space,
156 unsigned long offset, unsigned short *value)
157{
158 struct ipack_addr_space *addr;
159 struct tpci200_board *tpci200;
160
161 tpci200 = check_slot(dev);
162 if (tpci200 == NULL)
163 return -EINVAL;
164
165 addr = get_slot_address_space(dev, space);
166 if (addr == NULL)
167 return -EINVAL;
168
169 if ((offset+2) >= addr->size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200170 dev_err(&dev->dev, "Error, slot space offset error !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200171 return -EFAULT;
172 }
173 *value = __tpci200_read16(addr->address, offset);
174
175 return 0;
176}
177
178static int tpci200_read32(struct ipack_device *dev, int space,
179 unsigned long offset, unsigned int *value)
180{
181 struct ipack_addr_space *addr;
182 struct tpci200_board *tpci200;
183
184 tpci200 = check_slot(dev);
185 if (tpci200 == NULL)
186 return -EINVAL;
187
188 addr = get_slot_address_space(dev, space);
189 if (addr == NULL)
190 return -EINVAL;
191
192 if ((offset+4) >= addr->size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200193 dev_err(&dev->dev, "Error, slot space offset error !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200194 return -EFAULT;
195 }
196
197 *value = __tpci200_read32(addr->address, offset);
198
199 return 0;
200}
201
202static int tpci200_write8(struct ipack_device *dev, int space,
203 unsigned long offset, unsigned char value)
204{
205 struct ipack_addr_space *addr;
206 struct tpci200_board *tpci200;
207
208 tpci200 = check_slot(dev);
209 if (tpci200 == NULL)
210 return -EINVAL;
211
212 addr = get_slot_address_space(dev, space);
213 if (addr == NULL)
214 return -EINVAL;
215
216 if (offset >= addr->size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200217 dev_err(&dev->dev, "Error, slot space offset error !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200218 return -EFAULT;
219 }
220
221 __tpci200_write8(value, addr->address, offset);
222
223 return 0;
224}
225
226static int tpci200_write16(struct ipack_device *dev, int space,
227 unsigned long offset, unsigned short value)
228{
229 struct ipack_addr_space *addr;
230 struct tpci200_board *tpci200;
231
232 tpci200 = check_slot(dev);
233 if (tpci200 == NULL)
234 return -EINVAL;
235
236 addr = get_slot_address_space(dev, space);
237 if (addr == NULL)
238 return -EINVAL;
239
240 if ((offset+2) >= addr->size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200241 dev_err(&dev->dev, "Error, slot space offset error !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200242 return -EFAULT;
243 }
244
245 __tpci200_write16(value, addr->address, offset);
246
247 return 0;
248}
249
250static int tpci200_write32(struct ipack_device *dev, int space,
251 unsigned long offset, unsigned int value)
252{
253 struct ipack_addr_space *addr;
254 struct tpci200_board *tpci200;
255
256 tpci200 = check_slot(dev);
257 if (tpci200 == NULL)
258 return -EINVAL;
259
260 addr = get_slot_address_space(dev, space);
261 if (addr == NULL)
262 return -EINVAL;
263
264 if ((offset+4) >= addr->size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200265 dev_err(&dev->dev, "Error, slot space offset error !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200266 return -EFAULT;
267 }
268
269 __tpci200_write32(value, addr->address, offset);
270
271 return 0;
272}
273
274static void tpci200_unregister(struct tpci200_board *tpci200)
275{
276 int i;
277
278 free_irq(tpci200->info->pdev->irq, (void *) tpci200);
279
280 pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
281 pci_iounmap(tpci200->info->pdev, tpci200->info->ioidint_space);
282 pci_iounmap(tpci200->info->pdev, tpci200->info->mem8_space);
283
284 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
285 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
286 pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
287
288 pci_disable_device(tpci200->info->pdev);
289 pci_dev_put(tpci200->info->pdev);
290
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200291 for (i = 0; i < TPCI200_NB_SLOT; i++) {
292 tpci200->slots[i].io_phys.address = NULL;
293 tpci200->slots[i].io_phys.size = 0;
294 tpci200->slots[i].id_phys.address = NULL;
295 tpci200->slots[i].id_phys.size = 0;
296 tpci200->slots[i].mem_phys.address = NULL;
297 tpci200->slots[i].mem_phys.size = 0;
298 }
299}
300
301static irqreturn_t tpci200_interrupt(int irq, void *dev_id)
302{
303 struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id;
304 int i;
305 unsigned long flags;
306 unsigned short status_reg, reg_value;
307 unsigned short unhandled_ints = 0;
308 irqreturn_t ret = IRQ_NONE;
309
310 spin_lock_irqsave(&tpci200->info->access_lock, flags);
311
312 /* Read status register */
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200313 status_reg = readw(tpci200->info->interface_regs +
314 TPCI200_STATUS_REG);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200315
316 if (status_reg & TPCI200_SLOT_INT_MASK) {
317 unhandled_ints = status_reg & TPCI200_SLOT_INT_MASK;
318 /* callback to the IRQ handler for the corresponding slot */
319 for (i = 0; i < TPCI200_NB_SLOT; i++) {
320 if ((tpci200->slots[i].irq != NULL) &&
321 (status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)))) {
322
323 ret = tpci200->slots[i].irq->handler(tpci200->slots[i].irq->arg);
324
325 /* Dummy reads */
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200326 readw(tpci200->slots[i].dev->io_space.address +
327 0xC0);
328 readw(tpci200->slots[i].dev->io_space.address +
329 0xC2);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200330
331 unhandled_ints &= ~(((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)));
332 }
333 }
334 }
335 /* Interrupt not handled are disabled */
336 if (unhandled_ints) {
337 for (i = 0; i < TPCI200_NB_SLOT; i++) {
338 if (unhandled_ints & ((TPCI200_INT0_EN | TPCI200_INT1_EN) << (2*i))) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200339 dev_info(&tpci200->slots[i].dev->dev,
340 "No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
341 tpci200->number, i);
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200342 reg_value = readw(
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200343 tpci200->info->interface_regs +
344 control_reg[i]);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200345 reg_value &=
346 ~(TPCI200_INT0_EN | TPCI200_INT1_EN);
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200347 writew(reg_value,
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200348 (tpci200->info->interface_regs +
349 control_reg[i]));
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200350 }
351 }
352 }
353
354 spin_unlock_irqrestore(&tpci200->info->access_lock, flags);
355 return ret;
356}
357
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200358static int tpci200_register(struct tpci200_board *tpci200)
359{
Samuel Iglesias Gonsalvez862fe872012-05-23 15:54:40 +0200360 int i;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200361 int res;
362 unsigned long ioidint_base;
363 unsigned long mem_base;
364 unsigned short slot_ctrl;
365
366 if (pci_enable_device(tpci200->info->pdev) < 0)
367 return -ENODEV;
368
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200369 /* Request IP interface register (Bar 2) */
370 res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR,
371 "Carrier IP interface registers");
372 if (res) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200373 dev_err(&tpci200->info->pdev->dev,
374 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
375 tpci200->info->pdev->bus->number,
376 tpci200->info->pdev->devfn);
Samuel Iglesias Gonsalvez862fe872012-05-23 15:54:40 +0200377 goto out_disable_pci;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200378 }
379
380 /* Request IO ID INT space (Bar 3) */
381 res = pci_request_region(tpci200->info->pdev,
382 TPCI200_IO_ID_INT_SPACES_BAR,
383 "Carrier IO ID INT space");
384 if (res) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200385 dev_err(&tpci200->info->pdev->dev,
386 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
387 tpci200->info->pdev->bus->number,
388 tpci200->info->pdev->devfn);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200389 goto out_release_ip_space;
390 }
391
392 /* Request MEM space (Bar 4) */
393 res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR,
394 "Carrier MEM space");
395 if (res) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200396 dev_err(&tpci200->info->pdev->dev,
397 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
398 tpci200->info->pdev->bus->number,
399 tpci200->info->pdev->devfn);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200400 goto out_release_ioid_int_space;
401 }
402
403 /* Map internal tpci200 driver user space */
404 tpci200->info->interface_regs =
405 ioremap(pci_resource_start(tpci200->info->pdev,
406 TPCI200_IP_INTERFACE_BAR),
407 TPCI200_IFACE_SIZE);
408 tpci200->info->ioidint_space =
409 ioremap(pci_resource_start(tpci200->info->pdev,
410 TPCI200_IO_ID_INT_SPACES_BAR),
411 TPCI200_IOIDINT_SIZE);
412 tpci200->info->mem8_space =
413 ioremap(pci_resource_start(tpci200->info->pdev,
414 TPCI200_MEM8_SPACE_BAR),
415 TPCI200_MEM8_SIZE);
416
417 spin_lock_init(&tpci200->info->access_lock);
418 ioidint_base = pci_resource_start(tpci200->info->pdev,
419 TPCI200_IO_ID_INT_SPACES_BAR);
420 mem_base = pci_resource_start(tpci200->info->pdev,
421 TPCI200_MEM8_SPACE_BAR);
422
423 /* Set the default parameters of the slot
424 * INT0 disabled, level sensitive
425 * INT1 disabled, level sensitive
426 * error interrupt disabled
427 * timeout interrupt disabled
428 * recover time disabled
429 * clock rate 8 MHz
430 */
431 slot_ctrl = 0;
432
433 /* Set all slot physical address space */
434 for (i = 0; i < TPCI200_NB_SLOT; i++) {
435 tpci200->slots[i].io_phys.address =
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200436 (void __iomem *)ioidint_base +
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200437 TPCI200_IO_SPACE_OFF + TPCI200_IO_SPACE_GAP*i;
438 tpci200->slots[i].io_phys.size = TPCI200_IO_SPACE_SIZE;
439
440 tpci200->slots[i].id_phys.address =
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200441 (void __iomem *)ioidint_base +
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200442 TPCI200_ID_SPACE_OFF + TPCI200_ID_SPACE_GAP*i;
443 tpci200->slots[i].id_phys.size = TPCI200_ID_SPACE_SIZE;
444
445 tpci200->slots[i].mem_phys.address =
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200446 (void __iomem *)mem_base + TPCI200_MEM8_GAP*i;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200447 tpci200->slots[i].mem_phys.size = TPCI200_MEM8_SIZE;
448
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200449 writew(slot_ctrl, (tpci200->info->interface_regs +
450 control_reg[i]));
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200451 }
452
453 res = request_irq(tpci200->info->pdev->irq,
454 tpci200_interrupt, IRQF_SHARED,
Samuel Iglesias Gonsalvezc63a7dd2012-05-18 11:10:07 +0200455 KBUILD_MODNAME, (void *) tpci200);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200456 if (res) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200457 dev_err(&tpci200->info->pdev->dev,
458 "(bn 0x%X, sn 0x%X) unable to register IRQ !",
459 tpci200->info->pdev->bus->number,
460 tpci200->info->pdev->devfn);
Samuel Iglesias Gonsalvez862fe872012-05-23 15:54:40 +0200461 goto out_release_ioid_int_space;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200462 }
463
464 return 0;
465
466out_release_ioid_int_space:
467 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
468out_release_ip_space:
469 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200470out_disable_pci:
471 pci_disable_device(tpci200->info->pdev);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200472 return res;
473}
474
475static int __tpci200_request_irq(struct tpci200_board *tpci200,
476 struct ipack_device *dev)
477{
478 unsigned short slot_ctrl;
479
480 /* Set the default parameters of the slot
481 * INT0 enabled, level sensitive
482 * INT1 enabled, level sensitive
483 * error interrupt disabled
484 * timeout interrupt disabled
485 * recover time disabled
486 * clock rate 8 MHz
487 */
488 slot_ctrl = TPCI200_INT0_EN | TPCI200_INT1_EN;
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200489 writew(slot_ctrl, (tpci200->info->interface_regs +
490 control_reg[dev->slot]));
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200491
492 return 0;
493}
494
495static void __tpci200_free_irq(struct tpci200_board *tpci200,
496 struct ipack_device *dev)
497{
498 unsigned short slot_ctrl;
499
500 /* Set the default parameters of the slot
501 * INT0 disabled, level sensitive
502 * INT1 disabled, level sensitive
503 * error interrupt disabled
504 * timeout interrupt disabled
505 * recover time disabled
506 * clock rate 8 MHz
507 */
508 slot_ctrl = 0;
Samuel Iglesias Gonsalvezdd8a95e2012-05-18 11:10:08 +0200509 writew(slot_ctrl, (tpci200->info->interface_regs +
510 control_reg[dev->slot]));
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200511}
512
513static int tpci200_free_irq(struct ipack_device *dev)
514{
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200515 struct slot_irq *slot_irq;
516 struct tpci200_board *tpci200;
517
518 tpci200 = check_slot(dev);
Miguel Gómez7d3da092012-06-07 11:10:17 +0200519 if (tpci200 == NULL)
520 return -EINVAL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200521
Miguel Gómez7d3da092012-06-07 11:10:17 +0200522 if (mutex_lock_interruptible(&tpci200->mutex))
523 return -ERESTARTSYS;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200524
525 if (tpci200->slots[dev->slot].irq == NULL) {
Miguel Gómez7d3da092012-06-07 11:10:17 +0200526 mutex_unlock(&tpci200->mutex);
527 return -EINVAL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200528 }
529
530 __tpci200_free_irq(tpci200, dev);
531 slot_irq = tpci200->slots[dev->slot].irq;
532 tpci200->slots[dev->slot].irq = NULL;
533 kfree(slot_irq);
534
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200535 mutex_unlock(&tpci200->mutex);
Miguel Gómez7d3da092012-06-07 11:10:17 +0200536 return 0;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200537}
538
539static int tpci200_slot_unmap_space(struct ipack_device *dev, int space)
540{
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200541 struct ipack_addr_space *virt_addr_space;
542 struct tpci200_board *tpci200;
543
544 tpci200 = check_slot(dev);
Samuel Iglesias Gonsalvez38f1c532012-05-23 15:54:42 +0200545 if (tpci200 == NULL)
546 return -EINVAL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200547
Samuel Iglesias Gonsalvez38f1c532012-05-23 15:54:42 +0200548 if (mutex_lock_interruptible(&tpci200->mutex))
549 return -ERESTARTSYS;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200550
551 switch (space) {
552 case IPACK_IO_SPACE:
553 if (dev->io_space.address == NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200554 dev_info(&dev->dev,
555 "Slot [%d:%d] IO space not mapped !\n",
556 dev->bus_nr, dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200557 goto out_unlock;
558 }
559 virt_addr_space = &dev->io_space;
560 break;
561 case IPACK_ID_SPACE:
562 if (dev->id_space.address == NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200563 dev_info(&dev->dev,
564 "Slot [%d:%d] ID space not mapped !\n",
565 dev->bus_nr, dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200566 goto out_unlock;
567 }
568 virt_addr_space = &dev->id_space;
569 break;
570 case IPACK_MEM_SPACE:
571 if (dev->mem_space.address == NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200572 dev_info(&dev->dev,
573 "Slot [%d:%d] MEM space not mapped !\n",
574 dev->bus_nr, dev->slot);
Samuel Iglesias Gonsalveze149ef82012-05-23 15:54:45 +0200575 goto out_unlock;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200576 }
577 virt_addr_space = &dev->mem_space;
578 break;
579 default:
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200580 dev_err(&dev->dev,
581 "Slot [%d:%d] space number %d doesn't exist !\n",
582 dev->bus_nr, dev->slot, space);
Samuel Iglesias Gonsalvez38f1c532012-05-23 15:54:42 +0200583 mutex_unlock(&tpci200->mutex);
584 return -EINVAL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200585 }
586
587 iounmap(virt_addr_space->address);
588
589 virt_addr_space->address = NULL;
590 virt_addr_space->size = 0;
591out_unlock:
592 mutex_unlock(&tpci200->mutex);
Samuel Iglesias Gonsalvez38f1c532012-05-23 15:54:42 +0200593 return 0;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200594}
595
596static int tpci200_slot_unregister(struct ipack_device *dev)
597{
598 struct tpci200_board *tpci200;
599
600 if (dev == NULL)
601 return -ENODEV;
602
603 tpci200 = check_slot(dev);
604 if (tpci200 == NULL)
605 return -EINVAL;
606
607 tpci200_free_irq(dev);
608
609 if (mutex_lock_interruptible(&tpci200->mutex))
610 return -ERESTARTSYS;
611
612 ipack_device_unregister(dev);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200613 tpci200->slots[dev->slot].dev = NULL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200614 mutex_unlock(&tpci200->mutex);
615
616 return 0;
617}
618
619static int tpci200_slot_map_space(struct ipack_device *dev,
620 unsigned int memory_size, int space)
621{
Samuel Iglesias Gonsalvez6ad0c482012-05-23 15:54:43 +0200622 int res = 0;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200623 unsigned int size_to_map;
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200624 void __iomem *phys_address;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200625 struct ipack_addr_space *virt_addr_space;
626 struct tpci200_board *tpci200;
627
628 tpci200 = check_slot(dev);
Miguel Gómez5e4be522012-06-07 10:24:51 +0200629 if (tpci200 == NULL)
630 return -EINVAL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200631
Miguel Gómez5e4be522012-06-07 10:24:51 +0200632 if (mutex_lock_interruptible(&tpci200->mutex))
633 return -ERESTARTSYS;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200634
635 switch (space) {
636 case IPACK_IO_SPACE:
637 if (dev->io_space.address != NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200638 dev_err(&dev->dev,
639 "Slot [%d:%d] IO space already mapped !\n",
640 tpci200->number, dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200641 res = -EINVAL;
642 goto out_unlock;
643 }
644 virt_addr_space = &dev->io_space;
645
646 phys_address = tpci200->slots[dev->slot].io_phys.address;
647 size_to_map = tpci200->slots[dev->slot].io_phys.size;
648 break;
649 case IPACK_ID_SPACE:
650 if (dev->id_space.address != NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200651 dev_err(&dev->dev,
652 "Slot [%d:%d] ID space already mapped !\n",
653 tpci200->number, dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200654 res = -EINVAL;
655 goto out_unlock;
656 }
657 virt_addr_space = &dev->id_space;
658
659 phys_address = tpci200->slots[dev->slot].id_phys.address;
660 size_to_map = tpci200->slots[dev->slot].id_phys.size;
661 break;
662 case IPACK_MEM_SPACE:
663 if (dev->mem_space.address != NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200664 dev_err(&dev->dev,
665 "Slot [%d:%d] MEM space already mapped !\n",
666 tpci200->number, dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200667 res = -EINVAL;
668 goto out_unlock;
669 }
670 virt_addr_space = &dev->mem_space;
671
672 if (memory_size > tpci200->slots[dev->slot].mem_phys.size) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200673 dev_err(&dev->dev,
674 "Slot [%d:%d] request is 0x%X memory, only 0x%X available !\n",
675 dev->bus_nr, dev->slot, memory_size,
676 tpci200->slots[dev->slot].mem_phys.size);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200677 res = -EINVAL;
678 goto out_unlock;
679 }
680
681 phys_address = tpci200->slots[dev->slot].mem_phys.address;
682 size_to_map = memory_size;
683 break;
684 default:
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200685 dev_err(&dev->dev, "Slot [%d:%d] space %d doesn't exist !\n",
686 tpci200->number, dev->slot, space);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200687 res = -EINVAL;
688 goto out_unlock;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200689 }
690
691 virt_addr_space->size = size_to_map;
692 virt_addr_space->address =
693 ioremap((unsigned long)phys_address, size_to_map);
694
695out_unlock:
696 mutex_unlock(&tpci200->mutex);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200697 return res;
698}
699
700static int tpci200_request_irq(struct ipack_device *dev, int vector,
701 int (*handler)(void *), void *arg)
702{
703 int res;
704 struct slot_irq *slot_irq;
705 struct tpci200_board *tpci200;
706
707 tpci200 = check_slot(dev);
Miguel Gómezf7986a92012-06-07 10:24:53 +0200708 if (tpci200 == NULL)
709 return -EINVAL;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200710
Miguel Gómezf7986a92012-06-07 10:24:53 +0200711 if (mutex_lock_interruptible(&tpci200->mutex))
712 return -ERESTARTSYS;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200713
714 if (tpci200->slots[dev->slot].irq != NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200715 dev_err(&dev->dev,
716 "Slot [%d:%d] IRQ already registered !\n", dev->bus_nr,
717 dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200718 res = -EINVAL;
719 goto out_unlock;
720 }
721
722 slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL);
723 if (slot_irq == NULL) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200724 dev_err(&dev->dev,
725 "Slot [%d:%d] unable to allocate memory for IRQ !\n",
726 dev->bus_nr, dev->slot);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200727 res = -ENOMEM;
728 goto out_unlock;
729 }
730
731 /*
732 * WARNING: Setup Interrupt Vector in the IndustryPack device
733 * before an IRQ request.
734 * Read the User Manual of your IndustryPack device to know
735 * where to write the vector in memory.
736 */
737 slot_irq->vector = vector;
738 slot_irq->handler = handler;
739 slot_irq->arg = arg;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200740
741 tpci200->slots[dev->slot].irq = slot_irq;
742 res = __tpci200_request_irq(tpci200, dev);
743
744out_unlock:
745 mutex_unlock(&tpci200->mutex);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200746 return res;
747}
748
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200749static void tpci200_uninstall(struct tpci200_board *tpci200)
750{
751 int i;
752
753 for (i = 0; i < TPCI200_NB_SLOT; i++)
Samuel Iglesias Gonsalvez20cf7002012-05-25 10:03:05 +0200754 tpci200_slot_unregister(tpci200->slots[i].dev);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200755
756 tpci200_unregister(tpci200);
757 kfree(tpci200->slots);
758}
759
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +0200760static struct ipack_bus_ops tpci200_bus_ops = {
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200761 .map_space = tpci200_slot_map_space,
762 .unmap_space = tpci200_slot_unmap_space,
763 .request_irq = tpci200_request_irq,
764 .free_irq = tpci200_free_irq,
765 .read8 = tpci200_read8,
766 .read16 = tpci200_read16,
767 .read32 = tpci200_read32,
768 .write8 = tpci200_write8,
769 .write16 = tpci200_write16,
770 .write32 = tpci200_write32,
771 .remove_device = tpci200_slot_unregister,
772};
773
774static int tpci200_install(struct tpci200_board *tpci200)
775{
776 int res;
777
778 tpci200->slots = kzalloc(
779 TPCI200_NB_SLOT * sizeof(struct tpci200_slot), GFP_KERNEL);
Miguel Gómezec0ceb92012-06-07 10:24:54 +0200780 if (tpci200->slots == NULL)
781 return -ENOMEM;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200782
783 res = tpci200_register(tpci200);
Miguel Gómezec0ceb92012-06-07 10:24:54 +0200784 if (res) {
785 kfree(tpci200->slots);
786 tpci200->slots = NULL;
787 return res;
788 }
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200789
790 mutex_init(&tpci200->mutex);
791 return 0;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200792}
793
794static int tpci200_pciprobe(struct pci_dev *pdev,
795 const struct pci_device_id *id)
796{
Samuel Iglesias Gonsalvez862fe872012-05-23 15:54:40 +0200797 int ret, i;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200798 struct tpci200_board *tpci200;
799
800 tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL);
801 if (!tpci200)
802 return -ENOMEM;
803
804 tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL);
805 if (!tpci200->info) {
806 kfree(tpci200);
807 return -ENOMEM;
808 }
809
810 /* Save struct pci_dev pointer */
811 tpci200->info->pdev = pdev;
812 tpci200->info->id_table = (struct pci_device_id *)id;
813
814 /* register the device and initialize it */
815 ret = tpci200_install(tpci200);
816 if (ret) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200817 dev_err(&pdev->dev, "Error during tpci200 install !\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200818 kfree(tpci200->info);
819 kfree(tpci200);
820 return -ENODEV;
821 }
822
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200823 /* Register the carrier in the industry pack bus driver */
824 tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev,
825 TPCI200_NB_SLOT,
826 &tpci200_bus_ops);
827 if (!tpci200->info->ipack_bus) {
Samuel Iglesias Gonsalveza23d3202012-05-25 13:08:12 +0200828 dev_err(&pdev->dev,
829 "error registering the carrier on ipack driver\n");
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200830 tpci200_uninstall(tpci200);
831 kfree(tpci200->info);
832 kfree(tpci200);
833 return -EFAULT;
834 }
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200835
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200836 /* save the bus number given by ipack to logging purpose */
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200837 tpci200->number = tpci200->info->ipack_bus->bus_nr;
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200838 dev_set_drvdata(&pdev->dev, tpci200);
839 /* add the registered device in an internal linked list */
840 list_add_tail(&tpci200->list, &tpci200_list);
Samuel Iglesias Gonsalvez862fe872012-05-23 15:54:40 +0200841
842 /*
843 * Give the same IRQ number as the slot number.
844 * The TPCI200 has assigned his own two IRQ by PCI bus driver
845 */
846 for (i = 0; i < TPCI200_NB_SLOT; i++)
847 tpci200->slots[i].dev =
848 ipack_device_register(tpci200->info->ipack_bus, i, i);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200849 return ret;
850}
851
852static void __tpci200_pci_remove(struct tpci200_board *tpci200)
853{
854 tpci200_uninstall(tpci200);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200855 list_del(&tpci200->list);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200856 ipack_bus_unregister(tpci200->info->ipack_bus);
857 kfree(tpci200->info);
Samuel Iglesias Gonsalvez0eeca142012-05-09 15:27:20 +0200858 kfree(tpci200);
859}
860
861static void __devexit tpci200_pci_remove(struct pci_dev *dev)
862{
863 struct tpci200_board *tpci200, *next;
864
865 /* Search the registered device to uninstall it */
866 list_for_each_entry_safe(tpci200, next, &tpci200_list, list) {
867 if (tpci200->info->pdev == dev) {
868 __tpci200_pci_remove(tpci200);
869 break;
870 }
871 }
872}
873
874static struct pci_device_id tpci200_idtable[2] = {
875 { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID,
876 TPCI200_SUBDEVICE_ID },
877 { 0, },
878};
879
880static struct pci_driver tpci200_pci_drv = {
881 .name = "tpci200",
882 .id_table = tpci200_idtable,
883 .probe = tpci200_pciprobe,
884 .remove = __devexit_p(tpci200_pci_remove),
885};
886
887static int __init tpci200_drvr_init_module(void)
888{
889 return pci_register_driver(&tpci200_pci_drv);
890}
891
892static void __exit tpci200_drvr_exit_module(void)
893{
894 struct tpci200_board *tpci200, *next;
895
896 list_for_each_entry_safe(tpci200, next, &tpci200_list, list)
897 __tpci200_pci_remove(tpci200);
898
899 pci_unregister_driver(&tpci200_pci_drv);
900}
901
902MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
903MODULE_LICENSE("GPL");
904module_init(tpci200_drvr_init_module);
905module_exit(tpci200_drvr_exit_module);