Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2004 Steven J. Hill |
| 4 | * Copyright (C) 2001,2002,2003 Broadcom Corporation |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 5 | * Copyright (C) 1995-2000 Simon G. Vogl |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/i2c.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 12 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/sibyte/sb1250_regs.h> |
| 14 | #include <asm/sibyte/sb1250_smbus.h> |
| 15 | |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 16 | |
| 17 | struct i2c_algo_sibyte_data { |
| 18 | void *data; /* private data */ |
| 19 | int bus; /* which bus */ |
| 20 | void *reg_base; /* CSR base */ |
| 21 | }; |
| 22 | |
| 23 | /* ----- global defines ----------------------------------------------- */ |
| 24 | #define SMB_CSR(a,r) ((long)(a->reg_base + r)) |
| 25 | |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 26 | |
| 27 | static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr, |
| 28 | unsigned short flags, char read_write, |
| 29 | u8 command, int size, union i2c_smbus_data * data) |
| 30 | { |
| 31 | struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data; |
| 32 | int data_bytes = 0; |
| 33 | int error; |
| 34 | |
| 35 | while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY) |
| 36 | ; |
| 37 | |
| 38 | switch (size) { |
| 39 | case I2C_SMBUS_QUICK: |
| 40 | csr_out32((V_SMB_ADDR(addr) | |
| 41 | (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) | |
| 42 | V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START)); |
| 43 | break; |
| 44 | case I2C_SMBUS_BYTE: |
| 45 | if (read_write == I2C_SMBUS_READ) { |
| 46 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE), |
| 47 | SMB_CSR(adap, R_SMB_START)); |
| 48 | data_bytes = 1; |
| 49 | } else { |
| 50 | csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD)); |
| 51 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE), |
| 52 | SMB_CSR(adap, R_SMB_START)); |
| 53 | } |
| 54 | break; |
| 55 | case I2C_SMBUS_BYTE_DATA: |
| 56 | csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD)); |
| 57 | if (read_write == I2C_SMBUS_READ) { |
| 58 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE), |
| 59 | SMB_CSR(adap, R_SMB_START)); |
| 60 | data_bytes = 1; |
| 61 | } else { |
| 62 | csr_out32(V_SMB_LB(data->byte), |
| 63 | SMB_CSR(adap, R_SMB_DATA)); |
| 64 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE), |
| 65 | SMB_CSR(adap, R_SMB_START)); |
| 66 | } |
| 67 | break; |
| 68 | case I2C_SMBUS_WORD_DATA: |
| 69 | csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD)); |
| 70 | if (read_write == I2C_SMBUS_READ) { |
| 71 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE), |
| 72 | SMB_CSR(adap, R_SMB_START)); |
| 73 | data_bytes = 2; |
| 74 | } else { |
| 75 | csr_out32(V_SMB_LB(data->word & 0xff), |
| 76 | SMB_CSR(adap, R_SMB_DATA)); |
| 77 | csr_out32(V_SMB_MB(data->word >> 8), |
| 78 | SMB_CSR(adap, R_SMB_DATA)); |
| 79 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE), |
| 80 | SMB_CSR(adap, R_SMB_START)); |
| 81 | } |
| 82 | break; |
| 83 | default: |
Guenter Roeck | 102b59c | 2010-07-10 09:42:47 +0200 | [diff] [blame] | 84 | return -EOPNOTSUPP; |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY) |
| 88 | ; |
| 89 | |
| 90 | error = csr_in32(SMB_CSR(adap, R_SMB_STATUS)); |
| 91 | if (error & M_SMB_ERROR) { |
| 92 | /* Clear error bit by writing a 1 */ |
| 93 | csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS)); |
Guenter Roeck | 102b59c | 2010-07-10 09:42:47 +0200 | [diff] [blame] | 94 | return (error & M_SMB_ERROR_TYPE) ? -EIO : -ENXIO; |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (data_bytes == 1) |
| 98 | data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff; |
| 99 | if (data_bytes == 2) |
| 100 | data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static u32 bit_func(struct i2c_adapter *adap) |
| 106 | { |
| 107 | return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 108 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* -----exported algorithm data: ------------------------------------- */ |
| 113 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 114 | static const struct i2c_algorithm i2c_sibyte_algo = { |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 115 | .smbus_xfer = smbus_xfer, |
| 116 | .functionality = bit_func, |
| 117 | }; |
| 118 | |
| 119 | /* |
| 120 | * registering functions to load algorithms at runtime |
| 121 | */ |
Maciej W. Rozycki | b11a9d8 | 2008-05-11 20:37:05 +0200 | [diff] [blame] | 122 | static int __init i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed) |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 123 | { |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 124 | struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data; |
| 125 | |
Maciej W. Rozycki | b3eb5a0bc | 2008-05-11 20:37:05 +0200 | [diff] [blame] | 126 | /* Register new adapter to i2c module... */ |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 127 | i2c_adap->algo = &i2c_sibyte_algo; |
| 128 | |
Maciej W. Rozycki | b3eb5a0bc | 2008-05-11 20:37:05 +0200 | [diff] [blame] | 129 | /* Set the requested frequency. */ |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 130 | csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ)); |
| 131 | csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL)); |
| 132 | |
Maciej W. Rozycki | 392a040 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 133 | return i2c_add_numbered_adapter(i2c_adap); |
Jean Delvare | 51c3711 | 2006-08-13 23:33:16 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static struct i2c_algo_sibyte_data sibyte_board_data[2] = { |
Ralf Baechle | a242b44 | 2005-08-09 10:07:57 -0700 | [diff] [blame] | 138 | { NULL, 0, (void *) (CKSEG1+A_SMB_BASE(0)) }, |
| 139 | { NULL, 1, (void *) (CKSEG1+A_SMB_BASE(1)) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static struct i2c_adapter sibyte_board_adapter[2] = { |
| 143 | { |
| 144 | .owner = THIS_MODULE, |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 145 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | .algo = NULL, |
| 147 | .algo_data = &sibyte_board_data[0], |
Maciej W. Rozycki | 392a040 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 148 | .nr = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .name = "SiByte SMBus 0", |
| 150 | }, |
| 151 | { |
| 152 | .owner = THIS_MODULE, |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 153 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | .algo = NULL, |
| 155 | .algo_data = &sibyte_board_data[1], |
Maciej W. Rozycki | 392a040 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 156 | .nr = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | .name = "SiByte SMBus 1", |
| 158 | }, |
| 159 | }; |
| 160 | |
| 161 | static int __init i2c_sibyte_init(void) |
| 162 | { |
Jean Delvare | 5cd6e67 | 2008-01-14 21:53:31 +0100 | [diff] [blame] | 163 | pr_info("i2c-sibyte: i2c SMBus adapter module for SiByte board\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (i2c_sibyte_add_bus(&sibyte_board_adapter[0], K_SMB_FREQ_100KHZ) < 0) |
| 165 | return -ENODEV; |
Jean Delvare | 5cd6e67 | 2008-01-14 21:53:31 +0100 | [diff] [blame] | 166 | if (i2c_sibyte_add_bus(&sibyte_board_adapter[1], |
| 167 | K_SMB_FREQ_400KHZ) < 0) { |
| 168 | i2c_del_adapter(&sibyte_board_adapter[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return -ENODEV; |
Jean Delvare | 5cd6e67 | 2008-01-14 21:53:31 +0100 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void __exit i2c_sibyte_exit(void) |
| 175 | { |
Yoichi Yuasa | ae1390d | 2006-09-29 08:42:38 +0200 | [diff] [blame] | 176 | i2c_del_adapter(&sibyte_board_adapter[0]); |
| 177 | i2c_del_adapter(&sibyte_board_adapter[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | module_init(i2c_sibyte_init); |
| 181 | module_exit(i2c_sibyte_exit); |
| 182 | |
Jean Delvare | 643bd3f | 2006-08-13 23:34:05 +0200 | [diff] [blame] | 183 | MODULE_AUTHOR("Kip Walker (Broadcom Corp.), Steven J. Hill <sjhill@realitydiluted.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | MODULE_DESCRIPTION("SMBus adapter routines for SiByte boards"); |
| 185 | MODULE_LICENSE("GPL"); |