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 __used
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 __used
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 __used
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 __used
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 __used
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 __used
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_PS_CMD_INHIBIT
|SDHCI_PS_DATA_INHIBIT
, 100000, 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 if (FNAME_SDCBUS(WaitCmd
)(SDHCI_PS_CMD_INHIBIT
|SDHCI_PS_DATA_INHIBIT
, 100000, unit
->sdcu_Bus
) == -1)
164 bug("[SDCard%02ld] %s: Failed to ACK termination of Read operation\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
167 *act
= sdcReadTags
[5].ti_Data
;
171 bug("[SDCard%02ld] %s: Transfer error\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
172 retVal
= IOERR_ABORTED
;
177 bug("[SDCard%02ld] %s: Error ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
178 retVal
= IOERR_ABORTED
;
181 DTRANS(bug("[SDCard%02ld] %s: %d bytes Read\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
, *act
));
186 BYTE
FNAME_SDCIO(ReadMultiple32
)(struct sdcard_Unit
*unit
, ULONG block
,
187 ULONG count
, APTR buffer
, ULONG
*act
)
190 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
195 BYTE
FNAME_SDCIO(ReadDMA32
)(struct sdcard_Unit
*unit
, ULONG block
,
196 ULONG count
, APTR buffer
, ULONG
*act
)
198 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
204 * 64bit Read operations
206 BYTE
FNAME_SDCIO(ReadSector64
)(struct sdcard_Unit
*unit
, UQUAD block
,
207 ULONG count
, APTR buffer
, ULONG
*act
)
209 struct TagItem sdcReadTags
[] =
211 {SDCARD_TAG_CMD
, MMC_CMD_READ_SINGLE_BLOCK
},
212 {SDCARD_TAG_ARG
, block
},
213 {SDCARD_TAG_RSPTYPE
, MMC_RSP_R1
},
215 {SDCARD_TAG_DATA
, (IPTR
)buffer
},
216 {SDCARD_TAG_DATALEN
, count
* (1 << unit
->sdcu_Bus
->sdcb_SectorShift
)},
217 {SDCARD_TAG_DATAFLAGS
, MMC_DATA_READ
},
222 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
227 sdcReadTags
[0].ti_Data
= MMC_CMD_READ_MULTIPLE_BLOCK
;
229 if (!(unit
->sdcu_Flags
& AF_Card_HighCapacity
))
232 D(bug("[SDCard%02ld] %s: Sending CMD %d, block %08x, len %d [buffer @ 0x%p]\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
,
233 sdcReadTags
[0].ti_Data
, sdcReadTags
[1].ti_Data
, sdcReadTags
[5].ti_Data
, sdcReadTags
[4].ti_Data
));
235 if (FNAME_SDCBUS(SendCmd
)(sdcReadTags
, unit
->sdcu_Bus
) != -1)
237 if (FNAME_SDCBUS(WaitCmd
)(SDHCI_PS_CMD_INHIBIT
|SDHCI_PS_DATA_INHIBIT
, 100000, unit
->sdcu_Bus
) != -1)
241 DTRANS(bug("[SDCard%02ld] %s: Finishing transaction ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
242 sdcReadTags
[0].ti_Data
= MMC_CMD_STOP_TRANSMISSION
;
243 sdcReadTags
[1].ti_Data
= 0;
244 sdcReadTags
[2].ti_Data
= MMC_RSP_R1b
;
245 sdcReadTags
[4].ti_Tag
= TAG_DONE
;
246 if (FNAME_SDCBUS(SendCmd
)(sdcReadTags
, unit
->sdcu_Bus
) == -1)
248 bug("[SDCard%02ld] %s: Failed to terminate Read operation\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
250 if (FNAME_SDCBUS(WaitCmd
)(SDHCI_PS_CMD_INHIBIT
|SDHCI_PS_DATA_INHIBIT
, 100000, unit
->sdcu_Bus
) == -1)
252 bug("[SDCard%02ld] %s: Failed to ACK termination of Read operation\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
255 *act
= sdcReadTags
[5].ti_Data
;
259 bug("[SDCard%02ld] %s: Transfer error\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
260 retVal
= IOERR_ABORTED
;
265 bug("[SDCard%02ld] %s: Error ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
266 retVal
= IOERR_ABORTED
;
269 DTRANS(bug("[SDCard%02ld] %s: %d bytes Read\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
, *act
));
274 BYTE
FNAME_SDCIO(ReadMultiple64
)(struct sdcard_Unit
*unit
, UQUAD block
,
275 ULONG count
, APTR buffer
, ULONG
*act
)
277 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
282 BYTE
FNAME_SDCIO(ReadDMA64
)(struct sdcard_Unit
*unit
, UQUAD block
,
283 ULONG count
, APTR buffer
, ULONG
*act
)
285 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
291 * 32bit Write operations
293 BYTE
FNAME_SDCIO(WriteSector32
)(struct sdcard_Unit
*unit
, ULONG block
,
294 ULONG count
, APTR buffer
, ULONG
*act
)
296 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
301 BYTE
FNAME_SDCIO(WriteMultiple32
)(struct sdcard_Unit
*unit
, ULONG block
,
302 ULONG count
, APTR buffer
, ULONG
*act
)
304 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
309 BYTE
FNAME_SDCIO(WriteDMA32
)(struct sdcard_Unit
*unit
, ULONG block
,
310 ULONG count
, APTR buffer
, ULONG
*act
)
312 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
318 * 64bit Write operations
320 BYTE
FNAME_SDCIO(WriteSector64
)(struct sdcard_Unit
*unit
, UQUAD block
,
321 ULONG count
, APTR buffer
, ULONG
*act
)
323 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
328 BYTE
FNAME_SDCIO(WriteMultiple64
)(struct sdcard_Unit
*unit
, UQUAD block
,
329 ULONG count
, APTR buffer
, ULONG
*act
)
331 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
336 BYTE
FNAME_SDCIO(WriteDMA64
)(struct sdcard_Unit
*unit
, UQUAD block
,
337 ULONG count
, APTR buffer
, ULONG
*act
)
339 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
347 BYTE
FNAME_SDCIO(Eject
)(struct sdcard_Unit
*unit
)
349 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));