i2c-img-scb.c 38 KB

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