parport_gsc.c 11 KB

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