parport_gsc.c 11 KB

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