Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*====================================================================== |
| 2 | |
| 3 | Common support code for the PCMCIA control functionality of |
| 4 | integrated SOCs like the SA-11x0 and PXA2xx microprocessors. |
| 5 | |
| 6 | The contents of this file are subject to the Mozilla Public |
| 7 | License Version 1.1 (the "License"); you may not use this file |
| 8 | except in compliance with the License. You may obtain a copy of |
| 9 | the License at http://www.mozilla.org/MPL/ |
| 10 | |
| 11 | Software distributed under the License is distributed on an "AS |
| 12 | IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 13 | implied. See the License for the specific language governing |
| 14 | rights and limitations under the License. |
| 15 | |
| 16 | The initial developer of the original code is John G. Dorsey |
| 17 | <john+@cs.cmu.edu>. Portions created by John G. Dorsey are |
| 18 | Copyright (C) 1999 John G. Dorsey. All Rights Reserved. |
| 19 | |
| 20 | Alternatively, the contents of this file may be used under the |
| 21 | terms of the GNU Public License version 2 (the "GPL"), in which |
| 22 | case the provisions of the GPL are applicable instead of the |
| 23 | above. If you wish to allow the use of your version of this file |
| 24 | only under the terms of the GPL and not to allow others to use |
| 25 | your version of this file under the MPL, indicate your decision |
| 26 | by deleting the provisions above and replace them with the notice |
| 27 | and other provisions required by the GPL. If you do not delete |
| 28 | the provisions above, a recipient may use your version of this |
| 29 | file under either the MPL or the GPL. |
| 30 | |
| 31 | ======================================================================*/ |
| 32 | |
| 33 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 34 | #include <linux/cpufreq.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/io.h> |
| 38 | #include <linux/irq.h> |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/moduleparam.h> |
Andrew Morton | 23d077e | 2008-05-01 04:34:54 -0700 | [diff] [blame] | 43 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 45 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 47 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/system.h> |
| 49 | |
| 50 | #include "soc_common.h" |
| 51 | |
Dominik Brodowski | 7d16b65 | 2008-08-02 21:02:01 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_PCMCIA_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | static int pc_debug; |
| 55 | module_param(pc_debug, int, 0644); |
| 56 | |
| 57 | void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func, |
| 58 | int lvl, const char *fmt, ...) |
| 59 | { |
Joe Perches | 106665d | 2010-11-09 17:14:01 -0800 | [diff] [blame] | 60 | struct va_format vaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | va_list args; |
| 62 | if (pc_debug > lvl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | va_start(args, fmt); |
Joe Perches | 106665d | 2010-11-09 17:14:01 -0800 | [diff] [blame] | 64 | |
| 65 | vaf.fmt = fmt; |
| 66 | vaf.va = &args; |
| 67 | |
| 68 | printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | va_end(args); |
| 71 | } |
| 72 | } |
Marcelo Roberto Jimenez | b9f515e | 2010-10-18 22:38:08 +0100 | [diff] [blame] | 73 | EXPORT_SYMBOL(soc_pcmcia_debug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | #endif |
| 76 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 77 | #define to_soc_pcmcia_socket(x) \ |
| 78 | container_of(x, struct soc_pcmcia_socket, socket) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | static unsigned short |
| 81 | calc_speed(unsigned short *spds, int num, unsigned short dflt) |
| 82 | { |
| 83 | unsigned short speed = 0; |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < num; i++) |
| 87 | if (speed < spds[i]) |
| 88 | speed = spds[i]; |
| 89 | if (speed == 0) |
| 90 | speed = dflt; |
| 91 | |
| 92 | return speed; |
| 93 | } |
| 94 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 95 | void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt, |
| 96 | struct soc_pcmcia_timing *timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 98 | timing->io = |
| 99 | calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS); |
| 100 | timing->mem = |
| 101 | calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS); |
| 102 | timing->attr = |
| 103 | calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL(soc_common_pcmcia_get_timing); |
| 106 | |
| 107 | static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt) |
| 108 | { |
| 109 | struct pcmcia_state state; |
| 110 | unsigned int stat; |
| 111 | |
| 112 | memset(&state, 0, sizeof(struct pcmcia_state)); |
| 113 | |
| 114 | skt->ops->socket_state(skt, &state); |
| 115 | |
| 116 | stat = state.detect ? SS_DETECT : 0; |
| 117 | stat |= state.ready ? SS_READY : 0; |
| 118 | stat |= state.wrprot ? SS_WRPROT : 0; |
| 119 | stat |= state.vs_3v ? SS_3VCARD : 0; |
| 120 | stat |= state.vs_Xv ? SS_XVCARD : 0; |
| 121 | |
| 122 | /* The power status of individual sockets is not available |
| 123 | * explicitly from the hardware, so we just remember the state |
| 124 | * and regurgitate it upon request: |
| 125 | */ |
| 126 | stat |= skt->cs_state.Vcc ? SS_POWERON : 0; |
| 127 | |
| 128 | if (skt->cs_state.flags & SS_IOCARD) |
| 129 | stat |= state.bvd1 ? SS_STSCHG : 0; |
| 130 | else { |
| 131 | if (state.bvd1 == 0) |
| 132 | stat |= SS_BATDEAD; |
| 133 | else if (state.bvd2 == 0) |
| 134 | stat |= SS_BATWARN; |
| 135 | } |
| 136 | return stat; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * soc_common_pcmcia_config_skt |
| 141 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 142 | * |
| 143 | * Convert PCMCIA socket state to our socket configure structure. |
| 144 | */ |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 145 | static int soc_common_pcmcia_config_skt( |
| 146 | struct soc_pcmcia_socket *skt, socket_state_t *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | int ret; |
| 149 | |
| 150 | ret = skt->ops->configure_socket(skt, state); |
| 151 | if (ret == 0) { |
| 152 | /* |
| 153 | * This really needs a better solution. The IRQ |
| 154 | * may or may not be claimed by the driver. |
| 155 | */ |
| 156 | if (skt->irq_state != 1 && state->io_irq) { |
| 157 | skt->irq_state = 1; |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 158 | irq_set_irq_type(skt->socket.pci_irq, |
| 159 | IRQ_TYPE_EDGE_FALLING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } else if (skt->irq_state == 1 && state->io_irq == 0) { |
| 161 | skt->irq_state = 0; |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 162 | irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | skt->cs_state = *state; |
| 166 | } |
| 167 | |
| 168 | if (ret < 0) |
| 169 | printk(KERN_ERR "soc_common_pcmcia: unable to configure " |
| 170 | "socket %d\n", skt->nr); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | /* soc_common_pcmcia_sock_init() |
| 176 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 177 | * |
| 178 | * (Re-)Initialise the socket, turning on status interrupts |
| 179 | * and PCMCIA bus. This must wait for power to stabilise |
| 180 | * so that the card status signals report correctly. |
| 181 | * |
| 182 | * Returns: 0 |
| 183 | */ |
| 184 | static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock) |
| 185 | { |
| 186 | struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); |
| 187 | |
| 188 | debug(skt, 2, "initializing socket\n"); |
Jonathan Cameron | a747ce8 | 2011-07-13 15:54:56 +0100 | [diff] [blame] | 189 | if (skt->ops->socket_init) |
| 190 | skt->ops->socket_init(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * soc_common_pcmcia_suspend() |
| 197 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 198 | * |
| 199 | * Remove power on the socket, disable IRQs from the card. |
| 200 | * Turn off status interrupts, and disable the PCMCIA bus. |
| 201 | * |
| 202 | * Returns: 0 |
| 203 | */ |
| 204 | static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock) |
| 205 | { |
| 206 | struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); |
| 207 | |
| 208 | debug(skt, 2, "suspending socket\n"); |
| 209 | |
Jonathan Cameron | a747ce8 | 2011-07-13 15:54:56 +0100 | [diff] [blame] | 210 | if (skt->ops->socket_suspend) |
| 211 | skt->ops->socket_suspend(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static DEFINE_SPINLOCK(status_lock); |
| 217 | |
| 218 | static void soc_common_check_status(struct soc_pcmcia_socket *skt) |
| 219 | { |
| 220 | unsigned int events; |
| 221 | |
| 222 | debug(skt, 4, "entering PCMCIA monitoring thread\n"); |
| 223 | |
| 224 | do { |
| 225 | unsigned int status; |
| 226 | unsigned long flags; |
| 227 | |
| 228 | status = soc_common_pcmcia_skt_state(skt); |
| 229 | |
| 230 | spin_lock_irqsave(&status_lock, flags); |
| 231 | events = (status ^ skt->status) & skt->cs_state.csc_mask; |
| 232 | skt->status = status; |
| 233 | spin_unlock_irqrestore(&status_lock, flags); |
| 234 | |
| 235 | debug(skt, 4, "events: %s%s%s%s%s%s\n", |
| 236 | events == 0 ? "<NONE>" : "", |
| 237 | events & SS_DETECT ? "DETECT " : "", |
| 238 | events & SS_READY ? "READY " : "", |
| 239 | events & SS_BATDEAD ? "BATDEAD " : "", |
| 240 | events & SS_BATWARN ? "BATWARN " : "", |
| 241 | events & SS_STSCHG ? "STSCHG " : ""); |
| 242 | |
| 243 | if (events) |
| 244 | pcmcia_parse_events(&skt->socket, events); |
| 245 | } while (events); |
| 246 | } |
| 247 | |
| 248 | /* Let's poll for events in addition to IRQs since IRQ only is unreliable... */ |
| 249 | static void soc_common_pcmcia_poll_event(unsigned long dummy) |
| 250 | { |
| 251 | struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy; |
| 252 | debug(skt, 4, "polling for events\n"); |
| 253 | |
| 254 | mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD); |
| 255 | |
| 256 | soc_common_check_status(skt); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* |
| 261 | * Service routine for socket driver interrupts (requested by the |
| 262 | * low-level PCMCIA init() operation via soc_common_pcmcia_thread()). |
| 263 | * The actual interrupt-servicing work is performed by |
| 264 | * soc_common_pcmcia_thread(), largely because the Card Services event- |
| 265 | * handling code performs scheduling operations which cannot be |
| 266 | * executed from within an interrupt context. |
| 267 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 268 | static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
| 270 | struct soc_pcmcia_socket *skt = dev; |
| 271 | |
| 272 | debug(skt, 3, "servicing IRQ %d\n", irq); |
| 273 | |
| 274 | soc_common_check_status(skt); |
| 275 | |
| 276 | return IRQ_HANDLED; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /* |
| 281 | * Implements the get_status() operation for the in-kernel PCMCIA |
| 282 | * service (formerly SS_GetStatus in Card Services). Essentially just |
| 283 | * fills in bits in `status' according to internal driver state or |
| 284 | * the value of the voltage detect chipselect register. |
| 285 | * |
| 286 | * As a debugging note, during card startup, the PCMCIA core issues |
| 287 | * three set_socket() commands in a row the first with RESET deasserted, |
| 288 | * the second with RESET asserted, and the last with RESET deasserted |
| 289 | * again. Following the third set_socket(), a get_status() command will |
| 290 | * be issued. The kernel is looking for the SS_READY flag (see |
| 291 | * setup_socket(), reset_socket(), and unreset_socket() in cs.c). |
| 292 | * |
| 293 | * Returns: 0 |
| 294 | */ |
| 295 | static int |
| 296 | soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status) |
| 297 | { |
| 298 | struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); |
| 299 | |
| 300 | skt->status = soc_common_pcmcia_skt_state(skt); |
| 301 | *status = skt->status; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * Implements the set_socket() operation for the in-kernel PCMCIA |
| 309 | * service (formerly SS_SetSocket in Card Services). We more or |
| 310 | * less punt all of this work and let the kernel handle the details |
| 311 | * of power configuration, reset, &c. We also record the value of |
| 312 | * `state' in order to regurgitate it to the PCMCIA core later. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | */ |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 314 | static int soc_common_pcmcia_set_socket( |
| 315 | struct pcmcia_socket *sock, socket_state_t *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
| 317 | struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); |
| 318 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 319 | debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n", |
| 320 | (state->csc_mask == 0) ? "<NONE> " : "", |
| 321 | (state->csc_mask & SS_DETECT) ? "DETECT " : "", |
| 322 | (state->csc_mask & SS_READY) ? "READY " : "", |
| 323 | (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "", |
| 324 | (state->csc_mask & SS_BATWARN) ? "BATWARN " : "", |
| 325 | (state->csc_mask & SS_STSCHG) ? "STSCHG " : "", |
| 326 | (state->flags == 0) ? "<NONE> " : "", |
| 327 | (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "", |
| 328 | (state->flags & SS_IOCARD) ? "IOCARD " : "", |
| 329 | (state->flags & SS_RESET) ? "RESET " : "", |
| 330 | (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "", |
| 331 | (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | state->Vcc, state->Vpp, state->io_irq); |
| 333 | |
| 334 | return soc_common_pcmcia_config_skt(skt, state); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* |
| 339 | * Implements the set_io_map() operation for the in-kernel PCMCIA |
| 340 | * service (formerly SS_SetIOMap in Card Services). We configure |
| 341 | * the map speed as requested, but override the address ranges |
| 342 | * supplied by Card Services. |
| 343 | * |
| 344 | * Returns: 0 on success, -1 on error |
| 345 | */ |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 346 | static int soc_common_pcmcia_set_io_map( |
| 347 | struct pcmcia_socket *sock, struct pccard_io_map *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); |
| 350 | unsigned short speed = map->speed; |
| 351 | |
Wolfram Sang | 5f78433 | 2009-10-19 11:42:13 +0200 | [diff] [blame] | 352 | debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n", |
| 353 | map->map, map->speed, (unsigned long long)map->start, |
| 354 | (unsigned long long)map->stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n", |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 356 | (map->flags == 0) ? "<NONE>" : "", |
| 357 | (map->flags & MAP_ACTIVE) ? "ACTIVE " : "", |
| 358 | (map->flags & MAP_16BIT) ? "16BIT " : "", |
| 359 | (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "", |
| 360 | (map->flags & MAP_0WS) ? "0WS " : "", |
| 361 | (map->flags & MAP_WRPROT) ? "WRPROT " : "", |
| 362 | (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "", |
| 363 | (map->flags & MAP_PREFETCH) ? "PREFETCH " : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | if (map->map >= MAX_IO_WIN) { |
Harvey Harrison | 2e11cb4 | 2008-05-01 04:34:54 -0700 | [diff] [blame] | 366 | printk(KERN_ERR "%s(): map (%d) out of range\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | map->map); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | if (map->flags & MAP_ACTIVE) { |
| 372 | if (speed == 0) |
| 373 | speed = SOC_PCMCIA_IO_ACCESS; |
| 374 | } else { |
| 375 | speed = 0; |
| 376 | } |
| 377 | |
| 378 | skt->spd_io[map->map] = speed; |
| 379 | skt->ops->set_timing(skt); |
| 380 | |
| 381 | if (map->stop == 1) |
| 382 | map->stop = PAGE_SIZE-1; |
| 383 | |
| 384 | map->stop -= map->start; |
| 385 | map->stop += skt->socket.io_offset; |
| 386 | map->start = skt->socket.io_offset; |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /* |
| 393 | * Implements the set_mem_map() operation for the in-kernel PCMCIA |
| 394 | * service (formerly SS_SetMemMap in Card Services). We configure |
| 395 | * the map speed as requested, but override the address ranges |
| 396 | * supplied by Card Services. |
| 397 | * |
Pavel Machek | 4846d01 | 2005-10-14 15:59:02 -0700 | [diff] [blame] | 398 | * Returns: 0 on success, -ERRNO on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | */ |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 400 | static int soc_common_pcmcia_set_mem_map( |
| 401 | struct pcmcia_socket *sock, struct pccard_mem_map *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
| 403 | struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); |
| 404 | struct resource *res; |
| 405 | unsigned short speed = map->speed; |
| 406 | |
| 407 | debug(skt, 2, "map %u speed %u card_start %08x\n", |
| 408 | map->map, map->speed, map->card_start); |
| 409 | debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n", |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 410 | (map->flags == 0) ? "<NONE>" : "", |
| 411 | (map->flags & MAP_ACTIVE) ? "ACTIVE " : "", |
| 412 | (map->flags & MAP_16BIT) ? "16BIT " : "", |
| 413 | (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "", |
| 414 | (map->flags & MAP_0WS) ? "0WS " : "", |
| 415 | (map->flags & MAP_WRPROT) ? "WRPROT " : "", |
| 416 | (map->flags & MAP_ATTRIB) ? "ATTRIB " : "", |
| 417 | (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | if (map->map >= MAX_WIN) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | if (map->flags & MAP_ACTIVE) { |
| 423 | if (speed == 0) |
| 424 | speed = 300; |
| 425 | } else { |
| 426 | speed = 0; |
| 427 | } |
| 428 | |
| 429 | if (map->flags & MAP_ATTRIB) { |
| 430 | res = &skt->res_attr; |
| 431 | skt->spd_attr[map->map] = speed; |
| 432 | skt->spd_mem[map->map] = 0; |
| 433 | } else { |
| 434 | res = &skt->res_mem; |
| 435 | skt->spd_attr[map->map] = 0; |
| 436 | skt->spd_mem[map->map] = speed; |
| 437 | } |
| 438 | |
| 439 | skt->ops->set_timing(skt); |
| 440 | |
| 441 | map->static_start = res->start + map->card_start; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | struct bittbl { |
| 447 | unsigned int mask; |
| 448 | const char *name; |
| 449 | }; |
| 450 | |
| 451 | static struct bittbl status_bits[] = { |
| 452 | { SS_WRPROT, "SS_WRPROT" }, |
| 453 | { SS_BATDEAD, "SS_BATDEAD" }, |
| 454 | { SS_BATWARN, "SS_BATWARN" }, |
| 455 | { SS_READY, "SS_READY" }, |
| 456 | { SS_DETECT, "SS_DETECT" }, |
| 457 | { SS_POWERON, "SS_POWERON" }, |
| 458 | { SS_STSCHG, "SS_STSCHG" }, |
| 459 | { SS_3VCARD, "SS_3VCARD" }, |
| 460 | { SS_XVCARD, "SS_XVCARD" }, |
| 461 | }; |
| 462 | |
| 463 | static struct bittbl conf_bits[] = { |
| 464 | { SS_PWR_AUTO, "SS_PWR_AUTO" }, |
| 465 | { SS_IOCARD, "SS_IOCARD" }, |
| 466 | { SS_RESET, "SS_RESET" }, |
| 467 | { SS_DMA_MODE, "SS_DMA_MODE" }, |
| 468 | { SS_SPKR_ENA, "SS_SPKR_ENA" }, |
| 469 | { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" }, |
| 470 | }; |
| 471 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 472 | static void dump_bits(char **p, const char *prefix, |
| 473 | unsigned int val, struct bittbl *bits, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
| 475 | char *b = *p; |
| 476 | int i; |
| 477 | |
| 478 | b += sprintf(b, "%-9s:", prefix); |
| 479 | for (i = 0; i < sz; i++) |
| 480 | if (val & bits[i].mask) |
| 481 | b += sprintf(b, " %s", bits[i].name); |
| 482 | *b++ = '\n'; |
| 483 | *p = b; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Implements the /sys/class/pcmcia_socket/??/status file. |
| 488 | * |
| 489 | * Returns: the number of characters added to the buffer |
| 490 | */ |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 491 | static ssize_t show_status( |
| 492 | struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | struct soc_pcmcia_socket *skt = |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 495 | container_of(dev, struct soc_pcmcia_socket, socket.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | char *p = buf; |
| 497 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 498 | p += sprintf(p, "slot : %d\n", skt->nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | dump_bits(&p, "status", skt->status, |
| 501 | status_bits, ARRAY_SIZE(status_bits)); |
| 502 | dump_bits(&p, "csc_mask", skt->cs_state.csc_mask, |
| 503 | status_bits, ARRAY_SIZE(status_bits)); |
| 504 | dump_bits(&p, "cs_flags", skt->cs_state.flags, |
| 505 | conf_bits, ARRAY_SIZE(conf_bits)); |
| 506 | |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 507 | p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc); |
| 508 | p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp); |
| 509 | p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq, |
Russell King - ARM Linux | 66024db | 2009-03-29 22:45:26 +0100 | [diff] [blame] | 510 | skt->socket.pci_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | if (skt->ops->show_timing) |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 512 | p += skt->ops->show_timing(skt, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | return p-buf; |
| 515 | } |
Alexey Dobriyan | e4a3c3f | 2007-02-13 22:39:27 -0800 | [diff] [blame] | 516 | static DEVICE_ATTR(status, S_IRUGO, show_status, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
| 518 | |
| 519 | static struct pccard_operations soc_common_pcmcia_operations = { |
| 520 | .init = soc_common_pcmcia_sock_init, |
| 521 | .suspend = soc_common_pcmcia_suspend, |
| 522 | .get_status = soc_common_pcmcia_get_status, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | .set_socket = soc_common_pcmcia_set_socket, |
| 524 | .set_io_map = soc_common_pcmcia_set_io_map, |
| 525 | .set_mem_map = soc_common_pcmcia_set_mem_map, |
| 526 | }; |
| 527 | |
| 528 | |
| 529 | int soc_pcmcia_request_irqs(struct soc_pcmcia_socket *skt, |
| 530 | struct pcmcia_irqs *irqs, int nr) |
| 531 | { |
| 532 | int i, res = 0; |
| 533 | |
| 534 | for (i = 0; i < nr; i++) { |
| 535 | if (irqs[i].sock != skt->nr) |
| 536 | continue; |
| 537 | res = request_irq(irqs[i].irq, soc_common_pcmcia_interrupt, |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 538 | IRQF_DISABLED, irqs[i].str, skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (res) |
| 540 | break; |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 541 | irq_set_irq_type(irqs[i].irq, IRQ_TYPE_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (res) { |
| 545 | printk(KERN_ERR "PCMCIA: request for IRQ%d failed (%d)\n", |
| 546 | irqs[i].irq, res); |
| 547 | |
| 548 | while (i--) |
| 549 | if (irqs[i].sock == skt->nr) |
| 550 | free_irq(irqs[i].irq, skt); |
| 551 | } |
| 552 | return res; |
| 553 | } |
| 554 | EXPORT_SYMBOL(soc_pcmcia_request_irqs); |
| 555 | |
| 556 | void soc_pcmcia_free_irqs(struct soc_pcmcia_socket *skt, |
| 557 | struct pcmcia_irqs *irqs, int nr) |
| 558 | { |
| 559 | int i; |
| 560 | |
| 561 | for (i = 0; i < nr; i++) |
| 562 | if (irqs[i].sock == skt->nr) |
| 563 | free_irq(irqs[i].irq, skt); |
| 564 | } |
| 565 | EXPORT_SYMBOL(soc_pcmcia_free_irqs); |
| 566 | |
| 567 | void soc_pcmcia_disable_irqs(struct soc_pcmcia_socket *skt, |
| 568 | struct pcmcia_irqs *irqs, int nr) |
| 569 | { |
| 570 | int i; |
| 571 | |
| 572 | for (i = 0; i < nr; i++) |
| 573 | if (irqs[i].sock == skt->nr) |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 574 | irq_set_irq_type(irqs[i].irq, IRQ_TYPE_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | EXPORT_SYMBOL(soc_pcmcia_disable_irqs); |
| 577 | |
| 578 | void soc_pcmcia_enable_irqs(struct soc_pcmcia_socket *skt, |
| 579 | struct pcmcia_irqs *irqs, int nr) |
| 580 | { |
| 581 | int i; |
| 582 | |
| 583 | for (i = 0; i < nr; i++) |
| 584 | if (irqs[i].sock == skt->nr) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 585 | irq_set_irq_type(irqs[i].irq, IRQ_TYPE_EDGE_RISING); |
| 586 | irq_set_irq_type(irqs[i].irq, IRQ_TYPE_EDGE_BOTH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | EXPORT_SYMBOL(soc_pcmcia_enable_irqs); |
| 590 | |
| 591 | |
Russell King - ARM Linux | 097e296 | 2009-03-26 21:45:05 +0000 | [diff] [blame] | 592 | static LIST_HEAD(soc_pcmcia_sockets); |
Andrew Morton | 23d077e | 2008-05-01 04:34:54 -0700 | [diff] [blame] | 593 | static DEFINE_MUTEX(soc_pcmcia_sockets_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | #ifdef CONFIG_CPU_FREQ |
| 596 | static int |
| 597 | soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data) |
| 598 | { |
| 599 | struct soc_pcmcia_socket *skt; |
| 600 | struct cpufreq_freqs *freqs = data; |
| 601 | int ret = 0; |
| 602 | |
Andrew Morton | 23d077e | 2008-05-01 04:34:54 -0700 | [diff] [blame] | 603 | mutex_lock(&soc_pcmcia_sockets_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | list_for_each_entry(skt, &soc_pcmcia_sockets, node) |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 605 | if (skt->ops->frequency_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | ret += skt->ops->frequency_change(skt, val, freqs); |
Andrew Morton | 23d077e | 2008-05-01 04:34:54 -0700 | [diff] [blame] | 607 | mutex_unlock(&soc_pcmcia_sockets_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | static struct notifier_block soc_pcmcia_notifier_block = { |
| 613 | .notifier_call = soc_pcmcia_notifier |
| 614 | }; |
| 615 | |
| 616 | static int soc_pcmcia_cpufreq_register(void) |
| 617 | { |
| 618 | int ret; |
| 619 | |
| 620 | ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block, |
| 621 | CPUFREQ_TRANSITION_NOTIFIER); |
| 622 | if (ret < 0) |
| 623 | printk(KERN_ERR "Unable to register CPU frequency change " |
| 624 | "notifier for PCMCIA (%d)\n", ret); |
| 625 | return ret; |
| 626 | } |
Russell King - ARM Linux | 0f767de | 2009-03-26 21:14:19 +0000 | [diff] [blame] | 627 | fs_initcall(soc_pcmcia_cpufreq_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | static void soc_pcmcia_cpufreq_unregister(void) |
| 630 | { |
Marcelo Roberto Jimenez | 17b38eb | 2010-10-18 22:39:05 +0100 | [diff] [blame] | 631 | cpufreq_unregister_notifier(&soc_pcmcia_notifier_block, |
| 632 | CPUFREQ_TRANSITION_NOTIFIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
Russell King - ARM Linux | 0f767de | 2009-03-26 21:14:19 +0000 | [diff] [blame] | 634 | module_exit(soc_pcmcia_cpufreq_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | #endif |
| 637 | |
Russell King - ARM Linux | 097e296 | 2009-03-26 21:45:05 +0000 | [diff] [blame] | 638 | void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt) |
| 639 | { |
| 640 | mutex_lock(&soc_pcmcia_sockets_lock); |
| 641 | del_timer_sync(&skt->poll_timer); |
| 642 | |
| 643 | pcmcia_unregister_socket(&skt->socket); |
| 644 | |
Russell King - ARM Linux | 097e296 | 2009-03-26 21:45:05 +0000 | [diff] [blame] | 645 | skt->ops->hw_shutdown(skt); |
| 646 | |
| 647 | soc_common_pcmcia_config_skt(skt, &dead_socket); |
| 648 | |
| 649 | list_del(&skt->node); |
| 650 | mutex_unlock(&soc_pcmcia_sockets_lock); |
| 651 | |
| 652 | iounmap(skt->virt_io); |
| 653 | skt->virt_io = NULL; |
| 654 | release_resource(&skt->res_attr); |
| 655 | release_resource(&skt->res_mem); |
| 656 | release_resource(&skt->res_io); |
| 657 | release_resource(&skt->res_skt); |
| 658 | } |
| 659 | EXPORT_SYMBOL(soc_pcmcia_remove_one); |
| 660 | |
| 661 | int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt) |
| 662 | { |
| 663 | int ret; |
| 664 | |
| 665 | init_timer(&skt->poll_timer); |
| 666 | skt->poll_timer.function = soc_common_pcmcia_poll_event; |
| 667 | skt->poll_timer.data = (unsigned long)skt; |
| 668 | skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD; |
| 669 | |
| 670 | ret = request_resource(&iomem_resource, &skt->res_skt); |
| 671 | if (ret) |
| 672 | goto out_err_1; |
| 673 | |
| 674 | ret = request_resource(&skt->res_skt, &skt->res_io); |
| 675 | if (ret) |
| 676 | goto out_err_2; |
| 677 | |
| 678 | ret = request_resource(&skt->res_skt, &skt->res_mem); |
| 679 | if (ret) |
| 680 | goto out_err_3; |
| 681 | |
| 682 | ret = request_resource(&skt->res_skt, &skt->res_attr); |
| 683 | if (ret) |
| 684 | goto out_err_4; |
| 685 | |
| 686 | skt->virt_io = ioremap(skt->res_io.start, 0x10000); |
| 687 | if (skt->virt_io == NULL) { |
| 688 | ret = -ENOMEM; |
| 689 | goto out_err_5; |
| 690 | } |
| 691 | |
| 692 | mutex_lock(&soc_pcmcia_sockets_lock); |
| 693 | |
| 694 | list_add(&skt->node, &soc_pcmcia_sockets); |
| 695 | |
| 696 | /* |
| 697 | * We initialize default socket timing here, because |
| 698 | * we are not guaranteed to see a SetIOMap operation at |
| 699 | * runtime. |
| 700 | */ |
| 701 | skt->ops->set_timing(skt); |
| 702 | |
| 703 | ret = skt->ops->hw_init(skt); |
| 704 | if (ret) |
| 705 | goto out_err_6; |
| 706 | |
| 707 | skt->socket.ops = &soc_common_pcmcia_operations; |
| 708 | skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD; |
| 709 | skt->socket.resource_ops = &pccard_static_ops; |
| 710 | skt->socket.irq_mask = 0; |
| 711 | skt->socket.map_size = PAGE_SIZE; |
Russell King - ARM Linux | 097e296 | 2009-03-26 21:45:05 +0000 | [diff] [blame] | 712 | skt->socket.io_offset = (unsigned long)skt->virt_io; |
| 713 | |
| 714 | skt->status = soc_common_pcmcia_skt_state(skt); |
| 715 | |
| 716 | ret = pcmcia_register_socket(&skt->socket); |
| 717 | if (ret) |
| 718 | goto out_err_7; |
| 719 | |
| 720 | add_timer(&skt->poll_timer); |
| 721 | |
| 722 | mutex_unlock(&soc_pcmcia_sockets_lock); |
| 723 | |
| 724 | ret = device_create_file(&skt->socket.dev, &dev_attr_status); |
| 725 | if (ret) |
| 726 | goto out_err_8; |
| 727 | |
| 728 | return ret; |
| 729 | |
| 730 | out_err_8: |
| 731 | mutex_lock(&soc_pcmcia_sockets_lock); |
| 732 | del_timer_sync(&skt->poll_timer); |
| 733 | pcmcia_unregister_socket(&skt->socket); |
| 734 | |
| 735 | out_err_7: |
Russell King - ARM Linux | 097e296 | 2009-03-26 21:45:05 +0000 | [diff] [blame] | 736 | skt->ops->hw_shutdown(skt); |
| 737 | out_err_6: |
| 738 | list_del(&skt->node); |
| 739 | mutex_unlock(&soc_pcmcia_sockets_lock); |
| 740 | iounmap(skt->virt_io); |
| 741 | out_err_5: |
| 742 | release_resource(&skt->res_attr); |
| 743 | out_err_4: |
| 744 | release_resource(&skt->res_mem); |
| 745 | out_err_3: |
| 746 | release_resource(&skt->res_io); |
| 747 | out_err_2: |
| 748 | release_resource(&skt->res_skt); |
| 749 | out_err_1: |
| 750 | |
| 751 | return ret; |
| 752 | } |
| 753 | EXPORT_SYMBOL(soc_pcmcia_add_one); |
| 754 | |
Russell King - ARM Linux | 0f767de | 2009-03-26 21:14:19 +0000 | [diff] [blame] | 755 | MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>"); |
| 756 | MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support"); |
| 757 | MODULE_LICENSE("Dual MPL/GPL"); |