i2c-mpc.c 20 KB

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