blob: d3ef5534991e6557819cc128e2be44cb72351f3f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Brownellf74e48a2005-09-09 13:03:28 -07002/*
3 * omap_cf.c -- OMAP 16xx CompactFlash controller driver
4 *
5 * Copyright (c) 2005 David Brownell
David Brownellf74e48a2005-09-09 13:03:28 -07006 */
7
8#include <linux/module.h>
9#include <linux/kernel.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010010#include <linux/platform_device.h>
David Brownellf74e48a2005-09-09 13:03:28 -070011#include <linux/errno.h>
12#include <linux/init.h>
13#include <linux/delay.h>
14#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Brownellf74e48a2005-09-09 13:03:28 -070016
17#include <pcmcia/ss.h>
18
Russell Kinga09e64f2008-08-05 16:14:15 +010019#include <mach/hardware.h>
David Brownellf74e48a2005-09-09 13:03:28 -070020#include <asm/io.h>
Masahiro Yamada87dfb312019-05-14 15:46:51 -070021#include <linux/sizes.h>
David Brownellf74e48a2005-09-09 13:03:28 -070022
Tony Lindgren70c494c2012-09-19 10:46:56 -070023#include <mach/mux.h>
Tony Lindgren54b693d2012-10-02 13:39:28 -070024#include <mach/tc.h>
David Brownellf74e48a2005-09-09 13:03:28 -070025
26
27/* NOTE: don't expect this to support many I/O cards. The 16xx chips have
28 * hard-wired timings to support Compact Flash memory cards; they won't work
29 * with various other devices (like WLAN adapters) without some external
30 * logic to help out.
31 *
32 * NOTE: CF controller docs disagree with address space docs as to where
33 * CF_BASE really lives; this is a doc erratum.
34 */
35#define CF_BASE 0xfffe2800
36
37/* status; read after IRQ */
Tony Lindgren030b1542008-07-03 12:24:41 +030038#define CF_STATUS (CF_BASE + 0x00)
David Brownellf74e48a2005-09-09 13:03:28 -070039# define CF_STATUS_BAD_READ (1 << 2)
40# define CF_STATUS_BAD_WRITE (1 << 1)
41# define CF_STATUS_CARD_DETECT (1 << 0)
42
43/* which chipselect (CS0..CS3) is used for CF (active low) */
Tony Lindgren030b1542008-07-03 12:24:41 +030044#define CF_CFG (CF_BASE + 0x02)
David Brownellf74e48a2005-09-09 13:03:28 -070045
46/* card reset */
Tony Lindgren030b1542008-07-03 12:24:41 +030047#define CF_CONTROL (CF_BASE + 0x04)
David Brownellf74e48a2005-09-09 13:03:28 -070048# define CF_CONTROL_RESET (1 << 0)
49
Tony Lindgren030b1542008-07-03 12:24:41 +030050#define omap_cf_present() (!(omap_readw(CF_STATUS) & CF_STATUS_CARD_DETECT))
David Brownellf74e48a2005-09-09 13:03:28 -070051
52/*--------------------------------------------------------------------------*/
53
54static const char driver_name[] = "omap_cf";
55
56struct omap_cf_socket {
57 struct pcmcia_socket socket;
58
59 struct timer_list timer;
60 unsigned present:1;
61 unsigned active:1;
62
63 struct platform_device *pdev;
64 unsigned long phys_cf;
65 u_int irq;
David Brownelldcb9c392006-09-30 23:28:19 -070066 struct resource iomem;
David Brownellf74e48a2005-09-09 13:03:28 -070067};
68
69#define POLL_INTERVAL (2 * HZ)
70
David Brownellf74e48a2005-09-09 13:03:28 -070071/*--------------------------------------------------------------------------*/
72
73static int omap_cf_ss_init(struct pcmcia_socket *s)
74{
75 return 0;
76}
77
78/* the timer is primarily to kick this socket's pccardd */
Kees Cook41760d02017-10-21 12:09:17 -070079static void omap_cf_timer(struct timer_list *t)
David Brownellf74e48a2005-09-09 13:03:28 -070080{
Kees Cook41760d02017-10-21 12:09:17 -070081 struct omap_cf_socket *cf = from_timer(cf, t, timer);
David Brownellf74e48a2005-09-09 13:03:28 -070082 unsigned present = omap_cf_present();
83
84 if (present != cf->present) {
85 cf->present = present;
86 pr_debug("%s: card %s\n", driver_name,
87 present ? "present" : "gone");
88 pcmcia_parse_events(&cf->socket, SS_DETECT);
89 }
90
91 if (cf->active)
92 mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
93}
94
95/* This irq handler prevents "irqNNN: nobody cared" messages as drivers
96 * claim the card's IRQ. It may also detect some card insertions, but
97 * not removals; it can't always eliminate timer irqs.
98 */
David Howells7d12e782006-10-05 14:55:46 +010099static irqreturn_t omap_cf_irq(int irq, void *_cf)
David Brownellf74e48a2005-09-09 13:03:28 -0700100{
Kees Cook439dc052017-11-04 19:32:31 -0700101 struct omap_cf_socket *cf = (struct omap_cf_socket *)_cf;
102
103 omap_cf_timer(&cf->timer);
David Brownellf74e48a2005-09-09 13:03:28 -0700104 return IRQ_HANDLED;
105}
106
107static int omap_cf_get_status(struct pcmcia_socket *s, u_int *sp)
108{
109 if (!sp)
110 return -EINVAL;
111
David Brownelldcb9c392006-09-30 23:28:19 -0700112 /* NOTE CF is always 3VCARD */
David Brownellf74e48a2005-09-09 13:03:28 -0700113 if (omap_cf_present()) {
114 struct omap_cf_socket *cf;
115
116 *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
117 cf = container_of(s, struct omap_cf_socket, socket);
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100118 s->pcmcia_irq = 0;
David Brownelldcb9c392006-09-30 23:28:19 -0700119 s->pci_irq = cf->irq;
David Brownellf74e48a2005-09-09 13:03:28 -0700120 } else
121 *sp = 0;
122 return 0;
123}
124
125static int
126omap_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
127{
128 u16 control;
129
David Brownelldcb9c392006-09-30 23:28:19 -0700130 /* REVISIT some non-OSK boards may support power switching */
David Brownellf74e48a2005-09-09 13:03:28 -0700131 switch (s->Vcc) {
132 case 0:
133 case 33:
134 break;
135 default:
136 return -EINVAL;
137 }
138
Tony Lindgren030b1542008-07-03 12:24:41 +0300139 control = omap_readw(CF_CONTROL);
David Brownellf74e48a2005-09-09 13:03:28 -0700140 if (s->flags & SS_RESET)
Tony Lindgren030b1542008-07-03 12:24:41 +0300141 omap_writew(CF_CONTROL_RESET, CF_CONTROL);
David Brownellf74e48a2005-09-09 13:03:28 -0700142 else
Tony Lindgren030b1542008-07-03 12:24:41 +0300143 omap_writew(0, CF_CONTROL);
David Brownellf74e48a2005-09-09 13:03:28 -0700144
145 pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
146 driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
147
148 return 0;
149}
150
151static int omap_cf_ss_suspend(struct pcmcia_socket *s)
152{
Harvey Harrison2e11cb42008-05-01 04:34:54 -0700153 pr_debug("%s: %s\n", driver_name, __func__);
David Brownellf74e48a2005-09-09 13:03:28 -0700154 return omap_cf_set_socket(s, &dead_socket);
155}
156
157/* regions are 2K each: mem, attrib, io (and reserved-for-ide) */
158
159static int
160omap_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
161{
162 struct omap_cf_socket *cf;
163
164 cf = container_of(s, struct omap_cf_socket, socket);
165 io->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
166 io->start = cf->phys_cf + SZ_4K;
167 io->stop = io->start + SZ_2K - 1;
168 return 0;
169}
170
171static int
172omap_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
173{
174 struct omap_cf_socket *cf;
175
176 if (map->card_start)
177 return -EINVAL;
178 cf = container_of(s, struct omap_cf_socket, socket);
179 map->static_start = cf->phys_cf;
180 map->flags &= MAP_ACTIVE|MAP_ATTRIB|MAP_16BIT;
181 if (map->flags & MAP_ATTRIB)
182 map->static_start += SZ_2K;
183 return 0;
184}
185
186static struct pccard_operations omap_cf_ops = {
187 .init = omap_cf_ss_init,
188 .suspend = omap_cf_ss_suspend,
189 .get_status = omap_cf_get_status,
190 .set_socket = omap_cf_set_socket,
191 .set_io_map = omap_cf_set_io_map,
192 .set_mem_map = omap_cf_set_mem_map,
193};
194
195/*--------------------------------------------------------------------------*/
196
197/*
198 * NOTE: right now the only board-specific platform_data is
199 * "what chipselect is used". Boards could want more.
200 */
201
David Brownellb6d2cccb2007-04-08 16:04:02 -0700202static int __init omap_cf_probe(struct platform_device *pdev)
David Brownellf74e48a2005-09-09 13:03:28 -0700203{
204 unsigned seg;
205 struct omap_cf_socket *cf;
David Brownellf74e48a2005-09-09 13:03:28 -0700206 int irq;
207 int status;
208
David Brownellb6d2cccb2007-04-08 16:04:02 -0700209 seg = (int) pdev->dev.platform_data;
David Brownellf74e48a2005-09-09 13:03:28 -0700210 if (seg == 0 || seg > 3)
211 return -ENODEV;
212
213 /* either CFLASH.IREQ (INT_1610_CF) or some GPIO */
214 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000215 if (irq < 0)
David Brownellf74e48a2005-09-09 13:03:28 -0700216 return -EINVAL;
217
Robert P. J. Daycd861282006-12-13 00:34:52 -0800218 cf = kzalloc(sizeof *cf, GFP_KERNEL);
David Brownellf74e48a2005-09-09 13:03:28 -0700219 if (!cf)
220 return -ENOMEM;
Kees Cook41760d02017-10-21 12:09:17 -0700221 timer_setup(&cf->timer, omap_cf_timer, 0);
David Brownellf74e48a2005-09-09 13:03:28 -0700222
223 cf->pdev = pdev;
David Brownellb6d2cccb2007-04-08 16:04:02 -0700224 platform_set_drvdata(pdev, cf);
David Brownellf74e48a2005-09-09 13:03:28 -0700225
226 /* this primarily just shuts up irq handling noise */
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700227 status = request_irq(irq, omap_cf_irq, IRQF_SHARED,
David Brownellf74e48a2005-09-09 13:03:28 -0700228 driver_name, cf);
229 if (status < 0)
230 goto fail0;
231 cf->irq = irq;
232 cf->socket.pci_irq = irq;
233
234 switch (seg) {
235 /* NOTE: CS0 could be configured too ... */
236 case 1:
237 cf->phys_cf = OMAP_CS1_PHYS;
238 break;
239 case 2:
240 cf->phys_cf = OMAP_CS2_PHYS;
241 break;
242 case 3:
243 cf->phys_cf = omap_cs3_phys();
244 break;
245 default:
246 goto fail1;
247 }
David Brownelldcb9c392006-09-30 23:28:19 -0700248 cf->iomem.start = cf->phys_cf;
249 cf->iomem.end = cf->iomem.end + SZ_8K - 1;
250 cf->iomem.flags = IORESOURCE_MEM;
David Brownellf74e48a2005-09-09 13:03:28 -0700251
252 /* pcmcia layer only remaps "real" memory */
253 cf->socket.io_offset = (unsigned long)
254 ioremap(cf->phys_cf + SZ_4K, SZ_2K);
255 if (!cf->socket.io_offset)
256 goto fail1;
257
258 if (!request_mem_region(cf->phys_cf, SZ_8K, driver_name))
259 goto fail1;
260
261 /* NOTE: CF conflicts with MMC1 */
262 omap_cfg_reg(W11_1610_CF_CD1);
263 omap_cfg_reg(P11_1610_CF_CD2);
264 omap_cfg_reg(R11_1610_CF_IOIS16);
265 omap_cfg_reg(V10_1610_CF_IREQ);
266 omap_cfg_reg(W10_1610_CF_RESET);
267
Tony Lindgren030b1542008-07-03 12:24:41 +0300268 omap_writew(~(1 << seg), CF_CFG);
David Brownellf74e48a2005-09-09 13:03:28 -0700269
270 pr_info("%s: cs%d on irq %d\n", driver_name, seg, irq);
271
272 /* NOTE: better EMIFS setup might support more cards; but the
273 * TRM only shows how to affect regular flash signals, not their
274 * CF/PCMCIA variants...
275 */
276 pr_debug("%s: cs%d, previous ccs %08x acs %08x\n", driver_name,
Tony Lindgren030b1542008-07-03 12:24:41 +0300277 seg, omap_readl(EMIFS_CCS(seg)), omap_readl(EMIFS_ACS(seg)));
278 omap_writel(0x0004a1b3, EMIFS_CCS(seg)); /* synch mode 4 etc */
279 omap_writel(0x00000000, EMIFS_ACS(seg)); /* OE hold/setup */
David Brownellf74e48a2005-09-09 13:03:28 -0700280
281 /* CF uses armxor_ck, which is "always" available */
282
283 pr_debug("%s: sts %04x cfg %04x control %04x %s\n", driver_name,
Tony Lindgren030b1542008-07-03 12:24:41 +0300284 omap_readw(CF_STATUS), omap_readw(CF_CFG),
285 omap_readw(CF_CONTROL),
David Brownellf74e48a2005-09-09 13:03:28 -0700286 omap_cf_present() ? "present" : "(not present)");
287
288 cf->socket.owner = THIS_MODULE;
David Brownellb6d2cccb2007-04-08 16:04:02 -0700289 cf->socket.dev.parent = &pdev->dev;
David Brownellf74e48a2005-09-09 13:03:28 -0700290 cf->socket.ops = &omap_cf_ops;
291 cf->socket.resource_ops = &pccard_static_ops;
292 cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
293 | SS_CAP_MEM_ALIGN;
294 cf->socket.map_size = SZ_2K;
David Brownelldcb9c392006-09-30 23:28:19 -0700295 cf->socket.io[0].res = &cf->iomem;
David Brownellf74e48a2005-09-09 13:03:28 -0700296
297 status = pcmcia_register_socket(&cf->socket);
298 if (status < 0)
299 goto fail2;
300
301 cf->active = 1;
302 mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
303 return 0;
304
305fail2:
David Brownellf74e48a2005-09-09 13:03:28 -0700306 release_mem_region(cf->phys_cf, SZ_8K);
307fail1:
Amol Lad3efa9972006-10-20 14:44:18 -0700308 if (cf->socket.io_offset)
309 iounmap((void __iomem *) cf->socket.io_offset);
David Brownellf74e48a2005-09-09 13:03:28 -0700310 free_irq(irq, cf);
311fail0:
312 kfree(cf);
313 return status;
314}
315
David Brownellb6d2cccb2007-04-08 16:04:02 -0700316static int __exit omap_cf_remove(struct platform_device *pdev)
David Brownellf74e48a2005-09-09 13:03:28 -0700317{
David Brownellb6d2cccb2007-04-08 16:04:02 -0700318 struct omap_cf_socket *cf = platform_get_drvdata(pdev);
David Brownellf74e48a2005-09-09 13:03:28 -0700319
320 cf->active = 0;
321 pcmcia_unregister_socket(&cf->socket);
322 del_timer_sync(&cf->timer);
323 iounmap((void __iomem *) cf->socket.io_offset);
324 release_mem_region(cf->phys_cf, SZ_8K);
325 free_irq(cf->irq, cf);
326 kfree(cf);
327 return 0;
328}
329
David Brownellb6d2cccb2007-04-08 16:04:02 -0700330static struct platform_driver omap_cf_driver = {
331 .driver = {
Corentin Labbe7c8c5672020-02-18 20:07:16 +0000332 .name = driver_name,
David Brownellb6d2cccb2007-04-08 16:04:02 -0700333 },
334 .remove = __exit_p(omap_cf_remove),
David Brownellf74e48a2005-09-09 13:03:28 -0700335};
336
337static int __init omap_cf_init(void)
338{
339 if (cpu_is_omap16xx())
David Brownellb6d2cccb2007-04-08 16:04:02 -0700340 return platform_driver_probe(&omap_cf_driver, omap_cf_probe);
David Brownelldcb9c392006-09-30 23:28:19 -0700341 return -ENODEV;
David Brownellf74e48a2005-09-09 13:03:28 -0700342}
343
344static void __exit omap_cf_exit(void)
345{
346 if (cpu_is_omap16xx())
David Brownellb6d2cccb2007-04-08 16:04:02 -0700347 platform_driver_unregister(&omap_cf_driver);
David Brownellf74e48a2005-09-09 13:03:28 -0700348}
349
350module_init(omap_cf_init);
351module_exit(omap_cf_exit);
352
353MODULE_DESCRIPTION("OMAP CF Driver");
354MODULE_LICENSE("GPL");
Kay Sievers12c2c012008-04-15 14:34:34 -0700355MODULE_ALIAS("platform:omap_cf");