2 Copyright (c), 2004-2005,2007-2010 Trident Microsystems, Inc.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of Trident Microsystems nor Hauppauge Computer Works
14 nor the names of its contributors may be used to endorse or promote
15 products derived from this software without specific prior written
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
31 /*******************************************************************************
32 * FILENAME: $Id: drx_dap_fasi.c,v 1.7 2009/12/28 14:36:21 carlo Exp $
36 * Data access protocol: Fast Access Sequential Interface (fasi)
37 * Fast access, because of short addressing format (16 instead of 32 bits addr)
38 * Sequential, because of I2C.
39 * These functions know how the chip's memory and registers are to be accessed,
42 * These functions should not need adapting to a new platform.
50 *******************************************************************************/
52 #include "drx_dap_fasi.h"
53 #include "drx_driver.h" /* for drxbsp_hst_memcpy() */
55 /*============================================================================*/
57 /* Function prototypes */
58 static int drxdap_fasi_write_block(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
59 dr_xaddr_t addr
, /* address of register/memory */
60 u16 datasize
, /* size of data */
61 u8
*data
, /* data to send */
62 dr_xflags_t flags
); /* special device flags */
64 static int drxdap_fasi_read_block(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
65 dr_xaddr_t addr
, /* address of register/memory */
66 u16 datasize
, /* size of data */
67 u8
*data
, /* data to send */
68 dr_xflags_t flags
); /* special device flags */
70 static int drxdap_fasi_write_reg8(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
71 dr_xaddr_t addr
, /* address of register */
72 u8 data
, /* data to write */
73 dr_xflags_t flags
); /* special device flags */
75 static int drxdap_fasi_read_reg8(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
76 dr_xaddr_t addr
, /* address of register */
77 u8
*data
, /* buffer to receive data */
78 dr_xflags_t flags
); /* special device flags */
80 static int drxdap_fasi_read_modify_write_reg8(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
81 dr_xaddr_t waddr
, /* address of register */
82 dr_xaddr_t raddr
, /* address to read back from */
83 u8 datain
, /* data to send */
84 u8
*dataout
); /* data to receive back */
86 static int drxdap_fasi_write_reg16(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
87 dr_xaddr_t addr
, /* address of register */
88 u16 data
, /* data to write */
89 dr_xflags_t flags
); /* special device flags */
91 static int drxdap_fasi_read_reg16(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
92 dr_xaddr_t addr
, /* address of register */
93 u16
*data
, /* buffer to receive data */
94 dr_xflags_t flags
); /* special device flags */
96 static int drxdap_fasi_read_modify_write_reg16(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
97 dr_xaddr_t waddr
, /* address of register */
98 dr_xaddr_t raddr
, /* address to read back from */
99 u16 datain
, /* data to send */
100 u16
*dataout
); /* data to receive back */
102 static int drxdap_fasi_write_reg32(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
103 dr_xaddr_t addr
, /* address of register */
104 u32 data
, /* data to write */
105 dr_xflags_t flags
); /* special device flags */
107 static int drxdap_fasi_read_reg32(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
108 dr_xaddr_t addr
, /* address of register */
109 u32
*data
, /* buffer to receive data */
110 dr_xflags_t flags
); /* special device flags */
112 static int drxdap_fasi_read_modify_write_reg32(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
113 dr_xaddr_t waddr
, /* address of register */
114 dr_xaddr_t raddr
, /* address to read back from */
115 u32 datain
, /* data to send */
116 u32
*dataout
); /* data to receive back */
118 /* The version structure of this protocol implementation */
119 char drx_dap_fasi_module_name
[] = "FASI Data Access Protocol";
120 char drx_dap_fasi_version_text
[] = "";
122 drx_version_t drx_dap_fasi_version
= {
123 DRX_MODULE_DAP
, /**< type identifier of the module */
124 drx_dap_fasi_module_name
, /**< name or description of module */
126 0, /**< major version number */
127 0, /**< minor version number */
128 0, /**< patch version number */
129 drx_dap_fasi_version_text
/**< version as text string */
132 /* The structure containing the protocol interface */
133 drx_access_func_t drx_dap_fasi_funct_g
= {
134 &drx_dap_fasi_version
,
135 drxdap_fasi_write_block
, /* Supported */
136 drxdap_fasi_read_block
, /* Supported */
137 drxdap_fasi_write_reg8
, /* Not supported */
138 drxdap_fasi_read_reg8
, /* Not supported */
139 drxdap_fasi_read_modify_write_reg8
, /* Not supported */
140 drxdap_fasi_write_reg16
, /* Supported */
141 drxdap_fasi_read_reg16
, /* Supported */
142 drxdap_fasi_read_modify_write_reg16
, /* Supported */
143 drxdap_fasi_write_reg32
, /* Supported */
144 drxdap_fasi_read_reg32
, /* Supported */
145 drxdap_fasi_read_modify_write_reg32
/* Not supported */
148 /*============================================================================*/
150 /* Functions not supported by protocol*/
152 static int drxdap_fasi_write_reg8(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
153 dr_xaddr_t addr
, /* address of register */
154 u8 data
, /* data to write */
156 { /* special device flags */
157 return DRX_STS_ERROR
;
160 static int drxdap_fasi_read_reg8(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
161 dr_xaddr_t addr
, /* address of register */
162 u8
*data
, /* buffer to receive data */
164 { /* special device flags */
165 return DRX_STS_ERROR
;
168 static int drxdap_fasi_read_modify_write_reg8(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
169 dr_xaddr_t waddr
, /* address of register */
170 dr_xaddr_t raddr
, /* address to read back from */
171 u8 datain
, /* data to send */
173 { /* data to receive back */
174 return DRX_STS_ERROR
;
177 static int drxdap_fasi_read_modify_write_reg32(struct i2c_device_addr
*dev_addr
, /* address of I2C device */
178 dr_xaddr_t waddr
, /* address of register */
179 dr_xaddr_t raddr
, /* address to read back from */
180 u32 datain
, /* data to send */
182 { /* data to receive back */
183 return DRX_STS_ERROR
;
186 /*============================================================================*/
188 /******************************
190 * int drxdap_fasi_read_block (
191 * struct i2c_device_addr *dev_addr, -- address of I2C device
192 * dr_xaddr_t addr, -- address of chip register/memory
193 * u16 datasize, -- number of bytes to read
194 * u8 *data, -- data to receive
195 * dr_xflags_t flags) -- special device flags
197 * Read block data from chip address. Because the chip is word oriented,
198 * the number of bytes to read must be even.
200 * Make sure that the buffer to receive the data is large enough.
202 * Although this function expects an even number of bytes, it is still byte
203 * oriented, and the data read back is NOT translated to the endianness of
204 * the target platform.
207 * - DRX_STS_OK if reading was successful
208 * in that case: data read is in *data.
209 * - DRX_STS_ERROR if anything went wrong
211 ******************************/
213 static int drxdap_fasi_read_block(struct i2c_device_addr
*dev_addr
,
216 u8
*data
, dr_xflags_t flags
)
221 u16 overhead_size
= 0;
223 /* Check parameters ******************************************************* */
224 if (dev_addr
== NULL
) {
225 return DRX_STS_INVALID_ARG
;
228 overhead_size
= (IS_I2C_10BIT(dev_addr
->i2c_addr
) ? 2 : 1) +
229 (DRXDAP_FASI_LONG_FORMAT(addr
) ? 4 : 2);
231 if ((DRXDAP_FASI_OFFSET_TOO_LARGE(addr
)) ||
232 ((!(DRXDAPFASI_LONG_ADDR_ALLOWED
)) &&
233 DRXDAP_FASI_LONG_FORMAT(addr
)) ||
234 (overhead_size
> (DRXDAP_MAX_WCHUNKSIZE
)) ||
235 ((datasize
!= 0) && (data
== NULL
)) || ((datasize
& 1) == 1)) {
236 return DRX_STS_INVALID_ARG
;
239 /* ReadModifyWrite & mode flag bits are not allowed */
240 flags
&= (~DRXDAP_FASI_RMW
& ~DRXDAP_FASI_MODEFLAGS
);
241 #if DRXDAP_SINGLE_MASTER
242 flags
|= DRXDAP_FASI_SINGLE_MASTER
;
245 /* Read block from I2C **************************************************** */
247 u16 todo
= (datasize
< DRXDAP_MAX_RCHUNKSIZE
?
248 datasize
: DRXDAP_MAX_RCHUNKSIZE
);
252 addr
&= ~DRXDAP_FASI_FLAGS
;
255 #if ((DRXDAPFASI_LONG_ADDR_ALLOWED == 1) && \
256 (DRXDAPFASI_SHORT_ADDR_ALLOWED == 1))
257 /* short format address preferred but long format otherwise */
258 if (DRXDAP_FASI_LONG_FORMAT(addr
)) {
260 #if (DRXDAPFASI_LONG_ADDR_ALLOWED == 1)
261 buf
[bufx
++] = (u8
) (((addr
<< 1) & 0xFF) | 0x01);
262 buf
[bufx
++] = (u8
) ((addr
>> 16) & 0xFF);
263 buf
[bufx
++] = (u8
) ((addr
>> 24) & 0xFF);
264 buf
[bufx
++] = (u8
) ((addr
>> 7) & 0xFF);
266 #if ((DRXDAPFASI_LONG_ADDR_ALLOWED == 1) && \
267 (DRXDAPFASI_SHORT_ADDR_ALLOWED == 1))
270 #if (DRXDAPFASI_SHORT_ADDR_ALLOWED == 1)
271 buf
[bufx
++] = (u8
) ((addr
<< 1) & 0xFF);
273 (u8
) (((addr
>> 16) & 0x0F) |
274 ((addr
>> 18) & 0xF0));
276 #if ((DRXDAPFASI_LONG_ADDR_ALLOWED == 1) && \
277 (DRXDAPFASI_SHORT_ADDR_ALLOWED == 1))
281 #if DRXDAP_SINGLE_MASTER
283 * In single master mode, split the read and write actions.
284 * No special action is needed for write chunks here.
286 rc
= drxbsp_i2c_write_read(dev_addr
, bufx
, buf
, 0, 0, 0);
287 if (rc
== DRX_STS_OK
) {
288 rc
= drxbsp_i2c_write_read(0, 0, 0, dev_addr
, todo
, data
);
291 /* In multi master mode, do everything in one RW action */
292 rc
= drxbsp_i2c_write_read(dev_addr
, bufx
, buf
, dev_addr
, todo
,
298 } while (datasize
&& rc
== DRX_STS_OK
);
303 /******************************
305 * int drxdap_fasi_read_modify_write_reg16 (
306 * struct i2c_device_addr *dev_addr, -- address of I2C device
307 * dr_xaddr_t waddr, -- address of chip register/memory
308 * dr_xaddr_t raddr, -- chip address to read back from
309 * u16 wdata, -- data to send
310 * u16 *rdata) -- data to receive back
312 * Write 16-bit data, then read back the original contents of that location.
313 * Requires long addressing format to be allowed.
315 * Before sending data, the data is converted to little endian. The
316 * data received back is converted back to the target platform's endianness.
318 * WARNING: This function is only guaranteed to work if there is one
319 * master on the I2C bus.
322 * - DRX_STS_OK if reading was successful
323 * in that case: read back data is at *rdata
324 * - DRX_STS_ERROR if anything went wrong
326 ******************************/
328 static int drxdap_fasi_read_modify_write_reg16(struct i2c_device_addr
*dev_addr
,
331 u16 wdata
, u16
*rdata
)
333 int rc
= DRX_STS_ERROR
;
335 #if (DRXDAPFASI_LONG_ADDR_ALLOWED == 1)
337 return DRX_STS_INVALID_ARG
;
340 rc
= drxdap_fasi_write_reg16(dev_addr
, waddr
, wdata
, DRXDAP_FASI_RMW
);
341 if (rc
== DRX_STS_OK
) {
342 rc
= drxdap_fasi_read_reg16(dev_addr
, raddr
, rdata
, 0);
349 /******************************
351 * int drxdap_fasi_read_reg16 (
352 * struct i2c_device_addr *dev_addr, -- address of I2C device
353 * dr_xaddr_t addr, -- address of chip register/memory
354 * u16 *data, -- data to receive
355 * dr_xflags_t flags) -- special device flags
357 * Read one 16-bit register or memory location. The data received back is
358 * converted back to the target platform's endianness.
361 * - DRX_STS_OK if reading was successful
362 * in that case: read data is at *data
363 * - DRX_STS_ERROR if anything went wrong
365 ******************************/
367 static int drxdap_fasi_read_reg16(struct i2c_device_addr
*dev_addr
,
369 u16
*data
, dr_xflags_t flags
)
371 u8 buf
[sizeof(*data
)];
375 return DRX_STS_INVALID_ARG
;
377 rc
= drxdap_fasi_read_block(dev_addr
, addr
, sizeof(*data
), buf
, flags
);
378 *data
= buf
[0] + (((u16
) buf
[1]) << 8);
382 /******************************
384 * int drxdap_fasi_read_reg32 (
385 * struct i2c_device_addr *dev_addr, -- address of I2C device
386 * dr_xaddr_t addr, -- address of chip register/memory
387 * u32 *data, -- data to receive
388 * dr_xflags_t flags) -- special device flags
390 * Read one 32-bit register or memory location. The data received back is
391 * converted back to the target platform's endianness.
394 * - DRX_STS_OK if reading was successful
395 * in that case: read data is at *data
396 * - DRX_STS_ERROR if anything went wrong
398 ******************************/
400 static int drxdap_fasi_read_reg32(struct i2c_device_addr
*dev_addr
,
402 u32
*data
, dr_xflags_t flags
)
404 u8 buf
[sizeof(*data
)];
408 return DRX_STS_INVALID_ARG
;
410 rc
= drxdap_fasi_read_block(dev_addr
, addr
, sizeof(*data
), buf
, flags
);
411 *data
= (((u32
) buf
[0]) << 0) +
412 (((u32
) buf
[1]) << 8) +
413 (((u32
) buf
[2]) << 16) + (((u32
) buf
[3]) << 24);
417 /******************************
419 * int drxdap_fasi_write_block (
420 * struct i2c_device_addr *dev_addr, -- address of I2C device
421 * dr_xaddr_t addr, -- address of chip register/memory
422 * u16 datasize, -- number of bytes to read
423 * u8 *data, -- data to receive
424 * dr_xflags_t flags) -- special device flags
426 * Write block data to chip address. Because the chip is word oriented,
427 * the number of bytes to write must be even.
429 * Although this function expects an even number of bytes, it is still byte
430 * oriented, and the data being written is NOT translated from the endianness of
431 * the target platform.
434 * - DRX_STS_OK if writing was successful
435 * - DRX_STS_ERROR if anything went wrong
437 ******************************/
439 static int drxdap_fasi_write_block(struct i2c_device_addr
*dev_addr
,
442 u8
*data
, dr_xflags_t flags
)
444 u8 buf
[DRXDAP_MAX_WCHUNKSIZE
];
445 int st
= DRX_STS_ERROR
;
446 int first_err
= DRX_STS_OK
;
447 u16 overhead_size
= 0;
450 /* Check parameters ******************************************************* */
451 if (dev_addr
== NULL
) {
452 return DRX_STS_INVALID_ARG
;
455 overhead_size
= (IS_I2C_10BIT(dev_addr
->i2c_addr
) ? 2 : 1) +
456 (DRXDAP_FASI_LONG_FORMAT(addr
) ? 4 : 2);
458 if ((DRXDAP_FASI_OFFSET_TOO_LARGE(addr
)) ||
459 ((!(DRXDAPFASI_LONG_ADDR_ALLOWED
)) &&
460 DRXDAP_FASI_LONG_FORMAT(addr
)) ||
461 (overhead_size
> (DRXDAP_MAX_WCHUNKSIZE
)) ||
462 ((datasize
!= 0) && (data
== NULL
)) || ((datasize
& 1) == 1)) {
463 return DRX_STS_INVALID_ARG
;
466 flags
&= DRXDAP_FASI_FLAGS
;
467 flags
&= ~DRXDAP_FASI_MODEFLAGS
;
468 #if DRXDAP_SINGLE_MASTER
469 flags
|= DRXDAP_FASI_SINGLE_MASTER
;
472 /* Write block to I2C ***************************************************** */
473 block_size
= ((DRXDAP_MAX_WCHUNKSIZE
) - overhead_size
) & ~1;
478 /* Buffer device address */
479 addr
&= ~DRXDAP_FASI_FLAGS
;
481 #if (((DRXDAPFASI_LONG_ADDR_ALLOWED) == 1) && \
482 ((DRXDAPFASI_SHORT_ADDR_ALLOWED) == 1))
483 /* short format address preferred but long format otherwise */
484 if (DRXDAP_FASI_LONG_FORMAT(addr
)) {
486 #if ((DRXDAPFASI_LONG_ADDR_ALLOWED) == 1)
487 buf
[bufx
++] = (u8
) (((addr
<< 1) & 0xFF) | 0x01);
488 buf
[bufx
++] = (u8
) ((addr
>> 16) & 0xFF);
489 buf
[bufx
++] = (u8
) ((addr
>> 24) & 0xFF);
490 buf
[bufx
++] = (u8
) ((addr
>> 7) & 0xFF);
492 #if (((DRXDAPFASI_LONG_ADDR_ALLOWED) == 1) && \
493 ((DRXDAPFASI_SHORT_ADDR_ALLOWED) == 1))
496 #if ((DRXDAPFASI_SHORT_ADDR_ALLOWED) == 1)
497 buf
[bufx
++] = (u8
) ((addr
<< 1) & 0xFF);
499 (u8
) (((addr
>> 16) & 0x0F) |
500 ((addr
>> 18) & 0xF0));
502 #if (((DRXDAPFASI_LONG_ADDR_ALLOWED) == 1) && \
503 ((DRXDAPFASI_SHORT_ADDR_ALLOWED) == 1))
508 In single master mode block_size can be 0. In such a case this I2C
509 sequense will be visible: (1) write address {i2c addr,
510 4 bytes chip address} (2) write data {i2c addr, 4 bytes data }
511 (3) write address (4) write data etc...
512 Addres must be rewriten because HI is reset after data transport and
515 todo
= (block_size
< datasize
? block_size
: datasize
);
517 u16 overhead_sizeI2cAddr
= 0;
518 u16 data_block_size
= 0;
520 overhead_sizeI2cAddr
=
521 (IS_I2C_10BIT(dev_addr
->i2c_addr
) ? 2 : 1);
523 (DRXDAP_MAX_WCHUNKSIZE
- overhead_sizeI2cAddr
) & ~1;
525 /* write device address */
526 st
= drxbsp_i2c_write_read(dev_addr
,
529 (struct i2c_device_addr
*)(NULL
),
532 if ((st
!= DRX_STS_OK
) && (first_err
== DRX_STS_OK
)) {
533 /* at the end, return the first error encountered */
539 datasize
? data_block_size
: datasize
);
541 drxbsp_hst_memcpy(&buf
[bufx
], data
, todo
);
542 /* write (address if can do and) data */
543 st
= drxbsp_i2c_write_read(dev_addr
,
546 (struct i2c_device_addr
*)(NULL
),
549 if ((st
!= DRX_STS_OK
) && (first_err
== DRX_STS_OK
)) {
550 /* at the end, return the first error encountered */
561 /******************************
563 * int drxdap_fasi_write_reg16 (
564 * struct i2c_device_addr *dev_addr, -- address of I2C device
565 * dr_xaddr_t addr, -- address of chip register/memory
566 * u16 data, -- data to send
567 * dr_xflags_t flags) -- special device flags
569 * Write one 16-bit register or memory location. The data being written is
570 * converted from the target platform's endianness to little endian.
573 * - DRX_STS_OK if writing was successful
574 * - DRX_STS_ERROR if anything went wrong
576 ******************************/
578 static int drxdap_fasi_write_reg16(struct i2c_device_addr
*dev_addr
,
580 u16 data
, dr_xflags_t flags
)
582 u8 buf
[sizeof(data
)];
584 buf
[0] = (u8
) ((data
>> 0) & 0xFF);
585 buf
[1] = (u8
) ((data
>> 8) & 0xFF);
587 return drxdap_fasi_write_block(dev_addr
, addr
, sizeof(data
), buf
, flags
);
590 /******************************
592 * int drxdap_fasi_write_reg32 (
593 * struct i2c_device_addr *dev_addr, -- address of I2C device
594 * dr_xaddr_t addr, -- address of chip register/memory
595 * u32 data, -- data to send
596 * dr_xflags_t flags) -- special device flags
598 * Write one 32-bit register or memory location. The data being written is
599 * converted from the target platform's endianness to little endian.
602 * - DRX_STS_OK if writing was successful
603 * - DRX_STS_ERROR if anything went wrong
605 ******************************/
607 static int drxdap_fasi_write_reg32(struct i2c_device_addr
*dev_addr
,
609 u32 data
, dr_xflags_t flags
)
611 u8 buf
[sizeof(data
)];
613 buf
[0] = (u8
) ((data
>> 0) & 0xFF);
614 buf
[1] = (u8
) ((data
>> 8) & 0xFF);
615 buf
[2] = (u8
) ((data
>> 16) & 0xFF);
616 buf
[3] = (u8
) ((data
>> 24) & 0xFF);
618 return drxdap_fasi_write_block(dev_addr
, addr
, sizeof(data
), buf
, flags
);