aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/comedi/drivers/addi-data/hwdrv_apci3501.c
blob: 375707497896f65fc562fdc1c35e4e5f67732f5d (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
/* Watchdog Related Defines */

#define ADDIDATA_TIMER			0
#define ADDIDATA_WATCHDOG		2

/*
 * (*insn_config) for the timer subdevice
 *
 * Configures The Timer, Counter or Watchdog
 * Data Pointer contains configuration parameters as below
 *	data[0] : 0 Configure As Timer
 *		  1 Configure As Counter
 *		  2 Configure As Watchdog
 *	data[1] : 1 Enable  Interrupt
 *		  0 Disable Interrupt
 *	data[2] : Time Unit
 *	data[3] : Reload Value
 */
static int apci3501_config_insn_timer(struct comedi_device *dev,
				      struct comedi_subdevice *s,
				      struct comedi_insn *insn,
				      unsigned int *data)
{
	struct apci3501_private *devpriv = dev->private;
	unsigned int ctrl;

	if (data[0] != ADDIDATA_WATCHDOG &&
	    data[0] != ADDIDATA_TIMER)
		return -EINVAL;

	devpriv->tsk_Current = current;

	devpriv->timer_mode = data[0];

	/* first, disable the watchdog or stop the timer */
	if (devpriv->timer_mode == ADDIDATA_WATCHDOG) {
		ctrl = 0;
	} else {
		ctrl = inl(devpriv->tcw + ADDI_TCW_CTRL_REG);
		ctrl &= ~(ADDI_TCW_CTRL_GATE | ADDI_TCW_CTRL_TRIG |
			  ADDI_TCW_CTRL_ENA);
	}
	outl(ctrl, devpriv->tcw + ADDI_TCW_CTRL_REG);

	/* enable/disable the timer interrupt */
	ctrl = (data[1] == 1) ? ADDI_TCW_CTRL_IRQ_ENA : 0;
	outl(ctrl, devpriv->tcw + ADDI_TCW_CTRL_REG);

	outl(data[2], devpriv->tcw + ADDI_TCW_TIMEBASE_REG);
	outl(data[3], devpriv->tcw + ADDI_TCW_RELOAD_REG);

	ctrl = inl(devpriv->tcw + ADDI_TCW_CTRL_REG);
	if (devpriv->timer_mode == ADDIDATA_WATCHDOG) {
		/* Set the mode (e2->e0) NOTE: this doesn't look correct */
		ctrl |= ~(ADDI_TCW_CTRL_CNT_UP | ADDI_TCW_CTRL_EXT_CLK_MASK |
			  ADDI_TCW_CTRL_MODE_MASK | ADDI_TCW_CTRL_GATE |
			  ADDI_TCW_CTRL_TRIG | ADDI_TCW_CTRL_TIMER_ENA |
			  ADDI_TCW_CTRL_RESET_ENA | ADDI_TCW_CTRL_WARN_ENA |
			  ADDI_TCW_CTRL_IRQ_ENA | ADDI_TCW_CTRL_ENA);
	} else {
		/* mode 2 */
		ctrl &= ~(ADDI_TCW_CTRL_CNTR_ENA | ADDI_TCW_CTRL_MODE_MASK |
			  ADDI_TCW_CTRL_GATE | ADDI_TCW_CTRL_TRIG |
			  ADDI_TCW_CTRL_TIMER_ENA | ADDI_TCW_CTRL_RESET_ENA |
			  ADDI_TCW_CTRL_WARN_ENA | ADDI_TCW_CTRL_ENA);
		ctrl |= ADDI_TCW_CTRL_MODE(2) | ADDI_TCW_CTRL_TIMER_ENA;
	}
	outl(ctrl, devpriv->tcw + ADDI_TCW_CTRL_REG);

	return insn->n;
}

/*
 * (*insn_write) for the timer subdevice
 *
 * Start / Stop The Selected Timer , Counter or Watchdog
 * Data Pointer contains configuration parameters as below
 *	data[0] : 0 Timer
 *		  1 Counter
 *		  2 Watchdog
 *	data[1] : 1 Start
 *		  0 Stop
 *		  2 Trigger
 */
static int apci3501_write_insn_timer(struct comedi_device *dev,
				     struct comedi_subdevice *s,
				     struct comedi_insn *insn,
				     unsigned int *data)
{
	struct apci3501_private *devpriv = dev->private;
	unsigned int ctrl;

	if (devpriv->timer_mode == ADDIDATA_WATCHDOG ||
	    devpriv->timer_mode == ADDIDATA_TIMER) {
		ctrl = inl(devpriv->tcw + ADDI_TCW_CTRL_REG);
		ctrl &= ~(ADDI_TCW_CTRL_GATE | ADDI_TCW_CTRL_TRIG);

		if (data[1] == 1) {		/* enable */
			ctrl |= ADDI_TCW_CTRL_ENA;
		} else if (data[1] == 0) {	/* stop */
			if (devpriv->timer_mode == ADDIDATA_WATCHDOG)
				ctrl = 0;
			else
				ctrl &= ~ADDI_TCW_CTRL_ENA;
		} else if (data[1] == 2) {	/* trigger */
			ctrl |= ADDI_TCW_CTRL_TRIG;
		}
		outl(ctrl, devpriv->tcw + ADDI_TCW_CTRL_REG);
	}

	inl(devpriv->tcw + ADDI_TCW_STATUS_REG);
	return insn->n;
}

/*
 * (*insn_read) for the timer subdevice
 *
 * Read The Selected Timer, Counter or Watchdog
 * Data Pointer contains configuration parameters as below
 *	data[0] : 0 Timer
 *		  1 Counter
 *		  2 Watchdog
 *	data[1] : Timer Counter Watchdog Number
 */
static int apci3501_read_insn_timer(struct comedi_device *dev,
				    struct comedi_subdevice *s,
				    struct comedi_insn *insn,
				    unsigned int *data)
{
	struct apci3501_private *devpriv = dev->private;

	if (devpriv->timer_mode != ADDIDATA_TIMER &&
	    devpriv->timer_mode != ADDIDATA_WATCHDOG)
		return -EINVAL;

	data[0] = inl(devpriv->tcw + ADDI_TCW_STATUS_REG) &
		  ADDI_TCW_STATUS_OVERFLOW;
	data[1] = inl(devpriv->tcw + ADDI_TCW_VAL_REG);

	return insn->n;
}