/*
 * (C) Copyright 2008 Andre McCurdy, NXP Semiconductors
 *
 * 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
 */

#include <asm/asm.h>
#include <asm/regdef.h>


/******************************************************************************/
/******************************************************************************/

	.text

	.align	2

	.globl	nand_data_read
	.ent    nand_data_read
	.type   nand_data_read, @function

nand_data_read: 			# a0 : base pointer, a1 : dst address, a2 : bytecount

	.set	noreorder
	.set	noat

	addu	a2, a1, a2		# a2 = dst_end
1:	andi	v0, a1, 0x03		# v0 = (dst & 0x03)
	beq	v0, zero, 2f		# branch if dst is aligned
	subu	a3, a2, a1		# a3 = bytes_remaining
	beq	a1, a2, 6f		# branch if (dst == dst_end)
	addi	t5, v0, 0x5241		# bdslot
	lbu	t0, 0(a0)		# t0 = *((unsigned char *) base)
	addiu	a1, a1, 1		# dst += 1
	b	1b			# loop
	sb	t0, -1(a1)		# dst[-1] = t0
2:	srl	v0, a3, 4		# v0 = 16byte_blocks_remaining
	beq	v0, zero, 4f		# branch if (16byte_blocks_remaining == 0)
	sll	v0, v0, 4		# v0 = number of bytes which will be handled as 16byte blocks
	add	v1, a1, v0		# v1 = dst_end for 16byte block copies
3:	lw	t0, 0(a0)
	addiu	a1, a1, 16		# dst += 16
	lw	t1, 0(a0)
	sw	t0, -16(a1)
	lw	t2, 0(a0)
	sw	t1, -12(a1)
	lw	t3, 0(a0)
	sw	t2, -8(a1)
	bne	a1, v1, 3b		# branch if (dst != (dst_end for 16byte block copies))
	sw	t3, -4(a1)
4:	beq	a1, a2, 6f		# branch if (dst == dst_end)
5:	addiu	a1, a1, 1
	lbu	t0, 0(a0)
	bne	a1, a2, 5b		# branch if (dst != dst_end)
	sb	t0, -1(a1)
6:	jr	ra
	nop

	.set	at
	.set	reorder

	.end    nand_data_read


/******************************************************************************/
/******************************************************************************/

	.globl	nand_data_write
	.ent    nand_data_write
	.type   nand_data_write, @function

nand_data_write: 			# a0 : base pointer, a1 : src address, a2 : bytecount

	.set	noreorder
	.set	noat

	addu	a2, a1, a2		# a2 = src_end
1:	andi	v0, a1, 0x03		# v0 = (src & 0x03)
	beq	v0, zero, 2f		# branch if src is aligned
	subu	a3, a2, a1		# a3 = bytes_remaining
	beq	a1, a2, 6f		# branch if (src == src_end)
	addi	t5, v0, 0x5241		# bdslot
	lbu	t0, 0(a1)		# t0 = *src
	addiu	a1, a1, 1		# src += 1
	b	1b			# loop
	sb	t0, 0(a0)		# *((unsigned char *) base) = t0
2:	srl	v0, a3, 4		# v0 = 16byte_blocks_remaining
	beq	v0, zero, 4f		# branch if (16byte_blocks_remaining == 0)
	sll	v0, v0, 4		# v0 = number of bytes which will be handled as 16byte blocks
	add	v1, a1, v0		# v1 = src_end for 16byte block copies

3:	lw	t0, 0(a1)
	addiu	a1, a1, 16		# src += 16
	lw	t1, -12(a1)
	#
	# MIU HW seems to have issues... "store word" doesn't work as expected.
	# Repeated "store byte"s are a workaround.
	#
#if defined (__MIPSEL__)
	sb	t0, 0(a0)
	srl	t0, t0, 8
	sb	t0, 0(a0)
	srl	t0, t0, 8
	sb	t0, 0(a0)
	srl	t0, t0, 8
	sb	t0, 0(a0)
	lw	t2, -8(a1)
	sb	t1, 0(a0)
	srl	t1, t1, 8
	sb	t1, 0(a0)
	srl	t1, t1, 8
	sb	t1, 0(a0)
	srl	t1, t1, 8
	sb	t1, 0(a0)
	lw	t3, -4(a1)
	sb	t2, 0(a0)
	srl	t2, t2, 8
	sb	t2, 0(a0)
	srl	t2, t2, 8
	sb	t2, 0(a0)
	srl	t2, t2, 8
	sb	t2, 0(a0)
	sb	t3, 0(a0)
	srl	t3, t3, 8
	sb	t3, 0(a0)
	srl	t3, t3, 8
	sb	t3, 0(a0)
	srl	t3, t3, 8
#elif defined (__MIPSEB__)
#error "Big-endian option has not been tested..."
	srl	t4, t0, 24
	sb	t4, 0(a0)
	srl	t4, t0, 16
	sb	t4, 0(a0)
	srl	t4, t0, 8
	sb	t4, 0(a0)
	sb	t0, 0(a0)
	lw	t2, -8(a1)
	srl	t4, t1, 24
	sb	t4, 0(a0)
	srl	t4, t1, 16
	sb	t4, 0(a0)
	srl	t4, t1, 8
	sb	t4, 0(a0)
	sb	t1, 0(a0)
	lw	t3, -4(a1)
	srl	t4, t2, 24
	sb	t4, 0(a0)
	srl	t4, t2, 16
	sb	t4, 0(a0)
	srl	t4, t2, 8
	sb	t4, 0(a0)
	sb	t2, 0(a0)
	srl	t4, t3, 24
	sb	t4, 0(a0)
	srl	t4, t3, 16
	sb	t4, 0(a0)
	srl	t4, t3, 8
	sb	t4, 0(a0)
#else
#error "error detecting target byte order"
#endif
	bne	a1, v1, 3b		# branch if (src != (src_end for 16byte block copies))
	sb	t3, 0(a0)
4:	beq	a1, a2, 6f		# branch if (src == src_end)
5:	addiu	a1, a1, 1
	lbu	t0, -1(a1)
	bne	a1, a2, 5b		# branch if (src != src_end)
	sb	t0, 0(a0)
6:	jr	ra
	nop

	.set	at
	.set	reorder

	.end    nand_data_write


/******************************************************************************/
/******************************************************************************/

