
/*
 * lp driver for the parallel port of a Multiface Card III
 * 6.11.95 Joerg Dorchain (dorchain@mpi-sb.mpg.de)
 *
 */

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/lp_mfc.h>
#include <asm/amigahw.h>
#include "multiface.h"
#include "mc6821.h"

#define MAX_MFC 5  /* Max. number of cards supported */

static void lp_mfc_out(int,int);
static int lp_mfc_busy(int);
static int lp_mfc_pout(int);
static int lp_mfc_online(int);
static int lp_mfc_interrupt(int);

static inline struct pia *pia(int);

int lp_mfc_init(struct lp_struct *,int,int,int);

static volatile int dummy; /* for trigger reads */
static int pia0 = 0;
static struct pia *pia_base[MAX_MFC];


static void lp_mfc_out(int c, int dev)
{
int wait = 0;

while (wait != lp_table[dev].wait) wait++;
dummy = pia(dev)->prb; /* trigger read clears irq bit*/
pia(dev)->prb = c;  /* strobe goes down by hardware */
}

static int lp_mfc_busy(int dev)
{
return pia(dev)->pra&1;
}

static int lp_mfc_pout(int dev)
{
return pia(dev)->pra&2;
}

static int lp_mfc_online(int dev)
{
return pia(dev)->pra&4;
}

static int lp_mfc_interrupt(int dev)
{
int inter = pia(dev)->crb&128;

dummy = pia(dev)->prb; /* clears irq bit */
return inter;
}

static inline struct pia *pia(int dev)
{
return pia_base[dev-pia0];
}

int lp_mfc_init(struct lp_struct *lp_table, int entry, int max_lp,int irq)
{
int i;
int pias;
struct pia *pp;
int key;
struct ConfigDev *cd;

for(i=0;i<MAX_MFC;i++)
  pia_base[i] = NULL;
pia0 = entry;

pias = 0;
/* roman: The following may be wrong if there are more than one autocon
 * structures, but I didn't find a zorro function for this. Please fix if
 * necessary! */
while((key = zorro_find( MANUF_BSC2, PROD_MULTIFACE_III, 0 , 0))) {
  cd = zorro_get_board( key );
  pp = (struct pia *)ZTWO_VADDR((((u_char *)cd->cd_BoardAddr)+PIABASE));
  if ((max_lp - entry > 1) && (pias < MAX_MFC)) {
    pia_base[pias] = pp;
	pp->crb = 0;
	pp->ddrb = 255;    /* all pins output */
	pp->crb = DDR|32;
	dummy = pp->prb;
	pp->crb |=(irq!=0)?C1_ENABLE_IRQ:0; 
	pp->cra = 0;
	pp->ddra = 0xe0;   /* /RESET,  /DIR ,/AUTO-FEED output */
	pp->cra = DDR;
	pp->pra = 0;    /* reset printer */
	udelay(5);
	pp->pra |= 128;
	lp_table[entry].name="Multiface III LP";
	lp_table[entry].lp_out=lp_mfc_out;
	lp_table[entry].lp_is_busy=lp_mfc_busy;
	lp_table[entry].lp_has_pout=lp_mfc_pout;
	lp_table[entry].lp_is_online=lp_mfc_online;
	lp_table[entry].lp_my_interrupt=lp_mfc_interrupt;
	lp_table[entry].flags=LP_EXIST;
	lp_table[entry].chars=LP_INIT_CHAR;
	lp_table[entry].time=LP_INIT_TIME;
	lp_table[entry].wait=LP_INIT_WAIT;
	lp_table[entry].lp_wait_q=NULL;
	printk("lp%d: Multiface Card III nr. %d, pia at 0x%08lx\n",entry,pias,(u_long)pp);
	zorro_config_board( key, 0 );
	entry++;
	pias++;
  }
}

return pias;
}

