at91_cf.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * at91_cf.c -- AT91 CompactFlash controller driver
 *
 * Copyright (C) 2005 David Brownell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>

#include <pcmcia/ss.h>

21
#include <mach/hardware.h>
22 23
#include <asm/io.h>
#include <asm/sizes.h>
D
David Brownell 已提交
24
#include <asm/gpio.h>
25

26 27
#include <mach/board.h>
#include <mach/at91rm9200_mc.h>
28 29 30 31 32 33


/*
 * A0..A10 work in each range; A23 indicates I/O space;  A25 is CFRNW;
 * some other bit in {A24,A22..A11} is nREG to flag memory access
 * (vs attributes).  So more than 2KB/region would just be waste.
A
Andrew Victor 已提交
34
 * Note: These are offsets from the physical base address.
35
 */
A
Andrew Victor 已提交
36 37 38
#define	CF_ATTR_PHYS	(0)
#define	CF_IO_PHYS	(1 << 23)
#define	CF_MEM_PHYS	(0x017ff800)
39 40 41 42 43 44 45 46 47 48 49 50

/*--------------------------------------------------------------------------*/

static const char driver_name[] = "at91_cf";

struct at91_cf_socket {
	struct pcmcia_socket	socket;

	unsigned		present:1;

	struct platform_device	*pdev;
	struct at91_cf_data	*board;
A
Andrew Victor 已提交
51 52

	unsigned long		phys_baseaddr;
53 54 55 56 57 58
};

#define	SZ_2K			(2 * SZ_1K)

static inline int at91_cf_present(struct at91_cf_socket *cf)
{
D
David Brownell 已提交
59
	return !gpio_get_value(cf->board->det_pin);
60 61 62 63 64 65 66 67 68
}

/*--------------------------------------------------------------------------*/

static int at91_cf_ss_init(struct pcmcia_socket *s)
{
	return 0;
}

69
static irqreturn_t at91_cf_irq(int irq, void *_cf)
70
{
71
	struct at91_cf_socket *cf = _cf;
72 73 74 75 76 77 78

	if (irq == cf->board->det_pin) {
		unsigned present = at91_cf_present(cf);

		/* kick pccard as needed */
		if (present != cf->present) {
			cf->present = present;
D
David Brownell 已提交
79 80
			pr_debug("%s: card %s\n", driver_name,
					present ? "present" : "gone");
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			pcmcia_parse_events(&cf->socket, SS_DETECT);
		}
	}

	return IRQ_HANDLED;
}

static int at91_cf_get_status(struct pcmcia_socket *s, u_int *sp)
{
	struct at91_cf_socket	*cf;

	if (!sp)
		return -EINVAL;

	cf = container_of(s, struct at91_cf_socket, socket);

D
David Brownell 已提交
97
	/* NOTE: CF is always 3VCARD */
98 99 100 101 102
	if (at91_cf_present(cf)) {
		int rdy	= cf->board->irq_pin;	/* RDY/nIRQ */
		int vcc	= cf->board->vcc_pin;

		*sp = SS_DETECT | SS_3VCARD;
D
David Brownell 已提交
103
		if (!rdy || gpio_get_value(rdy))
104
			*sp |= SS_READY;
D
David Brownell 已提交
105
		if (!vcc || gpio_get_value(vcc))
106 107 108 109 110 111 112
			*sp |= SS_POWERON;
	} else
		*sp = 0;

	return 0;
}

D
David Brownell 已提交
113 114
static int
at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
115 116 117 118 119 120 121 122 123
{
	struct at91_cf_socket	*cf;

	cf = container_of(sock, struct at91_cf_socket, socket);

	/* switch Vcc if needed and possible */
	if (cf->board->vcc_pin) {
		switch (s->Vcc) {
			case 0:
D
David Brownell 已提交
124
				gpio_set_value(cf->board->vcc_pin, 0);
125 126
				break;
			case 33:
D
David Brownell 已提交
127
				gpio_set_value(cf->board->vcc_pin, 1);
128 129 130 131 132 133 134
				break;
			default:
				return -EINVAL;
		}
	}

	/* toggle reset if needed */
D
David Brownell 已提交
135
	gpio_set_value(cf->board->rst_pin, s->flags & SS_RESET);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
		driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);

	return 0;
}

static int at91_cf_ss_suspend(struct pcmcia_socket *s)
{
	return at91_cf_set_socket(s, &dead_socket);
}

/* we already mapped the I/O region */
static int at91_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
{
	struct at91_cf_socket	*cf;
	u32			csr;

	cf = container_of(s, struct at91_cf_socket, socket);
	io->flags &= (MAP_ACTIVE | MAP_16BIT | MAP_AUTOSZ);

	/*
	 * Use 16 bit accesses unless/until we need 8-bit i/o space.
	 */
A
Andrew Victor 已提交
160
	csr = at91_sys_read(AT91_SMC_CSR(cf->board->chipselect)) & ~AT91_SMC_DBW;
161 162 163 164 165 166 167 168 169 170 171

	/*
	 * NOTE: this CF controller ignores IOIS16, so we can't really do
	 * MAP_AUTOSZ.  The 16bit mode allows single byte access on either
	 * D0-D7 (even addr) or D8-D15 (odd), so it's close enough for many
	 * purposes (and handles ide-cs).
	 *
	 * The 8bit mode is needed for odd byte access on D0-D7.  It seems
	 * some cards only like that way to get at the odd byte, despite
	 * CF 3.0 spec table 35 also giving the D8-D15 option.
	 */
A
Andrew Victor 已提交
172
	if (!(io->flags & (MAP_16BIT | MAP_AUTOSZ))) {
173 174 175 176 177 178
		csr |= AT91_SMC_DBW_8;
		pr_debug("%s: 8bit i/o bus\n", driver_name);
	} else {
		csr |= AT91_SMC_DBW_16;
		pr_debug("%s: 16bit i/o bus\n", driver_name);
	}
A
Andrew Victor 已提交
179
	at91_sys_write(AT91_SMC_CSR(cf->board->chipselect), csr);
180 181 182 183 184 185 186 187

	io->start = cf->socket.io_offset;
	io->stop = io->start + SZ_2K - 1;

	return 0;
}

/* pcmcia layer maps/unmaps mem regions */
D
David Brownell 已提交
188 189
static int
at91_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
190 191 192 193 194 195 196 197
{
	struct at91_cf_socket	*cf;

	if (map->card_start)
		return -EINVAL;

	cf = container_of(s, struct at91_cf_socket, socket);

A
Andrew Victor 已提交
198
	map->flags &= (MAP_ACTIVE | MAP_ATTRIB | MAP_16BIT);
199
	if (map->flags & MAP_ATTRIB)
A
Andrew Victor 已提交
200
		map->static_start = cf->phys_baseaddr + CF_ATTR_PHYS;
201
	else
A
Andrew Victor 已提交
202
		map->static_start = cf->phys_baseaddr + CF_MEM_PHYS;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

	return 0;
}

static struct pccard_operations at91_cf_ops = {
	.init			= at91_cf_ss_init,
	.suspend		= at91_cf_ss_suspend,
	.get_status		= at91_cf_get_status,
	.set_socket		= at91_cf_set_socket,
	.set_io_map		= at91_cf_set_io_map,
	.set_mem_map		= at91_cf_set_mem_map,
};

/*--------------------------------------------------------------------------*/

218
static int __init at91_cf_probe(struct platform_device *pdev)
219 220
{
	struct at91_cf_socket	*cf;
221
	struct at91_cf_data	*board = pdev->dev.platform_data;
D
David Brownell 已提交
222
	struct resource		*io;
223 224 225 226 227
	int			status;

	if (!board || !board->det_pin || !board->rst_pin)
		return -ENODEV;

D
David Brownell 已提交
228 229 230 231
	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!io)
		return -ENODEV;

232
	cf = kzalloc(sizeof *cf, GFP_KERNEL);
233 234 235 236 237
	if (!cf)
		return -ENOMEM;

	cf->board = board;
	cf->pdev = pdev;
A
Andrew Victor 已提交
238
	cf->phys_baseaddr = io->start;
239
	platform_set_drvdata(pdev, cf);
240 241

	/* must be a GPIO; ergo must trigger on both edges */
D
David Brownell 已提交
242
	status = gpio_request(board->det_pin, "cf_det");
243 244
	if (status < 0)
		goto fail0;
D
David Brownell 已提交
245 246 247
	status = request_irq(board->det_pin, at91_cf_irq, 0, driver_name, cf);
	if (status < 0)
		goto fail00;
248
	device_init_wakeup(&pdev->dev, 1);
249

D
David Brownell 已提交
250 251 252 253 254 255 256 257 258 259
	status = gpio_request(board->rst_pin, "cf_rst");
	if (status < 0)
		goto fail0a;

	if (board->vcc_pin) {
		status = gpio_request(board->vcc_pin, "cf_vcc");
		if (status < 0)
			goto fail0b;
	}

260 261 262 263 264 265 266
	/*
	 * The card driver will request this irq later as needed.
	 * but it causes lots of "irqNN: nobody cared" messages
	 * unless we report that we handle everything (sigh).
	 * (Note:  DK board doesn't wire the IRQ pin...)
	 */
	if (board->irq_pin) {
D
David Brownell 已提交
267 268 269
		status = gpio_request(board->irq_pin, "cf_irq");
		if (status < 0)
			goto fail0c;
270
		status = request_irq(board->irq_pin, at91_cf_irq,
271
				IRQF_SHARED, driver_name, cf);
272
		if (status < 0)
D
David Brownell 已提交
273
			goto fail0d;
274
		cf->socket.pci_irq = board->irq_pin;
D
David Brownell 已提交
275
	} else
Y
Yinghai Lu 已提交
276
		cf->socket.pci_irq = nr_irqs + 1;
277 278

	/* pcmcia layer only remaps "real" memory not iospace */
D
David Brownell 已提交
279 280
	cf->socket.io_offset = (unsigned long)
			ioremap(cf->phys_baseaddr + CF_IO_PHYS, SZ_2K);
A
Andrew Victor 已提交
281 282
	if (!cf->socket.io_offset) {
		status = -ENXIO;
283
		goto fail1;
A
Andrew Victor 已提交
284
	}
285

A
Andrew Victor 已提交
286
	/* reserve chip-select regions */
D
David Brownell 已提交
287
	if (!request_mem_region(io->start, io->end + 1 - io->start,
A
Andrew Victor 已提交
288 289
				driver_name)) {
		status = -ENXIO;
290
		goto fail1;
A
Andrew Victor 已提交
291
	}
292 293 294 295 296

	pr_info("%s: irqs det #%d, io #%d\n", driver_name,
		board->det_pin, board->irq_pin);

	cf->socket.owner = THIS_MODULE;
297
	cf->socket.dev.parent = &pdev->dev;
298 299 300 301 302
	cf->socket.ops = &at91_cf_ops;
	cf->socket.resource_ops = &pccard_static_ops;
	cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
				| SS_CAP_MEM_ALIGN;
	cf->socket.map_size = SZ_2K;
D
David Brownell 已提交
303
	cf->socket.io[0].res = io;
304 305 306 307 308 309 310 311

	status = pcmcia_register_socket(&cf->socket);
	if (status < 0)
		goto fail2;

	return 0;

fail2:
D
David Brownell 已提交
312
	release_mem_region(io->start, io->end + 1 - io->start);
313
fail1:
314 315
	if (cf->socket.io_offset)
		iounmap((void __iomem *) cf->socket.io_offset);
D
David Brownell 已提交
316
	if (board->irq_pin) {
317
		free_irq(board->irq_pin, cf);
D
David Brownell 已提交
318 319 320 321 322 323 324 325
fail0d:
		gpio_free(board->irq_pin);
	}
fail0c:
	if (board->vcc_pin)
		gpio_free(board->vcc_pin);
fail0b:
	gpio_free(board->rst_pin);
326
fail0a:
D
David Brownell 已提交
327
	device_init_wakeup(&pdev->dev, 0);
328
	free_irq(board->det_pin, cf);
D
David Brownell 已提交
329 330
fail00:
	gpio_free(board->det_pin);
331 332 333 334 335
fail0:
	kfree(cf);
	return status;
}

336
static int __exit at91_cf_remove(struct platform_device *pdev)
337
{
338 339
	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
	struct at91_cf_data	*board = cf->board;
D
David Brownell 已提交
340
	struct resource		*io = cf->socket.io[0].res;
341 342

	pcmcia_unregister_socket(&cf->socket);
D
David Brownell 已提交
343 344 345
	release_mem_region(io->start, io->end + 1 - io->start);
	iounmap((void __iomem *) cf->socket.io_offset);
	if (board->irq_pin) {
346
		free_irq(board->irq_pin, cf);
D
David Brownell 已提交
347 348 349 350 351
		gpio_free(board->irq_pin);
	}
	if (board->vcc_pin)
		gpio_free(board->vcc_pin);
	gpio_free(board->rst_pin);
352
	device_init_wakeup(&pdev->dev, 0);
A
Andrew Victor 已提交
353
	free_irq(board->det_pin, cf);
D
David Brownell 已提交
354
	gpio_free(board->det_pin);
355 356 357 358
	kfree(cf);
	return 0;
}

359 360 361 362 363 364 365
#ifdef	CONFIG_PM

static int at91_cf_suspend(struct platform_device *pdev, pm_message_t mesg)
{
	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
	struct at91_cf_data	*board = cf->board;

366
	pcmcia_socket_dev_suspend(&pdev->dev);
D
David Brownell 已提交
367
	if (device_may_wakeup(&pdev->dev)) {
368
		enable_irq_wake(board->det_pin);
D
David Brownell 已提交
369 370
		if (board->irq_pin)
			enable_irq_wake(board->irq_pin);
371 372 373 374 375 376
	}
	return 0;
}

static int at91_cf_resume(struct platform_device *pdev)
{
377 378 379 380 381 382 383 384 385
	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
	struct at91_cf_data	*board = cf->board;

	if (device_may_wakeup(&pdev->dev)) {
		disable_irq_wake(board->det_pin);
		if (board->irq_pin)
			disable_irq_wake(board->irq_pin);
	}

386 387 388 389 390 391 392 393 394 395 396 397 398 399
	pcmcia_socket_dev_resume(&pdev->dev);
	return 0;
}

#else
#define	at91_cf_suspend		NULL
#define	at91_cf_resume		NULL
#endif

static struct platform_driver at91_cf_driver = {
	.driver = {
		.name		= (char *) driver_name,
		.owner		= THIS_MODULE,
	},
400
	.remove		= __exit_p(at91_cf_remove),
401 402
	.suspend	= at91_cf_suspend,
	.resume		= at91_cf_resume,
403 404 405 406 407 408
};

/*--------------------------------------------------------------------------*/

static int __init at91_cf_init(void)
{
D
David Brownell 已提交
409
	return platform_driver_probe(&at91_cf_driver, at91_cf_probe);
410 411 412 413 414
}
module_init(at91_cf_init);

static void __exit at91_cf_exit(void)
{
415
	platform_driver_unregister(&at91_cf_driver);
416 417 418 419 420 421
}
module_exit(at91_cf_exit);

MODULE_DESCRIPTION("AT91 Compact Flash Driver");
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");
422
MODULE_ALIAS("platform:at91_cf");