parport_gsc.c 11 KB

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