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