pch_can.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * Copyright (C) 1999 - 2010 Intel Corporation.
  3. * Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/pci.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/errno.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/can.h>
  31. #include <linux/can/dev.h>
  32. #include <linux/can/error.h>
  33. #define PCH_CTRL_INIT BIT(0) /* The INIT bit of CANCONT register. */
  34. #define PCH_CTRL_IE BIT(1) /* The IE bit of CAN control register */
  35. #define PCH_CTRL_IE_SIE_EIE (BIT(3) | BIT(2) | BIT(1))
  36. #define PCH_CTRL_CCE BIT(6)
  37. #define PCH_CTRL_OPT BIT(7) /* The OPT bit of CANCONT register. */
  38. #define PCH_OPT_SILENT BIT(3) /* The Silent bit of CANOPT reg. */
  39. #define PCH_OPT_LBACK BIT(4) /* The LoopBack bit of CANOPT reg. */
  40. #define PCH_CMASK_RX_TX_SET 0x00f3
  41. #define PCH_CMASK_RX_TX_GET 0x0073
  42. #define PCH_CMASK_ALL 0xff
  43. #define PCH_CMASK_NEWDAT BIT(2)
  44. #define PCH_CMASK_CLRINTPND BIT(3)
  45. #define PCH_CMASK_CTRL BIT(4)
  46. #define PCH_CMASK_ARB BIT(5)
  47. #define PCH_CMASK_MASK BIT(6)
  48. #define PCH_CMASK_RDWR BIT(7)
  49. #define PCH_IF_MCONT_NEWDAT BIT(15)
  50. #define PCH_IF_MCONT_MSGLOST BIT(14)
  51. #define PCH_IF_MCONT_INTPND BIT(13)
  52. #define PCH_IF_MCONT_UMASK BIT(12)
  53. #define PCH_IF_MCONT_TXIE BIT(11)
  54. #define PCH_IF_MCONT_RXIE BIT(10)
  55. #define PCH_IF_MCONT_RMTEN BIT(9)
  56. #define PCH_IF_MCONT_TXRQXT BIT(8)
  57. #define PCH_IF_MCONT_EOB BIT(7)
  58. #define PCH_IF_MCONT_DLC (BIT(0) | BIT(1) | BIT(2) | BIT(3))
  59. #define PCH_MASK2_MDIR_MXTD (BIT(14) | BIT(15))
  60. #define PCH_ID2_DIR BIT(13)
  61. #define PCH_ID2_XTD BIT(14)
  62. #define PCH_ID_MSGVAL BIT(15)
  63. #define PCH_IF_CREQ_BUSY BIT(15)
  64. #define PCH_STATUS_INT 0x8000
  65. #define PCH_REC 0x00007f00
  66. #define PCH_TEC 0x000000ff
  67. #define PCH_TX_OK BIT(3)
  68. #define PCH_RX_OK BIT(4)
  69. #define PCH_EPASSIV BIT(5)
  70. #define PCH_EWARN BIT(6)
  71. #define PCH_BUS_OFF BIT(7)
  72. /* bit position of certain controller bits. */
  73. #define PCH_BIT_BRP_SHIFT 0
  74. #define PCH_BIT_SJW_SHIFT 6
  75. #define PCH_BIT_TSEG1_SHIFT 8
  76. #define PCH_BIT_TSEG2_SHIFT 12
  77. #define PCH_BIT_BRPE_BRPE_SHIFT 6
  78. #define PCH_MSK_BITT_BRP 0x3f
  79. #define PCH_MSK_BRPE_BRPE 0x3c0
  80. #define PCH_MSK_CTRL_IE_SIE_EIE 0x07
  81. #define PCH_COUNTER_LIMIT 10
  82. #define PCH_CAN_CLK 50000000 /* 50MHz */
  83. /*
  84. * Define the number of message object.
  85. * PCH CAN communications are done via Message RAM.
  86. * The Message RAM consists of 32 message objects.
  87. */
  88. #define PCH_RX_OBJ_NUM 26
  89. #define PCH_TX_OBJ_NUM 6
  90. #define PCH_RX_OBJ_START 1
  91. #define PCH_RX_OBJ_END PCH_RX_OBJ_NUM
  92. #define PCH_TX_OBJ_START (PCH_RX_OBJ_END + 1)
  93. #define PCH_TX_OBJ_END (PCH_RX_OBJ_NUM + PCH_TX_OBJ_NUM)
  94. #define PCH_FIFO_THRESH 16
  95. /* TxRqst2 show status of MsgObjNo.17~32 */
  96. #define PCH_TREQ2_TX_MASK (((1 << PCH_TX_OBJ_NUM) - 1) <<\
  97. (PCH_RX_OBJ_END - 16))
  98. enum pch_ifreg {
  99. PCH_RX_IFREG,
  100. PCH_TX_IFREG,
  101. };
  102. enum pch_can_err {
  103. PCH_STUF_ERR = 1,
  104. PCH_FORM_ERR,
  105. PCH_ACK_ERR,
  106. PCH_BIT1_ERR,
  107. PCH_BIT0_ERR,
  108. PCH_CRC_ERR,
  109. PCH_LEC_ALL,
  110. };
  111. enum pch_can_mode {
  112. PCH_CAN_ENABLE,
  113. PCH_CAN_DISABLE,
  114. PCH_CAN_ALL,
  115. PCH_CAN_NONE,
  116. PCH_CAN_STOP,
  117. PCH_CAN_RUN,
  118. };
  119. struct pch_can_if_regs {
  120. u32 creq;
  121. u32 cmask;
  122. u32 mask1;
  123. u32 mask2;
  124. u32 id1;
  125. u32 id2;
  126. u32 mcont;
  127. u32 data[4];
  128. u32 rsv[13];
  129. };
  130. struct pch_can_regs {
  131. u32 cont;
  132. u32 stat;
  133. u32 errc;
  134. u32 bitt;
  135. u32 intr;
  136. u32 opt;
  137. u32 brpe;
  138. u32 reserve;
  139. struct pch_can_if_regs ifregs[2]; /* [0]=if1 [1]=if2 */
  140. u32 reserve1[8];
  141. u32 treq1;
  142. u32 treq2;
  143. u32 reserve2[6];
  144. u32 data1;
  145. u32 data2;
  146. u32 reserve3[6];
  147. u32 canipend1;
  148. u32 canipend2;
  149. u32 reserve4[6];
  150. u32 canmval1;
  151. u32 canmval2;
  152. u32 reserve5[37];
  153. u32 srst;
  154. };
  155. struct pch_can_priv {
  156. struct can_priv can;
  157. struct pci_dev *dev;
  158. u32 tx_enable[PCH_TX_OBJ_END];
  159. u32 rx_enable[PCH_TX_OBJ_END];
  160. u32 rx_link[PCH_TX_OBJ_END];
  161. u32 int_enables;
  162. struct net_device *ndev;
  163. struct pch_can_regs __iomem *regs;
  164. struct napi_struct napi;
  165. int tx_obj; /* Point next Tx Obj index */
  166. int use_msi;
  167. };
  168. static struct can_bittiming_const pch_can_bittiming_const = {
  169. .name = KBUILD_MODNAME,
  170. .tseg1_min = 2,
  171. .tseg1_max = 16,
  172. .tseg2_min = 1,
  173. .tseg2_max = 8,
  174. .sjw_max = 4,
  175. .brp_min = 1,
  176. .brp_max = 1024, /* 6bit + extended 4bit */
  177. .brp_inc = 1,
  178. };
  179. static DEFINE_PCI_DEVICE_TABLE(pch_pci_tbl) = {
  180. {PCI_VENDOR_ID_INTEL, 0x8818, PCI_ANY_ID, PCI_ANY_ID,},
  181. {0,}
  182. };
  183. MODULE_DEVICE_TABLE(pci, pch_pci_tbl);
  184. static inline void pch_can_bit_set(void __iomem *addr, u32 mask)
  185. {
  186. iowrite32(ioread32(addr) | mask, addr);
  187. }
  188. static inline void pch_can_bit_clear(void __iomem *addr, u32 mask)
  189. {
  190. iowrite32(ioread32(addr) & ~mask, addr);
  191. }
  192. static void pch_can_set_run_mode(struct pch_can_priv *priv,
  193. enum pch_can_mode mode)
  194. {
  195. switch (mode) {
  196. case PCH_CAN_RUN:
  197. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_INIT);
  198. break;
  199. case PCH_CAN_STOP:
  200. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_INIT);
  201. break;
  202. default:
  203. netdev_err(priv->ndev, "%s -> Invalid Mode.\n", __func__);
  204. break;
  205. }
  206. }
  207. static void pch_can_set_optmode(struct pch_can_priv *priv)
  208. {
  209. u32 reg_val = ioread32(&priv->regs->opt);
  210. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  211. reg_val |= PCH_OPT_SILENT;
  212. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  213. reg_val |= PCH_OPT_LBACK;
  214. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_OPT);
  215. iowrite32(reg_val, &priv->regs->opt);
  216. }
  217. static void pch_can_rw_msg_obj(void __iomem *creq_addr, u32 num)
  218. {
  219. int counter = PCH_COUNTER_LIMIT;
  220. u32 ifx_creq;
  221. iowrite32(num, creq_addr);
  222. while (counter) {
  223. ifx_creq = ioread32(creq_addr) & PCH_IF_CREQ_BUSY;
  224. if (!ifx_creq)
  225. break;
  226. counter--;
  227. udelay(1);
  228. }
  229. if (!counter)
  230. pr_err("%s:IF1 BUSY Flag is set forever.\n", __func__);
  231. }
  232. static void pch_can_set_int_enables(struct pch_can_priv *priv,
  233. enum pch_can_mode interrupt_no)
  234. {
  235. switch (interrupt_no) {
  236. case PCH_CAN_DISABLE:
  237. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE);
  238. break;
  239. case PCH_CAN_ALL:
  240. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE);
  241. break;
  242. case PCH_CAN_NONE:
  243. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE);
  244. break;
  245. default:
  246. netdev_err(priv->ndev, "Invalid interrupt number.\n");
  247. break;
  248. }
  249. }
  250. static void pch_can_set_rxtx(struct pch_can_priv *priv, u32 buff_num,
  251. int set, enum pch_ifreg dir)
  252. {
  253. u32 ie;
  254. if (dir)
  255. ie = PCH_IF_MCONT_TXIE;
  256. else
  257. ie = PCH_IF_MCONT_RXIE;
  258. /* Reading the Msg buffer from Message RAM to IF1/2 registers. */
  259. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[dir].cmask);
  260. pch_can_rw_msg_obj(&priv->regs->ifregs[dir].creq, buff_num);
  261. /* Setting the IF1/2MASK1 register to access MsgVal and RxIE bits */
  262. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_ARB | PCH_CMASK_CTRL,
  263. &priv->regs->ifregs[dir].cmask);
  264. if (set) {
  265. /* Setting the MsgVal and RxIE/TxIE bits */
  266. pch_can_bit_set(&priv->regs->ifregs[dir].mcont, ie);
  267. pch_can_bit_set(&priv->regs->ifregs[dir].id2, PCH_ID_MSGVAL);
  268. } else {
  269. /* Clearing the MsgVal and RxIE/TxIE bits */
  270. pch_can_bit_clear(&priv->regs->ifregs[dir].mcont, ie);
  271. pch_can_bit_clear(&priv->regs->ifregs[dir].id2, PCH_ID_MSGVAL);
  272. }
  273. pch_can_rw_msg_obj(&priv->regs->ifregs[dir].creq, buff_num);
  274. }
  275. static void pch_can_set_rx_all(struct pch_can_priv *priv, int set)
  276. {
  277. int i;
  278. /* Traversing to obtain the object configured as receivers. */
  279. for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++)
  280. pch_can_set_rxtx(priv, i, set, PCH_RX_IFREG);
  281. }
  282. static void pch_can_set_tx_all(struct pch_can_priv *priv, int set)
  283. {
  284. int i;
  285. /* Traversing to obtain the object configured as transmit object. */
  286. for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++)
  287. pch_can_set_rxtx(priv, i, set, PCH_TX_IFREG);
  288. }
  289. static u32 pch_can_int_pending(struct pch_can_priv *priv)
  290. {
  291. return ioread32(&priv->regs->intr) & 0xffff;
  292. }
  293. static void pch_can_clear_if_buffers(struct pch_can_priv *priv)
  294. {
  295. int i; /* Msg Obj ID (1~32) */
  296. for (i = PCH_RX_OBJ_START; i <= PCH_TX_OBJ_END; i++) {
  297. iowrite32(PCH_CMASK_RX_TX_SET, &priv->regs->ifregs[0].cmask);
  298. iowrite32(0xffff, &priv->regs->ifregs[0].mask1);
  299. iowrite32(0xffff, &priv->regs->ifregs[0].mask2);
  300. iowrite32(0x0, &priv->regs->ifregs[0].id1);
  301. iowrite32(0x0, &priv->regs->ifregs[0].id2);
  302. iowrite32(0x0, &priv->regs->ifregs[0].mcont);
  303. iowrite32(0x0, &priv->regs->ifregs[0].data[0]);
  304. iowrite32(0x0, &priv->regs->ifregs[0].data[1]);
  305. iowrite32(0x0, &priv->regs->ifregs[0].data[2]);
  306. iowrite32(0x0, &priv->regs->ifregs[0].data[3]);
  307. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK |
  308. PCH_CMASK_ARB | PCH_CMASK_CTRL,
  309. &priv->regs->ifregs[0].cmask);
  310. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, i);
  311. }
  312. }
  313. static void pch_can_config_rx_tx_buffers(struct pch_can_priv *priv)
  314. {
  315. int i;
  316. for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) {
  317. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask);
  318. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, i);
  319. iowrite32(0x0, &priv->regs->ifregs[0].id1);
  320. iowrite32(0x0, &priv->regs->ifregs[0].id2);
  321. pch_can_bit_set(&priv->regs->ifregs[0].mcont,
  322. PCH_IF_MCONT_UMASK);
  323. /* In case FIFO mode, Last EoB of Rx Obj must be 1 */
  324. if (i == PCH_RX_OBJ_END)
  325. pch_can_bit_set(&priv->regs->ifregs[0].mcont,
  326. PCH_IF_MCONT_EOB);
  327. else
  328. pch_can_bit_clear(&priv->regs->ifregs[0].mcont,
  329. PCH_IF_MCONT_EOB);
  330. iowrite32(0, &priv->regs->ifregs[0].mask1);
  331. pch_can_bit_clear(&priv->regs->ifregs[0].mask2,
  332. 0x1fff | PCH_MASK2_MDIR_MXTD);
  333. /* Setting CMASK for writing */
  334. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK | PCH_CMASK_ARB |
  335. PCH_CMASK_CTRL, &priv->regs->ifregs[0].cmask);
  336. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, i);
  337. }
  338. for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++) {
  339. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[1].cmask);
  340. pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, i);
  341. /* Resetting DIR bit for reception */
  342. iowrite32(0x0, &priv->regs->ifregs[1].id1);
  343. iowrite32(PCH_ID2_DIR, &priv->regs->ifregs[1].id2);
  344. /* Setting EOB bit for transmitter */
  345. iowrite32(PCH_IF_MCONT_EOB | PCH_IF_MCONT_UMASK,
  346. &priv->regs->ifregs[1].mcont);
  347. iowrite32(0, &priv->regs->ifregs[1].mask1);
  348. pch_can_bit_clear(&priv->regs->ifregs[1].mask2, 0x1fff);
  349. /* Setting CMASK for writing */
  350. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK | PCH_CMASK_ARB |
  351. PCH_CMASK_CTRL, &priv->regs->ifregs[1].cmask);
  352. pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, i);
  353. }
  354. }
  355. static void pch_can_init(struct pch_can_priv *priv)
  356. {
  357. /* Stopping the Can device. */
  358. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  359. /* Clearing all the message object buffers. */
  360. pch_can_clear_if_buffers(priv);
  361. /* Configuring the respective message object as either rx/tx object. */
  362. pch_can_config_rx_tx_buffers(priv);
  363. /* Enabling the interrupts. */
  364. pch_can_set_int_enables(priv, PCH_CAN_ALL);
  365. }
  366. static void pch_can_release(struct pch_can_priv *priv)
  367. {
  368. /* Stooping the CAN device. */
  369. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  370. /* Disabling the interrupts. */
  371. pch_can_set_int_enables(priv, PCH_CAN_NONE);
  372. /* Disabling all the receive object. */
  373. pch_can_set_rx_all(priv, 0);
  374. /* Disabling all the transmit object. */
  375. pch_can_set_tx_all(priv, 0);
  376. }
  377. /* This function clears interrupt(s) from the CAN device. */
  378. static void pch_can_int_clr(struct pch_can_priv *priv, u32 mask)
  379. {
  380. /* Clear interrupt for transmit object */
  381. if ((mask >= PCH_RX_OBJ_START) && (mask <= PCH_RX_OBJ_END)) {
  382. /* Setting CMASK for clearing the reception interrupts. */
  383. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | PCH_CMASK_ARB,
  384. &priv->regs->ifregs[0].cmask);
  385. /* Clearing the Dir bit. */
  386. pch_can_bit_clear(&priv->regs->ifregs[0].id2, PCH_ID2_DIR);
  387. /* Clearing NewDat & IntPnd */
  388. pch_can_bit_clear(&priv->regs->ifregs[0].mcont,
  389. PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND);
  390. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, mask);
  391. } else if ((mask >= PCH_TX_OBJ_START) && (mask <= PCH_TX_OBJ_END)) {
  392. /*
  393. * Setting CMASK for clearing interrupts for frame transmission.
  394. */
  395. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | PCH_CMASK_ARB,
  396. &priv->regs->ifregs[1].cmask);
  397. /* Resetting the ID registers. */
  398. pch_can_bit_set(&priv->regs->ifregs[1].id2,
  399. PCH_ID2_DIR | (0x7ff << 2));
  400. iowrite32(0x0, &priv->regs->ifregs[1].id1);
  401. /* Claring NewDat, TxRqst & IntPnd */
  402. pch_can_bit_clear(&priv->regs->ifregs[1].mcont,
  403. PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND |
  404. PCH_IF_MCONT_TXRQXT);
  405. pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, mask);
  406. }
  407. }
  408. static void pch_can_reset(struct pch_can_priv *priv)
  409. {
  410. /* write to sw reset register */
  411. iowrite32(1, &priv->regs->srst);
  412. iowrite32(0, &priv->regs->srst);
  413. }
  414. static void pch_can_error(struct net_device *ndev, u32 status)
  415. {
  416. struct sk_buff *skb;
  417. struct pch_can_priv *priv = netdev_priv(ndev);
  418. struct can_frame *cf;
  419. u32 errc, lec;
  420. struct net_device_stats *stats = &(priv->ndev->stats);
  421. enum can_state state = priv->can.state;
  422. skb = alloc_can_err_skb(ndev, &cf);
  423. if (!skb)
  424. return;
  425. if (status & PCH_BUS_OFF) {
  426. pch_can_set_tx_all(priv, 0);
  427. pch_can_set_rx_all(priv, 0);
  428. state = CAN_STATE_BUS_OFF;
  429. cf->can_id |= CAN_ERR_BUSOFF;
  430. can_bus_off(ndev);
  431. }
  432. errc = ioread32(&priv->regs->errc);
  433. /* Warning interrupt. */
  434. if (status & PCH_EWARN) {
  435. state = CAN_STATE_ERROR_WARNING;
  436. priv->can.can_stats.error_warning++;
  437. cf->can_id |= CAN_ERR_CRTL;
  438. if (((errc & PCH_REC) >> 8) > 96)
  439. cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
  440. if ((errc & PCH_TEC) > 96)
  441. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
  442. netdev_dbg(ndev,
  443. "%s -> Error Counter is more than 96.\n", __func__);
  444. }
  445. /* Error passive interrupt. */
  446. if (status & PCH_EPASSIV) {
  447. priv->can.can_stats.error_passive++;
  448. state = CAN_STATE_ERROR_PASSIVE;
  449. cf->can_id |= CAN_ERR_CRTL;
  450. if (((errc & PCH_REC) >> 8) > 127)
  451. cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  452. if ((errc & PCH_TEC) > 127)
  453. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  454. netdev_dbg(ndev,
  455. "%s -> CAN controller is ERROR PASSIVE .\n", __func__);
  456. }
  457. lec = status & PCH_LEC_ALL;
  458. switch (lec) {
  459. case PCH_STUF_ERR:
  460. cf->data[2] |= CAN_ERR_PROT_STUFF;
  461. priv->can.can_stats.bus_error++;
  462. stats->rx_errors++;
  463. break;
  464. case PCH_FORM_ERR:
  465. cf->data[2] |= CAN_ERR_PROT_FORM;
  466. priv->can.can_stats.bus_error++;
  467. stats->rx_errors++;
  468. break;
  469. case PCH_ACK_ERR:
  470. cf->can_id |= CAN_ERR_ACK;
  471. priv->can.can_stats.bus_error++;
  472. stats->rx_errors++;
  473. break;
  474. case PCH_BIT1_ERR:
  475. case PCH_BIT0_ERR:
  476. cf->data[2] |= CAN_ERR_PROT_BIT;
  477. priv->can.can_stats.bus_error++;
  478. stats->rx_errors++;
  479. break;
  480. case PCH_CRC_ERR:
  481. cf->data[2] |= CAN_ERR_PROT_LOC_CRC_SEQ |
  482. CAN_ERR_PROT_LOC_CRC_DEL;
  483. priv->can.can_stats.bus_error++;
  484. stats->rx_errors++;
  485. break;
  486. case PCH_LEC_ALL: /* Written by CPU. No error status */
  487. break;
  488. }
  489. cf->data[6] = errc & PCH_TEC;
  490. cf->data[7] = (errc & PCH_REC) >> 8;
  491. priv->can.state = state;
  492. netif_receive_skb(skb);
  493. stats->rx_packets++;
  494. stats->rx_bytes += cf->can_dlc;
  495. }
  496. static irqreturn_t pch_can_interrupt(int irq, void *dev_id)
  497. {
  498. struct net_device *ndev = (struct net_device *)dev_id;
  499. struct pch_can_priv *priv = netdev_priv(ndev);
  500. if (!pch_can_int_pending(priv))
  501. return IRQ_NONE;
  502. pch_can_set_int_enables(priv, PCH_CAN_NONE);
  503. napi_schedule(&priv->napi);
  504. return IRQ_HANDLED;
  505. }
  506. static void pch_fifo_thresh(struct pch_can_priv *priv, int obj_id)
  507. {
  508. if (obj_id < PCH_FIFO_THRESH) {
  509. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL |
  510. PCH_CMASK_ARB, &priv->regs->ifregs[0].cmask);
  511. /* Clearing the Dir bit. */
  512. pch_can_bit_clear(&priv->regs->ifregs[0].id2, PCH_ID2_DIR);
  513. /* Clearing NewDat & IntPnd */
  514. pch_can_bit_clear(&priv->regs->ifregs[0].mcont,
  515. PCH_IF_MCONT_INTPND);
  516. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, obj_id);
  517. } else if (obj_id > PCH_FIFO_THRESH) {
  518. pch_can_int_clr(priv, obj_id);
  519. } else if (obj_id == PCH_FIFO_THRESH) {
  520. int cnt;
  521. for (cnt = 0; cnt < PCH_FIFO_THRESH; cnt++)
  522. pch_can_int_clr(priv, cnt + 1);
  523. }
  524. }
  525. static void pch_can_rx_msg_lost(struct net_device *ndev, int obj_id)
  526. {
  527. struct pch_can_priv *priv = netdev_priv(ndev);
  528. struct net_device_stats *stats = &(priv->ndev->stats);
  529. struct sk_buff *skb;
  530. struct can_frame *cf;
  531. netdev_dbg(priv->ndev, "Msg Obj is overwritten.\n");
  532. pch_can_bit_clear(&priv->regs->ifregs[0].mcont,
  533. PCH_IF_MCONT_MSGLOST);
  534. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL,
  535. &priv->regs->ifregs[0].cmask);
  536. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, obj_id);
  537. skb = alloc_can_err_skb(ndev, &cf);
  538. if (!skb)
  539. return;
  540. cf->can_id |= CAN_ERR_CRTL;
  541. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  542. stats->rx_over_errors++;
  543. stats->rx_errors++;
  544. netif_receive_skb(skb);
  545. }
  546. static int pch_can_rx_normal(struct net_device *ndev, u32 obj_num, int quota)
  547. {
  548. u32 reg;
  549. canid_t id;
  550. int rcv_pkts = 0;
  551. struct sk_buff *skb;
  552. struct can_frame *cf;
  553. struct pch_can_priv *priv = netdev_priv(ndev);
  554. struct net_device_stats *stats = &(priv->ndev->stats);
  555. int i;
  556. u32 id2;
  557. u16 data_reg;
  558. do {
  559. /* Reading the message object from the Message RAM */
  560. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask);
  561. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, obj_num);
  562. /* Reading the MCONT register. */
  563. reg = ioread32(&priv->regs->ifregs[0].mcont);
  564. if (reg & PCH_IF_MCONT_EOB)
  565. break;
  566. /* If MsgLost bit set. */
  567. if (reg & PCH_IF_MCONT_MSGLOST) {
  568. pch_can_rx_msg_lost(ndev, obj_num);
  569. rcv_pkts++;
  570. quota--;
  571. obj_num++;
  572. continue;
  573. } else if (!(reg & PCH_IF_MCONT_NEWDAT)) {
  574. obj_num++;
  575. continue;
  576. }
  577. skb = alloc_can_skb(priv->ndev, &cf);
  578. if (!skb) {
  579. netdev_err(ndev, "alloc_can_skb Failed\n");
  580. return rcv_pkts;
  581. }
  582. /* Get Received data */
  583. id2 = ioread32(&priv->regs->ifregs[0].id2);
  584. if (id2 & PCH_ID2_XTD) {
  585. id = (ioread32(&priv->regs->ifregs[0].id1) & 0xffff);
  586. id |= (((id2) & 0x1fff) << 16);
  587. cf->can_id = id | CAN_EFF_FLAG;
  588. } else {
  589. id = (id2 >> 2) & CAN_SFF_MASK;
  590. cf->can_id = id;
  591. }
  592. if (id2 & PCH_ID2_DIR)
  593. cf->can_id |= CAN_RTR_FLAG;
  594. cf->can_dlc = get_can_dlc((ioread32(&priv->regs->
  595. ifregs[0].mcont)) & 0xF);
  596. for (i = 0; i < cf->can_dlc; i += 2) {
  597. data_reg = ioread16(&priv->regs->ifregs[0].data[i / 2]);
  598. cf->data[i] = data_reg;
  599. cf->data[i + 1] = data_reg >> 8;
  600. }
  601. netif_receive_skb(skb);
  602. rcv_pkts++;
  603. stats->rx_packets++;
  604. quota--;
  605. stats->rx_bytes += cf->can_dlc;
  606. pch_fifo_thresh(priv, obj_num);
  607. obj_num++;
  608. } while (quota > 0);
  609. return rcv_pkts;
  610. }
  611. static void pch_can_tx_complete(struct net_device *ndev, u32 int_stat)
  612. {
  613. struct pch_can_priv *priv = netdev_priv(ndev);
  614. struct net_device_stats *stats = &(priv->ndev->stats);
  615. u32 dlc;
  616. can_get_echo_skb(ndev, int_stat - PCH_RX_OBJ_END - 1);
  617. iowrite32(PCH_CMASK_RX_TX_GET | PCH_CMASK_CLRINTPND,
  618. &priv->regs->ifregs[1].cmask);
  619. pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, int_stat);
  620. dlc = get_can_dlc(ioread32(&priv->regs->ifregs[1].mcont) &
  621. PCH_IF_MCONT_DLC);
  622. stats->tx_bytes += dlc;
  623. stats->tx_packets++;
  624. if (int_stat == PCH_TX_OBJ_END)
  625. netif_wake_queue(ndev);
  626. }
  627. static int pch_can_poll(struct napi_struct *napi, int quota)
  628. {
  629. struct net_device *ndev = napi->dev;
  630. struct pch_can_priv *priv = netdev_priv(ndev);
  631. u32 int_stat;
  632. u32 reg_stat;
  633. int quota_save = quota;
  634. int_stat = pch_can_int_pending(priv);
  635. if (!int_stat)
  636. goto end;
  637. if (int_stat == PCH_STATUS_INT) {
  638. reg_stat = ioread32(&priv->regs->stat);
  639. if ((reg_stat & (PCH_BUS_OFF | PCH_LEC_ALL)) &&
  640. ((reg_stat & PCH_LEC_ALL) != PCH_LEC_ALL)) {
  641. pch_can_error(ndev, reg_stat);
  642. quota--;
  643. }
  644. if (reg_stat & (PCH_TX_OK | PCH_RX_OK))
  645. pch_can_bit_clear(&priv->regs->stat,
  646. reg_stat & (PCH_TX_OK | PCH_RX_OK));
  647. int_stat = pch_can_int_pending(priv);
  648. }
  649. if (quota == 0)
  650. goto end;
  651. if ((int_stat >= PCH_RX_OBJ_START) && (int_stat <= PCH_RX_OBJ_END)) {
  652. quota -= pch_can_rx_normal(ndev, int_stat, quota);
  653. } else if ((int_stat >= PCH_TX_OBJ_START) &&
  654. (int_stat <= PCH_TX_OBJ_END)) {
  655. /* Handle transmission interrupt */
  656. pch_can_tx_complete(ndev, int_stat);
  657. }
  658. end:
  659. napi_complete(napi);
  660. pch_can_set_int_enables(priv, PCH_CAN_ALL);
  661. return quota_save - quota;
  662. }
  663. static int pch_set_bittiming(struct net_device *ndev)
  664. {
  665. struct pch_can_priv *priv = netdev_priv(ndev);
  666. const struct can_bittiming *bt = &priv->can.bittiming;
  667. u32 canbit;
  668. u32 bepe;
  669. /* Setting the CCE bit for accessing the Can Timing register. */
  670. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_CCE);
  671. canbit = (bt->brp - 1) & PCH_MSK_BITT_BRP;
  672. canbit |= (bt->sjw - 1) << PCH_BIT_SJW_SHIFT;
  673. canbit |= (bt->phase_seg1 + bt->prop_seg - 1) << PCH_BIT_TSEG1_SHIFT;
  674. canbit |= (bt->phase_seg2 - 1) << PCH_BIT_TSEG2_SHIFT;
  675. bepe = ((bt->brp - 1) & PCH_MSK_BRPE_BRPE) >> PCH_BIT_BRPE_BRPE_SHIFT;
  676. iowrite32(canbit, &priv->regs->bitt);
  677. iowrite32(bepe, &priv->regs->brpe);
  678. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_CCE);
  679. return 0;
  680. }
  681. static void pch_can_start(struct net_device *ndev)
  682. {
  683. struct pch_can_priv *priv = netdev_priv(ndev);
  684. if (priv->can.state != CAN_STATE_STOPPED)
  685. pch_can_reset(priv);
  686. pch_set_bittiming(ndev);
  687. pch_can_set_optmode(priv);
  688. pch_can_set_tx_all(priv, 1);
  689. pch_can_set_rx_all(priv, 1);
  690. /* Setting the CAN to run mode. */
  691. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  692. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  693. return;
  694. }
  695. static int pch_can_do_set_mode(struct net_device *ndev, enum can_mode mode)
  696. {
  697. int ret = 0;
  698. switch (mode) {
  699. case CAN_MODE_START:
  700. pch_can_start(ndev);
  701. netif_wake_queue(ndev);
  702. break;
  703. default:
  704. ret = -EOPNOTSUPP;
  705. break;
  706. }
  707. return ret;
  708. }
  709. static int pch_can_open(struct net_device *ndev)
  710. {
  711. struct pch_can_priv *priv = netdev_priv(ndev);
  712. int retval;
  713. /* Regstering the interrupt. */
  714. retval = request_irq(priv->dev->irq, pch_can_interrupt, IRQF_SHARED,
  715. ndev->name, ndev);
  716. if (retval) {
  717. netdev_err(ndev, "request_irq failed.\n");
  718. goto req_irq_err;
  719. }
  720. /* Open common can device */
  721. retval = open_candev(ndev);
  722. if (retval) {
  723. netdev_err(ndev, "open_candev() failed %d\n", retval);
  724. goto err_open_candev;
  725. }
  726. pch_can_init(priv);
  727. pch_can_start(ndev);
  728. napi_enable(&priv->napi);
  729. netif_start_queue(ndev);
  730. return 0;
  731. err_open_candev:
  732. free_irq(priv->dev->irq, ndev);
  733. req_irq_err:
  734. pch_can_release(priv);
  735. return retval;
  736. }
  737. static int pch_close(struct net_device *ndev)
  738. {
  739. struct pch_can_priv *priv = netdev_priv(ndev);
  740. netif_stop_queue(ndev);
  741. napi_disable(&priv->napi);
  742. pch_can_release(priv);
  743. free_irq(priv->dev->irq, ndev);
  744. close_candev(ndev);
  745. priv->can.state = CAN_STATE_STOPPED;
  746. return 0;
  747. }
  748. static netdev_tx_t pch_xmit(struct sk_buff *skb, struct net_device *ndev)
  749. {
  750. struct pch_can_priv *priv = netdev_priv(ndev);
  751. struct can_frame *cf = (struct can_frame *)skb->data;
  752. int tx_obj_no;
  753. int i;
  754. u32 id2;
  755. if (can_dropped_invalid_skb(ndev, skb))
  756. return NETDEV_TX_OK;
  757. tx_obj_no = priv->tx_obj;
  758. if (priv->tx_obj == PCH_TX_OBJ_END) {
  759. if (ioread32(&priv->regs->treq2) & PCH_TREQ2_TX_MASK)
  760. netif_stop_queue(ndev);
  761. priv->tx_obj = PCH_TX_OBJ_START;
  762. } else {
  763. priv->tx_obj++;
  764. }
  765. /* Setting the CMASK register. */
  766. pch_can_bit_set(&priv->regs->ifregs[1].cmask, PCH_CMASK_ALL);
  767. /* If ID extended is set. */
  768. if (cf->can_id & CAN_EFF_FLAG) {
  769. iowrite32(cf->can_id & 0xffff, &priv->regs->ifregs[1].id1);
  770. id2 = ((cf->can_id >> 16) & 0x1fff) | PCH_ID2_XTD;
  771. } else {
  772. iowrite32(0, &priv->regs->ifregs[1].id1);
  773. id2 = (cf->can_id & CAN_SFF_MASK) << 2;
  774. }
  775. id2 |= PCH_ID_MSGVAL;
  776. /* If remote frame has to be transmitted.. */
  777. if (!(cf->can_id & CAN_RTR_FLAG))
  778. id2 |= PCH_ID2_DIR;
  779. iowrite32(id2, &priv->regs->ifregs[1].id2);
  780. /* Copy data to register */
  781. for (i = 0; i < cf->can_dlc; i += 2) {
  782. iowrite16(cf->data[i] | (cf->data[i + 1] << 8),
  783. &priv->regs->ifregs[1].data[i / 2]);
  784. }
  785. can_put_echo_skb(skb, ndev, tx_obj_no - PCH_RX_OBJ_END - 1);
  786. /* Set the size of the data. Update if2_mcont */
  787. iowrite32(cf->can_dlc | PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_TXRQXT |
  788. PCH_IF_MCONT_TXIE, &priv->regs->ifregs[1].mcont);
  789. pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, tx_obj_no);
  790. return NETDEV_TX_OK;
  791. }
  792. static const struct net_device_ops pch_can_netdev_ops = {
  793. .ndo_open = pch_can_open,
  794. .ndo_stop = pch_close,
  795. .ndo_start_xmit = pch_xmit,
  796. };
  797. static void __devexit pch_can_remove(struct pci_dev *pdev)
  798. {
  799. struct net_device *ndev = pci_get_drvdata(pdev);
  800. struct pch_can_priv *priv = netdev_priv(ndev);
  801. unregister_candev(priv->ndev);
  802. if (priv->use_msi)
  803. pci_disable_msi(priv->dev);
  804. pci_release_regions(pdev);
  805. pci_disable_device(pdev);
  806. pci_set_drvdata(pdev, NULL);
  807. pch_can_reset(priv);
  808. pci_iounmap(pdev, priv->regs);
  809. free_candev(priv->ndev);
  810. }
  811. #ifdef CONFIG_PM
  812. static void pch_can_set_int_custom(struct pch_can_priv *priv)
  813. {
  814. /* Clearing the IE, SIE and EIE bits of Can control register. */
  815. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE);
  816. /* Appropriately setting them. */
  817. pch_can_bit_set(&priv->regs->cont,
  818. ((priv->int_enables & PCH_MSK_CTRL_IE_SIE_EIE) << 1));
  819. }
  820. /* This function retrieves interrupt enabled for the CAN device. */
  821. static u32 pch_can_get_int_enables(struct pch_can_priv *priv)
  822. {
  823. /* Obtaining the status of IE, SIE and EIE interrupt bits. */
  824. return (ioread32(&priv->regs->cont) & PCH_CTRL_IE_SIE_EIE) >> 1;
  825. }
  826. static u32 pch_can_get_rxtx_ir(struct pch_can_priv *priv, u32 buff_num,
  827. enum pch_ifreg dir)
  828. {
  829. u32 ie, enable;
  830. if (dir)
  831. ie = PCH_IF_MCONT_RXIE;
  832. else
  833. ie = PCH_IF_MCONT_TXIE;
  834. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[dir].cmask);
  835. pch_can_rw_msg_obj(&priv->regs->ifregs[dir].creq, buff_num);
  836. if (((ioread32(&priv->regs->ifregs[dir].id2)) & PCH_ID_MSGVAL) &&
  837. ((ioread32(&priv->regs->ifregs[dir].mcont)) & ie))
  838. enable = 1;
  839. else
  840. enable = 0;
  841. return enable;
  842. }
  843. static void pch_can_set_rx_buffer_link(struct pch_can_priv *priv,
  844. u32 buffer_num, int set)
  845. {
  846. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask);
  847. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, buffer_num);
  848. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL,
  849. &priv->regs->ifregs[0].cmask);
  850. if (set)
  851. pch_can_bit_clear(&priv->regs->ifregs[0].mcont,
  852. PCH_IF_MCONT_EOB);
  853. else
  854. pch_can_bit_set(&priv->regs->ifregs[0].mcont, PCH_IF_MCONT_EOB);
  855. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, buffer_num);
  856. }
  857. static u32 pch_can_get_rx_buffer_link(struct pch_can_priv *priv, u32 buffer_num)
  858. {
  859. u32 link;
  860. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask);
  861. pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, buffer_num);
  862. if (ioread32(&priv->regs->ifregs[0].mcont) & PCH_IF_MCONT_EOB)
  863. link = 0;
  864. else
  865. link = 1;
  866. return link;
  867. }
  868. static int pch_can_get_buffer_status(struct pch_can_priv *priv)
  869. {
  870. return (ioread32(&priv->regs->treq1) & 0xffff) |
  871. (ioread32(&priv->regs->treq2) << 16);
  872. }
  873. static int pch_can_suspend(struct pci_dev *pdev, pm_message_t state)
  874. {
  875. int i;
  876. int retval;
  877. u32 buf_stat; /* Variable for reading the transmit buffer status. */
  878. int counter = PCH_COUNTER_LIMIT;
  879. struct net_device *dev = pci_get_drvdata(pdev);
  880. struct pch_can_priv *priv = netdev_priv(dev);
  881. /* Stop the CAN controller */
  882. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  883. /* Indicate that we are aboutto/in suspend */
  884. priv->can.state = CAN_STATE_STOPPED;
  885. /* Waiting for all transmission to complete. */
  886. while (counter) {
  887. buf_stat = pch_can_get_buffer_status(priv);
  888. if (!buf_stat)
  889. break;
  890. counter--;
  891. udelay(1);
  892. }
  893. if (!counter)
  894. dev_err(&pdev->dev, "%s -> Transmission time out.\n", __func__);
  895. /* Save interrupt configuration and then disable them */
  896. priv->int_enables = pch_can_get_int_enables(priv);
  897. pch_can_set_int_enables(priv, PCH_CAN_DISABLE);
  898. /* Save Tx buffer enable state */
  899. for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++)
  900. priv->tx_enable[i - 1] = pch_can_get_rxtx_ir(priv, i,
  901. PCH_TX_IFREG);
  902. /* Disable all Transmit buffers */
  903. pch_can_set_tx_all(priv, 0);
  904. /* Save Rx buffer enable state */
  905. for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) {
  906. priv->rx_enable[i - 1] = pch_can_get_rxtx_ir(priv, i,
  907. PCH_RX_IFREG);
  908. priv->rx_link[i - 1] = pch_can_get_rx_buffer_link(priv, i);
  909. }
  910. /* Disable all Receive buffers */
  911. pch_can_set_rx_all(priv, 0);
  912. retval = pci_save_state(pdev);
  913. if (retval) {
  914. dev_err(&pdev->dev, "pci_save_state failed.\n");
  915. } else {
  916. pci_enable_wake(pdev, PCI_D3hot, 0);
  917. pci_disable_device(pdev);
  918. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  919. }
  920. return retval;
  921. }
  922. static int pch_can_resume(struct pci_dev *pdev)
  923. {
  924. int i;
  925. int retval;
  926. struct net_device *dev = pci_get_drvdata(pdev);
  927. struct pch_can_priv *priv = netdev_priv(dev);
  928. pci_set_power_state(pdev, PCI_D0);
  929. pci_restore_state(pdev);
  930. retval = pci_enable_device(pdev);
  931. if (retval) {
  932. dev_err(&pdev->dev, "pci_enable_device failed.\n");
  933. return retval;
  934. }
  935. pci_enable_wake(pdev, PCI_D3hot, 0);
  936. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  937. /* Disabling all interrupts. */
  938. pch_can_set_int_enables(priv, PCH_CAN_DISABLE);
  939. /* Setting the CAN device in Stop Mode. */
  940. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  941. /* Configuring the transmit and receive buffers. */
  942. pch_can_config_rx_tx_buffers(priv);
  943. /* Restore the CAN state */
  944. pch_set_bittiming(dev);
  945. /* Listen/Active */
  946. pch_can_set_optmode(priv);
  947. /* Enabling the transmit buffer. */
  948. for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++)
  949. pch_can_set_rxtx(priv, i, priv->tx_enable[i - 1], PCH_TX_IFREG);
  950. /* Configuring the receive buffer and enabling them. */
  951. for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) {
  952. /* Restore buffer link */
  953. pch_can_set_rx_buffer_link(priv, i, priv->rx_link[i - 1]);
  954. /* Restore buffer enables */
  955. pch_can_set_rxtx(priv, i, priv->rx_enable[i - 1], PCH_RX_IFREG);
  956. }
  957. /* Enable CAN Interrupts */
  958. pch_can_set_int_custom(priv);
  959. /* Restore Run Mode */
  960. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  961. return retval;
  962. }
  963. #else
  964. #define pch_can_suspend NULL
  965. #define pch_can_resume NULL
  966. #endif
  967. static int pch_can_get_berr_counter(const struct net_device *dev,
  968. struct can_berr_counter *bec)
  969. {
  970. struct pch_can_priv *priv = netdev_priv(dev);
  971. u32 errc = ioread32(&priv->regs->errc);
  972. bec->txerr = errc & PCH_TEC;
  973. bec->rxerr = (errc & PCH_REC) >> 8;
  974. return 0;
  975. }
  976. static int __devinit pch_can_probe(struct pci_dev *pdev,
  977. const struct pci_device_id *id)
  978. {
  979. struct net_device *ndev;
  980. struct pch_can_priv *priv;
  981. int rc;
  982. void __iomem *addr;
  983. rc = pci_enable_device(pdev);
  984. if (rc) {
  985. dev_err(&pdev->dev, "Failed pci_enable_device %d\n", rc);
  986. goto probe_exit_endev;
  987. }
  988. rc = pci_request_regions(pdev, KBUILD_MODNAME);
  989. if (rc) {
  990. dev_err(&pdev->dev, "Failed pci_request_regions %d\n", rc);
  991. goto probe_exit_pcireq;
  992. }
  993. addr = pci_iomap(pdev, 1, 0);
  994. if (!addr) {
  995. rc = -EIO;
  996. dev_err(&pdev->dev, "Failed pci_iomap\n");
  997. goto probe_exit_ipmap;
  998. }
  999. ndev = alloc_candev(sizeof(struct pch_can_priv), PCH_TX_OBJ_END);
  1000. if (!ndev) {
  1001. rc = -ENOMEM;
  1002. dev_err(&pdev->dev, "Failed alloc_candev\n");
  1003. goto probe_exit_alloc_candev;
  1004. }
  1005. priv = netdev_priv(ndev);
  1006. priv->ndev = ndev;
  1007. priv->regs = addr;
  1008. priv->dev = pdev;
  1009. priv->can.bittiming_const = &pch_can_bittiming_const;
  1010. priv->can.do_set_mode = pch_can_do_set_mode;
  1011. priv->can.do_get_berr_counter = pch_can_get_berr_counter;
  1012. priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
  1013. CAN_CTRLMODE_LOOPBACK;
  1014. priv->tx_obj = PCH_TX_OBJ_START; /* Point head of Tx Obj */
  1015. ndev->irq = pdev->irq;
  1016. ndev->flags |= IFF_ECHO;
  1017. pci_set_drvdata(pdev, ndev);
  1018. SET_NETDEV_DEV(ndev, &pdev->dev);
  1019. ndev->netdev_ops = &pch_can_netdev_ops;
  1020. priv->can.clock.freq = PCH_CAN_CLK; /* Hz */
  1021. netif_napi_add(ndev, &priv->napi, pch_can_poll, PCH_RX_OBJ_END);
  1022. rc = pci_enable_msi(priv->dev);
  1023. if (rc) {
  1024. netdev_err(ndev, "PCH CAN opened without MSI\n");
  1025. priv->use_msi = 0;
  1026. } else {
  1027. netdev_err(ndev, "PCH CAN opened with MSI\n");
  1028. pci_set_master(pdev);
  1029. priv->use_msi = 1;
  1030. }
  1031. rc = register_candev(ndev);
  1032. if (rc) {
  1033. dev_err(&pdev->dev, "Failed register_candev %d\n", rc);
  1034. goto probe_exit_reg_candev;
  1035. }
  1036. return 0;
  1037. probe_exit_reg_candev:
  1038. if (priv->use_msi)
  1039. pci_disable_msi(priv->dev);
  1040. free_candev(ndev);
  1041. probe_exit_alloc_candev:
  1042. pci_iounmap(pdev, addr);
  1043. probe_exit_ipmap:
  1044. pci_release_regions(pdev);
  1045. probe_exit_pcireq:
  1046. pci_disable_device(pdev);
  1047. probe_exit_endev:
  1048. return rc;
  1049. }
  1050. static struct pci_driver pch_can_pci_driver = {
  1051. .name = "pch_can",
  1052. .id_table = pch_pci_tbl,
  1053. .probe = pch_can_probe,
  1054. .remove = __devexit_p(pch_can_remove),
  1055. .suspend = pch_can_suspend,
  1056. .resume = pch_can_resume,
  1057. };
  1058. static int __init pch_can_pci_init(void)
  1059. {
  1060. return pci_register_driver(&pch_can_pci_driver);
  1061. }
  1062. module_init(pch_can_pci_init);
  1063. static void __exit pch_can_pci_exit(void)
  1064. {
  1065. pci_unregister_driver(&pch_can_pci_driver);
  1066. }
  1067. module_exit(pch_can_pci_exit);
  1068. MODULE_DESCRIPTION("Intel EG20T PCH CAN(Controller Area Network) Driver");
  1069. MODULE_LICENSE("GPL v2");
  1070. MODULE_VERSION("0.94");