/drivers/parport/parport_mfc3.c

http://github.com/mirrors/linux · C · 371 lines · 255 code · 57 blank · 59 comment · 32 complexity · 4344c0d6ced992a2389c761ea103dbc1 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Low-level parallel port routines for the Multiface 3 card
  3. *
  4. * Author: Joerg Dorchain <joerg@dorchain.net>
  5. *
  6. * (C) The elitist m68k Users(TM)
  7. *
  8. * based on the existing parport_amiga and lp_mfc
  9. *
  10. *
  11. * From the MFC3 documentation:
  12. *
  13. * Miscellaneous PIA Details
  14. * -------------------------
  15. *
  16. * The two open-drain interrupt outputs /IRQA and /IRQB are routed to
  17. * /INT2 of the Z2 bus.
  18. *
  19. * The CPU data bus of the PIA (D0-D7) is connected to D8-D15 on the Z2
  20. * bus. This means that any PIA registers are accessed at even addresses.
  21. *
  22. * Centronics Pin Connections for the PIA
  23. * --------------------------------------
  24. *
  25. * The following table shows the connections between the PIA and the
  26. * Centronics interface connector. These connections implement a single, but
  27. * very complete, Centronics type interface. The Pin column gives the pin
  28. * numbers of the PIA. The Centronics pin numbers can be found in the section
  29. * "Parallel Connectors".
  30. *
  31. *
  32. * Pin | PIA | Dir | Centronics Names
  33. * -------+-----+-----+---------------------------------------------------------
  34. * 19 | CB2 | --> | /STROBE (aka /DRDY)
  35. * 10-17 | PBx | <-> | DATA0 - DATA7
  36. * 18 | CB1 | <-- | /ACK
  37. * 40 | CA1 | <-- | BUSY
  38. * 3 | PA1 | <-- | PAPER-OUT (aka POUT)
  39. * 4 | PA2 | <-- | SELECTED (aka SEL)
  40. * 9 | PA7 | --> | /INIT (aka /RESET or /INPUT-PRIME)
  41. * 6 | PA4 | <-- | /ERROR (aka /FAULT)
  42. * 7 | PA5 | --> | DIR (aka /SELECT-IN)
  43. * 8 | PA6 | --> | /AUTO-FEED-XT
  44. * 39 | CA2 | --> | open
  45. * 5 | PA3 | <-- | /ACK (same as CB1!)
  46. * 2 | PA0 | <-- | BUSY (same as CA1!)
  47. * -------+-----+-----+---------------------------------------------------------
  48. *
  49. * Should be enough to understand some of the driver.
  50. *
  51. * Per convention for normal use the port registers are visible.
  52. * If you need the data direction registers, restore the value in the
  53. * control register.
  54. */
  55. #include "multiface.h"
  56. #include <linux/module.h>
  57. #include <linux/init.h>
  58. #include <linux/parport.h>
  59. #include <linux/delay.h>
  60. #include <linux/mc6821.h>
  61. #include <linux/zorro.h>
  62. #include <linux/interrupt.h>
  63. #include <asm/setup.h>
  64. #include <asm/amigahw.h>
  65. #include <asm/irq.h>
  66. #include <asm/amigaints.h>
  67. /* Maximum Number of Cards supported */
  68. #define MAX_MFC 5
  69. #undef DEBUG
  70. #ifdef DEBUG
  71. #define DPRINTK printk
  72. #else
  73. static inline int DPRINTK(void *nothing, ...) {return 0;}
  74. #endif
  75. static struct parport *this_port[MAX_MFC] = {NULL, };
  76. static volatile int dummy; /* for trigger readds */
  77. #define pia(dev) ((struct pia *)(dev->base))
  78. static struct parport_operations pp_mfc3_ops;
  79. static void mfc3_write_data(struct parport *p, unsigned char data)
  80. {
  81. DPRINTK(KERN_DEBUG "write_data %c\n",data);
  82. dummy = pia(p)->pprb; /* clears irq bit */
  83. /* Triggers also /STROBE.*/
  84. pia(p)->pprb = data;
  85. }
  86. static unsigned char mfc3_read_data(struct parport *p)
  87. {
  88. /* clears interrupt bit. Triggers also /STROBE. */
  89. return pia(p)->pprb;
  90. }
  91. static unsigned char control_pc_to_mfc3(unsigned char control)
  92. {
  93. unsigned char ret = 32|64;
  94. if (control & PARPORT_CONTROL_SELECT) /* XXX: What is SELECP? */
  95. ret &= ~32; /* /SELECT_IN */
  96. if (control & PARPORT_CONTROL_INIT) /* INITP */
  97. ret |= 128;
  98. if (control & PARPORT_CONTROL_AUTOFD) /* AUTOLF */
  99. ret &= ~64;
  100. if (control & PARPORT_CONTROL_STROBE) /* Strobe */
  101. /* Handled directly by hardware */;
  102. return ret;
  103. }
  104. static unsigned char control_mfc3_to_pc(unsigned char control)
  105. {
  106. unsigned char ret = PARPORT_CONTROL_STROBE
  107. | PARPORT_CONTROL_AUTOFD | PARPORT_CONTROL_SELECT;
  108. if (control & 128) /* /INITP */
  109. ret |= PARPORT_CONTROL_INIT;
  110. if (control & 64) /* /AUTOLF */
  111. ret &= ~PARPORT_CONTROL_AUTOFD;
  112. if (control & 32) /* /SELECT_IN */
  113. ret &= ~PARPORT_CONTROL_SELECT;
  114. return ret;
  115. }
  116. static void mfc3_write_control(struct parport *p, unsigned char control)
  117. {
  118. DPRINTK(KERN_DEBUG "write_control %02x\n",control);
  119. pia(p)->ppra = (pia(p)->ppra & 0x1f) | control_pc_to_mfc3(control);
  120. }
  121. static unsigned char mfc3_read_control( struct parport *p)
  122. {
  123. DPRINTK(KERN_DEBUG "read_control \n");
  124. return control_mfc3_to_pc(pia(p)->ppra & 0xe0);
  125. }
  126. static unsigned char mfc3_frob_control( struct parport *p, unsigned char mask, unsigned char val)
  127. {
  128. unsigned char old;
  129. DPRINTK(KERN_DEBUG "frob_control mask %02x, value %02x\n",mask,val);
  130. old = mfc3_read_control(p);
  131. mfc3_write_control(p, (old & ~mask) ^ val);
  132. return old;
  133. }
  134. static unsigned char status_mfc3_to_pc(unsigned char status)
  135. {
  136. unsigned char ret = PARPORT_STATUS_BUSY;
  137. if (status & 1) /* Busy */
  138. ret &= ~PARPORT_STATUS_BUSY;
  139. if (status & 2) /* PaperOut */
  140. ret |= PARPORT_STATUS_PAPEROUT;
  141. if (status & 4) /* Selected */
  142. ret |= PARPORT_STATUS_SELECT;
  143. if (status & 8) /* Ack */
  144. ret |= PARPORT_STATUS_ACK;
  145. if (status & 16) /* /ERROR */
  146. ret |= PARPORT_STATUS_ERROR;
  147. return ret;
  148. }
  149. static unsigned char mfc3_read_status(struct parport *p)
  150. {
  151. unsigned char status;
  152. status = status_mfc3_to_pc(pia(p)->ppra & 0x1f);
  153. DPRINTK(KERN_DEBUG "read_status %02x\n", status);
  154. return status;
  155. }
  156. static int use_cnt;
  157. static irqreturn_t mfc3_interrupt(int irq, void *dev_id)
  158. {
  159. int i;
  160. for( i = 0; i < MAX_MFC; i++)
  161. if (this_port[i] != NULL)
  162. if (pia(this_port[i])->crb & 128) { /* Board caused interrupt */
  163. dummy = pia(this_port[i])->pprb; /* clear irq bit */
  164. parport_generic_irq(this_port[i]);
  165. }
  166. return IRQ_HANDLED;
  167. }
  168. static void mfc3_enable_irq(struct parport *p)
  169. {
  170. pia(p)->crb |= PIA_C1_ENABLE_IRQ;
  171. }
  172. static void mfc3_disable_irq(struct parport *p)
  173. {
  174. pia(p)->crb &= ~PIA_C1_ENABLE_IRQ;
  175. }
  176. static void mfc3_data_forward(struct parport *p)
  177. {
  178. DPRINTK(KERN_DEBUG "forward\n");
  179. pia(p)->crb &= ~PIA_DDR; /* make data direction register visible */
  180. pia(p)->pddrb = 255; /* all pins output */
  181. pia(p)->crb |= PIA_DDR; /* make data register visible - default */
  182. }
  183. static void mfc3_data_reverse(struct parport *p)
  184. {
  185. DPRINTK(KERN_DEBUG "reverse\n");
  186. pia(p)->crb &= ~PIA_DDR; /* make data direction register visible */
  187. pia(p)->pddrb = 0; /* all pins input */
  188. pia(p)->crb |= PIA_DDR; /* make data register visible - default */
  189. }
  190. static void mfc3_init_state(struct pardevice *dev, struct parport_state *s)
  191. {
  192. s->u.amiga.data = 0;
  193. s->u.amiga.datadir = 255;
  194. s->u.amiga.status = 0;
  195. s->u.amiga.statusdir = 0xe0;
  196. }
  197. static void mfc3_save_state(struct parport *p, struct parport_state *s)
  198. {
  199. s->u.amiga.data = pia(p)->pprb;
  200. pia(p)->crb &= ~PIA_DDR;
  201. s->u.amiga.datadir = pia(p)->pddrb;
  202. pia(p)->crb |= PIA_DDR;
  203. s->u.amiga.status = pia(p)->ppra;
  204. pia(p)->cra &= ~PIA_DDR;
  205. s->u.amiga.statusdir = pia(p)->pddrb;
  206. pia(p)->cra |= PIA_DDR;
  207. }
  208. static void mfc3_restore_state(struct parport *p, struct parport_state *s)
  209. {
  210. pia(p)->pprb = s->u.amiga.data;
  211. pia(p)->crb &= ~PIA_DDR;
  212. pia(p)->pddrb = s->u.amiga.datadir;
  213. pia(p)->crb |= PIA_DDR;
  214. pia(p)->ppra = s->u.amiga.status;
  215. pia(p)->cra &= ~PIA_DDR;
  216. pia(p)->pddrb = s->u.amiga.statusdir;
  217. pia(p)->cra |= PIA_DDR;
  218. }
  219. static struct parport_operations pp_mfc3_ops = {
  220. .write_data = mfc3_write_data,
  221. .read_data = mfc3_read_data,
  222. .write_control = mfc3_write_control,
  223. .read_control = mfc3_read_control,
  224. .frob_control = mfc3_frob_control,
  225. .read_status = mfc3_read_status,
  226. .enable_irq = mfc3_enable_irq,
  227. .disable_irq = mfc3_disable_irq,
  228. .data_forward = mfc3_data_forward,
  229. .data_reverse = mfc3_data_reverse,
  230. .init_state = mfc3_init_state,
  231. .save_state = mfc3_save_state,
  232. .restore_state = mfc3_restore_state,
  233. .epp_write_data = parport_ieee1284_epp_write_data,
  234. .epp_read_data = parport_ieee1284_epp_read_data,
  235. .epp_write_addr = parport_ieee1284_epp_write_addr,
  236. .epp_read_addr = parport_ieee1284_epp_read_addr,
  237. .ecp_write_data = parport_ieee1284_ecp_write_data,
  238. .ecp_read_data = parport_ieee1284_ecp_read_data,
  239. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  240. .compat_write_data = parport_ieee1284_write_compat,
  241. .nibble_read_data = parport_ieee1284_read_nibble,
  242. .byte_read_data = parport_ieee1284_read_byte,
  243. .owner = THIS_MODULE,
  244. };
  245. /* ----------- Initialisation code --------------------------------- */
  246. static int __init parport_mfc3_init(void)
  247. {
  248. struct parport *p;
  249. int pias = 0;
  250. struct pia *pp;
  251. struct zorro_dev *z = NULL;
  252. if (!MACH_IS_AMIGA)
  253. return -ENODEV;
  254. while ((z = zorro_find_device(ZORRO_PROD_BSC_MULTIFACE_III, z))) {
  255. unsigned long piabase = z->resource.start+PIABASE;
  256. if (!request_mem_region(piabase, sizeof(struct pia), "PIA"))
  257. continue;
  258. pp = ZTWO_VADDR(piabase);
  259. pp->crb = 0;
  260. pp->pddrb = 255; /* all data pins output */
  261. pp->crb = PIA_DDR|32|8;
  262. dummy = pp->pddrb; /* reading clears interrupt */
  263. pp->cra = 0;
  264. pp->pddra = 0xe0; /* /RESET, /DIR ,/AUTO-FEED output */
  265. pp->cra = PIA_DDR;
  266. pp->ppra = 0; /* reset printer */
  267. udelay(10);
  268. pp->ppra = 128;
  269. p = parport_register_port((unsigned long)pp, IRQ_AMIGA_PORTS,
  270. PARPORT_DMA_NONE, &pp_mfc3_ops);
  271. if (!p)
  272. goto out_port;
  273. if (p->irq != PARPORT_IRQ_NONE) {
  274. if (use_cnt++ == 0)
  275. if (request_irq(IRQ_AMIGA_PORTS, mfc3_interrupt, IRQF_SHARED, p->name, &pp_mfc3_ops))
  276. goto out_irq;
  277. }
  278. p->dev = &z->dev;
  279. this_port[pias++] = p;
  280. printk(KERN_INFO "%s: Multiface III port using irq\n", p->name);
  281. /* XXX: set operating mode */
  282. p->private_data = (void *)piabase;
  283. parport_announce_port (p);
  284. if (pias >= MAX_MFC)
  285. break;
  286. continue;
  287. out_irq:
  288. parport_put_port(p);
  289. out_port:
  290. release_mem_region(piabase, sizeof(struct pia));
  291. }
  292. return pias ? 0 : -ENODEV;
  293. }
  294. static void __exit parport_mfc3_exit(void)
  295. {
  296. int i;
  297. for (i = 0; i < MAX_MFC; i++) {
  298. if (!this_port[i])
  299. continue;
  300. parport_remove_port(this_port[i]);
  301. if (this_port[i]->irq != PARPORT_IRQ_NONE) {
  302. if (--use_cnt == 0)
  303. free_irq(IRQ_AMIGA_PORTS, &pp_mfc3_ops);
  304. }
  305. release_mem_region(ZTWO_PADDR(this_port[i]->private_data), sizeof(struct pia));
  306. parport_put_port(this_port[i]);
  307. }
  308. }
  309. MODULE_AUTHOR("Joerg Dorchain <joerg@dorchain.net>");
  310. MODULE_DESCRIPTION("Parport Driver for Multiface 3 expansion cards Parallel Port");
  311. MODULE_SUPPORTED_DEVICE("Multiface 3 Parallel Port");
  312. MODULE_LICENSE("GPL");
  313. module_init(parport_mfc3_init)
  314. module_exit(parport_mfc3_exit)