summaryrefslogtreecommitdiff
path: root/Kernel/BlkDev/DskFDC.HC
blob: 4012627d2155adf36cb26a5c225dade43d6210a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
    Copyright (C) 2025-2026 Harley Travis <yoshi128k@gmail.com>.
    This software (including source code) is licensed under the BSD Zero Clause
    License. See the Copying.TXT file for details.
*/

// TO INSTALL: #include this file after DskATA in MakeBlkDev.HC.

interrupt U0 FDCInt()
{
  // On IRQ6, set a semaphore for anything waiting for an FDC int
  fdc_int_semaphore = TRUE;
  OutU8(0x20,0x20); // Send EOI to PIC
}

U0 FDCDMAInit(U16 len)
{
  U64 buf_lo, buf_hi, page, cnt_lo, cnt_hi;

  buf_lo = &FDC_DMA & 0xFF;
  buf_hi = &FDC_DMA >> 8;
  page = &FDC_DMA >> 16;
  cnt_lo = (len - 1) & 0xFF;
  cnt_hi = (len - 1) >> 8;

  OutU8(0x0A, 6); // mask ch 0 and 2
  Sleep(1);
  OutU8(0x0C, -1); // reset flip flop
  Sleep(1);
  OutU8(0x04, buf_lo); // buf low byte
  Sleep(1);
  OutU8(0x04, buf_hi); // buf high byte
  Sleep(1);
  OutU8(0x0C, -1); // reset flip flop again
  Sleep(1);
  OutU8(0x05, cnt_lo); // cntr low byte
  Sleep(1);
  OutU8(0x05, cnt_hi); // cntr high byte
  Sleep(1);
  OutU8(0x81, 0); // page
  Sleep(1);
  OutU8(0x0A, 2); // unmask ch 0 and 2
  Sleep(1);
}

U0 FDCDMAPrepWrite()
{
  OutU8(0x0A, 6); // mask ch 0 and 2
  Sleep(1);
  OutU8(0x0B, 0x5A); // single xfer, addr inc, auto init, write, ch 2
  Sleep(1);
  OutU8(0x0A, 2); // unmask ch 0 and 2
  Sleep(1);
}

U0 FDCDMAPrepRead()
{
  OutU8(0x0A, 6); // mask ch 0 and 2
  Sleep(1);
  OutU8(0x0B, 0x56); // single xfer, addr inc, auto init, read, ch 2
  Sleep(1);
  OutU8(0x0A, 2); // unmask ch 0 and 2
  Sleep(1);
}

U8 FDCReadByte(CBlkDev *bd)
{
  // Read byte from FDC.

  U8 byte; // The byte read
  F64 timeout; // Timeout variable
  timeout = tS()+3.0; // 3 second timeout

  while (TRUE) {
    if (InU8(bd->base0+FDC_MSR_DSR)>>6==3) {
      byte=InU8(bd->base0+FDC_DATA);
      return byte;
    }
    if (tS()>timeout) {
      throw('BlkDev');
    }
  }
}

U0 FDCSendByte(CBlkDev *bd,U8 byte)
{
  // Send byte to FDC.

  F64 timeout; // Timeout variable
  timeout=tS()+3.0; // 3 second timeout

  while (TRUE) {
    if (InU8(bd->base0+FDC_MSR_DSR)>>6==2) {
      OutU8(bd->base0+FDC_DATA,byte);
      return;
    }
    if (tS()>timeout) {
      throw('BlkDev');
    }
  }
}

U0 FDCReset(CBlkDev *bd)
{
  // Reset the FDC

  fdc_int_semaphore=FALSE;

  // Twiddle the reset bit in the DSR
  OutU8(bd->base0+FDC_MSR_DSR,0x80|bd->bps);

  // Wait for the int
  while (!fdc_int_semaphore)
    Yield;

  // Send 4 SISes
  for (i=0,i<4,i++) {
    FDCSendByte(bd,FDC_SENSE_INTR)
    FDCReadByte(bd);
    FDCReadByte(bd);
  }

  // Send "CONFIGURE" to fix FDC cfg
  FDCSendByte(bd,FDC_CONFIGURE);
  FDCSendByte(bd,0); // Null byte
  FDCSendByte(bd,0b01010111); // Implied Seek, FIFO, No Polling, Threshold 8
  FDCSendByte(bd,0); // No Write Precomp

  // Select our drive
  FDCSelDrv(bd);
}

U0 FDCMotorTask(CBlkDev *bd)
{
  // Motor timeout task
  F64 timeout = tS()+3.0; // 3 sec timeout
  U8 dor;
  while(tS() != timeout) {
    if (bd->mtr==FDC_MOTOR_ON)
      return;
    Yield;
  }
  dor=InU8(bd->base0+FDC_DOR);
  OutU8(bd->base0+FDC_DOR,dor^(1<<4+bd->unit));
}

U0 FDCMotor(CBlkDev *bd, Bool onoff)
{
  // Motor control
  F64 timeout;
  U8 dor=InU8(bd->base0+FDC_DOR);
  if (onoff) {
    OutU8(bd->base0+FDC_DOR,dor^(1<<4+bd->unit)|(1<<4+bd->unit));
    timeout=tS()+0.5;
    while (tS()<timeout) Yield;
    bd->mtr=FDC_MOTOR_ON;
  } else {
    bd->mtr == FDC_MOTOR_WAIT;
    Spawn(&FDCMotorTask,bd,"FDC Motor");
  }
}

U0 FDCSelDrv(CBlkDev *bd)
{
  // Select the drive assigned to this blkdev

  // Set the CCR appropriately
  OutU8(bd->base0+FDC_CCR_DSR,bd->bps);

  // Send a "SPECIFY" command
  FDCSendByte(bd,FDC_SPECIFY);
  FDCSendByte(bd,bd->srt<<4|bd->hut); // Step Rate Time, Head Unload Time
  FDCSendByte(bd,bd->hlt<<1|!bd->dma); // Head Load Time, DMA

  // Set drv sel in DOR (and enable DMA/IRQs)
  OutU8(bd->base0+FDC_DOR,8|bd->unit);
}

Bool FDCInit(CBlkDev *bd)
{
  // Recalibrate the drive and (on the first drive) initialize the FDC

  U8 ver,st0,pcn;
  Bool unlock=BlkDevLock(bd),okay=FALSE;

  // If this drv is unit 0, do initialization
  if (!bd->unit) {
    // Check if the controller is 82077AA-compatible
    FDCSendByte(bd,FDC_VERSION);
    ver=FDCReadByte(bd);

    if (ver==0x90)
      okay=TRUE;
  
    FDCReset(bd);
  }

  recalibrate:
  // Recalibrate this drive
  fdc_int_semaphore=FALSE;
  FDCSendByte(bd,FDC_RECALIBRATE);
  FDCSendByte(bd,bd->unit); // Drive number

  // Wait for an IRQ
  while (!fdc_int_semaphore) Yield;

  // Get the result of the recalibration
  FDCSendByte(bd,FDC_SENSE_INTR);
  st0=FDCReadByte(bd);
  pcn=FDCReadByte(bd);

  if (st0>>6) okay=FALSE;

  // This should only be needed for disks with more than 80 tracks,
  // in which case we will do another recalibration
  if (!(st0&0x20)) goto recalibrate;

  bd->max_blk=(bd->cyls*bd->heads*bd->spt)-1;

  if (unlock) BlkDevUnlock(bd);
  return okay;
}

U8 FDCWriteData(CBlkDev *bd,U8 cyl,Bool head,U8 start,U8 end)
{
  U16 length=(end-start+1)*bd->blk_size;
  U8 st0,st1,st2,pcn,hd,sect,size

  fdc_int_semaphore=FALSE;

  FDCDMAInit(length);
  FDCDMAPrepWrite();

  FDCSendByte(bd,FDC_WRITE_DATA|(bd->heads>1<<7)|mfm<<6|);
  FDCSendByte(bd,cyl);
  FDCSendByte(bd,head);
  FDCSendByte(bd,start);
  FDCSendByte(bd,Bsf(bd->blk_size>>7));
  FDCSendByte(bd,end);
  FDCSendByte(bd,bd->gpl1);
  FDCSendByte(bd,255);

  while(!fdc_int_semaphore) Yield;

  st0=FDCReadByte(bd);
  st1=FDCReadByte(bd);
  st2=FDCReadByte(bd);
  pcn=FDCReadByte(bd);
  hd=FDCReadByte(bd);
  sect=FDCReadByte(bd);
  size=FDCReadByte(bd);

  return st0;
}

U8 FDCReadData(CBlkDev *bd,U8 cyl,Bool head,U8 start,U8 end)
{
  U16 length=(end-start+1)*bd->blk_size;
  U8 st0,st1,st2,pcn,hd,sect,size

  fdc_int_semaphore=FALSE;

  FDCDMAInit(length);
  FDCDMAPrepRead();

  FDCSendByte(bd,FDC_READ_DATA|mfm<<6|);
  FDCSendByte(bd,cyl);
  FDCSendByte(bd,head);
  FDCSendByte(bd,start); // sector is ignored
  FDCSendByte(bd,Bsf(bd->blk_size>>7));
  FDCSendByte(bd,end);
  FDCSendByte(bd,bd->gpl1);
  FDCSendByte(bd,255);

  while(!fdc_int_semaphore) Yield;

  st0=FDCReadByte(bd);
  st1=FDCReadByte(bd);
  st2=FDCReadByte(bd);
  pcn=FDCReadByte(bd);
  hd=FDCReadByte(bd);
  sect=FDCReadByte(bd);
  size=FDCReadByte(bd);

  return st0;
}

U0 FDCRBlks(CDrv *dv,U8 *buf,I64 blk,I64 cnt)
{
  I64 n;
  CBlkDev *bd=dv->bd;
  U8 s;

  while (cnt>0) {
    s=(blk%(bd->spt))+1; // sector
    n=cnt;
    if (n>(bd->max_reads-s+1)) {
      n=bd->max_reads-s+1;
    }
    FDCReadBlks(bd,buf,blk,n);
    buf+n<<BLK_SIZE_BITS;
    blk+=n;
    cnt-=n;
  }
}

U0 FDCReadBlks(CBlkDev *bd,U8 *buf,I64 blk,I64 cnt)
{
  I64 retries=3;
  U8 c,h,s,st0;
  Bool unlock=BlkDevLock(bd);

  retry:
  if (retries) throw('BlkDev');

  FDCSelDrv(bd);

  c=blk/(bd->heads*bd->spt);
  h=blk/bd->spt%heads;
  s=blk%bd->spt+1;

  st0=FDCReadData(bd,c,h,s,s+cnt-1);

  if (st0>>6) {
    retries-=1;
    goto retry;
  }

  MemCpy(buf,&FDC_DMA,cnt*bd->blk_size);
}

U0 FDCWBlks(CDrv *dv,U8 *buf,I64 blk,I64 cnt)
{
  I64 n;
  CBlkDev *bd=dv->bd;
  U8 s;

  while (cnt>0) {
    s=(blk%(bd->spt))+1; // sector
    n=cnt;
    if (n>(bd->max_reads-s+1)) {
      n=bd->max_reads-s+1;
    }
    FDCWriteBlks(bd,buf,blk,n);
    buf+n<<BLK_SIZE_BITS;
    blk+=n;
    cnt-=n;
  }
}

U0 FDCWriteBlks(CBlkDev *bd,U8 *buf,I64 blk,I64 cnt)
{
  I64 retries=3;
  U8 c,h,s,st0;
  Bool unlock=BlkDevLock(bd);

  if (Bt(InU8(bd->base0+FDC_STAT_A),1))
    throw('BlkDev');

  retry:
  if (!retries) throw('BlkDev');

  MemCpy(&FDC_DMA,buf,cnt);

  FDCSelDrv(bd);

  c=blk/(bd->heads*bd->spt);
  h=blk/bd->spt%heads;
  s=blk%bd->spt+1;

  st0=FDCWriteData(bd,c,h,s,s+cnt-1);

  if (st0>>6) {
    retries-=1;
    goto retry;
  }
}

U8 FDCReadId(CBlkDev *bd,Bool hd)
{
  U8 st0,st1,st2,c,h,r,n;
  fdc_int_semaphore=FALSE;

  FDCSendByte(*bd,FDC_READ_ID|bd->mfm<<6);
  FDCSendByte(*bd,bd->unit|hd<<2);

  while (!fdc_int_semaphore) Yield;

  st0=FDCReadByte(*bd);
  st1=FDCReadByte(*bd);
  st2=FDCReadByte(*bd);
  c=FDCReadByte(*bd);
  h=FDCReadByte(*bd);
  r=FDCReadByte(*bd);
  n=FDCReadByte(*bd);

  return st0>>6;
}

U8 FDCChk(CBlkDev *bd)
{
  // Check if there is a disk of the selected density in the drive

  Bool unlock=BlkDevLock(bd),h0,h1;

  FDCSelDrv(bd);

  h0=!FDCReadId(bd,0);
  h1=!FDCReadId(bd,1);

  if (h0) {
    if (h1) {
      return FDC_CHK_DS;
    } else {
      return FDC_CHK_SS;
    }
  } else {
    return FDC_CHK_ERR;
  }
}