i2c-xiic.c 23 KB

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