at91_cf.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>
18
#include <linux/slab.h>
19
#include <linux/gpio.h>
20
#include <linux/platform_data/atmel.h>
21 22
#include <linux/io.h>
#include <linux/sizes.h>
23 24 25
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
26 27 28

#include <pcmcia/ss.h>

29
#include <mach/at91rm9200_mc.h>
30
#include <mach/at91_ramc.h>
31 32 33 34 35 36


/*
 * 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.
37
 * Note: These are offsets from the physical base address.
38
 */
39 40 41
#define	CF_ATTR_PHYS	(0)
#define	CF_IO_PHYS	(1 << 23)
#define	CF_MEM_PHYS	(0x017ff800)
42 43 44 45 46 47 48 49 50 51

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

struct at91_cf_socket {
	struct pcmcia_socket	socket;

	unsigned		present:1;

	struct platform_device	*pdev;
	struct at91_cf_data	*board;
52 53

	unsigned long		phys_baseaddr;
54 55 56 57
};

static inline int at91_cf_present(struct at91_cf_socket *cf)
{
58
	return !gpio_get_value(cf->board->det_pin);
59 60 61 62 63 64 65 66 67
}

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

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

68
static irqreturn_t at91_cf_irq(int irq, void *_cf)
69
{
70
	struct at91_cf_socket *cf = _cf;
71

72
	if (irq == gpio_to_irq(cf->board->det_pin)) {
73 74 75 76 77
		unsigned present = at91_cf_present(cf);

		/* kick pccard as needed */
		if (present != cf->present) {
			cf->present = present;
78
			dev_dbg(&cf->pdev->dev, "card %s\n",
79
					present ? "present" : "gone");
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
			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);

96
	/* NOTE: CF is always 3VCARD */
97
	if (at91_cf_present(cf)) {
98 99
		int rdy	= gpio_is_valid(cf->board->irq_pin);	/* RDY/nIRQ */
		int vcc	= gpio_is_valid(cf->board->vcc_pin);
100 101

		*sp = SS_DETECT | SS_3VCARD;
102
		if (!rdy || gpio_get_value(cf->board->irq_pin))
103
			*sp |= SS_READY;
104
		if (!vcc || gpio_get_value(cf->board->vcc_pin))
105 106 107 108 109 110 111
			*sp |= SS_POWERON;
	} else
		*sp = 0;

	return 0;
}

112 113
static int
at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
114 115 116 117 118 119
{
	struct at91_cf_socket	*cf;

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

	/* switch Vcc if needed and possible */
120
	if (gpio_is_valid(cf->board->vcc_pin)) {
121
		switch (s->Vcc) {
122 123 124 125 126 127 128 129
		case 0:
			gpio_set_value(cf->board->vcc_pin, 0);
			break;
		case 33:
			gpio_set_value(cf->board->vcc_pin, 1);
			break;
		default:
			return -EINVAL;
130 131 132 133
		}
	}

	/* toggle reset if needed */
134
	gpio_set_value(cf->board->rst_pin, s->flags & SS_RESET);
135

136 137
	dev_dbg(&cf->pdev->dev, "Vcc %d, io_irq %d, flags %04x csc %04x\n",
				s->Vcc, s->io_irq, s->flags, s->csc_mask);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

	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.
	 */
159
	csr = at91_ramc_read(0, AT91_SMC_CSR(cf->board->chipselect)) & ~AT91_SMC_DBW;
160 161 162 163 164 165 166 167 168 169 170

	/*
	 * 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.
	 */
171
	if (!(io->flags & (MAP_16BIT | MAP_AUTOSZ))) {
172
		csr |= AT91_SMC_DBW_8;
173
		dev_dbg(&cf->pdev->dev, "8bit i/o bus\n");
174 175
	} else {
		csr |= AT91_SMC_DBW_16;
176
		dev_dbg(&cf->pdev->dev, "16bit i/o bus\n");
177
	}
178
	at91_ramc_write(0, AT91_SMC_CSR(cf->board->chipselect), csr);
179 180 181 182 183 184 185 186

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

	return 0;
}

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

	if (map->card_start)
		return -EINVAL;

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

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

	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,
};

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

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#if defined(CONFIG_OF)
static const struct of_device_id at91_cf_dt_ids[] = {
	{ .compatible = "atmel,at91rm9200-cf" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, at91_cf_dt_ids);

static int at91_cf_dt_init(struct platform_device *pdev)
{
	struct at91_cf_data *board;

	board = devm_kzalloc(&pdev->dev, sizeof(*board), GFP_KERNEL);
	if (!board)
		return -ENOMEM;

	board->irq_pin = of_get_gpio(pdev->dev.of_node, 0);
	board->det_pin = of_get_gpio(pdev->dev.of_node, 1);
	board->vcc_pin = of_get_gpio(pdev->dev.of_node, 2);
	board->rst_pin = of_get_gpio(pdev->dev.of_node, 3);

	pdev->dev.platform_data = board;

	return 0;
}
#else
static int at91_cf_dt_init(struct platform_device *pdev)
{
	return -ENODEV;
}
#endif

248
static int at91_cf_probe(struct platform_device *pdev)
249 250
{
	struct at91_cf_socket	*cf;
251
	struct at91_cf_data	*board = pdev->dev.platform_data;
252
	struct resource		*io;
253 254
	int			status;

255 256 257 258 259 260 261 262 263
	if (!board) {
		status = at91_cf_dt_init(pdev);
		if (status)
			return status;

		board = pdev->dev.platform_data;
	}

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

266 267 268 269
	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!io)
		return -ENODEV;

270
	cf = devm_kzalloc(&pdev->dev, sizeof(*cf), GFP_KERNEL);
271 272 273 274 275
	if (!cf)
		return -ENOMEM;

	cf->board = board;
	cf->pdev = pdev;
276
	cf->phys_baseaddr = io->start;
277
	platform_set_drvdata(pdev, cf);
278 279

	/* must be a GPIO; ergo must trigger on both edges */
280
	status = devm_gpio_request(&pdev->dev, board->det_pin, "cf_det");
281
	if (status < 0)
282 283 284 285
		return status;

	status = devm_request_irq(&pdev->dev, gpio_to_irq(board->det_pin),
					at91_cf_irq, 0, "at91_cf detect", cf);
286
	if (status < 0)
287 288
		return status;

289
	device_init_wakeup(&pdev->dev, 1);
290

291
	status = devm_gpio_request(&pdev->dev, board->rst_pin, "cf_rst");
292 293 294
	if (status < 0)
		goto fail0a;

295
	if (gpio_is_valid(board->vcc_pin)) {
296
		status = devm_gpio_request(&pdev->dev, board->vcc_pin, "cf_vcc");
297
		if (status < 0)
298
			goto fail0a;
299 300
	}

301 302 303 304 305 306
	/*
	 * 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...)
	 */
307
	if (gpio_is_valid(board->irq_pin)) {
308
		status = devm_gpio_request(&pdev->dev, board->irq_pin, "cf_irq");
309
		if (status < 0)
310 311 312 313
			goto fail0a;

		status = devm_request_irq(&pdev->dev, gpio_to_irq(board->irq_pin),
					at91_cf_irq, IRQF_SHARED, "at91_cf", cf);
314
		if (status < 0)
315
			goto fail0a;
316
		cf->socket.pci_irq = gpio_to_irq(board->irq_pin);
317
	} else
Yinghai Lu's avatar
Yinghai Lu committed
318
		cf->socket.pci_irq = nr_irqs + 1;
319

320 321 322 323 324 325 326
	/*
	 * pcmcia layer only remaps "real" memory not iospace
	 * io_offset is set to 0x10000 to avoid the check in static_find_io().
	 * */
	cf->socket.io_offset = 0x10000;
	status = pci_ioremap_io(0x10000, cf->phys_baseaddr + CF_IO_PHYS);
	if (status)
327
		goto fail0a;
328

329
	/* reserve chip-select regions */
330
	if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io), "at91_cf")) {
331
		status = -ENXIO;
332
		goto fail0a;
333
	}
334

335
	dev_info(&pdev->dev, "irqs det #%d, io #%d\n",
336
		gpio_to_irq(board->det_pin), gpio_to_irq(board->irq_pin));
337 338

	cf->socket.owner = THIS_MODULE;
339
	cf->socket.dev.parent = &pdev->dev;
340 341 342 343 344
	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;
345
	cf->socket.io[0].res = io;
346 347 348

	status = pcmcia_register_socket(&cf->socket);
	if (status < 0)
349
		goto fail0a;
350 351 352 353

	return 0;

fail0a:
354
	device_init_wakeup(&pdev->dev, 0);
355 356 357
	return status;
}

358
static int at91_cf_remove(struct platform_device *pdev)
359
{
360
	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
361 362

	pcmcia_unregister_socket(&cf->socket);
363
	device_init_wakeup(&pdev->dev, 0);
364

365 366 367
	return 0;
}

368 369 370 371 372 373 374
#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;

375
	if (device_may_wakeup(&pdev->dev)) {
376 377 378
		enable_irq_wake(gpio_to_irq(board->det_pin));
		if (gpio_is_valid(board->irq_pin))
			enable_irq_wake(gpio_to_irq(board->irq_pin));
379 380 381 382 383 384
	}
	return 0;
}

static int at91_cf_resume(struct platform_device *pdev)
{
385 386 387 388
	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
	struct at91_cf_data	*board = cf->board;

	if (device_may_wakeup(&pdev->dev)) {
389 390 391
		disable_irq_wake(gpio_to_irq(board->det_pin));
		if (gpio_is_valid(board->irq_pin))
			disable_irq_wake(gpio_to_irq(board->irq_pin));
392 393
	}

394 395 396 397 398 399 400 401 402 403
	return 0;
}

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

static struct platform_driver at91_cf_driver = {
	.driver = {
404
		.name		= "at91_cf",
405
		.of_match_table = of_match_ptr(at91_cf_dt_ids),
406
	},
407 408
	.probe		= at91_cf_probe,
	.remove		= at91_cf_remove,
409 410
	.suspend	= at91_cf_suspend,
	.resume		= at91_cf_resume,
411 412
};

413
module_platform_driver(at91_cf_driver);
414 415 416 417

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