Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 1 | /* |
Atsushi Nemoto | 22b1d70 | 2008-07-11 00:31:36 +0900 | [diff] [blame] | 2 | * spi_eeprom.c |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 3 | * Copyright (C) 2000-2001 Toshiba Corporation |
| 4 | * |
| 5 | * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the |
| 6 | * terms of the GNU General Public License version 2. This program is |
| 7 | * licensed "as is" without any warranty of any kind, whether express |
| 8 | * or implied. |
| 9 | * |
| 10 | * Support for TX4938 in 2.6 - Manish Lachwani (mlachwani@mvista.com) |
| 11 | */ |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 12 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/spi/eeprom.h> |
Atsushi Nemoto | 22b1d70 | 2008-07-11 00:31:36 +0900 | [diff] [blame] | 17 | #include <asm/txx9/spi.h> |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 18 | |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 19 | #define AT250X0_PAGE_SIZE 8 |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 20 | |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 21 | /* register board information for at25 driver */ |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 22 | int __init spi_eeprom_register(int busid, int chipid, int size) |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 23 | { |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 24 | struct spi_board_info info = { |
| 25 | .modalias = "at25", |
| 26 | .max_speed_hz = 1500000, /* 1.5Mbps */ |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 27 | .bus_num = busid, |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 28 | .chip_select = chipid, |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 29 | /* Mode 0: High-Active, Sample-Then-Shift */ |
| 30 | }; |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 31 | struct spi_eeprom *eeprom; |
| 32 | eeprom = kzalloc(sizeof(*eeprom), GFP_KERNEL); |
| 33 | if (!eeprom) |
| 34 | return -ENOMEM; |
| 35 | strcpy(eeprom->name, "at250x0"); |
| 36 | eeprom->byte_len = size; |
| 37 | eeprom->page_size = AT250X0_PAGE_SIZE; |
| 38 | eeprom->flags = EE_ADDR1; |
| 39 | info.platform_data = eeprom; |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 40 | return spi_register_board_info(&info, 1); |
| 41 | } |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 42 | |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 43 | /* simple temporary spi driver to provide early access to seeprom. */ |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 44 | |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 45 | static struct read_param { |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 46 | int busid; |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 47 | int chipid; |
| 48 | int address; |
| 49 | unsigned char *buf; |
| 50 | int len; |
| 51 | } *read_param; |
| 52 | |
| 53 | static int __init early_seeprom_probe(struct spi_device *spi) |
| 54 | { |
| 55 | int stat = 0; |
| 56 | u8 cmd[2]; |
| 57 | int len = read_param->len; |
| 58 | char *buf = read_param->buf; |
| 59 | int address = read_param->address; |
| 60 | |
| 61 | dev_info(&spi->dev, "spiclk %u KHz.\n", |
| 62 | (spi->max_speed_hz + 500) / 1000); |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 63 | if (read_param->busid != spi->master->bus_num || |
| 64 | read_param->chipid != spi->chip_select) |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 65 | return -ENODEV; |
| 66 | while (len > 0) { |
| 67 | /* spi_write_then_read can only work with small chunk */ |
| 68 | int c = len < AT250X0_PAGE_SIZE ? len : AT250X0_PAGE_SIZE; |
| 69 | cmd[0] = 0x03; /* AT25_READ */ |
| 70 | cmd[1] = address; |
| 71 | stat = spi_write_then_read(spi, cmd, sizeof(cmd), buf, c); |
| 72 | buf += c; |
| 73 | len -= c; |
| 74 | address += c; |
| 75 | } |
| 76 | return stat; |
| 77 | } |
| 78 | |
| 79 | static struct spi_driver early_seeprom_driver __initdata = { |
| 80 | .driver = { |
| 81 | .name = "at25", |
| 82 | .owner = THIS_MODULE, |
| 83 | }, |
| 84 | .probe = early_seeprom_probe, |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 85 | }; |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 86 | |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 87 | int __init spi_eeprom_read(int busid, int chipid, int address, |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 88 | unsigned char *buf, int len) |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 89 | { |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 90 | int ret; |
| 91 | struct read_param param = { |
Atsushi Nemoto | d75a40e | 2008-08-19 22:55:14 +0900 | [diff] [blame] | 92 | .busid = busid, |
Atsushi Nemoto | f74cf6f | 2007-06-22 23:22:06 +0900 | [diff] [blame] | 93 | .chipid = chipid, |
| 94 | .address = address, |
| 95 | .buf = buf, |
| 96 | .len = len |
| 97 | }; |
| 98 | |
| 99 | read_param = ¶m; |
| 100 | ret = spi_register_driver(&early_seeprom_driver); |
| 101 | if (!ret) |
| 102 | spi_unregister_driver(&early_seeprom_driver); |
| 103 | return ret; |
Ralf Baechle | 23fbee9 | 2005-07-25 22:45:45 +0000 | [diff] [blame] | 104 | } |