bbc_i2c.c 9.6 KB

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