i2c-eg20t.c 25 KB

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