Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * vrc4171_card.c, NEC VRC4171 Card Controller driver for Socket Services. | |
3 | * | |
4 | * Copyright (C) 2003-2005 Yoichi Yuasa <yuasa@hh.iij4u.or.jp> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | #include <linux/init.h> | |
21 | #include <linux/ioport.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/spinlock.h> | |
25 | #include <linux/sched.h> | |
26 | #include <linux/types.h> | |
d052d1be | 27 | #include <linux/platform_device.h> |
1da177e4 LT |
28 | |
29 | #include <asm/io.h> | |
30 | ||
31 | #include <pcmcia/ss.h> | |
32 | ||
33 | #include "i82365.h" | |
34 | ||
35 | MODULE_DESCRIPTION("NEC VRC4171 Card Controllers driver for Socket Services"); | |
36 | MODULE_AUTHOR("Yoichi Yuasa <yuasa@hh.iij4u.or.jp>"); | |
37 | MODULE_LICENSE("GPL"); | |
38 | ||
39 | #define CARD_MAX_SLOTS 2 | |
40 | #define CARD_SLOTA 0 | |
41 | #define CARD_SLOTB 1 | |
42 | #define CARD_SLOTB_OFFSET 0x40 | |
43 | ||
44 | #define CARD_MEM_START 0x10000000 | |
45 | #define CARD_MEM_END 0x13ffffff | |
46 | #define CARD_MAX_MEM_OFFSET 0x3ffffff | |
47 | #define CARD_MAX_MEM_SPEED 1000 | |
48 | ||
49 | #define CARD_CONTROLLER_INDEX 0x03e0 | |
50 | #define CARD_CONTROLLER_DATA 0x03e1 | |
51 | /* Power register */ | |
52 | #define VPP_GET_VCC 0x01 | |
53 | #define POWER_ENABLE 0x10 | |
54 | #define CARD_VOLTAGE_SENSE 0x1f | |
55 | #define VCC_3VORXV_CAPABLE 0x00 | |
56 | #define VCC_XV_ONLY 0x01 | |
57 | #define VCC_3V_CAPABLE 0x02 | |
58 | #define VCC_5V_ONLY 0x03 | |
59 | #define CARD_VOLTAGE_SELECT 0x2f | |
60 | #define VCC_3V 0x01 | |
61 | #define VCC_5V 0x00 | |
62 | #define VCC_XV 0x02 | |
63 | #define VCC_STATUS_3V 0x02 | |
64 | #define VCC_STATUS_5V 0x01 | |
65 | #define VCC_STATUS_XV 0x03 | |
66 | #define GLOBAL_CONTROL 0x1e | |
67 | #define EXWRBK 0x04 | |
68 | #define IRQPM_EN 0x08 | |
69 | #define CLRPMIRQ 0x10 | |
70 | ||
71 | #define INTERRUPT_STATUS 0x05fa | |
72 | #define IRQ_A 0x02 | |
73 | #define IRQ_B 0x04 | |
74 | ||
75 | #define CONFIGURATION1 0x05fe | |
76 | #define SLOTB_CONFIG 0xc000 | |
77 | #define SLOTB_NONE 0x0000 | |
78 | #define SLOTB_PCCARD 0x4000 | |
79 | #define SLOTB_CF 0x8000 | |
80 | #define SLOTB_FLASHROM 0xc000 | |
81 | ||
82 | #define CARD_CONTROLLER_START CARD_CONTROLLER_INDEX | |
83 | #define CARD_CONTROLLER_END CARD_CONTROLLER_DATA | |
84 | ||
85 | #define IO_MAX_MAPS 2 | |
86 | #define MEM_MAX_MAPS 5 | |
87 | ||
88 | typedef enum { | |
89 | SLOT_PROBE = 0, | |
90 | SLOT_NOPROBE_IO, | |
91 | SLOT_NOPROBE_MEM, | |
92 | SLOT_NOPROBE_ALL, | |
93 | SLOT_INITIALIZED, | |
94 | } vrc4171_slot_t; | |
95 | ||
96 | typedef enum { | |
97 | SLOTB_IS_NONE, | |
98 | SLOTB_IS_PCCARD, | |
99 | SLOTB_IS_CF, | |
100 | SLOTB_IS_FLASHROM, | |
101 | } vrc4171_slotb_t; | |
102 | ||
103 | typedef struct vrc4171_socket { | |
104 | vrc4171_slot_t slot; | |
105 | struct pcmcia_socket pcmcia_socket; | |
106 | char name[24]; | |
107 | int csc_irq; | |
108 | int io_irq; | |
109 | } vrc4171_socket_t; | |
110 | ||
111 | static vrc4171_socket_t vrc4171_sockets[CARD_MAX_SLOTS]; | |
112 | static vrc4171_slotb_t vrc4171_slotb = SLOTB_IS_NONE; | |
113 | static char vrc4171_card_name[] = "NEC VRC4171 Card Controller"; | |
114 | static unsigned int vrc4171_irq; | |
115 | static uint16_t vrc4171_irq_mask = 0xdeb8; | |
116 | ||
117 | static struct resource vrc4171_card_resource[3] = { | |
118 | { .name = vrc4171_card_name, | |
119 | .start = CARD_CONTROLLER_START, | |
120 | .end = CARD_CONTROLLER_END, | |
121 | .flags = IORESOURCE_IO, }, | |
122 | { .name = vrc4171_card_name, | |
123 | .start = INTERRUPT_STATUS, | |
124 | .end = INTERRUPT_STATUS, | |
125 | .flags = IORESOURCE_IO, }, | |
126 | { .name = vrc4171_card_name, | |
127 | .start = CONFIGURATION1, | |
128 | .end = CONFIGURATION1, | |
129 | .flags = IORESOURCE_IO, }, | |
130 | }; | |
131 | ||
132 | static struct platform_device vrc4171_card_device = { | |
133 | .name = vrc4171_card_name, | |
134 | .id = 0, | |
135 | .num_resources = 3, | |
136 | .resource = vrc4171_card_resource, | |
137 | }; | |
138 | ||
139 | static inline uint16_t vrc4171_get_irq_status(void) | |
140 | { | |
141 | return inw(INTERRUPT_STATUS); | |
142 | } | |
143 | ||
144 | static inline void vrc4171_set_multifunction_pin(vrc4171_slotb_t config) | |
145 | { | |
146 | uint16_t config1; | |
147 | ||
148 | config1 = inw(CONFIGURATION1); | |
149 | config1 &= ~SLOTB_CONFIG; | |
150 | ||
151 | switch (config) { | |
152 | case SLOTB_IS_NONE: | |
153 | config1 |= SLOTB_NONE; | |
154 | break; | |
155 | case SLOTB_IS_PCCARD: | |
156 | config1 |= SLOTB_PCCARD; | |
157 | break; | |
158 | case SLOTB_IS_CF: | |
159 | config1 |= SLOTB_CF; | |
160 | break; | |
161 | case SLOTB_IS_FLASHROM: | |
162 | config1 |= SLOTB_FLASHROM; | |
163 | break; | |
164 | default: | |
165 | break; | |
166 | } | |
167 | ||
168 | outw(config1, CONFIGURATION1); | |
169 | } | |
170 | ||
171 | static inline uint8_t exca_read_byte(int slot, uint8_t index) | |
172 | { | |
173 | if (slot == CARD_SLOTB) | |
174 | index += CARD_SLOTB_OFFSET; | |
175 | ||
176 | outb(index, CARD_CONTROLLER_INDEX); | |
177 | return inb(CARD_CONTROLLER_DATA); | |
178 | } | |
179 | ||
180 | static inline uint16_t exca_read_word(int slot, uint8_t index) | |
181 | { | |
182 | uint16_t data; | |
183 | ||
184 | if (slot == CARD_SLOTB) | |
185 | index += CARD_SLOTB_OFFSET; | |
186 | ||
187 | outb(index++, CARD_CONTROLLER_INDEX); | |
188 | data = inb(CARD_CONTROLLER_DATA); | |
189 | ||
190 | outb(index, CARD_CONTROLLER_INDEX); | |
191 | data |= ((uint16_t)inb(CARD_CONTROLLER_DATA)) << 8; | |
192 | ||
193 | return data; | |
194 | } | |
195 | ||
196 | static inline uint8_t exca_write_byte(int slot, uint8_t index, uint8_t data) | |
197 | { | |
198 | if (slot == CARD_SLOTB) | |
199 | index += CARD_SLOTB_OFFSET; | |
200 | ||
201 | outb(index, CARD_CONTROLLER_INDEX); | |
202 | outb(data, CARD_CONTROLLER_DATA); | |
203 | ||
204 | return data; | |
205 | } | |
206 | ||
207 | static inline uint16_t exca_write_word(int slot, uint8_t index, uint16_t data) | |
208 | { | |
209 | if (slot == CARD_SLOTB) | |
210 | index += CARD_SLOTB_OFFSET; | |
211 | ||
212 | outb(index++, CARD_CONTROLLER_INDEX); | |
213 | outb(data, CARD_CONTROLLER_DATA); | |
214 | ||
215 | outb(index, CARD_CONTROLLER_INDEX); | |
216 | outb((uint8_t)(data >> 8), CARD_CONTROLLER_DATA); | |
217 | ||
218 | return data; | |
219 | } | |
220 | ||
221 | static inline int search_nonuse_irq(void) | |
222 | { | |
223 | int i; | |
224 | ||
225 | for (i = 0; i < 16; i++) { | |
226 | if (vrc4171_irq_mask & (1 << i)) { | |
227 | vrc4171_irq_mask &= ~(1 << i); | |
228 | return i; | |
229 | } | |
230 | } | |
231 | ||
232 | return -1; | |
233 | } | |
234 | ||
235 | static int pccard_init(struct pcmcia_socket *sock) | |
236 | { | |
237 | vrc4171_socket_t *socket; | |
238 | unsigned int slot; | |
239 | ||
240 | sock->features |= SS_CAP_PCCARD | SS_CAP_PAGE_REGS; | |
241 | sock->irq_mask = 0; | |
242 | sock->map_size = 0x1000; | |
243 | sock->pci_irq = vrc4171_irq; | |
244 | ||
245 | slot = sock->sock; | |
246 | socket = &vrc4171_sockets[slot]; | |
247 | socket->csc_irq = search_nonuse_irq(); | |
248 | socket->io_irq = search_nonuse_irq(); | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | static int pccard_get_status(struct pcmcia_socket *sock, u_int *value) | |
254 | { | |
255 | unsigned int slot; | |
256 | uint8_t status, sense; | |
257 | u_int val = 0; | |
258 | ||
259 | if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || value == NULL) | |
260 | return -EINVAL; | |
261 | ||
262 | slot = sock->sock; | |
263 | ||
264 | status = exca_read_byte(slot, I365_STATUS); | |
265 | if (exca_read_byte(slot, I365_INTCTL) & I365_PC_IOCARD) { | |
266 | if (status & I365_CS_STSCHG) | |
267 | val |= SS_STSCHG; | |
268 | } else { | |
269 | if (!(status & I365_CS_BVD1)) | |
270 | val |= SS_BATDEAD; | |
271 | else if ((status & (I365_CS_BVD1 | I365_CS_BVD2)) == I365_CS_BVD1) | |
272 | val |= SS_BATWARN; | |
273 | } | |
274 | if ((status & I365_CS_DETECT) == I365_CS_DETECT) | |
275 | val |= SS_DETECT; | |
276 | if (status & I365_CS_WRPROT) | |
277 | val |= SS_WRPROT; | |
278 | if (status & I365_CS_READY) | |
279 | val |= SS_READY; | |
280 | if (status & I365_CS_POWERON) | |
281 | val |= SS_POWERON; | |
282 | ||
283 | sense = exca_read_byte(slot, CARD_VOLTAGE_SENSE); | |
284 | switch (sense) { | |
285 | case VCC_3VORXV_CAPABLE: | |
286 | val |= SS_3VCARD | SS_XVCARD; | |
287 | break; | |
288 | case VCC_XV_ONLY: | |
289 | val |= SS_XVCARD; | |
290 | break; | |
291 | case VCC_3V_CAPABLE: | |
292 | val |= SS_3VCARD; | |
293 | break; | |
294 | default: | |
295 | /* 5V only */ | |
296 | break; | |
297 | } | |
298 | ||
299 | *value = val; | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static inline u_char get_Vcc_value(uint8_t voltage) | |
305 | { | |
306 | switch (voltage) { | |
307 | case VCC_STATUS_3V: | |
308 | return 33; | |
309 | case VCC_STATUS_5V: | |
310 | return 50; | |
311 | default: | |
312 | break; | |
313 | } | |
314 | ||
315 | return 0; | |
316 | } | |
317 | ||
318 | static inline u_char get_Vpp_value(uint8_t power, u_char Vcc) | |
319 | { | |
320 | if ((power & 0x03) == 0x01 || (power & 0x03) == 0x02) | |
321 | return Vcc; | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | static int pccard_get_socket(struct pcmcia_socket *sock, socket_state_t *state) | |
327 | { | |
328 | unsigned int slot; | |
329 | uint8_t power, voltage, control, cscint; | |
330 | ||
331 | if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || state == NULL) | |
332 | return -EINVAL; | |
333 | ||
334 | slot = sock->sock; | |
335 | ||
336 | power = exca_read_byte(slot, I365_POWER); | |
337 | voltage = exca_read_byte(slot, CARD_VOLTAGE_SELECT); | |
338 | ||
339 | state->Vcc = get_Vcc_value(voltage); | |
340 | state->Vpp = get_Vpp_value(power, state->Vcc); | |
341 | ||
342 | state->flags = 0; | |
343 | if (power & POWER_ENABLE) | |
344 | state->flags |= SS_PWR_AUTO; | |
345 | if (power & I365_PWR_OUT) | |
346 | state->flags |= SS_OUTPUT_ENA; | |
347 | ||
348 | control = exca_read_byte(slot, I365_INTCTL); | |
349 | if (control & I365_PC_IOCARD) | |
350 | state->flags |= SS_IOCARD; | |
351 | if (!(control & I365_PC_RESET)) | |
352 | state->flags |= SS_RESET; | |
353 | ||
354 | cscint = exca_read_byte(slot, I365_CSCINT); | |
355 | state->csc_mask = 0; | |
356 | if (state->flags & SS_IOCARD) { | |
357 | if (cscint & I365_CSC_STSCHG) | |
358 | state->flags |= SS_STSCHG; | |
359 | } else { | |
360 | if (cscint & I365_CSC_BVD1) | |
361 | state->csc_mask |= SS_BATDEAD; | |
362 | if (cscint & I365_CSC_BVD2) | |
363 | state->csc_mask |= SS_BATWARN; | |
364 | } | |
365 | if (cscint & I365_CSC_READY) | |
366 | state->csc_mask |= SS_READY; | |
367 | if (cscint & I365_CSC_DETECT) | |
368 | state->csc_mask |= SS_DETECT; | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
373 | static inline uint8_t set_Vcc_value(u_char Vcc) | |
374 | { | |
375 | switch (Vcc) { | |
376 | case 33: | |
377 | return VCC_3V; | |
378 | case 50: | |
379 | return VCC_5V; | |
380 | } | |
381 | ||
382 | /* Small voltage is chosen for safety. */ | |
383 | return VCC_3V; | |
384 | } | |
385 | ||
386 | static int pccard_set_socket(struct pcmcia_socket *sock, socket_state_t *state) | |
387 | { | |
388 | vrc4171_socket_t *socket; | |
389 | unsigned int slot; | |
390 | uint8_t voltage, power, control, cscint; | |
391 | ||
392 | if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || | |
393 | (state->Vpp != state->Vcc && state->Vpp != 0) || | |
394 | (state->Vcc != 50 && state->Vcc != 33 && state->Vcc != 0)) | |
395 | return -EINVAL; | |
396 | ||
397 | slot = sock->sock; | |
398 | socket = &vrc4171_sockets[slot]; | |
399 | ||
400 | spin_lock_irq(&sock->lock); | |
401 | ||
402 | voltage = set_Vcc_value(state->Vcc); | |
403 | exca_write_byte(slot, CARD_VOLTAGE_SELECT, voltage); | |
404 | ||
405 | power = POWER_ENABLE; | |
406 | if (state->Vpp == state->Vcc) | |
407 | power |= VPP_GET_VCC; | |
408 | if (state->flags & SS_OUTPUT_ENA) | |
409 | power |= I365_PWR_OUT; | |
410 | exca_write_byte(slot, I365_POWER, power); | |
411 | ||
412 | control = 0; | |
413 | if (state->io_irq != 0) | |
414 | control |= socket->io_irq; | |
415 | if (state->flags & SS_IOCARD) | |
416 | control |= I365_PC_IOCARD; | |
417 | if (state->flags & SS_RESET) | |
418 | control &= ~I365_PC_RESET; | |
419 | else | |
420 | control |= I365_PC_RESET; | |
421 | exca_write_byte(slot, I365_INTCTL, control); | |
422 | ||
423 | cscint = 0; | |
424 | exca_write_byte(slot, I365_CSCINT, cscint); | |
425 | exca_read_byte(slot, I365_CSC); /* clear CardStatus change */ | |
426 | if (state->csc_mask != 0) | |
427 | cscint |= socket->csc_irq << 8; | |
428 | if (state->flags & SS_IOCARD) { | |
429 | if (state->csc_mask & SS_STSCHG) | |
430 | cscint |= I365_CSC_STSCHG; | |
431 | } else { | |
432 | if (state->csc_mask & SS_BATDEAD) | |
433 | cscint |= I365_CSC_BVD1; | |
434 | if (state->csc_mask & SS_BATWARN) | |
435 | cscint |= I365_CSC_BVD2; | |
436 | } | |
437 | if (state->csc_mask & SS_READY) | |
438 | cscint |= I365_CSC_READY; | |
439 | if (state->csc_mask & SS_DETECT) | |
440 | cscint |= I365_CSC_DETECT; | |
441 | exca_write_byte(slot, I365_CSCINT, cscint); | |
442 | ||
443 | spin_unlock_irq(&sock->lock); | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | static int pccard_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *io) | |
449 | { | |
450 | unsigned int slot; | |
451 | uint8_t ioctl, addrwin; | |
452 | u_char map; | |
453 | ||
454 | if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || | |
455 | io == NULL || io->map >= IO_MAX_MAPS || | |
456 | io->start > 0xffff || io->stop > 0xffff || io->start > io->stop) | |
457 | return -EINVAL; | |
458 | ||
459 | slot = sock->sock; | |
460 | map = io->map; | |
461 | ||
462 | addrwin = exca_read_byte(slot, I365_ADDRWIN); | |
463 | if (addrwin & I365_ENA_IO(map)) { | |
464 | addrwin &= ~I365_ENA_IO(map); | |
465 | exca_write_byte(slot, I365_ADDRWIN, addrwin); | |
466 | } | |
467 | ||
468 | exca_write_word(slot, I365_IO(map)+I365_W_START, io->start); | |
469 | exca_write_word(slot, I365_IO(map)+I365_W_STOP, io->stop); | |
470 | ||
471 | ioctl = 0; | |
472 | if (io->speed > 0) | |
473 | ioctl |= I365_IOCTL_WAIT(map); | |
474 | if (io->flags & MAP_16BIT) | |
475 | ioctl |= I365_IOCTL_16BIT(map); | |
476 | if (io->flags & MAP_AUTOSZ) | |
477 | ioctl |= I365_IOCTL_IOCS16(map); | |
478 | if (io->flags & MAP_0WS) | |
479 | ioctl |= I365_IOCTL_0WS(map); | |
480 | exca_write_byte(slot, I365_IOCTL, ioctl); | |
481 | ||
482 | if (io->flags & MAP_ACTIVE) { | |
483 | addrwin |= I365_ENA_IO(map); | |
484 | exca_write_byte(slot, I365_ADDRWIN, addrwin); | |
485 | } | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
490 | static int pccard_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *mem) | |
491 | { | |
492 | unsigned int slot; | |
493 | uint16_t start, stop, offset; | |
494 | uint8_t addrwin; | |
495 | u_char map; | |
496 | ||
497 | if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || | |
498 | mem == NULL || mem->map >= MEM_MAX_MAPS || | |
499 | mem->res->start < CARD_MEM_START || mem->res->start > CARD_MEM_END || | |
500 | mem->res->end < CARD_MEM_START || mem->res->end > CARD_MEM_END || | |
501 | mem->res->start > mem->res->end || | |
502 | mem->card_start > CARD_MAX_MEM_OFFSET || | |
503 | mem->speed > CARD_MAX_MEM_SPEED) | |
504 | return -EINVAL; | |
505 | ||
506 | slot = sock->sock; | |
507 | map = mem->map; | |
508 | ||
509 | addrwin = exca_read_byte(slot, I365_ADDRWIN); | |
510 | if (addrwin & I365_ENA_MEM(map)) { | |
511 | addrwin &= ~I365_ENA_MEM(map); | |
512 | exca_write_byte(slot, I365_ADDRWIN, addrwin); | |
513 | } | |
514 | ||
515 | start = (mem->res->start >> 12) & 0x3fff; | |
516 | if (mem->flags & MAP_16BIT) | |
517 | start |= I365_MEM_16BIT; | |
518 | exca_write_word(slot, I365_MEM(map)+I365_W_START, start); | |
519 | ||
520 | stop = (mem->res->end >> 12) & 0x3fff; | |
521 | switch (mem->speed) { | |
522 | case 0: | |
523 | break; | |
524 | case 1: | |
525 | stop |= I365_MEM_WS0; | |
526 | break; | |
527 | case 2: | |
528 | stop |= I365_MEM_WS1; | |
529 | break; | |
530 | default: | |
531 | stop |= I365_MEM_WS0 | I365_MEM_WS1; | |
532 | break; | |
533 | } | |
534 | exca_write_word(slot, I365_MEM(map)+I365_W_STOP, stop); | |
535 | ||
536 | offset = (mem->card_start >> 12) & 0x3fff; | |
537 | if (mem->flags & MAP_ATTRIB) | |
538 | offset |= I365_MEM_REG; | |
539 | if (mem->flags & MAP_WRPROT) | |
540 | offset |= I365_MEM_WRPROT; | |
541 | exca_write_word(slot, I365_MEM(map)+I365_W_OFF, offset); | |
542 | ||
543 | if (mem->flags & MAP_ACTIVE) { | |
544 | addrwin |= I365_ENA_MEM(map); | |
545 | exca_write_byte(slot, I365_ADDRWIN, addrwin); | |
546 | } | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | static struct pccard_operations vrc4171_pccard_operations = { | |
552 | .init = pccard_init, | |
553 | .get_status = pccard_get_status, | |
554 | .get_socket = pccard_get_socket, | |
555 | .set_socket = pccard_set_socket, | |
556 | .set_io_map = pccard_set_io_map, | |
557 | .set_mem_map = pccard_set_mem_map, | |
558 | }; | |
559 | ||
560 | static inline unsigned int get_events(int slot) | |
561 | { | |
562 | unsigned int events = 0; | |
563 | uint8_t status, csc; | |
564 | ||
565 | status = exca_read_byte(slot, I365_STATUS); | |
566 | csc = exca_read_byte(slot, I365_CSC); | |
567 | ||
568 | if (exca_read_byte(slot, I365_INTCTL) & I365_PC_IOCARD) { | |
569 | if ((csc & I365_CSC_STSCHG) && (status & I365_CS_STSCHG)) | |
570 | events |= SS_STSCHG; | |
571 | } else { | |
572 | if (csc & (I365_CSC_BVD1 | I365_CSC_BVD2)) { | |
573 | if (!(status & I365_CS_BVD1)) | |
574 | events |= SS_BATDEAD; | |
575 | else if ((status & (I365_CS_BVD1 | I365_CS_BVD2)) == I365_CS_BVD1) | |
576 | events |= SS_BATWARN; | |
577 | } | |
578 | } | |
579 | if ((csc & I365_CSC_READY) && (status & I365_CS_READY)) | |
580 | events |= SS_READY; | |
581 | if ((csc & I365_CSC_DETECT) && ((status & I365_CS_DETECT) == I365_CS_DETECT)) | |
582 | events |= SS_DETECT; | |
583 | ||
584 | return events; | |
585 | } | |
586 | ||
587 | static irqreturn_t pccard_interrupt(int irq, void *dev_id, struct pt_regs *regs) | |
588 | { | |
589 | vrc4171_socket_t *socket; | |
590 | unsigned int events; | |
591 | irqreturn_t retval = IRQ_NONE; | |
592 | uint16_t status; | |
593 | ||
594 | status = vrc4171_get_irq_status(); | |
595 | if (status & IRQ_A) { | |
596 | socket = &vrc4171_sockets[CARD_SLOTA]; | |
597 | if (socket->slot == SLOT_INITIALIZED) { | |
598 | if (status & (1 << socket->csc_irq)) { | |
599 | events = get_events(CARD_SLOTA); | |
600 | if (events != 0) { | |
601 | pcmcia_parse_events(&socket->pcmcia_socket, events); | |
602 | retval = IRQ_HANDLED; | |
603 | } | |
604 | } | |
605 | } | |
606 | } | |
607 | ||
608 | if (status & IRQ_B) { | |
609 | socket = &vrc4171_sockets[CARD_SLOTB]; | |
610 | if (socket->slot == SLOT_INITIALIZED) { | |
611 | if (status & (1 << socket->csc_irq)) { | |
612 | events = get_events(CARD_SLOTB); | |
613 | if (events != 0) { | |
614 | pcmcia_parse_events(&socket->pcmcia_socket, events); | |
615 | retval = IRQ_HANDLED; | |
616 | } | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
621 | return retval; | |
622 | } | |
623 | ||
624 | static inline void reserve_using_irq(int slot) | |
625 | { | |
626 | unsigned int irq; | |
627 | ||
628 | irq = exca_read_byte(slot, I365_INTCTL); | |
629 | irq &= 0x0f; | |
630 | vrc4171_irq_mask &= ~(1 << irq); | |
631 | ||
632 | irq = exca_read_byte(slot, I365_CSCINT); | |
633 | irq = (irq & 0xf0) >> 4; | |
634 | vrc4171_irq_mask &= ~(1 << irq); | |
635 | } | |
636 | ||
637 | static int __devinit vrc4171_add_sockets(void) | |
638 | { | |
639 | vrc4171_socket_t *socket; | |
640 | int slot, retval; | |
641 | ||
642 | for (slot = 0; slot < CARD_MAX_SLOTS; slot++) { | |
643 | if (slot == CARD_SLOTB && vrc4171_slotb == SLOTB_IS_NONE) | |
644 | continue; | |
645 | ||
646 | socket = &vrc4171_sockets[slot]; | |
647 | if (socket->slot != SLOT_PROBE) { | |
648 | uint8_t addrwin; | |
649 | ||
650 | switch (socket->slot) { | |
651 | case SLOT_NOPROBE_MEM: | |
652 | addrwin = exca_read_byte(slot, I365_ADDRWIN); | |
653 | addrwin &= 0x1f; | |
654 | exca_write_byte(slot, I365_ADDRWIN, addrwin); | |
655 | break; | |
656 | case SLOT_NOPROBE_IO: | |
657 | addrwin = exca_read_byte(slot, I365_ADDRWIN); | |
658 | addrwin &= 0xc0; | |
659 | exca_write_byte(slot, I365_ADDRWIN, addrwin); | |
660 | break; | |
661 | default: | |
662 | break; | |
663 | } | |
664 | ||
665 | reserve_using_irq(slot); | |
666 | continue; | |
667 | } | |
668 | ||
669 | sprintf(socket->name, "NEC VRC4171 Card Slot %1c", 'A' + slot); | |
670 | socket->pcmcia_socket.dev.dev = &vrc4171_card_device.dev; | |
671 | socket->pcmcia_socket.ops = &vrc4171_pccard_operations; | |
672 | socket->pcmcia_socket.owner = THIS_MODULE; | |
673 | ||
674 | retval = pcmcia_register_socket(&socket->pcmcia_socket); | |
675 | if (retval < 0) | |
676 | return retval; | |
677 | ||
678 | exca_write_byte(slot, I365_ADDRWIN, 0); | |
679 | exca_write_byte(slot, GLOBAL_CONTROL, 0); | |
680 | ||
681 | socket->slot = SLOT_INITIALIZED; | |
682 | } | |
683 | ||
684 | return 0; | |
685 | } | |
686 | ||
687 | static void vrc4171_remove_sockets(void) | |
688 | { | |
689 | vrc4171_socket_t *socket; | |
690 | int slot; | |
691 | ||
692 | for (slot = 0; slot < CARD_MAX_SLOTS; slot++) { | |
693 | if (slot == CARD_SLOTB && vrc4171_slotb == SLOTB_IS_NONE) | |
694 | continue; | |
695 | ||
696 | socket = &vrc4171_sockets[slot]; | |
697 | if (socket->slot == SLOT_INITIALIZED) | |
698 | pcmcia_unregister_socket(&socket->pcmcia_socket); | |
699 | ||
700 | socket->slot = SLOT_PROBE; | |
701 | } | |
702 | } | |
703 | ||
704 | static int __devinit vrc4171_card_setup(char *options) | |
705 | { | |
706 | if (options == NULL || *options == '\0') | |
707 | return 0; | |
708 | ||
709 | if (strncmp(options, "irq:", 4) == 0) { | |
710 | int irq; | |
711 | options += 4; | |
712 | irq = simple_strtoul(options, &options, 0); | |
713 | if (irq >= 0 && irq < NR_IRQS) | |
714 | vrc4171_irq = irq; | |
715 | ||
716 | if (*options != ',') | |
717 | return 0; | |
718 | options++; | |
719 | } | |
720 | ||
721 | if (strncmp(options, "slota:", 6) == 0) { | |
722 | options += 6; | |
723 | if (*options != '\0') { | |
724 | if (strncmp(options, "memnoprobe", 10) == 0) { | |
725 | vrc4171_sockets[CARD_SLOTA].slot = SLOT_NOPROBE_MEM; | |
726 | options += 10; | |
727 | } else if (strncmp(options, "ionoprobe", 9) == 0) { | |
728 | vrc4171_sockets[CARD_SLOTA].slot = SLOT_NOPROBE_IO; | |
729 | options += 9; | |
730 | } else if ( strncmp(options, "noprobe", 7) == 0) { | |
731 | vrc4171_sockets[CARD_SLOTA].slot = SLOT_NOPROBE_ALL; | |
732 | options += 7; | |
733 | } | |
734 | ||
735 | if (*options != ',') | |
736 | return 0; | |
737 | options++; | |
738 | } else | |
739 | return 0; | |
740 | ||
741 | } | |
742 | ||
743 | if (strncmp(options, "slotb:", 6) == 0) { | |
744 | options += 6; | |
745 | if (*options != '\0') { | |
746 | if (strncmp(options, "pccard", 6) == 0) { | |
747 | vrc4171_slotb = SLOTB_IS_PCCARD; | |
748 | options += 6; | |
749 | } else if (strncmp(options, "cf", 2) == 0) { | |
750 | vrc4171_slotb = SLOTB_IS_CF; | |
751 | options += 2; | |
752 | } else if (strncmp(options, "flashrom", 8) == 0) { | |
753 | vrc4171_slotb = SLOTB_IS_FLASHROM; | |
754 | options += 8; | |
755 | } else if (strncmp(options, "none", 4) == 0) { | |
756 | vrc4171_slotb = SLOTB_IS_NONE; | |
757 | options += 4; | |
758 | } | |
759 | ||
760 | if (*options != ',') | |
761 | return 0; | |
762 | options++; | |
763 | ||
764 | if (strncmp(options, "memnoprobe", 10) == 0) | |
765 | vrc4171_sockets[CARD_SLOTB].slot = SLOT_NOPROBE_MEM; | |
766 | if (strncmp(options, "ionoprobe", 9) == 0) | |
767 | vrc4171_sockets[CARD_SLOTB].slot = SLOT_NOPROBE_IO; | |
768 | if (strncmp(options, "noprobe", 7) == 0) | |
769 | vrc4171_sockets[CARD_SLOTB].slot = SLOT_NOPROBE_ALL; | |
770 | } | |
771 | } | |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | __setup("vrc4171_card=", vrc4171_card_setup); | |
777 | ||
1da177e4 LT |
778 | static struct device_driver vrc4171_card_driver = { |
779 | .name = vrc4171_card_name, | |
780 | .bus = &platform_bus_type, | |
9480e307 RK |
781 | .suspend = pcmcia_socket_dev_suspend, |
782 | .resume = pcmcia_socket_dev_resume, | |
1da177e4 LT |
783 | }; |
784 | ||
785 | static int __devinit vrc4171_card_init(void) | |
786 | { | |
787 | int retval; | |
788 | ||
789 | retval = driver_register(&vrc4171_card_driver); | |
790 | if (retval < 0) | |
791 | return retval; | |
792 | ||
793 | retval = platform_device_register(&vrc4171_card_device); | |
794 | if (retval < 0) { | |
795 | driver_unregister(&vrc4171_card_driver); | |
796 | return retval; | |
797 | } | |
798 | ||
799 | vrc4171_set_multifunction_pin(vrc4171_slotb); | |
800 | ||
801 | retval = vrc4171_add_sockets(); | |
802 | if (retval == 0) | |
803 | retval = request_irq(vrc4171_irq, pccard_interrupt, SA_SHIRQ, | |
804 | vrc4171_card_name, vrc4171_sockets); | |
805 | ||
806 | if (retval < 0) { | |
807 | vrc4171_remove_sockets(); | |
808 | platform_device_unregister(&vrc4171_card_device); | |
809 | driver_unregister(&vrc4171_card_driver); | |
810 | return retval; | |
811 | } | |
812 | ||
813 | printk(KERN_INFO "%s, connected to IRQ %d\n", vrc4171_card_driver.name, vrc4171_irq); | |
814 | ||
815 | return 0; | |
816 | } | |
817 | ||
818 | static void __devexit vrc4171_card_exit(void) | |
819 | { | |
820 | free_irq(vrc4171_irq, vrc4171_sockets); | |
821 | vrc4171_remove_sockets(); | |
822 | platform_device_unregister(&vrc4171_card_device); | |
823 | driver_unregister(&vrc4171_card_driver); | |
824 | } | |
825 | ||
826 | module_init(vrc4171_card_init); | |
827 | module_exit(vrc4171_card_exit); |