spi_eeprom.c 2.5 KB

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