950-0033-i2c-bcm2835-Add-debug-support.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. From 390de19e8520898d19726ffa3d9a420349342442 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <[email protected]>
  3. Date: Tue, 1 Nov 2016 15:15:41 +0100
  4. Subject: [PATCH 033/454] i2c: bcm2835: Add debug support
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. This adds a debug module parameter to aid in debugging transfer issues
  9. by printing info to the kernel log. When enabled, status values are
  10. collected in the interrupt routine and msg info in
  11. bcm2835_i2c_start_transfer(). This is done in a way that tries to avoid
  12. affecting timing. Having printk in the isr can mask issues.
  13. debug values (additive):
  14. 1: Print info on error
  15. 2: Print info on all transfers
  16. 3: Print messages before transfer is started
  17. The value can be changed at runtime:
  18. /sys/module/i2c_bcm2835/parameters/debug
  19. Example output, debug=3:
  20. [ 747.114448] bcm2835_i2c_xfer: msg(1/2) write addr=0x54, len=2 flags= [i2c1]
  21. [ 747.114463] bcm2835_i2c_xfer: msg(2/2) read addr=0x54, len=32 flags= [i2c1]
  22. [ 747.117809] start_transfer: msg(1/2) write addr=0x54, len=2 flags= [i2c1]
  23. [ 747.117825] isr: remain=2, status=0x30000055 : TA TXW TXD TXE [i2c1]
  24. [ 747.117839] start_transfer: msg(2/2) read addr=0x54, len=32 flags= [i2c1]
  25. [ 747.117849] isr: remain=32, status=0xd0000039 : TA RXR TXD RXD [i2c1]
  26. [ 747.117861] isr: remain=20, status=0xd0000039 : TA RXR TXD RXD [i2c1]
  27. [ 747.117870] isr: remain=8, status=0x32 : DONE TXD RXD [i2c1]
  28. Signed-off-by: Noralf Trønnes <[email protected]>
  29. ---
  30. drivers/i2c/busses/i2c-bcm2835.c | 99 +++++++++++++++++++++++++++++++-
  31. 1 file changed, 98 insertions(+), 1 deletion(-)
  32. --- a/drivers/i2c/busses/i2c-bcm2835.c
  33. +++ b/drivers/i2c/busses/i2c-bcm2835.c
  34. @@ -56,6 +56,18 @@
  35. #define BCM2835_I2C_CDIV_MIN 0x0002
  36. #define BCM2835_I2C_CDIV_MAX 0xFFFE
  37. +static unsigned int debug;
  38. +module_param(debug, uint, 0644);
  39. +MODULE_PARM_DESC(debug, "1=err, 2=isr, 3=xfer");
  40. +
  41. +#define BCM2835_DEBUG_MAX 512
  42. +struct bcm2835_debug {
  43. + struct i2c_msg *msg;
  44. + int msg_idx;
  45. + size_t remain;
  46. + u32 status;
  47. +};
  48. +
  49. struct bcm2835_i2c_dev {
  50. struct device *dev;
  51. void __iomem *regs;
  52. @@ -69,8 +81,78 @@ struct bcm2835_i2c_dev {
  53. u32 msg_err;
  54. u8 *msg_buf;
  55. size_t msg_buf_remaining;
  56. + struct bcm2835_debug debug[BCM2835_DEBUG_MAX];
  57. + unsigned int debug_num;
  58. + unsigned int debug_num_msgs;
  59. };
  60. +static inline void bcm2835_debug_add(struct bcm2835_i2c_dev *i2c_dev, u32 s)
  61. +{
  62. + if (!i2c_dev->debug_num_msgs || i2c_dev->debug_num >= BCM2835_DEBUG_MAX)
  63. + return;
  64. +
  65. + i2c_dev->debug[i2c_dev->debug_num].msg = i2c_dev->curr_msg;
  66. + i2c_dev->debug[i2c_dev->debug_num].msg_idx =
  67. + i2c_dev->debug_num_msgs - i2c_dev->num_msgs;
  68. + i2c_dev->debug[i2c_dev->debug_num].remain = i2c_dev->msg_buf_remaining;
  69. + i2c_dev->debug[i2c_dev->debug_num].status = s;
  70. + i2c_dev->debug_num++;
  71. +}
  72. +
  73. +static void bcm2835_debug_print_status(struct bcm2835_i2c_dev *i2c_dev,
  74. + struct bcm2835_debug *d)
  75. +{
  76. + u32 s = d->status;
  77. +
  78. + pr_info("isr: remain=%zu, status=0x%x : %s%s%s%s%s%s%s%s%s%s [i2c%d]\n",
  79. + d->remain, s,
  80. + s & BCM2835_I2C_S_TA ? "TA " : "",
  81. + s & BCM2835_I2C_S_DONE ? "DONE " : "",
  82. + s & BCM2835_I2C_S_TXW ? "TXW " : "",
  83. + s & BCM2835_I2C_S_RXR ? "RXR " : "",
  84. + s & BCM2835_I2C_S_TXD ? "TXD " : "",
  85. + s & BCM2835_I2C_S_RXD ? "RXD " : "",
  86. + s & BCM2835_I2C_S_TXE ? "TXE " : "",
  87. + s & BCM2835_I2C_S_RXF ? "RXF " : "",
  88. + s & BCM2835_I2C_S_ERR ? "ERR " : "",
  89. + s & BCM2835_I2C_S_CLKT ? "CLKT " : "",
  90. + i2c_dev->adapter.nr);
  91. +}
  92. +
  93. +static void bcm2835_debug_print_msg(struct bcm2835_i2c_dev *i2c_dev,
  94. + struct i2c_msg *msg, int i, int total,
  95. + const char *fname)
  96. +{
  97. + pr_info("%s: msg(%d/%d) %s addr=0x%02x, len=%u flags=%s%s%s%s%s%s%s [i2c%d]\n",
  98. + fname, i, total,
  99. + msg->flags & I2C_M_RD ? "read" : "write", msg->addr, msg->len,
  100. + msg->flags & I2C_M_TEN ? "TEN" : "",
  101. + msg->flags & I2C_M_RECV_LEN ? "RECV_LEN" : "",
  102. + msg->flags & I2C_M_NO_RD_ACK ? "NO_RD_ACK" : "",
  103. + msg->flags & I2C_M_IGNORE_NAK ? "IGNORE_NAK" : "",
  104. + msg->flags & I2C_M_REV_DIR_ADDR ? "REV_DIR_ADDR" : "",
  105. + msg->flags & I2C_M_NOSTART ? "NOSTART" : "",
  106. + msg->flags & I2C_M_STOP ? "STOP" : "",
  107. + i2c_dev->adapter.nr);
  108. +}
  109. +
  110. +static void bcm2835_debug_print(struct bcm2835_i2c_dev *i2c_dev)
  111. +{
  112. + struct bcm2835_debug *d;
  113. + unsigned int i;
  114. +
  115. + for (i = 0; i < i2c_dev->debug_num; i++) {
  116. + d = &i2c_dev->debug[i];
  117. + if (d->status == ~0)
  118. + bcm2835_debug_print_msg(i2c_dev, d->msg, d->msg_idx,
  119. + i2c_dev->debug_num_msgs, "start_transfer");
  120. + else
  121. + bcm2835_debug_print_status(i2c_dev, d);
  122. + }
  123. + if (i2c_dev->debug_num >= BCM2835_DEBUG_MAX)
  124. + pr_info("BCM2835_DEBUG_MAX reached\n");
  125. +}
  126. +
  127. static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
  128. u32 reg, u32 val)
  129. {
  130. @@ -189,6 +271,7 @@ static void bcm2835_i2c_start_transfer(s
  131. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
  132. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
  133. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
  134. + bcm2835_debug_add(i2c_dev, ~0);
  135. }
  136. static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev)
  137. @@ -215,6 +298,7 @@ static irqreturn_t bcm2835_i2c_isr(int t
  138. u32 val, err;
  139. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  140. + bcm2835_debug_add(i2c_dev, val);
  141. err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
  142. if (err) {
  143. @@ -281,6 +365,13 @@ static int bcm2835_i2c_xfer(struct i2c_a
  144. unsigned long time_left;
  145. int i, ret;
  146. + if (debug)
  147. + i2c_dev->debug_num_msgs = num;
  148. +
  149. + if (debug > 2)
  150. + for (i = 0; i < num; i++)
  151. + bcm2835_debug_print_msg(i2c_dev, &msgs[i], i + 1, num, __func__);
  152. +
  153. for (i = 0; i < (num - 1); i++)
  154. if (msgs[i].flags & I2C_M_RD) {
  155. dev_warn_once(i2c_dev->dev,
  156. @@ -301,6 +392,11 @@ static int bcm2835_i2c_xfer(struct i2c_a
  157. time_left = wait_for_completion_timeout(&i2c_dev->completion,
  158. adap->timeout);
  159. + if (debug > 1 || (debug && (!time_left || i2c_dev->msg_err)))
  160. + bcm2835_debug_print(i2c_dev);
  161. + i2c_dev->debug_num_msgs = 0;
  162. + i2c_dev->debug_num = 0;
  163. +
  164. bcm2835_i2c_finish_transfer(i2c_dev);
  165. if (!time_left) {
  166. @@ -313,7 +409,9 @@ static int bcm2835_i2c_xfer(struct i2c_a
  167. if (!i2c_dev->msg_err)
  168. return num;
  169. - dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
  170. + if (debug)
  171. + dev_err(i2c_dev->dev, "i2c transfer failed: %x\n",
  172. + i2c_dev->msg_err);
  173. if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
  174. return -EREMOTEIO;