diff options
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/busses/i2c-pxa.c | 94 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-sh_mobile.c | 271 |
2 files changed, 285 insertions, 80 deletions
diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c index 44d838410f15..906f9b9d715d 100644 --- a/drivers/i2c/busses/i2c-pxa.c +++ b/drivers/i2c/busses/i2c-pxa.c | |||
@@ -38,7 +38,44 @@ | |||
38 | #include <asm/irq.h> | 38 | #include <asm/irq.h> |
39 | #include <asm/io.h> | 39 | #include <asm/io.h> |
40 | #include <mach/i2c.h> | 40 | #include <mach/i2c.h> |
41 | #include <mach/pxa-regs.h> | 41 | |
42 | /* | ||
43 | * I2C registers and bit definitions | ||
44 | */ | ||
45 | #define IBMR (0x00) | ||
46 | #define IDBR (0x08) | ||
47 | #define ICR (0x10) | ||
48 | #define ISR (0x18) | ||
49 | #define ISAR (0x20) | ||
50 | |||
51 | #define ICR_START (1 << 0) /* start bit */ | ||
52 | #define ICR_STOP (1 << 1) /* stop bit */ | ||
53 | #define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */ | ||
54 | #define ICR_TB (1 << 3) /* transfer byte bit */ | ||
55 | #define ICR_MA (1 << 4) /* master abort */ | ||
56 | #define ICR_SCLE (1 << 5) /* master clock enable */ | ||
57 | #define ICR_IUE (1 << 6) /* unit enable */ | ||
58 | #define ICR_GCD (1 << 7) /* general call disable */ | ||
59 | #define ICR_ITEIE (1 << 8) /* enable tx interrupts */ | ||
60 | #define ICR_IRFIE (1 << 9) /* enable rx interrupts */ | ||
61 | #define ICR_BEIE (1 << 10) /* enable bus error ints */ | ||
62 | #define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */ | ||
63 | #define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */ | ||
64 | #define ICR_SADIE (1 << 13) /* slave address detected int enable */ | ||
65 | #define ICR_UR (1 << 14) /* unit reset */ | ||
66 | #define ICR_FM (1 << 15) /* fast mode */ | ||
67 | |||
68 | #define ISR_RWM (1 << 0) /* read/write mode */ | ||
69 | #define ISR_ACKNAK (1 << 1) /* ack/nak status */ | ||
70 | #define ISR_UB (1 << 2) /* unit busy */ | ||
71 | #define ISR_IBB (1 << 3) /* bus busy */ | ||
72 | #define ISR_SSD (1 << 4) /* slave stop detected */ | ||
73 | #define ISR_ALD (1 << 5) /* arbitration loss detected */ | ||
74 | #define ISR_ITE (1 << 6) /* tx buffer empty */ | ||
75 | #define ISR_IRF (1 << 7) /* rx buffer full */ | ||
76 | #define ISR_GCAD (1 << 8) /* general call address detected */ | ||
77 | #define ISR_SAD (1 << 9) /* slave address detected */ | ||
78 | #define ISR_BED (1 << 10) /* bus error no ACK/NAK */ | ||
42 | 79 | ||
43 | struct pxa_i2c { | 80 | struct pxa_i2c { |
44 | spinlock_t lock; | 81 | spinlock_t lock; |
@@ -60,19 +97,21 @@ struct pxa_i2c { | |||
60 | u32 icrlog[32]; | 97 | u32 icrlog[32]; |
61 | 98 | ||
62 | void __iomem *reg_base; | 99 | void __iomem *reg_base; |
100 | unsigned int reg_shift; | ||
63 | 101 | ||
64 | unsigned long iobase; | 102 | unsigned long iobase; |
65 | unsigned long iosize; | 103 | unsigned long iosize; |
66 | 104 | ||
67 | int irq; | 105 | int irq; |
68 | int use_pio; | 106 | unsigned int use_pio :1; |
107 | unsigned int fast_mode :1; | ||
69 | }; | 108 | }; |
70 | 109 | ||
71 | #define _IBMR(i2c) ((i2c)->reg_base + 0) | 110 | #define _IBMR(i2c) ((i2c)->reg_base + (0x0 << (i2c)->reg_shift)) |
72 | #define _IDBR(i2c) ((i2c)->reg_base + 8) | 111 | #define _IDBR(i2c) ((i2c)->reg_base + (0x4 << (i2c)->reg_shift)) |
73 | #define _ICR(i2c) ((i2c)->reg_base + 0x10) | 112 | #define _ICR(i2c) ((i2c)->reg_base + (0x8 << (i2c)->reg_shift)) |
74 | #define _ISR(i2c) ((i2c)->reg_base + 0x18) | 113 | #define _ISR(i2c) ((i2c)->reg_base + (0xc << (i2c)->reg_shift)) |
75 | #define _ISAR(i2c) ((i2c)->reg_base + 0x20) | 114 | #define _ISAR(i2c) ((i2c)->reg_base + (0x10 << (i2c)->reg_shift)) |
76 | 115 | ||
77 | /* | 116 | /* |
78 | * I2C Slave mode address | 117 | * I2C Slave mode address |
@@ -188,14 +227,14 @@ static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c) | |||
188 | 227 | ||
189 | static void i2c_pxa_abort(struct pxa_i2c *i2c) | 228 | static void i2c_pxa_abort(struct pxa_i2c *i2c) |
190 | { | 229 | { |
191 | unsigned long timeout = jiffies + HZ/4; | 230 | int i = 250; |
192 | 231 | ||
193 | if (i2c_pxa_is_slavemode(i2c)) { | 232 | if (i2c_pxa_is_slavemode(i2c)) { |
194 | dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__); | 233 | dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__); |
195 | return; | 234 | return; |
196 | } | 235 | } |
197 | 236 | ||
198 | while (time_before(jiffies, timeout) && (readl(_IBMR(i2c)) & 0x1) == 0) { | 237 | while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) { |
199 | unsigned long icr = readl(_ICR(i2c)); | 238 | unsigned long icr = readl(_ICR(i2c)); |
200 | 239 | ||
201 | icr &= ~ICR_START; | 240 | icr &= ~ICR_START; |
@@ -205,7 +244,8 @@ static void i2c_pxa_abort(struct pxa_i2c *i2c) | |||
205 | 244 | ||
206 | show_state(i2c); | 245 | show_state(i2c); |
207 | 246 | ||
208 | msleep(1); | 247 | mdelay(1); |
248 | i --; | ||
209 | } | 249 | } |
210 | 250 | ||
211 | writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP), | 251 | writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP), |
@@ -364,7 +404,7 @@ static void i2c_pxa_reset(struct pxa_i2c *i2c) | |||
364 | writel(i2c->slave_addr, _ISAR(i2c)); | 404 | writel(i2c->slave_addr, _ISAR(i2c)); |
365 | 405 | ||
366 | /* set control register values */ | 406 | /* set control register values */ |
367 | writel(I2C_ICR_INIT, _ICR(i2c)); | 407 | writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c)); |
368 | 408 | ||
369 | #ifdef CONFIG_I2C_PXA_SLAVE | 409 | #ifdef CONFIG_I2C_PXA_SLAVE |
370 | dev_info(&i2c->adap.dev, "Enabling slave mode\n"); | 410 | dev_info(&i2c->adap.dev, "Enabling slave mode\n"); |
@@ -907,12 +947,6 @@ static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num | |||
907 | struct pxa_i2c *i2c = adap->algo_data; | 947 | struct pxa_i2c *i2c = adap->algo_data; |
908 | int ret, i; | 948 | int ret, i; |
909 | 949 | ||
910 | /* If the I2C controller is disabled we need to reset it (probably due | ||
911 | to a suspend/resume destroying state). We do this here as we can then | ||
912 | avoid worrying about resuming the controller before its users. */ | ||
913 | if (!(readl(_ICR(i2c)) & ICR_IUE)) | ||
914 | i2c_pxa_reset(i2c); | ||
915 | |||
916 | for (i = adap->retries; i >= 0; i--) { | 950 | for (i = adap->retries; i >= 0; i--) { |
917 | ret = i2c_pxa_do_xfer(i2c, msgs, num); | 951 | ret = i2c_pxa_do_xfer(i2c, msgs, num); |
918 | if (ret != I2C_RETRY) | 952 | if (ret != I2C_RETRY) |
@@ -993,6 +1027,7 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
993 | ret = -EIO; | 1027 | ret = -EIO; |
994 | goto eremap; | 1028 | goto eremap; |
995 | } | 1029 | } |
1030 | i2c->reg_shift = (cpu_is_pxa3xx() && (dev->id == 1)) ? 0 : 1; | ||
996 | 1031 | ||
997 | i2c->iobase = res->start; | 1032 | i2c->iobase = res->start; |
998 | i2c->iosize = res_len(res); | 1033 | i2c->iosize = res_len(res); |
@@ -1013,6 +1048,7 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
1013 | if (plat) { | 1048 | if (plat) { |
1014 | i2c->adap.class = plat->class; | 1049 | i2c->adap.class = plat->class; |
1015 | i2c->use_pio = plat->use_pio; | 1050 | i2c->use_pio = plat->use_pio; |
1051 | i2c->fast_mode = plat->fast_mode; | ||
1016 | } | 1052 | } |
1017 | 1053 | ||
1018 | if (i2c->use_pio) { | 1054 | if (i2c->use_pio) { |
@@ -1082,9 +1118,33 @@ static int __exit i2c_pxa_remove(struct platform_device *dev) | |||
1082 | return 0; | 1118 | return 0; |
1083 | } | 1119 | } |
1084 | 1120 | ||
1121 | #ifdef CONFIG_PM | ||
1122 | static int i2c_pxa_suspend_late(struct platform_device *dev, pm_message_t state) | ||
1123 | { | ||
1124 | struct pxa_i2c *i2c = platform_get_drvdata(dev); | ||
1125 | clk_disable(i2c->clk); | ||
1126 | return 0; | ||
1127 | } | ||
1128 | |||
1129 | static int i2c_pxa_resume_early(struct platform_device *dev) | ||
1130 | { | ||
1131 | struct pxa_i2c *i2c = platform_get_drvdata(dev); | ||
1132 | |||
1133 | clk_enable(i2c->clk); | ||
1134 | i2c_pxa_reset(i2c); | ||
1135 | |||
1136 | return 0; | ||
1137 | } | ||
1138 | #else | ||
1139 | #define i2c_pxa_suspend_late NULL | ||
1140 | #define i2c_pxa_resume_early NULL | ||
1141 | #endif | ||
1142 | |||
1085 | static struct platform_driver i2c_pxa_driver = { | 1143 | static struct platform_driver i2c_pxa_driver = { |
1086 | .probe = i2c_pxa_probe, | 1144 | .probe = i2c_pxa_probe, |
1087 | .remove = __exit_p(i2c_pxa_remove), | 1145 | .remove = __exit_p(i2c_pxa_remove), |
1146 | .suspend_late = i2c_pxa_suspend_late, | ||
1147 | .resume_early = i2c_pxa_resume_early, | ||
1088 | .driver = { | 1148 | .driver = { |
1089 | .name = "pxa2xx-i2c", | 1149 | .name = "pxa2xx-i2c", |
1090 | .owner = THIS_MODULE, | 1150 | .owner = THIS_MODULE, |
diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c index 840e634fa31f..640cbb237328 100644 --- a/drivers/i2c/busses/i2c-sh_mobile.c +++ b/drivers/i2c/busses/i2c-sh_mobile.c | |||
@@ -31,13 +31,84 @@ | |||
31 | #include <linux/clk.h> | 31 | #include <linux/clk.h> |
32 | #include <linux/io.h> | 32 | #include <linux/io.h> |
33 | 33 | ||
34 | /* Transmit operation: */ | ||
35 | /* */ | ||
36 | /* 0 byte transmit */ | ||
37 | /* BUS: S A8 ACK P */ | ||
38 | /* IRQ: DTE WAIT */ | ||
39 | /* ICIC: */ | ||
40 | /* ICCR: 0x94 0x90 */ | ||
41 | /* ICDR: A8 */ | ||
42 | /* */ | ||
43 | /* 1 byte transmit */ | ||
44 | /* BUS: S A8 ACK D8(1) ACK P */ | ||
45 | /* IRQ: DTE WAIT WAIT */ | ||
46 | /* ICIC: -DTE */ | ||
47 | /* ICCR: 0x94 0x90 */ | ||
48 | /* ICDR: A8 D8(1) */ | ||
49 | /* */ | ||
50 | /* 2 byte transmit */ | ||
51 | /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P */ | ||
52 | /* IRQ: DTE WAIT WAIT WAIT */ | ||
53 | /* ICIC: -DTE */ | ||
54 | /* ICCR: 0x94 0x90 */ | ||
55 | /* ICDR: A8 D8(1) D8(2) */ | ||
56 | /* */ | ||
57 | /* 3 bytes or more, +---------+ gets repeated */ | ||
58 | /* */ | ||
59 | /* */ | ||
60 | /* Receive operation: */ | ||
61 | /* */ | ||
62 | /* 0 byte receive - not supported since slave may hold SDA low */ | ||
63 | /* */ | ||
64 | /* 1 byte receive [TX] | [RX] */ | ||
65 | /* BUS: S A8 ACK | D8(1) ACK P */ | ||
66 | /* IRQ: DTE WAIT | WAIT DTE */ | ||
67 | /* ICIC: -DTE | +DTE */ | ||
68 | /* ICCR: 0x94 0x81 | 0xc0 */ | ||
69 | /* ICDR: A8 | D8(1) */ | ||
70 | /* */ | ||
71 | /* 2 byte receive [TX]| [RX] */ | ||
72 | /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P */ | ||
73 | /* IRQ: DTE WAIT | WAIT WAIT DTE */ | ||
74 | /* ICIC: -DTE | +DTE */ | ||
75 | /* ICCR: 0x94 0x81 | 0xc0 */ | ||
76 | /* ICDR: A8 | D8(1) D8(2) */ | ||
77 | /* */ | ||
78 | /* 3 byte receive [TX] | [RX] */ | ||
79 | /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */ | ||
80 | /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */ | ||
81 | /* ICIC: -DTE | +DTE */ | ||
82 | /* ICCR: 0x94 0x81 | 0xc0 */ | ||
83 | /* ICDR: A8 | D8(1) D8(2) D8(3) */ | ||
84 | /* */ | ||
85 | /* 4 bytes or more, this part is repeated +---------+ */ | ||
86 | /* */ | ||
87 | /* */ | ||
88 | /* Interrupt order and BUSY flag */ | ||
89 | /* ___ _ */ | ||
90 | /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */ | ||
91 | /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */ | ||
92 | /* */ | ||
93 | /* S D7 D6 D5 D4 D3 D2 D1 D0 P */ | ||
94 | /* ___ */ | ||
95 | /* WAIT IRQ ________________________________/ \___________ */ | ||
96 | /* TACK IRQ ____________________________________/ \_______ */ | ||
97 | /* DTE IRQ __________________________________________/ \_ */ | ||
98 | /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ | ||
99 | /* _______________________________________________ */ | ||
100 | /* BUSY __/ \_ */ | ||
101 | /* */ | ||
102 | |||
34 | enum sh_mobile_i2c_op { | 103 | enum sh_mobile_i2c_op { |
35 | OP_START = 0, | 104 | OP_START = 0, |
36 | OP_TX_ONLY, | 105 | OP_TX_FIRST, |
106 | OP_TX, | ||
37 | OP_TX_STOP, | 107 | OP_TX_STOP, |
38 | OP_TX_TO_RX, | 108 | OP_TX_TO_RX, |
39 | OP_RX_ONLY, | 109 | OP_RX, |
40 | OP_RX_STOP, | 110 | OP_RX_STOP, |
111 | OP_RX_STOP_DATA, | ||
41 | }; | 112 | }; |
42 | 113 | ||
43 | struct sh_mobile_i2c_data { | 114 | struct sh_mobile_i2c_data { |
@@ -127,25 +198,34 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, | |||
127 | spin_lock_irqsave(&pd->lock, flags); | 198 | spin_lock_irqsave(&pd->lock, flags); |
128 | 199 | ||
129 | switch (op) { | 200 | switch (op) { |
130 | case OP_START: | 201 | case OP_START: /* issue start and trigger DTE interrupt */ |
131 | iowrite8(0x94, ICCR(pd)); | 202 | iowrite8(0x94, ICCR(pd)); |
132 | break; | 203 | break; |
133 | case OP_TX_ONLY: | 204 | case OP_TX_FIRST: /* disable DTE interrupt and write data */ |
205 | iowrite8(ICIC_WAITE | ICIC_ALE | ICIC_TACKE, ICIC(pd)); | ||
134 | iowrite8(data, ICDR(pd)); | 206 | iowrite8(data, ICDR(pd)); |
135 | break; | 207 | break; |
136 | case OP_TX_STOP: | 208 | case OP_TX: /* write data */ |
137 | iowrite8(data, ICDR(pd)); | 209 | iowrite8(data, ICDR(pd)); |
138 | iowrite8(0x90, ICCR(pd)); | ||
139 | iowrite8(ICIC_ALE | ICIC_TACKE, ICIC(pd)); | ||
140 | break; | 210 | break; |
141 | case OP_TX_TO_RX: | 211 | case OP_TX_STOP: /* write data and issue a stop afterwards */ |
142 | iowrite8(data, ICDR(pd)); | 212 | iowrite8(data, ICDR(pd)); |
213 | iowrite8(0x90, ICCR(pd)); | ||
214 | break; | ||
215 | case OP_TX_TO_RX: /* select read mode */ | ||
143 | iowrite8(0x81, ICCR(pd)); | 216 | iowrite8(0x81, ICCR(pd)); |
144 | break; | 217 | break; |
145 | case OP_RX_ONLY: | 218 | case OP_RX: /* just read data */ |
146 | ret = ioread8(ICDR(pd)); | 219 | ret = ioread8(ICDR(pd)); |
147 | break; | 220 | break; |
148 | case OP_RX_STOP: | 221 | case OP_RX_STOP: /* enable DTE interrupt, issue stop */ |
222 | iowrite8(ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE, | ||
223 | ICIC(pd)); | ||
224 | iowrite8(0xc0, ICCR(pd)); | ||
225 | break; | ||
226 | case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ | ||
227 | iowrite8(ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE, | ||
228 | ICIC(pd)); | ||
149 | ret = ioread8(ICDR(pd)); | 229 | ret = ioread8(ICDR(pd)); |
150 | iowrite8(0xc0, ICCR(pd)); | 230 | iowrite8(0xc0, ICCR(pd)); |
151 | break; | 231 | break; |
@@ -157,58 +237,120 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, | |||
157 | return ret; | 237 | return ret; |
158 | } | 238 | } |
159 | 239 | ||
240 | static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd) | ||
241 | { | ||
242 | if (pd->pos == -1) | ||
243 | return 1; | ||
244 | |||
245 | return 0; | ||
246 | } | ||
247 | |||
248 | static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd) | ||
249 | { | ||
250 | if (pd->pos == (pd->msg->len - 1)) | ||
251 | return 1; | ||
252 | |||
253 | return 0; | ||
254 | } | ||
255 | |||
256 | static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd, | ||
257 | unsigned char *buf) | ||
258 | { | ||
259 | switch (pd->pos) { | ||
260 | case -1: | ||
261 | *buf = (pd->msg->addr & 0x7f) << 1; | ||
262 | *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0; | ||
263 | break; | ||
264 | default: | ||
265 | *buf = pd->msg->buf[pd->pos]; | ||
266 | } | ||
267 | } | ||
268 | |||
269 | static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd) | ||
270 | { | ||
271 | unsigned char data; | ||
272 | |||
273 | if (pd->pos == pd->msg->len) | ||
274 | return 1; | ||
275 | |||
276 | sh_mobile_i2c_get_data(pd, &data); | ||
277 | |||
278 | if (sh_mobile_i2c_is_last_byte(pd)) | ||
279 | i2c_op(pd, OP_TX_STOP, data); | ||
280 | else if (sh_mobile_i2c_is_first_byte(pd)) | ||
281 | i2c_op(pd, OP_TX_FIRST, data); | ||
282 | else | ||
283 | i2c_op(pd, OP_TX, data); | ||
284 | |||
285 | pd->pos++; | ||
286 | return 0; | ||
287 | } | ||
288 | |||
289 | static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd) | ||
290 | { | ||
291 | unsigned char data; | ||
292 | int real_pos; | ||
293 | |||
294 | do { | ||
295 | if (pd->pos <= -1) { | ||
296 | sh_mobile_i2c_get_data(pd, &data); | ||
297 | |||
298 | if (sh_mobile_i2c_is_first_byte(pd)) | ||
299 | i2c_op(pd, OP_TX_FIRST, data); | ||
300 | else | ||
301 | i2c_op(pd, OP_TX, data); | ||
302 | break; | ||
303 | } | ||
304 | |||
305 | if (pd->pos == 0) { | ||
306 | i2c_op(pd, OP_TX_TO_RX, 0); | ||
307 | break; | ||
308 | } | ||
309 | |||
310 | real_pos = pd->pos - 2; | ||
311 | |||
312 | if (pd->pos == pd->msg->len) { | ||
313 | if (real_pos < 0) { | ||
314 | i2c_op(pd, OP_RX_STOP, 0); | ||
315 | break; | ||
316 | } | ||
317 | data = i2c_op(pd, OP_RX_STOP_DATA, 0); | ||
318 | } else | ||
319 | data = i2c_op(pd, OP_RX, 0); | ||
320 | |||
321 | pd->msg->buf[real_pos] = data; | ||
322 | } while (0); | ||
323 | |||
324 | pd->pos++; | ||
325 | return pd->pos == (pd->msg->len + 2); | ||
326 | } | ||
327 | |||
160 | static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) | 328 | static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) |
161 | { | 329 | { |
162 | struct platform_device *dev = dev_id; | 330 | struct platform_device *dev = dev_id; |
163 | struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); | 331 | struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); |
164 | struct i2c_msg *msg = pd->msg; | 332 | unsigned char sr; |
165 | unsigned char data, sr; | 333 | int wakeup; |
166 | int wakeup = 0; | ||
167 | 334 | ||
168 | sr = ioread8(ICSR(pd)); | 335 | sr = ioread8(ICSR(pd)); |
169 | pd->sr |= sr; | 336 | pd->sr |= sr; /* remember state */ |
170 | 337 | ||
171 | dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, | 338 | dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, |
172 | (msg->flags & I2C_M_RD) ? "read" : "write", | 339 | (pd->msg->flags & I2C_M_RD) ? "read" : "write", |
173 | pd->pos, msg->len); | 340 | pd->pos, pd->msg->len); |
174 | 341 | ||
175 | if (sr & (ICSR_AL | ICSR_TACK)) { | 342 | if (sr & (ICSR_AL | ICSR_TACK)) { |
176 | iowrite8(0, ICIC(pd)); /* disable interrupts */ | 343 | /* don't interrupt transaction - continue to issue stop */ |
177 | wakeup = 1; | 344 | iowrite8(sr & ~(ICSR_AL | ICSR_TACK), ICSR(pd)); |
178 | goto do_wakeup; | 345 | wakeup = 0; |
179 | } | 346 | } else if (pd->msg->flags & I2C_M_RD) |
347 | wakeup = sh_mobile_i2c_isr_rx(pd); | ||
348 | else | ||
349 | wakeup = sh_mobile_i2c_isr_tx(pd); | ||
180 | 350 | ||
181 | if (pd->pos == msg->len) { | 351 | if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */ |
182 | i2c_op(pd, OP_RX_ONLY, 0); | 352 | iowrite8(sr & ~ICSR_WAIT, ICSR(pd)); |
183 | wakeup = 1; | ||
184 | goto do_wakeup; | ||
185 | } | ||
186 | 353 | ||
187 | if (pd->pos == -1) { | ||
188 | data = (msg->addr & 0x7f) << 1; | ||
189 | data |= (msg->flags & I2C_M_RD) ? 1 : 0; | ||
190 | } else | ||
191 | data = msg->buf[pd->pos]; | ||
192 | |||
193 | if ((pd->pos == -1) || !(msg->flags & I2C_M_RD)) { | ||
194 | if (msg->flags & I2C_M_RD) | ||
195 | i2c_op(pd, OP_TX_TO_RX, data); | ||
196 | else if (pd->pos == (msg->len - 1)) { | ||
197 | i2c_op(pd, OP_TX_STOP, data); | ||
198 | wakeup = 1; | ||
199 | } else | ||
200 | i2c_op(pd, OP_TX_ONLY, data); | ||
201 | } else { | ||
202 | if (pd->pos == (msg->len - 1)) | ||
203 | data = i2c_op(pd, OP_RX_STOP, 0); | ||
204 | else | ||
205 | data = i2c_op(pd, OP_RX_ONLY, 0); | ||
206 | |||
207 | msg->buf[pd->pos] = data; | ||
208 | } | ||
209 | pd->pos++; | ||
210 | |||
211 | do_wakeup: | ||
212 | if (wakeup) { | 354 | if (wakeup) { |
213 | pd->sr |= SW_DONE; | 355 | pd->sr |= SW_DONE; |
214 | wake_up(&pd->wait); | 356 | wake_up(&pd->wait); |
@@ -219,6 +361,11 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) | |||
219 | 361 | ||
220 | static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg) | 362 | static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg) |
221 | { | 363 | { |
364 | if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) { | ||
365 | dev_err(pd->dev, "Unsupported zero length i2c read\n"); | ||
366 | return -EIO; | ||
367 | } | ||
368 | |||
222 | /* Initialize channel registers */ | 369 | /* Initialize channel registers */ |
223 | iowrite8(ioread8(ICCR(pd)) & ~ICCR_ICE, ICCR(pd)); | 370 | iowrite8(ioread8(ICCR(pd)) & ~ICCR_ICE, ICCR(pd)); |
224 | 371 | ||
@@ -233,9 +380,8 @@ static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg) | |||
233 | pd->pos = -1; | 380 | pd->pos = -1; |
234 | pd->sr = 0; | 381 | pd->sr = 0; |
235 | 382 | ||
236 | /* Enable all interrupts except wait */ | 383 | /* Enable all interrupts to begin with */ |
237 | iowrite8(ioread8(ICIC(pd)) | ICIC_ALE | ICIC_TACKE | ICIC_DTEE, | 384 | iowrite8(ICIC_WAITE | ICIC_ALE | ICIC_TACKE | ICIC_DTEE, ICIC(pd)); |
238 | ICIC(pd)); | ||
239 | return 0; | 385 | return 0; |
240 | } | 386 | } |
241 | 387 | ||
@@ -268,25 +414,18 @@ static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, | |||
268 | if (!k) | 414 | if (!k) |
269 | dev_err(pd->dev, "Transfer request timed out\n"); | 415 | dev_err(pd->dev, "Transfer request timed out\n"); |
270 | 416 | ||
271 | retry_count = 10; | 417 | retry_count = 1000; |
272 | again: | 418 | again: |
273 | val = ioread8(ICSR(pd)); | 419 | val = ioread8(ICSR(pd)); |
274 | 420 | ||
275 | dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); | 421 | dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); |
276 | 422 | ||
277 | if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) { | ||
278 | err = -EIO; | ||
279 | break; | ||
280 | } | ||
281 | |||
282 | /* the interrupt handler may wake us up before the | 423 | /* the interrupt handler may wake us up before the |
283 | * transfer is finished, so poll the hardware | 424 | * transfer is finished, so poll the hardware |
284 | * until we're done. | 425 | * until we're done. |
285 | */ | 426 | */ |
286 | 427 | if (val & ICSR_BUSY) { | |
287 | if (!(!(val & ICSR_BUSY) && (val & ICSR_SCLM) && | 428 | udelay(10); |
288 | (val & ICSR_SDAM))) { | ||
289 | msleep(1); | ||
290 | if (retry_count--) | 429 | if (retry_count--) |
291 | goto again; | 430 | goto again; |
292 | 431 | ||
@@ -294,6 +433,12 @@ again: | |||
294 | dev_err(pd->dev, "Polling timed out\n"); | 433 | dev_err(pd->dev, "Polling timed out\n"); |
295 | break; | 434 | break; |
296 | } | 435 | } |
436 | |||
437 | /* handle missing acknowledge and arbitration lost */ | ||
438 | if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) { | ||
439 | err = -EIO; | ||
440 | break; | ||
441 | } | ||
297 | } | 442 | } |
298 | 443 | ||
299 | deactivate_ch(pd); | 444 | deactivate_ch(pd); |