[IA64] Rationalise Region Definitions
[linux-2.6/sactl.git] / drivers / pcmcia / vrc4173_cardu.c
blobdb91259dc50eb33d0cd3480d01ffa3152a170cf9
1 /*
2 * FILE NAME
3 * drivers/pcmcia/vrc4173_cardu.c
5 * BRIEF MODULE DESCRIPTION
6 * NEC VRC4173 CARDU driver for Socket Services
7 * (This device doesn't support CardBus. it is supporting only 16bit PC Card.)
9 * Copyright 2002,2003 Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
24 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/spinlock.h>
35 #include <linux/types.h>
37 #include <asm/io.h>
39 #include <pcmcia/ss.h>
41 #include "vrc4173_cardu.h"
43 MODULE_DESCRIPTION("NEC VRC4173 CARDU driver for Socket Services");
44 MODULE_AUTHOR("Yoichi Yuasa <yuasa@hh.iij4u.or.jp>");
45 MODULE_LICENSE("GPL");
47 static int vrc4173_cardu_slots;
49 static vrc4173_socket_t cardu_sockets[CARDU_MAX_SOCKETS];
51 extern struct socket_info_t *pcmcia_register_socket (int slot,
52 struct pccard_operations *vtable,
53 int use_bus_pm);
54 extern void pcmcia_unregister_socket(struct socket_info_t *s);
56 static inline uint8_t exca_readb(vrc4173_socket_t *socket, uint16_t offset)
58 return readb(socket->base + EXCA_REGS_BASE + offset);
61 static inline uint16_t exca_readw(vrc4173_socket_t *socket, uint16_t offset)
63 uint16_t val;
65 val = readb(socket->base + EXCA_REGS_BASE + offset);
66 val |= (u16)readb(socket->base + EXCA_REGS_BASE + offset + 1) << 8;
68 return val;
71 static inline void exca_writeb(vrc4173_socket_t *socket, uint16_t offset, uint8_t val)
73 writeb(val, socket->base + EXCA_REGS_BASE + offset);
76 static inline void exca_writew(vrc4173_socket_t *socket, uint8_t offset, uint16_t val)
78 writeb((u8)val, socket->base + EXCA_REGS_BASE + offset);
79 writeb((u8)(val >> 8), socket->base + EXCA_REGS_BASE + offset + 1);
82 static inline uint32_t cardbus_socket_readl(vrc4173_socket_t *socket, u16 offset)
84 return readl(socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
87 static inline void cardbus_socket_writel(vrc4173_socket_t *socket, u16 offset, uint32_t val)
89 writel(val, socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
92 static void cardu_pciregs_init(struct pci_dev *dev)
94 u32 syscnt;
95 u16 brgcnt;
96 u8 devcnt;
98 pci_write_config_dword(dev, 0x1c, 0x10000000);
99 pci_write_config_dword(dev, 0x20, 0x17fff000);
100 pci_write_config_dword(dev, 0x2c, 0);
101 pci_write_config_dword(dev, 0x30, 0xfffc);
103 pci_read_config_word(dev, BRGCNT, &brgcnt);
104 brgcnt &= ~IREQ_INT;
105 pci_write_config_word(dev, BRGCNT, brgcnt);
107 pci_read_config_dword(dev, SYSCNT, &syscnt);
108 syscnt &= ~(BAD_VCC_REQ_DISB|PCPCI_EN|CH_ASSIGN_MASK|SUB_ID_WR_EN|PCI_CLK_RIN);
109 syscnt |= (CH_ASSIGN_NODMA|ASYN_INT_MODE);
110 pci_write_config_dword(dev, SYSCNT, syscnt);
112 pci_read_config_byte(dev, DEVCNT, &devcnt);
113 devcnt &= ~(ZOOM_VIDEO_EN|SR_PCI_INT_SEL_MASK|PCI_INT_MODE|IRQ_MODE);
114 devcnt |= (SR_PCI_INT_SEL_NONE|IFG);
115 pci_write_config_byte(dev, DEVCNT, devcnt);
117 pci_write_config_byte(dev, CHIPCNT, S_PREF_DISB);
119 pci_write_config_byte(dev, SERRDIS, 0);
122 static int cardu_init(unsigned int slot)
124 vrc4173_socket_t *socket = &cardu_sockets[slot];
126 cardu_pciregs_init(socket->dev);
128 /* CARD_SC bits are cleared by reading CARD_SC. */
129 exca_writeb(socket, GLO_CNT, 0);
131 socket->cap.features |= SS_CAP_PCCARD | SS_CAP_PAGE_REGS;
132 socket->cap.irq_mask = 0;
133 socket->cap.map_size = 0x1000;
134 socket->cap.pci_irq = socket->dev->irq;
135 socket->events = 0;
136 spin_lock_init(socket->event_lock);
138 /* Enable PC Card status interrupts */
139 exca_writeb(socket, CARD_SCI, CARD_DT_EN|RDY_EN|BAT_WAR_EN|BAT_DEAD_EN);
141 return 0;
144 static int cardu_register_callback(unsigned int sock,
145 void (*handler)(void *, unsigned int),
146 void * info)
148 vrc4173_socket_t *socket = &cardu_sockets[sock];
150 socket->handler = handler;
151 socket->info = info;
153 return 0;
156 static int cardu_inquire_socket(unsigned int sock, socket_cap_t *cap)
158 vrc4173_socket_t *socket = &cardu_sockets[sock];
160 *cap = socket->cap;
162 return 0;
165 static int cardu_get_status(unsigned int sock, u_int *value)
167 vrc4173_socket_t *socket = &cardu_sockets[sock];
168 uint32_t state;
169 uint8_t status;
170 u_int val = 0;
172 status = exca_readb(socket, IF_STATUS);
173 if (status & CARD_PWR) val |= SS_POWERON;
174 if (status & READY) val |= SS_READY;
175 if (status & CARD_WP) val |= SS_WRPROT;
176 if ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2))
177 val |= SS_DETECT;
178 if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
179 if (status & STSCHG) val |= SS_STSCHG;
180 } else {
181 status &= BV_DETECT_MASK;
182 if (status != BV_DETECT_GOOD) {
183 if (status == BV_DETECT_WARN) val |= SS_BATWARN;
184 else val |= SS_BATDEAD;
188 state = cardbus_socket_readl(socket, SKT_PRE_STATE);
189 if (state & VOL_3V_CARD_DT) val |= SS_3VCARD;
190 if (state & VOL_XV_CARD_DT) val |= SS_XVCARD;
191 if (state & CB_CARD_DT) val |= SS_CARDBUS;
192 if (!(state &
193 (VOL_YV_CARD_DT|VOL_XV_CARD_DT|VOL_3V_CARD_DT|VOL_5V_CARD_DT|CCD20|CCD10)))
194 val |= SS_PENDING;
196 *value = val;
198 return 0;
201 static inline u_char get_Vcc_value(uint8_t val)
203 switch (val & VCC_MASK) {
204 case VCC_3V:
205 return 33;
206 case VCC_5V:
207 return 50;
210 return 0;
213 static inline u_char get_Vpp_value(uint8_t val)
215 switch (val & VPP_MASK) {
216 case VPP_12V:
217 return 120;
218 case VPP_VCC:
219 return get_Vcc_value(val);
222 return 0;
225 static int cardu_get_socket(unsigned int sock, socket_state_t *state)
227 vrc4173_socket_t *socket = &cardu_sockets[sock];
228 uint8_t val;
230 val = exca_readb(socket, PWR_CNT);
231 state->Vcc = get_Vcc_value(val);
232 state->Vpp = get_Vpp_value(val);
233 state->flags = 0;
234 if (val & CARD_OUT_EN) state->flags |= SS_OUTPUT_ENA;
236 val = exca_readb(socket, INT_GEN_CNT);
237 if (!(val & CARD_REST0)) state->flags |= SS_RESET;
238 if (val & CARD_TYPE_IO) state->flags |= SS_IOCARD;
240 return 0;
243 static inline uint8_t set_Vcc_value(u_char Vcc)
245 switch (Vcc) {
246 case 33:
247 return VCC_3V;
248 case 50:
249 return VCC_5V;
252 return VCC_0V;
255 static inline uint8_t set_Vpp_value(u_char Vpp)
257 switch (Vpp) {
258 case 33:
259 case 50:
260 return VPP_VCC;
261 case 120:
262 return VPP_12V;
265 return VPP_0V;
268 static int cardu_set_socket(unsigned int sock, socket_state_t *state)
270 vrc4173_socket_t *socket = &cardu_sockets[sock];
271 uint8_t val;
273 if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
274 return -EINVAL;
276 val = set_Vcc_value(state->Vcc);
277 val |= set_Vpp_value(state->Vpp);
278 if (state->flags & SS_OUTPUT_ENA) val |= CARD_OUT_EN;
279 exca_writeb(socket, PWR_CNT, val);
281 val = exca_readb(socket, INT_GEN_CNT) & CARD_REST0;
282 if (state->flags & SS_RESET) val &= ~CARD_REST0;
283 else val |= CARD_REST0;
284 if (state->flags & SS_IOCARD) val |= CARD_TYPE_IO;
285 exca_writeb(socket, INT_GEN_CNT, val);
287 return 0;
290 static int cardu_get_io_map(unsigned int sock, struct pccard_io_map *io)
292 vrc4173_socket_t *socket = &cardu_sockets[sock];
293 uint8_t ioctl, window;
294 u_char map;
296 map = io->map;
297 if (map > 1)
298 return -EINVAL;
300 io->start = exca_readw(socket, IO_WIN_SA(map));
301 io->stop = exca_readw(socket, IO_WIN_EA(map));
303 ioctl = exca_readb(socket, IO_WIN_CNT);
304 window = exca_readb(socket, ADR_WIN_EN);
305 io->flags = (window & IO_WIN_EN(map)) ? MAP_ACTIVE : 0;
306 if (ioctl & IO_WIN_DATA_AUTOSZ(map))
307 io->flags |= MAP_AUTOSZ;
308 else if (ioctl & IO_WIN_DATA_16BIT(map))
309 io->flags |= MAP_16BIT;
311 return 0;
314 static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
316 vrc4173_socket_t *socket = &cardu_sockets[sock];
317 uint16_t ioctl;
318 uint8_t window, enable;
319 u_char map;
321 map = io->map;
322 if (map > 1)
323 return -EINVAL;
325 window = exca_readb(socket, ADR_WIN_EN);
326 enable = IO_WIN_EN(map);
328 if (window & enable) {
329 window &= ~enable;
330 exca_writeb(socket, ADR_WIN_EN, window);
333 exca_writew(socket, IO_WIN_SA(map), io->start);
334 exca_writew(socket, IO_WIN_EA(map), io->stop);
336 ioctl = exca_readb(socket, IO_WIN_CNT) & ~IO_WIN_CNT_MASK(map);
337 if (io->flags & MAP_AUTOSZ) ioctl |= IO_WIN_DATA_AUTOSZ(map);
338 else if (io->flags & MAP_16BIT) ioctl |= IO_WIN_DATA_16BIT(map);
339 exca_writeb(socket, IO_WIN_CNT, ioctl);
341 if (io->flags & MAP_ACTIVE)
342 exca_writeb(socket, ADR_WIN_EN, window | enable);
344 return 0;
347 static int cardu_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
349 vrc4173_socket_t *socket = &cardu_sockets[sock];
350 uint32_t start, stop, offset, page;
351 uint8_t window;
352 u_char map;
354 map = mem->map;
355 if (map > 4)
356 return -EINVAL;
358 window = exca_readb(socket, ADR_WIN_EN);
359 mem->flags = (window & MEM_WIN_EN(map)) ? MAP_ACTIVE : 0;
361 start = exca_readw(socket, MEM_WIN_SA(map));
362 mem->flags |= (start & MEM_WIN_DSIZE) ? MAP_16BIT : 0;
363 start = (start & 0x0fff) << 12;
365 stop = exca_readw(socket, MEM_WIN_EA(map));
366 stop = ((stop & 0x0fff) << 12) + 0x0fff;
368 offset = exca_readw(socket, MEM_WIN_OA(map));
369 mem->flags |= (offset & MEM_WIN_WP) ? MAP_WRPROT : 0;
370 mem->flags |= (offset & MEM_WIN_REGSET) ? MAP_ATTRIB : 0;
371 offset = ((offset & 0x3fff) << 12) + start;
372 mem->card_start = offset & 0x03ffffff;
374 page = exca_readb(socket, MEM_WIN_SAU(map)) << 24;
375 mem->sys_start = start + page;
376 mem->sys_stop = start + page;
378 return 0;
381 static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
383 vrc4173_socket_t *socket = &cardu_sockets[sock];
384 uint16_t value;
385 uint8_t window, enable;
386 u_long sys_start, sys_stop, card_start;
387 u_char map;
389 map = mem->map;
390 sys_start = mem->sys_start;
391 sys_stop = mem->sys_stop;
392 card_start = mem->card_start;
394 if (map > 4 || sys_start > sys_stop || ((sys_start ^ sys_stop) >> 24) ||
395 (card_start >> 26))
396 return -EINVAL;
398 window = exca_readb(socket, ADR_WIN_EN);
399 enable = MEM_WIN_EN(map);
400 if (window & enable) {
401 window &= ~enable;
402 exca_writeb(socket, ADR_WIN_EN, window);
405 exca_writeb(socket, MEM_WIN_SAU(map), sys_start >> 24);
407 value = (sys_start >> 12) & 0x0fff;
408 if (mem->flags & MAP_16BIT) value |= MEM_WIN_DSIZE;
409 exca_writew(socket, MEM_WIN_SA(map), value);
411 value = (sys_stop >> 12) & 0x0fff;
412 exca_writew(socket, MEM_WIN_EA(map), value);
414 value = ((card_start - sys_start) >> 12) & 0x3fff;
415 if (mem->flags & MAP_WRPROT) value |= MEM_WIN_WP;
416 if (mem->flags & MAP_ATTRIB) value |= MEM_WIN_REGSET;
417 exca_writew(socket, MEM_WIN_OA(map), value);
419 if (mem->flags & MAP_ACTIVE)
420 exca_writeb(socket, ADR_WIN_EN, window | enable);
422 return 0;
425 static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
429 static struct pccard_operations cardu_operations = {
430 .init = cardu_init,
431 .register_callback = cardu_register_callback,
432 .inquire_socket = cardu_inquire_socket,
433 .get_status = cardu_get_status,
434 .get_socket = cardu_get_socket,
435 .set_socket = cardu_set_socket,
436 .get_io_map = cardu_get_io_map,
437 .set_io_map = cardu_set_io_map,
438 .get_mem_map = cardu_get_mem_map,
439 .set_mem_map = cardu_set_mem_map,
440 .proc_setup = cardu_proc_setup,
443 static void cardu_bh(void *data)
445 vrc4173_socket_t *socket = (vrc4173_socket_t *)data;
446 uint16_t events;
448 spin_lock_irq(&socket->event_lock);
449 events = socket->events;
450 socket->events = 0;
451 spin_unlock_irq(&socket->event_lock);
453 if (socket->handler)
454 socket->handler(socket->info, events);
457 static uint16_t get_events(vrc4173_socket_t *socket)
459 uint16_t events = 0;
460 uint8_t csc, status;
462 status = exca_readb(socket, IF_STATUS);
463 csc = exca_readb(socket, CARD_SC);
464 if ((csc & CARD_DT_CHG) &&
465 ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2)))
466 events |= SS_DETECT;
468 if ((csc & RDY_CHG) && (status & READY))
469 events |= SS_READY;
471 if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
472 if ((csc & BAT_DEAD_ST_CHG) && (status & STSCHG))
473 events |= SS_STSCHG;
474 } else {
475 if (csc & (BAT_WAR_CHG|BAT_DEAD_ST_CHG)) {
476 if ((status & BV_DETECT_MASK) != BV_DETECT_GOOD) {
477 if (status == BV_DETECT_WARN) events |= SS_BATWARN;
478 else events |= SS_BATDEAD;
483 return events;
486 static void cardu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
488 vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
489 uint16_t events;
491 INIT_WORK(&socket->tq_work, cardu_bh, socket);
493 events = get_events(socket);
494 if (events) {
495 spin_lock(&socket->event_lock);
496 socket->events |= events;
497 spin_unlock(&socket->event_lock);
498 schedule_work(&socket->tq_work);
502 static int __devinit vrc4173_cardu_probe(struct pci_dev *dev,
503 const struct pci_device_id *ent)
505 vrc4173_socket_t *socket;
506 unsigned long start, len, flags;
507 int slot, err;
509 slot = vrc4173_cardu_slots++;
510 socket = &cardu_sockets[slot];
511 if (socket->noprobe != 0)
512 return -EBUSY;
514 sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
516 if ((err = pci_enable_device(dev)) < 0)
517 return err;
519 start = pci_resource_start(dev, 0);
520 if (start == 0)
521 return -ENODEV;
523 len = pci_resource_len(dev, 0);
524 if (len == 0)
525 return -ENODEV;
527 if (((flags = pci_resource_flags(dev, 0)) & IORESOURCE_MEM) == 0)
528 return -EBUSY;
530 if ((err = pci_request_regions(dev, socket->name)) < 0)
531 return err;
533 socket->base = ioremap(start, len);
534 if (socket->base == NULL)
535 return -ENODEV;
537 socket->dev = dev;
539 socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
540 if (socket->pcmcia_socket == NULL) {
541 iounmap(socket->base);
542 socket->base = NULL;
543 return -ENOMEM;
546 if (request_irq(dev->irq, cardu_interrupt, SA_SHIRQ, socket->name, socket) < 0) {
547 pcmcia_unregister_socket(socket->pcmcia_socket);
548 socket->pcmcia_socket = NULL;
549 iounmap(socket->base);
550 socket->base = NULL;
551 return -EBUSY;
554 printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
556 return 0;
559 static int __devinit vrc4173_cardu_setup(char *options)
561 if (options == NULL || *options == '\0')
562 return 0;
564 if (strncmp(options, "cardu1:", 7) == 0) {
565 options += 7;
566 if (*options != '\0') {
567 if (strncmp(options, "noprobe", 7) == 0) {
568 cardu_sockets[CARDU1].noprobe = 1;
569 options += 7;
572 if (*options != ',')
573 return 0;
574 } else
575 return 0;
578 if (strncmp(options, "cardu2:", 7) == 0) {
579 options += 7;
580 if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
581 cardu_sockets[CARDU2].noprobe = 1;
584 return 0;
587 __setup("vrc4173_cardu=", vrc4173_cardu_setup);
589 static struct pci_device_id vrc4173_cardu_id_table[] __devinitdata = {
590 { .vendor = PCI_VENDOR_ID_NEC,
591 .device = PCI_DEVICE_ID_NEC_NAPCCARD,
592 .subvendor = PCI_ANY_ID,
593 .subdevice = PCI_ANY_ID, },
594 {0, }
597 static struct pci_driver vrc4173_cardu_driver = {
598 .name = "NEC VRC4173 CARDU",
599 .probe = vrc4173_cardu_probe,
600 .id_table = vrc4173_cardu_id_table,
603 static int __devinit vrc4173_cardu_init(void)
605 vrc4173_cardu_slots = 0;
607 return pci_module_init(&vrc4173_cardu_driver);
610 static void __devexit vrc4173_cardu_exit(void)
612 pci_unregister_driver(&vrc4173_cardu_driver);
615 module_init(vrc4173_cardu_init);
616 module_exit(vrc4173_cardu_exit);
617 MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);