blob: 543d39c73c3763127f3aa87b53e1bc2257b27ced [file] [log] [blame]
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +09001/*
2 * Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/i2c.h>
24#include <linux/fs.h>
25#include <linux/io.h>
26#include <linux/types.h>
27#include <linux/interrupt.h>
28#include <linux/jiffies.h>
29#include <linux/pci.h>
30#include <linux/mutex.h>
31#include <linux/ktime.h>
Wolfram Sang6dbc2f32011-02-23 11:11:35 +010032#include <linux/slab.h>
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +090033
34#define PCH_EVENT_SET 0 /* I2C Interrupt Event Set Status */
35#define PCH_EVENT_NONE 1 /* I2C Interrupt Event Clear Status */
36#define PCH_MAX_CLK 100000 /* Maximum Clock speed in MHz */
37#define PCH_BUFFER_MODE_ENABLE 0x0002 /* flag for Buffer mode enable */
38#define PCH_EEPROM_SW_RST_MODE_ENABLE 0x0008 /* EEPROM SW RST enable flag */
39
40#define PCH_I2CSADR 0x00 /* I2C slave address register */
41#define PCH_I2CCTL 0x04 /* I2C control register */
42#define PCH_I2CSR 0x08 /* I2C status register */
43#define PCH_I2CDR 0x0C /* I2C data register */
44#define PCH_I2CMON 0x10 /* I2C bus monitor register */
45#define PCH_I2CBC 0x14 /* I2C bus transfer rate setup counter */
46#define PCH_I2CMOD 0x18 /* I2C mode register */
47#define PCH_I2CBUFSLV 0x1C /* I2C buffer mode slave address register */
48#define PCH_I2CBUFSUB 0x20 /* I2C buffer mode subaddress register */
49#define PCH_I2CBUFFOR 0x24 /* I2C buffer mode format register */
50#define PCH_I2CBUFCTL 0x28 /* I2C buffer mode control register */
51#define PCH_I2CBUFMSK 0x2C /* I2C buffer mode interrupt mask register */
52#define PCH_I2CBUFSTA 0x30 /* I2C buffer mode status register */
53#define PCH_I2CBUFLEV 0x34 /* I2C buffer mode level register */
54#define PCH_I2CESRFOR 0x38 /* EEPROM software reset mode format register */
55#define PCH_I2CESRCTL 0x3C /* EEPROM software reset mode ctrl register */
56#define PCH_I2CESRMSK 0x40 /* EEPROM software reset mode */
57#define PCH_I2CESRSTA 0x44 /* EEPROM software reset mode status register */
58#define PCH_I2CTMR 0x48 /* I2C timer register */
59#define PCH_I2CSRST 0xFC /* I2C reset register */
60#define PCH_I2CNF 0xF8 /* I2C noise filter register */
61
62#define BUS_IDLE_TIMEOUT 20
63#define PCH_I2CCTL_I2CMEN 0x0080
64#define TEN_BIT_ADDR_DEFAULT 0xF000
65#define TEN_BIT_ADDR_MASK 0xF0
66#define PCH_START 0x0020
67#define PCH_ESR_START 0x0001
68#define PCH_BUFF_START 0x1
69#define PCH_REPSTART 0x0004
70#define PCH_ACK 0x0008
71#define PCH_GETACK 0x0001
72#define CLR_REG 0x0
73#define I2C_RD 0x1
74#define I2CMCF_BIT 0x0080
75#define I2CMIF_BIT 0x0002
76#define I2CMAL_BIT 0x0010
77#define I2CBMFI_BIT 0x0001
78#define I2CBMAL_BIT 0x0002
79#define I2CBMNA_BIT 0x0004
80#define I2CBMTO_BIT 0x0008
81#define I2CBMIS_BIT 0x0010
82#define I2CESRFI_BIT 0X0001
83#define I2CESRTO_BIT 0x0002
84#define I2CESRFIIE_BIT 0x1
85#define I2CESRTOIE_BIT 0x2
86#define I2CBMDZ_BIT 0x0040
87#define I2CBMAG_BIT 0x0020
88#define I2CMBB_BIT 0x0020
89#define BUFFER_MODE_MASK (I2CBMFI_BIT | I2CBMAL_BIT | I2CBMNA_BIT | \
90 I2CBMTO_BIT | I2CBMIS_BIT)
91#define I2C_ADDR_MSK 0xFF
92#define I2C_MSB_2B_MSK 0x300
93#define FAST_MODE_CLK 400
94#define FAST_MODE_EN 0x0001
95#define SUB_ADDR_LEN_MAX 4
96#define BUF_LEN_MAX 32
97#define PCH_BUFFER_MODE 0x1
98#define EEPROM_SW_RST_MODE 0x0002
99#define NORMAL_INTR_ENBL 0x0300
100#define EEPROM_RST_INTR_ENBL (I2CESRFIIE_BIT | I2CESRTOIE_BIT)
101#define EEPROM_RST_INTR_DISBL 0x0
102#define BUFFER_MODE_INTR_ENBL 0x001F
103#define BUFFER_MODE_INTR_DISBL 0x0
104#define NORMAL_MODE 0x0
105#define BUFFER_MODE 0x1
106#define EEPROM_SR_MODE 0x2
107#define I2C_TX_MODE 0x0010
108#define PCH_BUF_TX 0xFFF7
109#define PCH_BUF_RD 0x0008
110#define I2C_ERROR_MASK (I2CESRTO_EVENT | I2CBMIS_EVENT | I2CBMTO_EVENT | \
111 I2CBMNA_EVENT | I2CBMAL_EVENT | I2CMAL_EVENT)
112#define I2CMAL_EVENT 0x0001
113#define I2CMCF_EVENT 0x0002
114#define I2CBMFI_EVENT 0x0004
115#define I2CBMAL_EVENT 0x0008
116#define I2CBMNA_EVENT 0x0010
117#define I2CBMTO_EVENT 0x0020
118#define I2CBMIS_EVENT 0x0040
119#define I2CESRFI_EVENT 0x0080
120#define I2CESRTO_EVENT 0x0100
121#define PCI_DEVICE_ID_PCH_I2C 0x8817
122
123#define pch_dbg(adap, fmt, arg...) \
124 dev_dbg(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
125
126#define pch_err(adap, fmt, arg...) \
127 dev_err(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
128
129#define pch_pci_err(pdev, fmt, arg...) \
130 dev_err(&pdev->dev, "%s :" fmt, __func__, ##arg)
131
132#define pch_pci_dbg(pdev, fmt, arg...) \
133 dev_dbg(&pdev->dev, "%s :" fmt, __func__, ##arg)
134
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900135/*
136Set the number of I2C instance max
137Intel EG20T PCH : 1ch
138OKI SEMICONDUCTOR ML7213 IOH : 2ch
139*/
140#define PCH_I2C_MAX_DEV 2
141
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900142/**
143 * struct i2c_algo_pch_data - for I2C driver functionalities
144 * @pch_adapter: stores the reference to i2c_adapter structure
145 * @p_adapter_info: stores the reference to adapter_info structure
146 * @pch_base_address: specifies the remapped base address
147 * @pch_buff_mode_en: specifies if buffer mode is enabled
148 * @pch_event_flag: specifies occurrence of interrupt events
149 * @pch_i2c_xfer_in_progress: specifies whether the transfer is completed
150 */
151struct i2c_algo_pch_data {
152 struct i2c_adapter pch_adapter;
153 struct adapter_info *p_adapter_info;
154 void __iomem *pch_base_address;
155 int pch_buff_mode_en;
156 u32 pch_event_flag;
157 bool pch_i2c_xfer_in_progress;
158};
159
160/**
161 * struct adapter_info - This structure holds the adapter information for the
162 PCH i2c controller
163 * @pch_data: stores a list of i2c_algo_pch_data
164 * @pch_i2c_suspended: specifies whether the system is suspended or not
165 * perhaps with more lines and words.
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900166 * @ch_num: specifies the number of i2c instance
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900167 *
168 * pch_data has as many elements as maximum I2C channels
169 */
170struct adapter_info {
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900171 struct i2c_algo_pch_data pch_data[PCH_I2C_MAX_DEV];
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900172 bool pch_i2c_suspended;
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900173 int ch_num;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900174};
175
176
177static int pch_i2c_speed = 100; /* I2C bus speed in Kbps */
178static int pch_clk = 50000; /* specifies I2C clock speed in KHz */
179static wait_queue_head_t pch_event;
180static DEFINE_MUTEX(pch_mutex);
181
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900182/* Definition for ML7213 by OKI SEMICONDUCTOR */
183#define PCI_VENDOR_ID_ROHM 0x10DB
184#define PCI_DEVICE_ID_ML7213_I2C 0x802D
Tomoya MORINAGAefbe0f22011-05-09 16:32:31 +0900185#define PCI_DEVICE_ID_ML7223_I2C 0x8010
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900186
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900187static struct pci_device_id __devinitdata pch_pcidev_id[] = {
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900188 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PCH_I2C), 1, },
189 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_I2C), 2, },
Tomoya MORINAGAefbe0f22011-05-09 16:32:31 +0900190 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_I2C), 1, },
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900191 {0,}
192};
193
194static irqreturn_t pch_i2c_handler(int irq, void *pData);
195
196static inline void pch_setbit(void __iomem *addr, u32 offset, u32 bitmask)
197{
198 u32 val;
199 val = ioread32(addr + offset);
200 val |= bitmask;
201 iowrite32(val, addr + offset);
202}
203
204static inline void pch_clrbit(void __iomem *addr, u32 offset, u32 bitmask)
205{
206 u32 val;
207 val = ioread32(addr + offset);
208 val &= (~bitmask);
209 iowrite32(val, addr + offset);
210}
211
212/**
213 * pch_i2c_init() - hardware initialization of I2C module
214 * @adap: Pointer to struct i2c_algo_pch_data.
215 */
216static void pch_i2c_init(struct i2c_algo_pch_data *adap)
217{
218 void __iomem *p = adap->pch_base_address;
219 u32 pch_i2cbc;
220 u32 pch_i2ctmr;
221 u32 reg_value;
222
223 /* reset I2C controller */
224 iowrite32(0x01, p + PCH_I2CSRST);
225 msleep(20);
226 iowrite32(0x0, p + PCH_I2CSRST);
227
228 /* Initialize I2C registers */
229 iowrite32(0x21, p + PCH_I2CNF);
230
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900231 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_I2CCTL_I2CMEN);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900232
233 if (pch_i2c_speed != 400)
234 pch_i2c_speed = 100;
235
236 reg_value = PCH_I2CCTL_I2CMEN;
237 if (pch_i2c_speed == FAST_MODE_CLK) {
238 reg_value |= FAST_MODE_EN;
239 pch_dbg(adap, "Fast mode enabled\n");
240 }
241
242 if (pch_clk > PCH_MAX_CLK)
243 pch_clk = 62500;
244
245 pch_i2cbc = (pch_clk + (pch_i2c_speed * 4)) / pch_i2c_speed * 8;
246 /* Set transfer speed in I2CBC */
247 iowrite32(pch_i2cbc, p + PCH_I2CBC);
248
249 pch_i2ctmr = (pch_clk) / 8;
250 iowrite32(pch_i2ctmr, p + PCH_I2CTMR);
251
252 reg_value |= NORMAL_INTR_ENBL; /* Enable interrupts in normal mode */
253 iowrite32(reg_value, p + PCH_I2CCTL);
254
255 pch_dbg(adap,
256 "I2CCTL=%x pch_i2cbc=%x pch_i2ctmr=%x Enable interrupts\n",
257 ioread32(p + PCH_I2CCTL), pch_i2cbc, pch_i2ctmr);
258
259 init_waitqueue_head(&pch_event);
260}
261
262static inline bool ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
263{
264 return cmp1.tv64 < cmp2.tv64;
265}
266
267/**
268 * pch_i2c_wait_for_bus_idle() - check the status of bus.
269 * @adap: Pointer to struct i2c_algo_pch_data.
270 * @timeout: waiting time counter (us).
271 */
272static s32 pch_i2c_wait_for_bus_idle(struct i2c_algo_pch_data *adap,
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900273 s32 timeout)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900274{
275 void __iomem *p = adap->pch_base_address;
Tomoya MORINAGA93e4ad72011-10-12 13:13:00 +0900276 ktime_t ns_val;
277
278 if ((ioread32(p + PCH_I2CSR) & I2CMBB_BIT) == 0)
279 return 0;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900280
281 /* MAX timeout value is timeout*1000*1000nsec */
Tomoya MORINAGA93e4ad72011-10-12 13:13:00 +0900282 ns_val = ktime_add_ns(ktime_get(), timeout*1000*1000);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900283 do {
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900284 msleep(20);
Tomoya MORINAGA93e4ad72011-10-12 13:13:00 +0900285 if ((ioread32(p + PCH_I2CSR) & I2CMBB_BIT) == 0)
286 return 0;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900287 } while (ktime_lt(ktime_get(), ns_val));
288
289 pch_dbg(adap, "I2CSR = %x\n", ioread32(p + PCH_I2CSR));
Tomoya MORINAGA93e4ad72011-10-12 13:13:00 +0900290 pch_err(adap, "%s: Timeout Error.return%d\n", __func__, -ETIME);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900291
Tomoya MORINAGA93e4ad72011-10-12 13:13:00 +0900292 return -ETIME;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900293}
294
295/**
296 * pch_i2c_start() - Generate I2C start condition in normal mode.
297 * @adap: Pointer to struct i2c_algo_pch_data.
298 *
299 * Generate I2C start condition in normal mode by setting I2CCTL.I2CMSTA to 1.
300 */
301static void pch_i2c_start(struct i2c_algo_pch_data *adap)
302{
303 void __iomem *p = adap->pch_base_address;
304 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
305 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
306}
307
308/**
309 * pch_i2c_wait_for_xfer_complete() - initiates a wait for the tx complete event
310 * @adap: Pointer to struct i2c_algo_pch_data.
311 */
312static s32 pch_i2c_wait_for_xfer_complete(struct i2c_algo_pch_data *adap)
313{
Tomoya MORINAGAc7b41f32011-10-12 13:13:01 +0900314 long ret;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900315 ret = wait_event_timeout(pch_event,
316 (adap->pch_event_flag != 0), msecs_to_jiffies(50));
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900317
318 if (ret == 0) {
319 pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
320 return -ETIMEDOUT;
321 }
322
323 if (adap->pch_event_flag & I2C_ERROR_MASK) {
324 pch_err(adap, "error bits set: %x\n", adap->pch_event_flag);
325 return -EIO;
326 }
327
328 adap->pch_event_flag = 0;
329
330 return 0;
331}
332
333/**
334 * pch_i2c_getack() - to confirm ACK/NACK
335 * @adap: Pointer to struct i2c_algo_pch_data.
336 */
337static s32 pch_i2c_getack(struct i2c_algo_pch_data *adap)
338{
339 u32 reg_val;
340 void __iomem *p = adap->pch_base_address;
341 reg_val = ioread32(p + PCH_I2CSR) & PCH_GETACK;
342
343 if (reg_val != 0) {
344 pch_err(adap, "return%d\n", -EPROTO);
345 return -EPROTO;
346 }
347
348 return 0;
349}
350
351/**
352 * pch_i2c_stop() - generate stop condition in normal mode.
353 * @adap: Pointer to struct i2c_algo_pch_data.
354 */
355static void pch_i2c_stop(struct i2c_algo_pch_data *adap)
356{
357 void __iomem *p = adap->pch_base_address;
358 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
359 /* clear the start bit */
360 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
361}
362
363/**
364 * pch_i2c_repstart() - generate repeated start condition in normal mode
365 * @adap: Pointer to struct i2c_algo_pch_data.
366 */
367static void pch_i2c_repstart(struct i2c_algo_pch_data *adap)
368{
369 void __iomem *p = adap->pch_base_address;
370 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
371 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_REPSTART);
372}
373
374/**
375 * pch_i2c_writebytes() - write data to I2C bus in normal mode
376 * @i2c_adap: Pointer to the struct i2c_adapter.
377 * @last: specifies whether last message or not.
378 * In the case of compound mode it will be 1 for last message,
379 * otherwise 0.
380 * @first: specifies whether first message or not.
381 * 1 for first message otherwise 0.
382 */
383static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,
384 struct i2c_msg *msgs, u32 last, u32 first)
385{
386 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
387 u8 *buf;
388 u32 length;
389 u32 addr;
390 u32 addr_2_msb;
391 u32 addr_8_lsb;
392 s32 wrcount;
393 void __iomem *p = adap->pch_base_address;
394
395 length = msgs->len;
396 buf = msgs->buf;
397 addr = msgs->addr;
398
399 /* enable master tx */
400 pch_setbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
401
402 pch_dbg(adap, "I2CCTL = %x msgs->len = %d\n", ioread32(p + PCH_I2CCTL),
403 length);
404
405 if (first) {
406 if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
407 return -ETIME;
408 }
409
410 if (msgs->flags & I2C_M_TEN) {
411 addr_2_msb = ((addr & I2C_MSB_2B_MSK) >> 7);
412 iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
413 if (first)
414 pch_i2c_start(adap);
415 if (pch_i2c_wait_for_xfer_complete(adap) == 0 &&
416 pch_i2c_getack(adap) == 0) {
417 addr_8_lsb = (addr & I2C_ADDR_MSK);
418 iowrite32(addr_8_lsb, p + PCH_I2CDR);
419 } else {
420 pch_i2c_stop(adap);
421 return -ETIME;
422 }
423 } else {
424 /* set 7 bit slave address and R/W bit as 0 */
425 iowrite32(addr << 1, p + PCH_I2CDR);
426 if (first)
427 pch_i2c_start(adap);
428 }
429
430 if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
431 (pch_i2c_getack(adap) == 0)) {
432 for (wrcount = 0; wrcount < length; ++wrcount) {
433 /* write buffer value to I2C data register */
434 iowrite32(buf[wrcount], p + PCH_I2CDR);
435 pch_dbg(adap, "writing %x to Data register\n",
436 buf[wrcount]);
437
438 if (pch_i2c_wait_for_xfer_complete(adap) != 0)
439 return -ETIME;
440
441 if (pch_i2c_getack(adap))
442 return -EIO;
443 }
444
445 /* check if this is the last message */
446 if (last)
447 pch_i2c_stop(adap);
448 else
449 pch_i2c_repstart(adap);
450 } else {
451 pch_i2c_stop(adap);
452 return -EIO;
453 }
454
455 pch_dbg(adap, "return=%d\n", wrcount);
456
457 return wrcount;
458}
459
460/**
461 * pch_i2c_sendack() - send ACK
462 * @adap: Pointer to struct i2c_algo_pch_data.
463 */
464static void pch_i2c_sendack(struct i2c_algo_pch_data *adap)
465{
466 void __iomem *p = adap->pch_base_address;
467 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
468 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
469}
470
471/**
472 * pch_i2c_sendnack() - send NACK
473 * @adap: Pointer to struct i2c_algo_pch_data.
474 */
475static void pch_i2c_sendnack(struct i2c_algo_pch_data *adap)
476{
477 void __iomem *p = adap->pch_base_address;
478 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
479 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
480}
481
482/**
483 * pch_i2c_readbytes() - read data from I2C bus in normal mode.
484 * @i2c_adap: Pointer to the struct i2c_adapter.
485 * @msgs: Pointer to i2c_msg structure.
486 * @last: specifies whether last message or not.
487 * @first: specifies whether first message or not.
488 */
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900489static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
490 u32 last, u32 first)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900491{
492 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
493
494 u8 *buf;
495 u32 count;
496 u32 length;
497 u32 addr;
498 u32 addr_2_msb;
499 void __iomem *p = adap->pch_base_address;
500
501 length = msgs->len;
502 buf = msgs->buf;
503 addr = msgs->addr;
504
505 /* enable master reception */
506 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
507
508 if (first) {
509 if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
510 return -ETIME;
511 }
512
513 if (msgs->flags & I2C_M_TEN) {
514 addr_2_msb = (((addr & I2C_MSB_2B_MSK) >> 7) | (I2C_RD));
515 iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
516
517 } else {
518 /* 7 address bits + R/W bit */
519 addr = (((addr) << 1) | (I2C_RD));
520 iowrite32(addr, p + PCH_I2CDR);
521 }
522
523 /* check if it is the first message */
524 if (first)
525 pch_i2c_start(adap);
526
527 if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
528 (pch_i2c_getack(adap) == 0)) {
529 pch_dbg(adap, "return %d\n", 0);
530
531 if (length == 0) {
532 pch_i2c_stop(adap);
533 ioread32(p + PCH_I2CDR); /* Dummy read needs */
534
535 count = length;
536 } else {
537 int read_index;
538 int loop;
539 pch_i2c_sendack(adap);
540
541 /* Dummy read */
542 for (loop = 1, read_index = 0; loop < length; loop++) {
543 buf[read_index] = ioread32(p + PCH_I2CDR);
544
545 if (loop != 1)
546 read_index++;
547
548 if (pch_i2c_wait_for_xfer_complete(adap) != 0) {
549 pch_i2c_stop(adap);
550 return -ETIME;
551 }
552 } /* end for */
553
554 pch_i2c_sendnack(adap);
555
556 buf[read_index] = ioread32(p + PCH_I2CDR);
557
558 if (length != 1)
559 read_index++;
560
561 if (pch_i2c_wait_for_xfer_complete(adap) == 0) {
562 if (last)
563 pch_i2c_stop(adap);
564 else
565 pch_i2c_repstart(adap);
566
567 buf[read_index++] = ioread32(p + PCH_I2CDR);
568 count = read_index;
569 } else {
570 count = -ETIME;
571 }
572
573 }
574 } else {
575 count = -ETIME;
576 pch_i2c_stop(adap);
577 }
578
579 return count;
580}
581
582/**
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900583 * pch_i2c_cb() - Interrupt handler Call back function
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900584 * @adap: Pointer to struct i2c_algo_pch_data.
585 */
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900586static void pch_i2c_cb(struct i2c_algo_pch_data *adap)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900587{
588 u32 sts;
589 void __iomem *p = adap->pch_base_address;
590
591 sts = ioread32(p + PCH_I2CSR);
592 sts &= (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT);
593 if (sts & I2CMAL_BIT)
594 adap->pch_event_flag |= I2CMAL_EVENT;
595
596 if (sts & I2CMCF_BIT)
597 adap->pch_event_flag |= I2CMCF_EVENT;
598
599 /* clear the applicable bits */
600 pch_clrbit(adap->pch_base_address, PCH_I2CSR, sts);
601
602 pch_dbg(adap, "PCH_I2CSR = %x\n", ioread32(p + PCH_I2CSR));
603
604 wake_up(&pch_event);
605}
606
607/**
608 * pch_i2c_handler() - interrupt handler for the PCH I2C controller
609 * @irq: irq number.
610 * @pData: cookie passed back to the handler function.
611 */
612static irqreturn_t pch_i2c_handler(int irq, void *pData)
613{
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900614 u32 reg_val;
615 int flag;
616 int i;
617 struct adapter_info *adap_info = pData;
618 void __iomem *p;
619 u32 mode;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900620
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900621 for (i = 0, flag = 0; i < adap_info->ch_num; i++) {
622 p = adap_info->pch_data[i].pch_base_address;
623 mode = ioread32(p + PCH_I2CMOD);
624 mode &= BUFFER_MODE | EEPROM_SR_MODE;
625 if (mode != NORMAL_MODE) {
626 pch_err(adap_info->pch_data,
627 "I2C-%d mode(%d) is not supported\n", mode, i);
628 continue;
629 }
630 reg_val = ioread32(p + PCH_I2CSR);
631 if (reg_val & (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT)) {
632 pch_i2c_cb(&adap_info->pch_data[i]);
633 flag = 1;
634 }
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900635 }
636
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900637 return flag ? IRQ_HANDLED : IRQ_NONE;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900638}
639
640/**
641 * pch_i2c_xfer() - Reading adnd writing data through I2C bus
642 * @i2c_adap: Pointer to the struct i2c_adapter.
643 * @msgs: Pointer to i2c_msg structure.
644 * @num: number of messages.
645 */
646static s32 pch_i2c_xfer(struct i2c_adapter *i2c_adap,
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900647 struct i2c_msg *msgs, s32 num)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900648{
649 struct i2c_msg *pmsg;
650 u32 i = 0;
651 u32 status;
652 u32 msglen;
653 u32 subaddrlen;
654 s32 ret;
655
656 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
657
658 ret = mutex_lock_interruptible(&pch_mutex);
659 if (ret)
660 return -ERESTARTSYS;
661
662 if (adap->p_adapter_info->pch_i2c_suspended) {
663 mutex_unlock(&pch_mutex);
664 return -EBUSY;
665 }
666
667 pch_dbg(adap, "adap->p_adapter_info->pch_i2c_suspended is %d\n",
668 adap->p_adapter_info->pch_i2c_suspended);
669 /* transfer not completed */
670 adap->pch_i2c_xfer_in_progress = true;
671
Tomoya MORINAGA07e729c2011-06-23 16:17:10 +0900672 for (i = 0; i < num && ret >= 0; i++) {
Tomoya MORINAGA7a9c42c2011-06-09 11:29:29 +0900673 pmsg = &msgs[i];
674 pmsg->flags |= adap->pch_buff_mode_en;
675 status = pmsg->flags;
676 pch_dbg(adap,
677 "After invoking I2C_MODE_SEL :flag= 0x%x\n", status);
678 /* calculate sub address length and message length */
679 /* these are applicable only for buffer mode */
680 subaddrlen = pmsg->buf[0];
681 /* calculate actual message length excluding
682 * the sub address fields */
683 msglen = (pmsg->len) - (subaddrlen + 1);
684
685 if ((status & (I2C_M_RD)) != false) {
686 ret = pch_i2c_readbytes(i2c_adap, pmsg, (i + 1 == num),
687 (i == 0));
688 } else {
689 ret = pch_i2c_writebytes(i2c_adap, pmsg, (i + 1 == num),
690 (i == 0));
691 }
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900692 }
693
694 adap->pch_i2c_xfer_in_progress = false; /* transfer completed */
695
696 mutex_unlock(&pch_mutex);
697
Tomoya MORINAGA07e729c2011-06-23 16:17:10 +0900698 return (ret < 0) ? ret : num;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900699}
700
701/**
702 * pch_i2c_func() - return the functionality of the I2C driver
703 * @adap: Pointer to struct i2c_algo_pch_data.
704 */
705static u32 pch_i2c_func(struct i2c_adapter *adap)
706{
707 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
708}
709
710static struct i2c_algorithm pch_algorithm = {
711 .master_xfer = pch_i2c_xfer,
712 .functionality = pch_i2c_func
713};
714
715/**
716 * pch_i2c_disbl_int() - Disable PCH I2C interrupts
717 * @adap: Pointer to struct i2c_algo_pch_data.
718 */
719static void pch_i2c_disbl_int(struct i2c_algo_pch_data *adap)
720{
721 void __iomem *p = adap->pch_base_address;
722
723 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, NORMAL_INTR_ENBL);
724
725 iowrite32(EEPROM_RST_INTR_DISBL, p + PCH_I2CESRMSK);
726
727 iowrite32(BUFFER_MODE_INTR_DISBL, p + PCH_I2CBUFMSK);
728}
729
730static int __devinit pch_i2c_probe(struct pci_dev *pdev,
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900731 const struct pci_device_id *id)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900732{
733 void __iomem *base_addr;
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900734 int ret;
735 int i, j;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900736 struct adapter_info *adap_info;
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900737 struct i2c_adapter *pch_adap;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900738
739 pch_pci_dbg(pdev, "Entered.\n");
740
741 adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL);
742 if (adap_info == NULL) {
743 pch_pci_err(pdev, "Memory allocation FAILED\n");
744 return -ENOMEM;
745 }
746
747 ret = pci_enable_device(pdev);
748 if (ret) {
749 pch_pci_err(pdev, "pci_enable_device FAILED\n");
750 goto err_pci_enable;
751 }
752
753 ret = pci_request_regions(pdev, KBUILD_MODNAME);
754 if (ret) {
755 pch_pci_err(pdev, "pci_request_regions FAILED\n");
756 goto err_pci_req;
757 }
758
759 base_addr = pci_iomap(pdev, 1, 0);
760
761 if (base_addr == NULL) {
762 pch_pci_err(pdev, "pci_iomap FAILED\n");
763 ret = -ENOMEM;
764 goto err_pci_iomap;
765 }
766
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900767 /* Set the number of I2C channel instance */
768 adap_info->ch_num = id->driver_data;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900769
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900770 for (i = 0; i < adap_info->ch_num; i++) {
771 pch_adap = &adap_info->pch_data[i].pch_adapter;
772 adap_info->pch_i2c_suspended = false;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900773
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900774 adap_info->pch_data[i].p_adapter_info = adap_info;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900775
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900776 pch_adap->owner = THIS_MODULE;
777 pch_adap->class = I2C_CLASS_HWMON;
778 strcpy(pch_adap->name, KBUILD_MODNAME);
779 pch_adap->algo = &pch_algorithm;
780 pch_adap->algo_data = &adap_info->pch_data[i];
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900781
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900782 /* base_addr + offset; */
783 adap_info->pch_data[i].pch_base_address = base_addr + 0x100 * i;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900784
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900785 pch_adap->dev.parent = &pdev->dev;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900786
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900787 ret = i2c_add_adapter(pch_adap);
788 if (ret) {
789 pch_pci_err(pdev, "i2c_add_adapter[ch:%d] FAILED\n", i);
790 goto err_i2c_add_adapter;
791 }
792
793 pch_i2c_init(&adap_info->pch_data[i]);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900794 }
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900795 ret = request_irq(pdev->irq, pch_i2c_handler, IRQF_SHARED,
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900796 KBUILD_MODNAME, adap_info);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900797 if (ret) {
798 pch_pci_err(pdev, "request_irq FAILED\n");
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900799 goto err_i2c_add_adapter;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900800 }
801
802 pci_set_drvdata(pdev, adap_info);
803 pch_pci_dbg(pdev, "returns %d.\n", ret);
804 return 0;
805
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900806err_i2c_add_adapter:
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900807 for (j = 0; j < i; j++)
808 i2c_del_adapter(&adap_info->pch_data[j].pch_adapter);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900809 pci_iounmap(pdev, base_addr);
810err_pci_iomap:
811 pci_release_regions(pdev);
812err_pci_req:
813 pci_disable_device(pdev);
814err_pci_enable:
815 kfree(adap_info);
816 return ret;
817}
818
819static void __devexit pch_i2c_remove(struct pci_dev *pdev)
820{
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900821 int i;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900822 struct adapter_info *adap_info = pci_get_drvdata(pdev);
823
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900824 free_irq(pdev->irq, adap_info);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900825
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900826 for (i = 0; i < adap_info->ch_num; i++) {
827 pch_i2c_disbl_int(&adap_info->pch_data[i]);
828 i2c_del_adapter(&adap_info->pch_data[i].pch_adapter);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900829 }
830
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900831 if (adap_info->pch_data[0].pch_base_address)
832 pci_iounmap(pdev, adap_info->pch_data[0].pch_base_address);
833
834 for (i = 0; i < adap_info->ch_num; i++)
835 adap_info->pch_data[i].pch_base_address = 0;
836
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900837 pci_set_drvdata(pdev, NULL);
838
839 pci_release_regions(pdev);
840
841 pci_disable_device(pdev);
842 kfree(adap_info);
843}
844
845#ifdef CONFIG_PM
846static int pch_i2c_suspend(struct pci_dev *pdev, pm_message_t state)
847{
848 int ret;
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900849 int i;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900850 struct adapter_info *adap_info = pci_get_drvdata(pdev);
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900851 void __iomem *p = adap_info->pch_data[0].pch_base_address;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900852
853 adap_info->pch_i2c_suspended = true;
854
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900855 for (i = 0; i < adap_info->ch_num; i++) {
856 while ((adap_info->pch_data[i].pch_i2c_xfer_in_progress)) {
857 /* Wait until all channel transfers are completed */
858 msleep(20);
859 }
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900860 }
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900861
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900862 /* Disable the i2c interrupts */
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900863 for (i = 0; i < adap_info->ch_num; i++)
864 pch_i2c_disbl_int(&adap_info->pch_data[i]);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900865
866 pch_pci_dbg(pdev, "I2CSR = %x I2CBUFSTA = %x I2CESRSTA = %x "
867 "invoked function pch_i2c_disbl_int successfully\n",
868 ioread32(p + PCH_I2CSR), ioread32(p + PCH_I2CBUFSTA),
869 ioread32(p + PCH_I2CESRSTA));
870
871 ret = pci_save_state(pdev);
872
873 if (ret) {
874 pch_pci_err(pdev, "pci_save_state\n");
875 return ret;
876 }
877
878 pci_enable_wake(pdev, PCI_D3hot, 0);
879 pci_disable_device(pdev);
880 pci_set_power_state(pdev, pci_choose_state(pdev, state));
881
882 return 0;
883}
884
885static int pch_i2c_resume(struct pci_dev *pdev)
886{
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900887 int i;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900888 struct adapter_info *adap_info = pci_get_drvdata(pdev);
889
890 pci_set_power_state(pdev, PCI_D0);
891 pci_restore_state(pdev);
892
893 if (pci_enable_device(pdev) < 0) {
894 pch_pci_err(pdev, "pch_i2c_resume:pci_enable_device FAILED\n");
895 return -EIO;
896 }
897
898 pci_enable_wake(pdev, PCI_D3hot, 0);
899
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900900 for (i = 0; i < adap_info->ch_num; i++)
901 pch_i2c_init(&adap_info->pch_data[i]);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900902
903 adap_info->pch_i2c_suspended = false;
904
905 return 0;
906}
907#else
908#define pch_i2c_suspend NULL
909#define pch_i2c_resume NULL
910#endif
911
912static struct pci_driver pch_pcidriver = {
913 .name = KBUILD_MODNAME,
914 .id_table = pch_pcidev_id,
915 .probe = pch_i2c_probe,
916 .remove = __devexit_p(pch_i2c_remove),
917 .suspend = pch_i2c_suspend,
918 .resume = pch_i2c_resume
919};
920
921static int __init pch_pci_init(void)
922{
923 return pci_register_driver(&pch_pcidriver);
924}
925module_init(pch_pci_init);
926
927static void __exit pch_pci_exit(void)
928{
929 pci_unregister_driver(&pch_pcidriver);
930}
931module_exit(pch_pci_exit);
932
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900933MODULE_DESCRIPTION("Intel EG20T PCH/OKI SEMICONDUCTOR ML7213 IOH I2C Driver");
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900934MODULE_LICENSE("GPL");
935MODULE_AUTHOR("Tomoya MORINAGA. <tomoya-linux@dsn.okisemi.com>");
936module_param(pch_i2c_speed, int, (S_IRUSR | S_IWUSR));
937module_param(pch_clk, int, (S_IRUSR | S_IWUSR));