xref: /linux/drivers/staging/gpib/tms9914/tms9914.c (revision 91fff6fa94cbe13d28caa978ce3f600749304e11)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  *   copyright		  : (C) 2001, 2002 by Frank Mori Hess
5  ***************************************************************************/
6 
7 #include <linux/ioport.h>
8 #include <linux/sched.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <asm/dma.h>
12 #include <linux/io.h>
13 #include <linux/bitops.h>
14 #include <linux/pci.h>
15 #include <linux/pci_ids.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/delay.h>
20 
21 #include "gpibP.h"
22 #include "tms9914.h"
23 
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("GPIB library for tms9914");
26 
27 static unsigned int update_status_nolock(gpib_board_t *board, struct tms9914_priv *priv);
28 
tms9914_take_control(gpib_board_t * board,struct tms9914_priv * priv,int synchronous)29 int tms9914_take_control(gpib_board_t *board, struct tms9914_priv *priv, int synchronous)
30 {
31 	int i;
32 	const int timeout = 100;
33 
34 	if (synchronous)
35 		write_byte(priv, AUX_TCS, AUXCR);
36 	else
37 		write_byte(priv, AUX_TCA, AUXCR);
38 	// busy wait until ATN is asserted
39 	for (i = 0; i < timeout; i++) {
40 		if ((read_byte(priv, ADSR) & HR_ATN))
41 			break;
42 		udelay(1);
43 	}
44 	if (i == timeout)
45 		return -ETIMEDOUT;
46 
47 	clear_bit(WRITE_READY_BN, &priv->state);
48 
49 	return 0;
50 }
51 EXPORT_SYMBOL_GPL(tms9914_take_control);
52 
53 /* The agilent 82350B has a buggy implementation of tcs which interferes with the
54  * operation of tca.  It appears to be based on the controller state machine
55  * described in the TI 9900 TMS9914A data manual published in 1982.  This
56  * manual describes tcs as putting the controller into a CWAS
57  * state where it waits indefinitely for ANRS and ignores tca.	Since a
58  * functioning tca is far more important than tcs, we work around the
59  * problem by never issuing tcs.
60  *
61  * I don't know if this problem exists in the real tms9914a or just in the fpga
62  * of the 82350B.  For now, only the agilent_82350b uses this workaround.
63  * The rest of the tms9914 based drivers still use tms9914_take_control
64  * directly (which does issue tcs).
65  */
tms9914_take_control_workaround(gpib_board_t * board,struct tms9914_priv * priv,int synchronous)66 int tms9914_take_control_workaround(gpib_board_t *board, struct tms9914_priv *priv, int synchronous)
67 {
68 	if (synchronous)
69 		return -ETIMEDOUT;
70 	return tms9914_take_control(board, priv, synchronous);
71 }
72 EXPORT_SYMBOL_GPL(tms9914_take_control_workaround);
73 
tms9914_go_to_standby(gpib_board_t * board,struct tms9914_priv * priv)74 int tms9914_go_to_standby(gpib_board_t *board, struct tms9914_priv *priv)
75 {
76 	int i;
77 	const int timeout = 1000;
78 
79 	write_byte(priv, AUX_GTS, AUXCR);
80 	// busy wait until ATN is released
81 	for (i = 0; i < timeout; i++) {
82 		if ((read_byte(priv, ADSR) & HR_ATN) == 0)
83 			break;
84 		udelay(1);
85 	}
86 	if (i == timeout) {
87 		pr_err("error waiting for NATN\n");
88 		return -ETIMEDOUT;
89 	}
90 
91 	clear_bit(COMMAND_READY_BN, &priv->state);
92 
93 	return 0;
94 }
95 EXPORT_SYMBOL_GPL(tms9914_go_to_standby);
96 
tms9914_interface_clear(gpib_board_t * board,struct tms9914_priv * priv,int assert)97 void tms9914_interface_clear(gpib_board_t *board, struct tms9914_priv *priv, int assert)
98 {
99 	if (assert) {
100 		write_byte(priv, AUX_SIC | AUX_CS, AUXCR);
101 
102 		set_bit(CIC_NUM, &board->status);
103 	} else {
104 		write_byte(priv, AUX_SIC, AUXCR);
105 	}
106 }
107 EXPORT_SYMBOL_GPL(tms9914_interface_clear);
108 
tms9914_remote_enable(gpib_board_t * board,struct tms9914_priv * priv,int enable)109 void tms9914_remote_enable(gpib_board_t *board, struct tms9914_priv *priv, int enable)
110 {
111 	if (enable)
112 		write_byte(priv, AUX_SRE | AUX_CS, AUXCR);
113 	else
114 		write_byte(priv, AUX_SRE, AUXCR);
115 }
116 EXPORT_SYMBOL_GPL(tms9914_remote_enable);
117 
tms9914_request_system_control(gpib_board_t * board,struct tms9914_priv * priv,int request_control)118 void tms9914_request_system_control(gpib_board_t *board, struct tms9914_priv *priv,
119 				    int request_control)
120 {
121 	if (request_control) {
122 		write_byte(priv, AUX_RQC, AUXCR);
123 	} else {
124 		clear_bit(CIC_NUM, &board->status);
125 		write_byte(priv, AUX_RLC, AUXCR);
126 	}
127 }
128 EXPORT_SYMBOL_GPL(tms9914_request_system_control);
129 
tms9914_t1_delay(gpib_board_t * board,struct tms9914_priv * priv,unsigned int nano_sec)130 unsigned int tms9914_t1_delay(gpib_board_t *board, struct tms9914_priv *priv,
131 			      unsigned int nano_sec)
132 {
133 	static const int clock_period = 200;	// assuming 5Mhz input clock
134 	int num_cycles;
135 
136 	num_cycles = 12;
137 
138 	if (nano_sec <= 8 * clock_period) {
139 		write_byte(priv, AUX_STDL | AUX_CS, AUXCR);
140 		num_cycles = 8;
141 	} else {
142 		write_byte(priv, AUX_STDL, AUXCR);
143 	}
144 
145 	if (nano_sec <= 4 * clock_period) {
146 		write_byte(priv, AUX_VSTDL | AUX_CS, AUXCR);
147 		num_cycles = 4;
148 	} else {
149 		write_byte(priv, AUX_VSTDL, AUXCR);
150 	}
151 
152 	return num_cycles * clock_period;
153 }
154 EXPORT_SYMBOL_GPL(tms9914_t1_delay);
155 
tms9914_return_to_local(const gpib_board_t * board,struct tms9914_priv * priv)156 void tms9914_return_to_local(const gpib_board_t *board, struct tms9914_priv *priv)
157 {
158 	write_byte(priv, AUX_RTL, AUXCR);
159 }
160 EXPORT_SYMBOL_GPL(tms9914_return_to_local);
161 
tms9914_set_holdoff_mode(struct tms9914_priv * priv,enum tms9914_holdoff_mode mode)162 void tms9914_set_holdoff_mode(struct tms9914_priv *priv, enum tms9914_holdoff_mode mode)
163 {
164 	switch (mode) {
165 	case TMS9914_HOLDOFF_NONE:
166 		write_byte(priv, AUX_HLDE, AUXCR);
167 		write_byte(priv, AUX_HLDA, AUXCR);
168 		break;
169 	case TMS9914_HOLDOFF_EOI:
170 		write_byte(priv, AUX_HLDE | AUX_CS, AUXCR);
171 		write_byte(priv, AUX_HLDA, AUXCR);
172 		break;
173 	case TMS9914_HOLDOFF_ALL:
174 		write_byte(priv, AUX_HLDE, AUXCR);
175 		write_byte(priv, AUX_HLDA | AUX_CS, AUXCR);
176 		break;
177 	default:
178 		pr_err("%s: bug! bad holdoff mode %i\n", __func__, mode);
179 		break;
180 	}
181 	priv->holdoff_mode = mode;
182 }
183 EXPORT_SYMBOL_GPL(tms9914_set_holdoff_mode);
184 
tms9914_release_holdoff(struct tms9914_priv * priv)185 void tms9914_release_holdoff(struct tms9914_priv *priv)
186 {
187 	if (priv->holdoff_active) {
188 		write_byte(priv, AUX_RHDF, AUXCR);
189 		priv->holdoff_active = 0;
190 	}
191 }
192 EXPORT_SYMBOL_GPL(tms9914_release_holdoff);
193 
tms9914_enable_eos(gpib_board_t * board,struct tms9914_priv * priv,uint8_t eos_byte,int compare_8_bits)194 int tms9914_enable_eos(gpib_board_t *board, struct tms9914_priv *priv, uint8_t eos_byte,
195 		       int compare_8_bits)
196 {
197 	priv->eos = eos_byte;
198 	priv->eos_flags = REOS;
199 	if (compare_8_bits)
200 		priv->eos_flags |= BIN;
201 	return 0;
202 }
203 EXPORT_SYMBOL(tms9914_enable_eos);
204 
tms9914_disable_eos(gpib_board_t * board,struct tms9914_priv * priv)205 void tms9914_disable_eos(gpib_board_t *board, struct tms9914_priv *priv)
206 {
207 	priv->eos_flags &= ~REOS;
208 }
209 EXPORT_SYMBOL(tms9914_disable_eos);
210 
tms9914_parallel_poll(gpib_board_t * board,struct tms9914_priv * priv,uint8_t * result)211 int tms9914_parallel_poll(gpib_board_t *board, struct tms9914_priv *priv, uint8_t *result)
212 {
213 	// execute parallel poll
214 	write_byte(priv, AUX_CS | AUX_RPP, AUXCR);
215 	udelay(2);
216 	*result = read_byte(priv, CPTR);
217 	// clear parallel poll state
218 	write_byte(priv, AUX_RPP, AUXCR);
219 	return 0;
220 }
221 EXPORT_SYMBOL(tms9914_parallel_poll);
222 
set_ppoll_reg(struct tms9914_priv * priv,int enable,unsigned int dio_line,int sense,int ist)223 static void set_ppoll_reg(struct tms9914_priv *priv, int enable,
224 			  unsigned int dio_line, int sense, int ist)
225 {
226 	u8 dio_byte;
227 
228 	if (enable && ((sense && ist) || (!sense && !ist))) {
229 		dio_byte = 1 << (dio_line - 1);
230 		write_byte(priv, dio_byte, PPR);
231 	} else {
232 		write_byte(priv, 0, PPR);
233 	}
234 }
235 
tms9914_parallel_poll_configure(gpib_board_t * board,struct tms9914_priv * priv,uint8_t config)236 void tms9914_parallel_poll_configure(gpib_board_t *board,
237 				     struct tms9914_priv *priv, uint8_t config)
238 {
239 	priv->ppoll_enable = (config & PPC_DISABLE) == 0;
240 	priv->ppoll_line = (config & PPC_DIO_MASK) + 1;
241 	priv->ppoll_sense = (config & PPC_SENSE) != 0;
242 	set_ppoll_reg(priv, priv->ppoll_enable, priv->ppoll_line, priv->ppoll_sense, board->ist);
243 }
244 EXPORT_SYMBOL(tms9914_parallel_poll_configure);
245 
tms9914_parallel_poll_response(gpib_board_t * board,struct tms9914_priv * priv,int ist)246 void tms9914_parallel_poll_response(gpib_board_t *board,
247 				    struct tms9914_priv *priv, int ist)
248 {
249 	set_ppoll_reg(priv, priv->ppoll_enable, priv->ppoll_line, priv->ppoll_sense, ist);
250 }
251 EXPORT_SYMBOL(tms9914_parallel_poll_response);
252 
tms9914_serial_poll_response(gpib_board_t * board,struct tms9914_priv * priv,uint8_t status)253 void tms9914_serial_poll_response(gpib_board_t *board, struct tms9914_priv *priv, uint8_t status)
254 {
255 	unsigned long flags;
256 
257 	spin_lock_irqsave(&board->spinlock, flags);
258 	write_byte(priv, status, SPMR);
259 	priv->spoll_status = status;
260 	if (status & request_service_bit)
261 		write_byte(priv, AUX_RSV2 | AUX_CS, AUXCR);
262 	else
263 		write_byte(priv, AUX_RSV2, AUXCR);
264 	spin_unlock_irqrestore(&board->spinlock, flags);
265 }
266 EXPORT_SYMBOL(tms9914_serial_poll_response);
267 
tms9914_serial_poll_status(gpib_board_t * board,struct tms9914_priv * priv)268 uint8_t tms9914_serial_poll_status(gpib_board_t *board, struct tms9914_priv *priv)
269 {
270 	u8 status;
271 	unsigned long flags;
272 
273 	spin_lock_irqsave(&board->spinlock, flags);
274 	status = priv->spoll_status;
275 	spin_unlock_irqrestore(&board->spinlock, flags);
276 
277 	return status;
278 }
279 EXPORT_SYMBOL(tms9914_serial_poll_status);
280 
tms9914_primary_address(gpib_board_t * board,struct tms9914_priv * priv,unsigned int address)281 int tms9914_primary_address(gpib_board_t *board, struct tms9914_priv *priv, unsigned int address)
282 {
283 	// put primary address in address0
284 	write_byte(priv, address & ADDRESS_MASK, ADR);
285 	return 0;
286 }
287 EXPORT_SYMBOL(tms9914_primary_address);
288 
tms9914_secondary_address(gpib_board_t * board,struct tms9914_priv * priv,unsigned int address,int enable)289 int tms9914_secondary_address(gpib_board_t *board, struct tms9914_priv *priv,
290 			      unsigned int address, int enable)
291 {
292 	if (enable)
293 		priv->imr1_bits |= HR_APTIE;
294 	else
295 		priv->imr1_bits &= ~HR_APTIE;
296 
297 	write_byte(priv, priv->imr1_bits, IMR1);
298 	return 0;
299 }
300 EXPORT_SYMBOL(tms9914_secondary_address);
301 
tms9914_update_status(gpib_board_t * board,struct tms9914_priv * priv,unsigned int clear_mask)302 unsigned int tms9914_update_status(gpib_board_t *board, struct tms9914_priv *priv,
303 				   unsigned int clear_mask)
304 {
305 	unsigned long flags;
306 	unsigned int retval;
307 
308 	spin_lock_irqsave(&board->spinlock, flags);
309 	retval = update_status_nolock(board, priv);
310 	board->status &= ~clear_mask;
311 	spin_unlock_irqrestore(&board->spinlock, flags);
312 
313 	return retval;
314 }
315 EXPORT_SYMBOL(tms9914_update_status);
316 
update_talker_state(struct tms9914_priv * priv,unsigned int address_status_bits)317 static void update_talker_state(struct tms9914_priv *priv, unsigned int address_status_bits)
318 {
319 	if (address_status_bits & HR_TA)	{
320 		if (address_status_bits & HR_ATN)
321 			priv->talker_state = talker_addressed;
322 		else
323 			/* this could also be serial_poll_active, but the tms9914 provides no
324 			 * way to distinguish, so we'll assume talker_active
325 			 */
326 			priv->talker_state = talker_active;
327 	} else {
328 		priv->talker_state = talker_idle;
329 	}
330 }
331 
update_listener_state(struct tms9914_priv * priv,unsigned int address_status_bits)332 static void update_listener_state(struct tms9914_priv *priv, unsigned int address_status_bits)
333 {
334 	if (address_status_bits & HR_LA)	{
335 		if (address_status_bits & HR_ATN)
336 			priv->listener_state = listener_addressed;
337 		else
338 			priv->listener_state = listener_active;
339 	} else {
340 		priv->listener_state = listener_idle;
341 	}
342 }
343 
update_status_nolock(gpib_board_t * board,struct tms9914_priv * priv)344 static unsigned int update_status_nolock(gpib_board_t *board, struct tms9914_priv *priv)
345 {
346 	int address_status;
347 	int bsr_bits;
348 
349 	address_status = read_byte(priv, ADSR);
350 
351 	// check for remote/local
352 	if (address_status & HR_REM)
353 		set_bit(REM_NUM, &board->status);
354 	else
355 		clear_bit(REM_NUM, &board->status);
356 	// check for lockout
357 	if (address_status & HR_LLO)
358 		set_bit(LOK_NUM, &board->status);
359 	else
360 		clear_bit(LOK_NUM, &board->status);
361 	// check for ATN
362 	if (address_status & HR_ATN)
363 		set_bit(ATN_NUM, &board->status);
364 	else
365 		clear_bit(ATN_NUM, &board->status);
366 	// check for talker/listener addressed
367 	update_talker_state(priv, address_status);
368 	if (priv->talker_state == talker_active || priv->talker_state == talker_addressed)
369 		set_bit(TACS_NUM, &board->status);
370 	else
371 		clear_bit(TACS_NUM, &board->status);
372 
373 	update_listener_state(priv, address_status);
374 	if (priv->listener_state == listener_active || priv->listener_state == listener_addressed)
375 		set_bit(LACS_NUM, &board->status);
376 	else
377 		clear_bit(LACS_NUM, &board->status);
378 	// Check for SRQI - not reset elsewhere except in autospoll
379 	if (board->status & SRQI) {
380 		bsr_bits = read_byte(priv, BSR);
381 		if (!(bsr_bits & BSR_SRQ_BIT))
382 			clear_bit(SRQI_NUM, &board->status);
383 	}
384 
385 	dev_dbg(board->gpib_dev, "status 0x%lx, state 0x%lx\n", board->status, priv->state);
386 
387 	return board->status;
388 }
389 
tms9914_line_status(const gpib_board_t * board,struct tms9914_priv * priv)390 int tms9914_line_status(const gpib_board_t *board, struct tms9914_priv *priv)
391 {
392 	int bsr_bits;
393 	int status = ValidALL;
394 
395 	bsr_bits = read_byte(priv, BSR);
396 
397 	if (bsr_bits & BSR_REN_BIT)
398 		status |= BusREN;
399 	if (bsr_bits & BSR_IFC_BIT)
400 		status |= BusIFC;
401 	if (bsr_bits & BSR_SRQ_BIT)
402 		status |= BusSRQ;
403 	if (bsr_bits & BSR_EOI_BIT)
404 		status |= BusEOI;
405 	if (bsr_bits & BSR_NRFD_BIT)
406 		status |= BusNRFD;
407 	if (bsr_bits & BSR_NDAC_BIT)
408 		status |= BusNDAC;
409 	if (bsr_bits & BSR_DAV_BIT)
410 		status |= BusDAV;
411 	if (bsr_bits & BSR_ATN_BIT)
412 		status |= BusATN;
413 
414 	return status;
415 }
416 EXPORT_SYMBOL(tms9914_line_status);
417 
check_for_eos(struct tms9914_priv * priv,uint8_t byte)418 static int check_for_eos(struct tms9914_priv *priv, uint8_t byte)
419 {
420 	static const u8 seven_bit_compare_mask = 0x7f;
421 
422 	if ((priv->eos_flags & REOS) == 0)
423 		return 0;
424 
425 	if (priv->eos_flags & BIN) {
426 		if (priv->eos == byte)
427 			return 1;
428 	} else	{
429 		if ((priv->eos & seven_bit_compare_mask) == (byte & seven_bit_compare_mask))
430 			return 1;
431 	}
432 	return 0;
433 }
434 
wait_for_read_byte(gpib_board_t * board,struct tms9914_priv * priv)435 static int wait_for_read_byte(gpib_board_t *board, struct tms9914_priv *priv)
436 {
437 	if (wait_event_interruptible(board->wait,
438 				     test_bit(READ_READY_BN, &priv->state) ||
439 				     test_bit(DEV_CLEAR_BN, &priv->state) ||
440 				     test_bit(TIMO_NUM, &board->status))) {
441 		pr_debug("gpib: pio read wait interrupted\n");
442 		return -ERESTARTSYS;
443 	}
444 	if (test_bit(TIMO_NUM, &board->status))
445 		return -ETIMEDOUT;
446 
447 	if (test_bit(DEV_CLEAR_BN, &priv->state))
448 		return -EINTR;
449 	return 0;
450 }
451 
tms9914_read_data_in(gpib_board_t * board,struct tms9914_priv * priv,int * end)452 static inline uint8_t tms9914_read_data_in(gpib_board_t *board, struct tms9914_priv *priv, int *end)
453 {
454 	unsigned long flags;
455 	u8 data;
456 
457 	spin_lock_irqsave(&board->spinlock, flags);
458 	clear_bit(READ_READY_BN, &priv->state);
459 	data = read_byte(priv, DIR);
460 	if (test_and_clear_bit(RECEIVED_END_BN, &priv->state))
461 		*end = 1;
462 	else
463 		*end = 0;
464 	switch (priv->holdoff_mode) {
465 	case TMS9914_HOLDOFF_EOI:
466 		if (*end)
467 			priv->holdoff_active = 1;
468 		break;
469 	case TMS9914_HOLDOFF_ALL:
470 		priv->holdoff_active = 1;
471 		break;
472 	case TMS9914_HOLDOFF_NONE:
473 		break;
474 	default:
475 		pr_err("%s: bug! bad holdoff mode %i\n", __func__, priv->holdoff_mode);
476 		break;
477 	}
478 	spin_unlock_irqrestore(&board->spinlock, flags);
479 
480 	return data;
481 }
482 
pio_read(gpib_board_t * board,struct tms9914_priv * priv,uint8_t * buffer,size_t length,int * end,size_t * bytes_read)483 static int pio_read(gpib_board_t *board, struct tms9914_priv *priv, uint8_t *buffer,
484 		    size_t length, int *end, size_t *bytes_read)
485 {
486 	ssize_t retval = 0;
487 
488 	*bytes_read = 0;
489 	*end = 0;
490 	while (*bytes_read < length && *end == 0) {
491 		tms9914_release_holdoff(priv);
492 		retval = wait_for_read_byte(board, priv);
493 		if (retval < 0)
494 			return retval;
495 		buffer[(*bytes_read)++] = tms9914_read_data_in(board, priv, end);
496 
497 		if (check_for_eos(priv, buffer[*bytes_read - 1]))
498 			*end = 1;
499 	}
500 
501 	return retval;
502 }
503 
tms9914_read(gpib_board_t * board,struct tms9914_priv * priv,uint8_t * buffer,size_t length,int * end,size_t * bytes_read)504 int tms9914_read(gpib_board_t *board, struct tms9914_priv *priv, uint8_t *buffer,
505 		 size_t length, int *end, size_t *bytes_read)
506 {
507 	ssize_t retval = 0;
508 	size_t num_bytes;
509 
510 	*end = 0;
511 	*bytes_read = 0;
512 	if (length == 0)
513 		return 0;
514 
515 	clear_bit(DEV_CLEAR_BN, &priv->state);
516 
517 	// transfer data (except for last byte)
518 	if (length > 1)	{
519 		if (priv->eos_flags & REOS)
520 			tms9914_set_holdoff_mode(priv, TMS9914_HOLDOFF_ALL);
521 		else
522 			tms9914_set_holdoff_mode(priv, TMS9914_HOLDOFF_EOI);
523 		// PIO transfer
524 		retval = pio_read(board, priv, buffer, length - 1, end, &num_bytes);
525 		*bytes_read += num_bytes;
526 		if (retval < 0)
527 			return retval;
528 		buffer += num_bytes;
529 		length -= num_bytes;
530 	}
531 	// read last bytes if we havn't received an END yet
532 	if (*end == 0) {
533 		// make sure we holdoff after last byte read
534 		tms9914_set_holdoff_mode(priv, TMS9914_HOLDOFF_ALL);
535 		retval = pio_read(board, priv, buffer, length, end, &num_bytes);
536 		*bytes_read += num_bytes;
537 		if (retval < 0)
538 			return retval;
539 	}
540 	return 0;
541 }
542 EXPORT_SYMBOL(tms9914_read);
543 
pio_write_wait(gpib_board_t * board,struct tms9914_priv * priv)544 static int pio_write_wait(gpib_board_t *board, struct tms9914_priv *priv)
545 {
546 	// wait until next byte is ready to be sent
547 	if (wait_event_interruptible(board->wait,
548 				     test_bit(WRITE_READY_BN, &priv->state) ||
549 				     test_bit(BUS_ERROR_BN, &priv->state) ||
550 				     test_bit(DEV_CLEAR_BN, &priv->state) ||
551 				     test_bit(TIMO_NUM, &board->status))) {
552 		dev_dbg(board->gpib_dev, "gpib write interrupted!\n");
553 		return -ERESTARTSYS;
554 	}
555 	if (test_bit(TIMO_NUM, &board->status))
556 		return -ETIMEDOUT;
557 	if (test_bit(BUS_ERROR_BN, &priv->state))
558 		return -EIO;
559 	if (test_bit(DEV_CLEAR_BN, &priv->state))
560 		return -EINTR;
561 
562 	return 0;
563 }
564 
pio_write(gpib_board_t * board,struct tms9914_priv * priv,uint8_t * buffer,size_t length,size_t * bytes_written)565 static int pio_write(gpib_board_t *board, struct tms9914_priv *priv, uint8_t *buffer,
566 		     size_t length, size_t *bytes_written)
567 {
568 	ssize_t retval = 0;
569 	unsigned long flags;
570 
571 	*bytes_written = 0;
572 	while (*bytes_written < length) {
573 		retval = pio_write_wait(board, priv);
574 		if (retval < 0)
575 			break;
576 
577 		spin_lock_irqsave(&board->spinlock, flags);
578 		clear_bit(WRITE_READY_BN, &priv->state);
579 		write_byte(priv, buffer[(*bytes_written)++], CDOR);
580 		spin_unlock_irqrestore(&board->spinlock, flags);
581 	}
582 	retval = pio_write_wait(board, priv);
583 	if (retval < 0)
584 		return retval;
585 
586 	return length;
587 }
588 
tms9914_write(gpib_board_t * board,struct tms9914_priv * priv,uint8_t * buffer,size_t length,int send_eoi,size_t * bytes_written)589 int tms9914_write(gpib_board_t *board, struct tms9914_priv *priv, uint8_t *buffer, size_t length,
590 		  int send_eoi, size_t *bytes_written)
591 {
592 	ssize_t retval = 0;
593 
594 	*bytes_written = 0;
595 	if (length == 0)
596 		return 0;
597 
598 	clear_bit(BUS_ERROR_BN, &priv->state);
599 	clear_bit(DEV_CLEAR_BN, &priv->state);
600 
601 	if (send_eoi)
602 		length-- ; /* save the last byte for sending EOI */
603 
604 	if (length > 0)	{
605 		size_t num_bytes;
606 		// PIO transfer
607 		retval = pio_write(board, priv, buffer, length, &num_bytes);
608 		*bytes_written += num_bytes;
609 		if (retval < 0)
610 			return retval;
611 	}
612 	if (send_eoi) {
613 		size_t num_bytes;
614 		/*send EOI */
615 		write_byte(priv, AUX_SEOI, AUXCR);
616 
617 		retval = pio_write(board, priv, &buffer[*bytes_written], 1, &num_bytes);
618 		*bytes_written += num_bytes;
619 	}
620 	return retval;
621 }
622 EXPORT_SYMBOL(tms9914_write);
623 
check_my_address_state(gpib_board_t * board,struct tms9914_priv * priv,int cmd_byte)624 static void check_my_address_state(gpib_board_t *board, struct tms9914_priv *priv, int cmd_byte)
625 {
626 	if (cmd_byte == MLA(board->pad)) {
627 		priv->primary_listen_addressed = 1;
628 		// become active listener
629 		if (board->sad < 0)
630 			write_byte(priv, AUX_LON | AUX_CS, AUXCR);
631 	} else if (board->sad >= 0 && priv->primary_listen_addressed &&
632 		  cmd_byte == MSA(board->sad)) {
633 		// become active listener
634 		write_byte(priv, AUX_LON | AUX_CS, AUXCR);
635 	} else if (cmd_byte != MLA(board->pad) && (cmd_byte & 0xe0) == LAD) {
636 		priv->primary_listen_addressed = 0;
637 	} else if (cmd_byte == UNL) {
638 		priv->primary_listen_addressed = 0;
639 		write_byte(priv, AUX_LON, AUXCR);
640 	} else if (cmd_byte == MTA(board->pad))	{
641 		priv->primary_talk_addressed = 1;
642 		if (board->sad < 0)
643 			//make active talker
644 			write_byte(priv, AUX_TON | AUX_CS, AUXCR);
645 	} else if (board->sad >= 0 && priv->primary_talk_addressed &&
646 		   cmd_byte == MSA(board->sad)) {
647 		// become active talker
648 		write_byte(priv, AUX_TON | AUX_CS, AUXCR);
649 	} else if (cmd_byte != MTA(board->pad) && (cmd_byte & 0xe0) == TAD) {
650 		// Other Talk Address
651 		priv->primary_talk_addressed = 0;
652 		write_byte(priv, AUX_TON, AUXCR);
653 	} else if (cmd_byte == UNT) {
654 		priv->primary_talk_addressed = 0;
655 		write_byte(priv, AUX_TON, AUXCR);
656 	}
657 }
658 
tms9914_command(gpib_board_t * board,struct tms9914_priv * priv,uint8_t * buffer,size_t length,size_t * bytes_written)659 int tms9914_command(gpib_board_t *board, struct tms9914_priv *priv,  uint8_t *buffer,
660 		    size_t length, size_t *bytes_written)
661 {
662 	int retval = 0;
663 	unsigned long flags;
664 
665 	*bytes_written = 0;
666 	while (*bytes_written < length) {
667 		if (wait_event_interruptible(board->wait,
668 					     test_bit(COMMAND_READY_BN,
669 						      &priv->state) ||
670 					     test_bit(TIMO_NUM, &board->status))) {
671 			pr_debug("gpib command wait interrupted\n");
672 			break;
673 		}
674 		if (test_bit(TIMO_NUM, &board->status))
675 			break;
676 
677 		spin_lock_irqsave(&board->spinlock, flags);
678 		clear_bit(COMMAND_READY_BN, &priv->state);
679 		write_byte(priv, buffer[*bytes_written], CDOR);
680 		spin_unlock_irqrestore(&board->spinlock, flags);
681 
682 		check_my_address_state(board, priv, buffer[*bytes_written]);
683 
684 		++(*bytes_written);
685 	}
686 	// wait until last command byte is written
687 	if (wait_event_interruptible(board->wait,
688 				     test_bit(COMMAND_READY_BN,
689 					      &priv->state) || test_bit(TIMO_NUM, &board->status)))
690 		retval = -ERESTARTSYS;
691 	if (test_bit(TIMO_NUM, &board->status))
692 		retval = -ETIMEDOUT;
693 
694 	return retval;
695 }
696 EXPORT_SYMBOL(tms9914_command);
697 
tms9914_interrupt(gpib_board_t * board,struct tms9914_priv * priv)698 irqreturn_t tms9914_interrupt(gpib_board_t *board, struct tms9914_priv *priv)
699 {
700 	int status0, status1;
701 
702 	// read interrupt status (also clears status)
703 	status0 = read_byte(priv, ISR0);
704 	status1 = read_byte(priv, ISR1);
705 	return tms9914_interrupt_have_status(board, priv, status0, status1);
706 }
707 EXPORT_SYMBOL(tms9914_interrupt);
708 
tms9914_interrupt_have_status(gpib_board_t * board,struct tms9914_priv * priv,int status0,int status1)709 irqreturn_t tms9914_interrupt_have_status(gpib_board_t *board, struct tms9914_priv *priv,
710 					  int status0, int status1)
711 {
712 	// record reception of END
713 	if (status0 & HR_END)
714 		set_bit(RECEIVED_END_BN, &priv->state);
715 	// get incoming data in PIO mode
716 	if ((status0 & HR_BI))
717 		set_bit(READ_READY_BN, &priv->state);
718 	if ((status0 & HR_BO))	{
719 		if (read_byte(priv, ADSR) & HR_ATN)
720 			set_bit(COMMAND_READY_BN, &priv->state);
721 		else
722 			set_bit(WRITE_READY_BN, &priv->state);
723 	}
724 
725 	if (status0 & HR_SPAS) {
726 		priv->spoll_status &= ~request_service_bit;
727 		write_byte(priv, priv->spoll_status, SPMR);
728 		//FIXME: set SPOLL status bit
729 	}
730 	// record service request in status
731 	if (status1 & HR_SRQ)
732 		set_bit(SRQI_NUM, &board->status);
733 	// have been addressed (with secondary addressing disabled)
734 	if (status1 & HR_MA)
735 		// clear dac holdoff
736 		write_byte(priv, AUX_VAL, AUXCR);
737 	// unrecognized command received
738 	if (status1 & HR_UNC) {
739 		unsigned short command_byte = read_byte(priv, CPTR) & gpib_command_mask;
740 
741 		switch (command_byte) {
742 		case PPConfig:
743 			priv->ppoll_configure_state = 1;
744 			/* AUX_PTS generates another UNC interrupt on the next command byte
745 			 * if it is in the secondary address group (such as PPE and PPD).
746 			 */
747 			write_byte(priv, AUX_PTS, AUXCR);
748 			write_byte(priv, AUX_VAL, AUXCR);
749 			break;
750 		case PPU:
751 			tms9914_parallel_poll_configure(board, priv, command_byte);
752 			write_byte(priv, AUX_VAL, AUXCR);
753 			break;
754 		default:
755 			if (is_PPE(command_byte) || is_PPD(command_byte)) {
756 				if (priv->ppoll_configure_state) {
757 					tms9914_parallel_poll_configure(board, priv, command_byte);
758 					write_byte(priv, AUX_VAL, AUXCR);
759 				} else	{// bad parallel poll configure byte
760 					// clear dac holdoff
761 					write_byte(priv, AUX_INVAL, AUXCR);
762 				}
763 			} else	{
764 				// printk("tms9914: unrecognized gpib command pass thru 0x%x\n",
765 				// command_byte);
766 				// clear dac holdoff
767 				write_byte(priv, AUX_INVAL, AUXCR);
768 			}
769 			break;
770 		}
771 
772 		if (in_primary_command_group(command_byte) && command_byte != PPConfig)
773 			priv->ppoll_configure_state = 0;
774 	}
775 
776 	if (status1 & HR_ERR) {
777 		dev_dbg(board->gpib_dev, "gpib bus error\n");
778 		set_bit(BUS_ERROR_BN, &priv->state);
779 	}
780 
781 	if (status1 & HR_IFC) {
782 		push_gpib_event(board, EventIFC);
783 		clear_bit(CIC_NUM, &board->status);
784 	}
785 
786 	if (status1 & HR_GET) {
787 		push_gpib_event(board, EventDevTrg);
788 		// clear dac holdoff
789 		write_byte(priv, AUX_VAL, AUXCR);
790 	}
791 
792 	if (status1 & HR_DCAS) {
793 		push_gpib_event(board, EventDevClr);
794 		// clear dac holdoff
795 		write_byte(priv, AUX_VAL, AUXCR);
796 		set_bit(DEV_CLEAR_BN, &priv->state);
797 	}
798 
799 	// check for being addressed with secondary addressing
800 	if (status1 & HR_APT) {
801 		if (board->sad < 0)
802 			pr_err("tms9914: bug, APT interrupt without secondary addressing?\n");
803 		if ((read_byte(priv, CPTR) & gpib_command_mask) == MSA(board->sad))
804 			write_byte(priv, AUX_VAL, AUXCR);
805 		else
806 			write_byte(priv, AUX_INVAL, AUXCR);
807 	}
808 
809 	if ((status0 & priv->imr0_bits) || (status1 & priv->imr1_bits))	{
810 //		dev_dbg(board->gpib_dev, "isr0 0x%x, imr0 0x%x, isr1 0x%x, imr1 0x%x\n",
811 //			status0, priv->imr0_bits, status1, priv->imr1_bits);
812 		update_status_nolock(board, priv);
813 		wake_up_interruptible(&board->wait);
814 	}
815 	return IRQ_HANDLED;
816 }
817 EXPORT_SYMBOL(tms9914_interrupt_have_status);
818 
tms9914_board_reset(struct tms9914_priv * priv)819 void tms9914_board_reset(struct tms9914_priv *priv)
820 {
821 	/* chip reset */
822 	write_byte(priv, AUX_CHIP_RESET | AUX_CS, AUXCR);
823 
824 	/* disable all interrupts */
825 	priv->imr0_bits = 0;
826 	write_byte(priv, priv->imr0_bits, IMR0);
827 	priv->imr1_bits = 0;
828 	write_byte(priv, priv->imr1_bits, IMR1);
829 	write_byte(priv, AUX_DAI | AUX_CS, AUXCR);
830 
831 	/* clear registers by reading */
832 	read_byte(priv, CPTR);
833 	read_byte(priv, ISR0);
834 	read_byte(priv, ISR1);
835 
836 	write_byte(priv, 0, SPMR);
837 
838 	/* parallel poll unconfigure */
839 	write_byte(priv, 0, PPR);
840 	// request for data holdoff
841 	tms9914_set_holdoff_mode(priv, TMS9914_HOLDOFF_ALL);
842 }
843 EXPORT_SYMBOL_GPL(tms9914_board_reset);
844 
tms9914_online(gpib_board_t * board,struct tms9914_priv * priv)845 void tms9914_online(gpib_board_t *board, struct tms9914_priv *priv)
846 {
847 	/* set GPIB address */
848 	tms9914_primary_address(board, priv, board->pad);
849 	tms9914_secondary_address(board, priv, board->sad, board->sad >= 0);
850 
851 	// enable tms9914 interrupts
852 	priv->imr0_bits |= HR_MACIE | HR_RLCIE | HR_ENDIE | HR_BOIE | HR_BIIE |
853 		HR_SPASIE;
854 	priv->imr1_bits |= HR_MAIE | HR_SRQIE | HR_UNCIE | HR_ERRIE | HR_IFCIE |
855 		HR_GETIE | HR_DCASIE;
856 	write_byte(priv, priv->imr0_bits, IMR0);
857 	write_byte(priv, priv->imr1_bits, IMR1);
858 	write_byte(priv, AUX_DAI, AUXCR);
859 
860 	// turn off reset state
861 	write_byte(priv, AUX_CHIP_RESET, AUXCR);
862 }
863 EXPORT_SYMBOL_GPL(tms9914_online);
864 
865 #ifdef CONFIG_HAS_IOPORT
866 // wrapper for inb
tms9914_ioport_read_byte(struct tms9914_priv * priv,unsigned int register_num)867 uint8_t tms9914_ioport_read_byte(struct tms9914_priv *priv, unsigned int register_num)
868 {
869 	return inb(priv->iobase + register_num * priv->offset);
870 }
871 EXPORT_SYMBOL_GPL(tms9914_ioport_read_byte);
872 
873 // wrapper for outb
tms9914_ioport_write_byte(struct tms9914_priv * priv,uint8_t data,unsigned int register_num)874 void tms9914_ioport_write_byte(struct tms9914_priv *priv, uint8_t data, unsigned int register_num)
875 {
876 	outb(data, priv->iobase + register_num * priv->offset);
877 	if (register_num == AUXCR)
878 		udelay(1);
879 }
880 EXPORT_SYMBOL_GPL(tms9914_ioport_write_byte);
881 #endif
882 
883 // wrapper for readb
tms9914_iomem_read_byte(struct tms9914_priv * priv,unsigned int register_num)884 uint8_t tms9914_iomem_read_byte(struct tms9914_priv *priv, unsigned int register_num)
885 {
886 	return readb(priv->mmiobase + register_num * priv->offset);
887 }
888 EXPORT_SYMBOL_GPL(tms9914_iomem_read_byte);
889 
890 // wrapper for writeb
tms9914_iomem_write_byte(struct tms9914_priv * priv,uint8_t data,unsigned int register_num)891 void tms9914_iomem_write_byte(struct tms9914_priv *priv, uint8_t data, unsigned int register_num)
892 {
893 	writeb(data, priv->mmiobase + register_num * priv->offset);
894 	if (register_num == AUXCR)
895 		udelay(1);
896 }
897 EXPORT_SYMBOL_GPL(tms9914_iomem_write_byte);
898 
tms9914_init_module(void)899 static int __init tms9914_init_module(void)
900 {
901 	return 0;
902 }
903 
tms9914_exit_module(void)904 static void __exit tms9914_exit_module(void)
905 {
906 }
907 
908 module_init(tms9914_init_module);
909 module_exit(tms9914_exit_module);
910 
911