i2c-eg20t.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. #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_ESR_START 0x0001
  63. #define PCH_BUFF_START 0x1
  64. #define PCH_REPSTART 0x0004
  65. #define PCH_ACK 0x0008
  66. #define PCH_GETACK 0x0001
  67. #define CLR_REG 0x0
  68. #define I2C_RD 0x1
  69. #define I2CMCF_BIT 0x0080
  70. #define I2CMIF_BIT 0x0002
  71. #define I2CMAL_BIT 0x0010
  72. #define I2CBMFI_BIT 0x0001
  73. #define I2CBMAL_BIT 0x0002
  74. #define I2CBMNA_BIT 0x0004
  75. #define I2CBMTO_BIT 0x0008
  76. #define I2CBMIS_BIT 0x0010
  77. #define I2CESRFI_BIT 0X0001
  78. #define I2CESRTO_BIT 0x0002
  79. #define I2CESRFIIE_BIT 0x1
  80. #define I2CESRTOIE_BIT 0x2
  81. #define I2CBMDZ_BIT 0x0040
  82. #define I2CBMAG_BIT 0x0020
  83. #define I2CMBB_BIT 0x0020
  84. #define BUFFER_MODE_MASK (I2CBMFI_BIT | I2CBMAL_BIT | I2CBMNA_BIT | \
  85. I2CBMTO_BIT | I2CBMIS_BIT)
  86. #define I2C_ADDR_MSK 0xFF
  87. #define I2C_MSB_2B_MSK 0x300
  88. #define FAST_MODE_CLK 400
  89. #define FAST_MODE_EN 0x0001
  90. #define SUB_ADDR_LEN_MAX 4
  91. #define BUF_LEN_MAX 32
  92. #define PCH_BUFFER_MODE 0x1
  93. #define EEPROM_SW_RST_MODE 0x0002
  94. #define NORMAL_INTR_ENBL 0x0300
  95. #define EEPROM_RST_INTR_ENBL (I2CESRFIIE_BIT | I2CESRTOIE_BIT)
  96. #define EEPROM_RST_INTR_DISBL 0x0
  97. #define BUFFER_MODE_INTR_ENBL 0x001F
  98. #define BUFFER_MODE_INTR_DISBL 0x0
  99. #define NORMAL_MODE 0x0
  100. #define BUFFER_MODE 0x1
  101. #define EEPROM_SR_MODE 0x2
  102. #define I2C_TX_MODE 0x0010
  103. #define PCH_BUF_TX 0xFFF7
  104. #define PCH_BUF_RD 0x0008
  105. #define I2C_ERROR_MASK (I2CESRTO_EVENT | I2CBMIS_EVENT | I2CBMTO_EVENT | \
  106. I2CBMNA_EVENT | I2CBMAL_EVENT | I2CMAL_EVENT)
  107. #define I2CMAL_EVENT 0x0001
  108. #define I2CMCF_EVENT 0x0002
  109. #define I2CBMFI_EVENT 0x0004
  110. #define I2CBMAL_EVENT 0x0008
  111. #define I2CBMNA_EVENT 0x0010
  112. #define I2CBMTO_EVENT 0x0020
  113. #define I2CBMIS_EVENT 0x0040
  114. #define I2CESRFI_EVENT 0x0080
  115. #define I2CESRTO_EVENT 0x0100
  116. #define PCI_DEVICE_ID_PCH_I2C 0x8817
  117. #define pch_dbg(adap, fmt, arg...) \
  118. dev_dbg(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
  119. #define pch_err(adap, fmt, arg...) \
  120. dev_err(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
  121. #define pch_pci_err(pdev, fmt, arg...) \
  122. dev_err(&pdev->dev, "%s :" fmt, __func__, ##arg)
  123. #define pch_pci_dbg(pdev, fmt, arg...) \
  124. dev_dbg(&pdev->dev, "%s :" fmt, __func__, ##arg)
  125. /**
  126. * struct i2c_algo_pch_data - for I2C driver functionalities
  127. * @pch_adapter: stores the reference to i2c_adapter structure
  128. * @p_adapter_info: stores the reference to adapter_info structure
  129. * @pch_base_address: specifies the remapped base address
  130. * @pch_buff_mode_en: specifies if buffer mode is enabled
  131. * @pch_event_flag: specifies occurrence of interrupt events
  132. * @pch_i2c_xfer_in_progress: specifies whether the transfer is completed
  133. */
  134. struct i2c_algo_pch_data {
  135. struct i2c_adapter pch_adapter;
  136. struct adapter_info *p_adapter_info;
  137. void __iomem *pch_base_address;
  138. int pch_buff_mode_en;
  139. u32 pch_event_flag;
  140. bool pch_i2c_xfer_in_progress;
  141. };
  142. /**
  143. * struct adapter_info - This structure holds the adapter information for the
  144. PCH i2c controller
  145. * @pch_data: stores a list of i2c_algo_pch_data
  146. * @pch_i2c_suspended: specifies whether the system is suspended or not
  147. * perhaps with more lines and words.
  148. *
  149. * pch_data has as many elements as maximum I2C channels
  150. */
  151. struct adapter_info {
  152. struct i2c_algo_pch_data pch_data;
  153. bool pch_i2c_suspended;
  154. };
  155. static int pch_i2c_speed = 100; /* I2C bus speed in Kbps */
  156. static int pch_clk = 50000; /* specifies I2C clock speed in KHz */
  157. static wait_queue_head_t pch_event;
  158. static DEFINE_MUTEX(pch_mutex);
  159. static struct pci_device_id __devinitdata pch_pcidev_id[] = {
  160. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PCH_I2C)},
  161. {0,}
  162. };
  163. static irqreturn_t pch_i2c_handler(int irq, void *pData);
  164. static inline void pch_setbit(void __iomem *addr, u32 offset, u32 bitmask)
  165. {
  166. u32 val;
  167. val = ioread32(addr + offset);
  168. val |= bitmask;
  169. iowrite32(val, addr + offset);
  170. }
  171. static inline void pch_clrbit(void __iomem *addr, u32 offset, u32 bitmask)
  172. {
  173. u32 val;
  174. val = ioread32(addr + offset);
  175. val &= (~bitmask);
  176. iowrite32(val, addr + offset);
  177. }
  178. /**
  179. * pch_i2c_init() - hardware initialization of I2C module
  180. * @adap: Pointer to struct i2c_algo_pch_data.
  181. */
  182. static void pch_i2c_init(struct i2c_algo_pch_data *adap)
  183. {
  184. void __iomem *p = adap->pch_base_address;
  185. u32 pch_i2cbc;
  186. u32 pch_i2ctmr;
  187. u32 reg_value;
  188. /* reset I2C controller */
  189. iowrite32(0x01, p + PCH_I2CSRST);
  190. msleep(20);
  191. iowrite32(0x0, p + PCH_I2CSRST);
  192. /* Initialize I2C registers */
  193. iowrite32(0x21, p + PCH_I2CNF);
  194. pch_setbit(adap->pch_base_address, PCH_I2CCTL,
  195. PCH_I2CCTL_I2CMEN);
  196. if (pch_i2c_speed != 400)
  197. pch_i2c_speed = 100;
  198. reg_value = PCH_I2CCTL_I2CMEN;
  199. if (pch_i2c_speed == FAST_MODE_CLK) {
  200. reg_value |= FAST_MODE_EN;
  201. pch_dbg(adap, "Fast mode enabled\n");
  202. }
  203. if (pch_clk > PCH_MAX_CLK)
  204. pch_clk = 62500;
  205. pch_i2cbc = (pch_clk + (pch_i2c_speed * 4)) / pch_i2c_speed * 8;
  206. /* Set transfer speed in I2CBC */
  207. iowrite32(pch_i2cbc, p + PCH_I2CBC);
  208. pch_i2ctmr = (pch_clk) / 8;
  209. iowrite32(pch_i2ctmr, p + PCH_I2CTMR);
  210. reg_value |= NORMAL_INTR_ENBL; /* Enable interrupts in normal mode */
  211. iowrite32(reg_value, p + PCH_I2CCTL);
  212. pch_dbg(adap,
  213. "I2CCTL=%x pch_i2cbc=%x pch_i2ctmr=%x Enable interrupts\n",
  214. ioread32(p + PCH_I2CCTL), pch_i2cbc, pch_i2ctmr);
  215. init_waitqueue_head(&pch_event);
  216. }
  217. static inline bool ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
  218. {
  219. return cmp1.tv64 < cmp2.tv64;
  220. }
  221. /**
  222. * pch_i2c_wait_for_bus_idle() - check the status of bus.
  223. * @adap: Pointer to struct i2c_algo_pch_data.
  224. * @timeout: waiting time counter (us).
  225. */
  226. static s32 pch_i2c_wait_for_bus_idle(struct i2c_algo_pch_data *adap,
  227. s32 timeout)
  228. {
  229. void __iomem *p = adap->pch_base_address;
  230. /* MAX timeout value is timeout*1000*1000nsec */
  231. ktime_t ns_val = ktime_add_ns(ktime_get(), timeout*1000*1000);
  232. do {
  233. if ((ioread32(p + PCH_I2CSR) & I2CMBB_BIT) == 0)
  234. break;
  235. msleep(20);
  236. } while (ktime_lt(ktime_get(), ns_val));
  237. pch_dbg(adap, "I2CSR = %x\n", ioread32(p + PCH_I2CSR));
  238. if (timeout == 0) {
  239. pch_err(adap, "%s: Timeout Error.return%d\n", __func__, -ETIME);
  240. return -ETIME;
  241. }
  242. return 0;
  243. }
  244. /**
  245. * pch_i2c_start() - Generate I2C start condition in normal mode.
  246. * @adap: Pointer to struct i2c_algo_pch_data.
  247. *
  248. * Generate I2C start condition in normal mode by setting I2CCTL.I2CMSTA to 1.
  249. */
  250. static void pch_i2c_start(struct i2c_algo_pch_data *adap)
  251. {
  252. void __iomem *p = adap->pch_base_address;
  253. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  254. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
  255. }
  256. /**
  257. * pch_i2c_wait_for_xfer_complete() - initiates a wait for the tx complete event
  258. * @adap: Pointer to struct i2c_algo_pch_data.
  259. */
  260. static s32 pch_i2c_wait_for_xfer_complete(struct i2c_algo_pch_data *adap)
  261. {
  262. s32 ret;
  263. ret = wait_event_timeout(pch_event,
  264. (adap->pch_event_flag != 0), msecs_to_jiffies(50));
  265. if (ret < 0) {
  266. pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
  267. return ret;
  268. }
  269. if (ret == 0) {
  270. pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
  271. return -ETIMEDOUT;
  272. }
  273. if (adap->pch_event_flag & I2C_ERROR_MASK) {
  274. pch_err(adap, "error bits set: %x\n", adap->pch_event_flag);
  275. return -EIO;
  276. }
  277. adap->pch_event_flag = 0;
  278. return 0;
  279. }
  280. /**
  281. * pch_i2c_getack() - to confirm ACK/NACK
  282. * @adap: Pointer to struct i2c_algo_pch_data.
  283. */
  284. static s32 pch_i2c_getack(struct i2c_algo_pch_data *adap)
  285. {
  286. u32 reg_val;
  287. void __iomem *p = adap->pch_base_address;
  288. reg_val = ioread32(p + PCH_I2CSR) & PCH_GETACK;
  289. if (reg_val != 0) {
  290. pch_err(adap, "return%d\n", -EPROTO);
  291. return -EPROTO;
  292. }
  293. return 0;
  294. }
  295. /**
  296. * pch_i2c_stop() - generate stop condition in normal mode.
  297. * @adap: Pointer to struct i2c_algo_pch_data.
  298. */
  299. static void pch_i2c_stop(struct i2c_algo_pch_data *adap)
  300. {
  301. void __iomem *p = adap->pch_base_address;
  302. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  303. /* clear the start bit */
  304. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
  305. }
  306. /**
  307. * pch_i2c_repstart() - generate repeated start condition in normal mode
  308. * @adap: Pointer to struct i2c_algo_pch_data.
  309. */
  310. static void pch_i2c_repstart(struct i2c_algo_pch_data *adap)
  311. {
  312. void __iomem *p = adap->pch_base_address;
  313. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  314. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_REPSTART);
  315. }
  316. /**
  317. * pch_i2c_writebytes() - write data to I2C bus in normal mode
  318. * @i2c_adap: Pointer to the struct i2c_adapter.
  319. * @last: specifies whether last message or not.
  320. * In the case of compound mode it will be 1 for last message,
  321. * otherwise 0.
  322. * @first: specifies whether first message or not.
  323. * 1 for first message otherwise 0.
  324. */
  325. static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,
  326. struct i2c_msg *msgs, u32 last, u32 first)
  327. {
  328. struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
  329. u8 *buf;
  330. u32 length;
  331. u32 addr;
  332. u32 addr_2_msb;
  333. u32 addr_8_lsb;
  334. s32 wrcount;
  335. void __iomem *p = adap->pch_base_address;
  336. length = msgs->len;
  337. buf = msgs->buf;
  338. addr = msgs->addr;
  339. /* enable master tx */
  340. pch_setbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
  341. pch_dbg(adap, "I2CCTL = %x msgs->len = %d\n", ioread32(p + PCH_I2CCTL),
  342. length);
  343. if (first) {
  344. if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
  345. return -ETIME;
  346. }
  347. if (msgs->flags & I2C_M_TEN) {
  348. addr_2_msb = ((addr & I2C_MSB_2B_MSK) >> 7);
  349. iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
  350. if (first)
  351. pch_i2c_start(adap);
  352. if (pch_i2c_wait_for_xfer_complete(adap) == 0 &&
  353. pch_i2c_getack(adap) == 0) {
  354. addr_8_lsb = (addr & I2C_ADDR_MSK);
  355. iowrite32(addr_8_lsb, p + PCH_I2CDR);
  356. } else {
  357. pch_i2c_stop(adap);
  358. return -ETIME;
  359. }
  360. } else {
  361. /* set 7 bit slave address and R/W bit as 0 */
  362. iowrite32(addr << 1, p + PCH_I2CDR);
  363. if (first)
  364. pch_i2c_start(adap);
  365. }
  366. if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
  367. (pch_i2c_getack(adap) == 0)) {
  368. for (wrcount = 0; wrcount < length; ++wrcount) {
  369. /* write buffer value to I2C data register */
  370. iowrite32(buf[wrcount], p + PCH_I2CDR);
  371. pch_dbg(adap, "writing %x to Data register\n",
  372. buf[wrcount]);
  373. if (pch_i2c_wait_for_xfer_complete(adap) != 0)
  374. return -ETIME;
  375. if (pch_i2c_getack(adap))
  376. return -EIO;
  377. }
  378. /* check if this is the last message */
  379. if (last)
  380. pch_i2c_stop(adap);
  381. else
  382. pch_i2c_repstart(adap);
  383. } else {
  384. pch_i2c_stop(adap);
  385. return -EIO;
  386. }
  387. pch_dbg(adap, "return=%d\n", wrcount);
  388. return wrcount;
  389. }
  390. /**
  391. * pch_i2c_sendack() - send ACK
  392. * @adap: Pointer to struct i2c_algo_pch_data.
  393. */
  394. static void pch_i2c_sendack(struct i2c_algo_pch_data *adap)
  395. {
  396. void __iomem *p = adap->pch_base_address;
  397. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  398. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
  399. }
  400. /**
  401. * pch_i2c_sendnack() - send NACK
  402. * @adap: Pointer to struct i2c_algo_pch_data.
  403. */
  404. static void pch_i2c_sendnack(struct i2c_algo_pch_data *adap)
  405. {
  406. void __iomem *p = adap->pch_base_address;
  407. pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
  408. pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
  409. }
  410. /**
  411. * pch_i2c_readbytes() - read data from I2C bus in normal mode.
  412. * @i2c_adap: Pointer to the struct i2c_adapter.
  413. * @msgs: Pointer to i2c_msg structure.
  414. * @last: specifies whether last message or not.
  415. * @first: specifies whether first message or not.
  416. */
  417. s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
  418. u32 last, u32 first)
  419. {
  420. struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
  421. u8 *buf;
  422. u32 count;
  423. u32 length;
  424. u32 addr;
  425. u32 addr_2_msb;
  426. void __iomem *p = adap->pch_base_address;
  427. length = msgs->len;
  428. buf = msgs->buf;
  429. addr = msgs->addr;
  430. /* enable master reception */
  431. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
  432. if (first) {
  433. if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
  434. return -ETIME;
  435. }
  436. if (msgs->flags & I2C_M_TEN) {
  437. addr_2_msb = (((addr & I2C_MSB_2B_MSK) >> 7) | (I2C_RD));
  438. iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
  439. } else {
  440. /* 7 address bits + R/W bit */
  441. addr = (((addr) << 1) | (I2C_RD));
  442. iowrite32(addr, p + PCH_I2CDR);
  443. }
  444. /* check if it is the first message */
  445. if (first)
  446. pch_i2c_start(adap);
  447. if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
  448. (pch_i2c_getack(adap) == 0)) {
  449. pch_dbg(adap, "return %d\n", 0);
  450. if (length == 0) {
  451. pch_i2c_stop(adap);
  452. ioread32(p + PCH_I2CDR); /* Dummy read needs */
  453. count = length;
  454. } else {
  455. int read_index;
  456. int loop;
  457. pch_i2c_sendack(adap);
  458. /* Dummy read */
  459. for (loop = 1, read_index = 0; loop < length; loop++) {
  460. buf[read_index] = ioread32(p + PCH_I2CDR);
  461. if (loop != 1)
  462. read_index++;
  463. if (pch_i2c_wait_for_xfer_complete(adap) != 0) {
  464. pch_i2c_stop(adap);
  465. return -ETIME;
  466. }
  467. } /* end for */
  468. pch_i2c_sendnack(adap);
  469. buf[read_index] = ioread32(p + PCH_I2CDR);
  470. if (length != 1)
  471. read_index++;
  472. if (pch_i2c_wait_for_xfer_complete(adap) == 0) {
  473. if (last)
  474. pch_i2c_stop(adap);
  475. else
  476. pch_i2c_repstart(adap);
  477. buf[read_index++] = ioread32(p + PCH_I2CDR);
  478. count = read_index;
  479. } else {
  480. count = -ETIME;
  481. }
  482. }
  483. } else {
  484. count = -ETIME;
  485. pch_i2c_stop(adap);
  486. }
  487. return count;
  488. }
  489. /**
  490. * pch_i2c_cb_ch0() - Interrupt handler Call back function
  491. * @adap: Pointer to struct i2c_algo_pch_data.
  492. */
  493. static void pch_i2c_cb_ch0(struct i2c_algo_pch_data *adap)
  494. {
  495. u32 sts;
  496. void __iomem *p = adap->pch_base_address;
  497. sts = ioread32(p + PCH_I2CSR);
  498. sts &= (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT);
  499. if (sts & I2CMAL_BIT)
  500. adap->pch_event_flag |= I2CMAL_EVENT;
  501. if (sts & I2CMCF_BIT)
  502. adap->pch_event_flag |= I2CMCF_EVENT;
  503. /* clear the applicable bits */
  504. pch_clrbit(adap->pch_base_address, PCH_I2CSR, sts);
  505. pch_dbg(adap, "PCH_I2CSR = %x\n", ioread32(p + PCH_I2CSR));
  506. wake_up(&pch_event);
  507. }
  508. /**
  509. * pch_i2c_handler() - interrupt handler for the PCH I2C controller
  510. * @irq: irq number.
  511. * @pData: cookie passed back to the handler function.
  512. */
  513. static irqreturn_t pch_i2c_handler(int irq, void *pData)
  514. {
  515. s32 reg_val;
  516. struct i2c_algo_pch_data *adap_data = (struct i2c_algo_pch_data *)pData;
  517. void __iomem *p = adap_data->pch_base_address;
  518. u32 mode = ioread32(p + PCH_I2CMOD) & (BUFFER_MODE | EEPROM_SR_MODE);
  519. if (mode != NORMAL_MODE) {
  520. pch_err(adap_data, "I2C mode is not supported\n");
  521. return IRQ_NONE;
  522. }
  523. reg_val = ioread32(p + PCH_I2CSR);
  524. if (reg_val & (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT))
  525. pch_i2c_cb_ch0(adap_data);
  526. else
  527. return IRQ_NONE;
  528. return IRQ_HANDLED;
  529. }
  530. /**
  531. * pch_i2c_xfer() - Reading adnd writing data through I2C bus
  532. * @i2c_adap: Pointer to the struct i2c_adapter.
  533. * @msgs: Pointer to i2c_msg structure.
  534. * @num: number of messages.
  535. */
  536. static s32 pch_i2c_xfer(struct i2c_adapter *i2c_adap,
  537. struct i2c_msg *msgs, s32 num)
  538. {
  539. struct i2c_msg *pmsg;
  540. u32 i = 0;
  541. u32 status;
  542. u32 msglen;
  543. u32 subaddrlen;
  544. s32 ret;
  545. struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
  546. ret = mutex_lock_interruptible(&pch_mutex);
  547. if (ret)
  548. return -ERESTARTSYS;
  549. if (adap->p_adapter_info->pch_i2c_suspended) {
  550. mutex_unlock(&pch_mutex);
  551. return -EBUSY;
  552. }
  553. pch_dbg(adap, "adap->p_adapter_info->pch_i2c_suspended is %d\n",
  554. adap->p_adapter_info->pch_i2c_suspended);
  555. /* transfer not completed */
  556. adap->pch_i2c_xfer_in_progress = true;
  557. pmsg = &msgs[0];
  558. pmsg->flags |= adap->pch_buff_mode_en;
  559. status = pmsg->flags;
  560. pch_dbg(adap,
  561. "After invoking I2C_MODE_SEL :flag= 0x%x\n", status);
  562. /* calculate sub address length and message length */
  563. /* these are applicable only for buffer mode */
  564. subaddrlen = pmsg->buf[0];
  565. /* calculate actual message length excluding
  566. * the sub address fields */
  567. msglen = (pmsg->len) - (subaddrlen + 1);
  568. if (status & (I2C_M_RD)) {
  569. pch_dbg(adap, "invoking pch_i2c_readbytes\n");
  570. ret = pch_i2c_readbytes(i2c_adap, pmsg, (i + 1 == num),
  571. (i == 0));
  572. } else {
  573. pch_dbg(adap, "invoking pch_i2c_writebytes\n");
  574. ret = pch_i2c_writebytes(i2c_adap, pmsg, (i + 1 == num),
  575. (i == 0));
  576. }
  577. adap->pch_i2c_xfer_in_progress = false; /* transfer completed */
  578. mutex_unlock(&pch_mutex);
  579. return ret;
  580. }
  581. /**
  582. * pch_i2c_func() - return the functionality of the I2C driver
  583. * @adap: Pointer to struct i2c_algo_pch_data.
  584. */
  585. static u32 pch_i2c_func(struct i2c_adapter *adap)
  586. {
  587. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  588. }
  589. static struct i2c_algorithm pch_algorithm = {
  590. .master_xfer = pch_i2c_xfer,
  591. .functionality = pch_i2c_func
  592. };
  593. /**
  594. * pch_i2c_disbl_int() - Disable PCH I2C interrupts
  595. * @adap: Pointer to struct i2c_algo_pch_data.
  596. */
  597. static void pch_i2c_disbl_int(struct i2c_algo_pch_data *adap)
  598. {
  599. void __iomem *p = adap->pch_base_address;
  600. pch_clrbit(adap->pch_base_address, PCH_I2CCTL, NORMAL_INTR_ENBL);
  601. iowrite32(EEPROM_RST_INTR_DISBL, p + PCH_I2CESRMSK);
  602. iowrite32(BUFFER_MODE_INTR_DISBL, p + PCH_I2CBUFMSK);
  603. }
  604. static int __devinit pch_i2c_probe(struct pci_dev *pdev,
  605. const struct pci_device_id *id)
  606. {
  607. void __iomem *base_addr;
  608. s32 ret;
  609. struct adapter_info *adap_info;
  610. pch_pci_dbg(pdev, "Entered.\n");
  611. adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL);
  612. if (adap_info == NULL) {
  613. pch_pci_err(pdev, "Memory allocation FAILED\n");
  614. return -ENOMEM;
  615. }
  616. ret = pci_enable_device(pdev);
  617. if (ret) {
  618. pch_pci_err(pdev, "pci_enable_device FAILED\n");
  619. goto err_pci_enable;
  620. }
  621. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  622. if (ret) {
  623. pch_pci_err(pdev, "pci_request_regions FAILED\n");
  624. goto err_pci_req;
  625. }
  626. base_addr = pci_iomap(pdev, 1, 0);
  627. if (base_addr == NULL) {
  628. pch_pci_err(pdev, "pci_iomap FAILED\n");
  629. ret = -ENOMEM;
  630. goto err_pci_iomap;
  631. }
  632. adap_info->pch_i2c_suspended = false;
  633. adap_info->pch_data.p_adapter_info = adap_info;
  634. adap_info->pch_data.pch_adapter.owner = THIS_MODULE;
  635. adap_info->pch_data.pch_adapter.class = I2C_CLASS_HWMON;
  636. strcpy(adap_info->pch_data.pch_adapter.name, KBUILD_MODNAME);
  637. adap_info->pch_data.pch_adapter.algo = &pch_algorithm;
  638. adap_info->pch_data.pch_adapter.algo_data =
  639. &adap_info->pch_data;
  640. /* (i * 0x80) + base_addr; */
  641. adap_info->pch_data.pch_base_address = base_addr;
  642. adap_info->pch_data.pch_adapter.dev.parent = &pdev->dev;
  643. ret = i2c_add_adapter(&(adap_info->pch_data.pch_adapter));
  644. if (ret) {
  645. pch_pci_err(pdev, "i2c_add_adapter FAILED\n");
  646. goto err_i2c_add_adapter;
  647. }
  648. pch_i2c_init(&adap_info->pch_data);
  649. ret = request_irq(pdev->irq, pch_i2c_handler, IRQF_SHARED,
  650. KBUILD_MODNAME, &adap_info->pch_data);
  651. if (ret) {
  652. pch_pci_err(pdev, "request_irq FAILED\n");
  653. goto err_request_irq;
  654. }
  655. pci_set_drvdata(pdev, adap_info);
  656. pch_pci_dbg(pdev, "returns %d.\n", ret);
  657. return 0;
  658. err_request_irq:
  659. i2c_del_adapter(&(adap_info->pch_data.pch_adapter));
  660. err_i2c_add_adapter:
  661. pci_iounmap(pdev, base_addr);
  662. err_pci_iomap:
  663. pci_release_regions(pdev);
  664. err_pci_req:
  665. pci_disable_device(pdev);
  666. err_pci_enable:
  667. kfree(adap_info);
  668. return ret;
  669. }
  670. static void __devexit pch_i2c_remove(struct pci_dev *pdev)
  671. {
  672. struct adapter_info *adap_info = pci_get_drvdata(pdev);
  673. pch_i2c_disbl_int(&adap_info->pch_data);
  674. free_irq(pdev->irq, &adap_info->pch_data);
  675. i2c_del_adapter(&(adap_info->pch_data.pch_adapter));
  676. if (adap_info->pch_data.pch_base_address) {
  677. pci_iounmap(pdev, adap_info->pch_data.pch_base_address);
  678. adap_info->pch_data.pch_base_address = 0;
  679. }
  680. pci_set_drvdata(pdev, NULL);
  681. pci_release_regions(pdev);
  682. pci_disable_device(pdev);
  683. kfree(adap_info);
  684. }
  685. #ifdef CONFIG_PM
  686. static int pch_i2c_suspend(struct pci_dev *pdev, pm_message_t state)
  687. {
  688. int ret;
  689. struct adapter_info *adap_info = pci_get_drvdata(pdev);
  690. void __iomem *p = adap_info->pch_data.pch_base_address;
  691. adap_info->pch_i2c_suspended = true;
  692. while ((adap_info->pch_data.pch_i2c_xfer_in_progress)) {
  693. /* Wait until all channel transfers are completed */
  694. msleep(20);
  695. }
  696. /* Disable the i2c interrupts */
  697. pch_i2c_disbl_int(&adap_info->pch_data);
  698. pch_pci_dbg(pdev, "I2CSR = %x I2CBUFSTA = %x I2CESRSTA = %x "
  699. "invoked function pch_i2c_disbl_int successfully\n",
  700. ioread32(p + PCH_I2CSR), ioread32(p + PCH_I2CBUFSTA),
  701. ioread32(p + PCH_I2CESRSTA));
  702. ret = pci_save_state(pdev);
  703. if (ret) {
  704. pch_pci_err(pdev, "pci_save_state\n");
  705. return ret;
  706. }
  707. pci_enable_wake(pdev, PCI_D3hot, 0);
  708. pci_disable_device(pdev);
  709. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  710. return 0;
  711. }
  712. static int pch_i2c_resume(struct pci_dev *pdev)
  713. {
  714. struct adapter_info *adap_info = pci_get_drvdata(pdev);
  715. pci_set_power_state(pdev, PCI_D0);
  716. pci_restore_state(pdev);
  717. if (pci_enable_device(pdev) < 0) {
  718. pch_pci_err(pdev, "pch_i2c_resume:pci_enable_device FAILED\n");
  719. return -EIO;
  720. }
  721. pci_enable_wake(pdev, PCI_D3hot, 0);
  722. pch_i2c_init(&adap_info->pch_data);
  723. adap_info->pch_i2c_suspended = false;
  724. return 0;
  725. }
  726. #else
  727. #define pch_i2c_suspend NULL
  728. #define pch_i2c_resume NULL
  729. #endif
  730. static struct pci_driver pch_pcidriver = {
  731. .name = KBUILD_MODNAME,
  732. .id_table = pch_pcidev_id,
  733. .probe = pch_i2c_probe,
  734. .remove = __devexit_p(pch_i2c_remove),
  735. .suspend = pch_i2c_suspend,
  736. .resume = pch_i2c_resume
  737. };
  738. static int __init pch_pci_init(void)
  739. {
  740. return pci_register_driver(&pch_pcidriver);
  741. }
  742. module_init(pch_pci_init);
  743. static void __exit pch_pci_exit(void)
  744. {
  745. pci_unregister_driver(&pch_pcidriver);
  746. }
  747. module_exit(pch_pci_exit);
  748. MODULE_DESCRIPTION("PCH I2C PCI Driver");
  749. MODULE_LICENSE("GPL");
  750. MODULE_AUTHOR("Tomoya MORINAGA. <tomoya-linux@dsn.okisemi.com>");
  751. module_param(pch_i2c_speed, int, (S_IRUSR | S_IWUSR));
  752. module_param(pch_clk, int, (S_IRUSR | S_IWUSR));