revert between 56095 -> 55830 in arch
[AROS.git] / rom / devs / sdcard / sdcard_ioops.c
blobc85939e37c22c07f9d8401c1aaccbc598a85a77e
1 /*
2 Copyright � 2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
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>
13 #include <oop/oop.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;
33 while (size--)
34 str2[size ^ 1] = str1[size];
36 while (i > 0 && str2[--i] <= ' ')
37 str2[i] = '\0';
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));
46 return CDERR_NOCMD;
49 static BYTE __used sdcard_STUB_IO32(struct sdcard_Unit *unit, ULONG blk, ULONG len,
50 APTR buf, ULONG* act)
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));
54 return CDERR_NOCMD;
57 static BYTE __used sdcard_STUB_IO64(struct sdcard_Unit *unit, UQUAD blk, ULONG len,
58 APTR buf, ULONG* act)
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));
62 return CDERR_NOCMD;
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));
68 return CDERR_NOCMD;
71 static inline BOOL sdcard_SelectUnit(struct sdcard_Unit* unit)
73 return TRUE;
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)
111 BYTE err = 0;
112 return err;
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},
126 {SDCARD_TAG_RSP, 0},
127 {SDCARD_TAG_DATA, (IPTR)buffer},
128 {SDCARD_TAG_DATALEN, count * (1 << unit->sdcu_Bus->sdcb_SectorShift)},
129 {SDCARD_TAG_DATAFLAGS, MMC_DATA_READ},
130 {TAG_DONE, 0}
132 BYTE retVal = 0;
134 D(bug("[SDCard%02ld] %s()\n", unit->sdcu_UnitNum, __PRETTY_FUNCTION__));
136 *act = 0;
138 if (count > 1)
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)
151 if (count > 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;
169 else
171 bug("[SDCard%02ld] %s: Transfer error\n", unit->sdcu_UnitNum, __PRETTY_FUNCTION__);
172 retVal = IOERR_ABORTED;
175 else
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));
183 return retVal;
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__));
192 return IOERR_NOCMD;
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__));
200 return IOERR_NOCMD;
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},
214 {SDCARD_TAG_RSP, 0},
215 {SDCARD_TAG_DATA, (IPTR)buffer},
216 {SDCARD_TAG_DATALEN, count * (1 << unit->sdcu_Bus->sdcb_SectorShift)},
217 {SDCARD_TAG_DATAFLAGS, MMC_DATA_READ},
218 {TAG_DONE, 0}
220 BYTE retVal = 0;
222 D(bug("[SDCard%02ld] %s()\n", unit->sdcu_UnitNum, __PRETTY_FUNCTION__));
224 *act = 0;
226 if (count > 1)
227 sdcReadTags[0].ti_Data = MMC_CMD_READ_MULTIPLE_BLOCK;
229 if (!(unit->sdcu_Flags & AF_Card_HighCapacity))
230 return IOERR_NOCMD;
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)
239 if (count > 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;
257 else
259 bug("[SDCard%02ld] %s: Transfer error\n", unit->sdcu_UnitNum, __PRETTY_FUNCTION__);
260 retVal = IOERR_ABORTED;
263 else
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));
271 return retVal;
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__));
279 return IOERR_NOCMD;
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__));
287 return IOERR_NOCMD;
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__));
298 return IOERR_NOCMD;
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__));
306 return IOERR_NOCMD;
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__));
314 return IOERR_NOCMD;
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__));
325 return IOERR_NOCMD;
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__));
333 return IOERR_NOCMD;
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__));
341 return IOERR_NOCMD;
345 * miscellaneous
347 BYTE FNAME_SDCIO(Eject)(struct sdcard_Unit *unit)
349 D(bug("[SDCard%02ld] %s()\n", unit->sdcu_UnitNum, __PRETTY_FUNCTION__));
351 return IOERR_NOCMD;