* [patch 2.6.15-rc3-mm1 2/4] ads7836 uses spi_driver
[not found] <200512031556.32214.david-b@pacbell.net>
@ 2005-12-04 0:23 ` David Brownell
2005-12-04 0:26 ` [patch 2.6.15-rc3-mm1 3/4] add spi_bitbang driver David Brownell
2005-12-04 0:28 ` [patch 2.6.15-rc3-mm1 4/4] mtd_dataflash uses spi_driver David Brownell
2 siblings, 0 replies; 3+ messages in thread
From: David Brownell @ 2005-12-04 0:23 UTC (permalink / raw)
To: Andrew Morton, linux-kernel; +Cc: Mark Underwood, Stephen Street, Vitaly Wool
[-- Attachment #1: Type: text/plain, Size: 126 bytes --]
This updates the ads7846 driver to match the preceding patch
(requring spi_driver); and tweaks a handful of comments.
- Dave
[-- Attachment #2: ads7846-refresh.patch --]
[-- Type: text/x-diff, Size: 6864 bytes --]
This updates the ads7864 driver to use the new "spi_driver" struct,
and includes some minor unrelated cleanup.
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
--- mm-tmp.orig/include/linux/spi/ads7846.h 2005-12-03 14:12:40.000000000 -0800
+++ mm-tmp/include/linux/spi/ads7846.h 2005-12-03 15:13:50.000000000 -0800
@@ -1,7 +1,7 @@
/* linux/spi/ads7846.h */
/* Touchscreen characteristics vary between boards and models. The
- * platform_data for the device's "struct device" holts this information.
+ * platform_data for the device's "struct device" holds this information.
*
* It's OK if the min/max values are zero.
*/
--- mm-tmp.orig/drivers/input/touchscreen/ads7846.c 2005-12-03 14:12:41.000000000 -0800
+++ mm-tmp/drivers/input/touchscreen/ads7846.c 2005-12-03 15:15:00.000000000 -0800
@@ -345,19 +345,15 @@ static irqreturn_t ads7846_irq(int irq,
/*--------------------------------------------------------------------------*/
-/* non-empty "extra" is needed before 2.6.14-git5 or so */
-#define EXTRA // , u32 level
-#define EXTRA2 // , 0
-
static int
-ads7846_suspend(struct device *dev, pm_message_t message EXTRA)
+ads7846_suspend(struct spi_device *spi, pm_message_t message)
{
- struct ads7846 *ts = dev_get_drvdata(dev);
+ struct ads7846 *ts = dev_get_drvdata(&spi->dev);
unsigned long flags;
spin_lock_irqsave(&ts->lock, flags);
- ts->spi->dev.power.power_state = message;
+ spi->dev.power.power_state = message;
/* are we waiting for IRQ, or polling? */
if (!ts->pendown) {
@@ -387,36 +383,35 @@ ads7846_suspend(struct device *dev, pm_m
return 0;
}
-static int ads7846_resume(struct device *dev EXTRA)
+static int ads7846_resume(struct spi_device *spi)
{
- struct ads7846 *ts = dev_get_drvdata(dev);
+ struct ads7846 *ts = dev_get_drvdata(&spi->dev);
ts->irq_disabled = 0;
enable_irq(ts->spi->irq);
- dev->power.power_state = PMSG_ON;
+ spi->dev.power.power_state = PMSG_ON;
return 0;
}
-static int __init ads7846_probe(struct device *dev)
+static int __devinit ads7846_probe(struct spi_device *spi)
{
- struct spi_device *spi = to_spi_device(dev);
struct ads7846 *ts;
- struct ads7846_platform_data *pdata = dev->platform_data;
+ struct ads7846_platform_data *pdata = spi->dev.platform_data;
struct spi_transfer *x;
if (!spi->irq) {
- dev_dbg(dev, "no IRQ?\n");
+ dev_dbg(&spi->dev, "no IRQ?\n");
return -ENODEV;
}
if (!pdata) {
- dev_dbg(dev, "no platform data?\n");
+ dev_dbg(&spi->dev, "no platform data?\n");
return -ENODEV;
}
/* don't exceed max specified sample rate */
if (spi->max_speed_hz > (125000 * 16)) {
- dev_dbg(dev, "f(sample) %d KHz?\n",
+ dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
(spi->max_speed_hz/16)/1000);
return -EINVAL;
}
@@ -430,7 +425,7 @@ static int __init ads7846_probe(struct d
if (!(ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL)))
return -ENOMEM;
- dev_set_drvdata(dev, ts);
+ dev_set_drvdata(&spi->dev, ts);
ts->spi = spi;
spi->dev.power.power_state = PMSG_ON;
@@ -445,9 +440,9 @@ static int __init ads7846_probe(struct d
init_input_dev(&ts->input);
- ts->input.dev = dev;
+ ts->input.dev = &spi->dev;
ts->input.name = "ADS784x Touchscreen";
- snprintf(ts->phys, sizeof ts->phys, "%s/input0", dev->bus_id);
+ snprintf(ts->phys, sizeof ts->phys, "%s/input0", spi->dev.bus_id);
ts->input.phys = ts->phys;
ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
@@ -511,65 +506,68 @@ static int __init ads7846_probe(struct d
ts->msg.context = ts;
if (request_irq(spi->irq, ads7846_irq, SA_SAMPLE_RANDOM,
- dev->bus_id, ts)) {
- dev_dbg(dev, "irq %d busy?\n", spi->irq);
+ spi->dev.bus_id, ts)) {
+ dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
input_unregister_device(&ts->input);
kfree(ts);
return -EBUSY;
}
set_irq_type(spi->irq, IRQT_FALLING);
- dev_info(dev, "touchscreen, irq %d\n", spi->irq);
+ dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
/* take a first sample, leaving nPENIRQ active; avoid
* the touchscreen, in case it's not connected.
*/
- (void) ads7846_read12_ser(dev,
+ (void) ads7846_read12_ser(&spi->dev,
READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
/* ads7843/7845 don't have temperature sensors, and
* use the other sensors a bit differently too
*/
if (ts->model == 7846) {
- device_create_file(dev, &dev_attr_temp0);
- device_create_file(dev, &dev_attr_temp1);
+ device_create_file(&spi->dev, &dev_attr_temp0);
+ device_create_file(&spi->dev, &dev_attr_temp1);
}
if (ts->model != 7845)
- device_create_file(dev, &dev_attr_vbatt);
- device_create_file(dev, &dev_attr_vaux);
+ device_create_file(&spi->dev, &dev_attr_vbatt);
+ device_create_file(&spi->dev, &dev_attr_vaux);
return 0;
}
-static int __exit ads7846_remove(struct device *dev)
+static int __devexit ads7846_remove(struct spi_device *spi)
{
- struct ads7846 *ts = dev_get_drvdata(dev);
+ struct ads7846 *ts = dev_get_drvdata(&spi->dev);
- ads7846_suspend(dev, PMSG_SUSPEND EXTRA2);
+ ads7846_suspend(spi, PMSG_SUSPEND);
free_irq(ts->spi->irq, ts);
if (ts->irq_disabled)
enable_irq(ts->spi->irq);
if (ts->model == 7846) {
- device_remove_file(dev, &dev_attr_temp0);
- device_remove_file(dev, &dev_attr_temp1);
+ device_remove_file(&spi->dev, &dev_attr_temp0);
+ device_remove_file(&spi->dev, &dev_attr_temp1);
}
if (ts->model != 7845)
- device_remove_file(dev, &dev_attr_vbatt);
- device_remove_file(dev, &dev_attr_vaux);
+ device_remove_file(&spi->dev, &dev_attr_vbatt);
+ device_remove_file(&spi->dev, &dev_attr_vaux);
input_unregister_device(&ts->input);
kfree(ts);
- dev_dbg(dev, "unregistered touchscreen\n");
+ dev_dbg(&spi->dev, "unregistered touchscreen\n");
return 0;
}
-static struct device_driver ads7846_driver = {
- .name = "ads7846",
- .bus = &spi_bus_type,
+static struct spi_driver ads7846_driver = {
+ .driver = {
+ .name = "ads7846",
+ .bus = &spi_bus_type,
+ .owner = THIS_MODULE,
+ },
.probe = ads7846_probe,
- .remove = __exit_p(ads7846_remove),
+ .remove = __devexit_p(ads7846_remove),
.suspend = ads7846_suspend,
.resume = ads7846_resume,
};
@@ -594,18 +592,20 @@ static int __init ads7846_init(void)
// PXA:
// also Dell Axim X50
// also HP iPaq H191x/H192x/H415x/H435x
- // also Intel Lubbock (alternate to UCB1400)
+ // also Intel Lubbock (additional to UCB1400; as temperature sensor)
// also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
+ // Atmel at91sam9261-EK uses ads7843
+
// also various AMD Au1x00 devel boards
- return driver_register(&ads7846_driver);
+ return spi_register_driver(&ads7846_driver);
}
module_init(ads7846_init);
static void __exit ads7846_exit(void)
{
- driver_unregister(&ads7846_driver);
+ spi_unregister_driver(&ads7846_driver);
#ifdef CONFIG_ARCH_OMAP
if (machine_is_omap_osk()) {
^ permalink raw reply [flat|nested] 3+ messages in thread
* [patch 2.6.15-rc3-mm1 3/4] add spi_bitbang driver
[not found] <200512031556.32214.david-b@pacbell.net>
2005-12-04 0:23 ` [patch 2.6.15-rc3-mm1 2/4] ads7836 uses spi_driver David Brownell
@ 2005-12-04 0:26 ` David Brownell
2005-12-04 0:28 ` [patch 2.6.15-rc3-mm1 4/4] mtd_dataflash uses spi_driver David Brownell
2 siblings, 0 replies; 3+ messages in thread
From: David Brownell @ 2005-12-04 0:26 UTC (permalink / raw)
To: Andrew Morton, linux-kernel
Cc: linuxMark Underwood, Stephen Street, Vitaly Wool
[-- Attachment #1: Type: text/plain, Size: 833 bytes --]
Here's a mostly-working bitbang driver for SPI. It seems to
handle SPI mode 0 just fine; I've done a bunch of flash updates
through a parport wrapper, and no bits were damaged.
Eventually I hope this will serve as a reference implementation,
showing how the various API operations map to bits on the wire.
(Contrast it to the I2C bitbangers, also. Simpler...)
Likewise, folk looking at getting more standard SPI support on
their hardware could start with this. It's reasonably common
for the pins used for SPI controllers to be accessible as GPIOs,
so this bitbanger might be used until a real hardware driver was
debugged and tuned. (That's assuming there _is_ a dedicated
SPI controller to use!)
Comments appreciated. Also testing with other SPI modes,
and potentially code tweaks to make the banging go faster.
- Dave
[-- Attachment #2: bitbang_spi.patch --]
[-- Type: text/x-diff, Size: 15412 bytes --]
This adds a bitbanging spi master, hooking up to board/adapter-specific glue
code which knows how to set and read the signals (gpios etc).
This code kicks in after the glue code creates a platform_device with the
right platform_data. That data includes I/O loops, which will usually
come from expanding an inline function (provided in the header). One goal
is that the I/O loops should be easily optimized down to a few GPIO register
accesses, in common cases, for speed and minimized overhead.
This understands all the currently defined protocol tweaking options in the
SPI framework, and might eventually serve as as reference implementation.
- different word sizes (1..32 bits)
- differing clock rates
- SPI modes differing by CPOL (affecting chip select and I/O loops)
- SPI modes differing by CPHA (affecting I/O loops)
- delays (usecs) after transfers
- temporarily deselecting chips in mid-transfer
This version is circulating mostly as an RFC. SPI Mode 0 works talking
to a Flash chip, but I'm still chasing down some bugs that show up after
I rmmod the parport adapter.
--- mm-tmp.orig/drivers/spi/Kconfig 2005-12-03 14:23:27.000000000 -0800
+++ mm-tmp/drivers/spi/Kconfig 2005-12-03 15:45:32.000000000 -0800
@@ -66,6 +66,19 @@ config SPI_MASTER
comment "SPI Master Controller Drivers"
depends on SPI_MASTER
+config SPI_BITBANG
+ tristate "Bitbanging SPI master"
+ depends on SPI_MASTER && EXPERIMENTAL
+ help
+ With a few GPIO pins, your system can bitbang the SPI protocol.
+ Choose this if you need SPI support through I/O pins (GPIO,
+ parallel port, etc).
+
+ Make sure you initialize one platform device object for each SPI
+ master device on your system, using "spi_bitbang" as the device
+ name with platform_data saying how to use which pins. That will
+ often be done using static board-specific tables in board-specific
+ (or adapter-specific) setup code.
#
# Add new SPI master controllers in alphabetical order above this line
--- mm-tmp.orig/drivers/spi/Makefile 2005-12-03 14:17:41.000000000 -0800
+++ mm-tmp/drivers/spi/Makefile 2005-12-03 15:45:32.000000000 -0800
@@ -11,6 +11,7 @@ endif
obj-$(CONFIG_SPI_MASTER) += spi.o
# SPI master controller drivers (bus)
+obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o
# ... add above this line ...
# SPI protocol drivers (device/link on bus)
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ mm-tmp/include/linux/spi/spi_bitbang.h 2005-12-03 15:45:32.000000000 -0800
@@ -0,0 +1,109 @@
+#ifndef __SPI_BITBANG_H
+#define __SPI_BITBANG_H
+
+/* delarations for interface between core/algorithm and the part of the
+ * driver that actually knows what pins do what
+ */
+
+/**
+ * struct spi_bitbang_plat_data - pass hardware-specific knowledge
+ */
+struct spi_bitbang_plat_data {
+ unsigned num_chipselect;
+
+ /* enable/disable the chip; before enable, sets phase */
+ void (*chipselect)(struct spi_device *spi, int is_on);
+
+ /* probably these should become txrx_mode0(), txrx_mode1(),
+ * and so on, when speed matters.
+ */
+ u32 (*txrx_word)(struct spi_device *spi,
+ unsigned nsecs,
+ u32 word, u8 bits);
+};
+
+extern struct platform_driver spi_bitbang_driver;
+
+
+/* FIXME just name functions after SCK and MOSI */
+enum spi_bitbang_pin {
+ SPI_BITBANG_SCK,
+ SPI_BITBANG_MOSI,
+};
+
+#endif /* __SPI_BITBANG_H */
+
+#ifdef EXPAND_BITBANG_TXRX
+
+/*
+ * The code that knows what GPIO pins do what should have declared three
+ * functions, ideally as inlines, before #defining EXPAND_BITBANG_TXRX
+ * and including this header:
+ *
+ * void setpin(struct spi_device *, enum spi_bitbang_pin, int is_on);
+ * int getmiso(struct spi_device *);
+ * void spidelay(unsigned);
+ *
+ * A non-inlined routine would call bitbang_txrx_*() routines. The
+ * main loop could easily compile down to a handful of instructions,
+ * especially if the delay is a NOP (to run at peak speed).
+ *
+ * Since this is software, the timings may not be exactly what your board's
+ * chips need ... there may be several reasons you'd need to tweak timings
+ * in this routine, not just make to make it faster or slower to match a
+ * particular CPU clock rate.
+ */
+
+// SPI_MODE_0 seems to work fine
+
+static inline u32
+bitbang_txrx_be_cpha0(struct spi_device *spi,
+ unsigned nsecs, unsigned cpol,
+ u32 word, u8 bits)
+{
+ /* if (cpol) this is SPI_MODE_0; else this is SPI_MODE_2 */
+
+ /* clock starts at inactive polarity */
+ for (word <<= (32 - bits); likely(bits); bits--) {
+
+ /* setup MSB (to slave) on trailing edge */
+ setpin(spi, SPI_BITBANG_MOSI, word & (1 << 31));
+ spidelay(nsecs); /* T(setup) */
+
+ setpin(spi, SPI_BITBANG_SCK, !cpol);
+ spidelay(nsecs);
+
+ /* sample MSB (from slave) on leading edge */
+ word <<= 1;
+ word |= getmiso(spi);
+ setpin(spi, SPI_BITBANG_SCK, cpol);
+ }
+ return word;
+}
+
+static inline u32
+bitbang_txrx_be_cpha1(struct spi_device *spi,
+ unsigned nsecs, unsigned cpol,
+ u32 word, u8 bits)
+{
+ /* if (cpol) this is SPI_MODE_1; else this is SPI_MODE_3 */
+
+ /* clock starts at inactive polarity */
+ for (word <<= (32 - bits); likely(bits); bits--) {
+
+ /* setup MSB (to slave) on leading edge */
+ setpin(spi, SPI_BITBANG_SCK, !cpol);
+ setpin(spi, SPI_BITBANG_MOSI, word & (1 << 31));
+ spidelay(nsecs);
+
+ setpin(spi, SPI_BITBANG_SCK, cpol);
+ spidelay(nsecs); /* T(setup) */
+
+ /* sample MSB (from slave) on trailing edge */
+ word <<= 1;
+ word |= getmiso(spi);
+ }
+ return word;
+}
+
+#endif /* EXPAND_BITBANG_TXRX */
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ mm-tmp/drivers/spi/spi_bitbang.c 2005-12-03 15:45:32.000000000 -0800
@@ -0,0 +1,401 @@
+/*
+ * spi_bitbang.c - bitbanging SPI driver
+ *
+ * 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 <linux/config.h>
+#include <linux/init.h>
+#include <linux/spinlock.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/platform_device.h>
+
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+
+
+struct spi_bitbang {
+ struct work_struct work;
+ struct workqueue_struct *workqueue;
+
+ spinlock_t lock;
+ struct list_head queue;
+
+ struct spi_master *master;
+
+ void (*chipselect)(struct spi_device *spi, int is_on);
+ u32 (*txrx_word)(struct spi_device *spi,
+ unsigned nsecs,
+ u32 word, u8 bits);
+};
+
+/* spi_bitbang_cs is in spi_device->controller_state.
+ *
+ * chipselect() etc probably use use spi_device->controller_data
+ * to remember chipselect, clock, and data i/o pins.
+ */
+struct spi_bitbang_cs {
+ unsigned nsecs; /* (clock cycle time)/2 */
+ unsigned (*txrx_buf)(struct spi_bitbang *, struct spi_device *,
+ unsigned, struct spi_transfer *);
+};
+
+/*----------------------------------------------------------------------*/
+
+static unsigned bitbang_txrx_8(
+ struct spi_bitbang *bitbang,
+ struct spi_device *spi,
+ unsigned ns,
+ struct spi_transfer *t
+) {
+ unsigned bits = spi->bits_per_word;
+ unsigned count = t->len;
+ const u8 *tx = t->tx_buf;
+ u8 *rx = t->rx_buf;
+
+ while (likely(count > 0)) {
+ u8 word = 0;
+
+ if (tx)
+ word = *tx++;
+ word = bitbang->txrx_word(spi, ns, word, bits);
+ if (rx)
+ *rx++ = word;
+ count -= 1;
+ }
+ return t->len - count;
+}
+
+static unsigned bitbang_txrx_16(
+ struct spi_bitbang *bitbang,
+ struct spi_device *spi,
+ unsigned ns,
+ struct spi_transfer *t
+) {
+ unsigned bits = spi->bits_per_word;
+ unsigned count = t->len;
+ const u16 *tx = t->tx_buf;
+ u16 *rx = t->rx_buf;
+
+ while (likely(count > 1)) {
+ u16 word = 0;
+
+ if (tx)
+ word = *tx++;
+ word = bitbang->txrx_word(spi, ns, word, bits);
+ if (rx)
+ *rx++ = word;
+ count -= 2;
+ }
+ return t->len - count;
+}
+
+static unsigned bitbang_txrx_32(
+ struct spi_bitbang *bitbang,
+ struct spi_device *spi,
+ unsigned ns,
+ struct spi_transfer *t
+) {
+ unsigned bits = spi->bits_per_word;
+ unsigned count = t->len;
+ const u32 *tx = t->tx_buf;
+ u32 *rx = t->rx_buf;
+
+ while (likely(count > 3)) {
+ u32 word = 0;
+
+ if (tx)
+ word = *tx++;
+ word = bitbang->txrx_word(spi, ns, word, bits);
+ if (rx)
+ *rx++ = word;
+ count -= 4;
+ }
+ return t->len - count;
+}
+
+static void bitbang_work(void *_bitbang)
+{
+ struct spi_bitbang *bitbang = _bitbang;
+ unsigned long flags;
+
+ spin_lock_irqsave(&bitbang->lock, flags);
+ while (!list_empty(&bitbang->queue)) {
+ struct spi_message *m;
+ struct spi_device *spi;
+ struct spi_bitbang_cs *cs;
+ unsigned nsecs;
+ struct spi_transfer *t;
+ unsigned tmp;
+ unsigned chipselect;
+ int status;
+
+ m = container_of(bitbang->queue.next, struct spi_message,
+ queue);
+ list_del_init(&m->queue);
+ spin_unlock_irqrestore(&bitbang->lock, flags);
+
+ spi = m->spi;
+ cs = spi->controller_state;
+ nsecs = cs->nsecs;
+ t = m->transfers;
+ tmp = 0;
+ chipselect = 0;
+ status = 0;
+
+ ndelay(nsecs);
+
+ for (;;t++) {
+
+ /* set up default clock polarity and select */
+ if (!chipselect) {
+ bitbang->chipselect(spi, 1);
+ ndelay(nsecs);
+ }
+ if (!t->tx_buf && !t->rx_buf && t->len) {
+ status = -EINVAL;
+ break;
+ }
+
+ /* transfer data */
+ status = cs->txrx_buf(bitbang, spi, nsecs, t);
+ if (status != t->len) {
+ if (status > 0)
+ status = -EMSGSIZE;
+ break;
+ }
+ m->actual_length += status;
+ status = 0;
+
+ /* protocol tweaks before next transfer */
+ if (t->delay_usecs)
+ udelay(t->delay_usecs);
+
+ tmp++;
+ if (tmp >= m->n_transfer)
+ break;
+
+ chipselect = !t->cs_change;
+ if (chipselect);
+ continue;
+
+ bitbang->chipselect(spi, 0);
+
+ /* REVISIT do we want the udelay here instead? */
+ msleep(1);
+ }
+
+ tmp = m->n_transfer - 1;
+ tmp = m->transfers[tmp].cs_change;
+
+ m->status = status;
+ m->complete(m->context);
+
+ ndelay(2 * nsecs);
+ bitbang->chipselect(spi, status == 0 && tmp);
+ ndelay(nsecs);
+
+ spin_lock_irqsave(&bitbang->lock, flags);
+ }
+ spin_unlock_irqrestore(&bitbang->lock, flags);
+
+}
+
+/*----------------------------------------------------------------------*/
+
+static int bitbang_setup(struct spi_device *spi)
+{
+ struct spi_bitbang_cs *cs = spi->controller_state;
+ struct spi_bitbang *bitbang;
+
+ if (!cs) {
+ cs = kzalloc(sizeof *cs, SLAB_KERNEL);
+ if (!cs)
+ return -ENOMEM;
+ spi->controller_state = cs;
+ }
+ bitbang = class_get_devdata(&spi->master->cdev);
+
+ if (!spi->bits_per_word)
+ spi->bits_per_word = 8;
+ if (spi->bits_per_word <= 8)
+ cs->txrx_buf = bitbang_txrx_8;
+ else if (spi->bits_per_word <= 16)
+ cs->txrx_buf = bitbang_txrx_16;
+ else if (spi->bits_per_word <= 32)
+ cs->txrx_buf = bitbang_txrx_32;
+ else
+ return -EINVAL;
+
+ if (!spi->max_speed_hz)
+ spi->max_speed_hz = 500 * 1000;
+
+ /* nsecs = max(50, (clock period)/2), be optimistic */
+ cs->nsecs = (1000000000/2) / (spi->max_speed_hz);
+ if (cs->nsecs < 50)
+ cs->nsecs = 50;
+ if (cs->nsecs > MAX_UDELAY_MS * 1000)
+ return -EINVAL;
+
+ dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
+ __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
+ spi->bits_per_word, 2 * cs->nsecs);
+
+ /* deselect chip (low or high) */
+ bitbang->chipselect(spi, 0);
+ ndelay(cs->nsecs);
+ return 0;
+}
+
+static void bitbang_cleanup(const struct spi_device *spi)
+{
+ kfree(spi->controller_state);
+}
+
+static int bitbang_transfer(struct spi_device *spi, struct spi_message *m)
+{
+ struct spi_bitbang *bitbang;
+ unsigned long flags;
+
+ m->actual_length = 0;
+ m->status = 0;
+
+ bitbang = class_get_devdata(&spi->master->cdev);
+
+ spin_lock_irqsave(&bitbang->lock, flags);
+ list_add_tail(&m->queue, &bitbang->queue);
+ queue_work(bitbang->workqueue, &bitbang->work);
+ spin_unlock_irqrestore(&bitbang->lock, flags);
+
+ return 0;
+}
+
+/*----------------------------------------------------------------------*/
+
+static int __devinit bitbang_probe(struct platform_device *pdev)
+{
+ struct spi_bitbang_plat_data *pdata = pdev->dev.platform_data;
+ struct spi_master *master;
+ struct spi_bitbang *bitbang;
+ int status;
+
+ if (!pdata)
+ return -EINVAL;
+
+ master = spi_alloc_master(&pdev->dev, sizeof *bitbang);
+ if (!master)
+ return -ENOMEM;
+
+ if (pdev->id != -1)
+ master->bus_num = pdev->id;
+ master->setup = bitbang_setup;
+ master->transfer = bitbang_transfer;
+ master->cleanup = bitbang_cleanup;
+ master->num_chipselect = pdata->num_chipselect;
+
+ bitbang = class_get_devdata(&master->cdev);
+ bitbang->chipselect = pdata->chipselect;
+ bitbang->txrx_word = pdata->txrx_word;
+
+ INIT_WORK(&bitbang->work, bitbang_work, bitbang);
+ spin_lock_init(&bitbang->lock);
+ INIT_LIST_HEAD(&bitbang->queue);
+ bitbang->master = master;
+
+ if (class_device_get(&master->cdev) == NULL) {
+ status = -ENOMEM;
+ goto err0;
+ }
+
+ /* bridging code is allowed to know that this is where
+ * the spi_master is stored ... so it can then for example
+ * use that with spi_new_device().
+ */
+ dev_set_drvdata(&pdev->dev, master);
+
+ /* this task is the only thing to touch the SPI bits.
+ * it's OK if clock cycles stretch when it gets preempted
+ */
+ bitbang->workqueue = create_singlethread_workqueue(pdev->dev.bus_id);
+ if (bitbang->workqueue == NULL) {
+ status = -EBUSY;
+ goto err1;
+ }
+
+ /* driver may get busy before register() returns, especially
+ * if someone registered boardinfo for devices
+ */
+ status = spi_register_master(master);
+ if (status < 0)
+ goto err2;
+
+ return status;
+
+err2:
+ destroy_workqueue(bitbang->workqueue);
+err1:
+ class_device_put(&master->cdev);
+err0:
+ return status;
+}
+
+static int __devexit bitbang_remove(struct platform_device *pdev)
+{
+ struct spi_master *master;
+ struct spi_bitbang *bitbang;
+
+ master = dev_get_drvdata(&pdev->dev);
+ bitbang = class_get_devdata(&master->cdev);
+
+ /* NOTE: assumes that all children were already cleaned up,
+ * which also means there will be no pending requests...
+ */
+ WARN_ON(!list_empty(&bitbang->queue));
+ WARN_ON(!list_empty(&pdev->dev.klist_children.k_list));
+
+ destroy_workqueue(bitbang->workqueue);
+ class_device_put(&master->cdev);
+ spi_unregister_master(master);
+
+ return 0;
+}
+
+struct platform_driver spi_bitbang_driver = {
+ .driver = {
+ .name = "spi_bitbang",
+ .owner = THIS_MODULE,
+ },
+ .probe = bitbang_probe,
+ .remove = __devexit_p(bitbang_remove),
+ // suspend, resume ... stop queue after current message, restart later
+};
+EXPORT_SYMBOL_GPL(spi_bitbang_driver);
+
+
+static int __init bitbang_init(void)
+{
+ return platform_driver_register(&spi_bitbang_driver);
+}
+device_initcall(bitbang_init);
+
+static void __exit bitbang_exit(void)
+{
+ platform_driver_unregister(&spi_bitbang_driver);
+}
+module_exit(bitbang_exit);
+
+MODULE_LICENSE("GPL");
+
^ permalink raw reply [flat|nested] 3+ messages in thread
* [patch 2.6.15-rc3-mm1 4/4] mtd_dataflash uses spi_driver
[not found] <200512031556.32214.david-b@pacbell.net>
2005-12-04 0:23 ` [patch 2.6.15-rc3-mm1 2/4] ads7836 uses spi_driver David Brownell
2005-12-04 0:26 ` [patch 2.6.15-rc3-mm1 3/4] add spi_bitbang driver David Brownell
@ 2005-12-04 0:28 ` David Brownell
2 siblings, 0 replies; 3+ messages in thread
From: David Brownell @ 2005-12-04 0:28 UTC (permalink / raw)
To: Andrew Morton, linux-kernel
Cc: Andrew Victor, Mark Underwood, Stephen Street, Vitaly Wool
[-- Attachment #1: Type: text/plain, Size: 440 bytes --]
This patch should *replace* your latest mtd_dataflash patch,
instead of applying on top of things like the preceding patches.
(Sorry.)
It does the spi_driver conversion mentioned in $SUBJECT, and
also includes a couple other updates. There's a bugfix in
the READ request (don't count the 8 tx bytes with rx), a
performance speedup in ERASE (use block erase, more or less
eight times faster for JFFS2), and more probe() cleanups.
- Dave
[-- Attachment #2: dataflash.patch --]
[-- Type: text/x-diff, Size: 20259 bytes --]
This is a conversion of the AT91rm9200 DataFlash MTD driver to use the
lightweight SPI framework, and no longer be AT91-specific. It compiles
down to less than 3KBytes on ARM.
The driver allows board-specific init code to provide platform_data with
the relevant MTD partitioning information, and hotplugs.
This version has been lightly tested. Its parent at91_dataflash driver
has been pretty well banged on, although kernel.org JFFS2 dataflash support
was acting broken the last time I tried it.
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
--- mm-tmp.orig/drivers/mtd/devices/Kconfig 2005-12-03 14:05:42.000000000 -0800
+++ mm-tmp/drivers/mtd/devices/Kconfig 2005-12-03 15:22:41.000000000 -0800
@@ -255,5 +255,13 @@ config MTD_DOCPROBE_55AA
LinuxBIOS or if you need to recover a DiskOnChip Millennium on which
you have managed to wipe the first block.
+config MTD_DATAFLASH
+ tristate "Support for AT45xxx DataFlash"
+ depends on MTD && SPI_MASTER && EXPERIMENTAL
+ help
+ This enables access to AT45xxx DataFlash chips, using SPI.
+ Sometimes DataFlash chips are packaged inside MMC-format
+ cards; at this writing, the MMC stack won't handle those.
+
endmenu
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ mm-tmp/include/linux/spi/flash.h 2005-12-03 15:22:41.000000000 -0800
@@ -0,0 +1,27 @@
+#ifndef LINUX_SPI_FLASH_H
+#define LINUX_SPI_FLASH_H
+
+struct mtd_partition;
+
+/**
+ * struct flash_platform_data: board-specific flash data
+ * @name: optional flash device name (eg, as used with mtdparts=)
+ * @parts: optional array of mtd_partitions for static partitioning
+ * @nr_parts: number of mtd_partitions for static partitoning
+ *
+ * Board init code (in arch/.../mach-xxx/board-yyy.c files) can
+ * provide information about SPI flash parts (such as DataFlash) to
+ * help set up the device and its appropriate default partitioning.
+ *
+ * Note that for DataFlash, sizes for pages, blocks, and sectors are
+ * rarely powers of two; and partitions should be sector-aligned.
+ */
+struct flash_platform_data {
+ char *name;
+ struct mtd_partition *parts;
+ unsigned int nr_parts;
+
+ /* we'll likely add more ... use JEDEC IDs, etc */
+};
+
+#endif
--- mm-tmp.orig/drivers/mtd/devices/Makefile 2005-12-03 14:05:42.000000000 -0800
+++ mm-tmp/drivers/mtd/devices/Makefile 2005-12-03 15:22:41.000000000 -0800
@@ -23,3 +23,4 @@ obj-$(CONFIG_MTD_MTDRAM) += mtdram.o
obj-$(CONFIG_MTD_LART) += lart.o
obj-$(CONFIG_MTD_BLKMTD) += blkmtd.o
obj-$(CONFIG_MTD_BLOCK2MTD) += block2mtd.o
+obj-$(CONFIG_MTD_DATAFLASH) += mtd_dataflash.o
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ mm-tmp/drivers/mtd/devices/mtd_dataflash.c 2005-12-03 15:23:23.000000000 -0800
@@ -0,0 +1,633 @@
+/*
+ * Atmel AT45xxx DataFlash MTD driver for lightweight SPI framework
+ *
+ * Largely derived from at91_dataflash.c:
+ * Copyright (C) 2003-2005 SAN People (Pty) Ltd
+ *
+ * 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.
+*/
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/flash.h>
+
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+
+
+/*
+ * DataFlash is a kind of SPI flash. Most AT45 chips have two buffers in
+ * each chip, which may be used for double buffered I/O; but this driver
+ * doesn't (yet) use these for any kind of i/o overlap or prefetching.
+ *
+ * Sometimes DataFlash is packaged in MMC-format cards, although the
+ * MMC stack can't use SPI (yet), or distinguish between MMC and DataFlash
+ * protocols during enumeration.
+ */
+
+#define CONFIG_DATAFLASH_WRITE_VERIFY
+
+/* reads can bypass the buffers */
+#define OP_READ_CONTINUOUS 0xE8
+#define OP_READ_PAGE 0xD2
+
+/* group B requests can run even while status reports "busy" */
+#define OP_READ_STATUS 0xD7 /* group B */
+
+/* move data between host and buffer */
+#define OP_READ_BUFFER1 0xD4 /* group B */
+#define OP_READ_BUFFER2 0xD6 /* group B */
+#define OP_WRITE_BUFFER1 0x84 /* group B */
+#define OP_WRITE_BUFFER2 0x87 /* group B */
+
+/* erasing flash */
+#define OP_ERASE_PAGE 0x81
+#define OP_ERASE_BLOCK 0x50
+
+/* move data between buffer and flash */
+#define OP_TRANSFER_BUF1 0x53
+#define OP_TRANSFER_BUF2 0x55
+#define OP_MREAD_BUFFER1 0xD4
+#define OP_MREAD_BUFFER2 0xD6
+#define OP_MWERASE_BUFFER1 0x83
+#define OP_MWERASE_BUFFER2 0x86
+#define OP_MWRITE_BUFFER1 0x88 /* sector must be pre-erased */
+#define OP_MWRITE_BUFFER2 0x89 /* sector must be pre-erased */
+
+/* write to buffer, then write-erase to flash */
+#define OP_PROGRAM_VIA_BUF1 0x82
+#define OP_PROGRAM_VIA_BUF2 0x85
+
+/* compare buffer to flash */
+#define OP_COMPARE_BUF1 0x60
+#define OP_COMPARE_BUF2 0x61
+
+/* read flash to buffer, then write-erase to flash */
+#define OP_REWRITE_VIA_BUF1 0x58
+#define OP_REWRITE_VIA_BUF2 0x59
+
+/* newer chips report JEDEC manufacturer and device IDs; chip
+ * serial number and OTP bits; and per-sector writeprotect.
+ */
+#define OP_READ_ID 0x9F
+#define OP_READ_SECURITY 0x77
+#define OP_WRITE_SECURITY 0x9A /* OTP bits */
+
+
+struct dataflash {
+ u8 command[4];
+ char name[24];
+
+ unsigned partitioned:1;
+
+ unsigned short page_offset; /* offset in flash address */
+ unsigned int page_size; /* of bytes per page */
+
+ struct semaphore lock;
+ struct spi_device *spi;
+
+ struct mtd_info mtd;
+};
+
+/* ......................................................................... */
+
+/*
+ * Return the status of the DataFlash device.
+ */
+static inline int dataflash_status(struct spi_device *spi)
+{
+ /* NOTE: at45db321c over 25 MHz wants to write
+ * a dummy byte after the opcode...
+ */
+ return spi_w8r8(spi, OP_READ_STATUS);
+}
+
+/*
+ * Poll the DataFlash device until it is READY.
+ * This usually takes 5-20 msec or so; more for sector erase.
+ */
+static int dataflash_waitready(struct spi_device *spi)
+{
+ int status;
+
+ for (;;) {
+ status = dataflash_status(spi);
+ if (status < 0) {
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: status %d?\n",
+ spi->dev.bus_id, status);
+ status = 0;
+ }
+
+ if (status & (1 << 7)) /* RDY/nBSY */
+ return status;
+
+ msleep(3);
+ }
+}
+
+/* ......................................................................... */
+
+/*
+ * Erase pages of flash.
+ */
+static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
+{
+ struct dataflash *priv = (struct dataflash *)mtd->priv;
+ struct spi_device *spi = priv->spi;
+ struct spi_transfer x[1] = { { .tx_dma = 0, }, };
+ struct spi_message msg;
+ unsigned blocksize = priv->page_size << 3;
+ u8 *command;
+
+ DEBUG(MTD_DEBUG_LEVEL2, "%s: erase addr=0x%x len 0x%x\n",
+ spi->dev.bus_id,
+ instr->addr, instr->len);
+
+ /* Sanity checks */
+ if ((instr->addr + instr->len) > mtd->size
+ || (instr->len % priv->page_size) != 0
+ || (instr->addr % priv->page_size) != 0)
+ return -EINVAL;
+
+ x[0].tx_buf = command = priv->command;
+ x[0].len = 4;
+ msg.transfers = x;
+ msg.n_transfer = 1;
+
+ down(&priv->lock);
+ while (instr->len > 0) {
+ unsigned int pageaddr;
+ int status;
+ int do_block;
+
+ /* Calculate flash page address; use block erase (for speed) if
+ * we're at a block boundary and need to erase the whole block.
+ */
+ pageaddr = instr->addr / priv->page_size;
+ do_block = (pageaddr & 0x7) == 0 && instr->len <= blocksize;
+ pageaddr = pageaddr << priv->page_offset;
+
+ command[0] = do_block ? OP_ERASE_BLOCK : OP_ERASE_PAGE;
+ command[1] = (u8)(pageaddr >> 16);
+ command[2] = (u8)(pageaddr >> 8);
+ command[3] = 0;
+
+ DEBUG(MTD_DEBUG_LEVEL3, "ERASE %s: (%x) %x %x %x [%i]\n",
+ do_block ? "block" : "page",
+ command[0], command[1], command[2], command[3],
+ pageaddr);
+
+ status = spi_sync(spi, &msg);
+ (void) dataflash_waitready(spi);
+
+ if (status < 0) {
+ printk(KERN_ERR "%s: erase %x, err %d\n",
+ spi->dev.bus_id, pageaddr, status);
+ /* REVISIT: can retry instr->retries times; or
+ * giveup and instr->fail_addr = instr->addr;
+ */
+ continue;
+ }
+
+ if (do_block) {
+ instr->addr += blocksize;
+ instr->len -= blocksize;
+ } else {
+ instr->addr += priv->page_size;
+ instr->len -= priv->page_size;
+ }
+ }
+ up(&priv->lock);
+
+ /* Inform MTD subsystem that erase is complete */
+ instr->state = MTD_ERASE_DONE;
+ mtd_erase_callback(instr);
+
+ return 0;
+}
+
+/*
+ * Read from the DataFlash device.
+ * from : Start offset in flash device
+ * len : Amount to read
+ * retlen : About of data actually read
+ * buf : Buffer containing the data
+ */
+static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
+ size_t *retlen, u_char *buf)
+{
+ struct dataflash *priv = (struct dataflash *)mtd->priv;
+ struct spi_transfer x[2] = { { .tx_dma = 0, }, };
+ struct spi_message msg;
+ unsigned int addr;
+ u8 *command;
+ int status;
+
+ DEBUG(MTD_DEBUG_LEVEL2, "%s: read 0x%x..0x%x\n",
+ priv->spi->dev.bus_id, (unsigned)from, (unsigned)(from + len));
+
+ *retlen = 0;
+
+ /* Sanity checks */
+ if (!len)
+ return 0;
+ if (from + len > mtd->size)
+ return -EINVAL;
+
+ /* Calculate flash page/byte address */
+ addr = (((unsigned)from / priv->page_size) << priv->page_offset)
+ + ((unsigned)from % priv->page_size);
+
+ command = priv->command;
+
+ DEBUG(MTD_DEBUG_LEVEL3, "READ: (%x) %x %x %x\n",
+ command[0], command[1], command[2], command[3]);
+
+ x[0].tx_buf = command;
+ x[0].len = 8;
+ x[1].rx_buf = buf;
+ x[1].len = len;
+ msg.transfers = x;
+ msg.n_transfer = 2;
+
+ down(&priv->lock);
+
+ /* Continuous read, max clock = f(car) which may be less than
+ * the peak rate available. Some chips support commands with
+ * fewer "don't care" bytes. Both buffers stay unchanged.
+ */
+ command[0] = OP_READ_CONTINUOUS;
+ command[1] = (u8)(addr >> 16);
+ command[2] = (u8)(addr >> 8);
+ command[3] = (u8)(addr >> 0);
+ /* plus 4 "don't care" bytes */
+
+ status = spi_sync(priv->spi, &msg);
+ up(&priv->lock);
+
+ if (status >= 0) {
+ *retlen = msg.actual_length - 8;
+ status = 0;
+ } else
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: read %x..%x --> %d\n",
+ priv->spi->dev.bus_id,
+ (unsigned)from, (unsigned)(from + len),
+ status);
+ return status;
+}
+
+/*
+ * Write to the DataFlash device.
+ * to : Start offset in flash device
+ * len : Amount to write
+ * retlen : Amount of data actually written
+ * buf : Buffer containing the data
+ */
+static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
+ size_t * retlen, const u_char * buf)
+{
+ struct dataflash *priv = (struct dataflash *)mtd->priv;
+ struct spi_device *spi = priv->spi;
+ struct spi_transfer x[2] = { { .tx_dma = 0, }, };
+ struct spi_message msg;
+ unsigned int pageaddr, addr, offset, writelen;
+ size_t remaining = len;
+ u_char *writebuf = (u_char *) buf;
+ int status = -EINVAL;
+ u8 *command;
+
+ DEBUG(MTD_DEBUG_LEVEL2, "%s: write 0x%x..0x%x\n",
+ spi->dev.bus_id, (unsigned)to, (unsigned)(to + len));
+
+ *retlen = 0;
+
+ /* Sanity checks */
+ if (!len)
+ return 0;
+ if ((to + len) > mtd->size)
+ return -EINVAL;
+
+ x[0].tx_buf = command = priv->command;
+ x[0].len = 4;
+ msg.transfers = x;
+
+ pageaddr = ((unsigned)to / priv->page_size);
+ offset = ((unsigned)to % priv->page_size);
+ if (offset + len > priv->page_size)
+ writelen = priv->page_size - offset;
+ else
+ writelen = len;
+
+ down(&priv->lock);
+ while (remaining > 0) {
+ DEBUG(MTD_DEBUG_LEVEL3, "write @ %i:%i len=%i\n",
+ pageaddr, offset, writelen);
+
+ /* REVISIT:
+ * (a) each page in a sector must be rewritten at least
+ * once every 10K sibling erase/program operations.
+ * (b) for pages that are already erased, we could
+ * use WRITE+MWRITE not PROGRAM for ~30% speedup.
+ * (c) WRITE to buffer could be done while waiting for
+ * a previous MWRITE/MWERASE to complete ...
+ * (d) error handling here seems to be mostly missing.
+ *
+ * Two persistent bits per page, plus a per-sector counter,
+ * could support (a) and (b) ... we might consider using
+ * the second half of sector zero, which is just one block,
+ * to track that state. (On AT91, that sector should also
+ * support boot-from-DataFlash.)
+ */
+
+ addr = pageaddr << priv->page_offset;
+
+ /* (1) Maybe transfer partial page to Buffer1 */
+ if (writelen != priv->page_size) {
+ command[0] = OP_TRANSFER_BUF1;
+ command[1] = (addr & 0x00FF0000) >> 16;
+ command[2] = (addr & 0x0000FF00) >> 8;
+ command[3] = 0;
+
+ DEBUG(MTD_DEBUG_LEVEL3, "TRANSFER: (%x) %x %x %x\n",
+ command[0], command[1], command[2], command[3]);
+
+ msg.n_transfer = 1;
+ status = spi_sync(spi, &msg);
+ if (status < 0)
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: xfer %u -> %d \n",
+ spi->dev.bus_id, addr, status);
+
+ (void) dataflash_waitready(priv->spi);
+ }
+
+ /* (2) Program full page via Buffer1 */
+ addr += offset;
+ command[0] = OP_PROGRAM_VIA_BUF1;
+ command[1] = (addr & 0x00FF0000) >> 16;
+ command[2] = (addr & 0x0000FF00) >> 8;
+ command[3] = (addr & 0x000000FF);
+
+ DEBUG(MTD_DEBUG_LEVEL3, "PROGRAM: (%x) %x %x %x\n",
+ command[0], command[1], command[2], command[3]);
+
+ x[1].tx_buf = writebuf;
+ x[1].len = writelen;
+ msg.n_transfer = 2;
+ status = spi_sync(spi, &msg);
+ if (status < 0)
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: pgm %u/%u -> %d \n",
+ spi->dev.bus_id, addr, writelen, status);
+
+ (void) dataflash_waitready(priv->spi);
+
+#ifdef CONFIG_DATAFLASH_WRITE_VERIFY
+
+ /* (3) Compare to Buffer1 */
+ addr = pageaddr << priv->page_offset;
+ command[0] = OP_COMPARE_BUF1;
+ command[1] = (addr & 0x00FF0000) >> 16;
+ command[2] = (addr & 0x0000FF00) >> 8;
+ command[3] = 0;
+
+ DEBUG(MTD_DEBUG_LEVEL3, "COMPARE: (%x) %x %x %x\n",
+ command[0], command[1], command[2], command[3]);
+
+ msg.n_transfer = 1;
+ status = spi_sync(spi, &msg);
+ if (status < 0)
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: compare %u -> %d \n",
+ spi->dev.bus_id, addr, status);
+
+ status = dataflash_waitready(priv->spi);
+
+ /* Check result of the compare operation */
+ if ((status & (1 << 6)) == 1) {
+ printk(KERN_ERR "%s: compare page %u, err %d\n",
+ spi->dev.bus_id, pageaddr, status);
+ remaining = 0;
+ status = -EIO;
+ break;
+ } else
+ status = 0;
+
+#endif /* CONFIG_DATAFLASH_WRITE_VERIFY */
+
+ remaining = remaining - writelen;
+ pageaddr++;
+ offset = 0;
+ writebuf += writelen;
+ *retlen += writelen;
+
+ if (remaining > priv->page_size)
+ writelen = priv->page_size;
+ else
+ writelen = remaining;
+ }
+ up(&priv->lock);
+
+ return status;
+}
+
+/* ......................................................................... */
+
+/*
+ * Register DataFlash device with MTD subsystem.
+ */
+static int __devinit
+add_dataflash(struct spi_device *spi, char *name,
+ int nr_pages, int pagesize, int pageoffset)
+{
+ struct dataflash *priv;
+ struct mtd_info *device;
+ struct flash_platform_data *pdata = spi->dev.platform_data;
+
+ priv = (struct dataflash *) kzalloc(sizeof *priv, GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ init_MUTEX(&priv->lock);
+ priv->spi = spi;
+ priv->page_size = pagesize;
+ priv->page_offset = pageoffset;
+
+ /* name must be usable with cmdlinepart */
+ sprintf(priv->name, "spi%d.%d-%s",
+ spi->master->bus_num, spi->chip_select,
+ name);
+
+ device = &priv->mtd;
+ device->name = (pdata && pdata->name) ? pdata->name : priv->name;
+ device->size = nr_pages * pagesize;
+ device->erasesize = pagesize;
+ device->owner = THIS_MODULE;
+ device->type = MTD_DATAFLASH;
+ device->flags = MTD_CAP_NORFLASH;
+ device->erase = dataflash_erase;
+ device->read = dataflash_read;
+ device->write = dataflash_write;
+ device->priv = priv;
+
+ dev_info(&spi->dev, "%s (%d KBytes)\n", name, device->size/1024);
+ dev_set_drvdata(&spi->dev, priv);
+
+#ifdef CONFIG_MTD_PARTITIONS
+ {
+ struct mtd_partition *parts;
+ int nr_parts = 0;
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+ static const char *part_probes[] = { "cmdlinepart", NULL, };
+
+ nr_parts = parse_mtd_partitions(device, part_probes, &parts, 0);
+#endif
+
+ if (nr_parts <= 0 && pdata && pdata->parts) {
+ parts = pdata->parts;
+ nr_parts = pdata->nr_parts;
+ }
+
+ if (nr_parts > 0) {
+ priv->partitioned = 1;
+ return add_mtd_partitions(device, parts, nr_parts);
+ }
+ }
+#else
+ if (pdata->nr_parts)
+ dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
+ pdata->nr_parts, device->name);
+#endif
+ return add_mtd_device(device) == 1 ? -ENODEV : 0;
+}
+
+/*
+ * Detect and initialize DataFlash device:
+ *
+ * Device Density ID code #Pages PageSize Offset
+ * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
+ * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1025 264 9
+ * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
+ * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
+ * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
+ * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
+ * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
+ * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
+ */
+static int __devinit dataflash_probe(struct spi_device *spi)
+{
+ int status;
+
+ status = dataflash_status(spi);
+ if (status <= 0 || status == 0xff) {
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: status error %d\n",
+ spi->dev.bus_id, status);
+ if (status == 0xff)
+ status = -ENODEV;
+ return status;
+ }
+
+ /* if there's a device there, assume it's dataflash.
+ * board setup should have set spi->max_speed_max to
+ * match f(car) for continuous reads, mode 0 or 3.
+ */
+ switch (status & 0x3c) {
+ case 0x0c: /* 0 0 1 1 x x */
+ status = add_dataflash(spi, "AT45DB011B", 512, 264, 9);
+ break;
+ case 0x14: /* 0 1 0 1 x x */
+ status = add_dataflash(spi, "AT45DB021B", 1025, 264, 9);
+ break;
+ case 0x1c: /* 0 1 1 1 x x */
+ status = add_dataflash(spi, "AT45DB041x", 2048, 264, 9);
+ break;
+ case 0x24: /* 1 0 0 1 x x */
+ status = add_dataflash(spi, "AT45DB081B", 4096, 264, 9);
+ break;
+ case 0x2c: /* 1 0 1 1 x x */
+ status = add_dataflash(spi, "AT45DB161x", 4096, 528, 10);
+ break;
+ case 0x34: /* 1 1 0 1 x x */
+ status = add_dataflash(spi, "AT45DB321x", 8192, 528, 10);
+ break;
+ case 0x38: /* 1 1 1 x x x */
+ case 0x3c:
+ status = add_dataflash(spi, "AT45DB642x", 8192, 1056, 11);
+ break;
+ /* obsolete AT45DB1282 not (yet?) supported */
+ default:
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: unsupported device (%x)\n",
+ spi->dev.bus_id, status & 0x3c);
+ status = -ENODEV;
+ }
+
+ if (status < 0)
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: add_dataflash --> %d\n",
+ spi->dev.bus_id, status);
+
+#ifdef DATAFLASH_TEST
+ else {
+ struct dataflash *flash;
+
+ flash = dev_get_drvdata(&spi->dev);
+
+ dataflash_test1(flash);
+ dataflash_test2(flash);
+ }
+#endif
+
+ return status;
+}
+
+static int __devexit dataflash_remove(struct spi_device *spi)
+{
+ struct dataflash *flash = dev_get_drvdata(&spi->dev);
+ int status;
+
+ DEBUG(MTD_DEBUG_LEVEL1, "%s: remove\n", spi->dev.bus_id);
+
+#ifdef CONFIG_MTD_PARTITIONS
+ if (flash->partitioned)
+ status = del_mtd_partitions(&flash->mtd);
+ else
+#endif
+ status = del_mtd_device(&flash->mtd);
+ if (status == 0)
+ kfree(flash);
+ return status;
+}
+
+static struct spi_driver dataflash_driver = {
+ .driver = {
+ .name = "mtd_dataflash",
+ .bus = &spi_bus_type,
+ .owner = THIS_MODULE,
+ },
+
+ .probe = dataflash_probe,
+ .remove = __devexit_p(dataflash_remove),
+
+ /* FIXME: investigate suspend and resume... */
+};
+
+static int __init dataflash_init(void)
+{
+ return spi_register_driver(&dataflash_driver);
+}
+module_init(dataflash_init);
+
+static void __exit dataflash_exit(void)
+{
+ spi_unregister_driver(&dataflash_driver);
+}
+module_exit(dataflash_exit);
+
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Andrew Victor, David Brownell");
+MODULE_DESCRIPTION("MTD DataFlash driver");
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-12-04 0:28 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <200512031556.32214.david-b@pacbell.net>
2005-12-04 0:23 ` [patch 2.6.15-rc3-mm1 2/4] ads7836 uses spi_driver David Brownell
2005-12-04 0:26 ` [patch 2.6.15-rc3-mm1 3/4] add spi_bitbang driver David Brownell
2005-12-04 0:28 ` [patch 2.6.15-rc3-mm1 4/4] mtd_dataflash uses spi_driver David Brownell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®