i2c-eg20t.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. static inline bool ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
  236. {
  237. return cmp1.tv64 < cmp2.tv64;
  238. }
  239. /**
  240. * pch_i2c_wait_for_bus_idle() - check the status of bus.
  241. * @adap: Pointer to struct i2c_algo_pch_data.
  242. * @timeout: waiting time counter (ms).
  243. */
  244. static s32 pch_i2c_wait_for_bus_idle(struct i2c_algo_pch_data *adap,
  245. s32 timeout)
  246. {
  247. void __iomem *p = adap->pch_base_address;
  248. int schedule = 0;
  249. unsigned long end = jiffies + msecs_to_jiffies(timeout);
  250. while (ioread32(p + PCH_I2CSR) & I2CMBB_BIT) {
  251. if (time_after(jiffies, end)) {
  252. pch_dbg(adap, "I2CSR = %x\n", ioread32(p + PCH_I2CSR));
  253. pch_err(adap, "%s: Timeout Error.return%d\n",
  254. __func__, -ETIME);
  255. pch_i2c_init(adap);
  256. return -ETIME;
  257. }
  258. if (!schedule)
  259. /* Retry after some usecs */
  260. udelay(5);
  261. else
  262. /* Wait a bit more without consuming CPU */
  263. usleep_range(20, 1000);
  264. schedule = 1;
  265. }
  266. return 0;
  267. }
  268. /**
  269. * pch_i2c_start() - Generate I2C start condition in normal mode.
  270. * @adap: Pointer to struct i2c_algo_pch_data.
  271. *
  272. * Generate I2C start condition in normal mode by setting I2CCTL.I2CMSTA to 1.
  273. */
  274. static void pch_i2c_start(struct i2c_algo_pch_data *adap)
  275. {
  276. void __iomem *p = adap->pch_base_address;
  277. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  278. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
  279. }
  280. /**
  281. * pch_i2c_wait_for_xfer_complete() - initiates a wait for the tx complete event
  282. * @adap: Pointer to struct i2c_algo_pch_data.
  283. */
  284. static s32 pch_i2c_wait_for_xfer_complete(struct i2c_algo_pch_data *adap)
  285. {
  286. long ret;
  287. ret = wait_event_timeout(pch_event,
  288. (adap->pch_event_flag != 0), msecs_to_jiffies(1000));
  289. if (ret == 0) {
  290. pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
  291. adap->pch_event_flag = 0;
  292. return -ETIMEDOUT;
  293. }
  294. if (adap->pch_event_flag & I2C_ERROR_MASK) {
  295. pch_err(adap, "error bits set: %x\n", adap->pch_event_flag);
  296. adap->pch_event_flag = 0;
  297. return -EIO;
  298. }
  299. adap->pch_event_flag = 0;
  300. return 0;
  301. }
  302. /**
  303. * pch_i2c_getack() - to confirm ACK/NACK
  304. * @adap: Pointer to struct i2c_algo_pch_data.
  305. */
  306. static s32 pch_i2c_getack(struct i2c_algo_pch_data *adap)
  307. {
  308. u32 reg_val;
  309. void __iomem *p = adap->pch_base_address;
  310. reg_val = ioread32(p + PCH_I2CSR) & PCH_GETACK;
  311. if (reg_val != 0) {
  312. pch_err(adap, "return%d\n", -EPROTO);
  313. return -EPROTO;
  314. }
  315. return 0;
  316. }
  317. /**
  318. * pch_i2c_stop() - generate stop condition in normal mode.
  319. * @adap: Pointer to struct i2c_algo_pch_data.
  320. */
  321. static void pch_i2c_stop(struct i2c_algo_pch_data *adap)
  322. {
  323. void __iomem *p = adap->pch_base_address;
  324. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  325. /* clear the start bit */
  326. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
  327. }
  328. /**
  329. * pch_i2c_repstart() - generate repeated start condition in normal mode
  330. * @adap: Pointer to struct i2c_algo_pch_data.
  331. */
  332. static void pch_i2c_repstart(struct i2c_algo_pch_data *adap)
  333. {
  334. void __iomem *p = adap->pch_base_address;
  335. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  336. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_REPSTART);
  337. }
  338. /**
  339. * pch_i2c_writebytes() - write data to I2C bus in normal mode
  340. * @i2c_adap: Pointer to the struct i2c_adapter.
  341. * @last: specifies whether last message or not.
  342. * In the case of compound mode it will be 1 for last message,
  343. * otherwise 0.
  344. * @first: specifies whether first message or not.
  345. * 1 for first message otherwise 0.
  346. */
  347. static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,
  348. struct i2c_msg *msgs, u32 last, u32 first)
  349. {
  350. struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
  351. u8 *buf;
  352. u32 length;
  353. u32 addr;
  354. u32 addr_2_msb;
  355. u32 addr_8_lsb;
  356. s32 wrcount;
  357. s32 rtn;
  358. void __iomem *p = adap->pch_base_address;
  359. length = msgs->len;
  360. buf = msgs->buf;
  361. addr = msgs->addr;
  362. /* enable master tx */
  363. pch_setbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
  364. pch_dbg(adap, "I2CCTL = %x msgs->len = %d\n", ioread32(p + PCH_I2CCTL),
  365. length);
  366. if (first) {
  367. if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
  368. return -ETIME;
  369. }
  370. if (msgs->flags & I2C_M_TEN) {
  371. addr_2_msb = ((addr & I2C_MSB_2B_MSK) >> 7) & 0x06;
  372. iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
  373. if (first)
  374. pch_i2c_start(adap);
  375. rtn = pch_i2c_wait_for_xfer_complete(adap);
  376. if (rtn == 0) {
  377. if (pch_i2c_getack(adap)) {
  378. pch_dbg(adap, "Receive NACK for slave address"
  379. "setting\n");
  380. return -EIO;
  381. }
  382. addr_8_lsb = (addr & I2C_ADDR_MSK);
  383. iowrite32(addr_8_lsb, p + PCH_I2CDR);
  384. } else if (rtn == -EIO) { /* Arbitration Lost */
  385. pch_err(adap, "Lost Arbitration\n");
  386. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  387. I2CMAL_BIT);
  388. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  389. I2CMIF_BIT);
  390. pch_i2c_init(adap);
  391. return -EAGAIN;
  392. } else { /* wait-event timeout */
  393. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  394. __func__, __LINE__);
  395. pch_i2c_stop(adap);
  396. pch_i2c_init(adap);
  397. return -ETIME;
  398. }
  399. } else {
  400. /* set 7 bit slave address and R/W bit as 0 */
  401. iowrite32(addr << 1, p + PCH_I2CDR);
  402. if (first)
  403. pch_i2c_start(adap);
  404. }
  405. rtn = pch_i2c_wait_for_xfer_complete(adap);
  406. if (rtn == 0) {
  407. if (pch_i2c_getack(adap)) {
  408. pch_dbg(adap, "Receive NACK for slave address"
  409. "setting\n");
  410. return -EIO;
  411. }
  412. } else if (rtn == -EIO) { /* Arbitration Lost */
  413. pch_err(adap, "Lost Arbitration\n");
  414. pch_clrbit(adap->pch_base_address, PCH_I2CSR, I2CMAL_BIT);
  415. pch_clrbit(adap->pch_base_address, PCH_I2CSR, I2CMIF_BIT);
  416. pch_i2c_init(adap);
  417. return -EAGAIN;
  418. } else { /* wait-event timeout */
  419. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  420. __func__, __LINE__);
  421. pch_i2c_stop(adap);
  422. pch_i2c_init(adap);
  423. return -ETIME;
  424. }
  425. for (wrcount = 0; wrcount < length; ++wrcount) {
  426. /* write buffer value to I2C data register */
  427. iowrite32(buf[wrcount], p + PCH_I2CDR);
  428. pch_dbg(adap, "writing %x to Data register\n", buf[wrcount]);
  429. rtn = pch_i2c_wait_for_xfer_complete(adap);
  430. if (rtn == 0) {
  431. if (pch_i2c_getack(adap)) {
  432. pch_dbg(adap, "Receive NACK for slave address"
  433. "setting\n");
  434. return -EIO;
  435. }
  436. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  437. I2CMCF_BIT);
  438. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  439. I2CMIF_BIT);
  440. } else { /* wait-event timeout */
  441. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  442. __func__, __LINE__);
  443. pch_i2c_stop(adap);
  444. pch_i2c_init(adap);
  445. return -ETIME;
  446. }
  447. }
  448. /* check if this is the last message */
  449. if (last)
  450. pch_i2c_stop(adap);
  451. else
  452. pch_i2c_repstart(adap);
  453. pch_dbg(adap, "return=%d\n", wrcount);
  454. return wrcount;
  455. }
  456. /**
  457. * pch_i2c_sendack() - send ACK
  458. * @adap: Pointer to struct i2c_algo_pch_data.
  459. */
  460. static void pch_i2c_sendack(struct i2c_algo_pch_data *adap)
  461. {
  462. void __iomem *p = adap->pch_base_address;
  463. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  464. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
  465. }
  466. /**
  467. * pch_i2c_sendnack() - send NACK
  468. * @adap: Pointer to struct i2c_algo_pch_data.
  469. */
  470. static void pch_i2c_sendnack(struct i2c_algo_pch_data *adap)
  471. {
  472. void __iomem *p = adap->pch_base_address;
  473. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  474. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
  475. }
  476. /**
  477. * pch_i2c_restart() - Generate I2C restart condition in normal mode.
  478. * @adap: Pointer to struct i2c_algo_pch_data.
  479. *
  480. * Generate I2C restart condition in normal mode by setting I2CCTL.I2CRSTA.
  481. */
  482. static void pch_i2c_restart(struct i2c_algo_pch_data *adap)
  483. {
  484. void __iomem *p = adap->pch_base_address;
  485. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  486. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_RESTART);
  487. }
  488. /**
  489. * pch_i2c_readbytes() - read data from I2C bus in normal mode.
  490. * @i2c_adap: Pointer to the struct i2c_adapter.
  491. * @msgs: Pointer to i2c_msg structure.
  492. * @last: specifies whether last message or not.
  493. * @first: specifies whether first message or not.
  494. */
  495. static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
  496. u32 last, u32 first)
  497. {
  498. struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
  499. u8 *buf;
  500. u32 count;
  501. u32 length;
  502. u32 addr;
  503. u32 addr_2_msb;
  504. u32 addr_8_lsb;
  505. void __iomem *p = adap->pch_base_address;
  506. s32 rtn;
  507. length = msgs->len;
  508. buf = msgs->buf;
  509. addr = msgs->addr;
  510. /* enable master reception */
  511. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
  512. if (first) {
  513. if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
  514. return -ETIME;
  515. }
  516. if (msgs->flags & I2C_M_TEN) {
  517. addr_2_msb = ((addr & I2C_MSB_2B_MSK) >> 7);
  518. iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
  519. if (first)
  520. pch_i2c_start(adap);
  521. rtn = pch_i2c_wait_for_xfer_complete(adap);
  522. if (rtn == 0) {
  523. if (pch_i2c_getack(adap)) {
  524. pch_dbg(adap, "Receive NACK for slave address"
  525. "setting\n");
  526. return -EIO;
  527. }
  528. addr_8_lsb = (addr & I2C_ADDR_MSK);
  529. iowrite32(addr_8_lsb, p + PCH_I2CDR);
  530. } else if (rtn == -EIO) { /* Arbitration Lost */
  531. pch_err(adap, "Lost Arbitration\n");
  532. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  533. I2CMAL_BIT);
  534. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  535. I2CMIF_BIT);
  536. pch_i2c_init(adap);
  537. return -EAGAIN;
  538. } else { /* wait-event timeout */
  539. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  540. __func__, __LINE__);
  541. pch_i2c_stop(adap);
  542. pch_i2c_init(adap);
  543. return -ETIME;
  544. }
  545. pch_i2c_restart(adap);
  546. rtn = pch_i2c_wait_for_xfer_complete(adap);
  547. if (rtn == 0) {
  548. if (pch_i2c_getack(adap)) {
  549. pch_dbg(adap, "Receive NACK for slave address"
  550. "setting\n");
  551. return -EIO;
  552. }
  553. addr_2_msb |= I2C_RD;
  554. iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK,
  555. p + PCH_I2CDR);
  556. } else if (rtn == -EIO) { /* Arbitration Lost */
  557. pch_err(adap, "Lost Arbitration\n");
  558. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  559. I2CMAL_BIT);
  560. pch_clrbit(adap->pch_base_address, PCH_I2CSR,
  561. I2CMIF_BIT);
  562. pch_i2c_init(adap);
  563. return -EAGAIN;
  564. } else { /* wait-event timeout */
  565. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  566. __func__, __LINE__);
  567. pch_i2c_stop(adap);
  568. pch_i2c_init(adap);
  569. return -ETIME;
  570. }
  571. } else {
  572. /* 7 address bits + R/W bit */
  573. addr = (((addr) << 1) | (I2C_RD));
  574. iowrite32(addr, p + PCH_I2CDR);
  575. }
  576. /* check if it is the first message */
  577. if (first)
  578. pch_i2c_start(adap);
  579. rtn = pch_i2c_wait_for_xfer_complete(adap);
  580. if (rtn == 0) {
  581. if (pch_i2c_getack(adap)) {
  582. pch_dbg(adap, "Receive NACK for slave address"
  583. "setting\n");
  584. return -EIO;
  585. }
  586. } else if (rtn == -EIO) { /* Arbitration Lost */
  587. pch_err(adap, "Lost Arbitration\n");
  588. pch_clrbit(adap->pch_base_address, PCH_I2CSR, I2CMAL_BIT);
  589. pch_clrbit(adap->pch_base_address, PCH_I2CSR, I2CMIF_BIT);
  590. pch_i2c_init(adap);
  591. return -EAGAIN;
  592. } else { /* wait-event timeout */
  593. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  594. __func__, __LINE__);
  595. pch_i2c_stop(adap);
  596. pch_i2c_init(adap);
  597. return -ETIME;
  598. }
  599. if (length == 0) {
  600. pch_i2c_stop(adap);
  601. ioread32(p + PCH_I2CDR); /* Dummy read needs */
  602. count = length;
  603. } else {
  604. int read_index;
  605. int loop;
  606. pch_i2c_sendack(adap);
  607. /* Dummy read */
  608. for (loop = 1, read_index = 0; loop < length; loop++) {
  609. buf[read_index] = ioread32(p + PCH_I2CDR);
  610. if (loop != 1)
  611. read_index++;
  612. rtn = pch_i2c_wait_for_xfer_complete(adap);
  613. if (rtn == 0) {
  614. if (pch_i2c_getack(adap)) {
  615. pch_dbg(adap, "Receive NACK for slave"
  616. "address setting\n");
  617. return -EIO;
  618. }
  619. } else { /* wait-event timeout */
  620. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  621. __func__, __LINE__);
  622. pch_i2c_stop(adap);
  623. pch_i2c_init(adap);
  624. return -ETIME;
  625. }
  626. } /* end for */
  627. pch_i2c_sendnack(adap);
  628. buf[read_index] = ioread32(p + PCH_I2CDR); /* Read final - 1 */
  629. if (length != 1)
  630. read_index++;
  631. rtn = pch_i2c_wait_for_xfer_complete(adap);
  632. if (rtn == 0) {
  633. if (pch_i2c_getack(adap)) {
  634. pch_dbg(adap, "Receive NACK for slave"
  635. "address setting\n");
  636. return -EIO;
  637. }
  638. } else { /* wait-event timeout */
  639. pch_err(adap, "%s(L.%d):wait-event timeout\n",
  640. __func__, __LINE__);
  641. pch_i2c_stop(adap);
  642. pch_i2c_init(adap);
  643. return -ETIME;
  644. }
  645. if (last)
  646. pch_i2c_stop(adap);
  647. else
  648. pch_i2c_repstart(adap);
  649. buf[read_index++] = ioread32(p + PCH_I2CDR); /* Read Final */
  650. count = read_index;
  651. }
  652. return count;
  653. }
  654. /**
  655. * pch_i2c_cb() - Interrupt handler Call back function
  656. * @adap: Pointer to struct i2c_algo_pch_data.
  657. */
  658. static void pch_i2c_cb(struct i2c_algo_pch_data *adap)
  659. {
  660. u32 sts;
  661. void __iomem *p = adap->pch_base_address;
  662. sts = ioread32(p + PCH_I2CSR);
  663. sts &= (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT);
  664. if (sts & I2CMAL_BIT)
  665. adap->pch_event_flag |= I2CMAL_EVENT;
  666. if (sts & I2CMCF_BIT)
  667. adap->pch_event_flag |= I2CMCF_EVENT;
  668. /* clear the applicable bits */
  669. pch_clrbit(adap->pch_base_address, PCH_I2CSR, sts);
  670. pch_dbg(adap, "PCH_I2CSR = %x\n", ioread32(p + PCH_I2CSR));
  671. wake_up(&pch_event);
  672. }
  673. /**
  674. * pch_i2c_handler() - interrupt handler for the PCH I2C controller
  675. * @irq: irq number.
  676. * @pData: cookie passed back to the handler function.
  677. */
  678. static irqreturn_t pch_i2c_handler(int irq, void *pData)
  679. {
  680. u32 reg_val;
  681. int flag;
  682. int i;
  683. struct adapter_info *adap_info = pData;
  684. void __iomem *p;
  685. u32 mode;
  686. for (i = 0, flag = 0; i < adap_info->ch_num; i++) {
  687. p = adap_info->pch_data[i].pch_base_address;
  688. mode = ioread32(p + PCH_I2CMOD);
  689. mode &= BUFFER_MODE | EEPROM_SR_MODE;
  690. if (mode != NORMAL_MODE) {
  691. pch_err(adap_info->pch_data,
  692. "I2C-%d mode(%d) is not supported\n", mode, i);
  693. continue;
  694. }
  695. reg_val = ioread32(p + PCH_I2CSR);
  696. if (reg_val & (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT)) {
  697. pch_i2c_cb(&adap_info->pch_data[i]);
  698. flag = 1;
  699. }
  700. }
  701. return flag ? IRQ_HANDLED : IRQ_NONE;
  702. }
  703. /**
  704. * pch_i2c_xfer() - Reading adnd writing data through I2C bus
  705. * @i2c_adap: Pointer to the struct i2c_adapter.
  706. * @msgs: Pointer to i2c_msg structure.
  707. * @num: number of messages.
  708. */
  709. static s32 pch_i2c_xfer(struct i2c_adapter *i2c_adap,
  710. struct i2c_msg *msgs, s32 num)
  711. {
  712. struct i2c_msg *pmsg;
  713. u32 i = 0;
  714. u32 status;
  715. s32 ret;
  716. struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
  717. ret = mutex_lock_interruptible(&pch_mutex);
  718. if (ret)
  719. return -ERESTARTSYS;
  720. if (adap->p_adapter_info->pch_i2c_suspended) {
  721. mutex_unlock(&pch_mutex);
  722. return -EBUSY;
  723. }
  724. pch_dbg(adap, "adap->p_adapter_info->pch_i2c_suspended is %d\n",
  725. adap->p_adapter_info->pch_i2c_suspended);
  726. /* transfer not completed */
  727. adap->pch_i2c_xfer_in_progress = true;
  728. for (i = 0; i < num && ret >= 0; i++) {
  729. pmsg = &msgs[i];
  730. pmsg->flags |= adap->pch_buff_mode_en;
  731. status = pmsg->flags;
  732. pch_dbg(adap,
  733. "After invoking I2C_MODE_SEL :flag= 0x%x\n", status);
  734. if ((status & (I2C_M_RD)) != false) {
  735. ret = pch_i2c_readbytes(i2c_adap, pmsg, (i + 1 == num),
  736. (i == 0));
  737. } else {
  738. ret = pch_i2c_writebytes(i2c_adap, pmsg, (i + 1 == num),
  739. (i == 0));
  740. }
  741. }
  742. adap->pch_i2c_xfer_in_progress = false; /* transfer completed */
  743. mutex_unlock(&pch_mutex);
  744. return (ret < 0) ? ret : num;
  745. }
  746. /**
  747. * pch_i2c_func() - return the functionality of the I2C driver
  748. * @adap: Pointer to struct i2c_algo_pch_data.
  749. */
  750. static u32 pch_i2c_func(struct i2c_adapter *adap)
  751. {
  752. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  753. }
  754. static struct i2c_algorithm pch_algorithm = {
  755. .master_xfer = pch_i2c_xfer,
  756. .functionality = pch_i2c_func
  757. };
  758. /**
  759. * pch_i2c_disbl_int() - Disable PCH I2C interrupts
  760. * @adap: Pointer to struct i2c_algo_pch_data.
  761. */
  762. static void pch_i2c_disbl_int(struct i2c_algo_pch_data *adap)
  763. {
  764. void __iomem *p = adap->pch_base_address;
  765. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, NORMAL_INTR_ENBL);
  766. iowrite32(EEPROM_RST_INTR_DISBL, p + PCH_I2CESRMSK);
  767. iowrite32(BUFFER_MODE_INTR_DISBL, p + PCH_I2CBUFMSK);
  768. }
  769. static int __devinit pch_i2c_probe(struct pci_dev *pdev,
  770. const struct pci_device_id *id)
  771. {
  772. void __iomem *base_addr;
  773. int ret;
  774. int i, j;
  775. struct adapter_info *adap_info;
  776. struct i2c_adapter *pch_adap;
  777. pch_pci_dbg(pdev, "Entered.\n");
  778. adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL);
  779. if (adap_info == NULL) {
  780. pch_pci_err(pdev, "Memory allocation FAILED\n");
  781. return -ENOMEM;
  782. }
  783. ret = pci_enable_device(pdev);
  784. if (ret) {
  785. pch_pci_err(pdev, "pci_enable_device FAILED\n");
  786. goto err_pci_enable;
  787. }
  788. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  789. if (ret) {
  790. pch_pci_err(pdev, "pci_request_regions FAILED\n");
  791. goto err_pci_req;
  792. }
  793. base_addr = pci_iomap(pdev, 1, 0);
  794. if (base_addr == NULL) {
  795. pch_pci_err(pdev, "pci_iomap FAILED\n");
  796. ret = -ENOMEM;
  797. goto err_pci_iomap;
  798. }
  799. /* Set the number of I2C channel instance */
  800. adap_info->ch_num = id->driver_data;
  801. ret = request_irq(pdev->irq, pch_i2c_handler, IRQF_SHARED,
  802. KBUILD_MODNAME, adap_info);
  803. if (ret) {
  804. pch_pci_err(pdev, "request_irq FAILED\n");
  805. goto err_request_irq;
  806. }
  807. for (i = 0; i < adap_info->ch_num; i++) {
  808. pch_adap = &adap_info->pch_data[i].pch_adapter;
  809. adap_info->pch_i2c_suspended = false;
  810. adap_info->pch_data[i].p_adapter_info = adap_info;
  811. pch_adap->owner = THIS_MODULE;
  812. pch_adap->class = I2C_CLASS_HWMON;
  813. strcpy(pch_adap->name, KBUILD_MODNAME);
  814. pch_adap->algo = &pch_algorithm;
  815. pch_adap->algo_data = &adap_info->pch_data[i];
  816. /* base_addr + offset; */
  817. adap_info->pch_data[i].pch_base_address = base_addr + 0x100 * i;
  818. pch_adap->dev.parent = &pdev->dev;
  819. pch_i2c_init(&adap_info->pch_data[i]);
  820. pch_adap->nr = i;
  821. ret = i2c_add_numbered_adapter(pch_adap);
  822. if (ret) {
  823. pch_pci_err(pdev, "i2c_add_adapter[ch:%d] FAILED\n", i);
  824. goto err_add_adapter;
  825. }
  826. }
  827. pci_set_drvdata(pdev, adap_info);
  828. pch_pci_dbg(pdev, "returns %d.\n", ret);
  829. return 0;
  830. err_add_adapter:
  831. for (j = 0; j < i; j++)
  832. i2c_del_adapter(&adap_info->pch_data[j].pch_adapter);
  833. free_irq(pdev->irq, adap_info);
  834. err_request_irq:
  835. pci_iounmap(pdev, base_addr);
  836. err_pci_iomap:
  837. pci_release_regions(pdev);
  838. err_pci_req:
  839. pci_disable_device(pdev);
  840. err_pci_enable:
  841. kfree(adap_info);
  842. return ret;
  843. }
  844. static void __devexit pch_i2c_remove(struct pci_dev *pdev)
  845. {
  846. int i;
  847. struct adapter_info *adap_info = pci_get_drvdata(pdev);
  848. free_irq(pdev->irq, adap_info);
  849. for (i = 0; i < adap_info->ch_num; i++) {
  850. pch_i2c_disbl_int(&adap_info->pch_data[i]);
  851. i2c_del_adapter(&adap_info->pch_data[i].pch_adapter);
  852. }
  853. if (adap_info->pch_data[0].pch_base_address)
  854. pci_iounmap(pdev, adap_info->pch_data[0].pch_base_address);
  855. for (i = 0; i < adap_info->ch_num; i++)
  856. adap_info->pch_data[i].pch_base_address = 0;
  857. pci_set_drvdata(pdev, NULL);
  858. pci_release_regions(pdev);
  859. pci_disable_device(pdev);
  860. kfree(adap_info);
  861. }
  862. #ifdef CONFIG_PM
  863. static int pch_i2c_suspend(struct pci_dev *pdev, pm_message_t state)
  864. {
  865. int ret;
  866. int i;
  867. struct adapter_info *adap_info = pci_get_drvdata(pdev);
  868. void __iomem *p = adap_info->pch_data[0].pch_base_address;
  869. adap_info->pch_i2c_suspended = true;
  870. for (i = 0; i < adap_info->ch_num; i++) {
  871. while ((adap_info->pch_data[i].pch_i2c_xfer_in_progress)) {
  872. /* Wait until all channel transfers are completed */
  873. msleep(20);
  874. }
  875. }
  876. /* Disable the i2c interrupts */
  877. for (i = 0; i < adap_info->ch_num; i++)
  878. pch_i2c_disbl_int(&adap_info->pch_data[i]);
  879. pch_pci_dbg(pdev, "I2CSR = %x I2CBUFSTA = %x I2CESRSTA = %x "
  880. "invoked function pch_i2c_disbl_int successfully\n",
  881. ioread32(p + PCH_I2CSR), ioread32(p + PCH_I2CBUFSTA),
  882. ioread32(p + PCH_I2CESRSTA));
  883. ret = pci_save_state(pdev);
  884. if (ret) {
  885. pch_pci_err(pdev, "pci_save_state\n");
  886. return ret;
  887. }
  888. pci_enable_wake(pdev, PCI_D3hot, 0);
  889. pci_disable_device(pdev);
  890. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  891. return 0;
  892. }
  893. static int pch_i2c_resume(struct pci_dev *pdev)
  894. {
  895. int i;
  896. struct adapter_info *adap_info = pci_get_drvdata(pdev);
  897. pci_set_power_state(pdev, PCI_D0);
  898. pci_restore_state(pdev);
  899. if (pci_enable_device(pdev) < 0) {
  900. pch_pci_err(pdev, "pch_i2c_resume:pci_enable_device FAILED\n");
  901. return -EIO;
  902. }
  903. pci_enable_wake(pdev, PCI_D3hot, 0);
  904. for (i = 0; i < adap_info->ch_num; i++)
  905. pch_i2c_init(&adap_info->pch_data[i]);
  906. adap_info->pch_i2c_suspended = false;
  907. return 0;
  908. }
  909. #else
  910. #define pch_i2c_suspend NULL
  911. #define pch_i2c_resume NULL
  912. #endif
  913. static struct pci_driver pch_pcidriver = {
  914. .name = KBUILD_MODNAME,
  915. .id_table = pch_pcidev_id,
  916. .probe = pch_i2c_probe,
  917. .remove = __devexit_p(pch_i2c_remove),
  918. .suspend = pch_i2c_suspend,
  919. .resume = pch_i2c_resume
  920. };
  921. static int __init pch_pci_init(void)
  922. {
  923. return pci_register_driver(&pch_pcidriver);
  924. }
  925. module_init(pch_pci_init);
  926. static void __exit pch_pci_exit(void)
  927. {
  928. pci_unregister_driver(&pch_pcidriver);
  929. }
  930. module_exit(pch_pci_exit);
  931. MODULE_DESCRIPTION("Intel EG20T PCH/LAPIS Semico ML7213/ML7223/ML7831 IOH I2C");
  932. MODULE_LICENSE("GPL");
  933. MODULE_AUTHOR("Tomoya MORINAGA. <tomoya.rohm@gmail.com>");
  934. module_param(pch_i2c_speed, int, (S_IRUSR | S_IWUSR));
  935. module_param(pch_clk, int, (S_IRUSR | S_IWUSR));