at93c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Atmel AT93C46 serial eeprom driver
  3. *
  4. * Brian Murphy <brian.murphy@eicon.com>
  5. *
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/delay.h>
  9. #include <asm/lasat/lasat.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include "at93c.h"
  13. #define AT93C_ADDR_SHIFT 7
  14. #define AT93C_ADDR_MAX ((1 << AT93C_ADDR_SHIFT) - 1)
  15. #define AT93C_RCMD (0x6 << AT93C_ADDR_SHIFT)
  16. #define AT93C_WCMD (0x5 << AT93C_ADDR_SHIFT)
  17. #define AT93C_WENCMD 0x260
  18. #define AT93C_WDSCMD 0x200
  19. struct at93c_defs *at93c;
  20. static void at93c_reg_write(u32 val)
  21. {
  22. *at93c->reg = val;
  23. }
  24. static u32 at93c_reg_read(void)
  25. {
  26. u32 tmp = *at93c->reg;
  27. return tmp;
  28. }
  29. static u32 at93c_datareg_read(void)
  30. {
  31. u32 tmp = *at93c->rdata_reg;
  32. return tmp;
  33. }
  34. static void at93c_cycle_clk(u32 data)
  35. {
  36. at93c_reg_write(data | at93c->clk);
  37. lasat_ndelay(250);
  38. at93c_reg_write(data & ~at93c->clk);
  39. lasat_ndelay(250);
  40. }
  41. static void at93c_write_databit(u8 bit)
  42. {
  43. u32 data = at93c_reg_read();
  44. if (bit)
  45. data |= 1 << at93c->wdata_shift;
  46. else
  47. data &= ~(1 << at93c->wdata_shift);
  48. at93c_reg_write(data);
  49. lasat_ndelay(100);
  50. at93c_cycle_clk(data);
  51. }
  52. static unsigned int at93c_read_databit(void)
  53. {
  54. u32 data;
  55. at93c_cycle_clk(at93c_reg_read());
  56. data = (at93c_datareg_read() >> at93c->rdata_shift) & 1;
  57. return data;
  58. }
  59. static u8 at93c_read_byte(void)
  60. {
  61. int i;
  62. u8 data = 0;
  63. for (i = 0; i <= 7; i++) {
  64. data <<= 1;
  65. data |= at93c_read_databit();
  66. }
  67. return data;
  68. }
  69. static void at93c_write_bits(u32 data, int size)
  70. {
  71. int i;
  72. int shift = size - 1;
  73. u32 mask = (1 << shift);
  74. for (i = 0; i < size; i++) {
  75. at93c_write_databit((data & mask) >> shift);
  76. data <<= 1;
  77. }
  78. }
  79. static void at93c_init_op(void)
  80. {
  81. at93c_reg_write((at93c_reg_read() | at93c->cs) &
  82. ~at93c->clk & ~(1 << at93c->rdata_shift));
  83. lasat_ndelay(50);
  84. }
  85. static void at93c_end_op(void)
  86. {
  87. at93c_reg_write(at93c_reg_read() & ~at93c->cs);
  88. lasat_ndelay(250);
  89. }
  90. static void at93c_wait(void)
  91. {
  92. at93c_init_op();
  93. while (!at93c_read_databit())
  94. ;
  95. at93c_end_op();
  96. };
  97. static void at93c_disable_wp(void)
  98. {
  99. at93c_init_op();
  100. at93c_write_bits(AT93C_WENCMD, 10);
  101. at93c_end_op();
  102. }
  103. static void at93c_enable_wp(void)
  104. {
  105. at93c_init_op();
  106. at93c_write_bits(AT93C_WDSCMD, 10);
  107. at93c_end_op();
  108. }
  109. u8 at93c_read(u8 addr)
  110. {
  111. u8 byte;
  112. at93c_init_op();
  113. at93c_write_bits((addr & AT93C_ADDR_MAX)|AT93C_RCMD, 10);
  114. byte = at93c_read_byte();
  115. at93c_end_op();
  116. return byte;
  117. }
  118. void at93c_write(u8 addr, u8 data)
  119. {
  120. at93c_disable_wp();
  121. at93c_init_op();
  122. at93c_write_bits((addr & AT93C_ADDR_MAX)|AT93C_WCMD, 10);
  123. at93c_write_bits(data, 8);
  124. at93c_end_op();
  125. at93c_wait();
  126. at93c_enable_wp();
  127. }