2 Copyright © 2013, The AROS Development Team. All rights reserved.
7 #include <aros/debug.h>
9 #include <exec/types.h>
10 #include <exec/exec.h>
11 #include <exec/resident.h>
12 #include <utility/utility.h>
15 #include <proto/exec.h>
16 #include <devices/cd.h>
17 #include <devices/timer.h>
19 #include <hardware/mmc.h>
20 #include <hardware/sdhc.h>
22 #include "sdcard_base.h"
23 #include "sdcard_unit.h"
25 static BOOL
sdcard_WaitBusyTO(struct sdcard_Unit
*unit
, UWORD tout
, BOOL irq
, UBYTE
*stout
);
27 #define DEVHEAD_VAL 0x40
29 static void sdcard_strcpy(const UBYTE
*str1
, UBYTE
*str2
, ULONG size
)
31 register int i
= size
;
34 str2
[size
^ 1] = str1
[size
];
36 while (i
> 0 && str2
[--i
] <= ' ')
41 * a STUB function for commands not supported by this particular device
43 static BYTE
sdcard_STUB(struct sdcard_Unit
*unit
)
45 DERROR(bug("[SDCard%02ld] CALLED STUB FUNCTION (GENERIC). OPERATION IS NOT SUPPORTED BY DEVICE\n", unit
->sdcu_UnitNum
));
49 static BYTE
sdcard_STUB_IO32(struct sdcard_Unit
*unit
, ULONG blk
, ULONG len
,
52 DERROR(bug("[SDCard%02ld] CALLED STUB FUNCTION (IO32). OPERATION IS NOT SUPPORTED BY DEVICE\n", unit
->sdcu_UnitNum
));
53 DERROR(bug("[SDCard%02ld] -- IO ACCESS TO BLOCK %08lx, LENGTH %08lx\n", unit
->sdcu_UnitNum
, blk
, len
));
57 static BYTE
sdcard_STUB_IO64(struct sdcard_Unit
*unit
, UQUAD blk
, ULONG len
,
60 DERROR(bug("[SDCard%02ld] CALLED STUB FUNCTION (IO64). OPERATION IS NOT SUPPORTED BY DEVICE\n", unit
->sdcu_UnitNum
));
61 DERROR(bug("[SDCard%02ld] -- IO ACCESS TO BLOCK %08lx:%08lx, LENGTH %08lx\n", unit
->sdcu_UnitNum
, (blk
>> 32), (blk
& 0xffffffff), len
));
65 static BYTE
sdcard_STUB_SCSI(struct sdcard_Unit
*unit
, struct SCSICmd
* cmd
)
67 DERROR(bug("[SDCard%02ld] CALLED STUB FUNCTION (IOSCSI). OPERATION IS NOT SUPPORTED BY DEVICE\n", unit
->sdcu_UnitNum
));
71 static inline BOOL
sdcard_SelectUnit(struct sdcard_Unit
* unit
)
76 void sdcard_IRQSetHandler(struct sdcard_Unit
*unit
, void (*handler
)(struct sdcard_Unit
*, UBYTE
), APTR piomem
, ULONG blklen
, ULONG piolen
)
81 void sdcard_IRQNoData(struct sdcard_Unit
*unit
, UBYTE status
)
86 void sdcard_IRQPIORead(struct sdcard_Unit
*unit
, UBYTE status
)
91 void sdcard_PIOWriteBlk(struct sdcard_Unit
*unit
)
96 void sdcard_IRQPIOWrite(struct sdcard_Unit
*unit
, UBYTE status
)
101 void sdcard_IRQDMAReadWrite(struct sdcard_Unit
*unit
, UBYTE status
)
107 * wait for timeout or drive ready
109 BOOL
sdcard_WaitBusyTO(struct sdcard_Unit
*unit
, UWORD tout
, BOOL irq
, UBYTE
*stout
)
116 * 32bit Read operations
118 BYTE
FNAME_SDCIO(ReadSector32
)(struct sdcard_Unit
*unit
, ULONG block
,
119 ULONG count
, APTR buffer
, ULONG
*act
)
121 struct TagItem sdcReadTags
[] =
123 {SDCARD_TAG_CMD
, MMC_CMD_READ_SINGLE_BLOCK
},
124 {SDCARD_TAG_ARG
, block
},
125 {SDCARD_TAG_RSPTYPE
, MMC_RSP_R1
},
127 {SDCARD_TAG_DATA
, (IPTR
)buffer
},
128 {SDCARD_TAG_DATALEN
, count
* (1 << unit
->sdcu_Bus
->sdcb_SectorShift
)},
129 {SDCARD_TAG_DATAFLAGS
, MMC_DATA_READ
},
134 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
139 sdcReadTags
[0].ti_Data
= MMC_CMD_READ_MULTIPLE_BLOCK
;
141 if (!(unit
->sdcu_Flags
& AF_Card_HighCapacity
))
142 sdcReadTags
[1].ti_Data
<<= unit
->sdcu_Bus
->sdcb_SectorShift
;
144 D(bug("[SDCard%02ld] %s: Sending CMD %d, block %08x, len %d [buffer @ 0x%p]\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
,
145 sdcReadTags
[0].ti_Data
, sdcReadTags
[1].ti_Data
, sdcReadTags
[5].ti_Data
, sdcReadTags
[4].ti_Data
));
147 if (FNAME_SDCBUS(SendCmd
)(sdcReadTags
, unit
->sdcu_Bus
) != -1)
149 if (FNAME_SDCBUS(WaitCmd
)(SDHCI_INT_DATA_AVAIL
|SDHCI_INT_DATA_END
, 1000, unit
->sdcu_Bus
) != -1)
153 DTRANS(bug("[SDCard%02ld] %s: Finishing transaction ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
154 sdcReadTags
[0].ti_Data
= MMC_CMD_STOP_TRANSMISSION
;
155 sdcReadTags
[1].ti_Data
= 0;
156 sdcReadTags
[2].ti_Data
= MMC_RSP_R1b
;
157 sdcReadTags
[4].ti_Tag
= TAG_DONE
;
158 if (FNAME_SDCBUS(SendCmd
)(sdcReadTags
, unit
->sdcu_Bus
) == -1)
160 bug("[SDCard%02ld] %s: Failed to terminate Read operation\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
162 retVal
= IOERR_ABORTED
;
168 D(bug("[SDCard%02ld] %s: Transfer error\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
170 retVal
= IOERR_ABORTED
;
175 bug("[SDCard%02ld] %s: Error ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
177 retVal
= IOERR_ABORTED
;
180 *act
= count
<< unit
->sdcu_Bus
->sdcb_SectorShift
;
182 DTRANS(bug("[SDCard%02ld] %s: %d bytes Read\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
, *act
));
187 BYTE
FNAME_SDCIO(ReadMultiple32
)(struct sdcard_Unit
*unit
, ULONG block
,
188 ULONG count
, APTR buffer
, ULONG
*act
)
191 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
196 BYTE
FNAME_SDCIO(ReadDMA32
)(struct sdcard_Unit
*unit
, ULONG block
,
197 ULONG count
, APTR buffer
, ULONG
*act
)
199 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
205 * 64bit Read operations
207 BYTE
FNAME_SDCIO(ReadSector64
)(struct sdcard_Unit
*unit
, UQUAD block
,
208 ULONG count
, APTR buffer
, ULONG
*act
)
210 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
215 BYTE
FNAME_SDCIO(ReadMultiple64
)(struct sdcard_Unit
*unit
, UQUAD block
,
216 ULONG count
, APTR buffer
, ULONG
*act
)
218 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
223 BYTE
FNAME_SDCIO(ReadDMA64
)(struct sdcard_Unit
*unit
, UQUAD block
,
224 ULONG count
, APTR buffer
, ULONG
*act
)
226 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
232 * 32bit Write operations
234 BYTE
FNAME_SDCIO(WriteSector32
)(struct sdcard_Unit
*unit
, ULONG block
,
235 ULONG count
, APTR buffer
, ULONG
*act
)
237 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
242 BYTE
FNAME_SDCIO(WriteMultiple32
)(struct sdcard_Unit
*unit
, ULONG block
,
243 ULONG count
, APTR buffer
, ULONG
*act
)
245 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
250 BYTE
FNAME_SDCIO(WriteDMA32
)(struct sdcard_Unit
*unit
, ULONG block
,
251 ULONG count
, APTR buffer
, ULONG
*act
)
253 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
259 * 64bit Write operations
261 BYTE
FNAME_SDCIO(WriteSector64
)(struct sdcard_Unit
*unit
, UQUAD block
,
262 ULONG count
, APTR buffer
, ULONG
*act
)
264 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
269 BYTE
FNAME_SDCIO(WriteMultiple64
)(struct sdcard_Unit
*unit
, UQUAD block
,
270 ULONG count
, APTR buffer
, ULONG
*act
)
272 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
277 BYTE
FNAME_SDCIO(WriteDMA64
)(struct sdcard_Unit
*unit
, UQUAD block
,
278 ULONG count
, APTR buffer
, ULONG
*act
)
280 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
288 BYTE
FNAME_SDCIO(Eject
)(struct sdcard_Unit
*unit
)
290 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));