| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* omap_cf.c -- OMAP 16xx CompactFlash controller driver |
| 4 |
* |
| 5 |
* Copyright (c) 2005 David Brownell |
| 6 |
*/ |
| 7 |
|
| 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> |
| 16 |
|
| 17 |
#include <pcmcia/ss.h> |
| 18 |
|
| 19 |
#include <asm/io.h> |
| 20 |
#include <linux/sizes.h> |
| 21 |
|
| 22 |
#include <linux/soc/ti/omap1-io.h> |
| 23 |
#include <linux/soc/ti/omap1-soc.h> |
| 24 |
#include <linux/soc/ti/omap1-mux.h> |
| 25 |
|
| 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 |
| 29 |
* logic to help out. |
| 30 |
* |
| 31 |
* NOTE: CF controller docs disagree with address space docs as to where |
| 32 |
* CF_BASE really lives; this is a doc erratum. |
| 33 |
*/ |
| 34 |
#define CF_BASE 0xfffe2800 |
| 35 |
|
| 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) |
| 41 |
|
| 42 |
/* which chipselect (CS0..CS3) is used for CF (active low) */ |
| 43 |
#define CF_CFG (CF_BASE + 0x02) |
| 44 |
|
| 45 |
/* card reset */ |
| 46 |
#define CF_CONTROL (CF_BASE + 0x04) |
| 47 |
# define CF_CONTROL_RESET (1 << 0) |
| 48 |
|
| 49 |
#define omap_cf_present() (!(omap_readw(CF_STATUS) & CF_STATUS_CARD_DETECT)) |
| 50 |
|
| 51 |
/*--------------------------------------------------------------------------*/ |
| 52 |
|
| 53 |
static const char driver_name[] = "omap_cf"; |
| 54 |
|
| 55 |
struct omap_cf_socket { |
| 56 |
struct pcmcia_socket socket; |
| 57 |
|
| 58 |
struct timer_list timer; |
| 59 |
unsigned present:1; |
| 60 |
unsigned active:1; |
| 61 |
|
| 62 |
struct platform_device *pdev; |
| 63 |
unsigned long phys_cf; |
| 64 |
u_int irq; |
| 65 |
struct resource iomem; |
| 66 |
}; |
| 67 |
|
| 68 |
#define POLL_INTERVAL (2 * HZ) |
| 69 |
|
| 70 |
/*--------------------------------------------------------------------------*/ |
| 71 |
|
| 72 |
static int omap_cf_ss_init(struct pcmcia_socket *s) |
| 73 |
{ |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
/* the timer is primarily to kick this socket's pccardd */ |
| 78 |
static void omap_cf_timer(struct timer_list *t) |
| 79 |
{ |
| 80 |
struct omap_cf_socket *cf = timer_container_of(cf, t, timer); |
| 81 |
unsigned present = omap_cf_present(); |
| 82 |
|
| 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); |
| 88 |
} |
| 89 |
|
| 90 |
if (cf->active) |
| 91 |
mod_timer(&cf->timer, jiffies + POLL_INTERVAL); |
| 92 |
} |
| 93 |
|
| 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. |
| 97 |
*/ |
| 98 |
static irqreturn_t omap_cf_irq(int irq, void *_cf) |
| 99 |
{ |
| 100 |
struct omap_cf_socket *cf = (struct omap_cf_socket *)_cf; |
| 101 |
|
| 102 |
omap_cf_timer(&cf->timer); |
| 103 |
return IRQ_HANDLED; |
| 104 |
} |
| 105 |
|
| 106 |
static int omap_cf_get_status(struct pcmcia_socket *s, u_int *sp) |
| 107 |
{ |
| 108 |
if (!sp) |
| 109 |
return -EINVAL; |
| 110 |
|
| 111 |
/* NOTE CF is always 3VCARD */ |
| 112 |
if (omap_cf_present()) { |
| 113 |
struct omap_cf_socket *cf; |
| 114 |
|
| 115 |
*sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD; |
| 116 |
cf = container_of(s, struct omap_cf_socket, socket); |
| 117 |
s->pcmcia_irq = 0; |
| 118 |
s->pci_irq = cf->irq; |
| 119 |
} else |
| 120 |
*sp = 0; |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
static int |
| 125 |
omap_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s) |
| 126 |
{ |
| 127 |
/* REVISIT some non-OSK boards may support power switching */ |
| 128 |
switch (s->Vcc) { |
| 129 |
case 0: |
| 130 |
case 33: |
| 131 |
break; |
| 132 |
default: |
| 133 |
return -EINVAL; |
| 134 |
} |
| 135 |
|
| 136 |
omap_readw(CF_CONTROL); |
| 137 |
if (s->flags & SS_RESET) |
| 138 |
omap_writew(CF_CONTROL_RESET, CF_CONTROL); |
| 139 |
else |
| 140 |
omap_writew(0, CF_CONTROL); |
| 141 |
|
| 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); |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int omap_cf_ss_suspend(struct pcmcia_socket *s) |
| 149 |
{ |
| 150 |
pr_debug("%s: %s\n", driver_name, __func__); |
| 151 |
return omap_cf_set_socket(s, &dead_socket); |
| 152 |
} |
| 153 |
|
| 154 |
/* regions are 2K each: mem, attrib, io (and reserved-for-ide) */ |
| 155 |
|
| 156 |
static int |
| 157 |
omap_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io) |
| 158 |
{ |
| 159 |
struct omap_cf_socket *cf; |
| 160 |
|
| 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; |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
static int |
| 169 |
omap_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map) |
| 170 |
{ |
| 171 |
struct omap_cf_socket *cf; |
| 172 |
|
| 173 |
if (map->card_start) |
| 174 |
return -EINVAL; |
| 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; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 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, |
| 190 |
}; |
| 191 |
|
| 192 |
/*--------------------------------------------------------------------------*/ |
| 193 |
|
| 194 |
/* |
| 195 |
* NOTE: right now the only board-specific platform_data is |
| 196 |
* "what chipselect is used". Boards could want more. |
| 197 |
*/ |
| 198 |
|
| 199 |
static int __init omap_cf_probe(struct platform_device *pdev) |
| 200 |
{ |
| 201 |
unsigned seg; |
| 202 |
struct omap_cf_socket *cf; |
| 203 |
int irq; |
| 204 |
int status; |
| 205 |
struct resource *res; |
| 206 |
struct resource iospace = DEFINE_RES_IO(SZ_64, SZ_4K); |
| 207 |
|
| 208 |
seg = (int) pdev->dev.platform_data; |
| 209 |
if (seg == 0 || seg > 3) |
| 210 |
return -ENODEV; |
| 211 |
|
| 212 |
/* either CFLASH.IREQ (INT_1610_CF) or some GPIO */ |
| 213 |
irq = platform_get_irq(pdev, 0); |
| 214 |
if (irq < 0) |
| 215 |
return -EINVAL; |
| 216 |
|
| 217 |
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 218 |
if (!res) |
| 219 |
return -EINVAL; |
| 220 |
|
| 221 |
cf = kzalloc_obj(*cf); |
| 222 |
if (!cf) |
| 223 |
return -ENOMEM; |
| 224 |
timer_setup(&cf->timer, omap_cf_timer, 0); |
| 225 |
|
| 226 |
cf->pdev = pdev; |
| 227 |
platform_set_drvdata(pdev, cf); |
| 228 |
|
| 229 |
/* this primarily just shuts up irq handling noise */ |
| 230 |
status = request_irq(irq, omap_cf_irq, IRQF_SHARED, |
| 231 |
driver_name, cf); |
| 232 |
if (status < 0) |
| 233 |
goto fail0; |
| 234 |
cf->irq = irq; |
| 235 |
cf->socket.pci_irq = irq; |
| 236 |
cf->phys_cf = res->start; |
| 237 |
|
| 238 |
/* pcmcia layer only remaps "real" memory */ |
| 239 |
cf->socket.io_offset = iospace.start; |
| 240 |
status = pci_remap_iospace(&iospace, cf->phys_cf + SZ_4K); |
| 241 |
if (status) { |
| 242 |
status = -ENOMEM; |
| 243 |
goto fail1; |
| 244 |
} |
| 245 |
|
| 246 |
if (!request_mem_region(cf->phys_cf, SZ_8K, driver_name)) { |
| 247 |
status = -ENXIO; |
| 248 |
goto fail1; |
| 249 |
} |
| 250 |
|
| 251 |
/* NOTE: CF conflicts with MMC1 */ |
| 252 |
omap_cfg_reg(W11_1610_CF_CD1); |
| 253 |
omap_cfg_reg(P11_1610_CF_CD2); |
| 254 |
omap_cfg_reg(R11_1610_CF_IOIS16); |
| 255 |
omap_cfg_reg(V10_1610_CF_IREQ); |
| 256 |
omap_cfg_reg(W10_1610_CF_RESET); |
| 257 |
|
| 258 |
omap_writew(~(1 << seg), CF_CFG); |
| 259 |
|
| 260 |
pr_info("%s: cs%d on irq %d\n", driver_name, seg, irq); |
| 261 |
|
| 262 |
/* CF uses armxor_ck, which is "always" available */ |
| 263 |
|
| 264 |
pr_debug("%s: sts %04x cfg %04x control %04x %s\n", driver_name, |
| 265 |
omap_readw(CF_STATUS), omap_readw(CF_CFG), |
| 266 |
omap_readw(CF_CONTROL), |
| 267 |
omap_cf_present() ? "present" : "(not present)"); |
| 268 |
|
| 269 |
cf->socket.owner = THIS_MODULE; |
| 270 |
cf->socket.dev.parent = &pdev->dev; |
| 271 |
cf->socket.ops = &omap_cf_ops; |
| 272 |
cf->socket.resource_ops = &pccard_static_ops; |
| 273 |
cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP |
| 274 |
| SS_CAP_MEM_ALIGN; |
| 275 |
cf->socket.map_size = SZ_2K; |
| 276 |
cf->socket.io[0].res = &cf->iomem; |
| 277 |
|
| 278 |
status = pcmcia_register_socket(&cf->socket); |
| 279 |
if (status < 0) |
| 280 |
goto fail2; |
| 281 |
|
| 282 |
cf->active = 1; |
| 283 |
mod_timer(&cf->timer, jiffies + POLL_INTERVAL); |
| 284 |
return 0; |
| 285 |
|
| 286 |
fail2: |
| 287 |
release_mem_region(cf->phys_cf, SZ_8K); |
| 288 |
fail1: |
| 289 |
free_irq(irq, cf); |
| 290 |
fail0: |
| 291 |
kfree(cf); |
| 292 |
return status; |
| 293 |
} |
| 294 |
|
| 295 |
static void __exit omap_cf_remove(struct platform_device *pdev) |
| 296 |
{ |
| 297 |
struct omap_cf_socket *cf = platform_get_drvdata(pdev); |
| 298 |
|
| 299 |
cf->active = 0; |
| 300 |
pcmcia_unregister_socket(&cf->socket); |
| 301 |
timer_shutdown_sync(&cf->timer); |
| 302 |
release_mem_region(cf->phys_cf, SZ_8K); |
| 303 |
free_irq(cf->irq, cf); |
| 304 |
kfree(cf); |
| 305 |
} |
| 306 |
|
| 307 |
/* |
| 308 |
* omap_cf_remove() lives in .exit.text. For drivers registered via |
| 309 |
* platform_driver_probe() this is ok because they cannot get unbound at |
| 310 |
* runtime. So mark the driver struct with __refdata to prevent modpost |
| 311 |
* triggering a section mismatch warning. |
| 312 |
*/ |
| 313 |
static struct platform_driver omap_cf_driver __refdata = { |
| 314 |
.driver = { |
| 315 |
.name = driver_name, |
| 316 |
}, |
| 317 |
.remove = __exit_p(omap_cf_remove), |
| 318 |
}; |
| 319 |
|
| 320 |
static int __init omap_cf_init(void) |
| 321 |
{ |
| 322 |
if (cpu_is_omap16xx()) |
| 323 |
return platform_driver_probe(&omap_cf_driver, omap_cf_probe); |
| 324 |
return -ENODEV; |
| 325 |
} |
| 326 |
|
| 327 |
static void __exit omap_cf_exit(void) |
| 328 |
{ |
| 329 |
if (cpu_is_omap16xx()) |
| 330 |
platform_driver_unregister(&omap_cf_driver); |
| 331 |
} |
| 332 |
|
| 333 |
module_init(omap_cf_init); |
| 334 |
module_exit(omap_cf_exit); |
| 335 |
|
| 336 |
MODULE_DESCRIPTION("OMAP CF Driver"); |
| 337 |
MODULE_LICENSE("GPL"); |
| 338 |
MODULE_ALIAS("platform:omap_cf"); |