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