flexcop-sram.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
  4. * flexcop-sram.c - functions for controlling the SRAM
  5. * see flexcop.c for copyright information
  6. */
  7. #include "flexcop.h"
  8. static void flexcop_sram_set_chip(struct flexcop_device *fc,
  9. flexcop_sram_type_t type)
  10. {
  11. flexcop_set_ibi_value(wan_ctrl_reg_71c, sram_chip, type);
  12. }
  13. int flexcop_sram_init(struct flexcop_device *fc)
  14. {
  15. switch (fc->rev) {
  16. case FLEXCOP_II:
  17. case FLEXCOP_IIB:
  18. flexcop_sram_set_chip(fc, FC_SRAM_1_32KB);
  19. break;
  20. case FLEXCOP_III:
  21. flexcop_sram_set_chip(fc, FC_SRAM_1_48KB);
  22. break;
  23. default:
  24. return -EINVAL;
  25. }
  26. return 0;
  27. }
  28. int flexcop_sram_set_dest(struct flexcop_device *fc, flexcop_sram_dest_t dest,
  29. flexcop_sram_dest_target_t target)
  30. {
  31. flexcop_ibi_value v;
  32. v = fc->read_ibi_reg(fc, sram_dest_reg_714);
  33. if (fc->rev != FLEXCOP_III && target == FC_SRAM_DEST_TARGET_FC3_CA) {
  34. err("SRAM destination target to available on FlexCopII(b)\n");
  35. return -EINVAL;
  36. }
  37. deb_sram("sram dest: %x target: %x\n", dest, target);
  38. if (dest & FC_SRAM_DEST_NET)
  39. v.sram_dest_reg_714.NET_Dest = target;
  40. if (dest & FC_SRAM_DEST_CAI)
  41. v.sram_dest_reg_714.CAI_Dest = target;
  42. if (dest & FC_SRAM_DEST_CAO)
  43. v.sram_dest_reg_714.CAO_Dest = target;
  44. if (dest & FC_SRAM_DEST_MEDIA)
  45. v.sram_dest_reg_714.MEDIA_Dest = target;
  46. fc->write_ibi_reg(fc,sram_dest_reg_714,v);
  47. udelay(1000); /* TODO delay really necessary */
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(flexcop_sram_set_dest);
  51. void flexcop_wan_set_speed(struct flexcop_device *fc, flexcop_wan_speed_t s)
  52. {
  53. flexcop_set_ibi_value(wan_ctrl_reg_71c,wan_speed_sig,s);
  54. }
  55. EXPORT_SYMBOL(flexcop_wan_set_speed);
  56. void flexcop_sram_ctrl(struct flexcop_device *fc, int usb_wan, int sramdma, int maximumfill)
  57. {
  58. flexcop_ibi_value v = fc->read_ibi_reg(fc,sram_dest_reg_714);
  59. v.sram_dest_reg_714.ctrl_usb_wan = usb_wan;
  60. v.sram_dest_reg_714.ctrl_sramdma = sramdma;
  61. v.sram_dest_reg_714.ctrl_maximumfill = maximumfill;
  62. fc->write_ibi_reg(fc,sram_dest_reg_714,v);
  63. }
  64. EXPORT_SYMBOL(flexcop_sram_ctrl);
  65. #if 0
  66. static void flexcop_sram_write(struct adapter *adapter, u32 bank, u32 addr, u8 *buf, u32 len)
  67. {
  68. int i, retries;
  69. u32 command;
  70. for (i = 0; i < len; i++) {
  71. command = bank | addr | 0x04000000 | (*buf << 0x10);
  72. retries = 2;
  73. while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
  74. mdelay(1);
  75. retries--;
  76. }
  77. if (retries == 0)
  78. printk("%s: SRAM timeout\n", __func__);
  79. write_reg_dw(adapter, 0x700, command);
  80. buf++;
  81. addr++;
  82. }
  83. }
  84. static void flex_sram_read(struct adapter *adapter, u32 bank, u32 addr, u8 *buf, u32 len)
  85. {
  86. int i, retries;
  87. u32 command, value;
  88. for (i = 0; i < len; i++) {
  89. command = bank | addr | 0x04008000;
  90. retries = 10000;
  91. while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
  92. mdelay(1);
  93. retries--;
  94. }
  95. if (retries == 0)
  96. printk("%s: SRAM timeout\n", __func__);
  97. write_reg_dw(adapter, 0x700, command);
  98. retries = 10000;
  99. while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
  100. mdelay(1);
  101. retries--;
  102. }
  103. if (retries == 0)
  104. printk("%s: SRAM timeout\n", __func__);
  105. value = read_reg_dw(adapter, 0x700) >> 0x10;
  106. *buf = (value & 0xff);
  107. addr++;
  108. buf++;
  109. }
  110. }
  111. static void sram_write_chunk(struct adapter *adapter, u32 addr, u8 *buf, u16 len)
  112. {
  113. u32 bank;
  114. bank = 0;
  115. if (adapter->dw_sram_type == 0x20000) {
  116. bank = (addr & 0x18000) << 0x0d;
  117. }
  118. if (adapter->dw_sram_type == 0x00000) {
  119. if ((addr >> 0x0f) == 0)
  120. bank = 0x20000000;
  121. else
  122. bank = 0x10000000;
  123. }
  124. flex_sram_write(adapter, bank, addr & 0x7fff, buf, len);
  125. }
  126. static void sram_read_chunk(struct adapter *adapter, u32 addr, u8 *buf, u16 len)
  127. {
  128. u32 bank;
  129. bank = 0;
  130. if (adapter->dw_sram_type == 0x20000) {
  131. bank = (addr & 0x18000) << 0x0d;
  132. }
  133. if (adapter->dw_sram_type == 0x00000) {
  134. if ((addr >> 0x0f) == 0)
  135. bank = 0x20000000;
  136. else
  137. bank = 0x10000000;
  138. }
  139. flex_sram_read(adapter, bank, addr & 0x7fff, buf, len);
  140. }
  141. static void sram_read(struct adapter *adapter, u32 addr, u8 *buf, u32 len)
  142. {
  143. u32 length;
  144. while (len != 0) {
  145. length = len;
  146. /* check if the address range belongs to the same
  147. * 32K memory chip. If not, the data is read
  148. * from one chip at a time */
  149. if ((addr >> 0x0f) != ((addr + len - 1) >> 0x0f)) {
  150. length = (((addr >> 0x0f) + 1) << 0x0f) - addr;
  151. }
  152. sram_read_chunk(adapter, addr, buf, length);
  153. addr = addr + length;
  154. buf = buf + length;
  155. len = len - length;
  156. }
  157. }
  158. static void sram_write(struct adapter *adapter, u32 addr, u8 *buf, u32 len)
  159. {
  160. u32 length;
  161. while (len != 0) {
  162. length = len;
  163. /* check if the address range belongs to the same
  164. * 32K memory chip. If not, the data is
  165. * written to one chip at a time */
  166. if ((addr >> 0x0f) != ((addr + len - 1) >> 0x0f)) {
  167. length = (((addr >> 0x0f) + 1) << 0x0f) - addr;
  168. }
  169. sram_write_chunk(adapter, addr, buf, length);
  170. addr = addr + length;
  171. buf = buf + length;
  172. len = len - length;
  173. }
  174. }
  175. static void sram_set_size(struct adapter *adapter, u32 mask)
  176. {
  177. write_reg_dw(adapter, 0x71c,
  178. (mask | (~0x30000 & read_reg_dw(adapter, 0x71c))));
  179. }
  180. static void sram_init(struct adapter *adapter)
  181. {
  182. u32 tmp;
  183. tmp = read_reg_dw(adapter, 0x71c);
  184. write_reg_dw(adapter, 0x71c, 1);
  185. if (read_reg_dw(adapter, 0x71c) != 0) {
  186. write_reg_dw(adapter, 0x71c, tmp);
  187. adapter->dw_sram_type = tmp & 0x30000;
  188. ddprintk("%s: dw_sram_type = %x\n", __func__, adapter->dw_sram_type);
  189. } else {
  190. adapter->dw_sram_type = 0x10000;
  191. ddprintk("%s: dw_sram_type = %x\n", __func__, adapter->dw_sram_type);
  192. }
  193. }
  194. static int sram_test_location(struct adapter *adapter, u32 mask, u32 addr)
  195. {
  196. u8 tmp1, tmp2;
  197. dprintk("%s: mask = %x, addr = %x\n", __func__, mask, addr);
  198. sram_set_size(adapter, mask);
  199. sram_init(adapter);
  200. tmp2 = 0xa5;
  201. tmp1 = 0x4f;
  202. sram_write(adapter, addr, &tmp2, 1);
  203. sram_write(adapter, addr + 4, &tmp1, 1);
  204. tmp2 = 0;
  205. mdelay(20);
  206. sram_read(adapter, addr, &tmp2, 1);
  207. sram_read(adapter, addr, &tmp2, 1);
  208. dprintk("%s: wrote 0xa5, read 0x%2x\n", __func__, tmp2);
  209. if (tmp2 != 0xa5)
  210. return 0;
  211. tmp2 = 0x5a;
  212. tmp1 = 0xf4;
  213. sram_write(adapter, addr, &tmp2, 1);
  214. sram_write(adapter, addr + 4, &tmp1, 1);
  215. tmp2 = 0;
  216. mdelay(20);
  217. sram_read(adapter, addr, &tmp2, 1);
  218. sram_read(adapter, addr, &tmp2, 1);
  219. dprintk("%s: wrote 0x5a, read 0x%2x\n", __func__, tmp2);
  220. if (tmp2 != 0x5a)
  221. return 0;
  222. return 1;
  223. }
  224. static u32 sram_length(struct adapter *adapter)
  225. {
  226. if (adapter->dw_sram_type == 0x10000)
  227. return 32768; /* 32K */
  228. if (adapter->dw_sram_type == 0x00000)
  229. return 65536; /* 64K */
  230. if (adapter->dw_sram_type == 0x20000)
  231. return 131072; /* 128K */
  232. return 32768; /* 32K */
  233. }
  234. /* FlexcopII can work with 32K, 64K or 128K of external SRAM memory.
  235. - for 128K there are 4x32K chips at bank 0,1,2,3.
  236. - for 64K there are 2x32K chips at bank 1,2.
  237. - for 32K there is one 32K chip at bank 0.
  238. FlexCop works only with one bank at a time. The bank is selected
  239. by bits 28-29 of the 0x700 register.
  240. bank 0 covers addresses 0x00000-0x07fff
  241. bank 1 covers addresses 0x08000-0x0ffff
  242. bank 2 covers addresses 0x10000-0x17fff
  243. bank 3 covers addresses 0x18000-0x1ffff */
  244. static int flexcop_sram_detect(struct flexcop_device *fc)
  245. {
  246. flexcop_ibi_value r208, r71c_0, vr71c_1;
  247. r208 = fc->read_ibi_reg(fc, ctrl_208);
  248. fc->write_ibi_reg(fc, ctrl_208, ibi_zero);
  249. r71c_0 = fc->read_ibi_reg(fc, wan_ctrl_reg_71c);
  250. write_reg_dw(adapter, 0x71c, 1);
  251. tmp3 = read_reg_dw(adapter, 0x71c);
  252. dprintk("%s: tmp3 = %x\n", __func__, tmp3);
  253. write_reg_dw(adapter, 0x71c, tmp2);
  254. // check for internal SRAM ???
  255. tmp3--;
  256. if (tmp3 != 0) {
  257. sram_set_size(adapter, 0x10000);
  258. sram_init(adapter);
  259. write_reg_dw(adapter, 0x208, tmp);
  260. dprintk("%s: sram size = 32K\n", __func__);
  261. return 32;
  262. }
  263. if (sram_test_location(adapter, 0x20000, 0x18000) != 0) {
  264. sram_set_size(adapter, 0x20000);
  265. sram_init(adapter);
  266. write_reg_dw(adapter, 0x208, tmp);
  267. dprintk("%s: sram size = 128K\n", __func__);
  268. return 128;
  269. }
  270. if (sram_test_location(adapter, 0x00000, 0x10000) != 0) {
  271. sram_set_size(adapter, 0x00000);
  272. sram_init(adapter);
  273. write_reg_dw(adapter, 0x208, tmp);
  274. dprintk("%s: sram size = 64K\n", __func__);
  275. return 64;
  276. }
  277. if (sram_test_location(adapter, 0x10000, 0x00000) != 0) {
  278. sram_set_size(adapter, 0x10000);
  279. sram_init(adapter);
  280. write_reg_dw(adapter, 0x208, tmp);
  281. dprintk("%s: sram size = 32K\n", __func__);
  282. return 32;
  283. }
  284. sram_set_size(adapter, 0x10000);
  285. sram_init(adapter);
  286. write_reg_dw(adapter, 0x208, tmp);
  287. dprintk("%s: SRAM detection failed. Set to 32K \n", __func__);
  288. return 0;
  289. }
  290. static void sll_detect_sram_size(struct adapter *adapter)
  291. {
  292. sram_detect_for_flex2(adapter);
  293. }
  294. #endif