parport_sunbpp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* $Id: parport_sunbpp.c,v 1.12 2001/05/26 03:01:42 davem Exp $
  2. * Parallel-port routines for Sun architecture
  3. *
  4. * Author: Derrick J. Brashear <shadow@dementia.org>
  5. *
  6. * based on work by:
  7. * Phil Blundell <philb@gnu.org>
  8. * Tim Waugh <tim@cyberelk.demon.co.uk>
  9. * Jose Renau <renau@acm.org>
  10. * David Campbell <campbell@tirian.che.curtin.edu.au>
  11. * Grant Guenther <grant@torque.net>
  12. * Eddie C. Dost <ecd@skynet.be>
  13. * Stephen Williams (steve@icarus.com)
  14. * Gus Baldauf (gbaldauf@ix.netcom.com)
  15. * Peter Zaitcev
  16. * Tom Dyas
  17. */
  18. #include <linux/string.h>
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/parport.h>
  27. #include <asm/ptrace.h>
  28. #include <linux/interrupt.h>
  29. #include <asm/io.h>
  30. #include <asm/oplib.h> /* OpenProm Library */
  31. #include <asm/sbus.h>
  32. #include <asm/dma.h> /* BPP uses LSI 64854 for DMA */
  33. #include <asm/irq.h>
  34. #include <asm/sunbpp.h>
  35. #undef __SUNBPP_DEBUG
  36. #ifdef __SUNBPP_DEBUG
  37. #define dprintk(x) printk x
  38. #else
  39. #define dprintk(x)
  40. #endif
  41. static irqreturn_t parport_sunbpp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  42. {
  43. parport_generic_irq(irq, (struct parport *) dev_id, regs);
  44. return IRQ_HANDLED;
  45. }
  46. static void parport_sunbpp_disable_irq(struct parport *p)
  47. {
  48. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  49. u32 tmp;
  50. tmp = sbus_readl(&regs->p_csr);
  51. tmp &= ~DMA_INT_ENAB;
  52. sbus_writel(tmp, &regs->p_csr);
  53. }
  54. static void parport_sunbpp_enable_irq(struct parport *p)
  55. {
  56. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  57. u32 tmp;
  58. tmp = sbus_readl(&regs->p_csr);
  59. tmp |= DMA_INT_ENAB;
  60. sbus_writel(tmp, &regs->p_csr);
  61. }
  62. static void parport_sunbpp_write_data(struct parport *p, unsigned char d)
  63. {
  64. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  65. sbus_writeb(d, &regs->p_dr);
  66. dprintk((KERN_DEBUG "wrote 0x%x\n", d));
  67. }
  68. static unsigned char parport_sunbpp_read_data(struct parport *p)
  69. {
  70. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  71. return sbus_readb(&regs->p_dr);
  72. }
  73. #if 0
  74. static void control_pc_to_sunbpp(struct parport *p, unsigned char status)
  75. {
  76. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  77. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  78. unsigned char value_or = sbus_readb(&regs->p_or);
  79. if (status & PARPORT_CONTROL_STROBE)
  80. value_tcr |= P_TCR_DS;
  81. if (status & PARPORT_CONTROL_AUTOFD)
  82. value_or |= P_OR_AFXN;
  83. if (status & PARPORT_CONTROL_INIT)
  84. value_or |= P_OR_INIT;
  85. if (status & PARPORT_CONTROL_SELECT)
  86. value_or |= P_OR_SLCT_IN;
  87. sbus_writeb(value_or, &regs->p_or);
  88. sbus_writeb(value_tcr, &regs->p_tcr);
  89. }
  90. #endif
  91. static unsigned char status_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_ir = sbus_readb(&regs->p_ir);
  97. if (!(value_ir & P_IR_ERR))
  98. bits |= PARPORT_STATUS_ERROR;
  99. if (!(value_ir & P_IR_SLCT))
  100. bits |= PARPORT_STATUS_SELECT;
  101. if (!(value_ir & P_IR_PE))
  102. bits |= PARPORT_STATUS_PAPEROUT;
  103. if (value_tcr & P_TCR_ACK)
  104. bits |= PARPORT_STATUS_ACK;
  105. if (!(value_tcr & P_TCR_BUSY))
  106. bits |= PARPORT_STATUS_BUSY;
  107. dprintk((KERN_DEBUG "tcr 0x%x ir 0x%x\n", regs->p_tcr, regs->p_ir));
  108. dprintk((KERN_DEBUG "read status 0x%x\n", bits));
  109. return bits;
  110. }
  111. static unsigned char control_sunbpp_to_pc(struct parport *p)
  112. {
  113. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  114. unsigned char bits = 0;
  115. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  116. unsigned char value_or = sbus_readb(&regs->p_or);
  117. if (!(value_tcr & P_TCR_DS))
  118. bits |= PARPORT_CONTROL_STROBE;
  119. if (!(value_or & P_OR_AFXN))
  120. bits |= PARPORT_CONTROL_AUTOFD;
  121. if (!(value_or & P_OR_INIT))
  122. bits |= PARPORT_CONTROL_INIT;
  123. if (value_or & P_OR_SLCT_IN)
  124. bits |= PARPORT_CONTROL_SELECT;
  125. dprintk((KERN_DEBUG "tcr 0x%x or 0x%x\n", regs->p_tcr, regs->p_or));
  126. dprintk((KERN_DEBUG "read control 0x%x\n", bits));
  127. return bits;
  128. }
  129. static unsigned char parport_sunbpp_read_control(struct parport *p)
  130. {
  131. return control_sunbpp_to_pc(p);
  132. }
  133. static unsigned char parport_sunbpp_frob_control(struct parport *p,
  134. unsigned char mask,
  135. unsigned char val)
  136. {
  137. struct bpp_regs __iomem *regs = (struct bpp_regs __iomem *)p->base;
  138. unsigned char value_tcr = sbus_readb(&regs->p_tcr);
  139. unsigned char value_or = sbus_readb(&regs->p_or);
  140. dprintk((KERN_DEBUG "frob1: tcr 0x%x or 0x%x\n", regs->p_tcr, regs->p_or));
  141. if (mask & PARPORT_CONTROL_STROBE) {
  142. if (val & PARPORT_CONTROL_STROBE) {
  143. value_tcr &= ~P_TCR_DS;
  144. } else {
  145. value_tcr |= P_TCR_DS;
  146. }
  147. }
  148. if (mask & PARPORT_CONTROL_AUTOFD) {
  149. if (val & PARPORT_CONTROL_AUTOFD) {
  150. value_or &= ~P_OR_AFXN;
  151. } else {
  152. value_or |= P_OR_AFXN;
  153. }
  154. }
  155. if (mask & PARPORT_CONTROL_INIT) {
  156. if (val & PARPORT_CONTROL_INIT) {
  157. value_or &= ~P_OR_INIT;
  158. } else {
  159. value_or |= P_OR_INIT;
  160. }
  161. }
  162. if (mask & PARPORT_CONTROL_SELECT) {
  163. if (val & PARPORT_CONTROL_SELECT) {
  164. value_or |= P_OR_SLCT_IN;
  165. } else {
  166. value_or &= ~P_OR_SLCT_IN;
  167. }
  168. }
  169. sbus_writeb(value_or, &regs->p_or);
  170. sbus_writeb(value_tcr, &regs->p_tcr);
  171. dprintk((KERN_DEBUG "frob2: tcr 0x%x or 0x%x\n", regs->p_tcr, regs->p_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. typedef struct {
  243. struct list_head list;
  244. struct parport *port;
  245. } Node;
  246. /* no locks, everything's serialized */
  247. static LIST_HEAD(port_list);
  248. static int __init init_one_port(struct sbus_dev *sdev)
  249. {
  250. struct parport *p;
  251. /* at least in theory there may be a "we don't dma" case */
  252. struct parport_operations *ops;
  253. void __iomem *base;
  254. int irq, dma, err = 0, size;
  255. struct bpp_regs __iomem *regs;
  256. unsigned char value_tcr;
  257. Node *node;
  258. dprintk((KERN_DEBUG "init_one_port(%p): ranges, alloc_io, ", sdev));
  259. node = kmalloc(sizeof(Node), GFP_KERNEL);
  260. if (!node)
  261. goto out0;
  262. irq = sdev->irqs[0];
  263. base = sbus_ioremap(&sdev->resource[0], 0,
  264. sdev->reg_addrs[0].reg_size,
  265. "sunbpp");
  266. if (!base)
  267. goto out1;
  268. size = sdev->reg_addrs[0].reg_size;
  269. dma = PARPORT_DMA_NONE;
  270. dprintk(("alloc(ppops), "));
  271. ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
  272. if (!ops)
  273. goto out2;
  274. memcpy (ops, &parport_sunbpp_ops, sizeof (struct parport_operations));
  275. dprintk(("register_port\n"));
  276. if (!(p = parport_register_port((unsigned long)base, irq, dma, ops)))
  277. goto out3;
  278. p->size = size;
  279. dprintk((KERN_DEBUG "init_one_port: request_irq(%08x:%p:%x:%s:%p) ",
  280. p->irq, parport_sunbpp_interrupt, SA_SHIRQ, p->name, p));
  281. if ((err = request_irq(p->irq, parport_sunbpp_interrupt,
  282. SA_SHIRQ, p->name, p)) != 0) {
  283. dprintk(("ERROR %d\n", err));
  284. goto out4;
  285. }
  286. dprintk(("OK\n"));
  287. parport_sunbpp_enable_irq(p);
  288. regs = (struct bpp_regs __iomem *)p->base;
  289. dprintk((KERN_DEBUG "forward\n"));
  290. value_tcr = sbus_readb(&regs->p_tcr);
  291. value_tcr &= ~P_TCR_DIR;
  292. sbus_writeb(value_tcr, &regs->p_tcr);
  293. printk(KERN_INFO "%s: sunbpp at 0x%lx\n", p->name, p->base);
  294. node->port = p;
  295. list_add(&node->list, &port_list);
  296. parport_announce_port (p);
  297. return 1;
  298. out4:
  299. parport_put_port(p);
  300. out3:
  301. kfree(ops);
  302. out2:
  303. sbus_iounmap(base, size);
  304. out1:
  305. kfree(node);
  306. out0:
  307. return err;
  308. }
  309. static int __init parport_sunbpp_init(void)
  310. {
  311. struct sbus_bus *sbus;
  312. struct sbus_dev *sdev;
  313. int count = 0;
  314. for_each_sbus(sbus) {
  315. for_each_sbusdev(sdev, sbus) {
  316. if (!strcmp(sdev->prom_name, "SUNW,bpp"))
  317. count += init_one_port(sdev);
  318. }
  319. }
  320. return count ? 0 : -ENODEV;
  321. }
  322. static void __exit parport_sunbpp_exit(void)
  323. {
  324. while (!list_empty(&port_list)) {
  325. Node *node = list_entry(port_list.next, Node, list);
  326. struct parport *p = node->port;
  327. struct parport_operations *ops = p->ops;
  328. parport_remove_port(p);
  329. if (p->irq != PARPORT_IRQ_NONE) {
  330. parport_sunbpp_disable_irq(p);
  331. free_irq(p->irq, p);
  332. }
  333. sbus_iounmap((void __iomem *)p->base, p->size);
  334. parport_put_port(p);
  335. kfree (ops);
  336. list_del(&node->list);
  337. kfree (node);
  338. }
  339. }
  340. MODULE_AUTHOR("Derrick J Brashear");
  341. MODULE_DESCRIPTION("Parport Driver for Sparc bidirectional Port");
  342. MODULE_SUPPORTED_DEVICE("Sparc Bidirectional Parallel Port");
  343. MODULE_LICENSE("GPL");
  344. module_init(parport_sunbpp_init)
  345. module_exit(parport_sunbpp_exit)