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>

authored by Linus Walleij and committed by Ben Dooks c37faafa 61149787

+91 -64
+91 -64
drivers/i2c/busses/i2c-stu300.c
··· 117 117 STU300_ERROR_NONE = 0, 118 118 STU300_ERROR_ACKNOWLEDGE_FAILURE, 119 119 STU300_ERROR_BUS_ERROR, 120 - STU300_ERROR_ARBITRATION_LOST 120 + STU300_ERROR_ARBITRATION_LOST, 121 + STU300_ERROR_UNKNOWN 121 122 }; 122 123 123 124 /* timeout waiting for the controller to respond */ ··· 128 127 * The number of address send athemps tried before giving up. 129 128 * If the first one failes it seems like 5 to 8 attempts are required. 130 129 */ 131 - #define NUM_ADDR_RESEND_ATTEMPTS 10 130 + #define NUM_ADDR_RESEND_ATTEMPTS 12 132 131 133 132 /* I2C clock speed, in Hz 0-400kHz*/ 134 133 static unsigned int scl_frequency = 100000; ··· 150 149 * @msg_index: index of current message 151 150 * @msg_len: length of current message 152 151 */ 152 + 153 153 struct stu300_dev { 154 154 struct platform_device *pdev; 155 155 struct i2c_adapter adapter; ··· 190 188 return readl(address) & 0x000000FFU; 191 189 } 192 190 191 + static void stu300_irq_enable(struct stu300_dev *dev) 192 + { 193 + u32 val; 194 + val = stu300_r8(dev->virtbase + I2C_CR); 195 + val |= I2C_CR_INTERRUPT_ENABLE; 196 + /* Twice paranoia (possible HW glitch) */ 197 + stu300_wr8(val, dev->virtbase + I2C_CR); 198 + stu300_wr8(val, dev->virtbase + I2C_CR); 199 + } 200 + 201 + static void stu300_irq_disable(struct stu300_dev *dev) 202 + { 203 + u32 val; 204 + val = stu300_r8(dev->virtbase + I2C_CR); 205 + val &= ~I2C_CR_INTERRUPT_ENABLE; 206 + /* Twice paranoia (possible HW glitch) */ 207 + stu300_wr8(val, dev->virtbase + I2C_CR); 208 + stu300_wr8(val, dev->virtbase + I2C_CR); 209 + } 210 + 211 + 193 212 /* 194 213 * Tells whether a certain event or events occurred in 195 214 * response to a command. The events represent states in ··· 219 196 * documentation and can only be treated as abstract state 220 197 * machine states. 221 198 * 222 - * @ret 0 = event has not occurred, any other value means 223 - * the event occurred. 199 + * @ret 0 = event has not occurred or unknown error, any 200 + * other value means the correct event occurred or an error. 224 201 */ 202 + 225 203 static int stu300_event_occurred(struct stu300_dev *dev, 226 204 enum stu300_event mr_event) { 227 205 u32 status1; ··· 230 206 231 207 /* What event happened? */ 232 208 status1 = stu300_r8(dev->virtbase + I2C_SR1); 209 + 233 210 if (!(status1 & I2C_SR1_EVF_IND)) 234 211 /* No event at all */ 235 212 return 0; 213 + 236 214 status2 = stu300_r8(dev->virtbase + I2C_SR2); 215 + 216 + /* Block any multiple interrupts */ 217 + stu300_irq_disable(dev); 218 + 219 + /* Check for errors first */ 220 + if (status2 & I2C_SR2_AF_IND) { 221 + dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE; 222 + return 1; 223 + } else if (status2 & I2C_SR2_BERR_IND) { 224 + dev->cmd_err = STU300_ERROR_BUS_ERROR; 225 + return 1; 226 + } else if (status2 & I2C_SR2_ARLO_IND) { 227 + dev->cmd_err = STU300_ERROR_ARBITRATION_LOST; 228 + return 1; 229 + } 237 230 238 231 switch (mr_event) { 239 232 case STU300_EVENT_1: ··· 262 221 case STU300_EVENT_7: 263 222 case STU300_EVENT_8: 264 223 if (status1 & I2C_SR1_BTF_IND) { 265 - if (status2 & I2C_SR2_AF_IND) 266 - dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE; 267 - else if (status2 & I2C_SR2_BERR_IND) 268 - dev->cmd_err = STU300_ERROR_BUS_ERROR; 269 224 return 1; 270 225 } 271 226 break; ··· 277 240 case STU300_EVENT_6: 278 241 if (status2 & I2C_SR2_ENDAD_IND) { 279 242 /* First check for any errors */ 280 - if (status2 & I2C_SR2_AF_IND) 281 - dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE; 282 243 return 1; 283 244 } 284 245 break; ··· 287 252 default: 288 253 break; 289 254 } 290 - if (status2 & I2C_SR2_ARLO_IND) 291 - dev->cmd_err = STU300_ERROR_ARBITRATION_LOST; 255 + /* If we get here, we're on thin ice. 256 + * Here we are in a status where we have 257 + * gotten a response that does not match 258 + * what we requested. 259 + */ 260 + dev->cmd_err = STU300_ERROR_UNKNOWN; 261 + dev_err(&dev->pdev->dev, 262 + "Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n", 263 + mr_event, status1, status2); 292 264 return 0; 293 265 } 294 266 ··· 304 262 struct stu300_dev *dev = data; 305 263 int res; 306 264 265 + /* Just make sure that the block is clocked */ 266 + clk_enable(dev->clk); 267 + 307 268 /* See if this was what we were waiting for */ 308 269 spin_lock(&dev->cmd_issue_lock); 309 - if (dev->cmd_event != STU300_EVENT_NONE) { 310 - res = stu300_event_occurred(dev, dev->cmd_event); 311 - if (res || dev->cmd_err != STU300_ERROR_NONE) { 312 - u32 val; 313 270 314 - complete(&dev->cmd_complete); 315 - /* Block any multiple interrupts */ 316 - val = stu300_r8(dev->virtbase + I2C_CR); 317 - val &= ~I2C_CR_INTERRUPT_ENABLE; 318 - stu300_wr8(val, dev->virtbase + I2C_CR); 319 - } 320 - } 271 + res = stu300_event_occurred(dev, dev->cmd_event); 272 + if (res || dev->cmd_err != STU300_ERROR_NONE) 273 + complete(&dev->cmd_complete); 274 + 321 275 spin_unlock(&dev->cmd_issue_lock); 276 + 277 + clk_disable(dev->clk); 278 + 322 279 return IRQ_HANDLED; 323 280 } 324 281 ··· 349 308 stu300_wr8(cr_value, dev->virtbase + I2C_CR); 350 309 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, 351 310 STU300_TIMEOUT); 352 - 353 311 if (ret < 0) { 354 312 dev_err(&dev->pdev->dev, 355 313 "wait_for_completion_interruptible_timeout() " ··· 382 342 enum stu300_event mr_event) 383 343 { 384 344 int ret; 385 - u32 val; 386 345 387 346 if (unlikely(irqs_disabled())) { 388 347 /* TODO: implement polling for this case if need be. */ ··· 393 354 /* Is it already here? */ 394 355 spin_lock_irq(&dev->cmd_issue_lock); 395 356 dev->cmd_err = STU300_ERROR_NONE; 396 - if (stu300_event_occurred(dev, mr_event)) { 397 - spin_unlock_irq(&dev->cmd_issue_lock); 398 - goto exit_await_check_err; 399 - } 400 - init_completion(&dev->cmd_complete); 401 - dev->cmd_err = STU300_ERROR_NONE; 402 357 dev->cmd_event = mr_event; 403 358 359 + init_completion(&dev->cmd_complete); 360 + 404 361 /* Turn on the I2C interrupt for current operation */ 405 - val = stu300_r8(dev->virtbase + I2C_CR); 406 - val |= I2C_CR_INTERRUPT_ENABLE; 407 - stu300_wr8(val, dev->virtbase + I2C_CR); 408 - 409 - /* Twice paranoia (possible HW glitch) */ 410 - stu300_wr8(val, dev->virtbase + I2C_CR); 411 - 412 - /* Check again: is it already here? */ 413 - if (unlikely(stu300_event_occurred(dev, mr_event))) { 414 - /* Disable IRQ again. */ 415 - val &= ~I2C_CR_INTERRUPT_ENABLE; 416 - stu300_wr8(val, dev->virtbase + I2C_CR); 417 - spin_unlock_irq(&dev->cmd_issue_lock); 418 - goto exit_await_check_err; 419 - } 362 + stu300_irq_enable(dev); 420 363 421 364 /* Unlock the command block and wait for the event to occur */ 422 365 spin_unlock_irq(&dev->cmd_issue_lock); 366 + 423 367 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, 424 368 STU300_TIMEOUT); 425 - 426 369 if (ret < 0) { 427 370 dev_err(&dev->pdev->dev, 428 371 "wait_for_completion_interruptible_timeout()" ··· 422 401 return -ETIMEDOUT; 423 402 } 424 403 425 - exit_await_check_err: 426 404 if (dev->cmd_err != STU300_ERROR_NONE) { 427 405 if (mr_event != STU300_EVENT_6) { 428 406 dev_err(&dev->pdev->dev, "controller " ··· 477 457 }; 478 458 479 459 static const struct stu300_clkset stu300_clktable[] = { 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 }, 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 }, 489 469 { 100000000, 0xFFU }, 490 470 }; 471 + 491 472 492 473 static int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate) 493 474 { ··· 515 494 516 495 if (dev->speed > 100000) 517 496 /* Fast Mode I2C */ 518 - val = ((clkrate/dev->speed)-9)/3; 497 + val = ((clkrate/dev->speed) - 9)/3 + 1; 519 498 else 520 499 /* Standard Mode I2C */ 521 - val = ((clkrate/dev->speed)-7)/2; 500 + val = ((clkrate/dev->speed) - 7)/2 + 1; 522 501 523 502 /* According to spec the divider must be > 2 */ 524 503 if (val < 0x002) { ··· 578 557 */ 579 558 clkrate = clk_get_rate(dev->clk); 580 559 ret = stu300_set_clk(dev, clkrate); 560 + 581 561 if (ret) 582 562 return ret; 583 563 /* ··· 663 641 int attempts = 0; 664 642 struct stu300_dev *dev = i2c_get_adapdata(adap); 665 643 666 - 667 644 clk_enable(dev->clk); 668 645 669 646 /* Remove this if (0) to trace each and every message. */ ··· 736 715 737 716 if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) { 738 717 dev_dbg(&dev->pdev->dev, "managed to get address " 739 - "through after %d attempts\n", attempts); 718 + "through after %d attempts\n", attempts); 740 719 } else if (attempts == NUM_ADDR_RESEND_ATTEMPTS) { 741 720 dev_dbg(&dev->pdev->dev, "I give up, tried %d times " 742 - "to resend address.\n", 743 - NUM_ADDR_RESEND_ATTEMPTS); 721 + "to resend address.\n", 722 + NUM_ADDR_RESEND_ATTEMPTS); 744 723 goto exit_disable; 745 724 } 725 + 746 726 747 727 if (msg->flags & I2C_M_RD) { 748 728 /* READ: we read the actual bytes one at a time */ ··· 826 804 { 827 805 int ret = -1; 828 806 int i; 807 + 829 808 struct stu300_dev *dev = i2c_get_adapdata(adap); 830 809 dev->msg_len = num; 810 + 831 811 for (i = 0; i < num; i++) { 832 812 /* 833 813 * Another driver appears to send stop for each message, ··· 841 817 dev->msg_index = i; 842 818 843 819 ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1))); 820 + 844 821 if (ret != 0) { 845 822 num = ret; 846 823 break; ··· 870 845 struct resource *res; 871 846 int bus_nr; 872 847 int ret = 0; 848 + char clk_name[] = "I2C0"; 873 849 874 850 dev = kzalloc(sizeof(struct stu300_dev), GFP_KERNEL); 875 851 if (!dev) { ··· 880 854 } 881 855 882 856 bus_nr = pdev->id; 883 - dev->clk = clk_get(&pdev->dev, NULL); 857 + clk_name[3] += (char)bus_nr; 858 + dev->clk = clk_get(&pdev->dev, clk_name); 884 859 if (IS_ERR(dev->clk)) { 885 860 ret = PTR_ERR(dev->clk); 886 861 dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");