/*
 * (C) Copyright 2001
 * John Clemens <clemens@mclx.com>, Mission Critical Linux, Inc.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 * mpsc.c - driver for console over the MPSC.
 */

#include <ppcboot.h>
#include <config.h>

#include <malloc.h>
#include "mpsc.h"

unsigned int *rx_desc_base;
unsigned int rx_desc_index;
unsigned int *tx_desc_base;
unsigned int tx_desc_index;

int (*mpsc_putchar)(char ch) = mpsc_putchar_early;

/* local function declarations */
static int galmpsc_connect(int channel, int connect);
static int galmpsc_route_serial(int channel, int connect);
static int galmpsc_route_rx_clock(int channel, int brg);
static int galmpsc_route_tx_clock(int channel, int brg);
static int galmpsc_write_config_regs(int mpsc, int mode);
static int galmpsc_config_channel_regs(int mpsc);
static int galmpsc_set_char_length(int mpsc, int value);
static int galmpsc_set_stop_bit_length(int mpsc, int value);
static int galmpsc_set_parity(int mpsc, int value);
static int galmpsc_enter_hunt(int mpsc);
static int galmpsc_set_brkcnt(int mpsc, int value);
static int galmpsc_set_tcschar(int mpsc, int value);
static int galmpsc_shutdown(int mpsc);

static int galsdma_set_RFT(int channel);
static int galsdma_set_SFM(int channel);
static int galsdma_set_rxle(int channel);
static int galsdma_set_txle(int channel);
static int galsdma_set_burstsize(int channel, unsigned int value);
static int galsdma_set_RC(int channel, unsigned int value);

static int galbrg_set_CDV(int channel, int value);
static int galbrg_enable(int channel);
static int galbrg_disable(int channel);
static int galbrg_set_clksrc(int channel, int value);
static int galbrg_set_CUV(int channel, int value);
#if 0
static int galbrg_reset(int channel);
#endif

/* special function for running out of flash.  doesn't modify any
 * global variables [josh] */
int
mpsc_putchar_early(char ch)
{
	unsigned int tx_buffers[20] = { [0 ... 19] = 0 };
	unsigned int temp;
	unsigned int *tx_descriptor;

	memset(tx_buffers, 0, 20 * sizeof(unsigned int));

	/* align the descriptor */
	tx_descriptor = (unsigned int *)((unsigned int)(&tx_buffers[4]) &
					 0xFFFFFFF0);

	tx_descriptor[5] = (unsigned int)ch;
	tx_descriptor[3] = (unsigned int)&tx_descriptor[4];
	tx_descriptor[2] = (unsigned int)&tx_descriptor[0];
	tx_descriptor[1] = DESC_OWNER | DESC_FIRST | DESC_LAST;
	tx_descriptor[0] = 0x00010000;

#if 0
	tx_descriptor[9] = DESC_FIRST | DESC_LAST;
	tx_descriptor[10] = (unsigned int)&tx_descriptor[0];
	tx_descriptor[11] = (unsigned int)&tx_descriptor[12];
#endif

	GT_REG_WRITE(GALSDMA_0_CUR_TX_PTR+(CHANNEL*GALSDMA_REG_DIFF), 
		     (unsigned int)&tx_descriptor[0]);
	GT_REG_WRITE(GALSDMA_0_FIR_TX_PTR+(CHANNEL*GALSDMA_REG_DIFF), 
		     (unsigned int)&tx_descriptor[0]);

	temp = GTREGREAD(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF));
	temp |= (TX_DEMAND | TX_STOP);
	GT_REG_WRITE(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF), temp);

	while(tx_descriptor[1] & DESC_OWNER);

	return 0;
}

/* this function is for use after we relocate and aren't running out
 * of flash any more.  Currently, it's not used, since mpsc_putchar_early
 * seems to be working just fine. [josh] */
int
mpsc_putchar_full(char ch)
{
	unsigned int temp;

	tx_desc_index = (tx_desc_index + 1) % TX_DESC;

	tx_desc_base[(tx_desc_index*8) + 5] = (unsigned int)ch;
	tx_desc_base[(tx_desc_index*8) + 4] = (unsigned int)0x0;
	tx_desc_base[(tx_desc_index*8) + 3] =
		(unsigned int)&tx_desc_base[(tx_desc_index*8) + 4];

	tx_desc_base[(tx_desc_index*8) + 2] =
		(unsigned int)&tx_desc_base[(tx_desc_index*8) + 0];

	tx_desc_base[(tx_desc_index*8) + 1] =
		DESC_OWNER | DESC_FIRST | DESC_LAST;

	/* set sbytecnt and shadow byte cnt to 1 */
	tx_desc_base[(tx_desc_index*8) + 0] =
		0x00010001;

	GT_REG_WRITE(GALSDMA_0_CUR_TX_PTR+(CHANNEL*GALSDMA_REG_DIFF), 
		     (unsigned int)&tx_desc_base[0]);
	GT_REG_WRITE(GALSDMA_0_FIR_TX_PTR+(CHANNEL*GALSDMA_REG_DIFF), 
		     (unsigned int)&tx_desc_base[0]);

	temp = GTREGREAD(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF));
	temp |= (TX_DEMAND);
	GT_REG_WRITE(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF), temp);
      
	while(tx_desc_base[(tx_desc_index*8) + 1] & DESC_OWNER);

	tx_desc_base[(tx_desc_index*8) + 5] = (unsigned int)0x0;
	tx_desc_base[(tx_desc_index*8) + 4] = (unsigned int)0x0;
	tx_desc_base[(tx_desc_index*8) + 3] =
		(unsigned int)&tx_desc_base[(tx_desc_index*8) + 4];

	tx_desc_base[(tx_desc_index*8) + 2] =
		(unsigned int)&tx_desc_base[(tx_desc_index*8) + 0];

	tx_desc_base[(tx_desc_index*8) + 1] =
		DESC_OWNER | DESC_FIRST | DESC_LAST;

	/* set sbytecnt and shadow byte cnt to 1 */
	tx_desc_base[(tx_desc_index*8) + 0] = 0x00010001;

	tx_desc_index = (tx_desc_index - 1) % TX_DESC;

	return 0;
}

char
mpsc_getchar(void)
{
	unsigned int temp;
	volatile char ch;
#if 0
	int nextindex;
#endif

#if 0
	/* set SDMA Receive Enable */
	temp = GTREGREAD(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF));

	if (!(temp & RX_ENABLE)) {
#define DEBUG
#ifdef DEBUG
		mpsc_putchar_early('!');
#endif
		temp |= RX_ENABLE;
		GT_REG_WRITE(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF), 
			     temp);
		
		galmpsc_enter_hunt(CHANNEL);
		
		GT_REG_WRITE(GALSDMA_0_CUR_RX_PTR+(CHANNEL*GALSDMA_REG_DIFF),
			     (unsigned int)&rx_desc_base[(rx_desc_index*8)]);
	}
#endif

	while (rx_desc_base[(rx_desc_index*8) + 1] & DESC_OWNER) ;

	if (rx_desc_base[(rx_desc_index * 8) + 1] & (1 << 15)) {
		printf("oops, error: %08x\n",
		       rx_desc_base[(rx_desc_index * 8) + 1]);

		temp = GTREGREAD(GALMPSC_CHANNELREG_2);
		temp |= (1 << 23);
		GT_REG_WRITE(GALMPSC_CHANNELREG_2, temp);

		while (GTREGREAD(GALMPSC_CHANNELREG_2) & (1 << 23)) ;

		galmpsc_enter_hunt(CHANNEL);
	}

	ch = (char)(rx_desc_base[(rx_desc_index*8) + 5] & 0x000000FF);

	rx_desc_base[(rx_desc_index*8) + 1] =
		DESC_OWNER | DESC_FIRST | DESC_LAST;

	rx_desc_index = (rx_desc_index + 1) % RX_DESC;

#if 0
	nextindex = (rx_desc_index + 1) % RX_DESC;

	rx_desc_base[(rx_desc_index*8) + 0] = 0x00010000;

	rx_desc_base[(rx_desc_index*8) + 1] =
		DESC_OWNER | DESC_FIRST | DESC_LAST;

	rx_desc_base[(rx_desc_index*8) + 2] =
		(unsigned int)&rx_desc_base[(nextindex*8) + 0];

	rx_desc_base[(rx_desc_index*8) + 3] =
		(unsigned int)&rx_desc_base[(rx_desc_index*8) + 4];

	rx_desc_base[(rx_desc_index*8) + 5] = 0;

	GT_REG_WRITE(GALSDMA_0_CUR_RX_PTR+(CHANNEL*GALSDMA_REG_DIFF),
		     (unsigned int)&rx_desc_base[(rx_desc_index*8)]);
#endif

	return ch;
}

int
mpsc_test_char(void)
{
	if (rx_desc_base[(rx_desc_index*8) + 1] & DESC_OWNER)
		return 0;
	else
		return 1;
}

int
mpsc_init(int baud)
{

	/* BRG CONFIG */
	galbrg_set_baudrate(CHANNEL, baud);
	galbrg_set_clksrc(CHANNEL,0);
	galbrg_set_CUV(CHANNEL, 0);
	galbrg_enable(CHANNEL);

	/* Set up clock routing */
	galmpsc_connect(CHANNEL, GALMPSC_CONNECT);
	galmpsc_route_serial(CHANNEL, GALMPSC_CONNECT);
	galmpsc_route_rx_clock(CHANNEL, CHANNEL);
	galmpsc_route_tx_clock(CHANNEL, CHANNEL);

	/* reset MPSC state */
	galmpsc_shutdown(CHANNEL);
	
	/* SDMA CONFIG */
	galsdma_set_burstsize(CHANNEL, 8);
	galsdma_set_txle(CHANNEL);
	galsdma_set_rxle(CHANNEL);
	galsdma_set_RC(CHANNEL, 0xf);
	galsdma_set_SFM(CHANNEL);
	galsdma_set_RFT(CHANNEL);

	/* MPSC CONFIG */
	galmpsc_write_config_regs(CHANNEL, GALMPSC_UART);
	galmpsc_config_channel_regs(CHANNEL);
	galmpsc_set_char_length(CHANNEL, GALMPSC_CHAR_LENGTH_8);       /* 8 */
	galmpsc_set_parity(CHANNEL, GALMPSC_PARITY_NONE);              /* N */
	galmpsc_set_stop_bit_length(CHANNEL, GALMPSC_STOP_BITS_1);     /* 1 */

	return 0;
}

void
mpsc_init2(void)
{
	int i, temp;

	mpsc_putchar = mpsc_putchar_full;

	rx_desc_base = (unsigned int *)malloc(((RX_DESC+1)*8) *
					      sizeof(unsigned int));

	/* align at a 4 word boundry */
	rx_desc_base = (unsigned int *)
		(((unsigned int)rx_desc_base+32) & 0xFFFFFFF0);

	rx_desc_index = 0;

	memset(rx_desc_base, 0, (RX_DESC*8)*sizeof(unsigned int));

	for (i = 0; i < RX_DESC; i++) {
		rx_desc_base[i*8 + 5] = (unsigned int)0x0;
		rx_desc_base[i*8 + 4] = (unsigned int)0x0;
		rx_desc_base[i*8 + 3] = (unsigned int)&rx_desc_base[i*8 + 4];
		rx_desc_base[i*8 + 2] = (unsigned int)&rx_desc_base[i*8 + 8];
		rx_desc_base[i*8 + 1] = DESC_OWNER | DESC_FIRST | DESC_LAST;

		/* set sbytecnt and shadow byte cnt to 1 */
		rx_desc_base[i*8] = 0x00010000;
	}
	rx_desc_base[i*8 - 6] = (unsigned int)&rx_desc_base[0];

	tx_desc_base = (unsigned int *)malloc((TX_DESC*8) *
					      sizeof(unsigned int));
	tx_desc_index = -1;

	memset(tx_desc_base, 0, (TX_DESC*8)*sizeof(unsigned int));

	for (i = 0; i < TX_DESC; i++) {
		tx_desc_base[i*8 + 5] = (unsigned int)0x23232323;
		tx_desc_base[i*8 + 4] = (unsigned int)0x23232323;
		tx_desc_base[i*8 + 3] = (unsigned int)&tx_desc_base[i*8 + 4];
		tx_desc_base[i*8 + 2] = (unsigned int)&tx_desc_base[i*8 + 8];
		tx_desc_base[i*8 + 1] = DESC_OWNER | DESC_FIRST | DESC_LAST;

		/* set sbytecnt and shadow byte cnt to 1 */
		tx_desc_base[i*8] = 0x00010001;
	}
	tx_desc_base[i*8 - 6] = (unsigned int)&tx_desc_base[0];

	GT_REG_WRITE(GALSDMA_0_CUR_RX_PTR+(CHANNEL*GALSDMA_REG_DIFF), 
		     (unsigned int)&rx_desc_base[0]);

	udelay(100);
	
	temp = GTREGREAD(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF));
	temp |= RX_ENABLE;
	GT_REG_WRITE(GALSDMA_0_COM_REG+(CHANNEL*GALSDMA_REG_DIFF), temp);
	
	galmpsc_enter_hunt(CHANNEL);

	return;
}

int
galbrg_set_baudrate(int channel, int rate) 
{
	int clock;

	galbrg_disable(channel);

	clock = (3686400/(16*rate)) - 1;

	galbrg_set_CDV(channel, clock);

	galbrg_enable(channel);

	return 0;
}

/* ------------------------------------------------------------------ */

/* Below are all the private functions that no one else needs */

static int
galbrg_set_CDV(int channel, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP));
	temp &= 0xFFFF0000;
	temp |= (value & 0x0000FFFF);
	GT_REG_WRITE(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP), temp);

	return 0;
}

static int
galbrg_enable(int channel)
{
	unsigned int temp;

	temp = GTREGREAD(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP));
	temp |= 0x00010000;
	GT_REG_WRITE(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP),temp);

	return 0;
}

static int
galbrg_disable(int channel)
{
	unsigned int temp;

	temp = GTREGREAD(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP));
	temp &= 0xFFFEFFFF;
	GT_REG_WRITE(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP),temp);

	return 0;
}

static int
galbrg_set_clksrc(int channel, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP));
	temp &= 0xFF83FFFF;
	temp |= (value << 18); 
	GT_REG_WRITE(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP),temp);

	return 0;
}

static int
galbrg_set_CUV(int channel, int value)
{
	GT_REG_WRITE(GALBRG_0_BTREG + (channel * GALBRG_REG_GAP), value);

	return 0;
}

#if 0
static int
galbrg_reset(int channel)
{
	unsigned int temp;

	temp = GTREGREAD(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP));
	temp |= 0x20000;
	GT_REG_WRITE(GALBRG_0_CONFREG + (channel * GALBRG_REG_GAP), temp);

	return 0;
}
#endif

static int
galsdma_set_RFT(int channel)
{
	unsigned int temp;

        temp = GTREGREAD(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF));
	temp |= 0x00000001;
	GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF), temp);

	return 0;
}

static int
galsdma_set_SFM(int channel)
{
	unsigned int temp;

        temp = GTREGREAD(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF));
	temp |= 0x00000002;
	GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF), temp);

	return 0;
}

static int
galsdma_set_rxle(int channel)
{
	unsigned int temp;

        temp = GTREGREAD(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF));
	temp |= 0x00000040;
	GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF), temp);

	return 0;
}

static int
galsdma_set_txle(int channel)
{
	unsigned int temp;

        temp = GTREGREAD(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF));
	temp |= 0x00000080;
	GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF), temp);

	return 0;
}

static int
galsdma_set_RC(int channel, unsigned int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF));
	temp &= ~0x0000003c;
	temp |= (value << 2);
	GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF), temp);

	return 0;
}

static int
galsdma_set_burstsize(int channel, unsigned int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF));
	temp &= 0xFFFFCFFF;
	switch (value) {
	 case 8:
		GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF),
			     (temp | (0x3 << 12)));
		break;

	 case 4:
		GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF),
			     (temp | (0x2 << 12)));
		break;

	 case 2:
		GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF),
			     (temp | (0x1 << 12)));
		break;

	 case 1:
		GT_REG_WRITE(GALSDMA_0_CONF_REG+(channel*GALSDMA_REG_DIFF),
			     (temp | (0x0 << 12)));
		break;

	 default:
		return -1;
		break;
	}

	return 0;
}

static int
galmpsc_connect(int channel, int connect)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_ROUTING_REGISTER);

	if ((channel == 0) && connect) 
		temp &= ~0x00000007;
	else if ((channel == 1) && connect)
		temp &= ~(0x00000007 << 6);
	else if ((channel == 0) && !connect)
		temp |= 0x00000007;
	else
		temp |= (0x00000007 << 6);

	/* Just in case... */
	temp &= 0x3fffffff;

	GT_REG_WRITE(GALMPSC_ROUTING_REGISTER, temp);

	return 0;
}

static int
galmpsc_route_serial(int channel, int connect)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_SERIAL_MULTIPLEX);

	if ((channel == 0) && connect) 
		temp |= 0x00000100;
	else if ((channel == 1) && connect)
		temp |= 0x00001000;
	else if ((channel == 0) && !connect)
		temp &= ~0x00000100;
	else
		temp &= ~0x00001000;

	GT_REG_WRITE(GALMPSC_SERIAL_MULTIPLEX,temp);

	return 0;
}

static int
galmpsc_route_rx_clock(int channel, int brg)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_RxC_ROUTE);

	if (channel == 0) 
		temp |= brg;
	else
		temp |= (brg << 8);

	GT_REG_WRITE(GALMPSC_RxC_ROUTE,temp);

	return 0;
}

static int
galmpsc_route_tx_clock(int channel, int brg)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_TxC_ROUTE);

	if (channel == 0) 
		temp |= brg;
	else
		temp |= (brg << 8);

	GT_REG_WRITE(GALMPSC_TxC_ROUTE,temp);

	return 0;
}

static int
galmpsc_write_config_regs(int mpsc, int mode)
{
	if (mode == GALMPSC_UART) {
		/* Main config reg Low (Null modem, Enable Tx/Rx, UART mode */
		GT_REG_WRITE(GALMPSC_MCONF_LOW + (mpsc*GALMPSC_REG_GAP),
			     0x000004c4);
		
		/* Main config reg High (32x Rx/Tx clock mode, width=8bits */
		GT_REG_WRITE(GALMPSC_MCONF_HIGH +(mpsc*GALMPSC_REG_GAP),
			     0x024003f8);
	} else 
		return -1;

	return 0;
}

static int
galmpsc_config_channel_regs(int mpsc)
{
	GT_REG_WRITE(GALMPSC_CHANNELREG_1+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_2+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_3+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_4+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_5+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_6+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_7+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_8+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_9+(mpsc*GALMPSC_REG_GAP), 0);
	GT_REG_WRITE(GALMPSC_CHANNELREG_10+(mpsc*GALMPSC_REG_GAP), 0);

	galmpsc_set_brkcnt(mpsc, 0x3);
	galmpsc_set_tcschar(mpsc, 0xab);

	return 0;
}

static int
galmpsc_set_brkcnt(int mpsc, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_CHANNELREG_1+(mpsc*GALMPSC_REG_GAP));
	temp &= 0x0000FFFF;
	temp |= (value << 16);
	GT_REG_WRITE(GALMPSC_CHANNELREG_1+(mpsc*GALMPSC_REG_GAP), temp);

	return 0;
}

static int
galmpsc_set_tcschar(int mpsc, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_CHANNELREG_1+(mpsc*GALMPSC_REG_GAP));
	temp &= 0xFFFF0000;
	temp |= value;
	GT_REG_WRITE(GALMPSC_CHANNELREG_1+(mpsc*GALMPSC_REG_GAP), temp);

	return 0;
}

static int
galmpsc_set_char_length(int mpsc, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_PROTOCONF_REG + (mpsc*GALMPSC_REG_GAP));
	temp &= 0xFFFFCFFF;
	temp |= (value << 12);
	GT_REG_WRITE(GALMPSC_PROTOCONF_REG + (mpsc*GALMPSC_REG_GAP), temp);

	return 0;
}

static int
galmpsc_set_stop_bit_length(int mpsc, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_PROTOCONF_REG + (mpsc*GALMPSC_REG_GAP));
	temp |= (value << 14);
	GT_REG_WRITE(GALMPSC_PROTOCONF_REG + (mpsc*GALMPSC_REG_GAP),temp);

	return 0;
}

static int
galmpsc_set_parity(int mpsc, int value)
{
	unsigned int temp;

	temp = GTREGREAD(GALMPSC_CHANNELREG_2+(mpsc*GALMPSC_REG_GAP));
	if (value != -1) {
		temp &= 0xFFF3FFF3;
		temp |= ((value << 18) | (value << 2));
		temp |= ((value << 17) | (value << 1));
	} else {
		temp &= 0xFFF1FFF1;
	}
		
	GT_REG_WRITE(GALMPSC_CHANNELREG_2+(mpsc*GALMPSC_REG_GAP), temp);

	return 0;
}

static int
galmpsc_enter_hunt(int mpsc)
{
	unsigned int temp;

	GT_REG_WRITE(GALMPSC_CHANNELREG_2+(mpsc*GALMPSC_REG_GAP), 0x80000000);

	/* read reg 10 to see when we actually ENTER hunt mode */
	temp=GTREGREAD(GALMPSC_CHANNELREG_10);

	while (temp & 0x00000020) {
		temp = GTREGREAD(GALMPSC_CHANNELREG_10);
	}

	return 0;
}

static int
galmpsc_shutdown(int mpsc)
{
	unsigned int temp;

	/* cause RX abort (clears RX) */
	temp = GTREGREAD(GALMPSC_CHANNELREG_2);
	temp |= MPSC_RX_ABORT | MPSC_TX_ABORT;
	temp &= ~MPSC_ENTER_HUNT;
	GT_REG_WRITE(GALMPSC_CHANNELREG_2, temp);
	
	GT_REG_WRITE(GALSDMA_0_COM_REG, 0);
	GT_REG_WRITE(GALSDMA_0_COM_REG, SDMA_TX_ABORT | SDMA_RX_ABORT);

	/* shut down the MPSC */
	GT_REG_WRITE(GALMPSC_MCONF_LOW, 0);
	GT_REG_WRITE(GALMPSC_MCONF_HIGH, 0);
	GT_REG_WRITE(GALMPSC_PROTOCONF_REG, 0);
	
	udelay(100);
	
	/* shut down the sdma engines. */
	/* reset config to default */	
	GT_REG_WRITE(GALSDMA_0_CONF_REG, 0x000000fc); 
	
	udelay(100);
	
	/* clear the SDMA current and first TX and RX pointers */
	GT_REG_WRITE(GALSDMA_0_CUR_RX_PTR, 0);
	GT_REG_WRITE(GALSDMA_0_CUR_TX_PTR, 0);
	GT_REG_WRITE(GALSDMA_0_FIR_TX_PTR, 0);

	udelay(100);

	return 0;
}
