i2c-bcm-kona.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Copyright (C) 2013 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <linux/slab.h>
  23. /* Hardware register offsets and field defintions */
  24. #define CS_OFFSET 0x00000020
  25. #define CS_ACK_SHIFT 3
  26. #define CS_ACK_MASK 0x00000008
  27. #define CS_ACK_CMD_GEN_START 0x00000000
  28. #define CS_ACK_CMD_GEN_RESTART 0x00000001
  29. #define CS_CMD_SHIFT 1
  30. #define CS_CMD_CMD_NO_ACTION 0x00000000
  31. #define CS_CMD_CMD_START_RESTART 0x00000001
  32. #define CS_CMD_CMD_STOP 0x00000002
  33. #define CS_EN_SHIFT 0
  34. #define CS_EN_CMD_ENABLE_BSC 0x00000001
  35. #define TIM_OFFSET 0x00000024
  36. #define TIM_PRESCALE_SHIFT 6
  37. #define TIM_P_SHIFT 3
  38. #define TIM_NO_DIV_SHIFT 2
  39. #define TIM_DIV_SHIFT 0
  40. #define DAT_OFFSET 0x00000028
  41. #define TOUT_OFFSET 0x0000002c
  42. #define TXFCR_OFFSET 0x0000003c
  43. #define TXFCR_FIFO_FLUSH_MASK 0x00000080
  44. #define TXFCR_FIFO_EN_MASK 0x00000040
  45. #define IER_OFFSET 0x00000044
  46. #define IER_READ_COMPLETE_INT_MASK 0x00000010
  47. #define IER_I2C_INT_EN_MASK 0x00000008
  48. #define IER_FIFO_INT_EN_MASK 0x00000002
  49. #define IER_NOACK_EN_MASK 0x00000001
  50. #define ISR_OFFSET 0x00000048
  51. #define ISR_RESERVED_MASK 0xffffff60
  52. #define ISR_CMDBUSY_MASK 0x00000080
  53. #define ISR_READ_COMPLETE_MASK 0x00000010
  54. #define ISR_SES_DONE_MASK 0x00000008
  55. #define ISR_ERR_MASK 0x00000004
  56. #define ISR_TXFIFOEMPTY_MASK 0x00000002
  57. #define ISR_NOACK_MASK 0x00000001
  58. #define CLKEN_OFFSET 0x0000004C
  59. #define CLKEN_AUTOSENSE_OFF_MASK 0x00000080
  60. #define CLKEN_M_SHIFT 4
  61. #define CLKEN_N_SHIFT 1
  62. #define CLKEN_CLKEN_MASK 0x00000001
  63. #define FIFO_STATUS_OFFSET 0x00000054
  64. #define FIFO_STATUS_RXFIFO_EMPTY_MASK 0x00000004
  65. #define FIFO_STATUS_TXFIFO_EMPTY_MASK 0x00000010
  66. #define HSTIM_OFFSET 0x00000058
  67. #define HSTIM_HS_MODE_MASK 0x00008000
  68. #define HSTIM_HS_HOLD_SHIFT 10
  69. #define HSTIM_HS_HIGH_PHASE_SHIFT 5
  70. #define HSTIM_HS_SETUP_SHIFT 0
  71. #define PADCTL_OFFSET 0x0000005c
  72. #define PADCTL_PAD_OUT_EN_MASK 0x00000004
  73. #define RXFCR_OFFSET 0x00000068
  74. #define RXFCR_NACK_EN_SHIFT 7
  75. #define RXFCR_READ_COUNT_SHIFT 0
  76. #define RXFIFORDOUT_OFFSET 0x0000006c
  77. /* Locally used constants */
  78. #define MAX_RX_FIFO_SIZE 64U /* bytes */
  79. #define MAX_TX_FIFO_SIZE 64U /* bytes */
  80. #define STD_EXT_CLK_FREQ 13000000UL
  81. #define HS_EXT_CLK_FREQ 104000000UL
  82. #define MASTERCODE 0x08 /* Mastercodes are 0000_1xxxb */
  83. #define I2C_TIMEOUT 100 /* msecs */
  84. /* Operations that can be commanded to the controller */
  85. enum bcm_kona_cmd_t {
  86. BCM_CMD_NOACTION = 0,
  87. BCM_CMD_START,
  88. BCM_CMD_RESTART,
  89. BCM_CMD_STOP,
  90. };
  91. enum bus_speed_index {
  92. BCM_SPD_100K = 0,
  93. BCM_SPD_400K,
  94. BCM_SPD_1MHZ,
  95. };
  96. enum hs_bus_speed_index {
  97. BCM_SPD_3P4MHZ = 0,
  98. };
  99. /* Internal divider settings for standard mode, fast mode and fast mode plus */
  100. struct bus_speed_cfg {
  101. uint8_t time_m; /* Number of cycles for setup time */
  102. uint8_t time_n; /* Number of cycles for hold time */
  103. uint8_t prescale; /* Prescale divider */
  104. uint8_t time_p; /* Timing coefficient */
  105. uint8_t no_div; /* Disable clock divider */
  106. uint8_t time_div; /* Post-prescale divider */
  107. };
  108. /* Internal divider settings for high-speed mode */
  109. struct hs_bus_speed_cfg {
  110. uint8_t hs_hold; /* Number of clock cycles SCL stays low until
  111. the end of bit period */
  112. uint8_t hs_high_phase; /* Number of clock cycles SCL stays high
  113. before it falls */
  114. uint8_t hs_setup; /* Number of clock cycles SCL stays low
  115. before it rises */
  116. uint8_t prescale; /* Prescale divider */
  117. uint8_t time_p; /* Timing coefficient */
  118. uint8_t no_div; /* Disable clock divider */
  119. uint8_t time_div; /* Post-prescale divider */
  120. };
  121. static const struct bus_speed_cfg std_cfg_table[] = {
  122. [BCM_SPD_100K] = {0x01, 0x01, 0x03, 0x06, 0x00, 0x02},
  123. [BCM_SPD_400K] = {0x05, 0x01, 0x03, 0x05, 0x01, 0x02},
  124. [BCM_SPD_1MHZ] = {0x01, 0x01, 0x03, 0x01, 0x01, 0x03},
  125. };
  126. static const struct hs_bus_speed_cfg hs_cfg_table[] = {
  127. [BCM_SPD_3P4MHZ] = {0x01, 0x08, 0x14, 0x00, 0x06, 0x01, 0x00},
  128. };
  129. struct bcm_kona_i2c_dev {
  130. struct device *device;
  131. void __iomem *base;
  132. int irq;
  133. struct clk *external_clk;
  134. struct i2c_adapter adapter;
  135. struct completion done;
  136. const struct bus_speed_cfg *std_cfg;
  137. const struct hs_bus_speed_cfg *hs_cfg;
  138. };
  139. static void bcm_kona_i2c_send_cmd_to_ctrl(struct bcm_kona_i2c_dev *dev,
  140. enum bcm_kona_cmd_t cmd)
  141. {
  142. dev_dbg(dev->device, "%s, %d\n", __func__, cmd);
  143. switch (cmd) {
  144. case BCM_CMD_NOACTION:
  145. writel((CS_CMD_CMD_NO_ACTION << CS_CMD_SHIFT) |
  146. (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
  147. dev->base + CS_OFFSET);
  148. break;
  149. case BCM_CMD_START:
  150. writel((CS_ACK_CMD_GEN_START << CS_ACK_SHIFT) |
  151. (CS_CMD_CMD_START_RESTART << CS_CMD_SHIFT) |
  152. (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
  153. dev->base + CS_OFFSET);
  154. break;
  155. case BCM_CMD_RESTART:
  156. writel((CS_ACK_CMD_GEN_RESTART << CS_ACK_SHIFT) |
  157. (CS_CMD_CMD_START_RESTART << CS_CMD_SHIFT) |
  158. (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
  159. dev->base + CS_OFFSET);
  160. break;
  161. case BCM_CMD_STOP:
  162. writel((CS_CMD_CMD_STOP << CS_CMD_SHIFT) |
  163. (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
  164. dev->base + CS_OFFSET);
  165. break;
  166. default:
  167. dev_err(dev->device, "Unknown command %d\n", cmd);
  168. }
  169. }
  170. static void bcm_kona_i2c_enable_clock(struct bcm_kona_i2c_dev *dev)
  171. {
  172. writel(readl(dev->base + CLKEN_OFFSET) | CLKEN_CLKEN_MASK,
  173. dev->base + CLKEN_OFFSET);
  174. }
  175. static void bcm_kona_i2c_disable_clock(struct bcm_kona_i2c_dev *dev)
  176. {
  177. writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_CLKEN_MASK,
  178. dev->base + CLKEN_OFFSET);
  179. }
  180. static irqreturn_t bcm_kona_i2c_isr(int irq, void *devid)
  181. {
  182. struct bcm_kona_i2c_dev *dev = devid;
  183. uint32_t status = readl(dev->base + ISR_OFFSET);
  184. if ((status & ~ISR_RESERVED_MASK) == 0)
  185. return IRQ_NONE;
  186. /* Must flush the TX FIFO when NAK detected */
  187. if (status & ISR_NOACK_MASK)
  188. writel(TXFCR_FIFO_FLUSH_MASK | TXFCR_FIFO_EN_MASK,
  189. dev->base + TXFCR_OFFSET);
  190. writel(status & ~ISR_RESERVED_MASK, dev->base + ISR_OFFSET);
  191. complete_all(&dev->done);
  192. return IRQ_HANDLED;
  193. }
  194. /* Wait for ISR_CMDBUSY_MASK to go low before writing to CS, DAT, or RCD */
  195. static int bcm_kona_i2c_wait_if_busy(struct bcm_kona_i2c_dev *dev)
  196. {
  197. unsigned long timeout = jiffies + msecs_to_jiffies(I2C_TIMEOUT);
  198. while (readl(dev->base + ISR_OFFSET) & ISR_CMDBUSY_MASK)
  199. if (time_after(jiffies, timeout)) {
  200. dev_err(dev->device, "CMDBUSY timeout\n");
  201. return -ETIMEDOUT;
  202. }
  203. return 0;
  204. }
  205. /* Send command to I2C bus */
  206. static int bcm_kona_send_i2c_cmd(struct bcm_kona_i2c_dev *dev,
  207. enum bcm_kona_cmd_t cmd)
  208. {
  209. int rc;
  210. unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
  211. /* Make sure the hardware is ready */
  212. rc = bcm_kona_i2c_wait_if_busy(dev);
  213. if (rc < 0)
  214. return rc;
  215. /* Unmask the session done interrupt */
  216. writel(IER_I2C_INT_EN_MASK, dev->base + IER_OFFSET);
  217. /* Mark as incomplete before sending the command */
  218. reinit_completion(&dev->done);
  219. /* Send the command */
  220. bcm_kona_i2c_send_cmd_to_ctrl(dev, cmd);
  221. /* Wait for transaction to finish or timeout */
  222. time_left = wait_for_completion_timeout(&dev->done, time_left);
  223. /* Mask all interrupts */
  224. writel(0, dev->base + IER_OFFSET);
  225. if (!time_left) {
  226. dev_err(dev->device, "controller timed out\n");
  227. rc = -ETIMEDOUT;
  228. }
  229. /* Clear command */
  230. bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION);
  231. return rc;
  232. }
  233. /* Read a single RX FIFO worth of data from the i2c bus */
  234. static int bcm_kona_i2c_read_fifo_single(struct bcm_kona_i2c_dev *dev,
  235. uint8_t *buf, unsigned int len,
  236. unsigned int last_byte_nak)
  237. {
  238. unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
  239. /* Mark as incomplete before starting the RX FIFO */
  240. reinit_completion(&dev->done);
  241. /* Unmask the read complete interrupt */
  242. writel(IER_READ_COMPLETE_INT_MASK, dev->base + IER_OFFSET);
  243. /* Start the RX FIFO */
  244. writel((last_byte_nak << RXFCR_NACK_EN_SHIFT) |
  245. (len << RXFCR_READ_COUNT_SHIFT),
  246. dev->base + RXFCR_OFFSET);
  247. /* Wait for FIFO read to complete */
  248. time_left = wait_for_completion_timeout(&dev->done, time_left);
  249. /* Mask all interrupts */
  250. writel(0, dev->base + IER_OFFSET);
  251. if (!time_left) {
  252. dev_err(dev->device, "RX FIFO time out\n");
  253. return -EREMOTEIO;
  254. }
  255. /* Read data from FIFO */
  256. for (; len > 0; len--, buf++)
  257. *buf = readl(dev->base + RXFIFORDOUT_OFFSET);
  258. return 0;
  259. }
  260. /* Read any amount of data using the RX FIFO from the i2c bus */
  261. static int bcm_kona_i2c_read_fifo(struct bcm_kona_i2c_dev *dev,
  262. struct i2c_msg *msg)
  263. {
  264. unsigned int bytes_to_read = MAX_RX_FIFO_SIZE;
  265. unsigned int last_byte_nak = 0;
  266. unsigned int bytes_read = 0;
  267. int rc;
  268. uint8_t *tmp_buf = msg->buf;
  269. while (bytes_read < msg->len) {
  270. if (msg->len - bytes_read <= MAX_RX_FIFO_SIZE) {
  271. last_byte_nak = 1; /* NAK last byte of transfer */
  272. bytes_to_read = msg->len - bytes_read;
  273. }
  274. rc = bcm_kona_i2c_read_fifo_single(dev, tmp_buf, bytes_to_read,
  275. last_byte_nak);
  276. if (rc < 0)
  277. return -EREMOTEIO;
  278. bytes_read += bytes_to_read;
  279. tmp_buf += bytes_to_read;
  280. }
  281. return 0;
  282. }
  283. /* Write a single byte of data to the i2c bus */
  284. static int bcm_kona_i2c_write_byte(struct bcm_kona_i2c_dev *dev, uint8_t data,
  285. unsigned int nak_expected)
  286. {
  287. int rc;
  288. unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
  289. unsigned int nak_received;
  290. /* Make sure the hardware is ready */
  291. rc = bcm_kona_i2c_wait_if_busy(dev);
  292. if (rc < 0)
  293. return rc;
  294. /* Clear pending session done interrupt */
  295. writel(ISR_SES_DONE_MASK, dev->base + ISR_OFFSET);
  296. /* Unmask the session done interrupt */
  297. writel(IER_I2C_INT_EN_MASK, dev->base + IER_OFFSET);
  298. /* Mark as incomplete before sending the data */
  299. reinit_completion(&dev->done);
  300. /* Send one byte of data */
  301. writel(data, dev->base + DAT_OFFSET);
  302. /* Wait for byte to be written */
  303. time_left = wait_for_completion_timeout(&dev->done, time_left);
  304. /* Mask all interrupts */
  305. writel(0, dev->base + IER_OFFSET);
  306. if (!time_left) {
  307. dev_dbg(dev->device, "controller timed out\n");
  308. return -ETIMEDOUT;
  309. }
  310. nak_received = readl(dev->base + CS_OFFSET) & CS_ACK_MASK ? 1 : 0;
  311. if (nak_received ^ nak_expected) {
  312. dev_dbg(dev->device, "unexpected NAK/ACK\n");
  313. return -EREMOTEIO;
  314. }
  315. return 0;
  316. }
  317. /* Write a single TX FIFO worth of data to the i2c bus */
  318. static int bcm_kona_i2c_write_fifo_single(struct bcm_kona_i2c_dev *dev,
  319. uint8_t *buf, unsigned int len)
  320. {
  321. int k;
  322. unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
  323. unsigned int fifo_status;
  324. /* Mark as incomplete before sending data to the TX FIFO */
  325. reinit_completion(&dev->done);
  326. /* Unmask the fifo empty and nak interrupt */
  327. writel(IER_FIFO_INT_EN_MASK | IER_NOACK_EN_MASK,
  328. dev->base + IER_OFFSET);
  329. /* Disable IRQ to load a FIFO worth of data without interruption */
  330. disable_irq(dev->irq);
  331. /* Write data into FIFO */
  332. for (k = 0; k < len; k++)
  333. writel(buf[k], (dev->base + DAT_OFFSET));
  334. /* Enable IRQ now that data has been loaded */
  335. enable_irq(dev->irq);
  336. /* Wait for FIFO to empty */
  337. do {
  338. time_left = wait_for_completion_timeout(&dev->done, time_left);
  339. fifo_status = readl(dev->base + FIFO_STATUS_OFFSET);
  340. } while (time_left && !(fifo_status & FIFO_STATUS_TXFIFO_EMPTY_MASK));
  341. /* Mask all interrupts */
  342. writel(0, dev->base + IER_OFFSET);
  343. /* Check if there was a NAK */
  344. if (readl(dev->base + CS_OFFSET) & CS_ACK_MASK) {
  345. dev_err(dev->device, "unexpected NAK\n");
  346. return -EREMOTEIO;
  347. }
  348. /* Check if a timeout occured */
  349. if (!time_left) {
  350. dev_err(dev->device, "completion timed out\n");
  351. return -EREMOTEIO;
  352. }
  353. return 0;
  354. }
  355. /* Write any amount of data using TX FIFO to the i2c bus */
  356. static int bcm_kona_i2c_write_fifo(struct bcm_kona_i2c_dev *dev,
  357. struct i2c_msg *msg)
  358. {
  359. unsigned int bytes_to_write = MAX_TX_FIFO_SIZE;
  360. unsigned int bytes_written = 0;
  361. int rc;
  362. uint8_t *tmp_buf = msg->buf;
  363. while (bytes_written < msg->len) {
  364. if (msg->len - bytes_written <= MAX_TX_FIFO_SIZE)
  365. bytes_to_write = msg->len - bytes_written;
  366. rc = bcm_kona_i2c_write_fifo_single(dev, tmp_buf,
  367. bytes_to_write);
  368. if (rc < 0)
  369. return -EREMOTEIO;
  370. bytes_written += bytes_to_write;
  371. tmp_buf += bytes_to_write;
  372. }
  373. return 0;
  374. }
  375. /* Send i2c address */
  376. static int bcm_kona_i2c_do_addr(struct bcm_kona_i2c_dev *dev,
  377. struct i2c_msg *msg)
  378. {
  379. unsigned char addr;
  380. if (msg->flags & I2C_M_TEN) {
  381. /* First byte is 11110XX0 where XX is upper 2 bits */
  382. addr = 0xF0 | ((msg->addr & 0x300) >> 7);
  383. if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
  384. return -EREMOTEIO;
  385. /* Second byte is the remaining 8 bits */
  386. addr = msg->addr & 0xFF;
  387. if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
  388. return -EREMOTEIO;
  389. if (msg->flags & I2C_M_RD) {
  390. /* For read, send restart command */
  391. if (bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART) < 0)
  392. return -EREMOTEIO;
  393. /* Then re-send the first byte with the read bit set */
  394. addr = 0xF0 | ((msg->addr & 0x300) >> 7) | 0x01;
  395. if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
  396. return -EREMOTEIO;
  397. }
  398. } else {
  399. addr = msg->addr << 1;
  400. if (msg->flags & I2C_M_RD)
  401. addr |= 1;
  402. if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
  403. return -EREMOTEIO;
  404. }
  405. return 0;
  406. }
  407. static void bcm_kona_i2c_enable_autosense(struct bcm_kona_i2c_dev *dev)
  408. {
  409. writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_AUTOSENSE_OFF_MASK,
  410. dev->base + CLKEN_OFFSET);
  411. }
  412. static void bcm_kona_i2c_config_timing(struct bcm_kona_i2c_dev *dev)
  413. {
  414. writel(readl(dev->base + HSTIM_OFFSET) & ~HSTIM_HS_MODE_MASK,
  415. dev->base + HSTIM_OFFSET);
  416. writel((dev->std_cfg->prescale << TIM_PRESCALE_SHIFT) |
  417. (dev->std_cfg->time_p << TIM_P_SHIFT) |
  418. (dev->std_cfg->no_div << TIM_NO_DIV_SHIFT) |
  419. (dev->std_cfg->time_div << TIM_DIV_SHIFT),
  420. dev->base + TIM_OFFSET);
  421. writel((dev->std_cfg->time_m << CLKEN_M_SHIFT) |
  422. (dev->std_cfg->time_n << CLKEN_N_SHIFT) |
  423. CLKEN_CLKEN_MASK,
  424. dev->base + CLKEN_OFFSET);
  425. }
  426. static void bcm_kona_i2c_config_timing_hs(struct bcm_kona_i2c_dev *dev)
  427. {
  428. writel((dev->hs_cfg->prescale << TIM_PRESCALE_SHIFT) |
  429. (dev->hs_cfg->time_p << TIM_P_SHIFT) |
  430. (dev->hs_cfg->no_div << TIM_NO_DIV_SHIFT) |
  431. (dev->hs_cfg->time_div << TIM_DIV_SHIFT),
  432. dev->base + TIM_OFFSET);
  433. writel((dev->hs_cfg->hs_hold << HSTIM_HS_HOLD_SHIFT) |
  434. (dev->hs_cfg->hs_high_phase << HSTIM_HS_HIGH_PHASE_SHIFT) |
  435. (dev->hs_cfg->hs_setup << HSTIM_HS_SETUP_SHIFT),
  436. dev->base + HSTIM_OFFSET);
  437. writel(readl(dev->base + HSTIM_OFFSET) | HSTIM_HS_MODE_MASK,
  438. dev->base + HSTIM_OFFSET);
  439. }
  440. static int bcm_kona_i2c_switch_to_hs(struct bcm_kona_i2c_dev *dev)
  441. {
  442. int rc;
  443. /* Send mastercode at standard speed */
  444. rc = bcm_kona_i2c_write_byte(dev, MASTERCODE, 1);
  445. if (rc < 0) {
  446. pr_err("High speed handshake failed\n");
  447. return rc;
  448. }
  449. /* Configure external clock to higher frequency */
  450. rc = clk_set_rate(dev->external_clk, HS_EXT_CLK_FREQ);
  451. if (rc) {
  452. dev_err(dev->device, "%s: clk_set_rate returned %d\n",
  453. __func__, rc);
  454. return rc;
  455. }
  456. /* Reconfigure internal dividers */
  457. bcm_kona_i2c_config_timing_hs(dev);
  458. /* Send a restart command */
  459. rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART);
  460. if (rc < 0)
  461. dev_err(dev->device, "High speed restart command failed\n");
  462. return rc;
  463. }
  464. static int bcm_kona_i2c_switch_to_std(struct bcm_kona_i2c_dev *dev)
  465. {
  466. int rc;
  467. /* Reconfigure internal dividers */
  468. bcm_kona_i2c_config_timing(dev);
  469. /* Configure external clock to lower frequency */
  470. rc = clk_set_rate(dev->external_clk, STD_EXT_CLK_FREQ);
  471. if (rc) {
  472. dev_err(dev->device, "%s: clk_set_rate returned %d\n",
  473. __func__, rc);
  474. }
  475. return rc;
  476. }
  477. /* Master transfer function */
  478. static int bcm_kona_i2c_xfer(struct i2c_adapter *adapter,
  479. struct i2c_msg msgs[], int num)
  480. {
  481. struct bcm_kona_i2c_dev *dev = i2c_get_adapdata(adapter);
  482. struct i2c_msg *pmsg;
  483. int rc = 0;
  484. int i;
  485. rc = clk_prepare_enable(dev->external_clk);
  486. if (rc) {
  487. dev_err(dev->device, "%s: peri clock enable failed. err %d\n",
  488. __func__, rc);
  489. return rc;
  490. }
  491. /* Enable pad output */
  492. writel(0, dev->base + PADCTL_OFFSET);
  493. /* Enable internal clocks */
  494. bcm_kona_i2c_enable_clock(dev);
  495. /* Send start command */
  496. rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_START);
  497. if (rc < 0) {
  498. dev_err(dev->device, "Start command failed rc = %d\n", rc);
  499. goto xfer_disable_pad;
  500. }
  501. /* Switch to high speed if applicable */
  502. if (dev->hs_cfg) {
  503. rc = bcm_kona_i2c_switch_to_hs(dev);
  504. if (rc < 0)
  505. goto xfer_send_stop;
  506. }
  507. /* Loop through all messages */
  508. for (i = 0; i < num; i++) {
  509. pmsg = &msgs[i];
  510. /* Send restart for subsequent messages */
  511. if ((i != 0) && ((pmsg->flags & I2C_M_NOSTART) == 0)) {
  512. rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART);
  513. if (rc < 0) {
  514. dev_err(dev->device,
  515. "restart cmd failed rc = %d\n", rc);
  516. goto xfer_send_stop;
  517. }
  518. }
  519. /* Send slave address */
  520. if (!(pmsg->flags & I2C_M_NOSTART)) {
  521. rc = bcm_kona_i2c_do_addr(dev, pmsg);
  522. if (rc < 0) {
  523. dev_err(dev->device,
  524. "NAK from addr %2.2x msg#%d rc = %d\n",
  525. pmsg->addr, i, rc);
  526. goto xfer_send_stop;
  527. }
  528. }
  529. /* Perform data transfer */
  530. if (pmsg->flags & I2C_M_RD) {
  531. rc = bcm_kona_i2c_read_fifo(dev, pmsg);
  532. if (rc < 0) {
  533. dev_err(dev->device, "read failure\n");
  534. goto xfer_send_stop;
  535. }
  536. } else {
  537. rc = bcm_kona_i2c_write_fifo(dev, pmsg);
  538. if (rc < 0) {
  539. dev_err(dev->device, "write failure");
  540. goto xfer_send_stop;
  541. }
  542. }
  543. }
  544. rc = num;
  545. xfer_send_stop:
  546. /* Send a STOP command */
  547. bcm_kona_send_i2c_cmd(dev, BCM_CMD_STOP);
  548. /* Return from high speed if applicable */
  549. if (dev->hs_cfg) {
  550. int hs_rc = bcm_kona_i2c_switch_to_std(dev);
  551. if (hs_rc)
  552. rc = hs_rc;
  553. }
  554. xfer_disable_pad:
  555. /* Disable pad output */
  556. writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET);
  557. /* Stop internal clock */
  558. bcm_kona_i2c_disable_clock(dev);
  559. clk_disable_unprepare(dev->external_clk);
  560. return rc;
  561. }
  562. static uint32_t bcm_kona_i2c_functionality(struct i2c_adapter *adap)
  563. {
  564. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
  565. I2C_FUNC_NOSTART;
  566. }
  567. static const struct i2c_algorithm bcm_algo = {
  568. .master_xfer = bcm_kona_i2c_xfer,
  569. .functionality = bcm_kona_i2c_functionality,
  570. };
  571. static int bcm_kona_i2c_assign_bus_speed(struct bcm_kona_i2c_dev *dev)
  572. {
  573. unsigned int bus_speed;
  574. int ret = of_property_read_u32(dev->device->of_node, "clock-frequency",
  575. &bus_speed);
  576. if (ret < 0) {
  577. dev_err(dev->device, "missing clock-frequency property\n");
  578. return -ENODEV;
  579. }
  580. switch (bus_speed) {
  581. case 100000:
  582. dev->std_cfg = &std_cfg_table[BCM_SPD_100K];
  583. break;
  584. case 400000:
  585. dev->std_cfg = &std_cfg_table[BCM_SPD_400K];
  586. break;
  587. case 1000000:
  588. dev->std_cfg = &std_cfg_table[BCM_SPD_1MHZ];
  589. break;
  590. case 3400000:
  591. /* Send mastercode at 100k */
  592. dev->std_cfg = &std_cfg_table[BCM_SPD_100K];
  593. dev->hs_cfg = &hs_cfg_table[BCM_SPD_3P4MHZ];
  594. break;
  595. default:
  596. pr_err("%d hz bus speed not supported\n", bus_speed);
  597. pr_err("Valid speeds are 100khz, 400khz, 1mhz, and 3.4mhz\n");
  598. return -EINVAL;
  599. }
  600. return 0;
  601. }
  602. static int bcm_kona_i2c_probe(struct platform_device *pdev)
  603. {
  604. int rc = 0;
  605. struct bcm_kona_i2c_dev *dev;
  606. struct i2c_adapter *adap;
  607. struct resource *iomem;
  608. /* Allocate memory for private data structure */
  609. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  610. if (!dev)
  611. return -ENOMEM;
  612. platform_set_drvdata(pdev, dev);
  613. dev->device = &pdev->dev;
  614. init_completion(&dev->done);
  615. /* Map hardware registers */
  616. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  617. dev->base = devm_ioremap_resource(dev->device, iomem);
  618. if (IS_ERR(dev->base))
  619. return -ENOMEM;
  620. /* Get and enable external clock */
  621. dev->external_clk = devm_clk_get(dev->device, NULL);
  622. if (IS_ERR(dev->external_clk)) {
  623. dev_err(dev->device, "couldn't get clock\n");
  624. return -ENODEV;
  625. }
  626. rc = clk_set_rate(dev->external_clk, STD_EXT_CLK_FREQ);
  627. if (rc) {
  628. dev_err(dev->device, "%s: clk_set_rate returned %d\n",
  629. __func__, rc);
  630. return rc;
  631. }
  632. rc = clk_prepare_enable(dev->external_clk);
  633. if (rc) {
  634. dev_err(dev->device, "couldn't enable clock\n");
  635. return rc;
  636. }
  637. /* Parse bus speed */
  638. rc = bcm_kona_i2c_assign_bus_speed(dev);
  639. if (rc)
  640. goto probe_disable_clk;
  641. /* Enable internal clocks */
  642. bcm_kona_i2c_enable_clock(dev);
  643. /* Configure internal dividers */
  644. bcm_kona_i2c_config_timing(dev);
  645. /* Disable timeout */
  646. writel(0, dev->base + TOUT_OFFSET);
  647. /* Enable autosense */
  648. bcm_kona_i2c_enable_autosense(dev);
  649. /* Enable TX FIFO */
  650. writel(TXFCR_FIFO_FLUSH_MASK | TXFCR_FIFO_EN_MASK,
  651. dev->base + TXFCR_OFFSET);
  652. /* Mask all interrupts */
  653. writel(0, dev->base + IER_OFFSET);
  654. /* Clear all pending interrupts */
  655. writel(ISR_CMDBUSY_MASK |
  656. ISR_READ_COMPLETE_MASK |
  657. ISR_SES_DONE_MASK |
  658. ISR_ERR_MASK |
  659. ISR_TXFIFOEMPTY_MASK |
  660. ISR_NOACK_MASK,
  661. dev->base + ISR_OFFSET);
  662. /* Get the interrupt number */
  663. dev->irq = platform_get_irq(pdev, 0);
  664. if (dev->irq < 0) {
  665. dev_err(dev->device, "no irq resource\n");
  666. rc = -ENODEV;
  667. goto probe_disable_clk;
  668. }
  669. /* register the ISR handler */
  670. rc = devm_request_irq(&pdev->dev, dev->irq, bcm_kona_i2c_isr,
  671. IRQF_SHARED, pdev->name, dev);
  672. if (rc) {
  673. dev_err(dev->device, "failed to request irq %i\n", dev->irq);
  674. goto probe_disable_clk;
  675. }
  676. /* Enable the controller but leave it idle */
  677. bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION);
  678. /* Disable pad output */
  679. writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET);
  680. /* Disable internal clock */
  681. bcm_kona_i2c_disable_clock(dev);
  682. /* Disable external clock */
  683. clk_disable_unprepare(dev->external_clk);
  684. /* Add the i2c adapter */
  685. adap = &dev->adapter;
  686. i2c_set_adapdata(adap, dev);
  687. adap->owner = THIS_MODULE;
  688. strlcpy(adap->name, "Broadcom I2C adapter", sizeof(adap->name));
  689. adap->algo = &bcm_algo;
  690. adap->dev.parent = &pdev->dev;
  691. adap->dev.of_node = pdev->dev.of_node;
  692. rc = i2c_add_adapter(adap);
  693. if (rc) {
  694. dev_err(dev->device, "failed to add adapter\n");
  695. return rc;
  696. }
  697. dev_info(dev->device, "device registered successfully\n");
  698. return 0;
  699. probe_disable_clk:
  700. bcm_kona_i2c_disable_clock(dev);
  701. clk_disable_unprepare(dev->external_clk);
  702. return rc;
  703. }
  704. static int bcm_kona_i2c_remove(struct platform_device *pdev)
  705. {
  706. struct bcm_kona_i2c_dev *dev = platform_get_drvdata(pdev);
  707. i2c_del_adapter(&dev->adapter);
  708. return 0;
  709. }
  710. static const struct of_device_id bcm_kona_i2c_of_match[] = {
  711. {.compatible = "brcm,kona-i2c",},
  712. {},
  713. };
  714. MODULE_DEVICE_TABLE(of, bcm_kona_i2c_of_match);
  715. static struct platform_driver bcm_kona_i2c_driver = {
  716. .driver = {
  717. .name = "bcm-kona-i2c",
  718. .of_match_table = bcm_kona_i2c_of_match,
  719. },
  720. .probe = bcm_kona_i2c_probe,
  721. .remove = bcm_kona_i2c_remove,
  722. };
  723. module_platform_driver(bcm_kona_i2c_driver);
  724. MODULE_AUTHOR("Tim Kryger <tkryger@broadcom.com>");
  725. MODULE_DESCRIPTION("Broadcom Kona I2C Driver");
  726. MODULE_LICENSE("GPL v2");