drm/fsl-dcu: Remove unneeded NULL check
[linux/fpc-iii.git] / drivers / pcmcia / db1xxx_ss.c
blob944674ee34644fa2583f2193ad738a4b5cbe4912
1 /*
2 * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards.
4 * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
6 */
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>
27 #include <linux/pm.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 */
45 void *virt_io;
47 phys_addr_t phys_io;
48 phys_addr_t phys_attr;
49 phys_addr_t phys_mem;
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 */
65 int board_type;
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);
97 default:
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) {
109 if (en)
110 enable_irq(sock->stschg_irq);
111 else
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);
122 return IRQ_HANDLED;
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);
131 return IRQ_HANDLED;
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);
146 } else {
147 disable_irq_nosync(sock->eject_irq);
148 enable_irq(sock->insert_irq);
151 pcmcia_parse_events(&sock->socket, SS_DETECT);
153 return IRQ_HANDLED;
156 static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
158 int ret;
160 if (sock->stschg_irq != -1) {
161 ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
162 0, "pcmcia_stschg", sock);
163 if (ret)
164 return ret;
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);
177 if (ret)
178 goto out1;
180 ret = request_irq(sock->eject_irq, db1200_pcmcia_cdirq,
181 0, "pcmcia_eject", sock);
182 if (ret) {
183 free_irq(sock->insert_irq, sock);
184 goto out1;
187 /* enable the currently silent one */
188 if (db1x_card_inserted(sock))
189 enable_irq(sock->eject_irq);
190 else
191 enable_irq(sock->insert_irq);
192 } else {
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);
200 if (ret)
201 goto out1;
204 return 0; /* all done */
206 out1:
207 if (sock->stschg_irq != -1)
208 free_irq(sock->stschg_irq, sock);
210 return ret;
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
225 * compatibles).
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;
242 int v, p, ret;
244 /* card voltage setup */
245 cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
246 cr_set = 0;
247 v = p = ret = 0;
249 switch (state->Vcc) {
250 case 50:
251 ++v;
252 case 33:
253 ++v;
254 case 0:
255 break;
256 default:
257 printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
258 sock->nr, state->Vcc);
261 switch (state->Vpp) {
262 case 12:
263 ++p;
264 case 33:
265 case 50:
266 ++p;
267 case 0:
268 break;
269 default:
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);
279 v = p = 0;
280 ret = -EINVAL;
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) {
291 set_stschg(sock, 0);
292 /* assert reset, disable io buffers */
293 cr_clr |= (1 << (7 + (sock->nr * 8)));
294 cr_clr |= (1 << (4 + (sock->nr * 8)));
295 } else {
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)) {
309 msleep(500);
310 set_stschg(sock, 1);
313 return ret;
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,
329 unsigned int *value)
331 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
332 unsigned short cr, sr;
333 unsigned int status;
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)
342 sr >>= 4;
344 /* determine card type */
345 switch (GET_VS(sr, sock->nr)) {
346 case 0:
347 case 2:
348 status |= SS_3VCARD; /* 3V card */
349 case 3:
350 break; /* 5V card: set nothing */
351 default:
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;
365 *value = status;
367 return 0;
370 static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
372 return 0;
375 static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
377 return 0;
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;
388 return 0;
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;
398 else
399 map->static_start = sock->phys_mem + map->card_start;
401 return 0;
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;
416 struct resource *r;
417 int ret, bid;
419 sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
420 if (!sock)
421 return -ENOMEM;
423 sock->nr = pdev->id;
425 bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
426 switch (bid) {
427 case BCSR_WHOAMI_PB1500:
428 case BCSR_WHOAMI_PB1500R2:
429 case BCSR_WHOAMI_PB1100:
430 sock->board_type = BOARD_TYPE_PB1100;
431 break;
432 case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
433 sock->board_type = BOARD_TYPE_DEFAULT;
434 break;
435 case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
436 sock->board_type = BOARD_TYPE_DB1200;
437 break;
438 case BCSR_WHOAMI_DB1300:
439 sock->board_type = BOARD_TYPE_DB1300;
440 break;
441 default:
442 printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
443 ret = -ENODEV;
444 goto out0;
448 * gather resources necessary and optional nice-to-haves to
449 * operate a socket:
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;
479 ret = -ENODEV;
481 /* 36bit PCMCIA Attribute area address */
482 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
483 if (!r) {
484 printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
485 sock->nr);
486 goto out0;
488 sock->phys_attr = r->start;
490 /* 36bit PCMCIA Memory area address */
491 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
492 if (!r) {
493 printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
494 sock->nr);
495 goto out0;
497 sock->phys_mem = r->start;
499 /* 36bit PCMCIA IO area address */
500 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
501 if (!r) {
502 printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
503 sock->nr);
504 goto out0;
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) -
517 mips_io_port_base);
519 if (!sock->virt_io) {
520 printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
521 sock->nr);
522 ret = -ENOMEM;
523 goto out0;
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);
538 if (ret) {
539 printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
540 sock->nr);
541 goto out1;
544 set_stschg(sock, 0);
546 ret = pcmcia_register_socket(&sock->socket);
547 if (ret) {
548 printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
549 goto out2;
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);
558 return 0;
560 out2:
561 db1x_pcmcia_free_irqs(sock);
562 out1:
563 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
564 out0:
565 kfree(sock);
566 return ret;
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));
576 kfree(sock);
578 return 0;
581 static struct platform_driver db1x_pcmcia_socket_driver = {
582 .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");