xref: /openbmc/linux/drivers/pcmcia/db1xxx_ss.c (revision c67e8ec0)
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, Db1300
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  *	- Db1300:	  Db1200-like, no pwr ctrl, single socket (#1).
22  */
23 
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>
33 
34 #include <pcmcia/ss.h>
35 
36 #include <asm/mach-au1x00/au1000.h>
37 #include <asm/mach-db1x00/bcsr.h>
38 
39 #define MEM_MAP_SIZE	0x400000
40 #define IO_MAP_SIZE	0x1000
41 
42 struct db1x_pcmcia_sock {
43 	struct pcmcia_socket	socket;
44 	int		nr;		/* socket number */
45 	void		*virt_io;
46 
47 	phys_addr_t	phys_io;
48 	phys_addr_t	phys_attr;
49 	phys_addr_t	phys_mem;
50 
51 	/* previous flags for set_socket() */
52 	unsigned int old_flags;
53 
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 */
60 
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;
66 };
67 
68 #define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket)
69 
70 static int db1300_card_inserted(struct db1x_pcmcia_sock *sock)
71 {
72 	return bcsr_read(BCSR_SIGSTAT) & (1 << 8);
73 }
74 
75 /* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */
76 static int db1200_card_inserted(struct db1x_pcmcia_sock *sock)
77 {
78 	unsigned short sigstat;
79 
80 	sigstat = bcsr_read(BCSR_SIGSTAT);
81 	return sigstat & 1 << (8 + 2 * sock->nr);
82 }
83 
84 /* carddetect gpio: low-active */
85 static int db1000_card_inserted(struct db1x_pcmcia_sock *sock)
86 {
87 	return !gpio_get_value(sock->insert_gpio);
88 }
89 
90 static int db1x_card_inserted(struct db1x_pcmcia_sock *sock)
91 {
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);
99 	}
100 }
101 
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.
105  */
106 static inline void set_stschg(struct db1x_pcmcia_sock *sock, int en)
107 {
108 	if (sock->stschg_irq != -1) {
109 		if (en)
110 			enable_irq(sock->stschg_irq);
111 		else
112 			disable_irq(sock->stschg_irq);
113 	}
114 }
115 
116 static irqreturn_t db1000_pcmcia_cdirq(int irq, void *data)
117 {
118 	struct db1x_pcmcia_sock *sock = data;
119 
120 	pcmcia_parse_events(&sock->socket, SS_DETECT);
121 
122 	return IRQ_HANDLED;
123 }
124 
125 static irqreturn_t db1000_pcmcia_stschgirq(int irq, void *data)
126 {
127 	struct db1x_pcmcia_sock *sock = data;
128 
129 	pcmcia_parse_events(&sock->socket, SS_STSCHG);
130 
131 	return IRQ_HANDLED;
132 }
133 
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.
138  */
139 static irqreturn_t db1200_pcmcia_cdirq(int irq, void *data)
140 {
141 	disable_irq_nosync(irq);
142 	return IRQ_WAKE_THREAD;
143 }
144 
145 static irqreturn_t db1200_pcmcia_cdirq_fn(int irq, void *data)
146 {
147 	struct db1x_pcmcia_sock *sock = data;
148 
149 	/* Wait a bit for the signals to stop bouncing. */
150 	msleep(100);
151 	if (irq == sock->insert_irq)
152 		enable_irq(sock->eject_irq);
153 	else
154 		enable_irq(sock->insert_irq);
155 
156 	pcmcia_parse_events(&sock->socket, SS_DETECT);
157 
158 	return IRQ_HANDLED;
159 }
160 
161 static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
162 {
163 	int ret;
164 
165 	if (sock->stschg_irq != -1) {
166 		ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
167 				  0, "pcmcia_stschg", sock);
168 		if (ret)
169 			return ret;
170 	}
171 
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.
177 	 */
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);
182 		if (ret)
183 			goto out1;
184 
185 		ret = request_threaded_irq(sock->eject_irq, db1200_pcmcia_cdirq,
186 			db1200_pcmcia_cdirq_fn, 0, "pcmcia_eject", sock);
187 		if (ret) {
188 			free_irq(sock->insert_irq, sock);
189 			goto out1;
190 		}
191 
192 		/* enable the currently silent one */
193 		if (db1x_card_inserted(sock))
194 			enable_irq(sock->eject_irq);
195 		else
196 			enable_irq(sock->insert_irq);
197 	} else {
198 		/* all other (older) Db1x00 boards use a GPIO to show
199 		 * card detection status:  use both-edge triggers.
200 		 */
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);
204 
205 		if (ret)
206 			goto out1;
207 	}
208 
209 	return 0;	/* all done */
210 
211 out1:
212 	if (sock->stschg_irq != -1)
213 		free_irq(sock->stschg_irq, sock);
214 
215 	return ret;
216 }
217 
218 static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock)
219 {
220 	if (sock->stschg_irq != -1)
221 		free_irq(sock->stschg_irq, sock);
222 
223 	free_irq(sock->insert_irq, sock);
224 	if (sock->eject_irq != -1)
225 		free_irq(sock->eject_irq, sock);
226 }
227 
228 /*
229  * configure a PCMCIA socket on the Db1x00 series of boards (and
230  * compatibles).
231  *
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.
240  */
241 static int db1x_pcmcia_configure(struct pcmcia_socket *skt,
242 				 struct socket_state_t *state)
243 {
244 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
245 	unsigned short cr_clr, cr_set;
246 	unsigned int changed;
247 	int v, p, ret;
248 
249 	/* card voltage setup */
250 	cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
251 	cr_set = 0;
252 	v = p = ret = 0;
253 
254 	switch (state->Vcc) {
255 	case 50:
256 		++v;
257 	case 33:
258 		++v;
259 	case 0:
260 		break;
261 	default:
262 		printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
263 			sock->nr, state->Vcc);
264 	}
265 
266 	switch (state->Vpp) {
267 	case 12:
268 		++p;
269 	case 33:
270 	case 50:
271 		++p;
272 	case 0:
273 		break;
274 	default:
275 		printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n",
276 			sock->nr, state->Vpp);
277 	}
278 
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);
284 		v = p = 0;
285 		ret = -EINVAL;
286 	}
287 
288 	/* create new voltage code */
289 	if (sock->board_type != BOARD_TYPE_DB1300)
290 		cr_set |= ((v << 2) | p) << (sock->nr * 8);
291 
292 	changed = state->flags ^ sock->old_flags;
293 
294 	if (changed & SS_RESET) {
295 		if (state->flags & SS_RESET) {
296 			set_stschg(sock, 0);
297 			/* assert reset, disable io buffers */
298 			cr_clr |= (1 << (7 + (sock->nr * 8)));
299 			cr_clr |= (1 << (4 + (sock->nr * 8)));
300 		} else {
301 			/* de-assert reset, enable io buffers */
302 			cr_set |= 1 << (7 + (sock->nr * 8));
303 			cr_set |= 1 << (4 + (sock->nr * 8));
304 		}
305 	}
306 
307 	/* update PCMCIA configuration */
308 	bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set);
309 
310 	sock->old_flags = state->flags;
311 
312 	/* reset was taken away: give card time to initialize properly */
313 	if ((changed & SS_RESET) && !(state->flags & SS_RESET)) {
314 		msleep(500);
315 		set_stschg(sock, 1);
316 	}
317 
318 	return ret;
319 }
320 
321 /* VCC bits at [3:2]/[11:10] */
322 #define GET_VCC(cr, socknr)		\
323 	((((cr) >> 2) >> ((socknr) * 8)) & 3)
324 
325 /* VS bits at [0:1]/[3:2] */
326 #define GET_VS(sr, socknr)		\
327 	(((sr) >> (2 * (socknr))) & 3)
328 
329 /* reset bits at [7]/[15] */
330 #define GET_RESET(cr, socknr)		\
331 	((cr) & (1 << (7 + (8 * (socknr)))))
332 
333 static int db1x_pcmcia_get_status(struct pcmcia_socket *skt,
334 				  unsigned int *value)
335 {
336 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
337 	unsigned short cr, sr;
338 	unsigned int status;
339 
340 	status = db1x_card_inserted(sock) ? SS_DETECT : 0;
341 
342 	cr = bcsr_read(BCSR_PCMCIA);
343 	sr = bcsr_read(BCSR_STATUS);
344 
345 	/* PB1100/PB1500: voltage key bits are at [5:4] */
346 	if (sock->board_type == BOARD_TYPE_PB1100)
347 		sr >>= 4;
348 
349 	/* determine card type */
350 	switch (GET_VS(sr, sock->nr)) {
351 	case 0:
352 	case 2:
353 		status |= SS_3VCARD;	/* 3V card */
354 	case 3:
355 		break;			/* 5V card: set nothing */
356 	default:
357 		status |= SS_XVCARD;	/* treated as unsupported in core */
358 	}
359 
360 	/* if Vcc is not zero, we have applied power to a card */
361 	status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0;
362 
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;
366 
367 	/* reset de-asserted? then we're ready */
368 	status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET;
369 
370 	*value = status;
371 
372 	return 0;
373 }
374 
375 static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
376 {
377 	return 0;
378 }
379 
380 static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
381 {
382 	return 0;
383 }
384 
385 static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
386 				    struct pccard_io_map *map)
387 {
388 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
389 
390 	map->start = (u32)sock->virt_io;
391 	map->stop = map->start + IO_MAP_SIZE;
392 
393 	return 0;
394 }
395 
396 static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
397 				     struct pccard_mem_map *map)
398 {
399 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
400 
401 	if (map->flags & MAP_ATTRIB)
402 		map->static_start = sock->phys_attr + map->card_start;
403 	else
404 		map->static_start = sock->phys_mem + map->card_start;
405 
406 	return 0;
407 }
408 
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,
416 };
417 
418 static int db1x_pcmcia_socket_probe(struct platform_device *pdev)
419 {
420 	struct db1x_pcmcia_sock *sock;
421 	struct resource *r;
422 	int ret, bid;
423 
424 	sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
425 	if (!sock)
426 		return -ENOMEM;
427 
428 	sock->nr = pdev->id;
429 
430 	bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
431 	switch (bid) {
432 	case BCSR_WHOAMI_PB1500:
433 	case BCSR_WHOAMI_PB1500R2:
434 	case BCSR_WHOAMI_PB1100:
435 		sock->board_type = BOARD_TYPE_PB1100;
436 		break;
437 	case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
438 		sock->board_type = BOARD_TYPE_DEFAULT;
439 		break;
440 	case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
441 		sock->board_type = BOARD_TYPE_DB1200;
442 		break;
443 	case BCSR_WHOAMI_DB1300:
444 		sock->board_type = BOARD_TYPE_DB1300;
445 		break;
446 	default:
447 		printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
448 		ret = -ENODEV;
449 		goto out0;
450 	};
451 
452 	/*
453 	 * gather resources necessary and optional nice-to-haves to
454 	 * operate a socket:
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).
460 	 */
461 
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;
465 
466 	/* insert: irq which triggers on card insertion/ejection
467 	 * BIG FAT NOTE: on DB1000/1100/1500/1550 we pass a GPIO here!
468 	 */
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;
474 	}
475 
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;
479 
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;
483 
484 	ret = -ENODEV;
485 
486 	/* 36bit PCMCIA Attribute area address */
487 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
488 	if (!r) {
489 		printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
490 			sock->nr);
491 		goto out0;
492 	}
493 	sock->phys_attr = r->start;
494 
495 	/* 36bit PCMCIA Memory area address */
496 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
497 	if (!r) {
498 		printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
499 			sock->nr);
500 		goto out0;
501 	}
502 	sock->phys_mem = r->start;
503 
504 	/* 36bit PCMCIA IO area address */
505 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
506 	if (!r) {
507 		printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
508 			sock->nr);
509 		goto out0;
510 	}
511 	sock->phys_io = r->start;
512 
513 	/*
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.
520 	 */
521 	sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) -
522 				 mips_io_port_base);
523 
524 	if (!sock->virt_io) {
525 		printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
526 			sock->nr);
527 		ret = -ENOMEM;
528 		goto out0;
529 	}
530 
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;
539 
540 	platform_set_drvdata(pdev, sock);
541 
542 	ret = db1x_pcmcia_setup_irqs(sock);
543 	if (ret) {
544 		printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
545 			sock->nr);
546 		goto out1;
547 	}
548 
549 	set_stschg(sock, 0);
550 
551 	ret = pcmcia_register_socket(&sock->socket);
552 	if (ret) {
553 		printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
554 		goto out2;
555 	}
556 
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);
562 
563 	return 0;
564 
565 out2:
566 	db1x_pcmcia_free_irqs(sock);
567 out1:
568 	iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
569 out0:
570 	kfree(sock);
571 	return ret;
572 }
573 
574 static int db1x_pcmcia_socket_remove(struct platform_device *pdev)
575 {
576 	struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev);
577 
578 	db1x_pcmcia_free_irqs(sock);
579 	pcmcia_unregister_socket(&sock->socket);
580 	iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
581 	kfree(sock);
582 
583 	return 0;
584 }
585 
586 static struct platform_driver db1x_pcmcia_socket_driver = {
587 	.driver	= {
588 		.name	= "db1xxx_pcmcia",
589 	},
590 	.probe		= db1x_pcmcia_socket_probe,
591 	.remove		= db1x_pcmcia_socket_remove,
592 };
593 
594 module_platform_driver(db1x_pcmcia_socket_driver);
595 
596 MODULE_LICENSE("GPL");
597 MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
598 MODULE_AUTHOR("Manuel Lauss");
599