common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2010-2020 CS Group
  4. * Charles Frey <charles.frey@c-s.fr>
  5. * Florent Trinh Thai <florent.trinh-thai@c-s.fr>
  6. * Christophe Leroy <christophe.leroy@c-s.fr>
  7. *
  8. * Common specific routines for the CS Group boards
  9. */
  10. #include <dm.h>
  11. #include <env.h>
  12. #include <fdt_support.h>
  13. #include <hang.h>
  14. #include <spi.h>
  15. #include <linux/delay.h>
  16. #include <asm/io.h>
  17. #include "common.h"
  18. #define ADDR_FPGA_R_BASE ((unsigned char __iomem *)CONFIG_FPGA_BASE)
  19. #define FPGA_R_ACQ_AL_FAV 0x04
  20. #define TYPE_MCR 0x22
  21. #define TYPE_MIAE 0x23
  22. #define FAR_CASRSA 2
  23. #define FAR_VGOIP 4
  24. #define FAV_CLA 7
  25. #define FAV_SRSA 8
  26. #define SPI_EEPROM_READ 0x03
  27. static int fdt_set_node_and_value(void *blob, char *node, const char *prop,
  28. void *var, int size)
  29. {
  30. int ret, off;
  31. off = fdt_path_offset(blob, node);
  32. if (off < 0) {
  33. printf("Cannot find %s node err:%s\n", node, fdt_strerror(off));
  34. return off;
  35. }
  36. ret = fdt_setprop(blob, off, prop, var, size);
  37. if (ret < 0)
  38. printf("Cannot set %s/%s prop err: %s\n", node, prop, fdt_strerror(ret));
  39. return ret;
  40. }
  41. /* Checks front/rear id and remove unneeded nodes from the blob */
  42. static void ft_cleanup(void *blob, unsigned long id, const char *prop, const char *compatible)
  43. {
  44. int off;
  45. off = fdt_node_offset_by_compatible(blob, -1, compatible);
  46. while (off != -FDT_ERR_NOTFOUND) {
  47. const struct fdt_property *ids;
  48. int nb_ids, idx;
  49. int tmp = -1;
  50. ids = fdt_get_property(blob, off, prop, &nb_ids);
  51. for (idx = 0; idx < nb_ids; idx += 4) {
  52. if (*((uint32_t *)&ids->data[idx]) == id)
  53. break;
  54. }
  55. if (idx >= nb_ids)
  56. fdt_del_node(blob, off);
  57. else
  58. tmp = off;
  59. off = fdt_node_offset_by_compatible(blob, tmp, compatible);
  60. }
  61. fdt_set_node_and_value(blob, "/", prop, &id, sizeof(uint32_t));
  62. }
  63. int read_eeprom(u8 *din, int len)
  64. {
  65. struct udevice *eeprom;
  66. struct spi_slave *slave;
  67. uchar dout[3] = {SPI_EEPROM_READ, 0, 0};
  68. int ret;
  69. ret = uclass_get_device(UCLASS_SPI, 0, &eeprom);
  70. if (ret)
  71. return ret;
  72. ret = _spi_get_bus_and_cs(0, 0, 1000000, 0, "spi_generic_drv",
  73. "generic_0:0", &eeprom, &slave);
  74. if (ret)
  75. return ret;
  76. ret = spi_claim_bus(slave);
  77. ret = spi_xfer(slave, sizeof(dout) << 3, dout, NULL, SPI_XFER_BEGIN);
  78. if (ret)
  79. return ret;
  80. ret = spi_xfer(slave, len << 3, NULL, din, SPI_XFER_END);
  81. if (ret)
  82. return ret;
  83. spi_release_bus(slave);
  84. return 0;
  85. }
  86. int ft_board_setup_common(void *blob)
  87. {
  88. u8 far_id, fav_id;
  89. if (in_8(ADDR_FPGA_R_BASE) != TYPE_MIAE)
  90. return 0;
  91. far_id = in_8(ADDR_FPGA_R_BASE + 0x43) >> 5;
  92. ft_cleanup(blob, far_id, "far-id", "cs,mia-far");
  93. fav_id = in_8(ADDR_FPGA_R_BASE + 0x44) >> 5;
  94. if (far_id == FAR_CASRSA && fav_id == FAV_CLA)
  95. fav_id = FAV_SRSA;
  96. ft_cleanup(blob, fav_id, "fav-id", "cs,mia-fav");
  97. if (far_id == FAR_CASRSA)
  98. ft_board_setup_phy3();
  99. return 0;
  100. }
  101. int checkboard_common(void)
  102. {
  103. switch (in_8(ADDR_FPGA_R_BASE)) {
  104. int far_id;
  105. case TYPE_MCR:
  106. printf("MCR3000_2G (CS GROUP)\n");
  107. break;
  108. case TYPE_MIAE:
  109. far_id = in_8(ADDR_FPGA_R_BASE + 0x43) >> 5;
  110. if (far_id == FAR_VGOIP)
  111. printf("VGoIP (CS GROUP)\n");
  112. else
  113. printf("MIAE (CS GROUP)\n");
  114. break;
  115. default:
  116. printf("Unknown\n");
  117. for (;;)
  118. ;
  119. break;
  120. }
  121. return 0;
  122. }
  123. void misc_init_r_common(void)
  124. {
  125. u8 tmp, far_id;
  126. int count = 3;
  127. switch (in_8(ADDR_FPGA_R_BASE)) {
  128. case TYPE_MCR:
  129. /* if at boot alarm button is pressed, delay boot */
  130. if ((in_8(ADDR_FPGA_R_BASE + 0x31) & FPGA_R_ACQ_AL_FAV) == 0)
  131. env_set("bootdelay", "60");
  132. env_set("config", CFG_BOARD_MCR3000_2G);
  133. env_set("hostname", CFG_BOARD_MCR3000_2G);
  134. break;
  135. case TYPE_MIAE:
  136. do {
  137. tmp = in_8(ADDR_FPGA_R_BASE + 0x41);
  138. count--;
  139. mdelay(10); /* 10msec wait */
  140. } while (count && tmp != in_8(ADDR_FPGA_R_BASE + 0x41));
  141. if (!count) {
  142. printf("Cannot read the reset factory switch position\n");
  143. hang();
  144. }
  145. if (tmp & 0x1)
  146. env_set_default("Factory settings switch ON", 0);
  147. env_set("config", CFG_BOARD_MIAE);
  148. far_id = in_8(ADDR_FPGA_R_BASE + 0x43) >> 5;
  149. if (far_id == FAR_VGOIP)
  150. env_set("hostname", CFG_BOARD_VGOIP);
  151. else
  152. env_set("hostname", CFG_BOARD_MIAE);
  153. break;
  154. default:
  155. env_set("config", CFG_BOARD_CMPCXXX);
  156. env_set("hostname", CFG_BOARD_CMPCXXX);
  157. break;
  158. }
  159. }
  160. void iop_setup_common(void)
  161. {
  162. u8 type = in_8(ADDR_FPGA_R_BASE);
  163. if (type == TYPE_MCR)
  164. iop_setup_mcr();
  165. else if (type == TYPE_MIAE)
  166. iop_setup_miae();
  167. }