Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Cirrus PD6729 PCI-PCMCIA bridge. |
| 3 | * |
| 4 | * Based on the i82092.c driver. |
| 5 | * |
| 6 | * This software may be used and distributed according to the terms of |
| 7 | * the GNU General Public License, incorporated herein by reference. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/pci.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/device.h> |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 18 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <pcmcia/ss.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include "pd6729.h" |
| 25 | #include "i82365.h" |
| 26 | #include "cirrus.h" |
| 27 | |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge"); |
| 30 | MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>"); |
| 31 | |
| 32 | #define MAX_SOCKETS 2 |
| 33 | |
| 34 | /* |
| 35 | * simple helper functions |
| 36 | * External clock time, in nanoseconds. 120 ns = 8.33 MHz |
| 37 | */ |
| 38 | #define to_cycles(ns) ((ns)/120) |
| 39 | |
| 40 | #ifndef NO_IRQ |
| 41 | #define NO_IRQ ((unsigned int)(0)) |
| 42 | #endif |
| 43 | |
| 44 | /* |
| 45 | * PARAMETERS |
| 46 | * irq_mode=n |
| 47 | * Specifies the interrupt delivery mode. The default (1) is to use PCI |
| 48 | * interrupts; a value of 0 selects ISA interrupts. This must be set for |
| 49 | * correct operation of PCI card readers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | module_param(irq_mode, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | MODULE_PARM_DESC(irq_mode, |
| 56 | "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | static DEFINE_SPINLOCK(port_lock); |
| 59 | |
| 60 | /* basic value read/write functions */ |
| 61 | |
| 62 | static unsigned char indirect_read(struct pd6729_socket *socket, |
| 63 | unsigned short reg) |
| 64 | { |
| 65 | unsigned long port; |
| 66 | unsigned char val; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | spin_lock_irqsave(&port_lock, flags); |
| 70 | reg += socket->number * 0x40; |
| 71 | port = socket->io_base; |
| 72 | outb(reg, port); |
| 73 | val = inb(port + 1); |
| 74 | spin_unlock_irqrestore(&port_lock, flags); |
| 75 | |
| 76 | return val; |
| 77 | } |
| 78 | |
| 79 | static unsigned short indirect_read16(struct pd6729_socket *socket, |
| 80 | unsigned short reg) |
| 81 | { |
| 82 | unsigned long port; |
| 83 | unsigned short tmp; |
| 84 | unsigned long flags; |
| 85 | |
| 86 | spin_lock_irqsave(&port_lock, flags); |
| 87 | reg = reg + socket->number * 0x40; |
| 88 | port = socket->io_base; |
| 89 | outb(reg, port); |
| 90 | tmp = inb(port + 1); |
| 91 | reg++; |
| 92 | outb(reg, port); |
| 93 | tmp = tmp | (inb(port + 1) << 8); |
| 94 | spin_unlock_irqrestore(&port_lock, flags); |
| 95 | |
| 96 | return tmp; |
| 97 | } |
| 98 | |
| 99 | static void indirect_write(struct pd6729_socket *socket, unsigned short reg, |
| 100 | unsigned char value) |
| 101 | { |
| 102 | unsigned long port; |
| 103 | unsigned long flags; |
| 104 | |
| 105 | spin_lock_irqsave(&port_lock, flags); |
| 106 | reg = reg + socket->number * 0x40; |
| 107 | port = socket->io_base; |
| 108 | outb(reg, port); |
| 109 | outb(value, port + 1); |
| 110 | spin_unlock_irqrestore(&port_lock, flags); |
| 111 | } |
| 112 | |
| 113 | static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg, |
| 114 | unsigned char mask) |
| 115 | { |
| 116 | unsigned long port; |
| 117 | unsigned char val; |
| 118 | unsigned long flags; |
| 119 | |
| 120 | spin_lock_irqsave(&port_lock, flags); |
| 121 | reg = reg + socket->number * 0x40; |
| 122 | port = socket->io_base; |
| 123 | outb(reg, port); |
| 124 | val = inb(port + 1); |
| 125 | val |= mask; |
| 126 | outb(reg, port); |
| 127 | outb(val, port + 1); |
| 128 | spin_unlock_irqrestore(&port_lock, flags); |
| 129 | } |
| 130 | |
| 131 | static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg, |
| 132 | unsigned char mask) |
| 133 | { |
| 134 | unsigned long port; |
| 135 | unsigned char val; |
| 136 | unsigned long flags; |
| 137 | |
| 138 | spin_lock_irqsave(&port_lock, flags); |
| 139 | reg = reg + socket->number * 0x40; |
| 140 | port = socket->io_base; |
| 141 | outb(reg, port); |
| 142 | val = inb(port + 1); |
| 143 | val &= ~mask; |
| 144 | outb(reg, port); |
| 145 | outb(val, port + 1); |
| 146 | spin_unlock_irqrestore(&port_lock, flags); |
| 147 | } |
| 148 | |
| 149 | static void indirect_write16(struct pd6729_socket *socket, unsigned short reg, |
| 150 | unsigned short value) |
| 151 | { |
| 152 | unsigned long port; |
| 153 | unsigned char val; |
| 154 | unsigned long flags; |
| 155 | |
| 156 | spin_lock_irqsave(&port_lock, flags); |
| 157 | reg = reg + socket->number * 0x40; |
| 158 | port = socket->io_base; |
| 159 | |
| 160 | outb(reg, port); |
| 161 | val = value & 255; |
| 162 | outb(val, port + 1); |
| 163 | |
| 164 | reg++; |
| 165 | |
| 166 | outb(reg, port); |
| 167 | val = value >> 8; |
| 168 | outb(val, port + 1); |
| 169 | spin_unlock_irqrestore(&port_lock, flags); |
| 170 | } |
| 171 | |
| 172 | /* Interrupt handler functionality */ |
| 173 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 174 | static irqreturn_t pd6729_interrupt(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct pd6729_socket *socket = (struct pd6729_socket *)dev; |
| 177 | int i; |
| 178 | int loopcount = 0; |
| 179 | int handled = 0; |
| 180 | unsigned int events, active = 0; |
| 181 | |
| 182 | while (1) { |
| 183 | loopcount++; |
| 184 | if (loopcount > 20) { |
| 185 | printk(KERN_ERR "pd6729: infinite eventloop " |
| 186 | "in interrupt\n"); |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | active = 0; |
| 191 | |
| 192 | for (i = 0; i < MAX_SOCKETS; i++) { |
| 193 | unsigned int csc; |
| 194 | |
| 195 | /* card status change register */ |
| 196 | csc = indirect_read(&socket[i], I365_CSC); |
| 197 | if (csc == 0) /* no events on this socket */ |
| 198 | continue; |
| 199 | |
| 200 | handled = 1; |
| 201 | events = 0; |
| 202 | |
| 203 | if (csc & I365_CSC_DETECT) { |
| 204 | events |= SS_DETECT; |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 205 | dev_vdbg(&socket[i].socket.dev, |
| 206 | "Card detected in socket %i!\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (indirect_read(&socket[i], I365_INTCTL) |
| 210 | & I365_PC_IOCARD) { |
| 211 | /* For IO/CARDS, bit 0 means "read the card" */ |
| 212 | events |= (csc & I365_CSC_STSCHG) |
| 213 | ? SS_STSCHG : 0; |
| 214 | } else { |
| 215 | /* Check for battery/ready events */ |
| 216 | events |= (csc & I365_CSC_BVD1) |
| 217 | ? SS_BATDEAD : 0; |
| 218 | events |= (csc & I365_CSC_BVD2) |
| 219 | ? SS_BATWARN : 0; |
| 220 | events |= (csc & I365_CSC_READY) |
| 221 | ? SS_READY : 0; |
| 222 | } |
| 223 | |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 224 | if (events) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | pcmcia_parse_events(&socket[i].socket, events); |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | active |= events; |
| 228 | } |
| 229 | |
| 230 | if (active == 0) /* no more events to handle */ |
| 231 | break; |
| 232 | } |
| 233 | return IRQ_RETVAL(handled); |
| 234 | } |
| 235 | |
| 236 | /* socket functions */ |
| 237 | |
| 238 | static void pd6729_interrupt_wrapper(unsigned long data) |
| 239 | { |
| 240 | struct pd6729_socket *socket = (struct pd6729_socket *) data; |
| 241 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 242 | pd6729_interrupt(0, (void *)socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | mod_timer(&socket->poll_timer, jiffies + HZ); |
| 244 | } |
| 245 | |
| 246 | static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value) |
| 247 | { |
| 248 | struct pd6729_socket *socket |
| 249 | = container_of(sock, struct pd6729_socket, socket); |
| 250 | unsigned int status; |
| 251 | unsigned int data; |
| 252 | struct pd6729_socket *t; |
| 253 | |
| 254 | /* Interface Status Register */ |
| 255 | status = indirect_read(socket, I365_STATUS); |
| 256 | *value = 0; |
| 257 | |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 258 | if ((status & I365_CS_DETECT) == I365_CS_DETECT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | *value |= SS_DETECT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | * IO cards have a different meaning of bits 0,1 |
| 263 | * Also notice the inverse-logic on the bits |
| 264 | */ |
| 265 | if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) { |
| 266 | /* IO card */ |
| 267 | if (!(status & I365_CS_STSCHG)) |
| 268 | *value |= SS_STSCHG; |
| 269 | } else { |
| 270 | /* non I/O card */ |
| 271 | if (!(status & I365_CS_BVD1)) |
| 272 | *value |= SS_BATDEAD; |
| 273 | if (!(status & I365_CS_BVD2)) |
| 274 | *value |= SS_BATWARN; |
| 275 | } |
| 276 | |
| 277 | if (status & I365_CS_WRPROT) |
| 278 | *value |= SS_WRPROT; /* card is write protected */ |
| 279 | |
| 280 | if (status & I365_CS_READY) |
| 281 | *value |= SS_READY; /* card is not busy */ |
| 282 | |
| 283 | if (status & I365_CS_POWERON) |
| 284 | *value |= SS_POWERON; /* power is applied to the card */ |
| 285 | |
| 286 | t = (socket->number) ? socket : socket + 1; |
| 287 | indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA); |
| 288 | data = indirect_read16(t, PD67_EXT_DATA); |
| 289 | *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state) |
| 296 | { |
| 297 | struct pd6729_socket *socket |
| 298 | = container_of(sock, struct pd6729_socket, socket); |
| 299 | unsigned char reg, data; |
| 300 | |
| 301 | /* First, set the global controller options */ |
| 302 | indirect_write(socket, I365_GBLCTL, 0x00); |
| 303 | indirect_write(socket, I365_GENCTL, 0x00); |
| 304 | |
| 305 | /* Values for the IGENC register */ |
| 306 | socket->card_irq = state->io_irq; |
| 307 | |
| 308 | reg = 0; |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 309 | /* The reset bit has "inverse" logic */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (!(state->flags & SS_RESET)) |
| 311 | reg |= I365_PC_RESET; |
| 312 | if (state->flags & SS_IOCARD) |
| 313 | reg |= I365_PC_IOCARD; |
| 314 | |
| 315 | /* IGENC, Interrupt and General Control Register */ |
| 316 | indirect_write(socket, I365_INTCTL, reg); |
| 317 | |
| 318 | /* Power registers */ |
| 319 | |
| 320 | reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */ |
| 321 | |
| 322 | if (state->flags & SS_PWR_AUTO) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 323 | dev_dbg(&sock->dev, "Auto power\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | reg |= I365_PWR_AUTO; /* automatic power mngmnt */ |
| 325 | } |
| 326 | if (state->flags & SS_OUTPUT_ENA) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 327 | dev_dbg(&sock->dev, "Power Enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | reg |= I365_PWR_OUT; /* enable power */ |
| 329 | } |
| 330 | |
| 331 | switch (state->Vcc) { |
| 332 | case 0: |
| 333 | break; |
| 334 | case 33: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 335 | dev_dbg(&sock->dev, |
| 336 | "setting voltage to Vcc to 3.3V on socket %i\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | socket->number); |
| 338 | reg |= I365_VCC_5V; |
| 339 | indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V); |
| 340 | break; |
| 341 | case 50: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 342 | dev_dbg(&sock->dev, |
| 343 | "setting voltage to Vcc to 5V on socket %i\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | socket->number); |
| 345 | reg |= I365_VCC_5V; |
| 346 | indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V); |
| 347 | break; |
| 348 | default: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 349 | dev_dbg(&sock->dev, |
| 350 | "pd6729_set_socket called with invalid VCC power " |
| 351 | "value: %i\n", state->Vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | } |
| 354 | |
| 355 | switch (state->Vpp) { |
| 356 | case 0: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 357 | dev_dbg(&sock->dev, "not setting Vpp on socket %i\n", |
| 358 | socket->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | break; |
| 360 | case 33: |
| 361 | case 50: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 362 | dev_dbg(&sock->dev, "setting Vpp to Vcc for socket %i\n", |
| 363 | socket->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | reg |= I365_VPP1_5V; |
| 365 | break; |
| 366 | case 120: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 367 | dev_dbg(&sock->dev, "setting Vpp to 12.0\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | reg |= I365_VPP1_12V; |
| 369 | break; |
| 370 | default: |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 371 | dev_dbg(&sock->dev, "pd6729: pd6729_set_socket called with " |
| 372 | "invalid VPP power value: %i\n", state->Vpp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return -EINVAL; |
| 374 | } |
| 375 | |
| 376 | /* only write if changed */ |
| 377 | if (reg != indirect_read(socket, I365_POWER)) |
| 378 | indirect_write(socket, I365_POWER, reg); |
| 379 | |
| 380 | if (irq_mode == 1) { |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 381 | /* all interrupts are to be done as PCI interrupts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ; |
| 383 | } else |
| 384 | data = 0; |
| 385 | |
| 386 | indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1); |
| 387 | indirect_write(socket, PD67_EXT_DATA, data); |
| 388 | |
| 389 | /* Enable specific interrupt events */ |
| 390 | |
| 391 | reg = 0x00; |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 392 | if (state->csc_mask & SS_DETECT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | reg |= I365_CSC_DETECT; |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (state->flags & SS_IOCARD) { |
| 396 | if (state->csc_mask & SS_STSCHG) |
| 397 | reg |= I365_CSC_STSCHG; |
| 398 | } else { |
| 399 | if (state->csc_mask & SS_BATDEAD) |
| 400 | reg |= I365_CSC_BVD1; |
| 401 | if (state->csc_mask & SS_BATWARN) |
| 402 | reg |= I365_CSC_BVD2; |
| 403 | if (state->csc_mask & SS_READY) |
| 404 | reg |= I365_CSC_READY; |
| 405 | } |
| 406 | if (irq_mode == 1) |
| 407 | reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */ |
| 408 | indirect_write(socket, I365_CSCINT, reg); |
| 409 | |
| 410 | reg = indirect_read(socket, I365_INTCTL); |
| 411 | if (irq_mode == 1) |
| 412 | reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */ |
| 413 | else |
| 414 | reg |= socket->card_irq; |
| 415 | indirect_write(socket, I365_INTCTL, reg); |
| 416 | |
| 417 | /* now clear the (probably bogus) pending stuff by doing a dummy read */ |
| 418 | (void)indirect_read(socket, I365_CSC); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int pd6729_set_io_map(struct pcmcia_socket *sock, |
| 424 | struct pccard_io_map *io) |
| 425 | { |
| 426 | struct pd6729_socket *socket |
| 427 | = container_of(sock, struct pd6729_socket, socket); |
| 428 | unsigned char map, ioctl; |
| 429 | |
| 430 | map = io->map; |
| 431 | |
| 432 | /* Check error conditions */ |
| 433 | if (map > 1) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 434 | dev_dbg(&sock->dev, "pd6729_set_io_map with invalid map\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | /* Turn off the window before changing anything */ |
| 439 | if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map)) |
| 440 | indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map)); |
| 441 | |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 442 | /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | io->start, io->stop);*/ |
| 444 | |
| 445 | /* write the new values */ |
| 446 | indirect_write16(socket, I365_IO(map)+I365_W_START, io->start); |
| 447 | indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop); |
| 448 | |
| 449 | ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map); |
| 450 | |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 451 | if (io->flags & MAP_0WS) |
| 452 | ioctl |= I365_IOCTL_0WS(map); |
| 453 | if (io->flags & MAP_16BIT) |
| 454 | ioctl |= I365_IOCTL_16BIT(map); |
| 455 | if (io->flags & MAP_AUTOSZ) |
| 456 | ioctl |= I365_IOCTL_IOCS16(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | indirect_write(socket, I365_IOCTL, ioctl); |
| 459 | |
| 460 | /* Turn the window back on if needed */ |
| 461 | if (io->flags & MAP_ACTIVE) |
| 462 | indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map)); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int pd6729_set_mem_map(struct pcmcia_socket *sock, |
| 468 | struct pccard_mem_map *mem) |
| 469 | { |
| 470 | struct pd6729_socket *socket |
| 471 | = container_of(sock, struct pd6729_socket, socket); |
| 472 | unsigned short base, i; |
| 473 | unsigned char map; |
| 474 | |
| 475 | map = mem->map; |
| 476 | if (map > 4) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 477 | dev_warn(&sock->dev, "invalid map requested\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 482 | dev_warn(&sock->dev, "invalid invalid address / speed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | /* Turn off the window before changing anything */ |
| 487 | if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map)) |
| 488 | indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map)); |
| 489 | |
| 490 | /* write the start address */ |
| 491 | base = I365_MEM(map); |
| 492 | i = (mem->res->start >> 12) & 0x0fff; |
| 493 | if (mem->flags & MAP_16BIT) |
| 494 | i |= I365_MEM_16BIT; |
| 495 | if (mem->flags & MAP_0WS) |
| 496 | i |= I365_MEM_0WS; |
| 497 | indirect_write16(socket, base + I365_W_START, i); |
| 498 | |
| 499 | /* write the stop address */ |
| 500 | |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 501 | i = (mem->res->end >> 12) & 0x0fff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | switch (to_cycles(mem->speed)) { |
| 503 | case 0: |
| 504 | break; |
| 505 | case 1: |
| 506 | i |= I365_MEM_WS0; |
| 507 | break; |
| 508 | case 2: |
| 509 | i |= I365_MEM_WS1; |
| 510 | break; |
| 511 | default: |
| 512 | i |= I365_MEM_WS1 | I365_MEM_WS0; |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | indirect_write16(socket, base + I365_W_STOP, i); |
| 517 | |
| 518 | /* Take care of high byte */ |
| 519 | indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map)); |
| 520 | indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24); |
| 521 | |
| 522 | /* card start */ |
| 523 | |
| 524 | i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff; |
| 525 | if (mem->flags & MAP_WRPROT) |
| 526 | i |= I365_MEM_WRPROT; |
| 527 | if (mem->flags & MAP_ATTRIB) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 528 | /* dev_dbg(&sock->dev, "requesting attribute memory for " |
| 529 | "socket %i\n", socket->number);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | i |= I365_MEM_REG; |
| 531 | } else { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 532 | /* dev_dbg(&sock->dev, "requesting normal memory for " |
| 533 | "socket %i\n", socket->number);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | indirect_write16(socket, base + I365_W_OFF, i); |
| 536 | |
| 537 | /* Enable the window if necessary */ |
| 538 | if (mem->flags & MAP_ACTIVE) |
| 539 | indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map)); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int pd6729_init(struct pcmcia_socket *sock) |
| 545 | { |
| 546 | int i; |
| 547 | struct resource res = { .end = 0x0fff }; |
| 548 | pccard_io_map io = { 0, 0, 0, 0, 1 }; |
| 549 | pccard_mem_map mem = { .res = &res, }; |
| 550 | |
| 551 | pd6729_set_socket(sock, &dead_socket); |
| 552 | for (i = 0; i < 2; i++) { |
| 553 | io.map = i; |
| 554 | pd6729_set_io_map(sock, &io); |
| 555 | } |
| 556 | for (i = 0; i < 5; i++) { |
| 557 | mem.map = i; |
| 558 | pd6729_set_mem_map(sock, &mem); |
| 559 | } |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* the pccard structure and its functions */ |
| 566 | static struct pccard_operations pd6729_operations = { |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 567 | .init = pd6729_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | .get_status = pd6729_get_status, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | .set_socket = pd6729_set_socket, |
| 570 | .set_io_map = pd6729_set_io_map, |
| 571 | .set_mem_map = pd6729_set_mem_map, |
| 572 | }; |
| 573 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 574 | static irqreturn_t pd6729_test(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 576 | pr_devel("-> hit on irq %d\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return IRQ_HANDLED; |
| 578 | } |
| 579 | |
Komuro | 3e022d0 | 2006-11-04 10:00:47 +0900 | [diff] [blame] | 580 | static int pd6729_check_irq(int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 582 | int ret; |
| 583 | |
| 584 | ret = request_irq(irq, pd6729_test, IRQF_PROBE_SHARED, "x", |
| 585 | pd6729_test); |
| 586 | if (ret) |
| 587 | return -1; |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | free_irq(irq, pd6729_test); |
| 590 | return 0; |
| 591 | } |
| 592 | |
Andrew Morton | 9781b8b | 2006-05-20 15:00:32 -0700 | [diff] [blame] | 593 | static u_int __devinit pd6729_isa_scan(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
| 595 | u_int mask0, mask = 0; |
| 596 | int i; |
| 597 | |
| 598 | if (irq_mode == 1) { |
| 599 | printk(KERN_INFO "pd6729: PCI card interrupts, " |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 600 | "PCI status changes\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
Komuro | f9097dc | 2010-02-28 15:39:33 +0900 | [diff] [blame] | 604 | mask0 = PD67_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | /* just find interrupts that aren't in use */ |
| 607 | for (i = 0; i < 16; i++) |
Komuro | 3e022d0 | 2006-11-04 10:00:47 +0900 | [diff] [blame] | 608 | if ((mask0 & (1 << i)) && (pd6729_check_irq(i) == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | mask |= (1 << i); |
| 610 | |
| 611 | printk(KERN_INFO "pd6729: ISA irqs = "); |
| 612 | for (i = 0; i < 16; i++) |
| 613 | if (mask & (1<<i)) |
| 614 | printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i); |
| 615 | |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 616 | if (mask == 0) |
| 617 | printk("none!"); |
| 618 | else |
| 619 | printk(" polling status changes.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | return mask; |
| 622 | } |
| 623 | |
| 624 | static int __devinit pd6729_pci_probe(struct pci_dev *dev, |
| 625 | const struct pci_device_id *id) |
| 626 | { |
| 627 | int i, j, ret; |
| 628 | u_int mask; |
| 629 | char configbyte; |
| 630 | struct pd6729_socket *socket; |
| 631 | |
Dominik Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 632 | socket = kzalloc(sizeof(struct pd6729_socket) * MAX_SOCKETS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | GFP_KERNEL); |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 634 | if (!socket) { |
| 635 | dev_warn(&dev->dev, "failed to kzalloc socket.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return -ENOMEM; |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 639 | ret = pci_enable_device(dev); |
| 640 | if (ret) { |
| 641 | dev_warn(&dev->dev, "failed to enable pci_device.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | goto err_out_free_mem; |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Komuro | 94efb72 | 2009-10-24 08:07:39 +0900 | [diff] [blame] | 645 | if (!pci_resource_start(dev, 0)) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 646 | dev_warn(&dev->dev, "refusing to load the driver as the " |
| 647 | "io_base is NULL.\n"); |
Rahul Ruikar | 40d24ff | 2010-09-26 17:00:29 +0530 | [diff] [blame] | 648 | goto err_out_disable; |
Komuro | 94efb72 | 2009-10-24 08:07:39 +0900 | [diff] [blame] | 649 | } |
| 650 | |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 651 | dev_info(&dev->dev, "Cirrus PD6729 PCI to PCMCIA Bridge at 0x%llx " |
| 652 | "on irq %d\n", |
Greg Kroah-Hartman | 490ab72 | 2006-06-12 15:17:34 -0700 | [diff] [blame] | 653 | (unsigned long long)pci_resource_start(dev, 0), dev->irq); |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 654 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | * Since we have no memory BARs some firmware may not |
| 656 | * have had PCI_COMMAND_MEMORY enabled, yet the device needs it. |
| 657 | */ |
| 658 | pci_read_config_byte(dev, PCI_COMMAND, &configbyte); |
| 659 | if (!(configbyte & PCI_COMMAND_MEMORY)) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 660 | dev_dbg(&dev->dev, "pd6729: Enabling PCI_COMMAND_MEMORY.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | configbyte |= PCI_COMMAND_MEMORY; |
| 662 | pci_write_config_byte(dev, PCI_COMMAND, configbyte); |
| 663 | } |
| 664 | |
| 665 | ret = pci_request_regions(dev, "pd6729"); |
| 666 | if (ret) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 667 | dev_warn(&dev->dev, "pci request region failed.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | goto err_out_disable; |
| 669 | } |
| 670 | |
| 671 | if (dev->irq == NO_IRQ) |
| 672 | irq_mode = 0; /* fall back to ISA interrupt mode */ |
| 673 | |
| 674 | mask = pd6729_isa_scan(); |
| 675 | if (irq_mode == 0 && mask == 0) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 676 | dev_warn(&dev->dev, "no ISA interrupt is available.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | goto err_out_free_res; |
| 678 | } |
| 679 | |
| 680 | for (i = 0; i < MAX_SOCKETS; i++) { |
| 681 | socket[i].io_base = pci_resource_start(dev, 0); |
Dominik Brodowski | c35e66a | 2005-05-05 16:15:56 -0700 | [diff] [blame] | 682 | socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | socket[i].socket.map_size = 0x1000; |
| 684 | socket[i].socket.irq_mask = mask; |
| 685 | socket[i].socket.pci_irq = dev->irq; |
Dominik Brodowski | 7a96e87 | 2010-03-13 17:42:39 +0100 | [diff] [blame] | 686 | socket[i].socket.cb_dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | socket[i].socket.owner = THIS_MODULE; |
| 688 | |
| 689 | socket[i].number = i; |
| 690 | |
| 691 | socket[i].socket.ops = &pd6729_operations; |
| 692 | socket[i].socket.resource_ops = &pccard_nonstatic_ops; |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 693 | socket[i].socket.dev.parent = &dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | socket[i].socket.driver_data = &socket[i]; |
| 695 | } |
| 696 | |
| 697 | pci_set_drvdata(dev, socket); |
| 698 | if (irq_mode == 1) { |
| 699 | /* Register the interrupt handler */ |
Komuro | 5cbb2b9 | 2010-03-20 06:39:19 +0900 | [diff] [blame] | 700 | ret = request_irq(dev->irq, pd6729_interrupt, IRQF_SHARED, |
| 701 | "pd6729", socket); |
| 702 | if (ret) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 703 | dev_err(&dev->dev, "Failed to register irq %d\n", |
| 704 | dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | goto err_out_free_res; |
| 706 | } |
| 707 | } else { |
| 708 | /* poll Card status change */ |
| 709 | init_timer(&socket->poll_timer); |
| 710 | socket->poll_timer.function = pd6729_interrupt_wrapper; |
| 711 | socket->poll_timer.data = (unsigned long)socket; |
| 712 | socket->poll_timer.expires = jiffies + HZ; |
| 713 | add_timer(&socket->poll_timer); |
| 714 | } |
| 715 | |
| 716 | for (i = 0; i < MAX_SOCKETS; i++) { |
| 717 | ret = pcmcia_register_socket(&socket[i].socket); |
| 718 | if (ret) { |
Dominik Brodowski | a7149f9 | 2009-10-24 18:07:16 +0200 | [diff] [blame] | 719 | dev_warn(&dev->dev, "pcmcia_register_socket failed.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | for (j = 0; j < i ; j++) |
| 721 | pcmcia_unregister_socket(&socket[j].socket); |
| 722 | goto err_out_free_res2; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | return 0; |
| 727 | |
| 728 | err_out_free_res2: |
| 729 | if (irq_mode == 1) |
| 730 | free_irq(dev->irq, socket); |
| 731 | else |
| 732 | del_timer_sync(&socket->poll_timer); |
| 733 | err_out_free_res: |
| 734 | pci_release_regions(dev); |
| 735 | err_out_disable: |
| 736 | pci_disable_device(dev); |
| 737 | |
| 738 | err_out_free_mem: |
| 739 | kfree(socket); |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static void __devexit pd6729_pci_remove(struct pci_dev *dev) |
| 744 | { |
| 745 | int i; |
| 746 | struct pd6729_socket *socket = pci_get_drvdata(dev); |
| 747 | |
| 748 | for (i = 0; i < MAX_SOCKETS; i++) { |
| 749 | /* Turn off all interrupt sources */ |
| 750 | indirect_write(&socket[i], I365_CSCINT, 0); |
| 751 | indirect_write(&socket[i], I365_INTCTL, 0); |
| 752 | |
| 753 | pcmcia_unregister_socket(&socket[i].socket); |
| 754 | } |
| 755 | |
| 756 | if (irq_mode == 1) |
| 757 | free_irq(dev->irq, socket); |
| 758 | else |
| 759 | del_timer_sync(&socket->poll_timer); |
| 760 | pci_release_regions(dev); |
| 761 | pci_disable_device(dev); |
| 762 | |
| 763 | kfree(socket); |
| 764 | } |
| 765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | static struct pci_device_id pd6729_pci_ids[] = { |
| 767 | { |
| 768 | .vendor = PCI_VENDOR_ID_CIRRUS, |
| 769 | .device = PCI_DEVICE_ID_CIRRUS_6729, |
| 770 | .subvendor = PCI_ANY_ID, |
| 771 | .subdevice = PCI_ANY_ID, |
| 772 | }, |
| 773 | { } |
| 774 | }; |
| 775 | MODULE_DEVICE_TABLE(pci, pd6729_pci_ids); |
| 776 | |
Sam Ravnborg | ba66ddf | 2008-05-01 04:34:51 -0700 | [diff] [blame] | 777 | static struct pci_driver pd6729_pci_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | .name = "pd6729", |
| 779 | .id_table = pd6729_pci_ids, |
| 780 | .probe = pd6729_pci_probe, |
| 781 | .remove = __devexit_p(pd6729_pci_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | static int pd6729_module_init(void) |
| 785 | { |
Sam Ravnborg | ba66ddf | 2008-05-01 04:34:51 -0700 | [diff] [blame] | 786 | return pci_register_driver(&pd6729_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static void pd6729_module_exit(void) |
| 790 | { |
Sam Ravnborg | ba66ddf | 2008-05-01 04:34:51 -0700 | [diff] [blame] | 791 | pci_unregister_driver(&pd6729_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | module_init(pd6729_module_init); |
| 795 | module_exit(pd6729_module_exit); |