spi_eeprom.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * spi_eeprom.c
  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. */
  12. #include <linux/init.h>
  13. #include <linux/device.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/spi/eeprom.h>
  16. #include <asm/txx9/spi.h>
  17. #define AT250X0_PAGE_SIZE 8
  18. /* register board information for at25 driver */
  19. int __init spi_eeprom_register(int chipid)
  20. {
  21. static struct spi_eeprom eeprom = {
  22. .name = "at250x0",
  23. .byte_len = 128,
  24. .page_size = AT250X0_PAGE_SIZE,
  25. .flags = EE_ADDR1,
  26. };
  27. struct spi_board_info info = {
  28. .modalias = "at25",
  29. .max_speed_hz = 1500000, /* 1.5Mbps */
  30. .bus_num = 0,
  31. .chip_select = chipid,
  32. .platform_data = &eeprom,
  33. /* Mode 0: High-Active, Sample-Then-Shift */
  34. };
  35. return spi_register_board_info(&info, 1);
  36. }
  37. /* simple temporary spi driver to provide early access to seeprom. */
  38. static struct read_param {
  39. int chipid;
  40. int address;
  41. unsigned char *buf;
  42. int len;
  43. } *read_param;
  44. static int __init early_seeprom_probe(struct spi_device *spi)
  45. {
  46. int stat = 0;
  47. u8 cmd[2];
  48. int len = read_param->len;
  49. char *buf = read_param->buf;
  50. int address = read_param->address;
  51. dev_info(&spi->dev, "spiclk %u KHz.\n",
  52. (spi->max_speed_hz + 500) / 1000);
  53. if (read_param->chipid != spi->chip_select)
  54. return -ENODEV;
  55. while (len > 0) {
  56. /* spi_write_then_read can only work with small chunk */
  57. int c = len < AT250X0_PAGE_SIZE ? len : AT250X0_PAGE_SIZE;
  58. cmd[0] = 0x03; /* AT25_READ */
  59. cmd[1] = address;
  60. stat = spi_write_then_read(spi, cmd, sizeof(cmd), buf, c);
  61. buf += c;
  62. len -= c;
  63. address += c;
  64. }
  65. return stat;
  66. }
  67. static struct spi_driver early_seeprom_driver __initdata = {
  68. .driver = {
  69. .name = "at25",
  70. .owner = THIS_MODULE,
  71. },
  72. .probe = early_seeprom_probe,
  73. };
  74. int __init spi_eeprom_read(int chipid, int address,
  75. unsigned char *buf, int len)
  76. {
  77. int ret;
  78. struct read_param param = {
  79. .chipid = chipid,
  80. .address = address,
  81. .buf = buf,
  82. .len = len
  83. };
  84. read_param = &param;
  85. ret = spi_register_driver(&early_seeprom_driver);
  86. if (!ret)
  87. spi_unregister_driver(&early_seeprom_driver);
  88. return ret;
  89. }