x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / drivers / media / dvb / b2c2 / flexcop-sram.c
blobf2199e43e803594253a1ad655d93b196ecffb843
1 /*
2 * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
3 * flexcop-sram.c - functions for controlling the SRAM
4 * see flexcop.c for copyright information
5 */
6 #include "flexcop.h"
8 static void flexcop_sram_set_chip(struct flexcop_device *fc,
9 flexcop_sram_type_t type)
11 flexcop_set_ibi_value(wan_ctrl_reg_71c, sram_chip, type);
14 int flexcop_sram_init(struct flexcop_device *fc)
16 switch (fc->rev) {
17 case FLEXCOP_II:
18 case FLEXCOP_IIB:
19 flexcop_sram_set_chip(fc, FC_SRAM_1_32KB);
20 break;
21 case FLEXCOP_III:
22 flexcop_sram_set_chip(fc, FC_SRAM_1_48KB);
23 break;
24 default:
25 return -EINVAL;
27 return 0;
30 int flexcop_sram_set_dest(struct flexcop_device *fc, flexcop_sram_dest_t dest,
31 flexcop_sram_dest_target_t target)
33 flexcop_ibi_value v;
34 v = fc->read_ibi_reg(fc, sram_dest_reg_714);
36 if (fc->rev != FLEXCOP_III && target == FC_SRAM_DEST_TARGET_FC3_CA) {
37 err("SRAM destination target to available on FlexCopII(b)\n");
38 return -EINVAL;
40 deb_sram("sram dest: %x target: %x\n", dest, target);
42 if (dest & FC_SRAM_DEST_NET)
43 v.sram_dest_reg_714.NET_Dest = target;
44 if (dest & FC_SRAM_DEST_CAI)
45 v.sram_dest_reg_714.CAI_Dest = target;
46 if (dest & FC_SRAM_DEST_CAO)
47 v.sram_dest_reg_714.CAO_Dest = target;
48 if (dest & FC_SRAM_DEST_MEDIA)
49 v.sram_dest_reg_714.MEDIA_Dest = target;
51 fc->write_ibi_reg(fc,sram_dest_reg_714,v);
52 udelay(1000); /* TODO delay really necessary */
54 return 0;
56 EXPORT_SYMBOL(flexcop_sram_set_dest);
58 void flexcop_wan_set_speed(struct flexcop_device *fc, flexcop_wan_speed_t s)
60 flexcop_set_ibi_value(wan_ctrl_reg_71c,wan_speed_sig,s);
62 EXPORT_SYMBOL(flexcop_wan_set_speed);
64 void flexcop_sram_ctrl(struct flexcop_device *fc, int usb_wan, int sramdma, int maximumfill)
66 flexcop_ibi_value v = fc->read_ibi_reg(fc,sram_dest_reg_714);
67 v.sram_dest_reg_714.ctrl_usb_wan = usb_wan;
68 v.sram_dest_reg_714.ctrl_sramdma = sramdma;
69 v.sram_dest_reg_714.ctrl_maximumfill = maximumfill;
70 fc->write_ibi_reg(fc,sram_dest_reg_714,v);
72 EXPORT_SYMBOL(flexcop_sram_ctrl);
74 #if 0
75 static void flexcop_sram_write(struct adapter *adapter, u32 bank, u32 addr, u8 *buf, u32 len)
77 int i, retries;
78 u32 command;
80 for (i = 0; i < len; i++) {
81 command = bank | addr | 0x04000000 | (*buf << 0x10);
83 retries = 2;
85 while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
86 mdelay(1);
87 retries--;
90 if (retries == 0)
91 printk("%s: SRAM timeout\n", __func__);
93 write_reg_dw(adapter, 0x700, command);
95 buf++;
96 addr++;
100 static void flex_sram_read(struct adapter *adapter, u32 bank, u32 addr, u8 *buf, u32 len)
102 int i, retries;
103 u32 command, value;
105 for (i = 0; i < len; i++) {
106 command = bank | addr | 0x04008000;
108 retries = 10000;
110 while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
111 mdelay(1);
112 retries--;
115 if (retries == 0)
116 printk("%s: SRAM timeout\n", __func__);
118 write_reg_dw(adapter, 0x700, command);
120 retries = 10000;
122 while (((read_reg_dw(adapter, 0x700) & 0x80000000) != 0) && (retries > 0)) {
123 mdelay(1);
124 retries--;
127 if (retries == 0)
128 printk("%s: SRAM timeout\n", __func__);
130 value = read_reg_dw(adapter, 0x700) >> 0x10;
132 *buf = (value & 0xff);
134 addr++;
135 buf++;
139 static void sram_write_chunk(struct adapter *adapter, u32 addr, u8 *buf, u16 len)
141 u32 bank;
143 bank = 0;
145 if (adapter->dw_sram_type == 0x20000) {
146 bank = (addr & 0x18000) << 0x0d;
149 if (adapter->dw_sram_type == 0x00000) {
150 if ((addr >> 0x0f) == 0)
151 bank = 0x20000000;
152 else
153 bank = 0x10000000;
155 flex_sram_write(adapter, bank, addr & 0x7fff, buf, len);
158 static void sram_read_chunk(struct adapter *adapter, u32 addr, u8 *buf, u16 len)
160 u32 bank;
161 bank = 0;
163 if (adapter->dw_sram_type == 0x20000) {
164 bank = (addr & 0x18000) << 0x0d;
167 if (adapter->dw_sram_type == 0x00000) {
168 if ((addr >> 0x0f) == 0)
169 bank = 0x20000000;
170 else
171 bank = 0x10000000;
173 flex_sram_read(adapter, bank, addr & 0x7fff, buf, len);
176 static void sram_read(struct adapter *adapter, u32 addr, u8 *buf, u32 len)
178 u32 length;
179 while (len != 0) {
180 length = len;
181 /* check if the address range belongs to the same
182 * 32K memory chip. If not, the data is read
183 * from one chip at a time */
184 if ((addr >> 0x0f) != ((addr + len - 1) >> 0x0f)) {
185 length = (((addr >> 0x0f) + 1) << 0x0f) - addr;
188 sram_read_chunk(adapter, addr, buf, length);
189 addr = addr + length;
190 buf = buf + length;
191 len = len - length;
195 static void sram_write(struct adapter *adapter, u32 addr, u8 *buf, u32 len)
197 u32 length;
198 while (len != 0) {
199 length = len;
201 /* check if the address range belongs to the same
202 * 32K memory chip. If not, the data is
203 * written to one chip at a time */
204 if ((addr >> 0x0f) != ((addr + len - 1) >> 0x0f)) {
205 length = (((addr >> 0x0f) + 1) << 0x0f) - addr;
208 sram_write_chunk(adapter, addr, buf, length);
209 addr = addr + length;
210 buf = buf + length;
211 len = len - length;
215 static void sram_set_size(struct adapter *adapter, u32 mask)
217 write_reg_dw(adapter, 0x71c,
218 (mask | (~0x30000 & read_reg_dw(adapter, 0x71c))));
221 static void sram_init(struct adapter *adapter)
223 u32 tmp;
224 tmp = read_reg_dw(adapter, 0x71c);
225 write_reg_dw(adapter, 0x71c, 1);
227 if (read_reg_dw(adapter, 0x71c) != 0) {
228 write_reg_dw(adapter, 0x71c, tmp);
229 adapter->dw_sram_type = tmp & 0x30000;
230 ddprintk("%s: dw_sram_type = %x\n", __func__, adapter->dw_sram_type);
231 } else {
232 adapter->dw_sram_type = 0x10000;
233 ddprintk("%s: dw_sram_type = %x\n", __func__, adapter->dw_sram_type);
237 static int sram_test_location(struct adapter *adapter, u32 mask, u32 addr)
239 u8 tmp1, tmp2;
240 dprintk("%s: mask = %x, addr = %x\n", __func__, mask, addr);
242 sram_set_size(adapter, mask);
243 sram_init(adapter);
245 tmp2 = 0xa5;
246 tmp1 = 0x4f;
248 sram_write(adapter, addr, &tmp2, 1);
249 sram_write(adapter, addr + 4, &tmp1, 1);
251 tmp2 = 0;
252 mdelay(20);
254 sram_read(adapter, addr, &tmp2, 1);
255 sram_read(adapter, addr, &tmp2, 1);
257 dprintk("%s: wrote 0xa5, read 0x%2x\n", __func__, tmp2);
259 if (tmp2 != 0xa5)
260 return 0;
262 tmp2 = 0x5a;
263 tmp1 = 0xf4;
265 sram_write(adapter, addr, &tmp2, 1);
266 sram_write(adapter, addr + 4, &tmp1, 1);
268 tmp2 = 0;
269 mdelay(20);
271 sram_read(adapter, addr, &tmp2, 1);
272 sram_read(adapter, addr, &tmp2, 1);
274 dprintk("%s: wrote 0x5a, read 0x%2x\n", __func__, tmp2);
276 if (tmp2 != 0x5a)
277 return 0;
278 return 1;
281 static u32 sram_length(struct adapter *adapter)
283 if (adapter->dw_sram_type == 0x10000)
284 return 32768; /* 32K */
285 if (adapter->dw_sram_type == 0x00000)
286 return 65536; /* 64K */
287 if (adapter->dw_sram_type == 0x20000)
288 return 131072; /* 128K */
289 return 32768; /* 32K */
292 /* FlexcopII can work with 32K, 64K or 128K of external SRAM memory.
293 - for 128K there are 4x32K chips at bank 0,1,2,3.
294 - for 64K there are 2x32K chips at bank 1,2.
295 - for 32K there is one 32K chip at bank 0.
297 FlexCop works only with one bank at a time. The bank is selected
298 by bits 28-29 of the 0x700 register.
300 bank 0 covers addresses 0x00000-0x07fff
301 bank 1 covers addresses 0x08000-0x0ffff
302 bank 2 covers addresses 0x10000-0x17fff
303 bank 3 covers addresses 0x18000-0x1ffff */
305 static int flexcop_sram_detect(struct flexcop_device *fc)
307 flexcop_ibi_value r208, r71c_0, vr71c_1;
308 r208 = fc->read_ibi_reg(fc, ctrl_208);
309 fc->write_ibi_reg(fc, ctrl_208, ibi_zero);
311 r71c_0 = fc->read_ibi_reg(fc, wan_ctrl_reg_71c);
312 write_reg_dw(adapter, 0x71c, 1);
313 tmp3 = read_reg_dw(adapter, 0x71c);
314 dprintk("%s: tmp3 = %x\n", __func__, tmp3);
315 write_reg_dw(adapter, 0x71c, tmp2);
317 // check for internal SRAM ???
318 tmp3--;
319 if (tmp3 != 0) {
320 sram_set_size(adapter, 0x10000);
321 sram_init(adapter);
322 write_reg_dw(adapter, 0x208, tmp);
323 dprintk("%s: sram size = 32K\n", __func__);
324 return 32;
327 if (sram_test_location(adapter, 0x20000, 0x18000) != 0) {
328 sram_set_size(adapter, 0x20000);
329 sram_init(adapter);
330 write_reg_dw(adapter, 0x208, tmp);
331 dprintk("%s: sram size = 128K\n", __func__);
332 return 128;
335 if (sram_test_location(adapter, 0x00000, 0x10000) != 0) {
336 sram_set_size(adapter, 0x00000);
337 sram_init(adapter);
338 write_reg_dw(adapter, 0x208, tmp);
339 dprintk("%s: sram size = 64K\n", __func__);
340 return 64;
343 if (sram_test_location(adapter, 0x10000, 0x00000) != 0) {
344 sram_set_size(adapter, 0x10000);
345 sram_init(adapter);
346 write_reg_dw(adapter, 0x208, tmp);
347 dprintk("%s: sram size = 32K\n", __func__);
348 return 32;
351 sram_set_size(adapter, 0x10000);
352 sram_init(adapter);
353 write_reg_dw(adapter, 0x208, tmp);
354 dprintk("%s: SRAM detection failed. Set to 32K \n", __func__);
355 return 0;
358 static void sll_detect_sram_size(struct adapter *adapter)
360 sram_detect_for_flex2(adapter);
363 #endif