pmac_low_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * arch/ppc/platforms/pmac_low_i2c.c
  3. *
  4. * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * This file contains some low-level i2c access routines that
  12. * need to be used by various bits of the PowerMac platform code
  13. * at times where the real asynchronous & interrupt driven driver
  14. * cannot be used. The API borrows some semantics from the darwin
  15. * driver in order to ease the implementation of the platform
  16. * properties parser
  17. */
  18. #include <linux/config.h>
  19. #include <linux/types.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/adb.h>
  25. #include <linux/pmu.h>
  26. #include <asm/keylargo.h>
  27. #include <asm/uninorth.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/machdep.h>
  31. #include <asm/pmac_low_i2c.h>
  32. #define MAX_LOW_I2C_HOST 4
  33. #if 1
  34. #define DBG(x...) do {\
  35. printk(KERN_DEBUG "KW:" x); \
  36. } while(0)
  37. #else
  38. #define DBGG(x...)
  39. #endif
  40. struct low_i2c_host;
  41. typedef int (*low_i2c_func_t)(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len);
  42. struct low_i2c_host
  43. {
  44. struct device_node *np; /* OF device node */
  45. struct semaphore mutex; /* Access mutex for use by i2c-keywest */
  46. low_i2c_func_t func; /* Access function */
  47. int is_open : 1; /* Poor man's access control */
  48. int mode; /* Current mode */
  49. int channel; /* Current channel */
  50. int num_channels; /* Number of channels */
  51. void __iomem * base; /* For keywest-i2c, base address */
  52. int bsteps; /* And register stepping */
  53. int speed; /* And speed */
  54. };
  55. static struct low_i2c_host low_i2c_hosts[MAX_LOW_I2C_HOST];
  56. /* No locking is necessary on allocation, we are running way before
  57. * anything can race with us
  58. */
  59. static struct low_i2c_host *find_low_i2c_host(struct device_node *np)
  60. {
  61. int i;
  62. for (i = 0; i < MAX_LOW_I2C_HOST; i++)
  63. if (low_i2c_hosts[i].np == np)
  64. return &low_i2c_hosts[i];
  65. return NULL;
  66. }
  67. /*
  68. *
  69. * i2c-keywest implementation (UniNorth, U2, U3, Keylargo's)
  70. *
  71. */
  72. /*
  73. * Keywest i2c definitions borrowed from drivers/i2c/i2c-keywest.h,
  74. * should be moved somewhere in include/asm-ppc/
  75. */
  76. /* Register indices */
  77. typedef enum {
  78. reg_mode = 0,
  79. reg_control,
  80. reg_status,
  81. reg_isr,
  82. reg_ier,
  83. reg_addr,
  84. reg_subaddr,
  85. reg_data
  86. } reg_t;
  87. /* Mode register */
  88. #define KW_I2C_MODE_100KHZ 0x00
  89. #define KW_I2C_MODE_50KHZ 0x01
  90. #define KW_I2C_MODE_25KHZ 0x02
  91. #define KW_I2C_MODE_DUMB 0x00
  92. #define KW_I2C_MODE_STANDARD 0x04
  93. #define KW_I2C_MODE_STANDARDSUB 0x08
  94. #define KW_I2C_MODE_COMBINED 0x0C
  95. #define KW_I2C_MODE_MODE_MASK 0x0C
  96. #define KW_I2C_MODE_CHAN_MASK 0xF0
  97. /* Control register */
  98. #define KW_I2C_CTL_AAK 0x01
  99. #define KW_I2C_CTL_XADDR 0x02
  100. #define KW_I2C_CTL_STOP 0x04
  101. #define KW_I2C_CTL_START 0x08
  102. /* Status register */
  103. #define KW_I2C_STAT_BUSY 0x01
  104. #define KW_I2C_STAT_LAST_AAK 0x02
  105. #define KW_I2C_STAT_LAST_RW 0x04
  106. #define KW_I2C_STAT_SDA 0x08
  107. #define KW_I2C_STAT_SCL 0x10
  108. /* IER & ISR registers */
  109. #define KW_I2C_IRQ_DATA 0x01
  110. #define KW_I2C_IRQ_ADDR 0x02
  111. #define KW_I2C_IRQ_STOP 0x04
  112. #define KW_I2C_IRQ_START 0x08
  113. #define KW_I2C_IRQ_MASK 0x0F
  114. /* State machine states */
  115. enum {
  116. state_idle,
  117. state_addr,
  118. state_read,
  119. state_write,
  120. state_stop,
  121. state_dead
  122. };
  123. #define WRONG_STATE(name) do {\
  124. printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
  125. name, __kw_state_names[state], isr); \
  126. } while(0)
  127. static const char *__kw_state_names[] = {
  128. "state_idle",
  129. "state_addr",
  130. "state_read",
  131. "state_write",
  132. "state_stop",
  133. "state_dead"
  134. };
  135. static inline u8 __kw_read_reg(struct low_i2c_host *host, reg_t reg)
  136. {
  137. return in_8(host->base + (((unsigned)reg) << host->bsteps));
  138. }
  139. static inline void __kw_write_reg(struct low_i2c_host *host, reg_t reg, u8 val)
  140. {
  141. out_8(host->base + (((unsigned)reg) << host->bsteps), val);
  142. (void)__kw_read_reg(host, reg_subaddr);
  143. }
  144. #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
  145. #define kw_read_reg(reg) __kw_read_reg(host, reg)
  146. /* Don't schedule, the g5 fan controller is too
  147. * timing sensitive
  148. */
  149. static u8 kw_wait_interrupt(struct low_i2c_host* host)
  150. {
  151. int i;
  152. u8 isr;
  153. for (i = 0; i < 200000; i++) {
  154. isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
  155. if (isr != 0)
  156. return isr;
  157. udelay(1);
  158. }
  159. return isr;
  160. }
  161. static int kw_handle_interrupt(struct low_i2c_host *host, int state, int rw, int *rc, u8 **data, int *len, u8 isr)
  162. {
  163. u8 ack;
  164. if (isr == 0) {
  165. if (state != state_stop) {
  166. DBG("KW: Timeout !\n");
  167. *rc = -EIO;
  168. goto stop;
  169. }
  170. if (state == state_stop) {
  171. ack = kw_read_reg(reg_status);
  172. if (!(ack & KW_I2C_STAT_BUSY)) {
  173. state = state_idle;
  174. kw_write_reg(reg_ier, 0x00);
  175. }
  176. }
  177. return state;
  178. }
  179. if (isr & KW_I2C_IRQ_ADDR) {
  180. ack = kw_read_reg(reg_status);
  181. if (state != state_addr) {
  182. kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  183. WRONG_STATE("KW_I2C_IRQ_ADDR");
  184. *rc = -EIO;
  185. goto stop;
  186. }
  187. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  188. *rc = -ENODEV;
  189. DBG("KW: NAK on address\n");
  190. return state_stop;
  191. } else {
  192. if (rw) {
  193. state = state_read;
  194. if (*len > 1)
  195. kw_write_reg(reg_control, KW_I2C_CTL_AAK);
  196. } else {
  197. state = state_write;
  198. kw_write_reg(reg_data, **data);
  199. (*data)++; (*len)--;
  200. }
  201. }
  202. kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  203. }
  204. if (isr & KW_I2C_IRQ_DATA) {
  205. if (state == state_read) {
  206. **data = kw_read_reg(reg_data);
  207. (*data)++; (*len)--;
  208. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  209. if ((*len) == 0)
  210. state = state_stop;
  211. else if ((*len) == 1)
  212. kw_write_reg(reg_control, 0);
  213. } else if (state == state_write) {
  214. ack = kw_read_reg(reg_status);
  215. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  216. DBG("KW: nack on data write\n");
  217. *rc = -EIO;
  218. goto stop;
  219. } else if (*len) {
  220. kw_write_reg(reg_data, **data);
  221. (*data)++; (*len)--;
  222. } else {
  223. kw_write_reg(reg_control, KW_I2C_CTL_STOP);
  224. state = state_stop;
  225. *rc = 0;
  226. }
  227. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  228. } else {
  229. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  230. WRONG_STATE("KW_I2C_IRQ_DATA");
  231. if (state != state_stop) {
  232. *rc = -EIO;
  233. goto stop;
  234. }
  235. }
  236. }
  237. if (isr & KW_I2C_IRQ_STOP) {
  238. kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
  239. if (state != state_stop) {
  240. WRONG_STATE("KW_I2C_IRQ_STOP");
  241. *rc = -EIO;
  242. }
  243. return state_idle;
  244. }
  245. if (isr & KW_I2C_IRQ_START)
  246. kw_write_reg(reg_isr, KW_I2C_IRQ_START);
  247. return state;
  248. stop:
  249. kw_write_reg(reg_control, KW_I2C_CTL_STOP);
  250. return state_stop;
  251. }
  252. static int keywest_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 subaddr, u8 *data, int len)
  253. {
  254. u8 mode_reg = host->speed;
  255. int state = state_addr;
  256. int rc = 0;
  257. /* Setup mode & subaddress if any */
  258. switch(host->mode) {
  259. case pmac_low_i2c_mode_dumb:
  260. printk(KERN_ERR "low_i2c: Dumb mode not supported !\n");
  261. return -EINVAL;
  262. case pmac_low_i2c_mode_std:
  263. mode_reg |= KW_I2C_MODE_STANDARD;
  264. break;
  265. case pmac_low_i2c_mode_stdsub:
  266. mode_reg |= KW_I2C_MODE_STANDARDSUB;
  267. kw_write_reg(reg_subaddr, subaddr);
  268. break;
  269. case pmac_low_i2c_mode_combined:
  270. mode_reg |= KW_I2C_MODE_COMBINED;
  271. kw_write_reg(reg_subaddr, subaddr);
  272. break;
  273. }
  274. /* Setup channel & clear pending irqs */
  275. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  276. kw_write_reg(reg_mode, mode_reg | (host->channel << 4));
  277. kw_write_reg(reg_status, 0);
  278. /* Set up address and r/w bit */
  279. kw_write_reg(reg_addr, addr);
  280. /* Start sending address & disable interrupt*/
  281. kw_write_reg(reg_ier, 0 /*KW_I2C_IRQ_MASK*/);
  282. kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
  283. /* State machine, to turn into an interrupt handler */
  284. while(state != state_idle) {
  285. u8 isr = kw_wait_interrupt(host);
  286. state = kw_handle_interrupt(host, state, addr & 1, &rc, &data, &len, isr);
  287. }
  288. return rc;
  289. }
  290. static void keywest_low_i2c_add(struct device_node *np)
  291. {
  292. struct low_i2c_host *host = find_low_i2c_host(NULL);
  293. unsigned long *psteps, *prate, steps, aoffset = 0;
  294. struct device_node *parent;
  295. if (host == NULL) {
  296. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  297. np->full_name);
  298. return;
  299. }
  300. memset(host, 0, sizeof(*host));
  301. init_MUTEX(&host->mutex);
  302. host->np = of_node_get(np);
  303. psteps = (unsigned long *)get_property(np, "AAPL,address-step", NULL);
  304. steps = psteps ? (*psteps) : 0x10;
  305. for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
  306. steps >>= 1;
  307. parent = of_get_parent(np);
  308. host->num_channels = 1;
  309. if (parent && parent->name[0] == 'u') {
  310. host->num_channels = 2;
  311. aoffset = 3;
  312. }
  313. /* Select interface rate */
  314. host->speed = KW_I2C_MODE_100KHZ;
  315. prate = (unsigned long *)get_property(np, "AAPL,i2c-rate", NULL);
  316. if (prate) switch(*prate) {
  317. case 100:
  318. host->speed = KW_I2C_MODE_100KHZ;
  319. break;
  320. case 50:
  321. host->speed = KW_I2C_MODE_50KHZ;
  322. break;
  323. case 25:
  324. host->speed = KW_I2C_MODE_25KHZ;
  325. break;
  326. }
  327. host->mode = pmac_low_i2c_mode_std;
  328. host->base = ioremap(np->addrs[0].address + aoffset,
  329. np->addrs[0].size);
  330. host->func = keywest_low_i2c_func;
  331. }
  332. /*
  333. *
  334. * PMU implementation
  335. *
  336. */
  337. #ifdef CONFIG_ADB_PMU
  338. static int pmu_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len)
  339. {
  340. // TODO
  341. return -ENODEV;
  342. }
  343. static void pmu_low_i2c_add(struct device_node *np)
  344. {
  345. struct low_i2c_host *host = find_low_i2c_host(NULL);
  346. if (host == NULL) {
  347. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  348. np->full_name);
  349. return;
  350. }
  351. memset(host, 0, sizeof(*host));
  352. init_MUTEX(&host->mutex);
  353. host->np = of_node_get(np);
  354. host->num_channels = 3;
  355. host->mode = pmac_low_i2c_mode_std;
  356. host->func = pmu_low_i2c_func;
  357. }
  358. #endif /* CONFIG_ADB_PMU */
  359. void __init pmac_init_low_i2c(void)
  360. {
  361. struct device_node *np;
  362. /* Probe keywest-i2c busses */
  363. np = of_find_compatible_node(NULL, "i2c", "keywest-i2c");
  364. while(np) {
  365. keywest_low_i2c_add(np);
  366. np = of_find_compatible_node(np, "i2c", "keywest-i2c");
  367. }
  368. #ifdef CONFIG_ADB_PMU
  369. /* Probe PMU busses */
  370. np = of_find_node_by_name(NULL, "via-pmu");
  371. if (np)
  372. pmu_low_i2c_add(np);
  373. #endif /* CONFIG_ADB_PMU */
  374. /* TODO: Add CUDA support as well */
  375. }
  376. int pmac_low_i2c_lock(struct device_node *np)
  377. {
  378. struct low_i2c_host *host = find_low_i2c_host(np);
  379. if (!host)
  380. return -ENODEV;
  381. down(&host->mutex);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(pmac_low_i2c_lock);
  385. int pmac_low_i2c_unlock(struct device_node *np)
  386. {
  387. struct low_i2c_host *host = find_low_i2c_host(np);
  388. if (!host)
  389. return -ENODEV;
  390. up(&host->mutex);
  391. return 0;
  392. }
  393. EXPORT_SYMBOL(pmac_low_i2c_unlock);
  394. int pmac_low_i2c_open(struct device_node *np, int channel)
  395. {
  396. struct low_i2c_host *host = find_low_i2c_host(np);
  397. if (!host)
  398. return -ENODEV;
  399. if (channel >= host->num_channels)
  400. return -EINVAL;
  401. down(&host->mutex);
  402. host->is_open = 1;
  403. host->channel = channel;
  404. return 0;
  405. }
  406. EXPORT_SYMBOL(pmac_low_i2c_open);
  407. int pmac_low_i2c_close(struct device_node *np)
  408. {
  409. struct low_i2c_host *host = find_low_i2c_host(np);
  410. if (!host)
  411. return -ENODEV;
  412. host->is_open = 0;
  413. up(&host->mutex);
  414. return 0;
  415. }
  416. EXPORT_SYMBOL(pmac_low_i2c_close);
  417. int pmac_low_i2c_setmode(struct device_node *np, int mode)
  418. {
  419. struct low_i2c_host *host = find_low_i2c_host(np);
  420. if (!host)
  421. return -ENODEV;
  422. WARN_ON(!host->is_open);
  423. host->mode = mode;
  424. return 0;
  425. }
  426. EXPORT_SYMBOL(pmac_low_i2c_setmode);
  427. int pmac_low_i2c_xfer(struct device_node *np, u8 addrdir, u8 subaddr, u8 *data, int len)
  428. {
  429. struct low_i2c_host *host = find_low_i2c_host(np);
  430. if (!host)
  431. return -ENODEV;
  432. WARN_ON(!host->is_open);
  433. return host->func(host, addrdir, subaddr, data, len);
  434. }
  435. EXPORT_SYMBOL(pmac_low_i2c_xfer);