bbc_i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
  3. * platforms.
  4. *
  5. * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/bbc.h>
  19. #include <asm/io.h>
  20. #include "bbc_i2c.h"
  21. /* Convert this driver to use i2c bus layer someday... */
  22. #define I2C_PCF_PIN 0x80
  23. #define I2C_PCF_ESO 0x40
  24. #define I2C_PCF_ES1 0x20
  25. #define I2C_PCF_ES2 0x10
  26. #define I2C_PCF_ENI 0x08
  27. #define I2C_PCF_STA 0x04
  28. #define I2C_PCF_STO 0x02
  29. #define I2C_PCF_ACK 0x01
  30. #define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
  31. #define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
  32. #define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
  33. #define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
  34. #define I2C_PCF_INI 0x40 /* 1 if not initialized */
  35. #define I2C_PCF_STS 0x20
  36. #define I2C_PCF_BER 0x10
  37. #define I2C_PCF_AD0 0x08
  38. #define I2C_PCF_LRB 0x08
  39. #define I2C_PCF_AAS 0x04
  40. #define I2C_PCF_LAB 0x02
  41. #define I2C_PCF_BB 0x01
  42. /* The BBC devices have two I2C controllers. The first I2C controller
  43. * connects mainly to configuration proms (NVRAM, cpu configuration,
  44. * dimm types, etc.). Whereas the second I2C controller connects to
  45. * environmental control devices such as fans and temperature sensors.
  46. * The second controller also connects to the smartcard reader, if present.
  47. */
  48. static void set_device_claimage(struct bbc_i2c_bus *bp, struct platform_device *op, int val)
  49. {
  50. int i;
  51. for (i = 0; i < NUM_CHILDREN; i++) {
  52. if (bp->devs[i].device == op) {
  53. bp->devs[i].client_claimed = val;
  54. return;
  55. }
  56. }
  57. }
  58. #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
  59. #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
  60. struct platform_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index)
  61. {
  62. struct platform_device *op = NULL;
  63. int curidx = 0, i;
  64. for (i = 0; i < NUM_CHILDREN; i++) {
  65. if (!(op = bp->devs[i].device))
  66. break;
  67. if (curidx == index)
  68. goto out;
  69. op = NULL;
  70. curidx++;
  71. }
  72. out:
  73. if (curidx == index)
  74. return op;
  75. return NULL;
  76. }
  77. struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct platform_device *op)
  78. {
  79. struct bbc_i2c_client *client;
  80. const u32 *reg;
  81. client = kzalloc(sizeof(*client), GFP_KERNEL);
  82. if (!client)
  83. return NULL;
  84. client->bp = bp;
  85. client->op = op;
  86. reg = of_get_property(op->dev.of_node, "reg", NULL);
  87. if (!reg) {
  88. kfree(client);
  89. return NULL;
  90. }
  91. client->bus = reg[0];
  92. client->address = reg[1];
  93. claim_device(bp, op);
  94. return client;
  95. }
  96. void bbc_i2c_detach(struct bbc_i2c_client *client)
  97. {
  98. struct bbc_i2c_bus *bp = client->bp;
  99. struct platform_device *op = client->op;
  100. release_device(bp, op);
  101. kfree(client);
  102. }
  103. static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
  104. {
  105. DECLARE_WAITQUEUE(wait, current);
  106. int limit = 32;
  107. int ret = 1;
  108. bp->waiting = 1;
  109. add_wait_queue(&bp->wq, &wait);
  110. while (limit-- > 0) {
  111. long val;
  112. val = wait_event_interruptible_timeout(
  113. bp->wq,
  114. (((*status = readb(bp->i2c_control_regs + 0))
  115. & I2C_PCF_PIN) == 0),
  116. msecs_to_jiffies(250));
  117. if (val > 0) {
  118. ret = 0;
  119. break;
  120. }
  121. }
  122. remove_wait_queue(&bp->wq, &wait);
  123. bp->waiting = 0;
  124. return ret;
  125. }
  126. int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
  127. {
  128. struct bbc_i2c_bus *bp = client->bp;
  129. int address = client->address;
  130. u8 status;
  131. int ret = -1;
  132. if (bp->i2c_bussel_reg != NULL)
  133. writeb(client->bus, bp->i2c_bussel_reg);
  134. writeb(address, bp->i2c_control_regs + 0x1);
  135. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  136. if (wait_for_pin(bp, &status))
  137. goto out;
  138. writeb(off, bp->i2c_control_regs + 0x1);
  139. if (wait_for_pin(bp, &status) ||
  140. (status & I2C_PCF_LRB) != 0)
  141. goto out;
  142. writeb(val, bp->i2c_control_regs + 0x1);
  143. if (wait_for_pin(bp, &status))
  144. goto out;
  145. ret = 0;
  146. out:
  147. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  148. return ret;
  149. }
  150. int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
  151. {
  152. struct bbc_i2c_bus *bp = client->bp;
  153. unsigned char address = client->address, status;
  154. int ret = -1;
  155. if (bp->i2c_bussel_reg != NULL)
  156. writeb(client->bus, bp->i2c_bussel_reg);
  157. writeb(address, bp->i2c_control_regs + 0x1);
  158. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  159. if (wait_for_pin(bp, &status))
  160. goto out;
  161. writeb(off, bp->i2c_control_regs + 0x1);
  162. if (wait_for_pin(bp, &status) ||
  163. (status & I2C_PCF_LRB) != 0)
  164. goto out;
  165. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  166. address |= 0x1; /* READ */
  167. writeb(address, bp->i2c_control_regs + 0x1);
  168. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  169. if (wait_for_pin(bp, &status))
  170. goto out;
  171. /* Set PIN back to one so the device sends the first
  172. * byte.
  173. */
  174. (void) readb(bp->i2c_control_regs + 0x1);
  175. if (wait_for_pin(bp, &status))
  176. goto out;
  177. writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
  178. *byte = readb(bp->i2c_control_regs + 0x1);
  179. if (wait_for_pin(bp, &status))
  180. goto out;
  181. ret = 0;
  182. out:
  183. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  184. (void) readb(bp->i2c_control_regs + 0x1);
  185. return ret;
  186. }
  187. int bbc_i2c_write_buf(struct bbc_i2c_client *client,
  188. char *buf, int len, int off)
  189. {
  190. int ret = 0;
  191. while (len > 0) {
  192. ret = bbc_i2c_writeb(client, *buf, off);
  193. if (ret < 0)
  194. break;
  195. len--;
  196. buf++;
  197. off++;
  198. }
  199. return ret;
  200. }
  201. int bbc_i2c_read_buf(struct bbc_i2c_client *client,
  202. char *buf, int len, int off)
  203. {
  204. int ret = 0;
  205. while (len > 0) {
  206. ret = bbc_i2c_readb(client, buf, off);
  207. if (ret < 0)
  208. break;
  209. len--;
  210. buf++;
  211. off++;
  212. }
  213. return ret;
  214. }
  215. EXPORT_SYMBOL(bbc_i2c_getdev);
  216. EXPORT_SYMBOL(bbc_i2c_attach);
  217. EXPORT_SYMBOL(bbc_i2c_detach);
  218. EXPORT_SYMBOL(bbc_i2c_writeb);
  219. EXPORT_SYMBOL(bbc_i2c_readb);
  220. EXPORT_SYMBOL(bbc_i2c_write_buf);
  221. EXPORT_SYMBOL(bbc_i2c_read_buf);
  222. static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
  223. {
  224. struct bbc_i2c_bus *bp = dev_id;
  225. /* PIN going from set to clear is the only event which
  226. * makes the i2c assert an interrupt.
  227. */
  228. if (bp->waiting &&
  229. !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
  230. wake_up_interruptible(&bp->wq);
  231. return IRQ_HANDLED;
  232. }
  233. static void reset_one_i2c(struct bbc_i2c_bus *bp)
  234. {
  235. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  236. writeb(bp->own, bp->i2c_control_regs + 0x1);
  237. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  238. writeb(bp->clock, bp->i2c_control_regs + 0x1);
  239. writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
  240. }
  241. static struct bbc_i2c_bus * attach_one_i2c(struct platform_device *op, int index)
  242. {
  243. struct bbc_i2c_bus *bp;
  244. struct device_node *dp;
  245. int entry;
  246. bp = kzalloc(sizeof(*bp), GFP_KERNEL);
  247. if (!bp)
  248. return NULL;
  249. INIT_LIST_HEAD(&bp->temps);
  250. INIT_LIST_HEAD(&bp->fans);
  251. bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs");
  252. if (!bp->i2c_control_regs)
  253. goto fail;
  254. if (op->num_resources == 2) {
  255. bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel");
  256. if (!bp->i2c_bussel_reg)
  257. goto fail;
  258. }
  259. bp->waiting = 0;
  260. init_waitqueue_head(&bp->wq);
  261. if (request_irq(op->archdata.irqs[0], bbc_i2c_interrupt,
  262. IRQF_SHARED, "bbc_i2c", bp))
  263. goto fail;
  264. bp->index = index;
  265. bp->op = op;
  266. spin_lock_init(&bp->lock);
  267. entry = 0;
  268. for (dp = op->dev.of_node->child;
  269. dp && entry < 8;
  270. dp = dp->sibling, entry++) {
  271. struct platform_device *child_op;
  272. child_op = of_find_device_by_node(dp);
  273. bp->devs[entry].device = child_op;
  274. bp->devs[entry].client_claimed = 0;
  275. }
  276. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  277. bp->own = readb(bp->i2c_control_regs + 0x01);
  278. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  279. bp->clock = readb(bp->i2c_control_regs + 0x01);
  280. printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
  281. bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
  282. reset_one_i2c(bp);
  283. return bp;
  284. fail:
  285. if (bp->i2c_bussel_reg)
  286. of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1);
  287. if (bp->i2c_control_regs)
  288. of_iounmap(&op->resource[0], bp->i2c_control_regs, 2);
  289. kfree(bp);
  290. return NULL;
  291. }
  292. static int bbc_i2c_probe(struct platform_device *op)
  293. {
  294. struct bbc_i2c_bus *bp;
  295. int err, index = 0;
  296. bp = attach_one_i2c(op, index);
  297. if (!bp)
  298. return -EINVAL;
  299. err = bbc_envctrl_init(bp);
  300. if (err) {
  301. free_irq(op->archdata.irqs[0], bp);
  302. if (bp->i2c_bussel_reg)
  303. of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
  304. if (bp->i2c_control_regs)
  305. of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
  306. kfree(bp);
  307. } else {
  308. dev_set_drvdata(&op->dev, bp);
  309. }
  310. return err;
  311. }
  312. static void bbc_i2c_remove(struct platform_device *op)
  313. {
  314. struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev);
  315. bbc_envctrl_cleanup(bp);
  316. free_irq(op->archdata.irqs[0], bp);
  317. if (bp->i2c_bussel_reg)
  318. of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
  319. if (bp->i2c_control_regs)
  320. of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
  321. kfree(bp);
  322. }
  323. static const struct of_device_id bbc_i2c_match[] = {
  324. {
  325. .name = "i2c",
  326. .compatible = "SUNW,bbc-i2c",
  327. },
  328. {},
  329. };
  330. MODULE_DEVICE_TABLE(of, bbc_i2c_match);
  331. static struct platform_driver bbc_i2c_driver = {
  332. .driver = {
  333. .name = "bbc_i2c",
  334. .of_match_table = bbc_i2c_match,
  335. },
  336. .probe = bbc_i2c_probe,
  337. .remove_new = bbc_i2c_remove,
  338. };
  339. module_platform_driver(bbc_i2c_driver);
  340. MODULE_DESCRIPTION("UltraSPARC-III bootbus i2c controller driver");
  341. MODULE_LICENSE("GPL");