1 /*
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>
14 #include <linux/platform_device.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/slab.h>
19 #include <linux/gpio.h>
20 #include <linux/platform_data/atmel.h>
21 #include <linux/io.h>
22 #include <linux/sizes.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/mfd/syscon/atmel-mc.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/regmap.h>
29 
30 #include <pcmcia/ss.h>
31 
32 /*
33  * A0..A10 work in each range; A23 indicates I/O space;  A25 is CFRNW;
34  * some other bit in {A24,A22..A11} is nREG to flag memory access
35  * (vs attributes).  So more than 2KB/region would just be waste.
36  * Note: These are offsets from the physical base address.
37  */
38 #define	CF_ATTR_PHYS	(0)
39 #define	CF_IO_PHYS	(1 << 23)
40 #define	CF_MEM_PHYS	(0x017ff800)
41 
42 struct regmap *mc;
43 
44 /*--------------------------------------------------------------------------*/
45 
46 struct at91_cf_socket {
47 	struct pcmcia_socket	socket;
48 
49 	unsigned		present:1;
50 
51 	struct platform_device	*pdev;
52 	struct at91_cf_data	*board;
53 
54 	unsigned long		phys_baseaddr;
55 };
56 
at91_cf_present(struct at91_cf_socket * cf)57 static inline int at91_cf_present(struct at91_cf_socket *cf)
58 {
59 	return !gpio_get_value(cf->board->det_pin);
60 }
61 
62 /*--------------------------------------------------------------------------*/
63 
at91_cf_ss_init(struct pcmcia_socket * s)64 static int at91_cf_ss_init(struct pcmcia_socket *s)
65 {
66 	return 0;
67 }
68 
at91_cf_irq(int irq,void * _cf)69 static irqreturn_t at91_cf_irq(int irq, void *_cf)
70 {
71 	struct at91_cf_socket *cf = _cf;
72 
73 	if (irq == gpio_to_irq(cf->board->det_pin)) {
74 		unsigned present = at91_cf_present(cf);
75 
76 		/* kick pccard as needed */
77 		if (present != cf->present) {
78 			cf->present = present;
79 			dev_dbg(&cf->pdev->dev, "card %s\n",
80 					present ? "present" : "gone");
81 			pcmcia_parse_events(&cf->socket, SS_DETECT);
82 		}
83 	}
84 
85 	return IRQ_HANDLED;
86 }
87 
at91_cf_get_status(struct pcmcia_socket * s,u_int * sp)88 static int at91_cf_get_status(struct pcmcia_socket *s, u_int *sp)
89 {
90 	struct at91_cf_socket	*cf;
91 
92 	if (!sp)
93 		return -EINVAL;
94 
95 	cf = container_of(s, struct at91_cf_socket, socket);
96 
97 	/* NOTE: CF is always 3VCARD */
98 	if (at91_cf_present(cf)) {
99 		int rdy	= gpio_is_valid(cf->board->irq_pin);	/* RDY/nIRQ */
100 		int vcc	= gpio_is_valid(cf->board->vcc_pin);
101 
102 		*sp = SS_DETECT | SS_3VCARD;
103 		if (!rdy || gpio_get_value(cf->board->irq_pin))
104 			*sp |= SS_READY;
105 		if (!vcc || gpio_get_value(cf->board->vcc_pin))
106 			*sp |= SS_POWERON;
107 	} else
108 		*sp = 0;
109 
110 	return 0;
111 }
112 
113 static int
at91_cf_set_socket(struct pcmcia_socket * sock,struct socket_state_t * s)114 at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
115 {
116 	struct at91_cf_socket	*cf;
117 
118 	cf = container_of(sock, struct at91_cf_socket, socket);
119 
120 	/* switch Vcc if needed and possible */
121 	if (gpio_is_valid(cf->board->vcc_pin)) {
122 		switch (s->Vcc) {
123 		case 0:
124 			gpio_set_value(cf->board->vcc_pin, 0);
125 			break;
126 		case 33:
127 			gpio_set_value(cf->board->vcc_pin, 1);
128 			break;
129 		default:
130 			return -EINVAL;
131 		}
132 	}
133 
134 	/* toggle reset if needed */
135 	gpio_set_value(cf->board->rst_pin, s->flags & SS_RESET);
136 
137 	dev_dbg(&cf->pdev->dev, "Vcc %d, io_irq %d, flags %04x csc %04x\n",
138 				s->Vcc, s->io_irq, s->flags, s->csc_mask);
139 
140 	return 0;
141 }
142 
at91_cf_ss_suspend(struct pcmcia_socket * s)143 static int at91_cf_ss_suspend(struct pcmcia_socket *s)
144 {
145 	return at91_cf_set_socket(s, &dead_socket);
146 }
147 
148 /* we already mapped the I/O region */
at91_cf_set_io_map(struct pcmcia_socket * s,struct pccard_io_map * io)149 static int at91_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
150 {
151 	struct at91_cf_socket	*cf;
152 	u32			csr;
153 
154 	cf = container_of(s, struct at91_cf_socket, socket);
155 	io->flags &= (MAP_ACTIVE | MAP_16BIT | MAP_AUTOSZ);
156 
157 	/*
158 	 * Use 16 bit accesses unless/until we need 8-bit i/o space.
159 	 *
160 	 * NOTE: this CF controller ignores IOIS16, so we can't really do
161 	 * MAP_AUTOSZ.  The 16bit mode allows single byte access on either
162 	 * D0-D7 (even addr) or D8-D15 (odd), so it's close enough for many
163 	 * purposes (and handles ide-cs).
164 	 *
165 	 * The 8bit mode is needed for odd byte access on D0-D7.  It seems
166 	 * some cards only like that way to get at the odd byte, despite
167 	 * CF 3.0 spec table 35 also giving the D8-D15 option.
168 	 */
169 	if (!(io->flags & (MAP_16BIT | MAP_AUTOSZ))) {
170 		csr = AT91_MC_SMC_DBW_8;
171 		dev_dbg(&cf->pdev->dev, "8bit i/o bus\n");
172 	} else {
173 		csr = AT91_MC_SMC_DBW_16;
174 		dev_dbg(&cf->pdev->dev, "16bit i/o bus\n");
175 	}
176 	regmap_update_bits(mc, AT91_MC_SMC_CSR(cf->board->chipselect),
177 			   AT91_MC_SMC_DBW, csr);
178 
179 	io->start = cf->socket.io_offset;
180 	io->stop = io->start + SZ_2K - 1;
181 
182 	return 0;
183 }
184 
185 /* pcmcia layer maps/unmaps mem regions */
186 static int
at91_cf_set_mem_map(struct pcmcia_socket * s,struct pccard_mem_map * map)187 at91_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
188 {
189 	struct at91_cf_socket	*cf;
190 
191 	if (map->card_start)
192 		return -EINVAL;
193 
194 	cf = container_of(s, struct at91_cf_socket, socket);
195 
196 	map->flags &= (MAP_ACTIVE | MAP_ATTRIB | MAP_16BIT);
197 	if (map->flags & MAP_ATTRIB)
198 		map->static_start = cf->phys_baseaddr + CF_ATTR_PHYS;
199 	else
200 		map->static_start = cf->phys_baseaddr + CF_MEM_PHYS;
201 
202 	return 0;
203 }
204 
205 static struct pccard_operations at91_cf_ops = {
206 	.init			= at91_cf_ss_init,
207 	.suspend		= at91_cf_ss_suspend,
208 	.get_status		= at91_cf_get_status,
209 	.set_socket		= at91_cf_set_socket,
210 	.set_io_map		= at91_cf_set_io_map,
211 	.set_mem_map		= at91_cf_set_mem_map,
212 };
213 
214 /*--------------------------------------------------------------------------*/
215 
216 #if defined(CONFIG_OF)
217 static const struct of_device_id at91_cf_dt_ids[] = {
218 	{ .compatible = "atmel,at91rm9200-cf" },
219 	{ /* sentinel */ }
220 };
221 MODULE_DEVICE_TABLE(of, at91_cf_dt_ids);
222 
at91_cf_dt_init(struct platform_device * pdev)223 static int at91_cf_dt_init(struct platform_device *pdev)
224 {
225 	struct at91_cf_data *board;
226 
227 	board = devm_kzalloc(&pdev->dev, sizeof(*board), GFP_KERNEL);
228 	if (!board)
229 		return -ENOMEM;
230 
231 	board->irq_pin = of_get_gpio(pdev->dev.of_node, 0);
232 	board->det_pin = of_get_gpio(pdev->dev.of_node, 1);
233 	board->vcc_pin = of_get_gpio(pdev->dev.of_node, 2);
234 	board->rst_pin = of_get_gpio(pdev->dev.of_node, 3);
235 
236 	pdev->dev.platform_data = board;
237 
238 	mc = syscon_regmap_lookup_by_compatible("atmel,at91rm9200-sdramc");
239 
240 	return PTR_ERR_OR_ZERO(mc);
241 }
242 #else
at91_cf_dt_init(struct platform_device * pdev)243 static int at91_cf_dt_init(struct platform_device *pdev)
244 {
245 	return -ENODEV;
246 }
247 #endif
248 
at91_cf_probe(struct platform_device * pdev)249 static int at91_cf_probe(struct platform_device *pdev)
250 {
251 	struct at91_cf_socket	*cf;
252 	struct at91_cf_data	*board = pdev->dev.platform_data;
253 	struct resource		*io;
254 	int			status;
255 
256 	if (!board) {
257 		status = at91_cf_dt_init(pdev);
258 		if (status)
259 			return status;
260 
261 		board = pdev->dev.platform_data;
262 	}
263 
264 	if (!gpio_is_valid(board->det_pin) || !gpio_is_valid(board->rst_pin))
265 		return -ENODEV;
266 
267 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
268 	if (!io)
269 		return -ENODEV;
270 
271 	cf = devm_kzalloc(&pdev->dev, sizeof(*cf), GFP_KERNEL);
272 	if (!cf)
273 		return -ENOMEM;
274 
275 	cf->board = board;
276 	cf->pdev = pdev;
277 	cf->phys_baseaddr = io->start;
278 	platform_set_drvdata(pdev, cf);
279 
280 	/* must be a GPIO; ergo must trigger on both edges */
281 	status = devm_gpio_request(&pdev->dev, board->det_pin, "cf_det");
282 	if (status < 0)
283 		return status;
284 
285 	status = devm_request_irq(&pdev->dev, gpio_to_irq(board->det_pin),
286 					at91_cf_irq, 0, "at91_cf detect", cf);
287 	if (status < 0)
288 		return status;
289 
290 	device_init_wakeup(&pdev->dev, 1);
291 
292 	status = devm_gpio_request(&pdev->dev, board->rst_pin, "cf_rst");
293 	if (status < 0)
294 		goto fail0a;
295 
296 	if (gpio_is_valid(board->vcc_pin)) {
297 		status = devm_gpio_request(&pdev->dev, board->vcc_pin, "cf_vcc");
298 		if (status < 0)
299 			goto fail0a;
300 	}
301 
302 	/*
303 	 * The card driver will request this irq later as needed.
304 	 * but it causes lots of "irqNN: nobody cared" messages
305 	 * unless we report that we handle everything (sigh).
306 	 * (Note:  DK board doesn't wire the IRQ pin...)
307 	 */
308 	if (gpio_is_valid(board->irq_pin)) {
309 		status = devm_gpio_request(&pdev->dev, board->irq_pin, "cf_irq");
310 		if (status < 0)
311 			goto fail0a;
312 
313 		status = devm_request_irq(&pdev->dev, gpio_to_irq(board->irq_pin),
314 					at91_cf_irq, IRQF_SHARED, "at91_cf", cf);
315 		if (status < 0)
316 			goto fail0a;
317 		cf->socket.pci_irq = gpio_to_irq(board->irq_pin);
318 	} else
319 		cf->socket.pci_irq = nr_irqs + 1;
320 
321 	/*
322 	 * pcmcia layer only remaps "real" memory not iospace
323 	 * io_offset is set to 0x10000 to avoid the check in static_find_io().
324 	 * */
325 	cf->socket.io_offset = 0x10000;
326 	status = pci_ioremap_io(0x10000, cf->phys_baseaddr + CF_IO_PHYS);
327 	if (status)
328 		goto fail0a;
329 
330 	/* reserve chip-select regions */
331 	if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io), "at91_cf")) {
332 		status = -ENXIO;
333 		goto fail0a;
334 	}
335 
336 	dev_info(&pdev->dev, "irqs det #%d, io #%d\n",
337 		gpio_to_irq(board->det_pin), gpio_to_irq(board->irq_pin));
338 
339 	cf->socket.owner = THIS_MODULE;
340 	cf->socket.dev.parent = &pdev->dev;
341 	cf->socket.ops = &at91_cf_ops;
342 	cf->socket.resource_ops = &pccard_static_ops;
343 	cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
344 				| SS_CAP_MEM_ALIGN;
345 	cf->socket.map_size = SZ_2K;
346 	cf->socket.io[0].res = io;
347 
348 	status = pcmcia_register_socket(&cf->socket);
349 	if (status < 0)
350 		goto fail0a;
351 
352 	return 0;
353 
354 fail0a:
355 	device_init_wakeup(&pdev->dev, 0);
356 	return status;
357 }
358 
at91_cf_remove(struct platform_device * pdev)359 static int at91_cf_remove(struct platform_device *pdev)
360 {
361 	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
362 
363 	pcmcia_unregister_socket(&cf->socket);
364 	device_init_wakeup(&pdev->dev, 0);
365 
366 	return 0;
367 }
368 
369 #ifdef	CONFIG_PM
370 
at91_cf_suspend(struct platform_device * pdev,pm_message_t mesg)371 static int at91_cf_suspend(struct platform_device *pdev, pm_message_t mesg)
372 {
373 	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
374 	struct at91_cf_data	*board = cf->board;
375 
376 	if (device_may_wakeup(&pdev->dev)) {
377 		enable_irq_wake(gpio_to_irq(board->det_pin));
378 		if (gpio_is_valid(board->irq_pin))
379 			enable_irq_wake(gpio_to_irq(board->irq_pin));
380 	}
381 	return 0;
382 }
383 
at91_cf_resume(struct platform_device * pdev)384 static int at91_cf_resume(struct platform_device *pdev)
385 {
386 	struct at91_cf_socket	*cf = platform_get_drvdata(pdev);
387 	struct at91_cf_data	*board = cf->board;
388 
389 	if (device_may_wakeup(&pdev->dev)) {
390 		disable_irq_wake(gpio_to_irq(board->det_pin));
391 		if (gpio_is_valid(board->irq_pin))
392 			disable_irq_wake(gpio_to_irq(board->irq_pin));
393 	}
394 
395 	return 0;
396 }
397 
398 #else
399 #define	at91_cf_suspend		NULL
400 #define	at91_cf_resume		NULL
401 #endif
402 
403 static struct platform_driver at91_cf_driver = {
404 	.driver = {
405 		.name		= "at91_cf",
406 		.of_match_table = of_match_ptr(at91_cf_dt_ids),
407 	},
408 	.probe		= at91_cf_probe,
409 	.remove		= at91_cf_remove,
410 	.suspend	= at91_cf_suspend,
411 	.resume		= at91_cf_resume,
412 };
413 
414 module_platform_driver(at91_cf_driver);
415 
416 MODULE_DESCRIPTION("AT91 Compact Flash Driver");
417 MODULE_AUTHOR("David Brownell");
418 MODULE_LICENSE("GPL");
419 MODULE_ALIAS("platform:at91_cf");
420