1 // SPDX-License-Identifier: GPL-2.0
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
9 static void flexcop_sram_set_chip(struct flexcop_device
*fc
,
10 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
)
20 flexcop_sram_set_chip(fc
, FC_SRAM_1_32KB
);
23 flexcop_sram_set_chip(fc
, FC_SRAM_1_48KB
);
31 int flexcop_sram_set_dest(struct flexcop_device
*fc
, flexcop_sram_dest_t dest
,
32 flexcop_sram_dest_target_t target
)
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");
41 deb_sram("sram dest: %x target: %x\n", dest
, target
);
43 if (dest
& FC_SRAM_DEST_NET
)
44 v
.sram_dest_reg_714
.NET_Dest
= target
;
45 if (dest
& FC_SRAM_DEST_CAI
)
46 v
.sram_dest_reg_714
.CAI_Dest
= target
;
47 if (dest
& FC_SRAM_DEST_CAO
)
48 v
.sram_dest_reg_714
.CAO_Dest
= target
;
49 if (dest
& FC_SRAM_DEST_MEDIA
)
50 v
.sram_dest_reg_714
.MEDIA_Dest
= target
;
52 fc
->write_ibi_reg(fc
,sram_dest_reg_714
,v
);
53 udelay(1000); /* TODO delay really necessary */
57 EXPORT_SYMBOL(flexcop_sram_set_dest
);
59 void flexcop_wan_set_speed(struct flexcop_device
*fc
, flexcop_wan_speed_t s
)
61 flexcop_set_ibi_value(wan_ctrl_reg_71c
,wan_speed_sig
,s
);
63 EXPORT_SYMBOL(flexcop_wan_set_speed
);
65 void flexcop_sram_ctrl(struct flexcop_device
*fc
, int usb_wan
, int sramdma
, int maximumfill
)
67 flexcop_ibi_value v
= fc
->read_ibi_reg(fc
,sram_dest_reg_714
);
68 v
.sram_dest_reg_714
.ctrl_usb_wan
= usb_wan
;
69 v
.sram_dest_reg_714
.ctrl_sramdma
= sramdma
;
70 v
.sram_dest_reg_714
.ctrl_maximumfill
= maximumfill
;
71 fc
->write_ibi_reg(fc
,sram_dest_reg_714
,v
);
73 EXPORT_SYMBOL(flexcop_sram_ctrl
);
76 static void flexcop_sram_write(struct adapter
*adapter
, u32 bank
, u32 addr
, u8
*buf
, u32 len
)
81 for (i
= 0; i
< len
; i
++) {
82 command
= bank
| addr
| 0x04000000 | (*buf
<< 0x10);
86 while (((read_reg_dw(adapter
, 0x700) & 0x80000000) != 0) && (retries
> 0)) {
92 printk("%s: SRAM timeout\n", __func__
);
94 write_reg_dw(adapter
, 0x700, command
);
101 static void flex_sram_read(struct adapter
*adapter
, u32 bank
, u32 addr
, u8
*buf
, u32 len
)
106 for (i
= 0; i
< len
; i
++) {
107 command
= bank
| addr
| 0x04008000;
111 while (((read_reg_dw(adapter
, 0x700) & 0x80000000) != 0) && (retries
> 0)) {
117 printk("%s: SRAM timeout\n", __func__
);
119 write_reg_dw(adapter
, 0x700, command
);
123 while (((read_reg_dw(adapter
, 0x700) & 0x80000000) != 0) && (retries
> 0)) {
129 printk("%s: SRAM timeout\n", __func__
);
131 value
= read_reg_dw(adapter
, 0x700) >> 0x10;
133 *buf
= (value
& 0xff);
140 static void sram_write_chunk(struct adapter
*adapter
, u32 addr
, u8
*buf
, u16 len
)
146 if (adapter
->dw_sram_type
== 0x20000) {
147 bank
= (addr
& 0x18000) << 0x0d;
150 if (adapter
->dw_sram_type
== 0x00000) {
151 if ((addr
>> 0x0f) == 0)
156 flex_sram_write(adapter
, bank
, addr
& 0x7fff, buf
, len
);
159 static void sram_read_chunk(struct adapter
*adapter
, u32 addr
, u8
*buf
, u16 len
)
164 if (adapter
->dw_sram_type
== 0x20000) {
165 bank
= (addr
& 0x18000) << 0x0d;
168 if (adapter
->dw_sram_type
== 0x00000) {
169 if ((addr
>> 0x0f) == 0)
174 flex_sram_read(adapter
, bank
, addr
& 0x7fff, buf
, len
);
177 static void sram_read(struct adapter
*adapter
, u32 addr
, u8
*buf
, u32 len
)
182 /* check if the address range belongs to the same
183 * 32K memory chip. If not, the data is read
184 * from one chip at a time */
185 if ((addr
>> 0x0f) != ((addr
+ len
- 1) >> 0x0f)) {
186 length
= (((addr
>> 0x0f) + 1) << 0x0f) - addr
;
189 sram_read_chunk(adapter
, addr
, buf
, length
);
190 addr
= addr
+ length
;
196 static void sram_write(struct adapter
*adapter
, u32 addr
, u8
*buf
, u32 len
)
202 /* check if the address range belongs to the same
203 * 32K memory chip. If not, the data is
204 * written to one chip at a time */
205 if ((addr
>> 0x0f) != ((addr
+ len
- 1) >> 0x0f)) {
206 length
= (((addr
>> 0x0f) + 1) << 0x0f) - addr
;
209 sram_write_chunk(adapter
, addr
, buf
, length
);
210 addr
= addr
+ length
;
216 static void sram_set_size(struct adapter
*adapter
, u32 mask
)
218 write_reg_dw(adapter
, 0x71c,
219 (mask
| (~0x30000 & read_reg_dw(adapter
, 0x71c))));
222 static void sram_init(struct adapter
*adapter
)
225 tmp
= read_reg_dw(adapter
, 0x71c);
226 write_reg_dw(adapter
, 0x71c, 1);
228 if (read_reg_dw(adapter
, 0x71c) != 0) {
229 write_reg_dw(adapter
, 0x71c, tmp
);
230 adapter
->dw_sram_type
= tmp
& 0x30000;
231 ddprintk("%s: dw_sram_type = %x\n", __func__
, adapter
->dw_sram_type
);
233 adapter
->dw_sram_type
= 0x10000;
234 ddprintk("%s: dw_sram_type = %x\n", __func__
, adapter
->dw_sram_type
);
238 static int sram_test_location(struct adapter
*adapter
, u32 mask
, u32 addr
)
241 dprintk("%s: mask = %x, addr = %x\n", __func__
, mask
, addr
);
243 sram_set_size(adapter
, mask
);
249 sram_write(adapter
, addr
, &tmp2
, 1);
250 sram_write(adapter
, addr
+ 4, &tmp1
, 1);
255 sram_read(adapter
, addr
, &tmp2
, 1);
256 sram_read(adapter
, addr
, &tmp2
, 1);
258 dprintk("%s: wrote 0xa5, read 0x%2x\n", __func__
, tmp2
);
266 sram_write(adapter
, addr
, &tmp2
, 1);
267 sram_write(adapter
, addr
+ 4, &tmp1
, 1);
272 sram_read(adapter
, addr
, &tmp2
, 1);
273 sram_read(adapter
, addr
, &tmp2
, 1);
275 dprintk("%s: wrote 0x5a, read 0x%2x\n", __func__
, tmp2
);
282 static u32
sram_length(struct adapter
*adapter
)
284 if (adapter
->dw_sram_type
== 0x10000)
285 return 32768; /* 32K */
286 if (adapter
->dw_sram_type
== 0x00000)
287 return 65536; /* 64K */
288 if (adapter
->dw_sram_type
== 0x20000)
289 return 131072; /* 128K */
290 return 32768; /* 32K */
293 /* FlexcopII can work with 32K, 64K or 128K of external SRAM memory.
294 - for 128K there are 4x32K chips at bank 0,1,2,3.
295 - for 64K there are 2x32K chips at bank 1,2.
296 - for 32K there is one 32K chip at bank 0.
298 FlexCop works only with one bank at a time. The bank is selected
299 by bits 28-29 of the 0x700 register.
301 bank 0 covers addresses 0x00000-0x07fff
302 bank 1 covers addresses 0x08000-0x0ffff
303 bank 2 covers addresses 0x10000-0x17fff
304 bank 3 covers addresses 0x18000-0x1ffff */
306 static int flexcop_sram_detect(struct flexcop_device
*fc
)
308 flexcop_ibi_value r208
, r71c_0
, vr71c_1
;
309 r208
= fc
->read_ibi_reg(fc
, ctrl_208
);
310 fc
->write_ibi_reg(fc
, ctrl_208
, ibi_zero
);
312 r71c_0
= fc
->read_ibi_reg(fc
, wan_ctrl_reg_71c
);
313 write_reg_dw(adapter
, 0x71c, 1);
314 tmp3
= read_reg_dw(adapter
, 0x71c);
315 dprintk("%s: tmp3 = %x\n", __func__
, tmp3
);
316 write_reg_dw(adapter
, 0x71c, tmp2
);
318 // check for internal SRAM ???
321 sram_set_size(adapter
, 0x10000);
323 write_reg_dw(adapter
, 0x208, tmp
);
324 dprintk("%s: sram size = 32K\n", __func__
);
328 if (sram_test_location(adapter
, 0x20000, 0x18000) != 0) {
329 sram_set_size(adapter
, 0x20000);
331 write_reg_dw(adapter
, 0x208, tmp
);
332 dprintk("%s: sram size = 128K\n", __func__
);
336 if (sram_test_location(adapter
, 0x00000, 0x10000) != 0) {
337 sram_set_size(adapter
, 0x00000);
339 write_reg_dw(adapter
, 0x208, tmp
);
340 dprintk("%s: sram size = 64K\n", __func__
);
344 if (sram_test_location(adapter
, 0x10000, 0x00000) != 0) {
345 sram_set_size(adapter
, 0x10000);
347 write_reg_dw(adapter
, 0x208, tmp
);
348 dprintk("%s: sram size = 32K\n", __func__
);
352 sram_set_size(adapter
, 0x10000);
354 write_reg_dw(adapter
, 0x208, tmp
);
355 dprintk("%s: SRAM detection failed. Set to 32K \n", __func__
);
359 static void sll_detect_sram_size(struct adapter
*adapter
)
361 sram_detect_for_flex2(adapter
);