i2c-mpc.c 22 KB

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