i2c-eg20t.c 25 KB

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