ip22-nvram.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * ip22-nvram.c: NVRAM and serial EEPROM handling.
  3. *
  4. * Copyright (C) 2003 Ladislav Michl (ladis@linux-mips.org)
  5. */
  6. #include <linux/module.h>
  7. #include <asm/sgi/hpc3.h>
  8. #include <asm/sgi/ip22.h>
  9. /* Control opcode for serial eeprom */
  10. #define EEPROM_READ 0xc000 /* serial memory read */
  11. #define EEPROM_WEN 0x9800 /* write enable before prog modes */
  12. #define EEPROM_WRITE 0xa000 /* serial memory write */
  13. #define EEPROM_WRALL 0x8800 /* write all registers */
  14. #define EEPROM_WDS 0x8000 /* disable all programming */
  15. #define EEPROM_PRREAD 0xc000 /* read protect register */
  16. #define EEPROM_PREN 0x9800 /* enable protect register mode */
  17. #define EEPROM_PRCLEAR 0xffff /* clear protect register */
  18. #define EEPROM_PRWRITE 0xa000 /* write protect register */
  19. #define EEPROM_PRDS 0x8000 /* disable protect register, forever */
  20. #define EEPROM_EPROT 0x01 /* Protect register enable */
  21. #define EEPROM_CSEL 0x02 /* Chip select */
  22. #define EEPROM_ECLK 0x04 /* EEPROM clock */
  23. #define EEPROM_DATO 0x08 /* Data out */
  24. #define EEPROM_DATI 0x10 /* Data in */
  25. /* We need to use these functions early... */
  26. #define delay() ({ \
  27. int x; \
  28. for (x=0; x<100000; x++) __asm__ __volatile__(""); })
  29. #define eeprom_cs_on(ptr) ({ \
  30. __raw_writel(__raw_readl(ptr) & ~EEPROM_DATO, ptr); \
  31. __raw_writel(__raw_readl(ptr) & ~EEPROM_ECLK, ptr); \
  32. __raw_writel(__raw_readl(ptr) & ~EEPROM_EPROT, ptr); \
  33. delay(); \
  34. __raw_writel(__raw_readl(ptr) | EEPROM_CSEL, ptr); \
  35. __raw_writel(__raw_readl(ptr) | EEPROM_ECLK, ptr); })
  36. #define eeprom_cs_off(ptr) ({ \
  37. __raw_writel(__raw_readl(ptr) & ~EEPROM_ECLK, ptr); \
  38. __raw_writel(__raw_readl(ptr) & ~EEPROM_CSEL, ptr); \
  39. __raw_writel(__raw_readl(ptr) | EEPROM_EPROT, ptr); \
  40. __raw_writel(__raw_readl(ptr) | EEPROM_ECLK, ptr); })
  41. #define BITS_IN_COMMAND 11
  42. /*
  43. * clock in the nvram command and the register number. For the
  44. * national semiconductor nv ram chip the op code is 3 bits and
  45. * the address is 6/8 bits.
  46. */
  47. static inline void eeprom_cmd(unsigned int *ctrl, unsigned cmd, unsigned reg)
  48. {
  49. unsigned short ser_cmd;
  50. int i;
  51. ser_cmd = cmd | (reg << (16 - BITS_IN_COMMAND));
  52. for (i = 0; i < BITS_IN_COMMAND; i++) {
  53. if (ser_cmd & (1<<15)) /* if high order bit set */
  54. __raw_writel(__raw_readl(ctrl) | EEPROM_DATO, ctrl);
  55. else
  56. __raw_writel(__raw_readl(ctrl) & ~EEPROM_DATO, ctrl);
  57. __raw_writel(__raw_readl(ctrl) & ~EEPROM_ECLK, ctrl);
  58. delay();
  59. __raw_writel(__raw_readl(ctrl) | EEPROM_ECLK, ctrl);
  60. delay();
  61. ser_cmd <<= 1;
  62. }
  63. /* see data sheet timing diagram */
  64. __raw_writel(__raw_readl(ctrl) & ~EEPROM_DATO, ctrl);
  65. }
  66. unsigned short ip22_eeprom_read(unsigned int *ctrl, int reg)
  67. {
  68. unsigned short res = 0;
  69. int i;
  70. __raw_writel(__raw_readl(ctrl) & ~EEPROM_EPROT, ctrl);
  71. eeprom_cs_on(ctrl);
  72. eeprom_cmd(ctrl, EEPROM_READ, reg);
  73. /* clock the data ouf of serial mem */
  74. for (i = 0; i < 16; i++) {
  75. __raw_writel(__raw_readl(ctrl) & ~EEPROM_ECLK, ctrl);
  76. delay();
  77. __raw_writel(__raw_readl(ctrl) | EEPROM_ECLK, ctrl);
  78. delay();
  79. res <<= 1;
  80. if (__raw_readl(ctrl) & EEPROM_DATI)
  81. res |= 1;
  82. }
  83. eeprom_cs_off(ctrl);
  84. return res;
  85. }
  86. EXPORT_SYMBOL(ip22_eeprom_read);
  87. /*
  88. * Read specified register from main NVRAM
  89. */
  90. unsigned short ip22_nvram_read(int reg)
  91. {
  92. if (ip22_is_fullhouse())
  93. /* IP22 (Indigo2 aka FullHouse) stores env variables into
  94. * 93CS56 Microwire Bus EEPROM 2048 Bit (128x16) */
  95. return ip22_eeprom_read(&hpc3c0->eeprom, reg);
  96. else {
  97. unsigned short tmp;
  98. /* IP24 (Indy aka Guiness) uses DS1386 8K version */
  99. reg <<= 1;
  100. tmp = hpc3c0->bbram[reg++] & 0xff;
  101. return (tmp << 8) | (hpc3c0->bbram[reg] & 0xff);
  102. }
  103. }
  104. EXPORT_SYMBOL(ip22_nvram_read);