1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15:
16: 17: 18: 19: 20:
21: #include <tk/tkernel.h>
22: #include <libstr.h>
23: #include <tk/util.h>
24: #include <device/sdrvif.h>
25: #include <device/gdrvif.h>
26: #include <device/disk.h>
27: #include <device/pcmcia.h>
28: #include <sys/util.h>
29:
30:
31: #define DP(arg)
32:
33:
34: #if BIGENDIAN
35: #define CH4toW(c1, c2, c3, c4) ( ((c1)<<24)|((c2)<<16)|((c3)<<8)|(c4) )
36: #else
37: #define CH4toW(c1, c2, c3, c4) ( ((c4)<<24)|((c3)<<16)|((c2)<<8)|(c1) )
38: #endif
39:
40: 41: 42:
43: typedef struct {
44: UW accif:8;
45: UW blocksz:2;
46: UW rsv1:6;
47: BOOL pccard:1;
48: BOOL rsv2:1;
49: BOOL readonly:1;
50: BOOL subtsk:1;
51: BOOL eject:1;
52: BOOL lockreq:1;
53: BOOL wlock:1;
54: BOOL susres:1;
55: BOOL nowprotect:1;
56: UW rsv3:7;
57: } SDSpec;
58:
59: #define MEM_ACCIF 1
60: #define ATA_ACCIF 2
61: #define MMC_ACCIF 3
62:
63:
64: #define ROM_DISK {MEM_ACCIF, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0}
65: #define RAM_DISK {MEM_ACCIF, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
66: #define ATA_CARD {ATA_ACCIF, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0}
67: #define MMC_CARD {MMC_ACCIF, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0}
68:
69: #define MEM_DRV 0x00
70: #define PC_DRV 0x0C
71:
72: typedef struct {
73: W num;
74: SDSpec spec;
75: UB devnm[L_DEVNM];
76: UW name4;
77: UW drvno;
78: UW iob;
79: UW misc;
80: } SDInfo;
81:
82: 83: 84: 85:
86:
87: 88: 89:
90: #define MAX_PART 4
91: #define MAX_SUNIT (MAX_PART + 1)
92: #define IDENT_DTSZ 96
93: #define IDENT2_DTSZ 16
94:
95: typedef struct {
96: DiskSystemId SystemId;
97: UW StartBlock;
98: UW EndBlock;
99: W OpenCnt;
100: } SUNIT;
101:
102: typedef struct _drvtab {
103:
104: SDSpec Spec;
105: UB DevName[L_DEVNM];
106: UW DrvNo;
107: UW DrvBit;
108: UW IOB;
109: UW MiscPar;
110:
111: struct _drvtab *Next;
112: struct _drvtab *Top;
113:
114:
115: ID MbfId;
116: GDI Gdi;
117: ID DevId;
118: ID WaitTskId;
119:
120:
121: void (*Abort)(struct _drvtab *drv);
122: INT (*ReadWrite)(struct _drvtab *drv, W blk, W cnt, void *mptr,
123: BOOL write);
124: ER (*Format)(struct _drvtab *drv, DiskFormat *fmt, W dcnt);
125: ER (*Identify)(struct _drvtab *drv, BOOL check);
126: ER (*DiskInit)(struct _drvtab *drv);
127: ER (*Misc)(struct _drvtab *drv, W cmd);
128:
129: void (*IntHdr)(struct _drvtab *drv);
130:
131:
132: BOOL DriveOK:1;
133: BOOL MediaOK:1;
134: BOOL Wprotect:1;
135: BOOL EnbInt:1;
136: BOOL Suspended:1;
137: BOOL Aborted:1;
138: BOOL Reset:1;
139: BOOL SuptMBR:1;
140:
141:
142: BOOL UseLBA:1;
143: BOOL SetXCHS:1;
144:
145: BOOL ResvFlags:22;
146:
147:
148: FastLock ProcLock;
149: T_DEVREQ *CurReq;
150: TMO ReqTmout;
151:
152:
153: union {
154: struct {
155: ID CardId;
156: ID MapId;
157: ID MapId2;
158: W CardInf;
159: UH IOConf[2];
160: } pcc;
161: union {
162: UW uw[5];
163: UH uh[10];
164: UB ub[20];
165: } g;
166: } d;
167:
168:
169: DiskFormat DiskFmt;
170: W SecSize;
171: W SecBias;
172:
173: W CurSUnit;
174: W OpenCnt;
175:
176: UH MultiCnt;
177: UH nSUnit;
178:
179: UH nCyl;
180: UH nHead;
181: UH nSec;
182: UH nXCyl;
183: UH nXHead;
184: UH nXSec;
185:
186: union {
187: SUNIT SUnit[MAX_SUNIT];
188: } s;
189: UB Ident[IDENT_DTSZ];
190: UB Ident2[IDENT2_DTSZ];
191:
192: union {
193: UW uw[1];
194: UH uh[2];
195: UB ub[4];
196: } wrk;
197: } DrvTab;
198:
199: #define ENB_INT(drv) ((drv)->EnbInt = TRUE)
200: #define DIS_INT(drv) ((drv)->EnbInt = FALSE)
201:
202:
203: #define DC_OPEN (1)
204: #define DC_CLOSE (2)
205: #define DC_CLOSEALL (3)
206: #define DC_SUSPEND (4)
207: #define DC_RESUME (5)
208: #define DC_TSKINIT (-1)
209: #define DC_TSKTMO (-2)
210: #define DC_DRVCHG (-3)
211:
212: 213: 214:
215: #define TMO_EVENT (2 * 1000)
216:
217: 218: 219:
220: #define CP_POFF CP_POFFREQ(5)
221:
222: 223: 224:
225: #define ERR_ABORT (E_IO | 0)
226: #define ERR_TMOUT (E_IO | 1)
227: #define ERR_MEDIA (E_IO | 2)
228: #define ERR_HARD (E_IO | 3)
229: #define ERR_DMA (E_IO | 4)
230: #define ERR_BLKSZ (E_NOSPT)
231:
232: #define ERR_CMDBUSY (E_IO | 0x10)
233: #define ERR_DATABUSY (E_IO | 0x11)
234: #define ERR_NOTRDY (E_IO | 0x12)
235:
236: #define ERR_ATA(c) (E_IO | 0x8000 | (c))
237: #define ERR_ATAMSK(er, m) ((er) & (0xffff8000 | (m)))
238:
239: #define ERR_ATASENSE ERR_ATA(0xF0)
240:
241: #define ERR_NOBLK (E_OBJ | 0)
242: #define ERR_NOPORT (E_OBJ | 1)
243: #define ERR_NOMEDIA E_NOMDA
244: #define ERR_RONLY E_RONLY
245: #define ERR_NOTSAME E_PAR
246:
247: 248: 249:
250: #define DISWAI 16
251: #define DISWAI_REQ DEVREQ_ACPPTN(DISWAI)
252: #define NORMAL_REQPTN (DRP_NORMREQ | DISWAI_REQ)
253:
254: 255: 256:
257: #define INVALID_DEVID (-1)
258:
259: 260: 261:
262: #ifdef DEBUG
263: #define PRINTF_STKSZ (1536)
264: #else
265: #define PRINTF_STKSZ (0)
266: #endif
267: #define DRVTSK_STKSZ (3072 + PRINTF_STKSZ)
268:
269: 270: 271:
272:
273: IMPORT ER SysDiskDrv(INT ac, UB *av[]);
274:
275:
276: IMPORT INT CheckSize(W datacnt, W size);
277: IMPORT ER sdAbortFn(T_DEVREQ *req, GDI gdi);
278: IMPORT INT sdEventFn(INT evttyp, void *evtinf, GDI gdi);
279: IMPORT ER sdOpenFn(ID devid, UINT omode, GDI gdi);
280: IMPORT ER sdCloseFn(ID devid, UINT option, GDI gdi);
281: IMPORT void sdAcceptRequest(DrvTab *drv);
282:
283:
284: IMPORT void sdSendEvt(DrvTab *drv, W kind);
285: IMPORT void sdSetUpAccFn(DrvTab *drv);
286: IMPORT void sdMakeDiskInfo(DrvTab *drv, DiskInfo *inf);
287: IMPORT ER sdRegistDevice(DrvTab *drv);
288: IMPORT UW sdGetMsec(void);
289: IMPORT BOOL sdChkTmout(UW *tm, UW tmo, W delay);
290:
291:
292: IMPORT ER sdDefIntHdr(DrvTab *drv, BOOL regist);
293: IMPORT void sdSetIntWait(DrvTab *drv);
294:
295:
296: IMPORT ER sdPowerOn(DrvTab *drv, UW req);
297: IMPORT void sdPowerOff(DrvTab *drv, UW req);
298: IMPORT INT sdCardEvent(INT evttyp, CardReq *req, GDI gdi);
299: IMPORT ER sdInitCard(DrvTab *drv);
300:
301:
302: IMPORT INT ataReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write);
303: IMPORT ER ataFormat(DrvTab *drv, DiskFormat *fmt, W dcnt);
304: IMPORT ER ataIdentify(DrvTab *drv, BOOL check);
305: IMPORT ER ataDiskInit(DrvTab *drv);
306: IMPORT ER ataMisc(DrvTab *drv, W cmd);
307: IMPORT W ataSetXCHS(DrvTab *drv);
308: IMPORT ER ataSetupPart(DrvTab *drv);
309:
310:
311: IMPORT void ataSelDrv(DrvTab *drv);
312: IMPORT void ataIntHdr(DrvTab *drv);
313: IMPORT void ataAbort(DrvTab *drv);
314: IMPORT ER ataCmd(DrvTab *drv, W cmd, UW lba, W len, void *buf);
315: IMPORT INT atapiCmd(DrvTab *drv, W cmd, UW lba, W len, void *buf);
316:
317:
318: IMPORT void memAbort(DrvTab *drv);
319: IMPORT INT memReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write);
320: IMPORT ER memFormat(DrvTab *drv, DiskFormat *fmt, W dcnt);
321: IMPORT ER memIdentify(DrvTab *drv, BOOL check);
322: IMPORT ER memDiskInit(DrvTab *drv);
323: IMPORT ER memMisc(DrvTab *drv, W cmd);
324:
325:
326: IMPORT void mmcAbort(DrvTab *drv);
327: IMPORT INT mmcReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write);
328: IMPORT ER mmcFormat(DrvTab *drv, DiskFormat *fmt, W dcnt);
329: IMPORT ER mmcIdentify(DrvTab *drv, BOOL check);
330: IMPORT ER mmcDiskInit(DrvTab *drv);
331: IMPORT ER mmcMisc(DrvTab *drv, W cmd);
332:
333:
334: IMPORT W ataStatusIn(DrvTab *drv);
335: IMPORT W ataErrorIn(DrvTab *drv);
336: IMPORT W ataCylIn(DrvTab *drv);
337: IMPORT ER ataSetDrive(DrvTab *drv, W drvno);
338: IMPORT void ataEnbInt(DrvTab *drv);
339: IMPORT ER ataWaitData(DrvTab *drv);
340: IMPORT ER ataWaitReady(DrvTab *drv);
341: IMPORT void ataReset(DrvTab *drv);
342: IMPORT void atapiReset(DrvTab *drv);
343: IMPORT BOOL atapiTest(DrvTab *drv, W init);
344: IMPORT ER ataCommandOut(DrvTab *drv, W cmd, W cnt, W sec, W cyl, W head);
345: IMPORT ER atapiCommandOut(DrvTab *drv, UH *cmd, W cnt, W dma);
346: IMPORT void ataDataIn(DrvTab *drv, UH *buf, W cnt);
347: IMPORT void ataDataSkip(DrvTab *drv, W cnt);
348: IMPORT void ataDataOut(DrvTab *drv, UH *buf, W cnt);