parport_sunbpp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. static unsigned char status_sunbpp_to_pc(struct parport *p)
  72. {
  73. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  74. unsigned char bits = 0;
  75. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  76. unsigned char value_ir = sbus_readb(&regs->p_ir);
  77. if (!(value_ir & P_IR_ERR))
  78. bits |= PARPORT_STATUS_ERROR;
  79. if (!(value_ir & P_IR_SLCT))
  80. bits |= PARPORT_STATUS_SELECT;
  81. if (!(value_ir & P_IR_PE))
  82. bits |= PARPORT_STATUS_PAPEROUT;
  83. if (value_tcr & P_TCR_ACK)
  84. bits |= PARPORT_STATUS_ACK;
  85. if (!(value_tcr & P_TCR_BUSY))
  86. bits |= PARPORT_STATUS_BUSY;
  87. dprintk((KERN_DEBUG "tcr 0x%x ir 0x%x\n", value_tcr, value_ir));
  88. dprintk((KERN_DEBUG "read status 0x%x\n", bits));
  89. return bits;
  90. }
  91. static unsigned char control_sunbpp_to_pc(struct parport *p)
  92. {
  93. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  94. unsigned char bits = 0;
  95. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  96. unsigned char value_or = sbus_readb(&regs->p_or);
  97. if (!(value_tcr & P_TCR_DS))
  98. bits |= PARPORT_CONTROL_STROBE;
  99. if (!(value_or & P_OR_AFXN))
  100. bits |= PARPORT_CONTROL_AUTOFD;
  101. if (!(value_or & P_OR_INIT))
  102. bits |= PARPORT_CONTROL_INIT;
  103. if (value_or & P_OR_SLCT_IN)
  104. bits |= PARPORT_CONTROL_SELECT;
  105. dprintk((KERN_DEBUG "tcr 0x%x or 0x%x\n", value_tcr, value_or));
  106. dprintk((KERN_DEBUG "read control 0x%x\n", bits));
  107. return bits;
  108. }
  109. static unsigned char parport_sunbpp_read_control(struct parport *p)
  110. {
  111. return control_sunbpp_to_pc(p);
  112. }
  113. static unsigned char parport_sunbpp_frob_control(struct parport *p,
  114. unsigned char mask,
  115. unsigned char val)
  116. {
  117. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  118. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  119. unsigned char value_or = sbus_readb(&regs->p_or);
  120. dprintk((KERN_DEBUG "frob1: tcr 0x%x or 0x%x\n",
  121. value_tcr, value_or));
  122. if (mask & PARPORT_CONTROL_STROBE) {
  123. if (val & PARPORT_CONTROL_STROBE) {
  124. value_tcr &= ~P_TCR_DS;
  125. } else {
  126. value_tcr |= P_TCR_DS;
  127. }
  128. }
  129. if (mask & PARPORT_CONTROL_AUTOFD) {
  130. if (val & PARPORT_CONTROL_AUTOFD) {
  131. value_or &= ~P_OR_AFXN;
  132. } else {
  133. value_or |= P_OR_AFXN;
  134. }
  135. }
  136. if (mask & PARPORT_CONTROL_INIT) {
  137. if (val & PARPORT_CONTROL_INIT) {
  138. value_or &= ~P_OR_INIT;
  139. } else {
  140. value_or |= P_OR_INIT;
  141. }
  142. }
  143. if (mask & PARPORT_CONTROL_SELECT) {
  144. if (val & PARPORT_CONTROL_SELECT) {
  145. value_or |= P_OR_SLCT_IN;
  146. } else {
  147. value_or &= ~P_OR_SLCT_IN;
  148. }
  149. }
  150. sbus_writeb(value_or, &regs->p_or);
  151. sbus_writeb(value_tcr, &regs->p_tcr);
  152. dprintk((KERN_DEBUG "frob2: tcr 0x%x or 0x%x\n",
  153. value_tcr, value_or));
  154. return parport_sunbpp_read_control(p);
  155. }
  156. static void parport_sunbpp_write_control(struct parport *p, unsigned char d)
  157. {
  158. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  159. PARPORT_CONTROL_AUTOFD |
  160. PARPORT_CONTROL_INIT |
  161. PARPORT_CONTROL_SELECT);
  162. parport_sunbpp_frob_control (p, wm, d & wm);
  163. }
  164. static unsigned char parport_sunbpp_read_status(struct parport *p)
  165. {
  166. return status_sunbpp_to_pc(p);
  167. }
  168. static void parport_sunbpp_data_forward (struct parport *p)
  169. {
  170. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  171. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  172. dprintk((KERN_DEBUG "forward\n"));
  173. value_tcr &= ~P_TCR_DIR;
  174. sbus_writeb(value_tcr, &regs->p_tcr);
  175. }
  176. static void parport_sunbpp_data_reverse (struct parport *p)
  177. {
  178. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  179. u8 val = sbus_readb(&regs->p_tcr);
  180. dprintk((KERN_DEBUG "reverse\n"));
  181. val |= P_TCR_DIR;
  182. sbus_writeb(val, &regs->p_tcr);
  183. }
  184. static void parport_sunbpp_init_state(struct pardevice *dev, struct parport_state *s)
  185. {
  186. s->u.pc.ctr = 0xc;
  187. s->u.pc.ecr = 0x0;
  188. }
  189. static void parport_sunbpp_save_state(struct parport *p, struct parport_state *s)
  190. {
  191. s->u.pc.ctr = parport_sunbpp_read_control(p);
  192. }
  193. static void parport_sunbpp_restore_state(struct parport *p, struct parport_state *s)
  194. {
  195. parport_sunbpp_write_control(p, s->u.pc.ctr);
  196. }
  197. static struct parport_operations parport_sunbpp_ops =
  198. {
  199. .write_data = parport_sunbpp_write_data,
  200. .read_data = parport_sunbpp_read_data,
  201. .write_control = parport_sunbpp_write_control,
  202. .read_control = parport_sunbpp_read_control,
  203. .frob_control = parport_sunbpp_frob_control,
  204. .read_status = parport_sunbpp_read_status,
  205. .enable_irq = parport_sunbpp_enable_irq,
  206. .disable_irq = parport_sunbpp_disable_irq,
  207. .data_forward = parport_sunbpp_data_forward,
  208. .data_reverse = parport_sunbpp_data_reverse,
  209. .init_state = parport_sunbpp_init_state,
  210. .save_state = parport_sunbpp_save_state,
  211. .restore_state = parport_sunbpp_restore_state,
  212. .epp_write_data = parport_ieee1284_epp_write_data,
  213. .epp_read_data = parport_ieee1284_epp_read_data,
  214. .epp_write_addr = parport_ieee1284_epp_write_addr,
  215. .epp_read_addr = parport_ieee1284_epp_read_addr,
  216. .ecp_write_data = parport_ieee1284_ecp_write_data,
  217. .ecp_read_data = parport_ieee1284_ecp_read_data,
  218. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  219. .compat_write_data = parport_ieee1284_write_compat,
  220. .nibble_read_data = parport_ieee1284_read_nibble,
  221. .byte_read_data = parport_ieee1284_read_byte,
  222. .owner = THIS_MODULE,
  223. };
  224. static int bpp_probe(struct platform_device *op)
  225. {
  226. struct parport_operations *ops;
  227. struct bpp_regs __iomem *regs;
  228. int irq, dma, err = 0, size;
  229. unsigned char value_tcr;
  230. void __iomem *base;
  231. struct parport *p;
  232. irq = op->archdata.irqs[0];
  233. base = of_ioremap(&op->resource[0], 0,
  234. resource_size(&op->resource[0]),
  235. "sunbpp");
  236. if (!base)
  237. return -ENODEV;
  238. size = resource_size(&op->resource[0]);
  239. dma = PARPORT_DMA_NONE;
  240. ops = kmemdup(&parport_sunbpp_ops, sizeof(struct parport_operations),
  241. GFP_KERNEL);
  242. if (!ops) {
  243. err = -ENOMEM;
  244. goto out_unmap;
  245. }
  246. dprintk(("register_port\n"));
  247. if (!(p = parport_register_port((unsigned long)base, irq, dma, ops))) {
  248. err = -ENOMEM;
  249. goto out_free_ops;
  250. }
  251. p->size = size;
  252. p->dev = &op->dev;
  253. if ((err = request_irq(p->irq, parport_irq_handler,
  254. IRQF_SHARED, p->name, p)) != 0) {
  255. goto out_put_port;
  256. }
  257. parport_sunbpp_enable_irq(p);
  258. regs = (struct bpp_regs __iomem *)p->base;
  259. value_tcr = sbus_readb(&regs->p_tcr);
  260. value_tcr &= ~P_TCR_DIR;
  261. sbus_writeb(value_tcr, &regs->p_tcr);
  262. printk(KERN_INFO "%s: sunbpp at 0x%lx\n", p->name, p->base);
  263. dev_set_drvdata(&op->dev, p);
  264. parport_announce_port(p);
  265. return 0;
  266. out_put_port:
  267. parport_put_port(p);
  268. out_free_ops:
  269. kfree(ops);
  270. out_unmap:
  271. of_iounmap(&op->resource[0], base, size);
  272. return err;
  273. }
  274. static int bpp_remove(struct platform_device *op)
  275. {
  276. struct parport *p = dev_get_drvdata(&op->dev);
  277. struct parport_operations *ops = p->ops;
  278. parport_remove_port(p);
  279. if (p->irq != PARPORT_IRQ_NONE) {
  280. parport_sunbpp_disable_irq(p);
  281. free_irq(p->irq, p);
  282. }
  283. of_iounmap(&op->resource[0], (void __iomem *) p->base, p->size);
  284. parport_put_port(p);
  285. kfree(ops);
  286. dev_set_drvdata(&op->dev, NULL);
  287. return 0;
  288. }
  289. static const struct of_device_id bpp_match[] = {
  290. {
  291. .name = "SUNW,bpp",
  292. },
  293. {},
  294. };
  295. MODULE_DEVICE_TABLE(of, bpp_match);
  296. static struct platform_driver bpp_sbus_driver = {
  297. .driver = {
  298. .name = "bpp",
  299. .of_match_table = bpp_match,
  300. },
  301. .probe = bpp_probe,
  302. .remove = bpp_remove,
  303. };
  304. module_platform_driver(bpp_sbus_driver);
  305. MODULE_AUTHOR("Derrick J Brashear");
  306. MODULE_DESCRIPTION("Parport Driver for Sparc bidirectional Port");
  307. MODULE_SUPPORTED_DEVICE("Sparc Bidirectional Parallel Port");
  308. MODULE_VERSION("2.0");
  309. MODULE_LICENSE("GPL");