1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /***************************************************************************
4 * copyright : (C) 2002 by Frank Mori Hess
5 ***************************************************************************/
6
7 #include "nec7210.h"
8 #include "gpibP.h"
9 #include "amccs5933.h"
10
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13
14 enum {
15 PCI_DEVICE_ID_CBOARDS_PCI_GPIB = 0x6,
16 PCI_DEVICE_ID_CBOARDS_CPCI_GPIB = 0xe,
17 };
18
19 enum pci_chip {
20 PCI_CHIP_NONE = 0,
21 PCI_CHIP_AMCC_S5933,
22 PCI_CHIP_QUANCOM
23 };
24
25 // struct which defines private_data for cb7210 boards
26 struct cb7210_priv {
27 struct nec7210_priv nec7210_priv;
28 struct pci_dev *pci_device;
29 // base address of amccs5933 pci chip
30 unsigned long amcc_iobase;
31 unsigned long fifo_iobase;
32 unsigned int irq;
33 enum pci_chip pci_chip;
34 u8 hs_mode_bits;
35 unsigned out_fifo_half_empty : 1;
36 unsigned in_fifo_half_full : 1;
37 };
38
39 // interrupt service routines
40 irqreturn_t cb_pci_interrupt(int irq, void *arg);
41 irqreturn_t cb7210_interrupt(int irq, void *arg);
42 irqreturn_t cb7210_internal_interrupt(gpib_board_t *board);
43
44 // interface functions
45 int cb7210_read(gpib_board_t *board, uint8_t *buffer, size_t length,
46 int *end, size_t *bytes_read);
47 int cb7210_accel_read(gpib_board_t *board, uint8_t *buffer, size_t length,
48 int *end, size_t *bytes_read);
49 int cb7210_write(gpib_board_t *board, uint8_t *buffer, size_t length,
50 int send_eoi, size_t *bytes_written);
51 int cb7210_accel_write(gpib_board_t *board, uint8_t *buffer, size_t length,
52 int send_eoi, size_t *bytes_written);
53 int cb7210_command(gpib_board_t *board, uint8_t *buffer, size_t length, size_t *bytes_written);
54 int cb7210_take_control(gpib_board_t *board, int synchronous);
55 int cb7210_go_to_standby(gpib_board_t *board);
56 void cb7210_request_system_control(gpib_board_t *board, int request_control);
57 void cb7210_interface_clear(gpib_board_t *board, int assert);
58 void cb7210_remote_enable(gpib_board_t *board, int enable);
59 int cb7210_enable_eos(gpib_board_t *board, uint8_t eos_byte,
60 int compare_8_bits);
61 void cb7210_disable_eos(gpib_board_t *board);
62 unsigned int cb7210_update_status(gpib_board_t *board, unsigned int clear_mask);
63 int cb7210_primary_address(gpib_board_t *board, unsigned int address);
64 int cb7210_secondary_address(gpib_board_t *board, unsigned int address,
65 int enable);
66 int cb7210_parallel_poll(gpib_board_t *board, uint8_t *result);
67 void cb7210_serial_poll_response(gpib_board_t *board, uint8_t status);
68 uint8_t cb7210_serial_poll_status(gpib_board_t *board);
69 void cb7210_parallel_poll_configure(gpib_board_t *board, uint8_t configuration);
70 void cb7210_parallel_poll_response(gpib_board_t *board, int ist);
71 int cb7210_line_status(const gpib_board_t *board);
72 unsigned int cb7210_t1_delay(gpib_board_t *board, unsigned int nano_sec);
73 void cb7210_return_to_local(gpib_board_t *board);
74
75 // utility functions
76 void cb7210_generic_detach(gpib_board_t *board);
77 int cb7210_generic_attach(gpib_board_t *board);
78 int cb7210_init(struct cb7210_priv *priv, gpib_board_t *board);
79
80 // pcmcia init/cleanup
81 int cb_pcmcia_init_module(void);
82 void cb_pcmcia_cleanup_module(void);
83
84 // pci-gpib register offset
85 static const int cb7210_reg_offset = 1;
86
87 // uses 10 ioports
88 static const int cb7210_iosize = 10;
89
90 // fifo size in bytes
91 static const int cb7210_fifo_size = 2048;
92 static const int cb7210_fifo_width = 2;
93
94 // cb7210 specific registers and bits
95 enum cb7210_regs {
96 BUS_STATUS = 0x7,
97 };
98
99 enum cb7210_page_in {
100 BUS_STATUS_PAGE = 1,
101 };
102
103 enum hs_regs {
104 //write registers
105 HS_MODE = 0x8, /* HS_MODE register */
106 HS_INT_LEVEL = 0x9, /* HS_INT_LEVEL register */
107 //read registers
108 HS_STATUS = 0x8, /* HS_STATUS register */
109 };
110
nec7210_iobase(const struct cb7210_priv * cb_priv)111 static inline u32 nec7210_iobase(const struct cb7210_priv *cb_priv)
112 {
113 return cb_priv->nec7210_priv.iobase;
114 }
115
cb7210_page_in_bits(unsigned int page)116 static inline int cb7210_page_in_bits(unsigned int page)
117 {
118 return 0x50 | (page & 0xf);
119 }
120
cb7210_paged_read_byte(struct cb7210_priv * cb_priv,unsigned int register_num,unsigned int page)121 static inline uint8_t cb7210_paged_read_byte(struct cb7210_priv *cb_priv,
122 unsigned int register_num, unsigned int page)
123 {
124 struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
125 u8 retval;
126 unsigned long flags;
127
128 spin_lock_irqsave(&nec_priv->register_page_lock, flags);
129 outb(cb7210_page_in_bits(page), nec7210_iobase(cb_priv) + AUXMR * nec_priv->offset);
130 udelay(1);
131 retval = inb(nec7210_iobase(cb_priv) + register_num * nec_priv->offset);
132 spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
133 return retval;
134 }
135
136 // don't use for register_num < 8, since it doesn't lock
cb7210_read_byte(const struct cb7210_priv * cb_priv,enum hs_regs register_num)137 static inline uint8_t cb7210_read_byte(const struct cb7210_priv *cb_priv,
138 enum hs_regs register_num)
139 {
140 const struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
141 u8 retval;
142
143 retval = inb(nec7210_iobase(cb_priv) + register_num * nec_priv->offset);
144 return retval;
145 }
146
cb7210_paged_write_byte(struct cb7210_priv * cb_priv,uint8_t data,unsigned int register_num,unsigned int page)147 static inline void cb7210_paged_write_byte(struct cb7210_priv *cb_priv, uint8_t data,
148 unsigned int register_num, unsigned int page)
149 {
150 struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
151 unsigned long flags;
152
153 spin_lock_irqsave(&nec_priv->register_page_lock, flags);
154 outb(cb7210_page_in_bits(page), nec7210_iobase(cb_priv) + AUXMR * nec_priv->offset);
155 udelay(1);
156 outb(data, nec7210_iobase(cb_priv) + register_num * nec_priv->offset);
157 spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
158 }
159
160 // don't use for register_num < 8, since it doesn't lock
cb7210_write_byte(const struct cb7210_priv * cb_priv,uint8_t data,enum hs_regs register_num)161 static inline void cb7210_write_byte(const struct cb7210_priv *cb_priv, uint8_t data,
162 enum hs_regs register_num)
163 {
164 const struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
165
166 outb(data, nec7210_iobase(cb_priv) + register_num * nec_priv->offset);
167 }
168
169 enum bus_status_bits {
170 BSR_ATN_BIT = 0x1,
171 BSR_EOI_BIT = 0x2,
172 BSR_SRQ_BIT = 0x4,
173 BSR_IFC_BIT = 0x8,
174 BSR_REN_BIT = 0x10,
175 BSR_DAV_BIT = 0x20,
176 BSR_NRFD_BIT = 0x40,
177 BSR_NDAC_BIT = 0x80,
178 };
179
180 /* CBI 488.2 HS control */
181
182 /* when both bit 0 and 1 are set, it
183 * 1 clears the transmit state machine to an initial condition
184 * 2 clears any residual interrupts left latched on cbi488.2
185 * 3 resets all control bits in HS_MODE to zero
186 * 4 enables TX empty interrupts
187 * when both bit 0 and 1 are zero, then the high speed mode is disabled
188 */
189 enum hs_mode_bits {
190 HS_ENABLE_MASK = 0x3,
191 HS_TX_ENABLE = (1 << 0),
192 HS_RX_ENABLE = (1 << 1),
193 HS_HF_INT_EN = (1 << 3),
194 HS_CLR_SRQ_INT = (1 << 4),
195 HS_CLR_EOI_EMPTY_INT = (1 << 5),
196 HS_CLR_HF_INT = (1 << 6),
197 HS_SYS_CONTROL = (1 << 7),
198 };
199
200 /* CBI 488.2 status */
201 enum hs_status_bits {
202 HS_FIFO_FULL = (1 << 0),
203 HS_HALF_FULL = (1 << 1),
204 HS_SRQ_INT = (1 << 2),
205 HS_EOI_INT = (1 << 3),
206 HS_TX_MSB_NOT_EMPTY = (1 << 4),
207 HS_RX_MSB_NOT_EMPTY = (1 << 5),
208 HS_TX_LSB_NOT_EMPTY = (1 << 6),
209 HS_RX_LSB_NOT_EMPTY = (1 << 7),
210 };
211
212 /* CBI488.2 hs_int_level register */
213 enum hs_int_level_bits {
214 HS_RESET7210 = (1 << 7),
215 };
216
irq_bits(unsigned int irq)217 static inline unsigned int irq_bits(unsigned int irq)
218 {
219 switch (irq) {
220 case 2:
221 case 3:
222 case 4:
223 case 5:
224 return irq - 1;
225 case 7:
226 return 0x5;
227 case 10:
228 return 0x6;
229 case 11:
230 return 0x7;
231 default:
232 return 0;
233 }
234 }
235
236 enum cb7210_aux_cmds {
237 /* AUX_RTL2 is an undocumented aux command which causes cb7210 to assert
238 * (and keep asserted) local rtl message. This is used in conjunction
239 * with the (stupid) cb7210 implementation
240 * of the normal nec7210 AUX_RTL aux command, which
241 * causes the rtl message to toggle between on and off.
242 */
243 AUX_RTL2 = 0xd,
244 AUX_LO_SPEED = 0x40,
245 AUX_HI_SPEED = 0x41,
246 };
247