parport_mfc3.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Low-level parallel port routines for the Multiface 3 card
  3. *
  4. * Author: Joerg Dorchain <[email protected]>
  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. static struct parport *this_port[MAX_MFC] = {NULL, };
  71. static volatile int dummy; /* for trigger readds */
  72. #define pia(dev) ((struct pia *)(dev->base))
  73. static struct parport_operations pp_mfc3_ops;
  74. static void mfc3_write_data(struct parport *p, unsigned char data)
  75. {
  76. pr_debug("write_data %c\n", data);
  77. dummy = pia(p)->pprb; /* clears irq bit */
  78. /* Triggers also /STROBE.*/
  79. pia(p)->pprb = data;
  80. }
  81. static unsigned char mfc3_read_data(struct parport *p)
  82. {
  83. /* clears interrupt bit. Triggers also /STROBE. */
  84. return pia(p)->pprb;
  85. }
  86. static unsigned char control_pc_to_mfc3(unsigned char control)
  87. {
  88. unsigned char ret = 32|64;
  89. if (control & PARPORT_CONTROL_SELECT) /* XXX: What is SELECP? */
  90. ret &= ~32; /* /SELECT_IN */
  91. if (control & PARPORT_CONTROL_INIT) /* INITP */
  92. ret |= 128;
  93. if (control & PARPORT_CONTROL_AUTOFD) /* AUTOLF */
  94. ret &= ~64;
  95. if (control & PARPORT_CONTROL_STROBE) /* Strobe */
  96. /* Handled directly by hardware */;
  97. return ret;
  98. }
  99. static unsigned char control_mfc3_to_pc(unsigned char control)
  100. {
  101. unsigned char ret = PARPORT_CONTROL_STROBE
  102. | PARPORT_CONTROL_AUTOFD | PARPORT_CONTROL_SELECT;
  103. if (control & 128) /* /INITP */
  104. ret |= PARPORT_CONTROL_INIT;
  105. if (control & 64) /* /AUTOLF */
  106. ret &= ~PARPORT_CONTROL_AUTOFD;
  107. if (control & 32) /* /SELECT_IN */
  108. ret &= ~PARPORT_CONTROL_SELECT;
  109. return ret;
  110. }
  111. static void mfc3_write_control(struct parport *p, unsigned char control)
  112. {
  113. pr_debug("write_control %02x\n", control);
  114. pia(p)->ppra = (pia(p)->ppra & 0x1f) | control_pc_to_mfc3(control);
  115. }
  116. static unsigned char mfc3_read_control( struct parport *p)
  117. {
  118. pr_debug("read_control\n");
  119. return control_mfc3_to_pc(pia(p)->ppra & 0xe0);
  120. }
  121. static unsigned char mfc3_frob_control( struct parport *p, unsigned char mask, unsigned char val)
  122. {
  123. unsigned char old;
  124. pr_debug("frob_control mask %02x, value %02x\n", mask, val);
  125. old = mfc3_read_control(p);
  126. mfc3_write_control(p, (old & ~mask) ^ val);
  127. return old;
  128. }
  129. static unsigned char status_mfc3_to_pc(unsigned char status)
  130. {
  131. unsigned char ret = PARPORT_STATUS_BUSY;
  132. if (status & 1) /* Busy */
  133. ret &= ~PARPORT_STATUS_BUSY;
  134. if (status & 2) /* PaperOut */
  135. ret |= PARPORT_STATUS_PAPEROUT;
  136. if (status & 4) /* Selected */
  137. ret |= PARPORT_STATUS_SELECT;
  138. if (status & 8) /* Ack */
  139. ret |= PARPORT_STATUS_ACK;
  140. if (status & 16) /* /ERROR */
  141. ret |= PARPORT_STATUS_ERROR;
  142. return ret;
  143. }
  144. static unsigned char mfc3_read_status(struct parport *p)
  145. {
  146. unsigned char status;
  147. status = status_mfc3_to_pc(pia(p)->ppra & 0x1f);
  148. pr_debug("read_status %02x\n", status);
  149. return status;
  150. }
  151. static int use_cnt;
  152. static irqreturn_t mfc3_interrupt(int irq, void *dev_id)
  153. {
  154. int i;
  155. for( i = 0; i < MAX_MFC; i++)
  156. if (this_port[i] != NULL)
  157. if (pia(this_port[i])->crb & 128) { /* Board caused interrupt */
  158. dummy = pia(this_port[i])->pprb; /* clear irq bit */
  159. parport_generic_irq(this_port[i]);
  160. }
  161. return IRQ_HANDLED;
  162. }
  163. static void mfc3_enable_irq(struct parport *p)
  164. {
  165. pia(p)->crb |= PIA_C1_ENABLE_IRQ;
  166. }
  167. static void mfc3_disable_irq(struct parport *p)
  168. {
  169. pia(p)->crb &= ~PIA_C1_ENABLE_IRQ;
  170. }
  171. static void mfc3_data_forward(struct parport *p)
  172. {
  173. pr_debug("forward\n");
  174. pia(p)->crb &= ~PIA_DDR; /* make data direction register visible */
  175. pia(p)->pddrb = 255; /* all pins output */
  176. pia(p)->crb |= PIA_DDR; /* make data register visible - default */
  177. }
  178. static void mfc3_data_reverse(struct parport *p)
  179. {
  180. pr_debug("reverse\n");
  181. pia(p)->crb &= ~PIA_DDR; /* make data direction register visible */
  182. pia(p)->pddrb = 0; /* all pins input */
  183. pia(p)->crb |= PIA_DDR; /* make data register visible - default */
  184. }
  185. static void mfc3_init_state(struct pardevice *dev, struct parport_state *s)
  186. {
  187. s->u.amiga.data = 0;
  188. s->u.amiga.datadir = 255;
  189. s->u.amiga.status = 0;
  190. s->u.amiga.statusdir = 0xe0;
  191. }
  192. static void mfc3_save_state(struct parport *p, struct parport_state *s)
  193. {
  194. s->u.amiga.data = pia(p)->pprb;
  195. pia(p)->crb &= ~PIA_DDR;
  196. s->u.amiga.datadir = pia(p)->pddrb;
  197. pia(p)->crb |= PIA_DDR;
  198. s->u.amiga.status = pia(p)->ppra;
  199. pia(p)->cra &= ~PIA_DDR;
  200. s->u.amiga.statusdir = pia(p)->pddrb;
  201. pia(p)->cra |= PIA_DDR;
  202. }
  203. static void mfc3_restore_state(struct parport *p, struct parport_state *s)
  204. {
  205. pia(p)->pprb = s->u.amiga.data;
  206. pia(p)->crb &= ~PIA_DDR;
  207. pia(p)->pddrb = s->u.amiga.datadir;
  208. pia(p)->crb |= PIA_DDR;
  209. pia(p)->ppra = s->u.amiga.status;
  210. pia(p)->cra &= ~PIA_DDR;
  211. pia(p)->pddrb = s->u.amiga.statusdir;
  212. pia(p)->cra |= PIA_DDR;
  213. }
  214. static struct parport_operations pp_mfc3_ops = {
  215. .write_data = mfc3_write_data,
  216. .read_data = mfc3_read_data,
  217. .write_control = mfc3_write_control,
  218. .read_control = mfc3_read_control,
  219. .frob_control = mfc3_frob_control,
  220. .read_status = mfc3_read_status,
  221. .enable_irq = mfc3_enable_irq,
  222. .disable_irq = mfc3_disable_irq,
  223. .data_forward = mfc3_data_forward,
  224. .data_reverse = mfc3_data_reverse,
  225. .init_state = mfc3_init_state,
  226. .save_state = mfc3_save_state,
  227. .restore_state = mfc3_restore_state,
  228. .epp_write_data = parport_ieee1284_epp_write_data,
  229. .epp_read_data = parport_ieee1284_epp_read_data,
  230. .epp_write_addr = parport_ieee1284_epp_write_addr,
  231. .epp_read_addr = parport_ieee1284_epp_read_addr,
  232. .ecp_write_data = parport_ieee1284_ecp_write_data,
  233. .ecp_read_data = parport_ieee1284_ecp_read_data,
  234. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  235. .compat_write_data = parport_ieee1284_write_compat,
  236. .nibble_read_data = parport_ieee1284_read_nibble,
  237. .byte_read_data = parport_ieee1284_read_byte,
  238. .owner = THIS_MODULE,
  239. };
  240. /* ----------- Initialisation code --------------------------------- */
  241. static int __init parport_mfc3_init(void)
  242. {
  243. struct parport *p;
  244. int pias = 0;
  245. struct pia *pp;
  246. struct zorro_dev *z = NULL;
  247. if (!MACH_IS_AMIGA)
  248. return -ENODEV;
  249. while ((z = zorro_find_device(ZORRO_PROD_BSC_MULTIFACE_III, z))) {
  250. unsigned long piabase = z->resource.start+PIABASE;
  251. if (!request_mem_region(piabase, sizeof(struct pia), "PIA"))
  252. continue;
  253. pp = ZTWO_VADDR(piabase);
  254. pp->crb = 0;
  255. pp->pddrb = 255; /* all data pins output */
  256. pp->crb = PIA_DDR|32|8;
  257. dummy = pp->pddrb; /* reading clears interrupt */
  258. pp->cra = 0;
  259. pp->pddra = 0xe0; /* /RESET, /DIR ,/AUTO-FEED output */
  260. pp->cra = PIA_DDR;
  261. pp->ppra = 0; /* reset printer */
  262. udelay(10);
  263. pp->ppra = 128;
  264. p = parport_register_port((unsigned long)pp, IRQ_AMIGA_PORTS,
  265. PARPORT_DMA_NONE, &pp_mfc3_ops);
  266. if (!p)
  267. goto out_port;
  268. if (p->irq != PARPORT_IRQ_NONE) {
  269. if (use_cnt++ == 0)
  270. if (request_irq(IRQ_AMIGA_PORTS, mfc3_interrupt, IRQF_SHARED, p->name, &pp_mfc3_ops))
  271. goto out_irq;
  272. }
  273. p->dev = &z->dev;
  274. this_port[pias++] = p;
  275. pr_info("%s: Multiface III port using irq\n", p->name);
  276. /* XXX: set operating mode */
  277. p->private_data = (void *)piabase;
  278. parport_announce_port (p);
  279. if (pias >= MAX_MFC)
  280. break;
  281. continue;
  282. out_irq:
  283. parport_put_port(p);
  284. out_port:
  285. release_mem_region(piabase, sizeof(struct pia));
  286. }
  287. return pias ? 0 : -ENODEV;
  288. }
  289. static void __exit parport_mfc3_exit(void)
  290. {
  291. int i;
  292. for (i = 0; i < MAX_MFC; i++) {
  293. if (!this_port[i])
  294. continue;
  295. parport_remove_port(this_port[i]);
  296. if (this_port[i]->irq != PARPORT_IRQ_NONE) {
  297. if (--use_cnt == 0)
  298. free_irq(IRQ_AMIGA_PORTS, &pp_mfc3_ops);
  299. }
  300. release_mem_region(ZTWO_PADDR(this_port[i]->private_data), sizeof(struct pia));
  301. parport_put_port(this_port[i]);
  302. }
  303. }
  304. MODULE_AUTHOR("Joerg Dorchain <[email protected]>");
  305. MODULE_DESCRIPTION("Parport Driver for Multiface 3 expansion cards Parallel Port");
  306. MODULE_LICENSE("GPL");
  307. module_init(parport_mfc3_init)
  308. module_exit(parport_mfc3_exit)