Linux 3.3-rc6
[linux/fpc-iii.git] / drivers / pcmcia / vrc4173_cardu.c
blobc6d36b3a6ce89b04fb84540be5a32203fb5405b2
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@linux-mips.org>
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@linux-mips.org>");
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 uint8_t set_Vcc_value(u_char Vcc)
203 switch (Vcc) {
204 case 33:
205 return VCC_3V;
206 case 50:
207 return VCC_5V;
210 return VCC_0V;
213 static inline uint8_t set_Vpp_value(u_char Vpp)
215 switch (Vpp) {
216 case 33:
217 case 50:
218 return VPP_VCC;
219 case 120:
220 return VPP_12V;
223 return VPP_0V;
226 static int cardu_set_socket(unsigned int sock, socket_state_t *state)
228 vrc4173_socket_t *socket = &cardu_sockets[sock];
229 uint8_t val;
231 if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
232 return -EINVAL;
234 val = set_Vcc_value(state->Vcc);
235 val |= set_Vpp_value(state->Vpp);
236 if (state->flags & SS_OUTPUT_ENA) val |= CARD_OUT_EN;
237 exca_writeb(socket, PWR_CNT, val);
239 val = exca_readb(socket, INT_GEN_CNT) & CARD_REST0;
240 if (state->flags & SS_RESET) val &= ~CARD_REST0;
241 else val |= CARD_REST0;
242 if (state->flags & SS_IOCARD) val |= CARD_TYPE_IO;
243 exca_writeb(socket, INT_GEN_CNT, val);
245 return 0;
248 static int cardu_get_io_map(unsigned int sock, struct pccard_io_map *io)
250 vrc4173_socket_t *socket = &cardu_sockets[sock];
251 uint8_t ioctl, window;
252 u_char map;
254 map = io->map;
255 if (map > 1)
256 return -EINVAL;
258 io->start = exca_readw(socket, IO_WIN_SA(map));
259 io->stop = exca_readw(socket, IO_WIN_EA(map));
261 ioctl = exca_readb(socket, IO_WIN_CNT);
262 window = exca_readb(socket, ADR_WIN_EN);
263 io->flags = (window & IO_WIN_EN(map)) ? MAP_ACTIVE : 0;
264 if (ioctl & IO_WIN_DATA_AUTOSZ(map))
265 io->flags |= MAP_AUTOSZ;
266 else if (ioctl & IO_WIN_DATA_16BIT(map))
267 io->flags |= MAP_16BIT;
269 return 0;
272 static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
274 vrc4173_socket_t *socket = &cardu_sockets[sock];
275 uint16_t ioctl;
276 uint8_t window, enable;
277 u_char map;
279 map = io->map;
280 if (map > 1)
281 return -EINVAL;
283 window = exca_readb(socket, ADR_WIN_EN);
284 enable = IO_WIN_EN(map);
286 if (window & enable) {
287 window &= ~enable;
288 exca_writeb(socket, ADR_WIN_EN, window);
291 exca_writew(socket, IO_WIN_SA(map), io->start);
292 exca_writew(socket, IO_WIN_EA(map), io->stop);
294 ioctl = exca_readb(socket, IO_WIN_CNT) & ~IO_WIN_CNT_MASK(map);
295 if (io->flags & MAP_AUTOSZ) ioctl |= IO_WIN_DATA_AUTOSZ(map);
296 else if (io->flags & MAP_16BIT) ioctl |= IO_WIN_DATA_16BIT(map);
297 exca_writeb(socket, IO_WIN_CNT, ioctl);
299 if (io->flags & MAP_ACTIVE)
300 exca_writeb(socket, ADR_WIN_EN, window | enable);
302 return 0;
305 static int cardu_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
307 vrc4173_socket_t *socket = &cardu_sockets[sock];
308 uint32_t start, stop, offset, page;
309 uint8_t window;
310 u_char map;
312 map = mem->map;
313 if (map > 4)
314 return -EINVAL;
316 window = exca_readb(socket, ADR_WIN_EN);
317 mem->flags = (window & MEM_WIN_EN(map)) ? MAP_ACTIVE : 0;
319 start = exca_readw(socket, MEM_WIN_SA(map));
320 mem->flags |= (start & MEM_WIN_DSIZE) ? MAP_16BIT : 0;
321 start = (start & 0x0fff) << 12;
323 stop = exca_readw(socket, MEM_WIN_EA(map));
324 stop = ((stop & 0x0fff) << 12) + 0x0fff;
326 offset = exca_readw(socket, MEM_WIN_OA(map));
327 mem->flags |= (offset & MEM_WIN_WP) ? MAP_WRPROT : 0;
328 mem->flags |= (offset & MEM_WIN_REGSET) ? MAP_ATTRIB : 0;
329 offset = ((offset & 0x3fff) << 12) + start;
330 mem->card_start = offset & 0x03ffffff;
332 page = exca_readb(socket, MEM_WIN_SAU(map)) << 24;
333 mem->sys_start = start + page;
334 mem->sys_stop = start + page;
336 return 0;
339 static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
341 vrc4173_socket_t *socket = &cardu_sockets[sock];
342 uint16_t value;
343 uint8_t window, enable;
344 u_long sys_start, sys_stop, card_start;
345 u_char map;
347 map = mem->map;
348 sys_start = mem->sys_start;
349 sys_stop = mem->sys_stop;
350 card_start = mem->card_start;
352 if (map > 4 || sys_start > sys_stop || ((sys_start ^ sys_stop) >> 24) ||
353 (card_start >> 26))
354 return -EINVAL;
356 window = exca_readb(socket, ADR_WIN_EN);
357 enable = MEM_WIN_EN(map);
358 if (window & enable) {
359 window &= ~enable;
360 exca_writeb(socket, ADR_WIN_EN, window);
363 exca_writeb(socket, MEM_WIN_SAU(map), sys_start >> 24);
365 value = (sys_start >> 12) & 0x0fff;
366 if (mem->flags & MAP_16BIT) value |= MEM_WIN_DSIZE;
367 exca_writew(socket, MEM_WIN_SA(map), value);
369 value = (sys_stop >> 12) & 0x0fff;
370 exca_writew(socket, MEM_WIN_EA(map), value);
372 value = ((card_start - sys_start) >> 12) & 0x3fff;
373 if (mem->flags & MAP_WRPROT) value |= MEM_WIN_WP;
374 if (mem->flags & MAP_ATTRIB) value |= MEM_WIN_REGSET;
375 exca_writew(socket, MEM_WIN_OA(map), value);
377 if (mem->flags & MAP_ACTIVE)
378 exca_writeb(socket, ADR_WIN_EN, window | enable);
380 return 0;
383 static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
387 static struct pccard_operations cardu_operations = {
388 .init = cardu_init,
389 .register_callback = cardu_register_callback,
390 .inquire_socket = cardu_inquire_socket,
391 .get_status = cardu_get_status,
392 .set_socket = cardu_set_socket,
393 .get_io_map = cardu_get_io_map,
394 .set_io_map = cardu_set_io_map,
395 .get_mem_map = cardu_get_mem_map,
396 .set_mem_map = cardu_set_mem_map,
397 .proc_setup = cardu_proc_setup,
400 static void cardu_bh(void *data)
402 vrc4173_socket_t *socket = (vrc4173_socket_t *)data;
403 uint16_t events;
405 spin_lock_irq(&socket->event_lock);
406 events = socket->events;
407 socket->events = 0;
408 spin_unlock_irq(&socket->event_lock);
410 if (socket->handler)
411 socket->handler(socket->info, events);
414 static uint16_t get_events(vrc4173_socket_t *socket)
416 uint16_t events = 0;
417 uint8_t csc, status;
419 status = exca_readb(socket, IF_STATUS);
420 csc = exca_readb(socket, CARD_SC);
421 if ((csc & CARD_DT_CHG) &&
422 ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2)))
423 events |= SS_DETECT;
425 if ((csc & RDY_CHG) && (status & READY))
426 events |= SS_READY;
428 if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
429 if ((csc & BAT_DEAD_ST_CHG) && (status & STSCHG))
430 events |= SS_STSCHG;
431 } else {
432 if (csc & (BAT_WAR_CHG|BAT_DEAD_ST_CHG)) {
433 if ((status & BV_DETECT_MASK) != BV_DETECT_GOOD) {
434 if (status == BV_DETECT_WARN) events |= SS_BATWARN;
435 else events |= SS_BATDEAD;
440 return events;
443 static void cardu_interrupt(int irq, void *dev_id)
445 vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
446 uint16_t events;
448 INIT_WORK(&socket->tq_work, cardu_bh, socket);
450 events = get_events(socket);
451 if (events) {
452 spin_lock(&socket->event_lock);
453 socket->events |= events;
454 spin_unlock(&socket->event_lock);
455 schedule_work(&socket->tq_work);
459 static int __devinit vrc4173_cardu_probe(struct pci_dev *dev,
460 const struct pci_device_id *ent)
462 vrc4173_socket_t *socket;
463 unsigned long start, len, flags;
464 int slot, err, ret;
466 slot = vrc4173_cardu_slots++;
467 socket = &cardu_sockets[slot];
468 if (socket->noprobe != 0)
469 return -EBUSY;
471 sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
473 if ((err = pci_enable_device(dev)) < 0)
474 return err;
476 start = pci_resource_start(dev, 0);
477 if (start == 0) {
478 ret = -ENODEV;
479 goto disable;
482 len = pci_resource_len(dev, 0);
483 if (len == 0) {
484 ret = -ENODEV;
485 goto disable;
488 flags = pci_resource_flags(dev, 0);
489 if ((flags & IORESOURCE_MEM) == 0) {
490 ret = -EBUSY;
491 goto disable;
494 err = pci_request_regions(dev, socket->name);
495 if (err < 0) {
496 ret = err;
497 goto disable;
500 socket->base = ioremap(start, len);
501 if (socket->base == NULL) {
502 ret = -ENODEV;
503 goto release;
506 socket->dev = dev;
508 socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
509 if (socket->pcmcia_socket == NULL) {
510 ret = -ENOMEM;
511 goto unmap;
514 if (request_irq(dev->irq, cardu_interrupt, IRQF_SHARED, socket->name, socket) < 0) {
515 ret = -EBUSY;
516 goto unregister;
519 printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
521 return 0;
523 unregister:
524 pcmcia_unregister_socket(socket->pcmcia_socket);
525 socket->pcmcia_socket = NULL;
526 unmap:
527 iounmap(socket->base);
528 socket->base = NULL;
529 release:
530 pci_release_regions(dev);
531 disable:
532 pci_disable_device(dev);
533 return ret;
536 static int __devinit vrc4173_cardu_setup(char *options)
538 if (options == NULL || *options == '\0')
539 return 1;
541 if (strncmp(options, "cardu1:", 7) == 0) {
542 options += 7;
543 if (*options != '\0') {
544 if (strncmp(options, "noprobe", 7) == 0) {
545 cardu_sockets[CARDU1].noprobe = 1;
546 options += 7;
549 if (*options != ',')
550 return 1;
551 } else
552 return 1;
555 if (strncmp(options, "cardu2:", 7) == 0) {
556 options += 7;
557 if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
558 cardu_sockets[CARDU2].noprobe = 1;
561 return 1;
564 __setup("vrc4173_cardu=", vrc4173_cardu_setup);
566 static struct pci_device_id vrc4173_cardu_id_table[] __devinitdata = {
567 { .vendor = PCI_VENDOR_ID_NEC,
568 .device = PCI_DEVICE_ID_NEC_NAPCCARD,
569 .subvendor = PCI_ANY_ID,
570 .subdevice = PCI_ANY_ID, },
571 {0, }
574 static struct pci_driver vrc4173_cardu_driver = {
575 .name = "NEC VRC4173 CARDU",
576 .probe = vrc4173_cardu_probe,
577 .id_table = vrc4173_cardu_id_table,
580 static int __devinit vrc4173_cardu_init(void)
582 vrc4173_cardu_slots = 0;
584 return pci_register_driver(&vrc4173_cardu_driver);
587 static void __devexit vrc4173_cardu_exit(void)
589 pci_unregister_driver(&vrc4173_cardu_driver);
592 module_init(vrc4173_cardu_init);
593 module_exit(vrc4173_cardu_exit);
594 MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);