rsb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  4. *
  5. * Based on allwinner u-boot sources rsb code which is:
  6. * (C) Copyright 2007-2013
  7. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  8. * lixiang <lixiang@allwinnertech.com>
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <asm/arch/cpu.h>
  13. #include <asm/arch/gpio.h>
  14. #include <asm/arch/prcm.h>
  15. #include <asm/arch/rsb.h>
  16. static int rsb_set_device_mode(void);
  17. static void rsb_cfg_io(void)
  18. {
  19. #ifdef CONFIG_MACH_SUN8I
  20. sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_GPL_R_RSB);
  21. sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_GPL_R_RSB);
  22. sunxi_gpio_set_pull(SUNXI_GPL(0), 1);
  23. sunxi_gpio_set_pull(SUNXI_GPL(1), 1);
  24. sunxi_gpio_set_drv(SUNXI_GPL(0), 2);
  25. sunxi_gpio_set_drv(SUNXI_GPL(1), 2);
  26. #elif defined CONFIG_MACH_SUN9I
  27. sunxi_gpio_set_cfgpin(SUNXI_GPN(0), SUN9I_GPN_R_RSB);
  28. sunxi_gpio_set_cfgpin(SUNXI_GPN(1), SUN9I_GPN_R_RSB);
  29. sunxi_gpio_set_pull(SUNXI_GPN(0), 1);
  30. sunxi_gpio_set_pull(SUNXI_GPN(1), 1);
  31. sunxi_gpio_set_drv(SUNXI_GPN(0), 2);
  32. sunxi_gpio_set_drv(SUNXI_GPN(1), 2);
  33. #else
  34. #error unsupported MACH_SUNXI
  35. #endif
  36. }
  37. static void rsb_set_clk(void)
  38. {
  39. struct sunxi_rsb_reg * const rsb =
  40. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  41. u32 div = 0;
  42. u32 cd_odly = 0;
  43. /* Source is Hosc24M, set RSB clk to 3Mhz */
  44. div = 24000000 / 3000000 / 2 - 1;
  45. cd_odly = div >> 1;
  46. if (!cd_odly)
  47. cd_odly = 1;
  48. writel((cd_odly << 8) | div, &rsb->ccr);
  49. }
  50. int rsb_init(void)
  51. {
  52. struct sunxi_rsb_reg * const rsb =
  53. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  54. /* Enable RSB and PIO clk, and de-assert their resets */
  55. prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_RSB);
  56. /* Setup external pins */
  57. rsb_cfg_io();
  58. writel(RSB_CTRL_SOFT_RST, &rsb->ctrl);
  59. rsb_set_clk();
  60. return rsb_set_device_mode();
  61. }
  62. static int rsb_await_trans(void)
  63. {
  64. struct sunxi_rsb_reg * const rsb =
  65. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  66. unsigned long tmo = timer_get_us() + 1000000;
  67. u32 stat;
  68. int ret;
  69. while (1) {
  70. stat = readl(&rsb->stat);
  71. if (stat & RSB_STAT_LBSY_INT) {
  72. ret = -EBUSY;
  73. break;
  74. }
  75. if (stat & RSB_STAT_TERR_INT) {
  76. ret = -EIO;
  77. break;
  78. }
  79. if (stat & RSB_STAT_TOVER_INT) {
  80. ret = 0;
  81. break;
  82. }
  83. if (timer_get_us() > tmo) {
  84. ret = -ETIME;
  85. break;
  86. }
  87. }
  88. writel(stat, &rsb->stat); /* Clear status bits */
  89. return ret;
  90. }
  91. static int rsb_set_device_mode(void)
  92. {
  93. struct sunxi_rsb_reg * const rsb =
  94. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  95. unsigned long tmo = timer_get_us() + 1000000;
  96. writel(RSB_DMCR_DEVICE_MODE_START | RSB_DMCR_DEVICE_MODE_DATA,
  97. &rsb->dmcr);
  98. while (readl(&rsb->dmcr) & RSB_DMCR_DEVICE_MODE_START) {
  99. if (timer_get_us() > tmo)
  100. return -ETIME;
  101. }
  102. return rsb_await_trans();
  103. }
  104. static int rsb_do_trans(void)
  105. {
  106. struct sunxi_rsb_reg * const rsb =
  107. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  108. setbits_le32(&rsb->ctrl, RSB_CTRL_START_TRANS);
  109. return rsb_await_trans();
  110. }
  111. int rsb_set_device_address(u16 device_addr, u16 runtime_addr)
  112. {
  113. struct sunxi_rsb_reg * const rsb =
  114. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  115. writel(RSB_DEVADDR_RUNTIME_ADDR(runtime_addr) |
  116. RSB_DEVADDR_DEVICE_ADDR(device_addr), &rsb->devaddr);
  117. writel(RSB_CMD_SET_RTSADDR, &rsb->cmd);
  118. return rsb_do_trans();
  119. }
  120. int rsb_write(const u16 runtime_device_addr, const u8 reg_addr, u8 data)
  121. {
  122. struct sunxi_rsb_reg * const rsb =
  123. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  124. writel(RSB_DEVADDR_RUNTIME_ADDR(runtime_device_addr), &rsb->devaddr);
  125. writel(reg_addr, &rsb->addr);
  126. writel(data, &rsb->data);
  127. writel(RSB_CMD_BYTE_WRITE, &rsb->cmd);
  128. return rsb_do_trans();
  129. }
  130. int rsb_read(const u16 runtime_device_addr, const u8 reg_addr, u8 *data)
  131. {
  132. struct sunxi_rsb_reg * const rsb =
  133. (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
  134. int ret;
  135. writel(RSB_DEVADDR_RUNTIME_ADDR(runtime_device_addr), &rsb->devaddr);
  136. writel(reg_addr, &rsb->addr);
  137. writel(RSB_CMD_BYTE_READ, &rsb->cmd);
  138. ret = rsb_do_trans();
  139. if (ret)
  140. return ret;
  141. *data = readl(&rsb->data) & 0xff;
  142. return 0;
  143. }