parport_gsc.c 11 KB

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