i2c-npcm7xx.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Nuvoton NPCM7xx I2C Controller driver
  4. *
  5. * Copyright (C) 2020 Nuvoton Technologies [email protected]
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/errno.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/irq.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. enum i2c_mode {
  24. I2C_MASTER,
  25. I2C_SLAVE,
  26. };
  27. /*
  28. * External I2C Interface driver xfer indication values, which indicate status
  29. * of the bus.
  30. */
  31. enum i2c_state_ind {
  32. I2C_NO_STATUS_IND = 0,
  33. I2C_SLAVE_RCV_IND,
  34. I2C_SLAVE_XMIT_IND,
  35. I2C_SLAVE_XMIT_MISSING_DATA_IND,
  36. I2C_SLAVE_RESTART_IND,
  37. I2C_SLAVE_DONE_IND,
  38. I2C_MASTER_DONE_IND,
  39. I2C_NACK_IND,
  40. I2C_BUS_ERR_IND,
  41. I2C_WAKE_UP_IND,
  42. I2C_BLOCK_BYTES_ERR_IND,
  43. I2C_SLAVE_RCV_MISSING_DATA_IND,
  44. };
  45. /*
  46. * Operation type values (used to define the operation currently running)
  47. * module is interrupt driven, on each interrupt the current operation is
  48. * checked to see if the module is currently reading or writing.
  49. */
  50. enum i2c_oper {
  51. I2C_NO_OPER = 0,
  52. I2C_WRITE_OPER,
  53. I2C_READ_OPER,
  54. };
  55. /* I2C Bank (module had 2 banks of registers) */
  56. enum i2c_bank {
  57. I2C_BANK_0 = 0,
  58. I2C_BANK_1,
  59. };
  60. /* Internal I2C states values (for the I2C module state machine). */
  61. enum i2c_state {
  62. I2C_DISABLE = 0,
  63. I2C_IDLE,
  64. I2C_MASTER_START,
  65. I2C_SLAVE_MATCH,
  66. I2C_OPER_STARTED,
  67. I2C_STOP_PENDING,
  68. };
  69. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  70. /* Module supports setting multiple own slave addresses */
  71. enum i2c_addr {
  72. I2C_SLAVE_ADDR1 = 0,
  73. I2C_SLAVE_ADDR2,
  74. I2C_SLAVE_ADDR3,
  75. I2C_SLAVE_ADDR4,
  76. I2C_SLAVE_ADDR5,
  77. I2C_SLAVE_ADDR6,
  78. I2C_SLAVE_ADDR7,
  79. I2C_SLAVE_ADDR8,
  80. I2C_SLAVE_ADDR9,
  81. I2C_SLAVE_ADDR10,
  82. I2C_GC_ADDR,
  83. I2C_ARP_ADDR,
  84. };
  85. #endif
  86. /* init register and default value required to enable module */
  87. #define NPCM_I2CSEGCTL 0xE4
  88. /* Common regs */
  89. #define NPCM_I2CSDA 0x00
  90. #define NPCM_I2CST 0x02
  91. #define NPCM_I2CCST 0x04
  92. #define NPCM_I2CCTL1 0x06
  93. #define NPCM_I2CADDR1 0x08
  94. #define NPCM_I2CCTL2 0x0A
  95. #define NPCM_I2CADDR2 0x0C
  96. #define NPCM_I2CCTL3 0x0E
  97. #define NPCM_I2CCST2 0x18
  98. #define NPCM_I2CCST3 0x19
  99. #define I2C_VER 0x1F
  100. /*BANK0 regs*/
  101. #define NPCM_I2CADDR3 0x10
  102. #define NPCM_I2CADDR7 0x11
  103. #define NPCM_I2CADDR4 0x12
  104. #define NPCM_I2CADDR8 0x13
  105. #define NPCM_I2CADDR5 0x14
  106. #define NPCM_I2CADDR9 0x15
  107. #define NPCM_I2CADDR6 0x16
  108. #define NPCM_I2CADDR10 0x17
  109. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  110. /*
  111. * npcm_i2caddr array:
  112. * The module supports having multiple own slave addresses.
  113. * Since the addr regs are sprinkled all over the address space,
  114. * use this array to get the address or each register.
  115. */
  116. #define I2C_NUM_OWN_ADDR 2
  117. #define I2C_NUM_OWN_ADDR_SUPPORTED 2
  118. static const int npcm_i2caddr[I2C_NUM_OWN_ADDR] = {
  119. NPCM_I2CADDR1, NPCM_I2CADDR2,
  120. };
  121. #endif
  122. #define NPCM_I2CCTL4 0x1A
  123. #define NPCM_I2CCTL5 0x1B
  124. #define NPCM_I2CSCLLT 0x1C /* SCL Low Time */
  125. #define NPCM_I2CFIF_CTL 0x1D /* FIFO Control */
  126. #define NPCM_I2CSCLHT 0x1E /* SCL High Time */
  127. /* BANK 1 regs */
  128. #define NPCM_I2CFIF_CTS 0x10 /* Both FIFOs Control and Status */
  129. #define NPCM_I2CTXF_CTL 0x12 /* Tx-FIFO Control */
  130. #define NPCM_I2CT_OUT 0x14 /* Bus T.O. */
  131. #define NPCM_I2CPEC 0x16 /* PEC Data */
  132. #define NPCM_I2CTXF_STS 0x1A /* Tx-FIFO Status */
  133. #define NPCM_I2CRXF_STS 0x1C /* Rx-FIFO Status */
  134. #define NPCM_I2CRXF_CTL 0x1E /* Rx-FIFO Control */
  135. /* NPCM_I2CST reg fields */
  136. #define NPCM_I2CST_XMIT BIT(0)
  137. #define NPCM_I2CST_MASTER BIT(1)
  138. #define NPCM_I2CST_NMATCH BIT(2)
  139. #define NPCM_I2CST_STASTR BIT(3)
  140. #define NPCM_I2CST_NEGACK BIT(4)
  141. #define NPCM_I2CST_BER BIT(5)
  142. #define NPCM_I2CST_SDAST BIT(6)
  143. #define NPCM_I2CST_SLVSTP BIT(7)
  144. /* NPCM_I2CCST reg fields */
  145. #define NPCM_I2CCST_BUSY BIT(0)
  146. #define NPCM_I2CCST_BB BIT(1)
  147. #define NPCM_I2CCST_MATCH BIT(2)
  148. #define NPCM_I2CCST_GCMATCH BIT(3)
  149. #define NPCM_I2CCST_TSDA BIT(4)
  150. #define NPCM_I2CCST_TGSCL BIT(5)
  151. #define NPCM_I2CCST_MATCHAF BIT(6)
  152. #define NPCM_I2CCST_ARPMATCH BIT(7)
  153. /* NPCM_I2CCTL1 reg fields */
  154. #define NPCM_I2CCTL1_START BIT(0)
  155. #define NPCM_I2CCTL1_STOP BIT(1)
  156. #define NPCM_I2CCTL1_INTEN BIT(2)
  157. #define NPCM_I2CCTL1_EOBINTE BIT(3)
  158. #define NPCM_I2CCTL1_ACK BIT(4)
  159. #define NPCM_I2CCTL1_GCMEN BIT(5)
  160. #define NPCM_I2CCTL1_NMINTE BIT(6)
  161. #define NPCM_I2CCTL1_STASTRE BIT(7)
  162. /* RW1S fields (inside a RW reg): */
  163. #define NPCM_I2CCTL1_RWS \
  164. (NPCM_I2CCTL1_START | NPCM_I2CCTL1_STOP | NPCM_I2CCTL1_ACK)
  165. /* npcm_i2caddr reg fields */
  166. #define NPCM_I2CADDR_A GENMASK(6, 0)
  167. #define NPCM_I2CADDR_SAEN BIT(7)
  168. /* NPCM_I2CCTL2 reg fields */
  169. #define I2CCTL2_ENABLE BIT(0)
  170. #define I2CCTL2_SCLFRQ6_0 GENMASK(7, 1)
  171. /* NPCM_I2CCTL3 reg fields */
  172. #define I2CCTL3_SCLFRQ8_7 GENMASK(1, 0)
  173. #define I2CCTL3_ARPMEN BIT(2)
  174. #define I2CCTL3_IDL_START BIT(3)
  175. #define I2CCTL3_400K_MODE BIT(4)
  176. #define I2CCTL3_BNK_SEL BIT(5)
  177. #define I2CCTL3_SDA_LVL BIT(6)
  178. #define I2CCTL3_SCL_LVL BIT(7)
  179. /* NPCM_I2CCST2 reg fields */
  180. #define NPCM_I2CCST2_MATCHA1F BIT(0)
  181. #define NPCM_I2CCST2_MATCHA2F BIT(1)
  182. #define NPCM_I2CCST2_MATCHA3F BIT(2)
  183. #define NPCM_I2CCST2_MATCHA4F BIT(3)
  184. #define NPCM_I2CCST2_MATCHA5F BIT(4)
  185. #define NPCM_I2CCST2_MATCHA6F BIT(5)
  186. #define NPCM_I2CCST2_MATCHA7F BIT(5)
  187. #define NPCM_I2CCST2_INTSTS BIT(7)
  188. /* NPCM_I2CCST3 reg fields */
  189. #define NPCM_I2CCST3_MATCHA8F BIT(0)
  190. #define NPCM_I2CCST3_MATCHA9F BIT(1)
  191. #define NPCM_I2CCST3_MATCHA10F BIT(2)
  192. #define NPCM_I2CCST3_EO_BUSY BIT(7)
  193. /* NPCM_I2CCTL4 reg fields */
  194. #define I2CCTL4_HLDT GENMASK(5, 0)
  195. #define I2CCTL4_LVL_WE BIT(7)
  196. /* NPCM_I2CCTL5 reg fields */
  197. #define I2CCTL5_DBNCT GENMASK(3, 0)
  198. /* NPCM_I2CFIF_CTS reg fields */
  199. #define NPCM_I2CFIF_CTS_RXF_TXE BIT(1)
  200. #define NPCM_I2CFIF_CTS_RFTE_IE BIT(3)
  201. #define NPCM_I2CFIF_CTS_CLR_FIFO BIT(6)
  202. #define NPCM_I2CFIF_CTS_SLVRSTR BIT(7)
  203. /* NPCM_I2CTXF_CTL reg field */
  204. #define NPCM_I2CTXF_CTL_THR_TXIE BIT(6)
  205. /* NPCM_I2CT_OUT reg fields */
  206. #define NPCM_I2CT_OUT_TO_CKDIV GENMASK(5, 0)
  207. #define NPCM_I2CT_OUT_T_OUTIE BIT(6)
  208. #define NPCM_I2CT_OUT_T_OUTST BIT(7)
  209. /* NPCM_I2CTXF_STS reg fields */
  210. #define NPCM_I2CTXF_STS_TX_THST BIT(6)
  211. /* NPCM_I2CRXF_STS reg fields */
  212. #define NPCM_I2CRXF_STS_RX_THST BIT(6)
  213. /* NPCM_I2CFIF_CTL reg fields */
  214. #define NPCM_I2CFIF_CTL_FIFO_EN BIT(4)
  215. /* NPCM_I2CRXF_CTL reg fields */
  216. #define NPCM_I2CRXF_CTL_THR_RXIE BIT(6)
  217. #define MAX_I2C_HW_FIFO_SIZE 32
  218. /* I2C_VER reg fields */
  219. #define I2C_VER_VERSION GENMASK(6, 0)
  220. #define I2C_VER_FIFO_EN BIT(7)
  221. /* stall/stuck timeout in us */
  222. #define DEFAULT_STALL_COUNT 25
  223. /* SCLFRQ field position */
  224. #define SCLFRQ_0_TO_6 GENMASK(6, 0)
  225. #define SCLFRQ_7_TO_8 GENMASK(8, 7)
  226. /* supported clk settings. values in Hz. */
  227. #define I2C_FREQ_MIN_HZ 10000
  228. #define I2C_FREQ_MAX_HZ I2C_MAX_FAST_MODE_PLUS_FREQ
  229. struct npcm_i2c_data {
  230. u8 fifo_size;
  231. u32 segctl_init_val;
  232. u8 txf_sts_tx_bytes;
  233. u8 rxf_sts_rx_bytes;
  234. u8 rxf_ctl_last_pec;
  235. };
  236. static const struct npcm_i2c_data npxm7xx_i2c_data = {
  237. .fifo_size = 16,
  238. .segctl_init_val = 0x0333F000,
  239. .txf_sts_tx_bytes = GENMASK(4, 0),
  240. .rxf_sts_rx_bytes = GENMASK(4, 0),
  241. .rxf_ctl_last_pec = BIT(5),
  242. };
  243. static const struct npcm_i2c_data npxm8xx_i2c_data = {
  244. .fifo_size = 32,
  245. .segctl_init_val = 0x9333F000,
  246. .txf_sts_tx_bytes = GENMASK(5, 0),
  247. .rxf_sts_rx_bytes = GENMASK(5, 0),
  248. .rxf_ctl_last_pec = BIT(7),
  249. };
  250. /* Status of one I2C module */
  251. struct npcm_i2c {
  252. struct i2c_adapter adap;
  253. struct device *dev;
  254. unsigned char __iomem *reg;
  255. const struct npcm_i2c_data *data;
  256. spinlock_t lock; /* IRQ synchronization */
  257. struct completion cmd_complete;
  258. int cmd_err;
  259. struct i2c_msg *msgs;
  260. int msgs_num;
  261. int num;
  262. u32 apb_clk;
  263. struct i2c_bus_recovery_info rinfo;
  264. enum i2c_state state;
  265. enum i2c_oper operation;
  266. enum i2c_mode master_or_slave;
  267. enum i2c_state_ind stop_ind;
  268. u8 dest_addr;
  269. u8 *rd_buf;
  270. u16 rd_size;
  271. u16 rd_ind;
  272. u8 *wr_buf;
  273. u16 wr_size;
  274. u16 wr_ind;
  275. bool fifo_use;
  276. u16 PEC_mask; /* PEC bit mask per slave address */
  277. bool PEC_use;
  278. bool read_block_use;
  279. unsigned long int_time_stamp;
  280. unsigned long bus_freq; /* in Hz */
  281. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  282. u8 own_slave_addr;
  283. struct i2c_client *slave;
  284. int slv_rd_size;
  285. int slv_rd_ind;
  286. int slv_wr_size;
  287. int slv_wr_ind;
  288. u8 slv_rd_buf[MAX_I2C_HW_FIFO_SIZE];
  289. u8 slv_wr_buf[MAX_I2C_HW_FIFO_SIZE];
  290. #endif
  291. struct dentry *debugfs; /* debugfs device directory */
  292. u64 ber_cnt;
  293. u64 rec_succ_cnt;
  294. u64 rec_fail_cnt;
  295. u64 nack_cnt;
  296. u64 timeout_cnt;
  297. u64 tx_complete_cnt;
  298. };
  299. static inline void npcm_i2c_select_bank(struct npcm_i2c *bus,
  300. enum i2c_bank bank)
  301. {
  302. u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3);
  303. if (bank == I2C_BANK_0)
  304. i2cctl3 = i2cctl3 & ~I2CCTL3_BNK_SEL;
  305. else
  306. i2cctl3 = i2cctl3 | I2CCTL3_BNK_SEL;
  307. iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3);
  308. }
  309. static void npcm_i2c_init_params(struct npcm_i2c *bus)
  310. {
  311. bus->stop_ind = I2C_NO_STATUS_IND;
  312. bus->rd_size = 0;
  313. bus->wr_size = 0;
  314. bus->rd_ind = 0;
  315. bus->wr_ind = 0;
  316. bus->read_block_use = false;
  317. bus->int_time_stamp = 0;
  318. bus->PEC_use = false;
  319. bus->PEC_mask = 0;
  320. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  321. if (bus->slave)
  322. bus->master_or_slave = I2C_SLAVE;
  323. #endif
  324. }
  325. static inline void npcm_i2c_wr_byte(struct npcm_i2c *bus, u8 data)
  326. {
  327. iowrite8(data, bus->reg + NPCM_I2CSDA);
  328. }
  329. static inline u8 npcm_i2c_rd_byte(struct npcm_i2c *bus)
  330. {
  331. return ioread8(bus->reg + NPCM_I2CSDA);
  332. }
  333. static int npcm_i2c_get_SCL(struct i2c_adapter *_adap)
  334. {
  335. struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
  336. return !!(I2CCTL3_SCL_LVL & ioread8(bus->reg + NPCM_I2CCTL3));
  337. }
  338. static int npcm_i2c_get_SDA(struct i2c_adapter *_adap)
  339. {
  340. struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
  341. return !!(I2CCTL3_SDA_LVL & ioread8(bus->reg + NPCM_I2CCTL3));
  342. }
  343. static inline u16 npcm_i2c_get_index(struct npcm_i2c *bus)
  344. {
  345. if (bus->operation == I2C_READ_OPER)
  346. return bus->rd_ind;
  347. if (bus->operation == I2C_WRITE_OPER)
  348. return bus->wr_ind;
  349. return 0;
  350. }
  351. /* quick protocol (just address) */
  352. static inline bool npcm_i2c_is_quick(struct npcm_i2c *bus)
  353. {
  354. return bus->wr_size == 0 && bus->rd_size == 0;
  355. }
  356. static void npcm_i2c_disable(struct npcm_i2c *bus)
  357. {
  358. u8 i2cctl2;
  359. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  360. int i;
  361. /* Slave addresses removal */
  362. for (i = I2C_SLAVE_ADDR1; i < I2C_NUM_OWN_ADDR_SUPPORTED; i++)
  363. iowrite8(0, bus->reg + npcm_i2caddr[i]);
  364. #endif
  365. /* Disable module */
  366. i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2);
  367. i2cctl2 = i2cctl2 & ~I2CCTL2_ENABLE;
  368. iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2);
  369. bus->state = I2C_DISABLE;
  370. }
  371. static void npcm_i2c_enable(struct npcm_i2c *bus)
  372. {
  373. u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2);
  374. i2cctl2 = i2cctl2 | I2CCTL2_ENABLE;
  375. iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2);
  376. bus->state = I2C_IDLE;
  377. }
  378. /* enable\disable end of busy (EOB) interrupts */
  379. static inline void npcm_i2c_eob_int(struct npcm_i2c *bus, bool enable)
  380. {
  381. u8 val;
  382. /* Clear EO_BUSY pending bit: */
  383. val = ioread8(bus->reg + NPCM_I2CCST3);
  384. val = val | NPCM_I2CCST3_EO_BUSY;
  385. iowrite8(val, bus->reg + NPCM_I2CCST3);
  386. val = ioread8(bus->reg + NPCM_I2CCTL1);
  387. val &= ~NPCM_I2CCTL1_RWS;
  388. if (enable)
  389. val |= NPCM_I2CCTL1_EOBINTE;
  390. else
  391. val &= ~NPCM_I2CCTL1_EOBINTE;
  392. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  393. }
  394. static inline bool npcm_i2c_tx_fifo_empty(struct npcm_i2c *bus)
  395. {
  396. u8 tx_fifo_sts;
  397. tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS);
  398. /* check if TX FIFO is not empty */
  399. if ((tx_fifo_sts & bus->data->txf_sts_tx_bytes) == 0)
  400. return false;
  401. /* check if TX FIFO status bit is set: */
  402. return !!FIELD_GET(NPCM_I2CTXF_STS_TX_THST, tx_fifo_sts);
  403. }
  404. static inline bool npcm_i2c_rx_fifo_full(struct npcm_i2c *bus)
  405. {
  406. u8 rx_fifo_sts;
  407. rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS);
  408. /* check if RX FIFO is not empty: */
  409. if ((rx_fifo_sts & bus->data->rxf_sts_rx_bytes) == 0)
  410. return false;
  411. /* check if rx fifo full status is set: */
  412. return !!FIELD_GET(NPCM_I2CRXF_STS_RX_THST, rx_fifo_sts);
  413. }
  414. static inline void npcm_i2c_clear_fifo_int(struct npcm_i2c *bus)
  415. {
  416. u8 val;
  417. val = ioread8(bus->reg + NPCM_I2CFIF_CTS);
  418. val = (val & NPCM_I2CFIF_CTS_SLVRSTR) | NPCM_I2CFIF_CTS_RXF_TXE;
  419. iowrite8(val, bus->reg + NPCM_I2CFIF_CTS);
  420. }
  421. static inline void npcm_i2c_clear_tx_fifo(struct npcm_i2c *bus)
  422. {
  423. u8 val;
  424. val = ioread8(bus->reg + NPCM_I2CTXF_STS);
  425. val = val | NPCM_I2CTXF_STS_TX_THST;
  426. iowrite8(val, bus->reg + NPCM_I2CTXF_STS);
  427. }
  428. static inline void npcm_i2c_clear_rx_fifo(struct npcm_i2c *bus)
  429. {
  430. u8 val;
  431. val = ioread8(bus->reg + NPCM_I2CRXF_STS);
  432. val = val | NPCM_I2CRXF_STS_RX_THST;
  433. iowrite8(val, bus->reg + NPCM_I2CRXF_STS);
  434. }
  435. static void npcm_i2c_int_enable(struct npcm_i2c *bus, bool enable)
  436. {
  437. u8 val;
  438. val = ioread8(bus->reg + NPCM_I2CCTL1);
  439. val &= ~NPCM_I2CCTL1_RWS;
  440. if (enable)
  441. val |= NPCM_I2CCTL1_INTEN;
  442. else
  443. val &= ~NPCM_I2CCTL1_INTEN;
  444. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  445. }
  446. static inline void npcm_i2c_master_start(struct npcm_i2c *bus)
  447. {
  448. u8 val;
  449. val = ioread8(bus->reg + NPCM_I2CCTL1);
  450. val &= ~(NPCM_I2CCTL1_STOP | NPCM_I2CCTL1_ACK);
  451. val |= NPCM_I2CCTL1_START;
  452. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  453. }
  454. static inline void npcm_i2c_master_stop(struct npcm_i2c *bus)
  455. {
  456. u8 val;
  457. /*
  458. * override HW issue: I2C may fail to supply stop condition in Master
  459. * Write operation.
  460. * Need to delay at least 5 us from the last int, before issueing a stop
  461. */
  462. udelay(10); /* function called from interrupt, can't sleep */
  463. val = ioread8(bus->reg + NPCM_I2CCTL1);
  464. val &= ~(NPCM_I2CCTL1_START | NPCM_I2CCTL1_ACK);
  465. val |= NPCM_I2CCTL1_STOP;
  466. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  467. if (!bus->fifo_use)
  468. return;
  469. npcm_i2c_select_bank(bus, I2C_BANK_1);
  470. if (bus->operation == I2C_READ_OPER)
  471. npcm_i2c_clear_rx_fifo(bus);
  472. else
  473. npcm_i2c_clear_tx_fifo(bus);
  474. npcm_i2c_clear_fifo_int(bus);
  475. iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
  476. }
  477. static inline void npcm_i2c_stall_after_start(struct npcm_i2c *bus, bool stall)
  478. {
  479. u8 val;
  480. val = ioread8(bus->reg + NPCM_I2CCTL1);
  481. val &= ~NPCM_I2CCTL1_RWS;
  482. if (stall)
  483. val |= NPCM_I2CCTL1_STASTRE;
  484. else
  485. val &= ~NPCM_I2CCTL1_STASTRE;
  486. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  487. }
  488. static inline void npcm_i2c_nack(struct npcm_i2c *bus)
  489. {
  490. u8 val;
  491. val = ioread8(bus->reg + NPCM_I2CCTL1);
  492. val &= ~(NPCM_I2CCTL1_STOP | NPCM_I2CCTL1_START);
  493. val |= NPCM_I2CCTL1_ACK;
  494. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  495. }
  496. static inline void npcm_i2c_clear_master_status(struct npcm_i2c *bus)
  497. {
  498. u8 val;
  499. /* Clear NEGACK, STASTR and BER bits */
  500. val = NPCM_I2CST_BER | NPCM_I2CST_NEGACK | NPCM_I2CST_STASTR;
  501. iowrite8(val, bus->reg + NPCM_I2CST);
  502. }
  503. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  504. static void npcm_i2c_slave_int_enable(struct npcm_i2c *bus, bool enable)
  505. {
  506. u8 i2cctl1;
  507. /* enable interrupt on slave match: */
  508. i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1);
  509. i2cctl1 &= ~NPCM_I2CCTL1_RWS;
  510. if (enable)
  511. i2cctl1 |= NPCM_I2CCTL1_NMINTE;
  512. else
  513. i2cctl1 &= ~NPCM_I2CCTL1_NMINTE;
  514. iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1);
  515. }
  516. static int npcm_i2c_slave_enable(struct npcm_i2c *bus, enum i2c_addr addr_type,
  517. u8 addr, bool enable)
  518. {
  519. u8 i2cctl1;
  520. u8 i2cctl3;
  521. u8 sa_reg;
  522. sa_reg = (addr & 0x7F) | FIELD_PREP(NPCM_I2CADDR_SAEN, enable);
  523. if (addr_type == I2C_GC_ADDR) {
  524. i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1);
  525. if (enable)
  526. i2cctl1 |= NPCM_I2CCTL1_GCMEN;
  527. else
  528. i2cctl1 &= ~NPCM_I2CCTL1_GCMEN;
  529. iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1);
  530. return 0;
  531. } else if (addr_type == I2C_ARP_ADDR) {
  532. i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3);
  533. if (enable)
  534. i2cctl3 |= I2CCTL3_ARPMEN;
  535. else
  536. i2cctl3 &= ~I2CCTL3_ARPMEN;
  537. iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3);
  538. return 0;
  539. }
  540. if (addr_type > I2C_SLAVE_ADDR2 && addr_type <= I2C_SLAVE_ADDR10)
  541. dev_err(bus->dev, "try to enable more than 2 SA not supported\n");
  542. if (addr_type >= I2C_ARP_ADDR)
  543. return -EFAULT;
  544. /* Set and enable the address */
  545. iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]);
  546. npcm_i2c_slave_int_enable(bus, enable);
  547. return 0;
  548. }
  549. #endif
  550. static void npcm_i2c_reset(struct npcm_i2c *bus)
  551. {
  552. /*
  553. * Save I2CCTL1 relevant bits. It is being cleared when the module
  554. * is disabled.
  555. */
  556. u8 i2cctl1;
  557. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  558. u8 addr;
  559. #endif
  560. i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1);
  561. npcm_i2c_disable(bus);
  562. npcm_i2c_enable(bus);
  563. /* Restore NPCM_I2CCTL1 Status */
  564. i2cctl1 &= ~NPCM_I2CCTL1_RWS;
  565. iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1);
  566. /* Clear BB (BUS BUSY) bit */
  567. iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
  568. iowrite8(0xFF, bus->reg + NPCM_I2CST);
  569. /* Clear and disable EOB */
  570. npcm_i2c_eob_int(bus, false);
  571. /* Clear all fifo bits: */
  572. iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS);
  573. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  574. if (bus->slave) {
  575. addr = bus->slave->addr;
  576. npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, addr, true);
  577. }
  578. #endif
  579. /* Clear status bits for spurious interrupts */
  580. npcm_i2c_clear_master_status(bus);
  581. bus->state = I2C_IDLE;
  582. }
  583. static inline bool npcm_i2c_is_master(struct npcm_i2c *bus)
  584. {
  585. return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST));
  586. }
  587. static void npcm_i2c_callback(struct npcm_i2c *bus,
  588. enum i2c_state_ind op_status, u16 info)
  589. {
  590. struct i2c_msg *msgs;
  591. int msgs_num;
  592. bool do_complete = false;
  593. msgs = bus->msgs;
  594. msgs_num = bus->msgs_num;
  595. /*
  596. * check that transaction was not timed-out, and msgs still
  597. * holds a valid value.
  598. */
  599. if (!msgs)
  600. return;
  601. if (completion_done(&bus->cmd_complete))
  602. return;
  603. switch (op_status) {
  604. case I2C_MASTER_DONE_IND:
  605. bus->cmd_err = bus->msgs_num;
  606. if (bus->tx_complete_cnt < ULLONG_MAX)
  607. bus->tx_complete_cnt++;
  608. fallthrough;
  609. case I2C_BLOCK_BYTES_ERR_IND:
  610. /* Master tx finished and all transmit bytes were sent */
  611. if (bus->msgs) {
  612. if (msgs[0].flags & I2C_M_RD)
  613. msgs[0].len = info;
  614. else if (msgs_num == 2 &&
  615. msgs[1].flags & I2C_M_RD)
  616. msgs[1].len = info;
  617. }
  618. do_complete = true;
  619. break;
  620. case I2C_NACK_IND:
  621. /* MASTER transmit got a NACK before tx all bytes */
  622. bus->cmd_err = -ENXIO;
  623. do_complete = true;
  624. break;
  625. case I2C_BUS_ERR_IND:
  626. /* Bus error */
  627. bus->cmd_err = -EAGAIN;
  628. do_complete = true;
  629. break;
  630. case I2C_WAKE_UP_IND:
  631. /* I2C wake up */
  632. break;
  633. default:
  634. break;
  635. }
  636. bus->operation = I2C_NO_OPER;
  637. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  638. if (bus->slave)
  639. bus->master_or_slave = I2C_SLAVE;
  640. #endif
  641. if (do_complete)
  642. complete(&bus->cmd_complete);
  643. }
  644. static u8 npcm_i2c_fifo_usage(struct npcm_i2c *bus)
  645. {
  646. if (bus->operation == I2C_WRITE_OPER)
  647. return (bus->data->txf_sts_tx_bytes &
  648. ioread8(bus->reg + NPCM_I2CTXF_STS));
  649. if (bus->operation == I2C_READ_OPER)
  650. return (bus->data->rxf_sts_rx_bytes &
  651. ioread8(bus->reg + NPCM_I2CRXF_STS));
  652. return 0;
  653. }
  654. static void npcm_i2c_write_to_fifo_master(struct npcm_i2c *bus, u16 max_bytes)
  655. {
  656. u8 size_free_fifo;
  657. /*
  658. * Fill the FIFO, while the FIFO is not full and there are more bytes
  659. * to write
  660. */
  661. size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus);
  662. while (max_bytes-- && size_free_fifo) {
  663. if (bus->wr_ind < bus->wr_size)
  664. npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]);
  665. else
  666. npcm_i2c_wr_byte(bus, 0xFF);
  667. size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus);
  668. }
  669. }
  670. /*
  671. * npcm_i2c_set_fifo:
  672. * configure the FIFO before using it. If nread is -1 RX FIFO will not be
  673. * configured. same for nwrite
  674. */
  675. static void npcm_i2c_set_fifo(struct npcm_i2c *bus, int nread, int nwrite)
  676. {
  677. u8 rxf_ctl = 0;
  678. if (!bus->fifo_use)
  679. return;
  680. npcm_i2c_select_bank(bus, I2C_BANK_1);
  681. npcm_i2c_clear_tx_fifo(bus);
  682. npcm_i2c_clear_rx_fifo(bus);
  683. /* configure RX FIFO */
  684. if (nread > 0) {
  685. rxf_ctl = min_t(int, nread, bus->data->fifo_size);
  686. /* set LAST bit. if LAST is set next FIFO packet is nacked */
  687. if (nread <= bus->data->fifo_size)
  688. rxf_ctl |= bus->data->rxf_ctl_last_pec;
  689. /*
  690. * if we are about to read the first byte in blk rd mode,
  691. * don't NACK it. If slave returns zero size HW can't NACK
  692. * it immediately, it will read extra byte and then NACK.
  693. */
  694. if (bus->rd_ind == 0 && bus->read_block_use) {
  695. /* set fifo to read one byte, no last: */
  696. rxf_ctl = 1;
  697. }
  698. /* set fifo size: */
  699. iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL);
  700. }
  701. /* configure TX FIFO */
  702. if (nwrite > 0) {
  703. if (nwrite > bus->data->fifo_size)
  704. /* data to send is more then FIFO size. */
  705. iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CTXF_CTL);
  706. else
  707. iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL);
  708. npcm_i2c_clear_tx_fifo(bus);
  709. }
  710. }
  711. static void npcm_i2c_read_fifo(struct npcm_i2c *bus, u8 bytes_in_fifo)
  712. {
  713. u8 data;
  714. while (bytes_in_fifo--) {
  715. data = npcm_i2c_rd_byte(bus);
  716. if (bus->rd_ind < bus->rd_size)
  717. bus->rd_buf[bus->rd_ind++] = data;
  718. }
  719. }
  720. static void npcm_i2c_master_abort(struct npcm_i2c *bus)
  721. {
  722. /* Only current master is allowed to issue a stop condition */
  723. if (!npcm_i2c_is_master(bus))
  724. return;
  725. npcm_i2c_eob_int(bus, true);
  726. npcm_i2c_master_stop(bus);
  727. npcm_i2c_clear_master_status(bus);
  728. }
  729. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  730. static u8 npcm_i2c_get_slave_addr(struct npcm_i2c *bus, enum i2c_addr addr_type)
  731. {
  732. u8 slave_add;
  733. if (addr_type > I2C_SLAVE_ADDR2 && addr_type <= I2C_SLAVE_ADDR10)
  734. dev_err(bus->dev, "get slave: try to use more than 2 SA not supported\n");
  735. slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]);
  736. return slave_add;
  737. }
  738. static int npcm_i2c_remove_slave_addr(struct npcm_i2c *bus, u8 slave_add)
  739. {
  740. int i;
  741. /* Set the enable bit */
  742. slave_add |= 0x80;
  743. for (i = I2C_SLAVE_ADDR1; i < I2C_NUM_OWN_ADDR_SUPPORTED; i++) {
  744. if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add)
  745. iowrite8(0, bus->reg + npcm_i2caddr[i]);
  746. }
  747. return 0;
  748. }
  749. static void npcm_i2c_write_fifo_slave(struct npcm_i2c *bus, u16 max_bytes)
  750. {
  751. /*
  752. * Fill the FIFO, while the FIFO is not full and there are more bytes
  753. * to write
  754. */
  755. npcm_i2c_clear_fifo_int(bus);
  756. npcm_i2c_clear_tx_fifo(bus);
  757. iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
  758. while (max_bytes-- && bus->data->fifo_size != npcm_i2c_fifo_usage(bus)) {
  759. if (bus->slv_wr_size <= 0)
  760. break;
  761. bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1);
  762. npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]);
  763. bus->slv_wr_ind++;
  764. bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1);
  765. bus->slv_wr_size--;
  766. }
  767. }
  768. static void npcm_i2c_read_fifo_slave(struct npcm_i2c *bus, u8 bytes_in_fifo)
  769. {
  770. u8 data;
  771. if (!bus->slave)
  772. return;
  773. while (bytes_in_fifo--) {
  774. data = npcm_i2c_rd_byte(bus);
  775. bus->slv_rd_ind = bus->slv_rd_ind & (bus->data->fifo_size - 1);
  776. bus->slv_rd_buf[bus->slv_rd_ind] = data;
  777. bus->slv_rd_ind++;
  778. /* 1st byte is length in block protocol: */
  779. if (bus->slv_rd_ind == 1 && bus->read_block_use)
  780. bus->slv_rd_size = data + bus->PEC_use + 1;
  781. }
  782. }
  783. static int npcm_i2c_slave_get_wr_buf(struct npcm_i2c *bus)
  784. {
  785. int i;
  786. u8 value;
  787. int ind;
  788. int ret = bus->slv_wr_ind;
  789. /* fill a cyclic buffer */
  790. for (i = 0; i < bus->data->fifo_size; i++) {
  791. if (bus->slv_wr_size >= bus->data->fifo_size)
  792. break;
  793. if (bus->state == I2C_SLAVE_MATCH) {
  794. i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value);
  795. bus->state = I2C_OPER_STARTED;
  796. } else {
  797. i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value);
  798. }
  799. ind = (bus->slv_wr_ind + bus->slv_wr_size) & (bus->data->fifo_size - 1);
  800. bus->slv_wr_buf[ind] = value;
  801. bus->slv_wr_size++;
  802. }
  803. return bus->data->fifo_size - ret;
  804. }
  805. static void npcm_i2c_slave_send_rd_buf(struct npcm_i2c *bus)
  806. {
  807. int i;
  808. for (i = 0; i < bus->slv_rd_ind; i++)
  809. i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED,
  810. &bus->slv_rd_buf[i]);
  811. /*
  812. * once we send bytes up, need to reset the counter of the wr buf
  813. * got data from master (new offset in device), ignore wr fifo:
  814. */
  815. if (bus->slv_rd_ind) {
  816. bus->slv_wr_size = 0;
  817. bus->slv_wr_ind = 0;
  818. }
  819. bus->slv_rd_ind = 0;
  820. bus->slv_rd_size = bus->adap.quirks->max_read_len;
  821. npcm_i2c_clear_fifo_int(bus);
  822. npcm_i2c_clear_rx_fifo(bus);
  823. }
  824. static void npcm_i2c_slave_receive(struct npcm_i2c *bus, u16 nread,
  825. u8 *read_data)
  826. {
  827. bus->state = I2C_OPER_STARTED;
  828. bus->operation = I2C_READ_OPER;
  829. bus->slv_rd_size = nread;
  830. bus->slv_rd_ind = 0;
  831. iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
  832. iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL);
  833. npcm_i2c_clear_tx_fifo(bus);
  834. npcm_i2c_clear_rx_fifo(bus);
  835. }
  836. static void npcm_i2c_slave_xmit(struct npcm_i2c *bus, u16 nwrite,
  837. u8 *write_data)
  838. {
  839. if (nwrite == 0)
  840. return;
  841. bus->operation = I2C_WRITE_OPER;
  842. /* get the next buffer */
  843. npcm_i2c_slave_get_wr_buf(bus);
  844. npcm_i2c_write_fifo_slave(bus, nwrite);
  845. }
  846. /*
  847. * npcm_i2c_slave_wr_buf_sync:
  848. * currently slave IF only supports single byte operations.
  849. * in order to utilize the npcm HW FIFO, the driver will ask for 16 bytes
  850. * at a time, pack them in buffer, and then transmit them all together
  851. * to the FIFO and onward to the bus.
  852. * NACK on read will be once reached to bus->adap->quirks->max_read_len.
  853. * sending a NACK wherever the backend requests for it is not supported.
  854. * the next two functions allow reading to local buffer before writing it all
  855. * to the HW FIFO.
  856. */
  857. static void npcm_i2c_slave_wr_buf_sync(struct npcm_i2c *bus)
  858. {
  859. int left_in_fifo;
  860. left_in_fifo = bus->data->txf_sts_tx_bytes &
  861. ioread8(bus->reg + NPCM_I2CTXF_STS);
  862. /* fifo already full: */
  863. if (left_in_fifo >= bus->data->fifo_size ||
  864. bus->slv_wr_size >= bus->data->fifo_size)
  865. return;
  866. /* update the wr fifo index back to the untransmitted bytes: */
  867. bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo;
  868. bus->slv_wr_size = bus->slv_wr_size + left_in_fifo;
  869. if (bus->slv_wr_ind < 0)
  870. bus->slv_wr_ind += bus->data->fifo_size;
  871. }
  872. static void npcm_i2c_slave_rd_wr(struct npcm_i2c *bus)
  873. {
  874. if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) {
  875. /*
  876. * Slave got an address match with direction bit 1 so it should
  877. * transmit data. Write till the master will NACK
  878. */
  879. bus->operation = I2C_WRITE_OPER;
  880. npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len,
  881. bus->slv_wr_buf);
  882. } else {
  883. /*
  884. * Slave got an address match with direction bit 0 so it should
  885. * receive data.
  886. * this module does not support saying no to bytes.
  887. * it will always ACK.
  888. */
  889. bus->operation = I2C_READ_OPER;
  890. npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus));
  891. bus->stop_ind = I2C_SLAVE_RCV_IND;
  892. npcm_i2c_slave_send_rd_buf(bus);
  893. npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len,
  894. bus->slv_rd_buf);
  895. }
  896. }
  897. static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus)
  898. {
  899. u8 val;
  900. irqreturn_t ret = IRQ_NONE;
  901. u8 i2cst = ioread8(bus->reg + NPCM_I2CST);
  902. /* Slave: A NACK has occurred */
  903. if (NPCM_I2CST_NEGACK & i2cst) {
  904. bus->stop_ind = I2C_NACK_IND;
  905. npcm_i2c_slave_wr_buf_sync(bus);
  906. if (bus->fifo_use)
  907. /* clear the FIFO */
  908. iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO,
  909. bus->reg + NPCM_I2CFIF_CTS);
  910. /* In slave write, NACK is OK, otherwise it is a problem */
  911. bus->stop_ind = I2C_NO_STATUS_IND;
  912. bus->operation = I2C_NO_OPER;
  913. bus->own_slave_addr = 0xFF;
  914. /*
  915. * Slave has to wait for STOP to decide this is the end
  916. * of the transaction. tx is not yet considered as done
  917. */
  918. iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST);
  919. ret = IRQ_HANDLED;
  920. }
  921. /* Slave mode: a Bus Error (BER) has been identified */
  922. if (NPCM_I2CST_BER & i2cst) {
  923. /*
  924. * Check whether bus arbitration or Start or Stop during data
  925. * xfer bus arbitration problem should not result in recovery
  926. */
  927. bus->stop_ind = I2C_BUS_ERR_IND;
  928. /* wait for bus busy before clear fifo */
  929. iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS);
  930. bus->state = I2C_IDLE;
  931. /*
  932. * in BER case we might get 2 interrupts: one for slave one for
  933. * master ( for a channel which is master\slave switching)
  934. */
  935. if (completion_done(&bus->cmd_complete) == false) {
  936. bus->cmd_err = -EIO;
  937. complete(&bus->cmd_complete);
  938. }
  939. bus->own_slave_addr = 0xFF;
  940. iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST);
  941. ret = IRQ_HANDLED;
  942. }
  943. /* A Slave Stop Condition has been identified */
  944. if (NPCM_I2CST_SLVSTP & i2cst) {
  945. u8 bytes_in_fifo = npcm_i2c_fifo_usage(bus);
  946. bus->stop_ind = I2C_SLAVE_DONE_IND;
  947. if (bus->operation == I2C_READ_OPER)
  948. npcm_i2c_read_fifo_slave(bus, bytes_in_fifo);
  949. /* if the buffer is empty nothing will be sent */
  950. npcm_i2c_slave_send_rd_buf(bus);
  951. /* Slave done transmitting or receiving */
  952. bus->stop_ind = I2C_NO_STATUS_IND;
  953. /*
  954. * Note, just because we got here, it doesn't mean we through
  955. * away the wr buffer.
  956. * we keep it until the next received offset.
  957. */
  958. bus->operation = I2C_NO_OPER;
  959. bus->own_slave_addr = 0xFF;
  960. i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0);
  961. iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST);
  962. if (bus->fifo_use) {
  963. npcm_i2c_clear_fifo_int(bus);
  964. npcm_i2c_clear_rx_fifo(bus);
  965. npcm_i2c_clear_tx_fifo(bus);
  966. iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO,
  967. bus->reg + NPCM_I2CFIF_CTS);
  968. }
  969. bus->state = I2C_IDLE;
  970. ret = IRQ_HANDLED;
  971. }
  972. /* restart condition occurred and Rx-FIFO was not empty */
  973. if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR,
  974. ioread8(bus->reg + NPCM_I2CFIF_CTS))) {
  975. bus->stop_ind = I2C_SLAVE_RESTART_IND;
  976. bus->master_or_slave = I2C_SLAVE;
  977. if (bus->operation == I2C_READ_OPER)
  978. npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus));
  979. bus->operation = I2C_WRITE_OPER;
  980. iowrite8(0, bus->reg + NPCM_I2CRXF_CTL);
  981. val = NPCM_I2CFIF_CTS_CLR_FIFO | NPCM_I2CFIF_CTS_SLVRSTR |
  982. NPCM_I2CFIF_CTS_RXF_TXE;
  983. iowrite8(val, bus->reg + NPCM_I2CFIF_CTS);
  984. npcm_i2c_slave_rd_wr(bus);
  985. ret = IRQ_HANDLED;
  986. }
  987. /* A Slave Address Match has been identified */
  988. if (NPCM_I2CST_NMATCH & i2cst) {
  989. u8 info = 0;
  990. /* Address match automatically implies slave mode */
  991. bus->master_or_slave = I2C_SLAVE;
  992. npcm_i2c_clear_fifo_int(bus);
  993. npcm_i2c_clear_rx_fifo(bus);
  994. npcm_i2c_clear_tx_fifo(bus);
  995. iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
  996. iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL);
  997. if (NPCM_I2CST_XMIT & i2cst) {
  998. bus->operation = I2C_WRITE_OPER;
  999. } else {
  1000. i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED,
  1001. &info);
  1002. bus->operation = I2C_READ_OPER;
  1003. }
  1004. if (bus->own_slave_addr == 0xFF) {
  1005. /* Check which type of address match */
  1006. val = ioread8(bus->reg + NPCM_I2CCST);
  1007. if (NPCM_I2CCST_MATCH & val) {
  1008. u16 addr;
  1009. enum i2c_addr eaddr;
  1010. u8 i2ccst2;
  1011. u8 i2ccst3;
  1012. i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3);
  1013. i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2);
  1014. /*
  1015. * the i2c module can response to 10 own SA.
  1016. * check which one was addressed by the master.
  1017. * respond to the first one.
  1018. */
  1019. addr = ((i2ccst3 & 0x07) << 7) |
  1020. (i2ccst2 & 0x7F);
  1021. info = ffs(addr);
  1022. eaddr = (enum i2c_addr)info;
  1023. addr = npcm_i2c_get_slave_addr(bus, eaddr);
  1024. addr &= 0x7F;
  1025. bus->own_slave_addr = addr;
  1026. if (bus->PEC_mask & BIT(info))
  1027. bus->PEC_use = true;
  1028. else
  1029. bus->PEC_use = false;
  1030. } else {
  1031. if (NPCM_I2CCST_GCMATCH & val)
  1032. bus->own_slave_addr = 0;
  1033. if (NPCM_I2CCST_ARPMATCH & val)
  1034. bus->own_slave_addr = 0x61;
  1035. }
  1036. } else {
  1037. /*
  1038. * Slave match can happen in two options:
  1039. * 1. Start, SA, read (slave read without further ado)
  1040. * 2. Start, SA, read, data, restart, SA, read, ...
  1041. * (slave read in fragmented mode)
  1042. * 3. Start, SA, write, data, restart, SA, read, ..
  1043. * (regular write-read mode)
  1044. */
  1045. if ((bus->state == I2C_OPER_STARTED &&
  1046. bus->operation == I2C_READ_OPER &&
  1047. bus->stop_ind == I2C_SLAVE_XMIT_IND) ||
  1048. bus->stop_ind == I2C_SLAVE_RCV_IND) {
  1049. /* slave tx after slave rx w/o STOP */
  1050. bus->stop_ind = I2C_SLAVE_RESTART_IND;
  1051. }
  1052. }
  1053. if (NPCM_I2CST_XMIT & i2cst)
  1054. bus->stop_ind = I2C_SLAVE_XMIT_IND;
  1055. else
  1056. bus->stop_ind = I2C_SLAVE_RCV_IND;
  1057. bus->state = I2C_SLAVE_MATCH;
  1058. npcm_i2c_slave_rd_wr(bus);
  1059. iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST);
  1060. ret = IRQ_HANDLED;
  1061. }
  1062. /* Slave SDA status is set - tx or rx */
  1063. if ((NPCM_I2CST_SDAST & i2cst) ||
  1064. (bus->fifo_use &&
  1065. (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) {
  1066. npcm_i2c_slave_rd_wr(bus);
  1067. iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST);
  1068. ret = IRQ_HANDLED;
  1069. } /* SDAST */
  1070. /*
  1071. * If irq is not one of the above, make sure EOB is disabled and all
  1072. * status bits are cleared.
  1073. */
  1074. if (ret == IRQ_NONE) {
  1075. npcm_i2c_eob_int(bus, false);
  1076. npcm_i2c_clear_master_status(bus);
  1077. }
  1078. return IRQ_HANDLED;
  1079. }
  1080. static int npcm_i2c_reg_slave(struct i2c_client *client)
  1081. {
  1082. unsigned long lock_flags;
  1083. struct npcm_i2c *bus = i2c_get_adapdata(client->adapter);
  1084. bus->slave = client;
  1085. if (!bus->slave)
  1086. return -EINVAL;
  1087. if (client->flags & I2C_CLIENT_TEN)
  1088. return -EAFNOSUPPORT;
  1089. spin_lock_irqsave(&bus->lock, lock_flags);
  1090. npcm_i2c_init_params(bus);
  1091. bus->slv_rd_size = 0;
  1092. bus->slv_wr_size = 0;
  1093. bus->slv_rd_ind = 0;
  1094. bus->slv_wr_ind = 0;
  1095. if (client->flags & I2C_CLIENT_PEC)
  1096. bus->PEC_use = true;
  1097. dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num,
  1098. client->addr, bus->PEC_use);
  1099. npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true);
  1100. npcm_i2c_clear_fifo_int(bus);
  1101. npcm_i2c_clear_rx_fifo(bus);
  1102. npcm_i2c_clear_tx_fifo(bus);
  1103. npcm_i2c_slave_int_enable(bus, true);
  1104. spin_unlock_irqrestore(&bus->lock, lock_flags);
  1105. return 0;
  1106. }
  1107. static int npcm_i2c_unreg_slave(struct i2c_client *client)
  1108. {
  1109. struct npcm_i2c *bus = client->adapter->algo_data;
  1110. unsigned long lock_flags;
  1111. spin_lock_irqsave(&bus->lock, lock_flags);
  1112. if (!bus->slave) {
  1113. spin_unlock_irqrestore(&bus->lock, lock_flags);
  1114. return -EINVAL;
  1115. }
  1116. npcm_i2c_slave_int_enable(bus, false);
  1117. npcm_i2c_remove_slave_addr(bus, client->addr);
  1118. bus->slave = NULL;
  1119. spin_unlock_irqrestore(&bus->lock, lock_flags);
  1120. return 0;
  1121. }
  1122. #endif /* CONFIG_I2C_SLAVE */
  1123. static void npcm_i2c_master_fifo_read(struct npcm_i2c *bus)
  1124. {
  1125. int rcount;
  1126. int fifo_bytes;
  1127. enum i2c_state_ind ind = I2C_MASTER_DONE_IND;
  1128. fifo_bytes = npcm_i2c_fifo_usage(bus);
  1129. rcount = bus->rd_size - bus->rd_ind;
  1130. /*
  1131. * In order not to change the RX_TRH during transaction (we found that
  1132. * this might be problematic if it takes too much time to read the FIFO)
  1133. * we read the data in the following way. If the number of bytes to
  1134. * read == FIFO Size + C (where C < FIFO Size)then first read C bytes
  1135. * and in the next int we read rest of the data.
  1136. */
  1137. if (rcount < (2 * bus->data->fifo_size) && rcount > bus->data->fifo_size)
  1138. fifo_bytes = rcount - bus->data->fifo_size;
  1139. if (rcount <= fifo_bytes) {
  1140. /* last bytes are about to be read - end of tx */
  1141. bus->state = I2C_STOP_PENDING;
  1142. bus->stop_ind = ind;
  1143. npcm_i2c_eob_int(bus, true);
  1144. /* Stop should be set before reading last byte. */
  1145. npcm_i2c_master_stop(bus);
  1146. npcm_i2c_read_fifo(bus, fifo_bytes);
  1147. } else {
  1148. npcm_i2c_read_fifo(bus, fifo_bytes);
  1149. rcount = bus->rd_size - bus->rd_ind;
  1150. npcm_i2c_set_fifo(bus, rcount, -1);
  1151. }
  1152. }
  1153. static void npcm_i2c_irq_master_handler_write(struct npcm_i2c *bus)
  1154. {
  1155. u16 wcount;
  1156. if (bus->fifo_use)
  1157. npcm_i2c_clear_tx_fifo(bus); /* clear the TX fifo status bit */
  1158. /* Master write operation - last byte handling */
  1159. if (bus->wr_ind == bus->wr_size) {
  1160. if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0)
  1161. /*
  1162. * No more bytes to send (to add to the FIFO),
  1163. * however the FIFO is not empty yet. It is
  1164. * still in the middle of tx. Currently there's nothing
  1165. * to do except for waiting to the end of the tx
  1166. * We will get an int when the FIFO will get empty.
  1167. */
  1168. return;
  1169. if (bus->rd_size == 0) {
  1170. /* all bytes have been written, in wr only operation */
  1171. npcm_i2c_eob_int(bus, true);
  1172. bus->state = I2C_STOP_PENDING;
  1173. bus->stop_ind = I2C_MASTER_DONE_IND;
  1174. npcm_i2c_master_stop(bus);
  1175. /* Clear SDA Status bit (by writing dummy byte) */
  1176. npcm_i2c_wr_byte(bus, 0xFF);
  1177. } else {
  1178. /* last write-byte written on previous int - restart */
  1179. npcm_i2c_set_fifo(bus, bus->rd_size, -1);
  1180. /* Generate repeated start upon next write to SDA */
  1181. npcm_i2c_master_start(bus);
  1182. /*
  1183. * Receiving one byte only - stall after successful
  1184. * completion of send address byte. If we NACK here, and
  1185. * slave doesn't ACK the address, we might
  1186. * unintentionally NACK the next multi-byte read.
  1187. */
  1188. if (bus->rd_size == 1)
  1189. npcm_i2c_stall_after_start(bus, true);
  1190. /* Next int will occur on read */
  1191. bus->operation = I2C_READ_OPER;
  1192. /* send the slave address in read direction */
  1193. npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1);
  1194. }
  1195. } else {
  1196. /* write next byte not last byte and not slave address */
  1197. if (!bus->fifo_use || bus->wr_size == 1) {
  1198. npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]);
  1199. } else {
  1200. wcount = bus->wr_size - bus->wr_ind;
  1201. npcm_i2c_set_fifo(bus, -1, wcount);
  1202. if (wcount)
  1203. npcm_i2c_write_to_fifo_master(bus, wcount);
  1204. }
  1205. }
  1206. }
  1207. static void npcm_i2c_irq_master_handler_read(struct npcm_i2c *bus)
  1208. {
  1209. u16 block_extra_bytes_size;
  1210. u8 data;
  1211. /* added bytes to the packet: */
  1212. block_extra_bytes_size = bus->read_block_use + bus->PEC_use;
  1213. /*
  1214. * Perform master read, distinguishing between last byte and the rest of
  1215. * the bytes. The last byte should be read when the clock is stopped
  1216. */
  1217. if (bus->rd_ind == 0) { /* first byte handling: */
  1218. if (bus->read_block_use) {
  1219. /* first byte in block protocol is the size: */
  1220. data = npcm_i2c_rd_byte(bus);
  1221. data = clamp_val(data, 1, I2C_SMBUS_BLOCK_MAX);
  1222. bus->rd_size = data + block_extra_bytes_size;
  1223. bus->rd_buf[bus->rd_ind++] = data;
  1224. /* clear RX FIFO interrupt status: */
  1225. if (bus->fifo_use) {
  1226. data = ioread8(bus->reg + NPCM_I2CFIF_CTS);
  1227. data = data | NPCM_I2CFIF_CTS_RXF_TXE;
  1228. iowrite8(data, bus->reg + NPCM_I2CFIF_CTS);
  1229. }
  1230. npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1);
  1231. npcm_i2c_stall_after_start(bus, false);
  1232. } else {
  1233. npcm_i2c_clear_tx_fifo(bus);
  1234. npcm_i2c_master_fifo_read(bus);
  1235. }
  1236. } else {
  1237. if (bus->rd_size == block_extra_bytes_size &&
  1238. bus->read_block_use) {
  1239. bus->state = I2C_STOP_PENDING;
  1240. bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND;
  1241. bus->cmd_err = -EIO;
  1242. npcm_i2c_eob_int(bus, true);
  1243. npcm_i2c_master_stop(bus);
  1244. npcm_i2c_read_fifo(bus, npcm_i2c_fifo_usage(bus));
  1245. } else {
  1246. npcm_i2c_master_fifo_read(bus);
  1247. }
  1248. }
  1249. }
  1250. static void npcm_i2c_irq_handle_nmatch(struct npcm_i2c *bus)
  1251. {
  1252. iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST);
  1253. npcm_i2c_nack(bus);
  1254. bus->stop_ind = I2C_BUS_ERR_IND;
  1255. npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus));
  1256. }
  1257. /* A NACK has occurred */
  1258. static void npcm_i2c_irq_handle_nack(struct npcm_i2c *bus)
  1259. {
  1260. u8 val;
  1261. if (bus->nack_cnt < ULLONG_MAX)
  1262. bus->nack_cnt++;
  1263. if (bus->fifo_use) {
  1264. /*
  1265. * if there are still untransmitted bytes in TX FIFO
  1266. * reduce them from wr_ind
  1267. */
  1268. if (bus->operation == I2C_WRITE_OPER)
  1269. bus->wr_ind -= npcm_i2c_fifo_usage(bus);
  1270. /* clear the FIFO */
  1271. iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS);
  1272. }
  1273. /* In master write operation, got unexpected NACK */
  1274. bus->stop_ind = I2C_NACK_IND;
  1275. /* Only current master is allowed to issue Stop Condition */
  1276. if (npcm_i2c_is_master(bus)) {
  1277. /* stopping in the middle */
  1278. npcm_i2c_eob_int(bus, false);
  1279. npcm_i2c_master_stop(bus);
  1280. /* Clear SDA Status bit (by reading dummy byte) */
  1281. npcm_i2c_rd_byte(bus);
  1282. /*
  1283. * The bus is released from stall only after the SW clears
  1284. * NEGACK bit. Then a Stop condition is sent.
  1285. */
  1286. npcm_i2c_clear_master_status(bus);
  1287. readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val,
  1288. !(val & NPCM_I2CCST_BUSY), 10, 200);
  1289. /* Verify no status bits are still set after bus is released */
  1290. npcm_i2c_clear_master_status(bus);
  1291. }
  1292. bus->state = I2C_IDLE;
  1293. /*
  1294. * In Master mode, NACK should be cleared only after STOP.
  1295. * In such case, the bus is released from stall only after the
  1296. * software clears NACK bit. Then a Stop condition is sent.
  1297. */
  1298. npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind);
  1299. }
  1300. /* Master mode: a Bus Error has been identified */
  1301. static void npcm_i2c_irq_handle_ber(struct npcm_i2c *bus)
  1302. {
  1303. if (bus->ber_cnt < ULLONG_MAX)
  1304. bus->ber_cnt++;
  1305. bus->stop_ind = I2C_BUS_ERR_IND;
  1306. if (npcm_i2c_is_master(bus)) {
  1307. npcm_i2c_master_abort(bus);
  1308. } else {
  1309. npcm_i2c_clear_master_status(bus);
  1310. /* Clear BB (BUS BUSY) bit */
  1311. iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
  1312. bus->cmd_err = -EAGAIN;
  1313. npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus));
  1314. }
  1315. bus->state = I2C_IDLE;
  1316. }
  1317. /* EOB: a master End Of Busy (meaning STOP completed) */
  1318. static void npcm_i2c_irq_handle_eob(struct npcm_i2c *bus)
  1319. {
  1320. npcm_i2c_eob_int(bus, false);
  1321. bus->state = I2C_IDLE;
  1322. npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind);
  1323. }
  1324. /* Address sent and requested stall occurred (Master mode) */
  1325. static void npcm_i2c_irq_handle_stall_after_start(struct npcm_i2c *bus)
  1326. {
  1327. if (npcm_i2c_is_quick(bus)) {
  1328. bus->state = I2C_STOP_PENDING;
  1329. bus->stop_ind = I2C_MASTER_DONE_IND;
  1330. npcm_i2c_eob_int(bus, true);
  1331. npcm_i2c_master_stop(bus);
  1332. } else if ((bus->rd_size == 1) && !bus->read_block_use) {
  1333. /*
  1334. * Receiving one byte only - set NACK after ensuring
  1335. * slave ACKed the address byte.
  1336. */
  1337. npcm_i2c_nack(bus);
  1338. }
  1339. /* Reset stall-after-address-byte */
  1340. npcm_i2c_stall_after_start(bus, false);
  1341. /* Clear stall only after setting STOP */
  1342. iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST);
  1343. }
  1344. /* SDA status is set - TX or RX, master */
  1345. static void npcm_i2c_irq_handle_sda(struct npcm_i2c *bus, u8 i2cst)
  1346. {
  1347. u8 fif_cts;
  1348. if (!npcm_i2c_is_master(bus))
  1349. return;
  1350. if (bus->state == I2C_IDLE) {
  1351. bus->stop_ind = I2C_WAKE_UP_IND;
  1352. if (npcm_i2c_is_quick(bus) || bus->read_block_use)
  1353. /*
  1354. * Need to stall after successful
  1355. * completion of sending address byte
  1356. */
  1357. npcm_i2c_stall_after_start(bus, true);
  1358. else
  1359. npcm_i2c_stall_after_start(bus, false);
  1360. /*
  1361. * Receiving one byte only - stall after successful completion
  1362. * of sending address byte If we NACK here, and slave doesn't
  1363. * ACK the address, we might unintentionally NACK the next
  1364. * multi-byte read
  1365. */
  1366. if (bus->wr_size == 0 && bus->rd_size == 1)
  1367. npcm_i2c_stall_after_start(bus, true);
  1368. /* Initiate I2C master tx */
  1369. /* select bank 1 for FIFO regs */
  1370. npcm_i2c_select_bank(bus, I2C_BANK_1);
  1371. fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS);
  1372. fif_cts = fif_cts & ~NPCM_I2CFIF_CTS_SLVRSTR;
  1373. /* clear FIFO and relevant status bits. */
  1374. fif_cts = fif_cts | NPCM_I2CFIF_CTS_CLR_FIFO;
  1375. iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS);
  1376. /* re-enable */
  1377. fif_cts = fif_cts | NPCM_I2CFIF_CTS_RXF_TXE;
  1378. iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS);
  1379. /*
  1380. * Configure the FIFO threshold:
  1381. * according to the needed # of bytes to read.
  1382. * Note: due to HW limitation can't config the rx fifo before it
  1383. * got and ACK on the restart. LAST bit will not be reset unless
  1384. * RX completed. It will stay set on the next tx.
  1385. */
  1386. if (bus->wr_size)
  1387. npcm_i2c_set_fifo(bus, -1, bus->wr_size);
  1388. else
  1389. npcm_i2c_set_fifo(bus, bus->rd_size, -1);
  1390. bus->state = I2C_OPER_STARTED;
  1391. if (npcm_i2c_is_quick(bus) || bus->wr_size)
  1392. npcm_i2c_wr_byte(bus, bus->dest_addr);
  1393. else
  1394. npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0));
  1395. /* SDA interrupt, after start\restart */
  1396. } else {
  1397. if (NPCM_I2CST_XMIT & i2cst) {
  1398. bus->operation = I2C_WRITE_OPER;
  1399. npcm_i2c_irq_master_handler_write(bus);
  1400. } else {
  1401. bus->operation = I2C_READ_OPER;
  1402. npcm_i2c_irq_master_handler_read(bus);
  1403. }
  1404. }
  1405. }
  1406. static int npcm_i2c_int_master_handler(struct npcm_i2c *bus)
  1407. {
  1408. u8 i2cst;
  1409. int ret = -EIO;
  1410. i2cst = ioread8(bus->reg + NPCM_I2CST);
  1411. if (FIELD_GET(NPCM_I2CST_NMATCH, i2cst)) {
  1412. npcm_i2c_irq_handle_nmatch(bus);
  1413. return 0;
  1414. }
  1415. /* A NACK has occurred */
  1416. if (FIELD_GET(NPCM_I2CST_NEGACK, i2cst)) {
  1417. npcm_i2c_irq_handle_nack(bus);
  1418. return 0;
  1419. }
  1420. /* Master mode: a Bus Error has been identified */
  1421. if (FIELD_GET(NPCM_I2CST_BER, i2cst)) {
  1422. npcm_i2c_irq_handle_ber(bus);
  1423. return 0;
  1424. }
  1425. /* EOB: a master End Of Busy (meaning STOP completed) */
  1426. if ((FIELD_GET(NPCM_I2CCTL1_EOBINTE,
  1427. ioread8(bus->reg + NPCM_I2CCTL1)) == 1) &&
  1428. (FIELD_GET(NPCM_I2CCST3_EO_BUSY,
  1429. ioread8(bus->reg + NPCM_I2CCST3)))) {
  1430. npcm_i2c_irq_handle_eob(bus);
  1431. return 0;
  1432. }
  1433. /* Address sent and requested stall occurred (Master mode) */
  1434. if (FIELD_GET(NPCM_I2CST_STASTR, i2cst)) {
  1435. npcm_i2c_irq_handle_stall_after_start(bus);
  1436. ret = 0;
  1437. }
  1438. /* SDA status is set - TX or RX, master */
  1439. if (FIELD_GET(NPCM_I2CST_SDAST, i2cst) ||
  1440. (bus->fifo_use &&
  1441. (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) {
  1442. npcm_i2c_irq_handle_sda(bus, i2cst);
  1443. ret = 0;
  1444. }
  1445. return ret;
  1446. }
  1447. /* recovery using TGCLK functionality of the module */
  1448. static int npcm_i2c_recovery_tgclk(struct i2c_adapter *_adap)
  1449. {
  1450. u8 val;
  1451. u8 fif_cts;
  1452. bool done = false;
  1453. int status = -ENOTRECOVERABLE;
  1454. struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
  1455. /* Allow 3 bytes (27 toggles) to be read from the slave: */
  1456. int iter = 27;
  1457. if ((npcm_i2c_get_SDA(_adap) == 1) && (npcm_i2c_get_SCL(_adap) == 1)) {
  1458. dev_dbg(bus->dev, "bus%d-0x%x recovery skipped, bus not stuck",
  1459. bus->num, bus->dest_addr);
  1460. npcm_i2c_reset(bus);
  1461. return 0;
  1462. }
  1463. npcm_i2c_int_enable(bus, false);
  1464. npcm_i2c_disable(bus);
  1465. npcm_i2c_enable(bus);
  1466. iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
  1467. npcm_i2c_clear_tx_fifo(bus);
  1468. npcm_i2c_clear_rx_fifo(bus);
  1469. iowrite8(0, bus->reg + NPCM_I2CRXF_CTL);
  1470. iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
  1471. npcm_i2c_stall_after_start(bus, false);
  1472. /* select bank 1 for FIFO regs */
  1473. npcm_i2c_select_bank(bus, I2C_BANK_1);
  1474. /* clear FIFO and relevant status bits. */
  1475. fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS);
  1476. fif_cts &= ~NPCM_I2CFIF_CTS_SLVRSTR;
  1477. fif_cts |= NPCM_I2CFIF_CTS_CLR_FIFO;
  1478. iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS);
  1479. npcm_i2c_set_fifo(bus, -1, 0);
  1480. /* Repeat the following sequence until SDA is released */
  1481. do {
  1482. /* Issue a single SCL toggle */
  1483. iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST);
  1484. usleep_range(20, 30);
  1485. /* If SDA line is inactive (high), stop */
  1486. if (npcm_i2c_get_SDA(_adap)) {
  1487. done = true;
  1488. status = 0;
  1489. }
  1490. } while (!done && iter--);
  1491. /* If SDA line is released: send start-addr-stop, to re-sync. */
  1492. if (npcm_i2c_get_SDA(_adap)) {
  1493. /* Send an address byte in write direction: */
  1494. npcm_i2c_wr_byte(bus, bus->dest_addr);
  1495. npcm_i2c_master_start(bus);
  1496. /* Wait until START condition is sent */
  1497. status = readx_poll_timeout(npcm_i2c_get_SCL, _adap, val, !val,
  1498. 20, 200);
  1499. /* If START condition was sent */
  1500. if (npcm_i2c_is_master(bus) > 0) {
  1501. usleep_range(20, 30);
  1502. npcm_i2c_master_stop(bus);
  1503. usleep_range(200, 500);
  1504. }
  1505. }
  1506. npcm_i2c_reset(bus);
  1507. npcm_i2c_int_enable(bus, true);
  1508. if ((npcm_i2c_get_SDA(_adap) == 1) && (npcm_i2c_get_SCL(_adap) == 1))
  1509. status = 0;
  1510. else
  1511. status = -ENOTRECOVERABLE;
  1512. if (status) {
  1513. if (bus->rec_fail_cnt < ULLONG_MAX)
  1514. bus->rec_fail_cnt++;
  1515. } else {
  1516. if (bus->rec_succ_cnt < ULLONG_MAX)
  1517. bus->rec_succ_cnt++;
  1518. }
  1519. return status;
  1520. }
  1521. /* recovery using bit banging functionality of the module */
  1522. static void npcm_i2c_recovery_init(struct i2c_adapter *_adap)
  1523. {
  1524. struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
  1525. struct i2c_bus_recovery_info *rinfo = &bus->rinfo;
  1526. rinfo->recover_bus = npcm_i2c_recovery_tgclk;
  1527. /*
  1528. * npcm i2c HW allows direct reading of SCL and SDA.
  1529. * However, it does not support setting SCL and SDA directly.
  1530. * The recovery function can toggle SCL when SDA is low (but not set)
  1531. * Getter functions used internally, and can be used externally.
  1532. */
  1533. rinfo->get_scl = npcm_i2c_get_SCL;
  1534. rinfo->get_sda = npcm_i2c_get_SDA;
  1535. _adap->bus_recovery_info = rinfo;
  1536. }
  1537. /* SCLFRQ min/max field values */
  1538. #define SCLFRQ_MIN 10
  1539. #define SCLFRQ_MAX 511
  1540. #define clk_coef(freq, mul) DIV_ROUND_UP((freq) * (mul), 1000000)
  1541. /*
  1542. * npcm_i2c_init_clk: init HW timing parameters.
  1543. * NPCM7XX i2c module timing parameters are dependent on module core clk (APB)
  1544. * and bus frequency.
  1545. * 100kHz bus requires tSCL = 4 * SCLFRQ * tCLK. LT and HT are symmetric.
  1546. * 400kHz bus requires asymmetric HT and LT. A different equation is recommended
  1547. * by the HW designer, given core clock range (equations in comments below).
  1548. *
  1549. */
  1550. static int npcm_i2c_init_clk(struct npcm_i2c *bus, u32 bus_freq_hz)
  1551. {
  1552. u32 k1 = 0;
  1553. u32 k2 = 0;
  1554. u8 dbnct = 0;
  1555. u32 sclfrq = 0;
  1556. u8 hldt = 7;
  1557. u8 fast_mode = 0;
  1558. u32 src_clk_khz;
  1559. u32 bus_freq_khz;
  1560. src_clk_khz = bus->apb_clk / 1000;
  1561. bus_freq_khz = bus_freq_hz / 1000;
  1562. bus->bus_freq = bus_freq_hz;
  1563. /* 100KHz and below: */
  1564. if (bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ) {
  1565. sclfrq = src_clk_khz / (bus_freq_khz * 4);
  1566. if (sclfrq < SCLFRQ_MIN || sclfrq > SCLFRQ_MAX)
  1567. return -EDOM;
  1568. if (src_clk_khz >= 40000)
  1569. hldt = 17;
  1570. else if (src_clk_khz >= 12500)
  1571. hldt = 15;
  1572. else
  1573. hldt = 7;
  1574. }
  1575. /* 400KHz: */
  1576. else if (bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ) {
  1577. sclfrq = 0;
  1578. fast_mode = I2CCTL3_400K_MODE;
  1579. if (src_clk_khz < 7500)
  1580. /* 400KHZ cannot be supported for core clock < 7.5MHz */
  1581. return -EDOM;
  1582. else if (src_clk_khz >= 50000) {
  1583. k1 = 80;
  1584. k2 = 48;
  1585. hldt = 12;
  1586. dbnct = 7;
  1587. }
  1588. /* Master or Slave with frequency > 25MHz */
  1589. else if (src_clk_khz > 25000) {
  1590. hldt = clk_coef(src_clk_khz, 300) + 7;
  1591. k1 = clk_coef(src_clk_khz, 1600);
  1592. k2 = clk_coef(src_clk_khz, 900);
  1593. }
  1594. }
  1595. /* 1MHz: */
  1596. else if (bus_freq_hz <= I2C_MAX_FAST_MODE_PLUS_FREQ) {
  1597. sclfrq = 0;
  1598. fast_mode = I2CCTL3_400K_MODE;
  1599. /* 1MHZ cannot be supported for core clock < 24 MHz */
  1600. if (src_clk_khz < 24000)
  1601. return -EDOM;
  1602. k1 = clk_coef(src_clk_khz, 620);
  1603. k2 = clk_coef(src_clk_khz, 380);
  1604. /* Core clk > 40 MHz */
  1605. if (src_clk_khz > 40000) {
  1606. /*
  1607. * Set HLDT:
  1608. * SDA hold time: (HLDT-7) * T(CLK) >= 120
  1609. * HLDT = 120/T(CLK) + 7 = 120 * FREQ(CLK) + 7
  1610. */
  1611. hldt = clk_coef(src_clk_khz, 120) + 7;
  1612. } else {
  1613. hldt = 7;
  1614. dbnct = 2;
  1615. }
  1616. }
  1617. /* Frequency larger than 1 MHz is not supported */
  1618. else
  1619. return -EINVAL;
  1620. if (bus_freq_hz >= I2C_MAX_FAST_MODE_FREQ) {
  1621. k1 = round_up(k1, 2);
  1622. k2 = round_up(k2 + 1, 2);
  1623. if (k1 < SCLFRQ_MIN || k1 > SCLFRQ_MAX ||
  1624. k2 < SCLFRQ_MIN || k2 > SCLFRQ_MAX)
  1625. return -EDOM;
  1626. }
  1627. /* write sclfrq value. bits [6:0] are in I2CCTL2 reg */
  1628. iowrite8(FIELD_PREP(I2CCTL2_SCLFRQ6_0, sclfrq & 0x7F),
  1629. bus->reg + NPCM_I2CCTL2);
  1630. /* bits [8:7] are in I2CCTL3 reg */
  1631. iowrite8(fast_mode | FIELD_PREP(I2CCTL3_SCLFRQ8_7, (sclfrq >> 7) & 0x3),
  1632. bus->reg + NPCM_I2CCTL3);
  1633. /* Select Bank 0 to access NPCM_I2CCTL4/NPCM_I2CCTL5 */
  1634. npcm_i2c_select_bank(bus, I2C_BANK_0);
  1635. if (bus_freq_hz >= I2C_MAX_FAST_MODE_FREQ) {
  1636. /*
  1637. * Set SCL Low/High Time:
  1638. * k1 = 2 * SCLLT7-0 -> Low Time = k1 / 2
  1639. * k2 = 2 * SCLLT7-0 -> High Time = k2 / 2
  1640. */
  1641. iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT);
  1642. iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT);
  1643. iowrite8(dbnct, bus->reg + NPCM_I2CCTL5);
  1644. }
  1645. iowrite8(hldt, bus->reg + NPCM_I2CCTL4);
  1646. /* Return to Bank 1, and stay there by default: */
  1647. npcm_i2c_select_bank(bus, I2C_BANK_1);
  1648. return 0;
  1649. }
  1650. static int npcm_i2c_init_module(struct npcm_i2c *bus, enum i2c_mode mode,
  1651. u32 bus_freq_hz)
  1652. {
  1653. u8 val;
  1654. int ret;
  1655. /* Check whether module already enabled or frequency is out of bounds */
  1656. if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) ||
  1657. bus_freq_hz < I2C_FREQ_MIN_HZ || bus_freq_hz > I2C_FREQ_MAX_HZ)
  1658. return -EINVAL;
  1659. npcm_i2c_int_enable(bus, false);
  1660. npcm_i2c_disable(bus);
  1661. /* Configure FIFO mode : */
  1662. if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) {
  1663. bus->fifo_use = true;
  1664. npcm_i2c_select_bank(bus, I2C_BANK_0);
  1665. val = ioread8(bus->reg + NPCM_I2CFIF_CTL);
  1666. val |= NPCM_I2CFIF_CTL_FIFO_EN;
  1667. iowrite8(val, bus->reg + NPCM_I2CFIF_CTL);
  1668. npcm_i2c_select_bank(bus, I2C_BANK_1);
  1669. } else {
  1670. bus->fifo_use = false;
  1671. }
  1672. /* Configure I2C module clock frequency */
  1673. ret = npcm_i2c_init_clk(bus, bus_freq_hz);
  1674. if (ret) {
  1675. dev_err(bus->dev, "npcm_i2c_init_clk failed\n");
  1676. return ret;
  1677. }
  1678. /* Enable module (before configuring CTL1) */
  1679. npcm_i2c_enable(bus);
  1680. bus->state = I2C_IDLE;
  1681. val = ioread8(bus->reg + NPCM_I2CCTL1);
  1682. val = (val | NPCM_I2CCTL1_NMINTE) & ~NPCM_I2CCTL1_RWS;
  1683. iowrite8(val, bus->reg + NPCM_I2CCTL1);
  1684. npcm_i2c_reset(bus);
  1685. /* Check HW is OK: SDA and SCL should be high at this point. */
  1686. if ((npcm_i2c_get_SDA(&bus->adap) == 0) || (npcm_i2c_get_SCL(&bus->adap) == 0)) {
  1687. dev_err(bus->dev, "I2C%d init fail: lines are low\n", bus->num);
  1688. dev_err(bus->dev, "SDA=%d SCL=%d\n", npcm_i2c_get_SDA(&bus->adap),
  1689. npcm_i2c_get_SCL(&bus->adap));
  1690. return -ENXIO;
  1691. }
  1692. npcm_i2c_int_enable(bus, true);
  1693. return 0;
  1694. }
  1695. static int __npcm_i2c_init(struct npcm_i2c *bus, struct platform_device *pdev)
  1696. {
  1697. u32 clk_freq_hz;
  1698. int ret;
  1699. /* Initialize the internal data structures */
  1700. bus->state = I2C_DISABLE;
  1701. bus->master_or_slave = I2C_SLAVE;
  1702. bus->int_time_stamp = 0;
  1703. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  1704. bus->slave = NULL;
  1705. #endif
  1706. ret = device_property_read_u32(&pdev->dev, "clock-frequency",
  1707. &clk_freq_hz);
  1708. if (ret) {
  1709. dev_info(&pdev->dev, "Could not read clock-frequency property");
  1710. clk_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
  1711. }
  1712. ret = npcm_i2c_init_module(bus, I2C_MASTER, clk_freq_hz);
  1713. if (ret) {
  1714. dev_err(&pdev->dev, "npcm_i2c_init_module failed\n");
  1715. return ret;
  1716. }
  1717. return 0;
  1718. }
  1719. static irqreturn_t npcm_i2c_bus_irq(int irq, void *dev_id)
  1720. {
  1721. struct npcm_i2c *bus = dev_id;
  1722. if (npcm_i2c_is_master(bus))
  1723. bus->master_or_slave = I2C_MASTER;
  1724. if (bus->master_or_slave == I2C_MASTER) {
  1725. bus->int_time_stamp = jiffies;
  1726. if (!npcm_i2c_int_master_handler(bus))
  1727. return IRQ_HANDLED;
  1728. }
  1729. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  1730. if (bus->slave) {
  1731. bus->master_or_slave = I2C_SLAVE;
  1732. if (npcm_i2c_int_slave_handler(bus))
  1733. return IRQ_HANDLED;
  1734. }
  1735. #endif
  1736. /* Clear status bits for spurious interrupts */
  1737. npcm_i2c_clear_master_status(bus);
  1738. return IRQ_HANDLED;
  1739. }
  1740. static bool npcm_i2c_master_start_xmit(struct npcm_i2c *bus,
  1741. u8 slave_addr, u16 nwrite, u16 nread,
  1742. u8 *write_data, u8 *read_data,
  1743. bool use_PEC, bool use_read_block)
  1744. {
  1745. if (bus->state != I2C_IDLE) {
  1746. bus->cmd_err = -EBUSY;
  1747. return false;
  1748. }
  1749. bus->dest_addr = slave_addr << 1;
  1750. bus->wr_buf = write_data;
  1751. bus->wr_size = nwrite;
  1752. bus->wr_ind = 0;
  1753. bus->rd_buf = read_data;
  1754. bus->rd_size = nread;
  1755. bus->rd_ind = 0;
  1756. bus->PEC_use = 0;
  1757. /* for tx PEC is appended to buffer from i2c IF. PEC flag is ignored */
  1758. if (nread)
  1759. bus->PEC_use = use_PEC;
  1760. bus->read_block_use = use_read_block;
  1761. if (nread && !nwrite)
  1762. bus->operation = I2C_READ_OPER;
  1763. else
  1764. bus->operation = I2C_WRITE_OPER;
  1765. if (bus->fifo_use) {
  1766. u8 i2cfif_cts;
  1767. npcm_i2c_select_bank(bus, I2C_BANK_1);
  1768. /* clear FIFO and relevant status bits. */
  1769. i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS);
  1770. i2cfif_cts &= ~NPCM_I2CFIF_CTS_SLVRSTR;
  1771. i2cfif_cts |= NPCM_I2CFIF_CTS_CLR_FIFO;
  1772. iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS);
  1773. }
  1774. bus->state = I2C_IDLE;
  1775. npcm_i2c_stall_after_start(bus, true);
  1776. npcm_i2c_master_start(bus);
  1777. return true;
  1778. }
  1779. static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  1780. int num)
  1781. {
  1782. struct npcm_i2c *bus = container_of(adap, struct npcm_i2c, adap);
  1783. struct i2c_msg *msg0, *msg1;
  1784. unsigned long time_left, flags;
  1785. u16 nwrite, nread;
  1786. u8 *write_data, *read_data;
  1787. u8 slave_addr;
  1788. unsigned long timeout;
  1789. bool read_block = false;
  1790. bool read_PEC = false;
  1791. u8 bus_busy;
  1792. unsigned long timeout_usec;
  1793. if (bus->state == I2C_DISABLE) {
  1794. dev_err(bus->dev, "I2C%d module is disabled", bus->num);
  1795. return -EINVAL;
  1796. }
  1797. msg0 = &msgs[0];
  1798. slave_addr = msg0->addr;
  1799. if (msg0->flags & I2C_M_RD) { /* read */
  1800. nwrite = 0;
  1801. write_data = NULL;
  1802. read_data = msg0->buf;
  1803. if (msg0->flags & I2C_M_RECV_LEN) {
  1804. nread = 1;
  1805. read_block = true;
  1806. if (msg0->flags & I2C_CLIENT_PEC)
  1807. read_PEC = true;
  1808. } else {
  1809. nread = msg0->len;
  1810. }
  1811. } else { /* write */
  1812. nwrite = msg0->len;
  1813. write_data = msg0->buf;
  1814. nread = 0;
  1815. read_data = NULL;
  1816. if (num == 2) {
  1817. msg1 = &msgs[1];
  1818. read_data = msg1->buf;
  1819. if (msg1->flags & I2C_M_RECV_LEN) {
  1820. nread = 1;
  1821. read_block = true;
  1822. if (msg1->flags & I2C_CLIENT_PEC)
  1823. read_PEC = true;
  1824. } else {
  1825. nread = msg1->len;
  1826. read_block = false;
  1827. }
  1828. }
  1829. }
  1830. /*
  1831. * Adaptive TimeOut: estimated time in usec + 100% margin:
  1832. * 2: double the timeout for clock stretching case
  1833. * 9: bits per transaction (including the ack/nack)
  1834. */
  1835. timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite);
  1836. timeout = max_t(unsigned long, bus->adap.timeout, usecs_to_jiffies(timeout_usec));
  1837. if (nwrite >= 32 * 1024 || nread >= 32 * 1024) {
  1838. dev_err(bus->dev, "i2c%d buffer too big\n", bus->num);
  1839. return -EINVAL;
  1840. }
  1841. time_left = jiffies + timeout + 1;
  1842. do {
  1843. /*
  1844. * we must clear slave address immediately when the bus is not
  1845. * busy, so we spinlock it, but we don't keep the lock for the
  1846. * entire while since it is too long.
  1847. */
  1848. spin_lock_irqsave(&bus->lock, flags);
  1849. bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB;
  1850. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  1851. if (!bus_busy && bus->slave)
  1852. iowrite8((bus->slave->addr & 0x7F),
  1853. bus->reg + NPCM_I2CADDR1);
  1854. #endif
  1855. spin_unlock_irqrestore(&bus->lock, flags);
  1856. } while (time_is_after_jiffies(time_left) && bus_busy);
  1857. if (bus_busy) {
  1858. iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
  1859. npcm_i2c_reset(bus);
  1860. i2c_recover_bus(adap);
  1861. return -EAGAIN;
  1862. }
  1863. npcm_i2c_init_params(bus);
  1864. bus->dest_addr = slave_addr;
  1865. bus->msgs = msgs;
  1866. bus->msgs_num = num;
  1867. bus->cmd_err = 0;
  1868. bus->read_block_use = read_block;
  1869. reinit_completion(&bus->cmd_complete);
  1870. npcm_i2c_int_enable(bus, true);
  1871. if (npcm_i2c_master_start_xmit(bus, slave_addr, nwrite, nread,
  1872. write_data, read_data, read_PEC,
  1873. read_block)) {
  1874. time_left = wait_for_completion_timeout(&bus->cmd_complete,
  1875. timeout);
  1876. if (time_left == 0) {
  1877. if (bus->timeout_cnt < ULLONG_MAX)
  1878. bus->timeout_cnt++;
  1879. if (bus->master_or_slave == I2C_MASTER) {
  1880. i2c_recover_bus(adap);
  1881. bus->cmd_err = -EIO;
  1882. bus->state = I2C_IDLE;
  1883. }
  1884. }
  1885. }
  1886. /* if there was BER, check if need to recover the bus: */
  1887. if (bus->cmd_err == -EAGAIN)
  1888. bus->cmd_err = i2c_recover_bus(adap);
  1889. /*
  1890. * After any type of error, check if LAST bit is still set,
  1891. * due to a HW issue.
  1892. * It cannot be cleared without resetting the module.
  1893. */
  1894. else if (bus->cmd_err &&
  1895. (bus->data->rxf_ctl_last_pec & ioread8(bus->reg + NPCM_I2CRXF_CTL)))
  1896. npcm_i2c_reset(bus);
  1897. /* After any xfer, successful or not, stall and EOB must be disabled */
  1898. npcm_i2c_stall_after_start(bus, false);
  1899. npcm_i2c_eob_int(bus, false);
  1900. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  1901. /* reenable slave if it was enabled */
  1902. if (bus->slave)
  1903. iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN,
  1904. bus->reg + NPCM_I2CADDR1);
  1905. #else
  1906. npcm_i2c_int_enable(bus, false);
  1907. #endif
  1908. return bus->cmd_err;
  1909. }
  1910. static u32 npcm_i2c_functionality(struct i2c_adapter *adap)
  1911. {
  1912. return I2C_FUNC_I2C |
  1913. I2C_FUNC_SMBUS_EMUL |
  1914. I2C_FUNC_SMBUS_BLOCK_DATA |
  1915. I2C_FUNC_SMBUS_PEC |
  1916. I2C_FUNC_SLAVE;
  1917. }
  1918. static const struct i2c_adapter_quirks npcm_i2c_quirks = {
  1919. .max_read_len = 32768,
  1920. .max_write_len = 32768,
  1921. .flags = I2C_AQ_COMB_WRITE_THEN_READ,
  1922. };
  1923. static const struct i2c_algorithm npcm_i2c_algo = {
  1924. .master_xfer = npcm_i2c_master_xfer,
  1925. .functionality = npcm_i2c_functionality,
  1926. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  1927. .reg_slave = npcm_i2c_reg_slave,
  1928. .unreg_slave = npcm_i2c_unreg_slave,
  1929. #endif
  1930. };
  1931. /* i2c debugfs directory: used to keep health monitor of i2c devices */
  1932. static struct dentry *npcm_i2c_debugfs_dir;
  1933. static void npcm_i2c_init_debugfs(struct platform_device *pdev,
  1934. struct npcm_i2c *bus)
  1935. {
  1936. struct dentry *d;
  1937. if (!npcm_i2c_debugfs_dir)
  1938. return;
  1939. d = debugfs_create_dir(dev_name(&pdev->dev), npcm_i2c_debugfs_dir);
  1940. if (IS_ERR_OR_NULL(d))
  1941. return;
  1942. debugfs_create_u64("ber_cnt", 0444, d, &bus->ber_cnt);
  1943. debugfs_create_u64("nack_cnt", 0444, d, &bus->nack_cnt);
  1944. debugfs_create_u64("rec_succ_cnt", 0444, d, &bus->rec_succ_cnt);
  1945. debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt);
  1946. debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt);
  1947. debugfs_create_u64("tx_complete_cnt", 0444, d, &bus->tx_complete_cnt);
  1948. bus->debugfs = d;
  1949. }
  1950. static int npcm_i2c_probe_bus(struct platform_device *pdev)
  1951. {
  1952. struct device_node *np = pdev->dev.of_node;
  1953. static struct regmap *gcr_regmap;
  1954. struct device *dev = &pdev->dev;
  1955. struct i2c_adapter *adap;
  1956. struct npcm_i2c *bus;
  1957. struct clk *i2c_clk;
  1958. int irq;
  1959. int ret;
  1960. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  1961. if (!bus)
  1962. return -ENOMEM;
  1963. bus->dev = &pdev->dev;
  1964. bus->data = of_device_get_match_data(dev);
  1965. if (!bus->data) {
  1966. dev_err(dev, "OF data missing\n");
  1967. return -EINVAL;
  1968. }
  1969. bus->num = of_alias_get_id(pdev->dev.of_node, "i2c");
  1970. /* core clk must be acquired to calculate module timing settings */
  1971. i2c_clk = devm_clk_get(&pdev->dev, NULL);
  1972. if (IS_ERR(i2c_clk))
  1973. return PTR_ERR(i2c_clk);
  1974. bus->apb_clk = clk_get_rate(i2c_clk);
  1975. gcr_regmap = syscon_regmap_lookup_by_phandle(np, "nuvoton,sys-mgr");
  1976. if (IS_ERR(gcr_regmap))
  1977. gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
  1978. if (IS_ERR(gcr_regmap))
  1979. return PTR_ERR(gcr_regmap);
  1980. regmap_write(gcr_regmap, NPCM_I2CSEGCTL, bus->data->segctl_init_val);
  1981. bus->reg = devm_platform_ioremap_resource(pdev, 0);
  1982. if (IS_ERR(bus->reg))
  1983. return PTR_ERR(bus->reg);
  1984. spin_lock_init(&bus->lock);
  1985. init_completion(&bus->cmd_complete);
  1986. adap = &bus->adap;
  1987. adap->owner = THIS_MODULE;
  1988. adap->retries = 3;
  1989. adap->timeout = msecs_to_jiffies(35);
  1990. adap->algo = &npcm_i2c_algo;
  1991. adap->quirks = &npcm_i2c_quirks;
  1992. adap->algo_data = bus;
  1993. adap->dev.parent = &pdev->dev;
  1994. adap->dev.of_node = pdev->dev.of_node;
  1995. adap->nr = pdev->id;
  1996. irq = platform_get_irq(pdev, 0);
  1997. if (irq < 0)
  1998. return irq;
  1999. ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0,
  2000. dev_name(bus->dev), bus);
  2001. if (ret)
  2002. return ret;
  2003. ret = __npcm_i2c_init(bus, pdev);
  2004. if (ret)
  2005. return ret;
  2006. npcm_i2c_recovery_init(adap);
  2007. i2c_set_adapdata(adap, bus);
  2008. snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d",
  2009. bus->num);
  2010. ret = i2c_add_numbered_adapter(&bus->adap);
  2011. if (ret)
  2012. return ret;
  2013. platform_set_drvdata(pdev, bus);
  2014. npcm_i2c_init_debugfs(pdev, bus);
  2015. return 0;
  2016. }
  2017. static int npcm_i2c_remove_bus(struct platform_device *pdev)
  2018. {
  2019. unsigned long lock_flags;
  2020. struct npcm_i2c *bus = platform_get_drvdata(pdev);
  2021. debugfs_remove_recursive(bus->debugfs);
  2022. spin_lock_irqsave(&bus->lock, lock_flags);
  2023. npcm_i2c_disable(bus);
  2024. spin_unlock_irqrestore(&bus->lock, lock_flags);
  2025. i2c_del_adapter(&bus->adap);
  2026. return 0;
  2027. }
  2028. static const struct of_device_id npcm_i2c_bus_of_table[] = {
  2029. { .compatible = "nuvoton,npcm750-i2c", .data = &npxm7xx_i2c_data },
  2030. { .compatible = "nuvoton,npcm845-i2c", .data = &npxm8xx_i2c_data },
  2031. {}
  2032. };
  2033. MODULE_DEVICE_TABLE(of, npcm_i2c_bus_of_table);
  2034. static struct platform_driver npcm_i2c_bus_driver = {
  2035. .probe = npcm_i2c_probe_bus,
  2036. .remove = npcm_i2c_remove_bus,
  2037. .driver = {
  2038. .name = "nuvoton-i2c",
  2039. .of_match_table = npcm_i2c_bus_of_table,
  2040. }
  2041. };
  2042. static int __init npcm_i2c_init(void)
  2043. {
  2044. int ret;
  2045. npcm_i2c_debugfs_dir = debugfs_create_dir("npcm_i2c", NULL);
  2046. ret = platform_driver_register(&npcm_i2c_bus_driver);
  2047. if (ret) {
  2048. debugfs_remove_recursive(npcm_i2c_debugfs_dir);
  2049. return ret;
  2050. }
  2051. return 0;
  2052. }
  2053. module_init(npcm_i2c_init);
  2054. static void __exit npcm_i2c_exit(void)
  2055. {
  2056. platform_driver_unregister(&npcm_i2c_bus_driver);
  2057. debugfs_remove_recursive(npcm_i2c_debugfs_dir);
  2058. }
  2059. module_exit(npcm_i2c_exit);
  2060. MODULE_AUTHOR("Avi Fishman <[email protected]>");
  2061. MODULE_AUTHOR("Tali Perry <[email protected]>");
  2062. MODULE_AUTHOR("Tyrone Ting <[email protected]>");
  2063. MODULE_DESCRIPTION("Nuvoton I2C Bus Driver");
  2064. MODULE_LICENSE("GPL v2");