parport_sunbpp.c 10 KB

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