Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Device driver for the PCMCIA controller module of the |
| 3 | * Hitachi HD64465 handheld companion chip. |
| 4 | * |
| 5 | * Note that the HD64465 provides a very thin PCMCIA host bridge |
| 6 | * layer, requiring a lot of the work of supporting cards to be |
| 7 | * performed by the processor. For example: mapping of card |
| 8 | * interrupts to processor IRQs is done by IRQ demuxing software; |
| 9 | * IO and memory mappings are fixed; setting voltages according |
| 10 | * to card Voltage Select pins etc is done in software. |
| 11 | * |
| 12 | * Note also that this driver uses only the simple, fixed, |
| 13 | * 16MB, 16-bit wide mappings to PCMCIA spaces defined by the |
| 14 | * HD64465. Larger mappings, smaller mappings, or mappings of |
| 15 | * different width to the same socket, are all possible only by |
| 16 | * involving the SH7750's MMU, which is considered unnecessary here. |
| 17 | * The downside is that it may be possible for some drivers to |
| 18 | * break because they need or expect 8-bit mappings. |
| 19 | * |
| 20 | * This driver currently supports only the following configuration: |
| 21 | * SH7750 CPU, HD64465, TPS2206 voltage control chip. |
| 22 | * |
| 23 | * by Greg Banks <gbanks@pocketpenguins.com> |
| 24 | * (c) 2000 PocketPenguins Inc |
| 25 | */ |
| 26 | |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/ioport.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/vmalloc.h> |
| 35 | #include <asm/errno.h> |
| 36 | #include <linux/irq.h> |
| 37 | #include <linux/interrupt.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 38 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/io.h> |
| 41 | #include <asm/hd64465/hd64465.h> |
| 42 | #include <asm/hd64465/io.h> |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <pcmcia/cs_types.h> |
| 45 | #include <pcmcia/cs.h> |
| 46 | #include <pcmcia/cistpl.h> |
| 47 | #include <pcmcia/ds.h> |
| 48 | #include <pcmcia/ss.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include "cs_internal.h" |
| 50 | |
| 51 | #define MODNAME "hd64465_ss" |
| 52 | |
| 53 | /* #define HD64465_DEBUG 1 */ |
| 54 | |
| 55 | #if HD64465_DEBUG |
| 56 | #define DPRINTK(args...) printk(MODNAME ": " args) |
| 57 | #else |
| 58 | #define DPRINTK(args...) |
| 59 | #endif |
| 60 | |
| 61 | extern int hd64465_io_debug; |
| 62 | extern void * p3_ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags); |
| 63 | extern void p3_iounmap(void *addr); |
| 64 | |
| 65 | /*============================================================*/ |
| 66 | |
| 67 | #define HS_IO_MAP_SIZE (64*1024) |
| 68 | |
| 69 | typedef struct hs_socket_t |
| 70 | { |
| 71 | unsigned int number; |
| 72 | u_int irq; |
| 73 | u_long mem_base; |
| 74 | void *io_base; |
| 75 | u_long mem_length; |
| 76 | u_int ctrl_base; |
| 77 | socket_state_t state; |
| 78 | pccard_io_map io_maps[MAX_IO_WIN]; |
| 79 | pccard_mem_map mem_maps[MAX_WIN]; |
| 80 | struct pcmcia_socket socket; |
| 81 | } hs_socket_t; |
| 82 | |
| 83 | |
| 84 | |
| 85 | #define HS_MAX_SOCKETS 2 |
| 86 | static hs_socket_t hs_sockets[HS_MAX_SOCKETS]; |
| 87 | |
| 88 | #define hs_in(sp, r) inb((sp)->ctrl_base + (r)) |
| 89 | #define hs_out(sp, v, r) outb(v, (sp)->ctrl_base + (r)) |
| 90 | |
| 91 | |
| 92 | /* translate a boolean value to a bit in a register */ |
| 93 | #define bool_to_regbit(sp, r, bi, bo) \ |
| 94 | do { \ |
| 95 | unsigned short v = hs_in(sp, r); \ |
| 96 | if (bo) \ |
| 97 | v |= (bi); \ |
| 98 | else \ |
| 99 | v &= ~(bi); \ |
| 100 | hs_out(sp, v, r); \ |
| 101 | } while(0) |
| 102 | |
| 103 | /* register offsets from HD64465_REG_PCC[01]ISR */ |
| 104 | #define ISR 0x0 |
| 105 | #define GCR 0x2 |
| 106 | #define CSCR 0x4 |
| 107 | #define CSCIER 0x6 |
| 108 | #define SCR 0x8 |
| 109 | |
| 110 | |
| 111 | /* Mask and values for CSCIER register */ |
| 112 | #define IER_MASK 0x80 |
| 113 | #define IER_ON 0x3f /* interrupts on */ |
| 114 | #define IER_OFF 0x00 /* interrupts off */ |
| 115 | |
| 116 | /*============================================================*/ |
| 117 | |
| 118 | #if HD64465_DEBUG > 10 |
| 119 | |
| 120 | static void cis_hex_dump(const unsigned char *x, int len) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | for (i=0 ; i<len ; i++) |
| 125 | { |
| 126 | if (!(i & 0xf)) |
| 127 | printk("\n%08x", (unsigned)(x + i)); |
| 128 | printk(" %02x", *(volatile unsigned short*)x); |
| 129 | x += 2; |
| 130 | } |
| 131 | printk("\n"); |
| 132 | } |
| 133 | |
| 134 | #endif |
| 135 | /*============================================================*/ |
| 136 | |
| 137 | /* |
| 138 | * This code helps create the illusion that the IREQ line from |
| 139 | * the PC card is mapped to one of the CPU's IRQ lines by the |
| 140 | * host bridge hardware (which is how every host bridge *except* |
| 141 | * the HD64465 works). In particular, it supports enabling |
| 142 | * and disabling the IREQ line by code which knows nothing |
| 143 | * about the host bridge (e.g. device drivers, IDE code) using |
| 144 | * the request_irq(), free_irq(), probe_irq_on() and probe_irq_off() |
| 145 | * functions. Also, it supports sharing the mapped IRQ with |
| 146 | * real hardware IRQs from the -IRL0-3 lines. |
| 147 | */ |
| 148 | |
| 149 | #define HS_NUM_MAPPED_IRQS 16 /* Limitation of the PCMCIA code */ |
| 150 | static struct |
| 151 | { |
| 152 | /* index is mapped irq number */ |
| 153 | hs_socket_t *sock; |
| 154 | hw_irq_controller *old_handler; |
| 155 | } hs_mapped_irq[HS_NUM_MAPPED_IRQS]; |
| 156 | |
| 157 | static void hs_socket_enable_ireq(hs_socket_t *sp) |
| 158 | { |
| 159 | unsigned short cscier; |
| 160 | |
| 161 | DPRINTK("hs_socket_enable_ireq(sock=%d)\n", sp->number); |
| 162 | |
| 163 | cscier = hs_in(sp, CSCIER); |
| 164 | cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK; |
| 165 | cscier |= HD64465_PCCCSCIER_PIREQE_LEVEL; |
| 166 | hs_out(sp, cscier, CSCIER); |
| 167 | } |
| 168 | |
| 169 | static void hs_socket_disable_ireq(hs_socket_t *sp) |
| 170 | { |
| 171 | unsigned short cscier; |
| 172 | |
| 173 | DPRINTK("hs_socket_disable_ireq(sock=%d)\n", sp->number); |
| 174 | |
| 175 | cscier = hs_in(sp, CSCIER); |
| 176 | cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK; |
| 177 | hs_out(sp, cscier, CSCIER); |
| 178 | } |
| 179 | |
| 180 | static unsigned int hs_startup_irq(unsigned int irq) |
| 181 | { |
| 182 | hs_socket_enable_ireq(hs_mapped_irq[irq].sock); |
| 183 | hs_mapped_irq[irq].old_handler->startup(irq); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static void hs_shutdown_irq(unsigned int irq) |
| 188 | { |
| 189 | hs_socket_disable_ireq(hs_mapped_irq[irq].sock); |
| 190 | hs_mapped_irq[irq].old_handler->shutdown(irq); |
| 191 | } |
| 192 | |
| 193 | static void hs_enable_irq(unsigned int irq) |
| 194 | { |
| 195 | hs_socket_enable_ireq(hs_mapped_irq[irq].sock); |
| 196 | hs_mapped_irq[irq].old_handler->enable(irq); |
| 197 | } |
| 198 | |
| 199 | static void hs_disable_irq(unsigned int irq) |
| 200 | { |
| 201 | hs_socket_disable_ireq(hs_mapped_irq[irq].sock); |
| 202 | hs_mapped_irq[irq].old_handler->disable(irq); |
| 203 | } |
| 204 | |
| 205 | extern struct hw_interrupt_type no_irq_type; |
| 206 | |
| 207 | static void hs_mask_and_ack_irq(unsigned int irq) |
| 208 | { |
| 209 | hs_socket_disable_ireq(hs_mapped_irq[irq].sock); |
| 210 | /* ack_none() spuriously complains about an unexpected IRQ */ |
| 211 | if (hs_mapped_irq[irq].old_handler != &no_irq_type) |
| 212 | hs_mapped_irq[irq].old_handler->ack(irq); |
| 213 | } |
| 214 | |
| 215 | static void hs_end_irq(unsigned int irq) |
| 216 | { |
| 217 | hs_socket_enable_ireq(hs_mapped_irq[irq].sock); |
| 218 | hs_mapped_irq[irq].old_handler->end(irq); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static struct hw_interrupt_type hd64465_ss_irq_type = { |
| 223 | .typename = "PCMCIA-IRQ", |
| 224 | .startup = hs_startup_irq, |
| 225 | .shutdown = hs_shutdown_irq, |
| 226 | .enable = hs_enable_irq, |
| 227 | .disable = hs_disable_irq, |
| 228 | .ack = hs_mask_and_ack_irq, |
| 229 | .end = hs_end_irq |
| 230 | }; |
| 231 | |
| 232 | /* |
| 233 | * This function should only ever be called with interrupts disabled. |
| 234 | */ |
| 235 | static void hs_map_irq(hs_socket_t *sp, unsigned int irq) |
| 236 | { |
| 237 | DPRINTK("hs_map_irq(sock=%d irq=%d)\n", sp->number, irq); |
| 238 | |
| 239 | if (irq >= HS_NUM_MAPPED_IRQS) |
| 240 | return; |
| 241 | |
| 242 | hs_mapped_irq[irq].sock = sp; |
| 243 | /* insert ourselves as the irq controller */ |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 244 | hs_mapped_irq[irq].old_handler = irq_desc[irq].chip; |
| 245 | irq_desc[irq].chip = &hd64465_ss_irq_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * This function should only ever be called with interrupts disabled. |
| 251 | */ |
| 252 | static void hs_unmap_irq(hs_socket_t *sp, unsigned int irq) |
| 253 | { |
| 254 | DPRINTK("hs_unmap_irq(sock=%d irq=%d)\n", sp->number, irq); |
| 255 | |
| 256 | if (irq >= HS_NUM_MAPPED_IRQS) |
| 257 | return; |
| 258 | |
| 259 | /* restore the original irq controller */ |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 260 | irq_desc[irq].chip = hs_mapped_irq[irq].old_handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /*============================================================*/ |
| 264 | |
| 265 | |
| 266 | /* |
| 267 | * Set Vpp and Vcc (in tenths of a Volt). Does not |
| 268 | * support the hi-Z state. |
| 269 | * |
| 270 | * Note, this assumes the board uses a TPS2206 chip to control |
| 271 | * the Vcc and Vpp voltages to the hs_sockets. If your board |
| 272 | * uses the MIC2563 (also supported by the HD64465) then you |
| 273 | * will have to modify this function. |
| 274 | */ |
| 275 | /* 0V 3.3V 5.5V */ |
| 276 | static const u_char hs_tps2206_avcc[3] = { 0x00, 0x04, 0x08 }; |
| 277 | static const u_char hs_tps2206_bvcc[3] = { 0x00, 0x80, 0x40 }; |
| 278 | |
| 279 | static int hs_set_voltages(hs_socket_t *sp, int Vcc, int Vpp) |
| 280 | { |
| 281 | u_int psr; |
| 282 | u_int vcci = 0; |
| 283 | u_int sock = sp->number; |
| 284 | |
| 285 | DPRINTK("hs_set_voltage(%d, %d, %d)\n", sock, Vcc, Vpp); |
| 286 | |
| 287 | switch (Vcc) |
| 288 | { |
| 289 | case 0: vcci = 0; break; |
| 290 | case 33: vcci = 1; break; |
| 291 | case 50: vcci = 2; break; |
| 292 | default: return 0; |
| 293 | } |
| 294 | |
| 295 | /* Note: Vpp = 120 not supported -- Greg Banks */ |
| 296 | if (Vpp != 0 && Vpp != Vcc) |
| 297 | return 0; |
| 298 | |
| 299 | /* The PSR register holds 8 of the 9 bits which control |
| 300 | * the TPS2206 via its serial interface. |
| 301 | */ |
| 302 | psr = inw(HD64465_REG_PCCPSR); |
| 303 | switch (sock) |
| 304 | { |
| 305 | case 0: |
| 306 | psr &= 0x0f; |
| 307 | psr |= hs_tps2206_avcc[vcci]; |
| 308 | psr |= (Vpp == 0 ? 0x00 : 0x02); |
| 309 | break; |
| 310 | case 1: |
| 311 | psr &= 0xf0; |
| 312 | psr |= hs_tps2206_bvcc[vcci]; |
| 313 | psr |= (Vpp == 0 ? 0x00 : 0x20); |
| 314 | break; |
| 315 | }; |
| 316 | outw(psr, HD64465_REG_PCCPSR); |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /*============================================================*/ |
| 323 | |
| 324 | /* |
| 325 | * Drive the RESET line to the card. |
| 326 | */ |
| 327 | static void hs_reset_socket(hs_socket_t *sp, int on) |
| 328 | { |
| 329 | unsigned short v; |
| 330 | |
| 331 | v = hs_in(sp, GCR); |
| 332 | if (on) |
| 333 | v |= HD64465_PCCGCR_PCCR; |
| 334 | else |
| 335 | v &= ~HD64465_PCCGCR_PCCR; |
| 336 | hs_out(sp, v, GCR); |
| 337 | } |
| 338 | |
| 339 | /*============================================================*/ |
| 340 | |
| 341 | static int hs_init(struct pcmcia_socket *s) |
| 342 | { |
| 343 | hs_socket_t *sp = container_of(s, struct hs_socket_t, socket); |
| 344 | |
| 345 | DPRINTK("hs_init(%d)\n", sp->number); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /*============================================================*/ |
| 351 | |
| 352 | |
| 353 | static int hs_get_status(struct pcmcia_socket *s, u_int *value) |
| 354 | { |
| 355 | hs_socket_t *sp = container_of(s, struct hs_socket_t, socket); |
| 356 | unsigned int isr; |
| 357 | u_int status = 0; |
| 358 | |
| 359 | |
| 360 | isr = hs_in(sp, ISR); |
| 361 | |
| 362 | /* Card is seated and powered when *both* CD pins are low */ |
| 363 | if ((isr & HD64465_PCCISR_PCD_MASK) == 0) |
| 364 | { |
| 365 | status |= SS_DETECT; /* card present */ |
| 366 | |
| 367 | switch (isr & HD64465_PCCISR_PBVD_MASK) |
| 368 | { |
| 369 | case HD64465_PCCISR_PBVD_BATGOOD: |
| 370 | break; |
| 371 | case HD64465_PCCISR_PBVD_BATWARN: |
| 372 | status |= SS_BATWARN; |
| 373 | break; |
| 374 | default: |
| 375 | status |= SS_BATDEAD; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | if (isr & HD64465_PCCISR_PREADY) |
| 380 | status |= SS_READY; |
| 381 | |
| 382 | if (isr & HD64465_PCCISR_PMWP) |
| 383 | status |= SS_WRPROT; |
| 384 | |
| 385 | /* Voltage Select pins interpreted as per Table 4-5 of the std. |
| 386 | * Assuming we have the TPS2206, the socket is a "Low Voltage |
| 387 | * key, 3.3V and 5V available, no X.XV available". |
| 388 | */ |
| 389 | switch (isr & (HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1)) |
| 390 | { |
| 391 | case HD64465_PCCISR_PVS1: |
| 392 | printk(KERN_NOTICE MODNAME ": cannot handle X.XV card, ignored\n"); |
| 393 | status = 0; |
| 394 | break; |
| 395 | case 0: |
| 396 | case HD64465_PCCISR_PVS2: |
| 397 | /* 3.3V */ |
| 398 | status |= SS_3VCARD; |
| 399 | break; |
| 400 | case HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1: |
| 401 | /* 5V */ |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | /* TODO: SS_POWERON */ |
| 406 | /* TODO: SS_STSCHG */ |
| 407 | } |
| 408 | |
| 409 | DPRINTK("hs_get_status(%d) = %x\n", sock, status); |
| 410 | |
| 411 | *value = status; |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /*============================================================*/ |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | static int hs_set_socket(struct pcmcia_socket *s, socket_state_t *state) |
| 418 | { |
| 419 | hs_socket_t *sp = container_of(s, struct hs_socket_t, socket); |
| 420 | u_long flags; |
| 421 | u_int changed; |
| 422 | unsigned short cscier; |
| 423 | |
| 424 | DPRINTK("hs_set_socket(sock=%d, flags=%x, csc_mask=%x, Vcc=%d, Vpp=%d, io_irq=%d)\n", |
| 425 | sock, state->flags, state->csc_mask, state->Vcc, state->Vpp, state->io_irq); |
| 426 | |
| 427 | local_irq_save(flags); /* Don't want interrupts happening here */ |
| 428 | |
| 429 | if (state->Vpp != sp->state.Vpp || |
| 430 | state->Vcc != sp->state.Vcc) { |
| 431 | if (!hs_set_voltages(sp, state->Vcc, state->Vpp)) { |
| 432 | local_irq_restore(flags); |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* hd64465_io_debug = 1; */ |
| 438 | /* |
| 439 | * Handle changes in the Card Status Change mask, |
| 440 | * by propagating to the CSCR register |
| 441 | */ |
| 442 | changed = sp->state.csc_mask ^ state->csc_mask; |
| 443 | cscier = hs_in(sp, CSCIER); |
| 444 | |
| 445 | if (changed & SS_DETECT) { |
| 446 | if (state->csc_mask & SS_DETECT) |
| 447 | cscier |= HD64465_PCCCSCIER_PCDE; |
| 448 | else |
| 449 | cscier &= ~HD64465_PCCCSCIER_PCDE; |
| 450 | } |
| 451 | |
| 452 | if (changed & SS_READY) { |
| 453 | if (state->csc_mask & SS_READY) |
| 454 | cscier |= HD64465_PCCCSCIER_PRE; |
| 455 | else |
| 456 | cscier &= ~HD64465_PCCCSCIER_PRE; |
| 457 | } |
| 458 | |
| 459 | if (changed & SS_BATDEAD) { |
| 460 | if (state->csc_mask & SS_BATDEAD) |
| 461 | cscier |= HD64465_PCCCSCIER_PBDE; |
| 462 | else |
| 463 | cscier &= ~HD64465_PCCCSCIER_PBDE; |
| 464 | } |
| 465 | |
| 466 | if (changed & SS_BATWARN) { |
| 467 | if (state->csc_mask & SS_BATWARN) |
| 468 | cscier |= HD64465_PCCCSCIER_PBWE; |
| 469 | else |
| 470 | cscier &= ~HD64465_PCCCSCIER_PBWE; |
| 471 | } |
| 472 | |
| 473 | if (changed & SS_STSCHG) { |
| 474 | if (state->csc_mask & SS_STSCHG) |
| 475 | cscier |= HD64465_PCCCSCIER_PSCE; |
| 476 | else |
| 477 | cscier &= ~HD64465_PCCCSCIER_PSCE; |
| 478 | } |
| 479 | |
| 480 | hs_out(sp, cscier, CSCIER); |
| 481 | |
| 482 | if (sp->state.io_irq && !state->io_irq) |
| 483 | hs_unmap_irq(sp, sp->state.io_irq); |
| 484 | else if (!sp->state.io_irq && state->io_irq) |
| 485 | hs_map_irq(sp, state->io_irq); |
| 486 | |
| 487 | |
| 488 | /* |
| 489 | * Handle changes in the flags field, |
| 490 | * by propagating to config registers. |
| 491 | */ |
| 492 | changed = sp->state.flags ^ state->flags; |
| 493 | |
| 494 | if (changed & SS_IOCARD) { |
| 495 | DPRINTK("card type: %s\n", |
| 496 | (state->flags & SS_IOCARD ? "i/o" : "memory" )); |
| 497 | bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCT, |
| 498 | state->flags & SS_IOCARD); |
| 499 | } |
| 500 | |
| 501 | if (changed & SS_RESET) { |
| 502 | DPRINTK("%s reset card\n", |
| 503 | (state->flags & SS_RESET ? "start" : "stop")); |
| 504 | bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCR, |
| 505 | state->flags & SS_RESET); |
| 506 | } |
| 507 | |
| 508 | if (changed & SS_OUTPUT_ENA) { |
| 509 | DPRINTK("%sabling card output\n", |
| 510 | (state->flags & SS_OUTPUT_ENA ? "en" : "dis")); |
| 511 | bool_to_regbit(sp, GCR, HD64465_PCCGCR_PDRV, |
| 512 | state->flags & SS_OUTPUT_ENA); |
| 513 | } |
| 514 | |
| 515 | /* TODO: SS_SPKR_ENA */ |
| 516 | |
| 517 | /* hd64465_io_debug = 0; */ |
| 518 | sp->state = *state; |
| 519 | |
| 520 | local_irq_restore(flags); |
| 521 | |
| 522 | #if HD64465_DEBUG > 10 |
| 523 | if (state->flags & SS_OUTPUT_ENA) |
| 524 | cis_hex_dump((const unsigned char*)sp->mem_base, 0x100); |
| 525 | #endif |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /*============================================================*/ |
| 530 | |
| 531 | static int hs_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io) |
| 532 | { |
| 533 | hs_socket_t *sp = container_of(s, struct hs_socket_t, socket); |
| 534 | int map = io->map; |
| 535 | int sock = sp->number; |
| 536 | struct pccard_io_map *sio; |
| 537 | pgprot_t prot; |
| 538 | |
| 539 | DPRINTK("hs_set_io_map(sock=%d, map=%d, flags=0x%x, speed=%dns, start=%#lx, stop=%#lx)\n", |
| 540 | sock, map, io->flags, io->speed, io->start, io->stop); |
| 541 | if (map >= MAX_IO_WIN) |
| 542 | return -EINVAL; |
| 543 | sio = &sp->io_maps[map]; |
| 544 | |
| 545 | /* check for null changes */ |
| 546 | if (io->flags == sio->flags && |
| 547 | io->start == sio->start && |
| 548 | io->stop == sio->stop) |
| 549 | return 0; |
| 550 | |
| 551 | if (io->flags & MAP_AUTOSZ) |
| 552 | prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IODYN); |
| 553 | else if (io->flags & MAP_16BIT) |
| 554 | prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO16); |
| 555 | else |
| 556 | prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO8); |
| 557 | |
| 558 | /* TODO: handle MAP_USE_WAIT */ |
| 559 | if (io->flags & MAP_USE_WAIT) |
| 560 | printk(KERN_INFO MODNAME ": MAP_USE_WAIT unimplemented\n"); |
| 561 | /* TODO: handle MAP_PREFETCH */ |
| 562 | if (io->flags & MAP_PREFETCH) |
| 563 | printk(KERN_INFO MODNAME ": MAP_PREFETCH unimplemented\n"); |
| 564 | /* TODO: handle MAP_WRPROT */ |
| 565 | if (io->flags & MAP_WRPROT) |
| 566 | printk(KERN_INFO MODNAME ": MAP_WRPROT unimplemented\n"); |
| 567 | /* TODO: handle MAP_0WS */ |
| 568 | if (io->flags & MAP_0WS) |
| 569 | printk(KERN_INFO MODNAME ": MAP_0WS unimplemented\n"); |
| 570 | |
| 571 | if (io->flags & MAP_ACTIVE) { |
| 572 | unsigned long pstart, psize, paddrbase; |
| 573 | |
| 574 | paddrbase = virt_to_phys((void*)(sp->mem_base + 2 * HD64465_PCC_WINDOW)); |
| 575 | pstart = io->start & PAGE_MASK; |
| 576 | psize = ((io->stop + PAGE_SIZE) & PAGE_MASK) - pstart; |
| 577 | |
| 578 | /* |
| 579 | * Change PTEs in only that portion of the mapping requested |
| 580 | * by the caller. This means that most of the time, most of |
| 581 | * the PTEs in the io_vma will be unmapped and only the bottom |
| 582 | * page will be mapped. But the code allows for weird cards |
| 583 | * that might want IO ports > 4K. |
| 584 | */ |
| 585 | sp->io_base = p3_ioremap(paddrbase + pstart, psize, pgprot_val(prot)); |
| 586 | |
| 587 | /* |
| 588 | * Change the mapping used by inb() outb() etc |
| 589 | */ |
| 590 | hd64465_port_map(io->start, |
| 591 | io->stop - io->start + 1, |
| 592 | (unsigned long)sp->io_base + io->start, 0); |
| 593 | } else { |
| 594 | hd64465_port_unmap(sio->start, sio->stop - sio->start + 1); |
| 595 | p3_iounmap(sp->io_base); |
| 596 | } |
| 597 | |
| 598 | *sio = *io; |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /*============================================================*/ |
| 603 | |
| 604 | static int hs_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem) |
| 605 | { |
| 606 | hs_socket_t *sp = container_of(s, struct hs_socket_t, socket); |
| 607 | struct pccard_mem_map *smem; |
| 608 | int map = mem->map; |
| 609 | unsigned long paddr; |
| 610 | |
| 611 | #if 0 |
| 612 | DPRINTK("hs_set_mem_map(sock=%d, map=%d, flags=0x%x, card_start=0x%08x)\n", |
| 613 | sock, map, mem->flags, mem->card_start); |
| 614 | #endif |
| 615 | |
| 616 | if (map >= MAX_WIN) |
| 617 | return -EINVAL; |
| 618 | smem = &sp->mem_maps[map]; |
| 619 | |
| 620 | paddr = sp->mem_base; /* base of Attribute mapping */ |
| 621 | if (!(mem->flags & MAP_ATTRIB)) |
| 622 | paddr += HD64465_PCC_WINDOW; /* base of Common mapping */ |
| 623 | paddr += mem->card_start; |
| 624 | |
| 625 | /* Because we specified SS_CAP_STATIC_MAP, we are obliged |
| 626 | * at this time to report the system address corresponding |
| 627 | * to the card address requested. This is how Socket Services |
| 628 | * queries our fixed mapping. I wish this fact had been |
| 629 | * documented - Greg Banks. |
| 630 | */ |
| 631 | mem->static_start = paddr; |
| 632 | |
| 633 | *smem = *mem; |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | /* TODO: do we need to use the MMU to access Common memory ??? */ |
| 639 | |
| 640 | /*============================================================*/ |
| 641 | |
| 642 | /* |
| 643 | * This function is registered with the HD64465 glue code to do a |
| 644 | * secondary demux step on the PCMCIA interrupts. It handles |
| 645 | * mapping the IREQ request from the card to a standard Linux |
| 646 | * IRQ, as requested by SocketServices. |
| 647 | */ |
| 648 | static int hs_irq_demux(int irq, void *dev) |
| 649 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 650 | hs_socket_t *sp = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | u_int cscr; |
| 652 | |
| 653 | DPRINTK("hs_irq_demux(irq=%d)\n", irq); |
| 654 | |
| 655 | if (sp->state.io_irq && |
| 656 | (cscr = hs_in(sp, CSCR)) & HD64465_PCCCSCR_PIREQ) { |
| 657 | cscr &= ~HD64465_PCCCSCR_PIREQ; |
| 658 | hs_out(sp, cscr, CSCR); |
| 659 | return sp->state.io_irq; |
| 660 | } |
| 661 | |
| 662 | return irq; |
| 663 | } |
| 664 | |
| 665 | /*============================================================*/ |
| 666 | |
| 667 | /* |
| 668 | * Interrupt handling routine. |
| 669 | */ |
| 670 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 671 | static irqreturn_t hs_interrupt(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 673 | hs_socket_t *sp = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | u_int events = 0; |
| 675 | u_int cscr; |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | cscr = hs_in(sp, CSCR); |
| 678 | |
| 679 | DPRINTK("hs_interrupt, cscr=%04x\n", cscr); |
| 680 | |
| 681 | /* check for bus-related changes to be reported to Socket Services */ |
| 682 | if (cscr & HD64465_PCCCSCR_PCDC) { |
| 683 | /* double-check for a 16-bit card, as we don't support CardBus */ |
| 684 | if ((hs_in(sp, ISR) & HD64465_PCCISR_PCD_MASK) != 0) { |
| 685 | printk(KERN_NOTICE MODNAME |
| 686 | ": socket %d, card not a supported card type or not inserted correctly\n", |
| 687 | sp->number); |
| 688 | /* Don't do the rest unless a card is present */ |
| 689 | cscr &= ~(HD64465_PCCCSCR_PCDC| |
| 690 | HD64465_PCCCSCR_PRC| |
| 691 | HD64465_PCCCSCR_PBW| |
| 692 | HD64465_PCCCSCR_PBD| |
| 693 | HD64465_PCCCSCR_PSC); |
| 694 | } else { |
| 695 | cscr &= ~HD64465_PCCCSCR_PCDC; |
| 696 | events |= SS_DETECT; /* card insertion or removal */ |
| 697 | } |
| 698 | } |
| 699 | if (cscr & HD64465_PCCCSCR_PRC) { |
| 700 | cscr &= ~HD64465_PCCCSCR_PRC; |
| 701 | events |= SS_READY; /* ready signal changed */ |
| 702 | } |
| 703 | if (cscr & HD64465_PCCCSCR_PBW) { |
| 704 | cscr &= ~HD64465_PCCCSCR_PSC; |
| 705 | events |= SS_BATWARN; /* battery warning */ |
| 706 | } |
| 707 | if (cscr & HD64465_PCCCSCR_PBD) { |
| 708 | cscr &= ~HD64465_PCCCSCR_PSC; |
| 709 | events |= SS_BATDEAD; /* battery dead */ |
| 710 | } |
| 711 | if (cscr & HD64465_PCCCSCR_PSC) { |
| 712 | cscr &= ~HD64465_PCCCSCR_PSC; |
| 713 | events |= SS_STSCHG; /* STSCHG (status changed) signal */ |
| 714 | } |
| 715 | |
| 716 | if (cscr & HD64465_PCCCSCR_PIREQ) { |
| 717 | cscr &= ~HD64465_PCCCSCR_PIREQ; |
| 718 | |
| 719 | /* This should have been dealt with during irq demux */ |
| 720 | printk(KERN_NOTICE MODNAME ": unexpected IREQ from card\n"); |
| 721 | } |
| 722 | |
| 723 | hs_out(sp, cscr, CSCR); |
| 724 | |
| 725 | if (events) |
| 726 | pcmcia_parse_events(&sp->socket, events); |
| 727 | |
| 728 | return IRQ_HANDLED; |
| 729 | } |
| 730 | |
| 731 | /*============================================================*/ |
| 732 | |
| 733 | static struct pccard_operations hs_operations = { |
| 734 | .init = hs_init, |
| 735 | .get_status = hs_get_status, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | .set_socket = hs_set_socket, |
| 737 | .set_io_map = hs_set_io_map, |
| 738 | .set_mem_map = hs_set_mem_map, |
| 739 | }; |
| 740 | |
| 741 | static int hs_init_socket(hs_socket_t *sp, int irq, unsigned long mem_base, |
| 742 | unsigned int ctrl_base) |
| 743 | { |
| 744 | unsigned short v; |
| 745 | int i, err; |
| 746 | |
| 747 | memset(sp, 0, sizeof(*sp)); |
| 748 | sp->irq = irq; |
| 749 | sp->mem_base = mem_base; |
| 750 | sp->mem_length = 4*HD64465_PCC_WINDOW; /* 16MB */ |
| 751 | sp->ctrl_base = ctrl_base; |
| 752 | |
| 753 | for (i=0 ; i<MAX_IO_WIN ; i++) |
| 754 | sp->io_maps[i].map = i; |
| 755 | for (i=0 ; i<MAX_WIN ; i++) |
| 756 | sp->mem_maps[i].map = i; |
| 757 | |
| 758 | hd64465_register_irq_demux(sp->irq, hs_irq_demux, sp); |
| 759 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 760 | if ((err = request_irq(sp->irq, hs_interrupt, IRQF_DISABLED, MODNAME, sp)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return err; |
| 762 | if (request_mem_region(sp->mem_base, sp->mem_length, MODNAME) == 0) { |
| 763 | sp->mem_base = 0; |
| 764 | return -ENOMEM; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /* According to section 3.2 of the PCMCIA standard, low-voltage |
| 769 | * capable cards must implement cold insertion, i.e. Vpp and |
| 770 | * Vcc set to 0 before card is inserted. |
| 771 | */ |
| 772 | /*hs_set_voltages(sp, 0, 0);*/ |
| 773 | |
| 774 | /* hi-Z the outputs to the card and set 16MB map mode */ |
| 775 | v = hs_in(sp, GCR); |
| 776 | v &= ~HD64465_PCCGCR_PCCT; /* memory-only card */ |
| 777 | hs_out(sp, v, GCR); |
| 778 | |
| 779 | v = hs_in(sp, GCR); |
| 780 | v |= HD64465_PCCGCR_PDRV; /* enable outputs to card */ |
| 781 | hs_out(sp, v, GCR); |
| 782 | |
| 783 | v = hs_in(sp, GCR); |
| 784 | v |= HD64465_PCCGCR_PMMOD; /* 16MB mapping mode */ |
| 785 | hs_out(sp, v, GCR); |
| 786 | |
| 787 | v = hs_in(sp, GCR); |
| 788 | /* lowest 16MB of Common */ |
| 789 | v &= ~(HD64465_PCCGCR_PPA25|HD64465_PCCGCR_PPA24); |
| 790 | hs_out(sp, v, GCR); |
| 791 | |
| 792 | hs_reset_socket(sp, 1); |
| 793 | |
| 794 | printk(KERN_INFO "HD64465 PCMCIA bridge socket %d at 0x%08lx irq %d\n", |
| 795 | i, sp->mem_base, sp->irq); |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | static void hs_exit_socket(hs_socket_t *sp) |
| 801 | { |
| 802 | unsigned short cscier, gcr; |
| 803 | unsigned long flags; |
| 804 | |
| 805 | local_irq_save(flags); |
| 806 | |
| 807 | /* turn off interrupts in hardware */ |
| 808 | cscier = hs_in(sp, CSCIER); |
| 809 | cscier = (cscier & IER_MASK) | IER_OFF; |
| 810 | hs_out(sp, cscier, CSCIER); |
| 811 | |
| 812 | /* hi-Z the outputs to the card */ |
| 813 | gcr = hs_in(sp, GCR); |
| 814 | gcr &= HD64465_PCCGCR_PDRV; |
| 815 | hs_out(sp, gcr, GCR); |
| 816 | |
| 817 | /* power the card down */ |
| 818 | hs_set_voltages(sp, 0, 0); |
| 819 | |
| 820 | if (sp->mem_base != 0) |
| 821 | release_mem_region(sp->mem_base, sp->mem_length); |
| 822 | if (sp->irq != 0) { |
| 823 | free_irq(sp->irq, hs_interrupt); |
| 824 | hd64465_unregister_irq_demux(sp->irq); |
| 825 | } |
| 826 | |
| 827 | local_irq_restore(flags); |
| 828 | } |
| 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | static struct device_driver hd64465_driver = { |
| 831 | .name = "hd64465-pcmcia", |
| 832 | .bus = &platform_bus_type, |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 833 | .suspend = pcmcia_socket_dev_suspend, |
| 834 | .resume = pcmcia_socket_dev_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | }; |
| 836 | |
| 837 | static struct platform_device hd64465_device = { |
| 838 | .name = "hd64465-pcmcia", |
| 839 | .id = 0, |
| 840 | }; |
| 841 | |
| 842 | static int __init init_hs(void) |
| 843 | { |
| 844 | int i; |
| 845 | unsigned short v; |
| 846 | |
| 847 | /* hd64465_io_debug = 1; */ |
| 848 | if (driver_register(&hd64465_driver)) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | /* Wake both sockets out of STANDBY mode */ |
| 852 | /* TODO: wait 15ms */ |
| 853 | v = inw(HD64465_REG_SMSCR); |
| 854 | v &= ~(HD64465_SMSCR_PC0ST|HD64465_SMSCR_PC1ST); |
| 855 | outw(v, HD64465_REG_SMSCR); |
| 856 | |
| 857 | /* keep power controller out of shutdown mode */ |
| 858 | v = inb(HD64465_REG_PCC0SCR); |
| 859 | v |= HD64465_PCCSCR_SHDN; |
| 860 | outb(v, HD64465_REG_PCC0SCR); |
| 861 | |
| 862 | /* use serial (TPS2206) power controller */ |
| 863 | v = inb(HD64465_REG_PCC0CSCR); |
| 864 | v |= HD64465_PCCCSCR_PSWSEL; |
| 865 | outb(v, HD64465_REG_PCC0CSCR); |
| 866 | |
| 867 | /* |
| 868 | * Setup hs_sockets[] structures and request system resources. |
| 869 | * TODO: on memory allocation failure, power down the socket |
| 870 | * before quitting. |
| 871 | */ |
| 872 | for (i=0; i<HS_MAX_SOCKETS; i++) { |
| 873 | hs_set_voltages(&hs_sockets[i], 0, 0); |
| 874 | |
| 875 | hs_sockets[i].socket.features |= SS_CAP_PCCARD | SS_CAP_STATIC_MAP; /* mappings are fixed in host memory */ |
| 876 | hs_sockets[i].socket.resource_ops = &pccard_static_ops; |
| 877 | hs_sockets[i].socket.irq_mask = 0xffde;/*0xffff*/ /* IRQs mapped in s/w so can do any, really */ |
| 878 | hs_sockets[i].socket.map_size = HD64465_PCC_WINDOW; /* 16MB fixed window size */ |
| 879 | |
| 880 | hs_sockets[i].socket.owner = THIS_MODULE; |
| 881 | hs_sockets[i].socket.ss_entry = &hs_operations; |
| 882 | } |
| 883 | |
| 884 | i = hs_init_socket(&hs_sockets[0], |
| 885 | HD64465_IRQ_PCMCIA0, |
| 886 | HD64465_PCC0_BASE, |
| 887 | HD64465_REG_PCC0ISR); |
| 888 | if (i < 0) { |
| 889 | unregister_driver(&hd64465_driver); |
| 890 | return i; |
| 891 | } |
| 892 | i = hs_init_socket(&hs_sockets[1], |
| 893 | HD64465_IRQ_PCMCIA1, |
| 894 | HD64465_PCC1_BASE, |
| 895 | HD64465_REG_PCC1ISR); |
| 896 | if (i < 0) { |
| 897 | unregister_driver(&hd64465_driver); |
| 898 | return i; |
| 899 | } |
| 900 | |
| 901 | /* hd64465_io_debug = 0; */ |
| 902 | |
| 903 | platform_device_register(&hd64465_device); |
| 904 | |
| 905 | for (i=0; i<HS_MAX_SOCKETS; i++) { |
| 906 | unsigned int ret; |
Manuel Lauss | dfe461a | 2007-02-21 14:47:20 +0100 | [diff] [blame] | 907 | hs_sockets[i].socket.dev.parent = &hd64465_device.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | hs_sockets[i].number = i; |
| 909 | ret = pcmcia_register_socket(&hs_sockets[i].socket); |
| 910 | if (ret && i) |
| 911 | pcmcia_unregister_socket(&hs_sockets[0].socket); |
| 912 | } |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | static void __exit exit_hs(void) |
| 918 | { |
| 919 | int i; |
| 920 | |
| 921 | for (i=0 ; i<HS_MAX_SOCKETS ; i++) { |
| 922 | pcmcia_unregister_socket(&hs_sockets[i].socket); |
| 923 | hs_exit_socket(&hs_sockets[i]); |
| 924 | } |
| 925 | |
| 926 | platform_device_unregister(&hd64465_device); |
| 927 | unregister_driver(&hd64465_driver); |
| 928 | } |
| 929 | |
| 930 | module_init(init_hs); |
| 931 | module_exit(exit_hs); |
| 932 | |
| 933 | /*============================================================*/ |
| 934 | /*END*/ |