blob: d19a9138135f0181b54ef034623bd2ce5dbd7118 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * FILE NAME
3 * drivers/pcmcia/vrc4173_cardu.c
4 *
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.)
8 *
Ralf Baechle29ce2c72005-12-12 20:11:50 +00009 * Copyright 2002,2003 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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.
15 *
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.
26 *
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.
30 */
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>
36
37#include <asm/io.h>
38
39#include <pcmcia/ss.h>
40
41#include "vrc4173_cardu.h"
42
43MODULE_DESCRIPTION("NEC VRC4173 CARDU driver for Socket Services");
Ralf Baechle29ce2c72005-12-12 20:11:50 +000044MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_LICENSE("GPL");
46
47static int vrc4173_cardu_slots;
48
49static vrc4173_socket_t cardu_sockets[CARDU_MAX_SOCKETS];
50
51extern struct socket_info_t *pcmcia_register_socket (int slot,
52 struct pccard_operations *vtable,
53 int use_bus_pm);
54extern void pcmcia_unregister_socket(struct socket_info_t *s);
55
56static inline uint8_t exca_readb(vrc4173_socket_t *socket, uint16_t offset)
57{
58 return readb(socket->base + EXCA_REGS_BASE + offset);
59}
60
61static inline uint16_t exca_readw(vrc4173_socket_t *socket, uint16_t offset)
62{
63 uint16_t val;
64
65 val = readb(socket->base + EXCA_REGS_BASE + offset);
66 val |= (u16)readb(socket->base + EXCA_REGS_BASE + offset + 1) << 8;
67
68 return val;
69}
70
71static inline void exca_writeb(vrc4173_socket_t *socket, uint16_t offset, uint8_t val)
72{
73 writeb(val, socket->base + EXCA_REGS_BASE + offset);
74}
75
76static inline void exca_writew(vrc4173_socket_t *socket, uint8_t offset, uint16_t val)
77{
78 writeb((u8)val, socket->base + EXCA_REGS_BASE + offset);
79 writeb((u8)(val >> 8), socket->base + EXCA_REGS_BASE + offset + 1);
80}
81
82static inline uint32_t cardbus_socket_readl(vrc4173_socket_t *socket, u16 offset)
83{
84 return readl(socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
85}
86
87static inline void cardbus_socket_writel(vrc4173_socket_t *socket, u16 offset, uint32_t val)
88{
89 writel(val, socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
90}
91
92static void cardu_pciregs_init(struct pci_dev *dev)
93{
94 u32 syscnt;
95 u16 brgcnt;
96 u8 devcnt;
97
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);
102
103 pci_read_config_word(dev, BRGCNT, &brgcnt);
104 brgcnt &= ~IREQ_INT;
105 pci_write_config_word(dev, BRGCNT, brgcnt);
106
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);
111
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);
116
117 pci_write_config_byte(dev, CHIPCNT, S_PREF_DISB);
118
119 pci_write_config_byte(dev, SERRDIS, 0);
120}
121
122static int cardu_init(unsigned int slot)
123{
124 vrc4173_socket_t *socket = &cardu_sockets[slot];
125
126 cardu_pciregs_init(socket->dev);
127
128 /* CARD_SC bits are cleared by reading CARD_SC. */
129 exca_writeb(socket, GLO_CNT, 0);
130
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);
137
138 /* Enable PC Card status interrupts */
139 exca_writeb(socket, CARD_SCI, CARD_DT_EN|RDY_EN|BAT_WAR_EN|BAT_DEAD_EN);
140
141 return 0;
142}
143
144static int cardu_register_callback(unsigned int sock,
145 void (*handler)(void *, unsigned int),
146 void * info)
147{
148 vrc4173_socket_t *socket = &cardu_sockets[sock];
149
150 socket->handler = handler;
151 socket->info = info;
152
153 return 0;
154}
155
156static int cardu_inquire_socket(unsigned int sock, socket_cap_t *cap)
157{
158 vrc4173_socket_t *socket = &cardu_sockets[sock];
159
160 *cap = socket->cap;
161
162 return 0;
163}
164
165static int cardu_get_status(unsigned int sock, u_int *value)
166{
167 vrc4173_socket_t *socket = &cardu_sockets[sock];
168 uint32_t state;
169 uint8_t status;
170 u_int val = 0;
171
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;
185 }
186 }
187
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;
195
196 *value = val;
197
198 return 0;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static inline uint8_t set_Vcc_value(u_char Vcc)
202{
203 switch (Vcc) {
204 case 33:
205 return VCC_3V;
206 case 50:
207 return VCC_5V;
208 }
209
210 return VCC_0V;
211}
212
213static inline uint8_t set_Vpp_value(u_char Vpp)
214{
215 switch (Vpp) {
216 case 33:
217 case 50:
218 return VPP_VCC;
219 case 120:
220 return VPP_12V;
221 }
222
223 return VPP_0V;
224}
225
226static int cardu_set_socket(unsigned int sock, socket_state_t *state)
227{
228 vrc4173_socket_t *socket = &cardu_sockets[sock];
229 uint8_t val;
230
231 if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
232 return -EINVAL;
233
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);
238
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);
244
245 return 0;
246}
247
248static int cardu_get_io_map(unsigned int sock, struct pccard_io_map *io)
249{
250 vrc4173_socket_t *socket = &cardu_sockets[sock];
251 uint8_t ioctl, window;
252 u_char map;
253
254 map = io->map;
255 if (map > 1)
256 return -EINVAL;
257
258 io->start = exca_readw(socket, IO_WIN_SA(map));
259 io->stop = exca_readw(socket, IO_WIN_EA(map));
260
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;
268
269 return 0;
270}
271
272static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
273{
274 vrc4173_socket_t *socket = &cardu_sockets[sock];
275 uint16_t ioctl;
276 uint8_t window, enable;
277 u_char map;
278
279 map = io->map;
280 if (map > 1)
281 return -EINVAL;
282
283 window = exca_readb(socket, ADR_WIN_EN);
284 enable = IO_WIN_EN(map);
285
286 if (window & enable) {
287 window &= ~enable;
288 exca_writeb(socket, ADR_WIN_EN, window);
289 }
290
291 exca_writew(socket, IO_WIN_SA(map), io->start);
292 exca_writew(socket, IO_WIN_EA(map), io->stop);
293
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);
298
299 if (io->flags & MAP_ACTIVE)
300 exca_writeb(socket, ADR_WIN_EN, window | enable);
301
302 return 0;
303}
304
305static int cardu_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
306{
307 vrc4173_socket_t *socket = &cardu_sockets[sock];
308 uint32_t start, stop, offset, page;
309 uint8_t window;
310 u_char map;
311
312 map = mem->map;
313 if (map > 4)
314 return -EINVAL;
315
316 window = exca_readb(socket, ADR_WIN_EN);
317 mem->flags = (window & MEM_WIN_EN(map)) ? MAP_ACTIVE : 0;
318
319 start = exca_readw(socket, MEM_WIN_SA(map));
320 mem->flags |= (start & MEM_WIN_DSIZE) ? MAP_16BIT : 0;
321 start = (start & 0x0fff) << 12;
322
323 stop = exca_readw(socket, MEM_WIN_EA(map));
324 stop = ((stop & 0x0fff) << 12) + 0x0fff;
325
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;
331
332 page = exca_readb(socket, MEM_WIN_SAU(map)) << 24;
333 mem->sys_start = start + page;
334 mem->sys_stop = start + page;
335
336 return 0;
337}
338
339static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
340{
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;
346
347 map = mem->map;
348 sys_start = mem->sys_start;
349 sys_stop = mem->sys_stop;
350 card_start = mem->card_start;
351
352 if (map > 4 || sys_start > sys_stop || ((sys_start ^ sys_stop) >> 24) ||
353 (card_start >> 26))
354 return -EINVAL;
355
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);
361 }
362
363 exca_writeb(socket, MEM_WIN_SAU(map), sys_start >> 24);
364
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);
368
369 value = (sys_stop >> 12) & 0x0fff;
370 exca_writew(socket, MEM_WIN_EA(map), value);
371
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);
376
377 if (mem->flags & MAP_ACTIVE)
378 exca_writeb(socket, ADR_WIN_EN, window | enable);
379
380 return 0;
381}
382
383static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
384{
385}
386
387static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 .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,
398};
399
400static void cardu_bh(void *data)
401{
402 vrc4173_socket_t *socket = (vrc4173_socket_t *)data;
403 uint16_t events;
404
405 spin_lock_irq(&socket->event_lock);
406 events = socket->events;
407 socket->events = 0;
408 spin_unlock_irq(&socket->event_lock);
409
410 if (socket->handler)
411 socket->handler(socket->info, events);
412}
413
414static uint16_t get_events(vrc4173_socket_t *socket)
415{
416 uint16_t events = 0;
417 uint8_t csc, status;
418
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;
424
425 if ((csc & RDY_CHG) && (status & READY))
426 events |= SS_READY;
427
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;
436 }
437 }
438 }
439
440 return events;
441}
442
443static void cardu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
444{
445 vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
446 uint16_t events;
447
448 INIT_WORK(&socket->tq_work, cardu_bh, socket);
449
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);
456 }
457}
458
459static int __devinit vrc4173_cardu_probe(struct pci_dev *dev,
460 const struct pci_device_id *ent)
461{
462 vrc4173_socket_t *socket;
463 unsigned long start, len, flags;
464 int slot, err;
465
466 slot = vrc4173_cardu_slots++;
467 socket = &cardu_sockets[slot];
468 if (socket->noprobe != 0)
469 return -EBUSY;
470
471 sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
472
473 if ((err = pci_enable_device(dev)) < 0)
474 return err;
475
476 start = pci_resource_start(dev, 0);
477 if (start == 0)
478 return -ENODEV;
479
480 len = pci_resource_len(dev, 0);
481 if (len == 0)
482 return -ENODEV;
483
484 if (((flags = pci_resource_flags(dev, 0)) & IORESOURCE_MEM) == 0)
485 return -EBUSY;
486
487 if ((err = pci_request_regions(dev, socket->name)) < 0)
488 return err;
489
490 socket->base = ioremap(start, len);
491 if (socket->base == NULL)
492 return -ENODEV;
493
494 socket->dev = dev;
495
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;
501 }
502
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700503 if (request_irq(dev->irq, cardu_interrupt, IRQF_SHARED, socket->name, socket) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 pcmcia_unregister_socket(socket->pcmcia_socket);
505 socket->pcmcia_socket = NULL;
506 iounmap(socket->base);
507 socket->base = NULL;
508 return -EBUSY;
509 }
510
511 printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
512
513 return 0;
514}
515
516static int __devinit vrc4173_cardu_setup(char *options)
517{
518 if (options == NULL || *options == '\0')
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800519 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
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;
527 }
528
529 if (*options != ',')
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800530 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 } else
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800532 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
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;
539 }
540
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800541 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544__setup("vrc4173_cardu=", vrc4173_cardu_setup);
545
546static 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, }
552};
553
554static struct pci_driver vrc4173_cardu_driver = {
555 .name = "NEC VRC4173 CARDU",
556 .probe = vrc4173_cardu_probe,
557 .id_table = vrc4173_cardu_id_table,
558};
559
560static int __devinit vrc4173_cardu_init(void)
561{
562 vrc4173_cardu_slots = 0;
563
Richard Knutsson93b47682005-11-30 01:00:35 +0100564 return pci_register_driver(&vrc4173_cardu_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567static void __devexit vrc4173_cardu_exit(void)
568{
569 pci_unregister_driver(&vrc4173_cardu_driver);
570}
571
572module_init(vrc4173_cardu_init);
573module_exit(vrc4173_cardu_exit);
574MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);