i2c-mlxbf.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  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_device.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->params = platform_get_resource(pdev, IORESOURCE_MEM, type);
  909. if (!tmp_res->params) {
  910. devm_kfree(dev, tmp_res);
  911. return -EIO;
  912. }
  913. tmp_res->io = devm_ioremap_resource(dev, tmp_res->params);
  914. if (IS_ERR(tmp_res->io)) {
  915. devm_kfree(dev, tmp_res);
  916. return PTR_ERR(tmp_res->io);
  917. }
  918. tmp_res->type = type;
  919. *res = tmp_res;
  920. return 0;
  921. }
  922. static u32 mlxbf_i2c_get_ticks(struct mlxbf_i2c_priv *priv, u64 nanoseconds,
  923. bool minimum)
  924. {
  925. u64 frequency;
  926. u32 ticks;
  927. /*
  928. * Compute ticks as follow:
  929. *
  930. * Ticks
  931. * Time = --------- x 10^9 => Ticks = Time x Frequency x 10^-9
  932. * Frequency
  933. */
  934. frequency = priv->frequency;
  935. ticks = (nanoseconds * frequency) / MLXBF_I2C_FREQUENCY_1GHZ;
  936. /*
  937. * The number of ticks is rounded down and if minimum is equal to 1
  938. * then add one tick.
  939. */
  940. if (minimum)
  941. ticks++;
  942. return ticks;
  943. }
  944. static u32 mlxbf_i2c_set_timer(struct mlxbf_i2c_priv *priv, u64 nsec, bool opt,
  945. u32 mask, u8 shift)
  946. {
  947. u32 val = (mlxbf_i2c_get_ticks(priv, nsec, opt) & mask) << shift;
  948. return val;
  949. }
  950. static void mlxbf_i2c_set_timings(struct mlxbf_i2c_priv *priv,
  951. const struct mlxbf_i2c_timings *timings)
  952. {
  953. u32 timer;
  954. timer = mlxbf_i2c_set_timer(priv, timings->scl_high,
  955. false, MLXBF_I2C_MASK_16,
  956. MLXBF_I2C_SHIFT_0);
  957. timer |= mlxbf_i2c_set_timer(priv, timings->scl_low,
  958. false, MLXBF_I2C_MASK_16,
  959. MLXBF_I2C_SHIFT_16);
  960. writel(timer, priv->timer->io +
  961. MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH);
  962. timer = mlxbf_i2c_set_timer(priv, timings->sda_rise, false,
  963. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_0);
  964. timer |= mlxbf_i2c_set_timer(priv, timings->sda_fall, false,
  965. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_8);
  966. timer |= mlxbf_i2c_set_timer(priv, timings->scl_rise, false,
  967. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_16);
  968. timer |= mlxbf_i2c_set_timer(priv, timings->scl_fall, false,
  969. MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_24);
  970. writel(timer, priv->timer->io +
  971. MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE);
  972. timer = mlxbf_i2c_set_timer(priv, timings->hold_start, true,
  973. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  974. timer |= mlxbf_i2c_set_timer(priv, timings->hold_data, true,
  975. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
  976. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_TIMER_THOLD);
  977. timer = mlxbf_i2c_set_timer(priv, timings->setup_start, true,
  978. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  979. timer |= mlxbf_i2c_set_timer(priv, timings->setup_stop, true,
  980. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
  981. writel(timer, priv->timer->io +
  982. MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP);
  983. timer = mlxbf_i2c_set_timer(priv, timings->setup_data, true,
  984. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  985. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA);
  986. timer = mlxbf_i2c_set_timer(priv, timings->buf, false,
  987. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
  988. timer |= mlxbf_i2c_set_timer(priv, timings->thigh_max, false,
  989. MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
  990. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_THIGH_MAX_TBUF);
  991. timer = timings->timeout;
  992. writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT);
  993. }
  994. enum mlxbf_i2c_timings_config {
  995. MLXBF_I2C_TIMING_CONFIG_100KHZ,
  996. MLXBF_I2C_TIMING_CONFIG_400KHZ,
  997. MLXBF_I2C_TIMING_CONFIG_1000KHZ,
  998. };
  999. /*
  1000. * Note that the mlxbf_i2c_timings->timeout value is not related to the
  1001. * bus frequency, it is impacted by the time it takes the driver to
  1002. * complete data transmission before transaction abort.
  1003. */
  1004. static const struct mlxbf_i2c_timings mlxbf_i2c_timings[] = {
  1005. [MLXBF_I2C_TIMING_CONFIG_100KHZ] = {
  1006. .scl_high = 4810,
  1007. .scl_low = 5000,
  1008. .hold_start = 4000,
  1009. .setup_start = 4800,
  1010. .setup_stop = 4000,
  1011. .setup_data = 250,
  1012. .sda_rise = 50,
  1013. .sda_fall = 50,
  1014. .scl_rise = 50,
  1015. .scl_fall = 50,
  1016. .hold_data = 300,
  1017. .buf = 20000,
  1018. .thigh_max = 5000,
  1019. .timeout = 106500
  1020. },
  1021. [MLXBF_I2C_TIMING_CONFIG_400KHZ] = {
  1022. .scl_high = 1011,
  1023. .scl_low = 1300,
  1024. .hold_start = 600,
  1025. .setup_start = 700,
  1026. .setup_stop = 600,
  1027. .setup_data = 100,
  1028. .sda_rise = 50,
  1029. .sda_fall = 50,
  1030. .scl_rise = 50,
  1031. .scl_fall = 50,
  1032. .hold_data = 300,
  1033. .buf = 20000,
  1034. .thigh_max = 5000,
  1035. .timeout = 106500
  1036. },
  1037. [MLXBF_I2C_TIMING_CONFIG_1000KHZ] = {
  1038. .scl_high = 600,
  1039. .scl_low = 1300,
  1040. .hold_start = 600,
  1041. .setup_start = 600,
  1042. .setup_stop = 600,
  1043. .setup_data = 100,
  1044. .sda_rise = 50,
  1045. .sda_fall = 50,
  1046. .scl_rise = 50,
  1047. .scl_fall = 50,
  1048. .hold_data = 300,
  1049. .buf = 20000,
  1050. .thigh_max = 5000,
  1051. .timeout = 106500
  1052. }
  1053. };
  1054. static int mlxbf_i2c_init_timings(struct platform_device *pdev,
  1055. struct mlxbf_i2c_priv *priv)
  1056. {
  1057. enum mlxbf_i2c_timings_config config_idx;
  1058. struct device *dev = &pdev->dev;
  1059. u32 config_khz;
  1060. int ret;
  1061. ret = device_property_read_u32(dev, "clock-frequency", &config_khz);
  1062. if (ret < 0)
  1063. config_khz = I2C_MAX_STANDARD_MODE_FREQ;
  1064. switch (config_khz) {
  1065. default:
  1066. /* Default settings is 100 KHz. */
  1067. pr_warn("Illegal value %d: defaulting to 100 KHz\n",
  1068. config_khz);
  1069. fallthrough;
  1070. case I2C_MAX_STANDARD_MODE_FREQ:
  1071. config_idx = MLXBF_I2C_TIMING_CONFIG_100KHZ;
  1072. break;
  1073. case I2C_MAX_FAST_MODE_FREQ:
  1074. config_idx = MLXBF_I2C_TIMING_CONFIG_400KHZ;
  1075. break;
  1076. case I2C_MAX_FAST_MODE_PLUS_FREQ:
  1077. config_idx = MLXBF_I2C_TIMING_CONFIG_1000KHZ;
  1078. break;
  1079. }
  1080. mlxbf_i2c_set_timings(priv, &mlxbf_i2c_timings[config_idx]);
  1081. return 0;
  1082. }
  1083. static int mlxbf_i2c_get_gpio(struct platform_device *pdev,
  1084. struct mlxbf_i2c_priv *priv)
  1085. {
  1086. struct mlxbf_i2c_resource *gpio_res;
  1087. struct device *dev = &pdev->dev;
  1088. struct resource *params;
  1089. resource_size_t size;
  1090. gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
  1091. if (!gpio_res)
  1092. return -EPERM;
  1093. /*
  1094. * The GPIO region in TYU space is shared among I2C busses.
  1095. * This function MUST be serialized to avoid racing when
  1096. * claiming the memory region and/or setting up the GPIO.
  1097. */
  1098. lockdep_assert_held(gpio_res->lock);
  1099. /* Check whether the memory map exist. */
  1100. if (gpio_res->io)
  1101. return 0;
  1102. params = gpio_res->params;
  1103. size = resource_size(params);
  1104. if (!devm_request_mem_region(dev, params->start, size, params->name))
  1105. return -EFAULT;
  1106. gpio_res->io = devm_ioremap(dev, params->start, size);
  1107. if (!gpio_res->io) {
  1108. devm_release_mem_region(dev, params->start, size);
  1109. return -ENOMEM;
  1110. }
  1111. return 0;
  1112. }
  1113. static int mlxbf_i2c_release_gpio(struct platform_device *pdev,
  1114. struct mlxbf_i2c_priv *priv)
  1115. {
  1116. struct mlxbf_i2c_resource *gpio_res;
  1117. struct device *dev = &pdev->dev;
  1118. struct resource *params;
  1119. gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
  1120. if (!gpio_res)
  1121. return 0;
  1122. mutex_lock(gpio_res->lock);
  1123. if (gpio_res->io) {
  1124. /* Release the GPIO resource. */
  1125. params = gpio_res->params;
  1126. devm_iounmap(dev, gpio_res->io);
  1127. devm_release_mem_region(dev, params->start,
  1128. resource_size(params));
  1129. }
  1130. mutex_unlock(gpio_res->lock);
  1131. return 0;
  1132. }
  1133. static int mlxbf_i2c_get_corepll(struct platform_device *pdev,
  1134. struct mlxbf_i2c_priv *priv)
  1135. {
  1136. struct mlxbf_i2c_resource *corepll_res;
  1137. struct device *dev = &pdev->dev;
  1138. struct resource *params;
  1139. resource_size_t size;
  1140. corepll_res = mlxbf_i2c_get_shared_resource(priv,
  1141. MLXBF_I2C_COREPLL_RES);
  1142. if (!corepll_res)
  1143. return -EPERM;
  1144. /*
  1145. * The COREPLL region in TYU space is shared among I2C busses.
  1146. * This function MUST be serialized to avoid racing when
  1147. * claiming the memory region.
  1148. */
  1149. lockdep_assert_held(corepll_res->lock);
  1150. /* Check whether the memory map exist. */
  1151. if (corepll_res->io)
  1152. return 0;
  1153. params = corepll_res->params;
  1154. size = resource_size(params);
  1155. if (!devm_request_mem_region(dev, params->start, size, params->name))
  1156. return -EFAULT;
  1157. corepll_res->io = devm_ioremap(dev, params->start, size);
  1158. if (!corepll_res->io) {
  1159. devm_release_mem_region(dev, params->start, size);
  1160. return -ENOMEM;
  1161. }
  1162. return 0;
  1163. }
  1164. static int mlxbf_i2c_release_corepll(struct platform_device *pdev,
  1165. struct mlxbf_i2c_priv *priv)
  1166. {
  1167. struct mlxbf_i2c_resource *corepll_res;
  1168. struct device *dev = &pdev->dev;
  1169. struct resource *params;
  1170. corepll_res = mlxbf_i2c_get_shared_resource(priv,
  1171. MLXBF_I2C_COREPLL_RES);
  1172. mutex_lock(corepll_res->lock);
  1173. if (corepll_res->io) {
  1174. /* Release the CorePLL resource. */
  1175. params = corepll_res->params;
  1176. devm_iounmap(dev, corepll_res->io);
  1177. devm_release_mem_region(dev, params->start,
  1178. resource_size(params));
  1179. }
  1180. mutex_unlock(corepll_res->lock);
  1181. return 0;
  1182. }
  1183. static int mlxbf_i2c_init_master(struct platform_device *pdev,
  1184. struct mlxbf_i2c_priv *priv)
  1185. {
  1186. struct mlxbf_i2c_resource *gpio_res;
  1187. struct device *dev = &pdev->dev;
  1188. u32 config_reg;
  1189. int ret;
  1190. /* This configuration is only needed for BlueField 1. */
  1191. if (!mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1))
  1192. return 0;
  1193. gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
  1194. if (!gpio_res)
  1195. return -EPERM;
  1196. /*
  1197. * The GPIO region in TYU space is shared among I2C busses.
  1198. * This function MUST be serialized to avoid racing when
  1199. * claiming the memory region and/or setting up the GPIO.
  1200. */
  1201. mutex_lock(gpio_res->lock);
  1202. ret = mlxbf_i2c_get_gpio(pdev, priv);
  1203. if (ret < 0) {
  1204. dev_err(dev, "Failed to get gpio resource");
  1205. mutex_unlock(gpio_res->lock);
  1206. return ret;
  1207. }
  1208. /*
  1209. * TYU - Configuration for GPIO pins. Those pins must be asserted in
  1210. * MLXBF_I2C_GPIO_0_FUNC_EN_0, i.e. GPIO 0 is controlled by HW, and must
  1211. * be reset in MLXBF_I2C_GPIO_0_FORCE_OE_EN, i.e. GPIO_OE will be driven
  1212. * instead of HW_OE.
  1213. * For now, we do not reset the GPIO state when the driver is removed.
  1214. * First, it is not necessary to disable the bus since we are using
  1215. * the same busses. Then, some busses might be shared among Linux and
  1216. * platform firmware; disabling the bus might compromise the system
  1217. * functionality.
  1218. */
  1219. config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
  1220. config_reg = MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(priv->bus,
  1221. config_reg);
  1222. writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
  1223. config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
  1224. config_reg = MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(priv->bus,
  1225. config_reg);
  1226. writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
  1227. mutex_unlock(gpio_res->lock);
  1228. return 0;
  1229. }
  1230. static u64 mlxbf_i2c_calculate_freq_from_tyu(struct mlxbf_i2c_resource *corepll_res)
  1231. {
  1232. u64 core_frequency;
  1233. u8 core_od, core_r;
  1234. u32 corepll_val;
  1235. u16 core_f;
  1236. corepll_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
  1237. /* Get Core PLL configuration bits. */
  1238. core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_TYU_MASK, corepll_val);
  1239. core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK, corepll_val);
  1240. core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_TYU_MASK, corepll_val);
  1241. /*
  1242. * Compute PLL output frequency as follow:
  1243. *
  1244. * CORE_F + 1
  1245. * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
  1246. * (CORE_R + 1) * (CORE_OD + 1)
  1247. *
  1248. * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
  1249. * and PadFrequency, respectively.
  1250. */
  1251. core_frequency = MLXBF_I2C_PLL_IN_FREQ * (++core_f);
  1252. core_frequency /= (++core_r) * (++core_od);
  1253. return core_frequency;
  1254. }
  1255. static u64 mlxbf_i2c_calculate_freq_from_yu(struct mlxbf_i2c_resource *corepll_res)
  1256. {
  1257. u32 corepll_reg1_val, corepll_reg2_val;
  1258. u64 corepll_frequency;
  1259. u8 core_od, core_r;
  1260. u32 core_f;
  1261. corepll_reg1_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
  1262. corepll_reg2_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG2);
  1263. /* Get Core PLL configuration bits */
  1264. core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_YU_MASK, corepll_reg1_val);
  1265. core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_YU_MASK, corepll_reg1_val);
  1266. core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_YU_MASK, corepll_reg2_val);
  1267. /*
  1268. * Compute PLL output frequency as follow:
  1269. *
  1270. * CORE_F / 16384
  1271. * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
  1272. * (CORE_R + 1) * (CORE_OD + 1)
  1273. *
  1274. * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
  1275. * and PadFrequency, respectively.
  1276. */
  1277. corepll_frequency = (MLXBF_I2C_PLL_IN_FREQ * core_f) / MLNXBF_I2C_COREPLL_CONST;
  1278. corepll_frequency /= (++core_r) * (++core_od);
  1279. return corepll_frequency;
  1280. }
  1281. static int mlxbf_i2c_calculate_corepll_freq(struct platform_device *pdev,
  1282. struct mlxbf_i2c_priv *priv)
  1283. {
  1284. const struct mlxbf_i2c_chip_info *chip = priv->chip;
  1285. struct mlxbf_i2c_resource *corepll_res;
  1286. struct device *dev = &pdev->dev;
  1287. u64 *freq = &priv->frequency;
  1288. int ret;
  1289. corepll_res = mlxbf_i2c_get_shared_resource(priv,
  1290. MLXBF_I2C_COREPLL_RES);
  1291. if (!corepll_res)
  1292. return -EPERM;
  1293. /*
  1294. * First, check whether the TYU core Clock frequency is set.
  1295. * The TYU core frequency is the same for all I2C busses; when
  1296. * the first device gets probed the frequency is determined and
  1297. * stored into a globally visible variable. So, first of all,
  1298. * check whether the frequency is already set. Here, we assume
  1299. * that the frequency is expected to be greater than 0.
  1300. */
  1301. mutex_lock(corepll_res->lock);
  1302. if (!mlxbf_i2c_corepll_frequency) {
  1303. if (!chip->calculate_freq) {
  1304. mutex_unlock(corepll_res->lock);
  1305. return -EPERM;
  1306. }
  1307. ret = mlxbf_i2c_get_corepll(pdev, priv);
  1308. if (ret < 0) {
  1309. dev_err(dev, "Failed to get corePLL resource");
  1310. mutex_unlock(corepll_res->lock);
  1311. return ret;
  1312. }
  1313. mlxbf_i2c_corepll_frequency = chip->calculate_freq(corepll_res);
  1314. }
  1315. mutex_unlock(corepll_res->lock);
  1316. *freq = mlxbf_i2c_corepll_frequency;
  1317. return 0;
  1318. }
  1319. static int mlxbf_i2c_slave_enable(struct mlxbf_i2c_priv *priv,
  1320. struct i2c_client *slave)
  1321. {
  1322. u8 reg, reg_cnt, byte, addr_tmp;
  1323. u32 slave_reg, slave_reg_tmp;
  1324. if (!priv)
  1325. return -EPERM;
  1326. reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
  1327. /*
  1328. * Read the slave registers. There are 4 * 32-bit slave registers.
  1329. * Each slave register can hold up to 4 * 8-bit slave configuration:
  1330. * 1) A 7-bit address
  1331. * 2) And a status bit (1 if enabled, 0 if not).
  1332. * Look for the next available slave register slot.
  1333. */
  1334. for (reg = 0; reg < reg_cnt; reg++) {
  1335. slave_reg = readl(priv->slv->io +
  1336. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
  1337. /*
  1338. * Each register holds 4 slave addresses. So, we have to keep
  1339. * the byte order consistent with the value read in order to
  1340. * update the register correctly, if needed.
  1341. */
  1342. slave_reg_tmp = slave_reg;
  1343. for (byte = 0; byte < 4; byte++) {
  1344. addr_tmp = slave_reg_tmp & GENMASK(7, 0);
  1345. /*
  1346. * If an enable bit is not set in the
  1347. * MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG register, then the
  1348. * slave address slot associated with that bit is
  1349. * free. So set the enable bit and write the
  1350. * slave address bits.
  1351. */
  1352. if (!(addr_tmp & MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT)) {
  1353. slave_reg &= ~(MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK << (byte * 8));
  1354. slave_reg |= (slave->addr << (byte * 8));
  1355. slave_reg |= MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT << (byte * 8);
  1356. writel(slave_reg, priv->slv->io +
  1357. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
  1358. (reg * 0x4));
  1359. /*
  1360. * Set the slave at the corresponding index.
  1361. */
  1362. priv->slave[(reg * 4) + byte] = slave;
  1363. return 0;
  1364. }
  1365. /* Parse next byte. */
  1366. slave_reg_tmp >>= 8;
  1367. }
  1368. }
  1369. return -EBUSY;
  1370. }
  1371. static int mlxbf_i2c_slave_disable(struct mlxbf_i2c_priv *priv, u8 addr)
  1372. {
  1373. u8 addr_tmp, reg, reg_cnt, byte;
  1374. u32 slave_reg, slave_reg_tmp;
  1375. reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
  1376. /*
  1377. * Read the slave registers. There are 4 * 32-bit slave registers.
  1378. * Each slave register can hold up to 4 * 8-bit slave configuration:
  1379. * 1) A 7-bit address
  1380. * 2) And a status bit (1 if enabled, 0 if not).
  1381. * Check if addr is present in the registers.
  1382. */
  1383. for (reg = 0; reg < reg_cnt; reg++) {
  1384. slave_reg = readl(priv->slv->io +
  1385. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
  1386. /* Check whether the address slots are empty. */
  1387. if (!slave_reg)
  1388. continue;
  1389. /*
  1390. * Check if addr matches any of the 4 slave addresses
  1391. * in the register.
  1392. */
  1393. slave_reg_tmp = slave_reg;
  1394. for (byte = 0; byte < 4; byte++) {
  1395. addr_tmp = slave_reg_tmp & MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK;
  1396. /*
  1397. * Parse slave address bytes and check whether the
  1398. * slave address already exists.
  1399. */
  1400. if (addr_tmp == addr) {
  1401. /* Clear the slave address slot. */
  1402. slave_reg &= ~(GENMASK(7, 0) << (byte * 8));
  1403. writel(slave_reg, priv->slv->io +
  1404. MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
  1405. (reg * 0x4));
  1406. /* Free slave at the corresponding index */
  1407. priv->slave[(reg * 4) + byte] = NULL;
  1408. return 0;
  1409. }
  1410. /* Parse next byte. */
  1411. slave_reg_tmp >>= 8;
  1412. }
  1413. }
  1414. return -ENXIO;
  1415. }
  1416. static int mlxbf_i2c_init_coalesce(struct platform_device *pdev,
  1417. struct mlxbf_i2c_priv *priv)
  1418. {
  1419. struct mlxbf_i2c_resource *coalesce_res;
  1420. struct resource *params;
  1421. resource_size_t size;
  1422. int ret = 0;
  1423. /*
  1424. * Unlike BlueField-1 platform, the coalesce registers is a dedicated
  1425. * resource in the next generations of BlueField.
  1426. */
  1427. if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
  1428. coalesce_res = mlxbf_i2c_get_shared_resource(priv,
  1429. MLXBF_I2C_COALESCE_RES);
  1430. if (!coalesce_res)
  1431. return -EPERM;
  1432. /*
  1433. * The Cause Coalesce group in TYU space is shared among
  1434. * I2C busses. This function MUST be serialized to avoid
  1435. * racing when claiming the memory region.
  1436. */
  1437. lockdep_assert_held(mlxbf_i2c_gpio_res->lock);
  1438. /* Check whether the memory map exist. */
  1439. if (coalesce_res->io) {
  1440. priv->coalesce = coalesce_res;
  1441. return 0;
  1442. }
  1443. params = coalesce_res->params;
  1444. size = resource_size(params);
  1445. if (!request_mem_region(params->start, size, params->name))
  1446. return -EFAULT;
  1447. coalesce_res->io = ioremap(params->start, size);
  1448. if (!coalesce_res->io) {
  1449. release_mem_region(params->start, size);
  1450. return -ENOMEM;
  1451. }
  1452. priv->coalesce = coalesce_res;
  1453. } else {
  1454. ret = mlxbf_i2c_init_resource(pdev, &priv->coalesce,
  1455. MLXBF_I2C_COALESCE_RES);
  1456. }
  1457. return ret;
  1458. }
  1459. static int mlxbf_i2c_release_coalesce(struct platform_device *pdev,
  1460. struct mlxbf_i2c_priv *priv)
  1461. {
  1462. struct mlxbf_i2c_resource *coalesce_res;
  1463. struct device *dev = &pdev->dev;
  1464. struct resource *params;
  1465. resource_size_t size;
  1466. coalesce_res = priv->coalesce;
  1467. if (coalesce_res->io) {
  1468. params = coalesce_res->params;
  1469. size = resource_size(params);
  1470. if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
  1471. mutex_lock(coalesce_res->lock);
  1472. iounmap(coalesce_res->io);
  1473. release_mem_region(params->start, size);
  1474. mutex_unlock(coalesce_res->lock);
  1475. } else {
  1476. devm_release_mem_region(dev, params->start, size);
  1477. }
  1478. }
  1479. return 0;
  1480. }
  1481. static int mlxbf_i2c_init_slave(struct platform_device *pdev,
  1482. struct mlxbf_i2c_priv *priv)
  1483. {
  1484. struct device *dev = &pdev->dev;
  1485. u32 int_reg;
  1486. int ret;
  1487. /* Reset FSM. */
  1488. writel(0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_FSM);
  1489. /*
  1490. * Enable slave cause interrupt bits. Drive
  1491. * MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE and
  1492. * MLXBF_I2C_CAUSE_WRITE_SUCCESS, these are enabled when an external
  1493. * masters issue a Read and Write, respectively. But, clear all
  1494. * interrupts first.
  1495. */
  1496. writel(~0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
  1497. int_reg = MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE;
  1498. int_reg |= MLXBF_I2C_CAUSE_WRITE_SUCCESS;
  1499. writel(int_reg, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_EVTEN0);
  1500. /* Finally, set the 'ready' bit to start handling transactions. */
  1501. writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
  1502. /* Initialize the cause coalesce resource. */
  1503. ret = mlxbf_i2c_init_coalesce(pdev, priv);
  1504. if (ret < 0) {
  1505. dev_err(dev, "failed to initialize cause coalesce\n");
  1506. return ret;
  1507. }
  1508. return 0;
  1509. }
  1510. static bool mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv *priv, bool *read,
  1511. bool *write)
  1512. {
  1513. const struct mlxbf_i2c_chip_info *chip = priv->chip;
  1514. u32 coalesce0_reg, cause_reg;
  1515. u8 slave_shift, is_set;
  1516. *write = false;
  1517. *read = false;
  1518. slave_shift = chip->type != MLXBF_I2C_CHIP_TYPE_1 ?
  1519. MLXBF_I2C_CAUSE_YU_SLAVE_BIT :
  1520. priv->bus + MLXBF_I2C_CAUSE_TYU_SLAVE_BIT;
  1521. coalesce0_reg = readl(priv->coalesce->io + MLXBF_I2C_CAUSE_COALESCE_0);
  1522. is_set = coalesce0_reg & (1 << slave_shift);
  1523. if (!is_set)
  1524. return false;
  1525. /* Check the source of the interrupt, i.e. whether a Read or Write. */
  1526. cause_reg = readl(priv->slv_cause->io + MLXBF_I2C_CAUSE_ARBITER);
  1527. if (cause_reg & MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE)
  1528. *read = true;
  1529. else if (cause_reg & MLXBF_I2C_CAUSE_WRITE_SUCCESS)
  1530. *write = true;
  1531. /* Clear cause bits. */
  1532. writel(~0x0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
  1533. return true;
  1534. }
  1535. static bool mlxbf_i2c_slave_wait_for_idle(struct mlxbf_i2c_priv *priv,
  1536. u32 timeout)
  1537. {
  1538. u32 mask = MLXBF_I2C_CAUSE_S_GW_BUSY_FALL;
  1539. u32 addr = MLXBF_I2C_CAUSE_ARBITER;
  1540. if (mlxbf_i2c_poll(priv->slv_cause->io, addr, mask, false, timeout))
  1541. return true;
  1542. return false;
  1543. }
  1544. static struct i2c_client *mlxbf_i2c_get_slave_from_addr(
  1545. struct mlxbf_i2c_priv *priv, u8 addr)
  1546. {
  1547. int i;
  1548. for (i = 0; i < MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT; i++) {
  1549. if (!priv->slave[i])
  1550. continue;
  1551. if (priv->slave[i]->addr == addr)
  1552. return priv->slave[i];
  1553. }
  1554. return NULL;
  1555. }
  1556. /*
  1557. * Send byte to 'external' smbus master. This function is executed when
  1558. * an external smbus master wants to read data from the BlueField.
  1559. */
  1560. static int mlxbf_i2c_irq_send(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
  1561. {
  1562. u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
  1563. u8 write_size, pec_en, addr, value, byte_cnt;
  1564. struct i2c_client *slave;
  1565. u32 control32, data32;
  1566. int ret = 0;
  1567. /*
  1568. * Read the first byte received from the external master to
  1569. * determine the slave address. This byte is located in the
  1570. * first data descriptor register of the slave GW.
  1571. */
  1572. data32 = ioread32be(priv->slv->io +
  1573. MLXBF_I2C_SLAVE_DATA_DESC_ADDR);
  1574. addr = (data32 & GENMASK(7, 0)) >> 1;
  1575. /*
  1576. * Check if the slave address received in the data descriptor register
  1577. * matches any of the slave addresses registered. If there is a match,
  1578. * set the slave.
  1579. */
  1580. slave = mlxbf_i2c_get_slave_from_addr(priv, addr);
  1581. if (!slave) {
  1582. ret = -ENXIO;
  1583. goto clear_csr;
  1584. }
  1585. /*
  1586. * An I2C read can consist of a WRITE bit transaction followed by
  1587. * a READ bit transaction. Indeed, slave devices often expect
  1588. * the slave address to be followed by the internal address.
  1589. * So, write the internal address byte first, and then, send the
  1590. * requested data to the master.
  1591. */
  1592. if (recv_bytes > 1) {
  1593. i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  1594. value = (data32 >> 8) & GENMASK(7, 0);
  1595. ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
  1596. &value);
  1597. i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
  1598. if (ret < 0)
  1599. goto clear_csr;
  1600. }
  1601. /*
  1602. * Send data to the master. Currently, the driver supports
  1603. * READ_BYTE, READ_WORD and BLOCK READ protocols. The
  1604. * hardware can send up to 128 bytes per transfer which is
  1605. * the total size of the data registers.
  1606. */
  1607. i2c_slave_event(slave, I2C_SLAVE_READ_REQUESTED, &value);
  1608. for (byte_cnt = 0; byte_cnt < MLXBF_I2C_SLAVE_DATA_DESC_SIZE; byte_cnt++) {
  1609. data_desc[byte_cnt] = value;
  1610. i2c_slave_event(slave, I2C_SLAVE_READ_PROCESSED, &value);
  1611. }
  1612. /* Send a stop condition to the backend. */
  1613. i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
  1614. /* Set the number of bytes to write to master. */
  1615. write_size = (byte_cnt - 1) & 0x7f;
  1616. /* Write data to Slave GW data descriptor. */
  1617. mlxbf_i2c_smbus_write_data(priv, data_desc, byte_cnt,
  1618. MLXBF_I2C_SLAVE_DATA_DESC_ADDR, false);
  1619. pec_en = 0; /* Disable PEC since it is not supported. */
  1620. /* Prepare control word. */
  1621. control32 = MLXBF_I2C_SLAVE_ENABLE;
  1622. control32 |= rol32(write_size, MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT);
  1623. control32 |= rol32(pec_en, MLXBF_I2C_SLAVE_SEND_PEC_SHIFT);
  1624. writel(control32, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_GW);
  1625. /*
  1626. * Wait until the transfer is completed; the driver will wait
  1627. * until the GW is idle, a cause will rise on fall of GW busy.
  1628. */
  1629. mlxbf_i2c_slave_wait_for_idle(priv, MLXBF_I2C_SMBUS_TIMEOUT);
  1630. clear_csr:
  1631. /* Release the Slave GW. */
  1632. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
  1633. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
  1634. writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
  1635. return ret;
  1636. }
  1637. /*
  1638. * Receive bytes from 'external' smbus master. This function is executed when
  1639. * an external smbus master wants to write data to the BlueField.
  1640. */
  1641. static int mlxbf_i2c_irq_recv(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
  1642. {
  1643. u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
  1644. struct i2c_client *slave;
  1645. u8 value, byte, addr;
  1646. int ret = 0;
  1647. /* Read data from Slave GW data descriptor. */
  1648. mlxbf_i2c_smbus_read_data(priv, data_desc, recv_bytes,
  1649. MLXBF_I2C_SLAVE_DATA_DESC_ADDR, false);
  1650. addr = data_desc[0] >> 1;
  1651. /*
  1652. * Check if the slave address received in the data descriptor register
  1653. * matches any of the slave addresses registered.
  1654. */
  1655. slave = mlxbf_i2c_get_slave_from_addr(priv, addr);
  1656. if (!slave) {
  1657. ret = -EINVAL;
  1658. goto clear_csr;
  1659. }
  1660. /*
  1661. * Notify the slave backend that an smbus master wants to write data
  1662. * to the BlueField.
  1663. */
  1664. i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  1665. /* Send the received data to the slave backend. */
  1666. for (byte = 1; byte < recv_bytes; byte++) {
  1667. value = data_desc[byte];
  1668. ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
  1669. &value);
  1670. if (ret < 0)
  1671. break;
  1672. }
  1673. /*
  1674. * Send a stop event to the slave backend, to signal
  1675. * the end of the write transactions.
  1676. */
  1677. i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
  1678. clear_csr:
  1679. /* Release the Slave GW. */
  1680. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
  1681. writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
  1682. writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
  1683. return ret;
  1684. }
  1685. static irqreturn_t mlxbf_i2c_irq(int irq, void *ptr)
  1686. {
  1687. struct mlxbf_i2c_priv *priv = ptr;
  1688. bool read, write, irq_is_set;
  1689. u32 rw_bytes_reg;
  1690. u8 recv_bytes;
  1691. /*
  1692. * Read TYU interrupt register and determine the source of the
  1693. * interrupt. Based on the source of the interrupt one of the
  1694. * following actions are performed:
  1695. * - Receive data and send response to master.
  1696. * - Send data and release slave GW.
  1697. *
  1698. * Handle read/write transaction only. CRmaster and Iarp requests
  1699. * are ignored for now.
  1700. */
  1701. irq_is_set = mlxbf_i2c_has_coalesce(priv, &read, &write);
  1702. if (!irq_is_set || (!read && !write)) {
  1703. /* Nothing to do here, interrupt was not from this device. */
  1704. return IRQ_NONE;
  1705. }
  1706. /*
  1707. * The MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES includes the number of
  1708. * bytes from/to master. These are defined by 8-bits each. If the lower
  1709. * 8 bits are set, then the master expect to read N bytes from the
  1710. * slave, if the higher 8 bits are sent then the slave expect N bytes
  1711. * from the master.
  1712. */
  1713. rw_bytes_reg = readl(priv->slv->io +
  1714. MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
  1715. recv_bytes = (rw_bytes_reg >> 8) & GENMASK(7, 0);
  1716. /*
  1717. * For now, the slave supports 128 bytes transfer. Discard remaining
  1718. * data bytes if the master wrote more than
  1719. * MLXBF_I2C_SLAVE_DATA_DESC_SIZE, i.e, the actual size of the slave
  1720. * data descriptor.
  1721. *
  1722. * Note that we will never expect to transfer more than 128 bytes; as
  1723. * specified in the SMBus standard, block transactions cannot exceed
  1724. * 32 bytes.
  1725. */
  1726. recv_bytes = recv_bytes > MLXBF_I2C_SLAVE_DATA_DESC_SIZE ?
  1727. MLXBF_I2C_SLAVE_DATA_DESC_SIZE : recv_bytes;
  1728. if (read)
  1729. mlxbf_i2c_irq_send(priv, recv_bytes);
  1730. else
  1731. mlxbf_i2c_irq_recv(priv, recv_bytes);
  1732. return IRQ_HANDLED;
  1733. }
  1734. /* Return negative errno on error. */
  1735. static s32 mlxbf_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  1736. unsigned short flags, char read_write,
  1737. u8 command, int size,
  1738. union i2c_smbus_data *data)
  1739. {
  1740. struct mlxbf_i2c_smbus_request request = { 0 };
  1741. struct mlxbf_i2c_priv *priv;
  1742. bool read, pec;
  1743. u8 byte_cnt;
  1744. request.slave = addr;
  1745. read = (read_write == I2C_SMBUS_READ);
  1746. pec = flags & I2C_FUNC_SMBUS_PEC;
  1747. switch (size) {
  1748. case I2C_SMBUS_QUICK:
  1749. mlxbf_i2c_smbus_quick_command(&request, read);
  1750. dev_dbg(&adap->dev, "smbus quick, slave 0x%02x\n", addr);
  1751. break;
  1752. case I2C_SMBUS_BYTE:
  1753. mlxbf_i2c_smbus_byte_func(&request,
  1754. read ? &data->byte : &command, read,
  1755. pec);
  1756. dev_dbg(&adap->dev, "smbus %s byte, slave 0x%02x.\n",
  1757. read ? "read" : "write", addr);
  1758. break;
  1759. case I2C_SMBUS_BYTE_DATA:
  1760. mlxbf_i2c_smbus_data_byte_func(&request, &command, &data->byte,
  1761. read, pec);
  1762. dev_dbg(&adap->dev, "smbus %s byte data at 0x%02x, slave 0x%02x.\n",
  1763. read ? "read" : "write", command, addr);
  1764. break;
  1765. case I2C_SMBUS_WORD_DATA:
  1766. mlxbf_i2c_smbus_data_word_func(&request, &command,
  1767. (u8 *)&data->word, read, pec);
  1768. dev_dbg(&adap->dev, "smbus %s word data at 0x%02x, slave 0x%02x.\n",
  1769. read ? "read" : "write", command, addr);
  1770. break;
  1771. case I2C_SMBUS_I2C_BLOCK_DATA:
  1772. byte_cnt = data->block[0];
  1773. mlxbf_i2c_smbus_i2c_block_func(&request, &command, data->block,
  1774. &byte_cnt, read, pec);
  1775. dev_dbg(&adap->dev, "i2c %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
  1776. read ? "read" : "write", byte_cnt, command, addr);
  1777. break;
  1778. case I2C_SMBUS_BLOCK_DATA:
  1779. byte_cnt = read ? I2C_SMBUS_BLOCK_MAX : data->block[0];
  1780. mlxbf_i2c_smbus_block_func(&request, &command, data->block,
  1781. &byte_cnt, read, pec);
  1782. dev_dbg(&adap->dev, "smbus %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
  1783. read ? "read" : "write", byte_cnt, command, addr);
  1784. break;
  1785. case I2C_FUNC_SMBUS_PROC_CALL:
  1786. mlxbf_i2c_smbus_process_call_func(&request, &command,
  1787. (u8 *)&data->word, pec);
  1788. dev_dbg(&adap->dev, "process call, wr/rd at 0x%02x, slave 0x%02x.\n",
  1789. command, addr);
  1790. break;
  1791. case I2C_FUNC_SMBUS_BLOCK_PROC_CALL:
  1792. byte_cnt = data->block[0];
  1793. mlxbf_i2c_smbus_blk_process_call_func(&request, &command,
  1794. data->block, &byte_cnt,
  1795. pec);
  1796. dev_dbg(&adap->dev, "block process call, wr/rd %d bytes, slave 0x%02x.\n",
  1797. byte_cnt, addr);
  1798. break;
  1799. default:
  1800. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command %d\n",
  1801. size);
  1802. return -EOPNOTSUPP;
  1803. }
  1804. priv = i2c_get_adapdata(adap);
  1805. return mlxbf_i2c_smbus_start_transaction(priv, &request);
  1806. }
  1807. static int mlxbf_i2c_reg_slave(struct i2c_client *slave)
  1808. {
  1809. struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  1810. struct device *dev = &slave->dev;
  1811. int ret;
  1812. /*
  1813. * Do not support ten bit chip address and do not use Packet Error
  1814. * Checking (PEC).
  1815. */
  1816. if (slave->flags & (I2C_CLIENT_TEN | I2C_CLIENT_PEC)) {
  1817. dev_err(dev, "SMBus PEC and 10 bit address not supported\n");
  1818. return -EAFNOSUPPORT;
  1819. }
  1820. ret = mlxbf_i2c_slave_enable(priv, slave);
  1821. if (ret)
  1822. dev_err(dev, "Surpassed max number of registered slaves allowed\n");
  1823. return 0;
  1824. }
  1825. static int mlxbf_i2c_unreg_slave(struct i2c_client *slave)
  1826. {
  1827. struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  1828. struct device *dev = &slave->dev;
  1829. int ret;
  1830. /*
  1831. * Unregister slave by:
  1832. * 1) Disabling the slave address in hardware
  1833. * 2) Freeing priv->slave at the corresponding index
  1834. */
  1835. ret = mlxbf_i2c_slave_disable(priv, slave->addr);
  1836. if (ret)
  1837. dev_err(dev, "Unable to find slave 0x%x\n", slave->addr);
  1838. return ret;
  1839. }
  1840. static u32 mlxbf_i2c_functionality(struct i2c_adapter *adap)
  1841. {
  1842. return MLXBF_I2C_FUNC_ALL;
  1843. }
  1844. static struct mlxbf_i2c_chip_info mlxbf_i2c_chip[] = {
  1845. [MLXBF_I2C_CHIP_TYPE_1] = {
  1846. .type = MLXBF_I2C_CHIP_TYPE_1,
  1847. .shared_res = {
  1848. [0] = &mlxbf_i2c_coalesce_res[MLXBF_I2C_CHIP_TYPE_1],
  1849. [1] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_1],
  1850. [2] = &mlxbf_i2c_gpio_res[MLXBF_I2C_CHIP_TYPE_1]
  1851. },
  1852. .calculate_freq = mlxbf_i2c_calculate_freq_from_tyu,
  1853. .smbus_master_rs_bytes_off = MLXBF_I2C_YU_SMBUS_RS_BYTES,
  1854. .smbus_master_fsm_off = MLXBF_I2C_YU_SMBUS_MASTER_FSM
  1855. },
  1856. [MLXBF_I2C_CHIP_TYPE_2] = {
  1857. .type = MLXBF_I2C_CHIP_TYPE_2,
  1858. .shared_res = {
  1859. [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_2]
  1860. },
  1861. .calculate_freq = mlxbf_i2c_calculate_freq_from_yu,
  1862. .smbus_master_rs_bytes_off = MLXBF_I2C_YU_SMBUS_RS_BYTES,
  1863. .smbus_master_fsm_off = MLXBF_I2C_YU_SMBUS_MASTER_FSM
  1864. },
  1865. [MLXBF_I2C_CHIP_TYPE_3] = {
  1866. .type = MLXBF_I2C_CHIP_TYPE_3,
  1867. .shared_res = {
  1868. [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_3]
  1869. },
  1870. .calculate_freq = mlxbf_i2c_calculate_freq_from_yu,
  1871. .smbus_master_rs_bytes_off = MLXBF_I2C_RSH_YU_SMBUS_RS_BYTES,
  1872. .smbus_master_fsm_off = MLXBF_I2C_RSH_YU_SMBUS_MASTER_FSM
  1873. }
  1874. };
  1875. static const struct i2c_algorithm mlxbf_i2c_algo = {
  1876. .smbus_xfer = mlxbf_i2c_smbus_xfer,
  1877. .functionality = mlxbf_i2c_functionality,
  1878. .reg_slave = mlxbf_i2c_reg_slave,
  1879. .unreg_slave = mlxbf_i2c_unreg_slave,
  1880. };
  1881. static struct i2c_adapter_quirks mlxbf_i2c_quirks = {
  1882. .max_read_len = MLXBF_I2C_MASTER_DATA_R_LENGTH,
  1883. .max_write_len = MLXBF_I2C_MASTER_DATA_W_LENGTH,
  1884. };
  1885. static const struct acpi_device_id mlxbf_i2c_acpi_ids[] = {
  1886. { "MLNXBF03", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_1] },
  1887. { "MLNXBF23", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_2] },
  1888. { "MLNXBF31", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_3] },
  1889. {},
  1890. };
  1891. MODULE_DEVICE_TABLE(acpi, mlxbf_i2c_acpi_ids);
  1892. static int mlxbf_i2c_acpi_probe(struct device *dev, struct mlxbf_i2c_priv *priv)
  1893. {
  1894. const struct acpi_device_id *aid;
  1895. u64 bus_id;
  1896. int ret;
  1897. if (acpi_disabled)
  1898. return -ENOENT;
  1899. aid = acpi_match_device(mlxbf_i2c_acpi_ids, dev);
  1900. if (!aid)
  1901. return -ENODEV;
  1902. priv->chip = (struct mlxbf_i2c_chip_info *)aid->driver_data;
  1903. ret = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &bus_id);
  1904. if (ret) {
  1905. dev_err(dev, "Cannot retrieve UID\n");
  1906. return ret;
  1907. }
  1908. priv->bus = bus_id;
  1909. return 0;
  1910. }
  1911. static int mlxbf_i2c_probe(struct platform_device *pdev)
  1912. {
  1913. struct device *dev = &pdev->dev;
  1914. struct mlxbf_i2c_priv *priv;
  1915. struct i2c_adapter *adap;
  1916. u32 resource_version;
  1917. int irq, ret;
  1918. priv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_priv), GFP_KERNEL);
  1919. if (!priv)
  1920. return -ENOMEM;
  1921. ret = mlxbf_i2c_acpi_probe(dev, priv);
  1922. if (ret < 0)
  1923. return ret;
  1924. /* This property allows the driver to stay backward compatible with older
  1925. * ACPI tables.
  1926. * Starting BlueField-3 SoC, the "smbus" resource was broken down into 3
  1927. * separate resources "timer", "master" and "slave".
  1928. */
  1929. if (device_property_read_u32(dev, "resource_version", &resource_version))
  1930. resource_version = 0;
  1931. priv->resource_version = resource_version;
  1932. if (priv->chip->type < MLXBF_I2C_CHIP_TYPE_3 && resource_version == 0) {
  1933. priv->timer = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
  1934. if (!priv->timer)
  1935. return -ENOMEM;
  1936. priv->mst = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
  1937. if (!priv->mst)
  1938. return -ENOMEM;
  1939. priv->slv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
  1940. if (!priv->slv)
  1941. return -ENOMEM;
  1942. ret = mlxbf_i2c_init_resource(pdev, &priv->smbus,
  1943. MLXBF_I2C_SMBUS_RES);
  1944. if (ret < 0) {
  1945. dev_err(dev, "Cannot fetch smbus resource info");
  1946. return ret;
  1947. }
  1948. priv->timer->io = priv->smbus->io;
  1949. priv->mst->io = priv->smbus->io + MLXBF_I2C_MST_ADDR_OFFSET;
  1950. priv->slv->io = priv->smbus->io + MLXBF_I2C_SLV_ADDR_OFFSET;
  1951. } else {
  1952. ret = mlxbf_i2c_init_resource(pdev, &priv->timer,
  1953. MLXBF_I2C_SMBUS_TIMER_RES);
  1954. if (ret < 0) {
  1955. dev_err(dev, "Cannot fetch timer resource info");
  1956. return ret;
  1957. }
  1958. ret = mlxbf_i2c_init_resource(pdev, &priv->mst,
  1959. MLXBF_I2C_SMBUS_MST_RES);
  1960. if (ret < 0) {
  1961. dev_err(dev, "Cannot fetch master resource info");
  1962. return ret;
  1963. }
  1964. ret = mlxbf_i2c_init_resource(pdev, &priv->slv,
  1965. MLXBF_I2C_SMBUS_SLV_RES);
  1966. if (ret < 0) {
  1967. dev_err(dev, "Cannot fetch slave resource info");
  1968. return ret;
  1969. }
  1970. }
  1971. ret = mlxbf_i2c_init_resource(pdev, &priv->mst_cause,
  1972. MLXBF_I2C_MST_CAUSE_RES);
  1973. if (ret < 0) {
  1974. dev_err(dev, "Cannot fetch cause master resource info");
  1975. return ret;
  1976. }
  1977. ret = mlxbf_i2c_init_resource(pdev, &priv->slv_cause,
  1978. MLXBF_I2C_SLV_CAUSE_RES);
  1979. if (ret < 0) {
  1980. dev_err(dev, "Cannot fetch cause slave resource info");
  1981. return ret;
  1982. }
  1983. adap = &priv->adap;
  1984. adap->owner = THIS_MODULE;
  1985. adap->class = I2C_CLASS_HWMON;
  1986. adap->algo = &mlxbf_i2c_algo;
  1987. adap->quirks = &mlxbf_i2c_quirks;
  1988. adap->dev.parent = dev;
  1989. adap->dev.of_node = dev->of_node;
  1990. adap->nr = priv->bus;
  1991. snprintf(adap->name, sizeof(adap->name), "i2c%d", adap->nr);
  1992. i2c_set_adapdata(adap, priv);
  1993. /* Read Core PLL frequency. */
  1994. ret = mlxbf_i2c_calculate_corepll_freq(pdev, priv);
  1995. if (ret < 0) {
  1996. dev_err(dev, "cannot get core clock frequency\n");
  1997. /* Set to default value. */
  1998. priv->frequency = MLXBF_I2C_COREPLL_FREQ;
  1999. }
  2000. /*
  2001. * Initialize master.
  2002. * Note that a physical bus might be shared among Linux and firmware
  2003. * (e.g., ATF). Thus, the bus should be initialized and ready and
  2004. * bus initialization would be unnecessary. This requires additional
  2005. * knowledge about physical busses. But, since an extra initialization
  2006. * does not really hurt, then keep the code as is.
  2007. */
  2008. ret = mlxbf_i2c_init_master(pdev, priv);
  2009. if (ret < 0) {
  2010. dev_err(dev, "failed to initialize smbus master %d",
  2011. priv->bus);
  2012. return ret;
  2013. }
  2014. mlxbf_i2c_init_timings(pdev, priv);
  2015. mlxbf_i2c_init_slave(pdev, priv);
  2016. irq = platform_get_irq(pdev, 0);
  2017. if (irq < 0)
  2018. return irq;
  2019. ret = devm_request_irq(dev, irq, mlxbf_i2c_irq,
  2020. IRQF_SHARED | IRQF_PROBE_SHARED,
  2021. dev_name(dev), priv);
  2022. if (ret < 0) {
  2023. dev_err(dev, "Cannot get irq %d\n", irq);
  2024. return ret;
  2025. }
  2026. priv->irq = irq;
  2027. platform_set_drvdata(pdev, priv);
  2028. ret = i2c_add_numbered_adapter(adap);
  2029. if (ret < 0)
  2030. return ret;
  2031. mutex_lock(&mlxbf_i2c_bus_lock);
  2032. mlxbf_i2c_bus_count++;
  2033. mutex_unlock(&mlxbf_i2c_bus_lock);
  2034. return 0;
  2035. }
  2036. static int mlxbf_i2c_remove(struct platform_device *pdev)
  2037. {
  2038. struct mlxbf_i2c_priv *priv = platform_get_drvdata(pdev);
  2039. struct device *dev = &pdev->dev;
  2040. struct resource *params;
  2041. if (priv->chip->type < MLXBF_I2C_CHIP_TYPE_3 && priv->resource_version == 0) {
  2042. params = priv->smbus->params;
  2043. devm_release_mem_region(dev, params->start, resource_size(params));
  2044. } else {
  2045. params = priv->timer->params;
  2046. devm_release_mem_region(dev, params->start, resource_size(params));
  2047. params = priv->mst->params;
  2048. devm_release_mem_region(dev, params->start, resource_size(params));
  2049. params = priv->slv->params;
  2050. devm_release_mem_region(dev, params->start, resource_size(params));
  2051. }
  2052. params = priv->mst_cause->params;
  2053. devm_release_mem_region(dev, params->start, resource_size(params));
  2054. params = priv->slv_cause->params;
  2055. devm_release_mem_region(dev, params->start, resource_size(params));
  2056. /*
  2057. * Release shared resources. This should be done when releasing
  2058. * the I2C controller.
  2059. */
  2060. mutex_lock(&mlxbf_i2c_bus_lock);
  2061. if (--mlxbf_i2c_bus_count == 0) {
  2062. mlxbf_i2c_release_coalesce(pdev, priv);
  2063. mlxbf_i2c_release_corepll(pdev, priv);
  2064. mlxbf_i2c_release_gpio(pdev, priv);
  2065. }
  2066. mutex_unlock(&mlxbf_i2c_bus_lock);
  2067. devm_free_irq(dev, priv->irq, priv);
  2068. i2c_del_adapter(&priv->adap);
  2069. return 0;
  2070. }
  2071. static struct platform_driver mlxbf_i2c_driver = {
  2072. .probe = mlxbf_i2c_probe,
  2073. .remove = mlxbf_i2c_remove,
  2074. .driver = {
  2075. .name = "i2c-mlxbf",
  2076. .acpi_match_table = ACPI_PTR(mlxbf_i2c_acpi_ids),
  2077. },
  2078. };
  2079. static int __init mlxbf_i2c_init(void)
  2080. {
  2081. mutex_init(&mlxbf_i2c_coalesce_lock);
  2082. mutex_init(&mlxbf_i2c_corepll_lock);
  2083. mutex_init(&mlxbf_i2c_gpio_lock);
  2084. mutex_init(&mlxbf_i2c_bus_lock);
  2085. return platform_driver_register(&mlxbf_i2c_driver);
  2086. }
  2087. module_init(mlxbf_i2c_init);
  2088. static void __exit mlxbf_i2c_exit(void)
  2089. {
  2090. platform_driver_unregister(&mlxbf_i2c_driver);
  2091. mutex_destroy(&mlxbf_i2c_bus_lock);
  2092. mutex_destroy(&mlxbf_i2c_gpio_lock);
  2093. mutex_destroy(&mlxbf_i2c_corepll_lock);
  2094. mutex_destroy(&mlxbf_i2c_coalesce_lock);
  2095. }
  2096. module_exit(mlxbf_i2c_exit);
  2097. MODULE_DESCRIPTION("Mellanox BlueField I2C bus driver");
  2098. MODULE_AUTHOR("Khalil Blaiech <[email protected]>");
  2099. MODULE_AUTHOR("Asmaa Mnebhi <[email protected]>");
  2100. MODULE_LICENSE("GPL v2");