i2c-mlxbf.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Mellanox BlueField I2C bus driver
  4. *
  5. * Copyright (C) 2020 Mellanox Technologies, Ltd.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/i2c.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/string.h>
  20. /* Defines what functionality is present. */
  21. #define MLXBF_I2C_FUNC_SMBUS_BLOCK \
  22. (I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
  23. #define MLXBF_I2C_FUNC_SMBUS_DEFAULT \
  24. (I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA | \
  25. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_I2C_BLOCK | \
  26. I2C_FUNC_SMBUS_PROC_CALL)
  27. #define MLXBF_I2C_FUNC_ALL \
  28. (MLXBF_I2C_FUNC_SMBUS_DEFAULT | MLXBF_I2C_FUNC_SMBUS_BLOCK | \
  29. I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SLAVE)
  30. /* Shared resources info in BlueField platforms. */
  31. #define MLXBF_I2C_COALESCE_TYU_ADDR 0x02801300
  32. #define MLXBF_I2C_COALESCE_TYU_SIZE 0x010
  33. #define MLXBF_I2C_GPIO_TYU_ADDR 0x02802000
  34. #define MLXBF_I2C_GPIO_TYU_SIZE 0x100
  35. #define MLXBF_I2C_COREPLL_TYU_ADDR 0x02800358
  36. #define MLXBF_I2C_COREPLL_TYU_SIZE 0x008
  37. #define MLXBF_I2C_COREPLL_YU_ADDR 0x02800c30
  38. #define MLXBF_I2C_COREPLL_YU_SIZE 0x00c
  39. #define MLXBF_I2C_COREPLL_RSH_YU_ADDR 0x13409824
  40. #define MLXBF_I2C_COREPLL_RSH_YU_SIZE 0x00c
  41. #define MLXBF_I2C_SHARED_RES_MAX 3
  42. /*
  43. * Note that the following SMBus, CAUSE, GPIO and PLL register addresses
  44. * refer to their respective offsets relative to the corresponding
  45. * memory-mapped region whose addresses are specified in either the DT or
  46. * the ACPI tables or above.
  47. */
  48. /*
  49. * SMBus Master core clock frequency. Timing configurations are
  50. * strongly dependent on the core clock frequency of the SMBus
  51. * Master. Default value is set to 400MHz.
  52. */
  53. #define MLXBF_I2C_TYU_PLL_OUT_FREQ (400 * 1000 * 1000)
  54. /* Reference clock for Bluefield - 156 MHz. */
  55. #define MLXBF_I2C_PLL_IN_FREQ 156250000ULL
  56. /* Constant used to determine the PLL frequency. */
  57. #define MLNXBF_I2C_COREPLL_CONST 16384ULL
  58. #define MLXBF_I2C_FREQUENCY_1GHZ 1000000000ULL
  59. /* PLL registers. */
  60. #define MLXBF_I2C_CORE_PLL_REG1 0x4
  61. #define MLXBF_I2C_CORE_PLL_REG2 0x8
  62. /* OR cause register. */
  63. #define MLXBF_I2C_CAUSE_OR_EVTEN0 0x14
  64. #define MLXBF_I2C_CAUSE_OR_CLEAR 0x18
  65. /* Arbiter Cause Register. */
  66. #define MLXBF_I2C_CAUSE_ARBITER 0x1c
  67. /*
  68. * Cause Status flags. Note that those bits might be considered
  69. * as interrupt enabled bits.
  70. */
  71. /* Transaction ended with STOP. */
  72. #define MLXBF_I2C_CAUSE_TRANSACTION_ENDED BIT(0)
  73. /* Master arbitration lost. */
  74. #define MLXBF_I2C_CAUSE_M_ARBITRATION_LOST BIT(1)
  75. /* Unexpected start detected. */
  76. #define MLXBF_I2C_CAUSE_UNEXPECTED_START BIT(2)
  77. /* Unexpected stop detected. */
  78. #define MLXBF_I2C_CAUSE_UNEXPECTED_STOP BIT(3)
  79. /* Wait for transfer continuation. */
  80. #define MLXBF_I2C_CAUSE_WAIT_FOR_FW_DATA BIT(4)
  81. /* Failed to generate STOP. */
  82. #define MLXBF_I2C_CAUSE_PUT_STOP_FAILED BIT(5)
  83. /* Failed to generate START. */
  84. #define MLXBF_I2C_CAUSE_PUT_START_FAILED BIT(6)
  85. /* Clock toggle completed. */
  86. #define MLXBF_I2C_CAUSE_CLK_TOGGLE_DONE BIT(7)
  87. /* Transfer timeout occurred. */
  88. #define MLXBF_I2C_CAUSE_M_FW_TIMEOUT BIT(8)
  89. /* Master busy bit reset. */
  90. #define MLXBF_I2C_CAUSE_M_GW_BUSY_FALL BIT(9)
  91. #define MLXBF_I2C_CAUSE_MASTER_ARBITER_BITS_MASK GENMASK(9, 0)
  92. #define MLXBF_I2C_CAUSE_MASTER_STATUS_ERROR \
  93. (MLXBF_I2C_CAUSE_M_ARBITRATION_LOST | \
  94. MLXBF_I2C_CAUSE_UNEXPECTED_START | \
  95. MLXBF_I2C_CAUSE_UNEXPECTED_STOP | \
  96. MLXBF_I2C_CAUSE_PUT_STOP_FAILED | \
  97. MLXBF_I2C_CAUSE_PUT_START_FAILED | \
  98. MLXBF_I2C_CAUSE_CLK_TOGGLE_DONE | \
  99. MLXBF_I2C_CAUSE_M_FW_TIMEOUT)
  100. /*
  101. * Slave cause status flags. Note that those bits might be considered
  102. * as interrupt enabled bits.
  103. */
  104. /* Write transaction received successfully. */
  105. #define MLXBF_I2C_CAUSE_WRITE_SUCCESS BIT(0)
  106. /* Read transaction received, waiting for response. */
  107. #define MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE BIT(13)
  108. /* Slave busy bit reset. */
  109. #define MLXBF_I2C_CAUSE_S_GW_BUSY_FALL BIT(18)
  110. /* Cause coalesce registers. */
  111. #define MLXBF_I2C_CAUSE_COALESCE_0 0x00
  112. #define MLXBF_I2C_CAUSE_TYU_SLAVE_BIT 3
  113. #define MLXBF_I2C_CAUSE_YU_SLAVE_BIT 1
  114. /* Functional enable register. */
  115. #define MLXBF_I2C_GPIO_0_FUNC_EN_0 0x28
  116. /* Force OE enable register. */
  117. #define MLXBF_I2C_GPIO_0_FORCE_OE_EN 0x30
  118. /*
  119. * Note that Smbus GWs are on GPIOs 30:25. Two pins are used to control
  120. * SDA/SCL lines:
  121. *
  122. * SMBUS GW0 -> bits[26:25]
  123. * SMBUS GW1 -> bits[28:27]
  124. * SMBUS GW2 -> bits[30:29]
  125. */
  126. #define MLXBF_I2C_GPIO_SMBUS_GW_PINS(num) (25 + ((num) << 1))
  127. /* Note that gw_id can be 0,1 or 2. */
  128. #define MLXBF_I2C_GPIO_SMBUS_GW_MASK(num) \
  129. (0xffffffff & (~(0x3 << MLXBF_I2C_GPIO_SMBUS_GW_PINS(num))))
  130. #define MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(num, val) \
  131. ((val) & MLXBF_I2C_GPIO_SMBUS_GW_MASK(num))
  132. #define MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(num, val) \
  133. ((val) | (0x3 << MLXBF_I2C_GPIO_SMBUS_GW_PINS(num)))
  134. /*
  135. * Defines SMBus operating frequency and core clock frequency.
  136. * According to ADB files, default values are compliant to 100KHz SMBus
  137. * @ 400MHz core clock. The driver should be able to calculate core
  138. * frequency based on PLL parameters.
  139. */
  140. #define MLXBF_I2C_COREPLL_FREQ MLXBF_I2C_TYU_PLL_OUT_FREQ
  141. /* Core PLL TYU configuration. */
  142. #define MLXBF_I2C_COREPLL_CORE_F_TYU_MASK GENMASK(15, 3)
  143. #define MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK GENMASK(19, 16)
  144. #define MLXBF_I2C_COREPLL_CORE_R_TYU_MASK GENMASK(25, 20)
  145. /* Core PLL YU configuration. */
  146. #define MLXBF_I2C_COREPLL_CORE_F_YU_MASK GENMASK(25, 0)
  147. #define MLXBF_I2C_COREPLL_CORE_OD_YU_MASK GENMASK(3, 0)
  148. #define MLXBF_I2C_COREPLL_CORE_R_YU_MASK GENMASK(31, 26)
  149. /* SMBus timing parameters. */
  150. #define MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH 0x00
  151. #define MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE 0x04
  152. #define MLXBF_I2C_SMBUS_TIMER_THOLD 0x08
  153. #define MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP 0x0c
  154. #define MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA 0x10
  155. #define MLXBF_I2C_SMBUS_THIGH_MAX_TBUF 0x14
  156. #define MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT 0x18
  157. #define MLXBF_I2C_SHIFT_0 0
  158. #define MLXBF_I2C_SHIFT_8 8
  159. #define MLXBF_I2C_SHIFT_16 16
  160. #define MLXBF_I2C_SHIFT_24 24
  161. #define MLXBF_I2C_MASK_8 GENMASK(7, 0)
  162. #define MLXBF_I2C_MASK_16 GENMASK(15, 0)
  163. #define MLXBF_I2C_MST_ADDR_OFFSET 0x200
  164. /* SMBus Master GW. */
  165. #define MLXBF_I2C_SMBUS_MASTER_GW 0x0
  166. /* Number of bytes received and sent. */
  167. #define MLXBF_I2C_YU_SMBUS_RS_BYTES 0x100
  168. #define MLXBF_I2C_RSH_YU_SMBUS_RS_BYTES 0x10c
  169. /* Packet error check (PEC) value. */
  170. #define MLXBF_I2C_SMBUS_MASTER_PEC 0x104
  171. /* Status bits (ACK/NACK/FW Timeout). */
  172. #define MLXBF_I2C_SMBUS_MASTER_STATUS 0x108
  173. /* SMbus Master Finite State Machine. */
  174. #define MLXBF_I2C_YU_SMBUS_MASTER_FSM 0x110
  175. #define MLXBF_I2C_RSH_YU_SMBUS_MASTER_FSM 0x100
  176. /* SMBus master GW control bits offset in MLXBF_I2C_SMBUS_MASTER_GW[31:3]. */
  177. #define MLXBF_I2C_MASTER_LOCK_BIT BIT(31) /* Lock bit. */
  178. #define MLXBF_I2C_MASTER_BUSY_BIT BIT(30) /* Busy bit. */
  179. #define MLXBF_I2C_MASTER_START_BIT BIT(29) /* Control start. */
  180. #define MLXBF_I2C_MASTER_CTL_WRITE_BIT BIT(28) /* Control write phase. */
  181. #define MLXBF_I2C_MASTER_CTL_READ_BIT BIT(19) /* Control read phase. */
  182. #define MLXBF_I2C_MASTER_STOP_BIT BIT(3) /* Control stop. */
  183. #define MLXBF_I2C_MASTER_ENABLE \
  184. (MLXBF_I2C_MASTER_LOCK_BIT | MLXBF_I2C_MASTER_BUSY_BIT | \
  185. MLXBF_I2C_MASTER_START_BIT | MLXBF_I2C_MASTER_STOP_BIT)
  186. #define MLXBF_I2C_MASTER_ENABLE_WRITE \
  187. (MLXBF_I2C_MASTER_ENABLE | MLXBF_I2C_MASTER_CTL_WRITE_BIT)
  188. #define MLXBF_I2C_MASTER_ENABLE_READ \
  189. (MLXBF_I2C_MASTER_ENABLE | MLXBF_I2C_MASTER_CTL_READ_BIT)
  190. #define MLXBF_I2C_MASTER_WRITE_SHIFT 21 /* Control write bytes */
  191. #define MLXBF_I2C_MASTER_SEND_PEC_SHIFT 20 /* Send PEC byte when set to 1 */
  192. #define MLXBF_I2C_MASTER_PARSE_EXP_SHIFT 11 /* Control parse expected bytes */
  193. #define MLXBF_I2C_MASTER_SLV_ADDR_SHIFT 12 /* Slave address */
  194. #define MLXBF_I2C_MASTER_READ_SHIFT 4 /* Control read bytes */
  195. /* SMBus master GW Data descriptor. */
  196. #define MLXBF_I2C_MASTER_DATA_DESC_ADDR 0x80
  197. #define MLXBF_I2C_MASTER_DATA_DESC_SIZE 0x80 /* Size in bytes. */
  198. /* Maximum bytes to read/write per SMBus transaction. */
  199. #define MLXBF_I2C_MASTER_DATA_R_LENGTH MLXBF_I2C_MASTER_DATA_DESC_SIZE
  200. #define MLXBF_I2C_MASTER_DATA_W_LENGTH (MLXBF_I2C_MASTER_DATA_DESC_SIZE - 1)
  201. /* All bytes were transmitted. */
  202. #define MLXBF_I2C_SMBUS_STATUS_BYTE_CNT_DONE BIT(0)
  203. /* NACK received. */
  204. #define MLXBF_I2C_SMBUS_STATUS_NACK_RCV BIT(1)
  205. /* Slave's byte count >128 bytes. */
  206. #define MLXBF_I2C_SMBUS_STATUS_READ_ERR BIT(2)
  207. /* Timeout occurred. */
  208. #define MLXBF_I2C_SMBUS_STATUS_FW_TIMEOUT BIT(3)
  209. #define MLXBF_I2C_SMBUS_MASTER_STATUS_MASK GENMASK(3, 0)
  210. #define MLXBF_I2C_SMBUS_MASTER_STATUS_ERROR \
  211. (MLXBF_I2C_SMBUS_STATUS_NACK_RCV | \
  212. MLXBF_I2C_SMBUS_STATUS_READ_ERR | \
  213. MLXBF_I2C_SMBUS_STATUS_FW_TIMEOUT)
  214. #define MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK BIT(31)
  215. #define MLXBF_I2C_SMBUS_MASTER_FSM_PS_STATE_MASK BIT(15)
  216. #define MLXBF_I2C_SLV_ADDR_OFFSET 0x400
  217. /* SMBus slave GW. */
  218. #define MLXBF_I2C_SMBUS_SLAVE_GW 0x0
  219. /* Number of bytes received and sent from/to master. */
  220. #define MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES 0x100
  221. /* Packet error check (PEC) value. */
  222. #define MLXBF_I2C_SMBUS_SLAVE_PEC 0x104
  223. /* SMBus slave Finite State Machine (FSM). */
  224. #define MLXBF_I2C_SMBUS_SLAVE_FSM 0x110
  225. /*
  226. * Should be set when all raised causes handled, and cleared by HW on
  227. * every new cause.
  228. */
  229. #define MLXBF_I2C_SMBUS_SLAVE_READY 0x12c
  230. /* SMBus slave GW control bits offset in MLXBF_I2C_SMBUS_SLAVE_GW[31:19]. */
  231. #define MLXBF_I2C_SLAVE_BUSY_BIT BIT(30) /* Busy bit. */
  232. #define MLXBF_I2C_SLAVE_WRITE_BIT BIT(29) /* Control write enable. */
  233. #define MLXBF_I2C_SLAVE_ENABLE \
  234. (MLXBF_I2C_SLAVE_BUSY_BIT | MLXBF_I2C_SLAVE_WRITE_BIT)
  235. #define MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT 22 /* Number of bytes to write. */
  236. #define MLXBF_I2C_SLAVE_SEND_PEC_SHIFT 21 /* Send PEC byte shift. */
  237. /* SMBus slave GW Data descriptor. */
  238. #define MLXBF_I2C_SLAVE_DATA_DESC_ADDR 0x80
  239. #define MLXBF_I2C_SLAVE_DATA_DESC_SIZE 0x80 /* Size in bytes. */
  240. /* SMbus slave configuration registers. */
  241. #define MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG 0x114
  242. #define MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT 16
  243. #define MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT BIT(7)
  244. #define MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK GENMASK(6, 0)
  245. /*
  246. * Timeout is given in microsends. Note also that timeout handling is not
  247. * exact.
  248. */
  249. #define MLXBF_I2C_SMBUS_TIMEOUT (300 * 1000) /* 300ms */
  250. #define MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT (300 * 1000) /* 300ms */
  251. /* Polling frequency in microseconds. */
  252. #define MLXBF_I2C_POLL_FREQ_IN_USEC 200
  253. #define MLXBF_I2C_SMBUS_OP_CNT_1 1
  254. #define MLXBF_I2C_SMBUS_OP_CNT_2 2
  255. #define MLXBF_I2C_SMBUS_OP_CNT_3 3
  256. #define MLXBF_I2C_SMBUS_MAX_OP_CNT MLXBF_I2C_SMBUS_OP_CNT_3
  257. /* Helper macro to define an I2C resource parameters. */
  258. #define MLXBF_I2C_RES_PARAMS(addr, size, str) \
  259. { \
  260. .start = (addr), \
  261. .end = (addr) + (size) - 1, \
  262. .name = (str) \
  263. }
  264. enum {
  265. MLXBF_I2C_TIMING_100KHZ = 100000,
  266. MLXBF_I2C_TIMING_400KHZ = 400000,
  267. MLXBF_I2C_TIMING_1000KHZ = 1000000,
  268. };
  269. enum {
  270. MLXBF_I2C_F_READ = BIT(0),
  271. MLXBF_I2C_F_WRITE = BIT(1),
  272. MLXBF_I2C_F_NORESTART = BIT(3),
  273. MLXBF_I2C_F_SMBUS_OPERATION = BIT(4),
  274. MLXBF_I2C_F_SMBUS_BLOCK = BIT(5),
  275. MLXBF_I2C_F_SMBUS_PEC = BIT(6),
  276. MLXBF_I2C_F_SMBUS_PROCESS_CALL = BIT(7),
  277. };
  278. /* Mellanox BlueField chip type. */
  279. enum mlxbf_i2c_chip_type {
  280. MLXBF_I2C_CHIP_TYPE_1, /* Mellanox BlueField-1 chip. */
  281. MLXBF_I2C_CHIP_TYPE_2, /* Mellanox BlueField-2 chip. */
  282. MLXBF_I2C_CHIP_TYPE_3 /* Mellanox BlueField-3 chip. */
  283. };
  284. /* List of chip resources that are being accessed by the driver. */
  285. enum {
  286. MLXBF_I2C_SMBUS_RES,
  287. MLXBF_I2C_MST_CAUSE_RES,
  288. MLXBF_I2C_SLV_CAUSE_RES,
  289. MLXBF_I2C_COALESCE_RES,
  290. MLXBF_I2C_SMBUS_TIMER_RES,
  291. MLXBF_I2C_SMBUS_MST_RES,
  292. MLXBF_I2C_SMBUS_SLV_RES,
  293. MLXBF_I2C_COREPLL_RES,
  294. MLXBF_I2C_GPIO_RES,
  295. MLXBF_I2C_END_RES
  296. };
  297. /* Encapsulates timing parameters. */
  298. struct mlxbf_i2c_timings {
  299. u16 scl_high; /* Clock high period. */
  300. u16 scl_low; /* Clock low period. */
  301. u8 sda_rise; /* Data rise time. */
  302. u8 sda_fall; /* Data fall time. */
  303. u8 scl_rise; /* Clock rise time. */
  304. u8 scl_fall; /* Clock fall time. */
  305. u16 hold_start; /* Hold time after (REPEATED) START. */
  306. u16 hold_data; /* Data hold time. */
  307. u16 setup_start; /* REPEATED START condition setup time. */
  308. u16 setup_stop; /* STOP condition setup time. */
  309. u16 setup_data; /* Data setup time. */
  310. u16 pad; /* Padding. */
  311. u16 buf; /* Bus free time between STOP and START. */
  312. u16 thigh_max; /* Thigh max. */
  313. u32 timeout; /* Detect clock low timeout. */
  314. };
  315. struct mlxbf_i2c_smbus_operation {
  316. u32 flags;
  317. u32 length; /* Buffer length in bytes. */
  318. u8 *buffer;
  319. };
  320. struct mlxbf_i2c_smbus_request {
  321. u8 slave;
  322. u8 operation_cnt;
  323. struct mlxbf_i2c_smbus_operation operation[MLXBF_I2C_SMBUS_MAX_OP_CNT];
  324. };
  325. struct mlxbf_i2c_resource {
  326. void __iomem *io;
  327. struct resource *params;
  328. struct mutex *lock; /* Mutex to protect mlxbf_i2c_resource. */
  329. u8 type;
  330. };
  331. struct mlxbf_i2c_chip_info {
  332. enum mlxbf_i2c_chip_type type;
  333. /* Chip shared resources that are being used by the I2C controller. */
  334. struct mlxbf_i2c_resource *shared_res[MLXBF_I2C_SHARED_RES_MAX];
  335. /* Callback to calculate the core PLL frequency. */
  336. u64 (*calculate_freq)(struct mlxbf_i2c_resource *corepll_res);
  337. /* Registers' address offset */
  338. u32 smbus_master_rs_bytes_off;
  339. u32 smbus_master_fsm_off;
  340. };
  341. struct mlxbf_i2c_priv {
  342. const struct mlxbf_i2c_chip_info *chip;
  343. struct i2c_adapter adap;
  344. struct mlxbf_i2c_resource *smbus;
  345. struct mlxbf_i2c_resource *timer;
  346. struct mlxbf_i2c_resource *mst;
  347. struct mlxbf_i2c_resource *slv;
  348. struct mlxbf_i2c_resource *mst_cause;
  349. struct mlxbf_i2c_resource *slv_cause;
  350. struct mlxbf_i2c_resource *coalesce;
  351. u64 frequency; /* Core frequency in Hz. */
  352. int bus; /* Physical bus identifier. */
  353. int irq;
  354. struct i2c_client *slave[MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT];
  355. u32 resource_version;
  356. };
  357. /* Core PLL frequency. */
  358. static u64 mlxbf_i2c_corepll_frequency;
  359. static struct resource mlxbf_i2c_coalesce_tyu_params =
  360. MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COALESCE_TYU_ADDR,
  361. MLXBF_I2C_COALESCE_TYU_SIZE,
  362. "COALESCE_MEM");
  363. static struct resource mlxbf_i2c_corepll_tyu_params =
  364. MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_TYU_ADDR,
  365. MLXBF_I2C_COREPLL_TYU_SIZE,
  366. "COREPLL_MEM");
  367. static struct resource mlxbf_i2c_corepll_yu_params =
  368. MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_YU_ADDR,
  369. MLXBF_I2C_COREPLL_YU_SIZE,
  370. "COREPLL_MEM");
  371. static struct resource mlxbf_i2c_corepll_rsh_yu_params =
  372. MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_RSH_YU_ADDR,
  373. MLXBF_I2C_COREPLL_RSH_YU_SIZE,
  374. "COREPLL_MEM");
  375. static struct resource mlxbf_i2c_gpio_tyu_params =
  376. MLXBF_I2C_RES_PARAMS(MLXBF_I2C_GPIO_TYU_ADDR,
  377. MLXBF_I2C_GPIO_TYU_SIZE,
  378. "GPIO_MEM");
  379. static struct mutex mlxbf_i2c_coalesce_lock;
  380. static struct mutex mlxbf_i2c_corepll_lock;
  381. static struct mutex mlxbf_i2c_gpio_lock;
  382. static struct mlxbf_i2c_resource mlxbf_i2c_coalesce_res[] = {
  383. [MLXBF_I2C_CHIP_TYPE_1] = {
  384. .params = &mlxbf_i2c_coalesce_tyu_params,
  385. .lock = &mlxbf_i2c_coalesce_lock,
  386. .type = MLXBF_I2C_COALESCE_RES
  387. },
  388. {}
  389. };
  390. static struct mlxbf_i2c_resource mlxbf_i2c_corepll_res[] = {
  391. [MLXBF_I2C_CHIP_TYPE_1] = {
  392. .params = &mlxbf_i2c_corepll_tyu_params,
  393. .lock = &mlxbf_i2c_corepll_lock,
  394. .type = MLXBF_I2C_COREPLL_RES
  395. },
  396. [MLXBF_I2C_CHIP_TYPE_2] = {
  397. .params = &mlxbf_i2c_corepll_yu_params,
  398. .lock = &mlxbf_i2c_corepll_lock,
  399. .type = MLXBF_I2C_COREPLL_RES,
  400. },
  401. [MLXBF_I2C_CHIP_TYPE_3] = {
  402. .params = &mlxbf_i2c_corepll_rsh_yu_params,
  403. .lock = &mlxbf_i2c_corepll_lock,
  404. .type = MLXBF_I2C_COREPLL_RES,
  405. }
  406. };
  407. static struct mlxbf_i2c_resource mlxbf_i2c_gpio_res[] = {
  408. [MLXBF_I2C_CHIP_TYPE_1] = {
  409. .params = &mlxbf_i2c_gpio_tyu_params,
  410. .lock = &mlxbf_i2c_gpio_lock,
  411. .type = MLXBF_I2C_GPIO_RES
  412. },
  413. {}
  414. };
  415. static u8 mlxbf_i2c_bus_count;
  416. static struct mutex mlxbf_i2c_bus_lock;
  417. /*
  418. * Function to poll a set of bits at a specific address; it checks whether
  419. * the bits are equal to zero when eq_zero is set to 'true', and not equal
  420. * to zero when eq_zero is set to 'false'.
  421. * Note that the timeout is given in microseconds.
  422. */
  423. static u32 mlxbf_i2c_poll(void __iomem *io, u32 addr, u32 mask,
  424. bool eq_zero, u32 timeout)
  425. {
  426. u32 bits;
  427. timeout = (timeout / MLXBF_I2C_POLL_FREQ_IN_USEC) + 1;
  428. do {
  429. bits = readl(io + addr) & mask;
  430. if (eq_zero ? bits == 0 : bits != 0)
  431. return eq_zero ? 1 : bits;
  432. udelay(MLXBF_I2C_POLL_FREQ_IN_USEC);
  433. } while (timeout-- != 0);
  434. return 0;
  435. }
  436. /*
  437. * SW must make sure that the SMBus Master GW is idle before starting
  438. * a transaction. Accordingly, this function polls the Master FSM stop
  439. * bit; it returns false when the bit is asserted, true if not.
  440. */
  441. static bool mlxbf_i2c_smbus_master_wait_for_idle(struct mlxbf_i2c_priv *priv)
  442. {
  443. u32 mask = MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK;
  444. u32 addr = priv->chip->smbus_master_fsm_off;
  445. u32 timeout = MLXBF_I2C_SMBUS_TIMEOUT;
  446. if (mlxbf_i2c_poll(priv->mst->io, addr, mask, true, timeout))
  447. return true;
  448. return false;
  449. }
  450. /*
  451. * wait for the lock to be released before acquiring it.
  452. */
  453. static bool mlxbf_i2c_smbus_master_lock(struct mlxbf_i2c_priv *priv)
  454. {
  455. if (mlxbf_i2c_poll(priv->mst->io, MLXBF_I2C_SMBUS_MASTER_GW,
  456. MLXBF_I2C_MASTER_LOCK_BIT, true,
  457. MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT))
  458. return true;
  459. return false;
  460. }
  461. static void mlxbf_i2c_smbus_master_unlock(struct mlxbf_i2c_priv *priv)
  462. {
  463. /* Clear the gw to clear the lock */
  464. writel(0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW);
  465. }
  466. static bool mlxbf_i2c_smbus_transaction_success(u32 master_status,
  467. u32 cause_status)
  468. {
  469. /*
  470. * When transaction ended with STOP, all bytes were transmitted,
  471. * and no NACK received, then the transaction ended successfully.
  472. * On the other hand, when the GW is configured with the stop bit
  473. * de-asserted then the SMBus expects the following GW configuration
  474. * for transfer continuation.
  475. */
  476. if ((cause_status & MLXBF_I2C_CAUSE_WAIT_FOR_FW_DATA) ||
  477. ((cause_status & MLXBF_I2C_CAUSE_TRANSACTION_ENDED) &&
  478. (master_status & MLXBF_I2C_SMBUS_STATUS_BYTE_CNT_DONE) &&
  479. !(master_status & MLXBF_I2C_SMBUS_STATUS_NACK_RCV)))
  480. return true;
  481. return false;
  482. }
  483. /*
  484. * Poll SMBus master status and return transaction status,
  485. * i.e. whether succeeded or failed. I2C and SMBus fault codes
  486. * are returned as negative numbers from most calls, with zero
  487. * or some positive number indicating a non-fault return.
  488. */
  489. static int mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv *priv)
  490. {
  491. u32 master_status_bits;
  492. u32 cause_status_bits;
  493. /*
  494. * GW busy bit is raised by the driver and cleared by the HW
  495. * when the transaction is completed. The busy bit is a good
  496. * indicator of transaction status. So poll the busy bit, and
  497. * then read the cause and master status bits to determine if
  498. * errors occurred during the transaction.
  499. */
  500. mlxbf_i2c_poll(priv->mst->io, MLXBF_I2C_SMBUS_MASTER_GW,
  501. MLXBF_I2C_MASTER_BUSY_BIT, true,
  502. MLXBF_I2C_SMBUS_TIMEOUT);
  503. /* Read cause status bits. */
  504. cause_status_bits = readl(priv->mst_cause->io +
  505. MLXBF_I2C_CAUSE_ARBITER);
  506. cause_status_bits &= MLXBF_I2C_CAUSE_MASTER_ARBITER_BITS_MASK;
  507. /*
  508. * Parse both Cause and Master GW bits, then return transaction status.
  509. */
  510. master_status_bits = readl(priv->mst->io +
  511. MLXBF_I2C_SMBUS_MASTER_STATUS);
  512. master_status_bits &= MLXBF_I2C_SMBUS_MASTER_STATUS_MASK;
  513. if (mlxbf_i2c_smbus_transaction_success(master_status_bits,
  514. cause_status_bits))
  515. return 0;
  516. /*
  517. * In case of timeout on GW busy, the ISR will clear busy bit but
  518. * transaction ended bits cause will not be set so the transaction
  519. * fails. Then, we must check Master GW status bits.
  520. */
  521. if ((master_status_bits & MLXBF_I2C_SMBUS_MASTER_STATUS_ERROR) &&
  522. (cause_status_bits & (MLXBF_I2C_CAUSE_TRANSACTION_ENDED |
  523. MLXBF_I2C_CAUSE_M_GW_BUSY_FALL)))
  524. return -EIO;
  525. if (cause_status_bits & MLXBF_I2C_CAUSE_MASTER_STATUS_ERROR)
  526. return -EAGAIN;
  527. return -ETIMEDOUT;
  528. }
  529. static void mlxbf_i2c_smbus_write_data(struct mlxbf_i2c_priv *priv,
  530. const u8 *data, u8 length, u32 addr,
  531. bool is_master)
  532. {
  533. u8 offset, aligned_length;
  534. u32 data32;
  535. aligned_length = round_up(length, 4);
  536. /*
  537. * Copy data bytes from 4-byte aligned source buffer.
  538. * Data copied to the Master GW Data Descriptor MUST be shifted
  539. * left so the data starts at the MSB of the descriptor registers
  540. * as required by the underlying hardware. Enable byte swapping
  541. * when writing data bytes to the 32 * 32-bit HW Data registers
  542. * a.k.a Master GW Data Descriptor.
  543. */
  544. for (offset = 0; offset < aligned_length; offset += sizeof(u32)) {
  545. data32 = *((u32 *)(data + offset));
  546. if (is_master)
  547. iowrite32be(data32, priv->mst->io + addr + offset);
  548. else
  549. iowrite32be(data32, priv->slv->io + addr + offset);
  550. }
  551. }
  552. static void mlxbf_i2c_smbus_read_data(struct mlxbf_i2c_priv *priv,
  553. u8 *data, u8 length, u32 addr,
  554. bool is_master)
  555. {
  556. u32 data32, mask;
  557. u8 byte, offset;
  558. mask = sizeof(u32) - 1;
  559. /*
  560. * Data bytes in the Master GW Data Descriptor are shifted left
  561. * so the data starts at the MSB of the descriptor registers as
  562. * set by the underlying hardware. Enable byte swapping while
  563. * reading data bytes from the 32 * 32-bit HW Data registers
  564. * a.k.a Master GW Data Descriptor.
  565. */
  566. for (offset = 0; offset < (length & ~mask); offset += sizeof(u32)) {
  567. if (is_master)
  568. data32 = ioread32be(priv->mst->io + addr + offset);
  569. else
  570. data32 = ioread32be(priv->slv->io + addr + offset);
  571. *((u32 *)(data + offset)) = data32;
  572. }
  573. if (!(length & mask))
  574. return;
  575. if (is_master)
  576. data32 = ioread32be(priv->mst->io + addr + offset);
  577. else
  578. data32 = ioread32be(priv->slv->io + addr + offset);
  579. for (byte = 0; byte < (length & mask); byte++) {
  580. data[offset + byte] = data32 & GENMASK(7, 0);
  581. data32 = ror32(data32, MLXBF_I2C_SHIFT_8);
  582. }
  583. }
  584. static int mlxbf_i2c_smbus_enable(struct mlxbf_i2c_priv *priv, u8 slave,
  585. u8 len, u8 block_en, u8 pec_en, bool read)
  586. {
  587. u32 command;
  588. /* Set Master GW control word. */
  589. if (read) {
  590. command = MLXBF_I2C_MASTER_ENABLE_READ;
  591. command |= rol32(len, MLXBF_I2C_MASTER_READ_SHIFT);
  592. } else {
  593. command = MLXBF_I2C_MASTER_ENABLE_WRITE;
  594. command |= rol32(len, MLXBF_I2C_MASTER_WRITE_SHIFT);
  595. }
  596. command |= rol32(slave, MLXBF_I2C_MASTER_SLV_ADDR_SHIFT);
  597. command |= rol32(block_en, MLXBF_I2C_MASTER_PARSE_EXP_SHIFT);
  598. command |= rol32(pec_en, MLXBF_I2C_MASTER_SEND_PEC_SHIFT);
  599. /* Clear status bits. */
  600. writel(0x0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_STATUS);
  601. /* Set the cause data. */
  602. writel(~0x0, priv->mst_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
  603. /* Zero PEC byte. */
  604. writel(0x0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_PEC);
  605. /* Zero byte count. */
  606. writel(0x0, priv->mst->io + priv->chip->smbus_master_rs_bytes_off);
  607. /* GW activation. */
  608. writel(command, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW);
  609. /*
  610. * Poll master status and check status bits. An ACK is sent when
  611. * completing writing data to the bus (Master 'byte_count_done' bit
  612. * is set to 1).
  613. */
  614. return mlxbf_i2c_smbus_check_status(priv);
  615. }
  616. static int
  617. mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
  618. struct mlxbf_i2c_smbus_request *request)
  619. {
  620. u8 data_desc[MLXBF_I2C_MASTER_DATA_DESC_SIZE] = { 0 };
  621. u8 op_idx, data_idx, data_len, write_len, read_len;
  622. struct mlxbf_i2c_smbus_operation *operation;
  623. u8 read_en, write_en, block_en, pec_en;
  624. u8 slave, flags, addr;
  625. u8 *read_buf;
  626. int ret = 0;
  627. if (request->operation_cnt > MLXBF_I2C_SMBUS_MAX_OP_CNT)
  628. return -EINVAL;
  629. read_buf = NULL;
  630. data_idx = 0;
  631. read_en = 0;
  632. write_en = 0;
  633. write_len = 0;
  634. read_len = 0;
  635. block_en = 0;
  636. pec_en = 0;
  637. slave = request->slave & GENMASK(6, 0);
  638. addr = slave << 1;
  639. /*
  640. * Try to acquire the smbus gw lock before any reads of the GW register since
  641. * a read sets the lock.
  642. */
  643. if (WARN_ON(!mlxbf_i2c_smbus_master_lock(priv)))
  644. return -EBUSY;
  645. /* Check whether the HW is idle */
  646. if (WARN_ON(!mlxbf_i2c_smbus_master_wait_for_idle(priv))) {
  647. ret = -EBUSY;
  648. goto out_unlock;
  649. }
  650. /* Set first byte. */
  651. data_desc[data_idx++] = addr;
  652. for (op_idx = 0; op_idx < request->operation_cnt; op_idx++) {
  653. operation = &request->operation[op_idx];
  654. flags = operation->flags;
  655. /*
  656. * Note that read and write operations might be handled by a
  657. * single command. If the MLXBF_I2C_F_SMBUS_OPERATION is set
  658. * then write command byte and set the optional SMBus specific
  659. * bits such as block_en and pec_en. These bits MUST be
  660. * submitted by the first operation only.
  661. */
  662. if (op_idx == 0 && flags & MLXBF_I2C_F_SMBUS_OPERATION) {
  663. block_en = flags & MLXBF_I2C_F_SMBUS_BLOCK;
  664. pec_en = flags & MLXBF_I2C_F_SMBUS_PEC;
  665. }
  666. if (flags & MLXBF_I2C_F_WRITE) {
  667. write_en = 1;
  668. write_len += operation->length;
  669. if (data_idx + operation->length >
  670. MLXBF_I2C_MASTER_DATA_DESC_SIZE) {
  671. ret = -ENOBUFS;
  672. goto out_unlock;
  673. }
  674. memcpy(data_desc + data_idx,
  675. operation->buffer, operation->length);
  676. data_idx += operation->length;
  677. }
  678. /*
  679. * We assume that read operations are performed only once per
  680. * SMBus transaction. *TBD* protect this statement so it won't
  681. * be executed twice? or return an error if we try to read more
  682. * than once?
  683. */
  684. if (flags & MLXBF_I2C_F_READ) {
  685. read_en = 1;
  686. /* Subtract 1 as required by HW. */
  687. read_len = operation->length - 1;
  688. read_buf = operation->buffer;
  689. }
  690. }
  691. /* Set Master GW data descriptor. */
  692. data_len = write_len + 1; /* Add one byte of the slave address. */
  693. /*
  694. * Note that data_len cannot be 0. Indeed, the slave address byte
  695. * must be written to the data registers.
  696. */
  697. mlxbf_i2c_smbus_write_data(priv, (const u8 *)data_desc, data_len,
  698. MLXBF_I2C_MASTER_DATA_DESC_ADDR, true);
  699. if (write_en) {
  700. ret = mlxbf_i2c_smbus_enable(priv, slave, write_len, block_en,
  701. pec_en, 0);
  702. if (ret)
  703. goto out_unlock;
  704. }
  705. if (read_en) {
  706. /* Write slave address to Master GW data descriptor. */
  707. mlxbf_i2c_smbus_write_data(priv, (const u8 *)&addr, 1,
  708. MLXBF_I2C_MASTER_DATA_DESC_ADDR, true);
  709. ret = mlxbf_i2c_smbus_enable(priv, slave, read_len, block_en,
  710. pec_en, 1);
  711. if (!ret) {
  712. /* Get Master GW data descriptor. */
  713. mlxbf_i2c_smbus_read_data(priv, data_desc, read_len + 1,
  714. MLXBF_I2C_MASTER_DATA_DESC_ADDR, true);
  715. /* Get data from Master GW data descriptor. */
  716. memcpy(read_buf, data_desc, read_len + 1);
  717. }
  718. /*
  719. * After a read operation the SMBus FSM ps (present state)
  720. * needs to be 'manually' reset. This should be removed in
  721. * next tag integration.
  722. */
  723. writel(MLXBF_I2C_SMBUS_MASTER_FSM_PS_STATE_MASK,
  724. priv->mst->io + priv->chip->smbus_master_fsm_off);
  725. }
  726. out_unlock:
  727. mlxbf_i2c_smbus_master_unlock(priv);
  728. return ret;
  729. }
  730. /* I2C SMBus protocols. */
  731. static void
  732. mlxbf_i2c_smbus_quick_command(struct mlxbf_i2c_smbus_request *request,
  733. u8 read)
  734. {
  735. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_1;
  736. request->operation[0].length = 0;
  737. request->operation[0].flags = MLXBF_I2C_F_WRITE;
  738. request->operation[0].flags |= read ? MLXBF_I2C_F_READ : 0;
  739. }
  740. static void mlxbf_i2c_smbus_byte_func(struct mlxbf_i2c_smbus_request *request,
  741. u8 *data, bool read, bool pec_check)
  742. {
  743. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_1;
  744. request->operation[0].length = 1;
  745. request->operation[0].length += pec_check;
  746. request->operation[0].flags = MLXBF_I2C_F_SMBUS_OPERATION;
  747. request->operation[0].flags |= read ?
  748. MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
  749. request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
  750. request->operation[0].buffer = data;
  751. }
  752. static void
  753. mlxbf_i2c_smbus_data_byte_func(struct mlxbf_i2c_smbus_request *request,
  754. u8 *command, u8 *data, bool read, bool pec_check)
  755. {
  756. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
  757. request->operation[0].length = 1;
  758. request->operation[0].flags =
  759. MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
  760. request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
  761. request->operation[0].buffer = command;
  762. request->operation[1].length = 1;
  763. request->operation[1].length += pec_check;
  764. request->operation[1].flags = read ?
  765. MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
  766. request->operation[1].buffer = data;
  767. }
  768. static void
  769. mlxbf_i2c_smbus_data_word_func(struct mlxbf_i2c_smbus_request *request,
  770. u8 *command, u8 *data, bool read, bool pec_check)
  771. {
  772. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
  773. request->operation[0].length = 1;
  774. request->operation[0].flags =
  775. MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
  776. request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
  777. request->operation[0].buffer = command;
  778. request->operation[1].length = 2;
  779. request->operation[1].length += pec_check;
  780. request->operation[1].flags = read ?
  781. MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
  782. request->operation[1].buffer = data;
  783. }
  784. static void
  785. mlxbf_i2c_smbus_i2c_block_func(struct mlxbf_i2c_smbus_request *request,
  786. u8 *command, u8 *data, u8 *data_len, bool read,
  787. bool pec_check)
  788. {
  789. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
  790. request->operation[0].length = 1;
  791. request->operation[0].flags =
  792. MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
  793. request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
  794. request->operation[0].buffer = command;
  795. /*
  796. * As specified in the standard, the max number of bytes to read/write
  797. * per block operation is 32 bytes. In Golan code, the controller can
  798. * read up to 128 bytes and write up to 127 bytes.
  799. */
  800. request->operation[1].length =
  801. (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
  802. I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
  803. request->operation[1].flags = read ?
  804. MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
  805. /*
  806. * Skip the first data byte, which corresponds to the number of bytes
  807. * to read/write.
  808. */
  809. request->operation[1].buffer = data + 1;
  810. *data_len = request->operation[1].length;
  811. /* Set the number of byte to read. This will be used by userspace. */
  812. if (read)
  813. data[0] = *data_len;
  814. }
  815. static void mlxbf_i2c_smbus_block_func(struct mlxbf_i2c_smbus_request *request,
  816. u8 *command, u8 *data, u8 *data_len,
  817. bool read, bool pec_check)
  818. {
  819. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
  820. request->operation[0].length = 1;
  821. request->operation[0].flags =
  822. MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
  823. request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
  824. request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
  825. request->operation[0].buffer = command;
  826. request->operation[1].length =
  827. (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
  828. I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
  829. request->operation[1].flags = read ?
  830. MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
  831. request->operation[1].buffer = data + 1;
  832. *data_len = request->operation[1].length;
  833. /* Set the number of bytes to read. This will be used by userspace. */
  834. if (read)
  835. data[0] = *data_len;
  836. }
  837. static void
  838. mlxbf_i2c_smbus_process_call_func(struct mlxbf_i2c_smbus_request *request,
  839. u8 *command, u8 *data, bool pec_check)
  840. {
  841. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_3;
  842. request->operation[0].length = 1;
  843. request->operation[0].flags =
  844. MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
  845. request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
  846. request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
  847. request->operation[0].buffer = command;
  848. request->operation[1].length = 2;
  849. request->operation[1].flags = MLXBF_I2C_F_WRITE;
  850. request->operation[1].buffer = data;
  851. request->operation[2].length = 3;
  852. request->operation[2].flags = MLXBF_I2C_F_READ;
  853. request->operation[2].buffer = data;
  854. }
  855. static void
  856. mlxbf_i2c_smbus_blk_process_call_func(struct mlxbf_i2c_smbus_request *request,
  857. u8 *command, u8 *data, u8 *data_len,
  858. bool pec_check)
  859. {
  860. u32 length;
  861. request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_3;
  862. request->operation[0].length = 1;
  863. request->operation[0].flags =
  864. MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
  865. request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
  866. request->operation[0].flags |= (pec_check) ? MLXBF_I2C_F_SMBUS_PEC : 0;
  867. request->operation[0].buffer = command;
  868. length = (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
  869. I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
  870. request->operation[1].length = length - pec_check;
  871. request->operation[1].flags = MLXBF_I2C_F_WRITE;
  872. request->operation[1].buffer = data;
  873. request->operation[2].length = length;
  874. request->operation[2].flags = MLXBF_I2C_F_READ;
  875. request->operation[2].buffer = data;
  876. *data_len = length; /* including PEC byte. */
  877. }
  878. /* Initialization functions. */
  879. static bool mlxbf_i2c_has_chip_type(struct mlxbf_i2c_priv *priv, u8 type)
  880. {
  881. return priv->chip->type == type;
  882. }
  883. static struct mlxbf_i2c_resource *
  884. mlxbf_i2c_get_shared_resource(struct mlxbf_i2c_priv *priv, u8 type)
  885. {
  886. const struct mlxbf_i2c_chip_info *chip = priv->chip;
  887. struct mlxbf_i2c_resource *res;
  888. u8 res_idx = 0;
  889. for (res_idx = 0; res_idx < MLXBF_I2C_SHARED_RES_MAX; res_idx++) {
  890. res = chip->shared_res[res_idx];
  891. if (res && res->type == type)
  892. return res;
  893. }
  894. return NULL;
  895. }
  896. static int mlxbf_i2c_init_resource(struct platform_device *pdev,
  897. struct mlxbf_i2c_resource **res,
  898. u8 type)
  899. {
  900. struct mlxbf_i2c_resource *tmp_res;
  901. struct device *dev = &pdev->dev;
  902. if (!res || *res || type >= MLXBF_I2C_END_RES)
  903. return -EINVAL;
  904. tmp_res = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource),
  905. GFP_KERNEL);
  906. if (!tmp_res)
  907. return -ENOMEM;
  908. tmp_res->io = devm_platform_get_and_ioremap_resource(pdev, type, &tmp_res->params);
  909. if (IS_ERR(tmp_res->io)) {
  910. devm_kfree(dev, tmp_res);
  911. return PTR_ERR(tmp_res->io);
  912. }
  913. tmp_res->type = type;
  914. *res = tmp_res;
  915. return 0;
  916. }
  917. static u32 mlxbf_i2c_get_ticks(struct mlxbf_i2c_priv *priv, u64 nanoseconds,
  918. bool minimum)
  919. {
  920. u64 frequency;
  921. u32 ticks;
  922. /*
  923. * Compute ticks as follow:
  924. *
  925. * Ticks
  926. * Time = --------- x 10^9 => Ticks = Time x Frequency x 10^-9
  927. * Frequency
  928. */
  929. frequency = priv->frequency;
  930. ticks = (nanoseconds * frequency) / MLXBF_I2C_FREQUENCY_1GHZ;
  931. /*
  932. * The number of ticks is rounded down and if minimum is equal to 1
  933. * then add one tick.
  934. */
  935. if (minimum)
  936. ticks++;
  937. return ticks;
  938. }
  939. static u32 mlxbf_i2c_set_timer(struct mlxbf_i2c_priv *priv, u64 nsec, bool opt,
  940. u32 mask, u8 shift)
  941. {
  942. u32 val = (mlxbf_i2c_get_ticks(priv, nsec, opt) & mask) << shift;
  943. return val;
  944. }
  945. static void mlxbf_i2c_set_timings(struct mlxbf_i2c_priv *priv,
  946. const struct mlxbf_i2c_timings *timings)
  947. {
  948. u32 timer;
  949. timer = mlxbf_i2c_set_timer(priv, timings->scl_high,
  950. false, MLXBF_I2C_MASK_16,
  951. MLXBF_I2C_SHIFT_0);
  952. timer |= mlxbf_i2c_set_timer(priv, timings->scl_low,
  953. false, MLXBF_I2C_MASK_16,
  954. MLXBF_I2C_SHIFT_16);
  955. writel(timer, priv->timer->io +
  956. MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH);
  957. timer = mlxbf_i2c_set_timer(priv, timings->sda_rise, false,
  958. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_0);
  959. timer |= mlxbf_i2c_set_timer(priv, timings->sda_fall, false,
  960. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_8);
  961. timer |= mlxbf_i2c_set_timer(priv, timings->scl_rise, false,
  962. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_16);
  963. timer |= mlxbf_i2c_set_timer(priv, timings->scl_fall, false,
  964. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_24);
  965. writel(timer, priv->timer->io +
  966. MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE);
  967. timer = mlxbf_i2c_set_timer(priv, timings->hold_start, true,
  968. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  969. timer |= mlxbf_i2c_set_timer(priv, timings->hold_data, true,
  970. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
  971. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_TIMER_THOLD);
  972. timer = mlxbf_i2c_set_timer(priv, timings->setup_start, true,
  973. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  974. timer |= mlxbf_i2c_set_timer(priv, timings->setup_stop, true,
  975. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
  976. writel(timer, priv->timer->io +
  977. MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP);
  978. timer = mlxbf_i2c_set_timer(priv, timings->setup_data, true,
  979. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  980. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA);
  981. timer = mlxbf_i2c_set_timer(priv, timings->buf, false,
  982. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  983. timer |= mlxbf_i2c_set_timer(priv, timings->thigh_max, false,
  984. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
  985. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_THIGH_MAX_TBUF);
  986. timer = timings->timeout;
  987. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT);
  988. }
  989. enum mlxbf_i2c_timings_config {
  990. MLXBF_I2C_TIMING_CONFIG_100KHZ,
  991. MLXBF_I2C_TIMING_CONFIG_400KHZ,
  992. MLXBF_I2C_TIMING_CONFIG_1000KHZ,
  993. };
  994. /*
  995. * Note that the mlxbf_i2c_timings->timeout value is not related to the
  996. * bus frequency, it is impacted by the time it takes the driver to
  997. * complete data transmission before transaction abort.
  998. */
  999. static const struct mlxbf_i2c_timings mlxbf_i2c_timings[] = {
  1000. [MLXBF_I2C_TIMING_CONFIG_100KHZ] = {
  1001. .scl_high = 4810,
  1002. .scl_low = 5000,
  1003. .hold_start = 4000,
  1004. .setup_start = 4800,
  1005. .setup_stop = 4000,
  1006. .setup_data = 250,
  1007. .sda_rise = 50,
  1008. .sda_fall = 50,
  1009. .scl_rise = 50,
  1010. .scl_fall = 50,
  1011. .hold_data = 300,
  1012. .buf = 20000,
  1013. .thigh_max = 5000,
  1014. .timeout = 106500
  1015. },
  1016. [MLXBF_I2C_TIMING_CONFIG_400KHZ] = {
  1017. .scl_high = 1011,
  1018. .scl_low = 1300,
  1019. .hold_start = 600,
  1020. .setup_start = 700,
  1021. .setup_stop = 600,
  1022. .setup_data = 100,
  1023. .sda_rise = 50,
  1024. .sda_fall = 50,
  1025. .scl_rise = 50,
  1026. .scl_fall = 50,
  1027. .hold_data = 300,
  1028. .buf = 20000,
  1029. .thigh_max = 5000,
  1030. .timeout = 106500
  1031. },
  1032. [MLXBF_I2C_TIMING_CONFIG_1000KHZ] = {
  1033. .scl_high = 600,
  1034. .scl_low = 1300,
  1035. .hold_start = 600,
  1036. .setup_start = 600,
  1037. .setup_stop = 600,
  1038. .setup_data = 100,
  1039. .sda_rise = 50,
  1040. .sda_fall = 50,
  1041. .scl_rise = 50,
  1042. .scl_fall = 50,
  1043. .hold_data = 300,
  1044. .buf = 20000,
  1045. .thigh_max = 5000,
  1046. .timeout = 106500
  1047. }
  1048. };
  1049. static int mlxbf_i2c_init_timings(struct platform_device *pdev,
  1050. struct mlxbf_i2c_priv *priv)
  1051. {
  1052. enum mlxbf_i2c_timings_config config_idx;
  1053. struct device *dev = &pdev->dev;
  1054. u32 config_khz;
  1055. int ret;
  1056. ret = device_property_read_u32(dev, "clock-frequency", &config_khz);
  1057. if (ret < 0)
  1058. config_khz = I2C_MAX_STANDARD_MODE_FREQ;
  1059. switch (config_khz) {
  1060. default:
  1061. /* Default settings is 100 KHz. */
  1062. pr_warn("Illegal value %d: defaulting to 100 KHz\n",
  1063. config_khz);
  1064. fallthrough;
  1065. case I2C_MAX_STANDARD_MODE_FREQ:
  1066. config_idx = MLXBF_I2C_TIMING_CONFIG_100KHZ;
  1067. break;
  1068. case I2C_MAX_FAST_MODE_FREQ:
  1069. config_idx = MLXBF_I2C_TIMING_CONFIG_400KHZ;
  1070. break;
  1071. case I2C_MAX_FAST_MODE_PLUS_FREQ:
  1072. config_idx = MLXBF_I2C_TIMING_CONFIG_1000KHZ;
  1073. break;
  1074. }
  1075. mlxbf_i2c_set_timings(priv, &mlxbf_i2c_timings[config_idx]);
  1076. return 0;
  1077. }
  1078. static int mlxbf_i2c_get_gpio(struct platform_device *pdev,
  1079. struct mlxbf_i2c_priv *priv)
  1080. {
  1081. struct mlxbf_i2c_resource *gpio_res;
  1082. struct device *dev = &pdev->dev;
  1083. struct resource *params;
  1084. resource_size_t size;
  1085. gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
  1086. if (!gpio_res)
  1087. return -EPERM;
  1088. /*
  1089. * The GPIO region in TYU space is shared among I2C busses.
  1090. * This function MUST be serialized to avoid racing when
  1091. * claiming the memory region and/or setting up the GPIO.
  1092. */
  1093. lockdep_assert_held(gpio_res->lock);
  1094. /* Check whether the memory map exist. */
  1095. if (gpio_res->io)
  1096. return 0;
  1097. params = gpio_res->params;
  1098. size = resource_size(params);
  1099. if (!devm_request_mem_region(dev, params->start, size, params->name))
  1100. return -EFAULT;
  1101. gpio_res->io = devm_ioremap(dev, params->start, size);
  1102. if (!gpio_res->io) {
  1103. devm_release_mem_region(dev, params->start, size);
  1104. return -ENOMEM;
  1105. }
  1106. return 0;
  1107. }
  1108. static int mlxbf_i2c_release_gpio(struct platform_device *pdev,
  1109. struct mlxbf_i2c_priv *priv)
  1110. {
  1111. struct mlxbf_i2c_resource *gpio_res;
  1112. struct device *dev = &pdev->dev;
  1113. struct resource *params;
  1114. gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
  1115. if (!gpio_res)
  1116. return 0;
  1117. mutex_lock(gpio_res->lock);
  1118. if (gpio_res->io) {
  1119. /* Release the GPIO resource. */
  1120. params = gpio_res->params;
  1121. devm_iounmap(dev, gpio_res->io);
  1122. devm_release_mem_region(dev, params->start,
  1123. resource_size(params));
  1124. }
  1125. mutex_unlock(gpio_res->lock);
  1126. return 0;
  1127. }
  1128. static int mlxbf_i2c_get_corepll(struct platform_device *pdev,
  1129. struct mlxbf_i2c_priv *priv)
  1130. {
  1131. struct mlxbf_i2c_resource *corepll_res;
  1132. struct device *dev = &pdev->dev;
  1133. struct resource *params;
  1134. resource_size_t size;
  1135. corepll_res = mlxbf_i2c_get_shared_resource(priv,
  1136. MLXBF_I2C_COREPLL_RES);
  1137. if (!corepll_res)
  1138. return -EPERM;
  1139. /*
  1140. * The COREPLL region in TYU space is shared among I2C busses.
  1141. * This function MUST be serialized to avoid racing when
  1142. * claiming the memory region.
  1143. */
  1144. lockdep_assert_held(corepll_res->lock);
  1145. /* Check whether the memory map exist. */
  1146. if (corepll_res->io)
  1147. return 0;
  1148. params = corepll_res->params;
  1149. size = resource_size(params);
  1150. if (!devm_request_mem_region(dev, params->start, size, params->name))
  1151. return -EFAULT;
  1152. corepll_res->io = devm_ioremap(dev, params->start, size);
  1153. if (!corepll_res->io) {
  1154. devm_release_mem_region(dev, params->start, size);
  1155. return -ENOMEM;
  1156. }
  1157. return 0;
  1158. }
  1159. static int mlxbf_i2c_release_corepll(struct platform_device *pdev,
  1160. struct mlxbf_i2c_priv *priv)
  1161. {
  1162. struct mlxbf_i2c_resource *corepll_res;
  1163. struct device *dev = &pdev->dev;
  1164. struct resource *params;
  1165. corepll_res = mlxbf_i2c_get_shared_resource(priv,
  1166. MLXBF_I2C_COREPLL_RES);
  1167. mutex_lock(corepll_res->lock);
  1168. if (corepll_res->io) {
  1169. /* Release the CorePLL resource. */
  1170. params = corepll_res->params;
  1171. devm_iounmap(dev, corepll_res->io);
  1172. devm_release_mem_region(dev, params->start,
  1173. resource_size(params));
  1174. }
  1175. mutex_unlock(corepll_res->lock);
  1176. return 0;
  1177. }
  1178. static int mlxbf_i2c_init_master(struct platform_device *pdev,
  1179. struct mlxbf_i2c_priv *priv)
  1180. {
  1181. struct mlxbf_i2c_resource *gpio_res;
  1182. struct device *dev = &pdev->dev;
  1183. u32 config_reg;
  1184. int ret;
  1185. /* This configuration is only needed for BlueField 1. */
  1186. if (!mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1))
  1187. return 0;
  1188. gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
  1189. if (!gpio_res)
  1190. return -EPERM;
  1191. /*
  1192. * The GPIO region in TYU space is shared among I2C busses.
  1193. * This function MUST be serialized to avoid racing when
  1194. * claiming the memory region and/or setting up the GPIO.
  1195. */
  1196. mutex_lock(gpio_res->lock);
  1197. ret = mlxbf_i2c_get_gpio(pdev, priv);
  1198. if (ret < 0) {
  1199. dev_err(dev, "Failed to get gpio resource");
  1200. mutex_unlock(gpio_res->lock);
  1201. return ret;
  1202. }
  1203. /*
  1204. * TYU - Configuration for GPIO pins. Those pins must be asserted in
  1205. * MLXBF_I2C_GPIO_0_FUNC_EN_0, i.e. GPIO 0 is controlled by HW, and must
  1206. * be reset in MLXBF_I2C_GPIO_0_FORCE_OE_EN, i.e. GPIO_OE will be driven
  1207. * instead of HW_OE.
  1208. * For now, we do not reset the GPIO state when the driver is removed.
  1209. * First, it is not necessary to disable the bus since we are using
  1210. * the same busses. Then, some busses might be shared among Linux and
  1211. * platform firmware; disabling the bus might compromise the system
  1212. * functionality.
  1213. */
  1214. config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
  1215. config_reg = MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(priv->bus,
  1216. config_reg);
  1217. writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
  1218. config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
  1219. config_reg = MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(priv->bus,
  1220. config_reg);
  1221. writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
  1222. mutex_unlock(gpio_res->lock);
  1223. return 0;
  1224. }
  1225. static u64 mlxbf_i2c_calculate_freq_from_tyu(struct mlxbf_i2c_resource *corepll_res)
  1226. {
  1227. u64 core_frequency;
  1228. u8 core_od, core_r;
  1229. u32 corepll_val;
  1230. u16 core_f;
  1231. corepll_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
  1232. /* Get Core PLL configuration bits. */
  1233. core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_TYU_MASK, corepll_val);
  1234. core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK, corepll_val);
  1235. core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_TYU_MASK, corepll_val);
  1236. /*
  1237. * Compute PLL output frequency as follow:
  1238. *
  1239. * CORE_F + 1
  1240. * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
  1241. * (CORE_R + 1) * (CORE_OD + 1)
  1242. *
  1243. * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
  1244. * and PadFrequency, respectively.
  1245. */
  1246. core_frequency = MLXBF_I2C_PLL_IN_FREQ * (++core_f);
  1247. core_frequency /= (++core_r) * (++core_od);
  1248. return core_frequency;
  1249. }
  1250. static u64 mlxbf_i2c_calculate_freq_from_yu(struct mlxbf_i2c_resource *corepll_res)
  1251. {
  1252. u32 corepll_reg1_val, corepll_reg2_val;
  1253. u64 corepll_frequency;
  1254. u8 core_od, core_r;
  1255. u32 core_f;
  1256. corepll_reg1_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
  1257. corepll_reg2_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG2);
  1258. /* Get Core PLL configuration bits */
  1259. core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_YU_MASK, corepll_reg1_val);
  1260. core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_YU_MASK, corepll_reg1_val);
  1261. core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_YU_MASK, corepll_reg2_val);
  1262. /*
  1263. * Compute PLL output frequency as follow:
  1264. *
  1265. * CORE_F / 16384
  1266. * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
  1267. * (CORE_R + 1) * (CORE_OD + 1)
  1268. *
  1269. * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
  1270. * and PadFrequency, respectively.
  1271. */
  1272. corepll_frequency = (MLXBF_I2C_PLL_IN_FREQ * core_f) / MLNXBF_I2C_COREPLL_CONST;
  1273. corepll_frequency /= (++core_r) * (++core_od);
  1274. return corepll_frequency;
  1275. }
  1276. static int mlxbf_i2c_calculate_corepll_freq(struct platform_device *pdev,
  1277. struct mlxbf_i2c_priv *priv)
  1278. {
  1279. const struct mlxbf_i2c_chip_info *chip = priv->chip;
  1280. struct mlxbf_i2c_resource *corepll_res;
  1281. struct device *dev = &pdev->dev;
  1282. u64 *freq = &priv->frequency;
  1283. int ret;
  1284. corepll_res = mlxbf_i2c_get_shared_resource(priv,
  1285. MLXBF_I2C_COREPLL_RES);
  1286. if (!corepll_res)
  1287. return -EPERM;
  1288. /*
  1289. * First, check whether the TYU core Clock frequency is set.
  1290. * The TYU core frequency is the same for all I2C busses; when
  1291. * the first device gets probed the frequency is determined and
  1292. * stored into a globally visible variable. So, first of all,
  1293. * check whether the frequency is already set. Here, we assume
  1294. * that the frequency is expected to be greater than 0.
  1295. */
  1296. mutex_lock(corepll_res->lock);
  1297. if (!mlxbf_i2c_corepll_frequency) {
  1298. if (!chip->calculate_freq) {
  1299. mutex_unlock(corepll_res->lock);
  1300. return -EPERM;
  1301. }
  1302. ret = mlxbf_i2c_get_corepll(pdev, priv);
  1303. if (ret < 0) {
  1304. dev_err(dev, "Failed to get corePLL resource");
  1305. mutex_unlock(corepll_res->lock);
  1306. return ret;
  1307. }
  1308. mlxbf_i2c_corepll_frequency = chip->calculate_freq(corepll_res);
  1309. }
  1310. mutex_unlock(corepll_res->lock);
  1311. *freq = mlxbf_i2c_corepll_frequency;
  1312. return 0;
  1313. }
  1314. static int mlxbf_i2c_slave_enable(struct mlxbf_i2c_priv *priv,
  1315. struct i2c_client *slave)
  1316. {
  1317. u8 reg, reg_cnt, byte, addr_tmp;
  1318. u32 slave_reg, slave_reg_tmp;
  1319. if (!priv)
  1320. return -EPERM;
  1321. reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
  1322. /*
  1323. * Read the slave registers. There are 4 * 32-bit slave registers.
  1324. * Each slave register can hold up to 4 * 8-bit slave configuration:
  1325. * 1) A 7-bit address
  1326. * 2) And a status bit (1 if enabled, 0 if not).
  1327. * Look for the next available slave register slot.
  1328. */
  1329. for (reg = 0; reg < reg_cnt; reg++) {
  1330. slave_reg = readl(priv->slv->io +
  1331. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
  1332. /*
  1333. * Each register holds 4 slave addresses. So, we have to keep
  1334. * the byte order consistent with the value read in order to
  1335. * update the register correctly, if needed.
  1336. */
  1337. slave_reg_tmp = slave_reg;
  1338. for (byte = 0; byte < 4; byte++) {
  1339. addr_tmp = slave_reg_tmp & GENMASK(7, 0);
  1340. /*
  1341. * If an enable bit is not set in the
  1342. * MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG register, then the
  1343. * slave address slot associated with that bit is
  1344. * free. So set the enable bit and write the
  1345. * slave address bits.
  1346. */
  1347. if (!(addr_tmp & MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT)) {
  1348. slave_reg &= ~(MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK << (byte * 8));
  1349. slave_reg |= (slave->addr << (byte * 8));
  1350. slave_reg |= MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT << (byte * 8);
  1351. writel(slave_reg, priv->slv->io +
  1352. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
  1353. (reg * 0x4));
  1354. /*
  1355. * Set the slave at the corresponding index.
  1356. */
  1357. priv->slave[(reg * 4) + byte] = slave;
  1358. return 0;
  1359. }
  1360. /* Parse next byte. */
  1361. slave_reg_tmp >>= 8;
  1362. }
  1363. }
  1364. return -EBUSY;
  1365. }
  1366. static int mlxbf_i2c_slave_disable(struct mlxbf_i2c_priv *priv, u8 addr)
  1367. {
  1368. u8 addr_tmp, reg, reg_cnt, byte;
  1369. u32 slave_reg, slave_reg_tmp;
  1370. reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
  1371. /*
  1372. * Read the slave registers. There are 4 * 32-bit slave registers.
  1373. * Each slave register can hold up to 4 * 8-bit slave configuration:
  1374. * 1) A 7-bit address
  1375. * 2) And a status bit (1 if enabled, 0 if not).
  1376. * Check if addr is present in the registers.
  1377. */
  1378. for (reg = 0; reg < reg_cnt; reg++) {
  1379. slave_reg = readl(priv->slv->io +
  1380. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
  1381. /* Check whether the address slots are empty. */
  1382. if (!slave_reg)
  1383. continue;
  1384. /*
  1385. * Check if addr matches any of the 4 slave addresses
  1386. * in the register.
  1387. */
  1388. slave_reg_tmp = slave_reg;
  1389. for (byte = 0; byte < 4; byte++) {
  1390. addr_tmp = slave_reg_tmp & MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK;
  1391. /*
  1392. * Parse slave address bytes and check whether the
  1393. * slave address already exists.
  1394. */
  1395. if (addr_tmp == addr) {
  1396. /* Clear the slave address slot. */
  1397. slave_reg &= ~(GENMASK(7, 0) << (byte * 8));
  1398. writel(slave_reg, priv->slv->io +
  1399. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
  1400. (reg * 0x4));
  1401. /* Free slave at the corresponding index */
  1402. priv->slave[(reg * 4) + byte] = NULL;
  1403. return 0;
  1404. }
  1405. /* Parse next byte. */
  1406. slave_reg_tmp >>= 8;
  1407. }
  1408. }
  1409. return -ENXIO;
  1410. }
  1411. static int mlxbf_i2c_init_coalesce(struct platform_device *pdev,
  1412. struct mlxbf_i2c_priv *priv)
  1413. {
  1414. struct mlxbf_i2c_resource *coalesce_res;
  1415. struct resource *params;
  1416. resource_size_t size;
  1417. int ret = 0;
  1418. /*
  1419. * Unlike BlueField-1 platform, the coalesce registers is a dedicated
  1420. * resource in the next generations of BlueField.
  1421. */
  1422. if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
  1423. coalesce_res = mlxbf_i2c_get_shared_resource(priv,
  1424. MLXBF_I2C_COALESCE_RES);
  1425. if (!coalesce_res)
  1426. return -EPERM;
  1427. /*
  1428. * The Cause Coalesce group in TYU space is shared among
  1429. * I2C busses. This function MUST be serialized to avoid
  1430. * racing when claiming the memory region.
  1431. */
  1432. lockdep_assert_held(mlxbf_i2c_gpio_res->lock);
  1433. /* Check whether the memory map exist. */
  1434. if (coalesce_res->io) {
  1435. priv->coalesce = coalesce_res;
  1436. return 0;
  1437. }
  1438. params = coalesce_res->params;
  1439. size = resource_size(params);
  1440. if (!request_mem_region(params->start, size, params->name))
  1441. return -EFAULT;
  1442. coalesce_res->io = ioremap(params->start, size);
  1443. if (!coalesce_res->io) {
  1444. release_mem_region(params->start, size);
  1445. return -ENOMEM;
  1446. }
  1447. priv->coalesce = coalesce_res;
  1448. } else {
  1449. ret = mlxbf_i2c_init_resource(pdev, &priv->coalesce,
  1450. MLXBF_I2C_COALESCE_RES);
  1451. }
  1452. return ret;
  1453. }
  1454. static int mlxbf_i2c_release_coalesce(struct platform_device *pdev,
  1455. struct mlxbf_i2c_priv *priv)
  1456. {
  1457. struct mlxbf_i2c_resource *coalesce_res;
  1458. struct device *dev = &pdev->dev;
  1459. struct resource *params;
  1460. resource_size_t size;
  1461. coalesce_res = priv->coalesce;
  1462. if (coalesce_res->io) {
  1463. params = coalesce_res->params;
  1464. size = resource_size(params);
  1465. if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
  1466. mutex_lock(coalesce_res->lock);
  1467. iounmap(coalesce_res->io);
  1468. release_mem_region(params->start, size);
  1469. mutex_unlock(coalesce_res->lock);
  1470. } else {
  1471. devm_release_mem_region(dev, params->start, size);
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. static int mlxbf_i2c_init_slave(struct platform_device *pdev,
  1477. struct mlxbf_i2c_priv *priv)
  1478. {
  1479. struct device *dev = &pdev->dev;
  1480. u32 int_reg;
  1481. int ret;
  1482. /* Reset FSM. */
  1483. writel(0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_FSM);
  1484. /*
  1485. * Enable slave cause interrupt bits. Drive
  1486. * MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE and
  1487. * MLXBF_I2C_CAUSE_WRITE_SUCCESS, these are enabled when an external
  1488. * masters issue a Read and Write, respectively. But, clear all
  1489. * interrupts first.
  1490. */
  1491. writel(~0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
  1492. int_reg = MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE;
  1493. int_reg |= MLXBF_I2C_CAUSE_WRITE_SUCCESS;
  1494. writel(int_reg, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_EVTEN0);
  1495. /* Finally, set the 'ready' bit to start handling transactions. */
  1496. writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
  1497. /* Initialize the cause coalesce resource. */
  1498. ret = mlxbf_i2c_init_coalesce(pdev, priv);
  1499. if (ret < 0) {
  1500. dev_err(dev, "failed to initialize cause coalesce\n");
  1501. return ret;
  1502. }
  1503. return 0;
  1504. }
  1505. static bool mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv *priv, bool *read,
  1506. bool *write)
  1507. {
  1508. const struct mlxbf_i2c_chip_info *chip = priv->chip;
  1509. u32 coalesce0_reg, cause_reg;
  1510. u8 slave_shift, is_set;
  1511. *write = false;
  1512. *read = false;
  1513. slave_shift = chip->type != MLXBF_I2C_CHIP_TYPE_1 ?
  1514. MLXBF_I2C_CAUSE_YU_SLAVE_BIT :
  1515. priv->bus + MLXBF_I2C_CAUSE_TYU_SLAVE_BIT;
  1516. coalesce0_reg = readl(priv->coalesce->io + MLXBF_I2C_CAUSE_COALESCE_0);
  1517. is_set = coalesce0_reg & (1 << slave_shift);
  1518. if (!is_set)
  1519. return false;
  1520. /* Check the source of the interrupt, i.e. whether a Read or Write. */
  1521. cause_reg = readl(priv->slv_cause->io + MLXBF_I2C_CAUSE_ARBITER);
  1522. if (cause_reg & MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE)
  1523. *read = true;
  1524. else if (cause_reg & MLXBF_I2C_CAUSE_WRITE_SUCCESS)
  1525. *write = true;
  1526. /* Clear cause bits. */
  1527. writel(~0x0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
  1528. return true;
  1529. }
  1530. static bool mlxbf_i2c_slave_wait_for_idle(struct mlxbf_i2c_priv *priv,
  1531. u32 timeout)
  1532. {
  1533. u32 mask = MLXBF_I2C_CAUSE_S_GW_BUSY_FALL;
  1534. u32 addr = MLXBF_I2C_CAUSE_ARBITER;
  1535. if (mlxbf_i2c_poll(priv->slv_cause->io, addr, mask, false, timeout))
  1536. return true;
  1537. return false;
  1538. }
  1539. static struct i2c_client *mlxbf_i2c_get_slave_from_addr(
  1540. struct mlxbf_i2c_priv *priv, u8 addr)
  1541. {
  1542. int i;
  1543. for (i = 0; i < MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT; i++) {
  1544. if (!priv->slave[i])
  1545. continue;
  1546. if (priv->slave[i]->addr == addr)
  1547. return priv->slave[i];
  1548. }
  1549. return NULL;
  1550. }
  1551. /*
  1552. * Send byte to 'external' smbus master. This function is executed when
  1553. * an external smbus master wants to read data from the BlueField.
  1554. */
  1555. static int mlxbf_i2c_irq_send(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
  1556. {
  1557. u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
  1558. u8 write_size, pec_en, addr, value, byte_cnt;
  1559. struct i2c_client *slave;
  1560. u32 control32, data32;
  1561. int ret = 0;
  1562. /*
  1563. * Read the first byte received from the external master to
  1564. * determine the slave address. This byte is located in the
  1565. * first data descriptor register of the slave GW.
  1566. */
  1567. data32 = ioread32be(priv->slv->io +
  1568. MLXBF_I2C_SLAVE_DATA_DESC_ADDR);
  1569. addr = (data32 & GENMASK(7, 0)) >> 1;
  1570. /*
  1571. * Check if the slave address received in the data descriptor register
  1572. * matches any of the slave addresses registered. If there is a match,
  1573. * set the slave.
  1574. */
  1575. slave = mlxbf_i2c_get_slave_from_addr(priv, addr);
  1576. if (!slave) {
  1577. ret = -ENXIO;
  1578. goto clear_csr;
  1579. }
  1580. /*
  1581. * An I2C read can consist of a WRITE bit transaction followed by
  1582. * a READ bit transaction. Indeed, slave devices often expect
  1583. * the slave address to be followed by the internal address.
  1584. * So, write the internal address byte first, and then, send the
  1585. * requested data to the master.
  1586. */
  1587. if (recv_bytes > 1) {
  1588. i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  1589. value = (data32 >> 8) & GENMASK(7, 0);
  1590. ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
  1591. &value);
  1592. i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
  1593. if (ret < 0)
  1594. goto clear_csr;
  1595. }
  1596. /*
  1597. * Send data to the master. Currently, the driver supports
  1598. * READ_BYTE, READ_WORD and BLOCK READ protocols. The
  1599. * hardware can send up to 128 bytes per transfer which is
  1600. * the total size of the data registers.
  1601. */
  1602. i2c_slave_event(slave, I2C_SLAVE_READ_REQUESTED, &value);
  1603. for (byte_cnt = 0; byte_cnt < MLXBF_I2C_SLAVE_DATA_DESC_SIZE; byte_cnt++) {
  1604. data_desc[byte_cnt] = value;
  1605. i2c_slave_event(slave, I2C_SLAVE_READ_PROCESSED, &value);
  1606. }
  1607. /* Send a stop condition to the backend. */
  1608. i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
  1609. /* Set the number of bytes to write to master. */
  1610. write_size = (byte_cnt - 1) & 0x7f;
  1611. /* Write data to Slave GW data descriptor. */
  1612. mlxbf_i2c_smbus_write_data(priv, data_desc, byte_cnt,
  1613. MLXBF_I2C_SLAVE_DATA_DESC_ADDR, false);
  1614. pec_en = 0; /* Disable PEC since it is not supported. */
  1615. /* Prepare control word. */
  1616. control32 = MLXBF_I2C_SLAVE_ENABLE;
  1617. control32 |= rol32(write_size, MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT);
  1618. control32 |= rol32(pec_en, MLXBF_I2C_SLAVE_SEND_PEC_SHIFT);
  1619. writel(control32, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_GW);
  1620. /*
  1621. * Wait until the transfer is completed; the driver will wait
  1622. * until the GW is idle, a cause will rise on fall of GW busy.
  1623. */
  1624. mlxbf_i2c_slave_wait_for_idle(priv, MLXBF_I2C_SMBUS_TIMEOUT);
  1625. clear_csr:
  1626. /* Release the Slave GW. */
  1627. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
  1628. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
  1629. writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
  1630. return ret;
  1631. }
  1632. /*
  1633. * Receive bytes from 'external' smbus master. This function is executed when
  1634. * an external smbus master wants to write data to the BlueField.
  1635. */
  1636. static int mlxbf_i2c_irq_recv(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
  1637. {
  1638. u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
  1639. struct i2c_client *slave;
  1640. u8 value, byte, addr;
  1641. int ret = 0;
  1642. /* Read data from Slave GW data descriptor. */
  1643. mlxbf_i2c_smbus_read_data(priv, data_desc, recv_bytes,
  1644. MLXBF_I2C_SLAVE_DATA_DESC_ADDR, false);
  1645. addr = data_desc[0] >> 1;
  1646. /*
  1647. * Check if the slave address received in the data descriptor register
  1648. * matches any of the slave addresses registered.
  1649. */
  1650. slave = mlxbf_i2c_get_slave_from_addr(priv, addr);
  1651. if (!slave) {
  1652. ret = -EINVAL;
  1653. goto clear_csr;
  1654. }
  1655. /*
  1656. * Notify the slave backend that an smbus master wants to write data
  1657. * to the BlueField.
  1658. */
  1659. i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  1660. /* Send the received data to the slave backend. */
  1661. for (byte = 1; byte < recv_bytes; byte++) {
  1662. value = data_desc[byte];
  1663. ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
  1664. &value);
  1665. if (ret < 0)
  1666. break;
  1667. }
  1668. /*
  1669. * Send a stop event to the slave backend, to signal
  1670. * the end of the write transactions.
  1671. */
  1672. i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
  1673. clear_csr:
  1674. /* Release the Slave GW. */
  1675. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
  1676. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
  1677. writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
  1678. return ret;
  1679. }
  1680. static irqreturn_t mlxbf_i2c_irq(int irq, void *ptr)
  1681. {
  1682. struct mlxbf_i2c_priv *priv = ptr;
  1683. bool read, write, irq_is_set;
  1684. u32 rw_bytes_reg;
  1685. u8 recv_bytes;
  1686. /*
  1687. * Read TYU interrupt register and determine the source of the
  1688. * interrupt. Based on the source of the interrupt one of the
  1689. * following actions are performed:
  1690. * - Receive data and send response to master.
  1691. * - Send data and release slave GW.
  1692. *
  1693. * Handle read/write transaction only. CRmaster and Iarp requests
  1694. * are ignored for now.
  1695. */
  1696. irq_is_set = mlxbf_i2c_has_coalesce(priv, &read, &write);
  1697. if (!irq_is_set || (!read && !write)) {
  1698. /* Nothing to do here, interrupt was not from this device. */
  1699. return IRQ_NONE;
  1700. }
  1701. /*
  1702. * The MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES includes the number of
  1703. * bytes from/to master. These are defined by 8-bits each. If the lower
  1704. * 8 bits are set, then the master expect to read N bytes from the
  1705. * slave, if the higher 8 bits are sent then the slave expect N bytes
  1706. * from the master.
  1707. */
  1708. rw_bytes_reg = readl(priv->slv->io +
  1709. MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
  1710. recv_bytes = (rw_bytes_reg >> 8) & GENMASK(7, 0);
  1711. /*
  1712. * For now, the slave supports 128 bytes transfer. Discard remaining
  1713. * data bytes if the master wrote more than
  1714. * MLXBF_I2C_SLAVE_DATA_DESC_SIZE, i.e, the actual size of the slave
  1715. * data descriptor.
  1716. *
  1717. * Note that we will never expect to transfer more than 128 bytes; as
  1718. * specified in the SMBus standard, block transactions cannot exceed
  1719. * 32 bytes.
  1720. */
  1721. recv_bytes = recv_bytes > MLXBF_I2C_SLAVE_DATA_DESC_SIZE ?
  1722. MLXBF_I2C_SLAVE_DATA_DESC_SIZE : recv_bytes;
  1723. if (read)
  1724. mlxbf_i2c_irq_send(priv, recv_bytes);
  1725. else
  1726. mlxbf_i2c_irq_recv(priv, recv_bytes);
  1727. return IRQ_HANDLED;
  1728. }
  1729. /* Return negative errno on error. */
  1730. static s32 mlxbf_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  1731. unsigned short flags, char read_write,
  1732. u8 command, int size,
  1733. union i2c_smbus_data *data)
  1734. {
  1735. struct mlxbf_i2c_smbus_request request = { 0 };
  1736. struct mlxbf_i2c_priv *priv;
  1737. bool read, pec;
  1738. u8 byte_cnt;
  1739. request.slave = addr;
  1740. read = (read_write == I2C_SMBUS_READ);
  1741. pec = flags & I2C_FUNC_SMBUS_PEC;
  1742. switch (size) {
  1743. case I2C_SMBUS_QUICK:
  1744. mlxbf_i2c_smbus_quick_command(&request, read);
  1745. dev_dbg(&adap->dev, "smbus quick, slave 0x%02x\n", addr);
  1746. break;
  1747. case I2C_SMBUS_BYTE:
  1748. mlxbf_i2c_smbus_byte_func(&request,
  1749. read ? &data->byte : &command, read,
  1750. pec);
  1751. dev_dbg(&adap->dev, "smbus %s byte, slave 0x%02x.\n",
  1752. read ? "read" : "write", addr);
  1753. break;
  1754. case I2C_SMBUS_BYTE_DATA:
  1755. mlxbf_i2c_smbus_data_byte_func(&request, &command, &data->byte,
  1756. read, pec);
  1757. dev_dbg(&adap->dev, "smbus %s byte data at 0x%02x, slave 0x%02x.\n",
  1758. read ? "read" : "write", command, addr);
  1759. break;
  1760. case I2C_SMBUS_WORD_DATA:
  1761. mlxbf_i2c_smbus_data_word_func(&request, &command,
  1762. (u8 *)&data->word, read, pec);
  1763. dev_dbg(&adap->dev, "smbus %s word data at 0x%02x, slave 0x%02x.\n",
  1764. read ? "read" : "write", command, addr);
  1765. break;
  1766. case I2C_SMBUS_I2C_BLOCK_DATA:
  1767. byte_cnt = data->block[0];
  1768. mlxbf_i2c_smbus_i2c_block_func(&request, &command, data->block,
  1769. &byte_cnt, read, pec);
  1770. dev_dbg(&adap->dev, "i2c %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
  1771. read ? "read" : "write", byte_cnt, command, addr);
  1772. break;
  1773. case I2C_SMBUS_BLOCK_DATA:
  1774. byte_cnt = read ? I2C_SMBUS_BLOCK_MAX : data->block[0];
  1775. mlxbf_i2c_smbus_block_func(&request, &command, data->block,
  1776. &byte_cnt, read, pec);
  1777. dev_dbg(&adap->dev, "smbus %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
  1778. read ? "read" : "write", byte_cnt, command, addr);
  1779. break;
  1780. case I2C_FUNC_SMBUS_PROC_CALL:
  1781. mlxbf_i2c_smbus_process_call_func(&request, &command,
  1782. (u8 *)&data->word, pec);
  1783. dev_dbg(&adap->dev, "process call, wr/rd at 0x%02x, slave 0x%02x.\n",
  1784. command, addr);
  1785. break;
  1786. case I2C_FUNC_SMBUS_BLOCK_PROC_CALL:
  1787. byte_cnt = data->block[0];
  1788. mlxbf_i2c_smbus_blk_process_call_func(&request, &command,
  1789. data->block, &byte_cnt,
  1790. pec);
  1791. dev_dbg(&adap->dev, "block process call, wr/rd %d bytes, slave 0x%02x.\n",
  1792. byte_cnt, addr);
  1793. break;
  1794. default:
  1795. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command %d\n",
  1796. size);
  1797. return -EOPNOTSUPP;
  1798. }
  1799. priv = i2c_get_adapdata(adap);
  1800. return mlxbf_i2c_smbus_start_transaction(priv, &request);
  1801. }
  1802. static int mlxbf_i2c_reg_slave(struct i2c_client *slave)
  1803. {
  1804. struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  1805. struct device *dev = &slave->dev;
  1806. int ret;
  1807. /*
  1808. * Do not support ten bit chip address and do not use Packet Error
  1809. * Checking (PEC).
  1810. */
  1811. if (slave->flags & (I2C_CLIENT_TEN | I2C_CLIENT_PEC)) {
  1812. dev_err(dev, "SMBus PEC and 10 bit address not supported\n");
  1813. return -EAFNOSUPPORT;
  1814. }
  1815. ret = mlxbf_i2c_slave_enable(priv, slave);
  1816. if (ret)
  1817. dev_err(dev, "Surpassed max number of registered slaves allowed\n");
  1818. return 0;
  1819. }
  1820. static int mlxbf_i2c_unreg_slave(struct i2c_client *slave)
  1821. {
  1822. struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  1823. struct device *dev = &slave->dev;
  1824. int ret;
  1825. /*
  1826. * Unregister slave by:
  1827. * 1) Disabling the slave address in hardware
  1828. * 2) Freeing priv->slave at the corresponding index
  1829. */
  1830. ret = mlxbf_i2c_slave_disable(priv, slave->addr);
  1831. if (ret)
  1832. dev_err(dev, "Unable to find slave 0x%x\n", slave->addr);
  1833. return ret;
  1834. }
  1835. static u32 mlxbf_i2c_functionality(struct i2c_adapter *adap)
  1836. {
  1837. return MLXBF_I2C_FUNC_ALL;
  1838. }
  1839. static struct mlxbf_i2c_chip_info mlxbf_i2c_chip[] = {
  1840. [MLXBF_I2C_CHIP_TYPE_1] = {
  1841. .type = MLXBF_I2C_CHIP_TYPE_1,
  1842. .shared_res = {
  1843. [0] = &mlxbf_i2c_coalesce_res[MLXBF_I2C_CHIP_TYPE_1],
  1844. [1] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_1],
  1845. [2] = &mlxbf_i2c_gpio_res[MLXBF_I2C_CHIP_TYPE_1]
  1846. },
  1847. .calculate_freq = mlxbf_i2c_calculate_freq_from_tyu,
  1848. .smbus_master_rs_bytes_off = MLXBF_I2C_YU_SMBUS_RS_BYTES,
  1849. .smbus_master_fsm_off = MLXBF_I2C_YU_SMBUS_MASTER_FSM
  1850. },
  1851. [MLXBF_I2C_CHIP_TYPE_2] = {
  1852. .type = MLXBF_I2C_CHIP_TYPE_2,
  1853. .shared_res = {
  1854. [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_2]
  1855. },
  1856. .calculate_freq = mlxbf_i2c_calculate_freq_from_yu,
  1857. .smbus_master_rs_bytes_off = MLXBF_I2C_YU_SMBUS_RS_BYTES,
  1858. .smbus_master_fsm_off = MLXBF_I2C_YU_SMBUS_MASTER_FSM
  1859. },
  1860. [MLXBF_I2C_CHIP_TYPE_3] = {
  1861. .type = MLXBF_I2C_CHIP_TYPE_3,
  1862. .shared_res = {
  1863. [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_3]
  1864. },
  1865. .calculate_freq = mlxbf_i2c_calculate_freq_from_yu,
  1866. .smbus_master_rs_bytes_off = MLXBF_I2C_RSH_YU_SMBUS_RS_BYTES,
  1867. .smbus_master_fsm_off = MLXBF_I2C_RSH_YU_SMBUS_MASTER_FSM
  1868. }
  1869. };
  1870. static const struct i2c_algorithm mlxbf_i2c_algo = {
  1871. .smbus_xfer = mlxbf_i2c_smbus_xfer,
  1872. .functionality = mlxbf_i2c_functionality,
  1873. .reg_slave = mlxbf_i2c_reg_slave,
  1874. .unreg_slave = mlxbf_i2c_unreg_slave,
  1875. };
  1876. static struct i2c_adapter_quirks mlxbf_i2c_quirks = {
  1877. .max_read_len = MLXBF_I2C_MASTER_DATA_R_LENGTH,
  1878. .max_write_len = MLXBF_I2C_MASTER_DATA_W_LENGTH,
  1879. };
  1880. static const struct acpi_device_id mlxbf_i2c_acpi_ids[] = {
  1881. { "MLNXBF03", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_1] },
  1882. { "MLNXBF23", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_2] },
  1883. { "MLNXBF31", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_3] },
  1884. {},
  1885. };
  1886. MODULE_DEVICE_TABLE(acpi, mlxbf_i2c_acpi_ids);
  1887. static int mlxbf_i2c_acpi_probe(struct device *dev, struct mlxbf_i2c_priv *priv)
  1888. {
  1889. const struct acpi_device_id *aid;
  1890. u64 bus_id;
  1891. int ret;
  1892. if (acpi_disabled)
  1893. return -ENOENT;
  1894. aid = acpi_match_device(mlxbf_i2c_acpi_ids, dev);
  1895. if (!aid)
  1896. return -ENODEV;
  1897. priv->chip = (struct mlxbf_i2c_chip_info *)aid->driver_data;
  1898. ret = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &bus_id);
  1899. if (ret) {
  1900. dev_err(dev, "Cannot retrieve UID\n");
  1901. return ret;
  1902. }
  1903. priv->bus = bus_id;
  1904. return 0;
  1905. }
  1906. static int mlxbf_i2c_probe(struct platform_device *pdev)
  1907. {
  1908. struct device *dev = &pdev->dev;
  1909. struct mlxbf_i2c_priv *priv;
  1910. struct i2c_adapter *adap;
  1911. u32 resource_version;
  1912. int irq, ret;
  1913. priv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_priv), GFP_KERNEL);
  1914. if (!priv)
  1915. return -ENOMEM;
  1916. ret = mlxbf_i2c_acpi_probe(dev, priv);
  1917. if (ret < 0)
  1918. return ret;
  1919. /* This property allows the driver to stay backward compatible with older
  1920. * ACPI tables.
  1921. * Starting BlueField-3 SoC, the "smbus" resource was broken down into 3
  1922. * separate resources "timer", "master" and "slave".
  1923. */
  1924. if (device_property_read_u32(dev, "resource_version", &resource_version))
  1925. resource_version = 0;
  1926. priv->resource_version = resource_version;
  1927. if (priv->chip->type < MLXBF_I2C_CHIP_TYPE_3 && resource_version == 0) {
  1928. priv->timer = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
  1929. if (!priv->timer)
  1930. return -ENOMEM;
  1931. priv->mst = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
  1932. if (!priv->mst)
  1933. return -ENOMEM;
  1934. priv->slv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
  1935. if (!priv->slv)
  1936. return -ENOMEM;
  1937. ret = mlxbf_i2c_init_resource(pdev, &priv->smbus,
  1938. MLXBF_I2C_SMBUS_RES);
  1939. if (ret < 0)
  1940. return dev_err_probe(dev, ret, "Cannot fetch smbus resource info");
  1941. priv->timer->io = priv->smbus->io;
  1942. priv->mst->io = priv->smbus->io + MLXBF_I2C_MST_ADDR_OFFSET;
  1943. priv->slv->io = priv->smbus->io + MLXBF_I2C_SLV_ADDR_OFFSET;
  1944. } else {
  1945. ret = mlxbf_i2c_init_resource(pdev, &priv->timer,
  1946. MLXBF_I2C_SMBUS_TIMER_RES);
  1947. if (ret < 0)
  1948. return dev_err_probe(dev, ret, "Cannot fetch timer resource info");
  1949. ret = mlxbf_i2c_init_resource(pdev, &priv->mst,
  1950. MLXBF_I2C_SMBUS_MST_RES);
  1951. if (ret < 0)
  1952. return dev_err_probe(dev, ret, "Cannot fetch master resource info");
  1953. ret = mlxbf_i2c_init_resource(pdev, &priv->slv,
  1954. MLXBF_I2C_SMBUS_SLV_RES);
  1955. if (ret < 0)
  1956. return dev_err_probe(dev, ret, "Cannot fetch slave resource info");
  1957. }
  1958. ret = mlxbf_i2c_init_resource(pdev, &priv->mst_cause,
  1959. MLXBF_I2C_MST_CAUSE_RES);
  1960. if (ret < 0)
  1961. return dev_err_probe(dev, ret, "Cannot fetch cause master resource info");
  1962. ret = mlxbf_i2c_init_resource(pdev, &priv->slv_cause,
  1963. MLXBF_I2C_SLV_CAUSE_RES);
  1964. if (ret < 0)
  1965. return dev_err_probe(dev, ret, "Cannot fetch cause slave resource info");
  1966. adap = &priv->adap;
  1967. adap->owner = THIS_MODULE;
  1968. adap->class = I2C_CLASS_HWMON;
  1969. adap->algo = &mlxbf_i2c_algo;
  1970. adap->quirks = &mlxbf_i2c_quirks;
  1971. adap->dev.parent = dev;
  1972. adap->dev.of_node = dev->of_node;
  1973. adap->nr = priv->bus;
  1974. snprintf(adap->name, sizeof(adap->name), "i2c%d", adap->nr);
  1975. i2c_set_adapdata(adap, priv);
  1976. /* Read Core PLL frequency. */
  1977. ret = mlxbf_i2c_calculate_corepll_freq(pdev, priv);
  1978. if (ret < 0) {
  1979. dev_err(dev, "cannot get core clock frequency\n");
  1980. /* Set to default value. */
  1981. priv->frequency = MLXBF_I2C_COREPLL_FREQ;
  1982. }
  1983. /*
  1984. * Initialize master.
  1985. * Note that a physical bus might be shared among Linux and firmware
  1986. * (e.g., ATF). Thus, the bus should be initialized and ready and
  1987. * bus initialization would be unnecessary. This requires additional
  1988. * knowledge about physical busses. But, since an extra initialization
  1989. * does not really hurt, then keep the code as is.
  1990. */
  1991. ret = mlxbf_i2c_init_master(pdev, priv);
  1992. if (ret < 0)
  1993. return dev_err_probe(dev, ret, "failed to initialize smbus master %d",
  1994. priv->bus);
  1995. mlxbf_i2c_init_timings(pdev, priv);
  1996. mlxbf_i2c_init_slave(pdev, priv);
  1997. irq = platform_get_irq(pdev, 0);
  1998. if (irq < 0)
  1999. return irq;
  2000. ret = devm_request_irq(dev, irq, mlxbf_i2c_irq,
  2001. IRQF_SHARED | IRQF_PROBE_SHARED,
  2002. dev_name(dev), priv);
  2003. if (ret < 0)
  2004. return dev_err_probe(dev, ret, "Cannot get irq %d\n", irq);
  2005. priv->irq = irq;
  2006. platform_set_drvdata(pdev, priv);
  2007. ret = i2c_add_numbered_adapter(adap);
  2008. if (ret < 0)
  2009. return ret;
  2010. mutex_lock(&mlxbf_i2c_bus_lock);
  2011. mlxbf_i2c_bus_count++;
  2012. mutex_unlock(&mlxbf_i2c_bus_lock);
  2013. return 0;
  2014. }
  2015. static void mlxbf_i2c_remove(struct platform_device *pdev)
  2016. {
  2017. struct mlxbf_i2c_priv *priv = platform_get_drvdata(pdev);
  2018. struct device *dev = &pdev->dev;
  2019. struct resource *params;
  2020. if (priv->chip->type < MLXBF_I2C_CHIP_TYPE_3 && priv->resource_version == 0) {
  2021. params = priv->smbus->params;
  2022. devm_release_mem_region(dev, params->start, resource_size(params));
  2023. } else {
  2024. params = priv->timer->params;
  2025. devm_release_mem_region(dev, params->start, resource_size(params));
  2026. params = priv->mst->params;
  2027. devm_release_mem_region(dev, params->start, resource_size(params));
  2028. params = priv->slv->params;
  2029. devm_release_mem_region(dev, params->start, resource_size(params));
  2030. }
  2031. params = priv->mst_cause->params;
  2032. devm_release_mem_region(dev, params->start, resource_size(params));
  2033. params = priv->slv_cause->params;
  2034. devm_release_mem_region(dev, params->start, resource_size(params));
  2035. /*
  2036. * Release shared resources. This should be done when releasing
  2037. * the I2C controller.
  2038. */
  2039. mutex_lock(&mlxbf_i2c_bus_lock);
  2040. if (--mlxbf_i2c_bus_count == 0) {
  2041. mlxbf_i2c_release_coalesce(pdev, priv);
  2042. mlxbf_i2c_release_corepll(pdev, priv);
  2043. mlxbf_i2c_release_gpio(pdev, priv);
  2044. }
  2045. mutex_unlock(&mlxbf_i2c_bus_lock);
  2046. devm_free_irq(dev, priv->irq, priv);
  2047. i2c_del_adapter(&priv->adap);
  2048. }
  2049. static struct platform_driver mlxbf_i2c_driver = {
  2050. .probe = mlxbf_i2c_probe,
  2051. .remove_new = mlxbf_i2c_remove,
  2052. .driver = {
  2053. .name = "i2c-mlxbf",
  2054. .acpi_match_table = ACPI_PTR(mlxbf_i2c_acpi_ids),
  2055. },
  2056. };
  2057. static int __init mlxbf_i2c_init(void)
  2058. {
  2059. mutex_init(&mlxbf_i2c_coalesce_lock);
  2060. mutex_init(&mlxbf_i2c_corepll_lock);
  2061. mutex_init(&mlxbf_i2c_gpio_lock);
  2062. mutex_init(&mlxbf_i2c_bus_lock);
  2063. return platform_driver_register(&mlxbf_i2c_driver);
  2064. }
  2065. module_init(mlxbf_i2c_init);
  2066. static void __exit mlxbf_i2c_exit(void)
  2067. {
  2068. platform_driver_unregister(&mlxbf_i2c_driver);
  2069. mutex_destroy(&mlxbf_i2c_bus_lock);
  2070. mutex_destroy(&mlxbf_i2c_gpio_lock);
  2071. mutex_destroy(&mlxbf_i2c_corepll_lock);
  2072. mutex_destroy(&mlxbf_i2c_coalesce_lock);
  2073. }
  2074. module_exit(mlxbf_i2c_exit);
  2075. MODULE_DESCRIPTION("Mellanox BlueField I2C bus driver");
  2076. MODULE_AUTHOR("Khalil Blaiech <kblaiech@nvidia.com>");
  2077. MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
  2078. MODULE_LICENSE("GPL v2");