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_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__
);
163 *act
= sdcReadTags
[5].ti_Data
;
167 bug("[SDCard%02ld] %s: Transfer error\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
168 retVal
= IOERR_ABORTED
;
173 bug("[SDCard%02ld] %s: Error ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
174 retVal
= IOERR_ABORTED
;
177 DTRANS(bug("[SDCard%02ld] %s: %d bytes Read\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
, *act
));
182 BYTE
FNAME_SDCIO(ReadMultiple32
)(struct sdcard_Unit
*unit
, ULONG block
,
183 ULONG count
, APTR buffer
, ULONG
*act
)
186 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
191 BYTE
FNAME_SDCIO(ReadDMA32
)(struct sdcard_Unit
*unit
, ULONG block
,
192 ULONG count
, APTR buffer
, ULONG
*act
)
194 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
200 * 64bit Read operations
202 BYTE
FNAME_SDCIO(ReadSector64
)(struct sdcard_Unit
*unit
, UQUAD block
,
203 ULONG count
, APTR buffer
, ULONG
*act
)
205 struct TagItem sdcReadTags
[] =
207 {SDCARD_TAG_CMD
, MMC_CMD_READ_SINGLE_BLOCK
},
208 {SDCARD_TAG_ARG
, block
},
209 {SDCARD_TAG_RSPTYPE
, MMC_RSP_R1
},
211 {SDCARD_TAG_DATA
, (IPTR
)buffer
},
212 {SDCARD_TAG_DATALEN
, count
* (1 << unit
->sdcu_Bus
->sdcb_SectorShift
)},
213 {SDCARD_TAG_DATAFLAGS
, MMC_DATA_READ
},
218 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
223 sdcReadTags
[0].ti_Data
= MMC_CMD_READ_MULTIPLE_BLOCK
;
225 if (!(unit
->sdcu_Flags
& AF_Card_HighCapacity
))
228 D(bug("[SDCard%02ld] %s: Sending CMD %d, block %08x, len %d [buffer @ 0x%p]\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
,
229 sdcReadTags
[0].ti_Data
, sdcReadTags
[1].ti_Data
, sdcReadTags
[5].ti_Data
, sdcReadTags
[4].ti_Data
));
231 if (FNAME_SDCBUS(SendCmd
)(sdcReadTags
, unit
->sdcu_Bus
) != -1)
233 if (FNAME_SDCBUS(WaitCmd
)(SDHCI_PS_CMD_INHIBIT
|SDHCI_PS_DATA_INHIBIT
, 100000, unit
->sdcu_Bus
) != -1)
237 DTRANS(bug("[SDCard%02ld] %s: Finishing transaction ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
238 sdcReadTags
[0].ti_Data
= MMC_CMD_STOP_TRANSMISSION
;
239 sdcReadTags
[1].ti_Data
= 0;
240 sdcReadTags
[2].ti_Data
= MMC_RSP_R1b
;
241 sdcReadTags
[4].ti_Tag
= TAG_DONE
;
242 if (FNAME_SDCBUS(SendCmd
)(sdcReadTags
, unit
->sdcu_Bus
) == -1)
244 bug("[SDCard%02ld] %s: Failed to terminate Read operation\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
247 *act
= sdcReadTags
[5].ti_Data
;
251 bug("[SDCard%02ld] %s: Transfer error\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
252 retVal
= IOERR_ABORTED
;
257 bug("[SDCard%02ld] %s: Error ..\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
);
258 retVal
= IOERR_ABORTED
;
261 DTRANS(bug("[SDCard%02ld] %s: %d bytes Read\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
, *act
));
266 BYTE
FNAME_SDCIO(ReadMultiple64
)(struct sdcard_Unit
*unit
, UQUAD block
,
267 ULONG count
, APTR buffer
, ULONG
*act
)
269 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
274 BYTE
FNAME_SDCIO(ReadDMA64
)(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__
));
283 * 32bit Write operations
285 BYTE
FNAME_SDCIO(WriteSector32
)(struct sdcard_Unit
*unit
, ULONG block
,
286 ULONG count
, APTR buffer
, ULONG
*act
)
288 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
293 BYTE
FNAME_SDCIO(WriteMultiple32
)(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(WriteDMA32
)(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__
));
310 * 64bit Write operations
312 BYTE
FNAME_SDCIO(WriteSector64
)(struct sdcard_Unit
*unit
, UQUAD block
,
313 ULONG count
, APTR buffer
, ULONG
*act
)
315 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));
320 BYTE
FNAME_SDCIO(WriteMultiple64
)(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(WriteDMA64
)(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__
));
339 BYTE
FNAME_SDCIO(Eject
)(struct sdcard_Unit
*unit
)
341 D(bug("[SDCard%02ld] %s()\n", unit
->sdcu_UnitNum
, __PRETTY_FUNCTION__
));