summaryrefslogtreecommitdiff
path: root/drivers/block/swim_asm.S
blob: 489512a0f561b2aea0400672bd8db8dc93b77623 (plain) (blame)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * low-level functions for the SWIM floppy controller
 *
 * needs assembly language because is very timing dependent
 * this controller exists only on macintosh 680x0 based
 *
 * Copyright (C) 2004, 2008 Laurent Vivier <Laurent@lvivier.info>
 *
 * based on Alastair Bridgewater SWIM analysis, 2001
 * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath.
 *
 * 2004-08-21 (lv) - Initial implementation
 * 2008-11-05 (lv) - add get_swim_mode
 */

	.equ	.Lwrite_data,		0x0000
	.equ	.Lwrite_mark,		0x0200
	.equ	.Lwrite_CRC,		0x0400
	.equ	.Lwrite_parameter,	0x0600
	.equ	.Lwrite_phase,		0x0800
	.equ	.Lwrite_setup,		0x0a00
	.equ	.Lwrite_mode0,		0x0c00
	.equ	.Lwrite_mode1,		0x0e00
	.equ	.Lread_data,		0x1000
	.equ	.Lread_mark,		0x1200
	.equ	.Lread_error,		0x1400
	.equ	.Lread_parameter,	0x1600
	.equ	.Lread_phase,		0x1800
	.equ	.Lread_setup,		0x1a00
	.equ	.Lread_status,		0x1c00
	.equ	.Lread_handshake,	0x1e00

	.equ	.Lo_side,		0
	.equ	.Lo_track,		1
	.equ	.Lo_sector,		2
	.equ	.Lo_size,		3
	.equ	.Lo_crc0,		4
	.equ	.Lo_crc1,		5

	.equ	.Lseek_time,		30000
	.equ	.Lmax_retry,		40
	.equ	.Lsector_size,		512
	.equ	.Lmark_sequence_len,	4

	.equ	.Lmr_clear_fifo,	0x01
	.equ	.Lmr_action,		0x08
	.equ	.Lmr_write_action,	0x18

	.equ	.Lhr_crc_error,		0x02
	.equ	.Lhr_fifo_2bytes,	0x40
	.equ	.Lhr_fifo_1byte,	0x80

.Lmfm_mark_check:
	/*
	 * This subroutine reads and validates a mark byte sequence.
	 * On entry, %a1 and %d4 shall hold the location and length (resp.)
	 * of the mark byte array.
	 * %a2 and %a3 shall hold the locations of the handshake and mark
	 * registers.
	 * Returns zero in %d1 for success.
	 */

	moveq	#-1, %d1
	subq	#1, %d4
	movew	#.Lseek_time, %d2

5:	tstb	%a2@
	dbmi	%d2, 5b
	bpl	6f

	moveb	%a3@, %d3
	cmpb	%a1@+, %d3
	dbne	%d4, 5b
	bne	6f

	moveq	#0, %d1
6:	rts

	.global swim_read_sector_header
swim_read_sector_header:
	link	%a6, #0
	moveml	%d1-%d5/%a0-%a5,%sp@-
	movel	%a6@(0x0c), %a4
	moveq	#-1, %d0
	bsr	.Lmfm_read_header
	moveml	%sp@+, %d1-%d5/%a0-%a5
	unlk	%a6
	rts

.Lsector_address_mark:
	.byte	0xa1, 0xa1, 0xa1, 0xfe
.Lsector_data_mark:
	.byte	0xa1, 0xa1, 0xa1, 0xfb

.Lmfm_read_header:
	movel	%a6@(0x08), %a3
	lea	%a3@(.Lread_handshake), %a2
	lea	%a3@(.Lread_data), %a5
	lea	%a3@(.Lread_mark), %a3

	moveb	#.Lmr_write_action, %a3@(.Lwrite_mode0 - .Lread_mark)
	moveb	#.Lmr_clear_fifo, %a3@(.Lwrite_mode1 - .Lread_mark)
	moveb	#.Lmr_clear_fifo, %a3@(.Lwrite_mode0 - .Lread_mark)
	tstb	%a3@(.Lread_error - .Lread_mark)
	moveb	#.Lmr_action, %a3@(.Lwrite_mode1 - .Lread_mark)

	lea	.Lsector_address_mark, %a1
	moveq	#.Lmark_sequence_len, %d4
	bsr	.Lmfm_mark_check
	tstl	%d1
	bne	.Lsignal_nonyb

	/* read header */

	moveq	#.Lmax_retry, %d2
.Lamark0:
	tstb	%a2@
	dbmi	%d2, .Lamark0
	bpl	.Lsignal_nonyb

	moveb	%a5@, %a4@(.Lo_track)

	moveq	#.Lmax_retry, %d2
.Lamark1:
	tstb	%a2@
	dbmi	%d2, .Lamark1
	bpl	.Lsignal_nonyb

	moveb	%a5@, %a4@(.Lo_side)

	moveq	#.Lmax_retry, %d2
.Lamark2:
	tstb	%a2@
	dbmi	%d2, .Lamark2
	bpl	.Lsignal_nonyb

	moveb	%a5@, %a4@(.Lo_sector)

	moveq	#.Lmax_retry, %d2
.Lamark3:
	tstb	%a2@
	dbmi	%d2, .Lamark3
	bpl	.Lsignal_nonyb

	moveb	%a5@, %a4@(.Lo_size)

	moveq	#.Lmax_retry, %d2
.Lcrc0:
	tstb	%a2@
	dbmi	%d2, .Lcrc0
	bpl	.Lsignal_nonyb

	moveb	%a5@, %a4@(.Lo_crc0)

	moveq	#.Lmax_retry, %d2
.Lcrc1:
	tstb	%a2@
	dbmi	%d2, .Lcrc1
	bpl	.Lsignal_nonyb

	moveb	%a5@, %a4@(.Lo_crc1)

	moveb	%a2@, %d5
	andb	#.Lhr_crc_error, %d5
	bne	.Lsignal_nonyb

	moveq	#0, %d0
.Lsignal_nonyb:
	moveb	#.Lmr_write_action, %a3@(.Lwrite_mode0 - .Lread_mark)
	rts

	.global swim_read_sector_data
swim_read_sector_data:
	link	%a6, #0
	moveml	%d1-%d5/%a0-%a5,%sp@-
	movel	%a6@(0x0c), %a4
	moveq	#-1, %d0
	bsr	.Lmfm_read_data
	moveml	%sp@+, %d1-%d5/%a0-%a5
	unlk	%a6
	rts

.Lmfm_read_data:
	movel	%a6@(0x08), %a3
	lea	%a3@(.Lread_handshake), %a2
	lea	%a3@(.Lread_data), %a5
	lea	%a3@(.Lread_mark), %a3

	moveb	#.Lmr_write_action, %a3@(.Lwrite_mode0 - .Lread_mark)
	moveb	#.Lmr_clear_fifo, %a3@(.Lwrite_mode1 - .Lread_mark)
	moveb	#.Lmr_clear_fifo, %a3@(.Lwrite_mode0 - .Lread_mark)
	tstb	%a3@(.Lread_error - .Lread_mark)
	moveb	#.Lmr_action, %a3@(.Lwrite_mode1 - .Lread_mark)

	lea	.Lsector_data_mark, %a1
	moveq	#.Lmark_sequence_len, %d4
	bsr	.Lmfm_mark_check
	tstl	%d1
	bne	.Ldata_exit

	/* read data */

	movel	#.Lsector_size - 1, %d4		/* sector size */
	movew	#.Lmax_retry, %d2
.Lread_data_loop:
	moveb	%a2@, %d5
	andb	#(.Lhr_fifo_1byte + .Lhr_fifo_2bytes), %d5
	dbne	%d2, .Lread_data_loop
	beq	.Ldata_exit

	moveq	#.Lmax_retry, %d2
	moveb	%a5@, %d3
	bsr	.Lmaybe_store
	dbra	%d4, 1f
	bra	.Ldata_crc0
1:	andb	#.Lhr_fifo_2bytes, %d5
	beq	.Lread_data_loop
	moveb	%a5@, %d3
	bsr	.Lmaybe_store
	dbra	%d4, .Lread_data_loop

	/* read CRC */

	movew	#.Lmax_retry, %d2
.Ldata_crc0:
	tstb	%a2@
	dbmi	%d2, .Ldata_crc0
	bpl	.Ldata_exit

	moveb	%a5@, %d5

	moveq	#.Lmax_retry, %d2
.Ldata_crc1:
	tstb	%a2@
	dbmi	%d2, .Ldata_crc1
	bpl	.Ldata_exit

	moveb	%a5@, %d5

	moveb	%a2@, %d5
	andb	#.Lhr_crc_error, %d5
	bne	.Ldata_exit

	/* return number of bytes read */

	movel	#.Lsector_size, %d0
	addw	#1, %d4
	subl	%d4, %d0

.Ldata_exit:
	moveb	#.Lmr_write_action, %a3@(.Lwrite_mode0 - .Lread_mark)
	rts

.Lmaybe_store:
	tstl	%a4
	beq	9f
	moveb	%d3, %a4@+
9:	rts