xgbe-i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2016 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2016 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/module.h>
  117. #include <linux/interrupt.h>
  118. #include <linux/kmod.h>
  119. #include <linux/delay.h>
  120. #include <linux/completion.h>
  121. #include <linux/mutex.h>
  122. #include "xgbe.h"
  123. #include "xgbe-common.h"
  124. #define XGBE_ABORT_COUNT 500
  125. #define XGBE_DISABLE_COUNT 1000
  126. #define XGBE_STD_SPEED 1
  127. #define XGBE_INTR_RX_FULL BIT(IC_RAW_INTR_STAT_RX_FULL_INDEX)
  128. #define XGBE_INTR_TX_EMPTY BIT(IC_RAW_INTR_STAT_TX_EMPTY_INDEX)
  129. #define XGBE_INTR_TX_ABRT BIT(IC_RAW_INTR_STAT_TX_ABRT_INDEX)
  130. #define XGBE_INTR_STOP_DET BIT(IC_RAW_INTR_STAT_STOP_DET_INDEX)
  131. #define XGBE_DEFAULT_INT_MASK (XGBE_INTR_RX_FULL | \
  132. XGBE_INTR_TX_EMPTY | \
  133. XGBE_INTR_TX_ABRT | \
  134. XGBE_INTR_STOP_DET)
  135. #define XGBE_I2C_READ BIT(8)
  136. #define XGBE_I2C_STOP BIT(9)
  137. static int xgbe_i2c_abort(struct xgbe_prv_data *pdata)
  138. {
  139. unsigned int wait = XGBE_ABORT_COUNT;
  140. /* Must be enabled to recognize the abort request */
  141. XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, 1);
  142. /* Issue the abort */
  143. XI2C_IOWRITE_BITS(pdata, IC_ENABLE, ABORT, 1);
  144. while (wait--) {
  145. if (!XI2C_IOREAD_BITS(pdata, IC_ENABLE, ABORT))
  146. return 0;
  147. usleep_range(500, 600);
  148. }
  149. return -EBUSY;
  150. }
  151. static int xgbe_i2c_set_enable(struct xgbe_prv_data *pdata, bool enable)
  152. {
  153. unsigned int wait = XGBE_DISABLE_COUNT;
  154. unsigned int mode = enable ? 1 : 0;
  155. while (wait--) {
  156. XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, mode);
  157. if (XI2C_IOREAD_BITS(pdata, IC_ENABLE_STATUS, EN) == mode)
  158. return 0;
  159. usleep_range(100, 110);
  160. }
  161. return -EBUSY;
  162. }
  163. static int xgbe_i2c_disable(struct xgbe_prv_data *pdata)
  164. {
  165. unsigned int ret;
  166. ret = xgbe_i2c_set_enable(pdata, false);
  167. if (ret) {
  168. /* Disable failed, try an abort */
  169. ret = xgbe_i2c_abort(pdata);
  170. if (ret)
  171. return ret;
  172. /* Abort succeeded, try to disable again */
  173. ret = xgbe_i2c_set_enable(pdata, false);
  174. }
  175. return ret;
  176. }
  177. static int xgbe_i2c_enable(struct xgbe_prv_data *pdata)
  178. {
  179. return xgbe_i2c_set_enable(pdata, true);
  180. }
  181. static void xgbe_i2c_clear_all_interrupts(struct xgbe_prv_data *pdata)
  182. {
  183. XI2C_IOREAD(pdata, IC_CLR_INTR);
  184. }
  185. static void xgbe_i2c_disable_interrupts(struct xgbe_prv_data *pdata)
  186. {
  187. XI2C_IOWRITE(pdata, IC_INTR_MASK, 0);
  188. }
  189. static void xgbe_i2c_enable_interrupts(struct xgbe_prv_data *pdata)
  190. {
  191. XI2C_IOWRITE(pdata, IC_INTR_MASK, XGBE_DEFAULT_INT_MASK);
  192. }
  193. static void xgbe_i2c_write(struct xgbe_prv_data *pdata)
  194. {
  195. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  196. unsigned int tx_slots;
  197. unsigned int cmd;
  198. /* Configured to never receive Rx overflows, so fill up Tx fifo */
  199. tx_slots = pdata->i2c.tx_fifo_size - XI2C_IOREAD(pdata, IC_TXFLR);
  200. while (tx_slots && state->tx_len) {
  201. if (state->op->cmd == XGBE_I2C_CMD_READ)
  202. cmd = XGBE_I2C_READ;
  203. else
  204. cmd = *state->tx_buf++;
  205. if (state->tx_len == 1)
  206. XI2C_SET_BITS(cmd, IC_DATA_CMD, STOP, 1);
  207. XI2C_IOWRITE(pdata, IC_DATA_CMD, cmd);
  208. tx_slots--;
  209. state->tx_len--;
  210. }
  211. /* No more Tx operations, so ignore TX_EMPTY and return */
  212. if (!state->tx_len)
  213. XI2C_IOWRITE_BITS(pdata, IC_INTR_MASK, TX_EMPTY, 0);
  214. }
  215. static void xgbe_i2c_read(struct xgbe_prv_data *pdata)
  216. {
  217. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  218. unsigned int rx_slots;
  219. /* Anything to be read? */
  220. if (state->op->cmd != XGBE_I2C_CMD_READ)
  221. return;
  222. rx_slots = XI2C_IOREAD(pdata, IC_RXFLR);
  223. while (rx_slots && state->rx_len) {
  224. *state->rx_buf++ = XI2C_IOREAD(pdata, IC_DATA_CMD);
  225. state->rx_len--;
  226. rx_slots--;
  227. }
  228. }
  229. static void xgbe_i2c_clear_isr_interrupts(struct xgbe_prv_data *pdata,
  230. unsigned int isr)
  231. {
  232. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  233. if (isr & XGBE_INTR_TX_ABRT) {
  234. state->tx_abort_source = XI2C_IOREAD(pdata, IC_TX_ABRT_SOURCE);
  235. XI2C_IOREAD(pdata, IC_CLR_TX_ABRT);
  236. }
  237. if (isr & XGBE_INTR_STOP_DET)
  238. XI2C_IOREAD(pdata, IC_CLR_STOP_DET);
  239. }
  240. static void xgbe_i2c_isr_task(struct tasklet_struct *t)
  241. {
  242. struct xgbe_prv_data *pdata = from_tasklet(pdata, t, tasklet_i2c);
  243. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  244. unsigned int isr;
  245. isr = XI2C_IOREAD(pdata, IC_RAW_INTR_STAT);
  246. if (!isr)
  247. goto reissue_check;
  248. netif_dbg(pdata, intr, pdata->netdev,
  249. "I2C interrupt received: status=%#010x\n", isr);
  250. xgbe_i2c_clear_isr_interrupts(pdata, isr);
  251. if (isr & XGBE_INTR_TX_ABRT) {
  252. netif_dbg(pdata, link, pdata->netdev,
  253. "I2C TX_ABRT received (%#010x) for target %#04x\n",
  254. state->tx_abort_source, state->op->target);
  255. xgbe_i2c_disable_interrupts(pdata);
  256. state->ret = -EIO;
  257. goto out;
  258. }
  259. /* Check for data in the Rx fifo */
  260. xgbe_i2c_read(pdata);
  261. /* Fill up the Tx fifo next */
  262. xgbe_i2c_write(pdata);
  263. out:
  264. /* Complete on an error or STOP condition */
  265. if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET))
  266. complete(&pdata->i2c_complete);
  267. reissue_check:
  268. /* Reissue interrupt if status is not clear */
  269. if (pdata->vdata->irq_reissue_support)
  270. XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 2);
  271. }
  272. static irqreturn_t xgbe_i2c_isr(int irq, void *data)
  273. {
  274. struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
  275. if (pdata->isr_as_tasklet)
  276. tasklet_schedule(&pdata->tasklet_i2c);
  277. else
  278. xgbe_i2c_isr_task(&pdata->tasklet_i2c);
  279. return IRQ_HANDLED;
  280. }
  281. static void xgbe_i2c_set_mode(struct xgbe_prv_data *pdata)
  282. {
  283. unsigned int reg;
  284. reg = XI2C_IOREAD(pdata, IC_CON);
  285. XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1);
  286. XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1);
  287. XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1);
  288. XI2C_SET_BITS(reg, IC_CON, SPEED, XGBE_STD_SPEED);
  289. XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1);
  290. XI2C_IOWRITE(pdata, IC_CON, reg);
  291. }
  292. static void xgbe_i2c_get_features(struct xgbe_prv_data *pdata)
  293. {
  294. struct xgbe_i2c *i2c = &pdata->i2c;
  295. unsigned int reg;
  296. reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1);
  297. i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  298. MAX_SPEED_MODE);
  299. i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  300. RX_BUFFER_DEPTH);
  301. i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  302. TX_BUFFER_DEPTH);
  303. if (netif_msg_probe(pdata))
  304. dev_dbg(pdata->dev, "I2C features: %s=%u, %s=%u, %s=%u\n",
  305. "MAX_SPEED_MODE", i2c->max_speed_mode,
  306. "RX_BUFFER_DEPTH", i2c->rx_fifo_size,
  307. "TX_BUFFER_DEPTH", i2c->tx_fifo_size);
  308. }
  309. static void xgbe_i2c_set_target(struct xgbe_prv_data *pdata, unsigned int addr)
  310. {
  311. XI2C_IOWRITE(pdata, IC_TAR, addr);
  312. }
  313. static irqreturn_t xgbe_i2c_combined_isr(struct xgbe_prv_data *pdata)
  314. {
  315. xgbe_i2c_isr_task(&pdata->tasklet_i2c);
  316. return IRQ_HANDLED;
  317. }
  318. static int xgbe_i2c_xfer(struct xgbe_prv_data *pdata, struct xgbe_i2c_op *op)
  319. {
  320. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  321. int ret;
  322. mutex_lock(&pdata->i2c_mutex);
  323. reinit_completion(&pdata->i2c_complete);
  324. ret = xgbe_i2c_disable(pdata);
  325. if (ret) {
  326. netdev_err(pdata->netdev, "failed to disable i2c master\n");
  327. goto unlock;
  328. }
  329. xgbe_i2c_set_target(pdata, op->target);
  330. memset(state, 0, sizeof(*state));
  331. state->op = op;
  332. state->tx_len = op->len;
  333. state->tx_buf = op->buf;
  334. state->rx_len = op->len;
  335. state->rx_buf = op->buf;
  336. xgbe_i2c_clear_all_interrupts(pdata);
  337. ret = xgbe_i2c_enable(pdata);
  338. if (ret) {
  339. netdev_err(pdata->netdev, "failed to enable i2c master\n");
  340. goto unlock;
  341. }
  342. /* Enabling the interrupts will cause the TX FIFO empty interrupt to
  343. * fire and begin to process the command via the ISR.
  344. */
  345. xgbe_i2c_enable_interrupts(pdata);
  346. if (!wait_for_completion_timeout(&pdata->i2c_complete, HZ)) {
  347. netdev_err(pdata->netdev, "i2c operation timed out\n");
  348. ret = -ETIMEDOUT;
  349. goto disable;
  350. }
  351. ret = state->ret;
  352. if (ret) {
  353. if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK)
  354. ret = -ENOTCONN;
  355. else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST)
  356. ret = -EAGAIN;
  357. }
  358. disable:
  359. xgbe_i2c_disable_interrupts(pdata);
  360. xgbe_i2c_disable(pdata);
  361. unlock:
  362. mutex_unlock(&pdata->i2c_mutex);
  363. return ret;
  364. }
  365. static void xgbe_i2c_stop(struct xgbe_prv_data *pdata)
  366. {
  367. if (!pdata->i2c.started)
  368. return;
  369. netif_dbg(pdata, link, pdata->netdev, "stopping I2C\n");
  370. pdata->i2c.started = 0;
  371. xgbe_i2c_disable_interrupts(pdata);
  372. xgbe_i2c_disable(pdata);
  373. xgbe_i2c_clear_all_interrupts(pdata);
  374. if (pdata->dev_irq != pdata->i2c_irq) {
  375. devm_free_irq(pdata->dev, pdata->i2c_irq, pdata);
  376. tasklet_kill(&pdata->tasklet_i2c);
  377. }
  378. }
  379. static int xgbe_i2c_start(struct xgbe_prv_data *pdata)
  380. {
  381. int ret;
  382. if (pdata->i2c.started)
  383. return 0;
  384. netif_dbg(pdata, link, pdata->netdev, "starting I2C\n");
  385. /* If we have a separate I2C irq, enable it */
  386. if (pdata->dev_irq != pdata->i2c_irq) {
  387. tasklet_setup(&pdata->tasklet_i2c, xgbe_i2c_isr_task);
  388. ret = devm_request_irq(pdata->dev, pdata->i2c_irq,
  389. xgbe_i2c_isr, 0, pdata->i2c_name,
  390. pdata);
  391. if (ret) {
  392. netdev_err(pdata->netdev, "i2c irq request failed\n");
  393. return ret;
  394. }
  395. }
  396. pdata->i2c.started = 1;
  397. return 0;
  398. }
  399. static int xgbe_i2c_init(struct xgbe_prv_data *pdata)
  400. {
  401. int ret;
  402. xgbe_i2c_disable_interrupts(pdata);
  403. ret = xgbe_i2c_disable(pdata);
  404. if (ret) {
  405. dev_err(pdata->dev, "failed to disable i2c master\n");
  406. return ret;
  407. }
  408. xgbe_i2c_get_features(pdata);
  409. xgbe_i2c_set_mode(pdata);
  410. xgbe_i2c_clear_all_interrupts(pdata);
  411. return 0;
  412. }
  413. void xgbe_init_function_ptrs_i2c(struct xgbe_i2c_if *i2c_if)
  414. {
  415. i2c_if->i2c_init = xgbe_i2c_init;
  416. i2c_if->i2c_start = xgbe_i2c_start;
  417. i2c_if->i2c_stop = xgbe_i2c_stop;
  418. i2c_if->i2c_xfer = xgbe_i2c_xfer;
  419. i2c_if->i2c_isr = xgbe_i2c_combined_isr;
  420. }