i2c-mpc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This is a combined i2c adapter and algorithm driver for the
  4. * MPC107/Tsi107 PowerPC northbridge and processors that include
  5. * the same I2C unit (8240, 8245, 85xx).
  6. *
  7. * Copyright (C) 2003-2004 Humboldt Solutions Ltd, adrian@humboldt.co.uk
  8. * Copyright (C) 2021 Allied Telesis Labs
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/property.h>
  17. #include <linux/slab.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/fsl_devices.h>
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <asm/mpc52xx.h>
  26. #include <asm/mpc85xx.h>
  27. #include <sysdev/fsl_soc.h>
  28. #define DRV_NAME "mpc-i2c"
  29. #define MPC_I2C_CLOCK_LEGACY 0
  30. #define MPC_I2C_CLOCK_PRESERVE (~0U)
  31. #define MPC_I2C_FDR 0x04
  32. #define MPC_I2C_CR 0x08
  33. #define MPC_I2C_SR 0x0c
  34. #define MPC_I2C_DR 0x10
  35. #define MPC_I2C_DFSRR 0x14
  36. #define CCR_MEN 0x80
  37. #define CCR_MIEN 0x40
  38. #define CCR_MSTA 0x20
  39. #define CCR_MTX 0x10
  40. #define CCR_TXAK 0x08
  41. #define CCR_RSTA 0x04
  42. #define CCR_RSVD 0x02
  43. #define CSR_MCF 0x80
  44. #define CSR_MAAS 0x40
  45. #define CSR_MBB 0x20
  46. #define CSR_MAL 0x10
  47. #define CSR_SRW 0x04
  48. #define CSR_MIF 0x02
  49. #define CSR_RXAK 0x01
  50. enum mpc_i2c_action {
  51. MPC_I2C_ACTION_START = 1,
  52. MPC_I2C_ACTION_RESTART,
  53. MPC_I2C_ACTION_READ_BEGIN,
  54. MPC_I2C_ACTION_READ_BYTE,
  55. MPC_I2C_ACTION_WRITE,
  56. MPC_I2C_ACTION_STOP,
  57. __MPC_I2C_ACTION_CNT
  58. };
  59. static const char * const action_str[] = {
  60. "invalid",
  61. "start",
  62. "restart",
  63. "read begin",
  64. "read",
  65. "write",
  66. "stop",
  67. };
  68. static_assert(ARRAY_SIZE(action_str) == __MPC_I2C_ACTION_CNT);
  69. struct mpc_i2c {
  70. struct device *dev;
  71. void __iomem *base;
  72. u32 interrupt;
  73. wait_queue_head_t waitq;
  74. spinlock_t lock;
  75. struct i2c_adapter adap;
  76. int irq;
  77. u32 real_clk;
  78. u8 fdr, dfsrr;
  79. struct clk *clk_per;
  80. u32 cntl_bits;
  81. enum mpc_i2c_action action;
  82. struct i2c_msg *msgs;
  83. int num_msgs;
  84. int curr_msg;
  85. u32 byte_posn;
  86. u32 block;
  87. int rc;
  88. int expect_rxack;
  89. bool has_errata_A004447;
  90. };
  91. struct mpc_i2c_divider {
  92. u16 divider;
  93. u16 fdr; /* including dfsrr */
  94. };
  95. struct mpc_i2c_data {
  96. void (*setup)(struct device_node *node, struct mpc_i2c *i2c, u32 clock);
  97. };
  98. static inline void writeccr(struct mpc_i2c *i2c, u32 x)
  99. {
  100. writeb(x, i2c->base + MPC_I2C_CR);
  101. }
  102. /* Sometimes 9th clock pulse isn't generated, and slave doesn't release
  103. * the bus, because it wants to send ACK.
  104. * Following sequence of enabling/disabling and sending start/stop generates
  105. * the 9 pulses, each with a START then ending with STOP, so it's all OK.
  106. */
  107. static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  108. {
  109. int k;
  110. unsigned long flags;
  111. for (k = 9; k; k--) {
  112. writeccr(i2c, 0);
  113. writeb(0, i2c->base + MPC_I2C_SR); /* clear any status bits */
  114. writeccr(i2c, CCR_MEN | CCR_MSTA); /* START */
  115. readb(i2c->base + MPC_I2C_DR); /* init xfer */
  116. udelay(15); /* let it hit the bus */
  117. local_irq_save(flags); /* should not be delayed further */
  118. writeccr(i2c, CCR_MEN | CCR_MSTA | CCR_RSTA); /* delay SDA */
  119. readb(i2c->base + MPC_I2C_DR);
  120. if (k != 1)
  121. udelay(5);
  122. local_irq_restore(flags);
  123. }
  124. writeccr(i2c, CCR_MEN); /* Initiate STOP */
  125. readb(i2c->base + MPC_I2C_DR);
  126. udelay(15); /* Let STOP propagate */
  127. writeccr(i2c, 0);
  128. }
  129. static int i2c_mpc_wait_sr(struct mpc_i2c *i2c, int mask)
  130. {
  131. void __iomem *addr = i2c->base + MPC_I2C_SR;
  132. u8 val;
  133. return readb_poll_timeout(addr, val, val & mask, 0, 100);
  134. }
  135. /*
  136. * Workaround for Erratum A004447. From the P2040CE Rev Q
  137. *
  138. * 1. Set up the frequency divider and sampling rate.
  139. * 2. I2CCR - a0h
  140. * 3. Poll for I2CSR[MBB] to get set.
  141. * 4. If I2CSR[MAL] is set (an indication that SDA is stuck low), then go to
  142. * step 5. If MAL is not set, then go to step 13.
  143. * 5. I2CCR - 00h
  144. * 6. I2CCR - 22h
  145. * 7. I2CCR - a2h
  146. * 8. Poll for I2CSR[MBB] to get set.
  147. * 9. Issue read to I2CDR.
  148. * 10. Poll for I2CSR[MIF] to be set.
  149. * 11. I2CCR - 82h
  150. * 12. Workaround complete. Skip the next steps.
  151. * 13. Issue read to I2CDR.
  152. * 14. Poll for I2CSR[MIF] to be set.
  153. * 15. I2CCR - 80h
  154. */
  155. static void mpc_i2c_fixup_A004447(struct mpc_i2c *i2c)
  156. {
  157. int ret;
  158. u32 val;
  159. writeccr(i2c, CCR_MEN | CCR_MSTA);
  160. ret = i2c_mpc_wait_sr(i2c, CSR_MBB);
  161. if (ret) {
  162. dev_err(i2c->dev, "timeout waiting for CSR_MBB\n");
  163. return;
  164. }
  165. val = readb(i2c->base + MPC_I2C_SR);
  166. if (val & CSR_MAL) {
  167. writeccr(i2c, 0x00);
  168. writeccr(i2c, CCR_MSTA | CCR_RSVD);
  169. writeccr(i2c, CCR_MEN | CCR_MSTA | CCR_RSVD);
  170. ret = i2c_mpc_wait_sr(i2c, CSR_MBB);
  171. if (ret) {
  172. dev_err(i2c->dev, "timeout waiting for CSR_MBB\n");
  173. return;
  174. }
  175. val = readb(i2c->base + MPC_I2C_DR);
  176. ret = i2c_mpc_wait_sr(i2c, CSR_MIF);
  177. if (ret) {
  178. dev_err(i2c->dev, "timeout waiting for CSR_MIF\n");
  179. return;
  180. }
  181. writeccr(i2c, CCR_MEN | CCR_RSVD);
  182. } else {
  183. val = readb(i2c->base + MPC_I2C_DR);
  184. ret = i2c_mpc_wait_sr(i2c, CSR_MIF);
  185. if (ret) {
  186. dev_err(i2c->dev, "timeout waiting for CSR_MIF\n");
  187. return;
  188. }
  189. writeccr(i2c, CCR_MEN);
  190. }
  191. }
  192. #if defined(CONFIG_PPC_MPC52xx) || defined(CONFIG_PPC_MPC512x)
  193. static const struct mpc_i2c_divider mpc_i2c_dividers_52xx[] = {
  194. {20, 0x20}, {22, 0x21}, {24, 0x22}, {26, 0x23},
  195. {28, 0x24}, {30, 0x01}, {32, 0x25}, {34, 0x02},
  196. {36, 0x26}, {40, 0x27}, {44, 0x04}, {48, 0x28},
  197. {52, 0x63}, {56, 0x29}, {60, 0x41}, {64, 0x2a},
  198. {68, 0x07}, {72, 0x2b}, {80, 0x2c}, {88, 0x09},
  199. {96, 0x2d}, {104, 0x0a}, {112, 0x2e}, {120, 0x81},
  200. {128, 0x2f}, {136, 0x47}, {144, 0x0c}, {160, 0x30},
  201. {176, 0x49}, {192, 0x31}, {208, 0x4a}, {224, 0x32},
  202. {240, 0x0f}, {256, 0x33}, {272, 0x87}, {288, 0x10},
  203. {320, 0x34}, {352, 0x89}, {384, 0x35}, {416, 0x8a},
  204. {448, 0x36}, {480, 0x13}, {512, 0x37}, {576, 0x14},
  205. {640, 0x38}, {768, 0x39}, {896, 0x3a}, {960, 0x17},
  206. {1024, 0x3b}, {1152, 0x18}, {1280, 0x3c}, {1536, 0x3d},
  207. {1792, 0x3e}, {1920, 0x1b}, {2048, 0x3f}, {2304, 0x1c},
  208. {2560, 0x1d}, {3072, 0x1e}, {3584, 0x7e}, {3840, 0x1f},
  209. {4096, 0x7f}, {4608, 0x5c}, {5120, 0x5d}, {6144, 0x5e},
  210. {7168, 0xbe}, {7680, 0x5f}, {8192, 0xbf}, {9216, 0x9c},
  211. {10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
  212. };
  213. static int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock,
  214. u32 *real_clk)
  215. {
  216. struct fwnode_handle *fwnode = of_fwnode_handle(node);
  217. const struct mpc_i2c_divider *div = NULL;
  218. unsigned int pvr = mfspr(SPRN_PVR);
  219. u32 divider;
  220. int i;
  221. if (clock == MPC_I2C_CLOCK_LEGACY) {
  222. /* see below - default fdr = 0x3f -> div = 2048 */
  223. *real_clk = mpc5xxx_fwnode_get_bus_frequency(fwnode) / 2048;
  224. return -EINVAL;
  225. }
  226. /* Determine divider value */
  227. divider = mpc5xxx_fwnode_get_bus_frequency(fwnode) / clock;
  228. /*
  229. * We want to choose an FDR/DFSR that generates an I2C bus speed that
  230. * is equal to or lower than the requested speed.
  231. */
  232. for (i = 0; i < ARRAY_SIZE(mpc_i2c_dividers_52xx); i++) {
  233. div = &mpc_i2c_dividers_52xx[i];
  234. /* Old MPC5200 rev A CPUs do not support the high bits */
  235. if (div->fdr & 0xc0 && pvr == 0x80822011)
  236. continue;
  237. if (div->divider >= divider)
  238. break;
  239. }
  240. *real_clk = mpc5xxx_fwnode_get_bus_frequency(fwnode) / div->divider;
  241. return (int)div->fdr;
  242. }
  243. static void mpc_i2c_setup_52xx(struct device_node *node,
  244. struct mpc_i2c *i2c,
  245. u32 clock)
  246. {
  247. int ret, fdr;
  248. if (clock == MPC_I2C_CLOCK_PRESERVE) {
  249. dev_dbg(i2c->dev, "using fdr %d\n",
  250. readb(i2c->base + MPC_I2C_FDR));
  251. return;
  252. }
  253. ret = mpc_i2c_get_fdr_52xx(node, clock, &i2c->real_clk);
  254. fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */
  255. writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
  256. if (ret >= 0)
  257. dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk,
  258. fdr);
  259. }
  260. #else /* !(CONFIG_PPC_MPC52xx || CONFIG_PPC_MPC512x) */
  261. static void mpc_i2c_setup_52xx(struct device_node *node,
  262. struct mpc_i2c *i2c,
  263. u32 clock)
  264. {
  265. }
  266. #endif /* CONFIG_PPC_MPC52xx || CONFIG_PPC_MPC512x */
  267. #ifdef CONFIG_PPC_MPC512x
  268. static void mpc_i2c_setup_512x(struct device_node *node,
  269. struct mpc_i2c *i2c,
  270. u32 clock)
  271. {
  272. struct device_node *node_ctrl;
  273. void __iomem *ctrl;
  274. const u32 *pval;
  275. u32 idx;
  276. /* Enable I2C interrupts for mpc5121 */
  277. node_ctrl = of_find_compatible_node(NULL, NULL,
  278. "fsl,mpc5121-i2c-ctrl");
  279. if (node_ctrl) {
  280. ctrl = of_iomap(node_ctrl, 0);
  281. if (ctrl) {
  282. /* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
  283. pval = of_get_property(node, "reg", NULL);
  284. idx = (*pval & 0xff) / 0x20;
  285. setbits32(ctrl, 1 << (24 + idx * 2));
  286. iounmap(ctrl);
  287. }
  288. of_node_put(node_ctrl);
  289. }
  290. /* The clock setup for the 52xx works also fine for the 512x */
  291. mpc_i2c_setup_52xx(node, i2c, clock);
  292. }
  293. #else /* CONFIG_PPC_MPC512x */
  294. static void mpc_i2c_setup_512x(struct device_node *node,
  295. struct mpc_i2c *i2c,
  296. u32 clock)
  297. {
  298. }
  299. #endif /* CONFIG_PPC_MPC512x */
  300. #ifdef CONFIG_FSL_SOC
  301. static const struct mpc_i2c_divider mpc_i2c_dividers_8xxx[] = {
  302. {160, 0x0120}, {192, 0x0121}, {224, 0x0122}, {256, 0x0123},
  303. {288, 0x0100}, {320, 0x0101}, {352, 0x0601}, {384, 0x0102},
  304. {416, 0x0602}, {448, 0x0126}, {480, 0x0103}, {512, 0x0127},
  305. {544, 0x0b03}, {576, 0x0104}, {608, 0x1603}, {640, 0x0105},
  306. {672, 0x2003}, {704, 0x0b05}, {736, 0x2b03}, {768, 0x0106},
  307. {800, 0x3603}, {832, 0x0b06}, {896, 0x012a}, {960, 0x0107},
  308. {1024, 0x012b}, {1088, 0x1607}, {1152, 0x0108}, {1216, 0x2b07},
  309. {1280, 0x0109}, {1408, 0x1609}, {1536, 0x010a}, {1664, 0x160a},
  310. {1792, 0x012e}, {1920, 0x010b}, {2048, 0x012f}, {2176, 0x2b0b},
  311. {2304, 0x010c}, {2560, 0x010d}, {2816, 0x2b0d}, {3072, 0x010e},
  312. {3328, 0x2b0e}, {3584, 0x0132}, {3840, 0x010f}, {4096, 0x0133},
  313. {4608, 0x0110}, {5120, 0x0111}, {6144, 0x0112}, {7168, 0x0136},
  314. {7680, 0x0113}, {8192, 0x0137}, {9216, 0x0114}, {10240, 0x0115},
  315. {12288, 0x0116}, {14336, 0x013a}, {15360, 0x0117}, {16384, 0x013b},
  316. {18432, 0x0118}, {20480, 0x0119}, {24576, 0x011a}, {28672, 0x013e},
  317. {30720, 0x011b}, {32768, 0x013f}, {36864, 0x011c}, {40960, 0x011d},
  318. {49152, 0x011e}, {61440, 0x011f}
  319. };
  320. static u32 mpc_i2c_get_sec_cfg_8xxx(void)
  321. {
  322. struct device_node *node;
  323. u32 __iomem *reg;
  324. u32 val = 0;
  325. node = of_find_node_by_name(NULL, "global-utilities");
  326. if (node) {
  327. const u32 *prop = of_get_property(node, "reg", NULL);
  328. if (prop) {
  329. /*
  330. * Map and check POR Device Status Register 2
  331. * (PORDEVSR2) at 0xE0014. Note than while MPC8533
  332. * and MPC8544 indicate SEC frequency ratio
  333. * configuration as bit 26 in PORDEVSR2, other MPC8xxx
  334. * parts may store it differently or may not have it
  335. * at all.
  336. */
  337. reg = ioremap(get_immrbase() + *prop + 0x14, 0x4);
  338. if (!reg)
  339. printk(KERN_ERR
  340. "Error: couldn't map PORDEVSR2\n");
  341. else
  342. val = in_be32(reg) & 0x00000020; /* sec-cfg */
  343. iounmap(reg);
  344. }
  345. }
  346. of_node_put(node);
  347. return val;
  348. }
  349. static u32 mpc_i2c_get_prescaler_8xxx(void)
  350. {
  351. /*
  352. * According to the AN2919 all MPC824x have prescaler 1, while MPC83xx
  353. * may have prescaler 1, 2, or 3, depending on the power-on
  354. * configuration.
  355. */
  356. u32 prescaler = 1;
  357. /* mpc85xx */
  358. if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2)
  359. || pvr_version_is(PVR_VER_E500MC)
  360. || pvr_version_is(PVR_VER_E5500)
  361. || pvr_version_is(PVR_VER_E6500)) {
  362. unsigned int svr = mfspr(SPRN_SVR);
  363. if ((SVR_SOC_VER(svr) == SVR_8540)
  364. || (SVR_SOC_VER(svr) == SVR_8541)
  365. || (SVR_SOC_VER(svr) == SVR_8560)
  366. || (SVR_SOC_VER(svr) == SVR_8555)
  367. || (SVR_SOC_VER(svr) == SVR_8610))
  368. /* the above 85xx SoCs have prescaler 1 */
  369. prescaler = 1;
  370. else if ((SVR_SOC_VER(svr) == SVR_8533)
  371. || (SVR_SOC_VER(svr) == SVR_8544))
  372. /* the above 85xx SoCs have prescaler 3 or 2 */
  373. prescaler = mpc_i2c_get_sec_cfg_8xxx() ? 3 : 2;
  374. else
  375. /* all the other 85xx have prescaler 2 */
  376. prescaler = 2;
  377. }
  378. return prescaler;
  379. }
  380. static int mpc_i2c_get_fdr_8xxx(struct device_node *node, u32 clock,
  381. u32 *real_clk)
  382. {
  383. const struct mpc_i2c_divider *div = NULL;
  384. u32 prescaler = mpc_i2c_get_prescaler_8xxx();
  385. u32 divider;
  386. int i;
  387. if (clock == MPC_I2C_CLOCK_LEGACY) {
  388. /* see below - default fdr = 0x1031 -> div = 16 * 3072 */
  389. *real_clk = fsl_get_sys_freq() / prescaler / (16 * 3072);
  390. return -EINVAL;
  391. }
  392. divider = fsl_get_sys_freq() / clock / prescaler;
  393. pr_debug("I2C: src_clock=%d clock=%d divider=%d\n",
  394. fsl_get_sys_freq(), clock, divider);
  395. /*
  396. * We want to choose an FDR/DFSR that generates an I2C bus speed that
  397. * is equal to or lower than the requested speed.
  398. */
  399. for (i = 0; i < ARRAY_SIZE(mpc_i2c_dividers_8xxx); i++) {
  400. div = &mpc_i2c_dividers_8xxx[i];
  401. if (div->divider >= divider)
  402. break;
  403. }
  404. *real_clk = fsl_get_sys_freq() / prescaler / div->divider;
  405. return (int)div->fdr;
  406. }
  407. static void mpc_i2c_setup_8xxx(struct device_node *node,
  408. struct mpc_i2c *i2c,
  409. u32 clock)
  410. {
  411. int ret, fdr;
  412. if (clock == MPC_I2C_CLOCK_PRESERVE) {
  413. dev_dbg(i2c->dev, "using dfsrr %d, fdr %d\n",
  414. readb(i2c->base + MPC_I2C_DFSRR),
  415. readb(i2c->base + MPC_I2C_FDR));
  416. return;
  417. }
  418. ret = mpc_i2c_get_fdr_8xxx(node, clock, &i2c->real_clk);
  419. fdr = (ret >= 0) ? ret : 0x1031; /* backward compatibility */
  420. writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
  421. writeb((fdr >> 8) & 0xff, i2c->base + MPC_I2C_DFSRR);
  422. if (ret >= 0)
  423. dev_info(i2c->dev, "clock %d Hz (dfsrr=%d fdr=%d)\n",
  424. i2c->real_clk, fdr >> 8, fdr & 0xff);
  425. }
  426. #else /* !CONFIG_FSL_SOC */
  427. static void mpc_i2c_setup_8xxx(struct device_node *node,
  428. struct mpc_i2c *i2c,
  429. u32 clock)
  430. {
  431. }
  432. #endif /* CONFIG_FSL_SOC */
  433. static void mpc_i2c_finish(struct mpc_i2c *i2c, int rc)
  434. {
  435. i2c->rc = rc;
  436. i2c->block = 0;
  437. i2c->cntl_bits = CCR_MEN;
  438. writeccr(i2c, i2c->cntl_bits);
  439. wake_up(&i2c->waitq);
  440. }
  441. static void mpc_i2c_do_action(struct mpc_i2c *i2c)
  442. {
  443. struct i2c_msg *msg = NULL;
  444. int dir = 0;
  445. int recv_len = 0;
  446. u8 byte;
  447. dev_dbg(i2c->dev, "action = %s\n", action_str[i2c->action]);
  448. i2c->cntl_bits &= ~(CCR_RSTA | CCR_MTX | CCR_TXAK);
  449. if (i2c->action != MPC_I2C_ACTION_STOP) {
  450. msg = &i2c->msgs[i2c->curr_msg];
  451. if (msg->flags & I2C_M_RD)
  452. dir = 1;
  453. if (msg->flags & I2C_M_RECV_LEN)
  454. recv_len = 1;
  455. }
  456. switch (i2c->action) {
  457. case MPC_I2C_ACTION_RESTART:
  458. i2c->cntl_bits |= CCR_RSTA;
  459. fallthrough;
  460. case MPC_I2C_ACTION_START:
  461. i2c->cntl_bits |= CCR_MSTA | CCR_MTX;
  462. writeccr(i2c, i2c->cntl_bits);
  463. writeb((msg->addr << 1) | dir, i2c->base + MPC_I2C_DR);
  464. i2c->expect_rxack = 1;
  465. i2c->action = dir ? MPC_I2C_ACTION_READ_BEGIN : MPC_I2C_ACTION_WRITE;
  466. break;
  467. case MPC_I2C_ACTION_READ_BEGIN:
  468. if (msg->len) {
  469. if (msg->len == 1 && !(msg->flags & I2C_M_RECV_LEN))
  470. i2c->cntl_bits |= CCR_TXAK;
  471. writeccr(i2c, i2c->cntl_bits);
  472. /* Dummy read */
  473. readb(i2c->base + MPC_I2C_DR);
  474. }
  475. i2c->action = MPC_I2C_ACTION_READ_BYTE;
  476. break;
  477. case MPC_I2C_ACTION_READ_BYTE:
  478. if (i2c->byte_posn || !recv_len) {
  479. /* Generate Tx ACK on next to last byte */
  480. if (i2c->byte_posn == msg->len - 2)
  481. i2c->cntl_bits |= CCR_TXAK;
  482. /* Do not generate stop on last byte */
  483. if (i2c->byte_posn == msg->len - 1)
  484. i2c->cntl_bits |= CCR_MTX;
  485. writeccr(i2c, i2c->cntl_bits);
  486. }
  487. byte = readb(i2c->base + MPC_I2C_DR);
  488. if (i2c->byte_posn == 0 && recv_len) {
  489. if (byte == 0 || byte > I2C_SMBUS_BLOCK_MAX) {
  490. mpc_i2c_finish(i2c, -EPROTO);
  491. return;
  492. }
  493. msg->len += byte;
  494. /*
  495. * For block reads, generate Tx ACK here if data length
  496. * is 1 byte (total length is 2 bytes).
  497. */
  498. if (msg->len == 2) {
  499. i2c->cntl_bits |= CCR_TXAK;
  500. writeccr(i2c, i2c->cntl_bits);
  501. }
  502. }
  503. dev_dbg(i2c->dev, "%s %02x\n", action_str[i2c->action], byte);
  504. msg->buf[i2c->byte_posn++] = byte;
  505. break;
  506. case MPC_I2C_ACTION_WRITE:
  507. dev_dbg(i2c->dev, "%s %02x\n", action_str[i2c->action],
  508. msg->buf[i2c->byte_posn]);
  509. writeb(msg->buf[i2c->byte_posn++], i2c->base + MPC_I2C_DR);
  510. i2c->expect_rxack = 1;
  511. break;
  512. case MPC_I2C_ACTION_STOP:
  513. mpc_i2c_finish(i2c, 0);
  514. break;
  515. default:
  516. WARN(1, "Unexpected action %d\n", i2c->action);
  517. break;
  518. }
  519. if (msg && msg->len == i2c->byte_posn) {
  520. i2c->curr_msg++;
  521. i2c->byte_posn = 0;
  522. if (i2c->curr_msg == i2c->num_msgs) {
  523. i2c->action = MPC_I2C_ACTION_STOP;
  524. /*
  525. * We don't get another interrupt on read so
  526. * finish the transfer now
  527. */
  528. if (dir)
  529. mpc_i2c_finish(i2c, 0);
  530. } else {
  531. i2c->action = MPC_I2C_ACTION_RESTART;
  532. }
  533. }
  534. }
  535. static void mpc_i2c_do_intr(struct mpc_i2c *i2c, u8 status)
  536. {
  537. spin_lock(&i2c->lock);
  538. if (!(status & CSR_MCF)) {
  539. dev_dbg(i2c->dev, "unfinished\n");
  540. mpc_i2c_finish(i2c, -EIO);
  541. goto out;
  542. }
  543. if (status & CSR_MAL) {
  544. dev_dbg(i2c->dev, "arbitration lost\n");
  545. mpc_i2c_finish(i2c, -EAGAIN);
  546. goto out;
  547. }
  548. if (i2c->expect_rxack && (status & CSR_RXAK)) {
  549. dev_dbg(i2c->dev, "no Rx ACK\n");
  550. mpc_i2c_finish(i2c, -ENXIO);
  551. goto out;
  552. }
  553. i2c->expect_rxack = 0;
  554. mpc_i2c_do_action(i2c);
  555. out:
  556. spin_unlock(&i2c->lock);
  557. }
  558. static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
  559. {
  560. struct mpc_i2c *i2c = dev_id;
  561. u8 status;
  562. status = readb(i2c->base + MPC_I2C_SR);
  563. if (status & CSR_MIF) {
  564. /* Wait up to 100us for transfer to properly complete */
  565. readb_poll_timeout_atomic(i2c->base + MPC_I2C_SR, status, status & CSR_MCF, 0, 100);
  566. writeb(0, i2c->base + MPC_I2C_SR);
  567. mpc_i2c_do_intr(i2c, status);
  568. return IRQ_HANDLED;
  569. }
  570. return IRQ_NONE;
  571. }
  572. static int mpc_i2c_wait_for_completion(struct mpc_i2c *i2c)
  573. {
  574. long time_left;
  575. time_left = wait_event_timeout(i2c->waitq, !i2c->block, i2c->adap.timeout);
  576. if (!time_left)
  577. return -ETIMEDOUT;
  578. if (time_left < 0)
  579. return time_left;
  580. return 0;
  581. }
  582. static int mpc_i2c_execute_msg(struct mpc_i2c *i2c)
  583. {
  584. unsigned long orig_jiffies;
  585. unsigned long flags;
  586. int ret;
  587. spin_lock_irqsave(&i2c->lock, flags);
  588. i2c->curr_msg = 0;
  589. i2c->rc = 0;
  590. i2c->byte_posn = 0;
  591. i2c->block = 1;
  592. i2c->action = MPC_I2C_ACTION_START;
  593. i2c->cntl_bits = CCR_MEN | CCR_MIEN;
  594. writeb(0, i2c->base + MPC_I2C_SR);
  595. writeccr(i2c, i2c->cntl_bits);
  596. mpc_i2c_do_action(i2c);
  597. spin_unlock_irqrestore(&i2c->lock, flags);
  598. ret = mpc_i2c_wait_for_completion(i2c);
  599. if (ret)
  600. i2c->rc = ret;
  601. if (i2c->rc == -EIO || i2c->rc == -EAGAIN || i2c->rc == -ETIMEDOUT)
  602. i2c_recover_bus(&i2c->adap);
  603. orig_jiffies = jiffies;
  604. /* Wait until STOP is seen, allow up to 1 s */
  605. while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
  606. if (time_after(jiffies, orig_jiffies + HZ)) {
  607. u8 status = readb(i2c->base + MPC_I2C_SR);
  608. dev_dbg(i2c->dev, "timeout\n");
  609. if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
  610. writeb(status & ~CSR_MAL,
  611. i2c->base + MPC_I2C_SR);
  612. i2c_recover_bus(&i2c->adap);
  613. }
  614. return -EIO;
  615. }
  616. cond_resched();
  617. }
  618. return i2c->rc;
  619. }
  620. static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  621. {
  622. int rc, ret = num;
  623. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  624. int i;
  625. dev_dbg(i2c->dev, "num = %d\n", num);
  626. for (i = 0; i < num; i++)
  627. dev_dbg(i2c->dev, " addr = %02x, flags = %02x, len = %d, %*ph\n",
  628. msgs[i].addr, msgs[i].flags, msgs[i].len,
  629. msgs[i].flags & I2C_M_RD ? 0 : msgs[i].len,
  630. msgs[i].buf);
  631. WARN_ON(i2c->msgs != NULL);
  632. i2c->msgs = msgs;
  633. i2c->num_msgs = num;
  634. rc = mpc_i2c_execute_msg(i2c);
  635. if (rc < 0)
  636. ret = rc;
  637. i2c->num_msgs = 0;
  638. i2c->msgs = NULL;
  639. return ret;
  640. }
  641. static u32 mpc_functionality(struct i2c_adapter *adap)
  642. {
  643. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  644. | I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
  645. }
  646. static int fsl_i2c_bus_recovery(struct i2c_adapter *adap)
  647. {
  648. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  649. if (i2c->has_errata_A004447)
  650. mpc_i2c_fixup_A004447(i2c);
  651. else
  652. mpc_i2c_fixup(i2c);
  653. return 0;
  654. }
  655. static const struct i2c_algorithm mpc_algo = {
  656. .master_xfer = mpc_xfer,
  657. .functionality = mpc_functionality,
  658. };
  659. static struct i2c_adapter mpc_ops = {
  660. .owner = THIS_MODULE,
  661. .algo = &mpc_algo,
  662. .timeout = HZ,
  663. };
  664. static struct i2c_bus_recovery_info fsl_i2c_recovery_info = {
  665. .recover_bus = fsl_i2c_bus_recovery,
  666. };
  667. static int fsl_i2c_probe(struct platform_device *op)
  668. {
  669. const struct mpc_i2c_data *data;
  670. struct mpc_i2c *i2c;
  671. const u32 *prop;
  672. u32 clock = MPC_I2C_CLOCK_LEGACY;
  673. int result = 0;
  674. int plen;
  675. struct clk *clk;
  676. int err;
  677. i2c = devm_kzalloc(&op->dev, sizeof(*i2c), GFP_KERNEL);
  678. if (!i2c)
  679. return -ENOMEM;
  680. i2c->dev = &op->dev; /* for debug and error output */
  681. init_waitqueue_head(&i2c->waitq);
  682. spin_lock_init(&i2c->lock);
  683. i2c->base = devm_platform_ioremap_resource(op, 0);
  684. if (IS_ERR(i2c->base))
  685. return PTR_ERR(i2c->base);
  686. i2c->irq = platform_get_irq(op, 0);
  687. if (i2c->irq < 0)
  688. return i2c->irq;
  689. result = devm_request_irq(&op->dev, i2c->irq, mpc_i2c_isr,
  690. IRQF_SHARED, "i2c-mpc", i2c);
  691. if (result < 0) {
  692. dev_err(i2c->dev, "failed to attach interrupt\n");
  693. return result;
  694. }
  695. /*
  696. * enable clock for the I2C peripheral (non fatal),
  697. * keep a reference upon successful allocation
  698. */
  699. clk = devm_clk_get_optional(&op->dev, NULL);
  700. if (IS_ERR(clk))
  701. return PTR_ERR(clk);
  702. err = clk_prepare_enable(clk);
  703. if (err) {
  704. dev_err(&op->dev, "failed to enable clock\n");
  705. return err;
  706. }
  707. i2c->clk_per = clk;
  708. if (of_property_read_bool(op->dev.of_node, "fsl,preserve-clocking")) {
  709. clock = MPC_I2C_CLOCK_PRESERVE;
  710. } else {
  711. prop = of_get_property(op->dev.of_node, "clock-frequency",
  712. &plen);
  713. if (prop && plen == sizeof(u32))
  714. clock = *prop;
  715. }
  716. data = device_get_match_data(&op->dev);
  717. if (data) {
  718. data->setup(op->dev.of_node, i2c, clock);
  719. } else {
  720. /* Backwards compatibility */
  721. if (of_get_property(op->dev.of_node, "dfsrr", NULL))
  722. mpc_i2c_setup_8xxx(op->dev.of_node, i2c, clock);
  723. }
  724. prop = of_get_property(op->dev.of_node, "fsl,timeout", &plen);
  725. if (prop && plen == sizeof(u32)) {
  726. mpc_ops.timeout = *prop * HZ / 1000000;
  727. if (mpc_ops.timeout < 5)
  728. mpc_ops.timeout = 5;
  729. }
  730. dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ);
  731. if (of_property_read_bool(op->dev.of_node, "fsl,i2c-erratum-a004447"))
  732. i2c->has_errata_A004447 = true;
  733. i2c->adap = mpc_ops;
  734. scnprintf(i2c->adap.name, sizeof(i2c->adap.name),
  735. "MPC adapter (%s)", of_node_full_name(op->dev.of_node));
  736. i2c->adap.dev.parent = &op->dev;
  737. i2c->adap.nr = op->id;
  738. i2c->adap.dev.of_node = of_node_get(op->dev.of_node);
  739. i2c->adap.bus_recovery_info = &fsl_i2c_recovery_info;
  740. platform_set_drvdata(op, i2c);
  741. i2c_set_adapdata(&i2c->adap, i2c);
  742. result = i2c_add_numbered_adapter(&i2c->adap);
  743. if (result)
  744. goto fail_add;
  745. return 0;
  746. fail_add:
  747. clk_disable_unprepare(i2c->clk_per);
  748. return result;
  749. };
  750. static int fsl_i2c_remove(struct platform_device *op)
  751. {
  752. struct mpc_i2c *i2c = platform_get_drvdata(op);
  753. i2c_del_adapter(&i2c->adap);
  754. clk_disable_unprepare(i2c->clk_per);
  755. return 0;
  756. };
  757. static int __maybe_unused mpc_i2c_suspend(struct device *dev)
  758. {
  759. struct mpc_i2c *i2c = dev_get_drvdata(dev);
  760. i2c->fdr = readb(i2c->base + MPC_I2C_FDR);
  761. i2c->dfsrr = readb(i2c->base + MPC_I2C_DFSRR);
  762. return 0;
  763. }
  764. static int __maybe_unused mpc_i2c_resume(struct device *dev)
  765. {
  766. struct mpc_i2c *i2c = dev_get_drvdata(dev);
  767. writeb(i2c->fdr, i2c->base + MPC_I2C_FDR);
  768. writeb(i2c->dfsrr, i2c->base + MPC_I2C_DFSRR);
  769. return 0;
  770. }
  771. static SIMPLE_DEV_PM_OPS(mpc_i2c_pm_ops, mpc_i2c_suspend, mpc_i2c_resume);
  772. static const struct mpc_i2c_data mpc_i2c_data_512x = {
  773. .setup = mpc_i2c_setup_512x,
  774. };
  775. static const struct mpc_i2c_data mpc_i2c_data_52xx = {
  776. .setup = mpc_i2c_setup_52xx,
  777. };
  778. static const struct mpc_i2c_data mpc_i2c_data_8313 = {
  779. .setup = mpc_i2c_setup_8xxx,
  780. };
  781. static const struct mpc_i2c_data mpc_i2c_data_8543 = {
  782. .setup = mpc_i2c_setup_8xxx,
  783. };
  784. static const struct mpc_i2c_data mpc_i2c_data_8544 = {
  785. .setup = mpc_i2c_setup_8xxx,
  786. };
  787. static const struct of_device_id mpc_i2c_of_match[] = {
  788. {.compatible = "mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
  789. {.compatible = "fsl,mpc5200b-i2c", .data = &mpc_i2c_data_52xx, },
  790. {.compatible = "fsl,mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
  791. {.compatible = "fsl,mpc5121-i2c", .data = &mpc_i2c_data_512x, },
  792. {.compatible = "fsl,mpc8313-i2c", .data = &mpc_i2c_data_8313, },
  793. {.compatible = "fsl,mpc8543-i2c", .data = &mpc_i2c_data_8543, },
  794. {.compatible = "fsl,mpc8544-i2c", .data = &mpc_i2c_data_8544, },
  795. /* Backward compatibility */
  796. {.compatible = "fsl-i2c", },
  797. {},
  798. };
  799. MODULE_DEVICE_TABLE(of, mpc_i2c_of_match);
  800. /* Structure for a device driver */
  801. static struct platform_driver mpc_i2c_driver = {
  802. .probe = fsl_i2c_probe,
  803. .remove = fsl_i2c_remove,
  804. .driver = {
  805. .name = DRV_NAME,
  806. .of_match_table = mpc_i2c_of_match,
  807. .pm = &mpc_i2c_pm_ops,
  808. },
  809. };
  810. module_platform_driver(mpc_i2c_driver);
  811. MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
  812. MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "
  813. "MPC824x/83xx/85xx/86xx/512x/52xx processors");
  814. MODULE_LICENSE("GPL");