parport_sunbpp.c 10 KB

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