^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * tpci200.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * driver for the TEWS TPCI-200 device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2009-2012 CERN (www.cern.ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Nicolas Serafini, EIC2 SA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "tpci200.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static const u16 tpci200_status_timeout[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) TPCI200_A_TIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) TPCI200_B_TIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) TPCI200_C_TIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) TPCI200_D_TIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const u16 tpci200_status_error[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) TPCI200_A_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) TPCI200_B_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) TPCI200_C_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) TPCI200_D_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static const size_t tpci200_space_size[IPACK_SPACE_COUNT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) [IPACK_IO_SPACE] = TPCI200_IO_SPACE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) [IPACK_ID_SPACE] = TPCI200_ID_SPACE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) [IPACK_INT_SPACE] = TPCI200_INT_SPACE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) [IPACK_MEM8_SPACE] = TPCI200_MEM8_SPACE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) [IPACK_MEM16_SPACE] = TPCI200_MEM16_SPACE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const size_t tpci200_space_interval[IPACK_SPACE_COUNT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) [IPACK_IO_SPACE] = TPCI200_IO_SPACE_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) [IPACK_ID_SPACE] = TPCI200_ID_SPACE_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) [IPACK_INT_SPACE] = TPCI200_INT_SPACE_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) [IPACK_MEM8_SPACE] = TPCI200_MEM8_SPACE_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) [IPACK_MEM16_SPACE] = TPCI200_MEM16_SPACE_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static struct tpci200_board *check_slot(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct tpci200_board *tpci200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) tpci200 = dev_get_drvdata(dev->bus->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (tpci200 == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) dev_info(&dev->dev, "carrier board not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (dev->slot >= TPCI200_NB_SLOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) dev_info(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) dev->bus->bus_nr, dev->slot, TPCI200_NB_SLOT-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return tpci200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void tpci200_clear_mask(struct tpci200_board *tpci200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __le16 __iomem *addr, u16 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) spin_lock_irqsave(&tpci200->regs_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) iowrite16(ioread16(addr) & (~mask), addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) spin_unlock_irqrestore(&tpci200->regs_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static void tpci200_set_mask(struct tpci200_board *tpci200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) __le16 __iomem *addr, u16 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) spin_lock_irqsave(&tpci200->regs_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) iowrite16(ioread16(addr) | mask, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spin_unlock_irqrestore(&tpci200->regs_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void tpci200_unregister(struct tpci200_board *tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) free_irq(tpci200->info->pdev->irq, (void *) tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) pci_release_region(tpci200->info->pdev, TPCI200_MEM16_SPACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pci_disable_device(tpci200->info->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void tpci200_enable_irq(struct tpci200_board *tpci200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int islot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) tpci200_set_mask(tpci200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) &tpci200->info->interface_regs->control[islot],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) TPCI200_INT0_EN | TPCI200_INT1_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static void tpci200_disable_irq(struct tpci200_board *tpci200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int islot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) tpci200_clear_mask(tpci200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) &tpci200->info->interface_regs->control[islot],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) TPCI200_INT0_EN | TPCI200_INT1_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static irqreturn_t tpci200_slot_irq(struct slot_irq *slot_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) irqreturn_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!slot_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ret = slot_irq->handler(slot_irq->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static irqreturn_t tpci200_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct slot_irq *slot_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) irqreturn_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) u16 status_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Read status register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) status_reg = ioread16(&tpci200->info->interface_regs->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Did we cause the interrupt? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (!(status_reg & TPCI200_SLOT_INT_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* callback to the IRQ handler for the corresponding slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) for (i = 0; i < TPCI200_NB_SLOT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!(status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2 * i))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) slot_irq = rcu_dereference(tpci200->slots[i].irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = tpci200_slot_irq(slot_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ret == -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) dev_info(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) "No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) tpci200->number, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tpci200_disable_irq(tpci200, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int tpci200_free_irq(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct slot_irq *slot_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct tpci200_board *tpci200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (tpci200 == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (mutex_lock_interruptible(&tpci200->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (tpci200->slots[dev->slot].irq == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) mutex_unlock(&tpci200->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) tpci200_disable_irq(tpci200, dev->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) slot_irq = tpci200->slots[dev->slot].irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* uninstall handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) RCU_INIT_POINTER(tpci200->slots[dev->slot].irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) kfree(slot_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_unlock(&tpci200->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int tpci200_request_irq(struct ipack_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) irqreturn_t (*handler)(void *), void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct slot_irq *slot_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct tpci200_board *tpci200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (tpci200 == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (mutex_lock_interruptible(&tpci200->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (tpci200->slots[dev->slot].irq != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) "Slot [%d:%d] IRQ already registered !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev->bus->bus_nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dev->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) res = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (slot_irq == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) "Slot [%d:%d] unable to allocate memory for IRQ !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev->bus->bus_nr, dev->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) res = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * WARNING: Setup Interrupt Vector in the IndustryPack device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * before an IRQ request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * Read the User Manual of your IndustryPack device to know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * where to write the vector in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) slot_irq->handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) slot_irq->arg = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) slot_irq->holder = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) rcu_assign_pointer(tpci200->slots[dev->slot].irq, slot_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) tpci200_enable_irq(tpci200, dev->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) mutex_unlock(&tpci200->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int tpci200_register(struct tpci200_board *tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) phys_addr_t ioidint_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) unsigned short slot_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (pci_enable_device(tpci200->info->pdev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* Request IP interface register (Bar 2) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) "Carrier IP interface registers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) dev_err(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) tpci200->info->pdev->bus->number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) tpci200->info->pdev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) goto err_disable_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Request IO ID INT space (Bar 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) res = pci_request_region(tpci200->info->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) TPCI200_IO_ID_INT_SPACES_BAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) "Carrier IO ID INT space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dev_err(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) tpci200->info->pdev->bus->number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) tpci200->info->pdev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto err_ip_interface_bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* Request MEM8 space (Bar 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) "Carrier MEM8 space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) dev_err(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 5!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) tpci200->info->pdev->bus->number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) tpci200->info->pdev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto err_io_id_int_spaces_bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* Request MEM16 space (Bar 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) res = pci_request_region(tpci200->info->pdev, TPCI200_MEM16_SPACE_BAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) "Carrier MEM16 space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) dev_err(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) tpci200->info->pdev->bus->number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) tpci200->info->pdev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto err_mem8_space_bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /* Map internal tpci200 driver user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) tpci200->info->interface_regs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ioremap(pci_resource_start(tpci200->info->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) TPCI200_IP_INTERFACE_BAR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) TPCI200_IFACE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!tpci200->info->interface_regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dev_err(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) "(bn 0x%X, sn 0x%X) failed to map driver user space!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) tpci200->info->pdev->bus->number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) tpci200->info->pdev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) res = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) goto err_mem16_space_bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* Initialize lock that protects interface_regs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) spin_lock_init(&tpci200->regs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ioidint_base = pci_resource_start(tpci200->info->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) TPCI200_IO_ID_INT_SPACES_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) tpci200->mod_mem[IPACK_IO_SPACE] = ioidint_base + TPCI200_IO_SPACE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) tpci200->mod_mem[IPACK_ID_SPACE] = ioidint_base + TPCI200_ID_SPACE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) tpci200->mod_mem[IPACK_INT_SPACE] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ioidint_base + TPCI200_INT_SPACE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) tpci200->mod_mem[IPACK_MEM8_SPACE] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) pci_resource_start(tpci200->info->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) TPCI200_MEM8_SPACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) tpci200->mod_mem[IPACK_MEM16_SPACE] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) pci_resource_start(tpci200->info->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) TPCI200_MEM16_SPACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* Set the default parameters of the slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * INT0 disabled, level sensitive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * INT1 disabled, level sensitive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * error interrupt disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * timeout interrupt disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * recover time disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * clock rate 8 MHz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) slot_ctrl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) for (i = 0; i < TPCI200_NB_SLOT; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) writew(slot_ctrl, &tpci200->info->interface_regs->control[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) res = request_irq(tpci200->info->pdev->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) tpci200_interrupt, IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) KBUILD_MODNAME, (void *) tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) dev_err(&tpci200->info->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) "(bn 0x%X, sn 0x%X) unable to register IRQ !",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) tpci200->info->pdev->bus->number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) tpci200->info->pdev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) goto err_interface_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) err_interface_regs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) err_mem16_space_bar:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) pci_release_region(tpci200->info->pdev, TPCI200_MEM16_SPACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) err_mem8_space_bar:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) err_io_id_int_spaces_bar:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) err_ip_interface_bar:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) err_disable_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) pci_disable_device(tpci200->info->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int tpci200_get_clockrate(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct tpci200_board *tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) __le16 __iomem *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) addr = &tpci200->info->interface_regs->control[dev->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return (ioread16(addr) & TPCI200_CLK32) ? 32 : 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int tpci200_set_clockrate(struct ipack_device *dev, int mherz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct tpci200_board *tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) __le16 __iomem *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) addr = &tpci200->info->interface_regs->control[dev->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) switch (mherz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) tpci200_clear_mask(tpci200, addr, TPCI200_CLK32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) case 32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) tpci200_set_mask(tpci200, addr, TPCI200_CLK32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static int tpci200_get_error(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct tpci200_board *tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) __le16 __iomem *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) u16 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (!tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) addr = &tpci200->info->interface_regs->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) mask = tpci200_status_error[dev->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return (ioread16(addr) & mask) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static int tpci200_get_timeout(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct tpci200_board *tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) __le16 __iomem *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) u16 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (!tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) addr = &tpci200->info->interface_regs->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) mask = tpci200_status_timeout[dev->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return (ioread16(addr) & mask) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static int tpci200_reset_timeout(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct tpci200_board *tpci200 = check_slot(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) __le16 __iomem *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) u16 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (!tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) addr = &tpci200->info->interface_regs->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) mask = tpci200_status_timeout[dev->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) iowrite16(mask, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void tpci200_uninstall(struct tpci200_board *tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) tpci200_unregister(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) kfree(tpci200->slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static const struct ipack_bus_ops tpci200_bus_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) .request_irq = tpci200_request_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) .free_irq = tpci200_free_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) .get_clockrate = tpci200_get_clockrate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) .set_clockrate = tpci200_set_clockrate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) .get_error = tpci200_get_error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) .get_timeout = tpci200_get_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) .reset_timeout = tpci200_reset_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static int tpci200_install(struct tpci200_board *tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) tpci200->slots = kcalloc(TPCI200_NB_SLOT, sizeof(struct tpci200_slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (tpci200->slots == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) res = tpci200_register(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) kfree(tpci200->slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) tpci200->slots = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) mutex_init(&tpci200->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static void tpci200_release_device(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int tpci200_create_device(struct tpci200_board *tpci200, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) enum ipack_space space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct ipack_device *dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) kzalloc(sizeof(struct ipack_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) dev->slot = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) dev->bus = tpci200->info->ipack_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) dev->release = tpci200_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) for (space = 0; space < IPACK_SPACE_COUNT; space++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) dev->region[space].start =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) tpci200->mod_mem[space]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) + tpci200_space_interval[space] * i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) dev->region[space].size = tpci200_space_size[space];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) ret = ipack_device_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) ipack_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ret = ipack_device_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ipack_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static int tpci200_pci_probe(struct pci_dev *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) const struct pci_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct tpci200_board *tpci200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) u32 reg32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (!tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (!tpci200->info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) goto err_tpci200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) pci_dev_get(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* Obtain a mapping of the carrier's PCI configuration registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) ret = pci_request_region(pdev, TPCI200_CFG_MEM_BAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) KBUILD_MODNAME " Configuration Memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) dev_err(&pdev->dev, "Failed to allocate PCI Configuration Memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) goto err_tpci200_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) tpci200->info->cfg_regs = ioremap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) pci_resource_start(pdev, TPCI200_CFG_MEM_BAR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) pci_resource_len(pdev, TPCI200_CFG_MEM_BAR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (!tpci200->info->cfg_regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dev_err(&pdev->dev, "Failed to map PCI Configuration Memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) goto err_request_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /* Disable byte swapping for 16 bit IP module access. This will ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * that the Industrypack big endian byte order is preserved by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * carrier. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) reg32 = ioread32(tpci200->info->cfg_regs + LAS1_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) reg32 |= 1 << LAS_BIT_BIGENDIAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) iowrite32(reg32, tpci200->info->cfg_regs + LAS1_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) reg32 = ioread32(tpci200->info->cfg_regs + LAS2_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) reg32 |= 1 << LAS_BIT_BIGENDIAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) iowrite32(reg32, tpci200->info->cfg_regs + LAS2_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) /* Save struct pci_dev pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) tpci200->info->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) tpci200->info->id_table = (struct pci_device_id *)id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /* register the device and initialize it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) ret = tpci200_install(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) dev_err(&pdev->dev, "error during tpci200 install\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto err_cfg_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* Register the carrier in the industry pack bus driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) TPCI200_NB_SLOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) &tpci200_bus_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (!tpci200->info->ipack_bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) "error registering the carrier on ipack driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto err_tpci200_install;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /* save the bus number given by ipack to logging purpose */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) tpci200->number = tpci200->info->ipack_bus->bus_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) dev_set_drvdata(&pdev->dev, tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) for (i = 0; i < TPCI200_NB_SLOT; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) tpci200_create_device(tpci200, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) err_tpci200_install:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) tpci200_uninstall(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) err_cfg_regs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) pci_iounmap(tpci200->info->pdev, tpci200->info->cfg_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) err_request_region:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) pci_release_region(pdev, TPCI200_CFG_MEM_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) err_tpci200_info:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) kfree(tpci200->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) pci_dev_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) err_tpci200:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) kfree(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) static void __tpci200_pci_remove(struct tpci200_board *tpci200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) ipack_bus_unregister(tpci200->info->ipack_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) tpci200_uninstall(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) pci_iounmap(tpci200->info->pdev, tpci200->info->cfg_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) pci_release_region(tpci200->info->pdev, TPCI200_CFG_MEM_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) pci_dev_put(tpci200->info->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) kfree(tpci200->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) kfree(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static void tpci200_pci_remove(struct pci_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) struct tpci200_board *tpci200 = pci_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) __tpci200_pci_remove(tpci200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) static const struct pci_device_id tpci200_idtable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) TPCI200_SUBDEVICE_ID },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) { 0, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) MODULE_DEVICE_TABLE(pci, tpci200_idtable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) static struct pci_driver tpci200_pci_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) .name = "tpci200",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) .id_table = tpci200_idtable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) .probe = tpci200_pci_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) .remove = tpci200_pci_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) module_pci_driver(tpci200_pci_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) MODULE_LICENSE("GPL");