blob: d833dd2c9b5572940e0f43baca3d9a60f3117bae [file] [log] [blame]
Ralf Baechle23fbee92005-07-25 22:45:45 +00001/*
Atsushi Nemoto22b1d702008-07-11 00:31:36 +09002 * spi_eeprom.c
Ralf Baechle23fbee92005-07-25 22:45:45 +00003 * 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 Baechle23fbee92005-07-25 22:45:45 +000012#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Paul Gortmakercae39d12011-07-28 18:46:31 -040014#include <linux/export.h>
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090015#include <linux/device.h>
16#include <linux/spi/spi.h>
17#include <linux/spi/eeprom.h>
Atsushi Nemoto22b1d702008-07-11 00:31:36 +090018#include <asm/txx9/spi.h>
Ralf Baechle23fbee92005-07-25 22:45:45 +000019
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090020#define AT250X0_PAGE_SIZE 8
Ralf Baechle23fbee92005-07-25 22:45:45 +000021
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090022/* register board information for at25 driver */
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090023int __init spi_eeprom_register(int busid, int chipid, int size)
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090024{
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090025 struct spi_board_info info = {
26 .modalias = "at25",
27 .max_speed_hz = 1500000, /* 1.5Mbps */
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090028 .bus_num = busid,
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090029 .chip_select = chipid,
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090030 /* Mode 0: High-Active, Sample-Then-Shift */
31 };
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090032 struct spi_eeprom *eeprom;
33 eeprom = kzalloc(sizeof(*eeprom), GFP_KERNEL);
34 if (!eeprom)
35 return -ENOMEM;
36 strcpy(eeprom->name, "at250x0");
37 eeprom->byte_len = size;
38 eeprom->page_size = AT250X0_PAGE_SIZE;
39 eeprom->flags = EE_ADDR1;
40 info.platform_data = eeprom;
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090041 return spi_register_board_info(&info, 1);
42}
Ralf Baechle23fbee92005-07-25 22:45:45 +000043
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090044/* simple temporary spi driver to provide early access to seeprom. */
Ralf Baechle23fbee92005-07-25 22:45:45 +000045
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090046static struct read_param {
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090047 int busid;
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090048 int chipid;
49 int address;
50 unsigned char *buf;
51 int len;
52} *read_param;
53
54static int __init early_seeprom_probe(struct spi_device *spi)
55{
56 int stat = 0;
57 u8 cmd[2];
58 int len = read_param->len;
59 char *buf = read_param->buf;
60 int address = read_param->address;
61
62 dev_info(&spi->dev, "spiclk %u KHz.\n",
63 (spi->max_speed_hz + 500) / 1000);
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090064 if (read_param->busid != spi->master->bus_num ||
65 read_param->chipid != spi->chip_select)
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090066 return -ENODEV;
67 while (len > 0) {
68 /* spi_write_then_read can only work with small chunk */
69 int c = len < AT250X0_PAGE_SIZE ? len : AT250X0_PAGE_SIZE;
70 cmd[0] = 0x03; /* AT25_READ */
71 cmd[1] = address;
72 stat = spi_write_then_read(spi, cmd, sizeof(cmd), buf, c);
73 buf += c;
74 len -= c;
75 address += c;
76 }
77 return stat;
78}
79
80static struct spi_driver early_seeprom_driver __initdata = {
81 .driver = {
82 .name = "at25",
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090083 },
84 .probe = early_seeprom_probe,
Ralf Baechle23fbee92005-07-25 22:45:45 +000085};
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090086
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090087int __init spi_eeprom_read(int busid, int chipid, int address,
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090088 unsigned char *buf, int len)
Ralf Baechle23fbee92005-07-25 22:45:45 +000089{
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090090 int ret;
91 struct read_param param = {
Atsushi Nemotod75a40e2008-08-19 22:55:14 +090092 .busid = busid,
Atsushi Nemotof74cf6f2007-06-22 23:22:06 +090093 .chipid = chipid,
94 .address = address,
95 .buf = buf,
96 .len = len
97 };
98
99 read_param = &param;
100 ret = spi_register_driver(&early_seeprom_driver);
101 if (!ret)
102 spi_unregister_driver(&early_seeprom_driver);
103 return ret;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000104}