cobalt-cpld.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Cobalt CPLD functions
  3. *
  4. * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you may redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. * SOFTWARE.
  19. */
  20. #include <linux/delay.h>
  21. #include "cobalt-cpld.h"
  22. #define ADRS(offset) (COBALT_BUS_CPLD_BASE + offset)
  23. static u16 cpld_read(struct cobalt *cobalt, u32 offset)
  24. {
  25. return cobalt_bus_read32(cobalt->bar1, ADRS(offset));
  26. }
  27. static void cpld_write(struct cobalt *cobalt, u32 offset, u16 val)
  28. {
  29. return cobalt_bus_write32(cobalt->bar1, ADRS(offset), val);
  30. }
  31. static void cpld_info_ver3(struct cobalt *cobalt)
  32. {
  33. u32 rd;
  34. u32 tmp;
  35. cobalt_info("CPLD System control register (read/write)\n");
  36. cobalt_info("\t\tSystem control: 0x%04x (0x0f00)\n",
  37. cpld_read(cobalt, 0));
  38. cobalt_info("CPLD Clock control register (read/write)\n");
  39. cobalt_info("\t\tClock control: 0x%04x (0x0000)\n",
  40. cpld_read(cobalt, 0x04));
  41. cobalt_info("CPLD HSMA Clk Osc register (read/write) - Must set wr trigger to load default values\n");
  42. cobalt_info("\t\tRegister #7:\t0x%04x (0x0022)\n",
  43. cpld_read(cobalt, 0x08));
  44. cobalt_info("\t\tRegister #8:\t0x%04x (0x0047)\n",
  45. cpld_read(cobalt, 0x0c));
  46. cobalt_info("\t\tRegister #9:\t0x%04x (0x00fa)\n",
  47. cpld_read(cobalt, 0x10));
  48. cobalt_info("\t\tRegister #10:\t0x%04x (0x0061)\n",
  49. cpld_read(cobalt, 0x14));
  50. cobalt_info("\t\tRegister #11:\t0x%04x (0x001e)\n",
  51. cpld_read(cobalt, 0x18));
  52. cobalt_info("\t\tRegister #12:\t0x%04x (0x0045)\n",
  53. cpld_read(cobalt, 0x1c));
  54. cobalt_info("\t\tRegister #135:\t0x%04x\n",
  55. cpld_read(cobalt, 0x20));
  56. cobalt_info("\t\tRegister #137:\t0x%04x\n",
  57. cpld_read(cobalt, 0x24));
  58. cobalt_info("CPLD System status register (read only)\n");
  59. cobalt_info("\t\tSystem status: 0x%04x\n",
  60. cpld_read(cobalt, 0x28));
  61. cobalt_info("CPLD MAXII info register (read only)\n");
  62. cobalt_info("\t\tBoard serial number: 0x%04x\n",
  63. cpld_read(cobalt, 0x2c));
  64. cobalt_info("\t\tMAXII program revision: 0x%04x\n",
  65. cpld_read(cobalt, 0x30));
  66. cobalt_info("CPLD temp and voltage ADT7411 registers (read only)\n");
  67. cobalt_info("\t\tBoard temperature: %u Celcius\n",
  68. cpld_read(cobalt, 0x34) / 4);
  69. cobalt_info("\t\tFPGA temperature: %u Celcius\n",
  70. cpld_read(cobalt, 0x38) / 4);
  71. rd = cpld_read(cobalt, 0x3c);
  72. tmp = (rd * 33 * 1000) / (483 * 10);
  73. cobalt_info("\t\tVDD 3V3: %u,%03uV\n", tmp / 1000, tmp % 1000);
  74. rd = cpld_read(cobalt, 0x40);
  75. tmp = (rd * 74 * 2197) / (27 * 1000);
  76. cobalt_info("\t\tADC ch3 5V: %u,%03uV\n", tmp / 1000, tmp % 1000);
  77. rd = cpld_read(cobalt, 0x44);
  78. tmp = (rd * 74 * 2197) / (47 * 1000);
  79. cobalt_info("\t\tADC ch4 3V: %u,%03uV\n", tmp / 1000, tmp % 1000);
  80. rd = cpld_read(cobalt, 0x48);
  81. tmp = (rd * 57 * 2197) / (47 * 1000);
  82. cobalt_info("\t\tADC ch5 2V5: %u,%03uV\n", tmp / 1000, tmp % 1000);
  83. rd = cpld_read(cobalt, 0x4c);
  84. tmp = (rd * 2197) / 1000;
  85. cobalt_info("\t\tADC ch6 1V8: %u,%03uV\n", tmp / 1000, tmp % 1000);
  86. rd = cpld_read(cobalt, 0x50);
  87. tmp = (rd * 2197) / 1000;
  88. cobalt_info("\t\tADC ch7 1V5: %u,%03uV\n", tmp / 1000, tmp % 1000);
  89. rd = cpld_read(cobalt, 0x54);
  90. tmp = (rd * 2197) / 1000;
  91. cobalt_info("\t\tADC ch8 0V9: %u,%03uV\n", tmp / 1000, tmp % 1000);
  92. }
  93. void cobalt_cpld_status(struct cobalt *cobalt)
  94. {
  95. u32 rev = cpld_read(cobalt, 0x30);
  96. switch (rev) {
  97. case 3:
  98. case 4:
  99. case 5:
  100. cpld_info_ver3(cobalt);
  101. break;
  102. default:
  103. cobalt_info("CPLD revision %u is not supported!\n", rev);
  104. break;
  105. }
  106. }
  107. #define DCO_MIN 4850000000ULL
  108. #define DCO_MAX 5670000000ULL
  109. #define SI570_CLOCK_CTRL 0x04
  110. #define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_WR_TRIGGER 0x200
  111. #define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_RST_TRIGGER 0x100
  112. #define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL 0x80
  113. #define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN 0x40
  114. #define SI570_REG7 0x08
  115. #define SI570_REG8 0x0c
  116. #define SI570_REG9 0x10
  117. #define SI570_REG10 0x14
  118. #define SI570_REG11 0x18
  119. #define SI570_REG12 0x1c
  120. #define SI570_REG135 0x20
  121. #define SI570_REG137 0x24
  122. struct multiplier {
  123. unsigned mult, hsdiv, n1;
  124. };
  125. /* List all possible multipliers (= hsdiv * n1). There are lots of duplicates,
  126. which are all removed in this list to keep the list as short as possible.
  127. The values for hsdiv and n1 are the actual values, not the register values.
  128. */
  129. static const struct multiplier multipliers[] = {
  130. { 4, 4, 1 }, { 5, 5, 1 }, { 6, 6, 1 },
  131. { 7, 7, 1 }, { 8, 4, 2 }, { 9, 9, 1 },
  132. { 10, 5, 2 }, { 11, 11, 1 }, { 12, 6, 2 },
  133. { 14, 7, 2 }, { 16, 4, 4 }, { 18, 9, 2 },
  134. { 20, 5, 4 }, { 22, 11, 2 }, { 24, 4, 6 },
  135. { 28, 7, 4 }, { 30, 5, 6 }, { 32, 4, 8 },
  136. { 36, 6, 6 }, { 40, 4, 10 }, { 42, 7, 6 },
  137. { 44, 11, 4 }, { 48, 4, 12 }, { 50, 5, 10 },
  138. { 54, 9, 6 }, { 56, 4, 14 }, { 60, 5, 12 },
  139. { 64, 4, 16 }, { 66, 11, 6 }, { 70, 5, 14 },
  140. { 72, 4, 18 }, { 80, 4, 20 }, { 84, 6, 14 },
  141. { 88, 11, 8 }, { 90, 5, 18 }, { 96, 4, 24 },
  142. { 98, 7, 14 }, { 100, 5, 20 }, { 104, 4, 26 },
  143. { 108, 6, 18 }, { 110, 11, 10 }, { 112, 4, 28 },
  144. { 120, 4, 30 }, { 126, 7, 18 }, { 128, 4, 32 },
  145. { 130, 5, 26 }, { 132, 11, 12 }, { 136, 4, 34 },
  146. { 140, 5, 28 }, { 144, 4, 36 }, { 150, 5, 30 },
  147. { 152, 4, 38 }, { 154, 11, 14 }, { 156, 6, 26 },
  148. { 160, 4, 40 }, { 162, 9, 18 }, { 168, 4, 42 },
  149. { 170, 5, 34 }, { 176, 11, 16 }, { 180, 5, 36 },
  150. { 182, 7, 26 }, { 184, 4, 46 }, { 190, 5, 38 },
  151. { 192, 4, 48 }, { 196, 7, 28 }, { 198, 11, 18 },
  152. { 198, 9, 22 }, { 200, 4, 50 }, { 204, 6, 34 },
  153. { 208, 4, 52 }, { 210, 5, 42 }, { 216, 4, 54 },
  154. { 220, 11, 20 }, { 224, 4, 56 }, { 228, 6, 38 },
  155. { 230, 5, 46 }, { 232, 4, 58 }, { 234, 9, 26 },
  156. { 238, 7, 34 }, { 240, 4, 60 }, { 242, 11, 22 },
  157. { 248, 4, 62 }, { 250, 5, 50 }, { 252, 6, 42 },
  158. { 256, 4, 64 }, { 260, 5, 52 }, { 264, 11, 24 },
  159. { 266, 7, 38 }, { 270, 5, 54 }, { 272, 4, 68 },
  160. { 276, 6, 46 }, { 280, 4, 70 }, { 286, 11, 26 },
  161. { 288, 4, 72 }, { 290, 5, 58 }, { 294, 7, 42 },
  162. { 296, 4, 74 }, { 300, 5, 60 }, { 304, 4, 76 },
  163. { 306, 9, 34 }, { 308, 11, 28 }, { 310, 5, 62 },
  164. { 312, 4, 78 }, { 320, 4, 80 }, { 322, 7, 46 },
  165. { 324, 6, 54 }, { 328, 4, 82 }, { 330, 11, 30 },
  166. { 336, 4, 84 }, { 340, 5, 68 }, { 342, 9, 38 },
  167. { 344, 4, 86 }, { 348, 6, 58 }, { 350, 5, 70 },
  168. { 352, 11, 32 }, { 360, 4, 90 }, { 364, 7, 52 },
  169. { 368, 4, 92 }, { 370, 5, 74 }, { 372, 6, 62 },
  170. { 374, 11, 34 }, { 376, 4, 94 }, { 378, 7, 54 },
  171. { 380, 5, 76 }, { 384, 4, 96 }, { 390, 5, 78 },
  172. { 392, 4, 98 }, { 396, 11, 36 }, { 400, 4, 100 },
  173. { 406, 7, 58 }, { 408, 4, 102 }, { 410, 5, 82 },
  174. { 414, 9, 46 }, { 416, 4, 104 }, { 418, 11, 38 },
  175. { 420, 5, 84 }, { 424, 4, 106 }, { 430, 5, 86 },
  176. { 432, 4, 108 }, { 434, 7, 62 }, { 440, 11, 40 },
  177. { 444, 6, 74 }, { 448, 4, 112 }, { 450, 5, 90 },
  178. { 456, 4, 114 }, { 460, 5, 92 }, { 462, 11, 42 },
  179. { 464, 4, 116 }, { 468, 6, 78 }, { 470, 5, 94 },
  180. { 472, 4, 118 }, { 476, 7, 68 }, { 480, 4, 120 },
  181. { 484, 11, 44 }, { 486, 9, 54 }, { 488, 4, 122 },
  182. { 490, 5, 98 }, { 492, 6, 82 }, { 496, 4, 124 },
  183. { 500, 5, 100 }, { 504, 4, 126 }, { 506, 11, 46 },
  184. { 510, 5, 102 }, { 512, 4, 128 }, { 516, 6, 86 },
  185. { 518, 7, 74 }, { 520, 5, 104 }, { 522, 9, 58 },
  186. { 528, 11, 48 }, { 530, 5, 106 }, { 532, 7, 76 },
  187. { 540, 5, 108 }, { 546, 7, 78 }, { 550, 11, 50 },
  188. { 552, 6, 92 }, { 558, 9, 62 }, { 560, 5, 112 },
  189. { 564, 6, 94 }, { 570, 5, 114 }, { 572, 11, 52 },
  190. { 574, 7, 82 }, { 576, 6, 96 }, { 580, 5, 116 },
  191. { 588, 6, 98 }, { 590, 5, 118 }, { 594, 11, 54 },
  192. { 600, 5, 120 }, { 602, 7, 86 }, { 610, 5, 122 },
  193. { 612, 6, 102 }, { 616, 11, 56 }, { 620, 5, 124 },
  194. { 624, 6, 104 }, { 630, 5, 126 }, { 636, 6, 106 },
  195. { 638, 11, 58 }, { 640, 5, 128 }, { 644, 7, 92 },
  196. { 648, 6, 108 }, { 658, 7, 94 }, { 660, 11, 60 },
  197. { 666, 9, 74 }, { 672, 6, 112 }, { 682, 11, 62 },
  198. { 684, 6, 114 }, { 686, 7, 98 }, { 696, 6, 116 },
  199. { 700, 7, 100 }, { 702, 9, 78 }, { 704, 11, 64 },
  200. { 708, 6, 118 }, { 714, 7, 102 }, { 720, 6, 120 },
  201. { 726, 11, 66 }, { 728, 7, 104 }, { 732, 6, 122 },
  202. { 738, 9, 82 }, { 742, 7, 106 }, { 744, 6, 124 },
  203. { 748, 11, 68 }, { 756, 6, 126 }, { 768, 6, 128 },
  204. { 770, 11, 70 }, { 774, 9, 86 }, { 784, 7, 112 },
  205. { 792, 11, 72 }, { 798, 7, 114 }, { 810, 9, 90 },
  206. { 812, 7, 116 }, { 814, 11, 74 }, { 826, 7, 118 },
  207. { 828, 9, 92 }, { 836, 11, 76 }, { 840, 7, 120 },
  208. { 846, 9, 94 }, { 854, 7, 122 }, { 858, 11, 78 },
  209. { 864, 9, 96 }, { 868, 7, 124 }, { 880, 11, 80 },
  210. { 882, 7, 126 }, { 896, 7, 128 }, { 900, 9, 100 },
  211. { 902, 11, 82 }, { 918, 9, 102 }, { 924, 11, 84 },
  212. { 936, 9, 104 }, { 946, 11, 86 }, { 954, 9, 106 },
  213. { 968, 11, 88 }, { 972, 9, 108 }, { 990, 11, 90 },
  214. { 1008, 9, 112 }, { 1012, 11, 92 }, { 1026, 9, 114 },
  215. { 1034, 11, 94 }, { 1044, 9, 116 }, { 1056, 11, 96 },
  216. { 1062, 9, 118 }, { 1078, 11, 98 }, { 1080, 9, 120 },
  217. { 1098, 9, 122 }, { 1100, 11, 100 }, { 1116, 9, 124 },
  218. { 1122, 11, 102 }, { 1134, 9, 126 }, { 1144, 11, 104 },
  219. { 1152, 9, 128 }, { 1166, 11, 106 }, { 1188, 11, 108 },
  220. { 1210, 11, 110 }, { 1232, 11, 112 }, { 1254, 11, 114 },
  221. { 1276, 11, 116 }, { 1298, 11, 118 }, { 1320, 11, 120 },
  222. { 1342, 11, 122 }, { 1364, 11, 124 }, { 1386, 11, 126 },
  223. { 1408, 11, 128 },
  224. };
  225. bool cobalt_cpld_set_freq(struct cobalt *cobalt, unsigned f_out)
  226. {
  227. const unsigned f_xtal = 39170000; /* xtal for si598 */
  228. u64 dco;
  229. u64 rfreq;
  230. unsigned delta = 0xffffffff;
  231. unsigned i_best = 0;
  232. unsigned i;
  233. u8 n1, hsdiv;
  234. u8 regs[6];
  235. int found = 0;
  236. u16 clock_ctrl;
  237. int retries = 3;
  238. for (i = 0; i < ARRAY_SIZE(multipliers); i++) {
  239. unsigned mult = multipliers[i].mult;
  240. u32 d;
  241. dco = (u64)f_out * mult;
  242. if (dco < DCO_MIN || dco > DCO_MAX)
  243. continue;
  244. div_u64_rem((dco << 28) + f_xtal / 2, f_xtal, &d);
  245. if (d < delta) {
  246. found = 1;
  247. i_best = i;
  248. delta = d;
  249. }
  250. }
  251. if (!found)
  252. return false;
  253. dco = (u64)f_out * multipliers[i_best].mult;
  254. n1 = multipliers[i_best].n1 - 1;
  255. hsdiv = multipliers[i_best].hsdiv - 4;
  256. rfreq = div_u64(dco << 28, f_xtal);
  257. clock_ctrl = cpld_read(cobalt, SI570_CLOCK_CTRL);
  258. clock_ctrl |= S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL;
  259. clock_ctrl |= S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN;
  260. regs[0] = (hsdiv << 5) | (n1 >> 2);
  261. regs[1] = ((n1 & 0x3) << 6) | (rfreq >> 32);
  262. regs[2] = (rfreq >> 24) & 0xff;
  263. regs[3] = (rfreq >> 16) & 0xff;
  264. regs[4] = (rfreq >> 8) & 0xff;
  265. regs[5] = rfreq & 0xff;
  266. /* The sequence of clock_ctrl flags to set is very weird. It looks
  267. like I have to reset it, then set the new frequency and reset it
  268. again. It shouldn't be necessary to do a reset, but if I don't,
  269. then a strange frequency is set (156.412034 MHz, or register values
  270. 0x01, 0xc7, 0xfc, 0x7f, 0x53, 0x62).
  271. */
  272. cobalt_dbg(1, "%u: %6ph\n", f_out, regs);
  273. while (retries--) {
  274. u8 read_regs[6];
  275. cpld_write(cobalt, SI570_CLOCK_CTRL,
  276. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
  277. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL);
  278. usleep_range(10000, 15000);
  279. cpld_write(cobalt, SI570_REG7, regs[0]);
  280. cpld_write(cobalt, SI570_REG8, regs[1]);
  281. cpld_write(cobalt, SI570_REG9, regs[2]);
  282. cpld_write(cobalt, SI570_REG10, regs[3]);
  283. cpld_write(cobalt, SI570_REG11, regs[4]);
  284. cpld_write(cobalt, SI570_REG12, regs[5]);
  285. cpld_write(cobalt, SI570_CLOCK_CTRL,
  286. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
  287. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_WR_TRIGGER);
  288. usleep_range(10000, 15000);
  289. cpld_write(cobalt, SI570_CLOCK_CTRL,
  290. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
  291. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL);
  292. usleep_range(10000, 15000);
  293. read_regs[0] = cpld_read(cobalt, SI570_REG7);
  294. read_regs[1] = cpld_read(cobalt, SI570_REG8);
  295. read_regs[2] = cpld_read(cobalt, SI570_REG9);
  296. read_regs[3] = cpld_read(cobalt, SI570_REG10);
  297. read_regs[4] = cpld_read(cobalt, SI570_REG11);
  298. read_regs[5] = cpld_read(cobalt, SI570_REG12);
  299. cpld_write(cobalt, SI570_CLOCK_CTRL,
  300. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
  301. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL |
  302. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_RST_TRIGGER);
  303. usleep_range(10000, 15000);
  304. cpld_write(cobalt, SI570_CLOCK_CTRL,
  305. S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN);
  306. usleep_range(10000, 15000);
  307. if (!memcmp(read_regs, regs, sizeof(read_regs)))
  308. break;
  309. cobalt_dbg(1, "retry: %6ph\n", read_regs);
  310. }
  311. if (2 - retries)
  312. cobalt_info("Needed %d retries\n", 2 - retries);
  313. return true;
  314. }