pch_can.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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_MAX_MSG_OBJ 32
  34. #define PCH_MSG_OBJ_RX 0 /* The receive message object flag. */
  35. #define PCH_MSG_OBJ_TX 1 /* The transmit message object flag. */
  36. #define PCH_ENABLE 1 /* The enable flag */
  37. #define PCH_DISABLE 0 /* The disable flag */
  38. #define PCH_CTRL_INIT 0x0001 /* The INIT bit of CANCONT register. */
  39. #define PCH_CTRL_IE 0x0002 /* The IE bit of CAN control register */
  40. #define PCH_CTRL_IE_SIE_EIE 0x000e
  41. #define PCH_CTRL_CCE 0x0040
  42. #define PCH_CTRL_OPT 0x0080 /* The OPT bit of CANCONT register. */
  43. #define PCH_OPT_SILENT 0x0008 /* The Silent bit of CANOPT reg. */
  44. #define PCH_OPT_LBACK 0x0010 /* The LoopBack bit of CANOPT reg. */
  45. #define PCH_CMASK_RX_TX_SET 0x00f3
  46. #define PCH_CMASK_RX_TX_GET 0x0073
  47. #define PCH_CMASK_ALL 0xff
  48. #define PCH_CMASK_RDWR 0x80
  49. #define PCH_CMASK_ARB 0x20
  50. #define PCH_CMASK_CTRL 0x10
  51. #define PCH_CMASK_MASK 0x40
  52. #define PCH_CMASK_NEWDAT 0x04
  53. #define PCH_CMASK_CLRINTPND 0x08
  54. #define PCH_IF_MCONT_NEWDAT 0x8000
  55. #define PCH_IF_MCONT_INTPND 0x2000
  56. #define PCH_IF_MCONT_UMASK 0x1000
  57. #define PCH_IF_MCONT_TXIE 0x0800
  58. #define PCH_IF_MCONT_RXIE 0x0400
  59. #define PCH_IF_MCONT_RMTEN 0x0200
  60. #define PCH_IF_MCONT_TXRQXT 0x0100
  61. #define PCH_IF_MCONT_EOB 0x0080
  62. #define PCH_IF_MCONT_DLC 0x000f
  63. #define PCH_IF_MCONT_MSGLOST 0x4000
  64. #define PCH_MASK2_MDIR_MXTD 0xc000
  65. #define PCH_ID2_DIR 0x2000
  66. #define PCH_ID2_XTD 0x4000
  67. #define PCH_ID_MSGVAL 0x8000
  68. #define PCH_IF_CREQ_BUSY 0x8000
  69. #define PCH_STATUS_INT 0x8000
  70. #define PCH_REC 0x00007f00
  71. #define PCH_TEC 0x000000ff
  72. #define PCH_RX_OK 0x00000010
  73. #define PCH_TX_OK 0x00000008
  74. #define PCH_BUS_OFF 0x00000080
  75. #define PCH_EWARN 0x00000040
  76. #define PCH_EPASSIV 0x00000020
  77. #define PCH_LEC0 0x00000001
  78. #define PCH_LEC1 0x00000002
  79. #define PCH_LEC2 0x00000004
  80. #define PCH_LEC_ALL (PCH_LEC0 | PCH_LEC1 | PCH_LEC2)
  81. #define PCH_STUF_ERR PCH_LEC0
  82. #define PCH_FORM_ERR PCH_LEC1
  83. #define PCH_ACK_ERR (PCH_LEC0 | PCH_LEC1)
  84. #define PCH_BIT1_ERR PCH_LEC2
  85. #define PCH_BIT0_ERR (PCH_LEC0 | PCH_LEC2)
  86. #define PCH_CRC_ERR (PCH_LEC1 | PCH_LEC2)
  87. /* bit position of certain controller bits. */
  88. #define PCH_BIT_BRP 0
  89. #define PCH_BIT_SJW 6
  90. #define PCH_BIT_TSEG1 8
  91. #define PCH_BIT_TSEG2 12
  92. #define PCH_BIT_BRPE_BRPE 6
  93. #define PCH_MSK_BITT_BRP 0x3f
  94. #define PCH_MSK_BRPE_BRPE 0x3c0
  95. #define PCH_MSK_CTRL_IE_SIE_EIE 0x07
  96. #define PCH_COUNTER_LIMIT 10
  97. #define PCH_CAN_CLK 50000000 /* 50MHz */
  98. /* Define the number of message object.
  99. * PCH CAN communications are done via Message RAM.
  100. * The Message RAM consists of 32 message objects. */
  101. #define PCH_RX_OBJ_NUM 26 /* 1~ PCH_RX_OBJ_NUM is Rx*/
  102. #define PCH_TX_OBJ_NUM 6 /* PCH_RX_OBJ_NUM is RX ~ Tx*/
  103. #define PCH_OBJ_NUM (PCH_TX_OBJ_NUM + PCH_RX_OBJ_NUM)
  104. #define PCH_FIFO_THRESH 16
  105. enum pch_can_mode {
  106. PCH_CAN_ENABLE,
  107. PCH_CAN_DISABLE,
  108. PCH_CAN_ALL,
  109. PCH_CAN_NONE,
  110. PCH_CAN_STOP,
  111. PCH_CAN_RUN
  112. };
  113. struct pch_can_regs {
  114. u32 cont;
  115. u32 stat;
  116. u32 errc;
  117. u32 bitt;
  118. u32 intr;
  119. u32 opt;
  120. u32 brpe;
  121. u32 reserve1;
  122. u32 if1_creq;
  123. u32 if1_cmask;
  124. u32 if1_mask1;
  125. u32 if1_mask2;
  126. u32 if1_id1;
  127. u32 if1_id2;
  128. u32 if1_mcont;
  129. u32 if1_dataa1;
  130. u32 if1_dataa2;
  131. u32 if1_datab1;
  132. u32 if1_datab2;
  133. u32 reserve2;
  134. u32 reserve3[12];
  135. u32 if2_creq;
  136. u32 if2_cmask;
  137. u32 if2_mask1;
  138. u32 if2_mask2;
  139. u32 if2_id1;
  140. u32 if2_id2;
  141. u32 if2_mcont;
  142. u32 if2_dataa1;
  143. u32 if2_dataa2;
  144. u32 if2_datab1;
  145. u32 if2_datab2;
  146. u32 reserve4;
  147. u32 reserve5[20];
  148. u32 treq1;
  149. u32 treq2;
  150. u32 reserve6[2];
  151. u32 reserve7[56];
  152. u32 reserve8[3];
  153. u32 srst;
  154. };
  155. struct pch_can_priv {
  156. struct can_priv can;
  157. unsigned int can_num;
  158. struct pci_dev *dev;
  159. unsigned int tx_enable[PCH_MAX_MSG_OBJ];
  160. unsigned int rx_enable[PCH_MAX_MSG_OBJ];
  161. unsigned int rx_link[PCH_MAX_MSG_OBJ];
  162. unsigned int int_enables;
  163. unsigned int int_stat;
  164. struct net_device *ndev;
  165. spinlock_t msgif_reg_lock; /* Message Interface Registers Access Lock*/
  166. unsigned int msg_obj[PCH_MAX_MSG_OBJ];
  167. struct pch_can_regs __iomem *regs;
  168. struct napi_struct napi;
  169. unsigned int tx_obj; /* Point next Tx Obj index */
  170. unsigned int use_msi;
  171. };
  172. static struct can_bittiming_const pch_can_bittiming_const = {
  173. .name = KBUILD_MODNAME,
  174. .tseg1_min = 1,
  175. .tseg1_max = 16,
  176. .tseg2_min = 1,
  177. .tseg2_max = 8,
  178. .sjw_max = 4,
  179. .brp_min = 1,
  180. .brp_max = 1024, /* 6bit + extended 4bit */
  181. .brp_inc = 1,
  182. };
  183. static DEFINE_PCI_DEVICE_TABLE(pch_pci_tbl) = {
  184. {PCI_VENDOR_ID_INTEL, 0x8818, PCI_ANY_ID, PCI_ANY_ID,},
  185. {0,}
  186. };
  187. MODULE_DEVICE_TABLE(pci, pch_pci_tbl);
  188. static inline void pch_can_bit_set(void __iomem *addr, u32 mask)
  189. {
  190. iowrite32(ioread32(addr) | mask, addr);
  191. }
  192. static inline void pch_can_bit_clear(void __iomem *addr, u32 mask)
  193. {
  194. iowrite32(ioread32(addr) & ~mask, addr);
  195. }
  196. static void pch_can_set_run_mode(struct pch_can_priv *priv,
  197. enum pch_can_mode mode)
  198. {
  199. switch (mode) {
  200. case PCH_CAN_RUN:
  201. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_INIT);
  202. break;
  203. case PCH_CAN_STOP:
  204. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_INIT);
  205. break;
  206. default:
  207. dev_err(&priv->ndev->dev, "%s -> Invalid Mode.\n", __func__);
  208. break;
  209. }
  210. }
  211. static void pch_can_set_optmode(struct pch_can_priv *priv)
  212. {
  213. u32 reg_val = ioread32(&priv->regs->opt);
  214. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  215. reg_val |= PCH_OPT_SILENT;
  216. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  217. reg_val |= PCH_OPT_LBACK;
  218. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_OPT);
  219. iowrite32(reg_val, &priv->regs->opt);
  220. }
  221. static void pch_can_set_int_custom(struct pch_can_priv *priv)
  222. {
  223. /* Clearing the IE, SIE and EIE bits of Can control register. */
  224. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE);
  225. /* Appropriately setting them. */
  226. pch_can_bit_set(&priv->regs->cont,
  227. ((priv->int_enables & PCH_MSK_CTRL_IE_SIE_EIE) << 1));
  228. }
  229. /* This function retrieves interrupt enabled for the CAN device. */
  230. static void pch_can_get_int_enables(struct pch_can_priv *priv, u32 *enables)
  231. {
  232. /* Obtaining the status of IE, SIE and EIE interrupt bits. */
  233. *enables = ((ioread32(&priv->regs->cont) & PCH_CTRL_IE_SIE_EIE) >> 1);
  234. }
  235. static void pch_can_set_int_enables(struct pch_can_priv *priv,
  236. enum pch_can_mode interrupt_no)
  237. {
  238. switch (interrupt_no) {
  239. case PCH_CAN_ENABLE:
  240. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_IE);
  241. break;
  242. case PCH_CAN_DISABLE:
  243. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE);
  244. break;
  245. case PCH_CAN_ALL:
  246. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE);
  247. break;
  248. case PCH_CAN_NONE:
  249. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE);
  250. break;
  251. default:
  252. dev_err(&priv->ndev->dev, "Invalid interrupt number.\n");
  253. break;
  254. }
  255. }
  256. static void pch_can_check_if_busy(u32 __iomem *creq_addr, u32 num)
  257. {
  258. u32 counter = PCH_COUNTER_LIMIT;
  259. u32 ifx_creq;
  260. iowrite32(num, creq_addr);
  261. while (counter) {
  262. ifx_creq = ioread32(creq_addr) & PCH_IF_CREQ_BUSY;
  263. if (!ifx_creq)
  264. break;
  265. counter--;
  266. udelay(1);
  267. }
  268. if (!counter)
  269. pr_err("%s:IF1 BUSY Flag is set forever.\n", __func__);
  270. }
  271. static void pch_can_set_rx_enable(struct pch_can_priv *priv, u32 buff_num,
  272. u32 set)
  273. {
  274. unsigned long flags;
  275. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  276. /* Reading the receive buffer data from RAM to Interface1 registers */
  277. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  278. pch_can_check_if_busy(&priv->regs->if1_creq, buff_num);
  279. /* Setting the IF1MASK1 register to access MsgVal and RxIE bits */
  280. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_ARB | PCH_CMASK_CTRL,
  281. &priv->regs->if1_cmask);
  282. if (set == PCH_ENABLE) {
  283. /* Setting the MsgVal and RxIE bits */
  284. pch_can_bit_set(&priv->regs->if1_mcont, PCH_IF_MCONT_RXIE);
  285. pch_can_bit_set(&priv->regs->if1_id2, PCH_ID_MSGVAL);
  286. } else if (set == PCH_DISABLE) {
  287. /* Resetting the MsgVal and RxIE bits */
  288. pch_can_bit_clear(&priv->regs->if1_mcont, PCH_IF_MCONT_RXIE);
  289. pch_can_bit_clear(&priv->regs->if1_id2, PCH_ID_MSGVAL);
  290. }
  291. pch_can_check_if_busy(&priv->regs->if1_creq, buff_num);
  292. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  293. }
  294. static void pch_can_rx_enable_all(struct pch_can_priv *priv)
  295. {
  296. int i;
  297. /* Traversing to obtain the object configured as receivers. */
  298. for (i = 0; i < PCH_OBJ_NUM; i++) {
  299. if (priv->msg_obj[i] == PCH_MSG_OBJ_RX)
  300. pch_can_set_rx_enable(priv, i + 1, PCH_ENABLE);
  301. }
  302. }
  303. static void pch_can_rx_disable_all(struct pch_can_priv *priv)
  304. {
  305. int i;
  306. /* Traversing to obtain the object configured as receivers. */
  307. for (i = 0; i < PCH_OBJ_NUM; i++) {
  308. if (priv->msg_obj[i] == PCH_MSG_OBJ_RX)
  309. pch_can_set_rx_enable(priv, i + 1, PCH_DISABLE);
  310. }
  311. }
  312. static void pch_can_set_tx_enable(struct pch_can_priv *priv, u32 buff_num,
  313. u32 set)
  314. {
  315. unsigned long flags;
  316. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  317. /* Reading the Msg buffer from Message RAM to Interface2 registers. */
  318. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  319. pch_can_check_if_busy(&priv->regs->if2_creq, buff_num);
  320. /* Setting the IF2CMASK register for accessing the
  321. MsgVal and TxIE bits */
  322. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_ARB | PCH_CMASK_CTRL,
  323. &priv->regs->if2_cmask);
  324. if (set == PCH_ENABLE) {
  325. /* Setting the MsgVal and TxIE bits */
  326. pch_can_bit_set(&priv->regs->if2_mcont, PCH_IF_MCONT_TXIE);
  327. pch_can_bit_set(&priv->regs->if2_id2, PCH_ID_MSGVAL);
  328. } else if (set == PCH_DISABLE) {
  329. /* Resetting the MsgVal and TxIE bits. */
  330. pch_can_bit_clear(&priv->regs->if2_mcont, PCH_IF_MCONT_TXIE);
  331. pch_can_bit_clear(&priv->regs->if2_id2, PCH_ID_MSGVAL);
  332. }
  333. pch_can_check_if_busy(&priv->regs->if2_creq, buff_num);
  334. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  335. }
  336. static void pch_can_tx_enable_all(struct pch_can_priv *priv)
  337. {
  338. int i;
  339. /* Traversing to obtain the object configured as transmit object. */
  340. for (i = 0; i < PCH_OBJ_NUM; i++) {
  341. if (priv->msg_obj[i] == PCH_MSG_OBJ_TX)
  342. pch_can_set_tx_enable(priv, i + 1, PCH_ENABLE);
  343. }
  344. }
  345. static void pch_can_tx_disable_all(struct pch_can_priv *priv)
  346. {
  347. int i;
  348. /* Traversing to obtain the object configured as transmit object. */
  349. for (i = 0; i < PCH_OBJ_NUM; i++) {
  350. if (priv->msg_obj[i] == PCH_MSG_OBJ_TX)
  351. pch_can_set_tx_enable(priv, i + 1, PCH_DISABLE);
  352. }
  353. }
  354. static void pch_can_get_rx_enable(struct pch_can_priv *priv, u32 buff_num,
  355. u32 *enable)
  356. {
  357. unsigned long flags;
  358. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  359. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  360. pch_can_check_if_busy(&priv->regs->if1_creq, buff_num);
  361. if (((ioread32(&priv->regs->if1_id2)) & PCH_ID_MSGVAL) &&
  362. ((ioread32(&priv->regs->if1_mcont)) &
  363. PCH_IF_MCONT_RXIE))
  364. *enable = PCH_ENABLE;
  365. else
  366. *enable = PCH_DISABLE;
  367. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  368. }
  369. static void pch_can_get_tx_enable(struct pch_can_priv *priv, u32 buff_num,
  370. u32 *enable)
  371. {
  372. unsigned long flags;
  373. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  374. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  375. pch_can_check_if_busy(&priv->regs->if2_creq, buff_num);
  376. if (((ioread32(&priv->regs->if2_id2)) & PCH_ID_MSGVAL) &&
  377. ((ioread32(&priv->regs->if2_mcont)) &
  378. PCH_IF_MCONT_TXIE)) {
  379. *enable = PCH_ENABLE;
  380. } else {
  381. *enable = PCH_DISABLE;
  382. }
  383. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  384. }
  385. static int pch_can_int_pending(struct pch_can_priv *priv)
  386. {
  387. return ioread32(&priv->regs->intr) & 0xffff;
  388. }
  389. static void pch_can_set_rx_buffer_link(struct pch_can_priv *priv,
  390. u32 buffer_num, u32 set)
  391. {
  392. unsigned long flags;
  393. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  394. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  395. pch_can_check_if_busy(&priv->regs->if1_creq, buffer_num);
  396. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL, &priv->regs->if1_cmask);
  397. if (set == PCH_ENABLE)
  398. pch_can_bit_clear(&priv->regs->if1_mcont, PCH_IF_MCONT_EOB);
  399. else
  400. pch_can_bit_set(&priv->regs->if1_mcont, PCH_IF_MCONT_EOB);
  401. pch_can_check_if_busy(&priv->regs->if1_creq, buffer_num);
  402. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  403. }
  404. static void pch_can_get_rx_buffer_link(struct pch_can_priv *priv,
  405. u32 buffer_num, u32 *link)
  406. {
  407. unsigned long flags;
  408. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  409. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  410. pch_can_check_if_busy(&priv->regs->if1_creq, buffer_num);
  411. if (ioread32(&priv->regs->if1_mcont) & PCH_IF_MCONT_EOB)
  412. *link = PCH_DISABLE;
  413. else
  414. *link = PCH_ENABLE;
  415. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  416. }
  417. static void pch_can_clear_buffers(struct pch_can_priv *priv)
  418. {
  419. int i;
  420. for (i = 0; i < PCH_RX_OBJ_NUM; i++) {
  421. iowrite32(PCH_CMASK_RX_TX_SET, &priv->regs->if1_cmask);
  422. iowrite32(0xffff, &priv->regs->if1_mask1);
  423. iowrite32(0xffff, &priv->regs->if1_mask2);
  424. iowrite32(0x0, &priv->regs->if1_id1);
  425. iowrite32(0x0, &priv->regs->if1_id2);
  426. iowrite32(0x0, &priv->regs->if1_mcont);
  427. iowrite32(0x0, &priv->regs->if1_dataa1);
  428. iowrite32(0x0, &priv->regs->if1_dataa2);
  429. iowrite32(0x0, &priv->regs->if1_datab1);
  430. iowrite32(0x0, &priv->regs->if1_datab2);
  431. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK |
  432. PCH_CMASK_ARB | PCH_CMASK_CTRL,
  433. &priv->regs->if1_cmask);
  434. pch_can_check_if_busy(&priv->regs->if1_creq, i+1);
  435. }
  436. for (i = i; i < PCH_OBJ_NUM; i++) {
  437. iowrite32(PCH_CMASK_RX_TX_SET, &priv->regs->if2_cmask);
  438. iowrite32(0xffff, &priv->regs->if2_mask1);
  439. iowrite32(0xffff, &priv->regs->if2_mask2);
  440. iowrite32(0x0, &priv->regs->if2_id1);
  441. iowrite32(0x0, &priv->regs->if2_id2);
  442. iowrite32(0x0, &priv->regs->if2_mcont);
  443. iowrite32(0x0, &priv->regs->if2_dataa1);
  444. iowrite32(0x0, &priv->regs->if2_dataa2);
  445. iowrite32(0x0, &priv->regs->if2_datab1);
  446. iowrite32(0x0, &priv->regs->if2_datab2);
  447. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK |
  448. PCH_CMASK_ARB | PCH_CMASK_CTRL,
  449. &priv->regs->if2_cmask);
  450. pch_can_check_if_busy(&priv->regs->if2_creq, i+1);
  451. }
  452. }
  453. static void pch_can_config_rx_tx_buffers(struct pch_can_priv *priv)
  454. {
  455. int i;
  456. unsigned long flags;
  457. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  458. for (i = 0; i < PCH_OBJ_NUM; i++) {
  459. if (priv->msg_obj[i] == PCH_MSG_OBJ_RX) {
  460. iowrite32(PCH_CMASK_RX_TX_GET,
  461. &priv->regs->if1_cmask);
  462. pch_can_check_if_busy(&priv->regs->if1_creq, i+1);
  463. iowrite32(0x0, &priv->regs->if1_id1);
  464. iowrite32(0x0, &priv->regs->if1_id2);
  465. pch_can_bit_set(&priv->regs->if1_mcont,
  466. PCH_IF_MCONT_UMASK);
  467. /* Set FIFO mode set to 0 except last Rx Obj*/
  468. pch_can_bit_clear(&priv->regs->if1_mcont,
  469. PCH_IF_MCONT_EOB);
  470. /* In case FIFO mode, Last EoB of Rx Obj must be 1 */
  471. if (i == (PCH_RX_OBJ_NUM - 1))
  472. pch_can_bit_set(&priv->regs->if1_mcont,
  473. PCH_IF_MCONT_EOB);
  474. iowrite32(0, &priv->regs->if1_mask1);
  475. pch_can_bit_clear(&priv->regs->if1_mask2,
  476. 0x1fff | PCH_MASK2_MDIR_MXTD);
  477. /* Setting CMASK for writing */
  478. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK |
  479. PCH_CMASK_ARB | PCH_CMASK_CTRL,
  480. &priv->regs->if1_cmask);
  481. pch_can_check_if_busy(&priv->regs->if1_creq, i+1);
  482. } else if (priv->msg_obj[i] == PCH_MSG_OBJ_TX) {
  483. iowrite32(PCH_CMASK_RX_TX_GET,
  484. &priv->regs->if2_cmask);
  485. pch_can_check_if_busy(&priv->regs->if2_creq, i+1);
  486. /* Resetting DIR bit for reception */
  487. iowrite32(0x0, &priv->regs->if2_id1);
  488. iowrite32(0x0, &priv->regs->if2_id2);
  489. pch_can_bit_set(&priv->regs->if2_id2, PCH_ID2_DIR);
  490. /* Setting EOB bit for transmitter */
  491. iowrite32(PCH_IF_MCONT_EOB, &priv->regs->if2_mcont);
  492. pch_can_bit_set(&priv->regs->if2_mcont,
  493. PCH_IF_MCONT_UMASK);
  494. iowrite32(0, &priv->regs->if2_mask1);
  495. pch_can_bit_clear(&priv->regs->if2_mask2, 0x1fff);
  496. /* Setting CMASK for writing */
  497. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK |
  498. PCH_CMASK_ARB | PCH_CMASK_CTRL,
  499. &priv->regs->if2_cmask);
  500. pch_can_check_if_busy(&priv->regs->if2_creq, i+1);
  501. }
  502. }
  503. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  504. }
  505. static void pch_can_init(struct pch_can_priv *priv)
  506. {
  507. /* Stopping the Can device. */
  508. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  509. /* Clearing all the message object buffers. */
  510. pch_can_clear_buffers(priv);
  511. /* Configuring the respective message object as either rx/tx object. */
  512. pch_can_config_rx_tx_buffers(priv);
  513. /* Enabling the interrupts. */
  514. pch_can_set_int_enables(priv, PCH_CAN_ALL);
  515. }
  516. static void pch_can_release(struct pch_can_priv *priv)
  517. {
  518. /* Stooping the CAN device. */
  519. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  520. /* Disabling the interrupts. */
  521. pch_can_set_int_enables(priv, PCH_CAN_NONE);
  522. /* Disabling all the receive object. */
  523. pch_can_rx_disable_all(priv);
  524. /* Disabling all the transmit object. */
  525. pch_can_tx_disable_all(priv);
  526. }
  527. /* This function clears interrupt(s) from the CAN device. */
  528. static void pch_can_int_clr(struct pch_can_priv *priv, u32 mask)
  529. {
  530. if (mask == PCH_STATUS_INT) {
  531. ioread32(&priv->regs->stat);
  532. return;
  533. }
  534. /* Clear interrupt for transmit object */
  535. if (priv->msg_obj[mask - 1] == PCH_MSG_OBJ_TX) {
  536. /* Setting CMASK for clearing interrupts for
  537. frame transmission. */
  538. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | PCH_CMASK_ARB,
  539. &priv->regs->if2_cmask);
  540. /* Resetting the ID registers. */
  541. pch_can_bit_set(&priv->regs->if2_id2,
  542. PCH_ID2_DIR | (0x7ff << 2));
  543. iowrite32(0x0, &priv->regs->if2_id1);
  544. /* Claring NewDat, TxRqst & IntPnd */
  545. pch_can_bit_clear(&priv->regs->if2_mcont,
  546. PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND |
  547. PCH_IF_MCONT_TXRQXT);
  548. pch_can_check_if_busy(&priv->regs->if2_creq, mask);
  549. } else if (priv->msg_obj[mask - 1] == PCH_MSG_OBJ_RX) {
  550. /* Setting CMASK for clearing the reception interrupts. */
  551. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | PCH_CMASK_ARB,
  552. &priv->regs->if1_cmask);
  553. /* Clearing the Dir bit. */
  554. pch_can_bit_clear(&priv->regs->if1_id2, PCH_ID2_DIR);
  555. /* Clearing NewDat & IntPnd */
  556. pch_can_bit_clear(&priv->regs->if1_mcont,
  557. PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND);
  558. pch_can_check_if_busy(&priv->regs->if1_creq, mask);
  559. }
  560. }
  561. static int pch_can_get_buffer_status(struct pch_can_priv *priv)
  562. {
  563. return (ioread32(&priv->regs->treq1) & 0xffff) |
  564. ((ioread32(&priv->regs->treq2) & 0xffff) << 16);
  565. }
  566. static void pch_can_reset(struct pch_can_priv *priv)
  567. {
  568. /* write to sw reset register */
  569. iowrite32(1, &priv->regs->srst);
  570. iowrite32(0, &priv->regs->srst);
  571. }
  572. static void pch_can_error(struct net_device *ndev, u32 status)
  573. {
  574. struct sk_buff *skb;
  575. struct pch_can_priv *priv = netdev_priv(ndev);
  576. struct can_frame *cf;
  577. u32 errc;
  578. struct net_device_stats *stats = &(priv->ndev->stats);
  579. enum can_state state = priv->can.state;
  580. skb = alloc_can_err_skb(ndev, &cf);
  581. if (!skb)
  582. return;
  583. if (status & PCH_BUS_OFF) {
  584. pch_can_tx_disable_all(priv);
  585. pch_can_rx_disable_all(priv);
  586. state = CAN_STATE_BUS_OFF;
  587. cf->can_id |= CAN_ERR_BUSOFF;
  588. can_bus_off(ndev);
  589. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  590. dev_err(&ndev->dev, "%s -> Bus Off occurres.\n", __func__);
  591. }
  592. /* Warning interrupt. */
  593. if (status & PCH_EWARN) {
  594. state = CAN_STATE_ERROR_WARNING;
  595. priv->can.can_stats.error_warning++;
  596. cf->can_id |= CAN_ERR_CRTL;
  597. errc = ioread32(&priv->regs->errc);
  598. if (((errc & PCH_REC) >> 8) > 96)
  599. cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
  600. if ((errc & PCH_TEC) > 96)
  601. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
  602. dev_warn(&ndev->dev,
  603. "%s -> Error Counter is more than 96.\n", __func__);
  604. }
  605. /* Error passive interrupt. */
  606. if (status & PCH_EPASSIV) {
  607. priv->can.can_stats.error_passive++;
  608. state = CAN_STATE_ERROR_PASSIVE;
  609. cf->can_id |= CAN_ERR_CRTL;
  610. errc = ioread32(&priv->regs->errc);
  611. if (((errc & PCH_REC) >> 8) > 127)
  612. cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  613. if ((errc & PCH_TEC) > 127)
  614. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  615. dev_err(&ndev->dev,
  616. "%s -> CAN controller is ERROR PASSIVE .\n", __func__);
  617. }
  618. if (status & PCH_LEC_ALL) {
  619. priv->can.can_stats.bus_error++;
  620. stats->rx_errors++;
  621. switch (status & PCH_LEC_ALL) {
  622. case PCH_STUF_ERR:
  623. cf->data[2] |= CAN_ERR_PROT_STUFF;
  624. break;
  625. case PCH_FORM_ERR:
  626. cf->data[2] |= CAN_ERR_PROT_FORM;
  627. break;
  628. case PCH_ACK_ERR:
  629. cf->data[2] |= CAN_ERR_PROT_LOC_ACK |
  630. CAN_ERR_PROT_LOC_ACK_DEL;
  631. break;
  632. case PCH_BIT1_ERR:
  633. case PCH_BIT0_ERR:
  634. cf->data[2] |= CAN_ERR_PROT_BIT;
  635. break;
  636. case PCH_CRC_ERR:
  637. cf->data[2] |= CAN_ERR_PROT_LOC_CRC_SEQ |
  638. CAN_ERR_PROT_LOC_CRC_DEL;
  639. break;
  640. default:
  641. iowrite32(status | PCH_LEC_ALL, &priv->regs->stat);
  642. break;
  643. }
  644. }
  645. priv->can.state = state;
  646. netif_rx(skb);
  647. stats->rx_packets++;
  648. stats->rx_bytes += cf->can_dlc;
  649. }
  650. static irqreturn_t pch_can_interrupt(int irq, void *dev_id)
  651. {
  652. struct net_device *ndev = (struct net_device *)dev_id;
  653. struct pch_can_priv *priv = netdev_priv(ndev);
  654. pch_can_set_int_enables(priv, PCH_CAN_NONE);
  655. napi_schedule(&priv->napi);
  656. return IRQ_HANDLED;
  657. }
  658. static int pch_can_rx_normal(struct net_device *ndev, u32 int_stat)
  659. {
  660. u32 reg;
  661. canid_t id;
  662. u32 ide;
  663. u32 rtr;
  664. int i, j, k;
  665. int rcv_pkts = 0;
  666. struct sk_buff *skb;
  667. struct can_frame *cf;
  668. struct pch_can_priv *priv = netdev_priv(ndev);
  669. struct net_device_stats *stats = &(priv->ndev->stats);
  670. /* Reading the messsage object from the Message RAM */
  671. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  672. pch_can_check_if_busy(&priv->regs->if1_creq, int_stat);
  673. /* Reading the MCONT register. */
  674. reg = ioread32(&priv->regs->if1_mcont);
  675. reg &= 0xffff;
  676. for (k = int_stat; !(reg & PCH_IF_MCONT_EOB); k++) {
  677. /* If MsgLost bit set. */
  678. if (reg & PCH_IF_MCONT_MSGLOST) {
  679. dev_err(&priv->ndev->dev, "Msg Obj is overwritten.\n");
  680. pch_can_bit_clear(&priv->regs->if1_mcont,
  681. PCH_IF_MCONT_MSGLOST);
  682. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL,
  683. &priv->regs->if1_cmask);
  684. pch_can_check_if_busy(&priv->regs->if1_creq, k);
  685. skb = alloc_can_err_skb(ndev, &cf);
  686. if (!skb)
  687. return -ENOMEM;
  688. priv->can.can_stats.error_passive++;
  689. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  690. cf->can_id |= CAN_ERR_CRTL;
  691. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  692. cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
  693. stats->rx_packets++;
  694. stats->rx_bytes += cf->can_dlc;
  695. netif_receive_skb(skb);
  696. rcv_pkts++;
  697. goto RX_NEXT;
  698. }
  699. if (!(reg & PCH_IF_MCONT_NEWDAT))
  700. goto RX_NEXT;
  701. skb = alloc_can_skb(priv->ndev, &cf);
  702. if (!skb)
  703. return -ENOMEM;
  704. /* Get Received data */
  705. ide = ((ioread32(&priv->regs->if1_id2)) & PCH_ID2_XTD) >> 14;
  706. if (ide) {
  707. id = (ioread32(&priv->regs->if1_id1) & 0xffff);
  708. id |= (((ioread32(&priv->regs->if1_id2)) &
  709. 0x1fff) << 16);
  710. cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
  711. } else {
  712. id = (((ioread32(&priv->regs->if1_id2)) &
  713. (CAN_SFF_MASK << 2)) >> 2);
  714. cf->can_id = (id & CAN_SFF_MASK);
  715. }
  716. rtr = (ioread32(&priv->regs->if1_id2) & PCH_ID2_DIR);
  717. if (rtr) {
  718. cf->can_dlc = 0;
  719. cf->can_id |= CAN_RTR_FLAG;
  720. } else {
  721. cf->can_dlc = ((ioread32(&priv->regs->if1_mcont)) &
  722. 0x0f);
  723. }
  724. for (i = 0, j = 0; i < cf->can_dlc; j++) {
  725. reg = ioread32(&priv->regs->if1_dataa1 + j*4);
  726. cf->data[i++] = cpu_to_le32(reg & 0xff);
  727. if (i == cf->can_dlc)
  728. break;
  729. cf->data[i++] = cpu_to_le32((reg >> 8) & 0xff);
  730. }
  731. netif_receive_skb(skb);
  732. rcv_pkts++;
  733. stats->rx_packets++;
  734. stats->rx_bytes += cf->can_dlc;
  735. if (k < PCH_FIFO_THRESH) {
  736. iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL |
  737. PCH_CMASK_ARB, &priv->regs->if1_cmask);
  738. /* Clearing the Dir bit. */
  739. pch_can_bit_clear(&priv->regs->if1_id2, PCH_ID2_DIR);
  740. /* Clearing NewDat & IntPnd */
  741. pch_can_bit_clear(&priv->regs->if1_mcont,
  742. PCH_IF_MCONT_INTPND);
  743. pch_can_check_if_busy(&priv->regs->if1_creq, k);
  744. } else if (k > PCH_FIFO_THRESH) {
  745. pch_can_int_clr(priv, k);
  746. } else if (k == PCH_FIFO_THRESH) {
  747. int cnt;
  748. for (cnt = 0; cnt < PCH_FIFO_THRESH; cnt++)
  749. pch_can_int_clr(priv, cnt+1);
  750. }
  751. RX_NEXT:
  752. /* Reading the messsage object from the Message RAM */
  753. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  754. pch_can_check_if_busy(&priv->regs->if1_creq, k + 1);
  755. reg = ioread32(&priv->regs->if1_mcont);
  756. }
  757. return rcv_pkts;
  758. }
  759. static int pch_can_rx_poll(struct napi_struct *napi, int quota)
  760. {
  761. struct net_device *ndev = napi->dev;
  762. struct pch_can_priv *priv = netdev_priv(ndev);
  763. struct net_device_stats *stats = &(priv->ndev->stats);
  764. u32 dlc;
  765. u32 int_stat;
  766. int rcv_pkts = 0;
  767. u32 reg_stat;
  768. unsigned long flags;
  769. int_stat = pch_can_int_pending(priv);
  770. if (!int_stat)
  771. return 0;
  772. INT_STAT:
  773. if (int_stat == PCH_STATUS_INT) {
  774. reg_stat = ioread32(&priv->regs->stat);
  775. if (reg_stat & (PCH_BUS_OFF | PCH_LEC_ALL)) {
  776. if ((reg_stat & PCH_LEC_ALL) != PCH_LEC_ALL)
  777. pch_can_error(ndev, reg_stat);
  778. }
  779. if (reg_stat & PCH_TX_OK) {
  780. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  781. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  782. pch_can_check_if_busy(&priv->regs->if2_creq,
  783. ioread32(&priv->regs->intr));
  784. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  785. pch_can_bit_clear(&priv->regs->stat, PCH_TX_OK);
  786. }
  787. if (reg_stat & PCH_RX_OK)
  788. pch_can_bit_clear(&priv->regs->stat, PCH_RX_OK);
  789. int_stat = pch_can_int_pending(priv);
  790. if (int_stat == PCH_STATUS_INT)
  791. goto INT_STAT;
  792. }
  793. MSG_OBJ:
  794. if ((int_stat >= 1) && (int_stat <= PCH_RX_OBJ_NUM)) {
  795. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  796. rcv_pkts = pch_can_rx_normal(ndev, int_stat);
  797. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  798. if (rcv_pkts < 0)
  799. return 0;
  800. } else if ((int_stat > PCH_RX_OBJ_NUM) && (int_stat <= PCH_OBJ_NUM)) {
  801. if (priv->msg_obj[int_stat - 1] == PCH_MSG_OBJ_TX) {
  802. /* Handle transmission interrupt */
  803. can_get_echo_skb(ndev, int_stat - PCH_RX_OBJ_NUM - 1);
  804. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  805. iowrite32(PCH_CMASK_RX_TX_GET | PCH_CMASK_CLRINTPND,
  806. &priv->regs->if2_cmask);
  807. dlc = ioread32(&priv->regs->if2_mcont) &
  808. PCH_IF_MCONT_DLC;
  809. pch_can_check_if_busy(&priv->regs->if2_creq, int_stat);
  810. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  811. if (dlc > 8)
  812. dlc = 8;
  813. stats->tx_bytes += dlc;
  814. stats->tx_packets++;
  815. }
  816. }
  817. int_stat = pch_can_int_pending(priv);
  818. if (int_stat == PCH_STATUS_INT)
  819. goto INT_STAT;
  820. else if (int_stat >= 1 && int_stat <= 32)
  821. goto MSG_OBJ;
  822. napi_complete(napi);
  823. pch_can_set_int_enables(priv, PCH_CAN_ALL);
  824. return rcv_pkts;
  825. }
  826. static int pch_set_bittiming(struct net_device *ndev)
  827. {
  828. struct pch_can_priv *priv = netdev_priv(ndev);
  829. const struct can_bittiming *bt = &priv->can.bittiming;
  830. u32 canbit;
  831. u32 bepe;
  832. u32 brp;
  833. /* Setting the CCE bit for accessing the Can Timing register. */
  834. pch_can_bit_set(&priv->regs->cont, PCH_CTRL_CCE);
  835. brp = (bt->tq) / (1000000000/PCH_CAN_CLK) - 1;
  836. canbit = brp & PCH_MSK_BITT_BRP;
  837. canbit |= (bt->sjw - 1) << PCH_BIT_SJW;
  838. canbit |= (bt->phase_seg1 + bt->prop_seg - 1) << PCH_BIT_TSEG1;
  839. canbit |= (bt->phase_seg2 - 1) << PCH_BIT_TSEG2;
  840. bepe = (brp & PCH_MSK_BRPE_BRPE) >> PCH_BIT_BRPE_BRPE;
  841. iowrite32(canbit, &priv->regs->bitt);
  842. iowrite32(bepe, &priv->regs->brpe);
  843. pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_CCE);
  844. return 0;
  845. }
  846. static void pch_can_start(struct net_device *ndev)
  847. {
  848. struct pch_can_priv *priv = netdev_priv(ndev);
  849. if (priv->can.state != CAN_STATE_STOPPED)
  850. pch_can_reset(priv);
  851. pch_set_bittiming(ndev);
  852. pch_can_set_optmode(priv);
  853. pch_can_tx_enable_all(priv);
  854. pch_can_rx_enable_all(priv);
  855. /* Setting the CAN to run mode. */
  856. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  857. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  858. return;
  859. }
  860. static int pch_can_do_set_mode(struct net_device *ndev, enum can_mode mode)
  861. {
  862. int ret = 0;
  863. switch (mode) {
  864. case CAN_MODE_START:
  865. pch_can_start(ndev);
  866. netif_wake_queue(ndev);
  867. break;
  868. default:
  869. ret = -EOPNOTSUPP;
  870. break;
  871. }
  872. return ret;
  873. }
  874. static int pch_can_open(struct net_device *ndev)
  875. {
  876. struct pch_can_priv *priv = netdev_priv(ndev);
  877. int retval;
  878. retval = pci_enable_msi(priv->dev);
  879. if (retval) {
  880. dev_info(&ndev->dev, "PCH CAN opened without MSI\n");
  881. priv->use_msi = 0;
  882. } else {
  883. dev_info(&ndev->dev, "PCH CAN opened with MSI\n");
  884. priv->use_msi = 1;
  885. }
  886. /* Regsitering the interrupt. */
  887. retval = request_irq(priv->dev->irq, pch_can_interrupt, IRQF_SHARED,
  888. ndev->name, ndev);
  889. if (retval) {
  890. dev_err(&ndev->dev, "request_irq failed.\n");
  891. goto req_irq_err;
  892. }
  893. /* Open common can device */
  894. retval = open_candev(ndev);
  895. if (retval) {
  896. dev_err(ndev->dev.parent, "open_candev() failed %d\n", retval);
  897. goto err_open_candev;
  898. }
  899. pch_can_init(priv);
  900. pch_can_start(ndev);
  901. napi_enable(&priv->napi);
  902. netif_start_queue(ndev);
  903. return 0;
  904. err_open_candev:
  905. free_irq(priv->dev->irq, ndev);
  906. req_irq_err:
  907. if (priv->use_msi)
  908. pci_disable_msi(priv->dev);
  909. pch_can_release(priv);
  910. return retval;
  911. }
  912. static int pch_close(struct net_device *ndev)
  913. {
  914. struct pch_can_priv *priv = netdev_priv(ndev);
  915. netif_stop_queue(ndev);
  916. napi_disable(&priv->napi);
  917. pch_can_release(priv);
  918. free_irq(priv->dev->irq, ndev);
  919. if (priv->use_msi)
  920. pci_disable_msi(priv->dev);
  921. close_candev(ndev);
  922. priv->can.state = CAN_STATE_STOPPED;
  923. return 0;
  924. }
  925. static int pch_get_msg_obj_sts(struct net_device *ndev, u32 obj_id)
  926. {
  927. u32 buffer_status = 0;
  928. struct pch_can_priv *priv = netdev_priv(ndev);
  929. /* Getting the message object status. */
  930. buffer_status = (u32) pch_can_get_buffer_status(priv);
  931. return buffer_status & obj_id;
  932. }
  933. static netdev_tx_t pch_xmit(struct sk_buff *skb, struct net_device *ndev)
  934. {
  935. int i, j;
  936. unsigned long flags;
  937. struct pch_can_priv *priv = netdev_priv(ndev);
  938. struct can_frame *cf = (struct can_frame *)skb->data;
  939. int tx_buffer_avail = 0;
  940. if (can_dropped_invalid_skb(ndev, skb))
  941. return NETDEV_TX_OK;
  942. if (priv->tx_obj == (PCH_OBJ_NUM + 1)) { /* Point tail Obj */
  943. while (pch_get_msg_obj_sts(ndev, (((1 << PCH_TX_OBJ_NUM)-1) <<
  944. PCH_RX_OBJ_NUM)))
  945. udelay(500);
  946. priv->tx_obj = PCH_RX_OBJ_NUM + 1; /* Point head of Tx Obj ID */
  947. tx_buffer_avail = priv->tx_obj; /* Point Tail of Tx Obj */
  948. } else {
  949. tx_buffer_avail = priv->tx_obj;
  950. }
  951. priv->tx_obj++;
  952. /* Attaining the lock. */
  953. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  954. /* Reading the Msg Obj from the Msg RAM to the Interface register. */
  955. iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  956. pch_can_check_if_busy(&priv->regs->if2_creq, tx_buffer_avail);
  957. /* Setting the CMASK register. */
  958. pch_can_bit_set(&priv->regs->if2_cmask, PCH_CMASK_ALL);
  959. /* If ID extended is set. */
  960. pch_can_bit_clear(&priv->regs->if2_id1, 0xffff);
  961. pch_can_bit_clear(&priv->regs->if2_id2, 0x1fff | PCH_ID2_XTD);
  962. if (cf->can_id & CAN_EFF_FLAG) {
  963. pch_can_bit_set(&priv->regs->if2_id1, cf->can_id & 0xffff);
  964. pch_can_bit_set(&priv->regs->if2_id2,
  965. ((cf->can_id >> 16) & 0x1fff) | PCH_ID2_XTD);
  966. } else {
  967. pch_can_bit_set(&priv->regs->if2_id1, 0);
  968. pch_can_bit_set(&priv->regs->if2_id2,
  969. (cf->can_id & CAN_SFF_MASK) << 2);
  970. }
  971. /* If remote frame has to be transmitted.. */
  972. if (cf->can_id & CAN_RTR_FLAG)
  973. pch_can_bit_clear(&priv->regs->if2_id2, PCH_ID2_DIR);
  974. for (i = 0, j = 0; i < cf->can_dlc; j++) {
  975. iowrite32(le32_to_cpu(cf->data[i++]),
  976. (&priv->regs->if2_dataa1) + j*4);
  977. if (i == cf->can_dlc)
  978. break;
  979. iowrite32(le32_to_cpu(cf->data[i++] << 8),
  980. (&priv->regs->if2_dataa1) + j*4);
  981. }
  982. can_put_echo_skb(skb, ndev, tx_buffer_avail - PCH_RX_OBJ_NUM - 1);
  983. /* Updating the size of the data. */
  984. pch_can_bit_clear(&priv->regs->if2_mcont, 0x0f);
  985. pch_can_bit_set(&priv->regs->if2_mcont, cf->can_dlc);
  986. /* Clearing IntPend, NewDat & TxRqst */
  987. pch_can_bit_clear(&priv->regs->if2_mcont,
  988. PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND |
  989. PCH_IF_MCONT_TXRQXT);
  990. /* Setting NewDat, TxRqst bits */
  991. pch_can_bit_set(&priv->regs->if2_mcont,
  992. PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_TXRQXT);
  993. pch_can_check_if_busy(&priv->regs->if2_creq, tx_buffer_avail);
  994. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  995. return NETDEV_TX_OK;
  996. }
  997. static const struct net_device_ops pch_can_netdev_ops = {
  998. .ndo_open = pch_can_open,
  999. .ndo_stop = pch_close,
  1000. .ndo_start_xmit = pch_xmit,
  1001. };
  1002. static void __devexit pch_can_remove(struct pci_dev *pdev)
  1003. {
  1004. struct net_device *ndev = pci_get_drvdata(pdev);
  1005. struct pch_can_priv *priv = netdev_priv(ndev);
  1006. unregister_candev(priv->ndev);
  1007. free_candev(priv->ndev);
  1008. pci_iounmap(pdev, priv->regs);
  1009. pci_release_regions(pdev);
  1010. pci_disable_device(pdev);
  1011. pci_set_drvdata(pdev, NULL);
  1012. pch_can_reset(priv);
  1013. }
  1014. #ifdef CONFIG_PM
  1015. static int pch_can_suspend(struct pci_dev *pdev, pm_message_t state)
  1016. {
  1017. int i; /* Counter variable. */
  1018. int retval; /* Return value. */
  1019. u32 buf_stat; /* Variable for reading the transmit buffer status. */
  1020. u32 counter = 0xFFFFFF;
  1021. struct net_device *dev = pci_get_drvdata(pdev);
  1022. struct pch_can_priv *priv = netdev_priv(dev);
  1023. /* Stop the CAN controller */
  1024. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  1025. /* Indicate that we are aboutto/in suspend */
  1026. priv->can.state = CAN_STATE_SLEEPING;
  1027. /* Waiting for all transmission to complete. */
  1028. while (counter) {
  1029. buf_stat = pch_can_get_buffer_status(priv);
  1030. if (!buf_stat)
  1031. break;
  1032. counter--;
  1033. udelay(1);
  1034. }
  1035. if (!counter)
  1036. dev_err(&pdev->dev, "%s -> Transmission time out.\n", __func__);
  1037. /* Save interrupt configuration and then disable them */
  1038. pch_can_get_int_enables(priv, &(priv->int_enables));
  1039. pch_can_set_int_enables(priv, PCH_CAN_DISABLE);
  1040. /* Save Tx buffer enable state */
  1041. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1042. if (priv->msg_obj[i] == PCH_MSG_OBJ_TX)
  1043. pch_can_get_tx_enable(priv, i + 1,
  1044. &(priv->tx_enable[i]));
  1045. }
  1046. /* Disable all Transmit buffers */
  1047. pch_can_tx_disable_all(priv);
  1048. /* Save Rx buffer enable state */
  1049. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1050. if (priv->msg_obj[i] == PCH_MSG_OBJ_RX) {
  1051. pch_can_get_rx_enable(priv, i + 1,
  1052. &(priv->rx_enable[i]));
  1053. pch_can_get_rx_buffer_link(priv, i + 1,
  1054. &(priv->rx_link[i]));
  1055. }
  1056. }
  1057. /* Disable all Receive buffers */
  1058. pch_can_rx_disable_all(priv);
  1059. retval = pci_save_state(pdev);
  1060. if (retval) {
  1061. dev_err(&pdev->dev, "pci_save_state failed.\n");
  1062. } else {
  1063. pci_enable_wake(pdev, PCI_D3hot, 0);
  1064. pci_disable_device(pdev);
  1065. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1066. }
  1067. return retval;
  1068. }
  1069. static int pch_can_resume(struct pci_dev *pdev)
  1070. {
  1071. int i; /* Counter variable. */
  1072. int retval; /* Return variable. */
  1073. struct net_device *dev = pci_get_drvdata(pdev);
  1074. struct pch_can_priv *priv = netdev_priv(dev);
  1075. pci_set_power_state(pdev, PCI_D0);
  1076. pci_restore_state(pdev);
  1077. retval = pci_enable_device(pdev);
  1078. if (retval) {
  1079. dev_err(&pdev->dev, "pci_enable_device failed.\n");
  1080. return retval;
  1081. }
  1082. pci_enable_wake(pdev, PCI_D3hot, 0);
  1083. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  1084. /* Disabling all interrupts. */
  1085. pch_can_set_int_enables(priv, PCH_CAN_DISABLE);
  1086. /* Setting the CAN device in Stop Mode. */
  1087. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  1088. /* Configuring the transmit and receive buffers. */
  1089. pch_can_config_rx_tx_buffers(priv);
  1090. /* Restore the CAN state */
  1091. pch_set_bittiming(dev);
  1092. /* Listen/Active */
  1093. pch_can_set_optmode(priv);
  1094. /* Enabling the transmit buffer. */
  1095. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1096. if (priv->msg_obj[i] == PCH_MSG_OBJ_TX) {
  1097. pch_can_set_tx_enable(priv, i + 1,
  1098. priv->tx_enable[i]);
  1099. }
  1100. }
  1101. /* Configuring the receive buffer and enabling them. */
  1102. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1103. if (priv->msg_obj[i] == PCH_MSG_OBJ_RX) {
  1104. /* Restore buffer link */
  1105. pch_can_set_rx_buffer_link(priv, i + 1,
  1106. priv->rx_link[i]);
  1107. /* Restore buffer enables */
  1108. pch_can_set_rx_enable(priv, i + 1, priv->rx_enable[i]);
  1109. }
  1110. }
  1111. /* Enable CAN Interrupts */
  1112. pch_can_set_int_custom(priv);
  1113. /* Restore Run Mode */
  1114. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  1115. return retval;
  1116. }
  1117. #else
  1118. #define pch_can_suspend NULL
  1119. #define pch_can_resume NULL
  1120. #endif
  1121. static int pch_can_get_berr_counter(const struct net_device *dev,
  1122. struct can_berr_counter *bec)
  1123. {
  1124. struct pch_can_priv *priv = netdev_priv(dev);
  1125. bec->txerr = ioread32(&priv->regs->errc) & PCH_TEC;
  1126. bec->rxerr = (ioread32(&priv->regs->errc) & PCH_REC) >> 8;
  1127. return 0;
  1128. }
  1129. static int __devinit pch_can_probe(struct pci_dev *pdev,
  1130. const struct pci_device_id *id)
  1131. {
  1132. struct net_device *ndev;
  1133. struct pch_can_priv *priv;
  1134. int rc;
  1135. int index;
  1136. void __iomem *addr;
  1137. rc = pci_enable_device(pdev);
  1138. if (rc) {
  1139. dev_err(&pdev->dev, "Failed pci_enable_device %d\n", rc);
  1140. goto probe_exit_endev;
  1141. }
  1142. rc = pci_request_regions(pdev, KBUILD_MODNAME);
  1143. if (rc) {
  1144. dev_err(&pdev->dev, "Failed pci_request_regions %d\n", rc);
  1145. goto probe_exit_pcireq;
  1146. }
  1147. addr = pci_iomap(pdev, 1, 0);
  1148. if (!addr) {
  1149. rc = -EIO;
  1150. dev_err(&pdev->dev, "Failed pci_iomap\n");
  1151. goto probe_exit_ipmap;
  1152. }
  1153. ndev = alloc_candev(sizeof(struct pch_can_priv), PCH_TX_OBJ_NUM);
  1154. if (!ndev) {
  1155. rc = -ENOMEM;
  1156. dev_err(&pdev->dev, "Failed alloc_candev\n");
  1157. goto probe_exit_alloc_candev;
  1158. }
  1159. priv = netdev_priv(ndev);
  1160. priv->ndev = ndev;
  1161. priv->regs = addr;
  1162. priv->dev = pdev;
  1163. priv->can.bittiming_const = &pch_can_bittiming_const;
  1164. priv->can.do_set_mode = pch_can_do_set_mode;
  1165. priv->can.do_get_berr_counter = pch_can_get_berr_counter;
  1166. priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
  1167. CAN_CTRLMODE_LOOPBACK;
  1168. priv->tx_obj = PCH_RX_OBJ_NUM + 1; /* Point head of Tx Obj */
  1169. ndev->irq = pdev->irq;
  1170. ndev->flags |= IFF_ECHO;
  1171. pci_set_drvdata(pdev, ndev);
  1172. SET_NETDEV_DEV(ndev, &pdev->dev);
  1173. ndev->netdev_ops = &pch_can_netdev_ops;
  1174. priv->can.clock.freq = PCH_CAN_CLK; /* Hz */
  1175. for (index = 0; index < PCH_RX_OBJ_NUM;)
  1176. priv->msg_obj[index++] = PCH_MSG_OBJ_RX;
  1177. for (index = index; index < PCH_OBJ_NUM;)
  1178. priv->msg_obj[index++] = PCH_MSG_OBJ_TX;
  1179. netif_napi_add(ndev, &priv->napi, pch_can_rx_poll, PCH_RX_OBJ_NUM);
  1180. rc = register_candev(ndev);
  1181. if (rc) {
  1182. dev_err(&pdev->dev, "Failed register_candev %d\n", rc);
  1183. goto probe_exit_reg_candev;
  1184. }
  1185. return 0;
  1186. probe_exit_reg_candev:
  1187. free_candev(ndev);
  1188. probe_exit_alloc_candev:
  1189. pci_iounmap(pdev, addr);
  1190. probe_exit_ipmap:
  1191. pci_release_regions(pdev);
  1192. probe_exit_pcireq:
  1193. pci_disable_device(pdev);
  1194. probe_exit_endev:
  1195. return rc;
  1196. }
  1197. static struct pci_driver pch_can_pci_driver = {
  1198. .name = "pch_can",
  1199. .id_table = pch_pci_tbl,
  1200. .probe = pch_can_probe,
  1201. .remove = __devexit_p(pch_can_remove),
  1202. .suspend = pch_can_suspend,
  1203. .resume = pch_can_resume,
  1204. };
  1205. static int __init pch_can_pci_init(void)
  1206. {
  1207. return pci_register_driver(&pch_can_pci_driver);
  1208. }
  1209. module_init(pch_can_pci_init);
  1210. static void __exit pch_can_pci_exit(void)
  1211. {
  1212. pci_unregister_driver(&pch_can_pci_driver);
  1213. }
  1214. module_exit(pch_can_pci_exit);
  1215. MODULE_DESCRIPTION("Controller Area Network Driver");
  1216. MODULE_LICENSE("GPL v2");
  1217. MODULE_VERSION("0.94");