blob: c57c8373469220c0fe11c28d0719ddb1ea0103ce [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>
32
33#define PCH_EVENT_SET 0 /* I2C Interrupt Event Set Status */
34#define PCH_EVENT_NONE 1 /* I2C Interrupt Event Clear Status */
35#define PCH_MAX_CLK 100000 /* Maximum Clock speed in MHz */
36#define PCH_BUFFER_MODE_ENABLE 0x0002 /* flag for Buffer mode enable */
37#define PCH_EEPROM_SW_RST_MODE_ENABLE 0x0008 /* EEPROM SW RST enable flag */
38
39#define PCH_I2CSADR 0x00 /* I2C slave address register */
40#define PCH_I2CCTL 0x04 /* I2C control register */
41#define PCH_I2CSR 0x08 /* I2C status register */
42#define PCH_I2CDR 0x0C /* I2C data register */
43#define PCH_I2CMON 0x10 /* I2C bus monitor register */
44#define PCH_I2CBC 0x14 /* I2C bus transfer rate setup counter */
45#define PCH_I2CMOD 0x18 /* I2C mode register */
46#define PCH_I2CBUFSLV 0x1C /* I2C buffer mode slave address register */
47#define PCH_I2CBUFSUB 0x20 /* I2C buffer mode subaddress register */
48#define PCH_I2CBUFFOR 0x24 /* I2C buffer mode format register */
49#define PCH_I2CBUFCTL 0x28 /* I2C buffer mode control register */
50#define PCH_I2CBUFMSK 0x2C /* I2C buffer mode interrupt mask register */
51#define PCH_I2CBUFSTA 0x30 /* I2C buffer mode status register */
52#define PCH_I2CBUFLEV 0x34 /* I2C buffer mode level register */
53#define PCH_I2CESRFOR 0x38 /* EEPROM software reset mode format register */
54#define PCH_I2CESRCTL 0x3C /* EEPROM software reset mode ctrl register */
55#define PCH_I2CESRMSK 0x40 /* EEPROM software reset mode */
56#define PCH_I2CESRSTA 0x44 /* EEPROM software reset mode status register */
57#define PCH_I2CTMR 0x48 /* I2C timer register */
58#define PCH_I2CSRST 0xFC /* I2C reset register */
59#define PCH_I2CNF 0xF8 /* I2C noise filter register */
60
61#define BUS_IDLE_TIMEOUT 20
62#define PCH_I2CCTL_I2CMEN 0x0080
63#define TEN_BIT_ADDR_DEFAULT 0xF000
64#define TEN_BIT_ADDR_MASK 0xF0
65#define PCH_START 0x0020
66#define PCH_ESR_START 0x0001
67#define PCH_BUFF_START 0x1
68#define PCH_REPSTART 0x0004
69#define PCH_ACK 0x0008
70#define PCH_GETACK 0x0001
71#define CLR_REG 0x0
72#define I2C_RD 0x1
73#define I2CMCF_BIT 0x0080
74#define I2CMIF_BIT 0x0002
75#define I2CMAL_BIT 0x0010
76#define I2CBMFI_BIT 0x0001
77#define I2CBMAL_BIT 0x0002
78#define I2CBMNA_BIT 0x0004
79#define I2CBMTO_BIT 0x0008
80#define I2CBMIS_BIT 0x0010
81#define I2CESRFI_BIT 0X0001
82#define I2CESRTO_BIT 0x0002
83#define I2CESRFIIE_BIT 0x1
84#define I2CESRTOIE_BIT 0x2
85#define I2CBMDZ_BIT 0x0040
86#define I2CBMAG_BIT 0x0020
87#define I2CMBB_BIT 0x0020
88#define BUFFER_MODE_MASK (I2CBMFI_BIT | I2CBMAL_BIT | I2CBMNA_BIT | \
89 I2CBMTO_BIT | I2CBMIS_BIT)
90#define I2C_ADDR_MSK 0xFF
91#define I2C_MSB_2B_MSK 0x300
92#define FAST_MODE_CLK 400
93#define FAST_MODE_EN 0x0001
94#define SUB_ADDR_LEN_MAX 4
95#define BUF_LEN_MAX 32
96#define PCH_BUFFER_MODE 0x1
97#define EEPROM_SW_RST_MODE 0x0002
98#define NORMAL_INTR_ENBL 0x0300
99#define EEPROM_RST_INTR_ENBL (I2CESRFIIE_BIT | I2CESRTOIE_BIT)
100#define EEPROM_RST_INTR_DISBL 0x0
101#define BUFFER_MODE_INTR_ENBL 0x001F
102#define BUFFER_MODE_INTR_DISBL 0x0
103#define NORMAL_MODE 0x0
104#define BUFFER_MODE 0x1
105#define EEPROM_SR_MODE 0x2
106#define I2C_TX_MODE 0x0010
107#define PCH_BUF_TX 0xFFF7
108#define PCH_BUF_RD 0x0008
109#define I2C_ERROR_MASK (I2CESRTO_EVENT | I2CBMIS_EVENT | I2CBMTO_EVENT | \
110 I2CBMNA_EVENT | I2CBMAL_EVENT | I2CMAL_EVENT)
111#define I2CMAL_EVENT 0x0001
112#define I2CMCF_EVENT 0x0002
113#define I2CBMFI_EVENT 0x0004
114#define I2CBMAL_EVENT 0x0008
115#define I2CBMNA_EVENT 0x0010
116#define I2CBMTO_EVENT 0x0020
117#define I2CBMIS_EVENT 0x0040
118#define I2CESRFI_EVENT 0x0080
119#define I2CESRTO_EVENT 0x0100
120#define PCI_DEVICE_ID_PCH_I2C 0x8817
121
122#define pch_dbg(adap, fmt, arg...) \
123 dev_dbg(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
124
125#define pch_err(adap, fmt, arg...) \
126 dev_err(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
127
128#define pch_pci_err(pdev, fmt, arg...) \
129 dev_err(&pdev->dev, "%s :" fmt, __func__, ##arg)
130
131#define pch_pci_dbg(pdev, fmt, arg...) \
132 dev_dbg(&pdev->dev, "%s :" fmt, __func__, ##arg)
133
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900134/*
135Set the number of I2C instance max
136Intel EG20T PCH : 1ch
137OKI SEMICONDUCTOR ML7213 IOH : 2ch
138*/
139#define PCH_I2C_MAX_DEV 2
140
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900141/**
142 * struct i2c_algo_pch_data - for I2C driver functionalities
143 * @pch_adapter: stores the reference to i2c_adapter structure
144 * @p_adapter_info: stores the reference to adapter_info structure
145 * @pch_base_address: specifies the remapped base address
146 * @pch_buff_mode_en: specifies if buffer mode is enabled
147 * @pch_event_flag: specifies occurrence of interrupt events
148 * @pch_i2c_xfer_in_progress: specifies whether the transfer is completed
149 */
150struct i2c_algo_pch_data {
151 struct i2c_adapter pch_adapter;
152 struct adapter_info *p_adapter_info;
153 void __iomem *pch_base_address;
154 int pch_buff_mode_en;
155 u32 pch_event_flag;
156 bool pch_i2c_xfer_in_progress;
157};
158
159/**
160 * struct adapter_info - This structure holds the adapter information for the
161 PCH i2c controller
162 * @pch_data: stores a list of i2c_algo_pch_data
163 * @pch_i2c_suspended: specifies whether the system is suspended or not
164 * perhaps with more lines and words.
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900165 * @ch_num: specifies the number of i2c instance
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900166 *
167 * pch_data has as many elements as maximum I2C channels
168 */
169struct adapter_info {
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900170 struct i2c_algo_pch_data pch_data[PCH_I2C_MAX_DEV];
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900171 bool pch_i2c_suspended;
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900172 int ch_num;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900173};
174
175
176static int pch_i2c_speed = 100; /* I2C bus speed in Kbps */
177static int pch_clk = 50000; /* specifies I2C clock speed in KHz */
178static wait_queue_head_t pch_event;
179static DEFINE_MUTEX(pch_mutex);
180
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900181/* Definition for ML7213 by OKI SEMICONDUCTOR */
182#define PCI_VENDOR_ID_ROHM 0x10DB
183#define PCI_DEVICE_ID_ML7213_I2C 0x802D
184
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900185static struct pci_device_id __devinitdata pch_pcidev_id[] = {
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900186 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PCH_I2C), 1, },
187 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_I2C), 2, },
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900188 {0,}
189};
190
191static irqreturn_t pch_i2c_handler(int irq, void *pData);
192
193static inline void pch_setbit(void __iomem *addr, u32 offset, u32 bitmask)
194{
195 u32 val;
196 val = ioread32(addr + offset);
197 val |= bitmask;
198 iowrite32(val, addr + offset);
199}
200
201static inline void pch_clrbit(void __iomem *addr, u32 offset, u32 bitmask)
202{
203 u32 val;
204 val = ioread32(addr + offset);
205 val &= (~bitmask);
206 iowrite32(val, addr + offset);
207}
208
209/**
210 * pch_i2c_init() - hardware initialization of I2C module
211 * @adap: Pointer to struct i2c_algo_pch_data.
212 */
213static void pch_i2c_init(struct i2c_algo_pch_data *adap)
214{
215 void __iomem *p = adap->pch_base_address;
216 u32 pch_i2cbc;
217 u32 pch_i2ctmr;
218 u32 reg_value;
219
220 /* reset I2C controller */
221 iowrite32(0x01, p + PCH_I2CSRST);
222 msleep(20);
223 iowrite32(0x0, p + PCH_I2CSRST);
224
225 /* Initialize I2C registers */
226 iowrite32(0x21, p + PCH_I2CNF);
227
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900228 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_I2CCTL_I2CMEN);
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900229
230 if (pch_i2c_speed != 400)
231 pch_i2c_speed = 100;
232
233 reg_value = PCH_I2CCTL_I2CMEN;
234 if (pch_i2c_speed == FAST_MODE_CLK) {
235 reg_value |= FAST_MODE_EN;
236 pch_dbg(adap, "Fast mode enabled\n");
237 }
238
239 if (pch_clk > PCH_MAX_CLK)
240 pch_clk = 62500;
241
242 pch_i2cbc = (pch_clk + (pch_i2c_speed * 4)) / pch_i2c_speed * 8;
243 /* Set transfer speed in I2CBC */
244 iowrite32(pch_i2cbc, p + PCH_I2CBC);
245
246 pch_i2ctmr = (pch_clk) / 8;
247 iowrite32(pch_i2ctmr, p + PCH_I2CTMR);
248
249 reg_value |= NORMAL_INTR_ENBL; /* Enable interrupts in normal mode */
250 iowrite32(reg_value, p + PCH_I2CCTL);
251
252 pch_dbg(adap,
253 "I2CCTL=%x pch_i2cbc=%x pch_i2ctmr=%x Enable interrupts\n",
254 ioread32(p + PCH_I2CCTL), pch_i2cbc, pch_i2ctmr);
255
256 init_waitqueue_head(&pch_event);
257}
258
259static inline bool ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
260{
261 return cmp1.tv64 < cmp2.tv64;
262}
263
264/**
265 * pch_i2c_wait_for_bus_idle() - check the status of bus.
266 * @adap: Pointer to struct i2c_algo_pch_data.
267 * @timeout: waiting time counter (us).
268 */
269static s32 pch_i2c_wait_for_bus_idle(struct i2c_algo_pch_data *adap,
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900270 s32 timeout)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900271{
272 void __iomem *p = adap->pch_base_address;
273
274 /* MAX timeout value is timeout*1000*1000nsec */
275 ktime_t ns_val = ktime_add_ns(ktime_get(), timeout*1000*1000);
276 do {
277 if ((ioread32(p + PCH_I2CSR) & I2CMBB_BIT) == 0)
278 break;
279 msleep(20);
280 } while (ktime_lt(ktime_get(), ns_val));
281
282 pch_dbg(adap, "I2CSR = %x\n", ioread32(p + PCH_I2CSR));
283
284 if (timeout == 0) {
285 pch_err(adap, "%s: Timeout Error.return%d\n", __func__, -ETIME);
286 return -ETIME;
287 }
288
289 return 0;
290}
291
292/**
293 * pch_i2c_start() - Generate I2C start condition in normal mode.
294 * @adap: Pointer to struct i2c_algo_pch_data.
295 *
296 * Generate I2C start condition in normal mode by setting I2CCTL.I2CMSTA to 1.
297 */
298static void pch_i2c_start(struct i2c_algo_pch_data *adap)
299{
300 void __iomem *p = adap->pch_base_address;
301 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
302 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
303}
304
305/**
306 * pch_i2c_wait_for_xfer_complete() - initiates a wait for the tx complete event
307 * @adap: Pointer to struct i2c_algo_pch_data.
308 */
309static s32 pch_i2c_wait_for_xfer_complete(struct i2c_algo_pch_data *adap)
310{
311 s32 ret;
312 ret = wait_event_timeout(pch_event,
313 (adap->pch_event_flag != 0), msecs_to_jiffies(50));
314 if (ret < 0) {
315 pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
316 return ret;
317 }
318
319 if (ret == 0) {
320 pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
321 return -ETIMEDOUT;
322 }
323
324 if (adap->pch_event_flag & I2C_ERROR_MASK) {
325 pch_err(adap, "error bits set: %x\n", adap->pch_event_flag);
326 return -EIO;
327 }
328
329 adap->pch_event_flag = 0;
330
331 return 0;
332}
333
334/**
335 * pch_i2c_getack() - to confirm ACK/NACK
336 * @adap: Pointer to struct i2c_algo_pch_data.
337 */
338static s32 pch_i2c_getack(struct i2c_algo_pch_data *adap)
339{
340 u32 reg_val;
341 void __iomem *p = adap->pch_base_address;
342 reg_val = ioread32(p + PCH_I2CSR) & PCH_GETACK;
343
344 if (reg_val != 0) {
345 pch_err(adap, "return%d\n", -EPROTO);
346 return -EPROTO;
347 }
348
349 return 0;
350}
351
352/**
353 * pch_i2c_stop() - generate stop condition in normal mode.
354 * @adap: Pointer to struct i2c_algo_pch_data.
355 */
356static void pch_i2c_stop(struct i2c_algo_pch_data *adap)
357{
358 void __iomem *p = adap->pch_base_address;
359 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
360 /* clear the start bit */
361 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
362}
363
364/**
365 * pch_i2c_repstart() - generate repeated start condition in normal mode
366 * @adap: Pointer to struct i2c_algo_pch_data.
367 */
368static void pch_i2c_repstart(struct i2c_algo_pch_data *adap)
369{
370 void __iomem *p = adap->pch_base_address;
371 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
372 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_REPSTART);
373}
374
375/**
376 * pch_i2c_writebytes() - write data to I2C bus in normal mode
377 * @i2c_adap: Pointer to the struct i2c_adapter.
378 * @last: specifies whether last message or not.
379 * In the case of compound mode it will be 1 for last message,
380 * otherwise 0.
381 * @first: specifies whether first message or not.
382 * 1 for first message otherwise 0.
383 */
384static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,
385 struct i2c_msg *msgs, u32 last, u32 first)
386{
387 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
388 u8 *buf;
389 u32 length;
390 u32 addr;
391 u32 addr_2_msb;
392 u32 addr_8_lsb;
393 s32 wrcount;
394 void __iomem *p = adap->pch_base_address;
395
396 length = msgs->len;
397 buf = msgs->buf;
398 addr = msgs->addr;
399
400 /* enable master tx */
401 pch_setbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
402
403 pch_dbg(adap, "I2CCTL = %x msgs->len = %d\n", ioread32(p + PCH_I2CCTL),
404 length);
405
406 if (first) {
407 if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
408 return -ETIME;
409 }
410
411 if (msgs->flags & I2C_M_TEN) {
412 addr_2_msb = ((addr & I2C_MSB_2B_MSK) >> 7);
413 iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
414 if (first)
415 pch_i2c_start(adap);
416 if (pch_i2c_wait_for_xfer_complete(adap) == 0 &&
417 pch_i2c_getack(adap) == 0) {
418 addr_8_lsb = (addr & I2C_ADDR_MSK);
419 iowrite32(addr_8_lsb, p + PCH_I2CDR);
420 } else {
421 pch_i2c_stop(adap);
422 return -ETIME;
423 }
424 } else {
425 /* set 7 bit slave address and R/W bit as 0 */
426 iowrite32(addr << 1, p + PCH_I2CDR);
427 if (first)
428 pch_i2c_start(adap);
429 }
430
431 if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
432 (pch_i2c_getack(adap) == 0)) {
433 for (wrcount = 0; wrcount < length; ++wrcount) {
434 /* write buffer value to I2C data register */
435 iowrite32(buf[wrcount], p + PCH_I2CDR);
436 pch_dbg(adap, "writing %x to Data register\n",
437 buf[wrcount]);
438
439 if (pch_i2c_wait_for_xfer_complete(adap) != 0)
440 return -ETIME;
441
442 if (pch_i2c_getack(adap))
443 return -EIO;
444 }
445
446 /* check if this is the last message */
447 if (last)
448 pch_i2c_stop(adap);
449 else
450 pch_i2c_repstart(adap);
451 } else {
452 pch_i2c_stop(adap);
453 return -EIO;
454 }
455
456 pch_dbg(adap, "return=%d\n", wrcount);
457
458 return wrcount;
459}
460
461/**
462 * pch_i2c_sendack() - send ACK
463 * @adap: Pointer to struct i2c_algo_pch_data.
464 */
465static void pch_i2c_sendack(struct i2c_algo_pch_data *adap)
466{
467 void __iomem *p = adap->pch_base_address;
468 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
469 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
470}
471
472/**
473 * pch_i2c_sendnack() - send NACK
474 * @adap: Pointer to struct i2c_algo_pch_data.
475 */
476static void pch_i2c_sendnack(struct i2c_algo_pch_data *adap)
477{
478 void __iomem *p = adap->pch_base_address;
479 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
480 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
481}
482
483/**
484 * pch_i2c_readbytes() - read data from I2C bus in normal mode.
485 * @i2c_adap: Pointer to the struct i2c_adapter.
486 * @msgs: Pointer to i2c_msg structure.
487 * @last: specifies whether last message or not.
488 * @first: specifies whether first message or not.
489 */
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900490static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
491 u32 last, u32 first)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900492{
493 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
494
495 u8 *buf;
496 u32 count;
497 u32 length;
498 u32 addr;
499 u32 addr_2_msb;
500 void __iomem *p = adap->pch_base_address;
501
502 length = msgs->len;
503 buf = msgs->buf;
504 addr = msgs->addr;
505
506 /* enable master reception */
507 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
508
509 if (first) {
510 if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
511 return -ETIME;
512 }
513
514 if (msgs->flags & I2C_M_TEN) {
515 addr_2_msb = (((addr & I2C_MSB_2B_MSK) >> 7) | (I2C_RD));
516 iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
517
518 } else {
519 /* 7 address bits + R/W bit */
520 addr = (((addr) << 1) | (I2C_RD));
521 iowrite32(addr, p + PCH_I2CDR);
522 }
523
524 /* check if it is the first message */
525 if (first)
526 pch_i2c_start(adap);
527
528 if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
529 (pch_i2c_getack(adap) == 0)) {
530 pch_dbg(adap, "return %d\n", 0);
531
532 if (length == 0) {
533 pch_i2c_stop(adap);
534 ioread32(p + PCH_I2CDR); /* Dummy read needs */
535
536 count = length;
537 } else {
538 int read_index;
539 int loop;
540 pch_i2c_sendack(adap);
541
542 /* Dummy read */
543 for (loop = 1, read_index = 0; loop < length; loop++) {
544 buf[read_index] = ioread32(p + PCH_I2CDR);
545
546 if (loop != 1)
547 read_index++;
548
549 if (pch_i2c_wait_for_xfer_complete(adap) != 0) {
550 pch_i2c_stop(adap);
551 return -ETIME;
552 }
553 } /* end for */
554
555 pch_i2c_sendnack(adap);
556
557 buf[read_index] = ioread32(p + PCH_I2CDR);
558
559 if (length != 1)
560 read_index++;
561
562 if (pch_i2c_wait_for_xfer_complete(adap) == 0) {
563 if (last)
564 pch_i2c_stop(adap);
565 else
566 pch_i2c_repstart(adap);
567
568 buf[read_index++] = ioread32(p + PCH_I2CDR);
569 count = read_index;
570 } else {
571 count = -ETIME;
572 }
573
574 }
575 } else {
576 count = -ETIME;
577 pch_i2c_stop(adap);
578 }
579
580 return count;
581}
582
583/**
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900584 * pch_i2c_cb() - Interrupt handler Call back function
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900585 * @adap: Pointer to struct i2c_algo_pch_data.
586 */
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900587static void pch_i2c_cb(struct i2c_algo_pch_data *adap)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900588{
589 u32 sts;
590 void __iomem *p = adap->pch_base_address;
591
592 sts = ioread32(p + PCH_I2CSR);
593 sts &= (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT);
594 if (sts & I2CMAL_BIT)
595 adap->pch_event_flag |= I2CMAL_EVENT;
596
597 if (sts & I2CMCF_BIT)
598 adap->pch_event_flag |= I2CMCF_EVENT;
599
600 /* clear the applicable bits */
601 pch_clrbit(adap->pch_base_address, PCH_I2CSR, sts);
602
603 pch_dbg(adap, "PCH_I2CSR = %x\n", ioread32(p + PCH_I2CSR));
604
605 wake_up(&pch_event);
606}
607
608/**
609 * pch_i2c_handler() - interrupt handler for the PCH I2C controller
610 * @irq: irq number.
611 * @pData: cookie passed back to the handler function.
612 */
613static irqreturn_t pch_i2c_handler(int irq, void *pData)
614{
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900615 u32 reg_val;
616 int flag;
617 int i;
618 struct adapter_info *adap_info = pData;
619 void __iomem *p;
620 u32 mode;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900621
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900622 for (i = 0, flag = 0; i < adap_info->ch_num; i++) {
623 p = adap_info->pch_data[i].pch_base_address;
624 mode = ioread32(p + PCH_I2CMOD);
625 mode &= BUFFER_MODE | EEPROM_SR_MODE;
626 if (mode != NORMAL_MODE) {
627 pch_err(adap_info->pch_data,
628 "I2C-%d mode(%d) is not supported\n", mode, i);
629 continue;
630 }
631 reg_val = ioread32(p + PCH_I2CSR);
632 if (reg_val & (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT)) {
633 pch_i2c_cb(&adap_info->pch_data[i]);
634 flag = 1;
635 }
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900636 }
637
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900638 return flag ? IRQ_HANDLED : IRQ_NONE;
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900639}
640
641/**
642 * pch_i2c_xfer() - Reading adnd writing data through I2C bus
643 * @i2c_adap: Pointer to the struct i2c_adapter.
644 * @msgs: Pointer to i2c_msg structure.
645 * @num: number of messages.
646 */
647static s32 pch_i2c_xfer(struct i2c_adapter *i2c_adap,
Tomoya MORINAGA173442f2011-03-01 14:16:23 +0900648 struct i2c_msg *msgs, s32 num)
Tomoya MORINAGAe9bc8fa2010-11-09 13:25:22 +0900649{
650 struct i2c_msg *pmsg;
651 u32 i = 0;
652 u32 status;
653 u32 msglen;
654 u32 subaddrlen;
655 s32 ret;
656
657 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
658
659 ret = mutex_lock_interruptible(&pch_mutex);
660 if (ret)
661 return -ERESTARTSYS;
662
663 if (adap->p_adapter_info->pch_i2c_suspended) {
664 mutex_unlock(&pch_mutex);
665 return -EBUSY;
666 }
667
668 pch_dbg(adap, "adap->p_adapter_info->pch_i2c_suspended is %d\n",
669 adap->p_adapter_info->pch_i2c_suspended);
670 /* transfer not completed */
671 adap->pch_i2c_xfer_in_progress = true;
672
673 pmsg = &msgs[0];
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 if (status & (I2C_M_RD)) {
685 pch_dbg(adap, "invoking pch_i2c_readbytes\n");
686 ret = pch_i2c_readbytes(i2c_adap, pmsg, (i + 1 == num),
687 (i == 0));
688 } else {
689 pch_dbg(adap, "invoking pch_i2c_writebytes\n");
690 ret = pch_i2c_writebytes(i2c_adap, pmsg, (i + 1 == num),
691 (i == 0));
692 }
693
694 adap->pch_i2c_xfer_in_progress = false; /* transfer completed */
695
696 mutex_unlock(&pch_mutex);
697
698 return ret;
699}
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));