parport_mfc3.c 9.9 KB

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