Skip to content

Commit c37faaf

Browse files
Linus WalleijBen Dooks
authored andcommitted
i2c-stu300: I2C STU300 stability updates
- blk clk is enabled when an irq arrives. The clk should be enabled, but just to make sure. - All error bits are handled no matter state machine state - All irq's will run complete() except for irq's that wasn't an event. - No more looking into status registers just in case an interrupt has happend and the irq handle wasn't executed. - irq_disable/enable are now separete functions. - clk settings calculation changed to round upwards instead of downwards. - Number of address send attempts before giving up is increased to 12 from 10 since it most times take 8 tries before getting through. Signed-off-by: Linus Walleij <linus.walleij@stericsson.com> Signed-off-by: Ben Dooks <ben-linux@fluff.org>
1 parent 6114978 commit c37faaf

File tree

1 file changed

+92
-65
lines changed

1 file changed

+92
-65
lines changed

drivers/i2c/busses/i2c-stu300.c

Lines changed: 92 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ enum stu300_error {
117117
STU300_ERROR_NONE = 0,
118118
STU300_ERROR_ACKNOWLEDGE_FAILURE,
119119
STU300_ERROR_BUS_ERROR,
120-
STU300_ERROR_ARBITRATION_LOST
120+
STU300_ERROR_ARBITRATION_LOST,
121+
STU300_ERROR_UNKNOWN
121122
};
122123

123124
/* timeout waiting for the controller to respond */
@@ -127,7 +128,7 @@ enum stu300_error {
127128
* The number of address send athemps tried before giving up.
128129
* If the first one failes it seems like 5 to 8 attempts are required.
129130
*/
130-
#define NUM_ADDR_RESEND_ATTEMPTS 10
131+
#define NUM_ADDR_RESEND_ATTEMPTS 12
131132

132133
/* I2C clock speed, in Hz 0-400kHz*/
133134
static unsigned int scl_frequency = 100000;
@@ -149,6 +150,7 @@ module_param(scl_frequency, uint, 0644);
149150
* @msg_index: index of current message
150151
* @msg_len: length of current message
151152
*/
153+
152154
struct stu300_dev {
153155
struct platform_device *pdev;
154156
struct i2c_adapter adapter;
@@ -188,6 +190,27 @@ static inline u32 stu300_r8(void __iomem *address)
188190
return readl(address) & 0x000000FFU;
189191
}
190192

193+
static void stu300_irq_enable(struct stu300_dev *dev)
194+
{
195+
u32 val;
196+
val = stu300_r8(dev->virtbase + I2C_CR);
197+
val |= I2C_CR_INTERRUPT_ENABLE;
198+
/* Twice paranoia (possible HW glitch) */
199+
stu300_wr8(val, dev->virtbase + I2C_CR);
200+
stu300_wr8(val, dev->virtbase + I2C_CR);
201+
}
202+
203+
static void stu300_irq_disable(struct stu300_dev *dev)
204+
{
205+
u32 val;
206+
val = stu300_r8(dev->virtbase + I2C_CR);
207+
val &= ~I2C_CR_INTERRUPT_ENABLE;
208+
/* Twice paranoia (possible HW glitch) */
209+
stu300_wr8(val, dev->virtbase + I2C_CR);
210+
stu300_wr8(val, dev->virtbase + I2C_CR);
211+
}
212+
213+
191214
/*
192215
* Tells whether a certain event or events occurred in
193216
* response to a command. The events represent states in
@@ -196,21 +219,39 @@ static inline u32 stu300_r8(void __iomem *address)
196219
* documentation and can only be treated as abstract state
197220
* machine states.
198221
*
199-
* @ret 0 = event has not occurred, any other value means
200-
* the event occurred.
222+
* @ret 0 = event has not occurred or unknown error, any
223+
* other value means the correct event occurred or an error.
201224
*/
225+
202226
static int stu300_event_occurred(struct stu300_dev *dev,
203227
enum stu300_event mr_event) {
204228
u32 status1;
205229
u32 status2;
206230

207231
/* What event happened? */
208232
status1 = stu300_r8(dev->virtbase + I2C_SR1);
233+
209234
if (!(status1 & I2C_SR1_EVF_IND))
210235
/* No event at all */
211236
return 0;
237+
212238
status2 = stu300_r8(dev->virtbase + I2C_SR2);
213239

240+
/* Block any multiple interrupts */
241+
stu300_irq_disable(dev);
242+
243+
/* Check for errors first */
244+
if (status2 & I2C_SR2_AF_IND) {
245+
dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
246+
return 1;
247+
} else if (status2 & I2C_SR2_BERR_IND) {
248+
dev->cmd_err = STU300_ERROR_BUS_ERROR;
249+
return 1;
250+
} else if (status2 & I2C_SR2_ARLO_IND) {
251+
dev->cmd_err = STU300_ERROR_ARBITRATION_LOST;
252+
return 1;
253+
}
254+
214255
switch (mr_event) {
215256
case STU300_EVENT_1:
216257
if (status1 & I2C_SR1_ADSL_IND)
@@ -221,10 +262,6 @@ static int stu300_event_occurred(struct stu300_dev *dev,
221262
case STU300_EVENT_7:
222263
case STU300_EVENT_8:
223264
if (status1 & I2C_SR1_BTF_IND) {
224-
if (status2 & I2C_SR2_AF_IND)
225-
dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
226-
else if (status2 & I2C_SR2_BERR_IND)
227-
dev->cmd_err = STU300_ERROR_BUS_ERROR;
228265
return 1;
229266
}
230267
break;
@@ -240,8 +277,6 @@ static int stu300_event_occurred(struct stu300_dev *dev,
240277
case STU300_EVENT_6:
241278
if (status2 & I2C_SR2_ENDAD_IND) {
242279
/* First check for any errors */
243-
if (status2 & I2C_SR2_AF_IND)
244-
dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
245280
return 1;
246281
}
247282
break;
@@ -252,8 +287,15 @@ static int stu300_event_occurred(struct stu300_dev *dev,
252287
default:
253288
break;
254289
}
255-
if (status2 & I2C_SR2_ARLO_IND)
256-
dev->cmd_err = STU300_ERROR_ARBITRATION_LOST;
290+
/* If we get here, we're on thin ice.
291+
* Here we are in a status where we have
292+
* gotten a response that does not match
293+
* what we requested.
294+
*/
295+
dev->cmd_err = STU300_ERROR_UNKNOWN;
296+
dev_err(&dev->pdev->dev,
297+
"Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n",
298+
mr_event, status1, status2);
257299
return 0;
258300
}
259301

@@ -262,21 +304,20 @@ static irqreturn_t stu300_irh(int irq, void *data)
262304
struct stu300_dev *dev = data;
263305
int res;
264306

307+
/* Just make sure that the block is clocked */
308+
clk_enable(dev->clk);
309+
265310
/* See if this was what we were waiting for */
266311
spin_lock(&dev->cmd_issue_lock);
267-
if (dev->cmd_event != STU300_EVENT_NONE) {
268-
res = stu300_event_occurred(dev, dev->cmd_event);
269-
if (res || dev->cmd_err != STU300_ERROR_NONE) {
270-
u32 val;
271-
272-
complete(&dev->cmd_complete);
273-
/* Block any multiple interrupts */
274-
val = stu300_r8(dev->virtbase + I2C_CR);
275-
val &= ~I2C_CR_INTERRUPT_ENABLE;
276-
stu300_wr8(val, dev->virtbase + I2C_CR);
277-
}
278-
}
312+
313+
res = stu300_event_occurred(dev, dev->cmd_event);
314+
if (res || dev->cmd_err != STU300_ERROR_NONE)
315+
complete(&dev->cmd_complete);
316+
279317
spin_unlock(&dev->cmd_issue_lock);
318+
319+
clk_disable(dev->clk);
320+
280321
return IRQ_HANDLED;
281322
}
282323

@@ -308,7 +349,6 @@ static int stu300_start_and_await_event(struct stu300_dev *dev,
308349
stu300_wr8(cr_value, dev->virtbase + I2C_CR);
309350
ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
310351
STU300_TIMEOUT);
311-
312352
if (ret < 0) {
313353
dev_err(&dev->pdev->dev,
314354
"wait_for_completion_interruptible_timeout() "
@@ -342,7 +382,6 @@ static int stu300_await_event(struct stu300_dev *dev,
342382
enum stu300_event mr_event)
343383
{
344384
int ret;
345-
u32 val;
346385

347386
if (unlikely(irqs_disabled())) {
348387
/* TODO: implement polling for this case if need be. */
@@ -354,36 +393,18 @@ static int stu300_await_event(struct stu300_dev *dev,
354393
/* Is it already here? */
355394
spin_lock_irq(&dev->cmd_issue_lock);
356395
dev->cmd_err = STU300_ERROR_NONE;
357-
if (stu300_event_occurred(dev, mr_event)) {
358-
spin_unlock_irq(&dev->cmd_issue_lock);
359-
goto exit_await_check_err;
360-
}
361-
init_completion(&dev->cmd_complete);
362-
dev->cmd_err = STU300_ERROR_NONE;
363396
dev->cmd_event = mr_event;
364397

365-
/* Turn on the I2C interrupt for current operation */
366-
val = stu300_r8(dev->virtbase + I2C_CR);
367-
val |= I2C_CR_INTERRUPT_ENABLE;
368-
stu300_wr8(val, dev->virtbase + I2C_CR);
369-
370-
/* Twice paranoia (possible HW glitch) */
371-
stu300_wr8(val, dev->virtbase + I2C_CR);
398+
init_completion(&dev->cmd_complete);
372399

373-
/* Check again: is it already here? */
374-
if (unlikely(stu300_event_occurred(dev, mr_event))) {
375-
/* Disable IRQ again. */
376-
val &= ~I2C_CR_INTERRUPT_ENABLE;
377-
stu300_wr8(val, dev->virtbase + I2C_CR);
378-
spin_unlock_irq(&dev->cmd_issue_lock);
379-
goto exit_await_check_err;
380-
}
400+
/* Turn on the I2C interrupt for current operation */
401+
stu300_irq_enable(dev);
381402

382403
/* Unlock the command block and wait for the event to occur */
383404
spin_unlock_irq(&dev->cmd_issue_lock);
405+
384406
ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
385407
STU300_TIMEOUT);
386-
387408
if (ret < 0) {
388409
dev_err(&dev->pdev->dev,
389410
"wait_for_completion_interruptible_timeout()"
@@ -401,7 +422,6 @@ static int stu300_await_event(struct stu300_dev *dev,
401422
return -ETIMEDOUT;
402423
}
403424

404-
exit_await_check_err:
405425
if (dev->cmd_err != STU300_ERROR_NONE) {
406426
if (mr_event != STU300_EVENT_6) {
407427
dev_err(&dev->pdev->dev, "controller "
@@ -457,18 +477,19 @@ struct stu300_clkset {
457477
};
458478

459479
static const struct stu300_clkset stu300_clktable[] = {
460-
{ 0, 0xFFU },
461-
{ 2500000, I2C_OAR2_FR_25_10MHZ },
462-
{ 10000000, I2C_OAR2_FR_10_1667MHZ },
463-
{ 16670000, I2C_OAR2_FR_1667_2667MHZ },
464-
{ 26670000, I2C_OAR2_FR_2667_40MHZ },
465-
{ 40000000, I2C_OAR2_FR_40_5333MHZ },
466-
{ 53330000, I2C_OAR2_FR_5333_66MHZ },
467-
{ 66000000, I2C_OAR2_FR_66_80MHZ },
468-
{ 80000000, I2C_OAR2_FR_80_100MHZ },
480+
{ 0, 0xFFU },
481+
{ 2500000, I2C_OAR2_FR_25_10MHZ },
482+
{ 10000000, I2C_OAR2_FR_10_1667MHZ },
483+
{ 16670000, I2C_OAR2_FR_1667_2667MHZ },
484+
{ 26670000, I2C_OAR2_FR_2667_40MHZ },
485+
{ 40000000, I2C_OAR2_FR_40_5333MHZ },
486+
{ 53330000, I2C_OAR2_FR_5333_66MHZ },
487+
{ 66000000, I2C_OAR2_FR_66_80MHZ },
488+
{ 80000000, I2C_OAR2_FR_80_100MHZ },
469489
{ 100000000, 0xFFU },
470490
};
471491

492+
472493
static int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate)
473494
{
474495

@@ -494,10 +515,10 @@ static int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate)
494515

495516
if (dev->speed > 100000)
496517
/* Fast Mode I2C */
497-
val = ((clkrate/dev->speed)-9)/3;
518+
val = ((clkrate/dev->speed) - 9)/3 + 1;
498519
else
499520
/* Standard Mode I2C */
500-
val = ((clkrate/dev->speed)-7)/2;
521+
val = ((clkrate/dev->speed) - 7)/2 + 1;
501522

502523
/* According to spec the divider must be > 2 */
503524
if (val < 0x002) {
@@ -557,6 +578,7 @@ static int stu300_init_hw(struct stu300_dev *dev)
557578
*/
558579
clkrate = clk_get_rate(dev->clk);
559580
ret = stu300_set_clk(dev, clkrate);
581+
560582
if (ret)
561583
return ret;
562584
/*
@@ -641,7 +663,6 @@ static int stu300_xfer_msg(struct i2c_adapter *adap,
641663
int attempts = 0;
642664
struct stu300_dev *dev = i2c_get_adapdata(adap);
643665

644-
645666
clk_enable(dev->clk);
646667

647668
/* Remove this if (0) to trace each and every message. */
@@ -715,14 +736,15 @@ static int stu300_xfer_msg(struct i2c_adapter *adap,
715736

716737
if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) {
717738
dev_dbg(&dev->pdev->dev, "managed to get address "
718-
"through after %d attempts\n", attempts);
739+
"through after %d attempts\n", attempts);
719740
} else if (attempts == NUM_ADDR_RESEND_ATTEMPTS) {
720741
dev_dbg(&dev->pdev->dev, "I give up, tried %d times "
721-
"to resend address.\n",
722-
NUM_ADDR_RESEND_ATTEMPTS);
742+
"to resend address.\n",
743+
NUM_ADDR_RESEND_ATTEMPTS);
723744
goto exit_disable;
724745
}
725746

747+
726748
if (msg->flags & I2C_M_RD) {
727749
/* READ: we read the actual bytes one at a time */
728750
for (i = 0; i < msg->len; i++) {
@@ -804,8 +826,10 @@ static int stu300_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
804826
{
805827
int ret = -1;
806828
int i;
829+
807830
struct stu300_dev *dev = i2c_get_adapdata(adap);
808831
dev->msg_len = num;
832+
809833
for (i = 0; i < num; i++) {
810834
/*
811835
* Another driver appears to send stop for each message,
@@ -817,6 +841,7 @@ static int stu300_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
817841
dev->msg_index = i;
818842

819843
ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1)));
844+
820845
if (ret != 0) {
821846
num = ret;
822847
break;
@@ -845,6 +870,7 @@ stu300_probe(struct platform_device *pdev)
845870
struct resource *res;
846871
int bus_nr;
847872
int ret = 0;
873+
char clk_name[] = "I2C0";
848874

849875
dev = kzalloc(sizeof(struct stu300_dev), GFP_KERNEL);
850876
if (!dev) {
@@ -854,7 +880,8 @@ stu300_probe(struct platform_device *pdev)
854880
}
855881

856882
bus_nr = pdev->id;
857-
dev->clk = clk_get(&pdev->dev, NULL);
883+
clk_name[3] += (char)bus_nr;
884+
dev->clk = clk_get(&pdev->dev, clk_name);
858885
if (IS_ERR(dev->clk)) {
859886
ret = PTR_ERR(dev->clk);
860887
dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");

0 commit comments

Comments
 (0)