spi_eeprom.c 2.5 KB

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