i2c-eg20t.c 28 KB

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