mcp251x.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * CAN bus driver for Microchip 251x CAN Controller with SPI Interface
  3. *
  4. * MCP2510 support and bug fixes by Christian Pellegrin
  5. * <chripell@evolware.org>
  6. *
  7. * Copyright 2009 Christian Pellegrin EVOL S.r.l.
  8. *
  9. * Copyright 2007 Raymarine UK, Ltd. All Rights Reserved.
  10. * Written under contract by:
  11. * Chris Elston, Katalix Systems, Ltd.
  12. *
  13. * Based on Microchip MCP251x CAN controller driver written by
  14. * David Vrabel, Copyright 2006 Arcom Control Systems Ltd.
  15. *
  16. * Based on CAN bus driver for the CCAN controller written by
  17. * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
  18. * - Simon Kallweit, intefo AG
  19. * Copyright 2007
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the version 2 of the GNU General Public License
  23. * as published by the Free Software Foundation
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  32. *
  33. *
  34. *
  35. * Your platform definition file should specify something like:
  36. *
  37. * static struct mcp251x_platform_data mcp251x_info = {
  38. * .oscillator_frequency = 8000000,
  39. * };
  40. *
  41. * static struct spi_board_info spi_board_info[] = {
  42. * {
  43. * .modalias = "mcp2510",
  44. * // or "mcp2515" depending on your controller
  45. * .platform_data = &mcp251x_info,
  46. * .irq = IRQ_EINT13,
  47. * .max_speed_hz = 2*1000*1000,
  48. * .chip_select = 2,
  49. * },
  50. * };
  51. *
  52. * Please see mcp251x.h for a description of the fields in
  53. * struct mcp251x_platform_data.
  54. *
  55. */
  56. #include <linux/can/core.h>
  57. #include <linux/can/dev.h>
  58. #include <linux/can/led.h>
  59. #include <linux/can/platform/mcp251x.h>
  60. #include <linux/clk.h>
  61. #include <linux/completion.h>
  62. #include <linux/delay.h>
  63. #include <linux/device.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/freezer.h>
  66. #include <linux/interrupt.h>
  67. #include <linux/io.h>
  68. #include <linux/kernel.h>
  69. #include <linux/module.h>
  70. #include <linux/netdevice.h>
  71. #include <linux/of.h>
  72. #include <linux/of_device.h>
  73. #include <linux/platform_device.h>
  74. #include <linux/slab.h>
  75. #include <linux/spi/spi.h>
  76. #include <linux/uaccess.h>
  77. #include <linux/regulator/consumer.h>
  78. /* SPI interface instruction set */
  79. #define INSTRUCTION_WRITE 0x02
  80. #define INSTRUCTION_READ 0x03
  81. #define INSTRUCTION_BIT_MODIFY 0x05
  82. #define INSTRUCTION_LOAD_TXB(n) (0x40 + 2 * (n))
  83. #define INSTRUCTION_READ_RXB(n) (((n) == 0) ? 0x90 : 0x94)
  84. #define INSTRUCTION_RESET 0xC0
  85. #define RTS_TXB0 0x01
  86. #define RTS_TXB1 0x02
  87. #define RTS_TXB2 0x04
  88. #define INSTRUCTION_RTS(n) (0x80 | ((n) & 0x07))
  89. /* MPC251x registers */
  90. #define CANSTAT 0x0e
  91. #define CANCTRL 0x0f
  92. # define CANCTRL_REQOP_MASK 0xe0
  93. # define CANCTRL_REQOP_CONF 0x80
  94. # define CANCTRL_REQOP_LISTEN_ONLY 0x60
  95. # define CANCTRL_REQOP_LOOPBACK 0x40
  96. # define CANCTRL_REQOP_SLEEP 0x20
  97. # define CANCTRL_REQOP_NORMAL 0x00
  98. # define CANCTRL_OSM 0x08
  99. # define CANCTRL_ABAT 0x10
  100. #define TEC 0x1c
  101. #define REC 0x1d
  102. #define CNF1 0x2a
  103. # define CNF1_SJW_SHIFT 6
  104. #define CNF2 0x29
  105. # define CNF2_BTLMODE 0x80
  106. # define CNF2_SAM 0x40
  107. # define CNF2_PS1_SHIFT 3
  108. #define CNF3 0x28
  109. # define CNF3_SOF 0x08
  110. # define CNF3_WAKFIL 0x04
  111. # define CNF3_PHSEG2_MASK 0x07
  112. #define CANINTE 0x2b
  113. # define CANINTE_MERRE 0x80
  114. # define CANINTE_WAKIE 0x40
  115. # define CANINTE_ERRIE 0x20
  116. # define CANINTE_TX2IE 0x10
  117. # define CANINTE_TX1IE 0x08
  118. # define CANINTE_TX0IE 0x04
  119. # define CANINTE_RX1IE 0x02
  120. # define CANINTE_RX0IE 0x01
  121. #define CANINTF 0x2c
  122. # define CANINTF_MERRF 0x80
  123. # define CANINTF_WAKIF 0x40
  124. # define CANINTF_ERRIF 0x20
  125. # define CANINTF_TX2IF 0x10
  126. # define CANINTF_TX1IF 0x08
  127. # define CANINTF_TX0IF 0x04
  128. # define CANINTF_RX1IF 0x02
  129. # define CANINTF_RX0IF 0x01
  130. # define CANINTF_RX (CANINTF_RX0IF | CANINTF_RX1IF)
  131. # define CANINTF_TX (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)
  132. # define CANINTF_ERR (CANINTF_ERRIF)
  133. #define EFLG 0x2d
  134. # define EFLG_EWARN 0x01
  135. # define EFLG_RXWAR 0x02
  136. # define EFLG_TXWAR 0x04
  137. # define EFLG_RXEP 0x08
  138. # define EFLG_TXEP 0x10
  139. # define EFLG_TXBO 0x20
  140. # define EFLG_RX0OVR 0x40
  141. # define EFLG_RX1OVR 0x80
  142. #define TXBCTRL(n) (((n) * 0x10) + 0x30 + TXBCTRL_OFF)
  143. # define TXBCTRL_ABTF 0x40
  144. # define TXBCTRL_MLOA 0x20
  145. # define TXBCTRL_TXERR 0x10
  146. # define TXBCTRL_TXREQ 0x08
  147. #define TXBSIDH(n) (((n) * 0x10) + 0x30 + TXBSIDH_OFF)
  148. # define SIDH_SHIFT 3
  149. #define TXBSIDL(n) (((n) * 0x10) + 0x30 + TXBSIDL_OFF)
  150. # define SIDL_SID_MASK 7
  151. # define SIDL_SID_SHIFT 5
  152. # define SIDL_EXIDE_SHIFT 3
  153. # define SIDL_EID_SHIFT 16
  154. # define SIDL_EID_MASK 3
  155. #define TXBEID8(n) (((n) * 0x10) + 0x30 + TXBEID8_OFF)
  156. #define TXBEID0(n) (((n) * 0x10) + 0x30 + TXBEID0_OFF)
  157. #define TXBDLC(n) (((n) * 0x10) + 0x30 + TXBDLC_OFF)
  158. # define DLC_RTR_SHIFT 6
  159. #define TXBCTRL_OFF 0
  160. #define TXBSIDH_OFF 1
  161. #define TXBSIDL_OFF 2
  162. #define TXBEID8_OFF 3
  163. #define TXBEID0_OFF 4
  164. #define TXBDLC_OFF 5
  165. #define TXBDAT_OFF 6
  166. #define RXBCTRL(n) (((n) * 0x10) + 0x60 + RXBCTRL_OFF)
  167. # define RXBCTRL_BUKT 0x04
  168. # define RXBCTRL_RXM0 0x20
  169. # define RXBCTRL_RXM1 0x40
  170. #define RXBSIDH(n) (((n) * 0x10) + 0x60 + RXBSIDH_OFF)
  171. # define RXBSIDH_SHIFT 3
  172. #define RXBSIDL(n) (((n) * 0x10) + 0x60 + RXBSIDL_OFF)
  173. # define RXBSIDL_IDE 0x08
  174. # define RXBSIDL_SRR 0x10
  175. # define RXBSIDL_EID 3
  176. # define RXBSIDL_SHIFT 5
  177. #define RXBEID8(n) (((n) * 0x10) + 0x60 + RXBEID8_OFF)
  178. #define RXBEID0(n) (((n) * 0x10) + 0x60 + RXBEID0_OFF)
  179. #define RXBDLC(n) (((n) * 0x10) + 0x60 + RXBDLC_OFF)
  180. # define RXBDLC_LEN_MASK 0x0f
  181. # define RXBDLC_RTR 0x40
  182. #define RXBCTRL_OFF 0
  183. #define RXBSIDH_OFF 1
  184. #define RXBSIDL_OFF 2
  185. #define RXBEID8_OFF 3
  186. #define RXBEID0_OFF 4
  187. #define RXBDLC_OFF 5
  188. #define RXBDAT_OFF 6
  189. #define RXFSID(n) ((n < 3) ? 0 : 4)
  190. #define RXFSIDH(n) ((n) * 4 + RXFSID(n))
  191. #define RXFSIDL(n) ((n) * 4 + 1 + RXFSID(n))
  192. #define RXFEID8(n) ((n) * 4 + 2 + RXFSID(n))
  193. #define RXFEID0(n) ((n) * 4 + 3 + RXFSID(n))
  194. #define RXMSIDH(n) ((n) * 4 + 0x20)
  195. #define RXMSIDL(n) ((n) * 4 + 0x21)
  196. #define RXMEID8(n) ((n) * 4 + 0x22)
  197. #define RXMEID0(n) ((n) * 4 + 0x23)
  198. #define GET_BYTE(val, byte) \
  199. (((val) >> ((byte) * 8)) & 0xff)
  200. #define SET_BYTE(val, byte) \
  201. (((val) & 0xff) << ((byte) * 8))
  202. /*
  203. * Buffer size required for the largest SPI transfer (i.e., reading a
  204. * frame)
  205. */
  206. #define CAN_FRAME_MAX_DATA_LEN 8
  207. #define SPI_TRANSFER_BUF_LEN (6 + CAN_FRAME_MAX_DATA_LEN)
  208. #define CAN_FRAME_MAX_BITS 128
  209. #define TX_ECHO_SKB_MAX 1
  210. #define MCP251X_OST_DELAY_MS (5)
  211. #define DEVICE_NAME "mcp251x"
  212. static int mcp251x_enable_dma; /* Enable SPI DMA. Default: 0 (Off) */
  213. module_param(mcp251x_enable_dma, int, S_IRUGO);
  214. MODULE_PARM_DESC(mcp251x_enable_dma, "Enable SPI DMA. Default: 0 (Off)");
  215. static const struct can_bittiming_const mcp251x_bittiming_const = {
  216. .name = DEVICE_NAME,
  217. .tseg1_min = 3,
  218. .tseg1_max = 16,
  219. .tseg2_min = 2,
  220. .tseg2_max = 8,
  221. .sjw_max = 4,
  222. .brp_min = 1,
  223. .brp_max = 64,
  224. .brp_inc = 1,
  225. };
  226. enum mcp251x_model {
  227. CAN_MCP251X_MCP2510 = 0x2510,
  228. CAN_MCP251X_MCP2515 = 0x2515,
  229. };
  230. struct mcp251x_priv {
  231. struct can_priv can;
  232. struct net_device *net;
  233. struct spi_device *spi;
  234. enum mcp251x_model model;
  235. struct mutex mcp_lock; /* SPI device lock */
  236. u8 *spi_tx_buf;
  237. u8 *spi_rx_buf;
  238. dma_addr_t spi_tx_dma;
  239. dma_addr_t spi_rx_dma;
  240. struct sk_buff *tx_skb;
  241. int tx_len;
  242. struct workqueue_struct *wq;
  243. struct work_struct tx_work;
  244. struct work_struct restart_work;
  245. int force_quit;
  246. int after_suspend;
  247. #define AFTER_SUSPEND_UP 1
  248. #define AFTER_SUSPEND_DOWN 2
  249. #define AFTER_SUSPEND_POWER 4
  250. #define AFTER_SUSPEND_RESTART 8
  251. int restart_tx;
  252. struct regulator *power;
  253. struct regulator *transceiver;
  254. struct clk *clk;
  255. };
  256. #define MCP251X_IS(_model) \
  257. static inline int mcp251x_is_##_model(struct spi_device *spi) \
  258. { \
  259. struct mcp251x_priv *priv = spi_get_drvdata(spi); \
  260. return priv->model == CAN_MCP251X_MCP##_model; \
  261. }
  262. MCP251X_IS(2510);
  263. MCP251X_IS(2515);
  264. static void mcp251x_clean(struct net_device *net)
  265. {
  266. struct mcp251x_priv *priv = netdev_priv(net);
  267. if (priv->tx_skb || priv->tx_len)
  268. net->stats.tx_errors++;
  269. if (priv->tx_skb)
  270. dev_kfree_skb(priv->tx_skb);
  271. if (priv->tx_len)
  272. can_free_echo_skb(priv->net, 0);
  273. priv->tx_skb = NULL;
  274. priv->tx_len = 0;
  275. }
  276. /*
  277. * Note about handling of error return of mcp251x_spi_trans: accessing
  278. * registers via SPI is not really different conceptually than using
  279. * normal I/O assembler instructions, although it's much more
  280. * complicated from a practical POV. So it's not advisable to always
  281. * check the return value of this function. Imagine that every
  282. * read{b,l}, write{b,l} and friends would be bracketed in "if ( < 0)
  283. * error();", it would be a great mess (well there are some situation
  284. * when exception handling C++ like could be useful after all). So we
  285. * just check that transfers are OK at the beginning of our
  286. * conversation with the chip and to avoid doing really nasty things
  287. * (like injecting bogus packets in the network stack).
  288. */
  289. static int mcp251x_spi_trans(struct spi_device *spi, int len)
  290. {
  291. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  292. struct spi_transfer t = {
  293. .tx_buf = priv->spi_tx_buf,
  294. .rx_buf = priv->spi_rx_buf,
  295. .len = len,
  296. .cs_change = 0,
  297. };
  298. struct spi_message m;
  299. int ret;
  300. spi_message_init(&m);
  301. if (mcp251x_enable_dma) {
  302. t.tx_dma = priv->spi_tx_dma;
  303. t.rx_dma = priv->spi_rx_dma;
  304. m.is_dma_mapped = 1;
  305. }
  306. spi_message_add_tail(&t, &m);
  307. ret = spi_sync(spi, &m);
  308. if (ret)
  309. dev_err(&spi->dev, "spi transfer failed: ret = %d\n", ret);
  310. return ret;
  311. }
  312. static u8 mcp251x_read_reg(struct spi_device *spi, uint8_t reg)
  313. {
  314. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  315. u8 val = 0;
  316. priv->spi_tx_buf[0] = INSTRUCTION_READ;
  317. priv->spi_tx_buf[1] = reg;
  318. mcp251x_spi_trans(spi, 3);
  319. val = priv->spi_rx_buf[2];
  320. return val;
  321. }
  322. static void mcp251x_read_2regs(struct spi_device *spi, uint8_t reg,
  323. uint8_t *v1, uint8_t *v2)
  324. {
  325. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  326. priv->spi_tx_buf[0] = INSTRUCTION_READ;
  327. priv->spi_tx_buf[1] = reg;
  328. mcp251x_spi_trans(spi, 4);
  329. *v1 = priv->spi_rx_buf[2];
  330. *v2 = priv->spi_rx_buf[3];
  331. }
  332. static void mcp251x_write_reg(struct spi_device *spi, u8 reg, uint8_t val)
  333. {
  334. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  335. priv->spi_tx_buf[0] = INSTRUCTION_WRITE;
  336. priv->spi_tx_buf[1] = reg;
  337. priv->spi_tx_buf[2] = val;
  338. mcp251x_spi_trans(spi, 3);
  339. }
  340. static void mcp251x_write_bits(struct spi_device *spi, u8 reg,
  341. u8 mask, uint8_t val)
  342. {
  343. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  344. priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY;
  345. priv->spi_tx_buf[1] = reg;
  346. priv->spi_tx_buf[2] = mask;
  347. priv->spi_tx_buf[3] = val;
  348. mcp251x_spi_trans(spi, 4);
  349. }
  350. static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf,
  351. int len, int tx_buf_idx)
  352. {
  353. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  354. if (mcp251x_is_2510(spi)) {
  355. int i;
  356. for (i = 1; i < TXBDAT_OFF + len; i++)
  357. mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i,
  358. buf[i]);
  359. } else {
  360. memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len);
  361. mcp251x_spi_trans(spi, TXBDAT_OFF + len);
  362. }
  363. }
  364. static void mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame,
  365. int tx_buf_idx)
  366. {
  367. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  368. u32 sid, eid, exide, rtr;
  369. u8 buf[SPI_TRANSFER_BUF_LEN];
  370. exide = (frame->can_id & CAN_EFF_FLAG) ? 1 : 0; /* Extended ID Enable */
  371. if (exide)
  372. sid = (frame->can_id & CAN_EFF_MASK) >> 18;
  373. else
  374. sid = frame->can_id & CAN_SFF_MASK; /* Standard ID */
  375. eid = frame->can_id & CAN_EFF_MASK; /* Extended ID */
  376. rtr = (frame->can_id & CAN_RTR_FLAG) ? 1 : 0; /* Remote transmission */
  377. buf[TXBCTRL_OFF] = INSTRUCTION_LOAD_TXB(tx_buf_idx);
  378. buf[TXBSIDH_OFF] = sid >> SIDH_SHIFT;
  379. buf[TXBSIDL_OFF] = ((sid & SIDL_SID_MASK) << SIDL_SID_SHIFT) |
  380. (exide << SIDL_EXIDE_SHIFT) |
  381. ((eid >> SIDL_EID_SHIFT) & SIDL_EID_MASK);
  382. buf[TXBEID8_OFF] = GET_BYTE(eid, 1);
  383. buf[TXBEID0_OFF] = GET_BYTE(eid, 0);
  384. buf[TXBDLC_OFF] = (rtr << DLC_RTR_SHIFT) | frame->can_dlc;
  385. memcpy(buf + TXBDAT_OFF, frame->data, frame->can_dlc);
  386. mcp251x_hw_tx_frame(spi, buf, frame->can_dlc, tx_buf_idx);
  387. /* use INSTRUCTION_RTS, to avoid "repeated frame problem" */
  388. priv->spi_tx_buf[0] = INSTRUCTION_RTS(1 << tx_buf_idx);
  389. mcp251x_spi_trans(priv->spi, 1);
  390. }
  391. static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf,
  392. int buf_idx)
  393. {
  394. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  395. if (mcp251x_is_2510(spi)) {
  396. int i, len;
  397. for (i = 1; i < RXBDAT_OFF; i++)
  398. buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
  399. len = get_can_dlc(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK);
  400. for (; i < (RXBDAT_OFF + len); i++)
  401. buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
  402. } else {
  403. priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx);
  404. mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
  405. memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
  406. }
  407. }
  408. static void mcp251x_hw_rx(struct spi_device *spi, int buf_idx)
  409. {
  410. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  411. struct sk_buff *skb;
  412. struct can_frame *frame;
  413. u8 buf[SPI_TRANSFER_BUF_LEN];
  414. skb = alloc_can_skb(priv->net, &frame);
  415. if (!skb) {
  416. dev_err(&spi->dev, "cannot allocate RX skb\n");
  417. priv->net->stats.rx_dropped++;
  418. return;
  419. }
  420. mcp251x_hw_rx_frame(spi, buf, buf_idx);
  421. if (buf[RXBSIDL_OFF] & RXBSIDL_IDE) {
  422. /* Extended ID format */
  423. frame->can_id = CAN_EFF_FLAG;
  424. frame->can_id |=
  425. /* Extended ID part */
  426. SET_BYTE(buf[RXBSIDL_OFF] & RXBSIDL_EID, 2) |
  427. SET_BYTE(buf[RXBEID8_OFF], 1) |
  428. SET_BYTE(buf[RXBEID0_OFF], 0) |
  429. /* Standard ID part */
  430. (((buf[RXBSIDH_OFF] << RXBSIDH_SHIFT) |
  431. (buf[RXBSIDL_OFF] >> RXBSIDL_SHIFT)) << 18);
  432. /* Remote transmission request */
  433. if (buf[RXBDLC_OFF] & RXBDLC_RTR)
  434. frame->can_id |= CAN_RTR_FLAG;
  435. } else {
  436. /* Standard ID format */
  437. frame->can_id =
  438. (buf[RXBSIDH_OFF] << RXBSIDH_SHIFT) |
  439. (buf[RXBSIDL_OFF] >> RXBSIDL_SHIFT);
  440. if (buf[RXBSIDL_OFF] & RXBSIDL_SRR)
  441. frame->can_id |= CAN_RTR_FLAG;
  442. }
  443. /* Data length */
  444. frame->can_dlc = get_can_dlc(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK);
  445. memcpy(frame->data, buf + RXBDAT_OFF, frame->can_dlc);
  446. priv->net->stats.rx_packets++;
  447. priv->net->stats.rx_bytes += frame->can_dlc;
  448. can_led_event(priv->net, CAN_LED_EVENT_RX);
  449. netif_rx_ni(skb);
  450. }
  451. static void mcp251x_hw_sleep(struct spi_device *spi)
  452. {
  453. mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP);
  454. }
  455. static netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb,
  456. struct net_device *net)
  457. {
  458. struct mcp251x_priv *priv = netdev_priv(net);
  459. struct spi_device *spi = priv->spi;
  460. if (priv->tx_skb || priv->tx_len) {
  461. dev_warn(&spi->dev, "hard_xmit called while tx busy\n");
  462. return NETDEV_TX_BUSY;
  463. }
  464. if (can_dropped_invalid_skb(net, skb))
  465. return NETDEV_TX_OK;
  466. netif_stop_queue(net);
  467. priv->tx_skb = skb;
  468. queue_work(priv->wq, &priv->tx_work);
  469. return NETDEV_TX_OK;
  470. }
  471. static int mcp251x_do_set_mode(struct net_device *net, enum can_mode mode)
  472. {
  473. struct mcp251x_priv *priv = netdev_priv(net);
  474. switch (mode) {
  475. case CAN_MODE_START:
  476. mcp251x_clean(net);
  477. /* We have to delay work since SPI I/O may sleep */
  478. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  479. priv->restart_tx = 1;
  480. if (priv->can.restart_ms == 0)
  481. priv->after_suspend = AFTER_SUSPEND_RESTART;
  482. queue_work(priv->wq, &priv->restart_work);
  483. break;
  484. default:
  485. return -EOPNOTSUPP;
  486. }
  487. return 0;
  488. }
  489. static int mcp251x_set_normal_mode(struct spi_device *spi)
  490. {
  491. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  492. unsigned long timeout;
  493. /* Enable interrupts */
  494. mcp251x_write_reg(spi, CANINTE,
  495. CANINTE_ERRIE | CANINTE_TX2IE | CANINTE_TX1IE |
  496. CANINTE_TX0IE | CANINTE_RX1IE | CANINTE_RX0IE);
  497. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
  498. /* Put device into loopback mode */
  499. mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LOOPBACK);
  500. } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
  501. /* Put device into listen-only mode */
  502. mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY);
  503. } else {
  504. /* Put device into normal mode */
  505. mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL);
  506. /* Wait for the device to enter normal mode */
  507. timeout = jiffies + HZ;
  508. while (mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK) {
  509. schedule();
  510. if (time_after(jiffies, timeout)) {
  511. dev_err(&spi->dev, "MCP251x didn't"
  512. " enter in normal mode\n");
  513. return -EBUSY;
  514. }
  515. }
  516. }
  517. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  518. return 0;
  519. }
  520. static int mcp251x_do_set_bittiming(struct net_device *net)
  521. {
  522. struct mcp251x_priv *priv = netdev_priv(net);
  523. struct can_bittiming *bt = &priv->can.bittiming;
  524. struct spi_device *spi = priv->spi;
  525. mcp251x_write_reg(spi, CNF1, ((bt->sjw - 1) << CNF1_SJW_SHIFT) |
  526. (bt->brp - 1));
  527. mcp251x_write_reg(spi, CNF2, CNF2_BTLMODE |
  528. (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ?
  529. CNF2_SAM : 0) |
  530. ((bt->phase_seg1 - 1) << CNF2_PS1_SHIFT) |
  531. (bt->prop_seg - 1));
  532. mcp251x_write_bits(spi, CNF3, CNF3_PHSEG2_MASK,
  533. (bt->phase_seg2 - 1));
  534. dev_dbg(&spi->dev, "CNF: 0x%02x 0x%02x 0x%02x\n",
  535. mcp251x_read_reg(spi, CNF1),
  536. mcp251x_read_reg(spi, CNF2),
  537. mcp251x_read_reg(spi, CNF3));
  538. return 0;
  539. }
  540. static int mcp251x_setup(struct net_device *net, struct mcp251x_priv *priv,
  541. struct spi_device *spi)
  542. {
  543. mcp251x_do_set_bittiming(net);
  544. mcp251x_write_reg(spi, RXBCTRL(0),
  545. RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1);
  546. mcp251x_write_reg(spi, RXBCTRL(1),
  547. RXBCTRL_RXM0 | RXBCTRL_RXM1);
  548. return 0;
  549. }
  550. static int mcp251x_hw_reset(struct spi_device *spi)
  551. {
  552. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  553. u8 reg;
  554. int ret;
  555. /* Wait for oscillator startup timer after power up */
  556. mdelay(MCP251X_OST_DELAY_MS);
  557. priv->spi_tx_buf[0] = INSTRUCTION_RESET;
  558. ret = mcp251x_spi_trans(spi, 1);
  559. if (ret)
  560. return ret;
  561. /* Wait for oscillator startup timer after reset */
  562. mdelay(MCP251X_OST_DELAY_MS);
  563. reg = mcp251x_read_reg(spi, CANSTAT);
  564. if ((reg & CANCTRL_REQOP_MASK) != CANCTRL_REQOP_CONF)
  565. return -ENODEV;
  566. return 0;
  567. }
  568. static int mcp251x_hw_probe(struct spi_device *spi)
  569. {
  570. u8 ctrl;
  571. int ret;
  572. ret = mcp251x_hw_reset(spi);
  573. if (ret)
  574. return ret;
  575. ctrl = mcp251x_read_reg(spi, CANCTRL);
  576. dev_dbg(&spi->dev, "CANCTRL 0x%02x\n", ctrl);
  577. /* Check for power up default value */
  578. if ((ctrl & 0x17) != 0x07)
  579. return -ENODEV;
  580. return 0;
  581. }
  582. static int mcp251x_power_enable(struct regulator *reg, int enable)
  583. {
  584. if (IS_ERR_OR_NULL(reg))
  585. return 0;
  586. if (enable)
  587. return regulator_enable(reg);
  588. else
  589. return regulator_disable(reg);
  590. }
  591. static void mcp251x_open_clean(struct net_device *net)
  592. {
  593. struct mcp251x_priv *priv = netdev_priv(net);
  594. struct spi_device *spi = priv->spi;
  595. free_irq(spi->irq, priv);
  596. mcp251x_hw_sleep(spi);
  597. mcp251x_power_enable(priv->transceiver, 0);
  598. close_candev(net);
  599. }
  600. static int mcp251x_stop(struct net_device *net)
  601. {
  602. struct mcp251x_priv *priv = netdev_priv(net);
  603. struct spi_device *spi = priv->spi;
  604. close_candev(net);
  605. priv->force_quit = 1;
  606. free_irq(spi->irq, priv);
  607. destroy_workqueue(priv->wq);
  608. priv->wq = NULL;
  609. mutex_lock(&priv->mcp_lock);
  610. /* Disable and clear pending interrupts */
  611. mcp251x_write_reg(spi, CANINTE, 0x00);
  612. mcp251x_write_reg(spi, CANINTF, 0x00);
  613. mcp251x_write_reg(spi, TXBCTRL(0), 0);
  614. mcp251x_clean(net);
  615. mcp251x_hw_sleep(spi);
  616. mcp251x_power_enable(priv->transceiver, 0);
  617. priv->can.state = CAN_STATE_STOPPED;
  618. mutex_unlock(&priv->mcp_lock);
  619. can_led_event(net, CAN_LED_EVENT_STOP);
  620. return 0;
  621. }
  622. static void mcp251x_error_skb(struct net_device *net, int can_id, int data1)
  623. {
  624. struct sk_buff *skb;
  625. struct can_frame *frame;
  626. skb = alloc_can_err_skb(net, &frame);
  627. if (skb) {
  628. frame->can_id |= can_id;
  629. frame->data[1] = data1;
  630. netif_rx_ni(skb);
  631. } else {
  632. netdev_err(net, "cannot allocate error skb\n");
  633. }
  634. }
  635. static void mcp251x_tx_work_handler(struct work_struct *ws)
  636. {
  637. struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
  638. tx_work);
  639. struct spi_device *spi = priv->spi;
  640. struct net_device *net = priv->net;
  641. struct can_frame *frame;
  642. mutex_lock(&priv->mcp_lock);
  643. if (priv->tx_skb) {
  644. if (priv->can.state == CAN_STATE_BUS_OFF) {
  645. mcp251x_clean(net);
  646. } else {
  647. frame = (struct can_frame *)priv->tx_skb->data;
  648. if (frame->can_dlc > CAN_FRAME_MAX_DATA_LEN)
  649. frame->can_dlc = CAN_FRAME_MAX_DATA_LEN;
  650. mcp251x_hw_tx(spi, frame, 0);
  651. priv->tx_len = 1 + frame->can_dlc;
  652. can_put_echo_skb(priv->tx_skb, net, 0);
  653. priv->tx_skb = NULL;
  654. }
  655. }
  656. mutex_unlock(&priv->mcp_lock);
  657. }
  658. static void mcp251x_restart_work_handler(struct work_struct *ws)
  659. {
  660. struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
  661. restart_work);
  662. struct spi_device *spi = priv->spi;
  663. struct net_device *net = priv->net;
  664. mutex_lock(&priv->mcp_lock);
  665. if (priv->after_suspend) {
  666. mcp251x_hw_reset(spi);
  667. mcp251x_setup(net, priv, spi);
  668. if (priv->after_suspend & AFTER_SUSPEND_RESTART) {
  669. mcp251x_set_normal_mode(spi);
  670. } else if (priv->after_suspend & AFTER_SUSPEND_UP) {
  671. netif_device_attach(net);
  672. mcp251x_clean(net);
  673. mcp251x_set_normal_mode(spi);
  674. netif_wake_queue(net);
  675. } else {
  676. mcp251x_hw_sleep(spi);
  677. }
  678. priv->after_suspend = 0;
  679. priv->force_quit = 0;
  680. }
  681. if (priv->restart_tx) {
  682. priv->restart_tx = 0;
  683. mcp251x_write_reg(spi, TXBCTRL(0), 0);
  684. mcp251x_clean(net);
  685. netif_wake_queue(net);
  686. mcp251x_error_skb(net, CAN_ERR_RESTARTED, 0);
  687. }
  688. mutex_unlock(&priv->mcp_lock);
  689. }
  690. static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
  691. {
  692. struct mcp251x_priv *priv = dev_id;
  693. struct spi_device *spi = priv->spi;
  694. struct net_device *net = priv->net;
  695. mutex_lock(&priv->mcp_lock);
  696. while (!priv->force_quit) {
  697. enum can_state new_state;
  698. u8 intf, eflag;
  699. u8 clear_intf = 0;
  700. int can_id = 0, data1 = 0;
  701. mcp251x_read_2regs(spi, CANINTF, &intf, &eflag);
  702. /* mask out flags we don't care about */
  703. intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
  704. /* receive buffer 0 */
  705. if (intf & CANINTF_RX0IF) {
  706. mcp251x_hw_rx(spi, 0);
  707. /*
  708. * Free one buffer ASAP
  709. * (The MCP2515 does this automatically.)
  710. */
  711. if (mcp251x_is_2510(spi))
  712. mcp251x_write_bits(spi, CANINTF, CANINTF_RX0IF, 0x00);
  713. }
  714. /* receive buffer 1 */
  715. if (intf & CANINTF_RX1IF) {
  716. mcp251x_hw_rx(spi, 1);
  717. /* the MCP2515 does this automatically */
  718. if (mcp251x_is_2510(spi))
  719. clear_intf |= CANINTF_RX1IF;
  720. }
  721. /* any error or tx interrupt we need to clear? */
  722. if (intf & (CANINTF_ERR | CANINTF_TX))
  723. clear_intf |= intf & (CANINTF_ERR | CANINTF_TX);
  724. if (clear_intf)
  725. mcp251x_write_bits(spi, CANINTF, clear_intf, 0x00);
  726. if (eflag)
  727. mcp251x_write_bits(spi, EFLG, eflag, 0x00);
  728. /* Update can state */
  729. if (eflag & EFLG_TXBO) {
  730. new_state = CAN_STATE_BUS_OFF;
  731. can_id |= CAN_ERR_BUSOFF;
  732. } else if (eflag & EFLG_TXEP) {
  733. new_state = CAN_STATE_ERROR_PASSIVE;
  734. can_id |= CAN_ERR_CRTL;
  735. data1 |= CAN_ERR_CRTL_TX_PASSIVE;
  736. } else if (eflag & EFLG_RXEP) {
  737. new_state = CAN_STATE_ERROR_PASSIVE;
  738. can_id |= CAN_ERR_CRTL;
  739. data1 |= CAN_ERR_CRTL_RX_PASSIVE;
  740. } else if (eflag & EFLG_TXWAR) {
  741. new_state = CAN_STATE_ERROR_WARNING;
  742. can_id |= CAN_ERR_CRTL;
  743. data1 |= CAN_ERR_CRTL_TX_WARNING;
  744. } else if (eflag & EFLG_RXWAR) {
  745. new_state = CAN_STATE_ERROR_WARNING;
  746. can_id |= CAN_ERR_CRTL;
  747. data1 |= CAN_ERR_CRTL_RX_WARNING;
  748. } else {
  749. new_state = CAN_STATE_ERROR_ACTIVE;
  750. }
  751. /* Update can state statistics */
  752. switch (priv->can.state) {
  753. case CAN_STATE_ERROR_ACTIVE:
  754. if (new_state >= CAN_STATE_ERROR_WARNING &&
  755. new_state <= CAN_STATE_BUS_OFF)
  756. priv->can.can_stats.error_warning++;
  757. case CAN_STATE_ERROR_WARNING: /* fallthrough */
  758. if (new_state >= CAN_STATE_ERROR_PASSIVE &&
  759. new_state <= CAN_STATE_BUS_OFF)
  760. priv->can.can_stats.error_passive++;
  761. break;
  762. default:
  763. break;
  764. }
  765. priv->can.state = new_state;
  766. if (intf & CANINTF_ERRIF) {
  767. /* Handle overflow counters */
  768. if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR)) {
  769. if (eflag & EFLG_RX0OVR) {
  770. net->stats.rx_over_errors++;
  771. net->stats.rx_errors++;
  772. }
  773. if (eflag & EFLG_RX1OVR) {
  774. net->stats.rx_over_errors++;
  775. net->stats.rx_errors++;
  776. }
  777. can_id |= CAN_ERR_CRTL;
  778. data1 |= CAN_ERR_CRTL_RX_OVERFLOW;
  779. }
  780. mcp251x_error_skb(net, can_id, data1);
  781. }
  782. if (priv->can.state == CAN_STATE_BUS_OFF) {
  783. if (priv->can.restart_ms == 0) {
  784. priv->force_quit = 1;
  785. priv->can.can_stats.bus_off++;
  786. can_bus_off(net);
  787. mcp251x_hw_sleep(spi);
  788. break;
  789. }
  790. }
  791. if (intf == 0)
  792. break;
  793. if (intf & CANINTF_TX) {
  794. net->stats.tx_packets++;
  795. net->stats.tx_bytes += priv->tx_len - 1;
  796. can_led_event(net, CAN_LED_EVENT_TX);
  797. if (priv->tx_len) {
  798. can_get_echo_skb(net, 0);
  799. priv->tx_len = 0;
  800. }
  801. netif_wake_queue(net);
  802. }
  803. }
  804. mutex_unlock(&priv->mcp_lock);
  805. return IRQ_HANDLED;
  806. }
  807. static int mcp251x_open(struct net_device *net)
  808. {
  809. struct mcp251x_priv *priv = netdev_priv(net);
  810. struct spi_device *spi = priv->spi;
  811. unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING;
  812. int ret;
  813. ret = open_candev(net);
  814. if (ret) {
  815. dev_err(&spi->dev, "unable to set initial baudrate!\n");
  816. return ret;
  817. }
  818. mutex_lock(&priv->mcp_lock);
  819. mcp251x_power_enable(priv->transceiver, 1);
  820. priv->force_quit = 0;
  821. priv->tx_skb = NULL;
  822. priv->tx_len = 0;
  823. ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist,
  824. flags | IRQF_ONESHOT, DEVICE_NAME, priv);
  825. if (ret) {
  826. dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
  827. mcp251x_power_enable(priv->transceiver, 0);
  828. close_candev(net);
  829. goto open_unlock;
  830. }
  831. priv->wq = create_freezable_workqueue("mcp251x_wq");
  832. INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler);
  833. INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler);
  834. ret = mcp251x_hw_reset(spi);
  835. if (ret) {
  836. mcp251x_open_clean(net);
  837. goto open_unlock;
  838. }
  839. ret = mcp251x_setup(net, priv, spi);
  840. if (ret) {
  841. mcp251x_open_clean(net);
  842. goto open_unlock;
  843. }
  844. ret = mcp251x_set_normal_mode(spi);
  845. if (ret) {
  846. mcp251x_open_clean(net);
  847. goto open_unlock;
  848. }
  849. can_led_event(net, CAN_LED_EVENT_OPEN);
  850. netif_wake_queue(net);
  851. open_unlock:
  852. mutex_unlock(&priv->mcp_lock);
  853. return ret;
  854. }
  855. static const struct net_device_ops mcp251x_netdev_ops = {
  856. .ndo_open = mcp251x_open,
  857. .ndo_stop = mcp251x_stop,
  858. .ndo_start_xmit = mcp251x_hard_start_xmit,
  859. .ndo_change_mtu = can_change_mtu,
  860. };
  861. static const struct of_device_id mcp251x_of_match[] = {
  862. {
  863. .compatible = "microchip,mcp2510",
  864. .data = (void *)CAN_MCP251X_MCP2510,
  865. },
  866. {
  867. .compatible = "microchip,mcp2515",
  868. .data = (void *)CAN_MCP251X_MCP2515,
  869. },
  870. { }
  871. };
  872. MODULE_DEVICE_TABLE(of, mcp251x_of_match);
  873. static const struct spi_device_id mcp251x_id_table[] = {
  874. {
  875. .name = "mcp2510",
  876. .driver_data = (kernel_ulong_t)CAN_MCP251X_MCP2510,
  877. },
  878. {
  879. .name = "mcp2515",
  880. .driver_data = (kernel_ulong_t)CAN_MCP251X_MCP2515,
  881. },
  882. { }
  883. };
  884. MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
  885. static int mcp251x_can_probe(struct spi_device *spi)
  886. {
  887. const struct of_device_id *of_id = of_match_device(mcp251x_of_match,
  888. &spi->dev);
  889. struct mcp251x_platform_data *pdata = dev_get_platdata(&spi->dev);
  890. struct net_device *net;
  891. struct mcp251x_priv *priv;
  892. struct clk *clk;
  893. int freq, ret;
  894. clk = devm_clk_get(&spi->dev, NULL);
  895. if (IS_ERR(clk)) {
  896. if (pdata)
  897. freq = pdata->oscillator_frequency;
  898. else
  899. return PTR_ERR(clk);
  900. } else {
  901. freq = clk_get_rate(clk);
  902. }
  903. /* Sanity check */
  904. if (freq < 1000000 || freq > 25000000)
  905. return -ERANGE;
  906. /* Allocate can/net device */
  907. net = alloc_candev(sizeof(struct mcp251x_priv), TX_ECHO_SKB_MAX);
  908. if (!net)
  909. return -ENOMEM;
  910. if (!IS_ERR(clk)) {
  911. ret = clk_prepare_enable(clk);
  912. if (ret)
  913. goto out_free;
  914. }
  915. net->netdev_ops = &mcp251x_netdev_ops;
  916. net->flags |= IFF_ECHO;
  917. priv = netdev_priv(net);
  918. priv->can.bittiming_const = &mcp251x_bittiming_const;
  919. priv->can.do_set_mode = mcp251x_do_set_mode;
  920. priv->can.clock.freq = freq / 2;
  921. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
  922. CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY;
  923. if (of_id)
  924. priv->model = (enum mcp251x_model)of_id->data;
  925. else
  926. priv->model = spi_get_device_id(spi)->driver_data;
  927. priv->net = net;
  928. priv->clk = clk;
  929. spi_set_drvdata(spi, priv);
  930. /* Configure the SPI bus */
  931. spi->bits_per_word = 8;
  932. if (mcp251x_is_2510(spi))
  933. spi->max_speed_hz = spi->max_speed_hz ? : 5 * 1000 * 1000;
  934. else
  935. spi->max_speed_hz = spi->max_speed_hz ? : 10 * 1000 * 1000;
  936. ret = spi_setup(spi);
  937. if (ret)
  938. goto out_clk;
  939. priv->power = devm_regulator_get_optional(&spi->dev, "vdd");
  940. priv->transceiver = devm_regulator_get_optional(&spi->dev, "xceiver");
  941. if ((PTR_ERR(priv->power) == -EPROBE_DEFER) ||
  942. (PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) {
  943. ret = -EPROBE_DEFER;
  944. goto out_clk;
  945. }
  946. ret = mcp251x_power_enable(priv->power, 1);
  947. if (ret)
  948. goto out_clk;
  949. priv->spi = spi;
  950. mutex_init(&priv->mcp_lock);
  951. /* If requested, allocate DMA buffers */
  952. if (mcp251x_enable_dma) {
  953. spi->dev.coherent_dma_mask = ~0;
  954. /*
  955. * Minimum coherent DMA allocation is PAGE_SIZE, so allocate
  956. * that much and share it between Tx and Rx DMA buffers.
  957. */
  958. priv->spi_tx_buf = dmam_alloc_coherent(&spi->dev,
  959. PAGE_SIZE,
  960. &priv->spi_tx_dma,
  961. GFP_DMA);
  962. if (priv->spi_tx_buf) {
  963. priv->spi_rx_buf = (priv->spi_tx_buf + (PAGE_SIZE / 2));
  964. priv->spi_rx_dma = (dma_addr_t)(priv->spi_tx_dma +
  965. (PAGE_SIZE / 2));
  966. } else {
  967. /* Fall back to non-DMA */
  968. mcp251x_enable_dma = 0;
  969. }
  970. }
  971. /* Allocate non-DMA buffers */
  972. if (!mcp251x_enable_dma) {
  973. priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN,
  974. GFP_KERNEL);
  975. if (!priv->spi_tx_buf) {
  976. ret = -ENOMEM;
  977. goto error_probe;
  978. }
  979. priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN,
  980. GFP_KERNEL);
  981. if (!priv->spi_rx_buf) {
  982. ret = -ENOMEM;
  983. goto error_probe;
  984. }
  985. }
  986. SET_NETDEV_DEV(net, &spi->dev);
  987. /* Here is OK to not lock the MCP, no one knows about it yet */
  988. ret = mcp251x_hw_probe(spi);
  989. if (ret)
  990. goto error_probe;
  991. mcp251x_hw_sleep(spi);
  992. ret = register_candev(net);
  993. if (ret)
  994. goto error_probe;
  995. devm_can_led_init(net);
  996. return 0;
  997. error_probe:
  998. mcp251x_power_enable(priv->power, 0);
  999. out_clk:
  1000. if (!IS_ERR(clk))
  1001. clk_disable_unprepare(clk);
  1002. out_free:
  1003. free_candev(net);
  1004. return ret;
  1005. }
  1006. static int mcp251x_can_remove(struct spi_device *spi)
  1007. {
  1008. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  1009. struct net_device *net = priv->net;
  1010. unregister_candev(net);
  1011. mcp251x_power_enable(priv->power, 0);
  1012. if (!IS_ERR(priv->clk))
  1013. clk_disable_unprepare(priv->clk);
  1014. free_candev(net);
  1015. return 0;
  1016. }
  1017. static int __maybe_unused mcp251x_can_suspend(struct device *dev)
  1018. {
  1019. struct spi_device *spi = to_spi_device(dev);
  1020. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  1021. struct net_device *net = priv->net;
  1022. priv->force_quit = 1;
  1023. disable_irq(spi->irq);
  1024. /*
  1025. * Note: at this point neither IST nor workqueues are running.
  1026. * open/stop cannot be called anyway so locking is not needed
  1027. */
  1028. if (netif_running(net)) {
  1029. netif_device_detach(net);
  1030. mcp251x_hw_sleep(spi);
  1031. mcp251x_power_enable(priv->transceiver, 0);
  1032. priv->after_suspend = AFTER_SUSPEND_UP;
  1033. } else {
  1034. priv->after_suspend = AFTER_SUSPEND_DOWN;
  1035. }
  1036. if (!IS_ERR_OR_NULL(priv->power)) {
  1037. regulator_disable(priv->power);
  1038. priv->after_suspend |= AFTER_SUSPEND_POWER;
  1039. }
  1040. return 0;
  1041. }
  1042. static int __maybe_unused mcp251x_can_resume(struct device *dev)
  1043. {
  1044. struct spi_device *spi = to_spi_device(dev);
  1045. struct mcp251x_priv *priv = spi_get_drvdata(spi);
  1046. if (priv->after_suspend & AFTER_SUSPEND_POWER)
  1047. mcp251x_power_enable(priv->power, 1);
  1048. if (priv->after_suspend & AFTER_SUSPEND_UP) {
  1049. mcp251x_power_enable(priv->transceiver, 1);
  1050. queue_work(priv->wq, &priv->restart_work);
  1051. } else {
  1052. priv->after_suspend = 0;
  1053. }
  1054. priv->force_quit = 0;
  1055. enable_irq(spi->irq);
  1056. return 0;
  1057. }
  1058. static SIMPLE_DEV_PM_OPS(mcp251x_can_pm_ops, mcp251x_can_suspend,
  1059. mcp251x_can_resume);
  1060. static struct spi_driver mcp251x_can_driver = {
  1061. .driver = {
  1062. .name = DEVICE_NAME,
  1063. .of_match_table = mcp251x_of_match,
  1064. .pm = &mcp251x_can_pm_ops,
  1065. },
  1066. .id_table = mcp251x_id_table,
  1067. .probe = mcp251x_can_probe,
  1068. .remove = mcp251x_can_remove,
  1069. };
  1070. module_spi_driver(mcp251x_can_driver);
  1071. MODULE_AUTHOR("Chris Elston <celston@katalix.com>, "
  1072. "Christian Pellegrin <chripell@evolware.org>");
  1073. MODULE_DESCRIPTION("Microchip 251x CAN driver");
  1074. MODULE_LICENSE("GPL v2");