i2c-pxa.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * i2c_adap_pxa.c
  4. *
  5. * I2C adapter for the PXA I2C bus access.
  6. *
  7. * Copyright (C) 2002 Intrinsyc Software Inc.
  8. * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
  9. *
  10. * History:
  11. * Apr 2002: Initial version [CS]
  12. * Jun 2002: Properly separated algo/adap [FB]
  13. * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
  14. * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
  15. * Sep 2004: Major rework to ensure efficient bus handling [RMK]
  16. * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
  17. * Feb 2005: Rework slave mode handling [RMK]
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/errno.h>
  23. #include <linux/gpio/consumer.h>
  24. #include <linux/i2c.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <linux/pinctrl/consumer.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/platform_data/i2c-pxa.h>
  35. #include <linux/property.h>
  36. #include <linux/slab.h>
  37. /* I2C register field definitions */
  38. #define IBMR_SDAS (1 << 0)
  39. #define IBMR_SCLS (1 << 1)
  40. #define ICR_START (1 << 0) /* start bit */
  41. #define ICR_STOP (1 << 1) /* stop bit */
  42. #define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */
  43. #define ICR_TB (1 << 3) /* transfer byte bit */
  44. #define ICR_MA (1 << 4) /* master abort */
  45. #define ICR_SCLE (1 << 5) /* master clock enable */
  46. #define ICR_IUE (1 << 6) /* unit enable */
  47. #define ICR_GCD (1 << 7) /* general call disable */
  48. #define ICR_ITEIE (1 << 8) /* enable tx interrupts */
  49. #define ICR_IRFIE (1 << 9) /* enable rx interrupts */
  50. #define ICR_BEIE (1 << 10) /* enable bus error ints */
  51. #define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */
  52. #define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */
  53. #define ICR_SADIE (1 << 13) /* slave address detected int enable */
  54. #define ICR_UR (1 << 14) /* unit reset */
  55. #define ICR_FM (1 << 15) /* fast mode */
  56. #define ICR_HS (1 << 16) /* High Speed mode */
  57. #define ICR_A3700_FM (1 << 16) /* fast mode for armada-3700 */
  58. #define ICR_A3700_HS (1 << 17) /* high speed mode for armada-3700 */
  59. #define ICR_GPIOEN (1 << 19) /* enable GPIO mode for SCL in HS */
  60. #define ISR_RWM (1 << 0) /* read/write mode */
  61. #define ISR_ACKNAK (1 << 1) /* ack/nak status */
  62. #define ISR_UB (1 << 2) /* unit busy */
  63. #define ISR_IBB (1 << 3) /* bus busy */
  64. #define ISR_SSD (1 << 4) /* slave stop detected */
  65. #define ISR_ALD (1 << 5) /* arbitration loss detected */
  66. #define ISR_ITE (1 << 6) /* tx buffer empty */
  67. #define ISR_IRF (1 << 7) /* rx buffer full */
  68. #define ISR_GCAD (1 << 8) /* general call address detected */
  69. #define ISR_SAD (1 << 9) /* slave address detected */
  70. #define ISR_BED (1 << 10) /* bus error no ACK/NAK */
  71. #define ILCR_SLV_SHIFT 0
  72. #define ILCR_SLV_MASK (0x1FF << ILCR_SLV_SHIFT)
  73. #define ILCR_FLV_SHIFT 9
  74. #define ILCR_FLV_MASK (0x1FF << ILCR_FLV_SHIFT)
  75. #define ILCR_HLVL_SHIFT 18
  76. #define ILCR_HLVL_MASK (0x1FF << ILCR_HLVL_SHIFT)
  77. #define ILCR_HLVH_SHIFT 27
  78. #define ILCR_HLVH_MASK (0x1F << ILCR_HLVH_SHIFT)
  79. #define IWCR_CNT_SHIFT 0
  80. #define IWCR_CNT_MASK (0x1F << IWCR_CNT_SHIFT)
  81. #define IWCR_HS_CNT1_SHIFT 5
  82. #define IWCR_HS_CNT1_MASK (0x1F << IWCR_HS_CNT1_SHIFT)
  83. #define IWCR_HS_CNT2_SHIFT 10
  84. #define IWCR_HS_CNT2_MASK (0x1F << IWCR_HS_CNT2_SHIFT)
  85. /* need a longer timeout if we're dealing with the fact we may well be
  86. * looking at a multi-master environment
  87. */
  88. #define DEF_TIMEOUT 32
  89. #define NO_SLAVE (-ENXIO)
  90. #define BUS_ERROR (-EREMOTEIO)
  91. #define XFER_NAKED (-ECONNREFUSED)
  92. #define I2C_RETRY (-2000) /* an error has occurred retry transmit */
  93. /* ICR initialize bit values
  94. *
  95. * 15 FM 0 (100 kHz operation)
  96. * 14 UR 0 (No unit reset)
  97. * 13 SADIE 0 (Disables the unit from interrupting on slave addresses
  98. * matching its slave address)
  99. * 12 ALDIE 0 (Disables the unit from interrupt when it loses arbitration
  100. * in master mode)
  101. * 11 SSDIE 0 (Disables interrupts from a slave stop detected, in slave mode)
  102. * 10 BEIE 1 (Enable interrupts from detected bus errors, no ACK sent)
  103. * 9 IRFIE 1 (Enable interrupts from full buffer received)
  104. * 8 ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty)
  105. * 7 GCD 1 (Disables i2c unit response to general call messages as a slave)
  106. * 6 IUE 0 (Disable unit until we change settings)
  107. * 5 SCLE 1 (Enables the i2c clock output for master mode (drives SCL)
  108. * 4 MA 0 (Only send stop with the ICR stop bit)
  109. * 3 TB 0 (We are not transmitting a byte initially)
  110. * 2 ACKNAK 0 (Send an ACK after the unit receives a byte)
  111. * 1 STOP 0 (Do not send a STOP)
  112. * 0 START 0 (Do not send a START)
  113. */
  114. #define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
  115. /* I2C status register init values
  116. *
  117. * 10 BED 1 (Clear bus error detected)
  118. * 9 SAD 1 (Clear slave address detected)
  119. * 7 IRF 1 (Clear IDBR Receive Full)
  120. * 6 ITE 1 (Clear IDBR Transmit Empty)
  121. * 5 ALD 1 (Clear Arbitration Loss Detected)
  122. * 4 SSD 1 (Clear Slave Stop Detected)
  123. */
  124. #define I2C_ISR_INIT 0x7FF /* status register init */
  125. struct pxa_reg_layout {
  126. u32 ibmr;
  127. u32 idbr;
  128. u32 icr;
  129. u32 isr;
  130. u32 isar;
  131. u32 ilcr;
  132. u32 iwcr;
  133. u32 fm;
  134. u32 hs;
  135. };
  136. enum pxa_i2c_types {
  137. REGS_PXA2XX,
  138. REGS_PXA3XX,
  139. REGS_CE4100,
  140. REGS_PXA910,
  141. REGS_A3700,
  142. };
  143. /* I2C register layout definitions */
  144. static struct pxa_reg_layout pxa_reg_layout[] = {
  145. [REGS_PXA2XX] = {
  146. .ibmr = 0x00,
  147. .idbr = 0x08,
  148. .icr = 0x10,
  149. .isr = 0x18,
  150. .isar = 0x20,
  151. .fm = ICR_FM,
  152. .hs = ICR_HS,
  153. },
  154. [REGS_PXA3XX] = {
  155. .ibmr = 0x00,
  156. .idbr = 0x04,
  157. .icr = 0x08,
  158. .isr = 0x0c,
  159. .isar = 0x10,
  160. .fm = ICR_FM,
  161. .hs = ICR_HS,
  162. },
  163. [REGS_CE4100] = {
  164. .ibmr = 0x14,
  165. .idbr = 0x0c,
  166. .icr = 0x00,
  167. .isr = 0x04,
  168. /* no isar register */
  169. .fm = ICR_FM,
  170. .hs = ICR_HS,
  171. },
  172. [REGS_PXA910] = {
  173. .ibmr = 0x00,
  174. .idbr = 0x08,
  175. .icr = 0x10,
  176. .isr = 0x18,
  177. .isar = 0x20,
  178. .ilcr = 0x28,
  179. .iwcr = 0x30,
  180. .fm = ICR_FM,
  181. .hs = ICR_HS,
  182. },
  183. [REGS_A3700] = {
  184. .ibmr = 0x00,
  185. .idbr = 0x04,
  186. .icr = 0x08,
  187. .isr = 0x0c,
  188. .isar = 0x10,
  189. .fm = ICR_A3700_FM,
  190. .hs = ICR_A3700_HS,
  191. },
  192. };
  193. static const struct of_device_id i2c_pxa_dt_ids[] = {
  194. { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
  195. { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
  196. { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA910 },
  197. { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 },
  198. {}
  199. };
  200. MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
  201. static const struct platform_device_id i2c_pxa_id_table[] = {
  202. { "pxa2xx-i2c", REGS_PXA2XX },
  203. { "pxa3xx-pwri2c", REGS_PXA3XX },
  204. { "ce4100-i2c", REGS_CE4100 },
  205. { "pxa910-i2c", REGS_PXA910 },
  206. { "armada-3700-i2c", REGS_A3700 },
  207. { }
  208. };
  209. MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);
  210. struct pxa_i2c {
  211. spinlock_t lock;
  212. wait_queue_head_t wait;
  213. struct i2c_msg *msg;
  214. unsigned int msg_num;
  215. unsigned int msg_idx;
  216. unsigned int msg_ptr;
  217. unsigned int slave_addr;
  218. unsigned int req_slave_addr;
  219. struct i2c_adapter adap;
  220. struct clk *clk;
  221. #ifdef CONFIG_I2C_PXA_SLAVE
  222. struct i2c_client *slave;
  223. #endif
  224. unsigned int irqlogidx;
  225. u32 isrlog[32];
  226. u32 icrlog[32];
  227. void __iomem *reg_base;
  228. void __iomem *reg_ibmr;
  229. void __iomem *reg_idbr;
  230. void __iomem *reg_icr;
  231. void __iomem *reg_isr;
  232. void __iomem *reg_isar;
  233. void __iomem *reg_ilcr;
  234. void __iomem *reg_iwcr;
  235. unsigned long iobase;
  236. unsigned long iosize;
  237. int irq;
  238. unsigned int use_pio :1;
  239. unsigned int fast_mode :1;
  240. unsigned int high_mode:1;
  241. unsigned char master_code;
  242. unsigned long rate;
  243. bool highmode_enter;
  244. u32 fm_mask;
  245. u32 hs_mask;
  246. struct i2c_bus_recovery_info recovery;
  247. struct pinctrl *pinctrl;
  248. struct pinctrl_state *pinctrl_default;
  249. struct pinctrl_state *pinctrl_recovery;
  250. };
  251. #define _IBMR(i2c) ((i2c)->reg_ibmr)
  252. #define _IDBR(i2c) ((i2c)->reg_idbr)
  253. #define _ICR(i2c) ((i2c)->reg_icr)
  254. #define _ISR(i2c) ((i2c)->reg_isr)
  255. #define _ISAR(i2c) ((i2c)->reg_isar)
  256. #define _ILCR(i2c) ((i2c)->reg_ilcr)
  257. #define _IWCR(i2c) ((i2c)->reg_iwcr)
  258. /*
  259. * I2C Slave mode address
  260. */
  261. #define I2C_PXA_SLAVE_ADDR 0x1
  262. #ifdef DEBUG
  263. struct bits {
  264. u32 mask;
  265. const char *set;
  266. const char *unset;
  267. };
  268. #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
  269. static inline void
  270. decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
  271. {
  272. printk("%s %08x:", prefix, val);
  273. while (num--) {
  274. const char *str = val & bits->mask ? bits->set : bits->unset;
  275. if (str)
  276. pr_cont(" %s", str);
  277. bits++;
  278. }
  279. pr_cont("\n");
  280. }
  281. static const struct bits isr_bits[] = {
  282. PXA_BIT(ISR_RWM, "RX", "TX"),
  283. PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
  284. PXA_BIT(ISR_UB, "Bsy", "Rdy"),
  285. PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
  286. PXA_BIT(ISR_SSD, "SlaveStop", NULL),
  287. PXA_BIT(ISR_ALD, "ALD", NULL),
  288. PXA_BIT(ISR_ITE, "TxEmpty", NULL),
  289. PXA_BIT(ISR_IRF, "RxFull", NULL),
  290. PXA_BIT(ISR_GCAD, "GenCall", NULL),
  291. PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
  292. PXA_BIT(ISR_BED, "BusErr", NULL),
  293. };
  294. static void decode_ISR(unsigned int val)
  295. {
  296. decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
  297. }
  298. #ifdef CONFIG_I2C_PXA_SLAVE
  299. static const struct bits icr_bits[] = {
  300. PXA_BIT(ICR_START, "START", NULL),
  301. PXA_BIT(ICR_STOP, "STOP", NULL),
  302. PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
  303. PXA_BIT(ICR_TB, "TB", NULL),
  304. PXA_BIT(ICR_MA, "MA", NULL),
  305. PXA_BIT(ICR_SCLE, "SCLE", "scle"),
  306. PXA_BIT(ICR_IUE, "IUE", "iue"),
  307. PXA_BIT(ICR_GCD, "GCD", NULL),
  308. PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
  309. PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
  310. PXA_BIT(ICR_BEIE, "BEIE", NULL),
  311. PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
  312. PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
  313. PXA_BIT(ICR_SADIE, "SADIE", NULL),
  314. PXA_BIT(ICR_UR, "UR", "ur"),
  315. };
  316. static void decode_ICR(unsigned int val)
  317. {
  318. decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
  319. }
  320. #endif
  321. static unsigned int i2c_debug = DEBUG;
  322. static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
  323. {
  324. dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
  325. readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  326. }
  327. #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
  328. static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
  329. {
  330. unsigned int i;
  331. struct device *dev = &i2c->adap.dev;
  332. dev_err(dev, "slave_0x%x error: %s\n",
  333. i2c->req_slave_addr >> 1, why);
  334. dev_err(dev, "msg_num: %d msg_idx: %d msg_ptr: %d\n",
  335. i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
  336. dev_err(dev, "IBMR: %08x IDBR: %08x ICR: %08x ISR: %08x\n",
  337. readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)),
  338. readl(_ISR(i2c)));
  339. dev_err(dev, "log:");
  340. for (i = 0; i < i2c->irqlogidx; i++)
  341. pr_cont(" [%03x:%05x]", i2c->isrlog[i], i2c->icrlog[i]);
  342. pr_cont("\n");
  343. }
  344. #else /* ifdef DEBUG */
  345. #define i2c_debug 0
  346. #define show_state(i2c) do { } while (0)
  347. #define decode_ISR(val) do { } while (0)
  348. #define decode_ICR(val) do { } while (0)
  349. #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
  350. #endif /* ifdef DEBUG / else */
  351. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
  352. static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
  353. {
  354. return !(readl(_ICR(i2c)) & ICR_SCLE);
  355. }
  356. static void i2c_pxa_abort(struct pxa_i2c *i2c)
  357. {
  358. int i = 250;
  359. if (i2c_pxa_is_slavemode(i2c)) {
  360. dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
  361. return;
  362. }
  363. while ((i > 0) && (readl(_IBMR(i2c)) & IBMR_SDAS) == 0) {
  364. unsigned long icr = readl(_ICR(i2c));
  365. icr &= ~ICR_START;
  366. icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
  367. writel(icr, _ICR(i2c));
  368. show_state(i2c);
  369. mdelay(1);
  370. i --;
  371. }
  372. writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
  373. _ICR(i2c));
  374. }
  375. static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
  376. {
  377. int timeout = DEF_TIMEOUT;
  378. u32 isr;
  379. while (1) {
  380. isr = readl(_ISR(i2c));
  381. if (!(isr & (ISR_IBB | ISR_UB)))
  382. return 0;
  383. if (isr & ISR_SAD)
  384. timeout += 4;
  385. if (!timeout--)
  386. break;
  387. msleep(2);
  388. show_state(i2c);
  389. }
  390. show_state(i2c);
  391. return I2C_RETRY;
  392. }
  393. static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
  394. {
  395. unsigned long timeout = jiffies + HZ*4;
  396. while (time_before(jiffies, timeout)) {
  397. if (i2c_debug > 1)
  398. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  399. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  400. if (readl(_ISR(i2c)) & ISR_SAD) {
  401. if (i2c_debug > 0)
  402. dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
  403. goto out;
  404. }
  405. /* wait for unit and bus being not busy, and we also do a
  406. * quick check of the i2c lines themselves to ensure they've
  407. * gone high...
  408. */
  409. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 &&
  410. readl(_IBMR(i2c)) == (IBMR_SCLS | IBMR_SDAS)) {
  411. if (i2c_debug > 0)
  412. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  413. return 1;
  414. }
  415. msleep(1);
  416. }
  417. if (i2c_debug > 0)
  418. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  419. out:
  420. return 0;
  421. }
  422. static int i2c_pxa_set_master(struct pxa_i2c *i2c)
  423. {
  424. if (i2c_debug)
  425. dev_dbg(&i2c->adap.dev, "setting to bus master\n");
  426. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
  427. dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
  428. if (!i2c_pxa_wait_master(i2c)) {
  429. dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
  430. return I2C_RETRY;
  431. }
  432. }
  433. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  434. return 0;
  435. }
  436. #ifdef CONFIG_I2C_PXA_SLAVE
  437. static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
  438. {
  439. unsigned long timeout = jiffies + HZ*1;
  440. /* wait for stop */
  441. show_state(i2c);
  442. while (time_before(jiffies, timeout)) {
  443. if (i2c_debug > 1)
  444. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  445. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  446. if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
  447. (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
  448. (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
  449. if (i2c_debug > 1)
  450. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  451. return 1;
  452. }
  453. msleep(1);
  454. }
  455. if (i2c_debug > 0)
  456. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  457. return 0;
  458. }
  459. /*
  460. * clear the hold on the bus, and take of anything else
  461. * that has been configured
  462. */
  463. static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
  464. {
  465. show_state(i2c);
  466. if (errcode < 0) {
  467. udelay(100); /* simple delay */
  468. } else {
  469. /* we need to wait for the stop condition to end */
  470. /* if we where in stop, then clear... */
  471. if (readl(_ICR(i2c)) & ICR_STOP) {
  472. udelay(100);
  473. writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
  474. }
  475. if (!i2c_pxa_wait_slave(i2c)) {
  476. dev_err(&i2c->adap.dev, "%s: wait timedout\n",
  477. __func__);
  478. return;
  479. }
  480. }
  481. writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
  482. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  483. if (i2c_debug) {
  484. dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
  485. decode_ICR(readl(_ICR(i2c)));
  486. }
  487. }
  488. #else
  489. #define i2c_pxa_set_slave(i2c, err) do { } while (0)
  490. #endif
  491. static void i2c_pxa_do_reset(struct pxa_i2c *i2c)
  492. {
  493. /* reset according to 9.8 */
  494. writel(ICR_UR, _ICR(i2c));
  495. writel(I2C_ISR_INIT, _ISR(i2c));
  496. writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
  497. if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
  498. writel(i2c->slave_addr, _ISAR(i2c));
  499. /* set control register values */
  500. writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c));
  501. writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c));
  502. #ifdef CONFIG_I2C_PXA_SLAVE
  503. dev_info(&i2c->adap.dev, "Enabling slave mode\n");
  504. writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
  505. #endif
  506. i2c_pxa_set_slave(i2c, 0);
  507. }
  508. static void i2c_pxa_enable(struct pxa_i2c *i2c)
  509. {
  510. /* enable unit */
  511. writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
  512. udelay(100);
  513. }
  514. static void i2c_pxa_reset(struct pxa_i2c *i2c)
  515. {
  516. pr_debug("Resetting I2C Controller Unit\n");
  517. /* abort any transfer currently under way */
  518. i2c_pxa_abort(i2c);
  519. i2c_pxa_do_reset(i2c);
  520. i2c_pxa_enable(i2c);
  521. }
  522. #ifdef CONFIG_I2C_PXA_SLAVE
  523. /*
  524. * PXA I2C Slave mode
  525. */
  526. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  527. {
  528. if (isr & ISR_BED) {
  529. /* what should we do here? */
  530. } else {
  531. u8 byte = 0;
  532. if (i2c->slave != NULL)
  533. i2c_slave_event(i2c->slave, I2C_SLAVE_READ_PROCESSED,
  534. &byte);
  535. writel(byte, _IDBR(i2c));
  536. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
  537. }
  538. }
  539. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  540. {
  541. u8 byte = readl(_IDBR(i2c));
  542. if (i2c->slave != NULL)
  543. i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_RECEIVED, &byte);
  544. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  545. }
  546. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  547. {
  548. int timeout;
  549. if (i2c_debug > 0)
  550. dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
  551. (isr & ISR_RWM) ? 'r' : 't');
  552. if (i2c->slave != NULL) {
  553. if (isr & ISR_RWM) {
  554. u8 byte = 0;
  555. i2c_slave_event(i2c->slave, I2C_SLAVE_READ_REQUESTED,
  556. &byte);
  557. writel(byte, _IDBR(i2c));
  558. } else {
  559. i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_REQUESTED,
  560. NULL);
  561. }
  562. }
  563. /*
  564. * slave could interrupt in the middle of us generating a
  565. * start condition... if this happens, we'd better back off
  566. * and stop holding the poor thing up
  567. */
  568. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  569. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  570. timeout = 0x10000;
  571. while (1) {
  572. if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS)
  573. break;
  574. timeout--;
  575. if (timeout <= 0) {
  576. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  577. break;
  578. }
  579. }
  580. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  581. }
  582. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  583. {
  584. if (i2c_debug > 2)
  585. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
  586. if (i2c->slave != NULL)
  587. i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL);
  588. if (i2c_debug > 2)
  589. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
  590. /*
  591. * If we have a master-mode message waiting,
  592. * kick it off now that the slave has completed.
  593. */
  594. if (i2c->msg)
  595. i2c_pxa_master_complete(i2c, I2C_RETRY);
  596. }
  597. static int i2c_pxa_slave_reg(struct i2c_client *slave)
  598. {
  599. struct pxa_i2c *i2c = slave->adapter->algo_data;
  600. if (i2c->slave)
  601. return -EBUSY;
  602. if (!i2c->reg_isar)
  603. return -EAFNOSUPPORT;
  604. i2c->slave = slave;
  605. i2c->slave_addr = slave->addr;
  606. writel(i2c->slave_addr, _ISAR(i2c));
  607. return 0;
  608. }
  609. static int i2c_pxa_slave_unreg(struct i2c_client *slave)
  610. {
  611. struct pxa_i2c *i2c = slave->adapter->algo_data;
  612. WARN_ON(!i2c->slave);
  613. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  614. writel(i2c->slave_addr, _ISAR(i2c));
  615. i2c->slave = NULL;
  616. return 0;
  617. }
  618. #else
  619. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  620. {
  621. if (isr & ISR_BED) {
  622. /* what should we do here? */
  623. } else {
  624. writel(0, _IDBR(i2c));
  625. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  626. }
  627. }
  628. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  629. {
  630. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  631. }
  632. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  633. {
  634. int timeout;
  635. /*
  636. * slave could interrupt in the middle of us generating a
  637. * start condition... if this happens, we'd better back off
  638. * and stop holding the poor thing up
  639. */
  640. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  641. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  642. timeout = 0x10000;
  643. while (1) {
  644. if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS)
  645. break;
  646. timeout--;
  647. if (timeout <= 0) {
  648. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  649. break;
  650. }
  651. }
  652. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  653. }
  654. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  655. {
  656. if (i2c->msg)
  657. i2c_pxa_master_complete(i2c, I2C_RETRY);
  658. }
  659. #endif
  660. /*
  661. * PXA I2C Master mode
  662. */
  663. static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
  664. {
  665. u32 icr;
  666. /*
  667. * Step 1: target slave address into IDBR
  668. */
  669. i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
  670. writel(i2c->req_slave_addr, _IDBR(i2c));
  671. /*
  672. * Step 2: initiate the write.
  673. */
  674. icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
  675. writel(icr | ICR_START | ICR_TB, _ICR(i2c));
  676. }
  677. static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
  678. {
  679. u32 icr;
  680. /* Clear the START, STOP, ACK, TB and MA flags */
  681. icr = readl(_ICR(i2c));
  682. icr &= ~(ICR_START | ICR_STOP | ICR_ACKNAK | ICR_TB | ICR_MA);
  683. writel(icr, _ICR(i2c));
  684. }
  685. /*
  686. * PXA I2C send master code
  687. * 1. Load master code to IDBR and send it.
  688. * Note for HS mode, set ICR [GPIOEN].
  689. * 2. Wait until win arbitration.
  690. */
  691. static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c)
  692. {
  693. u32 icr;
  694. long time_left;
  695. spin_lock_irq(&i2c->lock);
  696. i2c->highmode_enter = true;
  697. writel(i2c->master_code, _IDBR(i2c));
  698. icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
  699. icr |= ICR_GPIOEN | ICR_START | ICR_TB | ICR_ITEIE;
  700. writel(icr, _ICR(i2c));
  701. spin_unlock_irq(&i2c->lock);
  702. time_left = wait_event_timeout(i2c->wait,
  703. i2c->highmode_enter == false, HZ * 1);
  704. i2c->highmode_enter = false;
  705. return (time_left == 0) ? I2C_RETRY : 0;
  706. }
  707. /*
  708. * i2c_pxa_master_complete - complete the message and wake up.
  709. */
  710. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  711. {
  712. i2c->msg_ptr = 0;
  713. i2c->msg = NULL;
  714. i2c->msg_idx ++;
  715. i2c->msg_num = 0;
  716. if (ret)
  717. i2c->msg_idx = ret;
  718. if (!i2c->use_pio)
  719. wake_up(&i2c->wait);
  720. }
  721. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  722. {
  723. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  724. again:
  725. /*
  726. * If ISR_ALD is set, we lost arbitration.
  727. */
  728. if (isr & ISR_ALD) {
  729. /*
  730. * Do we need to do anything here? The PXA docs
  731. * are vague about what happens.
  732. */
  733. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  734. /*
  735. * We ignore this error. We seem to see spurious ALDs
  736. * for seemingly no reason. If we handle them as I think
  737. * they should, we end up causing an I2C error, which
  738. * is painful for some systems.
  739. */
  740. return; /* ignore */
  741. }
  742. if ((isr & ISR_BED) &&
  743. (!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
  744. (isr & ISR_ACKNAK)))) {
  745. int ret = BUS_ERROR;
  746. /*
  747. * I2C bus error - either the device NAK'd us, or
  748. * something more serious happened. If we were NAK'd
  749. * on the initial address phase, we can retry.
  750. */
  751. if (isr & ISR_ACKNAK) {
  752. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  753. ret = NO_SLAVE;
  754. else
  755. ret = XFER_NAKED;
  756. }
  757. i2c_pxa_master_complete(i2c, ret);
  758. } else if (isr & ISR_RWM) {
  759. /*
  760. * Read mode. We have just sent the address byte, and
  761. * now we must initiate the transfer.
  762. */
  763. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  764. i2c->msg_idx == i2c->msg_num - 1)
  765. icr |= ICR_STOP | ICR_ACKNAK;
  766. icr |= ICR_ALDIE | ICR_TB;
  767. } else if (i2c->msg_ptr < i2c->msg->len) {
  768. /*
  769. * Write mode. Write the next data byte.
  770. */
  771. writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
  772. icr |= ICR_ALDIE | ICR_TB;
  773. /*
  774. * If this is the last byte of the last message or last byte
  775. * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
  776. */
  777. if ((i2c->msg_ptr == i2c->msg->len) &&
  778. ((i2c->msg->flags & I2C_M_STOP) ||
  779. (i2c->msg_idx == i2c->msg_num - 1)))
  780. icr |= ICR_STOP;
  781. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  782. /*
  783. * Next segment of the message.
  784. */
  785. i2c->msg_ptr = 0;
  786. i2c->msg_idx ++;
  787. i2c->msg++;
  788. /*
  789. * If we aren't doing a repeated start and address,
  790. * go back and try to send the next byte. Note that
  791. * we do not support switching the R/W direction here.
  792. */
  793. if (i2c->msg->flags & I2C_M_NOSTART)
  794. goto again;
  795. /*
  796. * Write the next address.
  797. */
  798. i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
  799. writel(i2c->req_slave_addr, _IDBR(i2c));
  800. /*
  801. * And trigger a repeated start, and send the byte.
  802. */
  803. icr &= ~ICR_ALDIE;
  804. icr |= ICR_START | ICR_TB;
  805. } else {
  806. if (i2c->msg->len == 0)
  807. icr |= ICR_MA;
  808. i2c_pxa_master_complete(i2c, 0);
  809. }
  810. i2c->icrlog[i2c->irqlogidx-1] = icr;
  811. writel(icr, _ICR(i2c));
  812. show_state(i2c);
  813. }
  814. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  815. {
  816. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  817. /*
  818. * Read the byte.
  819. */
  820. i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
  821. if (i2c->msg_ptr < i2c->msg->len) {
  822. /*
  823. * If this is the last byte of the last
  824. * message, send a STOP.
  825. */
  826. if (i2c->msg_ptr == i2c->msg->len - 1)
  827. icr |= ICR_STOP | ICR_ACKNAK;
  828. icr |= ICR_ALDIE | ICR_TB;
  829. } else {
  830. i2c_pxa_master_complete(i2c, 0);
  831. }
  832. i2c->icrlog[i2c->irqlogidx-1] = icr;
  833. writel(icr, _ICR(i2c));
  834. }
  835. #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
  836. ISR_SAD | ISR_BED)
  837. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  838. {
  839. struct pxa_i2c *i2c = dev_id;
  840. u32 isr = readl(_ISR(i2c));
  841. if (!(isr & VALID_INT_SOURCE))
  842. return IRQ_NONE;
  843. if (i2c_debug > 2 && 0) {
  844. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  845. __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
  846. decode_ISR(isr);
  847. }
  848. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  849. i2c->isrlog[i2c->irqlogidx++] = isr;
  850. show_state(i2c);
  851. /*
  852. * Always clear all pending IRQs.
  853. */
  854. writel(isr & VALID_INT_SOURCE, _ISR(i2c));
  855. if (isr & ISR_SAD)
  856. i2c_pxa_slave_start(i2c, isr);
  857. if (isr & ISR_SSD)
  858. i2c_pxa_slave_stop(i2c);
  859. if (i2c_pxa_is_slavemode(i2c)) {
  860. if (isr & ISR_ITE)
  861. i2c_pxa_slave_txempty(i2c, isr);
  862. if (isr & ISR_IRF)
  863. i2c_pxa_slave_rxfull(i2c, isr);
  864. } else if (i2c->msg && (!i2c->highmode_enter)) {
  865. if (isr & ISR_ITE)
  866. i2c_pxa_irq_txempty(i2c, isr);
  867. if (isr & ISR_IRF)
  868. i2c_pxa_irq_rxfull(i2c, isr);
  869. } else if ((isr & ISR_ITE) && i2c->highmode_enter) {
  870. i2c->highmode_enter = false;
  871. wake_up(&i2c->wait);
  872. } else {
  873. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  874. }
  875. return IRQ_HANDLED;
  876. }
  877. /*
  878. * We are protected by the adapter bus mutex.
  879. */
  880. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  881. {
  882. long time_left;
  883. int ret;
  884. /*
  885. * Wait for the bus to become free.
  886. */
  887. ret = i2c_pxa_wait_bus_not_busy(i2c);
  888. if (ret) {
  889. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  890. i2c_recover_bus(&i2c->adap);
  891. goto out;
  892. }
  893. /*
  894. * Set master mode.
  895. */
  896. ret = i2c_pxa_set_master(i2c);
  897. if (ret) {
  898. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  899. goto out;
  900. }
  901. if (i2c->high_mode) {
  902. ret = i2c_pxa_send_mastercode(i2c);
  903. if (ret) {
  904. dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
  905. goto out;
  906. }
  907. }
  908. spin_lock_irq(&i2c->lock);
  909. i2c->msg = msg;
  910. i2c->msg_num = num;
  911. i2c->msg_idx = 0;
  912. i2c->msg_ptr = 0;
  913. i2c->irqlogidx = 0;
  914. i2c_pxa_start_message(i2c);
  915. spin_unlock_irq(&i2c->lock);
  916. /*
  917. * The rest of the processing occurs in the interrupt handler.
  918. */
  919. time_left = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  920. i2c_pxa_stop_message(i2c);
  921. /*
  922. * We place the return code in i2c->msg_idx.
  923. */
  924. ret = i2c->msg_idx;
  925. if (!time_left && i2c->msg_num) {
  926. i2c_pxa_scream_blue_murder(i2c, "timeout with active message");
  927. i2c_recover_bus(&i2c->adap);
  928. ret = I2C_RETRY;
  929. }
  930. out:
  931. return ret;
  932. }
  933. static int i2c_pxa_internal_xfer(struct pxa_i2c *i2c,
  934. struct i2c_msg *msgs, int num,
  935. int (*xfer)(struct pxa_i2c *,
  936. struct i2c_msg *, int num))
  937. {
  938. int ret, i;
  939. for (i = 0; ; ) {
  940. ret = xfer(i2c, msgs, num);
  941. if (ret != I2C_RETRY && ret != NO_SLAVE)
  942. goto out;
  943. if (++i >= i2c->adap.retries)
  944. break;
  945. if (i2c_debug)
  946. dev_dbg(&i2c->adap.dev, "Retrying transmission\n");
  947. udelay(100);
  948. }
  949. if (ret != NO_SLAVE)
  950. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  951. ret = -EREMOTEIO;
  952. out:
  953. i2c_pxa_set_slave(i2c, ret);
  954. return ret;
  955. }
  956. static int i2c_pxa_xfer(struct i2c_adapter *adap,
  957. struct i2c_msg msgs[], int num)
  958. {
  959. struct pxa_i2c *i2c = adap->algo_data;
  960. return i2c_pxa_internal_xfer(i2c, msgs, num, i2c_pxa_do_xfer);
  961. }
  962. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  963. {
  964. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  965. I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
  966. }
  967. static const struct i2c_algorithm i2c_pxa_algorithm = {
  968. .master_xfer = i2c_pxa_xfer,
  969. .functionality = i2c_pxa_functionality,
  970. #ifdef CONFIG_I2C_PXA_SLAVE
  971. .reg_slave = i2c_pxa_slave_reg,
  972. .unreg_slave = i2c_pxa_slave_unreg,
  973. #endif
  974. };
  975. /* Non-interrupt mode support */
  976. static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
  977. {
  978. /* make timeout the same as for interrupt based functions */
  979. long timeout = 2 * DEF_TIMEOUT;
  980. /*
  981. * Wait for the bus to become free.
  982. */
  983. while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB))
  984. udelay(1000);
  985. if (timeout < 0) {
  986. show_state(i2c);
  987. dev_err(&i2c->adap.dev,
  988. "i2c_pxa: timeout waiting for bus free (set_master)\n");
  989. return I2C_RETRY;
  990. }
  991. /*
  992. * Set master mode.
  993. */
  994. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  995. return 0;
  996. }
  997. static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
  998. struct i2c_msg *msg, int num)
  999. {
  1000. unsigned long timeout = 500000; /* 5 seconds */
  1001. int ret = 0;
  1002. ret = i2c_pxa_pio_set_master(i2c);
  1003. if (ret)
  1004. goto out;
  1005. i2c->msg = msg;
  1006. i2c->msg_num = num;
  1007. i2c->msg_idx = 0;
  1008. i2c->msg_ptr = 0;
  1009. i2c->irqlogidx = 0;
  1010. i2c_pxa_start_message(i2c);
  1011. while (i2c->msg_num > 0 && --timeout) {
  1012. i2c_pxa_handler(0, i2c);
  1013. udelay(10);
  1014. }
  1015. i2c_pxa_stop_message(i2c);
  1016. /*
  1017. * We place the return code in i2c->msg_idx.
  1018. */
  1019. ret = i2c->msg_idx;
  1020. out:
  1021. if (timeout == 0) {
  1022. i2c_pxa_scream_blue_murder(i2c, "timeout (do_pio_xfer)");
  1023. ret = I2C_RETRY;
  1024. }
  1025. return ret;
  1026. }
  1027. static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
  1028. struct i2c_msg msgs[], int num)
  1029. {
  1030. struct pxa_i2c *i2c = adap->algo_data;
  1031. /* If the I2C controller is disabled we need to reset it
  1032. (probably due to a suspend/resume destroying state). We do
  1033. this here as we can then avoid worrying about resuming the
  1034. controller before its users. */
  1035. if (!(readl(_ICR(i2c)) & ICR_IUE))
  1036. i2c_pxa_reset(i2c);
  1037. return i2c_pxa_internal_xfer(i2c, msgs, num, i2c_pxa_do_pio_xfer);
  1038. }
  1039. static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
  1040. .master_xfer = i2c_pxa_pio_xfer,
  1041. .functionality = i2c_pxa_functionality,
  1042. #ifdef CONFIG_I2C_PXA_SLAVE
  1043. .reg_slave = i2c_pxa_slave_reg,
  1044. .unreg_slave = i2c_pxa_slave_unreg,
  1045. #endif
  1046. };
  1047. static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
  1048. enum pxa_i2c_types *i2c_types)
  1049. {
  1050. struct device_node *np = pdev->dev.of_node;
  1051. if (!pdev->dev.of_node)
  1052. return 1;
  1053. /* For device tree we always use the dynamic or alias-assigned ID */
  1054. i2c->adap.nr = -1;
  1055. i2c->use_pio = of_property_read_bool(np, "mrvl,i2c-polling");
  1056. i2c->fast_mode = of_property_read_bool(np, "mrvl,i2c-fast-mode");
  1057. *i2c_types = (enum pxa_i2c_types)device_get_match_data(&pdev->dev);
  1058. return 0;
  1059. }
  1060. static int i2c_pxa_probe_pdata(struct platform_device *pdev,
  1061. struct pxa_i2c *i2c,
  1062. enum pxa_i2c_types *i2c_types)
  1063. {
  1064. struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev);
  1065. const struct platform_device_id *id = platform_get_device_id(pdev);
  1066. *i2c_types = id->driver_data;
  1067. if (plat) {
  1068. i2c->use_pio = plat->use_pio;
  1069. i2c->fast_mode = plat->fast_mode;
  1070. i2c->high_mode = plat->high_mode;
  1071. i2c->master_code = plat->master_code;
  1072. if (!i2c->master_code)
  1073. i2c->master_code = 0xe;
  1074. i2c->rate = plat->rate;
  1075. }
  1076. return 0;
  1077. }
  1078. static void i2c_pxa_prepare_recovery(struct i2c_adapter *adap)
  1079. {
  1080. struct pxa_i2c *i2c = adap->algo_data;
  1081. u32 ibmr = readl(_IBMR(i2c));
  1082. /*
  1083. * Program the GPIOs to reflect the current I2C bus state while
  1084. * we transition to recovery; this avoids glitching the bus.
  1085. */
  1086. gpiod_set_value(i2c->recovery.scl_gpiod, ibmr & IBMR_SCLS);
  1087. gpiod_set_value(i2c->recovery.sda_gpiod, ibmr & IBMR_SDAS);
  1088. WARN_ON(pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_recovery));
  1089. }
  1090. static void i2c_pxa_unprepare_recovery(struct i2c_adapter *adap)
  1091. {
  1092. struct pxa_i2c *i2c = adap->algo_data;
  1093. u32 isr;
  1094. /*
  1095. * The bus should now be free. Clear up the I2C controller before
  1096. * handing control of the bus back to avoid the bus changing state.
  1097. */
  1098. isr = readl(_ISR(i2c));
  1099. if (isr & (ISR_UB | ISR_IBB)) {
  1100. dev_dbg(&i2c->adap.dev,
  1101. "recovery: resetting controller, ISR=0x%08x\n", isr);
  1102. i2c_pxa_do_reset(i2c);
  1103. }
  1104. WARN_ON(pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_default));
  1105. dev_dbg(&i2c->adap.dev, "recovery: IBMR 0x%08x ISR 0x%08x\n",
  1106. readl(_IBMR(i2c)), readl(_ISR(i2c)));
  1107. i2c_pxa_enable(i2c);
  1108. }
  1109. static int i2c_pxa_init_recovery(struct pxa_i2c *i2c)
  1110. {
  1111. struct i2c_bus_recovery_info *bri = &i2c->recovery;
  1112. struct device *dev = i2c->adap.dev.parent;
  1113. /*
  1114. * When slave mode is enabled, we are not the only master on the bus.
  1115. * Bus recovery can only be performed when we are the master, which
  1116. * we can't be certain of. Therefore, when slave mode is enabled, do
  1117. * not configure bus recovery.
  1118. */
  1119. if (IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
  1120. return 0;
  1121. i2c->pinctrl = devm_pinctrl_get(dev);
  1122. if (PTR_ERR(i2c->pinctrl) == -ENODEV)
  1123. i2c->pinctrl = NULL;
  1124. if (IS_ERR(i2c->pinctrl))
  1125. return PTR_ERR(i2c->pinctrl);
  1126. if (!i2c->pinctrl)
  1127. return 0;
  1128. i2c->pinctrl_default = pinctrl_lookup_state(i2c->pinctrl,
  1129. PINCTRL_STATE_DEFAULT);
  1130. i2c->pinctrl_recovery = pinctrl_lookup_state(i2c->pinctrl, "recovery");
  1131. if (IS_ERR(i2c->pinctrl_default) || IS_ERR(i2c->pinctrl_recovery)) {
  1132. dev_info(dev, "missing pinmux recovery information: %ld %ld\n",
  1133. PTR_ERR(i2c->pinctrl_default),
  1134. PTR_ERR(i2c->pinctrl_recovery));
  1135. return 0;
  1136. }
  1137. /*
  1138. * Claiming GPIOs can influence the pinmux state, and may glitch the
  1139. * I2C bus. Do this carefully.
  1140. */
  1141. bri->scl_gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
  1142. if (bri->scl_gpiod == ERR_PTR(-EPROBE_DEFER))
  1143. return -EPROBE_DEFER;
  1144. if (IS_ERR(bri->scl_gpiod)) {
  1145. dev_info(dev, "missing scl gpio recovery information: %pe\n",
  1146. bri->scl_gpiod);
  1147. return 0;
  1148. }
  1149. /*
  1150. * We have SCL. Pull SCL low and wait a bit so that SDA glitches
  1151. * have no effect.
  1152. */
  1153. gpiod_direction_output(bri->scl_gpiod, 0);
  1154. udelay(10);
  1155. bri->sda_gpiod = devm_gpiod_get(dev, "sda", GPIOD_OUT_HIGH_OPEN_DRAIN);
  1156. /* Wait a bit in case of a SDA glitch, and then release SCL. */
  1157. udelay(10);
  1158. gpiod_direction_output(bri->scl_gpiod, 1);
  1159. if (bri->sda_gpiod == ERR_PTR(-EPROBE_DEFER))
  1160. return -EPROBE_DEFER;
  1161. if (IS_ERR(bri->sda_gpiod)) {
  1162. dev_info(dev, "missing sda gpio recovery information: %pe\n",
  1163. bri->sda_gpiod);
  1164. return 0;
  1165. }
  1166. bri->prepare_recovery = i2c_pxa_prepare_recovery;
  1167. bri->unprepare_recovery = i2c_pxa_unprepare_recovery;
  1168. bri->recover_bus = i2c_generic_scl_recovery;
  1169. i2c->adap.bus_recovery_info = bri;
  1170. /*
  1171. * Claiming GPIOs can change the pinmux state, which confuses the
  1172. * pinctrl since pinctrl's idea of the current setting is unaffected
  1173. * by the pinmux change caused by claiming the GPIO. Work around that
  1174. * by switching pinctrl to the GPIO state here. We do it this way to
  1175. * avoid glitching the I2C bus.
  1176. */
  1177. pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_recovery);
  1178. return pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_default);
  1179. }
  1180. static int i2c_pxa_probe(struct platform_device *dev)
  1181. {
  1182. struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev);
  1183. enum pxa_i2c_types i2c_type;
  1184. struct pxa_i2c *i2c;
  1185. struct resource *res;
  1186. int ret, irq;
  1187. i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL);
  1188. if (!i2c)
  1189. return -ENOMEM;
  1190. /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
  1191. i2c->adap.nr = dev->id;
  1192. i2c->adap.owner = THIS_MODULE;
  1193. i2c->adap.retries = 5;
  1194. i2c->adap.algo_data = i2c;
  1195. i2c->adap.dev.parent = &dev->dev;
  1196. #ifdef CONFIG_OF
  1197. i2c->adap.dev.of_node = dev->dev.of_node;
  1198. #endif
  1199. i2c->reg_base = devm_platform_get_and_ioremap_resource(dev, 0, &res);
  1200. if (IS_ERR(i2c->reg_base))
  1201. return PTR_ERR(i2c->reg_base);
  1202. irq = platform_get_irq(dev, 0);
  1203. if (irq < 0)
  1204. return irq;
  1205. ret = i2c_pxa_init_recovery(i2c);
  1206. if (ret)
  1207. return ret;
  1208. ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
  1209. if (ret > 0)
  1210. ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
  1211. if (ret < 0)
  1212. return ret;
  1213. spin_lock_init(&i2c->lock);
  1214. init_waitqueue_head(&i2c->wait);
  1215. strscpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
  1216. i2c->clk = devm_clk_get(&dev->dev, NULL);
  1217. if (IS_ERR(i2c->clk))
  1218. return dev_err_probe(&dev->dev, PTR_ERR(i2c->clk),
  1219. "failed to get the clk\n");
  1220. i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
  1221. i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
  1222. i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
  1223. i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
  1224. i2c->fm_mask = pxa_reg_layout[i2c_type].fm;
  1225. i2c->hs_mask = pxa_reg_layout[i2c_type].hs;
  1226. if (i2c_type != REGS_CE4100)
  1227. i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
  1228. if (i2c_type == REGS_PXA910) {
  1229. i2c->reg_ilcr = i2c->reg_base + pxa_reg_layout[i2c_type].ilcr;
  1230. i2c->reg_iwcr = i2c->reg_base + pxa_reg_layout[i2c_type].iwcr;
  1231. }
  1232. i2c->iobase = res->start;
  1233. i2c->iosize = resource_size(res);
  1234. i2c->irq = irq;
  1235. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  1236. i2c->highmode_enter = false;
  1237. if (plat) {
  1238. i2c->adap.class = plat->class;
  1239. }
  1240. if (i2c->high_mode) {
  1241. if (i2c->rate) {
  1242. clk_set_rate(i2c->clk, i2c->rate);
  1243. pr_info("i2c: <%s> set rate to %ld\n",
  1244. i2c->adap.name, clk_get_rate(i2c->clk));
  1245. } else
  1246. pr_warn("i2c: <%s> clock rate not set\n",
  1247. i2c->adap.name);
  1248. }
  1249. ret = clk_prepare_enable(i2c->clk);
  1250. if (ret)
  1251. return dev_err_probe(&dev->dev, ret,
  1252. "failed to enable clock\n");
  1253. if (i2c->use_pio) {
  1254. i2c->adap.algo = &i2c_pxa_pio_algorithm;
  1255. } else {
  1256. i2c->adap.algo = &i2c_pxa_algorithm;
  1257. ret = devm_request_irq(&dev->dev, irq, i2c_pxa_handler,
  1258. IRQF_SHARED | IRQF_NO_SUSPEND,
  1259. dev_name(&dev->dev), i2c);
  1260. if (ret) {
  1261. dev_err(&dev->dev, "failed to request irq: %d\n", ret);
  1262. goto ereqirq;
  1263. }
  1264. }
  1265. i2c_pxa_reset(i2c);
  1266. ret = i2c_add_numbered_adapter(&i2c->adap);
  1267. if (ret < 0)
  1268. goto ereqirq;
  1269. platform_set_drvdata(dev, i2c);
  1270. #ifdef CONFIG_I2C_PXA_SLAVE
  1271. dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n",
  1272. i2c->slave_addr);
  1273. #else
  1274. dev_info(&i2c->adap.dev, " PXA I2C adapter\n");
  1275. #endif
  1276. return 0;
  1277. ereqirq:
  1278. clk_disable_unprepare(i2c->clk);
  1279. return ret;
  1280. }
  1281. static void i2c_pxa_remove(struct platform_device *dev)
  1282. {
  1283. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  1284. i2c_del_adapter(&i2c->adap);
  1285. clk_disable_unprepare(i2c->clk);
  1286. }
  1287. static int i2c_pxa_suspend_noirq(struct device *dev)
  1288. {
  1289. struct pxa_i2c *i2c = dev_get_drvdata(dev);
  1290. clk_disable(i2c->clk);
  1291. return 0;
  1292. }
  1293. static int i2c_pxa_resume_noirq(struct device *dev)
  1294. {
  1295. struct pxa_i2c *i2c = dev_get_drvdata(dev);
  1296. clk_enable(i2c->clk);
  1297. i2c_pxa_reset(i2c);
  1298. return 0;
  1299. }
  1300. static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
  1301. .suspend_noirq = i2c_pxa_suspend_noirq,
  1302. .resume_noirq = i2c_pxa_resume_noirq,
  1303. };
  1304. static struct platform_driver i2c_pxa_driver = {
  1305. .probe = i2c_pxa_probe,
  1306. .remove_new = i2c_pxa_remove,
  1307. .driver = {
  1308. .name = "pxa2xx-i2c",
  1309. .pm = pm_sleep_ptr(&i2c_pxa_dev_pm_ops),
  1310. .of_match_table = i2c_pxa_dt_ids,
  1311. },
  1312. .id_table = i2c_pxa_id_table,
  1313. };
  1314. static int __init i2c_adap_pxa_init(void)
  1315. {
  1316. return platform_driver_register(&i2c_pxa_driver);
  1317. }
  1318. static void __exit i2c_adap_pxa_exit(void)
  1319. {
  1320. platform_driver_unregister(&i2c_pxa_driver);
  1321. }
  1322. MODULE_DESCRIPTION("Intel PXA2XX I2C adapter");
  1323. MODULE_LICENSE("GPL");
  1324. subsys_initcall(i2c_adap_pxa_init);
  1325. module_exit(i2c_adap_pxa_exit);