spi: add at25 spi eeprom driver
Subsystems (2)
RFC PATCH 1/1 spi: add at25 spi eeprom driver
--- drivers/Kconfig | 1 + drivers/Makefile | 1 + drivers/misc/Kconfig | 17 +++ drivers/misc/Makefile | 1 + drivers/misc/eeprom/Kconfig | 11 ++ drivers/misc/eeprom/Makefile | 1 + drivers/misc/eeprom/at25.c | 323 ++++++++++++++++++++++++++++++++++++++++++ drivers/spi/spi.c | 42 ++++++ include/spi/eeprom.h | 22 +++ include/spi/spi.h | 78 ++++++++++- 10 files changed, 495 insertions(+), 2 deletions(-) create mode 100644 drivers/misc/Kconfig create mode 100644 drivers/misc/Makefile create mode 100644 drivers/misc/eeprom/Kconfig create mode 100644 drivers/misc/eeprom/Makefile create mode 100644 drivers/misc/eeprom/at25.c create mode 100644 include/spi/eeprom.h
drivers/Kconfig +1 -0
@@ -13,5 +13,6 @@ source "drivers/mci/Kconfig" source "drivers/clk/Kconfig" source "drivers/mfd/Kconfig" source "drivers/led/Kconfig" +source "drivers/misc/Kconfig" endmenu
drivers/Makefile +1 -0
@@ -11,3 +11,4 @@ obj-$(CONFIG_VIDEO) += video/ obj-y += clk/ obj-y += mfd/ obj-$(CONFIG_LED) += led/ +obj-y += misc/
drivers/misc/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# +# Misc strange devices +# + +menuconfig MISC_DEVICES + bool "Misc devices" + ---help--- + Say Y here to get to see options for device drivers from various + different categories. This option alone does not add any kernel code. + + If you say N, all options in this submenu will be skipped and disabled. + +if MISC_DEVICES + +source "drivers/misc/eeprom/Kconfig" + +endif # MISC_DEVICES
drivers/misc/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-y += eeprom/
drivers/misc/eeprom/Kconfig new file +11 -0
@@ -0,0 +1,11 @@ +menu "EEPROM support" + +config EEPROM_AT25 + tristate "SPI EEPROMs from most vendors" + depends on SPI + help + Enable this driver to get read/write support to most SPI EEPROMs, + after you configure the board init code to know about each eeprom + on your target board. + +endmenu
drivers/misc/eeprom/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_EEPROM_AT25) += at25.o
drivers/misc/eeprom/at25.c new file +323 -0
323 lines not on this page · raw message
drivers/spi/spi.c +42 -0
51 lines not on this page · raw message
include/spi/eeprom.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef _SPI_EEPROM_H +#define _SPI_EEPROM_H + +/* +* Put one of these structures in platform_data for SPI EEPROMS handled +* by the "at25" driver. On SPI, most EEPROMS understand the same core +* command set. If you need to support EEPROMs that don't yet fit, add +* flags to support those protocol options. These values all come from +* the chip datasheets. +*/ +struct spi_eeprom { +char name[10 ]; +u16 page_size;/* for writes */ +u16 flags; +#define EE_ADDR1 0x0001/* 8 bit addrs */ +#define EE_ADDR2 0x0002/* 16 bit addrs */ +#define EE_ADDR3 0x0004/* 24 bit addrs */ +#define EE_READONLY 0x0008/* disallow writes */ +u32 size; +}; + +#endif /* _SPI_EEPROM_H */
include/spi/spi.h +76 -2
90 lines not on this page · raw message
--
1.7.4.1
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Hi Hubert,
drivers/misc/Kconfig
Quoted 4 lines · Hubert Feurstein
obj-y += mfd/
obj-$(CONFIG_LED) += led/drivers/Makefile +1
-0+obj-y += misc/drivers/misc/Kconfig +0
-0
I think we should move the driver up to drivers/eeprom and skip the 'misc'. The kernel guys like to get rid of it also.
Quoted 7 lines
drivers/misc/Kconfig +7
-0+ + /* Wait for non-busy status */ + start_time = get_time_ns(); + + retries = 0; + do { +
drivers/misc/Kconfig
Quoted 10 lines
drivers/misc/Kconfig +10
-0+ sr = spi_w8r8(at25->spi, AT25_RDSR); + if (sr < 0 || (sr & AT25_SR_nRDY)) { + dev_dbg(at25->cdev.dev, + "rdsr --> %d (%02x)\n", sr, sr); + mdelay(1); + continue; + } + if (!(sr & AT25_SR_nRDY)) + break; + } while (retries++ < 3 || !is_timeout(start_time, EE_TIMEOUT));
I don't understand this. The loop is limited by retries++ < 3. Why this additional is_timeout? Is this the same in the kernel?
Quoted 12 lines
drivers/misc/Kconfig +7
-0+static int at25_init(void) +{ + register_driver(&at25_driver); + return 0; +} + +device_initcall(at25_init);drivers/spi/spi.c +0
-0
drivers/spi/spi.c struct spi_device *spi_new_device(struct spi_master *master, · line 78
Quoted 4 lines
drivers/spi/spi.c +1
-0@@ -75,6 +75,7 @@ struct spi_device *spi_new_device(struct spi_master *master, proxy->chip_select = chip->chip_select; proxy->max_speed_hz = chip->max_speed_hz; proxy->mode = chip->mode; + proxy->dev.platform_data = chip->platform_data;
This should be a seperate patch.
Quoted 38 lines
drivers/spi/spi.c +31
-0strcpy (proxy->dev.name, chip->name); proxy->dev.type_data = proxy; status =register_device (&proxy->dev);@@ -194,3 +195,44 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)return spi->master->transfer (spi, message); } +/** +* spi_write_then_read - SPI synchronous write followed by read +* @spi: device with which data will be exchanged +* @txbuf: data to be written +* @n_tx: size of txbuf, in bytes +* @rxbuf: buffer into which data will be read +* @n_rx: size of rxbuf, in bytes +* Context: can sleep +* +* This performs a half duplex MicroWire style transaction with the +* device, sending txbuf and then reading rxbuf. The return value +* is zero for success, else a negative errno status code. +* This call may only be used from a context that may sleep. +*/ +int spi_write_then_read (struct spi_device *spi, +const void *txbuf,unsigned n_tx, +void *rxbuf,unsigned n_rx) +{ +int status; +struct spi_message message; +struct spi_transfer x[2 ]; + +spi_message_init (&message); +memset (x,0 ,sizeof x); +if (n_tx) { + x[0 ].len = n_tx; +spi_message_add_tail (&x[0 ], &message); + } +if (n_rx) { + x[1 ].len = n_rx; +spi_message_add_tail (&x[1 ], &message);
drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct spi_message *message) · line 238
Quoted 10 lines
drivers/spi/spi.c +10
-0+ } + + x[0 ].tx_buf = txbuf; + x[1 ].rx_buf = rxbuf; + +/* do the i/o */ + status =spi_sync (spi, &message); +return status; +} +EXPORT_SYMBOL (spi_write_then_read);
Also a seperate patch.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Hi Sascha,
2011/6/20 Sascha Hauer <[email protected]>:
Quoted 11 lines
Hi Hubert,
drivers/misc/Kconfig
Quoted 4 lines · Hubert Feurstein
obj-y += mfd/
obj-$(CONFIG_LED) += led/drivers/Makefile +1
-0+obj-y += misc/drivers/misc/Kconfig +0
-0I think we should move the driver up to
drivers/eepromand skip the 'misc'. The kernel guys like to get rid of it also.
OK
Quoted 12 lines
Quoted 12 lines
drivers/misc/Kconfig +12
-0+ + /* Wait for non-busy status */ + start_time = get_time_ns(); + + retries = 0; + do { + + sr = spi_w8r8(at25->spi, AT25_RDSR); + if (sr < 0 || (sr & AT25_SR_nRDY)) { + dev_dbg(at25->cdev.dev, + "rdsr --> %d (%02x)\n", sr, sr); + mdelay(1);
Quoted 9 lines
Quoted 5 lines
+ continue;
+ }
+ if (!(sr &AT25_SR_nRDY))
+ break;
+ } while (retries++ < 3 || !is_timeout(start_time,EE_TIMEOUT));I don't understand this. The loop is limited by retries++ < 3. Why this additional
is_timeout? Is this the same in the kernel?
Hmm, I don't know why we have both here. I simply ported the kernel code.
Quoted 10 lines
Quoted 10 lines
drivers/misc/Kconfig +7
-0+static int at25_init(void) +{ + register_driver(&at25_driver); + return 0; +} + +device_initcall(at25_init);drivers/spi/spi.c +0
-1--- a/drivers/spi/spi.c
Quoted 8 lines
drivers/spi/spi.c struct spi_device *spi_new_device(struct spi_master *master, · line 78
Quoted 6 lines
+++
b/drivers/spi/spi.cdrivers/spi/spi.c +1
-0@@ -75,6 +75,7 @@ struct spi_device *spi_new_device(struct spi_master *master, proxy->chip_select = chip->chip_select; proxy->max_speed_hz = chip->max_speed_hz; proxy->mode = chip->mode; + proxy->dev.platform_data = chip->platform_data;This should be a seperate patch.
OK
Quoted 43 lines
Quoted 43 lines
drivers/spi/spi.c +35
-0strcpy(proxy->dev.name, chip->name); proxy->dev.type_data = proxy; status = register_device(&proxy->dev);@@ -194,3 +195,44 @@ int spi_sync(struct spi_device *spi, struct spi_message *message) return spi->master->transfer(spi, message); } +/** +* spi_write_then_read - SPI synchronous write followed by read +* @spi: device with which data will be exchanged +* @txbuf: data to be written +* @n_tx: size of txbuf, in bytes +* @rxbuf: buffer into which data will be read +* @n_rx: size of rxbuf, in bytes +* Context: can sleep +* +* This performs a half duplex MicroWire style transaction with the +* device, sending txbuf and then reading rxbuf. The return value +* is zero for success, else a negative errno status code. +* This call may only be used from a context that may sleep. +*/ +int spi_write_then_read (struct spi_device *spi, + const void *txbuf, unsigned n_tx, + void *rxbuf, unsigned n_rx) +{ + int status; + struct spi_message message; + struct spi_transfer x[2]; + + spi_message_init(&message); + memset(x, 0, sizeof x); + if (n_tx) { + x[0].len = n_tx; + spi_message_add_tail(&x[0], &message); + } + if (n_rx) { + x[1].len = n_rx; + spi_message_add_tail(&x[1], &message); + } + + x[0].tx_buf = txbuf; + x[1].rx_buf = rxbuf;
Quoted 8 lines
drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct spi_message *message) · line 238
Quoted 6 lines
drivers/spi/spi.c +6
-0+ + /* do the i/o */ + status = spi_sync(spi, &message); + return status; +} +EXPORT_SYMBOL (spi_write_then_read);Also a seperate patch.
OK
Hubert
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Quoted 16 lines · Hubert Feurstein
Hi Sascha,
2011/6/20 Sascha Hauer <[email protected]>:
Quoted 11 lines
Hi Hubert,
drivers/misc/Kconfig
Quoted 4 lines · Hubert Feurstein
obj-y += mfd/
obj-$(CONFIG_LED) += led/drivers/Makefile +1
-0+obj-y += misc/drivers/misc/Kconfig +0
-0I think we should move the driver up to
drivers/eepromand skip the 'misc'. The kernel guys like to get rid of it also.OK
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/misc/Kconfig +4
-0+ + /* Wait for non-busy status */ + start_time = get_time_ns(); +Quoted 8 lines
Quoted 8 lines
+ retries = 0;
+ do {
+
+ sr =spi_w8r8(at25->spi, AT25_RDSR);
+ if (sr < 0 || (sr &AT25_SR_nRDY)) {
+ dev_dbg(at25->cdev.dev,
+ "rdsr --> %d (%02x)\n", sr, sr);
+ mdelay(1);
We can remove this mdelay. In the kernel it makes sense to use msleep to give the cpu a chance to sleep. In barebox we are polling anyway, so we can better poll the status register instead of the timer register. This gives us the chance to bail out here after 1.1ms instead of 1, 2, ...
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines
+ continue;
+ }
+ if (!(sr &AT25_SR_nRDY))
+ break;
+ } while (retries++ < 3 || !is_timeout(start_time,EE_TIMEOUT));I don't understand this. The loop is limited by retries++ < 3. Why this additional
is_timeout? Is this the same in the kernel?Hmm, I don't know why we have both here. I simply ported the kernel code.
Looking at it again it seems like 'poll for EE_TIMEOUT ms but at least three times'. I think we can skip the retries and just poll for EE_TIMEOUT ms. The most important thing about timeouts is that we bail out after reasonable time to give the user a chance to continue without this driver and to give him a clue where it hangs. The exact amount of time we poll is not really important.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox