i2c-xiic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * i2c-xiic.c
  3. * Copyright (c) 2002-2007 Xilinx Inc.
  4. * Copyright (c) 2009-2010 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. *
  20. * This code was implemented by Mocean Laboratories AB when porting linux
  21. * to the automotive development board Russellville. The copyright holder
  22. * as seen in the header is Intel corporation.
  23. * Mocean Laboratories forked off the GNU/Linux platform work into a
  24. * separate company called Pelagicore AB, which commited the code to the
  25. * kernel.
  26. */
  27. /* Supports:
  28. * Xilinx IIC
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/errno.h>
  34. #include <linux/delay.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/mfd/core.h>
  37. #include <linux/i2c.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/wait.h>
  40. #include <linux/i2c-xiic.h>
  41. #include <linux/io.h>
  42. #include <linux/slab.h>
  43. #define DRIVER_NAME "xiic-i2c"
  44. enum xilinx_i2c_state {
  45. STATE_DONE,
  46. STATE_ERROR,
  47. STATE_START
  48. };
  49. /**
  50. * struct xiic_i2c - Internal representation of the XIIC I2C bus
  51. * @base: Memory base of the HW registers
  52. * @wait: Wait queue for callers
  53. * @adap: Kernel adapter representation
  54. * @tx_msg: Messages from above to be sent
  55. * @lock: Mutual exclusion
  56. * @tx_pos: Current pos in TX message
  57. * @nmsgs: Number of messages in tx_msg
  58. * @state: See STATE_
  59. * @rx_msg: Current RX message
  60. * @rx_pos: Position within current RX message
  61. */
  62. struct xiic_i2c {
  63. void __iomem *base;
  64. wait_queue_head_t wait;
  65. struct i2c_adapter adap;
  66. struct i2c_msg *tx_msg;
  67. spinlock_t lock;
  68. unsigned int tx_pos;
  69. unsigned int nmsgs;
  70. enum xilinx_i2c_state state;
  71. struct i2c_msg *rx_msg;
  72. int rx_pos;
  73. };
  74. #define XIIC_MSB_OFFSET 0
  75. #define XIIC_REG_OFFSET (0x100+XIIC_MSB_OFFSET)
  76. /*
  77. * Register offsets in bytes from RegisterBase. Three is added to the
  78. * base offset to access LSB (IBM style) of the word
  79. */
  80. #define XIIC_CR_REG_OFFSET (0x00+XIIC_REG_OFFSET) /* Control Register */
  81. #define XIIC_SR_REG_OFFSET (0x04+XIIC_REG_OFFSET) /* Status Register */
  82. #define XIIC_DTR_REG_OFFSET (0x08+XIIC_REG_OFFSET) /* Data Tx Register */
  83. #define XIIC_DRR_REG_OFFSET (0x0C+XIIC_REG_OFFSET) /* Data Rx Register */
  84. #define XIIC_ADR_REG_OFFSET (0x10+XIIC_REG_OFFSET) /* Address Register */
  85. #define XIIC_TFO_REG_OFFSET (0x14+XIIC_REG_OFFSET) /* Tx FIFO Occupancy */
  86. #define XIIC_RFO_REG_OFFSET (0x18+XIIC_REG_OFFSET) /* Rx FIFO Occupancy */
  87. #define XIIC_TBA_REG_OFFSET (0x1C+XIIC_REG_OFFSET) /* 10 Bit Address reg */
  88. #define XIIC_RFD_REG_OFFSET (0x20+XIIC_REG_OFFSET) /* Rx FIFO Depth reg */
  89. #define XIIC_GPO_REG_OFFSET (0x24+XIIC_REG_OFFSET) /* Output Register */
  90. /* Control Register masks */
  91. #define XIIC_CR_ENABLE_DEVICE_MASK 0x01 /* Device enable = 1 */
  92. #define XIIC_CR_TX_FIFO_RESET_MASK 0x02 /* Transmit FIFO reset=1 */
  93. #define XIIC_CR_MSMS_MASK 0x04 /* Master starts Txing=1 */
  94. #define XIIC_CR_DIR_IS_TX_MASK 0x08 /* Dir of tx. Txing=1 */
  95. #define XIIC_CR_NO_ACK_MASK 0x10 /* Tx Ack. NO ack = 1 */
  96. #define XIIC_CR_REPEATED_START_MASK 0x20 /* Repeated start = 1 */
  97. #define XIIC_CR_GENERAL_CALL_MASK 0x40 /* Gen Call enabled = 1 */
  98. /* Status Register masks */
  99. #define XIIC_SR_GEN_CALL_MASK 0x01 /* 1=a mstr issued a GC */
  100. #define XIIC_SR_ADDR_AS_SLAVE_MASK 0x02 /* 1=when addr as slave */
  101. #define XIIC_SR_BUS_BUSY_MASK 0x04 /* 1 = bus is busy */
  102. #define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08 /* 1=Dir: mstr <-- slave */
  103. #define XIIC_SR_TX_FIFO_FULL_MASK 0x10 /* 1 = Tx FIFO full */
  104. #define XIIC_SR_RX_FIFO_FULL_MASK 0x20 /* 1 = Rx FIFO full */
  105. #define XIIC_SR_RX_FIFO_EMPTY_MASK 0x40 /* 1 = Rx FIFO empty */
  106. #define XIIC_SR_TX_FIFO_EMPTY_MASK 0x80 /* 1 = Tx FIFO empty */
  107. /* Interrupt Status Register masks Interrupt occurs when... */
  108. #define XIIC_INTR_ARB_LOST_MASK 0x01 /* 1 = arbitration lost */
  109. #define XIIC_INTR_TX_ERROR_MASK 0x02 /* 1=Tx error/msg complete */
  110. #define XIIC_INTR_TX_EMPTY_MASK 0x04 /* 1 = Tx FIFO/reg empty */
  111. #define XIIC_INTR_RX_FULL_MASK 0x08 /* 1=Rx FIFO/reg=OCY level */
  112. #define XIIC_INTR_BNB_MASK 0x10 /* 1 = Bus not busy */
  113. #define XIIC_INTR_AAS_MASK 0x20 /* 1 = when addr as slave */
  114. #define XIIC_INTR_NAAS_MASK 0x40 /* 1 = not addr as slave */
  115. #define XIIC_INTR_TX_HALF_MASK 0x80 /* 1 = TX FIFO half empty */
  116. /* The following constants specify the depth of the FIFOs */
  117. #define IIC_RX_FIFO_DEPTH 16 /* Rx fifo capacity */
  118. #define IIC_TX_FIFO_DEPTH 16 /* Tx fifo capacity */
  119. /* The following constants specify groups of interrupts that are typically
  120. * enabled or disables at the same time
  121. */
  122. #define XIIC_TX_INTERRUPTS \
  123. (XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)
  124. #define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)
  125. /* The following constants are used with the following macros to specify the
  126. * operation, a read or write operation.
  127. */
  128. #define XIIC_READ_OPERATION 1
  129. #define XIIC_WRITE_OPERATION 0
  130. /*
  131. * Tx Fifo upper bit masks.
  132. */
  133. #define XIIC_TX_DYN_START_MASK 0x0100 /* 1 = Set dynamic start */
  134. #define XIIC_TX_DYN_STOP_MASK 0x0200 /* 1 = Set dynamic stop */
  135. /*
  136. * The following constants define the register offsets for the Interrupt
  137. * registers. There are some holes in the memory map for reserved addresses
  138. * to allow other registers to be added and still match the memory map of the
  139. * interrupt controller registers
  140. */
  141. #define XIIC_DGIER_OFFSET 0x1C /* Device Global Interrupt Enable Register */
  142. #define XIIC_IISR_OFFSET 0x20 /* Interrupt Status Register */
  143. #define XIIC_IIER_OFFSET 0x28 /* Interrupt Enable Register */
  144. #define XIIC_RESETR_OFFSET 0x40 /* Reset Register */
  145. #define XIIC_RESET_MASK 0xAUL
  146. /*
  147. * The following constant is used for the device global interrupt enable
  148. * register, to enable all interrupts for the device, this is the only bit
  149. * in the register
  150. */
  151. #define XIIC_GINTR_ENABLE_MASK 0x80000000UL
  152. #define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
  153. #define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
  154. static void xiic_start_xfer(struct xiic_i2c *i2c);
  155. static void __xiic_start_xfer(struct xiic_i2c *i2c);
  156. static inline void xiic_setreg8(struct xiic_i2c *i2c, int reg, u8 value)
  157. {
  158. iowrite8(value, i2c->base + reg);
  159. }
  160. static inline u8 xiic_getreg8(struct xiic_i2c *i2c, int reg)
  161. {
  162. return ioread8(i2c->base + reg);
  163. }
  164. static inline void xiic_setreg16(struct xiic_i2c *i2c, int reg, u16 value)
  165. {
  166. iowrite16(value, i2c->base + reg);
  167. }
  168. static inline void xiic_setreg32(struct xiic_i2c *i2c, int reg, int value)
  169. {
  170. iowrite32(value, i2c->base + reg);
  171. }
  172. static inline int xiic_getreg32(struct xiic_i2c *i2c, int reg)
  173. {
  174. return ioread32(i2c->base + reg);
  175. }
  176. static inline void xiic_irq_dis(struct xiic_i2c *i2c, u32 mask)
  177. {
  178. u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  179. xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier & ~mask);
  180. }
  181. static inline void xiic_irq_en(struct xiic_i2c *i2c, u32 mask)
  182. {
  183. u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  184. xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier | mask);
  185. }
  186. static inline void xiic_irq_clr(struct xiic_i2c *i2c, u32 mask)
  187. {
  188. u32 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
  189. xiic_setreg32(i2c, XIIC_IISR_OFFSET, isr & mask);
  190. }
  191. static inline void xiic_irq_clr_en(struct xiic_i2c *i2c, u32 mask)
  192. {
  193. xiic_irq_clr(i2c, mask);
  194. xiic_irq_en(i2c, mask);
  195. }
  196. static void xiic_clear_rx_fifo(struct xiic_i2c *i2c)
  197. {
  198. u8 sr;
  199. for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
  200. !(sr & XIIC_SR_RX_FIFO_EMPTY_MASK);
  201. sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET))
  202. xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
  203. }
  204. static void xiic_reinit(struct xiic_i2c *i2c)
  205. {
  206. xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
  207. /* Set receive Fifo depth to maximum (zero based). */
  208. xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1);
  209. /* Reset Tx Fifo. */
  210. xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
  211. /* Enable IIC Device, remove Tx Fifo reset & disable general call. */
  212. xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK);
  213. /* make sure RX fifo is empty */
  214. xiic_clear_rx_fifo(i2c);
  215. /* Enable interrupts */
  216. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
  217. xiic_irq_clr_en(i2c, XIIC_INTR_AAS_MASK | XIIC_INTR_ARB_LOST_MASK);
  218. }
  219. static void xiic_deinit(struct xiic_i2c *i2c)
  220. {
  221. u8 cr;
  222. xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
  223. /* Disable IIC Device. */
  224. cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
  225. xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr & ~XIIC_CR_ENABLE_DEVICE_MASK);
  226. }
  227. static void xiic_read_rx(struct xiic_i2c *i2c)
  228. {
  229. u8 bytes_in_fifo;
  230. int i;
  231. bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;
  232. dev_dbg(i2c->adap.dev.parent, "%s entry, bytes in fifo: %d, msg: %d"
  233. ", SR: 0x%x, CR: 0x%x\n",
  234. __func__, bytes_in_fifo, xiic_rx_space(i2c),
  235. xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
  236. xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
  237. if (bytes_in_fifo > xiic_rx_space(i2c))
  238. bytes_in_fifo = xiic_rx_space(i2c);
  239. for (i = 0; i < bytes_in_fifo; i++)
  240. i2c->rx_msg->buf[i2c->rx_pos++] =
  241. xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
  242. xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET,
  243. (xiic_rx_space(i2c) > IIC_RX_FIFO_DEPTH) ?
  244. IIC_RX_FIFO_DEPTH - 1 : xiic_rx_space(i2c) - 1);
  245. }
  246. static int xiic_tx_fifo_space(struct xiic_i2c *i2c)
  247. {
  248. /* return the actual space left in the FIFO */
  249. return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1;
  250. }
  251. static void xiic_fill_tx_fifo(struct xiic_i2c *i2c)
  252. {
  253. u8 fifo_space = xiic_tx_fifo_space(i2c);
  254. int len = xiic_tx_space(i2c);
  255. len = (len > fifo_space) ? fifo_space : len;
  256. dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n",
  257. __func__, len, fifo_space);
  258. while (len--) {
  259. u16 data = i2c->tx_msg->buf[i2c->tx_pos++];
  260. if ((xiic_tx_space(i2c) == 0) && (i2c->nmsgs == 1)) {
  261. /* last message in transfer -> STOP */
  262. data |= XIIC_TX_DYN_STOP_MASK;
  263. dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__);
  264. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
  265. } else
  266. xiic_setreg8(i2c, XIIC_DTR_REG_OFFSET, data);
  267. }
  268. }
  269. static void xiic_wakeup(struct xiic_i2c *i2c, int code)
  270. {
  271. i2c->tx_msg = NULL;
  272. i2c->rx_msg = NULL;
  273. i2c->nmsgs = 0;
  274. i2c->state = code;
  275. wake_up(&i2c->wait);
  276. }
  277. static void xiic_process(struct xiic_i2c *i2c)
  278. {
  279. u32 pend, isr, ier;
  280. u32 clr = 0;
  281. /* Get the interrupt Status from the IPIF. There is no clearing of
  282. * interrupts in the IPIF. Interrupts must be cleared at the source.
  283. * To find which interrupts are pending; AND interrupts pending with
  284. * interrupts masked.
  285. */
  286. isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
  287. ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  288. pend = isr & ier;
  289. dev_dbg(i2c->adap.dev.parent, "%s entry, IER: 0x%x, ISR: 0x%x, "
  290. "pend: 0x%x, SR: 0x%x, msg: %p, nmsgs: %d\n",
  291. __func__, ier, isr, pend, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
  292. i2c->tx_msg, i2c->nmsgs);
  293. /* Do not processes a devices interrupts if the device has no
  294. * interrupts pending
  295. */
  296. if (!pend)
  297. return;
  298. /* Service requesting interrupt */
  299. if ((pend & XIIC_INTR_ARB_LOST_MASK) ||
  300. ((pend & XIIC_INTR_TX_ERROR_MASK) &&
  301. !(pend & XIIC_INTR_RX_FULL_MASK))) {
  302. /* bus arbritration lost, or...
  303. * Transmit error _OR_ RX completed
  304. * if this happens when RX_FULL is not set
  305. * this is probably a TX error
  306. */
  307. dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);
  308. /* dynamic mode seem to suffer from problems if we just flushes
  309. * fifos and the next message is a TX with len 0 (only addr)
  310. * reset the IP instead of just flush fifos
  311. */
  312. xiic_reinit(i2c);
  313. if (i2c->tx_msg)
  314. xiic_wakeup(i2c, STATE_ERROR);
  315. } else if (pend & XIIC_INTR_RX_FULL_MASK) {
  316. /* Receive register/FIFO is full */
  317. clr = XIIC_INTR_RX_FULL_MASK;
  318. if (!i2c->rx_msg) {
  319. dev_dbg(i2c->adap.dev.parent,
  320. "%s unexpexted RX IRQ\n", __func__);
  321. xiic_clear_rx_fifo(i2c);
  322. goto out;
  323. }
  324. xiic_read_rx(i2c);
  325. if (xiic_rx_space(i2c) == 0) {
  326. /* this is the last part of the message */
  327. i2c->rx_msg = NULL;
  328. /* also clear TX error if there (RX complete) */
  329. clr |= (isr & XIIC_INTR_TX_ERROR_MASK);
  330. dev_dbg(i2c->adap.dev.parent,
  331. "%s end of message, nmsgs: %d\n",
  332. __func__, i2c->nmsgs);
  333. /* send next message if this wasn't the last,
  334. * otherwise the transfer will be finialise when
  335. * receiving the bus not busy interrupt
  336. */
  337. if (i2c->nmsgs > 1) {
  338. i2c->nmsgs--;
  339. i2c->tx_msg++;
  340. dev_dbg(i2c->adap.dev.parent,
  341. "%s will start next...\n", __func__);
  342. __xiic_start_xfer(i2c);
  343. }
  344. }
  345. } else if (pend & XIIC_INTR_BNB_MASK) {
  346. /* IIC bus has transitioned to not busy */
  347. clr = XIIC_INTR_BNB_MASK;
  348. /* The bus is not busy, disable BusNotBusy interrupt */
  349. xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
  350. if (!i2c->tx_msg)
  351. goto out;
  352. if ((i2c->nmsgs == 1) && !i2c->rx_msg &&
  353. xiic_tx_space(i2c) == 0)
  354. xiic_wakeup(i2c, STATE_DONE);
  355. else
  356. xiic_wakeup(i2c, STATE_ERROR);
  357. } else if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {
  358. /* Transmit register/FIFO is empty or ½ empty */
  359. clr = pend &
  360. (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK);
  361. if (!i2c->tx_msg) {
  362. dev_dbg(i2c->adap.dev.parent,
  363. "%s unexpexted TX IRQ\n", __func__);
  364. goto out;
  365. }
  366. xiic_fill_tx_fifo(i2c);
  367. /* current message sent and there is space in the fifo */
  368. if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {
  369. dev_dbg(i2c->adap.dev.parent,
  370. "%s end of message sent, nmsgs: %d\n",
  371. __func__, i2c->nmsgs);
  372. if (i2c->nmsgs > 1) {
  373. i2c->nmsgs--;
  374. i2c->tx_msg++;
  375. __xiic_start_xfer(i2c);
  376. } else {
  377. xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
  378. dev_dbg(i2c->adap.dev.parent,
  379. "%s Got TX IRQ but no more to do...\n",
  380. __func__);
  381. }
  382. } else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))
  383. /* current frame is sent and is last,
  384. * make sure to disable tx half
  385. */
  386. xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
  387. } else {
  388. /* got IRQ which is not acked */
  389. dev_err(i2c->adap.dev.parent, "%s Got unexpected IRQ\n",
  390. __func__);
  391. clr = pend;
  392. }
  393. out:
  394. dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);
  395. xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);
  396. }
  397. static int xiic_bus_busy(struct xiic_i2c *i2c)
  398. {
  399. u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
  400. return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
  401. }
  402. static int xiic_busy(struct xiic_i2c *i2c)
  403. {
  404. int tries = 3;
  405. int err;
  406. if (i2c->tx_msg)
  407. return -EBUSY;
  408. /* for instance if previous transfer was terminated due to TX error
  409. * it might be that the bus is on it's way to become available
  410. * give it at most 3 ms to wake
  411. */
  412. err = xiic_bus_busy(i2c);
  413. while (err && tries--) {
  414. mdelay(1);
  415. err = xiic_bus_busy(i2c);
  416. }
  417. return err;
  418. }
  419. static void xiic_start_recv(struct xiic_i2c *i2c)
  420. {
  421. u8 rx_watermark;
  422. struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;
  423. /* Clear and enable Rx full interrupt. */
  424. xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK);
  425. /* we want to get all but last byte, because the TX_ERROR IRQ is used
  426. * to inidicate error ACK on the address, and negative ack on the last
  427. * received byte, so to not mix them receive all but last.
  428. * In the case where there is only one byte to receive
  429. * we can check if ERROR and RX full is set at the same time
  430. */
  431. rx_watermark = msg->len;
  432. if (rx_watermark > IIC_RX_FIFO_DEPTH)
  433. rx_watermark = IIC_RX_FIFO_DEPTH;
  434. xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rx_watermark - 1);
  435. if (!(msg->flags & I2C_M_NOSTART))
  436. /* write the address */
  437. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
  438. (msg->addr << 1) | XIIC_READ_OPERATION |
  439. XIIC_TX_DYN_START_MASK);
  440. xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
  441. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
  442. msg->len | ((i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0));
  443. if (i2c->nmsgs == 1)
  444. /* very last, enable bus not busy as well */
  445. xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
  446. /* the message is tx:ed */
  447. i2c->tx_pos = msg->len;
  448. }
  449. static void xiic_start_send(struct xiic_i2c *i2c)
  450. {
  451. struct i2c_msg *msg = i2c->tx_msg;
  452. xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);
  453. dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d, "
  454. "ISR: 0x%x, CR: 0x%x\n",
  455. __func__, msg, msg->len, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
  456. xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
  457. if (!(msg->flags & I2C_M_NOSTART)) {
  458. /* write the address */
  459. u16 data = ((msg->addr << 1) & 0xfe) | XIIC_WRITE_OPERATION |
  460. XIIC_TX_DYN_START_MASK;
  461. if ((i2c->nmsgs == 1) && msg->len == 0)
  462. /* no data and last message -> add STOP */
  463. data |= XIIC_TX_DYN_STOP_MASK;
  464. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
  465. }
  466. xiic_fill_tx_fifo(i2c);
  467. /* Clear any pending Tx empty, Tx Error and then enable them. */
  468. xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK |
  469. XIIC_INTR_BNB_MASK);
  470. }
  471. static irqreturn_t xiic_isr(int irq, void *dev_id)
  472. {
  473. struct xiic_i2c *i2c = dev_id;
  474. spin_lock(&i2c->lock);
  475. /* disable interrupts globally */
  476. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, 0);
  477. dev_dbg(i2c->adap.dev.parent, "%s entry\n", __func__);
  478. xiic_process(i2c);
  479. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
  480. spin_unlock(&i2c->lock);
  481. return IRQ_HANDLED;
  482. }
  483. static void __xiic_start_xfer(struct xiic_i2c *i2c)
  484. {
  485. int first = 1;
  486. int fifo_space = xiic_tx_fifo_space(i2c);
  487. dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",
  488. __func__, i2c->tx_msg, fifo_space);
  489. if (!i2c->tx_msg)
  490. return;
  491. i2c->rx_pos = 0;
  492. i2c->tx_pos = 0;
  493. i2c->state = STATE_START;
  494. while ((fifo_space >= 2) && (first || (i2c->nmsgs > 1))) {
  495. if (!first) {
  496. i2c->nmsgs--;
  497. i2c->tx_msg++;
  498. i2c->tx_pos = 0;
  499. } else
  500. first = 0;
  501. if (i2c->tx_msg->flags & I2C_M_RD) {
  502. /* we dont date putting several reads in the FIFO */
  503. xiic_start_recv(i2c);
  504. return;
  505. } else {
  506. xiic_start_send(i2c);
  507. if (xiic_tx_space(i2c) != 0) {
  508. /* the message could not be completely sent */
  509. break;
  510. }
  511. }
  512. fifo_space = xiic_tx_fifo_space(i2c);
  513. }
  514. /* there are more messages or the current one could not be completely
  515. * put into the FIFO, also enable the half empty interrupt
  516. */
  517. if (i2c->nmsgs > 1 || xiic_tx_space(i2c))
  518. xiic_irq_clr_en(i2c, XIIC_INTR_TX_HALF_MASK);
  519. }
  520. static void xiic_start_xfer(struct xiic_i2c *i2c)
  521. {
  522. unsigned long flags;
  523. spin_lock_irqsave(&i2c->lock, flags);
  524. xiic_reinit(i2c);
  525. /* disable interrupts globally */
  526. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, 0);
  527. spin_unlock_irqrestore(&i2c->lock, flags);
  528. __xiic_start_xfer(i2c);
  529. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
  530. }
  531. static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  532. {
  533. struct xiic_i2c *i2c = i2c_get_adapdata(adap);
  534. int err;
  535. dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
  536. xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
  537. err = xiic_busy(i2c);
  538. if (err)
  539. return err;
  540. i2c->tx_msg = msgs;
  541. i2c->nmsgs = num;
  542. xiic_start_xfer(i2c);
  543. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  544. (i2c->state == STATE_DONE), HZ))
  545. return (i2c->state == STATE_DONE) ? num : -EIO;
  546. else {
  547. i2c->tx_msg = NULL;
  548. i2c->rx_msg = NULL;
  549. i2c->nmsgs = 0;
  550. return -ETIMEDOUT;
  551. }
  552. }
  553. static u32 xiic_func(struct i2c_adapter *adap)
  554. {
  555. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  556. }
  557. static const struct i2c_algorithm xiic_algorithm = {
  558. .master_xfer = xiic_xfer,
  559. .functionality = xiic_func,
  560. };
  561. static struct i2c_adapter xiic_adapter = {
  562. .owner = THIS_MODULE,
  563. .name = DRIVER_NAME,
  564. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  565. .algo = &xiic_algorithm,
  566. };
  567. static int __devinit xiic_i2c_probe(struct platform_device *pdev)
  568. {
  569. struct xiic_i2c *i2c;
  570. struct xiic_i2c_platform_data *pdata;
  571. struct resource *res;
  572. int ret, irq;
  573. u8 i;
  574. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  575. if (!res)
  576. goto resource_missing;
  577. irq = platform_get_irq(pdev, 0);
  578. if (irq < 0)
  579. goto resource_missing;
  580. pdata = mfd_get_data(pdev);
  581. if (!pdata)
  582. return -EINVAL;
  583. i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
  584. if (!i2c)
  585. return -ENOMEM;
  586. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  587. dev_err(&pdev->dev, "Memory region busy\n");
  588. ret = -EBUSY;
  589. goto request_mem_failed;
  590. }
  591. i2c->base = ioremap(res->start, resource_size(res));
  592. if (!i2c->base) {
  593. dev_err(&pdev->dev, "Unable to map registers\n");
  594. ret = -EIO;
  595. goto map_failed;
  596. }
  597. /* hook up driver to tree */
  598. platform_set_drvdata(pdev, i2c);
  599. i2c->adap = xiic_adapter;
  600. i2c_set_adapdata(&i2c->adap, i2c);
  601. i2c->adap.dev.parent = &pdev->dev;
  602. xiic_reinit(i2c);
  603. spin_lock_init(&i2c->lock);
  604. init_waitqueue_head(&i2c->wait);
  605. ret = request_irq(irq, xiic_isr, 0, pdev->name, i2c);
  606. if (ret) {
  607. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  608. goto request_irq_failed;
  609. }
  610. /* add i2c adapter to i2c tree */
  611. ret = i2c_add_adapter(&i2c->adap);
  612. if (ret) {
  613. dev_err(&pdev->dev, "Failed to add adapter\n");
  614. goto add_adapter_failed;
  615. }
  616. /* add in known devices to the bus */
  617. for (i = 0; i < pdata->num_devices; i++)
  618. i2c_new_device(&i2c->adap, pdata->devices + i);
  619. return 0;
  620. add_adapter_failed:
  621. free_irq(irq, i2c);
  622. request_irq_failed:
  623. xiic_deinit(i2c);
  624. iounmap(i2c->base);
  625. map_failed:
  626. release_mem_region(res->start, resource_size(res));
  627. request_mem_failed:
  628. kfree(i2c);
  629. return ret;
  630. resource_missing:
  631. dev_err(&pdev->dev, "IRQ or Memory resource is missing\n");
  632. return -ENOENT;
  633. }
  634. static int __devexit xiic_i2c_remove(struct platform_device* pdev)
  635. {
  636. struct xiic_i2c *i2c = platform_get_drvdata(pdev);
  637. struct resource *res;
  638. /* remove adapter & data */
  639. i2c_del_adapter(&i2c->adap);
  640. xiic_deinit(i2c);
  641. platform_set_drvdata(pdev, NULL);
  642. free_irq(platform_get_irq(pdev, 0), i2c);
  643. iounmap(i2c->base);
  644. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  645. if (res)
  646. release_mem_region(res->start, resource_size(res));
  647. kfree(i2c);
  648. return 0;
  649. }
  650. /* work with hotplug and coldplug */
  651. MODULE_ALIAS("platform:"DRIVER_NAME);
  652. static struct platform_driver xiic_i2c_driver = {
  653. .probe = xiic_i2c_probe,
  654. .remove = __devexit_p(xiic_i2c_remove),
  655. .driver = {
  656. .owner = THIS_MODULE,
  657. .name = DRIVER_NAME,
  658. },
  659. };
  660. static int __init xiic_i2c_init(void)
  661. {
  662. return platform_driver_register(&xiic_i2c_driver);
  663. }
  664. static void __exit xiic_i2c_exit(void)
  665. {
  666. platform_driver_unregister(&xiic_i2c_driver);
  667. }
  668. module_init(xiic_i2c_init);
  669. module_exit(xiic_i2c_exit);
  670. MODULE_AUTHOR("info@mocean-labs.com");
  671. MODULE_DESCRIPTION("Xilinx I2C bus driver");
  672. MODULE_LICENSE("GPL v2");