/drivers/staging/comedi/drivers/ni_mio_common.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 6005 lines · 4968 code · 604 blank · 433 comment · 869 complexity · c44039260f42b06fffd172846b247bbb MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. comedi/drivers/ni_mio_common.c
  3. Hardware driver for DAQ-STC based boards
  4. COMEDI - Linux Control and Measurement Device Interface
  5. Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
  6. Copyright (C) 2002-2006 Frank Mori Hess <fmhess@users.sourceforge.net>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. This file is meant to be included by another file, e.g.,
  21. ni_atmio.c or ni_pcimio.c.
  22. Interrupt support originally added by Truxton Fulton
  23. <trux@truxton.com>
  24. References (from ftp://ftp.natinst.com/support/manuals):
  25. 340747b.pdf AT-MIO E series Register Level Programmer Manual
  26. 341079b.pdf PCI E Series RLPM
  27. 340934b.pdf DAQ-STC reference manual
  28. 67xx and 611x registers (from ftp://ftp.ni.com/support/daq/mhddk/documentation/)
  29. release_ni611x.pdf
  30. release_ni67xx.pdf
  31. Other possibly relevant info:
  32. 320517c.pdf User manual (obsolete)
  33. 320517f.pdf User manual (new)
  34. 320889a.pdf delete
  35. 320906c.pdf maximum signal ratings
  36. 321066a.pdf about 16x
  37. 321791a.pdf discontinuation of at-mio-16e-10 rev. c
  38. 321808a.pdf about at-mio-16e-10 rev P
  39. 321837a.pdf discontinuation of at-mio-16de-10 rev d
  40. 321838a.pdf about at-mio-16de-10 rev N
  41. ISSUES:
  42. - the interrupt routine needs to be cleaned up
  43. 2006-02-07: S-Series PCI-6143: Support has been added but is not
  44. fully tested as yet. Terry Barnaby, BEAM Ltd.
  45. */
  46. /* #define DEBUG_INTERRUPT */
  47. /* #define DEBUG_STATUS_A */
  48. /* #define DEBUG_STATUS_B */
  49. #include <linux/interrupt.h>
  50. #include <linux/sched.h>
  51. #include "8255.h"
  52. #include "mite.h"
  53. #include "comedi_fc.h"
  54. #ifndef MDPRINTK
  55. #define MDPRINTK(format, args...)
  56. #endif
  57. /* A timeout count */
  58. #define NI_TIMEOUT 1000
  59. static const unsigned old_RTSI_clock_channel = 7;
  60. /* Note: this table must match the ai_gain_* definitions */
  61. static const short ni_gainlkup[][16] = {
  62. [ai_gain_16] = {0, 1, 2, 3, 4, 5, 6, 7,
  63. 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
  64. [ai_gain_8] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
  65. [ai_gain_14] = {1, 2, 3, 4, 5, 6, 7,
  66. 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
  67. [ai_gain_4] = {0, 1, 4, 7},
  68. [ai_gain_611x] = {0x00a, 0x00b, 0x001, 0x002,
  69. 0x003, 0x004, 0x005, 0x006},
  70. [ai_gain_622x] = {0, 1, 4, 5},
  71. [ai_gain_628x] = {1, 2, 3, 4, 5, 6, 7},
  72. [ai_gain_6143] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  73. };
  74. static const struct comedi_lrange range_ni_E_ai = { 16, {
  75. RANGE(-10, 10),
  76. RANGE(-5, 5),
  77. RANGE(-2.5, 2.5),
  78. RANGE(-1, 1),
  79. RANGE(-0.5, 0.5),
  80. RANGE(-0.25, 0.25),
  81. RANGE(-0.1, 0.1),
  82. RANGE(-0.05, 0.05),
  83. RANGE(0, 20),
  84. RANGE(0, 10),
  85. RANGE(0, 5),
  86. RANGE(0, 2),
  87. RANGE(0, 1),
  88. RANGE(0, 0.5),
  89. RANGE(0, 0.2),
  90. RANGE(0, 0.1),
  91. }
  92. };
  93. static const struct comedi_lrange range_ni_E_ai_limited = { 8, {
  94. RANGE(-10, 10),
  95. RANGE(-5, 5),
  96. RANGE(-1, 1),
  97. RANGE(-0.1,
  98. 0.1),
  99. RANGE(0, 10),
  100. RANGE(0, 5),
  101. RANGE(0, 1),
  102. RANGE(0, 0.1),
  103. }
  104. };
  105. static const struct comedi_lrange range_ni_E_ai_limited14 = { 14, {
  106. RANGE(-10,
  107. 10),
  108. RANGE(-5, 5),
  109. RANGE(-2, 2),
  110. RANGE(-1, 1),
  111. RANGE(-0.5,
  112. 0.5),
  113. RANGE(-0.2,
  114. 0.2),
  115. RANGE(-0.1,
  116. 0.1),
  117. RANGE(0, 10),
  118. RANGE(0, 5),
  119. RANGE(0, 2),
  120. RANGE(0, 1),
  121. RANGE(0,
  122. 0.5),
  123. RANGE(0,
  124. 0.2),
  125. RANGE(0,
  126. 0.1),
  127. }
  128. };
  129. static const struct comedi_lrange range_ni_E_ai_bipolar4 = { 4, {
  130. RANGE(-10, 10),
  131. RANGE(-5, 5),
  132. RANGE(-0.5,
  133. 0.5),
  134. RANGE(-0.05,
  135. 0.05),
  136. }
  137. };
  138. static const struct comedi_lrange range_ni_E_ai_611x = { 8, {
  139. RANGE(-50, 50),
  140. RANGE(-20, 20),
  141. RANGE(-10, 10),
  142. RANGE(-5, 5),
  143. RANGE(-2, 2),
  144. RANGE(-1, 1),
  145. RANGE(-0.5, 0.5),
  146. RANGE(-0.2, 0.2),
  147. }
  148. };
  149. static const struct comedi_lrange range_ni_M_ai_622x = { 4, {
  150. RANGE(-10, 10),
  151. RANGE(-5, 5),
  152. RANGE(-1, 1),
  153. RANGE(-0.2, 0.2),
  154. }
  155. };
  156. static const struct comedi_lrange range_ni_M_ai_628x = { 7, {
  157. RANGE(-10, 10),
  158. RANGE(-5, 5),
  159. RANGE(-2, 2),
  160. RANGE(-1, 1),
  161. RANGE(-0.5, 0.5),
  162. RANGE(-0.2, 0.2),
  163. RANGE(-0.1, 0.1),
  164. }
  165. };
  166. static const struct comedi_lrange range_ni_S_ai_6143 = { 1, {
  167. RANGE(-5, +5),
  168. }
  169. };
  170. static const struct comedi_lrange range_ni_E_ao_ext = { 4, {
  171. RANGE(-10, 10),
  172. RANGE(0, 10),
  173. RANGE_ext(-1, 1),
  174. RANGE_ext(0, 1),
  175. }
  176. };
  177. static const struct comedi_lrange *const ni_range_lkup[] = {
  178. [ai_gain_16] = &range_ni_E_ai,
  179. [ai_gain_8] = &range_ni_E_ai_limited,
  180. [ai_gain_14] = &range_ni_E_ai_limited14,
  181. [ai_gain_4] = &range_ni_E_ai_bipolar4,
  182. [ai_gain_611x] = &range_ni_E_ai_611x,
  183. [ai_gain_622x] = &range_ni_M_ai_622x,
  184. [ai_gain_628x] = &range_ni_M_ai_628x,
  185. [ai_gain_6143] = &range_ni_S_ai_6143
  186. };
  187. static int ni_dio_insn_config(struct comedi_device *dev,
  188. struct comedi_subdevice *s,
  189. struct comedi_insn *insn, unsigned int *data);
  190. static int ni_dio_insn_bits(struct comedi_device *dev,
  191. struct comedi_subdevice *s,
  192. struct comedi_insn *insn, unsigned int *data);
  193. static int ni_cdio_cmdtest(struct comedi_device *dev,
  194. struct comedi_subdevice *s, struct comedi_cmd *cmd);
  195. static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
  196. static int ni_cdio_cancel(struct comedi_device *dev,
  197. struct comedi_subdevice *s);
  198. static void handle_cdio_interrupt(struct comedi_device *dev);
  199. static int ni_cdo_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
  200. unsigned int trignum);
  201. static int ni_serial_insn_config(struct comedi_device *dev,
  202. struct comedi_subdevice *s,
  203. struct comedi_insn *insn, unsigned int *data);
  204. static int ni_serial_hw_readwrite8(struct comedi_device *dev,
  205. struct comedi_subdevice *s,
  206. unsigned char data_out,
  207. unsigned char *data_in);
  208. static int ni_serial_sw_readwrite8(struct comedi_device *dev,
  209. struct comedi_subdevice *s,
  210. unsigned char data_out,
  211. unsigned char *data_in);
  212. static int ni_calib_insn_read(struct comedi_device *dev,
  213. struct comedi_subdevice *s,
  214. struct comedi_insn *insn, unsigned int *data);
  215. static int ni_calib_insn_write(struct comedi_device *dev,
  216. struct comedi_subdevice *s,
  217. struct comedi_insn *insn, unsigned int *data);
  218. static int ni_eeprom_insn_read(struct comedi_device *dev,
  219. struct comedi_subdevice *s,
  220. struct comedi_insn *insn, unsigned int *data);
  221. static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
  222. struct comedi_subdevice *s,
  223. struct comedi_insn *insn,
  224. unsigned int *data);
  225. static int ni_pfi_insn_bits(struct comedi_device *dev,
  226. struct comedi_subdevice *s,
  227. struct comedi_insn *insn, unsigned int *data);
  228. static int ni_pfi_insn_config(struct comedi_device *dev,
  229. struct comedi_subdevice *s,
  230. struct comedi_insn *insn, unsigned int *data);
  231. static unsigned ni_old_get_pfi_routing(struct comedi_device *dev,
  232. unsigned chan);
  233. static void ni_rtsi_init(struct comedi_device *dev);
  234. static int ni_rtsi_insn_bits(struct comedi_device *dev,
  235. struct comedi_subdevice *s,
  236. struct comedi_insn *insn, unsigned int *data);
  237. static int ni_rtsi_insn_config(struct comedi_device *dev,
  238. struct comedi_subdevice *s,
  239. struct comedi_insn *insn, unsigned int *data);
  240. static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s);
  241. static int ni_read_eeprom(struct comedi_device *dev, int addr);
  242. #ifdef DEBUG_STATUS_A
  243. static void ni_mio_print_status_a(int status);
  244. #else
  245. #define ni_mio_print_status_a(a)
  246. #endif
  247. #ifdef DEBUG_STATUS_B
  248. static void ni_mio_print_status_b(int status);
  249. #else
  250. #define ni_mio_print_status_b(a)
  251. #endif
  252. static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s);
  253. #ifndef PCIDMA
  254. static void ni_handle_fifo_half_full(struct comedi_device *dev);
  255. static int ni_ao_fifo_half_empty(struct comedi_device *dev,
  256. struct comedi_subdevice *s);
  257. #endif
  258. static void ni_handle_fifo_dregs(struct comedi_device *dev);
  259. static int ni_ai_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
  260. unsigned int trignum);
  261. static void ni_load_channelgain_list(struct comedi_device *dev,
  262. unsigned int n_chan, unsigned int *list);
  263. static void shutdown_ai_command(struct comedi_device *dev);
  264. static int ni_ao_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
  265. unsigned int trignum);
  266. static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s);
  267. static int ni_8255_callback(int dir, int port, int data, unsigned long arg);
  268. static int ni_gpct_insn_write(struct comedi_device *dev,
  269. struct comedi_subdevice *s,
  270. struct comedi_insn *insn, unsigned int *data);
  271. static int ni_gpct_insn_read(struct comedi_device *dev,
  272. struct comedi_subdevice *s,
  273. struct comedi_insn *insn, unsigned int *data);
  274. static int ni_gpct_insn_config(struct comedi_device *dev,
  275. struct comedi_subdevice *s,
  276. struct comedi_insn *insn, unsigned int *data);
  277. static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
  278. static int ni_gpct_cmdtest(struct comedi_device *dev,
  279. struct comedi_subdevice *s, struct comedi_cmd *cmd);
  280. static int ni_gpct_cancel(struct comedi_device *dev,
  281. struct comedi_subdevice *s);
  282. static void handle_gpct_interrupt(struct comedi_device *dev,
  283. unsigned short counter_index);
  284. static int init_cs5529(struct comedi_device *dev);
  285. static int cs5529_do_conversion(struct comedi_device *dev,
  286. unsigned short *data);
  287. static int cs5529_ai_insn_read(struct comedi_device *dev,
  288. struct comedi_subdevice *s,
  289. struct comedi_insn *insn, unsigned int *data);
  290. #ifdef NI_CS5529_DEBUG
  291. static unsigned int cs5529_config_read(struct comedi_device *dev,
  292. unsigned int reg_select_bits);
  293. #endif
  294. static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
  295. unsigned int reg_select_bits);
  296. static int ni_m_series_pwm_config(struct comedi_device *dev,
  297. struct comedi_subdevice *s,
  298. struct comedi_insn *insn, unsigned int *data);
  299. static int ni_6143_pwm_config(struct comedi_device *dev,
  300. struct comedi_subdevice *s,
  301. struct comedi_insn *insn, unsigned int *data);
  302. static int ni_set_master_clock(struct comedi_device *dev, unsigned source,
  303. unsigned period_ns);
  304. static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status);
  305. static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status);
  306. enum aimodes {
  307. AIMODE_NONE = 0,
  308. AIMODE_HALF_FULL = 1,
  309. AIMODE_SCAN = 2,
  310. AIMODE_SAMPLE = 3,
  311. };
  312. enum ni_common_subdevices {
  313. NI_AI_SUBDEV,
  314. NI_AO_SUBDEV,
  315. NI_DIO_SUBDEV,
  316. NI_8255_DIO_SUBDEV,
  317. NI_UNUSED_SUBDEV,
  318. NI_CALIBRATION_SUBDEV,
  319. NI_EEPROM_SUBDEV,
  320. NI_PFI_DIO_SUBDEV,
  321. NI_CS5529_CALIBRATION_SUBDEV,
  322. NI_SERIAL_SUBDEV,
  323. NI_RTSI_SUBDEV,
  324. NI_GPCT0_SUBDEV,
  325. NI_GPCT1_SUBDEV,
  326. NI_FREQ_OUT_SUBDEV,
  327. NI_NUM_SUBDEVICES
  328. };
  329. static inline unsigned NI_GPCT_SUBDEV(unsigned counter_index)
  330. {
  331. switch (counter_index) {
  332. case 0:
  333. return NI_GPCT0_SUBDEV;
  334. break;
  335. case 1:
  336. return NI_GPCT1_SUBDEV;
  337. break;
  338. default:
  339. break;
  340. }
  341. BUG();
  342. return NI_GPCT0_SUBDEV;
  343. }
  344. enum timebase_nanoseconds {
  345. TIMEBASE_1_NS = 50,
  346. TIMEBASE_2_NS = 10000
  347. };
  348. #define SERIAL_DISABLED 0
  349. #define SERIAL_600NS 600
  350. #define SERIAL_1_2US 1200
  351. #define SERIAL_10US 10000
  352. static const int num_adc_stages_611x = 3;
  353. static void handle_a_interrupt(struct comedi_device *dev, unsigned short status,
  354. unsigned ai_mite_status);
  355. static void handle_b_interrupt(struct comedi_device *dev, unsigned short status,
  356. unsigned ao_mite_status);
  357. static void get_last_sample_611x(struct comedi_device *dev);
  358. static void get_last_sample_6143(struct comedi_device *dev);
  359. static inline void ni_set_bitfield(struct comedi_device *dev, int reg,
  360. unsigned bit_mask, unsigned bit_values)
  361. {
  362. unsigned long flags;
  363. spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
  364. switch (reg) {
  365. case Interrupt_A_Enable_Register:
  366. devpriv->int_a_enable_reg &= ~bit_mask;
  367. devpriv->int_a_enable_reg |= bit_values & bit_mask;
  368. devpriv->stc_writew(dev, devpriv->int_a_enable_reg,
  369. Interrupt_A_Enable_Register);
  370. break;
  371. case Interrupt_B_Enable_Register:
  372. devpriv->int_b_enable_reg &= ~bit_mask;
  373. devpriv->int_b_enable_reg |= bit_values & bit_mask;
  374. devpriv->stc_writew(dev, devpriv->int_b_enable_reg,
  375. Interrupt_B_Enable_Register);
  376. break;
  377. case IO_Bidirection_Pin_Register:
  378. devpriv->io_bidirection_pin_reg &= ~bit_mask;
  379. devpriv->io_bidirection_pin_reg |= bit_values & bit_mask;
  380. devpriv->stc_writew(dev, devpriv->io_bidirection_pin_reg,
  381. IO_Bidirection_Pin_Register);
  382. break;
  383. case AI_AO_Select:
  384. devpriv->ai_ao_select_reg &= ~bit_mask;
  385. devpriv->ai_ao_select_reg |= bit_values & bit_mask;
  386. ni_writeb(devpriv->ai_ao_select_reg, AI_AO_Select);
  387. break;
  388. case G0_G1_Select:
  389. devpriv->g0_g1_select_reg &= ~bit_mask;
  390. devpriv->g0_g1_select_reg |= bit_values & bit_mask;
  391. ni_writeb(devpriv->g0_g1_select_reg, G0_G1_Select);
  392. break;
  393. default:
  394. printk("Warning %s() called with invalid register\n", __func__);
  395. printk("reg is %d\n", reg);
  396. break;
  397. }
  398. mmiowb();
  399. spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
  400. }
  401. #ifdef PCIDMA
  402. static int ni_ai_drain_dma(struct comedi_device *dev);
  403. /* DMA channel setup */
  404. /* negative channel means no channel */
  405. static inline void ni_set_ai_dma_channel(struct comedi_device *dev, int channel)
  406. {
  407. unsigned bitfield;
  408. if (channel >= 0) {
  409. bitfield =
  410. (ni_stc_dma_channel_select_bitfield(channel) <<
  411. AI_DMA_Select_Shift) & AI_DMA_Select_Mask;
  412. } else {
  413. bitfield = 0;
  414. }
  415. ni_set_bitfield(dev, AI_AO_Select, AI_DMA_Select_Mask, bitfield);
  416. }
  417. /* negative channel means no channel */
  418. static inline void ni_set_ao_dma_channel(struct comedi_device *dev, int channel)
  419. {
  420. unsigned bitfield;
  421. if (channel >= 0) {
  422. bitfield =
  423. (ni_stc_dma_channel_select_bitfield(channel) <<
  424. AO_DMA_Select_Shift) & AO_DMA_Select_Mask;
  425. } else {
  426. bitfield = 0;
  427. }
  428. ni_set_bitfield(dev, AI_AO_Select, AO_DMA_Select_Mask, bitfield);
  429. }
  430. /* negative mite_channel means no channel */
  431. static inline void ni_set_gpct_dma_channel(struct comedi_device *dev,
  432. unsigned gpct_index,
  433. int mite_channel)
  434. {
  435. unsigned bitfield;
  436. if (mite_channel >= 0) {
  437. bitfield = GPCT_DMA_Select_Bits(gpct_index, mite_channel);
  438. } else {
  439. bitfield = 0;
  440. }
  441. ni_set_bitfield(dev, G0_G1_Select, GPCT_DMA_Select_Mask(gpct_index),
  442. bitfield);
  443. }
  444. /* negative mite_channel means no channel */
  445. static inline void ni_set_cdo_dma_channel(struct comedi_device *dev,
  446. int mite_channel)
  447. {
  448. unsigned long flags;
  449. spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
  450. devpriv->cdio_dma_select_reg &= ~CDO_DMA_Select_Mask;
  451. if (mite_channel >= 0) {
  452. /*XXX just guessing ni_stc_dma_channel_select_bitfield() returns the right bits,
  453. under the assumption the cdio dma selection works just like ai/ao/gpct.
  454. Definitely works for dma channels 0 and 1. */
  455. devpriv->cdio_dma_select_reg |=
  456. (ni_stc_dma_channel_select_bitfield(mite_channel) <<
  457. CDO_DMA_Select_Shift) & CDO_DMA_Select_Mask;
  458. }
  459. ni_writeb(devpriv->cdio_dma_select_reg, M_Offset_CDIO_DMA_Select);
  460. mmiowb();
  461. spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
  462. }
  463. static int ni_request_ai_mite_channel(struct comedi_device *dev)
  464. {
  465. unsigned long flags;
  466. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  467. BUG_ON(devpriv->ai_mite_chan);
  468. devpriv->ai_mite_chan =
  469. mite_request_channel(devpriv->mite, devpriv->ai_mite_ring);
  470. if (devpriv->ai_mite_chan == NULL) {
  471. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  472. comedi_error(dev,
  473. "failed to reserve mite dma channel for analog input.");
  474. return -EBUSY;
  475. }
  476. devpriv->ai_mite_chan->dir = COMEDI_INPUT;
  477. ni_set_ai_dma_channel(dev, devpriv->ai_mite_chan->channel);
  478. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  479. return 0;
  480. }
  481. static int ni_request_ao_mite_channel(struct comedi_device *dev)
  482. {
  483. unsigned long flags;
  484. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  485. BUG_ON(devpriv->ao_mite_chan);
  486. devpriv->ao_mite_chan =
  487. mite_request_channel(devpriv->mite, devpriv->ao_mite_ring);
  488. if (devpriv->ao_mite_chan == NULL) {
  489. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  490. comedi_error(dev,
  491. "failed to reserve mite dma channel for analog outut.");
  492. return -EBUSY;
  493. }
  494. devpriv->ao_mite_chan->dir = COMEDI_OUTPUT;
  495. ni_set_ao_dma_channel(dev, devpriv->ao_mite_chan->channel);
  496. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  497. return 0;
  498. }
  499. static int ni_request_gpct_mite_channel(struct comedi_device *dev,
  500. unsigned gpct_index,
  501. enum comedi_io_direction direction)
  502. {
  503. unsigned long flags;
  504. struct mite_channel *mite_chan;
  505. BUG_ON(gpct_index >= NUM_GPCT);
  506. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  507. BUG_ON(devpriv->counter_dev->counters[gpct_index].mite_chan);
  508. mite_chan =
  509. mite_request_channel(devpriv->mite,
  510. devpriv->gpct_mite_ring[gpct_index]);
  511. if (mite_chan == NULL) {
  512. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  513. comedi_error(dev,
  514. "failed to reserve mite dma channel for counter.");
  515. return -EBUSY;
  516. }
  517. mite_chan->dir = direction;
  518. ni_tio_set_mite_channel(&devpriv->counter_dev->counters[gpct_index],
  519. mite_chan);
  520. ni_set_gpct_dma_channel(dev, gpct_index, mite_chan->channel);
  521. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  522. return 0;
  523. }
  524. #endif /* PCIDMA */
  525. static int ni_request_cdo_mite_channel(struct comedi_device *dev)
  526. {
  527. #ifdef PCIDMA
  528. unsigned long flags;
  529. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  530. BUG_ON(devpriv->cdo_mite_chan);
  531. devpriv->cdo_mite_chan =
  532. mite_request_channel(devpriv->mite, devpriv->cdo_mite_ring);
  533. if (devpriv->cdo_mite_chan == NULL) {
  534. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  535. comedi_error(dev,
  536. "failed to reserve mite dma channel for correlated digital outut.");
  537. return -EBUSY;
  538. }
  539. devpriv->cdo_mite_chan->dir = COMEDI_OUTPUT;
  540. ni_set_cdo_dma_channel(dev, devpriv->cdo_mite_chan->channel);
  541. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  542. #endif /* PCIDMA */
  543. return 0;
  544. }
  545. static void ni_release_ai_mite_channel(struct comedi_device *dev)
  546. {
  547. #ifdef PCIDMA
  548. unsigned long flags;
  549. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  550. if (devpriv->ai_mite_chan) {
  551. ni_set_ai_dma_channel(dev, -1);
  552. mite_release_channel(devpriv->ai_mite_chan);
  553. devpriv->ai_mite_chan = NULL;
  554. }
  555. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  556. #endif /* PCIDMA */
  557. }
  558. static void ni_release_ao_mite_channel(struct comedi_device *dev)
  559. {
  560. #ifdef PCIDMA
  561. unsigned long flags;
  562. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  563. if (devpriv->ao_mite_chan) {
  564. ni_set_ao_dma_channel(dev, -1);
  565. mite_release_channel(devpriv->ao_mite_chan);
  566. devpriv->ao_mite_chan = NULL;
  567. }
  568. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  569. #endif /* PCIDMA */
  570. }
  571. void ni_release_gpct_mite_channel(struct comedi_device *dev,
  572. unsigned gpct_index)
  573. {
  574. #ifdef PCIDMA
  575. unsigned long flags;
  576. BUG_ON(gpct_index >= NUM_GPCT);
  577. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  578. if (devpriv->counter_dev->counters[gpct_index].mite_chan) {
  579. struct mite_channel *mite_chan =
  580. devpriv->counter_dev->counters[gpct_index].mite_chan;
  581. ni_set_gpct_dma_channel(dev, gpct_index, -1);
  582. ni_tio_set_mite_channel(&devpriv->
  583. counter_dev->counters[gpct_index],
  584. NULL);
  585. mite_release_channel(mite_chan);
  586. }
  587. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  588. #endif /* PCIDMA */
  589. }
  590. static void ni_release_cdo_mite_channel(struct comedi_device *dev)
  591. {
  592. #ifdef PCIDMA
  593. unsigned long flags;
  594. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  595. if (devpriv->cdo_mite_chan) {
  596. ni_set_cdo_dma_channel(dev, -1);
  597. mite_release_channel(devpriv->cdo_mite_chan);
  598. devpriv->cdo_mite_chan = NULL;
  599. }
  600. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  601. #endif /* PCIDMA */
  602. }
  603. /* e-series boards use the second irq signals to generate dma requests for their counters */
  604. #ifdef PCIDMA
  605. static void ni_e_series_enable_second_irq(struct comedi_device *dev,
  606. unsigned gpct_index, short enable)
  607. {
  608. if (boardtype.reg_type & ni_reg_m_series_mask)
  609. return;
  610. switch (gpct_index) {
  611. case 0:
  612. if (enable) {
  613. devpriv->stc_writew(dev, G0_Gate_Second_Irq_Enable,
  614. Second_IRQ_A_Enable_Register);
  615. } else {
  616. devpriv->stc_writew(dev, 0,
  617. Second_IRQ_A_Enable_Register);
  618. }
  619. break;
  620. case 1:
  621. if (enable) {
  622. devpriv->stc_writew(dev, G1_Gate_Second_Irq_Enable,
  623. Second_IRQ_B_Enable_Register);
  624. } else {
  625. devpriv->stc_writew(dev, 0,
  626. Second_IRQ_B_Enable_Register);
  627. }
  628. break;
  629. default:
  630. BUG();
  631. break;
  632. }
  633. }
  634. #endif /* PCIDMA */
  635. static void ni_clear_ai_fifo(struct comedi_device *dev)
  636. {
  637. if (boardtype.reg_type == ni_reg_6143) {
  638. /* Flush the 6143 data FIFO */
  639. ni_writel(0x10, AIFIFO_Control_6143); /* Flush fifo */
  640. ni_writel(0x00, AIFIFO_Control_6143); /* Flush fifo */
  641. while (ni_readl(AIFIFO_Status_6143) & 0x10) ; /* Wait for complete */
  642. } else {
  643. devpriv->stc_writew(dev, 1, ADC_FIFO_Clear);
  644. if (boardtype.reg_type == ni_reg_625x) {
  645. ni_writeb(0, M_Offset_Static_AI_Control(0));
  646. ni_writeb(1, M_Offset_Static_AI_Control(0));
  647. #if 0
  648. /* the NI example code does 3 convert pulses for 625x boards,
  649. but that appears to be wrong in practice. */
  650. devpriv->stc_writew(dev, AI_CONVERT_Pulse,
  651. AI_Command_1_Register);
  652. devpriv->stc_writew(dev, AI_CONVERT_Pulse,
  653. AI_Command_1_Register);
  654. devpriv->stc_writew(dev, AI_CONVERT_Pulse,
  655. AI_Command_1_Register);
  656. #endif
  657. }
  658. }
  659. }
  660. static void win_out2(struct comedi_device *dev, uint32_t data, int reg)
  661. {
  662. devpriv->stc_writew(dev, data >> 16, reg);
  663. devpriv->stc_writew(dev, data & 0xffff, reg + 1);
  664. }
  665. static uint32_t win_in2(struct comedi_device *dev, int reg)
  666. {
  667. uint32_t bits;
  668. bits = devpriv->stc_readw(dev, reg) << 16;
  669. bits |= devpriv->stc_readw(dev, reg + 1);
  670. return bits;
  671. }
  672. #define ao_win_out(data, addr) ni_ao_win_outw(dev, data, addr)
  673. static inline void ni_ao_win_outw(struct comedi_device *dev, uint16_t data,
  674. int addr)
  675. {
  676. unsigned long flags;
  677. spin_lock_irqsave(&devpriv->window_lock, flags);
  678. ni_writew(addr, AO_Window_Address_611x);
  679. ni_writew(data, AO_Window_Data_611x);
  680. spin_unlock_irqrestore(&devpriv->window_lock, flags);
  681. }
  682. static inline void ni_ao_win_outl(struct comedi_device *dev, uint32_t data,
  683. int addr)
  684. {
  685. unsigned long flags;
  686. spin_lock_irqsave(&devpriv->window_lock, flags);
  687. ni_writew(addr, AO_Window_Address_611x);
  688. ni_writel(data, AO_Window_Data_611x);
  689. spin_unlock_irqrestore(&devpriv->window_lock, flags);
  690. }
  691. static inline unsigned short ni_ao_win_inw(struct comedi_device *dev, int addr)
  692. {
  693. unsigned long flags;
  694. unsigned short data;
  695. spin_lock_irqsave(&devpriv->window_lock, flags);
  696. ni_writew(addr, AO_Window_Address_611x);
  697. data = ni_readw(AO_Window_Data_611x);
  698. spin_unlock_irqrestore(&devpriv->window_lock, flags);
  699. return data;
  700. }
  701. /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
  702. * share registers (such as Interrupt_A_Register) without interfering with
  703. * each other.
  704. *
  705. * NOTE: the switch/case statements are optimized out for a constant argument
  706. * so this is actually quite fast--- If you must wrap another function around this
  707. * make it inline to avoid a large speed penalty.
  708. *
  709. * value should only be 1 or 0.
  710. */
  711. static inline void ni_set_bits(struct comedi_device *dev, int reg,
  712. unsigned bits, unsigned value)
  713. {
  714. unsigned bit_values;
  715. if (value)
  716. bit_values = bits;
  717. else
  718. bit_values = 0;
  719. ni_set_bitfield(dev, reg, bits, bit_values);
  720. }
  721. static irqreturn_t ni_E_interrupt(int irq, void *d)
  722. {
  723. struct comedi_device *dev = d;
  724. unsigned short a_status;
  725. unsigned short b_status;
  726. unsigned int ai_mite_status = 0;
  727. unsigned int ao_mite_status = 0;
  728. unsigned long flags;
  729. #ifdef PCIDMA
  730. struct mite_struct *mite = devpriv->mite;
  731. #endif
  732. if (dev->attached == 0)
  733. return IRQ_NONE;
  734. smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
  735. /* lock to avoid race with comedi_poll */
  736. spin_lock_irqsave(&dev->spinlock, flags);
  737. a_status = devpriv->stc_readw(dev, AI_Status_1_Register);
  738. b_status = devpriv->stc_readw(dev, AO_Status_1_Register);
  739. #ifdef PCIDMA
  740. if (mite) {
  741. unsigned long flags_too;
  742. spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
  743. if (devpriv->ai_mite_chan) {
  744. ai_mite_status = mite_get_status(devpriv->ai_mite_chan);
  745. if (ai_mite_status & CHSR_LINKC)
  746. writel(CHOR_CLRLC,
  747. devpriv->mite->mite_io_addr +
  748. MITE_CHOR(devpriv->
  749. ai_mite_chan->channel));
  750. }
  751. if (devpriv->ao_mite_chan) {
  752. ao_mite_status = mite_get_status(devpriv->ao_mite_chan);
  753. if (ao_mite_status & CHSR_LINKC)
  754. writel(CHOR_CLRLC,
  755. mite->mite_io_addr +
  756. MITE_CHOR(devpriv->
  757. ao_mite_chan->channel));
  758. }
  759. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
  760. }
  761. #endif
  762. ack_a_interrupt(dev, a_status);
  763. ack_b_interrupt(dev, b_status);
  764. if ((a_status & Interrupt_A_St) || (ai_mite_status & CHSR_INT))
  765. handle_a_interrupt(dev, a_status, ai_mite_status);
  766. if ((b_status & Interrupt_B_St) || (ao_mite_status & CHSR_INT))
  767. handle_b_interrupt(dev, b_status, ao_mite_status);
  768. handle_gpct_interrupt(dev, 0);
  769. handle_gpct_interrupt(dev, 1);
  770. handle_cdio_interrupt(dev);
  771. spin_unlock_irqrestore(&dev->spinlock, flags);
  772. return IRQ_HANDLED;
  773. }
  774. #ifdef PCIDMA
  775. static void ni_sync_ai_dma(struct comedi_device *dev)
  776. {
  777. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  778. unsigned long flags;
  779. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  780. if (devpriv->ai_mite_chan)
  781. mite_sync_input_dma(devpriv->ai_mite_chan, s->async);
  782. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  783. }
  784. static void mite_handle_b_linkc(struct mite_struct *mite,
  785. struct comedi_device *dev)
  786. {
  787. struct comedi_subdevice *s = dev->subdevices + NI_AO_SUBDEV;
  788. unsigned long flags;
  789. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  790. if (devpriv->ao_mite_chan) {
  791. mite_sync_output_dma(devpriv->ao_mite_chan, s->async);
  792. }
  793. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  794. }
  795. static int ni_ao_wait_for_dma_load(struct comedi_device *dev)
  796. {
  797. static const int timeout = 10000;
  798. int i;
  799. for (i = 0; i < timeout; i++) {
  800. unsigned short b_status;
  801. b_status = devpriv->stc_readw(dev, AO_Status_1_Register);
  802. if (b_status & AO_FIFO_Half_Full_St)
  803. break;
  804. /* if we poll too often, the pci bus activity seems
  805. to slow the dma transfer down */
  806. udelay(10);
  807. }
  808. if (i == timeout) {
  809. comedi_error(dev, "timed out waiting for dma load");
  810. return -EPIPE;
  811. }
  812. return 0;
  813. }
  814. #endif /* PCIDMA */
  815. static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
  816. {
  817. if (devpriv->aimode == AIMODE_SCAN) {
  818. #ifdef PCIDMA
  819. static const int timeout = 10;
  820. int i;
  821. for (i = 0; i < timeout; i++) {
  822. ni_sync_ai_dma(dev);
  823. if ((s->async->events & COMEDI_CB_EOS))
  824. break;
  825. udelay(1);
  826. }
  827. #else
  828. ni_handle_fifo_dregs(dev);
  829. s->async->events |= COMEDI_CB_EOS;
  830. #endif
  831. }
  832. /* handle special case of single scan using AI_End_On_End_Of_Scan */
  833. if ((devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)) {
  834. shutdown_ai_command(dev);
  835. }
  836. }
  837. static void shutdown_ai_command(struct comedi_device *dev)
  838. {
  839. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  840. #ifdef PCIDMA
  841. ni_ai_drain_dma(dev);
  842. #endif
  843. ni_handle_fifo_dregs(dev);
  844. get_last_sample_611x(dev);
  845. get_last_sample_6143(dev);
  846. s->async->events |= COMEDI_CB_EOA;
  847. }
  848. static void ni_event(struct comedi_device *dev, struct comedi_subdevice *s)
  849. {
  850. if (s->
  851. async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW |
  852. COMEDI_CB_EOA)) {
  853. switch (s - dev->subdevices) {
  854. case NI_AI_SUBDEV:
  855. ni_ai_reset(dev, s);
  856. break;
  857. case NI_AO_SUBDEV:
  858. ni_ao_reset(dev, s);
  859. break;
  860. case NI_GPCT0_SUBDEV:
  861. case NI_GPCT1_SUBDEV:
  862. ni_gpct_cancel(dev, s);
  863. break;
  864. case NI_DIO_SUBDEV:
  865. ni_cdio_cancel(dev, s);
  866. break;
  867. default:
  868. break;
  869. }
  870. }
  871. comedi_event(dev, s);
  872. }
  873. static void handle_gpct_interrupt(struct comedi_device *dev,
  874. unsigned short counter_index)
  875. {
  876. #ifdef PCIDMA
  877. struct comedi_subdevice *s =
  878. dev->subdevices + NI_GPCT_SUBDEV(counter_index);
  879. ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
  880. s);
  881. if (s->async->events)
  882. ni_event(dev, s);
  883. #endif
  884. }
  885. static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
  886. {
  887. unsigned short ack = 0;
  888. if (a_status & AI_SC_TC_St) {
  889. ack |= AI_SC_TC_Interrupt_Ack;
  890. }
  891. if (a_status & AI_START1_St) {
  892. ack |= AI_START1_Interrupt_Ack;
  893. }
  894. if (a_status & AI_START_St) {
  895. ack |= AI_START_Interrupt_Ack;
  896. }
  897. if (a_status & AI_STOP_St) {
  898. /* not sure why we used to ack the START here also, instead of doing it independently. Frank Hess 2007-07-06 */
  899. ack |= AI_STOP_Interrupt_Ack /*| AI_START_Interrupt_Ack */ ;
  900. }
  901. if (ack)
  902. devpriv->stc_writew(dev, ack, Interrupt_A_Ack_Register);
  903. }
  904. static void handle_a_interrupt(struct comedi_device *dev, unsigned short status,
  905. unsigned ai_mite_status)
  906. {
  907. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  908. /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
  909. if (s->type == COMEDI_SUBD_UNUSED)
  910. return;
  911. #ifdef DEBUG_INTERRUPT
  912. printk
  913. ("ni_mio_common: interrupt: a_status=%04x ai_mite_status=%08x\n",
  914. status, ai_mite_status);
  915. ni_mio_print_status_a(status);
  916. #endif
  917. #ifdef PCIDMA
  918. if (ai_mite_status & CHSR_LINKC) {
  919. ni_sync_ai_dma(dev);
  920. }
  921. if (ai_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
  922. CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
  923. CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
  924. printk
  925. ("unknown mite interrupt, ack! (ai_mite_status=%08x)\n",
  926. ai_mite_status);
  927. /* mite_print_chsr(ai_mite_status); */
  928. s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
  929. /* disable_irq(dev->irq); */
  930. }
  931. #endif
  932. /* test for all uncommon interrupt events at the same time */
  933. if (status & (AI_Overrun_St | AI_Overflow_St | AI_SC_TC_Error_St |
  934. AI_SC_TC_St | AI_START1_St)) {
  935. if (status == 0xffff) {
  936. printk
  937. ("ni_mio_common: a_status=0xffff. Card removed?\n");
  938. /* we probably aren't even running a command now,
  939. * so it's a good idea to be careful. */
  940. if (comedi_get_subdevice_runflags(s) & SRF_RUNNING) {
  941. s->async->events |=
  942. COMEDI_CB_ERROR | COMEDI_CB_EOA;
  943. ni_event(dev, s);
  944. }
  945. return;
  946. }
  947. if (status & (AI_Overrun_St | AI_Overflow_St |
  948. AI_SC_TC_Error_St)) {
  949. printk("ni_mio_common: ai error a_status=%04x\n",
  950. status);
  951. ni_mio_print_status_a(status);
  952. shutdown_ai_command(dev);
  953. s->async->events |= COMEDI_CB_ERROR;
  954. if (status & (AI_Overrun_St | AI_Overflow_St))
  955. s->async->events |= COMEDI_CB_OVERFLOW;
  956. ni_event(dev, s);
  957. return;
  958. }
  959. if (status & AI_SC_TC_St) {
  960. #ifdef DEBUG_INTERRUPT
  961. printk("ni_mio_common: SC_TC interrupt\n");
  962. #endif
  963. if (!devpriv->ai_continuous) {
  964. shutdown_ai_command(dev);
  965. }
  966. }
  967. }
  968. #ifndef PCIDMA
  969. if (status & AI_FIFO_Half_Full_St) {
  970. int i;
  971. static const int timeout = 10;
  972. /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
  973. *fail to get the fifo less than half full, so loop to be sure.*/
  974. for (i = 0; i < timeout; ++i) {
  975. ni_handle_fifo_half_full(dev);
  976. if ((devpriv->stc_readw(dev,
  977. AI_Status_1_Register) &
  978. AI_FIFO_Half_Full_St) == 0)
  979. break;
  980. }
  981. }
  982. #endif /* !PCIDMA */
  983. if ((status & AI_STOP_St)) {
  984. ni_handle_eos(dev, s);
  985. }
  986. ni_event(dev, s);
  987. #ifdef DEBUG_INTERRUPT
  988. status = devpriv->stc_readw(dev, AI_Status_1_Register);
  989. if (status & Interrupt_A_St) {
  990. printk
  991. ("handle_a_interrupt: didn't clear interrupt? status=0x%x\n",
  992. status);
  993. }
  994. #endif
  995. }
  996. static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
  997. {
  998. unsigned short ack = 0;
  999. if (b_status & AO_BC_TC_St) {
  1000. ack |= AO_BC_TC_Interrupt_Ack;
  1001. }
  1002. if (b_status & AO_Overrun_St) {
  1003. ack |= AO_Error_Interrupt_Ack;
  1004. }
  1005. if (b_status & AO_START_St) {
  1006. ack |= AO_START_Interrupt_Ack;
  1007. }
  1008. if (b_status & AO_START1_St) {
  1009. ack |= AO_START1_Interrupt_Ack;
  1010. }
  1011. if (b_status & AO_UC_TC_St) {
  1012. ack |= AO_UC_TC_Interrupt_Ack;
  1013. }
  1014. if (b_status & AO_UI2_TC_St) {
  1015. ack |= AO_UI2_TC_Interrupt_Ack;
  1016. }
  1017. if (b_status & AO_UPDATE_St) {
  1018. ack |= AO_UPDATE_Interrupt_Ack;
  1019. }
  1020. if (ack)
  1021. devpriv->stc_writew(dev, ack, Interrupt_B_Ack_Register);
  1022. }
  1023. static void handle_b_interrupt(struct comedi_device *dev,
  1024. unsigned short b_status, unsigned ao_mite_status)
  1025. {
  1026. struct comedi_subdevice *s = dev->subdevices + NI_AO_SUBDEV;
  1027. /* unsigned short ack=0; */
  1028. #ifdef DEBUG_INTERRUPT
  1029. printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
  1030. b_status, ao_mite_status);
  1031. ni_mio_print_status_b(b_status);
  1032. #endif
  1033. #ifdef PCIDMA
  1034. /* Currently, mite.c requires us to handle LINKC */
  1035. if (ao_mite_status & CHSR_LINKC) {
  1036. mite_handle_b_linkc(devpriv->mite, dev);
  1037. }
  1038. if (ao_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
  1039. CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
  1040. CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
  1041. printk
  1042. ("unknown mite interrupt, ack! (ao_mite_status=%08x)\n",
  1043. ao_mite_status);
  1044. /* mite_print_chsr(ao_mite_status); */
  1045. s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
  1046. }
  1047. #endif
  1048. if (b_status == 0xffff)
  1049. return;
  1050. if (b_status & AO_Overrun_St) {
  1051. printk
  1052. ("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",
  1053. b_status, devpriv->stc_readw(dev, AO_Status_2_Register));
  1054. s->async->events |= COMEDI_CB_OVERFLOW;
  1055. }
  1056. if (b_status & AO_BC_TC_St) {
  1057. MDPRINTK
  1058. ("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",
  1059. b_status, devpriv->stc_readw(dev, AO_Status_2_Register));
  1060. s->async->events |= COMEDI_CB_EOA;
  1061. }
  1062. #ifndef PCIDMA
  1063. if (b_status & AO_FIFO_Request_St) {
  1064. int ret;
  1065. ret = ni_ao_fifo_half_empty(dev, s);
  1066. if (!ret) {
  1067. printk("ni_mio_common: AO buffer underrun\n");
  1068. ni_set_bits(dev, Interrupt_B_Enable_Register,
  1069. AO_FIFO_Interrupt_Enable |
  1070. AO_Error_Interrupt_Enable, 0);
  1071. s->async->events |= COMEDI_CB_OVERFLOW;
  1072. }
  1073. }
  1074. #endif
  1075. ni_event(dev, s);
  1076. }
  1077. #ifdef DEBUG_STATUS_A
  1078. static const char *const status_a_strings[] = {
  1079. "passthru0", "fifo", "G0_gate", "G0_TC",
  1080. "stop", "start", "sc_tc", "start1",
  1081. "start2", "sc_tc_error", "overflow", "overrun",
  1082. "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_a"
  1083. };
  1084. static void ni_mio_print_status_a(int status)
  1085. {
  1086. int i;
  1087. printk("A status:");
  1088. for (i = 15; i >= 0; i--) {
  1089. if (status & (1 << i)) {
  1090. printk(" %s", status_a_strings[i]);
  1091. }
  1092. }
  1093. printk("\n");
  1094. }
  1095. #endif
  1096. #ifdef DEBUG_STATUS_B
  1097. static const char *const status_b_strings[] = {
  1098. "passthru1", "fifo", "G1_gate", "G1_TC",
  1099. "UI2_TC", "UPDATE", "UC_TC", "BC_TC",
  1100. "start1", "overrun", "start", "bc_tc_error",
  1101. "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_b"
  1102. };
  1103. static void ni_mio_print_status_b(int status)
  1104. {
  1105. int i;
  1106. printk("B status:");
  1107. for (i = 15; i >= 0; i--) {
  1108. if (status & (1 << i)) {
  1109. printk(" %s", status_b_strings[i]);
  1110. }
  1111. }
  1112. printk("\n");
  1113. }
  1114. #endif
  1115. #ifndef PCIDMA
  1116. static void ni_ao_fifo_load(struct comedi_device *dev,
  1117. struct comedi_subdevice *s, int n)
  1118. {
  1119. struct comedi_async *async = s->async;
  1120. struct comedi_cmd *cmd = &async->cmd;
  1121. int chan;
  1122. int i;
  1123. short d;
  1124. u32 packed_data;
  1125. int range;
  1126. int err = 1;
  1127. chan = async->cur_chan;
  1128. for (i = 0; i < n; i++) {
  1129. err &= comedi_buf_get(async, &d);
  1130. if (err == 0)
  1131. break;
  1132. range = CR_RANGE(cmd->chanlist[chan]);
  1133. if (boardtype.reg_type & ni_reg_6xxx_mask) {
  1134. packed_data = d & 0xffff;
  1135. /* 6711 only has 16 bit wide ao fifo */
  1136. if (boardtype.reg_type != ni_reg_6711) {
  1137. err &= comedi_buf_get(async, &d);
  1138. if (err == 0)
  1139. break;
  1140. chan++;
  1141. i++;
  1142. packed_data |= (d << 16) & 0xffff0000;
  1143. }
  1144. ni_writel(packed_data, DAC_FIFO_Data_611x);
  1145. } else {
  1146. ni_writew(d, DAC_FIFO_Data);
  1147. }
  1148. chan++;
  1149. chan %= cmd->chanlist_len;
  1150. }
  1151. async->cur_chan = chan;
  1152. if (err == 0) {
  1153. async->events |= COMEDI_CB_OVERFLOW;
  1154. }
  1155. }
  1156. /*
  1157. * There's a small problem if the FIFO gets really low and we
  1158. * don't have the data to fill it. Basically, if after we fill
  1159. * the FIFO with all the data available, the FIFO is _still_
  1160. * less than half full, we never clear the interrupt. If the
  1161. * IRQ is in edge mode, we never get another interrupt, because
  1162. * this one wasn't cleared. If in level mode, we get flooded
  1163. * with interrupts that we can't fulfill, because nothing ever
  1164. * gets put into the buffer.
  1165. *
  1166. * This kind of situation is recoverable, but it is easier to
  1167. * just pretend we had a FIFO underrun, since there is a good
  1168. * chance it will happen anyway. This is _not_ the case for
  1169. * RT code, as RT code might purposely be running close to the
  1170. * metal. Needs to be fixed eventually.
  1171. */
  1172. static int ni_ao_fifo_half_empty(struct comedi_device *dev,
  1173. struct comedi_subdevice *s)
  1174. {
  1175. int n;
  1176. n = comedi_buf_read_n_available(s->async);
  1177. if (n == 0) {
  1178. s->async->events |= COMEDI_CB_OVERFLOW;
  1179. return 0;
  1180. }
  1181. n /= sizeof(short);
  1182. if (n > boardtype.ao_fifo_depth / 2)
  1183. n = boardtype.ao_fifo_depth / 2;
  1184. ni_ao_fifo_load(dev, s, n);
  1185. s->async->events |= COMEDI_CB_BLOCK;
  1186. return 1;
  1187. }
  1188. static int ni_ao_prep_fifo(struct comedi_device *dev,
  1189. struct comedi_subdevice *s)
  1190. {
  1191. int n;
  1192. /* reset fifo */
  1193. devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
  1194. if (boardtype.reg_type & ni_reg_6xxx_mask)
  1195. ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
  1196. /* load some data */
  1197. n = comedi_buf_read_n_available(s->async);
  1198. if (n == 0)
  1199. return 0;
  1200. n /= sizeof(short);
  1201. if (n > boardtype.ao_fifo_depth)
  1202. n = boardtype.ao_fifo_depth;
  1203. ni_ao_fifo_load(dev, s, n);
  1204. return n;
  1205. }
  1206. static void ni_ai_fifo_read(struct comedi_device *dev,
  1207. struct comedi_subdevice *s, int n)
  1208. {
  1209. struct comedi_async *async = s->async;
  1210. int i;
  1211. if (boardtype.reg_type == ni_reg_611x) {
  1212. short data[2];
  1213. u32 dl;
  1214. for (i = 0; i < n / 2; i++) {
  1215. dl = ni_readl(ADC_FIFO_Data_611x);
  1216. /* This may get the hi/lo data in the wrong order */
  1217. data[0] = (dl >> 16) & 0xffff;
  1218. data[1] = dl & 0xffff;
  1219. cfc_write_array_to_buffer(s, data, sizeof(data));
  1220. }
  1221. /* Check if there's a single sample stuck in the FIFO */
  1222. if (n % 2) {
  1223. dl = ni_readl(ADC_FIFO_Data_611x);
  1224. data[0] = dl & 0xffff;
  1225. cfc_write_to_buffer(s, data[0]);
  1226. }
  1227. } else if (boardtype.reg_type == ni_reg_6143) {
  1228. short data[2];
  1229. u32 dl;
  1230. /* This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
  1231. for (i = 0; i < n / 2; i++) {
  1232. dl = ni_readl(AIFIFO_Data_6143);
  1233. data[0] = (dl >> 16) & 0xffff;
  1234. data[1] = dl & 0xffff;
  1235. cfc_write_array_to_buffer(s, data, sizeof(data));
  1236. }
  1237. if (n % 2) {
  1238. /* Assume there is a single sample stuck in the FIFO */
  1239. ni_writel(0x01, AIFIFO_Control_6143); /* Get stranded sample into FIFO */
  1240. dl = ni_readl(AIFIFO_Data_6143);
  1241. data[0] = (dl >> 16) & 0xffff;
  1242. cfc_write_to_buffer(s, data[0]);
  1243. }
  1244. } else {
  1245. if (n > sizeof(devpriv->ai_fifo_buffer) /
  1246. sizeof(devpriv->ai_fifo_buffer[0])) {
  1247. comedi_error(dev, "bug! ai_fifo_buffer too small");
  1248. async->events |= COMEDI_CB_ERROR;
  1249. return;
  1250. }
  1251. for (i = 0; i < n; i++) {
  1252. devpriv->ai_fifo_buffer[i] =
  1253. ni_readw(ADC_FIFO_Data_Register);
  1254. }
  1255. cfc_write_array_to_buffer(s, devpriv->ai_fifo_buffer,
  1256. n *
  1257. sizeof(devpriv->ai_fifo_buffer[0]));
  1258. }
  1259. }
  1260. static void ni_handle_fifo_half_full(struct comedi_device *dev)
  1261. {
  1262. int n;
  1263. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  1264. n = boardtype.ai_fifo_depth / 2;
  1265. ni_ai_fifo_read(dev, s, n);
  1266. }
  1267. #endif
  1268. #ifdef PCIDMA
  1269. static int ni_ai_drain_dma(struct comedi_device *dev)
  1270. {
  1271. int i;
  1272. static const int timeout = 10000;
  1273. unsigned long flags;
  1274. int retval = 0;
  1275. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  1276. if (devpriv->ai_mite_chan) {
  1277. for (i = 0; i < timeout; i++) {
  1278. if ((devpriv->stc_readw(dev,
  1279. AI_Status_1_Register) &
  1280. AI_FIFO_Empty_St)
  1281. && mite_bytes_in_transit(devpriv->ai_mite_chan) ==
  1282. 0)
  1283. break;
  1284. udelay(5);
  1285. }
  1286. if (i == timeout) {
  1287. printk("ni_mio_common: wait for dma drain timed out\n");
  1288. printk
  1289. ("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
  1290. mite_bytes_in_transit(devpriv->ai_mite_chan),
  1291. devpriv->stc_readw(dev, AI_Status_1_Register));
  1292. retval = -1;
  1293. }
  1294. }
  1295. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  1296. ni_sync_ai_dma(dev);
  1297. return retval;
  1298. }
  1299. #endif
  1300. /*
  1301. Empties the AI fifo
  1302. */
  1303. static void ni_handle_fifo_dregs(struct comedi_device *dev)
  1304. {
  1305. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  1306. short data[2];
  1307. u32 dl;
  1308. short fifo_empty;
  1309. int i;
  1310. if (boardtype.reg_type == ni_reg_611x) {
  1311. while ((devpriv->stc_readw(dev,
  1312. AI_Status_1_Register) &
  1313. AI_FIFO_Empty_St) == 0) {
  1314. dl = ni_readl(ADC_FIFO_Data_611x);
  1315. /* This may get the hi/lo data in the wrong order */
  1316. data[0] = (dl >> 16);
  1317. data[1] = (dl & 0xffff);
  1318. cfc_write_array_to_buffer(s, data, sizeof(data));
  1319. }
  1320. } else if (boardtype.reg_type == ni_reg_6143) {
  1321. i = 0;
  1322. while (ni_readl(AIFIFO_Status_6143) & 0x04) {
  1323. dl = ni_readl(AIFIFO_Data_6143);
  1324. /* This may get the hi/lo data in the wrong order */
  1325. data[0] = (dl >> 16);
  1326. data[1] = (dl & 0xffff);
  1327. cfc_write_array_to_buffer(s, data, sizeof(data));
  1328. i += 2;
  1329. }
  1330. /* Check if stranded sample is present */
  1331. if (ni_readl(AIFIFO_Status_6143) & 0x01) {
  1332. ni_writel(0x01, AIFIFO_Control_6143); /* Get stranded sample into FIFO */
  1333. dl = ni_readl(AIFIFO_Data_6143);
  1334. data[0] = (dl >> 16) & 0xffff;
  1335. cfc_write_to_buffer(s, data[0]);
  1336. }
  1337. } else {
  1338. fifo_empty =
  1339. devpriv->stc_readw(dev,
  1340. AI_Status_1_Register) & AI_FIFO_Empty_St;
  1341. while (fifo_empty == 0) {
  1342. for (i = 0;
  1343. i <
  1344. sizeof(devpriv->ai_fifo_buffer) /
  1345. sizeof(devpriv->ai_fifo_buffer[0]); i++) {
  1346. fifo_empty =
  1347. devpriv->stc_readw(dev,
  1348. AI_Status_1_Register) &
  1349. AI_FIFO_Empty_St;
  1350. if (fifo_empty)
  1351. break;
  1352. devpriv->ai_fifo_buffer[i] =
  1353. ni_readw(ADC_FIFO_Data_Register);
  1354. }
  1355. cfc_write_array_to_buffer(s, devpriv->ai_fifo_buffer,
  1356. i *
  1357. sizeof(devpriv->
  1358. ai_fifo_buffer[0]));
  1359. }
  1360. }
  1361. }
  1362. static void get_last_sample_611x(struct comedi_device *dev)
  1363. {
  1364. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  1365. short data;
  1366. u32 dl;
  1367. if (boardtype.reg_type != ni_reg_611x)
  1368. return;
  1369. /* Check if there's a single sample stuck in the FIFO */
  1370. if (ni_readb(XXX_Status) & 0x80) {
  1371. dl = ni_readl(ADC_FIFO_Data_611x);
  1372. data = (dl & 0xffff);
  1373. cfc_write_to_buffer(s, data);
  1374. }
  1375. }
  1376. static void get_last_sample_6143(struct comedi_device *dev)
  1377. {
  1378. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  1379. short data;
  1380. u32 dl;
  1381. if (boardtype.reg_type != ni_reg_6143)
  1382. return;
  1383. /* Check if there's a single sample stuck in the FIFO */
  1384. if (ni_readl(AIFIFO_Status_6143) & 0x01) {
  1385. ni_writel(0x01, AIFIFO_Control_6143); /* Get stranded sample into FIFO */
  1386. dl = ni_readl(AIFIFO_Data_6143);
  1387. /* This may get the hi/lo data in the wrong order */
  1388. data = (dl >> 16) & 0xffff;
  1389. cfc_write_to_buffer(s, data);
  1390. }
  1391. }
  1392. static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
  1393. void *data, unsigned int num_bytes,
  1394. unsigned int chan_index)
  1395. {
  1396. struct comedi_async *async = s->async;
  1397. unsigned int i;
  1398. unsigned int length = num_bytes / bytes_per_sample(s);
  1399. short *array = data;
  1400. unsigned int *larray = data;
  1401. for (i = 0; i < length; i++) {
  1402. #ifdef PCIDMA
  1403. if (s->subdev_flags & SDF_LSAMPL)
  1404. larray[i] = le32_to_cpu(larray[i]);
  1405. else
  1406. array[i] = le16_to_cpu(array[i]);
  1407. #endif
  1408. if (s->subdev_flags & SDF_LSAMPL)
  1409. larray[i] += devpriv->ai_offset[chan_index];
  1410. else
  1411. array[i] += devpriv->ai_offset[chan_index];
  1412. chan_index++;
  1413. chan_index %= async->cmd.chanlist_len;
  1414. }
  1415. }
  1416. #ifdef PCIDMA
  1417. static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
  1418. {
  1419. struct comedi_subdevice *s = dev->subdevices + NI_AI_SUBDEV;
  1420. int retval;
  1421. unsigned long flags;
  1422. retval = ni_request_ai_mite_channel(dev);
  1423. if (retval)
  1424. return retval;
  1425. /* printk("comedi_debug: using mite channel %i for ai.\n", devpriv->ai_mite_chan->channel); */
  1426. /* write alloc the entire buffer */
  1427. comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
  1428. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  1429. if (devpriv->ai_mite_chan == NULL) {
  1430. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  1431. return -EIO;
  1432. }
  1433. switch (boardtype.reg_type) {
  1434. case ni_reg_611x:
  1435. case ni_reg_6143:
  1436. mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
  1437. break;
  1438. case ni_reg_628x:
  1439. mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
  1440. break;
  1441. default:
  1442. mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
  1443. break;
  1444. }
  1445. /*start the MITE */
  1446. mite_dma_arm(devpriv->ai_mite_chan);
  1447. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  1448. return 0;
  1449. }
  1450. static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
  1451. {
  1452. struct comedi_subdevice *s = dev->subdevices + NI_AO_SUBDEV;
  1453. int retval;
  1454. unsigned long flags;
  1455. retval = ni_request_ao_mite_channel(dev);
  1456. if (retval)
  1457. return retval;
  1458. /* read alloc the entire buffer */
  1459. comedi_buf_read_alloc(s->async, s->async->prealloc_bufsz);
  1460. spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
  1461. if (devpriv->ao_mite_chan) {
  1462. if (boardtype.reg_type & (ni_reg_611x | ni_reg_6713)) {
  1463. mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
  1464. } else {
  1465. /* doing 32 instead of 16 bit wide transfers from memory
  1466. makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
  1467. mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
  1468. }
  1469. mite_dma_arm(devpriv->ao_mite_chan);
  1470. } else
  1471. retval = -EIO;
  1472. spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
  1473. return retval;
  1474. }
  1475. #endif /* PCIDMA */
  1476. /*
  1477. used for both cancel ioctl and board initialization
  1478. this is pretty harsh for a cancel, but it works...
  1479. */
  1480. static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
  1481. {
  1482. ni_release_ai_mite_channel(dev);
  1483. /* ai configuration */
  1484. devpriv->stc_writew(dev, AI_Configuration_Start | AI_Reset,
  1485. Joint_Reset_Register);
  1486. ni_set_bits(dev, Interrupt_A_Enable_Register,
  1487. AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable |
  1488. AI_START2_Interrupt_Enable | AI_START_Interrupt_Enable |
  1489. AI_STOP_Interrupt_Enable | AI_Error_Interrupt_Enable |
  1490. AI_FIFO_Interrupt_Enable, 0);
  1491. ni_clear_ai_fifo(dev);
  1492. if (boardtype.reg_type != ni_reg_6143)
  1493. ni_writeb(0, Misc_Command);
  1494. devpriv->stc_writew(dev, AI_Disarm, AI_Command_1_Register); /* reset pulses */
  1495. devpriv->stc_writew(dev,
  1496. AI_Start_Stop | AI_Mode_1_Reserved
  1497. /*| AI_Trigger_Once */ ,
  1498. AI_Mode_1_Register);
  1499. devpriv->stc_writew(dev, 0x0000, AI_Mode_2_Register);
  1500. /* generate FIFO interrupts on non-empty */
  1501. devpriv->stc_writew(dev, (0 << 6) | 0x0000, AI_Mode_3_Register);
  1502. if (boardtype.reg_type == ni_reg_611x) {
  1503. devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
  1504. AI_SOC_Polarity |
  1505. AI_LOCALMUX_CLK_Pulse_Width,
  1506. AI_Personal_Register);
  1507. devpriv->stc_writew(dev,
  1508. AI_SCAN_IN_PROG_Output_Select(3) |
  1509. AI_EXTMUX_CLK_Output_Select(0) |
  1510. AI_LOCALMUX_CLK_Output_Select(2) |
  1511. AI_SC_TC_Output_Select(3) |
  1512. AI_CONVERT_Output_Select
  1513. (AI_CONVERT_Output_Enable_High),
  1514. AI_Output_Control_Register);
  1515. } else if (boardtype.reg_type == ni_reg_6143) {
  1516. devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
  1517. AI_SOC_Polarity |
  1518. AI_LOCALMUX_CLK_Pulse_Width,
  1519. AI_Personal_Register);
  1520. devpriv->stc_writew(dev,
  1521. AI_SCAN_IN_PROG_Output_Select(3) |
  1522. AI_EXTMUX_CLK_Output_Select(0) |
  1523. AI_LOCALMUX_CLK_Output_Select(2) |
  1524. AI_SC_TC_Output_Select(3) |
  1525. AI_CONVERT_Output_Select
  1526. (AI_CONVERT_Output_Enable_Low),
  1527. AI_Output_Control_Register);
  1528. } else {
  1529. unsigned ai_output_control_bits;
  1530. devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
  1531. AI_SOC_Polarity |
  1532. AI_CONVERT_Pulse_Width |
  1533. AI_LOCALMUX_CLK_Pulse_Width,
  1534. AI_Personal_Register);
  1535. ai_output_control_bits =
  1536. AI_SCAN_IN_PROG_Output_Select(3) |
  1537. AI_EXTMUX_CLK_Output_Select(0) |
  1538. AI_LOCALMUX_CLK_Output_Select(2) |
  1539. AI_SC_TC_Output_Select(3);
  1540. if (boardtype.reg_type == ni_reg_622x)
  1541. ai_output_control_bits |=
  1542. AI_CONVERT_Output_Select
  1543. (AI_CONVERT_Output_Enable_High);
  1544. else
  1545. ai_output_control_bits |=
  1546. AI_CONVERT_Output_Select
  1547. (AI_CONVERT_Output_Enable_Low);
  1548. devpriv->stc_writew(dev, ai_output_control_bits,
  1549. AI_Output_Control_Register);
  1550. }
  1551. /* the following registers should not be changed, because there
  1552. * are no backup registers in devpriv. If you want to chang…