i2c-mv64xxx.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Driver for the i2c controller on the Marvell line of host bridges
  3. * (e.g, gt642[46]0, mv643[46]0, mv644[46]0, and Orion SoC family).
  4. *
  5. * Author: Mark A. Greer <mgreer@mvista.com>
  6. *
  7. * 2005 (c) MontaVista, Software, Inc. This file is licensed under
  8. * the terms of the GNU General Public License version 2. This program
  9. * is licensed "as is" without any warranty of any kind, whether express
  10. * or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mv643xx_i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/delay.h>
  28. #define MV64XXX_I2C_ADDR_ADDR(val) ((val & 0x7f) << 1)
  29. #define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7)
  30. #define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3)
  31. #define MV64XXX_I2C_REG_CONTROL_ACK BIT(2)
  32. #define MV64XXX_I2C_REG_CONTROL_IFLG BIT(3)
  33. #define MV64XXX_I2C_REG_CONTROL_STOP BIT(4)
  34. #define MV64XXX_I2C_REG_CONTROL_START BIT(5)
  35. #define MV64XXX_I2C_REG_CONTROL_TWSIEN BIT(6)
  36. #define MV64XXX_I2C_REG_CONTROL_INTEN BIT(7)
  37. /* Ctlr status values */
  38. #define MV64XXX_I2C_STATUS_BUS_ERR 0x00
  39. #define MV64XXX_I2C_STATUS_MAST_START 0x08
  40. #define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
  41. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
  42. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
  43. #define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
  44. #define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
  45. #define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
  46. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
  47. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
  48. #define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
  49. #define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
  50. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
  51. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
  52. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
  53. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
  54. #define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
  55. /* Register defines (I2C bridge) */
  56. #define MV64XXX_I2C_REG_TX_DATA_LO 0xc0
  57. #define MV64XXX_I2C_REG_TX_DATA_HI 0xc4
  58. #define MV64XXX_I2C_REG_RX_DATA_LO 0xc8
  59. #define MV64XXX_I2C_REG_RX_DATA_HI 0xcc
  60. #define MV64XXX_I2C_REG_BRIDGE_CONTROL 0xd0
  61. #define MV64XXX_I2C_REG_BRIDGE_STATUS 0xd4
  62. #define MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE 0xd8
  63. #define MV64XXX_I2C_REG_BRIDGE_INTR_MASK 0xdC
  64. #define MV64XXX_I2C_REG_BRIDGE_TIMING 0xe0
  65. /* Bridge Control values */
  66. #define MV64XXX_I2C_BRIDGE_CONTROL_WR BIT(0)
  67. #define MV64XXX_I2C_BRIDGE_CONTROL_RD BIT(1)
  68. #define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT 2
  69. #define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT BIT(12)
  70. #define MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT 13
  71. #define MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT 16
  72. #define MV64XXX_I2C_BRIDGE_CONTROL_ENABLE BIT(19)
  73. #define MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START BIT(20)
  74. /* Bridge Status values */
  75. #define MV64XXX_I2C_BRIDGE_STATUS_ERROR BIT(0)
  76. /* Driver states */
  77. enum {
  78. MV64XXX_I2C_STATE_INVALID,
  79. MV64XXX_I2C_STATE_IDLE,
  80. MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
  81. MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
  82. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
  83. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
  84. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
  85. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
  86. };
  87. /* Driver actions */
  88. enum {
  89. MV64XXX_I2C_ACTION_INVALID,
  90. MV64XXX_I2C_ACTION_CONTINUE,
  91. MV64XXX_I2C_ACTION_SEND_RESTART,
  92. MV64XXX_I2C_ACTION_SEND_ADDR_1,
  93. MV64XXX_I2C_ACTION_SEND_ADDR_2,
  94. MV64XXX_I2C_ACTION_SEND_DATA,
  95. MV64XXX_I2C_ACTION_RCV_DATA,
  96. MV64XXX_I2C_ACTION_RCV_DATA_STOP,
  97. MV64XXX_I2C_ACTION_SEND_STOP,
  98. };
  99. struct mv64xxx_i2c_regs {
  100. u8 addr;
  101. u8 ext_addr;
  102. u8 data;
  103. u8 control;
  104. u8 status;
  105. u8 clock;
  106. u8 soft_reset;
  107. };
  108. struct mv64xxx_i2c_data {
  109. struct i2c_msg *msgs;
  110. int num_msgs;
  111. int irq;
  112. u32 state;
  113. u32 action;
  114. u32 aborting;
  115. u32 cntl_bits;
  116. void __iomem *reg_base;
  117. struct mv64xxx_i2c_regs reg_offsets;
  118. u32 addr1;
  119. u32 addr2;
  120. u32 bytes_left;
  121. u32 byte_posn;
  122. u32 send_stop;
  123. u32 block;
  124. int rc;
  125. u32 freq_m;
  126. u32 freq_n;
  127. #if defined(CONFIG_HAVE_CLK)
  128. struct clk *clk;
  129. #endif
  130. wait_queue_head_t waitq;
  131. spinlock_t lock;
  132. struct i2c_msg *msg;
  133. struct i2c_adapter adapter;
  134. bool offload_enabled;
  135. /* 5us delay in order to avoid repeated start timing violation */
  136. bool errata_delay;
  137. struct reset_control *rstc;
  138. bool irq_clear_inverted;
  139. };
  140. static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = {
  141. .addr = 0x00,
  142. .ext_addr = 0x10,
  143. .data = 0x04,
  144. .control = 0x08,
  145. .status = 0x0c,
  146. .clock = 0x0c,
  147. .soft_reset = 0x1c,
  148. };
  149. static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_sun4i = {
  150. .addr = 0x00,
  151. .ext_addr = 0x04,
  152. .data = 0x08,
  153. .control = 0x0c,
  154. .status = 0x10,
  155. .clock = 0x14,
  156. .soft_reset = 0x18,
  157. };
  158. static void
  159. mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
  160. struct i2c_msg *msg)
  161. {
  162. u32 dir = 0;
  163. drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
  164. MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
  165. if (msg->flags & I2C_M_RD)
  166. dir = 1;
  167. if (msg->flags & I2C_M_TEN) {
  168. drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
  169. drv_data->addr2 = (u32)msg->addr & 0xff;
  170. } else {
  171. drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
  172. drv_data->addr2 = 0;
  173. }
  174. }
  175. /*
  176. *****************************************************************************
  177. *
  178. * Finite State Machine & Interrupt Routines
  179. *
  180. *****************************************************************************
  181. */
  182. /* Reset hardware and initialize FSM */
  183. static void
  184. mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
  185. {
  186. if (drv_data->offload_enabled) {
  187. writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
  188. writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_TIMING);
  189. writel(0, drv_data->reg_base +
  190. MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
  191. writel(0, drv_data->reg_base +
  192. MV64XXX_I2C_REG_BRIDGE_INTR_MASK);
  193. }
  194. writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset);
  195. writel(MV64XXX_I2C_BAUD_DIV_M(drv_data->freq_m) | MV64XXX_I2C_BAUD_DIV_N(drv_data->freq_n),
  196. drv_data->reg_base + drv_data->reg_offsets.clock);
  197. writel(0, drv_data->reg_base + drv_data->reg_offsets.addr);
  198. writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr);
  199. writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
  200. drv_data->reg_base + drv_data->reg_offsets.control);
  201. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  202. }
  203. static void
  204. mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
  205. {
  206. /*
  207. * If state is idle, then this is likely the remnants of an old
  208. * operation that driver has given up on or the user has killed.
  209. * If so, issue the stop condition and go to idle.
  210. */
  211. if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
  212. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  213. return;
  214. }
  215. /* The status from the ctlr [mostly] tells us what to do next */
  216. switch (status) {
  217. /* Start condition interrupt */
  218. case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
  219. case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
  220. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
  221. drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
  222. break;
  223. /* Performing a write */
  224. case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
  225. if (drv_data->msg->flags & I2C_M_TEN) {
  226. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
  227. drv_data->state =
  228. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
  229. break;
  230. }
  231. /* FALLTHRU */
  232. case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
  233. case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
  234. if ((drv_data->bytes_left == 0)
  235. || (drv_data->aborting
  236. && (drv_data->byte_posn != 0))) {
  237. if (drv_data->send_stop || drv_data->aborting) {
  238. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  239. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  240. } else {
  241. drv_data->action =
  242. MV64XXX_I2C_ACTION_SEND_RESTART;
  243. drv_data->state =
  244. MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
  245. }
  246. } else {
  247. drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
  248. drv_data->state =
  249. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
  250. drv_data->bytes_left--;
  251. }
  252. break;
  253. /* Performing a read */
  254. case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
  255. if (drv_data->msg->flags & I2C_M_TEN) {
  256. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
  257. drv_data->state =
  258. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
  259. break;
  260. }
  261. /* FALLTHRU */
  262. case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
  263. if (drv_data->bytes_left == 0) {
  264. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  265. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  266. break;
  267. }
  268. /* FALLTHRU */
  269. case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
  270. if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
  271. drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
  272. else {
  273. drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
  274. drv_data->bytes_left--;
  275. }
  276. drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
  277. if ((drv_data->bytes_left == 1) || drv_data->aborting)
  278. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
  279. break;
  280. case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
  281. drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
  282. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  283. break;
  284. case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
  285. case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
  286. case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
  287. /* Doesn't seem to be a device at other end */
  288. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  289. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  290. drv_data->rc = -ENXIO;
  291. break;
  292. default:
  293. dev_err(&drv_data->adapter.dev,
  294. "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
  295. "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
  296. drv_data->state, status, drv_data->msg->addr,
  297. drv_data->msg->flags);
  298. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  299. mv64xxx_i2c_hw_init(drv_data);
  300. drv_data->rc = -EIO;
  301. }
  302. }
  303. static void mv64xxx_i2c_send_start(struct mv64xxx_i2c_data *drv_data)
  304. {
  305. drv_data->msg = drv_data->msgs;
  306. drv_data->byte_posn = 0;
  307. drv_data->bytes_left = drv_data->msg->len;
  308. drv_data->aborting = 0;
  309. drv_data->rc = 0;
  310. mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
  311. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
  312. drv_data->reg_base + drv_data->reg_offsets.control);
  313. }
  314. static void
  315. mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
  316. {
  317. switch(drv_data->action) {
  318. case MV64XXX_I2C_ACTION_SEND_RESTART:
  319. /* We should only get here if we have further messages */
  320. BUG_ON(drv_data->num_msgs == 0);
  321. drv_data->msgs++;
  322. drv_data->num_msgs--;
  323. mv64xxx_i2c_send_start(drv_data);
  324. if (drv_data->errata_delay)
  325. udelay(5);
  326. /*
  327. * We're never at the start of the message here, and by this
  328. * time it's already too late to do any protocol mangling.
  329. * Thankfully, do not advertise support for that feature.
  330. */
  331. drv_data->send_stop = drv_data->num_msgs == 1;
  332. break;
  333. case MV64XXX_I2C_ACTION_CONTINUE:
  334. writel(drv_data->cntl_bits,
  335. drv_data->reg_base + drv_data->reg_offsets.control);
  336. break;
  337. case MV64XXX_I2C_ACTION_SEND_ADDR_1:
  338. writel(drv_data->addr1,
  339. drv_data->reg_base + drv_data->reg_offsets.data);
  340. writel(drv_data->cntl_bits,
  341. drv_data->reg_base + drv_data->reg_offsets.control);
  342. break;
  343. case MV64XXX_I2C_ACTION_SEND_ADDR_2:
  344. writel(drv_data->addr2,
  345. drv_data->reg_base + drv_data->reg_offsets.data);
  346. writel(drv_data->cntl_bits,
  347. drv_data->reg_base + drv_data->reg_offsets.control);
  348. break;
  349. case MV64XXX_I2C_ACTION_SEND_DATA:
  350. writel(drv_data->msg->buf[drv_data->byte_posn++],
  351. drv_data->reg_base + drv_data->reg_offsets.data);
  352. writel(drv_data->cntl_bits,
  353. drv_data->reg_base + drv_data->reg_offsets.control);
  354. break;
  355. case MV64XXX_I2C_ACTION_RCV_DATA:
  356. drv_data->msg->buf[drv_data->byte_posn++] =
  357. readl(drv_data->reg_base + drv_data->reg_offsets.data);
  358. writel(drv_data->cntl_bits,
  359. drv_data->reg_base + drv_data->reg_offsets.control);
  360. break;
  361. case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
  362. drv_data->msg->buf[drv_data->byte_posn++] =
  363. readl(drv_data->reg_base + drv_data->reg_offsets.data);
  364. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
  365. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
  366. drv_data->reg_base + drv_data->reg_offsets.control);
  367. drv_data->block = 0;
  368. if (drv_data->errata_delay)
  369. udelay(5);
  370. wake_up(&drv_data->waitq);
  371. break;
  372. case MV64XXX_I2C_ACTION_INVALID:
  373. default:
  374. dev_err(&drv_data->adapter.dev,
  375. "mv64xxx_i2c_do_action: Invalid action: %d\n",
  376. drv_data->action);
  377. drv_data->rc = -EIO;
  378. /* FALLTHRU */
  379. case MV64XXX_I2C_ACTION_SEND_STOP:
  380. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
  381. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
  382. drv_data->reg_base + drv_data->reg_offsets.control);
  383. drv_data->block = 0;
  384. wake_up(&drv_data->waitq);
  385. break;
  386. }
  387. }
  388. static void
  389. mv64xxx_i2c_read_offload_rx_data(struct mv64xxx_i2c_data *drv_data,
  390. struct i2c_msg *msg)
  391. {
  392. u32 buf[2];
  393. buf[0] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_LO);
  394. buf[1] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_HI);
  395. memcpy(msg->buf, buf, msg->len);
  396. }
  397. static int
  398. mv64xxx_i2c_intr_offload(struct mv64xxx_i2c_data *drv_data)
  399. {
  400. u32 cause, status;
  401. cause = readl(drv_data->reg_base +
  402. MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
  403. if (!cause)
  404. return IRQ_NONE;
  405. status = readl(drv_data->reg_base +
  406. MV64XXX_I2C_REG_BRIDGE_STATUS);
  407. if (status & MV64XXX_I2C_BRIDGE_STATUS_ERROR) {
  408. drv_data->rc = -EIO;
  409. goto out;
  410. }
  411. drv_data->rc = 0;
  412. /*
  413. * Transaction is a one message read transaction, read data
  414. * for this message.
  415. */
  416. if (drv_data->num_msgs == 1 && drv_data->msgs[0].flags & I2C_M_RD) {
  417. mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs);
  418. drv_data->msgs++;
  419. drv_data->num_msgs--;
  420. }
  421. /*
  422. * Transaction is a two messages write/read transaction, read
  423. * data for the second (read) message.
  424. */
  425. else if (drv_data->num_msgs == 2 &&
  426. !(drv_data->msgs[0].flags & I2C_M_RD) &&
  427. drv_data->msgs[1].flags & I2C_M_RD) {
  428. mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs + 1);
  429. drv_data->msgs += 2;
  430. drv_data->num_msgs -= 2;
  431. }
  432. out:
  433. writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
  434. writel(0, drv_data->reg_base +
  435. MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
  436. drv_data->block = 0;
  437. wake_up(&drv_data->waitq);
  438. return IRQ_HANDLED;
  439. }
  440. static irqreturn_t
  441. mv64xxx_i2c_intr(int irq, void *dev_id)
  442. {
  443. struct mv64xxx_i2c_data *drv_data = dev_id;
  444. unsigned long flags;
  445. u32 status;
  446. irqreturn_t rc = IRQ_NONE;
  447. spin_lock_irqsave(&drv_data->lock, flags);
  448. if (drv_data->offload_enabled)
  449. rc = mv64xxx_i2c_intr_offload(drv_data);
  450. while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
  451. MV64XXX_I2C_REG_CONTROL_IFLG) {
  452. status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
  453. mv64xxx_i2c_fsm(drv_data, status);
  454. mv64xxx_i2c_do_action(drv_data);
  455. if (drv_data->irq_clear_inverted)
  456. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_IFLG,
  457. drv_data->reg_base + drv_data->reg_offsets.control);
  458. rc = IRQ_HANDLED;
  459. }
  460. spin_unlock_irqrestore(&drv_data->lock, flags);
  461. return rc;
  462. }
  463. /*
  464. *****************************************************************************
  465. *
  466. * I2C Msg Execution Routines
  467. *
  468. *****************************************************************************
  469. */
  470. static void
  471. mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
  472. {
  473. long time_left;
  474. unsigned long flags;
  475. char abort = 0;
  476. time_left = wait_event_timeout(drv_data->waitq,
  477. !drv_data->block, drv_data->adapter.timeout);
  478. spin_lock_irqsave(&drv_data->lock, flags);
  479. if (!time_left) { /* Timed out */
  480. drv_data->rc = -ETIMEDOUT;
  481. abort = 1;
  482. } else if (time_left < 0) { /* Interrupted/Error */
  483. drv_data->rc = time_left; /* errno value */
  484. abort = 1;
  485. }
  486. if (abort && drv_data->block) {
  487. drv_data->aborting = 1;
  488. spin_unlock_irqrestore(&drv_data->lock, flags);
  489. time_left = wait_event_timeout(drv_data->waitq,
  490. !drv_data->block, drv_data->adapter.timeout);
  491. if ((time_left <= 0) && drv_data->block) {
  492. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  493. dev_err(&drv_data->adapter.dev,
  494. "mv64xxx: I2C bus locked, block: %d, "
  495. "time_left: %d\n", drv_data->block,
  496. (int)time_left);
  497. mv64xxx_i2c_hw_init(drv_data);
  498. }
  499. } else
  500. spin_unlock_irqrestore(&drv_data->lock, flags);
  501. }
  502. static int
  503. mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
  504. int is_last)
  505. {
  506. unsigned long flags;
  507. spin_lock_irqsave(&drv_data->lock, flags);
  508. drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
  509. drv_data->send_stop = is_last;
  510. drv_data->block = 1;
  511. mv64xxx_i2c_send_start(drv_data);
  512. spin_unlock_irqrestore(&drv_data->lock, flags);
  513. mv64xxx_i2c_wait_for_completion(drv_data);
  514. return drv_data->rc;
  515. }
  516. static void
  517. mv64xxx_i2c_prepare_tx(struct mv64xxx_i2c_data *drv_data)
  518. {
  519. struct i2c_msg *msg = drv_data->msgs;
  520. u32 buf[2];
  521. memcpy(buf, msg->buf, msg->len);
  522. writel(buf[0], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
  523. writel(buf[1], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
  524. }
  525. static int
  526. mv64xxx_i2c_offload_xfer(struct mv64xxx_i2c_data *drv_data)
  527. {
  528. struct i2c_msg *msgs = drv_data->msgs;
  529. int num = drv_data->num_msgs;
  530. unsigned long ctrl_reg;
  531. unsigned long flags;
  532. spin_lock_irqsave(&drv_data->lock, flags);
  533. /* Build transaction */
  534. ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE |
  535. (msgs[0].addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT);
  536. if (msgs[0].flags & I2C_M_TEN)
  537. ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT;
  538. /* Single write message transaction */
  539. if (num == 1 && !(msgs[0].flags & I2C_M_RD)) {
  540. size_t len = msgs[0].len - 1;
  541. ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
  542. (len << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT);
  543. mv64xxx_i2c_prepare_tx(drv_data);
  544. }
  545. /* Single read message transaction */
  546. else if (num == 1 && msgs[0].flags & I2C_M_RD) {
  547. size_t len = msgs[0].len - 1;
  548. ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD |
  549. (len << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT);
  550. }
  551. /*
  552. * Transaction with one write and one read message. This is
  553. * guaranteed by the mv64xx_i2c_can_offload() checks.
  554. */
  555. else if (num == 2) {
  556. size_t lentx = msgs[0].len - 1;
  557. size_t lenrx = msgs[1].len - 1;
  558. ctrl_reg |=
  559. MV64XXX_I2C_BRIDGE_CONTROL_RD |
  560. MV64XXX_I2C_BRIDGE_CONTROL_WR |
  561. (lentx << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT) |
  562. (lenrx << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT) |
  563. MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START;
  564. mv64xxx_i2c_prepare_tx(drv_data);
  565. }
  566. /* Execute transaction */
  567. drv_data->block = 1;
  568. writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
  569. spin_unlock_irqrestore(&drv_data->lock, flags);
  570. mv64xxx_i2c_wait_for_completion(drv_data);
  571. return drv_data->rc;
  572. }
  573. static bool
  574. mv64xxx_i2c_valid_offload_sz(struct i2c_msg *msg)
  575. {
  576. return msg->len <= 8 && msg->len >= 1;
  577. }
  578. static bool
  579. mv64xxx_i2c_can_offload(struct mv64xxx_i2c_data *drv_data)
  580. {
  581. struct i2c_msg *msgs = drv_data->msgs;
  582. int num = drv_data->num_msgs;
  583. return false;
  584. if (!drv_data->offload_enabled)
  585. return false;
  586. /*
  587. * We can offload a transaction consisting of a single
  588. * message, as long as the message has a length between 1 and
  589. * 8 bytes.
  590. */
  591. if (num == 1 && mv64xxx_i2c_valid_offload_sz(msgs))
  592. return true;
  593. /*
  594. * We can offload a transaction consisting of two messages, if
  595. * the first is a write and a second is a read, and both have
  596. * a length between 1 and 8 bytes.
  597. */
  598. if (num == 2 &&
  599. mv64xxx_i2c_valid_offload_sz(msgs) &&
  600. mv64xxx_i2c_valid_offload_sz(msgs + 1) &&
  601. !(msgs[0].flags & I2C_M_RD) &&
  602. msgs[1].flags & I2C_M_RD)
  603. return true;
  604. return false;
  605. }
  606. /*
  607. *****************************************************************************
  608. *
  609. * I2C Core Support Routines (Interface to higher level I2C code)
  610. *
  611. *****************************************************************************
  612. */
  613. static u32
  614. mv64xxx_i2c_functionality(struct i2c_adapter *adap)
  615. {
  616. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
  617. }
  618. static int
  619. mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  620. {
  621. struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
  622. int rc, ret = num;
  623. BUG_ON(drv_data->msgs != NULL);
  624. drv_data->msgs = msgs;
  625. drv_data->num_msgs = num;
  626. if (mv64xxx_i2c_can_offload(drv_data))
  627. rc = mv64xxx_i2c_offload_xfer(drv_data);
  628. else
  629. rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
  630. if (rc < 0)
  631. ret = rc;
  632. drv_data->num_msgs = 0;
  633. drv_data->msgs = NULL;
  634. return ret;
  635. }
  636. static const struct i2c_algorithm mv64xxx_i2c_algo = {
  637. .master_xfer = mv64xxx_i2c_xfer,
  638. .functionality = mv64xxx_i2c_functionality,
  639. };
  640. /*
  641. *****************************************************************************
  642. *
  643. * Driver Interface & Early Init Routines
  644. *
  645. *****************************************************************************
  646. */
  647. static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
  648. { .compatible = "allwinner,sun4i-a10-i2c", .data = &mv64xxx_i2c_regs_sun4i},
  649. { .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i},
  650. { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
  651. { .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
  652. { .compatible = "marvell,mv78230-a0-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
  653. {}
  654. };
  655. MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
  656. #ifdef CONFIG_OF
  657. #ifdef CONFIG_HAVE_CLK
  658. static int
  659. mv64xxx_calc_freq(const int tclk, const int n, const int m)
  660. {
  661. return tclk / (10 * (m + 1) * (2 << n));
  662. }
  663. static bool
  664. mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
  665. u32 *best_m)
  666. {
  667. int freq, delta, best_delta = INT_MAX;
  668. int m, n;
  669. for (n = 0; n <= 7; n++)
  670. for (m = 0; m <= 15; m++) {
  671. freq = mv64xxx_calc_freq(tclk, n, m);
  672. delta = req_freq - freq;
  673. if (delta >= 0 && delta < best_delta) {
  674. *best_m = m;
  675. *best_n = n;
  676. best_delta = delta;
  677. }
  678. if (best_delta == 0)
  679. return true;
  680. }
  681. if (best_delta == INT_MAX)
  682. return false;
  683. return true;
  684. }
  685. #endif /* CONFIG_HAVE_CLK */
  686. static int
  687. mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
  688. struct device *dev)
  689. {
  690. /* CLK is mandatory when using DT to describe the i2c bus. We
  691. * need to know tclk in order to calculate bus clock
  692. * factors.
  693. */
  694. #if !defined(CONFIG_HAVE_CLK)
  695. /* Have OF but no CLK */
  696. return -ENODEV;
  697. #else
  698. const struct of_device_id *device;
  699. struct device_node *np = dev->of_node;
  700. u32 bus_freq, tclk;
  701. int rc = 0;
  702. if (IS_ERR(drv_data->clk)) {
  703. rc = -ENODEV;
  704. goto out;
  705. }
  706. tclk = clk_get_rate(drv_data->clk);
  707. if (of_property_read_u32(np, "clock-frequency", &bus_freq))
  708. bus_freq = 100000; /* 100kHz by default */
  709. if (!mv64xxx_find_baud_factors(bus_freq, tclk,
  710. &drv_data->freq_n, &drv_data->freq_m)) {
  711. rc = -EINVAL;
  712. goto out;
  713. }
  714. drv_data->irq = irq_of_parse_and_map(np, 0);
  715. drv_data->rstc = devm_reset_control_get_optional(dev, NULL);
  716. if (IS_ERR(drv_data->rstc)) {
  717. if (PTR_ERR(drv_data->rstc) == -EPROBE_DEFER) {
  718. rc = -EPROBE_DEFER;
  719. goto out;
  720. }
  721. } else {
  722. reset_control_deassert(drv_data->rstc);
  723. }
  724. /* Its not yet defined how timeouts will be specified in device tree.
  725. * So hard code the value to 1 second.
  726. */
  727. drv_data->adapter.timeout = HZ;
  728. device = of_match_device(mv64xxx_i2c_of_match_table, dev);
  729. if (!device)
  730. return -ENODEV;
  731. memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets));
  732. /*
  733. * For controllers embedded in new SoCs activate the
  734. * Transaction Generator support and the errata fix.
  735. */
  736. if (of_device_is_compatible(np, "marvell,mv78230-i2c")) {
  737. drv_data->offload_enabled = true;
  738. drv_data->errata_delay = true;
  739. }
  740. if (of_device_is_compatible(np, "marvell,mv78230-a0-i2c")) {
  741. drv_data->offload_enabled = false;
  742. drv_data->errata_delay = true;
  743. }
  744. if (of_device_is_compatible(np, "allwinner,sun6i-a31-i2c"))
  745. drv_data->irq_clear_inverted = true;
  746. out:
  747. return rc;
  748. #endif
  749. }
  750. #else /* CONFIG_OF */
  751. static int
  752. mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
  753. struct device *dev)
  754. {
  755. return -ENODEV;
  756. }
  757. #endif /* CONFIG_OF */
  758. static int
  759. mv64xxx_i2c_probe(struct platform_device *pd)
  760. {
  761. struct mv64xxx_i2c_data *drv_data;
  762. struct mv64xxx_i2c_pdata *pdata = dev_get_platdata(&pd->dev);
  763. struct resource *r;
  764. int rc;
  765. if ((!pdata && !pd->dev.of_node))
  766. return -ENODEV;
  767. drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
  768. GFP_KERNEL);
  769. if (!drv_data)
  770. return -ENOMEM;
  771. r = platform_get_resource(pd, IORESOURCE_MEM, 0);
  772. drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
  773. if (IS_ERR(drv_data->reg_base))
  774. return PTR_ERR(drv_data->reg_base);
  775. strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
  776. sizeof(drv_data->adapter.name));
  777. init_waitqueue_head(&drv_data->waitq);
  778. spin_lock_init(&drv_data->lock);
  779. #if defined(CONFIG_HAVE_CLK)
  780. /* Not all platforms have a clk */
  781. drv_data->clk = devm_clk_get(&pd->dev, NULL);
  782. if (!IS_ERR(drv_data->clk)) {
  783. clk_prepare(drv_data->clk);
  784. clk_enable(drv_data->clk);
  785. }
  786. #endif
  787. if (pdata) {
  788. drv_data->freq_m = pdata->freq_m;
  789. drv_data->freq_n = pdata->freq_n;
  790. drv_data->irq = platform_get_irq(pd, 0);
  791. drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
  792. drv_data->offload_enabled = false;
  793. memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets));
  794. } else if (pd->dev.of_node) {
  795. rc = mv64xxx_of_config(drv_data, &pd->dev);
  796. if (rc)
  797. goto exit_clk;
  798. }
  799. if (drv_data->irq < 0) {
  800. rc = -ENXIO;
  801. goto exit_reset;
  802. }
  803. drv_data->adapter.dev.parent = &pd->dev;
  804. drv_data->adapter.algo = &mv64xxx_i2c_algo;
  805. drv_data->adapter.owner = THIS_MODULE;
  806. drv_data->adapter.class = I2C_CLASS_DEPRECATED;
  807. drv_data->adapter.nr = pd->id;
  808. drv_data->adapter.dev.of_node = pd->dev.of_node;
  809. platform_set_drvdata(pd, drv_data);
  810. i2c_set_adapdata(&drv_data->adapter, drv_data);
  811. mv64xxx_i2c_hw_init(drv_data);
  812. rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
  813. MV64XXX_I2C_CTLR_NAME, drv_data);
  814. if (rc) {
  815. dev_err(&drv_data->adapter.dev,
  816. "mv64xxx: Can't register intr handler irq%d: %d\n",
  817. drv_data->irq, rc);
  818. goto exit_reset;
  819. } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
  820. dev_err(&drv_data->adapter.dev,
  821. "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
  822. goto exit_free_irq;
  823. }
  824. return 0;
  825. exit_free_irq:
  826. free_irq(drv_data->irq, drv_data);
  827. exit_reset:
  828. if (!IS_ERR_OR_NULL(drv_data->rstc))
  829. reset_control_assert(drv_data->rstc);
  830. exit_clk:
  831. #if defined(CONFIG_HAVE_CLK)
  832. /* Not all platforms have a clk */
  833. if (!IS_ERR(drv_data->clk)) {
  834. clk_disable(drv_data->clk);
  835. clk_unprepare(drv_data->clk);
  836. }
  837. #endif
  838. return rc;
  839. }
  840. static int
  841. mv64xxx_i2c_remove(struct platform_device *dev)
  842. {
  843. struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
  844. i2c_del_adapter(&drv_data->adapter);
  845. free_irq(drv_data->irq, drv_data);
  846. if (!IS_ERR_OR_NULL(drv_data->rstc))
  847. reset_control_assert(drv_data->rstc);
  848. #if defined(CONFIG_HAVE_CLK)
  849. /* Not all platforms have a clk */
  850. if (!IS_ERR(drv_data->clk)) {
  851. clk_disable(drv_data->clk);
  852. clk_unprepare(drv_data->clk);
  853. }
  854. #endif
  855. return 0;
  856. }
  857. static struct platform_driver mv64xxx_i2c_driver = {
  858. .probe = mv64xxx_i2c_probe,
  859. .remove = mv64xxx_i2c_remove,
  860. .driver = {
  861. .name = MV64XXX_I2C_CTLR_NAME,
  862. .of_match_table = mv64xxx_i2c_of_match_table,
  863. },
  864. };
  865. module_platform_driver(mv64xxx_i2c_driver);
  866. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  867. MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
  868. MODULE_LICENSE("GPL");