Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
/*
 * $Id$
 *
 * Device driver for the PCMCIA controller module of the
 * Hitachi HD64465 handheld companion chip.
 *
 * Note that the HD64465 provides a very thin PCMCIA host bridge
 * layer, requiring a lot of the work of supporting cards to be
 * performed by the processor.  For example: mapping of card
 * interrupts to processor IRQs is done by IRQ demuxing software;
 * IO and memory mappings are fixed; setting voltages according
 * to card Voltage Select pins etc is done in software.
 *
 * Note also that this driver uses only the simple, fixed,
 * 16MB, 16-bit wide mappings to PCMCIA spaces defined by the
 * HD64465.  Larger mappings, smaller mappings, or mappings of
 * different width to the same socket, are all possible only by
 * involving the SH7750's MMU, which is considered unnecessary here.
 * The downside is that it may be possible for some drivers to
 * break because they need or expect 8-bit mappings.
 *
 * This driver currently supports only the following configuration:
 * SH7750 CPU, HD64465, TPS2206 voltage control chip.
 *
 * by Greg Banks <gbanks@pocketpenguins.com>
 * (c) 2000 PocketPenguins Inc
 *
 */

#include <linux/types.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <asm/errno.h>
#include <linux/irq.h>

#include <asm/io.h>
#include <asm/hd64465.h>

#include <pcmcia/version.h>
#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/ss.h>
#include <pcmcia/bulkmem.h>
#include <pcmcia/cistpl.h>
#include "cs_internal.h"

#define MODNAME "hd64465_ss"

/* #define HD64465_DEBUG 1 */

#ifndef HD64465_DEBUG
#define HD64465_DEBUG 0
#endif

#if HD64465_DEBUG
#define DPRINTK(args...)	printk(MODNAME ": " args)
#else
#define DPRINTK(args...)
#endif

extern int hd64465_io_debug;


/*============================================================*/

#define HS_IO_MAP_SIZE 	(64*1024)

typedef struct hs_socket_t
{
    u_int   	    	irq;
    u_long  	    	mem_base;
    u_long  	    	mem_length;
    void	    	(*handler)(void *info, u_int events);
    void    	    	*handler_info;
    u_int   	    	pending_events;
    u_int   	    	ctrl_base;
    socket_state_t  	state;
    pccard_io_map     	io_maps[MAX_IO_WIN];
    pccard_mem_map  	mem_maps[MAX_WIN];
    struct vm_struct	*io_vma;    /* allocated kernel vm for mapping IO space */
} hs_socket_t;

#define HS_MAX_SOCKETS 2
static hs_socket_t hs_sockets[HS_MAX_SOCKETS];
static spinlock_t hs_pending_event_lock = SPIN_LOCK_UNLOCKED;

/* Calculate socket number from ptr into hs_sockets[] */
#define hs_sockno(sp) 	(sp - hs_sockets)

static socket_cap_t hs_socket_cap =
{
    SS_CAP_PCCARD   	    /* support 16 bit cards */
    |SS_CAP_STATIC_MAP      /* mappings are fixed in host memory */
    ,
    0xffde/*0xffff*/, 	    /* IRQs mapped in s/w so can do any, really */
    HD64465_PCC_WINDOW,     /* 16MB fixed window size */
    0,	    	    	    /* no PCI support */
    0,	    	    	    /* no CardBus support */
    0	    	    	    /* no bus operations needed */
};

#define hs_in(sp, r)	    inb((sp)->ctrl_base + (r))
#define hs_out(sp, v, r)    outb(v, (sp)->ctrl_base + (r))


/* translate a boolean value to a bit in a register */
#define bool_to_regbit(sp, r, bi, bo)	    	\
    do {    	    	    	    	    	\
    	unsigned short v = hs_in(sp, r);    	\
	if (bo)     	    	    	    	\
	    v |= (bi);	    	    	    	\
	else	    	    	    	    	\
	    v &= ~(bi);     	    	    	\
	hs_out(sp, v, r);   	    	    	\
    } while(0)
    
/* register offsets from HD64465_REG_PCC[01]ISR */
#define ISR 	0x0
#define GCR 	0x2
#define CSCR 	0x4
#define CSCIER 	0x6
#define SCR 	0x8


/* Mask and values for CSCIER register */
#define IER_MASK    0x80
#define IER_ON	    0x3f    	/* interrupts on */
#define IER_OFF     0x00    	/* interrupts off */

/*============================================================*/

#if HD64465_DEBUG > 10

static void cis_hex_dump(const unsigned char *x, int len)
{
    	int i;
	
    	for (i=0 ; i<len ; i++)
	{
	    if (!(i & 0xf))
	    	printk("\n%08x", (unsigned)(x + i));
	    printk(" %02x", *(volatile unsigned short*)x);
	    x += 2;
	}
	printk("\n");
}

#endif
/*============================================================*/

/*
 * This code helps create the illusion that the IREQ line from
 * the PC card is mapped to one of the CPU's IRQ lines by the
 * host bridge hardware (which is how every host bridge *except*
 * the HD64465 works).  In particular, it supports enabling
 * and disabling the IREQ line by code which knows nothing
 * about the host bridge (e.g. device drivers, IDE code) using
 * the request_irq(), free_irq(), probe_irq_on() and probe_irq_off()
 * functions.  Also, it supports sharing the mapped IRQ with
 * real hardware IRQs from the -IRL0-3 lines.
 */

#define HS_NUM_MAPPED_IRQS  16	/* Limitation of the PCMCIA code */
static struct
{
    /* index is mapped irq number */
    hs_socket_t *sock;
    hw_irq_controller *old_handler;
} hs_mapped_irq[HS_NUM_MAPPED_IRQS];

static void hs_socket_enable_ireq(hs_socket_t *sp)
{
    	unsigned short cscier;
	
    	DPRINTK("hs_socket_enable_ireq(sock=%d)\n", hs_sockno(sp));

    	cscier = hs_in(sp, CSCIER);
	cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK;
    	cscier |= HD64465_PCCCSCIER_PIREQE_LEVEL;
	hs_out(sp, cscier, CSCIER);
}

static void hs_socket_disable_ireq(hs_socket_t *sp)
{
    	unsigned short cscier;
	
    	DPRINTK("hs_socket_disable_ireq(sock=%d)\n", hs_sockno(sp));
	
    	cscier = hs_in(sp, CSCIER);
	cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK;
	hs_out(sp, cscier, CSCIER);
}

static unsigned int hs_startup_irq(unsigned int irq)
{
	hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
	hs_mapped_irq[irq].old_handler->startup(irq);
	return 0;
}

static void hs_shutdown_irq(unsigned int irq)
{
	hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
	hs_mapped_irq[irq].old_handler->shutdown(irq);
}

static void hs_enable_irq(unsigned int irq)
{
	hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
	hs_mapped_irq[irq].old_handler->enable(irq);
}

static void hs_disable_irq(unsigned int irq)
{
	hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
	hs_mapped_irq[irq].old_handler->disable(irq);
}

extern struct hw_interrupt_type no_irq_type;

static void hs_mask_and_ack_irq(unsigned int irq)
{
	hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
	/* ack_none() spuriously complains about an unexpected IRQ */
	if (hs_mapped_irq[irq].old_handler != &no_irq_type)
	    hs_mapped_irq[irq].old_handler->ack(irq);
}

static void hs_end_irq(unsigned int irq)
{
	hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
	hs_mapped_irq[irq].old_handler->end(irq);
}


static struct hw_interrupt_type hd64465_ss_irq_type = {
	typename:	"PCMCIA-IRQ",
	startup:	hs_startup_irq,
	shutdown:	hs_shutdown_irq,
	enable:		hs_enable_irq,
	disable:	hs_disable_irq,
	ack:		hs_mask_and_ack_irq,
	end:		hs_end_irq
};

/* 
 * This function should only ever be called with interrupts disabled.
 */
static void hs_map_irq(hs_socket_t *sp, unsigned int irq)
{
    	DPRINTK("hs_map_irq(sock=%d irq=%d)\n", hs_sockno(sp), irq);
	
	if (irq >= HS_NUM_MAPPED_IRQS)
	    return;

    	hs_mapped_irq[irq].sock = sp;
	/* insert ourselves as the irq controller */
	hs_mapped_irq[irq].old_handler = irq_desc[irq].handler;
	irq_desc[irq].handler = &hd64465_ss_irq_type;
}


/* 
 * This function should only ever be called with interrupts disabled.
 */
static void hs_unmap_irq(hs_socket_t *sp, unsigned int irq)
{
    	DPRINTK("hs_unmap_irq(sock=%d irq=%d)\n", hs_sockno(sp), irq);
	
	if (irq >= HS_NUM_MAPPED_IRQS)
	    return;
		
	/* restore the original irq controller */
	irq_desc[irq].handler = hs_mapped_irq[irq].old_handler;
}

/*============================================================*/


/*
 * Set Vpp and Vcc (in tenths of a Volt).  Does not
 * support the hi-Z state.
 *
 * Note, this assumes the board uses a TPS2206 chip to control
 * the Vcc and Vpp voltages to the hs_sockets.  If your board
 * uses the MIC2563 (also supported by the HD64465) then you
 * will have to modify this function.
 */
    	    	    	    	         /* 0V   3.3V  5.5V */
static const u_char hs_tps2206_avcc[3] = { 0x00, 0x04, 0x08 };
static const u_char hs_tps2206_bvcc[3] = { 0x00, 0x80, 0x40 };

static int hs_set_voltages(hs_socket_t *sp, int Vcc, int Vpp)
{
    	u_int psr;
	u_int vcci = 0;
	u_int sock = hs_sockno(sp);
	
    	DPRINTK("hs_set_voltage(%d, %d, %d)\n", sock, Vcc, Vpp);

    	switch (Vcc)
	{
	case 0:  vcci = 0; break;
	case 33: vcci = 1; break;
	case 50: vcci = 2; break;
	default: return 0;
	}

    	/* Note: Vpp = 120 not supported -- Greg Banks */
	if (Vpp != 0 && Vpp != Vcc)
	    return 0;
	
	/* The PSR register holds 8 of the 9 bits which control
	 * the TPS2206 via its serial interface.
	 */
	psr = inw(HD64465_REG_PCCPSR);
	switch (sock)
	{
	case 0:
	    psr &= 0x0f;
	    psr |= hs_tps2206_avcc[vcci];
	    psr |= (Vpp == 0 ? 0x00 : 0x02);
	    break;
	case 1:
	    psr &= 0xf0;
	    psr |= hs_tps2206_bvcc[vcci];
	    psr |= (Vpp == 0 ? 0x00 : 0x20);
	    break;
	};
	outw(psr, HD64465_REG_PCCPSR);
	
	return 1;
}


/*============================================================*/

/*
 * Drive the RESET line to the card.
 */
static void hs_reset_socket(hs_socket_t *sp, int on)
{
    	unsigned short v;
	
	v = hs_in(sp, GCR);
	if (on)
	    v |= HD64465_PCCGCR_PCCR;
	else
	    v &= ~HD64465_PCCGCR_PCCR;
	hs_out(sp, v, GCR);
}

/*============================================================*/

static int hs_init(unsigned int sock)
{
    	hs_socket_t *sp = &hs_sockets[sock];
	
    	DPRINTK("hs_init(%d)\n", sock);
	
	sp->pending_events = 0;
	sp->state.Vcc = 0;
	sp->state.Vpp = 0;
	hs_set_voltages(sp, 0, 0);
	
	return 0;
}

/*============================================================*/

static int hs_suspend(unsigned int sock)
{
    	DPRINTK("hs_suspend(%d)\n", sock);

    	/* TODO */
	
	return 0;
}

/*============================================================*/

static int hs_register_callback(unsigned int sock,
    	    void (*handler)(void *, unsigned int), void * info)
{
    	hs_socket_t *sp = &hs_sockets[sock];
	
    	DPRINTK("hs_register_callback(%d)\n", sock);
	sp->handler = handler;
	sp->handler_info = info;
	if (handler == 0) {
	    MOD_DEC_USE_COUNT;
	} else {
	    MOD_INC_USE_COUNT;
	}
	return 0;
}

/*============================================================*/

static int hs_inquire_socket(unsigned int sock, socket_cap_t *cap)
{
    	DPRINTK("hs_inquire_socket(%d)\n", sock);

	*cap = hs_socket_cap;
	return 0;
}

/*============================================================*/

static int hs_get_status(unsigned int sock, u_int *value)
{
    	hs_socket_t *sp = &hs_sockets[sock];
    	unsigned int isr;
	u_int status = 0;
	
	
	isr = hs_in(sp, ISR);

    	/* Card is seated and powered when *both* CD pins are low */
	if ((isr & HD64465_PCCISR_PCD_MASK) == 0)
    	{
	    status |= SS_DETECT;    /* card present */

	    switch (isr & HD64465_PCCISR_PBVD_MASK)
	    {
	    case HD64465_PCCISR_PBVD_BATGOOD:   
		break;
	    case HD64465_PCCISR_PBVD_BATWARN:
		status |= SS_BATWARN;
		break;
	    default:
		status |= SS_BATDEAD;
		break;
	    }

	    if (isr & HD64465_PCCISR_PREADY)
		status |= SS_READY;

	    if (isr & HD64465_PCCISR_PMWP)
		status |= SS_WRPROT;
		
	    /* Voltage Select pins interpreted as per Table 4-5 of the std.
	     * Assuming we have the TPS2206, the socket is a "Low Voltage
	     * key, 3.3V and 5V available, no X.XV available".
	     */
	    switch (isr & (HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1))
	    {
	    case HD64465_PCCISR_PVS1:
	    	printk(KERN_NOTICE MODNAME ": cannot handle X.XV card, ignored\n");
		status = 0;
	    	break;
	    case 0:
	    case HD64465_PCCISR_PVS2:
    	    	/* 3.3V */
    	    	status |= SS_3VCARD;
	    	break;
	    case HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1:
	    	/* 5V */
	    	break;
	    }
		
	    /* TODO: SS_POWERON */
	    /* TODO: SS_STSCHG */
    	}	
	
    	DPRINTK("hs_get_status(%d) = %x\n", sock, status);
	
	*value = status;
	return 0;
}

/*============================================================*/

static int hs_get_socket(unsigned int sock, socket_state_t *state)
{
    	hs_socket_t *sp = &hs_sockets[sock];

    	DPRINTK("hs_get_socket(%d)\n", sock);
	
	*state = sp->state;
	return 0;
}

/*============================================================*/

static int hs_set_socket(unsigned int sock, socket_state_t *state)
{
    	hs_socket_t *sp = &hs_sockets[sock];
    	u_long flags;
	u_int changed;
	unsigned short cscier;

    	DPRINTK("hs_set_socket(sock=%d, flags=%x, csc_mask=%x, Vcc=%d, Vpp=%d, io_irq=%d)\n",
	    sock, state->flags, state->csc_mask, state->Vcc, state->Vpp, state->io_irq);
	
	save_and_cli(flags);	/* Don't want interrupts happening here */

	if (state->Vpp != sp->state.Vpp ||
	    state->Vcc != sp->state.Vcc) {
	    if (!hs_set_voltages(sp, state->Vcc, state->Vpp)) {
	    	restore_flags(flags);
	    	return -EINVAL;
	    }
	}

/*    	hd64465_io_debug = 1; */
    	/*
	 * Handle changes in the Card Status Change mask,
	 * by propagating to the CSCR register
	 */	
	changed = sp->state.csc_mask ^ state->csc_mask;
	cscier = hs_in(sp, CSCIER);
	    
	if (changed & SS_DETECT) {
	    if (state->csc_mask & SS_DETECT)
		cscier |= HD64465_PCCCSCIER_PCDE;
	    else
		cscier &= ~HD64465_PCCCSCIER_PCDE;
	}

	if (changed & SS_READY) {
	    if (state->csc_mask & SS_READY)
		cscier |= HD64465_PCCCSCIER_PRE;
	    else
		cscier &= ~HD64465_PCCCSCIER_PRE;
	}

	if (changed & SS_BATDEAD) {
	    if (state->csc_mask & SS_BATDEAD)
		cscier |= HD64465_PCCCSCIER_PBDE;
	    else
		cscier &= ~HD64465_PCCCSCIER_PBDE;
	}

	if (changed & SS_BATWARN) {
	    if (state->csc_mask & SS_BATWARN)
		cscier |= HD64465_PCCCSCIER_PBWE;
	    else
		cscier &= ~HD64465_PCCCSCIER_PBWE;
	}

	if (changed & SS_STSCHG) {
	    if (state->csc_mask & SS_STSCHG)
		cscier |= HD64465_PCCCSCIER_PSCE;
	    else
		cscier &= ~HD64465_PCCCSCIER_PSCE;
	}

    	hs_out(sp, cscier, CSCIER);

	if (sp->state.io_irq && !state->io_irq)
	    hs_unmap_irq(sp, sp->state.io_irq);
	else if (!sp->state.io_irq && state->io_irq)
	    hs_map_irq(sp, state->io_irq);


    	/*
	 * Handle changes in the flags field,
	 * by propagating to config registers.
	 */	
	changed = sp->state.flags ^ state->flags;

	if (changed & SS_IOCARD) {
	    DPRINTK("card type: %s\n",
		    (state->flags & SS_IOCARD ? "i/o" : "memory" ));
	    bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCT,
		state->flags & SS_IOCARD);
	}

	if (changed & SS_RESET) {
	    DPRINTK("%s reset card\n",
		(state->flags & SS_RESET ? "start" : "stop"));
	    bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCR,
		state->flags & SS_RESET);
	}

	if (changed & SS_OUTPUT_ENA) {
	    DPRINTK("%sabling card output\n",
		(state->flags & SS_OUTPUT_ENA ? "en" : "dis"));
	    bool_to_regbit(sp, GCR, HD64465_PCCGCR_PDRV,
		state->flags & SS_OUTPUT_ENA);
	}

    	/* TODO: SS_SPKR_ENA */
	    
/*    	hd64465_io_debug = 0; */
	sp->state = *state;
	    
	restore_flags(flags);

#if HD64465_DEBUG > 10
	if (state->flags & SS_OUTPUT_ENA)   
	    cis_hex_dump((const unsigned char*)sp->mem_base, 0x100);
#endif
	return 0;
}

/*============================================================*/

static int hs_get_io_map(unsigned int sock, struct pccard_io_map *io)
{
    	hs_socket_t *sp = &hs_sockets[sock];
	int map = io->map;

    	DPRINTK("hs_get_io_map(%d, %d)\n", sock, map);
	if (map >= MAX_IO_WIN)
	    return -EINVAL;
	
	*io = sp->io_maps[map];
	return 0;
}

/*============================================================*/

static int hs_set_io_map(unsigned int sock, struct pccard_io_map *io)
{
    	hs_socket_t *sp = &hs_sockets[sock];
	int map = io->map;
	struct pccard_io_map *sio;
	pgprot_t prot;

    	DPRINTK("hs_set_io_map(sock=%d, map=%d, flags=0x%x, speed=%dns, start=0x%04x, stop=0x%04x)\n",
	    sock, map, io->flags, io->speed, io->start, io->stop);
	if (map >= MAX_IO_WIN)
	    return -EINVAL;
	sio = &sp->io_maps[map];

    	/* check for null changes */	
    	if (io->flags == sio->flags &&
	    io->start == sio->start &&
	    io->stop == sio->stop)
	    return 0;
	
	if (io->flags & MAP_AUTOSZ)
	    prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IODYN);
	else if (io->flags & MAP_16BIT)
	    prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO16);
	else
	    prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO8);

	/* TODO: handle MAP_USE_WAIT */
	if (io->flags & MAP_USE_WAIT)
	    printk(KERN_INFO MODNAME ": MAP_USE_WAIT unimplemented\n");
	/* TODO: handle MAP_PREFETCH */
	if (io->flags & MAP_PREFETCH)
	    printk(KERN_INFO MODNAME ": MAP_PREFETCH unimplemented\n");
	/* TODO: handle MAP_WRPROT */
	if (io->flags & MAP_WRPROT)
	    printk(KERN_INFO MODNAME ": MAP_WRPROT unimplemented\n");
	/* TODO: handle MAP_0WS */
	if (io->flags & MAP_0WS)
	    printk(KERN_INFO MODNAME ": MAP_0WS unimplemented\n");

	if (io->flags & MAP_ACTIVE) {
	    unsigned long pstart, psize, paddrbase, vaddrbase;
	    
	    paddrbase = virt_to_phys((void*)(sp->mem_base + 2 * HD64465_PCC_WINDOW));
	    vaddrbase = (unsigned long)sp->io_vma->addr;
	    pstart = io->start & PAGE_MASK;
	    psize = ((io->stop + PAGE_SIZE) & PAGE_MASK) - pstart;

    	    /*
	     * Change PTEs in only that portion of the mapping requested
	     * by the caller.  This means that most of the time, most of
	     * the PTEs in the io_vma will be unmapped and only the bottom
	     * page will be mapped.  But the code allows for weird cards
	     * that might want IO ports > 4K.
	     */
	    DPRINTK("remap_page_range(vaddr=0x%08lx, paddr=0x%08lx, size=0x%08lxx)\n",
	    	vaddrbase + pstart, paddrbase + pstart, psize);
#error This does not work.  Firstly remap_page_range() uses current->mm for
#error the address space, which is wrong for kernel mappings.  remap_page_range
#error also does flush_{cache,tlb}_range() which ONLY works for user mappings.
#error Next, remap_page_range() now wants to take a vm_area_struct arg.
	    remap_page_range(vaddrbase + pstart, paddrbase + pstart, psize, prot);
	    
	    /*
	     * Change the mapping used by inb() outb() etc
	     */
	    hd64465_port_map(
	    	io->start,
		io->stop - io->start + 1,
	    	vaddrbase + io->start,0);
	} else {
	    hd64465_port_unmap(
	    	sio->start,
		sio->stop - sio->start + 1);
	    /* TODO: remap_page_range() to mark pages not present ? */
	}
	
	*sio = *io;
	return 0;
}

/*============================================================*/

static int hs_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
{
    	hs_socket_t *sp = &hs_sockets[sock];
	int map = mem->map;

    	DPRINTK("hs_get_mem_map(%d, %d)\n", sock, map);
	if (map >= MAX_WIN)
	    return -EINVAL;
	
	*mem = sp->mem_maps[map];
	return 0;
}

/*============================================================*/

static int hs_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
{
    	hs_socket_t *sp = &hs_sockets[sock];
	struct pccard_mem_map *smem;
	int map = mem->map;
	unsigned long paddr, size;

#if 0
    	DPRINTK("hs_set_mem_map(sock=%d, map=%d, flags=0x%x, sys_start=0x%08lx, sys_end=0x%08lx, card_start=0x%08x)\n",
	    sock, map, mem->flags, mem->sys_start, mem->sys_stop, mem->card_start);
#endif

	if (map >= MAX_WIN)
	    return -EINVAL;
	smem = &sp->mem_maps[map];
	
	size = mem->sys_stop - mem->sys_start + 1;
	
	paddr = sp->mem_base;	    	    /* base of Attribute mapping */
	if (!(mem->flags & MAP_ATTRIB))
	    paddr += HD64465_PCC_WINDOW;    /* base of Common mapping */
	paddr += mem->card_start;

    	/* Because we specified SS_CAP_STATIC_MAP, we are obliged
	 * at this time to report the system address corresponding
	 * to the card address requested.  This is how Socket Services
	 * queries our fixed mapping.  I wish this fact had been
	 * documented - Greg Banks.
	 */
    	mem->sys_start = paddr;
	mem->sys_stop = paddr + size - 1;
	
	*smem = *mem;
	
    	return 0;
}

/* TODO: do we need to use the MMU to access Common memory ??? */

/*============================================================*/

static void hs_proc_setup(unsigned int sock, struct proc_dir_entry *base)
{
    	DPRINTK("hs_proc_setup(%d)\n", sock);
}

/*============================================================*/

/*
 * This function is registered with the HD64465 glue code to do a
 * secondary demux step on the PCMCIA interrupts.  It handles 
 * mapping the IREQ request from the card to a standard Linux
 * IRQ, as requested by SocketServices.
 */
static int hs_irq_demux(int irq, void *dev)
{
    	hs_socket_t *sp = (hs_socket_t *)dev;
	u_int cscr;
    	
    	DPRINTK("hs_irq_demux(irq=%d)\n", irq);

    	if (sp->state.io_irq &&
	    (cscr = hs_in(sp, CSCR)) & HD64465_PCCCSCR_PIREQ) {
	    cscr &= ~HD64465_PCCCSCR_PIREQ;
	    hs_out(sp, cscr, CSCR);
	    return sp->state.io_irq;
	}
	    
	return irq;
}

/*============================================================*/

/*
 * Interrupt handling routine.
 *
 * This uses the schedule_task() technique to cause reportable events
 * such as card insertion and removal to be handled in keventd's
 * process context.
 */
 

static void hs_events_bh(void *dummy)
{
	hs_socket_t *sp;
	u_int events;
	int i;

	for (i=0; i<HS_MAX_SOCKETS; i++) {
	    sp = &hs_sockets[i];

	    spin_lock_irq(&hs_pending_event_lock);
	    events = sp->pending_events;
	    sp->pending_events = 0;
	    spin_unlock_irq(&hs_pending_event_lock);
	    
	    if (sp->handler)
		sp->handler(sp->handler_info, events);
	}
}

static struct tq_struct hs_events_task = {
	routine:	hs_events_bh
};

static void hs_interrupt(int irq, void *dev, struct pt_regs *regs)
{
    	hs_socket_t *sp = (hs_socket_t *)dev;
	u_int events = 0;
	u_int cscr;
	
	
	cscr = hs_in(sp, CSCR);
	
	DPRINTK("hs_interrupt, cscr=%04x\n", cscr);

	/* check for bus-related changes to be reported to Socket Services */
	if (cscr & HD64465_PCCCSCR_PCDC) {
	    /* double-check for a 16-bit card, as we don't support CardBus */
	    if ((hs_in(sp, ISR) & HD64465_PCCISR_PCD_MASK) != 0) {
	    	printk(KERN_NOTICE MODNAME
		    ": socket %d, card not a supported card type or not inserted correctly\n",
		    hs_sockno(sp));
		/* Don't do the rest unless a card is present */
		cscr &= ~(HD64465_PCCCSCR_PCDC|
		    	  HD64465_PCCCSCR_PRC|
			  HD64465_PCCCSCR_PBW|
		    	  HD64465_PCCCSCR_PBD|
			  HD64465_PCCCSCR_PSC);
	    } else {
	    	cscr &= ~HD64465_PCCCSCR_PCDC;
		events |= SS_DETECT;    	/* card insertion or removal */
    	    }
	}
	if (cscr & HD64465_PCCCSCR_PRC) {
	    cscr &= ~HD64465_PCCCSCR_PRC;
	    events |= SS_READY;     	/* ready signal changed */
	}
	if (cscr & HD64465_PCCCSCR_PBW) {
	    cscr &= ~HD64465_PCCCSCR_PSC;
	    events |= SS_BATWARN;     	/* battery warning */
	}
	if (cscr & HD64465_PCCCSCR_PBD) {
	    cscr &= ~HD64465_PCCCSCR_PSC;
	    events |= SS_BATDEAD;     	/* battery dead */
	}
	if (cscr & HD64465_PCCCSCR_PSC) {
	    cscr &= ~HD64465_PCCCSCR_PSC;
	    events |= SS_STSCHG;     	/* STSCHG (status changed) signal */
	}
	
	if (cscr & HD64465_PCCCSCR_PIREQ) {
	    cscr &= ~HD64465_PCCCSCR_PIREQ;

    	    /* This should have been dealt with during irq demux */	    
	    printk(KERN_NOTICE MODNAME ": unexpected IREQ from card\n");
	}

	hs_out(sp, cscr, CSCR);

	if (events) {
	    /*
    	     * Arrange for events to be reported to the registered
	     * event handler function (from CardServices) in a process
	     * context (keventd) "soon".
	     */
	    spin_lock(&hs_pending_event_lock);
	    sp->pending_events |= events;
	    spin_unlock(&hs_pending_event_lock);
	    
	    schedule_task(&hs_events_task);
	}
}

/*============================================================*/

static struct pccard_operations hs_operations = {
	hs_init,
	hs_suspend,
	hs_register_callback,
	hs_inquire_socket,
	hs_get_status,
	hs_get_socket,
	hs_set_socket,
	hs_get_io_map,
	hs_set_io_map,
	hs_get_mem_map,
	hs_set_mem_map,
	hs_proc_setup
};

static int hs_init_socket(hs_socket_t *sp, int irq, unsigned long mem_base,
    	    unsigned int ctrl_base)
{
    	unsigned short v;
    	int i, err;

    	memset(sp, 0, sizeof(*sp));
	sp->irq = irq;
	sp->mem_base = mem_base;
	sp->mem_length = 4*HD64465_PCC_WINDOW;	/* 16MB */
	sp->ctrl_base = ctrl_base;
	
	for (i=0 ; i<MAX_IO_WIN ; i++)
	    sp->io_maps[i].map = i;
	for (i=0 ; i<MAX_WIN ; i++)
	    sp->mem_maps[i].map = i;
	
	if ((sp->io_vma = get_vm_area(HS_IO_MAP_SIZE, VM_IOREMAP)) == 0)
	    return -ENOMEM;

	hd64465_register_irq_demux(sp->irq, hs_irq_demux, sp);
	
    	if ((err = request_irq(sp->irq, hs_interrupt, SA_INTERRUPT, MODNAME, sp)) < 0)
	    return err;
    	if (request_mem_region(sp->mem_base, sp->mem_length, MODNAME) == 0) {
    	    sp->mem_base = 0;
	    return -ENOMEM;
	}


	/* According to section 3.2 of the PCMCIA standard, low-voltage
	 * capable cards must implement cold insertion, i.e. Vpp and
	 * Vcc set to 0 before card is inserted.
	 */
	/*hs_set_voltages(sp, 0, 0);*/
	
	/* hi-Z the outputs to the card and set 16MB map mode */
	v = hs_in(sp, GCR);
	v &= ~HD64465_PCCGCR_PCCT;  	/* memory-only card */
	hs_out(sp, v, GCR);

	v = hs_in(sp, GCR);
	v |= HD64465_PCCGCR_PDRV;   	/* enable outputs to card */
	hs_out(sp, v, GCR);

	v = hs_in(sp, GCR);
	v |= HD64465_PCCGCR_PMMOD; 	/* 16MB mapping mode */
	hs_out(sp, v, GCR);

	v = hs_in(sp, GCR);
	/* lowest 16MB of Common */
	v &= ~(HD64465_PCCGCR_PPA25|HD64465_PCCGCR_PPA24); 
	hs_out(sp, v, GCR);
	
	hs_reset_socket(sp, 1);

    	return 0;
}

static void hs_exit_socket(hs_socket_t *sp)
{
    	unsigned short cscier, gcr;
	
	/* turn off interrupts in hardware */
    	cscier = hs_in(sp, CSCIER);
	cscier = (cscier & IER_MASK) | IER_OFF;
    	hs_out(sp, cscier, CSCIER);
	
	/* hi-Z the outputs to the card */
    	gcr = hs_in(sp, GCR);
	gcr &= HD64465_PCCGCR_PDRV;
	hs_out(sp, gcr, GCR);

    	/* power the card down */
	hs_set_voltages(sp, 0, 0);

    	if (sp->mem_base != 0)
	    release_mem_region(sp->mem_base, sp->mem_length);
	if (sp->irq != 0) {
	    free_irq(sp->irq, hs_interrupt);
    	    hd64465_unregister_irq_demux(sp->irq);
	}
	if (sp->io_vma != 0)
	    vfree(sp->io_vma->addr);
}


static int __init init_hs(void)
{
	servinfo_t serv;
	int i;
	unsigned short v;

    	/*
	 * Check API version
	 */
	pcmcia_get_card_services_info(&serv);
	if (serv.Revision != CS_RELEASE_CODE) {
	    printk(KERN_NOTICE MODNAME ": Card Services release does not match!\n");
	    return -ENODEV;
	}

/*	hd64465_io_debug = 1; */
	
	/* Wake both sockets out of STANDBY mode */
	/* TODO: wait 15ms */
	v = inw(HD64465_REG_SMSCR);
	v &= ~(HD64465_SMSCR_PC0ST|HD64465_SMSCR_PC1ST);
	outw(v, HD64465_REG_SMSCR);

	/* keep power controller out of shutdown mode */
	v = inb(HD64465_REG_PCC0SCR);
	v |= HD64465_PCCSCR_SHDN;
	outb(v, HD64465_REG_PCC0SCR);

    	/* use serial (TPS2206) power controller */
	v = inb(HD64465_REG_PCC0CSCR);
	v |= HD64465_PCCCSCR_PSWSEL;
	outb(v, HD64465_REG_PCC0CSCR);

    	hs_set_voltages(&hs_sockets[0], 0, 0);
    	hs_set_voltages(&hs_sockets[1], 0, 0);
	
	/*
	 * Setup hs_sockets[] structures and request system resources.
	 * TODO: on memory allocation failure, power down the socket
	 *       before quitting.
	 */
	i = hs_init_socket(&hs_sockets[0],
	    HD64465_IRQ_PCMCIA0,
	    HD64465_PCC0_BASE,
	    HD64465_REG_PCC0ISR);
	if (i < 0)
	    return i;
	i = hs_init_socket(&hs_sockets[1],
	    HD64465_IRQ_PCMCIA1,
	    HD64465_PCC1_BASE,
	    HD64465_REG_PCC1ISR);
	if (i < 0)
	    return i;

/*	hd64465_io_debug = 0; */
	    

	if (register_ss_entry(HS_MAX_SOCKETS, &hs_operations) != 0) {
	    for (i=0 ; i<HS_MAX_SOCKETS ; i++)
		hs_exit_socket(&hs_sockets[i]);
    	    return -ENODEV;
	}

	printk(KERN_INFO "HD64465 PCMCIA bridge:\n");
	for (i=0 ; i<HS_MAX_SOCKETS ; i++) {
	    hs_socket_t *sp = &hs_sockets[i];
	    
	    printk(KERN_INFO "  socket %d at 0x%08lx irq %d io window %ldK@0x%08lx\n",
	    	i, sp->mem_base, sp->irq,
		sp->io_vma->size>>10, (unsigned long)sp->io_vma->addr);
	}

    	return 0;
}

static void __exit exit_hs(void)
{
    	u_long flags;
	int i;
	
	save_and_cli(flags);

    	/*
	 * Release kernel resources
	 */
	for (i=0 ; i<HS_MAX_SOCKETS ; i++)
	    hs_exit_socket(&hs_sockets[i]);
	unregister_ss_entry(&hs_operations);
	
	restore_flags(flags);
}

module_init(init_hs);
module_exit(exit_hs);

/*============================================================*/
/*END*/