i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
  3. #include <linux/err.h>
  4. #include <linux/i2c.h>
  5. #include <linux/init.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/kernel.h>
  8. #include <linux/mutex.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_data/mlxreg.h>
  12. #include <linux/slab.h>
  13. #include "cmd.h"
  14. #include "core.h"
  15. #include "i2c.h"
  16. #include "resources.h"
  17. #define MLXSW_I2C_CIR2_BASE 0x72000
  18. #define MLXSW_I2C_CIR_STATUS_OFF 0x18
  19. #define MLXSW_I2C_CIR2_OFF_STATUS (MLXSW_I2C_CIR2_BASE + \
  20. MLXSW_I2C_CIR_STATUS_OFF)
  21. #define MLXSW_I2C_OPMOD_SHIFT 12
  22. #define MLXSW_I2C_EVENT_BIT_SHIFT 22
  23. #define MLXSW_I2C_GO_BIT_SHIFT 23
  24. #define MLXSW_I2C_CIR_CTRL_STATUS_SHIFT 24
  25. #define MLXSW_I2C_EVENT_BIT BIT(MLXSW_I2C_EVENT_BIT_SHIFT)
  26. #define MLXSW_I2C_GO_BIT BIT(MLXSW_I2C_GO_BIT_SHIFT)
  27. #define MLXSW_I2C_GO_OPMODE BIT(MLXSW_I2C_OPMOD_SHIFT)
  28. #define MLXSW_I2C_SET_IMM_CMD (MLXSW_I2C_GO_OPMODE | \
  29. MLXSW_CMD_OPCODE_QUERY_FW)
  30. #define MLXSW_I2C_PUSH_IMM_CMD (MLXSW_I2C_GO_BIT | \
  31. MLXSW_I2C_SET_IMM_CMD)
  32. #define MLXSW_I2C_SET_CMD (MLXSW_CMD_OPCODE_ACCESS_REG)
  33. #define MLXSW_I2C_PUSH_CMD (MLXSW_I2C_GO_BIT | MLXSW_I2C_SET_CMD)
  34. #define MLXSW_I2C_TLV_HDR_SIZE 0x10
  35. #define MLXSW_I2C_ADDR_WIDTH 4
  36. #define MLXSW_I2C_PUSH_CMD_SIZE (MLXSW_I2C_ADDR_WIDTH + 4)
  37. #define MLXSW_I2C_SET_EVENT_CMD (MLXSW_I2C_EVENT_BIT)
  38. #define MLXSW_I2C_PUSH_EVENT_CMD (MLXSW_I2C_GO_BIT | \
  39. MLXSW_I2C_SET_EVENT_CMD)
  40. #define MLXSW_I2C_READ_SEMA_SIZE 4
  41. #define MLXSW_I2C_PREP_SIZE (MLXSW_I2C_ADDR_WIDTH + 28)
  42. #define MLXSW_I2C_MBOX_SIZE 20
  43. #define MLXSW_I2C_MBOX_OUT_PARAM_OFF 12
  44. #define MLXSW_I2C_MBOX_OFFSET_BITS 20
  45. #define MLXSW_I2C_MBOX_SIZE_BITS 12
  46. #define MLXSW_I2C_ADDR_BUF_SIZE 4
  47. #define MLXSW_I2C_BLK_DEF 32
  48. #define MLXSW_I2C_BLK_MAX 100
  49. #define MLXSW_I2C_RETRY 5
  50. #define MLXSW_I2C_TIMEOUT_MSECS 5000
  51. #define MLXSW_I2C_MAX_DATA_SIZE 256
  52. /* Driver can be initialized by kernel platform driver or from the user
  53. * space. In the first case IRQ line number is passed through the platform
  54. * data, otherwise default IRQ line is to be used. Default IRQ is relevant
  55. * only for specific I2C slave address, allowing 3.4 MHz I2C path to the chip
  56. * (special hardware feature for I2C acceleration).
  57. */
  58. #define MLXSW_I2C_DEFAULT_IRQ 17
  59. #define MLXSW_FAST_I2C_SLAVE 0x37
  60. /**
  61. * struct mlxsw_i2c - device private data:
  62. * @cmd: command attributes;
  63. * @cmd.mb_size_in: input mailbox size;
  64. * @cmd.mb_off_in: input mailbox offset in register space;
  65. * @cmd.mb_size_out: output mailbox size;
  66. * @cmd.mb_off_out: output mailbox offset in register space;
  67. * @cmd.lock: command execution lock;
  68. * @dev: I2C device;
  69. * @core: switch core pointer;
  70. * @bus_info: bus info block;
  71. * @block_size: maximum block size allowed to pass to under layer;
  72. * @pdata: device platform data;
  73. * @irq_work: interrupts work item;
  74. * @irq: IRQ line number;
  75. */
  76. struct mlxsw_i2c {
  77. struct {
  78. u32 mb_size_in;
  79. u32 mb_off_in;
  80. u32 mb_size_out;
  81. u32 mb_off_out;
  82. struct mutex lock;
  83. } cmd;
  84. struct device *dev;
  85. struct mlxsw_core *core;
  86. struct mlxsw_bus_info bus_info;
  87. u16 block_size;
  88. struct mlxreg_core_hotplug_platform_data *pdata;
  89. struct work_struct irq_work;
  90. int irq;
  91. };
  92. #define MLXSW_I2C_READ_MSG(_client, _addr_buf, _buf, _len) { \
  93. { .addr = (_client)->addr, \
  94. .buf = (_addr_buf), \
  95. .len = MLXSW_I2C_ADDR_BUF_SIZE, \
  96. .flags = 0 }, \
  97. { .addr = (_client)->addr, \
  98. .buf = (_buf), \
  99. .len = (_len), \
  100. .flags = I2C_M_RD } }
  101. #define MLXSW_I2C_WRITE_MSG(_client, _buf, _len) \
  102. { .addr = (_client)->addr, \
  103. .buf = (u8 *)(_buf), \
  104. .len = (_len), \
  105. .flags = 0 }
  106. /* Routine converts in and out mail boxes offset and size. */
  107. static inline void
  108. mlxsw_i2c_convert_mbox(struct mlxsw_i2c *mlxsw_i2c, u8 *buf)
  109. {
  110. u32 tmp;
  111. /* Local in/out mailboxes: 20 bits for offset, 12 for size */
  112. tmp = be32_to_cpup((__be32 *) buf);
  113. mlxsw_i2c->cmd.mb_off_in = tmp &
  114. GENMASK(MLXSW_I2C_MBOX_OFFSET_BITS - 1, 0);
  115. mlxsw_i2c->cmd.mb_size_in = (tmp & GENMASK(31,
  116. MLXSW_I2C_MBOX_OFFSET_BITS)) >>
  117. MLXSW_I2C_MBOX_OFFSET_BITS;
  118. tmp = be32_to_cpup((__be32 *) (buf + MLXSW_I2C_ADDR_WIDTH));
  119. mlxsw_i2c->cmd.mb_off_out = tmp &
  120. GENMASK(MLXSW_I2C_MBOX_OFFSET_BITS - 1, 0);
  121. mlxsw_i2c->cmd.mb_size_out = (tmp & GENMASK(31,
  122. MLXSW_I2C_MBOX_OFFSET_BITS)) >>
  123. MLXSW_I2C_MBOX_OFFSET_BITS;
  124. }
  125. /* Routine obtains register size from mail box buffer. */
  126. static inline int mlxsw_i2c_get_reg_size(u8 *in_mbox)
  127. {
  128. u16 tmp = be16_to_cpup((__be16 *) (in_mbox + MLXSW_I2C_TLV_HDR_SIZE));
  129. return (tmp & 0x7ff) * 4 + MLXSW_I2C_TLV_HDR_SIZE;
  130. }
  131. /* Routine sets I2C device internal offset in the transaction buffer. */
  132. static inline void mlxsw_i2c_set_slave_addr(u8 *buf, u32 off)
  133. {
  134. __be32 *val = (__be32 *) buf;
  135. *val = htonl(off);
  136. }
  137. /* Routine waits until go bit is cleared. */
  138. static int mlxsw_i2c_wait_go_bit(struct i2c_client *client,
  139. struct mlxsw_i2c *mlxsw_i2c, u8 *p_status)
  140. {
  141. u8 addr_buf[MLXSW_I2C_ADDR_BUF_SIZE];
  142. u8 buf[MLXSW_I2C_READ_SEMA_SIZE];
  143. int len = MLXSW_I2C_READ_SEMA_SIZE;
  144. struct i2c_msg read_sema[] =
  145. MLXSW_I2C_READ_MSG(client, addr_buf, buf, len);
  146. bool wait_done = false;
  147. unsigned long end;
  148. int i = 0, err;
  149. mlxsw_i2c_set_slave_addr(addr_buf, MLXSW_I2C_CIR2_OFF_STATUS);
  150. end = jiffies + msecs_to_jiffies(MLXSW_I2C_TIMEOUT_MSECS);
  151. do {
  152. u32 ctrl;
  153. err = i2c_transfer(client->adapter, read_sema,
  154. ARRAY_SIZE(read_sema));
  155. ctrl = be32_to_cpu(*(__be32 *) buf);
  156. if (err == ARRAY_SIZE(read_sema)) {
  157. if (!(ctrl & MLXSW_I2C_GO_BIT)) {
  158. wait_done = true;
  159. *p_status = ctrl >>
  160. MLXSW_I2C_CIR_CTRL_STATUS_SHIFT;
  161. break;
  162. }
  163. }
  164. cond_resched();
  165. } while ((time_before(jiffies, end)) || (i++ < MLXSW_I2C_RETRY));
  166. if (wait_done) {
  167. if (*p_status)
  168. err = -EIO;
  169. } else {
  170. return -ETIMEDOUT;
  171. }
  172. return err > 0 ? 0 : err;
  173. }
  174. /* Routine posts a command to ASIC through mail box. */
  175. static int mlxsw_i2c_write_cmd(struct i2c_client *client,
  176. struct mlxsw_i2c *mlxsw_i2c,
  177. int immediate)
  178. {
  179. __be32 push_cmd_buf[MLXSW_I2C_PUSH_CMD_SIZE / 4] = {
  180. 0, cpu_to_be32(MLXSW_I2C_PUSH_IMM_CMD)
  181. };
  182. __be32 prep_cmd_buf[MLXSW_I2C_PREP_SIZE / 4] = {
  183. 0, 0, 0, 0, 0, 0,
  184. cpu_to_be32(client->adapter->nr & 0xffff),
  185. cpu_to_be32(MLXSW_I2C_SET_IMM_CMD)
  186. };
  187. struct i2c_msg push_cmd =
  188. MLXSW_I2C_WRITE_MSG(client, push_cmd_buf,
  189. MLXSW_I2C_PUSH_CMD_SIZE);
  190. struct i2c_msg prep_cmd =
  191. MLXSW_I2C_WRITE_MSG(client, prep_cmd_buf, MLXSW_I2C_PREP_SIZE);
  192. int err;
  193. if (!immediate) {
  194. push_cmd_buf[1] = cpu_to_be32(MLXSW_I2C_PUSH_CMD);
  195. prep_cmd_buf[7] = cpu_to_be32(MLXSW_I2C_SET_CMD);
  196. }
  197. mlxsw_i2c_set_slave_addr((u8 *)prep_cmd_buf,
  198. MLXSW_I2C_CIR2_BASE);
  199. mlxsw_i2c_set_slave_addr((u8 *)push_cmd_buf,
  200. MLXSW_I2C_CIR2_OFF_STATUS);
  201. /* Prepare Command Interface Register for transaction */
  202. err = i2c_transfer(client->adapter, &prep_cmd, 1);
  203. if (err < 0)
  204. return err;
  205. else if (err != 1)
  206. return -EIO;
  207. /* Write out Command Interface Register GO bit to push transaction */
  208. err = i2c_transfer(client->adapter, &push_cmd, 1);
  209. if (err < 0)
  210. return err;
  211. else if (err != 1)
  212. return -EIO;
  213. return 0;
  214. }
  215. /* Routine posts initialization command to ASIC through mail box. */
  216. static int
  217. mlxsw_i2c_write_init_cmd(struct i2c_client *client,
  218. struct mlxsw_i2c *mlxsw_i2c, u16 opcode, u32 in_mod)
  219. {
  220. __be32 push_cmd_buf[MLXSW_I2C_PUSH_CMD_SIZE / 4] = {
  221. 0, cpu_to_be32(MLXSW_I2C_PUSH_EVENT_CMD)
  222. };
  223. __be32 prep_cmd_buf[MLXSW_I2C_PREP_SIZE / 4] = {
  224. 0, 0, 0, 0, 0, 0,
  225. cpu_to_be32(client->adapter->nr & 0xffff),
  226. cpu_to_be32(MLXSW_I2C_SET_EVENT_CMD)
  227. };
  228. struct i2c_msg push_cmd =
  229. MLXSW_I2C_WRITE_MSG(client, push_cmd_buf,
  230. MLXSW_I2C_PUSH_CMD_SIZE);
  231. struct i2c_msg prep_cmd =
  232. MLXSW_I2C_WRITE_MSG(client, prep_cmd_buf, MLXSW_I2C_PREP_SIZE);
  233. u8 status;
  234. int err;
  235. push_cmd_buf[1] = cpu_to_be32(MLXSW_I2C_PUSH_EVENT_CMD | opcode);
  236. prep_cmd_buf[3] = cpu_to_be32(in_mod);
  237. prep_cmd_buf[7] = cpu_to_be32(MLXSW_I2C_GO_BIT | opcode);
  238. mlxsw_i2c_set_slave_addr((u8 *)prep_cmd_buf,
  239. MLXSW_I2C_CIR2_BASE);
  240. mlxsw_i2c_set_slave_addr((u8 *)push_cmd_buf,
  241. MLXSW_I2C_CIR2_OFF_STATUS);
  242. /* Prepare Command Interface Register for transaction */
  243. err = i2c_transfer(client->adapter, &prep_cmd, 1);
  244. if (err < 0)
  245. return err;
  246. else if (err != 1)
  247. return -EIO;
  248. /* Write out Command Interface Register GO bit to push transaction */
  249. err = i2c_transfer(client->adapter, &push_cmd, 1);
  250. if (err < 0)
  251. return err;
  252. else if (err != 1)
  253. return -EIO;
  254. /* Wait until go bit is cleared. */
  255. err = mlxsw_i2c_wait_go_bit(client, mlxsw_i2c, &status);
  256. if (err) {
  257. dev_err(&client->dev, "HW semaphore is not released");
  258. return err;
  259. }
  260. /* Validate transaction completion status. */
  261. if (status) {
  262. dev_err(&client->dev, "Bad transaction completion status %x\n",
  263. status);
  264. return -EIO;
  265. }
  266. return 0;
  267. }
  268. /* Routine obtains mail box offsets from ASIC register space. */
  269. static int mlxsw_i2c_get_mbox(struct i2c_client *client,
  270. struct mlxsw_i2c *mlxsw_i2c)
  271. {
  272. u8 addr_buf[MLXSW_I2C_ADDR_BUF_SIZE];
  273. u8 buf[MLXSW_I2C_MBOX_SIZE];
  274. struct i2c_msg mbox_cmd[] =
  275. MLXSW_I2C_READ_MSG(client, addr_buf, buf, MLXSW_I2C_MBOX_SIZE);
  276. int err;
  277. /* Read mail boxes offsets. */
  278. mlxsw_i2c_set_slave_addr(addr_buf, MLXSW_I2C_CIR2_BASE);
  279. err = i2c_transfer(client->adapter, mbox_cmd, 2);
  280. if (err != 2) {
  281. dev_err(&client->dev, "Could not obtain mail boxes\n");
  282. if (!err)
  283. return -EIO;
  284. else
  285. return err;
  286. }
  287. /* Convert mail boxes. */
  288. mlxsw_i2c_convert_mbox(mlxsw_i2c, &buf[MLXSW_I2C_MBOX_OUT_PARAM_OFF]);
  289. return err;
  290. }
  291. /* Routine sends I2C write transaction to ASIC device. */
  292. static int
  293. mlxsw_i2c_write(struct device *dev, size_t in_mbox_size, u8 *in_mbox, int num,
  294. u8 *p_status)
  295. {
  296. struct i2c_client *client = to_i2c_client(dev);
  297. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  298. unsigned long timeout = msecs_to_jiffies(MLXSW_I2C_TIMEOUT_MSECS);
  299. int off = mlxsw_i2c->cmd.mb_off_in, chunk_size, i, j;
  300. unsigned long end;
  301. u8 *tran_buf;
  302. struct i2c_msg write_tran =
  303. MLXSW_I2C_WRITE_MSG(client, NULL, MLXSW_I2C_PUSH_CMD_SIZE);
  304. int err;
  305. tran_buf = kmalloc(mlxsw_i2c->block_size + MLXSW_I2C_ADDR_BUF_SIZE,
  306. GFP_KERNEL);
  307. if (!tran_buf)
  308. return -ENOMEM;
  309. write_tran.buf = tran_buf;
  310. for (i = 0; i < num; i++) {
  311. chunk_size = (in_mbox_size > mlxsw_i2c->block_size) ?
  312. mlxsw_i2c->block_size : in_mbox_size;
  313. write_tran.len = MLXSW_I2C_ADDR_WIDTH + chunk_size;
  314. mlxsw_i2c_set_slave_addr(tran_buf, off);
  315. memcpy(&tran_buf[MLXSW_I2C_ADDR_BUF_SIZE], in_mbox +
  316. mlxsw_i2c->block_size * i, chunk_size);
  317. j = 0;
  318. end = jiffies + timeout;
  319. do {
  320. err = i2c_transfer(client->adapter, &write_tran, 1);
  321. if (err == 1)
  322. break;
  323. cond_resched();
  324. } while ((time_before(jiffies, end)) ||
  325. (j++ < MLXSW_I2C_RETRY));
  326. if (err != 1) {
  327. if (!err) {
  328. err = -EIO;
  329. goto mlxsw_i2c_write_exit;
  330. }
  331. }
  332. off += chunk_size;
  333. in_mbox_size -= chunk_size;
  334. }
  335. /* Prepare and write out Command Interface Register for transaction. */
  336. err = mlxsw_i2c_write_cmd(client, mlxsw_i2c, 0);
  337. if (err) {
  338. dev_err(&client->dev, "Could not start transaction");
  339. err = -EIO;
  340. goto mlxsw_i2c_write_exit;
  341. }
  342. /* Wait until go bit is cleared. */
  343. err = mlxsw_i2c_wait_go_bit(client, mlxsw_i2c, p_status);
  344. if (err) {
  345. dev_err(&client->dev, "HW semaphore is not released");
  346. goto mlxsw_i2c_write_exit;
  347. }
  348. /* Validate transaction completion status. */
  349. if (*p_status) {
  350. dev_err(&client->dev, "Bad transaction completion status %x\n",
  351. *p_status);
  352. err = -EIO;
  353. }
  354. mlxsw_i2c_write_exit:
  355. kfree(tran_buf);
  356. return err;
  357. }
  358. /* Routine executes I2C command. */
  359. static int
  360. mlxsw_i2c_cmd(struct device *dev, u16 opcode, u32 in_mod, size_t in_mbox_size,
  361. u8 *in_mbox, size_t out_mbox_size, u8 *out_mbox, u8 *status)
  362. {
  363. struct i2c_client *client = to_i2c_client(dev);
  364. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  365. unsigned long timeout = msecs_to_jiffies(MLXSW_I2C_TIMEOUT_MSECS);
  366. u8 tran_buf[MLXSW_I2C_ADDR_BUF_SIZE];
  367. int num, chunk_size, reg_size, i, j;
  368. int off = mlxsw_i2c->cmd.mb_off_out;
  369. unsigned long end;
  370. struct i2c_msg read_tran[] =
  371. MLXSW_I2C_READ_MSG(client, tran_buf, NULL, 0);
  372. int err;
  373. WARN_ON(in_mbox_size % sizeof(u32) || out_mbox_size % sizeof(u32));
  374. if (in_mbox) {
  375. reg_size = mlxsw_i2c_get_reg_size(in_mbox);
  376. num = DIV_ROUND_UP(reg_size, mlxsw_i2c->block_size);
  377. if (mutex_lock_interruptible(&mlxsw_i2c->cmd.lock) < 0) {
  378. dev_err(&client->dev, "Could not acquire lock");
  379. return -EINVAL;
  380. }
  381. err = mlxsw_i2c_write(dev, reg_size, in_mbox, num, status);
  382. if (err)
  383. goto cmd_fail;
  384. /* No out mailbox is case of write transaction. */
  385. if (!out_mbox) {
  386. mutex_unlock(&mlxsw_i2c->cmd.lock);
  387. return 0;
  388. }
  389. } else {
  390. /* No input mailbox is case of initialization query command. */
  391. reg_size = MLXSW_I2C_MAX_DATA_SIZE;
  392. num = DIV_ROUND_UP(reg_size, mlxsw_i2c->block_size);
  393. if (mutex_lock_interruptible(&mlxsw_i2c->cmd.lock) < 0) {
  394. dev_err(&client->dev, "Could not acquire lock");
  395. return -EINVAL;
  396. }
  397. err = mlxsw_i2c_write_init_cmd(client, mlxsw_i2c, opcode,
  398. in_mod);
  399. if (err)
  400. goto cmd_fail;
  401. }
  402. /* Send read transaction to get output mailbox content. */
  403. read_tran[1].buf = out_mbox;
  404. for (i = 0; i < num; i++) {
  405. chunk_size = (reg_size > mlxsw_i2c->block_size) ?
  406. mlxsw_i2c->block_size : reg_size;
  407. read_tran[1].len = chunk_size;
  408. mlxsw_i2c_set_slave_addr(tran_buf, off);
  409. j = 0;
  410. end = jiffies + timeout;
  411. do {
  412. err = i2c_transfer(client->adapter, read_tran,
  413. ARRAY_SIZE(read_tran));
  414. if (err == ARRAY_SIZE(read_tran))
  415. break;
  416. cond_resched();
  417. } while ((time_before(jiffies, end)) ||
  418. (j++ < MLXSW_I2C_RETRY));
  419. if (err != ARRAY_SIZE(read_tran)) {
  420. if (!err)
  421. err = -EIO;
  422. goto cmd_fail;
  423. }
  424. off += chunk_size;
  425. reg_size -= chunk_size;
  426. read_tran[1].buf += chunk_size;
  427. }
  428. mutex_unlock(&mlxsw_i2c->cmd.lock);
  429. return 0;
  430. cmd_fail:
  431. mutex_unlock(&mlxsw_i2c->cmd.lock);
  432. return err;
  433. }
  434. static int mlxsw_i2c_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
  435. u32 in_mod, bool out_mbox_direct,
  436. char *in_mbox, size_t in_mbox_size,
  437. char *out_mbox, size_t out_mbox_size,
  438. u8 *status)
  439. {
  440. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  441. return mlxsw_i2c_cmd(mlxsw_i2c->dev, opcode, in_mod, in_mbox_size,
  442. in_mbox, out_mbox_size, out_mbox, status);
  443. }
  444. static bool mlxsw_i2c_skb_transmit_busy(void *bus_priv,
  445. const struct mlxsw_tx_info *tx_info)
  446. {
  447. return false;
  448. }
  449. static int mlxsw_i2c_skb_transmit(void *bus_priv, struct sk_buff *skb,
  450. const struct mlxsw_tx_info *tx_info)
  451. {
  452. return 0;
  453. }
  454. static int
  455. mlxsw_i2c_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
  456. const struct mlxsw_config_profile *profile,
  457. struct mlxsw_res *res)
  458. {
  459. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  460. char *mbox;
  461. int err;
  462. mlxsw_i2c->core = mlxsw_core;
  463. mbox = mlxsw_cmd_mbox_alloc();
  464. if (!mbox)
  465. return -ENOMEM;
  466. err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
  467. if (err)
  468. goto mbox_put;
  469. mlxsw_i2c->bus_info.fw_rev.major =
  470. mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
  471. mlxsw_i2c->bus_info.fw_rev.minor =
  472. mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
  473. mlxsw_i2c->bus_info.fw_rev.subminor =
  474. mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
  475. err = mlxsw_core_resources_query(mlxsw_core, mbox, res);
  476. mbox_put:
  477. mlxsw_cmd_mbox_free(mbox);
  478. return err;
  479. }
  480. static void mlxsw_i2c_fini(void *bus_priv)
  481. {
  482. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  483. mlxsw_i2c->core = NULL;
  484. }
  485. static void mlxsw_i2c_work_handler(struct work_struct *work)
  486. {
  487. struct mlxsw_i2c *mlxsw_i2c;
  488. mlxsw_i2c = container_of(work, struct mlxsw_i2c, irq_work);
  489. mlxsw_core_irq_event_handlers_call(mlxsw_i2c->core);
  490. }
  491. static irqreturn_t mlxsw_i2c_irq_handler(int irq, void *dev)
  492. {
  493. struct mlxsw_i2c *mlxsw_i2c = dev;
  494. mlxsw_core_schedule_work(&mlxsw_i2c->irq_work);
  495. /* Interrupt handler shares IRQ line with 'main' interrupt handler.
  496. * Return here IRQ_NONE, while main handler will return IRQ_HANDLED.
  497. */
  498. return IRQ_NONE;
  499. }
  500. static int mlxsw_i2c_irq_init(struct mlxsw_i2c *mlxsw_i2c, u8 addr)
  501. {
  502. int err;
  503. /* Initialize interrupt handler if system hotplug driver is reachable,
  504. * otherwise interrupt line is not enabled and interrupts will not be
  505. * raised to CPU. Also request_irq() call will be not valid.
  506. */
  507. if (!IS_REACHABLE(CONFIG_MLXREG_HOTPLUG))
  508. return 0;
  509. /* Set default interrupt line. */
  510. if (mlxsw_i2c->pdata && mlxsw_i2c->pdata->irq)
  511. mlxsw_i2c->irq = mlxsw_i2c->pdata->irq;
  512. else if (addr == MLXSW_FAST_I2C_SLAVE)
  513. mlxsw_i2c->irq = MLXSW_I2C_DEFAULT_IRQ;
  514. if (!mlxsw_i2c->irq)
  515. return 0;
  516. INIT_WORK(&mlxsw_i2c->irq_work, mlxsw_i2c_work_handler);
  517. err = request_irq(mlxsw_i2c->irq, mlxsw_i2c_irq_handler,
  518. IRQF_TRIGGER_FALLING | IRQF_SHARED, "mlxsw-i2c",
  519. mlxsw_i2c);
  520. if (err) {
  521. dev_err(mlxsw_i2c->bus_info.dev, "Failed to request irq: %d\n",
  522. err);
  523. return err;
  524. }
  525. return 0;
  526. }
  527. static void mlxsw_i2c_irq_fini(struct mlxsw_i2c *mlxsw_i2c)
  528. {
  529. if (!IS_REACHABLE(CONFIG_MLXREG_HOTPLUG) || !mlxsw_i2c->irq)
  530. return;
  531. cancel_work_sync(&mlxsw_i2c->irq_work);
  532. free_irq(mlxsw_i2c->irq, mlxsw_i2c);
  533. }
  534. static const struct mlxsw_bus mlxsw_i2c_bus = {
  535. .kind = "i2c",
  536. .init = mlxsw_i2c_init,
  537. .fini = mlxsw_i2c_fini,
  538. .skb_transmit_busy = mlxsw_i2c_skb_transmit_busy,
  539. .skb_transmit = mlxsw_i2c_skb_transmit,
  540. .cmd_exec = mlxsw_i2c_cmd_exec,
  541. };
  542. static int mlxsw_i2c_probe(struct i2c_client *client)
  543. {
  544. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  545. const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
  546. struct mlxsw_i2c *mlxsw_i2c;
  547. u8 status;
  548. int err;
  549. mlxsw_i2c = devm_kzalloc(&client->dev, sizeof(*mlxsw_i2c), GFP_KERNEL);
  550. if (!mlxsw_i2c)
  551. return -ENOMEM;
  552. if (quirks) {
  553. if ((quirks->max_read_len &&
  554. quirks->max_read_len < MLXSW_I2C_BLK_DEF) ||
  555. (quirks->max_write_len &&
  556. quirks->max_write_len < MLXSW_I2C_BLK_DEF)) {
  557. dev_err(&client->dev, "Insufficient transaction buffer length\n");
  558. return -EOPNOTSUPP;
  559. }
  560. mlxsw_i2c->block_size = min_t(u16, MLXSW_I2C_BLK_MAX,
  561. min_t(u16, quirks->max_read_len,
  562. quirks->max_write_len));
  563. } else {
  564. mlxsw_i2c->block_size = MLXSW_I2C_BLK_DEF;
  565. }
  566. i2c_set_clientdata(client, mlxsw_i2c);
  567. mutex_init(&mlxsw_i2c->cmd.lock);
  568. /* In order to use mailboxes through the i2c, special area is reserved
  569. * on the i2c address space that can be used for input and output
  570. * mailboxes. Such mailboxes are called local mailboxes. When using a
  571. * local mailbox, software should specify 0 as the Input/Output
  572. * parameters. The location of the Local Mailbox addresses on the i2c
  573. * space can be retrieved through the QUERY_FW command.
  574. * For this purpose QUERY_FW is to be issued with opcode modifier equal
  575. * 0x01. For such command the output parameter is an immediate value.
  576. * Here QUERY_FW command is invoked for ASIC probing and for getting
  577. * local mailboxes addresses from immedate output parameters.
  578. */
  579. /* Prepare and write out Command Interface Register for transaction */
  580. err = mlxsw_i2c_write_cmd(client, mlxsw_i2c, 1);
  581. if (err) {
  582. dev_err(&client->dev, "Could not start transaction");
  583. goto errout;
  584. }
  585. /* Wait until go bit is cleared. */
  586. err = mlxsw_i2c_wait_go_bit(client, mlxsw_i2c, &status);
  587. if (err) {
  588. dev_err(&client->dev, "HW semaphore is not released");
  589. goto errout;
  590. }
  591. /* Validate transaction completion status. */
  592. if (status) {
  593. dev_err(&client->dev, "Bad transaction completion status %x\n",
  594. status);
  595. err = -EIO;
  596. goto errout;
  597. }
  598. /* Get mailbox offsets. */
  599. err = mlxsw_i2c_get_mbox(client, mlxsw_i2c);
  600. if (err < 0) {
  601. dev_err(&client->dev, "Fail to get mailboxes\n");
  602. goto errout;
  603. }
  604. dev_info(&client->dev, "%s mb size=%x off=0x%08x out mb size=%x off=0x%08x\n",
  605. id->name, mlxsw_i2c->cmd.mb_size_in,
  606. mlxsw_i2c->cmd.mb_off_in, mlxsw_i2c->cmd.mb_size_out,
  607. mlxsw_i2c->cmd.mb_off_out);
  608. /* Register device bus. */
  609. mlxsw_i2c->bus_info.device_kind = id->name;
  610. mlxsw_i2c->bus_info.device_name = client->name;
  611. mlxsw_i2c->bus_info.dev = &client->dev;
  612. mlxsw_i2c->bus_info.low_frequency = true;
  613. mlxsw_i2c->dev = &client->dev;
  614. mlxsw_i2c->pdata = client->dev.platform_data;
  615. err = mlxsw_i2c_irq_init(mlxsw_i2c, client->addr);
  616. if (err)
  617. goto errout;
  618. err = mlxsw_core_bus_device_register(&mlxsw_i2c->bus_info,
  619. &mlxsw_i2c_bus, mlxsw_i2c, false,
  620. NULL, NULL);
  621. if (err) {
  622. dev_err(&client->dev, "Fail to register core bus\n");
  623. goto err_bus_device_register;
  624. }
  625. return 0;
  626. err_bus_device_register:
  627. mlxsw_i2c_irq_fini(mlxsw_i2c);
  628. errout:
  629. mutex_destroy(&mlxsw_i2c->cmd.lock);
  630. i2c_set_clientdata(client, NULL);
  631. return err;
  632. }
  633. static void mlxsw_i2c_remove(struct i2c_client *client)
  634. {
  635. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  636. mlxsw_core_bus_device_unregister(mlxsw_i2c->core, false);
  637. mlxsw_i2c_irq_fini(mlxsw_i2c);
  638. mutex_destroy(&mlxsw_i2c->cmd.lock);
  639. }
  640. int mlxsw_i2c_driver_register(struct i2c_driver *i2c_driver)
  641. {
  642. i2c_driver->probe = mlxsw_i2c_probe;
  643. i2c_driver->remove = mlxsw_i2c_remove;
  644. return i2c_add_driver(i2c_driver);
  645. }
  646. EXPORT_SYMBOL(mlxsw_i2c_driver_register);
  647. void mlxsw_i2c_driver_unregister(struct i2c_driver *i2c_driver)
  648. {
  649. i2c_del_driver(i2c_driver);
  650. }
  651. EXPORT_SYMBOL(mlxsw_i2c_driver_unregister);
  652. MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
  653. MODULE_DESCRIPTION("Mellanox switch I2C interface driver");
  654. MODULE_LICENSE("Dual BSD/GPL");