1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
49 * Interface to the TWSI / I2C bus
51 * <hr>$Revision: 70030 $<hr>
54 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
55 #include <linux/i2c.h>
57 #include <asm/octeon/cvmx.h>
58 #include <asm/octeon/cvmx-twsi.h>
61 #include "cvmx-twsi.h"
62 #if !defined(CVMX_BUILD_FOR_FREEBSD_KERNEL)
63 #include "cvmx-csr-db.h"
67 //#define PRINT_TWSI_CONFIG
68 #ifdef PRINT_TWSI_CONFIG
69 #define twsi_printf printf
71 #define twsi_printf(...)
72 #define cvmx_csr_db_decode(...)
75 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
76 # if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
77 static struct i2c_adapter *__cvmx_twsix_get_adapter(int twsi_id)
80 wait_queue_head_t queue;
81 struct i2c_adapter adap;
85 resource_size_t twsi_phys;
86 void __iomem *twsi_base;
87 resource_size_t regsize;
91 struct i2c_adapter *adapter;
92 struct octeon_i2c *i2c;
94 adapter = i2c_get_adapter(0);
97 i2c = container_of(adapter, struct octeon_i2c, adap);
98 return &i2c[twsi_id].adap;
105 * Do a twsi read from a 7 bit device address using an (optional) internal address.
106 * Up to 8 bytes can be read at a time.
108 * @param twsi_id which Octeon TWSI bus to use
109 * @param dev_addr Device address (7 bit)
110 * @param internal_addr
111 * Internal address. Can be 0, 1 or 2 bytes in width
112 * @param num_bytes Number of data bytes to read
113 * @param ia_width_bytes
114 * Internal address size in bytes (0, 1, or 2)
115 * @param data Pointer argument where the read data is returned.
117 * @return read data returned in 'data' argument
118 * Number of bytes read on success
121 int cvmx_twsix_read_ia(int twsi_id, uint8_t dev_addr, uint16_t internal_addr, int num_bytes, int ia_width_bytes, uint64_t *data)
123 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
124 # if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
125 struct i2c_adapter *adapter;
128 struct i2c_msg msg[2];
132 if (ia_width_bytes == 0)
133 return cvmx_twsix_read(twsi_id, dev_addr, num_bytes, data);
135 BUG_ON(ia_width_bytes > 2);
136 BUG_ON(num_bytes > 8 || num_bytes < 1);
138 adapter = __cvmx_twsix_get_adapter(twsi_id);
142 for (j = 0, i = ia_width_bytes - 1; i >= 0; i--, j++)
143 addr_buf[j] = (u8)(internal_addr >> (i * 8));
145 msg[0].addr = dev_addr;
147 msg[0].len = ia_width_bytes;
148 msg[0].buf = addr_buf;
150 msg[1].addr = dev_addr;
151 msg[1].flags = I2C_M_RD;
152 msg[1].len = num_bytes;
153 msg[1].buf = data_buf;
155 i = i2c_transfer(adapter, msg, 2);
157 i2c_put_adapter(adapter);
161 for (i = 0; i < num_bytes; i++)
162 r = (r << 8) | data_buf[i];
169 BUG(); /* The I2C driver is not compiled in */
172 cvmx_mio_twsx_sw_twsi_t sw_twsi_val;
173 cvmx_mio_twsx_sw_twsi_ext_t twsi_ext;
176 if (num_bytes < 1 || num_bytes > 8 || !data || ia_width_bytes < 0 || ia_width_bytes > 2)
183 sw_twsi_val.s.sovr = 1;
184 sw_twsi_val.s.size = num_bytes - 1;
185 sw_twsi_val.s.a = dev_addr;
187 if (ia_width_bytes > 0) {
188 sw_twsi_val.s.op = 1;
189 sw_twsi_val.s.ia = (internal_addr >> 3) & 0x1f;
190 sw_twsi_val.s.eop_ia = internal_addr & 0x7;
192 if (ia_width_bytes == 2) {
193 sw_twsi_val.s.eia = 1;
194 twsi_ext.s.ia = internal_addr >> 8;
195 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI_EXT(twsi_id), twsi_ext.u64);
198 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
199 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
200 while (((cvmx_mio_twsx_sw_twsi_t)(sw_twsi_val.u64 = cvmx_read_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id)))).s.v)
202 twsi_printf("Results:\n");
203 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
204 if (!sw_twsi_val.s.r)
206 /* Check the reason for the failure. We may need to retry to handle multi-master
208 ** Lost arbitration : 0x38, 0x68, 0xB0, 0x78
209 ** Core busy as slave: 0x80, 0x88, 0xA0, 0xA8, 0xB8, 0xC0, 0xC8
211 if (sw_twsi_val.s.d == 0x38
212 || sw_twsi_val.s.d == 0x68
213 || sw_twsi_val.s.d == 0xB0
214 || sw_twsi_val.s.d == 0x78
215 || sw_twsi_val.s.d == 0x80
216 || sw_twsi_val.s.d == 0x88
217 || sw_twsi_val.s.d == 0xA0
218 || sw_twsi_val.s.d == 0xA8
219 || sw_twsi_val.s.d == 0xB8
220 || sw_twsi_val.s.d == 0xC8)
222 if (retry_limit-- > 0)
228 /* For all other errors, return an error code */
232 *data = (sw_twsi_val.s.d & (0xFFFFFFFF >> (32 - num_bytes*8)));
234 twsi_ext.u64 = cvmx_read_csr(CVMX_MIO_TWSX_SW_TWSI_EXT(twsi_id));
235 *data |= ((unsigned long long)(twsi_ext.s.d & (0xFFFFFFFF >> (32 - num_bytes*8))) << 32);
242 * Read from a TWSI device (7 bit device address only) without generating any
243 * internal addresses.
244 * Read from 1-8 bytes and returns them in the data pointer.
246 * @param twsi_id TWSI interface on Octeon to use
247 * @param dev_addr TWSI device address (7 bit only)
248 * @param num_bytes number of bytes to read
249 * @param data Pointer to data read from TWSI device
251 * @return Number of bytes read on success
254 int cvmx_twsix_read(int twsi_id, uint8_t dev_addr, int num_bytes, uint64_t *data)
256 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
257 # if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
258 struct i2c_adapter *adapter;
260 struct i2c_msg msg[1];
264 BUG_ON(num_bytes > 8 || num_bytes < 1);
266 adapter = __cvmx_twsix_get_adapter(twsi_id);
270 msg[0].addr = dev_addr;
271 msg[0].flags = I2C_M_RD;
272 msg[0].len = num_bytes;
273 msg[0].buf = data_buf;
275 i = i2c_transfer(adapter, msg, 1);
277 i2c_put_adapter(adapter);
281 for (i = 0; i < num_bytes; i++)
282 r = (r << 8) | data_buf[i];
289 BUG(); /* The I2C driver is not compiled in */
292 cvmx_mio_twsx_sw_twsi_t sw_twsi_val;
293 cvmx_mio_twsx_sw_twsi_ext_t twsi_ext;
296 if (num_bytes > 8 || num_bytes < 1)
302 sw_twsi_val.s.a = dev_addr;
303 sw_twsi_val.s.sovr = 1;
304 sw_twsi_val.s.size = num_bytes - 1;
306 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
307 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
308 while (((cvmx_mio_twsx_sw_twsi_t)(sw_twsi_val.u64 = cvmx_read_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id)))).s.v)
310 twsi_printf("Results:\n");
311 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
312 if (!sw_twsi_val.s.r)
313 if (!sw_twsi_val.s.r)
315 /* Check the reason for the failure. We may need to retry to handle multi-master
317 ** Lost arbitration : 0x38, 0x68, 0xB0, 0x78
318 ** Core busy as slave: 0x80, 0x88, 0xA0, 0xA8, 0xB8, 0xC0, 0xC8
320 if (sw_twsi_val.s.d == 0x38
321 || sw_twsi_val.s.d == 0x68
322 || sw_twsi_val.s.d == 0xB0
323 || sw_twsi_val.s.d == 0x78
324 || sw_twsi_val.s.d == 0x80
325 || sw_twsi_val.s.d == 0x88
326 || sw_twsi_val.s.d == 0xA0
327 || sw_twsi_val.s.d == 0xA8
328 || sw_twsi_val.s.d == 0xB8
329 || sw_twsi_val.s.d == 0xC8)
331 if (retry_limit-- > 0)
337 /* For all other errors, return an error code */
341 *data = (sw_twsi_val.s.d & (0xFFFFFFFF >> (32 - num_bytes*8)));
343 twsi_ext.u64 = cvmx_read_csr(CVMX_MIO_TWSX_SW_TWSI_EXT(twsi_id));
344 *data |= ((unsigned long long)(twsi_ext.s.d & (0xFFFFFFFF >> (32 - num_bytes*8))) << 32);
351 * Perform a twsi write operation to a 7 bit device address.
353 * Note that many eeprom devices have page restrictions regarding address boundaries
354 * that can be crossed in one write operation. This is device dependent, and this routine
355 * does nothing in this regard.
356 * This command does not generate any internal addressess.
358 * @param twsi_id Octeon TWSI interface to use
359 * @param dev_addr TWSI device address
360 * @param num_bytes Number of bytes to write (between 1 and 8 inclusive)
361 * @param data Data to write
363 * @return 0 on success
366 int cvmx_twsix_write(int twsi_id, uint8_t dev_addr, int num_bytes, uint64_t data)
368 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
369 # if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
370 struct i2c_adapter *adapter;
372 struct i2c_msg msg[1];
375 BUG_ON(num_bytes > 8 || num_bytes < 1);
377 adapter = __cvmx_twsix_get_adapter(twsi_id);
381 for (j = 0, i = num_bytes - 1; i >= 0; i--, j++)
382 data_buf[j] = (u8)(data >> (i * 8));
384 msg[0].addr = dev_addr;
386 msg[0].len = num_bytes;
387 msg[0].buf = data_buf;
389 i = i2c_transfer(adapter, msg, 1);
391 i2c_put_adapter(adapter);
398 BUG(); /* The I2C driver is not compiled in */
401 cvmx_mio_twsx_sw_twsi_t sw_twsi_val;
403 if (num_bytes > 8 || num_bytes < 1)
408 sw_twsi_val.s.a = dev_addr;
409 sw_twsi_val.s.d = data & 0xffffffff;
410 sw_twsi_val.s.sovr = 1;
411 sw_twsi_val.s.size = num_bytes - 1;
413 /* Upper four bytes go into a separate register */
414 cvmx_mio_twsx_sw_twsi_ext_t twsi_ext;
416 twsi_ext.s.d = data >> 32;
417 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI_EXT(twsi_id), twsi_ext.u64);
419 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
420 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
421 while (((cvmx_mio_twsx_sw_twsi_t)(sw_twsi_val.u64 = cvmx_read_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id)))).s.v)
423 twsi_printf("Results:\n");
424 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
425 if (!sw_twsi_val.s.r)
433 * Write 1-8 bytes to a TWSI device using an internal address.
435 * @param twsi_id which TWSI interface on Octeon to use
436 * @param dev_addr TWSI device address (7 bit only)
437 * @param internal_addr
438 * TWSI internal address (0, 8, or 16 bits)
439 * @param num_bytes Number of bytes to write (1-8)
440 * @param ia_width_bytes
441 * internal address width, in bytes (0, 1, 2)
442 * @param data Data to write. Data is written MSB first on the twsi bus, and only the lower
443 * num_bytes bytes of the argument are valid. (If a 2 byte write is done, only
444 * the low 2 bytes of the argument is used.
446 * @return Number of bytes read on success,
449 int cvmx_twsix_write_ia(int twsi_id, uint8_t dev_addr, uint16_t internal_addr, int num_bytes, int ia_width_bytes, uint64_t data)
451 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
452 # if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
453 struct i2c_adapter *adapter;
456 struct i2c_msg msg[2];
459 if (ia_width_bytes == 0)
460 return cvmx_twsix_write(twsi_id, dev_addr, num_bytes, data);
462 BUG_ON(ia_width_bytes > 2);
463 BUG_ON(num_bytes > 8 || num_bytes < 1);
465 adapter = __cvmx_twsix_get_adapter(twsi_id);
470 for (j = 0, i = ia_width_bytes - 1; i >= 0; i--, j++)
471 addr_buf[j] = (u8)(internal_addr >> (i * 8));
473 for (j = 0, i = num_bytes - 1; i >= 0; i--, j++)
474 data_buf[j] = (u8)(data >> (i * 8));
476 msg[0].addr = dev_addr;
478 msg[0].len = ia_width_bytes;
479 msg[0].buf = addr_buf;
481 msg[1].addr = dev_addr;
483 msg[1].len = num_bytes;
484 msg[1].buf = data_buf;
486 i = i2c_transfer(adapter, msg, 2);
488 i2c_put_adapter(adapter);
491 /* Poll until reads succeed, or polling times out */
495 if (cvmx_twsix_read(twsi_id, dev_addr, 1, &data) >= 0)
505 BUG(); /* The I2C driver is not compiled in */
508 cvmx_mio_twsx_sw_twsi_t sw_twsi_val;
509 cvmx_mio_twsx_sw_twsi_ext_t twsi_ext;
512 if (num_bytes < 1 || num_bytes > 8 || ia_width_bytes < 0 || ia_width_bytes > 2)
519 sw_twsi_val.s.sovr = 1;
520 sw_twsi_val.s.size = num_bytes - 1;
521 sw_twsi_val.s.a = dev_addr;
522 sw_twsi_val.s.d = 0xFFFFFFFF & data;
524 if (ia_width_bytes > 0) {
525 sw_twsi_val.s.op = 1;
526 sw_twsi_val.s.ia = (internal_addr >> 3) & 0x1f;
527 sw_twsi_val.s.eop_ia = internal_addr & 0x7;
529 if (ia_width_bytes == 2) {
530 sw_twsi_val.s.eia = 1;
531 twsi_ext.s.ia = internal_addr >> 8;
534 twsi_ext.s.d = data >> 32;
536 twsi_printf("%s: twsi_id=%x, dev_addr=%x, internal_addr=%x\n\tnum_bytes=%d, ia_width_bytes=%d, data=%lx\n",
537 __FUNCTION__, twsi_id, dev_addr, internal_addr, num_bytes, ia_width_bytes, data);
538 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI_EXT(twsi_id), twsi_ext.u64);
539 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI_EXT(twsi_id), twsi_ext.u64);
540 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
541 cvmx_write_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
542 while (((cvmx_mio_twsx_sw_twsi_t)(sw_twsi_val.u64 = cvmx_read_csr(CVMX_MIO_TWSX_SW_TWSI(twsi_id)))).s.v)
544 twsi_printf("Results:\n");
545 cvmx_csr_db_decode(cvmx_get_proc_id(), CVMX_MIO_TWSX_SW_TWSI(twsi_id), sw_twsi_val.u64);
547 /* Poll until reads succeed, or polling times out */
551 if (cvmx_twsix_read(twsi_id, dev_addr, 1, &data) >= 0)