bbc_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 = kzalloc(sizeof(*client), GFP_KERNEL);
  136. if (!client)
  137. return NULL;
  138. client->bp = bp;
  139. client->echild = echild;
  140. client->bus = echild->resource[0].start;
  141. client->address = echild->resource[1].start;
  142. claim_device(bp, echild);
  143. return client;
  144. }
  145. void bbc_i2c_detach(struct bbc_i2c_client *client)
  146. {
  147. struct bbc_i2c_bus *bp = client->bp;
  148. struct linux_ebus_child *echild = client->echild;
  149. release_device(bp, echild);
  150. kfree(client);
  151. }
  152. static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
  153. {
  154. DECLARE_WAITQUEUE(wait, current);
  155. int limit = 32;
  156. int ret = 1;
  157. bp->waiting = 1;
  158. add_wait_queue(&bp->wq, &wait);
  159. while (limit-- > 0) {
  160. unsigned long val;
  161. val = wait_event_interruptible_timeout(
  162. bp->wq,
  163. (((*status = readb(bp->i2c_control_regs + 0))
  164. & I2C_PCF_PIN) == 0),
  165. msecs_to_jiffies(250));
  166. if (val > 0) {
  167. ret = 0;
  168. break;
  169. }
  170. }
  171. remove_wait_queue(&bp->wq, &wait);
  172. bp->waiting = 0;
  173. return ret;
  174. }
  175. int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
  176. {
  177. struct bbc_i2c_bus *bp = client->bp;
  178. int address = client->address;
  179. u8 status;
  180. int ret = -1;
  181. if (bp->i2c_bussel_reg != NULL)
  182. writeb(client->bus, bp->i2c_bussel_reg);
  183. writeb(address, bp->i2c_control_regs + 0x1);
  184. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  185. if (wait_for_pin(bp, &status))
  186. goto out;
  187. writeb(off, bp->i2c_control_regs + 0x1);
  188. if (wait_for_pin(bp, &status) ||
  189. (status & I2C_PCF_LRB) != 0)
  190. goto out;
  191. writeb(val, bp->i2c_control_regs + 0x1);
  192. if (wait_for_pin(bp, &status))
  193. goto out;
  194. ret = 0;
  195. out:
  196. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  197. return ret;
  198. }
  199. int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
  200. {
  201. struct bbc_i2c_bus *bp = client->bp;
  202. unsigned char address = client->address, status;
  203. int ret = -1;
  204. if (bp->i2c_bussel_reg != NULL)
  205. writeb(client->bus, bp->i2c_bussel_reg);
  206. writeb(address, bp->i2c_control_regs + 0x1);
  207. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  208. if (wait_for_pin(bp, &status))
  209. goto out;
  210. writeb(off, bp->i2c_control_regs + 0x1);
  211. if (wait_for_pin(bp, &status) ||
  212. (status & I2C_PCF_LRB) != 0)
  213. goto out;
  214. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  215. address |= 0x1; /* READ */
  216. writeb(address, bp->i2c_control_regs + 0x1);
  217. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  218. if (wait_for_pin(bp, &status))
  219. goto out;
  220. /* Set PIN back to one so the device sends the first
  221. * byte.
  222. */
  223. (void) readb(bp->i2c_control_regs + 0x1);
  224. if (wait_for_pin(bp, &status))
  225. goto out;
  226. writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
  227. *byte = readb(bp->i2c_control_regs + 0x1);
  228. if (wait_for_pin(bp, &status))
  229. goto out;
  230. ret = 0;
  231. out:
  232. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  233. (void) readb(bp->i2c_control_regs + 0x1);
  234. return ret;
  235. }
  236. int bbc_i2c_write_buf(struct bbc_i2c_client *client,
  237. char *buf, int len, int off)
  238. {
  239. int ret = 0;
  240. while (len > 0) {
  241. int err = bbc_i2c_writeb(client, *buf, off);
  242. if (err < 0) {
  243. ret = err;
  244. break;
  245. }
  246. len--;
  247. buf++;
  248. off++;
  249. }
  250. return ret;
  251. }
  252. int bbc_i2c_read_buf(struct bbc_i2c_client *client,
  253. char *buf, int len, int off)
  254. {
  255. int ret = 0;
  256. while (len > 0) {
  257. int err = bbc_i2c_readb(client, buf, off);
  258. if (err < 0) {
  259. ret = err;
  260. break;
  261. }
  262. len--;
  263. buf++;
  264. off++;
  265. }
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(bbc_i2c_getdev);
  269. EXPORT_SYMBOL(bbc_i2c_attach);
  270. EXPORT_SYMBOL(bbc_i2c_detach);
  271. EXPORT_SYMBOL(bbc_i2c_writeb);
  272. EXPORT_SYMBOL(bbc_i2c_readb);
  273. EXPORT_SYMBOL(bbc_i2c_write_buf);
  274. EXPORT_SYMBOL(bbc_i2c_read_buf);
  275. static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
  276. {
  277. struct bbc_i2c_bus *bp = dev_id;
  278. /* PIN going from set to clear is the only event which
  279. * makes the i2c assert an interrupt.
  280. */
  281. if (bp->waiting &&
  282. !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
  283. wake_up_interruptible(&bp->wq);
  284. return IRQ_HANDLED;
  285. }
  286. static void __init reset_one_i2c(struct bbc_i2c_bus *bp)
  287. {
  288. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  289. writeb(bp->own, bp->i2c_control_regs + 0x1);
  290. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  291. writeb(bp->clock, bp->i2c_control_regs + 0x1);
  292. writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
  293. }
  294. static int __init attach_one_i2c(struct linux_ebus_device *edev, int index)
  295. {
  296. struct bbc_i2c_bus *bp;
  297. struct linux_ebus_child *echild;
  298. int entry;
  299. bp = kzalloc(sizeof(*bp), GFP_KERNEL);
  300. if (!bp)
  301. return -ENOMEM;
  302. bp->i2c_control_regs = ioremap(edev->resource[0].start, 0x2);
  303. if (!bp->i2c_control_regs)
  304. goto fail;
  305. if (edev->num_addrs == 2) {
  306. bp->i2c_bussel_reg = ioremap(edev->resource[1].start, 0x1);
  307. if (!bp->i2c_bussel_reg)
  308. goto fail;
  309. }
  310. bp->waiting = 0;
  311. init_waitqueue_head(&bp->wq);
  312. if (request_irq(edev->irqs[0], bbc_i2c_interrupt,
  313. IRQF_SHARED, "bbc_i2c", bp))
  314. goto fail;
  315. bp->index = index;
  316. bp->bus_edev = edev;
  317. spin_lock_init(&bp->lock);
  318. bp->next = all_bbc_i2c;
  319. all_bbc_i2c = bp;
  320. entry = 0;
  321. for (echild = edev->children;
  322. echild && entry < 8;
  323. echild = echild->next, entry++) {
  324. bp->devs[entry].device = echild;
  325. bp->devs[entry].client_claimed = 0;
  326. }
  327. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  328. bp->own = readb(bp->i2c_control_regs + 0x01);
  329. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  330. bp->clock = readb(bp->i2c_control_regs + 0x01);
  331. printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
  332. bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
  333. reset_one_i2c(bp);
  334. return 0;
  335. fail:
  336. if (bp->i2c_bussel_reg)
  337. iounmap(bp->i2c_bussel_reg);
  338. if (bp->i2c_control_regs)
  339. iounmap(bp->i2c_control_regs);
  340. kfree(bp);
  341. return -EINVAL;
  342. }
  343. static int __init bbc_present(void)
  344. {
  345. struct linux_ebus *ebus = NULL;
  346. struct linux_ebus_device *edev = NULL;
  347. for_each_ebus(ebus) {
  348. for_each_ebusdev(edev, ebus) {
  349. if (!strcmp(edev->prom_node->name, "bbc"))
  350. return 1;
  351. }
  352. }
  353. return 0;
  354. }
  355. extern int bbc_envctrl_init(void);
  356. extern void bbc_envctrl_cleanup(void);
  357. static void bbc_i2c_cleanup(void);
  358. static int __init bbc_i2c_init(void)
  359. {
  360. struct linux_ebus *ebus = NULL;
  361. struct linux_ebus_device *edev = NULL;
  362. int err, index = 0;
  363. if ((tlb_type != cheetah && tlb_type != cheetah_plus) ||
  364. !bbc_present())
  365. return -ENODEV;
  366. for_each_ebus(ebus) {
  367. for_each_ebusdev(edev, ebus) {
  368. if (!strcmp(edev->prom_node->name, "i2c")) {
  369. if (!attach_one_i2c(edev, index))
  370. index++;
  371. }
  372. }
  373. }
  374. if (!index)
  375. return -ENODEV;
  376. err = bbc_envctrl_init();
  377. if (err)
  378. bbc_i2c_cleanup();
  379. return err;
  380. }
  381. static void bbc_i2c_cleanup(void)
  382. {
  383. struct bbc_i2c_bus *bp = all_bbc_i2c;
  384. bbc_envctrl_cleanup();
  385. while (bp != NULL) {
  386. struct bbc_i2c_bus *next = bp->next;
  387. free_irq(bp->bus_edev->irqs[0], bp);
  388. if (bp->i2c_bussel_reg)
  389. iounmap(bp->i2c_bussel_reg);
  390. if (bp->i2c_control_regs)
  391. iounmap(bp->i2c_control_regs);
  392. kfree(bp);
  393. bp = next;
  394. }
  395. all_bbc_i2c = NULL;
  396. }
  397. module_init(bbc_i2c_init);
  398. module_exit(bbc_i2c_cleanup);
  399. MODULE_LICENSE("GPL");