pcmcia: Convert to DEFINE_PCI_DEVICE_TABLE
[linux-2.6.git] / drivers / pcmcia / i82092.c
1 /* 
2  * Driver for Intel I82092AA PCI-PCMCIA bridge.
3  *
4  * (C) 2001 Red Hat, Inc.
5  *
6  * Author: Arjan Van De Ven <arjanv@redhat.com>
7  * Loosly based on i82365.c from the pcmcia-cs package
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/init.h>
14 #include <linux/workqueue.h>
15 #include <linux/interrupt.h>
16 #include <linux/device.h>
17
18 #include <pcmcia/ss.h>
19
20 #include <asm/system.h>
21 #include <asm/io.h>
22
23 #include "i82092aa.h"
24 #include "i82365.h"
25
26 MODULE_LICENSE("GPL");
27
28 /* PCI core routines */
29 static DEFINE_PCI_DEVICE_TABLE(i82092aa_pci_ids) = {
30         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82092AA_0) },
31         { }
32 };
33 MODULE_DEVICE_TABLE(pci, i82092aa_pci_ids);
34
35 static struct pci_driver i82092aa_pci_driver = {
36         .name           = "i82092aa",
37         .id_table       = i82092aa_pci_ids,
38         .probe          = i82092aa_pci_probe,
39         .remove         = __devexit_p(i82092aa_pci_remove),
40 };
41
42
43 /* the pccard structure and its functions */
44 static struct pccard_operations i82092aa_operations = {
45         .init                   = i82092aa_init,
46         .get_status             = i82092aa_get_status,
47         .set_socket             = i82092aa_set_socket,
48         .set_io_map             = i82092aa_set_io_map,
49         .set_mem_map            = i82092aa_set_mem_map,
50 };
51
52 /* The card can do up to 4 sockets, allocate a structure for each of them */
53
54 struct socket_info {
55         int     number;
56         int     card_state;     /*  0 = no socket,
57                                     1 = empty socket, 
58                                     2 = card but not initialized,
59                                     3 = operational card */
60         unsigned int io_base;   /* base io address of the socket */
61         
62         struct pcmcia_socket socket;
63         struct pci_dev *dev;    /* The PCI device for the socket */
64 };
65
66 #define MAX_SOCKETS 4
67 static struct socket_info sockets[MAX_SOCKETS];
68 static int socket_count;  /* shortcut */                                                                                
69
70
71 static int __devinit i82092aa_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
72 {
73         unsigned char configbyte;
74         int i, ret;
75         
76         enter("i82092aa_pci_probe");
77         
78         if ((ret = pci_enable_device(dev)))
79                 return ret;
80                 
81         pci_read_config_byte(dev, 0x40, &configbyte);  /* PCI Configuration Control */
82         switch(configbyte&6) {
83                 case 0:
84                         socket_count = 2;
85                         break;
86                 case 2:
87                         socket_count = 1;
88                         break;
89                 case 4:
90                 case 6:
91                         socket_count = 4;
92                         break;
93                         
94                 default:
95                         printk(KERN_ERR "i82092aa: Oops, you did something we didn't think of.\n");
96                         ret = -EIO;
97                         goto err_out_disable;
98         }
99         printk(KERN_INFO "i82092aa: configured as a %d socket device.\n", socket_count);
100
101         if (!request_region(pci_resource_start(dev, 0), 2, "i82092aa")) {
102                 ret = -EBUSY;
103                 goto err_out_disable;
104         }
105         
106         for (i = 0;i<socket_count;i++) {
107                 sockets[i].card_state = 1; /* 1 = present but empty */
108                 sockets[i].io_base = pci_resource_start(dev, 0);
109                 sockets[i].socket.features |= SS_CAP_PCCARD;
110                 sockets[i].socket.map_size = 0x1000;
111                 sockets[i].socket.irq_mask = 0;
112                 sockets[i].socket.pci_irq  = dev->irq;
113                 sockets[i].socket.cb_dev  = dev;
114                 sockets[i].socket.owner = THIS_MODULE;
115
116                 sockets[i].number = i;
117                 
118                 if (card_present(i)) {
119                         sockets[i].card_state = 3;
120                         dprintk(KERN_DEBUG "i82092aa: slot %i is occupied\n",i);
121                 } else {
122                         dprintk(KERN_DEBUG "i82092aa: slot %i is vacant\n",i);
123                 }
124         }
125                 
126         /* Now, specifiy that all interrupts are to be done as PCI interrupts */
127         configbyte = 0xFF; /* bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt */
128         pci_write_config_byte(dev, 0x50, configbyte); /* PCI Interrupt Routing Register */
129
130         /* Register the interrupt handler */
131         dprintk(KERN_DEBUG "Requesting interrupt %i \n",dev->irq);
132         if ((ret = request_irq(dev->irq, i82092aa_interrupt, IRQF_SHARED, "i82092aa", i82092aa_interrupt))) {
133                 printk(KERN_ERR "i82092aa: Failed to register IRQ %d, aborting\n", dev->irq);
134                 goto err_out_free_res;
135         }
136
137         pci_set_drvdata(dev, &sockets[i].socket);
138
139         for (i = 0; i<socket_count; i++) {
140                 sockets[i].socket.dev.parent = &dev->dev;
141                 sockets[i].socket.ops = &i82092aa_operations;
142                 sockets[i].socket.resource_ops = &pccard_nonstatic_ops;
143                 ret = pcmcia_register_socket(&sockets[i].socket);
144                 if (ret) {
145                         goto err_out_free_sockets;
146                 }
147         }
148
149         leave("i82092aa_pci_probe");
150         return 0;
151
152 err_out_free_sockets:
153         if (i) {
154                 for (i--;i>=0;i--) {
155                         pcmcia_unregister_socket(&sockets[i].socket);
156                 }
157         }
158         free_irq(dev->irq, i82092aa_interrupt);
159 err_out_free_res:
160         release_region(pci_resource_start(dev, 0), 2);
161 err_out_disable:
162         pci_disable_device(dev);
163         return ret;                     
164 }
165
166 static void __devexit i82092aa_pci_remove(struct pci_dev *dev)
167 {
168         struct pcmcia_socket *socket = pci_get_drvdata(dev);
169
170         enter("i82092aa_pci_remove");
171         
172         free_irq(dev->irq, i82092aa_interrupt);
173
174         if (socket)
175                 pcmcia_unregister_socket(socket);
176
177         leave("i82092aa_pci_remove");
178 }
179
180 static DEFINE_SPINLOCK(port_lock);
181
182 /* basic value read/write functions */
183
184 static unsigned char indirect_read(int socket, unsigned short reg)
185 {
186         unsigned short int port;
187         unsigned char val;
188         unsigned long flags;
189         spin_lock_irqsave(&port_lock,flags);
190         reg += socket * 0x40;
191         port = sockets[socket].io_base;
192         outb(reg,port);
193         val = inb(port+1);
194         spin_unlock_irqrestore(&port_lock,flags);
195         return val;
196 }
197
198 #if 0
199 static unsigned short indirect_read16(int socket, unsigned short reg)
200 {
201         unsigned short int port;
202         unsigned short tmp;
203         unsigned long flags;
204         spin_lock_irqsave(&port_lock,flags);
205         reg  = reg + socket * 0x40;
206         port = sockets[socket].io_base;
207         outb(reg,port);
208         tmp = inb(port+1);
209         reg++;
210         outb(reg,port);
211         tmp = tmp | (inb(port+1)<<8);
212         spin_unlock_irqrestore(&port_lock,flags);
213         return tmp;
214 }
215 #endif
216
217 static void indirect_write(int socket, unsigned short reg, unsigned char value)
218 {
219         unsigned short int port;
220         unsigned long flags;
221         spin_lock_irqsave(&port_lock,flags);
222         reg = reg + socket * 0x40;
223         port = sockets[socket].io_base; 
224         outb(reg,port);
225         outb(value,port+1);
226         spin_unlock_irqrestore(&port_lock,flags);
227 }
228
229 static void indirect_setbit(int socket, unsigned short reg, unsigned char mask)
230 {
231         unsigned short int port;
232         unsigned char val;
233         unsigned long flags;
234         spin_lock_irqsave(&port_lock,flags);
235         reg = reg + socket * 0x40;
236         port = sockets[socket].io_base; 
237         outb(reg,port);
238         val = inb(port+1);
239         val |= mask;
240         outb(reg,port);
241         outb(val,port+1);
242         spin_unlock_irqrestore(&port_lock,flags);
243 }
244
245
246 static void indirect_resetbit(int socket, unsigned short reg, unsigned char mask)
247 {
248         unsigned short int port;
249         unsigned char val;
250         unsigned long flags;
251         spin_lock_irqsave(&port_lock,flags);
252         reg = reg + socket * 0x40;
253         port = sockets[socket].io_base; 
254         outb(reg,port);
255         val = inb(port+1);
256         val &= ~mask;
257         outb(reg,port);
258         outb(val,port+1);
259         spin_unlock_irqrestore(&port_lock,flags);
260 }
261
262 static void indirect_write16(int socket, unsigned short reg, unsigned short value)
263 {
264         unsigned short int port;
265         unsigned char val;
266         unsigned long flags;
267         spin_lock_irqsave(&port_lock,flags);
268         reg = reg + socket * 0x40;
269         port = sockets[socket].io_base; 
270         
271         outb(reg,port);
272         val = value & 255;
273         outb(val,port+1);
274         
275         reg++;
276         
277         outb(reg,port);
278         val = value>>8;
279         outb(val,port+1);
280         spin_unlock_irqrestore(&port_lock,flags);
281 }
282
283 /* simple helper functions */
284 /* External clock time, in nanoseconds.  120 ns = 8.33 MHz */
285 static int cycle_time = 120;
286
287 static int to_cycles(int ns)
288 {
289         if (cycle_time!=0)
290                 return ns/cycle_time;
291         else
292                 return 0;
293 }
294     
295
296 /* Interrupt handler functionality */
297
298 static irqreturn_t i82092aa_interrupt(int irq, void *dev)
299 {
300         int i;
301         int loopcount = 0;
302         int handled = 0;
303
304         unsigned int events, active=0;
305         
306 /*      enter("i82092aa_interrupt");*/
307         
308         while (1) {
309                 loopcount++;
310                 if (loopcount>20) {
311                         printk(KERN_ERR "i82092aa: infinite eventloop in interrupt \n");
312                         break;
313                 }
314                 
315                 active = 0;
316                 
317                 for (i=0;i<socket_count;i++) {
318                         int csc;
319                         if (sockets[i].card_state==0) /* Inactive socket, should not happen */
320                                 continue;
321                         
322                         csc = indirect_read(i,I365_CSC); /* card status change register */
323                         
324                         if (csc==0)  /* no events on this socket */
325                                 continue;
326                         handled = 1;
327                         events = 0;
328                          
329                         if (csc & I365_CSC_DETECT) {
330                                 events |= SS_DETECT;
331                                 printk("Card detected in socket %i!\n",i);
332                          }
333                         
334                         if (indirect_read(i,I365_INTCTL) & I365_PC_IOCARD) { 
335                                 /* For IO/CARDS, bit 0 means "read the card" */
336                                 events |= (csc & I365_CSC_STSCHG) ? SS_STSCHG : 0; 
337                         } else {
338                                 /* Check for battery/ready events */
339                                 events |= (csc & I365_CSC_BVD1) ? SS_BATDEAD : 0;
340                                 events |= (csc & I365_CSC_BVD2) ? SS_BATWARN : 0;
341                                 events |= (csc & I365_CSC_READY) ? SS_READY : 0;
342                         }
343                         
344                         if (events) {
345                                 pcmcia_parse_events(&sockets[i].socket, events);
346                         }
347                         active |= events;
348                 }
349                                 
350                 if (active==0) /* no more events to handle */
351                         break;                          
352                 
353         }
354         return IRQ_RETVAL(handled);
355 /*      leave("i82092aa_interrupt");*/
356 }
357
358
359
360 /* socket functions */
361
362 static int card_present(int socketno)
363 {       
364         unsigned int val;
365         enter("card_present");
366         
367         if ((socketno<0) || (socketno >= MAX_SOCKETS))
368                 return 0;
369         if (sockets[socketno].io_base == 0)
370                 return 0;
371
372                 
373         val = indirect_read(socketno, 1); /* Interface status register */
374         if ((val&12)==12) {
375                 leave("card_present 1");
376                 return 1;
377         }
378                 
379         leave("card_present 0");
380         return 0;
381 }
382
383 static void set_bridge_state(int sock)
384 {
385         enter("set_bridge_state");
386         indirect_write(sock, I365_GBLCTL,0x00);
387         indirect_write(sock, I365_GENCTL,0x00);
388         
389         indirect_setbit(sock, I365_INTCTL,0x08);
390         leave("set_bridge_state");
391 }
392
393
394
395
396
397       
398 static int i82092aa_init(struct pcmcia_socket *sock)
399 {
400         int i;
401         struct resource res = { .start = 0, .end = 0x0fff };
402         pccard_io_map io = { 0, 0, 0, 0, 1 };
403         pccard_mem_map mem = { .res = &res, };
404         
405         enter("i82092aa_init");
406                         
407         for (i = 0; i < 2; i++) {
408                 io.map = i;
409                 i82092aa_set_io_map(sock, &io);
410         }
411         for (i = 0; i < 5; i++) {
412                 mem.map = i;
413                 i82092aa_set_mem_map(sock, &mem);
414         }
415         
416         leave("i82092aa_init");
417         return 0;
418 }
419                                                                                                                                                                                                                                               
420 static int i82092aa_get_status(struct pcmcia_socket *socket, u_int *value)
421 {
422         unsigned int sock = container_of(socket, struct socket_info, socket)->number;
423         unsigned int status;
424         
425         enter("i82092aa_get_status");
426         
427         status = indirect_read(sock,I365_STATUS); /* Interface Status Register */
428         *value = 0;
429         
430         if ((status & I365_CS_DETECT) == I365_CS_DETECT) {
431                 *value |= SS_DETECT;
432         }
433                 
434         /* IO cards have a different meaning of bits 0,1 */
435         /* Also notice the inverse-logic on the bits */
436          if (indirect_read(sock, I365_INTCTL) & I365_PC_IOCARD) {
437                 /* IO card */
438                 if (!(status & I365_CS_STSCHG))
439                         *value |= SS_STSCHG;
440          } else { /* non I/O card */
441                 if (!(status & I365_CS_BVD1))
442                         *value |= SS_BATDEAD;
443                 if (!(status & I365_CS_BVD2))
444                         *value |= SS_BATWARN;
445                         
446          }
447          
448          if (status & I365_CS_WRPROT)
449                 (*value) |= SS_WRPROT;  /* card is write protected */
450          
451          if (status & I365_CS_READY)
452                 (*value) |= SS_READY;    /* card is not busy */
453                 
454          if (status & I365_CS_POWERON)
455                 (*value) |= SS_POWERON;  /* power is applied to the card */
456
457
458         leave("i82092aa_get_status");
459         return 0;
460 }
461
462
463 static int i82092aa_set_socket(struct pcmcia_socket *socket, socket_state_t *state) 
464 {
465         unsigned int sock = container_of(socket, struct socket_info, socket)->number;
466         unsigned char reg;
467         
468         enter("i82092aa_set_socket");
469         
470         /* First, set the global controller options */
471         
472         set_bridge_state(sock);
473         
474         /* Values for the IGENC register */
475         
476         reg = 0;
477         if (!(state->flags & SS_RESET))         /* The reset bit has "inverse" logic */
478                 reg = reg | I365_PC_RESET;  
479         if (state->flags & SS_IOCARD) 
480                 reg = reg | I365_PC_IOCARD;
481                 
482         indirect_write(sock,I365_INTCTL,reg); /* IGENC, Interrupt and General Control Register */
483         
484         /* Power registers */
485         
486         reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
487         
488         if (state->flags & SS_PWR_AUTO) {
489                 printk("Auto power\n");
490                 reg |= I365_PWR_AUTO;   /* automatic power mngmnt */
491         }
492         if (state->flags & SS_OUTPUT_ENA) {
493                 printk("Power Enabled \n");
494                 reg |= I365_PWR_OUT;    /* enable power */
495         }
496         
497         switch (state->Vcc) {
498                 case 0: 
499                         break;
500                 case 50: 
501                         printk("setting voltage to Vcc to 5V on socket %i\n",sock);
502                         reg |= I365_VCC_5V;
503                         break;
504                 default:
505                         printk("i82092aa: i82092aa_set_socket called with invalid VCC power value: %i ", state->Vcc);
506                         leave("i82092aa_set_socket");
507                         return -EINVAL;
508         }
509         
510         
511         switch (state->Vpp) {
512                 case 0: 
513                         printk("not setting Vpp on socket %i\n",sock);
514                         break;
515                 case 50: 
516                         printk("setting Vpp to 5.0 for socket %i\n",sock);
517                         reg |= I365_VPP1_5V | I365_VPP2_5V;
518                         break;
519                 case 120: 
520                         printk("setting Vpp to 12.0\n");
521                         reg |= I365_VPP1_12V | I365_VPP2_12V;
522                         break;
523                 default:
524                         printk("i82092aa: i82092aa_set_socket called with invalid VPP power value: %i ", state->Vcc);
525                         leave("i82092aa_set_socket");
526                         return -EINVAL;
527         }
528         
529         if (reg != indirect_read(sock,I365_POWER)) /* only write if changed */
530                 indirect_write(sock,I365_POWER,reg);
531                 
532         /* Enable specific interrupt events */
533         
534         reg = 0x00;
535         if (state->csc_mask & SS_DETECT) {
536                 reg |= I365_CSC_DETECT;
537         }
538         if (state->flags & SS_IOCARD) {
539                 if (state->csc_mask & SS_STSCHG)
540                         reg |= I365_CSC_STSCHG;
541         } else {
542                 if (state->csc_mask & SS_BATDEAD) 
543                         reg |= I365_CSC_BVD1;
544                 if (state->csc_mask & SS_BATWARN) 
545                         reg |= I365_CSC_BVD2;
546                 if (state->csc_mask & SS_READY) 
547                         reg |= I365_CSC_READY; 
548                                         
549         }
550         
551         /* now write the value and clear the (probably bogus) pending stuff by doing a dummy read*/
552         
553         indirect_write(sock,I365_CSCINT,reg);
554         (void)indirect_read(sock,I365_CSC);
555
556         leave("i82092aa_set_socket");
557         return 0;
558 }
559
560 static int i82092aa_set_io_map(struct pcmcia_socket *socket, struct pccard_io_map *io)
561 {
562         unsigned int sock = container_of(socket, struct socket_info, socket)->number;
563         unsigned char map, ioctl;
564         
565         enter("i82092aa_set_io_map");
566         
567         map = io->map;
568         
569         /* Check error conditions */    
570         if (map > 1) {
571                 leave("i82092aa_set_io_map with invalid map");
572                 return -EINVAL;
573         }
574         if ((io->start > 0xffff) || (io->stop > 0xffff) || (io->stop < io->start)){
575                 leave("i82092aa_set_io_map with invalid io");
576                 return -EINVAL;
577         }
578
579         /* Turn off the window before changing anything */ 
580         if (indirect_read(sock, I365_ADDRWIN) & I365_ENA_IO(map))
581                 indirect_resetbit(sock, I365_ADDRWIN, I365_ENA_IO(map));
582
583 /*      printk("set_io_map: Setting range to %x - %x \n",io->start,io->stop);  */
584         
585         /* write the new values */
586         indirect_write16(sock,I365_IO(map)+I365_W_START,io->start);             
587         indirect_write16(sock,I365_IO(map)+I365_W_STOP,io->stop);               
588                         
589         ioctl = indirect_read(sock,I365_IOCTL) & ~I365_IOCTL_MASK(map);
590         
591         if (io->flags & (MAP_16BIT|MAP_AUTOSZ))
592                 ioctl |= I365_IOCTL_16BIT(map);
593                 
594         indirect_write(sock,I365_IOCTL,ioctl);
595         
596         /* Turn the window back on if needed */
597         if (io->flags & MAP_ACTIVE)
598                 indirect_setbit(sock,I365_ADDRWIN,I365_ENA_IO(map));
599                         
600         leave("i82092aa_set_io_map");   
601         return 0;
602 }
603
604 static int i82092aa_set_mem_map(struct pcmcia_socket *socket, struct pccard_mem_map *mem)
605 {
606         struct socket_info *sock_info = container_of(socket, struct socket_info, socket);
607         unsigned int sock = sock_info->number;
608         struct pci_bus_region region;
609         unsigned short base, i;
610         unsigned char map;
611         
612         enter("i82092aa_set_mem_map");
613
614         pcibios_resource_to_bus(sock_info->dev, &region, mem->res);
615         
616         map = mem->map;
617         if (map > 4) {
618                 leave("i82092aa_set_mem_map: invalid map");
619                 return -EINVAL;
620         }
621         
622         
623         if ( (mem->card_start > 0x3ffffff) || (region.start > region.end) ||
624              (mem->speed > 1000) ) {
625                 leave("i82092aa_set_mem_map: invalid address / speed");
626                 printk("invalid mem map for socket %i: %llx to %llx with a "
627                         "start of %x\n",
628                         sock,
629                         (unsigned long long)region.start,
630                         (unsigned long long)region.end,
631                         mem->card_start);
632                 return -EINVAL;
633         }
634         
635         /* Turn off the window before changing anything */
636         if (indirect_read(sock, I365_ADDRWIN) & I365_ENA_MEM(map))
637                       indirect_resetbit(sock, I365_ADDRWIN, I365_ENA_MEM(map));
638                          
639                          
640 /*      printk("set_mem_map: Setting map %i range to %x - %x on socket %i, speed is %i, active = %i \n",map, region.start,region.end,sock,mem->speed,mem->flags & MAP_ACTIVE);  */
641
642         /* write the start address */
643         base = I365_MEM(map);
644         i = (region.start >> 12) & 0x0fff;
645         if (mem->flags & MAP_16BIT) 
646                 i |= I365_MEM_16BIT;
647         if (mem->flags & MAP_0WS)
648                 i |= I365_MEM_0WS;      
649         indirect_write16(sock,base+I365_W_START,i);
650                                
651         /* write the stop address */
652         
653         i= (region.end >> 12) & 0x0fff;
654         switch (to_cycles(mem->speed)) {
655                 case 0:
656                         break;
657                 case 1:
658                         i |= I365_MEM_WS0;
659                         break;
660                 case 2:
661                         i |= I365_MEM_WS1;
662                         break;
663                 default:
664                         i |= I365_MEM_WS1 | I365_MEM_WS0;
665                         break;
666         }
667         
668         indirect_write16(sock,base+I365_W_STOP,i);
669         
670         /* card start */
671         
672         i = ((mem->card_start - region.start) >> 12) & 0x3fff;
673         if (mem->flags & MAP_WRPROT)
674                 i |= I365_MEM_WRPROT;
675         if (mem->flags & MAP_ATTRIB) {
676 /*              printk("requesting attribute memory for socket %i\n",sock);*/
677                 i |= I365_MEM_REG;
678         } else {
679 /*              printk("requesting normal memory for socket %i\n",sock);*/
680         }
681         indirect_write16(sock,base+I365_W_OFF,i);
682         
683         /* Enable the window if necessary */
684         if (mem->flags & MAP_ACTIVE)
685                 indirect_setbit(sock, I365_ADDRWIN, I365_ENA_MEM(map));
686                     
687         leave("i82092aa_set_mem_map");
688         return 0;
689 }
690
691 static int i82092aa_module_init(void)
692 {
693         return pci_register_driver(&i82092aa_pci_driver);
694 }
695
696 static void i82092aa_module_exit(void)
697 {
698         enter("i82092aa_module_exit");
699         pci_unregister_driver(&i82092aa_pci_driver);
700         if (sockets[0].io_base>0)
701                          release_region(sockets[0].io_base, 2);
702         leave("i82092aa_module_exit");
703 }
704
705 module_init(i82092aa_module_init);
706 module_exit(i82092aa_module_exit);
707