i2c-intel-mid.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * Support for Moorestown/Medfield I2C chip
  3. *
  4. * Copyright (c) 2009 Intel Corporation.
  5. * Copyright (c) 2009 Synopsys. Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License, version
  9. * 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. * details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/stat.h>
  26. #include <linux/delay.h>
  27. #include <linux/i2c.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/io.h>
  33. #define DRIVER_NAME "i2c-intel-mid"
  34. #define VERSION "Version 0.5ac2"
  35. #define PLATFORM "Moorestown/Medfield"
  36. /* Tables use: 0 Moorestown, 1 Medfield */
  37. #define NUM_PLATFORMS 2
  38. enum platform_enum {
  39. MOORESTOWN = 0,
  40. MEDFIELD = 1,
  41. };
  42. enum mid_i2c_status {
  43. STATUS_IDLE = 0,
  44. STATUS_READ_START,
  45. STATUS_READ_IN_PROGRESS,
  46. STATUS_READ_SUCCESS,
  47. STATUS_WRITE_START,
  48. STATUS_WRITE_SUCCESS,
  49. STATUS_XFER_ABORT,
  50. STATUS_STANDBY
  51. };
  52. /**
  53. * struct intel_mid_i2c_private - per device I²C context
  54. * @adap: core i2c layer adapter information
  55. * @dev: device reference for power management
  56. * @base: register base
  57. * @speed: speed mode for this port
  58. * @complete: completion object for transaction wait
  59. * @abort: reason for last abort
  60. * @rx_buf: pointer into working receive buffer
  61. * @rx_buf_len: receive buffer length
  62. * @status: adapter state machine
  63. * @msg: the message we are currently processing
  64. * @platform: the MID device type we are part of
  65. * @lock: transaction serialization
  66. *
  67. * We allocate one of these per device we discover, it holds the core
  68. * i2c layer objects and the data we need to track privately.
  69. */
  70. struct intel_mid_i2c_private {
  71. struct i2c_adapter adap;
  72. struct device *dev;
  73. void __iomem *base;
  74. int speed;
  75. struct completion complete;
  76. int abort;
  77. u8 *rx_buf;
  78. int rx_buf_len;
  79. enum mid_i2c_status status;
  80. struct i2c_msg *msg;
  81. enum platform_enum platform;
  82. struct mutex lock;
  83. };
  84. #define NUM_SPEEDS 3
  85. #define ACTIVE 0
  86. #define STANDBY 1
  87. /* Control register */
  88. #define IC_CON 0x00
  89. #define SLV_DIS (1 << 6) /* Disable slave mode */
  90. #define RESTART (1 << 5) /* Send a Restart condition */
  91. #define ADDR_10BIT (1 << 4) /* 10-bit addressing */
  92. #define STANDARD_MODE (1 << 1) /* standard mode */
  93. #define FAST_MODE (2 << 1) /* fast mode */
  94. #define HIGH_MODE (3 << 1) /* high speed mode */
  95. #define MASTER_EN (1 << 0) /* Master mode */
  96. /* Target address register */
  97. #define IC_TAR 0x04
  98. #define IC_TAR_10BIT_ADDR (1 << 12) /* 10-bit addressing */
  99. #define IC_TAR_SPECIAL (1 << 11) /* Perform special I2C cmd */
  100. #define IC_TAR_GC_OR_START (1 << 10) /* 0: Gerneral Call Address */
  101. /* 1: START BYTE */
  102. /* Slave Address Register */
  103. #define IC_SAR 0x08 /* Not used in Master mode */
  104. /* High Speed Master Mode Code Address Register */
  105. #define IC_HS_MADDR 0x0c
  106. /* Rx/Tx Data Buffer and Command Register */
  107. #define IC_DATA_CMD 0x10
  108. #define IC_RD (1 << 8) /* 1: Read 0: Write */
  109. /* Standard Speed Clock SCL High Count Register */
  110. #define IC_SS_SCL_HCNT 0x14
  111. /* Standard Speed Clock SCL Low Count Register */
  112. #define IC_SS_SCL_LCNT 0x18
  113. /* Fast Speed Clock SCL High Count Register */
  114. #define IC_FS_SCL_HCNT 0x1c
  115. /* Fast Spedd Clock SCL Low Count Register */
  116. #define IC_FS_SCL_LCNT 0x20
  117. /* High Speed Clock SCL High Count Register */
  118. #define IC_HS_SCL_HCNT 0x24
  119. /* High Speed Clock SCL Low Count Register */
  120. #define IC_HS_SCL_LCNT 0x28
  121. /* Interrupt Status Register */
  122. #define IC_INTR_STAT 0x2c /* Read only */
  123. #define R_GEN_CALL (1 << 11)
  124. #define R_START_DET (1 << 10)
  125. #define R_STOP_DET (1 << 9)
  126. #define R_ACTIVITY (1 << 8)
  127. #define R_RX_DONE (1 << 7)
  128. #define R_TX_ABRT (1 << 6)
  129. #define R_RD_REQ (1 << 5)
  130. #define R_TX_EMPTY (1 << 4)
  131. #define R_TX_OVER (1 << 3)
  132. #define R_RX_FULL (1 << 2)
  133. #define R_RX_OVER (1 << 1)
  134. #define R_RX_UNDER (1 << 0)
  135. /* Interrupt Mask Register */
  136. #define IC_INTR_MASK 0x30 /* Read and Write */
  137. #define M_GEN_CALL (1 << 11)
  138. #define M_START_DET (1 << 10)
  139. #define M_STOP_DET (1 << 9)
  140. #define M_ACTIVITY (1 << 8)
  141. #define M_RX_DONE (1 << 7)
  142. #define M_TX_ABRT (1 << 6)
  143. #define M_RD_REQ (1 << 5)
  144. #define M_TX_EMPTY (1 << 4)
  145. #define M_TX_OVER (1 << 3)
  146. #define M_RX_FULL (1 << 2)
  147. #define M_RX_OVER (1 << 1)
  148. #define M_RX_UNDER (1 << 0)
  149. /* Raw Interrupt Status Register */
  150. #define IC_RAW_INTR_STAT 0x34 /* Read Only */
  151. #define GEN_CALL (1 << 11) /* General call */
  152. #define START_DET (1 << 10) /* (RE)START occurred */
  153. #define STOP_DET (1 << 9) /* STOP occurred */
  154. #define ACTIVITY (1 << 8) /* Bus busy */
  155. #define RX_DONE (1 << 7) /* Not used in Master mode */
  156. #define TX_ABRT (1 << 6) /* Transmit Abort */
  157. #define RD_REQ (1 << 5) /* Not used in Master mode */
  158. #define TX_EMPTY (1 << 4) /* TX FIFO <= threshold */
  159. #define TX_OVER (1 << 3) /* TX FIFO overflow */
  160. #define RX_FULL (1 << 2) /* RX FIFO >= threshold */
  161. #define RX_OVER (1 << 1) /* RX FIFO overflow */
  162. #define RX_UNDER (1 << 0) /* RX FIFO empty */
  163. /* Receive FIFO Threshold Register */
  164. #define IC_RX_TL 0x38
  165. /* Transmit FIFO Treshold Register */
  166. #define IC_TX_TL 0x3c
  167. /* Clear Combined and Individual Interrupt Register */
  168. #define IC_CLR_INTR 0x40
  169. #define CLR_INTR (1 << 0)
  170. /* Clear RX_UNDER Interrupt Register */
  171. #define IC_CLR_RX_UNDER 0x44
  172. #define CLR_RX_UNDER (1 << 0)
  173. /* Clear RX_OVER Interrupt Register */
  174. #define IC_CLR_RX_OVER 0x48
  175. #define CLR_RX_OVER (1 << 0)
  176. /* Clear TX_OVER Interrupt Register */
  177. #define IC_CLR_TX_OVER 0x4c
  178. #define CLR_TX_OVER (1 << 0)
  179. #define IC_CLR_RD_REQ 0x50
  180. /* Clear TX_ABRT Interrupt Register */
  181. #define IC_CLR_TX_ABRT 0x54
  182. #define CLR_TX_ABRT (1 << 0)
  183. #define IC_CLR_RX_DONE 0x58
  184. /* Clear ACTIVITY Interrupt Register */
  185. #define IC_CLR_ACTIVITY 0x5c
  186. #define CLR_ACTIVITY (1 << 0)
  187. /* Clear STOP_DET Interrupt Register */
  188. #define IC_CLR_STOP_DET 0x60
  189. #define CLR_STOP_DET (1 << 0)
  190. /* Clear START_DET Interrupt Register */
  191. #define IC_CLR_START_DET 0x64
  192. #define CLR_START_DET (1 << 0)
  193. /* Clear GEN_CALL Interrupt Register */
  194. #define IC_CLR_GEN_CALL 0x68
  195. #define CLR_GEN_CALL (1 << 0)
  196. /* Enable Register */
  197. #define IC_ENABLE 0x6c
  198. #define ENABLE (1 << 0)
  199. /* Status Register */
  200. #define IC_STATUS 0x70 /* Read Only */
  201. #define STAT_SLV_ACTIVITY (1 << 6) /* Slave not in idle */
  202. #define STAT_MST_ACTIVITY (1 << 5) /* Master not in idle */
  203. #define STAT_RFF (1 << 4) /* RX FIFO Full */
  204. #define STAT_RFNE (1 << 3) /* RX FIFO Not Empty */
  205. #define STAT_TFE (1 << 2) /* TX FIFO Empty */
  206. #define STAT_TFNF (1 << 1) /* TX FIFO Not Full */
  207. #define STAT_ACTIVITY (1 << 0) /* Activity Status */
  208. /* Transmit FIFO Level Register */
  209. #define IC_TXFLR 0x74 /* Read Only */
  210. #define TXFLR (1 << 0) /* TX FIFO level */
  211. /* Receive FIFO Level Register */
  212. #define IC_RXFLR 0x78 /* Read Only */
  213. #define RXFLR (1 << 0) /* RX FIFO level */
  214. /* Transmit Abort Source Register */
  215. #define IC_TX_ABRT_SOURCE 0x80
  216. #define ABRT_SLVRD_INTX (1 << 15)
  217. #define ABRT_SLV_ARBLOST (1 << 14)
  218. #define ABRT_SLVFLUSH_TXFIFO (1 << 13)
  219. #define ARB_LOST (1 << 12)
  220. #define ABRT_MASTER_DIS (1 << 11)
  221. #define ABRT_10B_RD_NORSTRT (1 << 10)
  222. #define ABRT_SBYTE_NORSTRT (1 << 9)
  223. #define ABRT_HS_NORSTRT (1 << 8)
  224. #define ABRT_SBYTE_ACKDET (1 << 7)
  225. #define ABRT_HS_ACKDET (1 << 6)
  226. #define ABRT_GCALL_READ (1 << 5)
  227. #define ABRT_GCALL_NOACK (1 << 4)
  228. #define ABRT_TXDATA_NOACK (1 << 3)
  229. #define ABRT_10ADDR2_NOACK (1 << 2)
  230. #define ABRT_10ADDR1_NOACK (1 << 1)
  231. #define ABRT_7B_ADDR_NOACK (1 << 0)
  232. /* Enable Status Register */
  233. #define IC_ENABLE_STATUS 0x9c
  234. #define IC_EN (1 << 0) /* I2C in an enabled state */
  235. /* Component Parameter Register 1*/
  236. #define IC_COMP_PARAM_1 0xf4
  237. #define APB_DATA_WIDTH (0x3 << 0)
  238. /* added by xiaolin --begin */
  239. #define SS_MIN_SCL_HIGH 4000
  240. #define SS_MIN_SCL_LOW 4700
  241. #define FS_MIN_SCL_HIGH 600
  242. #define FS_MIN_SCL_LOW 1300
  243. #define HS_MIN_SCL_HIGH_100PF 60
  244. #define HS_MIN_SCL_LOW_100PF 120
  245. #define STANDARD 0
  246. #define FAST 1
  247. #define HIGH 2
  248. #define NUM_SPEEDS 3
  249. static int speed_mode[6] = {
  250. FAST,
  251. FAST,
  252. FAST,
  253. STANDARD,
  254. FAST,
  255. FAST
  256. };
  257. static int ctl_num = 6;
  258. module_param_array(speed_mode, int, &ctl_num, S_IRUGO);
  259. MODULE_PARM_DESC(speed_mode, "Set the speed of the i2c interface (0-2)");
  260. /**
  261. * intel_mid_i2c_disable - Disable I2C controller
  262. * @adap: struct pointer to i2c_adapter
  263. *
  264. * Return Value:
  265. * 0 success
  266. * -EBUSY if device is busy
  267. * -ETIMEDOUT if i2c cannot be disabled within the given time
  268. *
  269. * I2C bus state should be checked prior to disabling the hardware. If bus is
  270. * not in idle state, an errno is returned. Write "0" to IC_ENABLE to disable
  271. * I2C controller.
  272. */
  273. static int intel_mid_i2c_disable(struct i2c_adapter *adap)
  274. {
  275. struct intel_mid_i2c_private *i2c = i2c_get_adapdata(adap);
  276. int err = 0;
  277. int count = 0;
  278. int ret1, ret2;
  279. static const u16 delay[NUM_SPEEDS] = {100, 25, 3};
  280. /* Set IC_ENABLE to 0 */
  281. writel(0, i2c->base + IC_ENABLE);
  282. /* Check if device is busy */
  283. dev_dbg(&adap->dev, "mrst i2c disable\n");
  284. while ((ret1 = readl(i2c->base + IC_ENABLE_STATUS) & 0x1)
  285. || (ret2 = readl(i2c->base + IC_STATUS) & 0x1)) {
  286. udelay(delay[i2c->speed]);
  287. writel(0, i2c->base + IC_ENABLE);
  288. dev_dbg(&adap->dev, "i2c is busy, count is %d speed %d\n",
  289. count, i2c->speed);
  290. if (count++ > 10) {
  291. err = -ETIMEDOUT;
  292. break;
  293. }
  294. }
  295. /* Clear all interrupts */
  296. readl(i2c->base + IC_CLR_INTR);
  297. readl(i2c->base + IC_CLR_STOP_DET);
  298. readl(i2c->base + IC_CLR_START_DET);
  299. readl(i2c->base + IC_CLR_ACTIVITY);
  300. readl(i2c->base + IC_CLR_TX_ABRT);
  301. readl(i2c->base + IC_CLR_RX_OVER);
  302. readl(i2c->base + IC_CLR_RX_UNDER);
  303. readl(i2c->base + IC_CLR_TX_OVER);
  304. readl(i2c->base + IC_CLR_RX_DONE);
  305. readl(i2c->base + IC_CLR_GEN_CALL);
  306. /* Disable all interupts */
  307. writel(0x0000, i2c->base + IC_INTR_MASK);
  308. return err;
  309. }
  310. /**
  311. * intel_mid_i2c_hwinit - Initialize the I2C hardware registers
  312. * @dev: pci device struct pointer
  313. *
  314. * This function will be called in intel_mid_i2c_probe() before device
  315. * registration.
  316. *
  317. * Return Values:
  318. * 0 success
  319. * -EBUSY i2c cannot be disabled
  320. * -ETIMEDOUT i2c cannot be disabled
  321. * -EFAULT If APB data width is not 32-bit wide
  322. *
  323. * I2C should be disabled prior to other register operation. If failed, an
  324. * errno is returned. Mask and Clear all interrpts, this should be done at
  325. * first. Set common registers which will not be modified during normal
  326. * transfers, including: control register, FIFO threshold and clock freq.
  327. * Check APB data width at last.
  328. */
  329. static int intel_mid_i2c_hwinit(struct intel_mid_i2c_private *i2c)
  330. {
  331. int err;
  332. static const u16 hcnt[NUM_PLATFORMS][NUM_SPEEDS] = {
  333. { 0x75, 0x15, 0x07 },
  334. { 0x04c, 0x10, 0x06 }
  335. };
  336. static const u16 lcnt[NUM_PLATFORMS][NUM_SPEEDS] = {
  337. { 0x7C, 0x21, 0x0E },
  338. { 0x053, 0x19, 0x0F }
  339. };
  340. /* Disable i2c first */
  341. err = intel_mid_i2c_disable(&i2c->adap);
  342. if (err)
  343. return err;
  344. /*
  345. * Setup clock frequency and speed mode
  346. * Enable restart condition,
  347. * enable master FSM, disable slave FSM,
  348. * use target address when initiating transfer
  349. */
  350. writel((i2c->speed + 1) << 1 | SLV_DIS | RESTART | MASTER_EN,
  351. i2c->base + IC_CON);
  352. writel(hcnt[i2c->platform][i2c->speed],
  353. i2c->base + (IC_SS_SCL_HCNT + (i2c->speed << 3)));
  354. writel(lcnt[i2c->platform][i2c->speed],
  355. i2c->base + (IC_SS_SCL_LCNT + (i2c->speed << 3)));
  356. /* Set tranmit & receive FIFO threshold to zero */
  357. writel(0x0, i2c->base + IC_RX_TL);
  358. writel(0x0, i2c->base + IC_TX_TL);
  359. return 0;
  360. }
  361. /**
  362. * intel_mid_i2c_func - Return the supported three I2C operations.
  363. * @adapter: i2c_adapter struct pointer
  364. */
  365. static u32 intel_mid_i2c_func(struct i2c_adapter *adapter)
  366. {
  367. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
  368. }
  369. /**
  370. * intel_mid_i2c_address_neq - To check if the addresses for different i2c messages
  371. * are equal.
  372. * @p1: first i2c_msg
  373. * @p2: second i2c_msg
  374. *
  375. * Return Values:
  376. * 0 if addresses are equal
  377. * 1 if not equal
  378. *
  379. * Within a single transfer, the I2C client may need to send its address more
  380. * than once. So a check if the addresses match is needed.
  381. */
  382. static inline bool intel_mid_i2c_address_neq(const struct i2c_msg *p1,
  383. const struct i2c_msg *p2)
  384. {
  385. if (p1->addr != p2->addr)
  386. return 1;
  387. if ((p1->flags ^ p2->flags) & I2C_M_TEN)
  388. return 1;
  389. return 0;
  390. }
  391. /**
  392. * intel_mid_i2c_abort - To handle transfer abortions and print error messages.
  393. * @adap: i2c_adapter struct pointer
  394. *
  395. * By reading register IC_TX_ABRT_SOURCE, various transfer errors can be
  396. * distingushed. At present, no circumstances have been found out that
  397. * multiple errors would be occurred simutaneously, so we simply use the
  398. * register value directly.
  399. *
  400. * At last the error bits are cleared. (Note clear ABRT_SBYTE_NORSTRT bit need
  401. * a few extra steps)
  402. */
  403. static void intel_mid_i2c_abort(struct intel_mid_i2c_private *i2c)
  404. {
  405. /* Read about source register */
  406. int abort = i2c->abort;
  407. struct i2c_adapter *adap = &i2c->adap;
  408. /* Single transfer error check:
  409. * According to databook, TX/RX FIFOs would be flushed when
  410. * the abort interrupt occurred.
  411. */
  412. if (abort & ABRT_MASTER_DIS)
  413. dev_err(&adap->dev,
  414. "initiate master operation with master mode disabled.\n");
  415. if (abort & ABRT_10B_RD_NORSTRT)
  416. dev_err(&adap->dev,
  417. "RESTART disabled and master sent READ cmd in 10-bit addressing.\n");
  418. if (abort & ABRT_SBYTE_NORSTRT) {
  419. dev_err(&adap->dev,
  420. "RESTART disabled and user is trying to send START byte.\n");
  421. writel(~ABRT_SBYTE_NORSTRT, i2c->base + IC_TX_ABRT_SOURCE);
  422. writel(RESTART, i2c->base + IC_CON);
  423. writel(~IC_TAR_SPECIAL, i2c->base + IC_TAR);
  424. }
  425. if (abort & ABRT_SBYTE_ACKDET)
  426. dev_err(&adap->dev,
  427. "START byte was not acknowledged.\n");
  428. if (abort & ABRT_TXDATA_NOACK)
  429. dev_dbg(&adap->dev,
  430. "No acknowledgement received from slave.\n");
  431. if (abort & ABRT_10ADDR2_NOACK)
  432. dev_dbg(&adap->dev,
  433. "The 2nd address byte of the 10-bit address was not acknowledged.\n");
  434. if (abort & ABRT_10ADDR1_NOACK)
  435. dev_dbg(&adap->dev,
  436. "The 1st address byte of 10-bit address was not acknowledged.\n");
  437. if (abort & ABRT_7B_ADDR_NOACK)
  438. dev_dbg(&adap->dev,
  439. "I2C slave device not acknowledged.\n");
  440. /* Clear TX_ABRT bit */
  441. readl(i2c->base + IC_CLR_TX_ABRT);
  442. i2c->status = STATUS_XFER_ABORT;
  443. }
  444. /**
  445. * xfer_read - Internal function to implement master read transfer.
  446. * @adap: i2c_adapter struct pointer
  447. * @buf: buffer in i2c_msg
  448. * @length: number of bytes to be read
  449. *
  450. * Return Values:
  451. * 0 if the read transfer succeeds
  452. * -ETIMEDOUT if cannot read the "raw" interrupt register
  453. * -EINVAL if a transfer abort occurred
  454. *
  455. * For every byte, a "READ" command will be loaded into IC_DATA_CMD prior to
  456. * data transfer. The actual "read" operation will be performed if an RX_FULL
  457. * interrupt occurred.
  458. *
  459. * Note there may be two interrupt signals captured, one should read
  460. * IC_RAW_INTR_STAT to separate between errors and actual data.
  461. */
  462. static int xfer_read(struct i2c_adapter *adap, unsigned char *buf, int length)
  463. {
  464. struct intel_mid_i2c_private *i2c = i2c_get_adapdata(adap);
  465. int i = length;
  466. int err;
  467. if (length >= 256) {
  468. dev_err(&adap->dev,
  469. "I2C FIFO cannot support larger than 256 bytes\n");
  470. return -EMSGSIZE;
  471. }
  472. INIT_COMPLETION(i2c->complete);
  473. readl(i2c->base + IC_CLR_INTR);
  474. writel(0x0044, i2c->base + IC_INTR_MASK);
  475. i2c->status = STATUS_READ_START;
  476. while (i--)
  477. writel(IC_RD, i2c->base + IC_DATA_CMD);
  478. i2c->status = STATUS_READ_START;
  479. err = wait_for_completion_interruptible_timeout(&i2c->complete, HZ);
  480. if (!err) {
  481. dev_err(&adap->dev, "Timeout for ACK from I2C slave device\n");
  482. intel_mid_i2c_hwinit(i2c);
  483. return -ETIMEDOUT;
  484. }
  485. if (i2c->status == STATUS_READ_SUCCESS)
  486. return 0;
  487. else
  488. return -EIO;
  489. }
  490. /**
  491. * xfer_write - Internal function to implement master write transfer.
  492. * @adap: i2c_adapter struct pointer
  493. * @buf: buffer in i2c_msg
  494. * @length: number of bytes to be read
  495. *
  496. * Return Values:
  497. * 0 if the read transfer succeeds
  498. * -ETIMEDOUT if we cannot read the "raw" interrupt register
  499. * -EINVAL if a transfer abort occurred
  500. *
  501. * For every byte, a "WRITE" command will be loaded into IC_DATA_CMD prior to
  502. * data transfer. The actual "write" operation will be performed when the
  503. * RX_FULL interrupt signal occurs.
  504. *
  505. * Note there may be two interrupt signals captured, one should read
  506. * IC_RAW_INTR_STAT to separate between errors and actual data.
  507. */
  508. static int xfer_write(struct i2c_adapter *adap,
  509. unsigned char *buf, int length)
  510. {
  511. struct intel_mid_i2c_private *i2c = i2c_get_adapdata(adap);
  512. int i, err;
  513. if (length >= 256) {
  514. dev_err(&adap->dev,
  515. "I2C FIFO cannot support larger than 256 bytes\n");
  516. return -EMSGSIZE;
  517. }
  518. INIT_COMPLETION(i2c->complete);
  519. readl(i2c->base + IC_CLR_INTR);
  520. writel(0x0050, i2c->base + IC_INTR_MASK);
  521. i2c->status = STATUS_WRITE_START;
  522. for (i = 0; i < length; i++)
  523. writel((u16)(*(buf + i)), i2c->base + IC_DATA_CMD);
  524. i2c->status = STATUS_WRITE_START;
  525. err = wait_for_completion_interruptible_timeout(&i2c->complete, HZ);
  526. if (!err) {
  527. dev_err(&adap->dev, "Timeout for ACK from I2C slave device\n");
  528. intel_mid_i2c_hwinit(i2c);
  529. return -ETIMEDOUT;
  530. } else {
  531. if (i2c->status == STATUS_WRITE_SUCCESS)
  532. return 0;
  533. else
  534. return -EIO;
  535. }
  536. }
  537. static int intel_mid_i2c_setup(struct i2c_adapter *adap, struct i2c_msg *pmsg)
  538. {
  539. struct intel_mid_i2c_private *i2c = i2c_get_adapdata(adap);
  540. int err;
  541. u32 reg;
  542. u32 bit_mask;
  543. u32 mode;
  544. /* Disable device first */
  545. err = intel_mid_i2c_disable(adap);
  546. if (err) {
  547. dev_err(&adap->dev,
  548. "Cannot disable i2c controller, timeout\n");
  549. return err;
  550. }
  551. mode = (1 + i2c->speed) << 1;
  552. /* set the speed mode */
  553. reg = readl(i2c->base + IC_CON);
  554. if ((reg & 0x06) != mode) {
  555. dev_dbg(&adap->dev, "set mode %d\n", i2c->speed);
  556. writel((reg & ~0x6) | mode, i2c->base + IC_CON);
  557. }
  558. reg = readl(i2c->base + IC_CON);
  559. /* use 7-bit addressing */
  560. if (pmsg->flags & I2C_M_TEN) {
  561. if ((reg & ADDR_10BIT) != ADDR_10BIT) {
  562. dev_dbg(&adap->dev, "set i2c 10 bit address mode\n");
  563. writel(reg | ADDR_10BIT, i2c->base + IC_CON);
  564. }
  565. } else {
  566. if ((reg & ADDR_10BIT) != 0x0) {
  567. dev_dbg(&adap->dev, "set i2c 7 bit address mode\n");
  568. writel(reg & ~ADDR_10BIT, i2c->base + IC_CON);
  569. }
  570. }
  571. /* enable restart conditions */
  572. reg = readl(i2c->base + IC_CON);
  573. if ((reg & RESTART) != RESTART) {
  574. dev_dbg(&adap->dev, "enable restart conditions\n");
  575. writel(reg | RESTART, i2c->base + IC_CON);
  576. }
  577. /* enable master FSM */
  578. reg = readl(i2c->base + IC_CON);
  579. dev_dbg(&adap->dev, "ic_con reg is 0x%x\n", reg);
  580. writel(reg | MASTER_EN, i2c->base + IC_CON);
  581. if ((reg & SLV_DIS) != SLV_DIS) {
  582. dev_dbg(&adap->dev, "enable master FSM\n");
  583. writel(reg | SLV_DIS, i2c->base + IC_CON);
  584. dev_dbg(&adap->dev, "ic_con reg is 0x%x\n", reg);
  585. }
  586. /* use target address when initiating transfer */
  587. reg = readl(i2c->base + IC_TAR);
  588. bit_mask = IC_TAR_SPECIAL | IC_TAR_GC_OR_START;
  589. if ((reg & bit_mask) != 0x0) {
  590. dev_dbg(&adap->dev,
  591. "WR: use target address when intiating transfer, i2c_tx_target\n");
  592. writel(reg & ~bit_mask, i2c->base + IC_TAR);
  593. }
  594. /* set target address to the I2C slave address */
  595. dev_dbg(&adap->dev,
  596. "set target address to the I2C slave address, addr is %x\n",
  597. pmsg->addr);
  598. writel(pmsg->addr | (pmsg->flags & I2C_M_TEN ? IC_TAR_10BIT_ADDR : 0),
  599. i2c->base + IC_TAR);
  600. /* Enable I2C controller */
  601. writel(ENABLE, i2c->base + IC_ENABLE);
  602. return 0;
  603. }
  604. /**
  605. * intel_mid_i2c_xfer - Main master transfer routine.
  606. * @adap: i2c_adapter struct pointer
  607. * @pmsg: i2c_msg struct pointer
  608. * @num: number of i2c_msg
  609. *
  610. * Return Values:
  611. * + number of messages transferred
  612. * -ETIMEDOUT If cannot disable I2C controller or read IC_STATUS
  613. * -EINVAL If the address in i2c_msg is invalid
  614. *
  615. * This function will be registered in i2c-core and exposed to external
  616. * I2C clients.
  617. * 1. Disable I2C controller
  618. * 2. Unmask three interrupts: RX_FULL, TX_EMPTY, TX_ABRT
  619. * 3. Check if address in i2c_msg is valid
  620. * 4. Enable I2C controller
  621. * 5. Perform real transfer (call xfer_read or xfer_write)
  622. * 6. Wait until the current transfer is finished (check bus state)
  623. * 7. Mask and clear all interrupts
  624. */
  625. static int intel_mid_i2c_xfer(struct i2c_adapter *adap,
  626. struct i2c_msg *pmsg,
  627. int num)
  628. {
  629. struct intel_mid_i2c_private *i2c = i2c_get_adapdata(adap);
  630. int i, err = 0;
  631. /* if number of messages equal 0*/
  632. if (num == 0)
  633. return 0;
  634. pm_runtime_get(i2c->dev);
  635. mutex_lock(&i2c->lock);
  636. dev_dbg(&adap->dev, "intel_mid_i2c_xfer, process %d msg(s)\n", num);
  637. dev_dbg(&adap->dev, "slave address is %x\n", pmsg->addr);
  638. if (i2c->status != STATUS_IDLE) {
  639. dev_err(&adap->dev, "Adapter %d in transfer/standby\n",
  640. adap->nr);
  641. mutex_unlock(&i2c->lock);
  642. pm_runtime_put(i2c->dev);
  643. return -1;
  644. }
  645. for (i = 1; i < num; i++) {
  646. /* Message address equal? */
  647. if (unlikely(intel_mid_i2c_address_neq(&pmsg[0], &pmsg[i]))) {
  648. dev_err(&adap->dev, "Invalid address in msg[%d]\n", i);
  649. mutex_unlock(&i2c->lock);
  650. pm_runtime_put(i2c->dev);
  651. return -EINVAL;
  652. }
  653. }
  654. if (intel_mid_i2c_setup(adap, pmsg)) {
  655. mutex_unlock(&i2c->lock);
  656. pm_runtime_put(i2c->dev);
  657. return -EINVAL;
  658. }
  659. for (i = 0; i < num; i++) {
  660. i2c->msg = pmsg;
  661. i2c->status = STATUS_IDLE;
  662. /* Read or Write */
  663. if (pmsg->flags & I2C_M_RD) {
  664. dev_dbg(&adap->dev, "I2C_M_RD\n");
  665. err = xfer_read(adap, pmsg->buf, pmsg->len);
  666. } else {
  667. dev_dbg(&adap->dev, "I2C_M_WR\n");
  668. err = xfer_write(adap, pmsg->buf, pmsg->len);
  669. }
  670. if (err < 0)
  671. break;
  672. dev_dbg(&adap->dev, "msg[%d] transfer complete\n", i);
  673. pmsg++; /* next message */
  674. }
  675. /* Mask interrupts */
  676. writel(0x0000, i2c->base + IC_INTR_MASK);
  677. /* Clear all interrupts */
  678. readl(i2c->base + IC_CLR_INTR);
  679. i2c->status = STATUS_IDLE;
  680. mutex_unlock(&i2c->lock);
  681. pm_runtime_put(i2c->dev);
  682. return err;
  683. }
  684. static int intel_mid_i2c_runtime_suspend(struct device *dev)
  685. {
  686. struct pci_dev *pdev = to_pci_dev(dev);
  687. struct intel_mid_i2c_private *i2c = pci_get_drvdata(pdev);
  688. struct i2c_adapter *adap = to_i2c_adapter(dev);
  689. int err;
  690. if (i2c->status != STATUS_IDLE)
  691. return -1;
  692. intel_mid_i2c_disable(adap);
  693. err = pci_save_state(pdev);
  694. if (err) {
  695. dev_err(dev, "pci_save_state failed\n");
  696. return err;
  697. }
  698. err = pci_set_power_state(pdev, PCI_D3hot);
  699. if (err) {
  700. dev_err(dev, "pci_set_power_state failed\n");
  701. return err;
  702. }
  703. i2c->status = STATUS_STANDBY;
  704. return 0;
  705. }
  706. static int intel_mid_i2c_runtime_resume(struct device *dev)
  707. {
  708. struct pci_dev *pdev = to_pci_dev(dev);
  709. struct intel_mid_i2c_private *i2c = pci_get_drvdata(pdev);
  710. int err;
  711. if (i2c->status != STATUS_STANDBY)
  712. return 0;
  713. pci_set_power_state(pdev, PCI_D0);
  714. pci_restore_state(pdev);
  715. err = pci_enable_device(pdev);
  716. if (err) {
  717. dev_err(dev, "pci_enable_device failed\n");
  718. return err;
  719. }
  720. i2c->status = STATUS_IDLE;
  721. intel_mid_i2c_hwinit(i2c);
  722. return err;
  723. }
  724. static void i2c_isr_read(struct intel_mid_i2c_private *i2c)
  725. {
  726. struct i2c_msg *msg = i2c->msg;
  727. int rx_num;
  728. u32 len;
  729. u8 *buf;
  730. if (!(msg->flags & I2C_M_RD))
  731. return;
  732. if (i2c->status != STATUS_READ_IN_PROGRESS) {
  733. len = msg->len;
  734. buf = msg->buf;
  735. } else {
  736. len = i2c->rx_buf_len;
  737. buf = i2c->rx_buf;
  738. }
  739. rx_num = readl(i2c->base + IC_RXFLR);
  740. for (; len > 0 && rx_num > 0; len--, rx_num--)
  741. *buf++ = readl(i2c->base + IC_DATA_CMD);
  742. if (len > 0) {
  743. i2c->status = STATUS_READ_IN_PROGRESS;
  744. i2c->rx_buf_len = len;
  745. i2c->rx_buf = buf;
  746. } else
  747. i2c->status = STATUS_READ_SUCCESS;
  748. return;
  749. }
  750. static irqreturn_t intel_mid_i2c_isr(int this_irq, void *dev)
  751. {
  752. struct intel_mid_i2c_private *i2c = dev;
  753. u32 stat = readl(i2c->base + IC_INTR_STAT);
  754. if (!stat)
  755. return IRQ_NONE;
  756. dev_dbg(&i2c->adap.dev, "%s, stat = 0x%x\n", __func__, stat);
  757. stat &= 0x54;
  758. if (i2c->status != STATUS_WRITE_START &&
  759. i2c->status != STATUS_READ_START &&
  760. i2c->status != STATUS_READ_IN_PROGRESS)
  761. goto err;
  762. if (stat & TX_ABRT)
  763. i2c->abort = readl(i2c->base + IC_TX_ABRT_SOURCE);
  764. readl(i2c->base + IC_CLR_INTR);
  765. if (stat & TX_ABRT) {
  766. intel_mid_i2c_abort(i2c);
  767. goto exit;
  768. }
  769. if (stat & RX_FULL) {
  770. i2c_isr_read(i2c);
  771. goto exit;
  772. }
  773. if (stat & TX_EMPTY) {
  774. if (readl(i2c->base + IC_STATUS) & 0x4)
  775. i2c->status = STATUS_WRITE_SUCCESS;
  776. }
  777. exit:
  778. if (i2c->status == STATUS_READ_SUCCESS ||
  779. i2c->status == STATUS_WRITE_SUCCESS ||
  780. i2c->status == STATUS_XFER_ABORT) {
  781. /* Clear all interrupts */
  782. readl(i2c->base + IC_CLR_INTR);
  783. /* Mask interrupts */
  784. writel(0, i2c->base + IC_INTR_MASK);
  785. complete(&i2c->complete);
  786. }
  787. err:
  788. return IRQ_HANDLED;
  789. }
  790. static struct i2c_algorithm intel_mid_i2c_algorithm = {
  791. .master_xfer = intel_mid_i2c_xfer,
  792. .functionality = intel_mid_i2c_func,
  793. };
  794. static const struct dev_pm_ops intel_mid_i2c_pm_ops = {
  795. .runtime_suspend = intel_mid_i2c_runtime_suspend,
  796. .runtime_resume = intel_mid_i2c_runtime_resume,
  797. };
  798. /**
  799. * intel_mid_i2c_probe - I2C controller initialization routine
  800. * @dev: pci device
  801. * @id: device id
  802. *
  803. * Return Values:
  804. * 0 success
  805. * -ENODEV If cannot allocate pci resource
  806. * -ENOMEM If the register base remapping failed, or
  807. * if kzalloc failed
  808. *
  809. * Initialization steps:
  810. * 1. Request for PCI resource
  811. * 2. Remap the start address of PCI resource to register base
  812. * 3. Request for device memory region
  813. * 4. Fill in the struct members of intel_mid_i2c_private
  814. * 5. Call intel_mid_i2c_hwinit() for hardware initialization
  815. * 6. Register I2C adapter in i2c-core
  816. */
  817. static int __devinit intel_mid_i2c_probe(struct pci_dev *dev,
  818. const struct pci_device_id *id)
  819. {
  820. struct intel_mid_i2c_private *mrst;
  821. unsigned long start, len;
  822. int err, busnum;
  823. void __iomem *base = NULL;
  824. dev_dbg(&dev->dev, "Get into probe function for I2C\n");
  825. err = pci_enable_device(dev);
  826. if (err) {
  827. dev_err(&dev->dev, "Failed to enable I2C PCI device (%d)\n",
  828. err);
  829. goto exit;
  830. }
  831. /* Determine the address of the I2C area */
  832. start = pci_resource_start(dev, 0);
  833. len = pci_resource_len(dev, 0);
  834. if (!start || len == 0) {
  835. dev_err(&dev->dev, "base address not set\n");
  836. err = -ENODEV;
  837. goto exit;
  838. }
  839. dev_dbg(&dev->dev, "%s i2c resource start 0x%lx, len=%ld\n",
  840. PLATFORM, start, len);
  841. err = pci_request_region(dev, 0, DRIVER_NAME);
  842. if (err) {
  843. dev_err(&dev->dev, "failed to request I2C region "
  844. "0x%lx-0x%lx\n", start,
  845. (unsigned long)pci_resource_end(dev, 0));
  846. goto exit;
  847. }
  848. base = ioremap_nocache(start, len);
  849. if (!base) {
  850. dev_err(&dev->dev, "I/O memory remapping failed\n");
  851. err = -ENOMEM;
  852. goto fail0;
  853. }
  854. /* Allocate the per-device data structure, intel_mid_i2c_private */
  855. mrst = kzalloc(sizeof(struct intel_mid_i2c_private), GFP_KERNEL);
  856. if (mrst == NULL) {
  857. dev_err(&dev->dev, "can't allocate interface\n");
  858. err = -ENOMEM;
  859. goto fail1;
  860. }
  861. /* Initialize struct members */
  862. snprintf(mrst->adap.name, sizeof(mrst->adap.name),
  863. "Intel MID I2C at %lx", start);
  864. mrst->adap.owner = THIS_MODULE;
  865. mrst->adap.algo = &intel_mid_i2c_algorithm;
  866. mrst->adap.dev.parent = &dev->dev;
  867. mrst->dev = &dev->dev;
  868. mrst->base = base;
  869. mrst->speed = STANDARD;
  870. mrst->abort = 0;
  871. mrst->rx_buf_len = 0;
  872. mrst->status = STATUS_IDLE;
  873. pci_set_drvdata(dev, mrst);
  874. i2c_set_adapdata(&mrst->adap, mrst);
  875. mrst->adap.nr = busnum = id->driver_data;
  876. if (dev->device <= 0x0804)
  877. mrst->platform = MOORESTOWN;
  878. else
  879. mrst->platform = MEDFIELD;
  880. dev_dbg(&dev->dev, "I2C%d\n", busnum);
  881. if (ctl_num > busnum) {
  882. if (speed_mode[busnum] < 0 || speed_mode[busnum] >= NUM_SPEEDS)
  883. dev_warn(&dev->dev, "invalid speed %d ignored.\n",
  884. speed_mode[busnum]);
  885. else
  886. mrst->speed = speed_mode[busnum];
  887. }
  888. /* Initialize i2c controller */
  889. err = intel_mid_i2c_hwinit(mrst);
  890. if (err < 0) {
  891. dev_err(&dev->dev, "I2C interface initialization failed\n");
  892. goto fail2;
  893. }
  894. mutex_init(&mrst->lock);
  895. init_completion(&mrst->complete);
  896. /* Clear all interrupts */
  897. readl(mrst->base + IC_CLR_INTR);
  898. writel(0x0000, mrst->base + IC_INTR_MASK);
  899. err = request_irq(dev->irq, intel_mid_i2c_isr, IRQF_SHARED,
  900. mrst->adap.name, mrst);
  901. if (err) {
  902. dev_err(&dev->dev, "Failed to request IRQ for I2C controller: "
  903. "%s", mrst->adap.name);
  904. goto fail2;
  905. }
  906. /* Adapter registration */
  907. err = i2c_add_numbered_adapter(&mrst->adap);
  908. if (err) {
  909. dev_err(&dev->dev, "Adapter %s registration failed\n",
  910. mrst->adap.name);
  911. goto fail3;
  912. }
  913. dev_dbg(&dev->dev, "%s I2C bus %d driver bind success.\n",
  914. (mrst->platform == MOORESTOWN) ? "Moorestown" : "Medfield",
  915. busnum);
  916. pm_runtime_enable(&dev->dev);
  917. return 0;
  918. fail3:
  919. free_irq(dev->irq, mrst);
  920. fail2:
  921. pci_set_drvdata(dev, NULL);
  922. kfree(mrst);
  923. fail1:
  924. iounmap(base);
  925. fail0:
  926. pci_release_region(dev, 0);
  927. exit:
  928. return err;
  929. }
  930. static void __devexit intel_mid_i2c_remove(struct pci_dev *dev)
  931. {
  932. struct intel_mid_i2c_private *mrst = pci_get_drvdata(dev);
  933. intel_mid_i2c_disable(&mrst->adap);
  934. if (i2c_del_adapter(&mrst->adap))
  935. dev_err(&dev->dev, "Failed to delete i2c adapter");
  936. free_irq(dev->irq, mrst);
  937. pci_set_drvdata(dev, NULL);
  938. iounmap(mrst->base);
  939. kfree(mrst);
  940. pci_release_region(dev, 0);
  941. }
  942. static DEFINE_PCI_DEVICE_TABLE(intel_mid_i2c_ids) = {
  943. /* Moorestown */
  944. { PCI_VDEVICE(INTEL, 0x0802), 0 },
  945. { PCI_VDEVICE(INTEL, 0x0803), 1 },
  946. { PCI_VDEVICE(INTEL, 0x0804), 2 },
  947. /* Medfield */
  948. { PCI_VDEVICE(INTEL, 0x0817), 3,},
  949. { PCI_VDEVICE(INTEL, 0x0818), 4 },
  950. { PCI_VDEVICE(INTEL, 0x0819), 5 },
  951. { PCI_VDEVICE(INTEL, 0x082C), 0 },
  952. { PCI_VDEVICE(INTEL, 0x082D), 1 },
  953. { PCI_VDEVICE(INTEL, 0x082E), 2 },
  954. { 0,}
  955. };
  956. MODULE_DEVICE_TABLE(pci, intel_mid_i2c_ids);
  957. static struct pci_driver intel_mid_i2c_driver = {
  958. .name = DRIVER_NAME,
  959. .id_table = intel_mid_i2c_ids,
  960. .probe = intel_mid_i2c_probe,
  961. .remove = __devexit_p(intel_mid_i2c_remove),
  962. };
  963. static int __init intel_mid_i2c_init(void)
  964. {
  965. return pci_register_driver(&intel_mid_i2c_driver);
  966. }
  967. static void __exit intel_mid_i2c_exit(void)
  968. {
  969. pci_unregister_driver(&intel_mid_i2c_driver);
  970. }
  971. module_init(intel_mid_i2c_init);
  972. module_exit(intel_mid_i2c_exit);
  973. MODULE_AUTHOR("Ba Zheng <zheng.ba@intel.com>");
  974. MODULE_DESCRIPTION("I2C driver for Moorestown Platform");
  975. MODULE_LICENSE("GPL");
  976. MODULE_VERSION(VERSION);