i2c-img-scb.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C adapter for the IMG Serial Control Bus (SCB) IP block.
  4. *
  5. * Copyright (C) 2009, 2010, 2012, 2014 Imagination Technologies Ltd.
  6. *
  7. * There are three ways that this I2C controller can be driven:
  8. *
  9. * - Raw control of the SDA and SCK signals.
  10. *
  11. * This corresponds to MODE_RAW, which takes control of the signals
  12. * directly for a certain number of clock cycles (the INT_TIMING
  13. * interrupt can be used for timing).
  14. *
  15. * - Atomic commands. A low level I2C symbol (such as generate
  16. * start/stop/ack/nack bit, generate byte, receive byte, and receive
  17. * ACK) is given to the hardware, with detection of completion by bits
  18. * in the LINESTAT register.
  19. *
  20. * This mode of operation is used by MODE_ATOMIC, which uses an I2C
  21. * state machine in the interrupt handler to compose/react to I2C
  22. * transactions using atomic mode commands, and also by MODE_SEQUENCE,
  23. * which emits a simple fixed sequence of atomic mode commands.
  24. *
  25. * Due to software control, the use of atomic commands usually results
  26. * in suboptimal use of the bus, with gaps between the I2C symbols while
  27. * the driver decides what to do next.
  28. *
  29. * - Automatic mode. A bus address, and whether to read/write is
  30. * specified, and the hardware takes care of the I2C state machine,
  31. * using a FIFO to send/receive bytes of data to an I2C slave. The
  32. * driver just has to keep the FIFO drained or filled in response to the
  33. * appropriate FIFO interrupts.
  34. *
  35. * This corresponds to MODE_AUTOMATIC, which manages the FIFOs and deals
  36. * with control of repeated start bits between I2C messages.
  37. *
  38. * Use of automatic mode and the FIFO can make much more efficient use
  39. * of the bus compared to individual atomic commands, with potentially
  40. * no wasted time between I2C symbols or I2C messages.
  41. *
  42. * In most cases MODE_AUTOMATIC is used, however if any of the messages in
  43. * a transaction are zero byte writes (e.g. used by i2cdetect for probing
  44. * the bus), MODE_ATOMIC must be used since automatic mode is normally
  45. * started by the writing of data into the FIFO.
  46. *
  47. * The other modes are used in specific circumstances where MODE_ATOMIC and
  48. * MODE_AUTOMATIC aren't appropriate. MODE_RAW is used to implement a bus
  49. * recovery routine. MODE_SEQUENCE is used to reset the bus and make sure
  50. * it is in a sane state.
  51. *
  52. * Notice that the driver implements a timer-based timeout mechanism.
  53. * The reason for this mechanism is to reduce the number of interrupts
  54. * received in automatic mode.
  55. *
  56. * The driver would get a slave event and transaction done interrupts for
  57. * each atomic mode command that gets completed. However, these events are
  58. * not needed in automatic mode, becase those atomic mode commands are
  59. * managed automatically by the hardware.
  60. *
  61. * In practice, normal I2C transactions will be complete well before you
  62. * get the timer interrupt, as the timer is re-scheduled during FIFO
  63. * maintenance and disabled after the transaction is complete.
  64. *
  65. * In this way normal automatic mode operation isn't impacted by
  66. * unnecessary interrupts, but the exceptional abort condition can still be
  67. * detected (with a slight delay).
  68. */
  69. #include <linux/bitops.h>
  70. #include <linux/clk.h>
  71. #include <linux/completion.h>
  72. #include <linux/err.h>
  73. #include <linux/i2c.h>
  74. #include <linux/init.h>
  75. #include <linux/interrupt.h>
  76. #include <linux/io.h>
  77. #include <linux/kernel.h>
  78. #include <linux/module.h>
  79. #include <linux/of_platform.h>
  80. #include <linux/platform_device.h>
  81. #include <linux/pm_runtime.h>
  82. #include <linux/slab.h>
  83. #include <linux/timer.h>
  84. /* Register offsets */
  85. #define SCB_STATUS_REG 0x00
  86. #define SCB_OVERRIDE_REG 0x04
  87. #define SCB_READ_ADDR_REG 0x08
  88. #define SCB_READ_COUNT_REG 0x0c
  89. #define SCB_WRITE_ADDR_REG 0x10
  90. #define SCB_READ_DATA_REG 0x14
  91. #define SCB_WRITE_DATA_REG 0x18
  92. #define SCB_FIFO_STATUS_REG 0x1c
  93. #define SCB_CONTROL_SOFT_RESET 0x1f
  94. #define SCB_CLK_SET_REG 0x3c
  95. #define SCB_INT_STATUS_REG 0x40
  96. #define SCB_INT_CLEAR_REG 0x44
  97. #define SCB_INT_MASK_REG 0x48
  98. #define SCB_CONTROL_REG 0x4c
  99. #define SCB_TIME_TPL_REG 0x50
  100. #define SCB_TIME_TPH_REG 0x54
  101. #define SCB_TIME_TP2S_REG 0x58
  102. #define SCB_TIME_TBI_REG 0x60
  103. #define SCB_TIME_TSL_REG 0x64
  104. #define SCB_TIME_TDL_REG 0x68
  105. #define SCB_TIME_TSDL_REG 0x6c
  106. #define SCB_TIME_TSDH_REG 0x70
  107. #define SCB_READ_XADDR_REG 0x74
  108. #define SCB_WRITE_XADDR_REG 0x78
  109. #define SCB_WRITE_COUNT_REG 0x7c
  110. #define SCB_CORE_REV_REG 0x80
  111. #define SCB_TIME_TCKH_REG 0x84
  112. #define SCB_TIME_TCKL_REG 0x88
  113. #define SCB_FIFO_FLUSH_REG 0x8c
  114. #define SCB_READ_FIFO_REG 0x94
  115. #define SCB_CLEAR_REG 0x98
  116. /* SCB_CONTROL_REG bits */
  117. #define SCB_CONTROL_CLK_ENABLE 0x1e0
  118. #define SCB_CONTROL_TRANSACTION_HALT 0x200
  119. #define FIFO_READ_FULL BIT(0)
  120. #define FIFO_READ_EMPTY BIT(1)
  121. #define FIFO_WRITE_FULL BIT(2)
  122. #define FIFO_WRITE_EMPTY BIT(3)
  123. /* SCB_CLK_SET_REG bits */
  124. #define SCB_FILT_DISABLE BIT(31)
  125. #define SCB_FILT_BYPASS BIT(30)
  126. #define SCB_FILT_INC_MASK 0x7f
  127. #define SCB_FILT_INC_SHIFT 16
  128. #define SCB_INC_MASK 0x7f
  129. #define SCB_INC_SHIFT 8
  130. /* SCB_INT_*_REG bits */
  131. #define INT_BUS_INACTIVE BIT(0)
  132. #define INT_UNEXPECTED_START BIT(1)
  133. #define INT_SCLK_LOW_TIMEOUT BIT(2)
  134. #define INT_SDAT_LOW_TIMEOUT BIT(3)
  135. #define INT_WRITE_ACK_ERR BIT(4)
  136. #define INT_ADDR_ACK_ERR BIT(5)
  137. #define INT_FIFO_FULL BIT(9)
  138. #define INT_FIFO_FILLING BIT(10)
  139. #define INT_FIFO_EMPTY BIT(11)
  140. #define INT_FIFO_EMPTYING BIT(12)
  141. #define INT_TRANSACTION_DONE BIT(15)
  142. #define INT_SLAVE_EVENT BIT(16)
  143. #define INT_MASTER_HALTED BIT(17)
  144. #define INT_TIMING BIT(18)
  145. #define INT_STOP_DETECTED BIT(19)
  146. #define INT_FIFO_FULL_FILLING (INT_FIFO_FULL | INT_FIFO_FILLING)
  147. /* Level interrupts need clearing after handling instead of before */
  148. #define INT_LEVEL 0x01e00
  149. /* Don't allow any interrupts while the clock may be off */
  150. #define INT_ENABLE_MASK_INACTIVE 0x00000
  151. /* Interrupt masks for the different driver modes */
  152. #define INT_ENABLE_MASK_RAW INT_TIMING
  153. #define INT_ENABLE_MASK_ATOMIC (INT_TRANSACTION_DONE | \
  154. INT_SLAVE_EVENT | \
  155. INT_ADDR_ACK_ERR | \
  156. INT_WRITE_ACK_ERR)
  157. #define INT_ENABLE_MASK_AUTOMATIC (INT_SCLK_LOW_TIMEOUT | \
  158. INT_ADDR_ACK_ERR | \
  159. INT_WRITE_ACK_ERR | \
  160. INT_FIFO_FULL | \
  161. INT_FIFO_FILLING | \
  162. INT_FIFO_EMPTY | \
  163. INT_MASTER_HALTED | \
  164. INT_STOP_DETECTED)
  165. #define INT_ENABLE_MASK_WAITSTOP (INT_SLAVE_EVENT | \
  166. INT_ADDR_ACK_ERR | \
  167. INT_WRITE_ACK_ERR)
  168. /* SCB_STATUS_REG fields */
  169. #define LINESTAT_SCLK_LINE_STATUS BIT(0)
  170. #define LINESTAT_SCLK_EN BIT(1)
  171. #define LINESTAT_SDAT_LINE_STATUS BIT(2)
  172. #define LINESTAT_SDAT_EN BIT(3)
  173. #define LINESTAT_DET_START_STATUS BIT(4)
  174. #define LINESTAT_DET_STOP_STATUS BIT(5)
  175. #define LINESTAT_DET_ACK_STATUS BIT(6)
  176. #define LINESTAT_DET_NACK_STATUS BIT(7)
  177. #define LINESTAT_BUS_IDLE BIT(8)
  178. #define LINESTAT_T_DONE_STATUS BIT(9)
  179. #define LINESTAT_SCLK_OUT_STATUS BIT(10)
  180. #define LINESTAT_SDAT_OUT_STATUS BIT(11)
  181. #define LINESTAT_GEN_LINE_MASK_STATUS BIT(12)
  182. #define LINESTAT_START_BIT_DET BIT(13)
  183. #define LINESTAT_STOP_BIT_DET BIT(14)
  184. #define LINESTAT_ACK_DET BIT(15)
  185. #define LINESTAT_NACK_DET BIT(16)
  186. #define LINESTAT_INPUT_HELD_V BIT(17)
  187. #define LINESTAT_ABORT_DET BIT(18)
  188. #define LINESTAT_ACK_OR_NACK_DET (LINESTAT_ACK_DET | LINESTAT_NACK_DET)
  189. #define LINESTAT_INPUT_DATA 0xff000000
  190. #define LINESTAT_INPUT_DATA_SHIFT 24
  191. #define LINESTAT_CLEAR_SHIFT 13
  192. #define LINESTAT_LATCHED (0x3f << LINESTAT_CLEAR_SHIFT)
  193. /* SCB_OVERRIDE_REG fields */
  194. #define OVERRIDE_SCLK_OVR BIT(0)
  195. #define OVERRIDE_SCLKEN_OVR BIT(1)
  196. #define OVERRIDE_SDAT_OVR BIT(2)
  197. #define OVERRIDE_SDATEN_OVR BIT(3)
  198. #define OVERRIDE_MASTER BIT(9)
  199. #define OVERRIDE_LINE_OVR_EN BIT(10)
  200. #define OVERRIDE_DIRECT BIT(11)
  201. #define OVERRIDE_CMD_SHIFT 4
  202. #define OVERRIDE_CMD_MASK 0x1f
  203. #define OVERRIDE_DATA_SHIFT 24
  204. #define OVERRIDE_SCLK_DOWN (OVERRIDE_LINE_OVR_EN | \
  205. OVERRIDE_SCLKEN_OVR)
  206. #define OVERRIDE_SCLK_UP (OVERRIDE_LINE_OVR_EN | \
  207. OVERRIDE_SCLKEN_OVR | \
  208. OVERRIDE_SCLK_OVR)
  209. #define OVERRIDE_SDAT_DOWN (OVERRIDE_LINE_OVR_EN | \
  210. OVERRIDE_SDATEN_OVR)
  211. #define OVERRIDE_SDAT_UP (OVERRIDE_LINE_OVR_EN | \
  212. OVERRIDE_SDATEN_OVR | \
  213. OVERRIDE_SDAT_OVR)
  214. /* OVERRIDE_CMD values */
  215. #define CMD_PAUSE 0x00
  216. #define CMD_GEN_DATA 0x01
  217. #define CMD_GEN_START 0x02
  218. #define CMD_GEN_STOP 0x03
  219. #define CMD_GEN_ACK 0x04
  220. #define CMD_GEN_NACK 0x05
  221. #define CMD_RET_DATA 0x08
  222. #define CMD_RET_ACK 0x09
  223. /* Fixed timing values */
  224. #define TIMEOUT_TBI 0x0
  225. #define TIMEOUT_TSL 0xffff
  226. #define TIMEOUT_TDL 0x0
  227. /* Transaction timeout */
  228. #define IMG_I2C_TIMEOUT (msecs_to_jiffies(1000))
  229. /*
  230. * Worst incs are 1 (innacurate) and 16*256 (irregular).
  231. * So a sensible inc is the logarithmic mean: 64 (2^6), which is
  232. * in the middle of the valid range (0-127).
  233. */
  234. #define SCB_OPT_INC 64
  235. /* Setup the clock enable filtering for 25 ns */
  236. #define SCB_FILT_GLITCH 25
  237. /*
  238. * Bits to return from interrupt handler functions for different modes.
  239. * This delays completion until we've finished with the registers, so that the
  240. * function waiting for completion can safely disable the clock to save power.
  241. */
  242. #define ISR_COMPLETE_M BIT(31)
  243. #define ISR_FATAL_M BIT(30)
  244. #define ISR_WAITSTOP BIT(29)
  245. #define ISR_STATUS_M 0x0000ffff /* contains +ve errno */
  246. #define ISR_COMPLETE(err) (ISR_COMPLETE_M | (ISR_STATUS_M & (err)))
  247. #define ISR_FATAL(err) (ISR_COMPLETE(err) | ISR_FATAL_M)
  248. #define IMG_I2C_PM_TIMEOUT 1000 /* ms */
  249. enum img_i2c_mode {
  250. MODE_INACTIVE,
  251. MODE_RAW,
  252. MODE_ATOMIC,
  253. MODE_AUTOMATIC,
  254. MODE_SEQUENCE,
  255. MODE_FATAL,
  256. MODE_WAITSTOP,
  257. MODE_SUSPEND,
  258. };
  259. /* Timing parameters for i2c modes (in ns) */
  260. struct img_i2c_timings {
  261. const char *name;
  262. unsigned int max_bitrate;
  263. unsigned int tckh, tckl, tsdh, tsdl;
  264. unsigned int tp2s, tpl, tph;
  265. };
  266. /* The timings array must be ordered from slower to faster */
  267. static struct img_i2c_timings timings[] = {
  268. /* Standard mode */
  269. {
  270. .name = "standard",
  271. .max_bitrate = I2C_MAX_STANDARD_MODE_FREQ,
  272. .tckh = 4000,
  273. .tckl = 4700,
  274. .tsdh = 4700,
  275. .tsdl = 8700,
  276. .tp2s = 4700,
  277. .tpl = 4700,
  278. .tph = 4000,
  279. },
  280. /* Fast mode */
  281. {
  282. .name = "fast",
  283. .max_bitrate = I2C_MAX_FAST_MODE_FREQ,
  284. .tckh = 600,
  285. .tckl = 1300,
  286. .tsdh = 600,
  287. .tsdl = 1200,
  288. .tp2s = 1300,
  289. .tpl = 600,
  290. .tph = 600,
  291. },
  292. };
  293. /* Reset dance */
  294. static u8 img_i2c_reset_seq[] = { CMD_GEN_START,
  295. CMD_GEN_DATA, 0xff,
  296. CMD_RET_ACK,
  297. CMD_GEN_START,
  298. CMD_GEN_STOP,
  299. 0 };
  300. /* Just issue a stop (after an abort condition) */
  301. static u8 img_i2c_stop_seq[] = { CMD_GEN_STOP,
  302. 0 };
  303. /* We're interested in different interrupts depending on the mode */
  304. static unsigned int img_i2c_int_enable_by_mode[] = {
  305. [MODE_INACTIVE] = INT_ENABLE_MASK_INACTIVE,
  306. [MODE_RAW] = INT_ENABLE_MASK_RAW,
  307. [MODE_ATOMIC] = INT_ENABLE_MASK_ATOMIC,
  308. [MODE_AUTOMATIC] = INT_ENABLE_MASK_AUTOMATIC,
  309. [MODE_SEQUENCE] = INT_ENABLE_MASK_ATOMIC,
  310. [MODE_FATAL] = 0,
  311. [MODE_WAITSTOP] = INT_ENABLE_MASK_WAITSTOP,
  312. [MODE_SUSPEND] = 0,
  313. };
  314. /* Atomic command names */
  315. static const char * const img_i2c_atomic_cmd_names[] = {
  316. [CMD_PAUSE] = "PAUSE",
  317. [CMD_GEN_DATA] = "GEN_DATA",
  318. [CMD_GEN_START] = "GEN_START",
  319. [CMD_GEN_STOP] = "GEN_STOP",
  320. [CMD_GEN_ACK] = "GEN_ACK",
  321. [CMD_GEN_NACK] = "GEN_NACK",
  322. [CMD_RET_DATA] = "RET_DATA",
  323. [CMD_RET_ACK] = "RET_ACK",
  324. };
  325. struct img_i2c {
  326. struct i2c_adapter adap;
  327. void __iomem *base;
  328. /*
  329. * The scb core clock is used to get the input frequency, and to disable
  330. * it after every set of transactions to save some power.
  331. */
  332. struct clk *scb_clk, *sys_clk;
  333. unsigned int bitrate;
  334. bool need_wr_rd_fence;
  335. /* state */
  336. struct completion msg_complete;
  337. spinlock_t lock; /* lock before doing anything with the state */
  338. struct i2c_msg msg;
  339. /* After the last transaction, wait for a stop bit */
  340. bool last_msg;
  341. int msg_status;
  342. enum img_i2c_mode mode;
  343. u32 int_enable; /* depends on mode */
  344. u32 line_status; /* line status over command */
  345. /*
  346. * To avoid slave event interrupts in automatic mode, use a timer to
  347. * poll the abort condition if we don't get an interrupt for too long.
  348. */
  349. struct timer_list check_timer;
  350. bool t_halt;
  351. /* atomic mode state */
  352. bool at_t_done;
  353. bool at_slave_event;
  354. int at_cur_cmd;
  355. u8 at_cur_data;
  356. /* Sequence: either reset or stop. See img_i2c_sequence. */
  357. u8 *seq;
  358. /* raw mode */
  359. unsigned int raw_timeout;
  360. };
  361. static int img_i2c_runtime_suspend(struct device *dev);
  362. static int img_i2c_runtime_resume(struct device *dev);
  363. static void img_i2c_writel(struct img_i2c *i2c, u32 offset, u32 value)
  364. {
  365. writel(value, i2c->base + offset);
  366. }
  367. static u32 img_i2c_readl(struct img_i2c *i2c, u32 offset)
  368. {
  369. return readl(i2c->base + offset);
  370. }
  371. /*
  372. * The code to read from the master read fifo, and write to the master
  373. * write fifo, checks a bit in an SCB register before every byte to
  374. * ensure that the fifo is not full (write fifo) or empty (read fifo).
  375. * Due to clock domain crossing inside the SCB block the updated value
  376. * of this bit is only visible after 2 cycles.
  377. *
  378. * The scb_wr_rd_fence() function does 2 dummy writes (to the read-only
  379. * revision register), and it's called after reading from or writing to the
  380. * fifos to ensure that subsequent reads of the fifo status bits do not read
  381. * stale values.
  382. */
  383. static void img_i2c_wr_rd_fence(struct img_i2c *i2c)
  384. {
  385. if (i2c->need_wr_rd_fence) {
  386. img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
  387. img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
  388. }
  389. }
  390. static void img_i2c_switch_mode(struct img_i2c *i2c, enum img_i2c_mode mode)
  391. {
  392. i2c->mode = mode;
  393. i2c->int_enable = img_i2c_int_enable_by_mode[mode];
  394. i2c->line_status = 0;
  395. }
  396. static void img_i2c_raw_op(struct img_i2c *i2c)
  397. {
  398. i2c->raw_timeout = 0;
  399. img_i2c_writel(i2c, SCB_OVERRIDE_REG,
  400. OVERRIDE_SCLKEN_OVR |
  401. OVERRIDE_SDATEN_OVR |
  402. OVERRIDE_MASTER |
  403. OVERRIDE_LINE_OVR_EN |
  404. OVERRIDE_DIRECT |
  405. ((i2c->at_cur_cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
  406. (i2c->at_cur_data << OVERRIDE_DATA_SHIFT));
  407. }
  408. static const char *img_i2c_atomic_op_name(unsigned int cmd)
  409. {
  410. if (unlikely(cmd >= ARRAY_SIZE(img_i2c_atomic_cmd_names)))
  411. return "UNKNOWN";
  412. return img_i2c_atomic_cmd_names[cmd];
  413. }
  414. /* Send a single atomic mode command to the hardware */
  415. static void img_i2c_atomic_op(struct img_i2c *i2c, int cmd, u8 data)
  416. {
  417. i2c->at_cur_cmd = cmd;
  418. i2c->at_cur_data = data;
  419. /* work around lack of data setup time when generating data */
  420. if (cmd == CMD_GEN_DATA && i2c->mode == MODE_ATOMIC) {
  421. u32 line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
  422. if (line_status & LINESTAT_SDAT_LINE_STATUS && !(data & 0x80)) {
  423. /* hold the data line down for a moment */
  424. img_i2c_switch_mode(i2c, MODE_RAW);
  425. img_i2c_raw_op(i2c);
  426. return;
  427. }
  428. }
  429. dev_dbg(i2c->adap.dev.parent,
  430. "atomic cmd=%s (%d) data=%#x\n",
  431. img_i2c_atomic_op_name(cmd), cmd, data);
  432. i2c->at_t_done = (cmd == CMD_RET_DATA || cmd == CMD_RET_ACK);
  433. i2c->at_slave_event = false;
  434. i2c->line_status = 0;
  435. img_i2c_writel(i2c, SCB_OVERRIDE_REG,
  436. ((cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
  437. OVERRIDE_MASTER |
  438. OVERRIDE_DIRECT |
  439. (data << OVERRIDE_DATA_SHIFT));
  440. }
  441. /* Start a transaction in atomic mode */
  442. static void img_i2c_atomic_start(struct img_i2c *i2c)
  443. {
  444. img_i2c_switch_mode(i2c, MODE_ATOMIC);
  445. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  446. img_i2c_atomic_op(i2c, CMD_GEN_START, 0x00);
  447. }
  448. static void img_i2c_soft_reset(struct img_i2c *i2c)
  449. {
  450. i2c->t_halt = false;
  451. img_i2c_writel(i2c, SCB_CONTROL_REG, 0);
  452. img_i2c_writel(i2c, SCB_CONTROL_REG,
  453. SCB_CONTROL_CLK_ENABLE | SCB_CONTROL_SOFT_RESET);
  454. }
  455. /*
  456. * Enable or release transaction halt for control of repeated starts.
  457. * In version 3.3 of the IP when transaction halt is set, an interrupt
  458. * will be generated after each byte of a transfer instead of after
  459. * every transfer but before the stop bit.
  460. * Due to this behaviour we have to be careful that every time we
  461. * release the transaction halt we have to re-enable it straight away
  462. * so that we only process a single byte, not doing so will result in
  463. * all remaining bytes been processed and a stop bit being issued,
  464. * which will prevent us having a repeated start.
  465. */
  466. static void img_i2c_transaction_halt(struct img_i2c *i2c, bool t_halt)
  467. {
  468. u32 val;
  469. if (i2c->t_halt == t_halt)
  470. return;
  471. i2c->t_halt = t_halt;
  472. val = img_i2c_readl(i2c, SCB_CONTROL_REG);
  473. if (t_halt)
  474. val |= SCB_CONTROL_TRANSACTION_HALT;
  475. else
  476. val &= ~SCB_CONTROL_TRANSACTION_HALT;
  477. img_i2c_writel(i2c, SCB_CONTROL_REG, val);
  478. }
  479. /* Drain data from the FIFO into the buffer (automatic mode) */
  480. static void img_i2c_read_fifo(struct img_i2c *i2c)
  481. {
  482. while (i2c->msg.len) {
  483. u32 fifo_status;
  484. u8 data;
  485. img_i2c_wr_rd_fence(i2c);
  486. fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
  487. if (fifo_status & FIFO_READ_EMPTY)
  488. break;
  489. data = img_i2c_readl(i2c, SCB_READ_DATA_REG);
  490. *i2c->msg.buf = data;
  491. img_i2c_writel(i2c, SCB_READ_FIFO_REG, 0xff);
  492. i2c->msg.len--;
  493. i2c->msg.buf++;
  494. }
  495. }
  496. /* Fill the FIFO with data from the buffer (automatic mode) */
  497. static void img_i2c_write_fifo(struct img_i2c *i2c)
  498. {
  499. while (i2c->msg.len) {
  500. u32 fifo_status;
  501. img_i2c_wr_rd_fence(i2c);
  502. fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
  503. if (fifo_status & FIFO_WRITE_FULL)
  504. break;
  505. img_i2c_writel(i2c, SCB_WRITE_DATA_REG, *i2c->msg.buf);
  506. i2c->msg.len--;
  507. i2c->msg.buf++;
  508. }
  509. /* Disable fifo emptying interrupt if nothing more to write */
  510. if (!i2c->msg.len)
  511. i2c->int_enable &= ~INT_FIFO_EMPTYING;
  512. }
  513. /* Start a read transaction in automatic mode */
  514. static void img_i2c_read(struct img_i2c *i2c)
  515. {
  516. img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
  517. if (!i2c->last_msg)
  518. i2c->int_enable |= INT_SLAVE_EVENT;
  519. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  520. img_i2c_writel(i2c, SCB_READ_ADDR_REG, i2c->msg.addr);
  521. img_i2c_writel(i2c, SCB_READ_COUNT_REG, i2c->msg.len);
  522. mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
  523. }
  524. /* Start a write transaction in automatic mode */
  525. static void img_i2c_write(struct img_i2c *i2c)
  526. {
  527. img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
  528. if (!i2c->last_msg)
  529. i2c->int_enable |= INT_SLAVE_EVENT;
  530. img_i2c_writel(i2c, SCB_WRITE_ADDR_REG, i2c->msg.addr);
  531. img_i2c_writel(i2c, SCB_WRITE_COUNT_REG, i2c->msg.len);
  532. mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
  533. img_i2c_write_fifo(i2c);
  534. /* img_i2c_write_fifo() may modify int_enable */
  535. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  536. }
  537. /*
  538. * Indicate that the transaction is complete. This is called from the
  539. * ISR to wake up the waiting thread, after which the ISR must not
  540. * access any more SCB registers.
  541. */
  542. static void img_i2c_complete_transaction(struct img_i2c *i2c, int status)
  543. {
  544. img_i2c_switch_mode(i2c, MODE_INACTIVE);
  545. if (status) {
  546. i2c->msg_status = status;
  547. img_i2c_transaction_halt(i2c, false);
  548. }
  549. complete(&i2c->msg_complete);
  550. }
  551. static unsigned int img_i2c_raw_atomic_delay_handler(struct img_i2c *i2c,
  552. u32 int_status, u32 line_status)
  553. {
  554. /* Stay in raw mode for this, so we don't just loop infinitely */
  555. img_i2c_atomic_op(i2c, i2c->at_cur_cmd, i2c->at_cur_data);
  556. img_i2c_switch_mode(i2c, MODE_ATOMIC);
  557. return 0;
  558. }
  559. static unsigned int img_i2c_raw(struct img_i2c *i2c, u32 int_status,
  560. u32 line_status)
  561. {
  562. if (int_status & INT_TIMING) {
  563. if (i2c->raw_timeout == 0)
  564. return img_i2c_raw_atomic_delay_handler(i2c,
  565. int_status, line_status);
  566. --i2c->raw_timeout;
  567. }
  568. return 0;
  569. }
  570. static unsigned int img_i2c_sequence(struct img_i2c *i2c, u32 int_status)
  571. {
  572. static const unsigned int continue_bits[] = {
  573. [CMD_GEN_START] = LINESTAT_START_BIT_DET,
  574. [CMD_GEN_DATA] = LINESTAT_INPUT_HELD_V,
  575. [CMD_RET_ACK] = LINESTAT_ACK_DET | LINESTAT_NACK_DET,
  576. [CMD_RET_DATA] = LINESTAT_INPUT_HELD_V,
  577. [CMD_GEN_STOP] = LINESTAT_STOP_BIT_DET,
  578. };
  579. int next_cmd = -1;
  580. u8 next_data = 0x00;
  581. if (int_status & INT_SLAVE_EVENT)
  582. i2c->at_slave_event = true;
  583. if (int_status & INT_TRANSACTION_DONE)
  584. i2c->at_t_done = true;
  585. if (!i2c->at_slave_event || !i2c->at_t_done)
  586. return 0;
  587. /* wait if no continue bits are set */
  588. if (i2c->at_cur_cmd >= 0 &&
  589. i2c->at_cur_cmd < ARRAY_SIZE(continue_bits)) {
  590. unsigned int cont_bits = continue_bits[i2c->at_cur_cmd];
  591. if (cont_bits) {
  592. cont_bits |= LINESTAT_ABORT_DET;
  593. if (!(i2c->line_status & cont_bits))
  594. return 0;
  595. }
  596. }
  597. /* follow the sequence of commands in i2c->seq */
  598. next_cmd = *i2c->seq;
  599. /* stop on a nil */
  600. if (!next_cmd) {
  601. img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
  602. return ISR_COMPLETE(0);
  603. }
  604. /* when generating data, the next byte is the data */
  605. if (next_cmd == CMD_GEN_DATA) {
  606. ++i2c->seq;
  607. next_data = *i2c->seq;
  608. }
  609. ++i2c->seq;
  610. img_i2c_atomic_op(i2c, next_cmd, next_data);
  611. return 0;
  612. }
  613. static void img_i2c_reset_start(struct img_i2c *i2c)
  614. {
  615. /* Initiate the magic dance */
  616. img_i2c_switch_mode(i2c, MODE_SEQUENCE);
  617. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  618. i2c->seq = img_i2c_reset_seq;
  619. i2c->at_slave_event = true;
  620. i2c->at_t_done = true;
  621. i2c->at_cur_cmd = -1;
  622. /* img_i2c_reset_seq isn't empty so the following won't fail */
  623. img_i2c_sequence(i2c, 0);
  624. }
  625. static void img_i2c_stop_start(struct img_i2c *i2c)
  626. {
  627. /* Initiate a stop bit sequence */
  628. img_i2c_switch_mode(i2c, MODE_SEQUENCE);
  629. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  630. i2c->seq = img_i2c_stop_seq;
  631. i2c->at_slave_event = true;
  632. i2c->at_t_done = true;
  633. i2c->at_cur_cmd = -1;
  634. /* img_i2c_stop_seq isn't empty so the following won't fail */
  635. img_i2c_sequence(i2c, 0);
  636. }
  637. static unsigned int img_i2c_atomic(struct img_i2c *i2c,
  638. u32 int_status,
  639. u32 line_status)
  640. {
  641. int next_cmd = -1;
  642. u8 next_data = 0x00;
  643. if (int_status & INT_SLAVE_EVENT)
  644. i2c->at_slave_event = true;
  645. if (int_status & INT_TRANSACTION_DONE)
  646. i2c->at_t_done = true;
  647. if (!i2c->at_slave_event || !i2c->at_t_done)
  648. goto next_atomic_cmd;
  649. if (i2c->line_status & LINESTAT_ABORT_DET) {
  650. dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
  651. next_cmd = CMD_GEN_STOP;
  652. i2c->msg_status = -EIO;
  653. goto next_atomic_cmd;
  654. }
  655. /* i2c->at_cur_cmd may have completed */
  656. switch (i2c->at_cur_cmd) {
  657. case CMD_GEN_START:
  658. next_cmd = CMD_GEN_DATA;
  659. next_data = i2c_8bit_addr_from_msg(&i2c->msg);
  660. break;
  661. case CMD_GEN_DATA:
  662. if (i2c->line_status & LINESTAT_INPUT_HELD_V)
  663. next_cmd = CMD_RET_ACK;
  664. break;
  665. case CMD_RET_ACK:
  666. if (i2c->line_status & LINESTAT_ACK_DET ||
  667. (i2c->line_status & LINESTAT_NACK_DET &&
  668. i2c->msg.flags & I2C_M_IGNORE_NAK)) {
  669. if (i2c->msg.len == 0) {
  670. next_cmd = CMD_GEN_STOP;
  671. } else if (i2c->msg.flags & I2C_M_RD) {
  672. next_cmd = CMD_RET_DATA;
  673. } else {
  674. next_cmd = CMD_GEN_DATA;
  675. next_data = *i2c->msg.buf;
  676. --i2c->msg.len;
  677. ++i2c->msg.buf;
  678. }
  679. } else if (i2c->line_status & LINESTAT_NACK_DET) {
  680. i2c->msg_status = -EIO;
  681. next_cmd = CMD_GEN_STOP;
  682. }
  683. break;
  684. case CMD_RET_DATA:
  685. if (i2c->line_status & LINESTAT_INPUT_HELD_V) {
  686. *i2c->msg.buf = (i2c->line_status &
  687. LINESTAT_INPUT_DATA)
  688. >> LINESTAT_INPUT_DATA_SHIFT;
  689. --i2c->msg.len;
  690. ++i2c->msg.buf;
  691. if (i2c->msg.len)
  692. next_cmd = CMD_GEN_ACK;
  693. else
  694. next_cmd = CMD_GEN_NACK;
  695. }
  696. break;
  697. case CMD_GEN_ACK:
  698. if (i2c->line_status & LINESTAT_ACK_DET) {
  699. next_cmd = CMD_RET_DATA;
  700. } else {
  701. i2c->msg_status = -EIO;
  702. next_cmd = CMD_GEN_STOP;
  703. }
  704. break;
  705. case CMD_GEN_NACK:
  706. next_cmd = CMD_GEN_STOP;
  707. break;
  708. case CMD_GEN_STOP:
  709. img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
  710. return ISR_COMPLETE(0);
  711. default:
  712. dev_err(i2c->adap.dev.parent, "bad atomic command %d\n",
  713. i2c->at_cur_cmd);
  714. i2c->msg_status = -EIO;
  715. next_cmd = CMD_GEN_STOP;
  716. break;
  717. }
  718. next_atomic_cmd:
  719. if (next_cmd != -1) {
  720. /* don't actually stop unless we're the last transaction */
  721. if (next_cmd == CMD_GEN_STOP && !i2c->msg_status &&
  722. !i2c->last_msg)
  723. return ISR_COMPLETE(0);
  724. img_i2c_atomic_op(i2c, next_cmd, next_data);
  725. }
  726. return 0;
  727. }
  728. /*
  729. * Timer function to check if something has gone wrong in automatic mode (so we
  730. * don't have to handle so many interrupts just to catch an exception).
  731. */
  732. static void img_i2c_check_timer(struct timer_list *t)
  733. {
  734. struct img_i2c *i2c = from_timer(i2c, t, check_timer);
  735. unsigned long flags;
  736. unsigned int line_status;
  737. spin_lock_irqsave(&i2c->lock, flags);
  738. line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
  739. /* check for an abort condition */
  740. if (line_status & LINESTAT_ABORT_DET) {
  741. dev_dbg(i2c->adap.dev.parent,
  742. "abort condition detected by check timer\n");
  743. /* enable slave event interrupt mask to trigger irq */
  744. img_i2c_writel(i2c, SCB_INT_MASK_REG,
  745. i2c->int_enable | INT_SLAVE_EVENT);
  746. }
  747. spin_unlock_irqrestore(&i2c->lock, flags);
  748. }
  749. static unsigned int img_i2c_auto(struct img_i2c *i2c,
  750. unsigned int int_status,
  751. unsigned int line_status)
  752. {
  753. if (int_status & (INT_WRITE_ACK_ERR | INT_ADDR_ACK_ERR))
  754. return ISR_COMPLETE(EIO);
  755. if (line_status & LINESTAT_ABORT_DET) {
  756. dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
  757. /* empty the read fifo */
  758. if ((i2c->msg.flags & I2C_M_RD) &&
  759. (int_status & INT_FIFO_FULL_FILLING))
  760. img_i2c_read_fifo(i2c);
  761. /* use atomic mode and try to force a stop bit */
  762. i2c->msg_status = -EIO;
  763. img_i2c_stop_start(i2c);
  764. return 0;
  765. }
  766. /* Enable transaction halt on start bit */
  767. if (!i2c->last_msg && line_status & LINESTAT_START_BIT_DET) {
  768. img_i2c_transaction_halt(i2c, !i2c->last_msg);
  769. /* we're no longer interested in the slave event */
  770. i2c->int_enable &= ~INT_SLAVE_EVENT;
  771. }
  772. mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
  773. if (int_status & INT_STOP_DETECTED) {
  774. /* Drain remaining data in FIFO and complete transaction */
  775. if (i2c->msg.flags & I2C_M_RD)
  776. img_i2c_read_fifo(i2c);
  777. return ISR_COMPLETE(0);
  778. }
  779. if (i2c->msg.flags & I2C_M_RD) {
  780. if (int_status & (INT_FIFO_FULL_FILLING | INT_MASTER_HALTED)) {
  781. img_i2c_read_fifo(i2c);
  782. if (i2c->msg.len == 0)
  783. return ISR_WAITSTOP;
  784. }
  785. } else {
  786. if (int_status & (INT_FIFO_EMPTY | INT_MASTER_HALTED)) {
  787. if ((int_status & INT_FIFO_EMPTY) &&
  788. i2c->msg.len == 0)
  789. return ISR_WAITSTOP;
  790. img_i2c_write_fifo(i2c);
  791. }
  792. }
  793. if (int_status & INT_MASTER_HALTED) {
  794. /*
  795. * Release and then enable transaction halt, to
  796. * allow only a single byte to proceed.
  797. */
  798. img_i2c_transaction_halt(i2c, false);
  799. img_i2c_transaction_halt(i2c, !i2c->last_msg);
  800. }
  801. return 0;
  802. }
  803. static irqreturn_t img_i2c_isr(int irq, void *dev_id)
  804. {
  805. struct img_i2c *i2c = (struct img_i2c *)dev_id;
  806. u32 int_status, line_status;
  807. /* We handle transaction completion AFTER accessing registers */
  808. unsigned int hret;
  809. /* Read interrupt status register. */
  810. int_status = img_i2c_readl(i2c, SCB_INT_STATUS_REG);
  811. /* Clear detected interrupts. */
  812. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status);
  813. /*
  814. * Read line status and clear it until it actually is clear. We have
  815. * to be careful not to lose any line status bits that get latched.
  816. */
  817. line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
  818. if (line_status & LINESTAT_LATCHED) {
  819. img_i2c_writel(i2c, SCB_CLEAR_REG,
  820. (line_status & LINESTAT_LATCHED)
  821. >> LINESTAT_CLEAR_SHIFT);
  822. img_i2c_wr_rd_fence(i2c);
  823. }
  824. spin_lock(&i2c->lock);
  825. /* Keep track of line status bits received */
  826. i2c->line_status &= ~LINESTAT_INPUT_DATA;
  827. i2c->line_status |= line_status;
  828. /*
  829. * Certain interrupts indicate that sclk low timeout is not
  830. * a problem. If any of these are set, just continue.
  831. */
  832. if ((int_status & INT_SCLK_LOW_TIMEOUT) &&
  833. !(int_status & (INT_SLAVE_EVENT |
  834. INT_FIFO_EMPTY |
  835. INT_FIFO_FULL))) {
  836. dev_crit(i2c->adap.dev.parent,
  837. "fatal: clock low timeout occurred %s addr 0x%02x\n",
  838. (i2c->msg.flags & I2C_M_RD) ? "reading" : "writing",
  839. i2c->msg.addr);
  840. hret = ISR_FATAL(EIO);
  841. goto out;
  842. }
  843. if (i2c->mode == MODE_ATOMIC)
  844. hret = img_i2c_atomic(i2c, int_status, line_status);
  845. else if (i2c->mode == MODE_AUTOMATIC)
  846. hret = img_i2c_auto(i2c, int_status, line_status);
  847. else if (i2c->mode == MODE_SEQUENCE)
  848. hret = img_i2c_sequence(i2c, int_status);
  849. else if (i2c->mode == MODE_WAITSTOP && (int_status & INT_SLAVE_EVENT) &&
  850. (line_status & LINESTAT_STOP_BIT_DET))
  851. hret = ISR_COMPLETE(0);
  852. else if (i2c->mode == MODE_RAW)
  853. hret = img_i2c_raw(i2c, int_status, line_status);
  854. else
  855. hret = 0;
  856. /* Clear detected level interrupts. */
  857. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status & INT_LEVEL);
  858. out:
  859. if (hret & ISR_WAITSTOP) {
  860. /*
  861. * Only wait for stop on last message.
  862. * Also we may already have detected the stop bit.
  863. */
  864. if (!i2c->last_msg || i2c->line_status & LINESTAT_STOP_BIT_DET)
  865. hret = ISR_COMPLETE(0);
  866. else
  867. img_i2c_switch_mode(i2c, MODE_WAITSTOP);
  868. }
  869. /* now we've finished using regs, handle transaction completion */
  870. if (hret & ISR_COMPLETE_M) {
  871. int status = -(hret & ISR_STATUS_M);
  872. img_i2c_complete_transaction(i2c, status);
  873. if (hret & ISR_FATAL_M)
  874. img_i2c_switch_mode(i2c, MODE_FATAL);
  875. }
  876. /* Enable interrupts (int_enable may be altered by changing mode) */
  877. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  878. spin_unlock(&i2c->lock);
  879. return IRQ_HANDLED;
  880. }
  881. /* Force a bus reset sequence and wait for it to complete */
  882. static int img_i2c_reset_bus(struct img_i2c *i2c)
  883. {
  884. unsigned long flags;
  885. unsigned long time_left;
  886. spin_lock_irqsave(&i2c->lock, flags);
  887. reinit_completion(&i2c->msg_complete);
  888. img_i2c_reset_start(i2c);
  889. spin_unlock_irqrestore(&i2c->lock, flags);
  890. time_left = wait_for_completion_timeout(&i2c->msg_complete,
  891. IMG_I2C_TIMEOUT);
  892. if (time_left == 0)
  893. return -ETIMEDOUT;
  894. return 0;
  895. }
  896. static int img_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  897. int num)
  898. {
  899. struct img_i2c *i2c = i2c_get_adapdata(adap);
  900. bool atomic = false;
  901. int i, ret;
  902. unsigned long time_left;
  903. if (i2c->mode == MODE_SUSPEND) {
  904. WARN(1, "refusing to service transaction in suspended state\n");
  905. return -EIO;
  906. }
  907. if (i2c->mode == MODE_FATAL)
  908. return -EIO;
  909. for (i = 0; i < num; i++) {
  910. /*
  911. * 0 byte reads are not possible because the slave could try
  912. * and pull the data line low, preventing a stop bit.
  913. */
  914. if (!msgs[i].len && msgs[i].flags & I2C_M_RD)
  915. return -EIO;
  916. /*
  917. * 0 byte writes are possible and used for probing, but we
  918. * cannot do them in automatic mode, so use atomic mode
  919. * instead.
  920. *
  921. * Also, the I2C_M_IGNORE_NAK mode can only be implemented
  922. * in atomic mode.
  923. */
  924. if (!msgs[i].len ||
  925. (msgs[i].flags & I2C_M_IGNORE_NAK))
  926. atomic = true;
  927. }
  928. ret = pm_runtime_resume_and_get(adap->dev.parent);
  929. if (ret < 0)
  930. return ret;
  931. for (i = 0; i < num; i++) {
  932. struct i2c_msg *msg = &msgs[i];
  933. unsigned long flags;
  934. spin_lock_irqsave(&i2c->lock, flags);
  935. /*
  936. * Make a copy of the message struct. We mustn't modify the
  937. * original or we'll confuse drivers and i2c-dev.
  938. */
  939. i2c->msg = *msg;
  940. i2c->msg_status = 0;
  941. /*
  942. * After the last message we must have waited for a stop bit.
  943. * Not waiting can cause problems when the clock is disabled
  944. * before the stop bit is sent, and the linux I2C interface
  945. * requires separate transfers not to joined with repeated
  946. * start.
  947. */
  948. i2c->last_msg = (i == num - 1);
  949. reinit_completion(&i2c->msg_complete);
  950. /*
  951. * Clear line status and all interrupts before starting a
  952. * transfer, as we may have unserviced interrupts from
  953. * previous transfers that might be handled in the context
  954. * of the new transfer.
  955. */
  956. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
  957. img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
  958. if (atomic) {
  959. img_i2c_atomic_start(i2c);
  960. } else {
  961. /*
  962. * Enable transaction halt if not the last message in
  963. * the queue so that we can control repeated starts.
  964. */
  965. img_i2c_transaction_halt(i2c, !i2c->last_msg);
  966. if (msg->flags & I2C_M_RD)
  967. img_i2c_read(i2c);
  968. else
  969. img_i2c_write(i2c);
  970. /*
  971. * Release and then enable transaction halt, to
  972. * allow only a single byte to proceed.
  973. * This doesn't have an effect on the initial transfer
  974. * but will allow the following transfers to start
  975. * processing if the previous transfer was marked as
  976. * complete while the i2c block was halted.
  977. */
  978. img_i2c_transaction_halt(i2c, false);
  979. img_i2c_transaction_halt(i2c, !i2c->last_msg);
  980. }
  981. spin_unlock_irqrestore(&i2c->lock, flags);
  982. time_left = wait_for_completion_timeout(&i2c->msg_complete,
  983. IMG_I2C_TIMEOUT);
  984. del_timer_sync(&i2c->check_timer);
  985. if (time_left == 0) {
  986. dev_err(adap->dev.parent, "i2c transfer timed out\n");
  987. i2c->msg_status = -ETIMEDOUT;
  988. break;
  989. }
  990. if (i2c->msg_status)
  991. break;
  992. }
  993. pm_runtime_mark_last_busy(adap->dev.parent);
  994. pm_runtime_put_autosuspend(adap->dev.parent);
  995. return i2c->msg_status ? i2c->msg_status : num;
  996. }
  997. static u32 img_i2c_func(struct i2c_adapter *adap)
  998. {
  999. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  1000. }
  1001. static const struct i2c_algorithm img_i2c_algo = {
  1002. .master_xfer = img_i2c_xfer,
  1003. .functionality = img_i2c_func,
  1004. };
  1005. static int img_i2c_init(struct img_i2c *i2c)
  1006. {
  1007. unsigned int clk_khz, bitrate_khz, clk_period, tckh, tckl, tsdh;
  1008. unsigned int i, data, prescale, inc, int_bitrate, filt;
  1009. struct img_i2c_timings timing;
  1010. u32 rev;
  1011. int ret;
  1012. ret = pm_runtime_resume_and_get(i2c->adap.dev.parent);
  1013. if (ret < 0)
  1014. return ret;
  1015. rev = img_i2c_readl(i2c, SCB_CORE_REV_REG);
  1016. if ((rev & 0x00ffffff) < 0x00020200) {
  1017. dev_info(i2c->adap.dev.parent,
  1018. "Unknown hardware revision (%d.%d.%d.%d)\n",
  1019. (rev >> 24) & 0xff, (rev >> 16) & 0xff,
  1020. (rev >> 8) & 0xff, rev & 0xff);
  1021. pm_runtime_mark_last_busy(i2c->adap.dev.parent);
  1022. pm_runtime_put_autosuspend(i2c->adap.dev.parent);
  1023. return -EINVAL;
  1024. }
  1025. /* Fencing enabled by default. */
  1026. i2c->need_wr_rd_fence = true;
  1027. /* Determine what mode we're in from the bitrate */
  1028. timing = timings[0];
  1029. for (i = 0; i < ARRAY_SIZE(timings); i++) {
  1030. if (i2c->bitrate <= timings[i].max_bitrate) {
  1031. timing = timings[i];
  1032. break;
  1033. }
  1034. }
  1035. if (i2c->bitrate > timings[ARRAY_SIZE(timings) - 1].max_bitrate) {
  1036. dev_warn(i2c->adap.dev.parent,
  1037. "requested bitrate (%u) is higher than the max bitrate supported (%u)\n",
  1038. i2c->bitrate,
  1039. timings[ARRAY_SIZE(timings) - 1].max_bitrate);
  1040. timing = timings[ARRAY_SIZE(timings) - 1];
  1041. i2c->bitrate = timing.max_bitrate;
  1042. }
  1043. bitrate_khz = i2c->bitrate / 1000;
  1044. clk_khz = clk_get_rate(i2c->scb_clk) / 1000;
  1045. /* Find the prescale that would give us that inc (approx delay = 0) */
  1046. prescale = SCB_OPT_INC * clk_khz / (256 * 16 * bitrate_khz);
  1047. prescale = clamp_t(unsigned int, prescale, 1, 8);
  1048. clk_khz /= prescale;
  1049. /* Setup the clock increment value */
  1050. inc = (256 * 16 * bitrate_khz) / clk_khz;
  1051. /*
  1052. * The clock generation logic allows to filter glitches on the bus.
  1053. * This filter is able to remove bus glitches shorter than 50ns.
  1054. * If the clock enable rate is greater than 20 MHz, no filtering
  1055. * is required, so we need to disable it.
  1056. * If it's between the 20-40 MHz range, there's no need to divide
  1057. * the clock to get a filter.
  1058. */
  1059. if (clk_khz < 20000) {
  1060. filt = SCB_FILT_DISABLE;
  1061. } else if (clk_khz < 40000) {
  1062. filt = SCB_FILT_BYPASS;
  1063. } else {
  1064. /* Calculate filter clock */
  1065. filt = (64000 / ((clk_khz / 1000) * SCB_FILT_GLITCH));
  1066. /* Scale up if needed */
  1067. if (64000 % ((clk_khz / 1000) * SCB_FILT_GLITCH))
  1068. inc++;
  1069. if (filt > SCB_FILT_INC_MASK)
  1070. filt = SCB_FILT_INC_MASK;
  1071. filt = (filt & SCB_FILT_INC_MASK) << SCB_FILT_INC_SHIFT;
  1072. }
  1073. data = filt | ((inc & SCB_INC_MASK) << SCB_INC_SHIFT) | (prescale - 1);
  1074. img_i2c_writel(i2c, SCB_CLK_SET_REG, data);
  1075. /* Obtain the clock period of the fx16 clock in ns */
  1076. clk_period = (256 * 1000000) / (clk_khz * inc);
  1077. /* Calculate the bitrate in terms of internal clock pulses */
  1078. int_bitrate = 1000000 / (bitrate_khz * clk_period);
  1079. if ((1000000 % (bitrate_khz * clk_period)) >=
  1080. ((bitrate_khz * clk_period) / 2))
  1081. int_bitrate++;
  1082. /*
  1083. * Setup clock duty cycle, start with 50% and adjust TCKH and TCKL
  1084. * values from there if they don't meet minimum timing requirements
  1085. */
  1086. tckh = int_bitrate / 2;
  1087. tckl = int_bitrate - tckh;
  1088. /* Adjust TCKH and TCKL values */
  1089. data = DIV_ROUND_UP(timing.tckl, clk_period);
  1090. if (tckl < data) {
  1091. tckl = data;
  1092. tckh = int_bitrate - tckl;
  1093. }
  1094. if (tckh > 0)
  1095. --tckh;
  1096. if (tckl > 0)
  1097. --tckl;
  1098. img_i2c_writel(i2c, SCB_TIME_TCKH_REG, tckh);
  1099. img_i2c_writel(i2c, SCB_TIME_TCKL_REG, tckl);
  1100. /* Setup TSDH value */
  1101. tsdh = DIV_ROUND_UP(timing.tsdh, clk_period);
  1102. if (tsdh > 1)
  1103. data = tsdh - 1;
  1104. else
  1105. data = 0x01;
  1106. img_i2c_writel(i2c, SCB_TIME_TSDH_REG, data);
  1107. /* This value is used later */
  1108. tsdh = data;
  1109. /* Setup TPL value */
  1110. data = timing.tpl / clk_period;
  1111. if (data > 0)
  1112. --data;
  1113. img_i2c_writel(i2c, SCB_TIME_TPL_REG, data);
  1114. /* Setup TPH value */
  1115. data = timing.tph / clk_period;
  1116. if (data > 0)
  1117. --data;
  1118. img_i2c_writel(i2c, SCB_TIME_TPH_REG, data);
  1119. /* Setup TSDL value to TPL + TSDH + 2 */
  1120. img_i2c_writel(i2c, SCB_TIME_TSDL_REG, data + tsdh + 2);
  1121. /* Setup TP2S value */
  1122. data = timing.tp2s / clk_period;
  1123. if (data > 0)
  1124. --data;
  1125. img_i2c_writel(i2c, SCB_TIME_TP2S_REG, data);
  1126. img_i2c_writel(i2c, SCB_TIME_TBI_REG, TIMEOUT_TBI);
  1127. img_i2c_writel(i2c, SCB_TIME_TSL_REG, TIMEOUT_TSL);
  1128. img_i2c_writel(i2c, SCB_TIME_TDL_REG, TIMEOUT_TDL);
  1129. /* Take module out of soft reset and enable clocks */
  1130. img_i2c_soft_reset(i2c);
  1131. /* Disable all interrupts */
  1132. img_i2c_writel(i2c, SCB_INT_MASK_REG, 0);
  1133. /* Clear all interrupts */
  1134. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
  1135. /* Clear the scb_line_status events */
  1136. img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
  1137. /* Enable interrupts */
  1138. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  1139. /* Perform a synchronous sequence to reset the bus */
  1140. ret = img_i2c_reset_bus(i2c);
  1141. pm_runtime_mark_last_busy(i2c->adap.dev.parent);
  1142. pm_runtime_put_autosuspend(i2c->adap.dev.parent);
  1143. return ret;
  1144. }
  1145. static int img_i2c_probe(struct platform_device *pdev)
  1146. {
  1147. struct device_node *node = pdev->dev.of_node;
  1148. struct img_i2c *i2c;
  1149. int irq, ret;
  1150. u32 val;
  1151. i2c = devm_kzalloc(&pdev->dev, sizeof(struct img_i2c), GFP_KERNEL);
  1152. if (!i2c)
  1153. return -ENOMEM;
  1154. i2c->base = devm_platform_ioremap_resource(pdev, 0);
  1155. if (IS_ERR(i2c->base))
  1156. return PTR_ERR(i2c->base);
  1157. irq = platform_get_irq(pdev, 0);
  1158. if (irq < 0)
  1159. return irq;
  1160. i2c->sys_clk = devm_clk_get(&pdev->dev, "sys");
  1161. if (IS_ERR(i2c->sys_clk)) {
  1162. dev_err(&pdev->dev, "can't get system clock\n");
  1163. return PTR_ERR(i2c->sys_clk);
  1164. }
  1165. i2c->scb_clk = devm_clk_get(&pdev->dev, "scb");
  1166. if (IS_ERR(i2c->scb_clk)) {
  1167. dev_err(&pdev->dev, "can't get core clock\n");
  1168. return PTR_ERR(i2c->scb_clk);
  1169. }
  1170. ret = devm_request_irq(&pdev->dev, irq, img_i2c_isr, 0,
  1171. pdev->name, i2c);
  1172. if (ret) {
  1173. dev_err(&pdev->dev, "can't request irq %d\n", irq);
  1174. return ret;
  1175. }
  1176. /* Set up the exception check timer */
  1177. timer_setup(&i2c->check_timer, img_i2c_check_timer, 0);
  1178. i2c->bitrate = timings[0].max_bitrate;
  1179. if (!of_property_read_u32(node, "clock-frequency", &val))
  1180. i2c->bitrate = val;
  1181. i2c_set_adapdata(&i2c->adap, i2c);
  1182. i2c->adap.dev.parent = &pdev->dev;
  1183. i2c->adap.dev.of_node = node;
  1184. i2c->adap.owner = THIS_MODULE;
  1185. i2c->adap.algo = &img_i2c_algo;
  1186. i2c->adap.retries = 5;
  1187. i2c->adap.nr = pdev->id;
  1188. snprintf(i2c->adap.name, sizeof(i2c->adap.name), "IMG SCB I2C");
  1189. img_i2c_switch_mode(i2c, MODE_INACTIVE);
  1190. spin_lock_init(&i2c->lock);
  1191. init_completion(&i2c->msg_complete);
  1192. platform_set_drvdata(pdev, i2c);
  1193. pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_I2C_PM_TIMEOUT);
  1194. pm_runtime_use_autosuspend(&pdev->dev);
  1195. pm_runtime_enable(&pdev->dev);
  1196. if (!pm_runtime_enabled(&pdev->dev)) {
  1197. ret = img_i2c_runtime_resume(&pdev->dev);
  1198. if (ret)
  1199. return ret;
  1200. }
  1201. ret = img_i2c_init(i2c);
  1202. if (ret)
  1203. goto rpm_disable;
  1204. ret = i2c_add_numbered_adapter(&i2c->adap);
  1205. if (ret < 0)
  1206. goto rpm_disable;
  1207. return 0;
  1208. rpm_disable:
  1209. if (!pm_runtime_enabled(&pdev->dev))
  1210. img_i2c_runtime_suspend(&pdev->dev);
  1211. pm_runtime_disable(&pdev->dev);
  1212. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1213. return ret;
  1214. }
  1215. static int img_i2c_remove(struct platform_device *dev)
  1216. {
  1217. struct img_i2c *i2c = platform_get_drvdata(dev);
  1218. i2c_del_adapter(&i2c->adap);
  1219. pm_runtime_disable(&dev->dev);
  1220. if (!pm_runtime_status_suspended(&dev->dev))
  1221. img_i2c_runtime_suspend(&dev->dev);
  1222. return 0;
  1223. }
  1224. static int img_i2c_runtime_suspend(struct device *dev)
  1225. {
  1226. struct img_i2c *i2c = dev_get_drvdata(dev);
  1227. clk_disable_unprepare(i2c->scb_clk);
  1228. clk_disable_unprepare(i2c->sys_clk);
  1229. return 0;
  1230. }
  1231. static int img_i2c_runtime_resume(struct device *dev)
  1232. {
  1233. struct img_i2c *i2c = dev_get_drvdata(dev);
  1234. int ret;
  1235. ret = clk_prepare_enable(i2c->sys_clk);
  1236. if (ret) {
  1237. dev_err(dev, "Unable to enable sys clock\n");
  1238. return ret;
  1239. }
  1240. ret = clk_prepare_enable(i2c->scb_clk);
  1241. if (ret) {
  1242. dev_err(dev, "Unable to enable scb clock\n");
  1243. clk_disable_unprepare(i2c->sys_clk);
  1244. return ret;
  1245. }
  1246. return 0;
  1247. }
  1248. #ifdef CONFIG_PM_SLEEP
  1249. static int img_i2c_suspend(struct device *dev)
  1250. {
  1251. struct img_i2c *i2c = dev_get_drvdata(dev);
  1252. int ret;
  1253. ret = pm_runtime_force_suspend(dev);
  1254. if (ret)
  1255. return ret;
  1256. img_i2c_switch_mode(i2c, MODE_SUSPEND);
  1257. return 0;
  1258. }
  1259. static int img_i2c_resume(struct device *dev)
  1260. {
  1261. struct img_i2c *i2c = dev_get_drvdata(dev);
  1262. int ret;
  1263. ret = pm_runtime_force_resume(dev);
  1264. if (ret)
  1265. return ret;
  1266. img_i2c_init(i2c);
  1267. return 0;
  1268. }
  1269. #endif /* CONFIG_PM_SLEEP */
  1270. static const struct dev_pm_ops img_i2c_pm = {
  1271. SET_RUNTIME_PM_OPS(img_i2c_runtime_suspend,
  1272. img_i2c_runtime_resume,
  1273. NULL)
  1274. SET_SYSTEM_SLEEP_PM_OPS(img_i2c_suspend, img_i2c_resume)
  1275. };
  1276. static const struct of_device_id img_scb_i2c_match[] = {
  1277. { .compatible = "img,scb-i2c" },
  1278. { }
  1279. };
  1280. MODULE_DEVICE_TABLE(of, img_scb_i2c_match);
  1281. static struct platform_driver img_scb_i2c_driver = {
  1282. .driver = {
  1283. .name = "img-i2c-scb",
  1284. .of_match_table = img_scb_i2c_match,
  1285. .pm = &img_i2c_pm,
  1286. },
  1287. .probe = img_i2c_probe,
  1288. .remove = img_i2c_remove,
  1289. };
  1290. module_platform_driver(img_scb_i2c_driver);
  1291. MODULE_AUTHOR("James Hogan <[email protected]>");
  1292. MODULE_DESCRIPTION("IMG host I2C driver");
  1293. MODULE_LICENSE("GPL v2");