xref: /openbmc/linux/drivers/pcmcia/omap_cf.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f74e48a5SDavid Brownell /*
3f74e48a5SDavid Brownell  * omap_cf.c -- OMAP 16xx CompactFlash controller driver
4f74e48a5SDavid Brownell  *
5f74e48a5SDavid Brownell  * Copyright (c) 2005 David Brownell
6f74e48a5SDavid Brownell  */
7f74e48a5SDavid Brownell 
8f74e48a5SDavid Brownell #include <linux/module.h>
9f74e48a5SDavid Brownell #include <linux/kernel.h>
10d052d1beSRussell King #include <linux/platform_device.h>
11f74e48a5SDavid Brownell #include <linux/errno.h>
12f74e48a5SDavid Brownell #include <linux/init.h>
13f74e48a5SDavid Brownell #include <linux/delay.h>
14f74e48a5SDavid Brownell #include <linux/interrupt.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
16f74e48a5SDavid Brownell 
17f74e48a5SDavid Brownell #include <pcmcia/ss.h>
18f74e48a5SDavid Brownell 
19f74e48a5SDavid Brownell #include <asm/io.h>
2087dfb311SMasahiro Yamada #include <linux/sizes.h>
21f74e48a5SDavid Brownell 
22d87d44f7SArnd Bergmann #include <linux/soc/ti/omap1-io.h>
23d87d44f7SArnd Bergmann #include <linux/soc/ti/omap1-soc.h>
24d87d44f7SArnd Bergmann #include <linux/soc/ti/omap1-mux.h>
25f74e48a5SDavid Brownell 
26f74e48a5SDavid Brownell /* NOTE:  don't expect this to support many I/O cards.  The 16xx chips have
27f74e48a5SDavid Brownell  * hard-wired timings to support Compact Flash memory cards; they won't work
28f74e48a5SDavid Brownell  * with various other devices (like WLAN adapters) without some external
29f74e48a5SDavid Brownell  * logic to help out.
30f74e48a5SDavid Brownell  *
31f74e48a5SDavid Brownell  * NOTE:  CF controller docs disagree with address space docs as to where
32f74e48a5SDavid Brownell  * CF_BASE really lives; this is a doc erratum.
33f74e48a5SDavid Brownell  */
34f74e48a5SDavid Brownell #define	CF_BASE	0xfffe2800
35f74e48a5SDavid Brownell 
36f74e48a5SDavid Brownell /* status; read after IRQ */
37030b1545STony Lindgren #define CF_STATUS			(CF_BASE + 0x00)
38f74e48a5SDavid Brownell #	define	CF_STATUS_BAD_READ	(1 << 2)
39f74e48a5SDavid Brownell #	define	CF_STATUS_BAD_WRITE	(1 << 1)
40f74e48a5SDavid Brownell #	define	CF_STATUS_CARD_DETECT	(1 << 0)
41f74e48a5SDavid Brownell 
42f74e48a5SDavid Brownell /* which chipselect (CS0..CS3) is used for CF (active low) */
43030b1545STony Lindgren #define CF_CFG				(CF_BASE + 0x02)
44f74e48a5SDavid Brownell 
45f74e48a5SDavid Brownell /* card reset */
46030b1545STony Lindgren #define CF_CONTROL			(CF_BASE + 0x04)
47f74e48a5SDavid Brownell #	define	CF_CONTROL_RESET	(1 << 0)
48f74e48a5SDavid Brownell 
49030b1545STony Lindgren #define omap_cf_present() (!(omap_readw(CF_STATUS) & CF_STATUS_CARD_DETECT))
50f74e48a5SDavid Brownell 
51f74e48a5SDavid Brownell /*--------------------------------------------------------------------------*/
52f74e48a5SDavid Brownell 
53f74e48a5SDavid Brownell static const char driver_name[] = "omap_cf";
54f74e48a5SDavid Brownell 
55f74e48a5SDavid Brownell struct omap_cf_socket {
56f74e48a5SDavid Brownell 	struct pcmcia_socket	socket;
57f74e48a5SDavid Brownell 
58f74e48a5SDavid Brownell 	struct timer_list	timer;
59f74e48a5SDavid Brownell 	unsigned		present:1;
60f74e48a5SDavid Brownell 	unsigned		active:1;
61f74e48a5SDavid Brownell 
62f74e48a5SDavid Brownell 	struct platform_device	*pdev;
63f74e48a5SDavid Brownell 	unsigned long		phys_cf;
64f74e48a5SDavid Brownell 	u_int			irq;
65dcb9c392SDavid Brownell 	struct resource		iomem;
66f74e48a5SDavid Brownell };
67f74e48a5SDavid Brownell 
68f74e48a5SDavid Brownell #define	POLL_INTERVAL		(2 * HZ)
69f74e48a5SDavid Brownell 
70f74e48a5SDavid Brownell /*--------------------------------------------------------------------------*/
71f74e48a5SDavid Brownell 
omap_cf_ss_init(struct pcmcia_socket * s)72f74e48a5SDavid Brownell static int omap_cf_ss_init(struct pcmcia_socket *s)
73f74e48a5SDavid Brownell {
74f74e48a5SDavid Brownell 	return 0;
75f74e48a5SDavid Brownell }
76f74e48a5SDavid Brownell 
77f74e48a5SDavid Brownell /* the timer is primarily to kick this socket's pccardd */
omap_cf_timer(struct timer_list * t)7841760d0eSKees Cook static void omap_cf_timer(struct timer_list *t)
79f74e48a5SDavid Brownell {
8041760d0eSKees Cook 	struct omap_cf_socket	*cf = from_timer(cf, t, timer);
81f74e48a5SDavid Brownell 	unsigned		present = omap_cf_present();
82f74e48a5SDavid Brownell 
83f74e48a5SDavid Brownell 	if (present != cf->present) {
84f74e48a5SDavid Brownell 		cf->present = present;
85f74e48a5SDavid Brownell 		pr_debug("%s: card %s\n", driver_name,
86f74e48a5SDavid Brownell 			present ? "present" : "gone");
87f74e48a5SDavid Brownell 		pcmcia_parse_events(&cf->socket, SS_DETECT);
88f74e48a5SDavid Brownell 	}
89f74e48a5SDavid Brownell 
90f74e48a5SDavid Brownell 	if (cf->active)
91f74e48a5SDavid Brownell 		mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
92f74e48a5SDavid Brownell }
93f74e48a5SDavid Brownell 
94f74e48a5SDavid Brownell /* This irq handler prevents "irqNNN: nobody cared" messages as drivers
95f74e48a5SDavid Brownell  * claim the card's IRQ.  It may also detect some card insertions, but
96f74e48a5SDavid Brownell  * not removals; it can't always eliminate timer irqs.
97f74e48a5SDavid Brownell  */
omap_cf_irq(int irq,void * _cf)987d12e780SDavid Howells static irqreturn_t omap_cf_irq(int irq, void *_cf)
99f74e48a5SDavid Brownell {
100439dc05fSKees Cook 	struct omap_cf_socket *cf = (struct omap_cf_socket *)_cf;
101439dc05fSKees Cook 
102439dc05fSKees Cook 	omap_cf_timer(&cf->timer);
103f74e48a5SDavid Brownell 	return IRQ_HANDLED;
104f74e48a5SDavid Brownell }
105f74e48a5SDavid Brownell 
omap_cf_get_status(struct pcmcia_socket * s,u_int * sp)106f74e48a5SDavid Brownell static int omap_cf_get_status(struct pcmcia_socket *s, u_int *sp)
107f74e48a5SDavid Brownell {
108f74e48a5SDavid Brownell 	if (!sp)
109f74e48a5SDavid Brownell 		return -EINVAL;
110f74e48a5SDavid Brownell 
111dcb9c392SDavid Brownell 	/* NOTE CF is always 3VCARD */
112f74e48a5SDavid Brownell 	if (omap_cf_present()) {
113f74e48a5SDavid Brownell 		struct omap_cf_socket	*cf;
114f74e48a5SDavid Brownell 
115f74e48a5SDavid Brownell 		*sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
116f74e48a5SDavid Brownell 		cf = container_of(s, struct omap_cf_socket, socket);
1176f840afbSDominik Brodowski 		s->pcmcia_irq = 0;
118dcb9c392SDavid Brownell 		s->pci_irq = cf->irq;
119f74e48a5SDavid Brownell 	} else
120f74e48a5SDavid Brownell 		*sp = 0;
121f74e48a5SDavid Brownell 	return 0;
122f74e48a5SDavid Brownell }
123f74e48a5SDavid Brownell 
124f74e48a5SDavid Brownell static int
omap_cf_set_socket(struct pcmcia_socket * sock,struct socket_state_t * s)125f74e48a5SDavid Brownell omap_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
126f74e48a5SDavid Brownell {
127dcb9c392SDavid Brownell 	/* REVISIT some non-OSK boards may support power switching */
128f74e48a5SDavid Brownell 	switch (s->Vcc) {
129f74e48a5SDavid Brownell 	case 0:
130f74e48a5SDavid Brownell 	case 33:
131f74e48a5SDavid Brownell 		break;
132f74e48a5SDavid Brownell 	default:
133f74e48a5SDavid Brownell 		return -EINVAL;
134f74e48a5SDavid Brownell 	}
135f74e48a5SDavid Brownell 
13650f9926dSSouptick Joarder (HPE) 	omap_readw(CF_CONTROL);
137f74e48a5SDavid Brownell 	if (s->flags & SS_RESET)
138030b1545STony Lindgren 		omap_writew(CF_CONTROL_RESET, CF_CONTROL);
139f74e48a5SDavid Brownell 	else
140030b1545STony Lindgren 		omap_writew(0, CF_CONTROL);
141f74e48a5SDavid Brownell 
142f74e48a5SDavid Brownell 	pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
143f74e48a5SDavid Brownell 		driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
144f74e48a5SDavid Brownell 
145f74e48a5SDavid Brownell 	return 0;
146f74e48a5SDavid Brownell }
147f74e48a5SDavid Brownell 
omap_cf_ss_suspend(struct pcmcia_socket * s)148f74e48a5SDavid Brownell static int omap_cf_ss_suspend(struct pcmcia_socket *s)
149f74e48a5SDavid Brownell {
1502e11cb4cSHarvey Harrison 	pr_debug("%s: %s\n", driver_name, __func__);
151f74e48a5SDavid Brownell 	return omap_cf_set_socket(s, &dead_socket);
152f74e48a5SDavid Brownell }
153f74e48a5SDavid Brownell 
154f74e48a5SDavid Brownell /* regions are 2K each:  mem, attrib, io (and reserved-for-ide) */
155f74e48a5SDavid Brownell 
156f74e48a5SDavid Brownell static int
omap_cf_set_io_map(struct pcmcia_socket * s,struct pccard_io_map * io)157f74e48a5SDavid Brownell omap_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
158f74e48a5SDavid Brownell {
159f74e48a5SDavid Brownell 	struct omap_cf_socket	*cf;
160f74e48a5SDavid Brownell 
161f74e48a5SDavid Brownell 	cf = container_of(s, struct omap_cf_socket, socket);
162f74e48a5SDavid Brownell 	io->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
163f74e48a5SDavid Brownell 	io->start = cf->phys_cf + SZ_4K;
164f74e48a5SDavid Brownell 	io->stop = io->start + SZ_2K - 1;
165f74e48a5SDavid Brownell 	return 0;
166f74e48a5SDavid Brownell }
167f74e48a5SDavid Brownell 
168f74e48a5SDavid Brownell static int
omap_cf_set_mem_map(struct pcmcia_socket * s,struct pccard_mem_map * map)169f74e48a5SDavid Brownell omap_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
170f74e48a5SDavid Brownell {
171f74e48a5SDavid Brownell 	struct omap_cf_socket	*cf;
172f74e48a5SDavid Brownell 
173f74e48a5SDavid Brownell 	if (map->card_start)
174f74e48a5SDavid Brownell 		return -EINVAL;
175f74e48a5SDavid Brownell 	cf = container_of(s, struct omap_cf_socket, socket);
176f74e48a5SDavid Brownell 	map->static_start = cf->phys_cf;
177f74e48a5SDavid Brownell 	map->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
178f74e48a5SDavid Brownell 	if (map->flags & MAP_ATTRIB)
179f74e48a5SDavid Brownell 		map->static_start += SZ_2K;
180f74e48a5SDavid Brownell 	return 0;
181f74e48a5SDavid Brownell }
182f74e48a5SDavid Brownell 
183f74e48a5SDavid Brownell static struct pccard_operations omap_cf_ops = {
184f74e48a5SDavid Brownell 	.init			= omap_cf_ss_init,
185f74e48a5SDavid Brownell 	.suspend		= omap_cf_ss_suspend,
186f74e48a5SDavid Brownell 	.get_status		= omap_cf_get_status,
187f74e48a5SDavid Brownell 	.set_socket		= omap_cf_set_socket,
188f74e48a5SDavid Brownell 	.set_io_map		= omap_cf_set_io_map,
189f74e48a5SDavid Brownell 	.set_mem_map		= omap_cf_set_mem_map,
190f74e48a5SDavid Brownell };
191f74e48a5SDavid Brownell 
192f74e48a5SDavid Brownell /*--------------------------------------------------------------------------*/
193f74e48a5SDavid Brownell 
194f74e48a5SDavid Brownell /*
195f74e48a5SDavid Brownell  * NOTE:  right now the only board-specific platform_data is
196f74e48a5SDavid Brownell  * "what chipselect is used".  Boards could want more.
197f74e48a5SDavid Brownell  */
198f74e48a5SDavid Brownell 
omap_cf_probe(struct platform_device * pdev)199b6d2cccbSDavid Brownell static int __init omap_cf_probe(struct platform_device *pdev)
200f74e48a5SDavid Brownell {
201f74e48a5SDavid Brownell 	unsigned		seg;
202f74e48a5SDavid Brownell 	struct omap_cf_socket	*cf;
203f74e48a5SDavid Brownell 	int			irq;
204f74e48a5SDavid Brownell 	int			status;
205d87d44f7SArnd Bergmann 	struct resource		*res;
206df99e7bbSArnd Bergmann 	struct resource		iospace = DEFINE_RES_IO(SZ_64, SZ_4K);
207f74e48a5SDavid Brownell 
208b6d2cccbSDavid Brownell 	seg = (int) pdev->dev.platform_data;
209f74e48a5SDavid Brownell 	if (seg == 0 || seg > 3)
210f74e48a5SDavid Brownell 		return -ENODEV;
211f74e48a5SDavid Brownell 
212f74e48a5SDavid Brownell 	/* either CFLASH.IREQ (INT_1610_CF) or some GPIO */
213f74e48a5SDavid Brownell 	irq = platform_get_irq(pdev, 0);
21448944738SDavid Vrabel 	if (irq < 0)
215f74e48a5SDavid Brownell 		return -EINVAL;
216f74e48a5SDavid Brownell 
217d87d44f7SArnd Bergmann 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218d87d44f7SArnd Bergmann 
219cd861280SRobert P. J. Day 	cf = kzalloc(sizeof *cf, GFP_KERNEL);
220f74e48a5SDavid Brownell 	if (!cf)
221f74e48a5SDavid Brownell 		return -ENOMEM;
22241760d0eSKees Cook 	timer_setup(&cf->timer, omap_cf_timer, 0);
223f74e48a5SDavid Brownell 
224f74e48a5SDavid Brownell 	cf->pdev = pdev;
225b6d2cccbSDavid Brownell 	platform_set_drvdata(pdev, cf);
226f74e48a5SDavid Brownell 
227f74e48a5SDavid Brownell 	/* this primarily just shuts up irq handling noise */
228dace1453SThomas Gleixner 	status = request_irq(irq, omap_cf_irq, IRQF_SHARED,
229f74e48a5SDavid Brownell 			driver_name, cf);
230f74e48a5SDavid Brownell 	if (status < 0)
231f74e48a5SDavid Brownell 		goto fail0;
232f74e48a5SDavid Brownell 	cf->irq = irq;
233f74e48a5SDavid Brownell 	cf->socket.pci_irq = irq;
234d87d44f7SArnd Bergmann 	cf->phys_cf = res->start;
235f74e48a5SDavid Brownell 
236f74e48a5SDavid Brownell 	/* pcmcia layer only remaps "real" memory */
237df99e7bbSArnd Bergmann 	cf->socket.io_offset = iospace.start;
238df99e7bbSArnd Bergmann 	status = pci_remap_iospace(&iospace, cf->phys_cf + SZ_4K);
239df99e7bbSArnd Bergmann 	if (status) {
24070d3a462SWang ShaoBo 		status = -ENOMEM;
241f74e48a5SDavid Brownell 		goto fail1;
24270d3a462SWang ShaoBo 	}
243f74e48a5SDavid Brownell 
24470d3a462SWang ShaoBo 	if (!request_mem_region(cf->phys_cf, SZ_8K, driver_name)) {
24570d3a462SWang ShaoBo 		status = -ENXIO;
246f74e48a5SDavid Brownell 		goto fail1;
24770d3a462SWang ShaoBo 	}
248f74e48a5SDavid Brownell 
249f74e48a5SDavid Brownell 	/* NOTE:  CF conflicts with MMC1 */
250f74e48a5SDavid Brownell 	omap_cfg_reg(W11_1610_CF_CD1);
251f74e48a5SDavid Brownell 	omap_cfg_reg(P11_1610_CF_CD2);
252f74e48a5SDavid Brownell 	omap_cfg_reg(R11_1610_CF_IOIS16);
253f74e48a5SDavid Brownell 	omap_cfg_reg(V10_1610_CF_IREQ);
254f74e48a5SDavid Brownell 	omap_cfg_reg(W10_1610_CF_RESET);
255f74e48a5SDavid Brownell 
256030b1545STony Lindgren 	omap_writew(~(1 << seg), CF_CFG);
257f74e48a5SDavid Brownell 
258f74e48a5SDavid Brownell 	pr_info("%s: cs%d on irq %d\n", driver_name, seg, irq);
259f74e48a5SDavid Brownell 
260f74e48a5SDavid Brownell 	/* CF uses armxor_ck, which is "always" available */
261f74e48a5SDavid Brownell 
262f74e48a5SDavid Brownell 	pr_debug("%s: sts %04x cfg %04x control %04x %s\n", driver_name,
263030b1545STony Lindgren 		omap_readw(CF_STATUS), omap_readw(CF_CFG),
264030b1545STony Lindgren 		omap_readw(CF_CONTROL),
265f74e48a5SDavid Brownell 		omap_cf_present() ? "present" : "(not present)");
266f74e48a5SDavid Brownell 
267f74e48a5SDavid Brownell 	cf->socket.owner = THIS_MODULE;
268b6d2cccbSDavid Brownell 	cf->socket.dev.parent = &pdev->dev;
269f74e48a5SDavid Brownell 	cf->socket.ops = &omap_cf_ops;
270f74e48a5SDavid Brownell 	cf->socket.resource_ops = &pccard_static_ops;
271f74e48a5SDavid Brownell 	cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
272f74e48a5SDavid Brownell 				| SS_CAP_MEM_ALIGN;
273f74e48a5SDavid Brownell 	cf->socket.map_size = SZ_2K;
274dcb9c392SDavid Brownell 	cf->socket.io[0].res = &cf->iomem;
275f74e48a5SDavid Brownell 
276f74e48a5SDavid Brownell 	status = pcmcia_register_socket(&cf->socket);
277f74e48a5SDavid Brownell 	if (status < 0)
278f74e48a5SDavid Brownell 		goto fail2;
279f74e48a5SDavid Brownell 
280f74e48a5SDavid Brownell 	cf->active = 1;
281f74e48a5SDavid Brownell 	mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
282f74e48a5SDavid Brownell 	return 0;
283f74e48a5SDavid Brownell 
284f74e48a5SDavid Brownell fail2:
285f74e48a5SDavid Brownell 	release_mem_region(cf->phys_cf, SZ_8K);
286f74e48a5SDavid Brownell fail1:
287f74e48a5SDavid Brownell 	free_irq(irq, cf);
288f74e48a5SDavid Brownell fail0:
289f74e48a5SDavid Brownell 	kfree(cf);
290f74e48a5SDavid Brownell 	return status;
291f74e48a5SDavid Brownell }
292f74e48a5SDavid Brownell 
omap_cf_remove(struct platform_device * pdev)293b6d2cccbSDavid Brownell static int __exit omap_cf_remove(struct platform_device *pdev)
294f74e48a5SDavid Brownell {
295b6d2cccbSDavid Brownell 	struct omap_cf_socket *cf = platform_get_drvdata(pdev);
296f74e48a5SDavid Brownell 
297f74e48a5SDavid Brownell 	cf->active = 0;
298f74e48a5SDavid Brownell 	pcmcia_unregister_socket(&cf->socket);
299*292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&cf->timer);
300f74e48a5SDavid Brownell 	release_mem_region(cf->phys_cf, SZ_8K);
301f74e48a5SDavid Brownell 	free_irq(cf->irq, cf);
302f74e48a5SDavid Brownell 	kfree(cf);
303f74e48a5SDavid Brownell 	return 0;
304f74e48a5SDavid Brownell }
305f74e48a5SDavid Brownell 
306b6d2cccbSDavid Brownell static struct platform_driver omap_cf_driver = {
307b6d2cccbSDavid Brownell 	.driver = {
3087c8c5673SCorentin Labbe 		.name	= driver_name,
309b6d2cccbSDavid Brownell 	},
310b6d2cccbSDavid Brownell 	.remove		= __exit_p(omap_cf_remove),
311f74e48a5SDavid Brownell };
312f74e48a5SDavid Brownell 
omap_cf_init(void)313f74e48a5SDavid Brownell static int __init omap_cf_init(void)
314f74e48a5SDavid Brownell {
315f74e48a5SDavid Brownell 	if (cpu_is_omap16xx())
316b6d2cccbSDavid Brownell 		return platform_driver_probe(&omap_cf_driver, omap_cf_probe);
317dcb9c392SDavid Brownell 	return -ENODEV;
318f74e48a5SDavid Brownell }
319f74e48a5SDavid Brownell 
omap_cf_exit(void)320f74e48a5SDavid Brownell static void __exit omap_cf_exit(void)
321f74e48a5SDavid Brownell {
322f74e48a5SDavid Brownell 	if (cpu_is_omap16xx())
323b6d2cccbSDavid Brownell 		platform_driver_unregister(&omap_cf_driver);
324f74e48a5SDavid Brownell }
325f74e48a5SDavid Brownell 
326f74e48a5SDavid Brownell module_init(omap_cf_init);
327f74e48a5SDavid Brownell module_exit(omap_cf_exit);
328f74e48a5SDavid Brownell 
329f74e48a5SDavid Brownell MODULE_DESCRIPTION("OMAP CF Driver");
330f74e48a5SDavid Brownell MODULE_LICENSE("GPL");
33112c2c019SKay Sievers MODULE_ALIAS("platform:omap_cf");
332