cobalt-i2c.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * cobalt I2C functions
  3. *
  4. * Derived from cx18-i2c.c
  5. *
  6. * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
  7. * All rights reserved.
  8. *
  9. * This program is free software; you may redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "cobalt-driver.h"
  23. #include "cobalt-i2c.h"
  24. struct cobalt_i2c_regs {
  25. /* Clock prescaler register lo-byte */
  26. u8 prerlo;
  27. u8 dummy0[3];
  28. /* Clock prescaler register high-byte */
  29. u8 prerhi;
  30. u8 dummy1[3];
  31. /* Control register */
  32. u8 ctr;
  33. u8 dummy2[3];
  34. /* Transmit/Receive register */
  35. u8 txr_rxr;
  36. u8 dummy3[3];
  37. /* Command and Status register */
  38. u8 cr_sr;
  39. u8 dummy4[3];
  40. };
  41. /* CTR[7:0] - Control register */
  42. /* I2C Core enable bit */
  43. #define M00018_CTR_BITMAP_EN_MSK (1 << 7)
  44. /* I2C Core interrupt enable bit */
  45. #define M00018_CTR_BITMAP_IEN_MSK (1 << 6)
  46. /* CR[7:0] - Command register */
  47. /* I2C start condition */
  48. #define M00018_CR_BITMAP_STA_MSK (1 << 7)
  49. /* I2C stop condition */
  50. #define M00018_CR_BITMAP_STO_MSK (1 << 6)
  51. /* I2C read from slave */
  52. #define M00018_CR_BITMAP_RD_MSK (1 << 5)
  53. /* I2C write to slave */
  54. #define M00018_CR_BITMAP_WR_MSK (1 << 4)
  55. /* I2C ack */
  56. #define M00018_CR_BITMAP_ACK_MSK (1 << 3)
  57. /* I2C Interrupt ack */
  58. #define M00018_CR_BITMAP_IACK_MSK (1 << 0)
  59. /* SR[7:0] - Status register */
  60. /* Receive acknowledge from slave */
  61. #define M00018_SR_BITMAP_RXACK_MSK (1 << 7)
  62. /* Busy, I2C bus busy (as defined by start / stop bits) */
  63. #define M00018_SR_BITMAP_BUSY_MSK (1 << 6)
  64. /* Arbitration lost - core lost arbitration */
  65. #define M00018_SR_BITMAP_AL_MSK (1 << 5)
  66. /* Transfer in progress */
  67. #define M00018_SR_BITMAP_TIP_MSK (1 << 1)
  68. /* Interrupt flag */
  69. #define M00018_SR_BITMAP_IF_MSK (1 << 0)
  70. /* Frequency, in Hz */
  71. #define I2C_FREQUENCY 400000
  72. #define ALT_CPU_FREQ 83333333
  73. static struct cobalt_i2c_regs __iomem *
  74. cobalt_i2c_regs(struct cobalt *cobalt, unsigned idx)
  75. {
  76. switch (idx) {
  77. case 0:
  78. default:
  79. return (struct cobalt_i2c_regs __iomem *)
  80. (cobalt->bar1 + COBALT_I2C_0_BASE);
  81. case 1:
  82. return (struct cobalt_i2c_regs __iomem *)
  83. (cobalt->bar1 + COBALT_I2C_1_BASE);
  84. case 2:
  85. return (struct cobalt_i2c_regs __iomem *)
  86. (cobalt->bar1 + COBALT_I2C_2_BASE);
  87. case 3:
  88. return (struct cobalt_i2c_regs __iomem *)
  89. (cobalt->bar1 + COBALT_I2C_3_BASE);
  90. case 4:
  91. return (struct cobalt_i2c_regs __iomem *)
  92. (cobalt->bar1 + COBALT_I2C_HSMA_BASE);
  93. }
  94. }
  95. /* Do low-level i2c byte transfer.
  96. * Returns -1 in case of an error or 0 otherwise.
  97. */
  98. static int cobalt_tx_bytes(struct cobalt_i2c_regs __iomem *regs,
  99. struct i2c_adapter *adap, bool start, bool stop,
  100. u8 *data, u16 len)
  101. {
  102. unsigned long start_time;
  103. int status;
  104. int cmd;
  105. int i;
  106. for (i = 0; i < len; i++) {
  107. /* Setup data */
  108. iowrite8(data[i], &regs->txr_rxr);
  109. /* Setup command */
  110. if (i == 0 && start != 0) {
  111. /* Write + Start */
  112. cmd = M00018_CR_BITMAP_WR_MSK |
  113. M00018_CR_BITMAP_STA_MSK;
  114. } else if (i == len - 1 && stop != 0) {
  115. /* Write + Stop */
  116. cmd = M00018_CR_BITMAP_WR_MSK |
  117. M00018_CR_BITMAP_STO_MSK;
  118. } else {
  119. /* Write only */
  120. cmd = M00018_CR_BITMAP_WR_MSK;
  121. }
  122. /* Execute command */
  123. iowrite8(cmd, &regs->cr_sr);
  124. /* Wait for transfer to complete (TIP = 0) */
  125. start_time = jiffies;
  126. status = ioread8(&regs->cr_sr);
  127. while (status & M00018_SR_BITMAP_TIP_MSK) {
  128. if (time_after(jiffies, start_time + adap->timeout))
  129. return -ETIMEDOUT;
  130. cond_resched();
  131. status = ioread8(&regs->cr_sr);
  132. }
  133. /* Verify ACK */
  134. if (status & M00018_SR_BITMAP_RXACK_MSK) {
  135. /* NO ACK! */
  136. return -EIO;
  137. }
  138. /* Verify arbitration */
  139. if (status & M00018_SR_BITMAP_AL_MSK) {
  140. /* Arbitration lost! */
  141. return -EIO;
  142. }
  143. }
  144. return 0;
  145. }
  146. /* Do low-level i2c byte read.
  147. * Returns -1 in case of an error or 0 otherwise.
  148. */
  149. static int cobalt_rx_bytes(struct cobalt_i2c_regs __iomem *regs,
  150. struct i2c_adapter *adap, bool start, bool stop,
  151. u8 *data, u16 len)
  152. {
  153. unsigned long start_time;
  154. int status;
  155. int cmd;
  156. int i;
  157. for (i = 0; i < len; i++) {
  158. /* Setup command */
  159. if (i == 0 && start != 0) {
  160. /* Read + Start */
  161. cmd = M00018_CR_BITMAP_RD_MSK |
  162. M00018_CR_BITMAP_STA_MSK;
  163. } else if (i == len - 1 && stop != 0) {
  164. /* Read + Stop */
  165. cmd = M00018_CR_BITMAP_RD_MSK |
  166. M00018_CR_BITMAP_STO_MSK;
  167. } else {
  168. /* Read only */
  169. cmd = M00018_CR_BITMAP_RD_MSK;
  170. }
  171. /* Last byte to read, no ACK */
  172. if (i == len - 1)
  173. cmd |= M00018_CR_BITMAP_ACK_MSK;
  174. /* Execute command */
  175. iowrite8(cmd, &regs->cr_sr);
  176. /* Wait for transfer to complete (TIP = 0) */
  177. start_time = jiffies;
  178. status = ioread8(&regs->cr_sr);
  179. while (status & M00018_SR_BITMAP_TIP_MSK) {
  180. if (time_after(jiffies, start_time + adap->timeout))
  181. return -ETIMEDOUT;
  182. cond_resched();
  183. status = ioread8(&regs->cr_sr);
  184. }
  185. /* Verify arbitration */
  186. if (status & M00018_SR_BITMAP_AL_MSK) {
  187. /* Arbitration lost! */
  188. return -EIO;
  189. }
  190. /* Store data */
  191. data[i] = ioread8(&regs->txr_rxr);
  192. }
  193. return 0;
  194. }
  195. /* Generate stop condition on i2c bus.
  196. * The m00018 stop isn't doing the right thing (wrong timing).
  197. * So instead send a start condition, 8 zeroes and a stop condition.
  198. */
  199. static int cobalt_stop(struct cobalt_i2c_regs __iomem *regs,
  200. struct i2c_adapter *adap)
  201. {
  202. u8 data = 0;
  203. return cobalt_tx_bytes(regs, adap, true, true, &data, 1);
  204. }
  205. static int cobalt_xfer(struct i2c_adapter *adap,
  206. struct i2c_msg msgs[], int num)
  207. {
  208. struct cobalt_i2c_data *data = adap->algo_data;
  209. struct cobalt_i2c_regs __iomem *regs = data->regs;
  210. struct i2c_msg *pmsg;
  211. unsigned short flags;
  212. int ret = 0;
  213. int i, j;
  214. for (i = 0; i < num; i++) {
  215. int stop = (i == num - 1);
  216. pmsg = &msgs[i];
  217. flags = pmsg->flags;
  218. if (!(pmsg->flags & I2C_M_NOSTART)) {
  219. u8 addr = pmsg->addr << 1;
  220. if (flags & I2C_M_RD)
  221. addr |= 1;
  222. if (flags & I2C_M_REV_DIR_ADDR)
  223. addr ^= 1;
  224. for (j = 0; j < adap->retries; j++) {
  225. ret = cobalt_tx_bytes(regs, adap, true, false,
  226. &addr, 1);
  227. if (!ret)
  228. break;
  229. cobalt_stop(regs, adap);
  230. }
  231. if (ret < 0)
  232. return ret;
  233. ret = 0;
  234. }
  235. if (pmsg->flags & I2C_M_RD) {
  236. /* read bytes into buffer */
  237. ret = cobalt_rx_bytes(regs, adap, false, stop,
  238. pmsg->buf, pmsg->len);
  239. if (ret < 0)
  240. goto bailout;
  241. } else {
  242. /* write bytes from buffer */
  243. ret = cobalt_tx_bytes(regs, adap, false, stop,
  244. pmsg->buf, pmsg->len);
  245. if (ret < 0)
  246. goto bailout;
  247. }
  248. }
  249. ret = i;
  250. bailout:
  251. if (ret < 0)
  252. cobalt_stop(regs, adap);
  253. return ret;
  254. }
  255. static u32 cobalt_func(struct i2c_adapter *adap)
  256. {
  257. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  258. }
  259. /* template for i2c-bit-algo */
  260. static struct i2c_adapter cobalt_i2c_adap_template = {
  261. .name = "cobalt i2c driver",
  262. .algo = NULL, /* set by i2c-algo-bit */
  263. .algo_data = NULL, /* filled from template */
  264. .owner = THIS_MODULE,
  265. };
  266. static const struct i2c_algorithm cobalt_algo = {
  267. .master_xfer = cobalt_xfer,
  268. .functionality = cobalt_func,
  269. };
  270. /* init + register i2c algo-bit adapter */
  271. int cobalt_i2c_init(struct cobalt *cobalt)
  272. {
  273. int i, err;
  274. int status;
  275. int prescale;
  276. unsigned long start_time;
  277. cobalt_dbg(1, "i2c init\n");
  278. /* Define I2C clock prescaler */
  279. prescale = ((ALT_CPU_FREQ) / (5 * I2C_FREQUENCY)) - 1;
  280. for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
  281. struct cobalt_i2c_regs __iomem *regs =
  282. cobalt_i2c_regs(cobalt, i);
  283. struct i2c_adapter *adap = &cobalt->i2c_adap[i];
  284. /* Disable I2C */
  285. iowrite8(M00018_CTR_BITMAP_EN_MSK, &regs->cr_sr);
  286. iowrite8(0, &regs->ctr);
  287. iowrite8(0, &regs->cr_sr);
  288. start_time = jiffies;
  289. do {
  290. if (time_after(jiffies, start_time + HZ)) {
  291. if (cobalt_ignore_err) {
  292. adap->dev.parent = NULL;
  293. return 0;
  294. }
  295. return -ETIMEDOUT;
  296. }
  297. status = ioread8(&regs->cr_sr);
  298. } while (status & M00018_SR_BITMAP_TIP_MSK);
  299. /* Disable I2C */
  300. iowrite8(0, &regs->ctr);
  301. iowrite8(0, &regs->cr_sr);
  302. /* Calculate i2c prescaler */
  303. iowrite8(prescale & 0xff, &regs->prerlo);
  304. iowrite8((prescale >> 8) & 0xff, &regs->prerhi);
  305. /* Enable I2C, interrupts disabled */
  306. iowrite8(M00018_CTR_BITMAP_EN_MSK, &regs->ctr);
  307. /* Setup algorithm for adapter */
  308. cobalt->i2c_data[i].cobalt = cobalt;
  309. cobalt->i2c_data[i].regs = regs;
  310. *adap = cobalt_i2c_adap_template;
  311. adap->algo = &cobalt_algo;
  312. adap->algo_data = &cobalt->i2c_data[i];
  313. adap->retries = 3;
  314. sprintf(adap->name + strlen(adap->name),
  315. " #%d-%d", cobalt->instance, i);
  316. i2c_set_adapdata(adap, &cobalt->v4l2_dev);
  317. adap->dev.parent = &cobalt->pci_dev->dev;
  318. err = i2c_add_adapter(adap);
  319. if (err) {
  320. if (cobalt_ignore_err) {
  321. adap->dev.parent = NULL;
  322. return 0;
  323. }
  324. while (i--)
  325. i2c_del_adapter(&cobalt->i2c_adap[i]);
  326. return err;
  327. }
  328. cobalt_info("registered bus %s\n", adap->name);
  329. }
  330. return 0;
  331. }
  332. void cobalt_i2c_exit(struct cobalt *cobalt)
  333. {
  334. int i;
  335. cobalt_dbg(1, "i2c exit\n");
  336. for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
  337. cobalt_err("unregistered bus %s\n", cobalt->i2c_adap[i].name);
  338. i2c_del_adapter(&cobalt->i2c_adap[i]);
  339. }
  340. }