xgbe-i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 irqreturn_t xgbe_i2c_isr(int irq, void *data)
  241. {
  242. struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
  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. netif_dbg(pdata, intr, pdata->netdev,
  247. "I2C interrupt received: status=%#010x\n", isr);
  248. xgbe_i2c_clear_isr_interrupts(pdata, isr);
  249. if (isr & XGBE_INTR_TX_ABRT) {
  250. netif_dbg(pdata, link, pdata->netdev,
  251. "I2C TX_ABRT received (%#010x) for target %#04x\n",
  252. state->tx_abort_source, state->op->target);
  253. xgbe_i2c_disable_interrupts(pdata);
  254. state->ret = -EIO;
  255. goto out;
  256. }
  257. /* Check for data in the Rx fifo */
  258. xgbe_i2c_read(pdata);
  259. /* Fill up the Tx fifo next */
  260. xgbe_i2c_write(pdata);
  261. out:
  262. /* Complete on an error or STOP condition */
  263. if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET))
  264. complete(&pdata->i2c_complete);
  265. return IRQ_HANDLED;
  266. }
  267. static void xgbe_i2c_set_mode(struct xgbe_prv_data *pdata)
  268. {
  269. unsigned int reg;
  270. reg = XI2C_IOREAD(pdata, IC_CON);
  271. XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1);
  272. XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1);
  273. XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1);
  274. XI2C_SET_BITS(reg, IC_CON, SPEED, XGBE_STD_SPEED);
  275. XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1);
  276. XI2C_IOWRITE(pdata, IC_CON, reg);
  277. }
  278. static void xgbe_i2c_get_features(struct xgbe_prv_data *pdata)
  279. {
  280. struct xgbe_i2c *i2c = &pdata->i2c;
  281. unsigned int reg;
  282. reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1);
  283. i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  284. MAX_SPEED_MODE);
  285. i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  286. RX_BUFFER_DEPTH);
  287. i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  288. TX_BUFFER_DEPTH);
  289. if (netif_msg_probe(pdata))
  290. dev_dbg(pdata->dev, "I2C features: %s=%u, %s=%u, %s=%u\n",
  291. "MAX_SPEED_MODE", i2c->max_speed_mode,
  292. "RX_BUFFER_DEPTH", i2c->rx_fifo_size,
  293. "TX_BUFFER_DEPTH", i2c->tx_fifo_size);
  294. }
  295. static void xgbe_i2c_set_target(struct xgbe_prv_data *pdata, unsigned int addr)
  296. {
  297. XI2C_IOWRITE(pdata, IC_TAR, addr);
  298. }
  299. static irqreturn_t xgbe_i2c_combined_isr(int irq, struct xgbe_prv_data *pdata)
  300. {
  301. if (!XI2C_IOREAD(pdata, IC_RAW_INTR_STAT))
  302. return IRQ_HANDLED;
  303. return xgbe_i2c_isr(irq, pdata);
  304. }
  305. static int xgbe_i2c_xfer(struct xgbe_prv_data *pdata, struct xgbe_i2c_op *op)
  306. {
  307. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  308. int ret;
  309. mutex_lock(&pdata->i2c_mutex);
  310. reinit_completion(&pdata->i2c_complete);
  311. ret = xgbe_i2c_disable(pdata);
  312. if (ret) {
  313. netdev_err(pdata->netdev, "failed to disable i2c master\n");
  314. goto unlock;
  315. }
  316. xgbe_i2c_set_target(pdata, op->target);
  317. memset(state, 0, sizeof(*state));
  318. state->op = op;
  319. state->tx_len = op->len;
  320. state->tx_buf = op->buf;
  321. state->rx_len = op->len;
  322. state->rx_buf = op->buf;
  323. xgbe_i2c_clear_all_interrupts(pdata);
  324. ret = xgbe_i2c_enable(pdata);
  325. if (ret) {
  326. netdev_err(pdata->netdev, "failed to enable i2c master\n");
  327. goto unlock;
  328. }
  329. /* Enabling the interrupts will cause the TX FIFO empty interrupt to
  330. * fire and begin to process the command via the ISR.
  331. */
  332. xgbe_i2c_enable_interrupts(pdata);
  333. if (!wait_for_completion_timeout(&pdata->i2c_complete, HZ)) {
  334. netdev_err(pdata->netdev, "i2c operation timed out\n");
  335. ret = -ETIMEDOUT;
  336. goto disable;
  337. }
  338. ret = state->ret;
  339. if (ret) {
  340. if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK)
  341. ret = -ENOTCONN;
  342. else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST)
  343. ret = -EAGAIN;
  344. }
  345. disable:
  346. xgbe_i2c_disable_interrupts(pdata);
  347. xgbe_i2c_disable(pdata);
  348. unlock:
  349. mutex_unlock(&pdata->i2c_mutex);
  350. return ret;
  351. }
  352. static void xgbe_i2c_stop(struct xgbe_prv_data *pdata)
  353. {
  354. if (!pdata->i2c.started)
  355. return;
  356. netif_dbg(pdata, link, pdata->netdev, "stopping I2C\n");
  357. pdata->i2c.started = 0;
  358. xgbe_i2c_disable_interrupts(pdata);
  359. xgbe_i2c_disable(pdata);
  360. xgbe_i2c_clear_all_interrupts(pdata);
  361. if (pdata->dev_irq != pdata->i2c_irq)
  362. devm_free_irq(pdata->dev, pdata->i2c_irq, pdata);
  363. }
  364. static int xgbe_i2c_start(struct xgbe_prv_data *pdata)
  365. {
  366. int ret;
  367. if (pdata->i2c.started)
  368. return 0;
  369. netif_dbg(pdata, link, pdata->netdev, "starting I2C\n");
  370. /* If we have a separate I2C irq, enable it */
  371. if (pdata->dev_irq != pdata->i2c_irq) {
  372. ret = devm_request_irq(pdata->dev, pdata->i2c_irq,
  373. xgbe_i2c_isr, 0, pdata->i2c_name,
  374. pdata);
  375. if (ret) {
  376. netdev_err(pdata->netdev, "i2c irq request failed\n");
  377. return ret;
  378. }
  379. }
  380. pdata->i2c.started = 1;
  381. return 0;
  382. }
  383. static int xgbe_i2c_init(struct xgbe_prv_data *pdata)
  384. {
  385. int ret;
  386. xgbe_i2c_disable_interrupts(pdata);
  387. ret = xgbe_i2c_disable(pdata);
  388. if (ret) {
  389. dev_err(pdata->dev, "failed to disable i2c master\n");
  390. return ret;
  391. }
  392. xgbe_i2c_get_features(pdata);
  393. xgbe_i2c_set_mode(pdata);
  394. xgbe_i2c_clear_all_interrupts(pdata);
  395. return 0;
  396. }
  397. void xgbe_init_function_ptrs_i2c(struct xgbe_i2c_if *i2c_if)
  398. {
  399. i2c_if->i2c_init = xgbe_i2c_init;
  400. i2c_if->i2c_start = xgbe_i2c_start;
  401. i2c_if->i2c_stop = xgbe_i2c_stop;
  402. i2c_if->i2c_xfer = xgbe_i2c_xfer;
  403. i2c_if->i2c_isr = xgbe_i2c_combined_isr;
  404. }