ip22-nvram.c 3.6 KB

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