parport_gsc.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:14k
源码类别:

嵌入式Linux

开发平台:

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