bbc_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 "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. #define NUM_CHILDREN 8
  49. struct bbc_i2c_bus {
  50. struct bbc_i2c_bus *next;
  51. int index;
  52. spinlock_t lock;
  53. void __iomem *i2c_bussel_reg;
  54. void __iomem *i2c_control_regs;
  55. unsigned char own, clock;
  56. wait_queue_head_t wq;
  57. volatile int waiting;
  58. struct linux_ebus_device *bus_edev;
  59. struct {
  60. struct linux_ebus_child *device;
  61. int client_claimed;
  62. } devs[NUM_CHILDREN];
  63. };
  64. static struct bbc_i2c_bus *all_bbc_i2c;
  65. struct bbc_i2c_client {
  66. struct bbc_i2c_bus *bp;
  67. struct linux_ebus_child *echild;
  68. int bus;
  69. int address;
  70. };
  71. static int find_device(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild)
  72. {
  73. int i;
  74. for (i = 0; i < NUM_CHILDREN; i++) {
  75. if (bp->devs[i].device == echild) {
  76. if (bp->devs[i].client_claimed)
  77. return 0;
  78. return 1;
  79. }
  80. }
  81. return 0;
  82. }
  83. static void set_device_claimage(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild, int val)
  84. {
  85. int i;
  86. for (i = 0; i < NUM_CHILDREN; i++) {
  87. if (bp->devs[i].device == echild) {
  88. bp->devs[i].client_claimed = val;
  89. return;
  90. }
  91. }
  92. }
  93. #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
  94. #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
  95. static struct bbc_i2c_bus *find_bus_for_device(struct linux_ebus_child *echild)
  96. {
  97. struct bbc_i2c_bus *bp = all_bbc_i2c;
  98. while (bp != NULL) {
  99. if (find_device(bp, echild) != 0)
  100. break;
  101. bp = bp->next;
  102. }
  103. return bp;
  104. }
  105. struct linux_ebus_child *bbc_i2c_getdev(int index)
  106. {
  107. struct bbc_i2c_bus *bp = all_bbc_i2c;
  108. struct linux_ebus_child *echild = NULL;
  109. int curidx = 0;
  110. while (bp != NULL) {
  111. struct bbc_i2c_bus *next = bp->next;
  112. int i;
  113. for (i = 0; i < NUM_CHILDREN; i++) {
  114. if (!(echild = bp->devs[i].device))
  115. break;
  116. if (curidx == index)
  117. goto out;
  118. echild = NULL;
  119. curidx++;
  120. }
  121. bp = next;
  122. }
  123. out:
  124. if (curidx == index)
  125. return echild;
  126. return NULL;
  127. }
  128. struct bbc_i2c_client *bbc_i2c_attach(struct linux_ebus_child *echild)
  129. {
  130. struct bbc_i2c_bus *bp = find_bus_for_device(echild);
  131. struct bbc_i2c_client *client;
  132. if (!bp)
  133. return NULL;
  134. client = kmalloc(sizeof(*client), GFP_KERNEL);
  135. if (!client)
  136. return NULL;
  137. memset(client, 0, sizeof(*client));
  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. u8 val;
  161. set_current_state(TASK_INTERRUPTIBLE);
  162. *status = val = readb(bp->i2c_control_regs + 0);
  163. if ((val & I2C_PCF_PIN) == 0) {
  164. ret = 0;
  165. break;
  166. }
  167. msleep_interruptible(250);
  168. }
  169. remove_wait_queue(&bp->wq, &wait);
  170. bp->waiting = 0;
  171. current->state = TASK_RUNNING;
  172. return ret;
  173. }
  174. int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
  175. {
  176. struct bbc_i2c_bus *bp = client->bp;
  177. int address = client->address;
  178. u8 status;
  179. int ret = -1;
  180. if (bp->i2c_bussel_reg != NULL)
  181. writeb(client->bus, bp->i2c_bussel_reg);
  182. writeb(address, bp->i2c_control_regs + 0x1);
  183. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  184. if (wait_for_pin(bp, &status))
  185. goto out;
  186. writeb(off, bp->i2c_control_regs + 0x1);
  187. if (wait_for_pin(bp, &status) ||
  188. (status & I2C_PCF_LRB) != 0)
  189. goto out;
  190. writeb(val, bp->i2c_control_regs + 0x1);
  191. if (wait_for_pin(bp, &status))
  192. goto out;
  193. ret = 0;
  194. out:
  195. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  196. return ret;
  197. }
  198. int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
  199. {
  200. struct bbc_i2c_bus *bp = client->bp;
  201. unsigned char address = client->address, status;
  202. int ret = -1;
  203. if (bp->i2c_bussel_reg != NULL)
  204. writeb(client->bus, bp->i2c_bussel_reg);
  205. writeb(address, bp->i2c_control_regs + 0x1);
  206. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  207. if (wait_for_pin(bp, &status))
  208. goto out;
  209. writeb(off, bp->i2c_control_regs + 0x1);
  210. if (wait_for_pin(bp, &status) ||
  211. (status & I2C_PCF_LRB) != 0)
  212. goto out;
  213. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  214. address |= 0x1; /* READ */
  215. writeb(address, bp->i2c_control_regs + 0x1);
  216. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  217. if (wait_for_pin(bp, &status))
  218. goto out;
  219. /* Set PIN back to one so the device sends the first
  220. * byte.
  221. */
  222. (void) readb(bp->i2c_control_regs + 0x1);
  223. if (wait_for_pin(bp, &status))
  224. goto out;
  225. writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
  226. *byte = readb(bp->i2c_control_regs + 0x1);
  227. if (wait_for_pin(bp, &status))
  228. goto out;
  229. ret = 0;
  230. out:
  231. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  232. (void) readb(bp->i2c_control_regs + 0x1);
  233. return ret;
  234. }
  235. int bbc_i2c_write_buf(struct bbc_i2c_client *client,
  236. char *buf, int len, int off)
  237. {
  238. int ret = 0;
  239. while (len > 0) {
  240. int err = bbc_i2c_writeb(client, *buf, off);
  241. if (err < 0) {
  242. ret = err;
  243. break;
  244. }
  245. len--;
  246. buf++;
  247. off++;
  248. }
  249. return ret;
  250. }
  251. int bbc_i2c_read_buf(struct bbc_i2c_client *client,
  252. char *buf, int len, int off)
  253. {
  254. int ret = 0;
  255. while (len > 0) {
  256. int err = bbc_i2c_readb(client, buf, off);
  257. if (err < 0) {
  258. ret = err;
  259. break;
  260. }
  261. len--;
  262. buf++;
  263. off++;
  264. }
  265. return ret;
  266. }
  267. EXPORT_SYMBOL(bbc_i2c_getdev);
  268. EXPORT_SYMBOL(bbc_i2c_attach);
  269. EXPORT_SYMBOL(bbc_i2c_detach);
  270. EXPORT_SYMBOL(bbc_i2c_writeb);
  271. EXPORT_SYMBOL(bbc_i2c_readb);
  272. EXPORT_SYMBOL(bbc_i2c_write_buf);
  273. EXPORT_SYMBOL(bbc_i2c_read_buf);
  274. static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  275. {
  276. struct bbc_i2c_bus *bp = dev_id;
  277. /* PIN going from set to clear is the only event which
  278. * makes the i2c assert an interrupt.
  279. */
  280. if (bp->waiting &&
  281. !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
  282. wake_up(&bp->wq);
  283. return IRQ_HANDLED;
  284. }
  285. static void __init reset_one_i2c(struct bbc_i2c_bus *bp)
  286. {
  287. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  288. writeb(bp->own, bp->i2c_control_regs + 0x1);
  289. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  290. writeb(bp->clock, bp->i2c_control_regs + 0x1);
  291. writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
  292. }
  293. static int __init attach_one_i2c(struct linux_ebus_device *edev, int index)
  294. {
  295. struct bbc_i2c_bus *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
  296. struct linux_ebus_child *echild;
  297. int entry;
  298. if (!bp)
  299. return -ENOMEM;
  300. memset(bp, 0, sizeof(*bp));
  301. bp->i2c_control_regs = ioremap(edev->resource[0].start, 0x2);
  302. if (!bp->i2c_control_regs)
  303. goto fail;
  304. if (edev->num_addrs == 2) {
  305. bp->i2c_bussel_reg = ioremap(edev->resource[1].start, 0x1);
  306. if (!bp->i2c_bussel_reg)
  307. goto fail;
  308. }
  309. bp->waiting = 0;
  310. init_waitqueue_head(&bp->wq);
  311. if (request_irq(edev->irqs[0], bbc_i2c_interrupt,
  312. SA_SHIRQ, "bbc_i2c", bp))
  313. goto fail;
  314. bp->index = index;
  315. bp->bus_edev = edev;
  316. spin_lock_init(&bp->lock);
  317. bp->next = all_bbc_i2c;
  318. all_bbc_i2c = bp;
  319. entry = 0;
  320. for (echild = edev->children;
  321. echild && entry < 8;
  322. echild = echild->next, entry++) {
  323. bp->devs[entry].device = echild;
  324. bp->devs[entry].client_claimed = 0;
  325. }
  326. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  327. bp->own = readb(bp->i2c_control_regs + 0x01);
  328. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  329. bp->clock = readb(bp->i2c_control_regs + 0x01);
  330. printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
  331. bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
  332. reset_one_i2c(bp);
  333. return 0;
  334. fail:
  335. if (bp->i2c_bussel_reg)
  336. iounmap(bp->i2c_bussel_reg);
  337. if (bp->i2c_control_regs)
  338. iounmap(bp->i2c_control_regs);
  339. kfree(bp);
  340. return -EINVAL;
  341. }
  342. static int __init bbc_present(void)
  343. {
  344. struct linux_ebus *ebus = NULL;
  345. struct linux_ebus_device *edev = NULL;
  346. for_each_ebus(ebus) {
  347. for_each_ebusdev(edev, ebus) {
  348. if (!strcmp(edev->prom_name, "bbc"))
  349. return 1;
  350. }
  351. }
  352. return 0;
  353. }
  354. extern int bbc_envctrl_init(void);
  355. extern void bbc_envctrl_cleanup(void);
  356. static void bbc_i2c_cleanup(void);
  357. static int __init bbc_i2c_init(void)
  358. {
  359. struct linux_ebus *ebus = NULL;
  360. struct linux_ebus_device *edev = NULL;
  361. int err, index = 0;
  362. if ((tlb_type != cheetah && tlb_type != cheetah_plus) ||
  363. !bbc_present())
  364. return -ENODEV;
  365. for_each_ebus(ebus) {
  366. for_each_ebusdev(edev, ebus) {
  367. if (!strcmp(edev->prom_name, "i2c")) {
  368. if (!attach_one_i2c(edev, index))
  369. index++;
  370. }
  371. }
  372. }
  373. if (!index)
  374. return -ENODEV;
  375. err = bbc_envctrl_init();
  376. if (err)
  377. bbc_i2c_cleanup();
  378. return err;
  379. }
  380. static void bbc_i2c_cleanup(void)
  381. {
  382. struct bbc_i2c_bus *bp = all_bbc_i2c;
  383. bbc_envctrl_cleanup();
  384. while (bp != NULL) {
  385. struct bbc_i2c_bus *next = bp->next;
  386. free_irq(bp->bus_edev->irqs[0], bp);
  387. if (bp->i2c_bussel_reg)
  388. iounmap(bp->i2c_bussel_reg);
  389. if (bp->i2c_control_regs)
  390. iounmap(bp->i2c_control_regs);
  391. kfree(bp);
  392. bp = next;
  393. }
  394. all_bbc_i2c = NULL;
  395. }
  396. module_init(bbc_i2c_init);
  397. module_exit(bbc_i2c_cleanup);
  398. MODULE_LICENSE("GPL");