12 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/sched.h>
40 #define AT25_WREN 0x06
41 #define AT25_WRDI 0x04
42 #define AT25_RDSR 0x05
43 #define AT25_WRSR 0x01
44 #define AT25_READ 0x03
45 #define AT25_WRITE 0x02
47 #define AT25_SR_nRDY 0x01
48 #define AT25_SR_WEN 0x02
49 #define AT25_SR_BP0 0x04
50 #define AT25_SR_BP1 0x08
51 #define AT25_SR_WPEN 0x80
53 #define AT25_INSTR_BIT3 0x08
55 #define EE_MAXADDRLEN 3
64 #define io_limit PAGE_SIZE
83 if ((offset + count) > at25->
bin.size)
92 if (offset >= (1
U << (at25->
addrlen * 8)))
107 spi_message_init(&m);
112 spi_message_add_tail(&t[0], &m);
116 spi_message_add_tail(&t[1], &m);
128 "read %Zd bytes at %d --> %d\n",
129 count, offset, (
int) status);
132 return status ? status :
count;
136 at25_bin_read(
struct file *filp,
struct kobject *kobj,
138 char *
buf, loff_t off,
size_t count)
146 return at25_ee_read(at25, buf, off, count);
151 at25_ee_write(
struct at25_data *at25,
const char *
buf, loff_t off,
155 unsigned written = 0;
161 if ((off + count) > at25->
bin.size)
162 count = at25->
bin.size - off;
167 buf_size = at25->
chip.page_size;
181 unsigned offset = (unsigned) off;
187 status = spi_write(at25->
spi, cp, 1);
196 if (offset >= (1
U << (at25->
addrlen * 8)))
203 *cp++ = offset >> 16;
212 segment = buf_size - (offset %
buf_size);
216 status = spi_write(at25->
spi, bounce,
219 "write %u bytes at %u --> %d\n",
220 segment, offset, (
int) status);
236 "rdsr --> %d (%02x)\n", sr, sr);
241 if (!(sr & AT25_SR_nRDY))
245 if ((sr < 0) || (sr & AT25_SR_nRDY)) {
247 "write %d bytes offset %d, "
248 "timeout after %u msecs\n",
266 return written ? written :
status;
270 at25_bin_write(
struct file *filp,
struct kobject *kobj,
272 char *buf, loff_t off,
size_t count)
280 return at25_ee_write(at25, buf, off, count);
288 off_t offset,
size_t count)
292 return at25_ee_read(at25, buf, offset, count);
296 off_t offset,
size_t count)
300 return at25_ee_write(at25, buf, offset, count);
305 static int at25_np_to_chip(
struct device *
dev,
311 memset(chip, 0,
sizeof(*chip));
314 if (of_property_read_u32(np,
"size", &val) == 0 ||
315 of_property_read_u32(np,
"at25,byte-len", &val) == 0) {
318 dev_err(dev,
"Error: missing \"size\" property\n");
322 if (of_property_read_u32(np,
"pagesize", &val) == 0 ||
323 of_property_read_u32(np,
"at25,page-size", &val) == 0) {
326 dev_err(dev,
"Error: missing \"pagesize\" property\n");
330 if (of_property_read_u32(np,
"at25,addr-mode", &val) == 0) {
333 if (of_property_read_u32(np,
"address-width", &val)) {
335 "Error: missing \"address-width\" property\n");
350 "Error: bad \"address-width\" property: %u\n",
370 if (!spi->
dev.platform_data) {
372 err = at25_np_to_chip(&spi->
dev, np, &chip);
376 dev_err(&spi->
dev,
"Error: no chip description\n");
391 dev_dbg(&spi->
dev,
"unsupported address type\n");
401 if (sr < 0 || sr & AT25_SR_nRDY) {
402 dev_dbg(&spi->
dev,
"rdsr --> %d (%02x)\n", sr, sr);
407 if (!(at25 = kzalloc(
sizeof *at25,
GFP_KERNEL))) {
414 at25->
spi = spi_dev_get(spi);
428 at25->
bin.attr.name =
"eeprom";
430 at25->
bin.read = at25_bin_read;
431 at25->
mem.read = at25_mem_read;
433 at25->
bin.size = at25->
chip.byte_len;
435 at25->
bin.write = at25_bin_write;
437 at25->
mem.write = at25_mem_write;
447 dev_info(&spi->
dev,
"%Zd %s %s eeprom%s, pagesize %u\n",
448 (at25->
bin.size < 1024)
450 : (at25->
bin.size / 1024),
451 (at25->
bin.size < 1024) ?
"Byte" :
"KByte",
454 at25->
chip.page_size);