parport_sunbpp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* parport_sunbpp.c: Parallel-port routines for SBUS
  3. *
  4. * Author: Derrick J. Brashear <shadow@dementia.org>
  5. *
  6. * based on work by:
  7. * Phil Blundell <philb@gnu.org>
  8. * Tim Waugh <tim@cyberelk.demon.co.uk>
  9. * Jose Renau <renau@acm.org>
  10. * David Campbell <campbell@tirian.che.curtin.edu.au>
  11. * Grant Guenther <grant@torque.net>
  12. * Eddie C. Dost <ecd@skynet.be>
  13. * Stephen Williams (steve@icarus.com)
  14. * Gus Baldauf (gbaldauf@ix.netcom.com)
  15. * Peter Zaitcev
  16. * Tom Dyas
  17. *
  18. * Updated to new SBUS device framework: David S. Miller <davem@davemloft.net>
  19. *
  20. */
  21. #include <linux/string.h>
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/ioport.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/parport.h>
  32. #include <asm/ptrace.h>
  33. #include <linux/interrupt.h>
  34. #include <asm/io.h>
  35. #include <asm/oplib.h> /* OpenProm Library */
  36. #include <asm/dma.h> /* BPP uses LSI 64854 for DMA */
  37. #include <asm/irq.h>
  38. #include <asm/sunbpp.h>
  39. #undef __SUNBPP_DEBUG
  40. #ifdef __SUNBPP_DEBUG
  41. #define dprintk(x) printk x
  42. #else
  43. #define dprintk(x)
  44. #endif
  45. static void parport_sunbpp_disable_irq(struct parport *p)
  46. {
  47. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  48. u32 tmp;
  49. tmp = sbus_readl(&regs->p_csr);
  50. tmp &= ~DMA_INT_ENAB;
  51. sbus_writel(tmp, &regs->p_csr);
  52. }
  53. static void parport_sunbpp_enable_irq(struct parport *p)
  54. {
  55. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  56. u32 tmp;
  57. tmp = sbus_readl(&regs->p_csr);
  58. tmp |= DMA_INT_ENAB;
  59. sbus_writel(tmp, &regs->p_csr);
  60. }
  61. static void parport_sunbpp_write_data(struct parport *p, unsigned char d)
  62. {
  63. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  64. sbus_writeb(d, &regs->p_dr);
  65. dprintk((KERN_DEBUG "wrote 0x%x\n", d));
  66. }
  67. static unsigned char parport_sunbpp_read_data(struct parport *p)
  68. {
  69. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  70. return sbus_readb(&regs->p_dr);
  71. }
  72. static unsigned char status_sunbpp_to_pc(struct parport *p)
  73. {
  74. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  75. unsigned char bits = 0;
  76. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  77. unsigned char value_ir = sbus_readb(&regs->p_ir);
  78. if (!(value_ir & P_IR_ERR))
  79. bits |= PARPORT_STATUS_ERROR;
  80. if (!(value_ir & P_IR_SLCT))
  81. bits |= PARPORT_STATUS_SELECT;
  82. if (!(value_ir & P_IR_PE))
  83. bits |= PARPORT_STATUS_PAPEROUT;
  84. if (value_tcr & P_TCR_ACK)
  85. bits |= PARPORT_STATUS_ACK;
  86. if (!(value_tcr & P_TCR_BUSY))
  87. bits |= PARPORT_STATUS_BUSY;
  88. dprintk((KERN_DEBUG "tcr 0x%x ir 0x%x\n", value_tcr, value_ir));
  89. dprintk((KERN_DEBUG "read status 0x%x\n", bits));
  90. return bits;
  91. }
  92. static unsigned char control_sunbpp_to_pc(struct parport *p)
  93. {
  94. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  95. unsigned char bits = 0;
  96. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  97. unsigned char value_or = sbus_readb(&regs->p_or);
  98. if (!(value_tcr & P_TCR_DS))
  99. bits |= PARPORT_CONTROL_STROBE;
  100. if (!(value_or & P_OR_AFXN))
  101. bits |= PARPORT_CONTROL_AUTOFD;
  102. if (!(value_or & P_OR_INIT))
  103. bits |= PARPORT_CONTROL_INIT;
  104. if (value_or & P_OR_SLCT_IN)
  105. bits |= PARPORT_CONTROL_SELECT;
  106. dprintk((KERN_DEBUG "tcr 0x%x or 0x%x\n", value_tcr, value_or));
  107. dprintk((KERN_DEBUG "read control 0x%x\n", bits));
  108. return bits;
  109. }
  110. static unsigned char parport_sunbpp_read_control(struct parport *p)
  111. {
  112. return control_sunbpp_to_pc(p);
  113. }
  114. static unsigned char parport_sunbpp_frob_control(struct parport *p,
  115. unsigned char mask,
  116. unsigned char val)
  117. {
  118. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  119. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  120. unsigned char value_or = sbus_readb(&regs->p_or);
  121. dprintk((KERN_DEBUG "frob1: tcr 0x%x or 0x%x\n",
  122. value_tcr, value_or));
  123. if (mask & PARPORT_CONTROL_STROBE) {
  124. if (val & PARPORT_CONTROL_STROBE) {
  125. value_tcr &= ~P_TCR_DS;
  126. } else {
  127. value_tcr |= P_TCR_DS;
  128. }
  129. }
  130. if (mask & PARPORT_CONTROL_AUTOFD) {
  131. if (val & PARPORT_CONTROL_AUTOFD) {
  132. value_or &= ~P_OR_AFXN;
  133. } else {
  134. value_or |= P_OR_AFXN;
  135. }
  136. }
  137. if (mask & PARPORT_CONTROL_INIT) {
  138. if (val & PARPORT_CONTROL_INIT) {
  139. value_or &= ~P_OR_INIT;
  140. } else {
  141. value_or |= P_OR_INIT;
  142. }
  143. }
  144. if (mask & PARPORT_CONTROL_SELECT) {
  145. if (val & PARPORT_CONTROL_SELECT) {
  146. value_or |= P_OR_SLCT_IN;
  147. } else {
  148. value_or &= ~P_OR_SLCT_IN;
  149. }
  150. }
  151. sbus_writeb(value_or, &regs->p_or);
  152. sbus_writeb(value_tcr, &regs->p_tcr);
  153. dprintk((KERN_DEBUG "frob2: tcr 0x%x or 0x%x\n",
  154. value_tcr, value_or));
  155. return parport_sunbpp_read_control(p);
  156. }
  157. static void parport_sunbpp_write_control(struct parport *p, unsigned char d)
  158. {
  159. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  160. PARPORT_CONTROL_AUTOFD |
  161. PARPORT_CONTROL_INIT |
  162. PARPORT_CONTROL_SELECT);
  163. parport_sunbpp_frob_control (p, wm, d & wm);
  164. }
  165. static unsigned char parport_sunbpp_read_status(struct parport *p)
  166. {
  167. return status_sunbpp_to_pc(p);
  168. }
  169. static void parport_sunbpp_data_forward (struct parport *p)
  170. {
  171. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  172. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  173. dprintk((KERN_DEBUG "forward\n"));
  174. value_tcr &= ~P_TCR_DIR;
  175. sbus_writeb(value_tcr, &regs->p_tcr);
  176. }
  177. static void parport_sunbpp_data_reverse (struct parport *p)
  178. {
  179. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  180. u8 val = sbus_readb(&regs->p_tcr);
  181. dprintk((KERN_DEBUG "reverse\n"));
  182. val |= P_TCR_DIR;
  183. sbus_writeb(val, &regs->p_tcr);
  184. }
  185. static void parport_sunbpp_init_state(struct pardevice *dev, struct parport_state *s)
  186. {
  187. s->u.pc.ctr = 0xc;
  188. s->u.pc.ecr = 0x0;
  189. }
  190. static void parport_sunbpp_save_state(struct parport *p, struct parport_state *s)
  191. {
  192. s->u.pc.ctr = parport_sunbpp_read_control(p);
  193. }
  194. static void parport_sunbpp_restore_state(struct parport *p, struct parport_state *s)
  195. {
  196. parport_sunbpp_write_control(p, s->u.pc.ctr);
  197. }
  198. static struct parport_operations parport_sunbpp_ops =
  199. {
  200. .write_data = parport_sunbpp_write_data,
  201. .read_data = parport_sunbpp_read_data,
  202. .write_control = parport_sunbpp_write_control,
  203. .read_control = parport_sunbpp_read_control,
  204. .frob_control = parport_sunbpp_frob_control,
  205. .read_status = parport_sunbpp_read_status,
  206. .enable_irq = parport_sunbpp_enable_irq,
  207. .disable_irq = parport_sunbpp_disable_irq,
  208. .data_forward = parport_sunbpp_data_forward,
  209. .data_reverse = parport_sunbpp_data_reverse,
  210. .init_state = parport_sunbpp_init_state,
  211. .save_state = parport_sunbpp_save_state,
  212. .restore_state = parport_sunbpp_restore_state,
  213. .epp_write_data = parport_ieee1284_epp_write_data,
  214. .epp_read_data = parport_ieee1284_epp_read_data,
  215. .epp_write_addr = parport_ieee1284_epp_write_addr,
  216. .epp_read_addr = parport_ieee1284_epp_read_addr,
  217. .ecp_write_data = parport_ieee1284_ecp_write_data,
  218. .ecp_read_data = parport_ieee1284_ecp_read_data,
  219. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  220. .compat_write_data = parport_ieee1284_write_compat,
  221. .nibble_read_data = parport_ieee1284_read_nibble,
  222. .byte_read_data = parport_ieee1284_read_byte,
  223. .owner = THIS_MODULE,
  224. };
  225. static int bpp_probe(struct platform_device *op)
  226. {
  227. struct parport_operations *ops;
  228. struct bpp_regs __iomem *regs;
  229. int irq, dma, err = 0, size;
  230. unsigned char value_tcr;
  231. void __iomem *base;
  232. struct parport *p;
  233. irq = op->archdata.irqs[0];
  234. base = of_ioremap(&op->resource[0], 0,
  235. resource_size(&op->resource[0]),
  236. "sunbpp");
  237. if (!base)
  238. return -ENODEV;
  239. size = resource_size(&op->resource[0]);
  240. dma = PARPORT_DMA_NONE;
  241. ops = kmemdup(&parport_sunbpp_ops, sizeof(struct parport_operations),
  242. GFP_KERNEL);
  243. if (!ops) {
  244. err = -ENOMEM;
  245. goto out_unmap;
  246. }
  247. dprintk(("register_port\n"));
  248. if (!(p = parport_register_port((unsigned long)base, irq, dma, ops))) {
  249. err = -ENOMEM;
  250. goto out_free_ops;
  251. }
  252. p->size = size;
  253. p->dev = &op->dev;
  254. if ((err = request_irq(p->irq, parport_irq_handler,
  255. IRQF_SHARED, p->name, p)) != 0) {
  256. goto out_put_port;
  257. }
  258. parport_sunbpp_enable_irq(p);
  259. regs = (struct bpp_regs __iomem *)p->base;
  260. value_tcr = sbus_readb(&regs->p_tcr);
  261. value_tcr &= ~P_TCR_DIR;
  262. sbus_writeb(value_tcr, &regs->p_tcr);
  263. pr_info("%s: sunbpp at 0x%lx\n", p->name, p->base);
  264. dev_set_drvdata(&op->dev, p);
  265. parport_announce_port(p);
  266. return 0;
  267. out_put_port:
  268. parport_put_port(p);
  269. out_free_ops:
  270. kfree(ops);
  271. out_unmap:
  272. of_iounmap(&op->resource[0], base, size);
  273. return err;
  274. }
  275. static int bpp_remove(struct platform_device *op)
  276. {
  277. struct parport *p = dev_get_drvdata(&op->dev);
  278. struct parport_operations *ops = p->ops;
  279. parport_remove_port(p);
  280. if (p->irq != PARPORT_IRQ_NONE) {
  281. parport_sunbpp_disable_irq(p);
  282. free_irq(p->irq, p);
  283. }
  284. of_iounmap(&op->resource[0], (void __iomem *) p->base, p->size);
  285. parport_put_port(p);
  286. kfree(ops);
  287. dev_set_drvdata(&op->dev, NULL);
  288. return 0;
  289. }
  290. static const struct of_device_id bpp_match[] = {
  291. {
  292. .name = "SUNW,bpp",
  293. },
  294. {},
  295. };
  296. MODULE_DEVICE_TABLE(of, bpp_match);
  297. static struct platform_driver bpp_sbus_driver = {
  298. .driver = {
  299. .name = "bpp",
  300. .of_match_table = bpp_match,
  301. },
  302. .probe = bpp_probe,
  303. .remove = bpp_remove,
  304. };
  305. module_platform_driver(bpp_sbus_driver);
  306. MODULE_AUTHOR("Derrick J Brashear");
  307. MODULE_DESCRIPTION("Parport Driver for Sparc bidirectional Port");
  308. MODULE_SUPPORTED_DEVICE("Sparc Bidirectional Parallel Port");
  309. MODULE_VERSION("2.0");
  310. MODULE_LICENSE("GPL");