i2c-tegra.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/i2c/busses/i2c-tegra.c
  4. *
  5. * Copyright (C) 2010 Google, Inc.
  6. * Author: Colin Cross <[email protected]>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ktime.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/reset.h>
  29. #define BYTES_PER_FIFO_WORD 4
  30. #define I2C_CNFG 0x000
  31. #define I2C_CNFG_DEBOUNCE_CNT GENMASK(14, 12)
  32. #define I2C_CNFG_PACKET_MODE_EN BIT(10)
  33. #define I2C_CNFG_NEW_MASTER_FSM BIT(11)
  34. #define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
  35. #define I2C_STATUS 0x01c
  36. #define I2C_SL_CNFG 0x020
  37. #define I2C_SL_CNFG_NACK BIT(1)
  38. #define I2C_SL_CNFG_NEWSL BIT(2)
  39. #define I2C_SL_ADDR1 0x02c
  40. #define I2C_SL_ADDR2 0x030
  41. #define I2C_TLOW_SEXT 0x034
  42. #define I2C_TX_FIFO 0x050
  43. #define I2C_RX_FIFO 0x054
  44. #define I2C_PACKET_TRANSFER_STATUS 0x058
  45. #define I2C_FIFO_CONTROL 0x05c
  46. #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
  47. #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
  48. #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
  49. #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
  50. #define I2C_FIFO_STATUS 0x060
  51. #define I2C_FIFO_STATUS_TX GENMASK(7, 4)
  52. #define I2C_FIFO_STATUS_RX GENMASK(3, 0)
  53. #define I2C_INT_MASK 0x064
  54. #define I2C_INT_STATUS 0x068
  55. #define I2C_INT_BUS_CLR_DONE BIT(11)
  56. #define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
  57. #define I2C_INT_NO_ACK BIT(3)
  58. #define I2C_INT_ARBITRATION_LOST BIT(2)
  59. #define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
  60. #define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
  61. #define I2C_CLK_DIVISOR 0x06c
  62. #define I2C_CLK_DIVISOR_STD_FAST_MODE GENMASK(31, 16)
  63. #define I2C_CLK_DIVISOR_HSMODE GENMASK(15, 0)
  64. #define DVC_CTRL_REG1 0x000
  65. #define DVC_CTRL_REG1_INTR_EN BIT(10)
  66. #define DVC_CTRL_REG3 0x008
  67. #define DVC_CTRL_REG3_SW_PROG BIT(26)
  68. #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
  69. #define DVC_STATUS 0x00c
  70. #define DVC_STATUS_I2C_DONE_INTR BIT(30)
  71. #define I2C_ERR_NONE 0x00
  72. #define I2C_ERR_NO_ACK BIT(0)
  73. #define I2C_ERR_ARBITRATION_LOST BIT(1)
  74. #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2)
  75. #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3)
  76. #define PACKET_HEADER0_HEADER_SIZE GENMASK(29, 28)
  77. #define PACKET_HEADER0_PACKET_ID GENMASK(23, 16)
  78. #define PACKET_HEADER0_CONT_ID GENMASK(15, 12)
  79. #define PACKET_HEADER0_PROTOCOL GENMASK(7, 4)
  80. #define PACKET_HEADER0_PROTOCOL_I2C 1
  81. #define I2C_HEADER_CONT_ON_NAK BIT(21)
  82. #define I2C_HEADER_READ BIT(19)
  83. #define I2C_HEADER_10BIT_ADDR BIT(18)
  84. #define I2C_HEADER_IE_ENABLE BIT(17)
  85. #define I2C_HEADER_REPEAT_START BIT(16)
  86. #define I2C_HEADER_CONTINUE_XFER BIT(15)
  87. #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
  88. #define I2C_BUS_CLEAR_CNFG 0x084
  89. #define I2C_BC_SCLK_THRESHOLD GENMASK(23, 16)
  90. #define I2C_BC_STOP_COND BIT(2)
  91. #define I2C_BC_TERMINATE BIT(1)
  92. #define I2C_BC_ENABLE BIT(0)
  93. #define I2C_BUS_CLEAR_STATUS 0x088
  94. #define I2C_BC_STATUS BIT(0)
  95. #define I2C_CONFIG_LOAD 0x08c
  96. #define I2C_MSTR_CONFIG_LOAD BIT(0)
  97. #define I2C_CLKEN_OVERRIDE 0x090
  98. #define I2C_MST_CORE_CLKEN_OVR BIT(0)
  99. #define I2C_INTERFACE_TIMING_0 0x094
  100. #define I2C_INTERFACE_TIMING_THIGH GENMASK(13, 8)
  101. #define I2C_INTERFACE_TIMING_TLOW GENMASK(5, 0)
  102. #define I2C_INTERFACE_TIMING_1 0x098
  103. #define I2C_INTERFACE_TIMING_TBUF GENMASK(29, 24)
  104. #define I2C_INTERFACE_TIMING_TSU_STO GENMASK(21, 16)
  105. #define I2C_INTERFACE_TIMING_THD_STA GENMASK(13, 8)
  106. #define I2C_INTERFACE_TIMING_TSU_STA GENMASK(5, 0)
  107. #define I2C_HS_INTERFACE_TIMING_0 0x09c
  108. #define I2C_HS_INTERFACE_TIMING_THIGH GENMASK(13, 8)
  109. #define I2C_HS_INTERFACE_TIMING_TLOW GENMASK(5, 0)
  110. #define I2C_HS_INTERFACE_TIMING_1 0x0a0
  111. #define I2C_HS_INTERFACE_TIMING_TSU_STO GENMASK(21, 16)
  112. #define I2C_HS_INTERFACE_TIMING_THD_STA GENMASK(13, 8)
  113. #define I2C_HS_INTERFACE_TIMING_TSU_STA GENMASK(5, 0)
  114. #define I2C_MST_FIFO_CONTROL 0x0b4
  115. #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
  116. #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
  117. #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
  118. #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
  119. #define I2C_MST_FIFO_STATUS 0x0b8
  120. #define I2C_MST_FIFO_STATUS_TX GENMASK(23, 16)
  121. #define I2C_MST_FIFO_STATUS_RX GENMASK(7, 0)
  122. /* configuration load timeout in microseconds */
  123. #define I2C_CONFIG_LOAD_TIMEOUT 1000000
  124. /* packet header size in bytes */
  125. #define I2C_PACKET_HEADER_SIZE 12
  126. /*
  127. * I2C Controller will use PIO mode for transfers up to 32 bytes in order to
  128. * avoid DMA overhead, otherwise external APB DMA controller will be used.
  129. * Note that the actual MAX PIO length is 20 bytes because 32 bytes include
  130. * I2C_PACKET_HEADER_SIZE.
  131. */
  132. #define I2C_PIO_MODE_PREFERRED_LEN 32
  133. /*
  134. * msg_end_type: The bus control which needs to be sent at end of transfer.
  135. * @MSG_END_STOP: Send stop pulse.
  136. * @MSG_END_REPEAT_START: Send repeat-start.
  137. * @MSG_END_CONTINUE: Don't send stop or repeat-start.
  138. */
  139. enum msg_end_type {
  140. MSG_END_STOP,
  141. MSG_END_REPEAT_START,
  142. MSG_END_CONTINUE,
  143. };
  144. /**
  145. * struct tegra_i2c_hw_feature : per hardware generation features
  146. * @has_continue_xfer_support: continue-transfer supported
  147. * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
  148. * completion interrupt on per packet basis.
  149. * @has_config_load_reg: Has the config load register to load the new
  150. * configuration.
  151. * @clk_divisor_hs_mode: Clock divisor in HS mode.
  152. * @clk_divisor_std_mode: Clock divisor in standard mode. It is
  153. * applicable if there is no fast clock source i.e. single clock
  154. * source.
  155. * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
  156. * applicable if there is no fast clock source i.e. single clock
  157. * source.
  158. * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
  159. * applicable if there is no fast clock source (i.e. single
  160. * clock source).
  161. * @has_multi_master_mode: The I2C controller supports running in single-master
  162. * or multi-master mode.
  163. * @has_slcg_override_reg: The I2C controller supports a register that
  164. * overrides the second level clock gating.
  165. * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
  166. * provides additional features and allows for longer messages to
  167. * be transferred in one go.
  168. * @quirks: I2C adapter quirks for limiting write/read transfer size and not
  169. * allowing 0 length transfers.
  170. * @supports_bus_clear: Bus Clear support to recover from bus hang during
  171. * SDA stuck low from device for some unknown reasons.
  172. * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
  173. * @tlow_std_mode: Low period of the clock in standard mode.
  174. * @thigh_std_mode: High period of the clock in standard mode.
  175. * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
  176. * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
  177. * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
  178. * in standard mode.
  179. * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
  180. * conditions in fast/fast-plus modes.
  181. * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
  182. * in HS mode.
  183. * @has_interface_timing_reg: Has interface timing register to program the tuned
  184. * timing settings.
  185. */
  186. struct tegra_i2c_hw_feature {
  187. bool has_continue_xfer_support;
  188. bool has_per_pkt_xfer_complete_irq;
  189. bool has_config_load_reg;
  190. u32 clk_divisor_hs_mode;
  191. u32 clk_divisor_std_mode;
  192. u32 clk_divisor_fast_mode;
  193. u32 clk_divisor_fast_plus_mode;
  194. bool has_multi_master_mode;
  195. bool has_slcg_override_reg;
  196. bool has_mst_fifo;
  197. const struct i2c_adapter_quirks *quirks;
  198. bool supports_bus_clear;
  199. bool has_apb_dma;
  200. u32 tlow_std_mode;
  201. u32 thigh_std_mode;
  202. u32 tlow_fast_fastplus_mode;
  203. u32 thigh_fast_fastplus_mode;
  204. u32 setup_hold_time_std_mode;
  205. u32 setup_hold_time_fast_fast_plus_mode;
  206. u32 setup_hold_time_hs_mode;
  207. bool has_interface_timing_reg;
  208. };
  209. /**
  210. * struct tegra_i2c_dev - per device I2C context
  211. * @dev: device reference for power management
  212. * @hw: Tegra I2C HW feature
  213. * @adapter: core I2C layer adapter information
  214. * @div_clk: clock reference for div clock of I2C controller
  215. * @clocks: array of I2C controller clocks
  216. * @nclocks: number of clocks in the array
  217. * @rst: reset control for the I2C controller
  218. * @base: ioremapped registers cookie
  219. * @base_phys: physical base address of the I2C controller
  220. * @cont_id: I2C controller ID, used for packet header
  221. * @irq: IRQ number of transfer complete interrupt
  222. * @is_dvc: identifies the DVC I2C controller, has a different register layout
  223. * @is_vi: identifies the VI I2C controller, has a different register layout
  224. * @msg_complete: transfer completion notifier
  225. * @msg_buf_remaining: size of unsent data in the message buffer
  226. * @msg_len: length of message in current transfer
  227. * @msg_err: error code for completed message
  228. * @msg_buf: pointer to current message data
  229. * @msg_read: indicates that the transfer is a read access
  230. * @timings: i2c timings information like bus frequency
  231. * @multimaster_mode: indicates that I2C controller is in multi-master mode
  232. * @tx_dma_chan: DMA transmit channel
  233. * @rx_dma_chan: DMA receive channel
  234. * @dma_phys: handle to DMA resources
  235. * @dma_buf: pointer to allocated DMA buffer
  236. * @dma_buf_size: DMA buffer size
  237. * @dma_mode: indicates active DMA transfer
  238. * @dma_complete: DMA completion notifier
  239. * @atomic_mode: indicates active atomic transfer
  240. */
  241. struct tegra_i2c_dev {
  242. struct device *dev;
  243. struct i2c_adapter adapter;
  244. const struct tegra_i2c_hw_feature *hw;
  245. struct reset_control *rst;
  246. unsigned int cont_id;
  247. unsigned int irq;
  248. phys_addr_t base_phys;
  249. void __iomem *base;
  250. struct clk_bulk_data clocks[2];
  251. unsigned int nclocks;
  252. struct clk *div_clk;
  253. struct i2c_timings timings;
  254. struct completion msg_complete;
  255. size_t msg_buf_remaining;
  256. unsigned int msg_len;
  257. int msg_err;
  258. u8 *msg_buf;
  259. struct completion dma_complete;
  260. struct dma_chan *tx_dma_chan;
  261. struct dma_chan *rx_dma_chan;
  262. unsigned int dma_buf_size;
  263. struct device *dma_dev;
  264. dma_addr_t dma_phys;
  265. void *dma_buf;
  266. bool multimaster_mode;
  267. bool atomic_mode;
  268. bool dma_mode;
  269. bool msg_read;
  270. bool is_dvc;
  271. bool is_vi;
  272. };
  273. static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
  274. unsigned int reg)
  275. {
  276. writel_relaxed(val, i2c_dev->base + reg);
  277. }
  278. static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
  279. {
  280. return readl_relaxed(i2c_dev->base + reg);
  281. }
  282. /*
  283. * If necessary, i2c_writel() and i2c_readl() will offset the register
  284. * in order to talk to the I2C block inside the DVC block.
  285. */
  286. static u32 tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
  287. {
  288. if (i2c_dev->is_dvc)
  289. reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
  290. else if (i2c_dev->is_vi)
  291. reg = 0xc00 + (reg << 2);
  292. return reg;
  293. }
  294. static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned int reg)
  295. {
  296. writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  297. /* read back register to make sure that register writes completed */
  298. if (reg != I2C_TX_FIFO)
  299. readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  300. else if (i2c_dev->is_vi)
  301. readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, I2C_INT_STATUS));
  302. }
  303. static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
  304. {
  305. return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  306. }
  307. static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
  308. unsigned int reg, unsigned int len)
  309. {
  310. writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
  311. }
  312. static void i2c_writesl_vi(struct tegra_i2c_dev *i2c_dev, void *data,
  313. unsigned int reg, unsigned int len)
  314. {
  315. u32 *data32 = data;
  316. /*
  317. * VI I2C controller has known hardware bug where writes get stuck
  318. * when immediate multiple writes happen to TX_FIFO register.
  319. * Recommended software work around is to read I2C register after
  320. * each write to TX_FIFO register to flush out the data.
  321. */
  322. while (len--)
  323. i2c_writel(i2c_dev, *data32++, reg);
  324. }
  325. static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
  326. unsigned int reg, unsigned int len)
  327. {
  328. readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
  329. }
  330. static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
  331. {
  332. u32 int_mask;
  333. int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
  334. i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
  335. }
  336. static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
  337. {
  338. u32 int_mask;
  339. int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
  340. i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
  341. }
  342. static void tegra_i2c_dma_complete(void *args)
  343. {
  344. struct tegra_i2c_dev *i2c_dev = args;
  345. complete(&i2c_dev->dma_complete);
  346. }
  347. static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
  348. {
  349. struct dma_async_tx_descriptor *dma_desc;
  350. enum dma_transfer_direction dir;
  351. struct dma_chan *chan;
  352. dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
  353. reinit_completion(&i2c_dev->dma_complete);
  354. dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  355. chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan;
  356. dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys,
  357. len, dir, DMA_PREP_INTERRUPT |
  358. DMA_CTRL_ACK);
  359. if (!dma_desc) {
  360. dev_err(i2c_dev->dev, "failed to get %s DMA descriptor\n",
  361. i2c_dev->msg_read ? "RX" : "TX");
  362. return -EINVAL;
  363. }
  364. dma_desc->callback = tegra_i2c_dma_complete;
  365. dma_desc->callback_param = i2c_dev;
  366. dmaengine_submit(dma_desc);
  367. dma_async_issue_pending(chan);
  368. return 0;
  369. }
  370. static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
  371. {
  372. if (i2c_dev->dma_buf) {
  373. dma_free_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size,
  374. i2c_dev->dma_buf, i2c_dev->dma_phys);
  375. i2c_dev->dma_buf = NULL;
  376. }
  377. if (i2c_dev->tx_dma_chan) {
  378. dma_release_channel(i2c_dev->tx_dma_chan);
  379. i2c_dev->tx_dma_chan = NULL;
  380. }
  381. if (i2c_dev->rx_dma_chan) {
  382. dma_release_channel(i2c_dev->rx_dma_chan);
  383. i2c_dev->rx_dma_chan = NULL;
  384. }
  385. }
  386. static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
  387. {
  388. struct dma_chan *chan;
  389. dma_addr_t dma_phys;
  390. u32 *dma_buf;
  391. int err;
  392. if (i2c_dev->is_vi)
  393. return 0;
  394. if (i2c_dev->hw->has_apb_dma) {
  395. if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
  396. dev_dbg(i2c_dev->dev, "APB DMA support not enabled\n");
  397. return 0;
  398. }
  399. } else if (!IS_ENABLED(CONFIG_TEGRA186_GPC_DMA)) {
  400. dev_dbg(i2c_dev->dev, "GPC DMA support not enabled\n");
  401. return 0;
  402. }
  403. chan = dma_request_chan(i2c_dev->dev, "rx");
  404. if (IS_ERR(chan)) {
  405. err = PTR_ERR(chan);
  406. goto err_out;
  407. }
  408. i2c_dev->rx_dma_chan = chan;
  409. chan = dma_request_chan(i2c_dev->dev, "tx");
  410. if (IS_ERR(chan)) {
  411. err = PTR_ERR(chan);
  412. goto err_out;
  413. }
  414. i2c_dev->tx_dma_chan = chan;
  415. WARN_ON(i2c_dev->tx_dma_chan->device != i2c_dev->rx_dma_chan->device);
  416. i2c_dev->dma_dev = chan->device->dev;
  417. i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len +
  418. I2C_PACKET_HEADER_SIZE;
  419. dma_buf = dma_alloc_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size,
  420. &dma_phys, GFP_KERNEL | __GFP_NOWARN);
  421. if (!dma_buf) {
  422. dev_err(i2c_dev->dev, "failed to allocate DMA buffer\n");
  423. err = -ENOMEM;
  424. goto err_out;
  425. }
  426. i2c_dev->dma_buf = dma_buf;
  427. i2c_dev->dma_phys = dma_phys;
  428. return 0;
  429. err_out:
  430. tegra_i2c_release_dma(i2c_dev);
  431. if (err != -EPROBE_DEFER) {
  432. dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
  433. dev_err(i2c_dev->dev, "falling back to PIO\n");
  434. return 0;
  435. }
  436. return err;
  437. }
  438. /*
  439. * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
  440. * block. This block is identical to the rest of the I2C blocks, except that
  441. * it only supports master mode, it has registers moved around, and it needs
  442. * some extra init to get it into I2C mode. The register moves are handled
  443. * by i2c_readl() and i2c_writel().
  444. */
  445. static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
  446. {
  447. u32 val;
  448. val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
  449. val |= DVC_CTRL_REG3_SW_PROG;
  450. val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
  451. dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
  452. val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
  453. val |= DVC_CTRL_REG1_INTR_EN;
  454. dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
  455. }
  456. static void tegra_i2c_vi_init(struct tegra_i2c_dev *i2c_dev)
  457. {
  458. u32 value;
  459. value = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, 2) |
  460. FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, 4);
  461. i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_0);
  462. value = FIELD_PREP(I2C_INTERFACE_TIMING_TBUF, 4) |
  463. FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STO, 7) |
  464. FIELD_PREP(I2C_INTERFACE_TIMING_THD_STA, 4) |
  465. FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STA, 4);
  466. i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_1);
  467. value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, 3) |
  468. FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, 8);
  469. i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_0);
  470. value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STO, 11) |
  471. FIELD_PREP(I2C_HS_INTERFACE_TIMING_THD_STA, 11) |
  472. FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STA, 11);
  473. i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_1);
  474. value = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND;
  475. i2c_writel(i2c_dev, value, I2C_BUS_CLEAR_CNFG);
  476. i2c_writel(i2c_dev, 0x0, I2C_TLOW_SEXT);
  477. }
  478. static int tegra_i2c_poll_register(struct tegra_i2c_dev *i2c_dev,
  479. u32 reg, u32 mask, u32 delay_us,
  480. u32 timeout_us)
  481. {
  482. void __iomem *addr = i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg);
  483. u32 val;
  484. if (!i2c_dev->atomic_mode)
  485. return readl_relaxed_poll_timeout(addr, val, !(val & mask),
  486. delay_us, timeout_us);
  487. return readl_relaxed_poll_timeout_atomic(addr, val, !(val & mask),
  488. delay_us, timeout_us);
  489. }
  490. static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
  491. {
  492. u32 mask, val, offset;
  493. int err;
  494. if (i2c_dev->hw->has_mst_fifo) {
  495. mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
  496. I2C_MST_FIFO_CONTROL_RX_FLUSH;
  497. offset = I2C_MST_FIFO_CONTROL;
  498. } else {
  499. mask = I2C_FIFO_CONTROL_TX_FLUSH |
  500. I2C_FIFO_CONTROL_RX_FLUSH;
  501. offset = I2C_FIFO_CONTROL;
  502. }
  503. val = i2c_readl(i2c_dev, offset);
  504. val |= mask;
  505. i2c_writel(i2c_dev, val, offset);
  506. err = tegra_i2c_poll_register(i2c_dev, offset, mask, 1000, 1000000);
  507. if (err) {
  508. dev_err(i2c_dev->dev, "failed to flush FIFO\n");
  509. return err;
  510. }
  511. return 0;
  512. }
  513. static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
  514. {
  515. int err;
  516. if (!i2c_dev->hw->has_config_load_reg)
  517. return 0;
  518. i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
  519. err = tegra_i2c_poll_register(i2c_dev, I2C_CONFIG_LOAD, 0xffffffff,
  520. 1000, I2C_CONFIG_LOAD_TIMEOUT);
  521. if (err) {
  522. dev_err(i2c_dev->dev, "failed to load config\n");
  523. return err;
  524. }
  525. return 0;
  526. }
  527. static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
  528. {
  529. u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode;
  530. acpi_handle handle = ACPI_HANDLE(i2c_dev->dev);
  531. struct i2c_timings *t = &i2c_dev->timings;
  532. int err;
  533. /*
  534. * The reset shouldn't ever fail in practice. The failure will be a
  535. * sign of a severe problem that needs to be resolved. Still we don't
  536. * want to fail the initialization completely because this may break
  537. * kernel boot up since voltage regulators use I2C. Hence, we will
  538. * emit a noisy warning on error, which won't stay unnoticed and
  539. * won't hose machine entirely.
  540. */
  541. if (handle)
  542. err = acpi_evaluate_object(handle, "_RST", NULL, NULL);
  543. else
  544. err = reset_control_reset(i2c_dev->rst);
  545. WARN_ON_ONCE(err);
  546. if (i2c_dev->is_dvc)
  547. tegra_dvc_init(i2c_dev);
  548. val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
  549. FIELD_PREP(I2C_CNFG_DEBOUNCE_CNT, 2);
  550. if (i2c_dev->hw->has_multi_master_mode)
  551. val |= I2C_CNFG_MULTI_MASTER_MODE;
  552. i2c_writel(i2c_dev, val, I2C_CNFG);
  553. i2c_writel(i2c_dev, 0, I2C_INT_MASK);
  554. if (i2c_dev->is_vi)
  555. tegra_i2c_vi_init(i2c_dev);
  556. switch (t->bus_freq_hz) {
  557. case I2C_MAX_STANDARD_MODE_FREQ + 1 ... I2C_MAX_FAST_MODE_PLUS_FREQ:
  558. default:
  559. tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
  560. thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
  561. tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
  562. if (t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ)
  563. non_hs_mode = i2c_dev->hw->clk_divisor_fast_plus_mode;
  564. else
  565. non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode;
  566. break;
  567. case 0 ... I2C_MAX_STANDARD_MODE_FREQ:
  568. tlow = i2c_dev->hw->tlow_std_mode;
  569. thigh = i2c_dev->hw->thigh_std_mode;
  570. tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
  571. non_hs_mode = i2c_dev->hw->clk_divisor_std_mode;
  572. break;
  573. }
  574. /* make sure clock divisor programmed correctly */
  575. clk_divisor = FIELD_PREP(I2C_CLK_DIVISOR_HSMODE,
  576. i2c_dev->hw->clk_divisor_hs_mode) |
  577. FIELD_PREP(I2C_CLK_DIVISOR_STD_FAST_MODE, non_hs_mode);
  578. i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
  579. if (i2c_dev->hw->has_interface_timing_reg) {
  580. val = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, thigh) |
  581. FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, tlow);
  582. i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
  583. }
  584. /*
  585. * Configure setup and hold times only when tsu_thd is non-zero.
  586. * Otherwise, preserve the chip default values.
  587. */
  588. if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
  589. i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
  590. clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
  591. err = clk_set_rate(i2c_dev->div_clk,
  592. t->bus_freq_hz * clk_multiplier);
  593. if (err) {
  594. dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err);
  595. return err;
  596. }
  597. if (!i2c_dev->is_dvc && !i2c_dev->is_vi) {
  598. u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
  599. sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
  600. i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
  601. i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
  602. i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
  603. }
  604. err = tegra_i2c_flush_fifos(i2c_dev);
  605. if (err)
  606. return err;
  607. if (i2c_dev->multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
  608. i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
  609. err = tegra_i2c_wait_for_config_load(i2c_dev);
  610. if (err)
  611. return err;
  612. return 0;
  613. }
  614. static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
  615. {
  616. u32 cnfg;
  617. /*
  618. * NACK interrupt is generated before the I2C controller generates
  619. * the STOP condition on the bus. So, wait for 2 clock periods
  620. * before disabling the controller so that the STOP condition has
  621. * been delivered properly.
  622. */
  623. udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->timings.bus_freq_hz));
  624. cnfg = i2c_readl(i2c_dev, I2C_CNFG);
  625. if (cnfg & I2C_CNFG_PACKET_MODE_EN)
  626. i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
  627. return tegra_i2c_wait_for_config_load(i2c_dev);
  628. }
  629. static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
  630. {
  631. size_t buf_remaining = i2c_dev->msg_buf_remaining;
  632. unsigned int words_to_transfer, rx_fifo_avail;
  633. u8 *buf = i2c_dev->msg_buf;
  634. u32 val;
  635. /*
  636. * Catch overflow due to message fully sent before the check for
  637. * RX FIFO availability.
  638. */
  639. if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
  640. return -EINVAL;
  641. if (i2c_dev->hw->has_mst_fifo) {
  642. val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
  643. rx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_RX, val);
  644. } else {
  645. val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
  646. rx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_RX, val);
  647. }
  648. /* round down to exclude partial word at the end of buffer */
  649. words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
  650. if (words_to_transfer > rx_fifo_avail)
  651. words_to_transfer = rx_fifo_avail;
  652. i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
  653. buf += words_to_transfer * BYTES_PER_FIFO_WORD;
  654. buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
  655. rx_fifo_avail -= words_to_transfer;
  656. /*
  657. * If there is a partial word at the end of buffer, handle it
  658. * manually to prevent overwriting past the end of buffer.
  659. */
  660. if (rx_fifo_avail > 0 && buf_remaining > 0) {
  661. /*
  662. * buf_remaining > 3 check not needed as rx_fifo_avail == 0
  663. * when (words_to_transfer was > rx_fifo_avail) earlier
  664. * in this function.
  665. */
  666. val = i2c_readl(i2c_dev, I2C_RX_FIFO);
  667. val = cpu_to_le32(val);
  668. memcpy(buf, &val, buf_remaining);
  669. buf_remaining = 0;
  670. rx_fifo_avail--;
  671. }
  672. /* RX FIFO must be drained, otherwise it's an Overflow case. */
  673. if (WARN_ON_ONCE(rx_fifo_avail))
  674. return -EINVAL;
  675. i2c_dev->msg_buf_remaining = buf_remaining;
  676. i2c_dev->msg_buf = buf;
  677. return 0;
  678. }
  679. static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
  680. {
  681. size_t buf_remaining = i2c_dev->msg_buf_remaining;
  682. unsigned int words_to_transfer, tx_fifo_avail;
  683. u8 *buf = i2c_dev->msg_buf;
  684. u32 val;
  685. if (i2c_dev->hw->has_mst_fifo) {
  686. val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
  687. tx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_TX, val);
  688. } else {
  689. val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
  690. tx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_TX, val);
  691. }
  692. /* round down to exclude partial word at the end of buffer */
  693. words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
  694. /*
  695. * This hunk pushes 4 bytes at a time into the TX FIFO.
  696. *
  697. * It's very common to have < 4 bytes, hence there is no word
  698. * to push if we have less than 4 bytes to transfer.
  699. */
  700. if (words_to_transfer) {
  701. if (words_to_transfer > tx_fifo_avail)
  702. words_to_transfer = tx_fifo_avail;
  703. /*
  704. * Update state before writing to FIFO. Note that this may
  705. * cause us to finish writing all bytes (AKA buf_remaining
  706. * goes to 0), hence we have a potential for an interrupt
  707. * (PACKET_XFER_COMPLETE is not maskable), but GIC interrupt
  708. * is disabled at this point.
  709. */
  710. buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
  711. tx_fifo_avail -= words_to_transfer;
  712. i2c_dev->msg_buf_remaining = buf_remaining;
  713. i2c_dev->msg_buf = buf + words_to_transfer * BYTES_PER_FIFO_WORD;
  714. if (i2c_dev->is_vi)
  715. i2c_writesl_vi(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
  716. else
  717. i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
  718. buf += words_to_transfer * BYTES_PER_FIFO_WORD;
  719. }
  720. /*
  721. * If there is a partial word at the end of buffer, handle it manually
  722. * to prevent reading past the end of buffer, which could cross a page
  723. * boundary and fault.
  724. */
  725. if (tx_fifo_avail > 0 && buf_remaining > 0) {
  726. /*
  727. * buf_remaining > 3 check not needed as tx_fifo_avail == 0
  728. * when (words_to_transfer was > tx_fifo_avail) earlier
  729. * in this function for non-zero words_to_transfer.
  730. */
  731. memcpy(&val, buf, buf_remaining);
  732. val = le32_to_cpu(val);
  733. i2c_dev->msg_buf_remaining = 0;
  734. i2c_dev->msg_buf = NULL;
  735. i2c_writel(i2c_dev, val, I2C_TX_FIFO);
  736. }
  737. return 0;
  738. }
  739. static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
  740. {
  741. const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  742. struct tegra_i2c_dev *i2c_dev = dev_id;
  743. u32 status;
  744. status = i2c_readl(i2c_dev, I2C_INT_STATUS);
  745. if (status == 0) {
  746. dev_warn(i2c_dev->dev, "IRQ status 0 %08x %08x %08x\n",
  747. i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
  748. i2c_readl(i2c_dev, I2C_STATUS),
  749. i2c_readl(i2c_dev, I2C_CNFG));
  750. i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
  751. goto err;
  752. }
  753. if (status & status_err) {
  754. tegra_i2c_disable_packet_mode(i2c_dev);
  755. if (status & I2C_INT_NO_ACK)
  756. i2c_dev->msg_err |= I2C_ERR_NO_ACK;
  757. if (status & I2C_INT_ARBITRATION_LOST)
  758. i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
  759. goto err;
  760. }
  761. /*
  762. * I2C transfer is terminated during the bus clear, so skip
  763. * processing the other interrupts.
  764. */
  765. if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
  766. goto err;
  767. if (!i2c_dev->dma_mode) {
  768. if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
  769. if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
  770. /*
  771. * Overflow error condition: message fully sent,
  772. * with no XFER_COMPLETE interrupt but hardware
  773. * asks to transfer more.
  774. */
  775. i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
  776. goto err;
  777. }
  778. }
  779. if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
  780. if (i2c_dev->msg_buf_remaining)
  781. tegra_i2c_fill_tx_fifo(i2c_dev);
  782. else
  783. tegra_i2c_mask_irq(i2c_dev,
  784. I2C_INT_TX_FIFO_DATA_REQ);
  785. }
  786. }
  787. i2c_writel(i2c_dev, status, I2C_INT_STATUS);
  788. if (i2c_dev->is_dvc)
  789. dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
  790. /*
  791. * During message read XFER_COMPLETE interrupt is triggered prior to
  792. * DMA completion and during message write XFER_COMPLETE interrupt is
  793. * triggered after DMA completion.
  794. *
  795. * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer,
  796. * so forcing msg_buf_remaining to 0 in DMA mode.
  797. */
  798. if (status & I2C_INT_PACKET_XFER_COMPLETE) {
  799. if (i2c_dev->dma_mode)
  800. i2c_dev->msg_buf_remaining = 0;
  801. /*
  802. * Underflow error condition: XFER_COMPLETE before message
  803. * fully sent.
  804. */
  805. if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
  806. i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
  807. goto err;
  808. }
  809. complete(&i2c_dev->msg_complete);
  810. }
  811. goto done;
  812. err:
  813. /* mask all interrupts on error */
  814. tegra_i2c_mask_irq(i2c_dev,
  815. I2C_INT_NO_ACK |
  816. I2C_INT_ARBITRATION_LOST |
  817. I2C_INT_PACKET_XFER_COMPLETE |
  818. I2C_INT_TX_FIFO_DATA_REQ |
  819. I2C_INT_RX_FIFO_DATA_REQ);
  820. if (i2c_dev->hw->supports_bus_clear)
  821. tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
  822. i2c_writel(i2c_dev, status, I2C_INT_STATUS);
  823. if (i2c_dev->is_dvc)
  824. dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
  825. if (i2c_dev->dma_mode) {
  826. if (i2c_dev->msg_read)
  827. dmaengine_terminate_async(i2c_dev->rx_dma_chan);
  828. else
  829. dmaengine_terminate_async(i2c_dev->tx_dma_chan);
  830. complete(&i2c_dev->dma_complete);
  831. }
  832. complete(&i2c_dev->msg_complete);
  833. done:
  834. return IRQ_HANDLED;
  835. }
  836. static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
  837. size_t len)
  838. {
  839. struct dma_slave_config slv_config = {0};
  840. u32 val, reg, dma_burst, reg_offset;
  841. struct dma_chan *chan;
  842. int err;
  843. if (i2c_dev->hw->has_mst_fifo)
  844. reg = I2C_MST_FIFO_CONTROL;
  845. else
  846. reg = I2C_FIFO_CONTROL;
  847. if (i2c_dev->dma_mode) {
  848. if (len & 0xF)
  849. dma_burst = 1;
  850. else if (len & 0x10)
  851. dma_burst = 4;
  852. else
  853. dma_burst = 8;
  854. if (i2c_dev->msg_read) {
  855. chan = i2c_dev->rx_dma_chan;
  856. reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
  857. slv_config.src_addr = i2c_dev->base_phys + reg_offset;
  858. slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  859. slv_config.src_maxburst = dma_burst;
  860. if (i2c_dev->hw->has_mst_fifo)
  861. val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
  862. else
  863. val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
  864. } else {
  865. chan = i2c_dev->tx_dma_chan;
  866. reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
  867. slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
  868. slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  869. slv_config.dst_maxburst = dma_burst;
  870. if (i2c_dev->hw->has_mst_fifo)
  871. val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
  872. else
  873. val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
  874. }
  875. slv_config.device_fc = true;
  876. err = dmaengine_slave_config(chan, &slv_config);
  877. if (err) {
  878. dev_err(i2c_dev->dev, "DMA config failed: %d\n", err);
  879. dev_err(i2c_dev->dev, "falling back to PIO\n");
  880. tegra_i2c_release_dma(i2c_dev);
  881. i2c_dev->dma_mode = false;
  882. } else {
  883. goto out;
  884. }
  885. }
  886. if (i2c_dev->hw->has_mst_fifo)
  887. val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
  888. I2C_MST_FIFO_CONTROL_RX_TRIG(1);
  889. else
  890. val = I2C_FIFO_CONTROL_TX_TRIG(8) |
  891. I2C_FIFO_CONTROL_RX_TRIG(1);
  892. out:
  893. i2c_writel(i2c_dev, val, reg);
  894. }
  895. static unsigned long tegra_i2c_poll_completion(struct tegra_i2c_dev *i2c_dev,
  896. struct completion *complete,
  897. unsigned int timeout_ms)
  898. {
  899. ktime_t ktime = ktime_get();
  900. ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms);
  901. do {
  902. u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
  903. if (status)
  904. tegra_i2c_isr(i2c_dev->irq, i2c_dev);
  905. if (completion_done(complete)) {
  906. s64 delta = ktime_ms_delta(ktimeout, ktime);
  907. return msecs_to_jiffies(delta) ?: 1;
  908. }
  909. ktime = ktime_get();
  910. } while (ktime_before(ktime, ktimeout));
  911. return 0;
  912. }
  913. static unsigned long tegra_i2c_wait_completion(struct tegra_i2c_dev *i2c_dev,
  914. struct completion *complete,
  915. unsigned int timeout_ms)
  916. {
  917. unsigned long ret;
  918. if (i2c_dev->atomic_mode) {
  919. ret = tegra_i2c_poll_completion(i2c_dev, complete, timeout_ms);
  920. } else {
  921. enable_irq(i2c_dev->irq);
  922. ret = wait_for_completion_timeout(complete,
  923. msecs_to_jiffies(timeout_ms));
  924. disable_irq(i2c_dev->irq);
  925. /*
  926. * Under some rare circumstances (like running KASAN +
  927. * NFS root) CPU, which handles interrupt, may stuck in
  928. * uninterruptible state for a significant time. In this
  929. * case we will get timeout if I2C transfer is running on
  930. * a sibling CPU, despite of IRQ being raised.
  931. *
  932. * In order to handle this rare condition, the IRQ status
  933. * needs to be checked after timeout.
  934. */
  935. if (ret == 0)
  936. ret = tegra_i2c_poll_completion(i2c_dev, complete, 0);
  937. }
  938. return ret;
  939. }
  940. static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
  941. {
  942. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  943. u32 val, time_left;
  944. int err;
  945. reinit_completion(&i2c_dev->msg_complete);
  946. val = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND |
  947. I2C_BC_TERMINATE;
  948. i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
  949. err = tegra_i2c_wait_for_config_load(i2c_dev);
  950. if (err)
  951. return err;
  952. val |= I2C_BC_ENABLE;
  953. i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
  954. tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
  955. time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 50);
  956. tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
  957. if (time_left == 0) {
  958. dev_err(i2c_dev->dev, "failed to clear bus\n");
  959. return -ETIMEDOUT;
  960. }
  961. val = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
  962. if (!(val & I2C_BC_STATUS)) {
  963. dev_err(i2c_dev->dev, "un-recovered arbitration lost\n");
  964. return -EIO;
  965. }
  966. return -EAGAIN;
  967. }
  968. static void tegra_i2c_push_packet_header(struct tegra_i2c_dev *i2c_dev,
  969. struct i2c_msg *msg,
  970. enum msg_end_type end_state)
  971. {
  972. u32 *dma_buf = i2c_dev->dma_buf;
  973. u32 packet_header;
  974. packet_header = FIELD_PREP(PACKET_HEADER0_HEADER_SIZE, 0) |
  975. FIELD_PREP(PACKET_HEADER0_PROTOCOL,
  976. PACKET_HEADER0_PROTOCOL_I2C) |
  977. FIELD_PREP(PACKET_HEADER0_CONT_ID, i2c_dev->cont_id) |
  978. FIELD_PREP(PACKET_HEADER0_PACKET_ID, 1);
  979. if (i2c_dev->dma_mode && !i2c_dev->msg_read)
  980. *dma_buf++ = packet_header;
  981. else
  982. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  983. packet_header = i2c_dev->msg_len - 1;
  984. if (i2c_dev->dma_mode && !i2c_dev->msg_read)
  985. *dma_buf++ = packet_header;
  986. else
  987. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  988. packet_header = I2C_HEADER_IE_ENABLE;
  989. if (end_state == MSG_END_CONTINUE)
  990. packet_header |= I2C_HEADER_CONTINUE_XFER;
  991. else if (end_state == MSG_END_REPEAT_START)
  992. packet_header |= I2C_HEADER_REPEAT_START;
  993. if (msg->flags & I2C_M_TEN) {
  994. packet_header |= msg->addr;
  995. packet_header |= I2C_HEADER_10BIT_ADDR;
  996. } else {
  997. packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
  998. }
  999. if (msg->flags & I2C_M_IGNORE_NAK)
  1000. packet_header |= I2C_HEADER_CONT_ON_NAK;
  1001. if (msg->flags & I2C_M_RD)
  1002. packet_header |= I2C_HEADER_READ;
  1003. if (i2c_dev->dma_mode && !i2c_dev->msg_read)
  1004. *dma_buf++ = packet_header;
  1005. else
  1006. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  1007. }
  1008. static int tegra_i2c_error_recover(struct tegra_i2c_dev *i2c_dev,
  1009. struct i2c_msg *msg)
  1010. {
  1011. if (i2c_dev->msg_err == I2C_ERR_NONE)
  1012. return 0;
  1013. tegra_i2c_init(i2c_dev);
  1014. /* start recovery upon arbitration loss in single master mode */
  1015. if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
  1016. if (!i2c_dev->multimaster_mode)
  1017. return i2c_recover_bus(&i2c_dev->adapter);
  1018. return -EAGAIN;
  1019. }
  1020. if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
  1021. if (msg->flags & I2C_M_IGNORE_NAK)
  1022. return 0;
  1023. return -EREMOTEIO;
  1024. }
  1025. return -EIO;
  1026. }
  1027. static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
  1028. struct i2c_msg *msg,
  1029. enum msg_end_type end_state)
  1030. {
  1031. unsigned long time_left, xfer_time = 100;
  1032. size_t xfer_size;
  1033. u32 int_mask;
  1034. int err;
  1035. err = tegra_i2c_flush_fifos(i2c_dev);
  1036. if (err)
  1037. return err;
  1038. i2c_dev->msg_buf = msg->buf;
  1039. i2c_dev->msg_len = msg->len;
  1040. i2c_dev->msg_err = I2C_ERR_NONE;
  1041. i2c_dev->msg_read = !!(msg->flags & I2C_M_RD);
  1042. reinit_completion(&i2c_dev->msg_complete);
  1043. /*
  1044. * For SMBUS block read command, read only 1 byte in the first transfer.
  1045. * Adjust that 1 byte for the next transfer in the msg buffer and msg
  1046. * length.
  1047. */
  1048. if (msg->flags & I2C_M_RECV_LEN) {
  1049. if (end_state == MSG_END_CONTINUE) {
  1050. i2c_dev->msg_len = 1;
  1051. } else {
  1052. i2c_dev->msg_buf += 1;
  1053. i2c_dev->msg_len -= 1;
  1054. }
  1055. }
  1056. i2c_dev->msg_buf_remaining = i2c_dev->msg_len;
  1057. if (i2c_dev->msg_read)
  1058. xfer_size = i2c_dev->msg_len;
  1059. else
  1060. xfer_size = i2c_dev->msg_len + I2C_PACKET_HEADER_SIZE;
  1061. xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
  1062. i2c_dev->dma_mode = xfer_size > I2C_PIO_MODE_PREFERRED_LEN &&
  1063. i2c_dev->dma_buf && !i2c_dev->atomic_mode;
  1064. tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
  1065. /*
  1066. * Transfer time in mSec = Total bits / transfer rate
  1067. * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
  1068. */
  1069. xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
  1070. i2c_dev->timings.bus_freq_hz);
  1071. int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  1072. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  1073. if (i2c_dev->dma_mode) {
  1074. if (i2c_dev->msg_read) {
  1075. dma_sync_single_for_device(i2c_dev->dma_dev,
  1076. i2c_dev->dma_phys,
  1077. xfer_size, DMA_FROM_DEVICE);
  1078. err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
  1079. if (err)
  1080. return err;
  1081. } else {
  1082. dma_sync_single_for_cpu(i2c_dev->dma_dev,
  1083. i2c_dev->dma_phys,
  1084. xfer_size, DMA_TO_DEVICE);
  1085. }
  1086. }
  1087. tegra_i2c_push_packet_header(i2c_dev, msg, end_state);
  1088. if (!i2c_dev->msg_read) {
  1089. if (i2c_dev->dma_mode) {
  1090. memcpy(i2c_dev->dma_buf + I2C_PACKET_HEADER_SIZE,
  1091. msg->buf, i2c_dev->msg_len);
  1092. dma_sync_single_for_device(i2c_dev->dma_dev,
  1093. i2c_dev->dma_phys,
  1094. xfer_size, DMA_TO_DEVICE);
  1095. err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
  1096. if (err)
  1097. return err;
  1098. } else {
  1099. tegra_i2c_fill_tx_fifo(i2c_dev);
  1100. }
  1101. }
  1102. if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
  1103. int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
  1104. if (!i2c_dev->dma_mode) {
  1105. if (msg->flags & I2C_M_RD)
  1106. int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
  1107. else if (i2c_dev->msg_buf_remaining)
  1108. int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
  1109. }
  1110. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  1111. dev_dbg(i2c_dev->dev, "unmasked IRQ: %02x\n",
  1112. i2c_readl(i2c_dev, I2C_INT_MASK));
  1113. if (i2c_dev->dma_mode) {
  1114. time_left = tegra_i2c_wait_completion(i2c_dev,
  1115. &i2c_dev->dma_complete,
  1116. xfer_time);
  1117. /*
  1118. * Synchronize DMA first, since dmaengine_terminate_sync()
  1119. * performs synchronization after the transfer's termination
  1120. * and we want to get a completion if transfer succeeded.
  1121. */
  1122. dmaengine_synchronize(i2c_dev->msg_read ?
  1123. i2c_dev->rx_dma_chan :
  1124. i2c_dev->tx_dma_chan);
  1125. dmaengine_terminate_sync(i2c_dev->msg_read ?
  1126. i2c_dev->rx_dma_chan :
  1127. i2c_dev->tx_dma_chan);
  1128. if (!time_left && !completion_done(&i2c_dev->dma_complete)) {
  1129. dev_err(i2c_dev->dev, "DMA transfer timed out\n");
  1130. tegra_i2c_init(i2c_dev);
  1131. return -ETIMEDOUT;
  1132. }
  1133. if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
  1134. dma_sync_single_for_cpu(i2c_dev->dma_dev,
  1135. i2c_dev->dma_phys,
  1136. xfer_size, DMA_FROM_DEVICE);
  1137. memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, i2c_dev->msg_len);
  1138. }
  1139. }
  1140. time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete,
  1141. xfer_time);
  1142. tegra_i2c_mask_irq(i2c_dev, int_mask);
  1143. if (time_left == 0) {
  1144. dev_err(i2c_dev->dev, "I2C transfer timed out\n");
  1145. tegra_i2c_init(i2c_dev);
  1146. return -ETIMEDOUT;
  1147. }
  1148. dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
  1149. time_left, completion_done(&i2c_dev->msg_complete),
  1150. i2c_dev->msg_err);
  1151. i2c_dev->dma_mode = false;
  1152. err = tegra_i2c_error_recover(i2c_dev, msg);
  1153. if (err)
  1154. return err;
  1155. return 0;
  1156. }
  1157. static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  1158. int num)
  1159. {
  1160. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  1161. int i, ret;
  1162. ret = pm_runtime_get_sync(i2c_dev->dev);
  1163. if (ret < 0) {
  1164. dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
  1165. pm_runtime_put_noidle(i2c_dev->dev);
  1166. return ret;
  1167. }
  1168. for (i = 0; i < num; i++) {
  1169. enum msg_end_type end_type = MSG_END_STOP;
  1170. if (i < (num - 1)) {
  1171. /* check whether follow up message is coming */
  1172. if (msgs[i + 1].flags & I2C_M_NOSTART)
  1173. end_type = MSG_END_CONTINUE;
  1174. else
  1175. end_type = MSG_END_REPEAT_START;
  1176. }
  1177. /* If M_RECV_LEN use ContinueXfer to read the first byte */
  1178. if (msgs[i].flags & I2C_M_RECV_LEN) {
  1179. ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], MSG_END_CONTINUE);
  1180. if (ret)
  1181. break;
  1182. /* Set the msg length from first byte */
  1183. msgs[i].len += msgs[i].buf[0];
  1184. dev_dbg(i2c_dev->dev, "reading %d bytes\n", msgs[i].len);
  1185. }
  1186. ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
  1187. if (ret)
  1188. break;
  1189. }
  1190. pm_runtime_put(i2c_dev->dev);
  1191. return ret ?: i;
  1192. }
  1193. static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
  1194. struct i2c_msg msgs[], int num)
  1195. {
  1196. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  1197. int ret;
  1198. i2c_dev->atomic_mode = true;
  1199. ret = tegra_i2c_xfer(adap, msgs, num);
  1200. i2c_dev->atomic_mode = false;
  1201. return ret;
  1202. }
  1203. static u32 tegra_i2c_func(struct i2c_adapter *adap)
  1204. {
  1205. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  1206. u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  1207. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  1208. if (i2c_dev->hw->has_continue_xfer_support)
  1209. ret |= I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  1210. return ret;
  1211. }
  1212. static const struct i2c_algorithm tegra_i2c_algo = {
  1213. .master_xfer = tegra_i2c_xfer,
  1214. .master_xfer_atomic = tegra_i2c_xfer_atomic,
  1215. .functionality = tegra_i2c_func,
  1216. };
  1217. /* payload size is only 12 bit */
  1218. static const struct i2c_adapter_quirks tegra_i2c_quirks = {
  1219. .flags = I2C_AQ_NO_ZERO_LEN,
  1220. .max_read_len = SZ_4K,
  1221. .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
  1222. };
  1223. static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
  1224. .flags = I2C_AQ_NO_ZERO_LEN,
  1225. .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
  1226. };
  1227. static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
  1228. .recover_bus = tegra_i2c_issue_bus_clear,
  1229. };
  1230. static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
  1231. .has_continue_xfer_support = false,
  1232. .has_per_pkt_xfer_complete_irq = false,
  1233. .clk_divisor_hs_mode = 3,
  1234. .clk_divisor_std_mode = 0,
  1235. .clk_divisor_fast_mode = 0,
  1236. .clk_divisor_fast_plus_mode = 0,
  1237. .has_config_load_reg = false,
  1238. .has_multi_master_mode = false,
  1239. .has_slcg_override_reg = false,
  1240. .has_mst_fifo = false,
  1241. .quirks = &tegra_i2c_quirks,
  1242. .supports_bus_clear = false,
  1243. .has_apb_dma = true,
  1244. .tlow_std_mode = 0x4,
  1245. .thigh_std_mode = 0x2,
  1246. .tlow_fast_fastplus_mode = 0x4,
  1247. .thigh_fast_fastplus_mode = 0x2,
  1248. .setup_hold_time_std_mode = 0x0,
  1249. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1250. .setup_hold_time_hs_mode = 0x0,
  1251. .has_interface_timing_reg = false,
  1252. };
  1253. static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
  1254. .has_continue_xfer_support = true,
  1255. .has_per_pkt_xfer_complete_irq = false,
  1256. .clk_divisor_hs_mode = 3,
  1257. .clk_divisor_std_mode = 0,
  1258. .clk_divisor_fast_mode = 0,
  1259. .clk_divisor_fast_plus_mode = 0,
  1260. .has_config_load_reg = false,
  1261. .has_multi_master_mode = false,
  1262. .has_slcg_override_reg = false,
  1263. .has_mst_fifo = false,
  1264. .quirks = &tegra_i2c_quirks,
  1265. .supports_bus_clear = false,
  1266. .has_apb_dma = true,
  1267. .tlow_std_mode = 0x4,
  1268. .thigh_std_mode = 0x2,
  1269. .tlow_fast_fastplus_mode = 0x4,
  1270. .thigh_fast_fastplus_mode = 0x2,
  1271. .setup_hold_time_std_mode = 0x0,
  1272. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1273. .setup_hold_time_hs_mode = 0x0,
  1274. .has_interface_timing_reg = false,
  1275. };
  1276. static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
  1277. .has_continue_xfer_support = true,
  1278. .has_per_pkt_xfer_complete_irq = true,
  1279. .clk_divisor_hs_mode = 1,
  1280. .clk_divisor_std_mode = 0x19,
  1281. .clk_divisor_fast_mode = 0x19,
  1282. .clk_divisor_fast_plus_mode = 0x10,
  1283. .has_config_load_reg = false,
  1284. .has_multi_master_mode = false,
  1285. .has_slcg_override_reg = false,
  1286. .has_mst_fifo = false,
  1287. .quirks = &tegra_i2c_quirks,
  1288. .supports_bus_clear = true,
  1289. .has_apb_dma = true,
  1290. .tlow_std_mode = 0x4,
  1291. .thigh_std_mode = 0x2,
  1292. .tlow_fast_fastplus_mode = 0x4,
  1293. .thigh_fast_fastplus_mode = 0x2,
  1294. .setup_hold_time_std_mode = 0x0,
  1295. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1296. .setup_hold_time_hs_mode = 0x0,
  1297. .has_interface_timing_reg = false,
  1298. };
  1299. static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
  1300. .has_continue_xfer_support = true,
  1301. .has_per_pkt_xfer_complete_irq = true,
  1302. .clk_divisor_hs_mode = 1,
  1303. .clk_divisor_std_mode = 0x19,
  1304. .clk_divisor_fast_mode = 0x19,
  1305. .clk_divisor_fast_plus_mode = 0x10,
  1306. .has_config_load_reg = true,
  1307. .has_multi_master_mode = false,
  1308. .has_slcg_override_reg = true,
  1309. .has_mst_fifo = false,
  1310. .quirks = &tegra_i2c_quirks,
  1311. .supports_bus_clear = true,
  1312. .has_apb_dma = true,
  1313. .tlow_std_mode = 0x4,
  1314. .thigh_std_mode = 0x2,
  1315. .tlow_fast_fastplus_mode = 0x4,
  1316. .thigh_fast_fastplus_mode = 0x2,
  1317. .setup_hold_time_std_mode = 0x0,
  1318. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1319. .setup_hold_time_hs_mode = 0x0,
  1320. .has_interface_timing_reg = true,
  1321. };
  1322. static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
  1323. .has_continue_xfer_support = true,
  1324. .has_per_pkt_xfer_complete_irq = true,
  1325. .clk_divisor_hs_mode = 1,
  1326. .clk_divisor_std_mode = 0x19,
  1327. .clk_divisor_fast_mode = 0x19,
  1328. .clk_divisor_fast_plus_mode = 0x10,
  1329. .has_config_load_reg = true,
  1330. .has_multi_master_mode = false,
  1331. .has_slcg_override_reg = true,
  1332. .has_mst_fifo = false,
  1333. .quirks = &tegra_i2c_quirks,
  1334. .supports_bus_clear = true,
  1335. .has_apb_dma = true,
  1336. .tlow_std_mode = 0x4,
  1337. .thigh_std_mode = 0x2,
  1338. .tlow_fast_fastplus_mode = 0x4,
  1339. .thigh_fast_fastplus_mode = 0x2,
  1340. .setup_hold_time_std_mode = 0,
  1341. .setup_hold_time_fast_fast_plus_mode = 0,
  1342. .setup_hold_time_hs_mode = 0,
  1343. .has_interface_timing_reg = true,
  1344. };
  1345. static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
  1346. .has_continue_xfer_support = true,
  1347. .has_per_pkt_xfer_complete_irq = true,
  1348. .clk_divisor_hs_mode = 1,
  1349. .clk_divisor_std_mode = 0x16,
  1350. .clk_divisor_fast_mode = 0x19,
  1351. .clk_divisor_fast_plus_mode = 0x10,
  1352. .has_config_load_reg = true,
  1353. .has_multi_master_mode = false,
  1354. .has_slcg_override_reg = true,
  1355. .has_mst_fifo = false,
  1356. .quirks = &tegra_i2c_quirks,
  1357. .supports_bus_clear = true,
  1358. .has_apb_dma = false,
  1359. .tlow_std_mode = 0x4,
  1360. .thigh_std_mode = 0x3,
  1361. .tlow_fast_fastplus_mode = 0x4,
  1362. .thigh_fast_fastplus_mode = 0x2,
  1363. .setup_hold_time_std_mode = 0,
  1364. .setup_hold_time_fast_fast_plus_mode = 0,
  1365. .setup_hold_time_hs_mode = 0,
  1366. .has_interface_timing_reg = true,
  1367. };
  1368. static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
  1369. .has_continue_xfer_support = true,
  1370. .has_per_pkt_xfer_complete_irq = true,
  1371. .clk_divisor_hs_mode = 1,
  1372. .clk_divisor_std_mode = 0x4f,
  1373. .clk_divisor_fast_mode = 0x3c,
  1374. .clk_divisor_fast_plus_mode = 0x16,
  1375. .has_config_load_reg = true,
  1376. .has_multi_master_mode = true,
  1377. .has_slcg_override_reg = true,
  1378. .has_mst_fifo = true,
  1379. .quirks = &tegra194_i2c_quirks,
  1380. .supports_bus_clear = true,
  1381. .has_apb_dma = false,
  1382. .tlow_std_mode = 0x8,
  1383. .thigh_std_mode = 0x7,
  1384. .tlow_fast_fastplus_mode = 0x2,
  1385. .thigh_fast_fastplus_mode = 0x2,
  1386. .setup_hold_time_std_mode = 0x08080808,
  1387. .setup_hold_time_fast_fast_plus_mode = 0x02020202,
  1388. .setup_hold_time_hs_mode = 0x090909,
  1389. .has_interface_timing_reg = true,
  1390. };
  1391. static const struct of_device_id tegra_i2c_of_match[] = {
  1392. { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
  1393. { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
  1394. { .compatible = "nvidia,tegra210-i2c-vi", .data = &tegra210_i2c_hw, },
  1395. { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
  1396. { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
  1397. { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
  1398. { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
  1399. { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
  1400. { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
  1401. {},
  1402. };
  1403. MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
  1404. static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
  1405. {
  1406. struct device_node *np = i2c_dev->dev->of_node;
  1407. bool multi_mode;
  1408. i2c_parse_fw_timings(i2c_dev->dev, &i2c_dev->timings, true);
  1409. multi_mode = device_property_read_bool(i2c_dev->dev, "multi-master");
  1410. i2c_dev->multimaster_mode = multi_mode;
  1411. if (of_device_is_compatible(np, "nvidia,tegra20-i2c-dvc"))
  1412. i2c_dev->is_dvc = true;
  1413. if (of_device_is_compatible(np, "nvidia,tegra210-i2c-vi"))
  1414. i2c_dev->is_vi = true;
  1415. }
  1416. static int tegra_i2c_init_reset(struct tegra_i2c_dev *i2c_dev)
  1417. {
  1418. if (ACPI_HANDLE(i2c_dev->dev))
  1419. return 0;
  1420. i2c_dev->rst = devm_reset_control_get_exclusive(i2c_dev->dev, "i2c");
  1421. if (IS_ERR(i2c_dev->rst))
  1422. return dev_err_probe(i2c_dev->dev, PTR_ERR(i2c_dev->rst),
  1423. "failed to get reset control\n");
  1424. return 0;
  1425. }
  1426. static int tegra_i2c_init_clocks(struct tegra_i2c_dev *i2c_dev)
  1427. {
  1428. int err;
  1429. if (ACPI_HANDLE(i2c_dev->dev))
  1430. return 0;
  1431. i2c_dev->clocks[i2c_dev->nclocks++].id = "div-clk";
  1432. if (i2c_dev->hw == &tegra20_i2c_hw || i2c_dev->hw == &tegra30_i2c_hw)
  1433. i2c_dev->clocks[i2c_dev->nclocks++].id = "fast-clk";
  1434. if (i2c_dev->is_vi)
  1435. i2c_dev->clocks[i2c_dev->nclocks++].id = "slow";
  1436. err = devm_clk_bulk_get(i2c_dev->dev, i2c_dev->nclocks,
  1437. i2c_dev->clocks);
  1438. if (err)
  1439. return err;
  1440. err = clk_bulk_prepare(i2c_dev->nclocks, i2c_dev->clocks);
  1441. if (err)
  1442. return err;
  1443. i2c_dev->div_clk = i2c_dev->clocks[0].clk;
  1444. if (!i2c_dev->multimaster_mode)
  1445. return 0;
  1446. err = clk_enable(i2c_dev->div_clk);
  1447. if (err) {
  1448. dev_err(i2c_dev->dev, "failed to enable div-clk: %d\n", err);
  1449. goto unprepare_clocks;
  1450. }
  1451. return 0;
  1452. unprepare_clocks:
  1453. clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
  1454. return err;
  1455. }
  1456. static void tegra_i2c_release_clocks(struct tegra_i2c_dev *i2c_dev)
  1457. {
  1458. if (i2c_dev->multimaster_mode)
  1459. clk_disable(i2c_dev->div_clk);
  1460. clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
  1461. }
  1462. static int tegra_i2c_init_hardware(struct tegra_i2c_dev *i2c_dev)
  1463. {
  1464. int ret;
  1465. ret = pm_runtime_get_sync(i2c_dev->dev);
  1466. if (ret < 0)
  1467. dev_err(i2c_dev->dev, "runtime resume failed: %d\n", ret);
  1468. else
  1469. ret = tegra_i2c_init(i2c_dev);
  1470. pm_runtime_put_sync(i2c_dev->dev);
  1471. return ret;
  1472. }
  1473. static int tegra_i2c_probe(struct platform_device *pdev)
  1474. {
  1475. struct tegra_i2c_dev *i2c_dev;
  1476. struct resource *res;
  1477. int err;
  1478. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  1479. if (!i2c_dev)
  1480. return -ENOMEM;
  1481. platform_set_drvdata(pdev, i2c_dev);
  1482. init_completion(&i2c_dev->msg_complete);
  1483. init_completion(&i2c_dev->dma_complete);
  1484. i2c_dev->hw = device_get_match_data(&pdev->dev);
  1485. i2c_dev->cont_id = pdev->id;
  1486. i2c_dev->dev = &pdev->dev;
  1487. i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1488. if (IS_ERR(i2c_dev->base))
  1489. return PTR_ERR(i2c_dev->base);
  1490. i2c_dev->base_phys = res->start;
  1491. err = platform_get_irq(pdev, 0);
  1492. if (err < 0)
  1493. return err;
  1494. i2c_dev->irq = err;
  1495. /* interrupt will be enabled during of transfer time */
  1496. irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);
  1497. err = devm_request_threaded_irq(i2c_dev->dev, i2c_dev->irq,
  1498. NULL, tegra_i2c_isr,
  1499. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1500. dev_name(i2c_dev->dev), i2c_dev);
  1501. if (err)
  1502. return err;
  1503. tegra_i2c_parse_dt(i2c_dev);
  1504. err = tegra_i2c_init_reset(i2c_dev);
  1505. if (err)
  1506. return err;
  1507. err = tegra_i2c_init_clocks(i2c_dev);
  1508. if (err)
  1509. return err;
  1510. err = tegra_i2c_init_dma(i2c_dev);
  1511. if (err)
  1512. goto release_clocks;
  1513. /*
  1514. * VI I2C is in VE power domain which is not always ON and not
  1515. * IRQ-safe. Thus, IRQ-safe device shouldn't be attached to a
  1516. * non IRQ-safe domain because this prevents powering off the power
  1517. * domain.
  1518. *
  1519. * VI I2C device shouldn't be marked as IRQ-safe because VI I2C won't
  1520. * be used for atomic transfers.
  1521. */
  1522. if (!i2c_dev->is_vi)
  1523. pm_runtime_irq_safe(i2c_dev->dev);
  1524. pm_runtime_enable(i2c_dev->dev);
  1525. err = tegra_i2c_init_hardware(i2c_dev);
  1526. if (err)
  1527. goto release_rpm;
  1528. i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
  1529. i2c_dev->adapter.dev.of_node = i2c_dev->dev->of_node;
  1530. i2c_dev->adapter.dev.parent = i2c_dev->dev;
  1531. i2c_dev->adapter.retries = 1;
  1532. i2c_dev->adapter.timeout = 6 * HZ;
  1533. i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
  1534. i2c_dev->adapter.owner = THIS_MODULE;
  1535. i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
  1536. i2c_dev->adapter.algo = &tegra_i2c_algo;
  1537. i2c_dev->adapter.nr = pdev->id;
  1538. if (i2c_dev->hw->supports_bus_clear)
  1539. i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
  1540. strscpy(i2c_dev->adapter.name, dev_name(i2c_dev->dev),
  1541. sizeof(i2c_dev->adapter.name));
  1542. err = i2c_add_numbered_adapter(&i2c_dev->adapter);
  1543. if (err)
  1544. goto release_rpm;
  1545. return 0;
  1546. release_rpm:
  1547. pm_runtime_disable(i2c_dev->dev);
  1548. tegra_i2c_release_dma(i2c_dev);
  1549. release_clocks:
  1550. tegra_i2c_release_clocks(i2c_dev);
  1551. return err;
  1552. }
  1553. static int tegra_i2c_remove(struct platform_device *pdev)
  1554. {
  1555. struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  1556. i2c_del_adapter(&i2c_dev->adapter);
  1557. pm_runtime_force_suspend(i2c_dev->dev);
  1558. tegra_i2c_release_dma(i2c_dev);
  1559. tegra_i2c_release_clocks(i2c_dev);
  1560. return 0;
  1561. }
  1562. static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
  1563. {
  1564. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1565. int err;
  1566. err = pinctrl_pm_select_default_state(dev);
  1567. if (err)
  1568. return err;
  1569. err = clk_bulk_enable(i2c_dev->nclocks, i2c_dev->clocks);
  1570. if (err)
  1571. return err;
  1572. /*
  1573. * VI I2C device is attached to VE power domain which goes through
  1574. * power ON/OFF during runtime PM resume/suspend, meaning that
  1575. * controller needs to be re-initialized after power ON.
  1576. */
  1577. if (i2c_dev->is_vi) {
  1578. err = tegra_i2c_init(i2c_dev);
  1579. if (err)
  1580. goto disable_clocks;
  1581. }
  1582. return 0;
  1583. disable_clocks:
  1584. clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
  1585. return err;
  1586. }
  1587. static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
  1588. {
  1589. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1590. clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
  1591. return pinctrl_pm_select_idle_state(dev);
  1592. }
  1593. static int __maybe_unused tegra_i2c_suspend(struct device *dev)
  1594. {
  1595. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1596. int err;
  1597. i2c_mark_adapter_suspended(&i2c_dev->adapter);
  1598. if (!pm_runtime_status_suspended(dev)) {
  1599. err = tegra_i2c_runtime_suspend(dev);
  1600. if (err)
  1601. return err;
  1602. }
  1603. return 0;
  1604. }
  1605. static int __maybe_unused tegra_i2c_resume(struct device *dev)
  1606. {
  1607. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1608. int err;
  1609. /*
  1610. * We need to ensure that clocks are enabled so that registers can be
  1611. * restored in tegra_i2c_init().
  1612. */
  1613. err = tegra_i2c_runtime_resume(dev);
  1614. if (err)
  1615. return err;
  1616. err = tegra_i2c_init(i2c_dev);
  1617. if (err)
  1618. return err;
  1619. /*
  1620. * In case we are runtime suspended, disable clocks again so that we
  1621. * don't unbalance the clock reference counts during the next runtime
  1622. * resume transition.
  1623. */
  1624. if (pm_runtime_status_suspended(dev)) {
  1625. err = tegra_i2c_runtime_suspend(dev);
  1626. if (err)
  1627. return err;
  1628. }
  1629. i2c_mark_adapter_resumed(&i2c_dev->adapter);
  1630. return 0;
  1631. }
  1632. static const struct dev_pm_ops tegra_i2c_pm = {
  1633. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
  1634. SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
  1635. NULL)
  1636. };
  1637. static const struct acpi_device_id tegra_i2c_acpi_match[] = {
  1638. {.id = "NVDA0101", .driver_data = (kernel_ulong_t)&tegra210_i2c_hw},
  1639. {.id = "NVDA0201", .driver_data = (kernel_ulong_t)&tegra186_i2c_hw},
  1640. {.id = "NVDA0301", .driver_data = (kernel_ulong_t)&tegra194_i2c_hw},
  1641. { }
  1642. };
  1643. MODULE_DEVICE_TABLE(acpi, tegra_i2c_acpi_match);
  1644. static struct platform_driver tegra_i2c_driver = {
  1645. .probe = tegra_i2c_probe,
  1646. .remove = tegra_i2c_remove,
  1647. .driver = {
  1648. .name = "tegra-i2c",
  1649. .of_match_table = tegra_i2c_of_match,
  1650. .acpi_match_table = tegra_i2c_acpi_match,
  1651. .pm = &tegra_i2c_pm,
  1652. },
  1653. };
  1654. module_platform_driver(tegra_i2c_driver);
  1655. MODULE_DESCRIPTION("NVIDIA Tegra I2C Bus Controller driver");
  1656. MODULE_AUTHOR("Colin Cross");
  1657. MODULE_LICENSE("GPL v2");