Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * tifm_7xx1.c - TI FlashMedia driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/tifm.h> |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 13 | |
| 14 | #define DRIVER_NAME "tifm_7xx1" |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 15 | #define DRIVER_VERSION "0.8" |
| 16 | |
| 17 | #define TIFM_IRQ_ENABLE 0x80000000 |
| 18 | #define TIFM_IRQ_SOCKMASK(x) (x) |
| 19 | #define TIFM_IRQ_CARDMASK(x) ((x) << 8) |
| 20 | #define TIFM_IRQ_FIFOMASK(x) ((x) << 16) |
| 21 | #define TIFM_IRQ_SETALL 0xffffffff |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 22 | |
Alex Dubov | 6113ed7 | 2007-04-12 16:59:17 +1000 | [diff] [blame] | 23 | static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm, |
| 24 | struct tifm_dev *sock) |
| 25 | { |
| 26 | } |
| 27 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 28 | static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock) |
| 29 | { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 30 | unsigned long flags; |
| 31 | |
| 32 | spin_lock_irqsave(&fm->lock, flags); |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 33 | fm->socket_change_set |= 1 << sock->socket_id; |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 34 | tifm_queue_work(&fm->media_switcher); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 35 | spin_unlock_irqrestore(&fm->lock, flags); |
| 36 | } |
| 37 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 38 | static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 39 | { |
| 40 | struct tifm_adapter *fm = dev_id; |
Alex Dubov | 217334d | 2006-12-11 01:55:31 +1100 | [diff] [blame] | 41 | struct tifm_dev *sock; |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame^] | 42 | unsigned int irq_status, cnt; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 43 | |
| 44 | spin_lock(&fm->lock); |
| 45 | irq_status = readl(fm->addr + FM_INTERRUPT_STATUS); |
| 46 | if (irq_status == 0 || irq_status == (~0)) { |
| 47 | spin_unlock(&fm->lock); |
| 48 | return IRQ_NONE; |
| 49 | } |
| 50 | |
| 51 | if (irq_status & TIFM_IRQ_ENABLE) { |
| 52 | writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE); |
| 53 | |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 54 | for (cnt = 0; cnt < fm->num_sockets; cnt++) { |
Alex Dubov | 217334d | 2006-12-11 01:55:31 +1100 | [diff] [blame] | 55 | sock = fm->sockets[cnt]; |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 56 | if (sock) { |
| 57 | if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1)) |
| 58 | sock->data_event(sock); |
| 59 | if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1)) |
| 60 | sock->card_event(sock); |
| 61 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 62 | } |
Alex Dubov | 6412d92 | 2006-12-11 01:55:33 +1100 | [diff] [blame] | 63 | |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 64 | fm->socket_change_set |= irq_status |
| 65 | & ((1 << fm->num_sockets) - 1); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 66 | } |
| 67 | writel(irq_status, fm->addr + FM_INTERRUPT_STATUS); |
| 68 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 69 | if (fm->finish_me) |
| 70 | complete_all(fm->finish_me); |
| 71 | else if (!fm->socket_change_set) |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 72 | writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE); |
| 73 | else |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 74 | tifm_queue_work(&fm->media_switcher); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 75 | |
| 76 | spin_unlock(&fm->lock); |
| 77 | return IRQ_HANDLED; |
| 78 | } |
| 79 | |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 80 | static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 81 | { |
| 82 | unsigned int s_state; |
| 83 | int cnt; |
| 84 | |
| 85 | writel(0x0e00, sock_addr + SOCK_CONTROL); |
| 86 | |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 87 | for (cnt = 16; cnt <= 256; cnt <<= 1) { |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 88 | if (!(TIFM_SOCK_STATE_POWERED |
| 89 | & readl(sock_addr + SOCK_PRESENT_STATE))) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 90 | break; |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 91 | |
| 92 | msleep(cnt); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | s_state = readl(sock_addr + SOCK_PRESENT_STATE); |
| 96 | if (!(TIFM_SOCK_STATE_OCCUPIED & s_state)) |
Alex Dubov | e23f2b8 | 2007-04-12 16:59:14 +1000 | [diff] [blame] | 97 | return 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 98 | |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 99 | writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED, |
| 100 | sock_addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 101 | |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 102 | /* xd needs some extra time before power on */ |
| 103 | if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) |
| 104 | == TIFM_TYPE_XD) |
| 105 | msleep(40); |
| 106 | |
| 107 | writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL); |
| 108 | /* wait for power to stabilize */ |
| 109 | msleep(20); |
| 110 | for (cnt = 16; cnt <= 256; cnt <<= 1) { |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 111 | if ((TIFM_SOCK_STATE_POWERED |
| 112 | & readl(sock_addr + SOCK_PRESENT_STATE))) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 113 | break; |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 114 | |
| 115 | msleep(cnt); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alex Dubov | 342c0ec | 2007-04-12 16:59:16 +1000 | [diff] [blame] | 118 | writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED), |
| 119 | sock_addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 120 | |
| 121 | return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7; |
| 122 | } |
| 123 | |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 124 | inline static char __iomem * |
| 125 | tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 126 | { |
| 127 | return base_addr + ((sock_num + 1) << 10); |
| 128 | } |
| 129 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 130 | static void tifm_7xx1_switch_media(struct work_struct *work) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 131 | { |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 132 | struct tifm_adapter *fm = container_of(work, struct tifm_adapter, |
| 133 | media_switcher); |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame^] | 134 | struct tifm_dev *sock; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 135 | unsigned long flags; |
Alex Dubov | e23f2b8 | 2007-04-12 16:59:14 +1000 | [diff] [blame] | 136 | unsigned char media_id; |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame^] | 137 | unsigned int socket_change_set, cnt; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 138 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 139 | spin_lock_irqsave(&fm->lock, flags); |
| 140 | socket_change_set = fm->socket_change_set; |
| 141 | fm->socket_change_set = 0; |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 142 | |
Alex Dubov | 6113ed7 | 2007-04-12 16:59:17 +1000 | [diff] [blame] | 143 | dev_dbg(fm->cdev.dev, "checking media set %x\n", |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 144 | socket_change_set); |
Alex Dubov | 6412d92 | 2006-12-11 01:55:33 +1100 | [diff] [blame] | 145 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 146 | if (!socket_change_set) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 147 | spin_unlock_irqrestore(&fm->lock, flags); |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 148 | return; |
| 149 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 150 | |
Alex Dubov | 2428a8f | 2007-04-12 16:59:18 +1000 | [diff] [blame] | 151 | for (cnt = 0; cnt < fm->num_sockets; cnt++) { |
| 152 | if (!(socket_change_set & (1 << cnt))) |
| 153 | continue; |
| 154 | sock = fm->sockets[cnt]; |
| 155 | if (sock) { |
| 156 | printk(KERN_INFO |
| 157 | "%s : demand removing card from socket %u:%u\n", |
| 158 | fm->cdev.class_id, fm->id, cnt); |
| 159 | fm->sockets[cnt] = NULL; |
Alex Dubov | 6412d92 | 2006-12-11 01:55:33 +1100 | [diff] [blame] | 160 | spin_unlock_irqrestore(&fm->lock, flags); |
Alex Dubov | 2428a8f | 2007-04-12 16:59:18 +1000 | [diff] [blame] | 161 | device_unregister(&sock->dev); |
| 162 | spin_lock_irqsave(&fm->lock, flags); |
| 163 | writel(0x0e00, tifm_7xx1_sock_addr(fm->addr, cnt) |
| 164 | + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 165 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 166 | |
Alex Dubov | 2428a8f | 2007-04-12 16:59:18 +1000 | [diff] [blame] | 167 | spin_unlock_irqrestore(&fm->lock, flags); |
| 168 | |
| 169 | media_id = tifm_7xx1_toggle_sock_power( |
| 170 | tifm_7xx1_sock_addr(fm->addr, cnt)); |
| 171 | |
| 172 | // tifm_alloc_device will check if media_id is valid |
| 173 | sock = tifm_alloc_device(fm, cnt, media_id); |
| 174 | if (sock) { |
| 175 | sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt); |
| 176 | |
| 177 | if (!device_register(&sock->dev)) { |
| 178 | spin_lock_irqsave(&fm->lock, flags); |
| 179 | if (!fm->sockets[cnt]) { |
| 180 | fm->sockets[cnt] = sock; |
| 181 | sock = NULL; |
| 182 | } |
| 183 | spin_unlock_irqrestore(&fm->lock, flags); |
| 184 | } |
| 185 | if (sock) |
| 186 | tifm_free_device(&sock->dev); |
| 187 | } |
| 188 | spin_lock_irqsave(&fm->lock, flags); |
| 189 | } |
| 190 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 191 | writel(TIFM_IRQ_FIFOMASK(socket_change_set) |
| 192 | | TIFM_IRQ_CARDMASK(socket_change_set), |
| 193 | fm->addr + FM_CLEAR_INTERRUPT_ENABLE); |
| 194 | |
| 195 | writel(TIFM_IRQ_FIFOMASK(socket_change_set) |
| 196 | | TIFM_IRQ_CARDMASK(socket_change_set), |
| 197 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
| 198 | |
| 199 | writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE); |
| 200 | spin_unlock_irqrestore(&fm->lock, flags); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 203 | #ifdef CONFIG_PM |
| 204 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 205 | static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state) |
| 206 | { |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 207 | dev_dbg(&dev->dev, "suspending host\n"); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 208 | |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 209 | pci_save_state(dev); |
| 210 | pci_enable_wake(dev, pci_choose_state(dev, state), 0); |
| 211 | pci_disable_device(dev); |
| 212 | pci_set_power_state(dev, pci_choose_state(dev, state)); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int tifm_7xx1_resume(struct pci_dev *dev) |
| 217 | { |
| 218 | struct tifm_adapter *fm = pci_get_drvdata(dev); |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 219 | int rc; |
| 220 | unsigned int good_sockets = 0, bad_sockets = 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 221 | unsigned long flags; |
Alex Dubov | e23f2b8 | 2007-04-12 16:59:14 +1000 | [diff] [blame] | 222 | unsigned char new_ids[fm->num_sockets]; |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 223 | DECLARE_COMPLETION_ONSTACK(finish_resume); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 224 | |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 225 | pci_set_power_state(dev, PCI_D0); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 226 | pci_restore_state(dev); |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 227 | rc = pci_enable_device(dev); |
| 228 | if (rc) |
| 229 | return rc; |
| 230 | pci_set_master(dev); |
| 231 | |
| 232 | dev_dbg(&dev->dev, "resuming host\n"); |
| 233 | |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 234 | for (rc = 0; rc < fm->num_sockets; rc++) |
| 235 | new_ids[rc] = tifm_7xx1_toggle_sock_power( |
| 236 | tifm_7xx1_sock_addr(fm->addr, rc)); |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 237 | spin_lock_irqsave(&fm->lock, flags); |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 238 | for (rc = 0; rc < fm->num_sockets; rc++) { |
| 239 | if (fm->sockets[rc]) { |
| 240 | if (fm->sockets[rc]->type == new_ids[rc]) |
| 241 | good_sockets |= 1 << rc; |
| 242 | else |
| 243 | bad_sockets |= 1 << rc; |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1), |
| 248 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 249 | dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n", |
| 250 | good_sockets, bad_sockets); |
| 251 | |
| 252 | fm->socket_change_set = 0; |
| 253 | if (good_sockets) { |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 254 | fm->finish_me = &finish_resume; |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 255 | spin_unlock_irqrestore(&fm->lock, flags); |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 256 | rc = wait_for_completion_timeout(&finish_resume, HZ); |
| 257 | dev_dbg(&dev->dev, "wait returned %d\n", rc); |
| 258 | writel(TIFM_IRQ_FIFOMASK(good_sockets) |
| 259 | | TIFM_IRQ_CARDMASK(good_sockets), |
| 260 | fm->addr + FM_CLEAR_INTERRUPT_ENABLE); |
| 261 | writel(TIFM_IRQ_FIFOMASK(good_sockets) |
| 262 | | TIFM_IRQ_CARDMASK(good_sockets), |
| 263 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
| 264 | spin_lock_irqsave(&fm->lock, flags); |
| 265 | fm->finish_me = NULL; |
| 266 | fm->socket_change_set ^= good_sockets & fm->socket_change_set; |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 267 | } |
| 268 | |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 269 | fm->socket_change_set |= bad_sockets; |
| 270 | if (fm->socket_change_set) |
| 271 | tifm_queue_work(&fm->media_switcher); |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 272 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 273 | spin_unlock_irqrestore(&fm->lock, flags); |
Alex Dubov | 88de1b2 | 2007-04-12 16:59:19 +1000 | [diff] [blame] | 274 | writel(TIFM_IRQ_ENABLE, |
| 275 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
| 276 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 280 | #else |
| 281 | |
| 282 | #define tifm_7xx1_suspend NULL |
| 283 | #define tifm_7xx1_resume NULL |
| 284 | |
| 285 | #endif /* CONFIG_PM */ |
| 286 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 287 | static int tifm_7xx1_probe(struct pci_dev *dev, |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 288 | const struct pci_device_id *dev_id) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 289 | { |
| 290 | struct tifm_adapter *fm; |
| 291 | int pci_dev_busy = 0; |
| 292 | int rc; |
| 293 | |
| 294 | rc = pci_set_dma_mask(dev, DMA_32BIT_MASK); |
| 295 | if (rc) |
| 296 | return rc; |
| 297 | |
| 298 | rc = pci_enable_device(dev); |
| 299 | if (rc) |
| 300 | return rc; |
| 301 | |
| 302 | pci_set_master(dev); |
| 303 | |
| 304 | rc = pci_request_regions(dev, DRIVER_NAME); |
| 305 | if (rc) { |
| 306 | pci_dev_busy = 1; |
| 307 | goto err_out; |
| 308 | } |
| 309 | |
| 310 | pci_intx(dev, 1); |
| 311 | |
Alex Dubov | 6113ed7 | 2007-04-12 16:59:17 +1000 | [diff] [blame] | 312 | fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM |
| 313 | ? 4 : 2, &dev->dev); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 314 | if (!fm) { |
| 315 | rc = -ENOMEM; |
| 316 | goto err_out_int; |
| 317 | } |
| 318 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 319 | INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 320 | fm->eject = tifm_7xx1_eject; |
| 321 | pci_set_drvdata(dev, fm); |
| 322 | |
| 323 | fm->addr = ioremap(pci_resource_start(dev, 0), |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 324 | pci_resource_len(dev, 0)); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 325 | if (!fm->addr) |
| 326 | goto err_out_free; |
| 327 | |
Alex Dubov | 6113ed7 | 2007-04-12 16:59:17 +1000 | [diff] [blame] | 328 | rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 329 | if (rc) |
| 330 | goto err_out_unmap; |
| 331 | |
Alex Dubov | 3540af8ff | 2007-04-12 16:59:15 +1000 | [diff] [blame] | 332 | rc = tifm_add_adapter(fm); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 333 | if (rc) |
| 334 | goto err_out_irq; |
| 335 | |
Alex Dubov | 6412d92 | 2006-12-11 01:55:33 +1100 | [diff] [blame] | 336 | writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1), |
Alex Dubov | 8b40ada | 2006-12-11 01:55:36 +1100 | [diff] [blame] | 337 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 338 | return 0; |
| 339 | |
| 340 | err_out_irq: |
| 341 | free_irq(dev->irq, fm); |
| 342 | err_out_unmap: |
| 343 | iounmap(fm->addr); |
| 344 | err_out_free: |
| 345 | pci_set_drvdata(dev, NULL); |
| 346 | tifm_free_adapter(fm); |
| 347 | err_out_int: |
| 348 | pci_intx(dev, 0); |
| 349 | pci_release_regions(dev); |
| 350 | err_out: |
| 351 | if (!pci_dev_busy) |
| 352 | pci_disable_device(dev); |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | static void tifm_7xx1_remove(struct pci_dev *dev) |
| 357 | { |
| 358 | struct tifm_adapter *fm = pci_get_drvdata(dev); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 359 | |
Alex Dubov | 6113ed7 | 2007-04-12 16:59:17 +1000 | [diff] [blame] | 360 | fm->eject = tifm_7xx1_dummy_eject; |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 361 | writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE); |
| 362 | mmiowb(); |
| 363 | free_irq(dev->irq, fm); |
| 364 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 365 | tifm_remove_adapter(fm); |
| 366 | |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 367 | pci_set_drvdata(dev, NULL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 368 | |
| 369 | iounmap(fm->addr); |
| 370 | pci_intx(dev, 0); |
| 371 | pci_release_regions(dev); |
| 372 | |
| 373 | pci_disable_device(dev); |
| 374 | tifm_free_adapter(fm); |
| 375 | } |
| 376 | |
| 377 | static struct pci_device_id tifm_7xx1_pci_tbl [] = { |
Alex Dubov | b5ad676 | 2006-12-11 01:55:35 +1100 | [diff] [blame] | 378 | { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID, |
| 379 | PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */ |
| 380 | { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID, |
| 381 | PCI_ANY_ID, 0, 0, 0 }, |
| 382 | { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID, |
| 383 | PCI_ANY_ID, 0, 0, 0 }, |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 384 | { } |
| 385 | }; |
| 386 | |
| 387 | static struct pci_driver tifm_7xx1_driver = { |
| 388 | .name = DRIVER_NAME, |
| 389 | .id_table = tifm_7xx1_pci_tbl, |
| 390 | .probe = tifm_7xx1_probe, |
| 391 | .remove = tifm_7xx1_remove, |
| 392 | .suspend = tifm_7xx1_suspend, |
| 393 | .resume = tifm_7xx1_resume, |
| 394 | }; |
| 395 | |
| 396 | static int __init tifm_7xx1_init(void) |
| 397 | { |
| 398 | return pci_register_driver(&tifm_7xx1_driver); |
| 399 | } |
| 400 | |
| 401 | static void __exit tifm_7xx1_exit(void) |
| 402 | { |
| 403 | pci_unregister_driver(&tifm_7xx1_driver); |
| 404 | } |
| 405 | |
| 406 | MODULE_AUTHOR("Alex Dubov"); |
| 407 | MODULE_DESCRIPTION("TI FlashMedia host driver"); |
| 408 | MODULE_LICENSE("GPL"); |
| 409 | MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl); |
| 410 | MODULE_VERSION(DRIVER_VERSION); |
| 411 | |
| 412 | module_init(tifm_7xx1_init); |
| 413 | module_exit(tifm_7xx1_exit); |