i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 = reg_size / mlxsw_i2c->block_size;
  377. if (reg_size % mlxsw_i2c->block_size)
  378. num++;
  379. if (mutex_lock_interruptible(&mlxsw_i2c->cmd.lock) < 0) {
  380. dev_err(&client->dev, "Could not acquire lock");
  381. return -EINVAL;
  382. }
  383. err = mlxsw_i2c_write(dev, reg_size, in_mbox, num, status);
  384. if (err)
  385. goto cmd_fail;
  386. /* No out mailbox is case of write transaction. */
  387. if (!out_mbox) {
  388. mutex_unlock(&mlxsw_i2c->cmd.lock);
  389. return 0;
  390. }
  391. } else {
  392. /* No input mailbox is case of initialization query command. */
  393. reg_size = MLXSW_I2C_MAX_DATA_SIZE;
  394. num = DIV_ROUND_UP(reg_size, mlxsw_i2c->block_size);
  395. if (mutex_lock_interruptible(&mlxsw_i2c->cmd.lock) < 0) {
  396. dev_err(&client->dev, "Could not acquire lock");
  397. return -EINVAL;
  398. }
  399. err = mlxsw_i2c_write_init_cmd(client, mlxsw_i2c, opcode,
  400. in_mod);
  401. if (err)
  402. goto cmd_fail;
  403. }
  404. /* Send read transaction to get output mailbox content. */
  405. read_tran[1].buf = out_mbox;
  406. for (i = 0; i < num; i++) {
  407. chunk_size = (reg_size > mlxsw_i2c->block_size) ?
  408. mlxsw_i2c->block_size : reg_size;
  409. read_tran[1].len = chunk_size;
  410. mlxsw_i2c_set_slave_addr(tran_buf, off);
  411. j = 0;
  412. end = jiffies + timeout;
  413. do {
  414. err = i2c_transfer(client->adapter, read_tran,
  415. ARRAY_SIZE(read_tran));
  416. if (err == ARRAY_SIZE(read_tran))
  417. break;
  418. cond_resched();
  419. } while ((time_before(jiffies, end)) ||
  420. (j++ < MLXSW_I2C_RETRY));
  421. if (err != ARRAY_SIZE(read_tran)) {
  422. if (!err)
  423. err = -EIO;
  424. goto cmd_fail;
  425. }
  426. off += chunk_size;
  427. reg_size -= chunk_size;
  428. read_tran[1].buf += chunk_size;
  429. }
  430. mutex_unlock(&mlxsw_i2c->cmd.lock);
  431. return 0;
  432. cmd_fail:
  433. mutex_unlock(&mlxsw_i2c->cmd.lock);
  434. return err;
  435. }
  436. static int mlxsw_i2c_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
  437. u32 in_mod, bool out_mbox_direct,
  438. char *in_mbox, size_t in_mbox_size,
  439. char *out_mbox, size_t out_mbox_size,
  440. u8 *status)
  441. {
  442. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  443. return mlxsw_i2c_cmd(mlxsw_i2c->dev, opcode, in_mod, in_mbox_size,
  444. in_mbox, out_mbox_size, out_mbox, status);
  445. }
  446. static bool mlxsw_i2c_skb_transmit_busy(void *bus_priv,
  447. const struct mlxsw_tx_info *tx_info)
  448. {
  449. return false;
  450. }
  451. static int mlxsw_i2c_skb_transmit(void *bus_priv, struct sk_buff *skb,
  452. const struct mlxsw_tx_info *tx_info)
  453. {
  454. return 0;
  455. }
  456. static int
  457. mlxsw_i2c_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
  458. const struct mlxsw_config_profile *profile,
  459. struct mlxsw_res *res)
  460. {
  461. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  462. char *mbox;
  463. int err;
  464. mlxsw_i2c->core = mlxsw_core;
  465. mbox = mlxsw_cmd_mbox_alloc();
  466. if (!mbox)
  467. return -ENOMEM;
  468. err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
  469. if (err)
  470. goto mbox_put;
  471. mlxsw_i2c->bus_info.fw_rev.major =
  472. mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
  473. mlxsw_i2c->bus_info.fw_rev.minor =
  474. mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
  475. mlxsw_i2c->bus_info.fw_rev.subminor =
  476. mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
  477. err = mlxsw_core_resources_query(mlxsw_core, mbox, res);
  478. mbox_put:
  479. mlxsw_cmd_mbox_free(mbox);
  480. return err;
  481. }
  482. static void mlxsw_i2c_fini(void *bus_priv)
  483. {
  484. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  485. mlxsw_i2c->core = NULL;
  486. }
  487. static void mlxsw_i2c_work_handler(struct work_struct *work)
  488. {
  489. struct mlxsw_i2c *mlxsw_i2c;
  490. mlxsw_i2c = container_of(work, struct mlxsw_i2c, irq_work);
  491. mlxsw_core_irq_event_handlers_call(mlxsw_i2c->core);
  492. }
  493. static irqreturn_t mlxsw_i2c_irq_handler(int irq, void *dev)
  494. {
  495. struct mlxsw_i2c *mlxsw_i2c = dev;
  496. mlxsw_core_schedule_work(&mlxsw_i2c->irq_work);
  497. /* Interrupt handler shares IRQ line with 'main' interrupt handler.
  498. * Return here IRQ_NONE, while main handler will return IRQ_HANDLED.
  499. */
  500. return IRQ_NONE;
  501. }
  502. static int mlxsw_i2c_irq_init(struct mlxsw_i2c *mlxsw_i2c, u8 addr)
  503. {
  504. int err;
  505. /* Initialize interrupt handler if system hotplug driver is reachable,
  506. * otherwise interrupt line is not enabled and interrupts will not be
  507. * raised to CPU. Also request_irq() call will be not valid.
  508. */
  509. if (!IS_REACHABLE(CONFIG_MLXREG_HOTPLUG))
  510. return 0;
  511. /* Set default interrupt line. */
  512. if (mlxsw_i2c->pdata && mlxsw_i2c->pdata->irq)
  513. mlxsw_i2c->irq = mlxsw_i2c->pdata->irq;
  514. else if (addr == MLXSW_FAST_I2C_SLAVE)
  515. mlxsw_i2c->irq = MLXSW_I2C_DEFAULT_IRQ;
  516. if (!mlxsw_i2c->irq)
  517. return 0;
  518. INIT_WORK(&mlxsw_i2c->irq_work, mlxsw_i2c_work_handler);
  519. err = request_irq(mlxsw_i2c->irq, mlxsw_i2c_irq_handler,
  520. IRQF_TRIGGER_FALLING | IRQF_SHARED, "mlxsw-i2c",
  521. mlxsw_i2c);
  522. if (err) {
  523. dev_err(mlxsw_i2c->bus_info.dev, "Failed to request irq: %d\n",
  524. err);
  525. return err;
  526. }
  527. return 0;
  528. }
  529. static void mlxsw_i2c_irq_fini(struct mlxsw_i2c *mlxsw_i2c)
  530. {
  531. if (!IS_REACHABLE(CONFIG_MLXREG_HOTPLUG) || !mlxsw_i2c->irq)
  532. return;
  533. cancel_work_sync(&mlxsw_i2c->irq_work);
  534. free_irq(mlxsw_i2c->irq, mlxsw_i2c);
  535. }
  536. static const struct mlxsw_bus mlxsw_i2c_bus = {
  537. .kind = "i2c",
  538. .init = mlxsw_i2c_init,
  539. .fini = mlxsw_i2c_fini,
  540. .skb_transmit_busy = mlxsw_i2c_skb_transmit_busy,
  541. .skb_transmit = mlxsw_i2c_skb_transmit,
  542. .cmd_exec = mlxsw_i2c_cmd_exec,
  543. };
  544. static int mlxsw_i2c_probe(struct i2c_client *client,
  545. const struct i2c_device_id *id)
  546. {
  547. const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
  548. struct mlxsw_i2c *mlxsw_i2c;
  549. u8 status;
  550. int err;
  551. mlxsw_i2c = devm_kzalloc(&client->dev, sizeof(*mlxsw_i2c), GFP_KERNEL);
  552. if (!mlxsw_i2c)
  553. return -ENOMEM;
  554. if (quirks) {
  555. if ((quirks->max_read_len &&
  556. quirks->max_read_len < MLXSW_I2C_BLK_DEF) ||
  557. (quirks->max_write_len &&
  558. quirks->max_write_len < MLXSW_I2C_BLK_DEF)) {
  559. dev_err(&client->dev, "Insufficient transaction buffer length\n");
  560. return -EOPNOTSUPP;
  561. }
  562. mlxsw_i2c->block_size = min_t(u16, MLXSW_I2C_BLK_MAX,
  563. min_t(u16, quirks->max_read_len,
  564. quirks->max_write_len));
  565. } else {
  566. mlxsw_i2c->block_size = MLXSW_I2C_BLK_DEF;
  567. }
  568. i2c_set_clientdata(client, mlxsw_i2c);
  569. mutex_init(&mlxsw_i2c->cmd.lock);
  570. /* In order to use mailboxes through the i2c, special area is reserved
  571. * on the i2c address space that can be used for input and output
  572. * mailboxes. Such mailboxes are called local mailboxes. When using a
  573. * local mailbox, software should specify 0 as the Input/Output
  574. * parameters. The location of the Local Mailbox addresses on the i2c
  575. * space can be retrieved through the QUERY_FW command.
  576. * For this purpose QUERY_FW is to be issued with opcode modifier equal
  577. * 0x01. For such command the output parameter is an immediate value.
  578. * Here QUERY_FW command is invoked for ASIC probing and for getting
  579. * local mailboxes addresses from immedate output parameters.
  580. */
  581. /* Prepare and write out Command Interface Register for transaction */
  582. err = mlxsw_i2c_write_cmd(client, mlxsw_i2c, 1);
  583. if (err) {
  584. dev_err(&client->dev, "Could not start transaction");
  585. goto errout;
  586. }
  587. /* Wait until go bit is cleared. */
  588. err = mlxsw_i2c_wait_go_bit(client, mlxsw_i2c, &status);
  589. if (err) {
  590. dev_err(&client->dev, "HW semaphore is not released");
  591. goto errout;
  592. }
  593. /* Validate transaction completion status. */
  594. if (status) {
  595. dev_err(&client->dev, "Bad transaction completion status %x\n",
  596. status);
  597. err = -EIO;
  598. goto errout;
  599. }
  600. /* Get mailbox offsets. */
  601. err = mlxsw_i2c_get_mbox(client, mlxsw_i2c);
  602. if (err < 0) {
  603. dev_err(&client->dev, "Fail to get mailboxes\n");
  604. goto errout;
  605. }
  606. dev_info(&client->dev, "%s mb size=%x off=0x%08x out mb size=%x off=0x%08x\n",
  607. id->name, mlxsw_i2c->cmd.mb_size_in,
  608. mlxsw_i2c->cmd.mb_off_in, mlxsw_i2c->cmd.mb_size_out,
  609. mlxsw_i2c->cmd.mb_off_out);
  610. /* Register device bus. */
  611. mlxsw_i2c->bus_info.device_kind = id->name;
  612. mlxsw_i2c->bus_info.device_name = client->name;
  613. mlxsw_i2c->bus_info.dev = &client->dev;
  614. mlxsw_i2c->bus_info.low_frequency = true;
  615. mlxsw_i2c->dev = &client->dev;
  616. mlxsw_i2c->pdata = client->dev.platform_data;
  617. err = mlxsw_i2c_irq_init(mlxsw_i2c, client->addr);
  618. if (err)
  619. goto errout;
  620. err = mlxsw_core_bus_device_register(&mlxsw_i2c->bus_info,
  621. &mlxsw_i2c_bus, mlxsw_i2c, false,
  622. NULL, NULL);
  623. if (err) {
  624. dev_err(&client->dev, "Fail to register core bus\n");
  625. goto err_bus_device_register;
  626. }
  627. return 0;
  628. err_bus_device_register:
  629. mlxsw_i2c_irq_fini(mlxsw_i2c);
  630. errout:
  631. mutex_destroy(&mlxsw_i2c->cmd.lock);
  632. i2c_set_clientdata(client, NULL);
  633. return err;
  634. }
  635. static void mlxsw_i2c_remove(struct i2c_client *client)
  636. {
  637. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  638. mlxsw_core_bus_device_unregister(mlxsw_i2c->core, false);
  639. mlxsw_i2c_irq_fini(mlxsw_i2c);
  640. mutex_destroy(&mlxsw_i2c->cmd.lock);
  641. }
  642. int mlxsw_i2c_driver_register(struct i2c_driver *i2c_driver)
  643. {
  644. i2c_driver->probe = mlxsw_i2c_probe;
  645. i2c_driver->remove = mlxsw_i2c_remove;
  646. return i2c_add_driver(i2c_driver);
  647. }
  648. EXPORT_SYMBOL(mlxsw_i2c_driver_register);
  649. void mlxsw_i2c_driver_unregister(struct i2c_driver *i2c_driver)
  650. {
  651. i2c_del_driver(i2c_driver);
  652. }
  653. EXPORT_SYMBOL(mlxsw_i2c_driver_unregister);
  654. MODULE_AUTHOR("Vadim Pasternak <[email protected]>");
  655. MODULE_DESCRIPTION("Mellanox switch I2C interface driver");
  656. MODULE_LICENSE("Dual BSD/GPL");