parport_sunbpp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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", value_tcr, value_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", value_tcr, value_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",
  143. value_tcr, value_or));
  144. if (mask & PARPORT_CONTROL_STROBE) {
  145. if (val & PARPORT_CONTROL_STROBE) {
  146. value_tcr &= ~P_TCR_DS;
  147. } else {
  148. value_tcr |= P_TCR_DS;
  149. }
  150. }
  151. if (mask & PARPORT_CONTROL_AUTOFD) {
  152. if (val & PARPORT_CONTROL_AUTOFD) {
  153. value_or &= ~P_OR_AFXN;
  154. } else {
  155. value_or |= P_OR_AFXN;
  156. }
  157. }
  158. if (mask & PARPORT_CONTROL_INIT) {
  159. if (val & PARPORT_CONTROL_INIT) {
  160. value_or &= ~P_OR_INIT;
  161. } else {
  162. value_or |= P_OR_INIT;
  163. }
  164. }
  165. if (mask & PARPORT_CONTROL_SELECT) {
  166. if (val & PARPORT_CONTROL_SELECT) {
  167. value_or |= P_OR_SLCT_IN;
  168. } else {
  169. value_or &= ~P_OR_SLCT_IN;
  170. }
  171. }
  172. sbus_writeb(value_or, &regs->p_or);
  173. sbus_writeb(value_tcr, &regs->p_tcr);
  174. dprintk((KERN_DEBUG "frob2: tcr 0x%x or 0x%x\n",
  175. value_tcr, value_or));
  176. return parport_sunbpp_read_control(p);
  177. }
  178. static void parport_sunbpp_write_control(struct parport *p, unsigned char d)
  179. {
  180. const unsigned char wm = (PARPORT_CONTROL_STROBE |
  181. PARPORT_CONTROL_AUTOFD |
  182. PARPORT_CONTROL_INIT |
  183. PARPORT_CONTROL_SELECT);
  184. parport_sunbpp_frob_control (p, wm, d & wm);
  185. }
  186. static unsigned char parport_sunbpp_read_status(struct parport *p)
  187. {
  188. return status_sunbpp_to_pc(p);
  189. }
  190. static void parport_sunbpp_data_forward (struct parport *p)
  191. {
  192. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  193. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  194. dprintk((KERN_DEBUG "forward\n"));
  195. value_tcr &= ~P_TCR_DIR;
  196. sbus_writeb(value_tcr, &regs->p_tcr);
  197. }
  198. static void parport_sunbpp_data_reverse (struct parport *p)
  199. {
  200. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  201. u8 val = sbus_readb(&regs->p_tcr);
  202. dprintk((KERN_DEBUG "reverse\n"));
  203. val |= P_TCR_DIR;
  204. sbus_writeb(val, &regs->p_tcr);
  205. }
  206. static void parport_sunbpp_init_state(struct pardevice *dev, struct parport_state *s)
  207. {
  208. s->u.pc.ctr = 0xc;
  209. s->u.pc.ecr = 0x0;
  210. }
  211. static void parport_sunbpp_save_state(struct parport *p, struct parport_state *s)
  212. {
  213. s->u.pc.ctr = parport_sunbpp_read_control(p);
  214. }
  215. static void parport_sunbpp_restore_state(struct parport *p, struct parport_state *s)
  216. {
  217. parport_sunbpp_write_control(p, s->u.pc.ctr);
  218. }
  219. static struct parport_operations parport_sunbpp_ops =
  220. {
  221. .write_data = parport_sunbpp_write_data,
  222. .read_data = parport_sunbpp_read_data,
  223. .write_control = parport_sunbpp_write_control,
  224. .read_control = parport_sunbpp_read_control,
  225. .frob_control = parport_sunbpp_frob_control,
  226. .read_status = parport_sunbpp_read_status,
  227. .enable_irq = parport_sunbpp_enable_irq,
  228. .disable_irq = parport_sunbpp_disable_irq,
  229. .data_forward = parport_sunbpp_data_forward,
  230. .data_reverse = parport_sunbpp_data_reverse,
  231. .init_state = parport_sunbpp_init_state,
  232. .save_state = parport_sunbpp_save_state,
  233. .restore_state = parport_sunbpp_restore_state,
  234. .epp_write_data = parport_ieee1284_epp_write_data,
  235. .epp_read_data = parport_ieee1284_epp_read_data,
  236. .epp_write_addr = parport_ieee1284_epp_write_addr,
  237. .epp_read_addr = parport_ieee1284_epp_read_addr,
  238. .ecp_write_data = parport_ieee1284_ecp_write_data,
  239. .ecp_read_data = parport_ieee1284_ecp_read_data,
  240. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  241. .compat_write_data = parport_ieee1284_write_compat,
  242. .nibble_read_data = parport_ieee1284_read_nibble,
  243. .byte_read_data = parport_ieee1284_read_byte,
  244. .owner = THIS_MODULE,
  245. };
  246. static int __devinit init_one_port(struct sbus_dev *sdev)
  247. {
  248. struct parport *p;
  249. /* at least in theory there may be a "we don't dma" case */
  250. struct parport_operations *ops;
  251. void __iomem *base;
  252. int irq, dma, err = 0, size;
  253. struct bpp_regs __iomem *regs;
  254. unsigned char value_tcr;
  255. irq = sdev->irqs[0];
  256. base = sbus_ioremap(&sdev->resource[0], 0,
  257. sdev->reg_addrs[0].reg_size,
  258. "sunbpp");
  259. if (!base)
  260. return -ENODEV;
  261. size = sdev->reg_addrs[0].reg_size;
  262. dma = PARPORT_DMA_NONE;
  263. ops = kmalloc(sizeof(struct parport_operations), GFP_KERNEL);
  264. if (!ops)
  265. goto out_unmap;
  266. memcpy (ops, &parport_sunbpp_ops, sizeof (struct parport_operations));
  267. dprintk(("register_port\n"));
  268. if (!(p = parport_register_port((unsigned long)base, irq, dma, ops)))
  269. goto out_free_ops;
  270. p->size = size;
  271. if ((err = request_irq(p->irq, parport_sunbpp_interrupt,
  272. IRQF_SHARED, p->name, p)) != 0) {
  273. goto out_put_port;
  274. }
  275. parport_sunbpp_enable_irq(p);
  276. regs = (struct bpp_regs __iomem *)p->base;
  277. value_tcr = sbus_readb(&regs->p_tcr);
  278. value_tcr &= ~P_TCR_DIR;
  279. sbus_writeb(value_tcr, &regs->p_tcr);
  280. printk(KERN_INFO "%s: sunbpp at 0x%lx\n", p->name, p->base);
  281. dev_set_drvdata(&sdev->ofdev.dev, p);
  282. parport_announce_port(p);
  283. return 0;
  284. out_put_port:
  285. parport_put_port(p);
  286. out_free_ops:
  287. kfree(ops);
  288. out_unmap:
  289. sbus_iounmap(base, size);
  290. return err;
  291. }
  292. static int __devinit bpp_probe(struct of_device *dev, const struct of_device_id *match)
  293. {
  294. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  295. return init_one_port(sdev);
  296. }
  297. static int __devexit bpp_remove(struct of_device *dev)
  298. {
  299. struct parport *p = dev_get_drvdata(&dev->dev);
  300. struct parport_operations *ops = p->ops;
  301. parport_remove_port(p);
  302. if (p->irq != PARPORT_IRQ_NONE) {
  303. parport_sunbpp_disable_irq(p);
  304. free_irq(p->irq, p);
  305. }
  306. sbus_iounmap((void __iomem *) p->base, p->size);
  307. parport_put_port(p);
  308. kfree(ops);
  309. dev_set_drvdata(&dev->dev, NULL);
  310. return 0;
  311. }
  312. static struct of_device_id bpp_match[] = {
  313. {
  314. .name = "SUNW,bpp",
  315. },
  316. {},
  317. };
  318. MODULE_DEVICE_TABLE(of, bpp_match);
  319. static struct of_platform_driver bpp_sbus_driver = {
  320. .name = "bpp",
  321. .match_table = bpp_match,
  322. .probe = bpp_probe,
  323. .remove = __devexit_p(bpp_remove),
  324. };
  325. static int __init parport_sunbpp_init(void)
  326. {
  327. return of_register_driver(&bpp_sbus_driver, &sbus_bus_type);
  328. }
  329. static void __exit parport_sunbpp_exit(void)
  330. {
  331. of_unregister_driver(&bpp_sbus_driver);
  332. }
  333. MODULE_AUTHOR("Derrick J Brashear");
  334. MODULE_DESCRIPTION("Parport Driver for Sparc bidirectional Port");
  335. MODULE_SUPPORTED_DEVICE("Sparc Bidirectional Parallel Port");
  336. MODULE_VERSION("2.0");
  337. MODULE_LICENSE("GPL");
  338. module_init(parport_sunbpp_init)
  339. module_exit(parport_sunbpp_exit)