linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dirk Brandewie <dirk.brandewie@gmail.com>
To: Thomas Chou <thomas@wytron.com.tw>
Cc: David Brownell <dbrownell@users.sourceforge.net>,
	Grant Likely <grant.likely@secretlab.ca>,
	nios2-dev@sopc.et.ntust.edu.tw,
	devicetree-discuss@lists.ozlabs.org,
	linux-kernel@vger.kernel.org,
	spi-devel-general@lists.sourceforge.net
Subject: Re: [PATCH v4] spi: add OpenCores tiny SPI driver
Date: Thu, 03 Feb 2011 07:20:32 -0800	[thread overview]
Message-ID: <4D4AC7C0.4050608@gmail.com> (raw)
In-Reply-To: <1296729466-2936-1-git-send-email-thomas@wytron.com.tw>

On 02/03/2011 02:37 AM, Thomas Chou wrote:
> This patch adds support of OpenCores tiny SPI driver.
>
> http://opencores.org/project,tiny_spi
>

> +static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
> +{
> +	struct tiny_spi *hw = tiny_spi_to_hw(spi);
> +	const u8 *txp = t->tx_buf;
> +	u8 *rxp = t->rx_buf;
> +	unsigned int i;
> +
> +	if (hw->irq>= 0) {
> +		/* use intrrupt driven data transfer */
> +		hw->len = t->len;
> +		hw->txp = t->tx_buf;
> +		hw->rxp = t->rx_buf;
> +		hw->txc = 0;
> +		hw->rxc = 0;
> +
> +		/* send the first byte */
> +		if (t->len>  1) {
> +			writeb(hw->txp ? *hw->txp++ : 0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(hw->txp ? *hw->txp++ : 0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
> +		} else {
> +			writeb(hw->txp ? *hw->txp++ : 0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
> +		}
> +
> +		wait_for_completion(&hw->done);
> +	} else if (txp&&  rxp) {
> +		/* we need to tighten the transfer loop */
> +		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				u8 rx, tx = *txp++;
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();

Putting the read status, cpu releax in an inline function would make this 
function a lot easier to read.

> +				rx = readb(hw->base + TINY_SPI_TXDATA);
> +				writeb(tx, hw->base + TINY_SPI_TXDATA);
> +				*rxp++ = rx;
> +			}
> +			while (!(readb(hw->base + TINY_SPI_STATUS)&
> +				 TINY_SPI_STATUS_TXR))
> +				cpu_relax();
> +			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +	} else if (rxp) {
> +		writeb(0, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				u8 rx;
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				rx = readb(hw->base + TINY_SPI_TXDATA);
> +				writeb(0,
> +				       hw->base + TINY_SPI_TXDATA);
> +				*rxp++ = rx;
> +			}
> +			while (!(readb(hw->base + TINY_SPI_STATUS)&
> +				 TINY_SPI_STATUS_TXR))
> +				cpu_relax();
> +			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +	} else if (txp) {
> +		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				u8 tx = *txp++;
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				writeb(tx, hw->base + TINY_SPI_TXDATA);
> +			}
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +	} else {
> +		writeb(0, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				writeb(0,
> +				       hw->base + TINY_SPI_TXDATA);
> +			}
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +	}
> +	return t->len;
> +}

      parent reply	other threads:[~2011-02-03 15:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1294645208-6322-1-git-send-email-thomas@wytron.com.tw>
     [not found] ` <1294645208-6322-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-12  2:41   ` [PATCH v2] spi: add OpenCores tiny SPI driver Thomas Chou
     [not found]     ` <1294800109-31603-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-17  7:32       ` Thomas Chou
     [not found]         ` <1295249542-26743-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-20 16:54           ` Grant Likely
2011-01-20 21:36             ` Thomas Chou
     [not found]               ` <4D38AAEB.7020308-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-21 12:27                 ` Jonas Bonn
2011-01-24  1:19                   ` Thomas Chou
     [not found]                     ` <4D3CD38D.4080808-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-24 10:59                       ` Jonas Bonn
2011-01-24 14:50                         ` Thomas Chou
     [not found]                           ` <4D3D91D1.8000701-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-24 15:50                             ` Grant Likely
2011-01-25  3:17                         ` [PATCH v3] " Thomas Chou
     [not found]                           ` <1295925450-28550-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-02-03 10:37                             ` [PATCH v4] " Thomas Chou
2011-02-03 11:03                               ` Wolfram Sang
2011-02-03 15:20                               ` Dirk Brandewie [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D4AC7C0.4050608@gmail.com \
    --to=dirk.brandewie@gmail.com \
    --cc=dbrownell@users.sourceforge.net \
    --cc=devicetree-discuss@lists.ozlabs.org \
    --cc=grant.likely@secretlab.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nios2-dev@sopc.et.ntust.edu.tw \
    --cc=spi-devel-general@lists.sourceforge.net \
    --cc=thomas@wytron.com.tw \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).