i2c-mv64xxx.c 28 KB

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