2 * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards.
4 * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
8 /* This is a fairly generic PCMCIA socket driver suitable for the
9 * following Alchemy Development boards:
10 * Db1000, Db/Pb1500, Db/Pb1100, Db/Pb1550, Db/Pb1200, Db1300
12 * The Db1000 is used as a reference: Per-socket card-, carddetect- and
13 * statuschange IRQs connected to SoC GPIOs, control and status register
14 * bits arranged in per-socket groups in an external PLD. All boards
15 * listed here use this layout, including bit positions and meanings.
16 * Of course there are exceptions in later boards:
18 * - Pb1100/Pb1500: single socket only; voltage key bits VS are
19 * at STATUS[5:4] (instead of STATUS[1:0]).
20 * - Au1200-based: additional card-eject irqs, irqs not gpios!
21 * - Db1300: Db1200-like, no pwr ctrl, single socket (#1).
24 #include <linux/delay.h>
25 #include <linux/gpio.h>
26 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/resource.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
34 #include <pcmcia/ss.h>
36 #include <asm/mach-au1x00/au1000.h>
37 #include <asm/mach-db1x00/bcsr.h>
39 #define MEM_MAP_SIZE 0x400000
40 #define IO_MAP_SIZE 0x1000
42 struct db1x_pcmcia_sock
{
43 struct pcmcia_socket socket
;
44 int nr
; /* socket number */
48 phys_addr_t phys_attr
;
51 /* previous flags for set_socket() */
52 unsigned int old_flags
;
54 /* interrupt sources: linux irq numbers! */
55 int insert_irq
; /* default carddetect irq */
56 int stschg_irq
; /* card-status-change irq */
57 int card_irq
; /* card irq */
58 int eject_irq
; /* db1200/pb1200 have these */
59 int insert_gpio
; /* db1000 carddetect gpio */
61 #define BOARD_TYPE_DEFAULT 0 /* most boards */
62 #define BOARD_TYPE_DB1200 1 /* IRQs aren't gpios */
63 #define BOARD_TYPE_PB1100 2 /* VS bits slightly different */
64 #define BOARD_TYPE_DB1300 3 /* no power control */
68 #define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket)
70 static int db1300_card_inserted(struct db1x_pcmcia_sock
*sock
)
72 return bcsr_read(BCSR_SIGSTAT
) & (1 << 8);
75 /* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */
76 static int db1200_card_inserted(struct db1x_pcmcia_sock
*sock
)
78 unsigned short sigstat
;
80 sigstat
= bcsr_read(BCSR_SIGSTAT
);
81 return sigstat
& 1 << (8 + 2 * sock
->nr
);
84 /* carddetect gpio: low-active */
85 static int db1000_card_inserted(struct db1x_pcmcia_sock
*sock
)
87 return !gpio_get_value(sock
->insert_gpio
);
90 static int db1x_card_inserted(struct db1x_pcmcia_sock
*sock
)
92 switch (sock
->board_type
) {
93 case BOARD_TYPE_DB1200
:
94 return db1200_card_inserted(sock
);
95 case BOARD_TYPE_DB1300
:
96 return db1300_card_inserted(sock
);
98 return db1000_card_inserted(sock
);
102 /* STSCHG tends to bounce heavily when cards are inserted/ejected.
103 * To avoid this, the interrupt is normally disabled and only enabled
104 * after reset to a card has been de-asserted.
106 static inline void set_stschg(struct db1x_pcmcia_sock
*sock
, int en
)
108 if (sock
->stschg_irq
!= -1) {
110 enable_irq(sock
->stschg_irq
);
112 disable_irq(sock
->stschg_irq
);
116 static irqreturn_t
db1000_pcmcia_cdirq(int irq
, void *data
)
118 struct db1x_pcmcia_sock
*sock
= data
;
120 pcmcia_parse_events(&sock
->socket
, SS_DETECT
);
125 static irqreturn_t
db1000_pcmcia_stschgirq(int irq
, void *data
)
127 struct db1x_pcmcia_sock
*sock
= data
;
129 pcmcia_parse_events(&sock
->socket
, SS_STSCHG
);
134 static irqreturn_t
db1200_pcmcia_cdirq(int irq
, void *data
)
136 struct db1x_pcmcia_sock
*sock
= data
;
138 /* Db/Pb1200 have separate per-socket insertion and ejection
139 * interrupts which stay asserted as long as the card is
140 * inserted/missing. The one which caused us to be called
141 * needs to be disabled and the other one enabled.
143 if (irq
== sock
->insert_irq
) {
144 disable_irq_nosync(sock
->insert_irq
);
145 enable_irq(sock
->eject_irq
);
147 disable_irq_nosync(sock
->eject_irq
);
148 enable_irq(sock
->insert_irq
);
151 pcmcia_parse_events(&sock
->socket
, SS_DETECT
);
156 static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock
*sock
)
160 if (sock
->stschg_irq
!= -1) {
161 ret
= request_irq(sock
->stschg_irq
, db1000_pcmcia_stschgirq
,
162 0, "pcmcia_stschg", sock
);
167 /* Db/Pb1200 have separate per-socket insertion and ejection
168 * interrupts, which should show edge behaviour but don't.
169 * So interrupts are disabled until both insertion and
170 * ejection handler have been registered and the currently
171 * active one disabled.
173 if ((sock
->board_type
== BOARD_TYPE_DB1200
) ||
174 (sock
->board_type
== BOARD_TYPE_DB1300
)) {
175 ret
= request_irq(sock
->insert_irq
, db1200_pcmcia_cdirq
,
176 0, "pcmcia_insert", sock
);
180 ret
= request_irq(sock
->eject_irq
, db1200_pcmcia_cdirq
,
181 0, "pcmcia_eject", sock
);
183 free_irq(sock
->insert_irq
, sock
);
187 /* enable the currently silent one */
188 if (db1x_card_inserted(sock
))
189 enable_irq(sock
->eject_irq
);
191 enable_irq(sock
->insert_irq
);
193 /* all other (older) Db1x00 boards use a GPIO to show
194 * card detection status: use both-edge triggers.
196 irq_set_irq_type(sock
->insert_irq
, IRQ_TYPE_EDGE_BOTH
);
197 ret
= request_irq(sock
->insert_irq
, db1000_pcmcia_cdirq
,
198 0, "pcmcia_carddetect", sock
);
204 return 0; /* all done */
207 if (sock
->stschg_irq
!= -1)
208 free_irq(sock
->stschg_irq
, sock
);
213 static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock
*sock
)
215 if (sock
->stschg_irq
!= -1)
216 free_irq(sock
->stschg_irq
, sock
);
218 free_irq(sock
->insert_irq
, sock
);
219 if (sock
->eject_irq
!= -1)
220 free_irq(sock
->eject_irq
, sock
);
224 * configure a PCMCIA socket on the Db1x00 series of boards (and
227 * 2 external registers are involved:
228 * pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id
229 * pcmcia_control(offset 0x10):
230 * bits[0:1] set vcc for card
231 * bits[2:3] set vpp for card
232 * bit 4: enable data buffers
233 * bit 7: reset# for card
234 * add 8 for second socket.
236 static int db1x_pcmcia_configure(struct pcmcia_socket
*skt
,
237 struct socket_state_t
*state
)
239 struct db1x_pcmcia_sock
*sock
= to_db1x_socket(skt
);
240 unsigned short cr_clr
, cr_set
;
241 unsigned int changed
;
244 /* card voltage setup */
245 cr_clr
= (0xf << (sock
->nr
* 8)); /* clear voltage settings */
249 switch (state
->Vcc
) {
257 printk(KERN_INFO
"pcmcia%d unsupported Vcc %d\n",
258 sock
->nr
, state
->Vcc
);
261 switch (state
->Vpp
) {
270 printk(KERN_INFO
"pcmcia%d unsupported Vpp %d\n",
271 sock
->nr
, state
->Vpp
);
274 /* sanity check: Vpp must be 0, 12, or Vcc */
275 if (((state
->Vcc
== 33) && (state
->Vpp
== 50)) ||
276 ((state
->Vcc
== 50) && (state
->Vpp
== 33))) {
277 printk(KERN_INFO
"pcmcia%d bad Vcc/Vpp combo (%d %d)\n",
278 sock
->nr
, state
->Vcc
, state
->Vpp
);
283 /* create new voltage code */
284 if (sock
->board_type
!= BOARD_TYPE_DB1300
)
285 cr_set
|= ((v
<< 2) | p
) << (sock
->nr
* 8);
287 changed
= state
->flags
^ sock
->old_flags
;
289 if (changed
& SS_RESET
) {
290 if (state
->flags
& SS_RESET
) {
292 /* assert reset, disable io buffers */
293 cr_clr
|= (1 << (7 + (sock
->nr
* 8)));
294 cr_clr
|= (1 << (4 + (sock
->nr
* 8)));
296 /* de-assert reset, enable io buffers */
297 cr_set
|= 1 << (7 + (sock
->nr
* 8));
298 cr_set
|= 1 << (4 + (sock
->nr
* 8));
302 /* update PCMCIA configuration */
303 bcsr_mod(BCSR_PCMCIA
, cr_clr
, cr_set
);
305 sock
->old_flags
= state
->flags
;
307 /* reset was taken away: give card time to initialize properly */
308 if ((changed
& SS_RESET
) && !(state
->flags
& SS_RESET
)) {
316 /* VCC bits at [3:2]/[11:10] */
317 #define GET_VCC(cr, socknr) \
318 ((((cr) >> 2) >> ((socknr) * 8)) & 3)
320 /* VS bits at [0:1]/[3:2] */
321 #define GET_VS(sr, socknr) \
322 (((sr) >> (2 * (socknr))) & 3)
324 /* reset bits at [7]/[15] */
325 #define GET_RESET(cr, socknr) \
326 ((cr) & (1 << (7 + (8 * (socknr)))))
328 static int db1x_pcmcia_get_status(struct pcmcia_socket
*skt
,
331 struct db1x_pcmcia_sock
*sock
= to_db1x_socket(skt
);
332 unsigned short cr
, sr
;
335 status
= db1x_card_inserted(sock
) ? SS_DETECT
: 0;
337 cr
= bcsr_read(BCSR_PCMCIA
);
338 sr
= bcsr_read(BCSR_STATUS
);
340 /* PB1100/PB1500: voltage key bits are at [5:4] */
341 if (sock
->board_type
== BOARD_TYPE_PB1100
)
344 /* determine card type */
345 switch (GET_VS(sr
, sock
->nr
)) {
348 status
|= SS_3VCARD
; /* 3V card */
350 break; /* 5V card: set nothing */
352 status
|= SS_XVCARD
; /* treated as unsupported in core */
355 /* if Vcc is not zero, we have applied power to a card */
356 status
|= GET_VCC(cr
, sock
->nr
) ? SS_POWERON
: 0;
358 /* DB1300: power always on, but don't tell when no card present */
359 if ((sock
->board_type
== BOARD_TYPE_DB1300
) && (status
& SS_DETECT
))
360 status
= SS_POWERON
| SS_3VCARD
| SS_DETECT
;
362 /* reset de-asserted? then we're ready */
363 status
|= (GET_RESET(cr
, sock
->nr
)) ? SS_READY
: SS_RESET
;
370 static int db1x_pcmcia_sock_init(struct pcmcia_socket
*skt
)
375 static int db1x_pcmcia_sock_suspend(struct pcmcia_socket
*skt
)
380 static int au1x00_pcmcia_set_io_map(struct pcmcia_socket
*skt
,
381 struct pccard_io_map
*map
)
383 struct db1x_pcmcia_sock
*sock
= to_db1x_socket(skt
);
385 map
->start
= (u32
)sock
->virt_io
;
386 map
->stop
= map
->start
+ IO_MAP_SIZE
;
391 static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket
*skt
,
392 struct pccard_mem_map
*map
)
394 struct db1x_pcmcia_sock
*sock
= to_db1x_socket(skt
);
396 if (map
->flags
& MAP_ATTRIB
)
397 map
->static_start
= sock
->phys_attr
+ map
->card_start
;
399 map
->static_start
= sock
->phys_mem
+ map
->card_start
;
404 static struct pccard_operations db1x_pcmcia_operations
= {
405 .init
= db1x_pcmcia_sock_init
,
406 .suspend
= db1x_pcmcia_sock_suspend
,
407 .get_status
= db1x_pcmcia_get_status
,
408 .set_socket
= db1x_pcmcia_configure
,
409 .set_io_map
= au1x00_pcmcia_set_io_map
,
410 .set_mem_map
= au1x00_pcmcia_set_mem_map
,
413 static int db1x_pcmcia_socket_probe(struct platform_device
*pdev
)
415 struct db1x_pcmcia_sock
*sock
;
419 sock
= kzalloc(sizeof(struct db1x_pcmcia_sock
), GFP_KERNEL
);
425 bid
= BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI
));
427 case BCSR_WHOAMI_PB1500
:
428 case BCSR_WHOAMI_PB1500R2
:
429 case BCSR_WHOAMI_PB1100
:
430 sock
->board_type
= BOARD_TYPE_PB1100
;
432 case BCSR_WHOAMI_DB1000
... BCSR_WHOAMI_PB1550_SDR
:
433 sock
->board_type
= BOARD_TYPE_DEFAULT
;
435 case BCSR_WHOAMI_PB1200
... BCSR_WHOAMI_DB1200
:
436 sock
->board_type
= BOARD_TYPE_DB1200
;
438 case BCSR_WHOAMI_DB1300
:
439 sock
->board_type
= BOARD_TYPE_DB1300
;
442 printk(KERN_INFO
"db1xxx-ss: unknown board %d!\n", bid
);
448 * gather resources necessary and optional nice-to-haves to
450 * This includes IRQs for Carddetection/ejection, the card
451 * itself and optional status change detection.
452 * Also, the memory areas covered by a socket. For these
453 * we require the real 36bit addresses (see the au1000.h
454 * header for more information).
457 /* card: irq assigned to the card itself. */
458 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "card");
459 sock
->card_irq
= r
? r
->start
: 0;
461 /* insert: irq which triggers on card insertion/ejection
462 * BIG FAT NOTE: on DB1000/1100/1500/1550 we pass a GPIO here!
464 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "insert");
465 sock
->insert_irq
= r
? r
->start
: -1;
466 if (sock
->board_type
== BOARD_TYPE_DEFAULT
) {
467 sock
->insert_gpio
= r
? r
->start
: -1;
468 sock
->insert_irq
= r
? gpio_to_irq(r
->start
) : -1;
471 /* stschg: irq which trigger on card status change (optional) */
472 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "stschg");
473 sock
->stschg_irq
= r
? r
->start
: -1;
475 /* eject: irq which triggers on ejection (DB1200/PB1200 only) */
476 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "eject");
477 sock
->eject_irq
= r
? r
->start
: -1;
481 /* 36bit PCMCIA Attribute area address */
482 r
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pcmcia-attr");
484 printk(KERN_ERR
"pcmcia%d has no 'pseudo-attr' resource!\n",
488 sock
->phys_attr
= r
->start
;
490 /* 36bit PCMCIA Memory area address */
491 r
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pcmcia-mem");
493 printk(KERN_ERR
"pcmcia%d has no 'pseudo-mem' resource!\n",
497 sock
->phys_mem
= r
->start
;
499 /* 36bit PCMCIA IO area address */
500 r
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pcmcia-io");
502 printk(KERN_ERR
"pcmcia%d has no 'pseudo-io' resource!\n",
506 sock
->phys_io
= r
->start
;
509 * PCMCIA client drivers use the inb/outb macros to access
510 * the IO registers. Since mips_io_port_base is added
511 * to the access address of the mips implementation of
512 * inb/outb, we need to subtract it here because we want
513 * to access the I/O or MEM address directly, without
514 * going through this "mips_io_port_base" mechanism.
516 sock
->virt_io
= (void *)(ioremap(sock
->phys_io
, IO_MAP_SIZE
) -
519 if (!sock
->virt_io
) {
520 printk(KERN_ERR
"pcmcia%d: cannot remap IO area\n",
526 sock
->socket
.ops
= &db1x_pcmcia_operations
;
527 sock
->socket
.owner
= THIS_MODULE
;
528 sock
->socket
.pci_irq
= sock
->card_irq
;
529 sock
->socket
.features
= SS_CAP_STATIC_MAP
| SS_CAP_PCCARD
;
530 sock
->socket
.map_size
= MEM_MAP_SIZE
;
531 sock
->socket
.io_offset
= (unsigned long)sock
->virt_io
;
532 sock
->socket
.dev
.parent
= &pdev
->dev
;
533 sock
->socket
.resource_ops
= &pccard_static_ops
;
535 platform_set_drvdata(pdev
, sock
);
537 ret
= db1x_pcmcia_setup_irqs(sock
);
539 printk(KERN_ERR
"pcmcia%d cannot setup interrupts\n",
546 ret
= pcmcia_register_socket(&sock
->socket
);
548 printk(KERN_ERR
"pcmcia%d failed to register\n", sock
->nr
);
552 printk(KERN_INFO
"Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %09llx"
553 "(%p) %09llx %09llx card/insert/stschg/eject irqs @ %d "
554 "%d %d %d\n", sock
->nr
, sock
->phys_io
, sock
->virt_io
,
555 sock
->phys_attr
, sock
->phys_mem
, sock
->card_irq
,
556 sock
->insert_irq
, sock
->stschg_irq
, sock
->eject_irq
);
561 db1x_pcmcia_free_irqs(sock
);
563 iounmap((void *)(sock
->virt_io
+ (u32
)mips_io_port_base
));
569 static int db1x_pcmcia_socket_remove(struct platform_device
*pdev
)
571 struct db1x_pcmcia_sock
*sock
= platform_get_drvdata(pdev
);
573 db1x_pcmcia_free_irqs(sock
);
574 pcmcia_unregister_socket(&sock
->socket
);
575 iounmap((void *)(sock
->virt_io
+ (u32
)mips_io_port_base
));
581 static struct platform_driver db1x_pcmcia_socket_driver
= {
583 .name
= "db1xxx_pcmcia",
585 .probe
= db1x_pcmcia_socket_probe
,
586 .remove
= db1x_pcmcia_socket_remove
,
589 module_platform_driver(db1x_pcmcia_socket_driver
);
591 MODULE_LICENSE("GPL");
592 MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
593 MODULE_AUTHOR("Manuel Lauss");