blob: 2b6219d86b0f31372ecceea15a6a8e29966f8eae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 Steven J. Hill
3 * Copyright (C) 2001,2002,2003 Broadcom Corporation
Jean Delvare51c37112006-08-13 23:33:16 +02004 * Copyright (C) 1995-2000 Simon G. Vogl
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Jean Delvare51c37112006-08-13 23:33:16 +020017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Jean Delvare51c37112006-08-13 23:33:16 +020019#include <linux/init.h>
20#include <linux/i2c.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020021#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/sibyte/sb1250_regs.h>
23#include <asm/sibyte/sb1250_smbus.h>
24
Jean Delvare51c37112006-08-13 23:33:16 +020025
26struct i2c_algo_sibyte_data {
27 void *data; /* private data */
28 int bus; /* which bus */
29 void *reg_base; /* CSR base */
30};
31
32/* ----- global defines ----------------------------------------------- */
33#define SMB_CSR(a,r) ((long)(a->reg_base + r))
34
Jean Delvare51c37112006-08-13 23:33:16 +020035
36static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr,
37 unsigned short flags, char read_write,
38 u8 command, int size, union i2c_smbus_data * data)
39{
40 struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
41 int data_bytes = 0;
42 int error;
43
44 while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
45 ;
46
47 switch (size) {
48 case I2C_SMBUS_QUICK:
49 csr_out32((V_SMB_ADDR(addr) |
50 (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) |
51 V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START));
52 break;
53 case I2C_SMBUS_BYTE:
54 if (read_write == I2C_SMBUS_READ) {
55 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE),
56 SMB_CSR(adap, R_SMB_START));
57 data_bytes = 1;
58 } else {
59 csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
60 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE),
61 SMB_CSR(adap, R_SMB_START));
62 }
63 break;
64 case I2C_SMBUS_BYTE_DATA:
65 csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
66 if (read_write == I2C_SMBUS_READ) {
67 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE),
68 SMB_CSR(adap, R_SMB_START));
69 data_bytes = 1;
70 } else {
71 csr_out32(V_SMB_LB(data->byte),
72 SMB_CSR(adap, R_SMB_DATA));
73 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
74 SMB_CSR(adap, R_SMB_START));
75 }
76 break;
77 case I2C_SMBUS_WORD_DATA:
78 csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
79 if (read_write == I2C_SMBUS_READ) {
80 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE),
81 SMB_CSR(adap, R_SMB_START));
82 data_bytes = 2;
83 } else {
84 csr_out32(V_SMB_LB(data->word & 0xff),
85 SMB_CSR(adap, R_SMB_DATA));
86 csr_out32(V_SMB_MB(data->word >> 8),
87 SMB_CSR(adap, R_SMB_DATA));
88 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
89 SMB_CSR(adap, R_SMB_START));
90 }
91 break;
92 default:
Guenter Roeck102b59c2010-07-10 09:42:47 +020093 return -EOPNOTSUPP;
Jean Delvare51c37112006-08-13 23:33:16 +020094 }
95
96 while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
97 ;
98
99 error = csr_in32(SMB_CSR(adap, R_SMB_STATUS));
100 if (error & M_SMB_ERROR) {
101 /* Clear error bit by writing a 1 */
102 csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS));
Guenter Roeck102b59c2010-07-10 09:42:47 +0200103 return (error & M_SMB_ERROR_TYPE) ? -EIO : -ENXIO;
Jean Delvare51c37112006-08-13 23:33:16 +0200104 }
105
106 if (data_bytes == 1)
107 data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff;
108 if (data_bytes == 2)
109 data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff;
110
111 return 0;
112}
113
114static u32 bit_func(struct i2c_adapter *adap)
115{
116 return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
117 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA);
118}
119
120
121/* -----exported algorithm data: ------------------------------------- */
122
Jean Delvare8f9082c2006-09-03 22:39:46 +0200123static const struct i2c_algorithm i2c_sibyte_algo = {
Jean Delvare51c37112006-08-13 23:33:16 +0200124 .smbus_xfer = smbus_xfer,
125 .functionality = bit_func,
126};
127
128/*
129 * registering functions to load algorithms at runtime
130 */
Maciej W. Rozyckib11a9d82008-05-11 20:37:05 +0200131static int __init i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
Jean Delvare51c37112006-08-13 23:33:16 +0200132{
Jean Delvare51c37112006-08-13 23:33:16 +0200133 struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
134
Maciej W. Rozyckib3eb5a0bc2008-05-11 20:37:05 +0200135 /* Register new adapter to i2c module... */
Jean Delvare51c37112006-08-13 23:33:16 +0200136 i2c_adap->algo = &i2c_sibyte_algo;
137
Maciej W. Rozyckib3eb5a0bc2008-05-11 20:37:05 +0200138 /* Set the requested frequency. */
Jean Delvare51c37112006-08-13 23:33:16 +0200139 csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
140 csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
141
Maciej W. Rozycki392a0402008-07-14 22:38:33 +0200142 return i2c_add_numbered_adapter(i2c_adap);
Jean Delvare51c37112006-08-13 23:33:16 +0200143}
144
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static struct i2c_algo_sibyte_data sibyte_board_data[2] = {
Ralf Baechlea242b442005-08-09 10:07:57 -0700147 { NULL, 0, (void *) (CKSEG1+A_SMB_BASE(0)) },
148 { NULL, 1, (void *) (CKSEG1+A_SMB_BASE(1)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151static struct i2c_adapter sibyte_board_adapter[2] = {
152 {
153 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200154 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .algo = NULL,
156 .algo_data = &sibyte_board_data[0],
Maciej W. Rozycki392a0402008-07-14 22:38:33 +0200157 .nr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .name = "SiByte SMBus 0",
159 },
160 {
161 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200162 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .algo = NULL,
164 .algo_data = &sibyte_board_data[1],
Maciej W. Rozycki392a0402008-07-14 22:38:33 +0200165 .nr = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .name = "SiByte SMBus 1",
167 },
168};
169
170static int __init i2c_sibyte_init(void)
171{
Jean Delvare5cd6e672008-01-14 21:53:31 +0100172 pr_info("i2c-sibyte: i2c SMBus adapter module for SiByte board\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (i2c_sibyte_add_bus(&sibyte_board_adapter[0], K_SMB_FREQ_100KHZ) < 0)
174 return -ENODEV;
Jean Delvare5cd6e672008-01-14 21:53:31 +0100175 if (i2c_sibyte_add_bus(&sibyte_board_adapter[1],
176 K_SMB_FREQ_400KHZ) < 0) {
177 i2c_del_adapter(&sibyte_board_adapter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return -ENODEV;
Jean Delvare5cd6e672008-01-14 21:53:31 +0100179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181}
182
183static void __exit i2c_sibyte_exit(void)
184{
Yoichi Yuasaae1390d2006-09-29 08:42:38 +0200185 i2c_del_adapter(&sibyte_board_adapter[0]);
186 i2c_del_adapter(&sibyte_board_adapter[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189module_init(i2c_sibyte_init);
190module_exit(i2c_sibyte_exit);
191
Jean Delvare643bd3f2006-08-13 23:34:05 +0200192MODULE_AUTHOR("Kip Walker (Broadcom Corp.), Steven J. Hill <sjhill@realitydiluted.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193MODULE_DESCRIPTION("SMBus adapter routines for SiByte boards");
194MODULE_LICENSE("GPL");