i2c-img-scb.c 40 KB

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