Line 1... |
Line 1... |
/*
|
/*
|
* Opencore tiny_spi driver
|
* OpenCores tiny SPI master driver
|
|
*
|
|
* http://opencores.org/project,tiny_spi
|
*
|
*
|
* Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
|
* Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
|
*
|
*
|
* Based on spi_s3c24xx.c, which is:
|
* Based on spi_s3c24xx.c, which is:
|
* Copyright (c) 2006 Ben Dooks
|
* Copyright (c) 2006 Ben Dooks
|
Line 10... |
Line 12... |
*
|
*
|
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
* it under the terms of the GNU General Public License version 2 as
|
* it under the terms of the GNU General Public License version 2 as
|
* published by the Free Software Foundation.
|
* published by the Free Software Foundation.
|
*/
|
*/
|
|
|
#include <linux/init.h>
|
#include <linux/init.h>
|
#include <linux/spinlock.h>
|
#include <linux/spinlock.h>
|
#include <linux/interrupt.h>
|
#include <linux/interrupt.h>
|
#include <linux/delay.h>
|
#include <linux/delay.h>
|
#include <linux/errno.h>
|
#include <linux/errno.h>
|
#include <linux/err.h>
|
#include <linux/err.h>
|
#include <linux/clk.h>
|
#include <linux/clk.h>
|
#include <linux/platform_device.h>
|
#include <linux/platform_device.h>
|
#include <linux/spi/spi.h>
|
#include <linux/spi/spi.h>
|
#include <linux/spi/spi_bitbang.h>
|
#include <linux/spi/spi_bitbang.h>
|
|
#include <linux/spi/oc_tiny_spi.h>
|
#include <linux/io.h>
|
#include <linux/io.h>
|
#include <linux/gpio.h>
|
#include <linux/gpio.h>
|
|
#include <linux/of.h>
|
|
|
#define DRV_NAME "oc_tiny_spi"
|
#define DRV_NAME "oc_tiny_spi"
|
|
|
#define TINY_SPI_RXDATA 0
|
#define TINY_SPI_RXDATA 0
|
#define TINY_SPI_TXDATA 4
|
#define TINY_SPI_TXDATA 4
|
Line 41... |
Line 46... |
struct spi_bitbang bitbang;
|
struct spi_bitbang bitbang;
|
struct completion done;
|
struct completion done;
|
|
|
void __iomem *base;
|
void __iomem *base;
|
int irq;
|
int irq;
|
uint freq;
|
unsigned int freq;
|
uint baudwidth;
|
unsigned int baudwidth;
|
uint baud;
|
int interrupt; /* use interrupt driven data transfer, slow */
|
uint speed_hz;
|
unsigned int baud;
|
|
unsigned int speed_hz;
|
struct spi_master *master;
|
unsigned int mode;
|
struct resource *ioarea;
|
unsigned int len;
|
struct device *dev;
|
unsigned int txc, rxc;
|
|
const u8 *txp;
|
|
u8 *rxp;
|
};
|
};
|
|
|
static inline struct tiny_spi *to_hw(struct spi_device *sdev)
|
static inline struct tiny_spi *to_hw(struct spi_device *sdev)
|
{
|
{
|
return spi_master_get_devdata(sdev->master);
|
return spi_master_get_devdata(sdev->master);
|
}
|
}
|
|
|
static uint tiny_spi_baud(struct spi_device *spi, uint hz)
|
static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
|
{
|
{
|
struct tiny_spi *hw = to_hw(spi);
|
struct tiny_spi *hw = to_hw(spi);
|
uint baud;
|
|
baud = DIV_ROUND_UP(hw->freq, hz * 2) - 1;
|
return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
|
if (baud > (1 << hw->baudwidth) - 1)
|
|
baud = (1 << hw->baudwidth) - 1;
|
|
return baud;
|
|
}
|
}
|
|
|
static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
|
static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
|
{
|
{
|
gpio_set_value(spi->chip_select, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
|
gpio_set_value(spi->chip_select,
|
|
(spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
|
}
|
}
|
|
|
static int tiny_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
|
static int tiny_spi_setup_transfer(struct spi_device *spi,
|
|
struct spi_transfer *t)
|
{
|
{
|
struct tiny_spi *hw = to_hw(spi);
|
struct tiny_spi *hw = to_hw(spi);
|
uint baud = hw->baud;
|
unsigned int baud = hw->baud;
|
|
|
if (t) {
|
if (t) {
|
dev_dbg(&spi->dev, "%s: %u bpw, %d hz\n",
|
|
__func__, t->bits_per_word, t->speed_hz);
|
|
if (t->speed_hz && t->speed_hz != hw->speed_hz)
|
if (t->speed_hz && t->speed_hz != hw->speed_hz)
|
baud = tiny_spi_baud(spi, t->speed_hz);
|
baud = tiny_spi_baud(spi, t->speed_hz);
|
}
|
}
|
writel(baud, hw->base + TINY_SPI_BAUD);
|
writel(baud, hw->base + TINY_SPI_BAUD);
|
|
writel(hw->mode, hw->base + TINY_SPI_CONTROL);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static int tiny_spi_setup(struct spi_device *spi)
|
static int tiny_spi_setup(struct spi_device *spi)
|
{
|
{
|
struct tiny_spi *hw = to_hw(spi);
|
struct tiny_spi *hw = to_hw(spi);
|
|
|
dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n",
|
|
__func__, spi->mode, spi->bits_per_word, spi->max_speed_hz);
|
|
if (spi->max_speed_hz != hw->speed_hz) {
|
if (spi->max_speed_hz != hw->speed_hz) {
|
hw->speed_hz = spi->max_speed_hz;
|
hw->speed_hz = spi->max_speed_hz;
|
hw->baud = tiny_spi_baud(spi, hw->speed_hz);
|
hw->baud = tiny_spi_baud(spi, hw->speed_hz);
|
}
|
}
|
|
hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
#ifndef CONFIG_TINY_SPI_IDLE_VAL
|
#ifndef CONFIG_TINY_SPI_IDLE_VAL
|
# define CONFIG_TINY_SPI_IDLE_VAL 0xff
|
# define CONFIG_TINY_SPI_IDLE_VAL 0x00
|
#endif
|
#endif
|
|
|
static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
|
static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
|
{
|
{
|
struct tiny_spi *hw = to_hw(spi);
|
struct tiny_spi *hw = to_hw(spi);
|
const u8 *txp = t->tx_buf;
|
const u8 *txp = t->tx_buf;
|
u8 *rxp = t->rx_buf;
|
u8 *rxp = t->rx_buf;
|
uint i;
|
unsigned int i;
|
|
|
dev_dbg(&spi->dev, "%s: tx %p, rx %p, len %d\n",
|
if (hw->irq >= 0 && hw->interrupt) {
|
__func__, t->tx_buf, t->rx_buf, t->len);
|
/* 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;
|
|
init_completion(&hw->done);
|
|
|
|
/* send the first byte */
|
|
if (t->len > 1) {
|
|
writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
|
|
hw->base + TINY_SPI_TXDATA);
|
|
hw->txc++;
|
|
writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
|
|
hw->base + TINY_SPI_TXDATA);
|
|
hw->txc++;
|
|
writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
|
|
} else {
|
|
writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
|
|
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 */
|
/* we need to tighten the transfer loop */
|
if (txp && rxp) {
|
|
writeb(*txp++, hw->base + TINY_SPI_TXDATA);
|
writeb(*txp++, hw->base + TINY_SPI_TXDATA);
|
if (t->len > 1) {
|
if (t->len > 1) {
|
writeb(*txp++, hw->base + TINY_SPI_TXDATA);
|
writeb(*txp++, hw->base + TINY_SPI_TXDATA);
|
for (i = 2; i < t->len; i++) {
|
for (i = 2; i < t->len; i++) {
|
u8 rx, tx = *txp++;
|
u8 rx, tx = *txp++;
|
Line 197... |
Line 225... |
}
|
}
|
|
|
static irqreturn_t tiny_spi_irq(int irq, void *dev)
|
static irqreturn_t tiny_spi_irq(int irq, void *dev)
|
{
|
{
|
struct tiny_spi *hw = dev;
|
struct tiny_spi *hw = dev;
|
|
|
writeb(0, hw->base + TINY_SPI_STATUS);
|
writeb(0, hw->base + TINY_SPI_STATUS);
|
|
if (hw->rxc + 1 == hw->len) {
|
|
if (hw->rxp)
|
|
*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
|
|
hw->rxc++;
|
complete(&hw->done);
|
complete(&hw->done);
|
|
} else {
|
|
if (hw->rxp)
|
|
*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
|
|
hw->rxc++;
|
|
if (hw->txc < hw->len) {
|
|
writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
|
|
hw->base + TINY_SPI_TXDATA);
|
|
hw->txc++;
|
|
writeb(TINY_SPI_STATUS_TXR,
|
|
hw->base + TINY_SPI_STATUS);
|
|
} else {
|
|
writeb(TINY_SPI_STATUS_TXE,
|
|
hw->base + TINY_SPI_STATUS);
|
|
}
|
|
}
|
return IRQ_HANDLED;
|
return IRQ_HANDLED;
|
}
|
}
|
|
|
static int __init tiny_spi_probe(struct platform_device *pdev)
|
#ifdef CONFIG_OF
|
|
static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
|
|
struct tiny_spi *hw)
|
|
{
|
|
const __be32 *val;
|
|
|
|
hw->bitbang.master->dev.of_node = pdev->dev.of_node;
|
|
val = of_get_property(pdev->dev.of_node, "baud-width", NULL);
|
|
if (val)
|
|
hw->baudwidth = be32_to_cpup(val);
|
|
val = of_get_property(pdev->dev.of_node, "clock-frequency", NULL);
|
|
if (val)
|
|
hw->freq = be32_to_cpup(val);
|
|
val = of_get_property(pdev->dev.of_node, "interrupt-driven", NULL);
|
|
if (val)
|
|
hw->interrupt = be32_to_cpup(val);
|
|
return 0;
|
|
}
|
|
#else
|
|
static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
|
|
struct tiny_spi *hw)
|
{
|
{
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
static int __devinit tiny_spi_probe(struct platform_device *pdev)
|
|
{
|
|
struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
|
struct tiny_spi *hw;
|
struct tiny_spi *hw;
|
struct spi_master *master;
|
struct spi_master *master;
|
struct resource *res;
|
struct resource *res;
|
int err = 0;
|
int err = 0;
|
|
|
master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
|
master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
|
if (master == NULL) {
|
if (master == NULL) {
|
dev_err(&pdev->dev, "No memory for spi_master\n");
|
dev_err(&pdev->dev, "No memory for spi_master\n");
|
err = -ENOMEM;
|
err = -ENOMEM;
|
goto err_nomem;
|
goto err_no_mem;
|
}
|
}
|
|
|
hw = spi_master_get_devdata(master);
|
|
memset(hw, 0, sizeof(struct tiny_spi));
|
|
|
|
hw->master = spi_master_get(master);
|
|
hw->dev = &pdev->dev;
|
|
|
|
platform_set_drvdata(pdev, hw);
|
|
init_completion(&hw->done);
|
|
|
|
/* setup the master state. */
|
/* setup the master state. */
|
master->bus_num = pdev->id;
|
master->bus_num = pdev->id;
|
master->num_chipselect = 256;
|
master->num_chipselect = 255;
|
master->mode_bits = SPI_CS_HIGH;
|
master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
|
|
master->setup = tiny_spi_setup;
|
|
|
/* setup the state for the bitbang driver */
|
hw = spi_master_get_devdata(master);
|
|
platform_set_drvdata(pdev, hw);
|
|
|
hw->bitbang.master = hw->master;
|
/* setup the state for the bitbang driver */
|
|
hw->bitbang.master = spi_master_get(master);
|
|
if (hw->bitbang.master == NULL) {
|
|
dev_err(&pdev->dev, "Cannot get device\n");
|
|
err = -ENODEV;
|
|
goto err_no_dev;
|
|
}
|
hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
|
hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
|
hw->bitbang.chipselect = tiny_spi_chipselect;
|
hw->bitbang.chipselect = tiny_spi_chipselect;
|
hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
|
hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
|
hw->bitbang.master->setup = tiny_spi_setup;
|
|
|
|
dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
|
|
|
|
/* find and map our resources */
|
/* find and map our resources */
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
if (res == NULL) {
|
if (res == NULL) {
|
dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
|
dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
|
err = -ENOENT;
|
err = -ENOENT;
|
goto err_no_iores;
|
goto err_no_iores;
|
}
|
}
|
|
hw->base = ioremap(res->start, (res->end - res->start) + 1);
|
hw->ioarea = request_mem_region(res->start, (res->end - res->start) + 1,
|
|
pdev->name);
|
|
|
|
if (hw->ioarea == NULL) {
|
|
dev_err(&pdev->dev, "Cannot reserve region\n");
|
|
err = -ENXIO;
|
|
goto err_no_iores;
|
|
}
|
|
|
|
hw->base =
|
|
ioremap(res->start, (res->end - res->start) + 1);
|
|
if (hw->base == 0) {
|
if (hw->base == 0) {
|
dev_err(&pdev->dev, "Cannot map IO\n");
|
dev_err(&pdev->dev, "Cannot map IO\n");
|
err = -ENXIO;
|
err = -ENXIO;
|
goto err_no_iomap;
|
goto err_no_iomap;
|
}
|
}
|
|
/* irq is optional */
|
hw->irq = platform_get_irq(pdev, 0);
|
hw->irq = platform_get_irq(pdev, 0);
|
if (hw->irq < 0) {
|
if (hw->irq >= 0) {
|
dev_err(&pdev->dev, "No IRQ specified\n");
|
init_completion(&hw->done);
|
err = -ENOENT;
|
|
goto err_no_irq;
|
|
}
|
|
|
|
err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
|
err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
|
if (err) {
|
if (err) {
|
dev_err(&pdev->dev, "Cannot claim IRQ\n");
|
dev_err(&pdev->dev, "Cannot claim IRQ\n");
|
goto err_no_irq;
|
goto err_no_irq;
|
}
|
}
|
|
}
|
hw->freq = 100000000;
|
/* find platform data */
|
hw->baudwidth = 7;
|
if (platp) {
|
hw->baud = 1;
|
hw->freq = platp->freq;
|
|
hw->baudwidth = platp->baudwidth;
|
dev_info(hw->dev, "base %p, irq %d\n", hw->base, hw->irq);
|
hw->interrupt = platp->interrupt;
|
|
} else {
|
|
err = tiny_spi_of_probe(pdev, hw);
|
|
if (err)
|
|
goto err_no_of;
|
|
}
|
|
|
/* register our spi controller */
|
/* register our spi controller */
|
err = spi_bitbang_start(&hw->bitbang);
|
err = spi_bitbang_start(&hw->bitbang);
|
if (err) {
|
if (err) {
|
dev_err(&pdev->dev, "Failed to register SPI master\n");
|
dev_err(&pdev->dev, "Failed to register SPI master\n");
|
goto err_register;
|
goto err_register;
|
}
|
}
|
|
dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
|
|
|
return 0;
|
return 0;
|
|
|
err_register:
|
err_register:
|
|
if (hw->irq >= 0)
|
free_irq(hw->irq, hw);
|
free_irq(hw->irq, hw);
|
err_no_irq:
|
err_no_irq:
|
iounmap((void *)hw->base);
|
iounmap((void *)hw->base);
|
err_no_iomap:
|
err_no_iomap:
|
release_resource(hw->ioarea);
|
|
kfree(hw->ioarea);
|
|
err_no_iores:
|
err_no_iores:
|
spi_master_put(hw->master);;
|
spi_master_put(master);
|
err_nomem:
|
err_no_mem:
|
|
err_no_of:
|
|
err_no_dev:
|
return err;
|
return err;
|
}
|
}
|
|
|
static int __exit tiny_spi_remove(struct platform_device *dev)
|
static int __devexit tiny_spi_remove(struct platform_device *dev)
|
{
|
{
|
struct tiny_spi *hw = platform_get_drvdata(dev);
|
struct tiny_spi *hw = platform_get_drvdata(dev);
|
|
struct spi_master *master = hw->bitbang.master;
|
|
|
platform_set_drvdata(dev, NULL);
|
spi_bitbang_stop(&hw->bitbang);
|
|
|
spi_unregister_master(hw->master);
|
|
|
|
|
if (hw->irq >= 0)
|
free_irq(hw->irq, hw);
|
free_irq(hw->irq, hw);
|
iounmap((void *)hw->base);
|
iounmap((void *)hw->base);
|
|
|
release_resource(hw->ioarea);
|
platform_set_drvdata(dev, NULL);
|
kfree(hw->ioarea);
|
spi_master_put(master);
|
|
|
spi_master_put(hw->master);
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
#ifdef CONFIG_OF
|
|
static struct of_device_id oc_tiny_spi_match[] = {
|
|
{
|
|
.compatible = "opencores,oc_tiny_spi",
|
|
},
|
|
{},
|
|
}
|
|
MODULE_DEVICE_TABLE(of, oc_tiny_spi_match);
|
|
#endif
|
|
|
static struct platform_driver tiny_spidrv = {
|
static struct platform_driver tiny_spidrv = {
|
.remove = __exit_p(tiny_spi_remove),
|
.remove = __devexit_p(tiny_spi_remove),
|
.driver = {
|
.driver = {
|
.name = DRV_NAME,
|
.name = DRV_NAME,
|
.owner = THIS_MODULE,
|
.owner = THIS_MODULE,
|
.pm = NULL,
|
.pm = NULL,
|
|
#ifdef CONFIG_OF
|
|
.of_match_table = oc_tiny_spi_match,
|
|
#endif
|
},
|
},
|
};
|
};
|
|
|
static int __init tiny_spi_init(void)
|
static int __init tiny_spi_init(void)
|
{
|
{
|
Line 347... |
Line 420... |
}
|
}
|
|
|
module_init(tiny_spi_init);
|
module_init(tiny_spi_init);
|
module_exit(tiny_spi_exit);
|
module_exit(tiny_spi_exit);
|
|
|
MODULE_DESCRIPTION("Opencore tiny_spi driver");
|
MODULE_DESCRIPTION("OpenCores tiny SPI driver");
|
MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
|
MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
|
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
MODULE_ALIAS("platform:" DRV_NAME);
|
MODULE_ALIAS("platform:" DRV_NAME);
|
|
|
No newline at end of file
|
No newline at end of file
|