i2c-eg20t.c 28 KB

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