mcp251x.c 32 KB

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