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>
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,
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)
65 val = readb(socket->base + EXCA_REGS_BASE + offset);
66 val |= (u16)readb(socket->base + EXCA_REGS_BASE + offset + 1) << 8;
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)
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);
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;
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);
144 static int cardu_register_callback(unsigned int sock,
145 void (*handler)(void *, unsigned int),
148 vrc4173_socket_t *socket = &cardu_sockets[sock];
150 socket->handler = handler;
156 static int cardu_inquire_socket(unsigned int sock, socket_cap_t *cap)
158 vrc4173_socket_t *socket = &cardu_sockets[sock];
165 static int cardu_get_status(unsigned int sock, u_int *value)
167 vrc4173_socket_t *socket = &cardu_sockets[sock];
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))
178 if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
179 if (status & STSCHG) val |= SS_STSCHG;
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;
193 (VOL_YV_CARD_DT|VOL_XV_CARD_DT|VOL_3V_CARD_DT|VOL_5V_CARD_DT|CCD20|CCD10)))
201 static inline u_char get_Vcc_value(uint8_t val)
203 switch (val & VCC_MASK) {
213 static inline u_char get_Vpp_value(uint8_t val)
215 switch (val & VPP_MASK) {
219 return get_Vcc_value(val);
225 static int cardu_get_socket(unsigned int sock, socket_state_t *state)
227 vrc4173_socket_t *socket = &cardu_sockets[sock];
230 val = exca_readb(socket, PWR_CNT);
231 state->Vcc = get_Vcc_value(val);
232 state->Vpp = get_Vpp_value(val);
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;
243 static inline uint8_t set_Vcc_value(u_char Vcc)
255 static inline uint8_t set_Vpp_value(u_char Vpp)
268 static int cardu_set_socket(unsigned int sock, socket_state_t *state)
270 vrc4173_socket_t *socket = &cardu_sockets[sock];
273 if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
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);
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;
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;
314 static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
316 vrc4173_socket_t *socket = &cardu_sockets[sock];
318 uint8_t window, enable;
325 window = exca_readb(socket, ADR_WIN_EN);
326 enable = IO_WIN_EN(map);
328 if (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);
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;
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;
381 static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
383 vrc4173_socket_t *socket = &cardu_sockets[sock];
385 uint8_t window, enable;
386 u_long sys_start, sys_stop, card_start;
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) ||
398 window = exca_readb(socket, ADR_WIN_EN);
399 enable = MEM_WIN_EN(map);
400 if (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);
425 static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
429 static struct pccard_operations cardu_operations = {
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;
448 spin_lock_irq(&socket->event_lock);
449 events = socket->events;
451 spin_unlock_irq(&socket->event_lock);
454 socket->handler(socket->info, events);
457 static uint16_t get_events(vrc4173_socket_t *socket)
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)))
468 if ((csc & RDY_CHG) && (status & READY))
471 if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
472 if ((csc & BAT_DEAD_ST_CHG) && (status & STSCHG))
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;
486 static void cardu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
488 vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
491 INIT_WORK(&socket->tq_work, cardu_bh, socket);
493 events = get_events(socket);
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;
509 slot = vrc4173_cardu_slots++;
510 socket = &cardu_sockets[slot];
511 if (socket->noprobe != 0)
514 sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
516 if ((err = pci_enable_device(dev)) < 0)
519 start = pci_resource_start(dev, 0);
523 len = pci_resource_len(dev, 0);
527 if (((flags = pci_resource_flags(dev, 0)) & IORESOURCE_MEM) == 0)
530 if ((err = pci_request_regions(dev, socket->name)) < 0)
533 socket->base = ioremap(start, len);
534 if (socket->base == NULL)
539 socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
540 if (socket->pcmcia_socket == NULL) {
541 iounmap(socket->base);
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);
554 printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
559 static int __devinit vrc4173_cardu_setup(char *options)
561 if (options == NULL || *options == '\0')
564 if (strncmp(options, "cardu1:", 7) == 0) {
566 if (*options != '\0') {
567 if (strncmp(options, "noprobe", 7) == 0) {
568 cardu_sockets[CARDU1].noprobe = 1;
578 if (strncmp(options, "cardu2:", 7) == 0) {
580 if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
581 cardu_sockets[CARDU2].noprobe = 1;
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, },
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);