1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * omap_cf.c -- OMAP 16xx CompactFlash controller driver
5 * Copyright (c) 2005 David Brownell
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/platform_device.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
15 #include <linux/slab.h>
17 #include <pcmcia/ss.h>
20 #include <linux/sizes.h>
22 #include <linux/soc/ti/omap1-io.h>
23 #include <linux/soc/ti/omap1-soc.h>
24 #include <linux/soc/ti/omap1-mux.h>
26 /* NOTE: don't expect this to support many I/O cards. The 16xx chips have
27 * hard-wired timings to support Compact Flash memory cards; they won't work
28 * with various other devices (like WLAN adapters) without some external
31 * NOTE: CF controller docs disagree with address space docs as to where
32 * CF_BASE really lives; this is a doc erratum.
34 #define CF_BASE 0xfffe2800
36 /* status; read after IRQ */
37 #define CF_STATUS (CF_BASE + 0x00)
38 # define CF_STATUS_BAD_READ (1 << 2)
39 # define CF_STATUS_BAD_WRITE (1 << 1)
40 # define CF_STATUS_CARD_DETECT (1 << 0)
42 /* which chipselect (CS0..CS3) is used for CF (active low) */
43 #define CF_CFG (CF_BASE + 0x02)
46 #define CF_CONTROL (CF_BASE + 0x04)
47 # define CF_CONTROL_RESET (1 << 0)
49 #define omap_cf_present() (!(omap_readw(CF_STATUS) & CF_STATUS_CARD_DETECT))
51 /*--------------------------------------------------------------------------*/
53 static const char driver_name
[] = "omap_cf";
55 struct omap_cf_socket
{
56 struct pcmcia_socket socket
;
58 struct timer_list timer
;
62 struct platform_device
*pdev
;
63 unsigned long phys_cf
;
65 struct resource iomem
;
68 #define POLL_INTERVAL (2 * HZ)
70 /*--------------------------------------------------------------------------*/
72 static int omap_cf_ss_init(struct pcmcia_socket
*s
)
77 /* the timer is primarily to kick this socket's pccardd */
78 static void omap_cf_timer(struct timer_list
*t
)
80 struct omap_cf_socket
*cf
= from_timer(cf
, t
, timer
);
81 unsigned present
= omap_cf_present();
83 if (present
!= cf
->present
) {
84 cf
->present
= present
;
85 pr_debug("%s: card %s\n", driver_name
,
86 present
? "present" : "gone");
87 pcmcia_parse_events(&cf
->socket
, SS_DETECT
);
91 mod_timer(&cf
->timer
, jiffies
+ POLL_INTERVAL
);
94 /* This irq handler prevents "irqNNN: nobody cared" messages as drivers
95 * claim the card's IRQ. It may also detect some card insertions, but
96 * not removals; it can't always eliminate timer irqs.
98 static irqreturn_t
omap_cf_irq(int irq
, void *_cf
)
100 struct omap_cf_socket
*cf
= (struct omap_cf_socket
*)_cf
;
102 omap_cf_timer(&cf
->timer
);
106 static int omap_cf_get_status(struct pcmcia_socket
*s
, u_int
*sp
)
111 /* NOTE CF is always 3VCARD */
112 if (omap_cf_present()) {
113 struct omap_cf_socket
*cf
;
115 *sp
= SS_READY
| SS_DETECT
| SS_POWERON
| SS_3VCARD
;
116 cf
= container_of(s
, struct omap_cf_socket
, socket
);
118 s
->pci_irq
= cf
->irq
;
125 omap_cf_set_socket(struct pcmcia_socket
*sock
, struct socket_state_t
*s
)
127 /* REVISIT some non-OSK boards may support power switching */
136 omap_readw(CF_CONTROL
);
137 if (s
->flags
& SS_RESET
)
138 omap_writew(CF_CONTROL_RESET
, CF_CONTROL
);
140 omap_writew(0, CF_CONTROL
);
142 pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
143 driver_name
, s
->Vcc
, s
->io_irq
, s
->flags
, s
->csc_mask
);
148 static int omap_cf_ss_suspend(struct pcmcia_socket
*s
)
150 pr_debug("%s: %s\n", driver_name
, __func__
);
151 return omap_cf_set_socket(s
, &dead_socket
);
154 /* regions are 2K each: mem, attrib, io (and reserved-for-ide) */
157 omap_cf_set_io_map(struct pcmcia_socket
*s
, struct pccard_io_map
*io
)
159 struct omap_cf_socket
*cf
;
161 cf
= container_of(s
, struct omap_cf_socket
, socket
);
162 io
->flags
&= MAP_ACTIVE
|MAP_ATTRIB
|MAP_16BIT
;
163 io
->start
= cf
->phys_cf
+ SZ_4K
;
164 io
->stop
= io
->start
+ SZ_2K
- 1;
169 omap_cf_set_mem_map(struct pcmcia_socket
*s
, struct pccard_mem_map
*map
)
171 struct omap_cf_socket
*cf
;
175 cf
= container_of(s
, struct omap_cf_socket
, socket
);
176 map
->static_start
= cf
->phys_cf
;
177 map
->flags
&= MAP_ACTIVE
|MAP_ATTRIB
|MAP_16BIT
;
178 if (map
->flags
& MAP_ATTRIB
)
179 map
->static_start
+= SZ_2K
;
183 static struct pccard_operations omap_cf_ops
= {
184 .init
= omap_cf_ss_init
,
185 .suspend
= omap_cf_ss_suspend
,
186 .get_status
= omap_cf_get_status
,
187 .set_socket
= omap_cf_set_socket
,
188 .set_io_map
= omap_cf_set_io_map
,
189 .set_mem_map
= omap_cf_set_mem_map
,
192 /*--------------------------------------------------------------------------*/
195 * NOTE: right now the only board-specific platform_data is
196 * "what chipselect is used". Boards could want more.
199 static int __init
omap_cf_probe(struct platform_device
*pdev
)
202 struct omap_cf_socket
*cf
;
205 struct resource
*res
;
206 struct resource iospace
= DEFINE_RES_IO(SZ_64
, SZ_4K
);
208 seg
= (int) pdev
->dev
.platform_data
;
209 if (seg
== 0 || seg
> 3)
212 /* either CFLASH.IREQ (INT_1610_CF) or some GPIO */
213 irq
= platform_get_irq(pdev
, 0);
217 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
219 cf
= kzalloc(sizeof *cf
, GFP_KERNEL
);
222 timer_setup(&cf
->timer
, omap_cf_timer
, 0);
225 platform_set_drvdata(pdev
, cf
);
227 /* this primarily just shuts up irq handling noise */
228 status
= request_irq(irq
, omap_cf_irq
, IRQF_SHARED
,
233 cf
->socket
.pci_irq
= irq
;
234 cf
->phys_cf
= res
->start
;
236 /* pcmcia layer only remaps "real" memory */
237 cf
->socket
.io_offset
= iospace
.start
;
238 status
= pci_remap_iospace(&iospace
, cf
->phys_cf
+ SZ_4K
);
244 if (!request_mem_region(cf
->phys_cf
, SZ_8K
, driver_name
)) {
249 /* NOTE: CF conflicts with MMC1 */
250 omap_cfg_reg(W11_1610_CF_CD1
);
251 omap_cfg_reg(P11_1610_CF_CD2
);
252 omap_cfg_reg(R11_1610_CF_IOIS16
);
253 omap_cfg_reg(V10_1610_CF_IREQ
);
254 omap_cfg_reg(W10_1610_CF_RESET
);
256 omap_writew(~(1 << seg
), CF_CFG
);
258 pr_info("%s: cs%d on irq %d\n", driver_name
, seg
, irq
);
260 /* CF uses armxor_ck, which is "always" available */
262 pr_debug("%s: sts %04x cfg %04x control %04x %s\n", driver_name
,
263 omap_readw(CF_STATUS
), omap_readw(CF_CFG
),
264 omap_readw(CF_CONTROL
),
265 omap_cf_present() ? "present" : "(not present)");
267 cf
->socket
.owner
= THIS_MODULE
;
268 cf
->socket
.dev
.parent
= &pdev
->dev
;
269 cf
->socket
.ops
= &omap_cf_ops
;
270 cf
->socket
.resource_ops
= &pccard_static_ops
;
271 cf
->socket
.features
= SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
273 cf
->socket
.map_size
= SZ_2K
;
274 cf
->socket
.io
[0].res
= &cf
->iomem
;
276 status
= pcmcia_register_socket(&cf
->socket
);
281 mod_timer(&cf
->timer
, jiffies
+ POLL_INTERVAL
);
285 release_mem_region(cf
->phys_cf
, SZ_8K
);
293 static void __exit
omap_cf_remove(struct platform_device
*pdev
)
295 struct omap_cf_socket
*cf
= platform_get_drvdata(pdev
);
298 pcmcia_unregister_socket(&cf
->socket
);
299 timer_shutdown_sync(&cf
->timer
);
300 release_mem_region(cf
->phys_cf
, SZ_8K
);
301 free_irq(cf
->irq
, cf
);
305 static struct platform_driver omap_cf_driver
= {
309 .remove
= __exit_p(omap_cf_remove
),
312 static int __init
omap_cf_init(void)
314 if (cpu_is_omap16xx())
315 return platform_driver_probe(&omap_cf_driver
, omap_cf_probe
);
319 static void __exit
omap_cf_exit(void)
321 if (cpu_is_omap16xx())
322 platform_driver_unregister(&omap_cf_driver
);
325 module_init(omap_cf_init
);
326 module_exit(omap_cf_exit
);
328 MODULE_DESCRIPTION("OMAP CF Driver");
329 MODULE_LICENSE("GPL");
330 MODULE_ALIAS("platform:omap_cf");