16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
22 #include <linux/slab.h>
26 #include <linux/i2c.h>
33 #define DRV_NAME "mpc-i2c"
35 #define MPC_I2C_CLOCK_LEGACY 0
36 #define MPC_I2C_CLOCK_PRESERVE (~0U)
38 #define MPC_I2C_FDR 0x04
39 #define MPC_I2C_CR 0x08
40 #define MPC_I2C_SR 0x0c
41 #define MPC_I2C_DR 0x10
42 #define MPC_I2C_DFSRR 0x14
83 static inline void writeccr(
struct mpc_i2c *i2c,
u32 x)
105 static void mpc_i2c_fixup(
struct mpc_i2c *i2c)
113 for (k = 9;
k; k--) {
122 static int i2c_wait(
struct mpc_i2c *i2c,
unsigned timeout,
int writing)
124 unsigned long orig_jiffies =
jiffies;
131 if (
time_after(jiffies, orig_jiffies + timeout)) {
177 #if defined(CONFIG_PPC_MPC52xx) || defined(CONFIG_PPC_MPC512x)
179 {20, 0x20}, {22, 0x21}, {24, 0x22}, {26, 0x23},
180 {28, 0x24}, {30, 0x01}, {32, 0x25}, {34, 0x02},
181 {36, 0x26}, {40, 0x27}, {44, 0x04}, {48, 0x28},
182 {52, 0x63}, {56, 0x29}, {60, 0x41}, {64, 0x2a},
183 {68, 0x07}, {72, 0x2b}, {80, 0x2c}, {88, 0x09},
184 {96, 0x2d}, {104, 0x0a}, {112, 0x2e}, {120, 0x81},
185 {128, 0x2f}, {136, 0x47}, {144, 0x0c}, {160, 0x30},
186 {176, 0x49}, {192, 0x31}, {208, 0x4a}, {224, 0x32},
187 {240, 0x0f}, {256, 0x33}, {272, 0x87}, {288, 0x10},
188 {320, 0x34}, {352, 0x89}, {384, 0x35}, {416, 0x8a},
189 {448, 0x36}, {480, 0x13}, {512, 0x37}, {576, 0x14},
190 {640, 0x38}, {768, 0x39}, {896, 0x3a}, {960, 0x17},
191 {1024, 0x3b}, {1152, 0x18}, {1280, 0x3c}, {1536, 0x3d},
192 {1792, 0x3e}, {1920, 0x1b}, {2048, 0x3f}, {2304, 0x1c},
193 {2560, 0x1d}, {3072, 0x1e}, {3584, 0x7e}, {3840, 0x1f},
194 {4096, 0x7f}, {4608, 0x5c}, {5120, 0x5d}, {6144, 0x5e},
195 {7168, 0xbe}, {7680, 0x5f}, {8192, 0xbf}, {9216, 0x9c},
196 {10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
200 int prescaler,
u32 *real_clk)
220 for (i = 0; i <
ARRAY_SIZE(mpc_i2c_dividers_52xx); i++) {
221 div = &mpc_i2c_dividers_52xx[
i];
223 if (div->
fdr & 0xc0 && pvr == 0x80822011)
230 return (
int)div->
fdr;
245 ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler, &i2c->
real_clk);
246 fdr = (ret >= 0) ? ret : 0x3f;
262 #ifdef CONFIG_PPC_MPC512x
274 "fsl,mpc5121-i2c-ctrl");
280 idx = (*pval & 0xff) / 0x20;
281 setbits32(ctrl, 1 << (24 + idx * 2));
284 of_node_put(node_ctrl);
288 mpc_i2c_setup_52xx(node, i2c, clock, prescaler);
298 #ifdef CONFIG_FSL_SOC
299 static const struct mpc_i2c_divider mpc_i2c_dividers_8xxx[] __devinitconst = {
300 {160, 0x0120}, {192, 0x0121}, {224, 0x0122}, {256, 0x0123},
301 {288, 0x0100}, {320, 0x0101}, {352, 0x0601}, {384, 0x0102},
302 {416, 0x0602}, {448, 0x0126}, {480, 0x0103}, {512, 0x0127},
303 {544, 0x0b03}, {576, 0x0104}, {608, 0x1603}, {640, 0x0105},
304 {672, 0x2003}, {704, 0x0b05}, {736, 0x2b03}, {768, 0x0106},
305 {800, 0x3603}, {832, 0x0b06}, {896, 0x012a}, {960, 0x0107},
306 {1024, 0x012b}, {1088, 0x1607}, {1152, 0x0108}, {1216, 0x2b07},
307 {1280, 0x0109}, {1408, 0x1609}, {1536, 0x010a}, {1664, 0x160a},
308 {1792, 0x012e}, {1920, 0x010b}, {2048, 0x012f}, {2176, 0x2b0b},
309 {2304, 0x010c}, {2560, 0x010d}, {2816, 0x2b0d}, {3072, 0x010e},
310 {3328, 0x2b0e}, {3584, 0x0132}, {3840, 0x010f}, {4096, 0x0133},
311 {4608, 0x0110}, {5120, 0x0111}, {6144, 0x0112}, {7168, 0x0136},
312 {7680, 0x0113}, {8192, 0x0137}, {9216, 0x0114}, {10240, 0x0115},
313 {12288, 0x0116}, {14336, 0x013a}, {15360, 0x0117}, {16384, 0x013b},
314 {18432, 0x0118}, {20480, 0x0119}, {24576, 0x011a}, {28672, 0x013e},
315 {30720, 0x011b}, {32768, 0x013f}, {36864, 0x011c}, {40960, 0x011d},
316 {49152, 0x011e}, {61440, 0x011f}
336 "Error: couldn't map PORDEVSR2\n");
338 val =
in_be32(reg) & 0x00000080;
349 u32 prescaler,
u32 *real_clk)
363 prescaler = mpc_i2c_get_sec_cfg_8xxx() ? 3 : 2;
369 pr_debug(
"I2C: src_clock=%d clock=%d divider=%d\n",
376 for (i = 0; i <
ARRAY_SIZE(mpc_i2c_dividers_8xxx); i++) {
377 div = &mpc_i2c_dividers_8xxx[
i];
399 ret = mpc_i2c_get_fdr_8xxx(node, clock, prescaler, &i2c->
real_clk);
400 fdr = (ret >= 0) ? ret : 0x1031;
406 dev_info(i2c->
dev,
"clock %d Hz (dfsrr=%d fdr=%d)\n",
407 i2c->
real_clk, fdr >> 8, fdr & 0xff);
418 static void mpc_i2c_start(
struct mpc_i2c *i2c)
426 static void mpc_i2c_stop(
struct mpc_i2c *i2c)
435 unsigned timeout = i2c->
adap.timeout;
443 result = i2c_wait(i2c, timeout, 1);
447 for (i = 0; i <
length; i++) {
451 result = i2c_wait(i2c, timeout, 1);
459 static int mpc_read(
struct mpc_i2c *i2c,
int target,
460 u8 *data,
int length,
int restart,
bool recv_len)
462 unsigned timeout = i2c->
adap.timeout;
471 result = i2c_wait(i2c, timeout, 1);
476 if (length == 1 && !recv_len)
484 for (i = 0; i <
length; i++) {
487 result = i2c_wait(i2c, timeout, 0);
495 if (i || !recv_len) {
512 if (i == 0 && recv_len) {
535 unsigned long orig_jiffies =
jiffies;
536 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
551 if ((status & (CSR_MCF | CSR_MBB |
CSR_RXAK)) != 0) {
561 for (i = 0; ret >= 0 && i < num; i++) {
564 "Doing %s %d bytes to 0x%02x - %d of %d messages\n",
566 pmsg->
len, pmsg->
addr, i + 1, num);
570 ret = mpc_read(i2c, pmsg->
addr, pmsg->
buf, pmsg->
len, i,
572 if (recv_len && ret > 0)
576 mpc_write(i2c, pmsg->
addr, pmsg->
buf, pmsg->
len, i);
587 if ((status & (CSR_MCF | CSR_MBB |
CSR_RXAK)) != 0) {
596 return (ret < 0) ? ret : num;
606 .master_xfer = mpc_xfer,
607 .functionality = mpc_functionality,
612 .name =
"MPC adapter",
641 dev_err(i2c->
dev,
"failed to map controller\n");
651 dev_err(i2c->
dev,
"failed to attach interrupt\n");
661 if (prop && plen ==
sizeof(
u32))
671 mpc_i2c_setup_8xxx(op->
dev.of_node, i2c, clock, 0);
675 if (prop && plen ==
sizeof(
u32)) {
685 i2c_set_adapdata(&i2c->
adap, i2c);
686 i2c->
adap.dev.parent = &op->
dev;
687 i2c->
adap.dev.of_node = of_node_get(op->
dev.of_node);
726 static int mpc_i2c_suspend(
struct device *
dev)
736 static int mpc_i2c_resume(
struct device *
dev)
750 .setup = mpc_i2c_setup_512x,
753 static const struct mpc_i2c_data mpc_i2c_data_52xx __devinitdata = {
754 .
setup = mpc_i2c_setup_52xx,
757 static const struct mpc_i2c_data mpc_i2c_data_8313 __devinitdata = {
758 .
setup = mpc_i2c_setup_8xxx,
761 static const struct mpc_i2c_data mpc_i2c_data_8543 __devinitdata = {
762 .
setup = mpc_i2c_setup_8xxx,
766 static const struct mpc_i2c_data mpc_i2c_data_8544 __devinitdata = {
767 .
setup = mpc_i2c_setup_8xxx,
772 {.compatible =
"mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
773 {.compatible =
"fsl,mpc5200b-i2c", .data = &mpc_i2c_data_52xx, },
774 {.compatible =
"fsl,mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
775 {.compatible =
"fsl,mpc5121-i2c", .data = &mpc_i2c_data_512x, },
776 {.compatible =
"fsl,mpc8313-i2c", .data = &mpc_i2c_data_8313, },
777 {.compatible =
"fsl,mpc8543-i2c", .data = &mpc_i2c_data_8543, },
778 {.compatible =
"fsl,mpc8544-i2c", .data = &mpc_i2c_data_8544, },
780 {.compatible =
"fsl-i2c", },
787 .probe = fsl_i2c_probe,
792 .of_match_table = mpc_i2c_of_match,
794 .pm = &mpc_i2c_pm_ops,
803 "MPC824x/83xx/85xx/86xx/512x/52xx processors");