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