3 Copyright (C) 2000-2005 Neil Cafferkey
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <exec/types.h>
24 #include <utility/tagitem.h>
25 #include <libraries/pccard.h>
27 #include <proto/exec.h>
28 #include <proto/utility.h>
29 #include <proto/cardres.h>
30 #include <proto/pccard.h>
35 #include "pccard_protos.h"
36 #include "unit_protos.h"
38 #define MAX_TUPLE_SIZE 0xff
39 #define TUPLE_BUFFER_SIZE (MAX_TUPLE_SIZE + 8)
40 #define HANDLE_PRIORITY 10
41 #define IO_WINDOW_SIZE 0x10
47 struct CardHandle
*card_handle
;
51 UWORD resource_version
;
56 /* Private prototypes */
58 static struct DevUnit
*FindPCCardUnit(ULONG index
, struct DevBase
*base
);
59 static struct DevUnit
*CreatePCCardUnit(ULONG index
,
60 struct DevBase
*base
);
61 static struct BusContext
*AllocCard(struct DevBase
*base
);
62 static VOID
FreeCard(struct BusContext
*context
, struct DevBase
*base
);
63 static BOOL
IsCardCompatible(struct BusContext
*context
,
64 struct DevBase
*base
);
65 static BOOL
InitialiseCard(struct BusContext
*context
,
66 struct DevBase
*base
);
67 static VOID
CardRemovedHook(struct BusContext
*context
,
68 struct DevBase
*base
);
69 static BOOL
CardInsertedHook(struct BusContext
*context
,
70 struct DevBase
*base
);
71 static VOID
CardRemovedInt(REG(a1
, struct BusContext
*context
),
72 REG(a6
, APTR int_code
));
73 static VOID
CardInsertedInt(REG(a1
, struct BusContext
*context
),
74 REG(a6
, APTR int_code
));
75 static UBYTE
CardStatusInt(REG(d0
, UBYTE mask
),
76 REG(a1
, struct BusContext
*context
), REG(a6
, APTR int_code
));
77 static UBYTE
ByteInHook(struct BusContext
*context
, ULONG offset
);
78 static ULONG
LongInHook(struct BusContext
*context
, ULONG offset
);
79 static VOID
ByteOutHook(struct BusContext
*context
, ULONG offset
,
81 static VOID
WordOutHook(struct BusContext
*context
, ULONG offset
,
83 static VOID
LongOutHook(struct BusContext
*context
, ULONG offset
,
85 static VOID
LongsInHook(struct BusContext
*context
, ULONG offset
,
86 ULONG
*buffer
, ULONG count
);
87 static VOID
LongsOutHook(struct BusContext
*context
, ULONG offset
,
88 const ULONG
*buffer
, ULONG count
);
89 static VOID
BEWordOutHook(struct BusContext
*context
, ULONG offset
,
91 static UWORD
LEWordInHook(struct BusContext
*context
, ULONG offset
);
92 static ULONG
LELongInHook(struct BusContext
*context
, ULONG offset
);
93 static VOID
LEWordOutHook(struct BusContext
*context
, ULONG offset
,
95 static VOID
LELongOutHook(struct BusContext
*context
, ULONG offset
,
99 static const ULONG product_codes
[] =
109 static const struct TagItem unit_tags
[] =
111 {IOTAG_ByteIn
, (UPINT
)ByteInHook
},
112 {IOTAG_LongIn
, (UPINT
)LongInHook
},
113 {IOTAG_ByteOut
, (UPINT
)ByteOutHook
},
114 {IOTAG_WordOut
, (UPINT
)WordOutHook
},
115 {IOTAG_LongOut
, (UPINT
)LongOutHook
},
116 {IOTAG_LongsIn
, (UPINT
)LongsInHook
},
117 {IOTAG_LongsOut
, (UPINT
)LongsOutHook
},
118 {IOTAG_BEWordOut
, (UPINT
)BEWordOutHook
},
119 {IOTAG_LEWordIn
, (UPINT
)LEWordInHook
},
120 {IOTAG_LELongIn
, (UPINT
)LELongInHook
},
121 {IOTAG_LEWordOut
, (UPINT
)LEWordOutHook
},
122 {IOTAG_LELongOut
, (UPINT
)LELongOutHook
},
127 /****i* etherlink3.device/GetPCCardCount ***********************************
130 * GetPCCardCount -- Get the number of compatible PC Cards.
133 * count = GetPCCardCount()
135 * ULONG GetPCCardCount();
137 ****************************************************************************
141 ULONG
GetPCCardCount(struct DevBase
*base
)
144 struct BusContext
*context
;
146 if(CardResource
!= NULL
)
148 if(FindPCCardUnit(0, base
) != NULL
)
152 context
= AllocCard(base
);
156 FreeCard(context
, base
);
165 /****i* etherlink3.device/GetPCCardUnit ************************************
168 * GetPCCardUnit -- Get a unit by number.
171 * unit = GetPCCardUnit(index)
173 * struct DevUnit *GetPCCardUnit(ULONG);
175 ****************************************************************************
179 struct DevUnit
*GetPCCardUnit(ULONG index
, struct DevBase
*base
)
181 struct DevUnit
*unit
;
183 unit
= FindPCCardUnit(index
, base
);
187 unit
= CreatePCCardUnit(index
, base
);
189 AddTail((APTR
)&base
->pccard_units
, (APTR
)unit
);
197 /****i* etherlink3.device/FindPCCardUnit ***********************************
200 * FindPCCardUnit -- Find a unit by number.
203 * unit = FindPCCardUnit(unit_num)
205 * struct DevUnit *FindPCCardUnit(ULONG);
207 ****************************************************************************
211 static struct DevUnit
*FindPCCardUnit(ULONG index
, struct DevBase
*base
)
213 struct DevUnit
*unit
, *tail
;
215 unit
= (APTR
)base
->pccard_units
.mlh_Head
;
216 tail
= (APTR
)&base
->pccard_units
.mlh_Tail
;
225 /****i* etherlink3.device/CreatePCCardUnit *********************************
228 * CreatePCCardUnit -- Create a unit.
231 * unit = CreatePCCardUnit(index)
233 * struct DevUnit *CreatePCCardUnit(ULONG);
236 * Creates a new unit.
238 ****************************************************************************
242 static struct DevUnit
*CreatePCCardUnit(ULONG index
,
243 struct DevBase
*base
)
246 struct BusContext
*context
;
247 struct DevUnit
*unit
;
249 /* Get card from system */
251 context
= AllocCard(base
);
255 /* Prepare card for use */
259 if(!InitialiseCard(context
, base
))
263 /* Create device driver unit */
267 context
->unit
= unit
=
268 CreateUnit(index
, context
, unit_tags
, SECOND_GEN
,
276 unit
->insertion_function
= (APTR
)CardInsertedHook
;
277 unit
->removal_function
= (APTR
)CardRemovedHook
;
284 DeleteUnit(context
->unit
, base
);
285 FreeCard(context
, base
);
295 /****i* etherlink3.device/DeletePCCardUnit *********************************
298 * DeletePCCardUnit -- Delete a unit.
301 * DeletePCCardUnit(unit)
303 * VOID DeletePCCardUnit(struct DevUnit *);
309 * unit - Device unit (may be NULL).
314 ****************************************************************************
318 VOID
DeletePCCardUnit(struct DevUnit
*unit
, struct DevBase
*base
)
320 struct BusContext
*context
;
324 context
= unit
->card
;
325 DeleteUnit(unit
, base
);
326 FreeCard(context
, base
);
334 /****i* etherlink3.device/AllocCard ****************************************
337 * AllocCard -- Get card from system.
342 * struct BusContext *AllocCard();
344 ****************************************************************************
348 static struct BusContext
*AllocCard(struct DevBase
*base
)
350 BOOL success
= TRUE
, have_card
= FALSE
;
351 struct BusContext
*context
;
352 struct CardHandle
*card_handle
;
353 struct Interrupt
*card_removed_int
, *card_inserted_int
, *card_status_int
;
355 context
= AllocMem(sizeof(struct BusContext
), MEMF_PUBLIC
| MEMF_CLEAR
);
361 context
->resource_version
=
362 ((struct Library
*)base
->card_base
)->lib_Version
;
363 context
->card_handle
= card_handle
=
364 AllocMem(sizeof(struct CardHandle
), MEMF_PUBLIC
| MEMF_CLEAR
);
365 context
->tuple_buffer
=
366 AllocVec(TUPLE_BUFFER_SIZE
, MEMF_PUBLIC
);
368 if(card_handle
== NULL
|| context
->tuple_buffer
== NULL
)
374 /* Set up card handle */
376 card_handle
->cah_CardNode
.ln_Pri
= HANDLE_PRIORITY
;
377 card_handle
->cah_CardNode
.ln_Name
=
378 base
->device
.dd_Library
.lib_Node
.ln_Name
;
379 /* card_handle->cah_CardFlags = CARDF_IFAVAILABLE | CARDF_POSTSTATUS;*/
380 card_handle
->cah_CardFlags
= CARDF_POSTSTATUS
;
382 card_handle
->cah_CardRemoved
= card_removed_int
=
383 AllocVec(sizeof(struct Interrupt
), MEMF_PUBLIC
| MEMF_CLEAR
);
385 card_handle
->cah_CardInserted
= card_inserted_int
=
386 AllocVec(sizeof(struct Interrupt
), MEMF_PUBLIC
| MEMF_CLEAR
);
388 card_handle
->cah_CardStatus
= card_status_int
=
389 AllocVec(sizeof(struct Interrupt
), MEMF_PUBLIC
| MEMF_CLEAR
);
391 if(card_removed_int
== NULL
|| card_inserted_int
== NULL
392 || card_status_int
== NULL
)
398 /* Try to gain access to card */
400 card_removed_int
->is_Code
= CardRemovedInt
;
401 card_removed_int
->is_Data
= context
;
402 card_inserted_int
->is_Code
= CardInsertedInt
;
403 card_inserted_int
->is_Data
= context
;
404 card_status_int
->is_Code
= (APTR
)CardStatusInt
;
405 card_status_int
->is_Data
= context
;
407 if(OwnCard(card_handle
) != 0)
414 if(!IsCardCompatible(context
, base
))
420 FreeCard(context
, base
);
428 /****i* etherlink3.device/FreeCard *****************************************
436 * VOID FreeCard(struct BusContext *);
438 ****************************************************************************
442 static VOID
FreeCard(struct BusContext
*context
, struct DevBase
*base
)
444 struct CardHandle
*card_handle
;
448 card_handle
= context
->card_handle
;
450 if(context
->have_card
)
452 CardMiscControl(card_handle
, 0);
453 CardResetCard(card_handle
);
455 ReleaseCard(card_handle
, CARDF_REMOVEHANDLE
);
457 FreeVec(card_handle
->cah_CardStatus
);
458 FreeVec(card_handle
->cah_CardInserted
);
459 FreeVec(card_handle
->cah_CardRemoved
);
460 FreeVec(context
->tuple_buffer
);
461 FreeMem(card_handle
, sizeof(struct CardHandle
));
463 FreeMem(context
, sizeof(struct BusContext
));
471 /****i* etherlink3.device/IsCardCompatible *********************************
477 * compatible = IsCardCompatible(context)
479 * BOOL IsCardCompatible(struct BusContext *);
481 ****************************************************************************
485 static BOOL
IsCardCompatible(struct BusContext
*context
,
486 struct DevBase
*base
)
489 struct CardHandle
*card_handle
;
491 const struct TagItem
*tuple_tags
= NULL
;
494 UWORD maker
= 0, product
= 0;
496 card_handle
= context
->card_handle
;
497 tuple_buffer
= context
->tuple_buffer
;
499 /* Get card's make and model */
501 if(CopyTuple(card_handle
, tuple_buffer
, PCCARD_TPL_MANFID
,
504 tuple_tags
= PCCard_GetTupleInfo(tuple_buffer
);
505 if(tuple_tags
!= NULL
)
507 maker
= GetTagData(PCCARD_Maker
, 0, tuple_tags
);
508 product
= GetTagData(PCCARD_Product
, 0, tuple_tags
);
512 /* Check this is a card we can use */
514 code
= maker
<< 16 | product
;
515 for(success
= FALSE
, p
= product_codes
; *p
!= 0; p
++)
518 PCCard_FreeTupleInfo(tuple_tags
);
525 /****i* etherlink3.device/InitialiseCard ***********************************
531 * success = InitialiseCard(context)
533 * BOOL InitialiseCard(struct BusContext *);
535 ****************************************************************************
539 static BOOL
InitialiseCard(struct BusContext
*context
,
540 struct DevBase
*base
)
543 struct CardHandle
*card_handle
;
544 struct CardMemoryMap
*card_map
;
545 UBYTE config_value
, i
, window_count
, *tuple_buffer
;
546 const struct TagItem
*tuple_tags
= NULL
;
547 ULONG
*io_bases
, *io_lengths
, io_base_offset
= 0, config_base_offset
;
549 /* Wake up card's I/O functionality */
551 card_handle
= context
->card_handle
;
552 tuple_buffer
= context
->tuple_buffer
;
553 CardMiscControl(card_handle
,
554 CARD_ENABLEF_DIGAUDIO
| CARD_DISABLEF_WP
);
556 /* Get configuration data */
558 if(!CopyTuple(card_handle
, tuple_buffer
, PCCARD_TPL_CONFIG
,
564 PCCard_FreeTupleInfo(tuple_tags
);
565 tuple_tags
= PCCard_GetTupleInfo(tuple_buffer
);
566 if(tuple_tags
== NULL
)
572 config_base_offset
= GetTagData(PCCARD_RegisterBase
, 0, tuple_tags
);
574 PCCard_FreeTupleInfo(tuple_tags
);
579 if(!CopyTuple(card_handle
, tuple_buffer
, PCCARD_TPL_CFTABLEENTRY
,
586 tuple_tags
= PCCard_GetTupleInfo(tuple_buffer
);
587 if(tuple_tags
== NULL
)
593 config_value
= GetTagData(PCCARD_ModeNo
, 0, tuple_tags
);
596 (APTR
)GetTagData(PCCARD_IOWinBases
, (UPINT
)NULL
, tuple_tags
);
601 /* Find the appropriate IO window */
606 (APTR
)GetTagData(PCCARD_IOWinLengths
, (UPINT
)NULL
, tuple_tags
);
608 window_count
= GetTagData(PCCARD_IOWinCount
, 0, tuple_tags
);
610 for(i
= 0; (i
< window_count
) && (io_base_offset
== 0); i
++)
611 if(io_lengths
[i
] == IO_WINDOW_SIZE
)
612 io_base_offset
= io_bases
[i
];
615 PCCard_FreeTupleInfo(tuple_tags
);
621 card_map
= GetCardMap();
622 context
->config_base
=
623 (UPINT
)card_map
->cmm_AttributeMemory
+ config_base_offset
;
625 context
->io_base
= (UPINT
)card_map
->cmm_IOMemory
+ io_base_offset
;
626 BYTEOUT(context
->config_base
+ PCCARD_REG_COR
, config_value
);
627 BYTEOUT(context
->config_base
+ PCCARD_REG_CCSR
,
628 BYTEIN(context
->config_base
+ PCCARD_REG_CCSR
)
629 | PCCARD_REG_CCSRF_AUDIOENABLE
);
637 /****i* etherlink3.device/CardRemovedHook **********************************
643 * CardRemovedHook(context)
645 * VOID CardRemovedHook(struct BusContext *);
647 ****************************************************************************
651 static VOID
CardRemovedHook(struct BusContext
*context
,
652 struct DevBase
*base
)
654 ReleaseCard(context
->card_handle
, 0);
661 /****i* etherlink3.device/CardInsertedHook *********************************
667 * success = CardInsertedHook(context)
669 * BOOL CardInsertedHook(struct BusContext *);
671 ****************************************************************************
675 static BOOL
CardInsertedHook(struct BusContext
*context
,
676 struct DevBase
*base
)
680 success
= IsCardCompatible(context
, base
);
683 success
= InitialiseCard(context
, base
);
686 success
= InitialiseAdapter(context
->unit
, TRUE
, base
);
689 ReleaseCard(context
->card_handle
, 0);
696 /****i* etherlink3.device/CardRemovedInt ***********************************
702 * CardRemovedInt(unit)
704 * VOID CardRemovedInt(struct DevUnit *);
706 ****************************************************************************
710 static VOID
CardRemovedInt(REG(a1
, struct BusContext
*context
),
711 REG(a6
, APTR int_code
))
713 struct DevBase
*base
;
714 struct DevUnit
*unit
;
716 /* Record loss of card and get our task to call ReleaseCard() */
718 unit
= context
->unit
;
720 if((unit
->flags
& UNITF_ONLINE
) != 0)
721 unit
->flags
|= UNITF_WASONLINE
;
722 unit
->flags
&= ~(UNITF_HAVEADAPTER
| UNITF_ONLINE
);
723 context
->have_card
= FALSE
;
724 Signal(unit
->task
, unit
->card_removed_signal
);
731 /****i* etherlink3.device/CardInsertedInt **********************************
737 * CardInsertedInt(unit)
739 * VOID CardInsertedInt(struct DevUnit *);
741 ****************************************************************************
745 static VOID
CardInsertedInt(REG(a1
, struct BusContext
*context
),
746 REG(a6
, APTR int_code
))
748 struct DevBase
*base
;
749 struct DevUnit
*unit
;
751 unit
= context
->unit
;
753 context
->have_card
= TRUE
;
754 Signal(unit
->task
, unit
->card_inserted_signal
);
761 /****i* etherlink3.device/CardStatusInt ************************************
767 * mask = CardStatusInt(mask, unit)
769 * UBYTE CardStatusInt(UBYTE mask, struct DevUnit *);
771 ****************************************************************************
773 * We pretend the int_code parameter goes in A6 rather than A5 because 68k
774 * GCC can't cope with A5 and we know the parameter isn't used in this case.
778 static UBYTE
CardStatusInt(REG(d0
, UBYTE mask
),
779 REG(a1
, struct BusContext
*context
), REG(a6
, APTR int_code
))
781 if(context
->resource_version
< 39)
783 /* Work around gayle interrupt bug */
785 *((volatile UBYTE
*)0xda9000) = (mask
^ 0x2c) | 0xc0;
789 if(context
->unit
!= NULL
)
790 StatusInt(context
->unit
, StatusInt
);
797 /****i* etherlink3.device/ByteInHook ***************************************
803 * value = ByteInHook(context, offset)
805 * UBYTE ByteInHook(struct BusContext *, ULONG);
807 ****************************************************************************
811 static UBYTE
ByteInHook(struct BusContext
*context
, ULONG offset
)
813 return BYTEIN(context
->io_base
+ offset
);
818 /****i* etherlink3.device/LongInHook ***************************************
824 * value = LongInHook(context, offset)
826 * ULONG LongInHook(struct BusContext *, ULONG);
828 ****************************************************************************
832 static ULONG
LongInHook(struct BusContext
*context
, ULONG offset
)
834 return LONGIN(context
->io_base
+ offset
);
839 /****i* etherlink3.device/ByteOutHook **************************************
845 * ByteOutHook(context, offset, value)
847 * VOID ByteOutHook(struct BusContext *, ULONG, UBYTE);
849 ****************************************************************************
853 static VOID
ByteOutHook(struct BusContext
*context
, ULONG offset
,
856 BYTEOUT(context
->io_base
+ offset
, value
);
863 /****i* etherlink3.device/WordOutHook **************************************
869 * WordOutHook(context, offset, value)
871 * VOID WordOutHook(struct BusContext *, ULONG, UWORD);
873 ****************************************************************************
877 static VOID
WordOutHook(struct BusContext
*context
, ULONG offset
,
880 WORDOUT(context
->io_base
+ offset
, value
);
887 /****i* etherlink3.device/LongOutHook **************************************
893 * LongOutHook(context, offset, value)
895 * VOID LongOutHook(struct BusContext *, ULONG, ULONG);
897 ****************************************************************************
901 static VOID
LongOutHook(struct BusContext
*context
, ULONG offset
,
904 LONGOUT(context
->io_base
+ offset
, value
);
911 /****i* etherlink3.device/LongsInHook **************************************
917 * LongsInHook(context, offset, buffer, count)
919 * VOID LongsInHook(struct BusContext *, ULONG, ULONG *, ULONG);
921 ****************************************************************************
925 static VOID
LongsInHook(struct BusContext
*context
, ULONG offset
,
926 ULONG
*buffer
, ULONG count
)
928 LONGSIN(context
->io_base
+ offset
, buffer
, count
);
935 /****i* etherlink3.device/LongsOutHook *************************************
941 * LongsOutHook(context, offset, buffer, count)
943 * VOID LongsOutHook(struct BusContext *, ULONG, const ULONG *, ULONG);
945 ****************************************************************************
949 static VOID
LongsOutHook(struct BusContext
*context
, ULONG offset
,
950 const ULONG
*buffer
, ULONG count
)
952 LONGSOUT(context
->io_base
+ offset
, buffer
, count
);
959 /****i* etherlink3.device/BEWordOutHook ************************************
965 * BEWordOutHook(context, offset, value)
967 * VOID BEWordOutHook(struct BusContext *, ULONG, UWORD);
969 ****************************************************************************
973 static VOID
BEWordOutHook(struct BusContext
*context
, ULONG offset
,
976 BEWORDOUT(context
->io_base
+ offset
, value
);
983 /****i* etherlink3.device/LEWordInHook *************************************
989 * value = LEWordInHook(context, offset)
991 * UWORD LEWordInHook(struct BusContext *, ULONG);
993 ****************************************************************************
997 static UWORD
LEWordInHook(struct BusContext
*context
, ULONG offset
)
999 return LEWORDIN(context
->io_base
+ offset
);
1004 /****i* etherlink3.device/LELongInHook ***************************************
1010 * value = LELongInHook(context, offset)
1012 * ULONG LELongInHook(struct BusContext *, ULONG);
1014 ****************************************************************************
1018 static ULONG
LELongInHook(struct BusContext
*context
, ULONG offset
)
1020 return LELONGIN(context
->io_base
+ offset
);
1025 /****i* etherlink3.device/LEWordOutHook ************************************
1031 * LEWordOutHook(context, offset, value)
1033 * VOID LEWordOutHook(struct BusContext *, ULONG, UWORD);
1035 ****************************************************************************
1039 static VOID
LEWordOutHook(struct BusContext
*context
, ULONG offset
,
1042 LEWORDOUT(context
->io_base
+ offset
, value
);
1049 /****i* etherlink3.device/LELongOutHook ************************************
1055 * LELongOutHook(context, offset, value)
1057 * VOID LELongOutHook(struct BusContext *, ULONG, ULONG);
1059 ****************************************************************************
1063 static VOID
LELongOutHook(struct BusContext
*context
, ULONG offset
,
1066 LELONGOUT(context
->io_base
+ offset
, value
);