-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathquickdisk.c
373 lines (300 loc) · 9.29 KB
/
quickdisk.c
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
/*
* quickdisk.c
*
* Quick Disk interface control.
*
* Written & released by Keir Fraser <[email protected]>
*
* This is free and unencumbered software released into the public domain.
* See the file COPYING for more details, or visit <http://unlicense.org>.
*/
#define GPI_bus GPI_floating
#define GPO_bus GPO_pushpull(_2MHz, HIGH)
#define GPO_rdata GPO_pushpull(_2MHz, LOW)
#define AFO_rdata _AFO_pushpull(_2MHz,LOW)
/* READY-window state machine and timer handling. */
static struct window {
struct timer timer; /* Timer callback for state changes */
/* States describe action to take at *next* timer deadline. */
#define WIN_rdata_on 1 /* Activate RD */
#define WIN_ready_on 2 /* Assert /RY */
#define WIN_ready_off 3 /* Deassert /RY */
#define WIN_rdata_off 4 /* Mask RD */
uint8_t state; /* WIN_* */
/* For restarting reads after a write. */
bool_t paused;
uint32_t pause_pos;
} window;
/* RD-active has a wider window than /RY-asserted. */
#define rd_before_ry time_ms(10) /* RD-active before /RY-asserted */
#define rd_after_ry time_ms(10) /* RD-inactive after /RY-deasserted */
/* MOTOR state. */
static struct motor {
bool_t on; /* Is motor fully spun up? */
struct timer timer; /* Spin-up timer */
} motor;
/* Timer functions. */
static void motor_timer(void *_drv);
static void window_timer(void *_drv);
static void index_assert(void *);
/* Track and modify states of output pins. */
static volatile struct {
bool_t media;
bool_t wrprot;
bool_t ready;
} pins;
#define read_pin(pin) pins.pin
#define write_pin(pin, level) ({ \
unsigned int __pin = pin_##pin; \
if (__pin >= 16) \
gpio_write_pin(gpioa, __pin-16, level); \
else \
gpio_write_pin(gpiob, __pin, level); \
pins.pin = level; })
#include "floppy_generic.c"
void floppy_cancel(void)
{
struct drive *drv = &drive;
/* Initialised? Bail if not. */
if (!dma_rd)
return;
/* Immediately change outputs that we control entirely from the main loop.
* Asserting WRPROT prevents any further calls to wdata_start(). */
write_pin(wrprot, HIGH);
write_pin(media, HIGH);
/* Deasserts /RY and turns off motor. */
IRQx_set_pending(motor_irq);
/* Stop DMA + timer work. */
IRQx_disable(dma_rdata_irq);
IRQx_disable(dma_wdata_irq);
rdata_stop();
wdata_stop();
dma_rdata.ccr = 0;
dma_wdata.ccr = 0;
/* Clear soft state. */
timer_cancel(&window.timer);
timer_cancel(&index.timer);
barrier(); /* cancel index.timer /then/ clear dma rings */
dma_rd = dma_wr = NULL;
barrier(); /* /then/ clear soft state */
drv->index_suppressed = FALSE;
drv->image = image = NULL;
window.state = 0;
}
void floppy_set_fintf_mode(void)
{
/* Quick Disk interface is static. */
}
void floppy_set_max_cyl(void)
{
/* Quick Disk has no STEP signal. */
}
static void drive_configure_output_pin(unsigned int pin)
{
if (pin >= 16) {
gpio_configure_pin(gpioa, pin-16, GPO_bus);
} else {
gpio_configure_pin(gpiob, pin, GPO_bus);
}
}
void floppy_init(void)
{
struct drive *drv = &drive;
floppy_set_fintf_mode();
printk("Interface: QuickDisk, JC=%s\n",
qd_roland_mode() ? "On (Roland)" : "Off");
board_floppy_init();
timer_init(&motor.timer, motor_timer, drv);
timer_init(&window.timer, window_timer, drv);
drive_configure_output_pin(pin_02);
drive_configure_output_pin(pin_08);
drive_configure_output_pin(pin_26);
drive_configure_output_pin(pin_28);
drive_configure_output_pin(pin_34);
write_pin(media, HIGH);
write_pin(wrprot, HIGH);
write_pin(ready, HIGH);
floppy_init_irqs();
timer_init(&index.timer, index_assert, NULL);
}
void floppy_insert(unsigned int unit, struct slot *slot)
{
floppy_mount(slot);
timer_dma_init();
tim_rdata->ccr2 = sampleclk_ns(1500); /* RD: 1.5us positive pulses */
/* Drive is ready. Set output signals appropriately. */
write_pin(media, LOW);
if (!(slot->attributes & AM_RDO))
write_pin(wrprot, LOW);
/* Motor spins up, if enabled. */
IRQx_set_pending(motor_irq);
window.paused = FALSE;
}
static void floppy_unpause_window(struct drive *drv)
{
struct window *w = &window;
unsigned int i, state_times[] = {
drv->image->qd.win_start - rd_before_ry,
rd_before_ry,
drv->image->qd.win_end - drv->image->qd.win_start,
rd_after_ry };
uint32_t oldpri, pos = window.pause_pos;
time_t t;
oldpri = IRQ_save(TIMER_IRQ_PRI);
timer_cancel(&index.timer);
rdata_start();
if (!read_pin(ready))
gpio_configure_pin(gpio_data, pin_rdata, AFO_rdata);
t = index.timer.deadline = index.prev_time = time_now() - pos;
w->state = WIN_rdata_on;
for (i = 0; i < ARRAY_SIZE(state_times); i++) {
unsigned int delta = state_times[i];
if (pos < delta) {
timer_set(&w->timer, t + delta);
break;
}
w->state++;
t += delta;
pos -= delta;
}
window.paused = FALSE;
IRQ_restore(oldpri);
}
static void floppy_sync_flux(void)
{
const uint16_t buf_mask = ARRAY_SIZE(dma_rd->buf) - 1;
struct drive *drv = &drive;
uint16_t nr_to_wrap, nr_to_cons, nr;
uint32_t oldpri;
/* No DMA should occur until the timer is enabled. */
ASSERT(dma_rd->cons == (ARRAY_SIZE(dma_rd->buf) - dma_rdata.cndtr));
nr_to_wrap = ARRAY_SIZE(dma_rd->buf) - dma_rd->prod;
nr_to_cons = (dma_rd->cons - dma_rd->prod - 1) & buf_mask;
nr = min(nr_to_wrap, nr_to_cons);
if (nr) {
dma_rd->prod += image_rdata_flux(
drv->image, &dma_rd->buf[dma_rd->prod], nr);
dma_rd->prod &= buf_mask;
}
nr = (dma_rd->prod - dma_rd->cons) & buf_mask;
if (nr < buf_mask)
return;
if (window.paused)
return floppy_unpause_window(drv);
/* Must not currently be driving through the state machine. */
if (window.state != 0)
return;
/* Motor must be spun up to start reading. */
if (!motor.on)
return;
oldpri = IRQ_save(TIMER_IRQ_PRI);
timer_cancel(&index.timer);
rdata_start();
index.timer.deadline = time_now();
index_assert(NULL);
IRQ_restore(oldpri);
}
static bool_t dma_rd_handle(struct drive *drv)
{
switch (dma_rd->state) {
case DMA_inactive: {
time_t read_start_pos = window.paused ? window.pause_pos : 0;
read_start_pos %= drv->image->stk_per_rev;
read_start_pos *= SAMPLECLK_MHZ/STK_MHZ;
if (image_setup_track(drv->image, 0, &read_start_pos))
return TRUE;
/* Change state /then/ check for race against step or side change. */
dma_rd->state = DMA_starting;
barrier();
if (dma_wr->state != DMA_inactive)
dma_rd->state = DMA_stopping;
break;
}
case DMA_starting:
floppy_sync_flux();
/* fall through */
case DMA_active:
floppy_read_data(drv);
break;
case DMA_stopping:
dma_rd->state = DMA_inactive;
/* Reinitialise the circular buffer to empty. */
dma_rd->cons = dma_rd->prod =
ARRAY_SIZE(dma_rd->buf) - dma_rdata.cndtr;
/* Free-running index timer. */
timer_cancel(&index.timer);
timer_set(&index.timer, index.prev_time + drv->image->stk_per_rev);
break;
}
return FALSE;
}
void floppy_get_track(struct track_info *ti)
{
ti->cyl = ti->side = 0;
ti->sel = TRUE;
ti->writing = (dma_wr && dma_wr->state != DMA_inactive);
}
static void index_assert(void *dat)
{
struct drive *drv = &drive;
struct window *w = &window;
time_t now = index.timer.deadline;
index.prev_time = now;
if (motor.on && (dma_rd->state == DMA_active)) {
/* Reset the window state machine to start over. */
w->state = WIN_rdata_on;
timer_set(&w->timer, now + drv->image->qd.win_start - rd_before_ry);
} else {
/* Disable RDATA. */
rdata_stop();
/* Window state machine is idle. */
w->state = 0;
timer_cancel(&w->timer);
/* Stop any ongoing write. */
if (dma_wr->state != DMA_inactive)
IRQx_set_pending(wgate_irq);
}
}
static void motor_timer(void *_drv)
{
/* Motor is now spun up. */
motor.on = TRUE;
}
static void window_timer(void *_drv)
{
struct drive *drv = _drv;
struct window *w = &window;
time_t now = w->timer.deadline;
if (window.paused)
return;
switch (w->state) {
case WIN_rdata_on:
if (dma_rd->state == DMA_active)
gpio_configure_pin(gpio_data, pin_rdata, AFO_rdata);
timer_set(&w->timer, now + rd_before_ry);
break;
case WIN_ready_on:
if (motor.on)
write_pin(ready, LOW);
timer_set(&w->timer,
now + drv->image->qd.win_end - drv->image->qd.win_start);
break;
case WIN_ready_off:
write_pin(ready, HIGH);
timer_set(&w->timer, now + rd_after_ry);
break;
case WIN_rdata_off:
gpio_configure_pin(gpio_data, pin_rdata, GPO_rdata);
break;
}
w->state++;
}
/*
* Local variables:
* mode: C
* c-file-style: "Linux"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/