166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/*
266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards.
366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *
466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *
666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss */
766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* This is a fairly generic PCMCIA socket driver suitable for the
966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * following Alchemy Development boards:
1064cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss *  Db1000, Db/Pb1500, Db/Pb1100, Db/Pb1550, Db/Pb1200, Db1300
1166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *
1266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * The Db1000 is used as a reference:  Per-socket card-, carddetect- and
1366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *  statuschange IRQs connected to SoC GPIOs, control and status register
1466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *  bits arranged in per-socket groups in an external PLD.  All boards
1566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *  listed here use this layout, including bit positions and meanings.
1666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *  Of course there are exceptions in later boards:
1766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *
1866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	- Pb1100/Pb1500:  single socket only; voltage key bits VS are
1966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *			  at STATUS[5:4] (instead of STATUS[1:0]).
2066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	- Au1200-based:	  additional card-eject irqs, irqs not gpios!
2164cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss *	- Db1300:	  Db1200-like, no pwr ctrl, single socket (#1).
2266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss */
2366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
2466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/delay.h>
2566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/gpio.h>
2666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/interrupt.h>
2766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/pm.h>
283bf8d64d394dc26b17abde97a541229a2a146ad2Paul Gortmaker#include <linux/module.h>
2966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/platform_device.h>
3066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/resource.h>
315a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
3266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <linux/spinlock.h>
3366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
3466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <pcmcia/ss.h>
3566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
3666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <asm/mach-au1x00/au1000.h>
3766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#include <asm/mach-db1x00/bcsr.h>
3866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
3966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define MEM_MAP_SIZE	0x400000
4066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define IO_MAP_SIZE	0x1000
4166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
4266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstruct db1x_pcmcia_sock {
4366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct pcmcia_socket	socket;
4466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int		nr;		/* socket number */
4566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	void		*virt_io;
4666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
4711b897cf84c37e6522db914793677e933ef311fbManuel Lauss	phys_addr_t	phys_io;
4811b897cf84c37e6522db914793677e933ef311fbManuel Lauss	phys_addr_t	phys_attr;
4911b897cf84c37e6522db914793677e933ef311fbManuel Lauss	phys_addr_t	phys_mem;
5066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
5166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* previous flags for set_socket() */
5266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	unsigned int old_flags;
5366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
5466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* interrupt sources: linux irq numbers! */
5566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int	insert_irq;	/* default carddetect irq */
5666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int	stschg_irq;	/* card-status-change irq */
5766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int	card_irq;	/* card irq */
5866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int	eject_irq;	/* db1200/pb1200 have these */
5966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
6066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define BOARD_TYPE_DEFAULT	0	/* most boards */
6166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define BOARD_TYPE_DB1200	1	/* IRQs aren't gpios */
6266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define BOARD_TYPE_PB1100	2	/* VS bits slightly different */
6364cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss#define BOARD_TYPE_DB1300	3	/* no power control */
6466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int	board_type;
6566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss};
6666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
6766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket)
6866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
6964cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Laussstatic int db1300_card_inserted(struct db1x_pcmcia_sock *sock)
7064cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss{
7164cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	return bcsr_read(BCSR_SIGSTAT) & (1 << 8);
7264cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss}
7364cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss
7466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */
7566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1200_card_inserted(struct db1x_pcmcia_sock *sock)
7666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
7766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	unsigned short sigstat;
7866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
7966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sigstat = bcsr_read(BCSR_SIGSTAT);
8066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return sigstat & 1 << (8 + 2 * sock->nr);
8166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
8266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
8366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* carddetect gpio: low-active */
8466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1000_card_inserted(struct db1x_pcmcia_sock *sock)
8566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
8666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return !gpio_get_value(irq_to_gpio(sock->insert_irq));
8766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
8866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
8966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1x_card_inserted(struct db1x_pcmcia_sock *sock)
9066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
9166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	switch (sock->board_type) {
9266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case BOARD_TYPE_DB1200:
9366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		return db1200_card_inserted(sock);
9464cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	case BOARD_TYPE_DB1300:
9564cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss		return db1300_card_inserted(sock);
9666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	default:
9766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		return db1000_card_inserted(sock);
9866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
9966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
10066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
10166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* STSCHG tends to bounce heavily when cards are inserted/ejected.
10266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * To avoid this, the interrupt is normally disabled and only enabled
10366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * after reset to a card has been de-asserted.
10466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss */
10566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic inline void set_stschg(struct db1x_pcmcia_sock *sock, int en)
10666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
10766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (sock->stschg_irq != -1) {
10866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		if (en)
10966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			enable_irq(sock->stschg_irq);
11066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		else
11166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			disable_irq(sock->stschg_irq);
11266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
11366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
11466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
11566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic irqreturn_t db1000_pcmcia_cdirq(int irq, void *data)
11666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
11766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = data;
11866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
11966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	pcmcia_parse_events(&sock->socket, SS_DETECT);
12066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
12166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return IRQ_HANDLED;
12266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
12366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
12466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic irqreturn_t db1000_pcmcia_stschgirq(int irq, void *data)
12566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
12666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = data;
12766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
12866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	pcmcia_parse_events(&sock->socket, SS_STSCHG);
12966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
13066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return IRQ_HANDLED;
13166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
13266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
13366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic irqreturn_t db1200_pcmcia_cdirq(int irq, void *data)
13466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
13566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = data;
13666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
13766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* Db/Pb1200 have separate per-socket insertion and ejection
13866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * interrupts which stay asserted as long as the card is
13966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * inserted/missing.  The one which caused us to be called
14066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * needs to be disabled and the other one enabled.
14166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 */
14266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (irq == sock->insert_irq) {
14366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		disable_irq_nosync(sock->insert_irq);
14466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		enable_irq(sock->eject_irq);
14566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	} else {
14666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		disable_irq_nosync(sock->eject_irq);
14766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		enable_irq(sock->insert_irq);
14866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
14966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
15066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	pcmcia_parse_events(&sock->socket, SS_DETECT);
15166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
15266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return IRQ_HANDLED;
15366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
15466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
15566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
15666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
15766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int ret;
15866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
15966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (sock->stschg_irq != -1) {
16066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
16166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				  0, "pcmcia_stschg", sock);
16266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		if (ret)
16366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			return ret;
16466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
16566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
16666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* Db/Pb1200 have separate per-socket insertion and ejection
16766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * interrupts, which should show edge behaviour but don't.
16866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * So interrupts are disabled until both insertion and
16966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * ejection handler have been registered and the currently
17066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * active one disabled.
17166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 */
17264cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	if ((sock->board_type == BOARD_TYPE_DB1200) ||
17364cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	    (sock->board_type == BOARD_TYPE_DB1300)) {
17466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = request_irq(sock->insert_irq, db1200_pcmcia_cdirq,
17566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				  IRQF_DISABLED, "pcmcia_insert", sock);
1760000a5390184af1459e82506fcfa7db96f3e6d33Manuel Lauss		if (ret)
17766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			goto out1;
17866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
17966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = request_irq(sock->eject_irq, db1200_pcmcia_cdirq,
18066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				  IRQF_DISABLED, "pcmcia_eject", sock);
18166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		if (ret) {
18266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			free_irq(sock->insert_irq, sock);
18366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			goto out1;
18466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		}
18566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
1860000a5390184af1459e82506fcfa7db96f3e6d33Manuel Lauss		/* enable the currently silent one */
18764cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss		if (db1x_card_inserted(sock))
1880000a5390184af1459e82506fcfa7db96f3e6d33Manuel Lauss			enable_irq(sock->eject_irq);
18966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		else
1900000a5390184af1459e82506fcfa7db96f3e6d33Manuel Lauss			enable_irq(sock->insert_irq);
19166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	} else {
19266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		/* all other (older) Db1x00 boards use a GPIO to show
19366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		 * card detection status:  use both-edge triggers.
19466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		 */
195dced35aeb0367dda2636ee9ee914bda14510dcc9Thomas Gleixner		irq_set_irq_type(sock->insert_irq, IRQ_TYPE_EDGE_BOTH);
19666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = request_irq(sock->insert_irq, db1000_pcmcia_cdirq,
19766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				  0, "pcmcia_carddetect", sock);
19866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
19966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		if (ret)
20066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			goto out1;
20166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
20266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
20366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;	/* all done */
20466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
20566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussout1:
20666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (sock->stschg_irq != -1)
20766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		free_irq(sock->stschg_irq, sock);
20866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
20966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return ret;
21066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
21166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
21266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock)
21366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
21466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (sock->stschg_irq != -1)
21566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		free_irq(sock->stschg_irq, sock);
21666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
21766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	free_irq(sock->insert_irq, sock);
21866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (sock->eject_irq != -1)
21966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		free_irq(sock->eject_irq, sock);
22066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
22166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
22266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/*
22366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * configure a PCMCIA socket on the Db1x00 series of boards (and
22466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * compatibles).
22566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *
22666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss * 2 external registers are involved:
22766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *   pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id
22866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *   pcmcia_control(offset 0x10):
22966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	bits[0:1] set vcc for card
23066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	bits[2:3] set vpp for card
23166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	bit 4:	enable data buffers
23266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	bit 7:	reset# for card
23366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss *	add 8 for second socket.
23466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss */
23566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1x_pcmcia_configure(struct pcmcia_socket *skt,
23666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				 struct socket_state_t *state)
23766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
23866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
23966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	unsigned short cr_clr, cr_set;
24066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	unsigned int changed;
24166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int v, p, ret;
24266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
24366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* card voltage setup */
24466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
24566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	cr_set = 0;
24666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	v = p = ret = 0;
24766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
24866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	switch (state->Vcc) {
24966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 50:
25066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		++v;
25166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 33:
25266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		++v;
25366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 0:
25466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		break;
25566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	default:
25666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
25766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr, state->Vcc);
25866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
25966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
26066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	switch (state->Vpp) {
26166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 12:
26266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		++p;
26366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 33:
26466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 50:
26566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		++p;
26666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 0:
26766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		break;
26866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	default:
26966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n",
27066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr, state->Vpp);
27166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
27266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
27366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* sanity check: Vpp must be 0, 12, or Vcc */
27466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (((state->Vcc == 33) && (state->Vpp == 50)) ||
27566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	    ((state->Vcc == 50) && (state->Vpp == 33))) {
27666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_INFO "pcmcia%d bad Vcc/Vpp combo (%d %d)\n",
27766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr, state->Vcc, state->Vpp);
27866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		v = p = 0;
27966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = -EINVAL;
28066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
28166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
28266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* create new voltage code */
28364cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	if (sock->board_type != BOARD_TYPE_DB1300)
28464cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss		cr_set |= ((v << 2) | p) << (sock->nr * 8);
28566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
28666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	changed = state->flags ^ sock->old_flags;
28766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
28866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (changed & SS_RESET) {
28966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		if (state->flags & SS_RESET) {
29066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			set_stschg(sock, 0);
29166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			/* assert reset, disable io buffers */
29266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			cr_clr |= (1 << (7 + (sock->nr * 8)));
29366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			cr_clr |= (1 << (4 + (sock->nr * 8)));
29466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		} else {
29566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			/* de-assert reset, enable io buffers */
29666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			cr_set |= 1 << (7 + (sock->nr * 8));
29766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			cr_set |= 1 << (4 + (sock->nr * 8));
29866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		}
29966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
30066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
30166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* update PCMCIA configuration */
30266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set);
30366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
30466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->old_flags = state->flags;
30566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
30666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* reset was taken away: give card time to initialize properly */
30766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if ((changed & SS_RESET) && !(state->flags & SS_RESET)) {
30866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		msleep(500);
30966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		set_stschg(sock, 1);
31066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
31166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
31266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return ret;
31366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
31466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
31566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* VCC bits at [3:2]/[11:10] */
31666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define GET_VCC(cr, socknr)		\
31766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	((((cr) >> 2) >> ((socknr) * 8)) & 3)
31866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
31966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* VS bits at [0:1]/[3:2] */
32066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define GET_VS(sr, socknr)		\
32166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	(((sr) >> (2 * (socknr))) & 3)
32266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
32366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss/* reset bits at [7]/[15] */
32466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss#define GET_RESET(cr, socknr)		\
32566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	((cr) & (1 << (7 + (8 * (socknr)))))
32666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
32766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1x_pcmcia_get_status(struct pcmcia_socket *skt,
32866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				  unsigned int *value)
32966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
33066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
33166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	unsigned short cr, sr;
33266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	unsigned int status;
33366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
33466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	status = db1x_card_inserted(sock) ? SS_DETECT : 0;
33566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
33666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	cr = bcsr_read(BCSR_PCMCIA);
33766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sr = bcsr_read(BCSR_STATUS);
33866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
33966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* PB1100/PB1500: voltage key bits are at [5:4] */
34066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (sock->board_type == BOARD_TYPE_PB1100)
34166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		sr >>= 4;
34266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
34366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* determine card type */
34466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	switch (GET_VS(sr, sock->nr)) {
34566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 0:
34666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 2:
34766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		status |= SS_3VCARD;	/* 3V card */
34866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case 3:
34966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		break;			/* 5V card: set nothing */
35066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	default:
35166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		status |= SS_XVCARD;	/* treated as unsupported in core */
35266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
35366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
35466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* if Vcc is not zero, we have applied power to a card */
35566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0;
35666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
35764cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	/* DB1300: power always on, but don't tell when no card present */
35864cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	if ((sock->board_type == BOARD_TYPE_DB1300) && (status & SS_DETECT))
35964cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss		status = SS_POWERON | SS_3VCARD | SS_DETECT;
36064cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss
36166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* reset de-asserted? then we're ready */
36266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET;
36366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
36466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	*value = status;
36566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
36666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
36766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
36866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
36966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
37066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
37166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
37266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
37366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
37466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
37566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
37666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
37766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
37866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
37966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
38066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				    struct pccard_io_map *map)
38166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
38266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
38366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
38466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	map->start = (u32)sock->virt_io;
38566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	map->stop = map->start + IO_MAP_SIZE;
38666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
38766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
38866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
38966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
39066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
39166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				     struct pccard_mem_map *map)
39266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
39366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
39466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
39566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (map->flags & MAP_ATTRIB)
39666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		map->static_start = sock->phys_attr + map->card_start;
39766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	else
39866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		map->static_start = sock->phys_mem + map->card_start;
39966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
40066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
40166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
40266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
40366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic struct pccard_operations db1x_pcmcia_operations = {
40466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.init			= db1x_pcmcia_sock_init,
40566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.suspend		= db1x_pcmcia_sock_suspend,
40666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.get_status		= db1x_pcmcia_get_status,
40766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.set_socket		= db1x_pcmcia_configure,
40866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.set_io_map		= au1x00_pcmcia_set_io_map,
40966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.set_mem_map		= au1x00_pcmcia_set_mem_map,
41066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss};
41166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
41266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int __devinit db1x_pcmcia_socket_probe(struct platform_device *pdev)
41366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
41466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock;
41566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct resource *r;
41666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	int ret, bid;
41766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
41866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
41966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (!sock)
42066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		return -ENOMEM;
42166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
42266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->nr = pdev->id;
42366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
42466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
42566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	switch (bid) {
42666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case BCSR_WHOAMI_PB1500:
42766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case BCSR_WHOAMI_PB1500R2:
42866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case BCSR_WHOAMI_PB1100:
42966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		sock->board_type = BOARD_TYPE_PB1100;
43066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		break;
43166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
43266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		sock->board_type = BOARD_TYPE_DEFAULT;
43366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		break;
43466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
43566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		sock->board_type = BOARD_TYPE_DB1200;
43666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		break;
43764cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss	case BCSR_WHOAMI_DB1300:
43864cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss		sock->board_type = BOARD_TYPE_DB1300;
43964cd04d0cffa3b3af0e81aa3112b71f135739e1aManuel Lauss		break;
44066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	default:
44166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
44266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = -ENODEV;
44366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out0;
44466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	};
44566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
44666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/*
44766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * gather resources necessary and optional nice-to-haves to
44866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * operate a socket:
44966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * This includes IRQs for Carddetection/ejection, the card
45066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 *  itself and optional status change detection.
45166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * Also, the memory areas covered by a socket.  For these
452f25e188c892a9a82f8c3babf6fda304fff8cb3ccManuel Lauss	 *  we require the real 36bit addresses (see the au1000.h
45366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 *  header for more information).
45466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 */
45566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
45666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* card: irq assigned to the card itself. */
45766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "card");
45866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->card_irq = r ? r->start : 0;
45966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
46066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* insert: irq which triggers on card insertion/ejection */
46166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "insert");
46266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->insert_irq = r ? r->start : -1;
46366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
46466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* stschg: irq which trigger on card status change (optional) */
46566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "stschg");
46666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->stschg_irq = r ? r->start : -1;
46766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
46866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/* eject: irq which triggers on ejection (DB1200/PB1200 only) */
46966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "eject");
47066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->eject_irq = r ? r->start : -1;
47166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
47266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	ret = -ENODEV;
47366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
474f25e188c892a9a82f8c3babf6fda304fff8cb3ccManuel Lauss	/* 36bit PCMCIA Attribute area address */
47511b897cf84c37e6522db914793677e933ef311fbManuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
47666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (!r) {
47766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
47866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr);
47966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out0;
48066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
48166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->phys_attr = r->start;
48266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
483f25e188c892a9a82f8c3babf6fda304fff8cb3ccManuel Lauss	/* 36bit PCMCIA Memory area address */
48411b897cf84c37e6522db914793677e933ef311fbManuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
48566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (!r) {
48666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
48766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr);
48866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out0;
48966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
49066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->phys_mem = r->start;
49166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
492f25e188c892a9a82f8c3babf6fda304fff8cb3ccManuel Lauss	/* 36bit PCMCIA IO area address */
49311b897cf84c37e6522db914793677e933ef311fbManuel Lauss	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
49466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (!r) {
49566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
49666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr);
49766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out0;
49866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
49966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->phys_io = r->start;
50066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
50166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	/*
50266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * PCMCIA client drivers use the inb/outb macros to access
50366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * the IO registers.  Since mips_io_port_base is added
50466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * to the access address of the mips implementation of
50566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * inb/outb, we need to subtract it here because we want
50666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * to access the I/O or MEM address directly, without
50766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 * going through this "mips_io_port_base" mechanism.
50866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	 */
50911b897cf84c37e6522db914793677e933ef311fbManuel Lauss	sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) -
51066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss				 mips_io_port_base);
51166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
51266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (!sock->virt_io) {
51366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
51466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr);
51566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		ret = -ENOMEM;
51666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out0;
51766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
51866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
51966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.ops	= &db1x_pcmcia_operations;
52066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.owner	= THIS_MODULE;
52166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.pci_irq	= sock->card_irq;
52266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.features	= SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
52366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.map_size	= MEM_MAP_SIZE;
52466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.io_offset	= (unsigned long)sock->virt_io;
52566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.dev.parent	= &pdev->dev;
52666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	sock->socket.resource_ops = &pccard_static_ops;
52766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
52866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	platform_set_drvdata(pdev, sock);
52966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
53066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	ret = db1x_pcmcia_setup_irqs(sock);
53166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (ret) {
53266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
53366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss			sock->nr);
53466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out1;
53566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
53666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
53766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	set_stschg(sock, 0);
53866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
53966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	ret = pcmcia_register_socket(&sock->socket);
54066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	if (ret) {
54166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
54266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		goto out2;
54366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	}
54466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
54511b897cf84c37e6522db914793677e933ef311fbManuel Lauss	printk(KERN_INFO "Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %09llx"
54611b897cf84c37e6522db914793677e933ef311fbManuel Lauss		"(%p) %09llx %09llx  card/insert/stschg/eject irqs @ %d "
54766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		"%d %d %d\n", sock->nr, sock->phys_io, sock->virt_io,
54866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		sock->phys_attr, sock->phys_mem, sock->card_irq,
54966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		sock->insert_irq, sock->stschg_irq, sock->eject_irq);
55066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
55166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
55266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
55366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussout2:
55466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	db1x_pcmcia_free_irqs(sock);
55566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussout1:
55666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
55766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussout0:
55866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	kfree(sock);
55966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return ret;
56066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
56166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
56266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic int __devexit db1x_pcmcia_socket_remove(struct platform_device *pdev)
56366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
56466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev);
56566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
56666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	db1x_pcmcia_free_irqs(sock);
56766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	pcmcia_unregister_socket(&sock->socket);
56866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
56966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	kfree(sock);
57066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
57166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return 0;
57266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
57366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
57466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussstatic struct platform_driver db1x_pcmcia_socket_driver = {
57566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.driver	= {
57666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		.name	= "db1xxx_pcmcia",
57766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss		.owner	= THIS_MODULE,
57866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	},
57966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.probe		= db1x_pcmcia_socket_probe,
58066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	.remove		= __devexit_p(db1x_pcmcia_socket_remove),
58166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss};
58266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
58366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussint __init db1x_pcmcia_socket_load(void)
58466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
58566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	return platform_driver_register(&db1x_pcmcia_socket_driver);
58666213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
58766213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
58866213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussvoid  __exit db1x_pcmcia_socket_unload(void)
58966213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss{
59066213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss	platform_driver_unregister(&db1x_pcmcia_socket_driver);
59166213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss}
59266213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
59366213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussmodule_init(db1x_pcmcia_socket_load);
59466213b3ccfc770704025ce9465fa3aaedde21b55Manuel Laussmodule_exit(db1x_pcmcia_socket_unload);
59566213b3ccfc770704025ce9465fa3aaedde21b55Manuel Lauss
59666213b3ccfc770704025ce9465fa3aaedde21b55Manuel LaussMODULE_LICENSE("GPL");
59766213b3ccfc770704025ce9465fa3aaedde21b55Manuel LaussMODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
59866213b3ccfc770704025ce9465fa3aaedde21b55Manuel LaussMODULE_AUTHOR("Manuel Lauss");
599