parport_sunbpp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/parport.h>
  29. #include <asm/ptrace.h>
  30. #include <linux/interrupt.h>
  31. #include <asm/io.h>
  32. #include <asm/oplib.h> /* OpenProm Library */
  33. #include <asm/sbus.h>
  34. #include <asm/dma.h> /* BPP uses LSI 64854 for DMA */
  35. #include <asm/irq.h>
  36. #include <asm/sunbpp.h>
  37. #undef __SUNBPP_DEBUG
  38. #ifdef __SUNBPP_DEBUG
  39. #define dprintk(x) printk x
  40. #else
  41. #define dprintk(x)
  42. #endif
  43. static void parport_sunbpp_disable_irq(struct parport *p)
  44. {
  45. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  46. u32 tmp;
  47. tmp = sbus_readl(&regs->p_csr);
  48. tmp &= ~DMA_INT_ENAB;
  49. sbus_writel(tmp, &regs->p_csr);
  50. }
  51. static void parport_sunbpp_enable_irq(struct parport *p)
  52. {
  53. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  54. u32 tmp;
  55. tmp = sbus_readl(&regs->p_csr);
  56. tmp |= DMA_INT_ENAB;
  57. sbus_writel(tmp, &regs->p_csr);
  58. }
  59. static void parport_sunbpp_write_data(struct parport *p, unsigned char d)
  60. {
  61. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  62. sbus_writeb(d, &regs->p_dr);
  63. dprintk((KERN_DEBUG "wrote 0x%x\n", d));
  64. }
  65. static unsigned char parport_sunbpp_read_data(struct parport *p)
  66. {
  67. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  68. return sbus_readb(&regs->p_dr);
  69. }
  70. #if 0
  71. static void control_pc_to_sunbpp(struct parport *p, unsigned char status)
  72. {
  73. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  74. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  75. unsigned char value_or = sbus_readb(&regs->p_or);
  76. if (status & PARPORT_CONTROL_STROBE)
  77. value_tcr |= P_TCR_DS;
  78. if (status & PARPORT_CONTROL_AUTOFD)
  79. value_or |= P_OR_AFXN;
  80. if (status & PARPORT_CONTROL_INIT)
  81. value_or |= P_OR_INIT;
  82. if (status & PARPORT_CONTROL_SELECT)
  83. value_or |= P_OR_SLCT_IN;
  84. sbus_writeb(value_or, &regs->p_or);
  85. sbus_writeb(value_tcr, &regs->p_tcr);
  86. }
  87. #endif
  88. static unsigned char status_sunbpp_to_pc(struct parport *p)
  89. {
  90. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  91. unsigned char bits = 0;
  92. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  93. unsigned char value_ir = sbus_readb(&regs->p_ir);
  94. if (!(value_ir & P_IR_ERR))
  95. bits |= PARPORT_STATUS_ERROR;
  96. if (!(value_ir & P_IR_SLCT))
  97. bits |= PARPORT_STATUS_SELECT;
  98. if (!(value_ir & P_IR_PE))
  99. bits |= PARPORT_STATUS_PAPEROUT;
  100. if (value_tcr & P_TCR_ACK)
  101. bits |= PARPORT_STATUS_ACK;
  102. if (!(value_tcr & P_TCR_BUSY))
  103. bits |= PARPORT_STATUS_BUSY;
  104. dprintk((KERN_DEBUG "tcr 0x%x ir 0x%x\n", value_tcr, value_ir));
  105. dprintk((KERN_DEBUG "read status 0x%x\n", bits));
  106. return bits;
  107. }
  108. static unsigned char control_sunbpp_to_pc(struct parport *p)
  109. {
  110. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  111. unsigned char bits = 0;
  112. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  113. unsigned char value_or = sbus_readb(&regs->p_or);
  114. if (!(value_tcr & P_TCR_DS))
  115. bits |= PARPORT_CONTROL_STROBE;
  116. if (!(value_or & P_OR_AFXN))
  117. bits |= PARPORT_CONTROL_AUTOFD;
  118. if (!(value_or & P_OR_INIT))
  119. bits |= PARPORT_CONTROL_INIT;
  120. if (value_or & P_OR_SLCT_IN)
  121. bits |= PARPORT_CONTROL_SELECT;
  122. dprintk((KERN_DEBUG "tcr 0x%x or 0x%x\n", value_tcr, value_or));
  123. dprintk((KERN_DEBUG "read control 0x%x\n", bits));
  124. return bits;
  125. }
  126. static unsigned char parport_sunbpp_read_control(struct parport *p)
  127. {
  128. return control_sunbpp_to_pc(p);
  129. }
  130. static unsigned char parport_sunbpp_frob_control(struct parport *p,
  131. unsigned char mask,
  132. unsigned char val)
  133. {
  134. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  135. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  136. unsigned char value_or = sbus_readb(&regs->p_or);
  137. dprintk((KERN_DEBUG "frob1: tcr 0x%x or 0x%x\n",
  138. value_tcr, value_or));
  139. if (mask & PARPORT_CONTROL_STROBE) {
  140. if (val & PARPORT_CONTROL_STROBE) {
  141. value_tcr &= ~P_TCR_DS;
  142. } else {
  143. value_tcr |= P_TCR_DS;
  144. }
  145. }
  146. if (mask & PARPORT_CONTROL_AUTOFD) {
  147. if (val & PARPORT_CONTROL_AUTOFD) {
  148. value_or &= ~P_OR_AFXN;
  149. } else {
  150. value_or |= P_OR_AFXN;
  151. }
  152. }
  153. if (mask & PARPORT_CONTROL_INIT) {
  154. if (val & PARPORT_CONTROL_INIT) {
  155. value_or &= ~P_OR_INIT;
  156. } else {
  157. value_or |= P_OR_INIT;
  158. }
  159. }
  160. if (mask & PARPORT_CONTROL_SELECT) {
  161. if (val & PARPORT_CONTROL_SELECT) {
  162. value_or |= P_OR_SLCT_IN;
  163. } else {
  164. value_or &= ~P_OR_SLCT_IN;
  165. }
  166. }
  167. sbus_writeb(value_or, &regs->p_or);
  168. sbus_writeb(value_tcr, &regs->p_tcr);
  169. dprintk((KERN_DEBUG "frob2: tcr 0x%x or 0x%x\n",
  170. value_tcr, value_or));
  171. return parport_sunbpp_read_control(p);
  172. }
  173. static void parport_sunbpp_write_control(struct parport *p, unsigned char d)
  174. {
  175. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  176. PARPORT_CONTROL_AUTOFD |
  177. PARPORT_CONTROL_INIT |
  178. PARPORT_CONTROL_SELECT);
  179. parport_sunbpp_frob_control (p, wm, d & wm);
  180. }
  181. static unsigned char parport_sunbpp_read_status(struct parport *p)
  182. {
  183. return status_sunbpp_to_pc(p);
  184. }
  185. static void parport_sunbpp_data_forward (struct parport *p)
  186. {
  187. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  188. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  189. dprintk((KERN_DEBUG "forward\n"));
  190. value_tcr &= ~P_TCR_DIR;
  191. sbus_writeb(value_tcr, &regs->p_tcr);
  192. }
  193. static void parport_sunbpp_data_reverse (struct parport *p)
  194. {
  195. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  196. u8 val = sbus_readb(&regs->p_tcr);
  197. dprintk((KERN_DEBUG "reverse\n"));
  198. val |= P_TCR_DIR;
  199. sbus_writeb(val, &regs->p_tcr);
  200. }
  201. static void parport_sunbpp_init_state(struct pardevice *dev, struct parport_state *s)
  202. {
  203. s->u.pc.ctr = 0xc;
  204. s->u.pc.ecr = 0x0;
  205. }
  206. static void parport_sunbpp_save_state(struct parport *p, struct parport_state *s)
  207. {
  208. s->u.pc.ctr = parport_sunbpp_read_control(p);
  209. }
  210. static void parport_sunbpp_restore_state(struct parport *p, struct parport_state *s)
  211. {
  212. parport_sunbpp_write_control(p, s->u.pc.ctr);
  213. }
  214. static struct parport_operations parport_sunbpp_ops =
  215. {
  216. .write_data = parport_sunbpp_write_data,
  217. .read_data = parport_sunbpp_read_data,
  218. .write_control = parport_sunbpp_write_control,
  219. .read_control = parport_sunbpp_read_control,
  220. .frob_control = parport_sunbpp_frob_control,
  221. .read_status = parport_sunbpp_read_status,
  222. .enable_irq = parport_sunbpp_enable_irq,
  223. .disable_irq = parport_sunbpp_disable_irq,
  224. .data_forward = parport_sunbpp_data_forward,
  225. .data_reverse = parport_sunbpp_data_reverse,
  226. .init_state = parport_sunbpp_init_state,
  227. .save_state = parport_sunbpp_save_state,
  228. .restore_state = parport_sunbpp_restore_state,
  229. .epp_write_data = parport_ieee1284_epp_write_data,
  230. .epp_read_data = parport_ieee1284_epp_read_data,
  231. .epp_write_addr = parport_ieee1284_epp_write_addr,
  232. .epp_read_addr = parport_ieee1284_epp_read_addr,
  233. .ecp_write_data = parport_ieee1284_ecp_write_data,
  234. .ecp_read_data = parport_ieee1284_ecp_read_data,
  235. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  236. .compat_write_data = parport_ieee1284_write_compat,
  237. .nibble_read_data = parport_ieee1284_read_nibble,
  238. .byte_read_data = parport_ieee1284_read_byte,
  239. .owner = THIS_MODULE,
  240. };
  241. static int __devinit init_one_port(struct sbus_dev *sdev)
  242. {
  243. struct parport *p;
  244. /* at least in theory there may be a "we don't dma" case */
  245. struct parport_operations *ops;
  246. void __iomem *base;
  247. int irq, dma, err = 0, size;
  248. struct bpp_regs __iomem *regs;
  249. unsigned char value_tcr;
  250. irq = sdev->irqs[0];
  251. base = sbus_ioremap(&sdev->resource[0], 0,
  252. sdev->reg_addrs[0].reg_size,
  253. "sunbpp");
  254. if (!base)
  255. return -ENODEV;
  256. size = sdev->reg_addrs[0].reg_size;
  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 = &sdev->ofdev.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(&sdev->ofdev.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. sbus_iounmap(base, size);
  286. return err;
  287. }
  288. static int __devinit bpp_probe(struct of_device *dev, const struct of_device_id *match)
  289. {
  290. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  291. return init_one_port(sdev);
  292. }
  293. static int __devexit bpp_remove(struct of_device *dev)
  294. {
  295. struct parport *p = dev_get_drvdata(&dev->dev);
  296. struct parport_operations *ops = p->ops;
  297. parport_remove_port(p);
  298. if (p->irq != PARPORT_IRQ_NONE) {
  299. parport_sunbpp_disable_irq(p);
  300. free_irq(p->irq, p);
  301. }
  302. sbus_iounmap((void __iomem *) p->base, p->size);
  303. parport_put_port(p);
  304. kfree(ops);
  305. dev_set_drvdata(&dev->dev, NULL);
  306. return 0;
  307. }
  308. static struct of_device_id bpp_match[] = {
  309. {
  310. .name = "SUNW,bpp",
  311. },
  312. {},
  313. };
  314. MODULE_DEVICE_TABLE(of, bpp_match);
  315. static struct of_platform_driver bpp_sbus_driver = {
  316. .name = "bpp",
  317. .match_table = bpp_match,
  318. .probe = bpp_probe,
  319. .remove = __devexit_p(bpp_remove),
  320. };
  321. static int __init parport_sunbpp_init(void)
  322. {
  323. return of_register_driver(&bpp_sbus_driver, &sbus_bus_type);
  324. }
  325. static void __exit parport_sunbpp_exit(void)
  326. {
  327. of_unregister_driver(&bpp_sbus_driver);
  328. }
  329. MODULE_AUTHOR("Derrick J Brashear");
  330. MODULE_DESCRIPTION("Parport Driver for Sparc bidirectional Port");
  331. MODULE_SUPPORTED_DEVICE("Sparc Bidirectional Parallel Port");
  332. MODULE_VERSION("2.0");
  333. MODULE_LICENSE("GPL");
  334. module_init(parport_sunbpp_init)
  335. module_exit(parport_sunbpp_exit)