onewire.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd.
  4. * (http://www.friendlyarm.com)
  5. */
  6. #include <config.h>
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/clk.h>
  11. #include <i2c.h>
  12. #include <pwm.h>
  13. #include <irq_func.h>
  14. #include <asm/arch/nexell.h>
  15. #include <asm/arch/nx_gpio.h>
  16. #ifndef NSEC_PER_SEC
  17. #define NSEC_PER_SEC 1000000000L
  18. #endif
  19. #define SAMPLE_BPS 9600
  20. #define SAMPLE_IN_US 101 /* (1000000 / BPS) */
  21. #define REQ_INFO 0x60U
  22. #define REQ_BL 0x80U
  23. #define BUS_I2C 0x18
  24. #define ONEWIRE_I2C_BUS 2
  25. #define ONEWIRE_I2C_ADDR 0x2f
  26. static int bus_type = -1;
  27. static int lcd_id = -1;
  28. static unsigned short lcd_fwrev;
  29. static int current_brightness = -1;
  30. #ifdef CONFIG_DM_I2C
  31. static struct udevice *i2c_dev;
  32. #endif
  33. /* debug */
  34. #if (0)
  35. #define DBGOUT(msg...) do { printf("onewire: " msg); } while (0)
  36. #else
  37. #define DBGOUT(msg...) do {} while (0)
  38. #endif
  39. /* based on web page from http://lfh1986.blogspot.com */
  40. static unsigned char crc8_ow(unsigned int v, unsigned int len)
  41. {
  42. unsigned char crc = 0xACU;
  43. while (len--) {
  44. if ((crc & 0x80U) != 0) {
  45. crc <<= 1;
  46. crc ^= 0x7U;
  47. } else {
  48. crc <<= 1;
  49. }
  50. if ((v & (1U << 31)) != 0)
  51. crc ^= 0x7U;
  52. v <<= 1;
  53. }
  54. return crc;
  55. }
  56. /* GPIO helpers */
  57. #define __IO_GRP 2 /* GPIOC15 */
  58. #define __IO_IDX 15
  59. static inline void set_pin_as_input(void)
  60. {
  61. nx_gpio_set_output_enable(__IO_GRP, __IO_IDX, 0);
  62. }
  63. static inline void set_pin_as_output(void)
  64. {
  65. nx_gpio_set_output_enable(__IO_GRP, __IO_IDX, 1);
  66. }
  67. static inline void set_pin_value(int v)
  68. {
  69. nx_gpio_set_output_value(__IO_GRP, __IO_IDX, !!v);
  70. }
  71. static inline int get_pin_value(void)
  72. {
  73. return nx_gpio_get_input_value(__IO_GRP, __IO_IDX);
  74. }
  75. /* Timer helpers */
  76. #define PWM_CH 3
  77. #define PWM_TCON (PHY_BASEADDR_PWM + 0x08)
  78. #define PWM_TCON_START (1 << 16)
  79. #define PWM_TINT_CSTAT (PHY_BASEADDR_PWM + 0x44)
  80. static int onewire_init_timer(void)
  81. {
  82. int period_ns = NSEC_PER_SEC / SAMPLE_BPS;
  83. /* range: 1080~1970 */
  84. period_ns -= 1525;
  85. return pwm_config(PWM_CH, period_ns >> 1, period_ns);
  86. }
  87. static void wait_one_tick(void)
  88. {
  89. unsigned int tcon;
  90. tcon = readl(PWM_TCON);
  91. tcon |= PWM_TCON_START;
  92. writel(tcon, PWM_TCON);
  93. while (1) {
  94. if (readl(PWM_TINT_CSTAT) & (1 << (5 + PWM_CH)))
  95. break;
  96. }
  97. writel((1 << (5 + PWM_CH)), PWM_TINT_CSTAT);
  98. tcon &= ~PWM_TCON_START;
  99. writel(tcon, PWM_TCON);
  100. }
  101. /* Session handler */
  102. static int onewire_session(unsigned char req, unsigned char res[])
  103. {
  104. unsigned int Req;
  105. unsigned int *Res;
  106. int ints = disable_interrupts();
  107. int i;
  108. int ret;
  109. Req = (req << 24) | (crc8_ow(req << 24, 8) << 16);
  110. Res = (unsigned int *)res;
  111. set_pin_value(1);
  112. set_pin_as_output();
  113. for (i = 0; i < 60; i++)
  114. wait_one_tick();
  115. set_pin_value(0);
  116. for (i = 0; i < 2; i++)
  117. wait_one_tick();
  118. for (i = 0; i < 16; i++) {
  119. int v = !!(Req & (1U << 31));
  120. Req <<= 1;
  121. set_pin_value(v);
  122. wait_one_tick();
  123. }
  124. wait_one_tick();
  125. set_pin_as_input();
  126. wait_one_tick();
  127. for (i = 0; i < 32; i++) {
  128. (*Res) <<= 1;
  129. (*Res) |= get_pin_value();
  130. wait_one_tick();
  131. }
  132. set_pin_value(1);
  133. set_pin_as_output();
  134. if (ints)
  135. enable_interrupts();
  136. ret = crc8_ow(*Res, 24) == res[0];
  137. DBGOUT("req = %02X, res = %02X%02X%02X%02X, ret = %d\n",
  138. req, res[3], res[2], res[1], res[0], ret);
  139. return ret;
  140. }
  141. static int onewire_i2c_do_request(unsigned char req, unsigned char *buf)
  142. {
  143. unsigned char tx[4];
  144. int ret;
  145. tx[0] = req;
  146. tx[1] = crc8_ow(req << 24, 8);
  147. #ifdef CONFIG_DM_I2C
  148. if (dm_i2c_write(i2c_dev, 0, tx, 2))
  149. return -EIO;
  150. if (!buf)
  151. return 0;
  152. if (dm_i2c_read(i2c_dev, 0, buf, 4))
  153. return -EIO;
  154. #else
  155. if (i2c_write(ONEWIRE_I2C_ADDR, 0, 0, tx, 2))
  156. return -EIO;
  157. if (!buf) /* NO READ */
  158. return 0;
  159. if (i2c_read(ONEWIRE_I2C_ADDR, 0, 0, buf, 4))
  160. return -EIO;
  161. #endif
  162. ret = crc8_ow((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8), 24);
  163. DBGOUT("req = %02X, res = %02X%02X%02X%02X, ret = %02x\n",
  164. req, buf[0], buf[1], buf[2], buf[3], ret);
  165. return (ret == buf[3]) ? 0 : -EIO;
  166. }
  167. static void onewire_i2c_init(void)
  168. {
  169. unsigned char buf[4];
  170. int ret;
  171. #ifdef CONFIG_DM_I2C
  172. ret = i2c_get_chip_for_busnum(ONEWIRE_I2C_BUS,
  173. ONEWIRE_I2C_ADDR, 0, &i2c_dev);
  174. #else
  175. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  176. i2c_set_bus_num(ONEWIRE_I2C_BUS);
  177. ret = i2c_probe(ONEWIRE_I2C_ADDR);
  178. #endif
  179. if (ret)
  180. return;
  181. ret = onewire_i2c_do_request(REQ_INFO, buf);
  182. if (!ret) {
  183. lcd_id = buf[0];
  184. lcd_fwrev = buf[1] * 0x100 + buf[2];
  185. bus_type = BUS_I2C;
  186. }
  187. }
  188. void onewire_init(void)
  189. {
  190. /* GPIO, Pull-off */
  191. nx_gpio_set_pad_function(__IO_GRP, __IO_IDX, 1);
  192. nx_gpio_set_pull_mode(__IO_GRP, __IO_IDX, 2);
  193. onewire_init_timer();
  194. onewire_i2c_init();
  195. }
  196. int onewire_get_info(unsigned char *lcd, unsigned short *fw_ver)
  197. {
  198. unsigned char res[4];
  199. int i;
  200. if (bus_type == BUS_I2C && lcd_id > 0) {
  201. *lcd = lcd_id;
  202. *fw_ver = lcd_fwrev;
  203. return 0;
  204. }
  205. for (i = 0; i < 3; i++) {
  206. if (onewire_session(REQ_INFO, res)) {
  207. *lcd = res[3];
  208. *fw_ver = res[2] * 0x100 + res[1];
  209. lcd_id = *lcd;
  210. DBGOUT("lcd = %d, fw_ver = %x\n", *lcd, *fw_ver);
  211. return 0;
  212. }
  213. }
  214. /* LCD unknown or not connected */
  215. *lcd = 0;
  216. *fw_ver = -1;
  217. return -1;
  218. }
  219. int onewire_get_lcd_id(void)
  220. {
  221. return lcd_id;
  222. }
  223. int onewire_set_backlight(int brightness)
  224. {
  225. unsigned char res[4];
  226. int i;
  227. if (brightness == current_brightness)
  228. return 0;
  229. if (brightness > 127)
  230. brightness = 127;
  231. else if (brightness < 0)
  232. brightness = 0;
  233. if (bus_type == BUS_I2C) {
  234. onewire_i2c_do_request((REQ_BL | brightness), NULL);
  235. current_brightness = brightness;
  236. return 0;
  237. }
  238. for (i = 0; i < 3; i++) {
  239. if (onewire_session((REQ_BL | brightness), res)) {
  240. current_brightness = brightness;
  241. return 0;
  242. }
  243. }
  244. return -1;
  245. }