i2c-img-scb.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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_TIMING BIT(18)
  146. #define INT_FIFO_FULL_FILLING (INT_FIFO_FULL | INT_FIFO_FILLING)
  147. #define INT_FIFO_EMPTY_EMPTYING (INT_FIFO_EMPTY | INT_FIFO_EMPTYING)
  148. /* Level interrupts need clearing after handling instead of before */
  149. #define INT_LEVEL 0x01e00
  150. /* Don't allow any interrupts while the clock may be off */
  151. #define INT_ENABLE_MASK_INACTIVE 0x00000
  152. /* Interrupt masks for the different driver modes */
  153. #define INT_ENABLE_MASK_RAW INT_TIMING
  154. #define INT_ENABLE_MASK_ATOMIC (INT_TRANSACTION_DONE | \
  155. INT_SLAVE_EVENT | \
  156. INT_ADDR_ACK_ERR | \
  157. INT_WRITE_ACK_ERR)
  158. #define INT_ENABLE_MASK_AUTOMATIC (INT_SCLK_LOW_TIMEOUT | \
  159. INT_ADDR_ACK_ERR | \
  160. INT_WRITE_ACK_ERR | \
  161. INT_FIFO_FULL | \
  162. INT_FIFO_FILLING | \
  163. INT_FIFO_EMPTY | \
  164. INT_FIFO_EMPTYING)
  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. enum img_i2c_mode {
  249. MODE_INACTIVE,
  250. MODE_RAW,
  251. MODE_ATOMIC,
  252. MODE_AUTOMATIC,
  253. MODE_SEQUENCE,
  254. MODE_FATAL,
  255. MODE_WAITSTOP,
  256. MODE_SUSPEND,
  257. };
  258. /* Timing parameters for i2c modes (in ns) */
  259. struct img_i2c_timings {
  260. const char *name;
  261. unsigned int max_bitrate;
  262. unsigned int tckh, tckl, tsdh, tsdl;
  263. unsigned int tp2s, tpl, tph;
  264. };
  265. /* The timings array must be ordered from slower to faster */
  266. static struct img_i2c_timings timings[] = {
  267. /* Standard mode */
  268. {
  269. .name = "standard",
  270. .max_bitrate = 100000,
  271. .tckh = 4000,
  272. .tckl = 4700,
  273. .tsdh = 4700,
  274. .tsdl = 8700,
  275. .tp2s = 4700,
  276. .tpl = 4700,
  277. .tph = 4000,
  278. },
  279. /* Fast mode */
  280. {
  281. .name = "fast",
  282. .max_bitrate = 400000,
  283. .tckh = 600,
  284. .tckl = 1300,
  285. .tsdh = 600,
  286. .tsdl = 1200,
  287. .tp2s = 1300,
  288. .tpl = 600,
  289. .tph = 600,
  290. },
  291. };
  292. /* Reset dance */
  293. static u8 img_i2c_reset_seq[] = { CMD_GEN_START,
  294. CMD_GEN_DATA, 0xff,
  295. CMD_RET_ACK,
  296. CMD_GEN_START,
  297. CMD_GEN_STOP,
  298. 0 };
  299. /* Just issue a stop (after an abort condition) */
  300. static u8 img_i2c_stop_seq[] = { CMD_GEN_STOP,
  301. 0 };
  302. /* We're interested in different interrupts depending on the mode */
  303. static unsigned int img_i2c_int_enable_by_mode[] = {
  304. [MODE_INACTIVE] = INT_ENABLE_MASK_INACTIVE,
  305. [MODE_RAW] = INT_ENABLE_MASK_RAW,
  306. [MODE_ATOMIC] = INT_ENABLE_MASK_ATOMIC,
  307. [MODE_AUTOMATIC] = INT_ENABLE_MASK_AUTOMATIC,
  308. [MODE_SEQUENCE] = INT_ENABLE_MASK_ATOMIC,
  309. [MODE_FATAL] = 0,
  310. [MODE_WAITSTOP] = INT_ENABLE_MASK_WAITSTOP,
  311. [MODE_SUSPEND] = 0,
  312. };
  313. /* Atomic command names */
  314. static const char * const img_i2c_atomic_cmd_names[] = {
  315. [CMD_PAUSE] = "PAUSE",
  316. [CMD_GEN_DATA] = "GEN_DATA",
  317. [CMD_GEN_START] = "GEN_START",
  318. [CMD_GEN_STOP] = "GEN_STOP",
  319. [CMD_GEN_ACK] = "GEN_ACK",
  320. [CMD_GEN_NACK] = "GEN_NACK",
  321. [CMD_RET_DATA] = "RET_DATA",
  322. [CMD_RET_ACK] = "RET_ACK",
  323. };
  324. struct img_i2c {
  325. struct i2c_adapter adap;
  326. void __iomem *base;
  327. /*
  328. * The scb core clock is used to get the input frequency, and to disable
  329. * it after every set of transactions to save some power.
  330. */
  331. struct clk *scb_clk, *sys_clk;
  332. unsigned int bitrate;
  333. bool need_wr_rd_fence;
  334. /* state */
  335. struct completion msg_complete;
  336. spinlock_t lock; /* lock before doing anything with the state */
  337. struct i2c_msg msg;
  338. /* After the last transaction, wait for a stop bit */
  339. bool last_msg;
  340. int msg_status;
  341. enum img_i2c_mode mode;
  342. u32 int_enable; /* depends on mode */
  343. u32 line_status; /* line status over command */
  344. /*
  345. * To avoid slave event interrupts in automatic mode, use a timer to
  346. * poll the abort condition if we don't get an interrupt for too long.
  347. */
  348. struct timer_list check_timer;
  349. bool t_halt;
  350. /* atomic mode state */
  351. bool at_t_done;
  352. bool at_slave_event;
  353. int at_cur_cmd;
  354. u8 at_cur_data;
  355. /* Sequence: either reset or stop. See img_i2c_sequence. */
  356. u8 *seq;
  357. /* raw mode */
  358. unsigned int raw_timeout;
  359. };
  360. static void img_i2c_writel(struct img_i2c *i2c, u32 offset, u32 value)
  361. {
  362. writel(value, i2c->base + offset);
  363. }
  364. static u32 img_i2c_readl(struct img_i2c *i2c, u32 offset)
  365. {
  366. return readl(i2c->base + offset);
  367. }
  368. /*
  369. * The code to read from the master read fifo, and write to the master
  370. * write fifo, checks a bit in an SCB register before every byte to
  371. * ensure that the fifo is not full (write fifo) or empty (read fifo).
  372. * Due to clock domain crossing inside the SCB block the updated value
  373. * of this bit is only visible after 2 cycles.
  374. *
  375. * The scb_wr_rd_fence() function does 2 dummy writes (to the read-only
  376. * revision register), and it's called after reading from or writing to the
  377. * fifos to ensure that subsequent reads of the fifo status bits do not read
  378. * stale values.
  379. */
  380. static void img_i2c_wr_rd_fence(struct img_i2c *i2c)
  381. {
  382. if (i2c->need_wr_rd_fence) {
  383. img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
  384. img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
  385. }
  386. }
  387. static void img_i2c_switch_mode(struct img_i2c *i2c, enum img_i2c_mode mode)
  388. {
  389. i2c->mode = mode;
  390. i2c->int_enable = img_i2c_int_enable_by_mode[mode];
  391. i2c->line_status = 0;
  392. }
  393. static void img_i2c_raw_op(struct img_i2c *i2c)
  394. {
  395. i2c->raw_timeout = 0;
  396. img_i2c_writel(i2c, SCB_OVERRIDE_REG,
  397. OVERRIDE_SCLKEN_OVR |
  398. OVERRIDE_SDATEN_OVR |
  399. OVERRIDE_MASTER |
  400. OVERRIDE_LINE_OVR_EN |
  401. OVERRIDE_DIRECT |
  402. ((i2c->at_cur_cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
  403. (i2c->at_cur_data << OVERRIDE_DATA_SHIFT));
  404. }
  405. static const char *img_i2c_atomic_op_name(unsigned int cmd)
  406. {
  407. if (unlikely(cmd >= ARRAY_SIZE(img_i2c_atomic_cmd_names)))
  408. return "UNKNOWN";
  409. return img_i2c_atomic_cmd_names[cmd];
  410. }
  411. /* Send a single atomic mode command to the hardware */
  412. static void img_i2c_atomic_op(struct img_i2c *i2c, int cmd, u8 data)
  413. {
  414. i2c->at_cur_cmd = cmd;
  415. i2c->at_cur_data = data;
  416. /* work around lack of data setup time when generating data */
  417. if (cmd == CMD_GEN_DATA && i2c->mode == MODE_ATOMIC) {
  418. u32 line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
  419. if (line_status & LINESTAT_SDAT_LINE_STATUS && !(data & 0x80)) {
  420. /* hold the data line down for a moment */
  421. img_i2c_switch_mode(i2c, MODE_RAW);
  422. img_i2c_raw_op(i2c);
  423. return;
  424. }
  425. }
  426. dev_dbg(i2c->adap.dev.parent,
  427. "atomic cmd=%s (%d) data=%#x\n",
  428. img_i2c_atomic_op_name(cmd), cmd, data);
  429. i2c->at_t_done = (cmd == CMD_RET_DATA || cmd == CMD_RET_ACK);
  430. i2c->at_slave_event = false;
  431. i2c->line_status = 0;
  432. img_i2c_writel(i2c, SCB_OVERRIDE_REG,
  433. ((cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
  434. OVERRIDE_MASTER |
  435. OVERRIDE_DIRECT |
  436. (data << OVERRIDE_DATA_SHIFT));
  437. }
  438. /* Start a transaction in atomic mode */
  439. static void img_i2c_atomic_start(struct img_i2c *i2c)
  440. {
  441. img_i2c_switch_mode(i2c, MODE_ATOMIC);
  442. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  443. img_i2c_atomic_op(i2c, CMD_GEN_START, 0x00);
  444. }
  445. static void img_i2c_soft_reset(struct img_i2c *i2c)
  446. {
  447. i2c->t_halt = false;
  448. img_i2c_writel(i2c, SCB_CONTROL_REG, 0);
  449. img_i2c_writel(i2c, SCB_CONTROL_REG,
  450. SCB_CONTROL_CLK_ENABLE | SCB_CONTROL_SOFT_RESET);
  451. }
  452. /* enable or release transaction halt for control of repeated starts */
  453. static void img_i2c_transaction_halt(struct img_i2c *i2c, bool t_halt)
  454. {
  455. u32 val;
  456. if (i2c->t_halt == t_halt)
  457. return;
  458. i2c->t_halt = t_halt;
  459. val = img_i2c_readl(i2c, SCB_CONTROL_REG);
  460. if (t_halt)
  461. val |= SCB_CONTROL_TRANSACTION_HALT;
  462. else
  463. val &= ~SCB_CONTROL_TRANSACTION_HALT;
  464. img_i2c_writel(i2c, SCB_CONTROL_REG, val);
  465. }
  466. /* Drain data from the FIFO into the buffer (automatic mode) */
  467. static void img_i2c_read_fifo(struct img_i2c *i2c)
  468. {
  469. while (i2c->msg.len) {
  470. u32 fifo_status;
  471. u8 data;
  472. img_i2c_wr_rd_fence(i2c);
  473. fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
  474. if (fifo_status & FIFO_READ_EMPTY)
  475. break;
  476. data = img_i2c_readl(i2c, SCB_READ_DATA_REG);
  477. *i2c->msg.buf = data;
  478. img_i2c_writel(i2c, SCB_READ_FIFO_REG, 0xff);
  479. i2c->msg.len--;
  480. i2c->msg.buf++;
  481. }
  482. }
  483. /* Fill the FIFO with data from the buffer (automatic mode) */
  484. static void img_i2c_write_fifo(struct img_i2c *i2c)
  485. {
  486. while (i2c->msg.len) {
  487. u32 fifo_status;
  488. img_i2c_wr_rd_fence(i2c);
  489. fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
  490. if (fifo_status & FIFO_WRITE_FULL)
  491. break;
  492. img_i2c_writel(i2c, SCB_WRITE_DATA_REG, *i2c->msg.buf);
  493. i2c->msg.len--;
  494. i2c->msg.buf++;
  495. }
  496. /* Disable fifo emptying interrupt if nothing more to write */
  497. if (!i2c->msg.len)
  498. i2c->int_enable &= ~INT_FIFO_EMPTYING;
  499. }
  500. /* Start a read transaction in automatic mode */
  501. static void img_i2c_read(struct img_i2c *i2c)
  502. {
  503. img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
  504. if (!i2c->last_msg)
  505. i2c->int_enable |= INT_SLAVE_EVENT;
  506. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  507. img_i2c_writel(i2c, SCB_READ_ADDR_REG, i2c->msg.addr);
  508. img_i2c_writel(i2c, SCB_READ_COUNT_REG, i2c->msg.len);
  509. img_i2c_transaction_halt(i2c, false);
  510. mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
  511. }
  512. /* Start a write transaction in automatic mode */
  513. static void img_i2c_write(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_WRITE_ADDR_REG, i2c->msg.addr);
  519. img_i2c_writel(i2c, SCB_WRITE_COUNT_REG, i2c->msg.len);
  520. img_i2c_transaction_halt(i2c, false);
  521. mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
  522. img_i2c_write_fifo(i2c);
  523. /* img_i2c_write_fifo() may modify int_enable */
  524. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  525. }
  526. /*
  527. * Indicate that the transaction is complete. This is called from the
  528. * ISR to wake up the waiting thread, after which the ISR must not
  529. * access any more SCB registers.
  530. */
  531. static void img_i2c_complete_transaction(struct img_i2c *i2c, int status)
  532. {
  533. img_i2c_switch_mode(i2c, MODE_INACTIVE);
  534. if (status) {
  535. i2c->msg_status = status;
  536. img_i2c_transaction_halt(i2c, false);
  537. }
  538. complete(&i2c->msg_complete);
  539. }
  540. static unsigned int img_i2c_raw_atomic_delay_handler(struct img_i2c *i2c,
  541. u32 int_status, u32 line_status)
  542. {
  543. /* Stay in raw mode for this, so we don't just loop infinitely */
  544. img_i2c_atomic_op(i2c, i2c->at_cur_cmd, i2c->at_cur_data);
  545. img_i2c_switch_mode(i2c, MODE_ATOMIC);
  546. return 0;
  547. }
  548. static unsigned int img_i2c_raw(struct img_i2c *i2c, u32 int_status,
  549. u32 line_status)
  550. {
  551. if (int_status & INT_TIMING) {
  552. if (i2c->raw_timeout == 0)
  553. return img_i2c_raw_atomic_delay_handler(i2c,
  554. int_status, line_status);
  555. --i2c->raw_timeout;
  556. }
  557. return 0;
  558. }
  559. static unsigned int img_i2c_sequence(struct img_i2c *i2c, u32 int_status)
  560. {
  561. static const unsigned int continue_bits[] = {
  562. [CMD_GEN_START] = LINESTAT_START_BIT_DET,
  563. [CMD_GEN_DATA] = LINESTAT_INPUT_HELD_V,
  564. [CMD_RET_ACK] = LINESTAT_ACK_DET | LINESTAT_NACK_DET,
  565. [CMD_RET_DATA] = LINESTAT_INPUT_HELD_V,
  566. [CMD_GEN_STOP] = LINESTAT_STOP_BIT_DET,
  567. };
  568. int next_cmd = -1;
  569. u8 next_data = 0x00;
  570. if (int_status & INT_SLAVE_EVENT)
  571. i2c->at_slave_event = true;
  572. if (int_status & INT_TRANSACTION_DONE)
  573. i2c->at_t_done = true;
  574. if (!i2c->at_slave_event || !i2c->at_t_done)
  575. return 0;
  576. /* wait if no continue bits are set */
  577. if (i2c->at_cur_cmd >= 0 &&
  578. i2c->at_cur_cmd < ARRAY_SIZE(continue_bits)) {
  579. unsigned int cont_bits = continue_bits[i2c->at_cur_cmd];
  580. if (cont_bits) {
  581. cont_bits |= LINESTAT_ABORT_DET;
  582. if (!(i2c->line_status & cont_bits))
  583. return 0;
  584. }
  585. }
  586. /* follow the sequence of commands in i2c->seq */
  587. next_cmd = *i2c->seq;
  588. /* stop on a nil */
  589. if (!next_cmd) {
  590. img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
  591. return ISR_COMPLETE(0);
  592. }
  593. /* when generating data, the next byte is the data */
  594. if (next_cmd == CMD_GEN_DATA) {
  595. ++i2c->seq;
  596. next_data = *i2c->seq;
  597. }
  598. ++i2c->seq;
  599. img_i2c_atomic_op(i2c, next_cmd, next_data);
  600. return 0;
  601. }
  602. static void img_i2c_reset_start(struct img_i2c *i2c)
  603. {
  604. /* Initiate the magic dance */
  605. img_i2c_switch_mode(i2c, MODE_SEQUENCE);
  606. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  607. i2c->seq = img_i2c_reset_seq;
  608. i2c->at_slave_event = true;
  609. i2c->at_t_done = true;
  610. i2c->at_cur_cmd = -1;
  611. /* img_i2c_reset_seq isn't empty so the following won't fail */
  612. img_i2c_sequence(i2c, 0);
  613. }
  614. static void img_i2c_stop_start(struct img_i2c *i2c)
  615. {
  616. /* Initiate a stop bit sequence */
  617. img_i2c_switch_mode(i2c, MODE_SEQUENCE);
  618. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  619. i2c->seq = img_i2c_stop_seq;
  620. i2c->at_slave_event = true;
  621. i2c->at_t_done = true;
  622. i2c->at_cur_cmd = -1;
  623. /* img_i2c_stop_seq isn't empty so the following won't fail */
  624. img_i2c_sequence(i2c, 0);
  625. }
  626. static unsigned int img_i2c_atomic(struct img_i2c *i2c,
  627. u32 int_status,
  628. u32 line_status)
  629. {
  630. int next_cmd = -1;
  631. u8 next_data = 0x00;
  632. if (int_status & INT_SLAVE_EVENT)
  633. i2c->at_slave_event = true;
  634. if (int_status & INT_TRANSACTION_DONE)
  635. i2c->at_t_done = true;
  636. if (!i2c->at_slave_event || !i2c->at_t_done)
  637. goto next_atomic_cmd;
  638. if (i2c->line_status & LINESTAT_ABORT_DET) {
  639. dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
  640. next_cmd = CMD_GEN_STOP;
  641. i2c->msg_status = -EIO;
  642. goto next_atomic_cmd;
  643. }
  644. /* i2c->at_cur_cmd may have completed */
  645. switch (i2c->at_cur_cmd) {
  646. case CMD_GEN_START:
  647. next_cmd = CMD_GEN_DATA;
  648. next_data = (i2c->msg.addr << 1);
  649. if (i2c->msg.flags & I2C_M_RD)
  650. next_data |= 0x1;
  651. break;
  652. case CMD_GEN_DATA:
  653. if (i2c->line_status & LINESTAT_INPUT_HELD_V)
  654. next_cmd = CMD_RET_ACK;
  655. break;
  656. case CMD_RET_ACK:
  657. if (i2c->line_status & LINESTAT_ACK_DET) {
  658. if (i2c->msg.len == 0) {
  659. next_cmd = CMD_GEN_STOP;
  660. } else if (i2c->msg.flags & I2C_M_RD) {
  661. next_cmd = CMD_RET_DATA;
  662. } else {
  663. next_cmd = CMD_GEN_DATA;
  664. next_data = *i2c->msg.buf;
  665. --i2c->msg.len;
  666. ++i2c->msg.buf;
  667. }
  668. } else if (i2c->line_status & LINESTAT_NACK_DET) {
  669. i2c->msg_status = -EIO;
  670. next_cmd = CMD_GEN_STOP;
  671. }
  672. break;
  673. case CMD_RET_DATA:
  674. if (i2c->line_status & LINESTAT_INPUT_HELD_V) {
  675. *i2c->msg.buf = (i2c->line_status &
  676. LINESTAT_INPUT_DATA)
  677. >> LINESTAT_INPUT_DATA_SHIFT;
  678. --i2c->msg.len;
  679. ++i2c->msg.buf;
  680. if (i2c->msg.len)
  681. next_cmd = CMD_GEN_ACK;
  682. else
  683. next_cmd = CMD_GEN_NACK;
  684. }
  685. break;
  686. case CMD_GEN_ACK:
  687. if (i2c->line_status & LINESTAT_ACK_DET) {
  688. next_cmd = CMD_RET_DATA;
  689. } else {
  690. i2c->msg_status = -EIO;
  691. next_cmd = CMD_GEN_STOP;
  692. }
  693. break;
  694. case CMD_GEN_NACK:
  695. next_cmd = CMD_GEN_STOP;
  696. break;
  697. case CMD_GEN_STOP:
  698. img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
  699. return ISR_COMPLETE(0);
  700. default:
  701. dev_err(i2c->adap.dev.parent, "bad atomic command %d\n",
  702. i2c->at_cur_cmd);
  703. i2c->msg_status = -EIO;
  704. next_cmd = CMD_GEN_STOP;
  705. break;
  706. }
  707. next_atomic_cmd:
  708. if (next_cmd != -1) {
  709. /* don't actually stop unless we're the last transaction */
  710. if (next_cmd == CMD_GEN_STOP && !i2c->msg_status &&
  711. !i2c->last_msg)
  712. return ISR_COMPLETE(0);
  713. img_i2c_atomic_op(i2c, next_cmd, next_data);
  714. }
  715. return 0;
  716. }
  717. /*
  718. * Timer function to check if something has gone wrong in automatic mode (so we
  719. * don't have to handle so many interrupts just to catch an exception).
  720. */
  721. static void img_i2c_check_timer(unsigned long arg)
  722. {
  723. struct img_i2c *i2c = (struct img_i2c *)arg;
  724. unsigned long flags;
  725. unsigned int line_status;
  726. spin_lock_irqsave(&i2c->lock, flags);
  727. line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
  728. /* check for an abort condition */
  729. if (line_status & LINESTAT_ABORT_DET) {
  730. dev_dbg(i2c->adap.dev.parent,
  731. "abort condition detected by check timer\n");
  732. /* enable slave event interrupt mask to trigger irq */
  733. img_i2c_writel(i2c, SCB_INT_MASK_REG,
  734. i2c->int_enable | INT_SLAVE_EVENT);
  735. }
  736. spin_unlock_irqrestore(&i2c->lock, flags);
  737. }
  738. static unsigned int img_i2c_auto(struct img_i2c *i2c,
  739. unsigned int int_status,
  740. unsigned int line_status)
  741. {
  742. if (int_status & (INT_WRITE_ACK_ERR | INT_ADDR_ACK_ERR))
  743. return ISR_COMPLETE(EIO);
  744. if (line_status & LINESTAT_ABORT_DET) {
  745. dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
  746. /* empty the read fifo */
  747. if ((i2c->msg.flags & I2C_M_RD) &&
  748. (int_status & INT_FIFO_FULL_FILLING))
  749. img_i2c_read_fifo(i2c);
  750. /* use atomic mode and try to force a stop bit */
  751. i2c->msg_status = -EIO;
  752. img_i2c_stop_start(i2c);
  753. return 0;
  754. }
  755. /* Enable transaction halt on start bit */
  756. if (!i2c->last_msg && line_status & LINESTAT_START_BIT_DET) {
  757. img_i2c_transaction_halt(i2c, true);
  758. /* we're no longer interested in the slave event */
  759. i2c->int_enable &= ~INT_SLAVE_EVENT;
  760. }
  761. mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
  762. if (i2c->msg.flags & I2C_M_RD) {
  763. if (int_status & INT_FIFO_FULL_FILLING) {
  764. img_i2c_read_fifo(i2c);
  765. if (i2c->msg.len == 0)
  766. return ISR_WAITSTOP;
  767. }
  768. } else {
  769. if (int_status & INT_FIFO_EMPTY_EMPTYING) {
  770. /*
  771. * The write fifo empty indicates that we're in the
  772. * last byte so it's safe to start a new write
  773. * transaction without losing any bytes from the
  774. * previous one.
  775. * see 2.3.7 Repeated Start Transactions.
  776. */
  777. if ((int_status & INT_FIFO_EMPTY) &&
  778. i2c->msg.len == 0)
  779. return ISR_WAITSTOP;
  780. img_i2c_write_fifo(i2c);
  781. }
  782. }
  783. return 0;
  784. }
  785. static irqreturn_t img_i2c_isr(int irq, void *dev_id)
  786. {
  787. struct img_i2c *i2c = (struct img_i2c *)dev_id;
  788. u32 int_status, line_status;
  789. /* We handle transaction completion AFTER accessing registers */
  790. unsigned int hret;
  791. /* Read interrupt status register. */
  792. int_status = img_i2c_readl(i2c, SCB_INT_STATUS_REG);
  793. /* Clear detected interrupts. */
  794. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status);
  795. /*
  796. * Read line status and clear it until it actually is clear. We have
  797. * to be careful not to lose any line status bits that get latched.
  798. */
  799. line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
  800. if (line_status & LINESTAT_LATCHED) {
  801. img_i2c_writel(i2c, SCB_CLEAR_REG,
  802. (line_status & LINESTAT_LATCHED)
  803. >> LINESTAT_CLEAR_SHIFT);
  804. img_i2c_wr_rd_fence(i2c);
  805. }
  806. spin_lock(&i2c->lock);
  807. /* Keep track of line status bits received */
  808. i2c->line_status &= ~LINESTAT_INPUT_DATA;
  809. i2c->line_status |= line_status;
  810. /*
  811. * Certain interrupts indicate that sclk low timeout is not
  812. * a problem. If any of these are set, just continue.
  813. */
  814. if ((int_status & INT_SCLK_LOW_TIMEOUT) &&
  815. !(int_status & (INT_SLAVE_EVENT |
  816. INT_FIFO_EMPTY |
  817. INT_FIFO_FULL))) {
  818. dev_crit(i2c->adap.dev.parent,
  819. "fatal: clock low timeout occurred %s addr 0x%02x\n",
  820. (i2c->msg.flags & I2C_M_RD) ? "reading" : "writing",
  821. i2c->msg.addr);
  822. hret = ISR_FATAL(EIO);
  823. goto out;
  824. }
  825. if (i2c->mode == MODE_ATOMIC)
  826. hret = img_i2c_atomic(i2c, int_status, line_status);
  827. else if (i2c->mode == MODE_AUTOMATIC)
  828. hret = img_i2c_auto(i2c, int_status, line_status);
  829. else if (i2c->mode == MODE_SEQUENCE)
  830. hret = img_i2c_sequence(i2c, int_status);
  831. else if (i2c->mode == MODE_WAITSTOP && (int_status & INT_SLAVE_EVENT) &&
  832. (line_status & LINESTAT_STOP_BIT_DET))
  833. hret = ISR_COMPLETE(0);
  834. else if (i2c->mode == MODE_RAW)
  835. hret = img_i2c_raw(i2c, int_status, line_status);
  836. else
  837. hret = 0;
  838. /* Clear detected level interrupts. */
  839. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status & INT_LEVEL);
  840. out:
  841. if (hret & ISR_WAITSTOP) {
  842. /*
  843. * Only wait for stop on last message.
  844. * Also we may already have detected the stop bit.
  845. */
  846. if (!i2c->last_msg || i2c->line_status & LINESTAT_STOP_BIT_DET)
  847. hret = ISR_COMPLETE(0);
  848. else
  849. img_i2c_switch_mode(i2c, MODE_WAITSTOP);
  850. }
  851. /* now we've finished using regs, handle transaction completion */
  852. if (hret & ISR_COMPLETE_M) {
  853. int status = -(hret & ISR_STATUS_M);
  854. img_i2c_complete_transaction(i2c, status);
  855. if (hret & ISR_FATAL_M)
  856. img_i2c_switch_mode(i2c, MODE_FATAL);
  857. }
  858. /* Enable interrupts (int_enable may be altered by changing mode) */
  859. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  860. spin_unlock(&i2c->lock);
  861. return IRQ_HANDLED;
  862. }
  863. /* Force a bus reset sequence and wait for it to complete */
  864. static int img_i2c_reset_bus(struct img_i2c *i2c)
  865. {
  866. unsigned long flags;
  867. unsigned long time_left;
  868. spin_lock_irqsave(&i2c->lock, flags);
  869. reinit_completion(&i2c->msg_complete);
  870. img_i2c_reset_start(i2c);
  871. spin_unlock_irqrestore(&i2c->lock, flags);
  872. time_left = wait_for_completion_timeout(&i2c->msg_complete,
  873. IMG_I2C_TIMEOUT);
  874. if (time_left == 0)
  875. return -ETIMEDOUT;
  876. return 0;
  877. }
  878. static int img_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  879. int num)
  880. {
  881. struct img_i2c *i2c = i2c_get_adapdata(adap);
  882. bool atomic = false;
  883. int i, ret;
  884. unsigned long time_left;
  885. if (i2c->mode == MODE_SUSPEND) {
  886. WARN(1, "refusing to service transaction in suspended state\n");
  887. return -EIO;
  888. }
  889. if (i2c->mode == MODE_FATAL)
  890. return -EIO;
  891. for (i = 0; i < num; i++) {
  892. if (likely(msgs[i].len))
  893. continue;
  894. /*
  895. * 0 byte reads are not possible because the slave could try
  896. * and pull the data line low, preventing a stop bit.
  897. */
  898. if (unlikely(msgs[i].flags & I2C_M_RD))
  899. return -EIO;
  900. /*
  901. * 0 byte writes are possible and used for probing, but we
  902. * cannot do them in automatic mode, so use atomic mode
  903. * instead.
  904. */
  905. atomic = true;
  906. }
  907. ret = clk_prepare_enable(i2c->scb_clk);
  908. if (ret)
  909. return ret;
  910. for (i = 0; i < num; i++) {
  911. struct i2c_msg *msg = &msgs[i];
  912. unsigned long flags;
  913. spin_lock_irqsave(&i2c->lock, flags);
  914. /*
  915. * Make a copy of the message struct. We mustn't modify the
  916. * original or we'll confuse drivers and i2c-dev.
  917. */
  918. i2c->msg = *msg;
  919. i2c->msg_status = 0;
  920. /*
  921. * After the last message we must have waited for a stop bit.
  922. * Not waiting can cause problems when the clock is disabled
  923. * before the stop bit is sent, and the linux I2C interface
  924. * requires separate transfers not to joined with repeated
  925. * start.
  926. */
  927. i2c->last_msg = (i == num - 1);
  928. reinit_completion(&i2c->msg_complete);
  929. /*
  930. * Clear line status and all interrupts before starting a
  931. * transfer, as we may have unserviced interrupts from
  932. * previous transfers that might be handled in the context
  933. * of the new transfer.
  934. */
  935. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
  936. img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
  937. if (atomic)
  938. img_i2c_atomic_start(i2c);
  939. else if (msg->flags & I2C_M_RD)
  940. img_i2c_read(i2c);
  941. else
  942. img_i2c_write(i2c);
  943. spin_unlock_irqrestore(&i2c->lock, flags);
  944. time_left = wait_for_completion_timeout(&i2c->msg_complete,
  945. IMG_I2C_TIMEOUT);
  946. del_timer_sync(&i2c->check_timer);
  947. if (time_left == 0) {
  948. dev_err(adap->dev.parent, "i2c transfer timed out\n");
  949. i2c->msg_status = -ETIMEDOUT;
  950. break;
  951. }
  952. if (i2c->msg_status)
  953. break;
  954. }
  955. clk_disable_unprepare(i2c->scb_clk);
  956. return i2c->msg_status ? i2c->msg_status : num;
  957. }
  958. static u32 img_i2c_func(struct i2c_adapter *adap)
  959. {
  960. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  961. }
  962. static const struct i2c_algorithm img_i2c_algo = {
  963. .master_xfer = img_i2c_xfer,
  964. .functionality = img_i2c_func,
  965. };
  966. static int img_i2c_init(struct img_i2c *i2c)
  967. {
  968. unsigned int clk_khz, bitrate_khz, clk_period, tckh, tckl, tsdh;
  969. unsigned int i, ret, data, prescale, inc, int_bitrate, filt;
  970. struct img_i2c_timings timing;
  971. u32 rev;
  972. ret = clk_prepare_enable(i2c->scb_clk);
  973. if (ret)
  974. return ret;
  975. rev = img_i2c_readl(i2c, SCB_CORE_REV_REG);
  976. if ((rev & 0x00ffffff) < 0x00020200) {
  977. dev_info(i2c->adap.dev.parent,
  978. "Unknown hardware revision (%d.%d.%d.%d)\n",
  979. (rev >> 24) & 0xff, (rev >> 16) & 0xff,
  980. (rev >> 8) & 0xff, rev & 0xff);
  981. clk_disable_unprepare(i2c->scb_clk);
  982. return -EINVAL;
  983. }
  984. /* Fencing enabled by default. */
  985. i2c->need_wr_rd_fence = true;
  986. /* Determine what mode we're in from the bitrate */
  987. timing = timings[0];
  988. for (i = 0; i < ARRAY_SIZE(timings); i++) {
  989. if (i2c->bitrate <= timings[i].max_bitrate) {
  990. timing = timings[i];
  991. break;
  992. }
  993. }
  994. if (i2c->bitrate > timings[ARRAY_SIZE(timings) - 1].max_bitrate) {
  995. dev_warn(i2c->adap.dev.parent,
  996. "requested bitrate (%u) is higher than the max bitrate supported (%u)\n",
  997. i2c->bitrate,
  998. timings[ARRAY_SIZE(timings) - 1].max_bitrate);
  999. timing = timings[ARRAY_SIZE(timings) - 1];
  1000. i2c->bitrate = timing.max_bitrate;
  1001. }
  1002. bitrate_khz = i2c->bitrate / 1000;
  1003. clk_khz = clk_get_rate(i2c->scb_clk) / 1000;
  1004. /* Find the prescale that would give us that inc (approx delay = 0) */
  1005. prescale = SCB_OPT_INC * clk_khz / (256 * 16 * bitrate_khz);
  1006. prescale = clamp_t(unsigned int, prescale, 1, 8);
  1007. clk_khz /= prescale;
  1008. /* Setup the clock increment value */
  1009. inc = (256 * 16 * bitrate_khz) / clk_khz;
  1010. /*
  1011. * The clock generation logic allows to filter glitches on the bus.
  1012. * This filter is able to remove bus glitches shorter than 50ns.
  1013. * If the clock enable rate is greater than 20 MHz, no filtering
  1014. * is required, so we need to disable it.
  1015. * If it's between the 20-40 MHz range, there's no need to divide
  1016. * the clock to get a filter.
  1017. */
  1018. if (clk_khz < 20000) {
  1019. filt = SCB_FILT_DISABLE;
  1020. } else if (clk_khz < 40000) {
  1021. filt = SCB_FILT_BYPASS;
  1022. } else {
  1023. /* Calculate filter clock */
  1024. filt = (64000 / ((clk_khz / 1000) * SCB_FILT_GLITCH));
  1025. /* Scale up if needed */
  1026. if (64000 % ((clk_khz / 1000) * SCB_FILT_GLITCH))
  1027. inc++;
  1028. if (filt > SCB_FILT_INC_MASK)
  1029. filt = SCB_FILT_INC_MASK;
  1030. filt = (filt & SCB_FILT_INC_MASK) << SCB_FILT_INC_SHIFT;
  1031. }
  1032. data = filt | ((inc & SCB_INC_MASK) << SCB_INC_SHIFT) | (prescale - 1);
  1033. img_i2c_writel(i2c, SCB_CLK_SET_REG, data);
  1034. /* Obtain the clock period of the fx16 clock in ns */
  1035. clk_period = (256 * 1000000) / (clk_khz * inc);
  1036. /* Calculate the bitrate in terms of internal clock pulses */
  1037. int_bitrate = 1000000 / (bitrate_khz * clk_period);
  1038. if ((1000000 % (bitrate_khz * clk_period)) >=
  1039. ((bitrate_khz * clk_period) / 2))
  1040. int_bitrate++;
  1041. /*
  1042. * Setup clock duty cycle, start with 50% and adjust TCKH and TCKL
  1043. * values from there if they don't meet minimum timing requirements
  1044. */
  1045. tckh = int_bitrate / 2;
  1046. tckl = int_bitrate - tckh;
  1047. /* Adjust TCKH and TCKL values */
  1048. data = DIV_ROUND_UP(timing.tckl, clk_period);
  1049. if (tckl < data) {
  1050. tckl = data;
  1051. tckh = int_bitrate - tckl;
  1052. }
  1053. if (tckh > 0)
  1054. --tckh;
  1055. if (tckl > 0)
  1056. --tckl;
  1057. img_i2c_writel(i2c, SCB_TIME_TCKH_REG, tckh);
  1058. img_i2c_writel(i2c, SCB_TIME_TCKL_REG, tckl);
  1059. /* Setup TSDH value */
  1060. tsdh = DIV_ROUND_UP(timing.tsdh, clk_period);
  1061. if (tsdh > 1)
  1062. data = tsdh - 1;
  1063. else
  1064. data = 0x01;
  1065. img_i2c_writel(i2c, SCB_TIME_TSDH_REG, data);
  1066. /* This value is used later */
  1067. tsdh = data;
  1068. /* Setup TPL value */
  1069. data = timing.tpl / clk_period;
  1070. if (data > 0)
  1071. --data;
  1072. img_i2c_writel(i2c, SCB_TIME_TPL_REG, data);
  1073. /* Setup TPH value */
  1074. data = timing.tph / clk_period;
  1075. if (data > 0)
  1076. --data;
  1077. img_i2c_writel(i2c, SCB_TIME_TPH_REG, data);
  1078. /* Setup TSDL value to TPL + TSDH + 2 */
  1079. img_i2c_writel(i2c, SCB_TIME_TSDL_REG, data + tsdh + 2);
  1080. /* Setup TP2S value */
  1081. data = timing.tp2s / clk_period;
  1082. if (data > 0)
  1083. --data;
  1084. img_i2c_writel(i2c, SCB_TIME_TP2S_REG, data);
  1085. img_i2c_writel(i2c, SCB_TIME_TBI_REG, TIMEOUT_TBI);
  1086. img_i2c_writel(i2c, SCB_TIME_TSL_REG, TIMEOUT_TSL);
  1087. img_i2c_writel(i2c, SCB_TIME_TDL_REG, TIMEOUT_TDL);
  1088. /* Take module out of soft reset and enable clocks */
  1089. img_i2c_soft_reset(i2c);
  1090. /* Disable all interrupts */
  1091. img_i2c_writel(i2c, SCB_INT_MASK_REG, 0);
  1092. /* Clear all interrupts */
  1093. img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
  1094. /* Clear the scb_line_status events */
  1095. img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
  1096. /* Enable interrupts */
  1097. img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
  1098. /* Perform a synchronous sequence to reset the bus */
  1099. ret = img_i2c_reset_bus(i2c);
  1100. clk_disable_unprepare(i2c->scb_clk);
  1101. return ret;
  1102. }
  1103. static int img_i2c_probe(struct platform_device *pdev)
  1104. {
  1105. struct device_node *node = pdev->dev.of_node;
  1106. struct img_i2c *i2c;
  1107. struct resource *res;
  1108. int irq, ret;
  1109. u32 val;
  1110. i2c = devm_kzalloc(&pdev->dev, sizeof(struct img_i2c), GFP_KERNEL);
  1111. if (!i2c)
  1112. return -ENOMEM;
  1113. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1114. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  1115. if (IS_ERR(i2c->base))
  1116. return PTR_ERR(i2c->base);
  1117. irq = platform_get_irq(pdev, 0);
  1118. if (irq < 0) {
  1119. dev_err(&pdev->dev, "can't get irq number\n");
  1120. return irq;
  1121. }
  1122. i2c->sys_clk = devm_clk_get(&pdev->dev, "sys");
  1123. if (IS_ERR(i2c->sys_clk)) {
  1124. dev_err(&pdev->dev, "can't get system clock\n");
  1125. return PTR_ERR(i2c->sys_clk);
  1126. }
  1127. i2c->scb_clk = devm_clk_get(&pdev->dev, "scb");
  1128. if (IS_ERR(i2c->scb_clk)) {
  1129. dev_err(&pdev->dev, "can't get core clock\n");
  1130. return PTR_ERR(i2c->scb_clk);
  1131. }
  1132. ret = devm_request_irq(&pdev->dev, irq, img_i2c_isr, 0,
  1133. pdev->name, i2c);
  1134. if (ret) {
  1135. dev_err(&pdev->dev, "can't request irq %d\n", irq);
  1136. return ret;
  1137. }
  1138. /* Set up the exception check timer */
  1139. init_timer(&i2c->check_timer);
  1140. i2c->check_timer.function = img_i2c_check_timer;
  1141. i2c->check_timer.data = (unsigned long)i2c;
  1142. i2c->bitrate = timings[0].max_bitrate;
  1143. if (!of_property_read_u32(node, "clock-frequency", &val))
  1144. i2c->bitrate = val;
  1145. i2c_set_adapdata(&i2c->adap, i2c);
  1146. i2c->adap.dev.parent = &pdev->dev;
  1147. i2c->adap.dev.of_node = node;
  1148. i2c->adap.owner = THIS_MODULE;
  1149. i2c->adap.algo = &img_i2c_algo;
  1150. i2c->adap.retries = 5;
  1151. i2c->adap.nr = pdev->id;
  1152. snprintf(i2c->adap.name, sizeof(i2c->adap.name), "IMG SCB I2C");
  1153. img_i2c_switch_mode(i2c, MODE_INACTIVE);
  1154. spin_lock_init(&i2c->lock);
  1155. init_completion(&i2c->msg_complete);
  1156. platform_set_drvdata(pdev, i2c);
  1157. ret = clk_prepare_enable(i2c->sys_clk);
  1158. if (ret)
  1159. return ret;
  1160. ret = img_i2c_init(i2c);
  1161. if (ret)
  1162. goto disable_clk;
  1163. ret = i2c_add_numbered_adapter(&i2c->adap);
  1164. if (ret < 0) {
  1165. dev_err(&pdev->dev, "failed to add adapter\n");
  1166. goto disable_clk;
  1167. }
  1168. return 0;
  1169. disable_clk:
  1170. clk_disable_unprepare(i2c->sys_clk);
  1171. return ret;
  1172. }
  1173. static int img_i2c_remove(struct platform_device *dev)
  1174. {
  1175. struct img_i2c *i2c = platform_get_drvdata(dev);
  1176. i2c_del_adapter(&i2c->adap);
  1177. clk_disable_unprepare(i2c->sys_clk);
  1178. return 0;
  1179. }
  1180. #ifdef CONFIG_PM_SLEEP
  1181. static int img_i2c_suspend(struct device *dev)
  1182. {
  1183. struct img_i2c *i2c = dev_get_drvdata(dev);
  1184. img_i2c_switch_mode(i2c, MODE_SUSPEND);
  1185. clk_disable_unprepare(i2c->sys_clk);
  1186. return 0;
  1187. }
  1188. static int img_i2c_resume(struct device *dev)
  1189. {
  1190. struct img_i2c *i2c = dev_get_drvdata(dev);
  1191. int ret;
  1192. ret = clk_prepare_enable(i2c->sys_clk);
  1193. if (ret)
  1194. return ret;
  1195. img_i2c_init(i2c);
  1196. return 0;
  1197. }
  1198. #endif /* CONFIG_PM_SLEEP */
  1199. static SIMPLE_DEV_PM_OPS(img_i2c_pm, img_i2c_suspend, img_i2c_resume);
  1200. static const struct of_device_id img_scb_i2c_match[] = {
  1201. { .compatible = "img,scb-i2c" },
  1202. { }
  1203. };
  1204. MODULE_DEVICE_TABLE(of, img_scb_i2c_match);
  1205. static struct platform_driver img_scb_i2c_driver = {
  1206. .driver = {
  1207. .name = "img-i2c-scb",
  1208. .of_match_table = img_scb_i2c_match,
  1209. .pm = &img_i2c_pm,
  1210. },
  1211. .probe = img_i2c_probe,
  1212. .remove = img_i2c_remove,
  1213. };
  1214. module_platform_driver(img_scb_i2c_driver);
  1215. MODULE_AUTHOR("James Hogan <james.hogan@imgtec.com>");
  1216. MODULE_DESCRIPTION("IMG host I2C driver");
  1217. MODULE_LICENSE("GPL v2");