hamradio/scc: add missing block braces to multi-statement if
[linux/fpc-iii.git] / drivers / media / dvb / b2c2 / flexcop-sram.c
blobcda69528548ac9d0d9757f47786cbf732491a37f
1 /*
2 * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
4 * flexcop-sram.c - functions for controlling the SRAM.
6 * see flexcop.c for copyright information.
7 */
8 #include "flexcop.h"
10 static void flexcop_sram_set_chip (struct flexcop_device *fc, flexcop_sram_type_t type)
12 flexcop_set_ibi_value(wan_ctrl_reg_71c,sram_chip,type);
15 int flexcop_sram_init(struct flexcop_device *fc)
17 switch (fc->rev) {
18 case FLEXCOP_II:
19 case FLEXCOP_IIB:
20 flexcop_sram_set_chip(fc,FC_SRAM_1_32KB);
21 break;
22 case FLEXCOP_III:
23 flexcop_sram_set_chip(fc,FC_SRAM_1_48KB);
24 break;
25 default:
26 return -EINVAL;
28 return 0;
31 int flexcop_sram_set_dest(struct flexcop_device *fc, flexcop_sram_dest_t dest, flexcop_sram_dest_target_t target)
33 flexcop_ibi_value v;
35 v = fc->read_ibi_reg(fc,sram_dest_reg_714);
37 if (fc->rev != FLEXCOP_III && target == FC_SRAM_DEST_TARGET_FC3_CA) {
38 err("SRAM destination target to available on FlexCopII(b)\n");
39 return -EINVAL;
42 deb_sram("sram dest: %x target: %x\n",dest, target);
44 if (dest & FC_SRAM_DEST_NET)
45 v.sram_dest_reg_714.NET_Dest = target;
46 if (dest & FC_SRAM_DEST_CAI)
47 v.sram_dest_reg_714.CAI_Dest = target;
48 if (dest & FC_SRAM_DEST_CAO)
49 v.sram_dest_reg_714.CAO_Dest = target;
50 if (dest & FC_SRAM_DEST_MEDIA)
51 v.sram_dest_reg_714.MEDIA_Dest = target;
53 fc->write_ibi_reg(fc,sram_dest_reg_714,v);
54 udelay(1000); /* TODO delay really necessary */
56 return 0;
58 EXPORT_SYMBOL(flexcop_sram_set_dest);
60 void flexcop_wan_set_speed(struct flexcop_device *fc, flexcop_wan_speed_t s)
62 flexcop_set_ibi_value(wan_ctrl_reg_71c,wan_speed_sig,s);
64 EXPORT_SYMBOL(flexcop_wan_set_speed);
66 void flexcop_sram_ctrl(struct flexcop_device *fc, int usb_wan, int sramdma, int maximumfill)
68 flexcop_ibi_value v = fc->read_ibi_reg(fc,sram_dest_reg_714);
69 v.sram_dest_reg_714.ctrl_usb_wan = usb_wan;
70 v.sram_dest_reg_714.ctrl_sramdma = sramdma;
71 v.sram_dest_reg_714.ctrl_maximumfill = maximumfill;
72 fc->write_ibi_reg(fc,sram_dest_reg_714,v);
74 EXPORT_SYMBOL(flexcop_sram_ctrl);
76 #if 0
77 static void flexcop_sram_write(struct adapter *adapter, u32 bank, u32 addr, u8 *buf, u32 len)
79 int i, retries;
80 u32 command;
82 for (i = 0; i < len; i++) {
83 command = bank | addr | 0x04000000 | (*buf << 0x10);
85 retries = 2;
87 while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
88 mdelay(1);
89 retries--;
92 if (retries == 0)
93 printk("%s: SRAM timeout\n", __func__);
95 write_reg_dw(adapter, 0x700, command);
97 buf++;
98 addr++;
102 static void flex_sram_read(struct adapter *adapter, u32 bank, u32 addr, u8 *buf, u32 len)
104 int i, retries;
105 u32 command, value;
107 for (i = 0; i < len; i++) {
108 command = bank | addr | 0x04008000;
110 retries = 10000;
112 while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
113 mdelay(1);
114 retries--;
117 if (retries == 0)
118 printk("%s: SRAM timeout\n", __func__);
120 write_reg_dw(adapter, 0x700, command);
122 retries = 10000;
124 while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
125 mdelay(1);
126 retries--;
129 if (retries == 0)
130 printk("%s: SRAM timeout\n", __func__);
132 value = read_reg_dw(adapter, 0x700) >> 0x10;
134 *buf = (value & 0xff);
136 addr++;
137 buf++;
141 static void sram_write_chunk(struct adapter *adapter, u32 addr, u8 *buf, u16 len)
143 u32 bank;
145 bank = 0;
147 if (adapter->dw_sram_type == 0x20000) {
148 bank = (addr & 0x18000) << 0x0d;
151 if (adapter->dw_sram_type == 0x00000) {
152 if ((addr >> 0x0f) == 0)
153 bank = 0x20000000;
154 else
155 bank = 0x10000000;
158 flex_sram_write(adapter, bank, addr & 0x7fff, buf, len);
161 static void sram_read_chunk(struct adapter *adapter, u32 addr, u8 *buf, u16 len)
163 u32 bank;
165 bank = 0;
167 if (adapter->dw_sram_type == 0x20000) {
168 bank = (addr & 0x18000) << 0x0d;
171 if (adapter->dw_sram_type == 0x00000) {
172 if ((addr >> 0x0f) == 0)
173 bank = 0x20000000;
174 else
175 bank = 0x10000000;
178 flex_sram_read(adapter, bank, addr & 0x7fff, buf, len);
181 static void sram_read(struct adapter *adapter, u32 addr, u8 *buf, u32 len)
183 u32 length;
185 while (len != 0) {
186 length = len;
188 // check if the address range belongs to the same
189 // 32K memory chip. If not, the data is read from
190 // one chip at a time.
191 if ((addr >> 0x0f) != ((addr + len - 1) >> 0x0f)) {
192 length = (((addr >> 0x0f) + 1) << 0x0f) - addr;
195 sram_read_chunk(adapter, addr, buf, length);
197 addr = addr + length;
198 buf = buf + length;
199 len = len - length;
203 static void sram_write(struct adapter *adapter, u32 addr, u8 *buf, u32 len)
205 u32 length;
207 while (len != 0) {
208 length = len;
210 // check if the address range belongs to the same
211 // 32K memory chip. If not, the data is written to
212 // one chip at a time.
213 if ((addr >> 0x0f) != ((addr + len - 1) >> 0x0f)) {
214 length = (((addr >> 0x0f) + 1) << 0x0f) - addr;
217 sram_write_chunk(adapter, addr, buf, length);
219 addr = addr + length;
220 buf = buf + length;
221 len = len - length;
225 static void sram_set_size(struct adapter *adapter, u32 mask)
227 write_reg_dw(adapter, 0x71c, (mask | (~0x30000 & read_reg_dw(adapter, 0x71c))));
230 static void sram_init(struct adapter *adapter)
232 u32 tmp;
234 tmp = read_reg_dw(adapter, 0x71c);
236 write_reg_dw(adapter, 0x71c, 1);
238 if (read_reg_dw(adapter, 0x71c) != 0) {
239 write_reg_dw(adapter, 0x71c, tmp);
241 adapter->dw_sram_type = tmp & 0x30000;
243 ddprintk("%s: dw_sram_type = %x\n", __func__, adapter->dw_sram_type);
245 } else {
247 adapter->dw_sram_type = 0x10000;
249 ddprintk("%s: dw_sram_type = %x\n", __func__, adapter->dw_sram_type);
252 /* return value is never used? */
253 /* return adapter->dw_sram_type; */
256 static int sram_test_location(struct adapter *adapter, u32 mask, u32 addr)
258 u8 tmp1, tmp2;
260 dprintk("%s: mask = %x, addr = %x\n", __func__, mask, addr);
262 sram_set_size(adapter, mask);
263 sram_init(adapter);
265 tmp2 = 0xa5;
266 tmp1 = 0x4f;
268 sram_write(adapter, addr, &tmp2, 1);
269 sram_write(adapter, addr + 4, &tmp1, 1);
271 tmp2 = 0;
273 mdelay(20);
275 sram_read(adapter, addr, &tmp2, 1);
276 sram_read(adapter, addr, &tmp2, 1);
278 dprintk("%s: wrote 0xa5, read 0x%2x\n", __func__, tmp2);
280 if (tmp2 != 0xa5)
281 return 0;
283 tmp2 = 0x5a;
284 tmp1 = 0xf4;
286 sram_write(adapter, addr, &tmp2, 1);
287 sram_write(adapter, addr + 4, &tmp1, 1);
289 tmp2 = 0;
291 mdelay(20);
293 sram_read(adapter, addr, &tmp2, 1);
294 sram_read(adapter, addr, &tmp2, 1);
296 dprintk("%s: wrote 0x5a, read 0x%2x\n", __func__, tmp2);
298 if (tmp2 != 0x5a)
299 return 0;
301 return 1;
304 static u32 sram_length(struct adapter *adapter)
306 if (adapter->dw_sram_type == 0x10000)
307 return 32768; // 32K
308 if (adapter->dw_sram_type == 0x00000)
309 return 65536; // 64K
310 if (adapter->dw_sram_type == 0x20000)
311 return 131072; // 128K
313 return 32768; // 32K
316 /* FlexcopII can work with 32K, 64K or 128K of external SRAM memory.
317 - for 128K there are 4x32K chips at bank 0,1,2,3.
318 - for 64K there are 2x32K chips at bank 1,2.
319 - for 32K there is one 32K chip at bank 0.
321 FlexCop works only with one bank at a time. The bank is selected
322 by bits 28-29 of the 0x700 register.
324 bank 0 covers addresses 0x00000-0x07fff
325 bank 1 covers addresses 0x08000-0x0ffff
326 bank 2 covers addresses 0x10000-0x17fff
327 bank 3 covers addresses 0x18000-0x1ffff
330 static int flexcop_sram_detect(struct flexcop_device *fc)
332 flexcop_ibi_value r208,r71c_0,vr71c_1;
334 r208 = fc->read_ibi_reg(fc, ctrl_208);
335 fc->write_ibi_reg(fc, ctrl_208, ibi_zero);
337 r71c_0 = fc->read_ibi_reg(fc, wan_ctrl_reg_71c);
339 write_reg_dw(adapter, 0x71c, 1);
341 tmp3 = read_reg_dw(adapter, 0x71c);
343 dprintk("%s: tmp3 = %x\n", __func__, tmp3);
345 write_reg_dw(adapter, 0x71c, tmp2);
347 // check for internal SRAM ???
348 tmp3--;
349 if (tmp3 != 0) {
350 sram_set_size(adapter, 0x10000);
351 sram_init(adapter);
352 write_reg_dw(adapter, 0x208, tmp);
354 dprintk("%s: sram size = 32K\n", __func__);
356 return 32;
359 if (sram_test_location(adapter, 0x20000, 0x18000) != 0) {
360 sram_set_size(adapter, 0x20000);
361 sram_init(adapter);
362 write_reg_dw(adapter, 0x208, tmp);
364 dprintk("%s: sram size = 128K\n", __func__);
366 return 128;
369 if (sram_test_location(adapter, 0x00000, 0x10000) != 0) {
370 sram_set_size(adapter, 0x00000);
371 sram_init(adapter);
372 write_reg_dw(adapter, 0x208, tmp);
374 dprintk("%s: sram size = 64K\n", __func__);
376 return 64;
379 if (sram_test_location(adapter, 0x10000, 0x00000) != 0) {
380 sram_set_size(adapter, 0x10000);
381 sram_init(adapter);
382 write_reg_dw(adapter, 0x208, tmp);
384 dprintk("%s: sram size = 32K\n", __func__);
386 return 32;
389 sram_set_size(adapter, 0x10000);
390 sram_init(adapter);
391 write_reg_dw(adapter, 0x208, tmp);
393 dprintk("%s: SRAM detection failed. Set to 32K \n", __func__);
395 return 0;
398 static void sll_detect_sram_size(struct adapter *adapter)
400 sram_detect_for_flex2(adapter);
403 #endif