xref: /openbmc/linux/drivers/pcmcia/db1xxx_ss.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
266213b3cSManuel Lauss /*
366213b3cSManuel Lauss  * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards.
466213b3cSManuel Lauss  *
566213b3cSManuel Lauss  * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
666213b3cSManuel Lauss  *
766213b3cSManuel Lauss  */
866213b3cSManuel Lauss 
966213b3cSManuel Lauss /* This is a fairly generic PCMCIA socket driver suitable for the
1066213b3cSManuel Lauss  * following Alchemy Development boards:
1164cd04d0SManuel Lauss  *  Db1000, Db/Pb1500, Db/Pb1100, Db/Pb1550, Db/Pb1200, Db1300
1266213b3cSManuel Lauss  *
1366213b3cSManuel Lauss  * The Db1000 is used as a reference:  Per-socket card-, carddetect- and
1466213b3cSManuel Lauss  *  statuschange IRQs connected to SoC GPIOs, control and status register
1566213b3cSManuel Lauss  *  bits arranged in per-socket groups in an external PLD.  All boards
1666213b3cSManuel Lauss  *  listed here use this layout, including bit positions and meanings.
1766213b3cSManuel Lauss  *  Of course there are exceptions in later boards:
1866213b3cSManuel Lauss  *
1966213b3cSManuel Lauss  *	- Pb1100/Pb1500:  single socket only; voltage key bits VS are
2066213b3cSManuel Lauss  *			  at STATUS[5:4] (instead of STATUS[1:0]).
2166213b3cSManuel Lauss  *	- Au1200-based:	  additional card-eject irqs, irqs not gpios!
2264cd04d0SManuel Lauss  *	- Db1300:	  Db1200-like, no pwr ctrl, single socket (#1).
2366213b3cSManuel Lauss  */
2466213b3cSManuel Lauss 
2566213b3cSManuel Lauss #include <linux/delay.h>
2666213b3cSManuel Lauss #include <linux/gpio.h>
2766213b3cSManuel Lauss #include <linux/interrupt.h>
2866213b3cSManuel Lauss #include <linux/pm.h>
293bf8d64dSPaul Gortmaker #include <linux/module.h>
3066213b3cSManuel Lauss #include <linux/platform_device.h>
3166213b3cSManuel Lauss #include <linux/resource.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
3366213b3cSManuel Lauss #include <linux/spinlock.h>
3466213b3cSManuel Lauss 
3566213b3cSManuel Lauss #include <pcmcia/ss.h>
3666213b3cSManuel Lauss 
3766213b3cSManuel Lauss #include <asm/mach-au1x00/au1000.h>
3866213b3cSManuel Lauss #include <asm/mach-db1x00/bcsr.h>
3966213b3cSManuel Lauss 
4066213b3cSManuel Lauss #define MEM_MAP_SIZE	0x400000
4166213b3cSManuel Lauss #define IO_MAP_SIZE	0x1000
4266213b3cSManuel Lauss 
4366213b3cSManuel Lauss struct db1x_pcmcia_sock {
4466213b3cSManuel Lauss 	struct pcmcia_socket	socket;
4566213b3cSManuel Lauss 	int		nr;		/* socket number */
4666213b3cSManuel Lauss 	void		*virt_io;
4766213b3cSManuel Lauss 
4811b897cfSManuel Lauss 	phys_addr_t	phys_io;
4911b897cfSManuel Lauss 	phys_addr_t	phys_attr;
5011b897cfSManuel Lauss 	phys_addr_t	phys_mem;
5166213b3cSManuel Lauss 
5266213b3cSManuel Lauss 	/* previous flags for set_socket() */
5366213b3cSManuel Lauss 	unsigned int old_flags;
5466213b3cSManuel Lauss 
5566213b3cSManuel Lauss 	/* interrupt sources: linux irq numbers! */
5666213b3cSManuel Lauss 	int	insert_irq;	/* default carddetect irq */
5766213b3cSManuel Lauss 	int	stschg_irq;	/* card-status-change irq */
5866213b3cSManuel Lauss 	int	card_irq;	/* card irq */
5966213b3cSManuel Lauss 	int	eject_irq;	/* db1200/pb1200 have these */
60e34b6fcfSManuel Lauss 	int	insert_gpio;	/* db1000 carddetect gpio */
6166213b3cSManuel Lauss 
6266213b3cSManuel Lauss #define BOARD_TYPE_DEFAULT	0	/* most boards */
6366213b3cSManuel Lauss #define BOARD_TYPE_DB1200	1	/* IRQs aren't gpios */
6466213b3cSManuel Lauss #define BOARD_TYPE_PB1100	2	/* VS bits slightly different */
6564cd04d0SManuel Lauss #define BOARD_TYPE_DB1300	3	/* no power control */
6666213b3cSManuel Lauss 	int	board_type;
6766213b3cSManuel Lauss };
6866213b3cSManuel Lauss 
6966213b3cSManuel Lauss #define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket)
7066213b3cSManuel Lauss 
db1300_card_inserted(struct db1x_pcmcia_sock * sock)7164cd04d0SManuel Lauss static int db1300_card_inserted(struct db1x_pcmcia_sock *sock)
7264cd04d0SManuel Lauss {
7364cd04d0SManuel Lauss 	return bcsr_read(BCSR_SIGSTAT) & (1 << 8);
7464cd04d0SManuel Lauss }
7564cd04d0SManuel Lauss 
7666213b3cSManuel Lauss /* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */
db1200_card_inserted(struct db1x_pcmcia_sock * sock)7766213b3cSManuel Lauss static int db1200_card_inserted(struct db1x_pcmcia_sock *sock)
7866213b3cSManuel Lauss {
7966213b3cSManuel Lauss 	unsigned short sigstat;
8066213b3cSManuel Lauss 
8166213b3cSManuel Lauss 	sigstat = bcsr_read(BCSR_SIGSTAT);
8266213b3cSManuel Lauss 	return sigstat & 1 << (8 + 2 * sock->nr);
8366213b3cSManuel Lauss }
8466213b3cSManuel Lauss 
8566213b3cSManuel Lauss /* carddetect gpio: low-active */
db1000_card_inserted(struct db1x_pcmcia_sock * sock)8666213b3cSManuel Lauss static int db1000_card_inserted(struct db1x_pcmcia_sock *sock)
8766213b3cSManuel Lauss {
88e34b6fcfSManuel Lauss 	return !gpio_get_value(sock->insert_gpio);
8966213b3cSManuel Lauss }
9066213b3cSManuel Lauss 
db1x_card_inserted(struct db1x_pcmcia_sock * sock)9166213b3cSManuel Lauss static int db1x_card_inserted(struct db1x_pcmcia_sock *sock)
9266213b3cSManuel Lauss {
9366213b3cSManuel Lauss 	switch (sock->board_type) {
9466213b3cSManuel Lauss 	case BOARD_TYPE_DB1200:
9566213b3cSManuel Lauss 		return db1200_card_inserted(sock);
9664cd04d0SManuel Lauss 	case BOARD_TYPE_DB1300:
9764cd04d0SManuel Lauss 		return db1300_card_inserted(sock);
9866213b3cSManuel Lauss 	default:
9966213b3cSManuel Lauss 		return db1000_card_inserted(sock);
10066213b3cSManuel Lauss 	}
10166213b3cSManuel Lauss }
10266213b3cSManuel Lauss 
10366213b3cSManuel Lauss /* STSCHG tends to bounce heavily when cards are inserted/ejected.
10466213b3cSManuel Lauss  * To avoid this, the interrupt is normally disabled and only enabled
10566213b3cSManuel Lauss  * after reset to a card has been de-asserted.
10666213b3cSManuel Lauss  */
set_stschg(struct db1x_pcmcia_sock * sock,int en)10766213b3cSManuel Lauss static inline void set_stschg(struct db1x_pcmcia_sock *sock, int en)
10866213b3cSManuel Lauss {
10966213b3cSManuel Lauss 	if (sock->stschg_irq != -1) {
11066213b3cSManuel Lauss 		if (en)
11166213b3cSManuel Lauss 			enable_irq(sock->stschg_irq);
11266213b3cSManuel Lauss 		else
11366213b3cSManuel Lauss 			disable_irq(sock->stschg_irq);
11466213b3cSManuel Lauss 	}
11566213b3cSManuel Lauss }
11666213b3cSManuel Lauss 
db1000_pcmcia_cdirq(int irq,void * data)11766213b3cSManuel Lauss static irqreturn_t db1000_pcmcia_cdirq(int irq, void *data)
11866213b3cSManuel Lauss {
11966213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = data;
12066213b3cSManuel Lauss 
12166213b3cSManuel Lauss 	pcmcia_parse_events(&sock->socket, SS_DETECT);
12266213b3cSManuel Lauss 
12366213b3cSManuel Lauss 	return IRQ_HANDLED;
12466213b3cSManuel Lauss }
12566213b3cSManuel Lauss 
db1000_pcmcia_stschgirq(int irq,void * data)12666213b3cSManuel Lauss static irqreturn_t db1000_pcmcia_stschgirq(int irq, void *data)
12766213b3cSManuel Lauss {
12866213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = data;
12966213b3cSManuel Lauss 
13066213b3cSManuel Lauss 	pcmcia_parse_events(&sock->socket, SS_STSCHG);
13166213b3cSManuel Lauss 
13266213b3cSManuel Lauss 	return IRQ_HANDLED;
13366213b3cSManuel Lauss }
13466213b3cSManuel Lauss 
13566213b3cSManuel Lauss /* Db/Pb1200 have separate per-socket insertion and ejection
13666213b3cSManuel Lauss  * interrupts which stay asserted as long as the card is
13766213b3cSManuel Lauss  * inserted/missing.  The one which caused us to be called
13866213b3cSManuel Lauss  * needs to be disabled and the other one enabled.
13966213b3cSManuel Lauss  */
db1200_pcmcia_cdirq(int irq,void * data)140cc10815eSManuel Lauss static irqreturn_t db1200_pcmcia_cdirq(int irq, void *data)
141cc10815eSManuel Lauss {
142cc10815eSManuel Lauss 	disable_irq_nosync(irq);
143cc10815eSManuel Lauss 	return IRQ_WAKE_THREAD;
14466213b3cSManuel Lauss }
14566213b3cSManuel Lauss 
db1200_pcmcia_cdirq_fn(int irq,void * data)146cc10815eSManuel Lauss static irqreturn_t db1200_pcmcia_cdirq_fn(int irq, void *data)
147cc10815eSManuel Lauss {
148cc10815eSManuel Lauss 	struct db1x_pcmcia_sock *sock = data;
149cc10815eSManuel Lauss 
150cc10815eSManuel Lauss 	/* Wait a bit for the signals to stop bouncing. */
151cc10815eSManuel Lauss 	msleep(100);
152cc10815eSManuel Lauss 	if (irq == sock->insert_irq)
153cc10815eSManuel Lauss 		enable_irq(sock->eject_irq);
154cc10815eSManuel Lauss 	else
155cc10815eSManuel Lauss 		enable_irq(sock->insert_irq);
156cc10815eSManuel Lauss 
15766213b3cSManuel Lauss 	pcmcia_parse_events(&sock->socket, SS_DETECT);
15866213b3cSManuel Lauss 
15966213b3cSManuel Lauss 	return IRQ_HANDLED;
16066213b3cSManuel Lauss }
16166213b3cSManuel Lauss 
db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock * sock)16266213b3cSManuel Lauss static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
16366213b3cSManuel Lauss {
16466213b3cSManuel Lauss 	int ret;
16566213b3cSManuel Lauss 
16666213b3cSManuel Lauss 	if (sock->stschg_irq != -1) {
16766213b3cSManuel Lauss 		ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
16866213b3cSManuel Lauss 				  0, "pcmcia_stschg", sock);
16966213b3cSManuel Lauss 		if (ret)
17066213b3cSManuel Lauss 			return ret;
17166213b3cSManuel Lauss 	}
17266213b3cSManuel Lauss 
17366213b3cSManuel Lauss 	/* Db/Pb1200 have separate per-socket insertion and ejection
17466213b3cSManuel Lauss 	 * interrupts, which should show edge behaviour but don't.
17566213b3cSManuel Lauss 	 * So interrupts are disabled until both insertion and
17666213b3cSManuel Lauss 	 * ejection handler have been registered and the currently
17766213b3cSManuel Lauss 	 * active one disabled.
17866213b3cSManuel Lauss 	 */
17964cd04d0SManuel Lauss 	if ((sock->board_type == BOARD_TYPE_DB1200) ||
18064cd04d0SManuel Lauss 	    (sock->board_type == BOARD_TYPE_DB1300)) {
181cc10815eSManuel Lauss 		ret = request_threaded_irq(sock->insert_irq, db1200_pcmcia_cdirq,
182cc10815eSManuel Lauss 			db1200_pcmcia_cdirq_fn, 0, "pcmcia_insert", sock);
1830000a539SManuel Lauss 		if (ret)
18466213b3cSManuel Lauss 			goto out1;
18566213b3cSManuel Lauss 
186cc10815eSManuel Lauss 		ret = request_threaded_irq(sock->eject_irq, db1200_pcmcia_cdirq,
187cc10815eSManuel Lauss 			db1200_pcmcia_cdirq_fn, 0, "pcmcia_eject", sock);
18866213b3cSManuel Lauss 		if (ret) {
18966213b3cSManuel Lauss 			free_irq(sock->insert_irq, sock);
19066213b3cSManuel Lauss 			goto out1;
19166213b3cSManuel Lauss 		}
19266213b3cSManuel Lauss 
1930000a539SManuel Lauss 		/* enable the currently silent one */
19464cd04d0SManuel Lauss 		if (db1x_card_inserted(sock))
1950000a539SManuel Lauss 			enable_irq(sock->eject_irq);
19666213b3cSManuel Lauss 		else
1970000a539SManuel Lauss 			enable_irq(sock->insert_irq);
19866213b3cSManuel Lauss 	} else {
19966213b3cSManuel Lauss 		/* all other (older) Db1x00 boards use a GPIO to show
20066213b3cSManuel Lauss 		 * card detection status:  use both-edge triggers.
20166213b3cSManuel Lauss 		 */
202dced35aeSThomas Gleixner 		irq_set_irq_type(sock->insert_irq, IRQ_TYPE_EDGE_BOTH);
20366213b3cSManuel Lauss 		ret = request_irq(sock->insert_irq, db1000_pcmcia_cdirq,
20466213b3cSManuel Lauss 				  0, "pcmcia_carddetect", sock);
20566213b3cSManuel Lauss 
20666213b3cSManuel Lauss 		if (ret)
20766213b3cSManuel Lauss 			goto out1;
20866213b3cSManuel Lauss 	}
20966213b3cSManuel Lauss 
21066213b3cSManuel Lauss 	return 0;	/* all done */
21166213b3cSManuel Lauss 
21266213b3cSManuel Lauss out1:
21366213b3cSManuel Lauss 	if (sock->stschg_irq != -1)
21466213b3cSManuel Lauss 		free_irq(sock->stschg_irq, sock);
21566213b3cSManuel Lauss 
21666213b3cSManuel Lauss 	return ret;
21766213b3cSManuel Lauss }
21866213b3cSManuel Lauss 
db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock * sock)21966213b3cSManuel Lauss static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock)
22066213b3cSManuel Lauss {
22166213b3cSManuel Lauss 	if (sock->stschg_irq != -1)
22266213b3cSManuel Lauss 		free_irq(sock->stschg_irq, sock);
22366213b3cSManuel Lauss 
22466213b3cSManuel Lauss 	free_irq(sock->insert_irq, sock);
22566213b3cSManuel Lauss 	if (sock->eject_irq != -1)
22666213b3cSManuel Lauss 		free_irq(sock->eject_irq, sock);
22766213b3cSManuel Lauss }
22866213b3cSManuel Lauss 
22966213b3cSManuel Lauss /*
23066213b3cSManuel Lauss  * configure a PCMCIA socket on the Db1x00 series of boards (and
23166213b3cSManuel Lauss  * compatibles).
23266213b3cSManuel Lauss  *
23366213b3cSManuel Lauss  * 2 external registers are involved:
23466213b3cSManuel Lauss  *   pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id
23566213b3cSManuel Lauss  *   pcmcia_control(offset 0x10):
23666213b3cSManuel Lauss  *	bits[0:1] set vcc for card
23766213b3cSManuel Lauss  *	bits[2:3] set vpp for card
23866213b3cSManuel Lauss  *	bit 4:	enable data buffers
23966213b3cSManuel Lauss  *	bit 7:	reset# for card
24066213b3cSManuel Lauss  *	add 8 for second socket.
24166213b3cSManuel Lauss  */
db1x_pcmcia_configure(struct pcmcia_socket * skt,struct socket_state_t * state)24266213b3cSManuel Lauss static int db1x_pcmcia_configure(struct pcmcia_socket *skt,
24366213b3cSManuel Lauss 				 struct socket_state_t *state)
24466213b3cSManuel Lauss {
24566213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
24666213b3cSManuel Lauss 	unsigned short cr_clr, cr_set;
24766213b3cSManuel Lauss 	unsigned int changed;
24866213b3cSManuel Lauss 	int v, p, ret;
24966213b3cSManuel Lauss 
25066213b3cSManuel Lauss 	/* card voltage setup */
25166213b3cSManuel Lauss 	cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
25266213b3cSManuel Lauss 	cr_set = 0;
25366213b3cSManuel Lauss 	v = p = ret = 0;
25466213b3cSManuel Lauss 
25566213b3cSManuel Lauss 	switch (state->Vcc) {
25666213b3cSManuel Lauss 	case 50:
25766213b3cSManuel Lauss 		++v;
258df561f66SGustavo A. R. Silva 		fallthrough;
25966213b3cSManuel Lauss 	case 33:
26066213b3cSManuel Lauss 		++v;
261df561f66SGustavo A. R. Silva 		fallthrough;
26266213b3cSManuel Lauss 	case 0:
26366213b3cSManuel Lauss 		break;
26466213b3cSManuel Lauss 	default:
26566213b3cSManuel Lauss 		printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
26666213b3cSManuel Lauss 			sock->nr, state->Vcc);
26766213b3cSManuel Lauss 	}
26866213b3cSManuel Lauss 
26966213b3cSManuel Lauss 	switch (state->Vpp) {
27066213b3cSManuel Lauss 	case 12:
27166213b3cSManuel Lauss 		++p;
272df561f66SGustavo A. R. Silva 		fallthrough;
27366213b3cSManuel Lauss 	case 33:
27466213b3cSManuel Lauss 	case 50:
27566213b3cSManuel Lauss 		++p;
276df561f66SGustavo A. R. Silva 		fallthrough;
27766213b3cSManuel Lauss 	case 0:
27866213b3cSManuel Lauss 		break;
27966213b3cSManuel Lauss 	default:
28066213b3cSManuel Lauss 		printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n",
28166213b3cSManuel Lauss 			sock->nr, state->Vpp);
28266213b3cSManuel Lauss 	}
28366213b3cSManuel Lauss 
28466213b3cSManuel Lauss 	/* sanity check: Vpp must be 0, 12, or Vcc */
28566213b3cSManuel Lauss 	if (((state->Vcc == 33) && (state->Vpp == 50)) ||
28666213b3cSManuel Lauss 	    ((state->Vcc == 50) && (state->Vpp == 33))) {
28766213b3cSManuel Lauss 		printk(KERN_INFO "pcmcia%d bad Vcc/Vpp combo (%d %d)\n",
28866213b3cSManuel Lauss 			sock->nr, state->Vcc, state->Vpp);
28966213b3cSManuel Lauss 		v = p = 0;
29066213b3cSManuel Lauss 		ret = -EINVAL;
29166213b3cSManuel Lauss 	}
29266213b3cSManuel Lauss 
29366213b3cSManuel Lauss 	/* create new voltage code */
29464cd04d0SManuel Lauss 	if (sock->board_type != BOARD_TYPE_DB1300)
29566213b3cSManuel Lauss 		cr_set |= ((v << 2) | p) << (sock->nr * 8);
29666213b3cSManuel Lauss 
29766213b3cSManuel Lauss 	changed = state->flags ^ sock->old_flags;
29866213b3cSManuel Lauss 
29966213b3cSManuel Lauss 	if (changed & SS_RESET) {
30066213b3cSManuel Lauss 		if (state->flags & SS_RESET) {
30166213b3cSManuel Lauss 			set_stschg(sock, 0);
30266213b3cSManuel Lauss 			/* assert reset, disable io buffers */
30366213b3cSManuel Lauss 			cr_clr |= (1 << (7 + (sock->nr * 8)));
30466213b3cSManuel Lauss 			cr_clr |= (1 << (4 + (sock->nr * 8)));
30566213b3cSManuel Lauss 		} else {
30666213b3cSManuel Lauss 			/* de-assert reset, enable io buffers */
30766213b3cSManuel Lauss 			cr_set |= 1 << (7 + (sock->nr * 8));
30866213b3cSManuel Lauss 			cr_set |= 1 << (4 + (sock->nr * 8));
30966213b3cSManuel Lauss 		}
31066213b3cSManuel Lauss 	}
31166213b3cSManuel Lauss 
31266213b3cSManuel Lauss 	/* update PCMCIA configuration */
31366213b3cSManuel Lauss 	bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set);
31466213b3cSManuel Lauss 
31566213b3cSManuel Lauss 	sock->old_flags = state->flags;
31666213b3cSManuel Lauss 
31766213b3cSManuel Lauss 	/* reset was taken away: give card time to initialize properly */
31866213b3cSManuel Lauss 	if ((changed & SS_RESET) && !(state->flags & SS_RESET)) {
31966213b3cSManuel Lauss 		msleep(500);
32066213b3cSManuel Lauss 		set_stschg(sock, 1);
32166213b3cSManuel Lauss 	}
32266213b3cSManuel Lauss 
32366213b3cSManuel Lauss 	return ret;
32466213b3cSManuel Lauss }
32566213b3cSManuel Lauss 
32666213b3cSManuel Lauss /* VCC bits at [3:2]/[11:10] */
32766213b3cSManuel Lauss #define GET_VCC(cr, socknr)		\
32866213b3cSManuel Lauss 	((((cr) >> 2) >> ((socknr) * 8)) & 3)
32966213b3cSManuel Lauss 
33066213b3cSManuel Lauss /* VS bits at [0:1]/[3:2] */
33166213b3cSManuel Lauss #define GET_VS(sr, socknr)		\
33266213b3cSManuel Lauss 	(((sr) >> (2 * (socknr))) & 3)
33366213b3cSManuel Lauss 
33466213b3cSManuel Lauss /* reset bits at [7]/[15] */
33566213b3cSManuel Lauss #define GET_RESET(cr, socknr)		\
33666213b3cSManuel Lauss 	((cr) & (1 << (7 + (8 * (socknr)))))
33766213b3cSManuel Lauss 
db1x_pcmcia_get_status(struct pcmcia_socket * skt,unsigned int * value)33866213b3cSManuel Lauss static int db1x_pcmcia_get_status(struct pcmcia_socket *skt,
33966213b3cSManuel Lauss 				  unsigned int *value)
34066213b3cSManuel Lauss {
34166213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
34266213b3cSManuel Lauss 	unsigned short cr, sr;
34366213b3cSManuel Lauss 	unsigned int status;
34466213b3cSManuel Lauss 
34566213b3cSManuel Lauss 	status = db1x_card_inserted(sock) ? SS_DETECT : 0;
34666213b3cSManuel Lauss 
34766213b3cSManuel Lauss 	cr = bcsr_read(BCSR_PCMCIA);
34866213b3cSManuel Lauss 	sr = bcsr_read(BCSR_STATUS);
34966213b3cSManuel Lauss 
35066213b3cSManuel Lauss 	/* PB1100/PB1500: voltage key bits are at [5:4] */
35166213b3cSManuel Lauss 	if (sock->board_type == BOARD_TYPE_PB1100)
35266213b3cSManuel Lauss 		sr >>= 4;
35366213b3cSManuel Lauss 
35466213b3cSManuel Lauss 	/* determine card type */
35566213b3cSManuel Lauss 	switch (GET_VS(sr, sock->nr)) {
35666213b3cSManuel Lauss 	case 0:
35766213b3cSManuel Lauss 	case 2:
35866213b3cSManuel Lauss 		status |= SS_3VCARD;	/* 3V card */
359*02900f42SGustavo A. R. Silva 		break;
36066213b3cSManuel Lauss 	case 3:
36166213b3cSManuel Lauss 		break;			/* 5V card: set nothing */
36266213b3cSManuel Lauss 	default:
36366213b3cSManuel Lauss 		status |= SS_XVCARD;	/* treated as unsupported in core */
36466213b3cSManuel Lauss 	}
36566213b3cSManuel Lauss 
36666213b3cSManuel Lauss 	/* if Vcc is not zero, we have applied power to a card */
36766213b3cSManuel Lauss 	status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0;
36866213b3cSManuel Lauss 
36964cd04d0SManuel Lauss 	/* DB1300: power always on, but don't tell when no card present */
37064cd04d0SManuel Lauss 	if ((sock->board_type == BOARD_TYPE_DB1300) && (status & SS_DETECT))
37164cd04d0SManuel Lauss 		status = SS_POWERON | SS_3VCARD | SS_DETECT;
37264cd04d0SManuel Lauss 
37366213b3cSManuel Lauss 	/* reset de-asserted? then we're ready */
37466213b3cSManuel Lauss 	status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET;
37566213b3cSManuel Lauss 
37666213b3cSManuel Lauss 	*value = status;
37766213b3cSManuel Lauss 
37866213b3cSManuel Lauss 	return 0;
37966213b3cSManuel Lauss }
38066213b3cSManuel Lauss 
db1x_pcmcia_sock_init(struct pcmcia_socket * skt)38166213b3cSManuel Lauss static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
38266213b3cSManuel Lauss {
38366213b3cSManuel Lauss 	return 0;
38466213b3cSManuel Lauss }
38566213b3cSManuel Lauss 
db1x_pcmcia_sock_suspend(struct pcmcia_socket * skt)38666213b3cSManuel Lauss static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
38766213b3cSManuel Lauss {
38866213b3cSManuel Lauss 	return 0;
38966213b3cSManuel Lauss }
39066213b3cSManuel Lauss 
au1x00_pcmcia_set_io_map(struct pcmcia_socket * skt,struct pccard_io_map * map)39166213b3cSManuel Lauss static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
39266213b3cSManuel Lauss 				    struct pccard_io_map *map)
39366213b3cSManuel Lauss {
39466213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
39566213b3cSManuel Lauss 
39666213b3cSManuel Lauss 	map->start = (u32)sock->virt_io;
39766213b3cSManuel Lauss 	map->stop = map->start + IO_MAP_SIZE;
39866213b3cSManuel Lauss 
39966213b3cSManuel Lauss 	return 0;
40066213b3cSManuel Lauss }
40166213b3cSManuel Lauss 
au1x00_pcmcia_set_mem_map(struct pcmcia_socket * skt,struct pccard_mem_map * map)40266213b3cSManuel Lauss static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
40366213b3cSManuel Lauss 				     struct pccard_mem_map *map)
40466213b3cSManuel Lauss {
40566213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
40666213b3cSManuel Lauss 
40766213b3cSManuel Lauss 	if (map->flags & MAP_ATTRIB)
40866213b3cSManuel Lauss 		map->static_start = sock->phys_attr + map->card_start;
40966213b3cSManuel Lauss 	else
41066213b3cSManuel Lauss 		map->static_start = sock->phys_mem + map->card_start;
41166213b3cSManuel Lauss 
41266213b3cSManuel Lauss 	return 0;
41366213b3cSManuel Lauss }
41466213b3cSManuel Lauss 
41566213b3cSManuel Lauss static struct pccard_operations db1x_pcmcia_operations = {
41666213b3cSManuel Lauss 	.init			= db1x_pcmcia_sock_init,
41766213b3cSManuel Lauss 	.suspend		= db1x_pcmcia_sock_suspend,
41866213b3cSManuel Lauss 	.get_status		= db1x_pcmcia_get_status,
41966213b3cSManuel Lauss 	.set_socket		= db1x_pcmcia_configure,
42066213b3cSManuel Lauss 	.set_io_map		= au1x00_pcmcia_set_io_map,
42166213b3cSManuel Lauss 	.set_mem_map		= au1x00_pcmcia_set_mem_map,
42266213b3cSManuel Lauss };
42366213b3cSManuel Lauss 
db1x_pcmcia_socket_probe(struct platform_device * pdev)42434cdf25aSBill Pemberton static int db1x_pcmcia_socket_probe(struct platform_device *pdev)
42566213b3cSManuel Lauss {
42666213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock;
42766213b3cSManuel Lauss 	struct resource *r;
42866213b3cSManuel Lauss 	int ret, bid;
42966213b3cSManuel Lauss 
43066213b3cSManuel Lauss 	sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
43166213b3cSManuel Lauss 	if (!sock)
43266213b3cSManuel Lauss 		return -ENOMEM;
43366213b3cSManuel Lauss 
43466213b3cSManuel Lauss 	sock->nr = pdev->id;
43566213b3cSManuel Lauss 
43666213b3cSManuel Lauss 	bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
43766213b3cSManuel Lauss 	switch (bid) {
43866213b3cSManuel Lauss 	case BCSR_WHOAMI_PB1500:
43966213b3cSManuel Lauss 	case BCSR_WHOAMI_PB1500R2:
44066213b3cSManuel Lauss 	case BCSR_WHOAMI_PB1100:
44166213b3cSManuel Lauss 		sock->board_type = BOARD_TYPE_PB1100;
44266213b3cSManuel Lauss 		break;
44366213b3cSManuel Lauss 	case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
44466213b3cSManuel Lauss 		sock->board_type = BOARD_TYPE_DEFAULT;
44566213b3cSManuel Lauss 		break;
44666213b3cSManuel Lauss 	case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
44766213b3cSManuel Lauss 		sock->board_type = BOARD_TYPE_DB1200;
44866213b3cSManuel Lauss 		break;
44964cd04d0SManuel Lauss 	case BCSR_WHOAMI_DB1300:
45064cd04d0SManuel Lauss 		sock->board_type = BOARD_TYPE_DB1300;
45164cd04d0SManuel Lauss 		break;
45266213b3cSManuel Lauss 	default:
45366213b3cSManuel Lauss 		printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
45466213b3cSManuel Lauss 		ret = -ENODEV;
45566213b3cSManuel Lauss 		goto out0;
456370eb01fSJason Yan 	}
45766213b3cSManuel Lauss 
45866213b3cSManuel Lauss 	/*
45966213b3cSManuel Lauss 	 * gather resources necessary and optional nice-to-haves to
46066213b3cSManuel Lauss 	 * operate a socket:
46166213b3cSManuel Lauss 	 * This includes IRQs for Carddetection/ejection, the card
46266213b3cSManuel Lauss 	 *  itself and optional status change detection.
46366213b3cSManuel Lauss 	 * Also, the memory areas covered by a socket.  For these
464f25e188cSManuel Lauss 	 *  we require the real 36bit addresses (see the au1000.h
46566213b3cSManuel Lauss 	 *  header for more information).
46666213b3cSManuel Lauss 	 */
46766213b3cSManuel Lauss 
46866213b3cSManuel Lauss 	/* card: irq assigned to the card itself. */
46966213b3cSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "card");
47066213b3cSManuel Lauss 	sock->card_irq = r ? r->start : 0;
47166213b3cSManuel Lauss 
472e34b6fcfSManuel Lauss 	/* insert: irq which triggers on card insertion/ejection
473e34b6fcfSManuel Lauss 	 * BIG FAT NOTE: on DB1000/1100/1500/1550 we pass a GPIO here!
474e34b6fcfSManuel Lauss 	 */
47566213b3cSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "insert");
47666213b3cSManuel Lauss 	sock->insert_irq = r ? r->start : -1;
477e34b6fcfSManuel Lauss 	if (sock->board_type == BOARD_TYPE_DEFAULT) {
478e34b6fcfSManuel Lauss 		sock->insert_gpio = r ? r->start : -1;
479e34b6fcfSManuel Lauss 		sock->insert_irq = r ? gpio_to_irq(r->start) : -1;
480e34b6fcfSManuel Lauss 	}
48166213b3cSManuel Lauss 
48266213b3cSManuel Lauss 	/* stschg: irq which trigger on card status change (optional) */
48366213b3cSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "stschg");
48466213b3cSManuel Lauss 	sock->stschg_irq = r ? r->start : -1;
48566213b3cSManuel Lauss 
48666213b3cSManuel Lauss 	/* eject: irq which triggers on ejection (DB1200/PB1200 only) */
48766213b3cSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "eject");
48866213b3cSManuel Lauss 	sock->eject_irq = r ? r->start : -1;
48966213b3cSManuel Lauss 
49066213b3cSManuel Lauss 	ret = -ENODEV;
49166213b3cSManuel Lauss 
492f25e188cSManuel Lauss 	/* 36bit PCMCIA Attribute area address */
49311b897cfSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
49466213b3cSManuel Lauss 	if (!r) {
49566213b3cSManuel Lauss 		printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
49666213b3cSManuel Lauss 			sock->nr);
49766213b3cSManuel Lauss 		goto out0;
49866213b3cSManuel Lauss 	}
49966213b3cSManuel Lauss 	sock->phys_attr = r->start;
50066213b3cSManuel Lauss 
501f25e188cSManuel Lauss 	/* 36bit PCMCIA Memory area address */
50211b897cfSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
50366213b3cSManuel Lauss 	if (!r) {
50466213b3cSManuel Lauss 		printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
50566213b3cSManuel Lauss 			sock->nr);
50666213b3cSManuel Lauss 		goto out0;
50766213b3cSManuel Lauss 	}
50866213b3cSManuel Lauss 	sock->phys_mem = r->start;
50966213b3cSManuel Lauss 
510f25e188cSManuel Lauss 	/* 36bit PCMCIA IO area address */
51111b897cfSManuel Lauss 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
51266213b3cSManuel Lauss 	if (!r) {
51366213b3cSManuel Lauss 		printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
51466213b3cSManuel Lauss 			sock->nr);
51566213b3cSManuel Lauss 		goto out0;
51666213b3cSManuel Lauss 	}
51766213b3cSManuel Lauss 	sock->phys_io = r->start;
51866213b3cSManuel Lauss 
51966213b3cSManuel Lauss 	/*
52066213b3cSManuel Lauss 	 * PCMCIA client drivers use the inb/outb macros to access
52166213b3cSManuel Lauss 	 * the IO registers.  Since mips_io_port_base is added
52266213b3cSManuel Lauss 	 * to the access address of the mips implementation of
52366213b3cSManuel Lauss 	 * inb/outb, we need to subtract it here because we want
52466213b3cSManuel Lauss 	 * to access the I/O or MEM address directly, without
52566213b3cSManuel Lauss 	 * going through this "mips_io_port_base" mechanism.
52666213b3cSManuel Lauss 	 */
52711b897cfSManuel Lauss 	sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) -
52866213b3cSManuel Lauss 				 mips_io_port_base);
52966213b3cSManuel Lauss 
53066213b3cSManuel Lauss 	if (!sock->virt_io) {
53166213b3cSManuel Lauss 		printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
53266213b3cSManuel Lauss 			sock->nr);
53366213b3cSManuel Lauss 		ret = -ENOMEM;
53466213b3cSManuel Lauss 		goto out0;
53566213b3cSManuel Lauss 	}
53666213b3cSManuel Lauss 
53766213b3cSManuel Lauss 	sock->socket.ops	= &db1x_pcmcia_operations;
53866213b3cSManuel Lauss 	sock->socket.owner	= THIS_MODULE;
53966213b3cSManuel Lauss 	sock->socket.pci_irq	= sock->card_irq;
54066213b3cSManuel Lauss 	sock->socket.features	= SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
54166213b3cSManuel Lauss 	sock->socket.map_size	= MEM_MAP_SIZE;
54266213b3cSManuel Lauss 	sock->socket.io_offset	= (unsigned long)sock->virt_io;
54366213b3cSManuel Lauss 	sock->socket.dev.parent	= &pdev->dev;
54466213b3cSManuel Lauss 	sock->socket.resource_ops = &pccard_static_ops;
54566213b3cSManuel Lauss 
54666213b3cSManuel Lauss 	platform_set_drvdata(pdev, sock);
54766213b3cSManuel Lauss 
54866213b3cSManuel Lauss 	ret = db1x_pcmcia_setup_irqs(sock);
54966213b3cSManuel Lauss 	if (ret) {
55066213b3cSManuel Lauss 		printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
55166213b3cSManuel Lauss 			sock->nr);
55266213b3cSManuel Lauss 		goto out1;
55366213b3cSManuel Lauss 	}
55466213b3cSManuel Lauss 
55566213b3cSManuel Lauss 	set_stschg(sock, 0);
55666213b3cSManuel Lauss 
55766213b3cSManuel Lauss 	ret = pcmcia_register_socket(&sock->socket);
55866213b3cSManuel Lauss 	if (ret) {
55966213b3cSManuel Lauss 		printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
56066213b3cSManuel Lauss 		goto out2;
56166213b3cSManuel Lauss 	}
56266213b3cSManuel Lauss 
56311b897cfSManuel Lauss 	printk(KERN_INFO "Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %09llx"
56411b897cfSManuel Lauss 		"(%p) %09llx %09llx  card/insert/stschg/eject irqs @ %d "
56566213b3cSManuel Lauss 		"%d %d %d\n", sock->nr, sock->phys_io, sock->virt_io,
56666213b3cSManuel Lauss 		sock->phys_attr, sock->phys_mem, sock->card_irq,
56766213b3cSManuel Lauss 		sock->insert_irq, sock->stschg_irq, sock->eject_irq);
56866213b3cSManuel Lauss 
56966213b3cSManuel Lauss 	return 0;
57066213b3cSManuel Lauss 
57166213b3cSManuel Lauss out2:
57266213b3cSManuel Lauss 	db1x_pcmcia_free_irqs(sock);
57366213b3cSManuel Lauss out1:
57466213b3cSManuel Lauss 	iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
57566213b3cSManuel Lauss out0:
57666213b3cSManuel Lauss 	kfree(sock);
57766213b3cSManuel Lauss 	return ret;
57866213b3cSManuel Lauss }
57966213b3cSManuel Lauss 
db1x_pcmcia_socket_remove(struct platform_device * pdev)580e765a02cSBill Pemberton static int db1x_pcmcia_socket_remove(struct platform_device *pdev)
58166213b3cSManuel Lauss {
58266213b3cSManuel Lauss 	struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev);
58366213b3cSManuel Lauss 
58466213b3cSManuel Lauss 	db1x_pcmcia_free_irqs(sock);
58566213b3cSManuel Lauss 	pcmcia_unregister_socket(&sock->socket);
58666213b3cSManuel Lauss 	iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
58766213b3cSManuel Lauss 	kfree(sock);
58866213b3cSManuel Lauss 
58966213b3cSManuel Lauss 	return 0;
59066213b3cSManuel Lauss }
59166213b3cSManuel Lauss 
59266213b3cSManuel Lauss static struct platform_driver db1x_pcmcia_socket_driver = {
59366213b3cSManuel Lauss 	.driver	= {
59466213b3cSManuel Lauss 		.name	= "db1xxx_pcmcia",
59566213b3cSManuel Lauss 	},
59666213b3cSManuel Lauss 	.probe		= db1x_pcmcia_socket_probe,
59796364e3aSBill Pemberton 	.remove		= db1x_pcmcia_socket_remove,
59866213b3cSManuel Lauss };
59966213b3cSManuel Lauss 
6005d95f8e2SAxel Lin module_platform_driver(db1x_pcmcia_socket_driver);
60166213b3cSManuel Lauss 
60266213b3cSManuel Lauss MODULE_LICENSE("GPL");
60366213b3cSManuel Lauss MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
60466213b3cSManuel Lauss MODULE_AUTHOR("Manuel Lauss");
605