soft_tws.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * (C) Copyright 2009
  3. * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. #define TWS_IMPLEMENTATION
  25. #include <common.h>
  26. /*=====================================================================*/
  27. /* Public Functions */
  28. /*=====================================================================*/
  29. /*-----------------------------------------------------------------------
  30. * Read bits
  31. */
  32. int tws_read(uchar *buffer, int len)
  33. {
  34. int rem = len;
  35. uchar accu, shift;
  36. debug("tws_read: buffer %p len %d\n", buffer, len);
  37. /* Configure the data pin for input */
  38. tws_data_config_output(0);
  39. /* Disable WR, i.e. setup a read */
  40. tws_wr(0);
  41. udelay(1);
  42. /* Rise CE */
  43. tws_ce(1);
  44. udelay(1);
  45. for (; rem > 0; ) {
  46. for (shift = 0, accu = 0;
  47. (rem > 0) && (shift < 8);
  48. rem--, shift++) {
  49. tws_clk(1);
  50. udelay(10);
  51. accu |= (tws_data_read() << shift); /* LSB first */
  52. tws_clk(0);
  53. udelay(10);
  54. }
  55. *buffer++ = accu;
  56. }
  57. /* Lower CE */
  58. tws_ce(0);
  59. return len - rem;
  60. }
  61. /*-----------------------------------------------------------------------
  62. * Write bits
  63. */
  64. int tws_write(uchar *buffer, int len)
  65. {
  66. int rem = len;
  67. uchar accu, shift;
  68. debug("tws_write: buffer %p len %d\n", buffer, len);
  69. /* Configure the data pin for output */
  70. tws_data_config_output(1);
  71. /* Enable WR, i.e. setup a write */
  72. tws_wr(1);
  73. udelay(1);
  74. /* Rise CE */
  75. tws_ce(1);
  76. udelay(1);
  77. for (; rem > 0; ) {
  78. for (shift = 0, accu = *buffer++;
  79. (rem > 0) && (shift < 8);
  80. rem--, shift++) {
  81. tws_data(accu & 0x01); /* LSB first */
  82. tws_clk(1);
  83. udelay(10);
  84. tws_clk(0);
  85. udelay(10);
  86. accu >>= 1;
  87. }
  88. }
  89. /* Lower CE */
  90. tws_ce(0);
  91. return len - rem;
  92. }