bbc_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* $Id: bbc_i2c.c,v 1.2 2001/04/02 09:59:08 davem Exp $
  2. * bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
  3. * platforms.
  4. *
  5. * Copyright (C) 2001 David S. Miller (davem@redhat.com)
  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/init.h>
  15. #include <linux/interrupt.h>
  16. #include <asm/oplib.h>
  17. #include <asm/ebus.h>
  18. #include <asm/spitfire.h>
  19. #include <asm/bbc.h>
  20. #include <asm/io.h>
  21. #include "bbc_i2c.h"
  22. /* Convert this driver to use i2c bus layer someday... */
  23. #define I2C_PCF_PIN 0x80
  24. #define I2C_PCF_ESO 0x40
  25. #define I2C_PCF_ES1 0x20
  26. #define I2C_PCF_ES2 0x10
  27. #define I2C_PCF_ENI 0x08
  28. #define I2C_PCF_STA 0x04
  29. #define I2C_PCF_STO 0x02
  30. #define I2C_PCF_ACK 0x01
  31. #define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
  32. #define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
  33. #define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
  34. #define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
  35. #define I2C_PCF_INI 0x40 /* 1 if not initialized */
  36. #define I2C_PCF_STS 0x20
  37. #define I2C_PCF_BER 0x10
  38. #define I2C_PCF_AD0 0x08
  39. #define I2C_PCF_LRB 0x08
  40. #define I2C_PCF_AAS 0x04
  41. #define I2C_PCF_LAB 0x02
  42. #define I2C_PCF_BB 0x01
  43. /* The BBC devices have two I2C controllers. The first I2C controller
  44. * connects mainly to configuration proms (NVRAM, cpu configuration,
  45. * dimm types, etc.). Whereas the second I2C controller connects to
  46. * environmental control devices such as fans and temperature sensors.
  47. * The second controller also connects to the smartcard reader, if present.
  48. */
  49. #define NUM_CHILDREN 8
  50. struct bbc_i2c_bus {
  51. struct bbc_i2c_bus *next;
  52. int index;
  53. spinlock_t lock;
  54. void __iomem *i2c_bussel_reg;
  55. void __iomem *i2c_control_regs;
  56. unsigned char own, clock;
  57. wait_queue_head_t wq;
  58. volatile int waiting;
  59. struct linux_ebus_device *bus_edev;
  60. struct {
  61. struct linux_ebus_child *device;
  62. int client_claimed;
  63. } devs[NUM_CHILDREN];
  64. };
  65. static struct bbc_i2c_bus *all_bbc_i2c;
  66. struct bbc_i2c_client {
  67. struct bbc_i2c_bus *bp;
  68. struct linux_ebus_child *echild;
  69. int bus;
  70. int address;
  71. };
  72. static int find_device(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild)
  73. {
  74. int i;
  75. for (i = 0; i < NUM_CHILDREN; i++) {
  76. if (bp->devs[i].device == echild) {
  77. if (bp->devs[i].client_claimed)
  78. return 0;
  79. return 1;
  80. }
  81. }
  82. return 0;
  83. }
  84. static void set_device_claimage(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild, int val)
  85. {
  86. int i;
  87. for (i = 0; i < NUM_CHILDREN; i++) {
  88. if (bp->devs[i].device == echild) {
  89. bp->devs[i].client_claimed = val;
  90. return;
  91. }
  92. }
  93. }
  94. #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
  95. #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
  96. static struct bbc_i2c_bus *find_bus_for_device(struct linux_ebus_child *echild)
  97. {
  98. struct bbc_i2c_bus *bp = all_bbc_i2c;
  99. while (bp != NULL) {
  100. if (find_device(bp, echild) != 0)
  101. break;
  102. bp = bp->next;
  103. }
  104. return bp;
  105. }
  106. struct linux_ebus_child *bbc_i2c_getdev(int index)
  107. {
  108. struct bbc_i2c_bus *bp = all_bbc_i2c;
  109. struct linux_ebus_child *echild = NULL;
  110. int curidx = 0;
  111. while (bp != NULL) {
  112. struct bbc_i2c_bus *next = bp->next;
  113. int i;
  114. for (i = 0; i < NUM_CHILDREN; i++) {
  115. if (!(echild = bp->devs[i].device))
  116. break;
  117. if (curidx == index)
  118. goto out;
  119. echild = NULL;
  120. curidx++;
  121. }
  122. bp = next;
  123. }
  124. out:
  125. if (curidx == index)
  126. return echild;
  127. return NULL;
  128. }
  129. struct bbc_i2c_client *bbc_i2c_attach(struct linux_ebus_child *echild)
  130. {
  131. struct bbc_i2c_bus *bp = find_bus_for_device(echild);
  132. struct bbc_i2c_client *client;
  133. if (!bp)
  134. return NULL;
  135. client = kmalloc(sizeof(*client), GFP_KERNEL);
  136. if (!client)
  137. return NULL;
  138. memset(client, 0, sizeof(*client));
  139. client->bp = bp;
  140. client->echild = echild;
  141. client->bus = echild->resource[0].start;
  142. client->address = echild->resource[1].start;
  143. claim_device(bp, echild);
  144. return client;
  145. }
  146. void bbc_i2c_detach(struct bbc_i2c_client *client)
  147. {
  148. struct bbc_i2c_bus *bp = client->bp;
  149. struct linux_ebus_child *echild = client->echild;
  150. release_device(bp, echild);
  151. kfree(client);
  152. }
  153. static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
  154. {
  155. DECLARE_WAITQUEUE(wait, current);
  156. int limit = 32;
  157. int ret = 1;
  158. bp->waiting = 1;
  159. add_wait_queue(&bp->wq, &wait);
  160. while (limit-- > 0) {
  161. unsigned long val;
  162. val = wait_event_interruptible_timeout(
  163. bp->wq,
  164. (((*status = readb(bp->i2c_control_regs + 0))
  165. & I2C_PCF_PIN) == 0),
  166. msecs_to_jiffies(250));
  167. if (val > 0) {
  168. ret = 0;
  169. break;
  170. }
  171. }
  172. remove_wait_queue(&bp->wq, &wait);
  173. bp->waiting = 0;
  174. return ret;
  175. }
  176. int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
  177. {
  178. struct bbc_i2c_bus *bp = client->bp;
  179. int address = client->address;
  180. u8 status;
  181. int ret = -1;
  182. if (bp->i2c_bussel_reg != NULL)
  183. writeb(client->bus, bp->i2c_bussel_reg);
  184. writeb(address, bp->i2c_control_regs + 0x1);
  185. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  186. if (wait_for_pin(bp, &status))
  187. goto out;
  188. writeb(off, bp->i2c_control_regs + 0x1);
  189. if (wait_for_pin(bp, &status) ||
  190. (status & I2C_PCF_LRB) != 0)
  191. goto out;
  192. writeb(val, bp->i2c_control_regs + 0x1);
  193. if (wait_for_pin(bp, &status))
  194. goto out;
  195. ret = 0;
  196. out:
  197. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  198. return ret;
  199. }
  200. int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
  201. {
  202. struct bbc_i2c_bus *bp = client->bp;
  203. unsigned char address = client->address, status;
  204. int ret = -1;
  205. if (bp->i2c_bussel_reg != NULL)
  206. writeb(client->bus, bp->i2c_bussel_reg);
  207. writeb(address, bp->i2c_control_regs + 0x1);
  208. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  209. if (wait_for_pin(bp, &status))
  210. goto out;
  211. writeb(off, bp->i2c_control_regs + 0x1);
  212. if (wait_for_pin(bp, &status) ||
  213. (status & I2C_PCF_LRB) != 0)
  214. goto out;
  215. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  216. address |= 0x1; /* READ */
  217. writeb(address, bp->i2c_control_regs + 0x1);
  218. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  219. if (wait_for_pin(bp, &status))
  220. goto out;
  221. /* Set PIN back to one so the device sends the first
  222. * byte.
  223. */
  224. (void) readb(bp->i2c_control_regs + 0x1);
  225. if (wait_for_pin(bp, &status))
  226. goto out;
  227. writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
  228. *byte = readb(bp->i2c_control_regs + 0x1);
  229. if (wait_for_pin(bp, &status))
  230. goto out;
  231. ret = 0;
  232. out:
  233. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  234. (void) readb(bp->i2c_control_regs + 0x1);
  235. return ret;
  236. }
  237. int bbc_i2c_write_buf(struct bbc_i2c_client *client,
  238. char *buf, int len, int off)
  239. {
  240. int ret = 0;
  241. while (len > 0) {
  242. int err = bbc_i2c_writeb(client, *buf, off);
  243. if (err < 0) {
  244. ret = err;
  245. break;
  246. }
  247. len--;
  248. buf++;
  249. off++;
  250. }
  251. return ret;
  252. }
  253. int bbc_i2c_read_buf(struct bbc_i2c_client *client,
  254. char *buf, int len, int off)
  255. {
  256. int ret = 0;
  257. while (len > 0) {
  258. int err = bbc_i2c_readb(client, buf, off);
  259. if (err < 0) {
  260. ret = err;
  261. break;
  262. }
  263. len--;
  264. buf++;
  265. off++;
  266. }
  267. return ret;
  268. }
  269. EXPORT_SYMBOL(bbc_i2c_getdev);
  270. EXPORT_SYMBOL(bbc_i2c_attach);
  271. EXPORT_SYMBOL(bbc_i2c_detach);
  272. EXPORT_SYMBOL(bbc_i2c_writeb);
  273. EXPORT_SYMBOL(bbc_i2c_readb);
  274. EXPORT_SYMBOL(bbc_i2c_write_buf);
  275. EXPORT_SYMBOL(bbc_i2c_read_buf);
  276. static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
  277. {
  278. struct bbc_i2c_bus *bp = dev_id;
  279. /* PIN going from set to clear is the only event which
  280. * makes the i2c assert an interrupt.
  281. */
  282. if (bp->waiting &&
  283. !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
  284. wake_up_interruptible(&bp->wq);
  285. return IRQ_HANDLED;
  286. }
  287. static void __init reset_one_i2c(struct bbc_i2c_bus *bp)
  288. {
  289. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  290. writeb(bp->own, bp->i2c_control_regs + 0x1);
  291. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  292. writeb(bp->clock, bp->i2c_control_regs + 0x1);
  293. writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
  294. }
  295. static int __init attach_one_i2c(struct linux_ebus_device *edev, int index)
  296. {
  297. struct bbc_i2c_bus *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
  298. struct linux_ebus_child *echild;
  299. int entry;
  300. if (!bp)
  301. return -ENOMEM;
  302. memset(bp, 0, sizeof(*bp));
  303. bp->i2c_control_regs = ioremap(edev->resource[0].start, 0x2);
  304. if (!bp->i2c_control_regs)
  305. goto fail;
  306. if (edev->num_addrs == 2) {
  307. bp->i2c_bussel_reg = ioremap(edev->resource[1].start, 0x1);
  308. if (!bp->i2c_bussel_reg)
  309. goto fail;
  310. }
  311. bp->waiting = 0;
  312. init_waitqueue_head(&bp->wq);
  313. if (request_irq(edev->irqs[0], bbc_i2c_interrupt,
  314. IRQF_SHARED, "bbc_i2c", bp))
  315. goto fail;
  316. bp->index = index;
  317. bp->bus_edev = edev;
  318. spin_lock_init(&bp->lock);
  319. bp->next = all_bbc_i2c;
  320. all_bbc_i2c = bp;
  321. entry = 0;
  322. for (echild = edev->children;
  323. echild && entry < 8;
  324. echild = echild->next, entry++) {
  325. bp->devs[entry].device = echild;
  326. bp->devs[entry].client_claimed = 0;
  327. }
  328. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  329. bp->own = readb(bp->i2c_control_regs + 0x01);
  330. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  331. bp->clock = readb(bp->i2c_control_regs + 0x01);
  332. printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
  333. bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
  334. reset_one_i2c(bp);
  335. return 0;
  336. fail:
  337. if (bp->i2c_bussel_reg)
  338. iounmap(bp->i2c_bussel_reg);
  339. if (bp->i2c_control_regs)
  340. iounmap(bp->i2c_control_regs);
  341. kfree(bp);
  342. return -EINVAL;
  343. }
  344. static int __init bbc_present(void)
  345. {
  346. struct linux_ebus *ebus = NULL;
  347. struct linux_ebus_device *edev = NULL;
  348. for_each_ebus(ebus) {
  349. for_each_ebusdev(edev, ebus) {
  350. if (!strcmp(edev->prom_node->name, "bbc"))
  351. return 1;
  352. }
  353. }
  354. return 0;
  355. }
  356. extern int bbc_envctrl_init(void);
  357. extern void bbc_envctrl_cleanup(void);
  358. static void bbc_i2c_cleanup(void);
  359. static int __init bbc_i2c_init(void)
  360. {
  361. struct linux_ebus *ebus = NULL;
  362. struct linux_ebus_device *edev = NULL;
  363. int err, index = 0;
  364. if ((tlb_type != cheetah && tlb_type != cheetah_plus) ||
  365. !bbc_present())
  366. return -ENODEV;
  367. for_each_ebus(ebus) {
  368. for_each_ebusdev(edev, ebus) {
  369. if (!strcmp(edev->prom_node->name, "i2c")) {
  370. if (!attach_one_i2c(edev, index))
  371. index++;
  372. }
  373. }
  374. }
  375. if (!index)
  376. return -ENODEV;
  377. err = bbc_envctrl_init();
  378. if (err)
  379. bbc_i2c_cleanup();
  380. return err;
  381. }
  382. static void bbc_i2c_cleanup(void)
  383. {
  384. struct bbc_i2c_bus *bp = all_bbc_i2c;
  385. bbc_envctrl_cleanup();
  386. while (bp != NULL) {
  387. struct bbc_i2c_bus *next = bp->next;
  388. free_irq(bp->bus_edev->irqs[0], bp);
  389. if (bp->i2c_bussel_reg)
  390. iounmap(bp->i2c_bussel_reg);
  391. if (bp->i2c_control_regs)
  392. iounmap(bp->i2c_control_regs);
  393. kfree(bp);
  394. bp = next;
  395. }
  396. all_bbc_i2c = NULL;
  397. }
  398. module_init(bbc_i2c_init);
  399. module_exit(bbc_i2c_cleanup);
  400. MODULE_LICENSE("GPL");