i2c-eg20t.c 25 KB

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