parport_gsc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:13k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *      Low-level parallel-support for PC-style hardware integrated in the 
  3.  * LASI-Controller (on GSC-Bus) for HP-PARISC Workstations
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  *      the Free Software Foundation; either version 2 of the License, or
  8.  *      (at your option) any later version.
  9.  *
  10.  * (C) 1999-2001 by Helge Deller <deller@gmx.de>
  11.  *
  12.  * 
  13.  * based on parport_pc.c by 
  14.  *      Grant Guenther <grant@torque.net>
  15.  *      Phil Blundell <Philip.Blundell@pobox.com>
  16.  *          Tim Waugh <tim@cyberelk.demon.co.uk>
  17.  *     Jose Renau <renau@acm.org>
  18.  *          David Campbell <campbell@torque.net>
  19.  *          Andrea Arcangeli
  20.  */
  21. #undef DEBUG /* undef for production */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <linux/errno.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/ioport.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/pci.h>
  32. #include <linux/sysctl.h>
  33. #include <asm/io.h>
  34. #include <asm/dma.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/superio.h>
  37. #include <linux/parport.h>
  38. #include <asm/gsc.h>
  39. #include <asm/pdc.h>
  40. #include <asm/hardware.h>
  41. #include <asm/parport_gsc.h>
  42. MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
  43. MODULE_DESCRIPTION("HP-PARISC PC-style parallel port driver");
  44. MODULE_SUPPORTED_DEVICE("integrated PC-style parallel port");
  45. MODULE_LICENSE("GPL");
  46. /*
  47.  * Clear TIMEOUT BIT in EPP MODE
  48.  *
  49.  * This is also used in SPP detection.
  50.  */
  51. static int clear_epp_timeout(struct parport *pb)
  52. {
  53. unsigned char r;
  54. if (!(parport_gsc_read_status(pb) & 0x01))
  55. return 1;
  56. /* To clear timeout some chips require double read */
  57. parport_gsc_read_status(pb);
  58. r = parport_gsc_read_status(pb);
  59. parport_writeb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
  60. parport_writeb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
  61. r = parport_gsc_read_status(pb);
  62. return !(r & 0x01);
  63. }
  64. /*
  65.  * Access functions.
  66.  *
  67.  * Most of these aren't static because they may be used by the
  68.  * parport_xxx_yyy macros.  extern __inline__ versions of several
  69.  * of these are in parport_gsc.h.
  70.  */
  71. static void parport_gsc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  72. {
  73. parport_generic_irq(irq, (struct parport *) dev_id, regs);
  74. }
  75. void parport_gsc_write_data(struct parport *p, unsigned char d)
  76. {
  77. parport_writeb (d, DATA (p));
  78. }
  79. unsigned char parport_gsc_read_data(struct parport *p)
  80. {
  81. unsigned char c = parport_readb (DATA (p));
  82. return c;
  83. }
  84. void parport_gsc_write_control(struct parport *p, unsigned char d)
  85. {
  86. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  87.   PARPORT_CONTROL_AUTOFD |
  88.   PARPORT_CONTROL_INIT |
  89.   PARPORT_CONTROL_SELECT);
  90. /* Take this out when drivers have adapted to the newer interface. */
  91. if (d & 0x20) {
  92. pr_debug("%s (%s): use data_reverse for this!n",
  93.     p->name, p->cad->name);
  94. parport_gsc_data_reverse (p);
  95. }
  96. __parport_gsc_frob_control (p, wm, d & wm);
  97. }
  98. unsigned char parport_gsc_read_control(struct parport *p)
  99. {
  100. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  101.   PARPORT_CONTROL_AUTOFD |
  102.   PARPORT_CONTROL_INIT |
  103.   PARPORT_CONTROL_SELECT);
  104. const struct parport_gsc_private *priv = p->physport->private_data;
  105. return priv->ctr & wm; /* Use soft copy */
  106. }
  107. unsigned char parport_gsc_frob_control (struct parport *p, unsigned char mask,
  108.        unsigned char val)
  109. {
  110. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  111.   PARPORT_CONTROL_AUTOFD |
  112.   PARPORT_CONTROL_INIT |
  113.   PARPORT_CONTROL_SELECT);
  114. /* Take this out when drivers have adapted to the newer interface. */
  115. if (mask & 0x20) {
  116. pr_debug("%s (%s): use data_%s for this!n",
  117. p->name, p->cad->name,
  118. (val & 0x20) ? "reverse" : "forward");
  119. if (val & 0x20)
  120. parport_gsc_data_reverse (p);
  121. else
  122. parport_gsc_data_forward (p);
  123. }
  124. /* Restrict mask and val to control lines. */
  125. mask &= wm;
  126. val &= wm;
  127. return __parport_gsc_frob_control (p, mask, val);
  128. }
  129. unsigned char parport_gsc_read_status(struct parport *p)
  130. {
  131. return parport_readb (STATUS (p));
  132. }
  133. void parport_gsc_disable_irq(struct parport *p)
  134. {
  135. __parport_gsc_frob_control (p, 0x10, 0);
  136. }
  137. void parport_gsc_enable_irq(struct parport *p)
  138. {
  139. __parport_gsc_frob_control (p, 0x10, 0x10);
  140. }
  141. void parport_gsc_data_forward (struct parport *p)
  142. {
  143. __parport_gsc_frob_control (p, 0x20, 0);
  144. }
  145. void parport_gsc_data_reverse (struct parport *p)
  146. {
  147. __parport_gsc_frob_control (p, 0x20, 0x20);
  148. }
  149. void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
  150. {
  151. s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
  152. }
  153. void parport_gsc_save_state(struct parport *p, struct parport_state *s)
  154. {
  155. s->u.pc.ctr = parport_readb (CONTROL (p));
  156. }
  157. void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
  158. {
  159. parport_writeb (s->u.pc.ctr, CONTROL (p));
  160. }
  161. void parport_gsc_inc_use_count(void)
  162. {
  163. MOD_INC_USE_COUNT;
  164. }
  165. void parport_gsc_dec_use_count(void)
  166. {
  167. MOD_DEC_USE_COUNT;
  168. }
  169. struct parport_operations parport_gsc_ops = 
  170. {
  171. write_data: parport_gsc_write_data,
  172. read_data: parport_gsc_read_data,
  173. write_control: parport_gsc_write_control,
  174. read_control: parport_gsc_read_control,
  175. frob_control: parport_gsc_frob_control,
  176. read_status: parport_gsc_read_status,
  177. enable_irq: parport_gsc_enable_irq,
  178. disable_irq: parport_gsc_disable_irq,
  179. data_forward: parport_gsc_data_forward,
  180. data_reverse: parport_gsc_data_reverse,
  181. init_state: parport_gsc_init_state,
  182. save_state: parport_gsc_save_state,
  183. restore_state: parport_gsc_restore_state,
  184. inc_use_count: parport_gsc_inc_use_count,
  185. dec_use_count: parport_gsc_dec_use_count,
  186. epp_write_data: parport_ieee1284_epp_write_data,
  187. epp_read_data: parport_ieee1284_epp_read_data,
  188. epp_write_addr: parport_ieee1284_epp_write_addr,
  189. epp_read_addr: parport_ieee1284_epp_read_addr,
  190. ecp_write_data: parport_ieee1284_ecp_write_data,
  191. ecp_read_data: parport_ieee1284_ecp_read_data,
  192. ecp_write_addr: parport_ieee1284_ecp_write_addr,
  193. compat_write_data:  parport_ieee1284_write_compat,
  194. nibble_read_data: parport_ieee1284_read_nibble,
  195. byte_read_data: parport_ieee1284_read_byte,
  196. };
  197. /* --- Mode detection ------------------------------------- */
  198. /*
  199.  * Checks for port existence, all ports support SPP MODE
  200.  */
  201. static int __devinit parport_SPP_supported(struct parport *pb)
  202. {
  203. unsigned char r, w;
  204. /*
  205.  * first clear an eventually pending EPP timeout 
  206.  * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
  207.  * that does not even respond to SPP cycles if an EPP
  208.  * timeout is pending
  209.  */
  210. clear_epp_timeout(pb);
  211. /* Do a simple read-write test to make sure the port exists. */
  212. w = 0xc;
  213. parport_writeb (w, CONTROL (pb));
  214. /* Is there a control register that we can read from?  Some
  215.  * ports don't allow reads, so read_control just returns a
  216.  * software copy. Some ports _do_ allow reads, so bypass the
  217.  * software copy here.  In addition, some bits aren't
  218.  * writable. */
  219. r = parport_readb (CONTROL (pb));
  220. if ((r & 0xf) == w) {
  221. w = 0xe;
  222. parport_writeb (w, CONTROL (pb));
  223. r = parport_readb (CONTROL (pb));
  224. parport_writeb (0xc, CONTROL (pb));
  225. if ((r & 0xf) == w)
  226. return PARPORT_MODE_PCSPP;
  227. }
  228. /* Try the data register.  The data lines aren't tri-stated at
  229.  * this stage, so we expect back what we wrote. */
  230. w = 0xaa;
  231. parport_gsc_write_data (pb, w);
  232. r = parport_gsc_read_data (pb);
  233. if (r == w) {
  234. w = 0x55;
  235. parport_gsc_write_data (pb, w);
  236. r = parport_gsc_read_data (pb);
  237. if (r == w)
  238. return PARPORT_MODE_PCSPP;
  239. }
  240. return 0;
  241. }
  242. /* Detect PS/2 support.
  243.  *
  244.  * Bit 5 (0x20) sets the PS/2 data direction; setting this high
  245.  * allows us to read data from the data lines.  In theory we would get back
  246.  * 0xff but any peripheral attached to the port may drag some or all of the
  247.  * lines down to zero.  So if we get back anything that isn't the contents
  248.  * of the data register we deem PS/2 support to be present. 
  249.  *
  250.  * Some SPP ports have "half PS/2" ability - you can't turn off the line
  251.  * drivers, but an external peripheral with sufficiently beefy drivers of
  252.  * its own can overpower them and assert its own levels onto the bus, from
  253.  * where they can then be read back as normal.  Ports with this property
  254.  * and the right type of device attached are likely to fail the SPP test,
  255.  * (as they will appear to have stuck bits) and so the fact that they might
  256.  * be misdetected here is rather academic. 
  257.  */
  258. static int __devinit parport_PS2_supported(struct parport *pb)
  259. {
  260. int ok = 0;
  261.   
  262. clear_epp_timeout(pb);
  263. /* try to tri-state the buffer */
  264. parport_gsc_data_reverse (pb);
  265. parport_gsc_write_data(pb, 0x55);
  266. if (parport_gsc_read_data(pb) != 0x55) ok++;
  267. parport_gsc_write_data(pb, 0xaa);
  268. if (parport_gsc_read_data(pb) != 0xaa) ok++;
  269. /* cancel input mode */
  270. parport_gsc_data_forward (pb);
  271. if (ok) {
  272. pb->modes |= PARPORT_MODE_TRISTATE;
  273. } else {
  274. struct parport_gsc_private *priv = pb->private_data;
  275. priv->ctr_writable &= ~0x20;
  276. }
  277. return ok;
  278. }
  279. /* --- Initialisation code -------------------------------- */
  280. struct parport *__devinit parport_gsc_probe_port (unsigned long base,
  281.  unsigned long base_hi,
  282.  int irq, int dma,
  283.  struct pci_dev *dev)
  284. {
  285. struct parport_gsc_private *priv;
  286. struct parport_operations *ops;
  287. struct parport tmp;
  288. struct parport *p = &tmp;
  289. priv = kmalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
  290. if (!priv) {
  291. printk (KERN_DEBUG "parport (0x%lx): no memory!n", base);
  292. return NULL;
  293. }
  294. ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
  295. if (!ops) {
  296. printk (KERN_DEBUG "parport (0x%lx): no memory for ops!n",
  297. base);
  298. kfree (priv);
  299. return NULL;
  300. }
  301. memcpy (ops, &parport_gsc_ops, sizeof (struct parport_operations));
  302. priv->ctr = 0xc;
  303. priv->ctr_writable = 0xff;
  304. priv->dma_buf = 0;
  305. priv->dma_handle = 0;
  306. priv->dev = dev;
  307. p->base = base;
  308. p->base_hi = base_hi;
  309. p->irq = irq;
  310. p->dma = dma;
  311. p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
  312. p->ops = ops;
  313. p->private_data = priv;
  314. p->physport = p;
  315. if (!parport_SPP_supported (p)) {
  316. /* No port. */
  317. kfree (priv);
  318. return NULL;
  319. }
  320. parport_PS2_supported (p);
  321. if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
  322. PARPORT_DMA_NONE, ops))) {
  323. kfree (priv);
  324. kfree (ops);
  325. return NULL;
  326. }
  327. p->base_hi = base_hi;
  328. p->modes = tmp.modes;
  329. p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
  330. p->private_data = priv;
  331. printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
  332. p->irq = irq;
  333. if (p->irq == PARPORT_IRQ_AUTO) {
  334. p->irq = PARPORT_IRQ_NONE;
  335. }
  336. if (p->irq != PARPORT_IRQ_NONE) {
  337. printk(", irq %d", p->irq);
  338. if (p->dma == PARPORT_DMA_AUTO) {
  339. p->dma = PARPORT_DMA_NONE;
  340. }
  341. }
  342. if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
  343.                                            is mandatory (see above) */
  344. p->dma = PARPORT_DMA_NONE;
  345. printk(" [");
  346. #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
  347. {
  348. int f = 0;
  349. printmode(PCSPP);
  350. printmode(TRISTATE);
  351. printmode(COMPAT)
  352. printmode(EPP);
  353. // printmode(ECP);
  354. // printmode(DMA);
  355. }
  356. #undef printmode
  357. printk("]n");
  358. parport_proc_register(p);
  359. if (p->irq != PARPORT_IRQ_NONE) {
  360. if (request_irq (p->irq, parport_gsc_interrupt,
  361.  0, p->name, p)) {
  362. printk (KERN_WARNING "%s: irq %d in use, "
  363. "resorting to polled operationn",
  364. p->name, p->irq);
  365. p->irq = PARPORT_IRQ_NONE;
  366. p->dma = PARPORT_DMA_NONE;
  367. }
  368. }
  369. /* Done probing.  Now put the port into a sensible start-up state. */
  370. parport_gsc_write_data(p, 0);
  371. parport_gsc_data_forward (p);
  372. /* Now that we've told the sharing engine about the port, and
  373.    found out its characteristics, let the high-level drivers
  374.    know about it. */
  375. parport_announce_port (p);
  376. return p;
  377. }
  378. #define PARPORT_GSC_OFFSET 0x800
  379. static int __initdata parport_count;
  380. static int __devinit parport_init_chip(struct parisc_device *dev)
  381. {
  382. unsigned long port;
  383. if (!dev->irq) {
  384. printk("IRQ not found for parallel device at 0x%lxn", dev->hpa);
  385. return -ENODEV;
  386. }
  387. port = dev->hpa + PARPORT_GSC_OFFSET;
  388. /* some older machines with ASP-chip don't support
  389.  * the enhanced parport modes.
  390.  */
  391. if (boot_cpu_data.cpu_type > pcxt && !pdc_add_valid(port+4)) {
  392. /* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
  393. printk("%s: initialize bidirectional-mode.n", __FUNCTION__);
  394. parport_writeb ( (0x10 + 0x20), port + 4);
  395. } else {
  396. printk("%s: enhanced parport-modes not supported.n", __FUNCTION__);
  397. }
  398. if (parport_gsc_probe_port(port, 0, dev->irq,
  399. /* PARPORT_IRQ_NONE */ PARPORT_DMA_NONE, NULL))
  400. parport_count++;
  401. return 0;
  402. }
  403. static struct parisc_device_id parport_tbl[] = {
  404. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x74 },
  405. { 0, }
  406. };
  407. MODULE_DEVICE_TABLE(parisc, parport_tbl);
  408. static struct parisc_driver parport_driver = {
  409. name: "Parallel",
  410. id_table: parport_tbl,
  411. probe: parport_init_chip,
  412. };
  413. int __devinit parport_gsc_init(void)
  414. {
  415. return register_parisc_driver(&parport_driver);
  416. }
  417. static void __devexit parport_gsc_exit(void)
  418. {
  419. struct parport *p = parport_enumerate(), *tmp;
  420. while (p) {
  421. tmp = p->next;
  422. if (p->modes & PARPORT_MODE_PCSPP) { 
  423. struct parport_gsc_private *priv = p->private_data;
  424. struct parport_operations *ops = p->ops;
  425. if (p->dma != PARPORT_DMA_NONE)
  426. free_dma(p->dma);
  427. if (p->irq != PARPORT_IRQ_NONE)
  428. free_irq(p->irq, p);
  429. parport_proc_unregister(p);
  430. if (priv->dma_buf)
  431. pci_free_consistent(priv->dev, PAGE_SIZE,
  432.     priv->dma_buf,
  433.     priv->dma_handle);
  434. kfree (p->private_data);
  435. parport_unregister_port(p);
  436. kfree (ops); /* hope no-one cached it */
  437. }
  438. p = tmp;
  439. }
  440. unregister_parisc_driver(&parport_driver);
  441. }
  442. EXPORT_NO_SYMBOLS;
  443. module_init(parport_gsc_init);
  444. module_exit(parport_gsc_exit);