i2c-mchp-pci1xxxx.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip PCI1XXXX I2C adapter driver for PCIe Switch
  4. * which has I2C controller in one of its downstream functions
  5. *
  6. * Copyright (C) 2021 - 2022 Microchip Technology Inc.
  7. *
  8. * Authors: Tharun Kumar P <[email protected]>
  9. * Kumaravel Thiagarajan <[email protected]>
  10. */
  11. #include <linux/bits.h>
  12. #include <linux/delay.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-smbus.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/types.h>
  20. #define SMBUS_MAST_CORE_ADDR_BASE 0x00000
  21. #define SMBUS_MAST_SYS_REG_ADDR_BASE 0x01000
  22. /* SMB register space. */
  23. #define SMB_CORE_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x00)
  24. #define SMB_CORE_CTRL_ESO BIT(6)
  25. #define SMB_CORE_CTRL_FW_ACK BIT(4)
  26. #define SMB_CORE_CTRL_ACK BIT(0)
  27. #define SMB_CORE_CMD_REG_OFF3 (SMBUS_MAST_CORE_ADDR_BASE + 0x0F)
  28. #define SMB_CORE_CMD_REG_OFF2 (SMBUS_MAST_CORE_ADDR_BASE + 0x0E)
  29. #define SMB_CORE_CMD_REG_OFF1 (SMBUS_MAST_CORE_ADDR_BASE + 0x0D)
  30. #define SMB_CORE_CMD_READM BIT(4)
  31. #define SMB_CORE_CMD_STOP BIT(2)
  32. #define SMB_CORE_CMD_START BIT(0)
  33. #define SMB_CORE_CMD_REG_OFF0 (SMBUS_MAST_CORE_ADDR_BASE + 0x0C)
  34. #define SMB_CORE_CMD_M_PROCEED BIT(1)
  35. #define SMB_CORE_CMD_M_RUN BIT(0)
  36. #define SMB_CORE_SR_HOLD_TIME_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x18)
  37. /*
  38. * SR_HOLD_TIME_XK_TICKS field will indicate the number of ticks of the
  39. * baud clock required to program 'Hold Time' at X KHz.
  40. */
  41. #define SR_HOLD_TIME_100K_TICKS 150
  42. #define SR_HOLD_TIME_400K_TICKS 20
  43. #define SR_HOLD_TIME_1000K_TICKS 12
  44. #define SMB_CORE_COMPLETION_REG_OFF3 (SMBUS_MAST_CORE_ADDR_BASE + 0x23)
  45. #define COMPLETION_MDONE BIT(6)
  46. #define COMPLETION_IDLE BIT(5)
  47. #define COMPLETION_MNAKX BIT(0)
  48. #define SMB_CORE_IDLE_SCALING_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x24)
  49. /*
  50. * FAIR_BUS_IDLE_MIN_XK_TICKS field will indicate the number of ticks of
  51. * the baud clock required to program 'fair idle delay' at X KHz. Fair idle
  52. * delay establishes the MCTP T(IDLE_DELAY) period.
  53. */
  54. #define FAIR_BUS_IDLE_MIN_100K_TICKS 992
  55. #define FAIR_BUS_IDLE_MIN_400K_TICKS 500
  56. #define FAIR_BUS_IDLE_MIN_1000K_TICKS 500
  57. /*
  58. * FAIR_IDLE_DELAY_XK_TICKS field will indicate the number of ticks of the
  59. * baud clock required to satisfy the fairness protocol at X KHz.
  60. */
  61. #define FAIR_IDLE_DELAY_100K_TICKS 963
  62. #define FAIR_IDLE_DELAY_400K_TICKS 156
  63. #define FAIR_IDLE_DELAY_1000K_TICKS 156
  64. #define SMB_IDLE_SCALING_100K \
  65. ((FAIR_IDLE_DELAY_100K_TICKS << 16) | FAIR_BUS_IDLE_MIN_100K_TICKS)
  66. #define SMB_IDLE_SCALING_400K \
  67. ((FAIR_IDLE_DELAY_400K_TICKS << 16) | FAIR_BUS_IDLE_MIN_400K_TICKS)
  68. #define SMB_IDLE_SCALING_1000K \
  69. ((FAIR_IDLE_DELAY_1000K_TICKS << 16) | FAIR_BUS_IDLE_MIN_1000K_TICKS)
  70. #define SMB_CORE_CONFIG_REG3 (SMBUS_MAST_CORE_ADDR_BASE + 0x2B)
  71. #define SMB_CONFIG3_ENMI BIT(6)
  72. #define SMB_CONFIG3_ENIDI BIT(5)
  73. #define SMB_CORE_CONFIG_REG2 (SMBUS_MAST_CORE_ADDR_BASE + 0x2A)
  74. #define SMB_CORE_CONFIG_REG1 (SMBUS_MAST_CORE_ADDR_BASE + 0x29)
  75. #define SMB_CONFIG1_ASR BIT(7)
  76. #define SMB_CONFIG1_ENAB BIT(2)
  77. #define SMB_CONFIG1_RESET BIT(1)
  78. #define SMB_CONFIG1_FEN BIT(0)
  79. #define SMB_CORE_BUS_CLK_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x2C)
  80. /*
  81. * BUS_CLK_XK_LOW_PERIOD_TICKS field defines the number of I2C Baud Clock
  82. * periods that make up the low phase of the I2C/SMBus bus clock at X KHz.
  83. */
  84. #define BUS_CLK_100K_LOW_PERIOD_TICKS 156
  85. #define BUS_CLK_400K_LOW_PERIOD_TICKS 41
  86. #define BUS_CLK_1000K_LOW_PERIOD_TICKS 15
  87. /*
  88. * BUS_CLK_XK_HIGH_PERIOD_TICKS field defines the number of I2C Baud Clock
  89. * periods that make up the high phase of the I2C/SMBus bus clock at X KHz.
  90. */
  91. #define BUS_CLK_100K_HIGH_PERIOD_TICKS 154
  92. #define BUS_CLK_400K_HIGH_PERIOD_TICKS 35
  93. #define BUS_CLK_1000K_HIGH_PERIOD_TICKS 14
  94. #define BUS_CLK_100K \
  95. ((BUS_CLK_100K_HIGH_PERIOD_TICKS << 8) | BUS_CLK_100K_LOW_PERIOD_TICKS)
  96. #define BUS_CLK_400K \
  97. ((BUS_CLK_400K_HIGH_PERIOD_TICKS << 8) | BUS_CLK_400K_LOW_PERIOD_TICKS)
  98. #define BUS_CLK_1000K \
  99. ((BUS_CLK_1000K_HIGH_PERIOD_TICKS << 8) | BUS_CLK_1000K_LOW_PERIOD_TICKS)
  100. #define SMB_CORE_CLK_SYNC_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x3C)
  101. /*
  102. * CLK_SYNC_XK defines the number of clock cycles to sync up to the external
  103. * clock before comparing the internal and external clocks for clock stretching
  104. * at X KHz.
  105. */
  106. #define CLK_SYNC_100K 4
  107. #define CLK_SYNC_400K 4
  108. #define CLK_SYNC_1000K 4
  109. #define SMB_CORE_DATA_TIMING_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x40)
  110. /*
  111. *
  112. * FIRST_START_HOLD_XK_TICKS will indicate the number of ticks of the baud
  113. * clock required to program 'FIRST_START_HOLD' timer at X KHz. This timer
  114. * determines the SCLK hold time following SDAT driven low during the first
  115. * START bit in a transfer.
  116. */
  117. #define FIRST_START_HOLD_100K_TICKS 23
  118. #define FIRST_START_HOLD_400K_TICKS 8
  119. #define FIRST_START_HOLD_1000K_TICKS 12
  120. /*
  121. * STOP_SETUP_XK_TICKS will indicate the number of ticks of the baud clock
  122. * required to program 'STOP_SETUP' timer at X KHz. This timer determines the
  123. * SDAT setup time from the rising edge of SCLK for a STOP condition.
  124. */
  125. #define STOP_SETUP_100K_TICKS 150
  126. #define STOP_SETUP_400K_TICKS 20
  127. #define STOP_SETUP_1000K_TICKS 12
  128. /*
  129. * RESTART_SETUP_XK_TICKS will indicate the number of ticks of the baud clock
  130. * required to program 'RESTART_SETUP' timer at X KHz. This timer determines the
  131. * SDAT setup time from the rising edge of SCLK for a repeated START condition.
  132. */
  133. #define RESTART_SETUP_100K_TICKS 156
  134. #define RESTART_SETUP_400K_TICKS 20
  135. #define RESTART_SETUP_1000K_TICKS 12
  136. /*
  137. * DATA_HOLD_XK_TICKS will indicate the number of ticks of the baud clock
  138. * required to program 'DATA_HOLD' timer at X KHz. This timer determines the
  139. * SDAT hold time following SCLK driven low.
  140. */
  141. #define DATA_HOLD_100K_TICKS 12
  142. #define DATA_HOLD_400K_TICKS 2
  143. #define DATA_HOLD_1000K_TICKS 2
  144. #define DATA_TIMING_100K \
  145. ((FIRST_START_HOLD_100K_TICKS << 24) | (STOP_SETUP_100K_TICKS << 16) | \
  146. (RESTART_SETUP_100K_TICKS << 8) | DATA_HOLD_100K_TICKS)
  147. #define DATA_TIMING_400K \
  148. ((FIRST_START_HOLD_400K_TICKS << 24) | (STOP_SETUP_400K_TICKS << 16) | \
  149. (RESTART_SETUP_400K_TICKS << 8) | DATA_HOLD_400K_TICKS)
  150. #define DATA_TIMING_1000K \
  151. ((FIRST_START_HOLD_1000K_TICKS << 24) | (STOP_SETUP_1000K_TICKS << 16) | \
  152. (RESTART_SETUP_1000K_TICKS << 8) | DATA_HOLD_1000K_TICKS)
  153. #define SMB_CORE_TO_SCALING_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x44)
  154. /*
  155. * BUS_IDLE_MIN_XK_TICKS defines Bus Idle Minimum Time.
  156. * Bus Idle Minimum time = BUS_IDLE_MIN[7:0] x Baud_Clock_Period x
  157. * (BUS_IDLE_MIN_XK_TICKS[7] ? 4,1)
  158. */
  159. #define BUS_IDLE_MIN_100K_TICKS 36UL
  160. #define BUS_IDLE_MIN_400K_TICKS 10UL
  161. #define BUS_IDLE_MIN_1000K_TICKS 4UL
  162. /*
  163. * CTRL_CUM_TIME_OUT_XK_TICKS defines SMBus Controller Cumulative Time-Out.
  164. * SMBus Controller Cumulative Time-Out duration =
  165. * CTRL_CUM_TIME_OUT_XK_TICKS[7:0] x Baud_Clock_Period x 2048
  166. */
  167. #define CTRL_CUM_TIME_OUT_100K_TICKS 76
  168. #define CTRL_CUM_TIME_OUT_400K_TICKS 76
  169. #define CTRL_CUM_TIME_OUT_1000K_TICKS 76
  170. /*
  171. * TARGET_CUM_TIME_OUT_XK_TICKS defines SMBus Target Cumulative Time-Out duration.
  172. * SMBus Target Cumulative Time-Out duration = TARGET_CUM_TIME_OUT_XK_TICKS[7:0] x
  173. * Baud_Clock_Period x 4096
  174. */
  175. #define TARGET_CUM_TIME_OUT_100K_TICKS 95
  176. #define TARGET_CUM_TIME_OUT_400K_TICKS 95
  177. #define TARGET_CUM_TIME_OUT_1000K_TICKS 95
  178. /*
  179. * CLOCK_HIGH_TIME_OUT_XK defines Clock High time out period.
  180. * Clock High time out period = CLOCK_HIGH_TIME_OUT_XK[7:0] x Baud_Clock_Period x 8
  181. */
  182. #define CLOCK_HIGH_TIME_OUT_100K_TICKS 97
  183. #define CLOCK_HIGH_TIME_OUT_400K_TICKS 97
  184. #define CLOCK_HIGH_TIME_OUT_1000K_TICKS 97
  185. #define TO_SCALING_100K \
  186. ((BUS_IDLE_MIN_100K_TICKS << 24) | (CTRL_CUM_TIME_OUT_100K_TICKS << 16) | \
  187. (TARGET_CUM_TIME_OUT_100K_TICKS << 8) | CLOCK_HIGH_TIME_OUT_100K_TICKS)
  188. #define TO_SCALING_400K \
  189. ((BUS_IDLE_MIN_400K_TICKS << 24) | (CTRL_CUM_TIME_OUT_400K_TICKS << 16) | \
  190. (TARGET_CUM_TIME_OUT_400K_TICKS << 8) | CLOCK_HIGH_TIME_OUT_400K_TICKS)
  191. #define TO_SCALING_1000K \
  192. ((BUS_IDLE_MIN_1000K_TICKS << 24) | (CTRL_CUM_TIME_OUT_1000K_TICKS << 16) | \
  193. (TARGET_CUM_TIME_OUT_1000K_TICKS << 8) | CLOCK_HIGH_TIME_OUT_1000K_TICKS)
  194. #define I2C_SCL_PAD_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x100)
  195. #define I2C_SDA_PAD_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x101)
  196. #define I2C_FOD_EN BIT(4)
  197. #define I2C_PULL_UP_EN BIT(3)
  198. #define I2C_PULL_DOWN_EN BIT(2)
  199. #define I2C_INPUT_EN BIT(1)
  200. #define I2C_OUTPUT_EN BIT(0)
  201. #define SMBUS_CONTROL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x200)
  202. #define CTL_RESET_COUNTERS BIT(3)
  203. #define CTL_TRANSFER_DIR BIT(2)
  204. #define CTL_HOST_FIFO_ENTRY BIT(1)
  205. #define CTL_RUN BIT(0)
  206. #define I2C_DIRN_WRITE 0
  207. #define I2C_DIRN_READ 1
  208. #define SMBUS_STATUS_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x204)
  209. #define STA_DMA_TERM BIT(7)
  210. #define STA_DMA_REQ BIT(6)
  211. #define STA_THRESHOLD BIT(2)
  212. #define STA_BUF_FULL BIT(1)
  213. #define STA_BUF_EMPTY BIT(0)
  214. #define SMBUS_INTR_STAT_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x208)
  215. #define INTR_STAT_DMA_TERM BIT(7)
  216. #define INTR_STAT_THRESHOLD BIT(2)
  217. #define INTR_STAT_BUF_FULL BIT(1)
  218. #define INTR_STAT_BUF_EMPTY BIT(0)
  219. #define SMBUS_INTR_MSK_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x20C)
  220. #define INTR_MSK_DMA_TERM BIT(7)
  221. #define INTR_MSK_THRESHOLD BIT(2)
  222. #define INTR_MSK_BUF_FULL BIT(1)
  223. #define INTR_MSK_BUF_EMPTY BIT(0)
  224. #define ALL_NW_LAYER_INTERRUPTS \
  225. (INTR_MSK_DMA_TERM | INTR_MSK_THRESHOLD | INTR_MSK_BUF_FULL | \
  226. INTR_MSK_BUF_EMPTY)
  227. #define SMBUS_MCU_COUNTER_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x214)
  228. #define SMBALERT_MST_PAD_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x230)
  229. #define SMBALERT_MST_PU BIT(0)
  230. #define SMBUS_GEN_INT_STAT_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x23C)
  231. #define SMBUS_GEN_INT_MASK_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x240)
  232. #define SMBALERT_INTR_MASK BIT(10)
  233. #define I2C_BUF_MSTR_INTR_MASK BIT(9)
  234. #define I2C_INTR_MASK BIT(8)
  235. #define SMBALERT_WAKE_INTR_MASK BIT(2)
  236. #define I2C_BUF_MSTR_WAKE_INTR_MASK BIT(1)
  237. #define I2C_WAKE_INTR_MASK BIT(0)
  238. #define ALL_HIGH_LAYER_INTR \
  239. (SMBALERT_INTR_MASK | I2C_BUF_MSTR_INTR_MASK | I2C_INTR_MASK | \
  240. SMBALERT_WAKE_INTR_MASK | I2C_BUF_MSTR_WAKE_INTR_MASK | \
  241. I2C_WAKE_INTR_MASK)
  242. #define SMBUS_RESET_REG (SMBUS_MAST_CORE_ADDR_BASE + 0x248)
  243. #define PERI_SMBUS_D3_RESET_DIS BIT(16)
  244. #define SMBUS_MST_BUF (SMBUS_MAST_CORE_ADDR_BASE + 0x280)
  245. #define SMBUS_BUF_MAX_SIZE 0x80
  246. #define I2C_FLAGS_DIRECT_MODE BIT(7)
  247. #define I2C_FLAGS_POLLING_MODE BIT(6)
  248. #define I2C_FLAGS_STOP BIT(5)
  249. #define I2C_FLAGS_SMB_BLK_READ BIT(4)
  250. #define PCI1XXXX_I2C_TIMEOUT_MS 1000
  251. /* General Purpose Register. */
  252. #define SMB_GPR_REG (SMBUS_MAST_CORE_ADDR_BASE + 0x1000 + 0x0c00 + \
  253. 0x00)
  254. /* Lock Register. */
  255. #define SMB_GPR_LOCK_REG (SMBUS_MAST_CORE_ADDR_BASE + 0x1000 + 0x0000 + \
  256. 0x00A0)
  257. #define SMBUS_PERI_LOCK BIT(3)
  258. struct pci1xxxx_i2c {
  259. struct completion i2c_xfer_done;
  260. bool i2c_xfer_in_progress;
  261. struct i2c_adapter adap;
  262. void __iomem *i2c_base;
  263. u32 freq;
  264. u32 flags;
  265. };
  266. static int set_sys_lock(struct pci1xxxx_i2c *i2c)
  267. {
  268. void __iomem *p = i2c->i2c_base + SMB_GPR_LOCK_REG;
  269. u8 data;
  270. writel(SMBUS_PERI_LOCK, p);
  271. data = readl(p);
  272. if (data != SMBUS_PERI_LOCK)
  273. return -EPERM;
  274. return 0;
  275. }
  276. static int release_sys_lock(struct pci1xxxx_i2c *i2c)
  277. {
  278. void __iomem *p = i2c->i2c_base + SMB_GPR_LOCK_REG;
  279. u8 data;
  280. data = readl(p);
  281. if (data != SMBUS_PERI_LOCK)
  282. return 0;
  283. writel(0, p);
  284. data = readl(p);
  285. if (data & SMBUS_PERI_LOCK)
  286. return -EPERM;
  287. return 0;
  288. }
  289. static void pci1xxxx_ack_high_level_intr(struct pci1xxxx_i2c *i2c, u16 intr_msk)
  290. {
  291. writew(intr_msk, i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF);
  292. }
  293. static void pci1xxxx_i2c_configure_smbalert_pin(struct pci1xxxx_i2c *i2c,
  294. bool enable)
  295. {
  296. void __iomem *p = i2c->i2c_base + SMBALERT_MST_PAD_CTRL_REG_OFF;
  297. u8 regval;
  298. regval = readb(p);
  299. if (enable)
  300. regval |= SMBALERT_MST_PU;
  301. else
  302. regval &= ~SMBALERT_MST_PU;
  303. writeb(regval, p);
  304. }
  305. static void pci1xxxx_i2c_send_start_stop(struct pci1xxxx_i2c *i2c, bool start)
  306. {
  307. void __iomem *p = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
  308. u8 regval;
  309. regval = readb(p);
  310. if (start)
  311. regval |= SMB_CORE_CMD_START;
  312. else
  313. regval |= SMB_CORE_CMD_STOP;
  314. writeb(regval, p);
  315. }
  316. /*
  317. * When accessing the core control reg, we should not do a read modified write
  318. * as they are write '1' to clear bits. Instead we need to write with the
  319. * specific bits that needs to be set.
  320. */
  321. static void pci1xxxx_i2c_set_clear_FW_ACK(struct pci1xxxx_i2c *i2c, bool set)
  322. {
  323. u8 regval;
  324. if (set)
  325. regval = SMB_CORE_CTRL_FW_ACK | SMB_CORE_CTRL_ESO | SMB_CORE_CTRL_ACK;
  326. else
  327. regval = SMB_CORE_CTRL_ESO | SMB_CORE_CTRL_ACK;
  328. writeb(regval, i2c->i2c_base + SMB_CORE_CTRL_REG_OFF);
  329. }
  330. static void pci1xxxx_i2c_buffer_write(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
  331. u8 transferlen, unsigned char *buf)
  332. {
  333. void __iomem *p = i2c->i2c_base + SMBUS_MST_BUF;
  334. if (slaveaddr)
  335. writeb(slaveaddr, p++);
  336. if (buf)
  337. memcpy_toio(p, buf, transferlen);
  338. }
  339. /*
  340. * When accessing the core control reg, we should not do a read modified write
  341. * as there are write '1' to clear bits. Instead we need to write with the
  342. * specific bits that needs to be set.
  343. */
  344. static void pci1xxxx_i2c_enable_ESO(struct pci1xxxx_i2c *i2c)
  345. {
  346. writeb(SMB_CORE_CTRL_ESO, i2c->i2c_base + SMB_CORE_CTRL_REG_OFF);
  347. }
  348. static void pci1xxxx_i2c_reset_counters(struct pci1xxxx_i2c *i2c)
  349. {
  350. void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
  351. u8 regval;
  352. regval = readb(p);
  353. regval |= CTL_RESET_COUNTERS;
  354. writeb(regval, p);
  355. }
  356. static void pci1xxxx_i2c_set_transfer_dir(struct pci1xxxx_i2c *i2c, u8 direction)
  357. {
  358. void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
  359. u8 regval;
  360. regval = readb(p);
  361. if (direction == I2C_DIRN_WRITE)
  362. regval &= ~CTL_TRANSFER_DIR;
  363. else
  364. regval |= CTL_TRANSFER_DIR;
  365. writeb(regval, p);
  366. }
  367. static void pci1xxxx_i2c_set_mcu_count(struct pci1xxxx_i2c *i2c, u8 count)
  368. {
  369. writeb(count, i2c->i2c_base + SMBUS_MCU_COUNTER_REG_OFF);
  370. }
  371. static void pci1xxxx_i2c_set_read_count(struct pci1xxxx_i2c *i2c, u8 readcount)
  372. {
  373. writeb(readcount, i2c->i2c_base + SMB_CORE_CMD_REG_OFF3);
  374. }
  375. static void pci1xxxx_i2c_set_write_count(struct pci1xxxx_i2c *i2c, u8 writecount)
  376. {
  377. writeb(writecount, i2c->i2c_base + SMB_CORE_CMD_REG_OFF2);
  378. }
  379. static void pci1xxxx_i2c_set_DMA_run(struct pci1xxxx_i2c *i2c)
  380. {
  381. void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
  382. u8 regval;
  383. regval = readb(p);
  384. regval |= CTL_RUN;
  385. writeb(regval, p);
  386. }
  387. static void pci1xxxx_i2c_set_mrun_proceed(struct pci1xxxx_i2c *i2c)
  388. {
  389. void __iomem *p = i2c->i2c_base + SMB_CORE_CMD_REG_OFF0;
  390. u8 regval;
  391. regval = readb(p);
  392. regval |= SMB_CORE_CMD_M_RUN;
  393. regval |= SMB_CORE_CMD_M_PROCEED;
  394. writeb(regval, p);
  395. }
  396. static void pci1xxxx_i2c_start_DMA(struct pci1xxxx_i2c *i2c)
  397. {
  398. pci1xxxx_i2c_set_DMA_run(i2c);
  399. pci1xxxx_i2c_set_mrun_proceed(i2c);
  400. }
  401. static void pci1xxxx_i2c_config_asr(struct pci1xxxx_i2c *i2c, bool enable)
  402. {
  403. void __iomem *p = i2c->i2c_base + SMB_CORE_CONFIG_REG1;
  404. u8 regval;
  405. regval = readb(p);
  406. if (enable)
  407. regval |= SMB_CONFIG1_ASR;
  408. else
  409. regval &= ~SMB_CONFIG1_ASR;
  410. writeb(regval, p);
  411. }
  412. static irqreturn_t pci1xxxx_i2c_isr(int irq, void *dev)
  413. {
  414. struct pci1xxxx_i2c *i2c = dev;
  415. void __iomem *p1 = i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF;
  416. void __iomem *p2 = i2c->i2c_base + SMBUS_INTR_STAT_REG_OFF;
  417. irqreturn_t intr_handled = IRQ_NONE;
  418. u16 reg1;
  419. u8 reg3;
  420. /*
  421. * Read the SMBus interrupt status register to see if the
  422. * DMA_TERM interrupt has caused this callback.
  423. */
  424. reg1 = readw(p1);
  425. if (reg1 & I2C_BUF_MSTR_INTR_MASK) {
  426. reg3 = readb(p2);
  427. if (reg3 & INTR_STAT_DMA_TERM) {
  428. complete(&i2c->i2c_xfer_done);
  429. intr_handled = IRQ_HANDLED;
  430. writeb(INTR_STAT_DMA_TERM, p2);
  431. }
  432. pci1xxxx_ack_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK);
  433. }
  434. if (reg1 & SMBALERT_INTR_MASK) {
  435. intr_handled = IRQ_HANDLED;
  436. pci1xxxx_ack_high_level_intr(i2c, SMBALERT_INTR_MASK);
  437. }
  438. return intr_handled;
  439. }
  440. static void pci1xxxx_i2c_set_count(struct pci1xxxx_i2c *i2c, u8 mcucount,
  441. u8 writecount, u8 readcount)
  442. {
  443. pci1xxxx_i2c_set_mcu_count(i2c, mcucount);
  444. pci1xxxx_i2c_set_write_count(i2c, writecount);
  445. pci1xxxx_i2c_set_read_count(i2c, readcount);
  446. }
  447. static void pci1xxxx_i2c_set_readm(struct pci1xxxx_i2c *i2c, bool enable)
  448. {
  449. void __iomem *p = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
  450. u8 regval;
  451. regval = readb(p);
  452. if (enable)
  453. regval |= SMB_CORE_CMD_READM;
  454. else
  455. regval &= ~SMB_CORE_CMD_READM;
  456. writeb(regval, p);
  457. }
  458. static void pci1xxxx_ack_nw_layer_intr(struct pci1xxxx_i2c *i2c, u8 ack_intr_msk)
  459. {
  460. writeb(ack_intr_msk, i2c->i2c_base + SMBUS_INTR_STAT_REG_OFF);
  461. }
  462. static void pci1xxxx_config_nw_layer_intr(struct pci1xxxx_i2c *i2c,
  463. u8 intr_msk, bool enable)
  464. {
  465. void __iomem *p = i2c->i2c_base + SMBUS_INTR_MSK_REG_OFF;
  466. u8 regval;
  467. regval = readb(p);
  468. if (enable)
  469. regval &= ~intr_msk;
  470. else
  471. regval |= intr_msk;
  472. writeb(regval, p);
  473. }
  474. static void pci1xxxx_i2c_config_padctrl(struct pci1xxxx_i2c *i2c, bool enable)
  475. {
  476. void __iomem *p1 = i2c->i2c_base + I2C_SCL_PAD_CTRL_REG_OFF;
  477. void __iomem *p2 = i2c->i2c_base + I2C_SDA_PAD_CTRL_REG_OFF;
  478. u8 regval;
  479. regval = readb(p1);
  480. if (enable)
  481. regval |= I2C_INPUT_EN | I2C_OUTPUT_EN;
  482. else
  483. regval &= ~(I2C_INPUT_EN | I2C_OUTPUT_EN);
  484. writeb(regval, p1);
  485. regval = readb(p2);
  486. if (enable)
  487. regval |= I2C_INPUT_EN | I2C_OUTPUT_EN;
  488. else
  489. regval &= ~(I2C_INPUT_EN | I2C_OUTPUT_EN);
  490. writeb(regval, p2);
  491. }
  492. static void pci1xxxx_i2c_set_mode(struct pci1xxxx_i2c *i2c)
  493. {
  494. void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
  495. u8 regval;
  496. regval = readb(p);
  497. if (i2c->flags & I2C_FLAGS_DIRECT_MODE)
  498. regval &= ~CTL_HOST_FIFO_ENTRY;
  499. else
  500. regval |= CTL_HOST_FIFO_ENTRY;
  501. writeb(regval, p);
  502. }
  503. static void pci1xxxx_i2c_config_high_level_intr(struct pci1xxxx_i2c *i2c,
  504. u16 intr_msk, bool enable)
  505. {
  506. void __iomem *p = i2c->i2c_base + SMBUS_GEN_INT_MASK_REG_OFF;
  507. u16 regval;
  508. regval = readw(p);
  509. if (enable)
  510. regval &= ~intr_msk;
  511. else
  512. regval |= intr_msk;
  513. writew(regval, p);
  514. }
  515. static void pci1xxxx_i2c_configure_core_reg(struct pci1xxxx_i2c *i2c, bool enable)
  516. {
  517. void __iomem *p1 = i2c->i2c_base + SMB_CORE_CONFIG_REG1;
  518. void __iomem *p3 = i2c->i2c_base + SMB_CORE_CONFIG_REG3;
  519. u8 reg1;
  520. u8 reg3;
  521. reg1 = readb(p1);
  522. reg3 = readb(p3);
  523. if (enable) {
  524. reg1 |= SMB_CONFIG1_ENAB | SMB_CONFIG1_FEN;
  525. reg3 |= SMB_CONFIG3_ENMI | SMB_CONFIG3_ENIDI;
  526. } else {
  527. reg1 &= ~(SMB_CONFIG1_ENAB | SMB_CONFIG1_FEN);
  528. reg3 &= ~(SMB_CONFIG3_ENMI | SMB_CONFIG3_ENIDI);
  529. }
  530. writeb(reg1, p1);
  531. writeb(reg3, p3);
  532. }
  533. static void pci1xxxx_i2c_set_freq(struct pci1xxxx_i2c *i2c)
  534. {
  535. void __iomem *bp = i2c->i2c_base;
  536. void __iomem *p_idle_scaling = bp + SMB_CORE_IDLE_SCALING_REG_OFF;
  537. void __iomem *p_data_timing = bp + SMB_CORE_DATA_TIMING_REG_OFF;
  538. void __iomem *p_hold_time = bp + SMB_CORE_SR_HOLD_TIME_REG_OFF;
  539. void __iomem *p_to_scaling = bp + SMB_CORE_TO_SCALING_REG_OFF;
  540. void __iomem *p_clk_sync = bp + SMB_CORE_CLK_SYNC_REG_OFF;
  541. void __iomem *p_clk_reg = bp + SMB_CORE_BUS_CLK_REG_OFF;
  542. switch (i2c->freq) {
  543. case I2C_MAX_STANDARD_MODE_FREQ:
  544. writeb(SR_HOLD_TIME_100K_TICKS, p_hold_time);
  545. writel(SMB_IDLE_SCALING_100K, p_idle_scaling);
  546. writew(BUS_CLK_100K, p_clk_reg);
  547. writel(CLK_SYNC_100K, p_clk_sync);
  548. writel(DATA_TIMING_100K, p_data_timing);
  549. writel(TO_SCALING_100K, p_to_scaling);
  550. break;
  551. case I2C_MAX_FAST_MODE_PLUS_FREQ:
  552. writeb(SR_HOLD_TIME_1000K_TICKS, p_hold_time);
  553. writel(SMB_IDLE_SCALING_1000K, p_idle_scaling);
  554. writew(BUS_CLK_1000K, p_clk_reg);
  555. writel(CLK_SYNC_1000K, p_clk_sync);
  556. writel(DATA_TIMING_1000K, p_data_timing);
  557. writel(TO_SCALING_1000K, p_to_scaling);
  558. break;
  559. case I2C_MAX_FAST_MODE_FREQ:
  560. default:
  561. writeb(SR_HOLD_TIME_400K_TICKS, p_hold_time);
  562. writel(SMB_IDLE_SCALING_400K, p_idle_scaling);
  563. writew(BUS_CLK_400K, p_clk_reg);
  564. writel(CLK_SYNC_400K, p_clk_sync);
  565. writel(DATA_TIMING_400K, p_data_timing);
  566. writel(TO_SCALING_400K, p_to_scaling);
  567. break;
  568. }
  569. }
  570. static void pci1xxxx_i2c_init(struct pci1xxxx_i2c *i2c)
  571. {
  572. void __iomem *p2 = i2c->i2c_base + SMBUS_STATUS_REG_OFF;
  573. void __iomem *p1 = i2c->i2c_base + SMB_GPR_REG;
  574. u8 regval;
  575. int ret;
  576. ret = set_sys_lock(i2c);
  577. if (ret == -EPERM) {
  578. /*
  579. * Configure I2C Fast Mode as default frequency if unable
  580. * to acquire sys lock.
  581. */
  582. regval = 0;
  583. } else {
  584. regval = readl(p1);
  585. release_sys_lock(i2c);
  586. }
  587. switch (regval) {
  588. case 0:
  589. i2c->freq = I2C_MAX_FAST_MODE_FREQ;
  590. pci1xxxx_i2c_set_freq(i2c);
  591. break;
  592. case 1:
  593. i2c->freq = I2C_MAX_STANDARD_MODE_FREQ;
  594. pci1xxxx_i2c_set_freq(i2c);
  595. break;
  596. case 2:
  597. i2c->freq = I2C_MAX_FAST_MODE_PLUS_FREQ;
  598. pci1xxxx_i2c_set_freq(i2c);
  599. break;
  600. case 3:
  601. default:
  602. break;
  603. }
  604. pci1xxxx_i2c_config_padctrl(i2c, true);
  605. i2c->flags |= I2C_FLAGS_DIRECT_MODE;
  606. pci1xxxx_i2c_set_mode(i2c);
  607. /*
  608. * Added as a precaution since BUF_EMPTY in status register
  609. * also trigered an Interrupt.
  610. */
  611. writeb(STA_BUF_EMPTY, p2);
  612. /* Configure core I2c control registers. */
  613. pci1xxxx_i2c_configure_core_reg(i2c, true);
  614. /*
  615. * Enable pull-up for the SMB alert pin which is just used for
  616. * wakeup right now.
  617. */
  618. pci1xxxx_i2c_configure_smbalert_pin(i2c, true);
  619. }
  620. static void pci1xxxx_i2c_clear_flags(struct pci1xxxx_i2c *i2c)
  621. {
  622. u8 regval;
  623. /* Reset the internal buffer counters. */
  624. pci1xxxx_i2c_reset_counters(i2c);
  625. /* Clear low level interrupts. */
  626. regval = COMPLETION_MNAKX | COMPLETION_IDLE | COMPLETION_MDONE;
  627. writeb(regval, i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3);
  628. reinit_completion(&i2c->i2c_xfer_done);
  629. pci1xxxx_ack_nw_layer_intr(i2c, ALL_NW_LAYER_INTERRUPTS);
  630. pci1xxxx_ack_high_level_intr(i2c, ALL_HIGH_LAYER_INTR);
  631. }
  632. static int pci1xxxx_i2c_read(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
  633. unsigned char *buf, u16 total_len)
  634. {
  635. void __iomem *p2 = i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3;
  636. void __iomem *p1 = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
  637. void __iomem *p3 = i2c->i2c_base + SMBUS_MST_BUF;
  638. unsigned long time_left;
  639. u16 remainingbytes;
  640. u8 transferlen;
  641. int retval = 0;
  642. u8 read_count;
  643. u32 regval;
  644. u16 count;
  645. /* Enable I2C host controller by setting the ESO bit in the CONTROL REG. */
  646. pci1xxxx_i2c_enable_ESO(i2c);
  647. pci1xxxx_i2c_clear_flags(i2c);
  648. pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, true);
  649. pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, true);
  650. /*
  651. * The I2C transfer could be more than 128 bytes. Our Core is
  652. * capable of only sending 128 at a time.
  653. * As far as the I2C read is concerned, initailly send the
  654. * read slave address along with the number of bytes to read in
  655. * ReadCount. After sending the slave address the interrupt
  656. * is generated. On seeing the ACK for the slave address, reverse the
  657. * buffer direction and run the DMA to initiate Read from slave.
  658. */
  659. for (count = 0; count < total_len; count += transferlen) {
  660. /*
  661. * Before start of any transaction clear the existing
  662. * START/STOP conditions.
  663. */
  664. writeb(0, p1);
  665. remainingbytes = total_len - count;
  666. transferlen = min_t(u16, remainingbytes, SMBUS_BUF_MAX_SIZE);
  667. /*
  668. * Send STOP bit for the last chunk in the transaction.
  669. * For I2C read transaction of more than BUF_SIZE, NACK should
  670. * only be sent for the last read.
  671. * Hence a bit FW_ACK is set for all the read chunks except for
  672. * the last chunk. For the last chunk NACK should be sent and
  673. * FW_ACK is cleared Send STOP only when I2C_FLAGS_STOP bit is
  674. * set in the flags and only for the last transaction.
  675. */
  676. if ((count + transferlen >= total_len) &&
  677. (i2c->flags & I2C_FLAGS_STOP)) {
  678. pci1xxxx_i2c_set_clear_FW_ACK(i2c, false);
  679. pci1xxxx_i2c_send_start_stop(i2c, 0);
  680. } else {
  681. pci1xxxx_i2c_set_clear_FW_ACK(i2c, true);
  682. }
  683. /* Send START bit for the first transaction. */
  684. if (count == 0) {
  685. pci1xxxx_i2c_set_transfer_dir(i2c, I2C_DIRN_WRITE);
  686. pci1xxxx_i2c_send_start_stop(i2c, 1);
  687. /* Write I2c buffer with just the slave addr. */
  688. pci1xxxx_i2c_buffer_write(i2c, slaveaddr, 0, NULL);
  689. /* Set the count. Readcount is the transfer bytes. */
  690. pci1xxxx_i2c_set_count(i2c, 1, 1, transferlen);
  691. /*
  692. * Set the Auto_start_read bit so that the HW itself
  693. * will take care of the read phase.
  694. */
  695. pci1xxxx_i2c_config_asr(i2c, true);
  696. if (i2c->flags & I2C_FLAGS_SMB_BLK_READ)
  697. pci1xxxx_i2c_set_readm(i2c, true);
  698. } else {
  699. pci1xxxx_i2c_set_count(i2c, 0, 0, transferlen);
  700. pci1xxxx_i2c_config_asr(i2c, false);
  701. pci1xxxx_i2c_clear_flags(i2c);
  702. pci1xxxx_i2c_set_transfer_dir(i2c, I2C_DIRN_READ);
  703. }
  704. /* Start the DMA. */
  705. pci1xxxx_i2c_start_DMA(i2c);
  706. /* Wait for the DMA_TERM interrupt. */
  707. time_left = wait_for_completion_timeout(&i2c->i2c_xfer_done,
  708. msecs_to_jiffies(PCI1XXXX_I2C_TIMEOUT_MS));
  709. if (time_left == 0) {
  710. /* Reset the I2C core to release the bus lock. */
  711. pci1xxxx_i2c_init(i2c);
  712. retval = -ETIMEDOUT;
  713. goto cleanup;
  714. }
  715. /* Read the completion reg to know the reason for DMA_TERM. */
  716. regval = readb(p2);
  717. /* Slave did not respond. */
  718. if (regval & COMPLETION_MNAKX) {
  719. writeb(COMPLETION_MNAKX, p2);
  720. retval = -ETIMEDOUT;
  721. goto cleanup;
  722. }
  723. if (i2c->flags & I2C_FLAGS_SMB_BLK_READ) {
  724. buf[0] = readb(p3);
  725. read_count = buf[0];
  726. memcpy_fromio(&buf[1], p3 + 1, read_count);
  727. } else {
  728. memcpy_fromio(&buf[count], p3, transferlen);
  729. }
  730. }
  731. cleanup:
  732. /* Disable all the interrupts. */
  733. pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, false);
  734. pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, false);
  735. pci1xxxx_i2c_config_asr(i2c, false);
  736. return retval;
  737. }
  738. static int pci1xxxx_i2c_write(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
  739. unsigned char *buf, u16 total_len)
  740. {
  741. void __iomem *p2 = i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3;
  742. void __iomem *p1 = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
  743. unsigned long time_left;
  744. u16 remainingbytes;
  745. u8 actualwritelen;
  746. u8 transferlen;
  747. int retval = 0;
  748. u32 regval;
  749. u16 count;
  750. /* Enable I2C host controller by setting the ESO bit in the CONTROL REG. */
  751. pci1xxxx_i2c_enable_ESO(i2c);
  752. /* Set the Buffer direction. */
  753. pci1xxxx_i2c_set_transfer_dir(i2c, I2C_DIRN_WRITE);
  754. pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, true);
  755. pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, true);
  756. /*
  757. * The i2c transfer could be more than 128 bytes. Our Core is
  758. * capable of only sending 128 at a time.
  759. */
  760. for (count = 0; count < total_len; count += transferlen) {
  761. /*
  762. * Before start of any transaction clear the existing
  763. * START/STOP conditions.
  764. */
  765. writeb(0, p1);
  766. pci1xxxx_i2c_clear_flags(i2c);
  767. remainingbytes = total_len - count;
  768. /* If it is the starting of the transaction send START. */
  769. if (count == 0) {
  770. pci1xxxx_i2c_send_start_stop(i2c, 1);
  771. /* -1 for the slave address. */
  772. transferlen = min_t(u16, SMBUS_BUF_MAX_SIZE - 1,
  773. remainingbytes);
  774. pci1xxxx_i2c_buffer_write(i2c, slaveaddr,
  775. transferlen, &buf[count]);
  776. /*
  777. * The actual number of bytes written on the I2C bus
  778. * is including the slave address.
  779. */
  780. actualwritelen = transferlen + 1;
  781. } else {
  782. transferlen = min_t(u16, SMBUS_BUF_MAX_SIZE, remainingbytes);
  783. pci1xxxx_i2c_buffer_write(i2c, 0, transferlen, &buf[count]);
  784. actualwritelen = transferlen;
  785. }
  786. pci1xxxx_i2c_set_count(i2c, actualwritelen, actualwritelen, 0);
  787. /*
  788. * Send STOP only when I2C_FLAGS_STOP bit is set in the flags and
  789. * only for the last transaction.
  790. */
  791. if (remainingbytes <= transferlen &&
  792. (i2c->flags & I2C_FLAGS_STOP))
  793. pci1xxxx_i2c_send_start_stop(i2c, 0);
  794. pci1xxxx_i2c_start_DMA(i2c);
  795. /*
  796. * Wait for the DMA_TERM interrupt.
  797. */
  798. time_left = wait_for_completion_timeout(&i2c->i2c_xfer_done,
  799. msecs_to_jiffies(PCI1XXXX_I2C_TIMEOUT_MS));
  800. if (time_left == 0) {
  801. /* Reset the I2C core to release the bus lock. */
  802. pci1xxxx_i2c_init(i2c);
  803. retval = -ETIMEDOUT;
  804. goto cleanup;
  805. }
  806. regval = readb(p2);
  807. if (regval & COMPLETION_MNAKX) {
  808. writeb(COMPLETION_MNAKX, p2);
  809. retval = -ETIMEDOUT;
  810. goto cleanup;
  811. }
  812. }
  813. cleanup:
  814. /* Disable all the interrupts. */
  815. pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, false);
  816. pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, false);
  817. return retval;
  818. }
  819. static int pci1xxxx_i2c_xfer(struct i2c_adapter *adap,
  820. struct i2c_msg *msgs, int num)
  821. {
  822. struct pci1xxxx_i2c *i2c = i2c_get_adapdata(adap);
  823. u8 slaveaddr;
  824. int retval;
  825. u32 i;
  826. i2c->i2c_xfer_in_progress = true;
  827. for (i = 0; i < num; i++) {
  828. slaveaddr = i2c_8bit_addr_from_msg(&msgs[i]);
  829. /*
  830. * Send the STOP bit if the transfer is the final one or
  831. * if the I2C_M_STOP flag is set.
  832. */
  833. if ((i == num - 1) || (msgs[i].flags & I2C_M_STOP))
  834. i2c->flags |= I2C_FLAGS_STOP;
  835. else
  836. i2c->flags &= ~I2C_FLAGS_STOP;
  837. if (msgs[i].flags & I2C_M_RECV_LEN)
  838. i2c->flags |= I2C_FLAGS_SMB_BLK_READ;
  839. else
  840. i2c->flags &= ~I2C_FLAGS_SMB_BLK_READ;
  841. if (msgs[i].flags & I2C_M_RD)
  842. retval = pci1xxxx_i2c_read(i2c, slaveaddr,
  843. msgs[i].buf, msgs[i].len);
  844. else
  845. retval = pci1xxxx_i2c_write(i2c, slaveaddr,
  846. msgs[i].buf, msgs[i].len);
  847. if (retval < 0)
  848. break;
  849. }
  850. i2c->i2c_xfer_in_progress = false;
  851. if (retval < 0)
  852. return retval;
  853. return num;
  854. }
  855. /*
  856. * List of supported functions by the driver.
  857. */
  858. static u32 pci1xxxx_i2c_get_funcs(struct i2c_adapter *adap)
  859. {
  860. return I2C_FUNC_I2C | I2C_FUNC_PROTOCOL_MANGLING |
  861. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  862. I2C_FUNC_SMBUS_BYTE |
  863. I2C_FUNC_SMBUS_BYTE_DATA |
  864. I2C_FUNC_SMBUS_WORD_DATA |
  865. I2C_FUNC_SMBUS_PROC_CALL |
  866. I2C_FUNC_SMBUS_BLOCK_DATA;
  867. }
  868. static const struct i2c_algorithm pci1xxxx_i2c_algo = {
  869. .master_xfer = pci1xxxx_i2c_xfer,
  870. .functionality = pci1xxxx_i2c_get_funcs,
  871. };
  872. static const struct i2c_adapter_quirks pci1xxxx_i2c_quirks = {
  873. .flags = I2C_AQ_NO_ZERO_LEN,
  874. };
  875. static const struct i2c_adapter pci1xxxx_i2c_ops = {
  876. .owner = THIS_MODULE,
  877. .name = "PCI1xxxx I2C Adapter",
  878. .algo = &pci1xxxx_i2c_algo,
  879. .quirks = &pci1xxxx_i2c_quirks,
  880. };
  881. static int pci1xxxx_i2c_suspend(struct device *dev)
  882. {
  883. struct pci1xxxx_i2c *i2c = dev_get_drvdata(dev);
  884. void __iomem *p = i2c->i2c_base + SMBUS_RESET_REG;
  885. struct pci_dev *pdev = to_pci_dev(dev);
  886. u32 regval;
  887. i2c_mark_adapter_suspended(&i2c->adap);
  888. /*
  889. * If the system is put into 'suspend' state when the I2C transfer is in
  890. * progress, wait until the transfer completes.
  891. */
  892. while (i2c->i2c_xfer_in_progress)
  893. msleep(20);
  894. pci1xxxx_i2c_config_high_level_intr(i2c, SMBALERT_WAKE_INTR_MASK, true);
  895. /*
  896. * Enable the PERST_DIS bit to mask the PERST from resetting the core
  897. * registers.
  898. */
  899. regval = readl(p);
  900. regval |= PERI_SMBUS_D3_RESET_DIS;
  901. writel(regval, p);
  902. /* Enable PCI wake in the PMCSR register. */
  903. device_set_wakeup_enable(dev, true);
  904. pci_wake_from_d3(pdev, true);
  905. return 0;
  906. }
  907. static int pci1xxxx_i2c_resume(struct device *dev)
  908. {
  909. struct pci1xxxx_i2c *i2c = dev_get_drvdata(dev);
  910. void __iomem *p1 = i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF;
  911. void __iomem *p2 = i2c->i2c_base + SMBUS_RESET_REG;
  912. struct pci_dev *pdev = to_pci_dev(dev);
  913. u32 regval;
  914. regval = readw(p1);
  915. writew(regval, p1);
  916. pci1xxxx_i2c_config_high_level_intr(i2c, SMBALERT_WAKE_INTR_MASK, false);
  917. regval = readl(p2);
  918. regval &= ~PERI_SMBUS_D3_RESET_DIS;
  919. writel(regval, p2);
  920. i2c_mark_adapter_resumed(&i2c->adap);
  921. pci_wake_from_d3(pdev, false);
  922. return 0;
  923. }
  924. static DEFINE_SIMPLE_DEV_PM_OPS(pci1xxxx_i2c_pm_ops, pci1xxxx_i2c_suspend,
  925. pci1xxxx_i2c_resume);
  926. static void pci1xxxx_i2c_shutdown(void *data)
  927. {
  928. struct pci1xxxx_i2c *i2c = data;
  929. pci1xxxx_i2c_config_padctrl(i2c, false);
  930. pci1xxxx_i2c_configure_core_reg(i2c, false);
  931. }
  932. static int pci1xxxx_i2c_probe_pci(struct pci_dev *pdev,
  933. const struct pci_device_id *ent)
  934. {
  935. struct device *dev = &pdev->dev;
  936. struct pci1xxxx_i2c *i2c;
  937. int ret;
  938. i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
  939. if (!i2c)
  940. return -ENOMEM;
  941. pci_set_drvdata(pdev, i2c);
  942. i2c->i2c_xfer_in_progress = false;
  943. ret = pcim_enable_device(pdev);
  944. if (ret)
  945. return ret;
  946. pci_set_master(pdev);
  947. /*
  948. * We are getting the base address of the SMB core. SMB core uses
  949. * BAR0 and size is 32K.
  950. */
  951. ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
  952. if (ret < 0)
  953. return ret;
  954. i2c->i2c_base = pcim_iomap_table(pdev)[0];
  955. init_completion(&i2c->i2c_xfer_done);
  956. pci1xxxx_i2c_init(i2c);
  957. ret = devm_add_action(dev, pci1xxxx_i2c_shutdown, i2c);
  958. if (ret)
  959. return ret;
  960. ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  961. if (ret < 0)
  962. return ret;
  963. ret = devm_request_irq(dev, pci_irq_vector(pdev, 0), pci1xxxx_i2c_isr,
  964. 0, pci_name(pdev), i2c);
  965. if (ret)
  966. return ret;
  967. i2c->adap = pci1xxxx_i2c_ops;
  968. i2c->adap.dev.parent = dev;
  969. snprintf(i2c->adap.name, sizeof(i2c->adap.name),
  970. "MCHP PCI1xxxx i2c adapter at %s", pci_name(pdev));
  971. i2c_set_adapdata(&i2c->adap, i2c);
  972. ret = devm_i2c_add_adapter(dev, &i2c->adap);
  973. if (ret)
  974. return dev_err_probe(dev, ret, "i2c add adapter failed\n");
  975. return 0;
  976. }
  977. static const struct pci_device_id pci1xxxx_i2c_pci_id_table[] = {
  978. { PCI_VDEVICE(EFAR, 0xA003) },
  979. { PCI_VDEVICE(EFAR, 0xA013) },
  980. { PCI_VDEVICE(EFAR, 0xA023) },
  981. { PCI_VDEVICE(EFAR, 0xA033) },
  982. { PCI_VDEVICE(EFAR, 0xA043) },
  983. { }
  984. };
  985. MODULE_DEVICE_TABLE(pci, pci1xxxx_i2c_pci_id_table);
  986. static struct pci_driver pci1xxxx_i2c_pci_driver = {
  987. .name = "i2c-mchp-pci1xxxx",
  988. .id_table = pci1xxxx_i2c_pci_id_table,
  989. .probe = pci1xxxx_i2c_probe_pci,
  990. .driver = {
  991. .pm = pm_sleep_ptr(&pci1xxxx_i2c_pm_ops),
  992. },
  993. };
  994. module_pci_driver(pci1xxxx_i2c_pci_driver);
  995. MODULE_LICENSE("GPL");
  996. MODULE_AUTHOR("Tharun Kumar P<[email protected]>");
  997. MODULE_AUTHOR("Kumaravel Thiagarajan <[email protected]>");
  998. MODULE_DESCRIPTION("Microchip Technology Inc. pci1xxxx I2C bus driver");