[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / pcmcia / vrc4173_cardu.c
blob9b3c15827e5c763208a82b212eb7ab6cc2e9919f
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;
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 return -ENODEV;
480 len = pci_resource_len(dev, 0);
481 if (len == 0)
482 return -ENODEV;
484 if (((flags = pci_resource_flags(dev, 0)) & IORESOURCE_MEM) == 0)
485 return -EBUSY;
487 if ((err = pci_request_regions(dev, socket->name)) < 0)
488 return err;
490 socket->base = ioremap(start, len);
491 if (socket->base == NULL)
492 return -ENODEV;
494 socket->dev = dev;
496 socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
497 if (socket->pcmcia_socket == NULL) {
498 iounmap(socket->base);
499 socket->base = NULL;
500 return -ENOMEM;
503 if (request_irq(dev->irq, cardu_interrupt, IRQF_SHARED, socket->name, socket) < 0) {
504 pcmcia_unregister_socket(socket->pcmcia_socket);
505 socket->pcmcia_socket = NULL;
506 iounmap(socket->base);
507 socket->base = NULL;
508 return -EBUSY;
511 printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
513 return 0;
516 static int __devinit vrc4173_cardu_setup(char *options)
518 if (options == NULL || *options == '\0')
519 return 1;
521 if (strncmp(options, "cardu1:", 7) == 0) {
522 options += 7;
523 if (*options != '\0') {
524 if (strncmp(options, "noprobe", 7) == 0) {
525 cardu_sockets[CARDU1].noprobe = 1;
526 options += 7;
529 if (*options != ',')
530 return 1;
531 } else
532 return 1;
535 if (strncmp(options, "cardu2:", 7) == 0) {
536 options += 7;
537 if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
538 cardu_sockets[CARDU2].noprobe = 1;
541 return 1;
544 __setup("vrc4173_cardu=", vrc4173_cardu_setup);
546 static struct pci_device_id vrc4173_cardu_id_table[] __devinitdata = {
547 { .vendor = PCI_VENDOR_ID_NEC,
548 .device = PCI_DEVICE_ID_NEC_NAPCCARD,
549 .subvendor = PCI_ANY_ID,
550 .subdevice = PCI_ANY_ID, },
551 {0, }
554 static struct pci_driver vrc4173_cardu_driver = {
555 .name = "NEC VRC4173 CARDU",
556 .probe = vrc4173_cardu_probe,
557 .id_table = vrc4173_cardu_id_table,
560 static int __devinit vrc4173_cardu_init(void)
562 vrc4173_cardu_slots = 0;
564 return pci_register_driver(&vrc4173_cardu_driver);
567 static void __devexit vrc4173_cardu_exit(void)
569 pci_unregister_driver(&vrc4173_cardu_driver);
572 module_init(vrc4173_cardu_init);
573 module_exit(vrc4173_cardu_exit);
574 MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);