parport_mfc3.c 9.6 KB

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