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 /* Db/Pb1200 have separate per-socket insertion and ejection
135 * interrupts which stay asserted as long as the card is
136 * inserted/missing. The one which caused us to be called
137 * needs to be disabled and the other one enabled.
139 static irqreturn_t db1200_pcmcia_cdirq(int irq, void *data)
141 disable_irq_nosync(irq);
142 return IRQ_WAKE_THREAD;
145 static irqreturn_t db1200_pcmcia_cdirq_fn(int irq, void *data)
147 struct db1x_pcmcia_sock *sock = data;
149 /* Wait a bit for the signals to stop bouncing. */
151 if (irq == sock->insert_irq)
152 enable_irq(sock->eject_irq);
154 enable_irq(sock->insert_irq);
156 pcmcia_parse_events(&sock->socket, SS_DETECT);
161 static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
165 if (sock->stschg_irq != -1) {
166 ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
167 0, "pcmcia_stschg", sock);
172 /* Db/Pb1200 have separate per-socket insertion and ejection
173 * interrupts, which should show edge behaviour but don't.
174 * So interrupts are disabled until both insertion and
175 * ejection handler have been registered and the currently
176 * active one disabled.
178 if ((sock->board_type == BOARD_TYPE_DB1200) ||
179 (sock->board_type == BOARD_TYPE_DB1300)) {
180 ret = request_threaded_irq(sock->insert_irq, db1200_pcmcia_cdirq,
181 db1200_pcmcia_cdirq_fn, 0, "pcmcia_insert", sock);
185 ret = request_threaded_irq(sock->eject_irq, db1200_pcmcia_cdirq,
186 db1200_pcmcia_cdirq_fn, 0, "pcmcia_eject", sock);
188 free_irq(sock->insert_irq, sock);
192 /* enable the currently silent one */
193 if (db1x_card_inserted(sock))
194 enable_irq(sock->eject_irq);
196 enable_irq(sock->insert_irq);
198 /* all other (older) Db1x00 boards use a GPIO to show
199 * card detection status: use both-edge triggers.
201 irq_set_irq_type(sock->insert_irq, IRQ_TYPE_EDGE_BOTH);
202 ret = request_irq(sock->insert_irq, db1000_pcmcia_cdirq,
203 0, "pcmcia_carddetect", sock);
209 return 0; /* all done */
212 if (sock->stschg_irq != -1)
213 free_irq(sock->stschg_irq, sock);
218 static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock)
220 if (sock->stschg_irq != -1)
221 free_irq(sock->stschg_irq, sock);
223 free_irq(sock->insert_irq, sock);
224 if (sock->eject_irq != -1)
225 free_irq(sock->eject_irq, sock);
229 * configure a PCMCIA socket on the Db1x00 series of boards (and
232 * 2 external registers are involved:
233 * pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id
234 * pcmcia_control(offset 0x10):
235 * bits[0:1] set vcc for card
236 * bits[2:3] set vpp for card
237 * bit 4: enable data buffers
238 * bit 7: reset# for card
239 * add 8 for second socket.
241 static int db1x_pcmcia_configure(struct pcmcia_socket *skt,
242 struct socket_state_t *state)
244 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
245 unsigned short cr_clr, cr_set;
246 unsigned int changed;
249 /* card voltage setup */
250 cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
254 switch (state->Vcc) {
262 printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
263 sock->nr, state->Vcc);
266 switch (state->Vpp) {
275 printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n",
276 sock->nr, state->Vpp);
279 /* sanity check: Vpp must be 0, 12, or Vcc */
280 if (((state->Vcc == 33) && (state->Vpp == 50)) ||
281 ((state->Vcc == 50) && (state->Vpp == 33))) {
282 printk(KERN_INFO "pcmcia%d bad Vcc/Vpp combo (%d %d)\n",
283 sock->nr, state->Vcc, state->Vpp);
288 /* create new voltage code */
289 if (sock->board_type != BOARD_TYPE_DB1300)
290 cr_set |= ((v << 2) | p) << (sock->nr * 8);
292 changed = state->flags ^ sock->old_flags;
294 if (changed & SS_RESET) {
295 if (state->flags & SS_RESET) {
297 /* assert reset, disable io buffers */
298 cr_clr |= (1 << (7 + (sock->nr * 8)));
299 cr_clr |= (1 << (4 + (sock->nr * 8)));
301 /* de-assert reset, enable io buffers */
302 cr_set |= 1 << (7 + (sock->nr * 8));
303 cr_set |= 1 << (4 + (sock->nr * 8));
307 /* update PCMCIA configuration */
308 bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set);
310 sock->old_flags = state->flags;
312 /* reset was taken away: give card time to initialize properly */
313 if ((changed & SS_RESET) && !(state->flags & SS_RESET)) {
321 /* VCC bits at [3:2]/[11:10] */
322 #define GET_VCC(cr, socknr) \
323 ((((cr) >> 2) >> ((socknr) * 8)) & 3)
325 /* VS bits at [0:1]/[3:2] */
326 #define GET_VS(sr, socknr) \
327 (((sr) >> (2 * (socknr))) & 3)
329 /* reset bits at [7]/[15] */
330 #define GET_RESET(cr, socknr) \
331 ((cr) & (1 << (7 + (8 * (socknr)))))
333 static int db1x_pcmcia_get_status(struct pcmcia_socket *skt,
336 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
337 unsigned short cr, sr;
340 status = db1x_card_inserted(sock) ? SS_DETECT : 0;
342 cr = bcsr_read(BCSR_PCMCIA);
343 sr = bcsr_read(BCSR_STATUS);
345 /* PB1100/PB1500: voltage key bits are at [5:4] */
346 if (sock->board_type == BOARD_TYPE_PB1100)
349 /* determine card type */
350 switch (GET_VS(sr, sock->nr)) {
353 status |= SS_3VCARD; /* 3V card */
355 break; /* 5V card: set nothing */
357 status |= SS_XVCARD; /* treated as unsupported in core */
360 /* if Vcc is not zero, we have applied power to a card */
361 status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0;
363 /* DB1300: power always on, but don't tell when no card present */
364 if ((sock->board_type == BOARD_TYPE_DB1300) && (status & SS_DETECT))
365 status = SS_POWERON | SS_3VCARD | SS_DETECT;
367 /* reset de-asserted? then we're ready */
368 status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET;
375 static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
380 static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
385 static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
386 struct pccard_io_map *map)
388 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
390 map->start = (u32)sock->virt_io;
391 map->stop = map->start + IO_MAP_SIZE;
396 static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
397 struct pccard_mem_map *map)
399 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
401 if (map->flags & MAP_ATTRIB)
402 map->static_start = sock->phys_attr + map->card_start;
404 map->static_start = sock->phys_mem + map->card_start;
409 static struct pccard_operations db1x_pcmcia_operations = {
410 .init = db1x_pcmcia_sock_init,
411 .suspend = db1x_pcmcia_sock_suspend,
412 .get_status = db1x_pcmcia_get_status,
413 .set_socket = db1x_pcmcia_configure,
414 .set_io_map = au1x00_pcmcia_set_io_map,
415 .set_mem_map = au1x00_pcmcia_set_mem_map,
418 static int db1x_pcmcia_socket_probe(struct platform_device *pdev)
420 struct db1x_pcmcia_sock *sock;
424 sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
430 bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
432 case BCSR_WHOAMI_PB1500:
433 case BCSR_WHOAMI_PB1500R2:
434 case BCSR_WHOAMI_PB1100:
435 sock->board_type = BOARD_TYPE_PB1100;
437 case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
438 sock->board_type = BOARD_TYPE_DEFAULT;
440 case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
441 sock->board_type = BOARD_TYPE_DB1200;
443 case BCSR_WHOAMI_DB1300:
444 sock->board_type = BOARD_TYPE_DB1300;
447 printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
453 * gather resources necessary and optional nice-to-haves to
455 * This includes IRQs for Carddetection/ejection, the card
456 * itself and optional status change detection.
457 * Also, the memory areas covered by a socket. For these
458 * we require the real 36bit addresses (see the au1000.h
459 * header for more information).
462 /* card: irq assigned to the card itself. */
463 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "card");
464 sock->card_irq = r ? r->start : 0;
466 /* insert: irq which triggers on card insertion/ejection
467 * BIG FAT NOTE: on DB1000/1100/1500/1550 we pass a GPIO here!
469 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "insert");
470 sock->insert_irq = r ? r->start : -1;
471 if (sock->board_type == BOARD_TYPE_DEFAULT) {
472 sock->insert_gpio = r ? r->start : -1;
473 sock->insert_irq = r ? gpio_to_irq(r->start) : -1;
476 /* stschg: irq which trigger on card status change (optional) */
477 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "stschg");
478 sock->stschg_irq = r ? r->start : -1;
480 /* eject: irq which triggers on ejection (DB1200/PB1200 only) */
481 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "eject");
482 sock->eject_irq = r ? r->start : -1;
486 /* 36bit PCMCIA Attribute area address */
487 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
489 printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
493 sock->phys_attr = r->start;
495 /* 36bit PCMCIA Memory area address */
496 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
498 printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
502 sock->phys_mem = r->start;
504 /* 36bit PCMCIA IO area address */
505 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
507 printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
511 sock->phys_io = r->start;
514 * PCMCIA client drivers use the inb/outb macros to access
515 * the IO registers. Since mips_io_port_base is added
516 * to the access address of the mips implementation of
517 * inb/outb, we need to subtract it here because we want
518 * to access the I/O or MEM address directly, without
519 * going through this "mips_io_port_base" mechanism.
521 sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) -
524 if (!sock->virt_io) {
525 printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
531 sock->socket.ops = &db1x_pcmcia_operations;
532 sock->socket.owner = THIS_MODULE;
533 sock->socket.pci_irq = sock->card_irq;
534 sock->socket.features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
535 sock->socket.map_size = MEM_MAP_SIZE;
536 sock->socket.io_offset = (unsigned long)sock->virt_io;
537 sock->socket.dev.parent = &pdev->dev;
538 sock->socket.resource_ops = &pccard_static_ops;
540 platform_set_drvdata(pdev, sock);
542 ret = db1x_pcmcia_setup_irqs(sock);
544 printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
551 ret = pcmcia_register_socket(&sock->socket);
553 printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
557 printk(KERN_INFO "Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %09llx"
558 "(%p) %09llx %09llx card/insert/stschg/eject irqs @ %d "
559 "%d %d %d\n", sock->nr, sock->phys_io, sock->virt_io,
560 sock->phys_attr, sock->phys_mem, sock->card_irq,
561 sock->insert_irq, sock->stschg_irq, sock->eject_irq);
566 db1x_pcmcia_free_irqs(sock);
568 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
574 static int db1x_pcmcia_socket_remove(struct platform_device *pdev)
576 struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev);
578 db1x_pcmcia_free_irqs(sock);
579 pcmcia_unregister_socket(&sock->socket);
580 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
586 static struct platform_driver db1x_pcmcia_socket_driver = {
588 .name = "db1xxx_pcmcia",
590 .probe = db1x_pcmcia_socket_probe,
591 .remove = db1x_pcmcia_socket_remove,
594 module_platform_driver(db1x_pcmcia_socket_driver);
596 MODULE_LICENSE("GPL");
597 MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
598 MODULE_AUTHOR("Manuel Lauss");