blob: bce8a64cd7c0d52f19345e31ffcb36d27312df54 [file] [log] [blame]
Andrew Victor2c1f3b72006-02-21 12:56:52 +02001/*
2 * at91_cf.c -- AT91 CompactFlash controller driver
3 *
4 * Copyright (C) 2005 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
Andrew Victor2c1f3b72006-02-21 12:56:52 +020014#include <linux/platform_device.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Joachim Eastwood80af9e62012-01-10 02:37:25 +010019#include <linux/gpio.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080020#include <linux/platform_data/atmel.h>
Joachim Eastwooda8431682013-06-06 10:24:17 +020021#include <linux/io.h>
22#include <linux/sizes.h>
Andrew Victor2c1f3b72006-02-21 12:56:52 +020023
24#include <pcmcia/ss.h>
25
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/at91rm9200_mc.h>
Jean-Christophe PLAGNIOL-VILLARDf363c402012-02-13 12:58:53 +080027#include <mach/at91_ramc.h>
Andrew Victor2c1f3b72006-02-21 12:56:52 +020028
29
Andrew Victor2c1f3b72006-02-21 12:56:52 +020030/*
31 * A0..A10 work in each range; A23 indicates I/O space; A25 is CFRNW;
32 * some other bit in {A24,A22..A11} is nREG to flag memory access
33 * (vs attributes). So more than 2KB/region would just be waste.
Andrew Victorebe5cfb2006-11-06 15:56:07 +020034 * Note: These are offsets from the physical base address.
Andrew Victor2c1f3b72006-02-21 12:56:52 +020035 */
Andrew Victorebe5cfb2006-11-06 15:56:07 +020036#define CF_ATTR_PHYS (0)
37#define CF_IO_PHYS (1 << 23)
38#define CF_MEM_PHYS (0x017ff800)
Andrew Victor2c1f3b72006-02-21 12:56:52 +020039
40/*--------------------------------------------------------------------------*/
41
Andrew Victor2c1f3b72006-02-21 12:56:52 +020042struct at91_cf_socket {
43 struct pcmcia_socket socket;
44
45 unsigned present:1;
46
47 struct platform_device *pdev;
48 struct at91_cf_data *board;
Andrew Victorebe5cfb2006-11-06 15:56:07 +020049
50 unsigned long phys_baseaddr;
Andrew Victor2c1f3b72006-02-21 12:56:52 +020051};
52
Andrew Victor2c1f3b72006-02-21 12:56:52 +020053static inline int at91_cf_present(struct at91_cf_socket *cf)
54{
David Brownell4c1fc442008-02-04 22:27:42 -080055 return !gpio_get_value(cf->board->det_pin);
Andrew Victor2c1f3b72006-02-21 12:56:52 +020056}
57
58/*--------------------------------------------------------------------------*/
59
60static int at91_cf_ss_init(struct pcmcia_socket *s)
61{
62 return 0;
63}
64
David Howells7d12e782006-10-05 14:55:46 +010065static irqreturn_t at91_cf_irq(int irq, void *_cf)
Andrew Victor2c1f3b72006-02-21 12:56:52 +020066{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -040067 struct at91_cf_socket *cf = _cf;
Andrew Victor2c1f3b72006-02-21 12:56:52 +020068
Joachim Eastwood80af9e62012-01-10 02:37:25 +010069 if (irq == gpio_to_irq(cf->board->det_pin)) {
Andrew Victor2c1f3b72006-02-21 12:56:52 +020070 unsigned present = at91_cf_present(cf);
71
72 /* kick pccard as needed */
73 if (present != cf->present) {
74 cf->present = present;
Joachim Eastwood40ca0202013-06-06 10:24:15 +020075 dev_dbg(&cf->pdev->dev, "card %s\n",
David Brownell2c536202006-04-14 18:05:38 -070076 present ? "present" : "gone");
Andrew Victor2c1f3b72006-02-21 12:56:52 +020077 pcmcia_parse_events(&cf->socket, SS_DETECT);
78 }
79 }
80
81 return IRQ_HANDLED;
82}
83
84static int at91_cf_get_status(struct pcmcia_socket *s, u_int *sp)
85{
86 struct at91_cf_socket *cf;
87
88 if (!sp)
89 return -EINVAL;
90
91 cf = container_of(s, struct at91_cf_socket, socket);
92
David Brownell2c536202006-04-14 18:05:38 -070093 /* NOTE: CF is always 3VCARD */
Andrew Victor2c1f3b72006-02-21 12:56:52 +020094 if (at91_cf_present(cf)) {
Joachim Eastwood80af9e62012-01-10 02:37:25 +010095 int rdy = gpio_is_valid(cf->board->irq_pin); /* RDY/nIRQ */
96 int vcc = gpio_is_valid(cf->board->vcc_pin);
Andrew Victor2c1f3b72006-02-21 12:56:52 +020097
98 *sp = SS_DETECT | SS_3VCARD;
Joachim Eastwoode39506b2013-06-06 10:24:14 +020099 if (!rdy || gpio_get_value(cf->board->irq_pin))
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200100 *sp |= SS_READY;
Joachim Eastwoode39506b2013-06-06 10:24:14 +0200101 if (!vcc || gpio_get_value(cf->board->vcc_pin))
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200102 *sp |= SS_POWERON;
103 } else
104 *sp = 0;
105
106 return 0;
107}
108
David Brownell2c536202006-04-14 18:05:38 -0700109static int
110at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200111{
112 struct at91_cf_socket *cf;
113
114 cf = container_of(sock, struct at91_cf_socket, socket);
115
116 /* switch Vcc if needed and possible */
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100117 if (gpio_is_valid(cf->board->vcc_pin)) {
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200118 switch (s->Vcc) {
119 case 0:
David Brownell4c1fc442008-02-04 22:27:42 -0800120 gpio_set_value(cf->board->vcc_pin, 0);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200121 break;
122 case 33:
David Brownell4c1fc442008-02-04 22:27:42 -0800123 gpio_set_value(cf->board->vcc_pin, 1);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200124 break;
125 default:
126 return -EINVAL;
127 }
128 }
129
130 /* toggle reset if needed */
David Brownell4c1fc442008-02-04 22:27:42 -0800131 gpio_set_value(cf->board->rst_pin, s->flags & SS_RESET);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200132
Joachim Eastwood40ca0202013-06-06 10:24:15 +0200133 dev_dbg(&cf->pdev->dev, "Vcc %d, io_irq %d, flags %04x csc %04x\n",
134 s->Vcc, s->io_irq, s->flags, s->csc_mask);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200135
136 return 0;
137}
138
139static int at91_cf_ss_suspend(struct pcmcia_socket *s)
140{
141 return at91_cf_set_socket(s, &dead_socket);
142}
143
144/* we already mapped the I/O region */
145static int at91_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
146{
147 struct at91_cf_socket *cf;
148 u32 csr;
149
150 cf = container_of(s, struct at91_cf_socket, socket);
151 io->flags &= (MAP_ACTIVE | MAP_16BIT | MAP_AUTOSZ);
152
153 /*
154 * Use 16 bit accesses unless/until we need 8-bit i/o space.
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200155 */
Jean-Christophe PLAGNIOL-VILLARDf363c402012-02-13 12:58:53 +0800156 csr = at91_ramc_read(0, AT91_SMC_CSR(cf->board->chipselect)) & ~AT91_SMC_DBW;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200157
158 /*
159 * NOTE: this CF controller ignores IOIS16, so we can't really do
160 * MAP_AUTOSZ. The 16bit mode allows single byte access on either
161 * D0-D7 (even addr) or D8-D15 (odd), so it's close enough for many
162 * purposes (and handles ide-cs).
163 *
164 * The 8bit mode is needed for odd byte access on D0-D7. It seems
165 * some cards only like that way to get at the odd byte, despite
166 * CF 3.0 spec table 35 also giving the D8-D15 option.
167 */
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200168 if (!(io->flags & (MAP_16BIT | MAP_AUTOSZ))) {
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200169 csr |= AT91_SMC_DBW_8;
Joachim Eastwood40ca0202013-06-06 10:24:15 +0200170 dev_dbg(&cf->pdev->dev, "8bit i/o bus\n");
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200171 } else {
172 csr |= AT91_SMC_DBW_16;
Joachim Eastwood40ca0202013-06-06 10:24:15 +0200173 dev_dbg(&cf->pdev->dev, "16bit i/o bus\n");
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200174 }
Jean-Christophe PLAGNIOL-VILLARDf363c402012-02-13 12:58:53 +0800175 at91_ramc_write(0, AT91_SMC_CSR(cf->board->chipselect), csr);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200176
177 io->start = cf->socket.io_offset;
178 io->stop = io->start + SZ_2K - 1;
179
180 return 0;
181}
182
183/* pcmcia layer maps/unmaps mem regions */
David Brownell2c536202006-04-14 18:05:38 -0700184static int
185at91_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200186{
187 struct at91_cf_socket *cf;
188
189 if (map->card_start)
190 return -EINVAL;
191
192 cf = container_of(s, struct at91_cf_socket, socket);
193
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200194 map->flags &= (MAP_ACTIVE | MAP_ATTRIB | MAP_16BIT);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200195 if (map->flags & MAP_ATTRIB)
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200196 map->static_start = cf->phys_baseaddr + CF_ATTR_PHYS;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200197 else
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200198 map->static_start = cf->phys_baseaddr + CF_MEM_PHYS;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200199
200 return 0;
201}
202
203static struct pccard_operations at91_cf_ops = {
204 .init = at91_cf_ss_init,
205 .suspend = at91_cf_ss_suspend,
206 .get_status = at91_cf_get_status,
207 .set_socket = at91_cf_set_socket,
208 .set_io_map = at91_cf_set_io_map,
209 .set_mem_map = at91_cf_set_mem_map,
210};
211
212/*--------------------------------------------------------------------------*/
213
David Brownell0db60952006-04-19 06:37:48 -0700214static int __init at91_cf_probe(struct platform_device *pdev)
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200215{
216 struct at91_cf_socket *cf;
David Brownell0db60952006-04-19 06:37:48 -0700217 struct at91_cf_data *board = pdev->dev.platform_data;
David Brownell2c536202006-04-14 18:05:38 -0700218 struct resource *io;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200219 int status;
220
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100221 if (!board || !gpio_is_valid(board->det_pin) || !gpio_is_valid(board->rst_pin))
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200222 return -ENODEV;
223
David Brownell2c536202006-04-14 18:05:38 -0700224 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
225 if (!io)
226 return -ENODEV;
227
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200228 cf = devm_kzalloc(&pdev->dev, sizeof(*cf), GFP_KERNEL);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200229 if (!cf)
230 return -ENOMEM;
231
232 cf->board = board;
233 cf->pdev = pdev;
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200234 cf->phys_baseaddr = io->start;
David Brownell0db60952006-04-19 06:37:48 -0700235 platform_set_drvdata(pdev, cf);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200236
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200237 /* must be a GPIO; ergo must trigger on both edges */
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200238 status = devm_gpio_request(&pdev->dev, board->det_pin, "cf_det");
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200239 if (status < 0)
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200240 return status;
241
242 status = devm_request_irq(&pdev->dev, gpio_to_irq(board->det_pin),
243 at91_cf_irq, 0, "at91_cf detect", cf);
David Brownell4c1fc442008-02-04 22:27:42 -0800244 if (status < 0)
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200245 return status;
246
David Brownell0db60952006-04-19 06:37:48 -0700247 device_init_wakeup(&pdev->dev, 1);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200248
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200249 status = devm_gpio_request(&pdev->dev, board->rst_pin, "cf_rst");
David Brownell4c1fc442008-02-04 22:27:42 -0800250 if (status < 0)
251 goto fail0a;
252
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100253 if (gpio_is_valid(board->vcc_pin)) {
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200254 status = devm_gpio_request(&pdev->dev, board->vcc_pin, "cf_vcc");
David Brownell4c1fc442008-02-04 22:27:42 -0800255 if (status < 0)
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200256 goto fail0a;
David Brownell4c1fc442008-02-04 22:27:42 -0800257 }
258
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200259 /*
260 * The card driver will request this irq later as needed.
261 * but it causes lots of "irqNN: nobody cared" messages
262 * unless we report that we handle everything (sigh).
263 * (Note: DK board doesn't wire the IRQ pin...)
264 */
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100265 if (gpio_is_valid(board->irq_pin)) {
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200266 status = devm_gpio_request(&pdev->dev, board->irq_pin, "cf_irq");
David Brownell4c1fc442008-02-04 22:27:42 -0800267 if (status < 0)
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200268 goto fail0a;
269
270 status = devm_request_irq(&pdev->dev, gpio_to_irq(board->irq_pin),
271 at91_cf_irq, IRQF_SHARED, "at91_cf", cf);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200272 if (status < 0)
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200273 goto fail0a;
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100274 cf->socket.pci_irq = gpio_to_irq(board->irq_pin);
David Brownell2c536202006-04-14 18:05:38 -0700275 } else
Yinghai Lu9130add2008-08-19 20:49:52 -0700276 cf->socket.pci_irq = nr_irqs + 1;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200277
278 /* pcmcia layer only remaps "real" memory not iospace */
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200279 cf->socket.io_offset = (unsigned long) devm_ioremap(&pdev->dev,
280 cf->phys_baseaddr + CF_IO_PHYS, SZ_2K);
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200281 if (!cf->socket.io_offset) {
282 status = -ENXIO;
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200283 goto fail0a;
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200284 }
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200285
Andrew Victor40a00172006-12-04 12:26:36 +0200286 /* reserve chip-select regions */
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200287 if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io), "at91_cf")) {
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200288 status = -ENXIO;
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200289 goto fail0a;
Andrew Victorebe5cfb2006-11-06 15:56:07 +0200290 }
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200291
Joachim Eastwood40ca0202013-06-06 10:24:15 +0200292 dev_info(&pdev->dev, "irqs det #%d, io #%d\n",
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100293 gpio_to_irq(board->det_pin), gpio_to_irq(board->irq_pin));
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200294
295 cf->socket.owner = THIS_MODULE;
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800296 cf->socket.dev.parent = &pdev->dev;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200297 cf->socket.ops = &at91_cf_ops;
298 cf->socket.resource_ops = &pccard_static_ops;
299 cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
300 | SS_CAP_MEM_ALIGN;
301 cf->socket.map_size = SZ_2K;
David Brownell2c536202006-04-14 18:05:38 -0700302 cf->socket.io[0].res = io;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200303
304 status = pcmcia_register_socket(&cf->socket);
305 if (status < 0)
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200306 goto fail0a;
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200307
308 return 0;
309
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200310fail0a:
David Brownell1fbece12006-07-01 13:39:55 -0700311 device_init_wakeup(&pdev->dev, 0);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200312 return status;
313}
314
David Brownell0db60952006-04-19 06:37:48 -0700315static int __exit at91_cf_remove(struct platform_device *pdev)
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200316{
David Brownell0db60952006-04-19 06:37:48 -0700317 struct at91_cf_socket *cf = platform_get_drvdata(pdev);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200318
319 pcmcia_unregister_socket(&cf->socket);
David Brownell0db60952006-04-19 06:37:48 -0700320 device_init_wakeup(&pdev->dev, 0);
Joachim Eastwood54fe1592013-06-06 10:24:16 +0200321
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200322 return 0;
323}
324
David Brownell0db60952006-04-19 06:37:48 -0700325#ifdef CONFIG_PM
326
327static int at91_cf_suspend(struct platform_device *pdev, pm_message_t mesg)
328{
329 struct at91_cf_socket *cf = platform_get_drvdata(pdev);
330 struct at91_cf_data *board = cf->board;
331
David Brownell1fbece12006-07-01 13:39:55 -0700332 if (device_may_wakeup(&pdev->dev)) {
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100333 enable_irq_wake(gpio_to_irq(board->det_pin));
334 if (gpio_is_valid(board->irq_pin))
335 enable_irq_wake(gpio_to_irq(board->irq_pin));
David Brownell0db60952006-04-19 06:37:48 -0700336 }
David Brownell0db60952006-04-19 06:37:48 -0700337 return 0;
338}
339
340static int at91_cf_resume(struct platform_device *pdev)
341{
Marc Pignat9af20372007-05-31 00:40:44 -0700342 struct at91_cf_socket *cf = platform_get_drvdata(pdev);
343 struct at91_cf_data *board = cf->board;
344
345 if (device_may_wakeup(&pdev->dev)) {
Joachim Eastwood80af9e62012-01-10 02:37:25 +0100346 disable_irq_wake(gpio_to_irq(board->det_pin));
347 if (gpio_is_valid(board->irq_pin))
348 disable_irq_wake(gpio_to_irq(board->irq_pin));
Marc Pignat9af20372007-05-31 00:40:44 -0700349 }
350
David Brownell0db60952006-04-19 06:37:48 -0700351 return 0;
352}
353
354#else
355#define at91_cf_suspend NULL
356#define at91_cf_resume NULL
357#endif
358
359static struct platform_driver at91_cf_driver = {
360 .driver = {
Joachim Eastwood40ca0202013-06-06 10:24:15 +0200361 .name = "at91_cf",
David Brownell0db60952006-04-19 06:37:48 -0700362 .owner = THIS_MODULE,
363 },
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200364 .remove = __exit_p(at91_cf_remove),
David Brownell0db60952006-04-19 06:37:48 -0700365 .suspend = at91_cf_suspend,
366 .resume = at91_cf_resume,
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200367};
368
369/*--------------------------------------------------------------------------*/
370
371static int __init at91_cf_init(void)
372{
David Brownell02c83592007-05-06 14:48:42 -0700373 return platform_driver_probe(&at91_cf_driver, at91_cf_probe);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200374}
375module_init(at91_cf_init);
376
377static void __exit at91_cf_exit(void)
378{
David Brownell0db60952006-04-19 06:37:48 -0700379 platform_driver_unregister(&at91_cf_driver);
Andrew Victor2c1f3b72006-02-21 12:56:52 +0200380}
381module_exit(at91_cf_exit);
382
383MODULE_DESCRIPTION("AT91 Compact Flash Driver");
384MODULE_AUTHOR("David Brownell");
385MODULE_LICENSE("GPL");
Kay Sievers12c2c012008-04-15 14:34:34 -0700386MODULE_ALIAS("platform:at91_cf");