parport_mfc3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #if 0 /* currently unused */
  134. static unsigned char status_pc_to_mfc3(unsigned char status)
  135. {
  136. unsigned char ret = 1;
  137. if (status & PARPORT_STATUS_BUSY) /* Busy */
  138. ret &= ~1;
  139. if (status & PARPORT_STATUS_ACK) /* Ack */
  140. ret |= 8;
  141. if (status & PARPORT_STATUS_PAPEROUT) /* PaperOut */
  142. ret |= 2;
  143. if (status & PARPORT_STATUS_SELECT) /* select */
  144. ret |= 4;
  145. if (status & PARPORT_STATUS_ERROR) /* error */
  146. ret |= 16;
  147. return ret;
  148. }
  149. #endif
  150. static unsigned char status_mfc3_to_pc(unsigned char status)
  151. {
  152. unsigned char ret = PARPORT_STATUS_BUSY;
  153. if (status & 1) /* Busy */
  154. ret &= ~PARPORT_STATUS_BUSY;
  155. if (status & 2) /* PaperOut */
  156. ret |= PARPORT_STATUS_PAPEROUT;
  157. if (status & 4) /* Selected */
  158. ret |= PARPORT_STATUS_SELECT;
  159. if (status & 8) /* Ack */
  160. ret |= PARPORT_STATUS_ACK;
  161. if (status & 16) /* /ERROR */
  162. ret |= PARPORT_STATUS_ERROR;
  163. return ret;
  164. }
  165. #if 0 /* currently unused */
  166. static void mfc3_write_status( struct parport *p, unsigned char status)
  167. {
  168. DPRINTK(KERN_DEBUG "write_status %02x\n",status);
  169. pia(p)->ppra = (pia(p)->ppra & 0xe0) | status_pc_to_mfc3(status);
  170. }
  171. #endif
  172. static unsigned char mfc3_read_status(struct parport *p)
  173. {
  174. unsigned char status;
  175. status = status_mfc3_to_pc(pia(p)->ppra & 0x1f);
  176. DPRINTK(KERN_DEBUG "read_status %02x\n", status);
  177. return status;
  178. }
  179. #if 0 /* currently unused */
  180. static void mfc3_change_mode( struct parport *p, int m)
  181. {
  182. /* XXX: This port only has one mode, and I am
  183. not sure about the corresponding PC-style mode*/
  184. }
  185. #endif
  186. static int use_cnt = 0;
  187. static irqreturn_t mfc3_interrupt(int irq, void *dev_id)
  188. {
  189. int i;
  190. for( i = 0; i < MAX_MFC; i++)
  191. if (this_port[i] != NULL)
  192. if (pia(this_port[i])->crb & 128) { /* Board caused interrupt */
  193. dummy = pia(this_port[i])->pprb; /* clear irq bit */
  194. parport_generic_irq(irq, this_port[i]);
  195. }
  196. return IRQ_HANDLED;
  197. }
  198. static void mfc3_enable_irq(struct parport *p)
  199. {
  200. pia(p)->crb |= PIA_C1_ENABLE_IRQ;
  201. }
  202. static void mfc3_disable_irq(struct parport *p)
  203. {
  204. pia(p)->crb &= ~PIA_C1_ENABLE_IRQ;
  205. }
  206. static void mfc3_data_forward(struct parport *p)
  207. {
  208. DPRINTK(KERN_DEBUG "forward\n");
  209. pia(p)->crb &= ~PIA_DDR; /* make data direction register visible */
  210. pia(p)->pddrb = 255; /* all pins output */
  211. pia(p)->crb |= PIA_DDR; /* make data register visible - default */
  212. }
  213. static void mfc3_data_reverse(struct parport *p)
  214. {
  215. DPRINTK(KERN_DEBUG "reverse\n");
  216. pia(p)->crb &= ~PIA_DDR; /* make data direction register visible */
  217. pia(p)->pddrb = 0; /* all pins input */
  218. pia(p)->crb |= PIA_DDR; /* make data register visible - default */
  219. }
  220. static void mfc3_init_state(struct pardevice *dev, struct parport_state *s)
  221. {
  222. s->u.amiga.data = 0;
  223. s->u.amiga.datadir = 255;
  224. s->u.amiga.status = 0;
  225. s->u.amiga.statusdir = 0xe0;
  226. }
  227. static void mfc3_save_state(struct parport *p, struct parport_state *s)
  228. {
  229. s->u.amiga.data = pia(p)->pprb;
  230. pia(p)->crb &= ~PIA_DDR;
  231. s->u.amiga.datadir = pia(p)->pddrb;
  232. pia(p)->crb |= PIA_DDR;
  233. s->u.amiga.status = pia(p)->ppra;
  234. pia(p)->cra &= ~PIA_DDR;
  235. s->u.amiga.statusdir = pia(p)->pddrb;
  236. pia(p)->cra |= PIA_DDR;
  237. }
  238. static void mfc3_restore_state(struct parport *p, struct parport_state *s)
  239. {
  240. pia(p)->pprb = s->u.amiga.data;
  241. pia(p)->crb &= ~PIA_DDR;
  242. pia(p)->pddrb = s->u.amiga.datadir;
  243. pia(p)->crb |= PIA_DDR;
  244. pia(p)->ppra = s->u.amiga.status;
  245. pia(p)->cra &= ~PIA_DDR;
  246. pia(p)->pddrb = s->u.amiga.statusdir;
  247. pia(p)->cra |= PIA_DDR;
  248. }
  249. static struct parport_operations pp_mfc3_ops = {
  250. .write_data = mfc3_write_data,
  251. .read_data = mfc3_read_data,
  252. .write_control = mfc3_write_control,
  253. .read_control = mfc3_read_control,
  254. .frob_control = mfc3_frob_control,
  255. .read_status = mfc3_read_status,
  256. .enable_irq = mfc3_enable_irq,
  257. .disable_irq = mfc3_disable_irq,
  258. .data_forward = mfc3_data_forward,
  259. .data_reverse = mfc3_data_reverse,
  260. .init_state = mfc3_init_state,
  261. .save_state = mfc3_save_state,
  262. .restore_state = mfc3_restore_state,
  263. .epp_write_data = parport_ieee1284_epp_write_data,
  264. .epp_read_data = parport_ieee1284_epp_read_data,
  265. .epp_write_addr = parport_ieee1284_epp_write_addr,
  266. .epp_read_addr = parport_ieee1284_epp_read_addr,
  267. .ecp_write_data = parport_ieee1284_ecp_write_data,
  268. .ecp_read_data = parport_ieee1284_ecp_read_data,
  269. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  270. .compat_write_data = parport_ieee1284_write_compat,
  271. .nibble_read_data = parport_ieee1284_read_nibble,
  272. .byte_read_data = parport_ieee1284_read_byte,
  273. .owner = THIS_MODULE,
  274. };
  275. /* ----------- Initialisation code --------------------------------- */
  276. static int __init parport_mfc3_init(void)
  277. {
  278. struct parport *p;
  279. int pias = 0;
  280. struct pia *pp;
  281. struct zorro_dev *z = NULL;
  282. if (!MACH_IS_AMIGA)
  283. return -ENODEV;
  284. while ((z = zorro_find_device(ZORRO_PROD_BSC_MULTIFACE_III, z))) {
  285. unsigned long piabase = z->resource.start+PIABASE;
  286. if (!request_mem_region(piabase, sizeof(struct pia), "PIA"))
  287. continue;
  288. pp = (struct pia *)ZTWO_VADDR(piabase);
  289. pp->crb = 0;
  290. pp->pddrb = 255; /* all data pins output */
  291. pp->crb = PIA_DDR|32|8;
  292. dummy = pp->pddrb; /* reading clears interrupt */
  293. pp->cra = 0;
  294. pp->pddra = 0xe0; /* /RESET, /DIR ,/AUTO-FEED output */
  295. pp->cra = PIA_DDR;
  296. pp->ppra = 0; /* reset printer */
  297. udelay(10);
  298. pp->ppra = 128;
  299. p = parport_register_port((unsigned long)pp, IRQ_AMIGA_PORTS,
  300. PARPORT_DMA_NONE, &pp_mfc3_ops);
  301. if (!p)
  302. goto out_port;
  303. if (p->irq != PARPORT_IRQ_NONE) {
  304. if (use_cnt++ == 0)
  305. if (request_irq(IRQ_AMIGA_PORTS, mfc3_interrupt, IRQF_SHARED, p->name, &pp_mfc3_ops))
  306. goto out_irq;
  307. }
  308. this_port[pias++] = p;
  309. printk(KERN_INFO "%s: Multiface III port using irq\n", p->name);
  310. /* XXX: set operating mode */
  311. p->private_data = (void *)piabase;
  312. parport_announce_port (p);
  313. if (pias >= MAX_MFC)
  314. break;
  315. continue;
  316. out_irq:
  317. parport_put_port(p);
  318. out_port:
  319. release_mem_region(piabase, sizeof(struct pia));
  320. }
  321. return pias ? 0 : -ENODEV;
  322. }
  323. static void __exit parport_mfc3_exit(void)
  324. {
  325. int i;
  326. for (i = 0; i < MAX_MFC; i++) {
  327. if (!this_port[i])
  328. continue;
  329. parport_remove_port(this_port[i]);
  330. if (!this_port[i]->irq != PARPORT_IRQ_NONE) {
  331. if (--use_cnt == 0)
  332. free_irq(IRQ_AMIGA_PORTS, &pp_mfc3_ops);
  333. }
  334. release_mem_region(ZTWO_PADDR(this_port[i]->private_data), sizeof(struct pia));
  335. parport_put_port(this_port[i]);
  336. }
  337. }
  338. MODULE_AUTHOR("Joerg Dorchain <joerg@dorchain.net>");
  339. MODULE_DESCRIPTION("Parport Driver for Multiface 3 expansion cards Paralllel Port");
  340. MODULE_SUPPORTED_DEVICE("Multiface 3 Parallel Port");
  341. MODULE_LICENSE("GPL");
  342. module_init(parport_mfc3_init)
  343. module_exit(parport_mfc3_exit)