i2c-ocores.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
  3. * (http://www.opencores.org/projects.cgi/web/i2c/overview).
  4. *
  5. * Peter Korsgaard <jacmet@sunsite.dk>
  6. *
  7. * Support for the GRLIB port of the controller by
  8. * Andreas Larsson <andreas@gaisler.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public License
  11. * version 2. This program is licensed "as is" without any warranty of any
  12. * kind, whether express or implied.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/i2c.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/wait.h>
  23. #include <linux/i2c-ocores.h>
  24. #include <linux/slab.h>
  25. #include <linux/io.h>
  26. #include <linux/log2.h>
  27. struct ocores_i2c {
  28. void __iomem *base;
  29. u32 reg_shift;
  30. u32 reg_io_width;
  31. wait_queue_head_t wait;
  32. struct i2c_adapter adap;
  33. struct i2c_msg *msg;
  34. int pos;
  35. int nmsgs;
  36. int state; /* see STATE_ */
  37. struct clk *clk;
  38. int ip_clock_khz;
  39. int bus_clock_khz;
  40. void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
  41. u8 (*getreg)(struct ocores_i2c *i2c, int reg);
  42. };
  43. /* registers */
  44. #define OCI2C_PRELOW 0
  45. #define OCI2C_PREHIGH 1
  46. #define OCI2C_CONTROL 2
  47. #define OCI2C_DATA 3
  48. #define OCI2C_CMD 4 /* write only */
  49. #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
  50. #define OCI2C_CTRL_IEN 0x40
  51. #define OCI2C_CTRL_EN 0x80
  52. #define OCI2C_CMD_START 0x91
  53. #define OCI2C_CMD_STOP 0x41
  54. #define OCI2C_CMD_READ 0x21
  55. #define OCI2C_CMD_WRITE 0x11
  56. #define OCI2C_CMD_READ_ACK 0x21
  57. #define OCI2C_CMD_READ_NACK 0x29
  58. #define OCI2C_CMD_IACK 0x01
  59. #define OCI2C_STAT_IF 0x01
  60. #define OCI2C_STAT_TIP 0x02
  61. #define OCI2C_STAT_ARBLOST 0x20
  62. #define OCI2C_STAT_BUSY 0x40
  63. #define OCI2C_STAT_NACK 0x80
  64. #define STATE_DONE 0
  65. #define STATE_START 1
  66. #define STATE_WRITE 2
  67. #define STATE_READ 3
  68. #define STATE_ERROR 4
  69. #define TYPE_OCORES 0
  70. #define TYPE_GRLIB 1
  71. static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
  72. {
  73. iowrite8(value, i2c->base + (reg << i2c->reg_shift));
  74. }
  75. static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
  76. {
  77. iowrite16(value, i2c->base + (reg << i2c->reg_shift));
  78. }
  79. static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
  80. {
  81. iowrite32(value, i2c->base + (reg << i2c->reg_shift));
  82. }
  83. static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value)
  84. {
  85. iowrite16be(value, i2c->base + (reg << i2c->reg_shift));
  86. }
  87. static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value)
  88. {
  89. iowrite32be(value, i2c->base + (reg << i2c->reg_shift));
  90. }
  91. static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
  92. {
  93. return ioread8(i2c->base + (reg << i2c->reg_shift));
  94. }
  95. static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
  96. {
  97. return ioread16(i2c->base + (reg << i2c->reg_shift));
  98. }
  99. static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
  100. {
  101. return ioread32(i2c->base + (reg << i2c->reg_shift));
  102. }
  103. static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg)
  104. {
  105. return ioread16be(i2c->base + (reg << i2c->reg_shift));
  106. }
  107. static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg)
  108. {
  109. return ioread32be(i2c->base + (reg << i2c->reg_shift));
  110. }
  111. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  112. {
  113. i2c->setreg(i2c, reg, value);
  114. }
  115. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  116. {
  117. return i2c->getreg(i2c, reg);
  118. }
  119. static void ocores_process(struct ocores_i2c *i2c)
  120. {
  121. struct i2c_msg *msg = i2c->msg;
  122. u8 stat = oc_getreg(i2c, OCI2C_STATUS);
  123. if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
  124. /* stop has been sent */
  125. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  126. wake_up(&i2c->wait);
  127. return;
  128. }
  129. /* error? */
  130. if (stat & OCI2C_STAT_ARBLOST) {
  131. i2c->state = STATE_ERROR;
  132. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  133. return;
  134. }
  135. if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
  136. i2c->state =
  137. (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  138. if (stat & OCI2C_STAT_NACK) {
  139. i2c->state = STATE_ERROR;
  140. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  141. return;
  142. }
  143. } else
  144. msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
  145. /* end of msg? */
  146. if (i2c->pos == msg->len) {
  147. i2c->nmsgs--;
  148. i2c->msg++;
  149. i2c->pos = 0;
  150. msg = i2c->msg;
  151. if (i2c->nmsgs) { /* end? */
  152. /* send start? */
  153. if (!(msg->flags & I2C_M_NOSTART)) {
  154. u8 addr = (msg->addr << 1);
  155. if (msg->flags & I2C_M_RD)
  156. addr |= 1;
  157. i2c->state = STATE_START;
  158. oc_setreg(i2c, OCI2C_DATA, addr);
  159. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  160. return;
  161. } else
  162. i2c->state = (msg->flags & I2C_M_RD)
  163. ? STATE_READ : STATE_WRITE;
  164. } else {
  165. i2c->state = STATE_DONE;
  166. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  167. return;
  168. }
  169. }
  170. if (i2c->state == STATE_READ) {
  171. oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
  172. OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
  173. } else {
  174. oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
  175. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
  176. }
  177. }
  178. static irqreturn_t ocores_isr(int irq, void *dev_id)
  179. {
  180. struct ocores_i2c *i2c = dev_id;
  181. ocores_process(i2c);
  182. return IRQ_HANDLED;
  183. }
  184. static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  185. {
  186. struct ocores_i2c *i2c = i2c_get_adapdata(adap);
  187. i2c->msg = msgs;
  188. i2c->pos = 0;
  189. i2c->nmsgs = num;
  190. i2c->state = STATE_START;
  191. oc_setreg(i2c, OCI2C_DATA,
  192. (i2c->msg->addr << 1) |
  193. ((i2c->msg->flags & I2C_M_RD) ? 1:0));
  194. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  195. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  196. (i2c->state == STATE_DONE), HZ))
  197. return (i2c->state == STATE_DONE) ? num : -EIO;
  198. else
  199. return -ETIMEDOUT;
  200. }
  201. static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
  202. {
  203. int prescale;
  204. int diff;
  205. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  206. /* make sure the device is disabled */
  207. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  208. prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
  209. prescale = clamp(prescale, 0, 0xffff);
  210. diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
  211. if (abs(diff) > i2c->bus_clock_khz / 10) {
  212. dev_err(dev,
  213. "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
  214. i2c->ip_clock_khz, i2c->bus_clock_khz);
  215. return -EINVAL;
  216. }
  217. oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
  218. oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
  219. /* Init the device */
  220. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  221. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
  222. return 0;
  223. }
  224. static u32 ocores_func(struct i2c_adapter *adap)
  225. {
  226. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  227. }
  228. static const struct i2c_algorithm ocores_algorithm = {
  229. .master_xfer = ocores_xfer,
  230. .functionality = ocores_func,
  231. };
  232. static struct i2c_adapter ocores_adapter = {
  233. .owner = THIS_MODULE,
  234. .name = "i2c-ocores",
  235. .class = I2C_CLASS_DEPRECATED,
  236. .algo = &ocores_algorithm,
  237. };
  238. static const struct of_device_id ocores_i2c_match[] = {
  239. {
  240. .compatible = "opencores,i2c-ocores",
  241. .data = (void *)TYPE_OCORES,
  242. },
  243. {
  244. .compatible = "aeroflexgaisler,i2cmst",
  245. .data = (void *)TYPE_GRLIB,
  246. },
  247. {},
  248. };
  249. MODULE_DEVICE_TABLE(of, ocores_i2c_match);
  250. #ifdef CONFIG_OF
  251. /* Read and write functions for the GRLIB port of the controller. Registers are
  252. * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
  253. * register. The subsequent registers has their offset decreased accordingly. */
  254. static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
  255. {
  256. u32 rd;
  257. int rreg = reg;
  258. if (reg != OCI2C_PRELOW)
  259. rreg--;
  260. rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  261. if (reg == OCI2C_PREHIGH)
  262. return (u8)(rd >> 8);
  263. else
  264. return (u8)rd;
  265. }
  266. static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
  267. {
  268. u32 curr, wr;
  269. int rreg = reg;
  270. if (reg != OCI2C_PRELOW)
  271. rreg--;
  272. if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
  273. curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  274. if (reg == OCI2C_PRELOW)
  275. wr = (curr & 0xff00) | value;
  276. else
  277. wr = (((u32)value) << 8) | (curr & 0xff);
  278. } else {
  279. wr = value;
  280. }
  281. iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
  282. }
  283. static int ocores_i2c_of_probe(struct platform_device *pdev,
  284. struct ocores_i2c *i2c)
  285. {
  286. struct device_node *np = pdev->dev.of_node;
  287. const struct of_device_id *match;
  288. u32 val;
  289. u32 clock_frequency;
  290. bool clock_frequency_present;
  291. if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
  292. /* no 'reg-shift', check for deprecated 'regstep' */
  293. if (!of_property_read_u32(np, "regstep", &val)) {
  294. if (!is_power_of_2(val)) {
  295. dev_err(&pdev->dev, "invalid regstep %d\n",
  296. val);
  297. return -EINVAL;
  298. }
  299. i2c->reg_shift = ilog2(val);
  300. dev_warn(&pdev->dev,
  301. "regstep property deprecated, use reg-shift\n");
  302. }
  303. }
  304. clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
  305. &clock_frequency);
  306. i2c->bus_clock_khz = 100;
  307. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  308. if (!IS_ERR(i2c->clk)) {
  309. int ret = clk_prepare_enable(i2c->clk);
  310. if (ret) {
  311. dev_err(&pdev->dev,
  312. "clk_prepare_enable failed: %d\n", ret);
  313. return ret;
  314. }
  315. i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000;
  316. if (clock_frequency_present)
  317. i2c->bus_clock_khz = clock_frequency / 1000;
  318. }
  319. if (i2c->ip_clock_khz == 0) {
  320. if (of_property_read_u32(np, "opencores,ip-clock-frequency",
  321. &val)) {
  322. if (!clock_frequency_present) {
  323. dev_err(&pdev->dev,
  324. "Missing required parameter 'opencores,ip-clock-frequency'\n");
  325. return -ENODEV;
  326. }
  327. i2c->ip_clock_khz = clock_frequency / 1000;
  328. dev_warn(&pdev->dev,
  329. "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
  330. } else {
  331. i2c->ip_clock_khz = val / 1000;
  332. if (clock_frequency_present)
  333. i2c->bus_clock_khz = clock_frequency / 1000;
  334. }
  335. }
  336. of_property_read_u32(pdev->dev.of_node, "reg-io-width",
  337. &i2c->reg_io_width);
  338. match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
  339. if (match && (long)match->data == TYPE_GRLIB) {
  340. dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
  341. i2c->setreg = oc_setreg_grlib;
  342. i2c->getreg = oc_getreg_grlib;
  343. }
  344. return 0;
  345. }
  346. #else
  347. #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
  348. #endif
  349. static int ocores_i2c_probe(struct platform_device *pdev)
  350. {
  351. struct ocores_i2c *i2c;
  352. struct ocores_i2c_platform_data *pdata;
  353. struct resource *res;
  354. int irq;
  355. int ret;
  356. int i;
  357. irq = platform_get_irq(pdev, 0);
  358. if (irq < 0)
  359. return irq;
  360. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  361. if (!i2c)
  362. return -ENOMEM;
  363. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  364. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  365. if (IS_ERR(i2c->base))
  366. return PTR_ERR(i2c->base);
  367. pdata = dev_get_platdata(&pdev->dev);
  368. if (pdata) {
  369. i2c->reg_shift = pdata->reg_shift;
  370. i2c->reg_io_width = pdata->reg_io_width;
  371. i2c->ip_clock_khz = pdata->clock_khz;
  372. i2c->bus_clock_khz = 100;
  373. } else {
  374. ret = ocores_i2c_of_probe(pdev, i2c);
  375. if (ret)
  376. return ret;
  377. }
  378. if (i2c->reg_io_width == 0)
  379. i2c->reg_io_width = 1; /* Set to default value */
  380. if (!i2c->setreg || !i2c->getreg) {
  381. bool be = pdata ? pdata->big_endian :
  382. of_device_is_big_endian(pdev->dev.of_node);
  383. switch (i2c->reg_io_width) {
  384. case 1:
  385. i2c->setreg = oc_setreg_8;
  386. i2c->getreg = oc_getreg_8;
  387. break;
  388. case 2:
  389. i2c->setreg = be ? oc_setreg_16be : oc_setreg_16;
  390. i2c->getreg = be ? oc_getreg_16be : oc_getreg_16;
  391. break;
  392. case 4:
  393. i2c->setreg = be ? oc_setreg_32be : oc_setreg_32;
  394. i2c->getreg = be ? oc_getreg_32be : oc_getreg_32;
  395. break;
  396. default:
  397. dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
  398. i2c->reg_io_width);
  399. return -EINVAL;
  400. }
  401. }
  402. ret = ocores_init(&pdev->dev, i2c);
  403. if (ret)
  404. return ret;
  405. init_waitqueue_head(&i2c->wait);
  406. ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
  407. pdev->name, i2c);
  408. if (ret) {
  409. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  410. return ret;
  411. }
  412. /* hook up driver to tree */
  413. platform_set_drvdata(pdev, i2c);
  414. i2c->adap = ocores_adapter;
  415. i2c_set_adapdata(&i2c->adap, i2c);
  416. i2c->adap.dev.parent = &pdev->dev;
  417. i2c->adap.dev.of_node = pdev->dev.of_node;
  418. /* add i2c adapter to i2c tree */
  419. ret = i2c_add_adapter(&i2c->adap);
  420. if (ret) {
  421. dev_err(&pdev->dev, "Failed to add adapter\n");
  422. return ret;
  423. }
  424. /* add in known devices to the bus */
  425. if (pdata) {
  426. for (i = 0; i < pdata->num_devices; i++)
  427. i2c_new_device(&i2c->adap, pdata->devices + i);
  428. }
  429. return 0;
  430. }
  431. static int ocores_i2c_remove(struct platform_device *pdev)
  432. {
  433. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  434. /* disable i2c logic */
  435. oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
  436. & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  437. /* remove adapter & data */
  438. i2c_del_adapter(&i2c->adap);
  439. if (!IS_ERR(i2c->clk))
  440. clk_disable_unprepare(i2c->clk);
  441. return 0;
  442. }
  443. #ifdef CONFIG_PM_SLEEP
  444. static int ocores_i2c_suspend(struct device *dev)
  445. {
  446. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  447. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  448. /* make sure the device is disabled */
  449. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  450. if (!IS_ERR(i2c->clk))
  451. clk_disable_unprepare(i2c->clk);
  452. return 0;
  453. }
  454. static int ocores_i2c_resume(struct device *dev)
  455. {
  456. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  457. if (!IS_ERR(i2c->clk)) {
  458. unsigned long rate;
  459. int ret = clk_prepare_enable(i2c->clk);
  460. if (ret) {
  461. dev_err(dev,
  462. "clk_prepare_enable failed: %d\n", ret);
  463. return ret;
  464. }
  465. rate = clk_get_rate(i2c->clk) / 1000;
  466. if (rate)
  467. i2c->ip_clock_khz = rate;
  468. }
  469. return ocores_init(dev, i2c);
  470. }
  471. static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
  472. #define OCORES_I2C_PM (&ocores_i2c_pm)
  473. #else
  474. #define OCORES_I2C_PM NULL
  475. #endif
  476. static struct platform_driver ocores_i2c_driver = {
  477. .probe = ocores_i2c_probe,
  478. .remove = ocores_i2c_remove,
  479. .driver = {
  480. .name = "ocores-i2c",
  481. .of_match_table = ocores_i2c_match,
  482. .pm = OCORES_I2C_PM,
  483. },
  484. };
  485. module_platform_driver(ocores_i2c_driver);
  486. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  487. MODULE_DESCRIPTION("OpenCores I2C bus driver");
  488. MODULE_LICENSE("GPL");
  489. MODULE_ALIAS("platform:ocores-i2c");