aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pcmcia/db1xxx_ss.c
diff options
context:
space:
mode:
authorManuel Lauss <manuel.lauss@googlemail.com>2009-10-04 08:55:27 -0400
committerRalf Baechle <ralf@linux-mips.org>2010-02-27 06:52:51 -0500
commit66213b3ccfc770704025ce9465fa3aaedde21b55 (patch)
tree2f74c1819b371926952c9c5b8f98ae808588e728 /drivers/pcmcia/db1xxx_ss.c
parent7e50b2b741bb4f9dbddc9f56972ef82a7d4b33ed (diff)
MIPS: PCMCIA: new socket driver for Au1000 demoboards.
New PCMCIA socket driver for all Db/Pb1xxx boards (except Pb1000), which replaces au1000_db1x00.c and (most of) au1000_pb1x00.c. Notable improvements: - supports Db1000, DB/PB1100/1500/1550/1200. - support for carddetect and statuschange IRQs. - pcmcia socket mem/io/attr areas and irqs passed through platform resource information. - doesn't freeze system during card insertion/ejection like the one it replaces. - boardtype is automatically detected using BCSR ID register. Run-tested on the DB1200. Cc: Linux-PCMCIA <linux-pcmcia@lists.infradead.org> Signed-off-by: Manuel Lauss <manuel.lauss@gmail.com> Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'drivers/pcmcia/db1xxx_ss.c')
-rw-r--r--drivers/pcmcia/db1xxx_ss.c630
1 files changed, 630 insertions, 0 deletions
diff --git a/drivers/pcmcia/db1xxx_ss.c b/drivers/pcmcia/db1xxx_ss.c
new file mode 100644
index 000000000000..b35b72b0d5b5
--- /dev/null
+++ b/drivers/pcmcia/db1xxx_ss.c
@@ -0,0 +1,630 @@
1/*
2 * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards.
3 *
4 * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
5 *
6 */
7
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.
11 *
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:
17 *
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 */
22
23#include <linux/delay.h>
24#include <linux/gpio.h>
25#include <linux/interrupt.h>
26#include <linux/pm.h>
27#include <linux/platform_device.h>
28#include <linux/resource.h>
29#include <linux/spinlock.h>
30
31#include <pcmcia/cs_types.h>
32#include <pcmcia/ss.h>
33
34#include <asm/mach-au1x00/au1000.h>
35#include <asm/mach-db1x00/bcsr.h>
36
37#define MEM_MAP_SIZE 0x400000
38#define IO_MAP_SIZE 0x1000
39
40struct db1x_pcmcia_sock {
41 struct pcmcia_socket socket;
42 int nr; /* socket number */
43 void *virt_io;
44
45 /* the "pseudo" addresses of the PCMCIA space. */
46 unsigned long phys_io;
47 unsigned long phys_attr;
48 unsigned long phys_mem;
49
50 /* previous flags for set_socket() */
51 unsigned int old_flags;
52
53 /* interrupt sources: linux irq numbers! */
54 int insert_irq; /* default carddetect irq */
55 int stschg_irq; /* card-status-change irq */
56 int card_irq; /* card irq */
57 int eject_irq; /* db1200/pb1200 have these */
58
59#define BOARD_TYPE_DEFAULT 0 /* most boards */
60#define BOARD_TYPE_DB1200 1 /* IRQs aren't gpios */
61#define BOARD_TYPE_PB1100 2 /* VS bits slightly different */
62 int board_type;
63};
64
65#define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket)
66
67/* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */
68static int db1200_card_inserted(struct db1x_pcmcia_sock *sock)
69{
70 unsigned short sigstat;
71
72 sigstat = bcsr_read(BCSR_SIGSTAT);
73 return sigstat & 1 << (8 + 2 * sock->nr);
74}
75
76/* carddetect gpio: low-active */
77static int db1000_card_inserted(struct db1x_pcmcia_sock *sock)
78{
79 return !gpio_get_value(irq_to_gpio(sock->insert_irq));
80}
81
82static int db1x_card_inserted(struct db1x_pcmcia_sock *sock)
83{
84 switch (sock->board_type) {
85 case BOARD_TYPE_DB1200:
86 return db1200_card_inserted(sock);
87 default:
88 return db1000_card_inserted(sock);
89 }
90}
91
92/* STSCHG tends to bounce heavily when cards are inserted/ejected.
93 * To avoid this, the interrupt is normally disabled and only enabled
94 * after reset to a card has been de-asserted.
95 */
96static inline void set_stschg(struct db1x_pcmcia_sock *sock, int en)
97{
98 if (sock->stschg_irq != -1) {
99 if (en)
100 enable_irq(sock->stschg_irq);
101 else
102 disable_irq(sock->stschg_irq);
103 }
104}
105
106static irqreturn_t db1000_pcmcia_cdirq(int irq, void *data)
107{
108 struct db1x_pcmcia_sock *sock = data;
109
110 pcmcia_parse_events(&sock->socket, SS_DETECT);
111
112 return IRQ_HANDLED;
113}
114
115static irqreturn_t db1000_pcmcia_stschgirq(int irq, void *data)
116{
117 struct db1x_pcmcia_sock *sock = data;
118
119 pcmcia_parse_events(&sock->socket, SS_STSCHG);
120
121 return IRQ_HANDLED;
122}
123
124static irqreturn_t db1200_pcmcia_cdirq(int irq, void *data)
125{
126 struct db1x_pcmcia_sock *sock = data;
127
128 /* Db/Pb1200 have separate per-socket insertion and ejection
129 * interrupts which stay asserted as long as the card is
130 * inserted/missing. The one which caused us to be called
131 * needs to be disabled and the other one enabled.
132 */
133 if (irq == sock->insert_irq) {
134 disable_irq_nosync(sock->insert_irq);
135 enable_irq(sock->eject_irq);
136 } else {
137 disable_irq_nosync(sock->eject_irq);
138 enable_irq(sock->insert_irq);
139 }
140
141 pcmcia_parse_events(&sock->socket, SS_DETECT);
142
143 return IRQ_HANDLED;
144}
145
146static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
147{
148 int ret;
149 unsigned long flags;
150
151 if (sock->stschg_irq != -1) {
152 ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
153 0, "pcmcia_stschg", sock);
154 if (ret)
155 return ret;
156 }
157
158 /* Db/Pb1200 have separate per-socket insertion and ejection
159 * interrupts, which should show edge behaviour but don't.
160 * So interrupts are disabled until both insertion and
161 * ejection handler have been registered and the currently
162 * active one disabled.
163 */
164 if (sock->board_type == BOARD_TYPE_DB1200) {
165 local_irq_save(flags);
166
167 ret = request_irq(sock->insert_irq, db1200_pcmcia_cdirq,
168 IRQF_DISABLED, "pcmcia_insert", sock);
169 if (ret)
170 goto out1;
171
172 ret = request_irq(sock->eject_irq, db1200_pcmcia_cdirq,
173 IRQF_DISABLED, "pcmcia_eject", sock);
174 if (ret) {
175 free_irq(sock->insert_irq, sock);
176 local_irq_restore(flags);
177 goto out1;
178 }
179
180 /* disable the currently active one */
181 if (db1200_card_inserted(sock))
182 disable_irq_nosync(sock->insert_irq);
183 else
184 disable_irq_nosync(sock->eject_irq);
185
186 local_irq_restore(flags);
187 } else {
188 /* all other (older) Db1x00 boards use a GPIO to show
189 * card detection status: use both-edge triggers.
190 */
191 set_irq_type(sock->insert_irq, IRQ_TYPE_EDGE_BOTH);
192 ret = request_irq(sock->insert_irq, db1000_pcmcia_cdirq,
193 0, "pcmcia_carddetect", sock);
194
195 if (ret)
196 goto out1;
197 }
198
199 return 0; /* all done */
200
201out1:
202 if (sock->stschg_irq != -1)
203 free_irq(sock->stschg_irq, sock);
204
205 return ret;
206}
207
208static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock)
209{
210 if (sock->stschg_irq != -1)
211 free_irq(sock->stschg_irq, sock);
212
213 free_irq(sock->insert_irq, sock);
214 if (sock->eject_irq != -1)
215 free_irq(sock->eject_irq, sock);
216}
217
218/*
219 * configure a PCMCIA socket on the Db1x00 series of boards (and
220 * compatibles).
221 *
222 * 2 external registers are involved:
223 * pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id
224 * pcmcia_control(offset 0x10):
225 * bits[0:1] set vcc for card
226 * bits[2:3] set vpp for card
227 * bit 4: enable data buffers
228 * bit 7: reset# for card
229 * add 8 for second socket.
230 */
231static int db1x_pcmcia_configure(struct pcmcia_socket *skt,
232 struct socket_state_t *state)
233{
234 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
235 unsigned short cr_clr, cr_set;
236 unsigned int changed;
237 int v, p, ret;
238
239 /* card voltage setup */
240 cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
241 cr_set = 0;
242 v = p = ret = 0;
243
244 switch (state->Vcc) {
245 case 50:
246 ++v;
247 case 33:
248 ++v;
249 case 0:
250 break;
251 default:
252 printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
253 sock->nr, state->Vcc);
254 }
255
256 switch (state->Vpp) {
257 case 12:
258 ++p;
259 case 33:
260 case 50:
261 ++p;
262 case 0:
263 break;
264 default:
265 printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n",
266 sock->nr, state->Vpp);
267 }
268
269 /* sanity check: Vpp must be 0, 12, or Vcc */
270 if (((state->Vcc == 33) && (state->Vpp == 50)) ||
271 ((state->Vcc == 50) && (state->Vpp == 33))) {
272 printk(KERN_INFO "pcmcia%d bad Vcc/Vpp combo (%d %d)\n",
273 sock->nr, state->Vcc, state->Vpp);
274 v = p = 0;
275 ret = -EINVAL;
276 }
277
278 /* create new voltage code */
279 cr_set |= ((v << 2) | p) << (sock->nr * 8);
280
281 changed = state->flags ^ sock->old_flags;
282
283 if (changed & SS_RESET) {
284 if (state->flags & SS_RESET) {
285 set_stschg(sock, 0);
286 /* assert reset, disable io buffers */
287 cr_clr |= (1 << (7 + (sock->nr * 8)));
288 cr_clr |= (1 << (4 + (sock->nr * 8)));
289 } else {
290 /* de-assert reset, enable io buffers */
291 cr_set |= 1 << (7 + (sock->nr * 8));
292 cr_set |= 1 << (4 + (sock->nr * 8));
293 }
294 }
295
296 /* update PCMCIA configuration */
297 bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set);
298
299 sock->old_flags = state->flags;
300
301 /* reset was taken away: give card time to initialize properly */
302 if ((changed & SS_RESET) && !(state->flags & SS_RESET)) {
303 msleep(500);
304 set_stschg(sock, 1);
305 }
306
307 return ret;
308}
309
310/* VCC bits at [3:2]/[11:10] */
311#define GET_VCC(cr, socknr) \
312 ((((cr) >> 2) >> ((socknr) * 8)) & 3)
313
314/* VS bits at [0:1]/[3:2] */
315#define GET_VS(sr, socknr) \
316 (((sr) >> (2 * (socknr))) & 3)
317
318/* reset bits at [7]/[15] */
319#define GET_RESET(cr, socknr) \
320 ((cr) & (1 << (7 + (8 * (socknr)))))
321
322static int db1x_pcmcia_get_status(struct pcmcia_socket *skt,
323 unsigned int *value)
324{
325 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
326 unsigned short cr, sr;
327 unsigned int status;
328
329 status = db1x_card_inserted(sock) ? SS_DETECT : 0;
330
331 cr = bcsr_read(BCSR_PCMCIA);
332 sr = bcsr_read(BCSR_STATUS);
333
334 /* PB1100/PB1500: voltage key bits are at [5:4] */
335 if (sock->board_type == BOARD_TYPE_PB1100)
336 sr >>= 4;
337
338 /* determine card type */
339 switch (GET_VS(sr, sock->nr)) {
340 case 0:
341 case 2:
342 status |= SS_3VCARD; /* 3V card */
343 case 3:
344 break; /* 5V card: set nothing */
345 default:
346 status |= SS_XVCARD; /* treated as unsupported in core */
347 }
348
349 /* if Vcc is not zero, we have applied power to a card */
350 status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0;
351
352 /* reset de-asserted? then we're ready */
353 status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET;
354
355 *value = status;
356
357 return 0;
358}
359
360static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
361{
362 return 0;
363}
364
365static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
366{
367 return 0;
368}
369
370static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
371 struct pccard_io_map *map)
372{
373 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
374
375 map->start = (u32)sock->virt_io;
376 map->stop = map->start + IO_MAP_SIZE;
377
378 return 0;
379}
380
381static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
382 struct pccard_mem_map *map)
383{
384 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
385
386 if (map->flags & MAP_ATTRIB)
387 map->static_start = sock->phys_attr + map->card_start;
388 else
389 map->static_start = sock->phys_mem + map->card_start;
390
391 return 0;
392}
393
394static struct pccard_operations db1x_pcmcia_operations = {
395 .init = db1x_pcmcia_sock_init,
396 .suspend = db1x_pcmcia_sock_suspend,
397 .get_status = db1x_pcmcia_get_status,
398 .set_socket = db1x_pcmcia_configure,
399 .set_io_map = au1x00_pcmcia_set_io_map,
400 .set_mem_map = au1x00_pcmcia_set_mem_map,
401};
402
403static int __devinit db1x_pcmcia_socket_probe(struct platform_device *pdev)
404{
405 struct db1x_pcmcia_sock *sock;
406 struct resource *r;
407 phys_t physio;
408 int ret, bid;
409
410 sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
411 if (!sock)
412 return -ENOMEM;
413
414 sock->nr = pdev->id;
415
416 bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
417 switch (bid) {
418 case BCSR_WHOAMI_PB1500:
419 case BCSR_WHOAMI_PB1500R2:
420 case BCSR_WHOAMI_PB1100:
421 sock->board_type = BOARD_TYPE_PB1100;
422 break;
423 case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
424 sock->board_type = BOARD_TYPE_DEFAULT;
425 break;
426 case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
427 sock->board_type = BOARD_TYPE_DB1200;
428 break;
429 default:
430 printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
431 ret = -ENODEV;
432 goto out0;
433 };
434
435 /*
436 * gather resources necessary and optional nice-to-haves to
437 * operate a socket:
438 * This includes IRQs for Carddetection/ejection, the card
439 * itself and optional status change detection.
440 * Also, the memory areas covered by a socket. For these
441 * we require the 32bit "pseudo" addresses (see the au1000.h
442 * header for more information).
443 */
444
445 /* card: irq assigned to the card itself. */
446 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "card");
447 sock->card_irq = r ? r->start : 0;
448
449 /* insert: irq which triggers on card insertion/ejection */
450 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "insert");
451 sock->insert_irq = r ? r->start : -1;
452
453 /* stschg: irq which trigger on card status change (optional) */
454 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "stschg");
455 sock->stschg_irq = r ? r->start : -1;
456
457 /* eject: irq which triggers on ejection (DB1200/PB1200 only) */
458 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "eject");
459 sock->eject_irq = r ? r->start : -1;
460
461 ret = -ENODEV;
462
463 /*
464 * pseudo-attr: The 32bit address of the PCMCIA attribute space
465 * for this socket (usually the 36bit address shifted 4 to the
466 * right).
467 */
468 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pseudo-attr");
469 if (!r) {
470 printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
471 sock->nr);
472 goto out0;
473 }
474 sock->phys_attr = r->start;
475
476 /*
477 * pseudo-mem: The 32bit address of the PCMCIA memory space for
478 * this socket (usually the 36bit address shifted 4 to the right)
479 */
480 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pseudo-mem");
481 if (!r) {
482 printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
483 sock->nr);
484 goto out0;
485 }
486 sock->phys_mem = r->start;
487
488 /*
489 * pseudo-io: The 32bit address of the PCMCIA IO space for this
490 * socket (usually the 36bit address shifted 4 to the right).
491 */
492 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pseudo-io");
493 if (!r) {
494 printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
495 sock->nr);
496 goto out0;
497 }
498 sock->phys_io = r->start;
499
500
501 /* IO: we must remap the full 36bit address (for reference see
502 * alchemy/common/setup.c::__fixup_bigphys_addr())
503 */
504 physio = ((phys_t)sock->phys_io) << 4;
505
506 /*
507 * PCMCIA client drivers use the inb/outb macros to access
508 * the IO registers. Since mips_io_port_base is added
509 * to the access address of the mips implementation of
510 * inb/outb, we need to subtract it here because we want
511 * to access the I/O or MEM address directly, without
512 * going through this "mips_io_port_base" mechanism.
513 */
514 sock->virt_io = (void *)(ioremap(physio, IO_MAP_SIZE) -
515 mips_io_port_base);
516
517 if (!sock->virt_io) {
518 printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
519 sock->nr);
520 ret = -ENOMEM;
521 goto out0;
522 }
523
524 sock->socket.ops = &db1x_pcmcia_operations;
525 sock->socket.owner = THIS_MODULE;
526 sock->socket.pci_irq = sock->card_irq;
527 sock->socket.features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
528 sock->socket.map_size = MEM_MAP_SIZE;
529 sock->socket.io_offset = (unsigned long)sock->virt_io;
530 sock->socket.dev.parent = &pdev->dev;
531 sock->socket.resource_ops = &pccard_static_ops;
532
533 platform_set_drvdata(pdev, sock);
534
535 ret = db1x_pcmcia_setup_irqs(sock);
536 if (ret) {
537 printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
538 sock->nr);
539 goto out1;
540 }
541
542 set_stschg(sock, 0);
543
544 ret = pcmcia_register_socket(&sock->socket);
545 if (ret) {
546 printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
547 goto out2;
548 }
549
550 printk(KERN_INFO "Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %08lx"
551 "(%p) %08lx %08lx card/insert/stschg/eject irqs @ %d "
552 "%d %d %d\n", sock->nr, sock->phys_io, sock->virt_io,
553 sock->phys_attr, sock->phys_mem, sock->card_irq,
554 sock->insert_irq, sock->stschg_irq, sock->eject_irq);
555
556 return 0;
557
558out2:
559 db1x_pcmcia_free_irqs(sock);
560out1:
561 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
562out0:
563 kfree(sock);
564 return ret;
565}
566
567static int __devexit db1x_pcmcia_socket_remove(struct platform_device *pdev)
568{
569 struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev);
570
571 db1x_pcmcia_free_irqs(sock);
572 pcmcia_unregister_socket(&sock->socket);
573 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
574 kfree(sock);
575
576 return 0;
577}
578
579#ifdef CONFIG_PM
580static int db1x_pcmcia_suspend(struct device *dev)
581{
582 return pcmcia_socket_dev_suspend(dev);
583}
584
585static int db1x_pcmcia_resume(struct device *dev)
586{
587 return pcmcia_socket_dev_resume(dev);
588}
589
590static struct dev_pm_ops db1x_pcmcia_pmops = {
591 .resume = db1x_pcmcia_resume,
592 .suspend = db1x_pcmcia_suspend,
593 .thaw = db1x_pcmcia_resume,
594 .freeze = db1x_pcmcia_suspend,
595};
596
597#define DB1XXX_SS_PMOPS &db1x_pcmcia_pmops
598
599#else
600
601#define DB1XXX_SS_PMOPS NULL
602
603#endif
604
605static struct platform_driver db1x_pcmcia_socket_driver = {
606 .driver = {
607 .name = "db1xxx_pcmcia",
608 .owner = THIS_MODULE,
609 .pm = DB1XXX_SS_PMOPS
610 },
611 .probe = db1x_pcmcia_socket_probe,
612 .remove = __devexit_p(db1x_pcmcia_socket_remove),
613};
614
615int __init db1x_pcmcia_socket_load(void)
616{
617 return platform_driver_register(&db1x_pcmcia_socket_driver);
618}
619
620void __exit db1x_pcmcia_socket_unload(void)
621{
622 platform_driver_unregister(&db1x_pcmcia_socket_driver);
623}
624
625module_init(db1x_pcmcia_socket_load);
626module_exit(db1x_pcmcia_socket_unload);
627
628MODULE_LICENSE("GPL");
629MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
630MODULE_AUTHOR("Manuel Lauss");