i2c-xiic.c 23 KB

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